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