@svgrid/enterprise 2.2.0 → 2.3.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 (101) hide show
  1. package/README.md +95 -81
  2. package/dist/cdn/svgrid-enterprise.svelte-external.js +22479 -10466
  3. package/dist/node/studio.js +11590 -2907
  4. package/package.json +31 -10
  5. package/src/SvAlertRuleEditor.svelte +294 -0
  6. package/src/SvAlertsManager.svelte +210 -0
  7. package/src/SvAlertsPanel.svelte +129 -0
  8. package/src/SvBoard.svelte +3 -1
  9. package/src/SvExpressionEditor.svelte +341 -0
  10. package/src/SvGridAlerts.dom.test.ts +113 -0
  11. package/src/SvGridAlerts.svelte +265 -0
  12. package/src/SvGridBoard.svelte +2355 -0
  13. package/src/SvGridEditPanel.svelte +849 -794
  14. package/src/SvGridScheduler.svelte +5334 -4410
  15. package/src/SvPivotDesigner.svelte +3 -3
  16. package/src/SvRecordDetail.svelte +6 -2
  17. package/src/SvSchedule.svelte +3 -1
  18. package/src/{ai-export-pdf.test.ts → ai-export-pdf.dom.test.ts} +4 -1
  19. package/src/{ai-export-xlsx.test.ts → ai-export-xlsx.dom.test.ts} +4 -1
  20. package/src/{ai-export.test.ts → ai-export.dom.test.ts} +5 -1
  21. package/src/alerts/alert-engine-attach.ts +165 -0
  22. package/src/alerts/alert-engine.test.ts +135 -0
  23. package/src/alerts/alert-engine.ts +260 -0
  24. package/src/alerts/alert-formats.test.ts +87 -0
  25. package/src/alerts/alert-formats.ts +77 -0
  26. package/src/alerts/alert-observer.test.ts +189 -0
  27. package/src/alerts/alert-observer.ts +208 -0
  28. package/src/alerts/alert-scheduler.ts +96 -0
  29. package/src/alerts/alert-storage.test.ts +54 -0
  30. package/src/alerts/alert-storage.ts +116 -0
  31. package/src/alerts/alert-store.svelte.ts +80 -0
  32. package/src/alerts/alert-types.ts +94 -0
  33. package/src/alerts.ts +28 -0
  34. package/src/board.dom.test.ts +941 -0
  35. package/src/board.ts +36 -0
  36. package/src/export.ts +13 -1
  37. package/src/expressions/evaluate.test.ts +111 -0
  38. package/src/expressions/evaluate.ts +228 -0
  39. package/src/expressions/expression-columns.ts +133 -0
  40. package/src/expressions/expression-types.ts +84 -0
  41. package/src/expressions/parse.test.ts +106 -0
  42. package/src/expressions/parse.ts +610 -0
  43. package/src/import.ts +2 -0
  44. package/src/index.ts +180 -46
  45. package/src/install.ts +12 -2
  46. package/src/pivot-enable.ts +44 -0
  47. package/src/scheduler-assignments.test.ts +97 -0
  48. package/src/scheduler-assignments.ts +134 -0
  49. package/src/scheduler-axis.test.ts +108 -0
  50. package/src/scheduler-axis.ts +238 -0
  51. package/src/scheduler-booking.test.ts +57 -0
  52. package/src/scheduler-booking.ts +63 -0
  53. package/src/scheduler-config.ts +179 -0
  54. package/src/scheduler-dependencies.test.ts +155 -0
  55. package/src/scheduler-dependencies.ts +223 -0
  56. package/src/scheduler-freebusy.test.ts +41 -0
  57. package/src/scheduler-freebusy.ts +36 -0
  58. package/src/scheduler-heatmap.test.ts +39 -0
  59. package/src/scheduler-heatmap.ts +55 -0
  60. package/src/scheduler-resource-tree.test.ts +84 -0
  61. package/src/scheduler-resource-tree.ts +107 -0
  62. package/src/scheduler-slots.test.ts +53 -0
  63. package/src/scheduler-slots.ts +94 -0
  64. package/src/scheduler-summary.test.ts +47 -0
  65. package/src/scheduler-summary.ts +81 -0
  66. package/src/sources/index.ts +1 -1
  67. package/src/sources/introspect-supabase.test.ts +13 -1
  68. package/src/sources/introspect-supabase.ts +20 -0
  69. package/src/studio/copilot-core.test.ts +45 -0
  70. package/src/studio/copilot-core.ts +65 -0
  71. package/src/studio/deploy-cli.test.ts +56 -0
  72. package/src/studio/deploy-cli.ts +100 -0
  73. package/src/studio/emit-project.test.ts +503 -18
  74. package/src/studio/emit-project.ts +829 -111
  75. package/src/studio/emit-schema.test.ts +17 -0
  76. package/src/studio/emit-schema.ts +166 -35
  77. package/src/studio/index.ts +25 -1
  78. package/src/studio/introspect-openapi.test.ts +84 -0
  79. package/src/studio/introspect-openapi.ts +252 -0
  80. package/src/studio/project.test.ts +57 -0
  81. package/src/studio/project.ts +193 -6
  82. package/src/studio/samples/crm.ts +282 -258
  83. package/src/studio/samples/fleet.ts +213 -186
  84. package/src/studio/samples/insurance.ts +223 -195
  85. package/src/studio/samples/inventory.ts +213 -182
  86. package/src/studio/samples/live-data.test.ts +99 -98
  87. package/src/studio/samples/live-data.ts +8 -10
  88. package/src/studio/samples/projects.ts +212 -190
  89. package/src/studio/samples/samples.test.ts +52 -0
  90. package/src/studio/samples/shared.ts +26 -6
  91. package/src/studio/samples/starter.ts +251 -0
  92. package/src/studio/samples/support.ts +209 -184
  93. package/src/studio/ui-components-surface.test.ts +185 -0
  94. package/src/studio/ui-components.generated.ts +5617 -0
  95. package/src/studio/ui-components.ts +641 -472
  96. package/src/sveltekit/sql-source.test.ts +13 -0
  97. package/src/sveltekit/sql-source.ts +11 -6
  98. package/src/upgrade-prompt.ts +2 -2
  99. package/src/watermark.ts +2 -2
  100. package/src/ai.test.ts +0 -522
  101. package/src/ai.ts +0 -1388
