38 lines
1.9 KiB
TypeScript
38 lines
1.9 KiB
TypeScript
/**
|
|
* hoistToolImages — OpenAI 규격은 tool 메시지에 이미지를 못 싣는다(Gemini 400 "Invalid content part type: image_url" 실측).
|
|
*/
|
|
import { test, describe } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { hoistToolImages } from '../src/providers/openai-compat-adapter';
|
|
|
|
const IMG = { type: 'image_url', image_url: { url: 'data:image/png;base64,AAAA' } };
|
|
|
|
describe('hoistToolImages', () => {
|
|
test('tool 메시지의 이미지를 빼서 tool 뒤 user 메시지로 옮긴다', () => {
|
|
const out = hoistToolImages([
|
|
{ role: 'user', content: '차트 그려줘' },
|
|
{ role: 'assistant', content: '', tool_calls: [{ id: 'c1', type: 'function', function: { name: 'python_eval', arguments: '{}' } }] } as any,
|
|
{ role: 'tool', tool_call_id: 'c1', content: [{ type: 'text', text: 'ok' }, IMG] } as any,
|
|
]);
|
|
assert.equal(out.length, 4);
|
|
assert.equal(out[2].role, 'tool');
|
|
assert.equal(out[2].content, 'ok');
|
|
assert.equal(out[3].role, 'user');
|
|
assert.ok((out[3].content as any[]).some(p => p.type === 'image_url'));
|
|
});
|
|
test('도구 호출이 둘이면 tool 메시지는 연속, 이미지는 마지막 tool 뒤에 한 번', () => {
|
|
const out = hoistToolImages([
|
|
{ role: 'assistant', content: '', tool_calls: [{ id: 'a' }, { id: 'b' }] } as any,
|
|
{ role: 'tool', tool_call_id: 'a', content: [{ type: 'text', text: 'A' }, IMG] } as any,
|
|
{ role: 'tool', tool_call_id: 'b', content: [{ type: 'text', text: 'B' }, IMG] } as any,
|
|
{ role: 'user', content: '다음' },
|
|
]);
|
|
assert.deepEqual(out.map(m => m.role), ['assistant', 'tool', 'tool', 'user', 'user']);
|
|
assert.equal((out[3].content as any[]).filter(p => p.type === 'image_url').length, 2);
|
|
});
|
|
test('이미지 없으면 그대로(동일 참조)', () => {
|
|
const msgs = [{ role: 'user', content: 'hi' }, { role: 'tool', content: 'text only' }] as any;
|
|
assert.equal(hoistToolImages(msgs), msgs);
|
|
});
|
|
});
|