Files
t3code/apps/server/src/orchestration-v2/NotificationMailbox.ts
T

27 lines
1.1 KiB
TypeScript

import {
latestProviderTurnForAttempt,
type MessageId,
type OrchestrationV2ThreadProjection,
} from "@t3tools/contracts";
/**
* A persisted steer without an acceptance receipt can be retried as a continuation.
* Delivery is at least once: provider acceptance and our receipt cannot commit
* atomically. Reusing the message ID keeps recovery from duplicating timeline items.
*/
export function isUndeliveredMailboxSteer(
projection: Pick<OrchestrationV2ThreadProjection, "messages" | "runs" | "providerTurns">,
messageId: MessageId,
): boolean {
const message = projection.messages.find((candidate) => candidate.id === messageId);
if (message?.delegatedCompletion === undefined) return false;
const run = projection.runs.find((candidate) => candidate.id === message.runId);
return (
run !== undefined &&
run.userMessageId !== message.id &&
(["completed", "failed", "interrupted", "cancelled", "rolled_back"].includes(run.status) ||
latestProviderTurnForAttempt(projection.providerTurns, run.activeAttemptId)?.status ===
"completed")
);
}