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

threadAgentsPresentation.ts

97 lines3.2 KB

1import {
2 formatSubagentDisplayTitle,
3 subagentDetailPreview,
4} from "@t3tools/client-runtime/state/subagent-display";
5import { isActiveSubagentStatus } from "@t3tools/client-runtime/state/subagentRuntime";
6import type { EnvironmentId, OrchestrationV2Subagent, ThreadId } from "@t3tools/contracts";
7​
8const PROMPT_TITLE_LIMIT = 80;
9​
10export type SubagentRowTone = "working" | "completed" | "failed" | "stopped";
11​
12export interface SubagentRowPresentation {
13 readonly title: string;
14 readonly detail: string | null;
15 readonly statusLabel: string;
16 readonly tone: SubagentRowTone;
17 readonly live: boolean;
18 readonly canOpenThread: boolean;
19 /** The linked environment the task runs in, when it runs in one. */
20 readonly runsOn: string | null;
22​
23/**
24 * Where an agent's thread opens: here, or in the linked environment that runs
25 * it. Null when there is no thread, or this app is not connected to that
26 * environment.
27 */
28export function subagentThreadTarget(
29 subagent: Pick<OrchestrationV2Subagent, "childThreadId" | "remoteChild">,
30 environmentId: EnvironmentId,
31 isKnownEnvironment: (environmentId: EnvironmentId) => boolean,
32): { readonly environmentId: EnvironmentId; readonly threadId: ThreadId } | null {
33 if (subagent.childThreadId !== null) {
34 return { environmentId, threadId: subagent.childThreadId };
35 }
36 const remote = subagent.remoteChild;
37 return remote !== undefined && isKnownEnvironment(remote.environmentId)
38 ? { environmentId: remote.environmentId, threadId: remote.threadId }
39 : null;
41​
42function rowTitle(subagent: Pick<OrchestrationV2Subagent, "title" | "prompt">): string {
43 const title = subagent.title?.trim();
44 if (title) return formatSubagentDisplayTitle(title);
45 const prompt = subagent.prompt.trim();
46 if (prompt.length === 0) return "Subagent";
47 return prompt.length > PROMPT_TITLE_LIMIT
48 ? `${prompt.slice(0, PROMPT_TITLE_LIMIT - 3)}...`
49 : prompt;
51​
52function rowTone(status: OrchestrationV2Subagent["status"]): SubagentRowTone {
53 if (isActiveSubagentStatus(status)) return "working";
54 if (status === "completed") return "completed";
55 if (status === "failed") return "failed";
56 return "stopped";
58​
59function rowStatusLabel(status: OrchestrationV2Subagent["status"]): string {
60 switch (status) {
61 case "pending":
62 case "running":
63 return "Working";
64 case "waiting":
65 return "Waiting";
66 case "idle":
67 return "Idle";
68 case "completed":
69 return "Completed";
70 case "failed":
71 return "Failed";
72 case "cancelled":
73 return "Cancelled";
74 case "interrupted":
75 return "Interrupted";
76 }
78​
79export function resolveSubagentRowPresentation(
80 subagent: Pick<
81 OrchestrationV2Subagent,
82 "title" | "prompt" | "status" | "progress" | "result" | "childThreadId" | "remoteChild"
83 >,
84 canOpenRemote = false,
85): SubagentRowPresentation {
86 const live = isActiveSubagentStatus(subagent.status);
87 return {
88 title: rowTitle(subagent),
89 detail: subagentDetailPreview(subagent),
90 statusLabel: rowStatusLabel(subagent.status),
91 tone: rowTone(subagent.status),
92 live,
93 canOpenThread:
94 subagent.childThreadId !== null || (subagent.remoteChild !== undefined && canOpenRemote),
95 runsOn: subagent.remoteChild?.label ?? null,
96 };