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

orchestrationV2ThreadError.ts

112 lines4.0 KB

1import type {
2 OrchestrationV2ProviderFailure,
3 OrchestrationV2Run,
4 OrchestrationV2TurnItem,
5} from "@t3tools/contracts";
6import * as DateTime from "effect/DateTime";
7​
8export function latestRootProviderFailure(
9 run: OrchestrationV2Run | null,
10 turnItems: ReadonlyArray<OrchestrationV2TurnItem>,
11): OrchestrationV2ProviderFailure | null {
12 if (run?.status !== "failed") return null;
13 let latest: Extract<OrchestrationV2TurnItem, { type: "error" }> | null = null;
14 for (const item of turnItems) {
15 if (
16 item.type !== "error" ||
17 item.status !== "failed" ||
18 item.runId !== run.id ||
19 item.nodeId !== run.rootNodeId
20 )
21 continue;
22 if (
23 latest === null ||
24 DateTime.toEpochMillis(item.updatedAt) > DateTime.toEpochMillis(latest.updatedAt) ||
25 (DateTime.toEpochMillis(item.updatedAt) === DateTime.toEpochMillis(latest.updatedAt) &&
26 (item.ordinal > latest.ordinal || (item.ordinal === latest.ordinal && item.id > latest.id)))
27 ) {
28 latest = item;
29 }
30 }
31 return latest?.failure ?? null;
33​
34export function threadErrorSummary(
35 failure: OrchestrationV2ProviderFailure | null,
36 sessionError: string | null,
37) {
38 const currentFailure =
39 sessionError !== null && sessionError !== failure?.message ? null : failure;
40 return {
41 usageLimitResetAt:
42 currentFailure?.class === "usage_limit" ? (currentFailure.resetAt ?? null) : null,
43 lastError: sessionError ?? failure?.message ?? null,
44 lastErrorClass:
45 sessionError !== null && sessionError !== failure?.message ? null : (failure?.class ?? null),
46 };
48​
49export function latestExecutedRun(
50 runs: ReadonlyArray<OrchestrationV2Run>,
51): OrchestrationV2Run | null {
52 let latest: OrchestrationV2Run | null = null;
53 for (const run of runs) {
54 if (run.status === "queued") continue;
55 if (run.status === "cancelled" && run.startedAt === null) continue;
56 if (latest === null || runRanAfter(run, latest)) latest = run;
57 }
58 return latest;
60​
61/**
62 * Ordinals follow submission, but a run can start ahead of a held queue (a
63 * restart continuation, or a message sent while the queue is held), so a queued
64 * run resumed later can have a lower ordinal than one that already ended. An
65 * unfinished run is the latest.
66 */
67export function runRanAfter(
68 run: Pick<OrchestrationV2Run, "ordinal" | "completedAt">,
69 other: Pick<OrchestrationV2Run, "ordinal" | "completedAt">,
70): boolean {
71 const end = (candidate: typeof run) =>
72 !candidate.completedAt
73 ? Number.POSITIVE_INFINITY
74 : DateTime.toEpochMillis(candidate.completedAt);
75 return end(run) === end(other) ? run.ordinal > other.ordinal : end(run) > end(other);
77​
78export function usageLimitBlockedRun(
79 runs: ReadonlyArray<OrchestrationV2Run>,
80 turnItems: ReadonlyArray<OrchestrationV2TurnItem>,
81 sessionError: string | null,
82): OrchestrationV2Run | null {
83 const executed = latestExecutedRun(runs);
84 if (executed?.status !== "failed") return null;
85 const summary = threadErrorSummary(latestRootProviderFailure(executed, turnItems), sessionError);
86 return summary.lastErrorClass === "usage_limit" ? executed : null;
88​
89/**
90 * That limited run, when newer runs were queued or cancelled before starting.
91 * Callers that treat the highest-ordinal run as the thread outcome would
92 * otherwise hide the limit.
93 */
94export function usageLimitRunPresentedAsLatest(
95 runs: ReadonlyArray<OrchestrationV2Run>,
96 turnItems: ReadonlyArray<OrchestrationV2TurnItem>,
97 sessionError: string | null,
98): OrchestrationV2Run | null {
99 const blocked = usageLimitBlockedRun(runs, turnItems, sessionError);
100 return blocked !== null && runs.some((run) => run.ordinal > blocked.ordinal) ? blocked : null;
102​
103export function latestUnheldRun(
104 runs: ReadonlyArray<OrchestrationV2Run>,
105): OrchestrationV2Run | null {
106 let latest: OrchestrationV2Run | null = null;
107 for (const run of runs) {
108 if (run.status === "queued" && run.queueHeld === true) continue;
109 if (latest === null || run.ordinal > latest.ordinal) latest = run;
110 }
111 return latest;