@svgrid/enterprise 1.0.1

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/src/ai.ts ADDED
@@ -0,0 +1,782 @@
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 `assertProLicensed()`,
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 { assertProLicensed } from './license'
30
+
31
+ // ---------------------------------------------------------------------------
32
+ // Provider contract
33
+ // ---------------------------------------------------------------------------
34
+
35
+ /**
36
+ * Shape every consumer-provided model adapter must implement. Keeping this
37
+ * tiny (one async call, two response formats) lets the same adapter drive
38
+ * OpenAI's `chat.completions`, Anthropic's `messages`, a self-hosted
39
+ * llama.cpp endpoint, or a server-side proxy. The grid only cares that the
40
+ * provider eventually returns a string we can parse.
41
+ */
42
+ export type AIProvider = (request: AIRequest) => Promise<string>
43
+
44
+ export type AIRequest = {
45
+ /** Full prompt the grid built for the model. Already includes column
46
+ * schema and any sampled rows where applicable. */
47
+ prompt: string
48
+ /** When 'json', the provider should ask the model to return strict
49
+ * JSON only - no prose. We parse the response with JSON.parse and
50
+ * throw a typed error on failure. */
51
+ responseFormat?: 'text' | 'json'
52
+ /** Honored if the underlying transport supports cancellation. */
53
+ signal?: AbortSignal
54
+ /** Free-form tag for telemetry / logging. One of: 'filter',
55
+ * 'smart-fill', 'summarize', 'classify'. */
56
+ task: AITask
57
+ /** Soft hint to the provider about how many tokens we expect back.
58
+ * Useful for routing small jobs to a cheaper model. */
59
+ maxOutputTokens?: number
60
+ }
61
+
62
+ export type AITask = 'filter' | 'smart-fill' | 'summarize' | 'classify'
63
+
64
+ let provider: AIProvider | null = null
65
+
66
+ /**
67
+ * Register the model adapter every AI call will route through. Call once
68
+ * at app boot. Passing `null` clears the provider and AI calls revert to
69
+ * throwing "no provider" errors.
70
+ */
71
+ export function setAIProvider(p: AIProvider | null): void {
72
+ provider = p
73
+ }
74
+
75
+ export function getAIProvider(): AIProvider | null {
76
+ return provider
77
+ }
78
+
79
+ export function hasAIProvider(): boolean {
80
+ return provider != null
81
+ }
82
+
83
+ class NoProviderError extends Error {
84
+ constructor() {
85
+ super(
86
+ '@svgrid/enterprise/ai: no AI provider registered. Call setAIProvider(fn) ' +
87
+ 'with an adapter that talks to OpenAI / Anthropic / your proxy.',
88
+ )
89
+ this.name = 'NoProviderError'
90
+ }
91
+ }
92
+
93
+ class BadJsonError extends Error {
94
+ constructor(raw: string) {
95
+ super(
96
+ '@svgrid/enterprise/ai: provider returned non-JSON for a json-format request. ' +
97
+ `First 200 chars: ${raw.slice(0, 200)}`,
98
+ )
99
+ this.name = 'BadJsonError'
100
+ }
101
+ }
102
+
103
+ async function callProvider(req: AIRequest): Promise<string> {
104
+ if (!provider) throw new NoProviderError()
105
+ return provider(req)
106
+ }
107
+
108
+ async function callJSON<T>(req: AIRequest): Promise<T> {
109
+ const raw = await callProvider({ ...req, responseFormat: 'json' })
110
+ // Models occasionally wrap JSON in markdown code fences even when asked
111
+ // for raw JSON. Strip a single fenced block if present, then parse.
112
+ const stripped = raw.trim().replace(/^```(?:json)?\s*|\s*```$/g, '')
113
+ try {
114
+ return JSON.parse(stripped) as T
115
+ } catch {
116
+ throw new BadJsonError(raw)
117
+ }
118
+ }
119
+
120
+ // ---------------------------------------------------------------------------
121
+ // Helpers - build column schema + row sample from the grid API
122
+ // ---------------------------------------------------------------------------
123
+
124
+ type ColumnSchemaEntry = {
125
+ field: string
126
+ type: 'string' | 'number' | 'boolean' | 'date' | 'unknown'
127
+ sampleValues: unknown[]
128
+ }
129
+
130
+ /**
131
+ * Inspect the first N rows the grid is holding to infer a typed schema +
132
+ * a handful of sample values per column. This is the context every AI
133
+ * task needs and the most expensive thing to build, so the helpers cache
134
+ * the result inside one task call rather than calling api.getData()
135
+ * repeatedly.
136
+ */
137
+ function buildColumnSchema<
138
+ TFeatures extends TableFeatures,
139
+ TData extends RowData,
140
+ >(api: SvGridApi<TFeatures, TData>, sampleSize = 50): ColumnSchemaEntry[] {
141
+ const data = api.getData()
142
+ if (data.length === 0) return []
143
+ const slice = data.slice(0, Math.min(data.length, sampleSize))
144
+ const fields = new Set<string>()
145
+ for (const row of slice) {
146
+ for (const k of Object.keys(row as object)) fields.add(k)
147
+ }
148
+ const out: ColumnSchemaEntry[] = []
149
+ for (const field of fields) {
150
+ const values: unknown[] = []
151
+ for (const row of slice) {
152
+ const v = (row as Record<string, unknown>)[field]
153
+ if (v != null) values.push(v)
154
+ if (values.length >= 5) break
155
+ }
156
+ out.push({
157
+ field,
158
+ type: inferType(values),
159
+ sampleValues: values,
160
+ })
161
+ }
162
+ return out
163
+ }
164
+
165
+ function inferType(samples: unknown[]): ColumnSchemaEntry['type'] {
166
+ if (samples.length === 0) return 'unknown'
167
+ const v = samples[0]
168
+ if (typeof v === 'number') return 'number'
169
+ if (typeof v === 'boolean') return 'boolean'
170
+ if (typeof v === 'string') {
171
+ // Cheap ISO-date sniff so we route date-style filters correctly.
172
+ if (/^\d{4}-\d{2}-\d{2}/.test(v)) return 'date'
173
+ return 'string'
174
+ }
175
+ return 'unknown'
176
+ }
177
+
178
+ function schemaToPromptBlock(schema: ColumnSchemaEntry[]): string {
179
+ return schema
180
+ .map(
181
+ (c) =>
182
+ `- ${c.field} (${c.type}): e.g. ${c.sampleValues
183
+ .slice(0, 3)
184
+ .map((v) => JSON.stringify(v))
185
+ .join(', ')}`,
186
+ )
187
+ .join('\n')
188
+ }
189
+
190
+ // ---------------------------------------------------------------------------
191
+ // 1. Natural-language filter / sort
192
+ // ---------------------------------------------------------------------------
193
+
194
+ export type AIFilterClause = {
195
+ field: string
196
+ operator: 'contains' | 'equals' | 'startsWith' | 'greaterThan' | 'lessThan' | 'isBlank'
197
+ value?: string
198
+ }
199
+ export type AISortClause = { field: string; desc: boolean }
200
+
201
+ export type AIFilterResult = {
202
+ filters: AIFilterClause[]
203
+ sort: AISortClause[]
204
+ /** Plain-English explanation of how the model interpreted the query.
205
+ * Surface this in the UI so the user can confirm or undo. */
206
+ rationale: string
207
+ }
208
+
209
+ export type AIFilterOptions = {
210
+ /**
211
+ * When true, the helper not only RETURNS the plan but also applies it
212
+ * to the grid via `api.setFilter` / `api.setSort`. Defaults to false
213
+ * so callers can show a preview before committing.
214
+ */
215
+ apply?: boolean
216
+ signal?: AbortSignal
217
+ }
218
+
219
+ /**
220
+ * Translate a natural-language query into a filter+sort plan against
221
+ * the current grid's columns. The grid passes the column schema (names,
222
+ * types, sample values) to the model so it can pick the right field
223
+ * names without hallucinating.
224
+ */
225
+ export async function aiFilter<
226
+ TFeatures extends TableFeatures,
227
+ TData extends RowData,
228
+ >(
229
+ api: SvGridApi<TFeatures, TData>,
230
+ query: string,
231
+ opts: AIFilterOptions = {},
232
+ ): Promise<AIFilterResult> {
233
+ assertProLicensed('AI assistant')
234
+ const schema = buildColumnSchema(api)
235
+ const prompt =
236
+ `You are a data-grid filter planner. Translate the user's natural-language ` +
237
+ `query into a strict-JSON plan that the grid can apply.\n\n` +
238
+ `Columns:\n${schemaToPromptBlock(schema)}\n\n` +
239
+ `Output JSON schema:\n` +
240
+ `{ "filters": [{"field": "<one of the columns above>", ` +
241
+ `"operator": "contains"|"equals"|"startsWith"|"greaterThan"|"lessThan"|"isBlank", ` +
242
+ `"value": "<string>"}], ` +
243
+ `"sort": [{"field": "<column>", "desc": true|false}], ` +
244
+ `"rationale": "<one-sentence explanation>" }\n\n` +
245
+ `User query: ${query}\n\n` +
246
+ `Return JSON only, no prose.`
247
+
248
+ const result = await callJSON<AIFilterResult>({
249
+ prompt,
250
+ task: 'filter',
251
+ signal: opts.signal,
252
+ maxOutputTokens: 400,
253
+ })
254
+
255
+ // Defensive: drop anything that doesn't match a real column. Models love
256
+ // to invent plausible-sounding field names; we'd rather skip a clause
257
+ // than throw at runtime.
258
+ const valid = new Set(schema.map((c) => c.field))
259
+ result.filters = (result.filters ?? []).filter((f) => valid.has(f.field))
260
+ result.sort = (result.sort ?? []).filter((s) => valid.has(s.field))
261
+
262
+ if (opts.apply) {
263
+ api.clearAllFilters()
264
+ api.clearSort()
265
+ for (const f of result.filters) {
266
+ api.setFilter(f.field, { operator: f.operator, value: f.value ?? '' })
267
+ }
268
+ // Multiple sort clauses get applied last-wins because the public
269
+ // `setSort` is single-key. Multi-sort is on the roadmap.
270
+ const last = result.sort[result.sort.length - 1]
271
+ if (last) api.setSort(last.field, last.desc ? 'desc' : 'asc')
272
+ }
273
+
274
+ return result
275
+ }
276
+
277
+ // ---------------------------------------------------------------------------
278
+ // 2. Smart fill
279
+ // ---------------------------------------------------------------------------
280
+
281
+ export type AISmartFillExample = { input: Record<string, unknown>; output: unknown }
282
+
283
+ export type AISmartFillResult<TValue = unknown> = {
284
+ field: string
285
+ predictions: Array<{ rowIndex: number; value: TValue; confidence: number }>
286
+ rationale: string
287
+ }
288
+
289
+ export type AISmartFillOptions = {
290
+ /** Target column - the one whose values we want filled. */
291
+ field: string
292
+ /** Index of rows the model should propose values for. If omitted, every
293
+ * row whose current `field` value is `null`, `undefined` or `''` is
294
+ * selected automatically. */
295
+ targetRowIndices?: number[]
296
+ /**
297
+ * Worked examples the user has already filled in. Required - the
298
+ * model needs at least one to know the pattern, two or more to lock
299
+ * the schema. We don't pull these from the grid automatically because
300
+ * "edited" vs "untouched" isn't a state SvGrid exposes today.
301
+ */
302
+ examples: AISmartFillExample[]
303
+ signal?: AbortSignal
304
+ }
305
+
306
+ /**
307
+ * Given a column and a few user-provided examples, propose values for the
308
+ * untouched rows. Returns predictions only - the caller chooses whether to
309
+ * commit them via `api.setCellValue`. This is the right shape for an
310
+ * accept-per-cell UX with confidence-coloured highlights.
311
+ */
312
+ export async function aiSmartFill<
313
+ TFeatures extends TableFeatures,
314
+ TData extends RowData,
315
+ TValue = unknown,
316
+ >(
317
+ api: SvGridApi<TFeatures, TData>,
318
+ opts: AISmartFillOptions,
319
+ ): Promise<AISmartFillResult<TValue>> {
320
+ assertProLicensed('AI assistant')
321
+ if (opts.examples.length === 0) {
322
+ throw new Error('@svgrid/enterprise/ai: aiSmartFill requires at least one example.')
323
+ }
324
+ const data = api.getData()
325
+ const targets =
326
+ opts.targetRowIndices ??
327
+ data
328
+ .map((row, i) => {
329
+ const v = (row as Record<string, unknown>)[opts.field]
330
+ return v == null || v === '' ? i : -1
331
+ })
332
+ .filter((i) => i >= 0)
333
+
334
+ if (targets.length === 0) {
335
+ return { field: opts.field, predictions: [], rationale: 'No empty cells to fill.' }
336
+ }
337
+
338
+ const examplesBlock = opts.examples
339
+ .map((ex, i) => `Example ${i + 1}: input=${JSON.stringify(ex.input)} -> output=${JSON.stringify(ex.output)}`)
340
+ .join('\n')
341
+
342
+ const targetRowsBlock = targets
343
+ .map((i) => `Row ${i}: ${JSON.stringify(data[i])}`)
344
+ .join('\n')
345
+
346
+ const prompt =
347
+ `You are filling in a single column of a data grid based on user-provided ` +
348
+ `examples. Match the pattern in the examples exactly.\n\n` +
349
+ `Target column: ${opts.field}\n\n` +
350
+ `Examples:\n${examplesBlock}\n\n` +
351
+ `Rows to fill:\n${targetRowsBlock}\n\n` +
352
+ `Return strict JSON only:\n` +
353
+ `{ "predictions": [{"rowIndex": <number>, "value": <inferred value>, ` +
354
+ `"confidence": <0..1>}], "rationale": "<one-sentence pattern description>" }`
355
+
356
+ const result = await callJSON<{
357
+ predictions: Array<{ rowIndex: number; value: TValue; confidence: number }>
358
+ rationale: string
359
+ }>({
360
+ prompt,
361
+ task: 'smart-fill',
362
+ signal: opts.signal,
363
+ maxOutputTokens: 600,
364
+ })
365
+
366
+ return {
367
+ field: opts.field,
368
+ predictions: result.predictions ?? [],
369
+ rationale: result.rationale ?? '',
370
+ }
371
+ }
372
+
373
+ // ---------------------------------------------------------------------------
374
+ // 3. Summarise
375
+ // ---------------------------------------------------------------------------
376
+
377
+ export type AISummarizeTarget =
378
+ | { kind: 'row'; rowIndex: number }
379
+ | { kind: 'all' }
380
+ | { kind: 'selection'; rowIndices: number[] }
381
+ | { kind: 'group'; field: string; value: unknown }
382
+
383
+ export type AISummary = {
384
+ text: string
385
+ bullets: string[]
386
+ /** Field names the model thinks are the most load-bearing for the
387
+ * story it just told. UI can highlight those columns. */
388
+ highlightedFields: string[]
389
+ }
390
+
391
+ export type AISummarizeOptions = {
392
+ target: AISummarizeTarget
393
+ /** Optional question the user is trying to answer. Helps the model
394
+ * bias the summary toward the relevant columns. */
395
+ question?: string
396
+ signal?: AbortSignal
397
+ }
398
+
399
+ /**
400
+ * Ask the model to summarise a slice of the grid. Caps the row sample at
401
+ * a model-friendly size; for huge selections it samples uniformly so the
402
+ * summary stays representative without blowing the context window.
403
+ */
404
+ export async function aiSummarize<
405
+ TFeatures extends TableFeatures,
406
+ TData extends RowData,
407
+ >(
408
+ api: SvGridApi<TFeatures, TData>,
409
+ opts: AISummarizeOptions,
410
+ ): Promise<AISummary> {
411
+ assertProLicensed('AI assistant')
412
+ const all = api.getData()
413
+ const rows: TData[] = (() => {
414
+ const t = opts.target
415
+ if (t.kind === 'row') {
416
+ const r = all[t.rowIndex]
417
+ return r ? [r] : []
418
+ }
419
+ if (t.kind === 'selection') {
420
+ return t.rowIndices.map((i) => all[i]).filter((r): r is TData => r != null)
421
+ }
422
+ if (t.kind === 'group') {
423
+ return all.filter((r) => (r as Record<string, unknown>)[t.field] === t.value)
424
+ }
425
+ return all.slice()
426
+ })()
427
+
428
+ if (rows.length === 0) {
429
+ return { text: 'No rows in scope.', bullets: [], highlightedFields: [] }
430
+ }
431
+
432
+ // Uniform sample to stay inside a sensible context budget.
433
+ const MAX_SAMPLE = 25
434
+ const sample = rows.length <= MAX_SAMPLE
435
+ ? rows
436
+ : Array.from({ length: MAX_SAMPLE }, (_, i) => rows[Math.floor((i / MAX_SAMPLE) * rows.length)]!)
437
+
438
+ const schema = buildColumnSchema(api, 30)
439
+ const prompt =
440
+ `You are summarising a slice of a business data grid for a busy analyst.\n\n` +
441
+ `Columns:\n${schemaToPromptBlock(schema)}\n\n` +
442
+ `Slice size: ${rows.length} row(s). Sampled rows shown below.\n` +
443
+ `${sample.map((r) => JSON.stringify(r)).join('\n')}\n\n` +
444
+ (opts.question ? `User's question: ${opts.question}\n\n` : '') +
445
+ `Return strict JSON only:\n` +
446
+ `{ "text": "<one short paragraph>", ` +
447
+ `"bullets": ["<3-5 punchy bullets>"], ` +
448
+ `"highlightedFields": ["<column names the summary leans on>"] }`
449
+
450
+ const result = await callJSON<AISummary>({
451
+ prompt,
452
+ task: 'summarize',
453
+ signal: opts.signal,
454
+ maxOutputTokens: 600,
455
+ })
456
+ return {
457
+ text: result.text ?? '',
458
+ bullets: result.bullets ?? [],
459
+ highlightedFields: result.highlightedFields ?? [],
460
+ }
461
+ }
462
+
463
+ // ---------------------------------------------------------------------------
464
+ // 4. Classify (free-text -> bucketed value)
465
+ // ---------------------------------------------------------------------------
466
+
467
+ export type AIClassifyOptions = {
468
+ /** Column whose free-text we're classifying. */
469
+ inputField: string
470
+ /** Target column the model should write to. */
471
+ outputField: string
472
+ /** Allowed values. The model is constrained to pick one. */
473
+ classes: string[]
474
+ /** Optional one-line description of each class (acts as a labeling rubric). */
475
+ classDescriptions?: Record<string, string>
476
+ /** Rows to classify. Defaults to all. */
477
+ targetRowIndices?: number[]
478
+ signal?: AbortSignal
479
+ }
480
+
481
+ export type AIClassifyResult = {
482
+ inputField: string
483
+ outputField: string
484
+ predictions: Array<{ rowIndex: number; value: string; confidence: number }>
485
+ }
486
+
487
+ /**
488
+ * Bucket free-text cells into one of a known set of classes. The model
489
+ * is constrained to pick from `opts.classes`; predictions outside the set
490
+ * are dropped so the caller can rely on the output being clean enum
491
+ * values it can write straight back into the grid.
492
+ */
493
+ export async function aiClassify<
494
+ TFeatures extends TableFeatures,
495
+ TData extends RowData,
496
+ >(
497
+ api: SvGridApi<TFeatures, TData>,
498
+ opts: AIClassifyOptions,
499
+ ): Promise<AIClassifyResult> {
500
+ assertProLicensed('AI assistant')
501
+ const data = api.getData()
502
+ const targets = opts.targetRowIndices ?? data.map((_, i) => i)
503
+ const rubric = opts.classDescriptions
504
+ ? Object.entries(opts.classDescriptions)
505
+ .map(([k, v]) => `- ${k}: ${v}`)
506
+ .join('\n')
507
+ : opts.classes.map((c) => `- ${c}`).join('\n')
508
+
509
+ const rowsBlock = targets
510
+ .map((i) => `Row ${i}: ${JSON.stringify((data[i] as Record<string, unknown>)?.[opts.inputField] ?? '')}`)
511
+ .join('\n')
512
+
513
+ const prompt =
514
+ `You are a strict text classifier. For each row, pick exactly one ` +
515
+ `label from the list. If unsure, pick the closest match and lower the ` +
516
+ `confidence.\n\n` +
517
+ `Allowed labels (with optional rubric):\n${rubric}\n\n` +
518
+ `Rows to classify (text from column "${opts.inputField}"):\n${rowsBlock}\n\n` +
519
+ `Return strict JSON only:\n` +
520
+ `{ "predictions": [{"rowIndex": <number>, "value": "<label>", "confidence": <0..1>}] }`
521
+
522
+ const result = await callJSON<{
523
+ predictions: Array<{ rowIndex: number; value: string; confidence: number }>
524
+ }>({
525
+ prompt,
526
+ task: 'classify',
527
+ signal: opts.signal,
528
+ maxOutputTokens: Math.min(2000, 30 + targets.length * 30),
529
+ })
530
+
531
+ const allowed = new Set(opts.classes)
532
+ return {
533
+ inputField: opts.inputField,
534
+ outputField: opts.outputField,
535
+ predictions: (result.predictions ?? []).filter((p) => allowed.has(p.value)),
536
+ }
537
+ }
538
+
539
+ // ---------------------------------------------------------------------------
540
+ // 5. Mock provider for examples + tests
541
+ // ---------------------------------------------------------------------------
542
+
543
+ /**
544
+ * A deterministic provider that returns canned, schema-shaped responses
545
+ * for each task. Wire it in via `setAIProvider(mockAIProvider)` to make
546
+ * the AI demo work end-to-end without a real model key. Not for production.
547
+ */
548
+ export const mockAIProvider: AIProvider = async (req) => {
549
+ // Tiny artificial delay so the UI's "thinking" state is visible.
550
+ await new Promise((r) => setTimeout(r, 350 + Math.random() * 400))
551
+
552
+ if (req.task === 'filter') {
553
+ const q = extractUserQuery(req.prompt)
554
+ return JSON.stringify(buildMockFilter(req.prompt, q))
555
+ }
556
+ if (req.task === 'smart-fill') {
557
+ return JSON.stringify(buildMockSmartFill(req.prompt))
558
+ }
559
+ if (req.task === 'summarize') {
560
+ return JSON.stringify(buildMockSummary(req.prompt))
561
+ }
562
+ if (req.task === 'classify') {
563
+ return JSON.stringify(buildMockClassify(req.prompt))
564
+ }
565
+ return '{}'
566
+ }
567
+
568
+ function extractUserQuery(prompt: string): string {
569
+ const m = prompt.match(/User query:\s*(.+?)\n/)
570
+ return (m?.[1] ?? '').toLowerCase()
571
+ }
572
+
573
+ function extractFields(prompt: string): string[] {
574
+ const m = prompt.match(/Columns:\n([\s\S]*?)\n\n/)
575
+ if (!m) return []
576
+ return (m[1] ?? '')
577
+ .split('\n')
578
+ .map((l) => l.match(/^- (\w+)/)?.[1])
579
+ .filter((s): s is string => !!s)
580
+ }
581
+
582
+ function buildMockFilter(prompt: string, q: string): AIFilterResult {
583
+ const fields = extractFields(prompt)
584
+ const filters: AIFilterClause[] = []
585
+ const sort: AISortClause[] = []
586
+ const rationale: string[] = []
587
+
588
+ // Greedy keyword routing. Real model would be smarter; this only needs
589
+ // to convince the demo viewer the pipeline works.
590
+ const numberWord = q.match(/(?:over|above|>|greater than)\s+\$?([\d.,]+)([kmb])?/)
591
+ const lessWord = q.match(/(?:under|below|<|less than)\s+\$?([\d.,]+)([kmb])?/)
592
+ const numberFields = fields.filter((f) => /(arr|amount|price|stock|value|qty|sold|cost|revenue|probability|fillpct)/i.test(f))
593
+
594
+ if (numberWord && numberFields[0]) {
595
+ const raw = parseFloat(numberWord[1]!.replace(/,/g, ''))
596
+ const mul = numberWord[2] === 'k' ? 1_000 : numberWord[2] === 'm' ? 1_000_000 : numberWord[2] === 'b' ? 1_000_000_000 : 1
597
+ filters.push({ field: numberFields[0], operator: 'greaterThan', value: String(raw * mul) })
598
+ rationale.push(`amount > ${raw}${numberWord[2] ?? ''}`)
599
+ } else if (lessWord && numberFields[0]) {
600
+ const raw = parseFloat(lessWord[1]!.replace(/,/g, ''))
601
+ const mul = lessWord[2] === 'k' ? 1_000 : lessWord[2] === 'm' ? 1_000_000 : 1
602
+ filters.push({ field: numberFields[0], operator: 'lessThan', value: String(raw * mul) })
603
+ rationale.push(`amount < ${raw}${lessWord[2] ?? ''}`)
604
+ }
605
+
606
+ // Quoted phrases -> contains on a likely text field.
607
+ const quote = q.match(/"([^"]+)"|'([^']+)'/)
608
+ const textFields = fields.filter((f) => /(name|company|customer|title|owner|status|region|category|industry)/i.test(f))
609
+ if (quote) {
610
+ const term = quote[1] ?? quote[2] ?? ''
611
+ if (textFields[0]) {
612
+ filters.push({ field: textFields[0], operator: 'contains', value: term })
613
+ rationale.push(`text contains "${term}"`)
614
+ }
615
+ }
616
+
617
+ // Person-name routing -> match owner/contact fields.
618
+ const personOwners = ['sasha', 'jamie', 'casey', 'drew', 'robin', 'morgan', 'riley', 'quinn']
619
+ for (const n of personOwners) {
620
+ if (q.includes(n)) {
621
+ const ownerField = fields.find((f) => /owner|rep|assigned/i.test(f)) ?? fields.find((f) => /name/i.test(f))
622
+ if (ownerField) {
623
+ filters.push({ field: ownerField, operator: 'contains', value: n })
624
+ rationale.push(`owner ~ "${n}"`)
625
+ break
626
+ }
627
+ }
628
+ }
629
+
630
+ // Region routing.
631
+ for (const r of ['NA', 'EMEA', 'APAC', 'LATAM']) {
632
+ if (q.toUpperCase().includes(r)) {
633
+ const regionField = fields.find((f) => /region/i.test(f))
634
+ if (regionField) {
635
+ filters.push({ field: regionField, operator: 'equals', value: r })
636
+ rationale.push(`region = ${r}`)
637
+ break
638
+ }
639
+ }
640
+ }
641
+
642
+ // Status routing - any token in the query that matches a known status.
643
+ for (const s of ['active', 'pending', 'won', 'lost', 'live', 'paused', 'shipped', 'delivered']) {
644
+ if (q.includes(s)) {
645
+ const f = fields.find((f) => /status|stage/i.test(f))
646
+ if (f) {
647
+ filters.push({ field: f, operator: 'equals', value: s })
648
+ rationale.push(`status = ${s}`)
649
+ break
650
+ }
651
+ }
652
+ }
653
+
654
+ // Sort detection.
655
+ if (/(highest|largest|biggest|top|most)/.test(q)) {
656
+ const f = numberFields[0]
657
+ if (f) { sort.push({ field: f, desc: true }); rationale.push(`sort by ${f} desc`) }
658
+ } else if (/(lowest|smallest|least|cheapest)/.test(q)) {
659
+ const f = numberFields[0]
660
+ if (f) { sort.push({ field: f, desc: false }); rationale.push(`sort by ${f} asc`) }
661
+ } else if (/(newest|recent|latest)/.test(q)) {
662
+ const dateField = fields.find((f) => /date|at|time/i.test(f))
663
+ if (dateField) { sort.push({ field: dateField, desc: true }); rationale.push(`sort by ${dateField} desc`) }
664
+ } else if (/(oldest)/.test(q)) {
665
+ const dateField = fields.find((f) => /date|at|time/i.test(f))
666
+ if (dateField) { sort.push({ field: dateField, desc: false }); rationale.push(`sort by ${dateField} asc`) }
667
+ }
668
+
669
+ return {
670
+ filters,
671
+ sort,
672
+ rationale: rationale.length
673
+ ? `Interpreted: ${rationale.join(' AND ')}.`
674
+ : `Couldn't identify a clear filter for "${q}". Showing all rows.`,
675
+ }
676
+ }
677
+
678
+ function buildMockSmartFill(prompt: string): { predictions: Array<{ rowIndex: number; value: unknown; confidence: number }>; rationale: string } {
679
+ // Extract the target column and the rows-to-fill block; copy the first
680
+ // example's "output" pattern verbatim so the demo always returns
681
+ // something believable. Real models do much better - that's the point.
682
+ const fieldMatch = prompt.match(/Target column:\s*(\w+)/)
683
+ const rowsMatch = prompt.match(/Rows to fill:\n([\s\S]*?)\n\n/)
684
+ const exMatch = prompt.match(/Example 1: input=.* -> output=(.+)/)
685
+ const targetField = fieldMatch?.[1] ?? ''
686
+ const rowLines = (rowsMatch?.[1] ?? '').split('\n').filter(Boolean)
687
+ const exampleOutput = exMatch?.[1] ? safeParse(exMatch[1]) : ''
688
+ const predictions = rowLines
689
+ .map((line) => {
690
+ const m = line.match(/^Row (\d+): (\{.+\})/)
691
+ if (!m) return null
692
+ const rowIndex = parseInt(m[1]!, 10)
693
+ const row = safeParse(m[2]!) as Record<string, unknown> | null
694
+ if (!row) return null
695
+ const value = inferFromRow(targetField, row, exampleOutput)
696
+ return { rowIndex, value, confidence: 0.78 + Math.random() * 0.18 }
697
+ })
698
+ .filter((p): p is { rowIndex: number; value: unknown; confidence: number } => !!p)
699
+ return {
700
+ predictions,
701
+ rationale: `Inferred from ${exMatch ? 'the supplied' : 'no'} examples; mocked deterministically for the demo.`,
702
+ }
703
+ }
704
+
705
+ function safeParse(s: string): unknown {
706
+ try { return JSON.parse(s) } catch { return null }
707
+ }
708
+
709
+ function inferFromRow(field: string, row: Record<string, unknown>, exampleOutput: unknown): unknown {
710
+ // Mock heuristics that produce a value that "looks right" in common
711
+ // demo scenarios. The real provider would route through a real model.
712
+ if (field === 'priority') {
713
+ const arr = Number(row.arr ?? row.amount ?? row.price ?? 0)
714
+ return arr > 100_000 ? 'high' : arr > 30_000 ? 'medium' : 'low'
715
+ }
716
+ if (field === 'tier') {
717
+ const arr = Number(row.arr ?? row.amount ?? 0)
718
+ return arr > 500_000 ? 'enterprise' : arr > 100_000 ? 'growth' : 'starter'
719
+ }
720
+ if (field === 'tags' || field === 'category') {
721
+ return typeof exampleOutput === 'string' ? exampleOutput : 'auto-tag'
722
+ }
723
+ if (typeof exampleOutput === 'string') return exampleOutput
724
+ if (typeof exampleOutput === 'number') return exampleOutput
725
+ return exampleOutput ?? 'auto'
726
+ }
727
+
728
+ function buildMockSummary(prompt: string): AISummary {
729
+ const sliceMatch = prompt.match(/Slice size: (\d+) row/)
730
+ const n = sliceMatch ? parseInt(sliceMatch[1]!, 10) : 0
731
+ const fields = extractSummaryFields(prompt)
732
+ return {
733
+ text:
734
+ n === 1
735
+ ? `Single record. Notable signals come from ${fields.slice(0, 2).join(' and ') || 'the available fields'}.`
736
+ : `${n} rows in scope. The distribution skews toward live / active records, with a long tail in ${fields[0] ?? 'amount'}.`,
737
+ bullets: [
738
+ `${n} row${n === 1 ? '' : 's'} sampled`,
739
+ fields[0] ? `Largest variance in ${fields[0]}` : 'Tight clustering across visible fields',
740
+ fields[1] ? `${fields[1]} concentrates around the median` : 'Few outliers',
741
+ 'Mock summary - wire setAIProvider() to a real model for actual analysis',
742
+ ],
743
+ highlightedFields: fields.slice(0, 3),
744
+ }
745
+ }
746
+
747
+ function extractSummaryFields(prompt: string): string[] {
748
+ const m = prompt.match(/Columns:\n([\s\S]*?)\n\n/)
749
+ if (!m) return []
750
+ return (m[1] ?? '')
751
+ .split('\n')
752
+ .map((l) => l.match(/^- (\w+)/)?.[1])
753
+ .filter((s): s is string => !!s)
754
+ }
755
+
756
+ function buildMockClassify(prompt: string): { predictions: Array<{ rowIndex: number; value: string; confidence: number }> } {
757
+ const labelsMatch = prompt.match(/Allowed labels[\s\S]*?:\n([\s\S]*?)\n\n/)
758
+ // Capture the label up to the first colon, whitespace or end-of-line so
759
+ // we don't pick up the rubric ("- at-risk: churn signals..." -> "at-risk").
760
+ const labels = (labelsMatch?.[1] ?? '')
761
+ .split('\n')
762
+ .map((l) => l.match(/^-\s*([^\s:]+)/)?.[1])
763
+ .filter((s): s is string => !!s)
764
+ const rowLines = (prompt.match(/Rows to classify[\s\S]*?:\n([\s\S]*?)\n\n/)?.[1] ?? '').split('\n').filter(Boolean)
765
+ const predictions = rowLines.map((line) => {
766
+ const m = line.match(/^Row (\d+): "(.*)"/)
767
+ if (!m || labels.length === 0) return null
768
+ const rowIndex = parseInt(m[1]!, 10)
769
+ const text = (m[2] ?? '').toLowerCase()
770
+ // Pick the label whose token appears in the text; otherwise hash.
771
+ const hit = labels.find((l) => text.includes(l.toLowerCase()))
772
+ const value = hit ?? labels[Math.abs(hashStr(text)) % labels.length]!
773
+ return { rowIndex, value, confidence: hit ? 0.92 : 0.6 + Math.random() * 0.2 }
774
+ }).filter((p): p is { rowIndex: number; value: string; confidence: number } => !!p)
775
+ return { predictions }
776
+ }
777
+
778
+ function hashStr(s: string): number {
779
+ let h = 0
780
+ for (let i = 0; i < s.length; i += 1) h = (h * 31 + s.charCodeAt(i)) | 0
781
+ return h
782
+ }