Files
cutdex/lib/features/assistant/agent_message_codec.dart
T
lingjhf 27e77fff39 feat: initial Cutdex application
Add the Flutter video editing application with project management, media
import, timeline editing, preview, AI assistance, diagnostics and settings.

Include shared UI and native host packages, platform runners, application
tests and release tooling.

Provide usage and contribution guides, the MIT license and third-party
notices. Exclude local credentials, build outputs and validation data.
2026-10-05 23:36:23 +08:00

80 lines
2.4 KiB
Dart

import 'dart:convert';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'agent_media_attachments.dart';
import 'composer_document.dart';
part 'agent_message_codec.freezed.dart';
part 'agent_message_codec.g.dart';
@freezed
abstract class AgentComposerMessage with _$AgentComposerMessage {
const AgentComposerMessage._();
const factory AgentComposerMessage({
required String text,
required List<AgentMediaAttachment> attachments,
required List<ComposerSegment> segments,
}) = _AgentComposerMessage;
factory AgentComposerMessage.fromJson(Map<String, Object?> json) =>
_$AgentComposerMessageFromJson(json);
List<ComposerReference> get references => [
for (final segment in segments)
if (segment case ComposerReferenceSegment(:final value)) value,
];
static const _prefix = '[Cutdex composer message] ';
static const _separator = '\n\n';
static String encodeDocument(
List<ComposerSegment> segments,
List<AgentMediaAttachment> attachments,
) => _encode(
AgentComposerMessage(
text: segments.map((segment) => segment.displayText).join().trim(),
attachments: attachments,
segments: segments,
),
);
static String _encode(AgentComposerMessage message) {
if (message.attachments.isEmpty && message.references.isEmpty) {
return message.text;
}
final data = jsonEncode(message.toJson());
return '$_prefix$data$_separator'
'These references identify project resources by stable ID, not file '
'contents. A referenced timeline is context, not an instruction to '
"switch the user's current timeline. Resolve each ID with media_query "
'or the relevant project tool when needed.$_separator${message.text}';
}
static AgentComposerMessage decode(String value) {
AgentComposerMessage plain() => AgentComposerMessage(
text: value,
attachments: const [],
segments: const [],
);
if (!value.startsWith(_prefix)) return plain();
final end = value.indexOf(_separator, _prefix.length);
if (end < 0) return plain();
try {
final data = jsonDecode(value.substring(_prefix.length, end));
return AgentComposerMessage.fromJson(
(data as Map).cast<String, Object?>(),
);
} on FormatException {
return plain();
} on CheckedFromJsonException {
return plain();
} on TypeError {
return plain();
} on ArgumentError {
return plain();
}
}
}