Open History to see the last commit
1import type { Root, RootContent } from "mdast";
2import { resolveWorkEntryToolPresentation } from "@t3tools/client-runtime/work-log/presentation";
3import { isJavaScriptReplTool } from "@t3tools/shared/toolActivity";
4import { toolGroupAction } from "./MessagesTimeline.logic";
5import { formatWorkspaceRelativePath } from "../../filePathDisplay";
6import { type TimelineWorkEntry, type TimelineRow } from "./timelineRowContext";
7import { type WorkEntryIconName } from "./TimelineToolActivityIcon";
8
9export function toolGroupSummaryIconName(
10 kind: Extract<TimelineRow, { kind: "work-toggle" }>["summaryKind"],
11): WorkEntryIconName {
12 switch (kind) {
13 case "pull-request":
14 case "link-pr":
15 case "unlink-pr":
16 case "list-prs":
17 case "watch-pr":
18 case "unwatch-pr":
19 return "pull-request";
20 case "read":
21 return "eye";
22 case "edit":
23 return "square-pen";
24 case "command":
25 return "terminal";
26 case "thread-create":
27 return "t3-code";
28 case "browser":
29 return "browser";
30 case "device":
31 return "device";
32 case "search":
33 return "globe";
34 case "code-search":
35 return "search";
36 case "other":
37 return "wrench";
38 case "dynamic-tool":
39 return "hammer";
40 case "reasoning":
41 return "brain";
42 case "agent-tool":
43 return "bot";
44 case "tone-tool":
45 return "zap";
46 case "update":
47 case "mixed":
48 return "hammer";
49 }
50}
51
52export function workToneIcon(tone: TimelineWorkEntry["tone"]): {
53 iconName: WorkEntryIconName;
54 className: string;
55} {
56 if (tone === "error") {
57 return {
58 iconName: "circle-alert",
59 className: "text-foreground/92",
60 };
61 }
62 if (tone === "thinking") {
63 return {
64 iconName: "brain",
65 className: "text-icon-muted",
66 };
67 }
68 if (tone === "info") {
69 return {
70 iconName: "check",
71 className: "text-muted-foreground",
72 };
73 }
74 return {
75 iconName: "zap",
76 className: "text-foreground/92",
77 };
78}
79
80export function workEntryRawCommand(
81 workEntry: Pick<TimelineWorkEntry, "command" | "rawCommand">,
82): string | null {
83 const rawCommand = workEntry.rawCommand?.trim();
84 if (!rawCommand || !workEntry.command) {
85 return null;
86 }
87 return rawCommand === workEntry.command.trim() ? null : rawCommand;
88}
89
90export function buildToolCallExpandedBody(
91 workEntry: TimelineWorkEntry,
92 workspaceRoot: string | undefined,
93 visibleLabel: string,
94 viewedImagePath: string | null,
95): string | null {
96 const blocks: string[] = [];
97 const seen = new Set<string>([visibleLabel.trim()]);
98 const addBlock = (value: string | null | undefined) => {
99 const text = value?.trim();
100 if (!text || seen.has(text)) return;
101 seen.add(text);
102 blocks.push(text);
103 };
104 if (workEntry.itemType === "dynamic_tool" && workEntry.toolData !== undefined) {
105 const input =
106 workEntry.structuredPayload?.type === "dynamic_tool"
107 ? workEntry.structuredPayload.input
108 : workEntry.toolData !== null &&
109 typeof workEntry.toolData === "object" &&
110 "input" in workEntry.toolData
111 ? workEntry.toolData.input
112 : undefined;
113 if (input !== undefined) addBlock(`Tool input\n${JSON.stringify(input, null, 2)}`);
114 }
115 const command = workEntry.command?.trim();
116 const raw = workEntryRawCommand(workEntry);
117 if (command === visibleLabel.trim()) {
118 seen.add(command);
119 } else {
120 addBlock(raw ?? command);
121 }
122 const detail = workEntry.detail?.trim();
123 if (detail !== viewedImagePath?.trim()) {
124 addBlock(detail);
125 }
126 const viewedImagePaths = new Set(
127 viewedImagePath
128 ? [viewedImagePath.trim(), formatWorkspaceRelativePath(viewedImagePath, workspaceRoot)]
129 : [],
130 );
131 const changedFiles = (workEntry.changedFiles ?? []).flatMap((filePath) => {
132 const formattedPath = formatWorkspaceRelativePath(filePath, workspaceRoot);
133 return viewedImagePaths.has(filePath) ||
134 viewedImagePaths.has(formattedPath) ||
135 filePath.trim() === detail ||
136 formattedPath === detail
137 ? []
138 : [formattedPath];
139 });
140 if (changedFiles.length > 0) {
141 addBlock([...new Set(changedFiles)].join("\n"));
142 }
143 return blocks.length > 0 ? blocks.join("\n\n") : null;
144}
145
146export function workEntryIconName(workEntry: TimelineWorkEntry): WorkEntryIconName {
147 if (workEntry.structuredPayload?.type === "notification") {
148 if (workEntry.structuredPayload.outcome === "failed") return "circle-alert";
149 const source = workEntry.structuredPayload.source;
150 switch (source.kind) {
151 case "subagent":
152 case "delegated_task":
153 return "bot";
154 case "command":
155 return "terminal";
156 case "monitor":
157 return "eye";
158 case "background_task":
159 return "zap";
160 case "system":
161 return "t3-code";
162 default:
163 source satisfies never;
164 return "zap";
165 }
166 }
167 if (workEntry.itemType === "user_input_request" || workEntry.itemType === "approval_request") {
168 return "message-circle";
169 }
170 if (workEntry.toolSurface) return workEntry.toolSurface;
171 if (
172 workEntry.structuredPayload?.type === "dynamic_tool" &&
173 isJavaScriptReplTool(workEntry.structuredPayload.toolName)
174 )
175 return "code";
176 const toolPresentation = resolveWorkEntryToolPresentation(workEntry);
177 if (toolPresentation) return toolPresentation.icon;
178 const action = toolGroupAction(workEntry);
179 if (action !== "other") return toolGroupSummaryIconName(action);
180
181 switch (workEntry.itemType) {
182 case "dynamic_tool":
183 return "wrench";
184 case "subagent":
185 return "bot";
186 }
187
188 return workToneIcon(workEntry.tone).iconName;
189}
190
191/** Compact reasoning keeps formulas while flattening other Markdown into label text. */
192export function remarkThoughtPreview(fallback: string) {
193 return (tree: Root) => {
194 const children: RootContent[] = [];
195 const append = (value: string) => {
196 const last = children.at(-1);
197 const text = value.replace(/\s+/g, " ");
198 if (last?.type === "text") last.value = (last.value + text).replace(/\s+/g, " ");
199 else children.push({ type: "text", value: text });
200 };
201 const visit = (node: Root | RootContent) => {
202 if (node.type === "html" || node.type === "definition") return;
203 if (node.type === "math" || node.type === "inlineMath") {
204 children.push({
205 type: "inlineMath",
206 value: node.value,
207 position: node.position,
208 data: {
209 hName: "code",
210 hProperties: { className: ["language-math", "math-inline"] },
211 hChildren: [{ type: "text", value: node.value }],
212 },
213 });
214 } else if ("alt" in node) append(node.alt ?? "");
215 else if ("value" in node) append(node.value);
216 else if ("children" in node) {
217 node.children.forEach((child, index) => {
218 if (
219 index &&
220 ["root", "blockquote", "list", "listItem", "table", "tableRow"].includes(node.type)
221 )
222 append(" ");
223 visit(child);
224 });
225 } else if (node.type === "break") append(" ");
226 };
227 visit(tree);
228 const first = children[0];
229 const last = children.at(-1);
230 if (first?.type === "text") first.value = first.value.trimStart();
231 if (last?.type === "text") last.value = last.value.trimEnd();
232 tree.children = children.some((child) => child.type !== "text" || child.value)
233 ? children
234 : [{ type: "text", value: fallback }];
235 };
236}