@paramms/chat-widget 1.6.3 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/outbox.js CHANGED
@@ -183,6 +183,11 @@ class j {
183
183
  visibleActions() {
184
184
  return this.actions.filter((t) => !t.availableInStates || t.availableInStates.includes(this.state));
185
185
  }
186
+ /** Any manifest action by id, regardless of state/surface — used to open the
187
+ * form a `form`-kind message points at (search pre-fill). */
188
+ actionById(t) {
189
+ return this.actions.find((s) => s.id === t);
190
+ }
186
191
  highestSeq() {
187
192
  return this._maxSeq;
188
193
  }
@@ -1 +1 @@
1
- {"version":3,"file":"outbox.js","sources":["../src/protocol/ids.ts","../src/connection.ts","../src/store.ts","../src/outbox.ts"],"sourcesContent":["// Branded identifier types. A plain string can't be passed where a TenantId is\n// expected (and vice-versa), so id-confusion bugs — the kind that caused the\n// `t-1` vs `dev-tenant` mismatch in the previous system — become compile errors.\n\nexport type Brand<T, B extends string> = T & { readonly __brand: B }\n\nexport type TenantId = Brand<string, 'TenantId'>\nexport type UserId = Brand<string, 'UserId'>\nexport type ConversationId = Brand<string, 'ConversationId'>\nexport type SubjectId = Brand<string, 'SubjectId'>\nexport type MessageId = Brand<string, 'MessageId'>\nexport type ActionId = Brand<string, 'ActionId'>\nexport type ProfileId = Brand<string, 'ProfileId'>\nexport type ConnectionId = Brand<string, 'ConnectionId'>\n\nexport const asTenantId = (s: string): TenantId => s as TenantId\nexport const asUserId = (s: string): UserId => s as UserId\nexport const asConversationId = (s: string): ConversationId => s as ConversationId\nexport const asSubjectId = (s: string): SubjectId => s as SubjectId\nexport const asMessageId = (s: string): MessageId => s as MessageId\nexport const asActionId = (s: string): ActionId => s as ActionId\nexport const asProfileId = (s: string): ProfileId => s as ProfileId\nexport const asConnectionId = (s: string): ConnectionId => s as ConnectionId\n\n/** Anonymous guests live under this tenant; they bypass cross-tenant scoping but\n * are still gated by conversation membership. */\nexport const ANONYMOUS_TENANT = asTenantId('anonymous')\n","import {\n encodeFrame, decodeFrame, isClientFrame,\n type ClientFrame, type ServerFrame,\n} from './protocol/index.js'\n\n// Minimal socket surface so tests can inject a fake without a real WebSocket.\nexport interface SocketLike {\n binaryType: string\n send(data: Uint8Array): void\n close(): void\n onopen: (() => void) | null\n onclose: (() => void) | null\n onerror: (() => void) | null\n onmessage: ((ev: { data: ArrayBuffer }) => void) | null\n}\nexport type SocketFactory = (url: string) => SocketLike\n\nexport interface ConnectionOptions {\n url: string\n token: string\n /** Authenticated embeds: called when the server rejects the token\n * (typically an expired signed JWT). Return a freshly minted token to\n * resume seamlessly, or null to give up (shows the fatal error). */\n refreshToken?: () => Promise<string | null>\n open: ClientFrame // frame sent right after auth (e.g. open a conversation, or subscribe_inbox)\n onFrame: (frame: ServerFrame) => void\n getCursor: () => number // highest seq seen (for sync on reconnect)\n onStatusChange?: (status: 'connecting' | 'open' | 'reconnecting' | 'error', message?: string) => void\n socketFactory?: SocketFactory\n backoffBaseMs?: number\n backoffMaxMs?: number\n maxOutbox?: number\n}\n\ntype State = 'idle' | 'connecting' | 'open' | 'closed'\n\nexport class ConnectionManager {\n private socket: SocketLike | null = null\n private state: State = 'idle'\n private authed = false\n private everAuthed = false\n private attempt = 0\n private outbox: ClientFrame[] = []\n private stopped = false\n private timer: ReturnType<typeof setTimeout> | null = null\n\n constructor(private readonly opts: ConnectionOptions) {}\n\n connect(): void {\n if (this.state === 'connecting' || this.state === 'open') return\n this.stopped = false\n this.state = 'connecting'\n this.authed = false\n this.opts.onStatusChange?.(this.attempt > 0 ? 'reconnecting' : 'connecting')\n const make = this.opts.socketFactory ?? defaultFactory\n const sock = make(this.opts.url)\n sock.binaryType = 'arraybuffer'\n this.socket = sock\n\n sock.onopen = () => {\n // Don't reset attempt here — reset only after successful auth ('authed').\n // A connection that opens but fails during auth (bad token, server restart)\n // should still back off, not immediately retry at base delay.\n this.raw({ type: 'auth', token: this.opts.token })\n }\n sock.onmessage = (ev) => {\n const frame = decodeFrame(new Uint8Array(ev.data))\n if (!frame || isClientFrame(frame)) return // ignore non-server frames\n this.handle(frame)\n }\n sock.onclose = () => this.onClosed()\n sock.onerror = () => { try { sock.close() } catch { /* */ } }\n }\n\n /** Queue a frame; sent immediately if open, else flushed on (re)connect.\n * The outbox is bounded so a prolonged outage can't grow memory without limit\n * — oldest queued frames are dropped past the cap. */\n send(frame: ClientFrame): void {\n if (this.state === 'open' && this.authed) { this.raw(frame); return }\n // Evicts the oldest half when full to amortise the O(n) cost of overflow.\n this.queue(frame)\n }\n\n /** How many frames are waiting to go out. Useful for a host that wants to\n * show \"message pending\" state, and for asserting the outbox stays bounded. */\n pendingCount(): number { return this.outbox.length }\n\n close(): void {\n this.stopped = true\n if (this.timer) clearTimeout(this.timer)\n this.state = 'closed'\n try { this.socket?.close() } catch { /* */ }\n }\n\n private handle(frame: ServerFrame): void {\n if (frame.type === 'authed') {\n this.attempt = 0 // reset backoff only after a fully successful auth\n this.state = 'open'\n this.authed = true; this.everAuthed = true\n this.opts.onStatusChange?.('open')\n // Open/resolve the conversation. The catch-up `sync` is sent on 'opened'\n // (below), i.e. only after the server has joined us to the room — sending\n // it here would race the async open and be rejected as \"not joined\".\n this.raw(this.opts.open)\n // The QUEUED frames need exactly the same treatment, and used to not get\n // it: flushing here fired them straight after `open`, before the server\n // had joined us, so the engine answered FORBIDDEN 'Open the conversation\n // first' and the message was gone — no ack, no requeue. This is the\n // reconnect-drops-your-message bug. Frames now wait for 'opened' below.\n //\n // EXCEPT when the open frame doesn't produce a join at all: an inbox\n // subscription (`subscribe_inbox`) never gets an 'opened' reply, so\n // waiting for one would strand the queue forever. Nothing needs joining\n // in that case, so flushing immediately is both safe and required.\n if (this.opts.open.type !== 'open') this.flush()\n }\n // 'opened' confirms we're joined — now catch up from our cursor, then\n // release anything queued while we were disconnected.\n if (frame.type === 'opened') {\n this.raw({ type: 'sync', conversationId: frame.conversation.id, sinceSeq: this.opts.getCursor() })\n this.flush(frame.conversation.id)\n }\n // A CONNECTION-FATAL error (bad or rejected token, closed or missing\n // chatroom) will never succeed on retry — stop the reconnect loop and report\n // a clear reason instead of spinning on \"connecting…\" forever. Per-frame\n // errors (rate limit, one bad message) are NOT fatal and fall through.\n if (frame.type === 'error' && FATAL_ERRORS.has(frame.code)) {\n // Token refresh (authenticated embeds): a signed JWT expiring mid-session\n // used to be a dead end — the widget showed a fatal error until reload.\n // If the host supplied refreshToken, ask it to mint a fresh one and\n // reconnect. One in-flight attempt at a time; a refresh that returns\n // null/throws (user logged out, backend down) falls through to fatal.\n if (frame.code === 'UNAUTHORIZED' && this.opts.refreshToken && !this.refreshing) {\n this.refreshing = true\n this.opts.onStatusChange?.('reconnecting', 'Renewing session…')\n void this.opts.refreshToken()\n .then((fresh) => {\n this.refreshing = false\n if (!fresh) { this.fatal(frame); return }\n this.opts.token = fresh\n try { this.socket?.close() } catch { /* */ }\n // onClosed schedules the reconnect, which re-auths with the new token.\n })\n .catch(() => { this.refreshing = false; this.fatal(frame) })\n return\n }\n this.fatal(frame)\n return\n }\n this.opts.onFrame(frame)\n }\n\n private refreshing = false\n\n private fatal(frame: Extract<ServerFrame, { type: 'error' }>): void {\n this.stopped = true\n try { this.socket?.close() } catch { /* */ }\n this.state = 'closed'\n this.opts.onStatusChange?.('error', friendlyError(frame.code))\n this.opts.onFrame(frame)\n }\n\n /** Release queued frames. When called from 'opened' we know the canonical\n * conversation id the server just resolved us to, and queued `send` frames\n * are retargeted to it. A manager only ever opens ONE conversation (its\n * `opts.open`), so every queued send belongs to that thread by\n * construction — but the id it was queued with can be STALE (queued against\n * the previous session's conversation before a reconnect). Retargeting is a\n * no-op in the normal case and rescues the message in the stale one. */\n private flush(conversationId?: string): void {\n const pending = this.outbox\n this.outbox = []\n for (const f of pending) {\n this.raw(\n conversationId && f.type === 'send' && f.conversationId !== conversationId\n ? { ...f, conversationId: conversationId as typeof f.conversationId }\n : f,\n )\n }\n }\n\n /** Bounded enqueue — the cap lives here so EVERY path that queues respects\n * it (a failed `raw` used to push straight onto the array, bypassing it). */\n private queue(frame: ClientFrame): void {\n const cap = this.opts.maxOutbox ?? 1_000\n if (this.outbox.length >= cap) {\n this.outbox = this.outbox.slice(this.outbox.length - (cap >> 1))\n }\n this.outbox.push(frame)\n }\n\n private raw(frame: ClientFrame): void {\n // `this.socket?.send(...)` silently DROPPED the frame whenever the socket\n // was null (post-disconnect, pre-reconnect): optional chaining short-\n // circuits, so nothing throws and the catch that re-queues never runs.\n // A null socket is exactly when a frame most needs to be kept.\n if (!this.socket) { this.queue(frame); return }\n try { this.socket.send(encodeFrame(frame)) } catch { this.queue(frame) }\n }\n\n private onClosed(): void {\n this.authed = false\n this.socket = null\n if (this.stopped) { this.state = 'closed'; return }\n this.state = 'idle'\n // Exponential backoff with jitter; reconnect re-auths, re-opens, re-syncs.\n const base = this.opts.backoffBaseMs ?? 500\n const max = this.opts.backoffMaxMs ?? 15_000\n const delay = Math.min(max, base * 2 ** this.attempt) * (0.5 + Math.random() * 0.5)\n this.attempt++\n // If we've never once connected after several tries, the relay is likely\n // unreachable (wrong URL, server down, blocked) — say so, but keep retrying.\n if (this.attempt >= 3 && !this.everAuthed) {\n this.opts.onStatusChange?.('reconnecting', \"Can't reach chat — retrying…\")\n }\n this.timer = setTimeout(() => this.connect(), delay)\n }\n}\n\n/** Connection-fatal error codes — retrying can't fix these. ONLY auth-handshake\n * failure qualifies: FORBIDDEN / NOT_FOUND are per-REQUEST errors (a stale\n * reference, one permission check) and must NOT tear down the whole socket. */\nconst FATAL_ERRORS = new Set(['UNAUTHORIZED'])\nfunction friendlyError(code: string): string {\n switch (code) {\n case 'UNAUTHORIZED': return 'Chat unavailable — sign-in/token was rejected'\n default: return 'Chat unavailable'\n }\n}\n\nfunction defaultFactory(url: string): SocketLike {\n return new WebSocket(url) as unknown as SocketLike\n}\n","import type {\n ServerFrame, Message, ManifestAction, MessageContent,\n ConversationId, MessageId, UserId, Subject,\n} from './protocol/index.js'\n\nexport type SendStatus = 'pending' | 'sent' | 'delivered' | 'read'\n\nexport interface RenderMessage extends Message {\n clientMsgId?: string\n status?: SendStatus\n}\n\n/** Pure, DOM-free conversation state. Feed it ServerFrames (and local optimistic\n * sends); read an ordered, de-duplicated view out. Ordering is by `seq`; the\n * same message arriving twice (live + sync on reconnect) is collapsed by id —\n * the structural fix for the old duplicate-bubble bug. */\nexport class ChatStore {\n conversationId?: ConversationId\n state = ''\n version = 0\n hasMoreHistory = false\n lastReadByOthers = 0\n assignedAgentId: UserId | undefined\n accent: string | undefined\n subject: Subject | undefined\n name: string | undefined\n e2e = false\n offline = false\n offlineMessage = ''\n launcherMessage: { title: string; subtitle?: string } | null = null\n preChat: import('./protocol/frames.js').PreChatConfig | null = null\n whiteLabel = false\n readonly typing = new Set<string>()\n readonly online = new Set<string>()\n /** Live sentiment of the guest's latest message (agent-side only). */\n sentiment: 'positive' | 'neutral' | 'frustrated' | undefined\n sentimentScore: number | undefined\n\n private actions: ManifestAction[] = []\n private readonly byId = new Map<string, RenderMessage>()\n private readonly keyByClient = new Map<string, string>()\n private _maxSeq = 0\n private _sorted: RenderMessage[] | null = null\n\n constructor(private readonly me: UserId) {}\n\n messages(): RenderMessage[] {\n if (!this._sorted) {\n this._sorted = [...this.byId.values()].sort((a, b) => {\n const ap = a.status === 'pending', bp = b.status === 'pending'\n if (ap !== bp) return ap ? 1 : -1\n if (ap && bp) return a.ts - b.ts\n return a.seq - b.seq\n })\n }\n return this._sorted\n }\n\n visibleActions(): ManifestAction[] {\n return this.actions.filter(a => !a.availableInStates || a.availableInStates.includes(this.state))\n }\n\n highestSeq(): number { return this._maxSeq }\n\n addOptimistic(clientMsgId: string, content: MessageContent): RenderMessage {\n const msg: RenderMessage = {\n id: clientMsgId as MessageId, conversationId: this.conversationId as ConversationId,\n seq: 0, senderId: this.me, senderRole: 'guest', content, ts: Date.now(),\n clientMsgId, status: 'pending',\n }\n this.byId.set(clientMsgId, msg)\n this.keyByClient.set(clientMsgId, clientMsgId)\n this._sorted = null\n return msg\n }\n\n apply(frame: ServerFrame): void {\n switch (frame.type) {\n case 'opened':\n this.conversationId = frame.conversation.id\n this.state = frame.conversation.state\n if (frame.subject) this.subject = frame.subject\n return\n case 'manifest':\n this.actions = frame.actions\n this.version = frame.version\n if (frame.name) this.name = frame.name\n if (frame.theme?.accent) this.accent = frame.theme.accent\n if (frame.e2e) this.e2e = true\n // Track the manifest EXACTLY: a sticky `offline` (only ever set, never\n // cleared) kept the widget in away-mode for the whole session once a\n // single manifest said so — which used to hide the composer entirely.\n this.offline = frame.offline === true\n this.offlineMessage = frame.offlineMessage ?? ''\n if (frame.launcherMessage) this.launcherMessage = frame.launcherMessage\n if (frame.whiteLabel) this.whiteLabel = true\n if (frame.preChat) this.preChat = frame.preChat\n return\n case 'message':\n this.upsert({ ...frame.message })\n return\n case 'ack': {\n const key = this.keyByClient.get(frame.clientMsgId)\n const msg = key ? this.byId.get(key) : undefined\n if (msg && key) {\n this.byId.delete(key)\n const confirmed: RenderMessage = { ...msg, id: frame.messageId, seq: frame.seq, ts: frame.ts, status: 'sent' }\n this.byId.set(frame.messageId, confirmed)\n this.keyByClient.set(frame.clientMsgId, frame.messageId)\n if (frame.seq > this._maxSeq) this._maxSeq = frame.seq\n }\n this._sorted = null\n return\n }\n case 'delivered':\n this.markOwnStatus(frame.seq, 'delivered')\n return\n case 'read':\n if (frame.by !== this.me) {\n this.lastReadByOthers = Math.max(this.lastReadByOthers, frame.seq)\n this.markOwnStatus(frame.seq, 'read')\n }\n return\n case 'sync':\n for (const m of frame.messages) this.upsert({ ...m })\n return\n case 'history':\n for (const m of frame.messages) this.upsert({ ...m })\n this.hasMoreHistory = frame.hasMore\n return\n case 'typing':\n if (frame.userId !== this.me) {\n if (frame.isTyping) this.typing.add(frame.userId)\n else this.typing.delete(frame.userId)\n }\n return\n case 'reaction': {\n const m = this.byId.get(frame.messageId)\n if (!m) return\n const reactions: Record<string, UserId[]> = { ...(m.reactions ?? {}) }\n const users = (reactions[frame.emoji] ?? []).filter(u => u !== frame.by)\n if (!frame.removed) users.push(frame.by)\n if (users.length) reactions[frame.emoji] = users; else delete reactions[frame.emoji]\n this.byId.set(frame.messageId, { ...m, reactions })\n this._sorted = null\n return\n }\n case 'edited': {\n const m = this.byId.get(frame.messageId)\n if (m) { this.byId.set(frame.messageId, { ...m, content: frame.content, editedAt: frame.editedAt }); this._sorted = null }\n return\n }\n case 'deleted': {\n const m = this.byId.get(frame.messageId)\n if (m) { this.byId.set(frame.messageId, { ...m, deletedAt: frame.ts }); this._sorted = null }\n return\n }\n case 'state':\n this.state = frame.state\n return\n case 'assigned':\n this.assignedAgentId = frame.agentId ?? undefined\n return\n case 'presence':\n if (frame.status === 'online') this.online.add(frame.userId)\n else this.online.delete(frame.userId)\n return\n case 'subjectState':\n case 'invoked':\n case 'authed':\n case 'error':\n case 'pong':\n return\n case 'sentiment':\n this.sentiment = frame.label\n this.sentimentScore = frame.score\n return\n default:\n return\n }\n }\n\n private upsert(msg: RenderMessage): void {\n const existing = this.byId.get(msg.id)\n this.byId.set(msg.id, existing ? { ...existing, ...msg } : msg)\n if (msg.seq > this._maxSeq) this._maxSeq = msg.seq\n this._sorted = null\n }\n\n private markOwnStatus(uptoSeq: number, status: SendStatus): void {\n const targetRank = rank(status)\n let changed = false\n for (const [k, m] of this.byId) {\n if (m.senderId !== this.me || m.seq <= 0 || m.seq > uptoSeq) continue\n if (rank(m.status) >= targetRank) continue // already at or above target — skip\n this.byId.set(k, { ...m, status })\n changed = true\n }\n if (changed) this._sorted = null\n }\n}\nfunction rank(s: SendStatus | undefined): number {\n switch (s) { case 'read': return 3; case 'delivered': return 2; case 'sent': return 1; default: return 0 }\n}\n","import type { MessageContent } from './protocol/index.js'\n\nexport interface OutboxItem {\n clientMsgId: string\n content: MessageContent\n ts: number\n}\n\nconst MAX_ITEMS = 200 // cap so a long outage can't grow storage unboundedly\nconst MAX_AGE_MS = 7 * 86_400_000 // drop anything older than 7 days on load\n\n/** Persists not-yet-acknowledged outgoing messages to localStorage, keyed by\n * guest token, so a page reload during a connectivity drop doesn't silently\n * lose what the user typed (the \"WhatsApp\" guarantee: your message is queued\n * until it's confirmed sent, even across app restarts). */\nexport class PersistentOutbox {\n private readonly key: string\n\n constructor(token: string) {\n this.key = `ocw_outbox_${token}`\n }\n\n /** All pending items, oldest first, with stale (>7d) entries dropped. */\n load(): OutboxItem[] {\n try {\n const raw = localStorage.getItem(this.key)\n if (!raw) return []\n const items = JSON.parse(raw) as OutboxItem[]\n const cutoff = Date.now() - MAX_AGE_MS\n const fresh = items.filter(i => i.ts >= cutoff)\n if (fresh.length !== items.length) this.save(fresh)\n return fresh\n } catch {\n return []\n }\n }\n\n add(item: OutboxItem): void {\n try {\n const items = this.load()\n items.push(item)\n // Evict oldest when full — matches the in-memory ConnectionManager outbox policy.\n this.save(items.length > MAX_ITEMS ? items.slice(items.length - MAX_ITEMS) : items)\n } catch { /* localStorage unavailable (private mode, quota) — best-effort only */ }\n }\n\n /** Remove an item once it's been acknowledged by the server. */\n remove(clientMsgId: string): void {\n try {\n const items = this.load().filter(i => i.clientMsgId !== clientMsgId)\n this.save(items)\n } catch { /* best-effort */ }\n }\n\n private save(items: OutboxItem[]): void {\n try { localStorage.setItem(this.key, JSON.stringify(items)) } catch { /* quota exceeded — drop silently */ }\n }\n}\n"],"names":["asTenantId","s","asUserId","asConversationId","asSubjectId","asMessageId","asActionId","asProfileId","asConnectionId","ANONYMOUS_TENANT","ConnectionManager","opts","__publicField","_b","_a","sock","defaultFactory","ev","frame","decodeFrame","isClientFrame","FATAL_ERRORS","_d","_c","fresh","friendlyError","conversationId","pending","f","cap","encodeFrame","base","max","delay","code","url","ChatStore","me","a","b","ap","bp","clientMsgId","content","msg","key","confirmed","m","reactions","users","u","existing","uptoSeq","status","targetRank","rank","changed","k","MAX_ITEMS","MAX_AGE_MS","PersistentOutbox","token","raw","items","cutoff","i","item"],"mappings":";;;;AAeO,MAAMA,IAAmB,CAACC,MAA8BA,GAClDC,IAAmB,CAACD,MAA8BA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,IAAE,QAAQ;AAAA,IAAuC;AAAA,EAC7G;AACF;"}
1
+ {"version":3,"file":"outbox.js","sources":["../src/protocol/ids.ts","../src/connection.ts","../src/store.ts","../src/outbox.ts"],"sourcesContent":["// Branded identifier types. A plain string can't be passed where a TenantId is\n// expected (and vice-versa), so id-confusion bugs — the kind that caused the\n// `t-1` vs `dev-tenant` mismatch in the previous system — become compile errors.\n\nexport type Brand<T, B extends string> = T & { readonly __brand: B }\n\nexport type TenantId = Brand<string, 'TenantId'>\nexport type UserId = Brand<string, 'UserId'>\nexport type ConversationId = Brand<string, 'ConversationId'>\nexport type SubjectId = Brand<string, 'SubjectId'>\nexport type MessageId = Brand<string, 'MessageId'>\nexport type ActionId = Brand<string, 'ActionId'>\nexport type ProfileId = Brand<string, 'ProfileId'>\nexport type ConnectionId = Brand<string, 'ConnectionId'>\n\nexport const asTenantId = (s: string): TenantId => s as TenantId\nexport const asUserId = (s: string): UserId => s as UserId\nexport const asConversationId = (s: string): ConversationId => s as ConversationId\nexport const asSubjectId = (s: string): SubjectId => s as SubjectId\nexport const asMessageId = (s: string): MessageId => s as MessageId\nexport const asActionId = (s: string): ActionId => s as ActionId\nexport const asProfileId = (s: string): ProfileId => s as ProfileId\nexport const asConnectionId = (s: string): ConnectionId => s as ConnectionId\n\n/** Anonymous guests live under this tenant; they bypass cross-tenant scoping but\n * are still gated by conversation membership. */\nexport const ANONYMOUS_TENANT = asTenantId('anonymous')\n","import {\n encodeFrame, decodeFrame, isClientFrame,\n type ClientFrame, type ServerFrame,\n} from './protocol/index.js'\n\n// Minimal socket surface so tests can inject a fake without a real WebSocket.\nexport interface SocketLike {\n binaryType: string\n send(data: Uint8Array): void\n close(): void\n onopen: (() => void) | null\n onclose: (() => void) | null\n onerror: (() => void) | null\n onmessage: ((ev: { data: ArrayBuffer }) => void) | null\n}\nexport type SocketFactory = (url: string) => SocketLike\n\nexport interface ConnectionOptions {\n url: string\n token: string\n /** Authenticated embeds: called when the server rejects the token\n * (typically an expired signed JWT). Return a freshly minted token to\n * resume seamlessly, or null to give up (shows the fatal error). */\n refreshToken?: () => Promise<string | null>\n open: ClientFrame // frame sent right after auth (e.g. open a conversation, or subscribe_inbox)\n onFrame: (frame: ServerFrame) => void\n getCursor: () => number // highest seq seen (for sync on reconnect)\n onStatusChange?: (status: 'connecting' | 'open' | 'reconnecting' | 'error', message?: string) => void\n socketFactory?: SocketFactory\n backoffBaseMs?: number\n backoffMaxMs?: number\n maxOutbox?: number\n}\n\ntype State = 'idle' | 'connecting' | 'open' | 'closed'\n\nexport class ConnectionManager {\n private socket: SocketLike | null = null\n private state: State = 'idle'\n private authed = false\n private everAuthed = false\n private attempt = 0\n private outbox: ClientFrame[] = []\n private stopped = false\n private timer: ReturnType<typeof setTimeout> | null = null\n\n constructor(private readonly opts: ConnectionOptions) {}\n\n connect(): void {\n if (this.state === 'connecting' || this.state === 'open') return\n this.stopped = false\n this.state = 'connecting'\n this.authed = false\n this.opts.onStatusChange?.(this.attempt > 0 ? 'reconnecting' : 'connecting')\n const make = this.opts.socketFactory ?? defaultFactory\n const sock = make(this.opts.url)\n sock.binaryType = 'arraybuffer'\n this.socket = sock\n\n sock.onopen = () => {\n // Don't reset attempt here — reset only after successful auth ('authed').\n // A connection that opens but fails during auth (bad token, server restart)\n // should still back off, not immediately retry at base delay.\n this.raw({ type: 'auth', token: this.opts.token })\n }\n sock.onmessage = (ev) => {\n const frame = decodeFrame(new Uint8Array(ev.data))\n if (!frame || isClientFrame(frame)) return // ignore non-server frames\n this.handle(frame)\n }\n sock.onclose = () => this.onClosed()\n sock.onerror = () => { try { sock.close() } catch { /* */ } }\n }\n\n /** Queue a frame; sent immediately if open, else flushed on (re)connect.\n * The outbox is bounded so a prolonged outage can't grow memory without limit\n * — oldest queued frames are dropped past the cap. */\n send(frame: ClientFrame): void {\n if (this.state === 'open' && this.authed) { this.raw(frame); return }\n // Evicts the oldest half when full to amortise the O(n) cost of overflow.\n this.queue(frame)\n }\n\n /** How many frames are waiting to go out. Useful for a host that wants to\n * show \"message pending\" state, and for asserting the outbox stays bounded. */\n pendingCount(): number { return this.outbox.length }\n\n close(): void {\n this.stopped = true\n if (this.timer) clearTimeout(this.timer)\n this.state = 'closed'\n try { this.socket?.close() } catch { /* */ }\n }\n\n private handle(frame: ServerFrame): void {\n if (frame.type === 'authed') {\n this.attempt = 0 // reset backoff only after a fully successful auth\n this.state = 'open'\n this.authed = true; this.everAuthed = true\n this.opts.onStatusChange?.('open')\n // Open/resolve the conversation. The catch-up `sync` is sent on 'opened'\n // (below), i.e. only after the server has joined us to the room — sending\n // it here would race the async open and be rejected as \"not joined\".\n this.raw(this.opts.open)\n // The QUEUED frames need exactly the same treatment, and used to not get\n // it: flushing here fired them straight after `open`, before the server\n // had joined us, so the engine answered FORBIDDEN 'Open the conversation\n // first' and the message was gone — no ack, no requeue. This is the\n // reconnect-drops-your-message bug. Frames now wait for 'opened' below.\n //\n // EXCEPT when the open frame doesn't produce a join at all: an inbox\n // subscription (`subscribe_inbox`) never gets an 'opened' reply, so\n // waiting for one would strand the queue forever. Nothing needs joining\n // in that case, so flushing immediately is both safe and required.\n if (this.opts.open.type !== 'open') this.flush()\n }\n // 'opened' confirms we're joined — now catch up from our cursor, then\n // release anything queued while we were disconnected.\n if (frame.type === 'opened') {\n this.raw({ type: 'sync', conversationId: frame.conversation.id, sinceSeq: this.opts.getCursor() })\n this.flush(frame.conversation.id)\n }\n // A CONNECTION-FATAL error (bad or rejected token, closed or missing\n // chatroom) will never succeed on retry — stop the reconnect loop and report\n // a clear reason instead of spinning on \"connecting…\" forever. Per-frame\n // errors (rate limit, one bad message) are NOT fatal and fall through.\n if (frame.type === 'error' && FATAL_ERRORS.has(frame.code)) {\n // Token refresh (authenticated embeds): a signed JWT expiring mid-session\n // used to be a dead end — the widget showed a fatal error until reload.\n // If the host supplied refreshToken, ask it to mint a fresh one and\n // reconnect. One in-flight attempt at a time; a refresh that returns\n // null/throws (user logged out, backend down) falls through to fatal.\n if (frame.code === 'UNAUTHORIZED' && this.opts.refreshToken && !this.refreshing) {\n this.refreshing = true\n this.opts.onStatusChange?.('reconnecting', 'Renewing session…')\n void this.opts.refreshToken()\n .then((fresh) => {\n this.refreshing = false\n if (!fresh) { this.fatal(frame); return }\n this.opts.token = fresh\n try { this.socket?.close() } catch { /* */ }\n // onClosed schedules the reconnect, which re-auths with the new token.\n })\n .catch(() => { this.refreshing = false; this.fatal(frame) })\n return\n }\n this.fatal(frame)\n return\n }\n this.opts.onFrame(frame)\n }\n\n private refreshing = false\n\n private fatal(frame: Extract<ServerFrame, { type: 'error' }>): void {\n this.stopped = true\n try { this.socket?.close() } catch { /* */ }\n this.state = 'closed'\n this.opts.onStatusChange?.('error', friendlyError(frame.code))\n this.opts.onFrame(frame)\n }\n\n /** Release queued frames. When called from 'opened' we know the canonical\n * conversation id the server just resolved us to, and queued `send` frames\n * are retargeted to it. A manager only ever opens ONE conversation (its\n * `opts.open`), so every queued send belongs to that thread by\n * construction — but the id it was queued with can be STALE (queued against\n * the previous session's conversation before a reconnect). Retargeting is a\n * no-op in the normal case and rescues the message in the stale one. */\n private flush(conversationId?: string): void {\n const pending = this.outbox\n this.outbox = []\n for (const f of pending) {\n this.raw(\n conversationId && f.type === 'send' && f.conversationId !== conversationId\n ? { ...f, conversationId: conversationId as typeof f.conversationId }\n : f,\n )\n }\n }\n\n /** Bounded enqueue — the cap lives here so EVERY path that queues respects\n * it (a failed `raw` used to push straight onto the array, bypassing it). */\n private queue(frame: ClientFrame): void {\n const cap = this.opts.maxOutbox ?? 1_000\n if (this.outbox.length >= cap) {\n this.outbox = this.outbox.slice(this.outbox.length - (cap >> 1))\n }\n this.outbox.push(frame)\n }\n\n private raw(frame: ClientFrame): void {\n // `this.socket?.send(...)` silently DROPPED the frame whenever the socket\n // was null (post-disconnect, pre-reconnect): optional chaining short-\n // circuits, so nothing throws and the catch that re-queues never runs.\n // A null socket is exactly when a frame most needs to be kept.\n if (!this.socket) { this.queue(frame); return }\n try { this.socket.send(encodeFrame(frame)) } catch { this.queue(frame) }\n }\n\n private onClosed(): void {\n this.authed = false\n this.socket = null\n if (this.stopped) { this.state = 'closed'; return }\n this.state = 'idle'\n // Exponential backoff with jitter; reconnect re-auths, re-opens, re-syncs.\n const base = this.opts.backoffBaseMs ?? 500\n const max = this.opts.backoffMaxMs ?? 15_000\n const delay = Math.min(max, base * 2 ** this.attempt) * (0.5 + Math.random() * 0.5)\n this.attempt++\n // If we've never once connected after several tries, the relay is likely\n // unreachable (wrong URL, server down, blocked) — say so, but keep retrying.\n if (this.attempt >= 3 && !this.everAuthed) {\n this.opts.onStatusChange?.('reconnecting', \"Can't reach chat — retrying…\")\n }\n this.timer = setTimeout(() => this.connect(), delay)\n }\n}\n\n/** Connection-fatal error codes — retrying can't fix these. ONLY auth-handshake\n * failure qualifies: FORBIDDEN / NOT_FOUND are per-REQUEST errors (a stale\n * reference, one permission check) and must NOT tear down the whole socket. */\nconst FATAL_ERRORS = new Set(['UNAUTHORIZED'])\nfunction friendlyError(code: string): string {\n switch (code) {\n case 'UNAUTHORIZED': return 'Chat unavailable — sign-in/token was rejected'\n default: return 'Chat unavailable'\n }\n}\n\nfunction defaultFactory(url: string): SocketLike {\n return new WebSocket(url) as unknown as SocketLike\n}\n","import type {\n ServerFrame, Message, ManifestAction, MessageContent,\n ConversationId, MessageId, UserId, Subject,\n} from './protocol/index.js'\n\nexport type SendStatus = 'pending' | 'sent' | 'delivered' | 'read'\n\nexport interface RenderMessage extends Message {\n clientMsgId?: string\n status?: SendStatus\n}\n\n/** Pure, DOM-free conversation state. Feed it ServerFrames (and local optimistic\n * sends); read an ordered, de-duplicated view out. Ordering is by `seq`; the\n * same message arriving twice (live + sync on reconnect) is collapsed by id —\n * the structural fix for the old duplicate-bubble bug. */\nexport class ChatStore {\n conversationId?: ConversationId\n state = ''\n version = 0\n hasMoreHistory = false\n lastReadByOthers = 0\n assignedAgentId: UserId | undefined\n accent: string | undefined\n subject: Subject | undefined\n name: string | undefined\n e2e = false\n offline = false\n offlineMessage = ''\n launcherMessage: { title: string; subtitle?: string } | null = null\n preChat: import('./protocol/frames.js').PreChatConfig | null = null\n whiteLabel = false\n readonly typing = new Set<string>()\n readonly online = new Set<string>()\n /** Live sentiment of the guest's latest message (agent-side only). */\n sentiment: 'positive' | 'neutral' | 'frustrated' | undefined\n sentimentScore: number | undefined\n\n private actions: ManifestAction[] = []\n private readonly byId = new Map<string, RenderMessage>()\n private readonly keyByClient = new Map<string, string>()\n private _maxSeq = 0\n private _sorted: RenderMessage[] | null = null\n\n constructor(private readonly me: UserId) {}\n\n messages(): RenderMessage[] {\n if (!this._sorted) {\n this._sorted = [...this.byId.values()].sort((a, b) => {\n const ap = a.status === 'pending', bp = b.status === 'pending'\n if (ap !== bp) return ap ? 1 : -1\n if (ap && bp) return a.ts - b.ts\n return a.seq - b.seq\n })\n }\n return this._sorted\n }\n\n visibleActions(): ManifestAction[] {\n return this.actions.filter(a => !a.availableInStates || a.availableInStates.includes(this.state))\n }\n\n /** Any manifest action by id, regardless of state/surface — used to open the\n * form a `form`-kind message points at (search pre-fill). */\n actionById(id: string): ManifestAction | undefined {\n return this.actions.find(a => a.id === id)\n }\n\n highestSeq(): number { return this._maxSeq }\n\n addOptimistic(clientMsgId: string, content: MessageContent): RenderMessage {\n const msg: RenderMessage = {\n id: clientMsgId as MessageId, conversationId: this.conversationId as ConversationId,\n seq: 0, senderId: this.me, senderRole: 'guest', content, ts: Date.now(),\n clientMsgId, status: 'pending',\n }\n this.byId.set(clientMsgId, msg)\n this.keyByClient.set(clientMsgId, clientMsgId)\n this._sorted = null\n return msg\n }\n\n apply(frame: ServerFrame): void {\n switch (frame.type) {\n case 'opened':\n this.conversationId = frame.conversation.id\n this.state = frame.conversation.state\n if (frame.subject) this.subject = frame.subject\n return\n case 'manifest':\n this.actions = frame.actions\n this.version = frame.version\n if (frame.name) this.name = frame.name\n if (frame.theme?.accent) this.accent = frame.theme.accent\n if (frame.e2e) this.e2e = true\n // Track the manifest EXACTLY: a sticky `offline` (only ever set, never\n // cleared) kept the widget in away-mode for the whole session once a\n // single manifest said so — which used to hide the composer entirely.\n this.offline = frame.offline === true\n this.offlineMessage = frame.offlineMessage ?? ''\n if (frame.launcherMessage) this.launcherMessage = frame.launcherMessage\n if (frame.whiteLabel) this.whiteLabel = true\n if (frame.preChat) this.preChat = frame.preChat\n return\n case 'message':\n this.upsert({ ...frame.message })\n return\n case 'ack': {\n const key = this.keyByClient.get(frame.clientMsgId)\n const msg = key ? this.byId.get(key) : undefined\n if (msg && key) {\n this.byId.delete(key)\n const confirmed: RenderMessage = { ...msg, id: frame.messageId, seq: frame.seq, ts: frame.ts, status: 'sent' }\n this.byId.set(frame.messageId, confirmed)\n this.keyByClient.set(frame.clientMsgId, frame.messageId)\n if (frame.seq > this._maxSeq) this._maxSeq = frame.seq\n }\n this._sorted = null\n return\n }\n case 'delivered':\n this.markOwnStatus(frame.seq, 'delivered')\n return\n case 'read':\n if (frame.by !== this.me) {\n this.lastReadByOthers = Math.max(this.lastReadByOthers, frame.seq)\n this.markOwnStatus(frame.seq, 'read')\n }\n return\n case 'sync':\n for (const m of frame.messages) this.upsert({ ...m })\n return\n case 'history':\n for (const m of frame.messages) this.upsert({ ...m })\n this.hasMoreHistory = frame.hasMore\n return\n case 'typing':\n if (frame.userId !== this.me) {\n if (frame.isTyping) this.typing.add(frame.userId)\n else this.typing.delete(frame.userId)\n }\n return\n case 'reaction': {\n const m = this.byId.get(frame.messageId)\n if (!m) return\n const reactions: Record<string, UserId[]> = { ...(m.reactions ?? {}) }\n const users = (reactions[frame.emoji] ?? []).filter(u => u !== frame.by)\n if (!frame.removed) users.push(frame.by)\n if (users.length) reactions[frame.emoji] = users; else delete reactions[frame.emoji]\n this.byId.set(frame.messageId, { ...m, reactions })\n this._sorted = null\n return\n }\n case 'edited': {\n const m = this.byId.get(frame.messageId)\n if (m) { this.byId.set(frame.messageId, { ...m, content: frame.content, editedAt: frame.editedAt }); this._sorted = null }\n return\n }\n case 'deleted': {\n const m = this.byId.get(frame.messageId)\n if (m) { this.byId.set(frame.messageId, { ...m, deletedAt: frame.ts }); this._sorted = null }\n return\n }\n case 'state':\n this.state = frame.state\n return\n case 'assigned':\n this.assignedAgentId = frame.agentId ?? undefined\n return\n case 'presence':\n if (frame.status === 'online') this.online.add(frame.userId)\n else this.online.delete(frame.userId)\n return\n case 'subjectState':\n case 'invoked':\n case 'authed':\n case 'error':\n case 'pong':\n return\n case 'sentiment':\n this.sentiment = frame.label\n this.sentimentScore = frame.score\n return\n default:\n return\n }\n }\n\n private upsert(msg: RenderMessage): void {\n const existing = this.byId.get(msg.id)\n this.byId.set(msg.id, existing ? { ...existing, ...msg } : msg)\n if (msg.seq > this._maxSeq) this._maxSeq = msg.seq\n this._sorted = null\n }\n\n private markOwnStatus(uptoSeq: number, status: SendStatus): void {\n const targetRank = rank(status)\n let changed = false\n for (const [k, m] of this.byId) {\n if (m.senderId !== this.me || m.seq <= 0 || m.seq > uptoSeq) continue\n if (rank(m.status) >= targetRank) continue // already at or above target — skip\n this.byId.set(k, { ...m, status })\n changed = true\n }\n if (changed) this._sorted = null\n }\n}\nfunction rank(s: SendStatus | undefined): number {\n switch (s) { case 'read': return 3; case 'delivered': return 2; case 'sent': return 1; default: return 0 }\n}\n","import type { MessageContent } from './protocol/index.js'\n\nexport interface OutboxItem {\n clientMsgId: string\n content: MessageContent\n ts: number\n}\n\nconst MAX_ITEMS = 200 // cap so a long outage can't grow storage unboundedly\nconst MAX_AGE_MS = 7 * 86_400_000 // drop anything older than 7 days on load\n\n/** Persists not-yet-acknowledged outgoing messages to localStorage, keyed by\n * guest token, so a page reload during a connectivity drop doesn't silently\n * lose what the user typed (the \"WhatsApp\" guarantee: your message is queued\n * until it's confirmed sent, even across app restarts). */\nexport class PersistentOutbox {\n private readonly key: string\n\n constructor(token: string) {\n this.key = `ocw_outbox_${token}`\n }\n\n /** All pending items, oldest first, with stale (>7d) entries dropped. */\n load(): OutboxItem[] {\n try {\n const raw = localStorage.getItem(this.key)\n if (!raw) return []\n const items = JSON.parse(raw) as OutboxItem[]\n const cutoff = Date.now() - MAX_AGE_MS\n const fresh = items.filter(i => i.ts >= cutoff)\n if (fresh.length !== items.length) this.save(fresh)\n return fresh\n } catch {\n return []\n }\n }\n\n add(item: OutboxItem): void {\n try {\n const items = this.load()\n items.push(item)\n // Evict oldest when full — matches the in-memory ConnectionManager outbox policy.\n this.save(items.length > MAX_ITEMS ? items.slice(items.length - MAX_ITEMS) : items)\n } catch { /* localStorage unavailable (private mode, quota) — best-effort only */ }\n }\n\n /** Remove an item once it's been acknowledged by the server. */\n remove(clientMsgId: string): void {\n try {\n const items = this.load().filter(i => i.clientMsgId !== clientMsgId)\n this.save(items)\n } catch { /* best-effort */ }\n }\n\n private save(items: OutboxItem[]): void {\n try { localStorage.setItem(this.key, JSON.stringify(items)) } catch { /* quota exceeded — drop silently */ }\n }\n}\n"],"names":["asTenantId","s","asUserId","asConversationId","asSubjectId","asMessageId","asActionId","asProfileId","asConnectionId","ANONYMOUS_TENANT","ConnectionManager","opts","__publicField","_b","_a","sock","defaultFactory","ev","frame","decodeFrame","isClientFrame","FATAL_ERRORS","_d","_c","fresh","friendlyError","conversationId","pending","f","cap","encodeFrame","base","max","delay","code","url","ChatStore","me","a","b","ap","bp","id","clientMsgId","content","msg","key","confirmed","m","reactions","users","u","existing","uptoSeq","status","targetRank","rank","changed","k","MAX_ITEMS","MAX_AGE_MS","PersistentOutbox","token","raw","items","cutoff","i","item"],"mappings":";;;;AAeO,MAAMA,IAAmB,CAACC,MAA8BA,GAClDC,IAAmB,CAACD,MAA8BA,GAClDE,IAAmB,CAACF,MAA8BA,GAClDG,IAAmB,CAACH,MAA8BA,GAClDI,IAAmB,CAACJ,MAA8BA,GAClDK,IAAmB,CAACL,MAA8BA,GAClDM,IAAmB,CAACN,MAA8BA,GAClDO,IAAmB,CAACP,MAA8BA,GAIlDQ,IAAmBT,EAAW,WAAW;ACU/C,MAAMU,EAAkB;AAAA,EAU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: ActionId;
37
+ prefill?: Record<string, string>;
37
38
  } | {
38
39
  kind: 'system';
39
40
  event: string;
package/dist/react.d.ts CHANGED
@@ -47,6 +47,8 @@ interface BaseProps {
47
47
  quickReplies?: string[];
48
48
  /** Container height when rendered inline. Default: '100%' */
49
49
  height?: string;
50
+ /** UI language ('ko', 'ko-KR', …). Omit to auto-detect from the browser. */
51
+ locale?: MountOptions['locale'];
50
52
  /** i18n string overrides for non-English sites */
51
53
  i18n?: MountOptions['i18n'];
52
54
  /** Per-tenant feature switches (default all ON). Set a flag false to disable
@@ -101,7 +103,7 @@ export interface ChatWidgetProps extends BaseProps {
101
103
  * (inline) or `<ChatAppLauncher />` (floating bubble) — this component renders
102
104
  * a single conversation.
103
105
  */
104
- export declare function ChatWidget({ url, apiUrl, profileId, token, refreshToken, userId, userName, userEmail, userAvatar, contextTitle, contextSubtitle, contextStatus, subjectId, accent, accent2, theme, webfont, launcher, position, launcherMessage, quickReplies, height, i18n, features, translateLang, inbox, inboxScope, }: ChatWidgetProps): JSX.Element;
106
+ export declare function ChatWidget({ url, apiUrl, profileId, token, refreshToken, userId, userName, userEmail, userAvatar, contextTitle, contextSubtitle, contextStatus, subjectId, accent, accent2, theme, webfont, launcher, position, launcherMessage, quickReplies, height, i18n, features, translateLang, inbox, inboxScope, locale, }: ChatWidgetProps): JSX.Element;
105
107
  export interface MarketplaceChatProps extends BaseProps {
106
108
  /** Unique ID for this listing — each listing gets its own thread.
107
109
  * Omit for a general (non-item-specific) conversation. */
@@ -151,7 +153,7 @@ export interface MarketplaceChatProps extends BaseProps {
151
153
  *
152
154
  * Only `url` and `profileId` are required. All other props are optional.
153
155
  */
154
- export declare function MarketplaceChat({ url, apiUrl, profileId, token, refreshToken, listingId, listingTitle, listingMeta, listingPrice, listingStatus, userId, userName, userEmail, userAvatar, accent, accent2, theme, webfont, launcher, position, launcherMessage, quickReplies, height, i18n, features, translateLang, inbox, inboxScope, }: MarketplaceChatProps): JSX.Element;
156
+ export declare function MarketplaceChat({ url, apiUrl, profileId, token, refreshToken, listingId, listingTitle, listingMeta, listingPrice, listingStatus, userId, userName, userEmail, userAvatar, accent, accent2, theme, webfont, launcher, position, launcherMessage, quickReplies, height, i18n, features, translateLang, inbox, inboxScope, locale, }: MarketplaceChatProps): JSX.Element;
155
157
  export interface ChatAppProps {
156
158
  /** Relay URL — ONE url, any scheme; `https://api.relay.paramms.com` is the
157
159
  * recommended form. The WebSocket URL and REST base are derived from it —
@@ -208,6 +210,8 @@ export interface ChatAppProps {
208
210
  * fullscreen panel on a phone (no Escape key, no reachable backdrop) traps
209
211
  * the user. Omit for a bare inline embed the host chromes itself. */
210
212
  onClose?: () => void;
213
+ /** UI language ('ko', 'ko-KR', …). Omit to auto-detect from the browser. */
214
+ locale?: string;
211
215
  /** i18n overrides */
212
216
  i18n?: import('./chatlist.js').ChatListOptions['i18n'];
213
217
  }
@@ -252,7 +256,7 @@ export interface ChatAppProps {
252
256
  * }
253
257
  * ```
254
258
  */
255
- export declare function ChatApp({ url, apiUrl, profileId, tenantId, token, refreshToken, userId, userName, userEmail, accent, theme, webfont, height, i18n, onClose, scope, }: ChatAppProps): JSX.Element;
259
+ export declare function ChatApp({ url, apiUrl, profileId, tenantId, token, refreshToken, userId, userName, userEmail, accent, theme, webfont, height, i18n, onClose, locale, scope, }: ChatAppProps): JSX.Element;
256
260
  export interface ChatAppLauncherProps {
257
261
  /** Relay URL — ONE url, any scheme; `https://api.relay.paramms.com` is the
258
262
  * recommended form. The WebSocket URL and REST base are derived from it. */