@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.
Files changed (179) hide show
  1. package/README.md +6 -7
  2. package/dist/src/analysis/rules.d.ts.map +1 -1
  3. package/dist/src/analysis/rules.js +29 -36
  4. package/dist/src/analysis/rules.js.map +1 -1
  5. package/dist/src/analysis/types.d.ts +1 -2
  6. package/dist/src/analysis/types.d.ts.map +1 -1
  7. package/dist/src/connectors/csv.d.ts.map +1 -1
  8. package/dist/src/connectors/csv.js +131 -0
  9. package/dist/src/connectors/csv.js.map +1 -1
  10. package/dist/src/connectors/manager.d.ts +16 -12
  11. package/dist/src/connectors/manager.d.ts.map +1 -1
  12. package/dist/src/connectors/manager.js +49 -43
  13. package/dist/src/connectors/manager.js.map +1 -1
  14. package/dist/src/connectors/types.d.ts +4 -0
  15. package/dist/src/connectors/types.d.ts.map +1 -1
  16. package/dist/src/index.d.ts +2 -4
  17. package/dist/src/index.d.ts.map +1 -1
  18. package/dist/src/index.js +1 -2
  19. package/dist/src/index.js.map +1 -1
  20. package/dist/src/mcp/app-shell.js +1 -2
  21. package/dist/src/mcp/app-shell.js.map +1 -1
  22. package/dist/src/mcp/index.d.ts +8 -7
  23. package/dist/src/mcp/index.d.ts.map +1 -1
  24. package/dist/src/mcp/index.js +51 -73
  25. package/dist/src/mcp/index.js.map +1 -1
  26. package/dist/src/mcp/prompts.js +1 -1
  27. package/dist/src/mcp/tools/drop-columns.d.ts +12 -0
  28. package/dist/src/mcp/tools/drop-columns.d.ts.map +1 -0
  29. package/dist/src/mcp/tools/drop-columns.js +122 -0
  30. package/dist/src/mcp/tools/drop-columns.js.map +1 -0
  31. package/dist/src/mcp/tools/export-html.d.ts +6 -1
  32. package/dist/src/mcp/tools/export-html.d.ts.map +1 -1
  33. package/dist/src/mcp/tools/export-html.js +27 -8
  34. package/dist/src/mcp/tools/export-html.js.map +1 -1
  35. package/dist/src/mcp/tools/list-transforms.d.ts +11 -0
  36. package/dist/src/mcp/tools/list-transforms.d.ts.map +1 -0
  37. package/dist/src/mcp/tools/list-transforms.js +49 -0
  38. package/dist/src/mcp/tools/list-transforms.js.map +1 -0
  39. package/dist/src/mcp/tools/operation-log.d.ts +2 -13
  40. package/dist/src/mcp/tools/operation-log.d.ts.map +1 -1
  41. package/dist/src/mcp/tools/operation-log.js +1 -35
  42. package/dist/src/mcp/tools/operation-log.js.map +1 -1
  43. package/dist/src/mcp/tools/promote-columns.d.ts +11 -0
  44. package/dist/src/mcp/tools/promote-columns.d.ts.map +1 -0
  45. package/dist/src/mcp/tools/promote-columns.js +73 -0
  46. package/dist/src/mcp/tools/promote-columns.js.map +1 -0
  47. package/dist/src/mcp/tools/query-source.d.ts +4 -318
  48. package/dist/src/mcp/tools/query-source.d.ts.map +1 -1
  49. package/dist/src/mcp/tools/query-source.js +7 -13
  50. package/dist/src/mcp/tools/query-source.js.map +1 -1
  51. package/dist/src/mcp/tools/refine.d.ts +4 -4
  52. package/dist/src/mcp/tools/refine.js +2 -2
  53. package/dist/src/mcp/tools/refine.js.map +1 -1
  54. package/dist/src/mcp/tools/screenshot.js +1 -1
  55. package/dist/src/mcp/tools/screenshot.js.map +1 -1
  56. package/dist/src/mcp/tools/shared.d.ts +2 -23
  57. package/dist/src/mcp/tools/shared.d.ts.map +1 -1
  58. package/dist/src/mcp/tools/shared.js +4 -85
  59. package/dist/src/mcp/tools/shared.js.map +1 -1
  60. package/dist/src/mcp/tools/sources.js +1 -1
  61. package/dist/src/mcp/tools/sources.js.map +1 -1
  62. package/dist/src/mcp/tools/sql-schemas.d.ts +20 -0
  63. package/dist/src/mcp/tools/sql-schemas.d.ts.map +1 -0
  64. package/dist/src/mcp/tools/sql-schemas.js +18 -0
  65. package/dist/src/mcp/tools/sql-schemas.js.map +1 -0
  66. package/dist/src/mcp/tools/transform-data.d.ts +12 -0
  67. package/dist/src/mcp/tools/transform-data.d.ts.map +1 -0
  68. package/dist/src/mcp/tools/transform-data.js +73 -0
  69. package/dist/src/mcp/tools/transform-data.js.map +1 -0
  70. package/dist/src/mcp/tools/transform-schemas.d.ts +234 -0
  71. package/dist/src/mcp/tools/transform-schemas.d.ts.map +1 -0
  72. package/dist/src/mcp/tools/transform-schemas.js +35 -0
  73. package/dist/src/mcp/tools/transform-schemas.js.map +1 -0
  74. package/dist/src/mcp/tools/visualize.d.ts +18 -6
  75. package/dist/src/mcp/tools/visualize.d.ts.map +1 -1
  76. package/dist/src/mcp/tools/visualize.js +47 -12
  77. package/dist/src/mcp/tools/visualize.js.map +1 -1
  78. package/dist/src/renderers/html/index.d.ts +0 -2
  79. package/dist/src/renderers/html/index.d.ts.map +1 -1
  80. package/dist/src/renderers/html/index.js +0 -1
  81. package/dist/src/renderers/html/index.js.map +1 -1
  82. package/dist/src/transforms/column-functions.d.ts +19 -0
  83. package/dist/src/transforms/column-functions.d.ts.map +1 -0
  84. package/dist/src/transforms/column-functions.js +269 -0
  85. package/dist/src/transforms/column-functions.js.map +1 -0
  86. package/dist/src/transforms/column-manager.d.ts +35 -0
  87. package/dist/src/transforms/column-manager.d.ts.map +1 -0
  88. package/dist/src/transforms/column-manager.js +151 -0
  89. package/dist/src/transforms/column-manager.js.map +1 -0
  90. package/dist/src/transforms/dependency.d.ts +21 -0
  91. package/dist/src/transforms/dependency.d.ts.map +1 -0
  92. package/dist/src/transforms/dependency.js +129 -0
  93. package/dist/src/transforms/dependency.js.map +1 -0
  94. package/dist/src/transforms/evaluator.d.ts +38 -0
  95. package/dist/src/transforms/evaluator.d.ts.map +1 -0
  96. package/dist/src/transforms/evaluator.js +377 -0
  97. package/dist/src/transforms/evaluator.js.map +1 -0
  98. package/dist/src/transforms/index.d.ts +19 -0
  99. package/dist/src/transforms/index.d.ts.map +1 -0
  100. package/dist/src/transforms/index.js +21 -0
  101. package/dist/src/transforms/index.js.map +1 -0
  102. package/dist/src/transforms/manifest.d.ts +19 -0
  103. package/dist/src/transforms/manifest.d.ts.map +1 -0
  104. package/dist/src/transforms/manifest.js +136 -0
  105. package/dist/src/transforms/manifest.js.map +1 -0
  106. package/dist/src/transforms/metadata.d.ts +31 -0
  107. package/dist/src/transforms/metadata.d.ts.map +1 -0
  108. package/dist/src/transforms/metadata.js +116 -0
  109. package/dist/src/transforms/metadata.js.map +1 -0
  110. package/dist/src/transforms/parser.d.ts +22 -0
  111. package/dist/src/transforms/parser.d.ts.map +1 -0
  112. package/dist/src/transforms/parser.js +165 -0
  113. package/dist/src/transforms/parser.js.map +1 -0
  114. package/dist/src/transforms/pipeline.d.ts +40 -0
  115. package/dist/src/transforms/pipeline.d.ts.map +1 -0
  116. package/dist/src/transforms/pipeline.js +208 -0
  117. package/dist/src/transforms/pipeline.js.map +1 -0
  118. package/dist/src/transforms/row-functions.d.ts +11 -0
  119. package/dist/src/transforms/row-functions.d.ts.map +1 -0
  120. package/dist/src/transforms/row-functions.js +241 -0
  121. package/dist/src/transforms/row-functions.js.map +1 -0
  122. package/dist/src/transforms/tokenizer.d.ts +12 -0
  123. package/dist/src/transforms/tokenizer.d.ts.map +1 -0
  124. package/dist/src/transforms/tokenizer.js +135 -0
  125. package/dist/src/transforms/tokenizer.js.map +1 -0
  126. package/dist/src/transforms/types.d.ts +139 -0
  127. package/dist/src/transforms/types.d.ts.map +1 -0
  128. package/dist/src/transforms/types.js +19 -0
  129. package/dist/src/transforms/types.js.map +1 -0
  130. package/dist/src/types.d.ts +7 -164
  131. package/dist/src/types.d.ts.map +1 -1
  132. package/dist/src/types.js +0 -12
  133. package/dist/src/types.js.map +1 -1
  134. package/package.json +2 -2
  135. package/src/analysis/rules.ts +30 -37
  136. package/src/analysis/types.ts +1 -3
  137. package/src/connectors/csv.ts +147 -0
  138. package/src/connectors/manager.ts +69 -55
  139. package/src/connectors/types.ts +4 -0
  140. package/src/index.ts +3 -20
  141. package/src/mcp/app-shell.ts +1 -2
  142. package/src/mcp/index.ts +69 -83
  143. package/src/mcp/prompts.ts +1 -1
  144. package/src/mcp/tools/drop-columns.ts +144 -0
  145. package/src/mcp/tools/export-html.ts +25 -9
  146. package/src/mcp/tools/list-transforms.ts +63 -0
  147. package/src/mcp/tools/operation-log.ts +2 -50
  148. package/src/mcp/tools/promote-columns.ts +89 -0
  149. package/src/mcp/tools/query-source.ts +10 -17
  150. package/src/mcp/tools/refine.ts +2 -2
  151. package/src/mcp/tools/screenshot.ts +1 -1
  152. package/src/mcp/tools/shared.ts +5 -110
  153. package/src/mcp/tools/sources.ts +1 -1
  154. package/src/mcp/tools/sql-schemas.ts +22 -0
  155. package/src/mcp/tools/transform-data.ts +87 -0
  156. package/src/mcp/tools/transform-schemas.ts +45 -0
  157. package/src/mcp/tools/visualize.ts +52 -11
  158. package/src/renderers/html/index.ts +0 -2
  159. package/src/transforms/column-functions.ts +310 -0
  160. package/src/transforms/column-manager.ts +194 -0
  161. package/src/transforms/dependency.ts +141 -0
  162. package/src/transforms/evaluator.ts +418 -0
  163. package/src/transforms/index.ts +71 -0
  164. package/src/transforms/manifest.ts +162 -0
  165. package/src/transforms/metadata.ts +147 -0
  166. package/src/transforms/parser.ts +205 -0
  167. package/src/transforms/pipeline.ts +301 -0
  168. package/src/transforms/row-functions.ts +228 -0
  169. package/src/transforms/tokenizer.ts +154 -0
  170. package/src/transforms/types.ts +142 -0
  171. package/src/types.ts +8 -212
  172. package/src/connectors/dsl-compiler.ts +0 -361
  173. package/src/connectors/dsl-validator.ts +0 -495
  174. package/src/connectors/js-aggregation.ts +0 -404
  175. package/src/mcp/tools/dashboard-refine.ts +0 -456
  176. package/src/mcp/tools/dashboard.ts +0 -62
  177. package/src/mcp/tools/dsl-schemas.ts +0 -262
  178. package/src/mcp/tools/visualize-from-source.ts +0 -92
  179. package/src/renderers/html/builders/dashboard.ts +0 -449
