@opencx/widget-core 5.6.1 → 5.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +5 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +318 -306
- package/dist/index.js.map +1 -1
- package/dist/src/api/api-caller.d.ts +13 -0
- package/dist/src/api/api-caller.d.ts.map +1 -1
- package/dist/src/api/schema.d.ts +65 -0
- package/dist/src/api/schema.d.ts.map +1 -1
- package/dist/src/context/session.ctx.d.ts.map +1 -1
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.d.ts.map +1 -1
- package/package.json +8 -3
package/dist/index.js
CHANGED
|
@@ -12,24 +12,24 @@ const F = "[opencx]", b = {
|
|
|
12
12
|
};
|
|
13
13
|
class z {
|
|
14
14
|
constructor(e) {
|
|
15
|
-
this.subscribers = /* @__PURE__ */ new Set(), this.get = () => this.state, this.set = (
|
|
16
|
-
oe(this.state,
|
|
17
|
-
}, this.setPartial = (
|
|
18
|
-
if (
|
|
19
|
-
const o = { ...this.state, ...
|
|
15
|
+
this.subscribers = /* @__PURE__ */ new Set(), this.get = () => this.state, this.set = (s) => {
|
|
16
|
+
oe(this.state, s) || (this.state = s, this.notifySubscribers(s));
|
|
17
|
+
}, this.setPartial = (s) => {
|
|
18
|
+
if (s == null) return;
|
|
19
|
+
const o = { ...this.state, ...s };
|
|
20
20
|
this.set(o);
|
|
21
21
|
}, this.reset = () => {
|
|
22
22
|
this.set(this.initialState);
|
|
23
|
-
}, this.notifySubscribers = (
|
|
23
|
+
}, this.notifySubscribers = (s) => {
|
|
24
24
|
Array.from(this.subscribers).forEach((r) => {
|
|
25
25
|
try {
|
|
26
|
-
r(
|
|
26
|
+
r(s);
|
|
27
27
|
} catch (_) {
|
|
28
28
|
b.error("state subscriber threw", _);
|
|
29
29
|
}
|
|
30
30
|
});
|
|
31
|
-
}, this.subscribe = (
|
|
32
|
-
this.subscribers.delete(
|
|
31
|
+
}, this.subscribe = (s) => (this.subscribers.add(s), () => {
|
|
32
|
+
this.subscribers.delete(s);
|
|
33
33
|
}), this.state = e, this.initialState = e;
|
|
34
34
|
}
|
|
35
35
|
}
|
|
@@ -37,7 +37,7 @@ const ie = 5e3;
|
|
|
37
37
|
class re {
|
|
38
38
|
constructor({
|
|
39
39
|
config: e,
|
|
40
|
-
api:
|
|
40
|
+
api: s,
|
|
41
41
|
sessionsState: o,
|
|
42
42
|
setSessions: r
|
|
43
43
|
}) {
|
|
@@ -46,47 +46,47 @@ class re {
|
|
|
46
46
|
}, this.setViewingSessionId = (_) => {
|
|
47
47
|
this.viewingSessionId = _, this.evaluate(this.sessionsState.get().data);
|
|
48
48
|
}, this.evaluate = (_) => {
|
|
49
|
-
const
|
|
49
|
+
const t = [];
|
|
50
50
|
for (const a of _) {
|
|
51
51
|
if (a.id === this.viewingSessionId) {
|
|
52
52
|
(a.unread === !0 || a.unread === !1 && !this.reportedSessionIds.has(a.id)) && this.markViewedSessionRead(a.id);
|
|
53
53
|
continue;
|
|
54
54
|
}
|
|
55
|
-
a.unread === !0 &&
|
|
55
|
+
a.unread === !0 && t.push(a.id);
|
|
56
56
|
}
|
|
57
|
-
this.state.set({ unreadSessionIds:
|
|
57
|
+
this.state.set({ unreadSessionIds: t, count: t.length }), this.notifyCount(t.length);
|
|
58
58
|
}, this.markViewedSessionRead = (_) => {
|
|
59
59
|
if (this.readsInFlight.has(_)) return;
|
|
60
60
|
this.readsInFlight.add(_);
|
|
61
|
-
let
|
|
61
|
+
let t = !1;
|
|
62
62
|
this.api.markSessionRead({ sessionId: _ }).then(({ data: a }) => {
|
|
63
63
|
if (!a || this.disposed) return;
|
|
64
64
|
this.reportedSessionIds.add(_);
|
|
65
65
|
const n = this.sessionsState.get().data.find((c) => c.id === a.id);
|
|
66
|
-
n && _e(n, a) ?
|
|
66
|
+
n && _e(n, a) ? t = !0 : this.setSessions([a]);
|
|
67
67
|
}).catch((a) => {
|
|
68
68
|
b.warn("failed to mark the session read", {
|
|
69
69
|
sessionId: _,
|
|
70
70
|
error: a instanceof Error ? a.message : String(a)
|
|
71
71
|
}), this.scheduleRetry();
|
|
72
72
|
}).finally(() => {
|
|
73
|
-
this.readsInFlight.delete(_),
|
|
73
|
+
this.readsInFlight.delete(_), t && !this.disposed && this.evaluate(this.sessionsState.get().data);
|
|
74
74
|
});
|
|
75
75
|
}, this.scheduleRetry = () => {
|
|
76
76
|
this.retryTimer || this.disposed || (this.retryTimer = setTimeout(() => {
|
|
77
77
|
this.retryTimer = null, this.evaluate(this.sessionsState.get().data);
|
|
78
78
|
}, ie));
|
|
79
79
|
}, this.notifyCount = (_) => {
|
|
80
|
-
var
|
|
80
|
+
var t, a;
|
|
81
81
|
if (_ !== this.lastNotifiedCount) {
|
|
82
82
|
this.lastNotifiedCount = _;
|
|
83
83
|
try {
|
|
84
|
-
(a = (
|
|
84
|
+
(a = (t = this.config.hooks) == null ? void 0 : t.onUnreadCountChange) == null || a.call(t, _);
|
|
85
85
|
} catch (n) {
|
|
86
86
|
b.error("onUnreadCountChange hook failed", n);
|
|
87
87
|
}
|
|
88
88
|
}
|
|
89
|
-
}, this.config = e, this.api =
|
|
89
|
+
}, this.config = e, this.api = s, this.sessionsState = o, this.setSessions = r, this.subscriptions.push(
|
|
90
90
|
o.subscribe(({ data: _ }) => this.evaluate(_))
|
|
91
91
|
);
|
|
92
92
|
}
|
|
@@ -140,12 +140,12 @@ const Xa = (i) => i.type === "data-keepalive", et = (i) => i.type === "data-turn
|
|
|
140
140
|
}, ue = (i) => {
|
|
141
141
|
const e = ne({
|
|
142
142
|
baseUrl: i.baseUrl
|
|
143
|
-
}),
|
|
143
|
+
}), s = {
|
|
144
144
|
onRequest: i.onRequest,
|
|
145
145
|
onResponse: i.onResponse,
|
|
146
146
|
onError: i.onError || de
|
|
147
147
|
};
|
|
148
|
-
return e.use(
|
|
148
|
+
return e.use(s), e;
|
|
149
149
|
};
|
|
150
150
|
class me extends Error {
|
|
151
151
|
constructor() {
|
|
@@ -157,51 +157,51 @@ class he extends Error {
|
|
|
157
157
|
super("This connection attempt is no longer available."), this.name = "ConnectionAttemptUnavailableError";
|
|
158
158
|
}
|
|
159
159
|
}
|
|
160
|
-
const ge = "/backend/widget/v5/chat/stream", ke = "/backend/widget/v5/chat/{sessionId}/stream", B = (i, e,
|
|
160
|
+
const ge = "/backend/widget/v5/chat/stream", ke = "/backend/widget/v5/chat/{sessionId}/stream", B = (i, e, s) => `${i.replace(/\/$/, "")}${s ? e.replace("{sessionId}", encodeURIComponent(s)) : e}`;
|
|
161
161
|
class H {
|
|
162
162
|
constructor({ config: e }) {
|
|
163
163
|
var r, _;
|
|
164
|
-
this.userToken = null, this.constructClientOptions = (
|
|
164
|
+
this.userToken = null, this.constructClientOptions = (t) => {
|
|
165
165
|
const a = this.config.apiUrl || "https://api.open.cx", n = {
|
|
166
166
|
"X-Bot-Token": this.config.token,
|
|
167
167
|
"Content-Type": "application/json",
|
|
168
168
|
Accept: "application/json",
|
|
169
|
-
Authorization:
|
|
169
|
+
Authorization: t ? `Bearer ${t}` : void 0
|
|
170
170
|
};
|
|
171
171
|
return { baseUrl: a, headers: n };
|
|
172
172
|
}, this.createOpenAPIClient = ({
|
|
173
|
-
baseUrl:
|
|
173
|
+
baseUrl: t,
|
|
174
174
|
headers: a
|
|
175
175
|
}) => ue({
|
|
176
|
-
baseUrl:
|
|
176
|
+
baseUrl: t,
|
|
177
177
|
onRequest: ({ request: n }) => {
|
|
178
178
|
Object.entries(a).forEach(([c, d]) => {
|
|
179
179
|
d && n.headers.set(c, d);
|
|
180
180
|
});
|
|
181
181
|
}
|
|
182
182
|
}), this.listApprovalPreferences = async () => {
|
|
183
|
-
const { baseUrl:
|
|
184
|
-
`${
|
|
183
|
+
const { baseUrl: t, headers: a } = this.getStreamAuthContext(), n = await fetch(
|
|
184
|
+
`${t}/backend/widget/v5/connections/approval-preferences`,
|
|
185
185
|
{ headers: a }
|
|
186
186
|
);
|
|
187
187
|
if (!n.ok) throw new Error("Could not load permissions.");
|
|
188
188
|
return pe.parse(await n.json());
|
|
189
|
-
}, this.revokeApprovalPreference = async (
|
|
189
|
+
}, this.revokeApprovalPreference = async (t, a) => {
|
|
190
190
|
const { baseUrl: n, headers: c } = this.getStreamAuthContext();
|
|
191
191
|
if (!(await fetch(
|
|
192
|
-
`${n}/backend/widget/v5/connections/${encodeURIComponent(
|
|
192
|
+
`${n}/backend/widget/v5/connections/${encodeURIComponent(t)}/approval-preferences/${encodeURIComponent(a)}`,
|
|
193
193
|
{ method: "DELETE", headers: c }
|
|
194
194
|
)).ok) throw new Error("Could not reset permission.");
|
|
195
|
-
}, this.listElicitations = async (
|
|
195
|
+
}, this.listElicitations = async (t, a) => {
|
|
196
196
|
const { baseUrl: n, headers: c } = this.getStreamAuthContext(), d = await fetch(
|
|
197
|
-
`${n}/backend/widget/v5/connections/elicitation/${encodeURIComponent(
|
|
197
|
+
`${n}/backend/widget/v5/connections/elicitation/${encodeURIComponent(t)}`,
|
|
198
198
|
{ headers: c, signal: a }
|
|
199
199
|
);
|
|
200
200
|
if (!d.ok) throw new Error("Could not load the request.");
|
|
201
201
|
return ce.parse(await d.json());
|
|
202
|
-
}, this.answerElicitation = async (
|
|
202
|
+
}, this.answerElicitation = async (t, a, n) => {
|
|
203
203
|
const { baseUrl: c, headers: d } = this.getStreamAuthContext(), l = await fetch(
|
|
204
|
-
`${c}/backend/widget/v5/connections/elicitation/${encodeURIComponent(
|
|
204
|
+
`${c}/backend/widget/v5/connections/elicitation/${encodeURIComponent(t)}/${encodeURIComponent(a)}`,
|
|
205
205
|
{
|
|
206
206
|
method: "POST",
|
|
207
207
|
headers: { ...d, "Content-Type": "application/json" },
|
|
@@ -212,19 +212,19 @@ class H {
|
|
|
212
212
|
throw new Error(
|
|
213
213
|
l.status === 400 ? "Check your answers and try again." : "This request has ended. Ask the agent to try again."
|
|
214
214
|
);
|
|
215
|
-
}, this.listConnections = async (
|
|
215
|
+
}, this.listConnections = async (t) => {
|
|
216
216
|
const { data: a, error: n } = await this.client.GET(
|
|
217
217
|
"/backend/widget/v5/connections",
|
|
218
|
-
{ signal:
|
|
218
|
+
{ signal: t }
|
|
219
219
|
);
|
|
220
220
|
if (n || !a)
|
|
221
221
|
throw new Error("Could not load connections. Sign in and try again.");
|
|
222
222
|
return a;
|
|
223
|
-
}, this.startConnection = async (
|
|
223
|
+
}, this.startConnection = async (t, a, n) => {
|
|
224
224
|
const { data: c, error: d, response: l } = await this.client.POST(
|
|
225
225
|
"/backend/widget/v5/connections/{serverId}/start",
|
|
226
226
|
{
|
|
227
|
-
params: { path: { serverId:
|
|
227
|
+
params: { path: { serverId: t } },
|
|
228
228
|
body: { request_id: a },
|
|
229
229
|
signal: n
|
|
230
230
|
}
|
|
@@ -235,11 +235,11 @@ class H {
|
|
|
235
235
|
if (d || !c)
|
|
236
236
|
throw new Error("Could not start this connection. Please try again.");
|
|
237
237
|
return c;
|
|
238
|
-
}, this.getConnectionAttempt = async (
|
|
238
|
+
}, this.getConnectionAttempt = async (t, a, n) => {
|
|
239
239
|
const { data: c, error: d, response: l } = await this.client.GET(
|
|
240
240
|
"/backend/widget/v5/connections/{serverId}/attempts/{attemptId}",
|
|
241
241
|
{
|
|
242
|
-
params: { path: { serverId:
|
|
242
|
+
params: { path: { serverId: t, attemptId: a } },
|
|
243
243
|
signal: n
|
|
244
244
|
}
|
|
245
245
|
);
|
|
@@ -248,51 +248,63 @@ class H {
|
|
|
248
248
|
if (d || !c)
|
|
249
249
|
throw new Error(`Could not check the connection (${l.status}).`);
|
|
250
250
|
return c.status;
|
|
251
|
-
}, this.disconnectConnection = async (
|
|
251
|
+
}, this.disconnectConnection = async (t) => {
|
|
252
252
|
const { error: a } = await this.client.DELETE(
|
|
253
253
|
"/backend/widget/v5/connections/{serverId}",
|
|
254
254
|
{
|
|
255
|
-
params: { path: { serverId:
|
|
255
|
+
params: { path: { serverId: t } }
|
|
256
256
|
}
|
|
257
257
|
);
|
|
258
258
|
if (a) throw new Error("Could not disconnect. Please try again.");
|
|
259
|
-
}, this.setAuthToken = (
|
|
260
|
-
this.userToken =
|
|
261
|
-
const { baseUrl: a, headers: n } = this.constructClientOptions(
|
|
259
|
+
}, this.setAuthToken = (t) => {
|
|
260
|
+
this.userToken = t;
|
|
261
|
+
const { baseUrl: a, headers: n } = this.constructClientOptions(t);
|
|
262
262
|
this.client = this.createOpenAPIClient({ baseUrl: a, headers: n });
|
|
263
263
|
}, this.getStreamAuthContext = () => {
|
|
264
|
-
const { baseUrl:
|
|
264
|
+
const { baseUrl: t, headers: a } = this.constructClientOptions(this.userToken), n = {};
|
|
265
265
|
for (const [c, d] of Object.entries(a))
|
|
266
266
|
typeof d == "string" && (["content-type", "accept"].includes(c.toLowerCase()) || (n[c] = d));
|
|
267
|
-
return { baseUrl:
|
|
267
|
+
return { baseUrl: t, headers: n };
|
|
268
268
|
}, this.getStreamTransportOptions = () => {
|
|
269
|
-
const { baseUrl:
|
|
269
|
+
const { baseUrl: t, headers: a } = this.getStreamAuthContext();
|
|
270
270
|
return {
|
|
271
|
-
api: B(
|
|
272
|
-
reconnectApi: (n) => B(
|
|
271
|
+
api: B(t, ge),
|
|
272
|
+
reconnectApi: (n) => B(t, ke, n),
|
|
273
273
|
headers: a
|
|
274
274
|
};
|
|
275
|
-
}, this.stopStream = async (
|
|
275
|
+
}, this.stopStream = async (t) => {
|
|
276
276
|
const { response: a } = await this.client.POST(
|
|
277
277
|
"/backend/widget/v5/chat/{sessionId}/stop",
|
|
278
|
-
{ params: { path: { sessionId:
|
|
278
|
+
{ params: { path: { sessionId: t } } }
|
|
279
279
|
);
|
|
280
280
|
if (!a.ok)
|
|
281
281
|
throw new Error(`Failed to stop stream: ${a.status}`);
|
|
282
|
-
}, this.sendPageReply = async (
|
|
282
|
+
}, this.sendPageReply = async (t, a) => {
|
|
283
283
|
try {
|
|
284
284
|
await this.client.POST("/backend/widget/v5/chat/{sessionId}/page-reply", {
|
|
285
|
-
params: { path: { sessionId:
|
|
285
|
+
params: { path: { sessionId: t } },
|
|
286
286
|
body: a
|
|
287
287
|
});
|
|
288
288
|
} catch {
|
|
289
289
|
}
|
|
290
|
-
}, this.
|
|
290
|
+
}, this.reportClientStreamError = async (t, a) => {
|
|
291
|
+
try {
|
|
292
|
+
await this.client.POST(
|
|
293
|
+
"/backend/widget/v5/chat/{sessionId}/client-error",
|
|
294
|
+
{
|
|
295
|
+
params: { path: { sessionId: t } },
|
|
296
|
+
body: a
|
|
297
|
+
}
|
|
298
|
+
);
|
|
299
|
+
} catch (n) {
|
|
300
|
+
b.warn("stream error report failed", n);
|
|
301
|
+
}
|
|
302
|
+
}, this.getAgentTurnMessages = async (t, a) => {
|
|
291
303
|
const { data: n, response: c } = await this.client.GET(
|
|
292
304
|
"/backend/widget/v5/chat/{sessionId}/messages",
|
|
293
305
|
{
|
|
294
306
|
params: {
|
|
295
|
-
path: { sessionId:
|
|
307
|
+
path: { sessionId: t },
|
|
296
308
|
query: {
|
|
297
309
|
toolActivity: a == null ? void 0 : a.toolActivity,
|
|
298
310
|
reasoning: (a == null ? void 0 : a.reasoning) === void 0 ? void 0 : a.reasoning ? "true" : "false"
|
|
@@ -305,11 +317,11 @@ class H {
|
|
|
305
317
|
handled_connection_request_ids: n.handled_connection_request_ids ?? []
|
|
306
318
|
} : (b.warn("agent turn messages fetch failed", c.status), null);
|
|
307
319
|
}, this.createDictationSession = async ({
|
|
308
|
-
language:
|
|
320
|
+
language: t
|
|
309
321
|
}) => {
|
|
310
322
|
const { data: a, response: n } = await this.client.POST(
|
|
311
323
|
"/backend/widget/v5/dictation/sessions",
|
|
312
|
-
{ body:
|
|
324
|
+
{ body: t ? { language: t } : {} }
|
|
313
325
|
);
|
|
314
326
|
if (!a)
|
|
315
327
|
throw new Error(`Failed to start dictation: ${n.status}`);
|
|
@@ -318,53 +330,53 @@ class H {
|
|
|
318
330
|
params: {
|
|
319
331
|
header: { "x-bot-token": this.config.token }
|
|
320
332
|
}
|
|
321
|
-
}), this.sendMessage = async (
|
|
322
|
-
body:
|
|
333
|
+
}), this.sendMessage = async (t, a) => await this.client.POST("/backend/widget/v2/chat/send", {
|
|
334
|
+
body: t,
|
|
323
335
|
signal: a
|
|
324
|
-
}), this.createUnverifiedContact = async (
|
|
336
|
+
}), this.createUnverifiedContact = async (t) => await this.client.POST(
|
|
325
337
|
"/backend/widget/v2/contact/create-unverified",
|
|
326
338
|
{
|
|
327
339
|
params: { header: { "x-bot-token": this.config.token } },
|
|
328
|
-
body:
|
|
340
|
+
body: t
|
|
329
341
|
}
|
|
330
|
-
), this.createSession = async (
|
|
331
|
-
body:
|
|
342
|
+
), this.createSession = async (t, a) => await this.client.POST("/backend/widget/v2/create-session", {
|
|
343
|
+
body: t,
|
|
332
344
|
signal: a
|
|
333
345
|
}), this.pollSessionAndHistory = async ({
|
|
334
|
-
sessionId:
|
|
346
|
+
sessionId: t,
|
|
335
347
|
abortSignal: a
|
|
336
348
|
}) => await this.client.GET("/backend/widget/v2/poll/{sessionId}", {
|
|
337
|
-
params: { path: { sessionId:
|
|
349
|
+
params: { path: { sessionId: t } },
|
|
338
350
|
signal: a
|
|
339
351
|
}), this.markSessionRead = async ({
|
|
340
|
-
sessionId:
|
|
352
|
+
sessionId: t,
|
|
341
353
|
abortSignal: a
|
|
342
354
|
}) => await this.client.POST(
|
|
343
355
|
"/backend/widget/v2/session/{sessionId}/read",
|
|
344
356
|
{
|
|
345
|
-
params: { path: { sessionId:
|
|
357
|
+
params: { path: { sessionId: t } },
|
|
346
358
|
signal: a
|
|
347
359
|
}
|
|
348
360
|
), this.getSessions = async ({
|
|
349
|
-
cursor:
|
|
361
|
+
cursor: t,
|
|
350
362
|
filters: a,
|
|
351
363
|
abortSignal: n
|
|
352
364
|
}) => await this.client.GET("/backend/widget/v2/sessions", {
|
|
353
365
|
params: {
|
|
354
366
|
query: {
|
|
355
|
-
offset:
|
|
367
|
+
offset: t,
|
|
356
368
|
filters: JSON.stringify(a)
|
|
357
369
|
}
|
|
358
370
|
},
|
|
359
371
|
signal: n
|
|
360
372
|
}), this.uploadFile = async ({
|
|
361
|
-
file:
|
|
373
|
+
file: t,
|
|
362
374
|
abortSignal: a,
|
|
363
375
|
onProgress: n
|
|
364
376
|
}) => new Promise((c, d) => {
|
|
365
377
|
var y;
|
|
366
378
|
const l = new FormData();
|
|
367
|
-
l.append("file",
|
|
379
|
+
l.append("file", t);
|
|
368
380
|
const p = new XMLHttpRequest();
|
|
369
381
|
if (a && (a.addEventListener("abort", () => {
|
|
370
382
|
p.abort(), d(new DOMException("Aborted", "AbortError"));
|
|
@@ -394,16 +406,16 @@ class H {
|
|
|
394
406
|
});
|
|
395
407
|
const { baseUrl: u } = this.constructClientOptions(this.userToken), g = `${u}/backend/widget/v2/upload`;
|
|
396
408
|
p.open("POST", g), p.setRequestHeader("X-Bot-Token", this.config.token), this.userToken ?? ((y = this.config.user) == null ? void 0 : y.token) ? p.setRequestHeader("Authorization", `Bearer ${this.userToken}`) : b.error("user token not set"), p.send(l);
|
|
397
|
-
}), this.vote = async (
|
|
398
|
-
body:
|
|
409
|
+
}), this.vote = async (t) => await this.client.POST("/backend/widget/v2/chat/vote", { body: t }), this.resolveSession = async (t, a) => await this.client.POST("/backend/widget/v2/session/resolve", {
|
|
410
|
+
body: t,
|
|
399
411
|
signal: a
|
|
400
|
-
}), this.createStateCheckpoint = async (
|
|
401
|
-
body:
|
|
402
|
-
}), this.submitCsat = async (
|
|
403
|
-
const { baseUrl:
|
|
412
|
+
}), this.createStateCheckpoint = async (t) => await this.client.POST("/backend/widget/v2/checkpoint", {
|
|
413
|
+
body: t
|
|
414
|
+
}), this.submitCsat = async (t) => await this.client.POST("/backend/widget/v2/submit-csat", { body: t }), this.config = e, this.userToken = ((r = e.user) == null ? void 0 : r.token) || null;
|
|
415
|
+
const { baseUrl: s, headers: o } = this.constructClientOptions(
|
|
404
416
|
(_ = e.user) == null ? void 0 : _.token
|
|
405
417
|
);
|
|
406
|
-
this.client = this.createOpenAPIClient({ baseUrl:
|
|
418
|
+
this.client = this.createOpenAPIClient({ baseUrl: s, headers: o });
|
|
407
419
|
}
|
|
408
420
|
}
|
|
409
421
|
function be(i, e) {
|
|
@@ -417,7 +429,7 @@ class J {
|
|
|
417
429
|
}), this.abortController = new AbortController(), this.reset = () => {
|
|
418
430
|
var e;
|
|
419
431
|
this.abortController.abort("Resetting poller"), (e = this.stopPolling) == null || e.call(this), this.stopPolling = null;
|
|
420
|
-
}, this.stopPolling = null, this.startPolling = (e,
|
|
432
|
+
}, this.stopPolling = null, this.startPolling = (e, s) => {
|
|
421
433
|
if (this.stopPolling) return;
|
|
422
434
|
const o = [], r = async () => {
|
|
423
435
|
this.abortController = new AbortController(), this.state.setPartial({ isPolling: !0 });
|
|
@@ -430,7 +442,7 @@ class J {
|
|
|
430
442
|
} finally {
|
|
431
443
|
this.state.setPartial({ isPolling: !1 });
|
|
432
444
|
}
|
|
433
|
-
this.abortController.signal.aborted || o.push(setTimeout(r,
|
|
445
|
+
this.abortController.signal.aborted || o.push(setTimeout(r, s));
|
|
434
446
|
};
|
|
435
447
|
r(), this.stopPolling = () => {
|
|
436
448
|
o.forEach(clearTimeout), this.state.reset();
|
|
@@ -441,7 +453,7 @@ class J {
|
|
|
441
453
|
function Y(i) {
|
|
442
454
|
try {
|
|
443
455
|
const e = i();
|
|
444
|
-
return e instanceof Promise ? e.then((
|
|
456
|
+
return e instanceof Promise ? e.then((s) => ({ data: s })).catch((s) => ({ error: s })) : { data: e };
|
|
445
457
|
} catch (e) {
|
|
446
458
|
return { error: e };
|
|
447
459
|
}
|
|
@@ -449,31 +461,31 @@ function Y(i) {
|
|
|
449
461
|
class fe {
|
|
450
462
|
constructor({
|
|
451
463
|
api: e,
|
|
452
|
-
config:
|
|
464
|
+
config: s,
|
|
453
465
|
sessionCtx: o,
|
|
454
466
|
messageCtx: r,
|
|
455
467
|
sessionPollingIntervalSeconds: _
|
|
456
468
|
}) {
|
|
457
469
|
this.poller = new J(), this.fetchSessionAndFullHistoryAbortController = new AbortController(), this.reconcileAbortController = new AbortController(), this.subscriptions = [], this.reconcilingSessionId = null, this.seenHistoryIds = /* @__PURE__ */ new Set(), this.removedHistoryIds = /* @__PURE__ */ new Set(), this.protectedUntilSeq = /* @__PURE__ */ new Map(), this.landedPollSessionId = null, this.pollSeq = 0, this.lastAppliedSeq = 0, this.removableAfterSeq = 0, this.registerPolling = () => {
|
|
458
470
|
this.subscriptions.push(
|
|
459
|
-
this.sessionCtx.sessionState.subscribe(({ session:
|
|
460
|
-
((
|
|
471
|
+
this.sessionCtx.sessionState.subscribe(({ session: t }) => {
|
|
472
|
+
((t == null ? void 0 : t.id) ?? null) !== this.landedPollSessionId && (this.seenHistoryIds = /* @__PURE__ */ new Set(), this.removedHistoryIds = /* @__PURE__ */ new Set(), this.protectedUntilSeq = /* @__PURE__ */ new Map(), this.removableAfterSeq = 0, this.landedPollSessionId = null);
|
|
461
473
|
})
|
|
462
474
|
), this.subscriptions.push(
|
|
463
|
-
this.sessionCtx.sessionState.subscribe(({ session:
|
|
464
|
-
|
|
475
|
+
this.sessionCtx.sessionState.subscribe(({ session: t }) => {
|
|
476
|
+
t != null && t.id ? this.poller.startPolling(async (a) => {
|
|
465
477
|
this.fetchSessionAndHistory({
|
|
466
|
-
sessionId:
|
|
478
|
+
sessionId: t.id,
|
|
467
479
|
abortSignal: a
|
|
468
480
|
});
|
|
469
481
|
}, this.sessionPollingIntervalSeconds * 1e3) : this.poller.reset();
|
|
470
482
|
})
|
|
471
483
|
), this.subscriptions.push(
|
|
472
|
-
this.sessionCtx.sessionState.subscribe(({ session:
|
|
473
|
-
if (
|
|
484
|
+
this.sessionCtx.sessionState.subscribe(({ session: t }) => {
|
|
485
|
+
if (t != null && t.id && !t.isOpened)
|
|
474
486
|
try {
|
|
475
487
|
this.fetchSessionAndFullHistoryAbortController = new AbortController(), this.fetchSessionAndHistory({
|
|
476
|
-
sessionId:
|
|
488
|
+
sessionId: t.id,
|
|
477
489
|
abortSignal: this.fetchSessionAndFullHistoryAbortController.signal
|
|
478
490
|
});
|
|
479
491
|
} catch (a) {
|
|
@@ -483,36 +495,36 @@ class fe {
|
|
|
483
495
|
this.fetchSessionAndFullHistoryAbortController.abort();
|
|
484
496
|
})
|
|
485
497
|
), this.subscriptions.push(
|
|
486
|
-
this.sessionCtx.sessionState.subscribe(({ session:
|
|
487
|
-
this.reconcilingSessionId !== null && (
|
|
498
|
+
this.sessionCtx.sessionState.subscribe(({ session: t }) => {
|
|
499
|
+
this.reconcilingSessionId !== null && (t == null ? void 0 : t.id) !== this.reconcilingSessionId && this.reconcileAbortController.abort();
|
|
488
500
|
})
|
|
489
501
|
);
|
|
490
502
|
}, this.dispose = () => {
|
|
491
|
-
this.poller.reset(), this.fetchSessionAndFullHistoryAbortController.abort(), this.reconcileAbortController.abort(), this.reconcilingSessionId = null, this.subscriptions.forEach((
|
|
492
|
-
}, this.reconcileAfterStream = async (
|
|
503
|
+
this.poller.reset(), this.fetchSessionAndFullHistoryAbortController.abort(), this.reconcileAbortController.abort(), this.reconcilingSessionId = null, this.subscriptions.forEach((t) => t()), this.subscriptions = [];
|
|
504
|
+
}, this.reconcileAfterStream = async (t) => {
|
|
493
505
|
this.reconcileAbortController.abort(), this.reconcileAbortController = new AbortController();
|
|
494
506
|
const { signal: a } = this.reconcileAbortController;
|
|
495
|
-
this.reconcilingSessionId =
|
|
507
|
+
this.reconcilingSessionId = t;
|
|
496
508
|
try {
|
|
497
|
-
await this.fetchSessionAndHistory({ sessionId:
|
|
509
|
+
await this.fetchSessionAndHistory({ sessionId: t, abortSignal: a });
|
|
498
510
|
} catch (n) {
|
|
499
511
|
if (!a.aborted) throw n;
|
|
500
512
|
} finally {
|
|
501
|
-
this.reconcilingSessionId ===
|
|
513
|
+
this.reconcilingSessionId === t && (this.reconcilingSessionId = null);
|
|
502
514
|
}
|
|
503
515
|
}, this.fetchSessionAndHistory = async ({
|
|
504
|
-
sessionId:
|
|
516
|
+
sessionId: t,
|
|
505
517
|
abortSignal: a
|
|
506
518
|
}) => {
|
|
507
|
-
const n = this.landedPollSessionId !==
|
|
519
|
+
const n = this.landedPollSessionId !== t && this.messageCtx.state.get().messages.length === 0;
|
|
508
520
|
n && this.messageCtx.state.setPartial({ isInitialFetchLoading: !0 });
|
|
509
521
|
const c = ++this.pollSeq, { data: d } = await this.api.pollSessionAndHistory({
|
|
510
|
-
sessionId:
|
|
522
|
+
sessionId: t,
|
|
511
523
|
abortSignal: a
|
|
512
524
|
});
|
|
513
525
|
if (d != null && d.session && (this.sessionCtx.sessionState.setPartial({ session: d.session }), this.sessionCtx.setSessions([d.session])), d != null && d.history) {
|
|
514
526
|
const l = c < this.lastAppliedSeq, p = !l && c > this.removableAfterSeq;
|
|
515
|
-
l || (this.lastAppliedSeq = c), p && (this.removableAfterSeq = this.pollSeq), this.landedPollSessionId =
|
|
527
|
+
l || (this.lastAppliedSeq = c), p && (this.removableAfterSeq = this.pollSeq), this.landedPollSessionId = t, d.history.length > 0 && this.reconcileOptionPicks(d.history);
|
|
516
528
|
const u = new Set(d.history.map((k) => k.publicId)), m = p ? Array.from(this.seenHistoryIds).filter((k) => !u.has(k)) : [], g = new Set(
|
|
517
529
|
m.filter((k) => (this.protectedUntilSeq.get(k) ?? 0) < c)
|
|
518
530
|
);
|
|
@@ -540,50 +552,50 @@ class fe {
|
|
|
540
552
|
this.messageCtx.dispatchToOnMessageReceivedHook(k);
|
|
541
553
|
}
|
|
542
554
|
this.messageCtx.state.get().isInitialFetchLoading && this.messageCtx.state.setPartial({ isInitialFetchLoading: !1 });
|
|
543
|
-
}, this.reconcileOptionPicks = (
|
|
555
|
+
}, this.reconcileOptionPicks = (t) => {
|
|
544
556
|
var a;
|
|
545
|
-
for (const n of
|
|
557
|
+
for (const n of t) {
|
|
546
558
|
const c = (a = n.messageOptions) == null ? void 0 : a.pickedOptionId;
|
|
547
559
|
c && this.messageCtx.setMessageOptionPick(n.publicId, c);
|
|
548
560
|
}
|
|
549
|
-
}, this.mapHistoryToMessage = (
|
|
561
|
+
}, this.mapHistoryToMessage = (t) => {
|
|
550
562
|
var n;
|
|
551
563
|
const a = {
|
|
552
|
-
id:
|
|
553
|
-
timestamp:
|
|
554
|
-
attachments:
|
|
564
|
+
id: t.publicId,
|
|
565
|
+
timestamp: t.sentAt || "",
|
|
566
|
+
attachments: t.attachments || void 0
|
|
555
567
|
};
|
|
556
|
-
if (
|
|
568
|
+
if (t.sender.kind === "user")
|
|
557
569
|
return {
|
|
558
570
|
...a,
|
|
559
571
|
type: "USER",
|
|
560
572
|
deliveredAt: a.timestamp,
|
|
561
|
-
content:
|
|
573
|
+
content: t.content.text || "",
|
|
562
574
|
// Backend field name (schema.ts) → widget vocabulary.
|
|
563
|
-
markedElements:
|
|
575
|
+
markedElements: t.pickedElements
|
|
564
576
|
};
|
|
565
|
-
if (
|
|
577
|
+
if (t.sender.kind === "agent")
|
|
566
578
|
return {
|
|
567
579
|
...a,
|
|
568
580
|
type: "AGENT",
|
|
569
581
|
component: "agent_message",
|
|
570
582
|
data: {
|
|
571
|
-
message:
|
|
583
|
+
message: t.content.text || ""
|
|
572
584
|
},
|
|
573
585
|
agent: {
|
|
574
|
-
name:
|
|
586
|
+
name: t.sender.name || "",
|
|
575
587
|
// Do not set avatarUrl from config here... let it be taken from the config at render time
|
|
576
588
|
// Only set avatar url from the backend's response
|
|
577
|
-
avatarUrl:
|
|
578
|
-
avatar:
|
|
589
|
+
avatarUrl: t.sender.avatar || null,
|
|
590
|
+
avatar: t.sender.avatar || null,
|
|
579
591
|
id: null,
|
|
580
592
|
isAi: !1
|
|
581
593
|
},
|
|
582
|
-
...this.constructReplyTo(
|
|
583
|
-
...this.constructMessageOptions(
|
|
594
|
+
...this.constructReplyTo(t),
|
|
595
|
+
...this.constructMessageOptions(t)
|
|
584
596
|
};
|
|
585
|
-
if (
|
|
586
|
-
const c =
|
|
597
|
+
if (t.sender.kind === "ai") {
|
|
598
|
+
const c = t.actionCalls && t.actionCalls.length > 0 ? t.actionCalls[t.actionCalls.length - 1] : void 0;
|
|
587
599
|
return {
|
|
588
600
|
...a,
|
|
589
601
|
type: "AI",
|
|
@@ -597,25 +609,25 @@ class fe {
|
|
|
597
609
|
avatar: null
|
|
598
610
|
},
|
|
599
611
|
data: {
|
|
600
|
-
message:
|
|
612
|
+
message: t.content.text || "",
|
|
601
613
|
action: c ? {
|
|
602
614
|
name: c.actionName,
|
|
603
615
|
data: this.extractActionResult(c)
|
|
604
616
|
} : void 0
|
|
605
617
|
},
|
|
606
|
-
...this.constructReplyTo(
|
|
618
|
+
...this.constructReplyTo(t),
|
|
607
619
|
// Workflow messages (a sent macro) arrive as AI and carry options too.
|
|
608
|
-
...this.constructMessageOptions(
|
|
620
|
+
...this.constructMessageOptions(t)
|
|
609
621
|
};
|
|
610
622
|
}
|
|
611
|
-
if (
|
|
612
|
-
const c = this.constructSystemMessage(
|
|
623
|
+
if (t.sender.kind === "system") {
|
|
624
|
+
const c = this.constructSystemMessage(t);
|
|
613
625
|
return c === null ? null : { ...c };
|
|
614
626
|
}
|
|
615
627
|
return null;
|
|
616
|
-
}, this.constructReplyTo = (
|
|
617
|
-
if (!
|
|
618
|
-
const { publicId: a, text: n, sender: c } =
|
|
628
|
+
}, this.constructReplyTo = (t) => {
|
|
629
|
+
if (!t.replyTo) return {};
|
|
630
|
+
const { publicId: a, text: n, sender: c } = t.replyTo;
|
|
619
631
|
return {
|
|
620
632
|
replyTo: {
|
|
621
633
|
id: a,
|
|
@@ -623,64 +635,64 @@ class fe {
|
|
|
623
635
|
sender: c
|
|
624
636
|
}
|
|
625
637
|
};
|
|
626
|
-
}, this.constructMessageOptions = (
|
|
627
|
-
if (!
|
|
628
|
-
switch (
|
|
638
|
+
}, this.constructMessageOptions = (t) => t.messageOptions ? { messageOptions: t.messageOptions } : {}, this.constructSystemMessage = (t) => {
|
|
639
|
+
if (!t || !t.systemMessagePayload) return null;
|
|
640
|
+
switch (t.systemMessagePayload.type) {
|
|
629
641
|
case "state_checkpoint":
|
|
630
642
|
return {
|
|
631
|
-
id:
|
|
643
|
+
id: t.publicId,
|
|
632
644
|
type: "SYSTEM",
|
|
633
645
|
subtype: "state_checkpoint",
|
|
634
|
-
data: { payload:
|
|
635
|
-
timestamp:
|
|
646
|
+
data: { payload: t.systemMessagePayload.payload },
|
|
647
|
+
timestamp: t.sentAt || "",
|
|
636
648
|
attachments: void 0
|
|
637
649
|
};
|
|
638
650
|
case "csat_requested":
|
|
639
651
|
return {
|
|
640
|
-
id:
|
|
652
|
+
id: t.publicId,
|
|
641
653
|
type: "SYSTEM",
|
|
642
654
|
subtype: "csat_requested",
|
|
643
655
|
data: { payload: void 0 },
|
|
644
|
-
timestamp:
|
|
656
|
+
timestamp: t.sentAt || "",
|
|
645
657
|
attachments: void 0
|
|
646
658
|
};
|
|
647
659
|
case "csat_submitted":
|
|
648
660
|
return {
|
|
649
|
-
id:
|
|
661
|
+
id: t.publicId,
|
|
650
662
|
type: "SYSTEM",
|
|
651
663
|
subtype: "csat_submitted",
|
|
652
664
|
data: {
|
|
653
665
|
payload: {
|
|
654
|
-
score:
|
|
655
|
-
feedback:
|
|
666
|
+
score: t.systemMessagePayload.payload.score ?? void 0,
|
|
667
|
+
feedback: t.systemMessagePayload.payload.feedback ?? void 0
|
|
656
668
|
}
|
|
657
669
|
},
|
|
658
|
-
timestamp:
|
|
670
|
+
timestamp: t.sentAt || "",
|
|
659
671
|
attachments: void 0
|
|
660
672
|
};
|
|
661
673
|
case "none":
|
|
662
674
|
return null;
|
|
663
675
|
default:
|
|
664
676
|
return be(
|
|
665
|
-
|
|
677
|
+
t.systemMessagePayload,
|
|
666
678
|
this.constructSystemMessage.name
|
|
667
679
|
), null;
|
|
668
680
|
}
|
|
669
|
-
}, this.extractActionResult = (
|
|
670
|
-
const a =
|
|
681
|
+
}, this.extractActionResult = (t) => {
|
|
682
|
+
const a = t.result;
|
|
671
683
|
if (a === null || typeof a != "object") return a;
|
|
672
684
|
if ("responseBodyText" in a && typeof a.responseBodyText == "string") {
|
|
673
685
|
const n = a.responseBodyText, c = Y(() => JSON.parse(n)).data;
|
|
674
686
|
if (c) return c;
|
|
675
687
|
}
|
|
676
|
-
return
|
|
677
|
-
}, this.api = e, this.config =
|
|
688
|
+
return t.result;
|
|
689
|
+
}, this.api = e, this.config = s, this.sessionCtx = o, this.messageCtx = r, this.sessionPollingIntervalSeconds = _, this.registerPolling();
|
|
678
690
|
}
|
|
679
691
|
}
|
|
680
692
|
class ve {
|
|
681
693
|
constructor({
|
|
682
694
|
config: e,
|
|
683
|
-
api:
|
|
695
|
+
api: s,
|
|
684
696
|
storageCtx: o
|
|
685
697
|
}) {
|
|
686
698
|
var r;
|
|
@@ -688,9 +700,9 @@ class ve {
|
|
|
688
700
|
var _;
|
|
689
701
|
return !!(!((_ = this.state.get().contact) != null && _.token) && this.config.collectUserData);
|
|
690
702
|
}, this.autoCreateUnverifiedUserIfNotExists = async () => {
|
|
691
|
-
var _,
|
|
703
|
+
var _, t, a, n, c, d, l, p, u, m, g, f, y, v;
|
|
692
704
|
if (!((_ = this.config.user) != null && _.token)) {
|
|
693
|
-
if (this.config.collectUserData && !((a = (
|
|
705
|
+
if (this.config.collectUserData && !((a = (t = this.config.user) == null ? void 0 : t.data) != null && a.email)) {
|
|
694
706
|
if ((n = this.config.extraDataCollectionFields) != null && n.length)
|
|
695
707
|
return;
|
|
696
708
|
const w = await ((c = this.storageCtx) == null ? void 0 : c.getContactToken());
|
|
@@ -710,8 +722,8 @@ class ve {
|
|
|
710
722
|
non_verified_custom_data: (v = (y = this.config.user) == null ? void 0 : y.data) == null ? void 0 : v.customData
|
|
711
723
|
});
|
|
712
724
|
}
|
|
713
|
-
}, this.createUnverifiedContact = async (_,
|
|
714
|
-
this.state.setPartial({ extraCollectedData:
|
|
725
|
+
}, this.createUnverifiedContact = async (_, t) => {
|
|
726
|
+
this.state.setPartial({ extraCollectedData: t });
|
|
715
727
|
try {
|
|
716
728
|
this.state.setPartial({
|
|
717
729
|
isCreatingUnverifiedContact: !0,
|
|
@@ -727,11 +739,11 @@ class ve {
|
|
|
727
739
|
this.state.setPartial({ isCreatingUnverifiedContact: !1 });
|
|
728
740
|
}
|
|
729
741
|
}, this.recoverFromStaleToken = async () => {
|
|
730
|
-
var _,
|
|
742
|
+
var _, t, a, n, c, d, l, p, u, m;
|
|
731
743
|
if ((_ = this.config.user) != null && _.token) return !1;
|
|
732
744
|
this.api.setAuthToken(""), this.state.setPartial({ contact: null });
|
|
733
745
|
try {
|
|
734
|
-
await ((
|
|
746
|
+
await ((t = this.storageCtx) == null ? void 0 : t.clearContactToken()), await ((a = this.storageCtx) == null ? void 0 : a.clearActiveSessionId());
|
|
735
747
|
} catch (g) {
|
|
736
748
|
b.warn("failed to clear a stale contact token", {
|
|
737
749
|
error: g instanceof Error ? g.message : String(g)
|
|
@@ -744,9 +756,9 @@ class ve {
|
|
|
744
756
|
}), !!((m = this.state.get().contact) != null && m.token);
|
|
745
757
|
}, this.setUnverifiedContact = async (_) => {
|
|
746
758
|
var n, c, d, l;
|
|
747
|
-
const
|
|
759
|
+
const t = await ((n = this.storageCtx) == null ? void 0 : n.getExternalContactId()), a = ((c = this.config.user) == null ? void 0 : c.externalId) || t || K();
|
|
748
760
|
this.api.setAuthToken(_), await ((d = this.storageCtx) == null ? void 0 : d.setContactToken(_)), await ((l = this.storageCtx) == null ? void 0 : l.setExternalContactId(a)), this.state.setPartial({ contact: { token: _, externalId: a } });
|
|
749
|
-
}, this.config = e, this.storageCtx = o, this.api =
|
|
761
|
+
}, this.config = e, this.storageCtx = o, this.api = s, this.state = new z({
|
|
750
762
|
contact: (r = e.user) != null && r.token ? {
|
|
751
763
|
token: e.user.token,
|
|
752
764
|
// Set optional externalId from config... not local storage
|
|
@@ -760,8 +772,8 @@ class ve {
|
|
|
760
772
|
}
|
|
761
773
|
class ye {
|
|
762
774
|
constructor(e) {
|
|
763
|
-
this.api = e, this.state = new z([]), this.controllers = /* @__PURE__ */ new Map(), this.appendFiles = (
|
|
764
|
-
const o =
|
|
775
|
+
this.api = e, this.state = new z([]), this.controllers = /* @__PURE__ */ new Map(), this.appendFiles = (s) => {
|
|
776
|
+
const o = s.map(
|
|
765
777
|
(r) => ({
|
|
766
778
|
file: r,
|
|
767
779
|
id: K(),
|
|
@@ -770,25 +782,25 @@ class ye {
|
|
|
770
782
|
})
|
|
771
783
|
);
|
|
772
784
|
this.state.set([...this.state.get(), ...o]), o.forEach((r) => void this.upload(r));
|
|
773
|
-
}, this.cancel = (
|
|
785
|
+
}, this.cancel = (s) => {
|
|
774
786
|
var o;
|
|
775
|
-
(o = this.controllers.get(
|
|
787
|
+
(o = this.controllers.get(s)) == null || o.abort(), this.controllers.delete(s), this.state.set(this.state.get().filter((r) => r.id !== s));
|
|
776
788
|
}, this.reset = () => {
|
|
777
|
-
this.controllers.forEach((
|
|
789
|
+
this.controllers.forEach((s) => s.abort()), this.controllers.clear(), this.state.reset();
|
|
778
790
|
};
|
|
779
791
|
}
|
|
780
|
-
update(e,
|
|
792
|
+
update(e, s) {
|
|
781
793
|
this.state.set(
|
|
782
|
-
this.state.get().map((o) => o.id === e ? { ...o, ...
|
|
794
|
+
this.state.get().map((o) => o.id === e ? { ...o, ...s } : o)
|
|
783
795
|
);
|
|
784
796
|
}
|
|
785
797
|
async upload(e) {
|
|
786
|
-
const
|
|
787
|
-
this.controllers.set(e.id,
|
|
798
|
+
const s = new AbortController();
|
|
799
|
+
this.controllers.set(e.id, s), this.update(e.id, { status: "uploading" });
|
|
788
800
|
try {
|
|
789
801
|
const o = await this.api.uploadFile({
|
|
790
802
|
file: e.file,
|
|
791
|
-
abortSignal:
|
|
803
|
+
abortSignal: s.signal,
|
|
792
804
|
onProgress: (r) => this.update(e.id, { progress: r })
|
|
793
805
|
});
|
|
794
806
|
this.update(e.id, {
|
|
@@ -797,7 +809,7 @@ class ye {
|
|
|
797
809
|
progress: 100
|
|
798
810
|
});
|
|
799
811
|
} catch (o) {
|
|
800
|
-
|
|
812
|
+
s.signal.aborted || this.update(e.id, {
|
|
801
813
|
status: "error",
|
|
802
814
|
progress: 0,
|
|
803
815
|
error: o instanceof Error ? o.message : "Upload failed"
|
|
@@ -813,7 +825,7 @@ function q() {
|
|
|
813
825
|
class we {
|
|
814
826
|
constructor({
|
|
815
827
|
api: e,
|
|
816
|
-
sessionCtx:
|
|
828
|
+
sessionCtx: s,
|
|
817
829
|
messageCtx: o
|
|
818
830
|
}) {
|
|
819
831
|
this.submitCsat = async (r) => {
|
|
@@ -821,12 +833,12 @@ class we {
|
|
|
821
833
|
const _ = (c = this.sessionCtx.sessionState.get().session) == null ? void 0 : c.id;
|
|
822
834
|
if (!_)
|
|
823
835
|
return { data: null, error: "No session id found" };
|
|
824
|
-
const
|
|
836
|
+
const t = q();
|
|
825
837
|
this.messageCtx.state.setPartial({
|
|
826
838
|
messages: [
|
|
827
839
|
...this.messageCtx.state.get().messages,
|
|
828
840
|
{
|
|
829
|
-
id:
|
|
841
|
+
id: t,
|
|
830
842
|
type: "SYSTEM",
|
|
831
843
|
subtype: "csat_submitted",
|
|
832
844
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
@@ -841,11 +853,11 @@ class we {
|
|
|
841
853
|
});
|
|
842
854
|
const { data: a, error: n } = await this.api.submitCsat({
|
|
843
855
|
...r,
|
|
844
|
-
system_message_uuid:
|
|
856
|
+
system_message_uuid: t,
|
|
845
857
|
session_id: _
|
|
846
858
|
});
|
|
847
859
|
return { data: a, error: n };
|
|
848
|
-
}, this.api = e, this.sessionCtx =
|
|
860
|
+
}, this.api = e, this.sessionCtx = s, this.messageCtx = o;
|
|
849
861
|
}
|
|
850
862
|
}
|
|
851
863
|
const je = [
|
|
@@ -940,16 +952,16 @@ function Ce(i) {
|
|
|
940
952
|
function xe(i) {
|
|
941
953
|
const e = je.flatMap(
|
|
942
954
|
(r) => i.flatMap(
|
|
943
|
-
(_) => r.phrases[_].map((
|
|
955
|
+
(_) => r.phrases[_].map((t) => ({ phrase: t, action: r.action }))
|
|
944
956
|
)
|
|
945
|
-
),
|
|
957
|
+
), s = e.map((r) => Ce(r.phrase)).sort((r, _) => _.length - r.length).join("|"), o = String.raw`(?<=^\s{0,3}|[.!?\n]\s{0,3})`;
|
|
946
958
|
return {
|
|
947
959
|
regex: new RegExp(
|
|
948
|
-
`${o}(${
|
|
960
|
+
`${o}(${s})` + String.raw`([.,!?;:])(?=\s|$)`,
|
|
949
961
|
"giu"
|
|
950
962
|
),
|
|
951
963
|
finalRegex: new RegExp(
|
|
952
|
-
`${o}(${
|
|
964
|
+
`${o}(${s})` + String.raw`([.,!?;:]?)(?=\s|$)`,
|
|
953
965
|
"giu"
|
|
954
966
|
),
|
|
955
967
|
phraseToAction: new Map(
|
|
@@ -960,24 +972,24 @@ function xe(i) {
|
|
|
960
972
|
}
|
|
961
973
|
const V = /* @__PURE__ */ new Map();
|
|
962
974
|
function R(i) {
|
|
963
|
-
const e = i && ze(i) && i !== "en" ? i : null,
|
|
964
|
-
let o = V.get(
|
|
965
|
-
return o || (o = xe(e ? ["en", e] : ["en"]), V.set(
|
|
975
|
+
const e = i && ze(i) && i !== "en" ? i : null, s = e ?? "en";
|
|
976
|
+
let o = V.get(s);
|
|
977
|
+
return o || (o = xe(e ? ["en", e] : ["en"]), V.set(s, o)), o;
|
|
966
978
|
}
|
|
967
979
|
function qe(i) {
|
|
968
980
|
const e = i.replace(/\s+$/, "");
|
|
969
981
|
if (!e) return "";
|
|
970
|
-
const
|
|
982
|
+
const s = e.replace(/[.!?]+$/, "");
|
|
971
983
|
let o = -1;
|
|
972
|
-
for (let r =
|
|
973
|
-
const _ =
|
|
984
|
+
for (let r = s.length - 1; r >= 0; r--) {
|
|
985
|
+
const _ = s[r];
|
|
974
986
|
if (_ === "." || _ === "!" || _ === "?" || _ === `
|
|
975
987
|
`) {
|
|
976
988
|
o = r + 1;
|
|
977
989
|
break;
|
|
978
990
|
}
|
|
979
991
|
}
|
|
980
|
-
return o === -1 ? "" : `${
|
|
992
|
+
return o === -1 ? "" : `${s.slice(0, o)} `.replace(/\n $/, `
|
|
981
993
|
`);
|
|
982
994
|
}
|
|
983
995
|
const Ae = new RegExp(
|
|
@@ -987,7 +999,7 @@ const Ae = new RegExp(
|
|
|
987
999
|
function Ee(i) {
|
|
988
1000
|
return i.replace(
|
|
989
1001
|
Ae,
|
|
990
|
-
(e,
|
|
1002
|
+
(e, s, o) => s + o.toUpperCase()
|
|
991
1003
|
);
|
|
992
1004
|
}
|
|
993
1005
|
function Pe(i) {
|
|
@@ -999,10 +1011,10 @@ class A {
|
|
|
999
1011
|
* Pure and deterministic: always called with the entire raw string so the
|
|
1000
1012
|
* result never depends on delta chunking.
|
|
1001
1013
|
*/
|
|
1002
|
-
static process(e,
|
|
1003
|
-
const o = R(
|
|
1014
|
+
static process(e, s) {
|
|
1015
|
+
const o = R(s == null ? void 0 : s.language), r = s != null && s.final ? "" : A.commandPrefixTail(e, s == null ? void 0 : s.language), _ = e.slice(0, e.length - r.length), t = s != null && s.final ? o.finalRegex : o.regex;
|
|
1004
1016
|
let a = "", n = null, c = 0;
|
|
1005
|
-
for (const l of Array.from(_.matchAll(
|
|
1017
|
+
for (const l of Array.from(_.matchAll(t))) {
|
|
1006
1018
|
const [, p = ""] = l, u = o.phraseToAction.get(p.toLowerCase());
|
|
1007
1019
|
if (u)
|
|
1008
1020
|
if (a += _.slice(c, l.index), c = l.index + l[0].length, u === "newline") a = `${a.replace(/[ \t]+$/, "")}
|
|
@@ -1039,16 +1051,16 @@ class A {
|
|
|
1039
1051
|
* control command from an intermediate delta would stop or submit
|
|
1040
1052
|
* mid-sentence.
|
|
1041
1053
|
*/
|
|
1042
|
-
static commandPrefixTail(e,
|
|
1043
|
-
const o = R(
|
|
1054
|
+
static commandPrefixTail(e, s) {
|
|
1055
|
+
const o = R(s), r = Math.min(e.length, o.longestPhrase + 2);
|
|
1044
1056
|
for (let _ = r; _ > 0; _--) {
|
|
1045
|
-
const
|
|
1046
|
-
if (!Pe(e.slice(0,
|
|
1047
|
-
const a = e.slice(
|
|
1057
|
+
const t = e.length - _;
|
|
1058
|
+
if (!Pe(e.slice(0, t))) continue;
|
|
1059
|
+
const a = e.slice(t).toLowerCase(), n = a.replace(/^\s+/, "");
|
|
1048
1060
|
if (n) {
|
|
1049
1061
|
for (const c of Array.from(o.phraseToAction.keys()))
|
|
1050
1062
|
if (c.startsWith(n))
|
|
1051
|
-
return e.slice(
|
|
1063
|
+
return e.slice(t + (a.length - n.length));
|
|
1052
1064
|
}
|
|
1053
1065
|
}
|
|
1054
1066
|
return "";
|
|
@@ -1058,9 +1070,9 @@ class A {
|
|
|
1058
1070
|
* prefix) — i.e. a silence pause would resolve it into a command. Used by
|
|
1059
1071
|
* the manager's silence timer.
|
|
1060
1072
|
*/
|
|
1061
|
-
static pendingTailIsCompletePhrase(e,
|
|
1062
|
-
const o = A.commandPrefixTail(e,
|
|
1063
|
-
return o ? R(
|
|
1073
|
+
static pendingTailIsCompletePhrase(e, s) {
|
|
1074
|
+
const o = A.commandPrefixTail(e, s).trim().toLowerCase();
|
|
1075
|
+
return o ? R(s).phraseToAction.has(o) : !1;
|
|
1064
1076
|
}
|
|
1065
1077
|
}
|
|
1066
1078
|
const Te = 55, Ie = 90;
|
|
@@ -1073,23 +1085,23 @@ class G {
|
|
|
1073
1085
|
*/
|
|
1074
1086
|
static nextDripText({
|
|
1075
1087
|
displayed: e,
|
|
1076
|
-
processed:
|
|
1088
|
+
processed: s
|
|
1077
1089
|
}) {
|
|
1078
|
-
if (
|
|
1079
|
-
const o =
|
|
1090
|
+
if (s.length <= e.length) return s;
|
|
1091
|
+
const o = s.length - e.length;
|
|
1080
1092
|
if (o > Ie)
|
|
1081
|
-
return
|
|
1093
|
+
return s.slice(0, e.length + Math.ceil(o / 3));
|
|
1082
1094
|
let r = e.length;
|
|
1083
|
-
for (; r <
|
|
1084
|
-
for (; r <
|
|
1085
|
-
return r - e.length > 12 && (r = e.length + 6),
|
|
1095
|
+
for (; r < s.length && /\s/.test(s.charAt(r)); ) r++;
|
|
1096
|
+
for (; r < s.length && !/\s/.test(s.charAt(r)); ) r++;
|
|
1097
|
+
return r - e.length > 12 && (r = e.length + 6), s.slice(0, Math.max(r, e.length + 1));
|
|
1086
1098
|
}
|
|
1087
1099
|
/** Normalize a BCP-47 browser locale into the hint OpenAI accepts. */
|
|
1088
1100
|
static languageHint(e) {
|
|
1089
1101
|
if (!e) return;
|
|
1090
|
-
const
|
|
1091
|
-
if (/^zh-(cn|tw|hk)$/.test(
|
|
1092
|
-
const o =
|
|
1102
|
+
const s = e.toLowerCase();
|
|
1103
|
+
if (/^zh-(cn|tw|hk)$/.test(s)) return s;
|
|
1104
|
+
const o = s.split("-")[0];
|
|
1093
1105
|
return o && /^[a-z]{2,3}$/.test(o) ? o : void 0;
|
|
1094
1106
|
}
|
|
1095
1107
|
}
|
|
@@ -1109,8 +1121,8 @@ class De {
|
|
|
1109
1121
|
* click reuses the same in-flight promise instead of double-minting).
|
|
1110
1122
|
*/
|
|
1111
1123
|
async resolve(e) {
|
|
1112
|
-
const
|
|
1113
|
-
return
|
|
1124
|
+
const s = this.get();
|
|
1125
|
+
return s || (this.inflight || (this.inflight = e().then((o) => (this.cached = o, o)).finally(() => {
|
|
1114
1126
|
this.inflight = null;
|
|
1115
1127
|
})), this.inflight);
|
|
1116
1128
|
}
|
|
@@ -1137,12 +1149,12 @@ class T extends Error {
|
|
|
1137
1149
|
class Fe {
|
|
1138
1150
|
constructor({
|
|
1139
1151
|
mintToken: e,
|
|
1140
|
-
onDelta:
|
|
1152
|
+
onDelta: s,
|
|
1141
1153
|
onState: o,
|
|
1142
1154
|
onError: r,
|
|
1143
1155
|
onLevel: _
|
|
1144
1156
|
}) {
|
|
1145
|
-
this.state = "idle", this.pc = null, this.dc = null, this.stream = null, this.audioContext = null, this.levelRaf = 0, this.timers = [], this.loggedParseFailure = !1, this.mintToken = e, this.onDelta =
|
|
1157
|
+
this.state = "idle", this.pc = null, this.dc = null, this.stream = null, this.audioContext = null, this.levelRaf = 0, this.timers = [], this.loggedParseFailure = !1, this.mintToken = e, this.onDelta = s, this.onState = o, this.onError = r, this.onLevel = _;
|
|
1146
1158
|
}
|
|
1147
1159
|
async start() {
|
|
1148
1160
|
if (this.state === "idle") {
|
|
@@ -1157,22 +1169,22 @@ class Fe {
|
|
|
1157
1169
|
this.isTornDown() || this.stream && this.stream !== d ? d.getTracks().forEach((l) => l.stop()) : this.stream = d;
|
|
1158
1170
|
}).catch(() => {
|
|
1159
1171
|
});
|
|
1160
|
-
const [
|
|
1172
|
+
const [s, o] = await Promise.all([e, this.mintToken()]);
|
|
1161
1173
|
if (this.isTornDown()) {
|
|
1162
|
-
|
|
1174
|
+
s.getTracks().forEach((d) => d.stop());
|
|
1163
1175
|
return;
|
|
1164
1176
|
}
|
|
1165
|
-
this.stream =
|
|
1177
|
+
this.stream = s, this.startLevelMeter(s);
|
|
1166
1178
|
const r = new RTCPeerConnection();
|
|
1167
1179
|
this.pc = r;
|
|
1168
|
-
const [_] =
|
|
1180
|
+
const [_] = s.getAudioTracks();
|
|
1169
1181
|
if (!_) throw new T("No audio track");
|
|
1170
|
-
r.addTrack(_,
|
|
1171
|
-
const
|
|
1172
|
-
this.dc =
|
|
1182
|
+
r.addTrack(_, s);
|
|
1183
|
+
const t = r.createDataChannel("oai-events");
|
|
1184
|
+
this.dc = t, t.addEventListener(
|
|
1173
1185
|
"message",
|
|
1174
1186
|
(d) => this.handleServerEvent(d.data)
|
|
1175
|
-
),
|
|
1187
|
+
), t.addEventListener("open", () => {
|
|
1176
1188
|
this.state === "connecting" && this.setState("listening");
|
|
1177
1189
|
}), r.addEventListener("connectionstatechange", () => {
|
|
1178
1190
|
if (r.connectionState === "failed") {
|
|
@@ -1222,24 +1234,24 @@ class Fe {
|
|
|
1222
1234
|
autoGainControl: !0
|
|
1223
1235
|
}
|
|
1224
1236
|
});
|
|
1225
|
-
} catch (
|
|
1237
|
+
} catch (s) {
|
|
1226
1238
|
throw new T(
|
|
1227
|
-
|
|
1239
|
+
s instanceof DOMException ? s.name : "Could not access the microphone"
|
|
1228
1240
|
);
|
|
1229
1241
|
}
|
|
1230
1242
|
}
|
|
1231
1243
|
handleServerEvent(e) {
|
|
1232
1244
|
if (typeof e != "string") return;
|
|
1233
|
-
let
|
|
1245
|
+
let s;
|
|
1234
1246
|
try {
|
|
1235
|
-
|
|
1247
|
+
s = JSON.parse(e);
|
|
1236
1248
|
} catch {
|
|
1237
1249
|
this.loggedParseFailure || (this.loggedParseFailure = !0, b.error("dictation server event was not valid JSON", {
|
|
1238
1250
|
snippet: e.slice(0, 120)
|
|
1239
1251
|
}));
|
|
1240
1252
|
return;
|
|
1241
1253
|
}
|
|
1242
|
-
const o = Re(
|
|
1254
|
+
const o = Re(s);
|
|
1243
1255
|
if (!o) return;
|
|
1244
1256
|
const { type: r, delta: _ } = o;
|
|
1245
1257
|
if (r === "session.created" || r === "transcription_session.created") {
|
|
@@ -1257,11 +1269,11 @@ class Fe {
|
|
|
1257
1269
|
startLevelMeter(e) {
|
|
1258
1270
|
if (this.onLevel)
|
|
1259
1271
|
try {
|
|
1260
|
-
const
|
|
1261
|
-
this.audioContext =
|
|
1262
|
-
const o =
|
|
1272
|
+
const s = new AudioContext();
|
|
1273
|
+
this.audioContext = s;
|
|
1274
|
+
const o = s.createMediaStreamSource(e), r = s.createAnalyser();
|
|
1263
1275
|
r.fftSize = 512, o.connect(r);
|
|
1264
|
-
const _ = new Uint8Array(r.frequencyBinCount),
|
|
1276
|
+
const _ = new Uint8Array(r.frequencyBinCount), t = () => {
|
|
1265
1277
|
var c;
|
|
1266
1278
|
r.getByteTimeDomainData(_);
|
|
1267
1279
|
let a = 0;
|
|
@@ -1270,12 +1282,12 @@ class Fe {
|
|
|
1270
1282
|
a += l * l;
|
|
1271
1283
|
}
|
|
1272
1284
|
const n = Math.sqrt(a / _.length);
|
|
1273
|
-
(c = this.onLevel) == null || c.call(this, Math.min(1, n * 4)), this.levelRaf = requestAnimationFrame(
|
|
1285
|
+
(c = this.onLevel) == null || c.call(this, Math.min(1, n * 4)), this.levelRaf = requestAnimationFrame(t);
|
|
1274
1286
|
};
|
|
1275
|
-
this.levelRaf = requestAnimationFrame(
|
|
1276
|
-
} catch (
|
|
1287
|
+
this.levelRaf = requestAnimationFrame(t);
|
|
1288
|
+
} catch (s) {
|
|
1277
1289
|
b.error("dictation level meter unavailable", {
|
|
1278
|
-
_e:
|
|
1290
|
+
_e: s instanceof Error ? s.message : String(s)
|
|
1279
1291
|
});
|
|
1280
1292
|
}
|
|
1281
1293
|
}
|
|
@@ -1286,8 +1298,8 @@ class Fe {
|
|
|
1286
1298
|
this.isTornDown() || (this.teardown(), this.setState("error"), this.onError(e));
|
|
1287
1299
|
}
|
|
1288
1300
|
teardown() {
|
|
1289
|
-
var e,
|
|
1290
|
-
this.timers.forEach((_) => clearTimeout(_)), this.timers = [], this.levelRaf && cancelAnimationFrame(this.levelRaf), this.levelRaf = 0, (e = this.dc) == null || e.close(), this.dc = null, (
|
|
1301
|
+
var e, s, o, r;
|
|
1302
|
+
this.timers.forEach((_) => clearTimeout(_)), this.timers = [], this.levelRaf && cancelAnimationFrame(this.levelRaf), this.levelRaf = 0, (e = this.dc) == null || e.close(), this.dc = null, (s = this.pc) == null || s.close(), this.pc = null, (o = this.stream) == null || o.getTracks().forEach((_) => _.stop()), this.stream = null, (r = this.audioContext) == null || r.close().catch((_) => {
|
|
1291
1303
|
b.error("failed to close dictation audio context", {
|
|
1292
1304
|
_e: String(_)
|
|
1293
1305
|
});
|
|
@@ -1299,7 +1311,7 @@ class Fe {
|
|
|
1299
1311
|
}
|
|
1300
1312
|
const Ue = 1300, Be = { status: "idle", error: null };
|
|
1301
1313
|
class He {
|
|
1302
|
-
constructor({ api: e, config:
|
|
1314
|
+
constructor({ api: e, config: s }) {
|
|
1303
1315
|
this.state = new z(Be), this.levelRef = { current: 0 }, this.mintCache = new De(), this.session = null, this.target = null, this.prefix = "", this.raw = "", this.processed = "", this.displayed = "", this.dripTimer = null, this.silenceTimer = null, this.isActive = () => this.session !== null, this.prewarm = () => {
|
|
1304
1316
|
this.mintToken().catch((o) => {
|
|
1305
1317
|
this.mintCache.clear(), b.error("dictation token prewarm failed", {
|
|
@@ -1328,12 +1340,12 @@ class He {
|
|
|
1328
1340
|
return;
|
|
1329
1341
|
}
|
|
1330
1342
|
this.raw += _;
|
|
1331
|
-
const
|
|
1332
|
-
if (
|
|
1343
|
+
const t = this.ingest();
|
|
1344
|
+
if (t === "stop") {
|
|
1333
1345
|
this.teardown();
|
|
1334
1346
|
return;
|
|
1335
1347
|
}
|
|
1336
|
-
if (
|
|
1348
|
+
if (t === "send") {
|
|
1337
1349
|
this.teardown({ send: !0 });
|
|
1338
1350
|
return;
|
|
1339
1351
|
}
|
|
@@ -1366,7 +1378,7 @@ class He {
|
|
|
1366
1378
|
this.session && this.teardown(o);
|
|
1367
1379
|
}, this.mintToken = async () => this.mintCache.resolve(
|
|
1368
1380
|
() => this.api.createDictationSession({ language: this.language })
|
|
1369
|
-
), this.api = e, this.config =
|
|
1381
|
+
), this.api = e, this.config = s;
|
|
1370
1382
|
}
|
|
1371
1383
|
/** Applied text = prefix + dictated. A mismatch means the visitor edited. */
|
|
1372
1384
|
isConsistent() {
|
|
@@ -1378,25 +1390,25 @@ class He {
|
|
|
1378
1390
|
/** Process the transcript and apply the outcome. Shared by the delta path,
|
|
1379
1391
|
* the silence timer, and (with `final`) teardown. Returns the control. */
|
|
1380
1392
|
ingest(e) {
|
|
1381
|
-
const
|
|
1393
|
+
const s = A.process(this.raw, {
|
|
1382
1394
|
language: this.language,
|
|
1383
1395
|
final: e == null ? void 0 : e.final
|
|
1384
1396
|
});
|
|
1385
|
-
return this.processed =
|
|
1397
|
+
return this.processed = s.text, s.text.startsWith(this.displayed) || this.applyDisplayed(s.text), s.control;
|
|
1386
1398
|
}
|
|
1387
1399
|
stopSilenceTimer() {
|
|
1388
1400
|
this.silenceTimer && clearTimeout(this.silenceTimer), this.silenceTimer = null;
|
|
1389
1401
|
}
|
|
1390
1402
|
teardown(e) {
|
|
1391
1403
|
var _;
|
|
1392
|
-
const
|
|
1404
|
+
const s = this.session, o = this.target;
|
|
1393
1405
|
this.dripTimer && clearInterval(this.dripTimer), this.dripTimer = null, this.stopSilenceTimer();
|
|
1394
1406
|
let r = (e == null ? void 0 : e.send) ?? !1;
|
|
1395
1407
|
if (o && this.isConsistent()) {
|
|
1396
|
-
const
|
|
1397
|
-
(e == null ? void 0 : e.executeFinalCommand) !== !1 &&
|
|
1408
|
+
const t = this.ingest({ final: !0 });
|
|
1409
|
+
(e == null ? void 0 : e.executeFinalCommand) !== !1 && t === "send" && (r = !0), this.processed !== this.displayed && this.applyDisplayed(this.processed);
|
|
1398
1410
|
}
|
|
1399
|
-
this.session = null, this.target = null,
|
|
1411
|
+
this.session = null, this.target = null, s == null || s.stop(), this.levelRef.current = 0, this.state.set({ status: "idle", error: (e == null ? void 0 : e.error) ?? null }), r && ((_ = o == null ? void 0 : o.onSend) == null || _.call(o));
|
|
1400
1412
|
}
|
|
1401
1413
|
}
|
|
1402
1414
|
const Ve = {
|
|
@@ -5161,20 +5173,20 @@ function at(i) {
|
|
|
5161
5173
|
function Pa(i) {
|
|
5162
5174
|
return Aa(i) ? i : "en";
|
|
5163
5175
|
}
|
|
5164
|
-
function Ta(i, e,
|
|
5176
|
+
function Ta(i, e, s, o) {
|
|
5165
5177
|
var _;
|
|
5166
|
-
const r = ((_ =
|
|
5178
|
+
const r = ((_ = s == null ? void 0 : s[e]) == null ? void 0 : _[i]) || X[e][i] || Q[i] || i;
|
|
5167
5179
|
return o ? r.replace(
|
|
5168
5180
|
/\{(\w+)\}/g,
|
|
5169
|
-
(
|
|
5181
|
+
(t, a) => a in o ? String(o[a]) : t
|
|
5170
5182
|
) : r;
|
|
5171
5183
|
}
|
|
5172
|
-
function Ia(i, e,
|
|
5184
|
+
function Ia(i, e, s) {
|
|
5173
5185
|
return Ta(
|
|
5174
5186
|
e,
|
|
5175
5187
|
Pa(i.language),
|
|
5176
5188
|
i.translationOverrides,
|
|
5177
|
-
|
|
5189
|
+
s
|
|
5178
5190
|
);
|
|
5179
5191
|
}
|
|
5180
5192
|
function I(i) {
|
|
@@ -5183,24 +5195,24 @@ function I(i) {
|
|
|
5183
5195
|
function Ma(i, e) {
|
|
5184
5196
|
var o;
|
|
5185
5197
|
if ((e == null ? void 0 : e.isOpened) === !1) return null;
|
|
5186
|
-
const
|
|
5198
|
+
const s = i.findLast(
|
|
5187
5199
|
(r) => I(r) && !!r.messageOptions
|
|
5188
5200
|
);
|
|
5189
|
-
return ((o =
|
|
5201
|
+
return ((o = s == null ? void 0 : s.messageOptions) == null ? void 0 : o.pickedOptionId) === null ? s : null;
|
|
5190
5202
|
}
|
|
5191
5203
|
function tt(i, e) {
|
|
5192
5204
|
var o, r;
|
|
5193
|
-
const
|
|
5194
|
-
return ((o =
|
|
5205
|
+
const s = Ma(i, e);
|
|
5206
|
+
return ((o = s == null ? void 0 : s.messageOptions) == null ? void 0 : o.requireTap) !== !0 ? !1 : ((r = i.findLast(I)) == null ? void 0 : r.id) === s.id;
|
|
5195
5207
|
}
|
|
5196
5208
|
class Da {
|
|
5197
5209
|
constructor({
|
|
5198
5210
|
config: e,
|
|
5199
|
-
api:
|
|
5211
|
+
api: s,
|
|
5200
5212
|
contactCtx: o,
|
|
5201
5213
|
storageCtx: r,
|
|
5202
5214
|
sessionsPollingIntervalSeconds: _,
|
|
5203
|
-
sharedSessions:
|
|
5215
|
+
sharedSessions: t
|
|
5204
5216
|
}) {
|
|
5205
5217
|
this.sessionsRefresher = new J(), this.persistedSessionId = null, this.sessionAbortController = new AbortController(), this.sessionState = new z({
|
|
5206
5218
|
session: null,
|
|
@@ -5370,14 +5382,14 @@ class Da {
|
|
|
5370
5382
|
payload: a
|
|
5371
5383
|
});
|
|
5372
5384
|
return c != null && c.success ? { success: !0 } : { success: !1 };
|
|
5373
|
-
}, this.config = e, this.api =
|
|
5385
|
+
}, this.config = e, this.api = s, this.contactCtx = o, this.storageCtx = r, this.sessionsPollingIntervalSeconds = _, t ? this.sessionsState = t : this.registerSessionsRefresherWrapper(), this.registerActiveSessionPersistence();
|
|
5374
5386
|
}
|
|
5375
5387
|
}
|
|
5376
|
-
const Ra = (i, e, { sendsPageContext:
|
|
5377
|
-
const o = La(i), r =
|
|
5388
|
+
const Ra = (i, e, { sendsPageContext: s }) => {
|
|
5389
|
+
const o = La(i), r = s && e.mentions && e.mentions.length > 0 ? { mentions: e.mentions.map(Na) } : void 0, _ = s && (e.clientContext || r) ? { ...o, ...e.clientContext, ...r } : o, t = e.connectionRequestId ? {
|
|
5378
5390
|
..._,
|
|
5379
5391
|
opencx__connection_request_id: e.connectionRequestId
|
|
5380
|
-
} : _, a = e.withPageEntity === !1 &&
|
|
5392
|
+
} : _, a = e.withPageEntity === !1 && t && "entity" in t ? Ka(t, "entity") : t;
|
|
5381
5393
|
return {
|
|
5382
5394
|
clientContext: e.background ? { ...a, opencx__background: !0 } : a,
|
|
5383
5395
|
custom_data: {
|
|
@@ -5385,13 +5397,13 @@ const Ra = (i, e, { sendsPageContext: t }) => {
|
|
|
5385
5397
|
...e.customData || {}
|
|
5386
5398
|
}
|
|
5387
5399
|
};
|
|
5388
|
-
}, Na = ({ type: i, id: e, title:
|
|
5400
|
+
}, Na = ({ type: i, id: e, title: s, meta: o }) => ({
|
|
5389
5401
|
type: i,
|
|
5390
5402
|
id: e,
|
|
5391
|
-
title:
|
|
5403
|
+
title: s,
|
|
5392
5404
|
...o ? { meta: o } : {}
|
|
5393
5405
|
}), Ka = (i, e) => {
|
|
5394
|
-
const { [e]:
|
|
5406
|
+
const { [e]: s, ...o } = i;
|
|
5395
5407
|
return Object.keys(o).length > 0 ? o : void 0;
|
|
5396
5408
|
}, La = (i) => {
|
|
5397
5409
|
if (typeof i.context != "function") return i.context;
|
|
@@ -5402,24 +5414,24 @@ const Ra = (i, e, { sendsPageContext: t }) => {
|
|
|
5402
5414
|
return;
|
|
5403
5415
|
}
|
|
5404
5416
|
}, Oa = (i) => {
|
|
5405
|
-
const e = i.features,
|
|
5417
|
+
const e = i.features, s = {
|
|
5406
5418
|
page_context: (e == null ? void 0 : e.pageContext) === !0,
|
|
5407
5419
|
client_tools: (e == null ? void 0 : e.pageContext) === !0 && (e == null ? void 0 : e.clientTools) === !0,
|
|
5408
5420
|
page_actions: (e == null ? void 0 : e.pageContext) === !0 && (e == null ? void 0 : e.clientTools) === !0 && (e == null ? void 0 : e.pageActions) === !0
|
|
5409
5421
|
};
|
|
5410
|
-
return e && (e.preamble !== void 0 && (
|
|
5422
|
+
return e && (e.preamble !== void 0 && (s.preamble = e.preamble), e.inlineUi !== void 0 && (s.inline_ui = e.inlineUi)), s;
|
|
5411
5423
|
}, Fa = ({
|
|
5412
5424
|
config: i,
|
|
5413
5425
|
input: e,
|
|
5414
|
-
uuid:
|
|
5426
|
+
uuid: s,
|
|
5415
5427
|
sessionId: o,
|
|
5416
5428
|
content: r,
|
|
5417
5429
|
initialMessages: _,
|
|
5418
|
-
sendsPageContext:
|
|
5430
|
+
sendsPageContext: t
|
|
5419
5431
|
}) => {
|
|
5420
5432
|
var a, n, c, d;
|
|
5421
5433
|
return {
|
|
5422
|
-
uuid:
|
|
5434
|
+
uuid: s,
|
|
5423
5435
|
bot_token: i.token,
|
|
5424
5436
|
headers: i.headers,
|
|
5425
5437
|
query_params: i.queryParams,
|
|
@@ -5427,7 +5439,7 @@ const Ra = (i, e, { sendsPageContext: t }) => {
|
|
|
5427
5439
|
session_id: o,
|
|
5428
5440
|
content: r,
|
|
5429
5441
|
attachments: e.attachments,
|
|
5430
|
-
...Ra(i, e, { sendsPageContext:
|
|
5442
|
+
...Ra(i, e, { sendsPageContext: t }),
|
|
5431
5443
|
language: i.language,
|
|
5432
5444
|
features: Oa(i),
|
|
5433
5445
|
presentation: i.presentation,
|
|
@@ -5447,11 +5459,11 @@ const Ra = (i, e, { sendsPageContext: t }) => {
|
|
|
5447
5459
|
}, x = class x {
|
|
5448
5460
|
constructor({
|
|
5449
5461
|
config: e,
|
|
5450
|
-
api:
|
|
5462
|
+
api: s,
|
|
5451
5463
|
sessionCtx: o,
|
|
5452
5464
|
contactCtx: r,
|
|
5453
5465
|
streaming: _,
|
|
5454
|
-
sendsPageContext:
|
|
5466
|
+
sendsPageContext: t,
|
|
5455
5467
|
getClientCapabilities: a,
|
|
5456
5468
|
getRequestConfig: n,
|
|
5457
5469
|
isStreaming: c,
|
|
@@ -5722,7 +5734,7 @@ ${l}` : l;
|
|
|
5722
5734
|
variant: "error",
|
|
5723
5735
|
action: void 0
|
|
5724
5736
|
}
|
|
5725
|
-
}), this.config = e, this.getClientCapabilities = a ?? (() => this.getRequestConfig().capabilities), this.api =
|
|
5737
|
+
}), this.config = e, this.getClientCapabilities = a ?? (() => this.getRequestConfig().capabilities), this.api = s, this.sessionCtx = o, this.contactCtx = r, this.isStreaming = c ?? (() => _), this.getRequestConfig = n ?? (() => this.config), this.getSendsPageContext = d ?? (() => t);
|
|
5726
5738
|
}
|
|
5727
5739
|
/**
|
|
5728
5740
|
* Current transport choice. The org or this embed may request polling
|
|
@@ -5735,9 +5747,9 @@ ${l}` : l;
|
|
|
5735
5747
|
return this.getSendsPageContext();
|
|
5736
5748
|
}
|
|
5737
5749
|
/** Hand a send to the streaming engine; its failure can never propagate. */
|
|
5738
|
-
static dispatchToEngine(e,
|
|
5750
|
+
static dispatchToEngine(e, s) {
|
|
5739
5751
|
try {
|
|
5740
|
-
Promise.resolve(e.send(
|
|
5752
|
+
Promise.resolve(e.send(s)).catch((o) => {
|
|
5741
5753
|
b.error("streaming send failed", o);
|
|
5742
5754
|
});
|
|
5743
5755
|
} catch (o) {
|
|
@@ -5745,8 +5757,8 @@ ${l}` : l;
|
|
|
5745
5757
|
}
|
|
5746
5758
|
}
|
|
5747
5759
|
get hasPendingAgentWork() {
|
|
5748
|
-
var e,
|
|
5749
|
-
return this.bufferedAgentSends.length > 0 || (((
|
|
5760
|
+
var e, s;
|
|
5761
|
+
return this.bufferedAgentSends.length > 0 || (((s = (e = this.agentHandlers) == null ? void 0 : e.hasPendingWork) == null ? void 0 : s.call(e)) ?? !1);
|
|
5750
5762
|
}
|
|
5751
5763
|
/**
|
|
5752
5764
|
* Defensive read of `clientContext.page_marks` → the user bubble's context
|
|
@@ -5756,11 +5768,11 @@ ${l}` : l;
|
|
|
5756
5768
|
* without a usable name are dropped.
|
|
5757
5769
|
*/
|
|
5758
5770
|
static markedElementNames(e) {
|
|
5759
|
-
const
|
|
5760
|
-
if (!Array.isArray(
|
|
5761
|
-
const o =
|
|
5771
|
+
const s = e == null ? void 0 : e.page_marks;
|
|
5772
|
+
if (!Array.isArray(s)) return;
|
|
5773
|
+
const o = s.flatMap((r) => {
|
|
5762
5774
|
if (typeof r != "object" || r === null) return [];
|
|
5763
|
-
const _ = r.elements,
|
|
5775
|
+
const _ = r.elements, t = Array.isArray(_) ? _[0] : void 0, a = typeof t == "object" && t !== null && "name" in t ? t.name : void 0;
|
|
5764
5776
|
if (typeof a != "string" || a.length === 0) return [];
|
|
5765
5777
|
const n = r.note, c = r.snapshotUrl;
|
|
5766
5778
|
return [
|
|
@@ -5786,7 +5798,7 @@ x.SENT_TEXT_HISTORY_MAX = 50;
|
|
|
5786
5798
|
let N = x;
|
|
5787
5799
|
class Ua {
|
|
5788
5800
|
constructor(e) {
|
|
5789
|
-
var
|
|
5801
|
+
var s, o;
|
|
5790
5802
|
this.widget = e, this.subscriptions = [], this.disposed = !1, this.restorableSessionId = null, this.didReadStoredSession = !1, this.readStoredSession = () => {
|
|
5791
5803
|
if (!this.shouldRestoreLastSession) {
|
|
5792
5804
|
this.didReadStoredSession = !0;
|
|
@@ -5812,9 +5824,9 @@ class Ua {
|
|
|
5812
5824
|
isInitialFetchLoading: r,
|
|
5813
5825
|
data: _
|
|
5814
5826
|
}) => {
|
|
5815
|
-
var
|
|
5827
|
+
var t, a, n, c, d;
|
|
5816
5828
|
if (!(this.disposed || !this.didReadStoredSession)) {
|
|
5817
|
-
if (this.restorableSessionId && !((
|
|
5829
|
+
if (this.restorableSessionId && !((t = this.sessionCtx.sessionState.get().session) != null && t.id)) {
|
|
5818
5830
|
const l = _.find(
|
|
5819
5831
|
(p) => p.id === this.restorableSessionId && p.isOpened
|
|
5820
5832
|
);
|
|
@@ -5838,7 +5850,7 @@ class Ua {
|
|
|
5838
5850
|
}, this.toSessionsScreen = () => {
|
|
5839
5851
|
this.navigateConversation || this.resetChat(), this.state.setPartial({ screen: "sessions" });
|
|
5840
5852
|
}, this.toChatScreen = (r) => {
|
|
5841
|
-
var
|
|
5853
|
+
var t, a;
|
|
5842
5854
|
if (this.navigateConversation) return this.navigateConversation(r);
|
|
5843
5855
|
this.resetChat();
|
|
5844
5856
|
let _;
|
|
@@ -5846,16 +5858,16 @@ class Ua {
|
|
|
5846
5858
|
if (_ = this.sessionCtx.sessionsState.get().data.find((n) => n.id === r), !_) return;
|
|
5847
5859
|
this.sessionCtx.sessionState.setPartial({ session: _ });
|
|
5848
5860
|
}
|
|
5849
|
-
return this.state.setPartial({ screen: "chat" }), (a = (
|
|
5861
|
+
return this.state.setPartial({ screen: "chat" }), (a = (t = this.config.hooks) == null ? void 0 : t.onNavigateToChat) == null || a.call(t, { session: _ }), this.widget;
|
|
5850
5862
|
}, this.config = e.config, this.contactCtx = e.contactCtx, this.sessionCtx = e.sessionCtx, this.resetChat = e.resetChat, this.state = new z({
|
|
5851
|
-
screen: this.contactCtx.shouldCollectData() ? "welcome" : (
|
|
5863
|
+
screen: this.contactCtx.shouldCollectData() ? "welcome" : (s = this.config.router) != null && s.chatScreenOnly ? "chat" : "sessions"
|
|
5852
5864
|
}), this.shouldRestoreLastSession = ((o = this.config.router) == null ? void 0 : o.restoreLastSession) ?? !1, this.readStoredSession(), this.registerRoutingListener();
|
|
5853
5865
|
}
|
|
5854
5866
|
}
|
|
5855
5867
|
class Ba {
|
|
5856
5868
|
constructor({
|
|
5857
5869
|
storage: e,
|
|
5858
|
-
config:
|
|
5870
|
+
config: s
|
|
5859
5871
|
}) {
|
|
5860
5872
|
this.KEYS = {
|
|
5861
5873
|
contactToken: (o) => `opencx-widget:org-token-${o}:contact-token`,
|
|
@@ -5896,7 +5908,7 @@ class Ba {
|
|
|
5896
5908
|
return o === "docked" || o === "floating" ? o : null;
|
|
5897
5909
|
}, this.setCompanionSidebarMode = async (o) => {
|
|
5898
5910
|
await this.storage.set(this.companionKey("sidebar-mode"), o);
|
|
5899
|
-
}, this.storage = e, this.config =
|
|
5911
|
+
}, this.storage = e, this.config = s;
|
|
5900
5912
|
}
|
|
5901
5913
|
}
|
|
5902
5914
|
function Ha({
|
|
@@ -5932,31 +5944,31 @@ function W(i, e) {
|
|
|
5932
5944
|
return i && e !== !1;
|
|
5933
5945
|
}
|
|
5934
5946
|
function Va(i, e) {
|
|
5935
|
-
const
|
|
5947
|
+
const s = e.features;
|
|
5936
5948
|
return {
|
|
5937
|
-
dictation: W(i.features.dictation,
|
|
5949
|
+
dictation: W(i.features.dictation, s == null ? void 0 : s.dictation),
|
|
5938
5950
|
attachments: W(
|
|
5939
5951
|
i.features.attachments,
|
|
5940
|
-
|
|
5952
|
+
s == null ? void 0 : s.attachments
|
|
5941
5953
|
),
|
|
5942
|
-
pageContext: i.features.pageContext && (
|
|
5943
|
-
clientTools: i.features.clientTools && i.features.pageContext && (
|
|
5944
|
-
pageActions: i.features.pageActions && i.features.clientTools && i.features.pageContext && (
|
|
5954
|
+
pageContext: i.features.pageContext && (s == null ? void 0 : s.pageContext) === !0,
|
|
5955
|
+
clientTools: i.features.clientTools && i.features.pageContext && (s == null ? void 0 : s.pageContext) === !0 && (s == null ? void 0 : s.clientTools) === !0,
|
|
5956
|
+
pageActions: i.features.pageActions && i.features.clientTools && i.features.pageContext && (s == null ? void 0 : s.pageContext) === !0 && (s == null ? void 0 : s.clientTools) === !0 && (s == null ? void 0 : s.pageActions) === !0
|
|
5945
5957
|
};
|
|
5946
5958
|
}
|
|
5947
5959
|
class Ga extends Error {
|
|
5948
|
-
constructor(e,
|
|
5949
|
-
super(
|
|
5960
|
+
constructor(e, s, o) {
|
|
5961
|
+
super(s), this.name = "WidgetInitializationError", this.code = e, this.details = o;
|
|
5950
5962
|
}
|
|
5951
5963
|
}
|
|
5952
5964
|
const S = class S {
|
|
5953
5965
|
constructor({
|
|
5954
5966
|
config: e,
|
|
5955
|
-
storage:
|
|
5967
|
+
storage: s,
|
|
5956
5968
|
getClientCapabilities: o,
|
|
5957
5969
|
getRequestConfig: r,
|
|
5958
5970
|
modes: _,
|
|
5959
|
-
org:
|
|
5971
|
+
org: t,
|
|
5960
5972
|
agent: a,
|
|
5961
5973
|
parent: n
|
|
5962
5974
|
}) {
|
|
@@ -5992,7 +6004,7 @@ const S = class S {
|
|
|
5992
6004
|
throw Error(
|
|
5993
6005
|
"Widget polling values are not defined, did you call WidgetCtx.initialize()"
|
|
5994
6006
|
);
|
|
5995
|
-
this.config = e, this.getRequestConfig = r ?? (() => this.config), this.getClientCapabilities = o, this.org =
|
|
6007
|
+
this.config = e, this.getRequestConfig = r ?? (() => this.config), this.getClientCapabilities = o, this.org = t, this.agent = a, this.api = (n == null ? void 0 : n.api) ?? new H({ config: e }), this.storageCtx = (n == null ? void 0 : n.storageCtx) ?? (s ? new Ba({ storage: s, config: e }) : void 0), this.modes = _, this.contactCtx = (n == null ? void 0 : n.contactCtx) ?? new ve({
|
|
5996
6008
|
api: this.api,
|
|
5997
6009
|
config: this.config,
|
|
5998
6010
|
storageCtx: this.storageCtx
|
|
@@ -6038,8 +6050,8 @@ const S = class S {
|
|
|
6038
6050
|
}), this.routerCtx = new Ua(this);
|
|
6039
6051
|
}
|
|
6040
6052
|
get streaming() {
|
|
6041
|
-
var e,
|
|
6042
|
-
return (e = this.messageCtx) != null && e.hasPendingAgentWork ? !0 : (
|
|
6053
|
+
var e, s;
|
|
6054
|
+
return (e = this.messageCtx) != null && e.hasPendingAgentWork ? !0 : (s = this.messageCtx) != null && s.state.get().isSendingMessage ? !1 : this.agent.streaming && this.getRequestConfig().streaming === !0;
|
|
6043
6055
|
}
|
|
6044
6056
|
/**
|
|
6045
6057
|
* The org's features narrowed by this embed's `config.features` — the
|
|
@@ -6051,11 +6063,11 @@ const S = class S {
|
|
|
6051
6063
|
};
|
|
6052
6064
|
S.pollingIntervalsSeconds = null, S.initialize = async ({
|
|
6053
6065
|
config: e,
|
|
6054
|
-
storage:
|
|
6066
|
+
storage: s,
|
|
6055
6067
|
getClientCapabilities: o,
|
|
6056
6068
|
getRequestConfig: r
|
|
6057
6069
|
}) => {
|
|
6058
|
-
var
|
|
6070
|
+
var t, a;
|
|
6059
6071
|
const _ = await new H({
|
|
6060
6072
|
config: e
|
|
6061
6073
|
}).getExternalWidgetConfig();
|
|
@@ -6066,11 +6078,11 @@ S.pollingIntervalsSeconds = null, S.initialize = async ({
|
|
|
6066
6078
|
_.error
|
|
6067
6079
|
);
|
|
6068
6080
|
return S.pollingIntervalsSeconds = {
|
|
6069
|
-
session: ((
|
|
6081
|
+
session: ((t = _.data) == null ? void 0 : t.sessionPollingIntervalSeconds) || 10,
|
|
6070
6082
|
sessions: ((a = _.data) == null ? void 0 : a.sessionsPollingIntervalSeconds) || 60
|
|
6071
6083
|
}, new S({
|
|
6072
6084
|
config: e,
|
|
6073
|
-
storage:
|
|
6085
|
+
storage: s,
|
|
6074
6086
|
getClientCapabilities: o,
|
|
6075
6087
|
getRequestConfig: r,
|
|
6076
6088
|
modes: _.data.modes || [],
|
|
@@ -6090,8 +6102,8 @@ function st(i, e) {
|
|
|
6090
6102
|
} : e;
|
|
6091
6103
|
}
|
|
6092
6104
|
class ot {
|
|
6093
|
-
constructor({ attack: e, release:
|
|
6094
|
-
this.value = 0, this.attack = e, this.release =
|
|
6105
|
+
constructor({ attack: e, release: s }) {
|
|
6106
|
+
this.value = 0, this.attack = e, this.release = s;
|
|
6095
6107
|
}
|
|
6096
6108
|
next(e) {
|
|
6097
6109
|
return this.value += (e - this.value) * (e > this.value ? this.attack : this.release), this.value;
|
|
@@ -6112,9 +6124,9 @@ function ee(i) {
|
|
|
6112
6124
|
}
|
|
6113
6125
|
function nt(i) {
|
|
6114
6126
|
if (!Array.isArray(i)) return M;
|
|
6115
|
-
const e = [],
|
|
6127
|
+
const e = [], s = /* @__PURE__ */ new Set();
|
|
6116
6128
|
for (const o of i)
|
|
6117
|
-
!ee(o) ||
|
|
6129
|
+
!ee(o) || s.has(o) || (s.add(o), e.push(o));
|
|
6118
6130
|
return e.length > 0 ? e : M;
|
|
6119
6131
|
}
|
|
6120
6132
|
function it(i, e) {
|