wecool-chat 0.5.0 → 0.7.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 (202) hide show
  1. package/CHANGELOG.md +96 -1
  2. package/README.ja.md +354 -30
  3. package/README.md +359 -30
  4. package/dist/_commonjsHelpers-C6fGbg64.js +6 -0
  5. package/dist/browser-beO7dF90.js +1538 -0
  6. package/dist/core/cancellation-log.d.ts.map +1 -0
  7. package/dist/{client.d.ts → core/client.d.ts} +15 -3
  8. package/dist/core/client.d.ts.map +1 -0
  9. package/dist/{hooks → core}/consume-stream.d.ts +9 -5
  10. package/dist/core/consume-stream.d.ts.map +1 -0
  11. package/dist/{i18n → core/i18n}/messages.d.ts +9 -0
  12. package/dist/core/i18n/messages.d.ts.map +1 -0
  13. package/dist/core/index.d.ts +12 -0
  14. package/dist/core/index.d.ts.map +1 -0
  15. package/dist/core/run-conversation.d.ts +38 -0
  16. package/dist/core/run-conversation.d.ts.map +1 -0
  17. package/dist/core/sse-parser.d.ts.map +1 -0
  18. package/dist/{hooks → core}/stream-state.d.ts +14 -11
  19. package/dist/core/stream-state.d.ts.map +1 -0
  20. package/dist/{hooks → core}/tool-runner.d.ts +4 -5
  21. package/dist/core/tool-runner.d.ts.map +1 -0
  22. package/dist/core/types.d.ts +248 -0
  23. package/dist/core/types.d.ts.map +1 -0
  24. package/dist/index.css +1 -1
  25. package/dist/index.d.ts +32 -13
  26. package/dist/index.d.ts.map +1 -1
  27. package/dist/index.js +11481 -10535
  28. package/dist/plugin/context.d.ts +15 -0
  29. package/dist/plugin/context.d.ts.map +1 -0
  30. package/dist/plugin/define.d.ts +30 -0
  31. package/dist/plugin/define.d.ts.map +1 -0
  32. package/dist/plugin/id.d.ts +19 -0
  33. package/dist/plugin/id.d.ts.map +1 -0
  34. package/dist/plugin/index.d.ts +21 -0
  35. package/dist/plugin/index.d.ts.map +1 -0
  36. package/dist/plugin/logger.d.ts +11 -0
  37. package/dist/plugin/logger.d.ts.map +1 -0
  38. package/dist/plugin/registry.d.ts +21 -0
  39. package/dist/plugin/registry.d.ts.map +1 -0
  40. package/dist/plugin/types.d.ts +109 -0
  41. package/dist/plugin/types.d.ts.map +1 -0
  42. package/dist/plugin.d.ts +2 -0
  43. package/dist/plugin.js +4 -0
  44. package/dist/plugins/qrcode/__tests__/format-result.test.d.ts +2 -0
  45. package/dist/plugins/qrcode/__tests__/format-result.test.d.ts.map +1 -0
  46. package/dist/plugins/qrcode/__tests__/index.test.d.ts +2 -0
  47. package/dist/plugins/qrcode/__tests__/index.test.d.ts.map +1 -0
  48. package/dist/plugins/qrcode/__tests__/tool.test.d.ts +2 -0
  49. package/dist/plugins/qrcode/__tests__/tool.test.d.ts.map +1 -0
  50. package/dist/plugins/qrcode/format-result.d.ts +59 -0
  51. package/dist/plugins/qrcode/format-result.d.ts.map +1 -0
  52. package/dist/plugins/qrcode/index.d.ts +23 -0
  53. package/dist/plugins/qrcode/index.d.ts.map +1 -0
  54. package/dist/plugins/qrcode/tool.d.ts +50 -0
  55. package/dist/plugins/qrcode/tool.d.ts.map +1 -0
  56. package/dist/plugins/qrcode.d.ts +2 -0
  57. package/dist/plugins/qrcode.js +161 -0
  58. package/dist/ui/builtin-tools/get-current-page-info.d.ts +9 -0
  59. package/dist/ui/builtin-tools/get-current-page-info.d.ts.map +1 -0
  60. package/dist/ui/builtin-tools/index.d.ts +3 -0
  61. package/dist/ui/builtin-tools/index.d.ts.map +1 -0
  62. package/dist/ui/builtin-tools/merge-builtin-tools.d.ts +10 -0
  63. package/dist/ui/builtin-tools/merge-builtin-tools.d.ts.map +1 -0
  64. package/dist/ui/components/ChatFooter.d.ts +12 -0
  65. package/dist/ui/components/ChatFooter.d.ts.map +1 -0
  66. package/dist/ui/components/ChatHeader.d.ts +15 -0
  67. package/dist/ui/components/ChatHeader.d.ts.map +1 -0
  68. package/dist/ui/components/ChatHeaderMenu.d.ts +12 -0
  69. package/dist/ui/components/ChatHeaderMenu.d.ts.map +1 -0
  70. package/dist/ui/components/ChatPanel.d.ts +29 -0
  71. package/dist/ui/components/ChatPanel.d.ts.map +1 -0
  72. package/dist/ui/components/ChatWidget.d.ts +3 -0
  73. package/dist/ui/components/ChatWidget.d.ts.map +1 -0
  74. package/dist/{components → ui/components}/CopyIndicator.d.ts +1 -1
  75. package/dist/ui/components/CopyIndicator.d.ts.map +1 -0
  76. package/dist/ui/components/FloatButton.d.ts +13 -0
  77. package/dist/ui/components/FloatButton.d.ts.map +1 -0
  78. package/dist/{components → ui/components}/MessageActionsPopover.d.ts +3 -1
  79. package/dist/ui/components/MessageActionsPopover.d.ts.map +1 -0
  80. package/dist/ui/components/MessageAttachments.d.ts +21 -0
  81. package/dist/ui/components/MessageAttachments.d.ts.map +1 -0
  82. package/dist/ui/components/MessageBubble.d.ts +24 -0
  83. package/dist/ui/components/MessageBubble.d.ts.map +1 -0
  84. package/dist/{components → ui/components}/MessageCopyButton.d.ts +3 -1
  85. package/dist/ui/components/MessageCopyButton.d.ts.map +1 -0
  86. package/dist/{components → ui/components}/MessageEditor.d.ts +1 -1
  87. package/dist/ui/components/MessageEditor.d.ts.map +1 -0
  88. package/dist/{components → ui/components}/MessageList.d.ts +1 -1
  89. package/dist/ui/components/MessageList.d.ts.map +1 -0
  90. package/dist/{components → ui/components}/MessagePopoverButton.d.ts +1 -1
  91. package/dist/ui/components/MessagePopoverButton.d.ts.map +1 -0
  92. package/dist/ui/components/MessageRow.d.ts +18 -0
  93. package/dist/ui/components/MessageRow.d.ts.map +1 -0
  94. package/dist/{components → ui/components}/MessageRowActions.d.ts +2 -2
  95. package/dist/ui/components/MessageRowActions.d.ts.map +1 -0
  96. package/dist/{components → ui/components}/markdown/CodeBlock.d.ts +1 -1
  97. package/dist/ui/components/markdown/CodeBlock.d.ts.map +1 -0
  98. package/dist/{components → ui/components}/markdown/MarkdownMessage.d.ts +5 -1
  99. package/dist/ui/components/markdown/MarkdownMessage.d.ts.map +1 -0
  100. package/dist/ui/components/markdown/sanitize-clipboard.d.ts.map +1 -0
  101. package/dist/ui/components/markdown/stable-prefix.d.ts.map +1 -0
  102. package/dist/ui/components/markdown/strip-data-images.d.ts +2 -0
  103. package/dist/ui/components/markdown/strip-data-images.d.ts.map +1 -0
  104. package/dist/ui/components/markdown/url-transform.d.ts.map +1 -0
  105. package/dist/ui/components/message-row-handlers.d.ts.map +1 -0
  106. package/dist/ui/hooks/use-editing-message.d.ts.map +1 -0
  107. package/dist/ui/hooks/use-menu-keyboard.d.ts +37 -0
  108. package/dist/ui/hooks/use-menu-keyboard.d.ts.map +1 -0
  109. package/dist/ui/hooks/use-message-history.d.ts +20 -0
  110. package/dist/ui/hooks/use-message-history.d.ts.map +1 -0
  111. package/dist/{hooks → ui/hooks}/use-send-message.d.ts +8 -14
  112. package/dist/ui/hooks/use-send-message.d.ts.map +1 -0
  113. package/dist/ui/hooks/use-stream-lifecycle.d.ts.map +1 -0
  114. package/dist/{hooks → ui/hooks}/use-tool-runner.d.ts +2 -3
  115. package/dist/ui/hooks/use-tool-runner.d.ts.map +1 -0
  116. package/dist/ui/hooks/useActiveMessage.d.ts.map +1 -0
  117. package/dist/{hooks → ui/hooks}/useChatStream.d.ts +5 -3
  118. package/dist/ui/hooks/useChatStream.d.ts.map +1 -0
  119. package/dist/ui/hooks/useCopyToClipboard.d.ts.map +1 -0
  120. package/dist/ui/hooks/useDismissOnOutside.d.ts.map +1 -0
  121. package/dist/ui/hooks/useDraggablePanel.d.ts +32 -0
  122. package/dist/ui/hooks/useDraggablePanel.d.ts.map +1 -0
  123. package/dist/ui/hooks/useFocusTrap.d.ts +28 -0
  124. package/dist/ui/hooks/useFocusTrap.d.ts.map +1 -0
  125. package/dist/ui/hooks/useMediaQuery.d.ts +17 -0
  126. package/dist/ui/hooks/useMediaQuery.d.ts.map +1 -0
  127. package/dist/ui/hooks/useScrollToBottom.d.ts +22 -0
  128. package/dist/ui/hooks/useScrollToBottom.d.ts.map +1 -0
  129. package/dist/ui/i18n/format-time.d.ts +10 -0
  130. package/dist/ui/i18n/format-time.d.ts.map +1 -0
  131. package/dist/ui/i18n/locale-context.d.ts +10 -0
  132. package/dist/ui/i18n/locale-context.d.ts.map +1 -0
  133. package/dist/{i18n → ui/i18n}/messages-context.d.ts +1 -1
  134. package/dist/ui/i18n/messages-context.d.ts.map +1 -0
  135. package/dist/ui/mount.d.ts +19 -0
  136. package/dist/ui/mount.d.ts.map +1 -0
  137. package/dist/ui/styles/index.d.ts.map +1 -0
  138. package/dist/ui/types.d.ts +42 -0
  139. package/dist/ui/types.d.ts.map +1 -0
  140. package/package.json +39 -17
  141. package/dist/client.d.ts.map +0 -1
  142. package/dist/components/ChatFooter.d.ts +0 -10
  143. package/dist/components/ChatFooter.d.ts.map +0 -1
  144. package/dist/components/ChatHeader.d.ts +0 -7
  145. package/dist/components/ChatHeader.d.ts.map +0 -1
  146. package/dist/components/ChatPanel.d.ts +0 -16
  147. package/dist/components/ChatPanel.d.ts.map +0 -1
  148. package/dist/components/ChatWidget.d.ts +0 -3
  149. package/dist/components/ChatWidget.d.ts.map +0 -1
  150. package/dist/components/CopyIndicator.d.ts.map +0 -1
  151. package/dist/components/FloatButton.d.ts +0 -7
  152. package/dist/components/FloatButton.d.ts.map +0 -1
  153. package/dist/components/MessageActionsPopover.d.ts.map +0 -1
  154. package/dist/components/MessageBubble.d.ts +0 -15
  155. package/dist/components/MessageBubble.d.ts.map +0 -1
  156. package/dist/components/MessageCopyButton.d.ts.map +0 -1
  157. package/dist/components/MessageEditor.d.ts.map +0 -1
  158. package/dist/components/MessageList.d.ts.map +0 -1
  159. package/dist/components/MessagePopoverButton.d.ts.map +0 -1
  160. package/dist/components/MessageRow.d.ts +0 -17
  161. package/dist/components/MessageRow.d.ts.map +0 -1
  162. package/dist/components/MessageRowActions.d.ts.map +0 -1
  163. package/dist/components/markdown/CodeBlock.d.ts.map +0 -1
  164. package/dist/components/markdown/MarkdownMessage.d.ts.map +0 -1
  165. package/dist/components/markdown/sanitize-clipboard.d.ts.map +0 -1
  166. package/dist/components/markdown/stable-prefix.d.ts.map +0 -1
  167. package/dist/components/markdown/url-transform.d.ts.map +0 -1
  168. package/dist/components/message-row-handlers.d.ts.map +0 -1
  169. package/dist/hooks/cancellation-log.d.ts.map +0 -1
  170. package/dist/hooks/consume-stream.d.ts.map +0 -1
  171. package/dist/hooks/run-conversation.d.ts +0 -23
  172. package/dist/hooks/run-conversation.d.ts.map +0 -1
  173. package/dist/hooks/stream-state.d.ts.map +0 -1
  174. package/dist/hooks/tool-runner.d.ts.map +0 -1
  175. package/dist/hooks/use-editing-message.d.ts.map +0 -1
  176. package/dist/hooks/use-send-message.d.ts.map +0 -1
  177. package/dist/hooks/use-stream-lifecycle.d.ts.map +0 -1
  178. package/dist/hooks/use-tool-runner.d.ts.map +0 -1
  179. package/dist/hooks/useActiveMessage.d.ts.map +0 -1
  180. package/dist/hooks/useChatStream.d.ts.map +0 -1
  181. package/dist/hooks/useCopyToClipboard.d.ts.map +0 -1
  182. package/dist/hooks/useDismissOnOutside.d.ts.map +0 -1
  183. package/dist/hooks/useScrollToBottom.d.ts +0 -7
  184. package/dist/hooks/useScrollToBottom.d.ts.map +0 -1
  185. package/dist/i18n/messages-context.d.ts.map +0 -1
  186. package/dist/i18n/messages.d.ts.map +0 -1
  187. package/dist/sse-parser.d.ts.map +0 -1
  188. package/dist/styles/index.d.ts.map +0 -1
  189. package/dist/types.d.ts +0 -138
  190. package/dist/types.d.ts.map +0 -1
  191. /package/dist/{hooks → core}/cancellation-log.d.ts +0 -0
  192. /package/dist/{sse-parser.d.ts → core/sse-parser.d.ts} +0 -0
  193. /package/dist/{components → ui/components}/markdown/sanitize-clipboard.d.ts +0 -0
  194. /package/dist/{components → ui/components}/markdown/stable-prefix.d.ts +0 -0
  195. /package/dist/{components → ui/components}/markdown/url-transform.d.ts +0 -0
  196. /package/dist/{components → ui/components}/message-row-handlers.d.ts +0 -0
  197. /package/dist/{hooks → ui/hooks}/use-editing-message.d.ts +0 -0
  198. /package/dist/{hooks → ui/hooks}/use-stream-lifecycle.d.ts +0 -0
  199. /package/dist/{hooks → ui/hooks}/useActiveMessage.d.ts +0 -0
  200. /package/dist/{hooks → ui/hooks}/useCopyToClipboard.d.ts +0 -0
  201. /package/dist/{hooks → ui/hooks}/useDismissOnOutside.d.ts +0 -0
  202. /package/dist/{styles → ui/styles}/index.d.ts +0 -0
