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