Open History to see the last commit
1import type {
2 EnvironmentId,
3 MessageId,
4 OrchestrationProjectShell,
5 OrchestrationV2RunStatus,
6 OrchestrationV2ShellSnapshot,
7 OrchestrationV2ThreadProjection,
8 OrchestrationV2ThreadShell,
9 PlanId,
10 ProjectId,
11 ProviderInstanceId,
12 RunId,
13 ThreadId,
14} from "@t3tools/contracts";
15import * as DateTime from "effect/DateTime";
16
17export interface EnvironmentProject extends OrchestrationProjectShell {
18 readonly environmentId: EnvironmentId;
19}
20
21/**
22 * A pristine V2 thread projection paired with the environment that produced it.
23 *
24 * The projection stays nested so the server projection and all structurally
25 * shared collections retain their identities. Rich consumers read V2 state
26 * directly instead of a second presentation-shaped thread graph.
27 */
28export interface EnvironmentThread {
29 readonly environmentId: EnvironmentId;
30 readonly projection: OrchestrationV2ThreadProjection;
31}
32
33export interface ThreadRunSummary {
34 readonly runId: RunId;
35 readonly status: OrchestrationV2RunStatus;
36 readonly requestedAt: string | null;
37 readonly startedAt: string | null;
38 readonly completedAt: string | null;
39 readonly assistantMessageId: MessageId | null;
40 readonly sourcePlanRef?: {
41 readonly threadId: ThreadId;
42 readonly planId: PlanId;
43 };
44}
45
46export interface ThreadRuntimeSummary {
47 readonly status: OrchestrationV2RunStatus | "idle";
48 readonly activeRunId: RunId | null;
49 readonly providerInstanceId: ProviderInstanceId;
50 readonly providerName: string | null;
51 readonly lastError: string | null;
52 readonly updatedAt: string;
53}
54
55export function threadRuntimeIsActive(runtime: ThreadRuntimeSummary | null | undefined): boolean {
56 return runtime !== null && runtime !== undefined && threadRunStatusIsActive(runtime.status);
57}
58
59export function threadRunStatusIsActive(status: ThreadRuntimeSummary["status"]): boolean {
60 return (
61 status === "preparing" ||
62 status === "queued" ||
63 status === "starting" ||
64 status === "running" ||
65 status === "waiting"
66 );
67}
68
69export interface EnvironmentThreadShell {
70 readonly environmentId: EnvironmentId;
71 readonly id: ThreadId;
72 readonly projectId: ProjectId;
73 readonly title: string;
74 readonly providerInstanceId: ProviderInstanceId;
75 readonly modelSelection: OrchestrationV2ThreadShell["modelSelection"];
76 readonly runtimeMode: OrchestrationV2ThreadShell["runtimeMode"];
77 readonly interactionMode: OrchestrationV2ThreadShell["interactionMode"];
78 readonly branch: string | null;
79 readonly worktreePath: string | null;
80 readonly lineage: OrchestrationV2ThreadShell["lineage"];
81 readonly forkedFrom: OrchestrationV2ThreadShell["forkedFrom"];
82 readonly activeProviderThreadId: OrchestrationV2ThreadShell["activeProviderThreadId"];
83 readonly latestRun: ThreadRunSummary | null;
84 readonly runtime: ThreadRuntimeSummary | null;
85 readonly latestUserMessageAt: string | null;
86 readonly hasPendingApprovals: boolean;
87 readonly hasPendingUserInput: boolean;
88 readonly hasActionableProposedPlan: boolean;
89 readonly itemCount: number;
90 readonly visibleItemCount: number;
91 readonly createdAt: string;
92 readonly updatedAt: string;
93 readonly archivedAt: string | null;
94 readonly deletedAt: string | null;
95 readonly source: OrchestrationV2ThreadShell;
96}
97
98function iso(value: DateTime.Utc): string {
99 return DateTime.formatIso(value);
100}
101
102function nullableIso(value: DateTime.Utc | null): string | null {
103 return value === null ? null : iso(value);
104}
105
106function terminalRunStatus(status: OrchestrationV2RunStatus): boolean {
107 return (
108 status === "completed" ||
109 status === "interrupted" ||
110 status === "failed" ||
111 status === "cancelled" ||
112 status === "rolled_back"
113 );
114}
115
116function shellRuntime(thread: OrchestrationV2ThreadShell): ThreadRuntimeSummary | null {
117 if (thread.latestRunId === null && thread.activeProviderThreadId === null) return null;
118 return {
119 status: thread.status,
120 activeRunId: thread.activeRunId,
121 providerInstanceId: thread.providerInstanceId,
122 providerName: null,
123 lastError: null,
124 updatedAt: iso(thread.updatedAt),
125 };
126}
127
128export function scopeProject(
129 environmentId: EnvironmentId,
130 project: OrchestrationProjectShell,
131): EnvironmentProject {
132 return { ...project, environmentId };
133}
134
135export function presentThreadShell(
136 environmentId: EnvironmentId,
137 thread: OrchestrationV2ThreadShell,
138): EnvironmentThreadShell {
139 const updatedAt = iso(thread.updatedAt);
140 const latestRun =
141 thread.latestRunId === null
142 ? null
143 : ({
144 runId: thread.latestRunId,
145 status: thread.status === "idle" ? "completed" : thread.status,
146 requestedAt: null,
147 startedAt: null,
148 completedAt:
149 thread.status === "idle" || terminalRunStatus(thread.status) ? updatedAt : null,
150 assistantMessageId: null,
151 } satisfies ThreadRunSummary);
152 return {
153 environmentId,
154 id: thread.id,
155 projectId: thread.projectId,
156 title: thread.title,
157 providerInstanceId: thread.providerInstanceId,
158 modelSelection: thread.modelSelection,
159 runtimeMode: thread.runtimeMode,
160 interactionMode: thread.interactionMode,
161 branch: thread.branch,
162 worktreePath: thread.worktreePath,
163 lineage: thread.lineage,
164 forkedFrom: thread.forkedFrom,
165 activeProviderThreadId: thread.activeProviderThreadId,
166 latestRun,
167 runtime: shellRuntime(thread),
168 latestUserMessageAt: nullableIso(thread.latestUserMessageAt),
169 hasPendingApprovals:
170 thread.pendingRuntimeRequest !== null &&
171 thread.pendingRuntimeRequest.kind !== "user_input" &&
172 thread.pendingRuntimeRequest.kind !== "auth_refresh",
173 hasPendingUserInput: thread.pendingRuntimeRequest?.kind === "user_input",
174 hasActionableProposedPlan: thread.hasActionableProposedPlan,
175 itemCount: thread.itemCount,
176 visibleItemCount: thread.visibleItemCount,
177 createdAt: iso(thread.createdAt),
178 updatedAt,
179 archivedAt: nullableIso(thread.archivedAt),
180 deletedAt: nullableIso(thread.deletedAt),
181 source: thread,
182 };
183}
184
185export const scopeThreadShell = presentThreadShell;
186
187export function selectEnvironmentThreadShell(
188 snapshot: OrchestrationV2ShellSnapshot | null,
189 environmentId: EnvironmentId,
190 threadId: ThreadId,
191): EnvironmentThreadShell | null {
192 const thread = snapshot?.threads.find((candidate) => candidate.id === threadId) ?? null;
193 return thread ? presentThreadShell(environmentId, thread) : null;
194}