The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { resolveLinkedThreadOpen } from "@t3tools/client-runtime/linked-thread"; |
| 2 | import { |
| 3 | formatSubagentDisplayTitle, |
| 4 | resolveSubagentMetadata, |
| 5 | subagentCardDetail, |
| 6 | subagentDetailPreview, |
| 7 | } from "@t3tools/client-runtime/state/subagent-display"; |
| 8 | import { |
| 9 | isActiveSubagentStatus, |
| 10 | type RuntimeSubagent, |
| 11 | type SubagentWorkflowGroup, |
| 12 | } from "@t3tools/client-runtime/state/subagentRuntime"; |
| 13 | import { REASONING_EFFORT_OPTION_IDS } from "@t3tools/client-runtime/state/thread-execution"; |
| 14 | import type { |
| 15 | EnvironmentId, |
| 16 | OrchestrationV2Subagent, |
| 17 | ServerProvider, |
| 18 | ThreadId, |
| 19 | } from "@t3tools/contracts"; |
| 20 | import { resolveSelectableModel } from "@t3tools/shared/model"; |
| 21 | import { msg } from "gt-react-native"; |
| 22 | |
| 23 | import { m, type MessageResolver } from "../../i18n/messages"; |
| 24 | |
| 25 | const PROMPT_TITLE_LIMIT = 80; |
| 26 | |
| 27 | export type SubagentRowTone = "working" | "completed" | "failed" | "stopped"; |
| 28 | |
| 29 | export interface SubagentRowPresentation { |
| 30 | readonly title: string; |
| 31 | readonly detail: string | null; |
| 32 | readonly statusLabel: string; |
| 33 | readonly tone: SubagentRowTone; |
| 34 | readonly live: boolean; |
| 35 | readonly canOpenThread: boolean; |
| 36 | /** The linked environment the task runs in, when it runs in one. */ |
| 37 | readonly runsOn: string | null; |
| 38 | } |
| 39 | |
| 40 | export interface SubagentThreadTarget { |
| 41 | readonly environmentId: EnvironmentId; |
| 42 | readonly threadId: ThreadId; |
| 43 | /** Set when the thread is read through the environment that links to it, not opened there. */ |
| 44 | readonly linked?: { readonly viaEnvironmentId: EnvironmentId; readonly label: string }; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Where an agent's thread opens: here, in the linked environment that runs it when this app is |
| 49 | * connected there, or read-only through this environment's link. Once the link is gone a saved |
| 50 | * environment opens in place, where the app can reconnect to it; with neither, the read-only view |
| 51 | * says what to link again. Null when there is no thread. |
| 52 | */ |
| 53 | export function subagentThreadTarget( |
| 54 | subagent: Pick<OrchestrationV2Subagent, "childThreadId" | "remoteChild">, |
| 55 | environmentId: EnvironmentId, |
| 56 | ownConnection: (environmentId: EnvironmentId) => { readonly connected: boolean } | undefined, |
| 57 | /** Whether `environmentId` still holds a link to the given environment. */ |
| 58 | isLinked: (environmentId: EnvironmentId) => boolean, |
| 59 | ): SubagentThreadTarget | null { |
| 60 | if (subagent.childThreadId !== null) { |
| 61 | return { environmentId, threadId: subagent.childThreadId }; |
| 62 | } |
| 63 | const remote = subagent.remoteChild; |
| 64 | if (remote === undefined) return null; |
| 65 | const target = { environmentId: remote.environmentId, threadId: remote.threadId }; |
| 66 | const open = resolveLinkedThreadOpen({ |
| 67 | environmentId: remote.environmentId, |
| 68 | viaEnvironmentId: environmentId, |
| 69 | own: ownConnection(remote.environmentId), |
| 70 | linked: isLinked(remote.environmentId), |
| 71 | }); |
| 72 | return open === "direct" |
| 73 | ? target |
| 74 | : { ...target, linked: { viaEnvironmentId: environmentId, label: remote.label } }; |
| 75 | } |
| 76 | |
| 77 | function rowTitle( |
| 78 | subagent: Pick<OrchestrationV2Subagent, "title" | "prompt">, |
| 79 | resolve: MessageResolver, |
| 80 | ): string { |
| 81 | const title = subagent.title?.trim(); |
| 82 | if (title) return formatSubagentDisplayTitle(title); |
| 83 | const prompt = subagent.prompt.trim(); |
| 84 | if (prompt.length === 0) return resolve(msg("Subagent")); |
| 85 | return prompt.length > PROMPT_TITLE_LIMIT |
| 86 | ? `${prompt.slice(0, PROMPT_TITLE_LIMIT - 3)}...` |
| 87 | : prompt; |
| 88 | } |
| 89 | |
| 90 | function rowTone(status: OrchestrationV2Subagent["status"]): SubagentRowTone { |
| 91 | if (isActiveSubagentStatus(status)) return "working"; |
| 92 | if (status === "completed") return "completed"; |
| 93 | if (status === "failed") return "failed"; |
| 94 | return "stopped"; |
| 95 | } |
| 96 | |
| 97 | function rowStatusLabel( |
| 98 | status: OrchestrationV2Subagent["status"], |
| 99 | resolve: MessageResolver, |
| 100 | ): string { |
| 101 | switch (status) { |
| 102 | case "pending": |
| 103 | case "running": |
| 104 | return resolve(msg("Working", { $context: "agent status" })); |
| 105 | case "waiting": |
| 106 | return resolve(msg("Waiting", { $context: "agent status" })); |
| 107 | case "idle": |
| 108 | return resolve(msg("Idle", { $context: "agent status" })); |
| 109 | case "completed": |
| 110 | return resolve(msg("Completed", { $context: "agent status" })); |
| 111 | case "failed": |
| 112 | return resolve(msg("Failed", { $context: "agent status" })); |
| 113 | case "cancelled": |
| 114 | return resolve(msg("Cancelled", { $context: "agent status" })); |
| 115 | case "interrupted": |
| 116 | return resolve(msg("Interrupted", { $context: "agent status" })); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | export function resolveSubagentRowPresentation( |
| 121 | subagent: Pick< |
| 122 | OrchestrationV2Subagent, |
| 123 | "title" | "prompt" | "status" | "progress" | "result" | "childThreadId" | "remoteChild" |
| 124 | >, |
| 125 | canOpenRemote = false, |
| 126 | resolve: MessageResolver = m, |
| 127 | ): SubagentRowPresentation { |
| 128 | const live = isActiveSubagentStatus(subagent.status); |
| 129 | return { |
| 130 | title: rowTitle(subagent, resolve), |
| 131 | detail: subagentDetailPreview(subagent), |
| 132 | statusLabel: rowStatusLabel(subagent.status, resolve), |
| 133 | tone: rowTone(subagent.status), |
| 134 | live, |
| 135 | canOpenThread: |
| 136 | subagent.childThreadId !== null || (subagent.remoteChild !== undefined && canOpenRemote), |
| 137 | runsOn: subagent.remoteChild?.label ?? null, |
| 138 | }; |
| 139 | } |
| 140 | |
| 141 | export type WorkflowRailTone = SubagentRowTone | "queued"; |
| 142 | |
| 143 | export interface WorkflowRailItem { |
| 144 | readonly key: string; |
| 145 | readonly title: string; |
| 146 | readonly members: ReadonlyArray<RuntimeSubagent>; |
| 147 | readonly completedCount: number; |
| 148 | readonly tone: WorkflowRailTone; |
| 149 | } |
| 150 | |
| 151 | function railTone(members: ReadonlyArray<RuntimeSubagent>): WorkflowRailTone { |
| 152 | if (members.every((member) => member.status === "pending")) return "queued"; |
| 153 | if (members.some((member) => isActiveSubagentStatus(member.status) || member.status === "idle")) { |
| 154 | return "working"; |
| 155 | } |
| 156 | if (members.some((member) => member.status === "failed")) return "failed"; |
| 157 | return members.every((member) => member.status === "completed") ? "completed" : "stopped"; |
| 158 | } |
| 159 | |
| 160 | export function workflowRail( |
| 161 | group: SubagentWorkflowGroup, |
| 162 | resolve: MessageResolver = m, |
| 163 | ): ReadonlyArray<WorkflowRailItem> { |
| 164 | const items = group.phases.map((phase) => ({ |
| 165 | key: String(phase.index), |
| 166 | title: phase.title, |
| 167 | members: phase.members, |
| 168 | })); |
| 169 | if (group.unphasedMembers.length > 0) { |
| 170 | items.push({ |
| 171 | key: "unphased", |
| 172 | title: |
| 173 | items.length === 0 |
| 174 | ? resolve(msg("Agents")) |
| 175 | : resolve(msg("Other", { $context: "group of agents outside any workflow phase" })), |
| 176 | members: group.unphasedMembers, |
| 177 | }); |
| 178 | } |
| 179 | return items.map((item) => ({ |
| 180 | ...item, |
| 181 | completedCount: item.members.filter((member) => member.status === "completed").length, |
| 182 | tone: railTone(item.members), |
| 183 | })); |
| 184 | } |
| 185 | |
| 186 | export function nextWorkflowSelection( |
| 187 | rail: ReadonlyArray<WorkflowRailItem>, |
| 188 | selected: string | null, |
| 189 | tapped?: string, |
| 190 | ): string | null { |
| 191 | const open = rail.some((item) => item.key === selected) ? selected : null; |
| 192 | if (tapped !== undefined) return tapped === open ? null : tapped; |
| 193 | if (open !== null) return null; |
| 194 | const filled = rail.filter((item) => item.members.length > 0); |
| 195 | return ( |
| 196 | ( |
| 197 | filled.find((item) => item.tone === "working") ?? |
| 198 | filled.find((item) => item.tone === "failed") ?? |
| 199 | filled.at(-1) |
| 200 | )?.key ?? null |
| 201 | ); |
| 202 | } |
| 203 | |
| 204 | function capitalize(value: string): string { |
| 205 | return value.charAt(0).toUpperCase() + value.slice(1); |
| 206 | } |
| 207 | |
| 208 | export function workflowMemberMetadata( |
| 209 | member: Pick<RuntimeSubagent, "model" | "effort" | "speed">, |
| 210 | provider: Pick<ServerProvider, "driver" | "models"> | undefined, |
| 211 | resolve: MessageResolver = m, |
| 212 | ) { |
| 213 | const model = member.model?.trim() |
| 214 | ? resolveSubagentMetadata({ model: member.model, provider }, resolve).modelLabel |
| 215 | : null; |
| 216 | const slug = provider |
| 217 | ? resolveSelectableModel(provider.driver, member.model, provider.models) |
| 218 | : null; |
| 219 | const effortChoice = provider?.models |
| 220 | .find((candidate) => candidate.slug === slug) |
| 221 | ?.capabilities?.optionDescriptors?.flatMap((descriptor) => |
| 222 | descriptor.type === "select" && REASONING_EFFORT_OPTION_IDS.includes(descriptor.id) |
| 223 | ? descriptor.options |
| 224 | : [], |
| 225 | ) |
| 226 | .find((option) => option.id === member.effort); |
| 227 | return { |
| 228 | model, |
| 229 | effort: member.effort ? (effortChoice?.label ?? capitalize(member.effort)) : null, |
| 230 | speed: member.speed ? capitalize(member.speed) : null, |
| 231 | fast: member.speed === "fast", |
| 232 | }; |
| 233 | } |
| 234 | |
| 235 | export function workflowMemberDetail( |
| 236 | member: Pick<RuntimeSubagent, "status" | "progress" | "result" | "error">, |
| 237 | ) { |
| 238 | return { |
| 239 | detail: subagentCardDetail(subagentDetailPreview(member)), |
| 240 | error: member.status === "failed" ? subagentCardDetail(member.error) : null, |
| 241 | }; |
| 242 | } |
| 243 | |
| 244 | export function resolveSubagentStopTarget( |
| 245 | subagent: Pick< |
| 246 | OrchestrationV2Subagent, |
| 247 | | "id" |
| 248 | | "threadId" |
| 249 | | "childThreadId" |
| 250 | | "status" |
| 251 | | "origin" |
| 252 | | "driver" |
| 253 | | "runId" |
| 254 | | "nativeTaskRef" |
| 255 | | "workflow" |
| 256 | >, |
| 257 | ) { |
| 258 | if (!isActiveSubagentStatus(subagent.status)) return null; |
| 259 | if (subagent.workflow !== undefined) { |
| 260 | return subagent.status === "running" && |
| 261 | subagent.origin === "provider_native" && |
| 262 | subagent.driver === "claudeAgent" && |
| 263 | Boolean(subagent.nativeTaskRef?.nativeId) |
| 264 | ? { |
| 265 | kind: "workflow" as const, |
| 266 | input: { threadId: subagent.threadId, subagentId: subagent.id }, |
| 267 | } |
| 268 | : null; |
| 269 | } |
| 270 | if (subagent.origin === "app_owned" && subagent.childThreadId !== null) { |
| 271 | return { |
| 272 | kind: "subagent" as const, |
| 273 | input: { threadId: subagent.threadId, subagentId: subagent.id }, |
| 274 | }; |
| 275 | } |
| 276 | if ( |
| 277 | subagent.origin === "provider_native" && |
| 278 | subagent.driver === "claudeAgent" && |
| 279 | subagent.runId !== null && |
| 280 | subagent.nativeTaskRef?.strength === "strong" && |
| 281 | subagent.nativeTaskRef.nativeId !== null |
| 282 | ) { |
| 283 | return { |
| 284 | kind: "subagent" as const, |
| 285 | input: { threadId: subagent.threadId, subagentId: subagent.id }, |
| 286 | }; |
| 287 | } |
| 288 | return null; |
| 289 | } |