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

models.ts

238 lines8.6 KB

1import type {
2 ThreadLinkedPullRequest,
3 EnvironmentId,
4 MessageId,
5 OrchestrationProjectShell,
6 OrchestrationV2RunStatus,
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 pendingBackgroundTasks: ReadonlyArray<
90 NonNullable<OrchestrationV2ThreadShell["pendingBackgroundTasks"]>[number]
91 >;
92 readonly itemCount: number;
93 readonly visibleItemCount: number;
94 readonly createdAt: string;
95 readonly updatedAt: string;
96 readonly archivedAt: string | null;
97 readonly settledOverride: "settled" | "active" | null;
98 readonly settledAt: string | null;
99 readonly snoozedUntil: string | null;
100 readonly snoozedAt: string | null;
101 readonly pinnedAt: string | null;
102 /** Slot in the user-arranged pinned order; null for keyless (legacy) pins. */
103 readonly pinOrderKey: string | null;
104 /**
105 * Pull request the user linked to the thread (#8160). The v2 server does not
106 * project this yet, so it stays undefined on v2 environments; UI treats
107 * undefined and null alike.
108 */
109 readonly linkedPullRequest?: ThreadLinkedPullRequest | null;
110 /**
111 * Server-tracked visited watermark. `undefined` means the environment's
112 * server predates visited tracking and clients should fall back to any
113 * local visited state they keep.
114 */
115 readonly lastVisitedAt?: string | null;
116 /** Pending title regeneration marker; null when no request is in flight. */
117 readonly titleRegeneration?: { readonly requestId: string; readonly startedAt: string } | null;
118 readonly deletedAt: string | null;
119 readonly source: OrchestrationV2ThreadShell;
121​
122function iso(value: DateTime.Utc): string {
123 return DateTime.formatIso(value);
125​
126function nullableIso(value: DateTime.Utc | null): string | null {
127 return value === null ? null : iso(value);
129​
130function terminalRunStatus(status: OrchestrationV2RunStatus): boolean {
131 return (
132 status === "completed" ||
133 status === "interrupted" ||
134 status === "failed" ||
135 status === "cancelled" ||
136 status === "rolled_back"
137 );
139​
140// Park runtime at idle when the post-settlement background roster is nonempty
141// so #4415 waiting-presentation Waiting (session.idle) can consume CTM runtime.
142// The server suppresses the roster while an interruptible activity run exists,
143// so a remaining roster is stronger than checkpoint-oriented waiting.
144// latestRun keeps the latest run's status for history presentation.
145function shellRuntime(thread: OrchestrationV2ThreadShell): ThreadRuntimeSummary | null {
146 if (thread.latestRunId === null && thread.activeProviderThreadId === null) return null;
147 const hasPendingBackgroundTasks = (thread.pendingBackgroundTasks?.length ?? 0) > 0;
148 const status = hasPendingBackgroundTasks ? "idle" : (thread.activityRunStatus ?? thread.status);
149 return {
150 status,
151 activeRunId: thread.activeRunId,
152 providerInstanceId: thread.providerInstanceId,
153 providerName: null,
154 lastError: thread.lastError ?? null,
155 updatedAt: iso(thread.updatedAt),
156 };
158​
159export function scopeProject(
160 environmentId: EnvironmentId,
161 project: OrchestrationProjectShell,
162): EnvironmentProject {
163 return { ...project, environmentId };
165​
166export function presentThreadShell(
167 environmentId: EnvironmentId,
168 thread: OrchestrationV2ThreadShell,
169): EnvironmentThreadShell {
170 const updatedAt = iso(thread.updatedAt);
171 const latestRun =
172 thread.latestRunId === null
173 ? null
174 : ({
175 runId: thread.latestRunId,
176 status: thread.status === "idle" ? "completed" : thread.status,
177 requestedAt: nullableIso(thread.latestRunRequestedAt ?? null),
178 startedAt: nullableIso(thread.latestRunStartedAt ?? null),
179 completedAt:
180 thread.latestRunCompletedAt === undefined
181 ? thread.status === "idle" || terminalRunStatus(thread.status)
182 ? updatedAt
183 : null
184 : nullableIso(thread.latestRunCompletedAt),
185 assistantMessageId: null,
186 } satisfies ThreadRunSummary);
187 return {
188 environmentId,
189 id: thread.id,
190 projectId: thread.projectId,
191 title: thread.title,
192 providerInstanceId: thread.providerInstanceId,
193 modelSelection: thread.modelSelection,
194 runtimeMode: thread.runtimeMode,
195 interactionMode: thread.interactionMode,
196 branch: thread.branch,
197 worktreePath: thread.worktreePath,
198 linkedPullRequest: thread.linkedPullRequest ?? null,
199 lineage: thread.lineage,
200 forkedFrom: thread.forkedFrom,
201 activeProviderThreadId: thread.activeProviderThreadId,
202 latestRun,
203 runtime: shellRuntime(thread),
204 latestUserMessageAt: nullableIso(thread.latestUserMessageAt),
205 hasPendingApprovals:
206 thread.pendingRuntimeRequest !== null &&
207 thread.pendingRuntimeRequest.kind !== "user_input" &&
208 thread.pendingRuntimeRequest.kind !== "auth_refresh",
209 hasPendingUserInput: thread.pendingRuntimeRequest?.kind === "user_input",
210 hasActionableProposedPlan: thread.hasActionableProposedPlan,
211 pendingBackgroundTasks: thread.pendingBackgroundTasks ?? [],
212 itemCount: thread.itemCount,
213 visibleItemCount: thread.visibleItemCount,
214 createdAt: iso(thread.createdAt),
215 updatedAt,
216 archivedAt: nullableIso(thread.archivedAt),
217 settledOverride: thread.settledOverride,
218 settledAt: nullableIso(thread.settledAt),
219 snoozedUntil: nullableIso(thread.snoozedUntil ?? null),
220 snoozedAt: nullableIso(thread.snoozedAt ?? null),
221 pinnedAt: nullableIso(thread.pinnedAt ?? null),
222 pinOrderKey: thread.pinOrderKey ?? null,
223 ...(thread.lastVisitedAt === undefined
224 ? {}
225 : { lastVisitedAt: nullableIso(thread.lastVisitedAt) }),
226 titleRegeneration:
227 thread.titleRegeneration == null
228 ? null
229 : {
230 requestId: thread.titleRegeneration.requestId,
231 startedAt: iso(thread.titleRegeneration.startedAt),
232 },
233 deletedAt: nullableIso(thread.deletedAt),
234 source: thread,
235 };
237​
238export const scopeThreadShell = presentThreadShell;