@outsidedata/dolex 1.0.1 → 3.1.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 +6 -7
- package/dist/src/analysis/rules.d.ts.map +1 -1
- package/dist/src/analysis/rules.js +29 -36
- package/dist/src/analysis/rules.js.map +1 -1
- package/dist/src/analysis/types.d.ts +1 -2
- package/dist/src/analysis/types.d.ts.map +1 -1
- package/dist/src/connectors/csv.d.ts.map +1 -1
- package/dist/src/connectors/csv.js +131 -0
- package/dist/src/connectors/csv.js.map +1 -1
- package/dist/src/connectors/manager.d.ts +16 -12
- package/dist/src/connectors/manager.d.ts.map +1 -1
- package/dist/src/connectors/manager.js +49 -43
- package/dist/src/connectors/manager.js.map +1 -1
- package/dist/src/connectors/types.d.ts +4 -0
- package/dist/src/connectors/types.d.ts.map +1 -1
- package/dist/src/index.d.ts +2 -4
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +1 -2
- package/dist/src/index.js.map +1 -1
- package/dist/src/mcp/app-shell.js +1 -2
- package/dist/src/mcp/app-shell.js.map +1 -1
- package/dist/src/mcp/index.d.ts +8 -7
- package/dist/src/mcp/index.d.ts.map +1 -1
- package/dist/src/mcp/index.js +51 -73
- package/dist/src/mcp/index.js.map +1 -1
- package/dist/src/mcp/prompts.js +1 -1
- package/dist/src/mcp/tools/drop-columns.d.ts +12 -0
- package/dist/src/mcp/tools/drop-columns.d.ts.map +1 -0
- package/dist/src/mcp/tools/drop-columns.js +122 -0
- package/dist/src/mcp/tools/drop-columns.js.map +1 -0
- package/dist/src/mcp/tools/export-html.d.ts +6 -1
- package/dist/src/mcp/tools/export-html.d.ts.map +1 -1
- package/dist/src/mcp/tools/export-html.js +27 -8
- package/dist/src/mcp/tools/export-html.js.map +1 -1
- package/dist/src/mcp/tools/list-transforms.d.ts +11 -0
- package/dist/src/mcp/tools/list-transforms.d.ts.map +1 -0
- package/dist/src/mcp/tools/list-transforms.js +49 -0
- package/dist/src/mcp/tools/list-transforms.js.map +1 -0
- package/dist/src/mcp/tools/operation-log.d.ts +2 -13
- package/dist/src/mcp/tools/operation-log.d.ts.map +1 -1
- package/dist/src/mcp/tools/operation-log.js +1 -35
- package/dist/src/mcp/tools/operation-log.js.map +1 -1
- package/dist/src/mcp/tools/promote-columns.d.ts +11 -0
- package/dist/src/mcp/tools/promote-columns.d.ts.map +1 -0
- package/dist/src/mcp/tools/promote-columns.js +73 -0
- package/dist/src/mcp/tools/promote-columns.js.map +1 -0
- package/dist/src/mcp/tools/query-source.d.ts +4 -318
- package/dist/src/mcp/tools/query-source.d.ts.map +1 -1
- package/dist/src/mcp/tools/query-source.js +7 -13
- package/dist/src/mcp/tools/query-source.js.map +1 -1
- package/dist/src/mcp/tools/refine.d.ts +4 -4
- package/dist/src/mcp/tools/refine.js +2 -2
- package/dist/src/mcp/tools/refine.js.map +1 -1
- package/dist/src/mcp/tools/screenshot.js +1 -1
- package/dist/src/mcp/tools/screenshot.js.map +1 -1
- package/dist/src/mcp/tools/shared.d.ts +2 -23
- package/dist/src/mcp/tools/shared.d.ts.map +1 -1
- package/dist/src/mcp/tools/shared.js +4 -85
- package/dist/src/mcp/tools/shared.js.map +1 -1
- package/dist/src/mcp/tools/sources.js +1 -1
- package/dist/src/mcp/tools/sources.js.map +1 -1
- package/dist/src/mcp/tools/sql-schemas.d.ts +20 -0
- package/dist/src/mcp/tools/sql-schemas.d.ts.map +1 -0
- package/dist/src/mcp/tools/sql-schemas.js +18 -0
- package/dist/src/mcp/tools/sql-schemas.js.map +1 -0
- package/dist/src/mcp/tools/transform-data.d.ts +12 -0
- package/dist/src/mcp/tools/transform-data.d.ts.map +1 -0
- package/dist/src/mcp/tools/transform-data.js +73 -0
- package/dist/src/mcp/tools/transform-data.js.map +1 -0
- package/dist/src/mcp/tools/transform-schemas.d.ts +234 -0
- package/dist/src/mcp/tools/transform-schemas.d.ts.map +1 -0
- package/dist/src/mcp/tools/transform-schemas.js +35 -0
- package/dist/src/mcp/tools/transform-schemas.js.map +1 -0
- package/dist/src/mcp/tools/visualize.d.ts +18 -6
- package/dist/src/mcp/tools/visualize.d.ts.map +1 -1
- package/dist/src/mcp/tools/visualize.js +47 -12
- package/dist/src/mcp/tools/visualize.js.map +1 -1
- package/dist/src/renderers/html/index.d.ts +0 -2
- package/dist/src/renderers/html/index.d.ts.map +1 -1
- package/dist/src/renderers/html/index.js +0 -1
- package/dist/src/renderers/html/index.js.map +1 -1
- package/dist/src/transforms/column-functions.d.ts +19 -0
- package/dist/src/transforms/column-functions.d.ts.map +1 -0
- package/dist/src/transforms/column-functions.js +269 -0
- package/dist/src/transforms/column-functions.js.map +1 -0
- package/dist/src/transforms/column-manager.d.ts +35 -0
- package/dist/src/transforms/column-manager.d.ts.map +1 -0
- package/dist/src/transforms/column-manager.js +151 -0
- package/dist/src/transforms/column-manager.js.map +1 -0
- package/dist/src/transforms/dependency.d.ts +21 -0
- package/dist/src/transforms/dependency.d.ts.map +1 -0
- package/dist/src/transforms/dependency.js +129 -0
- package/dist/src/transforms/dependency.js.map +1 -0
- package/dist/src/transforms/evaluator.d.ts +38 -0
- package/dist/src/transforms/evaluator.d.ts.map +1 -0
- package/dist/src/transforms/evaluator.js +377 -0
- package/dist/src/transforms/evaluator.js.map +1 -0
- package/dist/src/transforms/index.d.ts +19 -0
- package/dist/src/transforms/index.d.ts.map +1 -0
- package/dist/src/transforms/index.js +21 -0
- package/dist/src/transforms/index.js.map +1 -0
- package/dist/src/transforms/manifest.d.ts +19 -0
- package/dist/src/transforms/manifest.d.ts.map +1 -0
- package/dist/src/transforms/manifest.js +136 -0
- package/dist/src/transforms/manifest.js.map +1 -0
- package/dist/src/transforms/metadata.d.ts +31 -0
- package/dist/src/transforms/metadata.d.ts.map +1 -0
- package/dist/src/transforms/metadata.js +116 -0
- package/dist/src/transforms/metadata.js.map +1 -0
- package/dist/src/transforms/parser.d.ts +22 -0
- package/dist/src/transforms/parser.d.ts.map +1 -0
- package/dist/src/transforms/parser.js +165 -0
- package/dist/src/transforms/parser.js.map +1 -0
- package/dist/src/transforms/pipeline.d.ts +40 -0
- package/dist/src/transforms/pipeline.d.ts.map +1 -0
- package/dist/src/transforms/pipeline.js +208 -0
- package/dist/src/transforms/pipeline.js.map +1 -0
- package/dist/src/transforms/row-functions.d.ts +11 -0
- package/dist/src/transforms/row-functions.d.ts.map +1 -0
- package/dist/src/transforms/row-functions.js +241 -0
- package/dist/src/transforms/row-functions.js.map +1 -0
- package/dist/src/transforms/tokenizer.d.ts +12 -0
- package/dist/src/transforms/tokenizer.d.ts.map +1 -0
- package/dist/src/transforms/tokenizer.js +135 -0
- package/dist/src/transforms/tokenizer.js.map +1 -0
- package/dist/src/transforms/types.d.ts +139 -0
- package/dist/src/transforms/types.d.ts.map +1 -0
- package/dist/src/transforms/types.js +19 -0
- package/dist/src/transforms/types.js.map +1 -0
- package/dist/src/types.d.ts +7 -164
- package/dist/src/types.d.ts.map +1 -1
- package/dist/src/types.js +0 -12
- package/dist/src/types.js.map +1 -1
- package/package.json +2 -2
- package/src/analysis/rules.ts +30 -37
- package/src/analysis/types.ts +1 -3
- package/src/connectors/csv.ts +147 -0
- package/src/connectors/manager.ts +69 -55
- package/src/connectors/types.ts +4 -0
- package/src/index.ts +3 -20
- package/src/mcp/app-shell.ts +1 -2
- package/src/mcp/index.ts +69 -83
- package/src/mcp/prompts.ts +1 -1
- package/src/mcp/tools/drop-columns.ts +144 -0
- package/src/mcp/tools/export-html.ts +25 -9
- package/src/mcp/tools/list-transforms.ts +63 -0
- package/src/mcp/tools/operation-log.ts +2 -50
- package/src/mcp/tools/promote-columns.ts +89 -0
- package/src/mcp/tools/query-source.ts +10 -17
- package/src/mcp/tools/refine.ts +2 -2
- package/src/mcp/tools/screenshot.ts +1 -1
- package/src/mcp/tools/shared.ts +5 -110
- package/src/mcp/tools/sources.ts +1 -1
- package/src/mcp/tools/sql-schemas.ts +22 -0
- package/src/mcp/tools/transform-data.ts +87 -0
- package/src/mcp/tools/transform-schemas.ts +45 -0
- package/src/mcp/tools/visualize.ts +52 -11
- package/src/renderers/html/index.ts +0 -2
- package/src/transforms/column-functions.ts +310 -0
- package/src/transforms/column-manager.ts +194 -0
- package/src/transforms/dependency.ts +141 -0
- package/src/transforms/evaluator.ts +418 -0
- package/src/transforms/index.ts +71 -0
- package/src/transforms/manifest.ts +162 -0
- package/src/transforms/metadata.ts +147 -0
- package/src/transforms/parser.ts +205 -0
- package/src/transforms/pipeline.ts +301 -0
- package/src/transforms/row-functions.ts +228 -0
- package/src/transforms/tokenizer.ts +154 -0
- package/src/transforms/types.ts +142 -0
- package/src/types.ts +8 -212
- package/src/connectors/dsl-compiler.ts +0 -361
- package/src/connectors/dsl-validator.ts +0 -495
- package/src/connectors/js-aggregation.ts +0 -404
- package/src/mcp/tools/dashboard-refine.ts +0 -456
- package/src/mcp/tools/dashboard.ts +0 -62
- package/src/mcp/tools/dsl-schemas.ts +0 -262
- package/src/mcp/tools/visualize-from-source.ts +0 -92
- package/src/renderers/html/builders/dashboard.ts +0 -449
package/src/mcp/tools/shared.ts
CHANGED
|
@@ -5,19 +5,13 @@
|
|
|
5
5
|
* - MCP response builders (error/success/html patterns)
|
|
6
6
|
* - Column inference from data rows
|
|
7
7
|
* - Color preference application to visualization specs
|
|
8
|
-
* - Dashboard-specific helpers (time bucket handling, auto layout)
|
|
9
|
-
* - Dashboard view execution (query + pattern selection + overrides)
|
|
10
8
|
*/
|
|
11
9
|
|
|
12
10
|
import type {
|
|
13
11
|
DataColumn,
|
|
14
|
-
DslGroupByField,
|
|
15
12
|
VisualizationSpec,
|
|
16
13
|
ColorPaletteName,
|
|
17
|
-
DashboardViewSpec,
|
|
18
14
|
} from '../../types.js';
|
|
19
|
-
import { selectPattern } from '../../patterns/selector.js';
|
|
20
|
-
import type { DashboardViewData } from '../../renderers/html/builders/dashboard.js';
|
|
21
15
|
|
|
22
16
|
// ─── MCP RESPONSE BUILDERS ─────────────────────────────────────────────────
|
|
23
17
|
|
|
@@ -27,7 +21,7 @@ export interface McpResponse {
|
|
|
27
21
|
[key: string]: unknown;
|
|
28
22
|
content: McpTextContent[];
|
|
29
23
|
isError?: boolean;
|
|
30
|
-
structuredContent?: { html: string };
|
|
24
|
+
structuredContent?: { specId?: string; html: string };
|
|
31
25
|
}
|
|
32
26
|
|
|
33
27
|
export function errorResponse(message: string): McpResponse {
|
|
@@ -44,9 +38,10 @@ export function jsonResponse(body: unknown): McpResponse {
|
|
|
44
38
|
}
|
|
45
39
|
|
|
46
40
|
export function htmlResponse(body: unknown, html: string): McpResponse {
|
|
41
|
+
const specId = (body as Record<string, unknown>)?.specId as string | undefined;
|
|
47
42
|
return {
|
|
48
43
|
content: [{ type: 'text', text: JSON.stringify(body, null, 2) }],
|
|
49
|
-
structuredContent: { html },
|
|
44
|
+
structuredContent: { ...(specId ? { specId } : {}), html },
|
|
50
45
|
};
|
|
51
46
|
}
|
|
52
47
|
|
|
@@ -64,7 +59,8 @@ export function inferColumns(data: Record<string, any>[]): DataColumn[] {
|
|
|
64
59
|
).length;
|
|
65
60
|
const isNumeric = numericCount > values.length * 0.7;
|
|
66
61
|
const isDate = /date|time|year|month|day/i.test(key) ||
|
|
67
|
-
values.every(v => /^\d{4}[-/]/.test(String(v)))
|
|
62
|
+
values.every(v => /^\d{4}[-/]/.test(String(v))) ||
|
|
63
|
+
(values.length > 0 && values.every(v => /^\d{4}-(?:Q\d|W\d{2}|\d{2}(?:-\d{2})?)$/.test(String(v))));
|
|
68
64
|
const isId = /^id$|_id$|Id$/i.test(key) && uniqueValues.length > data.length * 0.5;
|
|
69
65
|
|
|
70
66
|
const YEAR_NAME_PATTERN = /\byear\b|\bcohort\b|\bfiscal\b|\bfy\b|\bsemester\b|\bvintage\b|\bclass_of\b|\bgraduating\b|\bperiod\b|\bseason\b/i;
|
|
@@ -172,32 +168,6 @@ export function applyColorPreferences(
|
|
|
172
168
|
return { notes };
|
|
173
169
|
}
|
|
174
170
|
|
|
175
|
-
// ─── TIME BUCKET HELPERS ────────────────────────────────────────────────────
|
|
176
|
-
|
|
177
|
-
function getTimeBucketedFields(groupBy?: DslGroupByField[]): string[] {
|
|
178
|
-
if (!groupBy) return [];
|
|
179
|
-
return groupBy
|
|
180
|
-
.filter((g): g is Exclude<DslGroupByField, string> => typeof g === 'object' && 'bucket' in g)
|
|
181
|
-
.map(g => g.field);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
export function enhanceIntentForTimeBucket(intent: string, groupBy?: DslGroupByField[]): string {
|
|
185
|
-
const bucketedFields = getTimeBucketedFields(groupBy);
|
|
186
|
-
if (bucketedFields.length > 0 && !/time.series|trend|over.time/i.test(intent)) {
|
|
187
|
-
return intent + ' (time series trend)';
|
|
188
|
-
}
|
|
189
|
-
return intent;
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
export function applyTimeBucketColumnTypes(columns: DataColumn[], groupBy?: DslGroupByField[]): void {
|
|
193
|
-
const bucketedFields = getTimeBucketedFields(groupBy);
|
|
194
|
-
for (const col of columns) {
|
|
195
|
-
if (bucketedFields.some(f => col.name.includes(f) || f.includes(col.name))) {
|
|
196
|
-
col.type = 'date';
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
|
|
201
171
|
// ─── FORMATTING ─────────────────────────────────────────────────────────────
|
|
202
172
|
|
|
203
173
|
export function formatUptime(ms: number): string {
|
|
@@ -206,78 +176,3 @@ export function formatUptime(ms: number): string {
|
|
|
206
176
|
return `${Math.floor(min / 60)}h ${min % 60}m`;
|
|
207
177
|
}
|
|
208
178
|
|
|
209
|
-
// ─── DASHBOARD LAYOUT ───────────────────────────────────────────────────────
|
|
210
|
-
|
|
211
|
-
export function autoLayout(viewCount: number): { columns: number } {
|
|
212
|
-
if (viewCount <= 1) return { columns: 1 };
|
|
213
|
-
if (viewCount <= 4) return { columns: 2 };
|
|
214
|
-
return { columns: 3 };
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
// ─── DASHBOARD VIEW EXECUTION ───────────────────────────────────────────────
|
|
218
|
-
|
|
219
|
-
export interface ViewExecutionResult {
|
|
220
|
-
viewData: DashboardViewData[];
|
|
221
|
-
viewReasonings: { viewId: string; pattern: string; reasoning: string }[];
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
/**
|
|
225
|
-
* Execute queries and select patterns for each dashboard view.
|
|
226
|
-
* Shared between create_dashboard and refine_dashboard.
|
|
227
|
-
*/
|
|
228
|
-
export async function executeDashboardViews(
|
|
229
|
-
views: DashboardViewSpec[],
|
|
230
|
-
sourceId: string,
|
|
231
|
-
table: string,
|
|
232
|
-
sourceManager: any,
|
|
233
|
-
): Promise<ViewExecutionResult | McpResponse> {
|
|
234
|
-
const viewData: DashboardViewData[] = [];
|
|
235
|
-
const viewReasonings: { viewId: string; pattern: string; reasoning: string }[] = [];
|
|
236
|
-
|
|
237
|
-
for (const view of views) {
|
|
238
|
-
const result = await sourceManager.queryDsl(sourceId, table, view.query);
|
|
239
|
-
if (!result.ok) {
|
|
240
|
-
return errorResponse(`Query failed for view "${view.id}": ${result.error}`);
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
const data = result.rows!;
|
|
244
|
-
const columns = inferColumns(data);
|
|
245
|
-
applyTimeBucketColumnTypes(columns, view.query.groupBy);
|
|
246
|
-
const intent = enhanceIntentForTimeBucket(view.intent, view.query.groupBy);
|
|
247
|
-
|
|
248
|
-
const selection = selectPattern(data, columns, intent);
|
|
249
|
-
let spec: VisualizationSpec;
|
|
250
|
-
let reasoning: string;
|
|
251
|
-
|
|
252
|
-
if (view.pattern) {
|
|
253
|
-
const match = [selection.recommended, ...selection.alternatives]
|
|
254
|
-
.find(r => r.pattern.id === view.pattern);
|
|
255
|
-
if (match) {
|
|
256
|
-
spec = match.spec;
|
|
257
|
-
reasoning = `Pattern: ${view.pattern}`;
|
|
258
|
-
} else {
|
|
259
|
-
spec = selection.recommended.spec;
|
|
260
|
-
spec.pattern = view.pattern;
|
|
261
|
-
reasoning = `Pattern: ${view.pattern} (fallback)`;
|
|
262
|
-
}
|
|
263
|
-
} else {
|
|
264
|
-
spec = selection.recommended.spec;
|
|
265
|
-
reasoning = selection.recommended.reasoning;
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
spec.title = view.title;
|
|
269
|
-
if (view.config) {
|
|
270
|
-
spec.config = { ...spec.config, ...view.config };
|
|
271
|
-
}
|
|
272
|
-
applyColorPreferences(spec, view.colorPreferences, data);
|
|
273
|
-
|
|
274
|
-
viewData.push({ viewId: view.id, data, spec });
|
|
275
|
-
viewReasonings.push({ viewId: view.id, pattern: spec.pattern, reasoning });
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
return { viewData, viewReasonings };
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
export function isViewExecutionError(result: ViewExecutionResult | McpResponse): result is McpResponse {
|
|
282
|
-
return 'content' in result;
|
|
283
|
-
}
|
package/src/mcp/tools/sources.ts
CHANGED
|
@@ -20,7 +20,7 @@ export function isSandboxPath(filePath: string): boolean {
|
|
|
20
20
|
export const addSourceInputSchema = z.object({
|
|
21
21
|
name: z.string().describe('Name for this dataset'),
|
|
22
22
|
path: z.string().describe('Path to a CSV file or directory of CSV files'),
|
|
23
|
-
detail: z.enum(['compact', 'full']).default('
|
|
23
|
+
detail: z.enum(['compact', 'full']).default('compact').describe('Schema detail level: "compact" (default) returns just column names/types + row counts; "full" includes stats, top values, and sample rows'),
|
|
24
24
|
});
|
|
25
25
|
|
|
26
26
|
export const removeSourceInputSchema = z.object({
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared Zod schemas for the SQL query layer.
|
|
3
|
+
* Used by query_data and the visualize tool.
|
|
4
|
+
*/
|
|
5
|
+
import { z } from 'zod';
|
|
6
|
+
|
|
7
|
+
// ─── ROW FILTER SCHEMA (for transform pipeline) ────────────────────────────
|
|
8
|
+
|
|
9
|
+
export const rowFilterSchema = z.object({
|
|
10
|
+
field: z.string().describe('Field to filter on'),
|
|
11
|
+
op: z.enum(['=', '!=', '>', '>=', '<', '<=', 'in', 'not_in', 'between', 'is_null', 'is_not_null']).describe('Comparison operator'),
|
|
12
|
+
value: z.any().optional().describe('Filter value. For "in"/"not_in": use an array. For "between": use [min, max]. For "is_null"/"is_not_null": omit.'),
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
// ─── DASHBOARD SCHEMAS ──────────────────────────────────────────────────────
|
|
16
|
+
|
|
17
|
+
export const ALL_PALETTE_NAMES = [
|
|
18
|
+
'categorical', 'blue', 'green', 'purple', 'warm',
|
|
19
|
+
'blueRed', 'greenPurple', 'tealOrange', 'redGreen',
|
|
20
|
+
'traffic-light', 'profit-loss', 'temperature',
|
|
21
|
+
] as const;
|
|
22
|
+
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP Tool: transform_data
|
|
3
|
+
*
|
|
4
|
+
* Creates derived columns using the expression language.
|
|
5
|
+
* Supports single-column and batch modes.
|
|
6
|
+
*/
|
|
7
|
+
import type { z } from 'zod';
|
|
8
|
+
import { errorResponse, jsonResponse } from './shared.js';
|
|
9
|
+
import type { transformDataSchema } from './transform-schemas.js';
|
|
10
|
+
import { TransformMetadata } from '../../transforms/metadata.js';
|
|
11
|
+
import { executeSingleTransform, executeBatchTransform } from '../../transforms/pipeline.js';
|
|
12
|
+
import { ColumnManager } from '../../transforms/column-manager.js';
|
|
13
|
+
|
|
14
|
+
export function handleTransformData(deps: { sourceManager: any }) {
|
|
15
|
+
return async (args: z.infer<typeof transformDataSchema>) => {
|
|
16
|
+
const connResult = await deps.sourceManager.connect(args.sourceId);
|
|
17
|
+
if (!connResult.ok) {
|
|
18
|
+
return errorResponse(`Source not found: ${args.sourceId}`);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const source = connResult.source!;
|
|
22
|
+
const db = source.getDatabase?.();
|
|
23
|
+
if (!db) {
|
|
24
|
+
return errorResponse('Source does not support transforms (no database handle)');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const schema = await source.getSchema();
|
|
28
|
+
const table = schema.tables.find((t: any) => t.name === args.table);
|
|
29
|
+
if (!table) {
|
|
30
|
+
const available = schema.tables.map((t: any) => t.name);
|
|
31
|
+
return errorResponse(`Table '${args.table}' not found. Available: [${available.join(', ')}]`);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const metadata = new TransformMetadata(db);
|
|
35
|
+
metadata.init();
|
|
36
|
+
|
|
37
|
+
// Source columns = schema columns minus any transform-tracked columns.
|
|
38
|
+
// After schema invalidation, derived columns appear in the schema for DSL
|
|
39
|
+
// validation, but they're not source columns for collision detection.
|
|
40
|
+
const transformedNames = new Set(metadata.list(args.table).map(r => r.column));
|
|
41
|
+
const sourceColumns = table.columns.map((c: any) => c.name).filter((n: string) => !transformedNames.has(n));
|
|
42
|
+
|
|
43
|
+
try {
|
|
44
|
+
let result;
|
|
45
|
+
if (args.transforms) {
|
|
46
|
+
result = executeBatchTransform(db, metadata, {
|
|
47
|
+
sourceId: args.sourceId,
|
|
48
|
+
table: args.table,
|
|
49
|
+
transforms: args.transforms,
|
|
50
|
+
type: args.type,
|
|
51
|
+
filter: args.filter,
|
|
52
|
+
partitionBy: args.partitionBy,
|
|
53
|
+
}, sourceColumns);
|
|
54
|
+
} else {
|
|
55
|
+
result = executeSingleTransform(db, metadata, {
|
|
56
|
+
sourceId: args.sourceId,
|
|
57
|
+
table: args.table,
|
|
58
|
+
create: args.create!,
|
|
59
|
+
expr: args.expr!,
|
|
60
|
+
type: args.type,
|
|
61
|
+
filter: args.filter,
|
|
62
|
+
partitionBy: args.partitionBy,
|
|
63
|
+
}, sourceColumns);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Invalidate cached schema so DSL queries can see the new columns
|
|
67
|
+
source.invalidateSchema?.();
|
|
68
|
+
|
|
69
|
+
return jsonResponse({
|
|
70
|
+
created: result.created.map(c => ({
|
|
71
|
+
column: c.column,
|
|
72
|
+
expr: c.expr,
|
|
73
|
+
type: c.type,
|
|
74
|
+
layer: c.layer,
|
|
75
|
+
overwritten: c.overwritten,
|
|
76
|
+
stats: c.stats,
|
|
77
|
+
})),
|
|
78
|
+
warnings: result.warnings,
|
|
79
|
+
working_column_count: result.working_column_count,
|
|
80
|
+
derived_column_count: result.derived_column_count,
|
|
81
|
+
total_columns: result.total_columns,
|
|
82
|
+
});
|
|
83
|
+
} catch (err: any) {
|
|
84
|
+
return errorResponse(err.message);
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zod input schemas for the 4 derived data layer MCP tools.
|
|
3
|
+
*/
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
import { rowFilterSchema } from './sql-schemas.js';
|
|
6
|
+
|
|
7
|
+
export const transformDataBaseSchema = z.object({
|
|
8
|
+
sourceId: z.string().describe('Dataset ID returned by load_csv'),
|
|
9
|
+
table: z.string().describe('Table name within the source'),
|
|
10
|
+
create: z.string().optional().describe('Column name to create (single-column mode)'),
|
|
11
|
+
expr: z.string().optional().describe('Expression to evaluate (single-column mode). Examples: "score * 2", "zscore(score)", "if_else(age > 18, \"adult\", \"minor\")".'),
|
|
12
|
+
transforms: z.array(z.object({
|
|
13
|
+
create: z.string().describe('Column name to create'),
|
|
14
|
+
expr: z.string().describe('Expression to evaluate'),
|
|
15
|
+
})).optional().describe('Batch mode: array of { create, expr } pairs. Executed sequentially — later expressions can reference earlier ones. All-or-nothing on failure.'),
|
|
16
|
+
type: z.enum(['numeric', 'categorical', 'date', 'boolean']).optional().describe('Force output type. Auto-inferred if omitted.'),
|
|
17
|
+
filter: z.array(rowFilterSchema).optional().describe('Only evaluate for matching rows. Non-matching rows get null.'),
|
|
18
|
+
partitionBy: z.string().optional().describe('Compute column-wise stats (zscore, rank, etc.) within groups of this column.'),
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
export const transformDataSchema = transformDataBaseSchema.refine(
|
|
22
|
+
(d) => (d.create && d.expr) || d.transforms,
|
|
23
|
+
{ message: 'Provide either (create + expr) for single-column mode, or transforms array for batch mode' }
|
|
24
|
+
).refine(
|
|
25
|
+
(d) => !((d.create || d.expr) && d.transforms),
|
|
26
|
+
{ message: 'Cannot use both single-column and batch mode' }
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
export const promoteColumnsSchema = z.object({
|
|
30
|
+
sourceId: z.string().describe('Dataset ID returned by load_csv'),
|
|
31
|
+
table: z.string().describe('Table name within the source'),
|
|
32
|
+
columns: z.array(z.string()).describe('Column names to promote from working → derived. Use ["*"] to promote all working columns.'),
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
export const listTransformsSchema = z.object({
|
|
36
|
+
sourceId: z.string().describe('Dataset ID returned by load_csv'),
|
|
37
|
+
table: z.string().describe('Table name within the source'),
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
export const dropColumnsSchema = z.object({
|
|
41
|
+
sourceId: z.string().describe('Dataset ID returned by load_csv'),
|
|
42
|
+
table: z.string().describe('Table name within the source'),
|
|
43
|
+
columns: z.array(z.string()).describe('Column names to drop. Use ["*"] to drop all columns in the specified layer.'),
|
|
44
|
+
layer: z.enum(['derived', 'working']).optional().describe('Which layer to drop from. If omitted, auto-detects.'),
|
|
45
|
+
});
|
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* MCP Tool: visualize
|
|
3
|
-
* Takes inline
|
|
4
|
-
* from the handcrafted pattern library.
|
|
5
|
-
*
|
|
6
|
-
* For source-based data (sourceId + table + query), use visualize_data.
|
|
3
|
+
* Takes data (inline, cached, or from a loaded CSV via SQL) + intent
|
|
4
|
+
* and returns visualization recommendations from the handcrafted pattern library.
|
|
7
5
|
*
|
|
8
6
|
* Returns compact text content (specId + metadata, no data) while
|
|
9
7
|
* structuredContent still gets the full pre-rendered chart HTML.
|
|
@@ -12,7 +10,7 @@
|
|
|
12
10
|
import { z } from 'zod';
|
|
13
11
|
import type { VisualizeInput, VisualizeOutput, DataColumn, VisualizationSpec } from '../../types.js';
|
|
14
12
|
import { isCompoundSpec } from '../../types.js';
|
|
15
|
-
import { ALL_PALETTE_NAMES } from './
|
|
13
|
+
import { ALL_PALETTE_NAMES } from './sql-schemas.js';
|
|
16
14
|
import { buildChartHtml, isHtmlPatternSupported } from '../../renderers/html/index.js';
|
|
17
15
|
import { shouldCompound, buildCompoundSpec } from '../../renderers/html/compound.js';
|
|
18
16
|
import { buildCompoundHtml } from '../../renderers/html/builders/compound.js';
|
|
@@ -44,8 +42,10 @@ export const dataShapeHintsSchema = z.object({
|
|
|
44
42
|
}).optional().describe('Hints about the data shape to help pattern selection');
|
|
45
43
|
|
|
46
44
|
export const visualizeInputSchema = z.object({
|
|
47
|
-
data: z.array(z.record(z.any())).optional().describe('Array of data rows to visualize. Optional if resultId is provided.'),
|
|
45
|
+
data: z.array(z.record(z.any())).optional().describe('Array of data rows to visualize. Optional if resultId or sourceId+sql is provided.'),
|
|
48
46
|
resultId: z.string().optional().describe('Result ID from a previous query_data call — reuses cached data without re-sending it'),
|
|
47
|
+
sourceId: z.string().optional().describe('Dataset ID returned by load_csv — use with sql to query server-side (saves tokens)'),
|
|
48
|
+
sql: z.string().optional().describe('SQL SELECT query to slice/aggregate the data before visualizing. Use table and column names from load_csv/describe_data.'),
|
|
49
49
|
intent: z.string().describe('What the user wants to see — e.g., "compare sales by region", "show distribution of ages", "how do rankings change over time"'),
|
|
50
50
|
columns: columnsSchema,
|
|
51
51
|
dataShapeHints: dataShapeHintsSchema,
|
|
@@ -67,10 +67,12 @@ export const visualizeInputSchema = z.object({
|
|
|
67
67
|
.describe('Geographic level: "country" (each row = a nation) or "subdivision" (each row = a state/province). Auto-detected if omitted.'),
|
|
68
68
|
geoRegion: z.string().optional()
|
|
69
69
|
.describe('Geographic region code: "world", ISO country code (US, CN, AU, etc.), or continent (EU, AF, AS, SA, NA, OC). Auto-detected if omitted.'),
|
|
70
|
+
returnHtml: z.boolean().optional()
|
|
71
|
+
.describe('Whether to return pre-rendered HTML in the response. Default: true. Set to false to save tokens — use screenshot(specId) to render later.'),
|
|
70
72
|
});
|
|
71
73
|
|
|
72
74
|
/**
|
|
73
|
-
* Shared core logic for
|
|
75
|
+
* Shared core logic for all visualize data paths (inline, cached, source query).
|
|
74
76
|
* Takes resolved data + args and returns the MCP response.
|
|
75
77
|
*/
|
|
76
78
|
export function handleVisualizeCore(
|
|
@@ -91,6 +93,7 @@ export function handleVisualizeCore(
|
|
|
91
93
|
maxAlternativeChartTypes?: number;
|
|
92
94
|
geoLevel?: 'country' | 'subdivision';
|
|
93
95
|
geoRegion?: string;
|
|
96
|
+
returnHtml?: boolean;
|
|
94
97
|
}, queryMeta?: { truncated?: boolean; totalSourceRows?: number }, extraMeta?: Partial<OperationMeta>) => {
|
|
95
98
|
const start = Date.now();
|
|
96
99
|
const notes: string[] = [];
|
|
@@ -190,13 +193,15 @@ export function handleVisualizeCore(
|
|
|
190
193
|
},
|
|
191
194
|
});
|
|
192
195
|
|
|
196
|
+
const shouldReturnHtml = args.returnHtml !== false;
|
|
193
197
|
return {
|
|
194
198
|
content: [{
|
|
195
199
|
type: 'text' as const,
|
|
196
200
|
text: JSON.stringify(compactResponse, null, 2),
|
|
197
201
|
}],
|
|
198
|
-
...(outputHtml ? {
|
|
202
|
+
...(outputHtml && shouldReturnHtml ? {
|
|
199
203
|
structuredContent: {
|
|
204
|
+
specId,
|
|
200
205
|
html: outputHtml,
|
|
201
206
|
},
|
|
202
207
|
} : {}),
|
|
@@ -206,13 +211,47 @@ export function handleVisualizeCore(
|
|
|
206
211
|
|
|
207
212
|
export function handleVisualize(
|
|
208
213
|
selectPatterns: (input: VisualizeInput) => VisualizeOutput,
|
|
214
|
+
deps?: { sourceManager?: any },
|
|
209
215
|
) {
|
|
210
216
|
const core = handleVisualizeCore(selectPatterns);
|
|
211
217
|
|
|
212
218
|
return async (args: z.infer<typeof visualizeInputSchema>) => {
|
|
213
219
|
let data = args.data;
|
|
220
|
+
let queryMeta: { truncated?: boolean; totalSourceRows?: number } | undefined;
|
|
221
|
+
let extraMeta: Partial<OperationMeta> | undefined;
|
|
222
|
+
|
|
223
|
+
// Path 1: sourceId + sql → server-side query
|
|
224
|
+
if (args.sourceId && args.sql) {
|
|
225
|
+
if (!deps?.sourceManager) {
|
|
226
|
+
return errorResponse('Source manager not available.');
|
|
227
|
+
}
|
|
228
|
+
const start = Date.now();
|
|
229
|
+
const source = deps.sourceManager.get?.(args.sourceId);
|
|
230
|
+
const sourceType = source?.type;
|
|
231
|
+
|
|
232
|
+
const result = await deps.sourceManager.querySql(args.sourceId, args.sql);
|
|
233
|
+
if (!result.ok) {
|
|
234
|
+
logOperation({
|
|
235
|
+
toolName: 'visualize',
|
|
236
|
+
timestamp: start,
|
|
237
|
+
durationMs: Date.now() - start,
|
|
238
|
+
success: false,
|
|
239
|
+
meta: {
|
|
240
|
+
sqlPreview: args.sql.slice(0, 200),
|
|
241
|
+
sourceType,
|
|
242
|
+
error: result.error,
|
|
243
|
+
},
|
|
244
|
+
});
|
|
245
|
+
return errorResponse(result.error);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
data = result.rows;
|
|
249
|
+
queryMeta = { truncated: result.truncated, totalSourceRows: result.totalRows };
|
|
250
|
+
extraMeta = { sqlPreview: args.sql.slice(0, 200), sourceType };
|
|
251
|
+
}
|
|
214
252
|
|
|
215
|
-
|
|
253
|
+
// Path 2: resultId → cached query result
|
|
254
|
+
if (!data && args.resultId) {
|
|
216
255
|
const cached = getResult(args.resultId);
|
|
217
256
|
if (!cached) {
|
|
218
257
|
return errorResponse(`Result "${args.resultId}" not found or expired. Re-run query_data to get a new resultId.`);
|
|
@@ -220,10 +259,12 @@ export function handleVisualize(
|
|
|
220
259
|
data = cached.rows;
|
|
221
260
|
}
|
|
222
261
|
|
|
262
|
+
// Path 3: inline data (already in args.data)
|
|
263
|
+
|
|
223
264
|
if (!data || data.length === 0) {
|
|
224
|
-
return errorResponse('No data provided. Pass
|
|
265
|
+
return errorResponse('No data provided. Pass data array, resultId from query_data, or sourceId + sql.');
|
|
225
266
|
}
|
|
226
267
|
|
|
227
|
-
return core(data, args);
|
|
268
|
+
return core(data, args, queryMeta, extraMeta);
|
|
228
269
|
};
|
|
229
270
|
}
|
|
@@ -251,7 +251,5 @@ export { buildFunnelHtml } from './builders/funnel.js';
|
|
|
251
251
|
export { buildGroupedBarHtml } from './builders/grouped-bar.js';
|
|
252
252
|
export { buildWaterfallHtml } from './builders/waterfall.js';
|
|
253
253
|
export { buildCompoundHtml } from './builders/compound.js';
|
|
254
|
-
export { buildDashboardHtml } from './builders/dashboard.js';
|
|
255
|
-
export type { DashboardViewData } from './builders/dashboard.js';
|
|
256
254
|
export { shouldCompound, buildCompoundSpec } from './compound.js';
|
|
257
255
|
export { getPreferredHeight } from './sizing.js';
|