@vocantly/widget 0.1.10
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/README.md +147 -0
- package/dist/index.cjs +85 -0
- package/dist/index.d.ts +73 -0
- package/dist/index.mjs +85 -0
- package/dist/vocantly-widget-call.js +325 -0
- package/dist/vocantly-widget.js +85 -0
- package/package.json +59 -0
package/README.md
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# Vocantly Widget
|
|
2
|
+
|
|
3
|
+
A **drop-in embeddable support chat widget** built on the headless `@vocantly/sdk`. One
|
|
4
|
+
`<script>` renders a floating chat bubble + panel; your visitors chat with the AI bot (and human
|
|
5
|
+
agents) with zero UI work on your side.
|
|
6
|
+
|
|
7
|
+
`dist/vocantly-widget.js` is a single self-contained file (~63 KB) — the SDK, its deps, and the
|
|
8
|
+
UI are all bundled in. No import maps, no build step for the host.
|
|
9
|
+
|
|
10
|
+
## The security model (important)
|
|
11
|
+
|
|
12
|
+
The widget runs in the browser, so it only ever holds an **SDK token** (scoped to one app + one
|
|
13
|
+
visitor, short-lived). Your **API key + secret never touch the browser.** The flow:
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
Your backend (holds API key) Browser (this widget)
|
|
17
|
+
POST /apps/:id/support-sessions ── token + conversation_id ──▶ VocantlyWidget.init({...})
|
|
18
|
+
(X-Api-Key + X-Api-Secret)
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Your server calls the provisioning endpoint, then hands only `{ token, conversation_id }` to the page.
|
|
22
|
+
|
|
23
|
+
## Install
|
|
24
|
+
|
|
25
|
+
**Option A — script tag (any website, via CDN):**
|
|
26
|
+
|
|
27
|
+
```html
|
|
28
|
+
<!-- unpkg / jsDelivr serve the bundled IIFE; global: VocantlyWidget -->
|
|
29
|
+
<script src="https://unpkg.com/@vocantly/widget"></script>
|
|
30
|
+
<!-- pin a version: https://unpkg.com/@vocantly/widget@0.1.5 -->
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
**Option B — npm (React / Vue / Next / bundlers):**
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
npm install @vocantly/widget
|
|
37
|
+
```
|
|
38
|
+
```js
|
|
39
|
+
import { init } from '@vocantly/widget';
|
|
40
|
+
init({ wsUrl, token, conversationId, /* … */ });
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Both ship the SDK + deps bundled in — zero peer dependencies. Usage is the same either way:
|
|
44
|
+
|
|
45
|
+
```html
|
|
46
|
+
<script src="https://unpkg.com/@vocantly/widget"></script>
|
|
47
|
+
<script>
|
|
48
|
+
VocantlyWidget.init({
|
|
49
|
+
wsUrl: 'wss://your-realtime/ws',
|
|
50
|
+
token: 'SDK_TOKEN_FROM_YOUR_BACKEND',
|
|
51
|
+
conversationId: 'CONVERSATION_ID_FROM_YOUR_BACKEND',
|
|
52
|
+
apiUrl: 'https://your-backend/api/v1', // optional — enables message history
|
|
53
|
+
title: 'Support',
|
|
54
|
+
subtitle: 'We reply in a few minutes',
|
|
55
|
+
primaryColor: '#195EFF',
|
|
56
|
+
position: 'bottom-right', // or 'bottom-left'
|
|
57
|
+
greeting: 'Hi! 👋 How can we help?',
|
|
58
|
+
autoOpen: false,
|
|
59
|
+
|
|
60
|
+
// Optional pre-chat contact capture (needs apiUrl):
|
|
61
|
+
collectContact: true, // ask name + email before chatting
|
|
62
|
+
requireEmail: false, // if true, email is mandatory to start
|
|
63
|
+
contactPrompt: 'Before we start — how can we reach you?',
|
|
64
|
+
});
|
|
65
|
+
</script>
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Your backend (one call to provision)
|
|
69
|
+
|
|
70
|
+
```js
|
|
71
|
+
// server-side — keep the secret here
|
|
72
|
+
const r = await fetch(`${BASE}/apps/${APP_ID}/support-sessions`, {
|
|
73
|
+
method: 'POST',
|
|
74
|
+
headers: { 'X-Api-Key': PUBLIC_KEY, 'X-Api-Secret': SECRET_KEY, 'Content-Type': 'application/json' },
|
|
75
|
+
body: JSON.stringify({ external_user_id: yourUserId, name, email }),
|
|
76
|
+
});
|
|
77
|
+
const { token, conversation_id } = (await r.json()).data; // hand these to the browser
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## No backend? Anonymous embed with just your public key
|
|
81
|
+
|
|
82
|
+
For a static landing page with **unauthenticated visitors and no server**, use `publicKey` instead of
|
|
83
|
+
`token`/`conversationId`. The widget self-provisions an anonymous session — no secret in the browser:
|
|
84
|
+
|
|
85
|
+
```html
|
|
86
|
+
<script src="https://unpkg.com/@vocantly/widget"></script>
|
|
87
|
+
<script>
|
|
88
|
+
VocantlyWidget.init({
|
|
89
|
+
publicKey: 'pk_…', // your app PUBLIC key (safe to expose)
|
|
90
|
+
wsUrl: 'wss://ws.vocantly.com/ws',
|
|
91
|
+
apiUrl: 'https://api.vocantly.com/api/v1', // required in this mode
|
|
92
|
+
collectContact: true,
|
|
93
|
+
});
|
|
94
|
+
</script>
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
**One prerequisite — allow your site's origin.** Anonymous embedding is off by default. Add your
|
|
98
|
+
domain(s) to the app's `allowed_origins` (dashboard, or via the API):
|
|
99
|
+
|
|
100
|
+
```bash
|
|
101
|
+
PATCH /apps/:id { "allowed_origins": ["https://acme.com", "https://www.acme.com"] }
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
The widget generates a **stable visitor id** (localStorage) and caches `{token, conversationId}` so a
|
|
105
|
+
reload resumes the same conversation. Requests are origin-checked server-side and rate-limited.
|
|
106
|
+
|
|
107
|
+
> Provide **either** `publicKey` (anonymous) **or** `token` + `conversationId` (backend-provisioned).
|
|
108
|
+
|
|
109
|
+
## API
|
|
110
|
+
|
|
111
|
+
| Call | Effect |
|
|
112
|
+
|---|---|
|
|
113
|
+
| `VocantlyWidget.init(opts)` | Mount + connect. Returns `{ open, close, send, destroy }`. |
|
|
114
|
+
| `VocantlyWidget.open()` / `.close()` | Toggle the panel. |
|
|
115
|
+
| `VocantlyWidget.destroy()` | Disconnect + remove from the DOM. |
|
|
116
|
+
|
|
117
|
+
### Pre-chat contact capture
|
|
118
|
+
|
|
119
|
+
With `collectContact: true`, the widget asks the visitor for their name + (optional) email before
|
|
120
|
+
chatting and saves it to their customer profile via `PATCH /sdk/customers/me` (authenticated by the
|
|
121
|
+
SDK token — no secrets in the browser). Agents then see the visitor's contact details in the inbox.
|
|
122
|
+
Requires `apiUrl` to be set. Use `requireEmail: true` to make email mandatory.
|
|
123
|
+
|
|
124
|
+
## Build
|
|
125
|
+
|
|
126
|
+
```bash
|
|
127
|
+
npm run build # → dist/vocantly-widget.js (minified IIFE, global VocantlyWidget)
|
|
128
|
+
npm run build:dev # non-minified + sourcemap
|
|
129
|
+
```
|
|
130
|
+
The build bundles `../vocantly-js-sdk/dist/index.js`. If you rebuild the SDK, rebuild the widget.
|
|
131
|
+
|
|
132
|
+
## Demo
|
|
133
|
+
|
|
134
|
+
```bash
|
|
135
|
+
npm run serve:demo # serves this folder on :5173
|
|
136
|
+
# open http://localhost:5173/ (redirects to the demo)
|
|
137
|
+
```
|
|
138
|
+
> Avoid ports Chrome blocks as "unsafe" (e.g. 5060 = SIP → `ERR_UNSAFE_PORT`). 5173 is safe.
|
|
139
|
+
The demo is a mock business page. Enter a workspace owner's email/password + a **Development** App ID
|
|
140
|
+
and click **Boot** — it provisions via `/apps/:id/support-sessions` (dev login stands in for your
|
|
141
|
+
backend) and mounts the widget. Requires backend `:3001`, Go realtime `:8080`, and the app to have the
|
|
142
|
+
**bot enabled + an active FAQ**.
|
|
143
|
+
|
|
144
|
+
## Known limitation
|
|
145
|
+
|
|
146
|
+
Bot replies are currently delivered live but **not persisted** server-side, so they won't appear in
|
|
147
|
+
message history on reload (customer/agent messages do). Persisting bot messages is a backend follow-up.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
var Fe=Object.create;var z=Object.defineProperty;var Pe=Object.getOwnPropertyDescriptor;var $e=Object.getOwnPropertyNames;var Ve=Object.getPrototypeOf,We=Object.prototype.hasOwnProperty;var ye=(e,t)=>()=>{try{return t||e((t={exports:{}}).exports,t),t.exports}catch(s){throw t=0,s}},Ne=(e,t)=>{for(var s in t)z(e,s,{get:t[s],enumerable:!0})},Se=(e,t,s,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of $e(t))!We.call(e,i)&&i!==s&&z(e,i,{get:()=>t[i],enumerable:!(n=Pe(t,i))||n.enumerable});return e};var te=(e,t,s)=>(s=e!=null?Fe(Ve(e)):{},Se(t||!e||!e.__esModule?z(s,"default",{value:e,enumerable:!0}):s,e)),qe=e=>Se(z({},"__esModule",{value:!0}),e);var _e=ye((Ct,se)=>{"use strict";var Be=Object.prototype.hasOwnProperty,C="~";function F(){}Object.create&&(F.prototype=Object.create(null),new F().__proto__||(C=!1));function Je(e,t,s){this.fn=e,this.context=t,this.once=s||!1}function Ce(e,t,s,n,i){if(typeof s!="function")throw new TypeError("The listener must be a function");var a=new Je(s,n||e,i),r=C?C+t:t;return e._events[r]?e._events[r].fn?e._events[r]=[e._events[r],a]:e._events[r].push(a):(e._events[r]=a,e._eventsCount++),e}function K(e,t){--e._eventsCount===0?e._events=new F:delete e._events[t]}function S(){this._events=new F,this._eventsCount=0}S.prototype.eventNames=function(){var t=[],s,n;if(this._eventsCount===0)return t;for(n in s=this._events)Be.call(s,n)&&t.push(C?n.slice(1):n);return Object.getOwnPropertySymbols?t.concat(Object.getOwnPropertySymbols(s)):t};S.prototype.listeners=function(t){var s=C?C+t:t,n=this._events[s];if(!n)return[];if(n.fn)return[n.fn];for(var i=0,a=n.length,r=new Array(a);i<a;i++)r[i]=n[i].fn;return r};S.prototype.listenerCount=function(t){var s=C?C+t:t,n=this._events[s];return n?n.fn?1:n.length:0};S.prototype.emit=function(t,s,n,i,a,r){var l=C?C+t:t;if(!this._events[l])return!1;var c=this._events[l],h=arguments.length,f,d;if(c.fn){switch(c.once&&this.removeListener(t,c.fn,void 0,!0),h){case 1:return c.fn.call(c.context),!0;case 2:return c.fn.call(c.context,s),!0;case 3:return c.fn.call(c.context,s,n),!0;case 4:return c.fn.call(c.context,s,n,i),!0;case 5:return c.fn.call(c.context,s,n,i,a),!0;case 6:return c.fn.call(c.context,s,n,i,a,r),!0}for(d=1,f=new Array(h-1);d<h;d++)f[d-1]=arguments[d];c.fn.apply(c.context,f)}else{var y=c.length,m;for(d=0;d<y;d++)switch(c[d].once&&this.removeListener(t,c[d].fn,void 0,!0),h){case 1:c[d].fn.call(c[d].context);break;case 2:c[d].fn.call(c[d].context,s);break;case 3:c[d].fn.call(c[d].context,s,n);break;case 4:c[d].fn.call(c[d].context,s,n,i);break;default:if(!f)for(m=1,f=new Array(h-1);m<h;m++)f[m-1]=arguments[m];c[d].fn.apply(c[d].context,f)}}return!0};S.prototype.on=function(t,s,n){return Ce(this,t,s,n,!1)};S.prototype.once=function(t,s,n){return Ce(this,t,s,n,!0)};S.prototype.removeListener=function(t,s,n,i){var a=C?C+t:t;if(!this._events[a])return this;if(!s)return K(this,a),this;var r=this._events[a];if(r.fn)r.fn===s&&(!i||r.once)&&(!n||r.context===n)&&K(this,a);else{for(var l=0,c=[],h=r.length;l<h;l++)(r[l].fn!==s||i&&!r[l].once||n&&r[l].context!==n)&&c.push(r[l]);c.length?this._events[a]=c.length===1?c[0]:c:K(this,a)}return this};S.prototype.removeAllListeners=function(t){var s;return t?(s=C?C+t:t,this._events[s]&&K(this,s)):(this._events=new F,this._eventsCount=0),this};S.prototype.off=S.prototype.removeListener;S.prototype.addListener=S.prototype.on;S.prefixed=C;S.EventEmitter=S;typeof se!="undefined"&&(se.exports=S)});var xe=ye((jt,Te)=>{"use strict";Te.exports=function(){throw new Error("ws does not work in the browser. Browser clients must use the native WebSocket object")}});var yt={};Ne(yt,{close:()=>vt,destroy:()=>bt,init:()=>mt,open:()=>wt});module.exports=qe(yt);var k=te(_e(),1);var b=[];for(Q=0;Q<256;++Q)b.push((Q+256).toString(16).slice(1));var Q;function ke(e,t=0){return(b[e[t+0]]+b[e[t+1]]+b[e[t+2]]+b[e[t+3]]+"-"+b[e[t+4]]+b[e[t+5]]+"-"+b[e[t+6]]+b[e[t+7]]+"-"+b[e[t+8]]+b[e[t+9]]+"-"+b[e[t+10]]+b[e[t+11]]+b[e[t+12]]+b[e[t+13]]+b[e[t+14]]+b[e[t+15]]).toLowerCase()}var G,ze=new Uint8Array(16);function ne(){if(!G&&(G=typeof crypto!="undefined"&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto),!G))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return G(ze)}var Ke=typeof crypto!="undefined"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto),ie={randomUUID:Ke};function Qe(e,t,s){if(ie.randomUUID&&!t&&!e)return ie.randomUUID();e=e||{};var n=e.random||(e.rng||ne)();if(n[6]=n[6]&15|64,n[8]=n[8]&63|128,t){s=s||0;for(var i=0;i<16;++i)t[s+i]=n[i];return t}return ke(n)}var re=Qe;var ht={},Ge=(e=>typeof require!="undefined"?require:typeof Proxy!="undefined"?new Proxy(e,{get:(t,s)=>(typeof require!="undefined"?require:t)[s]}):e)(function(e){if(typeof require!="undefined")return require.apply(this,arguments);throw Error('Dynamic require of "'+e+'" is not supported')});var M=class{constructor(e=!1){this.enabled=e}setEnabled(e){this.enabled=e}debug(...e){this.enabled&&console.debug("[Vocantly SDK]",...e)}info(...e){this.enabled&&console.info("[Vocantly SDK]",...e)}warn(...e){this.enabled&&console.warn("[Vocantly SDK]",...e)}error(...e){this.enabled&&console.error("[Vocantly SDK]",...e)}},ae=typeof process!="undefined"&&process.versions!=null&&process.versions.node!=null,U=null,P=null;async function Ye(){return ae?U||P||(P=(async()=>{try{try{return U=await Promise.resolve().then(()=>te(xe(),1)),U}catch(e){if(typeof Ge!="undefined"){let{createRequire:t}=await import("module");return U=t(ht.url||__filename)("ws"),U}throw e}}catch{return null}finally{P=null}})(),P):null}var Xe=class extends k.default{constructor(e,t={}){var s,n,i;super(),this.ws=null,this.token=null,this.reconnectAttempts=0,this.reconnectTimer=null,this.isManuallyDisconnected=!1,this.isTokenExpired=!1,this.url=e,this.autoReconnect=(s=t.autoReconnect)!=null?s:!0,this.maxReconnectAttempts=(n=t.maxReconnectAttempts)!=null?n:5,this.logger=new M((i=t.debug)!=null?i:!1)}connect(e){if(!e||e.length===0){let t=new Error("Token is required for connection");return this.emit("error",t),Promise.reject(t)}return this.token=e,this.isManuallyDisconnected=!1,this.isTokenExpired=!1,this.attemptConnection()}async attemptConnection(){var s;let e=new URL(this.url),t=(s=this.token)!=null?s:"";if(ae){let n=await Ye();if(this.logger.debug("ws module loaded:",!!n),n&&n.WebSocket)return this.logger.debug("Using ws package with Authorization header"),new Promise((i,a)=>{try{let r=new n.WebSocket(e.toString(),{headers:{Authorization:`Bearer ${t}`}});this.ws=r;let l=!1,c=h=>{if(l)return;l=!0;let f=String((h==null?void 0:h.message)||h||"").toLowerCase();if(f.includes("401")||f.includes("unauthorized")||h!=null&&h.code&&String(h.code).includes("401")){this.handleTokenExpiration();let d=new Error("Token expired or invalid. Please refresh your token and reconnect.");d.name="TokenExpiredError",this.emit("error",d),a(d);return}};typeof r.addEventListener=="function"?(r.addEventListener("error",c),r.addEventListener("open",()=>{r.removeEventListener("error",c)})):typeof r.once=="function"&&(r.once("error",c),r.once("open",()=>{})),this.setupEventHandlers(i,a)}catch(r){this.logger.error("Failed to create ws.WebSocket:",r),a(r)}});this.logger.warn("ws package not available, falling back to native WebSocket")}return this.logger.debug("Using native WebSocket with query parameter",{url:e.toString(),hasToken:!!t}),new Promise((n,i)=>{try{if(!t||t.length===0){let l=new Error("Token is required for WebSocket connection");this.logger.error(l.message),i(l);return}let a=new URL(e.toString());a.searchParams.set("token",t),this.ws=new globalThis.WebSocket(a.toString()),this.logger.debug("WebSocket instance created",{readyState:this.ws.readyState,url:this.ws.url,protocol:this.ws.protocol});let r=this.ws;this.setupEventHandlers(n,i),setTimeout(()=>{(r.readyState===WebSocket.CLOSED||r.readyState===WebSocket.CLOSING)&&this.logger.warn("WebSocket closed immediately after creation",{readyState:r.readyState,url:r.url})},100)}catch(a){this.logger.error("Failed to create native WebSocket:",a),i(a)}})}setupEventHandlers(e,t){this.ws&&(this.ws.onopen=()=>{this.reconnectAttempts=0,this.isTokenExpired=!1;let s=this.ws;if(!s){this.logger.error("WebSocket is null in onopen handler"),t(new Error("WebSocket instance lost"));return}if(this.logger.debug("WebSocket connected",{readyState:s.readyState,url:s.url,protocol:s.protocol,extensions:s.extensions}),s.readyState!==WebSocket.OPEN){this.logger.error("WebSocket readyState is not OPEN after onopen",{readyState:s.readyState}),t(new Error(`WebSocket not open: readyState=${s.readyState}`));return}this.emit("connected"),e()},this.ws.onmessage=s=>{try{let n=JSON.parse(s.data);this.logger.debug("Received message:",n),this.emit("message",n)}catch(n){let i=n instanceof Error?n:new Error("Failed to parse message");this.logger.error("Failed to parse message:",i),this.emit("error",i)}},this.ws.onerror=s=>{this.logger.error("WebSocket error:",s);let n;if(s instanceof Error?n=s:s&&s.message?n=s instanceof Error?s:new Error(String(s.message)):n=new Error("WebSocket connection error"),ae&&s&&s.message){let i=String(s.message).toLowerCase();if(i.includes("401")||i.includes("unauthorized")){this.handleTokenExpiration();let a=new Error("Token expired or invalid. Please refresh your token and reconnect.");a.name="TokenExpiredError",this.emit("error",a),t(a);return}}this.emit("error",n)},this.ws.onclose=s=>{if(this.logger.debug("WebSocket closed:",{code:s.code,reason:s.reason,wasClean:s.wasClean}),s.code===1006&&this.logger.warn("Abnormal closure (1006) - connection closed without proper handshake. Possible causes: network issue, server closed connection, CORS issue, or protocol mismatch."),s.code===1008||s.code===1002||s.reason&&s.reason.toLowerCase().includes("unauthorized")||s.reason&&s.reason.toLowerCase().includes("401")){this.handleTokenExpiration();let i=new Error("Token expired or invalid. Please refresh your token and reconnect.");i.name="TokenExpiredError",this.emit("error",i),this.emit("disconnected",{code:s.code,reason:s.reason,tokenExpired:!0});return}this.emit("disconnected",{code:s.code,reason:s.reason}),!this.isManuallyDisconnected&&!this.isTokenExpired&&this.autoReconnect?this.reconnectAttempts<this.maxReconnectAttempts?this.scheduleReconnect():(this.logger.warn("Max reconnection attempts reached"),this.emit("error",new Error("Max reconnection attempts reached"))):this.isTokenExpired&&this.logger.warn("Not reconnecting: token is expired")})}handleTokenExpiration(){this.isTokenExpired=!0,this.autoReconnect=!1,this.reconnectTimer&&(clearTimeout(this.reconnectTimer),this.reconnectTimer=null)}disconnect(){this.isManuallyDisconnected=!0,this.autoReconnect=!1,this.reconnectTimer&&(clearTimeout(this.reconnectTimer),this.reconnectTimer=null),this.ws&&(this.ws.close(),this.ws=null),this.logger.debug("WebSocket disconnected")}send(e){if(!this.ws||this.ws.readyState!==WebSocket.OPEN){let t=new Error("WebSocket is not connected");throw this.logger.error(t.message),this.emit("error",t),t}try{let t=JSON.stringify(e);this.logger.debug("Sending message:",e),this.ws.send(t)}catch(t){let s=t instanceof Error?t:new Error("Failed to send message");throw this.logger.error("Send error:",s),this.emit("error",s),s}}isConnected(){return this.ws!==null&&this.ws.readyState===WebSocket.OPEN}getReconnectAttempts(){return this.reconnectAttempts}scheduleReconnect(){if(this.reconnectTimer||!this.token)return;this.reconnectAttempts++;let e=[1e3,2e3,5e3,1e4],t=e[Math.min(this.reconnectAttempts-1,e.length-1)];this.logger.debug(`Scheduling reconnect attempt ${this.reconnectAttempts} in ${t}ms`),this.emit("reconnecting",{attempt:this.reconnectAttempts,delay:t}),this.reconnectTimer=setTimeout(()=>{this.reconnectTimer=null,this.attemptConnection().catch(()=>{})},t)}},Ze=class{constructor(){this.token=null}setToken(e){if(!this.isValidFormat(e))throw new Error("Invalid token format");this.token=e}getToken(){return this.token}hasToken(){return this.token!==null&&this.token.length>0}clearToken(){this.token=null}rotateToken(e){if(!this.isValidFormat(e))throw new Error("Invalid token format");this.token=e}isValidFormat(e){return typeof e=="string"&&e.length>0}validateTokenPresence(){if(!this.hasToken())throw new Error("Token is required but not set")}},$=class{static createOptimistic(e,t,s,n="text",i){return{id:t,conversationId:e,content:s,messageType:n,optimistic:!0,receiptState:"sent",createdAt:new Date,metadata:i}}static fromServerPayload(e,t){var i,a;let s=(i=t.sender_id)!=null?i:t.senderId,n=(a=t.sender_type)!=null?a:t.senderType;return{id:t.message_id,conversationId:e,content:t.content,senderId:s,senderName:t.sender_name,senderType:n,messageType:t.message_type==="system"?"system":"text",optimistic:!1,receiptState:"sent",createdAt:t.sent_at?new Date(t.sent_at):new Date}}static confirmOptimistic(e,t){var i,a,r;let s=(i=t.sender_id)!=null?i:t.senderId,n=(a=t.sender_type)!=null?a:t.senderType;return{...e,optimistic:!1,senderId:s,senderName:t.sender_name,senderType:n,receiptState:(r=e.receiptState)!=null?r:"sent",createdAt:t.sent_at?new Date(t.sent_at):e.createdAt}}static updateReceiptState(e,t){return{...e,receiptState:t}}},et=class extends k.default{constructor(e=!1){super(),this.receiptState=new Map,this.logger=new M(e)}getReceiptState(e){return this.receiptState.get(e)}handleDelivered(e,t){var n;if(!e)return this.logger.warn("Invalid delivered receipt: missing messageId"),!1;let s=(n=this.receiptState.get(e))!=null?n:"sent";return this.compareStates(s,"delivered")<0?(this.receiptState.set(e,"delivered"),this.emit("receipt",{messageId:e,state:"delivered",userId:t}),this.logger.debug("Receipt delivered:",e),!0):(this.logger.debug("Delivered receipt ignored (already delivered or read):",e),!1)}handleRead(e,t){var n;if(!e)return this.logger.warn("Invalid read receipt: missing messageId"),!1;let s=(n=this.receiptState.get(e))!=null?n:"sent";return this.compareStates(s,"read")<0?(this.receiptState.set(e,"read"),this.emit("receipt",{messageId:e,state:"read",userId:t}),this.logger.debug("Receipt read:",e),!0):(this.logger.debug("Read receipt ignored (already read):",e),!1)}compareStates(e,t){let s=["sent","delivered","read"],n=s.indexOf(e),i=s.indexOf(t);return n===-1||i===-1?0:n-i}clearConversation(e,t){let s=0;for(let n of t)this.receiptState.has(n)&&(this.receiptState.delete(n),s++);s>0&&this.logger.debug("Cleared receipt state for conversation:",e,`(${s} messages)`)}clearMessage(e){this.receiptState.delete(e)&&this.logger.debug("Cleared receipt state for message:",e)}clear(){this.receiptState.clear(),this.logger.debug("Cleared all receipt state")}},tt=class extends k.default{constructor(e=!1){super(),this.seenMessageIds=new Map,this.optimisticMessages=new Map,this.confirmedMessages=new Map,this.warnedMissingSenderId=new Set,this.logger=new M(e),this.receiptManager=new et(e),this.receiptManager.on("receipt",t=>{this.updateMessageReceiptState(t)})}createOptimisticMessage(e,t,s,n="text",i){let a=$.createOptimistic(e,t,s,n,i);return this.trackOptimisticMessage(e,t,a),this.trackMessageId(e,t),this.emit("message",a),this.logger.debug("Created optimistic message:",t),a}handleIncomingMessage(e,t){let s=t.message_id,n=this.getOptimisticMessage(e,s);if(n){let a=this.reconcileOptimisticMessage(n,t);return a.senderId==null&&!this.warnedMissingSenderId.has(e)&&(this.warnedMissingSenderId.add(e),console.warn('[Vocantly SDK] Message payload missing sender_id (and sender_type). Clients cannot show "You" vs other participant. The realtime backend MUST include sender_id and sender_type in every type:"message" payload. See docs/REALTIME_WEBSOCKET_CONTRACT.md.')),this.removeOptimisticMessage(e,s),this.trackMessageId(e,s),this.trackConfirmedMessage(e,s,a),a}if(this.hasSeenMessage(e,s))return this.logger.debug("Duplicate message ignored:",s),null;this.trackMessageId(e,s);let i=$.fromServerPayload(e,t);return i.senderId==null&&!this.warnedMissingSenderId.has(e)&&(this.warnedMissingSenderId.add(e),console.warn('[Vocantly SDK] Message payload missing sender_id (and sender_type). Clients cannot show "You" vs other participant. The realtime backend MUST include sender_id and sender_type in every type:"message" payload. See docs/REALTIME_WEBSOCKET_CONTRACT.md.')),this.trackConfirmedMessage(e,s,i),this.emit("message",i),this.logger.debug("Handled incoming message:",s),i}handleReceipt(e,t,s,n){s==="delivered"?this.receiptManager.handleDelivered(t,n):s==="read"&&this.receiptManager.handleRead(t,n)}updateMessageReceiptState(e){for(let[,t]of this.confirmedMessages.entries()){let s=t.get(e.messageId);if(s){let n=$.updateReceiptState(s,e.state);t.set(e.messageId,n),this.emit("message",n),this.logger.debug("Updated message receipt state:",e.messageId,e.state);return}}for(let[,t]of this.optimisticMessages.entries()){let s=t.get(e.messageId);if(s){let n=$.updateReceiptState(s,e.state);t.set(e.messageId,n),this.emit("message",n),this.logger.debug("Updated optimistic message receipt state:",e.messageId,e.state);return}}this.logger.warn("Receipt for unknown message:",e.messageId)}reconcileOptimisticMessage(e,t){let s=$.confirmOptimistic(e,t);return this.logger.debug("Reconciled optimistic message (no second emit):",e.id),s}hasSeenMessage(e,t){var n;let s=this.seenMessageIds.get(e);return(n=s==null?void 0:s.has(t))!=null?n:!1}trackMessageIdsFromHistory(e,t){for(let s of t)this.trackMessageId(e,s)}trackMessageId(e,t){let s=this.seenMessageIds.get(e);s||(s=new Set,this.seenMessageIds.set(e,s)),s.add(t)}trackOptimisticMessage(e,t,s){let n=this.optimisticMessages.get(e);n||(n=new Map,this.optimisticMessages.set(e,n)),n.set(t,s)}getOptimisticMessage(e,t){let s=this.optimisticMessages.get(e);return s==null?void 0:s.get(t)}removeOptimisticMessage(e,t){let s=this.optimisticMessages.get(e);s&&(s.delete(t),s.size===0&&this.optimisticMessages.delete(e))}trackConfirmedMessage(e,t,s){let n=this.confirmedMessages.get(e);n||(n=new Map,this.confirmedMessages.set(e,n)),n.set(t,s)}getMessageIds(e){let t=this.confirmedMessages.get(e),s=this.optimisticMessages.get(e),n=new Set;if(t)for(let i of t.keys())n.add(i);if(s)for(let i of s.keys())n.add(i);return Array.from(n)}clearConversation(e){this.seenMessageIds.delete(e),this.optimisticMessages.delete(e);let t=this.getMessageIds(e);this.receiptManager.clearConversation(e,t),this.confirmedMessages.delete(e),this.logger.debug("Cleared message tracking for conversation:",e)}clear(){this.seenMessageIds.clear(),this.optimisticMessages.clear(),this.confirmedMessages.clear(),this.receiptManager.clear(),this.logger.debug("Cleared all message tracking")}},st=class extends k.default{constructor(e=!1){super(),this.typingTimers=new Map,this.activeTyping=new Map,this.TYPING_TIMEOUT_MS=5e3,this.logger=new M(e)}handleTypingStart(e,t){if(!e||!t){this.logger.warn("Invalid typing_start: missing conversationId or userId");return}let s=this.activeTyping.get(e);s||(s=new Set,this.activeTyping.set(e,s));let n=this.typingTimers.get(e);n||(n=new Map,this.typingTimers.set(e,n));let i=s.has(t),a=n.get(t);a&&clearTimeout(a),s.add(t);let r=setTimeout(()=>{this.handleTypingStop(e,t)},this.TYPING_TIMEOUT_MS);n.set(t,r),i?this.logger.debug("Typing timer restarted:",e,t):(this.emit("typing",{conversationId:e,userId:t,state:"start"}),this.logger.debug("Typing started:",e,t))}handleTypingStop(e,t){if(!e||!t){this.logger.warn("Invalid typing_stop: missing conversationId or userId");return}let s=this.activeTyping.get(e),n=this.typingTimers.get(e);if(!s||!s.has(t)){this.logger.debug("Typing stop ignored (user not typing):",e,t);return}let i=n==null?void 0:n.get(t);i&&(clearTimeout(i),n==null||n.delete(t)),s.delete(t),s.size===0&&this.activeTyping.delete(e),n&&n.size===0&&this.typingTimers.delete(e),this.emit("typing",{conversationId:e,userId:t,state:"stop"}),this.logger.debug("Typing stopped:",e,t)}clearConversation(e){let t=this.activeTyping.get(e),s=this.typingTimers.get(e);if(t){for(let n of t)this.emit("typing",{conversationId:e,userId:n,state:"stop"});this.activeTyping.delete(e)}if(s){for(let n of s.values())clearTimeout(n);this.typingTimers.delete(e)}this.logger.debug("Cleared typing state for conversation:",e)}clear(){for(let e of this.typingTimers.values())for(let t of e.values())clearTimeout(t);this.typingTimers.clear(),this.activeTyping.clear(),this.logger.debug("Cleared all typing state")}getTypingUsers(e){let t=this.activeTyping.get(e);return t?Array.from(t):[]}},nt=[{urls:"stun:stun.l.google.com:19302"},{urls:"stun:stun1.l.google.com:19302"}],Ee,Me=typeof RTCPeerConnection!="undefined"&&typeof navigator!="undefined"&&typeof((Ee=navigator.mediaDevices)==null?void 0:Ee.getUserMedia)=="function",V=class extends k.default{constructor(e={}){var t;super(),this.peers=new Map,this.localStream=null,this.mediaOptions={},this.screenTrack=null,this.cameraTrack=null,this.options=e,this.iceServers=(t=e.iceServers)!=null?t:nt,this.getIceServers=e.getIceServers,this.iceServersResolved=!e.getIceServers}static isAvailable(){return Me}static async enumerateDevices(){var e;if(!((e=navigator.mediaDevices)!=null&&e.enumerateDevices))return[];try{return await navigator.mediaDevices.enumerateDevices()}catch{return[]}}setMediaOptions(e){this.mediaOptions={...this.mediaOptions,...e}}async getMedia(e){if(this.localStream)return this.localStream;let t=this.mediaOptions,s={echoCancellation:!0,noiseSuppression:t.noiseSuppression!==!1};t.audioDeviceId&&(s.deviceId={exact:t.audioDeviceId});let n=!1;if(e==="video"){let a={};t.videoDeviceId&&(a.deviceId={exact:t.videoDeviceId}),t.lowBandwidth&&(a.width={ideal:640},a.height={ideal:360},a.frameRate={ideal:15,max:20}),n=Object.keys(a).length?a:!0}let i=await navigator.mediaDevices.getUserMedia({audio:s,video:n});return t.startMuted&&i.getAudioTracks().forEach(a=>a.enabled=!1),t.startVideoOff&&i.getVideoTracks().forEach(a=>a.enabled=!1),this.localStream=i,i}async createPeerConnection(e,t){if(!Me)return null;if(!this.iceServersResolved&&this.getIceServers)try{this.iceServers=await this.getIceServers(),this.iceServersResolved=!0}catch(i){this.options.debug&&console.warn("Failed to fetch ICE servers, using defaults:",i),this.iceServersResolved=!0}let s=new RTCPeerConnection({iceServers:this.iceServers}),n={pc:s};return this.peers.set(e,n),s.ontrack=i=>{i.streams[0]&&(n.remoteStream=i.streams[0],this.emit("remote_stream",{userId:e,stream:i.streams[0]}))},s.onicecandidate=i=>{i.candidate&&this.emit("ice_candidate",{userId:e,candidate:i.candidate.toJSON()})},s.oniceconnectionstatechange=()=>{if(s.iceConnectionState==="connected"||s.iceConnectionState==="completed")this.mediaOptions.lowBandwidth&&this.setVideoBitrate(35e4),this.emit("peer_connected",{userId:e});else if(s.iceConnectionState==="failed"){let i=this.peers.get(e);i&&!i.hasRestartedIce&&typeof s.restartIce=="function"?(i.hasRestartedIce=!0,s.restartIce()):this.emit("ice_failed",{userId:e})}},s}async createOffer(e,t){let s=await this.getMedia(t),n=await this.createPeerConnection(e,t);if(!n)return null;s.getTracks().forEach(a=>n.addTrack(a,s));let i=await n.createOffer();return await n.setLocalDescription(i),i}async handleOffer(e,t,s){let n=await this.getMedia(s),i=await this.createPeerConnection(e,s);if(!i)return null;n.getTracks().forEach(r=>i.addTrack(r,n)),await i.setRemoteDescription(new RTCSessionDescription(t));let a=await i.createAnswer();return await i.setLocalDescription(a),a}async handleAnswer(e,t){let s=this.peers.get(e);s&&await s.pc.setRemoteDescription(new RTCSessionDescription(t))}async addIceCandidate(e,t){let s=this.peers.get(e);if(s)try{await s.pc.addIceCandidate(new RTCIceCandidate(t))}catch(n){this.options.debug&&console.warn("addIceCandidate failed:",n)}}getLocalStream(){return this.localStream}getRemoteStream(e){var t;return(t=this.peers.get(e))==null?void 0:t.remoteStream}getAllRemoteStreams(){let e=new Map;for(let[t,s]of this.peers)s.remoteStream&&e.set(t,s.remoteStream);return e}async applyVideoConstraints(e){var s;let t=(s=this.localStream)==null?void 0:s.getVideoTracks()[0];if(t)try{await t.applyConstraints(e)}catch{}}async applyAudioConstraints(e){var s;let t=(s=this.localStream)==null?void 0:s.getAudioTracks()[0];if(t)try{await t.applyConstraints(e)}catch{}}async setVideoBitrate(e){for(let[,t]of this.peers){let s=t.pc.getSenders().find(i=>{var a;return((a=i.track)==null?void 0:a.kind)==="video"});if(!s)continue;let n=s.getParameters();(!n.encodings||n.encodings.length===0)&&(n.encodings=[{}]);for(let i of n.encodings)e==null?delete i.maxBitrate:i.maxBitrate=e;try{await s.setParameters(n)}catch{}}}async setLowBandwidth(e){this.mediaOptions.lowBandwidth=e,await this.applyVideoConstraints(e?{width:{max:640},height:{max:360},frameRate:{max:20}}:{width:{ideal:1280},height:{ideal:720},frameRate:{ideal:30}}),await this.setVideoBitrate(e?35e4:null)}async switchDevice(e,t){if(!this.localStream)return;let s=e==="video"?{video:{deviceId:{exact:t}}}:{audio:{deviceId:{exact:t}}},n=await navigator.mediaDevices.getUserMedia(s),i=e==="video"?n.getVideoTracks()[0]:n.getAudioTracks()[0];if(!i)return;let a=e==="video"?this.localStream.getVideoTracks()[0]:this.localStream.getAudioTracks()[0];a&&(i.enabled=a.enabled);for(let[,r]of this.peers){let l=r.pc.getSenders().find(c=>{var h;return((h=c.track)==null?void 0:h.kind)===e});l&&await l.replaceTrack(i)}a&&(this.localStream.removeTrack(a),a.stop()),this.localStream.addTrack(i)}isScreenSharing(){return!!this.screenTrack}async startScreenShare(){var i,a;if(this.screenTrack)return!0;let e=navigator.mediaDevices;if(!e.getDisplayMedia)return!1;let t=await e.getDisplayMedia({video:!0,audio:!1}),s=t.getVideoTracks()[0];if(!s)return!1;this.cameraTrack=(a=(i=this.localStream)==null?void 0:i.getVideoTracks()[0])!=null?a:null;let n=!1;for(let[,r]of this.peers){let l=r.pc.getSenders().find(c=>{var h;return((h=c.track)==null?void 0:h.kind)==="video"});l&&(await l.replaceTrack(s),n=!0)}return n?(this.screenTrack=s,s.onended=()=>{this.stopScreenShare()},this.emit("screen_share_changed",{active:!0,stream:t}),!0):(s.stop(),!1)}async stopScreenShare(){var e;if(this.screenTrack){for(let[,t]of this.peers){let s=t.pc.getSenders().find(n=>{var i;return((i=n.track)==null?void 0:i.kind)==="video"||n.track===this.screenTrack});s&&await s.replaceTrack((e=this.cameraTrack)!=null?e:null)}this.screenTrack.stop(),this.screenTrack=null,this.emit("screen_share_changed",{active:!1})}}closePeer(e){let t=this.peers.get(e);t&&t.pc.signalingState!=="closed"&&t.pc.close(),this.peers.delete(e)}cleanup(){for(let[e]of this.peers)this.closePeer(e);this.localStream&&(this.localStream.getTracks().forEach(e=>e.stop()),this.localStream=null),this.removeAllListeners()}async getCallStats(){let e={};for(let[,t]of this.peers)try{(await t.pc.getStats()).forEach(n=>{var a,r,l,c,h,f,d,y,m,v;let i=n;if(n.type==="inbound-rtp")e.packets_lost_in=((a=e.packets_lost_in)!=null?a:0)+((r=i.packetsLost)!=null?r:0),e.packets_received=((l=e.packets_received)!=null?l:0)+((c=i.packetsReceived)!=null?c:0),e.bytes_received=((h=e.bytes_received)!=null?h:0)+((f=i.bytesReceived)!=null?f:0),typeof i.jitter=="number"&&(e.jitter_ms=i.jitter*1e3);else if(n.type==="outbound-rtp")e.packets_sent=((d=e.packets_sent)!=null?d:0)+((y=i.packetsSent)!=null?y:0),e.bytes_sent=((m=e.bytes_sent)!=null?m:0)+((v=i.bytesSent)!=null?v:0);else if(n.type==="candidate-pair"&&n.state==="succeeded"){let R=n.currentRoundTripTime;typeof R=="number"&&(e.rtt_ms=R*1e3)}})}catch{}return e}setLocalAudioEnabled(e){this.localStream&&this.localStream.getAudioTracks().forEach(t=>t.enabled=e)}setLocalVideoEnabled(e){this.localStream&&this.localStream.getVideoTracks().forEach(t=>t.enabled=e)}},it=6e4,rt=class extends k.default{constructor(e,t={}){var s,n;super(),this.ringTimeoutId=null,this.webRTC=null,this.callRole=null,this.currentCallType="video",this.participants=new Set,this.webRTCEventsSetup=!1,this.callStartedAt=null,this.callConnectedEmitted=!1,this.conversationId=e,this.logger=new M((s=t.debug)!=null?s:!1),this.ringTimeoutMs=(n=t.ringTimeout)!=null?n:it,this.submitCallQuality=t.submitCallQuality,V.isAvailable()&&(this.webRTC=new V({debug:t.debug,getIceServers:t.getIceServers}))}emitCallConnected(e,t,s){this.callConnectedEmitted||(this.callConnectedEmitted=!0,this.callStartedAt==null&&(this.callStartedAt=Date.now()),this.emit("call_connected",{localStream:e,remoteStream:t,remoteUserId:s}))}setSendCallHandler(e){this.sendCallHandler=e}setCallOptions(e){var t;(t=this.webRTC)==null||t.setMediaOptions(e)}listDevices(){return V.enumerateDevices()}applyVideoConstraints(e){return this.webRTC?this.webRTC.applyVideoConstraints(e):Promise.resolve()}applyAudioConstraints(e){return this.webRTC?this.webRTC.applyAudioConstraints(e):Promise.resolve()}switchDevice(e,t){return this.webRTC?this.webRTC.switchDevice(e,t):Promise.resolve()}setLowBandwidth(e){return this.webRTC?this.webRTC.setLowBandwidth(e):Promise.resolve()}startScreenShare(){return this.webRTC?this.webRTC.startScreenShare():Promise.resolve(!1)}stopScreenShare(){return this.webRTC?this.webRTC.stopScreenShare():Promise.resolve()}isScreenSharing(){var e,t;return(t=(e=this.webRTC)==null?void 0:e.isScreenSharing())!=null?t:!1}setSubmitCallQuality(e){this.submitCallQuality=e}send(e){if(!this.sendCallHandler){let t=new Error("Call not connected. Join conversation first.");throw this.emit("call_error",{reason:"websocket_lost",message:t.message}),t}this.sendCallHandler(e)}clearRingTimeout(){this.ringTimeoutId&&(clearTimeout(this.ringTimeoutId),this.ringTimeoutId=null)}startCall(e="video"){this.clearRingTimeout(),this.callConnectedEmitted=!1,this.callRole="caller",this.currentCallType=e;let t={type:"call_invite",conversation_id:this.conversationId,payload:{call_type:e}};this.send(t),this.logger.debug("call.invite sent",{conversation_id:this.conversationId,call_type:e}),this.ringTimeoutId=setTimeout(()=>{this.ringTimeoutId=null,this.logger.debug("call ring timeout - no answer"),this.endCall("no_answer"),this.emit("call_timeout",{reason:"no_answer"})},this.ringTimeoutMs)}acceptCall(){this.clearRingTimeout(),this.callConnectedEmitted=!1,this.callRole="callee";let e={type:"call_accept",conversation_id:this.conversationId};this.send(e),this.logger.debug("call.accept sent",{conversation_id:this.conversationId})}joinCall(e="video"){this.callRole="joiner",this.currentCallType=e;let t={type:"call_join",conversation_id:this.conversationId};this.send(t),this.logger.debug("call.join sent",{conversation_id:this.conversationId})}rejectCall(e){this.clearRingTimeout();let t={type:"call_reject",conversation_id:this.conversationId,payload:e?{reason:e}:void 0};this.send(t),this.logger.debug("call.reject sent",{conversation_id:this.conversationId,reason:e})}endCall(e){this.clearRingTimeout();let t={type:"call_hangup",conversation_id:this.conversationId,payload:e?{reason:e}:void 0};this.send(t),this.logger.debug("call.hangup sent",{conversation_id:this.conversationId,reason:e})}handleCallInvite(e){this.currentCallType=e.call_type||"video",this.emit("call_invite",{callType:this.currentCallType,callerId:e.caller_id,callerName:e.caller_name})}async handleCallAccept(e){this.clearRingTimeout();let t=e==null?void 0:e.sender_id;if(this.participants.add(t!=null?t:"unknown"),this.webRTC&&t&&this.callRole==="caller")try{let s=await this.webRTC.createOffer(t,this.currentCallType);s&&(this.sendOffer(t,s),this.setupWebRTCEvents())}catch(s){this.logger.debug("WebRTC createOffer failed:",s),this.emit("call_error",{reason:"permission_denied",message:String(s)})}this.emit("call_accept",{accepterId:t})}async handleCallJoin(e){let t=e.joiner_id;if(t&&this.participants.add(t),this.webRTC&&t&&(this.callRole==="caller"||this.callRole==="callee"))try{let s=await this.webRTC.createOffer(t,this.currentCallType);s&&(this.sendOffer(t,s),this.setupWebRTCEvents())}catch(s){this.logger.debug("WebRTC createOffer for joiner failed:",s)}this.emit("call_join",{joinerId:e.joiner_id,joinerName:e.joiner_name})}handleCallReject(e){this.clearRingTimeout(),this.emit("call_reject",{reason:e==null?void 0:e.reason})}async handleCallHangup(e){var s;this.clearRingTimeout();let t=(s=e==null?void 0:e.reason)!=null?s:"user_hangup";await this.submitQualityAndCleanup(t),this.callRole=null,this.participants.clear(),this.emit("call_hangup",{endedBy:e==null?void 0:e.ended_by,reason:t})}handleCallParticipantLeft(e){var s;let t=e.left_user_id;t&&(this.participants.delete(t),(s=this.webRTC)==null||s.closePeer(t)),this.emit("peer_left",{userId:t})}handleCallError(e){this.clearRingTimeout(),this.emit("call_error",{code:e.code,message:e.message,details:e.details})}async handleCallOffer(e){var n;let t=(n=e.from_user_id)!=null?n:e.sender_id,s=e.sdp;if(!(!t||!s)){if(this.participants.add(t),this.webRTC&&(this.callRole==="callee"||this.callRole==="joiner"))try{this.setupWebRTCEvents();let i=await this.webRTC.handleOffer(t,s,this.currentCallType);if(i){this.sendAnswer(t,i);let a=this.webRTC.getLocalStream(),r=this.webRTC.getRemoteStream(t);a&&r&&this.emitCallConnected(a,r,t)}}catch(i){this.logger.debug("WebRTC handleOffer failed:",i),this.emit("call_error",{reason:"permission_denied",message:String(i)})}this.emit("call_offer",{sdp:s,fromUserId:t})}}async handleCallAnswer(e){var n;let t=(n=e.from_user_id)!=null?n:e.sender_id,s=e.sdp;if(!(!t||!s)){if(this.webRTC){await this.webRTC.handleAnswer(t,s);let i=this.webRTC.getLocalStream(),a=this.webRTC.getRemoteStream(t);i&&a&&this.emitCallConnected(i,a,t)}this.emit("call_answer",{sdp:s,fromUserId:t})}}async handleCallICECandidate(e){var n;let t=(n=e.from_user_id)!=null?n:e.sender_id,s=e.candidate;!t||!s||(this.webRTC&&await this.webRTC.addIceCandidate(t,s),this.emit("call_ice_candidate",{candidate:s,fromUserId:t}))}handleCallMute(e){this.emit("peer_mute",{userId:e==null?void 0:e.user_id})}handleCallUnmute(e){this.emit("peer_unmute",{userId:e==null?void 0:e.user_id})}handleVideoOff(e){this.emit("peer_video_off",{userId:e==null?void 0:e.user_id})}handleVideoOn(e){this.emit("peer_video_on",{userId:e==null?void 0:e.user_id})}setMuted(e){var s;(s=this.webRTC)==null||s.setLocalAudioEnabled(!e);let t=e?{type:"call_mute",conversation_id:this.conversationId}:{type:"call_unmute",conversation_id:this.conversationId};this.send(t),this.emit("local_mute_changed",{muted:e})}setVideoEnabled(e){var s;(s=this.webRTC)==null||s.setLocalVideoEnabled(e);let t=e?{type:"video_on",conversation_id:this.conversationId}:{type:"video_off",conversation_id:this.conversationId};this.send(t),this.emit("local_video_changed",{enabled:e})}getLocalStream(){var e,t;return(t=(e=this.webRTC)==null?void 0:e.getLocalStream())!=null?t:null}getRemoteStream(e){var t;return(t=this.webRTC)==null?void 0:t.getRemoteStream(e)}getAllRemoteStreams(){var e,t;return(t=(e=this.webRTC)==null?void 0:e.getAllRemoteStreams())!=null?t:new Map}sendOffer(e,t){let s={sdp:t,target_user_id:this.participants.size>2?e:void 0};this.send({type:"call_offer",conversation_id:this.conversationId,payload:s})}sendAnswer(e,t){let s={sdp:t,target_user_id:this.participants.size>2?e:void 0};this.send({type:"call_answer",conversation_id:this.conversationId,payload:s})}setupWebRTCEvents(){!this.webRTC||this.webRTCEventsSetup||(this.webRTCEventsSetup=!0,this.webRTC.on("ice_candidate",({userId:e,candidate:t})=>{let s={candidate:t,target_user_id:this.participants.size>2?e:void 0};this.send({type:"call_ice_candidate",conversation_id:this.conversationId,payload:s})}),this.webRTC.on("remote_stream",({userId:e,stream:t})=>{var n;let s=(n=this.webRTC)==null?void 0:n.getLocalStream();s&&this.emitCallConnected(s,t,e)}),this.webRTC.on("ice_failed",async()=>{await this.submitQualityAndCleanup("ice_failed"),this.emit("call_error",{reason:"ice_failed",message:"ICE connection failed"})}),this.webRTC.on("screen_share_changed",e=>this.emit("screen_share_changed",e)))}async submitQualityAndCleanup(e,t=!1){let s=this.webRTC,n={end_reason:e};this.callStartedAt!=null&&(n.duration_seconds=Math.max(0,(Date.now()-this.callStartedAt)/1e3));try{if(s){let i=await s.getCallStats();Object.assign(n,i)}}catch{}if(this.submitCallQuality)try{await this.submitCallQuality(this.conversationId,n,e)}catch(i){this.logger.debug("submitCallQuality failed:",i)}s==null||s.cleanup(),this.webRTC=t?null:V.isAvailable()?new V({}):null,this.webRTCEventsSetup=!1,this.callStartedAt=null,this.callConnectedEmitted=!1}async getCallStats(){let e={};if(this.callStartedAt!=null&&(e.duration_seconds=Math.max(0,(Date.now()-this.callStartedAt)/1e3)),this.webRTC){let t=await this.webRTC.getCallStats();Object.assign(e,t)}return e}cleanup(){this.clearRingTimeout(),this.submitQualityAndCleanup("user_left",!0),this.webRTC=null,this.webRTCEventsSetup=!1,this.callRole=null,this.participants.clear(),this.removeAllListeners()}},at=class extends k.default{constructor(e,t=!1){var n,i,a;super(),this._isJoined=!1;let s=typeof t=="boolean"?{debug:t}:t;this.id=e,this.messageManager=new tt((n=s.debug)!=null?n:!1),this.typingManager=new st((i=s.debug)!=null?i:!1),this.callManager=new rt(e,{debug:(a=s.debug)!=null?a:!1,getIceServers:s.getIceServers}),this.callManager.on("call_invite",r=>this.emit("call_invite",r)),this.callManager.on("call_accept",r=>this.emit("call_accept",r)),this.callManager.on("call_join",r=>this.emit("call_join",r)),this.callManager.on("call_reject",r=>this.emit("call_reject",r)),this.callManager.on("call_hangup",r=>this.emit("call_hangup",r)),this.callManager.on("peer_left",r=>this.emit("peer_left",r)),this.callManager.on("peer_mute",r=>this.emit("peer_mute",r)),this.callManager.on("peer_unmute",r=>this.emit("peer_unmute",r)),this.callManager.on("peer_video_off",r=>this.emit("peer_video_off",r)),this.callManager.on("peer_video_on",r=>this.emit("peer_video_on",r)),this.callManager.on("local_mute_changed",r=>this.emit("local_mute_changed",r)),this.callManager.on("local_video_changed",r=>this.emit("local_video_changed",r)),this.callManager.on("screen_share_changed",r=>this.emit("screen_share_changed",r)),this.callManager.on("call_connected",r=>this.emit("call_connected",r)),this.callManager.on("call_error",r=>this.emit("call_error",r)),this.callManager.on("call_timeout",r=>this.emit("call_timeout",r)),this.messageManager.on("message",r=>{r.conversationId===this.id&&this.emit("message",r)}),this.messageManager.on("receipt",r=>{this.emit("receipt",r)}),this.typingManager.on("typing",r=>{r.conversationId===this.id&&this.emit("typing",{userId:r.userId,state:r.state})})}get isJoined(){return this._isJoined}setSendMessageHandler(e){this.sendMessageHandler=e}setSendTypingHandler(e){this.sendTypingHandler=e}setSendReadHandler(e){this.sendReadHandler=e}setSendCallHandler(e){this.callManager.setSendCallHandler(e)}setSubmitCallQuality(e){this.callManager.setSubmitCallQuality(e)}setFetchMessagesCallback(e){this.fetchMessagesCallback=e}async fetchMessages(e){var s;if(!this.fetchMessagesCallback)throw new Error("REST not configured. Set apiUrl in Vocantly config for fetchMessages() (token, API keys, or getRestHeaders).");let t=await this.fetchMessagesCallback({externalUserId:(s=e==null?void 0:e.externalUserId)!=null?s:"",cursor:e==null?void 0:e.cursor,limit:e==null?void 0:e.limit});return this.messageManager.trackMessageIdsFromHistory(this.id,t.messages.map(n=>n.id)),t}markJoined(){this._isJoined||(this._isJoined=!0,this.emit("joined",{conversation_id:this.id}))}markLeft(){this._isJoined&&(this._isJoined=!1,this.callManager.cleanup(),this.emit("left",{conversation_id:this.id}))}sendMessage(e,t="text",s){if(!this.sendMessageHandler){let a=new Error("Conversation is not connected. Call join() first.");throw this.emit("error",a),a}if(!this._isJoined){let a=new Error("Conversation is not joined. Call join() first.");throw this.emit("error",a),a}let n=re();this.messageManager.createOptimisticMessage(this.id,n,e,t,s);let i={type:"message",conversation_id:this.id,payload:{message_id:n,content:e,message_type:t}};try{this.sendMessageHandler(i)}catch(a){let r=a instanceof Error?a:new Error("Failed to send message");throw this.emit("error",r),r}}handleIncomingMessage(e){this.messageManager.handleIncomingMessage(this.id,e)}handleReceipt(e,t,s){this.messageManager.handleReceipt(this.id,e,t,s)}handleTyping(e){e.state==="start"?this.typingManager.handleTypingStart(this.id,e.user_id):e.state==="stop"&&this.typingManager.handleTypingStop(this.id,e.user_id)}emitPresence(e){this.emit("presence",{userId:e.user_id,state:e.state})}clearMessages(){this.messageManager.clearConversation(this.id)}clearTyping(){this.typingManager.clearConversation(this.id)}startCall(e="video"){this.ensureJoinedForCall(),this.callManager.startCall(e)}acceptCall(){this.ensureJoinedForCall(),this.callManager.acceptCall()}joinCall(e){this.ensureJoinedForCall(),this.callManager.joinCall(e!=null?e:"video")}rejectCall(e){this.ensureJoinedForCall(),this.callManager.rejectCall(e)}endCall(e){this.ensureJoinedForCall(),this.callManager.endCall(e)}setMuted(e){this.ensureJoinedForCall(),this.callManager.setMuted(e)}setVideoEnabled(e){this.ensureJoinedForCall(),this.callManager.setVideoEnabled(e)}setCallOptions(e){this.callManager.setCallOptions(e)}listDevices(){return this.callManager.listDevices()}applyVideoConstraints(e){return this.callManager.applyVideoConstraints(e)}applyAudioConstraints(e){return this.callManager.applyAudioConstraints(e)}switchDevice(e,t){return this.callManager.switchDevice(e,t)}setLowBandwidth(e){return this.callManager.setLowBandwidth(e)}startScreenShare(){return this.ensureJoinedForCall(),this.callManager.startScreenShare()}stopScreenShare(){return this.callManager.stopScreenShare()}isScreenSharing(){return this.callManager.isScreenSharing()}getLocalStream(){return this.callManager.getLocalStream()}getRemoteStream(e){return this.callManager.getRemoteStream(e)}getAllRemoteStreams(){return this.callManager.getAllRemoteStreams()}getCallStats(){return this.callManager.getCallStats()}handleIncomingCallMessage(e,t){switch(e){case"call_invite":this.callManager.handleCallInvite(t||{});break;case"call_accept":this.callManager.handleCallAccept(t);break;case"call_join":this.callManager.handleCallJoin(t||{});break;case"call_reject":this.callManager.handleCallReject(t);break;case"call_hangup":this.callManager.handleCallHangup(t);break;case"call_participant_left":this.callManager.handleCallParticipantLeft(t||{});break;case"call_error":this.callManager.handleCallError(t||{});break;case"call_offer":this.callManager.handleCallOffer(t||{});break;case"call_answer":this.callManager.handleCallAnswer(t||{});break;case"call_ice_candidate":this.callManager.handleCallICECandidate(t||{});break;case"call_mute":this.callManager.handleCallMute(t||{});break;case"call_unmute":this.callManager.handleCallUnmute(t||{});break;case"video_off":this.callManager.handleVideoOff(t||{});break;case"video_on":this.callManager.handleVideoOn(t||{});break}}ensureJoinedForCall(){if(!this._isJoined)throw new Error("Conversation is not joined. Call join() first.")}startTyping(){if(!this.sendTypingHandler){let t=new Error("Conversation is not connected. Call join() first.");throw this.emit("error",t),t}if(!this._isJoined){let t=new Error("Conversation is not joined. Call join() first.");throw this.emit("error",t),t}let e={type:"typing_start",conversation_id:this.id};try{this.sendTypingHandler(e)}catch(t){let s=t instanceof Error?t:new Error("Failed to send typing_start");throw this.emit("error",s),s}}stopTyping(){if(!this.sendTypingHandler){let t=new Error("Conversation is not connected. Call join() first.");throw this.emit("error",t),t}if(!this._isJoined){let t=new Error("Conversation is not joined. Call join() first.");throw this.emit("error",t),t}let e={type:"typing_stop",conversation_id:this.id};try{this.sendTypingHandler(e)}catch(t){let s=t instanceof Error?t:new Error("Failed to send typing_stop");throw this.emit("error",s),s}}markRead(e){if(!this.sendReadHandler){let s=new Error("Conversation is not connected. Call join() first.");throw this.emit("error",s),s}if(!this._isJoined){let s=new Error("Conversation is not joined. Call join() first.");throw this.emit("error",s),s}let t={type:"read",conversation_id:this.id,payload:{message_id:e}};try{this.sendReadHandler(t)}catch(s){let n=s instanceof Error?s:new Error("Failed to send read receipt");throw this.emit("error",n),n}}},ot=class extends k.default{constructor(e,t=!1,s,n,i){super(),this.conversations=new Map,this.restFetcher=null,this.appId=null,this.iceServersCache=null,this.sendMessageHandler=e,this.sendTypingHandler=n,this.sendCallHandler=i,this.supportMessageHandler=s,this.debug=t,this.logger=new M(t)}setRestFetcher(e){if(this.restFetcher=e,e)for(let t of this.conversations.values())t.setSubmitCallQuality((s,n,i)=>this.submitCallQualityToApi(s,n,i))}setAppId(e){this.appId=e&&e.trim()||null,this.iceServersCache=null}getIceServersFetcher(){if(!(!this.restFetcher||!this.appId))return async()=>{var r;if(this.iceServersCache)return this.iceServersCache;let t=`${this.restFetcher.apiUrl.replace(/\/$/,"")}/apps/${encodeURIComponent(this.appId)}/ice-servers`,s=await this.restFetcher.getHeaders(),n=await fetch(t,{method:"GET",headers:s});if(!n.ok)throw new Error(`ICE servers fetch failed: ${n.status}`);let i=await n.json();if(!i.success||!((r=i.data)!=null&&r.iceServers))throw new Error("Invalid ICE servers response");let a=i.data.iceServers;return this.iceServersCache=a,a}}setSupportMessageHandler(e){this.supportMessageHandler=e}setSendTypingHandler(e){this.sendTypingHandler=e;for(let t of this.conversations.values())t.setSendTypingHandler(e)}setSendCallHandler(e){this.sendCallHandler=e;for(let t of this.conversations.values())t.setSendCallHandler(e)}setSendReadHandler(e){this.sendReadHandler=e;for(let t of this.conversations.values())t.setSendReadHandler(e)}async list(e){var m,v,R;if(!this.restFetcher)throw new Error("REST not configured. Set apiUrl in Vocantly config for list() and fetchMessages() (token, API keys, or getRestHeaders).");let t=this.restFetcher.apiUrl.replace(/\/$/,""),s=(m=e==null?void 0:e.externalUserId)==null?void 0:m.trim(),n=Math.min(Math.max(1,(v=e==null?void 0:e.limit)!=null?v:20),100),i=new URLSearchParams({limit:String(n)});s&&i.set("external_user_id",s),e!=null&&e.cursor&&i.set("cursor",e.cursor);let a=i.toString(),r=`${t}/sdk/conversations${a?`?${a}`:""}`,l=await this.restFetcher.getHeaders(),c=await fetch(r,{method:"GET",headers:l});if(!c.ok){let j=await c.text(),A={};try{A=JSON.parse(j)}catch{A={message:j}}throw new Error(A.message||`HTTP ${c.status}: ${j}`)}let h=await c.json();if(!h.success||!h.data)throw new Error(h.message||"Invalid response");let f=h.data,d=Array.isArray(f.conversations)?f.conversations:[],y=(R=f.pagination)!=null?R:{has_next_page:!1,next_cursor:null,limit:n};return{conversations:d,pagination:y}}async getMessages(e,t,s={}){var d;if(!(e!=null&&e.trim()))throw new Error("conversationId is required");if(!this.restFetcher)throw new Error("REST not configured. Set apiUrl in Vocantly config for list() and fetchMessages().");let n=Math.min(Math.max(1,(d=s.limit)!=null?d:20),100),i=new URLSearchParams({limit:String(n)});t!=null&&t.trim()&&i.set("external_user_id",t.trim()),s.cursor&&i.set("cursor",s.cursor);let a=this.restFetcher.apiUrl.replace(/\/$/,""),r=i.toString(),l=`${a}/sdk/conversations/${encodeURIComponent(e.trim())}/messages${r?`?${r}`:""}`,c=await this.restFetcher.getHeaders(),h=await fetch(l,{method:"GET",headers:c});if(!h.ok){let y=await h.text(),m={};try{m=JSON.parse(y)}catch{m={message:y}}throw new Error(m.message||`HTTP ${h.status}: ${y}`)}let f=await h.json();if(!f.success||!f.data)throw new Error(f.message||"Invalid response");return{messages:f.data.messages||[],pagination:f.data.pagination||{has_next_page:!1,next_cursor:null,limit:n}}}async submitCallQualityToApi(e,t,s){if(!this.restFetcher)return;let i=`${this.restFetcher.apiUrl.replace(/\/$/,"")}/conversations/${encodeURIComponent(e)}/call-quality`,a=await this.restFetcher.getHeaders(),r={end_reason:s};typeof t.duration_seconds=="number"&&(r.duration_seconds=t.duration_seconds),typeof t.packets_lost_in=="number"&&(r.packets_lost_in=t.packets_lost_in),typeof t.packets_lost_out=="number"&&(r.packets_lost_out=t.packets_lost_out),typeof t.packets_received=="number"&&(r.packets_received=t.packets_received),typeof t.packets_sent=="number"&&(r.packets_sent=t.packets_sent),typeof t.bytes_received=="number"&&(r.bytes_received=t.bytes_received),typeof t.bytes_sent=="number"&&(r.bytes_sent=t.bytes_sent),typeof t.jitter_ms=="number"&&(r.jitter_ms=t.jitter_ms),typeof t.rtt_ms=="number"&&(r.rtt_ms=t.rtt_ms);let l=await fetch(i,{method:"POST",headers:{...a,"Content-Type":"application/json"},body:JSON.stringify(r)});if(!l.ok){let c=await l.text();throw this.logger.debug("submitCallQuality failed:",l.status,c),new Error(`Call quality submit failed: ${l.status}`)}}get(e){if(!e||e.length===0)throw new Error("Conversation ID is required");let t=this.conversations.get(e);return t||(t=new at(e,{debug:this.debug,getIceServers:this.getIceServersFetcher()}),this.sendMessageHandler&&t.setSendMessageHandler(this.sendMessageHandler),this.sendTypingHandler&&t.setSendTypingHandler(this.sendTypingHandler),this.sendCallHandler&&t.setSendCallHandler(this.sendCallHandler),this.sendReadHandler&&t.setSendReadHandler(this.sendReadHandler),this.conversations.set(e,t),this.logger.debug("Created conversation instance:",e)),this.restFetcher&&(t.setFetchMessagesCallback(s=>this.getMessages(e,s.externalUserId,{cursor:s.cursor,limit:s.limit})),t.setSubmitCallQuality((s,n,i)=>this.submitCallQualityToApi(s,n,i))),t}has(e){return this.conversations.has(e)}async join(e,t){if(!e||e.length===0)throw new Error("Conversation ID is required");this.get(e),await t(e),this.logger.debug("Join requested for conversation:",e)}async leave(e,t){if(!e||e.length===0)throw new Error("Conversation ID is required");let s=this.conversations.get(e);s?(s.clearTyping(),await t(e),s.markLeft(),this.logger.debug("Left conversation:",e)):(await t(e),this.logger.warn("Attempted to leave unknown conversation:",e))}routeIncomingMessage(e){var s;let t;switch(e.type){case"joined":t=((s=e.payload)==null?void 0:s.conversation_id)||e.conversation_id,t&&this.get(t).markJoined();break;case"message":if(t=e.conversation_id,t){let n=this.conversations.get(t);n?n.isJoined?n.handleIncomingMessage(e.payload):this.logger.warn("Received message for conversation that is not joined:",t):this.logger.warn("Received message for unknown conversation:",t)}break;case"typing":if(t=e.conversation_id,t){let n=this.conversations.get(t);n?n.isJoined?n.handleTyping(e.payload):this.logger.warn("Received typing indicator for conversation that is not joined:",t):this.logger.warn("Received typing indicator for unknown conversation:",t)}break;case"receipt":if(t=e.conversation_id,t){let n=this.conversations.get(t);if(n)if(n.isJoined){let i=e;n.handleReceipt(i.payload.message_id,i.payload.state,i.payload.user_id)}else this.logger.warn("Received receipt for conversation that is not joined:",t);else this.logger.warn("Received receipt for unknown conversation:",t)}break;case"support":break;case"presence":if(t=e.conversation_id,t){let n=this.conversations.get(t);n&&n.isJoined&&n.emitPresence(e.payload)}break;case"error":this.logger.error("Received error message:",e.error);break;case"call_invite":case"call_accept":case"call_join":case"call_reject":case"call_hangup":case"call_participant_left":case"call_error":case"call_offer":case"call_answer":case"call_ice_candidate":case"call_mute":case"call_unmute":case"video_off":case"video_on":if(t=e.conversation_id,t){let n=this.get(t),i="payload"in e?e.payload:void 0;n.handleIncomingCallMessage(e.type,i)}break}}routeSupportMessage(e){var n;if(!this.supportMessageHandler){this.logger.warn("Support message handler not set, ignoring support message");return}let t=e.conversation_id;if(!t){this.logger.warn("Support message missing conversation_id");return}let s=(n=e.payload)==null?void 0:n.event;if(!s){this.logger.warn("Support message missing event type");return}this.supportMessageHandler(t,s,e.payload)}remove(e){let t=this.conversations.get(e);t&&(t.markLeft(),t.clearMessages(),t.clearTyping(),this.conversations.delete(e),this.logger.debug("Removed conversation instance:",e))}getAllIds(){return Array.from(this.conversations.keys())}clear(){for(let e of this.conversations.values())e.markLeft(),e.clearMessages(),e.clearTyping();this.conversations.clear(),this.logger.debug("Cleared all conversations")}},ct=class extends k.default{constructor(e=!1){super(),this.presenceState=new Map,this.logger=new M(e)}handlePresence(e,t){if(!e){this.logger.warn("Invalid presence event: missing userId");return}if(t!=="online"&&t!=="offline"){this.logger.warn("Invalid presence state:",t);return}this.presenceState.get(e)!==t?(this.presenceState.set(e,t),this.emit("presence",{userId:e,state:t}),this.logger.debug("Presence changed:",e,t)):this.logger.debug("Duplicate presence event ignored:",e,t)}getPresence(e){return this.presenceState.get(e)}getPresences(e){let t=new Map;for(let s of e){let n=this.presenceState.get(s);n&&t.set(s,n)}return t}clearPresence(e){let t=this.presenceState.has(e);this.presenceState.delete(e),t&&this.logger.debug("Cleared presence for user:",e)}clear(){this.presenceState.clear(),this.logger.debug("Cleared all presence state")}},lt=class extends k.default{constructor(e,t,s,n=!1){super(),this.botActive=!1,this.status="queued",this.conversationId=e,this.conversation=t,this.role=s,this.logger=new M(n),this.conversation.on("message",i=>this.emit("message",i)),this.conversation.on("typing",i=>this.emit("typing",i)),this.conversation.on("presence",i=>this.emit("presence",i)),this.conversation.on("joined",i=>this.emit("joined",i)),this.conversation.on("left",i=>this.emit("left",i)),this.conversation.on("error",i=>this.emit("error",i))}getConversation(){return this.conversation}isCustomer(){return this.role==="customer"}isAgent(){return this.role==="agent"}getRole(){return this.role}isBotActive(){return this.botActive}getAssignedAgent(){return this.assignedAgentId}getStatus(){return this.status}handleAssigned(e){if(this.assignedAgentId===e)return;let t=this.assignedAgentId,s=t!==void 0;this.assignedAgentId=e,this.status==="queued"?this.status="assigned":this.status==="assigned"||this.status,s?(this.emit("assigned",{agentId:e,previousAgentId:t}),this.logger.debug("Support session reassigned:",this.conversationId,e,"from",t)):(this.emit("assigned",{agentId:e}),this.logger.debug("Support session assigned:",this.conversationId,e))}handleUnassigned(){if(this.assignedAgentId===void 0)return;let e=this.assignedAgentId;this.assignedAgentId=void 0,(this.status==="assigned"||this.status==="active")&&(this.status="queued"),this.emit("unassigned",{previousAgentId:e}),this.logger.debug("Support session unassigned:",this.conversationId)}handleBotTakeover(){this.botActive||(this.botActive=!0,this.emit("bot_takeover",{}),this.logger.debug("Bot took over support session:",this.conversationId))}handleBotReleased(){this.botActive&&(this.botActive=!1,this.emit("bot_released",{}),this.logger.debug("Bot released support session:",this.conversationId))}handleClosed(){if(this.status==="closed")return;let e=this.status;this.status="closed",this.emit("closed",{previousStatus:e}),this.logger.debug("Support session closed:",this.conversationId)}handleReopened(){this.status==="closed"&&(this.status=this.assignedAgentId?"assigned":"queued",this.emit("reopened",{}),this.logger.debug("Support session reopened:",this.conversationId))}sendMessage(e,t="text",s){this.conversation.sendMessage(e,t,s)}async join(){}async leave(){}},dt=class extends k.default{constructor(e,t=!1){super(),this.sessions=new Map,this.getConversationHandler=e,this.debug=t,this.logger=new M(t)}get(e,t="customer"){if(!e||e.length===0)throw new Error("Conversation ID is required");let s=this.sessions.get(e);if(!s){let n=this.getConversationHandler(e);s=new lt(e,n,t,this.debug),s.on("assigned",i=>this.emit("assigned",{conversationId:e,...i})),s.on("unassigned",i=>this.emit("unassigned",{conversationId:e,...i})),s.on("bot_takeover",()=>this.emit("bot_takeover",{conversationId:e})),s.on("bot_released",()=>this.emit("bot_released",{conversationId:e})),s.on("closed",i=>this.emit("closed",{conversationId:e,...i})),s.on("reopened",()=>this.emit("reopened",{conversationId:e})),this.sessions.set(e,s),this.logger.debug("Created support session:",e,t)}return s}has(e){return this.sessions.has(e)}getSession(e){return this.sessions.get(e)}handleSupportEvent(e,t,s){let n=this.sessions.get(e);if(!n){t==="assigned"&&(s!=null&&s.agent_id)?this.logger.debug("Support event for unknown session, will be handled when session is created:",e):this.logger.warn("Support event for unknown session:",e,t);return}switch(t){case"assigned":s!=null&&s.agent_id&&typeof s.agent_id=="string"&&n.handleAssigned(s.agent_id);break;case"unassigned":n.handleUnassigned();break;case"bot_takeover":n.handleBotTakeover();break;case"bot_released":n.handleBotReleased();break;case"closed":n.handleClosed();break;case"reopened":n.handleReopened();break}}remove(e){this.sessions.get(e)&&(this.sessions.delete(e),this.logger.debug("Removed support session:",e))}clear(){this.sessions.clear(),this.logger.debug("Cleared all support sessions")}},Re=class extends k.default{constructor(e){var a,r;super(),this.connectionState="disconnected",this.wsClient=null,this.config={autoReconnect:!0,maxReconnectAttempts:5,reconnectDelay:1e3,debug:!1,apiUrl:"",...e},this.tokenManager=new Ze,this.tokenManager.setToken(e.token),this.logger=new M(this.config.debug),this.wsClient=new Xe(this.config.wsUrl,{autoReconnect:this.config.autoReconnect,maxReconnectAttempts:this.config.maxReconnectAttempts,reconnectDelay:this.config.reconnectDelay,debug:this.config.debug}),this.supportManager=new dt(l=>this.conversationManager.get(l),this.config.debug),this.conversationManager=new ot(l=>{if(!this.wsClient)throw new Error("WebSocket client not initialized");this.wsClient.send(l)},this.config.debug,void 0,l=>{if(!this.wsClient)throw new Error("WebSocket client not initialized");this.wsClient.send(l)},l=>{if(!this.wsClient)throw new Error("WebSocket client not initialized");this.wsClient.send(l)}),this.conversationManager.setSendReadHandler(l=>{if(!this.wsClient)throw new Error("WebSocket client not initialized");this.wsClient.send(l)}),this.presenceManager=new ct(this.config.debug),this.presenceManager.on("presence",l=>{this.emit("presence",l)});let t=(a=this.config.apiUrl)==null?void 0:a.trim(),s=(r=this.config.appId)==null?void 0:r.trim(),n=this.config.apiKey&&this.config.apiSecret,i=typeof this.config.getRestHeaders=="function";if(t){let l=async()=>{if(i&&this.config.getRestHeaders)return this.config.getRestHeaders();if(n)return{"X-Api-Key":this.config.apiKey,"X-Api-Secret":this.config.apiSecret,"Content-Type":"application/json"};let c=this.tokenManager.getToken();if(!c)throw new Error("Token required for list() and fetchMessages() when API keys are not set");return{Authorization:`Bearer ${c}`,"Content-Type":"application/json"}};this.conversationManager.setRestFetcher({apiUrl:t,getHeaders:l}),s&&this.conversationManager.setAppId(s)}this.setupWebSocketHandlers()}setupWebSocketHandlers(){this.wsClient&&(this.wsClient.on("connected",()=>{this.connectionState="connected",this.emit("connected"),this.emit("state",this.connectionState),this.logger.debug("SDK connected")}),this.wsClient.on("disconnected",e=>{this.connectionState="disconnected",this.emit("disconnected",e),this.emit("state",this.connectionState),this.logger.debug("SDK disconnected")}),this.wsClient.on("reconnecting",e=>{this.connectionState="reconnecting",this.emit("reconnecting",e),this.emit("state",this.connectionState),this.logger.debug("SDK reconnecting",e)}),this.wsClient.on("message",e=>{if(this.emit("message",e),this.conversationManager.routeIncomingMessage(e),e.type==="presence"){let t=e;t.payload&&this.presenceManager.handlePresence(t.payload.user_id,t.payload.state)}switch(e.type==="support"&&this.conversationManager.routeSupportMessage(e),e.type){case"joined":this.emit("joined",e);break;case"message":this.emit("message",e);break;case"typing":this.emit("typing",e);break;case"receipt":break;case"support":break;case"presence":break;case"error":this.emit("error",e);break}}),this.wsClient.on("error",e=>{this.connectionState="error";let t=e instanceof Error?e.message:"";(e instanceof Error?e.name:"")==="TokenExpiredError"||t&&t.includes("Token expired")?(this.logger.error("Token expired:",t),this.logger.warn("To reconnect, call sdk.updateToken(newToken) then sdk.connect()")):this.logger.error("SDK error:",e);let n=e instanceof Error?e:new Error("WebSocket connection error");this.emit("error",n),this.emit("state",this.connectionState)}))}getState(){return this.connectionState}getConfig(){return{...this.config}}async connect(){if(this.connectionState==="connected"){this.logger.warn("Already connected");return}if(this.connectionState==="connecting"){this.logger.warn("Connection already in progress");return}this.tokenManager.validateTokenPresence();let e=this.tokenManager.getToken();if(!e)throw new Error("Token is required for connection");this.connectionState="connecting",this.emit("state",this.connectionState),this.logger.debug("Connecting...");try{if(!this.wsClient)throw new Error("WebSocket client not initialized");await this.wsClient.connect(e)}catch(t){this.connectionState="error",this.emit("state",this.connectionState);let s=t instanceof Error?t:new Error("Connection failed");throw this.emit("error",s),s}}async disconnect(){this.connectionState!=="disconnected"&&(this.logger.debug("Disconnecting..."),this.conversationManager.clear(),this.presenceManager.clear(),this.supportManager.clear(),this.wsClient&&this.wsClient.disconnect(),this.connectionState="disconnected",this.emit("disconnected"),this.emit("state",this.connectionState))}isConnected(){var e;return this.connectionState==="connected"&&((e=this.wsClient)==null?void 0:e.isConnected())===!0}updateToken(e){if(!e||e.length===0)throw new Error("Token cannot be empty");this.tokenManager.setToken(e),this.logger.debug("Token updated"),this.isConnected()&&(this.logger.warn("Token updated while connected. Disconnecting to reconnect with new token..."),this.disconnect())}get conversations(){return{get:e=>this.conversationManager.get(e),has:e=>this.conversationManager.has(e),join:e=>this.conversationManager.join(e,t=>this.joinConversation(t)),leave:e=>this.conversationManager.leave(e,t=>this.leaveConversation(t)),list:e=>this.conversationManager.list(e)}}get support(){return{get:(e,t="customer")=>this.supportManager.get(e,t),has:e=>this.supportManager.has(e)}}async joinConversation(e){if(!this.isConnected())throw new Error("Must be connected before joining a conversation");if(!e||e.length===0)throw new Error("Conversation ID is required");if(!this.wsClient)throw new Error("WebSocket client not initialized");let t={type:"join",conversation_id:e};try{this.wsClient.send(t),this.logger.debug("Join conversation sent:",e)}catch(s){let n=s instanceof Error?s:new Error("Failed to join conversation");throw this.emit("error",n),n}}async leaveConversation(e){if(!this.isConnected())throw new Error("Must be connected before leaving a conversation");if(!e||e.length===0)throw new Error("Conversation ID is required");if(!this.wsClient)throw new Error("WebSocket client not initialized");let t={type:"leave",conversation_id:e};try{this.wsClient.send(t),this.logger.debug("Leave conversation sent:",e)}catch(s){let n=s instanceof Error?s:new Error("Failed to leave conversation");throw this.emit("error",n),n}}rotateToken(e){this.tokenManager.rotateToken(e),this.logger.debug("Token rotated"),this.isConnected()&&this.logger.warn("Token rotated while connected. Reconnect may be required.")}getTokenManager(){return this.tokenManager}};var gt={wsUrl:"",token:"",conversationId:"",publicKey:"",title:"Support",subtitle:"We typically reply in a few minutes",greeting:"Hi! \u{1F44B} How can we help you today?",placeholder:"Type your message\u2026",primaryColor:"#195EFF",position:"bottom-right",launcher:"\u{1F4AC}",autoOpen:!1,debug:!1,collectContact:!1,requireEmail:!1,contactPrompt:"Before we start \u2014 how can we reach you?",csat:!0,csatPrompt:"How would you rate the support you received?",csatThanks:"Thanks for the feedback!",calls:!1,callBundleUrl:"https://cdn.jsdelivr.net/npm/@vocantly/widget@0.1.10/dist/vocantly-widget-call.js"},T=null;function Ae(e){try{let t=JSON.parse(atob(e.split(".")[1]));return t.user_id||t.sub||null}catch{return null}}function E(e){return String(e).replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""")}function Oe(e){return E(e).replace(/\*\*([^*]+)\*\*/g,"<strong>$1</strong>").replace(/(^|[^*])\*([^*\n]+)\*(?!\*)/g,"$1<em>$2</em>").replace(/`([^`]+)`/g,"<code>$1</code>").replace(/\n/g,"<br>")}var W=null;function ut(){try{let e=window.AudioContext||window.webkitAudioContext;if(!e)return;W=W||new e,W.resume&&W.resume();let t=W,s=t.currentTime,n=t.createOscillator(),i=t.createGain();n.type="sine",n.frequency.setValueAtTime(880,s),n.frequency.exponentialRampToValueAtTime(1320,s+.08),i.gain.setValueAtTime(1e-4,s),i.gain.exponentialRampToValueAtTime(.16,s+.02),i.gain.exponentialRampToValueAtTime(1e-4,s+.34),n.connect(i).connect(t.destination),n.start(s),n.stop(s+.36)}catch{}}function ft(e,t){if(document.getElementById("caf-widget-styles"))return;let s=t==="bottom-left"?"left:20px":"right:20px",n=`
|
|
2
|
+
/* Reset so the host page's button/box styles can't bleed into the widget (WordPress
|
|
3
|
+
themes add padding + min-width, which was stretching the round buttons into ovals). */
|
|
4
|
+
.caf-root,.caf-root *{box-sizing:border-box;}
|
|
5
|
+
.caf-root button{margin:0;padding:0;min-width:0;min-height:0;line-height:normal;text-transform:none;letter-spacing:normal;appearance:none;-webkit-appearance:none;}
|
|
6
|
+
.caf-root{position:fixed;bottom:20px;${s};z-index:2147483000;font:14px/1.5 -apple-system,Segoe UI,Roboto,Helvetica,Arial,sans-serif;}
|
|
7
|
+
.caf-launcher{width:56px !important;height:56px !important;padding:0 !important;border-radius:50% !important;background:${e};color:#fff;border:none;cursor:pointer;
|
|
8
|
+
box-shadow:0 6px 20px rgba(0,0,0,.18);display:flex;align-items:center;justify-content:center;transition:transform .15s;flex:none;}
|
|
9
|
+
.caf-launcher svg{width:26px;height:26px;}
|
|
10
|
+
.caf-launcher:hover{transform:scale(1.06);}
|
|
11
|
+
.caf-callbtn{width:34px !important;height:34px !important;padding:0 !important;border-radius:50% !important;background:rgba(255,255,255,.18);border:0;color:#fff;cursor:pointer;
|
|
12
|
+
display:none;align-items:center;justify-content:center;flex:none;}
|
|
13
|
+
.caf-callbtn:hover{background:rgba(255,255,255,.3);}
|
|
14
|
+
.caf-callbtn svg{width:17px;height:17px;}
|
|
15
|
+
.caf-badge{position:absolute;top:-2px;right:-2px;min-width:18px;height:18px;padding:0 5px;border-radius:9px;background:#EF4444;color:#fff;
|
|
16
|
+
font-size:11px;font-weight:700;display:flex;align-items:center;justify-content:center;box-shadow:0 0 0 2px #fff;}
|
|
17
|
+
.caf-panel{position:absolute;bottom:72px;${s};width:370px;max-width:calc(100vw - 40px);height:560px;max-height:calc(100vh - 120px);
|
|
18
|
+
background:#fff;border-radius:16px;box-shadow:0 12px 40px rgba(0,0,0,.22);display:none;flex-direction:column;overflow:hidden;}
|
|
19
|
+
.caf-panel.open{display:flex;}
|
|
20
|
+
.caf-header{background:${e};color:#fff;padding:16px 18px;display:flex;align-items:center;gap:10px;}
|
|
21
|
+
.caf-header .caf-t{font-weight:700;font-size:15px;}
|
|
22
|
+
.caf-header .caf-s{font-size:12px;opacity:.85;}
|
|
23
|
+
.caf-dot{width:8px;height:8px;border-radius:50%;background:#9CA3AF;flex:none;}
|
|
24
|
+
.caf-dot.on{background:#34D399;} .caf-dot.err{background:#F87171;}
|
|
25
|
+
.caf-x{margin-left:auto;background:transparent;border:none;color:#fff;font-size:18px;cursor:pointer;opacity:.85;}
|
|
26
|
+
.caf-x:hover{opacity:1;}
|
|
27
|
+
.caf-msgs{flex:1;overflow-y:auto;padding:16px;background:#F7F8FA;display:flex;flex-direction:column;gap:8px;}
|
|
28
|
+
.caf-m{max-width:82%;padding:9px 12px;border-radius:14px;white-space:normal;word-wrap:break-word;}
|
|
29
|
+
.caf-m code{background:rgba(0,0,0,.06);padding:1px 4px;border-radius:4px;font-size:12px;}
|
|
30
|
+
.caf-self{align-self:flex-end;background:${e};color:#fff;border-bottom-right-radius:4px;}
|
|
31
|
+
.caf-other{align-self:flex-start;background:#fff;color:#111827;border:1px solid #E5E7EB;border-bottom-left-radius:4px;}
|
|
32
|
+
.caf-sys{align-self:center;background:#FEF3C7;color:#92400E;font-size:12px;text-align:center;max-width:92%;}
|
|
33
|
+
.caf-note{align-self:center;background:transparent;color:#6B7280;font-size:12px;line-height:1.45;text-align:center;max-width:92%;padding:2px 8px;}
|
|
34
|
+
.caf-csat{align-self:center;text-align:center;color:#6B7280;font-size:12px;padding:2px 8px 6px;}
|
|
35
|
+
.caf-csat-row{display:flex;gap:4px;justify-content:center;margin-top:6px;}
|
|
36
|
+
.caf-csat-b{border:1px solid #E5E7EB;background:#fff;border-radius:8px;padding:3px 7px;font-size:16px;line-height:1.2;cursor:pointer;}
|
|
37
|
+
.caf-csat-b:hover{border-color:${e};background:#F9FAFB;}
|
|
38
|
+
.caf-composer{border-top:1px solid #E5E7EB;padding:10px;display:flex;gap:8px;align-items:flex-end;background:#fff;}
|
|
39
|
+
.caf-input{flex:1;resize:none;max-height:96px;border:1px solid #E5E7EB;border-radius:10px;padding:9px 11px;font:inherit;outline:none;}
|
|
40
|
+
.caf-input:focus{border-color:${e};}
|
|
41
|
+
.caf-send{width:40px !important;height:40px !important;padding:0 !important;flex:none;background:${e};color:#fff;border:none;border-radius:50% !important;
|
|
42
|
+
display:flex;align-items:center;justify-content:center;cursor:pointer;transition:transform .12s ease,opacity .12s ease;box-shadow:0 2px 6px rgba(0,0,0,.12);}
|
|
43
|
+
.caf-send svg{width:18px;height:18px;transform:translateX(1px);}
|
|
44
|
+
.caf-send:hover{transform:scale(1.06);}
|
|
45
|
+
.caf-send:active{transform:scale(.96);}
|
|
46
|
+
.caf-send:disabled{opacity:.4;cursor:not-allowed;transform:none;box-shadow:none;}
|
|
47
|
+
.caf-foot{text-align:center;font-size:10px;color:#9CA3AF;padding:6px;background:#fff;}
|
|
48
|
+
.caf-form{background:#fff;border:1px solid #E5E7EB;border-radius:12px;padding:14px;margin:2px 0;display:flex;flex-direction:column;gap:8px;}
|
|
49
|
+
.caf-form-t{font-weight:600;font-size:13px;color:#111827;}
|
|
50
|
+
.caf-fi{border:1px solid #E5E7EB;border-radius:10px;padding:10px 12px;font:inherit;font-size:13px;outline:none;transition:border-color .12s ease;}
|
|
51
|
+
.caf-fi:focus{border-color:${e};}
|
|
52
|
+
.caf-ferr{color:#DC2626;font-size:12px;min-height:0;}
|
|
53
|
+
.caf-fbtn{width:100%;background:${e};color:#fff;border:none;border-radius:10px;padding:12px 14px;font-size:14px;font-weight:600;cursor:pointer;
|
|
54
|
+
margin-top:2px;transition:opacity .12s ease,transform .12s ease;box-shadow:0 2px 8px ${e}33;}
|
|
55
|
+
.caf-fbtn:hover{opacity:.94;}
|
|
56
|
+
.caf-fbtn:active{transform:scale(.99);}
|
|
57
|
+
.caf-fskip{align-self:center;background:transparent;border:none;color:#9CA3AF;font-size:12.5px;cursor:pointer;padding:4px 8px;margin-top:-2px;}
|
|
58
|
+
.caf-fskip:hover{color:#4B5563;text-decoration:underline;text-underline-offset:2px;}
|
|
59
|
+
`,i=document.createElement("style");i.id="caf-widget-styles",i.textContent=n,document.head.appendChild(i)}function pt(e){let t=document.createElement("div");return t.className="caf-root",t.innerHTML=`
|
|
60
|
+
<div class="caf-panel" role="dialog" aria-label="${E(e.title)}">
|
|
61
|
+
<div class="caf-header">
|
|
62
|
+
<span class="caf-dot" data-caf="dot"></span>
|
|
63
|
+
<div style="flex:1;min-width:0"><div class="caf-t">${E(e.title)}</div><div class="caf-s">${E(e.subtitle)}</div></div>
|
|
64
|
+
<button class="caf-callbtn" data-caf="call" title="Start a voice call" aria-label="Start a voice call">
|
|
65
|
+
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M22 16.92v3a2 2 0 0 1-2.18 2 19.79 19.79 0 0 1-8.63-3.07 19.5 19.5 0 0 1-6-6 19.79 19.79 0 0 1-3.07-8.67A2 2 0 0 1 4.11 2h3a2 2 0 0 1 2 1.72c.13.96.36 1.9.7 2.81a2 2 0 0 1-.45 2.11L8.09 9.91a16 16 0 0 0 6 6l1.27-1.27a2 2 0 0 1 2.11-.45c.9.34 1.85.57 2.81.7A2 2 0 0 1 22 16.92z"/></svg>
|
|
66
|
+
</button>
|
|
67
|
+
<button class="caf-x" data-caf="close" aria-label="Close">\u2715</button>
|
|
68
|
+
</div>
|
|
69
|
+
<div class="caf-msgs" data-caf="msgs"></div>
|
|
70
|
+
<div class="caf-composer">
|
|
71
|
+
<textarea class="caf-input" data-caf="input" rows="1" placeholder="${E(e.placeholder)}"></textarea>
|
|
72
|
+
<button class="caf-send" data-caf="send" aria-label="Send" title="Send"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M22 2 11 13"/><path d="M22 2 15 22l-4-9-9-4 20-7z"/></svg></button>
|
|
73
|
+
</div>
|
|
74
|
+
<div class="caf-foot">powered by Vocantly</div>
|
|
75
|
+
</div>
|
|
76
|
+
<button class="caf-launcher" data-caf="launcher" aria-label="Open chat">
|
|
77
|
+
<span data-caf="launcher-icon">${e.launcher==="\u{1F4AC}"?'<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 11.5a8.38 8.38 0 0 1-.9 3.8 8.5 8.5 0 0 1-7.6 4.7 8.38 8.38 0 0 1-3.8-.9L3 21l1.9-5.7a8.38 8.38 0 0 1-.9-3.8 8.5 8.5 0 0 1 4.7-7.6 8.38 8.38 0 0 1 3.8-.9h.5a8.48 8.48 0 0 1 8 8v.5z"/></svg>':E(e.launcher)}</span>
|
|
78
|
+
<span class="caf-badge" data-caf="badge" style="display:none">0</span>
|
|
79
|
+
</button>`,document.body.appendChild(t),{root:t,$:n=>t.querySelector(`[data-caf="${n}"]`)}}function mt(e){if(T)return T.open(),T.api;let t={...gt,...e||{}};if(!t.wsUrl)throw new Error("VocantlyWidget.init requires wsUrl");if(!t.publicKey&&!(t.token&&t.conversationId))throw new Error("VocantlyWidget.init requires either { publicKey } (anonymous) or { token, conversationId }");if(t.publicKey&&!t.apiUrl)throw new Error("VocantlyWidget.init with publicKey also requires apiUrl");ft(t.primaryColor,t.position);let{root:s,$:n}=pt(t),i={panel:s.querySelector(".caf-panel"),msgs:n("msgs"),input:n("input"),send:n("send"),dot:n("dot"),badge:n("badge"),launcher:n("launcher"),call:n("call")},a=null,r=!1,l=()=>t.calls===!0||r;function c(){a&&H&&i.call&&(i.call.style.display="flex")}function h(){return new Promise((o,u)=>{if(window.VocantlyWidgetCall)return o();let g=document.createElement("script");g.src=t.callBundleUrl,g.async=!0,g.onload=()=>o(),g.onerror=()=>u(new Error("call bundle load failed")),document.head.appendChild(g)})}async function f(){if(!(a||!O))try{if(await h(),!window.VocantlyWidgetCall)return;let o=document.createElement("div");document.body.appendChild(o),a=window.VocantlyWidgetCall.mount(o,O,{logoText:"CA",sessionTitle:t.title||"Call",sessionSubtitle:t.subtitle||"Secured via Vocantly",primaryColor:t.primaryColor,self:{name:"You",role:"Visitor"},peer:{name:t.title||"Support",role:"Agent"},voiceOnly:!0,simple:!0}),i.call&&(i.call.onclick=()=>a.startCall("voice")),c()}catch(o){t.debug&&console.error("[caf] call setup failed",o)}}let d=t.token||"",y=t.conversationId||"",m=d?Ae(d):null,v=null,R=new Set,j=!1,A=!1,oe=0,ce=!1,H=!t.collectContact,le=!1,D=o=>{i.dot.className="caf-dot"+(o?" "+o:"")},Y=()=>{i.msgs.scrollTop=i.msgs.scrollHeight},de=o=>{oe=o,i.badge.textContent=String(o),i.badge.style.display=o>0?"flex":"none"};function X(o,u){let g=document.createElement("div");g.className="caf-m "+o,g.innerHTML=u,i.msgs.appendChild(g),Y()}function he(o){var I,_,J,we,ve,be;if(o.optimistic)return;let u=(I=o.id)!=null?I:o.message_id,g=(J=(_=o.senderId)!=null?_:o.sender_id)!=null?J:"",p=(ve=(we=o.content)!=null?we:o.text)!=null?ve:"",w=u||`${g}:${p}`;if(R.has(w))return;if(R.add(w),((be=o.messageType)!=null?be:o.message_type)==="system"){X("caf-note",Oe(p)),t.csat&&Ie();return}let x=m&&g===m;X(x?"caf-self":"caf-other",Oe(p)),!x&&j&&ut(),!x&&!A&&de(oe+1)}let ge=!1;function Ie(){if(ge||!t.apiUrl||!y)return;ge=!0;let o=document.createElement("div");o.className="caf-csat",o.innerHTML=`<div>${E(t.csatPrompt)}</div><div class="caf-csat-row"></div>`;let u=o.querySelector(".caf-csat-row");[["1","\u{1F61E}"],["2","\u{1F641}"],["3","\u{1F610}"],["4","\u{1F642}"],["5","\u{1F600}"]].forEach(([g,p])=>{let w=document.createElement("button");w.className="caf-csat-b",w.type="button",w.textContent=p,w.setAttribute("aria-label",g+" out of 5"),w.onclick=()=>je(Number(g),o),u.appendChild(w)}),i.msgs.appendChild(o),Y()}async function je(o,u){u.innerHTML=`<div>${E(t.csatThanks)}</div>`,Y();try{await fetch(t.apiUrl.replace(/\/$/,"")+"/sdk/conversations/"+y+"/rating",{method:"POST",headers:{"Content-Type":"application/json",Authorization:"Bearer "+d},body:JSON.stringify({rating:o})})}catch(g){t.debug&&console.error("[caf] csat failed",g)}}function ue(o){i.input.disabled=!o,i.send.disabled=!o,i.input.placeholder=o?t.placeholder:"\u2026"}function fe(){!ce&&t.greeting&&(X("caf-sys",E(t.greeting)),ce=!0)}async function He(o,u){if(t.apiUrl)try{if(await B(),!d)return;let g=await fetch(t.apiUrl.replace(/\/$/,"")+"/sdk/customers/me",{method:"PATCH",headers:{"Content-Type":"application/json",Authorization:"Bearer "+d},body:JSON.stringify({name:o||void 0,email:u||void 0})});!g.ok&&t.debug&&console.error("[caf] saveContact HTTP "+g.status)}catch(g){t.debug&&console.error("[caf] saveContact failed",g)}}function De(){le=!0;let o=document.createElement("div");o.className="caf-form",o.innerHTML=`
|
|
80
|
+
<div class="caf-form-t">${E(t.contactPrompt)}</div>
|
|
81
|
+
<input class="caf-fi" data-caf="f-name" placeholder="Your name" />
|
|
82
|
+
<input class="caf-fi" data-caf="f-email" type="email" placeholder="Email${t.requireEmail?"":" (optional)"}" />
|
|
83
|
+
<div class="caf-ferr" data-caf="f-err"></div>
|
|
84
|
+
<button class="caf-fbtn" data-caf="f-go">Start chat</button>
|
|
85
|
+
${t.requireEmail?"":'<button class="caf-fskip" data-caf="f-skip">Skip</button>'}`,i.msgs.appendChild(o);let u=x=>o.querySelector(`[data-caf="${x}"]`),g=/^[^\s@]+@[^\s@]+\.[^\s@]+$/,p=async x=>{let I=u("f-name").value.trim(),_=u("f-email").value.trim();if(x){if(t.requireEmail&&!_){u("f-err").textContent="Email is required.";return}if(_&&!g.test(_)){u("f-err").textContent="Enter a valid email.";return}await He(I,_)}o.remove(),H=!0,c(),ue(!0),fe(),setTimeout(()=>i.input.focus(),30)};u("f-go").onclick=()=>p(!0);let w=u("f-skip");w&&(w.onclick=()=>p(!1)),setTimeout(()=>u("f-name").focus(),40)}function N(){if(A=!0,i.panel.classList.add("open"),de(0),B(),!H&&!le){De();return}H&&(fe(),setTimeout(()=>i.input.focus(),50))}function q(){A=!1,i.panel.classList.remove("open")}i.launcher.onclick=()=>A?q():N(),n("close").onclick=q;let O=null;function Z(){if(!H)return;let o=i.input.value.trim();if(!(!o||!O))try{O.sendMessage(o),i.input.value="",i.input.style.height="auto"}catch(u){t.debug&&console.error("[caf] send failed",u)}}i.send.onclick=Z,ue(H),i.input.addEventListener("input",()=>{i.input.style.height="auto",i.input.style.height=Math.min(i.input.scrollHeight,96)+"px"}),i.input.addEventListener("keydown",o=>{o.key==="Enter"&&!o.shiftKey&&(o.preventDefault(),Z())});let ee=t.publicKey?"caf_session_"+t.publicKey.slice(-10):"",Ue=t.publicKey?"caf_vid_"+t.publicKey.slice(-10):"";function pe(){if(!ee)return null;try{let o=JSON.parse(localStorage.getItem(ee)||"null");if(o&&o.token&&o.conversationId&&o.exp>Date.now())return o}catch{}return null}async function Le(){let o=t.apiUrl.replace(/\/$/,""),u=ee,g=Ue,p=pe();if(p)return r=!!p.calls_enabled,{token:p.token,conversationId:p.conversationId};let w=null;try{w=localStorage.getItem(g)}catch{}if(!w){w="v_"+Math.random().toString(36).slice(2)+Date.now().toString(36);try{localStorage.setItem(g,w)}catch{}}let x=await fetch(o+"/sdk/anonymous-session",{method:"POST",headers:{"Content-Type":"application/json","X-Api-Key":t.publicKey},body:JSON.stringify({visitor_id:w})});if(!x.ok)throw new Error("anonymous-session HTTP "+x.status);let I=await x.json(),_=I.data||I;r=!!_.calls_enabled;let J=Date.now()+Math.max(0,(_.expires_in||3600)*1e3-6e4);try{localStorage.setItem(u,JSON.stringify({token:_.token,conversationId:_.conversation_id,exp:J,calls_enabled:r}))}catch{}return{token:_.token,conversationId:_.conversation_id}}let L=null;function B(){return L||(L=(async()=>{var o,u,g;try{if(D(""),t.publicKey){let p=await Le();d=p.token,y=p.conversationId,m=Ae(d)}if(v=new Re({wsUrl:t.wsUrl,token:d,apiUrl:t.apiUrl,autoReconnect:!0,debug:t.debug}),v.on("connected",()=>D("on")),v.on("disconnected",()=>D("")),v.on("reconnecting",()=>D("")),v.on("error",()=>D("err")),await v.connect(),await v.conversations.join(y),O=v.conversations.get(y),O.on("message",he),t.apiUrl&&typeof O.fetchMessages=="function")try{let p=await O.fetchMessages({limit:30});((g=(u=p==null?void 0:p.messages)!=null?u:(o=p==null?void 0:p.data)==null?void 0:o.messages)!=null?g:[]).forEach(he)}catch{}j=!0,l()&&f()}catch(p){D("err"),L=null,t.debug&&console.error("[caf] connect/provision failed",p)}})(),L)}(!t.publicKey||pe())&&B(),t.autoOpen&&N();let me={open:N,close:q,send:async o=>{await B(),i.input.value=o,Z()},destroy:()=>{try{v==null||v.disconnect()}catch{}s.remove(),T=null}};return T={open:N,close:q,api:me},me}function wt(){T==null||T.open()}function vt(){T==null||T.close()}function bt(){T==null||T.api.destroy()}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// Type definitions for @vocantly/widget
|
|
2
|
+
|
|
3
|
+
export interface VocantlyWidgetOptions {
|
|
4
|
+
/** Realtime WebSocket URL, e.g. "wss://ws.vocantly.com/ws". */
|
|
5
|
+
wsUrl: string;
|
|
6
|
+
|
|
7
|
+
// Provisioning — supply EITHER (token + conversationId) OR publicKey.
|
|
8
|
+
/** SDK token for this visitor (minted by your backend via POST /apps/:id/support-sessions). */
|
|
9
|
+
token?: string;
|
|
10
|
+
/** Support conversation id (returned alongside the token). */
|
|
11
|
+
conversationId?: string;
|
|
12
|
+
/**
|
|
13
|
+
* App PUBLIC key (pk_…). Anonymous mode: the widget self-provisions a session via
|
|
14
|
+
* POST /sdk/anonymous-session — no backend needed. Requires `apiUrl`, and your site's origin
|
|
15
|
+
* must be in the app's allowed_origins.
|
|
16
|
+
*/
|
|
17
|
+
publicKey?: string;
|
|
18
|
+
|
|
19
|
+
/** Backend REST base, e.g. "https://api.vocantly.com/api/v1". Required for publicKey mode; enables history + contact capture. */
|
|
20
|
+
apiUrl?: string;
|
|
21
|
+
|
|
22
|
+
title?: string;
|
|
23
|
+
subtitle?: string;
|
|
24
|
+
/** Local greeting bubble shown when the panel opens. */
|
|
25
|
+
greeting?: string;
|
|
26
|
+
placeholder?: string;
|
|
27
|
+
/** Accent color (hex). Default "#195EFF". */
|
|
28
|
+
primaryColor?: string;
|
|
29
|
+
position?: 'bottom-right' | 'bottom-left';
|
|
30
|
+
/** Launcher glyph/emoji. Default "💬". */
|
|
31
|
+
launcher?: string;
|
|
32
|
+
/** Open the panel automatically on init. */
|
|
33
|
+
autoOpen?: boolean;
|
|
34
|
+
debug?: boolean;
|
|
35
|
+
|
|
36
|
+
/** Ask the visitor for name + email before chatting (saved via PATCH /sdk/customers/me). Needs apiUrl. */
|
|
37
|
+
collectContact?: boolean;
|
|
38
|
+
/** When collectContact is on, require an email to start. */
|
|
39
|
+
requireEmail?: boolean;
|
|
40
|
+
contactPrompt?: string;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Ask the visitor to rate the support (1–5) once an agent closes the conversation.
|
|
44
|
+
* Defaults to true. Needs apiUrl.
|
|
45
|
+
*/
|
|
46
|
+
csat?: boolean;
|
|
47
|
+
csatPrompt?: string;
|
|
48
|
+
csatThanks?: string;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface VocantlyWidgetInstance {
|
|
52
|
+
open(): void;
|
|
53
|
+
close(): void;
|
|
54
|
+
send(text: string): void;
|
|
55
|
+
destroy(): void;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Mount + connect the widget. Returns a handle to control it. */
|
|
59
|
+
export function init(opts: VocantlyWidgetOptions): VocantlyWidgetInstance;
|
|
60
|
+
/** Open the panel (no-op if not initialised). */
|
|
61
|
+
export function open(): void;
|
|
62
|
+
/** Close the panel. */
|
|
63
|
+
export function close(): void;
|
|
64
|
+
/** Disconnect and remove the widget from the DOM. */
|
|
65
|
+
export function destroy(): void;
|
|
66
|
+
|
|
67
|
+
declare const _default: {
|
|
68
|
+
init: typeof init;
|
|
69
|
+
open: typeof open;
|
|
70
|
+
close: typeof close;
|
|
71
|
+
destroy: typeof destroy;
|
|
72
|
+
};
|
|
73
|
+
export default _default;
|