wecool-chat 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/CHANGELOG.md +18 -0
- package/LICENSE +21 -0
- package/README.md +112 -0
- package/dist/client.d.ts +42 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/components/ChatFooter.d.ts +9 -0
- package/dist/components/ChatFooter.d.ts.map +1 -0
- package/dist/components/ChatHeader.d.ts +7 -0
- package/dist/components/ChatHeader.d.ts.map +1 -0
- package/dist/components/ChatPanel.d.ts +14 -0
- package/dist/components/ChatPanel.d.ts.map +1 -0
- package/dist/components/ChatWidget.d.ts +3 -0
- package/dist/components/ChatWidget.d.ts.map +1 -0
- package/dist/components/FloatButton.d.ts +7 -0
- package/dist/components/FloatButton.d.ts.map +1 -0
- package/dist/components/MessageList.d.ts +8 -0
- package/dist/components/MessageList.d.ts.map +1 -0
- package/dist/hooks/tool-runner.d.ts +20 -0
- package/dist/hooks/tool-runner.d.ts.map +1 -0
- package/dist/hooks/useChatStream.d.ts +26 -0
- package/dist/hooks/useChatStream.d.ts.map +1 -0
- package/dist/index.css +1 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +728 -0
- package/dist/sse-parser.d.ts +7 -0
- package/dist/sse-parser.d.ts.map +1 -0
- package/dist/types.d.ts +131 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +73 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,728 @@
|
|
|
1
|
+
import V, { useState as b, useRef as O, useEffect as j, useCallback as g, forwardRef as P, createElement as I, useMemo as M } from "react";
|
|
2
|
+
import { createRoot as X } from "react-dom/client";
|
|
3
|
+
import { jsx as l, jsxs as w } from "react/jsx-runtime";
|
|
4
|
+
async function* Z(e) {
|
|
5
|
+
if (!e) return;
|
|
6
|
+
const o = e.getReader(), t = new TextDecoder();
|
|
7
|
+
let n = "";
|
|
8
|
+
try {
|
|
9
|
+
for (; ; ) {
|
|
10
|
+
const { done: d, value: u } = await o.read();
|
|
11
|
+
if (d) break;
|
|
12
|
+
n += t.decode(u, { stream: !0 });
|
|
13
|
+
const p = n.split(`
|
|
14
|
+
`);
|
|
15
|
+
n = p.pop() ?? "";
|
|
16
|
+
for (const a of p) {
|
|
17
|
+
const i = D(a);
|
|
18
|
+
i && (yield i);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
const r = n + t.decode(), s = D(r);
|
|
22
|
+
s && (yield s);
|
|
23
|
+
} finally {
|
|
24
|
+
o.releaseLock();
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
const D = (e) => {
|
|
28
|
+
if (!e.startsWith("data:")) return null;
|
|
29
|
+
const o = e.slice(5).trim();
|
|
30
|
+
return o ? JSON.parse(o) : null;
|
|
31
|
+
};
|
|
32
|
+
class q {
|
|
33
|
+
endpoint;
|
|
34
|
+
apiKey;
|
|
35
|
+
conversationId = null;
|
|
36
|
+
constructor(o, t) {
|
|
37
|
+
this.endpoint = o.replace(/\/$/, ""), this.apiKey = t;
|
|
38
|
+
}
|
|
39
|
+
getConversationId() {
|
|
40
|
+
return this.conversationId || (this.conversationId = crypto.randomUUID()), this.conversationId;
|
|
41
|
+
}
|
|
42
|
+
resetConversation() {
|
|
43
|
+
this.conversationId = null;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Send a chat message or tool results and receive SSE events.
|
|
47
|
+
*/
|
|
48
|
+
async *chat(o) {
|
|
49
|
+
const t = await fetch(`${this.endpoint}/api/chat`, {
|
|
50
|
+
method: "POST",
|
|
51
|
+
headers: this.buildHeaders(),
|
|
52
|
+
body: JSON.stringify(this.buildBody(o))
|
|
53
|
+
});
|
|
54
|
+
if (!t.ok) {
|
|
55
|
+
const n = await t.text();
|
|
56
|
+
throw new Error(`API error: ${t.status} - ${n}`);
|
|
57
|
+
}
|
|
58
|
+
for await (const n of Z(t.body))
|
|
59
|
+
n.type === "done" && (this.conversationId = n.conversationId), yield n;
|
|
60
|
+
}
|
|
61
|
+
buildHeaders() {
|
|
62
|
+
const o = { "Content-Type": "application/json" };
|
|
63
|
+
return this.apiKey && (o["X-API-Key"] = this.apiKey), o;
|
|
64
|
+
}
|
|
65
|
+
buildBody(o) {
|
|
66
|
+
return {
|
|
67
|
+
conversationId: this.getConversationId(),
|
|
68
|
+
message: o.message,
|
|
69
|
+
tools: o.tools,
|
|
70
|
+
history: o.history?.map((t) => ({
|
|
71
|
+
role: t.role,
|
|
72
|
+
content: t.content,
|
|
73
|
+
toolCall: t.toolCall,
|
|
74
|
+
toolResult: t.toolResult
|
|
75
|
+
})),
|
|
76
|
+
toolResults: o.toolResults
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
function R() {
|
|
81
|
+
return `msg-${crypto.randomUUID()}`;
|
|
82
|
+
}
|
|
83
|
+
function G(e) {
|
|
84
|
+
return {
|
|
85
|
+
id: R(),
|
|
86
|
+
role: "user",
|
|
87
|
+
content: e,
|
|
88
|
+
timestamp: Date.now()
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
function E(e) {
|
|
92
|
+
return {
|
|
93
|
+
id: R(),
|
|
94
|
+
role: "assistant",
|
|
95
|
+
content: e,
|
|
96
|
+
timestamp: Date.now()
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
function J(e, o, t) {
|
|
100
|
+
return {
|
|
101
|
+
id: R(),
|
|
102
|
+
role: "tool",
|
|
103
|
+
content: `Executed ${e}`,
|
|
104
|
+
timestamp: Date.now(),
|
|
105
|
+
toolCall: {
|
|
106
|
+
name: e,
|
|
107
|
+
arguments: o
|
|
108
|
+
},
|
|
109
|
+
toolResult: t
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
const Y = 3e4, Q = (e, o) => new Error(`Tool "${e}" timed out after ${o}ms`), ee = (e, o, t) => Promise.race([
|
|
113
|
+
Promise.resolve(e),
|
|
114
|
+
new Promise((n, r) => setTimeout(() => r(Q(t, o)), o))
|
|
115
|
+
]), te = async (e, o, t) => {
|
|
116
|
+
const n = o(e.name);
|
|
117
|
+
if (!n)
|
|
118
|
+
return { event: e, result: { error: `Tool not found: ${e.name}` } };
|
|
119
|
+
t.onToolExecute?.(e.name, e.input);
|
|
120
|
+
try {
|
|
121
|
+
const r = await ee(
|
|
122
|
+
n.handler(e.input),
|
|
123
|
+
t.timeoutMs ?? Y,
|
|
124
|
+
e.name
|
|
125
|
+
);
|
|
126
|
+
return t.onToolResult?.(e.name, r), { event: e, result: r };
|
|
127
|
+
} catch (r) {
|
|
128
|
+
const s = {
|
|
129
|
+
error: r instanceof Error ? r.message : "Tool execution failed"
|
|
130
|
+
};
|
|
131
|
+
return t.onToolResult?.(e.name, s), { event: e, result: s };
|
|
132
|
+
}
|
|
133
|
+
}, $ = 5, _ = "エラーが発生しました。しばらくしてからお試しください。";
|
|
134
|
+
function oe(e) {
|
|
135
|
+
const [o, t] = b([]), [n, r] = b(!1), [s, d] = b(null), u = O(o);
|
|
136
|
+
j(() => {
|
|
137
|
+
u.current = o;
|
|
138
|
+
}, [o]);
|
|
139
|
+
const { client: p, tools: a, toolDefinitions: i, toolTimeout: c, onToolExecute: f, onToolResult: x, onError: h } = e, k = g((m) => a.find((S) => S.name === m), [a]), y = g(
|
|
140
|
+
(m) => te(m, k, {
|
|
141
|
+
timeoutMs: c,
|
|
142
|
+
onToolExecute: f,
|
|
143
|
+
onToolResult: x
|
|
144
|
+
}),
|
|
145
|
+
[k, c, f, x]
|
|
146
|
+
), H = g(() => {
|
|
147
|
+
t([]), d(null);
|
|
148
|
+
}, []), F = g(
|
|
149
|
+
async (m) => {
|
|
150
|
+
const S = G(m);
|
|
151
|
+
t((C) => [...C, S]), r(!0), d(null);
|
|
152
|
+
try {
|
|
153
|
+
await re({
|
|
154
|
+
client: p,
|
|
155
|
+
toolDefinitions: i,
|
|
156
|
+
initialMessage: m,
|
|
157
|
+
initialHistory: [...u.current, S],
|
|
158
|
+
setMessages: t,
|
|
159
|
+
runTool: y
|
|
160
|
+
});
|
|
161
|
+
} catch (C) {
|
|
162
|
+
d(_), h?.(C instanceof Error ? C : new Error(_)), console.error("Chat error:", C);
|
|
163
|
+
} finally {
|
|
164
|
+
r(!1);
|
|
165
|
+
}
|
|
166
|
+
},
|
|
167
|
+
[p, i, y, h]
|
|
168
|
+
);
|
|
169
|
+
return { messages: o, isLoading: n, error: s, sendMessage: F, clear: H };
|
|
170
|
+
}
|
|
171
|
+
async function re({
|
|
172
|
+
client: e,
|
|
173
|
+
toolDefinitions: o,
|
|
174
|
+
initialMessage: t,
|
|
175
|
+
initialHistory: n,
|
|
176
|
+
setMessages: r,
|
|
177
|
+
runTool: s
|
|
178
|
+
}) {
|
|
179
|
+
let d = n, u = t, p;
|
|
180
|
+
for (let a = 0; a < $; a++) {
|
|
181
|
+
const i = e.chat({
|
|
182
|
+
message: u,
|
|
183
|
+
tools: o,
|
|
184
|
+
history: d,
|
|
185
|
+
toolResults: p
|
|
186
|
+
}), { toolUses: c, finalized: f } = await ne(i, r, (y) => {
|
|
187
|
+
d = [...d, E(y)];
|
|
188
|
+
});
|
|
189
|
+
if (f || c.length === 0) return;
|
|
190
|
+
const x = await Promise.all(c.map(s)), { toolMessages: h, toolResults: k } = se(x);
|
|
191
|
+
r((y) => [...y, ...h]), d = [...d, ...h], p = k, u = void 0;
|
|
192
|
+
}
|
|
193
|
+
throw new Error(`Exceeded ${$} tool-call iterations`);
|
|
194
|
+
}
|
|
195
|
+
async function ne(e, o, t) {
|
|
196
|
+
const n = [];
|
|
197
|
+
let r = null, s = "", d = !1;
|
|
198
|
+
const u = () => {
|
|
199
|
+
if (r) return r;
|
|
200
|
+
const a = E("");
|
|
201
|
+
return r = a.id, o((i) => [...i, a]), a.id;
|
|
202
|
+
}, p = (a, i) => {
|
|
203
|
+
o((c) => c.map((f) => f.id === a ? { ...f, content: i } : f));
|
|
204
|
+
};
|
|
205
|
+
for await (const a of e)
|
|
206
|
+
switch (a.type) {
|
|
207
|
+
case "tool_use":
|
|
208
|
+
n.push(a);
|
|
209
|
+
break;
|
|
210
|
+
case "text_delta": {
|
|
211
|
+
const i = u();
|
|
212
|
+
s += a.delta, p(i, s);
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
case "text": {
|
|
216
|
+
r ? p(r, a.content) : o((i) => [...i, E(a.content)]), t(a.content), d = !0;
|
|
217
|
+
break;
|
|
218
|
+
}
|
|
219
|
+
case "error":
|
|
220
|
+
throw new Error(a.message);
|
|
221
|
+
}
|
|
222
|
+
return { toolUses: n, finalized: d };
|
|
223
|
+
}
|
|
224
|
+
function se(e) {
|
|
225
|
+
const o = [], t = [];
|
|
226
|
+
for (const { event: n, result: r } of e)
|
|
227
|
+
o.push(J(n.name, n.input, r)), t.push({
|
|
228
|
+
toolCallId: n.toolCallId,
|
|
229
|
+
toolName: n.name,
|
|
230
|
+
toolInput: n.input,
|
|
231
|
+
result: r
|
|
232
|
+
});
|
|
233
|
+
return { toolMessages: o, toolResults: t };
|
|
234
|
+
}
|
|
235
|
+
const le = (e) => e.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase(), ae = (e) => e.replace(
|
|
236
|
+
/^([A-Z])|[\s-_]+(\w)/g,
|
|
237
|
+
(o, t, n) => n ? n.toUpperCase() : t.toLowerCase()
|
|
238
|
+
), z = (e) => {
|
|
239
|
+
const o = ae(e);
|
|
240
|
+
return o.charAt(0).toUpperCase() + o.slice(1);
|
|
241
|
+
}, K = (...e) => e.filter((o, t, n) => !!o && o.trim() !== "" && n.indexOf(o) === t).join(" ").trim(), ie = (e) => {
|
|
242
|
+
for (const o in e)
|
|
243
|
+
if (o.startsWith("aria-") || o === "role" || o === "title")
|
|
244
|
+
return !0;
|
|
245
|
+
};
|
|
246
|
+
var ce = {
|
|
247
|
+
xmlns: "http://www.w3.org/2000/svg",
|
|
248
|
+
width: 24,
|
|
249
|
+
height: 24,
|
|
250
|
+
viewBox: "0 0 24 24",
|
|
251
|
+
fill: "none",
|
|
252
|
+
stroke: "currentColor",
|
|
253
|
+
strokeWidth: 2,
|
|
254
|
+
strokeLinecap: "round",
|
|
255
|
+
strokeLinejoin: "round"
|
|
256
|
+
};
|
|
257
|
+
const de = P(
|
|
258
|
+
({
|
|
259
|
+
color: e = "currentColor",
|
|
260
|
+
size: o = 24,
|
|
261
|
+
strokeWidth: t = 2,
|
|
262
|
+
absoluteStrokeWidth: n,
|
|
263
|
+
className: r = "",
|
|
264
|
+
children: s,
|
|
265
|
+
iconNode: d,
|
|
266
|
+
...u
|
|
267
|
+
}, p) => I(
|
|
268
|
+
"svg",
|
|
269
|
+
{
|
|
270
|
+
ref: p,
|
|
271
|
+
...ce,
|
|
272
|
+
width: o,
|
|
273
|
+
height: o,
|
|
274
|
+
stroke: e,
|
|
275
|
+
strokeWidth: n ? Number(t) * 24 / Number(o) : t,
|
|
276
|
+
className: K("lucide", r),
|
|
277
|
+
...!s && !ie(u) && { "aria-hidden": "true" },
|
|
278
|
+
...u
|
|
279
|
+
},
|
|
280
|
+
[
|
|
281
|
+
...d.map(([a, i]) => I(a, i)),
|
|
282
|
+
...Array.isArray(s) ? s : [s]
|
|
283
|
+
]
|
|
284
|
+
)
|
|
285
|
+
);
|
|
286
|
+
const T = (e, o) => {
|
|
287
|
+
const t = P(
|
|
288
|
+
({ className: n, ...r }, s) => I(de, {
|
|
289
|
+
ref: s,
|
|
290
|
+
iconNode: o,
|
|
291
|
+
className: K(
|
|
292
|
+
`lucide-${le(z(e))}`,
|
|
293
|
+
`lucide-${e}`,
|
|
294
|
+
n
|
|
295
|
+
),
|
|
296
|
+
...r
|
|
297
|
+
})
|
|
298
|
+
);
|
|
299
|
+
return t.displayName = z(e), t;
|
|
300
|
+
};
|
|
301
|
+
const ue = [
|
|
302
|
+
["path", { d: "M7.9 20A9 9 0 1 0 4 16.1L2 22Z", key: "vv11sd" }]
|
|
303
|
+
], pe = T("message-circle", ue);
|
|
304
|
+
const fe = [
|
|
305
|
+
[
|
|
306
|
+
"path",
|
|
307
|
+
{
|
|
308
|
+
d: "M14.536 21.686a.5.5 0 0 0 .937-.024l6.5-19a.496.496 0 0 0-.635-.635l-19 6.5a.5.5 0 0 0-.024.937l7.93 3.18a2 2 0 0 1 1.112 1.11z",
|
|
309
|
+
key: "1ffxy3"
|
|
310
|
+
}
|
|
311
|
+
],
|
|
312
|
+
["path", { d: "m21.854 2.147-10.94 10.939", key: "12cjpa" }]
|
|
313
|
+
], he = T("send", fe);
|
|
314
|
+
const xe = [
|
|
315
|
+
["path", { d: "M3 6h18", key: "d0wm0j" }],
|
|
316
|
+
["path", { d: "M19 6v14c0 1-1 2-2 2H7c-1 0-2-1-2-2V6", key: "4alrt4" }],
|
|
317
|
+
["path", { d: "M8 6V4c0-1 1-2 2-2h4c1 0 2 1 2 2v2", key: "v07s0e" }],
|
|
318
|
+
["line", { x1: "10", x2: "10", y1: "11", y2: "17", key: "1uufr5" }],
|
|
319
|
+
["line", { x1: "14", x2: "14", y1: "11", y2: "17", key: "xtxkd" }]
|
|
320
|
+
], ye = T("trash-2", xe);
|
|
321
|
+
const me = [
|
|
322
|
+
["path", { d: "M18 6 6 18", key: "1bl5f8" }],
|
|
323
|
+
["path", { d: "m6 6 12 12", key: "d8bk6v" }]
|
|
324
|
+
], N = T("x", me), B = {
|
|
325
|
+
display: "flex",
|
|
326
|
+
alignItems: "center",
|
|
327
|
+
justifyContent: "flex-end",
|
|
328
|
+
height: "44px",
|
|
329
|
+
padding: "0 12px",
|
|
330
|
+
borderBottom: "1px solid #e4e4e7",
|
|
331
|
+
backgroundColor: "#ffffff"
|
|
332
|
+
}, ge = {
|
|
333
|
+
display: "flex",
|
|
334
|
+
alignItems: "center",
|
|
335
|
+
justifyContent: "center",
|
|
336
|
+
width: "28px",
|
|
337
|
+
height: "28px",
|
|
338
|
+
borderRadius: "6px",
|
|
339
|
+
border: "none",
|
|
340
|
+
backgroundColor: "transparent",
|
|
341
|
+
color: "#71717a",
|
|
342
|
+
cursor: "pointer",
|
|
343
|
+
transition: "all 150ms ease"
|
|
344
|
+
};
|
|
345
|
+
function be({ onClose: e, showClose: o = !1 }) {
|
|
346
|
+
return !o || !e ? /* @__PURE__ */ l("div", { style: { ...B, height: "8px", borderBottom: "none" } }) : /* @__PURE__ */ l("div", { style: B, children: /* @__PURE__ */ l(
|
|
347
|
+
"button",
|
|
348
|
+
{
|
|
349
|
+
onClick: e,
|
|
350
|
+
style: ge,
|
|
351
|
+
onMouseEnter: (t) => {
|
|
352
|
+
t.currentTarget.style.backgroundColor = "#f4f4f5", t.currentTarget.style.color = "#18181b";
|
|
353
|
+
},
|
|
354
|
+
onMouseLeave: (t) => {
|
|
355
|
+
t.currentTarget.style.backgroundColor = "transparent", t.currentTarget.style.color = "#71717a";
|
|
356
|
+
},
|
|
357
|
+
"aria-label": "Close",
|
|
358
|
+
children: /* @__PURE__ */ l(N, { size: 16 })
|
|
359
|
+
}
|
|
360
|
+
) });
|
|
361
|
+
}
|
|
362
|
+
const Ce = {
|
|
363
|
+
display: "flex",
|
|
364
|
+
alignItems: "center",
|
|
365
|
+
gap: "8px",
|
|
366
|
+
padding: "12px",
|
|
367
|
+
borderTop: "1px solid #e4e4e7",
|
|
368
|
+
backgroundColor: "#ffffff"
|
|
369
|
+
}, we = {
|
|
370
|
+
flex: 1,
|
|
371
|
+
height: "40px",
|
|
372
|
+
padding: "0 14px",
|
|
373
|
+
fontSize: "14px",
|
|
374
|
+
color: "#18181b",
|
|
375
|
+
backgroundColor: "#fafafa",
|
|
376
|
+
border: "1px solid #e4e4e7",
|
|
377
|
+
borderRadius: "8px",
|
|
378
|
+
outline: "none",
|
|
379
|
+
transition: "border-color 150ms ease"
|
|
380
|
+
}, U = {
|
|
381
|
+
display: "flex",
|
|
382
|
+
alignItems: "center",
|
|
383
|
+
justifyContent: "center",
|
|
384
|
+
width: "40px",
|
|
385
|
+
height: "40px",
|
|
386
|
+
borderRadius: "8px",
|
|
387
|
+
border: "none",
|
|
388
|
+
cursor: "pointer",
|
|
389
|
+
transition: "all 150ms ease"
|
|
390
|
+
}, ke = {
|
|
391
|
+
...U,
|
|
392
|
+
backgroundColor: "#18181b",
|
|
393
|
+
color: "#ffffff"
|
|
394
|
+
}, Se = {
|
|
395
|
+
...U,
|
|
396
|
+
backgroundColor: "transparent",
|
|
397
|
+
color: "#71717a"
|
|
398
|
+
};
|
|
399
|
+
function Te({ onSubmit: e, onClear: o, disabled: t, placeholder: n }) {
|
|
400
|
+
const [r, s] = b(""), [d, u] = b(!1), p = g(
|
|
401
|
+
(c) => {
|
|
402
|
+
c.preventDefault();
|
|
403
|
+
const f = r.trim();
|
|
404
|
+
f && !t && (e(f), s(""));
|
|
405
|
+
},
|
|
406
|
+
[r, t, e]
|
|
407
|
+
), a = g(
|
|
408
|
+
(c) => {
|
|
409
|
+
if (c.key === "Enter" && !c.shiftKey) {
|
|
410
|
+
c.preventDefault();
|
|
411
|
+
const f = r.trim();
|
|
412
|
+
f && !t && (e(f), s(""));
|
|
413
|
+
}
|
|
414
|
+
},
|
|
415
|
+
[r, t, e]
|
|
416
|
+
), i = !t && r.trim();
|
|
417
|
+
return /* @__PURE__ */ w("form", { onSubmit: p, style: Ce, children: [
|
|
418
|
+
/* @__PURE__ */ l(
|
|
419
|
+
"input",
|
|
420
|
+
{
|
|
421
|
+
type: "text",
|
|
422
|
+
value: r,
|
|
423
|
+
onChange: (c) => s(c.target.value),
|
|
424
|
+
onKeyDown: a,
|
|
425
|
+
onFocus: () => u(!0),
|
|
426
|
+
onBlur: () => u(!1),
|
|
427
|
+
placeholder: n || "メッセージを入力...",
|
|
428
|
+
disabled: t,
|
|
429
|
+
style: {
|
|
430
|
+
...we,
|
|
431
|
+
borderColor: d ? "#18181b" : "#e4e4e7",
|
|
432
|
+
opacity: t ? 0.5 : 1,
|
|
433
|
+
cursor: t ? "not-allowed" : "text"
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
),
|
|
437
|
+
/* @__PURE__ */ l(
|
|
438
|
+
"button",
|
|
439
|
+
{
|
|
440
|
+
type: "submit",
|
|
441
|
+
disabled: !i,
|
|
442
|
+
style: {
|
|
443
|
+
...ke,
|
|
444
|
+
opacity: i ? 1 : 0.3,
|
|
445
|
+
cursor: i ? "pointer" : "not-allowed"
|
|
446
|
+
},
|
|
447
|
+
"aria-label": "Send",
|
|
448
|
+
children: /* @__PURE__ */ l(he, { size: 18 })
|
|
449
|
+
}
|
|
450
|
+
),
|
|
451
|
+
/* @__PURE__ */ l(
|
|
452
|
+
"button",
|
|
453
|
+
{
|
|
454
|
+
type: "button",
|
|
455
|
+
onClick: o,
|
|
456
|
+
style: Se,
|
|
457
|
+
onMouseEnter: (c) => {
|
|
458
|
+
c.currentTarget.style.backgroundColor = "#f4f4f5", c.currentTarget.style.color = "#18181b";
|
|
459
|
+
},
|
|
460
|
+
onMouseLeave: (c) => {
|
|
461
|
+
c.currentTarget.style.backgroundColor = "transparent", c.currentTarget.style.color = "#71717a";
|
|
462
|
+
},
|
|
463
|
+
"aria-label": "Clear",
|
|
464
|
+
children: /* @__PURE__ */ l(ye, { size: 18 })
|
|
465
|
+
}
|
|
466
|
+
)
|
|
467
|
+
] });
|
|
468
|
+
}
|
|
469
|
+
const ve = {
|
|
470
|
+
flex: 1,
|
|
471
|
+
overflowY: "auto",
|
|
472
|
+
padding: "16px",
|
|
473
|
+
display: "flex",
|
|
474
|
+
flexDirection: "column",
|
|
475
|
+
gap: "12px",
|
|
476
|
+
backgroundColor: "#ffffff"
|
|
477
|
+
}, Ie = {
|
|
478
|
+
flex: 1,
|
|
479
|
+
display: "flex",
|
|
480
|
+
alignItems: "center",
|
|
481
|
+
justifyContent: "center",
|
|
482
|
+
color: "#a1a1aa",
|
|
483
|
+
fontSize: "14px"
|
|
484
|
+
}, A = {
|
|
485
|
+
display: "flex",
|
|
486
|
+
flexDirection: "column",
|
|
487
|
+
maxWidth: "85%"
|
|
488
|
+
}, Ee = {
|
|
489
|
+
padding: "10px 14px",
|
|
490
|
+
fontSize: "14px",
|
|
491
|
+
lineHeight: "1.5",
|
|
492
|
+
wordBreak: "break-word",
|
|
493
|
+
backgroundColor: "#18181b",
|
|
494
|
+
color: "#fafafa",
|
|
495
|
+
borderRadius: "16px 16px 4px 16px"
|
|
496
|
+
}, Re = {
|
|
497
|
+
padding: "10px 14px",
|
|
498
|
+
fontSize: "14px",
|
|
499
|
+
lineHeight: "1.5",
|
|
500
|
+
wordBreak: "break-word",
|
|
501
|
+
backgroundColor: "#f4f4f5",
|
|
502
|
+
color: "#18181b",
|
|
503
|
+
borderRadius: "16px 16px 16px 4px"
|
|
504
|
+
}, Me = {
|
|
505
|
+
display: "flex",
|
|
506
|
+
alignItems: "center",
|
|
507
|
+
gap: "6px",
|
|
508
|
+
padding: "10px 14px",
|
|
509
|
+
backgroundColor: "#f4f4f5",
|
|
510
|
+
borderRadius: "16px 16px 16px 4px"
|
|
511
|
+
}, v = {
|
|
512
|
+
width: "6px",
|
|
513
|
+
height: "6px",
|
|
514
|
+
backgroundColor: "#71717a",
|
|
515
|
+
borderRadius: "50%",
|
|
516
|
+
animation: "bounce 1.4s infinite ease-in-out"
|
|
517
|
+
};
|
|
518
|
+
function De({ messages: e, isLoading: o }) {
|
|
519
|
+
const t = O(null);
|
|
520
|
+
j(() => {
|
|
521
|
+
t.current?.scrollIntoView({ behavior: "smooth" });
|
|
522
|
+
}, [e, o]);
|
|
523
|
+
const n = e.filter(
|
|
524
|
+
(r) => r.role === "user" || r.role === "assistant"
|
|
525
|
+
);
|
|
526
|
+
return /* @__PURE__ */ w("div", { style: ve, children: [
|
|
527
|
+
/* @__PURE__ */ l("style", { children: `
|
|
528
|
+
@keyframes bounce {
|
|
529
|
+
0%, 80%, 100% { transform: scale(0); }
|
|
530
|
+
40% { transform: scale(1); }
|
|
531
|
+
}
|
|
532
|
+
` }),
|
|
533
|
+
n.length === 0 && !o && /* @__PURE__ */ l("div", { style: Ie, children: "メッセージを入力してください" }),
|
|
534
|
+
n.map((r) => /* @__PURE__ */ l(
|
|
535
|
+
"div",
|
|
536
|
+
{
|
|
537
|
+
style: {
|
|
538
|
+
...A,
|
|
539
|
+
alignSelf: r.role === "user" ? "flex-end" : "flex-start"
|
|
540
|
+
},
|
|
541
|
+
children: /* @__PURE__ */ l("div", { style: r.role === "user" ? Ee : Re, children: r.content })
|
|
542
|
+
},
|
|
543
|
+
r.id
|
|
544
|
+
)),
|
|
545
|
+
o && /* @__PURE__ */ l("div", { style: { ...A, alignSelf: "flex-start" }, children: /* @__PURE__ */ w("div", { style: Me, children: [
|
|
546
|
+
/* @__PURE__ */ l("span", { style: { ...v, animationDelay: "-0.32s" } }),
|
|
547
|
+
/* @__PURE__ */ l("span", { style: { ...v, animationDelay: "-0.16s" } }),
|
|
548
|
+
/* @__PURE__ */ l("span", { style: v })
|
|
549
|
+
] }) }),
|
|
550
|
+
/* @__PURE__ */ l("div", { ref: t })
|
|
551
|
+
] });
|
|
552
|
+
}
|
|
553
|
+
const $e = {
|
|
554
|
+
display: "flex",
|
|
555
|
+
flexDirection: "column",
|
|
556
|
+
height: "100%",
|
|
557
|
+
backgroundColor: "#ffffff",
|
|
558
|
+
border: "1px solid #e4e4e7",
|
|
559
|
+
borderRadius: "12px",
|
|
560
|
+
overflow: "hidden"
|
|
561
|
+
}, _e = {
|
|
562
|
+
margin: "0 12px 8px 12px",
|
|
563
|
+
padding: "10px 12px",
|
|
564
|
+
backgroundColor: "#fef2f2",
|
|
565
|
+
color: "#dc2626",
|
|
566
|
+
borderRadius: "8px",
|
|
567
|
+
fontSize: "13px"
|
|
568
|
+
};
|
|
569
|
+
function L({
|
|
570
|
+
messages: e,
|
|
571
|
+
isLoading: o,
|
|
572
|
+
error: t,
|
|
573
|
+
onSendMessage: n,
|
|
574
|
+
onClear: r,
|
|
575
|
+
onClose: s,
|
|
576
|
+
showClose: d = !1,
|
|
577
|
+
placeholder: u
|
|
578
|
+
}) {
|
|
579
|
+
return /* @__PURE__ */ w("div", { style: $e, children: [
|
|
580
|
+
/* @__PURE__ */ l(be, { onClose: s, showClose: d }),
|
|
581
|
+
/* @__PURE__ */ l(De, { messages: e, isLoading: o }),
|
|
582
|
+
t && /* @__PURE__ */ l("div", { style: _e, children: t }),
|
|
583
|
+
/* @__PURE__ */ l(
|
|
584
|
+
Te,
|
|
585
|
+
{
|
|
586
|
+
onSubmit: n,
|
|
587
|
+
onClear: r,
|
|
588
|
+
disabled: o,
|
|
589
|
+
placeholder: u
|
|
590
|
+
}
|
|
591
|
+
)
|
|
592
|
+
] });
|
|
593
|
+
}
|
|
594
|
+
const ze = {
|
|
595
|
+
display: "flex",
|
|
596
|
+
alignItems: "center",
|
|
597
|
+
justifyContent: "center",
|
|
598
|
+
width: "52px",
|
|
599
|
+
height: "52px",
|
|
600
|
+
borderRadius: "26px",
|
|
601
|
+
backgroundColor: "#18181b",
|
|
602
|
+
color: "#ffffff",
|
|
603
|
+
border: "none",
|
|
604
|
+
cursor: "pointer",
|
|
605
|
+
boxShadow: "0 4px 16px rgba(0, 0, 0, 0.16)",
|
|
606
|
+
transition: "transform 150ms ease, box-shadow 150ms ease"
|
|
607
|
+
};
|
|
608
|
+
function Be({ isOpen: e, onClick: o }) {
|
|
609
|
+
return /* @__PURE__ */ l(
|
|
610
|
+
"button",
|
|
611
|
+
{
|
|
612
|
+
onClick: o,
|
|
613
|
+
style: ze,
|
|
614
|
+
onMouseEnter: (t) => {
|
|
615
|
+
t.currentTarget.style.transform = "scale(1.05)", t.currentTarget.style.boxShadow = "0 6px 20px rgba(0, 0, 0, 0.2)";
|
|
616
|
+
},
|
|
617
|
+
onMouseLeave: (t) => {
|
|
618
|
+
t.currentTarget.style.transform = "scale(1)", t.currentTarget.style.boxShadow = "0 4px 16px rgba(0, 0, 0, 0.16)";
|
|
619
|
+
},
|
|
620
|
+
"aria-label": e ? "Close chat" : "Open chat",
|
|
621
|
+
children: e ? /* @__PURE__ */ l(N, { size: 22 }) : /* @__PURE__ */ l(pe, { size: 22 })
|
|
622
|
+
}
|
|
623
|
+
);
|
|
624
|
+
}
|
|
625
|
+
const Ae = (e) => ({
|
|
626
|
+
name: e.name,
|
|
627
|
+
description: e.description,
|
|
628
|
+
parameters: e.parameters
|
|
629
|
+
}), Le = {
|
|
630
|
+
"bottom-right": { position: "fixed", bottom: "24px", right: "24px", zIndex: 9999 },
|
|
631
|
+
"bottom-left": { position: "fixed", bottom: "24px", left: "24px", zIndex: 9999 },
|
|
632
|
+
"top-right": { position: "fixed", top: "24px", right: "24px", zIndex: 9999 },
|
|
633
|
+
"top-left": { position: "fixed", top: "24px", left: "24px", zIndex: 9999 }
|
|
634
|
+
}, Oe = {
|
|
635
|
+
"bottom-right": { position: "absolute", bottom: "64px", right: "0" },
|
|
636
|
+
"bottom-left": { position: "absolute", bottom: "64px", left: "0" },
|
|
637
|
+
"top-right": { position: "absolute", top: "64px", right: "0" },
|
|
638
|
+
"top-left": { position: "absolute", top: "64px", left: "0" }
|
|
639
|
+
}, je = {
|
|
640
|
+
width: "380px",
|
|
641
|
+
height: "520px",
|
|
642
|
+
boxShadow: "0 8px 32px rgba(0, 0, 0, 0.12)",
|
|
643
|
+
borderRadius: "12px",
|
|
644
|
+
overflow: "hidden"
|
|
645
|
+
};
|
|
646
|
+
function W({
|
|
647
|
+
config: e,
|
|
648
|
+
tools: o,
|
|
649
|
+
floating: t = !1,
|
|
650
|
+
defaultOpen: n = !1,
|
|
651
|
+
position: r = "bottom-right"
|
|
652
|
+
}) {
|
|
653
|
+
const [s, d] = b(n), u = o || e.tools || [], p = M(
|
|
654
|
+
() => new q(e.endpoint, e.apiKey),
|
|
655
|
+
[e.endpoint, e.apiKey]
|
|
656
|
+
), a = M(() => u.map(Ae), [u]), { messages: i, isLoading: c, error: f, sendMessage: x, clear: h } = oe({
|
|
657
|
+
client: p,
|
|
658
|
+
tools: u,
|
|
659
|
+
toolDefinitions: a,
|
|
660
|
+
toolTimeout: e.toolTimeout,
|
|
661
|
+
onToolExecute: e.onToolExecute,
|
|
662
|
+
onToolResult: e.onToolResult,
|
|
663
|
+
onError: e.onError
|
|
664
|
+
});
|
|
665
|
+
return t ? /* @__PURE__ */ w("div", { style: Le[r], children: [
|
|
666
|
+
s && /* @__PURE__ */ l("div", { style: { ...Oe[r], ...je }, children: /* @__PURE__ */ l(
|
|
667
|
+
L,
|
|
668
|
+
{
|
|
669
|
+
messages: i,
|
|
670
|
+
isLoading: c,
|
|
671
|
+
error: f,
|
|
672
|
+
onSendMessage: x,
|
|
673
|
+
onClear: h,
|
|
674
|
+
onClose: () => d(!1),
|
|
675
|
+
showClose: !0,
|
|
676
|
+
placeholder: e.placeholder
|
|
677
|
+
}
|
|
678
|
+
) }),
|
|
679
|
+
/* @__PURE__ */ l(Be, { isOpen: s, onClick: () => d(!s) })
|
|
680
|
+
] }) : /* @__PURE__ */ l("div", { style: { height: "100%" }, children: /* @__PURE__ */ l(
|
|
681
|
+
L,
|
|
682
|
+
{
|
|
683
|
+
messages: i,
|
|
684
|
+
isLoading: c,
|
|
685
|
+
error: f,
|
|
686
|
+
onSendMessage: x,
|
|
687
|
+
onClear: h,
|
|
688
|
+
placeholder: e.placeholder
|
|
689
|
+
}
|
|
690
|
+
) });
|
|
691
|
+
}
|
|
692
|
+
function Pe(e, o) {
|
|
693
|
+
if (typeof document > "u")
|
|
694
|
+
throw new Error("initWecoolChat requires a browser environment");
|
|
695
|
+
let t;
|
|
696
|
+
if (typeof o == "string") {
|
|
697
|
+
const s = document.querySelector(o);
|
|
698
|
+
if (!s)
|
|
699
|
+
throw new Error(`Container element not found: ${o}`);
|
|
700
|
+
t = s;
|
|
701
|
+
} else o ? t = o : (t = document.createElement("div"), t.id = "wecool-chat-root", document.body.appendChild(t));
|
|
702
|
+
const n = X(t), r = {
|
|
703
|
+
config: e,
|
|
704
|
+
tools: e.tools,
|
|
705
|
+
floating: !o,
|
|
706
|
+
defaultOpen: !1,
|
|
707
|
+
position: "bottom-right"
|
|
708
|
+
};
|
|
709
|
+
return n.render(V.createElement(W, r)), () => {
|
|
710
|
+
n.unmount(), !o && t.parentNode && t.parentNode.removeChild(t);
|
|
711
|
+
};
|
|
712
|
+
}
|
|
713
|
+
const We = { initWecoolChat: Pe, ChatWidget: W };
|
|
714
|
+
export {
|
|
715
|
+
q as ChatClient,
|
|
716
|
+
Te as ChatFooter,
|
|
717
|
+
be as ChatHeader,
|
|
718
|
+
L as ChatPanel,
|
|
719
|
+
W as ChatWidget,
|
|
720
|
+
Be as FloatButton,
|
|
721
|
+
De as MessageList,
|
|
722
|
+
E as createAssistantMessage,
|
|
723
|
+
R as createMessageId,
|
|
724
|
+
J as createToolMessage,
|
|
725
|
+
G as createUserMessage,
|
|
726
|
+
We as default,
|
|
727
|
+
Pe as initWecoolChat
|
|
728
|
+
};
|