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