Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

toolOutputImageBytes.test.ts

233 lines7.6 KB

1import { NodeId, ThreadId, TurnItemId, type OrchestrationV2TurnItem } from "@t3tools/contracts";
2import {
3 MAX_TOOL_OUTPUT_IMAGE_BASE64_LENGTH,
4 MAX_TOOL_OUTPUT_IMAGES,
5 toolOutputImages,
6} from "@t3tools/shared/toolOutput";
7import * as DateTime from "effect/DateTime";
8import { describe, expect, it } from "vite-plus/test";
9​
10import { stripUnservedToolOutputImageBytes } from "./toolOutputImageBytes.ts";
11​
12const PNG_BYTES = 3_000;
13const PNG_BASE64 = Buffer.alloc(PNG_BYTES, 7).toString("base64");
14const PADDED_BASE64 = Buffer.alloc(PNG_BYTES + 1, 9).toString("base64");
15​
16const now = DateTime.makeUnsafe("2026-10-06T00:00:00.000Z");
17​
18const itemBase = {
19 id: TurnItemId.make("turn-item:tool"),
20 threadId: ThreadId.make("thread:tool"),
21 runId: null,
22 nodeId: NodeId.make("node:tool"),
23 providerThreadId: null,
24 providerTurnId: null,
25 nativeItemRef: null,
26 parentItemId: null,
27 ordinal: 1,
28 status: "completed",
29 title: null,
30 startedAt: now,
31 completedAt: now,
32 updatedAt: now,
33} as const;
34​
35function toolItem(output: unknown, toolName = "Read"): OrchestrationV2TurnItem {
36 return {
37 ...itemBase,
38 type: "dynamic_tool",
39 toolName,
40 input: { file_path: "/tmp/shot.png" },
41 output,
42 };
44​
45function outputOf(item: OrchestrationV2TurnItem): unknown {
46 return item.type === "dynamic_tool" ? item.output : undefined;
48​
49describe("stripUnservedToolOutputImageBytes", () => {
50 it("replaces the base64 of Claude's structured Read result with its measured size", () => {
51 const item = toolItem({
52 type: "image",
53 file: {
54 base64: PNG_BASE64,
55 type: "image/png",
56 originalSize: PNG_BYTES,
57 dimensions: { originalWidth: 180, originalHeight: 180 },
58 },
59 });
60​
61 expect(outputOf(stripUnservedToolOutputImageBytes(item))).toEqual({
62 type: "image",
63 file: {
64 type: "image/png",
65 originalSize: PNG_BYTES,
66 dimensions: { originalWidth: 180, originalHeight: 180 },
67 sizeBytes: PNG_BYTES,
68 },
69 });
70 });
71​
72 it("strips ACP image shapes: Grok's native ImageContent and an MCP result behind an error", () => {
73 const item = toolItem({
74 rawOutput: { type: "ReadFile", ImageContent: { data: PNG_BASE64, mime_type: "image/png" } },
75 failed: {
76 error: "partial result",
77 result: [{ type: "image", data: PADDED_BASE64, mimeType: "image/png" }],
78 },
79 });
80​
81 expect(outputOf(stripUnservedToolOutputImageBytes(item))).toEqual({
82 rawOutput: {
83 type: "ReadFile",
84 ImageContent: { mime_type: "image/png", sizeBytes: PNG_BYTES },
85 },
86 failed: {
87 error: "partial result",
88 result: [{ type: "image", mimeType: "image/png", sizeBytes: PNG_BYTES + 1 }],
89 },
90 });
91 });
92​
93 it("strips a Cursor image part that names no mime type", () => {
94 const item = toolItem({
95 content: [{ text: { text: "captured" } }, { image: { data: PNG_BASE64 } }],
96 });
97​
98 expect(outputOf(stripUnservedToolOutputImageBytes(item))).toEqual({
99 content: [{ text: { text: "captured" } }, { image: { sizeBytes: PNG_BYTES } }],
100 });
101 });
102​
103 it("keeps the screenshots a tool-output-image asset serves", () => {
104 const claudeImage = {
105 type: "image",
106 source: { type: "base64", media_type: "image/png", data: PNG_BASE64 },
107 };
108 const mcpImage = { type: "image", data: PADDED_BASE64, mimeType: "image/jpeg" };
109 const item = toolItem(
110 { content: [{ type: "text", text: "captured" }, claudeImage, mcpImage] },
111 "mcp__t3-code__device_screenshot",
112 );
113​
114 const stripped = stripUnservedToolOutputImageBytes(item);
115​
116 expect(stripped).toBe(item);
117 expect(toolOutputImages(outputOf(stripped))).toEqual([
118 { mimeType: "image/png", data: PNG_BASE64 },
119 { mimeType: "image/jpeg", data: PADDED_BASE64 },
120 ]);
121 });
122​
123 it("strips only the images past the last one an asset can serve", () => {
124 const images = Array.from({ length: MAX_TOOL_OUTPUT_IMAGES + 2 }, () => ({
125 type: "image",
126 data: PNG_BASE64,
127 mimeType: "image/png",
128 }));
129​
130 const output = outputOf(stripUnservedToolOutputImageBytes(toolItem(images)));
131​
132 expect(Array.isArray(output) ? output.slice(0, MAX_TOOL_OUTPUT_IMAGES) : null).toEqual(
133 images.slice(0, MAX_TOOL_OUTPUT_IMAGES),
134 );
135 expect(Array.isArray(output) ? output.slice(MAX_TOOL_OUTPUT_IMAGES) : null).toEqual([
136 { type: "image", mimeType: "image/png", sizeBytes: PNG_BYTES },
137 { type: "image", mimeType: "image/png", sizeBytes: PNG_BYTES },
138 ]);
139 });
140​
141 it("strips a served-position image larger than an asset may serve", () => {
142 const oversized = "A".repeat(MAX_TOOL_OUTPUT_IMAGE_BASE64_LENGTH + 4);
143 const servable = { type: "image", data: PNG_BASE64, mimeType: "image/png" };
144 const item = toolItem({
145 content: [
146 { type: "image", source: { type: "base64", media_type: "image/png", data: oversized } },
147 servable,
148 ],
149 });
150​
151 const output = outputOf(stripUnservedToolOutputImageBytes(item)) as {
152 content: ReadonlyArray<unknown>;
153 };
154​
155 expect(output.content).toEqual([
156 {
157 type: "image",
158 source: {
159 type: "base64",
160 media_type: "image/png",
161 sizeBytes: ((MAX_TOOL_OUTPUT_IMAGE_BASE64_LENGTH + 4) / 4) * 3,
162 },
163 },
164 servable,
165 ]);
166 expect(output.content[1]).toBe(servable);
167 });
168​
169 it("keeps image fields whose value is not a base64 body", () => {
170 const item = toolItem({
171 nested: {
172 byId: { type: "image", data: "asset-id-42" },
173 svg: {
174 mimeType: "image/svg+xml",
175 data: `<svg xmlns='http://www.w3.org/2000/svg'>${"<circle r='4'/>".repeat(8)}</svg>`,
176 },
177 dataUrl: { type: "image", data: `data:image/png;base64,${PNG_BASE64}` },
178 remote: { type: "image", source: { type: "url", url: "https://example.test/shot.png" } },
179 tiny: { type: "image", data: "AA==", mimeType: "image/png" },
180 notImage: { mimeType: "application/pdf", data: PNG_BASE64 },
181 },
182 });
183​
184 expect(stripUnservedToolOutputImageBytes(item)).toBe(item);
185 });
186​
187 it("measures the size instead of trusting one the provider supplied", () => {
188 const item = toolItem({
189 type: "image",
190 file: { base64: PNG_BASE64, type: "image/png", sizeBytes: 0 },
191 });
192​
193 expect(outputOf(stripUnservedToolOutputImageBytes(item))).toEqual({
194 type: "image",
195 file: { type: "image/png", sizeBytes: PNG_BYTES },
196 });
197 });
198​
199 it("returns items without image bytes by reference", () => {
200 const nested = { files: [{ path: "src/app.ts" }] };
201 const item = toolItem({ stdout: "data: 42", data: PNG_BASE64, nested });
202 const command: OrchestrationV2TurnItem = {
203 ...itemBase,
204 type: "command_execution",
205 input: "cat shot.png | base64",
206 output: PNG_BASE64,
207 };
208​
209 const stripped = stripUnservedToolOutputImageBytes(item);
210​
211 expect(stripped).toBe(item);
212 expect(outputOf(stripped)).toBe(outputOf(item));
213 expect(stripUnservedToolOutputImageBytes(command)).toBe(command);
214 expect(stripUnservedToolOutputImageBytes(toolItem("plain text output"))).toEqual(
215 toolItem("plain text output"),
216 );
217 });
218​
219 it("does not mutate the provider's item and shares untouched siblings", () => {
220 const untouched = { type: "text", text: "captured" };
221 const output = {
222 error: "partial result",
223 result: [untouched, { type: "image", data: PNG_BASE64, mimeType: "image/png" }],
224 };
225 const item = toolItem(output);
226 const snapshot = structuredClone(output);
227​
228 const stripped = outputOf(stripUnservedToolOutputImageBytes(item)) as typeof output;
229​
230 expect(outputOf(item)).toEqual(snapshot);
231 expect(stripped.result[0]).toBe(untouched);
232 });
233});