package/README.md CHANGED
@@ -11,9 +11,11 @@
11
11
 
12
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.
13
13
 
14
- ![hero demo](./docs/screenshots/hero.gif)
14
+ ![hero demo](https://unpkg.com/wecool-chat@latest/docs/screenshots/hero.gif)
15
15
 
16
- [Live Demo](https://demo.wecool.dev) ・ [Dashboard](https://app.wecool.dev) ・ [npm](https://www.npmjs.com/package/wecool-chat)
16
+ [Live Demo](https://demo.wecool.dev) ・ [LLM-driven Dining demo](https://demo.wecool.dev/demo/living-thread/dining) ・ [Dashboard](https://app.wecool.dev) ・ [npm](https://www.npmjs.com/package/wecool-chat)
17
+
18
+ > 🍝 **Try the conversational order flow** at [demo.wecool.dev/demo/living-thread/dining](https://demo.wecool.dev/demo/living-thread/dining) — type a request, and the model runs 7 tools in sequence (pick dishes, set quantity, allergens, confirm). Built on top of `layout: "input-only"` + `narrateToolResults`.
17
19
 
18
20
  ---
19
21
 
@@ -108,28 +110,109 @@ Since the API key is visible to the client, **restrict the calling origin** in t
108
110
 
109
111
  ---
110
112
 
111
- ## Display Modes
113
+ ## Display Modes (Layout Patterns)
112
114
 
113
- The widget has two display modes.
115
+ wecool-chat supports nine official layout patterns. Choose one with the `layout` option on `ChatConfig`.
114
116
 
115
- ### Floating (recommended)
117
+ ### Pattern matrix
116
118
 
117
- A persistent chat button anchored to a corner of the screen. Clicking it opens the panel. Best for support and tour-like experiences.
119
+ | `layout` value | Where it sits | Best for |
120
+ | ----------------------- | ------------------------- | --------------------------------------------------------------------------------------------------- |
121
+ | `floating-bottom-right` | Bottom-right corner | Classic chat bot (default if `layout` is omitted) |
122
+ | `floating-bottom-left` | Bottom-left corner | Same as above but on the left |
123
+ | `floating-top-right` | Top-right corner | When the bottom-right is occupied by other UI |
124
+ | `floating-top-left` | Top-left corner | Same as above on the left |
125
+ | `sidebar-left` | Left edge, ~360px wide | Always-on assistant (e.g. learning, documentation) |
126
+ | `sidebar-right` | Right edge, ~360px wide | Settings / help |
127
+ | `header-banner` | Top edge, ~80px tall | Concierge-style (e.g. travel booking). `transparent` overlay |
128
+ | `footer-fixed` | Bottom edge, full width | Always-visible small chat dock |
129
+ | `inline-custom` | Wherever your app puts it | Fully custom layout (Shadow DOM is disabled; you own the CSS) |
130
+ | `input-only` | Wherever your app puts it | Input field only — host renders messages in its own UI (see "Headless input + custom thread" below) |
118
131
 
119
- ![floating mode](./docs/screenshots/mode-floating.png)
132
+ ### Switching layout
120
133
 
121
- ### Embedded
134
+ ```ts
135
+ import { initWecoolChat } from "wecool-chat";
122
136
 
123
- Place the panel directly inside any container element. Best for embedding as an assistant section within a dashboard.
137
+ initWecoolChat({
138
+ endpoint: "https://your-api.example.com",
139
+ apiKey: "wlt_...",
140
+ tools: [],
141
+ layout: "sidebar-left",
142
+ });
143
+ ```
124
144
 
125
- ### How to switch
145
+ For `header-banner`, you can add `transparent: true` to overlay the banner on the page content with a translucent background.
126
146
 
127
- | Use case | React | Non-React |
128
- | -------- | -------------------------------------- | ------------------------------------------- |
129
- | Floating | `<ChatWidget config={...} floating />` | `initWecoolChat(config)` |
130
- | Embedded | `<ChatWidget config={...} />` | `initWecoolChat(config, "#chat-container")` |
147
+ ### React equivalent
131
148
 
132
- `initWecoolChat` is floating when the second argument is omitted, and embedded when an `HTMLElement` or CSS selector is passed.
149
+ When you use `<ChatWidget />` directly, pass `floating` / `position` as before; the `layout` field is consumed by `initWecoolChat` and translates to those props for you. The dedicated `sidebar-*` / `header-banner` / `footer-fixed` patterns are only available through `initWecoolChat` because they need a wrapper element on the host DOM.
150
+
151
+ ### Choosing `inline-custom`
152
+
153
+ Use this when you want the chat input/UI to be a building block inside your own layout. wecool will mount directly into the container you pass without Shadow DOM, so:
154
+
155
+ - Your site's CSS is in scope (style it however you like)
156
+ - You are responsible for positioning / sizing the container element
157
+
158
+ ```ts
159
+ initWecoolChat({ endpoint, apiKey, tools, layout: "inline-custom" }, "#my-chat-slot");
160
+ ```
161
+
162
+ ### Headless input + custom thread (`input-only`)
163
+
164
+ When you want the **message list to live in your own UI** — for example, when chat utterances, AI replies, and tool-driven UI components must interleave in a single timeline your host page renders — use `layout: "input-only"`. wecool-chat mounts **only the input dock** and streams `messages` to your callback so you can render them however you like.
165
+
166
+ ```ts
167
+ const controller = initWecoolChat(
168
+ {
169
+ endpoint: "https://your-api.example.com",
170
+ apiKey: "wlt_...",
171
+ tools,
172
+ layout: "input-only",
173
+ onMessagesChange: (messages) => {
174
+ // messages is the full conversation array kept in wecool-chat.
175
+ // Push new ones into your own UI store / thread.
176
+ hostStore.syncMessages(messages);
177
+ },
178
+ },
179
+ "#wecool-input-dock"
180
+ );
181
+
182
+ // You can drive the chat from your own UI too:
183
+ controller.sendMessage("Hello");
184
+ controller.editMessage("msg-123", "Hello (edited)");
185
+ controller.regenerate();
186
+ controller.clear();
187
+
188
+ // And `controller` itself is callable — same as before — to tear down:
189
+ controller(); // unmount
190
+ ```
191
+
192
+ This pairs nicely with `narrateToolResults` (see below) when you want the model to send a short acknowledgement after every tool call.
193
+
194
+ ### Controller API (return value of `initWecoolChat`)
195
+
196
+ `initWecoolChat` returns a **callable controller object**. Calling it unmounts the widget (back-compat with the previous `() => void` shape). On top of that, it exposes methods for driving the chat from outside the widget — useful for `input-only` mode, IME-aware editors, regenerate buttons, or "reset" controls placed on the host page.
197
+
198
+ | Method | What it does |
199
+ | ------------------------------------- | --------------------------------------------------------------------------- |
200
+ | `controller()` | Unmount the widget (cleanup). Same as the old return type. |
201
+ | `controller.sendMessage(s)` | Send a message programmatically, as if the user typed it. |
202
+ | `controller.editMessage(id, content)` | Replace the content of an existing user message and re-run from that point. |
203
+ | `controller.regenerate()` | Drop the last assistant turn and ask the model again. |
204
+ | `controller.clear()` | Clear all messages. |
205
+
206
+ The controller is the same object whether you mount in floating, sidebar, or input-only mode.
207
+
208
+ ### Backwards compatibility
209
+
210
+ `layout` is optional. If omitted, wecool-chat behaves exactly like before:
211
+
212
+ - `initWecoolChat(config)` → floating at bottom-right
213
+ - `initWecoolChat(config, "#container")` → embedded (Shadow DOM) in the container
214
+
215
+ The return value is callable, so existing code that does `const cleanup = initWecoolChat(...); cleanup()` keeps working without change.
133
216
 
134
217
  ---
135
218
 
@@ -220,6 +303,193 @@ const config = {
220
303
 
221
304
  > **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
305
 
306
+ ### Built-in tools
307
+
308
+ wecool-chat ships ready-to-use tools that are **registered automatically**. You don't need to import or add them to the `tools` array — `ChatWidget` injects them for you.
309
+
310
+ | Tool | What it does |
311
+ | -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ |
312
+ | `getCurrentPageInfo` | Returns the URL / title / description / pathname / host / language of the embedding page so the LLM can answer "what page am I looking at?" style questions. |
313
+
314
+ ```ts
315
+ import { initWecoolChat } from "wecool-chat";
316
+
317
+ // getCurrentPageInfo is included automatically — no manual registration needed.
318
+ initWecoolChat({
319
+ endpoint: "https://api.example.com",
320
+ tools: [
321
+ myCustomTool, // only your own tools here
322
+ ],
323
+ });
324
+ ```
325
+
326
+ A `web_search` tool is also registered on the backend and offered to the LLM automatically — you don't need to add anything client-side. Note that backend `web_search` answers are produced from the LLM's own knowledge rather than a live web crawl, so treat the results as best-effort.
327
+
328
+ #### Overriding or disabling a built-in tool
329
+
330
+ To disable or replace a built-in tool, register a `Tool` with the same `name` — your tool wins.
331
+
332
+ ```ts
333
+ initWecoolChat({
334
+ endpoint: "https://api.example.com",
335
+ tools: [
336
+ {
337
+ name: "getCurrentPageInfo", // same name disables the built-in
338
+ description: "disabled",
339
+ parameters: { type: "object", properties: {} },
340
+ handler: () => ({ disabled: true }),
341
+ },
342
+ ],
343
+ });
344
+ ```
345
+
346
+ > ⚠️ **Security note for `getCurrentPageInfo`**: The handler exposes the full `window.location.href`, including query parameters and hash. On pages whose URLs carry secrets (password-reset tokens, OAuth `access_token` in the fragment, signed download links, etc.) override the built-in with a no-op tool as shown above, since the original value would otherwise reach the LLM provider as part of the tool result.
347
+
348
+ ---
349
+
350
+ ## Plugins
351
+
352
+ Plugins package one or more tools (and any related cleanup) into a single reusable unit that can be published to npm. Third parties can ship `wecool-chat/plugin` plugins via `peerDependencies`; consumers just `npm install` and pass the plugin via `config.plugins`.
353
+
354
+ ### Authoring a plugin
355
+
356
+ ```ts
357
+ // my-plugin/src/index.ts
358
+ import { definePlugin } from "wecool-chat/plugin";
359
+
360
+ export default definePlugin({
361
+ pluginApiVersion: 1,
362
+ id: "acme/clock", // "scope/name" (lowercase, hyphen, npm-style)
363
+ version: "0.1.0",
364
+ setup(ctx) {
365
+ ctx.registerTool({
366
+ name: "get_current_time",
367
+ description: "Return the current ISO timestamp",
368
+ parameters: { type: "object", properties: {} },
369
+ handler: (_args, invocation) => {
370
+ // invocation.signal, invocation.locale, invocation.logger are available
371
+ return { now: new Date().toISOString() };
372
+ },
373
+ });
374
+ ctx.onCleanup(() => ctx.logger.info("clock plugin disposed"));
375
+ },
376
+ });
377
+ ```
378
+
379
+ ### Consuming a plugin
380
+
381
+ ```tsx
382
+ import { initWecoolChat } from "wecool-chat";
383
+ import clockPlugin from "@acme/wecool-plugin-clock";
384
+
385
+ initWecoolChat({
386
+ endpoint: "https://api.example.com/chat",
387
+ tools: [],
388
+ plugins: [clockPlugin],
389
+ });
390
+ ```
391
+
392
+ ### What `PluginContext` provides
393
+
394
+ | API | Purpose |
395
+ | -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
396
+ | `registerTool(tool)` | Register a tool. Same-plugin duplicates error, cross-plugin duplicates last-win with a warning. Pass `<TArgs, TResult>` generics for typed args. |
397
+ | `onCleanup(fn)` | Run `fn` on widget unmount (LIFO order). Use for `removeEventListener`, `clearInterval`, etc. |
398
+ | `logger` | Scoped logger (`[wecool-chat:<plugin-id>]` prefix). Use instead of `console.*` so output is identifiable. |
399
+ | `disposeSignal` | Plugin-lifetime `AbortSignal`. Aborts on widget unmount. Pass to long-running `fetch`. |
400
+ | `locale`, `pluginId` | Read-only context info for i18n / diagnostics. |
401
+
402
+ `PluginToolInvocation` (the second arg of `handler`) provides `signal` (per-tool-call), `logger`, and `locale`.
403
+
404
+ ### Tool resolution order
405
+
406
+ When the same tool name appears in multiple sources, the last one wins:
407
+
408
+ ```
409
+ builtin tools < plugin tools (declared order) < config.tools (user-provided)
410
+ ```
411
+
412
+ So `config.tools` always overrides a plugin tool of the same name. This lets host apps locally override a behavior without forking the plugin.
413
+
414
+ ### Publishing a plugin to npm
415
+
416
+ ```json
417
+ {
418
+ "name": "@acme/wecool-plugin-clock",
419
+ "version": "0.1.0",
420
+ "type": "module",
421
+ "main": "./dist/index.js",
422
+ "types": "./dist/index.d.ts",
423
+ "files": ["dist"],
424
+ "peerDependencies": { "wecool-chat": "^0.7.0" },
425
+ "publishConfig": { "access": "public" }
426
+ }
427
+ ```
428
+
429
+ - Import only from `wecool-chat/plugin` (not from `wecool-chat`). The subpath entry ships just the types and `definePlugin` helper (~90 bytes gzipped) and does not pull in React.
430
+ - Declare `wecool-chat` as a `peerDependency` so the host app's copy is reused.
431
+
432
+ ### Compatibility (`pluginApiVersion`)
433
+
434
+ `pluginApiVersion: 1` is **required**. It is an integer that bumps only when the plugin SDK contract is broken, and is **independent of `wecool-chat`'s own semver**.
435
+
436
+ A plugin declared with `pluginApiVersion: 1` is guaranteed to keep loading on any future `wecool-chat` 0.7.x / 0.8.x / 1.x release as long as the SDK contract version stays at 1. When mismatched, the plugin is skipped with a warning.
437
+
438
+ ### Built-in official plugins
439
+
440
+ Plugins under `wecool-chat/plugins/*` are shipped with the package itself. They are not bundled in the core entry — import the subpath explicitly to pull them in.
441
+
442
+ #### `wecool-chat/plugins/qrcode`
443
+
444
+ Generate scannable QR codes from arbitrary text — URL, Wi-Fi config, vCard, geo-location, and so on. The LLM decides what string to encode.
445
+
446
+ ```tsx
447
+ import { initWecoolChat } from "wecool-chat";
448
+ import { createQrcodePlugin } from "wecool-chat/plugins/qrcode";
449
+
450
+ initWecoolChat({
451
+ endpoint: "...",
452
+ tools: [],
453
+ plugins: [createQrcodePlugin()],
454
+ });
455
+ ```
456
+
457
+ The plugin registers a single tool `generate_qrcode` that the LLM calls:
458
+
459
+ | User says | LLM call |
460
+ | --------------------------------------- | ----------------------------------------------------------------- |
461
+ | "Make a QR for this URL" | `generate_qrcode({ text: url })` |
462
+ | "Show a QR for our Wi-Fi" | `generate_qrcode({ text: "WIFI:T:WPA;S:ssid;P:pw;;" })` |
463
+ | "QR for my contact card (printed)" | `generate_qrcode({ text: "<vCard>", errorCorrectionLevel: "Q" })` |
464
+ | "QR for the office address, large size" | `generate_qrcode({ text: "geo:35.6,139.7", size: 384 })` |
465
+
466
+ The QR image is rendered directly in the chat bubble together with a download button. The handler **does not** trigger a download itself — the user controls when (and whether) to save the image.
467
+
468
+ `createQrcodePlugin` options:
469
+
470
+ | Option | Default | Notes |
471
+ | ----------------------------- | ------- | ------------------------------------------------------ |
472
+ | `defaultSize` | `256` | QR side length in px |
473
+ | `defaultErrorCorrectionLevel` | `"M"` | Use `"Q"` or `"H"` for print scenarios |
474
+ | `maxTextLength` | `2000` | Set to `Number.POSITIVE_INFINITY` to disable the guard |
475
+
476
+ **Result shape passed to the LLM** (the image bytes themselves are kept out of the LLM context — they ride a separate `attachments` channel and are rendered client-side only):
477
+
478
+ ```ts
479
+ // success
480
+ { ok: true, size, errorCorrectionLevel, contentLength,
481
+ mimeType: "image/png", displayed: true }
482
+
483
+ // failure (LLM can retry based on `error` + `hint`)
484
+ { ok: false, error: "text_too_long" | "invalid_args"
485
+ | "browser_only" | "generation_failed" | "unsafe_content",
486
+ message, hint? }
487
+ ```
488
+
489
+ Cancellation only throws `AbortError` — every other failure is returned as `{ ok: false, ... }` so the LLM can decide whether to retry (e.g. lower `errorCorrectionLevel`, shorten the text).
490
+
491
+ **Bundle impact**: the `qrcode` library lives inside `dist/plugins/qrcode.js` and its associated chunks under `dist/` (~14 KB gzipped in total). The core widget bundle (`dist/index.js`) does **not** pull it in. The plugin code itself loads `qrcode` only when the tool is actually invoked.
492
+
223
493
  ---
224
494
 
225
495
  ## Framework Guides
@@ -376,19 +646,48 @@ const ChatWidget = dynamic(() => import("wecool-chat").then((m) => m.ChatWidget)
376
646
 
377
647
  ## ChatConfig Options
378
648
 
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 |
649
+ | Property | Type | Required | Default | Description |
650
+ | -------------------- | ------------------------------- | -------- | ----------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
651
+ | `endpoint` | `string` | ✓ | — | API endpoint URL (must be HTTPS; `http://localhost` and `http://127.0.0.1` are allowed for development) |
652
+ | `apiKey` | `string` | | — | API key issued from the dashboard |
653
+ | `tools` | `Tool[]` | ✓ | — | Array of tool definitions |
654
+ | `locale` | `"ja" \| "en"` | | `"ja"` | UI language. Set `"en"` for English |
655
+ | `placeholder` | `string` | | depends on `locale` | Input field placeholder |
656
+ | `theme` | `"auto" \| "light" \| "dark"` | | `"light"` | Color theme. `"auto"` follows OS dark-mode |
657
+ | `className` | `string` | | — | Class name applied to the root element |
658
+ | `toolTimeout` | `number` | | `30000` | Tool execution timeout (ms) |
659
+ | `disableShadowDom` | `boolean` | | `false` | Mount to normal DOM instead of Shadow DOM |
660
+ | `layout` | `LayoutPattern` | | `floating-bottom-right` | One of the nine layout patterns. See [Display Modes](#display-modes-layout-patterns). |
661
+ | `transparent` | `boolean` | | `false` | Only meaningful for `layout: "header-banner"`. Renders a translucent background that overlays the page content. |
662
+ | `narrateToolResults` | `boolean` | | `false` | After every tool result, make an extra LLM call with `tool_choice: "none"` so the model emits a short narration text. See [section](#narrate-tool-results) below for trade-offs. |
663
+ | `plugins` | `ChatPlugin[]` | | `[]` | Plugins to load. See the [Plugins](#plugins) section. |
664
+ | `onToolExecute` | `(name, args) => void` | | — | Callback fired right before tool execution |
665
+ | `onToolResult` | `(name, result) => void` | | — | Callback fired after tool execution finishes |
666
+ | `onMessagesChange` | `(messages: Message[]) => void` | | — | Fires whenever the internal `messages` list changes. Use this to mirror the conversation into your own host UI (often paired with `layout: "input-only"`). |
667
+ | `onError` | `(error: Error) => void` | | — | Callback fired on error |
668
+
669
+ ### Narrate tool results
670
+
671
+ Some models (e.g. `gpt-4o-mini`) suppress assistant text whenever they emit a `tool_call`. In long chains of tools (say, a 6-step order flow) this makes the UI feel like a silent state machine — the user clicks confirm buttons but never sees the model react.
672
+
673
+ Set `narrateToolResults: true` to opt in to a small extra round-trip after every tool result:
674
+
675
+ ```ts
676
+ initWecoolChat({
677
+ endpoint,
678
+ apiKey,
679
+ tools,
680
+ narrateToolResults: true,
681
+ });
682
+ ```
683
+
684
+ When enabled, after each tool finishes wecool-chat sends one more request to the API with `tool_choice: "none"` so the model is forced to reply with plain text. That short narration ("Got it, table 12 — picking dishes now") is then streamed into the UI right next to the tool that triggered it.
685
+
686
+ Trade-offs:
687
+
688
+ - **Cost / latency**: roughly doubles the number of LLM calls per turn. Off by default. Keep it off for utility-only chat bots and turn it on for guided multi-step flows where the cost is worth the conversational feel.
689
+ - **Duplicate suppression**: if the next regular round would also return text (i.e. the model decides the work is done after the narration), wecool-chat detects the duplicate and removes the older narration message so you don't see "we're done" twice in a row.
690
+ - **Loop budget**: enabling narration roughly doubles round-trips, so `MAX_TOOL_ITERATIONS` is bumped to 15 internally. Multi-tool flows up to ~7 tools complete comfortably.
392
691
 
393
692
  ---
394
693
 
@@ -412,11 +711,35 @@ If `placeholder` is provided, it takes precedence over the locale's default plac
412
711
 
413
712
  ### Colors / theme
414
713
 
415
- The current version does not expose official theme variables you can override from outside. To change colors, you have these options:
714
+ Switch the built-in color theme with `config.theme`:
715
+
716
+ ```ts
717
+ initWecoolChat({
718
+ endpoint: "https://api.wecool.dev",
719
+ apiKey: "...",
720
+ tools,
721
+ theme: "auto", // "auto" follows OS prefers-color-scheme; "light" / "dark" force a theme
722
+ });
723
+ ```
724
+
725
+ - **`"light"` (default)**: light background, dark text. Backwards-compatible with existing integrations.
726
+ - **`"dark"`**: dark background, light text.
727
+ - **`"auto"`**: follows the host OS's `prefers-color-scheme`. Switching happens in CSS only, so there is no JS-mount flash.
728
+
729
+ For finer-grained customization beyond these presets, you still have these escape hatches:
416
730
 
417
731
  - Apply a class to the root element via `className` and target it with Tailwind or your own CSS
418
732
  - 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
733
+ - Override the `--wecool-color-*` CSS variables on the Shadow Host (advanced)
734
+
735
+ ### Accessibility guidance for host pages
736
+
737
+ To stay compliant with WCAG 2.2 AA on the host page where you embed the widget, please keep the following in mind:
738
+
739
+ - **SC 2.4.11 Focus Not Obscured (AA)**: avoid placing other fixed-position elements (cookie banners, sticky footers) that fully cover the chat panel when it's open, or that cover keyboard-focused elements on the host page when the floating button is opened.
740
+ - **SC 3.2.6 Consistent Help (A)**: keep the floating widget at the same relative `position` (e.g. always `bottom-right`) across every page so users with cognitive disabilities can rely on muscle memory to find help.
741
+
742
+ See `docs/accessibility.md` in the repository for the full list of WCAG 2.2 success criteria covered by the widget and any known limitations.
420
743
 
421
744
  ---
422
745
 
@@ -436,6 +759,12 @@ const config = {
436
759
  // Audit log, Sentry breadcrumb, UI toast, etc.
437
760
  auditLog({ tool: name, result });
438
761
  },
762
+ onMessagesChange: (messages) => {
763
+ // Fires whenever the message list inside wecool-chat changes
764
+ // (new user/assistant message, streaming text update, deletion, ...).
765
+ // Mirror the conversation into your host UI here.
766
+ hostThreadStore.replaceMessages(messages);
767
+ },
439
768
  onError: (error) => {
440
769
  Sentry.captureException(error);
441
770
  },
@@ -0,0 +1,6 @@
1
+ function e(t) {
2
+ return t && t.__esModule && Object.prototype.hasOwnProperty.call(t, "default") ? t.default : t;
3
+ }
4
+ export {
5
+ e as g
6
+ };