diff --git a/apps/server/src/assets/AssetAccess.test.ts b/apps/server/src/assets/AssetAccess.test.ts index 681af43bdf347..a0a90df521a25 100644 --- a/apps/server/src/assets/AssetAccess.test.ts +++ b/apps/server/src/assets/AssetAccess.test.ts @@ -27,6 +27,7 @@ import { vi } from "vite-plus/test"; import * as AssetSigner from "./AssetSigner.ts"; import * as ServerSecretStore from "../auth/ServerSecretStore.ts"; +import { toolOutputImageAttachmentId } from "../attachmentStore.ts"; import * as ServerConfig from "../config.ts"; import * as Orchestrator from "../orchestration-v2/Orchestrator.ts"; import * as ProjectFaviconResolver from "../project/ProjectFaviconResolver.ts"; @@ -102,16 +103,32 @@ const oversizedScreenshotItem = { }, }; +const storedScreenshotAttachmentId = toolOutputImageAttachmentId(screenshotPng, ".png"); +const foreignAttachmentId = "thread-2-00000000-0000-4000-8000-000000000041-png"; +const storedScreenshotItem = { + ...screenshotItem, + id: TurnItemId.make("tool-screenshot-stored"), + output: { + content: [{ type: "image", mimeType: "image/png", attachmentId: storedScreenshotAttachmentId }], + }, +}; +// Rows persisted before the server stored images can carry any id a provider emitted. +const foreignScreenshotItem = { + ...screenshotItem, + id: TurnItemId.make("tool-screenshot-foreign"), + output: { + content: [{ type: "image", mimeType: "image/png", attachmentId: foreignAttachmentId }], + }, +}; + const layerTest = Layer.mergeAll( NodeHttpPlatform.layer, Layer.mock(Orchestrator.OrchestratorV2)({ getTurnItem: ({ itemId }) => Effect.succeed( - itemId === screenshotItem.id - ? screenshotItem - : itemId === oversizedScreenshotItem.id - ? oversizedScreenshotItem - : null, + [screenshotItem, oversizedScreenshotItem, storedScreenshotItem, foreignScreenshotItem].find( + (item) => item.id === itemId, + ) ?? null, ), }), layerConfig, @@ -186,31 +203,44 @@ describe("AssetAccess", () => { ); }); - it.effect("serves an image a tool returned inline from the stored item", () => + it.effect("serves an image a tool returned from the stored item or its attachment file", () => Effect.gen(function* () { + const config = yield* ServerConfig.ServerConfig; + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + yield* fs.makeDirectory(config.attachmentsDir, { recursive: true }); + for (const attachmentId of [storedScreenshotAttachmentId, foreignAttachmentId]) { + yield* fs.writeFile(path.join(config.attachmentsDir, `${attachmentId}.png`), screenshotPng); + } const resource = { _tag: "tool-output-image" as const, threadId: screenshotItem.threadId, itemId: screenshotItem.id, index: 0, }; - const result = yield* issueAssetUrl({ resource }); - expect(result.imageDimensions).toEqual({ width: 390, height: 844 }); - const suffix = result.relativeUrl.slice(`${ASSET_ROUTE_PREFIX}/`.length); - const separator = suffix.indexOf("/"); - expect(yield* resolveAsset(suffix.slice(0, separator), suffix.slice(separator + 1))).toEqual({ - kind: "bytes", - mimeType: "image/png", - bytes: Buffer.from(screenshotPng), - }); + for (const itemId of [screenshotItem.id, storedScreenshotItem.id]) { + const result = yield* issueAssetUrl({ resource: { ...resource, itemId } }); + expect(result.imageDimensions).toEqual({ width: 390, height: 844 }); + const suffix = result.relativeUrl.slice(`${ASSET_ROUTE_PREFIX}/`.length); + const separator = suffix.indexOf("/"); + expect( + yield* resolveAsset(suffix.slice(0, separator), suffix.slice(separator + 1)), + ).toEqual({ + kind: "bytes", + mimeType: "image/png", + bytes: Buffer.from(screenshotPng), + }); + } const missing = yield* issueAssetUrl({ resource: { ...resource, index: 1 } }).pipe( Effect.flip, ); expect(missing._tag).toBe("AssetWorkspaceAssetNotFoundError"); - const oversized = yield* issueAssetUrl({ - resource: { ...resource, itemId: oversizedScreenshotItem.id }, - }).pipe(Effect.flip); - expect(oversized._tag).toBe("AssetWorkspaceAssetNotFoundError"); + for (const itemId of [oversizedScreenshotItem.id, foreignScreenshotItem.id]) { + const unserved = yield* issueAssetUrl({ resource: { ...resource, itemId } }).pipe( + Effect.flip, + ); + expect(unserved._tag).toBe("AssetWorkspaceAssetNotFoundError"); + } }).pipe(Effect.provide(layerTest)), ); diff --git a/apps/server/src/assets/AssetAccess.ts b/apps/server/src/assets/AssetAccess.ts index ee211b2bdc36d..2c5bb80d757c9 100644 --- a/apps/server/src/assets/AssetAccess.ts +++ b/apps/server/src/assets/AssetAccess.ts @@ -50,7 +50,12 @@ import * as Stream from "effect/Stream"; import { base64UrlDecodeUtf8, base64UrlEncode } from "../auth/utils.ts"; import * as AssetSigner from "./AssetSigner.ts"; import { makeIsServerInternalPath } from "../auth/serverInternalPaths.ts"; -import { parseAttachmentFileExtension, resolveAttachmentPathById } from "../attachmentStore.ts"; +import { + parseAttachmentFileExtension, + resolveAttachmentPathById, + toolOutputImageAttachmentId, +} from "../attachmentStore.ts"; +import { inferImageExtension } from "../imageMime.ts"; import * as ServerConfig from "../config.ts"; import * as Orchestrator from "../orchestration-v2/Orchestrator.ts"; import { expandHomePath } from "@t3tools/provider-core/server/pathExpansion"; @@ -243,6 +248,20 @@ const readToolOutputImage = Effect.fn("AssetAccess.readToolOutputImage")(functio const item = yield* orchestrator.getTurnItem(input); const image = item?.type === "dynamic_tool" ? toolOutputImages(item.output)[input.index] : undefined; + if (image?.attachmentId !== undefined) { + const config = yield* ServerConfig.ServerConfig; + const filePath = resolveAttachmentPathById({ + attachmentsDir: config.attachmentsDir, + attachmentId: image.attachmentId, + }); + if (filePath === null) return null; + const fileSystem = yield* FileSystem.FileSystem; + const bytes = yield* fileSystem.readFile(filePath); + const extension = inferImageExtension({ mimeType: image.mimeType }); + return toolOutputImageAttachmentId(bytes, extension) === image.attachmentId + ? { mimeType: image.mimeType, bytes } + : null; + } return image?.data === undefined || image.data.length > MAX_TOOL_OUTPUT_IMAGE_BASE64_LENGTH ? null : { mimeType: image.mimeType, bytes: Buffer.from(image.data, "base64") }; diff --git a/apps/server/src/attachmentStore.ts b/apps/server/src/attachmentStore.ts index a2b531e3fa5b0..3aa3b9baa7b62 100644 --- a/apps/server/src/attachmentStore.ts +++ b/apps/server/src/attachmentStore.ts @@ -116,6 +116,13 @@ export function chatAttachmentStableKey( return `${messageId}:${index}:${digest}`; } +/** Content-addressed: the asset reader re-derives it from the file's bytes, so an id that + * provider output carried in a stored row can never resolve to a file it did not hash to. */ +export function toolOutputImageAttachmentId(bytes: Uint8Array, extension: string): string { + const digest = NodeCrypto.createHash("sha256").update(bytes).digest("hex"); + return `${createDeterministicAttachmentId("tool_output", digest)}${attachmentIdExtensionSuffix(extension)}`; +} + export function parseThreadSegmentFromAttachmentId(attachmentId: string): string | null { const normalizedId = normalizeAttachmentRelativePath(attachmentId); if (!normalizedId || normalizedId.includes("/") || normalizedId.includes(".")) { diff --git a/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts b/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts index f69444d235712..b8918e9c81270 100644 --- a/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts +++ b/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts @@ -69,7 +69,7 @@ const layerTestEventSink = EventSink.layer.pipe( ); const layerGit = GitVcsDriver.layer.pipe( - Layer.provide(ServerConfig.layerTest(process.cwd(), { prefix: "ingestor-git-" })), + Layer.provideMerge(ServerConfig.layerTest(process.cwd(), { prefix: "ingestor-git-" })), Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), ); @@ -1547,7 +1547,7 @@ layer("ProviderEventIngestorV2", (it) => { }), ); - it.effect("stores tool image bytes only where a tool-output-image asset serves them", () => + it.effect("keeps tool image bytes out of both SQLite tables, served ones included", () => Effect.gen(function* () { const now = yield* DateTime.now; const eventSink = yield* EventSink.EventSinkV2; @@ -1624,17 +1624,22 @@ layer("ProviderEventIngestorV2", (it) => { }); assert.equal(storedJson.includes(readBase64), false); - assert.equal(storedJson.includes(screenshotBase64), true); + assert.equal(storedJson.includes(screenshotBase64), false); assert.deepEqual(projectedRead?.type === "dynamic_tool" ? projectedRead.output : null, { type: "image", file: { type: "image/png", originalSize: 30_000, sizeBytes: 30_000 }, }); - assert.deepEqual( - toolOutputImages( - projectedScreenshot?.type === "dynamic_tool" ? projectedScreenshot.output : null, - ), - [{ mimeType: "image/png", data: screenshotBase64 }], + assert.equal(JSON.stringify(projectedScreenshot).includes(screenshotBase64), false); + const [stored] = toolOutputImages( + projectedScreenshot?.type === "dynamic_tool" ? projectedScreenshot.output : null, + ); + assert.equal(stored?.mimeType, "image/png"); + const config = yield* ServerConfig.ServerConfig; + const fs = yield* FileSystem.FileSystem; + const storedBytes = yield* fs.readFile( + `${config.attachmentsDir}/${stored?.attachmentId ?? "missing"}.png`, ); + assert.equal(Buffer.from(storedBytes).toString("base64"), screenshotBase64); }), ); diff --git a/apps/server/src/orchestration-v2/ProviderEventIngestor.ts b/apps/server/src/orchestration-v2/ProviderEventIngestor.ts index 6fd6e7bc3129c..8c5eb8af23de7 100644 --- a/apps/server/src/orchestration-v2/ProviderEventIngestor.ts +++ b/apps/server/src/orchestration-v2/ProviderEventIngestor.ts @@ -20,7 +20,9 @@ import { import * as Context from "effect/Context"; import * as DateTime from "effect/DateTime"; import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; +import * as Path from "effect/Path"; import * as Schema from "effect/Schema"; import * as SqlError from "effect/sql/SqlError"; @@ -30,6 +32,7 @@ import { ProviderTurnAnalytics, type ProviderTurnAnalyticsContext, } from "../telemetry/ProviderUsageAnalytics.ts"; +import * as ServerConfig from "../config.ts"; import * as GitVcsDriver from "../vcs/GitVcsDriver.ts"; import * as ProviderInstanceRegistry from "../provider/ProviderInstanceRegistry.ts"; import * as EventSink from "./EventSink.ts"; @@ -38,7 +41,10 @@ import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator"; import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter"; import { makeProviderFailureTurnItem } from "@t3tools/provider-core/server/failure"; import * as ThreadCommandExecutor from "./ThreadCommandExecutor.ts"; -import { stripUnservedToolOutputImageBytes } from "./toolOutputImageBytes.ts"; +import { + storeServedToolOutputImages, + stripUnservedToolOutputImageBytes, +} from "./toolOutputImageBytes.ts"; import { boundFileChangeForStorage } from "./WireProjection.ts"; export class ProviderEventNormalizeError extends Schema.TaggedError()( @@ -243,6 +249,9 @@ export const layer: Layer.Layer< | ProjectionStore.ProjectionStoreV2 | ThreadCommandExecutor.ThreadCommandExecutor | ProviderInstanceRegistry.ProviderInstanceRegistry + | ServerConfig.ServerConfig + | FileSystem.FileSystem + | Path.Path > = Layer.effect( ProviderEventIngestorV2, Effect.gen(function* () { @@ -254,6 +263,9 @@ export const layer: Layer.Layer< const analytics = yield* ProviderTurnAnalytics; const agentScope = yield* AgentScope; const instances = yield* ProviderInstanceRegistry.ProviderInstanceRegistry; + const imageStoreContext = yield* Effect.context< + ServerConfig.ServerConfig | FileSystem.FileSystem | Path.Path + >(); const scope = yield* Effect.scope; const completedTurnAnalytics = new Set(); @@ -574,7 +586,11 @@ export const layer: Layer.Layer< type: "turn-item.updated", threadId: input.event.turnItem.threadId, payload: boundFileChangeForStorage( - stripUnservedToolOutputImageBytes(input.event.turnItem), + stripUnservedToolOutputImageBytes( + yield* storeServedToolOutputImages(input.event.turnItem).pipe( + Effect.provide(imageStoreContext), + ), + ), ), runId: input.event.turnItem.runId, nodeId: input.event.turnItem.nodeId, diff --git a/apps/server/src/orchestration-v2/ProviderSessionManager.test.ts b/apps/server/src/orchestration-v2/ProviderSessionManager.test.ts index 3dff759550c78..c1929a210cd14 100644 --- a/apps/server/src/orchestration-v2/ProviderSessionManager.test.ts +++ b/apps/server/src/orchestration-v2/ProviderSessionManager.test.ts @@ -38,6 +38,7 @@ import { TestClock } from "effect/testing"; import { HttpServer } from "effect/http"; import { ProviderWorkspaceMissingError } from "../provider/Errors.ts"; +import * as ServerConfig from "../config.ts"; import * as ServerEnvironment from "../environment/ServerEnvironment.ts"; import * as ProjectService from "../project/ProjectService.ts"; import * as McpProviderSession from "@t3tools/provider-core/server/mcpSession"; @@ -717,6 +718,7 @@ function layerTest(input: { Layer.mock(GitVcsDriver.GitVcsDriver)({ resolveBranchName: () => Effect.succeed(null), }), + ServerConfig.layerTest(process.cwd(), { prefix: "provider-session-manager-" }), ), ), ); diff --git a/apps/server/src/orchestration-v2/testkit/ProviderReplayHarness.ts b/apps/server/src/orchestration-v2/testkit/ProviderReplayHarness.ts index 9f035b7e039b7..2e1a3bdb2392b 100644 --- a/apps/server/src/orchestration-v2/testkit/ProviderReplayHarness.ts +++ b/apps/server/src/orchestration-v2/testkit/ProviderReplayHarness.ts @@ -368,8 +368,8 @@ export function layerWithRegistry( IdAllocator.layer, ThreadCommandExecutor.layer, GitVcsDriver.layer.pipe( - Layer.provide(layerServerConfig), - Layer.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), + Layer.provideMerge(layerServerConfig), + Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), ), ), ), diff --git a/apps/server/src/orchestration-v2/toolOutputImageBytes.test.ts b/apps/server/src/orchestration-v2/toolOutputImageBytes.test.ts index 4a885dd4cf031..120283bae392b 100644 --- a/apps/server/src/orchestration-v2/toolOutputImageBytes.test.ts +++ b/apps/server/src/orchestration-v2/toolOutputImageBytes.test.ts @@ -4,10 +4,20 @@ import { MAX_TOOL_OUTPUT_IMAGES, toolOutputImages, } from "@t3tools/shared/toolOutput"; +import * as NodeServices from "@effect/platform-node/NodeServices"; +import { it as effectIt } from "@effect/vitest"; import * as DateTime from "effect/DateTime"; +import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; +import * as Layer from "effect/Layer"; +import * as PlatformError from "effect/PlatformError"; import { describe, expect, it } from "vite-plus/test"; -import { stripUnservedToolOutputImageBytes } from "./toolOutputImageBytes.ts"; +import * as ServerConfig from "../config.ts"; +import { + storeServedToolOutputImages, + stripUnservedToolOutputImageBytes, +} from "./toolOutputImageBytes.ts"; const PNG_BYTES = 3_000; const PNG_BASE64 = Buffer.alloc(PNG_BYTES, 7).toString("base64"); @@ -213,3 +223,74 @@ describe("stripUnservedToolOutputImageBytes", () => { expect(stripped.result[0]).toBe(untouched); }); }); + +describe("storeServedToolOutputImages", () => { + effectIt.effect("drops attachment ids the provider put in its own image blocks", () => + Effect.gen(function* () { + const attachmentId = "other-thread-00000000-0000-4000-8000-000000000001-png"; + for (const data of [undefined, "A".repeat(MAX_TOOL_OUTPUT_IMAGE_BASE64_LENGTH + 4)]) { + const item = toolItem({ + content: [{ type: "image", mimeType: "image/png", data, attachmentId }], + }); + const stored = stripUnservedToolOutputImageBytes(yield* storeServedToolOutputImages(item)); + expect( + toolOutputImages(stored.type === "dynamic_tool" ? stored.output : null)[0]?.attachmentId, + ).toBeUndefined(); + } + }).pipe( + Effect.provide( + ServerConfig.layerTest(process.cwd(), { prefix: "tool-output-images-" }).pipe( + Layer.provideMerge(NodeServices.layer), + ), + ), + ), + ); + + effectIt.effect("keeps an image inline until a retried write stores all of its bytes", () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + let failNextWrite = true; + const faultyFs = { + ...fs, + writeFile: (filePath: string, content: Uint8Array) => + failNextWrite + ? fs.writeFile(filePath, content.subarray(0, 10)).pipe( + Effect.andThen( + Effect.fail( + PlatformError.systemError({ + _tag: "Unknown", + module: "FileSystem", + method: "writeFile", + description: "ENOSPC", + }), + ), + ), + ) + : fs.writeFile(filePath, content), + } satisfies FileSystem.FileSystem; + const item = toolItem({ + content: [ + { type: "image", source: { type: "base64", media_type: "image/png", data: PNG_BASE64 } }, + ], + }); + const store = storeServedToolOutputImages(item).pipe( + Effect.provideService(FileSystem.FileSystem, faultyFs), + ); + + expect(yield* store).toBe(item); + failNextWrite = false; + const stored = yield* store; + const [image] = toolOutputImages(stored.type === "dynamic_tool" ? stored.output : null); + const config = yield* ServerConfig.ServerConfig; + const bytes = yield* fs.readFile(`${config.attachmentsDir}/${image?.attachmentId}.png`); + expect(Buffer.from(bytes).toString("base64")).toBe(PNG_BASE64); + expect((yield* fs.readDirectory(config.attachmentsDir)).length).toBe(1); + }).pipe( + Effect.provide( + ServerConfig.layerTest(process.cwd(), { prefix: "tool-output-images-" }).pipe( + Layer.provideMerge(NodeServices.layer), + ), + ), + ), + ); +}); diff --git a/apps/server/src/orchestration-v2/toolOutputImageBytes.ts b/apps/server/src/orchestration-v2/toolOutputImageBytes.ts index 6cde6c5448c71..f17dc75a58dac 100644 --- a/apps/server/src/orchestration-v2/toolOutputImageBytes.ts +++ b/apps/server/src/orchestration-v2/toolOutputImageBytes.ts @@ -1,10 +1,23 @@ +// @effect-diagnostics nodeBuiltinImport:off +import * as NodeCrypto from "node:crypto"; + import type { OrchestrationV2TurnItem } from "@t3tools/contracts"; +import * as Effect from "effect/Effect"; +import * as Base64 from "effect/encoding/Base64"; +import * as FileSystem from "effect/FileSystem"; +import * as Path from "effect/Path"; +import * as Predicate from "effect/Predicate"; +import * as Result from "effect/Result"; import { MAX_TOOL_OUTPUT_IMAGE_BASE64_LENGTH, readToolOutputImage, toolOutputImageBlocks, } from "@t3tools/shared/toolOutput"; +import { toolOutputImageAttachmentId } from "../attachmentStore.ts"; +import * as ServerConfig from "../config.ts"; +import { inferImageExtension } from "../imageMime.ts"; + const IMAGE_MIME_KEYS = ["mimeType", "mime_type", "media_type", "type"] as const; const IMAGE_BODY_KEYS = new Set(["data", "blob", "base64"]); const MAX_DEPTH = 32; @@ -112,3 +125,77 @@ export function stripUnservedToolOutputImageBytes( const output = strip(item.output, undefined, { output: item.output, served: undefined }, 0); return output === item.output ? item : { ...item, output }; } + +function mapOutputBlocks(value: unknown, map: (block: unknown) => unknown): unknown { + if (Array.isArray(value)) return value.map(map); + if (Predicate.isObject(value) && Array.isArray(value.content)) { + return { ...value, content: value.content.map(map) }; + } + return map(value); +} + +/** Moves served image bytes to the attachment store so SQLite rows keep only a reference. + * Ids address the bytes, so repeated updates and identical images share one file. */ +export const storeServedToolOutputImages = Effect.fn( + "ToolOutputImageBytes.storeServedToolOutputImages", +)(function* (item: OrchestrationV2TurnItem) { + if (item.type !== "dynamic_tool") return item; + const blocks = toolOutputImageBlocks(item.output); + if (blocks.length === 0) return item; + const config = yield* ServerConfig.ServerConfig; + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const replacements = new Map(); + yield* Effect.forEach( + blocks, + Effect.fnUntraced(function* (block) { + const image = readToolOutputImage(block); + if (image === null || !Predicate.isObject(block)) return; + // Provider output must not point at another thread's stored file, on any path below. + if (Object.hasOwn(block, "attachmentId")) { + const { attachmentId: _providerId, ...rest } = block; + replacements.set(block, rest); + } + if (image.data === undefined || image.data.length > MAX_TOOL_OUTPUT_IMAGE_BASE64_LENGTH) { + return; + } + const decoded = Base64.decode(image.data); + if (Result.isFailure(decoded)) return; + const bytes = decoded.success; + const extension = inferImageExtension({ mimeType: image.mimeType }); + const attachmentId = toolOutputImageAttachmentId(bytes, extension); + const filePath = path.join(config.attachmentsDir, `${attachmentId}${extension}`); + const partPath = `${filePath}.${NodeCrypto.randomUUID()}.part`; + const stored = yield* fileSystem.exists(filePath).pipe( + Effect.flatMap((exists) => + exists + ? Effect.void + : fileSystem.makeDirectory(config.attachmentsDir, { recursive: true }).pipe( + // Rename so an interrupted write never leaves a file that passes the exists check. + Effect.andThen(fileSystem.writeFile(partPath, bytes)), + Effect.andThen(fileSystem.rename(partPath, filePath)), + Effect.tapError(() => + fileSystem.remove(partPath, { force: true }).pipe(Effect.ignore), + ), + ), + ), + Effect.as(true), + Effect.catch((cause) => + Effect.logWarning("Keeping tool output image inline; storing it failed.", { + threadId: item.threadId, + itemId: item.id, + cause, + }).pipe(Effect.as(false)), + ), + ); + if (stored) + replacements.set(block, { type: "image", mimeType: image.mimeType, attachmentId }); + }), + { concurrency: 1, discard: true }, + ); + if (replacements.size === 0) return item; + return { + ...item, + output: mapOutputBlocks(item.output, (block) => replacements.get(block) ?? block), + }; +}); diff --git a/packages/shared/src/toolOutput.ts b/packages/shared/src/toolOutput.ts index e52546a87d636..a6f39bfbe58dc 100644 --- a/packages/shared/src/toolOutput.ts +++ b/packages/shared/src/toolOutput.ts @@ -253,6 +253,8 @@ export function toolOutputIndicatesFailure(text: string): boolean { export interface ToolOutputImage { readonly mimeType: string; readonly data?: string; + /** Set by the server once it moves the bytes to its attachment store. */ + readonly attachmentId?: string; } /** Raster types only, so agent SVG/HTML cannot be served inline. */ @@ -266,7 +268,10 @@ export function readToolOutputImage(block: unknown): ToolOutputImage | null { return null; } const image = { mimeType: mimeType.toLowerCase() }; - return typeof data === "string" ? { ...image, data } : image; + if (typeof data === "string") return { ...image, data }; + return source === undefined && typeof block.attachmentId === "string" + ? { ...image, attachmentId: block.attachmentId } + : image; } function outputBlocks(value: unknown): ReadonlyArray { @@ -313,7 +318,9 @@ export function mcpToolResultOutput(result: { export function omitToolOutputImageData(value: unknown): unknown { const omit = (block: unknown) => { const image = readToolOutputImage(block); - return image?.data === undefined ? block : { type: "image", mimeType: image.mimeType }; + return image?.data === undefined && image?.attachmentId === undefined + ? block + : { type: "image", mimeType: image.mimeType }; }; if (Array.isArray(value)) return value.map(omit); if (Predicate.isObject(value) && Array.isArray(value.content)) {