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

toolItems.ts

173 lines6.0 KB

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