@botdojo/chat-sdk 0.0.2
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/LICENSE +27 -0
- package/README.md +536 -0
- package/lib/index.d.ts +384 -0
- package/lib/index.js +9206 -0
- package/lib/index.js.map +1 -0
- package/package.json +62 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 BotDojo
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,536 @@
|
|
|
1
|
+
# @botdojo/chat-sdk
|
|
2
|
+
|
|
3
|
+
Simple React component for embedding BotDojo AI chat into any application.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 🚀 **Easy Integration** - Single React component
|
|
8
|
+
- 💬 **Four Display Modes** - Popup, side panel, side push, or inline
|
|
9
|
+
- 🎨 **Fully Customizable** - Colors, sizing, theming
|
|
10
|
+
- 🛠️ **Custom Tools** - Give your AI access to your app
|
|
11
|
+
- 📚 **Resource Support** - Serve content to the AI
|
|
12
|
+
- 🔐 **Secure** - API key-based authentication
|
|
13
|
+
- 🎯 **TypeScript** - Full type definitions
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install @botdojo/chat-sdk react
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
**Requirements:** React 17.0+ or React 18.0+
|
|
22
|
+
|
|
23
|
+
## Quick Start
|
|
24
|
+
|
|
25
|
+
### Basic Chat
|
|
26
|
+
|
|
27
|
+
```tsx
|
|
28
|
+
import { BotDojoChat } from '@botdojo/chat-sdk';
|
|
29
|
+
|
|
30
|
+
function App() {
|
|
31
|
+
return <BotDojoChat apiKey="YOUR_API_KEY" />;
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
That's it! You'll get a chat popup button in the bottom-right corner.
|
|
36
|
+
|
|
37
|
+
### Chat with Custom Tools
|
|
38
|
+
|
|
39
|
+
Give your AI the ability to interact with your application:
|
|
40
|
+
|
|
41
|
+
```tsx
|
|
42
|
+
import { BotDojoChat } from '@botdojo/chat-sdk';
|
|
43
|
+
|
|
44
|
+
function App() {
|
|
45
|
+
return (
|
|
46
|
+
<BotDojoChat
|
|
47
|
+
apiKey="YOUR_API_KEY"
|
|
48
|
+
modelContext={{
|
|
49
|
+
name: 'myapp',
|
|
50
|
+
description: 'My application tools',
|
|
51
|
+
toolPrefix: 'app',
|
|
52
|
+
tools: {
|
|
53
|
+
get_page_title: {
|
|
54
|
+
description: 'Get the current page title',
|
|
55
|
+
parameters: { type: 'object', properties: {} },
|
|
56
|
+
execute: async () => document.title
|
|
57
|
+
},
|
|
58
|
+
scroll_to_section: {
|
|
59
|
+
description: 'Scroll to a specific section',
|
|
60
|
+
parameters: {
|
|
61
|
+
type: 'object',
|
|
62
|
+
properties: {
|
|
63
|
+
section: { type: 'string', description: 'Section ID' }
|
|
64
|
+
},
|
|
65
|
+
required: ['section']
|
|
66
|
+
},
|
|
67
|
+
execute: async (args: { section: string }) => {
|
|
68
|
+
const element = document.getElementById(args.section);
|
|
69
|
+
if (element) {
|
|
70
|
+
element.scrollIntoView({ behavior: 'smooth' });
|
|
71
|
+
return `Scrolled to ${args.section}`;
|
|
72
|
+
}
|
|
73
|
+
return 'Section not found';
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}}
|
|
78
|
+
/>
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Display Modes
|
|
84
|
+
|
|
85
|
+
### Chat Popup (Default)
|
|
86
|
+
|
|
87
|
+
```tsx
|
|
88
|
+
<BotDojoChat apiKey="YOUR_API_KEY" mode="chat-popup" />
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Floating button in bottom-right corner. **Best for:** Most websites, minimal intrusion.
|
|
92
|
+
|
|
93
|
+
### Side Panel
|
|
94
|
+
|
|
95
|
+
```tsx
|
|
96
|
+
<BotDojoChat apiKey="YOUR_API_KEY" mode="side-panel" />
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Slides in from the side, overlays content. **Best for:** Documentation sites, support portals.
|
|
100
|
+
|
|
101
|
+
### Side Push
|
|
102
|
+
|
|
103
|
+
```tsx
|
|
104
|
+
<BotDojoChat apiKey="YOUR_API_KEY" mode="side-push" />
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Slides in from the side, pushes content. **Best for:** Integrated layouts.
|
|
108
|
+
|
|
109
|
+
### Inline
|
|
110
|
+
|
|
111
|
+
```tsx
|
|
112
|
+
<BotDojoChat apiKey="YOUR_API_KEY" mode="inline" width="100%" height="600px" />
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Direct embed in the page. **Best for:** Dedicated chat pages, full-screen experiences.
|
|
116
|
+
|
|
117
|
+
## Customization
|
|
118
|
+
|
|
119
|
+
### Colors & Theme
|
|
120
|
+
|
|
121
|
+
```tsx
|
|
122
|
+
<BotDojoChat
|
|
123
|
+
apiKey="YOUR_API_KEY"
|
|
124
|
+
accentColor="#ff6b6b"
|
|
125
|
+
backgroundColor="#f5f5f5"
|
|
126
|
+
theme="dark"
|
|
127
|
+
fontSize="16px"
|
|
128
|
+
/>
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Popup Options
|
|
132
|
+
|
|
133
|
+
```tsx
|
|
134
|
+
<BotDojoChat
|
|
135
|
+
apiKey="YOUR_API_KEY"
|
|
136
|
+
mode="chat-popup"
|
|
137
|
+
popupOptions={{
|
|
138
|
+
width: '500px',
|
|
139
|
+
height: '700px',
|
|
140
|
+
resizable: true,
|
|
141
|
+
minWidth: '350px',
|
|
142
|
+
maxWidth: '800px'
|
|
143
|
+
}}
|
|
144
|
+
/>
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### Side Panel Options
|
|
148
|
+
|
|
149
|
+
```tsx
|
|
150
|
+
<BotDojoChat
|
|
151
|
+
apiKey="YOUR_API_KEY"
|
|
152
|
+
mode="side-panel"
|
|
153
|
+
sidePanelOptions={{
|
|
154
|
+
direction: 'left',
|
|
155
|
+
defaultWidth: '450px',
|
|
156
|
+
resizable: true,
|
|
157
|
+
minWidth: '300px',
|
|
158
|
+
maxWidth: '1000px'
|
|
159
|
+
}}
|
|
160
|
+
/>
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## Key Props
|
|
164
|
+
|
|
165
|
+
| Prop | Type | Description |
|
|
166
|
+
|------|------|-------------|
|
|
167
|
+
| `apiKey` | `string` | **Required.** Your BotDojo API key |
|
|
168
|
+
| `mode` | `'chat-popup' \| 'side-panel' \| 'side-push' \| 'inline'` | Display mode. Default: `'chat-popup'` |
|
|
169
|
+
| `modelContext` | `ModelContext` | Define tools (with `execute` functions) and resources for the AI |
|
|
170
|
+
| `accentColor` | `string` | Primary color. Default: `'#4a3ed4'` |
|
|
171
|
+
| `backgroundColor` | `string` | Background color. Default: `'#ffffff'` |
|
|
172
|
+
| `theme` | `'light' \| 'dark'` | Theme mode. Default: `'light'` |
|
|
173
|
+
| `width` | `string` | Container width. CSS value |
|
|
174
|
+
| `height` | `string` | Container height. CSS value |
|
|
175
|
+
| `popupOptions` | `PopupOptions` | Popup-specific configuration |
|
|
176
|
+
| `sidePanelOptions` | `SidePanelOptions` | Side panel configuration |
|
|
177
|
+
| `flowHeaders` | `object` | Custom headers for flow execution |
|
|
178
|
+
| `sessionId` | `string` | Specific session ID to use/resume |
|
|
179
|
+
| `newSession` | `boolean` | Force new session. Default: `false` |
|
|
180
|
+
| `allowMicrophone` | `boolean` | Enable voice input. Default: `false` |
|
|
181
|
+
| `onLoad` | `() => void` | Called when iframe loads |
|
|
182
|
+
| `onOpen` | `() => void` | Called when chat opens |
|
|
183
|
+
| `onClose` | `() => void` | Called when chat closes |
|
|
184
|
+
| `onBotDojoChatControl` | `(control) => void` | Receive control methods |
|
|
185
|
+
|
|
186
|
+
## Real-World Examples
|
|
187
|
+
|
|
188
|
+
### E-commerce Assistant
|
|
189
|
+
|
|
190
|
+
```tsx
|
|
191
|
+
function Shop() {
|
|
192
|
+
const [cart, setCart] = useState([]);
|
|
193
|
+
|
|
194
|
+
return (
|
|
195
|
+
<BotDojoChat
|
|
196
|
+
apiKey="YOUR_KEY"
|
|
197
|
+
mode="side-panel"
|
|
198
|
+
accentColor="#2563eb"
|
|
199
|
+
modelContext={{
|
|
200
|
+
name: 'shop',
|
|
201
|
+
description: 'E-commerce store assistant',
|
|
202
|
+
toolPrefix: 'shop',
|
|
203
|
+
tools: {
|
|
204
|
+
add_to_cart: {
|
|
205
|
+
description: 'Add a product to the shopping cart',
|
|
206
|
+
parameters: {
|
|
207
|
+
type: 'object',
|
|
208
|
+
properties: {
|
|
209
|
+
productId: { type: 'string', description: 'Product ID' },
|
|
210
|
+
quantity: { type: 'number', description: 'Quantity' }
|
|
211
|
+
},
|
|
212
|
+
required: ['productId', 'quantity']
|
|
213
|
+
},
|
|
214
|
+
execute: async ({ productId, quantity }) => {
|
|
215
|
+
const product = await fetchProduct(productId);
|
|
216
|
+
setCart([...cart, { product, quantity }]);
|
|
217
|
+
return {
|
|
218
|
+
success: true,
|
|
219
|
+
message: `Added ${quantity}x ${product.name}`
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}}
|
|
225
|
+
/>
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
### Documentation Helper
|
|
231
|
+
|
|
232
|
+
```tsx
|
|
233
|
+
function Docs() {
|
|
234
|
+
return (
|
|
235
|
+
<BotDojoChat
|
|
236
|
+
apiKey="YOUR_KEY"
|
|
237
|
+
mode="chat-popup"
|
|
238
|
+
modelContext={{
|
|
239
|
+
name: 'docs',
|
|
240
|
+
description: 'Documentation assistant',
|
|
241
|
+
toolPrefix: 'docs',
|
|
242
|
+
tools: {
|
|
243
|
+
search_docs: {
|
|
244
|
+
description: 'Search the documentation',
|
|
245
|
+
parameters: {
|
|
246
|
+
type: 'object',
|
|
247
|
+
properties: {
|
|
248
|
+
query: { type: 'string' }
|
|
249
|
+
},
|
|
250
|
+
required: ['query']
|
|
251
|
+
},
|
|
252
|
+
execute: async ({ query }) => {
|
|
253
|
+
const results = await docsSearch(query);
|
|
254
|
+
return { results };
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
},
|
|
258
|
+
resources: [
|
|
259
|
+
{
|
|
260
|
+
name: 'getting_started',
|
|
261
|
+
uri: 'resource://docs/getting-started',
|
|
262
|
+
mimeType: 'text/markdown',
|
|
263
|
+
description: 'Getting started guide',
|
|
264
|
+
getContent: async () => '# Getting Started\n\n...'
|
|
265
|
+
}
|
|
266
|
+
]
|
|
267
|
+
}}
|
|
268
|
+
/>
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
## Programmatic Control
|
|
274
|
+
|
|
275
|
+
```tsx
|
|
276
|
+
function App() {
|
|
277
|
+
const [chatControl, setChatControl] = useState<BotDojoChatControl | null>(null);
|
|
278
|
+
|
|
279
|
+
return (
|
|
280
|
+
<>
|
|
281
|
+
<button onClick={() => chatControl?.openChat()}>Open Chat</button>
|
|
282
|
+
<button onClick={() => chatControl?.sendFlowRequest({ message: 'Hello!' })}>
|
|
283
|
+
Send Message
|
|
284
|
+
</button>
|
|
285
|
+
|
|
286
|
+
<BotDojoChat
|
|
287
|
+
apiKey="YOUR_KEY"
|
|
288
|
+
modelContext={{...}}
|
|
289
|
+
onBotDojoChatControl={setChatControl}
|
|
290
|
+
/>
|
|
291
|
+
</>
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
## TypeScript
|
|
297
|
+
|
|
298
|
+
Full TypeScript support:
|
|
299
|
+
|
|
300
|
+
```typescript
|
|
301
|
+
import {
|
|
302
|
+
BotDojoChat,
|
|
303
|
+
BotDojoChatProps,
|
|
304
|
+
ModelContext,
|
|
305
|
+
BotDojoChatControl
|
|
306
|
+
} from '@botdojo/chat-sdk';
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
## Migration from botdojo-embed.js
|
|
310
|
+
|
|
311
|
+
**Before (HTML + Script):**
|
|
312
|
+
```html
|
|
313
|
+
<script src="botdojo-embed.js"
|
|
314
|
+
data-iframe-url="https://embed.botdojo.com/embed/chat?key=KEY"
|
|
315
|
+
data-accent-color="#4a3ed4">
|
|
316
|
+
</script>
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
**After (React Component):**
|
|
320
|
+
```tsx
|
|
321
|
+
import { BotDojoChat } from '@botdojo/chat-sdk';
|
|
322
|
+
|
|
323
|
+
<BotDojoChat apiKey="KEY" mode="chat-popup" accentColor="#4a3ed4" />
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
## Canvas Components
|
|
327
|
+
|
|
328
|
+
### Building Interactive Canvas Cards
|
|
329
|
+
|
|
330
|
+
Canvas cards are React components that render inline in the chat message stream. They can send messages back to the agent, display rich content, and execute tools in the parent application.
|
|
331
|
+
|
|
332
|
+
#### Basic Canvas Component
|
|
333
|
+
|
|
334
|
+
```tsx
|
|
335
|
+
import { useBotDojoCanvas } from '@botdojo/chat-sdk';
|
|
336
|
+
|
|
337
|
+
function ProductCard() {
|
|
338
|
+
const canvas = useBotDojoCanvas();
|
|
339
|
+
const [quantity, setQuantity] = useState(1);
|
|
340
|
+
|
|
341
|
+
const handleAddToCart = async () => {
|
|
342
|
+
// Call a tool in the parent application
|
|
343
|
+
await canvas.connector.executeToolCall('addToCart', {
|
|
344
|
+
product_id: canvas.canvasData?.id,
|
|
345
|
+
quantity
|
|
346
|
+
});
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
const handleAskAI = async () => {
|
|
350
|
+
// Send a message to the AI agent
|
|
351
|
+
await canvas.sendMessage(`Tell me more about ${canvas.canvasData?.name}`);
|
|
352
|
+
};
|
|
353
|
+
|
|
354
|
+
if (!canvas.canvasData) return null;
|
|
355
|
+
|
|
356
|
+
return (
|
|
357
|
+
<div style={{ padding: '16px', border: '1px solid #e5e7eb', borderRadius: '8px' }}>
|
|
358
|
+
<h3>{canvas.canvasData.name}</h3>
|
|
359
|
+
<p style={{ color: '#10b981', fontSize: '24px', fontWeight: 'bold' }}>
|
|
360
|
+
${canvas.canvasData.price}
|
|
361
|
+
</p>
|
|
362
|
+
<p>{canvas.canvasData.description}</p>
|
|
363
|
+
|
|
364
|
+
<input
|
|
365
|
+
type="number"
|
|
366
|
+
value={quantity}
|
|
367
|
+
onChange={(e) => setQuantity(parseInt(e.target.value))}
|
|
368
|
+
min="1"
|
|
369
|
+
/>
|
|
370
|
+
|
|
371
|
+
<button onClick={handleAddToCart} disabled={!canvas.isReady}>
|
|
372
|
+
Add to Cart
|
|
373
|
+
</button>
|
|
374
|
+
|
|
375
|
+
<button onClick={handleAskAI} disabled={!canvas.connector}>
|
|
376
|
+
Ask AI
|
|
377
|
+
</button>
|
|
378
|
+
</div>
|
|
379
|
+
);
|
|
380
|
+
}
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
#### Rendering Canvas in Your App
|
|
384
|
+
|
|
385
|
+
To display canvas content, wrap your component in `BotDojoCanvasProvider`:
|
|
386
|
+
|
|
387
|
+
```tsx
|
|
388
|
+
import { BotDojoCanvasProvider } from '@botdojo/chat-sdk';
|
|
389
|
+
import { ProductCard } from './ProductCard';
|
|
390
|
+
|
|
391
|
+
function CanvasRenderer() {
|
|
392
|
+
const router = useRouter();
|
|
393
|
+
|
|
394
|
+
// Parse canvas data from URL or props
|
|
395
|
+
const canvasData = {
|
|
396
|
+
id: router.query.product_id,
|
|
397
|
+
name: router.query.name,
|
|
398
|
+
price: router.query.price,
|
|
399
|
+
description: router.query.description,
|
|
400
|
+
};
|
|
401
|
+
|
|
402
|
+
return (
|
|
403
|
+
<BotDojoCanvasProvider canvasData={canvasData} debug={true}>
|
|
404
|
+
<ProductCard />
|
|
405
|
+
</BotDojoCanvasProvider>
|
|
406
|
+
);
|
|
407
|
+
}
|
|
408
|
+
```
|
|
409
|
+
|
|
410
|
+
#### Creating Canvas from Agent Tools
|
|
411
|
+
|
|
412
|
+
Agent tools can create canvas cards by returning canvas data:
|
|
413
|
+
|
|
414
|
+
```tsx
|
|
415
|
+
const modelContext = {
|
|
416
|
+
name: 'shop',
|
|
417
|
+
tools: [
|
|
418
|
+
{
|
|
419
|
+
name: 'showProductCard',
|
|
420
|
+
description: 'Display a product card in the chat',
|
|
421
|
+
arguments: {
|
|
422
|
+
type: 'object',
|
|
423
|
+
properties: {
|
|
424
|
+
product_id: { type: 'string' }
|
|
425
|
+
},
|
|
426
|
+
required: ['product_id']
|
|
427
|
+
},
|
|
428
|
+
execute: async ({ product_id }) => {
|
|
429
|
+
const product = await fetchProduct(product_id);
|
|
430
|
+
|
|
431
|
+
// Build URL to your canvas renderer
|
|
432
|
+
const canvasUrl = new URL('https://yourapp.com/canvas-render');
|
|
433
|
+
canvasUrl.searchParams.set('type', 'product-card');
|
|
434
|
+
canvasUrl.searchParams.set('product_id', product.id);
|
|
435
|
+
canvasUrl.searchParams.set('name', product.name);
|
|
436
|
+
canvasUrl.searchParams.set('price', product.price);
|
|
437
|
+
canvasUrl.searchParams.set('description', product.description);
|
|
438
|
+
|
|
439
|
+
return {
|
|
440
|
+
success: true,
|
|
441
|
+
_canvas: {
|
|
442
|
+
canvasId: null,
|
|
443
|
+
canvasType: 'dojo-canvas',
|
|
444
|
+
canvasData: {
|
|
445
|
+
url: canvasUrl.toString(),
|
|
446
|
+
type: 'product-card',
|
|
447
|
+
product: product,
|
|
448
|
+
show_inline: true,
|
|
449
|
+
height: '400px',
|
|
450
|
+
agent_enabled: true,
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
]
|
|
457
|
+
};
|
|
458
|
+
```
|
|
459
|
+
|
|
460
|
+
#### Canvas Hook API
|
|
461
|
+
|
|
462
|
+
The `useBotDojoCanvas` hook provides:
|
|
463
|
+
|
|
464
|
+
```typescript
|
|
465
|
+
const canvas = useBotDojoCanvas();
|
|
466
|
+
|
|
467
|
+
// Properties
|
|
468
|
+
canvas.isReady // boolean - whether canvas is initialized
|
|
469
|
+
canvas.error // Error | null - any initialization error
|
|
470
|
+
canvas.canvasData // any - data passed to this canvas card
|
|
471
|
+
canvas.connector // BotDojoConnector | null - connection to parent
|
|
472
|
+
canvas.isMockMode // boolean - true when using MockCanvasProvider
|
|
473
|
+
|
|
474
|
+
// Methods
|
|
475
|
+
await canvas.sendMessage(text, params) // Send message to agent
|
|
476
|
+
await canvas.connector.executeToolCall(name, args) // Call parent tool
|
|
477
|
+
const unsubscribe = canvas.on(event, handler) // Listen to events
|
|
478
|
+
```
|
|
479
|
+
|
|
480
|
+
#### Canvas Events
|
|
481
|
+
|
|
482
|
+
Listen to canvas events:
|
|
483
|
+
|
|
484
|
+
```tsx
|
|
485
|
+
useEffect(() => {
|
|
486
|
+
const unsubscribe = canvas.on('message', (data) => {
|
|
487
|
+
console.log('Message received:', data);
|
|
488
|
+
});
|
|
489
|
+
|
|
490
|
+
return unsubscribe;
|
|
491
|
+
}, [canvas]);
|
|
492
|
+
```
|
|
493
|
+
|
|
494
|
+
Available events: `canvas:ready`, `canvas:update`, `message`, `token`, `complete`, `error`
|
|
495
|
+
|
|
496
|
+
#### Testing Canvas Components
|
|
497
|
+
|
|
498
|
+
Use `MockCanvasProvider` for standalone development:
|
|
499
|
+
|
|
500
|
+
```tsx
|
|
501
|
+
import { MockCanvasProvider } from '@botdojo/chat-sdk';
|
|
502
|
+
import { ProductCard } from './ProductCard';
|
|
503
|
+
|
|
504
|
+
function TestPage() {
|
|
505
|
+
return (
|
|
506
|
+
<MockCanvasProvider
|
|
507
|
+
mockCanvasData={{
|
|
508
|
+
id: 'test-product',
|
|
509
|
+
name: 'Test Product',
|
|
510
|
+
price: '99.99',
|
|
511
|
+
description: 'A test product'
|
|
512
|
+
}}
|
|
513
|
+
onSendMessage={async (text) => {
|
|
514
|
+
console.log('Would send:', text);
|
|
515
|
+
return { success: true };
|
|
516
|
+
}}
|
|
517
|
+
debug={true}
|
|
518
|
+
>
|
|
519
|
+
<ProductCard />
|
|
520
|
+
</MockCanvasProvider>
|
|
521
|
+
);
|
|
522
|
+
}
|
|
523
|
+
```
|
|
524
|
+
|
|
525
|
+
See the [test app](https://github.com/botdojo/botdojo/tree/main/apps/chat-embed-test) for complete working examples.
|
|
526
|
+
|
|
527
|
+
## Links
|
|
528
|
+
|
|
529
|
+
- [Documentation](https://docs.botdojo.com)
|
|
530
|
+
- [Website](https://botdojo.com)
|
|
531
|
+
- [Get API Key](https://app.botdojo.com)
|
|
532
|
+
- [NPM Package](https://www.npmjs.com/package/@botdojo/chat-sdk)
|
|
533
|
+
|
|
534
|
+
## License
|
|
535
|
+
|
|
536
|
+
MIT
|