dewee-webchat 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +144 -2
- package/astro/DeweeChat.astro +21 -0
- package/dist/chunk-D4JM2FOB.js +1271 -0
- package/dist/chunk-WD6V3TAX.js +10 -0
- package/dist/dewee-webchat.js +258 -0
- package/dist/index.d.ts +161 -0
- package/dist/index.js +17 -0
- package/dist/protocol-CS8_4zC0.d.ts +71 -0
- package/dist/react.d.ts +26 -0
- package/dist/react.js +44 -0
- package/dist/server.d.ts +125 -0
- package/dist/server.js +632 -0
- package/dist/vue.d.ts +254 -0
- package/dist/vue.js +66 -0
- package/package.json +97 -4
|
@@ -0,0 +1,1271 @@
|
|
|
1
|
+
import {
|
|
2
|
+
HEADER_GATEWAY_TOKEN,
|
|
3
|
+
HEADER_GATEWAY_URL
|
|
4
|
+
} from "./chunk-WD6V3TAX.js";
|
|
5
|
+
|
|
6
|
+
// src/widget/api-client.ts
|
|
7
|
+
var ApiError = class extends Error {
|
|
8
|
+
constructor(message, status, code) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.status = status;
|
|
11
|
+
this.code = code;
|
|
12
|
+
}
|
|
13
|
+
status;
|
|
14
|
+
code;
|
|
15
|
+
};
|
|
16
|
+
var store = {
|
|
17
|
+
get(k) {
|
|
18
|
+
try {
|
|
19
|
+
return localStorage.getItem(k);
|
|
20
|
+
} catch {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
set(k, v) {
|
|
25
|
+
try {
|
|
26
|
+
localStorage.setItem(k, v);
|
|
27
|
+
} catch {
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
del(k) {
|
|
31
|
+
try {
|
|
32
|
+
localStorage.removeItem(k);
|
|
33
|
+
} catch {
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
var ApiClient = class {
|
|
38
|
+
constructor(o) {
|
|
39
|
+
this.o = o;
|
|
40
|
+
}
|
|
41
|
+
o;
|
|
42
|
+
session = null;
|
|
43
|
+
config = null;
|
|
44
|
+
sessionPromise = null;
|
|
45
|
+
url(action, qs) {
|
|
46
|
+
const base = this.o.endpoint.replace(/\/+$/, "");
|
|
47
|
+
const q = qs ? "?" + new URLSearchParams(qs).toString() : "";
|
|
48
|
+
return `${base}/${action}${q}`;
|
|
49
|
+
}
|
|
50
|
+
get storageKey() {
|
|
51
|
+
return `dewee-webchat:session:${this.o.endpoint}`;
|
|
52
|
+
}
|
|
53
|
+
headers(auth = false) {
|
|
54
|
+
const h = { "content-type": "application/json" };
|
|
55
|
+
if (this.o.gatewayUrl && this.o.gatewayToken) {
|
|
56
|
+
h[HEADER_GATEWAY_URL] = this.o.gatewayUrl;
|
|
57
|
+
h[HEADER_GATEWAY_TOKEN] = this.o.gatewayToken;
|
|
58
|
+
}
|
|
59
|
+
if (auth && this.session) h.authorization = `Bearer ${this.session.token}`;
|
|
60
|
+
return h;
|
|
61
|
+
}
|
|
62
|
+
async parse(res) {
|
|
63
|
+
const body = await res.json().catch(() => ({}));
|
|
64
|
+
if (!res.ok) throw new ApiError(body?.error?.message || `HTTP ${res.status}`, res.status, body?.error?.code);
|
|
65
|
+
return body;
|
|
66
|
+
}
|
|
67
|
+
async getConfig(force = false) {
|
|
68
|
+
if (this.config && !force) return this.config;
|
|
69
|
+
this.config = await this.parse(await fetch(this.url("config"), { headers: this.headers() }));
|
|
70
|
+
return this.config;
|
|
71
|
+
}
|
|
72
|
+
/** Returns a valid visitor session, creating or refreshing it when needed. */
|
|
73
|
+
ensureSession() {
|
|
74
|
+
if (this.session && this.session.expiresAt - Date.now() > 864e5) return Promise.resolve(this.session);
|
|
75
|
+
if (!this.session) {
|
|
76
|
+
const saved = store.get(this.storageKey);
|
|
77
|
+
if (saved) {
|
|
78
|
+
try {
|
|
79
|
+
const s = JSON.parse(saved);
|
|
80
|
+
if (s.expiresAt - Date.now() > 864e5) return Promise.resolve(this.session = s);
|
|
81
|
+
this.session = s;
|
|
82
|
+
} catch {
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return this.sessionPromise ??= this.createSession().finally(() => this.sessionPromise = null);
|
|
87
|
+
}
|
|
88
|
+
async createSession() {
|
|
89
|
+
const cfg = await this.getConfig();
|
|
90
|
+
const body = {};
|
|
91
|
+
if (this.session?.token) body.token = this.session.token;
|
|
92
|
+
if (cfg.turnstileSiteKey) {
|
|
93
|
+
if (!this.o.turnstile) throw new ApiError("Verification is required but no Turnstile handler is set", 403, "turnstile_required");
|
|
94
|
+
body.turnstileToken = await this.o.turnstile(cfg.turnstileSiteKey);
|
|
95
|
+
}
|
|
96
|
+
const s = await this.parse(
|
|
97
|
+
await fetch(this.url("session"), { method: "POST", headers: this.headers(), body: JSON.stringify(body) })
|
|
98
|
+
);
|
|
99
|
+
this.session = s;
|
|
100
|
+
store.set(this.storageKey, JSON.stringify(s));
|
|
101
|
+
return s;
|
|
102
|
+
}
|
|
103
|
+
forgetSession() {
|
|
104
|
+
this.session = null;
|
|
105
|
+
store.del(this.storageKey);
|
|
106
|
+
}
|
|
107
|
+
async history(agent, conversationId) {
|
|
108
|
+
await this.ensureSession();
|
|
109
|
+
const res = await fetch(this.url("history", { agent, conversationId }), { headers: this.headers(true) });
|
|
110
|
+
return (await this.parse(res)).messages;
|
|
111
|
+
}
|
|
112
|
+
/** Sends a message and calls `onEvent` for every streamed event. Resolves when the run ends. */
|
|
113
|
+
async chat(input, onEvent, signal) {
|
|
114
|
+
await this.ensureSession();
|
|
115
|
+
let res = await fetch(this.url("chat"), { method: "POST", headers: this.headers(true), body: JSON.stringify(input), signal });
|
|
116
|
+
if (res.status === 401) {
|
|
117
|
+
this.forgetSession();
|
|
118
|
+
await this.ensureSession();
|
|
119
|
+
res = await fetch(this.url("chat"), { method: "POST", headers: this.headers(true), body: JSON.stringify(input), signal });
|
|
120
|
+
}
|
|
121
|
+
if (!res.ok || !res.body) await this.parse(res);
|
|
122
|
+
const reader = res.body.pipeThrough(new TextDecoderStream()).getReader();
|
|
123
|
+
let buf = "";
|
|
124
|
+
while (true) {
|
|
125
|
+
const { done, value } = await reader.read();
|
|
126
|
+
if (done) break;
|
|
127
|
+
buf += value;
|
|
128
|
+
let idx;
|
|
129
|
+
while ((idx = buf.indexOf("\n\n")) >= 0) {
|
|
130
|
+
const frame = buf.slice(0, idx);
|
|
131
|
+
buf = buf.slice(idx + 2);
|
|
132
|
+
const data = frame.split("\n").filter((l) => l.startsWith("data:")).map((l) => l.slice(5).trimStart()).join("\n");
|
|
133
|
+
if (!data) continue;
|
|
134
|
+
try {
|
|
135
|
+
onEvent(JSON.parse(data));
|
|
136
|
+
} catch {
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
// src/widget/i18n.ts
|
|
144
|
+
var en = {
|
|
145
|
+
title: "Assistant",
|
|
146
|
+
greeting: "Hi there \u{1F44B}",
|
|
147
|
+
greetingBody: "Ask me anything. I can look things up and use tools to help you.",
|
|
148
|
+
placeholder: "Type a message\u2026",
|
|
149
|
+
send: "Send",
|
|
150
|
+
stop: "Stop",
|
|
151
|
+
newChat: "New conversation",
|
|
152
|
+
close: "Close chat",
|
|
153
|
+
open: "Open chat",
|
|
154
|
+
thinking: "Thinking\u2026",
|
|
155
|
+
thoughtFor: (s) => `Thought for ${s}s`,
|
|
156
|
+
reasoning: "Reasoning",
|
|
157
|
+
toolRunning: "running",
|
|
158
|
+
toolDone: "done",
|
|
159
|
+
toolError: "error",
|
|
160
|
+
args: "Arguments",
|
|
161
|
+
result: "Result",
|
|
162
|
+
online: "Online",
|
|
163
|
+
poweredBy: "Powered by",
|
|
164
|
+
errorGeneric: "Something went wrong. Please try again.",
|
|
165
|
+
stopped: "Stopped."
|
|
166
|
+
};
|
|
167
|
+
var vi = {
|
|
168
|
+
title: "Tr\u1EE3 l\xFD",
|
|
169
|
+
greeting: "Xin ch\xE0o \u{1F44B}",
|
|
170
|
+
greetingBody: "B\u1EA1n c\u1EA7n h\u1ECFi g\xEC c\u1EE9 nh\u1EAFn nh\xE9. M\xECnh c\xF3 th\u1EC3 tra c\u1EE9u v\xE0 d\xF9ng c\xF4ng c\u1EE5 \u0111\u1EC3 h\u1ED7 tr\u1EE3 b\u1EA1n.",
|
|
171
|
+
placeholder: "Nh\u1EADp tin nh\u1EAFn\u2026",
|
|
172
|
+
send: "G\u1EEDi",
|
|
173
|
+
stop: "D\u1EEBng",
|
|
174
|
+
newChat: "Cu\u1ED9c tr\xF2 chuy\u1EC7n m\u1EDBi",
|
|
175
|
+
close: "\u0110\xF3ng khung chat",
|
|
176
|
+
open: "M\u1EDF khung chat",
|
|
177
|
+
thinking: "\u0110ang suy ngh\u0129\u2026",
|
|
178
|
+
thoughtFor: (s) => `\u0110\xE3 suy ngh\u0129 ${s} gi\xE2y`,
|
|
179
|
+
reasoning: "Suy lu\u1EADn",
|
|
180
|
+
toolRunning: "\u0111ang ch\u1EA1y",
|
|
181
|
+
toolDone: "xong",
|
|
182
|
+
toolError: "l\u1ED7i",
|
|
183
|
+
args: "Tham s\u1ED1",
|
|
184
|
+
result: "K\u1EBFt qu\u1EA3",
|
|
185
|
+
online: "\u0110ang tr\u1EF1c tuy\u1EBFn",
|
|
186
|
+
poweredBy: "V\u1EADn h\xE0nh b\u1EDFi",
|
|
187
|
+
errorGeneric: "C\xF3 l\u1ED7i x\u1EA3y ra. B\u1EA1n th\u1EED l\u1EA1i gi\xFAp m\xECnh nh\xE9.",
|
|
188
|
+
stopped: "\u0110\xE3 d\u1EEBng."
|
|
189
|
+
};
|
|
190
|
+
function pickStrings(locale) {
|
|
191
|
+
const l = (locale || (typeof document !== "undefined" ? document.documentElement.lang : "") || (typeof navigator !== "undefined" ? navigator.language : "") || "en").toLowerCase();
|
|
192
|
+
return l.startsWith("vi") ? vi : en;
|
|
193
|
+
}
|
|
194
|
+
var icons = {
|
|
195
|
+
chat: `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M21 12a8.5 8.5 0 0 1-12.4 7.6L3 21l1.4-5.1A8.5 8.5 0 1 1 21 12Z"/><path d="M8.5 11h.01M12 11h.01M15.5 11h.01"/></svg>`,
|
|
196
|
+
close: `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" aria-hidden="true"><path d="M18 6 6 18M6 6l12 12"/></svg>`,
|
|
197
|
+
down: `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m6 9 6 6 6-6"/></svg>`,
|
|
198
|
+
newChat: `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M12 20h9"/><path d="M16.4 3.6a2.1 2.1 0 1 1 3 3L7 19l-4 1 1-4Z"/></svg>`,
|
|
199
|
+
send: `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M12 19V5M5 12l7-7 7 7"/></svg>`,
|
|
200
|
+
stop: `<svg viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><rect x="6" y="6" width="12" height="12" rx="2.5"/></svg>`,
|
|
201
|
+
brain: `<svg class="ic" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M9.5 2A2.5 2.5 0 0 1 12 4.5v15a2.5 2.5 0 0 1-4.96.44 2.5 2.5 0 0 1-2.96-3.08 3 3 0 0 1-.34-5.58 2.5 2.5 0 0 1 1.32-4.24A2.5 2.5 0 0 1 9.5 2Z"/><path d="M14.5 2A2.5 2.5 0 0 0 12 4.5v15a2.5 2.5 0 0 0 4.96.44 2.5 2.5 0 0 0 2.96-3.08 3 3 0 0 0 .34-5.58 2.5 2.5 0 0 0-1.32-4.24A2.5 2.5 0 0 0 14.5 2Z"/></svg>`,
|
|
202
|
+
tool: `<svg class="ic" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M14.7 6.3a1 1 0 0 0 0 1.4l1.6 1.6a1 1 0 0 0 1.4 0l3.77-3.77a6 6 0 0 1-7.94 7.94l-6.91 6.91a2.12 2.12 0 0 1-3-3l6.91-6.91a6 6 0 0 1 7.94-7.94l-3.76 3.76Z"/></svg>`
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
// src/widget/markdown.ts
|
|
206
|
+
function el(doc, tag, cls) {
|
|
207
|
+
const e = doc.createElement(tag);
|
|
208
|
+
if (cls) e.className = cls;
|
|
209
|
+
return e;
|
|
210
|
+
}
|
|
211
|
+
function safeHref(url) {
|
|
212
|
+
const u = url.trim();
|
|
213
|
+
if (/^(https?:|mailto:|tel:)/i.test(u)) return u;
|
|
214
|
+
if (/^[/#?]/.test(u) && !u.startsWith("//")) return u;
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
var INLINE_SRC = /(`+)([\s\S]*?[^`])\1(?!`)|\*\*([^*]+?)\*\*|__([^_]+?)__|~~([^~]+?)~~|\*([^*\s][^*]*?)\*|(?<![\w])_([^_\s][^_]*?)_(?![\w])|\[([^\]]+)\]\(([^)\s]+)(?:\s+"[^"]*")?\)|(https?:\/\/[^\s<>()]+[^\s<>().,;:!?'"])/;
|
|
218
|
+
function renderInline(doc, text, parent) {
|
|
219
|
+
const re = new RegExp(INLINE_SRC.source, "g");
|
|
220
|
+
let last = 0;
|
|
221
|
+
for (let m; m = re.exec(text); ) {
|
|
222
|
+
if (m.index > last) parent.appendChild(doc.createTextNode(text.slice(last, m.index)));
|
|
223
|
+
last = m.index + m[0].length;
|
|
224
|
+
if (m[2] !== void 0) {
|
|
225
|
+
const c = el(doc, "code");
|
|
226
|
+
c.textContent = m[2];
|
|
227
|
+
parent.appendChild(c);
|
|
228
|
+
} else if (m[3] !== void 0 || m[4] !== void 0) {
|
|
229
|
+
const s = el(doc, "strong");
|
|
230
|
+
renderInline(doc, m[3] ?? m[4], s);
|
|
231
|
+
parent.appendChild(s);
|
|
232
|
+
} else if (m[5] !== void 0) {
|
|
233
|
+
const s = el(doc, "del");
|
|
234
|
+
renderInline(doc, m[5], s);
|
|
235
|
+
parent.appendChild(s);
|
|
236
|
+
} else if (m[6] !== void 0 || m[7] !== void 0) {
|
|
237
|
+
const s = el(doc, "em");
|
|
238
|
+
renderInline(doc, m[6] ?? m[7], s);
|
|
239
|
+
parent.appendChild(s);
|
|
240
|
+
} else if (m[8] !== void 0) {
|
|
241
|
+
const href = safeHref(m[9]);
|
|
242
|
+
if (href) {
|
|
243
|
+
const a = el(doc, "a");
|
|
244
|
+
a.href = href;
|
|
245
|
+
a.target = "_blank";
|
|
246
|
+
a.rel = "noopener noreferrer nofollow";
|
|
247
|
+
renderInline(doc, m[8], a);
|
|
248
|
+
parent.appendChild(a);
|
|
249
|
+
} else parent.appendChild(doc.createTextNode(m[8]));
|
|
250
|
+
} else if (m[10] !== void 0) {
|
|
251
|
+
const a = el(doc, "a");
|
|
252
|
+
a.href = m[10];
|
|
253
|
+
a.target = "_blank";
|
|
254
|
+
a.rel = "noopener noreferrer nofollow";
|
|
255
|
+
a.textContent = m[10];
|
|
256
|
+
parent.appendChild(a);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
if (last < text.length) parent.appendChild(doc.createTextNode(text.slice(last)));
|
|
260
|
+
}
|
|
261
|
+
var isTableSep = (l) => /^\s*\|?\s*:?-{2,}:?\s*(\|\s*:?-{2,}:?\s*)*\|?\s*$/.test(l);
|
|
262
|
+
var splitRow = (l) => l.trim().replace(/^\||\|$/g, "").split("|").map((c) => c.trim());
|
|
263
|
+
function renderMarkdown(doc, src) {
|
|
264
|
+
const frag = doc.createDocumentFragment();
|
|
265
|
+
const lines = src.replace(/\r\n?/g, "\n").split("\n");
|
|
266
|
+
let i = 0;
|
|
267
|
+
while (i < lines.length) {
|
|
268
|
+
const line = lines[i];
|
|
269
|
+
if (!line.trim()) {
|
|
270
|
+
i++;
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
const fence = line.match(/^\s*(```+|~~~+)\s*([\w+-]*)/);
|
|
274
|
+
if (fence) {
|
|
275
|
+
const body = [];
|
|
276
|
+
i++;
|
|
277
|
+
while (i < lines.length && !lines[i].trim().startsWith(fence[1])) body.push(lines[i++]);
|
|
278
|
+
i++;
|
|
279
|
+
const pre = el(doc, "pre");
|
|
280
|
+
const code = el(doc, "code");
|
|
281
|
+
if (fence[2]) code.dataset.lang = fence[2];
|
|
282
|
+
code.textContent = body.join("\n");
|
|
283
|
+
pre.appendChild(code);
|
|
284
|
+
frag.appendChild(pre);
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
const h = line.match(/^(#{1,6})\s+(.*)$/);
|
|
288
|
+
if (h) {
|
|
289
|
+
const tag = `h${Math.min(6, h[1].length + 2)}`;
|
|
290
|
+
const e = el(doc, tag);
|
|
291
|
+
renderInline(doc, h[2], e);
|
|
292
|
+
frag.appendChild(e);
|
|
293
|
+
i++;
|
|
294
|
+
continue;
|
|
295
|
+
}
|
|
296
|
+
if (/^\s*([-*_])(\s*\1){2,}\s*$/.test(line)) {
|
|
297
|
+
frag.appendChild(el(doc, "hr"));
|
|
298
|
+
i++;
|
|
299
|
+
continue;
|
|
300
|
+
}
|
|
301
|
+
if (line.includes("|") && i + 1 < lines.length && isTableSep(lines[i + 1])) {
|
|
302
|
+
const wrap = el(doc, "div", "table");
|
|
303
|
+
const table = el(doc, "table");
|
|
304
|
+
const thead = el(doc, "thead");
|
|
305
|
+
const tr = el(doc, "tr");
|
|
306
|
+
for (const c of splitRow(line)) {
|
|
307
|
+
const th = el(doc, "th");
|
|
308
|
+
renderInline(doc, c, th);
|
|
309
|
+
tr.appendChild(th);
|
|
310
|
+
}
|
|
311
|
+
thead.appendChild(tr);
|
|
312
|
+
table.appendChild(thead);
|
|
313
|
+
const tbody = el(doc, "tbody");
|
|
314
|
+
i += 2;
|
|
315
|
+
while (i < lines.length && lines[i].includes("|") && lines[i].trim()) {
|
|
316
|
+
const r = el(doc, "tr");
|
|
317
|
+
for (const c of splitRow(lines[i])) {
|
|
318
|
+
const td = el(doc, "td");
|
|
319
|
+
renderInline(doc, c, td);
|
|
320
|
+
r.appendChild(td);
|
|
321
|
+
}
|
|
322
|
+
tbody.appendChild(r);
|
|
323
|
+
i++;
|
|
324
|
+
}
|
|
325
|
+
table.appendChild(tbody);
|
|
326
|
+
wrap.appendChild(table);
|
|
327
|
+
frag.appendChild(wrap);
|
|
328
|
+
continue;
|
|
329
|
+
}
|
|
330
|
+
if (/^\s*>/.test(line)) {
|
|
331
|
+
const body = [];
|
|
332
|
+
while (i < lines.length && /^\s*>/.test(lines[i])) body.push(lines[i++].replace(/^\s*>\s?/, ""));
|
|
333
|
+
const q = el(doc, "blockquote");
|
|
334
|
+
q.appendChild(renderMarkdown(doc, body.join("\n")));
|
|
335
|
+
frag.appendChild(q);
|
|
336
|
+
continue;
|
|
337
|
+
}
|
|
338
|
+
const li = line.match(/^(\s*)([-*+]|\d+[.)])\s+(.*)$/);
|
|
339
|
+
if (li) {
|
|
340
|
+
const ordered = /\d/.test(li[2]);
|
|
341
|
+
const list = el(doc, ordered ? "ol" : "ul");
|
|
342
|
+
if (ordered && parseInt(li[2], 10) !== 1) list.setAttribute("start", String(parseInt(li[2], 10)));
|
|
343
|
+
const baseIndent = li[1].length;
|
|
344
|
+
while (i < lines.length) {
|
|
345
|
+
const m = lines[i].match(/^(\s*)([-*+]|\d+[.)])\s+(.*)$/);
|
|
346
|
+
if (!m || m[1].length < baseIndent || /\d/.test(m[2]) !== ordered) break;
|
|
347
|
+
if (m[1].length > baseIndent) {
|
|
348
|
+
const sub = [];
|
|
349
|
+
while (i < lines.length && (/^\s+/.test(lines[i]) && lines[i].match(/^(\s*)/)[1].length > baseIndent)) sub.push(lines[i++].slice(baseIndent + 2));
|
|
350
|
+
list.lastElementChild?.appendChild(renderMarkdown(doc, sub.join("\n")));
|
|
351
|
+
continue;
|
|
352
|
+
}
|
|
353
|
+
const item = el(doc, "li");
|
|
354
|
+
renderInline(doc, m[3], item);
|
|
355
|
+
list.appendChild(item);
|
|
356
|
+
i++;
|
|
357
|
+
}
|
|
358
|
+
frag.appendChild(list);
|
|
359
|
+
continue;
|
|
360
|
+
}
|
|
361
|
+
const para = [];
|
|
362
|
+
while (i < lines.length && lines[i].trim() && !/^\s*(```|~~~|#{1,6}\s|>|([-*+]|\d+[.)])\s)/.test(lines[i]) && !(lines[i].includes("|") && i + 1 < lines.length && isTableSep(lines[i + 1]))) para.push(lines[i++]);
|
|
363
|
+
if (!para.length) para.push(lines[i++]);
|
|
364
|
+
const p = el(doc, "p");
|
|
365
|
+
para.forEach((l, idx) => {
|
|
366
|
+
if (idx) p.appendChild(el(doc, "br"));
|
|
367
|
+
renderInline(doc, l, p);
|
|
368
|
+
});
|
|
369
|
+
frag.appendChild(p);
|
|
370
|
+
}
|
|
371
|
+
return frag;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// src/widget/styles.ts
|
|
375
|
+
var styles = (
|
|
376
|
+
/* css */
|
|
377
|
+
`
|
|
378
|
+
:host {
|
|
379
|
+
--dw-accent: #5446e8;
|
|
380
|
+
--dw-accent-contrast: #fff;
|
|
381
|
+
--dw-bg: #ffffff;
|
|
382
|
+
--dw-surface: #f5f4fb;
|
|
383
|
+
--dw-fg: #1b1f4b;
|
|
384
|
+
--dw-muted: #6b6f8f;
|
|
385
|
+
--dw-border: #e4e2f1;
|
|
386
|
+
--dw-radius: 18px;
|
|
387
|
+
--dw-font: "Be Vietnam Pro", ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
|
|
388
|
+
--dw-mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
|
|
389
|
+
--dw-width: 400px;
|
|
390
|
+
--dw-height: 640px;
|
|
391
|
+
--dw-z: 2147483000;
|
|
392
|
+
--dw-shadow: 0 24px 64px -16px rgba(27, 31, 75, .28), 0 4px 16px -4px rgba(27, 31, 75, .12);
|
|
393
|
+
all: initial;
|
|
394
|
+
font-family: var(--dw-font);
|
|
395
|
+
color: var(--dw-fg);
|
|
396
|
+
}
|
|
397
|
+
:host([data-theme="dark"]) {
|
|
398
|
+
--dw-bg: #14152b;
|
|
399
|
+
--dw-surface: #1e2040;
|
|
400
|
+
--dw-fg: #ecebfa;
|
|
401
|
+
--dw-muted: #a2a4c6;
|
|
402
|
+
--dw-border: #2d2f55;
|
|
403
|
+
--dw-shadow: 0 24px 64px -12px rgba(0, 0, 0, .6);
|
|
404
|
+
}
|
|
405
|
+
* { box-sizing: border-box; }
|
|
406
|
+
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; padding: 0; }
|
|
407
|
+
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
|
|
408
|
+
|
|
409
|
+
/* ---------- launcher ---------- */
|
|
410
|
+
.launcher {
|
|
411
|
+
position: fixed; bottom: 20px; right: 20px; z-index: var(--dw-z);
|
|
412
|
+
width: 58px; height: 58px; border-radius: 50%;
|
|
413
|
+
background: var(--dw-accent); color: var(--dw-accent-contrast);
|
|
414
|
+
display: grid; place-items: center;
|
|
415
|
+
box-shadow: 0 10px 30px -6px color-mix(in srgb, var(--dw-accent) 60%, transparent);
|
|
416
|
+
transition: transform .2s cubic-bezier(.3,1.4,.6,1), box-shadow .2s;
|
|
417
|
+
}
|
|
418
|
+
.launcher:hover { transform: translateY(-2px) scale(1.04); }
|
|
419
|
+
.launcher:focus-visible { outline: 3px solid color-mix(in srgb, var(--dw-accent) 45%, transparent); outline-offset: 3px; }
|
|
420
|
+
.launcher svg { width: 26px; height: 26px; transition: transform .25s, opacity .2s; }
|
|
421
|
+
.launcher img { width: 100%; height: 100%; border-radius: 50%; object-fit: cover; }
|
|
422
|
+
:host([data-position="bottom-left"]) .launcher { right: auto; left: 20px; }
|
|
423
|
+
.launcher .badge {
|
|
424
|
+
position: absolute; top: -2px; right: -2px; width: 14px; height: 14px; border-radius: 50%;
|
|
425
|
+
background: #ff5a7a; border: 2px solid var(--dw-bg); display: none;
|
|
426
|
+
}
|
|
427
|
+
:host([data-unread]) .launcher .badge { display: block; }
|
|
428
|
+
|
|
429
|
+
/* ---------- panel ---------- */
|
|
430
|
+
.panel {
|
|
431
|
+
position: fixed; bottom: 92px; right: 20px; z-index: var(--dw-z);
|
|
432
|
+
width: min(var(--dw-width), calc(100vw - 32px));
|
|
433
|
+
height: min(var(--dw-height), calc(100dvh - 120px));
|
|
434
|
+
background: var(--dw-bg); color: var(--dw-fg);
|
|
435
|
+
border: 1px solid var(--dw-border); border-radius: var(--dw-radius);
|
|
436
|
+
box-shadow: var(--dw-shadow);
|
|
437
|
+
display: flex; flex-direction: column; overflow: hidden;
|
|
438
|
+
transform-origin: bottom right;
|
|
439
|
+
transition: opacity .18s ease, transform .22s cubic-bezier(.2,.9,.3,1.1), visibility .2s;
|
|
440
|
+
}
|
|
441
|
+
:host([data-position="bottom-left"]) .panel { right: auto; left: 20px; transform-origin: bottom left; }
|
|
442
|
+
:host(:not([data-open])) .panel { opacity: 0; transform: translateY(12px) scale(.96); visibility: hidden; pointer-events: none; }
|
|
443
|
+
:host([data-mode="inline"]) .panel {
|
|
444
|
+
position: relative; inset: auto; width: 100%; height: 100%; min-height: 420px;
|
|
445
|
+
opacity: 1; transform: none; visibility: visible; pointer-events: auto; box-shadow: none;
|
|
446
|
+
}
|
|
447
|
+
:host([data-mode="inline"]) { display: block; height: var(--dw-height); }
|
|
448
|
+
:host([data-mode="inline"]) .launcher, :host([data-mode="inline"]) .close { display: none; }
|
|
449
|
+
@media (max-width: 520px) {
|
|
450
|
+
:host(:not([data-mode="inline"])) .panel { inset: 0; width: 100vw; height: 100dvh; border-radius: 0; border: 0; }
|
|
451
|
+
:host([data-open]:not([data-mode="inline"])) .launcher { display: none; }
|
|
452
|
+
:host(:not([data-mode="inline"])) .head { padding-top: max(14px, env(safe-area-inset-top)); }
|
|
453
|
+
:host(:not([data-mode="inline"])) .composer { padding-bottom: max(12px, env(safe-area-inset-bottom)); }
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
/* ---------- header ---------- */
|
|
457
|
+
.head {
|
|
458
|
+
display: flex; align-items: center; gap: 12px; padding: 14px 14px 14px 16px;
|
|
459
|
+
border-bottom: 1px solid var(--dw-border);
|
|
460
|
+
background: linear-gradient(180deg, color-mix(in srgb, var(--dw-accent) 7%, var(--dw-bg)), var(--dw-bg));
|
|
461
|
+
}
|
|
462
|
+
.avatar {
|
|
463
|
+
flex: none; width: 38px; height: 38px; border-radius: 12px;
|
|
464
|
+
background: var(--dw-accent); color: var(--dw-accent-contrast);
|
|
465
|
+
display: grid; place-items: center; font-weight: 700; font-size: 16px; overflow: hidden;
|
|
466
|
+
}
|
|
467
|
+
.avatar img { width: 100%; height: 100%; object-fit: cover; }
|
|
468
|
+
.who { flex: 1; min-width: 0; }
|
|
469
|
+
.title { font-weight: 650; font-size: 15px; line-height: 1.25; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
|
|
470
|
+
.subtitle { font-size: 12px; color: var(--dw-muted); display: flex; align-items: center; gap: 6px; margin-top: 2px; }
|
|
471
|
+
.dot { flex: none; width: 7px; height: 7px; border-radius: 50%; background: #22c58b; box-shadow: 0 0 0 3px color-mix(in srgb, #22c58b 22%, transparent); }
|
|
472
|
+
.agent-select {
|
|
473
|
+
font: inherit; font-size: 12px; color: var(--dw-muted); background: transparent; border: 0;
|
|
474
|
+
padding: 0 14px 0 0; margin: 0; max-width: 170px; cursor: pointer; appearance: none; field-sizing: content; text-overflow: ellipsis;
|
|
475
|
+
background-image: linear-gradient(45deg, transparent 50%, currentColor 50%), linear-gradient(135deg, currentColor 50%, transparent 50%);
|
|
476
|
+
background-position: right 4px top 55%, right 0 top 55%; background-size: 4px 4px; background-repeat: no-repeat;
|
|
477
|
+
}
|
|
478
|
+
.agent-select:focus-visible { outline: 2px solid var(--dw-accent); border-radius: 4px; }
|
|
479
|
+
.icon-btn {
|
|
480
|
+
width: 34px; height: 34px; border-radius: 10px; display: grid; place-items: center; color: var(--dw-muted);
|
|
481
|
+
transition: background .15s, color .15s;
|
|
482
|
+
}
|
|
483
|
+
.icon-btn:hover { background: var(--dw-surface); color: var(--dw-fg); }
|
|
484
|
+
.icon-btn:focus-visible { outline: 2px solid var(--dw-accent); }
|
|
485
|
+
.icon-btn svg { width: 18px; height: 18px; }
|
|
486
|
+
|
|
487
|
+
/* ---------- log ---------- */
|
|
488
|
+
.log { flex: 1; overflow-y: auto; padding: 18px 16px 8px; scroll-behavior: smooth; overscroll-behavior: contain; }
|
|
489
|
+
.log::-webkit-scrollbar { width: 8px; }
|
|
490
|
+
.log::-webkit-scrollbar-thumb { background: var(--dw-border); border-radius: 8px; }
|
|
491
|
+
.welcome { text-align: left; padding: 6px 2px 10px; animation: rise .35s ease both; }
|
|
492
|
+
.welcome h2 { margin: 0 0 6px; font-size: 20px; line-height: 1.3; font-weight: 700; letter-spacing: -.01em; }
|
|
493
|
+
.welcome p { margin: 0; color: var(--dw-muted); font-size: 14px; line-height: 1.55; }
|
|
494
|
+
.suggestions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 16px; }
|
|
495
|
+
.chip {
|
|
496
|
+
font-size: 13px; line-height: 1.3; padding: 8px 12px; border-radius: 999px;
|
|
497
|
+
border: 1px solid var(--dw-border); background: var(--dw-bg); text-align: left;
|
|
498
|
+
transition: border-color .15s, background .15s, transform .15s;
|
|
499
|
+
}
|
|
500
|
+
.chip:hover { border-color: var(--dw-accent); background: color-mix(in srgb, var(--dw-accent) 6%, var(--dw-bg)); transform: translateY(-1px); }
|
|
501
|
+
|
|
502
|
+
.msg { display: flex; margin: 0 0 16px; animation: rise .25s ease both; }
|
|
503
|
+
.msg.user { justify-content: flex-end; }
|
|
504
|
+
.bubble { max-width: 86%; font-size: 14.5px; line-height: 1.6; overflow-wrap: anywhere; }
|
|
505
|
+
.msg.user .bubble {
|
|
506
|
+
background: var(--dw-accent); color: var(--dw-accent-contrast);
|
|
507
|
+
padding: 9px 14px; border-radius: 18px 18px 6px 18px; white-space: pre-wrap;
|
|
508
|
+
}
|
|
509
|
+
.msg.assistant .bubble { max-width: 100%; width: 100%; }
|
|
510
|
+
.msg.error .bubble {
|
|
511
|
+
color: #c2334f; background: color-mix(in srgb, #ff5a7a 9%, var(--dw-bg));
|
|
512
|
+
border: 1px solid color-mix(in srgb, #ff5a7a 30%, transparent); padding: 9px 12px; border-radius: 12px; font-size: 13.5px;
|
|
513
|
+
}
|
|
514
|
+
@keyframes rise { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
|
|
515
|
+
|
|
516
|
+
/* markdown */
|
|
517
|
+
.md > :first-child { margin-top: 0; }
|
|
518
|
+
.md > :last-child { margin-bottom: 0; }
|
|
519
|
+
.md p, .md ul, .md ol, .md blockquote, .md pre, .md .table { margin: 0 0 10px; }
|
|
520
|
+
.md ul, .md ol { padding-left: 22px; }
|
|
521
|
+
.md li { margin: 3px 0; }
|
|
522
|
+
.md h3, .md h4, .md h5, .md h6 { margin: 14px 0 6px; font-size: 15px; line-height: 1.35; }
|
|
523
|
+
.md a { color: var(--dw-accent); text-decoration: underline; text-underline-offset: 2px; }
|
|
524
|
+
.md code { font-family: var(--dw-mono); font-size: .86em; background: var(--dw-surface); padding: 1.5px 5px; border-radius: 5px; }
|
|
525
|
+
.md pre { background: #12132a; color: #e6e6f5; padding: 12px 14px; border-radius: 12px; overflow-x: auto; font-size: 12.5px; line-height: 1.55; }
|
|
526
|
+
.md pre code { background: none; padding: 0; color: inherit; font-size: inherit; }
|
|
527
|
+
.md blockquote { border-left: 3px solid var(--dw-accent); padding: 2px 0 2px 12px; color: var(--dw-muted); }
|
|
528
|
+
.md hr { border: 0; border-top: 1px solid var(--dw-border); margin: 14px 0; }
|
|
529
|
+
.md .table { overflow-x: auto; border: 1px solid var(--dw-border); border-radius: 10px; }
|
|
530
|
+
.md table { border-collapse: collapse; width: 100%; font-size: 13px; }
|
|
531
|
+
.md th, .md td { padding: 7px 10px; border-bottom: 1px solid var(--dw-border); text-align: left; vertical-align: top; overflow-wrap: break-word; min-width: 4.5em; }
|
|
532
|
+
.md th { background: var(--dw-surface); font-weight: 600; }
|
|
533
|
+
.md tr:last-child td { border-bottom: 0; }
|
|
534
|
+
.cursor::after { content: ""; display: inline-block; width: 7px; height: 15px; margin-left: 2px; vertical-align: -2px; background: var(--dw-accent); border-radius: 2px; animation: blink 1s steps(2) infinite; }
|
|
535
|
+
@keyframes blink { 50% { opacity: 0; } }
|
|
536
|
+
|
|
537
|
+
/* reasoning + tools */
|
|
538
|
+
.trace { display: flex; flex-direction: column; gap: 6px; margin-bottom: 8px; }
|
|
539
|
+
details.thinking, details.tool {
|
|
540
|
+
border: 1px solid var(--dw-border); border-radius: 12px; background: var(--dw-surface); overflow: hidden;
|
|
541
|
+
}
|
|
542
|
+
details > summary {
|
|
543
|
+
list-style: none; cursor: pointer; display: flex; align-items: center; gap: 8px;
|
|
544
|
+
padding: 7px 10px; font-size: 12.5px; color: var(--dw-muted); user-select: none;
|
|
545
|
+
}
|
|
546
|
+
details > summary::-webkit-details-marker { display: none; }
|
|
547
|
+
details > summary:hover { color: var(--dw-fg); }
|
|
548
|
+
details > summary .chev { margin-left: auto; transition: transform .2s; width: 14px; height: 14px; flex: none; }
|
|
549
|
+
details[open] > summary .chev { transform: rotate(180deg); }
|
|
550
|
+
details > summary svg.ic { width: 14px; height: 14px; flex: none; }
|
|
551
|
+
.thinking-body {
|
|
552
|
+
padding: 2px 12px 10px; font-size: 12.5px; line-height: 1.55; color: var(--dw-muted);
|
|
553
|
+
white-space: pre-wrap; max-height: 220px; overflow-y: auto;
|
|
554
|
+
}
|
|
555
|
+
.tool-name { font-family: var(--dw-mono); font-size: 12px; color: var(--dw-fg); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
556
|
+
.tool-state { font-size: 11px; padding: 1px 7px; border-radius: 999px; background: var(--dw-bg); border: 1px solid var(--dw-border); flex: none; }
|
|
557
|
+
.tool[data-status="done"] .tool-state { color: #13a06f; border-color: color-mix(in srgb, #13a06f 35%, transparent); }
|
|
558
|
+
.tool[data-status="error"] .tool-state { color: #c2334f; border-color: color-mix(in srgb, #c2334f 35%, transparent); }
|
|
559
|
+
.tool-body { padding: 0 10px 10px; display: grid; gap: 6px; }
|
|
560
|
+
.tool-body .label { font-size: 10.5px; text-transform: uppercase; letter-spacing: .06em; color: var(--dw-muted); }
|
|
561
|
+
.tool-body pre {
|
|
562
|
+
margin: 0; font-family: var(--dw-mono); font-size: 11.5px; line-height: 1.5; background: var(--dw-bg);
|
|
563
|
+
border: 1px solid var(--dw-border); border-radius: 8px; padding: 8px; max-height: 180px; overflow: auto; white-space: pre-wrap; overflow-wrap: anywhere;
|
|
564
|
+
}
|
|
565
|
+
.spin { width: 13px; height: 13px; border-radius: 50%; border: 2px solid color-mix(in srgb, var(--dw-accent) 25%, transparent); border-top-color: var(--dw-accent); animation: spin .7s linear infinite; flex: none; }
|
|
566
|
+
@keyframes spin { to { transform: rotate(360deg); } }
|
|
567
|
+
.typing { display: inline-flex; gap: 4px; padding: 10px 2px; }
|
|
568
|
+
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--dw-muted); opacity: .4; animation: bounce 1.2s infinite; }
|
|
569
|
+
.typing i:nth-child(2) { animation-delay: .15s; } .typing i:nth-child(3) { animation-delay: .3s; }
|
|
570
|
+
@keyframes bounce { 0%, 60%, 100% { transform: none; opacity: .35; } 30% { transform: translateY(-4px); opacity: 1; } }
|
|
571
|
+
|
|
572
|
+
/* ---------- composer ---------- */
|
|
573
|
+
.composer { padding: 10px 12px 12px; border-top: 1px solid var(--dw-border); background: var(--dw-bg); }
|
|
574
|
+
.box {
|
|
575
|
+
display: flex; align-items: flex-end; gap: 8px; border: 1px solid var(--dw-border); border-radius: 14px;
|
|
576
|
+
padding: 6px 6px 6px 12px; background: var(--dw-bg); transition: border-color .15s, box-shadow .15s;
|
|
577
|
+
}
|
|
578
|
+
.box:focus-within { border-color: var(--dw-accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--dw-accent) 16%, transparent); }
|
|
579
|
+
textarea {
|
|
580
|
+
flex: 1; resize: none; border: 0; outline: 0; background: transparent; color: var(--dw-fg);
|
|
581
|
+
font: inherit; font-size: 16px; line-height: 1.45; max-height: 140px; padding: 5px 0; min-height: 34px;
|
|
582
|
+
}
|
|
583
|
+
textarea::placeholder { color: var(--dw-muted); }
|
|
584
|
+
.send {
|
|
585
|
+
width: 36px; height: 36px; border-radius: 10px; flex: none; display: grid; place-items: center;
|
|
586
|
+
background: var(--dw-accent); color: var(--dw-accent-contrast); transition: opacity .15s, transform .15s;
|
|
587
|
+
}
|
|
588
|
+
.send:disabled { opacity: .35; cursor: default; }
|
|
589
|
+
.send:not(:disabled):hover { transform: translateY(-1px); }
|
|
590
|
+
.send svg { width: 17px; height: 17px; }
|
|
591
|
+
.foot { display: flex; justify-content: space-between; align-items: center; margin-top: 7px; font-size: 11px; color: var(--dw-muted); }
|
|
592
|
+
.foot a { color: inherit; text-decoration: none; font-weight: 600; }
|
|
593
|
+
.foot a:hover { color: var(--dw-accent); }
|
|
594
|
+
.challenge:empty { display: none; }
|
|
595
|
+
.challenge { display: flex; justify-content: center; padding: 8px 0; }
|
|
596
|
+
/* iOS zooms into any focused field under 16px; keep touch-device fields at 16px. */
|
|
597
|
+
@media (pointer: coarse) {
|
|
598
|
+
.agent-select { font-size: 16px; max-width: 200px; }
|
|
599
|
+
}
|
|
600
|
+
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation: none !important; transition: none !important; } }
|
|
601
|
+
`
|
|
602
|
+
);
|
|
603
|
+
|
|
604
|
+
// src/widget/element.ts
|
|
605
|
+
var BOOL_ATTRS = ["show-reasoning", "show-tool-calls", "allow-agent-switch", "open", "persist", "hide-branding", "launcher-avatar"];
|
|
606
|
+
var ATTRS = [
|
|
607
|
+
"endpoint",
|
|
608
|
+
"agent",
|
|
609
|
+
"title",
|
|
610
|
+
"subtitle",
|
|
611
|
+
"greeting",
|
|
612
|
+
"intro",
|
|
613
|
+
"placeholder",
|
|
614
|
+
"suggestions",
|
|
615
|
+
"avatar",
|
|
616
|
+
"position",
|
|
617
|
+
"mode",
|
|
618
|
+
"theme",
|
|
619
|
+
"accent-color",
|
|
620
|
+
"locale",
|
|
621
|
+
"gateway-url",
|
|
622
|
+
"gateway-token",
|
|
623
|
+
"width",
|
|
624
|
+
"height",
|
|
625
|
+
"z-index",
|
|
626
|
+
...BOOL_ATTRS
|
|
627
|
+
];
|
|
628
|
+
var camel = (s) => s.replace(/-([a-z])/g, (_, c) => c.toUpperCase());
|
|
629
|
+
var parseBool = (v) => v !== null && !/^(false|0|off|no)$/i.test(v);
|
|
630
|
+
var randomConversationId = () => {
|
|
631
|
+
const b = crypto.getRandomValues(new Uint8Array(9));
|
|
632
|
+
return Array.from(b, (x) => "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"[x & 63]).join("");
|
|
633
|
+
};
|
|
634
|
+
var prettyToolName = (n) => n.replace(/^mcp_[^_]+(?:_[^_]+)*?__/, "").replace(/_/g, " ");
|
|
635
|
+
var store2 = {
|
|
636
|
+
get(k) {
|
|
637
|
+
try {
|
|
638
|
+
return localStorage.getItem(k);
|
|
639
|
+
} catch {
|
|
640
|
+
return null;
|
|
641
|
+
}
|
|
642
|
+
},
|
|
643
|
+
set(k, v) {
|
|
644
|
+
try {
|
|
645
|
+
localStorage.setItem(k, v);
|
|
646
|
+
} catch {
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
};
|
|
650
|
+
var turnstileScript = null;
|
|
651
|
+
function loadTurnstile() {
|
|
652
|
+
return turnstileScript ??= new Promise((resolve, reject) => {
|
|
653
|
+
if (window.turnstile) return resolve();
|
|
654
|
+
const s = document.createElement("script");
|
|
655
|
+
s.src = "https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit";
|
|
656
|
+
s.async = true;
|
|
657
|
+
s.onload = () => resolve();
|
|
658
|
+
s.onerror = () => reject(new Error("Could not load Turnstile"));
|
|
659
|
+
document.head.appendChild(s);
|
|
660
|
+
});
|
|
661
|
+
}
|
|
662
|
+
var ElementBase = typeof HTMLElement === "undefined" ? class {
|
|
663
|
+
} : HTMLElement;
|
|
664
|
+
var DeweeChatElement = class extends ElementBase {
|
|
665
|
+
static get observedAttributes() {
|
|
666
|
+
return ATTRS;
|
|
667
|
+
}
|
|
668
|
+
root;
|
|
669
|
+
overrides = {};
|
|
670
|
+
api = null;
|
|
671
|
+
apiKey = "";
|
|
672
|
+
server = null;
|
|
673
|
+
agentId = "";
|
|
674
|
+
conversationId = "";
|
|
675
|
+
messages = [];
|
|
676
|
+
running = null;
|
|
677
|
+
activated = false;
|
|
678
|
+
strings = pickStrings();
|
|
679
|
+
mql = null;
|
|
680
|
+
els;
|
|
681
|
+
constructor() {
|
|
682
|
+
super();
|
|
683
|
+
this.root = this.attachShadow({ mode: "open" });
|
|
684
|
+
}
|
|
685
|
+
// ---------- public API ----------
|
|
686
|
+
/** Current effective configuration (attributes merged with `configure()` overrides). */
|
|
687
|
+
get config() {
|
|
688
|
+
const c = { endpoint: "/api/dewee" };
|
|
689
|
+
for (const a of ATTRS) {
|
|
690
|
+
const v = this.getAttribute(a);
|
|
691
|
+
if (v === null) continue;
|
|
692
|
+
if (BOOL_ATTRS.includes(a)) c[camel(a)] = parseBool(v);
|
|
693
|
+
else if (a === "suggestions") c.suggestions = v.trim().startsWith("[") ? safeJson(v, []) : v.split("|").map((s) => s.trim()).filter(Boolean);
|
|
694
|
+
else if (a === "z-index") c.zIndex = Number(v);
|
|
695
|
+
else c[camel(a)] = v;
|
|
696
|
+
}
|
|
697
|
+
return { ...c, ...this.overrides };
|
|
698
|
+
}
|
|
699
|
+
/** Updates configuration at runtime (wins over attributes). */
|
|
700
|
+
configure(partial) {
|
|
701
|
+
this.overrides = { ...this.overrides, ...partial };
|
|
702
|
+
if (this.els) this.applyConfig();
|
|
703
|
+
}
|
|
704
|
+
open() {
|
|
705
|
+
if (this.config.mode === "inline") return;
|
|
706
|
+
this.setAttribute("data-open", "");
|
|
707
|
+
this.removeAttribute("data-unread");
|
|
708
|
+
this.els.launcher.setAttribute("aria-expanded", "true");
|
|
709
|
+
this.activate();
|
|
710
|
+
setTimeout(() => this.els.input.focus(), 120);
|
|
711
|
+
this.emit("dewee:open");
|
|
712
|
+
}
|
|
713
|
+
close() {
|
|
714
|
+
if (this.config.mode === "inline") return;
|
|
715
|
+
this.removeAttribute("data-open");
|
|
716
|
+
this.els.launcher.setAttribute("aria-expanded", "false");
|
|
717
|
+
this.emit("dewee:close");
|
|
718
|
+
}
|
|
719
|
+
toggle() {
|
|
720
|
+
this.hasAttribute("data-open") ? this.close() : this.open();
|
|
721
|
+
}
|
|
722
|
+
/** Sends a message programmatically (opens the widget first). */
|
|
723
|
+
async send(text) {
|
|
724
|
+
const message = text.trim();
|
|
725
|
+
if (!message || this.running) return;
|
|
726
|
+
if (this.config.mode !== "inline" && !this.hasAttribute("data-open")) this.open();
|
|
727
|
+
await this.activate();
|
|
728
|
+
this.els.input.value = "";
|
|
729
|
+
this.autosize();
|
|
730
|
+
this.pushMessage({ role: "user", content: message });
|
|
731
|
+
const reply = { role: "assistant", content: "", status: "streaming", startedAt: Date.now() };
|
|
732
|
+
this.pushMessage(reply);
|
|
733
|
+
this.setRunning(new AbortController());
|
|
734
|
+
this.emit("dewee:send", { message, agent: this.agentId, conversationId: this.conversationId });
|
|
735
|
+
try {
|
|
736
|
+
await this.client().chat(
|
|
737
|
+
{ message, agent: this.agentId, conversationId: this.conversationId },
|
|
738
|
+
(e) => this.onStreamEvent(reply, e),
|
|
739
|
+
this.running.signal
|
|
740
|
+
);
|
|
741
|
+
if (reply.status === "streaming") reply.status = "done";
|
|
742
|
+
} catch (e) {
|
|
743
|
+
if (e.name === "AbortError") {
|
|
744
|
+
reply.status = "done";
|
|
745
|
+
if (!reply.content) reply.content = `_${this.strings.stopped}_`;
|
|
746
|
+
} else {
|
|
747
|
+
reply.status = "error";
|
|
748
|
+
this.showError(reply, e);
|
|
749
|
+
}
|
|
750
|
+
} finally {
|
|
751
|
+
this.finishThinking(reply);
|
|
752
|
+
this.updateMessage(reply);
|
|
753
|
+
this.setRunning(null);
|
|
754
|
+
if (reply.status === "done") {
|
|
755
|
+
this.emit("dewee:reply", { content: reply.content, agent: this.agentId, conversationId: this.conversationId });
|
|
756
|
+
if (this.config.mode !== "inline" && !this.hasAttribute("data-open")) this.setAttribute("data-unread", "");
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
/** Stops the running reply. */
|
|
761
|
+
stop() {
|
|
762
|
+
this.running?.abort();
|
|
763
|
+
}
|
|
764
|
+
/** Starts a fresh conversation with the current agent. */
|
|
765
|
+
newConversation() {
|
|
766
|
+
this.stop();
|
|
767
|
+
this.conversationId = randomConversationId();
|
|
768
|
+
if (this.config.persist !== false) store2.set(this.convKey(), this.conversationId);
|
|
769
|
+
this.messages = [];
|
|
770
|
+
this.renderLog();
|
|
771
|
+
this.els.input.focus();
|
|
772
|
+
}
|
|
773
|
+
// ---------- lifecycle ----------
|
|
774
|
+
connectedCallback() {
|
|
775
|
+
if (!this.els) this.renderShell();
|
|
776
|
+
this.applyConfig();
|
|
777
|
+
if (this.config.mode === "inline" || this.config.open) {
|
|
778
|
+
if (this.config.mode !== "inline") this.open();
|
|
779
|
+
else this.activate();
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
disconnectedCallback() {
|
|
783
|
+
this.stop();
|
|
784
|
+
this.mql?.removeEventListener("change", this.onScheme);
|
|
785
|
+
}
|
|
786
|
+
attributeChangedCallback() {
|
|
787
|
+
if (this.els) this.applyConfig();
|
|
788
|
+
}
|
|
789
|
+
// ---------- internals ----------
|
|
790
|
+
emit(name, detail) {
|
|
791
|
+
this.dispatchEvent(new CustomEvent(name, { detail, bubbles: true, composed: true }));
|
|
792
|
+
}
|
|
793
|
+
client() {
|
|
794
|
+
const c = this.config;
|
|
795
|
+
const key = [c.endpoint, c.gatewayUrl, c.gatewayToken].join("|");
|
|
796
|
+
if (!this.api || key !== this.apiKey) {
|
|
797
|
+
this.apiKey = key;
|
|
798
|
+
this.api = new ApiClient({
|
|
799
|
+
endpoint: c.endpoint,
|
|
800
|
+
gatewayUrl: c.gatewayUrl,
|
|
801
|
+
gatewayToken: c.gatewayToken,
|
|
802
|
+
turnstile: (siteKey) => this.challenge(siteKey)
|
|
803
|
+
});
|
|
804
|
+
}
|
|
805
|
+
return this.api;
|
|
806
|
+
}
|
|
807
|
+
convKey() {
|
|
808
|
+
return `dewee-webchat:conv:${this.config.endpoint}:${this.config.gatewayUrl || ""}:${this.agentId}`;
|
|
809
|
+
}
|
|
810
|
+
onScheme = () => this.applyTheme();
|
|
811
|
+
applyTheme() {
|
|
812
|
+
const t = this.config.theme || "light";
|
|
813
|
+
const dark = t === "dark" || t === "auto" && !!this.mql?.matches;
|
|
814
|
+
this.setAttribute("data-theme", dark ? "dark" : "light");
|
|
815
|
+
}
|
|
816
|
+
applyConfig() {
|
|
817
|
+
const c = this.config;
|
|
818
|
+
this.strings = pickStrings(c.locale);
|
|
819
|
+
const s = this.strings;
|
|
820
|
+
this.setAttribute("data-position", c.position || "bottom-right");
|
|
821
|
+
this.setAttribute("data-mode", c.mode || "floating");
|
|
822
|
+
if (c.theme === "auto" && !this.mql) {
|
|
823
|
+
this.mql = matchMedia("(prefers-color-scheme: dark)");
|
|
824
|
+
this.mql.addEventListener("change", this.onScheme);
|
|
825
|
+
}
|
|
826
|
+
this.applyTheme();
|
|
827
|
+
const setVar = (k, v) => v !== void 0 && v !== "" ? this.style.setProperty(k, String(v)) : this.style.removeProperty(k);
|
|
828
|
+
setVar("--dw-accent", c.accentColor);
|
|
829
|
+
setVar("--dw-width", c.width);
|
|
830
|
+
setVar("--dw-height", c.height);
|
|
831
|
+
setVar("--dw-z", c.zIndex);
|
|
832
|
+
const e = this.els;
|
|
833
|
+
e.title.textContent = c.title || s.title;
|
|
834
|
+
e.avatar.replaceChildren();
|
|
835
|
+
if (c.avatar) {
|
|
836
|
+
const img = document.createElement("img");
|
|
837
|
+
img.src = c.avatar;
|
|
838
|
+
img.alt = "";
|
|
839
|
+
e.avatar.appendChild(img);
|
|
840
|
+
} else e.avatar.textContent = (c.title || s.title).trim().charAt(0).toUpperCase();
|
|
841
|
+
e.launcher.innerHTML = c.avatar && c.launcherAvatar ? "" : icons.chat;
|
|
842
|
+
if (c.avatar && c.launcherAvatar) {
|
|
843
|
+
const img = document.createElement("img");
|
|
844
|
+
img.src = c.avatar;
|
|
845
|
+
img.alt = "";
|
|
846
|
+
e.launcher.appendChild(img);
|
|
847
|
+
}
|
|
848
|
+
e.launcher.insertAdjacentHTML("beforeend", `<span class="badge"></span>`);
|
|
849
|
+
e.launcher.setAttribute("aria-label", s.open);
|
|
850
|
+
e.close.setAttribute("aria-label", s.close);
|
|
851
|
+
e.close.title = s.close;
|
|
852
|
+
e.newChat.setAttribute("aria-label", s.newChat);
|
|
853
|
+
e.newChat.title = s.newChat;
|
|
854
|
+
e.input.placeholder = c.placeholder || s.placeholder;
|
|
855
|
+
e.foot.hidden = !!c.hideBranding;
|
|
856
|
+
e.foot.querySelector(".by").textContent = s.poweredBy + " ";
|
|
857
|
+
this.renderSubtitle();
|
|
858
|
+
this.updateSendButton();
|
|
859
|
+
if (this.activated && this.api && this.apiKey !== [c.endpoint, c.gatewayUrl, c.gatewayToken].join("|")) {
|
|
860
|
+
this.activated = false;
|
|
861
|
+
this.server = null;
|
|
862
|
+
this.activate();
|
|
863
|
+
} else if (this.activated && c.agent && c.agent !== this.agentId && this.server) {
|
|
864
|
+
this.switchAgent(c.agent);
|
|
865
|
+
} else if (this.activated) {
|
|
866
|
+
this.renderLog();
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
renderShell() {
|
|
870
|
+
this.root.innerHTML = `
|
|
871
|
+
<style>${styles}</style>
|
|
872
|
+
<button class="launcher" part="launcher" aria-expanded="false" aria-haspopup="dialog"></button>
|
|
873
|
+
<section class="panel" part="panel" role="dialog" aria-label="Chat">
|
|
874
|
+
<header class="head" part="header">
|
|
875
|
+
<div class="avatar"></div>
|
|
876
|
+
<div class="who">
|
|
877
|
+
<div class="title"></div>
|
|
878
|
+
<div class="subtitle"><span class="dot"></span><span class="status"></span><select class="agent-select" hidden></select></div>
|
|
879
|
+
</div>
|
|
880
|
+
<button class="icon-btn new">${icons.newChat}</button>
|
|
881
|
+
<button class="icon-btn close">${icons.close}</button>
|
|
882
|
+
</header>
|
|
883
|
+
<div class="log" part="log" role="log" aria-live="polite"></div>
|
|
884
|
+
<div class="challenge"><slot name="challenge"></slot></div>
|
|
885
|
+
<form class="composer" part="composer">
|
|
886
|
+
<div class="box">
|
|
887
|
+
<textarea rows="1" maxlength="4000"></textarea>
|
|
888
|
+
<button class="send" type="submit"></button>
|
|
889
|
+
</div>
|
|
890
|
+
<div class="foot"><span><span class="by"></span><a href="https://dewee.sh" target="_blank" rel="noopener">dewee</a></span><span class="meta"></span></div>
|
|
891
|
+
</form>
|
|
892
|
+
</section>`;
|
|
893
|
+
const $ = (sel) => this.root.querySelector(sel);
|
|
894
|
+
this.els = {
|
|
895
|
+
launcher: $(".launcher"),
|
|
896
|
+
panel: $(".panel"),
|
|
897
|
+
log: $(".log"),
|
|
898
|
+
title: $(".title"),
|
|
899
|
+
subtitle: $(".subtitle"),
|
|
900
|
+
status: $(".status"),
|
|
901
|
+
avatar: $(".avatar"),
|
|
902
|
+
select: $(".agent-select"),
|
|
903
|
+
input: $("textarea"),
|
|
904
|
+
send: $(".send"),
|
|
905
|
+
newChat: $(".new"),
|
|
906
|
+
close: $(".close"),
|
|
907
|
+
foot: $(".foot")
|
|
908
|
+
};
|
|
909
|
+
const e = this.els;
|
|
910
|
+
e.launcher.addEventListener("click", () => this.toggle());
|
|
911
|
+
e.close.addEventListener("click", () => this.close());
|
|
912
|
+
e.log.addEventListener("scroll", () => {
|
|
913
|
+
this.following = this.nearBottom();
|
|
914
|
+
}, { passive: true });
|
|
915
|
+
e.newChat.addEventListener("click", () => this.newConversation());
|
|
916
|
+
e.select.addEventListener("change", () => this.switchAgent(e.select.value));
|
|
917
|
+
$("form").addEventListener("submit", (ev) => {
|
|
918
|
+
ev.preventDefault();
|
|
919
|
+
this.running ? this.stop() : this.send(e.input.value);
|
|
920
|
+
});
|
|
921
|
+
e.input.addEventListener("keydown", (ev) => {
|
|
922
|
+
if (ev.key === "Enter" && !ev.shiftKey && !ev.isComposing && ev.keyCode !== 229) {
|
|
923
|
+
ev.preventDefault();
|
|
924
|
+
if (!this.running) this.send(e.input.value);
|
|
925
|
+
}
|
|
926
|
+
});
|
|
927
|
+
e.input.addEventListener("input", () => {
|
|
928
|
+
this.autosize();
|
|
929
|
+
this.updateSendButton();
|
|
930
|
+
});
|
|
931
|
+
e.panel.addEventListener("keydown", (ev) => {
|
|
932
|
+
if (ev.key === "Escape") this.close();
|
|
933
|
+
});
|
|
934
|
+
}
|
|
935
|
+
autosize() {
|
|
936
|
+
const t = this.els.input;
|
|
937
|
+
t.style.height = "auto";
|
|
938
|
+
t.style.height = Math.min(t.scrollHeight, 140) + "px";
|
|
939
|
+
}
|
|
940
|
+
updateSendButton() {
|
|
941
|
+
const b = this.els.send;
|
|
942
|
+
b.innerHTML = this.running ? icons.stop : icons.send;
|
|
943
|
+
b.setAttribute("aria-label", this.running ? this.strings.stop : this.strings.send);
|
|
944
|
+
b.title = b.getAttribute("aria-label");
|
|
945
|
+
b.disabled = !this.running && !this.els.input.value.trim();
|
|
946
|
+
}
|
|
947
|
+
setRunning(ctrl) {
|
|
948
|
+
this.running = ctrl;
|
|
949
|
+
this.updateSendButton();
|
|
950
|
+
}
|
|
951
|
+
renderSubtitle() {
|
|
952
|
+
const e = this.els;
|
|
953
|
+
const c = this.config;
|
|
954
|
+
const agents = this.server?.agents ?? [];
|
|
955
|
+
const canSwitch = c.allowAgentSwitch !== false && !!this.server?.allowAgentSwitch && agents.length > 1;
|
|
956
|
+
e.select.hidden = !canSwitch;
|
|
957
|
+
e.status.hidden = canSwitch;
|
|
958
|
+
if (canSwitch) {
|
|
959
|
+
e.select.replaceChildren(...agents.map((a) => new Option(a.name || a.id, a.id, false, a.id === this.agentId)));
|
|
960
|
+
e.select.setAttribute("aria-label", "Agent");
|
|
961
|
+
}
|
|
962
|
+
const current = agents.find((a) => a.id === this.agentId);
|
|
963
|
+
e.status.textContent = c.subtitle || (current?.name && current.name !== c.title ? `${this.strings.online} \xB7 ${current.name}` : this.strings.online);
|
|
964
|
+
}
|
|
965
|
+
/** Loads backend config and the persisted conversation once. */
|
|
966
|
+
async activate() {
|
|
967
|
+
if (this.activated) return;
|
|
968
|
+
this.activated = true;
|
|
969
|
+
this.renderLog();
|
|
970
|
+
try {
|
|
971
|
+
this.server = await this.client().getConfig(true);
|
|
972
|
+
this.els.input.maxLength = this.server.maxMessageLength;
|
|
973
|
+
const wanted = this.config.agent;
|
|
974
|
+
const known = (id) => !!id && (this.server.agents.length === 0 || this.server.agents.some((a) => a.id === id));
|
|
975
|
+
this.agentId = known(wanted) ? wanted : this.server.defaultAgent || this.server.agents[0]?.id || wanted || "";
|
|
976
|
+
await this.loadConversation();
|
|
977
|
+
} catch (e) {
|
|
978
|
+
this.activated = false;
|
|
979
|
+
this.renderLog();
|
|
980
|
+
this.showError(null, e);
|
|
981
|
+
}
|
|
982
|
+
this.renderSubtitle();
|
|
983
|
+
this.emit("dewee:ready", { agent: this.agentId, config: this.server });
|
|
984
|
+
}
|
|
985
|
+
async switchAgent(id) {
|
|
986
|
+
if (id === this.agentId) return;
|
|
987
|
+
this.stop();
|
|
988
|
+
this.agentId = id;
|
|
989
|
+
this.renderSubtitle();
|
|
990
|
+
await this.loadConversation();
|
|
991
|
+
}
|
|
992
|
+
async loadConversation() {
|
|
993
|
+
const persist = this.config.persist !== false;
|
|
994
|
+
const saved = persist ? store2.get(this.convKey()) : null;
|
|
995
|
+
this.conversationId = saved || randomConversationId();
|
|
996
|
+
if (persist && !saved) store2.set(this.convKey(), this.conversationId);
|
|
997
|
+
this.messages = [];
|
|
998
|
+
this.renderLog();
|
|
999
|
+
if (!saved) return;
|
|
1000
|
+
try {
|
|
1001
|
+
const history = await this.client().history(this.agentId, this.conversationId);
|
|
1002
|
+
this.messages = history.map((m) => ({ ...m, status: "done" }));
|
|
1003
|
+
this.renderLog();
|
|
1004
|
+
} catch {
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
renderLog() {
|
|
1008
|
+
const log = this.els.log;
|
|
1009
|
+
log.replaceChildren();
|
|
1010
|
+
if (!this.messages.length) log.appendChild(this.renderWelcome());
|
|
1011
|
+
for (const m of this.messages) log.appendChild(this.renderMessage(m));
|
|
1012
|
+
log.scrollTop = log.scrollHeight;
|
|
1013
|
+
}
|
|
1014
|
+
renderWelcome() {
|
|
1015
|
+
const c = this.config;
|
|
1016
|
+
const s = this.strings;
|
|
1017
|
+
const w = document.createElement("div");
|
|
1018
|
+
w.className = "welcome";
|
|
1019
|
+
const h = document.createElement("h2");
|
|
1020
|
+
h.textContent = c.greeting || s.greeting;
|
|
1021
|
+
const p = document.createElement("p");
|
|
1022
|
+
p.textContent = c.intro || s.greetingBody;
|
|
1023
|
+
w.append(h, p);
|
|
1024
|
+
if (c.suggestions?.length) {
|
|
1025
|
+
const list = document.createElement("div");
|
|
1026
|
+
list.className = "suggestions";
|
|
1027
|
+
for (const text of c.suggestions) {
|
|
1028
|
+
const b = document.createElement("button");
|
|
1029
|
+
b.className = "chip";
|
|
1030
|
+
b.type = "button";
|
|
1031
|
+
b.textContent = text;
|
|
1032
|
+
b.addEventListener("click", () => this.send(text));
|
|
1033
|
+
list.appendChild(b);
|
|
1034
|
+
}
|
|
1035
|
+
w.appendChild(list);
|
|
1036
|
+
}
|
|
1037
|
+
return w;
|
|
1038
|
+
}
|
|
1039
|
+
pushMessage(m) {
|
|
1040
|
+
if (!this.messages.length) this.els.log.replaceChildren();
|
|
1041
|
+
this.messages.push(m);
|
|
1042
|
+
this.els.log.appendChild(this.renderMessage(m));
|
|
1043
|
+
this.scrollToBottom(true);
|
|
1044
|
+
}
|
|
1045
|
+
renderMessage(m) {
|
|
1046
|
+
const root = document.createElement("div");
|
|
1047
|
+
root.className = `msg ${m.role}${m.status === "error" ? " error" : ""}`;
|
|
1048
|
+
const bubble = document.createElement("div");
|
|
1049
|
+
bubble.className = "bubble";
|
|
1050
|
+
root.appendChild(bubble);
|
|
1051
|
+
const trace = document.createElement("div");
|
|
1052
|
+
trace.className = "trace";
|
|
1053
|
+
const md = document.createElement("div");
|
|
1054
|
+
md.className = m.role === "assistant" ? "md" : "";
|
|
1055
|
+
if (m.role === "assistant") bubble.appendChild(trace);
|
|
1056
|
+
bubble.appendChild(md);
|
|
1057
|
+
m.view = { root, trace, md, tools: /* @__PURE__ */ new Map() };
|
|
1058
|
+
this.updateMessage(m);
|
|
1059
|
+
return root;
|
|
1060
|
+
}
|
|
1061
|
+
updateMessage(m) {
|
|
1062
|
+
const v = m.view;
|
|
1063
|
+
if (!v) return;
|
|
1064
|
+
const c = this.config;
|
|
1065
|
+
if (m.role === "user") {
|
|
1066
|
+
v.md.textContent = m.content;
|
|
1067
|
+
return;
|
|
1068
|
+
}
|
|
1069
|
+
v.root.classList.toggle("error", m.status === "error");
|
|
1070
|
+
if (c.showReasoning !== false && m.thinking) {
|
|
1071
|
+
if (!v.thinking) {
|
|
1072
|
+
v.thinking = document.createElement("details");
|
|
1073
|
+
v.thinking.className = "thinking";
|
|
1074
|
+
v.thinking.innerHTML = `<summary></summary><div class="thinking-body"></div>`;
|
|
1075
|
+
v.trace.prepend(v.thinking);
|
|
1076
|
+
}
|
|
1077
|
+
const streaming = m.status === "streaming" && !m.thinkingMs;
|
|
1078
|
+
const label = streaming ? this.strings.thinking : m.thinkingMs ? this.strings.thoughtFor(Math.max(1, Math.round(m.thinkingMs / 1e3))) : this.strings.reasoning;
|
|
1079
|
+
const summary = v.thinking.querySelector("summary");
|
|
1080
|
+
summary.innerHTML = `${streaming ? '<span class="spin"></span>' : icons.brain}<span></span>${icons.down.replace("<svg", '<svg class="chev"')}`;
|
|
1081
|
+
summary.querySelector("span:not(.spin)").textContent = label;
|
|
1082
|
+
v.thinking.querySelector(".thinking-body").textContent = m.thinking;
|
|
1083
|
+
} else if (v.thinking) {
|
|
1084
|
+
v.thinking.remove();
|
|
1085
|
+
v.thinking = void 0;
|
|
1086
|
+
}
|
|
1087
|
+
if (c.showToolCalls !== false) {
|
|
1088
|
+
for (const t of m.toolCalls ?? []) this.renderTool(v, t);
|
|
1089
|
+
} else {
|
|
1090
|
+
v.tools.forEach((d) => d.remove());
|
|
1091
|
+
v.tools.clear();
|
|
1092
|
+
}
|
|
1093
|
+
v.trace.hidden = !v.trace.childElementCount;
|
|
1094
|
+
const typing = m.status === "streaming" && !m.content;
|
|
1095
|
+
if (typing) {
|
|
1096
|
+
v.md.innerHTML = `<span class="typing" aria-label="${this.strings.thinking}"><i></i><i></i><i></i></span>`;
|
|
1097
|
+
} else {
|
|
1098
|
+
v.md.replaceChildren(renderMarkdown(document, m.content));
|
|
1099
|
+
v.md.classList.toggle("cursor", m.status === "streaming");
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
renderTool(v, t) {
|
|
1103
|
+
let d = v.tools.get(t.id);
|
|
1104
|
+
if (!d) {
|
|
1105
|
+
d = document.createElement("details");
|
|
1106
|
+
d.className = "tool";
|
|
1107
|
+
d.innerHTML = `<summary><span class="lead"></span><span class="tool-name"></span><span class="tool-state"></span>${icons.down.replace("<svg", '<svg class="chev"')}</summary><div class="tool-body"></div>`;
|
|
1108
|
+
v.tools.set(t.id, d);
|
|
1109
|
+
v.trace.appendChild(d);
|
|
1110
|
+
}
|
|
1111
|
+
d.dataset.status = t.status;
|
|
1112
|
+
d.querySelector(".lead").innerHTML = t.status === "running" ? '<span class="spin"></span>' : icons.tool;
|
|
1113
|
+
const name = d.querySelector(".tool-name");
|
|
1114
|
+
name.textContent = prettyToolName(t.name);
|
|
1115
|
+
name.setAttribute("title", t.name);
|
|
1116
|
+
d.querySelector(".tool-state").textContent = t.status === "running" ? this.strings.toolRunning : t.status === "error" ? this.strings.toolError : this.strings.toolDone;
|
|
1117
|
+
const body = d.querySelector(".tool-body");
|
|
1118
|
+
body.replaceChildren();
|
|
1119
|
+
const block = (label, value) => {
|
|
1120
|
+
if (value === void 0 || value === null || value === "") return;
|
|
1121
|
+
const l = document.createElement("div");
|
|
1122
|
+
l.className = "label";
|
|
1123
|
+
l.textContent = label;
|
|
1124
|
+
const pre = document.createElement("pre");
|
|
1125
|
+
pre.textContent = typeof value === "string" ? tryPretty(value) : JSON.stringify(value, null, 2);
|
|
1126
|
+
body.append(l, pre);
|
|
1127
|
+
};
|
|
1128
|
+
block(this.strings.args, t.arguments);
|
|
1129
|
+
block(this.strings.result, t.result);
|
|
1130
|
+
}
|
|
1131
|
+
onStreamEvent(m, e) {
|
|
1132
|
+
switch (e.type) {
|
|
1133
|
+
case "start":
|
|
1134
|
+
if (e.conversationId && e.conversationId !== this.conversationId) {
|
|
1135
|
+
this.conversationId = e.conversationId;
|
|
1136
|
+
if (this.config.persist !== false) store2.set(this.convKey(), e.conversationId);
|
|
1137
|
+
}
|
|
1138
|
+
break;
|
|
1139
|
+
case "thinking":
|
|
1140
|
+
m.thinking = (m.thinking || "") + e.text;
|
|
1141
|
+
break;
|
|
1142
|
+
case "delta":
|
|
1143
|
+
this.finishThinking(m);
|
|
1144
|
+
m.content += e.text;
|
|
1145
|
+
break;
|
|
1146
|
+
case "tool_call":
|
|
1147
|
+
this.finishThinking(m);
|
|
1148
|
+
(m.toolCalls ??= []).push({ id: e.id, name: e.name, arguments: e.arguments, status: "running" });
|
|
1149
|
+
break;
|
|
1150
|
+
case "tool_result": {
|
|
1151
|
+
const t = m.toolCalls?.find((x) => x.id === e.id) ?? (m.toolCalls ??= [])[m.toolCalls.push({ id: e.id, name: e.name, status: "running" }) - 1];
|
|
1152
|
+
t.status = e.isError ? "error" : "done";
|
|
1153
|
+
t.result = e.result;
|
|
1154
|
+
t.isError = e.isError;
|
|
1155
|
+
break;
|
|
1156
|
+
}
|
|
1157
|
+
case "done":
|
|
1158
|
+
this.finishThinking(m);
|
|
1159
|
+
if (e.content && e.content.length >= m.content.length) m.content = e.content;
|
|
1160
|
+
m.status = "done";
|
|
1161
|
+
break;
|
|
1162
|
+
case "error":
|
|
1163
|
+
m.status = "error";
|
|
1164
|
+
this.showError(m, new ApiError(e.message, 500, e.code));
|
|
1165
|
+
break;
|
|
1166
|
+
}
|
|
1167
|
+
this.scheduleUpdate(m);
|
|
1168
|
+
}
|
|
1169
|
+
pendingFrame = 0;
|
|
1170
|
+
following = true;
|
|
1171
|
+
scheduleUpdate(m) {
|
|
1172
|
+
if (this.pendingFrame) return;
|
|
1173
|
+
this.pendingFrame = requestAnimationFrame(() => {
|
|
1174
|
+
this.pendingFrame = 0;
|
|
1175
|
+
this.updateMessage(m);
|
|
1176
|
+
if (this.following) this.scrollToBottom(true);
|
|
1177
|
+
});
|
|
1178
|
+
}
|
|
1179
|
+
finishThinking(m) {
|
|
1180
|
+
if (m.thinking && !m.thinkingMs && m.startedAt) m.thinkingMs = Date.now() - m.startedAt;
|
|
1181
|
+
}
|
|
1182
|
+
nearBottom() {
|
|
1183
|
+
const l = this.els.log;
|
|
1184
|
+
return l.scrollHeight - l.scrollTop - l.clientHeight < 96;
|
|
1185
|
+
}
|
|
1186
|
+
scrollToBottom(force = false) {
|
|
1187
|
+
const l = this.els.log;
|
|
1188
|
+
if (force || this.nearBottom()) {
|
|
1189
|
+
l.scrollTop = l.scrollHeight;
|
|
1190
|
+
this.following = true;
|
|
1191
|
+
}
|
|
1192
|
+
}
|
|
1193
|
+
showError(m, err) {
|
|
1194
|
+
const text = err.message || this.strings.errorGeneric;
|
|
1195
|
+
this.emit("dewee:error", { message: text, code: err.code });
|
|
1196
|
+
if (m) {
|
|
1197
|
+
m.content = m.content ? `${m.content}
|
|
1198
|
+
|
|
1199
|
+
> \u26A0\uFE0F ${text}` : `\u26A0\uFE0F ${text}`;
|
|
1200
|
+
m.status = "error";
|
|
1201
|
+
this.updateMessage(m);
|
|
1202
|
+
return;
|
|
1203
|
+
}
|
|
1204
|
+
const div = document.createElement("div");
|
|
1205
|
+
div.className = "msg error";
|
|
1206
|
+
div.innerHTML = `<div class="bubble"></div>`;
|
|
1207
|
+
div.firstElementChild.textContent = `\u26A0\uFE0F ${text}`;
|
|
1208
|
+
this.els.log.appendChild(div);
|
|
1209
|
+
}
|
|
1210
|
+
/** Renders a Turnstile challenge in light DOM (slotted) and resolves with its token. */
|
|
1211
|
+
async challenge(siteKey) {
|
|
1212
|
+
await loadTurnstile();
|
|
1213
|
+
const holder = document.createElement("div");
|
|
1214
|
+
holder.slot = "challenge";
|
|
1215
|
+
this.appendChild(holder);
|
|
1216
|
+
try {
|
|
1217
|
+
return await new Promise((resolve, reject) => {
|
|
1218
|
+
window.turnstile.render(holder, {
|
|
1219
|
+
sitekey: siteKey,
|
|
1220
|
+
appearance: "interaction-only",
|
|
1221
|
+
theme: this.getAttribute("data-theme") === "dark" ? "dark" : "light",
|
|
1222
|
+
callback: resolve,
|
|
1223
|
+
"error-callback": () => reject(new ApiError("Verification failed", 403, "turnstile_failed"))
|
|
1224
|
+
});
|
|
1225
|
+
});
|
|
1226
|
+
} finally {
|
|
1227
|
+
holder.remove();
|
|
1228
|
+
}
|
|
1229
|
+
}
|
|
1230
|
+
};
|
|
1231
|
+
function safeJson(s, fallback) {
|
|
1232
|
+
try {
|
|
1233
|
+
return JSON.parse(s);
|
|
1234
|
+
} catch {
|
|
1235
|
+
return fallback;
|
|
1236
|
+
}
|
|
1237
|
+
}
|
|
1238
|
+
function tryPretty(s) {
|
|
1239
|
+
const t = s.trim();
|
|
1240
|
+
if (!/^[[{]/.test(t)) return s;
|
|
1241
|
+
try {
|
|
1242
|
+
return JSON.stringify(JSON.parse(t), null, 2);
|
|
1243
|
+
} catch {
|
|
1244
|
+
return s;
|
|
1245
|
+
}
|
|
1246
|
+
}
|
|
1247
|
+
|
|
1248
|
+
// src/index.ts
|
|
1249
|
+
function defineDeweeChat(tag = "dewee-chat") {
|
|
1250
|
+
if (typeof window === "undefined" || !window.customElements) return;
|
|
1251
|
+
if (!customElements.get(tag)) customElements.define(tag, class extends DeweeChatElement {
|
|
1252
|
+
});
|
|
1253
|
+
}
|
|
1254
|
+
function mount(config = {}, target) {
|
|
1255
|
+
defineDeweeChat();
|
|
1256
|
+
const el2 = document.createElement("dewee-chat");
|
|
1257
|
+
el2.configure(config);
|
|
1258
|
+
const parent = typeof target === "string" ? document.querySelector(target) : target;
|
|
1259
|
+
(parent ?? document.body).appendChild(el2);
|
|
1260
|
+
return el2;
|
|
1261
|
+
}
|
|
1262
|
+
defineDeweeChat();
|
|
1263
|
+
|
|
1264
|
+
export {
|
|
1265
|
+
ApiError,
|
|
1266
|
+
ApiClient,
|
|
1267
|
+
renderMarkdown,
|
|
1268
|
+
DeweeChatElement,
|
|
1269
|
+
defineDeweeChat,
|
|
1270
|
+
mount
|
|
1271
|
+
};
|