@sqlrooms/ai-core 0.26.0-rc.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/LICENSE.md +9 -0
- package/README.md +488 -0
- package/dist/AiSlice.d.ts +52 -0
- package/dist/AiSlice.d.ts.map +1 -0
- package/dist/AiSlice.js +367 -0
- package/dist/AiSlice.js.map +1 -0
- package/dist/analysis.d.ts +51 -0
- package/dist/analysis.d.ts.map +1 -0
- package/dist/analysis.js +43 -0
- package/dist/analysis.js.map +1 -0
- package/dist/components/AnalysisAnswer.d.ts +15 -0
- package/dist/components/AnalysisAnswer.d.ts.map +1 -0
- package/dist/components/AnalysisAnswer.js +102 -0
- package/dist/components/AnalysisAnswer.js.map +1 -0
- package/dist/components/AnalysisResult.d.ts +22 -0
- package/dist/components/AnalysisResult.d.ts.map +1 -0
- package/dist/components/AnalysisResult.js +39 -0
- package/dist/components/AnalysisResult.js.map +1 -0
- package/dist/components/AnalysisResultsContainer.d.ts +5 -0
- package/dist/components/AnalysisResultsContainer.d.ts.map +1 -0
- package/dist/components/AnalysisResultsContainer.js +26 -0
- package/dist/components/AnalysisResultsContainer.js.map +1 -0
- package/dist/components/ErrorMessage.d.ts +4 -0
- package/dist/components/ErrorMessage.d.ts.map +1 -0
- package/dist/components/ErrorMessage.js +8 -0
- package/dist/components/ErrorMessage.js.map +1 -0
- package/dist/components/MessageContainer.d.ts +10 -0
- package/dist/components/MessageContainer.d.ts.map +1 -0
- package/dist/components/MessageContainer.js +9 -0
- package/dist/components/MessageContainer.js.map +1 -0
- package/dist/components/ModelSelector.d.ts +13 -0
- package/dist/components/ModelSelector.d.ts.map +1 -0
- package/dist/components/ModelSelector.js +34 -0
- package/dist/components/ModelSelector.js.map +1 -0
- package/dist/components/QueryControls.d.ts +10 -0
- package/dist/components/QueryControls.d.ts.map +1 -0
- package/dist/components/QueryControls.js +52 -0
- package/dist/components/QueryControls.js.map +1 -0
- package/dist/components/SessionControls.d.ts +17 -0
- package/dist/components/SessionControls.d.ts.map +1 -0
- package/dist/components/SessionControls.js +20 -0
- package/dist/components/SessionControls.js.map +1 -0
- package/dist/components/session/DeleteSessionButton.d.ts +19 -0
- package/dist/components/session/DeleteSessionButton.d.ts.map +1 -0
- package/dist/components/session/DeleteSessionButton.js +54 -0
- package/dist/components/session/DeleteSessionButton.js.map +1 -0
- package/dist/components/session/DeleteSessionDialog.d.ts +27 -0
- package/dist/components/session/DeleteSessionDialog.d.ts.map +1 -0
- package/dist/components/session/DeleteSessionDialog.js +19 -0
- package/dist/components/session/DeleteSessionDialog.js.map +1 -0
- package/dist/components/session/SessionActions.d.ts +18 -0
- package/dist/components/session/SessionActions.d.ts.map +1 -0
- package/dist/components/session/SessionActions.js +19 -0
- package/dist/components/session/SessionActions.js.map +1 -0
- package/dist/components/session/SessionDropdown.d.ts +18 -0
- package/dist/components/session/SessionDropdown.d.ts.map +1 -0
- package/dist/components/session/SessionDropdown.js +21 -0
- package/dist/components/session/SessionDropdown.js.map +1 -0
- package/dist/components/session/SessionTitle.d.ts +18 -0
- package/dist/components/session/SessionTitle.d.ts.map +1 -0
- package/dist/components/session/SessionTitle.js +22 -0
- package/dist/components/session/SessionTitle.js.map +1 -0
- package/dist/components/session/SessionType.d.ts +24 -0
- package/dist/components/session/SessionType.d.ts.map +1 -0
- package/dist/components/session/SessionType.js +2 -0
- package/dist/components/session/SessionType.js.map +1 -0
- package/dist/components/session/index.d.ts +7 -0
- package/dist/components/session/index.d.ts.map +1 -0
- package/dist/components/session/index.js +7 -0
- package/dist/components/session/index.js.map +1 -0
- package/dist/components/tools/ToolErrorMessage.d.ts +39 -0
- package/dist/components/tools/ToolErrorMessage.d.ts.map +1 -0
- package/dist/components/tools/ToolErrorMessage.js +20 -0
- package/dist/components/tools/ToolErrorMessage.js.map +1 -0
- package/dist/components/tools/ToolResult.d.ts +11 -0
- package/dist/components/tools/ToolResult.d.ts.map +1 -0
- package/dist/components/tools/ToolResult.js +29 -0
- package/dist/components/tools/ToolResult.js.map +1 -0
- package/dist/components/tools/ToolResultErrorBoundary.d.ts +19 -0
- package/dist/components/tools/ToolResultErrorBoundary.d.ts.map +1 -0
- package/dist/components/tools/ToolResultErrorBoundary.js +24 -0
- package/dist/components/tools/ToolResultErrorBoundary.js.map +1 -0
- package/dist/hasAiSettingsConfig.d.ts +13 -0
- package/dist/hasAiSettingsConfig.d.ts.map +1 -0
- package/dist/hasAiSettingsConfig.js +16 -0
- package/dist/hasAiSettingsConfig.js.map +1 -0
- package/dist/hooks/useScrollToBottom.d.ts +82 -0
- package/dist/hooks/useScrollToBottom.d.ts.map +1 -0
- package/dist/hooks/useScrollToBottom.js +142 -0
- package/dist/hooks/useScrollToBottom.js.map +1 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/utils.d.ts +15 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +31 -0
- package/dist/utils.js.map +1 -0
- package/package.json +51 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright 2025 Ilya Boyandin
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,488 @@
|
|
|
1
|
+
# @sqlrooms/ai
|
|
2
|
+
|
|
3
|
+
An AI integration package for SQLRooms that provides components and utilities for adding AI-powered features to your data applications. This package enables natural language querying, data analysis, and AI-assisted insights.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 🤖 **AI Query Interface**: Natural language to SQL conversion
|
|
8
|
+
- 📊 **Automated Analysis**: AI-powered data analysis and insights
|
|
9
|
+
- 🔄 **State Management**: Zustand-based state management for AI features
|
|
10
|
+
- 🧩 **UI Components**: Ready-to-use components for AI interactions
|
|
11
|
+
- 📝 **Query History**: Track and manage AI query history
|
|
12
|
+
- 🎯 **Tool Integration**: Framework for AI tools and actions
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @sqlrooms/ai
|
|
18
|
+
# or
|
|
19
|
+
yarn add @sqlrooms/ai
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Since version 0.8.2, you will need to install the LLM providers you want to use. For example, to use OpenAI, you can install the `@ai-sdk/openai` package:
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install @ai-sdk/openai
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Google LLM provider:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
npm install @ai-sdk/google
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Anthropic LLM provider:
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
npm install @ai-sdk/anthropic
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
DeepSeek LLM provider:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
npm install @ai-sdk/deepseek
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
XAI LLM provider:
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
npm install @ai-sdk/xai
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
ollama LLM provider:
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
npm install ollama-ai-provider-v2
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Basic Usage
|
|
59
|
+
|
|
60
|
+
### Setting Up AI Integration
|
|
61
|
+
|
|
62
|
+
```tsx
|
|
63
|
+
import {createAiSlice} from '@sqlrooms/ai';
|
|
64
|
+
import {createRoomStore} from '@sqlrooms/room-shell';
|
|
65
|
+
|
|
66
|
+
// Create a room store with AI capabilities
|
|
67
|
+
const {roomStore, useRoomStore} = createRoomStore({
|
|
68
|
+
// Base room configuration
|
|
69
|
+
...createRoomShellSlice({
|
|
70
|
+
config: {
|
|
71
|
+
// Your room configuration
|
|
72
|
+
},
|
|
73
|
+
}),
|
|
74
|
+
// Add AI slice
|
|
75
|
+
...createAiSlice({
|
|
76
|
+
getApiKey: (modelProvider) => {
|
|
77
|
+
// Return API key for the specified model provider
|
|
78
|
+
return process.env.OPENAI_API_KEY || '';
|
|
79
|
+
},
|
|
80
|
+
initialAnalysisPrompt: 'What insights can you provide from my data?',
|
|
81
|
+
// Optional: Add custom tools
|
|
82
|
+
customTools: {
|
|
83
|
+
// Your custom tools
|
|
84
|
+
},
|
|
85
|
+
// Optional: Custom instructions for the AI
|
|
86
|
+
getInstructions: (tablesSchema) => {
|
|
87
|
+
return `Analyze the following tables: ${tablesSchema.map((t) => t.name).join(', ')}`;
|
|
88
|
+
},
|
|
89
|
+
}),
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
function MyApp() {
|
|
93
|
+
return (
|
|
94
|
+
<RoomStateProvider roomStore={roomStore}>
|
|
95
|
+
<MyDataApp />
|
|
96
|
+
</RoomStateProvider>
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### Advanced Store Configuration
|
|
102
|
+
|
|
103
|
+
For more complex applications, you can combine multiple slices:
|
|
104
|
+
|
|
105
|
+
```tsx
|
|
106
|
+
import {createAiSlice} from '@sqlrooms/ai';
|
|
107
|
+
import {
|
|
108
|
+
createSqlEditorSlice,
|
|
109
|
+
createDefaultSqlEditorConfig,
|
|
110
|
+
} from '@sqlrooms/sql-editor';
|
|
111
|
+
import {createRoomStore, createRoomShellSlice} from '@sqlrooms/room-shell';
|
|
112
|
+
|
|
113
|
+
// Define your application state type
|
|
114
|
+
export type RoomState = RoomState<RoomConfig> &
|
|
115
|
+
AiSliceState &
|
|
116
|
+
SqlEditorSliceState;
|
|
117
|
+
|
|
118
|
+
// Create the store with multiple slices
|
|
119
|
+
export const {roomStore, useRoomStore} = createRoomStore<RoomConfig, RoomState>(
|
|
120
|
+
(set, get, store) => ({
|
|
121
|
+
// Base room slice
|
|
122
|
+
...createRoomShellSlice({
|
|
123
|
+
config: {
|
|
124
|
+
...createDefaultSqlEditorConfig(),
|
|
125
|
+
},
|
|
126
|
+
}),
|
|
127
|
+
// AI slice
|
|
128
|
+
...createAiSlice({
|
|
129
|
+
config: {
|
|
130
|
+
// Optional: Pre-configured AI sessions
|
|
131
|
+
sessions: [
|
|
132
|
+
{
|
|
133
|
+
id: 'default-session',
|
|
134
|
+
name: 'Default Analysis',
|
|
135
|
+
modelProvider: 'openai',
|
|
136
|
+
model: 'gpt-4o',
|
|
137
|
+
analysisResults: [],
|
|
138
|
+
createdAt: new Date(),
|
|
139
|
+
},
|
|
140
|
+
],
|
|
141
|
+
currentSessionId: 'default-session',
|
|
142
|
+
}
|
|
143
|
+
getApiKey: (modelProvider) => {
|
|
144
|
+
// Return API key based on provider
|
|
145
|
+
return apiKeys[modelProvider] || '';
|
|
146
|
+
},
|
|
147
|
+
// Custom tools and instructions
|
|
148
|
+
}),
|
|
149
|
+
// SQL Editor slice
|
|
150
|
+
...createSqlEditorSlice(),
|
|
151
|
+
}),
|
|
152
|
+
);
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### Using AI Query Controls
|
|
156
|
+
|
|
157
|
+
```tsx
|
|
158
|
+
import {QueryControls} from '@sqlrooms/ai';
|
|
159
|
+
|
|
160
|
+
function AiQueryPanel() {
|
|
161
|
+
return (
|
|
162
|
+
<div className="rounded-lg border p-4">
|
|
163
|
+
<h2 className="mb-4 text-xl font-bold">Ask AI</h2>
|
|
164
|
+
<QueryControls
|
|
165
|
+
placeholder="Ask a question about your data..."
|
|
166
|
+
onSubmit={(query) => console.log('Processing query:', query)}
|
|
167
|
+
/>
|
|
168
|
+
</div>
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
### Displaying Analysis Results
|
|
174
|
+
|
|
175
|
+
```tsx
|
|
176
|
+
import {AnalysisResultsContainer, AnalysisResult} from '@sqlrooms/ai';
|
|
177
|
+
|
|
178
|
+
function AnalysisPanel() {
|
|
179
|
+
// Get the current session and its analysis results
|
|
180
|
+
const currentSession = useRoomStore((state) => state.ai.getCurrentSession());
|
|
181
|
+
const analysisResults = currentSession?.analysisResults || [];
|
|
182
|
+
|
|
183
|
+
return (
|
|
184
|
+
<div className="rounded-lg border p-4">
|
|
185
|
+
<h2 className="mb-4 text-xl font-bold">AI Analysis</h2>
|
|
186
|
+
<AnalysisResultsContainer>
|
|
187
|
+
{analysisResults.map((result) => (
|
|
188
|
+
<AnalysisResult key={result.id} result={result} />
|
|
189
|
+
))}
|
|
190
|
+
</AnalysisResultsContainer>
|
|
191
|
+
</div>
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
### Working with AI State
|
|
197
|
+
|
|
198
|
+
```tsx
|
|
199
|
+
function AiStatusIndicator() {
|
|
200
|
+
const isRunningAnalysis = useRoomStore((state) => state.ai.isRunningAnalysis);
|
|
201
|
+
const analysisPrompt = useRoomStore((state) => state.ai.analysisPrompt);
|
|
202
|
+
const currentSession = useRoomStore((state) => state.ai.getCurrentSession());
|
|
203
|
+
const lastResult =
|
|
204
|
+
currentSession?.analysisResults[currentSession.analysisResults.length - 1];
|
|
205
|
+
|
|
206
|
+
if (isRunningAnalysis) {
|
|
207
|
+
return <div>AI is analyzing your data...</div>;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (lastResult?.errorMessage) {
|
|
211
|
+
return <div>Error: {lastResult.errorMessage.message}</div>;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
if (analysisPrompt) {
|
|
215
|
+
return <div>Last query: "{analysisPrompt}"</div>;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return <div>Ask AI a question about your data</div>;
|
|
219
|
+
}
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
## AiSlice API Reference
|
|
223
|
+
|
|
224
|
+
The AiSlice provides a comprehensive set of state fields and methods for managing AI interactions in your application.
|
|
225
|
+
|
|
226
|
+
### State Fields
|
|
227
|
+
|
|
228
|
+
#### `analysisPrompt`
|
|
229
|
+
|
|
230
|
+
The current prompt text entered by the user for analysis.
|
|
231
|
+
|
|
232
|
+
```tsx
|
|
233
|
+
const prompt = useRoomStore((state) => state.ai.analysisPrompt);
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
#### `isRunningAnalysis`
|
|
237
|
+
|
|
238
|
+
Boolean flag indicating whether an analysis is currently in progress.
|
|
239
|
+
|
|
240
|
+
```tsx
|
|
241
|
+
const isRunning = useRoomStore((state) => state.ai.isRunningAnalysis);
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
#### `tools`
|
|
245
|
+
|
|
246
|
+
Record of available AI tools that can be used during analysis.
|
|
247
|
+
|
|
248
|
+
```tsx
|
|
249
|
+
const availableTools = useRoomStore((state) => state.ai.tools);
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
#### `analysisAbortController`
|
|
253
|
+
|
|
254
|
+
Optional AbortController instance that can be used to cancel an ongoing analysis.
|
|
255
|
+
|
|
256
|
+
```tsx
|
|
257
|
+
const abortController = useRoomStore(
|
|
258
|
+
(state) => state.ai.analysisAbortController,
|
|
259
|
+
);
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
### Methods
|
|
263
|
+
|
|
264
|
+
#### `setAnalysisPrompt(prompt: string)`
|
|
265
|
+
|
|
266
|
+
Sets the current analysis prompt text.
|
|
267
|
+
|
|
268
|
+
```tsx
|
|
269
|
+
const setPrompt = useRoomStore((state) => state.ai.setAnalysisPrompt);
|
|
270
|
+
setPrompt('Analyze sales trends for the last quarter');
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
#### `startAnalysis()`
|
|
274
|
+
|
|
275
|
+
Starts the analysis process using the current prompt.
|
|
276
|
+
|
|
277
|
+
```tsx
|
|
278
|
+
const startAnalysis = useRoomStore((state) => state.ai.startAnalysis);
|
|
279
|
+
await startAnalysis();
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
#### `cancelAnalysis()`
|
|
283
|
+
|
|
284
|
+
Cancels any ongoing analysis.
|
|
285
|
+
|
|
286
|
+
```tsx
|
|
287
|
+
const cancelAnalysis = useRoomStore((state) => state.ai.cancelAnalysis);
|
|
288
|
+
cancelAnalysis();
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
#### `setAiModel(modelProvider: string, model: string)`
|
|
292
|
+
|
|
293
|
+
Sets the AI model and provider for the current session.
|
|
294
|
+
|
|
295
|
+
```tsx
|
|
296
|
+
const setModel = useRoomStore((state) => state.ai.setAiModel);
|
|
297
|
+
setModel('openai', 'gpt-4o');
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
#### `createSession(name?: string, modelProvider?: string, model?: string)`
|
|
301
|
+
|
|
302
|
+
Creates a new analysis session with optional name and model settings.
|
|
303
|
+
|
|
304
|
+
```tsx
|
|
305
|
+
const createSession = useRoomStore((state) => state.ai.createSession);
|
|
306
|
+
createSession('Financial Analysis', 'openai', 'gpt-4o');
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
#### `switchSession(sessionId: string)`
|
|
310
|
+
|
|
311
|
+
Switches to a different analysis session by ID.
|
|
312
|
+
|
|
313
|
+
```tsx
|
|
314
|
+
const switchSession = useRoomStore((state) => state.ai.switchSession);
|
|
315
|
+
switchSession('session-123');
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
#### `renameSession(sessionId: string, name: string)`
|
|
319
|
+
|
|
320
|
+
Renames an existing analysis session.
|
|
321
|
+
|
|
322
|
+
```tsx
|
|
323
|
+
const renameSession = useRoomStore((state) => state.ai.renameSession);
|
|
324
|
+
renameSession('session-123', 'Q4 Sales Analysis');
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
#### `deleteSession(sessionId: string)`
|
|
328
|
+
|
|
329
|
+
Deletes an analysis session by ID.
|
|
330
|
+
|
|
331
|
+
```tsx
|
|
332
|
+
const deleteSession = useRoomStore((state) => state.ai.deleteSession);
|
|
333
|
+
deleteSession('session-123');
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
#### `getCurrentSession()`
|
|
337
|
+
|
|
338
|
+
Returns the current active analysis session.
|
|
339
|
+
|
|
340
|
+
```tsx
|
|
341
|
+
const currentSession = useRoomStore((state) => state.ai.getCurrentSession());
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
#### `deleteAnalysisResult(sessionId: string, resultId: string)`
|
|
345
|
+
|
|
346
|
+
Deletes a specific analysis result from a session.
|
|
347
|
+
|
|
348
|
+
```tsx
|
|
349
|
+
const deleteResult = useRoomStore((state) => state.ai.deleteAnalysisResult);
|
|
350
|
+
deleteResult('session-123', 'result-456');
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
#### `findToolComponent(toolName: string)`
|
|
354
|
+
|
|
355
|
+
Finds the React component associated with a specific tool.
|
|
356
|
+
|
|
357
|
+
```tsx
|
|
358
|
+
const ChartComponent = useRoomStore((state) =>
|
|
359
|
+
state.ai.findToolComponent('chart'),
|
|
360
|
+
);
|
|
361
|
+
```
|
|
362
|
+
|
|
363
|
+
## Data Structure
|
|
364
|
+
|
|
365
|
+
The basic data structure of the AI package is:
|
|
366
|
+
|
|
367
|
+
```ts
|
|
368
|
+
ai: {
|
|
369
|
+
sessions: [
|
|
370
|
+
{
|
|
371
|
+
id: defaultSessionId,
|
|
372
|
+
name: 'Default Session',
|
|
373
|
+
modelProvider: 'openai',
|
|
374
|
+
model: 'gpt-4o-mini',
|
|
375
|
+
analysisResults: [],
|
|
376
|
+
createdAt: new Date(),
|
|
377
|
+
},
|
|
378
|
+
],
|
|
379
|
+
currentSessionId: defaultSessionId,
|
|
380
|
+
}
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
Each session has a `analysisResults` which is an array of `AnalysisResult`. Each `AnalysisResult` has the following properties:
|
|
384
|
+
|
|
385
|
+
- `id`: The unique identifier for the analysis result
|
|
386
|
+
- `prompt`: The user prompt that was used to generate the analysis result
|
|
387
|
+
- `streamMessage`: The stream message from the LLM
|
|
388
|
+
- `errorMessage`: The error message from the LLM
|
|
389
|
+
- `isCompleted`: Whether the analysis result has been completed
|
|
390
|
+
|
|
391
|
+
For each user prompt, the LLM will run multiple tools (e.g. `query`, `chart`) and return the result as the `streamMessage`. The structure of the `streamMessage` is as follows:
|
|
392
|
+
|
|
393
|
+
- `text`: the final response from the LLM (streamable)
|
|
394
|
+
- `reasoning`: the reasoning of the LLM (only for reason models)
|
|
395
|
+
- `toolCallMessages`: the message array of the tool calls executed by the LLM
|
|
396
|
+
|
|
397
|
+
Each `toolCallMessages` has the following properties:
|
|
398
|
+
|
|
399
|
+
- `toolName`: the name of the tool
|
|
400
|
+
- `toolCallId`: the id of the tool call
|
|
401
|
+
- `args`: the arguments of the tool call
|
|
402
|
+
- `llmResult`: the result from the execution of the tool, which will be sent back to the LLM as response.
|
|
403
|
+
- `additionalData`: the additional data of the tool, which can be used to pass the output of the tool to next tool call or the component for rendering.
|
|
404
|
+
|
|
405
|
+
## Rendering
|
|
406
|
+
|
|
407
|
+
```text
|
|
408
|
+
|--------------------------------|
|
|
409
|
+
| AnalysisResultsContainer |
|
|
410
|
+
|--------------------------------|
|
|
411
|
+
| |--------------------------| |
|
|
412
|
+
| | AnalysisResult | |
|
|
413
|
+
| | | |
|
|
414
|
+
| | streamMessage | |
|
|
415
|
+
| | | |
|
|
416
|
+
| | |---------------------| | |
|
|
417
|
+
| | | Tools | | |
|
|
418
|
+
| | |---------------------| | |
|
|
419
|
+
| | | |---------------| | | |
|
|
420
|
+
| | | |ToolCallMessage| | | |
|
|
421
|
+
| | | |---------------| | | |
|
|
422
|
+
| | | |---------------| | | |
|
|
423
|
+
| | | |ToolCallMessage| | | |
|
|
424
|
+
| | | |---------------| | | |
|
|
425
|
+
| | | ... | | |
|
|
426
|
+
| | |---------------------| | |
|
|
427
|
+
| | | |
|
|
428
|
+
| | text | |
|
|
429
|
+
| |--------------------------| |
|
|
430
|
+
|--------------------------------|
|
|
431
|
+
```
|
|
432
|
+
|
|
433
|
+
## Tools
|
|
434
|
+
|
|
435
|
+
In AI package, we provide a tool() to allow creating function tool for LLM to use. It is an extension of the `tool` from `vercel ai sdk`, and it supports not only `execute` function, but also `context` object and `component` object:
|
|
436
|
+
|
|
437
|
+
- `execute` needs to return
|
|
438
|
+
- llmResult: the result send back to LLM (no raw data)
|
|
439
|
+
- additionalData: the data will be used by `component` and next `tool`
|
|
440
|
+
- `context`
|
|
441
|
+
- provide e.g. runtime or async data for `execute`
|
|
442
|
+
- `execute` can access `context` via `options.context`
|
|
443
|
+
- `component`
|
|
444
|
+
- use `additionalData` to render a React component for this `tool`
|
|
445
|
+
|
|
446
|
+
For example, the `query` tool is defined as follows:
|
|
447
|
+
|
|
448
|
+
```ts
|
|
449
|
+
const functions = {
|
|
450
|
+
weather: tool({
|
|
451
|
+
description: 'Get the weather in a city from a weather station',
|
|
452
|
+
parameters: z.object({cityName: z.string()}),
|
|
453
|
+
execute: async ({cityName}, options) => {
|
|
454
|
+
const getStation = options.context?.getStation;
|
|
455
|
+
const station = getStation ? await getStation(cityName) : null;
|
|
456
|
+
return {
|
|
457
|
+
llmResult: `The weather in ${cityName} is sunny from weather station ${station}.`,
|
|
458
|
+
additionalData: {
|
|
459
|
+
weather: 'sunny',
|
|
460
|
+
station,
|
|
461
|
+
},
|
|
462
|
+
};
|
|
463
|
+
},
|
|
464
|
+
context: {
|
|
465
|
+
getStation: async (cityName: string) => {
|
|
466
|
+
const stations = {
|
|
467
|
+
'New York': '123',
|
|
468
|
+
'Los Angeles': '456',
|
|
469
|
+
Chicago: '789',
|
|
470
|
+
};
|
|
471
|
+
return stations[cityName];
|
|
472
|
+
},
|
|
473
|
+
},
|
|
474
|
+
component: WeatherStation,
|
|
475
|
+
}),
|
|
476
|
+
};
|
|
477
|
+
```
|
|
478
|
+
|
|
479
|
+
## Advanced Features
|
|
480
|
+
|
|
481
|
+
- **Custom AI Tools**: Define custom tools for AI to use with the tool() function
|
|
482
|
+
- **Multiple Sessions**: Create and manage multiple analysis sessions for different purposes
|
|
483
|
+
- **Model Selection**: Switch between different AI models and providers
|
|
484
|
+
- **Result Management**: Save, delete, and organize analysis results
|
|
485
|
+
- **Conversation Context**: Maintain context across multiple queries in a session
|
|
486
|
+
- **Feedback Loop**: Collect user feedback to improve AI responses
|
|
487
|
+
|
|
488
|
+
For more information, visit the SQLRooms documentation.
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { ExtendedTool } from '@openassistant/utils';
|
|
2
|
+
import { AiSliceConfig, AnalysisSessionSchema } from '@sqlrooms/ai-config';
|
|
3
|
+
import { BaseRoomConfig, RoomState, type StateCreator } from '@sqlrooms/room-store';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
export type AiSliceTool = ExtendedTool<z.ZodTypeAny, unknown, unknown, unknown>;
|
|
6
|
+
export type AiSliceState = {
|
|
7
|
+
ai: {
|
|
8
|
+
config: AiSliceConfig;
|
|
9
|
+
analysisPrompt: string;
|
|
10
|
+
isRunningAnalysis: boolean;
|
|
11
|
+
tools: Record<string, AiSliceTool>;
|
|
12
|
+
analysisAbortController?: AbortController;
|
|
13
|
+
setAnalysisPrompt: (prompt: string) => void;
|
|
14
|
+
startAnalysis: () => Promise<void>;
|
|
15
|
+
cancelAnalysis: () => void;
|
|
16
|
+
setAiModel: (modelProvider: string, model: string) => void;
|
|
17
|
+
createSession: (name?: string, modelProvider?: string, model?: string) => void;
|
|
18
|
+
switchSession: (sessionId: string) => void;
|
|
19
|
+
renameSession: (sessionId: string, name: string) => void;
|
|
20
|
+
deleteSession: (sessionId: string) => void;
|
|
21
|
+
getCurrentSession: () => AnalysisSessionSchema | undefined;
|
|
22
|
+
deleteAnalysisResult: (sessionId: string, resultId: string) => void;
|
|
23
|
+
findToolComponent: (toolName: string) => React.ComponentType | undefined;
|
|
24
|
+
getApiKeyFromSettings: () => string;
|
|
25
|
+
getBaseUrlFromSettings: () => string | undefined;
|
|
26
|
+
getMaxStepsFromSettings: () => number;
|
|
27
|
+
getFullInstructions: () => string;
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Configuration options for creating an AI slice
|
|
32
|
+
*/
|
|
33
|
+
export interface AiSliceOptions {
|
|
34
|
+
config?: Partial<AiSliceConfig>;
|
|
35
|
+
/** Initial prompt to display in the analysis input */
|
|
36
|
+
initialAnalysisPrompt?: string;
|
|
37
|
+
/** Tools to add to the AI assistant */
|
|
38
|
+
tools: Record<string, AiSliceTool>;
|
|
39
|
+
/**
|
|
40
|
+
* Function to get custom instructions for the AI assistant
|
|
41
|
+
* @returns The instructions string to use
|
|
42
|
+
*/
|
|
43
|
+
getInstructions: () => string;
|
|
44
|
+
defaultProvider?: string;
|
|
45
|
+
defaultModel?: string;
|
|
46
|
+
maxSteps?: number;
|
|
47
|
+
getApiKey?: (modelProvider: string) => string;
|
|
48
|
+
getBaseUrl?: () => string;
|
|
49
|
+
}
|
|
50
|
+
export declare function createAiSlice<PC extends BaseRoomConfig>(params: AiSliceOptions): StateCreator<AiSliceState>;
|
|
51
|
+
export declare function useStoreWithAi<T, PC extends BaseRoomConfig, S extends RoomState<PC> & AiSliceState>(selector: (state: S) => T): T;
|
|
52
|
+
//# sourceMappingURL=AiSlice.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AiSlice.d.ts","sourceRoot":"","sources":["../src/AiSlice.ts"],"names":[],"mappings":"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"}
|