Open History to see the last commit
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";
40}
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;
49}
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;
58}
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 }
67}
68
69export function openCodeToolTurnItem(
70 base: ToolItemBase,
71 tool: {
72 readonly name: string;
73 readonly input: Record<string, unknown>;
74 readonly output: string | undefined;
75 readonly completedMetadata: unknown;
76 },
77): OrchestrationV2TurnItem {
78 const { input, output } = tool;
79 switch (openCodeToolProjectionKind(tool.name)) {
80 case "command_execution": {
81 const exitCode = recordNumber(tool.completedMetadata, "exit", "exitCode");
82 return {
83 ...base,
84 type: "command_execution",
85 input: recordString(input, "command", "cmd") ?? stableJson(input),
86 ...(output === undefined ? {} : { output }),
87 ...(exitCode === undefined ? {} : { exitCode }),
88 };
89 }
90 case "file_change": {
91 const oldStr = recordString(input, "oldString", "oldText");
92 const newStr = recordString(input, "newString", "content", "newText");
93 const diffStr =
94 recordString(tool.completedMetadata, "diff", "patch") ??
95 (base.status === "failed" && output?.trim() ? output : undefined);
96 return {
97 ...base,
98 type: "file_change",
99 fileName: recordString(input, "filePath", "path", "file") ?? tool.name,
100 ...(oldStr === undefined ? {} : { oldStr }),
101 ...(newStr === undefined ? {} : { newStr }),
102 ...(diffStr === undefined ? {} : { diffStr }),
103 };
104 }
105 case "file_search": {
106 const pattern = recordString(input, "pattern", "query", "path", "filePath");
107 const searchRoot = (recordString(input, "path", "filePath") ?? pattern)?.trim();
108 return {
109 ...base,
110 title:
111 formatSearchToolLabel({ input, ...(pattern === undefined ? {} : { pattern }) }) ??
112 base.title,
113 type: "file_search",
114 ...(pattern === undefined ? {} : { pattern }),
115 ...(!output?.trim() || searchRoot === undefined
116 ? {}
117 : {
118 results: [
119 { fileName: searchRoot, preview: output.slice(0, SEARCH_PREVIEW_MAX_CHARS) },
120 ],
121 }),
122 };
123 }
124 case "web_search": {
125 const pattern = recordString(input, "query", "url", "pattern");
126 const url = recordString(input, "url")?.trim();
127 return {
128 ...base,
129 type: "web_search",
130 ...(pattern === undefined ? {} : { patterns: [pattern] }),
131 ...(!output?.trim()
132 ? {}
133 : {
134 results: [
135 {
136 ...(url === undefined ? {} : { url }),
137 snippet: output.slice(0, SEARCH_PREVIEW_MAX_CHARS),
138 },
139 ],
140 }),
141 };
142 }
143 case "dynamic_tool": {
144 const readPath = recordString(input, "filePath", "path", "file");
145 return {
146 ...base,
147 title:
148 tool.name.toLowerCase() === "read" && readPath !== undefined
149 ? formatReadToolLabel(readPath)
150 : base.title,
151 type: "dynamic_tool",
152 toolName: tool.name,
153 input,
154 ...(output === undefined ? {} : { output }),
155 };
156 }
157 }
158}