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

OpenCodeToolItems.ts

143 lines4.7 KB

1/**
2 * Turn-item bodies for OpenCode tool calls, shared by the 1.x and 2.x runtimes.
3 * Both report a tool as a name, a JSON input, text output, and free-form
4 * metadata; only how they deliver those differs.
5 */
6import type { OrchestrationV2TurnItem } from "@t3tools/contracts";
7import { formatReadToolLabel, formatSearchToolLabel } from "@t3tools/shared/toolActivity";
8​
9type ToolItemBase = Omit<
10 Extract<OrchestrationV2TurnItem, { type: "dynamic_tool" }>,
11 "type" | "toolName" | "input" | "output"
12>;
13​
14export function openCodeToolProjectionKind(
15 toolName: string,
16): "command_execution" | "file_change" | "file_search" | "web_search" | "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";
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;
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;
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 }
70​
71/**
72 * The turn item for one tool call. `completedMetadata` is the tool's metadata
73 * once it succeeded: exit codes and diffs are only trusted from a finished call.
74 */
75export function openCodeToolTurnItem(
76 base: ToolItemBase,
77 tool: {
78 readonly name: string;
79 readonly input: Record<string, unknown>;
80 readonly output: string | undefined;
81 readonly completedMetadata: unknown;
82 },
83): OrchestrationV2TurnItem {
84 const { input, output } = tool;
85 switch (openCodeToolProjectionKind(tool.name)) {
86 case "command_execution": {
87 const exitCode = recordNumber(tool.completedMetadata, "exit", "exitCode");
88 return {
89 ...base,
90 type: "command_execution",
91 input: recordString(input, "command", "cmd") ?? stableJson(input),
92 ...(output === undefined ? {} : { output }),
93 ...(exitCode === undefined ? {} : { exitCode }),
94 };
95 }
96 case "file_change": {
97 const oldStr = recordString(input, "oldString", "oldText");
98 const newStr = recordString(input, "newString", "content", "newText");
99 const diffStr = recordString(tool.completedMetadata, "diff", "patch");
100 return {
101 ...base,
102 type: "file_change",
103 fileName: recordString(input, "filePath", "path", "file") ?? tool.name,
104 ...(oldStr === undefined ? {} : { oldStr }),
105 ...(newStr === undefined ? {} : { newStr }),
106 ...(diffStr === undefined ? {} : { diffStr }),
107 };
108 }
109 case "file_search": {
110 const pattern = recordString(input, "pattern", "query", "path", "filePath");
111 return {
112 ...base,
113 title:
114 formatSearchToolLabel({ input, ...(pattern === undefined ? {} : { pattern }) }) ??
115 base.title,
116 type: "file_search",
117 ...(pattern === undefined ? {} : { pattern }),
118 };
119 }
120 case "web_search": {
121 const pattern = recordString(input, "query", "url", "pattern");
122 return {
123 ...base,
124 type: "web_search",
125 ...(pattern === undefined ? {} : { patterns: [pattern] }),
126 };
127 }
128 case "dynamic_tool": {
129 const readPath = recordString(input, "filePath", "path", "file");
130 return {
131 ...base,
132 title:
133 tool.name.toLowerCase() === "read" && readPath !== undefined
134 ? formatReadToolLabel(readPath)
135 : base.title,
136 type: "dynamic_tool",
137 toolName: tool.name,
138 input,
139 ...(output === undefined ? {} : { output }),
140 };
141 }
142 }