Files
next-tjwater-agent/src/sessions/interaction-delivery-reconciliation.ts
T

31 lines
1.6 KiB
TypeScript

import type { InteractionRequest } from "../chat/interaction-request-contract.js";
import type { OpencodeRuntimeAdapter } from "../runtime/opencode.js";
import type { SessionWorkspaceContext } from "../runtime/conversationWorkspace.js";
import { InteractionRequestError, type InteractionRequestStore } from "./interaction-request-store.js";
export async function reconcileInteractionDelivery(
context: SessionWorkspaceContext,
record: InteractionRequest,
store: InteractionRequestStore,
runtime?: Pick<OpencodeRuntimeAdapter, "agentMessageState" | "readInteractionResult">,
running = false
): Promise<InteractionRequest> {
if (running || !runtime || !record.delivery || !["answered", "skipped"].includes(record.status)
|| ["delivered", "cancelled"].includes(record.delivery.status)) return record;
try {
if (await runtime.agentMessageState(context.sessionId, record.delivery.native_message_id) !== "delivered") return record;
// Receipt recovery requires completed output, not merely a consumed input or partial text.
if (!(await runtime.readInteractionResult(context.sessionId, record.delivery.native_message_id)).trim()) return record;
} catch { return record; }
try {
return await store.updateDelivery(context, record.interaction_id, {
expected_revision: record.delivery.revision, status: "delivered", response_id: record.delivery.response_id
});
} catch (error) {
if (error instanceof InteractionRequestError && ["revision_conflict", "delivery_resolved"].includes(error.code)) {
return await store.get(context, record.interaction_id) ?? record;
}
throw error;
}
}