Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 compactDynamicToolOutput,
5 compactT3McpToolOutput,
6 MAX_TOOL_OUTPUT_IMAGES,
7 mcpToolResultOutput,
8 SEND_TO_USER_MAX_LENGTH,
9 sentMessageFromToolItem,
10 toolOutputImages,
11 toolOutputIndicatesFailure,
12} from "./toolOutput.ts";
13
14describe("compactDynamicToolOutput", () => {
15 it("extracts IDs through the MCP result envelopes used by T3 summaries", () => {
16 const metadata = { threadId: "thread-1", messageId: "message-1" };
17 const json = JSON.stringify({ ...metadata, response: "Private response body" });
18 for (const value of [
19 { ...metadata, response: "Private response body" },
20 { structuredContent: JSON.parse(json), content: "Ignored alternative body" },
21 [{ type: "text", text: json }],
22 { content: [{ text: { text: json } }], isError: false },
23 json,
24 ]) {
25 expect(compactDynamicToolOutput(value)).toEqual(metadata);
26 }
27 });
28
29 it("keeps a send_to_user message up to its limit, even in 3-byte characters", () => {
30 const longest = "界".repeat(SEND_TO_USER_MAX_LENGTH);
31 for (const sentMessage of ["Draft:\n\n> Hi Sam,", longest]) {
32 const json = JSON.stringify({ sentMessage, message: "Shown to the user." });
33 expect(compactDynamicToolOutput([{ type: "text", text: json }])).toEqual({ sentMessage });
34 }
35 expect(compactDynamicToolOutput({ sentMessage: `${longest}界` })).toBeUndefined();
36 // OpenCode 2 Code Mode reports a script's nested send_to_user call as one `execute` item.
37 const output = JSON.stringify({ sentMessage: "Draft", message: "Shown to the user." });
38 expect(sentMessageFromToolItem({ toolName: "execute", output })).toBe("Draft");
39 expect(compactT3McpToolOutput("t3-code.send_to_user", output)).toEqual({
40 sentMessage: "Draft",
41 });
42 expect(
43 compactT3McpToolOutput("t3-code.preview_recording_stop", {
44 structuredContent: { id: "thread-rec-1", path: "/tmp/rec.webm" },
45 }),
46 ).toEqual({ id: "thread-rec-1" });
47 expect(
48 compactT3McpToolOutput("t3-code.preview_recording_stop", { isError: true, id: "ignored" }),
49 ).toEqual({ isError: true });
50 expect(compactT3McpToolOutput("other.send_to_user", output)).toBeUndefined();
51 });
52
53 it("keeps nested thread identity and scalar task IDs without result bodies", () => {
54 const output = {
55 thread: { threadId: "thread-1", messages: ["Private message"] },
56 taskId: "task-1",
57 scheduledTaskId: "scheduled-1",
58 status: "failed",
59 summary: "A child failed; the tool itself succeeded.",
60 unlinked: [{ repository: "acme/app", state: "merged", numbers: [1] }],
61 unlinkedCount: 1,
62 linkedCount: 2,
63 };
64 expect(compactDynamicToolOutput(output)).toEqual({
65 thread: { threadId: "thread-1" },
66 taskId: "task-1",
67 scheduledTaskId: "scheduled-1",
68 linkedCount: 2,
69 unlinkedCount: 1,
70 });
71 expect(output.thread.messages).toEqual(["Private message"]);
72 });
73
74 it("keeps explicit envelope failure while dropping error text and arbitrary output", () => {
75 for (const failure of [
76 { isError: true },
77 { is_error: true },
78 { _tag: "OrchestratorMcpFailure" },
79 { error: { message: "Private error" } },
80 ]) {
81 expect(
82 compactDynamicToolOutput({
83 ...failure,
84 structuredContent: { threadId: "thread-1", error: "Private details" },
85 }),
86 ).toEqual({ isError: true, threadId: "thread-1" });
87 }
88 expect(compactDynamicToolOutput("Private plain text")).toBeUndefined();
89 expect(compactDynamicToolOutput({ result: { body: "Private result" } })).toBeUndefined();
90 });
91
92 it("uses the first result data but combines explicit failure flags across content blocks", () => {
93 expect(
94 compactDynamicToolOutput([
95 { text: JSON.stringify({ threadId: "first" }) },
96 { text: JSON.stringify({ threadId: "second", isError: true, error: "Private error" }) },
97 ]),
98 ).toEqual({ threadId: "first", isError: true });
99 expect(
100 compactDynamicToolOutput([{ text: "{}" }, { text: JSON.stringify({ threadId: "second" }) }]),
101 ).toBeUndefined();
102 });
103
104 it("keeps complete thread creation batches and rollback markers without row bodies", () => {
105 expect(
106 compactDynamicToolOutput({
107 threads: [
108 { threadId: "created", status: "running", messages: ["Private message"] },
109 { threadId: "reverted", status: "rolled_back", error: "Private error" },
110 { status: "rolled_back" },
111 ],
112 }),
113 ).toEqual({
114 threads: [
115 { threadId: "created" },
116 { threadId: "reverted", status: "rolled_back" },
117 { status: "rolled_back" },
118 ],
119 });
120 expect(compactDynamicToolOutput({ threadId: "reverted", status: "rolled_back" })).toEqual({
121 threadId: "reverted",
122 status: "rolled_back",
123 });
124 expect(compactDynamicToolOutput({ threads: [] })).toEqual({ threads: [] });
125 });
126
127 it("omits incomplete or oversized creation evidence instead of returning a partial count", () => {
128 for (const threads of [
129 [{ threadId: "known" }, { title: "No confirmed ID" }],
130 Array.from({ length: 101 }, (_, index) => ({ threadId: `thread-${index}` })),
131 Array.from({ length: 100 }, (_, index) => ({ threadId: `${index}`.padEnd(256, "x") })),
132 [{ threadId: "x".repeat(257) }],
133 ]) {
134 expect(
135 compactDynamicToolOutput({
136 threadId: "must-not-be-counted-as-one",
137 status: "rolled_back",
138 taskId: "task-1",
139 threads,
140 }),
141 ).toEqual({ taskId: "task-1" });
142 }
143 const normalBatch = {
144 threads: Array.from({ length: 100 }, (_, index) => ({
145 threadId: `00000000-0000-4000-8000-${String(index).padStart(12, "0")}`,
146 })),
147 };
148 expect(compactDynamicToolOutput(normalBatch)).toEqual(normalBatch);
149 expect(
150 new TextEncoder().encode(JSON.stringify(compactDynamicToolOutput(normalBatch))).byteLength,
151 ).toBeLessThanOrEqual(8_192);
152 });
153
154 it("bounds parsing and envelope scanning while preserving an outer failure marker", () => {
155 const oversizedJson = JSON.stringify({ threadId: "hidden", body: "😄".repeat(5_000) });
156 for (const content of [
157 "x".repeat(1_000_000),
158 oversizedJson,
159 Array.from({ length: 33 }, () => ({ text: JSON.stringify({ threadId: "hidden" }) })),
160 { content: { content: { content: { content: { threadId: "too-deep" } } } } },
161 ]) {
162 expect(compactDynamicToolOutput({ isError: true, content })).toEqual({ isError: true });
163 }
164 expect(compactDynamicToolOutput({ threadId: "known", body: "x".repeat(1_000_000) })).toEqual({
165 threadId: "known",
166 });
167 });
168
169 it("keeps pull request summary outcomes without private details", () => {
170 expect(
171 compactDynamicToolOutput({
172 number: 42,
173 url: "https://github.com/acme/app/pull/42",
174 created: false,
175 state: "merged",
176 }),
177 ).toEqual({ created: false, state: "merged" });
178 expect(
179 compactDynamicToolOutput({
180 guides: [
181 { number: 1, outcome: "started", detail: null },
182 { number: 2, outcome: "failed", detail: "Private error" },
183 ],
184 checks: [{ name: "external", rerun: false, detail: "Private refusal" }],
185 }),
186 ).toEqual({
187 guides: [{ outcome: "started" }, { outcome: "failed" }],
188 checks: [{ rerun: false }],
189 });
190 expect(
191 compactDynamicToolOutput({
192 state: "draft",
193 guides: [{ outcome: "started" }, { outcome: "unknown" }],
194 checks: [{ rerun: true }, { name: "missing-rerun" }],
195 }),
196 ).toBeUndefined();
197 });
198});
199
200describe("toolOutputIndicatesFailure", () => {
201 it("preserves the command failure phrases across shells without copying the output", () => {
202 for (const text of [
203 "FILE NOT FOUND",
204 "No files found",
205 "ENOENT",
206 "No such file or directory",
207 "CommandNotFoundException",
208 "command not found",
209 "Cannot find path 'a' because it does not exist",
210 "The term 'example' is not recognized",
211 "is not recognized as the name of a cmdlet",
212 "A parameter cannot be found that matches parameter name",
213 "<exited with exit code 2>",
214 "Exited with exit code 1",
215 "exit code: 127",
216 "clean exit first, then ExIt CoDe: 3",
217 ])
218 expect(toolOutputIndicatesFailure(text)).toBe(true);
219 });
220
221 it("does not mark successful exit codes or incomplete failure phrases", () => {
222 for (const text of [
223 "Done",
224 "exit code: 0",
225 "Exited with exit code 0",
226 "cannot find path",
227 "is not recognized",
228 "exit",
229 "not found",
230 "the path cannot be found",
231 ]) {
232 expect(toolOutputIndicatesFailure(text)).toBe(false);
233 }
234 });
235});
236
237describe("toolOutputImages", () => {
238 const mcpImage = { type: "image", data: "AAAA", mimeType: "image/png" };
239 const claudeImage = {
240 type: "image",
241 source: { type: "base64", media_type: "image/JPEG", data: "BBBB" },
242 };
243 const text = { type: "text", text: "{}" };
244
245 it("finds images in each place providers store them, in order", () => {
246 expect(toolOutputImages({ content: [text, claudeImage, mcpImage] })).toEqual([
247 { mimeType: "image/jpeg", data: "BBBB" },
248 { mimeType: "image/png", data: "AAAA" },
249 ]);
250 expect(toolOutputImages([mcpImage])).toEqual([{ mimeType: "image/png", data: "AAAA" }]);
251 expect(toolOutputImages(mcpImage)).toEqual([{ mimeType: "image/png", data: "AAAA" }]);
252 const structuredContent = { screenshot: { mimeType: "image/png", width: 728, height: 354 } };
253 const content = [text, mcpImage];
254 const output = mcpToolResultOutput({ content, structuredContent });
255 expect(toolOutputImages(output)).toEqual([{ mimeType: "image/png", data: "AAAA" }]);
256 expect(output).toEqual({ content, structuredContent });
257 expect(mcpToolResultOutput({ content: [text], structuredContent })).toBe(structuredContent);
258 expect(mcpToolResultOutput({ content })).toBe(content);
259 expect(mcpToolResultOutput({ content: [text], structuredContent: {} })).toEqual([text]);
260 expect(mcpToolResultOutput({ content: [text], structuredContent: [] })).toEqual([text]);
261 });
262
263 it("skips types the server must not serve inline", () => {
264 expect(
265 toolOutputImages([
266 { ...mcpImage, mimeType: "image/svg+xml" },
267 { ...mcpImage, mimeType: "text/html" },
268 { type: "image", source: { type: "url", url: "https://example.com/a.png" } },
269 ]),
270 ).toEqual([]);
271 });
272
273 it("stops after the first few images", () => {
274 const images = toolOutputImages(Array.from({ length: 10_000 }, () => mcpImage));
275 expect(images).toHaveLength(MAX_TOOL_OUTPUT_IMAGES);
276 });
277});