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

models.ts

224 lines7.6 KB

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;
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;
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 };
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;
54​
55export function threadRuntimeIsActive(runtime: ThreadRuntimeSummary | null | undefined): boolean {
56 return runtime !== null && runtime !== undefined && threadRunStatusIsActive(runtime.status);
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 );
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 settledOverride: "settled" | "active" | null;
95 readonly settledAt: string | null;
96 readonly snoozedUntil: string | null;
97 readonly snoozedAt: string | null;
98 /**
99 * Server-tracked visited watermark. `undefined` means the environment's
100 * server predates visited tracking and clients should fall back to any
101 * local visited state they keep.
102 */
103 readonly lastVisitedAt?: string | null;
104 /** Pending title regeneration marker; null when no request is in flight. */
105 readonly titleRegeneration?: { readonly requestId: string; readonly startedAt: string } | null;
106 readonly deletedAt: string | null;
107 readonly source: OrchestrationV2ThreadShell;
109​
110function iso(value: DateTime.Utc): string {
111 return DateTime.formatIso(value);
113​
114function nullableIso(value: DateTime.Utc | null): string | null {
115 return value === null ? null : iso(value);
117​
118function terminalRunStatus(status: OrchestrationV2RunStatus): boolean {
119 return (
120 status === "completed" ||
121 status === "interrupted" ||
122 status === "failed" ||
123 status === "cancelled" ||
124 status === "rolled_back"
125 );
127​
128function shellRuntime(thread: OrchestrationV2ThreadShell): ThreadRuntimeSummary | null {
129 if (thread.latestRunId === null && thread.activeProviderThreadId === null) return null;
130 return {
131 status: thread.status,
132 activeRunId: thread.activeRunId,
133 providerInstanceId: thread.providerInstanceId,
134 providerName: null,
135 lastError: thread.lastError ?? null,
136 updatedAt: iso(thread.updatedAt),
137 };
139​
140export function scopeProject(
141 environmentId: EnvironmentId,
142 project: OrchestrationProjectShell,
143): EnvironmentProject {
144 return { ...project, environmentId };
146​
147export function presentThreadShell(
148 environmentId: EnvironmentId,
149 thread: OrchestrationV2ThreadShell,
150): EnvironmentThreadShell {
151 const updatedAt = iso(thread.updatedAt);
152 const latestRun =
153 thread.latestRunId === null
154 ? null
155 : ({
156 runId: thread.latestRunId,
157 status: thread.status === "idle" ? "completed" : thread.status,
158 requestedAt: nullableIso(thread.latestRunRequestedAt ?? null),
159 startedAt: nullableIso(thread.latestRunStartedAt ?? null),
160 completedAt:
161 thread.latestRunCompletedAt === undefined
162 ? thread.status === "idle" || terminalRunStatus(thread.status)
163 ? updatedAt
164 : null
165 : nullableIso(thread.latestRunCompletedAt),
166 assistantMessageId: null,
167 } satisfies ThreadRunSummary);
168 return {
169 environmentId,
170 id: thread.id,
171 projectId: thread.projectId,
172 title: thread.title,
173 providerInstanceId: thread.providerInstanceId,
174 modelSelection: thread.modelSelection,
175 runtimeMode: thread.runtimeMode,
176 interactionMode: thread.interactionMode,
177 branch: thread.branch,
178 worktreePath: thread.worktreePath,
179 lineage: thread.lineage,
180 forkedFrom: thread.forkedFrom,
181 activeProviderThreadId: thread.activeProviderThreadId,
182 latestRun,
183 runtime: shellRuntime(thread),
184 latestUserMessageAt: nullableIso(thread.latestUserMessageAt),
185 hasPendingApprovals:
186 thread.pendingRuntimeRequest !== null &&
187 thread.pendingRuntimeRequest.kind !== "user_input" &&
188 thread.pendingRuntimeRequest.kind !== "auth_refresh",
189 hasPendingUserInput: thread.pendingRuntimeRequest?.kind === "user_input",
190 hasActionableProposedPlan: thread.hasActionableProposedPlan,
191 itemCount: thread.itemCount,
192 visibleItemCount: thread.visibleItemCount,
193 createdAt: iso(thread.createdAt),
194 updatedAt,
195 archivedAt: nullableIso(thread.archivedAt),
196 settledOverride: thread.settledOverride,
197 settledAt: nullableIso(thread.settledAt),
198 snoozedUntil: nullableIso(thread.snoozedUntil ?? null),
199 snoozedAt: nullableIso(thread.snoozedAt ?? null),
200 ...(thread.lastVisitedAt === undefined
201 ? {}
202 : { lastVisitedAt: nullableIso(thread.lastVisitedAt) }),
203 titleRegeneration:
204 thread.titleRegeneration == null
205 ? null
206 : {
207 requestId: thread.titleRegeneration.requestId,
208 startedAt: iso(thread.titleRegeneration.startedAt),
209 },
210 deletedAt: nullableIso(thread.deletedAt),
211 source: thread,
212 };
214​
215export const scopeThreadShell = presentThreadShell;
216​
217export function selectEnvironmentThreadShell(
218 snapshot: OrchestrationV2ShellSnapshot | null,
219 environmentId: EnvironmentId,
220 threadId: ThreadId,
221): EnvironmentThreadShell | null {
222 const thread = snapshot?.threads.find((candidate) => candidate.id === threadId) ?? null;
223 return thread ? presentThreadShell(environmentId, thread) : null;