|
1 | 1 | import { describe, it, expect } from 'vitest' |
2 | 2 | import { parseMemoryCommand, getMessageText } from '../server/utils/agent-commands' |
| 3 | +import { sanitizeUIMessagesForAgent } from '../server/utils/agent-messages' |
3 | 4 | import type { UIMessage } from 'ai' |
4 | 5 |
|
5 | 6 | describe('parseMemoryCommand', () => { |
@@ -55,3 +56,60 @@ describe('getMessageText', () => { |
55 | 56 | expect(getMessageText(m)).toBe('') |
56 | 57 | }) |
57 | 58 | }) |
| 59 | + |
| 60 | +describe('sanitizeUIMessagesForAgent', () => { |
| 61 | + it('drops assistant messages with only in-progress tool parts', () => { |
| 62 | + const messages = [ |
| 63 | + { id: '1', role: 'user', parts: [{ type: 'text', text: 'hi' }] }, |
| 64 | + { |
| 65 | + id: '2', |
| 66 | + role: 'assistant', |
| 67 | + parts: [ |
| 68 | + { |
| 69 | + type: 'tool-searchKnowledgeBase', |
| 70 | + state: 'input-streaming', |
| 71 | + toolCallId: 'call-1', |
| 72 | + }, |
| 73 | + ], |
| 74 | + }, |
| 75 | + ] as unknown as UIMessage[] |
| 76 | + expect(sanitizeUIMessagesForAgent(messages)).toEqual([messages[0]]) |
| 77 | + }) |
| 78 | + |
| 79 | + it('drops synthetic tool parts without toolCallId', () => { |
| 80 | + const messages = [ |
| 81 | + { id: '1', role: 'user', parts: [{ type: 'text', text: 'q' }] }, |
| 82 | + { |
| 83 | + id: '2', |
| 84 | + role: 'assistant', |
| 85 | + parts: [ |
| 86 | + { type: 'text', text: 'answer with [1]' }, |
| 87 | + { |
| 88 | + type: 'tool-searchKnowledgeBase', |
| 89 | + state: 'output-available', |
| 90 | + output: { sources: [{ chunkId: 'c1' }] }, |
| 91 | + }, |
| 92 | + ], |
| 93 | + }, |
| 94 | + ] as unknown as UIMessage[] |
| 95 | + const out = sanitizeUIMessagesForAgent(messages) |
| 96 | + expect(out).toHaveLength(2) |
| 97 | + expect(out[1].parts).toEqual([{ type: 'text', text: 'answer with [1]' }]) |
| 98 | + }) |
| 99 | + |
| 100 | + it('keeps completed tool parts that include toolCallId', () => { |
| 101 | + const toolPart = { |
| 102 | + type: 'tool-searchKnowledgeBase', |
| 103 | + state: 'output-available', |
| 104 | + toolCallId: 'call-1', |
| 105 | + input: { query: 'test' }, |
| 106 | + output: { context: 'ctx', sources: [], results: [], count: 0 }, |
| 107 | + } |
| 108 | + const messages = [ |
| 109 | + { id: '1', role: 'user', parts: [{ type: 'text', text: 'q' }] }, |
| 110 | + { id: '2', role: 'assistant', parts: [toolPart] }, |
| 111 | + ] as unknown as UIMessage[] |
| 112 | + const out = sanitizeUIMessagesForAgent(messages) |
| 113 | + expect(out[1].parts).toContainEqual(toolPart) |
| 114 | + }) |
| 115 | +}) |
0 commit comments