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

TimelineWorkEntryRow.logic.ts

231 lines7.1 KB

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 "code-search":
40 return "search";
41 case "other":
42 return "wrench";
43 case "dynamic-tool":
44 return "hammer";
45 case "reasoning":
46 return "brain";
47 case "agent-tool":
48 return "bot";
49 case "tone-tool":
50 return "zap";
51 case "update":
52 case "mixed":
53 return "hammer";
54 }
56​
57export function workToneIcon(tone: TimelineWorkEntry["tone"]): {
58 iconName: WorkEntryIconName;
59 className: string;
60} {
61 if (tone === "error") {
62 return {
63 iconName: "circle-alert",
64 className: "text-foreground/92",
65 };
66 }
67 if (tone === "thinking") {
68 return {
69 iconName: "brain",
70 className: "text-icon-muted",
71 };
72 }
73 if (tone === "info") {
74 return {
75 iconName: "check",
76 className: "text-muted-foreground",
77 };
78 }
79 return {
80 iconName: "zap",
81 className: "text-foreground/92",
82 };
84​
85export function workEntryRawCommand(
86 workEntry: Pick<TimelineWorkEntry, "command" | "rawCommand">,
87): string | null {
88 const rawCommand = workEntry.rawCommand?.trim();
89 if (!rawCommand || !workEntry.command) {
90 return null;
91 }
92 return rawCommand === workEntry.command.trim() ? null : rawCommand;
94​
95export function buildToolCallExpandedBody(
96 workEntry: TimelineWorkEntry,
97 workspaceRoot: string | undefined,
98 visibleLabel: string,
99 viewedImagePath: string | null,
100 resolve: MessageResolver = m,
101): string | null {
102 const blocks: string[] = [];
103 const seen = new Set<string>([visibleLabel.trim()]);
104 const addBlock = (value: string | null | undefined) => {
105 const text = value?.trim();
106 if (!text || seen.has(text)) return;
107 seen.add(text);
108 blocks.push(text);
109 };
110 if (workEntry.itemType === "dynamic_tool" && workEntry.toolData !== undefined) {
111 const input =
112 workEntry.structuredPayload?.type === "dynamic_tool"
113 ? workEntry.structuredPayload.input
114 : workEntry.toolData !== null &&
115 typeof workEntry.toolData === "object" &&
116 "input" in workEntry.toolData
117 ? workEntry.toolData.input
118 : undefined;
119 if (input !== undefined) {
120 addBlock(resolve(msg("Tool input\n{input}", { input: JSON.stringify(input, null, 2) })));
121 }
122 }
123 const command = workEntry.command?.trim();
124 const raw = workEntryRawCommand(workEntry);
125 if (command === visibleLabel.trim()) {
126 seen.add(command);
127 } else {
128 addBlock(raw ?? command);
129 }
130 const detail = workEntry.detail?.trim();
131 if (detail !== viewedImagePath?.trim()) {
132 addBlock(detail);
133 }
134 const viewedImagePaths = new Set(
135 viewedImagePath
136 ? [viewedImagePath.trim(), formatWorkspaceRelativePath(viewedImagePath, workspaceRoot)]
137 : [],
138 );
139 const changedFiles = (workEntry.changedFiles ?? []).flatMap((filePath) => {
140 const formattedPath = formatWorkspaceRelativePath(filePath, workspaceRoot);
141 return viewedImagePaths.has(filePath) ||
142 viewedImagePaths.has(formattedPath) ||
143 filePath.trim() === detail ||
144 formattedPath === detail
145 ? []
146 : [formattedPath];
147 });
148 if (changedFiles.length > 0) {
149 addBlock([...new Set(changedFiles)].join("\n"));
150 }
151 return blocks.length > 0 ? blocks.join("\n\n") : null;
153​
154export function workEntryIconName(workEntry: TimelineWorkEntry): WorkEntryIconName {
155 if (workEntry.structuredPayload?.type === "notification") {
156 if (workEntry.structuredPayload.outcome === "failed") return "circle-alert";
157 const source = workEntry.structuredPayload.source;
158 switch (source.kind) {
159 case "subagent":
160 case "delegated_task":
161 return "bot";
162 case "command":
163 return "terminal";
164 case "monitor":
165 return "eye";
166 case "background_task":
167 return "zap";
168 case "system":
169 return "t3-code";
170 default:
171 source satisfies never;
172 return "zap";
173 }
174 }
175 if (workEntry.itemType === "system_notice") {
176 return workEntry.structuredPayload?.type === "system_notice" &&
177 workEntry.structuredPayload.level === "info"
178 ? "zap"
179 : "circle-alert";
180 }
181 if (workEntry.itemType === "user_input_request" || workEntry.itemType === "approval_request") {
182 return "message-circle";
183 }
184 if (workEntry.itemType === "todo_list") return "list-todo";
185 if (workEntry.toolSurface) return workEntry.toolSurface;
186 if (
187 workEntry.structuredPayload?.type === "dynamic_tool" &&
188 isJavaScriptReplTool(workEntry.structuredPayload.toolName)
189 )
190 return "code";
191 const toolPresentation = resolveWorkEntryToolPresentation(workEntry, undefined, m);
192 if (toolPresentation) return toolPresentation.icon;
193 const action = toolGroupAction(workEntry);
194 if (action !== "other") return toolGroupSummaryIconName(action);
195​
196 switch (workEntry.itemType) {
197 case "dynamic_tool":
198 return "wrench";
199 case "subagent":
200 return "bot";
201 }
202​
203 return workToneIcon(workEntry.tone).iconName;
205​
206// Look past the visible label so long destinations and inline delimiters can close.
207// This is bounded lookahead, not document-wide Markdown parity: a closing delimiter
208// or reference definition beyond this prefix remains unresolved by the parser.
209export const THOUGHT_PREVIEW_SOURCE_MAX_LENGTH = 8_192;
210export const THOUGHT_PREVIEW_MAX_LENGTH = 2_048;
211​
212export function sliceThoughtPreviewSource(text: string): string {
213 return text.slice(
214 0,
215 splitsSurrogatePair(text, THOUGHT_PREVIEW_SOURCE_MAX_LENGTH)
216 ? THOUGHT_PREVIEW_SOURCE_MAX_LENGTH - 1
217 : THOUGHT_PREVIEW_SOURCE_MAX_LENGTH,
218 );
220​
221const thoughtPreviews = new Map<string, MarkdownExtensions>();
222​
223/** Compact reasoning keeps formulas while flattening other Markdown into label text. */
224export function thoughtPreviewMarkdown(fallback: string): MarkdownExtensions {
225 let extensions = thoughtPreviews.get(fallback);
226 if (!extensions) {
227 extensions = { preview: { fallback, maxLength: THOUGHT_PREVIEW_MAX_LENGTH } };
228 thoughtPreviews.set(fallback, extensions);
229 }
230 return extensions;