AI-native design editor. Open-source Figma alternative.
fix(core): send image tool results in the AI SDK's file shape (#857)
Danila Poyarkov31e2e84b087f
diff --git a/packages/core/src/tools/ai-adapter.ts b/packages/core/src/tools/ai-adapter.tsindex 1b5170531..42160aa08 100644--- a/packages/core/src/tools/ai-adapter.ts+++ b/packages/core/src/tools/ai-adapter.ts@@ -6,9 +6,7 @@// eslint-disable-next-line open-pencil/no-mixed-case-acronym-identifiers -- Upstream export spelling.import { toStandardJsonSchema as toStandardJSONSchema } from '@valibot/to-json-schema'-import type { ToolSet, tool as createTool } from 'ai'--import type { JSONObject } from '@open-pencil/scene-graph/primitives'+import type { JSONValue, ToolResultPart, ToolSet, tool as createTool } from 'ai'import type { FigmaAPI } from '#core/figma-api'@@ -202,19 +200,24 @@ export function toolsToAI(}}- // Image results reach the model as media, with their metadata as text.- toolOpts.toModelOutput = ({ output }: { output: unknown }) => {+ // Image results reach the model as files, with their metadata as text. Typed against the+ // SDK, because a shape it does not know fails the next step's prompt validation.+ toolOpts.toModelOutput = ({ output }: { output: unknown }): ToolResultPart['output'] => {if (isImageOutput(output)) {const { base64, mimeType, ...metadata } = output- const media = { type: 'media' as const, mediaType: mimeType, data: base64 }+ const image = {+ type: 'file' as const,+ mediaType: mimeType,+ data: { type: 'data' as const, data: base64 }+ }return Object.keys(metadata).length > 0? {- type: 'content' as const,- value: [{ type: 'text' as const, text: JSON.stringify(metadata) }, media]+ type: 'content',+ value: [{ type: 'text', text: JSON.stringify(metadata) }, image]}- : { type: 'content' as const, value: [media] }+ : { type: 'content', value: [image] }}- return { type: 'json' as const, value: output as JSONObject }+ return { type: 'json', value: output as JSONValue }}result[def.name] = tool(toolOpts as never)diff --git a/packages/core/tests/tools/ai-adapter.test.ts b/packages/core/tests/tools/ai-adapter.test.tsindex 1b50bb018..0077c8332 100644--- a/packages/core/tests/tools/ai-adapter.test.ts+++ b/packages/core/tests/tools/ai-adapter.test.ts@@ -1,5 +1,6 @@import { describe, expect, test } from 'bun:test'+import { toolModelMessageSchema } from 'ai'import * as v from 'valibot'import { FigmaAPI } from '@open-pencil/core/figma-api'@@ -40,17 +41,36 @@ function hasModelOutput(value: unknown): value is ModelOutputTool {}describe('AI adapter model output', () => {- test('sends any image result as media with its metadata as text', () => {+ test('sends any image result as a file with its metadata as text', () => {const output = { mimeType: 'image/png', base64: 'AAAA', changedPixels: 12 }expect(adapt(output).toModelOutput({ output })).toEqual({type: 'content',value: [{ type: 'text', text: '{"changedPixels":12}' },- { type: 'media', mediaType: 'image/png', data: 'AAAA' }+ { type: 'file', mediaType: 'image/png', data: { type: 'data', data: 'AAAA' } }]})})+ test.each([+ { mimeType: 'image/png', base64: 'AAAA', changedPixels: 12 },+ { mimeType: 'image/png', base64: 'AAAA' },+ { mimeType: 'application/pdf', base64: 'AAAA' }+ ])('produces a tool message the AI SDK accepts in the next prompt (%p)', (output) => {+ const message = {+ role: 'tool',+ content: [+ {+ type: 'tool-result',+ toolCallId: 'call',+ toolName: 'image_tool',+ output: adapt(output).toModelOutput({ output })+ }+ ]+ }+ expect(toolModelMessageSchema.safeParse(message).success).toBe(true)+ })+test('keeps non-image results as JSON', () => {const output = { mimeType: 'application/pdf', base64: 'AAAA' }expect(adapt(output).toModelOutput({ output })).toEqual({ type: 'json', value: output })