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

subagentDisplay.ts

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