toolTimeline.test.ts 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { describe, expect, it } from 'vitest'
  2. import { chatMessagesToUi } from '@/components/workspace/viewModels'
  3. import { normalizeChatMessage } from '@/utils/normalize'
  4. import { buildMessageTimelineRows } from '@/utils/turnTrace'
  5. describe('ordinary chat tool timeline', () => {
  6. it('folds persisted function calls and their result rows into one expandable tool', () => {
  7. const user = normalizeChatMessage({ id: 'user-row', role: 'user', content: 'MEDIA:/tmp/keep-raw.png', timestamp: 0 }, 'session-1', 'default')
  8. const call = normalizeChatMessage({
  9. id: 'assistant-call', role: 'assistant', content: '', tool_calls: JSON.stringify([{
  10. id: 'call-1', type: 'function', function: { name: 'terminal', arguments: '{"command":"pwd"}' },
  11. }]), timestamp: 1,
  12. }, 'session-1', 'default')
  13. const result = normalizeChatMessage({
  14. id: 'tool-result', role: 'tool', tool_call_id: 'call-1', tool_name: 'terminal',
  15. content: '{"output":"/tmp/work","exit_code":0,"error":null}', timestamp: 2,
  16. }, 'session-1', 'default')
  17. const system = normalizeChatMessage({ id: 'system-row', role: 'system', content: 'transport detail', timestamp: 3 }, 'session-1', 'default')
  18. const timeline = chatMessagesToUi([user, call, result, system])
  19. expect(timeline).toHaveLength(2)
  20. expect(timeline[0]).toMatchObject({ role: 'user', content: 'MEDIA:/tmp/keep-raw.png' })
  21. expect(timeline[1].role).toBe('assistant')
  22. expect(timeline[1].tools).toEqual([expect.objectContaining({
  23. id: 'call-1', name: 'terminal', status: 'success', input: '{"command":"pwd"}', output: { output: '/tmp/work', exit_code: 0, error: null },
  24. })])
  25. })
  26. it('renders asynchronous delegation completion as a timeline event, not a user message', () => {
  27. const notice = normalizeChatMessage({
  28. id: 'delegation', role: 'user', content: '[ASYNC DELEGATION BATCH COMPLETE]',
  29. display_kind: 'async_delegation_complete',
  30. display_metadata: JSON.stringify({ task_count: 2, completed_count: 2, failed_count: 0, duration_seconds: 71 }),
  31. timestamp: 1,
  32. }, 'session-1', 'default')
  33. const [timeline] = chatMessagesToUi([notice])
  34. expect(timeline).toMatchObject({
  35. role: 'system', timelineKind: 'delegation-complete',
  36. timelineMetadata: { task_count: 2, completed_count: 2 },
  37. })
  38. })
  39. it('groups one turn of reasoning and tools before its visible assistant reply', () => {
  40. const rows = buildMessageTimelineRows([
  41. { id: 'user', role: 'user', content: '检查一下' },
  42. { id: 'thinking', role: 'assistant', content: '', reasoning: '先读取配置', tools: [{ id: 'read', name: 'read_file', status: 'success', input: { path: 'a' }, output: 'ok' }] },
  43. { id: 'tool-two', role: 'assistant', content: '', reasoning: '再执行命令', tools: [{ id: 'run', name: 'terminal', status: 'running', input: { command: 'pwd' } }] },
  44. { id: 'answer', role: 'assistant', content: '检查完成', reasoning: '组织答案' },
  45. ])
  46. expect(rows.map(row => [row.kind, row.id])).toEqual([
  47. ['message', 'message:user'],
  48. ['trace', 'trace:thinking'],
  49. ['message', 'message:answer'],
  50. ])
  51. const trace = rows[1]
  52. expect(trace?.kind === 'trace' ? trace.entries.map(entry => entry.type) : []).toEqual(['reasoning', 'tool', 'reasoning', 'tool', 'reasoning'])
  53. expect(trace?.kind === 'trace' ? trace.status : '').toBe('running')
  54. expect(rows[2]?.kind === 'message' ? rows[2].message : undefined).toMatchObject({ content: '检查完成', reasoning: undefined, tools: undefined })
  55. })
  56. it('does not merge assistant traces across agents or system events', () => {
  57. const rows = buildMessageTimelineRows([
  58. { id: 'a', role: 'assistant', author: '甲', content: '', reasoning: '甲思考' },
  59. { id: 'b', role: 'assistant', author: '乙', content: '', reasoning: '乙思考' },
  60. { id: 'system', role: 'system', content: '系统信息' },
  61. { id: 'c', role: 'assistant', author: '乙', content: '', reasoning: '新回合' },
  62. ])
  63. expect(rows.map(row => row.id)).toEqual(['trace:a', 'trace:b', 'message:system', 'trace:c'])
  64. })
  65. })