Open History to see the last commit
1import type {
2 ModelSelection,
3 OrchestrationV2TurnItemStatus,
4 OrchestrationV2ThreadShell,
5 OrchestrationProjectShell,
6 ServerProvider,
7} from "@t3tools/contracts";
8import { formatModelSlugName, resolveSelectableModel } from "@t3tools/shared/model";
9import { fileDisplayName } from "@t3tools/shared/path";
10import { resolveModelShortName } from "../providerClients.ts";
11import { isTerminalSubagentStatus } from "./subagentRuntime.ts";
12import { mFallback, msg, plural, part } from "gt-i18n";
13import type { M } from "gt-i18n";
14
15export function subagentGroupSummary(
16 members: ReadonlyArray<{ readonly status: OrchestrationV2TurnItemStatus }>,
17 resolve: M = mFallback,
18) {
19 const active = members.some(
20 ({ status }) => status === "pending" || status === "running" || status === "waiting",
21 );
22 return {
23 label: active
24 ? resolve(
25 msg`${plural(
26 { count: members.length },
27 {
28 one: part`Kicked off ${{ count: members.length }} subagent`,
29 other: part`Kicked off ${{ count: members.length }} subagents`,
30 },
31 )}`,
32 )
33 : resolve(
34 msg`${plural(
35 { count: members.length },
36 {
37 one: part`Ran ${{ count: members.length }} subagent`,
38 other: part`Ran ${{ count: members.length }} subagents`,
39 },
40 )}`,
41 ),
42 active,
43 failed: members.some(({ status }) => status === "failed"),
44 };
45}
46
47const SUBAGENT_STATUS_COUNTS = {
48 working: ({ count }: { count: number }) => msg`${{ count }} working`,
49 done: ({ count }: { count: number }) => msg`${{ count }} done`,
50 failed: ({ count }: { count: number }) => msg`${{ count }} failed`,
51 stopped: ({ count }: { count: number }) => msg`${{ count }} stopped`,
52 idle: ({ count }: { count: number }) => msg`${{ count }} idle`,
53};
54
55export function summarizeSubagentStatuses(
56 statuses: ReadonlyArray<OrchestrationV2TurnItemStatus>,
57 resolve: M = mFallback,
58): string {
59 const counts = { working: 0, done: 0, failed: 0, stopped: 0, idle: 0 };
60 for (const status of statuses) {
61 if (status === "pending" || status === "running" || status === "waiting") counts.working += 1;
62 else if (status === "completed") counts.done += 1;
63 else if (status === "failed") counts.failed += 1;
64 else if (status === "idle") counts.idle += 1;
65 else counts.stopped += 1;
66 }
67 return (Object.keys(counts) as Array<keyof typeof counts>)
68 .filter((key) => counts[key] > 0)
69 .map((key) => resolve(SUBAGENT_STATUS_COUNTS[key]({ count: counts[key] })))
70 .join(" · ");
71}
72
73export function formatSubagentDisplayTitle(title: string): string {
74 const displayTitle = title.replace(/^Subagent:\s*/i, "");
75 const path = /^\/root\/(?:[^/]+\/)*([^/]+)\/?$/u.exec(displayTitle);
76 if (path === null) return displayTitle;
77
78 const name = path[1]!.replace(/[_\s]+/gu, " ").trim();
79 return name.replace(/(^|\s)\S/gu, (letter) => letter.toUpperCase()) || displayTitle;
80}
81
82export function resolveSubagentMetadata(
83 input: {
84 readonly model: string | null;
85 readonly modelSelection?: Pick<ModelSelection, "model"> | undefined;
86 readonly provider?: Pick<ServerProvider, "driver" | "models"> | null | undefined;
87 readonly parentThread?:
88 | Pick<OrchestrationV2ThreadShell, "projectId" | "worktreePath">
89 | null
90 | undefined;
91 readonly childThread?:
92 | Pick<OrchestrationV2ThreadShell, "branch" | "worktreePath">
93 | null
94 | undefined;
95 readonly parentProject?: Pick<OrchestrationProjectShell, "workspaceRoot"> | null | undefined;
96 readonly childProject?:
97 | Pick<OrchestrationProjectShell, "id" | "title" | "workspaceRoot">
98 | null
99 | undefined;
100 },
101 resolve: M = mFallback,
102) {
103 const model = input.model?.trim() || input.modelSelection?.model.trim();
104 const slug = input.provider
105 ? resolveSelectableModel(input.provider.driver, model, input.provider.models)
106 : model;
107 const catalogModel = input.provider?.models.find((candidate) => candidate.slug === slug);
108 const reportedLabel = catalogModel
109 ? (resolveModelShortName(catalogModel, input.provider?.driver) ?? catalogModel.name)
110 : model
111 ? formatModelSlugName(model)
112 : resolve(msg("Not reported"));
113 const qualifier = catalogModel?.subProvider?.trim();
114 const modelLabel = qualifier
115 ? reportedLabel
116 .replace(
117 new RegExp(
118 `^${qualifier.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}(?:\\s*[.:/-]\\s*|\\s+)`,
119 "iu",
120 ),
121 "",
122 )
123 .trim() || reportedLabel
124 : reportedLabel;
125 const parentWorkspace = input.parentThread?.worktreePath ?? input.parentProject?.workspaceRoot;
126 const childWorkspace = input.childThread?.worktreePath ?? input.childProject?.workspaceRoot;
127 const workspace = [
128 ...(input.parentThread &&
129 input.childProject &&
130 input.childProject.id !== input.parentThread.projectId
131 ? [
132 {
133 label: resolve(msg({ context: "field label" })`Project`),
134 value: input.childProject.title,
135 },
136 ]
137 : []),
138 ...(parentWorkspace && childWorkspace && parentWorkspace !== childWorkspace
139 ? [
140 {
141 label: input.childThread?.branch
142 ? resolve(msg({ context: "field label" })`Branch`)
143 : input.childThread?.worktreePath
144 ? resolve(msg({ context: "field label" })`Worktree`)
145 : resolve(msg({ context: "field label" })`Workspace`),
146 value: input.childThread?.branch ?? fileDisplayName(childWorkspace),
147 },
148 ]
149 : []),
150 ];
151 return { modelLabel, modelSlug: slug ?? model ?? null, workspace };
152}
153
154export function subagentDetailPreview(input: {
155 readonly status: OrchestrationV2TurnItemStatus;
156 readonly result?: string | null | undefined;
157 readonly progress?: string | null | undefined;
158}): string | null {
159 const terminal = isTerminalSubagentStatus(input.status);
160 const preferred = terminal ? input.result : input.progress;
161 const fallback = terminal ? input.progress : input.result;
162 const detail = preferred?.trimStart() || fallback?.trimStart() || "";
163 if (detail.length <= 280) return detail.trimEnd().replace(/\s+/gu, " ") || null;
164 const whitespace = /\s+/uy;
165 let compact = "";
166 for (let index = 0; index < detail.length; index += 1) {
167 whitespace.lastIndex = index;
168 const gap = whitespace.exec(detail);
169 if (gap) {
170 index += gap[0].length;
171 if (index === detail.length) break;
172 if (compact.length > 0) compact += " ";
173 }
174 compact += detail[index];
175 if (compact.length > 280) {
176 let end = 280;
177 const previous = compact.charCodeAt(end - 1);
178 const next = compact.charCodeAt(end);
179 if (previous >= 0xd800 && previous <= 0xdbff && next >= 0xdc00 && next <= 0xdfff) {
180 end -= 1;
181 }
182 return `${compact.slice(0, end).trimEnd()}…`;
183 }
184 }
185 return compact || null;
186}
187
188export function subagentCardDetail(detail: string | null | undefined): string | null {
189 if (!detail || /^Child task ended with status\b/i.test(detail)) return null;
190 return (
191 detail
192 .replace(/!?\[([^\]]+)\]\((?:\\.|[^\\()]|\((?:\\.|[^\\()])*\))*\)/g, "$1")
193 .replace(/`/g, "")
194 .replace(/^[ \t]*[-*][ \t]+/gm, "")
195 .replace(/\s+/g, " ")
196 .trim() || null
197 );
198}