@distri/react 0.1.8 → 0.2.1
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 +186 -0
- package/dist/globals.css +2358 -0
- package/dist/globals.d.cts +2 -0
- package/dist/globals.d.ts +2 -0
- package/dist/index.cjs +4724 -0
- package/dist/index.d.cts +850 -0
- package/dist/index.d.ts +811 -17
- package/dist/index.js +4358 -217
- package/package.json +42 -32
- package/dist/index.d.mts +0 -56
- package/dist/index.js.map +0 -1
- package/dist/index.mjs +0 -434
- package/dist/index.mjs.map +0 -1
package/README.md
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
# @distri/react
|
|
2
|
+
|
|
3
|
+
React components and hooks for building chat applications with DistriJS.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @distri/react @distri/core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Basic Usage
|
|
12
|
+
|
|
13
|
+
```tsx
|
|
14
|
+
import React from 'react';
|
|
15
|
+
import { DistriProvider, EmbeddableChat, useAgent } from '@distri/react';
|
|
16
|
+
|
|
17
|
+
function App() {
|
|
18
|
+
const { agent } = useAgent({ agentId: 'assistant' });
|
|
19
|
+
|
|
20
|
+
return (
|
|
21
|
+
<DistriProvider config={{ baseUrl: 'http://localhost:8080/api/v1' }}>
|
|
22
|
+
<EmbeddableChat
|
|
23
|
+
agent={agent}
|
|
24
|
+
placeholder="Ask me anything..."
|
|
25
|
+
/>
|
|
26
|
+
</DistriProvider>
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Using Built-in Tools
|
|
32
|
+
|
|
33
|
+
DistriJS provides built-in tools that render as React components within chat messages:
|
|
34
|
+
|
|
35
|
+
```tsx
|
|
36
|
+
import React, { useEffect } from 'react';
|
|
37
|
+
import {
|
|
38
|
+
DistriProvider,
|
|
39
|
+
EmbeddableChat,
|
|
40
|
+
useAgent,
|
|
41
|
+
createBuiltinTools
|
|
42
|
+
} from '@distri/react';
|
|
43
|
+
|
|
44
|
+
function App() {
|
|
45
|
+
const { agent } = useAgent({ agentId: 'assistant' });
|
|
46
|
+
|
|
47
|
+
useEffect(() => {
|
|
48
|
+
if (agent) {
|
|
49
|
+
// Register all built-in tools
|
|
50
|
+
const builtinTools = createBuiltinTools();
|
|
51
|
+
builtinTools.forEach(tool => agent.registerTool(tool));
|
|
52
|
+
}
|
|
53
|
+
}, [agent]);
|
|
54
|
+
|
|
55
|
+
return (
|
|
56
|
+
<DistriProvider config={{ baseUrl: 'http://localhost:8080/api/v1' }}>
|
|
57
|
+
<EmbeddableChat
|
|
58
|
+
agent={agent}
|
|
59
|
+
placeholder="Ask me anything..."
|
|
60
|
+
/>
|
|
61
|
+
</DistriProvider>
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Available Built-in Tools
|
|
67
|
+
|
|
68
|
+
### Approval Tool
|
|
69
|
+
Shows an interactive approval dialog within the chat:
|
|
70
|
+
|
|
71
|
+
```tsx
|
|
72
|
+
import { createApprovalTool } from '@distri/react';
|
|
73
|
+
|
|
74
|
+
const approvalTool = createApprovalTool();
|
|
75
|
+
agent.registerTool(approvalTool);
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
When called by the agent, it renders an interactive component with "Approve" and "Deny" buttons right in the chat.
|
|
79
|
+
|
|
80
|
+
### Toast Tool
|
|
81
|
+
Shows toast notifications and displays confirmation in chat:
|
|
82
|
+
|
|
83
|
+
```tsx
|
|
84
|
+
import { createToastTool } from '@distri/react';
|
|
85
|
+
|
|
86
|
+
const toastTool = createToastTool();
|
|
87
|
+
agent.registerTool(toastTool);
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
When called, it shows a toast notification and displays the confirmation in the chat message.
|
|
91
|
+
|
|
92
|
+
## How Built-in Tools Work
|
|
93
|
+
|
|
94
|
+
1. **Register the tool** with your agent like any other tool
|
|
95
|
+
2. **Agent calls the tool** during conversation
|
|
96
|
+
3. **React component renders** in the chat message showing the tool's UI
|
|
97
|
+
4. **User interacts** with the component (approve/deny, etc.)
|
|
98
|
+
5. **Tool completes** and returns result to the agent
|
|
99
|
+
6. **Tool results are automatically sent via streaming** to the agent when all pending tools are complete
|
|
100
|
+
7. **Conversation continues** with the agent having all tool results
|
|
101
|
+
|
|
102
|
+
## Tool Call State Management
|
|
103
|
+
|
|
104
|
+
The new `useToolCallState` hook provides clean, organized tool call management:
|
|
105
|
+
|
|
106
|
+
```tsx
|
|
107
|
+
import { useToolCallState } from '@distri/react';
|
|
108
|
+
|
|
109
|
+
const toolState = useToolCallState({
|
|
110
|
+
onAllToolsCompleted: (toolResults) => {
|
|
111
|
+
// Automatically triggered when all tools finish
|
|
112
|
+
console.log('All tools completed:', toolResults);
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
// Easy state management methods
|
|
117
|
+
toolState.addToolCall(toolCall);
|
|
118
|
+
toolState.setToolCallRunning(toolCallId);
|
|
119
|
+
toolState.completeToolCall(toolCallId, result, success);
|
|
120
|
+
toolState.setToolCallError(toolCallId, error);
|
|
121
|
+
|
|
122
|
+
// Query methods
|
|
123
|
+
const status = toolState.getToolCallStatus(toolCallId);
|
|
124
|
+
const hasPending = toolState.hasPendingToolCalls();
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### Key Features:
|
|
128
|
+
- **Automatic Tool Result Sending**: When all tool calls are completed, results are automatically sent via streaming to the agent
|
|
129
|
+
- **Streaming Integration**: Reuses the same streaming logic as regular messages for consistency
|
|
130
|
+
- **Clean State Updates**: Friendlier methods like `setToolCallRunning()`, `completeToolCall()`
|
|
131
|
+
- **Centralized State**: All tool call state in one place with `toolCallStates` Map
|
|
132
|
+
- **Status Tracking**: Each tool call maintains `pending`, `running`, `completed`, `error`, `user_action_required` states
|
|
133
|
+
- **Tool Retriggering**: Tool calls can be retriggered or cancelled through the state management
|
|
134
|
+
- **Completion Callbacks**: `onAllToolsCompleted` triggers when no pending tools remain
|
|
135
|
+
|
|
136
|
+
## Custom Tools
|
|
137
|
+
|
|
138
|
+
You can create your own tools with custom React components:
|
|
139
|
+
|
|
140
|
+
```tsx
|
|
141
|
+
const customTool = {
|
|
142
|
+
name: 'my_custom_tool',
|
|
143
|
+
description: 'Does something custom',
|
|
144
|
+
parameters: {
|
|
145
|
+
type: 'object',
|
|
146
|
+
properties: {
|
|
147
|
+
input: { type: 'string' }
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
handler: async (input: { input: string }) => {
|
|
151
|
+
// Your custom logic here
|
|
152
|
+
return { result: `Processed: ${input.input}` };
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
agent.registerTool(customTool);
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
Then add a custom renderer in `MessageComponents.tsx`:
|
|
160
|
+
|
|
161
|
+
```tsx
|
|
162
|
+
{toolCall.toolCall.tool_name === 'my_custom_tool' && (
|
|
163
|
+
<MyCustomToolComponent
|
|
164
|
+
toolCall={toolCall.toolCall}
|
|
165
|
+
onComplete={(result, success, error) => {
|
|
166
|
+
onCompleteTool?.(toolCall.toolCall.tool_call_id, result, success, error);
|
|
167
|
+
}}
|
|
168
|
+
status={toolCall.status}
|
|
169
|
+
/>
|
|
170
|
+
)}
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
## Components
|
|
174
|
+
|
|
175
|
+
- `EmbeddableChat` - Complete chat interface with built-in tool rendering
|
|
176
|
+
- `FullChat` - Full-page chat with sidebar
|
|
177
|
+
- `AgentSelect` - Dropdown for selecting agents
|
|
178
|
+
- `ChatInput` - Message input component
|
|
179
|
+
|
|
180
|
+
## Hooks
|
|
181
|
+
|
|
182
|
+
- `useAgent` - Manage a single agent
|
|
183
|
+
- `useAgents` - List available agents
|
|
184
|
+
- `useChat` - Chat functionality with automatic tool call state management
|
|
185
|
+
- `useThreads` - Thread management
|
|
186
|
+
- `useTools` - Tool registration and management
|