wecool-chat 0.3.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.
Files changed (53) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/README.ja.md +542 -0
  3. package/README.md +483 -53
  4. package/dist/client.d.ts +5 -0
  5. package/dist/client.d.ts.map +1 -1
  6. package/dist/components/ChatFooter.d.ts.map +1 -1
  7. package/dist/components/ChatHeader.d.ts.map +1 -1
  8. package/dist/components/ChatPanel.d.ts +3 -2
  9. package/dist/components/ChatPanel.d.ts.map +1 -1
  10. package/dist/components/ChatWidget.d.ts.map +1 -1
  11. package/dist/components/CopyIndicator.d.ts.map +1 -1
  12. package/dist/components/FloatButton.d.ts.map +1 -1
  13. package/dist/components/MessageActionsPopover.d.ts +6 -10
  14. package/dist/components/MessageActionsPopover.d.ts.map +1 -1
  15. package/dist/components/MessageBubble.d.ts +15 -0
  16. package/dist/components/MessageBubble.d.ts.map +1 -0
  17. package/dist/components/MessageCopyButton.d.ts.map +1 -1
  18. package/dist/components/MessageEditor.d.ts +14 -0
  19. package/dist/components/MessageEditor.d.ts.map +1 -0
  20. package/dist/components/MessageList.d.ts +3 -1
  21. package/dist/components/MessageList.d.ts.map +1 -1
  22. package/dist/components/MessagePopoverButton.d.ts +16 -0
  23. package/dist/components/MessagePopoverButton.d.ts.map +1 -0
  24. package/dist/components/MessageRow.d.ts +6 -0
  25. package/dist/components/MessageRow.d.ts.map +1 -1
  26. package/dist/components/MessageRowActions.d.ts +18 -0
  27. package/dist/components/MessageRowActions.d.ts.map +1 -0
  28. package/dist/components/markdown/CodeBlock.d.ts.map +1 -1
  29. package/dist/components/message-row-handlers.d.ts +27 -0
  30. package/dist/components/message-row-handlers.d.ts.map +1 -0
  31. package/dist/hooks/use-editing-message.d.ts +13 -0
  32. package/dist/hooks/use-editing-message.d.ts.map +1 -0
  33. package/dist/hooks/use-send-message.d.ts +20 -2
  34. package/dist/hooks/use-send-message.d.ts.map +1 -1
  35. package/dist/hooks/useChatStream.d.ts +6 -1
  36. package/dist/hooks/useChatStream.d.ts.map +1 -1
  37. package/dist/hooks/useDismissOnOutside.d.ts +6 -0
  38. package/dist/hooks/useDismissOnOutside.d.ts.map +1 -1
  39. package/dist/i18n/messages-context.d.ts +10 -0
  40. package/dist/i18n/messages-context.d.ts.map +1 -0
  41. package/dist/i18n/messages.d.ts +40 -0
  42. package/dist/i18n/messages.d.ts.map +1 -0
  43. package/dist/index.css +1 -1
  44. package/dist/index.d.ts +4 -1
  45. package/dist/index.d.ts.map +1 -1
  46. package/dist/index.js +19098 -8239
  47. package/dist/styles/index.d.ts +2 -0
  48. package/dist/styles/index.d.ts.map +1 -0
  49. package/dist/types.d.ts +7 -0
  50. package/dist/types.d.ts.map +1 -1
  51. package/docs/screenshots/hero.gif +0 -0
  52. package/docs/screenshots/mode-floating.png +0 -0
  53. package/package.json +16 -7
package/README.md CHANGED
@@ -1,37 +1,53 @@
1
- # wecool
1
+ **English** | [日本語](./README.ja.md)
2
+
3
+ # wecool-chat
2
4
 