package/src/ai.ts DELETED
@@ -1,1388 +0,0 @@
1
- /**
2
- * Pro AI feature pack
3
- * -------------------
4
- *
5
- * Three jobs every analyst wishes a data grid could do:
6
- *
7
- * 1. Natural-language filter / sort (`aiFilter`)
8
- * "show me deals closing this quarter over $50k owned by Sasha"
9
- * -> { filters: [...], sort: [...] }
10
- *
11
- * 2. Smart fill (`aiSmartFill`)
12
- * User edits two cells in a column with example values; the
13
- * Grid proposes the rest. Excel "Flash Fill" pattern.
14
- *
15
- * 3. Summarise rows (`aiSummarize`)
16
- * Single row, the current selection, a group key, or the whole
17
- * filtered view -> a one-paragraph summary.
18
- *
19
- * The grid stays headless / model-agnostic. The consumer supplies an
20
- * `AIProvider` that knows how to call OpenAI / Anthropic / a local
21
- * model / a server-side proxy. We never bundle a model client.
22
- *
23
- * License gate: every AI call routes through `assertEnterpriseLicensed()`,
24
- * which soft-gates (still works, adds a watermark, console nudge) when
25
- * no key is set so demos and evaluation stay frictionless.
26
- */
27
-
28
- import type { RowData, SvGridApi, TableFeatures } from '@svgrid/grid'
29
- import { assertEnterpriseLicensed } from './license'
30
- import { exportGrid, type ExportFormat } from './export'
31
-
32
- // ---------------------------------------------------------------------------
33
- // Provider contract
34
- // ---------------------------------------------------------------------------
35
-
36
- /**
37
- * Shape every consumer-provided model adapter must implement. Keeping this
38
- * tiny (one async call, two response formats) lets the same adapter drive
39
- * OpenAI's `chat.completions`, Anthropic's `messages`, a self-hosted
40
- * llama.cpp endpoint, or a server-side proxy. The grid only cares that the
41
- * provider eventually returns a string we can parse.
42
- */
43
- export type AIProvider = (request: AIRequest) => Promise<string>
44
-
45
- export type AIRequest = {
46
- /** Full prompt the grid built for the model. Already includes column
47
- * schema and any sampled rows where applicable. */
48
- prompt: string
49
- /** When 'json', the provider should ask the model to return strict
50
- * JSON only - no prose. We parse the response with JSON.parse and
51
- * throw a typed error on failure. */
52
- responseFormat?: 'text' | 'json'
53
- /** Honored if the underlying transport supports cancellation. */
54
- signal?: AbortSignal
55
- /** Free-form tag for telemetry / logging. One of: 'filter',
56
- * 'smart-fill', 'summarize', 'classify'. */
57
- task: AITask
58
- /** Soft hint to the provider about how many tokens we expect back.
59
- * Useful for routing small jobs to a cheaper model. */
60
- maxOutputTokens?: number
61
- }
62
-
63
- export type AITask = 'filter' | 'smart-fill' | 'summarize' | 'classify' | 'export' | 'anomaly' | 'chart'
64
-
65
- let provider: AIProvider | null = null
66
-
67
- /**
68
- * Register the model adapter every AI call will route through. Call once
69
- * at app boot. Passing `null` clears the provider and AI calls revert to
70
- * throwing "no provider" errors.
71
- */
72
- export function setAIProvider(p: AIProvider | null): void {
73
- provider = p
74
- }
75
-
76
- export function getAIProvider(): AIProvider | null {
77
- return provider
78
- }
79
-
80
- export function hasAIProvider(): boolean {
81
- return provider != null
82
- }
83
-
84
- class NoProviderError extends Error {
85
- constructor() {
86
- super(
87
- '@svgrid/enterprise/ai: no AI provider registered. Call setAIProvider(fn) ' +
88
- 'with an adapter that talks to OpenAI / Anthropic / your proxy.',
89
- )
90
- this.name = 'NoProviderError'
91
- }
92
- }
93
-
94
- class BadJsonError extends Error {
95
- constructor(raw: string) {
96
- super(
97
- '@svgrid/enterprise/ai: provider returned non-JSON for a json-format request. ' +
98
- `First 200 chars: ${raw.slice(0, 200)}`,
99
- )
100
- this.name = 'BadJsonError'
101
- }
102
- }
103
-
104
- async function callProvider(req: AIRequest): Promise<string> {
105
- if (!provider) throw new NoProviderError()
106
- return provider(req)
107
- }
108
-
109
- async function callJSON<T>(req: AIRequest): Promise<T> {
110
- const raw = await callProvider({ ...req, responseFormat: 'json' })
111
- // Models occasionally wrap JSON in markdown code fences even when asked
112
- // for raw JSON. Strip a single fenced block if present, then parse.
113
- const stripped = raw.trim().replace(/^```(?:json)?\s*|\s*```$/g, '')
114
- try {
115
- return JSON.parse(stripped) as T
116
- } catch {
117
- throw new BadJsonError(raw)
118
- }
119
- }
120
-
121
- // ---------------------------------------------------------------------------
122
- // Helpers - build column schema + row sample from the grid API
123
- // ---------------------------------------------------------------------------
124
-
125
- type ColumnSchemaEntry = {
126
- field: string
127
- type: 'string' | 'number' | 'boolean' | 'date' | 'unknown'
128
- sampleValues: unknown[]
129
- }
130
-
131
- /**
132
- * Inspect the first N rows the grid is holding to infer a typed schema +
133
- * a handful of sample values per column. This is the context every AI
134
- * task needs and the most expensive thing to build, so the helpers cache
135
- * the result inside one task call rather than calling api.getData()
136
- * repeatedly.
137
- */
138
- function buildColumnSchema<
139
- TFeatures extends TableFeatures,
140
- TData extends RowData,
141
- >(api: SvGridApi<TFeatures, TData>, sampleSize = 50): ColumnSchemaEntry[] {
142
- const data = api.getData()
143
- if (data.length === 0) return []
144
- const slice = data.slice(0, Math.min(data.length, sampleSize))
145
- const fields = new Set<string>()
146
- for (const row of slice) {
147
- for (const k of Object.keys(row as object)) fields.add(k)
148
- }
149
- const out: ColumnSchemaEntry[] = []
150
- for (const field of fields) {
151
- const values: unknown[] = []
152
- for (const row of slice) {
153
- const v = (row as Record<string, unknown>)[field]
154
- if (v != null) values.push(v)
155
- if (values.length >= 5) break
156
- }
157
- out.push({
158
- field,
159
- type: inferType(values),
160
- sampleValues: values,
161
- })
162
- }
163
- return out
164
- }
165
-
166
- function inferType(samples: unknown[]): ColumnSchemaEntry['type'] {
167
- if (samples.length === 0) return 'unknown'
168
- const v = samples[0]
169
- if (typeof v === 'number') return 'number'
170
- if (typeof v === 'boolean') return 'boolean'
171
- if (typeof v === 'string') {
172
- // Cheap ISO-date sniff so we route date-style filters correctly.
173
- if (/^\d{4}-\d{2}-\d{2}/.test(v)) return 'date'
174
- return 'string'
175
- }
176
- return 'unknown'
177
- }
178
-
179
- function schemaToPromptBlock(schema: ColumnSchemaEntry[]): string {
180
- return schema
181
- .map(
182
- (c) =>
183
- `- ${c.field} (${c.type}): e.g. ${c.sampleValues
184
- .slice(0, 3)
185
- .map((v) => JSON.stringify(v))
186
- .join(', ')}`,
187
- )
188
- .join('\n')
189
- }
190
-
191
- // ---------------------------------------------------------------------------
192
- // 1. Natural-language filter / sort
193
- // ---------------------------------------------------------------------------
194
-
195
- export type AIFilterClause = {
196
- field: string
197
- operator: 'contains' | 'equals' | 'startsWith' | 'greaterThan' | 'lessThan' | 'isBlank'
198
- value?: string
199
- }
200
- export type AISortClause = { field: string; desc: boolean }
201
-
202
- export type AIFilterResult = {
203
- filters: AIFilterClause[]
204
- sort: AISortClause[]
205
- /** Plain-English explanation of how the model interpreted the query.
206
- * Surface this in the UI so the user can confirm or undo. */
207
- rationale: string
208
- }
209
-
210
- export type AIFilterOptions = {
211
- /**
212
- * When true, the helper not only RETURNS the plan but also applies it
213
- * to the grid via `api.setFilter` / `api.setSort`. Defaults to false
214
- * so callers can show a preview before committing.
215
- */
216
- apply?: boolean
217
- signal?: AbortSignal
218
- }
219
-
220
- /**
221
- * Translate a natural-language query into a filter+sort plan against
222
- * the current grid's columns. The grid passes the column schema (names,
223
- * types, sample values) to the model so it can pick the right field
224
- * names without hallucinating.
225
- */
226
- export async function aiFilter<
227
- TFeatures extends TableFeatures,
228
- TData extends RowData,
229
- >(
230
- api: SvGridApi<TFeatures, TData>,
231
- query: string,
232
- opts: AIFilterOptions = {},
233
- ): Promise<AIFilterResult> {
234
- assertEnterpriseLicensed('AI assistant')
235
- const schema = buildColumnSchema(api)
236
- const prompt =
237
- `You are a data-grid filter planner. Translate the user's natural-language ` +
238
- `query into a strict-JSON plan that the grid can apply.\n\n` +
239
- `Columns:\n${schemaToPromptBlock(schema)}\n\n` +
240
- `Output JSON schema:\n` +
241
- `{ "filters": [{"field": "<one of the columns above>", ` +
242
- `"operator": "contains"|"equals"|"startsWith"|"greaterThan"|"lessThan"|"isBlank", ` +
243
- `"value": "<string>"}], ` +
244
- `"sort": [{"field": "<column>", "desc": true|false}], ` +
245
- `"rationale": "<one-sentence explanation>" }\n\n` +
246
- `User query: ${query}\n\n` +
247
- `Return JSON only, no prose.`
248
-
249
- const result = await callJSON<AIFilterResult>({
250
- prompt,
251
- task: 'filter',
252
- signal: opts.signal,
253
- maxOutputTokens: 400,
254
- })
255
-
256
- // Defensive: drop anything that doesn't match a real column. Models love
257
- // to invent plausible-sounding field names; we'd rather skip a clause
258
- // than throw at runtime.
259
- const valid = new Set(schema.map((c) => c.field))
260
- result.filters = (result.filters ?? []).filter((f) => valid.has(f.field))
261
- result.sort = (result.sort ?? []).filter((s) => valid.has(s.field))
262
-
263
- if (opts.apply) {
264
- api.clearAllFilters()
265
- api.clearSort()
266
- for (const f of result.filters) {
267
- api.setFilter(f.field, { operator: f.operator, value: f.value ?? '' })
268
- }
269
- // Multiple sort clauses get applied last-wins because the public
270
- // `setSort` is single-key. Multi-sort is on the roadmap.
271
- const last = result.sort[result.sort.length - 1]
272
- if (last) api.setSort(last.field, last.desc ? 'desc' : 'asc')
273
- }
274
-
275
- return result
276
- }
277
-
278
- // ---------------------------------------------------------------------------
279
- // 2. Smart fill
280
- // ---------------------------------------------------------------------------
281
-
282
- export type AISmartFillExample = { input: Record<string, unknown>; output: unknown }
283
-
284
- export type AISmartFillResult<TValue = unknown> = {
285
- field: string
286
- predictions: Array<{ rowIndex: number; value: TValue; confidence: number }>
287
- rationale: string
288
- }
289
-
290
- export type AISmartFillOptions = {
291
- /** Target column - the one whose values we want filled. */
292
- field: string
293
- /** Index of rows the model should propose values for. If omitted, every
294
- * row whose current `field` value is `null`, `undefined` or `''` is
295
- * selected automatically. */
296
- targetRowIndices?: number[]
297
- /**
298
- * Worked examples the user has already filled in. Required - the
299
- * model needs at least one to know the pattern, two or more to lock
300
- * the schema. We don't pull these from the grid automatically because
301
- * "edited" vs "untouched" isn't a state SvGrid exposes today.
302
- */
303
- examples: AISmartFillExample[]
304
- signal?: AbortSignal
305
- }
306
-
307
- /**
308
- * Given a column and a few user-provided examples, propose values for the
309
- * untouched rows. Returns predictions only - the caller chooses whether to
310
- * commit them via `api.setCellValue`. This is the right shape for an
311
- * accept-per-cell UX with confidence-coloured highlights.
312
- */
313
- export async function aiSmartFill<
314
- TFeatures extends TableFeatures,
315
- TData extends RowData,
316
- TValue = unknown,
317
- >(
318
- api: SvGridApi<TFeatures, TData>,
319
- opts: AISmartFillOptions,
320
- ): Promise<AISmartFillResult<TValue>> {
321
- assertEnterpriseLicensed('AI assistant')
322
- if (opts.examples.length === 0) {
323
- throw new Error('@svgrid/enterprise/ai: aiSmartFill requires at least one example.')
324
- }
325
- const data = api.getData()
326
- const targets =
327
- opts.targetRowIndices ??
328
- data
329
- .map((row, i) => {
330
- const v = (row as Record<string, unknown>)[opts.field]
331
- return v == null || v === '' ? i : -1
332
- })
333
- .filter((i) => i >= 0)
334
-
335
- if (targets.length === 0) {
336
- return { field: opts.field, predictions: [], rationale: 'No empty cells to fill.' }
337
- }
338
-
339
- const examplesBlock = opts.examples
340
- .map((ex, i) => `Example ${i + 1}: input=${JSON.stringify(ex.input)} -> output=${JSON.stringify(ex.output)}`)
341
- .join('\n')
342
-
343
- const targetRowsBlock = targets
344
- .map((i) => `Row ${i}: ${JSON.stringify(data[i])}`)
345
- .join('\n')
346
-
347
- const prompt =
348
- `You are filling in a single column of a data grid based on user-provided ` +
349
- `examples. Match the pattern in the examples exactly.\n\n` +
350
- `Target column: ${opts.field}\n\n` +
351
- `Examples:\n${examplesBlock}\n\n` +
352
- `Rows to fill:\n${targetRowsBlock}\n\n` +
353
- `Return strict JSON only:\n` +
354
- `{ "predictions": [{"rowIndex": <number>, "value": <inferred value>, ` +
355
- `"confidence": <0..1>}], "rationale": "<one-sentence pattern description>" }`
356
-
357
- const result = await callJSON<{
358
- predictions: Array<{ rowIndex: number; value: TValue; confidence: number }>
359
- rationale: string
360
- }>({
361
- prompt,
362
- task: 'smart-fill',
363
- signal: opts.signal,
364
- maxOutputTokens: 600,
365
- })
366
-
367
- return {
368
- field: opts.field,
369
- predictions: result.predictions ?? [],
370
- rationale: result.rationale ?? '',
371
- }
372
- }
373
-
374
- // ---------------------------------------------------------------------------
375
- // 3. Summarise
376
- // ---------------------------------------------------------------------------
377
-
378
- export type AISummarizeTarget =
379
- | { kind: 'row'; rowIndex: number }
380
- | { kind: 'all' }
381
- | { kind: 'selection'; rowIndices: number[] }
382
- | { kind: 'group'; field: string; value: unknown }
383
-
384
- export type AISummary = {
385
- text: string
386
- bullets: string[]
387
- /** Field names the model thinks are the most load-bearing for the
388
- * story it just told. UI can highlight those columns. */
389
- highlightedFields: string[]
390
- }
391
-
392
- export type AISummarizeOptions = {
393
- target: AISummarizeTarget
394
- /** Optional question the user is trying to answer. Helps the model
395
- * bias the summary toward the relevant columns. */
396
- question?: string
397
- signal?: AbortSignal
398
- }
399
-
400
- /**
401
- * Ask the model to summarise a slice of the grid. Caps the row sample at
402
- * a model-friendly size; for huge selections it samples uniformly so the
403
- * summary stays representative without blowing the context window.
404
- */
405
- export async function aiSummarize<
406
- TFeatures extends TableFeatures,
407
- TData extends RowData,
408
- >(
409
- api: SvGridApi<TFeatures, TData>,
410
- opts: AISummarizeOptions,
411
- ): Promise<AISummary> {
412
- assertEnterpriseLicensed('AI assistant')
413
- const all = api.getData()
414
- const rows: TData[] = (() => {
415
- const t = opts.target
416
- if (t.kind === 'row') {
417
- const r = all[t.rowIndex]
418
- return r ? [r] : []
419
- }
420
- if (t.kind === 'selection') {
421
- return t.rowIndices.map((i) => all[i]).filter((r): r is TData => r != null)
422
- }
423
- if (t.kind === 'group') {
424
- return all.filter((r) => (r as Record<string, unknown>)[t.field] === t.value)
425
- }
426
- return all.slice()
427
- })()
428
-
429
- if (rows.length === 0) {
430
- return { text: 'No rows in scope.', bullets: [], highlightedFields: [] }
431
- }
432
-
433
- // Uniform sample to stay inside a sensible context budget.
434
- const MAX_SAMPLE = 25
435
- const sample = rows.length <= MAX_SAMPLE
436
- ? rows
437
- : Array.from({ length: MAX_SAMPLE }, (_, i) => rows[Math.floor((i / MAX_SAMPLE) * rows.length)]!)
438
-
439
- const schema = buildColumnSchema(api, 30)
440
- const prompt =
441
- `You are summarising a slice of a business data grid for a busy analyst.\n\n` +
442
- `Columns:\n${schemaToPromptBlock(schema)}\n\n` +
443
- `Slice size: ${rows.length} row(s). Sampled rows shown below.\n` +
444
- `${sample.map((r) => JSON.stringify(r)).join('\n')}\n\n` +
445
- (opts.question ? `User's question: ${opts.question}\n\n` : '') +
446
- `Return strict JSON only:\n` +
447
- `{ "text": "<one short paragraph>", ` +
448
- `"bullets": ["<3-5 punchy bullets>"], ` +
449
- `"highlightedFields": ["<column names the summary leans on>"] }`
450
-
451
- const result = await callJSON<AISummary>({
452
- prompt,
453
- task: 'summarize',
454
- signal: opts.signal,
455
- maxOutputTokens: 600,
456
- })
457
- return {
458
- text: result.text ?? '',
459
- bullets: result.bullets ?? [],
460
- highlightedFields: result.highlightedFields ?? [],
461
- }
462
- }
463
-
464
- // ---------------------------------------------------------------------------
465
- // 4. Classify (free-text -> bucketed value)
466
- // ---------------------------------------------------------------------------
467
-
468
- export type AIClassifyOptions = {
469
- /** Column whose free-text we're classifying. */
470
- inputField: string
471
- /** Target column the model should write to. */
472
- outputField: string
473
- /** Allowed values. The model is constrained to pick one. */
474
- classes: string[]
475
- /** Optional one-line description of each class (acts as a labeling rubric). */
476
- classDescriptions?: Record<string, string>
477
- /** Rows to classify. Defaults to all. */
478
- targetRowIndices?: number[]
479
- signal?: AbortSignal
480
- }
481
-
482
- export type AIClassifyResult = {
483
- inputField: string
484
- outputField: string
485
- predictions: Array<{ rowIndex: number; value: string; confidence: number }>
486
- }
487
-
488
- /**
489
- * Bucket free-text cells into one of a known set of classes. The model
490
- * is constrained to pick from `opts.classes`; predictions outside the set
491
- * are dropped so the caller can rely on the output being clean enum
492
- * values it can write straight back into the grid.
493
- */
494
- export async function aiClassify<
495
- TFeatures extends TableFeatures,
496
- TData extends RowData,
497
- >(
498
- api: SvGridApi<TFeatures, TData>,
499
- opts: AIClassifyOptions,
500
- ): Promise<AIClassifyResult> {
501
- assertEnterpriseLicensed('AI assistant')
502
- const data = api.getData()
503
- const targets = opts.targetRowIndices ?? data.map((_, i) => i)
504
- const rubric = opts.classDescriptions
505
- ? Object.entries(opts.classDescriptions)
506
- .map(([k, v]) => `- ${k}: ${v}`)
507
- .join('\n')
508
- : opts.classes.map((c) => `- ${c}`).join('\n')
509
-
510
- const rowsBlock = targets
511
- .map((i) => `Row ${i}: ${JSON.stringify((data[i] as Record<string, unknown>)?.[opts.inputField] ?? '')}`)
512
- .join('\n')
513
-
514
- const prompt =
515
- `You are a strict text classifier. For each row, pick exactly one ` +
516
- `label from the list. If unsure, pick the closest match and lower the ` +
517
- `confidence.\n\n` +
518
- `Allowed labels (with optional rubric):\n${rubric}\n\n` +
519
- `Rows to classify (text from column "${opts.inputField}"):\n${rowsBlock}\n\n` +
520
- `Return strict JSON only:\n` +
521
- `{ "predictions": [{"rowIndex": <number>, "value": "<label>", "confidence": <0..1>}] }`
522
-
523
- const result = await callJSON<{
524
- predictions: Array<{ rowIndex: number; value: string; confidence: number }>
525
- }>({
526
- prompt,
527
- task: 'classify',
528
- signal: opts.signal,
529
- maxOutputTokens: Math.min(2000, 30 + targets.length * 30),
530
- })
531
-
532
- const allowed = new Set(opts.classes)
533
- return {
534
- inputField: opts.inputField,
535
- outputField: opts.outputField,
536
- predictions: (result.predictions ?? []).filter((p) => allowed.has(p.value)),
537
- }
538
- }
539
-
540
- // ---------------------------------------------------------------------------
541
- // 5. Natural-language export
542
- // ---------------------------------------------------------------------------
543
-
544
- const EXPORT_FORMATS: ExportFormat[] = ['xlsx', 'xls', 'pdf', 'csv', 'tsv', 'html', 'json', 'xml', 'md']
545
-
546
- export type AIExportPlan = {
547
- format: ExportFormat
548
- filters: AIFilterClause[]
549
- sort: AISortClause[]
550
- groupBy: string[]
551
- /** Plain-English explanation of how the model read the request. */
552
- rationale: string
553
- }
554
-
555
- export type AIExportOptions = {
556
- /**
557
- * Also apply the filter / sort / grouping to the grid (mutating the view) so
558
- * it mirrors the export. Default FALSE - the export is self-contained (it
559
- * computes its own rows), so the grid is left untouched unless you opt in.
560
- */
561
- apply?: boolean
562
- /** Actually download the file. Default true; set false to preview the plan. */
563
- run?: boolean
564
- /** Base filename (no extension). Default 'export'. */
565
- filename?: string
566
- signal?: AbortSignal
567
- }
568
-
569
- /**
570
- * Turn a natural-language request ("export EU orders from Q2 as a grouped PDF")
571
- * into an export: the model returns a `{ format, filters, sort, groupBy }` plan
572
- * against the grid's columns; we apply the filter/sort to the grid and hand the
573
- * result to `exportGrid`. Returns the plan so the UI can show what it did (or
574
- * preview it first with `run: false`).
575
- */
576
- export async function aiExport<
577
- TFeatures extends TableFeatures,
578
- TData extends RowData,
579
- >(
580
- api: SvGridApi<TFeatures, TData>,
581
- query: string,
582
- opts: AIExportOptions = {},
583
- ): Promise<AIExportPlan> {
584
- assertEnterpriseLicensed('AI assistant')
585
- const schema = buildColumnSchema(api)
586
- const prompt =
587
- `You are an export assistant for a data grid. Translate the user's request ` +
588
- `into a strict-JSON export plan.\n\n` +
589
- `Columns:\n${schemaToPromptBlock(schema)}\n\n` +
590
- `Output JSON schema:\n` +
591
- `{ "format": "xlsx"|"xls"|"pdf"|"csv"|"tsv"|"html"|"json"|"xml"|"md", ` +
592
- `"filters": [{"field":"<column>","operator":"contains"|"equals"|"startsWith"|"greaterThan"|"lessThan"|"isBlank","value":"<string>"}], ` +
593
- `"sort": [{"field":"<column>","desc":true|false}], ` +
594
- `"groupBy": ["<column>"], ` +
595
- `"rationale": "<one sentence>" }\n\n` +
596
- `Rules: use the format the user names (default "xlsx"); only use column ` +
597
- `names from the list above; "grouped" / "group by X" -> groupBy. ` +
598
- `Return JSON only, no prose.\n\n` +
599
- `User request: ${query}`
600
-
601
- const plan = await callJSON<AIExportPlan>({
602
- prompt,
603
- task: 'export',
604
- signal: opts.signal,
605
- maxOutputTokens: 500,
606
- })
607
-
608
- // Defensive validation - drop invented fields, clamp the format.
609
- const valid = new Set(schema.map((c) => c.field))
610
- plan.format = EXPORT_FORMATS.includes(plan.format) ? plan.format : 'xlsx'
611
- plan.filters = (plan.filters ?? []).filter((f) => valid.has(f.field))
612
- plan.sort = (plan.sort ?? []).filter((s) => valid.has(s.field))
613
- plan.groupBy = (plan.groupBy ?? []).filter((g) => valid.has(g))
614
- plan.rationale = plan.rationale ?? ''
615
-
616
- // Optionally mirror the plan onto the grid (opt-in). Off by default so the
617
- // export never disturbs the user's current view.
618
- if (opts.apply === true) {
619
- api.clearAllFilters()
620
- api.clearSort()
621
- for (const f of plan.filters) {
622
- api.setFilter(f.field, { operator: f.operator, value: f.value ?? '' })
623
- }
624
- const last = plan.sort[plan.sort.length - 1]
625
- if (last) api.setSort(last.field, last.desc ? 'desc' : 'asc')
626
- if (plan.groupBy.length > 0) {
627
- try {
628
- api.setGroupBy(plan.groupBy)
629
- } catch {
630
- // grouping feature not enabled - the export still groups on its own
631
- }
632
- }
633
- }
634
-
635
- if (opts.run !== false) {
636
- // Compute the export rows from the plan directly (filter + sort + make
637
- // contiguous by group) rather than reading the grid's reactive displayed
638
- // rows - so the download always has exactly the intended data.
639
- const exportRows = applyPlanToRows(api.getData(), plan)
640
- await exportGrid(api, {
641
- format: plan.format,
642
- filename: opts.filename ?? 'export',
643
- rows: exportRows,
644
- ...(plan.groupBy.length > 0 ? { groupBy: plan.groupBy } : {}),
645
- })
646
- }
647
- return plan
648
- }
649
-
650
- /** Evaluate one AI filter clause against a raw row value. */
651
- function matchesClause(row: Record<string, unknown>, clause: AIFilterClause): boolean {
652
- const v = row[clause.field]
653
- const val = clause.value ?? ''
654
- switch (clause.operator) {
655
- case 'contains':
656
- return String(v ?? '').toLowerCase().includes(val.toLowerCase())
657
- case 'equals':
658
- return String(v ?? '').toLowerCase() === val.toLowerCase()
659
- case 'startsWith':
660
- return String(v ?? '').toLowerCase().startsWith(val.toLowerCase())
661
- case 'greaterThan':
662
- return Number(v) > Number(val)
663
- case 'lessThan':
664
- return Number(v) < Number(val)
665
- case 'isBlank':
666
- return v == null || v === ''
667
- default:
668
- return true
669
- }
670
- }
671
-
672
- /** Filter + sort + (stable) group the raw rows per an export plan. */
673
- function applyPlanToRows<TData extends RowData>(
674
- data: ReadonlyArray<TData>,
675
- plan: AIExportPlan,
676
- ): TData[] {
677
- let rows: TData[] = data.slice()
678
- if (plan.filters.length > 0) {
679
- rows = rows.filter((r) =>
680
- plan.filters.every((f) => matchesClause(r as Record<string, unknown>, f)),
681
- )
682
- }
683
- const last = plan.sort[plan.sort.length - 1]
684
- if (last) {
685
- rows = [...rows].sort((a, b) => {
686
- const av = (a as Record<string, unknown>)[last.field]
687
- const bv = (b as Record<string, unknown>)[last.field]
688
- const na = Number(av)
689
- const nb = Number(bv)
690
- let cmp: number
691
- if (Number.isFinite(na) && Number.isFinite(nb)) cmp = na - nb
692
- else cmp = String(av ?? '').localeCompare(String(bv ?? ''))
693
- return last.desc ? -cmp : cmp
694
- })
695
- }
696
- // Contiguous grouping (first-seen order) so PDF group rows + Excel outline
697
- // line up.
698
- if (plan.groupBy.length > 0) {
699
- const buckets = new Map<string, TData[]>()
700
- for (const r of rows) {
701
- const key = plan.groupBy
702
- .map((k) => String((r as Record<string, unknown>)[k] ?? ''))
703
- .join('')
704
- const bucket = buckets.get(key)
705
- if (bucket) bucket.push(r)
706
- else buckets.set(key, [r])
707
- }
708
- rows = [...buckets.values()].flat()
709
- }
710
- return rows
711
- }
712
-
713
- // ---------------------------------------------------------------------------
714
- // 6. Find anomalies
715
- // ---------------------------------------------------------------------------
716
-
717
- export type AIAnomaly = {
718
- /** Index into the SCANNED rows (target order), when the model pins one row. */
719
- rowIndex?: number
720
- field?: string
721
- value?: unknown
722
- reason: string
723
- severity: 'low' | 'medium' | 'high'
724
- }
725
-
726
- export type AIAnomalyResult = {
727
- anomalies: AIAnomaly[]
728
- summary: string
729
- }
730
-
731
- export type AIAnomalyOptions = {
732
- /** Which rows to scan. Defaults to the whole dataset. */
733
- target?: AISummarizeTarget
734
- /** Optional focus, e.g. "look at pricing and margins". */
735
- question?: string
736
- signal?: AbortSignal
737
- }
738
-
739
- function sliceForTarget<TFeatures extends TableFeatures, TData extends RowData>(
740
- api: SvGridApi<TFeatures, TData>,
741
- target: AISummarizeTarget,
742
- ): TData[] {
743
- const all = api.getData()
744
- if (target.kind === 'row') {
745
- const r = all[target.rowIndex]
746
- return r ? [r] : []
747
- }
748
- if (target.kind === 'selection') {
749
- return target.rowIndices.map((i) => all[i]).filter((r): r is TData => r != null)
750
- }
751
- if (target.kind === 'group') {
752
- return all.filter((r) => (r as Record<string, unknown>)[target.field] === target.value)
753
- }
754
- return all.slice()
755
- }
756
-
757
- /**
758
- * Scan a slice of the grid (all / selection / group) for anomalies - outliers,
759
- * suspicious values, inconsistencies - and return a structured list plus a
760
- * one-line summary. Pairs naturally with an export: "find the odd ones, then
761
- * export just those".
762
- */
763
- export async function aiFindAnomalies<
764
- TFeatures extends TableFeatures,
765
- TData extends RowData,
766
- >(
767
- api: SvGridApi<TFeatures, TData>,
768
- opts: AIAnomalyOptions = {},
769
- ): Promise<AIAnomalyResult> {
770
- assertEnterpriseLicensed('AI assistant')
771
- const rows = sliceForTarget(api, opts.target ?? { kind: 'all' })
772
- if (rows.length === 0) return { anomalies: [], summary: 'No rows in scope.' }
773
-
774
- const MAX_SAMPLE = 40
775
- const sample =
776
- rows.length <= MAX_SAMPLE
777
- ? rows
778
- : Array.from(
779
- { length: MAX_SAMPLE },
780
- (_, i) => rows[Math.floor((i / MAX_SAMPLE) * rows.length)]!,
781
- )
782
-
783
- const schema = buildColumnSchema(api, 30)
784
- const prompt =
785
- `You are a data-quality analyst. Scan the rows below for anomalies: ` +
786
- `numeric outliers, impossible / inconsistent values, unexpected blanks, ` +
787
- `and values that break the pattern of the column.\n\n` +
788
- `Columns:\n${schemaToPromptBlock(schema)}\n\n` +
789
- `Rows (index is 0-based into this list):\n` +
790
- `${sample.map((r, i) => `${i}: ${JSON.stringify(r)}`).join('\n')}\n\n` +
791
- (opts.question ? `Focus: ${opts.question}\n\n` : '') +
792
- `Return strict JSON only:\n` +
793
- `{ "anomalies": [{"rowIndex": <number|null>, "field": "<column|null>", ` +
794
- `"reason": "<why it's odd>", "severity": "low"|"medium"|"high"}], ` +
795
- `"summary": "<one sentence>" }`
796
-
797
- const result = await callJSON<AIAnomalyResult>({
798
- prompt,
799
- task: 'anomaly',
800
- signal: opts.signal,
801
- maxOutputTokens: 800,
802
- })
803
- return {
804
- anomalies: result.anomalies ?? [],
805
- summary: result.summary ?? '',
806
- }
807
- }
808
-
809
- // ---------------------------------------------------------------------------
810
- // 7. Natural-language chart ("chart this")
811
- // ---------------------------------------------------------------------------
812
-
813
- export type AIChartType = 'bar' | 'line' | 'area' | 'pie'
814
-
815
- export type AIChartPlan = {
816
- type: AIChartType
817
- /** Group-by (category-axis) column field, or null. */
818
- dimension: string | null
819
- /** Split-by column field: one series per distinct value, or null. */
820
- series: string | null
821
- /** Measure (value-axis) column field, or null. */
822
- measure: string | null
823
- reduce: 'sum' | 'avg' | 'count'
824
- stacked: boolean
825
- logScale: boolean
826
- timeAxis: boolean
827
- valueFormat: 'number' | 'currency' | 'percent'
828
- rationale: string
829
- }
830
-
831
- export type AIChartOptions = {
832
- /** Apply the plan to the grid's chart panel (open + configure). Default false. */
833
- apply?: boolean
834
- signal?: AbortSignal
835
- }
836
-
837
- /**
838
- * Translate a natural-language request into a chart plan for the grid's built-in
839
- * `charting` panel. Validates every field against the real schema, then (with
840
- * `apply: true`) pushes it into the panel through `api.configureChart`.
841
- */
842
- export async function aiChart<
843
- TFeatures extends TableFeatures,
844
- TData extends RowData,
845
- >(
846
- api: SvGridApi<TFeatures, TData>,
847
- query: string,
848
- opts: AIChartOptions = {},
849
- ): Promise<AIChartPlan> {
850
- assertEnterpriseLicensed('AI assistant')
851
- const schema = buildColumnSchema(api)
852
- const prompt =
853
- `You are a charting assistant for a data grid. Translate the user's request ` +
854
- `into a strict-JSON chart plan the grid can render.\n\n` +
855
- `Columns:\n${schemaToPromptBlock(schema)}\n\n` +
856
- `Output JSON schema:\n` +
857
- `{ "type": "bar"|"line"|"area"|"pie", ` +
858
- `"dimension": "<a categorical column to group by>", ` +
859
- `"series": "<a second categorical column to split into series, or null>", ` +
860
- `"measure": "<a numeric column to aggregate>", ` +
861
- `"reduce": "sum"|"avg"|"count", ` +
862
- `"stacked": true|false, ` +
863
- `"logScale": true|false, ` +
864
- `"timeAxis": true|false, ` +
865
- `"valueFormat": "number"|"currency"|"percent", ` +
866
- `"rationale": "<one-sentence explanation>" }\n\n` +
867
- `Rules: pick "dimension" and "series" from text/date columns and "measure" ` +
868
- `from a numeric column; use only column names from the list above; ` +
869
- `"stacked"/"stack" -> stacked:true; "log"/"logarithmic" -> logScale:true; ` +
870
- `"over time"/"by date"/a date dimension -> timeAxis:true; ` +
871
- `money/revenue/price/$ -> valueFormat:"currency"; rate/%/share -> valueFormat:"percent"; ` +
872
- `else valueFormat:"number". default reduce is "sum". ` +
873
- `Return JSON only, no prose.\n\n` +
874
- `User request: ${query}`
875
-
876
- const plan = await callJSON<AIChartPlan>({
877
- prompt,
878
- task: 'chart',
879
- signal: opts.signal,
880
- maxOutputTokens: 300,
881
- })
882
-
883
- const valid = new Set(schema.map((c) => c.field))
884
- const numericFields = new Set(schema.filter((c) => c.type === 'number').map((c) => c.field))
885
- const CHART_TYPES: AIChartType[] = ['bar', 'line', 'area', 'pie']
886
- plan.type = CHART_TYPES.includes(plan.type) ? plan.type : 'bar'
887
- plan.dimension = plan.dimension && valid.has(plan.dimension) ? plan.dimension : null
888
- plan.series = plan.series && valid.has(plan.series) && plan.series !== plan.dimension ? plan.series : null
889
- plan.measure =
890
- plan.measure && numericFields.has(plan.measure)
891
- ? plan.measure
892
- : (schema.find((c) => c.type === 'number')?.field ?? null)
893
- plan.reduce = ['sum', 'avg', 'count'].includes(plan.reduce) ? plan.reduce : 'sum'
894
- plan.stacked = plan.stacked === true
895
- plan.logScale = plan.logScale === true
896
- plan.timeAxis = plan.timeAxis === true
897
- plan.valueFormat = ['number', 'currency', 'percent'].includes(plan.valueFormat) ? plan.valueFormat : 'number'
898
- plan.rationale = plan.rationale ?? ''
899
-
900
- if (opts.apply === true) {
901
- const cfg = api as unknown as { configureChart?: (c: Record<string, unknown>) => void }
902
- cfg.configureChart?.({
903
- open: true,
904
- type: plan.type,
905
- dimension: plan.dimension,
906
- series: plan.series,
907
- measure: plan.measure,
908
- reduce: plan.reduce,
909
- stacked: plan.stacked,
910
- logScale: plan.logScale,
911
- timeAxis: plan.timeAxis,
912
- valueFormat: plan.valueFormat,
913
- })
914
- }
915
-
916
- return plan
917
- }
918
-
919
- /**
920
- * Wire the grid's chart-panel AI button to the model: registers a handler (via
921
- * `api.setChartAiHandler`) that runs `aiChart` and returns the plan for the
922
- * panel to apply + explain. Call once after `onApiReady`. `installEnterprise`
923
- * calls this for you.
924
- */
925
- export function enableAiCharting<
926
- TFeatures extends TableFeatures,
927
- TData extends RowData,
928
- >(api: SvGridApi<TFeatures, TData>): void {
929
- const hook = api as unknown as {
930
- setChartAiHandler?: (
931
- fn: ((prompt: string) => Promise<Record<string, unknown> | null>) | null,
932
- ) => void
933
- }
934
- hook.setChartAiHandler?.(async (prompt: string) => {
935
- const plan = await aiChart(api, prompt)
936
- return {
937
- type: plan.type,
938
- dimension: plan.dimension,
939
- series: plan.series,
940
- measure: plan.measure,
941
- reduce: plan.reduce,
942
- stacked: plan.stacked,
943
- logScale: plan.logScale,
944
- timeAxis: plan.timeAxis,
945
- valueFormat: plan.valueFormat,
946
- rationale: plan.rationale,
947
- }
948
- })
949
- }
950
-
951
- export function disableAiCharting<
952
- TFeatures extends TableFeatures,
953
- TData extends RowData,
954
- >(api: SvGridApi<TFeatures, TData>): void {
955
- const hook = api as unknown as { setChartAiHandler?: (fn: null) => void }
956
- hook.setChartAiHandler?.(null)
957
- }
958
-
959
- // ---------------------------------------------------------------------------
960
- // 8. Mock provider for examples + tests
961
- // ---------------------------------------------------------------------------
962
-
963
- /**
964
- * A deterministic provider that returns canned, schema-shaped responses
965
- * for each task. Wire it in via `setAIProvider(mockAIProvider)` to make
966
- * the AI demo work end-to-end without a real model key. Not for production.
967
- */
968
- export const mockAIProvider: AIProvider = async (req) => {
969
- // Tiny artificial delay so the UI's "thinking" state is visible.
970
- await new Promise((r) => setTimeout(r, 350 + Math.random() * 400))
971
-
972
- if (req.task === 'filter') {
973
- const q = extractUserQuery(req.prompt)
974
- return JSON.stringify(buildMockFilter(req.prompt, q))
975
- }
976
- if (req.task === 'smart-fill') {
977
- return JSON.stringify(buildMockSmartFill(req.prompt))
978
- }
979
- if (req.task === 'summarize') {
980
- return JSON.stringify(buildMockSummary(req.prompt))
981
- }
982
- if (req.task === 'classify') {
983
- return JSON.stringify(buildMockClassify(req.prompt))
984
- }
985
- if (req.task === 'export') {
986
- return JSON.stringify(buildMockExport(req.prompt))
987
- }
988
- if (req.task === 'anomaly') {
989
- return JSON.stringify(buildMockAnomaly(req.prompt))
990
- }
991
- if (req.task === 'chart') {
992
- return JSON.stringify(buildMockChart(req.prompt))
993
- }
994
- return '{}'
995
- }
996
-
997
- function buildMockChart(prompt: string): AIChartPlan {
998
- const q = extractUserRequest(prompt)
999
- const typed = extractFieldTypes(prompt)
1000
- const textFields = typed.filter((f) => f.type === 'string' || f.type === 'date').map((f) => f.name)
1001
- const numberFields = typed.filter((f) => f.type === 'number').map((f) => f.name)
1002
-
1003
- const type: AIChartType =
1004
- /\bline\b|\btrend\b|over time/.test(q) ? 'line'
1005
- : /\barea\b/.test(q) ? 'area'
1006
- : /\bpie\b|share|proportion|breakdown/.test(q) ? 'pie'
1007
- : 'bar'
1008
-
1009
- const byMatch = q.match(/\bby\s+([a-z0-9_ ]+?)(?:\s+(?:split|stacked|grouped|by|as|,|$))/)
1010
- const pickText = (hint?: string): string | null => {
1011
- if (hint) {
1012
- const f = textFields.find((t) => t.toLowerCase() === hint.trim() || (hint.trim().length >= 4 && t.toLowerCase().includes(hint.trim())))
1013
- if (f) return f
1014
- }
1015
- return null
1016
- }
1017
- const dimension = pickText(byMatch?.[1]) ?? textFields[0] ?? null
1018
- const series =
1019
- /\b(split|stack(?:ed)?|by product|by category|per)\b/.test(q)
1020
- ? (textFields.find((t) => t !== dimension) ?? null)
1021
- : null
1022
- const measure = numberFields.find((n) => q.includes(n.toLowerCase())) ?? numberFields[0] ?? null
1023
- const reduce: 'sum' | 'avg' | 'count' =
1024
- /\b(average|avg|mean)\b/.test(q) ? 'avg' : /\b(count|number of|how many)\b/.test(q) ? 'count' : 'sum'
1025
- const stacked = /\bstack(ed)?\b/.test(q)
1026
- const logScale = /\b(log|logarithmic)\b/.test(q)
1027
- const timeAxis = /\b(over time|by date|by day|by month|by week|time series|timeline|trend)\b/.test(q)
1028
- const valueFormat: 'number' | 'currency' | 'percent' =
1029
- /\b(currency|money|revenue|sales|price|cost|\$|dollar)\b/.test(q) || /(price|revenue|amount|cost|sales)/i.test(measure ?? '')
1030
- ? 'currency'
1031
- : /\b(percent|percentage|%|rate|share)\b/.test(q)
1032
- ? 'percent'
1033
- : 'number'
1034
-
1035
- return {
1036
- type, dimension, series, measure, reduce, stacked, logScale, timeAxis, valueFormat,
1037
- rationale:
1038
- `${type} chart of ${reduce}(${measure ?? 'value'}) by ${dimension ?? 'category'}` +
1039
- `${series ? `, split by ${series}` : ''}${stacked ? ', stacked' : ''}` +
1040
- `${logScale ? ', log scale' : ''}${timeAxis ? ', date axis' : ''} (mock - wire a real model for genuine parsing).`,
1041
- }
1042
- }
1043
-
1044
- function extractUserRequest(prompt: string): string {
1045
- const m = prompt.match(/User request:\s*([\s\S]+)$/)
1046
- return (m?.[1] ?? '').trim().toLowerCase()
1047
- }
1048
-
1049
- function buildMockExport(prompt: string): AIExportPlan {
1050
- const q = extractUserRequest(prompt)
1051
- const fields = extractFields(prompt)
1052
- // Reuse the filter mock (it reads "User query:"; feed it a compatible line).
1053
- const filterPlan = buildMockFilter(prompt + `\nUser query: ${q}\n`, q)
1054
-
1055
- // Format keyword routing.
1056
- const format: ExportFormat =
1057
- /\bpdf\b/.test(q) ? 'pdf'
1058
- : /\bxls\b(?!x)/.test(q) || /97-2003|legacy excel/.test(q) ? 'xls'
1059
- : /\b(xlsx|excel|spreadsheet|workbook)\b/.test(q) ? 'xlsx'
1060
- : /\b(markdown|md)\b/.test(q) ? 'md'
1061
- : /\bjson\b/.test(q) ? 'json'
1062
- : /\bxml\b/.test(q) ? 'xml'
1063
- : /\btsv\b/.test(q) ? 'tsv'
1064
- : /\bhtml\b/.test(q) ? 'html'
1065
- : /\bcsv\b/.test(q) ? 'csv'
1066
- : 'xlsx'
1067
-
1068
- // Grouping: "group by X" / "grouped by X", else "grouped" -> a categorical field.
1069
- const groupBy: string[] = []
1070
- const byMatch = q.match(/group(?:ed)?\s+by\s+([a-z0-9_ ,]+)/)
1071
- if (byMatch) {
1072
- for (const token of byMatch[1]!.split(/[, ]+/).filter(Boolean)) {
1073
- // Exact field match, or a substring match only for tokens long enough to
1074
- // be meaningful (so "a"/"as" don't match "amount").
1075
- const f = fields.find(
1076
- (ff) => ff.toLowerCase() === token || (token.length >= 4 && ff.toLowerCase().includes(token)),
1077
- )
1078
- if (f && !groupBy.includes(f)) groupBy.push(f)
1079
- }
1080
- } else if (/\bgroup(ed)?\b/.test(q)) {
1081
- const cat = fields.find((f) => /region|country|category|status|stage|type|group|dept|team/i.test(f))
1082
- if (cat) groupBy.push(cat)
1083
- }
1084
-
1085
- return {
1086
- format,
1087
- filters: filterPlan.filters,
1088
- sort: filterPlan.sort,
1089
- groupBy,
1090
- rationale: `Export as ${format}${groupBy.length ? `, grouped by ${groupBy.join(' / ')}` : ''}${filterPlan.filters.length ? `; ${filterPlan.rationale}` : ''}`,
1091
- }
1092
- }
1093
-
1094
- function buildMockAnomaly(prompt: string): AIAnomalyResult {
1095
- // Parse the sampled rows, flag the numeric outliers per numeric field.
1096
- const rowsBlock = prompt.match(/Rows \(index is 0-based into this list\):\n([\s\S]*?)\n\n/)?.[1] ?? ''
1097
- const parsed = rowsBlock
1098
- .split('\n')
1099
- .map((line) => {
1100
- const m = line.match(/^(\d+):\s*(\{[\s\S]*\})$/)
1101
- if (!m) return null
1102
- const row = safeParse(m[2]!) as Record<string, unknown> | null
1103
- return row ? { idx: parseInt(m[1]!, 10), row } : null
1104
- })
1105
- .filter((r): r is { idx: number; row: Record<string, unknown> } => !!r)
1106
-
1107
- if (parsed.length === 0) {
1108
- return { anomalies: [], summary: 'No rows to scan (mock).' }
1109
- }
1110
-
1111
- // Genuinely numeric fields only: every value is null or a real number (so
1112
- // booleans like `inStock` and id-ish strings are excluded). Also skip
1113
- // identifier / row-index columns - a max "id" isn't an anomaly.
1114
- const isIdLike = (f: string) => /(^id$|_id$|index|idx|^#|guid|uuid|serial|rownum)/i.test(f)
1115
- const numericFields = Object.keys(parsed[0]!.row).filter(
1116
- (f) =>
1117
- !isIdLike(f) &&
1118
- parsed.some((p) => typeof p.row[f] === 'number') &&
1119
- parsed.every((p) => p.row[f] == null || typeof p.row[f] === 'number'),
1120
- )
1121
-
1122
- const anomalies: AIAnomaly[] = []
1123
- for (const f of numericFields.slice(0, 2)) {
1124
- let maxIdx = -1
1125
- let maxVal = -Infinity
1126
- for (const p of parsed) {
1127
- const n = Number(p.row[f])
1128
- if (Number.isFinite(n) && n > maxVal) {
1129
- maxVal = n
1130
- maxIdx = p.idx
1131
- }
1132
- }
1133
- if (maxIdx >= 0) {
1134
- anomalies.push({
1135
- rowIndex: maxIdx,
1136
- field: f,
1137
- value: maxVal,
1138
- reason: `${f} = ${maxVal} is the highest in the sample and may be an outlier`,
1139
- severity: 'medium',
1140
- })
1141
- }
1142
- }
1143
-
1144
- return {
1145
- anomalies,
1146
- summary:
1147
- anomalies.length > 0
1148
- ? `Flagged ${anomalies.length} potential outlier${anomalies.length === 1 ? '' : 's'} (mock heuristic - wire a real model for genuine analysis).`
1149
- : 'No obvious anomalies in the sample (mock).',
1150
- }
1151
- }
1152
-
1153
- function extractUserQuery(prompt: string): string {
1154
- const m = prompt.match(/User query:\s*(.+?)\n/)
1155
- return (m?.[1] ?? '').toLowerCase()
1156
- }
1157
-
1158
- function extractFields(prompt: string): string[] {
1159
- const m = prompt.match(/Columns:\n([\s\S]*?)\n\n/)
1160
- if (!m) return []
1161
- return (m[1] ?? '')
1162
- .split('\n')
1163
- .map((l) => l.match(/^- (\w+)/)?.[1])
1164
- .filter((s): s is string => !!s)
1165
- }
1166
-
1167
- /** Parse the "- name (type): ..." schema block into {name, type} pairs. */
1168
- function extractFieldTypes(prompt: string): Array<{ name: string; type: string }> {
1169
- const m = prompt.match(/Columns:\n([\s\S]*?)\n\n/)
1170
- if (!m) return []
1171
- return (m[1] ?? '')
1172
- .split('\n')
1173
- .map((l) => {
1174
- const mm = l.match(/^- (\w+)\s*\((\w+)\)/)
1175
- return mm ? { name: mm[1]!, type: mm[2]! } : null
1176
- })
1177
- .filter((x): x is { name: string; type: string } => !!x)
1178
- }
1179
-
1180
- function buildMockFilter(prompt: string, q: string): AIFilterResult {
1181
- const fields = extractFields(prompt)
1182
- const filters: AIFilterClause[] = []
1183
- const sort: AISortClause[] = []
1184
- const rationale: string[] = []
1185
-
1186
- // Greedy keyword routing. Real model would be smarter; this only needs
1187
- // to convince the demo viewer the pipeline works.
1188
- const numberWord = q.match(/(?:over|above|>|greater than)\s+\$?([\d.,]+)([kmb])?/)
1189
- const lessWord = q.match(/(?:under|below|<|less than)\s+\$?([\d.,]+)([kmb])?/)
1190
- // Pick a NUMBER-typed field (never a boolean like `inStock`), money-named
1191
- // ones first so "$300" targets price/amount rather than a count/index.
1192
- const numberTyped = extractFieldTypes(prompt).filter((f) => f.type === 'number').map((f) => f.name)
1193
- const moneyRe = /(price|amount|cost|revenue|total|value|salary|arr|fee|spend|budget|sales)/i
1194
- const numberPool = numberTyped.length > 0 ? numberTyped : fields
1195
- const numberFields = [
1196
- ...numberPool.filter((f) => moneyRe.test(f)),
1197
- ...numberPool.filter((f) => !moneyRe.test(f)),
1198
- ]
1199
-
1200
- if (numberWord && numberFields[0]) {
1201
- const raw = parseFloat(numberWord[1]!.replace(/,/g, ''))
1202
- const mul = numberWord[2] === 'k' ? 1_000 : numberWord[2] === 'm' ? 1_000_000 : numberWord[2] === 'b' ? 1_000_000_000 : 1
1203
- filters.push({ field: numberFields[0], operator: 'greaterThan', value: String(raw * mul) })
1204
- rationale.push(`amount > ${raw}${numberWord[2] ?? ''}`)
1205
- } else if (lessWord && numberFields[0]) {
1206
- const raw = parseFloat(lessWord[1]!.replace(/,/g, ''))
1207
- const mul = lessWord[2] === 'k' ? 1_000 : lessWord[2] === 'm' ? 1_000_000 : 1
1208
- filters.push({ field: numberFields[0], operator: 'lessThan', value: String(raw * mul) })
1209
- rationale.push(`amount < ${raw}${lessWord[2] ?? ''}`)
1210
- }
1211
-
1212
- // Quoted phrases -> contains on a likely text field.
1213
- const quote = q.match(/"([^"]+)"|'([^']+)'/)
1214
- const textFields = fields.filter((f) => /(name|company|customer|title|owner|status|region|category|industry)/i.test(f))
1215
- if (quote) {
1216
- const term = quote[1] ?? quote[2] ?? ''
1217
- if (textFields[0]) {
1218
- filters.push({ field: textFields[0], operator: 'contains', value: term })
1219
- rationale.push(`text contains "${term}"`)
1220
- }
1221
- }
1222
-
1223
- // Person-name routing -> match owner/contact fields.
1224
- const personOwners = ['sasha', 'jamie', 'casey', 'drew', 'robin', 'morgan', 'riley', 'quinn']
1225
- for (const n of personOwners) {
1226
- if (q.includes(n)) {
1227
- const ownerField = fields.find((f) => /owner|rep|assigned/i.test(f)) ?? fields.find((f) => /name/i.test(f))
1228
- if (ownerField) {
1229
- filters.push({ field: ownerField, operator: 'contains', value: n })
1230
- rationale.push(`owner ~ "${n}"`)
1231
- break
1232
- }
1233
- }
1234
- }
1235
-
1236
- // Region routing.
1237
- for (const r of ['NA', 'EMEA', 'APAC', 'LATAM']) {
1238
- if (q.toUpperCase().includes(r)) {
1239
- const regionField = fields.find((f) => /region/i.test(f))
1240
- if (regionField) {
1241
- filters.push({ field: regionField, operator: 'equals', value: r })
1242
- rationale.push(`region = ${r}`)
1243
- break
1244
- }
1245
- }
1246
- }
1247
-
1248
- // Status routing - any token in the query that matches a known status.
1249
- for (const s of ['active', 'pending', 'won', 'lost', 'live', 'paused', 'shipped', 'delivered']) {
1250
- if (q.includes(s)) {
1251
- const f = fields.find((f) => /status|stage/i.test(f))
1252
- if (f) {
1253
- filters.push({ field: f, operator: 'equals', value: s })
1254
- rationale.push(`status = ${s}`)
1255
- break
1256
- }
1257
- }
1258
- }
1259
-
1260
- // Sort detection.
1261
- if (/(highest|largest|biggest|top|most)/.test(q)) {
1262
- const f = numberFields[0]
1263
- if (f) { sort.push({ field: f, desc: true }); rationale.push(`sort by ${f} desc`) }
1264
- } else if (/(lowest|smallest|least|cheapest)/.test(q)) {
1265
- const f = numberFields[0]
1266
- if (f) { sort.push({ field: f, desc: false }); rationale.push(`sort by ${f} asc`) }
1267
- } else if (/(newest|recent|latest)/.test(q)) {
1268
- const dateField = fields.find((f) => /date|at|time/i.test(f))
1269
- if (dateField) { sort.push({ field: dateField, desc: true }); rationale.push(`sort by ${dateField} desc`) }
1270
- } else if (/(oldest)/.test(q)) {
1271
- const dateField = fields.find((f) => /date|at|time/i.test(f))
1272
- if (dateField) { sort.push({ field: dateField, desc: false }); rationale.push(`sort by ${dateField} asc`) }
1273
- }
1274
-
1275
- return {
1276
- filters,
1277
- sort,
1278
- rationale: rationale.length
1279
- ? `Interpreted: ${rationale.join(' AND ')}.`
1280
- : `Couldn't identify a clear filter for "${q}". Showing all rows.`,
1281
- }
1282
- }
1283
-
1284
- function buildMockSmartFill(prompt: string): { predictions: Array<{ rowIndex: number; value: unknown; confidence: number }>; rationale: string } {
1285
- // Extract the target column and the rows-to-fill block; copy the first
1286
- // example's "output" pattern verbatim so the demo always returns
1287
- // something believable. Real models do much better - that's the point.
1288
- const fieldMatch = prompt.match(/Target column:\s*(\w+)/)
1289
- const rowsMatch = prompt.match(/Rows to fill:\n([\s\S]*?)\n\n/)
1290
- const exMatch = prompt.match(/Example 1: input=.* -> output=(.+)/)
1291
- const targetField = fieldMatch?.[1] ?? ''
1292
- const rowLines = (rowsMatch?.[1] ?? '').split('\n').filter(Boolean)
1293
- const exampleOutput = exMatch?.[1] ? safeParse(exMatch[1]) : ''
1294
- const predictions = rowLines
1295
- .map((line) => {
1296
- const m = line.match(/^Row (\d+): (\{.+\})/)
1297
- if (!m) return null
1298
- const rowIndex = parseInt(m[1]!, 10)
1299
- const row = safeParse(m[2]!) as Record<string, unknown> | null
1300
- if (!row) return null
1301
- const value = inferFromRow(targetField, row, exampleOutput)
1302
- return { rowIndex, value, confidence: 0.78 + Math.random() * 0.18 }
1303
- })
1304
- .filter((p): p is { rowIndex: number; value: unknown; confidence: number } => !!p)
1305
- return {
1306
- predictions,
1307
- rationale: `Inferred from ${exMatch ? 'the supplied' : 'no'} examples; mocked deterministically for the demo.`,
1308
- }
1309
- }
1310
-
1311
- function safeParse(s: string): unknown {
1312
- try { return JSON.parse(s) } catch { return null }
1313
- }
1314
-
1315
- function inferFromRow(field: string, row: Record<string, unknown>, exampleOutput: unknown): unknown {
1316
- // Mock heuristics that produce a value that "looks right" in common
1317
- // demo scenarios. The real provider would route through a real model.
1318
- if (field === 'priority') {
1319
- const arr = Number(row.arr ?? row.amount ?? row.price ?? 0)
1320
- return arr > 100_000 ? 'high' : arr > 30_000 ? 'medium' : 'low'
1321
- }
1322
- if (field === 'tier') {
1323
- const arr = Number(row.arr ?? row.amount ?? 0)
1324
- return arr > 500_000 ? 'enterprise' : arr > 100_000 ? 'growth' : 'starter'
1325
- }
1326
- if (field === 'tags' || field === 'category') {
1327
- return typeof exampleOutput === 'string' ? exampleOutput : 'auto-tag'
1328
- }
1329
- if (typeof exampleOutput === 'string') return exampleOutput
1330
- if (typeof exampleOutput === 'number') return exampleOutput
1331
- return exampleOutput ?? 'auto'
1332
- }
1333
-
1334
- function buildMockSummary(prompt: string): AISummary {
1335
- const sliceMatch = prompt.match(/Slice size: (\d+) row/)
1336
- const n = sliceMatch ? parseInt(sliceMatch[1]!, 10) : 0
1337
- const fields = extractSummaryFields(prompt)
1338
- return {
1339
- text:
1340
- n === 1
1341
- ? `Single record. Notable signals come from ${fields.slice(0, 2).join(' and ') || 'the available fields'}.`
1342
- : `${n} rows in scope. The distribution skews toward live / active records, with a long tail in ${fields[0] ?? 'amount'}.`,
1343
- bullets: [
1344
- `${n} row${n === 1 ? '' : 's'} sampled`,
1345
- fields[0] ? `Largest variance in ${fields[0]}` : 'Tight clustering across visible fields',
1346
- fields[1] ? `${fields[1]} concentrates around the median` : 'Few outliers',
1347
- 'Mock summary - wire setAIProvider() to a real model for actual analysis',
1348
- ],
1349
- highlightedFields: fields.slice(0, 3),
1350
- }
1351
- }
1352
-
1353
- function extractSummaryFields(prompt: string): string[] {
1354
- const m = prompt.match(/Columns:\n([\s\S]*?)\n\n/)
1355
- if (!m) return []
1356
- return (m[1] ?? '')
1357
- .split('\n')
1358
- .map((l) => l.match(/^- (\w+)/)?.[1])
1359
- .filter((s): s is string => !!s)
1360
- }
1361
-
1362
- function buildMockClassify(prompt: string): { predictions: Array<{ rowIndex: number; value: string; confidence: number }> } {
1363
- const labelsMatch = prompt.match(/Allowed labels[\s\S]*?:\n([\s\S]*?)\n\n/)
1364
- // Capture the label up to the first colon, whitespace or end-of-line so
1365
- // we don't pick up the rubric ("- at-risk: churn signals..." -> "at-risk").
1366
- const labels = (labelsMatch?.[1] ?? '')
1367
- .split('\n')
1368
- .map((l) => l.match(/^-\s*([^\s:]+)/)?.[1])
1369
- .filter((s): s is string => !!s)
1370
- const rowLines = (prompt.match(/Rows to classify[\s\S]*?:\n([\s\S]*?)\n\n/)?.[1] ?? '').split('\n').filter(Boolean)
1371
- const predictions = rowLines.map((line) => {
1372
- const m = line.match(/^Row (\d+): "(.*)"/)
1373
- if (!m || labels.length === 0) return null
1374
- const rowIndex = parseInt(m[1]!, 10)
1375
- const text = (m[2] ?? '').toLowerCase()
1376
- // Pick the label whose token appears in the text; otherwise hash.
1377
- const hit = labels.find((l) => text.includes(l.toLowerCase()))
1378
- const value = hit ?? labels[Math.abs(hashStr(text)) % labels.length]!
1379
- return { rowIndex, value, confidence: hit ? 0.92 : 0.6 + Math.random() * 0.2 }
1380
- }).filter((p): p is { rowIndex: number; value: string; confidence: number } => !!p)
1381
- return { predictions }
1382
- }
1383
-
1384
- function hashStr(s: string): number {
1385
- let h = 0
1386
- for (let i = 0; i < s.length; i += 1) h = (h * 31 + s.charCodeAt(i)) | 0
1387
- return h
1388
- }