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

presentation.ts

192 lines6.7 KB

1import { isToolLifecycleItemType, type ToolLifecycleItemType } from "@t3tools/contracts";
2​
3export function isWorktreeSetupActivity(kind: string): boolean {
4 return kind === "setup-script.requested" || kind === "setup-script.started";
5}
6​
7export interface WorkLogPresentationEntry {
8 readonly label: string;
9 readonly toolTitle?: string;
10 readonly tone: "thinking" | "tool" | "info" | "error";
11 readonly command?: string;
12 readonly changedFiles?: ReadonlyArray<string>;
13 readonly itemType?: ToolLifecycleItemType;
14 readonly requestKind?: string;
15 readonly turnId?: string | null;
16 readonly toolCallId?: string;
17 readonly toolLifecycleStatus?: string;
18 readonly sourceActivityKind?: string;
19 readonly taskId?: string;
21​
22export type ToolGroupAction =
23 | "read"
24 | "edit"
25 | "command"
26 | "code-search"
27 | "search"
28 | "other"
29 | "update";
30​
31export type ToolGroupSummaryKind =
32 | ToolGroupAction
33 | "dynamic-tool"
34 | "agent-tool"
35 | "tone-tool"
36 | "mixed";
37​
38export function normalizeCompactToolLabel(value: string): string {
39 return value.replace(/\s+(?:complete|completed)\s*$/i, "").trim();
41​
42function workLogEntryIsToolLike(entry: WorkLogPresentationEntry): boolean {
43 if (entry.tone === "tool" || entry.tone === "thinking" || entry.tone === "error") return true;
44 if (entry.command !== undefined && entry.command.trim().length > 0) return true;
45 if (entry.requestKind !== undefined) return true;
46 return entry.itemType !== undefined && isToolLifecycleItemType(entry.itemType);
48​
49export function workLogEntryIsLocalCodeSearch(entry: WorkLogPresentationEntry): boolean {
50 return (
51 entry.itemType === "web_search" &&
52 /\bgrep\b/i.test(normalizeCompactToolLabel(entry.toolTitle ?? entry.label))
53 );
55​
56export function toolGroupAction(entry: WorkLogPresentationEntry): ToolGroupAction {
57 if (
58 entry.requestKind === "file-read" ||
59 entry.itemType === "image_view" ||
60 (entry.itemType === "dynamic_tool_call" && entry.toolTitle === "Read File")
61 ) {
62 return "read";
63 }
64 if (
65 entry.requestKind === "file-change" ||
66 entry.itemType === "file_change" ||
67 (entry.changedFiles?.length ?? 0) > 0
68 ) {
69 return "edit";
70 }
71 if (entry.requestKind === "command" || entry.itemType === "command_execution" || entry.command) {
72 return "command";
73 }
74 if (workLogEntryIsLocalCodeSearch(entry)) return "code-search";
75 if (entry.itemType === "web_search") return "search";
76 return workLogEntryIsToolLike(entry) ? "other" : "update";
78​
79function toolGroupActionCount(
80 action: ToolGroupAction,
81 entries: ReadonlyArray<WorkLogPresentationEntry>,
82): number {
83 if (action !== "edit") return entries.length;
84​
85 const changedFiles = new Set<string>();
86 let editsWithoutFileDetails = 0;
87 for (const entry of entries) {
88 if (!entry.changedFiles || entry.changedFiles.length === 0) {
89 editsWithoutFileDetails += 1;
90 continue;
91 }
92 for (const file of entry.changedFiles) changedFiles.add(file);
93 }
94 return changedFiles.size + editsWithoutFileDetails;
96​
97function toolGroupActionLabel(action: ToolGroupAction, count: number): string {
98 switch (action) {
99 case "read":
100 return `Read ${count} ${count === 1 ? "file" : "files"}`;
101 case "edit":
102 return `Changed ${count} ${count === 1 ? "file" : "files"}`;
103 case "command":
104 return `Ran ${count} ${count === 1 ? "command" : "commands"}`;
105 case "search":
106 return `Searched the web ${count} ${count === 1 ? "time" : "times"}`;
107 case "code-search":
108 return `Searched code ${count} ${count === 1 ? "time" : "times"}`;
109 case "other":
110 return `Used ${count} ${count === 1 ? "tool" : "tools"}`;
111 case "update":
112 return `Received ${count} ${count === 1 ? "update" : "updates"}`;
113 }
115​
116export function summarizeToolGroup(entries: ReadonlyArray<WorkLogPresentationEntry>): string {
117 const summaryEntries = omitSupersededLifecycleMarkers(entries, (entry) => entry);
118 const groupedEntries = new Map<ToolGroupAction, WorkLogPresentationEntry[]>();
119 for (const entry of summaryEntries) {
120 const action = toolGroupAction(entry);
121 const group = groupedEntries.get(action);
122 if (group) group.push(entry);
123 else groupedEntries.set(action, [entry]);
124 }
125 const labels = [...groupedEntries].map(([action, actionEntries]) =>
126 toolGroupActionLabel(action, toolGroupActionCount(action, actionEntries)),
127 );
128 const sentenceLabels = labels.map((label, index) =>
129 index === 0 ? label : label.charAt(0).toLowerCase() + label.slice(1),
130 );
131 if (sentenceLabels.length < 2) return sentenceLabels[0] ?? "";
132 if (sentenceLabels.length === 2) return sentenceLabels.join(" and ");
133 return `${sentenceLabels.slice(0, -1).join(", ")}, and ${sentenceLabels.at(-1)}`;
135​
136export function omitSupersededLifecycleMarkers<T>(
137 entries: readonly T[],
138 workEntryFor: (entry: T) => WorkLogPresentationEntry,
139): T[] {
140 const laterTerminalIdentities = new Set<string>();
141 const reversedEntries: T[] = [];
142​
143 for (let index = entries.length - 1; index >= 0; index -= 1) {
144 const entry = entries[index]!;
145 const workEntry = workEntryFor(entry);
146 const normalizedLabel = normalizeCompactToolLabel(workEntry.toolTitle ?? workEntry.label);
147 const identity = [
148 workEntry.turnId ?? "no-turn",
149 workEntry.itemType ?? "",
150 normalizedLabel,
151 ].join("\u001f");
152 const activityKind = workEntry.sourceActivityKind;
153 const isStatuslessIdlessMarker =
154 workEntry.toolCallId === undefined &&
155 workEntry.toolLifecycleStatus === undefined &&
156 (activityKind === "tool.started" || activityKind === "tool.updated");
157 if (isStatuslessIdlessMarker && laterTerminalIdentities.has(identity)) continue;
158​
159 reversedEntries.push(entry);
160 if (
161 activityKind === "tool.completed" ||
162 (workEntry.toolLifecycleStatus !== undefined &&
163 workEntry.toolLifecycleStatus !== "inProgress")
164 ) {
165 laterTerminalIdentities.add(identity);
166 }
167 }
168​
169 return reversedEntries.toReversed();
171​
172export function toolGroupSummaryKind(
173 entries: ReadonlyArray<WorkLogPresentationEntry>,
174): ToolGroupSummaryKind {
175 const actions = new Set(entries.map(toolGroupAction));
176 if (actions.size !== 1) return "mixed";
177​
178 const action = actions.values().next().value!;
179 if (action !== "other") return action;
180​
181 const fallbackKinds = new Set(
182 entries.map((entry): ToolGroupSummaryKind => {
183 if (entry.itemType === "mcp_tool_call") return "other";
184 if (entry.itemType === "dynamic_tool_call") return "dynamic-tool";
185 if (entry.itemType === "collab_agent_tool_call" || entry.taskId) return "agent-tool";
186 if (entry.tone === "thinking") return "agent-tool";
187 if (entry.tone === "tool") return "tone-tool";
188 return "other";
189 }),
190 );
191 return fallbackKinds.size === 1 ? fallbackKinds.values().next().value! : "mixed";