@@ -1,456 +0,0 @@
1
- /**
2
- * MCP Tool: refine_dashboard
3
- *
4
- * Takes a current dashboard spec and a natural language refinement,
5
- * applies changes, re-executes affected queries, and returns the updated dashboard.
6
- *
7
- * Refinement categories:
8
- * - Add view: "add a chart showing X"
9
- * - Remove view: "remove the trend chart" / "remove view-2"
10
- * - Modify view: "change the bar chart to show top 5"
11
- * - Layout: "make it 3 columns" / "make view-1 wider"
12
- * - Filters: "add a region filter"
13
- * - Theme: "dark mode" / "light mode"
14
- * - Swap: "swap view-1 and view-2"
15
- * - Pattern override: "show the trend as an area chart"
16
- */
17
-
18
- import { z } from 'zod';
19
- import type {
20
- DashboardSpec,
21
- DashboardViewSpec,
22
- DashboardFilter,
23
- } from '../../types.js';
24
- import { buildDashboardHtml } from '../../renderers/html/builders/dashboard.js';
25
- import { refineDashboardInputSchema } from './dsl-schemas.js';
26
- import {
27
- errorResponse,
28
- htmlResponse,
29
- executeDashboardViews,
30
- isViewExecutionError,
31
- } from './shared.js';
32
-
33
- export { refineDashboardInputSchema };
34
-
35
- // ─── VIEW MATCHING ──────────────────────────────────────────────────────────
36
-
37
- /** Find a view by ID, title, or pattern match */
38
- function findView(
39
- views: DashboardViewSpec[],
40
- query: string,
41
- ): DashboardViewSpec | undefined {
42
- const lower = query.toLowerCase();
43
-
44
- // Exact ID match
45
- const byId = views.find(v => v.id.toLowerCase() === lower);
46
- if (byId) return byId;
47
-
48
- // Title match (fuzzy)
49
- const byTitle = views.find(v => v.title.toLowerCase().includes(lower));
50
- if (byTitle) return byTitle;
51
-
52
- // Pattern match (e.g., "the bar chart")
53
- const patternNames = [
54
- 'bar', 'line', 'scatter', 'histogram', 'treemap', 'sankey', 'waffle',
55
- 'stacked-bar', 'diverging-bar', 'slope-chart', 'bump-chart', 'beeswarm',
56
- 'violin', 'ridgeline', 'strip-plot', 'sunburst', 'small-multiples',
57
- 'sparkline-grid', 'calendar-heatmap', 'connected-scatter', 'connected-dot-plot',
58
- 'parallel-coordinates', 'radar', 'alluvial', 'chord', 'choropleth',
59
- 'proportional-symbol', 'circle-pack', 'metric',
60
- ];
61
- for (const pname of patternNames) {
62
- if (lower.includes(pname)) {
63
- const byPattern = views.find(v => v.pattern === pname);
64
- if (byPattern) return byPattern;
65
- }
66
- }
67
-
68
- // Intent match
69
- const byIntent = views.find(v => v.intent.toLowerCase().includes(lower));
70
- if (byIntent) return byIntent;
71
-
72
- return undefined;
73
- }
74
-
75
- // ─── REFINEMENT PARSING ─────────────────────────────────────────────────────
76
-
77
- interface RefinementAction {
78
- type: 'add_view' | 'remove_view' | 'modify_view' | 'layout' | 'filter' | 'theme' | 'swap' | 'title' | 'unknown';
79
- details: Record<string, any>;
80
- }
81
-
82
- /**
83
- * Split a compound refinement into independent clauses.
84
- * Splits on commas and " and " between independent clauses,
85
- * but avoids splitting inside patterns like "swap X and Y".
86
- */
87
- function splitClauses(refinement: string): string[] {
88
- // First split on commas
89
- const commaParts = refinement.split(/\s*,\s*/);
90
- const clauses: string[] = [];
91
- for (const part of commaParts) {
92
- // Split on " and " but not when part of "swap X and Y" or "X and Y filter"
93
- if (/\bswap\b/i.test(part) || /\bfilter\b.*\band\b/i.test(part)) {
94
- clauses.push(part.trim());
95
- } else {
96
- // Split on " and " only when followed by a verb/action keyword
97
- const andParts = part.split(/\s+and\s+(?=(?:make|show|change|switch|use|set|add|remove|delete))/i);
98
- for (const ap of andParts) {
99
- const trimmed = ap.trim();
100
- if (trimmed) clauses.push(trimmed);
101
- }
102
- }
103
- }
104
- return clauses.length > 0 ? clauses : [refinement];
105
- }
106
-
107
- /** Known pattern names for matching */
108
- const KNOWN_PATTERNS = [
109
- 'bar', 'line', 'scatter', 'histogram', 'treemap', 'sankey', 'waffle',
110
- 'stacked-bar', 'diverging-bar', 'slope-chart', 'bump-chart', 'beeswarm',
111
- 'violin', 'ridgeline', 'strip-plot', 'sunburst', 'small-multiples',
112
- 'sparkline-grid', 'calendar-heatmap', 'connected-scatter', 'connected-dot-plot',
113
- 'parallel-coordinates', 'radar', 'alluvial', 'chord', 'choropleth',
114
- 'proportional-symbol', 'circle-pack', 'metric', 'area',
115
- ];
116
-
117
- /** Known palette keywords */
118
- const PALETTE_KEYWORDS: Record<string, string> = {
119
- warm: 'warm',
120
- cool: 'blue',
121
- blue: 'blue',
122
- green: 'green',
123
- purple: 'purple',
124
- 'teal-orange': 'tealOrange',
125
- 'blue-red': 'blueRed',
126
- 'green-purple': 'greenPurple',
127
- 'red-green': 'redGreen',
128
- };
129
-
130
- /** Parse a single clause into a RefinementAction (or null if not recognized) */
131
- function parseClause(clause: string, views: DashboardViewSpec[]): RefinementAction | null {
132
- const lower = clause.toLowerCase();
133
-
134
- // Theme
135
- if (lower.includes('dark mode') || lower.includes('dark theme')) {
136
- return { type: 'theme', details: { theme: 'dark' } };
137
- }
138
- if (lower.includes('light mode') || lower.includes('light theme')) {
139
- return { type: 'theme', details: { theme: 'light' } };
140
- }
141
-
142
- // Title
143
- const titleMatch = clause.match(/title[:\s]+"?([^"]+)"?/i);
144
- if (titleMatch) {
145
- return { type: 'title', details: { title: titleMatch[1].trim() } };
146
- }
147
-
148
- // Span/sizing: "full width" or "span N columns" or "spanning the full width"
149
- const fullWidthMatch = lower.match(/(?:(?:make|set)\s+)?(?:the\s+)?(.+?)\s+(?:span(?:ning)?|take)\s+(?:the\s+)?full\s+width/);
150
- const spanColMatch = lower.match(/(?:(?:make|set)\s+)?(?:the\s+)?(.+?)\s+span(?:ning)?\s+(\d+)\s+column/);
151
- if (fullWidthMatch) {
152
- const view = findView(views, fullWidthMatch[1].trim());
153
- if (view) {
154
- return { type: 'layout', details: { viewId: view.id, colSpan: 'full' } };
155
- }
156
- }
157
- if (spanColMatch) {
158
- const view = findView(views, spanColMatch[1].trim());
159
- if (view) {
160
- return { type: 'layout', details: { viewId: view.id, colSpan: parseInt(spanColMatch[2], 10) } };
161
- }
162
- }
163
-
164
- // Also match "X spanning the full width" where X comes before "spanning"
165
- const spanFullAlt = lower.match(/(.+?)\s+spanning\s+(?:the\s+)?full\s+width/);
166
- if (spanFullAlt) {
167
- const view = findView(views, spanFullAlt[1].trim());
168
- if (view) {
169
- return { type: 'layout', details: { viewId: view.id, colSpan: 'full' } };
170
- }
171
- }
172
-
173
- // Layout: column count
174
- const colMatch = lower.match(/(\d+)\s*column/);
175
- if (colMatch) {
176
- return { type: 'layout', details: { columns: parseInt(colMatch[1], 10) } };
177
- }
178
-
179
- // Layout: make view wider
180
- const widerMatch = lower.match(/(?:make|set)\s+(.+?)\s+wider/);
181
- if (widerMatch) {
182
- const view = findView(views, widerMatch[1].trim());
183
- if (view) {
184
- return { type: 'layout', details: { viewId: view.id, colSpan: 2 } };
185
- }
186
- }
187
-
188
- // Swap views
189
- const swapMatch = lower.match(/swap\s+(.+?)\s+and\s+(.+)/);
190
- if (swapMatch) {
191
- const v1 = findView(views, swapMatch[1].trim());
192
- const v2 = findView(views, swapMatch[2].trim());
193
- if (v1 && v2) {
194
- return { type: 'swap', details: { id1: v1.id, id2: v2.id } };
195
- }
196
- }
197
-
198
- // Remove view
199
- if (lower.includes('remove') || lower.includes('delete')) {
200
- const removeMatch = lower.match(/(?:remove|delete)\s+(?:the\s+)?(.+?)(?:\s+view|\s+chart|\s+panel)?$/);
201
- if (removeMatch) {
202
- const view = findView(views, removeMatch[1].trim());
203
- if (view) {
204
- return { type: 'remove_view', details: { viewId: view.id } };
205
- }
206
- }
207
- }
208
-
209
- // Add filter
210
- if (lower.includes('add') && lower.includes('filter')) {
211
- const filterMatch = lower.match(/add\s+(?:a\s+)?(?:(.+?)\s+)?filter/);
212
- if (filterMatch) {
213
- const field = filterMatch[1]?.trim();
214
- return {
215
- type: 'filter',
216
- details: {
217
- action: 'add',
218
- field: field || '',
219
- filterType: lower.includes('multi') ? 'multi-select' : 'select',
220
- },
221
- };
222
- }
223
- }
224
-
225
- // Filter to value
226
- if (lower.includes('filter to') || lower.includes('filter by')) {
227
- const filterValMatch = lower.match(/filter\s+(?:to|by)\s+(.+)/);
228
- if (filterValMatch) {
229
- return {
230
- type: 'filter',
231
- details: { action: 'set', value: filterValMatch[1].trim() },
232
- };
233
- }
234
- }
235
-
236
- // Add view
237
- if (lower.includes('add') && (lower.includes('chart') || lower.includes('view') || lower.includes('showing'))) {
238
- const addMatch = lower.match(/add\s+(?:a\s+)?(?:new\s+)?(?:chart|view|panel)\s+(?:showing\s+)?(.+)/);
239
- const intent = addMatch ? addMatch[1].trim() : clause;
240
- return { type: 'add_view', details: { intent } };
241
- }
242
-
243
- // Color/palette: "use a warm palette" or "warm colors" or "use warm palette for X"
244
- for (const [keyword, palette] of Object.entries(PALETTE_KEYWORDS)) {
245
- if (lower.includes(keyword) && (lower.includes('palette') || lower.includes('color'))) {
246
- // Check if it targets a specific view
247
- const viewMatch = lower.match(/(?:for|on)\s+(?:the\s+)?(.+?)(?:\s+view|\s+chart|\s+panel)?$/);
248
- if (viewMatch) {
249
- const view = findView(views, viewMatch[1].trim());
250
- if (view) {
251
- return { type: 'modify_view', details: { viewId: view.id, colorPreferences: { palette } } };
252
- }
253
- }
254
- // Apply to all views as a global color change
255
- return { type: 'modify_view', details: { allViews: true, colorPreferences: { palette } } };
256
- }
257
- }
258
-
259
- // Pattern override: "show X as a line chart" / "show the trend as a line"
260
- const patternOverride = lower.match(/(?:show|change|switch|display)\s+(?:the\s+)?(.+?)\s+(?:to|as)\s+(?:a\s+)?(\w[\w-]*)/);
261
- if (patternOverride) {
262
- const viewRef = patternOverride[1].trim();
263
- const patternCandidate = patternOverride[2].trim();
264
- // Only accept if the candidate is a known pattern or close to one
265
- const matchedPattern = KNOWN_PATTERNS.find(p => p === patternCandidate || p.startsWith(patternCandidate));
266
- if (matchedPattern) {
267
- const view = findView(views, viewRef);
268
- if (view) {
269
- return { type: 'modify_view', details: { viewId: view.id, pattern: matchedPattern } };
270
- }
271
- }
272
- }
273
-
274
- // Modify view: "change X to show top 5"
275
- const modifyMatch = lower.match(/(?:change|update|modify)\s+(?:the\s+)?(.+?)\s+to\s+(.+)/);
276
- if (modifyMatch) {
277
- const view = findView(views, modifyMatch[1].trim());
278
- if (view) {
279
- return { type: 'modify_view', details: { viewId: view.id, modification: modifyMatch[2].trim() } };
280
- }
281
- }
282
-
283
- return null;
284
- }
285
-
286
- /**
287
- * Parse a compound refinement into multiple actions.
288
- * Splits compound instructions, parses each clause independently.
289
- */
290
- function parseRefinements(refinement: string, views: DashboardViewSpec[]): RefinementAction[] {
291
- const clauses = splitClauses(refinement);
292
- const actions: RefinementAction[] = [];
293
-
294
- for (const clause of clauses) {
295
- const action = parseClause(clause, views);
296
- if (action) {
297
- actions.push(action);
298
- }
299
- }
300
-
301
- // If nothing matched, try the full string as one clause
302
- if (actions.length === 0) {
303
- const action = parseClause(refinement, views);
304
- if (action) {
305
- actions.push(action);
306
- } else {
307
- actions.push({ type: 'unknown', details: { refinement } });
308
- }
309
- }
310
-
311
- return actions;
312
- }
313
-
314
- // ─── HANDLER ────────────────────────────────────────────────────────────────
315
-
316
- export function handleRefineDashboard(deps: { sourceManager: any }) {
317
- return async (args: z.infer<typeof refineDashboardInputSchema>) => {
318
- const spec = JSON.parse(JSON.stringify(args.currentSpec)) as DashboardSpec;
319
- const refinement = args.refinement;
320
- const changes: string[] = [];
321
-
322
- const actions = parseRefinements(refinement, spec.views as DashboardViewSpec[]);
323
-
324
- for (const action of actions) {
325
- switch (action.type) {
326
- case 'theme': {
327
- spec.theme = action.details.theme;
328
- changes.push(`Changed theme to ${action.details.theme}`);
329
- break;
330
- }
331
-
332
- case 'title': {
333
- spec.title = action.details.title;
334
- changes.push(`Changed title to "${action.details.title}"`);
335
- break;
336
- }
337
-
338
- case 'layout': {
339
- if (action.details.columns) {
340
- spec.layout.columns = Math.min(4, Math.max(1, action.details.columns));
341
- changes.push(`Changed layout to ${spec.layout.columns} columns`);
342
- }
343
- if (action.details.viewId && action.details.colSpan) {
344
- if (!spec.layout.viewSizes) spec.layout.viewSizes = {};
345
- const span = action.details.colSpan === 'full' ? spec.layout.columns : action.details.colSpan;
346
- spec.layout.viewSizes[action.details.viewId] = {
347
- ...(spec.layout.viewSizes[action.details.viewId] || {}),
348
- colSpan: span,
349
- };
350
- changes.push(`Made view "${action.details.viewId}" span ${span} columns`);
351
- }
352
- break;
353
- }
354
-
355
- case 'swap': {
356
- const idx1 = spec.views.findIndex(v => v.id === action.details.id1);
357
- const idx2 = spec.views.findIndex(v => v.id === action.details.id2);
358
- if (idx1 >= 0 && idx2 >= 0) {
359
- const tmp = spec.views[idx1];
360
- spec.views[idx1] = spec.views[idx2];
361
- spec.views[idx2] = tmp;
362
- changes.push(`Swapped views "${action.details.id1}" and "${action.details.id2}"`);
363
- }
364
- break;
365
- }
366
-
367
- case 'remove_view': {
368
- const before = spec.views.length;
369
- spec.views = spec.views.filter(v => v.id !== action.details.viewId);
370
- if (spec.views.length < before) {
371
- changes.push(`Removed view "${action.details.viewId}"`);
372
- } else {
373
- changes.push(`View "${action.details.viewId}" not found`);
374
- }
375
- break;
376
- }
377
-
378
- case 'add_view': {
379
- const newId = `view-${spec.views.length + 1}`;
380
- const newView: DashboardViewSpec = {
381
- id: newId,
382
- title: action.details.intent,
383
- intent: action.details.intent,
384
- query: { select: ['*'] },
385
- };
386
- spec.views.push(newView);
387
- changes.push(`Added new view "${newId}" with intent: ${action.details.intent}`);
388
- break;
389
- }
390
-
391
- case 'filter': {
392
- if (action.details.action === 'add' && action.details.field) {
393
- if (!spec.globalFilters) spec.globalFilters = [];
394
- const filter: DashboardFilter = {
395
- field: action.details.field,
396
- type: action.details.filterType || 'select',
397
- };
398
- spec.globalFilters.push(filter);
399
- changes.push(`Added ${filter.type} filter for "${action.details.field}"`);
400
- } else if (action.details.action === 'set') {
401
- changes.push(`Filter value noted: "${action.details.value}" — apply via globalFilters.currentValue`);
402
- }
403
- break;
404
- }
405
-
406
- case 'modify_view': {
407
- // Handle allViews (e.g., global color change)
408
- const targetViews = action.details.allViews
409
- ? spec.views
410
- : spec.views.filter(v => v.id === action.details.viewId);
411
-
412
- for (const view of targetViews) {
413
- if (action.details.pattern) {
414
- view.pattern = action.details.pattern;
415
- changes.push(`Changed view "${view.id}" pattern to ${action.details.pattern}`);
416
- }
417
- if (action.details.colorPreferences) {
418
- if (!view.colorPreferences) view.colorPreferences = {};
419
- Object.assign(view.colorPreferences, action.details.colorPreferences);
420
- changes.push(`Updated color preferences for view "${view.id}"`);
421
- }
422
- if (action.details.modification) {
423
- const mod = action.details.modification.toLowerCase();
424
- const limitMatch = mod.match(/(?:top|limit|first)\s*(\d+)/);
425
- if (limitMatch) {
426
- view.query.limit = parseInt(limitMatch[1], 10);
427
- changes.push(`Limited view "${view.id}" to ${view.query.limit} rows`);
428
- } else {
429
- changes.push(`Modification noted for view "${view.id}": ${action.details.modification}`);
430
- }
431
- }
432
- }
433
- break;
434
- }
435
-
436
- default:
437
- changes.push(`Refinement noted: "${refinement}" — manual adjustment may be needed`);
438
- }
439
- }
440
-
441
- const viewResult = await executeDashboardViews(
442
- spec.views as DashboardViewSpec[],
443
- spec.sourceId,
444
- spec.table,
445
- deps.sourceManager,
446
- );
447
- if (isViewExecutionError(viewResult)) return viewResult;
448
-
449
- const html = buildDashboardHtml(spec, viewResult.viewData);
450
-
451
- return htmlResponse(
452
- { dashboardSpec: spec, changes, viewReasonings: viewResult.viewReasonings },
453
- html,
454
- );
455
- };
456
- }
@@ -1,62 +0,0 @@
1
- /**
2
- * MCP Tool: create_dashboard
3
- *
4
- * Creates a multi-view dashboard from a data source.
5
- * Each view has its own DSL query and intent for pattern selection.
6
- * Supports global filters and cross-view interactions.
7
- */
8
-
9
- import { z } from 'zod';
10
- import type { DashboardSpec, DashboardViewSpec } from '../../types.js';
11
- import { buildDashboardHtml } from '../../renderers/html/builders/dashboard.js';
12
- import { createDashboardInputSchema } from './dsl-schemas.js';
13
- import {
14
- errorResponse,
15
- htmlResponse,
16
- autoLayout,
17
- executeDashboardViews,
18
- isViewExecutionError,
19
- } from './shared.js';
20
-
21
- export { createDashboardInputSchema };
22
-
23
- export function handleCreateDashboard(deps: { sourceManager: any }) {
24
- return async (args: z.infer<typeof createDashboardInputSchema>) => {
25
- const sourceInfo = deps.sourceManager.get(args.sourceId);
26
- if (!sourceInfo) {
27
- return errorResponse(`Dataset not found: ${args.sourceId}. Use load_csv first.`);
28
- }
29
-
30
- const result = await executeDashboardViews(
31
- args.views as DashboardViewSpec[],
32
- args.sourceId,
33
- args.table,
34
- deps.sourceManager,
35
- );
36
- if (isViewExecutionError(result)) return result;
37
-
38
- const layout = args.layout || autoLayout(args.views.length);
39
- const dashboardId = `dashboard-${Date.now().toString(36)}`;
40
-
41
- const dashboardSpec: DashboardSpec = {
42
- dashboard: true,
43
- id: dashboardId,
44
- title: args.title || 'Dashboard',
45
- description: args.description,
46
- sourceId: args.sourceId,
47
- table: args.table,
48
- views: args.views as DashboardViewSpec[],
49
- globalFilters: args.globalFilters as DashboardSpec['globalFilters'],
50
- layout,
51
- interactions: args.interactions as DashboardSpec['interactions'],
52
- theme: args.theme,
53
- };
54
-
55
- const html = buildDashboardHtml(dashboardSpec, result.viewData);
56
-
57
- return htmlResponse(
58
- { dashboardSpec, viewReasonings: result.viewReasonings },
59
- html,
60
- );
61
- };
62
- }