3
5
  [![npm version](https://img.shields.io/npm/v/wecool-chat.svg)](https://www.npmjs.com/package/wecool-chat)
6
+ [![npm downloads](https://img.shields.io/npm/dm/wecool-chat.svg)](https://www.npmjs.com/package/wecool-chat)
4
7
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
8
+ [![bundle size](https://img.shields.io/bundlephobia/minzip/wecool-chat.svg)](https://bundlephobia.com/package/wecool-chat)
9
+
10
+ > A drop-in LLM chat widget that lets users operate your UI in natural language — works with any framework.
11
+
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.
5
13
 
6
- wecool は、Webアプリケーションに自然言語チャットインターフェースを組み込むためのReactウィジェットです。
14
+ ![hero demo](./docs/screenshots/hero.gif)
7
15
 
8
- ユーザーが「ダークモードにして」「一覧を名前順にソートして」のように話しかけると、LLM(大規模言語モデル)が意図を解釈し、あらかじめ定義したJavaScript関数(ツール)を自動で選択・実行します。テーマ切替、ページ遷移、テーブル操作、フォーム入力など、UIの操作を自然言語で行えるようになります。
16
+ [Live Demo](https://demo.wecool.dev) ・ [Dashboard](https://app.wecool.dev) ・ [npm](https://www.npmjs.com/package/wecool-chat)
9
17
 
10
- ## 特徴
18
+ ---
11
19
 
12
- - **自然言語でUI操作** - ユーザーは普通の言葉でアプリを操作できる
13
- - **Tool Calling** - LLMが意図を解釈し、定義済みのJavaScript関数を自動実行
14
- - **Reactコンポーネント** - Reactアプリに簡単に組み込み可能
15
- - **カスタマイズ可能** - フローティングモード・インラインモード対応
16
- - **ヘッドレス対応** - ChatEngineを使ってUIを自作することも可能
20
+ ## Features
17
21
 
18
- ## 利用開始手順
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
19
28
 
20
- ### 1. アカウント登録
29
+ ---
21
30
 
22
- [ダッシュボード(app.wecool.dev)](https://app.wecool.dev)にアクセスし、アカウントを作成します。登録後、確認メールが届くのでメール内のリンクをクリックして認証を完了してください。
31
+ ## Use Cases
23
32
 
24
- ### 2. APIキーの発行
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
25
37
 
26
- ダッシュボードにログインし、「API Keys」ページから新しいAPIキーを作成します。表示されたキーを控えておいてください(再表示はできません)。
38
+ ---
27
39
 
28
- ### 3. パッケージのインストール
40
+ ## Quick Start
41
+
42
+ ### 1. Install
29
43
 
30
44
  ```bash
31
45
  npm install wecool-chat
32
46
  ```
33
47
 
34
- ### 4. コンポーネントの配置
48
+ `pnpm` / `yarn` / `bun` work the same way.
49
+
50
+ ### 2. Define tools and place `<ChatWidget />`
35
51
 
36
52
  ```tsx
37
53
  import { ChatWidget } from "wecool-chat";
@@ -39,17 +55,18 @@ import "wecool-chat/styles.css";
39
55
 
40
56
  const tools = [
41
57
  {
42
- name: "toggleDarkMode",
43
- description: "ダークモードの切り替え",
58
+ name: "toggle_dark_mode",
59
+ description:
60
+ "Toggles dark mode. Use this when the user says things like 'switch to dark mode', 'make it dark', or 'make it light'.",
44
61
  parameters: {
45
62
  type: "object",
46
63
  properties: {
47
- enabled: { type: "boolean" },
64
+ enabled: { type: "boolean", description: "Whether to enable dark mode" },
48
65
  },
49
66
  required: ["enabled"],
50
67
  },
51
68
  handler: (args) => {
52
- document.body.classList.toggle("dark", args.enabled);
69
+ document.documentElement.classList.toggle("dark", args.enabled);
53
70
  return { success: true };
54
71
  },
55
72
  },
@@ -60,53 +77,466 @@ function App() {
60
77
  <ChatWidget
61
78
  config={{
62
79
  endpoint: "https://api.wecool.dev",
63
- apiKey: "your-api-key", // 手順2で発行したAPIキー
80
+ apiKey: "wecool_xxxxxxxx", // see steps below to issue
64
81
  tools,
65
82
  }}
66
- floating={true}
67
- position="bottom-right"
83
+ floating
68
84
  />
69
85
  );
70
86
  }
71
87
  ```
72
88
 
73
- ## API リファレンス
89
+ ### 3. Verify
90
+
91
+ Click the chat button (default: bottom-right) on the page → type "switch to dark mode" → the page changes to a dark theme.
92
+
93
+ > For Vue / Svelte / Astro / Vanilla JS integration, see the [Framework Guides](#framework-guides) section.
94
+
95
+ ### Getting an API key (one-time)
96
+
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.
98
+
99
+ In production, load `apiKey` from environment variables. The syntax depends on the build tool you're using.
100
+
101
+ | Environment | Syntax |
102
+ | ---------------- | ---------------------------------------- |
103
+ | Next.js | `process.env.NEXT_PUBLIC_WECOOL_API_KEY` |
104
+ | Vite (React/Vue) | `import.meta.env.VITE_WECOOL_API_KEY` |
105
+ | Astro | `import.meta.env.PUBLIC_WECOOL_API_KEY` |
106
+
107
+ Since the API key is visible to the client, **restrict the calling origin** in the dashboard's "Allowed Origins" setting.
108
+
109
+ ---
110
+
111
+ ## Display Modes
112
+
113
+ The widget has two display modes.
114
+
115
+ ### Floating (recommended)
116
+
117
+ A persistent chat button anchored to a corner of the screen. Clicking it opens the panel. Best for support and tour-like experiences.
118
+
119
+ ![floating mode](./docs/screenshots/mode-floating.png)
120
+
121
+ ### Embedded
122
+
123
+ Place the panel directly inside any container element. Best for embedding as an assistant section within a dashboard.
124
+
125
+ ### How to switch
126
+
127
+ | Use case | React | Non-React |
128
+ | -------- | -------------------------------------- | ------------------------------------------- |
129
+ | Floating | `<ChatWidget config={...} floating />` | `initWecoolChat(config)` |
130
+ | Embedded | `<ChatWidget config={...} />` | `initWecoolChat(config, "#chat-container")` |
131
+
132
+ `initWecoolChat` is floating when the second argument is omitted, and embedded when an `HTMLElement` or CSS selector is passed.
133
+
134
+ ---
135
+
136
+ ## Defining Tools
137
+
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`.
139
+
140
+ ### Basic shape
141
+
142
+ The `Tool` type has 4 fields.
143
+
144
+ ```ts
145
+ type Tool = {
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
149
+ handler: (args: Record<string, unknown>) => unknown | Promise<unknown>;
150
+ };
151
+ ```
152
+
153
+ ### How you write `description` determines accuracy
154
+
155
+ The LLM reads the `description` to decide "when to use this tool". **Including examples of how the user might phrase it** improves accuracy.
156
+
157
+ ```ts
158
+ {
159
+ name: "set_theme",
160
+ description:
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'.",
162
+ // ...
163
+ }
164
+ ```
165
+
166
+ Listing 3–4 user phrasings yields a higher tool invocation rate than just naming the feature ("change theme").
167
+
168
+ ### Complex arguments
169
+
170
+ `enum`, arrays, and nested objects are supported.
171
+
172
+ ```ts
173
+ {
174
+ name: "sort_table",
175
+ description: "Sorts the table. Use this when the user says things like 'sort by newest' or 'sort by title'.",
176
+ parameters: {
177
+ type: "object",
178
+ properties: {
179
+ column: {
180
+ type: "string",
181
+ enum: ["date", "title", "status"],
182
+ description: "The column to sort by",
183
+ },
184
+ order: {
185
+ type: "string",
186
+ enum: ["asc", "desc"],
187
+ description: "Sort order (asc: ascending, desc: descending)",
188
+ },
189
+ },
190
+ required: ["column"],
191
+ },
192
+ handler: ({ column, order }) => {
193
+ sortBy(column as string, (order as string) ?? "asc");
194
+ return { success: true };
195
+ },
196
+ }
197
+ ```
198
+
199
+ Writing a `description` for each property as well makes it less likely for the LLM to mix up arguments.
200
+
201
+ ### Common implementation patterns
202
+
203
+ You can also write code like the following inside `handler`:
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
207
+
208
+ ### Timeout (toolTimeout)
209
+
210
+ Tool execution times out at **30 seconds** by default. Override with `config.toolTimeout` (milliseconds, global setting).
211
+
212
+ ```ts
213
+ const config = {
214
+ endpoint: "https://api.wecool.dev",
215
+ apiKey: "...",
216
+ tools,
217
+ toolTimeout: 60_000, // 60 seconds
218
+ };
219
+ ```
220
+
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.
222
+
223
+ ---
224
+
225
+ ## Framework Guides
226
+
227
+ ### Vue 3
228
+
229
+ ```vue
230
+ <script setup lang="ts">
231
+ import { onMounted, onUnmounted } from "vue";
232
+ import { initWecoolChat } from "wecool-chat";
233
+ import "wecool-chat/styles.css";
234
+
235
+ let dispose: (() => void) | null = null;
236
+
237
+ onMounted(() => {
238
+ dispose = initWecoolChat({
239
+ endpoint: "https://api.wecool.dev",
240
+ apiKey: import.meta.env.VITE_WECOOL_API_KEY,
241
+ tools: [
242
+ /* ... */
243
+ ],
244
+ });
245
+ });
246
+
247
+ // Don't forget cleanup
248
+ onUnmounted(() => dispose?.());
249
+ </script>
250
+ ```
251
+
252
+ ### Svelte
253
+
254
+ ```svelte
255
+ <script lang="ts">
256
+ import { onMount } from "svelte";
257
+ import { initWecoolChat } from "wecool-chat";
258
+ import "wecool-chat/styles.css";
259
+
260
+ // The return value of onMount is called as the dispose function
261
+ onMount(() =>
262
+ initWecoolChat({
263
+ endpoint: "https://api.wecool.dev",
264
+ apiKey: import.meta.env.VITE_WECOOL_API_KEY,
265
+ tools: [/* ... */],
266
+ })
267
+ );
268
+ </script>
269
+ ```
270
+
271
+ ### Astro
272
+
273
+ ```astro
274
+ ---
275
+ // src/pages/index.astro
276
+ ---
277
+
278
+ <html>
279
+ <body>
280
+ <slot />
281
+ <script>
282
+ import { initWecoolChat } from "wecool-chat";
283
+ import "wecool-chat/styles.css";
284
+
285
+ const dispose = initWecoolChat({
286
+ endpoint: "https://api.wecool.dev",
287
+ apiKey: import.meta.env.PUBLIC_WECOOL_API_KEY,
288
+ tools: [/* ... */],
289
+ });
290
+
291
+ // Call dispose() when needed (e.g., SPA navigation)
292
+ window.addEventListener("beforeunload", () => dispose());
293
+ </script>
294
+ </body>
295
+ </html>
296
+ ```
297
+
298
+ ### Vanilla JS
299
+
300
+ ```html
301
+ <!doctype html>
302
+ <html>
303
+ <body>
304
+ <script type="module">
305
+ import { initWecoolChat } from "wecool-chat";
306
+ import "wecool-chat/styles.css";
307
+
308
+ const dispose = initWecoolChat({
309
+ endpoint: "https://api.wecool.dev",
310
+ apiKey: "<your-api-key>",
311
+ tools: [
312
+ /* ... */
313
+ ],
314
+ });
315
+ // window.addEventListener("beforeunload", () => dispose());
316
+ </script>
317
+ </body>
318
+ </html>
319
+ ```
320
+
321
+ ### Next.js (App Router)
322
+
323
+ Treat it as a `"use client"` component that runs only on the client side.
324
+
325
+ ```tsx
326
+ // app/_components/chat.tsx
327
+ "use client";
328
+
329
+ import { useEffect, useRef } from "react";
330
+ import { initWecoolChat } from "wecool-chat";
331
+ import "wecool-chat/styles.css";
332
+
333
+ export function Chat() {
334
+ const dispose = useRef<(() => void) | null>(null);
335
+
336
+ useEffect(() => {
337
+ dispose.current = initWecoolChat({
338
+ endpoint: process.env.NEXT_PUBLIC_API_URL!,
339
+ apiKey: process.env.NEXT_PUBLIC_WECOOL_API_KEY,
340
+ tools: [
341
+ /* ... */
342
+ ],
343
+ });
344
+ // Don't forget cleanup
345
+ return () => dispose.current?.();
346
+ }, []);
347
+
348
+ return null;
349
+ }
350
+ ```
351
+
352
+ If you want to use `<ChatWidget>` directly, import it via `dynamic` with `ssr: false` to avoid SSR.
353
+
354
+ ```tsx
355
+ import dynamic from "next/dynamic";
356
+ const ChatWidget = dynamic(() => import("wecool-chat").then((m) => m.ChatWidget), { ssr: false });
357
+ ```
358
+
359
+ ---
360
+
361
+ ## ChatWidget Props
362
+
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 |
370
+
371
+ ```tsx
372
+ <ChatWidget config={config} floating defaultOpen position="bottom-left" />
373
+ ```
374
+
375
+ ---
376
+
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 |
392
+
393
+ ---
394
+
395
+ ## Customization
396
+
397
+ ### Locale and labels
398
+
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"`.
400
+
401
+ ```ts
402
+ const config = {
403
+ endpoint: "https://api.wecool.dev",
404
+ apiKey: "...",
405
+ tools,
406
+ locale: "en", // UI labels in English
407
+ placeholder: "Ask me anything...", // override only the input placeholder
408
+ };
409
+ ```
410
+
411
+ If `placeholder` is provided, it takes precedence over the locale's default placeholder.
412
+
413
+ ### Colors / theme
414
+
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
420
+
421
+ ---
422
+
423
+ ## Event Callbacks
424
+
425
+ You can hook into each phase of tool execution to add your own logic.
426
+
427
+ ```ts
428
+ const config = {
429
+ endpoint: "https://api.wecool.dev",
430
+ apiKey: "...",
431
+ tools,
432
+ onToolExecute: (name, args) => {
433
+ console.log("[wecool] executing", name, args);
434
+ },
435
+ onToolResult: (name, result) => {
436
+ // Audit log, Sentry breadcrumb, UI toast, etc.
437
+ auditLog({ tool: name, result });
438
+ },
439
+ onError: (error) => {
440
+ Sentry.captureException(error);
441
+ },
442
+ };
443
+ ```
444
+
445
+ Heavy work inside callbacks can block chat rendering. Wrap long-running work in `setTimeout` or similar to keep it asynchronous.
446
+
447
+ ---
448
+
449
+ ## Building Your Own UI (ChatClient)
450
+
451
+ If you want to build a custom UI instead of the bundled one, use `ChatClient` directly to consume server responses incrementally.
452
+
453
+ ```ts
454
+ import { ChatClient, createUserMessage } from "wecool-chat";
455
+
456
+ const client = new ChatClient("https://api.wecool.dev", "<api-key>");
457
+
458
+ const history = [createUserMessage("switch to dark mode")];
459
+
460
+ for await (const event of client.chat({
461
+ message: "switch to dark mode",
462
+ tools: [
463
+ /* ToolDefinition[] */
464
+ ],
465
+ history,
466
+ })) {
467
+ switch (event.type) {
468
+ case "text_delta":
469
+ process.stdout.write(event.delta);
470
+ break;
471
+ case "tool_use":
472
+ console.log("tool:", event.name, event.input);
473
+ break;
474
+ case "done":
475
+ console.log("conversation:", event.conversationId);
476
+ break;
477
+ }
478
+ }
479
+ ```
480
+
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>`.
484
+
485
+ Helper functions are also provided:
486
+
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
493
+
494
+ ---
495
+
496
+ ## Using with TypeScript
497
+
498
+ Type definitions are bundled, so no extra setup is required.
499
+
500
+ ```ts
501
+ import type {
502
+ Tool,
503
+ ToolDefinition,
504
+ ToolParameters,
505
+ ChatConfig,
506
+ ChatWidgetProps,
507
+ Message,
508
+ ToolCall,
509
+ ChatEvent,
510
+ ToolResult,
511
+ } from "wecool-chat";
512
+ ```
513
+
514
+ ---
515
+
516
+ ## Troubleshooting
74
517
 
75
- ### ChatWidget Props
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`.
76
520
 
77
- | Prop | 型 | 必須 | 説明 |
78
- | ----------- | ------------------------------- | ------ | ----------------------------------------- |
79
- | config | ChatConfig | はい | 設定オブジェクト |
80
- | floating | boolean | いいえ | フローティングモード(デフォルト: false) |
81
- | position | 'bottom-right' \| 'bottom-left' | いいえ | フローティング時の表示位置 |
82
- | defaultOpen | boolean | いいえ | 初期表示状態(デフォルト: false) |
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.
83
523
 
84
- ### ChatConfig
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.
85
526
 
86
- | プロパティ | 型 | 必須 | 説明 |
87
- | ------------- | -------- | ------ | ------------------------------ |
88
- | endpoint | string | はい | APIエンドポイントURL |
89
- | apiKey | string | いいえ | APIキー |
90
- | tools | Tool[] | はい | ツール定義の配列 |
91
- | locale | string | いいえ | UIテキストのロケール |
92
- | placeholder | string | いいえ | 入力欄のプレースホルダー |
93
- | onToolExecute | function | いいえ | ツール実行時のコールバック |
94
- | onToolResult | function | いいえ | ツール結果返却時のコールバック |
95
- | onError | function | いいえ | エラー時のコールバック |
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.
96
529
 
97
- ### Tool 定義
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.
98
532
 
99
- | プロパティ | 型 | 必須 | 説明 |
100
- | ----------- | -------- | ---- | ------------------------- |
101
- | name | string | はい | ツールの一意な識別子 |
102
- | description | string | はい | ツールの説明(LLMが参照) |
103
- | parameters | object | はい | パラメータのJSON Schema |
104
- | handler | function | はい | 実行する関数 |
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.
105
535
 
106
- ## デモ
536
+ ---
107
537
 
108
- ライブデモ: [demo.wecool.dev](https://demo.wecool.dev)
538
+ ## Changelog / License
109
539
 
110
- ## ライセンス
540
+ See [CHANGELOG.md](./CHANGELOG.md) for the changelog.
111
541
 
112
- MIT
542
+ License: MIT
package/dist/client.d.ts CHANGED
@@ -34,6 +34,11 @@ export declare function createUserMessage(content: string): Message;
34
34
  * Create an assistant message
35
35
  */
36
36
  export declare function createAssistantMessage(content: string): Message;
37
+ /**
38
+ * Create an assistant message that represents an error so it can live inline
39
+ * in the message list and be regenerated from the same UI affordance.
40
+ */
41
+ export declare function createErrorMessage(content: string): Message;
37
42
  /**
38
43
  * Create a tool result message
39
44
  */
@@ -1 +1 @@
1
- {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAG9E,KAAK,UAAU,GAAG;IAChB,OAAO,CAAC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,wBAAgB,UAAU,CAAC,EACzB,QAAQ,EACR,OAAO,EACP,QAAQ,EACR,SAAS,EACT,WAAW,GACZ,EAAE,eAAe,2CA0FjB"}
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":"AAGA,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,2CAYzE"}
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"}
@@ -2,14 +2,15 @@ import { Message } from '../types';
2
2
  type ChatPanelProps = {
3
3
  messages: Message[];
4
4
  isLoading: boolean;
5
- error: string | null;
6
5
  onSendMessage: (message: string) => void;
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, error, onSendMessage, 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,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,aAAa,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;IACzC,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,KAAK,EACL,aAAa,EACb,QAAQ,EACR,OAAO,EACP,OAAO,EACP,SAAiB,EACjB,WAAW,GACZ,EAAE,cAAc,2CAehB"}
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;AA4BtE,wBAAgB,UAAU,CAAC,EACzB,MAAM,EACN,KAAK,EAAE,SAAS,EAChB,QAAgB,EAChB,WAAmB,EACnB,QAAyB,GAC1B,EAAE,eAAe,2CAwDjB"}
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":"AAIA,KAAK,kBAAkB,GAAG;IACxB,MAAM,EAAE,OAAO,CAAC;CACjB,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,aAAa,EAAE,KAAK,CAAC,EAAE,CAAC,kBAAkB,CAStD,CAAC"}
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":"AAEA,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,2CAUhE"}
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"}