feat: add task timeline model
This commit is contained in:
172
app-instance/frontend/lib/task-timeline.test.ts
Normal file
172
app-instance/frontend/lib/task-timeline.test.ts
Normal file
@ -0,0 +1,172 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import { buildTaskTimelineCards } from '@/lib/task-timeline';
|
||||
import type { BackendTask, ProcessArtifact, ProcessEvent, ProcessRun } from '@/types';
|
||||
|
||||
function makeTask(overrides: Partial<BackendTask> = {}): BackendTask {
|
||||
return {
|
||||
task_id: 'task-1',
|
||||
session_id: 'web:default',
|
||||
description: 'Research the market',
|
||||
short_title: 'Market research',
|
||||
is_open: true,
|
||||
goal: 'Summarize the market',
|
||||
constraints: [],
|
||||
priority: 1,
|
||||
status: 'running',
|
||||
creator: 'user',
|
||||
created_at: '2026-05-26T10:00:00.000Z',
|
||||
updated_at: '2026-05-26T10:00:00.000Z',
|
||||
run_ids: ['run-main'],
|
||||
skill_names: [],
|
||||
feedback: [],
|
||||
metadata: {},
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
function eventKind(kind: string): ProcessEvent['kind'] {
|
||||
return kind as ProcessEvent['kind'];
|
||||
}
|
||||
|
||||
describe('buildTaskTimelineCards', () => {
|
||||
it('builds ordered timeline cards from task process data', () => {
|
||||
const task = makeTask();
|
||||
const processRuns: ProcessRun[] = [
|
||||
{
|
||||
run_id: 'run-main',
|
||||
parent_run_id: null,
|
||||
session_id: 'web:default',
|
||||
actor_type: 'agent',
|
||||
actor_id: 'main-agent',
|
||||
actor_name: 'Main Agent',
|
||||
title: 'Plan and coordinate',
|
||||
status: 'running',
|
||||
started_at: '2026-05-26T10:00:30.000Z',
|
||||
},
|
||||
{
|
||||
run_id: 'run-research',
|
||||
parent_run_id: 'run-main',
|
||||
session_id: 'web:default',
|
||||
actor_type: 'agent',
|
||||
actor_id: 'research-agent',
|
||||
actor_name: 'Research Agent',
|
||||
title: 'Read source documents',
|
||||
status: 'done',
|
||||
started_at: '2026-05-26T10:05:00.000Z',
|
||||
finished_at: '2026-05-26T10:05:30.000Z',
|
||||
summary: 'Finished reading source documents.',
|
||||
},
|
||||
];
|
||||
const processEvents: ProcessEvent[] = [
|
||||
{
|
||||
event_id: 'evt-plan',
|
||||
run_id: 'run-main',
|
||||
parent_run_id: null,
|
||||
kind: eventKind('task_planned'),
|
||||
actor_type: 'agent',
|
||||
actor_id: 'main-agent',
|
||||
actor_name: 'Main Agent',
|
||||
text: 'Plan created.',
|
||||
created_at: '2026-05-26T10:01:00.000Z',
|
||||
},
|
||||
{
|
||||
event_id: 'evt-skill',
|
||||
run_id: 'run-main',
|
||||
parent_run_id: null,
|
||||
kind: eventKind('skill_selected'),
|
||||
actor_type: 'system',
|
||||
actor_id: 'skill-router',
|
||||
actor_name: 'Skill Router',
|
||||
text: 'Research skill selected.',
|
||||
created_at: '2026-05-26T10:02:00.000Z',
|
||||
metadata: {
|
||||
selected_skill_names: ['research'],
|
||||
reason: 'Need source review.',
|
||||
},
|
||||
},
|
||||
{
|
||||
event_id: 'evt-tool-start',
|
||||
run_id: 'run-research',
|
||||
parent_run_id: 'run-main',
|
||||
kind: eventKind('tool_call_started'),
|
||||
actor_type: 'mcp',
|
||||
actor_id: 'document-reader',
|
||||
actor_name: 'Document Reader',
|
||||
text: 'Reading source documents.',
|
||||
created_at: '2026-05-26T10:03:00.000Z',
|
||||
},
|
||||
{
|
||||
event_id: 'evt-tool-finish',
|
||||
run_id: 'run-research',
|
||||
parent_run_id: 'run-main',
|
||||
kind: eventKind('tool_call_finished'),
|
||||
actor_type: 'mcp',
|
||||
actor_id: 'document-reader',
|
||||
actor_name: 'Document Reader',
|
||||
text: 'Documents read.',
|
||||
created_at: '2026-05-26T10:04:00.000Z',
|
||||
metadata: {
|
||||
result_summary: '2 documents read successfully.',
|
||||
},
|
||||
},
|
||||
];
|
||||
const processArtifacts: ProcessArtifact[] = [
|
||||
{
|
||||
artifact_id: 'artifact-summary',
|
||||
run_id: 'run-research',
|
||||
actor_type: 'agent',
|
||||
actor_id: 'research-agent',
|
||||
actor_name: 'Research Agent',
|
||||
title: 'Research summary',
|
||||
artifact_type: 'markdown',
|
||||
content: '# Summary',
|
||||
created_at: '2026-05-26T10:06:00.000Z',
|
||||
},
|
||||
];
|
||||
|
||||
const cards = buildTaskTimelineCards({
|
||||
task,
|
||||
processRuns,
|
||||
processEvents,
|
||||
processArtifacts,
|
||||
});
|
||||
|
||||
expect(cards.map((card) => card.type)).toEqual([
|
||||
'task_created',
|
||||
'plan',
|
||||
'skill',
|
||||
'tool_call',
|
||||
'tool_result',
|
||||
'agent_progress',
|
||||
'artifact',
|
||||
]);
|
||||
expect(cards[1].title).toBe('执行计划');
|
||||
expect(cards[2].title).toBe('选择 Skill');
|
||||
expect(cards[4].summary).toBe('2 documents read successfully.');
|
||||
expect(cards[6].relatedArtifactIds).toEqual(['artifact-summary']);
|
||||
});
|
||||
|
||||
it('appends result and acceptance cards for closed tasks with feedback', () => {
|
||||
const task = makeTask({
|
||||
is_open: false,
|
||||
status: 'closed',
|
||||
updated_at: '2026-05-26T10:04:00.000Z',
|
||||
closed_at: '2026-05-26T10:04:00.000Z',
|
||||
feedback: [
|
||||
{
|
||||
acceptance_type: 'accept',
|
||||
comment: '可以',
|
||||
created_at: '2026-05-26T10:05:00.000Z',
|
||||
run_id: 'run-main',
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const cards = buildTaskTimelineCards({ task });
|
||||
|
||||
expect(cards.at(-2)?.type).toBe('result');
|
||||
expect(cards.at(-1)?.type).toBe('acceptance');
|
||||
expect(cards.at(-1)?.summary).toContain('可以');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user