toolTimeline.test.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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: '', reasoning_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].reasoning).toBe('先检查目录')
  23. expect(timeline[1].tools).toEqual([expect.objectContaining({
  24. id: 'call-1', name: 'terminal', status: 'success', input: '{"command":"pwd"}', output: { output: '/tmp/work', exit_code: 0, error: null },
  25. })])
  26. })
  27. it('renders asynchronous delegation completion as a timeline event, not a user message', () => {
  28. const notice = normalizeChatMessage({
  29. id: 'delegation', role: 'user', content: '[ASYNC DELEGATION BATCH COMPLETE]',
  30. display_kind: 'async_delegation_complete',
  31. display_metadata: JSON.stringify({ task_count: 2, completed_count: 2, failed_count: 0, duration_seconds: 71 }),
  32. timestamp: 1,
  33. }, 'session-1', 'default')
  34. const [timeline] = chatMessagesToUi([notice])
  35. expect(timeline).toMatchObject({
  36. role: 'system', timelineKind: 'delegation-complete',
  37. timelineMetadata: { task_count: 2, completed_count: 2 },
  38. })
  39. })
  40. it('classifies a persisted context compaction envelope as a system timeline event', () => {
  41. const compacted = normalizeChatMessage({
  42. id: 'compacted', role: 'user', content: '[CONTEXT COMPACTION — REFERENCE ONLY]\n\n## Historical Task Snapshot', timestamp: 1,
  43. }, 'session-1', 'default')
  44. const [timeline] = chatMessagesToUi([compacted])
  45. expect(timeline).toMatchObject({
  46. id: 'compacted', role: 'system', timelineKind: 'system', timelineMetadata: { eventKind: 'compaction' },
  47. })
  48. })
  49. it('groups one turn of reasoning and tools before its visible assistant reply', () => {
  50. const rows = buildMessageTimelineRows([
  51. { id: 'user', role: 'user', content: '检查一下' },
  52. { id: 'thinking', role: 'assistant', content: '', reasoning: '先读取配置', tools: [{ id: 'read', name: 'read_file', status: 'success', input: { path: 'a' }, output: 'ok' }] },
  53. { id: 'tool-two', role: 'assistant', content: '', reasoning: '再执行命令', tools: [{ id: 'run', name: 'terminal', status: 'running', input: { command: 'pwd' } }] },
  54. { id: 'answer', role: 'assistant', content: '检查完成', reasoning: '组织答案' },
  55. ])
  56. expect(rows.map(row => [row.kind, row.id])).toEqual([
  57. ['message', 'message:user'],
  58. ['trace', 'trace:thinking'],
  59. ['message', 'message:answer'],
  60. ])
  61. const trace = rows[1]
  62. expect(trace?.kind === 'trace' ? trace.entries.map(entry => entry.type) : []).toEqual(['reasoning', 'tool', 'reasoning', 'tool', 'reasoning'])
  63. expect(trace?.kind === 'trace' ? trace.status : '').toBe('running')
  64. expect(rows[2]?.kind === 'message' ? rows[2].message : undefined).toMatchObject({ content: '检查完成', reasoning: undefined, tools: undefined })
  65. })
  66. it('keeps a later streaming trace below sealed interim commentary', () => {
  67. const rows = buildMessageTimelineRows([
  68. { id: 'user', role: 'user', content: '检查项目' },
  69. { id: 'interim', role: 'assistant', content: '我先读取配置。', status: 'settled' },
  70. {
  71. id: 'streaming', role: 'assistant', content: '配置没有问题。', status: 'streaming',
  72. tools: [{ id: 'read', name: 'read_file', status: 'running' }],
  73. },
  74. ])
  75. expect(rows.map(row => [row.kind, row.id])).toEqual([
  76. ['message', 'message:user'],
  77. ['message', 'message:interim'],
  78. ['trace', 'trace:streaming'],
  79. ['message', 'message:streaming'],
  80. ])
  81. })
  82. it('does not merge assistant traces across agents or system events', () => {
  83. const rows = buildMessageTimelineRows([
  84. { id: 'a', role: 'assistant', author: '甲', content: '', reasoning: '甲思考' },
  85. { id: 'b', role: 'assistant', author: '乙', content: '', reasoning: '乙思考' },
  86. { id: 'system', role: 'system', content: '系统信息' },
  87. { id: 'c', role: 'assistant', author: '乙', content: '', reasoning: '新回合' },
  88. ])
  89. expect(rows.map(row => row.id)).toEqual(['trace:a', 'trace:b', 'message:system', 'trace:c'])
  90. })
  91. })