@gengage/assistant-fe 0.6.18 → 0.6.19

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.
Files changed (41) hide show
  1. package/dist/account-config-D_EMuR0g.js +1479 -0
  2. package/dist/agentic/context/context-store.d.ts +2 -1
  3. package/dist/agentic/index.d.ts +0 -1
  4. package/dist/agentic/index.js +155 -181
  5. package/dist/agentic/types.d.ts +6 -1
  6. package/dist/agentic/worker.d.ts +0 -2
  7. package/dist/agentic/worker.js +117 -415
  8. package/dist/agentic.iife.js +6 -6
  9. package/dist/{api-paths-CEDR4rld.js → api-paths-lIioK7-S.js} +1 -1
  10. package/dist/chat/components/InlineLauncher.d.ts +1 -1
  11. package/dist/chat-runtime.js +1 -1
  12. package/dist/chat.iife.js +1 -1
  13. package/dist/chat.js +1 -1
  14. package/dist/{common-BBgWfT-W.js → common-8WOyaL9-.js} +1 -1
  15. package/dist/common.js +5 -5
  16. package/dist/{connection-warning-B-doodzu.js → connection-warning-CBR0CWsY.js} +1 -1
  17. package/dist/{fastIntent-CloHPZQh.js → fastIntent-Dw79pmRK.js} +1 -1
  18. package/dist/index.js +10 -10
  19. package/dist/native.iife.js +1 -1
  20. package/dist/qna-runtime.js +1 -1
  21. package/dist/qna.iife.js +1 -1
  22. package/dist/qna.js +1 -1
  23. package/dist/{runtime-Ctqq0V6G.js → runtime-BsnZ6gt2.js} +3 -3
  24. package/dist/{runtime-BGCNkU1H.js → runtime-D5Sj-_tA.js} +3 -3
  25. package/dist/{runtime-CbWCU424.js → runtime-w0f2VkzL.js} +3 -3
  26. package/dist/{simbut-Dh4cJysK.js → simbut-BvdJtzCu.js} +1 -1
  27. package/dist/simbut.iife.js +1 -1
  28. package/dist/simbut.js +1 -1
  29. package/dist/{simrel-B-sjHbL5.js → simrel-DU6a27g9.js} +1 -1
  30. package/dist/simrel-runtime.js +1 -1
  31. package/dist/simrel.iife.js +1 -1
  32. package/dist/simrel.js +2 -2
  33. package/dist/{widget-base-CIJT5AiP.js → widget-base-BeWuIxD6.js} +1 -1
  34. package/package.json +4 -2
  35. package/dist/account-config-gj7_mF6Q.js +0 -1731
  36. package/dist/agentic/recipes/catalog-assistant-flow.d.ts +0 -50
  37. package/dist/agentic/recipes/input-text/actions.d.ts +0 -15
  38. package/dist/agentic/recipes/input-text/index.d.ts +0 -4
  39. package/dist/agentic/recipes/input-text/products.d.ts +0 -4
  40. package/dist/agentic/recipes/input-text/ranking.d.ts +0 -14
  41. package/dist/agentic/recipes/input-text/steps.d.ts +0 -31
