@smallwebco/tinypivot-react 1.0.61 → 1.0.63
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/index.cjs +35 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +35 -4
- package/dist/index.js.map +1 -1
- package/dist/style.css +1302 -0
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -101,6 +101,7 @@ function useAIAnalyst(options) {
|
|
|
101
101
|
}, [storageKey]);
|
|
102
102
|
const [conversation, setConversation] = (0, import_react.useState)(() => loadFromStorage());
|
|
103
103
|
const [schemas, setSchemas] = (0, import_react.useState)(/* @__PURE__ */ new Map());
|
|
104
|
+
const [allSchemas, setAllSchemas] = (0, import_react.useState)([]);
|
|
104
105
|
const [isLoading, setIsLoading] = (0, import_react.useState)(false);
|
|
105
106
|
const [error, setError] = (0, import_react.useState)(null);
|
|
106
107
|
const [lastLoadedData, setLastLoadedData] = (0, import_react.useState)(null);
|
|
@@ -122,6 +123,34 @@ function useAIAnalyst(options) {
|
|
|
122
123
|
);
|
|
123
124
|
const messages = conversation.messages;
|
|
124
125
|
const hasMessages = conversation.messages.length > 0;
|
|
126
|
+
const fetchAllSchemas = (0, import_react.useCallback)(async () => {
|
|
127
|
+
if (!configRef.current.endpoint)
|
|
128
|
+
return;
|
|
129
|
+
try {
|
|
130
|
+
const response = await fetch(configRef.current.endpoint, {
|
|
131
|
+
method: "POST",
|
|
132
|
+
headers: { "Content-Type": "application/json" },
|
|
133
|
+
body: JSON.stringify({ action: "get-all-schemas" })
|
|
134
|
+
});
|
|
135
|
+
if (!response.ok) {
|
|
136
|
+
throw new Error(`Failed to fetch all schemas: ${response.statusText}`);
|
|
137
|
+
}
|
|
138
|
+
const data = await response.json();
|
|
139
|
+
if (data.error) {
|
|
140
|
+
throw new Error(data.error);
|
|
141
|
+
}
|
|
142
|
+
setAllSchemas(data.schemas);
|
|
143
|
+
setSchemas((prev) => {
|
|
144
|
+
const newMap = new Map(prev);
|
|
145
|
+
for (const schema of data.schemas) {
|
|
146
|
+
newMap.set(schema.table, schema);
|
|
147
|
+
}
|
|
148
|
+
return newMap;
|
|
149
|
+
});
|
|
150
|
+
} catch (err) {
|
|
151
|
+
console.warn("[TinyPivot] Failed to fetch all schemas:", err);
|
|
152
|
+
}
|
|
153
|
+
}, []);
|
|
125
154
|
const fetchTables = (0, import_react.useCallback)(async () => {
|
|
126
155
|
if (!configRef.current.endpoint)
|
|
127
156
|
return;
|
|
@@ -146,6 +175,7 @@ function useAIAnalyst(options) {
|
|
|
146
175
|
// Capitalize
|
|
147
176
|
description: t.description
|
|
148
177
|
})));
|
|
178
|
+
await fetchAllSchemas();
|
|
149
179
|
} catch (err) {
|
|
150
180
|
console.warn("[TinyPivot] Failed to fetch tables:", err);
|
|
151
181
|
onError?.({
|
|
@@ -155,7 +185,7 @@ function useAIAnalyst(options) {
|
|
|
155
185
|
} finally {
|
|
156
186
|
setIsLoadingTables(false);
|
|
157
187
|
}
|
|
158
|
-
}, [onError]);
|
|
188
|
+
}, [onError, fetchAllSchemas]);
|
|
159
189
|
(0, import_react.useEffect)(() => {
|
|
160
190
|
if (configRef.current.endpoint && (!config.dataSources || config.dataSources.length === 0)) {
|
|
161
191
|
fetchTables();
|
|
@@ -278,7 +308,7 @@ What would you like to know about this data?`
|
|
|
278
308
|
await fetchSampleData(dataSource);
|
|
279
309
|
}
|
|
280
310
|
}, [effectiveDataSources, fetchSchema, fetchSampleData, onConversationUpdate, onDataLoaded]);
|
|
281
|
-
const callAIEndpoint = (0, import_react.useCallback)(async (userInput, currentConversation, currentSchemas, currentDataSources) => {
|
|
311
|
+
const callAIEndpoint = (0, import_react.useCallback)(async (userInput, currentConversation, currentSchemas, currentDataSources, currentAllSchemas) => {
|
|
282
312
|
if (!configRef.current.endpoint) {
|
|
283
313
|
throw new Error("No endpoint configured. Set `endpoint` in AI analyst config.");
|
|
284
314
|
}
|
|
@@ -286,7 +316,8 @@ What would you like to know about this data?`
|
|
|
286
316
|
const systemPrompt = (0, import_tinypivot_core.buildSystemPrompt)(
|
|
287
317
|
currentDataSources,
|
|
288
318
|
currentSchemas,
|
|
289
|
-
dataSourceId
|
|
319
|
+
dataSourceId,
|
|
320
|
+
currentAllSchemas.length > 0 ? currentAllSchemas : void 0
|
|
290
321
|
);
|
|
291
322
|
const apiMessages = (0, import_tinypivot_core.getMessagesForAPI)(currentConversation);
|
|
292
323
|
const messages2 = [
|
|
@@ -545,7 +576,7 @@ What would you like to know about this data?`
|
|
|
545
576
|
});
|
|
546
577
|
return;
|
|
547
578
|
}
|
|
548
|
-
const aiResponse = await callAIEndpoint(content, currentConv, schemas, effectiveDataSources);
|
|
579
|
+
const aiResponse = await callAIEndpoint(content, currentConv, schemas, effectiveDataSources, allSchemas);
|
|
549
580
|
const sqlQuery = (0, import_tinypivot_core.extractSQLFromResponse)(aiResponse);
|
|
550
581
|
if (sqlQuery) {
|
|
551
582
|
const validation = (0, import_tinypivot_core.validateSQLSafety)(sqlQuery);
|