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
170describe("toolOutputIndicatesFailure", () => {
171 it("preserves the command failure phrases across shells without copying the output", () => {
172 for (const text of [
173 "FILE NOT FOUND",
174 "No files found",
175 "ENOENT",
176 "No such file or directory",
177 "CommandNotFoundException",
178 "command not found",
179 "Cannot find path 'a' because it does not exist",
180 "The term 'example' is not recognized",
181 "is not recognized as the name of a cmdlet",
182 "A parameter cannot be found that matches parameter name",
183 "<exited with exit code 2>",
184 "Exited with exit code 1",
185 "exit code: 127",
186 "clean exit first, then ExIt CoDe: 3",
187 ])
188 expect(toolOutputIndicatesFailure(text)).toBe(true);
189 });
190
191 it("does not mark successful exit codes or incomplete failure phrases", () => {
192 for (const text of [
193 "Done",
194 "exit code: 0",
195 "Exited with exit code 0",
196 "cannot find path",
197 "is not recognized",
198 "exit",
199 "not found",
200 "the path cannot be found",
201 ]) {
202 expect(toolOutputIndicatesFailure(text)).toBe(false);
203 }
204 });
205});
206
207describe("toolOutputImages", () => {
208 const mcpImage = { type: "image", data: "AAAA", mimeType: "image/png" };
209 const claudeImage = {
210 type: "image",
211 source: { type: "base64", media_type: "image/JPEG", data: "BBBB" },
212 };
213 const text = { type: "text", text: "{}" };
214
215 it("finds images in each place providers store them, in order", () => {
216 expect(toolOutputImages({ content: [text, claudeImage, mcpImage] })).toEqual([
217 { mimeType: "image/jpeg", data: "BBBB" },
218 { mimeType: "image/png", data: "AAAA" },
219 ]);
220 expect(toolOutputImages([mcpImage])).toEqual([{ mimeType: "image/png", data: "AAAA" }]);
221 expect(toolOutputImages(mcpImage)).toEqual([{ mimeType: "image/png", data: "AAAA" }]);
222 const structuredContent = { screenshot: { mimeType: "image/png", width: 728, height: 354 } };
223 const content = [text, mcpImage];
224 const output = mcpToolResultOutput({ content, structuredContent });
225 expect(toolOutputImages(output)).toEqual([{ mimeType: "image/png", data: "AAAA" }]);
226 expect(output).toEqual({ content, structuredContent });
227 expect(mcpToolResultOutput({ content: [text], structuredContent })).toBe(structuredContent);
228 expect(mcpToolResultOutput({ content })).toBe(content);
229 expect(mcpToolResultOutput({ content: [text], structuredContent: {} })).toEqual([text]);
230 expect(mcpToolResultOutput({ content: [text], structuredContent: [] })).toEqual([text]);
231 });
232
233 it("skips types the server must not serve inline", () => {
234 expect(
235 toolOutputImages([
236 { ...mcpImage, mimeType: "image/svg+xml" },
237 { ...mcpImage, mimeType: "text/html" },
238 { type: "image", source: { type: "url", url: "https://example.com/a.png" } },
239 ]),
240 ).toEqual([]);
241 });
242
243 it("stops after the first few images", () => {
244 const images = toolOutputImages(Array.from({ length: 10_000 }, () => mcpImage));
245 expect(images).toHaveLength(MAX_TOOL_OUTPUT_IMAGES);
246 });
247});