Open History to see the last commit
1import type { OrchestrationV2TurnItem } from "@t3tools/contracts";
2import { resolveT3McpToolId } from "@t3tools/shared/t3McpToolPresentation";
3import { formatReadToolLabel, formatSearchToolLabel } from "@t3tools/shared/toolActivity";
4
5// Search results stay on the timeline wire, so keep their text a preview.
6const SEARCH_PREVIEW_MAX_CHARS = 8_000;
7
8type ToolItemBase = Omit<
9 Extract<OrchestrationV2TurnItem, { type: "dynamic_tool" }>,
10 "type" | "toolName" | "input" | "output"
11>;
12
13export function openCodeToolProjectionKind(
14 toolName: string,
15): "command_execution" | "file_change" | "file_search" | "web_search" | "dynamic_tool" {
16 if (resolveT3McpToolId(toolName) !== null) return "dynamic_tool";
17 const normalized = toolName.toLowerCase();
18 if (normalized === "todowrite") {
19 return "dynamic_tool";
20 }
21 if (normalized.includes("bash") || normalized.includes("shell")) {
22 return "command_execution";
23 }
24 if (normalized.includes("edit") || normalized.includes("write") || normalized.includes("patch")) {
25 return "file_change";
26 }
27 if (normalized.includes("web") || normalized === "codesearch" || normalized === "code_search") {
28 return "web_search";
29 }
30 if (normalized === "read") {
31 return "dynamic_tool";
32 }
33 if (
34 normalized.includes("glob") ||
35 normalized.includes("grep") ||
36 normalized.includes("search") ||
37 normalized.includes("lsp")
38 ) {
39 return "file_search";
40 }
41 return "dynamic_tool";
42}
43
44function recordString(input: unknown, ...keys: ReadonlyArray<string>): string | undefined {
45 if (typeof input !== "object" || input === null) return undefined;
46 for (const key of keys) {
47 const value = (input as Record<string, unknown>)[key];
48 if (typeof value === "string" && value.trim().length > 0) return value;
49 }
50 return undefined;
51}
52
53function recordNumber(input: unknown, ...keys: ReadonlyArray<string>): number | undefined {
54 if (typeof input !== "object" || input === null) return undefined;
55 for (const key of keys) {
56 const value = (input as Record<string, unknown>)[key];
57 if (typeof value === "number" && Number.isFinite(value)) return value;
58 }
59 return undefined;
60}
61
62function stableJson(value: unknown): string {
63 if (typeof value === "string") return value;
64 try {
65 return JSON.stringify(value, null, 2);
66 } catch {
67 return String(value);
68 }
69}
70
71function nativeReadOutput(output: string): string {
72 const header = /^<path>[^\r\n]+<\/path>\r?\n<type>(directory|file)<\/type>\r?\n/.exec(output);
73 if (header === null) return output;
74 const body = output.slice(header[0].length);
75 // File lines are numbered; only a closing tag on its own line ends the envelope.
76 const envelope =
77 header[1] === "directory"
78 ? /^<entries>\r?\n([\s\S]*?)\r?\n<\/entries>(?=\r?\n|$)/.exec(body)
79 : /^<content>(?:\r?\n)?([\s\S]*?)\r?\n<\/content>(?=\r?\n|$)/.exec(body);
80 // OpenCode can append instructions after the envelope; keep that suffix verbatim.
81 return envelope === null ? output : envelope[1] + body.slice(envelope[0].length);
82}
83
84export function openCodeToolTurnItem(
85 base: ToolItemBase,
86 tool: {
87 readonly name: string;
88 readonly input: Record<string, unknown>;
89 readonly output: string | undefined;
90 readonly completedMetadata: unknown;
91 },
92): OrchestrationV2TurnItem {
93 const { input, output } = tool;
94 switch (openCodeToolProjectionKind(tool.name)) {
95 case "command_execution": {
96 const exitCode = recordNumber(tool.completedMetadata, "exit", "exitCode");
97 return {
98 ...base,
99 type: "command_execution",
100 input: recordString(input, "command", "cmd") ?? stableJson(input),
101 ...(output === undefined ? {} : { output }),
102 ...(exitCode === undefined ? {} : { exitCode }),
103 };
104 }
105 case "file_change": {
106 const oldStr = recordString(input, "oldString", "oldText");
107 const newStr = recordString(input, "newString", "content", "newText");
108 const diffStr =
109 recordString(tool.completedMetadata, "diff", "patch") ??
110 (base.status === "failed" && output?.trim() ? output : undefined);
111 return {
112 ...base,
113 type: "file_change",
114 fileName: recordString(input, "filePath", "path", "file") ?? tool.name,
115 ...(oldStr === undefined ? {} : { oldStr }),
116 ...(newStr === undefined ? {} : { newStr }),
117 ...(diffStr === undefined ? {} : { diffStr }),
118 };
119 }
120 case "file_search": {
121 const pattern = recordString(input, "pattern", "query", "path", "filePath");
122 const searchRoot = (recordString(input, "path", "filePath") ?? pattern)?.trim();
123 return {
124 ...base,
125 title:
126 formatSearchToolLabel({ input, ...(pattern === undefined ? {} : { pattern }) }) ??
127 base.title,
128 type: "file_search",
129 ...(pattern === undefined ? {} : { pattern }),
130 ...(!output?.trim() || searchRoot === undefined
131 ? {}
132 : {
133 results: [
134 { fileName: searchRoot, preview: output.slice(0, SEARCH_PREVIEW_MAX_CHARS) },
135 ],
136 }),
137 };
138 }
139 case "web_search": {
140 const pattern = recordString(input, "query", "url", "pattern");
141 const url = recordString(input, "url")?.trim();
142 return {
143 ...base,
144 type: "web_search",
145 ...(pattern === undefined ? {} : { patterns: [pattern] }),
146 ...(!output?.trim()
147 ? {}
148 : {
149 results: [
150 {
151 ...(url === undefined ? {} : { url }),
152 snippet: output.slice(0, SEARCH_PREVIEW_MAX_CHARS),
153 },
154 ],
155 }),
156 };
157 }
158 case "dynamic_tool": {
159 const readPath = recordString(input, "filePath", "path", "file");
160 const cleanedOutput =
161 tool.name === "read" && output !== undefined ? nativeReadOutput(output) : output;
162 return {
163 ...base,
164 title:
165 tool.name.toLowerCase() === "read" && readPath !== undefined
166 ? formatReadToolLabel(readPath)
167 : base.title,
168 type: "dynamic_tool",
169 toolName: tool.name,
170 input,
171 ...(cleanedOutput === undefined ? {} : { output: cleanedOutput }),
172 };
173 }
174 }
175}