Gitea CI / Check code and tests (pull_request) Failing after 8s
Gitea CI / Build Android APK (pull_request) Skipped
Gitea CI / Build Windows x64 app (pull_request) Skipped
Gitea CI / Audit and package Windows x64 (pull_request) Skipped
Gitea CI / Assemble and validate CI artifacts (pull_request) Skipped
Gitea CI / Gitea CI passed (pull_request) Failing after 1s
398 lines
13 KiB
Dart
398 lines
13 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:cutdex/integrations/agent/agent_models.dart';
|
|
import 'package:cutdex/integrations/agent/agent_prompt_policy.dart';
|
|
import 'package:cutdex/integrations/agent/editor_agent_tools.dart';
|
|
import 'package:cutdex_agent/cutdex_agent.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:cutdex_core/tools.dart';
|
|
import 'package:cutdex_core/editor/commands.dart';
|
|
|
|
class _CatalogHost implements EditorCommandHost {
|
|
_CatalogHost([this.operations]);
|
|
final Set<EditorOperation>? operations;
|
|
@override
|
|
Set<EditorOperation> get supportedOperations =>
|
|
operations ?? EditorOperation.values.toSet();
|
|
@override
|
|
Future<CommandResult> execute(
|
|
EditorCommand command,
|
|
CommandContext context,
|
|
) => throw UnimplementedError('Only declarations are read by this test');
|
|
}
|
|
|
|
class _SnapshotHost extends _CatalogHost {
|
|
_SnapshotHost(this.result);
|
|
final CommandResult result;
|
|
@override
|
|
Future<CommandResult> execute(
|
|
EditorCommand command,
|
|
CommandContext context,
|
|
) async => result;
|
|
}
|
|
|
|
void main() {
|
|
test('Agent capabilities do not repeat already registered schemas', () async {
|
|
final source = CommandResult(
|
|
status: CommandStatus.succeeded,
|
|
message: 'Available tools',
|
|
data: {
|
|
'tools': editorToolDefinitions.map((tool) => tool.toJson()).toList(),
|
|
},
|
|
);
|
|
final tools = editorAgentTools({
|
|
'timeline': EditorToolDispatcher(host: _SnapshotHost(source)),
|
|
});
|
|
final tool = tools.singleWhere((tool) => tool.name == 'tools_capabilities');
|
|
final result = await tool.execute(
|
|
ToolCall(
|
|
id: 'c',
|
|
name: tool.name,
|
|
arguments: {'workspaceId': 'timeline'},
|
|
),
|
|
ToolContext(
|
|
sessionId: 's',
|
|
runId: 'r',
|
|
operationId: 'c',
|
|
cancellation: CancellationToken(),
|
|
onProgress: (_) {},
|
|
),
|
|
);
|
|
final declarations = ((result.data['data'] as Map)['tools'] as List)
|
|
.cast<Map<String, Object?>>();
|
|
expect(declarations, hasLength(editorToolDefinitions.length));
|
|
expect(
|
|
declarations.map((d) => d['name']),
|
|
editorToolDefinitions.map((d) => d.name),
|
|
);
|
|
expect(declarations.every((d) => !d.containsKey('inputSchema')), isTrue);
|
|
expect((source.data['tools'] as List).first, contains('inputSchema'));
|
|
});
|
|
|
|
test(
|
|
'multi-lane mask queries retain edit values and fit repeated verification',
|
|
() async {
|
|
final parameters = [
|
|
'canvas.position.x',
|
|
'canvas.scale.x',
|
|
'canvas.rotation',
|
|
for (final mask in ['ellipse', 'hole'])
|
|
for (final field in [
|
|
'center.x',
|
|
'center.y',
|
|
'width',
|
|
'height',
|
|
'rotation',
|
|
'feather',
|
|
'shape',
|
|
'enabled',
|
|
'inverted',
|
|
'mode',
|
|
'order',
|
|
])
|
|
'mask.$mask.$field',
|
|
];
|
|
final source = CommandResult(
|
|
status: CommandStatus.succeeded,
|
|
message: 'Timeline',
|
|
data: {
|
|
'duration': {'numerator': '35', 'denominator': 1},
|
|
'saveState': 'saved',
|
|
'clips': [
|
|
for (var i = 0; i < 8; i++)
|
|
{
|
|
'clipId': 'clip-$i',
|
|
'parameters': [
|
|
for (final id
|
|
in i < 2
|
|
? parameters
|
|
: ['canvas.position.x', 'canvas.position.y'])
|
|
{
|
|
'id': id,
|
|
'defaultValue': .5,
|
|
'minimum': -1000000,
|
|
'maximum': 1000000,
|
|
'unit': '',
|
|
'keyframes': id == 'canvas.position.x'
|
|
? [
|
|
{
|
|
'sourceTime': {
|
|
'numerator': '5',
|
|
'denominator': 1,
|
|
},
|
|
'value': 0,
|
|
'interpolation': 'linear',
|
|
},
|
|
{
|
|
'sourceTime': {
|
|
'numerator': '15',
|
|
'denominator': 1,
|
|
},
|
|
'value': .15625,
|
|
'interpolation': 'linear',
|
|
},
|
|
]
|
|
: <Object?>[],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
);
|
|
final tools = editorAgentTools({
|
|
'timeline': EditorToolDispatcher(host: _SnapshotHost(source)),
|
|
});
|
|
final query = tools.singleWhere((tool) => tool.name == 'timeline_query');
|
|
final result = await query.execute(
|
|
ToolCall(
|
|
id: 'q',
|
|
name: query.name,
|
|
arguments: {'workspaceId': 'timeline'},
|
|
),
|
|
ToolContext(
|
|
sessionId: 's',
|
|
runId: 'r',
|
|
operationId: 'q',
|
|
cancellation: CancellationToken(),
|
|
onProgress: (_) {},
|
|
),
|
|
);
|
|
final data = result.data['data'] as Map;
|
|
final actual =
|
|
((data['clips'] as List).first as Map)['parameters'] as List;
|
|
final original =
|
|
((source.data['clips'] as List).first as Map)['parameters'] as List;
|
|
expect(actual.first['keyframes'], original.first['keyframes']);
|
|
expect(
|
|
actual.map((p) => p['defaultValue']),
|
|
original.map((p) => p['defaultValue']),
|
|
);
|
|
expect(data['duration'], source.data['duration']);
|
|
expect(original.first['maximum'], 1000000);
|
|
List<AgentMessage> history(String text) => [
|
|
AgentMessage(
|
|
role: MessageRole.user,
|
|
text: 'Split all lanes, verify, undo and redo.',
|
|
),
|
|
for (var i = 0; i < 5; i++) ...[
|
|
AgentMessage(
|
|
role: MessageRole.assistant,
|
|
calls: [
|
|
ToolCall(
|
|
id: 'q$i',
|
|
name: query.name,
|
|
arguments: {'workspaceId': 'timeline'},
|
|
),
|
|
],
|
|
),
|
|
AgentMessage(role: MessageRole.tool, callId: 'q$i', text: text),
|
|
],
|
|
];
|
|
final builder = HarnessContextBuilder(maxResultBytes: 1024);
|
|
final raw = jsonEncode(
|
|
ToolResult(source.message, data: source.toJson()).toJson(),
|
|
);
|
|
expect(
|
|
builder.measureInput(
|
|
system: AgentPromptPolicy.editorAssistant,
|
|
history: history(raw),
|
|
tools: tools,
|
|
),
|
|
greaterThan(builder.maxInputTokens),
|
|
);
|
|
final request = await builder.build(
|
|
system: AgentPromptPolicy.editorAssistant,
|
|
history: history(jsonEncode(result.toJson())),
|
|
tools: tools,
|
|
cancellation: CancellationToken(),
|
|
);
|
|
expect(request.messages, hasLength(11));
|
|
final fullHistory = history(jsonEncode(result.toJson()));
|
|
final changed = jsonDecode(fullHistory[4].text);
|
|
changed['data']['data']['clips'][0]['parameters'][1]['defaultValue'] =
|
|
.75;
|
|
fullHistory[4] = AgentMessage.fromJson({
|
|
...fullHistory[4].toJson(),
|
|
'text': jsonEncode(changed),
|
|
});
|
|
final projection = HarnessContextBuilder(maxResultBytes: 1024);
|
|
final bounded = await projection.build(
|
|
system: AgentPromptPolicy.editorAssistant,
|
|
history: fullHistory,
|
|
tools: tools,
|
|
cancellation: CancellationToken(),
|
|
);
|
|
expect(bounded.messages, hasLength(fullHistory.length));
|
|
expect(jsonDecode(bounded.messages[4].text)['resultId'], 'q1');
|
|
expect(jsonDecode(bounded.messages[4].text)['omitted'], isTrue);
|
|
expect(fullHistory[4].text, contains('"defaultValue":0.75'));
|
|
final snapshot = SessionSnapshot(id: 's', history: fullHistory);
|
|
final reader = projection
|
|
.runtimeTools(() => snapshot, tools)
|
|
.singleWhere((tool) => tool.name == 'read_tool_result');
|
|
final recovered = await reader.execute(
|
|
ToolCall(
|
|
id: 'read',
|
|
name: reader.name,
|
|
arguments: {
|
|
'resultId': 'q1',
|
|
'path': '/data/data/clips/0/parameters/1/defaultValue',
|
|
},
|
|
),
|
|
ToolContext(
|
|
sessionId: 's',
|
|
runId: 'r',
|
|
operationId: 'read',
|
|
cancellation: CancellationToken(),
|
|
onProgress: (_) {},
|
|
),
|
|
);
|
|
expect(recovered.data['value'], .75);
|
|
expect(
|
|
bounded.messages.map((m) => m.callId),
|
|
fullHistory.map((m) => m.callId),
|
|
);
|
|
},
|
|
);
|
|
|
|
test('workspace discovery identifies timeline tools and rejects application routing', () async {
|
|
final tools = editorAgentTools({
|
|
'application': EditorToolDispatcher(
|
|
host: _CatalogHost({EditorOperation.queryProjects}),
|
|
),
|
|
'timeline-id': EditorToolDispatcher(
|
|
host: _CatalogHost({
|
|
EditorOperation.queryTimeline,
|
|
EditorOperation.split,
|
|
}),
|
|
),
|
|
});
|
|
final listing = await tools.first.execute(
|
|
ToolCall(id: 'list', name: 'cutdex_workspaces'),
|
|
ToolContext(
|
|
sessionId: 'session',
|
|
runId: 'run',
|
|
operationId: 'op',
|
|
cancellation: CancellationToken(),
|
|
onProgress: (_) {},
|
|
),
|
|
);
|
|
final data = jsonDecode(listing.text);
|
|
final targets = data['targets'] as List;
|
|
expect(
|
|
data['toolSets'][targets.last['toolSetId']],
|
|
contains('timeline_split'),
|
|
);
|
|
expect(
|
|
data['toolSets'][targets.first['toolSetId']],
|
|
isNot(contains('timeline_split')),
|
|
);
|
|
final query = tools.firstWhere((tool) => tool.name == 'timeline_query');
|
|
expect(
|
|
query.validate({'workspaceId': 'application'}),
|
|
contains('timeline-id'),
|
|
);
|
|
expect(query.validate({'workspaceId': 'timeline-id'}), isNull);
|
|
expect(
|
|
query.validate({'workspaceId': 'timeline-typo'}),
|
|
contains('timeline-id'),
|
|
);
|
|
});
|
|
|
|
test(
|
|
'large workspace discovery is lossless and shares a single tool catalog',
|
|
() async {
|
|
final dispatcher = EditorToolDispatcher(host: _CatalogHost());
|
|
final tool = editorAgentTools({
|
|
for (var i = 0; i < 100; i++) 'timeline-$i': dispatcher,
|
|
}).first;
|
|
final listing = await tool.execute(
|
|
ToolCall(id: 'list', name: tool.name),
|
|
ToolContext(
|
|
sessionId: 's',
|
|
runId: 'r',
|
|
operationId: 'list',
|
|
cancellation: CancellationToken(),
|
|
onProgress: (_) {},
|
|
),
|
|
);
|
|
final data = jsonDecode(listing.text);
|
|
expect(data['targets'], hasLength(100));
|
|
expect(data['toolSets'], hasLength(1));
|
|
final original = AgentMessage(
|
|
role: MessageRole.tool,
|
|
callId: 'list',
|
|
text: jsonEncode(listing.toJson()),
|
|
);
|
|
final projected = HarnessContextBuilder(maxResultBytes: 1024)
|
|
.projectHistory([original]);
|
|
expect(jsonDecode(projected.single.text)['resultId'], 'list');
|
|
expect(original.text, jsonEncode(listing.toJson()));
|
|
},
|
|
);
|
|
|
|
test('workspace growth does not duplicate IDs into every tool schema', () {
|
|
final dispatcher = EditorToolDispatcher(host: _CatalogHost());
|
|
final one = editorAgentTools({'first': dispatcher});
|
|
final many = editorAgentTools({
|
|
for (var i = 0; i < 100; i++) 'project/timeline-$i': dispatcher,
|
|
});
|
|
String schema(List<AgentTool> tools) =>
|
|
jsonEncode(tools.map((t) => t.declaration).toList());
|
|
expect(schema(many), schema(one));
|
|
final operation = many.firstWhere(
|
|
(tool) => tool.name != 'cutdex_workspaces',
|
|
);
|
|
expect(operation.validate.call({'workspaceId': 'missing'}), isNotNull);
|
|
});
|
|
test(
|
|
'greeting loads runtime tools without the full editor catalog',
|
|
() async {
|
|
final tools = editorAgentTools({}, dynamicWorkspaces: true);
|
|
const model = AgentModelSettings(id: 'custom-model');
|
|
final builder = HarnessContextBuilder(
|
|
directTools: const {'cutdex_workspaces'},
|
|
maxInputTokens: model.effectiveMaxInputTokens,
|
|
maxOutputTokens: model.effectiveMaxOutputTokens,
|
|
);
|
|
final history = [AgentMessage(role: MessageRole.user, text: 'hi')];
|
|
final input = builder.measureInput(
|
|
system: AgentPromptPolicy.editorAssistant,
|
|
history: history,
|
|
tools: tools,
|
|
);
|
|
// Keep a real catalog regression rather than a small placeholder tool.
|
|
expect(tools.length, greaterThan(20));
|
|
final request = await builder.build(
|
|
system: AgentPromptPolicy.editorAssistant,
|
|
history: history,
|
|
tools: [
|
|
...tools,
|
|
...builder.runtimeTools(
|
|
() => SessionSnapshot(id: 's', history: history),
|
|
tools,
|
|
),
|
|
],
|
|
cancellation: CancellationToken(),
|
|
);
|
|
expect(
|
|
request.tools.map((t) => t.name),
|
|
unorderedEquals([
|
|
'cutdex_workspaces',
|
|
'tool_search',
|
|
'read_tool_result',
|
|
'update_plan',
|
|
]),
|
|
);
|
|
expect(request.messages.single.text, 'hi');
|
|
final projectedInput = builder.measureInput(
|
|
system: request.system,
|
|
history: request.messages,
|
|
tools: request.tools,
|
|
);
|
|
expect(projectedInput, lessThan(input));
|
|
expect(projectedInput, lessThan(model.effectiveMaxInputTokens * .8));
|
|
},
|
|
);
|
|
}
|