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
@@ -0,0 +1,55 @@
|
||||
import { ThreadId, TurnItemId } from "@t3tools/contracts";
|
||||
import * as DateTime from "effect/DateTime";
|
||||
import { describe, expect, it } from "vite-plus/test";
|
||||
|
||||
import { turnItemOutputImages, turnItemOutputText } from "./itemDetail.ts";
|
||||
|
||||
const screenshot = {
|
||||
id: TurnItemId.make("tool-screenshot"),
|
||||
type: "dynamic_tool" as const,
|
||||
threadId: ThreadId.make("thread-1"),
|
||||
runId: null,
|
||||
nodeId: null,
|
||||
providerThreadId: null,
|
||||
providerTurnId: null,
|
||||
nativeItemRef: null,
|
||||
parentItemId: null,
|
||||
ordinal: 1,
|
||||
status: "completed" as const,
|
||||
title: null,
|
||||
toolName: "mcp__t3-code__device_screenshot",
|
||||
input: { deviceId: "phone" },
|
||||
// What a detail read returns: the image's position without its bytes.
|
||||
output: {
|
||||
content: [
|
||||
{ type: "text", text: "Captured the home screen." },
|
||||
{ type: "image", mimeType: "image/png" },
|
||||
],
|
||||
},
|
||||
startedAt: DateTime.makeUnsafe("2026-10-05T00:00:00.000Z"),
|
||||
completedAt: DateTime.makeUnsafe("2026-10-05T00:00:01.000Z"),
|
||||
updatedAt: DateTime.makeUnsafe("2026-10-05T00:00:01.000Z"),
|
||||
};
|
||||
|
||||
describe("tool output images", () => {
|
||||
it("shows a screenshot as an image asset, not as text", () => {
|
||||
expect(turnItemOutputImages(screenshot)).toEqual([
|
||||
{
|
||||
_tag: "tool-output-image",
|
||||
threadId: screenshot.threadId,
|
||||
itemId: screenshot.id,
|
||||
index: 0,
|
||||
},
|
||||
]);
|
||||
expect(turnItemOutputText(screenshot)).toBe("Captured the home screen.");
|
||||
expect(
|
||||
turnItemOutputText({ ...screenshot, output: { content: [screenshot.output.content[1]] } }),
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
it("keeps a placeholder for images it cannot show", () => {
|
||||
const output = { content: [{ type: "image", mimeType: "image/svg+xml" }] };
|
||||
expect(turnItemOutputImages({ ...screenshot, output })).toEqual([]);
|
||||
expect(turnItemOutputText({ ...screenshot, output })).toBe("[image]");
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,5 @@
|
||||
import type { OrchestrationV2TurnItem } from "@t3tools/contracts";
|
||||
import type { AssetResource, OrchestrationV2TurnItem } from "@t3tools/contracts";
|
||||
import { readToolOutputImage, toolOutputImages } from "@t3tools/shared/toolOutput";
|
||||
import * as DateTime from "effect/DateTime";
|
||||
|
||||
const MAX_TEXT_BLOCK_DEPTH = 4;
|
||||
@@ -17,11 +18,14 @@ function textFromBlocks(value: unknown, depth: number): string | null {
|
||||
if (typeof value === "string") return value;
|
||||
if (Array.isArray(value)) {
|
||||
const parts = value.map((block) => textFromBlocks(block, depth + 1));
|
||||
return parts.every((part) => part !== null) ? parts.join("\n") : null;
|
||||
return parts.every((part) => part !== null)
|
||||
? parts.filter((part) => part !== "").join("\n")
|
||||
: null;
|
||||
}
|
||||
if (!isRecord(value)) return null;
|
||||
if (value.type === "text" && typeof value.text === "string") return value.text;
|
||||
if (value.type === "image") return "[image]";
|
||||
// Images clients can show render on their own (see turnItemOutputImages).
|
||||
if (value.type === "image") return readToolOutputImage(value) ? "" : "[image]";
|
||||
if (value.type === "resource_link" && typeof value.uri === "string") return value.uri;
|
||||
if (value.type === "resource" && isRecord(value.resource)) {
|
||||
const resource = value.resource;
|
||||
@@ -200,6 +204,22 @@ export function turnItemOutputText(item: OrchestrationV2TurnItem): string | null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Images in a fetched item's tool output, as assets. The detail read leaves
|
||||
* the bytes out, so each loads over HTTP by its index.
|
||||
*/
|
||||
export function turnItemOutputImages(
|
||||
item: OrchestrationV2TurnItem,
|
||||
): ReadonlyArray<Extract<AssetResource, { readonly _tag: "tool-output-image" }>> {
|
||||
if (item.type !== "dynamic_tool" || item.outputOmitted === true) return [];
|
||||
return toolOutputImages(item.output).map((_, index) => ({
|
||||
_tag: "tool-output-image",
|
||||
threadId: item.threadId,
|
||||
itemId: item.id,
|
||||
index,
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether expanding the item shows anything. Rows without content must not
|
||||
* offer a disclosure, otherwise they open to an empty panel.
|
||||
|
||||
Reference in new issue
Block a user