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

TimelineWorkEntryRow.logic.ts

239 lines7.3 KB

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