fix(proxy): stringify tool_result images for OpenAI upstreams

This commit is contained in:
seilk
2026-04-30 00:55:45 +09:00
parent b33277bf93
commit f6bb31c956
2 changed files with 45 additions and 22 deletions
+12 -1
View File
@@ -219,7 +219,18 @@ function convertToolResultContent(content: unknown, isError: boolean, label: str
}
if (parsed.type === 'image') {
throw new Error(`${label}[${index}].type "image" is not supported in tool_result content`);
const source =
typeof parsed.source === 'object' && parsed.source !== null
? (parsed.source as Record<string, unknown>)
: undefined;
const description =
source?.type === 'url' && typeof source.url === 'string'
? source.url
: source?.type === 'base64' && typeof source.media_type === 'string'
? `${source.media_type} base64 payload`
: 'unsupported image payload';
parts.push(`[tool_result image omitted: ${description}]`);
continue;
}
if (typeof parsed.text === 'string') {
@@ -189,27 +189,39 @@ describe('ProxyRequestTransformer regressions', () => {
).toThrow('must start with tool_result blocks for pending tool_use ids');
});
it('rejects tool_result content that cannot be represented as OpenAI tool text', () => {
expect(() =>
new ProxyRequestTransformer().transform({
messages: [
{
role: 'assistant',
content: [{ type: 'tool_use', id: 'toolu_1', name: 'vision', input: { detail: 'high' } }],
},
{
role: 'user',
content: [
{
type: 'tool_result',
tool_use_id: 'toolu_1',
content: [{ type: 'image', source: { type: 'url', url: 'https://example.com/error.png' } }],
},
],
},
],
})
).toThrow('type "image" is not supported in tool_result content');
it('converts tool_result image blocks to text placeholders for OpenAI-compatible tool messages', () => {
const result = new ProxyRequestTransformer().transform({
messages: [
{
role: 'assistant',
content: [{ type: 'tool_use', id: 'toolu_1', name: 'vision', input: { detail: 'high' } }],
},
{
role: 'user',
content: [
{
type: 'tool_result',
tool_use_id: 'toolu_1',
content: [
{ type: 'text', text: 'screenshot captured' },
{ type: 'image', source: { type: 'url', url: 'https://example.com/error.png' } },
{
type: 'image',
source: { type: 'base64', media_type: 'image/png', data: 'ZmFrZQ==' },
},
],
},
],
},
],
});
expect(result.messages[1]).toEqual({
role: 'tool',
tool_call_id: 'toolu_1',
content:
'screenshot captured\n[tool_result image omitted: https://example.com/error.png]\n[tool_result image omitted: image/png base64 payload]',
});
});
it('rejects unsupported assistant blocks instead of silently dropping them', () => {