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

threadExecution.ts

89 lines3.6 KB

1import type { OrchestrationV2ThreadProjection } from "@t3tools/contracts";
2import * as DateTime from "effect/DateTime";
3​
4import type { ThreadRunSummary, ThreadRuntimeSummary } from "./models.ts";
5​
6const ACTIVITY_RUN_STATUSES = new Set(["preparing", "starting", "running", "waiting"]);
7const INTERRUPTIBLE_RUN_STATUSES = new Set(["preparing", "starting", "running"]);
8​
9function latestMatchingRun(
10 projection: OrchestrationV2ThreadProjection,
11 predicate: (run: OrchestrationV2ThreadProjection["runs"][number]) => boolean,
12): OrchestrationV2ThreadProjection["runs"][number] | null {
13 return projection.runs.reduce<OrchestrationV2ThreadProjection["runs"][number] | null>(
14 (latest, candidate) =>
15 predicate(candidate) && (latest === null || candidate.ordinal > latest.ordinal)
16 ? candidate
17 : latest,
18 null,
19 );
21​
22function summarizeThreadRun(
23 projection: OrchestrationV2ThreadProjection,
24 run: OrchestrationV2ThreadProjection["runs"][number],
25): ThreadRunSummary {
26 return {
27 runId: run.id,
28 status: run.status,
29 requestedAt: DateTime.formatIso(run.requestedAt),
30 startedAt: run.startedAt === null ? null : DateTime.formatIso(run.startedAt),
31 completedAt: run.completedAt === null ? null : DateTime.formatIso(run.completedAt),
32 assistantMessageId:
33 projection.messages.findLast(
34 (message) => message.runId === run.id && message.role === "assistant",
35 )?.id ?? null,
36 ...(run.sourcePlanRef === undefined ? {} : { sourcePlanRef: run.sourcePlanRef }),
37 };
39​
40export function deriveLatestThreadRun(
41 projection: OrchestrationV2ThreadProjection,
42): ThreadRunSummary | null {
43 const run = latestMatchingRun(projection, () => true);
44 return run === null ? null : summarizeThreadRun(projection, run);
46​
47/**
48 * Returns the run that owns live provider work, falling back to the newest run
49 * once the thread is idle. A newer queued run must not make an older executing
50 * run look settled in clients that render per-run activity.
51 */
52export function deriveThreadActivityRun(
53 projection: OrchestrationV2ThreadProjection,
54): ThreadRunSummary | null {
55 const run =
56 latestMatchingRun(projection, (candidate) => ACTIVITY_RUN_STATUSES.has(candidate.status)) ??
57 latestMatchingRun(projection, () => true);
58 return run === null ? null : summarizeThreadRun(projection, run);
60​
61export function deriveThreadRuntime(
62 projection: OrchestrationV2ThreadProjection,
63): ThreadRuntimeSummary | null {
64 const latestRun = deriveLatestThreadRun(projection);
65 const activityRun = deriveThreadActivityRun(projection);
66 const providerSession = projection.providerSessions.findLast(
67 (session) => session.providerInstanceId === projection.thread.providerInstanceId,
68 );
69 if (latestRun === null && projection.thread.activeProviderThreadId === null) return null;
70 const activeRunId =
71 latestMatchingRun(projection, (run) => INTERRUPTIBLE_RUN_STATUSES.has(run.status))?.id ?? null;
72 return {
73 // Queueing creates a newer run, but does not replace the provider work
74 // already in flight. Present the executing run's status until it reaches
75 // a terminal state so clients do not flash from "running" to "queued".
76 status: activityRun?.status ?? "idle",
77 activeRunId,
78 providerInstanceId: projection.thread.providerInstanceId,
79 providerName: providerSession?.driver ?? null,
80 lastError: providerSession?.lastError ?? null,
81 updatedAt: DateTime.formatIso(projection.updatedAt),
82 };
84​
85export function threadRuntimeHasInterruptibleRun(
86 runtime: ThreadRuntimeSummary | null | undefined,
87): boolean {
88 return runtime?.activeRunId !== null && runtime?.activeRunId !== undefined;