@sqlrooms/ai 0.5.0 → 0.6.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/dist/AiSlice.d.ts +90 -75
- package/dist/AiSlice.d.ts.map +1 -1
- package/dist/AiSlice.js +105 -61
- package/dist/AiSlice.js.map +1 -1
- package/dist/AnalysisResult.d.ts +16 -2
- package/dist/AnalysisResult.d.ts.map +1 -1
- package/dist/AnalysisResult.js +43 -7
- package/dist/AnalysisResult.js.map +1 -1
- package/dist/AnalysisResultsContainer.js.map +1 -1
- package/dist/QueryControls.js.map +1 -1
- package/dist/QueryResult.d.ts +9 -0
- package/dist/QueryResult.d.ts.map +1 -0
- package/dist/QueryResult.js +46 -0
- package/dist/QueryResult.js.map +1 -0
- package/dist/ToolCall.d.ts +64 -5
- package/dist/ToolCall.d.ts.map +1 -1
- package/dist/ToolCall.js +53 -10
- package/dist/ToolCall.js.map +1 -1
- package/dist/ToolResult.d.ts +3 -0
- package/dist/ToolResult.d.ts.map +1 -1
- package/dist/ToolResult.js +19 -2
- package/dist/ToolResult.js.map +1 -1
- package/dist/analysis.d.ts +48 -174
- package/dist/analysis.d.ts.map +1 -1
- package/dist/analysis.js +219 -61
- package/dist/analysis.js.map +1 -1
- package/dist/hooks/use-scroll-to-bottom.js.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/schemas.d.ts +84 -110
- package/dist/schemas.d.ts.map +1 -1
- package/dist/schemas.js +13 -11
- package/dist/schemas.js.map +1 -1
- package/package.json +9 -7
package/dist/analysis.js
CHANGED
|
@@ -1,88 +1,246 @@
|
|
|
1
|
-
import { arrowTableToJson, getDuckDb } from '@sqlrooms/duckdb';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { arrowTableToJson, getDuckDb, getDuckTableSchemas, } from '@sqlrooms/duckdb';
|
|
2
|
+
import { createAssistant, } from '@openassistant/core';
|
|
3
|
+
import { ChartToolParameters, QueryToolParameters } from './schemas';
|
|
4
|
+
import { queryMessage } from './QueryResult';
|
|
5
|
+
import { isChartToolParameters, isQueryToolParameters } from './ToolCall';
|
|
4
6
|
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
7
|
+
* System prompt template for the AI assistant that provides instructions for:
|
|
8
|
+
* - Using DuckDB-specific SQL syntax and functions
|
|
9
|
+
* - Handling query results and error cases
|
|
10
|
+
* - Creating visualizations with VegaLite
|
|
11
|
+
* - Formatting final answers
|
|
9
12
|
*/
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
const SYSTEM_PROMPT = `
|
|
14
|
+
You are analyzing tables in DuckDB database in the context of a project.
|
|
15
|
+
|
|
16
|
+
Instructions for analysis:
|
|
17
|
+
- Use DuckDB-specific SQL syntax and functions (not Oracle, PostgreSQL, or other SQL dialects)
|
|
18
|
+
- Some key DuckDB-specific functions to use:
|
|
19
|
+
* regexp_matches() for regex (not regexp_like)
|
|
20
|
+
* strftime() for date formatting (not to_char)
|
|
21
|
+
* list_aggregate() for array operations
|
|
22
|
+
* unnest() for array expansion
|
|
23
|
+
* regr_sxy()
|
|
24
|
+
* corr()
|
|
25
|
+
* skewness()
|
|
26
|
+
- Please always try to use SQL queries to answer users questions
|
|
27
|
+
- Please run tool calls sequentially, don't run multiple tool calls in parallel
|
|
28
|
+
- IMPORTANT: Do not list out raw query results in your response. Instead:
|
|
29
|
+
* Describe the results in natural language
|
|
30
|
+
* Provide summary statistics
|
|
31
|
+
* Use comparisons and relative terms
|
|
32
|
+
* Include only the most relevant values if necessary
|
|
33
|
+
- Break down complex problems into smaller steps
|
|
34
|
+
- Use "SUMMARIZE table_name"for quick overview of the table
|
|
35
|
+
- Please don't modify data
|
|
36
|
+
- IMPORTANT: When you receive an error response from a tool call (where success: false):
|
|
37
|
+
* Stop making any further tool calls immediately
|
|
38
|
+
* Return a final answer that includes the error message
|
|
39
|
+
* Explain what went wrong and suggest possible fixes if applicable
|
|
40
|
+
|
|
41
|
+
When creating visualizations:
|
|
42
|
+
- Follow VegaLite syntax
|
|
43
|
+
- Choose appropriate chart types based on the data and analysis goals
|
|
44
|
+
- Use clear titles and axis labels
|
|
45
|
+
- Consider color schemes for better readability
|
|
46
|
+
- Add meaningful tooltips when relevant
|
|
47
|
+
- Format numbers and dates appropriately
|
|
48
|
+
- Use aggregations when dealing with large datasets
|
|
49
|
+
|
|
50
|
+
For your final answer:
|
|
51
|
+
- Provide an explanation for how you got it
|
|
52
|
+
- Explain your reasoning step by step
|
|
53
|
+
- Include relevant statistics or metrics
|
|
54
|
+
- For each prompt, please alwasy provide the final answer.
|
|
55
|
+
|
|
56
|
+
Please use the following schema for the tables:
|
|
57
|
+
`;
|
|
58
|
+
/**
|
|
59
|
+
* Generates summary statistics for a SQL query result
|
|
60
|
+
* @param conn - DuckDB connection instance
|
|
61
|
+
* @param sqlQuery - SQL SELECT query to analyze
|
|
62
|
+
* @returns Summary statistics as JSON object, or null if the query is not a SELECT statement or if summary generation fails
|
|
63
|
+
*/
|
|
64
|
+
async function getQuerySummary(conn, sqlQuery) {
|
|
65
|
+
if (!sqlQuery.toLowerCase().trim().startsWith('select')) {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
try {
|
|
69
|
+
const viewName = `temp_result_${Date.now()}`; // unique view name to avoid conflicts
|
|
70
|
+
await conn.query(`CREATE TEMPORARY VIEW ${viewName} AS ${sqlQuery}`);
|
|
71
|
+
const summaryResult = await conn.query(`SUMMARIZE ${viewName}`);
|
|
72
|
+
const summaryData = arrowTableToJson(summaryResult);
|
|
73
|
+
await conn.query(`DROP VIEW IF EXISTS ${viewName}`);
|
|
74
|
+
return summaryData;
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
console.warn('Failed to get summary:', error);
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Executes an AI analysis session on the project data
|
|
83
|
+
*
|
|
84
|
+
* @param config - Analysis configuration options. See {@link AnalysisConfig} for more details.
|
|
85
|
+
* @returns Object containing tool calls executed and the final analysis result
|
|
86
|
+
*/
|
|
87
|
+
export async function runAnalysis({ name = 'sqlrooms-ai', modelProvider, model, apiKey, prompt, abortController, onStepFinish, onStreamResult, maxSteps = 5, }) {
|
|
88
|
+
const tablesSchema = await getDuckTableSchemas();
|
|
89
|
+
// get the singlton assistant instance
|
|
90
|
+
const assistant = await createAssistant({
|
|
91
|
+
name,
|
|
92
|
+
modelProvider,
|
|
14
93
|
model,
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
94
|
+
apiKey,
|
|
95
|
+
version: 'v1',
|
|
96
|
+
instructions: `${SYSTEM_PROMPT}\n${JSON.stringify(tablesSchema)}`,
|
|
97
|
+
vercelFunctions: TOOLS,
|
|
98
|
+
temperature: 0,
|
|
99
|
+
toolChoice: 'auto', // this will enable streaming
|
|
20
100
|
maxSteps,
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
101
|
+
...(abortController ? { abortController } : {}),
|
|
102
|
+
});
|
|
103
|
+
// process the prompt
|
|
104
|
+
const result = await assistant.processTextMessage({
|
|
105
|
+
textMessage: prompt,
|
|
106
|
+
streamMessageCallback: (message) => {
|
|
107
|
+
// the final result (before the answer) can be streamed back here
|
|
108
|
+
onStreamResult(message.deltaMessage, message.isCompleted ?? false);
|
|
109
|
+
},
|
|
26
110
|
onStepFinish,
|
|
27
111
|
});
|
|
28
|
-
// const answer = result.toolCalls.find((t) => t.toolName === 'answer');
|
|
29
|
-
// if (!answer) {
|
|
30
|
-
// console.error('No answer tool call found', {result});
|
|
31
|
-
// throw new Error('No answer tool call found');
|
|
32
|
-
// }
|
|
33
|
-
// return answer.args;
|
|
34
112
|
return result;
|
|
35
113
|
}
|
|
114
|
+
/**
|
|
115
|
+
* Extracts a readable error message from an error object
|
|
116
|
+
* @param error - Error object or unknown value
|
|
117
|
+
* @returns Formatted error message string
|
|
118
|
+
*/
|
|
119
|
+
function getErrorMessage(error) {
|
|
120
|
+
if (error instanceof Error) {
|
|
121
|
+
if (error.cause instanceof Error) {
|
|
122
|
+
return error.cause.message;
|
|
123
|
+
}
|
|
124
|
+
return error.message;
|
|
125
|
+
}
|
|
126
|
+
return String(error);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Collection of tools available to the AI assistant for data analysis
|
|
130
|
+
* Includes:
|
|
131
|
+
* - query: Executes SQL queries against DuckDB
|
|
132
|
+
* - chart: Creates VegaLite visualizations
|
|
133
|
+
*/
|
|
36
134
|
const TOOLS = {
|
|
37
|
-
query:
|
|
38
|
-
description:
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
'Include VegaLite charts in your response if the data is suitable for it. ' +
|
|
44
|
-
'Omit the data from the chart.vegaLiteSpec in the response, provide an sql query in chart.sqlQuery instead. ' +
|
|
45
|
-
'To obtain stats, use the `SUMMARIZE table_name` query. ' +
|
|
46
|
-
"Don't execute queries that modify data unless explicitly asked. ",
|
|
135
|
+
query: {
|
|
136
|
+
description: `A tool for executing SQL queries in DuckDB that is embedded in browser using duckdb-wasm.
|
|
137
|
+
Query results are returned as a json object "{success: boolean, data: object[], error?: string}"
|
|
138
|
+
Please only analyze tables which are in the main schema.
|
|
139
|
+
To obtain stats, use the "SUMMARIZE table_name" query.
|
|
140
|
+
Don't execute queries that modify data unless explicitly asked.`,
|
|
47
141
|
parameters: QueryToolParameters,
|
|
48
|
-
|
|
142
|
+
executeWithContext: async (props) => {
|
|
143
|
+
if (!isQueryToolParameters(props.functionArgs)) {
|
|
144
|
+
return {
|
|
145
|
+
name: 'query',
|
|
146
|
+
result: {
|
|
147
|
+
success: false,
|
|
148
|
+
error: 'Invalid query parameters',
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
const { type, sqlQuery } = props.functionArgs;
|
|
49
153
|
try {
|
|
50
154
|
const { conn } = await getDuckDb();
|
|
51
155
|
// TODO use options.abortSignal: maybe call db.cancelPendingQuery
|
|
52
156
|
const result = await conn.query(sqlQuery);
|
|
53
|
-
// if
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
157
|
+
// Only get summary if the query isn't already a SUMMARIZE query
|
|
158
|
+
const summaryData = sqlQuery.toLowerCase().includes('summarize')
|
|
159
|
+
? arrowTableToJson(result)
|
|
160
|
+
: await getQuerySummary(conn, sqlQuery);
|
|
161
|
+
// Get first 2 rows of the result as a json object
|
|
162
|
+
const subResult = result.slice(0, 2);
|
|
163
|
+
const firstTwoRows = arrowTableToJson(subResult);
|
|
164
|
+
// create result object sent back to LLM for tool call
|
|
165
|
+
const llmResult = {
|
|
166
|
+
type,
|
|
57
167
|
success: true,
|
|
58
|
-
data:
|
|
168
|
+
data: {
|
|
169
|
+
// only summary and first two rows will be sent back to LLM as context
|
|
170
|
+
summary: summaryData,
|
|
171
|
+
firstTwoRows,
|
|
172
|
+
},
|
|
173
|
+
};
|
|
174
|
+
// data object of the raw query result, which is NOT sent back to LLM
|
|
175
|
+
// we can use it to visualize the arrow table in the callback function `message()` below
|
|
176
|
+
const data = { sqlQuery };
|
|
177
|
+
return {
|
|
178
|
+
name: 'query',
|
|
179
|
+
result: llmResult,
|
|
180
|
+
data,
|
|
59
181
|
};
|
|
60
182
|
}
|
|
61
183
|
catch (error) {
|
|
62
|
-
console.error('SQL query error:', error);
|
|
63
|
-
const errorMessage = error instanceof Error
|
|
64
|
-
? error.cause instanceof Error
|
|
65
|
-
? error.cause.message
|
|
66
|
-
: error.message
|
|
67
|
-
: String(error);
|
|
68
184
|
return {
|
|
69
|
-
|
|
70
|
-
|
|
185
|
+
name: 'query',
|
|
186
|
+
result: {
|
|
187
|
+
success: false,
|
|
188
|
+
description: 'Failed to execute the query. Please stop tool call and return error message.',
|
|
189
|
+
error: getErrorMessage(error),
|
|
190
|
+
},
|
|
71
191
|
};
|
|
72
192
|
}
|
|
73
193
|
},
|
|
74
|
-
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
194
|
+
message: queryMessage,
|
|
195
|
+
},
|
|
196
|
+
chart: {
|
|
197
|
+
description: `A tool for creating VegaLite charts based on the schema of the SQL query result from the "query" tool.
|
|
198
|
+
In the response:
|
|
199
|
+
- omit the data from the vegaLiteSpec
|
|
200
|
+
- provide an sql query in sqlQuery instead.`,
|
|
201
|
+
parameters: ChartToolParameters,
|
|
202
|
+
executeWithContext: async (props) => {
|
|
203
|
+
if (!isChartToolParameters(props.functionArgs)) {
|
|
204
|
+
return {
|
|
205
|
+
name: 'chart',
|
|
206
|
+
result: {
|
|
207
|
+
success: false,
|
|
208
|
+
error: 'Invalid chart parameters',
|
|
209
|
+
},
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
const { sqlQuery, vegaLiteSpec } = props.functionArgs;
|
|
213
|
+
const llmResult = {
|
|
82
214
|
success: true,
|
|
83
|
-
|
|
215
|
+
details: 'Chart created successfully.',
|
|
216
|
+
};
|
|
217
|
+
// data object of the vegaLiteSpec and sqlQuery
|
|
218
|
+
// it is not used yet, but we can use it to create a JSON editor for user to edit the vegaLiteSpec so that chart can be updated
|
|
219
|
+
const data = {
|
|
220
|
+
sqlQuery,
|
|
221
|
+
vegaLiteSpec,
|
|
222
|
+
};
|
|
223
|
+
return {
|
|
224
|
+
name: 'chart',
|
|
225
|
+
result: llmResult,
|
|
226
|
+
data,
|
|
84
227
|
};
|
|
85
228
|
},
|
|
86
|
-
}
|
|
229
|
+
},
|
|
230
|
+
// answer tool: the LLM will provide a structured answer
|
|
231
|
+
// answer: {
|
|
232
|
+
// description: 'A tool for providing the final answer.',
|
|
233
|
+
// parameters: AnswerToolParameters,
|
|
234
|
+
// executeWithContext: async (props: CallbackFunctionProps) => {
|
|
235
|
+
// const {answer} = props.functionArgs;
|
|
236
|
+
// return {
|
|
237
|
+
// name: 'answer',
|
|
238
|
+
// result: {
|
|
239
|
+
// success: true,
|
|
240
|
+
// data: answer,
|
|
241
|
+
// },
|
|
242
|
+
// };
|
|
243
|
+
// },
|
|
244
|
+
// },
|
|
87
245
|
};
|
|
88
246
|
//# sourceMappingURL=analysis.js.map
|
package/dist/analysis.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"analysis.js","sourceRoot":"","sources":["../src/analysis.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,gBAAgB,EAAE,SAAS,EAAC,MAAM,kBAAkB,CAAC;AAC7D,OAAO,EAAC,YAAY,EAAc,IAAI,EAAuB,MAAM,IAAI,CAAC;AACxE,OAAO,EACL,oBAAoB,EACpB,mBAAmB,GAEpB,MAAM,WAAW,CAAC;AAEnB;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAAC,EAChC,KAAK;AACL,UAAU;AACV,WAAW,EACX,YAAY,EACZ,QAAQ,GAAG,GAAG,EACd,QAAQ,GAQT;IACC,MAAM,MAAM,GAAG,MAAM,YAAY,CAAC;QAChC,KAAK;QAEL,WAAW;QACX,UAAU;QACV,QAAQ;QAER,KAAK,EAAE,KAAK;QAEZ,UAAU,EAAE,MAAM;QAClB,QAAQ;QACR,UAAU,EAAE,CAAC;QAEb,MAAM,EACJ,2EAA2E;YAC3E,oEAAoE;YACpE,uBAAuB;YACvB,4EAA4E;QAE9E,YAAY;KACb,CAAC,CAAC;IAEH,wEAAwE;IACxE,iBAAiB;IACjB,0DAA0D;IAC1D,kDAAkD;IAClD,IAAI;IACJ,sBAAsB;IAEtB,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,MAAM,KAAK,GAAG;IACZ,KAAK,EAAE,IAAI,CAAC;QACV,WAAW,EACT,4FAA4F;YAC5F,4FAA4F;YAC5F,oGAAoG;YACpG,+DAA+D;YAC/D,8EAA8E;YAC9E,2EAA2E;YAC3E,6GAA6G;YAC7G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CACN,CAAC","sourcesContent":["import {\n arrowTableToJson,\n getDuckDb,\n getDuckTableSchemas,\n} from '@sqlrooms/duckdb';\nimport {StepResult} from 'ai';\nimport * as duckdb from '@duckdb/duckdb-wasm';\nimport {\n CallbackFunctionProps,\n createAssistant,\n ToolCallMessage,\n VercelToolSet,\n} from '@openassistant/core';\n\nimport {ChartToolParameters, QueryToolParameters} from './schemas';\nimport {queryMessage} from './QueryResult';\nimport {isChartToolParameters, isQueryToolParameters} from './ToolCall';\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 SYSTEM_PROMPT = `\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 alwasy provide the final answer.\n\nPlease use the following schema for the tables:\n`;\n\n/**\n * Generates summary statistics for a SQL query result\n * @param conn - 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(\n conn: duckdb.AsyncDuckDBConnection,\n sqlQuery: string,\n) {\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 conn.query(`CREATE TEMPORARY VIEW ${viewName} AS ${sqlQuery}`);\n const summaryResult = await conn.query(`SUMMARIZE ${viewName}`);\n const summaryData = arrowTableToJson(summaryResult);\n await conn.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 */\nexport type AnalysisConfig = {\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 /**\n * Callback fired after each analysis step completion\n * @param event - Current step result containing tool execution details. See Vercel AI SDK documentation for more details.\n * Specifically, it contains the array of tool calls and the results of the tool calls (toolResults).\n * @param toolCallMessages - Collection of messages generated during tool calls. They are linked to the tool call by the toolCallId.\n */\n onStepFinish?: (\n event: StepResult<typeof TOOLS>,\n toolCallMessages: ToolCallMessage[],\n ) => Promise<void> | void;\n\n /** Maximum number of analysis steps allowed (default: 100) */\n maxSteps?: number;\n\n /**\n * Callback for handling streaming results\n * @param message - Current message content being streamed\n * @param isCompleted - Indicates if this is the final message in the stream\n */\n onStreamResult: (message: string, isCompleted: boolean) => void;\n};\n\n/**\n * Executes an AI analysis session on the project data\n *\n * @param config - Analysis configuration options. See {@link AnalysisConfig} 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 modelProvider,\n model,\n apiKey,\n prompt,\n abortController,\n onStepFinish,\n onStreamResult,\n maxSteps = 5,\n}: AnalysisConfig) {\n const tablesSchema = await getDuckTableSchemas();\n\n // get the singlton assistant instance\n const assistant = await createAssistant({\n name,\n modelProvider,\n model,\n apiKey,\n version: 'v1',\n instructions: `${SYSTEM_PROMPT}\\n${JSON.stringify(tablesSchema)}`,\n vercelFunctions: TOOLS,\n temperature: 0,\n toolChoice: 'auto', // this will enable streaming\n maxSteps,\n ...(abortController ? {abortController} : {}),\n });\n\n // process the prompt\n const result = await assistant.processTextMessage({\n textMessage: prompt,\n streamMessageCallback: (message) => {\n // the final result (before the answer) can be streamed back here\n onStreamResult(message.deltaMessage, message.isCompleted ?? false);\n },\n onStepFinish,\n });\n\n return result;\n}\n\n/**\n * Extracts a readable error message from an error object\n * @param error - Error object or unknown value\n * @returns Formatted error message string\n */\nfunction getErrorMessage(error: unknown): string {\n if (error instanceof Error) {\n if (error.cause instanceof Error) {\n return error.cause.message;\n }\n return error.message;\n }\n return String(error);\n}\n\n/**\n * Collection of tools available to the AI assistant for data analysis\n * Includes:\n * - query: Executes SQL queries against DuckDB\n * - chart: Creates VegaLite visualizations\n */\nconst TOOLS: VercelToolSet = {\n query: {\n description: `A tool for executing SQL queries in DuckDB that is embedded in browser using duckdb-wasm.\nQuery results are returned as a json object \"{success: boolean, data: object[], error?: string}\"\nPlease only analyze tables which are in the main schema.\nTo obtain stats, use the \"SUMMARIZE table_name\" query.\nDon't execute queries that modify data unless explicitly asked.`,\n parameters: QueryToolParameters,\n executeWithContext: async (props: CallbackFunctionProps) => {\n if (!isQueryToolParameters(props.functionArgs)) {\n return {\n name: 'query',\n result: {\n success: false,\n error: 'Invalid query parameters',\n },\n };\n }\n\n const {type, sqlQuery} = props.functionArgs;\n try {\n const {conn} = await getDuckDb();\n // TODO use options.abortSignal: maybe call db.cancelPendingQuery\n const result = await conn.query(sqlQuery);\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(conn, 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 // create result object sent back to LLM for tool call\n const llmResult = {\n type,\n success: true,\n data: {\n // only summary and first two rows will be sent back to LLM as context\n summary: summaryData,\n firstTwoRows,\n },\n };\n\n // data object of the raw query result, which is NOT sent back to LLM\n // we can use it to visualize the arrow table in the callback function `message()` below\n const data = {sqlQuery};\n\n return {\n name: 'query',\n result: llmResult,\n data,\n };\n } catch (error) {\n return {\n name: 'query',\n result: {\n success: false,\n description:\n 'Failed to execute the query. Please stop tool call and return error message.',\n error: getErrorMessage(error),\n },\n };\n }\n },\n message: queryMessage,\n },\n\n chart: {\n description: `A tool for creating VegaLite charts based on the schema of the SQL query result from the \"query\" tool.\nIn the response:\n- omit the data from the vegaLiteSpec\n- provide an sql query in sqlQuery instead.`,\n parameters: ChartToolParameters,\n executeWithContext: async (props: CallbackFunctionProps) => {\n if (!isChartToolParameters(props.functionArgs)) {\n return {\n name: 'chart',\n result: {\n success: false,\n error: 'Invalid chart parameters',\n },\n };\n }\n const {sqlQuery, vegaLiteSpec} = props.functionArgs;\n const llmResult = {\n success: true,\n details: 'Chart created successfully.',\n };\n\n // data object of the vegaLiteSpec and sqlQuery\n // it is not used yet, but we can use it to create a JSON editor for user to edit the vegaLiteSpec so that chart can be updated\n const data = {\n sqlQuery,\n vegaLiteSpec,\n };\n\n return {\n name: 'chart',\n result: llmResult,\n data,\n };\n },\n },\n\n // answer tool: the LLM will provide a structured answer\n // answer: {\n // description: 'A tool for providing the final answer.',\n // parameters: AnswerToolParameters,\n // executeWithContext: async (props: CallbackFunctionProps) => {\n // const {answer} = props.functionArgs;\n // return {\n // name: 'answer',\n // result: {\n // success: true,\n // data: answer,\n // },\n // };\n // },\n // },\n};\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-scroll-to-bottom.js","sourceRoot":"","sources":["../../src/hooks/use-scroll-to-bottom.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,SAAS,EAAE,MAAM,EAAkB,QAAQ,EAAC,MAAM,OAAO,CAAC;AAElE,MAAM,UAAU,iBAAiB,CAC/B,UAAgC;IAC9B,SAAS,EAAE,IAAI;IACf,OAAO,EAAE,IAAI;IACb,aAAa,EAAE,IAAI;IACnB,oBAAoB;CACrB;IAED,MAAM,YAAY,GAAG,MAAM,CAAI,IAAI,CAAC,CAAC;IACrC,MAAM,MAAM,GAAG,MAAM,CAAI,IAAI,CAAC,CAAC;IAE/B,SAAS,CAAC,GAAG,EAAE;QACb,MAAM,SAAS,GAAG,YAAY,CAAC,OAAO,CAAC;QACvC,MAAM,GAAG,GAAG,MAAM,CAAC,OAAO,CAAC;QAE3B,IAAI,SAAS,IAAI,GAAG,EAAE,CAAC;YACrB,MAAM,QAAQ,GAAG,IAAI,gBAAgB,CAAC,GAAG,EAAE;gBACzC,GAAG,CAAC,cAAc,CAAC,EAAC,QAAQ,EAAE,SAAS,EAAE,KAAK,EAAE,KAAK,EAAC,CAAC,CAAC;YAC1D,CAAC,CAAC,CAAC;YAEH,QAAQ,CAAC,OAAO,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;YAErC,OAAO,GAAG,EAAE,CAAC,QAAQ,CAAC,UAAU,EAAE,CAAC;QACrC,CAAC;IACH,CAAC,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;IAEd,OAAO,CAAC,YAAY,EAAE,MAAM,CAAC,CAAC;AAChC,CAAC;AAED,MAAM,UAAU,uBAAuB,CAAC,YAAoC;IAC1E,MAAM,CAAC,UAAU,EAAE,aAAa,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC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CAAC,gBAAgB,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAC;QAEnD,iEAAiE;QACjE,MAAM,SAAS,GAAG,UAAU,CAAC,YAAY,EAAE,GAAG,CAAC,CAAC;QAEhD,OAAO,GAAG,EAAE;YACV,SAAS,CAAC,mBAAmB,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAC;YACtD,YAAY,CAAC,SAAS,CAAC,CAAC;QAC1B,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,YAAY,CAAC,CAAC,CAAC;IAEnB,MAAM,cAAc,GAAG,GAAG,EAAE;QAC1B,YAAY,CAAC,OAAO,EAAE,QAAQ,CAAC;YAC7B,GAAG,EAAE,YAAY,CAAC,OAAO,CAAC,YAAY;YACtC,QAAQ,EAAE,QAAQ;SACnB,CAAC,CAAC;IACL,CAAC,CAAC;IAEF,OAAO,EAAC,UAAU,EAAE,cAAc,EAAC,CAAC;AACtC,CAAC","sourcesContent":["import {useEffect, useRef, type RefObject, useState} from 'react';\n\nexport function useScrollToBottom<T extends HTMLElement>(\n options: MutationObserverInit = {\n childList: true,\n subtree: true,\n characterData: true,\n // attributes: true,\n },\n): [RefObject<T>, RefObject<T>] {\n const containerRef = useRef<T>(null);\n const endRef = useRef<T>(null);\n\n useEffect(() => {\n const container = containerRef.current;\n const end = endRef.current;\n\n if (container && end) {\n const observer = new MutationObserver(() => {\n end.scrollIntoView({behavior: 'instant', block: 'end'});\n });\n\n observer.observe(container, options);\n\n return () => observer.disconnect();\n }\n }, [options]);\n\n return [containerRef, endRef];\n}\n\nexport function useScrollToBottomButton(containerRef: RefObject<HTMLElement>) {\n const [showButton, setShowButton] = useState(false);\n\n useEffect(() => {\n const container = containerRef.current;\n if (!container) return;\n\n const handleScroll = () => {\n const {scrollTop, scrollHeight, clientHeight} = container;\n // Only show if we're scrolled up more than 200px from the bottom\n const isNotAtBottom = scrollHeight - scrollTop - clientHeight > 200;\n setShowButton(isNotAtBottom);\n };\n\n container.addEventListener('scroll', handleScroll);\n\n // Initial check with a slight delay to ensure proper measurement\n const timeoutId = setTimeout(handleScroll, 100);\n\n return () => {\n container.removeEventListener('scroll', handleScroll);\n clearTimeout(timeoutId);\n };\n }, [containerRef]);\n\n const scrollToBottom = () => {\n containerRef.current?.scrollTo({\n top: containerRef.current.scrollHeight,\n behavior: 'smooth',\n });\n };\n\n return {showButton, scrollToBottom};\n}\n"]}
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EACb,aAAa,EACb,cAAc,IAAI,UAAU,EAC5B,qBAAqB,GACtB,MAAM,WAAW,CAAC;AAGnB,OAAO,EAAC,aAAa,EAAC,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAC,wBAAwB,EAAC,MAAM,4BAA4B,CAAC;AACpE,OAAO,EAAC,cAAc,EAAC,MAAM,kBAAkB,CAAC;AAChD,OAAO,EACL,iBAAiB,EACjB,uBAAuB,GACxB,MAAM,8BAA8B,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EACb,aAAa,EACb,cAAc,IAAI,UAAU,EAC5B,qBAAqB,GACtB,MAAM,WAAW,CAAC;AAGnB,OAAO,EAAC,aAAa,EAAC,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAC,wBAAwB,EAAC,MAAM,4BAA4B,CAAC;AACpE,OAAO,EAAC,cAAc,EAAC,MAAM,kBAAkB,CAAC;AAChD,OAAO,EACL,iBAAiB,EACjB,uBAAuB,GACxB,MAAM,8BAA8B,CAAC","sourcesContent":["export {\n AiSliceConfig,\n createAiSlice,\n useStoreWithAi as useAiStore,\n createDefaultAiConfig,\n} from './AiSlice';\n\nexport type {AiSliceState} from './AiSlice';\nexport {QueryControls} from './QueryControls';\nexport {AnalysisResultsContainer} from './AnalysisResultsContainer';\nexport {AnalysisResult} from './AnalysisResult';\nexport {\n useScrollToBottom,\n useScrollToBottomButton,\n} from './hooks/use-scroll-to-bottom';\n"]}
|
package/dist/schemas.d.ts
CHANGED
|
@@ -13,35 +13,22 @@ export declare const QueryToolParameters: z.ZodObject<{
|
|
|
13
13
|
reasoning: string;
|
|
14
14
|
}>;
|
|
15
15
|
export type QueryToolParameters = z.infer<typeof QueryToolParameters>;
|
|
16
|
-
export declare const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}, "strip", z.ZodTypeAny, {
|
|
23
|
-
sqlQuery: string;
|
|
24
|
-
vegaLiteSpec: string;
|
|
25
|
-
}, {
|
|
26
|
-
sqlQuery: string;
|
|
27
|
-
vegaLiteSpec: string;
|
|
28
|
-
}>, z.ZodNull]>;
|
|
16
|
+
export declare const AnalysisSchema: z.ZodString;
|
|
17
|
+
export type AnalysisSchema = z.infer<typeof AnalysisSchema>;
|
|
18
|
+
export declare const ChartToolParameters: z.ZodObject<{
|
|
19
|
+
sqlQuery: z.ZodString;
|
|
20
|
+
vegaLiteSpec: z.ZodString;
|
|
21
|
+
reasoning: z.ZodString;
|
|
29
22
|
}, "strip", z.ZodTypeAny, {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
sqlQuery: string;
|
|
34
|
-
vegaLiteSpec: string;
|
|
35
|
-
} | null;
|
|
23
|
+
sqlQuery: string;
|
|
24
|
+
reasoning: string;
|
|
25
|
+
vegaLiteSpec: string;
|
|
36
26
|
}, {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
sqlQuery: string;
|
|
41
|
-
vegaLiteSpec: string;
|
|
42
|
-
} | null;
|
|
27
|
+
sqlQuery: string;
|
|
28
|
+
reasoning: string;
|
|
29
|
+
vegaLiteSpec: string;
|
|
43
30
|
}>;
|
|
44
|
-
export type
|
|
31
|
+
export type ChartToolParameters = z.infer<typeof ChartToolParameters>;
|
|
45
32
|
export declare const ToolCallSchema: z.ZodObject<{
|
|
46
33
|
toolName: z.ZodString;
|
|
47
34
|
toolCallId: z.ZodString;
|
|
@@ -58,32 +45,17 @@ export declare const ToolCallSchema: z.ZodObject<{
|
|
|
58
45
|
sqlQuery: string;
|
|
59
46
|
reasoning: string;
|
|
60
47
|
}>, z.ZodObject<{
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
sqlQuery: z.ZodString;
|
|
65
|
-
vegaLiteSpec: z.ZodString;
|
|
66
|
-
}, "strip", z.ZodTypeAny, {
|
|
67
|
-
sqlQuery: string;
|
|
68
|
-
vegaLiteSpec: string;
|
|
69
|
-
}, {
|
|
70
|
-
sqlQuery: string;
|
|
71
|
-
vegaLiteSpec: string;
|
|
72
|
-
}>, z.ZodNull]>;
|
|
48
|
+
sqlQuery: z.ZodString;
|
|
49
|
+
vegaLiteSpec: z.ZodString;
|
|
50
|
+
reasoning: z.ZodString;
|
|
73
51
|
}, "strip", z.ZodTypeAny, {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
sqlQuery: string;
|
|
78
|
-
vegaLiteSpec: string;
|
|
79
|
-
} | null;
|
|
52
|
+
sqlQuery: string;
|
|
53
|
+
reasoning: string;
|
|
54
|
+
vegaLiteSpec: string;
|
|
80
55
|
}, {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
sqlQuery: string;
|
|
85
|
-
vegaLiteSpec: string;
|
|
86
|
-
} | null;
|
|
56
|
+
sqlQuery: string;
|
|
57
|
+
reasoning: string;
|
|
58
|
+
vegaLiteSpec: string;
|
|
87
59
|
}>]>;
|
|
88
60
|
}, "strip", z.ZodTypeAny, {
|
|
89
61
|
toolName: string;
|
|
@@ -93,12 +65,9 @@ export declare const ToolCallSchema: z.ZodObject<{
|
|
|
93
65
|
sqlQuery: string;
|
|
94
66
|
reasoning: string;
|
|
95
67
|
} | {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
sqlQuery: string;
|
|
100
|
-
vegaLiteSpec: string;
|
|
101
|
-
} | null;
|
|
68
|
+
sqlQuery: string;
|
|
69
|
+
reasoning: string;
|
|
70
|
+
vegaLiteSpec: string;
|
|
102
71
|
};
|
|
103
72
|
}, {
|
|
104
73
|
toolName: string;
|
|
@@ -108,15 +77,23 @@ export declare const ToolCallSchema: z.ZodObject<{
|
|
|
108
77
|
sqlQuery: string;
|
|
109
78
|
reasoning: string;
|
|
110
79
|
} | {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
sqlQuery: string;
|
|
115
|
-
vegaLiteSpec: string;
|
|
116
|
-
} | null;
|
|
80
|
+
sqlQuery: string;
|
|
81
|
+
reasoning: string;
|
|
82
|
+
vegaLiteSpec: string;
|
|
117
83
|
};
|
|
118
84
|
}>;
|
|
119
85
|
export type ToolCallSchema = z.infer<typeof ToolCallSchema>;
|
|
86
|
+
export declare const ToolCallMessageSchema: z.ZodObject<{
|
|
87
|
+
toolCallId: z.ZodString;
|
|
88
|
+
element: z.ZodAny;
|
|
89
|
+
}, "strip", z.ZodTypeAny, {
|
|
90
|
+
toolCallId: string;
|
|
91
|
+
element?: any;
|
|
92
|
+
}, {
|
|
93
|
+
toolCallId: string;
|
|
94
|
+
element?: any;
|
|
95
|
+
}>;
|
|
96
|
+
export type ToolCallMessageSchema = z.infer<typeof ToolCallMessageSchema>;
|
|
120
97
|
export declare const ToolResultSchema: z.ZodObject<{
|
|
121
98
|
toolName: z.ZodString;
|
|
122
99
|
toolCallId: z.ZodString;
|
|
@@ -229,32 +206,17 @@ export declare const AnalysisResultSchema: z.ZodObject<{
|
|
|
229
206
|
sqlQuery: string;
|
|
230
207
|
reasoning: string;
|
|
231
208
|
}>, z.ZodObject<{
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
sqlQuery: z.ZodString;
|
|
236
|
-
vegaLiteSpec: z.ZodString;
|
|
237
|
-
}, "strip", z.ZodTypeAny, {
|
|
238
|
-
sqlQuery: string;
|
|
239
|
-
vegaLiteSpec: string;
|
|
240
|
-
}, {
|
|
241
|
-
sqlQuery: string;
|
|
242
|
-
vegaLiteSpec: string;
|
|
243
|
-
}>, z.ZodNull]>;
|
|
209
|
+
sqlQuery: z.ZodString;
|
|
210
|
+
vegaLiteSpec: z.ZodString;
|
|
211
|
+
reasoning: z.ZodString;
|
|
244
212
|
}, "strip", z.ZodTypeAny, {
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
sqlQuery: string;
|
|
249
|
-
vegaLiteSpec: string;
|
|
250
|
-
} | null;
|
|
213
|
+
sqlQuery: string;
|
|
214
|
+
reasoning: string;
|
|
215
|
+
vegaLiteSpec: string;
|
|
251
216
|
}, {
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
sqlQuery: string;
|
|
256
|
-
vegaLiteSpec: string;
|
|
257
|
-
} | null;
|
|
217
|
+
sqlQuery: string;
|
|
218
|
+
reasoning: string;
|
|
219
|
+
vegaLiteSpec: string;
|
|
258
220
|
}>]>;
|
|
259
221
|
}, "strip", z.ZodTypeAny, {
|
|
260
222
|
toolName: string;
|
|
@@ -264,12 +226,9 @@ export declare const AnalysisResultSchema: z.ZodObject<{
|
|
|
264
226
|
sqlQuery: string;
|
|
265
227
|
reasoning: string;
|
|
266
228
|
} | {
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
sqlQuery: string;
|
|
271
|
-
vegaLiteSpec: string;
|
|
272
|
-
} | null;
|
|
229
|
+
sqlQuery: string;
|
|
230
|
+
reasoning: string;
|
|
231
|
+
vegaLiteSpec: string;
|
|
273
232
|
};
|
|
274
233
|
}, {
|
|
275
234
|
toolName: string;
|
|
@@ -279,14 +238,23 @@ export declare const AnalysisResultSchema: z.ZodObject<{
|
|
|
279
238
|
sqlQuery: string;
|
|
280
239
|
reasoning: string;
|
|
281
240
|
} | {
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
sqlQuery: string;
|
|
286
|
-
vegaLiteSpec: string;
|
|
287
|
-
} | null;
|
|
241
|
+
sqlQuery: string;
|
|
242
|
+
reasoning: string;
|
|
243
|
+
vegaLiteSpec: string;
|
|
288
244
|
};
|
|
289
245
|
}>, "many">;
|
|
246
|
+
toolCallMessages: z.ZodArray<z.ZodObject<{
|
|
247
|
+
toolCallId: z.ZodString;
|
|
248
|
+
element: z.ZodAny;
|
|
249
|
+
}, "strip", z.ZodTypeAny, {
|
|
250
|
+
toolCallId: string;
|
|
251
|
+
element?: any;
|
|
252
|
+
}, {
|
|
253
|
+
toolCallId: string;
|
|
254
|
+
element?: any;
|
|
255
|
+
}>, "many">;
|
|
256
|
+
analysis: z.ZodString;
|
|
257
|
+
isCompleted: z.ZodBoolean;
|
|
290
258
|
}, "strip", z.ZodTypeAny, {
|
|
291
259
|
id: string;
|
|
292
260
|
prompt: string;
|
|
@@ -310,14 +278,17 @@ export declare const AnalysisResultSchema: z.ZodObject<{
|
|
|
310
278
|
sqlQuery: string;
|
|
311
279
|
reasoning: string;
|
|
312
280
|
} | {
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
sqlQuery: string;
|
|
317
|
-
vegaLiteSpec: string;
|
|
318
|
-
} | null;
|
|
281
|
+
sqlQuery: string;
|
|
282
|
+
reasoning: string;
|
|
283
|
+
vegaLiteSpec: string;
|
|
319
284
|
};
|
|
320
285
|
}[];
|
|
286
|
+
toolCallMessages: {
|
|
287
|
+
toolCallId: string;
|
|
288
|
+
element?: any;
|
|
289
|
+
}[];
|
|
290
|
+
analysis: string;
|
|
291
|
+
isCompleted: boolean;
|
|
321
292
|
}, {
|
|
322
293
|
id: string;
|
|
323
294
|
prompt: string;
|
|
@@ -341,14 +312,17 @@ export declare const AnalysisResultSchema: z.ZodObject<{
|
|
|
341
312
|
sqlQuery: string;
|
|
342
313
|
reasoning: string;
|
|
343
314
|
} | {
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
sqlQuery: string;
|
|
348
|
-
vegaLiteSpec: string;
|
|
349
|
-
} | null;
|
|
315
|
+
sqlQuery: string;
|
|
316
|
+
reasoning: string;
|
|
317
|
+
vegaLiteSpec: string;
|
|
350
318
|
};
|
|
351
319
|
}[];
|
|
320
|
+
toolCallMessages: {
|
|
321
|
+
toolCallId: string;
|
|
322
|
+
element?: any;
|
|
323
|
+
}[];
|
|
324
|
+
analysis: string;
|
|
325
|
+
isCompleted: boolean;
|
|
352
326
|
}>;
|
|
353
327
|
export type AnalysisResultSchema = z.infer<typeof AnalysisResultSchema>;
|
|
354
328
|
//# sourceMappingURL=schemas.d.ts.map
|
package/dist/schemas.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schemas.d.ts","sourceRoot":"","sources":["../src/schemas.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,CAAC,EAAC,MAAM,KAAK,CAAC;AAEtB,eAAO,MAAM,mBAAmB;;;;;;;;;;;;EAI9B,CAAC;AACH,MAAM,MAAM,mBAAmB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,mBAAmB,CAAC,CAAC;AAEtE,eAAO,MAAM,
|
|
1
|
+
{"version":3,"file":"schemas.d.ts","sourceRoot":"","sources":["../src/schemas.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,CAAC,EAAC,MAAM,KAAK,CAAC;AAEtB,eAAO,MAAM,mBAAmB;;;;;;;;;;;;EAI9B,CAAC;AACH,MAAM,MAAM,mBAAmB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,mBAAmB,CAAC,CAAC;AAEtE,eAAO,MAAM,cAAc,aAAa,CAAC;AACzC,MAAM,MAAM,cAAc,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,cAAc,CAAC,CAAC;AAE5D,eAAO,MAAM,mBAAmB;;;;;;;;;;;;EAI9B,CAAC;AACH,MAAM,MAAM,mBAAmB,GAAG,CAAC,CAAC,KAAK,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