sailfork / t3code-github / packages/ client-runtime/ src/ state/ subagentDisplay.ts
import type {
ModelSelection,
OrchestrationV2TurnItemStatus,
OrchestrationV2ThreadShell,
OrchestrationProjectShell,
ServerProvider,
} from "@t3tools/contracts";
import { formatModelSlugName, resolveSelectableModel } from "@t3tools/shared/model";
import { fileBasename } from "@t3tools/shared/path";
import { resolveModelShortName } from "../providerClients.ts";
import { isTerminalSubagentStatus } from "./subagentRuntime.ts";
export function subagentGroupSummary(
members: ReadonlyArray<{ readonly status: OrchestrationV2TurnItemStatus }>,
) {
const active = members.some(
({ status }) => status === "pending" || status === "running" || status === "waiting",
);
return {
label: `${active ? "Kicked off" : "Ran"} ${members.length} ${members.length === 1 ? "subagent" : "subagents"}`,
active,
failed: members.some(({ status }) => status === "failed"),
};
}
export function summarizeSubagentStatuses(
statuses: ReadonlyArray<OrchestrationV2TurnItemStatus>,
): string {
const counts = { working: 0, done: 0, failed: 0, stopped: 0, idle: 0 };
for (const status of statuses) {
if (status === "pending" || status === "running" || status === "waiting") counts.working += 1;
else if (status === "completed") counts.done += 1;
else if (status === "failed") counts.failed += 1;
else if (status === "idle") counts.idle += 1;
else counts.stopped += 1;
}
return (Object.keys(counts) as Array<keyof typeof counts>)
.filter((key) => counts[key] > 0)
.map((key) => `${counts[key]} ${key}`)
.join(" · ");
}
export function formatSubagentDisplayTitle(title: string): string {
const displayTitle = title.replace(/^Subagent:\s*/i, "");
const path = /^\/root\/(?:[^/]+\/)*([^/]+)\/?$/u.exec(displayTitle);
if (path === null) return displayTitle;
const name = path[1]!.replace(/[_\s]+/gu, " ").trim();
return name.replace(/(^|\s)\S/gu, (letter) => letter.toUpperCase()) || displayTitle;
}
export function resolveSubagentMetadata(input: {
readonly model: string | null;
readonly modelSelection?: Pick<ModelSelection, "model"> | undefined;
readonly provider?: Pick<ServerProvider, "driver" | "models"> | null | undefined;
readonly parentThread?:
| Pick<OrchestrationV2ThreadShell, "projectId" | "worktreePath">
| null
| undefined;
readonly childThread?:
| Pick<OrchestrationV2ThreadShell, "branch" | "worktreePath">
| null
| undefined;
readonly parentProject?: Pick<OrchestrationProjectShell, "workspaceRoot"> | null | undefined;
readonly childProject?:
| Pick<OrchestrationProjectShell, "id" | "title" | "workspaceRoot">
| null
| undefined;
}) {
const model = input.model?.trim() || input.modelSelection?.model.trim();
const slug = input.provider
? resolveSelectableModel(input.provider.driver, model, input.provider.models)
: model;
const catalogModel = input.provider?.models.find((candidate) => candidate.slug === slug);
const reportedLabel = catalogModel
? (resolveModelShortName(catalogModel, input.provider?.driver) ?? catalogModel.name)
: model
? formatModelSlugName(model)
: "Not reported";
const qualifier = catalogModel?.subProvider?.trim();
const modelLabel = qualifier
? reportedLabel
.replace(
new RegExp(
`^${qualifier.replace(/[.*+?^${}()|[\]\\]/g, "\\Loading Sailfork
amp;")}(?:\\s*[.:/-]\\s*|\\s+)`,
"iu",
),
"",
)
.trim() || reportedLabel
: reportedLabel;
const parentWorkspace = input.parentThread?.worktreePath ?? input.parentProject?.workspaceRoot;
const childWorkspace = input.childThread?.worktreePath ?? input.childProject?.workspaceRoot;
const workspace = [
...(input.parentThread &&
input.childProject &&
input.childProject.id !== input.parentThread.projectId
? [{ label: "Project", value: input.childProject.title }]
: []),
...(parentWorkspace && childWorkspace && parentWorkspace !== childWorkspace
? [
{
label: input.childThread?.branch
? "Branch"
: input.childThread?.worktreePath
? "Worktree"
: "Workspace",
value: input.childThread?.branch ?? fileBasename(childWorkspace),
},
]
: []),
];
return { modelLabel, modelSlug: slug ?? model ?? null, workspace };
}
export function subagentDetailPreview(input: {
readonly status: OrchestrationV2TurnItemStatus;
readonly result?: string | null | undefined;
readonly progress?: string | null | undefined;
}): string | null {
const terminal = isTerminalSubagentStatus(input.status);
const preferred = terminal ? input.result : input.progress;
const fallback = terminal ? input.progress : input.result;
const detail = preferred?.trimStart() || fallback?.trimStart() || "";
if (detail.length <= 280) return detail.trimEnd().replace(/\s+/gu, " ") || null;
const whitespace = /\s+/uy;
let compact = "";
for (let index = 0; index < detail.length; index += 1) {
whitespace.lastIndex = index;
const gap = whitespace.exec(detail);
if (gap) {
index += gap[0].length;
if (index === detail.length) break;
if (compact.length > 0) compact += " ";
}
compact += detail[index];
if (compact.length > 280) {
let end = 280;
const previous = compact.charCodeAt(end - 1);
const next = compact.charCodeAt(end);
if (previous >= 0xd800 && previous <= 0xdbff && next >= 0xdc00 && next <= 0xdfff) {
end -= 1;
}
return `${compact.slice(0, end).trimEnd()}…`;
}
}
return compact || null;
}