import '../protocol/runtime_protocol.dart'; class RuntimeCommand { const RuntimeCommand({ required this.type, this.target, this.payload = const {}, }); final String type; final String? target; final Map payload; static RuntimeCommand fromMap(Map map) { final type = map[RuntimeProtocolField.type]; if (type is! String || type.isEmpty) { throw const FormatException('RuntimeCommand.type must be a string'); } if (!RuntimeCommandType.isSupported(type)) { throw FormatException('RuntimeCommand.type is unsupported: $type'); } RuntimeProtocolSchema.ensureKnownKeys( map, allowed: RuntimeProtocolSchema.allowedCommandFields(type), context: 'RuntimeCommand.$type', ); final targetValue = map[RuntimeProtocolField.target]; if (targetValue != null && targetValue is! String) { throw const FormatException('RuntimeCommand.target must be a string'); } final payload = Map.from(map) ..remove(RuntimeProtocolField.type) ..remove(RuntimeProtocolField.target); return RuntimeCommand( type: type, target: targetValue as String?, payload: payload, ); } }