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

NotificationMailbox.ts

23 lines1.0 KB

1import type { MessageId, OrchestrationV2ThreadProjection } from "@t3tools/contracts";
2​
3/**
4 * A persisted steer without an acceptance receipt can be retried as a continuation.
5 * Delivery is at least once: provider acceptance and our receipt cannot commit
6 * atomically. Reusing the message ID keeps recovery from duplicating timeline items.
7 */
8export function isUndeliveredMailboxSteer(
9 projection: Pick<OrchestrationV2ThreadProjection, "messages" | "runs" | "providerTurns">,
10 messageId: MessageId,
11): boolean {
12 const message = projection.messages.find((candidate) => candidate.id === messageId);
13 if (message?.delegatedCompletion === undefined) return false;
14 const run = projection.runs.find((candidate) => candidate.id === message.runId);
15 return (
16 run !== undefined &&
17 run.userMessageId !== message.id &&
18 (["completed", "failed", "interrupted", "cancelled", "rolled_back"].includes(run.status) ||
19 projection.providerTurns.some(
20 (turn) => turn.runAttemptId === run.activeAttemptId && turn.status === "completed",
21 ))
22 );