feat(engine): 添加技能查看工具并优化异步任务管理 - 添加SkillViewTool到引擎加载器中,增强技能管理功能 - 在AgentLoop中引入_active_direct_task来跟踪活跃任务 - 实现直接任务执行时的同步处理逻辑 - 更新工具实例化方式以支持依赖注入 feat(config): 增加智能体运行时参数配置支持 - 扩展AgentDefaultsConfig添加max_tokens和temperature字段 - 实现配置解析函数_first_config_value处理多个配置源 - 支持通过Web API动态更新智能体运行时参数 - 添加前端页面配置表单和验证逻辑 refactor(provider): 统一最大令牌数参数类型为可选整型 - 将所有LLM提供者的max_tokens参数改为int | None类型 - 为AnthropicProvider实现模型特定的最大令牌数默认值 - 调整参数传递逻辑,优先级:调用参数 > 配置文件 > 模型默认值 - 移除硬编码的默认值,改用条件判断 feat(process): 增强事件投影功能 - 添加工具调用开始/结束事件的映射逻辑 - 实现技能激活事件的识别和展示 - 添加辅助函数处理工具调用名称和参数提取 - 优化运行记录关联逻辑,提升事件匹配准确性 fix(web): 更新网络请求客户端信任环境设置 - 将WebFetchTool和WebSearchTool的trust_env参数设为True - 确保HTTP客户端能够正确使用系统代理配置 - 修复可能的网络连接问题 test: 添加配置加载和事件投影相关测试 - 新增智能体默认参数配置测试用例 - 实现API配置持久化和重载测试 - 添加技能卡片和工具事件的投影测试 ```
470 lines
14 KiB
TypeScript
470 lines
14 KiB
TypeScript
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,
|
|
};
|
|
}
|
|
|
|
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: '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: '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: '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: '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('可以');
|
|
});
|
|
|
|
it('uses the latest assistant message from the acceptance run as the result body', () => {
|
|
const task = makeTask({
|
|
status: 'awaiting_acceptance',
|
|
updated_at: '2026-05-26T10:04:00.000Z',
|
|
run_ids: ['run-main'],
|
|
runs: [
|
|
{
|
|
run_id: 'run-main',
|
|
title: '主 Agent',
|
|
session_id: 'web:default',
|
|
messages: [
|
|
{ role: 'assistant', content: 'Draft answer', created_at: '2026-05-26T10:03:00.000Z' },
|
|
{ role: 'assistant', content: 'Final user-visible answer', created_at: '2026-05-26T10:04:00.000Z' },
|
|
],
|
|
},
|
|
],
|
|
});
|
|
const processEvents: ProcessEvent[] = [
|
|
{
|
|
event_id: 'evt-result-ready',
|
|
run_id: 'run-main',
|
|
parent_run_id: null,
|
|
kind: 'task_result_ready',
|
|
actor_type: 'system',
|
|
actor_id: 'evidence',
|
|
actor_name: 'Evidence',
|
|
text: 'The task result is ready for user acceptance.',
|
|
created_at: '2026-05-26T10:04:00.000Z',
|
|
metadata: {
|
|
result_summary: 'Summary should not replace the final answer.',
|
|
},
|
|
},
|
|
];
|
|
|
|
const cards = buildTaskTimelineCards({ task, processEvents });
|
|
const result = cards.find((card) => card.type === 'result');
|
|
|
|
expect(result?.summary).toBe('Final user-visible answer');
|
|
expect(result?.details?.result_summary).toBe('Summary should not replace the final answer.');
|
|
});
|
|
|
|
it('collapses previous result and acceptance cards into a history pack', () => {
|
|
const task = makeTask({
|
|
status: 'awaiting_acceptance',
|
|
updated_at: '2026-05-26T10:12:00.000Z',
|
|
run_ids: ['run-1', 'run-2'],
|
|
feedback: [
|
|
{
|
|
acceptance_type: 'revise',
|
|
comment: 'Add decisions',
|
|
created_at: '2026-05-26T10:06:00.000Z',
|
|
run_id: 'run-1',
|
|
},
|
|
],
|
|
runs: [
|
|
{
|
|
run_id: 'run-1',
|
|
title: '主 Agent',
|
|
session_id: 'web:default',
|
|
messages: [{ role: 'assistant', content: 'Version one answer', created_at: '2026-05-26T10:05:00.000Z' }],
|
|
},
|
|
{
|
|
run_id: 'run-2',
|
|
title: '主 Agent',
|
|
session_id: 'web:default',
|
|
messages: [{ role: 'assistant', content: 'Version two answer', created_at: '2026-05-26T10:12:00.000Z' }],
|
|
},
|
|
],
|
|
});
|
|
const processEvents: ProcessEvent[] = [
|
|
{
|
|
event_id: 'evt-result-1',
|
|
run_id: 'run-1',
|
|
parent_run_id: null,
|
|
kind: 'task_result_ready',
|
|
actor_type: 'system',
|
|
actor_id: 'evidence',
|
|
actor_name: 'Evidence',
|
|
text: 'Result one ready.',
|
|
created_at: '2026-05-26T10:05:00.000Z',
|
|
},
|
|
{
|
|
event_id: 'evt-plan-2',
|
|
run_id: 'run-2',
|
|
parent_run_id: null,
|
|
kind: 'task_planned',
|
|
actor_type: 'system',
|
|
actor_id: 'planner',
|
|
actor_name: 'Task Planner',
|
|
text: 'Second attempt planned.',
|
|
created_at: '2026-05-26T10:08:00.000Z',
|
|
},
|
|
{
|
|
event_id: 'evt-result-2',
|
|
run_id: 'run-2',
|
|
parent_run_id: null,
|
|
kind: 'task_result_ready',
|
|
actor_type: 'system',
|
|
actor_id: 'evidence',
|
|
actor_name: 'Evidence',
|
|
text: 'Result two ready.',
|
|
created_at: '2026-05-26T10:12:00.000Z',
|
|
},
|
|
];
|
|
|
|
const cards = buildTaskTimelineCards({ task, processEvents });
|
|
|
|
expect(cards.map((card) => card.type)).toEqual([
|
|
'task_created',
|
|
'result_history',
|
|
'plan',
|
|
'result',
|
|
]);
|
|
const history = cards.find((card) => card.type === 'result_history');
|
|
expect(history?.summary).toBe('1 历史结果版本');
|
|
expect(history?.details?.versions).toEqual([
|
|
expect.objectContaining({
|
|
runId: 'run-1',
|
|
result: 'Version one answer',
|
|
acceptanceType: 'revise',
|
|
comment: 'Add decisions',
|
|
}),
|
|
]);
|
|
expect(cards.find((card) => card.id === 'evt-plan-2')).toBeTruthy();
|
|
expect(cards.at(-1)?.summary).toBe('Version two answer');
|
|
});
|
|
|
|
it('does not add fallback progress when a child run already has progress events', () => {
|
|
const task = makeTask();
|
|
const processRuns: ProcessRun[] = [
|
|
{
|
|
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: 'running',
|
|
started_at: '2026-05-26T10:01:00.000Z',
|
|
},
|
|
];
|
|
const processEvents: ProcessEvent[] = [
|
|
{
|
|
event_id: 'evt-progress',
|
|
run_id: 'run-research',
|
|
parent_run_id: 'run-main',
|
|
kind: 'run_progress',
|
|
actor_type: 'agent',
|
|
actor_id: 'research-agent',
|
|
actor_name: 'Research Agent',
|
|
text: 'Reading source documents.',
|
|
created_at: '2026-05-26T10:02:00.000Z',
|
|
},
|
|
];
|
|
|
|
const cards = buildTaskTimelineCards({ task, processRuns, processEvents });
|
|
|
|
expect(cards.filter((card) => card.runId === 'run-research' && card.type === 'agent_progress')).toHaveLength(1);
|
|
expect(cards.map((card) => card.id)).not.toContain('run-research:fallback-progress');
|
|
});
|
|
|
|
it('marks a tool call as finished when a matching tool result exists', () => {
|
|
const task = makeTask();
|
|
const processEvents: ProcessEvent[] = [
|
|
{
|
|
event_id: 'evt-tool-start',
|
|
run_id: 'run-main',
|
|
parent_run_id: null,
|
|
kind: 'tool_call_started',
|
|
actor_type: 'mcp',
|
|
actor_id: 'web_search',
|
|
actor_name: 'web_search',
|
|
text: 'Calling tool: web_search.',
|
|
status: 'running',
|
|
created_at: '2026-05-26T10:02:00.000Z',
|
|
metadata: {
|
|
tool_call_id: 'call-1',
|
|
tool_name: 'web_search',
|
|
},
|
|
},
|
|
{
|
|
event_id: 'evt-tool-finish',
|
|
run_id: 'run-main',
|
|
parent_run_id: null,
|
|
kind: 'tool_call_finished',
|
|
actor_type: 'mcp',
|
|
actor_id: 'web_search',
|
|
actor_name: 'web_search',
|
|
text: 'Search failed.',
|
|
status: 'error',
|
|
created_at: '2026-05-26T10:03:00.000Z',
|
|
metadata: {
|
|
tool_call_id: 'call-1',
|
|
tool_name: 'web_search',
|
|
result_summary: 'Search failed.',
|
|
},
|
|
},
|
|
];
|
|
|
|
const cards = buildTaskTimelineCards({ task, processEvents });
|
|
|
|
expect(cards.find((card) => card.id === 'evt-tool-start')?.status).toBe('error');
|
|
expect(cards.find((card) => card.id === 'evt-tool-finish')?.type).toBe('tool_result');
|
|
expect(cards.find((card) => card.id === 'evt-tool-finish')?.summary).toBe('Search failed.');
|
|
});
|
|
|
|
it('maps agent_finished events without timeline metadata to agent progress cards', () => {
|
|
const task = makeTask();
|
|
const processEvents: ProcessEvent[] = [
|
|
{
|
|
event_id: 'evt-agent-finished',
|
|
run_id: 'run-research',
|
|
parent_run_id: 'run-main',
|
|
kind: 'agent_finished',
|
|
actor_type: 'agent',
|
|
actor_id: 'research-agent',
|
|
actor_name: 'Research Agent',
|
|
text: 'Finished reading source documents.',
|
|
status: 'done',
|
|
created_at: '2026-05-26T10:02:00.000Z',
|
|
},
|
|
];
|
|
|
|
const cards = buildTaskTimelineCards({ task, processEvents });
|
|
|
|
expect(cards.find((card) => card.id === 'evt-agent-finished')?.type).toBe('agent_progress');
|
|
});
|
|
|
|
it('sorts invalid timestamps after valid timestamps while preserving insertion order', () => {
|
|
const task = makeTask();
|
|
const processEvents: ProcessEvent[] = [
|
|
{
|
|
event_id: 'evt-invalid-date',
|
|
run_id: 'run-main',
|
|
parent_run_id: null,
|
|
kind: 'task_planned',
|
|
actor_type: 'agent',
|
|
actor_id: 'main-agent',
|
|
actor_name: 'Main Agent',
|
|
text: 'Plan created.',
|
|
created_at: 'not-a-date',
|
|
},
|
|
];
|
|
|
|
const cards = buildTaskTimelineCards({ task, processEvents });
|
|
|
|
expect(cards.map((card) => card.id)).toEqual(['task-1:created', 'evt-invalid-date']);
|
|
});
|
|
|
|
it('dedupes synthetic result and acceptance milestones when lifecycle events exist', () => {
|
|
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 processEvents: ProcessEvent[] = [
|
|
{
|
|
event_id: 'evt-result-ready',
|
|
run_id: 'run-main',
|
|
parent_run_id: null,
|
|
kind: 'task_result_ready',
|
|
actor_type: 'agent',
|
|
actor_id: 'main-agent',
|
|
actor_name: 'Main Agent',
|
|
text: 'Result is ready.',
|
|
created_at: '2026-05-26T10:04:00.000Z',
|
|
},
|
|
{
|
|
event_id: 'evt-acceptance-recorded',
|
|
run_id: 'run-main',
|
|
parent_run_id: null,
|
|
kind: 'task_acceptance_recorded',
|
|
actor_type: 'user',
|
|
actor_id: 'user-acceptance',
|
|
actor_name: 'User Acceptance',
|
|
text: '可以',
|
|
created_at: '2026-05-26T10:05:02.000Z',
|
|
metadata: {
|
|
acceptance_type: 'accept',
|
|
},
|
|
},
|
|
];
|
|
|
|
const cards = buildTaskTimelineCards({ task, processEvents });
|
|
|
|
expect(cards.filter((card) => card.type === 'result')).toHaveLength(1);
|
|
expect(cards.filter((card) => card.type === 'acceptance')).toHaveLength(1);
|
|
expect(cards.map((card) => card.id)).toContain('evt-result-ready');
|
|
expect(cards.map((card) => card.id)).toContain('evt-acceptance-recorded');
|
|
});
|
|
});
|