wecool-chat 0.6.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.
- package/CHANGELOG.md +59 -1
- package/README.ja.md +286 -29
- package/README.md +291 -29
- package/dist/_commonjsHelpers-C6fGbg64.js +6 -0
- package/dist/browser-beO7dF90.js +1538 -0
- package/dist/core/cancellation-log.d.ts.map +1 -0
- package/dist/{client.d.ts → core/client.d.ts} +15 -3
- package/dist/core/client.d.ts.map +1 -0
- package/dist/{hooks → core}/consume-stream.d.ts +9 -5
- package/dist/core/consume-stream.d.ts.map +1 -0
- package/dist/{i18n → core/i18n}/messages.d.ts +4 -0
- package/dist/core/i18n/messages.d.ts.map +1 -0
- package/dist/core/index.d.ts +12 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/run-conversation.d.ts +38 -0
- package/dist/core/run-conversation.d.ts.map +1 -0
- package/dist/core/sse-parser.d.ts.map +1 -0
- package/dist/{hooks → core}/stream-state.d.ts +14 -11
- package/dist/core/stream-state.d.ts.map +1 -0
- package/dist/{hooks → core}/tool-runner.d.ts +4 -5
- package/dist/core/tool-runner.d.ts.map +1 -0
- package/dist/core/types.d.ts +248 -0
- package/dist/core/types.d.ts.map +1 -0
- package/dist/index.css +1 -1
- package/dist/index.d.ts +32 -14
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11062 -10369
- package/dist/plugin/context.d.ts +15 -0
- package/dist/plugin/context.d.ts.map +1 -0
- package/dist/plugin/define.d.ts +30 -0
- package/dist/plugin/define.d.ts.map +1 -0
- package/dist/plugin/id.d.ts +19 -0
- package/dist/plugin/id.d.ts.map +1 -0
- package/dist/plugin/index.d.ts +21 -0
- package/dist/plugin/index.d.ts.map +1 -0
- package/dist/plugin/logger.d.ts +11 -0
- package/dist/plugin/logger.d.ts.map +1 -0
- package/dist/plugin/registry.d.ts +21 -0
- package/dist/plugin/registry.d.ts.map +1 -0
- package/dist/plugin/types.d.ts +109 -0
- package/dist/plugin/types.d.ts.map +1 -0
- package/dist/plugin.d.ts +2 -0
- package/dist/plugin.js +4 -0
- package/dist/plugins/qrcode/__tests__/format-result.test.d.ts +2 -0
- package/dist/plugins/qrcode/__tests__/format-result.test.d.ts.map +1 -0
- package/dist/plugins/qrcode/__tests__/index.test.d.ts +2 -0
- package/dist/plugins/qrcode/__tests__/index.test.d.ts.map +1 -0
- package/dist/plugins/qrcode/__tests__/tool.test.d.ts +2 -0
- package/dist/plugins/qrcode/__tests__/tool.test.d.ts.map +1 -0
- package/dist/plugins/qrcode/format-result.d.ts +59 -0
- package/dist/plugins/qrcode/format-result.d.ts.map +1 -0
- package/dist/plugins/qrcode/index.d.ts +23 -0
- package/dist/plugins/qrcode/index.d.ts.map +1 -0
- package/dist/plugins/qrcode/tool.d.ts +50 -0
- package/dist/plugins/qrcode/tool.d.ts.map +1 -0
- package/dist/plugins/qrcode.d.ts +2 -0
- package/dist/plugins/qrcode.js +161 -0
- package/dist/{builtin-tools → ui/builtin-tools}/get-current-page-info.d.ts +1 -1
- package/dist/ui/builtin-tools/get-current-page-info.d.ts.map +1 -0
- package/dist/ui/builtin-tools/index.d.ts.map +1 -0
- package/dist/{builtin-tools → ui/builtin-tools}/merge-builtin-tools.d.ts +1 -1
- package/dist/ui/builtin-tools/merge-builtin-tools.d.ts.map +1 -0
- package/dist/ui/components/ChatFooter.d.ts.map +1 -0
- package/dist/ui/components/ChatHeader.d.ts +15 -0
- package/dist/ui/components/ChatHeader.d.ts.map +1 -0
- package/dist/ui/components/ChatHeaderMenu.d.ts +12 -0
- package/dist/ui/components/ChatHeaderMenu.d.ts.map +1 -0
- package/dist/{components → ui/components}/ChatPanel.d.ts +9 -2
- package/dist/ui/components/ChatPanel.d.ts.map +1 -0
- package/dist/ui/components/ChatWidget.d.ts +3 -0
- package/dist/ui/components/ChatWidget.d.ts.map +1 -0
- package/dist/{components → ui/components}/CopyIndicator.d.ts +1 -1
- package/dist/ui/components/CopyIndicator.d.ts.map +1 -0
- package/dist/{components → ui/components}/FloatButton.d.ts +2 -1
- package/dist/ui/components/FloatButton.d.ts.map +1 -0
- package/dist/{components → ui/components}/MessageActionsPopover.d.ts +1 -1
- package/dist/ui/components/MessageActionsPopover.d.ts.map +1 -0
- package/dist/ui/components/MessageAttachments.d.ts +21 -0
- package/dist/ui/components/MessageAttachments.d.ts.map +1 -0
- package/dist/{components → ui/components}/MessageBubble.d.ts +14 -7
- package/dist/ui/components/MessageBubble.d.ts.map +1 -0
- package/dist/{components → ui/components}/MessageCopyButton.d.ts +1 -1
- package/dist/ui/components/MessageCopyButton.d.ts.map +1 -0
- package/dist/{components → ui/components}/MessageEditor.d.ts +1 -1
- package/dist/ui/components/MessageEditor.d.ts.map +1 -0
- package/dist/{components → ui/components}/MessageList.d.ts +1 -1
- package/dist/ui/components/MessageList.d.ts.map +1 -0
- package/dist/{components → ui/components}/MessagePopoverButton.d.ts +1 -1
- package/dist/ui/components/MessagePopoverButton.d.ts.map +1 -0
- package/dist/ui/components/MessageRow.d.ts +18 -0
- package/dist/ui/components/MessageRow.d.ts.map +1 -0
- package/dist/{components → ui/components}/MessageRowActions.d.ts +2 -2
- package/dist/ui/components/MessageRowActions.d.ts.map +1 -0
- package/dist/{components → ui/components}/markdown/CodeBlock.d.ts +1 -1
- package/dist/ui/components/markdown/CodeBlock.d.ts.map +1 -0
- package/dist/{components → ui/components}/markdown/MarkdownMessage.d.ts +5 -1
- package/dist/ui/components/markdown/MarkdownMessage.d.ts.map +1 -0
- package/dist/ui/components/markdown/sanitize-clipboard.d.ts.map +1 -0
- package/dist/ui/components/markdown/stable-prefix.d.ts.map +1 -0
- package/dist/ui/components/markdown/strip-data-images.d.ts +2 -0
- package/dist/ui/components/markdown/strip-data-images.d.ts.map +1 -0
- package/dist/ui/components/markdown/url-transform.d.ts.map +1 -0
- package/dist/ui/components/message-row-handlers.d.ts.map +1 -0
- package/dist/ui/hooks/use-editing-message.d.ts.map +1 -0
- package/dist/ui/hooks/use-menu-keyboard.d.ts.map +1 -0
- package/dist/ui/hooks/use-message-history.d.ts +20 -0
- package/dist/ui/hooks/use-message-history.d.ts.map +1 -0
- package/dist/{hooks → ui/hooks}/use-send-message.d.ts +8 -14
- package/dist/ui/hooks/use-send-message.d.ts.map +1 -0
- package/dist/ui/hooks/use-stream-lifecycle.d.ts.map +1 -0
- package/dist/{hooks → ui/hooks}/use-tool-runner.d.ts +2 -3
- package/dist/ui/hooks/use-tool-runner.d.ts.map +1 -0
- package/dist/ui/hooks/useActiveMessage.d.ts.map +1 -0
- package/dist/{hooks → ui/hooks}/useChatStream.d.ts +5 -3
- package/dist/ui/hooks/useChatStream.d.ts.map +1 -0
- package/dist/ui/hooks/useCopyToClipboard.d.ts.map +1 -0
- package/dist/ui/hooks/useDismissOnOutside.d.ts.map +1 -0
- package/dist/ui/hooks/useDraggablePanel.d.ts +32 -0
- package/dist/ui/hooks/useDraggablePanel.d.ts.map +1 -0
- package/dist/ui/hooks/useFocusTrap.d.ts.map +1 -0
- package/dist/ui/hooks/useMediaQuery.d.ts.map +1 -0
- package/dist/ui/hooks/useScrollToBottom.d.ts +22 -0
- package/dist/ui/hooks/useScrollToBottom.d.ts.map +1 -0
- package/dist/ui/i18n/format-time.d.ts +10 -0
- package/dist/ui/i18n/format-time.d.ts.map +1 -0
- package/dist/ui/i18n/locale-context.d.ts +10 -0
- package/dist/ui/i18n/locale-context.d.ts.map +1 -0
- package/dist/{i18n → ui/i18n}/messages-context.d.ts +1 -1
- package/dist/ui/i18n/messages-context.d.ts.map +1 -0
- package/dist/{mount.d.ts → ui/mount.d.ts} +1 -1
- package/dist/ui/mount.d.ts.map +1 -0
- package/dist/ui/styles/index.d.ts.map +1 -0
- package/dist/ui/types.d.ts +42 -0
- package/dist/ui/types.d.ts.map +1 -0
- package/package.json +23 -3
- package/dist/builtin-tools/get-current-page-info.d.ts.map +0 -1
- package/dist/builtin-tools/index.d.ts.map +0 -1
- package/dist/builtin-tools/merge-builtin-tools.d.ts.map +0 -1
- package/dist/client.d.ts.map +0 -1
- package/dist/components/ChatFooter.d.ts.map +0 -1
- package/dist/components/ChatHeader.d.ts +0 -7
- package/dist/components/ChatHeader.d.ts.map +0 -1
- package/dist/components/ChatPanel.d.ts.map +0 -1
- package/dist/components/ChatWidget.d.ts +0 -3
- package/dist/components/ChatWidget.d.ts.map +0 -1
- package/dist/components/CopyIndicator.d.ts.map +0 -1
- package/dist/components/FloatButton.d.ts.map +0 -1
- package/dist/components/MessageActionsPopover.d.ts.map +0 -1
- package/dist/components/MessageBubble.d.ts.map +0 -1
- package/dist/components/MessageCopyButton.d.ts.map +0 -1
- package/dist/components/MessageEditor.d.ts.map +0 -1
- package/dist/components/MessageList.d.ts.map +0 -1
- package/dist/components/MessagePopoverButton.d.ts.map +0 -1
- package/dist/components/MessageRow.d.ts +0 -17
- package/dist/components/MessageRow.d.ts.map +0 -1
- package/dist/components/MessageRowActions.d.ts.map +0 -1
- package/dist/components/markdown/CodeBlock.d.ts.map +0 -1
- package/dist/components/markdown/MarkdownMessage.d.ts.map +0 -1
- package/dist/components/markdown/sanitize-clipboard.d.ts.map +0 -1
- package/dist/components/markdown/stable-prefix.d.ts.map +0 -1
- package/dist/components/markdown/url-transform.d.ts.map +0 -1
- package/dist/components/message-row-handlers.d.ts.map +0 -1
- package/dist/hooks/cancellation-log.d.ts.map +0 -1
- package/dist/hooks/consume-stream.d.ts.map +0 -1
- package/dist/hooks/run-conversation.d.ts +0 -23
- package/dist/hooks/run-conversation.d.ts.map +0 -1
- package/dist/hooks/stream-state.d.ts.map +0 -1
- package/dist/hooks/tool-runner.d.ts.map +0 -1
- package/dist/hooks/use-editing-message.d.ts.map +0 -1
- package/dist/hooks/use-menu-keyboard.d.ts.map +0 -1
- package/dist/hooks/use-send-message.d.ts.map +0 -1
- package/dist/hooks/use-stream-lifecycle.d.ts.map +0 -1
- package/dist/hooks/use-tool-runner.d.ts.map +0 -1
- package/dist/hooks/useActiveMessage.d.ts.map +0 -1
- package/dist/hooks/useChatStream.d.ts.map +0 -1
- package/dist/hooks/useCopyToClipboard.d.ts.map +0 -1
- package/dist/hooks/useDismissOnOutside.d.ts.map +0 -1
- package/dist/hooks/useFocusTrap.d.ts.map +0 -1
- package/dist/hooks/useMediaQuery.d.ts.map +0 -1
- package/dist/hooks/useScrollToBottom.d.ts +0 -12
- package/dist/hooks/useScrollToBottom.d.ts.map +0 -1
- package/dist/i18n/messages-context.d.ts.map +0 -1
- package/dist/i18n/messages.d.ts.map +0 -1
- package/dist/mount.d.ts.map +0 -1
- package/dist/sse-parser.d.ts.map +0 -1
- package/dist/styles/index.d.ts.map +0 -1
- package/dist/types.d.ts +0 -148
- package/dist/types.d.ts.map +0 -1
- /package/dist/{hooks → core}/cancellation-log.d.ts +0 -0
- /package/dist/{sse-parser.d.ts → core/sse-parser.d.ts} +0 -0
- /package/dist/{builtin-tools → ui/builtin-tools}/index.d.ts +0 -0
- /package/dist/{components → ui/components}/ChatFooter.d.ts +0 -0
- /package/dist/{components → ui/components}/markdown/sanitize-clipboard.d.ts +0 -0
- /package/dist/{components → ui/components}/markdown/stable-prefix.d.ts +0 -0
- /package/dist/{components → ui/components}/markdown/url-transform.d.ts +0 -0
- /package/dist/{components → ui/components}/message-row-handlers.d.ts +0 -0
- /package/dist/{hooks → ui/hooks}/use-editing-message.d.ts +0 -0
- /package/dist/{hooks → ui/hooks}/use-menu-keyboard.d.ts +0 -0
- /package/dist/{hooks → ui/hooks}/use-stream-lifecycle.d.ts +0 -0
- /package/dist/{hooks → ui/hooks}/useActiveMessage.d.ts +0 -0
- /package/dist/{hooks → ui/hooks}/useCopyToClipboard.d.ts +0 -0
- /package/dist/{hooks → ui/hooks}/useDismissOnOutside.d.ts +0 -0
- /package/dist/{hooks → ui/hooks}/useFocusTrap.d.ts +0 -0
- /package/dist/{hooks → ui/hooks}/useMediaQuery.d.ts +0 -0
- /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
|
-

