@sqlrooms/ai 0.16.3 → 0.17.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/README.md +63 -73
- package/dist/AiSlice.d.ts +5 -5
- package/dist/AiSlice.d.ts.map +1 -1
- package/dist/AiSlice.js +2 -2
- package/dist/AiSlice.js.map +1 -1
- package/dist/analysis.d.ts +2 -2
- package/dist/analysis.d.ts.map +1 -1
- package/dist/analysis.js +2 -2
- package/dist/analysis.js.map +1 -1
- package/dist/components/QueryControls.js +1 -1
- package/dist/components/QueryControls.js.map +1 -1
- package/package.json +9 -9
package/README.md
CHANGED
|
@@ -59,14 +59,14 @@ npm install ollama-ai-provider
|
|
|
59
59
|
|
|
60
60
|
```tsx
|
|
61
61
|
import {createAiSlice, createDefaultAiConfig} from '@sqlrooms/ai';
|
|
62
|
-
import {
|
|
62
|
+
import {createRoomStore} from '@sqlrooms/room-shell';
|
|
63
63
|
|
|
64
|
-
// Create a
|
|
65
|
-
const {
|
|
66
|
-
// Base
|
|
67
|
-
...
|
|
64
|
+
// Create a room store with AI capabilities
|
|
65
|
+
const {roomStore, useRoomStore} = createRoomStore({
|
|
66
|
+
// Base room configuration
|
|
67
|
+
...createRoomSlice({
|
|
68
68
|
config: {
|
|
69
|
-
// Your
|
|
69
|
+
// Your room configuration
|
|
70
70
|
...createDefaultAiConfig(), // Default AI configuration
|
|
71
71
|
},
|
|
72
72
|
}),
|
|
@@ -90,9 +90,9 @@ const {projectStore, useProjectStore} = createProjectStore({
|
|
|
90
90
|
|
|
91
91
|
function MyApp() {
|
|
92
92
|
return (
|
|
93
|
-
<
|
|
93
|
+
<RoomStateProvider roomStore={roomStore}>
|
|
94
94
|
<MyDataApp />
|
|
95
|
-
</
|
|
95
|
+
</RoomStateProvider>
|
|
96
96
|
);
|
|
97
97
|
}
|
|
98
98
|
```
|
|
@@ -111,53 +111,49 @@ import {
|
|
|
111
111
|
createSqlEditorSlice,
|
|
112
112
|
createDefaultSqlEditorConfig,
|
|
113
113
|
} from '@sqlrooms/sql-editor';
|
|
114
|
-
import {
|
|
115
|
-
createProjectStore,
|
|
116
|
-
createProjectSlice,
|
|
117
|
-
} from '@sqlrooms/project-builder';
|
|
114
|
+
import {createRoomStore, createRoomSlice} from '@sqlrooms/room-shell';
|
|
118
115
|
|
|
119
116
|
// Define your application state type
|
|
120
|
-
export type
|
|
117
|
+
export type RoomState = RoomState<RoomConfig> &
|
|
121
118
|
AiSliceState &
|
|
122
119
|
SqlEditorSliceState;
|
|
123
120
|
|
|
124
121
|
// Create the store with multiple slices
|
|
125
|
-
export const {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
},
|
|
156
|
-
//
|
|
122
|
+
export const {roomStore, useRoomStore} = createRoomStore<RoomConfig, RoomState>(
|
|
123
|
+
(set, get, store) => ({
|
|
124
|
+
// Base room slice
|
|
125
|
+
...createRoomSlice({
|
|
126
|
+
config: {
|
|
127
|
+
// Your base configuration
|
|
128
|
+
...createDefaultAiConfig({
|
|
129
|
+
// Optional: Pre-configured AI sessions
|
|
130
|
+
sessions: [
|
|
131
|
+
{
|
|
132
|
+
id: 'default-session',
|
|
133
|
+
name: 'Default Analysis',
|
|
134
|
+
modelProvider: 'openai',
|
|
135
|
+
model: 'gpt-4o',
|
|
136
|
+
analysisResults: [],
|
|
137
|
+
createdAt: new Date(),
|
|
138
|
+
},
|
|
139
|
+
],
|
|
140
|
+
currentSessionId: 'default-session',
|
|
141
|
+
}),
|
|
142
|
+
...createDefaultSqlEditorConfig(),
|
|
143
|
+
},
|
|
144
|
+
}),
|
|
145
|
+
// AI slice
|
|
146
|
+
...createAiSlice({
|
|
147
|
+
getApiKey: (modelProvider) => {
|
|
148
|
+
// Return API key based on provider
|
|
149
|
+
return apiKeys[modelProvider] || '';
|
|
150
|
+
},
|
|
151
|
+
// Custom tools and instructions
|
|
152
|
+
}),
|
|
153
|
+
// SQL Editor slice
|
|
154
|
+
...createSqlEditorSlice(),
|
|
157
155
|
}),
|
|
158
|
-
|
|
159
|
-
...createSqlEditorSlice(),
|
|
160
|
-
}));
|
|
156
|
+
);
|
|
161
157
|
```
|
|
162
158
|
|
|
163
159
|
### Using AI Query Controls
|
|
@@ -185,9 +181,7 @@ import {AnalysisResultsContainer, AnalysisResult} from '@sqlrooms/ai';
|
|
|
185
181
|
|
|
186
182
|
function AnalysisPanel() {
|
|
187
183
|
// Get the current session and its analysis results
|
|
188
|
-
const currentSession =
|
|
189
|
-
state.ai.getCurrentSession(),
|
|
190
|
-
);
|
|
184
|
+
const currentSession = useRoomStore((state) => state.ai.getCurrentSession());
|
|
191
185
|
const analysisResults = currentSession?.analysisResults || [];
|
|
192
186
|
|
|
193
187
|
return (
|
|
@@ -207,13 +201,9 @@ function AnalysisPanel() {
|
|
|
207
201
|
|
|
208
202
|
```tsx
|
|
209
203
|
function AiStatusIndicator() {
|
|
210
|
-
const isRunningAnalysis =
|
|
211
|
-
|
|
212
|
-
);
|
|
213
|
-
const analysisPrompt = useProjectStore((state) => state.ai.analysisPrompt);
|
|
214
|
-
const currentSession = useProjectStore((state) =>
|
|
215
|
-
state.ai.getCurrentSession(),
|
|
216
|
-
);
|
|
204
|
+
const isRunningAnalysis = useRoomStore((state) => state.ai.isRunningAnalysis);
|
|
205
|
+
const analysisPrompt = useRoomStore((state) => state.ai.analysisPrompt);
|
|
206
|
+
const currentSession = useRoomStore((state) => state.ai.getCurrentSession());
|
|
217
207
|
const lastResult =
|
|
218
208
|
currentSession?.analysisResults[currentSession.analysisResults.length - 1];
|
|
219
209
|
|
|
@@ -244,7 +234,7 @@ The AiSlice provides a comprehensive set of state fields and methods for managin
|
|
|
244
234
|
The current prompt text entered by the user for analysis.
|
|
245
235
|
|
|
246
236
|
```tsx
|
|
247
|
-
const prompt =
|
|
237
|
+
const prompt = useRoomStore((state) => state.ai.analysisPrompt);
|
|
248
238
|
```
|
|
249
239
|
|
|
250
240
|
#### `isRunningAnalysis`
|
|
@@ -252,7 +242,7 @@ const prompt = useProjectStore((state) => state.ai.analysisPrompt);
|
|
|
252
242
|
Boolean flag indicating whether an analysis is currently in progress.
|
|
253
243
|
|
|
254
244
|
```tsx
|
|
255
|
-
const isRunning =
|
|
245
|
+
const isRunning = useRoomStore((state) => state.ai.isRunningAnalysis);
|
|
256
246
|
```
|
|
257
247
|
|
|
258
248
|
#### `tools`
|
|
@@ -260,7 +250,7 @@ const isRunning = useProjectStore((state) => state.ai.isRunningAnalysis);
|
|
|
260
250
|
Record of available AI tools that can be used during analysis.
|
|
261
251
|
|
|
262
252
|
```tsx
|
|
263
|
-
const availableTools =
|
|
253
|
+
const availableTools = useRoomStore((state) => state.ai.tools);
|
|
264
254
|
```
|
|
265
255
|
|
|
266
256
|
#### `analysisAbortController`
|
|
@@ -268,7 +258,7 @@ const availableTools = useProjectStore((state) => state.ai.tools);
|
|
|
268
258
|
Optional AbortController instance that can be used to cancel an ongoing analysis.
|
|
269
259
|
|
|
270
260
|
```tsx
|
|
271
|
-
const abortController =
|
|
261
|
+
const abortController = useRoomStore(
|
|
272
262
|
(state) => state.ai.analysisAbortController,
|
|
273
263
|
);
|
|
274
264
|
```
|
|
@@ -280,7 +270,7 @@ const abortController = useProjectStore(
|
|
|
280
270
|
Sets the current analysis prompt text.
|
|
281
271
|
|
|
282
272
|
```tsx
|
|
283
|
-
const setPrompt =
|
|
273
|
+
const setPrompt = useRoomStore((state) => state.ai.setAnalysisPrompt);
|
|
284
274
|
setPrompt('Analyze sales trends for the last quarter');
|
|
285
275
|
```
|
|
286
276
|
|
|
@@ -289,7 +279,7 @@ setPrompt('Analyze sales trends for the last quarter');
|
|
|
289
279
|
Starts the analysis process using the current prompt.
|
|
290
280
|
|
|
291
281
|
```tsx
|
|
292
|
-
const startAnalysis =
|
|
282
|
+
const startAnalysis = useRoomStore((state) => state.ai.startAnalysis);
|
|
293
283
|
await startAnalysis();
|
|
294
284
|
```
|
|
295
285
|
|
|
@@ -298,7 +288,7 @@ await startAnalysis();
|
|
|
298
288
|
Cancels any ongoing analysis.
|
|
299
289
|
|
|
300
290
|
```tsx
|
|
301
|
-
const cancelAnalysis =
|
|
291
|
+
const cancelAnalysis = useRoomStore((state) => state.ai.cancelAnalysis);
|
|
302
292
|
cancelAnalysis();
|
|
303
293
|
```
|
|
304
294
|
|
|
@@ -307,7 +297,7 @@ cancelAnalysis();
|
|
|
307
297
|
Sets the AI model and provider for the current session.
|
|
308
298
|
|
|
309
299
|
```tsx
|
|
310
|
-
const setModel =
|
|
300
|
+
const setModel = useRoomStore((state) => state.ai.setAiModel);
|
|
311
301
|
setModel('openai', 'gpt-4o');
|
|
312
302
|
```
|
|
313
303
|
|
|
@@ -316,7 +306,7 @@ setModel('openai', 'gpt-4o');
|
|
|
316
306
|
Creates a new analysis session with optional name and model settings.
|
|
317
307
|
|
|
318
308
|
```tsx
|
|
319
|
-
const createSession =
|
|
309
|
+
const createSession = useRoomStore((state) => state.ai.createSession);
|
|
320
310
|
createSession('Financial Analysis', 'openai', 'gpt-4o');
|
|
321
311
|
```
|
|
322
312
|
|
|
@@ -325,7 +315,7 @@ createSession('Financial Analysis', 'openai', 'gpt-4o');
|
|
|
325
315
|
Switches to a different analysis session by ID.
|
|
326
316
|
|
|
327
317
|
```tsx
|
|
328
|
-
const switchSession =
|
|
318
|
+
const switchSession = useRoomStore((state) => state.ai.switchSession);
|
|
329
319
|
switchSession('session-123');
|
|
330
320
|
```
|
|
331
321
|
|
|
@@ -334,7 +324,7 @@ switchSession('session-123');
|
|
|
334
324
|
Renames an existing analysis session.
|
|
335
325
|
|
|
336
326
|
```tsx
|
|
337
|
-
const renameSession =
|
|
327
|
+
const renameSession = useRoomStore((state) => state.ai.renameSession);
|
|
338
328
|
renameSession('session-123', 'Q4 Sales Analysis');
|
|
339
329
|
```
|
|
340
330
|
|
|
@@ -343,7 +333,7 @@ renameSession('session-123', 'Q4 Sales Analysis');
|
|
|
343
333
|
Deletes an analysis session by ID.
|
|
344
334
|
|
|
345
335
|
```tsx
|
|
346
|
-
const deleteSession =
|
|
336
|
+
const deleteSession = useRoomStore((state) => state.ai.deleteSession);
|
|
347
337
|
deleteSession('session-123');
|
|
348
338
|
```
|
|
349
339
|
|
|
@@ -352,7 +342,7 @@ deleteSession('session-123');
|
|
|
352
342
|
Returns the current active analysis session.
|
|
353
343
|
|
|
354
344
|
```tsx
|
|
355
|
-
const currentSession =
|
|
345
|
+
const currentSession = useRoomStore((state) => state.ai.getCurrentSession());
|
|
356
346
|
```
|
|
357
347
|
|
|
358
348
|
#### `deleteAnalysisResult(sessionId: string, resultId: string)`
|
|
@@ -360,7 +350,7 @@ const currentSession = useProjectStore((state) => state.ai.getCurrentSession());
|
|
|
360
350
|
Deletes a specific analysis result from a session.
|
|
361
351
|
|
|
362
352
|
```tsx
|
|
363
|
-
const deleteResult =
|
|
353
|
+
const deleteResult = useRoomStore((state) => state.ai.deleteAnalysisResult);
|
|
364
354
|
deleteResult('session-123', 'result-456');
|
|
365
355
|
```
|
|
366
356
|
|
|
@@ -369,7 +359,7 @@ deleteResult('session-123', 'result-456');
|
|
|
369
359
|
Finds the React component associated with a specific tool.
|
|
370
360
|
|
|
371
361
|
```tsx
|
|
372
|
-
const ChartComponent =
|
|
362
|
+
const ChartComponent = useRoomStore((state) =>
|
|
373
363
|
state.ai.findToolComponent('chart'),
|
|
374
364
|
);
|
|
375
365
|
```
|
package/dist/AiSlice.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ExtendedTool } from '@openassistant/core';
|
|
2
2
|
import { DataTable } from '@sqlrooms/duckdb';
|
|
3
|
-
import {
|
|
3
|
+
import { RoomShellSliceState, type StateCreator, BaseRoomConfig } from '@sqlrooms/room-shell';
|
|
4
4
|
import { z } from 'zod';
|
|
5
5
|
import { AnalysisSessionSchema } from './schemas';
|
|
6
6
|
export declare const AiSliceConfig: z.ZodObject<{
|
|
@@ -540,7 +540,7 @@ export type AiSliceState = {
|
|
|
540
540
|
findToolComponent: (toolName: string) => React.ComponentType | undefined;
|
|
541
541
|
};
|
|
542
542
|
};
|
|
543
|
-
export declare function createAiSlice<PC extends
|
|
543
|
+
export declare function createAiSlice<PC extends BaseRoomConfig & AiSliceConfig>({ getApiKey, initialAnalysisPrompt, customTools, getInstructions, }: {
|
|
544
544
|
getApiKey: (modelProvider: string) => string;
|
|
545
545
|
initialAnalysisPrompt?: string;
|
|
546
546
|
customTools?: Record<string, AiSliceTool>;
|
|
@@ -551,8 +551,8 @@ export declare function createAiSlice<PC extends BaseProjectConfig & AiSliceConf
|
|
|
551
551
|
*/
|
|
552
552
|
getInstructions?: (tablesSchema: DataTable[]) => string;
|
|
553
553
|
}): StateCreator<AiSliceState>;
|
|
554
|
-
type
|
|
555
|
-
type
|
|
556
|
-
export declare function useStoreWithAi<T>(selector: (state:
|
|
554
|
+
type RoomConfigWithAi = BaseRoomConfig & AiSliceConfig;
|
|
555
|
+
type RoomShellSliceStateWithAi = RoomShellSliceState<RoomConfigWithAi> & AiSliceState;
|
|
556
|
+
export declare function useStoreWithAi<T>(selector: (state: RoomShellSliceStateWithAi) => T): T;
|
|
557
557
|
export {};
|
|
558
558
|
//# sourceMappingURL=AiSlice.d.ts.map
|
package/dist/AiSlice.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AiSlice.d.ts","sourceRoot":"","sources":["../src/AiSlice.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAgB,MAAM,qBAAqB,CAA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AM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;IAC1C;;;;OAIG;IACH,eAAe,CAAC,EAAE,CAAC,YAAY,EAAE,SAAS,EAAE,KAAK,MAAM,CAAC;CACzD,GAAG,YAAY,CAAC,YAAY,CAAC,CAmQ7B;AA+FD,KAAK,gBAAgB,GAAG,cAAc,GAAG,aAAa,CAAC;AACvD,KAAK,yBAAyB,GAAG,mBAAmB,CAAC,gBAAgB,CAAC,GACpE,YAAY,CAAC;AAEf,wBAAgB,cAAc,CAAC,CAAC,EAC9B,QAAQ,EAAE,CAAC,KAAK,EAAE,yBAAyB,KAAK,CAAC,GAChD,CAAC,CAMH"}
|
package/dist/AiSlice.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createId } from '@paralleldrive/cuid2';
|
|
2
|
-
import { createSlice,
|
|
2
|
+
import { createSlice, useBaseRoomShellStore, } from '@sqlrooms/room-shell';
|
|
3
3
|
import { produce } from 'immer';
|
|
4
4
|
import { z } from 'zod';
|
|
5
5
|
import { getDefaultTools, runAnalysis } from './analysis';
|
|
@@ -305,6 +305,6 @@ function makeResultsAppender({ resultId, streamMessage, errorMessage, isComplete
|
|
|
305
305
|
});
|
|
306
306
|
}
|
|
307
307
|
export function useStoreWithAi(selector) {
|
|
308
|
-
return
|
|
308
|
+
return useBaseRoomShellStore((state) => selector(state));
|
|
309
309
|
}
|
|
310
310
|
//# sourceMappingURL=AiSlice.js.map
|
package/dist/AiSlice.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AiSlice.js","sourceRoot":"","sources":["../src/AiSlice.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,QAAQ,EAAC,MAAM,sBAAsB,CAAC;AAE9C,OAAO,EACL,WAAW,EAEX,0BAA0B,GAG3B,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAC,OAAO,EAAgB,MAAM,OAAO,CAAC;AAC7C,OAAO,EAAC,CAAC,EAAC,MAAM,KAAK,C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C,CAAC;IACH,CAAC,CAAC,CAAC;AACP,CAAC;AAMD,MAAM,UAAU,cAAc,CAC5B,QAAiD;IAEjD,OAAO,0BAA0B,CAI/B,CAAC,KAAK,EAAE,EAAE,CAAC,QAAQ,CAAC,KAA6C,CAAC,CAAC,CAAC;AACxE,CAAC","sourcesContent":["import {ExtendedTool, StreamMessage} from '@openassistant/core';\nimport {createId} from '@paralleldrive/cuid2';\nimport {DataTable} from '@sqlrooms/duckdb';\nimport {\n createSlice,\n ProjectBuilderState,\n useBaseProjectBuilderStore,\n type StateCreator,\n BaseProjectConfig,\n} from '@sqlrooms/project-builder';\nimport {produce, WritableDraft} from 'immer';\nimport {z} from 'zod';\nimport {getDefaultTools, runAnalysis} from './analysis';\nimport {\n AnalysisResultSchema,\n AnalysisSessionSchema,\n ErrorMessageSchema,\n} from './schemas';\n\nexport const AiSliceConfig = z.object({\n ai: z.object({\n sessions: z.array(AnalysisSessionSchema),\n currentSessionId: z.string().optional(),\n }),\n});\nexport type AiSliceConfig = z.infer<typeof AiSliceConfig>;\n\nexport function createDefaultAiConfig(\n props: Partial<AiSliceConfig['ai']>,\n): AiSliceConfig {\n const defaultSessionId = createId();\n return {\n ai: {\n sessions: [\n {\n id: defaultSessionId,\n name: 'Default Session',\n modelProvider: 'openai',\n model: 'gpt-4o-mini',\n analysisResults: [],\n createdAt: new Date(),\n },\n ],\n currentSessionId: defaultSessionId,\n ...props,\n },\n };\n}\n\nexport type AiSliceTool = ExtendedTool<any>;\n\nexport type AiSliceState = {\n ai: {\n analysisPrompt: string;\n isRunningAnalysis: boolean;\n tools: Record<string, AiSliceTool>;\n analysisAbortController?: AbortController;\n setAnalysisPrompt: (prompt: string) => void;\n startAnalysis: () => Promise<void>;\n cancelAnalysis: () => void;\n setAiModel: (modelProvider: string, model: string) => void;\n createSession: (\n name?: string,\n modelProvider?: string,\n model?: string,\n ) => void;\n switchSession: (sessionId: string) => void;\n renameSession: (sessionId: string, name: string) => void;\n deleteSession: (sessionId: string) => void;\n getCurrentSession: () => AnalysisSessionSchema | undefined;\n deleteAnalysisResult: (sessionId: string, resultId: string) => void;\n findToolComponent: (toolName: string) => React.ComponentType | undefined;\n };\n};\n\nexport function createAiSlice<PC extends BaseProjectConfig & AiSliceConfig>({\n getApiKey,\n initialAnalysisPrompt = '',\n customTools = {},\n getInstructions,\n}: {\n getApiKey: (modelProvider: string) => string;\n initialAnalysisPrompt?: string;\n customTools?: Record<string, AiSliceTool>;\n /**\n * Function to get custom instructions for the AI assistant\n * @param tablesSchema - The schema of the tables in the database\n * @returns The instructions string to use\n */\n getInstructions?: (tablesSchema: DataTable[]) => string;\n}): StateCreator<AiSliceState> {\n return createSlice<PC, AiSliceState>((set, get, store) => {\n return {\n ai: {\n analysisPrompt: initialAnalysisPrompt,\n isRunningAnalysis: false,\n\n tools: {\n ...getDefaultTools(store),\n ...customTools,\n },\n\n setAnalysisPrompt: (prompt: string) => {\n set((state) =>\n produce(state, (draft) => {\n draft.ai.analysisPrompt = prompt;\n }),\n );\n },\n\n /**\n * Set the AI model for the current session\n * @param model - The model to set\n */\n setAiModel: (modelProvider: string, model: string) => {\n set((state) =>\n produce(state, (draft) => {\n const currentSession = getCurrentSessionFromState(draft);\n if (currentSession) {\n currentSession.modelProvider = modelProvider;\n currentSession.model = model;\n }\n }),\n );\n },\n\n /**\n * Get the current active session\n */\n getCurrentSession: () => {\n const state = get();\n const {currentSessionId, sessions} = state.config.ai;\n return sessions.find((session) => session.id === currentSessionId);\n },\n\n /**\n * Create a new session with the given name and model settings\n */\n createSession: (\n name?: string,\n modelProvider?: string,\n model?: string,\n ) => {\n const currentSession = get().ai.getCurrentSession();\n const newSessionId = createId();\n\n // Generate a default name if none is provided\n let sessionName = name;\n if (!sessionName) {\n // Generate a human-readable date and time for the session name\n const now = new Date();\n const formattedDate = now.toLocaleDateString('en-US', {\n month: 'short',\n day: 'numeric',\n year: 'numeric',\n });\n const formattedTime = now.toLocaleTimeString('en-US', {\n hour: 'numeric',\n minute: 'numeric',\n hour12: true,\n });\n sessionName = `Session ${formattedDate} at ${formattedTime}`;\n }\n\n set((state) =>\n produce(state, (draft) => {\n draft.config.ai.sessions.unshift({\n id: newSessionId,\n name: sessionName,\n modelProvider:\n modelProvider || currentSession?.modelProvider || 'openai',\n model: model || currentSession?.model || 'gpt-4o-mini',\n analysisResults: [],\n createdAt: new Date(),\n });\n draft.config.ai.currentSessionId = newSessionId;\n }),\n );\n },\n\n /**\n * Switch to a different session\n */\n switchSession: (sessionId: string) => {\n set((state) =>\n produce(state, (draft) => {\n draft.config.ai.currentSessionId = sessionId;\n }),\n );\n },\n\n /**\n * Rename an existing session\n */\n renameSession: (sessionId: string, name: string) => {\n set((state) =>\n produce(state, (draft) => {\n const session = draft.config.ai.sessions.find(\n (s) => s.id === sessionId,\n );\n if (session) {\n session.name = name;\n }\n }),\n );\n },\n\n /**\n * Delete a session\n */\n deleteSession: (sessionId: string) => {\n set((state) =>\n produce(state, (draft) => {\n const sessionIndex = draft.config.ai.sessions.findIndex(\n (s) => s.id === sessionId,\n );\n if (sessionIndex !== -1) {\n // Don't delete the last session\n if (draft.config.ai.sessions.length > 1) {\n draft.config.ai.sessions.splice(sessionIndex, 1);\n // If we deleted the current session, switch to another one\n if (draft.config.ai.currentSessionId === sessionId) {\n // Make sure there's at least one session before accessing its id\n if (draft.config.ai.sessions.length > 0) {\n const firstSession = draft.config.ai.sessions[0];\n if (firstSession) {\n draft.config.ai.currentSessionId = firstSession.id;\n }\n }\n }\n }\n }\n }),\n );\n },\n\n /**\n * Start the analysis\n * TODO: how to pass the history analysisResults?\n */\n startAnalysis: async () => {\n const resultId = createId();\n const abortController = new AbortController();\n const currentSession = get().ai.getCurrentSession();\n\n if (!currentSession) {\n console.error('No current session found');\n return;\n }\n\n set((state) =>\n produce(state, (draft) => {\n draft.ai.analysisAbortController = abortController;\n draft.ai.isRunningAnalysis = true;\n\n const session = draft.config.ai.sessions.find(\n (s) => s.id === draft.config.ai.currentSessionId,\n );\n\n if (session) {\n session.analysisResults.push({\n id: resultId,\n prompt: get().ai.analysisPrompt,\n streamMessage: {\n toolCallMessages: [],\n reasoning: '',\n text: '',\n },\n isCompleted: false,\n });\n }\n }),\n );\n\n try {\n await runAnalysis({\n tableSchemas: get().db.tables,\n modelProvider: currentSession.modelProvider || 'openai',\n model: currentSession.model || 'gpt-4o-mini',\n apiKey: getApiKey(currentSession.modelProvider || 'openai'),\n prompt: get().ai.analysisPrompt,\n abortController,\n tools: get().ai.tools,\n getInstructions,\n onStreamResult: (isCompleted, streamMessage) => {\n set(\n makeResultsAppender({\n resultId,\n streamMessage,\n isCompleted,\n }),\n );\n },\n });\n } catch (err) {\n set(\n makeResultsAppender({\n resultId,\n isCompleted: true,\n errorMessage: {\n error: err instanceof Error ? err.message : String(err),\n },\n }),\n );\n } finally {\n set((state) =>\n produce(state, (draft) => {\n draft.ai.isRunningAnalysis = false;\n draft.ai.analysisPrompt = '';\n }),\n );\n }\n },\n\n cancelAnalysis: () => {\n set((state) =>\n produce(state, (draft) => {\n draft.ai.isRunningAnalysis = false;\n }),\n );\n get().ai.analysisAbortController?.abort('Analysis cancelled');\n },\n\n /**\n * Delete an analysis result from a session\n */\n deleteAnalysisResult: (sessionId: string, resultId: string) => {\n set((state) =>\n produce(state, (draft) => {\n const session = draft.config.ai.sessions.find(\n (s) => s.id === sessionId,\n );\n if (session) {\n session.analysisResults = session.analysisResults.filter(\n (r) => r.id !== resultId,\n );\n }\n }),\n );\n },\n\n findToolComponent: (toolName: string) => {\n return Object.entries(get().ai.tools).find(\n ([name]) => name === toolName,\n )?.[1]?.component as React.ComponentType;\n },\n },\n };\n });\n}\n\n/**\n * Helper function to get the current session from state\n */\nfunction getCurrentSessionFromState<\n PC extends BaseProjectConfig & AiSliceConfig,\n>(\n state: ProjectBuilderState<PC> | WritableDraft<ProjectBuilderState<PC>>,\n): AnalysisSessionSchema | undefined {\n const {currentSessionId, sessions} = state.config.ai;\n return sessions.find(\n (session: AnalysisSessionSchema) => session.id === currentSessionId,\n );\n}\n\nfunction findResultById(analysisResults: AnalysisResultSchema[], id: string) {\n return analysisResults.find((r: AnalysisResultSchema) => r.id === id);\n}\n\n/**\n * Appends the tool results, tool calls, and analysis to the state\n *\n * @param resultId - The id of the result to append to\n * @param message - The message to append to the state. The structure of the message is defined as:\n * - reasoning: string The reasoning of the assistant\n * - toolCallMessages: ToolCallMessage[] The tool call messages\n * - text: string The final text message\n * @param isCompleted - Whether the analysis is completed\n * @returns The new state\n */\nfunction makeResultsAppender<PC extends BaseProjectConfig & AiSliceConfig>({\n resultId,\n streamMessage,\n errorMessage,\n isCompleted,\n}: {\n resultId: string;\n streamMessage?: StreamMessage;\n errorMessage?: ErrorMessageSchema;\n isCompleted?: boolean;\n}) {\n return (state: ProjectBuilderState<PC>) =>\n produce(state, (draft) => {\n const currentSession = getCurrentSessionFromState(draft);\n if (!currentSession) {\n console.error('No current session found');\n return;\n }\n\n const result = findResultById(currentSession.analysisResults, resultId);\n if (result) {\n if (streamMessage) {\n result.streamMessage = {\n toolCallMessages: (streamMessage.toolCallMessages || []).map(\n (toolCall) => ({\n args: {...toolCall.args},\n isCompleted: toolCall.isCompleted,\n llmResult: toolCall.llmResult,\n additionalData: toolCall.additionalData,\n text: toolCall.text,\n toolCallId: toolCall.toolCallId,\n toolName: toolCall.toolName,\n }),\n ),\n reasoning: streamMessage.reasoning,\n text: streamMessage.text,\n analysis: streamMessage.analysis,\n parts: streamMessage.parts?.map((part) => ({\n ...part,\n ...(part.type === 'text' && {text: part.text}),\n ...(part.type === 'tool' && {\n toolCallMessages: part.toolCallMessages?.map((toolCall) => ({\n args: {...toolCall.args},\n isCompleted: toolCall.isCompleted,\n llmResult: toolCall.llmResult,\n additionalData: toolCall.additionalData,\n text: toolCall.text,\n toolCallId: toolCall.toolCallId,\n toolName: toolCall.toolName,\n })),\n }),\n })),\n };\n }\n if (errorMessage) {\n result.errorMessage = errorMessage;\n }\n if (isCompleted) {\n result.isCompleted = isCompleted;\n }\n } else {\n console.error('Result not found', resultId);\n }\n });\n}\n\ntype ProjectConfigWithAi = BaseProjectConfig & AiSliceConfig;\ntype ProjectBuilderStateWithAi = ProjectBuilderState<ProjectConfigWithAi> &\n AiSliceState;\n\nexport function useStoreWithAi<T>(\n selector: (state: ProjectBuilderStateWithAi) => T,\n): T {\n return useBaseProjectBuilderStore<\n BaseProjectConfig & AiSliceConfig,\n ProjectBuilderState<ProjectConfigWithAi>,\n T\n >((state) => selector(state as unknown as ProjectBuilderStateWithAi));\n}\n"]}
|
|
1
|
+
{"version":3,"file":"AiSlice.js","sourceRoot":"","sources":["../src/AiSlice.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,QAAQ,EAAC,MAAM,sBAAsB,CAAC;AAE9C,OAAO,EACL,WAAW,EAEX,qBAAqB,GAGtB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAC,OAAO,EAAgB,MAAM,OAAO,CAAC;AAC7C,OAAO,EAAC,CAAC,EAAC,MAAM,KAAK,CAAC;AACtB,OAAO,EAAC,eAAe,EAAE,WAAW,EAAC,MAAM,YAAY,CAAC;AACxD,OAAO,EAEL,qBAAqB,GAEtB,MAAM,WAAW,CAAC;AAEnB,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,CAAC,MAAM,CAAC;IACpC,EAAE,EAAE,CAAC,CAAC,MAAM,CAAC;QACX,QAAQ,EAAE,CAAC,CAAC,KAAK,CAAC,qBAAqB,CAAC;QACxC,gBAAgB,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;KACxC,CAAC;CACH,CAAC,CAAC;AAGH,MAAM,UAAU,qBAAqB,CACnC,KAAmC;IAEnC,MAAM,gBAAgB,GAAG,QAAQ,EAAE,CAAC;IACpC,OAAO;QACL,EAAE,EAAE;YACF,QAAQ,EAAE;gBACR;oBACE,EAAE,EAAE,gBAAgB;oBACpB,IAAI,EAAE,iBAAiB;oBACvB,aAAa,EAAE,QAAQ;oBACvB,KAAK,EAAE,aAAa;oBACp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,EAAC,GAAG,QAAQ,CAAC,IAAI,EAAC;gCACxB,WAAW,EAAE,QAAQ,CAAC,WAAW;gCACjC,SAAS,EAAE,QAAQ,CAAC,SAAS;gCAC7B,cAAc,EAAE,QAAQ,CAAC,cAAc;gCACvC,IAAI,EAAE,QAAQ,CAAC,IAAI;gCACnB,UAAU,EAAE,QAAQ,CAAC,UAAU;gCAC/B,QAAQ,EAAE,QAAQ,CAAC,QAAQ;6BAC5B,CAAC,CAAC;yBACJ,CAAC;qBACH,CAAC,CAAC;iBACJ,CAAC;YACJ,CAAC;YACD,IAAI,YAAY,EAAE,CAAC;gBACjB,MAAM,CAAC,YAAY,GAAG,YAAY,CAAC;YACrC,CAAC;YACD,IAAI,WAAW,EAAE,CAAC;gBAChB,MAAM,CAAC,WAAW,GAAG,WAAW,CAAC;YACnC,CAAC;QACH,CAAC;aAAM,CAAC;YACN,OAAO,CAAC,KAAK,CAAC,kBAAkB,EAAE,QAAQ,CAAC,CAAC;QAC9C,CAAC;IACH,CAAC,CAAC,CAAC;AACP,CAAC;AAMD,MAAM,UAAU,cAAc,CAC5B,QAAiD;IAEjD,OAAO,qBAAqB,CAI1B,CAAC,KAAK,EAAE,EAAE,CAAC,QAAQ,CAAC,KAA6C,CAAC,CAAC,CAAC;AACxE,CAAC","sourcesContent":["import {ExtendedTool, StreamMessage} from '@openassistant/core';\nimport {createId} from '@paralleldrive/cuid2';\nimport {DataTable} from '@sqlrooms/duckdb';\nimport {\n createSlice,\n RoomShellSliceState,\n useBaseRoomShellStore,\n type StateCreator,\n BaseRoomConfig,\n} from '@sqlrooms/room-shell';\nimport {produce, WritableDraft} from 'immer';\nimport {z} from 'zod';\nimport {getDefaultTools, runAnalysis} from './analysis';\nimport {\n AnalysisResultSchema,\n AnalysisSessionSchema,\n ErrorMessageSchema,\n} from './schemas';\n\nexport const AiSliceConfig = z.object({\n ai: z.object({\n sessions: z.array(AnalysisSessionSchema),\n currentSessionId: z.string().optional(),\n }),\n});\nexport type AiSliceConfig = z.infer<typeof AiSliceConfig>;\n\nexport function createDefaultAiConfig(\n props: Partial<AiSliceConfig['ai']>,\n): AiSliceConfig {\n const defaultSessionId = createId();\n return {\n ai: {\n sessions: [\n {\n id: defaultSessionId,\n name: 'Default Session',\n modelProvider: 'openai',\n model: 'gpt-4o-mini',\n analysisResults: [],\n createdAt: new Date(),\n },\n ],\n currentSessionId: defaultSessionId,\n ...props,\n },\n };\n}\n\nexport type AiSliceTool = ExtendedTool<any>;\n\nexport type AiSliceState = {\n ai: {\n analysisPrompt: string;\n isRunningAnalysis: boolean;\n tools: Record<string, AiSliceTool>;\n analysisAbortController?: AbortController;\n setAnalysisPrompt: (prompt: string) => void;\n startAnalysis: () => Promise<void>;\n cancelAnalysis: () => void;\n setAiModel: (modelProvider: string, model: string) => void;\n createSession: (\n name?: string,\n modelProvider?: string,\n model?: string,\n ) => void;\n switchSession: (sessionId: string) => void;\n renameSession: (sessionId: string, name: string) => void;\n deleteSession: (sessionId: string) => void;\n getCurrentSession: () => AnalysisSessionSchema | undefined;\n deleteAnalysisResult: (sessionId: string, resultId: string) => void;\n findToolComponent: (toolName: string) => React.ComponentType | undefined;\n };\n};\n\nexport function createAiSlice<PC extends BaseRoomConfig & AiSliceConfig>({\n getApiKey,\n initialAnalysisPrompt = '',\n customTools = {},\n getInstructions,\n}: {\n getApiKey: (modelProvider: string) => string;\n initialAnalysisPrompt?: string;\n customTools?: Record<string, AiSliceTool>;\n /**\n * Function to get custom instructions for the AI assistant\n * @param tablesSchema - The schema of the tables in the database\n * @returns The instructions string to use\n */\n getInstructions?: (tablesSchema: DataTable[]) => string;\n}): StateCreator<AiSliceState> {\n return createSlice<PC, AiSliceState>((set, get, store) => {\n return {\n ai: {\n analysisPrompt: initialAnalysisPrompt,\n isRunningAnalysis: false,\n\n tools: {\n ...getDefaultTools(store),\n ...customTools,\n },\n\n setAnalysisPrompt: (prompt: string) => {\n set((state) =>\n produce(state, (draft) => {\n draft.ai.analysisPrompt = prompt;\n }),\n );\n },\n\n /**\n * Set the AI model for the current session\n * @param model - The model to set\n */\n setAiModel: (modelProvider: string, model: string) => {\n set((state) =>\n produce(state, (draft) => {\n const currentSession = getCurrentSessionFromState(draft);\n if (currentSession) {\n currentSession.modelProvider = modelProvider;\n currentSession.model = model;\n }\n }),\n );\n },\n\n /**\n * Get the current active session\n */\n getCurrentSession: () => {\n const state = get();\n const {currentSessionId, sessions} = state.config.ai;\n return sessions.find((session) => session.id === currentSessionId);\n },\n\n /**\n * Create a new session with the given name and model settings\n */\n createSession: (\n name?: string,\n modelProvider?: string,\n model?: string,\n ) => {\n const currentSession = get().ai.getCurrentSession();\n const newSessionId = createId();\n\n // Generate a default name if none is provided\n let sessionName = name;\n if (!sessionName) {\n // Generate a human-readable date and time for the session name\n const now = new Date();\n const formattedDate = now.toLocaleDateString('en-US', {\n month: 'short',\n day: 'numeric',\n year: 'numeric',\n });\n const formattedTime = now.toLocaleTimeString('en-US', {\n hour: 'numeric',\n minute: 'numeric',\n hour12: true,\n });\n sessionName = `Session ${formattedDate} at ${formattedTime}`;\n }\n\n set((state) =>\n produce(state, (draft) => {\n draft.config.ai.sessions.unshift({\n id: newSessionId,\n name: sessionName,\n modelProvider:\n modelProvider || currentSession?.modelProvider || 'openai',\n model: model || currentSession?.model || 'gpt-4o-mini',\n analysisResults: [],\n createdAt: new Date(),\n });\n draft.config.ai.currentSessionId = newSessionId;\n }),\n );\n },\n\n /**\n * Switch to a different session\n */\n switchSession: (sessionId: string) => {\n set((state) =>\n produce(state, (draft) => {\n draft.config.ai.currentSessionId = sessionId;\n }),\n );\n },\n\n /**\n * Rename an existing session\n */\n renameSession: (sessionId: string, name: string) => {\n set((state) =>\n produce(state, (draft) => {\n const session = draft.config.ai.sessions.find(\n (s) => s.id === sessionId,\n );\n if (session) {\n session.name = name;\n }\n }),\n );\n },\n\n /**\n * Delete a session\n */\n deleteSession: (sessionId: string) => {\n set((state) =>\n produce(state, (draft) => {\n const sessionIndex = draft.config.ai.sessions.findIndex(\n (s) => s.id === sessionId,\n );\n if (sessionIndex !== -1) {\n // Don't delete the last session\n if (draft.config.ai.sessions.length > 1) {\n draft.config.ai.sessions.splice(sessionIndex, 1);\n // If we deleted the current session, switch to another one\n if (draft.config.ai.currentSessionId === sessionId) {\n // Make sure there's at least one session before accessing its id\n if (draft.config.ai.sessions.length > 0) {\n const firstSession = draft.config.ai.sessions[0];\n if (firstSession) {\n draft.config.ai.currentSessionId = firstSession.id;\n }\n }\n }\n }\n }\n }),\n );\n },\n\n /**\n * Start the analysis\n * TODO: how to pass the history analysisResults?\n */\n startAnalysis: async () => {\n const resultId = createId();\n const abortController = new AbortController();\n const currentSession = get().ai.getCurrentSession();\n\n if (!currentSession) {\n console.error('No current session found');\n return;\n }\n\n set((state) =>\n produce(state, (draft) => {\n draft.ai.analysisAbortController = abortController;\n draft.ai.isRunningAnalysis = true;\n\n const session = draft.config.ai.sessions.find(\n (s) => s.id === draft.config.ai.currentSessionId,\n );\n\n if (session) {\n session.analysisResults.push({\n id: resultId,\n prompt: get().ai.analysisPrompt,\n streamMessage: {\n toolCallMessages: [],\n reasoning: '',\n text: '',\n },\n isCompleted: false,\n });\n }\n }),\n );\n\n try {\n await runAnalysis({\n tableSchemas: get().db.tables,\n modelProvider: currentSession.modelProvider || 'openai',\n model: currentSession.model || 'gpt-4o-mini',\n apiKey: getApiKey(currentSession.modelProvider || 'openai'),\n prompt: get().ai.analysisPrompt,\n abortController,\n tools: get().ai.tools,\n getInstructions,\n onStreamResult: (isCompleted, streamMessage) => {\n set(\n makeResultsAppender({\n resultId,\n streamMessage,\n isCompleted,\n }),\n );\n },\n });\n } catch (err) {\n set(\n makeResultsAppender({\n resultId,\n isCompleted: true,\n errorMessage: {\n error: err instanceof Error ? err.message : String(err),\n },\n }),\n );\n } finally {\n set((state) =>\n produce(state, (draft) => {\n draft.ai.isRunningAnalysis = false;\n draft.ai.analysisPrompt = '';\n }),\n );\n }\n },\n\n cancelAnalysis: () => {\n set((state) =>\n produce(state, (draft) => {\n draft.ai.isRunningAnalysis = false;\n }),\n );\n get().ai.analysisAbortController?.abort('Analysis cancelled');\n },\n\n /**\n * Delete an analysis result from a session\n */\n deleteAnalysisResult: (sessionId: string, resultId: string) => {\n set((state) =>\n produce(state, (draft) => {\n const session = draft.config.ai.sessions.find(\n (s) => s.id === sessionId,\n );\n if (session) {\n session.analysisResults = session.analysisResults.filter(\n (r) => r.id !== resultId,\n );\n }\n }),\n );\n },\n\n findToolComponent: (toolName: string) => {\n return Object.entries(get().ai.tools).find(\n ([name]) => name === toolName,\n )?.[1]?.component as React.ComponentType;\n },\n },\n };\n });\n}\n\n/**\n * Helper function to get the current session from state\n */\nfunction getCurrentSessionFromState<PC extends BaseRoomConfig & AiSliceConfig>(\n state: RoomShellSliceState<PC> | WritableDraft<RoomShellSliceState<PC>>,\n): AnalysisSessionSchema | undefined {\n const {currentSessionId, sessions} = state.config.ai;\n return sessions.find(\n (session: AnalysisSessionSchema) => session.id === currentSessionId,\n );\n}\n\nfunction findResultById(analysisResults: AnalysisResultSchema[], id: string) {\n return analysisResults.find((r: AnalysisResultSchema) => r.id === id);\n}\n\n/**\n * Appends the tool results, tool calls, and analysis to the state\n *\n * @param resultId - The id of the result to append to\n * @param message - The message to append to the state. The structure of the message is defined as:\n * - reasoning: string The reasoning of the assistant\n * - toolCallMessages: ToolCallMessage[] The tool call messages\n * - text: string The final text message\n * @param isCompleted - Whether the analysis is completed\n * @returns The new state\n */\nfunction makeResultsAppender<PC extends BaseRoomConfig & AiSliceConfig>({\n resultId,\n streamMessage,\n errorMessage,\n isCompleted,\n}: {\n resultId: string;\n streamMessage?: StreamMessage;\n errorMessage?: ErrorMessageSchema;\n isCompleted?: boolean;\n}) {\n return (state: RoomShellSliceState<PC>) =>\n produce(state, (draft) => {\n const currentSession = getCurrentSessionFromState(draft);\n if (!currentSession) {\n console.error('No current session found');\n return;\n }\n\n const result = findResultById(currentSession.analysisResults, resultId);\n if (result) {\n if (streamMessage) {\n result.streamMessage = {\n toolCallMessages: (streamMessage.toolCallMessages || []).map(\n (toolCall) => ({\n args: {...toolCall.args},\n isCompleted: toolCall.isCompleted,\n llmResult: toolCall.llmResult,\n additionalData: toolCall.additionalData,\n text: toolCall.text,\n toolCallId: toolCall.toolCallId,\n toolName: toolCall.toolName,\n }),\n ),\n reasoning: streamMessage.reasoning,\n text: streamMessage.text,\n analysis: streamMessage.analysis,\n parts: streamMessage.parts?.map((part) => ({\n ...part,\n ...(part.type === 'text' && {text: part.text}),\n ...(part.type === 'tool' && {\n toolCallMessages: part.toolCallMessages?.map((toolCall) => ({\n args: {...toolCall.args},\n isCompleted: toolCall.isCompleted,\n llmResult: toolCall.llmResult,\n additionalData: toolCall.additionalData,\n text: toolCall.text,\n toolCallId: toolCall.toolCallId,\n toolName: toolCall.toolName,\n })),\n }),\n })),\n };\n }\n if (errorMessage) {\n result.errorMessage = errorMessage;\n }\n if (isCompleted) {\n result.isCompleted = isCompleted;\n }\n } else {\n console.error('Result not found', resultId);\n }\n });\n}\n\ntype RoomConfigWithAi = BaseRoomConfig & AiSliceConfig;\ntype RoomShellSliceStateWithAi = RoomShellSliceState<RoomConfigWithAi> &\n AiSliceState;\n\nexport function useStoreWithAi<T>(\n selector: (state: RoomShellSliceStateWithAi) => T,\n): T {\n return useBaseRoomShellStore<\n BaseRoomConfig & AiSliceConfig,\n RoomShellSliceState<RoomConfigWithAi>,\n T\n >((state) => selector(state as unknown as RoomShellSliceStateWithAi));\n}\n"]}
|
package/dist/analysis.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { StreamMessage } from '@openassistant/core';
|
|
2
2
|
import { DataTable, DuckDbSliceState } from '@sqlrooms/duckdb';
|
|
3
|
-
import type { StoreApi } from '@sqlrooms/
|
|
3
|
+
import type { StoreApi } from '@sqlrooms/room-shell';
|
|
4
4
|
import { AiSliceState, AiSliceTool } from './AiSlice';
|
|
5
5
|
import { AnalysisResultSchema } from './schemas';
|
|
6
6
|
/**
|
|
@@ -44,7 +44,7 @@ type AnalysisParameters = {
|
|
|
44
44
|
onStreamResult: (isCompleted: boolean, streamMessage?: StreamMessage) => void;
|
|
45
45
|
};
|
|
46
46
|
/**
|
|
47
|
-
* Executes an AI analysis session on the
|
|
47
|
+
* Executes an AI analysis session on the room data
|
|
48
48
|
*
|
|
49
49
|
* @param config - Analysis configuration options. See {@link AnalysisParameters} for more details.
|
|
50
50
|
* @returns Object containing tool calls executed and the final analysis result
|
package/dist/analysis.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"analysis.d.ts","sourceRoot":"","sources":["../src/analysis.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,aAAa,EAEd,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAEL,SAAS,EAET,gBAAgB,EACjB,MAAM,kBAAkB,CAAC;AAE1B,OAAO,KAAK,EAAC,QAAQ,EAAC,MAAM,
|
|
1
|
+
{"version":3,"file":"analysis.d.ts","sourceRoot":"","sources":["../src/analysis.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,aAAa,EAEd,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAEL,SAAS,EAET,gBAAgB,EACjB,MAAM,kBAAkB,CAAC;AAE1B,OAAO,KAAK,EAAC,QAAQ,EAAC,MAAM,sBAAsB,CAAC;AACnD,OAAO,EAAC,YAAY,EAAE,WAAW,EAAC,MAAM,WAAW,CAAC;AAEpD,OAAO,EAAC,oBAAoB,EAAsB,MAAM,WAAW,CAAC;AAuDpE;;GAEG;AACH,wBAAgB,sBAAsB,CAAC,YAAY,EAAE,SAAS,EAAE,GAAG,MAAM,CAExE;AA0BD;;GAEG;AACH,KAAK,kBAAkB,GAAG;IACxB,YAAY,EAAE,SAAS,EAAE,CAAC;IAE1B,6DAA6D;IAC7D,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd,sDAAsD;IACtD,aAAa,EAAE,MAAM,CAAC;IAEtB,mDAAmD;IACnD,KAAK,EAAE,MAAM,CAAC;IAEd,sDAAsD;IACtD,MAAM,EAAE,MAAM,CAAC;IAEf,kDAAkD;IAClD,MAAM,EAAE,MAAM,CAAC;IAEf,+DAA+D;IAC/D,eAAe,CAAC,EAAE,eAAe,CAAC;IAElC,8DAA8D;IAC9D,QAAQ,CAAC,EAAE,MAAM,CAAC;IAElB,mEAAmE;IACnE,eAAe,CAAC,EAAE,oBAAoB,EAAE,CAAC;IAEzC,mCAAmC;IACnC,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;IAEpC;;;;OAIG;IACH,eAAe,CAAC,EAAE,CAAC,YAAY,EAAE,SAAS,EAAE,KAAK,MAAM,CAAC;IAExD;;;;OAIG;IACH,cAAc,EAAE,CAAC,WAAW,EAAE,OAAO,EAAE,aAAa,CAAC,EAAE,aAAa,KAAK,IAAI,CAAC;CAC/E,CAAC;AAEF;;;;;GAKG;AACH,wBAAsB,WAAW,CAAC,EAChC,IAAoB,EACpB,YAAY,EACZ,aAAa,EACb,KAAK,EACL,MAAM,EACN,MAAM,EACN,eAAe,EACf,eAAe,EACf,cAAc,EACd,QAAY,EACZ,KAAU,EACV,eAAe,GAChB,EAAE,kBAAkB;;GAqCpB;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAC7B,KAAK,EAAE,QAAQ,CAAC,YAAY,GAAG,gBAAgB,CAAC,GAC/C,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAkD7B"}
|
package/dist/analysis.js
CHANGED
|
@@ -10,7 +10,7 @@ import { QueryToolParameters } from './schemas';
|
|
|
10
10
|
* - Formatting final answers
|
|
11
11
|
*/
|
|
12
12
|
const DEFAULT_INSTRUCTIONS = `
|
|
13
|
-
You are analyzing tables in DuckDB database in the context of a
|
|
13
|
+
You are analyzing tables in DuckDB database in the context of a room.
|
|
14
14
|
|
|
15
15
|
Instructions for analysis:
|
|
16
16
|
- Use DuckDB-specific SQL syntax and functions (not Oracle, PostgreSQL, or other SQL dialects)
|
|
@@ -84,7 +84,7 @@ async function getQuerySummary(connector, sqlQuery) {
|
|
|
84
84
|
}
|
|
85
85
|
}
|
|
86
86
|
/**
|
|
87
|
-
* Executes an AI analysis session on the
|
|
87
|
+
* Executes an AI analysis session on the room data
|
|
88
88
|
*
|
|
89
89
|
* @param config - Analysis configuration options. See {@link AnalysisParameters} for more details.
|
|
90
90
|
* @returns Object containing tool calls executed and the final analysis result
|
package/dist/analysis.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"analysis.js","sourceRoot":"","sources":["../src/analysis.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EACf,eAAe,EAEf,IAAI,GACL,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EACL,gBAAgB,GAIjB,MAAM,kBAAkB,CAAC;AAI1B,OAAO,EAAC,eAAe,EAAC,MAAM,oCAAoC,CAAC;AACnE,OAAO,EAAuB,mBAAmB,EAAC,MAAM,WAAW,CAAC;AAEpE;;;;;;GAMG;AACH,MAAM,oBAAoB,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA4C5B,CAAC;AAEF;;GAEG;AACH,MAAM,UAAU,sBAAsB,CAAC,YAAyB;IAC9D,OAAO,GAAG,oBAAoB,KAAK,IAAI,CAAC,SAAS,CAAC,YAAY,CAAC,EAAE,CAAC;AACpE,CAAC;AAED;;;;;GAKG;AACH,KAAK,UAAU,eAAe,CAAC,SAA0B,EAAE,QAAgB;IACzE,IAAI,CAAC,QAAQ,CAAC,WAAW,EAAE,CAAC,IAAI,EAAE,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,CAAC;QACxD,OAAO,IAAI,CAAC;IACd,CAAC;IAED,IAAI,CAAC;QACH,MAAM,QAAQ,GAAG,eAAe,IAAI,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,sCAAsC;QACpF,MAAM,SAAS,CAAC,KAAK,CAAC,yBAAyB,QAAQ,OAAO,QAAQ,EA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,EAAE,CAAC,CAAC,CAAC;oBACrC,MAAM,YAAY,GAAG,gBAAgB,CAAC,SAAS,CAAC,CAAC;oBAEjD,OAAO;wBACL,SAAS,EAAE;4BACT,OAAO,EAAE,IAAI;4BACb,IAAI,EAAE;gCACJ,IAAI;gCACJ,OAAO,EAAE,WAAW;gCACpB,YAAY;6BACb;yBACF;wBACD,cAAc,EAAE;4BACd,KAAK,EAAE,cAAc;4BACrB,QAAQ;yBACT;qBACF,CAAC;gBACJ,CAAC;gBAAC,OAAO,KAAK,EAAE,CAAC;oBACf,OAAO;wBACL,SAAS,EAAE;4BACT,OAAO,EAAE,KAAK;4BACd,OAAO,EAAE,yBAAyB;4BAClC,YAAY,EACV,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,eAAe;yBAC3D;qBACF,CAAC;gBACJ,CAAC;YACH,CAAC;YACD,SAAS,EAAE,eAAe;SAC3B,CAAC;KACH,CAAC;AACJ,CAAC","sourcesContent":["import {\n createAssistant,\n rebuildMessages,\n StreamMessage,\n tool,\n} from '@openassistant/core';\nimport {\n arrowTableToJson,\n DataTable,\n DuckDbConnector,\n DuckDbSliceState,\n} from '@sqlrooms/duckdb';\n\nimport type {StoreApi} from '@sqlrooms/project-builder';\nimport {AiSliceState, AiSliceTool} from './AiSlice';\nimport {QueryToolResult} from './components/tools/QueryToolResult';\nimport {AnalysisResultSchema, QueryToolParameters} from './schemas';\n\n/**\n * System prompt template for the AI assistant that provides instructions for:\n * - Using DuckDB-specific SQL syntax and functions\n * - Handling query results and error cases\n * - Creating visualizations with VegaLite\n * - Formatting final answers\n */\nconst DEFAULT_INSTRUCTIONS = `\nYou are analyzing tables in DuckDB database in the context of a project.\n\nInstructions for analysis:\n- Use DuckDB-specific SQL syntax and functions (not Oracle, PostgreSQL, or other SQL dialects)\n- Some key DuckDB-specific functions to use:\n * regexp_matches() for regex (not regexp_like)\n * strftime() for date formatting (not to_char)\n * list_aggregate() for array operations\n * unnest() for array expansion\n * regr_sxy()\n * corr()\n * skewness()\n- Please always try to use SQL queries to answer users questions\n- Please run tool calls sequentially, don't run multiple tool calls in parallel\n- IMPORTANT: Do not list out raw query results in your response. Instead:\n * Describe the results in natural language\n * Provide summary statistics\n * Use comparisons and relative terms\n * Include only the most relevant values if necessary\n- Break down complex problems into smaller steps\n- Use \"SUMMARIZE table_name\"for quick overview of the table\n- Please don't modify data\n- IMPORTANT: When you receive an error response from a tool call (where success: false):\n * Stop making any further tool calls immediately\n * Return a final answer that includes the error message\n * Explain what went wrong and suggest possible fixes if applicable\n\nWhen creating visualizations:\n- Follow VegaLite syntax\n- Choose appropriate chart types based on the data and analysis goals\n- Use clear titles and axis labels\n- Consider color schemes for better readability\n- Add meaningful tooltips when relevant\n- Format numbers and dates appropriately\n- Use aggregations when dealing with large datasets\n\nFor your final answer:\n- Provide an explanation for how you got it\n- Explain your reasoning step by step\n- Include relevant statistics or metrics\n- For each prompt, please always provide the final answer.\n\nPlease use the following schema for the tables:\n`;\n\n/**\n * Returns the default system instructions for the AI assistant\n */\nexport function getDefaultInstructions(tablesSchema: DataTable[]): string {\n return `${DEFAULT_INSTRUCTIONS}\\n${JSON.stringify(tablesSchema)}`;\n}\n\n/**\n * Generates summary statistics for a SQL query result\n * @param connector - DuckDB connection instance\n * @param sqlQuery - SQL SELECT query to analyze\n * @returns Summary statistics as JSON object, or null if the query is not a SELECT statement or if summary generation fails\n */\nasync function getQuerySummary(connector: DuckDbConnector, sqlQuery: string) {\n if (!sqlQuery.toLowerCase().trim().startsWith('select')) {\n return null;\n }\n\n try {\n const viewName = `temp_result_${Date.now()}`; // unique view name to avoid conflicts\n await connector.query(`CREATE TEMPORARY VIEW ${viewName} AS ${sqlQuery}`);\n const summaryResult = await connector.query(`SUMMARIZE ${viewName}`).result;\n const summaryData = arrowTableToJson(summaryResult);\n await connector.query(`DROP VIEW IF EXISTS ${viewName}`);\n return summaryData;\n } catch (error) {\n console.warn('Failed to get summary:', error);\n return null;\n }\n}\n\n/**\n * Configuration options for running an AI analysis session\n */\ntype AnalysisParameters = {\n tableSchemas: DataTable[];\n\n /** Assistant instance identifier (default: 'sqlrooms-ai') */\n name?: string;\n\n /** AI model provider (e.g., 'openai', 'anthropic') */\n modelProvider: string;\n\n /** Model identifier (e.g., 'gpt-4', 'claude-3') */\n model: string;\n\n /** Authentication key for the model provider's API */\n apiKey: string;\n\n /** Analysis prompt or question to be processed */\n prompt: string;\n\n /** Optional controller for canceling the analysis operation */\n abortController?: AbortController;\n\n /** Maximum number of analysis steps allowed (default: 100) */\n maxSteps?: number;\n\n /** The history of analysis results (e.g. saved in localStorage) */\n historyAnalysis?: AnalysisResultSchema[];\n\n /** Tools to use in the analysis */\n tools?: Record<string, AiSliceTool>;\n\n /**\n * Function to get custom instructions for the AI assistant\n * @param tablesSchema - The schema of the tables in the database\n * @returns The instructions string to use\n */\n getInstructions?: (tablesSchema: DataTable[]) => string;\n\n /**\n * Callback for handling streaming results\n * @param isCompleted - Indicates if this is the final message in the stream\n * @param streamMessage - Current message content being streamed\n */\n onStreamResult: (isCompleted: boolean, streamMessage?: StreamMessage) => void;\n};\n\n/**\n * Executes an AI analysis session on the project data\n *\n * @param config - Analysis configuration options. See {@link AnalysisParameters} for more details.\n * @returns Object containing tool calls executed and the final analysis result\n */\nexport async function runAnalysis({\n name = 'sqlrooms-ai',\n tableSchemas,\n modelProvider,\n model,\n apiKey,\n prompt,\n abortController,\n historyAnalysis,\n onStreamResult,\n maxSteps = 5,\n tools = {},\n getInstructions,\n}: AnalysisParameters) {\n // get the singleton assistant instance\n const assistant = await createAssistant({\n name,\n modelProvider,\n model,\n apiKey,\n version: 'v1',\n instructions: getInstructions\n ? getInstructions(tableSchemas)\n : getDefaultInstructions(tableSchemas),\n functions: tools,\n temperature: 0,\n toolChoice: 'auto', // this will enable streaming\n maxSteps,\n ...(abortController ? {abortController} : {}),\n });\n\n // restore ai messages from historyAnalysis?\n if (historyAnalysis) {\n const historyMessages = historyAnalysis.map((analysis) => ({\n prompt: analysis.prompt,\n response: analysis.streamMessage,\n }));\n const initialMessages = rebuildMessages(historyMessages);\n assistant.setMessages(initialMessages);\n }\n\n // process the prompt\n const newMessages = await assistant.processTextMessage({\n textMessage: prompt,\n streamMessageCallback: ({isCompleted, message}) => {\n onStreamResult(isCompleted ?? false, message);\n },\n });\n\n return newMessages;\n}\n\n/**\n * Default tools available to the AI assistant for data analysis\n * Includes:\n * - query: Executes SQL queries against DuckDB\n */\nexport function getDefaultTools(\n store: StoreApi<AiSliceState & DuckDbSliceState>,\n): Record<string, AiSliceTool> {\n return {\n query: tool({\n description: `A tool for running SQL queries on the tables in the database.\nPlease only run one query at a time.\nIf a query fails, please don't try to run it again with the same syntax.`,\n parameters: QueryToolParameters,\n // TODO: specify the return type e.g. Promise<Partial<ToolCallMessage>>\n execute: async ({type, sqlQuery}) => {\n try {\n const connector = await store.getState().db.getConnector();\n // TODO use options.abortSignal: maybe call db.cancelPendingQuery\n const result = await connector.query(sqlQuery).result;\n // Only get summary if the query isn't already a SUMMARIZE query\n const summaryData = sqlQuery.toLowerCase().includes('summarize')\n ? arrowTableToJson(result)\n : await getQuerySummary(connector, sqlQuery);\n\n // Get first 2 rows of the result as a json object\n const subResult = result.slice(0, 2);\n const firstTwoRows = arrowTableToJson(subResult);\n\n return {\n llmResult: {\n success: true,\n data: {\n type,\n summary: summaryData,\n firstTwoRows,\n },\n },\n additionalData: {\n title: 'Query Result',\n sqlQuery,\n },\n };\n } catch (error) {\n return {\n llmResult: {\n success: false,\n details: 'Query execution failed.',\n errorMessage:\n error instanceof Error ? error.message : 'Unknown error',\n },\n };\n }\n },\n component: QueryToolResult,\n }),\n };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"analysis.js","sourceRoot":"","sources":["../src/analysis.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EACf,eAAe,EAEf,IAAI,GACL,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EACL,gBAAgB,GAIjB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CAAC,OAAO,CAAC,CAAC,CAAC,eAAe;yBAC3D;qBACF,CAAC;gBACJ,CAAC;YACH,CAAC;YACD,SAAS,EAAE,eAAe;SAC3B,CAAC;KACH,CAAC;AACJ,CAAC","sourcesContent":["import {\n createAssistant,\n rebuildMessages,\n StreamMessage,\n tool,\n} from '@openassistant/core';\nimport {\n arrowTableToJson,\n DataTable,\n DuckDbConnector,\n DuckDbSliceState,\n} from '@sqlrooms/duckdb';\n\nimport type {StoreApi} from '@sqlrooms/room-shell';\nimport {AiSliceState, AiSliceTool} from './AiSlice';\nimport {QueryToolResult} from './components/tools/QueryToolResult';\nimport {AnalysisResultSchema, QueryToolParameters} from './schemas';\n\n/**\n * System prompt template for the AI assistant that provides instructions for:\n * - Using DuckDB-specific SQL syntax and functions\n * - Handling query results and error cases\n * - Creating visualizations with VegaLite\n * - Formatting final answers\n */\nconst DEFAULT_INSTRUCTIONS = `\nYou are analyzing tables in DuckDB database in the context of a room.\n\nInstructions for analysis:\n- Use DuckDB-specific SQL syntax and functions (not Oracle, PostgreSQL, or other SQL dialects)\n- Some key DuckDB-specific functions to use:\n * regexp_matches() for regex (not regexp_like)\n * strftime() for date formatting (not to_char)\n * list_aggregate() for array operations\n * unnest() for array expansion\n * regr_sxy()\n * corr()\n * skewness()\n- Please always try to use SQL queries to answer users questions\n- Please run tool calls sequentially, don't run multiple tool calls in parallel\n- IMPORTANT: Do not list out raw query results in your response. Instead:\n * Describe the results in natural language\n * Provide summary statistics\n * Use comparisons and relative terms\n * Include only the most relevant values if necessary\n- Break down complex problems into smaller steps\n- Use \"SUMMARIZE table_name\"for quick overview of the table\n- Please don't modify data\n- IMPORTANT: When you receive an error response from a tool call (where success: false):\n * Stop making any further tool calls immediately\n * Return a final answer that includes the error message\n * Explain what went wrong and suggest possible fixes if applicable\n\nWhen creating visualizations:\n- Follow VegaLite syntax\n- Choose appropriate chart types based on the data and analysis goals\n- Use clear titles and axis labels\n- Consider color schemes for better readability\n- Add meaningful tooltips when relevant\n- Format numbers and dates appropriately\n- Use aggregations when dealing with large datasets\n\nFor your final answer:\n- Provide an explanation for how you got it\n- Explain your reasoning step by step\n- Include relevant statistics or metrics\n- For each prompt, please always provide the final answer.\n\nPlease use the following schema for the tables:\n`;\n\n/**\n * Returns the default system instructions for the AI assistant\n */\nexport function getDefaultInstructions(tablesSchema: DataTable[]): string {\n return `${DEFAULT_INSTRUCTIONS}\\n${JSON.stringify(tablesSchema)}`;\n}\n\n/**\n * Generates summary statistics for a SQL query result\n * @param connector - DuckDB connection instance\n * @param sqlQuery - SQL SELECT query to analyze\n * @returns Summary statistics as JSON object, or null if the query is not a SELECT statement or if summary generation fails\n */\nasync function getQuerySummary(connector: DuckDbConnector, sqlQuery: string) {\n if (!sqlQuery.toLowerCase().trim().startsWith('select')) {\n return null;\n }\n\n try {\n const viewName = `temp_result_${Date.now()}`; // unique view name to avoid conflicts\n await connector.query(`CREATE TEMPORARY VIEW ${viewName} AS ${sqlQuery}`);\n const summaryResult = await connector.query(`SUMMARIZE ${viewName}`).result;\n const summaryData = arrowTableToJson(summaryResult);\n await connector.query(`DROP VIEW IF EXISTS ${viewName}`);\n return summaryData;\n } catch (error) {\n console.warn('Failed to get summary:', error);\n return null;\n }\n}\n\n/**\n * Configuration options for running an AI analysis session\n */\ntype AnalysisParameters = {\n tableSchemas: DataTable[];\n\n /** Assistant instance identifier (default: 'sqlrooms-ai') */\n name?: string;\n\n /** AI model provider (e.g., 'openai', 'anthropic') */\n modelProvider: string;\n\n /** Model identifier (e.g., 'gpt-4', 'claude-3') */\n model: string;\n\n /** Authentication key for the model provider's API */\n apiKey: string;\n\n /** Analysis prompt or question to be processed */\n prompt: string;\n\n /** Optional controller for canceling the analysis operation */\n abortController?: AbortController;\n\n /** Maximum number of analysis steps allowed (default: 100) */\n maxSteps?: number;\n\n /** The history of analysis results (e.g. saved in localStorage) */\n historyAnalysis?: AnalysisResultSchema[];\n\n /** Tools to use in the analysis */\n tools?: Record<string, AiSliceTool>;\n\n /**\n * Function to get custom instructions for the AI assistant\n * @param tablesSchema - The schema of the tables in the database\n * @returns The instructions string to use\n */\n getInstructions?: (tablesSchema: DataTable[]) => string;\n\n /**\n * Callback for handling streaming results\n * @param isCompleted - Indicates if this is the final message in the stream\n * @param streamMessage - Current message content being streamed\n */\n onStreamResult: (isCompleted: boolean, streamMessage?: StreamMessage) => void;\n};\n\n/**\n * Executes an AI analysis session on the room data\n *\n * @param config - Analysis configuration options. See {@link AnalysisParameters} for more details.\n * @returns Object containing tool calls executed and the final analysis result\n */\nexport async function runAnalysis({\n name = 'sqlrooms-ai',\n tableSchemas,\n modelProvider,\n model,\n apiKey,\n prompt,\n abortController,\n historyAnalysis,\n onStreamResult,\n maxSteps = 5,\n tools = {},\n getInstructions,\n}: AnalysisParameters) {\n // get the singleton assistant instance\n const assistant = await createAssistant({\n name,\n modelProvider,\n model,\n apiKey,\n version: 'v1',\n instructions: getInstructions\n ? getInstructions(tableSchemas)\n : getDefaultInstructions(tableSchemas),\n functions: tools,\n temperature: 0,\n toolChoice: 'auto', // this will enable streaming\n maxSteps,\n ...(abortController ? {abortController} : {}),\n });\n\n // restore ai messages from historyAnalysis?\n if (historyAnalysis) {\n const historyMessages = historyAnalysis.map((analysis) => ({\n prompt: analysis.prompt,\n response: analysis.streamMessage,\n }));\n const initialMessages = rebuildMessages(historyMessages);\n assistant.setMessages(initialMessages);\n }\n\n // process the prompt\n const newMessages = await assistant.processTextMessage({\n textMessage: prompt,\n streamMessageCallback: ({isCompleted, message}) => {\n onStreamResult(isCompleted ?? false, message);\n },\n });\n\n return newMessages;\n}\n\n/**\n * Default tools available to the AI assistant for data analysis\n * Includes:\n * - query: Executes SQL queries against DuckDB\n */\nexport function getDefaultTools(\n store: StoreApi<AiSliceState & DuckDbSliceState>,\n): Record<string, AiSliceTool> {\n return {\n query: tool({\n description: `A tool for running SQL queries on the tables in the database.\nPlease only run one query at a time.\nIf a query fails, please don't try to run it again with the same syntax.`,\n parameters: QueryToolParameters,\n // TODO: specify the return type e.g. Promise<Partial<ToolCallMessage>>\n execute: async ({type, sqlQuery}) => {\n try {\n const connector = await store.getState().db.getConnector();\n // TODO use options.abortSignal: maybe call db.cancelPendingQuery\n const result = await connector.query(sqlQuery).result;\n // Only get summary if the query isn't already a SUMMARIZE query\n const summaryData = sqlQuery.toLowerCase().includes('summarize')\n ? arrowTableToJson(result)\n : await getQuerySummary(connector, sqlQuery);\n\n // Get first 2 rows of the result as a json object\n const subResult = result.slice(0, 2);\n const firstTwoRows = arrowTableToJson(subResult);\n\n return {\n llmResult: {\n success: true,\n data: {\n type,\n summary: summaryData,\n firstTwoRows,\n },\n },\n additionalData: {\n title: 'Query Result',\n sqlQuery,\n },\n };\n } catch (error) {\n return {\n llmResult: {\n success: false,\n details: 'Query execution failed.',\n errorMessage:\n error instanceof Error ? error.message : 'Unknown error',\n },\n };\n }\n },\n component: QueryToolResult,\n }),\n };\n}\n"]}
|
|
@@ -9,7 +9,7 @@ export const QueryControls = ({ className, placeholder = 'Type here what would y
|
|
|
9
9
|
const runAnalysis = useStoreWithAi((s) => s.ai.startAnalysis);
|
|
10
10
|
const cancelAnalysis = useStoreWithAi((s) => s.ai.cancelAnalysis);
|
|
11
11
|
const analysisPrompt = useStoreWithAi((s) => s.ai.analysisPrompt);
|
|
12
|
-
const isDataAvailable = useStoreWithAi((s) => s.
|
|
12
|
+
const isDataAvailable = useStoreWithAi((s) => s.room.isDataAvailable);
|
|
13
13
|
const setAnalysisPrompt = useStoreWithAi((s) => s.ai.setAnalysisPrompt);
|
|
14
14
|
const currentSession = useStoreWithAi((s) => s.ai.getCurrentSession());
|
|
15
15
|
const model = currentSession?.model;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"QueryControls.js","sourceRoot":"","sources":["../../src/components/QueryControls.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAC,MAAM,EAAE,EAAE,EAAE,OAAO,EAAE,QAAQ,EAAC,MAAM,cAAc,CAAC;AAC3D,OAAO,E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AAM,QAAQ,GAAO,EACrB,cAAK,SAAS,EAAC,QAAQ,GAAG,EAC1B,0BACG,iBAAiB,IAAI,CACpB,MAAC,MAAM,IACL,OAAO,EAAC,SAAS,EACjB,SAAS,EAAC,cAAc,EACxB,OAAO,EAAE,cAAc,aAEvB,KAAC,YAAY,IAAC,SAAS,EAAC,SAAS,GAAG,YAE7B,CACV,EACD,KAAC,MAAM,IACL,SAAS,EAAC,wBAAwB,EAClC,OAAO,EAAC,SAAS,EACjB,IAAI,EAAC,MAAM,EACX,OAAO,EAAE,WAAW,EACpB,QAAQ,EAAE,CAAC,QAAQ,YAElB,iBAAiB,CAAC,CAAC,CAAC,CACnB,cAAK,SAAS,EAAC,yBAAyB,YACtC,KAAC,OAAO,IAAC,SAAS,EAAC,SAAS,GAAG,GAC3B,CACP,CAAC,CAAC,CAAC,CACF,KAAC,WAAW,KAAG,CAChB,GACM,IACL,IACF,IACF,GACF,GACF,CACP,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import {Button, cn, Spinner, Textarea} from '@sqlrooms/ui';\nimport {ArrowUpIcon, OctagonXIcon} from 'lucide-react';\nimport {PropsWithChildren, useCallback, useRef, useEffect} from 'react';\nimport {useStoreWithAi} from '../AiSlice';\n\ntype QueryControlsProps = PropsWithChildren<{\n className?: string;\n placeholder?: string;\n}>;\n\nexport const QueryControls: React.FC<QueryControlsProps> = ({\n className,\n placeholder = 'Type here what would you like to learn about the data?',\n children,\n}) => {\n const textareaRef = useRef<HTMLTextAreaElement>(null);\n const isRunningAnalysis = useStoreWithAi((s) => s.ai.isRunningAnalysis);\n const runAnalysis = useStoreWithAi((s) => s.ai.startAnalysis);\n const cancelAnalysis = useStoreWithAi((s) => s.ai.cancelAnalysis);\n const analysisPrompt = useStoreWithAi((s) => s.ai.analysisPrompt);\n const isDataAvailable = useStoreWithAi((s) => s.room.isDataAvailable);\n const setAnalysisPrompt = useStoreWithAi((s) => s.ai.setAnalysisPrompt);\n const currentSession = useStoreWithAi((s) => s.ai.getCurrentSession());\n const model = currentSession?.model;\n\n useEffect(() => {\n if (!isDataAvailable) return;\n // Focus the textarea when the component mounts\n // Using a small timeout ensures the data is loaded and\n // add timeout to prevent aria hidden warning caused by the\n // loading progress dialog being still open\n const timer = setTimeout(() => {\n if (textareaRef.current) {\n textareaRef.current.focus();\n }\n }, 500);\n\n return () => clearTimeout(timer);\n }, [isDataAvailable]);\n\n const handleKeyDown = useCallback(\n (e: React.KeyboardEvent<HTMLTextAreaElement>) => {\n if (\n e.key === 'Enter' &&\n !e.shiftKey &&\n !e.ctrlKey &&\n !e.altKey &&\n !e.metaKey\n ) {\n e.preventDefault();\n if (!isRunningAnalysis && model && analysisPrompt.trim().length) {\n runAnalysis();\n }\n }\n },\n [isRunningAnalysis, model, analysisPrompt, runAnalysis],\n );\n\n const canStart = Boolean(\n !isRunningAnalysis && model && analysisPrompt.trim().length,\n );\n return (\n <div\n className={cn(\n 'flex w-full flex-col items-center justify-center gap-4',\n className,\n )}\n >\n <div className=\"relative w-full overflow-hidden p-[1px]\">\n <div className=\"flex flex-col\">\n <Textarea\n ref={textareaRef}\n disabled={isRunningAnalysis}\n className=\"bg-muted/50 h-[100px] resize-none\"\n value={analysisPrompt}\n onChange={(e) => setAnalysisPrompt(e.target.value)}\n onKeyDown={handleKeyDown}\n placeholder={placeholder}\n autoFocus\n />\n <div className=\"bg-muted/30 flex w-full items-center justify-between gap-2 p-2\">\n <div>{children}</div>\n <div className=\"flex-1\" />\n <div>\n {isRunningAnalysis && (\n <Button\n variant=\"outline\"\n className=\"rounded-full\"\n onClick={cancelAnalysis}\n >\n <OctagonXIcon className=\"h-4 w-4\" />\n Stop\n </Button>\n )}\n <Button\n className=\"h-10 w-10 rounded-full\"\n variant=\"default\"\n size=\"icon\"\n onClick={runAnalysis}\n disabled={!canStart}\n >\n {isRunningAnalysis ? (\n <div className=\"flex items-center gap-2\">\n <Spinner className=\"h-4 w-4\" />\n </div>\n ) : (\n <ArrowUpIcon />\n )}\n </Button>\n </div>\n </div>\n </div>\n </div>\n </div>\n );\n};\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sqlrooms/ai",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.17.0",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"module": "dist/index.js",
|
|
@@ -21,13 +21,13 @@
|
|
|
21
21
|
"@ai-sdk/provider": "^1.1.3",
|
|
22
22
|
"@openassistant/core": "^0.2.7",
|
|
23
23
|
"@paralleldrive/cuid2": "^2.2.2",
|
|
24
|
-
"@sqlrooms/data-table": "0.
|
|
25
|
-
"@sqlrooms/duckdb": "0.
|
|
26
|
-
"@sqlrooms/monaco-editor": "0.
|
|
27
|
-
"@sqlrooms/
|
|
28
|
-
"@sqlrooms/
|
|
29
|
-
"@sqlrooms/ui": "0.
|
|
30
|
-
"@sqlrooms/utils": "0.
|
|
24
|
+
"@sqlrooms/data-table": "0.17.0",
|
|
25
|
+
"@sqlrooms/duckdb": "0.17.0",
|
|
26
|
+
"@sqlrooms/monaco-editor": "0.17.0",
|
|
27
|
+
"@sqlrooms/room-config": "0.17.0",
|
|
28
|
+
"@sqlrooms/room-shell": "0.17.0",
|
|
29
|
+
"@sqlrooms/ui": "0.17.0",
|
|
30
|
+
"@sqlrooms/utils": "0.17.0",
|
|
31
31
|
"ai": "^4.3.16",
|
|
32
32
|
"immer": "^10.1.1",
|
|
33
33
|
"lucide-react": "^0.475.0",
|
|
@@ -45,5 +45,5 @@
|
|
|
45
45
|
"typecheck": "tsc --noEmit",
|
|
46
46
|
"typedoc": "typedoc"
|
|
47
47
|
},
|
|
48
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "1f2a306da771481e2c89d8bfbd0507772167b1b5"
|
|
49
49
|
}
|