/** * Owns durable message-interaction sessions and their exactly-once execution * protocol. The authority binds callbacks to trusted context, while storage * implementations provide atomic claim/complete/revoke transitions. */ import type { InteractionBlock, InteractionField, InteractionKind } from "../types/interactions.js"; import type { ConnectorInteractionCapabilityProfile, InteractionNegotiationContext } from "./interaction-profiles.js"; export declare const MESSAGE_INTERACTION_SESSION_VERSION: 1; export declare const MESSAGE_INTERACTION_CALLBACK_PREFIX = "is1:"; export declare const MESSAGE_INTERACTION_CALLBACK_BYTES = 36; export type MessageInteractionPurpose = "choice" | "form" | "approval" | "setup" | "auth" | "task" | "file" | "followup"; export type MessageInteractionFlow = "native" | "conversational" | "signed-hosted" | "sensitive-request"; export interface MessageInteractionBindings { actorId: string; audience: { kind: string; id: string; }; agentId: string; connector: { source: string; accountId: string; }; roomId: string; sourceMessageId: string; } export type MessageInteractionResponseScalar = string | number | boolean; export interface MessageInteractionAttachmentResponse { mediaUrl: string; mimeType: string; bytes: number; } export type MessageInteractionResponseValue = MessageInteractionResponseScalar | MessageInteractionAttachmentResponse; export type MessageInteractionResponse = Record; export interface MessageInteractionResponseField { name: string; type: InteractionField["type"] | "acknowledgement"; required: boolean; options?: readonly string[]; maxBytes?: number; mimeTypes?: readonly string[]; } export interface MessageInteractionResponseSchema { fields: readonly MessageInteractionResponseField[]; additionalFields: false; } export interface MessageInteractionAuthorizationDecision { decisionId: string; policyRevision: string; decidedAt: string; state: "active" | "revoked"; revokedAt: string | null; } export interface MessageInteractionEffect { kind: string; /** Non-secret operation metadata. Credentials and submitted secrets are forbidden. */ metadata?: Readonly>; } export type MessageInteractionConsumeState = { state: "pending"; } | { state: "claimed"; claimId: string; replayKey: string; responseDigest: string; response: MessageInteractionResponse; claimedAt: string; claimExpiresAt: string; attempt: number; } | { state: "committed"; claimId: string; replayKey: string; responseDigest: string; response: MessageInteractionResponse; claimedAt: string; committedAt: string; attempt: number; } | { state: "completed"; claimId: string; replayKey: string; responseDigest: string; response: MessageInteractionResponse; claimedAt: string; committedAt: string; completedAt: string; attempt: number; receipt: MessageInteractionReceipt; }; export interface MessageInteractionSession { sessionVersion: typeof MESSAGE_INTERACTION_SESSION_VERSION; reference: string; purpose: MessageInteractionPurpose; blockKind: InteractionKind; flow: MessageInteractionFlow; profileId: string; bindings: MessageInteractionBindings; responseSchema: MessageInteractionResponseSchema; presetResponse: MessageInteractionResponse | null; authorization: MessageInteractionAuthorizationDecision; effect: MessageInteractionEffect; createdAt: string; expiresAt: string; consume: MessageInteractionConsumeState; revision: number; } export interface MessageInteractionReceipt { receiptId: string; idempotencyKey: string; status: "completed"; completedAt: string; result: Readonly>; } export interface MessageInteractionClaimContext extends MessageInteractionBindings { reference: string; replayKey: string; response?: MessageInteractionResponse; claimId: string; now: number; claimTtlMs: number; } export type MessageInteractionClaimResult = { status: "acquired" | "resumed"; session: MessageInteractionSession; } | { status: "in_progress"; session: MessageInteractionSession; } | { status: "replay"; session: MessageInteractionSession; receipt: MessageInteractionReceipt; }; export interface MessageInteractionCompleteContext { reference: string; claimId: string; replayKey: string; receipt: MessageInteractionReceipt; now: number; } export interface MessageInteractionCommitContext { reference: string; claimId: string; replayKey: string; now: number; } export interface MessageInteractionReconcileContext { reference: string; replayKey: string; receipt: MessageInteractionReceipt; now: number; } /** * Storage transaction boundary. Implementations must make every method atomic; * a distributed implementation maps these operations to one row transaction. */ export interface MessageInteractionSessionStore { create(session: MessageInteractionSession): Promise; get(reference: string): Promise; claimIfCurrent(context: MessageInteractionClaimContext): Promise; commitIfClaimed(context: MessageInteractionCommitContext): Promise; completeIfClaimed(context: MessageInteractionCompleteContext): Promise; /** Discover committed outcomes that require provider/outbox reconciliation. */ listCommitted(args: { committedBefore: number; limit: number; }): Promise; /** Retain a verified external outcome without re-executing its effect. */ reconcileCommitted(context: MessageInteractionReconcileContext): Promise; revokeAuthorization(args: { reference: string; decisionId: string; now: number; }): Promise; deleteExpired(before: number): Promise; } export interface MessageInteractionEffectExecutor { /** * Executes only after the host durably commits the replay key. A crash after * commit is permanently ambiguous and is never automatically re-executed. */ execute(args: { idempotencyKey: string; effect: MessageInteractionEffect; response: MessageInteractionResponse; session: MessageInteractionSession; }): Promise; } interface MessageInteractionConsumeArgs { callbackData: string; bindings: MessageInteractionBindings; replayKey: string; response?: MessageInteractionResponse; executor: MessageInteractionEffectExecutor; } export type MessageInteractionAuthorityConsumeOutcome = { status: "completed" | "replay"; receipt: MessageInteractionReceipt; } | { status: "in_progress"; }; /** Create a short opaque callback reference with no embedded authority or data. */ export declare function createOpaqueMessageInteractionReference(random?: (size: number) => Uint8Array): string; export declare function encodeMessageInteractionCallback(reference: string): string; export declare function decodeMessageInteractionCallback(value: unknown): string | null; /** Derive the exact accepted response shape from the canonical block. */ export declare function responseSchemaForInteraction(block: InteractionBlock, options?: { maxTextResponseBytes?: number; }): MessageInteractionResponseSchema; export declare function validateMessageInteractionResponse(value: MessageInteractionResponse, schema: MessageInteractionResponseSchema): MessageInteractionResponse; /** Pure atomic-transition helper shared by memory, file, and database stores. */ export declare function applyMessageInteractionClaim(sessionValue: MessageInteractionSession, context: MessageInteractionClaimContext): MessageInteractionClaimResult; export declare function applyMessageInteractionCompletion(sessionValue: MessageInteractionSession, context: MessageInteractionCompleteContext): MessageInteractionSession; export declare function applyMessageInteractionReconciliation(sessionValue: MessageInteractionSession, context: MessageInteractionReconcileContext): MessageInteractionSession; /** Durably linearize one effect before crossing the external side-effect boundary. */ export declare function applyMessageInteractionCommit(sessionValue: MessageInteractionSession, context: MessageInteractionCommitContext): MessageInteractionSession; export declare function applyMessageInteractionRevocation(sessionValue: MessageInteractionSession, decisionId: string, now: number): MessageInteractionSession; /** Process-local reference store used by deterministic tests and embedded hosts. */ export declare class InMemoryMessageInteractionSessionStore implements MessageInteractionSessionStore { private readonly sessions; private queue; private atomic; create(session: MessageInteractionSession): Promise; get(reference: string): Promise; /** Apply one pure transition to the stored session and retain its result. */ private update; claimIfCurrent(context: MessageInteractionClaimContext): Promise; completeIfClaimed(context: MessageInteractionCompleteContext): Promise; listCommitted(args: { committedBefore: number; limit: number; }): Promise; reconcileCommitted(context: MessageInteractionReconcileContext): Promise; commitIfClaimed(context: MessageInteractionCommitContext): Promise; revokeAuthorization(args: { reference: string; decisionId: string; now: number; }): Promise; deleteExpired(before: number): Promise; } export declare class MessageInteractionSessionAuthority { private readonly store; private readonly options; constructor(store: MessageInteractionSessionStore, options?: { clock?: () => number; referenceFactory?: () => string; claimTtlMs?: number; }); private now; create(args: { block: InteractionBlock; profile: ConnectorInteractionCapabilityProfile; bindings: MessageInteractionBindings; purpose: MessageInteractionPurpose; flow: MessageInteractionFlow; negotiationContext?: InteractionNegotiationContext; presetResponse?: MessageInteractionResponse; authorization: Omit; effect: MessageInteractionEffect; expiresAt: string; }): Promise<{ session: MessageInteractionSession; callbackData: string; }>; /** Return the atomic claim disposition together with any retained receipt. */ consumeWithOutcome(args: MessageInteractionConsumeArgs): Promise; /** Records a receipt without classifying replay. */ consume(args: MessageInteractionConsumeArgs): Promise; } export {};