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

subagentDisplay.ts

138 lines5.1 KB

1import type {
2 OrchestrationV2TurnItemStatus,
3 OrchestrationV2ThreadShell,
4 OrchestrationProjectShell,
5 ServerProvider,
6} from "@t3tools/contracts";
7import { formatModelSlugName, resolveSelectableModel } from "@t3tools/shared/model";
8import { fileBasename } from "@t3tools/shared/path";
9import { resolveModelShortName } from "../providerClients.ts";
10import { isTerminalSubagentStatus } from "./subagentRuntime.ts";
11​
12export function subagentGroupSummary(
13 members: ReadonlyArray<{ readonly status: OrchestrationV2TurnItemStatus }>,
14) {
15 const active = members.some(
16 ({ status }) => status === "pending" || status === "running" || status === "waiting",
17 );
18 return {
19 label: `${active ? "Kicked off" : "Ran"} ${members.length} ${members.length === 1 ? "subagent" : "subagents"}`,
20 active,
21 failed: members.some(({ status }) => status === "failed"),
22 };
24​
25export function summarizeSubagentStatuses(
26 statuses: ReadonlyArray<OrchestrationV2TurnItemStatus>,
27): string {
28 const counts = { working: 0, done: 0, failed: 0, stopped: 0, idle: 0 };
29 for (const status of statuses) {
30 if (status === "pending" || status === "running" || status === "waiting") counts.working += 1;
31 else if (status === "completed") counts.done += 1;
32 else if (status === "failed") counts.failed += 1;
33 else if (status === "idle") counts.idle += 1;
34 else counts.stopped += 1;
35 }
36 return (Object.keys(counts) as Array<keyof typeof counts>)
37 .filter((key) => counts[key] > 0)
38 .map((key) => `${counts[key]} ${key}`)
39 .join(" · ");
41​
42export function formatSubagentDisplayTitle(title: string): string {
43 const displayTitle = title.replace(/^Subagent:\s*/i, "");
44 const path = /^\/root\/(?:[^/]+\/)*([^/]+)\/?$/u.exec(displayTitle);
45 if (path === null) return displayTitle;
46​
47 const name = path[1]!.replace(/[_\s]+/gu, " ").trim();
48 return name.replace(/(^|\s)\S/gu, (letter) => letter.toUpperCase()) || displayTitle;
50​
51export function resolveSubagentMetadata(input: {
52 readonly model: string | null;
53 readonly provider?: Pick<ServerProvider, "driver" | "models"> | null | undefined;
54 readonly parentThread?:
55 | Pick<OrchestrationV2ThreadShell, "projectId" | "worktreePath">
56 | null
57 | undefined;
58 readonly childThread?:
59 | Pick<OrchestrationV2ThreadShell, "branch" | "worktreePath">
60 | null
61 | undefined;
62 readonly parentProject?: Pick<OrchestrationProjectShell, "workspaceRoot"> | null | undefined;
63 readonly childProject?:
64 | Pick<OrchestrationProjectShell, "id" | "title" | "workspaceRoot">
65 | null
66 | undefined;
67}) {
68 const model = input.model?.trim();
69 const slug = input.provider
70 ? resolveSelectableModel(input.provider.driver, model, input.provider.models)
71 : model;
72 const catalogModel = input.provider?.models.find((candidate) => candidate.slug === slug);
73 const reportedLabel = catalogModel
74 ? (resolveModelShortName(catalogModel, input.provider?.driver) ?? catalogModel.name)
75 : model
76 ? formatModelSlugName(model)
77 : "Not reported";
78 const qualifier = catalogModel?.subProvider?.trim();
79 const modelLabel = qualifier
80 ? reportedLabel
81 .replace(
82 new RegExp(
83 `^${qualifier.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}(?:\\s*[.:/-]\\s*|\\s+)`,
84 "iu",
85 ),
86 "",
87 )
88 .trim() || reportedLabel
89 : reportedLabel;
90 const parentWorkspace = input.parentThread?.worktreePath ?? input.parentProject?.workspaceRoot;
91 const childWorkspace = input.childThread?.worktreePath ?? input.childProject?.workspaceRoot;
92 const workspace = [
93 ...(input.parentThread &&
94 input.childProject &&
95 input.childProject.id !== input.parentThread.projectId
96 ? [{ label: "Project", value: input.childProject.title }]
97 : []),
98 ...(parentWorkspace && childWorkspace && parentWorkspace !== childWorkspace
99 ? [
100 {
101 label: input.childThread?.branch
102 ? "Branch"
103 : input.childThread?.worktreePath
104 ? "Worktree"
105 : "Workspace",
106 value: input.childThread?.branch ?? fileBasename(childWorkspace),
107 },
108 ]
109 : []),
110 ];
111 return { modelLabel, workspace };
113​
114export function subagentDetailPreview(input: {
115 readonly status: OrchestrationV2TurnItemStatus;
116 readonly result?: string | null | undefined;
117 readonly progress?: string | null | undefined;
118}): string | null {
119 const terminal = isTerminalSubagentStatus(input.status);
120 const preferred = terminal ? input.result : input.progress;
121 const fallback = terminal ? input.progress : input.result;
122 const detail = preferred?.trimStart() || fallback?.trimStart() || "";
123 if (detail.length <= 280) return detail.trimEnd().replace(/\s+/gu, " ") || null;
124 const whitespace = /\s+/uy;
125 let compact = "";
126 for (let index = 0; index < detail.length; index += 1) {
127 whitespace.lastIndex = index;
128 const gap = whitespace.exec(detail);
129 if (gap) {
130 index += gap[0].length;
131 if (index === detail.length) break;
132 if (compact.length > 0) compact += " ";
133 }
134 compact += detail[index];
135 if (compact.length > 280) return `${compact.slice(0, 280).trimEnd()}…`;
136 }
137 return compact || null;