@@ -1,1731 +0,0 @@
1
- import { t as Ae } from "./request-response-cache-BxRsKGJ0.js";
2
- function xe(e) {
3
- return !!e && typeof e == "object" && e.type === "rpc.result";
4
- }
5
- function _e(e) {
6
- return !!e && typeof e == "object" && e.type === "rpc.req";
7
- }
8
- function ur(e = self) {
9
- let t = 1;
10
- const r = /* @__PURE__ */ new Map();
11
- return e.addEventListener("message", (n) => {
12
- const o = n.data;
13
- if (!xe(o)) return;
14
- const i = r.get(o.rpcId);
15
- if (i) {
16
- if (r.delete(o.rpcId), o.ok === !1) {
17
- const a = typeof o.error == "object" ? o.error?.message : o.error;
18
- i.reject(new Error(a || "RPC failed"));
19
- return;
20
- }
21
- i.resolve(o.value);
22
- }
23
- }), function(o, i, a) {
24
- const c = t++;
25
- return e.postMessage({
26
- type: "rpc.req",
27
- rpcId: c,
28
- method: o,
29
- payload: i
30
- }, a || []), new Promise((s, u) => {
31
- r.set(c, {
32
- resolve: (l) => s(l),
33
- reject: u
34
- });
35
- });
36
- };
37
- }
38
- function lr({ worker: e, tools: t = {}, beacon: r, memory: n = sessionStorage }) {
39
- e.addEventListener("message", (o) => {
40
- const i = o.data;
41
- _e(i) && Te(e, i, {
42
- tools: t,
43
- beacon: r,
44
- memory: n
45
- });
46
- });
47
- }
48
- async function Te(e, t, r) {
49
- try {
50
- const n = await Ie(t.method, t.payload, r);
51
- e.postMessage({
52
- type: "rpc.result",
53
- rpcId: t.rpcId,
54
- ok: !0,
55
- value: n
56
- });
57
- } catch (n) {
58
- e.postMessage({
59
- type: "rpc.result",
60
- rpcId: t.rpcId,
61
- ok: !1,
62
- error: { message: n instanceof Error ? n.message : String(n) }
63
- });
64
- }
65
- }
66
- function je(e) {
67
- if (e && typeof e == "object" && typeof e.name == "string") return e;
68
- throw new Error("tool.invoke requires { name, input }");
69
- }
70
- function Ee(e) {
71
- if (e && typeof e == "object" && e.key !== void 0) return e;
72
- throw new Error("memory.get requires { key }");
73
- }
74
- function Oe(e) {
75
- if (e && typeof e == "object" && e.key !== void 0) return e;
76
- throw new Error("memory.set requires { key }");
77
- }
78
- async function Ie(e, t, { tools: r, beacon: n, memory: o }) {
79
- if (e === "tool.invoke") {
80
- const { name: i, input: a } = je(t), c = r[i];
81
- if (typeof c != "function") throw new Error(`Unknown tool: ${i}`);
82
- return c(a);
83
- }
84
- if (e === "beacon.send")
85
- return n?.(t), { sent: !0 };
86
- if (e === "memory.get") {
87
- const { key: i } = Ee(t), a = o.getItem(String(i));
88
- return a ? JSON.parse(a) : null;
89
- }
90
- if (e === "memory.set") {
91
- const { key: i, value: a } = Oe(t);
92
- return o.setItem(String(i), JSON.stringify(a ?? null)), { ok: !0 };
93
- }
94
- throw new Error(`Unknown RPC method: ${e}`);
95
- }
96
- function Pe() {
97
- return (/* @__PURE__ */ new Date()).toISOString();
98
- }
99
- function I(e) {
100
- return Math.max(0, Math.round(performance.now() - e));
101
- }
102
- function E(e) {
103
- return typeof e == "object" && e !== null && !Array.isArray(e);
104
- }
105
- function Ce(e) {
106
- const { accountId: t, locale: r = "en-GB", parentUrl: n = "", threadId: o, incomingContext: i } = e, a = E(i) ? i : {}, c = E(a.thread) ? a.thread : {}, s = E(a.panel) ? a.panel : {}, u = E(a.meta) ? a.meta : {}, l = oe(a.messages), f = {
107
- id: String(o || c.id || self.crypto.randomUUID()),
108
- started_at: String(c.started_at || Pe()),
109
- extensions: E(c.extensions) ? { ...c.extensions } : {}
110
- }, p = String(u.locale || r), d = String(u.parentUrl || n || typeof self < "u" && self.location?.href || "");
111
- return {
112
- panel: { ...s },
113
- messages: l,
114
- thread: f,
115
- meta: {
116
- locale: p,
117
- parentUrl: d,
118
- accountId: t
119
- }
120
- };
121
- }
122
- function Me(e, t) {
123
- const r = oe([t])[0];
124
- return r ? {
125
- ...e,
126
- messages: [...e.messages, r].slice(-50)
127
- } : e;
128
- }
129
- function oe(e) {
130
- return Array.isArray(e) ? e.map((t) => {
131
- const r = t?.role;
132
- return {
133
- role: r === "model" ? "assistant" : typeof r == "string" ? r : "",
134
- content: String(t?.content || "")
135
- };
136
- }).filter((t) => (t.role === "user" || t.role === "assistant") && !!t.content).slice(-50) : [];
137
- }
138
- var Be = "gengage:agent:context:";
139
- async function Ne({ accountId: e, threadId: t, rpc: r }) {
140
- const n = await r("memory.get", {
141
- tier: "session",
142
- key: ie(e, t)
143
- });
144
- return n && typeof n == "object" && !Array.isArray(n) ? n : {};
145
- }
146
- async function Re({ accountId: e, threadId: t, extensions: r, panel: n, rpc: o }) {
147
- await o("memory.set", {
148
- tier: "session",
149
- key: ie(e, t),
150
- value: {
151
- thread: { extensions: r },
152
- ...n && Object.keys(n).length > 0 ? { panel: n } : {}
153
- }
154
- });
155
- }
156
- function ie(e, t) {
157
- return `${Be}${e}:${t}`;
158
- }
159
- function g(e) {
160
- return typeof e == "object" && e !== null && !Array.isArray(e);
161
- }
162
- var fr = class {
163
- #e = /* @__PURE__ */ new Map();
164
- #t;
165
- #n;
166
- #o;
167
- #r;
168
- #i;
169
- constructor({ accountId: e, locale: t, parentUrl: r, rpc: n, persistentPanelKeys: o = [] }) {
170
- this.#t = e, this.#n = t, this.#o = r, this.#r = n, this.#i = [...new Set(o.filter(Boolean))];
171
- }
172
- async load(e) {
173
- const t = e?.meta?.threadId || e?.session_id || e?.sessionId, r = String(t || "default"), n = this.#e.get(r);
174
- if (n) return n;
175
- const o = await Ne({
176
- accountId: this.#t,
177
- threadId: r,
178
- rpc: this.#r
179
- }), i = Ce({
180
- accountId: this.#t,
181
- locale: e?.locale || this.#n,
182
- parentUrl: this.#o,
183
- threadId: r,
184
- incomingContext: $e(o, e?.context)
185
- });
186
- return this.#e.set(r, i), i;
187
- }
188
- patch(e, t) {
189
- const r = String(e || "default"), n = this.#e.get(r);
190
- if (!n) return null;
191
- const o = ae(n, typeof t == "function" ? t(n) : t);
192
- return this.#e.set(r, o), o;
193
- }
194
- appendUserMessage(e, t) {
195
- const r = String(e || "default");
196
- if (!t) return this.#e.get(r) ?? null;
197
- const n = this.#e.get(r);
198
- if (!n) return null;
199
- const o = Me(n, {
200
- role: "user",
201
- content: t
202
- });
203
- return this.#e.set(r, o), o;
204
- }
205
- async commit(e) {
206
- const t = String(e || "default"), r = this.#e.get(t);
207
- return r ? (await Re({
208
- accountId: this.#t,
209
- threadId: t,
210
- extensions: r.thread.extensions,
211
- panel: Ue(r.panel, this.#i),
212
- rpc: this.#r
213
- }), r) : null;
214
- }
215
- };
216
- function Ue(e, t) {
217
- if (!t.length || !g(e)) return;
218
- const r = {};
219
- for (const n of t) Object.prototype.hasOwnProperty.call(e, n) && (r[n] = e[n]);
220
- return r;
221
- }
222
- function $e(e, t) {
223
- return !g(e) && !g(t) ? {} : ae(g(e) ? e : {}, g(t) ? t : {});
224
- }
225
- function ae(e, t) {
226
- if (!g(t)) return e;
227
- const r = e, n = t, o = g(r.panel) ? r.panel : {}, i = g(n.panel) ? n.panel : {}, a = g(r.thread) ? r.thread : {}, c = g(n.thread) ? n.thread : {}, s = g(a.extensions) ? a.extensions : {}, u = g(c.extensions) ? c.extensions : {}, l = g(r.meta) ? r.meta : {}, f = g(n.meta) ? n.meta : {};
228
- return {
229
- ...e,
230
- ...t,
231
- panel: {
232
- ...o,
233
- ...i
234
- },
235
- thread: {
236
- ...a,
237
- ...c,
238
- extensions: {
239
- ...s,
240
- ...u
241
- }
242
- },
243
- meta: {
244
- ...l,
245
- ...f
246
- },
247
- messages: Array.isArray(n.messages) ? n.messages.slice(-50) : e.messages ?? []
248
- };
249
- }
250
- function ce(e) {
251
- const t = String.fromCharCode(...e);
252
- return btoa(t).replace(/\+/gu, "-").replace(/\//gu, "_").replace(/=+$/u, "");
253
- }
254
- function V(e) {
255
- return ce(new TextEncoder().encode(JSON.stringify(e)));
256
- }
257
- async function Fe({ accountId: e, devJwtSecret: t, ttlS: r = 300 }) {
258
- if (!t) throw new Error("devJwtSecret is required for local agent mode.");
259
- const n = Math.floor(Date.now() / 1e3), o = `${V({
260
- alg: "HS256",
261
- typ: "JWT"
262
- })}.${V({
263
- sub: e,
264
- iat: n,
265
- exp: n + r,
266
- scope: "invoke",
267
- jti: crypto.randomUUID?.() || `${n}-${Math.random()}`
268
- })}`, i = await crypto.subtle.importKey("raw", new TextEncoder().encode(t), {
269
- name: "HMAC",
270
- hash: "SHA-256"
271
- }, !1, ["sign"]), a = await crypto.subtle.sign("HMAC", i, new TextEncoder().encode(o));
272
- return `${o}.${ce(new Uint8Array(a))}`;
273
- }
274
- function De({ accountId: e, devJwtSecret: t, tokenBrokerUrl: r, tokenBrokerAudience: n, refreshSkewS: o = 30, fetchImpl: i = fetch }) {
275
- let a = null;
276
- return async ({ signal: c, parentUrl: s } = {}) => {
277
- if (t) return Fe({
278
- accountId: e,
279
- devJwtSecret: t
280
- });
281
- if (!r) throw new Error("tokenBrokerUrl is required for production agent mode.");
282
- const u = Math.floor(Date.now() / 1e3);
283
- return a?.token && a.expiresAtS - o > u || (a = await ze({
284
- accountId: e,
285
- tokenBrokerUrl: r,
286
- tokenBrokerAudience: n,
287
- parentUrl: s,
288
- signal: c,
289
- fetchImpl: i
290
- })), a.token;
291
- };
292
- }
293
- async function ze({ accountId: e, tokenBrokerUrl: t, tokenBrokerAudience: r, parentUrl: n, signal: o, fetchImpl: i }) {
294
- const a = await i(t, {
295
- method: "POST",
296
- credentials: "include",
297
- headers: {
298
- accept: "application/json",
299
- "content-type": "application/json"
300
- },
301
- body: JSON.stringify({
302
- accountId: e,
303
- scope: "invoke",
304
- audience: r || void 0,
305
- parentUrl: n || void 0
306
- }),
307
- ...o ? { signal: o } : {}
308
- });
309
- if (!a.ok) throw new Error(`Token broker request failed (${a.status}).`);
310
- const c = await a.json(), s = c?.token || c?.jwt || c?.access_token;
311
- if (!s || typeof s != "string") throw new Error("Token broker response did not include a JWT.");
312
- return {
313
- token: s,
314
- expiresAtS: Le(c) || Ke(s) || Math.floor(Date.now() / 1e3) + 300
315
- };
316
- }
317
- function Le(e) {
318
- const t = e?.expiresAtS ?? e?.expires_at_s ?? e?.expiresInS ?? e?.expires_in_s;
319
- if (Number.isFinite(t)) return Number(t);
320
- const r = e?.expiresIn ?? e?.expires_in;
321
- if (Number.isFinite(r)) return Math.floor(Date.now() / 1e3) + Number(r);
322
- const n = e?.expiresAt ?? e?.expires_at;
323
- if (typeof n == "string") {
324
- const o = Date.parse(n);
325
- if (Number.isFinite(o)) return Math.floor(o / 1e3);
326
- }
327
- return null;
328
- }
329
- function Ke(e) {
330
- try {
331
- const [, t] = e.split(".");
332
- if (!t) return null;
333
- const r = t.replace(/-/gu, "+").replace(/_/gu, "/"), n = atob(r.padEnd(Math.ceil(r.length / 4) * 4, "=")), o = JSON.parse(n);
334
- return Number.isFinite(o?.exp) ? Number(o.exp) : null;
335
- } catch {
336
- return null;
337
- }
338
- }
339
- var G = {
340
- unauthorized: {
341
- code: "auth",
342
- message: "Assistant authentication failed. Please retry."
343
- },
344
- forbidden: {
345
- code: "auth",
346
- message: "Assistant authentication failed. Please retry."
347
- },
348
- unknown_op: {
349
- code: "op_unavailable",
350
- message: "This assistant action is not available yet."
351
- },
352
- invalid_input: {
353
- code: "invalid_request",
354
- message: "The assistant request was not valid."
355
- },
356
- account_config: {
357
- code: "account_config",
358
- message: "Assistant configuration is unavailable."
359
- },
360
- upstream_llm: {
361
- code: "op_failed",
362
- message: "The assistant could not complete that request."
363
- },
364
- upstream_timeout: {
365
- code: "op_timeout",
366
- message: "The assistant took too long to respond."
367
- },
368
- schema_mismatch: {
369
- code: "op_failed",
370
- message: "The assistant could not complete that request."
371
- },
372
- rate_limited: {
373
- code: "rate_limited",
374
- message: "The assistant is receiving too many requests. Please retry shortly."
375
- },
376
- internal: {
377
- code: "op_failed",
378
- message: "The assistant could not complete that request."
379
- },
380
- payload_too_large: {
381
- code: "invalid_request",
382
- message: "The assistant request was too large."
383
- }
384
- }, L = class extends Error {
385
- constructor(e, t, r = {}) {
386
- super(t), this.name = "AgentError", this.code = e, this.source = r.source || "agent", r.sourceCode && (this.sourceCode = r.sourceCode), r.detail !== void 0 && (this.detail = r.detail);
387
- }
388
- };
389
- function se(e) {
390
- const t = String(e?.code || "upstream_llm"), r = G[t] || G.upstream_llm;
391
- return new L(r.code, r.message, {
392
- source: "be",
393
- sourceCode: t,
394
- detail: e?.detail
395
- });
396
- }
397
- function ve(e, t) {
398
- const r = t?.error && typeof t.error == "object" ? t.error : {};
399
- return se({
400
- code: String(r.code || qe(e)),
401
- detail: r.detail
402
- });
403
- }
404
- function dr(e) {
405
- return e instanceof L ? {
406
- type: "error",
407
- code: e.code,
408
- message: e.message
409
- } : {
410
- type: "error",
411
- code: "agent_invoke_failed",
412
- message: "The assistant could not complete that request."
413
- };
414
- }
415
- function qe(e) {
416
- return e === 401 ? "unauthorized" : e === 403 ? "forbidden" : e === 413 ? "payload_too_large" : e === 429 ? "rate_limited" : "internal";
417
- }
418
- async function mr({ beUrl: e, accountId: t, devJwtSecret: r, tokenBrokerUrl: n, tokenBrokerAudience: o, jwtProvider: i, parentUrl: a, op: c, input: s, cacheTtlS: u, cacheKey: l, browserCache: f, signal: p }) {
419
- const d = await (i || De({
420
- accountId: t,
421
- devJwtSecret: r,
422
- tokenBrokerUrl: n,
423
- tokenBrokerAudience: o
424
- }))({
425
- ...p ? { signal: p } : {},
426
- ...a ? { parentUrl: a } : {}
427
- }), { opDomain: h, opName: m } = Je(c, t), _ = {
428
- method: "POST",
429
- headers: {
430
- authorization: `Bearer ${d}`,
431
- "content-type": "application/json",
432
- accept: "application/x-ndjson"
433
- },
434
- body: JSON.stringify({
435
- op_domain: h,
436
- op_name: m,
437
- op_payload: s,
438
- ...u !== void 0 ? { cache_ttl_s: u } : {},
439
- ...l !== void 0 ? { cache_key: l } : {}
440
- }),
441
- ...p ? { signal: p } : {}
442
- }, Q = `${String(e).replace(/\/+$/u, "")}/v1/execute`, B = f === !1 ? await fetch(Q, _) : await Ae(Q, _, typeof f == "object" ? f : {});
443
- if (!B.ok) throw ve(B.status, await He(B));
444
- return Qe(B);
445
- }
446
- function Je(e, t) {
447
- if (!e || e.startsWith("-") || e.endsWith("-")) throw new Error(`Invalid BE op name: ${e}`);
448
- return {
449
- opDomain: We(t),
450
- opName: e
451
- };
452
- }
453
- function We(e) {
454
- return e.replace(/-agentic$/u, "");
455
- }
456
- async function He(e) {
457
- try {
458
- return await e.json();
459
- } catch {
460
- return { error: { code: e.statusText || "internal" } };
461
- }
462
- }
463
- async function Qe(e) {
464
- const t = e.body?.getReader();
465
- if (!t) throw new Error("BE response body is not readable.");
466
- const r = new TextDecoder();
467
- let n = "";
468
- const o = {};
469
- for (; ; ) {
470
- const { value: a, done: c } = await t.read();
471
- if (c) break;
472
- n += r.decode(a, { stream: !0 });
473
- let s = n.indexOf(`
474
- `);
475
- for (; s >= 0; ) {
476
- const u = n.slice(0, s).trim();
477
- n = n.slice(s + 1), u && Z(JSON.parse(u), o), s = n.indexOf(`
478
- `);
479
- }
480
- }
481
- const i = n.trim();
482
- return i && Z(JSON.parse(i), o), o;
483
- }
484
- function Z(e, t) {
485
- if (e._error) throw se(e._error);
486
- if (!e._end)
487
- for (const [r, n] of Object.entries(e)) {
488
- if (Object.prototype.hasOwnProperty.call(t, r)) throw new L("op_failed", `BE streamed duplicate field "${r}".`, {
489
- source: "be",
490
- sourceCode: "schema_mismatch"
491
- });
492
- t[r] = n;
493
- }
494
- }
495
- function pr(e = {}) {
496
- return {
497
- type: "metadata",
498
- ...e
499
- };
500
- }
501
- function ue(e, t = !1, r = {}) {
502
- return {
503
- type: "text_chunk",
504
- content: e,
505
- final: t,
506
- ...r
507
- };
508
- }
509
- function U(e) {
510
- return {
511
- type: "ui_spec",
512
- ...e
513
- };
514
- }
515
- function yr(e) {
516
- return {
517
- type: "action",
518
- action: e
519
- };
520
- }
521
- function Ve(e, t) {
522
- return {
523
- type: "error",
524
- code: e,
525
- message: t
526
- };
527
- }
528
- function le() {
529
- return { type: "done" };
530
- }
531
- function Ge(e, t) {
532
- return e[t?.type || t?.action?.type || "inputText"] || e.inputText;
533
- }
534
- async function gr({ request: e, accountModule: t, contextStore: r, beClient: n, toolBridge: o, emit: i, rpc: a, signal: c }) {
535
- const s = performance.now(), u = await r.load(e), l = u.thread.id, f = ut(e);
536
- r.appendUserMessage(l, f);
537
- const p = Ge(t.flows, e);
538
- if (!p)
539
- return i(Ve("unknown_action", `No agent flow for request type ${e?.type || "inputText"}`)), i(le()), {
540
- steps: 0,
541
- productSkusEmitted: []
542
- };
543
- const d = {
544
- request: e,
545
- context: r.patch(l, {}) || u,
546
- bag: {},
547
- accountConfig: t.accountConfig || {},
548
- threadId: l,
549
- steps: 0,
550
- productSkusEmitted: /* @__PURE__ */ new Set(),
551
- committed: !1
552
- }, h = {
553
- contextStore: r,
554
- beClient: n,
555
- toolBridge: o,
556
- emit: i,
557
- rpc: a,
558
- adapters: t.adapters || {},
559
- ...c ? { signal: c } : {}
560
- };
561
- return await C(p, d, h), d.committed || await K(d, h), await a("beacon.send", {
562
- type: "turnSummary",
563
- threadId: l,
564
- sessionId: e?.session_id || e?.sessionId || l,
565
- accountId: t.accountId || d.context.meta.accountId,
566
- steps: d.steps,
567
- totalLatencyMs: I(s),
568
- productSkusEmitted: [...d.productSkusEmitted]
569
- }), {
570
- steps: d.steps,
571
- productSkusEmitted: [...d.productSkusEmitted]
572
- };
573
- }
574
- async function C(e, t, r) {
575
- for (const n of e) {
576
- if (r.signal?.aborted) return;
577
- t.steps += 1, await Ze(n, t, r), t.context = r.contextStore.patch(t.threadId, {}) || t.context;
578
- }
579
- }
580
- async function Ze(e, t, r) {
581
- const n = q(t);
582
- if (e.kind === "be_op") {
583
- await Ye(e, t, r, n);
584
- return;
585
- }
586
- if (e.kind === "tool") {
587
- await Xe(e, t, r, n);
588
- return;
589
- }
590
- if (e.kind === "adapter") {
591
- await et(e, t, r, n);
592
- return;
593
- }
594
- if (e.kind === "emit") {
595
- tt(e, t, r);
596
- return;
597
- }
598
- if (e.kind === "branch") {
599
- await rt(e, t, r, n);
600
- return;
601
- }
602
- if (e.kind === "parallel") {
603
- await nt(e, t, r);
604
- return;
605
- }
606
- if (e.kind === "refusal") {
607
- await ot(e, t, r, n);
608
- return;
609
- }
610
- e.kind === "commit" && await it(e, t, r);
611
- }
612
- async function Ye(e, t, r, n) {
613
- const o = performance.now();
614
- let i;
615
- const a = x(e.cacheTtlS, n), c = x(e.cacheKey, n), s = x(e.browserCache, n);
616
- try {
617
- i = await r.beClient.invoke({
618
- op: e.op,
619
- input: x(e.input, n),
620
- ...a !== void 0 ? { cacheTtlS: a } : {},
621
- ...c !== void 0 ? { cacheKey: c } : {},
622
- ...s !== void 0 ? { browserCache: s } : {},
623
- ...r.signal ? { signal: r.signal } : {}
624
- });
625
- } catch (l) {
626
- const f = l, p = String(f?.sourceCode || f?.code || "unknown"), d = {
627
- type: "agentOp",
628
- threadId: t.threadId,
629
- sessionId: t.request?.session_id || t.request?.sessionId || t.threadId,
630
- accountId: t.context.meta.accountId,
631
- op: e.op,
632
- status: "error",
633
- latencyMs: I(o),
634
- errorCode: p
635
- };
636
- if (await r.rpc("beacon.send", d), e.onError) {
637
- e.errorOut && (t.bag[e.errorOut] = l), await C(e.onError, t, r);
638
- return;
639
- }
640
- throw l;
641
- }
642
- e.out && (t.bag[e.out] = i), v(e, i, t, r.contextStore);
643
- const u = {
644
- type: "agentOp",
645
- threadId: t.threadId,
646
- sessionId: t.request?.session_id || t.request?.sessionId || t.threadId,
647
- accountId: t.context.meta.accountId,
648
- op: e.op,
649
- status: "ok",
650
- latencyMs: I(o)
651
- };
652
- await r.rpc("beacon.send", u);
653
- }
654
- async function Xe(e, t, r, n) {
655
- const o = performance.now();
656
- try {
657
- const i = await r.toolBridge.invoke(e.name, x(e.input, n));
658
- e.out && (t.bag[e.out] = i), v(e, i, t, r.contextStore), await r.rpc("beacon.send", {
659
- type: "agentTool",
660
- threadId: t.threadId,
661
- sessionId: Y(t),
662
- accountId: t.context.meta.accountId,
663
- tool: e.name,
664
- status: "ok",
665
- latencyMs: I(o)
666
- });
667
- } catch (i) {
668
- const a = i;
669
- if (await r.rpc("beacon.send", {
670
- type: "agentTool",
671
- threadId: t.threadId,
672
- sessionId: Y(t),
673
- accountId: t.context.meta.accountId,
674
- tool: e.name,
675
- status: "error",
676
- latencyMs: I(o),
677
- errorCode: String(a?.code || a?.name || "unknown")
678
- }), e.onError) {
679
- e.errorOut && (t.bag[e.errorOut] = i), await C(e.onError, t, r);
680
- return;
681
- }
682
- throw i;
683
- }
684
- }
685
- async function et(e, t, r, n) {
686
- const o = r.adapters?.[e.name];
687
- if (typeof o != "function") throw new Error(`Unknown flow adapter: ${e.name}`);
688
- const i = await o(x(e.input, n), n);
689
- e.out && (t.bag[e.out] = i), v(e, i, t, r.contextStore);
690
- }
691
- function tt(e, t, r) {
692
- const n = e.build(q(t));
693
- lt(n, t.productSkusEmitted), r.emit(n);
694
- }
695
- async function rt(e, t, r, n) {
696
- const o = String(x(e.on, n) || "default");
697
- await C(e.cases[o] || e.cases.default || [], t, r);
698
- }
699
- async function nt(e, t, r) {
700
- const n = e.steps.map((a) => {
701
- const c = {
702
- ...t,
703
- context: at(t.context),
704
- bag: { ...t.bag },
705
- productSkusEmitted: t.productSkusEmitted,
706
- committed: !1
707
- };
708
- return C(a, c, {
709
- ...r,
710
- contextStore: ct(c)
711
- }).then(() => c);
712
- }), o = await Promise.all(n), i = e.merge || {};
713
- for (const a of o) {
714
- for (const c of i.bag || []) Object.prototype.hasOwnProperty.call(a.bag, c) && (t.bag[c] = a.bag[c]);
715
- if (i.panel?.length) {
716
- const c = X(a.context.panel, i.panel);
717
- Object.keys(c).length && (t.context = r.contextStore.patch(t.threadId, { panel: c }) || t.context);
718
- }
719
- if (i.threadExtensions?.length) {
720
- const c = X(a.context.thread.extensions, i.threadExtensions);
721
- Object.keys(c).length && (t.context = r.contextStore.patch(t.threadId, { thread: {
722
- ...t.context.thread,
723
- extensions: c
724
- } }) || t.context);
725
- }
726
- }
727
- }
728
- async function ot(e, t, r, n) {
729
- r.emit(ue(x(e.message, n), !0)), await K(t, r);
730
- }
731
- async function it(e, t, r) {
732
- await K(t, r);
733
- }
734
- async function K(e, { contextStore: t, emit: r }) {
735
- e.committed || (e.context = await t.commit(e.threadId) || e.context, e.committed = !0, r(le()));
736
- }
737
- function v(e, t, r, n) {
738
- if (typeof e.patch != "function") return;
739
- const o = e.patch(r.context, t, q(r));
740
- o && (r.context = n.patch(r.threadId, o) || r.context);
741
- }
742
- function q(e) {
743
- return {
744
- request: e.request,
745
- context: e.context,
746
- bag: e.bag,
747
- accountConfig: e.accountConfig
748
- };
749
- }
750
- function Y(e) {
751
- return e.request?.session_id || e.request?.sessionId || e.threadId;
752
- }
753
- function at(e) {
754
- return typeof structuredClone == "function" ? structuredClone(e) : JSON.parse(JSON.stringify(e));
755
- }
756
- function X(e, t) {
757
- const r = {};
758
- for (const n of t) Object.prototype.hasOwnProperty.call(e, n) && (r[n] = e[n]);
759
- return r;
760
- }
761
- function ct(e) {
762
- return {
763
- async load() {
764
- return e.context;
765
- },
766
- patch(t, r) {
767
- return e.context = st(e.context, r), e.context;
768
- },
769
- appendUserMessage() {
770
- return e.context;
771
- },
772
- async commit() {
773
- return e.context;
774
- }
775
- };
776
- }
777
- function st(e, t) {
778
- return !t || typeof t != "object" ? e : {
779
- ...e,
780
- ...t,
781
- panel: {
782
- ...e.panel || {},
783
- ...t.panel || {}
784
- },
785
- thread: {
786
- ...e.thread || {},
787
- ...t.thread || {},
788
- extensions: {
789
- ...e.thread?.extensions || {},
790
- ...t.thread?.extensions || {}
791
- }
792
- },
793
- meta: {
794
- ...e.meta || {},
795
- ...t.meta || {}
796
- },
797
- messages: Array.isArray(t.messages) ? t.messages.slice(-50) : e.messages
798
- };
799
- }
800
- function x(e, t) {
801
- return typeof e == "function" ? e(t) : e;
802
- }
803
- function ut(e) {
804
- const t = e?.payload;
805
- if (typeof t == "string") return t;
806
- if (t && typeof t == "object" && "text" in t) {
807
- const r = t.text;
808
- if (typeof r == "string") return r;
809
- }
810
- return typeof e?.action?.payload == "string" ? e.action.payload : typeof e?.action?.title == "string" ? e.action.title : "";
811
- }
812
- function lt(e, t) {
813
- const r = e, n = r.spec, o = Array.isArray(n?.items) ? n.items : void 0, i = Array.isArray(r.items) ? r.items : void 0, a = o || i;
814
- if (Array.isArray(a)) {
815
- for (const c of a) if (c && typeof c == "object") {
816
- const s = c.sku;
817
- typeof s == "string" && t.add(s);
818
- }
819
- }
820
- }
821
- function hr(e) {
822
- return { invoke(t, r) {
823
- return e("tool.invoke", {
824
- name: t,
825
- input: r
826
- });
827
- } };
828
- }
829
- function fe(e) {
830
- return e && typeof e == "object" && !Array.isArray(e) ? e : null;
831
- }
832
- function J(e) {
833
- const t = fe(e);
834
- if (!t) return null;
835
- const r = T(t.sku, t.SKU);
836
- if (!r) return null;
837
- const n = {
838
- sku: r,
839
- name: T(t.name, t.title, t.short_name, r) || r,
840
- url: T(t.url) || ""
841
- }, o = Array.isArray(t.images) ? t.images : void 0, i = T(t.imageUrl, t.image_url, t.image, o?.[0]);
842
- i && (n.imageUrl = i), o && o.length > 1 && (n.images = o.filter((m) => !!m).map(String));
843
- const a = O(t.price_discounted), c = O(t.price), s = a || c;
844
- s > 0 && (n.price = String(s));
845
- const u = a > 0 ? c : 0;
846
- u > 0 && (n.originalPrice = String(u));
847
- const l = T(t.brand);
848
- l && (n.brand = l);
849
- const f = O(t.rating);
850
- f > 0 && (n.rating = f);
851
- const p = O(t.review_count) || O(t.reviewCount);
852
- p > 0 && (n.reviewCount = p);
853
- const d = T(t.cart_code, t.cartCode);
854
- d && (n.cartCode = d), typeof t.in_stock == "boolean" && (n.inStock = t.in_stock), typeof t.inStock == "boolean" && (n.inStock = t.inStock);
855
- const h = t.category_names;
856
- return Array.isArray(h) && (n.categoryNames = h.map(String)), n;
857
- }
858
- function W(e) {
859
- const t = fe(e);
860
- if (!t) return e;
861
- const r = t.category_names, n = Array.isArray(r) ? r : void 0, o = t.images, i = Array.isArray(o) ? o : void 0, a = {
862
- sku: t.sku,
863
- name: t.name || t.title,
864
- url: t.url,
865
- price: t.price,
866
- currency: t.price_currency || t.currency,
867
- category: n?.[n.length - 1] ?? void 0,
868
- category_names: n ? n.slice(0, 4) : void 0,
869
- image: i ? i[0] : t.image,
870
- in_stock: t.in_stock,
871
- rating: t.rating,
872
- review_count: t.review_count
873
- };
874
- for (const c of Object.keys(a)) a[c] === void 0 && delete a[c];
875
- return a;
876
- }
877
- function br(e) {
878
- return Array.isArray(e) ? e.map(W).filter((t) => !!t) : [];
879
- }
880
- function T(...e) {
881
- for (const t of e) if (typeof t == "string" && t.trim()) return t.trim();
882
- return "";
883
- }
884
- function O(e) {
885
- const t = Number(e);
886
- return Number.isFinite(t) ? t : 0;
887
- }
888
- function ee(e) {
889
- return Array.isArray(e) ? e.map(String).filter(Boolean) : typeof e == "string" && e ? [e] : [];
890
- }
891
- function kr(e, t, r = {}) {
892
- return `${e}:${r.version || "v2"}:${de(t)}`;
893
- }
894
- function wr(e, t = {}) {
895
- return me(e).map((r) => pe(typeof r == "string" ? r : r.sku, t.locale)).filter(Boolean).filter((r, n, o) => o.indexOf(r) === n).sort((r, n) => H(r, n, t.locale)).slice(0, t.limit || 16).join(",");
896
- }
897
- function Sr(e, t = {}) {
898
- return de(me(e).map((r) => ft(r, t)).filter((r) => !!r).sort((r, n) => H(String(r.sku || r.name || ""), String(n.sku || n.name || ""), t.locale)).slice(0, t.limit || 16));
899
- }
900
- function Ar(e, t) {
901
- return A(e, t);
902
- }
903
- function de(e) {
904
- let t = 2166136261;
905
- const r = dt(e);
906
- for (let n = 0; n < r.length; n += 1)
907
- t ^= r.charCodeAt(n), t = Math.imul(t, 16777619);
908
- return (t >>> 0).toString(36);
909
- }
910
- function ft(e, t) {
911
- if (!e || typeof e != "object" || Array.isArray(e)) return null;
912
- const r = e, n = t.mapProduct?.(r);
913
- if (n) return P(n);
914
- const o = W(r);
915
- return o && typeof o == "object" && !Array.isArray(o) ? P(mt(o, t)) : null;
916
- }
917
- function me(e) {
918
- return Array.isArray(e) ? e.filter((t) => typeof t == "string" || !!(t && typeof t == "object" && !Array.isArray(t))) : [];
919
- }
920
- function dt(e) {
921
- return JSON.stringify(P(e));
922
- }
923
- function P(e) {
924
- return Array.isArray(e) ? e.map(P) : !e || typeof e != "object" ? e : Object.fromEntries(Object.entries(e).filter(([, t]) => t !== void 0).sort(([t], [r]) => t.localeCompare(r)).map(([t, r]) => [t, P(r)]));
925
- }
926
- function mt(e, t) {
927
- return {
928
- sku: pe(e.sku, t.locale),
929
- name: A(e.name, t.locale),
930
- url: A(e.url, t.locale),
931
- price: e.price,
932
- currency: A(e.currency, t.locale),
933
- category: A(e.category, t.locale),
934
- category_names: pt(e.category_names, t.locale, 16),
935
- image: A(e.image, t.locale),
936
- in_stock: e.in_stock,
937
- rating: e.rating,
938
- review_count: e.review_count
939
- };
940
- }
941
- function pe(e, t) {
942
- return A(e, t).toUpperCase();
943
- }
944
- function A(e, t) {
945
- return typeof e == "string" ? e.trim().replace(/\s+/gu, " ").toLocaleLowerCase(t || void 0) : "";
946
- }
947
- function H(e, t, r) {
948
- return e.localeCompare(t, r, { numeric: !0 });
949
- }
950
- function pt(e, t, r) {
951
- return Array.isArray(e) ? e.map((n) => A(n, t)).filter(Boolean).sort((n, o) => H(n, o, t)).slice(0, r) : void 0;
952
- }
953
- function xr(e, t = {}) {
954
- if (!Array.isArray(e)) return [];
955
- const r = t.maxItems ?? 8, n = t.maxFeatures ?? 8;
956
- return e.map((o) => yt(o, t.defaultBrand, n)).filter((o) => !!o).slice(0, r);
957
- }
958
- function yt(e, t, r) {
959
- if (!e || typeof e != "object" || Array.isArray(e)) return null;
960
- const n = e, o = W(n);
961
- return o ? bt({
962
- sku: o.sku,
963
- name: o.name,
964
- url: o.url,
965
- image: o.image,
966
- price: o.price,
967
- currency: o.currency,
968
- category: o.category,
969
- category_names: o.category_names,
970
- brand: n.brand || t,
971
- features: gt(n.features, r),
972
- specs: ht(n.specs, r),
973
- in_stock: o.in_stock
974
- }) : null;
975
- }
976
- function gt(e, t) {
977
- return Array.isArray(e) ? e.filter(Boolean).slice(0, t) : void 0;
978
- }
979
- function ht(e, t) {
980
- if (!(!e || typeof e != "object" || Array.isArray(e)))
981
- return Object.fromEntries(Object.entries(e).filter(([, r]) => r != null).slice(0, t));
982
- }
983
- function bt(e) {
984
- return Object.fromEntries(Object.entries(e).filter(([, t]) => t != null));
985
- }
986
- function ye(e) {
987
- if (!e || typeof e != "object" || Array.isArray(e)) return {};
988
- const t = e, r = Array.isArray(t.rows) ? t.rows : [];
989
- return {
990
- ...t,
991
- rows: r.map(wt).filter(Boolean)
992
- };
993
- }
994
- function kt(e, t) {
995
- const r = Array.isArray(e) ? e.filter((c) => !!(c && typeof c == "object" && !Array.isArray(c))) : [], n = ye(t), o = Array.isArray(n.columns) ? n.columns.map(String) : [];
996
- if (!o.length) return r;
997
- const i = new Map(r.map((c) => [String(c.sku || c.id || ""), c])), a = o.map((c) => i.get(c)).filter((c) => !!c);
998
- return a.length ? a : r;
999
- }
1000
- function _r(e, t) {
1001
- const r = ye(e?.table), n = kt(t, r);
1002
- return {
1003
- ...e || {},
1004
- table: r,
1005
- products: n,
1006
- multiple_product_details: n
1007
- };
1008
- }
1009
- function wt(e) {
1010
- return !e || typeof e != "object" || Array.isArray(e) ? null : Object.fromEntries(Object.entries(e).filter(([, t]) => t !== void 0));
1011
- }
1012
- function St(e = []) {
1013
- const t = {}, r = [];
1014
- return (Array.isArray(e) ? e.map(J).filter((n) => !!n) : []).forEach((n, o) => {
1015
- const i = `product-${o.toString()}`;
1016
- r.push(i);
1017
- const a = {
1018
- sku: n.sku,
1019
- product: n
1020
- };
1021
- t[i] = {
1022
- type: "ProductCard",
1023
- props: {
1024
- product: n,
1025
- index: o,
1026
- action: {
1027
- title: n.name,
1028
- type: "launchSingleProduct",
1029
- payload: a
1030
- }
1031
- }
1032
- };
1033
- }), t.root = {
1034
- type: "ProductGrid",
1035
- props: { layout: "grid" },
1036
- children: r
1037
- }, {
1038
- widget: "chat",
1039
- panelHint: "panel",
1040
- spec: {
1041
- root: "root",
1042
- elements: t
1043
- }
1044
- };
1045
- }
1046
- function At(e) {
1047
- return {
1048
- widget: "chat",
1049
- panelHint: "panel",
1050
- spec: {
1051
- root: "root",
1052
- elements: { root: {
1053
- type: "ProductDetailsPanel",
1054
- props: { product: J(e) || e || {} }
1055
- } }
1056
- }
1057
- };
1058
- }
1059
- function xt(e = {}) {
1060
- const t = (e.multiple_product_details || e.products || []).map(J).filter((n) => !!n), r = e.table || {};
1061
- return {
1062
- widget: "chat",
1063
- panelHint: "panel",
1064
- spec: {
1065
- root: "root",
1066
- elements: { root: {
1067
- type: "ComparisonTable",
1068
- props: {
1069
- products: t,
1070
- recommended: t.find((n) => n.sku === e.recommended_choice_sku) || t[0],
1071
- attributes: Object.entries(r).map(([n, o]) => ({
1072
- label: n,
1073
- values: Array.isArray(o) ? o.map(String) : [String(o ?? "")]
1074
- })),
1075
- highlights: ee(e.key_differences),
1076
- specialCases: ee(e.special_considerations),
1077
- recommendedText: e.recommended_choice,
1078
- productActions: Object.fromEntries(t.map((n) => [n.sku, {
1079
- title: n.name,
1080
- type: "launchSingleProduct",
1081
- payload: {
1082
- sku: n.sku,
1083
- product: n
1084
- }
1085
- }]))
1086
- }
1087
- } }
1088
- }
1089
- };
1090
- }
1091
- function _t(e = []) {
1092
- return {
1093
- widget: "chat",
1094
- panelHint: "inline",
1095
- spec: {
1096
- root: "root",
1097
- elements: { root: {
1098
- type: "ActionButtons",
1099
- props: { buttons: (Array.isArray(e) ? e : []).map((t) => {
1100
- const r = t.label || t.title || t.shortName || "", n = t.action || t.requestDetails || t.request_details;
1101
- return !r || !n?.type ? null : {
1102
- label: r,
1103
- action: {
1104
- title: r,
1105
- type: n.type,
1106
- ...n.payload !== void 0 ? { payload: n.payload } : {}
1107
- }
1108
- };
1109
- }).filter((t) => !!t) }
1110
- } }
1111
- }
1112
- };
1113
- }
1114
- function Tr(e) {
1115
- return Object.freeze([...e]);
1116
- }
1117
- var te = /* @__PURE__ */ new Set([
1118
- "and",
1119
- "are",
1120
- "for",
1121
- "from",
1122
- "the",
1123
- "with"
1124
- ]), Tt = {
1125
- token: 0.26,
1126
- keyword: 0.22,
1127
- category: 0.18,
1128
- facet: 0.16,
1129
- color: 0.08,
1130
- attribute: 0.06,
1131
- price: 0.08,
1132
- retrieval: 0.08
1133
- }, jt = [
1134
- "amber",
1135
- "beige",
1136
- "black",
1137
- "blue",
1138
- "blush",
1139
- "bronze",
1140
- "brown",
1141
- "cream",
1142
- "gold",
1143
- "green",
1144
- "grey",
1145
- "ivory",
1146
- "lilac",
1147
- "mixed",
1148
- "orange",
1149
- "pastel",
1150
- "pink",
1151
- "purple",
1152
- "red",
1153
- "silver",
1154
- "white",
1155
- "yellow"
1156
- ];
1157
- function w(e) {
1158
- return String(e ?? "").normalize("NFKD").replace(new RegExp("\\p{Diacritic}", "gu"), "").toLowerCase().replace(/[^\p{L}\p{N}]+/gu, " ").replace(/\s+/gu, " ").trim();
1159
- }
1160
- function b(e, t = te) {
1161
- const r = t instanceof Set ? t : /* @__PURE__ */ new Set([...te, ...t]), n = /* @__PURE__ */ new Set(), o = [];
1162
- for (const i of w(e).split(/\s+/u))
1163
- i.length < 3 || r.has(i) || n.has(i) || (n.add(i), o.push(i));
1164
- return o;
1165
- }
1166
- function $(e, t = {}) {
1167
- if (Ot(e)) return e;
1168
- const r = t.facetKeys, n = y(e.category_names ?? e.category), o = y(e.category_ids), i = It(e.facet_hits), a = Object.entries(i).filter(([m]) => !r || r.some((_) => w(_) === w(m))).map(([, m]) => m), c = k(e.name, e.title), s = k(e.brand), u = t.sourceKeywords ?? [], l = [
1169
- c,
1170
- s,
1171
- ...n,
1172
- ...a,
1173
- ...y(e.promotions),
1174
- k(e.description),
1175
- ...u
1176
- ].join(" "), f = b(l, t.stopWords), p = k(t.sourceProductType, Mt(n, c, f), f[0]), d = S([...Ct(l, jt), ...Object.entries(i).filter(([m]) => /colou?r/iu.test(m)).flatMap(([, m]) => b(m, t.stopWords))]), h = S([...a.flatMap((m) => b(m, t.stopWords)), ...y(e.promotions).flatMap((m) => b(m, t.stopWords))]).slice(0, 24);
1177
- return {
1178
- sku: k(e.sku),
1179
- title: c,
1180
- brand: s,
1181
- productType: p,
1182
- categoryNames: n,
1183
- categoryIds: o,
1184
- facets: i,
1185
- keywords: S([...u.flatMap((m) => b(m, t.stopWords)), ...f]).slice(0, 12),
1186
- tokens: f,
1187
- colors: d,
1188
- attributes: h,
1189
- price: Pt(e)
1190
- };
1191
- }
1192
- function jr(e, t = {}) {
1193
- const r = k(e.product_type, e.productType), n = k(e.title, r, y(e.keywords).join(" ")), o = S([
1194
- ...y(e.keywords),
1195
- ...y(e.colors),
1196
- ...y(e.materials),
1197
- ...y(e.styles),
1198
- ...y(e.attributes)
1199
- ]);
1200
- return $({
1201
- sku: "__photo__",
1202
- name: n,
1203
- category_names: r ? [r] : [],
1204
- facet_hits: {
1205
- colors: y(e.colors).join(", "),
1206
- materials: y(e.materials).join(", "),
1207
- styles: y(e.styles).join(", "),
1208
- attributes: y(e.attributes).join(", ")
1209
- },
1210
- promotions: o
1211
- }, {
1212
- ...t,
1213
- sourceProductType: r,
1214
- sourceKeywords: o
1215
- });
1216
- }
1217
- function Er(e, t = {}) {
1218
- const r = $(e, t);
1219
- return S([
1220
- ...t.sourceQueries ?? [],
1221
- N([
1222
- r.productType,
1223
- ...r.colors.slice(0, 1),
1224
- ...r.attributes.slice(0, 2)
1225
- ]),
1226
- N([r.productType, ...r.keywords.slice(0, 3)]),
1227
- N([r.categoryNames[r.categoryNames.length - 1], ...r.keywords.slice(0, 2)]),
1228
- N(r.title.split(/\s+/u).slice(0, 6)),
1229
- r.productType
1230
- ].map((n) => n.trim()).filter(Boolean)).slice(0, t.queryLimit ?? 6);
1231
- }
1232
- function Et(e, t, r = {}) {
1233
- const n = {
1234
- ...Tt,
1235
- ...r.weights
1236
- }, o = $(t, r), i = [], a = j(e.tokens, o.tokens), c = j(e.keywords, o.tokens), s = Bt(e.categoryNames, o.categoryNames) || j(re(e.categoryNames), re(o.categoryNames)), u = j(e.attributes, o.attributes), l = j(e.colors, o.colors), f = j(e.attributes, o.tokens), p = Nt(e.price, o.price), d = Rt(o.sku, r.hitCounts) > 0 ? 1 : 0, h = Ut(e, o), m = a * n.token + c * n.keyword + s * n.category + u * n.facet + l * n.color + f * n.attribute + p * n.price + d * n.retrieval, _ = $t(h ? m : m * 0.35);
1237
- return h && e.productType && e.productType === o.productType && i.push("same product type"), s > 0.4 && i.push("similar category"), u > 0.2 && i.push("matching product attributes"), l > 0 && i.push("matching colour"), p > 0.8 && i.push("similar price"), h || i.push("weaker product-type match"), {
1238
- score: Number(_.toFixed(4)),
1239
- reasons: i
1240
- };
1241
- }
1242
- function Or(e, t, r = {}) {
1243
- const n = $(e, r), o = new Set([n.sku, ...r.ignoreSkus ?? []].filter(Boolean));
1244
- return t.filter((i) => {
1245
- const a = k(i.sku);
1246
- return a && !o.has(a);
1247
- }).map((i) => {
1248
- const a = Et(n, i, r);
1249
- return {
1250
- ...i,
1251
- similarity_score: a.score,
1252
- similarity_reasons: a.reasons
1253
- };
1254
- }).sort((i, a) => a.similarity_score - i.similarity_score).slice(0, Math.max(1, Math.min(r.limit ?? 12, 100)));
1255
- }
1256
- function Ot(e) {
1257
- return !!(e && typeof e == "object" && Array.isArray(e.tokens));
1258
- }
1259
- function k(...e) {
1260
- for (const t of e) if (typeof t == "string" && t.trim()) return t.trim();
1261
- return "";
1262
- }
1263
- function y(e) {
1264
- return Array.isArray(e) ? e.map((t) => k(t)).filter(Boolean) : typeof e == "string" && e.trim() ? e.split(/[,;/|]+/u).map((t) => t.trim()).filter(Boolean) : [];
1265
- }
1266
- function It(e) {
1267
- return !e || typeof e != "object" || Array.isArray(e) ? {} : Object.fromEntries(Object.entries(e).map(([t, r]) => [t, k(r)]).filter(([, t]) => t));
1268
- }
1269
- function Pt(e) {
1270
- const t = Number(e.price_discounted);
1271
- if (Number.isFinite(t) && t > 0) return t;
1272
- const r = Number(e.price);
1273
- return Number.isFinite(r) && r > 0 ? r : 0;
1274
- }
1275
- function S(e) {
1276
- const t = /* @__PURE__ */ new Set(), r = [];
1277
- for (const n of e) {
1278
- const o = w(n);
1279
- !o || t.has(o) || (t.add(o), r.push(o));
1280
- }
1281
- return r;
1282
- }
1283
- function Ct(e, t) {
1284
- const r = ` ${w(e)} `;
1285
- return t.filter((n) => r.includes(` ${w(n)} `));
1286
- }
1287
- function Mt(e, t, r) {
1288
- const n = w(e[e.length - 1] ?? e[0] ?? "");
1289
- if (n) {
1290
- const o = b(n), i = new Set(b(t));
1291
- return o.find((a) => i.has(a)) ?? o[0] ?? n;
1292
- }
1293
- return r[0] ?? "";
1294
- }
1295
- function N(e) {
1296
- return e.map((t) => w(t)).filter(Boolean).join(" ").replace(/\s+/gu, " ").trim().slice(0, 120);
1297
- }
1298
- function re(e) {
1299
- return e.flatMap((t) => b(t));
1300
- }
1301
- function j(e, t) {
1302
- const r = S(e), n = new Set(S(t));
1303
- return r.length === 0 || n.size === 0 ? 0 : r.filter((o) => n.has(o)).length / r.length;
1304
- }
1305
- function Bt(e, t) {
1306
- const r = S(e), n = new Set(S(t));
1307
- return r.length === 0 || n.size === 0 ? 0 : r.filter((o) => n.has(o)).length / r.length;
1308
- }
1309
- function Nt(e, t) {
1310
- return e <= 0 || t <= 0 ? 0 : Math.min(e, t) / Math.max(e, t);
1311
- }
1312
- function Rt(e, t) {
1313
- return !e || !t ? 0 : typeof t.get == "function" ? t.get(e) ?? 0 : t[e] ?? 0;
1314
- }
1315
- function Ut(e, t) {
1316
- if (!e.productType || !t.productType || e.productType === t.productType) return !0;
1317
- const r = new Set(b(e.productType)), n = /* @__PURE__ */ new Set([...b(t.productType), ...t.tokens]);
1318
- return [...r].some((o) => n.has(o));
1319
- }
1320
- function $t(e) {
1321
- return Math.max(0, Math.min(1, e));
1322
- }
1323
- function Ir(e, t = {}) {
1324
- if (!Array.isArray(e)) return [];
1325
- const r = e.filter((n) => !!(n && typeof n == "object" && !Array.isArray(n))).map((n, o) => ({
1326
- product: n,
1327
- index: o,
1328
- score: Ft(n, t)
1329
- })).sort((n, o) => o.score - n.score || n.index - o.index).map(({ product: n }) => n);
1330
- return typeof t.limit == "number" ? r.slice(0, t.limit) : r;
1331
- }
1332
- function Ft(e, t = {}) {
1333
- const r = Dt(t), n = ge(e);
1334
- let o = 0;
1335
- for (const i of r) n.includes(i) && (o += i.length > 3 ? 3 : 1);
1336
- return zt(e) && (o += 2), Lt(e) && (o += 2), Kt(e) && (o += 1), e.in_stock === !1 && (o -= 5), o;
1337
- }
1338
- function Dt(e = {}) {
1339
- const t = [
1340
- e.query,
1341
- e.searchInput?.brands,
1342
- e.searchInput?.categories,
1343
- e.searchInput?.features,
1344
- e.searchInput?.keywords,
1345
- e.intent?.brands,
1346
- e.intent?.categories,
1347
- e.intent?.keywords
1348
- ];
1349
- return [...new Set(t.flatMap(qt))];
1350
- }
1351
- function zt(e) {
1352
- return e.price != null || e.price_discounted != null || e.priceText != null;
1353
- }
1354
- function Lt(e) {
1355
- return !!(e.image || e.imageUrl || Array.isArray(e.images) && e.images[0]);
1356
- }
1357
- function Kt(e) {
1358
- return !!(e.category || e.category_names || e.features || e.specs || e.facet_hits);
1359
- }
1360
- function ge(e) {
1361
- return w([
1362
- e.name,
1363
- e.title,
1364
- e.brand,
1365
- e.category,
1366
- e.category_names,
1367
- e.features,
1368
- e.specs,
1369
- e.facet_hits
1370
- ].flatMap(R).join(" "));
1371
- }
1372
- function vt(e) {
1373
- return w(typeof e == "string" ? e : e == null ? "" : String(e));
1374
- }
1375
- function qt(e) {
1376
- return R(e).join(" ").split(/\s+/u).map(vt).filter((t) => t.length > 1);
1377
- }
1378
- function R(e) {
1379
- return Array.isArray(e) ? e.flatMap(R) : e && typeof e == "object" ? Object.values(e).flatMap(R) : e == null ? [] : [String(e)];
1380
- }
1381
- function z(e) {
1382
- if (!Array.isArray(e)) return [];
1383
- const t = /* @__PURE__ */ new Set(), r = [];
1384
- for (const n of e) {
1385
- if (!n || typeof n != "object" || Array.isArray(n)) continue;
1386
- const o = n, i = String(o.sku || o.url || o.name || "");
1387
- !i || t.has(i) || (t.add(i), r.push(o));
1388
- }
1389
- return r;
1390
- }
1391
- function Pr(e, t) {
1392
- const r = z(e);
1393
- if (!t || typeof t != "object" || Array.isArray(t)) return r;
1394
- const n = Object.values(t).flatMap((o) => Array.isArray(o) ? o : [o]).map((o) => String(o || "").trim().toLocaleLowerCase()).filter(Boolean);
1395
- return n.length ? r.filter((o) => {
1396
- const i = ge(o);
1397
- return n.some((a) => i.includes(a));
1398
- }) : r;
1399
- }
1400
- function Cr(e, t, r = 8) {
1401
- const n = z(e), o = z(t), i = new Set(o.map((a) => String(a.sku || a.url || a.name || "")));
1402
- return n.map((a, c) => ({
1403
- product: a,
1404
- index: c,
1405
- score: Jt(a, i)
1406
- })).sort((a, c) => c.score - a.score || a.index - c.index).map(({ product: a }) => a).slice(0, r);
1407
- }
1408
- function Jt(e, t) {
1409
- const r = String(e.sku || e.url || e.name || "");
1410
- return t.has(r) ? 10 : 0;
1411
- }
1412
- function Wt(e, t = {}) {
1413
- const r = e.sku, n = t.labels || {};
1414
- return [
1415
- t.includeCompare !== !1 && r ? D(n.compare || "Compare", {
1416
- type: "compare",
1417
- payload: { skus: [r] }
1418
- }) : null,
1419
- t.includeSpecs !== !1 && r ? D(n.specs || "Specs", {
1420
- type: "productQuestion",
1421
- payload: {
1422
- sku: r,
1423
- text: n.specs || "Specs"
1424
- }
1425
- }) : null,
1426
- t.includeSimilar !== !1 && r ? D(n.similar || "Similar", {
1427
- type: "findSimilar",
1428
- payload: { sku: r }
1429
- }) : null
1430
- ].filter((o) => !!o);
1431
- }
1432
- function Mr(e, t = {}) {
1433
- return Array.isArray(e) ? e.flatMap((r) => r && typeof r == "object" && !Array.isArray(r) ? Wt(r, t) : []) : [];
1434
- }
1435
- function D(e, t) {
1436
- return {
1437
- label: he(e),
1438
- requestDetails: t
1439
- };
1440
- }
1441
- function Br(e, t = 4) {
1442
- return Array.isArray(e) ? e.map((r) => r && typeof r == "object" && !Array.isArray(r) ? r : null).filter((r) => !!r).map((r) => ({
1443
- ...r,
1444
- label: he(r.label)
1445
- })).filter((r) => r.label).slice(0, t) : [];
1446
- }
1447
- function he(e, t = 48) {
1448
- const r = typeof e == "string" ? e.trim().replace(/\s+/gu, " ") : "";
1449
- return r.length > t ? `${r.slice(0, t - 1).trim()}...` : r;
1450
- }
1451
- function Nr(e = {}) {
1452
- return e.steps ? [...e.steps] : F("intent", "intent", e);
1453
- }
1454
- function Rr(e = {}) {
1455
- return e.steps ? [...e.steps] : [
1456
- be("search", "products", e),
1457
- ...e.emitProducts === !1 ? [] : [ke(e.out || "products")],
1458
- ...M(e)
1459
- ];
1460
- }
1461
- function Ht(e = {}) {
1462
- if (e.steps) return [...e.steps];
1463
- const t = e.out || "answer";
1464
- return [
1465
- ...F("product-answer", "answer", e),
1466
- ...e.emitProductDetails ? [Vt("product")] : [],
1467
- ...M(e, t)
1468
- ];
1469
- }
1470
- function Ur(e = {}) {
1471
- return Ht(e);
1472
- }
1473
- function $r(e = {}) {
1474
- if (e.steps) return [...e.steps];
1475
- const t = e.out || "comparison";
1476
- return [
1477
- ...F("compare", "comparison", e),
1478
- ...e.emitComparison === !1 ? [] : [we(t)],
1479
- ...M(e, t)
1480
- ];
1481
- }
1482
- function Fr(e = {}) {
1483
- return e.steps ? [...e.steps] : [be("search", "products", e)];
1484
- }
1485
- function Dr(e = {}) {
1486
- return Qt(e);
1487
- }
1488
- function Qt(e = {}) {
1489
- return e.steps ? [...e.steps] : [we(e.out || "comparison"), ...M(e, "comparison")];
1490
- }
1491
- function zr(e = {}) {
1492
- return e.steps ? [...e.steps] : [ke(e.out || "products"), ...M(e, "products")];
1493
- }
1494
- function Lr(e = {}) {
1495
- return e.steps ? [...e.steps] : [...F("suggested-actions", "suggestedActions", e), ...e.emitActions === !1 ? [] : [Gt(e.out || "suggestedActions")]];
1496
- }
1497
- function F(e, t, r) {
1498
- return [{
1499
- kind: "be_op",
1500
- op: r.beOpName || e,
1501
- input: r.input ?? (() => ({})),
1502
- ...r.cacheTtlS !== void 0 ? { cacheTtlS: r.cacheTtlS } : {},
1503
- ...r.cacheKey !== void 0 ? { cacheKey: r.cacheKey } : {},
1504
- ...r.browserCache !== void 0 ? { browserCache: r.browserCache } : {},
1505
- ...r.errorOut ? { errorOut: r.errorOut } : {},
1506
- ...r.onError ? { onError: r.onError } : {},
1507
- ...r.patch ? { patch: r.patch } : {},
1508
- out: r.out || t
1509
- }];
1510
- }
1511
- function be(e, t, r) {
1512
- return {
1513
- kind: "tool",
1514
- name: r.toolName || e,
1515
- input: r.input ?? (() => ({})),
1516
- ...r.errorOut ? { errorOut: r.errorOut } : {},
1517
- ...r.onError ? { onError: r.onError } : {},
1518
- ...r.patch ? { patch: r.patch } : {},
1519
- out: r.out || t
1520
- };
1521
- }
1522
- function ke(e) {
1523
- return {
1524
- kind: "emit",
1525
- build: ({ bag: t }) => U(St(Yt(t[e])))
1526
- };
1527
- }
1528
- function Vt(e) {
1529
- return {
1530
- kind: "emit",
1531
- build: ({ bag: t }) => U(At(t[e]))
1532
- };
1533
- }
1534
- function we(e) {
1535
- return {
1536
- kind: "emit",
1537
- build: ({ bag: t }) => U(xt(t[e]))
1538
- };
1539
- }
1540
- function Gt(e) {
1541
- return {
1542
- kind: "emit",
1543
- build: ({ bag: t }) => U(_t(Xt(t[e])))
1544
- };
1545
- }
1546
- function M(e, t) {
1547
- return !e.emitText && !t ? [] : [{
1548
- kind: "emit",
1549
- build: (r) => ue(Zt(e.emitText, r, t), !0)
1550
- }];
1551
- }
1552
- function Zt(e, t, r) {
1553
- if (typeof e == "function") return e(t);
1554
- if (typeof e == "string") return e;
1555
- const n = r ? t.bag[r] : void 0;
1556
- return String(n?.answer_html || n?.plain_text || n?.summary || "");
1557
- }
1558
- function Yt(e) {
1559
- return Array.isArray(e) ? e : e && typeof e == "object" && Array.isArray(e.products) ? e.products : [];
1560
- }
1561
- function Xt(e) {
1562
- if (Array.isArray(e)) return e;
1563
- if (e && typeof e == "object") {
1564
- const t = e;
1565
- if (Array.isArray(t.actions)) return t.actions;
1566
- if (Array.isArray(t.buttons)) return t.buttons;
1567
- }
1568
- return [];
1569
- }
1570
- function er(e) {
1571
- const t = e?.payload;
1572
- if (typeof t == "string") return t;
1573
- if (t && typeof t == "object" && "text" in t) {
1574
- const n = t.text;
1575
- if (typeof n == "string") return n;
1576
- }
1577
- const r = e?.action;
1578
- return r && typeof r.payload == "string" ? r.payload : r && typeof r.title == "string" ? r.title : "";
1579
- }
1580
- var tr = [
1581
- {
1582
- pattern: /\bhttps?:\/\/[^\s?#]+(?:\?[^\s#]*)?/giu,
1583
- replacement: nr
1584
- },
1585
- {
1586
- pattern: /\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b/giu,
1587
- replacement: "[email]"
1588
- },
1589
- {
1590
- pattern: /\b(?:\+?\d[\d\s().-]{7,}\d)\b/gu,
1591
- replacement: "[phone]"
1592
- },
1593
- {
1594
- pattern: /\b(?:token|secret|csrf|cookie|bearer|session|sid|cart(?:[_-]?id)?)\s*[:=]\s*[^\s,;&]+/giu,
1595
- replacement: "[secret]"
1596
- },
1597
- {
1598
- pattern: /\b(?:order|cart|session)[_-]?(?:id|no|number)?\s*[:#=]?\s*[A-Z0-9-]{6,}\b/giu,
1599
- replacement: "[secret]"
1600
- }
1601
- ], Se = { patterns: tr };
1602
- function rr(e, t = Se) {
1603
- let r = typeof e == "string" ? e : e == null ? "" : String(e);
1604
- for (const n of t.patterns || []) {
1605
- const o = n.replacement;
1606
- r = typeof o == "function" ? r.replace(n.pattern, (i) => o(i)) : r.replace(n.pattern, o);
1607
- }
1608
- return r;
1609
- }
1610
- function Kr(e, t = Se) {
1611
- return rr(er(e), t);
1612
- }
1613
- function nr(e) {
1614
- try {
1615
- const t = new URL(e);
1616
- return `${t.origin}${t.pathname}${t.search ? "?[query]" : ""}`;
1617
- } catch {
1618
- return "[url]";
1619
- }
1620
- }
1621
- var or = [
1622
- "assistantName",
1623
- "homeDomain",
1624
- "categories",
1625
- "policyTopics"
1626
- ], ir = { intent: ["searchContract", "competitorBrands"] };
1627
- function vr(e, t, r, n = {}) {
1628
- const o = ar(t, r, n);
1629
- return Object.keys(o).length ? {
1630
- ...e,
1631
- account_config: o
1632
- } : e;
1633
- }
1634
- function ar(e, t, r = {}) {
1635
- if (!e) return {};
1636
- const n = {}, o = r.baseKeys || or;
1637
- for (const c of o) ne(n, e, c);
1638
- const i = e[t];
1639
- i && typeof i == "object" && !Array.isArray(i) && Object.assign(n, cr(i));
1640
- const a = r.opAllowlist || ir;
1641
- for (const c of a[t] || []) ne(n, e, c);
1642
- return n;
1643
- }
1644
- function ne(e, t, r) {
1645
- t[r] !== void 0 && (e[r] = t[r]);
1646
- }
1647
- function cr(e) {
1648
- return Object.fromEntries(Object.entries(e).filter(([, t]) => t !== void 0));
1649
- }
1650
- export {
1651
- wr as $,
1652
- vt as A,
1653
- Et as B,
1654
- Jt as C,
1655
- Pe as Ct,
1656
- Lt as D,
1657
- z as E,
1658
- Ie as Et,
1659
- jr as F,
1660
- At as G,
1661
- Tr as H,
1662
- $ as I,
1663
- _r as J,
1664
- St as K,
1665
- Er as L,
1666
- ge as M,
1667
- Ir as N,
1668
- zt as O,
1669
- Dt as P,
1670
- Sr as Q,
1671
- w as R,
1672
- Mr as S,
1673
- I as St,
1674
- Pr as T,
1675
- lr as Tt,
1676
- _t as U,
1677
- b as V,
1678
- xt as W,
1679
- xr as X,
1680
- ye as Y,
1681
- kr as Z,
1682
- Lr as _,
1683
- dr as _t,
1684
- tr as a,
1685
- hr as at,
1686
- Br as b,
1687
- Fe as bt,
1688
- Dr as c,
1689
- yr as ct,
1690
- $r as d,
1691
- pr as dt,
1692
- de as et,
1693
- Nr as f,
1694
- ue as ft,
1695
- Rr as g,
1696
- se as gt,
1697
- zr as h,
1698
- L as ht,
1699
- Se as i,
1700
- br as it,
1701
- Ft as j,
1702
- Kt as k,
1703
- Qt as l,
1704
- le as lt,
1705
- Ht as m,
1706
- mr as mt,
1707
- vr as n,
1708
- J as nt,
1709
- rr as o,
1710
- gr as ot,
1711
- Ur as p,
1712
- U as pt,
1713
- kt as q,
1714
- Kr as r,
1715
- W as rt,
1716
- er as s,
1717
- Ge as st,
1718
- ar as t,
1719
- Ar as tt,
1720
- Fr as u,
1721
- Ve as ut,
1722
- D as v,
1723
- ve as vt,
1724
- Cr as w,
1725
- ur as wt,
1726
- Wt as x,
1727
- fr as xt,
1728
- he as y,
1729
- De as yt,
1730
- Or as z
1731
- };