Open History to see the last commit
1import type {
2 OrchestrationEvent,
3 PendingProviderTurn,
4 PendingProviderTurnSummary,
5} from "@t3tools/contracts";
6
7/** The same pending-state transitions drive durable and connected projections. */
8export function pendingProviderTurnUpdate(
9 event: OrchestrationEvent,
10): PendingProviderTurn | null | undefined {
11 switch (event.type) {
12 case "thread.turn-queued":
13 return event.payload.turn;
14 case "thread.turn-start-requested":
15 return event.payload.providerAvailabilityWait === true ? undefined : null;
16 case "thread.session-set":
17 // Only a session coming alive adopts the queued turn. "stopped" is a
18 // consequence, not an intent: an idle provider exit or a crashed process
19 // reports it, and clearing here would silently discard the saved prompt
20 // (and prune its attachments) while the user still expects it to run.
21 // Explicit stops clear through thread.session-stop-requested instead.
22 return event.payload.session.status === "starting" ||
23 event.payload.session.status === "running"
24 ? null
25 : undefined;
26 case "thread.turn-interrupt-requested":
27 case "thread.session-stop-requested":
28 case "thread.archived":
29 case "thread.deleted":
30 case "thread.settled":
31 case "thread.reverted":
32 return null;
33 default:
34 return undefined;
35 }
36}
37
38/** Shells carry only the wait's marker fields; the full prompt snapshot lives on the detail thread. */
39export function pendingProviderTurnSummary(
40 turn: PendingProviderTurn | null | undefined,
41): PendingProviderTurnSummary | null {
42 return turn == null ? null : { messageId: turn.message.messageId, createdAt: turn.createdAt };
43}