The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | import type { OrchestrationV2Run, OrchestrationV2ThreadProjection } from "@t3tools/contracts"; |
| 2 | |
| 3 | export function unsentManualContinuationSource( |
| 4 | run: Pick<OrchestrationV2Run, "manualContinuationOfRunId"> | 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 latestAttempt = ( |
| 12 | runId: OrchestrationV2Run["id"], |
| 13 | matches: (attempt: (typeof projection.attempts)[number]) => boolean, |
| 14 | ) => |
| 15 | Math.max( |
| 16 | 0, |
| 17 | ...projection.attempts |
| 18 | .filter((attempt) => attempt.runId === runId && matches(attempt)) |
| 19 | .map((attempt) => attempt.attemptOrdinal), |
| 20 | ); |
| 21 | const visited = new Set<OrchestrationV2Run["id"]>(); |
| 22 | while (sourceId !== undefined && !visited.has(sourceId)) { |
| 23 | visited.add(sourceId); |
| 24 | const source = runs.get(sourceId); |
| 25 | if (source === undefined) return undefined; |
| 26 | const accepted = latestAttempt(sourceId, (attempt) => acceptedAttempts.has(attempt.id)); |
| 27 | // An accepted attempt delivered everything before it; only a later steering restart's message is unsent. |
| 28 | const restarted = latestAttempt(sourceId, (attempt) => attempt.reason === "steering_restart"); |
| 29 | if (restarted > accepted) return source; |
| 30 | if (accepted > 0) return undefined; |
| 31 | if (source.manualContinuationOfRunId === undefined) return source; |
| 32 | sourceId = source.manualContinuationOfRunId; |
| 33 | } |
| 34 | return undefined; |
| 35 | } |