@gengage/assistant-fe 0.6.7 → 0.6.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/agentic/index.js +117 -116
- package/dist/agentic/types.d.ts +2 -0
- package/dist/agentic/worker/be-client.d.ts +2 -1
- package/dist/agentic/worker.js +49 -48
- package/dist/agentic.iife.js +4 -4
- package/dist/{api-paths-Dkah8jhG.js → api-paths-DXlenbou.js} +1 -1
- package/dist/chat-runtime.js +1 -1
- package/dist/chat.iife.js +1 -1
- package/dist/chat.js +1 -1
- package/dist/{common-Buxw5G_i.js → common-BqxygI8c.js} +1 -1
- package/dist/common.js +5 -5
- package/dist/{connection-warning-CEm2TZpj.js → connection-warning-BX0ZcdJM.js} +1 -1
- package/dist/{fastIntent-HK3SqGgn.js → fastIntent-Dl9zgfsn.js} +1 -1
- package/dist/index.js +10 -10
- package/dist/native.iife.js +1 -1
- package/dist/qna-runtime.js +1 -1
- package/dist/qna.iife.js +1 -1
- package/dist/qna.js +1 -1
- package/dist/{runtime-DEOej3zn.js → runtime-CbSp96W2.js} +3 -3
- package/dist/{runtime-Dir14P0o.js → runtime-DKBxlh-F.js} +3 -3
- package/dist/{runtime-OdW89-j-.js → runtime-_HRuAyrh.js} +3 -3
- package/dist/{simbut-DWvSKXQ7.js → simbut-DotkjkIQ.js} +1 -1
- package/dist/simbut.iife.js +1 -1
- package/dist/simbut.js +1 -1
- package/dist/{similarity-CfOr8nPO.js → similarity-XEVfAN-6.js} +211 -200
- package/dist/{simrel-DWV_87jY.js → simrel-CBnnj4Mm.js} +1 -1
- package/dist/simrel-runtime.js +1 -1
- package/dist/simrel.iife.js +1 -1
- package/dist/simrel.js +2 -2
- package/dist/{widget-base-Cafa292X.js → widget-base-BEXFI7CM.js} +1 -1
- package/package.json +1 -1
package/dist/agentic/worker.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { A as
|
|
2
|
-
function
|
|
3
|
-
const a = self, n = /* @__PURE__ */ new Map(), c =
|
|
1
|
+
import { A as P, C as m, D as B, E as J, F as M, I as F, L as I, M as C, N as g, O as j, P as T, R as L, S as h, T as z, _ as R, a as q, b as D, c as K, d as O, f as W, g as G, h as N, i as Q, j as _, k as H, l as V, m as X, n as Y, o as Z, p as $, r as tt, s as et, t as rt, u as ot, v as x, w as at, x as nt, y as A, z as it } from "../similarity-XEVfAN-6.js";
|
|
2
|
+
function ct(i) {
|
|
3
|
+
const a = self, n = /* @__PURE__ */ new Map(), c = I(a), S = x(c);
|
|
4
4
|
let e = null, u = null, l = null, p = "";
|
|
5
5
|
a.addEventListener("message", (t) => {
|
|
6
6
|
const r = t.data;
|
|
@@ -29,7 +29,7 @@ function it(i) {
|
|
|
29
29
|
e.tokenBrokerAudience || ""
|
|
30
30
|
].join(`
|
|
31
31
|
`);
|
|
32
|
-
(!l || p !== r) && (l =
|
|
32
|
+
(!l || p !== r) && (l = C(e), p = r), u ||= new T({
|
|
33
33
|
accountId: e.accountId,
|
|
34
34
|
locale: e.locale,
|
|
35
35
|
parentUrl: e.parentUrl,
|
|
@@ -46,15 +46,15 @@ function it(i) {
|
|
|
46
46
|
};
|
|
47
47
|
try {
|
|
48
48
|
const o = e, f = l;
|
|
49
|
-
await
|
|
49
|
+
await A({
|
|
50
50
|
request: t.request || {},
|
|
51
51
|
accountModule: {
|
|
52
52
|
...i,
|
|
53
53
|
accountId: o.accountId
|
|
54
54
|
},
|
|
55
55
|
contextStore: u,
|
|
56
|
-
beClient: { invoke({ op: w, input: y, signal: k, cacheTtlS: b }) {
|
|
57
|
-
return
|
|
56
|
+
beClient: { invoke({ op: w, input: y, signal: k, cacheTtlS: b, cacheKey: U }) {
|
|
57
|
+
return B({
|
|
58
58
|
beUrl: o.beUrl,
|
|
59
59
|
accountId: o.accountId,
|
|
60
60
|
jwtProvider: f,
|
|
@@ -62,6 +62,7 @@ function it(i) {
|
|
|
62
62
|
op: w,
|
|
63
63
|
input: y,
|
|
64
64
|
cacheTtlS: b,
|
|
65
|
+
cacheKey: U,
|
|
65
66
|
...k ? { signal: k } : {}
|
|
66
67
|
});
|
|
67
68
|
} },
|
|
@@ -74,7 +75,7 @@ function it(i) {
|
|
|
74
75
|
type: "end"
|
|
75
76
|
});
|
|
76
77
|
} catch (o) {
|
|
77
|
-
d.signal.aborted || (s(
|
|
78
|
+
d.signal.aborted || (s(P(o)), s(h()), a.postMessage({
|
|
78
79
|
id: t.id,
|
|
79
80
|
type: "end"
|
|
80
81
|
}));
|
|
@@ -84,44 +85,44 @@ function it(i) {
|
|
|
84
85
|
}
|
|
85
86
|
}
|
|
86
87
|
export {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
88
|
+
j as AgentError,
|
|
89
|
+
T as ContextStore,
|
|
90
|
+
nt as action,
|
|
91
|
+
O as actionButtonsUiSpec,
|
|
92
|
+
H as beErrorToAgentError,
|
|
93
|
+
K as beautyConsultingTurnFlow,
|
|
94
|
+
rt as buildPhotoSimilarityProfile,
|
|
95
|
+
Y as buildProductSimilarityProfile,
|
|
96
|
+
tt as buildSimilarityQueries,
|
|
97
|
+
P as caughtToStreamError,
|
|
98
|
+
W as comparisonUiSpec,
|
|
99
|
+
V as createFlow,
|
|
100
|
+
C as createJwtProvider,
|
|
101
|
+
x as createToolBridge,
|
|
102
|
+
I as createWorkerRpc,
|
|
103
|
+
h as done,
|
|
104
|
+
M as elapsedMs,
|
|
105
|
+
m as error,
|
|
106
|
+
L as handleMainRpc,
|
|
107
|
+
_ as httpErrorToAgentError,
|
|
108
|
+
B as invokeBeOp,
|
|
109
|
+
at as metadata,
|
|
110
|
+
g as mintDevJwt,
|
|
111
|
+
N as normalizeProduct,
|
|
112
|
+
Q as normalizeSimilarityText,
|
|
113
|
+
F as nowIso,
|
|
114
|
+
$ as productDetailsUiSpec,
|
|
115
|
+
X as productsUiSpec,
|
|
116
|
+
q as rankSimilarProducts,
|
|
117
|
+
ot as requestText,
|
|
118
|
+
D as resolveFlow,
|
|
119
|
+
it as runMainRpc,
|
|
120
|
+
A as runTurn,
|
|
121
|
+
Z as scoreSimilarityCandidate,
|
|
122
|
+
ct as startWorker,
|
|
123
|
+
z as textChunk,
|
|
124
|
+
et as tokenizeSimilarityText,
|
|
125
|
+
G as trimProductFactsCore,
|
|
126
|
+
R as trimProductFactsListCore,
|
|
127
|
+
J as uiSpec
|
|
127
128
|
};
|
package/dist/agentic.iife.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
(function(m){Object.defineProperty(m,Symbol.toStringTag,{value:"Module"});function qe(e){return!!e&&typeof e=="object"&&e.type==="rpc.req"}function ze({worker:e,tools:t={},beacon:n,memory:r=sessionStorage}){e.addEventListener("message",o=>{const i=o.data;qe(i)&&Ge(e,i,{tools:t,beacon:n,memory:r})})}async function Ge(e,t,n){try{const r=await te(t.method,t.payload,n);e.postMessage({type:"rpc.result",rpcId:t.rpcId,ok:!0,value:r})}catch(r){e.postMessage({type:"rpc.result",rpcId:t.rpcId,ok:!1,error:{message:r instanceof Error?r.message:String(r)}})}}function Je(e){if(e&&typeof e=="object"&&typeof e.name=="string")return e;throw new Error("tool.invoke requires { name, input }")}function He(e){if(e&&typeof e=="object"&&e.key!==void 0)return e;throw new Error("memory.get requires { key }")}function Ke(e){if(e&&typeof e=="object"&&e.key!==void 0)return e;throw new Error("memory.set requires { key }")}async function te(e,t,{tools:n,beacon:r,memory:o}){if(e==="tool.invoke"){const{name:i,input:a}=Je(t),s=n[i];if(typeof s!="function")throw new Error(`Unknown tool: ${i}`);return s(a)}if(e==="beacon.send")return r?.(t),{sent:!0};if(e==="memory.get"){const{key:i}=He(t),a=o.getItem(String(i));return a?JSON.parse(a):null}if(e==="memory.set"){const{key:i,value:a}=Ke(t);return o.setItem(String(i),JSON.stringify(a??null)),{ok:!0}}throw new Error(`Unknown RPC method: ${e}`)}function ne(){return new Date().toISOString()}function U(e){return Math.max(0,Math.round(performance.now()-e))}function
|
|
1
|
+
(function(m){Object.defineProperty(m,Symbol.toStringTag,{value:"Module"});function qe(e){return!!e&&typeof e=="object"&&e.type==="rpc.req"}function ze({worker:e,tools:t={},beacon:n,memory:r=sessionStorage}){e.addEventListener("message",o=>{const i=o.data;qe(i)&&Ge(e,i,{tools:t,beacon:n,memory:r})})}async function Ge(e,t,n){try{const r=await te(t.method,t.payload,n);e.postMessage({type:"rpc.result",rpcId:t.rpcId,ok:!0,value:r})}catch(r){e.postMessage({type:"rpc.result",rpcId:t.rpcId,ok:!1,error:{message:r instanceof Error?r.message:String(r)}})}}function Je(e){if(e&&typeof e=="object"&&typeof e.name=="string")return e;throw new Error("tool.invoke requires { name, input }")}function He(e){if(e&&typeof e=="object"&&e.key!==void 0)return e;throw new Error("memory.get requires { key }")}function Ke(e){if(e&&typeof e=="object"&&e.key!==void 0)return e;throw new Error("memory.set requires { key }")}async function te(e,t,{tools:n,beacon:r,memory:o}){if(e==="tool.invoke"){const{name:i,input:a}=Je(t),s=n[i];if(typeof s!="function")throw new Error(`Unknown tool: ${i}`);return s(a)}if(e==="beacon.send")return r?.(t),{sent:!0};if(e==="memory.get"){const{key:i}=He(t),a=o.getItem(String(i));return a?JSON.parse(a):null}if(e==="memory.set"){const{key:i,value:a}=Ke(t);return o.setItem(String(i),JSON.stringify(a??null)),{ok:!0}}throw new Error(`Unknown RPC method: ${e}`)}function ne(){return new Date().toISOString()}function U(e){return Math.max(0,Math.round(performance.now()-e))}function v(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}function We(e){const{accountId:t,locale:n="en-GB",parentUrl:r="",threadId:o,incomingContext:i}=e,a=v(i)?i:{},s=v(a.thread)?a.thread:{},f=v(a.panel)?a.panel:{},p=v(a.meta)?a.meta:{},d=re(a.messages),u={id:String(o||s.id||self.crypto.randomUUID()),started_at:String(s.started_at||ne()),extensions:v(s.extensions)?{...s.extensions}:{}},c=String(p.locale||n),l=String(p.parentUrl||r||typeof self<"u"&&self.location?.href||"");return{panel:{...f},messages:d,thread:u,meta:{locale:c,parentUrl:l,accountId:t}}}function Ve(e,t){const n=re([t])[0];return n?{...e,messages:[...e.messages,n].slice(-50)}:e}function re(e){return Array.isArray(e)?e.map(t=>{const n=t?.role;return{role:n==="model"?"assistant":typeof n=="string"?n:"",content:String(t?.content||"")}}).filter(t=>(t.role==="user"||t.role==="assistant")&&!!t.content).slice(-50):[]}var Qe="gengage:agent:context:";async function Ye({accountId:e,threadId:t,rpc:n}){const r=await n("memory.get",{tier:"session",key:oe(e,t)});return r&&typeof r=="object"&&!Array.isArray(r)?r:{}}async function Xe({accountId:e,threadId:t,extensions:n,rpc:r}){await r("memory.set",{tier:"session",key:oe(e,t),value:{thread:{extensions:n}}})}function oe(e,t){return`${Qe}${e}:${t}`}function S(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}var ie=class{#e=new Map;#t;#r;#o;#n;constructor({accountId:e,locale:t,parentUrl:n,rpc:r}){this.#t=e,this.#r=t,this.#o=n,this.#n=r}async load(e){const t=e?.meta?.threadId||e?.session_id||e?.sessionId,n=String(t||"default"),r=this.#e.get(n);if(r)return r;const o=await Ye({accountId:this.#t,threadId:n,rpc:this.#n}),i=We({accountId:this.#t,locale:e?.locale||this.#r,parentUrl:this.#o,threadId:n,incomingContext:Ze(o,e?.context)});return this.#e.set(n,i),i}patch(e,t){const n=String(e||"default"),r=this.#e.get(n);if(!r)return null;const o=ae(r,typeof t=="function"?t(r):t);return this.#e.set(n,o),o}appendUserMessage(e,t){const n=String(e||"default");if(!t)return this.#e.get(n)??null;const r=this.#e.get(n);if(!r)return null;const o=Ve(r,{role:"user",content:t});return this.#e.set(n,o),o}async commit(e){const t=String(e||"default"),n=this.#e.get(t);return n?(await Xe({accountId:this.#t,threadId:t,extensions:n.thread.extensions,rpc:this.#n}),n):null}};function Ze(e,t){return!S(e)&&!S(t)?{}:ae(S(e)?e:{},S(t)?t:{})}function ae(e,t){if(!S(t))return e;const n=e,r=t,o=S(n.panel)?n.panel:{},i=S(r.panel)?r.panel:{},a=S(n.thread)?n.thread:{},s=S(r.thread)?r.thread:{},f=S(a.extensions)?a.extensions:{},p=S(s.extensions)?s.extensions:{},d=S(n.meta)?n.meta:{},u=S(r.meta)?r.meta:{};return{...e,...t,panel:{...o,...i},thread:{...a,...s,extensions:{...f,...p}},meta:{...d,...u},messages:Array.isArray(r.messages)?r.messages.slice(-50):e.messages??[]}}function se(e){const t=String.fromCharCode(...e);return btoa(t).replace(/\+/gu,"-").replace(/\//gu,"_").replace(/=+$/u,"")}function ce(e){return se(new TextEncoder().encode(JSON.stringify(e)))}async function et({accountId:e,devJwtSecret:t,ttlS:n=300}){if(!t)throw new Error("devJwtSecret is required for local agent mode.");const r=Math.floor(Date.now()/1e3),o=`${ce({alg:"HS256",typ:"JWT"})}.${ce({sub:e,iat:r,exp:r+n,scope:"invoke",jti:crypto.randomUUID?.()||`${r}-${Math.random()}`})}`,i=await crypto.subtle.importKey("raw",new TextEncoder().encode(t),{name:"HMAC",hash:"SHA-256"},!1,["sign"]),a=await crypto.subtle.sign("HMAC",i,new TextEncoder().encode(o));return`${o}.${se(new Uint8Array(a))}`}function ue({accountId:e,devJwtSecret:t,tokenBrokerUrl:n,tokenBrokerAudience:r,refreshSkewS:o=30,fetchImpl:i=fetch}){let a=null;return async({signal:s,parentUrl:f}={})=>{if(t)return et({accountId:e,devJwtSecret:t});if(!n)throw new Error("tokenBrokerUrl is required for production agent mode.");const p=Math.floor(Date.now()/1e3);return a?.token&&a.expiresAtS-o>p||(a=await tt({accountId:e,tokenBrokerUrl:n,tokenBrokerAudience:r,parentUrl:f,signal:s,fetchImpl:i})),a.token}}async function tt({accountId:e,tokenBrokerUrl:t,tokenBrokerAudience:n,parentUrl:r,signal:o,fetchImpl:i}){const a=await i(t,{method:"POST",credentials:"include",headers:{accept:"application/json","content-type":"application/json"},body:JSON.stringify({accountId:e,scope:"invoke",audience:n||void 0,parentUrl:r||void 0}),...o?{signal:o}:{}});if(!a.ok)throw new Error(`Token broker request failed (${a.status}).`);const s=await a.json(),f=s?.token||s?.jwt||s?.access_token;if(!f||typeof f!="string")throw new Error("Token broker response did not include a JWT.");return{token:f,expiresAtS:nt(s)||rt(f)||Math.floor(Date.now()/1e3)+300}}function nt(e){const t=e?.expiresAtS??e?.expires_at_s??e?.expiresInS??e?.expires_in_s;if(Number.isFinite(t))return Number(t);const n=e?.expiresIn??e?.expires_in;if(Number.isFinite(n))return Math.floor(Date.now()/1e3)+Number(n);const r=e?.expiresAt??e?.expires_at;if(typeof r=="string"){const o=Date.parse(r);if(Number.isFinite(o))return Math.floor(o/1e3)}return null}function rt(e){try{const[,t]=e.split(".");if(!t)return null;const n=t.replace(/-/gu,"+").replace(/_/gu,"/"),r=atob(n.padEnd(Math.ceil(n.length/4)*4,"=")),o=JSON.parse(r);return Number.isFinite(o?.exp)?Number(o.exp):null}catch{return null}}var le={unauthorized:{code:"auth",message:"Assistant authentication failed. Please retry."},forbidden:{code:"auth",message:"Assistant authentication failed. Please retry."},unknown_op:{code:"op_unavailable",message:"This assistant action is not available yet."},invalid_input:{code:"invalid_request",message:"The assistant request was not valid."},account_config:{code:"account_config",message:"Assistant configuration is unavailable."},upstream_llm:{code:"op_failed",message:"The assistant could not complete that request."},upstream_timeout:{code:"op_timeout",message:"The assistant took too long to respond."},schema_mismatch:{code:"op_failed",message:"The assistant could not complete that request."},rate_limited:{code:"rate_limited",message:"The assistant is receiving too many requests. Please retry shortly."},internal:{code:"op_failed",message:"The assistant could not complete that request."},payload_too_large:{code:"invalid_request",message:"The assistant request was too large."}},$=class extends Error{constructor(e,t,n={}){super(t),this.name="AgentError",this.code=e,this.source=n.source||"agent",n.sourceCode&&(this.sourceCode=n.sourceCode),n.detail!==void 0&&(this.detail=n.detail)}};function L(e){const t=String(e?.code||"upstream_llm"),n=le[t]||le.upstream_llm;return new $(n.code,n.message,{source:"be",sourceCode:t,detail:e?.detail})}function de(e,t){const n=t?.error&&typeof t.error=="object"?t.error:{};return L({code:String(n.code||ot(e)),detail:n.detail})}function fe(e){return e instanceof $?{type:"error",code:e.code,message:e.message}:{type:"error",code:"agent_invoke_failed",message:"The assistant could not complete that request."}}function ot(e){return e===401?"unauthorized":e===403?"forbidden":e===413?"payload_too_large":e===429?"rate_limited":"internal"}async function it({beUrl:e,accountId:t,devJwtSecret:n,tokenBrokerUrl:r,tokenBrokerAudience:o,jwtProvider:i,parentUrl:a,op:s,input:f,cacheTtlS:p,cacheKey:d,signal:u}){const c=await(i||ue({accountId:t,devJwtSecret:n,tokenBrokerUrl:r,tokenBrokerAudience:o}))({...u?{signal:u}:{},...a?{parentUrl:a}:{}}),{opDomain:l,opName:y}=at(s),g={method:"POST",headers:{authorization:`Bearer ${c}`,"content-type":"application/json",accept:"application/x-ndjson"},body:JSON.stringify({op_domain:l,op_name:y,op_payload:f,...p!==void 0?{cache_ttl_s:p}:{},...d!==void 0?{cache_key:d}:{}}),...u?{signal:u}:{}},h=await fetch(`${String(e).replace(/\/+$/u,"")}/v1/execute`,g);if(!h.ok)throw de(h.status,await st(h));return ct(h)}function at(e){const t=e.indexOf("-");if(t<=0||t===e.length-1)throw new Error(`Invalid BE op name: ${e}`);return{opDomain:e.slice(0,t),opName:e.slice(t+1)}}async function st(e){try{return await e.json()}catch{return{error:{code:e.statusText||"internal"}}}}async function ct(e){const t=e.body?.getReader();if(!t)throw new Error("BE response body is not readable.");const n=new TextDecoder;let r="";const o={};for(;;){const{value:a,done:s}=await t.read();if(s)break;r+=n.decode(a,{stream:!0});let f=r.indexOf(`
|
|
2
2
|
`);for(;f>=0;){const p=r.slice(0,f).trim();r=r.slice(f+1),p&&pe(JSON.parse(p),o),f=r.indexOf(`
|
|
3
|
-
`)}}const i=r.trim();return i&&pe(JSON.parse(i),o),o}function pe(e,t){if(e._error)throw L(e._error);e._end||Object.assign(t,e)}function
|
|
4
|
-
`))},
|
|
5
|
-
`],{type:"text/javascript"}),i=URL.createObjectURL(o);try{return{worker:new Worker(i,{type:"module",name:t}),cleanup(){URL.revokeObjectURL(i)}}}catch(a){throw URL.revokeObjectURL(i),a}}function xe(e){return e&&typeof e=="object"&&!Array.isArray(e)?e:null}function O(e){const t=xe(e);if(!t)return null;const n=x(t.sku,t.SKU);if(!n)return null;const r={sku:n,name:x(t.name,t.title,t.short_name,n)||n,url:x(t.url)||""},o=Array.isArray(t.images)?t.images:void 0,i=x(t.imageUrl,t.image_url,t.image,o?.[0]);i&&(r.imageUrl=i),o&&o.length>1&&(r.images=o.filter(g=>!!g).map(String));const a=M(t.price_discounted),s=M(t.price),f=a||s;f>0&&(r.price=String(f));const p=a>0?s:0;p>0&&(r.originalPrice=String(p));const u=x(t.brand);u&&(r.brand=u);const l=M(t.rating);l>0&&(r.rating=l);const c=M(t.review_count)||M(t.reviewCount);c>0&&(r.reviewCount=c);const d=x(t.cart_code,t.cartCode);d&&(r.cartCode=d),typeof t.in_stock=="boolean"&&(r.inStock=t.in_stock),typeof t.inStock=="boolean"&&(r.inStock=t.inStock);const y=t.category_names;return Array.isArray(y)&&(r.categoryNames=y.map(String)),r}function je(e){const t=xe(e);if(!t)return e;const n=t.category_names,r=Array.isArray(n)?n:void 0,o=t.images,i=Array.isArray(o)?o:void 0,a={sku:t.sku,name:t.name||t.title,url:t.url,price:t.price,currency:t.price_currency||t.currency,category:r?.[r.length-1]??void 0,category_names:r?r.slice(0,4):void 0,image:i?i[0]:t.image,in_stock:t.in_stock,rating:t.rating,review_count:t.review_count};for(const s of Object.keys(a))a[s]===void 0&&delete a[s];return a}function H(e){return Array.isArray(e)?e.map(je).filter(t=>!!t):[]}function x(...e){for(const t of e)if(typeof t=="string"&&t.trim())return t.trim();return""}function M(e){const t=Number(e);return Number.isFinite(t)?t:0}function Ie(e){return Array.isArray(e)?e.map(String).filter(Boolean):typeof e=="string"&&e?[e]:[]}function ve(e=[]){const t={},n=[];return(Array.isArray(e)?e.map(O).filter(r=>!!r):[]).forEach((r,o)=>{const i=`product-${o.toString()}`;n.push(i);const a={sku:r.sku,product:r};t[i]={type:"ProductCard",props:{product:r,index:o,action:{title:r.name,type:"launchSingleProduct",payload:a}}}}),t.root={type:"ProductGrid",props:{layout:"grid"},children:n},{widget:"chat",panelHint:"panel",spec:{root:"root",elements:t}}}function $t(e){return{widget:"chat",panelHint:"panel",spec:{root:"root",elements:{root:{type:"ProductDetailsPanel",props:{product:O(e)||e||{}}}}}}}function Lt(e={}){const t=(e.multiple_product_details||e.products||[]).map(O).filter(r=>!!r),n=e.table||{};return{widget:"chat",panelHint:"panel",spec:{root:"root",elements:{root:{type:"ComparisonTable",props:{products:t,recommended:t.find(r=>r.sku===e.recommended_choice_sku)||t[0],attributes:Object.entries(n).map(([r,o])=>({label:r,values:Array.isArray(o)?o.map(String):[String(o??"")]})),highlights:Ie(e.key_differences),specialCases:Ie(e.special_considerations),recommendedText:e.recommended_choice,productActions:Object.fromEntries(t.map(r=>[r.sku,{title:r.name,type:"launchSingleProduct",payload:{sku:r.sku,product:r}}]))}}}}}}function Dt(e=[]){return{widget:"chat",panelHint:"inline",spec:{root:"root",elements:{root:{type:"ActionButtons",props:{buttons:(Array.isArray(e)?e:[]).map(t=>{const n=t.label||t.title||t.shortName||"",r=t.action||t.requestDetails||t.request_details;return!n||!r?.type?null:{label:n,action:{title:n,type:r.type,...r.payload!==void 0?{payload:r.payload}:{}}}}).filter(t=>!!t)}}}}}}function B(e){const t=e?.payload;if(typeof t=="string")return t;if(t&&typeof t=="object"&&"text"in t){const r=t.text;if(typeof r=="string")return r}const n=e?.action;return n&&typeof n.payload=="string"?n.payload:n&&typeof n.title=="string"?n.title:""}function Ce(e){return Object.freeze([...e])}function P(e){return e&&typeof e=="object"?e:{}}function K(e){return e&&typeof e=="object"?e:{}}function Me(e){return e.thread.extensions.beauty_profile}var Ft=Ce([{kind:"be_op",op:"beauty-consulting-turn",input:({request:e,context:t})=>({utterance:B(e),prior_messages:t.messages.slice(-10),profile:Me(t),candidate_products:zt(e),locale:t.meta.locale}),out:"consultation",patch:Re},{kind:"branch",on:({bag:e})=>P(e.consultation).consultation_state?.stage||"answer",cases:{refuse:[{kind:"refusal",message:({bag:e})=>{const t=P(e.consultation);return t.refusal?.message||t.plain_text||"I cannot help with that request."}}],needs_more_info:[{kind:"emit",build:({bag:e})=>v(V(P(e.consultation)),!0)},{kind:"commit"}],search:[{kind:"tool",name:"search",input:({request:e,bag:t,context:n})=>{const r=P(t.consultation);return{query:r.search_params?.query||B(e),facets:r.search_params?.facets||{},limit:12,locale:n.meta.locale}},out:"products",patch:(e,t)=>{const n=K(t);return{panel:{...e.panel,screen_sku_list:W(n).map(r=>qt(r)).filter(r=>!!r),last_search:{query:n.query||""}}}}},{kind:"emit",build:({bag:e})=>me(ve(W(K(e.products))))},{kind:"be_op",op:"beauty-consulting-turn",input:({request:e,context:t,bag:n})=>({utterance:B(e),prior_messages:t.messages.slice(-10),profile:Me(t),candidate_products:H(W(K(n.products)).slice(0,12)),locale:t.meta.locale}),out:"answer",patch:(e,t)=>{const n=Re(e,t),r=(P(t).product_mentions||[]).map(o=>o.sku).filter(o=>!!o);return{...n||{},panel:{...e.panel,chat_mentioned_skus:r}}}},{kind:"emit",build:({bag:e})=>{const t=P(e.answer);return v(V(t),!0,{productMentions:t.product_mentions||[]})}},{kind:"commit"}],default:[{kind:"emit",build:({bag:e})=>{const t=P(e.consultation);return v(V(t),!0,{productMentions:t.product_mentions||[]})}},{kind:"commit"}]}}]);function Re(e,t){const n=P(t).consultation_state?.captured_profile;if(!n)return null;const r=e.thread.extensions.beauty_profile&&typeof e.thread.extensions.beauty_profile=="object"?e.thread.extensions.beauty_profile:{};return{thread:{...e.thread,extensions:{...e.thread.extensions,beauty_profile:{...r,...n}}}}}function W(e){return Array.isArray(e?.products)?e.products:[]}function qt(e){if(e&&typeof e=="object"&&"sku"in e){const t=e.sku;if(typeof t=="string")return t}}function zt(e){const t=e?.payload&&typeof e.payload=="object"?e.payload.products:void 0,n=e?.action?.payload&&typeof e.action.payload=="object"?e.action.payload.products:void 0,r=t||n;return Array.isArray(r)?H(r.slice(0,12)):void 0}function V(e){return e?.answer_html||e?.plain_text||e?.refusal?.message||"I can help with beauty shopping."}var Ue=new Set(["and","are","box","for","from","gift","gifts","the","with"]),Gt={token:.26,keyword:.22,category:.18,facet:.16,color:.08,attribute:.06,price:.08,retrieval:.08},Jt=["amber","beige","black","blue","blush","bronze","brown","cream","gold","green","grey","ivory","lilac","mixed","orange","pastel","pink","purple","red","silver","white","yellow"];function A(e){return String(e??"").normalize("NFKD").replace(new RegExp("\\p{Diacritic}","gu"),"").toLowerCase().replace(/[^\p{L}\p{N}]+/gu," ").replace(/\s+/gu," ").trim()}function k(e,t=Ue){const n=t instanceof Set?t:new Set([...Ue,...t]),r=new Set,o=[];for(const i of A(e).split(/\s+/u))i.length<3||n.has(i)||r.has(i)||(r.add(i),o.push(i));return o}function R(e,t={}){if(Vt(e))return e;const n=t.facetKeys,r=b(e.category_names??e.category),o=b(e.category_ids),i=Qt(e.facet_hits),a=Object.entries(i).filter(([g])=>!n||n.some(h=>A(h)===A(g))).map(([,g])=>g),s=_(e.name,e.title),f=_(e.brand),p=t.sourceKeywords??[],u=[s,f,...r,...a,...b(e.promotions),_(e.description),...p].join(" "),l=k(u,t.stopWords),c=_(t.sourceProductType,Zt(r,s,l),l[0]),d=T([...Xt(u,Jt),...Object.entries(i).filter(([g])=>/colou?r/iu.test(g)).flatMap(([,g])=>k(g,t.stopWords))]),y=T([...a.flatMap(g=>k(g,t.stopWords)),...b(e.promotions).flatMap(g=>k(g,t.stopWords))]).slice(0,24);return{sku:_(e.sku),title:s,brand:f,productType:c,categoryNames:r,categoryIds:o,facets:i,keywords:T([...p.flatMap(g=>k(g,t.stopWords)),...l]).slice(0,12),tokens:l,colors:d,attributes:y,price:Yt(e)}}function Ht(e,t={}){const n=_(e.product_type,e.productType),r=_(e.title,n,b(e.keywords).join(" ")),o=T([...b(e.keywords),...b(e.colors),...b(e.materials),...b(e.styles),...b(e.attributes)]);return R({sku:"__photo__",name:r,category_names:n?[n]:[],facet_hits:{colors:b(e.colors).join(", "),materials:b(e.materials).join(", "),styles:b(e.styles).join(", "),attributes:b(e.attributes).join(", ")},promotions:o},{...t,sourceProductType:n,sourceKeywords:o})}function Kt(e,t={}){const n=R(e,t);return T([...t.sourceQueries??[],N([n.productType,...n.colors.slice(0,1),...n.attributes.slice(0,2)]),N([n.productType,...n.keywords.slice(0,3)]),N([n.categoryNames[n.categoryNames.length-1],...n.keywords.slice(0,2)]),N(n.title.split(/\s+/u).slice(0,6)),n.productType].map(r=>r.trim()).filter(Boolean)).slice(0,t.queryLimit??6)}function Oe(e,t,n={}){const r={...Gt,...n.weights},o=R(t,n),i=[],a=j(e.tokens,o.tokens),s=j(e.keywords,o.tokens),f=en(e.categoryNames,o.categoryNames)||j(Be(e.categoryNames),Be(o.categoryNames)),p=j(e.attributes,o.attributes),u=j(e.colors,o.colors),l=j(e.attributes,o.tokens),c=tn(e.price,o.price),d=nn(o.sku,n.hitCounts)>0?1:0,y=rn(e,o),g=a*r.token+s*r.keyword+f*r.category+p*r.facet+u*r.color+l*r.attribute+c*r.price+d*r.retrieval,h=on(y?g:g*.35);return y&&e.productType&&e.productType===o.productType&&i.push("same product type"),f>.4&&i.push("similar category"),p>.2&&i.push("matching product attributes"),u>0&&i.push("matching colour"),c>.8&&i.push("similar price"),y||i.push("weaker product-type match"),{score:Number(h.toFixed(4)),reasons:i}}function Wt(e,t,n={}){const r=R(e,n),o=new Set([r.sku,...n.ignoreSkus??[]].filter(Boolean));return t.filter(i=>{const a=_(i.sku);return a&&!o.has(a)}).map(i=>{const a=Oe(r,i,n);return{...i,similarity_score:a.score,similarity_reasons:a.reasons}}).sort((i,a)=>a.similarity_score-i.similarity_score).slice(0,Math.max(1,Math.min(n.limit??12,100)))}function Vt(e){return!!(e&&typeof e=="object"&&Array.isArray(e.tokens))}function _(...e){for(const t of e)if(typeof t=="string"&&t.trim())return t.trim();return""}function b(e){return Array.isArray(e)?e.map(t=>_(t)).filter(Boolean):typeof e=="string"&&e.trim()?e.split(/[,;/|]+/u).map(t=>t.trim()).filter(Boolean):[]}function Qt(e){return!e||typeof e!="object"||Array.isArray(e)?{}:Object.fromEntries(Object.entries(e).map(([t,n])=>[t,_(n)]).filter(([,t])=>t))}function Yt(e){const t=Number(e.price_discounted);if(Number.isFinite(t)&&t>0)return t;const n=Number(e.price);return Number.isFinite(n)&&n>0?n:0}function T(e){const t=new Set,n=[];for(const r of e){const o=A(r);!o||t.has(o)||(t.add(o),n.push(o))}return n}function Xt(e,t){const n=` ${A(e)} `;return t.filter(r=>n.includes(` ${A(r)} `))}function Zt(e,t,n){const r=A(e[e.length-1]??e[0]??"");if(r){const o=k(r),i=new Set(k(t));return o.find(a=>i.has(a))??o[0]??r}return n[0]??""}function N(e){return e.map(t=>A(t)).filter(Boolean).join(" ").replace(/\s+/gu," ").trim().slice(0,120)}function Be(e){return e.flatMap(t=>k(t))}function j(e,t){const n=T(e),r=new Set(T(t));return n.length===0||r.size===0?0:n.filter(o=>r.has(o)).length/n.length}function en(e,t){const n=T(e),r=new Set(T(t));return n.length===0||r.size===0?0:n.filter(o=>r.has(o)).length/n.length}function tn(e,t){return e<=0||t<=0?0:Math.min(e,t)/Math.max(e,t)}function nn(e,t){return!e||!t?0:typeof t.get=="function"?t.get(e)??0:t[e]??0}function rn(e,t){if(!e.productType||!t.productType||e.productType===t.productType)return!0;const n=new Set(k(e.productType)),r=new Set([...k(t.productType),...t.tokens]);return[...n].some(o=>r.has(o))}function on(e){return Math.max(0,Math.min(1,e))}function an(e){const t=(e?.chat||e?._chat)?.root||e?._chat?.root;return t?t.getRootNode?.()?.host||t:Array.from(document.querySelectorAll("*")).find(n=>n.shadowRoot?.querySelector?.(".gengage-chat-root, .gengage-chat-launcher-container"))}function sn(e,t){const n=an(e);n?.style&&(t?n.style.removeProperty("display"):n.style.setProperty("display","none","important")),t||(e?.chat||e?._chat)?.close?.()}var cn="nd_be_url";function un(e,t){const n=ln()||e.beUrl||t;if(!n)throw new Error("resolveBeUrl: backend URL is required. The injector must supply a fallback URL; the SDK has no default.");return n}function ln(){try{const e=new URLSearchParams(window.location.search).get(cn)?.trim();if(!e)return null;const t=new URL(e);return t.protocol!=="https:"&&t.protocol!=="http:"?null:t.toString().replace(/\/+$/u,"")}catch{return null}}var dn="entries",fn=1,pn={volatile:"Hot per-page state and sensitive request context.",session:"Current-visit tool context without raw tokens or PII-heavy payloads.",local:"Small, non-sensitive capability facts or user preferences.",indexedDb:"Larger product/search payload caches with short TTLs."},Ne=new Map;function Q(){return Date.now()}function Y(e){return e?.expiresAt!=null&&e.expiresAt<=Q()}function X(e,t){return{value:e,createdAt:Q(),expiresAt:t?Q()+t:null}}function mn(e,t,n){try{const r=e.getItem(`${t}${n}`);if(!r)return null;const o=JSON.parse(r);return Y(o)?(e.removeItem(`${t}${n}`),null):o.value}catch{return null}}function yn(e,t,n,r,o){try{return e.setItem(`${t}${n}`,JSON.stringify(X(r,o))),!0}catch{return!1}}function gn(e,t,n){try{e.removeItem(`${t}${n}`)}catch{}}function $e(e){try{return window[e]}catch{return null}}function Le(e,t){return e?{get:n=>mn(e,t,n),set:(n,r,o={})=>yn(e,t,n,r,o.ttlMs),remove:n=>gn(e,t,n)}:{get:()=>null,set:()=>!1,remove:()=>{}}}function Z(e){if(Array.isArray(e))return`[${e.map(Z).join(",")}]`;if(e&&typeof e=="object"){const t=e;return`{${Object.keys(t).sort().map(n=>`${JSON.stringify(n)}:${Z(t[n])}`).join(",")}}`}return JSON.stringify(e)}function De(e){let t=2166136261;const n=Z(e);for(let r=0;r<n.length;r+=1)t^=n.charCodeAt(r),t=Math.imul(t,16777619);return(t>>>0).toString(36)}function hn(e,t){const n=t.dbName||`gengage-${e}`,r=t.dbStore||dn,o=t.dbVersion||fn,i=new Map;let a=null;const s=()=>"indexedDB"in window?a||(a=new Promise(l=>{const c=indexedDB.open(n,o);c.onupgradeneeded=()=>{c.result.createObjectStore(r,{keyPath:"key"})},c.onsuccess=()=>l(c.result),c.onerror=()=>l(null),c.onblocked=()=>l(null)}),a):Promise.resolve(null);return{volatileEntries:i,idbGet:async l=>{const c=await s();return c?new Promise(d=>{const y=c.transaction(r,"readwrite").objectStore(r),g=y.get(l);g.onsuccess=()=>{const h=g.result;if(!h||Y(h)){h&&y.delete(l),d(null);return}d(h.value)},g.onerror=()=>d(null)}):null},idbSet:async(l,c,d={})=>{const y=await s();return y?new Promise(g=>{const h=y.transaction(r,"readwrite");h.oncomplete=()=>g(!0),h.onerror=()=>g(!1),h.objectStore(r).put({key:l,...X(c,d.ttlMs)})}):!1},idbRemove:async l=>{const c=await s();c&&c.transaction(r,"readwrite").objectStore(r).delete(l)}}}function wn(e,t={}){const n=window,r=n.gengage||(n.gengage={}),o=r.memory||(r.memory={}),i=o[e];if(i)return i;const a=Ne.get(e)||hn(e,t);Ne.set(e,a);const s=t.sessionPrefix||`gengage:${e}:session:`,f=t.localPrefix||`gengage:${e}:local:`,u={get:d=>{const y=a.volatileEntries.get(d);return y?Y(y)?(a.volatileEntries.delete(d),null):y.value:null},set:(d,y,g={})=>(a.volatileEntries.set(d,X(y,g.ttlMs)),!0),remove:d=>{a.volatileEntries.delete(d)},clear:()=>a.volatileEntries.clear()},l={get:a.idbGet,set:a.idbSet,remove:a.idbRemove},c={accountId:e,volatile:u,session:Le($e("sessionStorage"),s),local:Le($e("localStorage"),f),indexedDb:l,stableKey:De,policy:{...pn,...t.policy||{}}};o[e]=c;for(const d of t.aliases||[])o[d]=c;return c}var bn=["addToCart","search","searchKeyword","facetedSearch","searchGiftOptions","similaritySearch"];function Sn(e,t,n={}){const r=window,o=r.gengage||(r.gengage={}),i=o.tools||(o.tools={});i[e]=t;for(const a of n.accountAliases||[])i[a]=t;if(n.exposeStandardAliases!==!1)for(const a of n.standardAliases||bn){const s=t[a];typeof s=="function"&&(i[a]=i[a]||s)}return t}function kn({getPageType:e,getProduct:t}){return()=>({url:window.location.href,title:document.title,pageType:e(),product:t()})}function _n({accountId:e,getPageType:t,getProduct:n,getToolNames:r,getSearchCapabilities:o,getMemory:i}){return async()=>({accountId:e,url:window.location.href,pageType:t(),productSku:n()?.sku??null,toolNames:r(),...o?{searchCapabilities:o()}:{},...i?{memoryPolicy:i().policy}:{},timestamp:new Date().toISOString()})}function An({accountId:e,runtimeFile:t="runtime.js",startExport:n="start",globalBaseUrlKey:r,errorLabel:o}={}){if(!e)throw new Error("accountId is required.");const i=o||e,a=window,s=()=>{const l=a.GengageInjectorConfig||{},c=l[e]||{};return{...l,...c}},f=()=>{const l=s();if(l.runtimeUrl)return l.runtimeUrl;const c=l.assetBaseUrl||l.baseUrl||document.currentScript?.getAttribute("src")||(r?a[r]:null);if(!c)throw new Error(`${i} runtime URL cannot be resolved.`);return new URL(t,c).href},p=a.GengageAssistantInjector||(a.GengageAssistantInjector={}),u=p[e]||(p[e]={});return u.loaderPromise||(u.loaderPromise=import(f()).then(l=>{const c=l[n];if(typeof c!="function")throw new Error(`${i} runtime export ${n} is unavailable.`);return c()}).catch(l=>{throw delete u.loaderPromise,console.error(`[Gengage][${e}] runtime load failed`,l),l})),u.loaderPromise}m.AgentError=$,m.ContextStore=ie,m.action=ut,m.actionButtonsUiSpec=Dt,m.beErrorToAgentError=L,m.beautyConsultingTurnFlow=Ft,m.buildPhotoSimilarityProfile=Ht,m.buildProductSimilarityProfile=R,m.buildSimilarityQueries=Kt,m.canUseModuleWorker=J,m.caughtToStreamError=fe,m.comparisonUiSpec=Lt,m.createBrowserMemory=wn,m.createDiagnosticsTool=_n,m.createFlow=Ce,m.createInjectorAdapter=we,m.createMainThreadInjectorAdapter=be,m.createModuleWorker=Pe,m.createReadPageTool=kn,m.done=D,m.elapsedMs=U,m.error=ye,m.httpErrorToAgentError=de,m.installBrowserTools=Sn,m.installFetchTransportBridge=vt,m.metadata=ct,m.mountAccount=Nt,m.normalizeProduct=O,m.normalizeSimilarityText=A,m.nowIso=ne,m.productDetailsUiSpec=$t,m.productsUiSpec=ve,m.rankSimilarProducts=Wt,m.requestText=B,m.resolveBeUrl=un,m.resolveFlow=ge,m.scoreSimilarityCandidate=Oe,m.setAssistantHostVisible=sn,m.stableKey=De,m.startLazyRuntimeLoader=An,m.textChunk=v,m.tokenizeSimilarityText=k,m.trimProductFactsCore=je,m.trimProductFactsListCore=H,m.uiSpec=me})(this.Gengage=this.Gengage||{});
|
|
3
|
+
`)}}const i=r.trim();return i&&pe(JSON.parse(i),o),o}function pe(e,t){if(e._error)throw L(e._error);e._end||Object.assign(t,e)}function ut(e={}){return{type:"metadata",...e}}function C(e,t=!1,n={}){return{type:"text_chunk",content:e,final:t,...n}}function me(e){return{type:"ui_spec",...e}}function lt(e){return{type:"action",action:e}}function ye(e,t){return{type:"error",code:e,message:t}}function D(){return{type:"done"}}function ge(e,t){return e[t?.type||t?.action?.type||"inputText"]||e.inputText}async function dt({request:e,accountModule:t,contextStore:n,beClient:r,toolBridge:o,emit:i,rpc:a,signal:s}){const f=performance.now(),p=await n.load(e),d=p.thread.id,u=St(e);n.appendUserMessage(d,u);const c=ge(t.flows,e);if(!c)return i(ye("unknown_action",`No agent flow for request type ${e?.type||"inputText"}`)),i(D()),{steps:0,productSkusEmitted:[]};const l={request:e,context:n.patch(d,{})||p,bag:{},accountConfig:t.accountConfig||{},threadId:d,steps:0,productSkusEmitted:new Set,committed:!1},y={contextStore:n,beClient:r,toolBridge:o,emit:i,rpc:a,...s?{signal:s}:{}};return await F(c,l,y),l.committed||await q(l,y),await a("beacon.send",{type:"turnSummary",threadId:d,sessionId:e?.session_id||e?.sessionId||d,accountId:t.accountId||l.context.meta.accountId,steps:l.steps,totalLatencyMs:U(f),productSkusEmitted:[...l.productSkusEmitted]}),{steps:l.steps,productSkusEmitted:[...l.productSkusEmitted]}}async function F(e,t,n){for(const r of e){if(n.signal?.aborted)return;t.steps+=1,await ft(r,t,n),t.context=n.contextStore.patch(t.threadId,{})||t.context}}async function ft(e,t,n){const r=z(t);if(e.kind==="be_op"){await pt(e,t,n,r);return}if(e.kind==="tool"){await mt(e,t,n,r);return}if(e.kind==="emit"){yt(e,t,n);return}if(e.kind==="branch"){await gt(e,t,n,r);return}if(e.kind==="parallel"){await ht(e,t,n);return}if(e.kind==="refusal"){await wt(e,t,n,r);return}e.kind==="commit"&&await bt(e,t,n)}async function pt(e,t,n,r){const o=performance.now();let i;const a=x(e.cacheTtlS,r),s=x(e.cacheKey,r);try{i=await n.beClient.invoke({op:e.op,input:x(e.input,r),...a!==void 0?{cacheTtlS:a}:{},...s!==void 0?{cacheKey:s}:{},...n.signal?{signal:n.signal}:{}})}catch(p){const d=p,u=String(d?.sourceCode||d?.code||"unknown"),c={type:"agentOp",threadId:t.threadId,sessionId:t.request?.session_id||t.request?.sessionId||t.threadId,accountId:t.context.meta.accountId,op:e.op,status:"error",latencyMs:U(o),errorCode:u};throw await n.rpc("beacon.send",c),p}e.out&&(t.bag[e.out]=i),he(e,i,t,n.contextStore);const f={type:"agentOp",threadId:t.threadId,sessionId:t.request?.session_id||t.request?.sessionId||t.threadId,accountId:t.context.meta.accountId,op:e.op,status:"ok",latencyMs:U(o)};await n.rpc("beacon.send",f)}async function mt(e,t,n,r){const o=await n.toolBridge.invoke(e.name,x(e.input,r));e.out&&(t.bag[e.out]=o),he(e,o,t,n.contextStore)}function yt(e,t,n){const r=e.build(z(t));_t(r,t.productSkusEmitted),n.emit(r)}async function gt(e,t,n,r){const o=String(x(e.on,r)||"default");await F(e.cases[o]||e.cases.default||[],t,n)}async function ht(e,t,n){await Promise.all(e.steps.map(r=>F(r,t,n)))}async function wt(e,t,n,r){n.emit(C(x(e.message,r),!0)),await q(t,n)}async function bt(e,t,n){await q(t,n)}async function q(e,{contextStore:t,emit:n}){e.committed||(e.context=await t.commit(e.threadId)||e.context,e.committed=!0,n(D()))}function he(e,t,n,r){if(typeof e.patch!="function")return;const o=e.patch(n.context,t,z(n));o&&(n.context=r.patch(n.threadId,o)||n.context)}function z(e){return{request:e.request,context:e.context,bag:e.bag,accountConfig:e.accountConfig}}function x(e,t){return typeof e=="function"?e(t):e}function St(e){const t=e?.payload;if(typeof t=="string")return t;if(t&&typeof t=="object"&&"text"in t){const n=t.text;if(typeof n=="string")return n}return typeof e?.action?.payload=="string"?e.action.payload:typeof e?.action?.title=="string"?e.action.title:""}function _t(e,t){const n=e,r=n.spec,o=Array.isArray(r?.items)?r.items:void 0,i=Array.isArray(n.items)?n.items:void 0,a=o||i;if(Array.isArray(a)){for(const s of a)if(s&&typeof s=="object"){const f=s.sku;typeof f=="string"&&t.add(f)}}}function kt(e){return{invoke(t,n){return e("tool.invoke",{name:t,input:n})}}}var At=512,Tt=.74;function we({accountId:e,worker:t,beUrl:n,devJwtSecret:r,tokenBrokerUrl:o,tokenBrokerAudience:i,defaultLocale:a,tools:s={},beacon:f}){let p=1;const d=new Map;return ze({worker:t,tools:s,beacon:u=>f?.({...u,accountId:u.accountId||e}),memory:sessionStorage}),t.addEventListener("message",u=>{const c=u.data||{},l=typeof c.id=="number"?c.id:null;if(l==null)return;const y=d.get(l);if(y){if(c.type==="event"&&c.event){G(y,c.event);return}if(c.type==="error"){y.onError(new Error(c.message||"Agent worker failed")),d.delete(l);return}c.type==="end"&&d.delete(l)}}),(u,c,l,y)=>{const g=p++;d.set(g,c);const h=()=>{d.delete(g),t.postMessage({id:g,type:"abort"})},E=w=>{if(l.aborted){h();return}t.postMessage({id:g,type:"invoke",accountId:e,beUrl:n,devJwtSecret:r,tokenBrokerUrl:o,tokenBrokerAudience:i,defaultLocale:a,request:w,parentUrl:window.location.href})};if(l.aborted){h();return}if(l.addEventListener("abort",h,{once:!0}),y){Se(u,y).then(E).catch(w=>{d.delete(g),c.onError(w instanceof Error?w:new Error("Failed to read image attachment"))});return}E(u)}}function be({accountId:e,accountModule:t,beUrl:n,devJwtSecret:r,tokenBrokerUrl:o,tokenBrokerAudience:i,defaultLocale:a,tools:s={},beacon:f}){let p=null;const d=Et({accountId:e,tools:s,beacon:f}),u=kt(d),c=ue({accountId:e,...r?{devJwtSecret:r}:{},...o?{tokenBrokerUrl:o}:{},...i?{tokenBrokerAudience:i}:{}});return async(l,y,g,h)=>{const E=await Se(l||{},h);p||=new ie({accountId:e,locale:E?.locale||a,parentUrl:window.location.href,rpc:d});try{await dt({request:E,accountModule:{...t,accountId:e},contextStore:p,beClient:{invoke({op:w,input:ee,signal:Fe,cacheTtlS:En,cacheKey:Pn}){return it({beUrl:n,accountId:e,jwtProvider:c,parentUrl:window.location.href,op:w,input:ee,cacheTtlS:En,cacheKey:Pn,...Fe?{signal:Fe}:{}})}},toolBridge:u,emit:w=>G(y,w),rpc:d,signal:g})}catch(w){g?.aborted||(G(y,fe(w)),y.onDone())}}}function Et({accountId:e,tools:t,beacon:n}){const r=o=>n?.({...o,accountId:o.accountId||e});return((o,i)=>te(o,i,{tools:t,beacon:r,memory:sessionStorage}))}async function Se(e,t){if(!t||!t.type?.startsWith("image/"))return e;const n=await jt(t),r={...Pt(e.payload??e.action?.payload),image_data_url:n.dataUrl,image_mime:n.mime};return{...e,payload:r,...e.action?{action:{...e.action,payload:r}}:{}}}function Pt(e){return xt(e)?{...e}:typeof e=="string"?{text:e}:{}}function xt(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}async function jt(e){const t=await It(e);return t||{dataUrl:await _e(e),mime:e.type}}async function It(e){if(typeof createImageBitmap!="function"||typeof document>"u")return null;let t=null;try{t=await createImageBitmap(e);const n=Math.min(1,At/Math.max(t.width,t.height)),r=Math.max(1,Math.round(t.width*n)),o=Math.max(1,Math.round(t.height*n)),i=document.createElement("canvas");i.width=r,i.height=o;const a=i.getContext("2d");if(!a)return null;a.drawImage(t,0,0,r,o);const s=await new Promise(f=>i.toBlob(f,"image/jpeg",Tt));return s?{dataUrl:await _e(s),mime:s.type||"image/jpeg"}:null}catch{return null}finally{t?.close()}}async function _e(e){const t=new Uint8Array(await e.arrayBuffer());let n="";const r=32768;for(let o=0;o<t.length;o+=r)n+=String.fromCharCode(...t.subarray(o,o+r));return`data:${e.type};base64,${btoa(n)}`}function G(e,t){switch(t?.type){case"text_chunk":{const n=t;e.onTextChunk(n.content||"",n.final===!0,n);break}case"ui_spec":{const n=t;e.onUISpec(n.spec,n.widget,n.panelHint,n.clearPanel===!0);break}case"action":e.onAction(t);break;case"metadata":e.onMetadata(t);break;case"error":e.onError(vt(t));break;case"done":e.onDone();break;default:break}}function vt(e){const t=new Error(e.message||e.code||"Agent error");return e.code&&(t.code=e.code),t}var ke="__gengageAgentFetchBridge";function Ct({accountId:e,streamTransport:t,endpoints:n={}}){if(!e)throw new Error("accountId is required.");if(typeof t!="function")throw new Error("streamTransport is required.");const r=Mt(),o=`https://gengage-injector.invalid/${encodeURIComponent(e)}`,i=new Set,a=s=>{const f=`${o}/chat/${s}`;i.add(f);const p=n[s],d={streamTransport:t,endpoint:s};p&&(d.endpointHandler=p),r.routes.set(f,d)};a("process_action");for(const s of Object.keys(n))s!=="process_action"&&a(s);return{middlewareUrl:o,stop(){for(const s of i)r.routes.delete(s)}}}function Mt(){const e=window,t=e[ke];if(t)return t;const n=e.fetch.bind(e),r={routes:new Map,originalFetch:n};return e.fetch=(o,i)=>{const a=Ae(o),s=r.routes.get(a);return s?Ut(s,o,i):n(o,i)},e[ke]=r,r}function Ae(e){return typeof e=="string"?e:e instanceof URL?e.href:e?.url||""}function Te(e,t){if(t?.signal)return t.signal;if(typeof Request<"u"&&e instanceof Request)return e.signal}function Ee(e,t){return t?.body!==void 0&&t?.body!==null?t.body:typeof Request<"u"&&e instanceof Request?e.clone().text():null}async function Rt(e,t){const n=Ee(e,t);if(n instanceof FormData){const o=n.get("request"),i=n.get("attachment");return{request:JSON.parse(String(o||"{}")),...i instanceof File?{attachment:i}:{}}}const r=await Promise.resolve(n);return typeof r=="string"?{request:JSON.parse(r||"{}")}:{request:{}}}async function Ut(e,t,n){if(e.endpoint&&e.endpoint!=="process_action"&&e.endpointHandler){const p=new AbortController,d=Te(t,n);let u=null;if(d){const c=()=>p.abort();d.aborted&&c(),d.addEventListener("abort",c,{once:!0}),u=()=>d.removeEventListener("abort",c)}try{const c=await Ot(t,n);return await e.endpointHandler(c,{accountId:Bt(t),endpoint:e.endpoint,signal:p.signal})}finally{u?.()}}const r=new TextEncoder,o=new AbortController,i=Te(t,n);let a=!1,s=null;const f=new ReadableStream({async start(p){const d=l=>{a||p.enqueue(r.encode(`${JSON.stringify(l)}
|
|
4
|
+
`))},u=()=>{a||(a=!0,s?.(),p.close())},c=l=>{d({type:"error",code:l?.code||"agent_bridge_error",message:l instanceof Error?l.message:String(l)}),d({type:"done"}),u()};if(i){const l=()=>{o.abort(),a||(a=!0,s?.(),p.error(new DOMException("Aborted","AbortError")))};if(i.aborted){l();return}i.addEventListener("abort",l,{once:!0}),s=()=>i.removeEventListener("abort",l)}try{const{request:l,attachment:y}=await Rt(t,n),g=e.streamTransport(l,{onTextChunk:(h,E,w={})=>d({type:"text_chunk",content:h,final:E===!0,...w}),onUISpec:(h,E,w,ee)=>d({type:"ui_spec",spec:h,widget:E,...w?{panelHint:w}:{},...ee?{clearPanel:!0}:{}}),onAction:h=>{d(h?.type==="action"?h:{type:"action",action:h})},onMetadata:h=>{d(h?.type==="metadata"?h:{type:"metadata",...h})},onError:c,onDone:()=>{d({type:"done"}),u()}},o.signal,y);Nt(g)&&(await g,o.signal.aborted||(d({type:"done"}),u()))}catch(l){o.signal.aborted||c(l)}},cancel(){o.abort(),s?.(),a=!0}});return new Response(f,{status:200,headers:{"Content-Type":"application/x-ndjson"}})}async function Ot(e,t){const n=await Promise.resolve(Ee(e,t));return typeof n!="string"||n.trim()===""?{}:JSON.parse(n)}function Bt(e){try{const[t]=new URL(Ae(e)).pathname.split("/").filter(Boolean);return decodeURIComponent(t||"")}catch{return""}}function Nt(e){return e!==null&&(typeof e=="object"||typeof e=="function")&&typeof e.then=="function"}function $t({accountId:e,beUrl:t,devJwtSecret:n,tokenBrokerUrl:r,tokenBrokerAudience:o,workerUrl:i,defaultLocale:a="en-GB",accountModule:s,tools:f={},beacon:p,allowBlobWorker:d=!1}){if(!e)throw new Error("accountId is required.");if(!t)throw new Error("beUrl is required. The injector owns backend URLs; the SDK has no default.");if(!i)throw new Error("workerUrl is required.");const u=window,c=u.GengageAssistantInjector||(u.GengageAssistantInjector={}),l=c[e]||(c[e]={});if(l.agentController)return l.agentController;const y=J(i)||d?Pe(i,`gengage-${e}-agent`,{allowBlobWorker:d}):null;l.streamTransport=y?we({accountId:e,worker:y.worker,beUrl:t,...n?{devJwtSecret:n}:{},...r?{tokenBrokerUrl:r}:{},...o?{tokenBrokerAudience:o}:{},defaultLocale:a,tools:f,...p?{beacon:p}:{}}):be({accountId:e,accountModule:s,beUrl:t,...n?{devJwtSecret:n}:{},...r?{tokenBrokerUrl:r}:{},...o?{tokenBrokerAudience:o}:{},defaultLocale:a,tools:f,...p?{beacon:p}:{}});const g={type:"agent",stop(){delete l.streamTransport,y?.worker.terminate(),y?.cleanup(),delete l.agentController},diagnostics(){return{accountId:e,beUrl:t,workerUrl:i,mounted:!0,transport:y?"worker":"main-thread",flows:Object.keys(s?.flows||{})}}};return l.agentController=g,g}function J(e){const t=new URL(e,window.location.href);return t.origin===window.location.origin||t.protocol==="blob:"}function Pe(e,t,n={}){const r=new URL(e,window.location.href);if(J(e))return{worker:new Worker(r.href,{type:"module",name:t}),cleanup(){}};if(!n.allowBlobWorker)throw new Error("Cross-origin agent workers require allowBlobWorker=true or a same-origin workerUrl.");const o=new Blob([`import ${JSON.stringify(r.href)};
|
|
5
|
+
`],{type:"text/javascript"}),i=URL.createObjectURL(o);try{return{worker:new Worker(i,{type:"module",name:t}),cleanup(){URL.revokeObjectURL(i)}}}catch(a){throw URL.revokeObjectURL(i),a}}function xe(e){return e&&typeof e=="object"&&!Array.isArray(e)?e:null}function O(e){const t=xe(e);if(!t)return null;const n=j(t.sku,t.SKU);if(!n)return null;const r={sku:n,name:j(t.name,t.title,t.short_name,n)||n,url:j(t.url)||""},o=Array.isArray(t.images)?t.images:void 0,i=j(t.imageUrl,t.image_url,t.image,o?.[0]);i&&(r.imageUrl=i),o&&o.length>1&&(r.images=o.filter(g=>!!g).map(String));const a=M(t.price_discounted),s=M(t.price),f=a||s;f>0&&(r.price=String(f));const p=a>0?s:0;p>0&&(r.originalPrice=String(p));const d=j(t.brand);d&&(r.brand=d);const u=M(t.rating);u>0&&(r.rating=u);const c=M(t.review_count)||M(t.reviewCount);c>0&&(r.reviewCount=c);const l=j(t.cart_code,t.cartCode);l&&(r.cartCode=l),typeof t.in_stock=="boolean"&&(r.inStock=t.in_stock),typeof t.inStock=="boolean"&&(r.inStock=t.inStock);const y=t.category_names;return Array.isArray(y)&&(r.categoryNames=y.map(String)),r}function je(e){const t=xe(e);if(!t)return e;const n=t.category_names,r=Array.isArray(n)?n:void 0,o=t.images,i=Array.isArray(o)?o:void 0,a={sku:t.sku,name:t.name||t.title,url:t.url,price:t.price,currency:t.price_currency||t.currency,category:r?.[r.length-1]??void 0,category_names:r?r.slice(0,4):void 0,image:i?i[0]:t.image,in_stock:t.in_stock,rating:t.rating,review_count:t.review_count};for(const s of Object.keys(a))a[s]===void 0&&delete a[s];return a}function H(e){return Array.isArray(e)?e.map(je).filter(t=>!!t):[]}function j(...e){for(const t of e)if(typeof t=="string"&&t.trim())return t.trim();return""}function M(e){const t=Number(e);return Number.isFinite(t)?t:0}function Ie(e){return Array.isArray(e)?e.map(String).filter(Boolean):typeof e=="string"&&e?[e]:[]}function ve(e=[]){const t={},n=[];return(Array.isArray(e)?e.map(O).filter(r=>!!r):[]).forEach((r,o)=>{const i=`product-${o.toString()}`;n.push(i);const a={sku:r.sku,product:r};t[i]={type:"ProductCard",props:{product:r,index:o,action:{title:r.name,type:"launchSingleProduct",payload:a}}}}),t.root={type:"ProductGrid",props:{layout:"grid"},children:n},{widget:"chat",panelHint:"panel",spec:{root:"root",elements:t}}}function Lt(e){return{widget:"chat",panelHint:"panel",spec:{root:"root",elements:{root:{type:"ProductDetailsPanel",props:{product:O(e)||e||{}}}}}}}function Dt(e={}){const t=(e.multiple_product_details||e.products||[]).map(O).filter(r=>!!r),n=e.table||{};return{widget:"chat",panelHint:"panel",spec:{root:"root",elements:{root:{type:"ComparisonTable",props:{products:t,recommended:t.find(r=>r.sku===e.recommended_choice_sku)||t[0],attributes:Object.entries(n).map(([r,o])=>({label:r,values:Array.isArray(o)?o.map(String):[String(o??"")]})),highlights:Ie(e.key_differences),specialCases:Ie(e.special_considerations),recommendedText:e.recommended_choice,productActions:Object.fromEntries(t.map(r=>[r.sku,{title:r.name,type:"launchSingleProduct",payload:{sku:r.sku,product:r}}]))}}}}}}function Ft(e=[]){return{widget:"chat",panelHint:"inline",spec:{root:"root",elements:{root:{type:"ActionButtons",props:{buttons:(Array.isArray(e)?e:[]).map(t=>{const n=t.label||t.title||t.shortName||"",r=t.action||t.requestDetails||t.request_details;return!n||!r?.type?null:{label:n,action:{title:n,type:r.type,...r.payload!==void 0?{payload:r.payload}:{}}}}).filter(t=>!!t)}}}}}}function B(e){const t=e?.payload;if(typeof t=="string")return t;if(t&&typeof t=="object"&&"text"in t){const r=t.text;if(typeof r=="string")return r}const n=e?.action;return n&&typeof n.payload=="string"?n.payload:n&&typeof n.title=="string"?n.title:""}function Ce(e){return Object.freeze([...e])}function P(e){return e&&typeof e=="object"?e:{}}function K(e){return e&&typeof e=="object"?e:{}}function Me(e){return e.thread.extensions.beauty_profile}var qt=Ce([{kind:"be_op",op:"beauty-consulting-turn",input:({request:e,context:t})=>({utterance:B(e),prior_messages:t.messages.slice(-10),profile:Me(t),candidate_products:Gt(e),locale:t.meta.locale}),out:"consultation",patch:Re},{kind:"branch",on:({bag:e})=>P(e.consultation).consultation_state?.stage||"answer",cases:{refuse:[{kind:"refusal",message:({bag:e})=>{const t=P(e.consultation);return t.refusal?.message||t.plain_text||"I cannot help with that request."}}],needs_more_info:[{kind:"emit",build:({bag:e})=>C(V(P(e.consultation)),!0)},{kind:"commit"}],search:[{kind:"tool",name:"search",input:({request:e,bag:t,context:n})=>{const r=P(t.consultation);return{query:r.search_params?.query||B(e),facets:r.search_params?.facets||{},limit:12,locale:n.meta.locale}},out:"products",patch:(e,t)=>{const n=K(t);return{panel:{...e.panel,screen_sku_list:W(n).map(r=>zt(r)).filter(r=>!!r),last_search:{query:n.query||""}}}}},{kind:"emit",build:({bag:e})=>me(ve(W(K(e.products))))},{kind:"be_op",op:"beauty-consulting-turn",input:({request:e,context:t,bag:n})=>({utterance:B(e),prior_messages:t.messages.slice(-10),profile:Me(t),candidate_products:H(W(K(n.products)).slice(0,12)),locale:t.meta.locale}),out:"answer",patch:(e,t)=>{const n=Re(e,t),r=(P(t).product_mentions||[]).map(o=>o.sku).filter(o=>!!o);return{...n||{},panel:{...e.panel,chat_mentioned_skus:r}}}},{kind:"emit",build:({bag:e})=>{const t=P(e.answer);return C(V(t),!0,{productMentions:t.product_mentions||[]})}},{kind:"commit"}],default:[{kind:"emit",build:({bag:e})=>{const t=P(e.consultation);return C(V(t),!0,{productMentions:t.product_mentions||[]})}},{kind:"commit"}]}}]);function Re(e,t){const n=P(t).consultation_state?.captured_profile;if(!n)return null;const r=e.thread.extensions.beauty_profile&&typeof e.thread.extensions.beauty_profile=="object"?e.thread.extensions.beauty_profile:{};return{thread:{...e.thread,extensions:{...e.thread.extensions,beauty_profile:{...r,...n}}}}}function W(e){return Array.isArray(e?.products)?e.products:[]}function zt(e){if(e&&typeof e=="object"&&"sku"in e){const t=e.sku;if(typeof t=="string")return t}}function Gt(e){const t=e?.payload&&typeof e.payload=="object"?e.payload.products:void 0,n=e?.action?.payload&&typeof e.action.payload=="object"?e.action.payload.products:void 0,r=t||n;return Array.isArray(r)?H(r.slice(0,12)):void 0}function V(e){return e?.answer_html||e?.plain_text||e?.refusal?.message||"I can help with beauty shopping."}var Ue=new Set(["and","are","box","for","from","gift","gifts","the","with"]),Jt={token:.26,keyword:.22,category:.18,facet:.16,color:.08,attribute:.06,price:.08,retrieval:.08},Ht=["amber","beige","black","blue","blush","bronze","brown","cream","gold","green","grey","ivory","lilac","mixed","orange","pastel","pink","purple","red","silver","white","yellow"];function A(e){return String(e??"").normalize("NFKD").replace(new RegExp("\\p{Diacritic}","gu"),"").toLowerCase().replace(/[^\p{L}\p{N}]+/gu," ").replace(/\s+/gu," ").trim()}function _(e,t=Ue){const n=t instanceof Set?t:new Set([...Ue,...t]),r=new Set,o=[];for(const i of A(e).split(/\s+/u))i.length<3||n.has(i)||r.has(i)||(r.add(i),o.push(i));return o}function R(e,t={}){if(Qt(e))return e;const n=t.facetKeys,r=b(e.category_names??e.category),o=b(e.category_ids),i=Yt(e.facet_hits),a=Object.entries(i).filter(([g])=>!n||n.some(h=>A(h)===A(g))).map(([,g])=>g),s=k(e.name,e.title),f=k(e.brand),p=t.sourceKeywords??[],d=[s,f,...r,...a,...b(e.promotions),k(e.description),...p].join(" "),u=_(d,t.stopWords),c=k(t.sourceProductType,en(r,s,u),u[0]),l=T([...Zt(d,Ht),...Object.entries(i).filter(([g])=>/colou?r/iu.test(g)).flatMap(([,g])=>_(g,t.stopWords))]),y=T([...a.flatMap(g=>_(g,t.stopWords)),...b(e.promotions).flatMap(g=>_(g,t.stopWords))]).slice(0,24);return{sku:k(e.sku),title:s,brand:f,productType:c,categoryNames:r,categoryIds:o,facets:i,keywords:T([...p.flatMap(g=>_(g,t.stopWords)),...u]).slice(0,12),tokens:u,colors:l,attributes:y,price:Xt(e)}}function Kt(e,t={}){const n=k(e.product_type,e.productType),r=k(e.title,n,b(e.keywords).join(" ")),o=T([...b(e.keywords),...b(e.colors),...b(e.materials),...b(e.styles),...b(e.attributes)]);return R({sku:"__photo__",name:r,category_names:n?[n]:[],facet_hits:{colors:b(e.colors).join(", "),materials:b(e.materials).join(", "),styles:b(e.styles).join(", "),attributes:b(e.attributes).join(", ")},promotions:o},{...t,sourceProductType:n,sourceKeywords:o})}function Wt(e,t={}){const n=R(e,t);return T([...t.sourceQueries??[],N([n.productType,...n.colors.slice(0,1),...n.attributes.slice(0,2)]),N([n.productType,...n.keywords.slice(0,3)]),N([n.categoryNames[n.categoryNames.length-1],...n.keywords.slice(0,2)]),N(n.title.split(/\s+/u).slice(0,6)),n.productType].map(r=>r.trim()).filter(Boolean)).slice(0,t.queryLimit??6)}function Oe(e,t,n={}){const r={...Jt,...n.weights},o=R(t,n),i=[],a=I(e.tokens,o.tokens),s=I(e.keywords,o.tokens),f=tn(e.categoryNames,o.categoryNames)||I(Be(e.categoryNames),Be(o.categoryNames)),p=I(e.attributes,o.attributes),d=I(e.colors,o.colors),u=I(e.attributes,o.tokens),c=nn(e.price,o.price),l=rn(o.sku,n.hitCounts)>0?1:0,y=on(e,o),g=a*r.token+s*r.keyword+f*r.category+p*r.facet+d*r.color+u*r.attribute+c*r.price+l*r.retrieval,h=an(y?g:g*.35);return y&&e.productType&&e.productType===o.productType&&i.push("same product type"),f>.4&&i.push("similar category"),p>.2&&i.push("matching product attributes"),d>0&&i.push("matching colour"),c>.8&&i.push("similar price"),y||i.push("weaker product-type match"),{score:Number(h.toFixed(4)),reasons:i}}function Vt(e,t,n={}){const r=R(e,n),o=new Set([r.sku,...n.ignoreSkus??[]].filter(Boolean));return t.filter(i=>{const a=k(i.sku);return a&&!o.has(a)}).map(i=>{const a=Oe(r,i,n);return{...i,similarity_score:a.score,similarity_reasons:a.reasons}}).sort((i,a)=>a.similarity_score-i.similarity_score).slice(0,Math.max(1,Math.min(n.limit??12,100)))}function Qt(e){return!!(e&&typeof e=="object"&&Array.isArray(e.tokens))}function k(...e){for(const t of e)if(typeof t=="string"&&t.trim())return t.trim();return""}function b(e){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):[]}function Yt(e){return!e||typeof e!="object"||Array.isArray(e)?{}:Object.fromEntries(Object.entries(e).map(([t,n])=>[t,k(n)]).filter(([,t])=>t))}function Xt(e){const t=Number(e.price_discounted);if(Number.isFinite(t)&&t>0)return t;const n=Number(e.price);return Number.isFinite(n)&&n>0?n:0}function T(e){const t=new Set,n=[];for(const r of e){const o=A(r);!o||t.has(o)||(t.add(o),n.push(o))}return n}function Zt(e,t){const n=` ${A(e)} `;return t.filter(r=>n.includes(` ${A(r)} `))}function en(e,t,n){const r=A(e[e.length-1]??e[0]??"");if(r){const o=_(r),i=new Set(_(t));return o.find(a=>i.has(a))??o[0]??r}return n[0]??""}function N(e){return e.map(t=>A(t)).filter(Boolean).join(" ").replace(/\s+/gu," ").trim().slice(0,120)}function Be(e){return e.flatMap(t=>_(t))}function I(e,t){const n=T(e),r=new Set(T(t));return n.length===0||r.size===0?0:n.filter(o=>r.has(o)).length/n.length}function tn(e,t){const n=T(e),r=new Set(T(t));return n.length===0||r.size===0?0:n.filter(o=>r.has(o)).length/n.length}function nn(e,t){return e<=0||t<=0?0:Math.min(e,t)/Math.max(e,t)}function rn(e,t){return!e||!t?0:typeof t.get=="function"?t.get(e)??0:t[e]??0}function on(e,t){if(!e.productType||!t.productType||e.productType===t.productType)return!0;const n=new Set(_(e.productType)),r=new Set([..._(t.productType),...t.tokens]);return[...n].some(o=>r.has(o))}function an(e){return Math.max(0,Math.min(1,e))}function sn(e){const t=(e?.chat||e?._chat)?.root||e?._chat?.root;return t?t.getRootNode?.()?.host||t:Array.from(document.querySelectorAll("*")).find(n=>n.shadowRoot?.querySelector?.(".gengage-chat-root, .gengage-chat-launcher-container"))}function cn(e,t){const n=sn(e);n?.style&&(t?n.style.removeProperty("display"):n.style.setProperty("display","none","important")),t||(e?.chat||e?._chat)?.close?.()}var un="nd_be_url";function ln(e,t){const n=dn()||e.beUrl||t;if(!n)throw new Error("resolveBeUrl: backend URL is required. The injector must supply a fallback URL; the SDK has no default.");return n}function dn(){try{const e=new URLSearchParams(window.location.search).get(un)?.trim();if(!e)return null;const t=new URL(e);return t.protocol!=="https:"&&t.protocol!=="http:"?null:t.toString().replace(/\/+$/u,"")}catch{return null}}var fn="entries",pn=1,mn={volatile:"Hot per-page state and sensitive request context.",session:"Current-visit tool context without raw tokens or PII-heavy payloads.",local:"Small, non-sensitive capability facts or user preferences.",indexedDb:"Larger product/search payload caches with short TTLs."},Ne=new Map;function Q(){return Date.now()}function Y(e){return e?.expiresAt!=null&&e.expiresAt<=Q()}function X(e,t){return{value:e,createdAt:Q(),expiresAt:t?Q()+t:null}}function yn(e,t,n){try{const r=e.getItem(`${t}${n}`);if(!r)return null;const o=JSON.parse(r);return Y(o)?(e.removeItem(`${t}${n}`),null):o.value}catch{return null}}function gn(e,t,n,r,o){try{return e.setItem(`${t}${n}`,JSON.stringify(X(r,o))),!0}catch{return!1}}function hn(e,t,n){try{e.removeItem(`${t}${n}`)}catch{}}function $e(e){try{return window[e]}catch{return null}}function Le(e,t){return e?{get:n=>yn(e,t,n),set:(n,r,o={})=>gn(e,t,n,r,o.ttlMs),remove:n=>hn(e,t,n)}:{get:()=>null,set:()=>!1,remove:()=>{}}}function Z(e){if(Array.isArray(e))return`[${e.map(Z).join(",")}]`;if(e&&typeof e=="object"){const t=e;return`{${Object.keys(t).sort().map(n=>`${JSON.stringify(n)}:${Z(t[n])}`).join(",")}}`}return JSON.stringify(e)}function De(e){let t=2166136261;const n=Z(e);for(let r=0;r<n.length;r+=1)t^=n.charCodeAt(r),t=Math.imul(t,16777619);return(t>>>0).toString(36)}function wn(e,t){const n=t.dbName||`gengage-${e}`,r=t.dbStore||fn,o=t.dbVersion||pn,i=new Map;let a=null;const s=()=>"indexedDB"in window?a||(a=new Promise(u=>{const c=indexedDB.open(n,o);c.onupgradeneeded=()=>{c.result.createObjectStore(r,{keyPath:"key"})},c.onsuccess=()=>u(c.result),c.onerror=()=>u(null),c.onblocked=()=>u(null)}),a):Promise.resolve(null);return{volatileEntries:i,idbGet:async u=>{const c=await s();return c?new Promise(l=>{const y=c.transaction(r,"readwrite").objectStore(r),g=y.get(u);g.onsuccess=()=>{const h=g.result;if(!h||Y(h)){h&&y.delete(u),l(null);return}l(h.value)},g.onerror=()=>l(null)}):null},idbSet:async(u,c,l={})=>{const y=await s();return y?new Promise(g=>{const h=y.transaction(r,"readwrite");h.oncomplete=()=>g(!0),h.onerror=()=>g(!1),h.objectStore(r).put({key:u,...X(c,l.ttlMs)})}):!1},idbRemove:async u=>{const c=await s();c&&c.transaction(r,"readwrite").objectStore(r).delete(u)}}}function bn(e,t={}){const n=window,r=n.gengage||(n.gengage={}),o=r.memory||(r.memory={}),i=o[e];if(i)return i;const a=Ne.get(e)||wn(e,t);Ne.set(e,a);const s=t.sessionPrefix||`gengage:${e}:session:`,f=t.localPrefix||`gengage:${e}:local:`,d={get:l=>{const y=a.volatileEntries.get(l);return y?Y(y)?(a.volatileEntries.delete(l),null):y.value:null},set:(l,y,g={})=>(a.volatileEntries.set(l,X(y,g.ttlMs)),!0),remove:l=>{a.volatileEntries.delete(l)},clear:()=>a.volatileEntries.clear()},u={get:a.idbGet,set:a.idbSet,remove:a.idbRemove},c={accountId:e,volatile:d,session:Le($e("sessionStorage"),s),local:Le($e("localStorage"),f),indexedDb:u,stableKey:De,policy:{...mn,...t.policy||{}}};o[e]=c;for(const l of t.aliases||[])o[l]=c;return c}var Sn=["addToCart","search","searchKeyword","facetedSearch","searchGiftOptions","similaritySearch"];function _n(e,t,n={}){const r=window,o=r.gengage||(r.gengage={}),i=o.tools||(o.tools={});i[e]=t;for(const a of n.accountAliases||[])i[a]=t;if(n.exposeStandardAliases!==!1)for(const a of n.standardAliases||Sn){const s=t[a];typeof s=="function"&&(i[a]=i[a]||s)}return t}function kn({getPageType:e,getProduct:t}){return()=>({url:window.location.href,title:document.title,pageType:e(),product:t()})}function An({accountId:e,getPageType:t,getProduct:n,getToolNames:r,getSearchCapabilities:o,getMemory:i}){return async()=>({accountId:e,url:window.location.href,pageType:t(),productSku:n()?.sku??null,toolNames:r(),...o?{searchCapabilities:o()}:{},...i?{memoryPolicy:i().policy}:{},timestamp:new Date().toISOString()})}function Tn({accountId:e,runtimeFile:t="runtime.js",startExport:n="start",globalBaseUrlKey:r,errorLabel:o}={}){if(!e)throw new Error("accountId is required.");const i=o||e,a=window,s=()=>{const u=a.GengageInjectorConfig||{},c=u[e]||{};return{...u,...c}},f=()=>{const u=s();if(u.runtimeUrl)return u.runtimeUrl;const c=u.assetBaseUrl||u.baseUrl||document.currentScript?.getAttribute("src")||(r?a[r]:null);if(!c)throw new Error(`${i} runtime URL cannot be resolved.`);return new URL(t,c).href},p=a.GengageAssistantInjector||(a.GengageAssistantInjector={}),d=p[e]||(p[e]={});return d.loaderPromise||(d.loaderPromise=import(f()).then(u=>{const c=u[n];if(typeof c!="function")throw new Error(`${i} runtime export ${n} is unavailable.`);return c()}).catch(u=>{throw delete d.loaderPromise,console.error(`[Gengage][${e}] runtime load failed`,u),u})),d.loaderPromise}m.AgentError=$,m.ContextStore=ie,m.action=lt,m.actionButtonsUiSpec=Ft,m.beErrorToAgentError=L,m.beautyConsultingTurnFlow=qt,m.buildPhotoSimilarityProfile=Kt,m.buildProductSimilarityProfile=R,m.buildSimilarityQueries=Wt,m.canUseModuleWorker=J,m.caughtToStreamError=fe,m.comparisonUiSpec=Dt,m.createBrowserMemory=bn,m.createDiagnosticsTool=An,m.createFlow=Ce,m.createInjectorAdapter=we,m.createMainThreadInjectorAdapter=be,m.createModuleWorker=Pe,m.createReadPageTool=kn,m.done=D,m.elapsedMs=U,m.error=ye,m.httpErrorToAgentError=de,m.installBrowserTools=_n,m.installFetchTransportBridge=Ct,m.metadata=ut,m.mountAccount=$t,m.normalizeProduct=O,m.normalizeSimilarityText=A,m.nowIso=ne,m.productDetailsUiSpec=Lt,m.productsUiSpec=ve,m.rankSimilarProducts=Vt,m.requestText=B,m.resolveBeUrl=ln,m.resolveFlow=ge,m.scoreSimilarityCandidate=Oe,m.setAssistantHostVisible=cn,m.stableKey=De,m.startLazyRuntimeLoader=Tn,m.textChunk=C,m.tokenizeSimilarityText=_,m.trimProductFactsCore=je,m.trimProductFactsListCore=H,m.uiSpec=me})(this.Gengage=this.Gengage||{});
|
package/dist/chat-runtime.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { c as e, d as t, i as r, l as s, n, o as i, r as l, s as C, t as c, u as d } from "./runtime-
|
|
1
|
+
import { c as e, d as t, i as r, l as s, n, o as i, r as l, s as C, t as c, u as d } from "./runtime-_HRuAyrh.js";
|
|
2
2
|
export {
|
|
3
3
|
s as CHAT_SCROLL_ELEMENT_ID,
|
|
4
4
|
r as ChatPresentationState,
|