fix: tool screenshots show as images, not base64 text (#16199)
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
1 parent
2a68a777af
commit
5eb8773087
17 files changed
+551
-21
No files matched your search
@@ -1,6 +1,12 @@
|
||||
import { describe, expect, it } from "vite-plus/test";
|
||||
|
||||
import { compactDynamicToolOutput, toolOutputIndicatesFailure } from "./toolOutput.ts";
|
||||
import {
|
||||
compactDynamicToolOutput,
|
||||
MAX_TOOL_OUTPUT_IMAGES,
|
||||
omitToolOutputImageData,
|
||||
toolOutputImages,
|
||||
toolOutputIndicatesFailure,
|
||||
} from "./toolOutput.ts";
|
||||
|
||||
describe("compactDynamicToolOutput", () => {
|
||||
it("extracts IDs through the MCP result envelopes used by T3 summaries", () => {
|
||||
@@ -173,3 +179,51 @@ describe("toolOutputIndicatesFailure", () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("toolOutputImages", () => {
|
||||
const mcpImage = { type: "image", data: "AAAA", mimeType: "image/png" };
|
||||
const claudeImage = {
|
||||
type: "image",
|
||||
source: { type: "base64", media_type: "image/JPEG", data: "BBBB" },
|
||||
};
|
||||
const text = { type: "text", text: "{}" };
|
||||
|
||||
it("finds images in each place providers store them, in order", () => {
|
||||
expect(toolOutputImages({ content: [text, claudeImage, mcpImage] })).toEqual([
|
||||
{ mimeType: "image/jpeg", data: "BBBB" },
|
||||
{ mimeType: "image/png", data: "AAAA" },
|
||||
]);
|
||||
expect(toolOutputImages([mcpImage])).toEqual([{ mimeType: "image/png", data: "AAAA" }]);
|
||||
expect(toolOutputImages(mcpImage)).toEqual([{ mimeType: "image/png", data: "AAAA" }]);
|
||||
});
|
||||
|
||||
it("skips types the server must not serve inline", () => {
|
||||
expect(
|
||||
toolOutputImages([
|
||||
{ ...mcpImage, mimeType: "image/svg+xml" },
|
||||
{ ...mcpImage, mimeType: "text/html" },
|
||||
{ type: "image", source: { type: "url", url: "https://example.com/a.png" } },
|
||||
]),
|
||||
).toEqual([]);
|
||||
});
|
||||
|
||||
it("stops after the first few images", () => {
|
||||
const images = toolOutputImages(Array.from({ length: 10_000 }, () => mcpImage));
|
||||
expect(images).toHaveLength(MAX_TOOL_OUTPUT_IMAGES);
|
||||
});
|
||||
|
||||
it("drops the bytes but keeps each image's position", () => {
|
||||
const omitted = omitToolOutputImageData({ content: [text, claudeImage, mcpImage] });
|
||||
expect(omitted).toEqual({
|
||||
content: [
|
||||
text,
|
||||
{ type: "image", mimeType: "image/jpeg" },
|
||||
{ type: "image", mimeType: "image/png" },
|
||||
],
|
||||
});
|
||||
expect(toolOutputImages(omitted)).toEqual([
|
||||
{ mimeType: "image/jpeg" },
|
||||
{ mimeType: "image/png" },
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -1,3 +1,4 @@
|
||||
import { isProviderSendTurnSupportedImageMimeType } from "@t3tools/contracts";
|
||||
import * as Predicate from "effect/Predicate";
|
||||
|
||||
const MAX_PARSED_BYTES = 16_384;
|
||||
@@ -166,3 +167,69 @@ export function toolOutputIndicatesFailure(text: string): boolean {
|
||||
/exit code\s*[:\s]\s*[1-9]\d*\b/i.test(text)
|
||||
);
|
||||
}
|
||||
|
||||
/** An image a tool returned inline. `data` is base64; a detail read omits it. */
|
||||
export interface ToolOutputImage {
|
||||
readonly mimeType: string;
|
||||
readonly data?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads one image block in the MCP `{ data, mimeType }` shape or the Anthropic
|
||||
* `{ source: { type: "base64", media_type, data } }` shape Claude stores.
|
||||
* Only raster types are recognized, so the server never serves an agent's SVG
|
||||
* or HTML inline.
|
||||
*/
|
||||
export function readToolOutputImage(block: unknown): ToolOutputImage | null {
|
||||
if (!Predicate.isObject(block) || block.type !== "image") return null;
|
||||
const source = Predicate.isObject(block.source) ? block.source : undefined;
|
||||
if (source !== undefined && source.type !== "base64") return null;
|
||||
const mimeType = source === undefined ? block.mimeType : source.media_type;
|
||||
const data = source === undefined ? block.data : source.data;
|
||||
if (typeof mimeType !== "string" || !isProviderSendTurnSupportedImageMimeType(mimeType)) {
|
||||
return null;
|
||||
}
|
||||
const image = { mimeType: mimeType.toLowerCase() };
|
||||
return typeof data === "string" ? { ...image, data } : image;
|
||||
}
|
||||
|
||||
/** Tools return a block, a list of blocks, or an MCP result with a `content` list. */
|
||||
function outputBlocks(value: unknown): ReadonlyArray<unknown> {
|
||||
if (Array.isArray(value)) return value;
|
||||
if (Predicate.isObject(value) && Array.isArray(value.content)) return value.content;
|
||||
return [value];
|
||||
}
|
||||
|
||||
/** A tool returns one screenshot or a few frames; more would only flood the timeline. */
|
||||
export const MAX_TOOL_OUTPUT_IMAGES = 8;
|
||||
|
||||
/**
|
||||
* The first images in a tool output, in order. The order is the
|
||||
* `tool-output-image` asset index, so servers and clients agree on it.
|
||||
*/
|
||||
export function toolOutputImages(value: unknown): ReadonlyArray<ToolOutputImage> {
|
||||
const images: ToolOutputImage[] = [];
|
||||
for (const block of outputBlocks(value)) {
|
||||
const image = readToolOutputImage(block);
|
||||
if (image === null) continue;
|
||||
images.push(image);
|
||||
if (images.length === MAX_TOOL_OUTPUT_IMAGES) break;
|
||||
}
|
||||
return images;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces each image's bytes with `{ type: "image", mimeType }` and keeps its
|
||||
* position, so detail reads stay small and clients load the bytes as assets.
|
||||
*/
|
||||
export function omitToolOutputImageData(value: unknown): unknown {
|
||||
const omit = (block: unknown) => {
|
||||
const image = readToolOutputImage(block);
|
||||
return image?.data === undefined ? block : { type: "image", mimeType: image.mimeType };
|
||||
};
|
||||
if (Array.isArray(value)) return value.map(omit);
|
||||
if (Predicate.isObject(value) && Array.isArray(value.content)) {
|
||||
return { ...value, content: value.content.map(omit) };
|
||||
}
|
||||
return omit(value);
|
||||
}
|
||||
Reference in new issue
Block a user