|
|
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)
|
|
114
|
+
|
|
115
|
+
wecool-chat supports nine official layout patterns. Choose one with the `layout` option on `ChatConfig`.
|
|
116
|
+
|
|
117
|
+
### Pattern matrix
|
|
118
|
+
|
|
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) |
|
|
131
|
+
|
|
132
|
+
### Switching layout
|
|
133
|
+
|
|
134
|
+
```ts
|
|
135
|
+
import { initWecoolChat } from "wecool-chat";
|
|
136
|
+
|
|
137
|
+
initWecoolChat({
|
|
138
|
+
endpoint: "https://your-api.example.com",
|
|
139
|
+
apiKey: "wlt_...",
|
|
140
|
+
tools: [],
|
|
141
|
+
layout: "sidebar-left",
|
|
142
|
+
});
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
For `header-banner`, you can add `transparent: true` to overlay the banner on the page content with a translucent background.
|
|
112
146
|
|
|
113
|
-
|
|
147
|
+
### React equivalent
|
|
114
148
|
|
|
115
|
-
|
|
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.
|
|
116
150
|
|
|
117
|
-
|
|
151
|
+
### Choosing `inline-custom`
|
|
118
152
|
|
|
119
|
-
|
|
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:
|
|
120
154
|
|
|
121
|
-
|
|
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
|
+
```
|
|
122
191
|
|
|
123
|
-
|
|
192
|
+
This pairs nicely with `narrateToolResults` (see below) when you want the model to send a short acknowledgement after every tool call.
|
|
124
193
|
|
|
125
|
-
###
|
|
194
|
+
### Controller API (return value of `initWecoolChat`)
|
|
126
195
|
|
|
127
|
-
|
|
128
|
-
| -------- | -------------------------------------- | ------------------------------------------- |
|
|
129
|
-
| Floating | `<ChatWidget config={...} floating />` | `initWecoolChat(config)` |
|
|
130
|
-
| Embedded | `<ChatWidget config={...} />` | `initWecoolChat(config, "#chat-container")` |
|
|
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.
|
|
131
197
|
|
|
132
|
-
|
|
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
|
|
|
@@ -264,6 +347,151 @@ initWecoolChat({
|
|
|
264
347
|
|
|
265
348
|
---
|
|
266
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
|
+
|
|
493
|
+
---
|
|
494
|
+
|
|
267
495
|
## Framework Guides
|
|
268
496
|
|
|
269
497
|
### Vue 3
|
|
@@ -418,20 +646,48 @@ const ChatWidget = dynamic(() => import("wecool-chat").then((m) => m.ChatWidget)
|
|
|
418
646
|
|
|
419
647
|
## ChatConfig Options
|
|
420
648
|
|
|
421
|
-
| Property
|
|
422
|
-
|
|
|
423
|
-
| `endpoint`
|
|
424
|
-
| `apiKey`
|
|
425
|
-
| `tools`
|
|
426
|
-
| `locale`
|
|
427
|
-
| `placeholder`
|
|
428
|
-
| `theme`
|
|
429
|
-
| `className`
|
|
430
|
-
| `toolTimeout`
|
|
431
|
-
| `disableShadowDom`
|
|
432
|
-
| `
|
|
433
|
-
| `
|
|
434
|
-
| `
|
|
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.
|
|
435
691
|
|
|
436
692
|
---
|
|
437
693
|
|
|
@@ -503,6 +759,12 @@ const config = {
|
|
|
503
759
|
// Audit log, Sentry breadcrumb, UI toast, etc.
|
|
504
760
|
auditLog({ tool: name, result });
|
|
505
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
|
+
},
|
|
506
768
|
onError: (error) => {
|
|
507
769
|
Sentry.captureException(error);
|
|
508
770
|
},
|