@botdojo/chat-sdk 0.0.24 → 0.0.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +188 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -524,12 +524,200 @@ function TestPage() {
|
|
|
524
524
|
|
|
525
525
|
See the [test app](https://github.com/botdojo/botdojo/tree/main/apps/chat-embed-test) for complete working examples.
|
|
526
526
|
|
|
527
|
+
## Headless Chat (Custom UI)
|
|
528
|
+
|
|
529
|
+
Build your own chat UI with full design control using the Provider + Hooks pattern.
|
|
530
|
+
|
|
531
|
+
### Basic Headless Example
|
|
532
|
+
|
|
533
|
+
```tsx
|
|
534
|
+
import {
|
|
535
|
+
BotDojoChatProvider,
|
|
536
|
+
useChatMessages,
|
|
537
|
+
useChatActions,
|
|
538
|
+
useChatStatus,
|
|
539
|
+
} from '@botdojo/chat-sdk';
|
|
540
|
+
|
|
541
|
+
// Your custom chat UI
|
|
542
|
+
function ChatUI() {
|
|
543
|
+
const { messages, isStreaming } = useChatMessages();
|
|
544
|
+
const { sendMessage, abortRequest } = useChatActions();
|
|
545
|
+
const { status, isReady } = useChatStatus();
|
|
546
|
+
const [input, setInput] = useState('');
|
|
547
|
+
|
|
548
|
+
const handleSubmit = (e: React.FormEvent) => {
|
|
549
|
+
e.preventDefault();
|
|
550
|
+
if (input.trim() && isReady) {
|
|
551
|
+
sendMessage(input);
|
|
552
|
+
setInput('');
|
|
553
|
+
}
|
|
554
|
+
};
|
|
555
|
+
|
|
556
|
+
return (
|
|
557
|
+
<div className="chat-container">
|
|
558
|
+
{/* Message list */}
|
|
559
|
+
<div className="messages">
|
|
560
|
+
{messages.map((msg) => (
|
|
561
|
+
<div key={msg.id} className={`message ${msg.role}`}>
|
|
562
|
+
<p>{msg.content}</p>
|
|
563
|
+
{msg.status === 'streaming' && <span className="typing">...</span>}
|
|
564
|
+
</div>
|
|
565
|
+
))}
|
|
566
|
+
</div>
|
|
567
|
+
|
|
568
|
+
{/* Input form */}
|
|
569
|
+
<form onSubmit={handleSubmit}>
|
|
570
|
+
<input
|
|
571
|
+
value={input}
|
|
572
|
+
onChange={(e) => setInput(e.target.value)}
|
|
573
|
+
placeholder="Type a message..."
|
|
574
|
+
disabled={!isReady || isStreaming}
|
|
575
|
+
/>
|
|
576
|
+
{isStreaming ? (
|
|
577
|
+
<button type="button" onClick={abortRequest}>Stop</button>
|
|
578
|
+
) : (
|
|
579
|
+
<button type="submit" disabled={!input.trim()}>Send</button>
|
|
580
|
+
)}
|
|
581
|
+
</form>
|
|
582
|
+
</div>
|
|
583
|
+
);
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
// Wrap with provider
|
|
587
|
+
function App() {
|
|
588
|
+
return (
|
|
589
|
+
<BotDojoChatProvider apiKey="YOUR_API_KEY">
|
|
590
|
+
<ChatUI />
|
|
591
|
+
</BotDojoChatProvider>
|
|
592
|
+
);
|
|
593
|
+
}
|
|
594
|
+
```
|
|
595
|
+
|
|
596
|
+
### Headless with Frontend MCP (Model Context)
|
|
597
|
+
|
|
598
|
+
Give your AI access to tools that run in the browser:
|
|
599
|
+
|
|
600
|
+
```tsx
|
|
601
|
+
import {
|
|
602
|
+
BotDojoChatProvider,
|
|
603
|
+
useChatMessages,
|
|
604
|
+
useChatActions,
|
|
605
|
+
type ModelContext
|
|
606
|
+
} from '@botdojo/chat-sdk';
|
|
607
|
+
|
|
608
|
+
function WeatherChat() {
|
|
609
|
+
// Define tools that run in the browser
|
|
610
|
+
const modelContext: ModelContext = {
|
|
611
|
+
name: 'weather_service',
|
|
612
|
+
description: 'Provides live weather information',
|
|
613
|
+
toolPrefix: 'weather',
|
|
614
|
+
uri: 'weather://context',
|
|
615
|
+
tools: [
|
|
616
|
+
{
|
|
617
|
+
name: 'get_weather',
|
|
618
|
+
description: 'Get weather for a city',
|
|
619
|
+
inputSchema: {
|
|
620
|
+
type: 'object',
|
|
621
|
+
properties: {
|
|
622
|
+
city: { type: 'string', description: 'City name' }
|
|
623
|
+
},
|
|
624
|
+
required: ['city']
|
|
625
|
+
},
|
|
626
|
+
execute: async ({ city }) => {
|
|
627
|
+
// Fetch real weather data
|
|
628
|
+
const response = await fetch(`/api/weather?city=${city}`);
|
|
629
|
+
const data = await response.json();
|
|
630
|
+
return {
|
|
631
|
+
temperature: data.temp,
|
|
632
|
+
conditions: data.conditions,
|
|
633
|
+
};
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
]
|
|
637
|
+
};
|
|
638
|
+
|
|
639
|
+
return (
|
|
640
|
+
<BotDojoChatProvider
|
|
641
|
+
apiKey="YOUR_API_KEY"
|
|
642
|
+
modelContext={modelContext}
|
|
643
|
+
>
|
|
644
|
+
<CustomChatUI />
|
|
645
|
+
</BotDojoChatProvider>
|
|
646
|
+
);
|
|
647
|
+
}
|
|
648
|
+
```
|
|
649
|
+
|
|
650
|
+
### Available Hooks
|
|
651
|
+
|
|
652
|
+
| Hook | Description |
|
|
653
|
+
|------|-------------|
|
|
654
|
+
| `useBotDojoChat()` | Combined state and actions |
|
|
655
|
+
| `useChatMessages()` | Access `messages`, `currentMessage`, `isStreaming` |
|
|
656
|
+
| `useChatActions()` | Access `sendMessage`, `abortRequest`, `bargeInRequest`, `setSessionId`, `reload` |
|
|
657
|
+
| `useChatStatus()` | Access `status`, `isReady`, `error`, `sessionId` |
|
|
658
|
+
|
|
659
|
+
### Provider Props
|
|
660
|
+
|
|
661
|
+
| Prop | Type | Description |
|
|
662
|
+
|------|------|-------------|
|
|
663
|
+
| `apiKey` | `string` | **Required.** Your BotDojo API key |
|
|
664
|
+
| `modelContext` | `ModelContext \| ModelContext[]` | Tools and resources for the AI |
|
|
665
|
+
| `baseUrl` | `string` | BotDojo embed URL. Default: auto-detected |
|
|
666
|
+
| `sessionId` | `string` | Resume a specific session |
|
|
667
|
+
| `newSession` | `boolean` | Force new session. Default: `false` |
|
|
668
|
+
| `debug` | `boolean` | Enable debug mode for MCP Apps |
|
|
669
|
+
| `onReady` | `() => void` | Called when provider is ready |
|
|
670
|
+
| `onError` | `(error: Error) => void` | Called on errors |
|
|
671
|
+
| `onSessionCreated` | `(sessionId: string) => void` | Called when session is created |
|
|
672
|
+
| `onOpenLink` | `(url, target, appId) => void` | Handle MCP App link requests |
|
|
673
|
+
| `onToolCall` | `(tool, params, appId) => Promise<any>` | Handle MCP App tool calls |
|
|
674
|
+
| `onUiMessage` | `(message, params, appId) => void` | Handle MCP App messages |
|
|
675
|
+
|
|
676
|
+
### Rendering MCP Apps in Headless Mode
|
|
677
|
+
|
|
678
|
+
When tools return UI (MCP Apps), render them using `McpAppHost`:
|
|
679
|
+
|
|
680
|
+
```tsx
|
|
681
|
+
import { useChatMessages, McpAppHost, extractMcpAppData } from '@botdojo/chat-sdk';
|
|
682
|
+
|
|
683
|
+
function MessageList() {
|
|
684
|
+
const { messages } = useChatMessages();
|
|
685
|
+
|
|
686
|
+
return (
|
|
687
|
+
<div>
|
|
688
|
+
{messages.map((msg) => (
|
|
689
|
+
<div key={msg.id}>
|
|
690
|
+
<p>{msg.content}</p>
|
|
691
|
+
|
|
692
|
+
{/* Render MCP Apps from message steps */}
|
|
693
|
+
{msg.steps.map((step) => {
|
|
694
|
+
const mcpApp = extractMcpAppData(step, {
|
|
695
|
+
isComplete: msg.status === 'complete'
|
|
696
|
+
});
|
|
697
|
+
if (!mcpApp) return null;
|
|
698
|
+
|
|
699
|
+
return (
|
|
700
|
+
<McpAppHost
|
|
701
|
+
key={mcpApp.mcpAppId}
|
|
702
|
+
mcpAppId={mcpApp.mcpAppId}
|
|
703
|
+
mcpAppData={mcpApp}
|
|
704
|
+
/>
|
|
705
|
+
);
|
|
706
|
+
})}
|
|
707
|
+
</div>
|
|
708
|
+
))}
|
|
709
|
+
</div>
|
|
710
|
+
);
|
|
711
|
+
}
|
|
712
|
+
```
|
|
713
|
+
|
|
527
714
|
## Links
|
|
528
715
|
|
|
529
716
|
- [Documentation](https://docs.botdojo.com)
|
|
530
717
|
- [Website](https://botdojo.com)
|
|
531
718
|
- [Get API Key](https://app.botdojo.com)
|
|
532
719
|
- [NPM Package](https://www.npmjs.com/package/@botdojo/chat-sdk)
|
|
720
|
+
- [SDK Playground](https://github.com/botdojo/chat-sdk-playground) - Live examples
|
|
533
721
|
|
|
534
722
|
## License
|
|
535
723
|
|