|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { |
| 3 | + piAiAssistantMessageToGenAiMessage, |
| 4 | + piAiContentToString, |
| 5 | + piAiMessagesToGenAiMessages, |
| 6 | +} from '../../../../src/ai/pi-ai/messages'; |
| 7 | + |
| 8 | +describe('convert pi-ai messages to gen_ai messages', () => { |
| 9 | + it('maps user, assistant and tool result messages', () => { |
| 10 | + expect( |
| 11 | + piAiMessagesToGenAiMessages([ |
| 12 | + { role: 'user', content: 'Weather in Vienna?', timestamp: 1 }, |
| 13 | + { |
| 14 | + role: 'assistant', |
| 15 | + content: [ |
| 16 | + { type: 'thinking', thinking: 'The user wants the weather.' }, |
| 17 | + { type: 'text', text: 'Let me check.' }, |
| 18 | + { type: 'toolCall', id: 'call_1', name: 'get_weather', arguments: { city: 'Vienna' } }, |
| 19 | + ], |
| 20 | + stopReason: 'toolUse', |
| 21 | + }, |
| 22 | + { |
| 23 | + role: 'toolResult', |
| 24 | + toolCallId: 'call_1', |
| 25 | + toolName: 'get_weather', |
| 26 | + content: [{ type: 'text', text: 'Sunny in Vienna.' }], |
| 27 | + isError: false, |
| 28 | + }, |
| 29 | + ]), |
| 30 | + ).toStrictEqual([ |
| 31 | + { role: 'user', parts: [{ type: 'text', content: 'Weather in Vienna?' }] }, |
| 32 | + { |
| 33 | + role: 'assistant', |
| 34 | + parts: [ |
| 35 | + { type: 'reasoning', content: 'The user wants the weather.' }, |
| 36 | + { type: 'text', content: 'Let me check.' }, |
| 37 | + { type: 'tool_call', id: 'call_1', name: 'get_weather', arguments: '{"city":"Vienna"}' }, |
| 38 | + ], |
| 39 | + }, |
| 40 | + { |
| 41 | + role: 'tool', |
| 42 | + parts: [{ type: 'tool_call_response', id: 'call_1', name: 'get_weather', result: 'Sunny in Vienna.' }], |
| 43 | + }, |
| 44 | + ]); |
| 45 | + }); |
| 46 | + |
| 47 | + it('reports images by media type and drops their base64 data', () => { |
| 48 | + expect( |
| 49 | + piAiMessagesToGenAiMessages([ |
| 50 | + { |
| 51 | + role: 'user', |
| 52 | + content: [ |
| 53 | + { type: 'text', text: 'What is this?' }, |
| 54 | + { type: 'image', data: 'iVBORw0KGgo=', mimeType: 'image/png' }, |
| 55 | + ], |
| 56 | + }, |
| 57 | + ]), |
| 58 | + ).toStrictEqual([ |
| 59 | + { |
| 60 | + role: 'user', |
| 61 | + parts: [ |
| 62 | + { type: 'text', content: 'What is this?' }, |
| 63 | + { type: 'blob', mime_type: 'image/png' }, |
| 64 | + ], |
| 65 | + }, |
| 66 | + ]); |
| 67 | + }); |
| 68 | + |
| 69 | + it('leaves out system messages, redacted thinking and messages without content', () => { |
| 70 | + expect( |
| 71 | + piAiMessagesToGenAiMessages([ |
| 72 | + { role: 'system', content: '', sections: { preamble: 'You are helpful.' } }, |
| 73 | + { role: 'assistant', content: [{ type: 'thinking', thinking: 'c2VjcmV0', redacted: true }] }, |
| 74 | + { role: 'user', content: '' }, |
| 75 | + 'not a message', |
| 76 | + ]), |
| 77 | + ).toStrictEqual([]); |
| 78 | + }); |
| 79 | + |
| 80 | + it('keeps unknown part kinds as objects', () => { |
| 81 | + expect(piAiMessagesToGenAiMessages([{ role: 'user', content: [{ type: 'audio', url: 'a.mp3' }] }])).toStrictEqual([ |
| 82 | + { role: 'user', parts: [{ type: 'object', content: { type: 'audio', url: 'a.mp3' } }] }, |
| 83 | + ]); |
| 84 | + }); |
| 85 | + |
| 86 | + it('maps an assistant message to an output message with its finish reason', () => { |
| 87 | + expect( |
| 88 | + piAiAssistantMessageToGenAiMessage( |
| 89 | + { |
| 90 | + role: 'assistant', |
| 91 | + content: [{ type: 'toolCall', id: 'call_1', name: 'get_weather', arguments: { city: 'Vienna' } }], |
| 92 | + }, |
| 93 | + 'tool_call', |
| 94 | + ), |
| 95 | + ).toStrictEqual({ |
| 96 | + role: 'assistant', |
| 97 | + parts: [{ type: 'tool_call', id: 'call_1', name: 'get_weather', arguments: '{"city":"Vienna"}' }], |
| 98 | + finish_reason: 'tool_call', |
| 99 | + }); |
| 100 | + expect(piAiAssistantMessageToGenAiMessage({ role: 'assistant', content: [] }, 'stop')).toBeUndefined(); |
| 101 | + }); |
| 102 | + |
| 103 | + it('renders text-only content as text and other content as mapped parts', () => { |
| 104 | + expect( |
| 105 | + piAiContentToString([ |
| 106 | + { type: 'text', text: 'line 1' }, |
| 107 | + { type: 'text', text: 'line 2' }, |
| 108 | + ]), |
| 109 | + ).toBe('line 1\nline 2'); |
| 110 | + expect(piAiContentToString([{ type: 'image', data: 'iVBORw0KGgo=', mimeType: 'image/png' }])).toBe( |
| 111 | + '[{"type":"blob","mime_type":"image/png"}]', |
| 112 | + ); |
| 113 | + }); |
| 114 | +}); |
0 commit comments