The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { OrchestrationV2Run, OrchestrationV2ThreadProjection } from "@t3tools/contracts"; |
| 2 | |
| 3 | import { cancelledBeforeStart } from "./RestartBackgroundNote.ts"; |
| 4 | |
| 5 | /** A fresh-session schedule run starts a side conversation; the thread's own one continues past it. */ |
| 6 | export function isScheduledFreshRun( |
| 7 | run: Pick<OrchestrationV2Run, "freshContext" | "userMessageId">, |
| 8 | turnItems: OrchestrationV2ThreadProjection["turnItems"], |
| 9 | ): boolean { |
| 10 | // A compacted thread's command read loads these prompts for every fresh-context run. |
| 11 | return ( |
| 12 | run.freshContext === true && |
| 13 | turnItems.some( |
| 14 | (item) => |
| 15 | item.type === "user_message" && |
| 16 | item.messageId === run.userMessageId && |
| 17 | item.scheduledTaskId !== undefined, |
| 18 | ) |
| 19 | ); |
| 20 | } |
| 21 | |
| 22 | /** Ordinal of the latest started fresh-context run. Runs before it never reach a provider again. */ |
| 23 | export function contextBoundaryOrdinal( |
| 24 | projection: Pick<OrchestrationV2ThreadProjection, "runs" | "turnItems">, |
| 25 | ): number { |
| 26 | let boundary = 0; |
| 27 | for (const run of projection.runs) { |
| 28 | if ( |
| 29 | run.freshContext === true && |
| 30 | run.status !== "queued" && |
| 31 | !cancelledBeforeStart(run) && |
| 32 | run.ordinal > boundary && |
| 33 | !isScheduledFreshRun(run, projection.turnItems) |
| 34 | ) { |
| 35 | boundary = run.ordinal; |
| 36 | } |
| 37 | } |
| 38 | return boundary; |
| 39 | } |