wecool-chat 0.4.0 → 0.5.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/README.ja.md +542 -0
- package/README.md +148 -140
- package/dist/components/ChatFooter.d.ts.map +1 -1
- package/dist/components/ChatHeader.d.ts.map +1 -1
- package/dist/components/ChatPanel.d.ts +2 -1
- package/dist/components/ChatPanel.d.ts.map +1 -1
- package/dist/components/ChatWidget.d.ts.map +1 -1
- package/dist/components/CopyIndicator.d.ts.map +1 -1
- package/dist/components/FloatButton.d.ts.map +1 -1
- package/dist/components/MessageBubble.d.ts +3 -8
- package/dist/components/MessageBubble.d.ts.map +1 -1
- package/dist/components/MessageCopyButton.d.ts.map +1 -1
- package/dist/components/MessageEditor.d.ts +14 -0
- package/dist/components/MessageEditor.d.ts.map +1 -0
- package/dist/components/MessageList.d.ts +2 -1
- package/dist/components/MessageList.d.ts.map +1 -1
- package/dist/components/MessagePopoverButton.d.ts +16 -0
- package/dist/components/MessagePopoverButton.d.ts.map +1 -0
- package/dist/components/MessageRow.d.ts +4 -0
- package/dist/components/MessageRow.d.ts.map +1 -1
- package/dist/components/MessageRowActions.d.ts +18 -0
- package/dist/components/MessageRowActions.d.ts.map +1 -0
- package/dist/components/markdown/CodeBlock.d.ts.map +1 -1
- package/dist/components/message-row-handlers.d.ts +27 -0
- package/dist/components/message-row-handlers.d.ts.map +1 -0
- package/dist/hooks/use-editing-message.d.ts +13 -0
- package/dist/hooks/use-editing-message.d.ts.map +1 -0
- package/dist/hooks/use-send-message.d.ts +16 -1
- package/dist/hooks/use-send-message.d.ts.map +1 -1
- package/dist/hooks/useChatStream.d.ts +3 -1
- package/dist/hooks/useChatStream.d.ts.map +1 -1
- package/dist/i18n/messages-context.d.ts +10 -0
- package/dist/i18n/messages-context.d.ts.map +1 -0
- package/dist/i18n/messages.d.ts +40 -0
- package/dist/i18n/messages.d.ts.map +1 -0
- package/dist/index.css +1 -1
- package/dist/index.js +9041 -8775
- package/package.json +4 -2
- package/dist/components/MessageRegenerateButton.d.ts +0 -12
- package/dist/components/MessageRegenerateButton.d.ts.map +0 -1
package/README.md
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
**English** | [日本語](./README.ja.md)
|
|
2
|
+
|
|
1
3
|
# wecool-chat
|
|
2
4
|
|
|
3
5
|
[](https://www.npmjs.com/package/wecool-chat)
|
|
@@ -5,47 +7,47 @@
|
|
|
5
7
|
[](https://opensource.org/licenses/MIT)
|
|
6
8
|
[](https://bundlephobia.com/package/wecool-chat)
|
|
7
9
|
|
|
8
|
-
>
|
|
10
|
+
> A drop-in LLM chat widget that lets users operate your UI in natural language — works with any framework.
|
|
9
11
|
|
|
10
|
-
|
|
12
|
+
When a user says things like "switch to dark mode" or "sort by newest", the LLM picks the right JavaScript function you defined and runs it. Works with React, Vue, or Vanilla JS — a single `npm install` away.
|
|
11
13
|
|
|
12
14
|

|
|
13
15
|
|
|
14
|
-
[
|
|
16
|
+
[Live Demo](https://demo.wecool.dev) ・ [Dashboard](https://app.wecool.dev) ・ [npm](https://www.npmjs.com/package/wecool-chat)
|
|
15
17
|
|
|
16
18
|
---
|
|
17
19
|
|
|
18
|
-
##
|
|
20
|
+
## Features
|
|
19
21
|
|
|
20
|
-
-
|
|
21
|
-
- **Tool Calling** — LLM
|
|
22
|
-
-
|
|
23
|
-
-
|
|
24
|
-
-
|
|
25
|
-
- **UI
|
|
22
|
+
- **Natural-language UI control** — users operate your app in plain language
|
|
23
|
+
- **Tool Calling** — the LLM interprets intent and runs your predefined JavaScript functions
|
|
24
|
+
- **Framework-agnostic** — `<ChatWidget>` for React, `initWecoolChat()` for Vue / Svelte / Astro / Vanilla JS
|
|
25
|
+
- **No extra dependencies** — `npm install wecool-chat` is all you need
|
|
26
|
+
- **Doesn't affect the host page** — Shadow DOM isolation prevents CSS conflicts with the host site
|
|
27
|
+
- **Bring your own UI** — use `ChatClient` directly to build a custom UI instead of the default one
|
|
26
28
|
|
|
27
29
|
---
|
|
28
30
|
|
|
29
|
-
##
|
|
31
|
+
## Use Cases
|
|
30
32
|
|
|
31
|
-
-
|
|
32
|
-
-
|
|
33
|
-
-
|
|
34
|
-
-
|
|
33
|
+
- **Dashboard assistance** — let users say "sort by newest" to trigger table sort, filter, or page navigation
|
|
34
|
+
- **Customer support embedding** — not just answering questions, but executing user setting changes and procedures on the spot
|
|
35
|
+
- **Documentation assistant** — answer questions like "how do I get started?" while auto-navigating to the right page
|
|
36
|
+
- **Internal tools** — add a natural-language operation entry point without changing the existing UI, so even new staff can operate it
|
|
35
37
|
|
|
36
38
|
---
|
|
37
39
|
|
|
38
|
-
##
|
|
40
|
+
## Quick Start
|
|
39
41
|
|
|
40
|
-
### 1.
|
|
42
|
+
### 1. Install
|
|
41
43
|
|
|
42
44
|
```bash
|
|
43
45
|
npm install wecool-chat
|
|
44
46
|
```
|
|
45
47
|
|
|
46
|
-
`pnpm` / `yarn` / `bun`
|
|
48
|
+
`pnpm` / `yarn` / `bun` work the same way.
|
|
47
49
|
|
|
48
|
-
### 2.
|
|
50
|
+
### 2. Define tools and place `<ChatWidget />`
|
|
49
51
|
|
|
50
52
|
```tsx
|
|
51
53
|
import { ChatWidget } from "wecool-chat";
|
|
@@ -55,11 +57,11 @@ const tools = [
|
|
|
55
57
|
{
|
|
56
58
|
name: "toggle_dark_mode",
|
|
57
59
|
description:
|
|
58
|
-
"
|
|
60
|
+
"Toggles dark mode. Use this when the user says things like 'switch to dark mode', 'make it dark', or 'make it light'.",
|
|
59
61
|
parameters: {
|
|
60
62
|
type: "object",
|
|
61
63
|
properties: {
|
|
62
|
-
enabled: { type: "boolean", description: "
|
|
64
|
+
enabled: { type: "boolean", description: "Whether to enable dark mode" },
|
|
63
65
|
},
|
|
64
66
|
required: ["enabled"],
|
|
65
67
|
},
|
|
@@ -75,7 +77,7 @@ function App() {
|
|
|
75
77
|
<ChatWidget
|
|
76
78
|
config={{
|
|
77
79
|
endpoint: "https://api.wecool.dev",
|
|
78
|
-
apiKey: "wecool_xxxxxxxx", //
|
|
80
|
+
apiKey: "wecool_xxxxxxxx", // see steps below to issue
|
|
79
81
|
tools,
|
|
80
82
|
}}
|
|
81
83
|
floating
|
|
@@ -84,105 +86,105 @@ function App() {
|
|
|
84
86
|
}
|
|
85
87
|
```
|
|
86
88
|
|
|
87
|
-
### 3.
|
|
89
|
+
### 3. Verify
|
|
88
90
|
|
|
89
|
-
|
|
91
|
+
Click the chat button (default: bottom-right) on the page → type "switch to dark mode" → the page changes to a dark theme.
|
|
90
92
|
|
|
91
|
-
> Vue / Svelte / Astro / Vanilla JS
|
|
93
|
+
> For Vue / Svelte / Astro / Vanilla JS integration, see the [Framework Guides](#framework-guides) section.
|
|
92
94
|
|
|
93
|
-
### API
|
|
95
|
+
### Getting an API key (one-time)
|
|
94
96
|
|
|
95
|
-
[
|
|
97
|
+
Sign up at the [Dashboard (app.wecool.dev)](https://app.wecool.dev) and issue a new key from "API Keys". The key is shown only once, so save it somewhere safe.
|
|
96
98
|
|
|
97
|
-
|
|
99
|
+
In production, load `apiKey` from environment variables. The syntax depends on the build tool you're using.
|
|
98
100
|
|
|
99
|
-
|
|
|
101
|
+
| Environment | Syntax |
|
|
100
102
|
| ---------------- | ---------------------------------------- |
|
|
101
103
|
| Next.js | `process.env.NEXT_PUBLIC_WECOOL_API_KEY` |
|
|
102
104
|
| Vite (React/Vue) | `import.meta.env.VITE_WECOOL_API_KEY` |
|
|
103
105
|
| Astro | `import.meta.env.PUBLIC_WECOOL_API_KEY` |
|
|
104
106
|
|
|
105
|
-
|
|
107
|
+
Since the API key is visible to the client, **restrict the calling origin** in the dashboard's "Allowed Origins" setting.
|
|
106
108
|
|
|
107
109
|
---
|
|
108
110
|
|
|
109
|
-
##
|
|
111
|
+
## Display Modes
|
|
110
112
|
|
|
111
|
-
|
|
113
|
+
The widget has two display modes.
|
|
112
114
|
|
|
113
|
-
###
|
|
115
|
+
### Floating (recommended)
|
|
114
116
|
|
|
115
|
-
|
|
117
|
+
A persistent chat button anchored to a corner of the screen. Clicking it opens the panel. Best for support and tour-like experiences.
|
|
116
118
|
|
|
117
119
|

|
|
118
120
|
|
|
119
|
-
###
|
|
121
|
+
### Embedded
|
|
120
122
|
|
|
121
|
-
|
|
123
|
+
Place the panel directly inside any container element. Best for embedding as an assistant section within a dashboard.
|
|
122
124
|
|
|
123
|
-
###
|
|
125
|
+
### How to switch
|
|
124
126
|
|
|
125
|
-
|
|
|
126
|
-
|
|
|
127
|
-
|
|
|
128
|
-
|
|
|
127
|
+
| Use case | React | Non-React |
|
|
128
|
+
| -------- | -------------------------------------- | ------------------------------------------- |
|
|
129
|
+
| Floating | `<ChatWidget config={...} floating />` | `initWecoolChat(config)` |
|
|
130
|
+
| Embedded | `<ChatWidget config={...} />` | `initWecoolChat(config, "#chat-container")` |
|
|
129
131
|
|
|
130
|
-
`initWecoolChat`
|
|
132
|
+
`initWecoolChat` is floating when the second argument is omitted, and embedded when an `HTMLElement` or CSS selector is passed.
|
|
131
133
|
|
|
132
134
|
---
|
|
133
135
|
|
|
134
|
-
##
|
|
136
|
+
## Defining Tools
|
|
135
137
|
|
|
136
|
-
wecool-chat
|
|
138
|
+
The core of wecool-chat is the "tool definition". The LLM reads the list of tools you provide, along with each `description`, and picks the most appropriate one based on what the user says to call its `handler`.
|
|
137
139
|
|
|
138
|
-
###
|
|
140
|
+
### Basic shape
|
|
139
141
|
|
|
140
|
-
`Tool`
|
|
142
|
+
The `Tool` type has 4 fields.
|
|
141
143
|
|
|
142
144
|
```ts
|
|
143
145
|
type Tool = {
|
|
144
|
-
name: string; //
|
|
145
|
-
description: string; // LLM
|
|
146
|
-
parameters: ToolParameters; //
|
|
146
|
+
name: string; // Unique identifier (e.g., "set_theme")
|
|
147
|
+
description: string; // What the LLM uses to decide when to call this tool
|
|
148
|
+
parameters: ToolParameters; // JSON Schema for arguments
|
|
147
149
|
handler: (args: Record<string, unknown>) => unknown | Promise<unknown>;
|
|
148
150
|
};
|
|
149
151
|
```
|
|
150
152
|
|
|
151
|
-
### `description`
|
|
153
|
+
### How you write `description` determines accuracy
|
|
152
154
|
|
|
153
|
-
LLM
|
|
155
|
+
The LLM reads the `description` to decide "when to use this tool". **Including examples of how the user might phrase it** improves accuracy.
|
|
154
156
|
|
|
155
157
|
```ts
|
|
156
158
|
{
|
|
157
159
|
name: "set_theme",
|
|
158
160
|
description:
|
|
159
|
-
"
|
|
161
|
+
"Changes the theme. Use this when the user says things like 'switch to dark mode', 'switch to light mode', 'make it dark', or 'make it light'.",
|
|
160
162
|
// ...
|
|
161
163
|
}
|
|
162
164
|
```
|
|
163
165
|
|
|
164
|
-
|
|
166
|
+
Listing 3–4 user phrasings yields a higher tool invocation rate than just naming the feature ("change theme").
|
|
165
167
|
|
|
166
|
-
###
|
|
168
|
+
### Complex arguments
|
|
167
169
|
|
|
168
|
-
`enum
|
|
170
|
+
`enum`, arrays, and nested objects are supported.
|
|
169
171
|
|
|
170
172
|
```ts
|
|
171
173
|
{
|
|
172
174
|
name: "sort_table",
|
|
173
|
-
description: "
|
|
175
|
+
description: "Sorts the table. Use this when the user says things like 'sort by newest' or 'sort by title'.",
|
|
174
176
|
parameters: {
|
|
175
177
|
type: "object",
|
|
176
178
|
properties: {
|
|
177
179
|
column: {
|
|
178
180
|
type: "string",
|
|
179
181
|
enum: ["date", "title", "status"],
|
|
180
|
-
description: "
|
|
182
|
+
description: "The column to sort by",
|
|
181
183
|
},
|
|
182
184
|
order: {
|
|
183
185
|
type: "string",
|
|
184
186
|
enum: ["asc", "desc"],
|
|
185
|
-
description: "
|
|
187
|
+
description: "Sort order (asc: ascending, desc: descending)",
|
|
186
188
|
},
|
|
187
189
|
},
|
|
188
190
|
required: ["column"],
|
|
@@ -194,33 +196,33 @@ LLM は `description` を読んで「いつこのツールを使うか」を判
|
|
|
194
196
|
}
|
|
195
197
|
```
|
|
196
198
|
|
|
197
|
-
|
|
199
|
+
Writing a `description` for each property as well makes it less likely for the LLM to mix up arguments.
|
|
198
200
|
|
|
199
|
-
###
|
|
201
|
+
### Common implementation patterns
|
|
200
202
|
|
|
201
|
-
`handler
|
|
203
|
+
You can also write code like the following inside `handler`:
|
|
202
204
|
|
|
203
|
-
- **API
|
|
204
|
-
-
|
|
205
|
+
- **Call an API and return the result** — write `handler: async (args) => { ... }` to await `fetch` results before returning
|
|
206
|
+
- **Surface errors to the user** — `throw new Error("message")` triggers `config.onError`. Returning `{ ok: false, message: "..." }` lets the LLM read the result and explain it back to the user in natural language
|
|
205
207
|
|
|
206
|
-
###
|
|
208
|
+
### Timeout (toolTimeout)
|
|
207
209
|
|
|
208
|
-
|
|
210
|
+
Tool execution times out at **30 seconds** by default. Override with `config.toolTimeout` (milliseconds, global setting).
|
|
209
211
|
|
|
210
212
|
```ts
|
|
211
213
|
const config = {
|
|
212
214
|
endpoint: "https://api.wecool.dev",
|
|
213
215
|
apiKey: "...",
|
|
214
216
|
tools,
|
|
215
|
-
toolTimeout: 60_000, // 60
|
|
217
|
+
toolTimeout: 60_000, // 60 seconds
|
|
216
218
|
};
|
|
217
219
|
```
|
|
218
220
|
|
|
219
|
-
>
|
|
221
|
+
> **Note**: Receiving an `AbortSignal` inside `handler` to cancel work is not supported in the current version. Keep heavy work lightweight on the handler side.
|
|
220
222
|
|
|
221
223
|
---
|
|
222
224
|
|
|
223
|
-
##
|
|
225
|
+
## Framework Guides
|
|
224
226
|
|
|
225
227
|
### Vue 3
|
|
226
228
|
|
|
@@ -242,7 +244,7 @@ onMounted(() => {
|
|
|
242
244
|
});
|
|
243
245
|
});
|
|
244
246
|
|
|
245
|
-
//
|
|
247
|
+
// Don't forget cleanup
|
|
246
248
|
onUnmounted(() => dispose?.());
|
|
247
249
|
</script>
|
|
248
250
|
```
|
|
@@ -255,7 +257,7 @@ onUnmounted(() => dispose?.());
|
|
|
255
257
|
import { initWecoolChat } from "wecool-chat";
|
|
256
258
|
import "wecool-chat/styles.css";
|
|
257
259
|
|
|
258
|
-
// onMount
|
|
260
|
+
// The return value of onMount is called as the dispose function
|
|
259
261
|
onMount(() =>
|
|
260
262
|
initWecoolChat({
|
|
261
263
|
endpoint: "https://api.wecool.dev",
|
|
@@ -286,7 +288,7 @@ onUnmounted(() => dispose?.());
|
|
|
286
288
|
tools: [/* ... */],
|
|
287
289
|
});
|
|
288
290
|
|
|
289
|
-
//
|
|
291
|
+
// Call dispose() when needed (e.g., SPA navigation)
|
|
290
292
|
window.addEventListener("beforeunload", () => dispose());
|
|
291
293
|
</script>
|
|
292
294
|
</body>
|
|
@@ -316,9 +318,9 @@ onUnmounted(() => dispose?.());
|
|
|
316
318
|
</html>
|
|
317
319
|
```
|
|
318
320
|
|
|
319
|
-
### Next.js
|
|
321
|
+
### Next.js (App Router)
|
|
320
322
|
|
|
321
|
-
|
|
323
|
+
Treat it as a `"use client"` component that runs only on the client side.
|
|
322
324
|
|
|
323
325
|
```tsx
|
|
324
326
|
// app/_components/chat.tsx
|
|
@@ -339,7 +341,7 @@ export function Chat() {
|
|
|
339
341
|
/* ... */
|
|
340
342
|
],
|
|
341
343
|
});
|
|
342
|
-
//
|
|
344
|
+
// Don't forget cleanup
|
|
343
345
|
return () => dispose.current?.();
|
|
344
346
|
}, []);
|
|
345
347
|
|
|
@@ -347,7 +349,7 @@ export function Chat() {
|
|
|
347
349
|
}
|
|
348
350
|
```
|
|
349
351
|
|
|
350
|
-
`<ChatWidget>`
|
|
352
|
+
If you want to use `<ChatWidget>` directly, import it via `dynamic` with `ssr: false` to avoid SSR.
|
|
351
353
|
|
|
352
354
|
```tsx
|
|
353
355
|
import dynamic from "next/dynamic";
|
|
@@ -358,13 +360,13 @@ const ChatWidget = dynamic(() => import("wecool-chat").then((m) => m.ChatWidget)
|
|
|
358
360
|
|
|
359
361
|
## ChatWidget Props
|
|
360
362
|
|
|
361
|
-
| Prop |
|
|
362
|
-
| ------------- | -------------------------------------------------------------- |
|
|
363
|
-
| `config` | `ChatConfig` | ✓
|
|
364
|
-
| `tools` | `Tool[]` |
|
|
365
|
-
| `floating` | `boolean` |
|
|
366
|
-
| `defaultOpen` | `boolean` |
|
|
367
|
-
| `position` | `"bottom-right" \| "bottom-left" \| "top-right" \| "top-left"` |
|
|
363
|
+
| Prop | Type | Required | Default | Description |
|
|
364
|
+
| ------------- | -------------------------------------------------------------- | -------- | ---------------- | ----------------------------------- |
|
|
365
|
+
| `config` | `ChatConfig` | ✓ | — | Configuration object |
|
|
366
|
+
| `tools` | `Tool[]` | | — | Overrides `config.tools` |
|
|
367
|
+
| `floating` | `boolean` | | `false` | Floating mode |
|
|
368
|
+
| `defaultOpen` | `boolean` | | `false` | Initial open state in floating mode |
|
|
369
|
+
| `position` | `"bottom-right" \| "bottom-left" \| "top-right" \| "top-left"` | | `"bottom-right"` | Position in floating mode |
|
|
368
370
|
|
|
369
371
|
```tsx
|
|
370
372
|
<ChatWidget config={config} floating defaultOpen position="bottom-left" />
|
|
@@ -372,51 +374,55 @@ const ChatWidget = dynamic(() => import("wecool-chat").then((m) => m.ChatWidget)
|
|
|
372
374
|
|
|
373
375
|
---
|
|
374
376
|
|
|
375
|
-
## ChatConfig
|
|
376
|
-
|
|
377
|
-
|
|
|
378
|
-
| ------------------ | ------------------------ |
|
|
379
|
-
| `endpoint` | `string` | ✓
|
|
380
|
-
| `apiKey` | `string` |
|
|
381
|
-
| `tools` | `Tool[]` | ✓
|
|
382
|
-
| `locale` | `"ja" \| "en"` |
|
|
383
|
-
| `placeholder` | `string` |
|
|
384
|
-
| `className` | `string` |
|
|
385
|
-
| `toolTimeout` | `number` |
|
|
386
|
-
| `disableShadowDom` | `boolean` |
|
|
387
|
-
| `onToolExecute` | `(name, args) => void` |
|
|
388
|
-
| `onToolResult` | `(name, result) => void` |
|
|
389
|
-
| `onError` | `(error: Error) => void` |
|
|
377
|
+
## ChatConfig Options
|
|
378
|
+
|
|
379
|
+
| Property | Type | Required | Default | Description |
|
|
380
|
+
| ------------------ | ------------------------ | -------- | ------------------- | -------------------------------------------- |
|
|
381
|
+
| `endpoint` | `string` | ✓ | — | API endpoint URL |
|
|
382
|
+
| `apiKey` | `string` | | — | API key issued from the dashboard |
|
|
383
|
+
| `tools` | `Tool[]` | ✓ | — | Array of tool definitions |
|
|
384
|
+
| `locale` | `"ja" \| "en"` | | `"ja"` | UI language. Set `"en"` for English |
|
|
385
|
+
| `placeholder` | `string` | | depends on `locale` | Input field placeholder |
|
|
386
|
+
| `className` | `string` | | — | Class name applied to the root element |
|
|
387
|
+
| `toolTimeout` | `number` | | `30000` | Tool execution timeout (ms) |
|
|
388
|
+
| `disableShadowDom` | `boolean` | | `false` | Mount to normal DOM instead of Shadow DOM |
|
|
389
|
+
| `onToolExecute` | `(name, args) => void` | | — | Callback fired right before tool execution |
|
|
390
|
+
| `onToolResult` | `(name, result) => void` | | — | Callback fired after tool execution finishes |
|
|
391
|
+
| `onError` | `(error: Error) => void` | | — | Callback fired on error |
|
|
390
392
|
|
|
391
393
|
---
|
|
392
394
|
|
|
393
|
-
##
|
|
395
|
+
## Customization
|
|
396
|
+
|
|
397
|
+
### Locale and labels
|
|
394
398
|
|
|
395
|
-
|
|
399
|
+
Setting `locale` switches all in-widget display strings at once: input placeholder, button `aria-label`s, error messages, and the empty-state label. Supports `"ja"` (default) and `"en"`.
|
|
396
400
|
|
|
397
401
|
```ts
|
|
398
402
|
const config = {
|
|
399
403
|
endpoint: "https://api.wecool.dev",
|
|
400
404
|
apiKey: "...",
|
|
401
405
|
tools,
|
|
402
|
-
locale: "en", // UI
|
|
403
|
-
placeholder: "Ask me anything...", //
|
|
406
|
+
locale: "en", // UI labels in English
|
|
407
|
+
placeholder: "Ask me anything...", // override only the input placeholder
|
|
404
408
|
};
|
|
405
409
|
```
|
|
406
410
|
|
|
407
|
-
|
|
411
|
+
If `placeholder` is provided, it takes precedence over the locale's default placeholder.
|
|
408
412
|
|
|
409
|
-
|
|
413
|
+
### Colors / theme
|
|
410
414
|
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
-
|
|
415
|
+
The current version does not expose official theme variables you can override from outside. To change colors, you have these options:
|
|
416
|
+
|
|
417
|
+
- Apply a class to the root element via `className` and target it with Tailwind or your own CSS
|
|
418
|
+
- Set `disableShadowDom: true` to let the host site's CSS apply directly (Shadow DOM isolation is lost)
|
|
419
|
+
- Official theme variables are planned for a future version
|
|
414
420
|
|
|
415
421
|
---
|
|
416
422
|
|
|
417
|
-
##
|
|
423
|
+
## Event Callbacks
|
|
418
424
|
|
|
419
|
-
|
|
425
|
+
You can hook into each phase of tool execution to add your own logic.
|
|
420
426
|
|
|
421
427
|
```ts
|
|
422
428
|
const config = {
|
|
@@ -427,7 +433,7 @@ const config = {
|
|
|
427
433
|
console.log("[wecool] executing", name, args);
|
|
428
434
|
},
|
|
429
435
|
onToolResult: (name, result) => {
|
|
430
|
-
//
|
|
436
|
+
// Audit log, Sentry breadcrumb, UI toast, etc.
|
|
431
437
|
auditLog({ tool: name, result });
|
|
432
438
|
},
|
|
433
439
|
onError: (error) => {
|
|
@@ -436,23 +442,23 @@ const config = {
|
|
|
436
442
|
};
|
|
437
443
|
```
|
|
438
444
|
|
|
439
|
-
|
|
445
|
+
Heavy work inside callbacks can block chat rendering. Wrap long-running work in `setTimeout` or similar to keep it asynchronous.
|
|
440
446
|
|
|
441
447
|
---
|
|
442
448
|
|
|
443
|
-
## UI
|
|
449
|
+
## Building Your Own UI (ChatClient)
|
|
444
450
|
|
|
445
|
-
|
|
451
|
+
If you want to build a custom UI instead of the bundled one, use `ChatClient` directly to consume server responses incrementally.
|
|
446
452
|
|
|
447
453
|
```ts
|
|
448
454
|
import { ChatClient, createUserMessage } from "wecool-chat";
|
|
449
455
|
|
|
450
456
|
const client = new ChatClient("https://api.wecool.dev", "<api-key>");
|
|
451
457
|
|
|
452
|
-
const history = [createUserMessage("
|
|
458
|
+
const history = [createUserMessage("switch to dark mode")];
|
|
453
459
|
|
|
454
460
|
for await (const event of client.chat({
|
|
455
|
-
message: "
|
|
461
|
+
message: "switch to dark mode",
|
|
456
462
|
tools: [
|
|
457
463
|
/* ToolDefinition[] */
|
|
458
464
|
],
|
|
@@ -472,22 +478,24 @@ for await (const event of client.chat({
|
|
|
472
478
|
}
|
|
473
479
|
```
|
|
474
480
|
|
|
475
|
-
`ChatEvent`
|
|
481
|
+
`ChatEvent` is one of 6 event types: `thinking` / `tool_use` / `text` / `text_delta` / `error` / `done`.
|
|
482
|
+
|
|
483
|
+
> **When using individually exported components (`ChatPanel` / `ChatHeader` / `ChatFooter` / `MessageList` / `FloatButton`) directly**, `config.locale` is not auto-distributed. To use them in non-default locales, either wrap them with your own provider or go through `<ChatWidget>`.
|
|
476
484
|
|
|
477
|
-
|
|
485
|
+
Helper functions are also provided:
|
|
478
486
|
|
|
479
|
-
- `createMessageId()` —
|
|
480
|
-
- `createUserMessage(content)` —
|
|
481
|
-
- `createAssistantMessage(content)` —
|
|
482
|
-
- `createToolMessage(name, args, result)` —
|
|
483
|
-
- `createErrorMessage(content)` —
|
|
484
|
-
- `client.getConversationId()` / `client.resetConversation()` —
|
|
487
|
+
- `createMessageId()` — generates a unique `msg-<UUID>`
|
|
488
|
+
- `createUserMessage(content)` — message object for a user utterance
|
|
489
|
+
- `createAssistantMessage(content)` — assistant response
|
|
490
|
+
- `createToolMessage(name, args, result)` — tool execution result
|
|
491
|
+
- `createErrorMessage(content)` — for displaying errors
|
|
492
|
+
- `client.getConversationId()` / `client.resetConversation()` — get / reset the conversation ID
|
|
485
493
|
|
|
486
494
|
---
|
|
487
495
|
|
|
488
|
-
## TypeScript
|
|
496
|
+
## Using with TypeScript
|
|
489
497
|
|
|
490
|
-
|
|
498
|
+
Type definitions are bundled, so no extra setup is required.
|
|
491
499
|
|
|
492
500
|
```ts
|
|
493
501
|
import type {
|
|
@@ -505,30 +513,30 @@ import type {
|
|
|
505
513
|
|
|
506
514
|
---
|
|
507
515
|
|
|
508
|
-
##
|
|
516
|
+
## Troubleshooting
|
|
509
517
|
|
|
510
|
-
|
|
511
|
-
|
|
518
|
+
**The widget doesn't show up**
|
|
519
|
+
Look for `<wecool-chat-shadow-host>` in the DevTools Elements panel and verify React is mounted under the Shadow Root. If the Shadow Root is empty, check the `endpoint` / `apiKey` values and any errors in the `console`.
|
|
512
520
|
|
|
513
|
-
|
|
514
|
-
`description`
|
|
521
|
+
**Tools aren't called (the LLM just replies as plain conversation)**
|
|
522
|
+
Review the quality of `description`. Listing several concrete examples of how a user might phrase it improves accuracy. Also check for missing `parameters.required` entries. Logging from `onError` helps you observe the calls the LLM actually makes.
|
|
515
523
|
|
|
516
|
-
**Next.js
|
|
517
|
-
`<ChatWidget>`
|
|
524
|
+
**Build error in Next.js**
|
|
525
|
+
Either load `<ChatWidget>` via `dynamic(() => import("wecool-chat").then((m) => m.ChatWidget), { ssr: false })`, or call `initWecoolChat` inside a `useEffect` of a `"use client"` component.
|
|
518
526
|
|
|
519
|
-
|
|
520
|
-
|
|
527
|
+
**Conflicts with the host site's CSS**
|
|
528
|
+
The widget is isolated inside Shadow DOM by default, so conflicts shouldn't normally happen. Make sure `config.disableShadowDom: true` isn't set.
|
|
521
529
|
|
|
522
|
-
|
|
523
|
-
`config.toolTimeout`
|
|
530
|
+
**Tools are timing out**
|
|
531
|
+
Increase `config.toolTimeout` (default 30,000ms) or make the `handler` lighter. Cancellation via `AbortSignal` is not supported in the current version.
|
|
524
532
|
|
|
525
|
-
|
|
526
|
-
wecool-chat
|
|
533
|
+
**Worried about React conflicts with my own app**
|
|
534
|
+
wecool-chat bundles React internally, but it's isolated inside the widget, so it doesn't affect the host React app. If versions match, npm / pnpm's automatic optimization will deduplicate them into one.
|
|
527
535
|
|
|
528
536
|
---
|
|
529
537
|
|
|
530
|
-
##
|
|
538
|
+
## Changelog / License
|
|
531
539
|
|
|
532
|
-
|
|
540
|
+
See [CHANGELOG.md](./CHANGELOG.md) for the changelog.
|
|
533
541
|
|
|
534
|
-
|
|
542
|
+
License: MIT
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ChatFooter.d.ts","sourceRoot":"","sources":["../../src/components/ChatFooter.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"ChatFooter.d.ts","sourceRoot":"","sources":["../../src/components/ChatFooter.tsx"],"names":[],"mappings":"AAKA,KAAK,eAAe,GAAG;IACrB,QAAQ,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;IACpC,OAAO,EAAE,MAAM,IAAI,CAAC;IACpB,QAAQ,EAAE,MAAM,IAAI,CAAC;IACrB,SAAS,EAAE,OAAO,CAAC;IACnB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,wBAAgB,UAAU,CAAC,EACzB,QAAQ,EACR,OAAO,EACP,QAAQ,EACR,SAAS,EACT,WAAW,GACZ,EAAE,eAAe,2CA2FjB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ChatHeader.d.ts","sourceRoot":"","sources":["../../src/components/ChatHeader.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"ChatHeader.d.ts","sourceRoot":"","sources":["../../src/components/ChatHeader.tsx"],"names":[],"mappings":"AAIA,KAAK,eAAe,GAAG;IACrB,OAAO,CAAC,EAAE,MAAM,IAAI,CAAC;IACrB,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB,CAAC;AAEF,wBAAgB,UAAU,CAAC,EAAE,OAAO,EAAE,SAAiB,EAAE,EAAE,eAAe,2CAazE"}
|
|
@@ -4,12 +4,13 @@ type ChatPanelProps = {
|
|
|
4
4
|
isLoading: boolean;
|
|
5
5
|
onSendMessage: (message: string) => void;
|
|
6
6
|
onRegenerate: () => void;
|
|
7
|
+
onEditMessage: (messageId: string, newContent: string) => void;
|
|
7
8
|
onCancel: () => void;
|
|
8
9
|
onClear: () => void;
|
|
9
10
|
onClose?: () => void;
|
|
10
11
|
showClose?: boolean;
|
|
11
12
|
placeholder?: string;
|
|
12
13
|
};
|
|
13
|
-
export declare function ChatPanel({ messages, isLoading, onSendMessage, onRegenerate, onCancel, onClear, onClose, showClose, placeholder, }: ChatPanelProps): import("react/jsx-runtime").JSX.Element;
|
|
14
|
+
export declare function ChatPanel({ messages, isLoading, onSendMessage, onRegenerate, onEditMessage, onCancel, onClear, onClose, showClose, placeholder, }: ChatPanelProps): import("react/jsx-runtime").JSX.Element;
|
|
14
15
|
export {};
|
|
15
16
|
//# sourceMappingURL=ChatPanel.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ChatPanel.d.ts","sourceRoot":"","sources":["../../src/components/ChatPanel.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAKxC,KAAK,cAAc,GAAG;IACpB,QAAQ,EAAE,OAAO,EAAE,CAAC;IACpB,SAAS,EAAE,OAAO,CAAC;IACnB,aAAa,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;IACzC,YAAY,EAAE,MAAM,IAAI,CAAC;IACzB,QAAQ,EAAE,MAAM,IAAI,CAAC;IACrB,OAAO,EAAE,MAAM,IAAI,CAAC;IACpB,OAAO,CAAC,EAAE,MAAM,IAAI,CAAC;IACrB,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,wBAAgB,SAAS,CAAC,EACxB,QAAQ,EACR,SAAS,EACT,aAAa,EACb,YAAY,EACZ,QAAQ,EACR,OAAO,EACP,OAAO,EACP,SAAiB,EACjB,WAAW,GACZ,EAAE,cAAc,
|
|
1
|
+
{"version":3,"file":"ChatPanel.d.ts","sourceRoot":"","sources":["../../src/components/ChatPanel.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAKxC,KAAK,cAAc,GAAG;IACpB,QAAQ,EAAE,OAAO,EAAE,CAAC;IACpB,SAAS,EAAE,OAAO,CAAC;IACnB,aAAa,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;IACzC,YAAY,EAAE,MAAM,IAAI,CAAC;IACzB,aAAa,EAAE,CAAC,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,KAAK,IAAI,CAAC;IAC/D,QAAQ,EAAE,MAAM,IAAI,CAAC;IACrB,OAAO,EAAE,MAAM,IAAI,CAAC;IACpB,OAAO,CAAC,EAAE,MAAM,IAAI,CAAC;IACrB,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,wBAAgB,SAAS,CAAC,EACxB,QAAQ,EACR,SAAS,EACT,aAAa,EACb,YAAY,EACZ,aAAa,EACb,QAAQ,EACR,OAAO,EACP,OAAO,EACP,SAAiB,EACjB,WAAW,GACZ,EAAE,cAAc,2CAmBhB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ChatWidget.d.ts","sourceRoot":"","sources":["../../src/components/ChatWidget.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,eAAe,EAAwB,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"ChatWidget.d.ts","sourceRoot":"","sources":["../../src/components/ChatWidget.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,eAAe,EAAwB,MAAM,UAAU,CAAC;AA8BtE,wBAAgB,UAAU,CAAC,EACzB,MAAM,EACN,KAAK,EAAE,SAAS,EAChB,QAAgB,EAChB,WAAmB,EACnB,QAAyB,GAC1B,EAAE,eAAe,2CAkEjB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CopyIndicator.d.ts","sourceRoot":"","sources":["../../src/components/CopyIndicator.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"CopyIndicator.d.ts","sourceRoot":"","sources":["../../src/components/CopyIndicator.tsx"],"names":[],"mappings":"AAGA,KAAK,kBAAkB,GAAG;IACxB,MAAM,EAAE,OAAO,CAAC;CACjB,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,aAAa,EAAE,KAAK,CAAC,EAAE,CAAC,kBAAkB,CAUtD,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FloatButton.d.ts","sourceRoot":"","sources":["../../src/components/FloatButton.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"FloatButton.d.ts","sourceRoot":"","sources":["../../src/components/FloatButton.tsx"],"names":[],"mappings":"AAGA,KAAK,gBAAgB,GAAG;IACtB,MAAM,EAAE,OAAO,CAAC;IAChB,OAAO,EAAE,MAAM,IAAI,CAAC;CACrB,CAAC;AAEF,wBAAgB,WAAW,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,EAAE,gBAAgB,2CAWhE"}
|