/** * Session-scoped outbound message queue for reliable widget delivery. */ import type { ChatMessage } from "../../../utils/globals"; /** Requeue `sending` rows stuck longer than this (crash / abort mid-flight). */ export declare const STALE_SENDING_MS = 30000; /** Reuse a recently created channel for `newChannel` drain instead of POSTing again. */ export declare const LAST_CREATED_CHANNEL_TTL_MS: number; export type OutboxEntryState = "pending" | "sending" | "failed"; export interface OutboxEntry { messageId: string; channelId: string; distinctId: string; body: string; content_type: ChatMessage["content_type"]; metadata: Record; created_at: string; state: OutboxEntryState; attempts: number; /** Epoch ms — do not drain before this instant (exponential backoff). */ nextRetryAt: number; /** Epoch ms — when the row entered `sending` (stale recovery). */ sendingSince?: number; /** When set, drain creates a channel before POSTing the message. */ newChannel?: { skipGreeting: boolean; }; } export interface LastCreatedChannelHint { channelId: string; at: number; } export declare function outboxStorageKey(projectToken: string, distinctId: string): string; export declare function lastCreatedChannelStorageKey(storageKey: string): string; export declare function readLastCreatedChannelHint(storageKey: string, now?: number): LastCreatedChannelHint | null; export declare function writeLastCreatedChannelHint(storageKey: string, channelId: string): void; export declare class MessageOutbox { private readonly _storageKey; /** Fallback when sessionStorage is unavailable or writes fail. */ private _memory; constructor(_storageKey: string); list(): OutboxEntry[]; get(messageId: string): OutboxEntry | undefined; upsert(entry: OutboxEntry): void; patch(messageId: string, partial: Partial): void; remove(messageId: string): void; /** Entries eligible for drain (not in backoff, not actively sending). */ dueEntries(now?: number): OutboxEntry[]; /** * Recover rows left in `sending` after a crash, tab kill, or aborted fetch. */ requeueStaleSending(maxAgeMs?: number, now?: number): void; private _readStorage; private _write; } /** Backoff: 1s, 2s, 4s, … capped at 60s. */ export declare function nextOutboxRetryAt(attempts: number, now?: number): number;