@ourguide-ai/client 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +6 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +303 -0
- package/dist/index.js +697 -0
- package/dist/index.js.map +1 -0
- package/package.json +46 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,697 @@
|
|
|
1
|
+
function K(e) {
|
|
2
|
+
return new Promise((t, s) => {
|
|
3
|
+
const n = new FileReader();
|
|
4
|
+
n.onload = () => {
|
|
5
|
+
const o = n.result;
|
|
6
|
+
t(o.split(",")[1]);
|
|
7
|
+
}, n.onerror = () => s(new Error("Failed to read file")), n.readAsDataURL(e);
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
async function Y(e, t) {
|
|
11
|
+
const s = `${e}/api/products/${t}/config`;
|
|
12
|
+
console.log("[Ourguide] fetchConfig HTTP GET", s);
|
|
13
|
+
try {
|
|
14
|
+
const n = await fetch(s, {
|
|
15
|
+
headers: { "ngrok-skip-browser-warning": "true" }
|
|
16
|
+
});
|
|
17
|
+
if (console.log("[Ourguide] fetchConfig HTTP status:", n.status, n.statusText), !n.ok)
|
|
18
|
+
return console.warn("[Ourguide] fetchConfig non-OK response, returning {}"), {};
|
|
19
|
+
const o = await n.json();
|
|
20
|
+
return console.log("[Ourguide] fetchConfig raw JSON:", JSON.stringify(o, null, 2)), o;
|
|
21
|
+
} catch (n) {
|
|
22
|
+
return console.error("[Ourguide] fetchConfig threw:", n), {};
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
async function H(e, t, s, n, o, r) {
|
|
26
|
+
const i = { productId: t, messages: s, endUserSessionId: n };
|
|
27
|
+
o && (i.conversationId = o), r && r.length > 0 && (i.attachments = r);
|
|
28
|
+
const l = await fetch(`${e}/api/chat`, {
|
|
29
|
+
method: "POST",
|
|
30
|
+
headers: {
|
|
31
|
+
"Content-Type": "application/json",
|
|
32
|
+
"ngrok-skip-browser-warning": "true"
|
|
33
|
+
},
|
|
34
|
+
body: JSON.stringify(i)
|
|
35
|
+
});
|
|
36
|
+
if (!l.ok) {
|
|
37
|
+
const a = await l.json().catch(() => ({ error: "Request failed" }));
|
|
38
|
+
throw new Error(a.error || `HTTP ${l.status}`);
|
|
39
|
+
}
|
|
40
|
+
if (!l.body)
|
|
41
|
+
throw new Error("No response stream available");
|
|
42
|
+
return {
|
|
43
|
+
stream: l.body,
|
|
44
|
+
conversationId: l.headers.get("X-Conversation-Id")
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
async function Q(e, t, s, n, o) {
|
|
48
|
+
const r = await fetch(`${e}/api/widget/identify`, {
|
|
49
|
+
method: "POST",
|
|
50
|
+
headers: {
|
|
51
|
+
"Content-Type": "application/json",
|
|
52
|
+
"ngrok-skip-browser-warning": "true"
|
|
53
|
+
},
|
|
54
|
+
body: JSON.stringify({ productId: t, endUserSessionId: s, token: n, name: o })
|
|
55
|
+
}), i = await r.json().catch(() => ({}));
|
|
56
|
+
if (!r.ok)
|
|
57
|
+
throw new Error(i.error || `HTTP ${r.status}`);
|
|
58
|
+
return i;
|
|
59
|
+
}
|
|
60
|
+
async function Z(e, t, s) {
|
|
61
|
+
const n = await fetch(`${e}/api/widget/reset-user`, {
|
|
62
|
+
method: "POST",
|
|
63
|
+
headers: {
|
|
64
|
+
"Content-Type": "application/json",
|
|
65
|
+
"ngrok-skip-browser-warning": "true"
|
|
66
|
+
},
|
|
67
|
+
body: JSON.stringify({ productId: t, endUserSessionId: s })
|
|
68
|
+
}), o = await n.json().catch(() => ({}));
|
|
69
|
+
if (!n.ok)
|
|
70
|
+
throw new Error(o.error || `HTTP ${n.status}`);
|
|
71
|
+
return o;
|
|
72
|
+
}
|
|
73
|
+
const tt = {
|
|
74
|
+
isOpen: !1,
|
|
75
|
+
messages: [],
|
|
76
|
+
isStreaming: !1,
|
|
77
|
+
error: null
|
|
78
|
+
};
|
|
79
|
+
function j() {
|
|
80
|
+
return Date.now().toString(36) + Math.random().toString(36).slice(2, 7);
|
|
81
|
+
}
|
|
82
|
+
function et(e, t) {
|
|
83
|
+
var s;
|
|
84
|
+
switch (t.type) {
|
|
85
|
+
case "OPEN":
|
|
86
|
+
return { ...e, isOpen: !0, error: null };
|
|
87
|
+
case "CLOSE":
|
|
88
|
+
return { ...e, isOpen: !1 };
|
|
89
|
+
case "LOAD_MESSAGES":
|
|
90
|
+
return { ...e, messages: t.messages };
|
|
91
|
+
case "ADD_USER_MESSAGE":
|
|
92
|
+
return {
|
|
93
|
+
...e,
|
|
94
|
+
error: null,
|
|
95
|
+
messages: [
|
|
96
|
+
...e.messages,
|
|
97
|
+
{ id: t.id, role: "user", content: t.content }
|
|
98
|
+
]
|
|
99
|
+
};
|
|
100
|
+
case "START_STREAMING":
|
|
101
|
+
return {
|
|
102
|
+
...e,
|
|
103
|
+
isStreaming: !0,
|
|
104
|
+
error: null,
|
|
105
|
+
messages: [
|
|
106
|
+
...e.messages,
|
|
107
|
+
{ id: t.id, role: "assistant", content: "", isStreaming: !0 }
|
|
108
|
+
]
|
|
109
|
+
};
|
|
110
|
+
case "APPEND_TEXT": {
|
|
111
|
+
const n = [...e.messages], o = n[n.length - 1];
|
|
112
|
+
return o && o.role === "assistant" && o.isStreaming && (n[n.length - 1] = { ...o, content: o.content + t.text }), { ...e, messages: n };
|
|
113
|
+
}
|
|
114
|
+
case "ADD_TOOL_CALL": {
|
|
115
|
+
const n = [...e.messages], o = n[n.length - 1];
|
|
116
|
+
if (o && o.role === "assistant") {
|
|
117
|
+
const r = [...o.toolCalls ?? [], t.toolCall];
|
|
118
|
+
n[n.length - 1] = { ...o, toolCalls: r };
|
|
119
|
+
}
|
|
120
|
+
return { ...e, messages: n };
|
|
121
|
+
}
|
|
122
|
+
case "SET_TOOL_RESULT": {
|
|
123
|
+
const n = [...e.messages], o = n[n.length - 1];
|
|
124
|
+
if (o && o.role === "assistant" && o.toolCalls) {
|
|
125
|
+
const r = o.toolCalls.map(
|
|
126
|
+
(i) => i.id === t.toolCallId ? { ...i, result: t.result, status: "done" } : i
|
|
127
|
+
);
|
|
128
|
+
n[n.length - 1] = { ...o, toolCalls: r };
|
|
129
|
+
}
|
|
130
|
+
return { ...e, messages: n };
|
|
131
|
+
}
|
|
132
|
+
case "FINISH_STREAMING": {
|
|
133
|
+
const n = [...e.messages], o = n[n.length - 1];
|
|
134
|
+
return o && o.role === "assistant" && o.isStreaming && (!o.content && ((s = o.toolCalls) != null && s.length) ? n.pop() : n[n.length - 1] = { ...o, isStreaming: !1 }), { ...e, isStreaming: !1, messages: n };
|
|
135
|
+
}
|
|
136
|
+
case "SET_ERROR": {
|
|
137
|
+
const n = [...e.messages], o = n[n.length - 1];
|
|
138
|
+
return o && o.role === "assistant" && o.isStreaming && (n[n.length - 1] = { ...o, isStreaming: !1 }), { ...e, isStreaming: !1, error: t.error, messages: n };
|
|
139
|
+
}
|
|
140
|
+
case "CLEAR_MESSAGES":
|
|
141
|
+
return { ...e, messages: [], error: null, isStreaming: !1 };
|
|
142
|
+
default:
|
|
143
|
+
return e;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
async function B(e, t) {
|
|
147
|
+
const s = e.getReader(), n = new TextDecoder();
|
|
148
|
+
let o = "";
|
|
149
|
+
try {
|
|
150
|
+
for (; ; ) {
|
|
151
|
+
const { done: r, value: i } = await s.read();
|
|
152
|
+
if (o += r ? n.decode() : n.decode(i, { stream: !0 }), r) break;
|
|
153
|
+
const l = o.split(`
|
|
154
|
+
|
|
155
|
+
`);
|
|
156
|
+
o = l.pop() ?? "";
|
|
157
|
+
for (const a of l)
|
|
158
|
+
G(a.trim(), t);
|
|
159
|
+
}
|
|
160
|
+
o.trim() && G(o.trim(), t), t.onFinish();
|
|
161
|
+
} catch (r) {
|
|
162
|
+
const i = r instanceof Error ? r.message : "Stream read failed";
|
|
163
|
+
t.onError(i);
|
|
164
|
+
} finally {
|
|
165
|
+
s.releaseLock();
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
function G(e, t) {
|
|
169
|
+
const s = e.split(`
|
|
170
|
+
`);
|
|
171
|
+
for (const n of s) {
|
|
172
|
+
if (!n.startsWith("data: ")) continue;
|
|
173
|
+
const o = n.slice(6);
|
|
174
|
+
if (o === "[DONE]") return;
|
|
175
|
+
try {
|
|
176
|
+
const r = JSON.parse(o);
|
|
177
|
+
switch (r.type) {
|
|
178
|
+
case "text-delta":
|
|
179
|
+
r.delta && t.onTextDelta(r.delta);
|
|
180
|
+
break;
|
|
181
|
+
// AI SDK v6: tool-input-available fires when tool input is fully parsed
|
|
182
|
+
case "tool-input-available":
|
|
183
|
+
r.toolCallId && r.toolName && t.onToolCall(
|
|
184
|
+
r.toolCallId,
|
|
185
|
+
r.toolName,
|
|
186
|
+
r.input ?? {},
|
|
187
|
+
r.providerMetadata
|
|
188
|
+
);
|
|
189
|
+
break;
|
|
190
|
+
// AI SDK v6: tool-output-available fires when tool execution completes
|
|
191
|
+
case "tool-output-available":
|
|
192
|
+
r.toolCallId && t.onToolResult(r.toolCallId, r.output);
|
|
193
|
+
break;
|
|
194
|
+
// Legacy event names (AI SDK v5 compat)
|
|
195
|
+
case "tool-call":
|
|
196
|
+
r.toolCallId && r.toolName && t.onToolCall(
|
|
197
|
+
r.toolCallId,
|
|
198
|
+
r.toolName,
|
|
199
|
+
r.input ?? {}
|
|
200
|
+
);
|
|
201
|
+
break;
|
|
202
|
+
case "tool-result":
|
|
203
|
+
r.toolCallId && t.onToolResult(r.toolCallId, r.output);
|
|
204
|
+
break;
|
|
205
|
+
case "error":
|
|
206
|
+
t.onError(r.errorText ?? r.reason ?? "Unknown error");
|
|
207
|
+
break;
|
|
208
|
+
case "abort":
|
|
209
|
+
t.onError(r.reason ?? "Response aborted");
|
|
210
|
+
break;
|
|
211
|
+
// Ignore: start, finish, text-start, text-end, start-step, finish-step,
|
|
212
|
+
// tool-input-start, tool-input-delta
|
|
213
|
+
default:
|
|
214
|
+
break;
|
|
215
|
+
}
|
|
216
|
+
} catch {
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
const M = {};
|
|
221
|
+
function F(e) {
|
|
222
|
+
Object.assign(M, e);
|
|
223
|
+
}
|
|
224
|
+
function nt(e) {
|
|
225
|
+
return M[e];
|
|
226
|
+
}
|
|
227
|
+
function st(e) {
|
|
228
|
+
return Object.hasOwn(M, e);
|
|
229
|
+
}
|
|
230
|
+
const ot = "og2-chat-", rt = "og2-enduser-session-", L = "og2-conv-";
|
|
231
|
+
function D(e) {
|
|
232
|
+
return `${ot}${e}`;
|
|
233
|
+
}
|
|
234
|
+
function q(e) {
|
|
235
|
+
return `${rt}${e}`;
|
|
236
|
+
}
|
|
237
|
+
function z() {
|
|
238
|
+
var t, s;
|
|
239
|
+
const e = (s = (t = globalThis.crypto) == null ? void 0 : t.randomUUID) == null ? void 0 : s.call(t);
|
|
240
|
+
return e || `${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
|
241
|
+
}
|
|
242
|
+
function R(e) {
|
|
243
|
+
const t = q(e);
|
|
244
|
+
try {
|
|
245
|
+
const s = localStorage.getItem(t);
|
|
246
|
+
if (s && s.trim()) return s;
|
|
247
|
+
const n = `sess_${z()}`;
|
|
248
|
+
return localStorage.setItem(t, n), n;
|
|
249
|
+
} catch {
|
|
250
|
+
return `sess_${z()}`;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
function it(e) {
|
|
254
|
+
try {
|
|
255
|
+
localStorage.removeItem(q(e));
|
|
256
|
+
} catch {
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
function at(e, t) {
|
|
260
|
+
try {
|
|
261
|
+
const s = t.map(({ isStreaming: n, ...o }) => o);
|
|
262
|
+
sessionStorage.setItem(D(e), JSON.stringify(s));
|
|
263
|
+
} catch {
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
function ct(e) {
|
|
267
|
+
try {
|
|
268
|
+
const t = sessionStorage.getItem(D(e));
|
|
269
|
+
return t ? JSON.parse(t) : [];
|
|
270
|
+
} catch {
|
|
271
|
+
return [];
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
function X(e) {
|
|
275
|
+
try {
|
|
276
|
+
sessionStorage.removeItem(D(e)), sessionStorage.removeItem(`${L}${e}`);
|
|
277
|
+
} catch {
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
function lt(e, t) {
|
|
281
|
+
try {
|
|
282
|
+
sessionStorage.setItem(`${L}${e}`, t);
|
|
283
|
+
} catch {
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
function ut(e) {
|
|
287
|
+
try {
|
|
288
|
+
return sessionStorage.getItem(`${L}${e}`);
|
|
289
|
+
} catch {
|
|
290
|
+
return null;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
const dt = 10, gt = 30, x = 500, ht = "og2-widget-root", ft = /* @__PURE__ */ new Set(["SCRIPT", "STYLE", "NOSCRIPT", "SVG", "NAV", "HEADER", "FOOTER"]), pt = "a[href], button, input, select, textarea";
|
|
294
|
+
function N(e) {
|
|
295
|
+
if (!(e instanceof HTMLElement)) return !1;
|
|
296
|
+
if (typeof e.checkVisibility == "function")
|
|
297
|
+
return e.checkVisibility({ checkOpacity: !1, checkVisibilityCSS: !0 });
|
|
298
|
+
let t = e;
|
|
299
|
+
for (; t; ) {
|
|
300
|
+
if (t.hidden || t.getAttribute("aria-hidden") === "true") return !1;
|
|
301
|
+
const s = getComputedStyle(t);
|
|
302
|
+
if (s.display === "none" || s.visibility === "hidden") return !1;
|
|
303
|
+
t = t.parentElement;
|
|
304
|
+
}
|
|
305
|
+
return !0;
|
|
306
|
+
}
|
|
307
|
+
function A(e) {
|
|
308
|
+
return !!e.closest(`#${ht}`);
|
|
309
|
+
}
|
|
310
|
+
function mt(e) {
|
|
311
|
+
var s, n;
|
|
312
|
+
if (e instanceof HTMLInputElement || e instanceof HTMLSelectElement || e instanceof HTMLTextAreaElement) {
|
|
313
|
+
if (e.id) {
|
|
314
|
+
const o = document.querySelector(`label[for="${CSS.escape(e.id)}"]`);
|
|
315
|
+
if ((s = o == null ? void 0 : o.textContent) != null && s.trim()) return o.textContent.trim();
|
|
316
|
+
}
|
|
317
|
+
return e.getAttribute("aria-label") || e.placeholder || e.getAttribute("name") || "";
|
|
318
|
+
}
|
|
319
|
+
const t = (n = e.innerText) == null ? void 0 : n.trim();
|
|
320
|
+
return t || e.getAttribute("aria-label") || e.getAttribute("title") || "";
|
|
321
|
+
}
|
|
322
|
+
function V(e, t) {
|
|
323
|
+
return e.length <= t ? e : e.slice(0, t - 3) + "...";
|
|
324
|
+
}
|
|
325
|
+
function St() {
|
|
326
|
+
var w;
|
|
327
|
+
const e = window.location.pathname + window.location.search, t = document.title || "", s = document.querySelectorAll("h1, h2, h3"), n = [];
|
|
328
|
+
for (const c of s) {
|
|
329
|
+
if (n.length >= dt) break;
|
|
330
|
+
if (A(c) || !N(c)) continue;
|
|
331
|
+
const g = (w = c.innerText) == null ? void 0 : w.trim();
|
|
332
|
+
g && !n.includes(g) && n.push(g);
|
|
333
|
+
}
|
|
334
|
+
const o = document.querySelectorAll(pt), r = [], i = /* @__PURE__ */ new Set();
|
|
335
|
+
for (const c of o) {
|
|
336
|
+
if (r.length >= gt) break;
|
|
337
|
+
if (A(c) || !N(c)) continue;
|
|
338
|
+
const g = mt(c);
|
|
339
|
+
if (!g) continue;
|
|
340
|
+
const E = `${c.tagName}:${g}`;
|
|
341
|
+
if (i.has(E)) continue;
|
|
342
|
+
i.add(E);
|
|
343
|
+
const m = {
|
|
344
|
+
tag: c.tagName.toLowerCase(),
|
|
345
|
+
text: V(g, 80)
|
|
346
|
+
};
|
|
347
|
+
if (c instanceof HTMLInputElement && (m.type = c.type), c instanceof HTMLAnchorElement && c.href)
|
|
348
|
+
try {
|
|
349
|
+
const C = new URL(c.href);
|
|
350
|
+
m.href = C.pathname + C.search;
|
|
351
|
+
} catch {
|
|
352
|
+
m.href = c.getAttribute("href") || void 0;
|
|
353
|
+
}
|
|
354
|
+
r.push(m);
|
|
355
|
+
}
|
|
356
|
+
const l = document.querySelector("main") || document.body;
|
|
357
|
+
let a = "";
|
|
358
|
+
function T(c) {
|
|
359
|
+
var E;
|
|
360
|
+
if (a.length >= x) return;
|
|
361
|
+
if (c.nodeType === Node.TEXT_NODE) {
|
|
362
|
+
const m = (E = c.textContent) == null ? void 0 : E.trim();
|
|
363
|
+
m && (a += (a ? " " : "") + m);
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
if (c.nodeType !== Node.ELEMENT_NODE) return;
|
|
367
|
+
const g = c;
|
|
368
|
+
if (!ft.has(g.tagName) && !A(g) && N(g)) {
|
|
369
|
+
for (const m of g.childNodes)
|
|
370
|
+
if (T(m), a.length >= x) return;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
return T(l), a = V(a.replace(/\s+/g, " ").trim(), x), { url: e, title: t, headings: n, interactiveElements: r, visibleText: a };
|
|
374
|
+
}
|
|
375
|
+
function yt(e, t) {
|
|
376
|
+
if (!t) return e;
|
|
377
|
+
let s = e;
|
|
378
|
+
for (const [n, o] of Object.entries(t)) {
|
|
379
|
+
const r = n.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
380
|
+
s = s.replace(new RegExp(`:${r}(?!\\w)`, "g"), encodeURIComponent(o));
|
|
381
|
+
}
|
|
382
|
+
return s;
|
|
383
|
+
}
|
|
384
|
+
function Et(e) {
|
|
385
|
+
var n;
|
|
386
|
+
const t = e.replace(/\/$/, "") || "/", s = document.querySelectorAll("a[href]");
|
|
387
|
+
for (const o of s) {
|
|
388
|
+
const r = o, i = ((n = r.pathname) == null ? void 0 : n.replace(/\/$/, "")) || "/";
|
|
389
|
+
if (r.origin === window.location.origin && i === t)
|
|
390
|
+
return r.click(), console.log(`[Ourguide] Navigated via anchor click: ${e}`), !0;
|
|
391
|
+
}
|
|
392
|
+
return !1;
|
|
393
|
+
}
|
|
394
|
+
function It(e) {
|
|
395
|
+
var o, r;
|
|
396
|
+
const t = window, s = t.next;
|
|
397
|
+
if ((o = s == null ? void 0 : s.router) != null && o.push)
|
|
398
|
+
return s.router.push(e), console.log(`[Ourguide] Navigated via Next.js router: ${e}`), !0;
|
|
399
|
+
if (t.__NUXT__) {
|
|
400
|
+
const i = t.$nuxt;
|
|
401
|
+
if ((r = i == null ? void 0 : i.$router) != null && r.push)
|
|
402
|
+
return i.$router.push(e), console.log(`[Ourguide] Navigated via Vue/Nuxt router: ${e}`), !0;
|
|
403
|
+
}
|
|
404
|
+
return !1;
|
|
405
|
+
}
|
|
406
|
+
function Tt(e) {
|
|
407
|
+
try {
|
|
408
|
+
return window.history.pushState({}, "", e), window.dispatchEvent(new PopStateEvent("popstate", { state: {} })), console.log(`[Ourguide] Navigated via pushState: ${e}`), !0;
|
|
409
|
+
} catch {
|
|
410
|
+
return !1;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
async function bt(e, t, s) {
|
|
414
|
+
const n = yt(e, t);
|
|
415
|
+
return s ? (s(n), console.log(`[Ourguide] Navigated via navigate prop: ${n}`), !0) : (Et(n) || It(n) || Tt(n) || (console.log(`[Ourguide] Navigated via hard navigation: ${n}`), window.location.href = n), !0);
|
|
416
|
+
}
|
|
417
|
+
class xt {
|
|
418
|
+
constructor(t) {
|
|
419
|
+
this.state = tt, this.conversationId = null, this.callbacks = {}, this.productId = t.productId, this.apiUrl = t.apiUrl, this.conversationId = ut(this.productId);
|
|
420
|
+
const s = ct(this.productId);
|
|
421
|
+
s.length > 0 && this.dispatch({ type: "LOAD_MESSAGES", messages: s }), F({
|
|
422
|
+
capture_screen: async () => St()
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
on(t) {
|
|
426
|
+
this.callbacks = { ...this.callbacks, ...t };
|
|
427
|
+
}
|
|
428
|
+
getState() {
|
|
429
|
+
return this.state;
|
|
430
|
+
}
|
|
431
|
+
getMessages() {
|
|
432
|
+
return this.state.messages;
|
|
433
|
+
}
|
|
434
|
+
dispatch(t) {
|
|
435
|
+
var s, n;
|
|
436
|
+
this.state = et(this.state, t), (n = (s = this.callbacks).onStateChange) == null || n.call(s, this.state);
|
|
437
|
+
}
|
|
438
|
+
open() {
|
|
439
|
+
this.dispatch({ type: "OPEN" });
|
|
440
|
+
}
|
|
441
|
+
close() {
|
|
442
|
+
this.dispatch({ type: "CLOSE" });
|
|
443
|
+
}
|
|
444
|
+
registerTools(t) {
|
|
445
|
+
F(t);
|
|
446
|
+
}
|
|
447
|
+
async loadConfig() {
|
|
448
|
+
var s, n;
|
|
449
|
+
const t = await Y(this.apiUrl, this.productId);
|
|
450
|
+
return (n = (s = this.callbacks).onConfigLoaded) == null || n.call(s, t), t;
|
|
451
|
+
}
|
|
452
|
+
async identify(t, s) {
|
|
453
|
+
const n = R(this.productId);
|
|
454
|
+
await Q(this.apiUrl, this.productId, n, t, s);
|
|
455
|
+
}
|
|
456
|
+
resetUser() {
|
|
457
|
+
const t = R(this.productId);
|
|
458
|
+
Z(this.apiUrl, this.productId, t).catch(() => {
|
|
459
|
+
}), it(this.productId), X(this.productId), this.conversationId = null, this.dispatch({ type: "FINISH_STREAMING" }), this.dispatch({ type: "CLEAR_MESSAGES" });
|
|
460
|
+
}
|
|
461
|
+
newConversation() {
|
|
462
|
+
const t = this.state.messages;
|
|
463
|
+
let s = null;
|
|
464
|
+
if (t.length > 0) {
|
|
465
|
+
const n = t.find((o) => o.role === "user");
|
|
466
|
+
s = {
|
|
467
|
+
title: n ? n.content.slice(0, 30) : "Conversation",
|
|
468
|
+
preview: t[t.length - 1].content.slice(0, 50),
|
|
469
|
+
messages: [...t]
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
return X(this.productId), this.conversationId = null, this.dispatch({ type: "CLEAR_MESSAGES" }), s;
|
|
473
|
+
}
|
|
474
|
+
async sendMessage(t, s, n) {
|
|
475
|
+
var E, m, C, P;
|
|
476
|
+
const o = t.trim(), r = s ? [...s] : [];
|
|
477
|
+
if (!o && r.length === 0 || this.state.isStreaming) return;
|
|
478
|
+
const i = r.length > 0 ? r.map((h) => `[${h.name}]`).join(" ") : "", l = [o, i].filter(Boolean).join(" ");
|
|
479
|
+
this.dispatch({ type: "ADD_USER_MESSAGE", id: j(), content: l });
|
|
480
|
+
let a = [];
|
|
481
|
+
if (r.length > 0)
|
|
482
|
+
try {
|
|
483
|
+
a = await Promise.all(r.map(async (h) => ({ name: h.name, type: h.type, data: await K(h) })));
|
|
484
|
+
} catch {
|
|
485
|
+
this.dispatch({ type: "SET_ERROR", error: "Failed to read attached files" });
|
|
486
|
+
return;
|
|
487
|
+
}
|
|
488
|
+
const T = [
|
|
489
|
+
...this.state.messages.map((h) => ({ role: h.role, content: h.content })),
|
|
490
|
+
{ role: "user", content: o || "Please review the attached file(s)." }
|
|
491
|
+
];
|
|
492
|
+
this.dispatch({ type: "START_STREAMING", id: j() });
|
|
493
|
+
const w = /* @__PURE__ */ new Map(), c = /* @__PURE__ */ new Map(), g = (h) => ({
|
|
494
|
+
onTextDelta: (d) => this.dispatch({ type: "APPEND_TEXT", text: d }),
|
|
495
|
+
onToolCall: (d, p, f, S) => {
|
|
496
|
+
var y, u;
|
|
497
|
+
const I = { id: d, name: p, args: f, status: "calling", providerMetadata: S };
|
|
498
|
+
w.set(d, I), this.dispatch({ type: "ADD_TOOL_CALL", toolCall: I }), (u = (y = this.callbacks).onToolCall) == null || u.call(y, I), h && st(p) && c.set(d, { name: p, args: f, providerMetadata: S });
|
|
499
|
+
},
|
|
500
|
+
onToolResult: (d, p) => {
|
|
501
|
+
var S, I;
|
|
502
|
+
this.dispatch({ type: "SET_TOOL_RESULT", toolCallId: d, result: p });
|
|
503
|
+
const f = w.get(d);
|
|
504
|
+
if (f && ((I = (S = this.callbacks).onToolResult) == null || I.call(S, f.name, p)), (f == null ? void 0 : f.name) === "navigate_to_page") {
|
|
505
|
+
const y = p;
|
|
506
|
+
y.route && (y.confidence ?? 0) >= 0.5 && bt(y.route, y.params, n);
|
|
507
|
+
}
|
|
508
|
+
},
|
|
509
|
+
onError: (d) => {
|
|
510
|
+
var p, f;
|
|
511
|
+
this.dispatch({ type: "SET_ERROR", error: d }), (f = (p = this.callbacks).onError) == null || f.call(p, d);
|
|
512
|
+
}
|
|
513
|
+
});
|
|
514
|
+
try {
|
|
515
|
+
const h = R(this.productId), { stream: d, conversationId: p } = await H(this.apiUrl, this.productId, T, h, this.conversationId || void 0, a);
|
|
516
|
+
p && !this.conversationId && (this.conversationId = p, lt(this.productId, p));
|
|
517
|
+
let f = !1;
|
|
518
|
+
if (await B(d, { ...g(!0), onError: (u) => {
|
|
519
|
+
var $, _;
|
|
520
|
+
f = !0, this.dispatch({ type: "SET_ERROR", error: u }), (_ = ($ = this.callbacks).onError) == null || _.call($, u);
|
|
521
|
+
}, onFinish: () => {
|
|
522
|
+
} }), f || c.size === 0) {
|
|
523
|
+
f || this.dispatch({ type: "FINISH_STREAMING" }), this.persistMessages();
|
|
524
|
+
return;
|
|
525
|
+
}
|
|
526
|
+
const S = [];
|
|
527
|
+
for (const [u, { name: $, args: _, providerMetadata: W }] of c) {
|
|
528
|
+
let v;
|
|
529
|
+
try {
|
|
530
|
+
v = await nt($)(_);
|
|
531
|
+
} catch (U) {
|
|
532
|
+
v = { status: "error", error: U instanceof Error ? U.message : "Handler failed" };
|
|
533
|
+
}
|
|
534
|
+
this.dispatch({ type: "SET_TOOL_RESULT", toolCallId: u, result: v }), (m = (E = this.callbacks).onToolResult) == null || m.call(E, $, v), S.push({ toolCallId: u, toolName: $, args: _, result: v, providerMetadata: W });
|
|
535
|
+
}
|
|
536
|
+
const I = [
|
|
537
|
+
...T,
|
|
538
|
+
{ role: "assistant", content: S.map((u) => ({ type: "tool-call", toolCallId: u.toolCallId, toolName: u.toolName, input: u.args, ...u.providerMetadata ? { providerOptions: u.providerMetadata } : {} })) },
|
|
539
|
+
{ role: "tool", content: S.map((u) => ({ type: "tool-result", toolCallId: u.toolCallId, toolName: u.toolName, output: { type: "json", value: u.result } })) }
|
|
540
|
+
], { stream: y } = await H(this.apiUrl, this.productId, I, void 0, this.conversationId || void 0);
|
|
541
|
+
await B(y, { ...g(!1), onFinish: () => this.dispatch({ type: "FINISH_STREAMING" }) });
|
|
542
|
+
} catch (h) {
|
|
543
|
+
this.dispatch({ type: "FINISH_STREAMING" });
|
|
544
|
+
const d = h instanceof Error ? h.message : "Something went wrong";
|
|
545
|
+
this.dispatch({ type: "SET_ERROR", error: d }), (P = (C = this.callbacks).onError) == null || P.call(C, d);
|
|
546
|
+
}
|
|
547
|
+
this.persistMessages();
|
|
548
|
+
}
|
|
549
|
+
persistMessages() {
|
|
550
|
+
!this.state.isStreaming && this.state.messages.length > 0 && at(this.productId, this.state.messages);
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
function k(e) {
|
|
554
|
+
return e.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
555
|
+
}
|
|
556
|
+
function b(e) {
|
|
557
|
+
let t = e;
|
|
558
|
+
return t = t.replace(/`([^`\n]+)`/g, '<code class="og2-md-inline-code">$1</code>'), t = t.replace(/\*\*(.+?)\*\*/g, "<strong>$1</strong>"), t = t.replace(/__(.+?)__/g, "<strong>$1</strong>"), t = t.replace(/\*(.+?)\*/g, "<em>$1</em>"), t = t.replace(new RegExp("(?<!\\w)_(.+?)_(?!\\w)", "g"), "<em>$1</em>"), t = t.replace(
|
|
559
|
+
/\[([^\]]+)\]\(([^)]+)\)/g,
|
|
560
|
+
(s, n, o) => {
|
|
561
|
+
const r = o.trim();
|
|
562
|
+
return /^(https?:\/\/|mailto:|\/|#)/i.test(r) ? `<a class="og2-md-link" href="${r}" target="_blank" rel="noopener noreferrer">${n}</a>` : n;
|
|
563
|
+
}
|
|
564
|
+
), t;
|
|
565
|
+
}
|
|
566
|
+
function Nt(e) {
|
|
567
|
+
if (!e) return "";
|
|
568
|
+
const t = [];
|
|
569
|
+
let s = e.replace(/```(\w*)\n([\s\S]*?)```/g, (n, o, r) => {
|
|
570
|
+
const i = t.length, l = k(r.replace(/\n$/, "")), a = o ? ` data-lang="${k(o)}"` : "";
|
|
571
|
+
return t.push(
|
|
572
|
+
`<pre class="og2-md-pre"${a}><code class="og2-md-code">${l}</code></pre>`
|
|
573
|
+
), `\0CB${i}\0`;
|
|
574
|
+
});
|
|
575
|
+
return s = k(s), s = Rt(s), s = s.replace(/\x00CB(\d+)\x00/g, (n, o) => t[parseInt(o)]), s;
|
|
576
|
+
}
|
|
577
|
+
function wt(e) {
|
|
578
|
+
return /^[\s]*[-*]\s/.test(e) ? "ul" : /^[\s]*\d+\.\s/.test(e) ? "ol" : /^#{1,4}\s+/.test(e) ? "heading" : /^\s*\|/.test(e) ? "table" : "text";
|
|
579
|
+
}
|
|
580
|
+
function $t(e) {
|
|
581
|
+
const t = [];
|
|
582
|
+
let s = null;
|
|
583
|
+
for (const n of e) {
|
|
584
|
+
const o = wt(n);
|
|
585
|
+
s && s.type === o ? s.lines.push(n) : (s && t.push(s), s = { type: o, lines: [n] });
|
|
586
|
+
}
|
|
587
|
+
return s && t.push(s), t;
|
|
588
|
+
}
|
|
589
|
+
function Ct(e) {
|
|
590
|
+
var t;
|
|
591
|
+
switch (e.type) {
|
|
592
|
+
case "ul":
|
|
593
|
+
return `<ul class="og2-md-list">${e.lines.map((n) => `<li>${b(n.replace(/^[\s]*[-*]\s/, ""))}</li>`).join("")}</ul>`;
|
|
594
|
+
case "ol": {
|
|
595
|
+
const s = e.lines.map((r) => `<li>${b(r.replace(/^[\s]*\d+\.\s/, ""))}</li>`).join(""), n = ((t = e.lines[0].match(/^[\s]*(\d+)\./)) == null ? void 0 : t[1]) ?? "1";
|
|
596
|
+
return `<ol class="og2-md-list"${n !== "1" ? ` start="${n}"` : ""}>${s}</ol>`;
|
|
597
|
+
}
|
|
598
|
+
case "heading":
|
|
599
|
+
return e.lines.map((s) => {
|
|
600
|
+
const n = s.match(/^(#{1,4})\s+(.+)$/), o = n[1].length;
|
|
601
|
+
return `<h${o + 2} class="og2-md-heading">${b(n[2])}</h${o + 2}>`;
|
|
602
|
+
}).join("");
|
|
603
|
+
case "table":
|
|
604
|
+
return Ot(e.lines);
|
|
605
|
+
case "text":
|
|
606
|
+
return `<p>${e.lines.map((s) => b(s)).join("<br>")}</p>`;
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
function O(e) {
|
|
610
|
+
return e.trim().replace(/^\|/, "").replace(/\|$/, "").split("|").map((n) => n.trim());
|
|
611
|
+
}
|
|
612
|
+
function _t(e) {
|
|
613
|
+
const t = e.trim(), s = t.startsWith(":"), n = t.endsWith(":");
|
|
614
|
+
return s && n ? "center" : n ? "right" : "left";
|
|
615
|
+
}
|
|
616
|
+
function vt(e) {
|
|
617
|
+
const t = O(e);
|
|
618
|
+
return t.length > 0 && t.every((s) => /^:?-{1,}:?$/.test(s.trim()));
|
|
619
|
+
}
|
|
620
|
+
function Ot(e) {
|
|
621
|
+
if (e.length < 2 || !vt(e[1]))
|
|
622
|
+
return `<p>${e.map((l) => b(l)).join("<br>")}</p>`;
|
|
623
|
+
const t = O(e[0]), s = O(e[1]).map(_t), n = e.slice(2), o = (l) => {
|
|
624
|
+
const a = s[l];
|
|
625
|
+
return a && a !== "left" ? ` style="text-align:${a}"` : "";
|
|
626
|
+
}, r = "<thead><tr>" + t.map((l, a) => `<th${o(a)}>${b(l)}</th>`).join("") + "</tr></thead>";
|
|
627
|
+
let i = "";
|
|
628
|
+
return n.length > 0 && (i = `<tbody>${n.map((a) => {
|
|
629
|
+
const T = O(a);
|
|
630
|
+
return "<tr>" + t.map((w, c) => `<td${o(c)}>${b(T[c] ?? "")}</td>`).join("") + "</tr>";
|
|
631
|
+
}).join("")}</tbody>`), `<table class="og2-md-table">${r}${i}</table>`;
|
|
632
|
+
}
|
|
633
|
+
function Rt(e) {
|
|
634
|
+
const t = e.split(/\n{2,}/), s = [];
|
|
635
|
+
for (const n of t) {
|
|
636
|
+
const o = n.trim();
|
|
637
|
+
if (!o) continue;
|
|
638
|
+
if (/^\x00CB\d+\x00$/.test(o)) {
|
|
639
|
+
s.push(o);
|
|
640
|
+
continue;
|
|
641
|
+
}
|
|
642
|
+
const r = o.split(`
|
|
643
|
+
`), i = $t(r);
|
|
644
|
+
for (const l of i)
|
|
645
|
+
s.push(Ct(l));
|
|
646
|
+
}
|
|
647
|
+
return s.join("");
|
|
648
|
+
}
|
|
649
|
+
const J = {
|
|
650
|
+
none: "none",
|
|
651
|
+
soft: "0 2px 8px rgba(0,0,0,0.08)",
|
|
652
|
+
medium: "0 4px 16px rgba(0,0,0,0.12)",
|
|
653
|
+
strong: "0 8px 32px rgba(0,0,0,0.18)",
|
|
654
|
+
"extra-strong": "0 16px 48px rgba(0,0,0,0.28)"
|
|
655
|
+
};
|
|
656
|
+
function At(e, t) {
|
|
657
|
+
if (!e || !t) return;
|
|
658
|
+
const s = (l, a) => {
|
|
659
|
+
a != null && a !== "" && e.style.setProperty(l, String(a));
|
|
660
|
+
}, { colors: n, typography: o, dimensions: r, shadow: i } = t;
|
|
661
|
+
n && (s("--og2-bg", n.background), s("--og2-border", n.border), s("--og2-text", n.text), s("--og2-messages-bg", n.messagesBackground), s("--og2-agent-bubble", n.agentBubble), s("--og2-agent-bubble-text", n.agentBubbleText), s("--og2-user-bubble", n.userBubble), s("--og2-user-bubble-text", n.userBubbleText), s("--og2-user-bubble-border", n.userBubbleBorder), s("--og2-send-bg", n.sendButtonBackground), s("--og2-send-icon", n.sendButtonIcon), s("--og2-send-inactive-bg", n.sendButtonInactiveBackground)), o && (s("--og2-font", o.fontFamily), s("--og2-font-weight", o.fontWeight), s("--og2-line-height", o.lineHeight), typeof o.fontSize == "number" && s("--og2-font-size", `${o.fontSize}px`), typeof o.headerSize == "number" && s("--og2-header-size", `${o.headerSize}px`), typeof o.letterSpacing == "number" && s("--og2-letter-spacing", `${o.letterSpacing}px`)), r && (r.width !== void 0 && s("--og2-width", `${r.width}px`), r.maxHeight !== void 0 && s("--og2-height", `${r.maxHeight}px`), r.borderRadius !== void 0 && s("--og2-radius", `${r.borderRadius}px`), r.padding !== void 0 && s("--og2-padding", `${r.padding}px`)), i && J[i] && s("--og2-shadow", J[i]);
|
|
662
|
+
}
|
|
663
|
+
function kt(e, t) {
|
|
664
|
+
if (!e || !t) return;
|
|
665
|
+
const s = (n, o) => {
|
|
666
|
+
o != null && o !== "" && e.style.setProperty(n, String(o));
|
|
667
|
+
};
|
|
668
|
+
t.background && s("--og2-bubble-bg", t.background), t.border && s("--og2-bubble-border", t.border), t.icon && s("--og2-bubble-icon", t.icon), t.buttonSize && s("--og2-bubble-size", `${t.buttonSize}px`), t.iconSize && s("--og2-bubble-icon-size", `${t.iconSize}px`), t.right !== void 0 && s("--og2-bubble-right", `${t.right}px`), t.bottom !== void 0 && s("--og2-bubble-bottom", `${t.bottom}px`);
|
|
669
|
+
}
|
|
670
|
+
export {
|
|
671
|
+
xt as OurguideClient,
|
|
672
|
+
At as applyAppearance,
|
|
673
|
+
kt as applyBubble,
|
|
674
|
+
St as captureDOM,
|
|
675
|
+
it as clearEndUserSessionId,
|
|
676
|
+
X as clearMessages,
|
|
677
|
+
Y as fetchConfig,
|
|
678
|
+
K as fileToBase64,
|
|
679
|
+
j as generateId,
|
|
680
|
+
nt as getHandler,
|
|
681
|
+
R as getOrCreateEndUserSessionId,
|
|
682
|
+
st as hasHandler,
|
|
683
|
+
Q as identifyEndUser,
|
|
684
|
+
tt as initialState,
|
|
685
|
+
ut as loadConversationId,
|
|
686
|
+
ct as loadMessages,
|
|
687
|
+
bt as navigateTo,
|
|
688
|
+
B as parseDataStream,
|
|
689
|
+
F as registerTools,
|
|
690
|
+
Nt as renderMarkdown,
|
|
691
|
+
Z as resetEndUser,
|
|
692
|
+
lt as saveConversationId,
|
|
693
|
+
at as saveMessages,
|
|
694
|
+
H as sendMessage,
|
|
695
|
+
et as widgetReducer
|
|
696
|
+
};
|
|
697
|
+
//# sourceMappingURL=index.js.map
|