Open History to see the last commit
1import type { OrchestrationV2Run, OrchestrationV2ThreadProjection } from "@t3tools/contracts";
2
3export function unsentManualContinuationSource(
4 run: OrchestrationV2Run | undefined,
5 projection: Pick<OrchestrationV2ThreadProjection, "runs" | "attempts" | "providerTurns">,
6) {
7 let sourceId = run?.manualContinuationOfRunId;
8 if (sourceId === undefined) return undefined;
9 const runs = new Map(projection.runs.map((source) => [source.id, source]));
10 const acceptedAttempts = new Set(projection.providerTurns.map((turn) => turn.runAttemptId));
11 const acceptedRuns = new Set(
12 projection.attempts
13 .filter((attempt) => acceptedAttempts.has(attempt.id))
14 .map((attempt) => attempt.runId),
15 );
16 const visited = new Set<OrchestrationV2Run["id"]>();
17 while (sourceId !== undefined && !visited.has(sourceId)) {
18 visited.add(sourceId);
19 const source = runs.get(sourceId);
20 if (source === undefined || acceptedRuns.has(sourceId)) return undefined;
21 if (source.manualContinuationOfRunId === undefined) return source;
22 sourceId = source.manualContinuationOfRunId;
23 }
24 return undefined;
25}