@superatomai/sdk-node 0.0.42-mds → 0.0.43-dsp

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -1,15 +1,964 @@
1
1
  import { z } from 'zod';
2
2
  import Anthropic from '@anthropic-ai/sdk';
3
3
 
4
+ /**
5
+ * What getAnalysis returns. `analysis` is the narrative and `summary` the short
6
+ * bullets the markdown block binds to — siblings over one dataset, never a
7
+ * summary OF the narrative. See backend/docs/ANALYSIS-AS-PROGRAM-DESIGN.md § 4.3.
8
+ */
9
+ interface AnalysisOutput {
10
+ analysis?: string;
11
+ summary?: string[];
12
+ method?: string;
13
+ }
14
+
15
+ /**
16
+ * StreamBuffer - Buffered streaming utility for smoother text delivery
17
+ * Batches small chunks together and flushes at regular intervals
18
+ */
19
+ type StreamCallback = (chunk: string) => void;
20
+ /**
21
+ * StreamBuffer class for managing buffered streaming output
22
+ * Provides smooth text delivery by batching small chunks
23
+ */
24
+ declare class StreamBuffer {
25
+ private buffer;
26
+ private flushTimer;
27
+ private callback;
28
+ private fullText;
29
+ constructor(callback?: StreamCallback);
30
+ /**
31
+ * Check if the buffer has a callback configured
32
+ */
33
+ hasCallback(): boolean;
34
+ /**
35
+ * Get all text that has been written (including already flushed)
36
+ */
37
+ getFullText(): string;
38
+ /**
39
+ * Write a chunk to the buffer
40
+ * Large chunks or chunks with newlines are flushed immediately
41
+ * Small chunks are batched and flushed after a short interval
42
+ *
43
+ * @param chunk - Text chunk to write
44
+ */
45
+ write(chunk: string): void;
46
+ /**
47
+ * Flush the buffer immediately
48
+ * Call this before tool execution or other operations that need clean output
49
+ */
50
+ flush(): void;
51
+ /**
52
+ * Internal flush implementation
53
+ */
54
+ private flushNow;
55
+ /**
56
+ * Clean up resources
57
+ * Call this when done with the buffer
58
+ */
59
+ dispose(): void;
60
+ }
61
+
62
+ /**
63
+ * ToolExecutorService - Handles execution of SQL queries and external tools
64
+ * Extracted from BaseLLM.generateTextResponse for better separation of concerns
65
+ */
66
+
67
+ /**
68
+ * External tool definition
69
+ */
70
+ interface ExternalTool {
71
+ id: string;
72
+ name: string;
73
+ description?: string;
74
+ /** Tool type: "source" = routed through SourceAgent, "direct" = called directly by MainAgent */
75
+ toolType?: 'source' | 'direct';
76
+ /** Full untruncated schema for source agent (all columns visible) */
77
+ fullSchema?: string;
78
+ /** Schema size tier: small (≤50 tables), medium (51-200), large (201-500), very_large (500+) */
79
+ schemaTier?: string;
80
+ /** Schema search function for very_large tier — keyword search over entities */
81
+ schemaSearchFn?: (keywords: string[]) => string;
82
+ /** `signal` fires when the user hits Stop mid-query — passed through to the backend's SourceTool.fn. */
83
+ fn: (input: any, signal?: AbortSignal) => Promise<any>;
84
+ limit?: number;
85
+ outputSchema?: any;
86
+ executionType?: 'immediate' | 'deferred';
87
+ userProvidedData?: any;
88
+ params?: Record<string, any>;
89
+ }
90
+ /**
91
+ * Executed tool tracking info
92
+ */
93
+ interface ExecutedToolInfo {
94
+ id: string;
95
+ name: string;
96
+ params: any;
97
+ result: {
98
+ _totalRecords: number;
99
+ _recordsShown: number;
100
+ _metadata?: any;
101
+ _sampleData: any[];
102
+ /** Bounded summary over the FULL fetched result (complete structure). */
103
+ _summary?: any;
104
+ /** Up to MAIN_AGENT_COMPLETE_ROWS rows — the complete result when small. */
105
+ _mainAgentRows?: any[];
106
+ };
107
+ outputSchema?: any;
108
+ sourceSchema?: string;
109
+ sourceType?: string;
110
+ }
111
+
112
+ /**
113
+ * Script Flow Types
114
+ *
115
+ * Defines interfaces for the script-based query architecture:
116
+ * - ScriptRecipe: metadata for matching, validation, and quality tracking
117
+ * - ScriptResult: output from executing a script
118
+ * - ScriptMatch: result from the LLM-based script matcher
119
+ */
120
+
121
+ /**
122
+ * Recipe metadata stored alongside each script.
123
+ * Used for matching, validation, and quality tracking.
124
+ */
125
+ interface ScriptRecipe {
126
+ /** Unique script identifier */
127
+ id: string;
128
+ /** Version number (incremented on regeneration) */
129
+ version: number;
130
+ /** Human-readable name (e.g., "Revenue by Dimension") */
131
+ name: string;
132
+ /** Natural language description of what this script does */
133
+ intentDescription: string;
134
+ /** Keyword tags for quick filtering */
135
+ tags: string[];
136
+ /** Source tool IDs this script queries (e.g., ["mssql-abc123_query"]) */
137
+ sourceIds: string[];
138
+ /** Table names used (for future schema drift detection) */
139
+ tables: string[];
140
+ /** Parameter definitions — what can vary */
141
+ parameters: ScriptParameter[];
142
+ /** The script function body as a string. Loaded from disk (scripts-store/<fileBase>.ts). */
143
+ scriptBody: string;
144
+ /**
145
+ * On-disk filename stem for the body: scripts-store/<fileBase>.ts.
146
+ * Editable in the IDE. Decided at authoring time (slug of `name`, with a
147
+ * short id suffix on collision) and stable across promotion.
148
+ */
149
+ fileBase?: string;
150
+ /** sha256 of the on-disk body — lets the runtime detect manual edits. */
151
+ bodyHash?: string;
152
+ /** Project scope (single-VM deployments may leave this undefined). */
153
+ projectId?: string;
154
+ /** Times this script was used successfully */
155
+ successCount: number;
156
+ /** Times this script failed */
157
+ failureCount: number;
158
+ /** ISO timestamp of last usage */
159
+ lastUsed: string;
160
+ /** Original user question that created this script */
161
+ createdFrom: string;
162
+ /** ISO timestamp */
163
+ createdAt: string;
164
+ /** ISO timestamp */
165
+ updatedAt: string;
166
+ /**
167
+ * `recipe.id` of the parent this script was forked from.
168
+ * Undefined for root scripts (those written from scratch by MainAgent).
169
+ * See backend/docs/SCRIPT-FLOW-FORK-ADAPT.md.
170
+ */
171
+ parentId?: string;
172
+ /** 0 for root scripts; `parent.forkDepth + 1` for forks. Capped at 3. */
173
+ forkDepth?: number;
174
+ /**
175
+ * Brief description of what this fork changed vs its parent
176
+ * (sourced from the matcher's `modificationHint`).
177
+ */
178
+ forkReason?: string;
179
+ /**
180
+ * Validated component specs captured at authoring time. On a tier-high
181
+ * replay these are rebound to fresh queryIds deterministically — no
182
+ * component-generation LLM call, and the rendered columns can't drift from
183
+ * what was validated when the script was authored. Absent on recipes
184
+ * authored before this landed; those fall back to LLM component generation.
185
+ * See backend/docs/SCRIPT-COMPONENT-CONSISTENCY.md.
186
+ */
187
+ components?: ScriptComponentSpec[];
188
+ /**
189
+ * What the user explicitly asked the output to LOOK like, set by a display
190
+ * edit ("show that as a bar chart", "make the bars horizontal").
191
+ *
192
+ * Deliberately SEPARATE from `components`. Those are validated bindings that
193
+ * must be cleared on a data edit — after "break it down by brand" the axis
194
+ * keys are wrong, and after "use the mode" the stored title still says
195
+ * "Average…". A rendering CHOICE, by contrast, is still valid afterwards.
196
+ * Keeping them in one field meant every data edit silently discarded the
197
+ * user's chart type.
198
+ *
199
+ * Fed to the component generator as a hint whenever specs are (re)generated,
200
+ * so the chosen rendering comes back even after the SQL changes.
201
+ */
202
+ displayPreference?: ScriptDisplayPreference;
203
+ /**
204
+ * Lifecycle stage of this recipe on disk.
205
+ * - 'draft': written by MainAgent's write_script during a turn; filtered out
206
+ * of FTS results (status='verified' only) so the matcher never picks it.
207
+ * Filename is suffixed with `turnId` to keep concurrent turns
208
+ * from clobbering each other's drafts.
209
+ * - 'verified': promoted after `execute_script` succeeded; the matcher sees it.
210
+ * Filename drops the turn suffix unless a verified file with
211
+ * the same slug already exists (collision case keeps the suffix).
212
+ *
213
+ * Recipes loaded from disk without this field default to 'verified' so
214
+ * existing scripts keep working unchanged.
215
+ */
216
+ status?: 'draft' | 'verified';
217
+ /**
218
+ * Per-turn unique suffix used for draft filenames (e.g. `1714745623-x9k2`).
219
+ * Set when the draft is saved; carried until the recipe is promoted.
220
+ */
221
+ turnId?: string;
222
+ /**
223
+ * Last execution error captured by `recordDraftError` while the recipe was
224
+ * still a draft. Lets users open the draft .json file and see why it failed
225
+ * without grepping logs. Cleared on promotion to 'verified'.
226
+ */
227
+ lastError?: {
228
+ phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
229
+ message: string;
230
+ at: string;
231
+ attempt: number;
232
+ };
233
+ /** userId who committed the most recent edit (audit trail — recipes are project-scoped). */
234
+ editedBy?: string;
235
+ /** The instruction that produced the current version. */
236
+ editedFrom?: string;
237
+ /**
238
+ * Prior versions, newest last. The body itself is archived on disk as
239
+ * `<fileBase>.v<version>.ts`; this records what changed and who did it.
240
+ */
241
+ history?: ScriptVersionRecord[];
242
+ }
243
+ /**
244
+ * A durable, user-stated rendering choice for a recipe. Survives data edits.
245
+ */
246
+ interface ScriptDisplayPreference {
247
+ /** Component types the user's chosen rendering resolved to, e.g. ["DynamicBarChart"]. */
248
+ componentTypes: string[];
249
+ /** The instruction itself — carries nuance the types can't ("horizontal", "sorted descending"). */
250
+ instruction: string;
251
+ /** ISO timestamp of the display edit that set this. */
252
+ at: string;
253
+ }
254
+ /** One superseded version of a recipe body (see ScriptStore.commitEdit). */
255
+ interface ScriptVersionRecord {
256
+ /** Version number this record superseded (i.e. the OLD version). */
257
+ version: number;
258
+ /** ISO timestamp of the edit that superseded it. */
259
+ at: string;
260
+ /** userId who made the edit, when known. */
261
+ by?: string;
262
+ /** The edit instruction that caused the supersede. */
263
+ instruction?: string;
264
+ /** One-line summary of what changed, for the version picker. */
265
+ changeSummary?: string;
266
+ /** sha256 of the superseded body — pairs with `<fileBase>.v<version>.ts`. */
267
+ bodyHash?: string;
268
+ }
269
+ interface ScriptParameter {
270
+ /** Parameter name (used in script body as params.name) */
271
+ name: string;
272
+ /** Parameter type */
273
+ type: 'string' | 'number' | 'date' | 'date_range' | 'enum' | 'boolean';
274
+ /** Whether this parameter is required */
275
+ required: boolean;
276
+ /** Default value if not provided */
277
+ default?: any;
278
+ /** For enum type — maps user-facing values to internal values */
279
+ enumValues?: Record<string, string>;
280
+ /** Human-readable description (used in the matcher LLM prompt) */
281
+ description: string;
282
+ }
283
+ /**
284
+ * A reusable component binding captured when a script is authored. Stored on
285
+ * the recipe so tier-high replays rebuild components deterministically (rebind
286
+ * to fresh queryIds) instead of re-running the component-picker LLM.
287
+ */
288
+ interface ScriptComponentSpec {
289
+ /** Registered component name (e.g. "DynamicBarChart") — matched against the available component library. */
290
+ componentType: string;
291
+ /** `executedQuery.sourceId` to bind to (e.g. a tool id or 'computed:_final'), 'federation' for a cross-source component, or 'markdown' for a content-only narrative block (no data source). */
292
+ sourceRef: string;
293
+ /** Present only when sourceRef === 'federation' — the DuckDB SQL to re-execute on replay. */
294
+ federationSql?: string;
295
+ /** Present only when sourceRef === 'markdown' — the narrative text to render on replay (markdown has no data source, so its content must be persisted). */
296
+ content?: string;
297
+ title?: string;
298
+ description?: string;
299
+ /** Validated axis/value keys + aggregation — all referencing real columns of the bound source. */
300
+ config: Record<string, any>;
301
+ }
302
+ /**
303
+ * Result from executing a script via ScriptRunner.
304
+ */
305
+ interface ScriptResult {
306
+ /** Whether the script executed successfully */
307
+ success: boolean;
308
+ /** Combined data from all queries */
309
+ data: any[];
310
+ /** Program-generated narrative, when the script exports getAnalysis. */
311
+ analysis?: AnalysisOutput;
312
+ /**
313
+ * getAnalysis threw. `success` stays true — the data is valid, so the caller
314
+ * falls back to LLM prose rather than losing the answer.
315
+ */
316
+ analysisError?: string;
317
+ /** Individual query results tracked during execution */
318
+ executedQueries: ScriptQueryResult[];
319
+ /** Error message if failed */
320
+ error?: string;
321
+ /**
322
+ * Where in the lifecycle the error occurred. Lets MainAgent's fix-loop
323
+ * decide between "rewrite the whole draft" (compile) and "patch the
324
+ * specific line" (runtime).
325
+ */
326
+ errorPhase?: 'compile' | 'runtime' | 'timeout' | 'ipc';
327
+ /** Total execution time in milliseconds */
328
+ executionTimeMs: number;
329
+ }
330
+ /**
331
+ * A single query executed during script runtime.
332
+ * Tracked by ScriptContext for component generation and debugging.
333
+ */
334
+ interface ScriptQueryResult {
335
+ /** Source tool ID */
336
+ sourceId: string;
337
+ /** Human-readable source name */
338
+ sourceName: string;
339
+ /** The SQL that was executed */
340
+ sql: string;
341
+ /** Result data rows */
342
+ data: any[];
343
+ /** Number of rows returned */
344
+ count: number;
345
+ /** Total rows that matched before limit (if available) */
346
+ totalCount?: number;
347
+ /** Query execution time in milliseconds */
348
+ executionTimeMs: number;
349
+ /**
350
+ * True for rows that did NOT come from a real SQL execution — either a
351
+ * ctx.emit() dataset or the synthesized "computed:_final" entry that
352
+ * carries the script's post-JS returned data. The component generator
353
+ * uses this to route the resulting component through the script_dataset
354
+ * sentinel toolId so the frontend resolves it via the queryCache short-circuit.
355
+ */
356
+ virtual?: boolean;
357
+ }
358
+ /**
359
+ * Match tier returned by the LLM script matcher.
360
+ *
361
+ * - 'high': the script answers the question directly; only parameter values
362
+ * may differ. The runtime replays it with extracted params (cheapest path).
363
+ * - 'near': the script answers a STRUCTURALLY similar question but needs
364
+ * body modification (different metric, dimension, table, filter shape).
365
+ * The runtime forks the parent and adapts the body via MainAgent's normal
366
+ * write_script + execute_script loop — no SourceAgent dispatch needed.
367
+ * See backend/docs/SCRIPT-FLOW-FORK-ADAPT.md for the full design.
368
+ * - 'edit': the user is INSTRUCTING a change to the script that produced the
369
+ * previous answer (not asking a new question). Only reachable when the turn
370
+ * carries a ScriptBinding — without one the matcher coerces it to 'none', so
371
+ * a prompt regression can't turn this into a loose similarity path. The
372
+ * runtime runs MainAgent in edit mode and commits the result in place.
373
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md.
374
+ * - 'none': no script is relevant; full agent flow runs.
375
+ */
376
+ type MatchTier = 'high' | 'near' | 'edit' | 'none';
377
+ /**
378
+ * Which recipe produced the answer the user is currently looking at.
379
+ *
380
+ * Set whenever a turn's answer came from a script (replay, fresh authoring, or
381
+ * a committed edit) and carried on the UIBlock + saved conversation row. Two
382
+ * consumers:
383
+ * 1. The matcher — the 'edit' tier is ONLY reachable when a binding exists, so
384
+ * "use mode instead of mean" resolves to a concrete script instead of being
385
+ * matched on keywords it shares with no script name.
386
+ * 2. Cache invalidation — after an edit commits, conversations bound to that
387
+ * recipeId must be dropped, or the exact-match cache replays the pre-edit
388
+ * answer and the edit looks like a no-op.
389
+ *
390
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 1.
391
+ */
392
+ interface ScriptBinding {
393
+ recipeId: string;
394
+ /** Params the script ran with — the edit's starting point. */
395
+ params: Record<string, any>;
396
+ /** Recipe name at bind time (matcher catalog + user-facing confirmation). */
397
+ name: string;
398
+ /** Columns the last run returned — grounds the editor without a re-query. */
399
+ columns?: string[];
400
+ /**
401
+ * The question that produced this answer. Required for disambiguation when a
402
+ * thread ran several scripts — without it the candidates are just names and
403
+ * the matcher cannot resolve "use the mode for the WSP one".
404
+ */
405
+ userPrompt?: string;
406
+ }
407
+ /**
408
+ * Result from the LLM-based script matcher.
409
+ *
410
+ * For `tier: 'high'`, `extractedParams` carries the values to pass to the
411
+ * existing script. For `tier: 'near'`, `gaps` and `modificationHint` describe
412
+ * what the fork-author needs to change in the parent body.
413
+ */
414
+ interface ScriptMatch {
415
+ /** The matched script recipe */
416
+ recipe: ScriptRecipe;
417
+ /** Match tier — see MatchTier docs */
418
+ tier: MatchTier;
419
+ /** Similarity score (0-1, derived from LLM tier) */
420
+ similarity: number;
421
+ /**
422
+ * Legacy confidence level. Mirrors `tier === 'high'`/`'near'` for now;
423
+ * kept so existing callers compile while we migrate to tier-based logic.
424
+ */
425
+ confidence: 'high' | 'medium';
426
+ /** Parameters extracted from the user question by the LLM (tier='high') */
427
+ extractedParams?: Record<string, any>;
428
+ /**
429
+ * Proper nouns the question named, VERBATIM, with the parameter each fills.
430
+ *
431
+ * The matcher must NOT resolve these itself. Observed live: it substituted the
432
+ * customer name "PREMER ENRGES PHOTOOTAIC" into an id parameter believing that
433
+ * "the runtime will resolve the customer name to its PartyId(s)" — nothing on
434
+ * the replay path does, and `WHERE BillingPartyId IN (PREMER ENRGES
435
+ * PHOTOOTAIC)` reached SQL. Reported here, the runtime resolves them before
436
+ * replay and rejects the match when one cannot be resolved.
437
+ */
438
+ mentions?: Array<{
439
+ text: string;
440
+ param?: string;
441
+ }>;
442
+ /** Business concepts the question relies on (e.g. "outstanding balance"). */
443
+ entityTypes?: string[];
444
+ /** What the user question needs that the parent doesn't cover (tier='near') */
445
+ gaps?: string[];
446
+ /** One-sentence description of the change the fork-author should make (tier='near') */
447
+ modificationHint?: string;
448
+ /**
449
+ * Which HALF of the recipe the edit targets (tier='edit'). A recipe has two
450
+ * independently editable halves:
451
+ * - 'data' — the scriptBody: how the rows are produced (aggregation,
452
+ * filters, joins, grouping). Runs MainAgent in edit mode.
453
+ * - 'display' — the component specs: how those SAME rows are shown (chart
454
+ * type, orientation, columns, labels). Replays the proven SQL
455
+ * and regenerates the specs — never authors a script.
456
+ * Defaults to 'data' when the matcher omits it.
457
+ */
458
+ editTarget?: 'data' | 'display';
459
+ /**
460
+ * Self-contained restatement of the change the user asked for (tier='edit').
461
+ * MUST have pronouns/deixis resolved ("this", "it", "that column") — the edit
462
+ * prompt never sees the conversation history, so an unresolved instruction is
463
+ * unusable downstream.
464
+ */
465
+ editInstruction?: string;
466
+ /** Why the matcher made this choice (for logs and telemetry) */
467
+ reasoning?: string;
468
+ }
469
+
470
+ /**
471
+ * Multi-Agent Architecture Types
472
+ *
473
+ * Defines interfaces for the hierarchical agent system:
474
+ * - Main Agent: ONE LLM.streamWithTools() call with source agent tools
475
+ * - Source Agents: independent agents that query individual data sources
476
+ *
477
+ * The main agent sees only source summaries. When it calls a source tool,
478
+ * the SourceAgent runs independently (own LLM, own retries) and returns clean data.
479
+ */
480
+
481
+ /**
482
+ * Per-entity detail: name, row count, and column names.
483
+ * Gives the main agent enough context to route to the right source.
484
+ */
485
+ interface EntityDetail {
486
+ /** Entity name (table, sheet, endpoint) */
487
+ name: string;
488
+ /** Approximate row count */
489
+ rowCount?: number;
490
+ /** Column/field names */
491
+ columns: string[];
492
+ /** Entity-level semantic summary (what the table means) — for main-agent routing. */
493
+ summary?: string;
494
+ }
495
+ /**
496
+ * Representation of a data source for the main agent.
497
+ * Contains entity names WITH column names so the LLM can route accurately.
498
+ */
499
+ interface SourceSummary {
500
+ /** Source ID (matches tool ID prefix) */
501
+ id: string;
502
+ /** Human-readable source name */
503
+ name: string;
504
+ /** Source type: postgres, excel, rest_api, etc. */
505
+ type: string;
506
+ /** Brief description of what data this source contains */
507
+ description: string;
508
+ /** Detailed entity info with column names for routing */
509
+ entityDetails: EntityDetail[];
510
+ /** The tool ID associated with this source */
511
+ toolId: string;
512
+ }
513
+ /**
514
+ * What a source agent returns after querying its data source.
515
+ * The main agent uses this to analyze and compose the final response.
516
+ */
517
+ interface SourceAgentResult {
518
+ /** Source ID */
519
+ sourceId: string;
520
+ /** Source name */
521
+ sourceName: string;
522
+ /** Whether the query succeeded */
523
+ success: boolean;
524
+ /** Result data rows */
525
+ data: any[];
526
+ /** Metadata about the query execution */
527
+ metadata: SourceAgentMetadata;
528
+ /** Tool execution info for the last successful query (backward compat) */
529
+ executedTool: ExecutedToolInfo;
530
+ /** All successful tool executions (primary + follow-up queries) */
531
+ allExecutedTools?: ExecutedToolInfo[];
532
+ /** Error message if failed */
533
+ error?: string;
534
+ }
535
+ interface SourceAgentMetadata {
536
+ /** Total rows that matched the query (before limit) */
537
+ totalRowsMatched: number;
538
+ /** Rows actually returned (after limit) */
539
+ rowsReturned: number;
540
+ /** Whether the result was truncated by the row limit */
541
+ isLimited: boolean;
542
+ /** The query/params that were executed */
543
+ queryExecuted?: string;
544
+ /** Execution time in milliseconds */
545
+ executionTimeMs: number;
546
+ }
547
+ /**
548
+ * A pre-built, multi-step UI flow registered with the SDK.
549
+ *
550
+ * When the main agent decides a user's question matches a workflow's whenToUse
551
+ * trigger, it picks the workflow instead of running source agents / generating
552
+ * dashboard components. The LLM extracts the workflow's required props from the
553
+ * prompt (using `propsSchema` as the tool input_schema) and the SDK returns the
554
+ * workflow component directly — no analysis text, no chart generation. The
555
+ * frontend renders the registered workflow component with the LLM-extracted
556
+ * props.
557
+ */
558
+ interface WorkflowDescriptor {
559
+ /** Unique workflow id (used as the LLM tool name) */
560
+ id: string;
561
+ /** Component name on the frontend (matches the registered React component) */
562
+ name: string;
563
+ /** Short human-readable description of what this workflow does */
564
+ description: string;
565
+ /**
566
+ * 1–2 sentence trigger condition. The LLM uses this to decide if the
567
+ * user's prompt matches this workflow. Be specific — e.g.
568
+ * "User wants to *initiate* an inventory transfer (review + submit POs),
569
+ * not just see analysis or charts."
570
+ */
571
+ whenToUse: string;
572
+ /**
573
+ * JSON-schema-style description of the props the workflow needs. Becomes
574
+ * the LLM tool's input_schema, so the model fills these from the prompt.
575
+ * Use the same shape as `params` on direct tools — string descriptors with
576
+ * an optional "(optional)" suffix.
577
+ *
578
+ * Example:
579
+ * ```
580
+ * {
581
+ * selectedStore: 'object — { id, name } of the source branch',
582
+ * minROI: 'number (optional) — only show transfers with ROI ≥ this',
583
+ * }
584
+ * ```
585
+ */
586
+ propsSchema: Record<string, string>;
587
+ /**
588
+ * Optional: static prop defaults merged with LLM-extracted props before
589
+ * the component is returned. Useful for things like the embedded
590
+ * `externalTool` config that the workflow uses to fetch its own data.
591
+ */
592
+ defaultProps?: Record<string, any>;
593
+ }
594
+ /**
595
+ * The workflow selection captured during a routing call.
596
+ * Set on AgentResponse when the LLM picks a workflow tool.
597
+ */
598
+ interface SelectedWorkflow {
599
+ /** Component name (matches WorkflowDescriptor.name) */
600
+ name: string;
601
+ /** Props extracted from the prompt + merged with workflow.defaultProps */
602
+ props: Record<string, any>;
603
+ }
604
+ /**
605
+ * Set when this turn applies a user-directed edit to an existing script instead
606
+ * of authoring a new one. MainAgent keeps the SAME harness (tools, loop,
607
+ * write_script/execute_script verification) and swaps only the system prompt —
608
+ * `agent-main-edit` instead of `agent-main`.
609
+ *
610
+ * The two framings contradict each other on whether to query a source first, so
611
+ * they must never be resident in one rendered prompt.
612
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § D3.
613
+ */
614
+ interface EditContext {
615
+ /** Recipe being edited — the shadow draft records it as parentId. */
616
+ recipeId: string;
617
+ parentName: string;
618
+ parentBody: string;
619
+ /** Self-contained restatement of the change (from the matcher). */
620
+ instruction: string;
621
+ /** Columns the last run returned — grounds the edit without a re-query. */
622
+ lastResultColumns?: string[];
623
+ /** Rendered parameter list of the parent, for the prompt. */
624
+ parentParams?: string;
625
+ }
626
+ /**
627
+ * The complete response from the multi-agent system.
628
+ * Contains everything needed for text display + component generation.
629
+ */
630
+ interface AgentResponse {
631
+ /** Generated text response (analysis of the data) */
632
+ text: string;
633
+ /**
634
+ * Program-generated narrative when the script exported getAnalysis. `text`
635
+ * already carries `analysis.analysis`; this also exposes `summary`, which
636
+ * the markdown block binds to instead of being an LLM summary of the prose.
637
+ */
638
+ analysis?: AnalysisOutput;
639
+ /** All executed tools across all source agents (for component generation) */
640
+ executedTools: ExecutedToolInfo[];
641
+ /** Individual results from each source agent */
642
+ sourceResults: SourceAgentResult[];
643
+ /**
644
+ * Populated when MainAgent wrote AND successfully executed a script during its turn.
645
+ * Caller (agent-user-response.ts) persists it via ScriptStore.save().
646
+ * Absent when MainAgent didn't write one (trivial question / all attempts failed).
647
+ */
648
+ savedScript?: AgentWrittenScript;
649
+ /** Failed execute_script tool_result texts this turn, in order — see RunTrace.script.failedAttempts. */
650
+ failedScriptAttempts?: string[];
651
+ /**
652
+ * Set when the LLM routed the question to a registered workflow component.
653
+ * When present, the upstream caller should skip component generation and
654
+ * return this workflow as the response.
655
+ */
656
+ workflow?: SelectedWorkflow;
657
+ /**
658
+ * Validated component specs the agent authored via `render_components`.
659
+ * When present the caller assembles the dashboard from these instead of
660
+ * running the component-generation LLM. Absent when the agent didn't call
661
+ * the tool — the caller then falls back to `generateScriptComponents`.
662
+ * See backend/docs/COMPONENT-GENERATION-V2.md §2.2.
663
+ */
664
+ componentSpecs?: ScriptComponentSpec[];
665
+ /** Layout title/description from the same `render_components` call. */
666
+ componentLayout?: {
667
+ title: string;
668
+ description?: string;
669
+ };
670
+ }
671
+ /**
672
+ * A script MainAgent authored + verified during its turn. Shape aligns with
673
+ * what ScriptStore.save() needs — minus store-assigned fields (id, timestamps, counts).
674
+ */
675
+ interface AgentWrittenScript {
676
+ /**
677
+ * `ScriptRecipe.id` of the draft that was authored + verified during this turn.
678
+ * The caller passes this to `ScriptStore.promoteToVerified(recipeId, …)` to
679
+ * flip the draft to verified status and (when possible) drop the turn-suffix
680
+ * from its filename.
681
+ */
682
+ recipeId: string;
683
+ name: string;
684
+ intentDescription: string;
685
+ tags: string[];
686
+ parameters: Array<{
687
+ name: string;
688
+ type: 'string' | 'number' | 'date' | 'date_range' | 'enum' | 'boolean';
689
+ required: boolean;
690
+ default?: any;
691
+ enumValues?: Record<string, string>;
692
+ description: string;
693
+ }>;
694
+ scriptBody: string;
695
+ /** Source IDs referenced by the script (extracted from ctx.query calls) */
696
+ sourceIds: string[];
697
+ /** Tables referenced in the script's SQL (regex-extracted) */
698
+ tables: string[];
699
+ /** Executed queries from the verified run — fed to component generation */
700
+ executedQueries: Array<{
701
+ sourceId: string;
702
+ sourceName: string;
703
+ sql: string;
704
+ data: any[];
705
+ count: number;
706
+ totalCount?: number;
707
+ executionTimeMs: number;
708
+ /**
709
+ * True for synthetic entries (ctx.emit datasets, the computed:_final
710
+ * post-JS data). The component generator routes virtual sources through
711
+ * the script_dataset sentinel toolId so the frontend resolves them via
712
+ * queryCache instead of attempting to re-execute SQL.
713
+ */
714
+ virtual?: boolean;
715
+ }>;
716
+ /** The tool_result JSON MainAgent's LLM got back from execute_script — see RunTrace.script.executionResult. */
717
+ executionResult?: string;
718
+ }
719
+ /**
720
+ * Debug trace for a single chat_agent turn — which KB/schema nodes were
721
+ * retrieved and considered, and which script (if any) produced the answer.
722
+ * Persisted separately from the answer itself (fusion-5's
723
+ * `conversation_traces` table, written via `saveTrace`), never sent through
724
+ * `analyticsClient` (data-residency — see design doc §5).
725
+ */
726
+ interface RunTrace {
727
+ version: 1;
728
+ kbNodes: {
729
+ /**
730
+ * Always-on nodes included regardless of the question (getGlobalKnowledgeBase).
731
+ * `content` is the node's full text AS IT WAS at retrieval time — captured so a
732
+ * later edit/delete of the KB node doesn't erase what the LLM actually saw.
733
+ */
734
+ global: Array<{
735
+ kbId: string;
736
+ title: string;
737
+ content?: string;
738
+ }>;
739
+ /** Semantically matched for this specific question (getKnowledgeBase). Same `content` reasoning as above. */
740
+ query: Array<{
741
+ kbId: string;
742
+ title: string;
743
+ similarity?: number;
744
+ content?: string;
745
+ }>;
746
+ };
747
+ schemaNodes: {
748
+ /** MainAgent's cross-source prefilter — which sources were even considered. */
749
+ routing: Array<{
750
+ sourceId: string;
751
+ sourceName?: string;
752
+ similarity: number;
753
+ }>;
754
+ /** SourceAgent-scope per-source table resolution (MainAgent.preResolveSchema). */
755
+ preResolve: Array<{
756
+ sourceId: string;
757
+ table?: string;
758
+ description?: string;
759
+ similarity: number;
760
+ }>;
761
+ /**
762
+ * The exact {{SOURCE_SUMMARIES}} text injected into MainAgent's system prompt
763
+ * (agent-prompt-builder.ts's formatSummariesForPrompt output) — every
764
+ * registered source's catalog entry, not retrieval-specific, captured once per
765
+ * turn. This is what the main agent actually saw when deciding which source(s)
766
+ * to call.
767
+ */
768
+ mainAgentCatalog?: string;
769
+ /**
770
+ * One entry per SourceAgent dispatch (a source called twice in one turn gets
771
+ * two entries) — the exact {{FULL_SCHEMA}} text injected into that
772
+ * SourceAgent's own system prompt, however it was sourced, plus any
773
+ * search_schema follow-up lookups it made mid-conversation when the schema
774
+ * shown wasn't enough.
775
+ */
776
+ sourceAgentSchemas: Array<{
777
+ sourceId: string;
778
+ sourceName?: string;
779
+ /**
780
+ * The exact intent text MainAgent wrote for this dispatch (before the
781
+ * resolved-entities block gets appended by code — see
782
+ * buildResolvedEntitiesBlock) — what MainAgent actually asked this
783
+ * source agent to go find.
784
+ */
785
+ intent: string;
786
+ /** How `schemaText` was sourced — see SourceAgent.buildPrompt's fullSchema fallback chain. */
787
+ method: 'preresolved-embedding' | 'tool-full-schema' | 'tool-description' | 'none';
788
+ schemaText: string;
789
+ /** In call order — each search_schema tool call this SourceAgent made, and the exact result text handed back to its own LLM. */
790
+ searchSchemaLookups: Array<{
791
+ keywords: string[];
792
+ result: string;
793
+ }>;
794
+ /** The tool_result text MainAgent's LLM read back for this dispatch (formatResultForMainAgent output). */
795
+ result?: string;
796
+ }>;
797
+ };
798
+ /** The script that produced this answer, if any. Null for a general (no-tool) answer. */
799
+ script: {
800
+ /** True when an existing recipe was replayed unchanged (ScriptMatcher tier='high'). */
801
+ reused: boolean;
802
+ recipeId?: string | null;
803
+ body?: string | null;
804
+ /** Set when reused — the matcher's reasoning for picking this recipe. */
805
+ reasoning?: string | null;
806
+ /** The winning execute_script tool_result text. Only set when reused=false — a replay never re-runs execute_script. Failed attempts aren't kept. */
807
+ executionResult?: string | null;
808
+ /** Failed execute_script tool_result texts this turn, in order (before the winning one, if any). */
809
+ failedAttempts?: string[];
810
+ } | null;
811
+ /**
812
+ * `resolve_entities` tool activity this turn (see main-agent.ts's
813
+ * RESOLVE_ENTITIES_TOOL_DEF / handleResolveEntities) — names/identifiers the
814
+ * user mentioned, resolved to database ids before any SQL was written.
815
+ * Absent/empty entirely for a project with no entity-search collection
816
+ * registered, or a turn that named nothing.
817
+ */
818
+ entityResolution: {
819
+ /**
820
+ * The entity-type catalog text injected into MainAgent's system prompt
821
+ * (loadEntityCatalog's output) — what let the LLM name a concept instead
822
+ * of guessing the project's vocabulary. Captured once per turn.
823
+ */
824
+ catalogText?: string;
825
+ /**
826
+ * One entry per `resolve_entities` tool call this turn (a turn can call it
827
+ * more than once) — the FULL raw response from the entity-search
828
+ * collection, not just the trimmed {mention,entityType,instanceId,
829
+ * displayName} MainAgent keeps for itself to build the dispatch block.
830
+ * `score`/`ambiguous`/`alternatives` only exist here, nowhere else.
831
+ */
832
+ calls: Array<{
833
+ mentions: Array<{
834
+ text: string;
835
+ entityType?: string;
836
+ }>;
837
+ entityTypes: string[];
838
+ resolved: Array<{
839
+ mention: string;
840
+ entityType: string;
841
+ instanceId: string;
842
+ displayName: string;
843
+ attrs?: Record<string, any>;
844
+ score?: number;
845
+ ambiguous?: boolean;
846
+ alternatives?: Array<{
847
+ instanceId: string;
848
+ displayName: string;
849
+ score: number;
850
+ }>;
851
+ }>;
852
+ unresolved: string[];
853
+ entityMap: Record<string, any>;
854
+ unmatchedEntityTypes?: Array<{
855
+ requested: string;
856
+ candidates?: string[];
857
+ }>;
858
+ /** Set when the collection call itself failed (fails open — see handleResolveEntities). */
859
+ error?: string;
860
+ }>;
861
+ };
862
+ /**
863
+ * The prior-turn conversation context injected into this turn's prompt as
864
+ * CONVERSATION_HISTORY — exactly what each flow already builds for itself
865
+ * (chat: thread.getConversationContext(); report/dashboard: their own
866
+ * equivalents), captured as-is. Every call site sets this unconditionally
867
+ * (empty string '' when there's no prior context — new thread, or history
868
+ * trimmed to nothing), so an empty turn is visibly "no history" in the
869
+ * saved trace rather than a silently missing key. Optional only because
870
+ * traces saved before this field existed won't have it at all.
871
+ */
872
+ conversationHistory?: string;
873
+ /**
874
+ * The exact {{USER_ACCESS_CONTEXT}} text injected into MainAgent's system
875
+ * prompt — the per-user authorization config (sa-api users.config)
876
+ * rendered by buildUserAccessContext, as-is. Captured once (MainAgent's
877
+ * prompt is what SourceAgent's identical value is derived from — no need
878
+ * to duplicate it per source dispatch). Set unconditionally (empty string
879
+ * '' when there's no config for this user), so "no restrictions" is
880
+ * visible in the saved trace rather than a silently missing key. Optional
881
+ * only because traces saved before this field existed won't have it.
882
+ */
883
+ userAccessContext?: string;
884
+ }
885
+ /**
886
+ * Configuration for the multi-agent system.
887
+ * Controls limits, models, and behavior.
888
+ */
889
+ interface AgentConfig {
890
+ /** Max rows shown to the UI preview / inlined per source (default: 10) */
891
+ maxRowsPerSource: number;
892
+ /**
893
+ * Max rows a source query may FETCH from the DB server-side (default: 2000).
894
+ * Decoupled from what the main agent is shown: the full result is fetched and
895
+ * summarized (bounded), but only a small/complete slice enters LLM context.
896
+ * This lets small lookups (benchmark maps) arrive COMPLETE without letting
897
+ * large results blow up context.
898
+ */
899
+ maxRowsFetched: number;
900
+ /** Model for the main agent (routing + analysis in one LLM call) */
901
+ mainAgentModel: string;
902
+ /** Model for source agent query generation */
903
+ sourceAgentModel: string;
904
+ /** API key for LLM calls */
905
+ apiKey?: string;
906
+ /** Max retry attempts per source agent */
907
+ maxRetries: number;
908
+ /** Max tool calling iterations for the main agent loop */
909
+ maxIterations: number;
910
+ /** Global knowledge base context (static, same for all users/questions — cached in system prompt) */
911
+ globalKnowledgeBase?: string;
912
+ /** Per-request knowledge base context (user-specific + query-matched — dynamic, not cached) */
913
+ knowledgeBaseContext?: string;
914
+ /** Per-user authorization config (sa-api users.config), rendered to text — see buildUserAccessContext */
915
+ userAccessContext?: string;
916
+ /** Collections registry (ChromaDB search hooks) for embedding-based schema + source search */
917
+ collections?: any;
918
+ /** Optional project ID for scoping embedding searches */
919
+ projectId?: string;
920
+ /**
921
+ * Optional debug-trace accumulator for this turn (Phase 1, chat_agent only).
922
+ * When present, MainAgent-scope retrieval call sites (e.g. preResolveSchema)
923
+ * push their structured results into it. Absent for callers that don't care
924
+ * about trace capture (e.g. headless flows) — every push site must treat
925
+ * this as optional and never let a capture failure affect the real answer.
926
+ */
927
+ trace?: RunTrace;
928
+ }
929
+ /**
930
+ * Default agent configuration
931
+ */
932
+ declare const DEFAULT_AGENT_CONFIG: AgentConfig;
933
+
4
934
  /**
5
935
  * Unified UIBlock structure for database storage
6
- * Used in both bookmarks and user-conversations tables
936
+ * Used in both bookmarks and user-conversations tables.
937
+ *
938
+ * `analysis` always holds whatever real narration exists — the full answer on
939
+ * success, or whatever was actually streamed before a failure (may be empty).
940
+ * `error` is a dedicated field, null on success — it can be a plain string OR
941
+ * a structured object/array (e.g. the agent's raw `errors` list), whatever
942
+ * shape the failure naturally has; it's never coerced into `analysis`.
7
943
  */
8
944
  interface DBUIBlock {
9
945
  id: string;
10
946
  component: Record<string, any> | null;
11
947
  analysis: string | null;
12
948
  user_prompt: string;
949
+ error?: unknown | null;
950
+ /**
951
+ * The script recipe that produced this answer, when one did. Read back via
952
+ * `conversation-history.exactMatch` so an edit follow-up still has a target
953
+ * after a reload (the in-memory thread is gone by then), and indexed so
954
+ * committing an edit can purge every cached answer bound to the recipe.
955
+ */
956
+ scriptBinding?: {
957
+ recipeId: string;
958
+ params?: Record<string, any>;
959
+ name?: string;
960
+ columns?: string[];
961
+ };
13
962
  }
14
963
 
15
964
  /**
@@ -160,6 +1109,7 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
160
1109
  dependencies?: string[] | undefined;
161
1110
  } | undefined;
162
1111
  props?: Record<string, any> | undefined;
1112
+ data?: Record<string, any> | undefined;
163
1113
  render?: any;
164
1114
  states?: Record<string, any> | undefined;
165
1115
  methods?: Record<string, {
@@ -170,7 +1120,6 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
170
1120
  fn: string;
171
1121
  deps?: string[] | undefined;
172
1122
  }[] | undefined;
173
- data?: Record<string, any> | undefined;
174
1123
  pages?: {
175
1124
  id: string;
176
1125
  name: string;
@@ -192,6 +1141,7 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
192
1141
  dependencies?: string[] | undefined;
193
1142
  } | undefined;
194
1143
  props?: Record<string, any> | undefined;
1144
+ data?: Record<string, any> | undefined;
195
1145
  render?: any;
196
1146
  states?: Record<string, any> | undefined;
197
1147
  methods?: Record<string, {
@@ -202,7 +1152,6 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
202
1152
  fn: string;
203
1153
  deps?: string[] | undefined;
204
1154
  }[] | undefined;
205
- data?: Record<string, any> | undefined;
206
1155
  pages?: {
207
1156
  id: string;
208
1157
  name: string;
@@ -228,6 +1177,7 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
228
1177
  dependencies?: string[] | undefined;
229
1178
  } | undefined;
230
1179
  props?: Record<string, any> | undefined;
1180
+ data?: Record<string, any> | undefined;
231
1181
  render?: any;
232
1182
  states?: Record<string, any> | undefined;
233
1183
  methods?: Record<string, {
@@ -238,7 +1188,6 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
238
1188
  fn: string;
239
1189
  deps?: string[] | undefined;
240
1190
  }[] | undefined;
241
- data?: Record<string, any> | undefined;
242
1191
  pages?: {
243
1192
  id: string;
244
1193
  name: string;
@@ -264,6 +1213,7 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
264
1213
  dependencies?: string[] | undefined;
265
1214
  } | undefined;
266
1215
  props?: Record<string, any> | undefined;
1216
+ data?: Record<string, any> | undefined;
267
1217
  render?: any;
268
1218
  states?: Record<string, any> | undefined;
269
1219
  methods?: Record<string, {
@@ -274,7 +1224,6 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
274
1224
  fn: string;
275
1225
  deps?: string[] | undefined;
276
1226
  }[] | undefined;
277
- data?: Record<string, any> | undefined;
278
1227
  pages?: {
279
1228
  id: string;
280
1229
  name: string;
@@ -355,6 +1304,7 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
355
1304
  dependencies?: string[] | undefined;
356
1305
  } | undefined;
357
1306
  props?: Record<string, any> | undefined;
1307
+ data?: Record<string, any> | undefined;
358
1308
  render?: any;
359
1309
  states?: Record<string, any> | undefined;
360
1310
  methods?: Record<string, {
@@ -365,7 +1315,6 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
365
1315
  fn: string;
366
1316
  deps?: string[] | undefined;
367
1317
  }[] | undefined;
368
- data?: Record<string, any> | undefined;
369
1318
  }, {
370
1319
  id: string;
371
1320
  name?: string | undefined;
@@ -379,6 +1328,7 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
379
1328
  dependencies?: string[] | undefined;
380
1329
  } | undefined;
381
1330
  props?: Record<string, any> | undefined;
1331
+ data?: Record<string, any> | undefined;
382
1332
  render?: any;
383
1333
  states?: Record<string, any> | undefined;
384
1334
  methods?: Record<string, {
@@ -389,7 +1339,6 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
389
1339
  fn: string;
390
1340
  deps?: string[] | undefined;
391
1341
  }[] | undefined;
392
- data?: Record<string, any> | undefined;
393
1342
  }>;
394
1343
  data: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
395
1344
  context: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
@@ -407,6 +1356,7 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
407
1356
  dependencies?: string[] | undefined;
408
1357
  } | undefined;
409
1358
  props?: Record<string, any> | undefined;
1359
+ data?: Record<string, any> | undefined;
410
1360
  render?: any;
411
1361
  states?: Record<string, any> | undefined;
412
1362
  methods?: Record<string, {
@@ -417,7 +1367,6 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
417
1367
  fn: string;
418
1368
  deps?: string[] | undefined;
419
1369
  }[] | undefined;
420
- data?: Record<string, any> | undefined;
421
1370
  };
422
1371
  data?: Record<string, any> | undefined;
423
1372
  context?: Record<string, any> | undefined;
@@ -435,6 +1384,7 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
435
1384
  dependencies?: string[] | undefined;
436
1385
  } | undefined;
437
1386
  props?: Record<string, any> | undefined;
1387
+ data?: Record<string, any> | undefined;
438
1388
  render?: any;
439
1389
  states?: Record<string, any> | undefined;
440
1390
  methods?: Record<string, {
@@ -445,7 +1395,6 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
445
1395
  fn: string;
446
1396
  deps?: string[] | undefined;
447
1397
  }[] | undefined;
448
- data?: Record<string, any> | undefined;
449
1398
  };
450
1399
  data?: Record<string, any> | undefined;
451
1400
  context?: Record<string, any> | undefined;
@@ -777,6 +1726,14 @@ declare const ToolSchema: z.ZodObject<{
777
1726
  description: string;
778
1727
  }[];
779
1728
  }>>;
1729
+ /** Cache policy. `false` = never cache (live data, write ops). Mirrors HTTP `Cache-Control: no-store`. */
1730
+ cache: z.ZodOptional<z.ZodUnion<[z.ZodLiteral<false>, z.ZodObject<{
1731
+ ttlMs: z.ZodOptional<z.ZodNumber>;
1732
+ }, "strip", z.ZodTypeAny, {
1733
+ ttlMs?: number | undefined;
1734
+ }, {
1735
+ ttlMs?: number | undefined;
1736
+ }>]>>;
780
1737
  }, "strip", z.ZodTypeAny, {
781
1738
  id: string;
782
1739
  params: Record<string, string>;
@@ -793,6 +1750,9 @@ declare const ToolSchema: z.ZodObject<{
793
1750
  description: string;
794
1751
  }[];
795
1752
  } | undefined;
1753
+ cache?: false | {
1754
+ ttlMs?: number | undefined;
1755
+ } | undefined;
796
1756
  }, {
797
1757
  id: string;
798
1758
  params: Record<string, string>;
@@ -809,6 +1769,9 @@ declare const ToolSchema: z.ZodObject<{
809
1769
  description: string;
810
1770
  }[];
811
1771
  } | undefined;
1772
+ cache?: false | {
1773
+ ttlMs?: number | undefined;
1774
+ } | undefined;
812
1775
  }>;
813
1776
  type Tool$1 = z.infer<typeof ToolSchema>;
814
1777
  type CollectionOperation = 'getMany' | 'getOne' | 'query' | 'mutation' | 'updateOne' | 'deleteOne' | 'createOne';
@@ -847,6 +1810,10 @@ interface SuperatomSDKConfig {
847
1810
  bundleDir?: string;
848
1811
  promptsDir?: string;
849
1812
  databaseType?: DatabaseType;
1813
+ /** BCP-47 locale for `fmt` in generated analyses, e.g. 'en-IN', 'en-US'. Defaults to 'en-IN'. */
1814
+ locale?: string;
1815
+ /** ISO-4217 currency for `fmt`, e.g. 'INR', 'USD'. Defaults to 'INR'. */
1816
+ currency?: string;
850
1817
  ANTHROPIC_API_KEY?: string;
851
1818
  GROQ_API_KEY?: string;
852
1819
  GEMINI_API_KEY?: string;
@@ -994,7 +1961,7 @@ declare const KbNodesRequestPayloadSchema: z.ZodObject<{
994
1961
  content?: string | undefined;
995
1962
  }>>;
996
1963
  }, "strip", z.ZodTypeAny, {
997
- operation: "create" | "getOne" | "update" | "delete" | "getAll" | "search" | "getByCategory" | "getByUser" | "getCategories" | "getTags";
1964
+ operation: "create" | "getOne" | "search" | "update" | "delete" | "getAll" | "getByCategory" | "getByUser" | "getCategories" | "getTags";
998
1965
  data?: {
999
1966
  id?: number | undefined;
1000
1967
  type?: "query" | "user" | "global" | undefined;
@@ -1017,7 +1984,7 @@ declare const KbNodesRequestPayloadSchema: z.ZodObject<{
1017
1984
  content?: string | undefined;
1018
1985
  } | undefined;
1019
1986
  }, {
1020
- operation: "create" | "getOne" | "update" | "delete" | "getAll" | "search" | "getByCategory" | "getByUser" | "getCategories" | "getTags";
1987
+ operation: "create" | "getOne" | "search" | "update" | "delete" | "getAll" | "getByCategory" | "getByUser" | "getCategories" | "getTags";
1021
1988
  data?: {
1022
1989
  id?: number | undefined;
1023
1990
  type?: "query" | "user" | "global" | undefined;
@@ -1233,347 +2200,383 @@ declare class DashboardManager {
1233
2200
  * @param dashboardId - Dashboard ID
1234
2201
  * @returns True if dashboard exists, false otherwise
1235
2202
  */
1236
- dashboardExists(dashboardId: string): boolean;
1237
- /**
1238
- * Get dashboard count
1239
- * @returns Number of dashboards
1240
- */
1241
- getDashboardCount(): number;
1242
- }
1243
-
1244
- /**
1245
- * ReportManager class to handle CRUD operations on reports
1246
- * All operations read/write directly to files (no in-memory caching)
1247
- */
1248
- declare class ReportManager {
1249
- private reportsBasePath;
1250
- private projectId;
1251
- /**
1252
- * Initialize ReportManager with project ID
1253
- * @param projectId - Project ID to use in file path
1254
- */
1255
- constructor(projectId?: string);
1256
- /**
1257
- * Get the file path for a specific report
1258
- * @param reportId - Report ID
1259
- * @returns Full path to report data.json file
1260
- */
1261
- private getReportPath;
1262
- /**
1263
- * Create a new report
1264
- * @param reportId - Unique report ID
1265
- * @param report - Report data
1266
- * @returns Created report with metadata
1267
- */
1268
- createReport(reportId: string, report: DSLRendererProps): DSLRendererProps;
1269
- /**
1270
- * Get a specific report by ID
1271
- * @param reportId - Report ID
1272
- * @returns Report data or null if not found
1273
- */
1274
- getReport(reportId: string): DSLRendererProps | null;
1275
- /**
1276
- * Get all reports
1277
- * @returns Array of report objects with their IDs
1278
- */
1279
- getAllReports(): Array<{
1280
- reportId: string;
1281
- report: DSLRendererProps;
1282
- }>;
1283
- /**
1284
- * Update an existing report
1285
- * @param reportId - Report ID
1286
- * @param report - Updated report data
1287
- * @returns Updated report or null if not found
1288
- */
1289
- updateReport(reportId: string, report: DSLRendererProps): DSLRendererProps | null;
1290
- /**
1291
- * Delete a report
1292
- * @param reportId - Report ID
1293
- * @returns True if deleted, false if not found
1294
- */
1295
- deleteReport(reportId: string): boolean;
1296
- /**
1297
- * Check if a report exists
1298
- * @param reportId - Report ID
1299
- * @returns True if report exists, false otherwise
1300
- */
1301
- reportExists(reportId: string): boolean;
1302
- /**
1303
- * Get report count
1304
- * @returns Number of reports
1305
- */
1306
- getReportCount(): number;
1307
- }
1308
-
1309
- /**
1310
- * StreamBuffer - Buffered streaming utility for smoother text delivery
1311
- * Batches small chunks together and flushes at regular intervals
1312
- */
1313
- type StreamCallback = (chunk: string) => void;
1314
- /**
1315
- * StreamBuffer class for managing buffered streaming output
1316
- * Provides smooth text delivery by batching small chunks
1317
- */
1318
- declare class StreamBuffer {
1319
- private buffer;
1320
- private flushTimer;
1321
- private callback;
1322
- private fullText;
1323
- constructor(callback?: StreamCallback);
1324
- /**
1325
- * Check if the buffer has a callback configured
1326
- */
1327
- hasCallback(): boolean;
1328
- /**
1329
- * Get all text that has been written (including already flushed)
1330
- */
1331
- getFullText(): string;
1332
- /**
1333
- * Write a chunk to the buffer
1334
- * Large chunks or chunks with newlines are flushed immediately
1335
- * Small chunks are batched and flushed after a short interval
1336
- *
1337
- * @param chunk - Text chunk to write
1338
- */
1339
- write(chunk: string): void;
1340
- /**
1341
- * Flush the buffer immediately
1342
- * Call this before tool execution or other operations that need clean output
1343
- */
1344
- flush(): void;
1345
- /**
1346
- * Internal flush implementation
1347
- */
1348
- private flushNow;
1349
- /**
1350
- * Clean up resources
1351
- * Call this when done with the buffer
1352
- */
1353
- dispose(): void;
1354
- }
1355
-
1356
- /**
1357
- * ToolExecutorService - Handles execution of SQL queries and external tools
1358
- * Extracted from BaseLLM.generateTextResponse for better separation of concerns
1359
- */
1360
-
1361
- /**
1362
- * External tool definition
1363
- */
1364
- interface ExternalTool {
1365
- id: string;
1366
- name: string;
1367
- description?: string;
1368
- /** Tool type: "source" = routed through SourceAgent, "direct" = called directly by MainAgent */
1369
- toolType?: 'source' | 'direct';
1370
- /** Full untruncated schema for source agent (all columns visible) */
1371
- fullSchema?: string;
1372
- /** Schema size tier: small (≤50 tables), medium (51-200), large (201-500), very_large (500+) */
1373
- schemaTier?: string;
1374
- /** Schema search function for very_large tier — keyword search over entities */
1375
- schemaSearchFn?: (keywords: string[]) => string;
1376
- fn: (input: any) => Promise<any>;
1377
- limit?: number;
1378
- outputSchema?: any;
1379
- executionType?: 'immediate' | 'deferred';
1380
- userProvidedData?: any;
1381
- params?: Record<string, any>;
1382
- }
1383
- /**
1384
- * Executed tool tracking info
1385
- */
1386
- interface ExecutedToolInfo {
1387
- id: string;
1388
- name: string;
1389
- params: any;
1390
- result: {
1391
- _totalRecords: number;
1392
- _recordsShown: number;
1393
- _metadata?: any;
1394
- _sampleData: any[];
1395
- };
1396
- outputSchema?: any;
1397
- sourceSchema?: string;
1398
- sourceType?: string;
1399
- }
1400
-
1401
- /**
1402
- * Multi-Agent Architecture Types
1403
- *
1404
- * Defines interfaces for the hierarchical agent system:
1405
- * - Main Agent: ONE LLM.streamWithTools() call with source agent tools
1406
- * - Source Agents: independent agents that query individual data sources
1407
- *
1408
- * The main agent sees only source summaries. When it calls a source tool,
1409
- * the SourceAgent runs independently (own LLM, own retries) and returns clean data.
1410
- */
1411
-
1412
- /**
1413
- * Per-entity detail: name, row count, and column names.
1414
- * Gives the main agent enough context to route to the right source.
1415
- */
1416
- interface EntityDetail {
1417
- /** Entity name (table, sheet, endpoint) */
1418
- name: string;
1419
- /** Approximate row count */
1420
- rowCount?: number;
1421
- /** Column/field names */
1422
- columns: string[];
1423
- }
1424
- /**
1425
- * Representation of a data source for the main agent.
1426
- * Contains entity names WITH column names so the LLM can route accurately.
1427
- */
1428
- interface SourceSummary {
1429
- /** Source ID (matches tool ID prefix) */
1430
- id: string;
1431
- /** Human-readable source name */
1432
- name: string;
1433
- /** Source type: postgres, excel, rest_api, etc. */
1434
- type: string;
1435
- /** Brief description of what data this source contains */
1436
- description: string;
1437
- /** Detailed entity info with column names for routing */
1438
- entityDetails: EntityDetail[];
1439
- /** The tool ID associated with this source */
1440
- toolId: string;
1441
- }
1442
- /**
1443
- * What a source agent returns after querying its data source.
1444
- * The main agent uses this to analyze and compose the final response.
1445
- */
1446
- interface SourceAgentResult {
1447
- /** Source ID */
1448
- sourceId: string;
1449
- /** Source name */
1450
- sourceName: string;
1451
- /** Whether the query succeeded */
1452
- success: boolean;
1453
- /** Result data rows */
1454
- data: any[];
1455
- /** Metadata about the query execution */
1456
- metadata: SourceAgentMetadata;
1457
- /** Tool execution info for the last successful query (backward compat) */
1458
- executedTool: ExecutedToolInfo;
1459
- /** All successful tool executions (primary + follow-up queries) */
1460
- allExecutedTools?: ExecutedToolInfo[];
1461
- /** Error message if failed */
1462
- error?: string;
1463
- }
1464
- interface SourceAgentMetadata {
1465
- /** Total rows that matched the query (before limit) */
1466
- totalRowsMatched: number;
1467
- /** Rows actually returned (after limit) */
1468
- rowsReturned: number;
1469
- /** Whether the result was truncated by the row limit */
1470
- isLimited: boolean;
1471
- /** The query/params that were executed */
1472
- queryExecuted?: string;
1473
- /** Execution time in milliseconds */
1474
- executionTimeMs: number;
2203
+ dashboardExists(dashboardId: string): boolean;
2204
+ /**
2205
+ * Get dashboard count
2206
+ * @returns Number of dashboards
2207
+ */
2208
+ getDashboardCount(): number;
1475
2209
  }
2210
+
1476
2211
  /**
1477
- * A pre-built, multi-step UI flow registered with the SDK.
1478
- *
1479
- * When the main agent decides a user's question matches a workflow's whenToUse
1480
- * trigger, it picks the workflow instead of running source agents / generating
1481
- * dashboard components. The LLM extracts the workflow's required props from the
1482
- * prompt (using `propsSchema` as the tool input_schema) and the SDK returns the
1483
- * workflow component directly — no analysis text, no chart generation. The
1484
- * frontend renders the registered workflow component with the LLM-extracted
1485
- * props.
2212
+ * ReportManager class to handle CRUD operations on reports
2213
+ * All operations read/write directly to files (no in-memory caching)
1486
2214
  */
1487
- interface WorkflowDescriptor {
1488
- /** Unique workflow id (used as the LLM tool name) */
1489
- id: string;
1490
- /** Component name on the frontend (matches the registered React component) */
1491
- name: string;
1492
- /** Short human-readable description of what this workflow does */
1493
- description: string;
2215
+ declare class ReportManager {
2216
+ private reportsBasePath;
2217
+ private projectId;
1494
2218
  /**
1495
- * 1–2 sentence trigger condition. The LLM uses this to decide if the
1496
- * user's prompt matches this workflow. Be specific — e.g.
1497
- * "User wants to *initiate* an inventory transfer (review + submit POs),
1498
- * not just see analysis or charts."
2219
+ * Initialize ReportManager with project ID
2220
+ * @param projectId - Project ID to use in file path
1499
2221
  */
1500
- whenToUse: string;
2222
+ constructor(projectId?: string);
1501
2223
  /**
1502
- * JSON-schema-style description of the props the workflow needs. Becomes
1503
- * the LLM tool's input_schema, so the model fills these from the prompt.
1504
- * Use the same shape as `params` on direct tools — string descriptors with
1505
- * an optional "(optional)" suffix.
1506
- *
1507
- * Example:
1508
- * ```
1509
- * {
1510
- * selectedStore: 'object — { id, name } of the source branch',
1511
- * minROI: 'number (optional) — only show transfers with ROI ≥ this',
1512
- * }
1513
- * ```
2224
+ * Get the file path for a specific report
2225
+ * @param reportId - Report ID
2226
+ * @returns Full path to report data.json file
1514
2227
  */
1515
- propsSchema: Record<string, string>;
2228
+ private getReportPath;
1516
2229
  /**
1517
- * Optional: static prop defaults merged with LLM-extracted props before
1518
- * the component is returned. Useful for things like the embedded
1519
- * `externalTool` config that the workflow uses to fetch its own data.
2230
+ * Create a new report
2231
+ * @param reportId - Unique report ID
2232
+ * @param report - Report data
2233
+ * @returns Created report with metadata
1520
2234
  */
1521
- defaultProps?: Record<string, any>;
2235
+ createReport(reportId: string, report: DSLRendererProps): DSLRendererProps;
2236
+ /**
2237
+ * Get a specific report by ID
2238
+ * @param reportId - Report ID
2239
+ * @returns Report data or null if not found
2240
+ */
2241
+ getReport(reportId: string): DSLRendererProps | null;
2242
+ /**
2243
+ * Get all reports
2244
+ * @returns Array of report objects with their IDs
2245
+ */
2246
+ getAllReports(): Array<{
2247
+ reportId: string;
2248
+ report: DSLRendererProps;
2249
+ }>;
2250
+ /**
2251
+ * Update an existing report
2252
+ * @param reportId - Report ID
2253
+ * @param report - Updated report data
2254
+ * @returns Updated report or null if not found
2255
+ */
2256
+ updateReport(reportId: string, report: DSLRendererProps): DSLRendererProps | null;
2257
+ /**
2258
+ * Delete a report
2259
+ * @param reportId - Report ID
2260
+ * @returns True if deleted, false if not found
2261
+ */
2262
+ deleteReport(reportId: string): boolean;
2263
+ /**
2264
+ * Check if a report exists
2265
+ * @param reportId - Report ID
2266
+ * @returns True if report exists, false otherwise
2267
+ */
2268
+ reportExists(reportId: string): boolean;
2269
+ /**
2270
+ * Get report count
2271
+ * @returns Number of reports
2272
+ */
2273
+ getReportCount(): number;
1522
2274
  }
2275
+
1523
2276
  /**
1524
- * The workflow selection captured during a routing call.
1525
- * Set on AgentResponse when the LLM picks a workflow tool.
2277
+ * ScriptRecipeStore — injected metadata backend for the script flow.
2278
+ *
2279
+ * The SDK is standalone (no DB dependency). The backend implements this
2280
+ * interface over Postgres (full-text search + atomic counters) and injects it
2281
+ * via `collections['script-recipes']`, exactly like `collections['source-embeddings']`.
2282
+ * `ScriptStore` consumes it for all METADATA operations while keeping the
2283
+ * executable body on disk as scripts-store/<fileBase>.ts.
2284
+ *
2285
+ * All metadata rows are plain JSON (no scriptBody — that lives on disk).
2286
+ * See backend/docs/SCRIPT-FLOW-SCALING-ISSUES.md (#1, #3, #7).
1526
2287
  */
1527
- interface SelectedWorkflow {
1528
- /** Component name (matches WorkflowDescriptor.name) */
2288
+
2289
+ /** One recipe's metadata as stored in Postgres (mirrors the script_recipes table). */
2290
+ interface ScriptRecipeMetaRow {
2291
+ id: string;
2292
+ projectId?: string | null;
2293
+ version: number;
1529
2294
  name: string;
1530
- /** Props extracted from the prompt + merged with workflow.defaultProps */
1531
- props: Record<string, any>;
2295
+ intentDescription: string;
2296
+ tags: string[] | null;
2297
+ createdFrom: string | null;
2298
+ sourceIds: string[] | null;
2299
+ tables: string[] | null;
2300
+ parameters: ScriptParameter[] | null;
2301
+ components?: ScriptComponentSpec[] | null;
2302
+ displayPreference?: ScriptDisplayPreference | null;
2303
+ fileBase: string;
2304
+ bodyHash?: string | null;
2305
+ successCount: number;
2306
+ failureCount: number;
2307
+ lastUsed: string | null;
2308
+ parentId?: string | null;
2309
+ forkDepth?: number | null;
2310
+ forkReason?: string | null;
2311
+ status: 'draft' | 'verified' | string;
2312
+ turnId?: string | null;
2313
+ editedBy?: string | null;
2314
+ editedFrom?: string | null;
2315
+ history?: ScriptVersionRecord[] | null;
2316
+ lastError?: {
2317
+ phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
2318
+ message: string;
2319
+ at: string;
2320
+ attempt: number;
2321
+ } | null;
2322
+ createdAt?: string | null;
2323
+ updatedAt?: string | null;
1532
2324
  }
2325
+ interface ScriptRecipeStore {
2326
+ /** FTS shortlist of healthy verified recipes for the matcher (metadata only). */
2327
+ search(params: {
2328
+ prompt: string;
2329
+ projectId?: string;
2330
+ limit?: number;
2331
+ }): Promise<ScriptRecipeMetaRow[]>;
2332
+ /** Fetch one recipe by id (any status). */
2333
+ getById(id: string): Promise<ScriptRecipeMetaRow | null>;
2334
+ /** Count healthy verified recipes (drives the "any scripts?" gate). */
2335
+ count(params?: {
2336
+ projectId?: string;
2337
+ }): Promise<number>;
2338
+ /** Insert or update a recipe row (keyed by id). */
2339
+ upsert(row: ScriptRecipeMetaRow): Promise<void>;
2340
+ /** Atomically bump counters / last-used. */
2341
+ updateStats(id: string, patch: {
2342
+ successDelta?: number;
2343
+ failureDelta?: number;
2344
+ lastUsed?: string;
2345
+ }): Promise<void>;
2346
+ /** Flip a draft to verified, applying provenance + optional fork lineage. */
2347
+ promote(id: string, patch: {
2348
+ sourceIds: string[];
2349
+ tables: string[];
2350
+ fileBase?: string;
2351
+ parentId?: string;
2352
+ forkDepth?: number;
2353
+ forkReason?: string;
2354
+ components?: ScriptComponentSpec[];
2355
+ }): Promise<ScriptRecipeMetaRow | null>;
2356
+ /**
2357
+ * Commit a verified edit onto an EXISTING recipe: bump `version`, replace the
2358
+ * body-bearing metadata, append a history record, reset health counters, and
2359
+ * clear the component specs (they were validated against the old shape).
2360
+ * Returns the updated row, or null when the target is gone.
2361
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 5.
2362
+ */
2363
+ commitEdit?(id: string, patch: {
2364
+ name?: string;
2365
+ intentDescription?: string;
2366
+ tags?: string[];
2367
+ parameters?: ScriptParameter[];
2368
+ bodyHash: string;
2369
+ /** New on-disk stem when the edit renamed the script; omitted otherwise. */
2370
+ fileBase?: string;
2371
+ sourceIds?: string[];
2372
+ tables?: string[];
2373
+ editedBy?: string;
2374
+ editedFrom?: string;
2375
+ historyEntry: {
2376
+ version: number;
2377
+ at: string;
2378
+ by?: string;
2379
+ instruction?: string;
2380
+ changeSummary?: string;
2381
+ bodyHash?: string;
2382
+ };
2383
+ }): Promise<ScriptRecipeMetaRow | null>;
2384
+ /** Stamp a draft's last execution error. */
2385
+ recordDraftError(id: string, err: {
2386
+ phase: string;
2387
+ message: string;
2388
+ attempt: number;
2389
+ at: string;
2390
+ }): Promise<void>;
2391
+ /** Delete a recipe row (body file removed separately). */
2392
+ remove(id: string): Promise<void>;
2393
+ /** True if `fileBase` is taken by a different recipe in this project. */
2394
+ fileBaseTaken(fileBase: string, excludeId: string, projectId?: string): Promise<boolean>;
2395
+ }
2396
+ /** Pull the injected store off the collections bag (or null if not wired). */
2397
+ declare function resolveScriptRecipeStore(collections: any): ScriptRecipeStore | null;
2398
+
1533
2399
  /**
1534
- * The complete response from the multi-agent system.
1535
- * Contains everything needed for text display + component generation.
2400
+ * ScriptStore — Postgres metadata + on-disk body for script recipes.
2401
+ *
2402
+ * Split of responsibilities:
2403
+ * - METADATA → injected `ScriptRecipeStore` (Postgres FTS + atomic counters),
2404
+ * resolved from `collections['script-recipes']`.
2405
+ * - BODY → scripts-store/<fileBase>.ts, editable in your IDE. Written
2406
+ * atomically (temp + rename); `bodyHash` (sha256) detects edits.
2407
+ *
2408
+ * The old "read every file every turn + send the whole catalog to the LLM"
2409
+ * matcher is gone — matching is `store.search(prompt)` (FTS shortlist). The
2410
+ * draft/verified filename dance is gone too: `status` is a DB column and the
2411
+ * file keeps a stable `<fileBase>.ts` name across promotion.
2412
+ *
2413
+ * When no metadata store is injected, the store degrades to a safe no-op
2414
+ * (count 0 → script flow disabled) instead of crashing.
2415
+ *
2416
+ * See backend/docs/SCRIPT-FLOW-SCALING-ISSUES.md.
1536
2417
  */
1537
- interface AgentResponse {
1538
- /** Generated text response (analysis of the data) */
1539
- text: string;
1540
- /** All executed tools across all source agents (for component generation) */
1541
- executedTools: ExecutedToolInfo[];
1542
- /** Individual results from each source agent */
1543
- sourceResults: SourceAgentResult[];
2418
+
2419
+ interface SaveDraftInput {
2420
+ /** Reuse an existing draft (retry); omit to mint a new one. */
2421
+ recipeId?: string;
2422
+ /** Per-turn unique suffix, stable across retries within the turn. */
2423
+ turnId: string;
2424
+ name: string;
2425
+ intentDescription: string;
2426
+ tags: string[];
2427
+ parameters: ScriptParameter[];
2428
+ scriptBody: string;
2429
+ createdFrom: string;
2430
+ /**
2431
+ * Set when this draft is a SHADOW of an existing recipe being edited. The
2432
+ * draft is verified independently and merged back onto the parent via
2433
+ * `commitEdit`, so the working script is never clobbered by an edit that
2434
+ * turns out not to run. See backend/docs/SCRIPT-EDIT-DESIGN.md § D5.
2435
+ */
2436
+ parentId?: string;
2437
+ }
2438
+ interface PromoteToVerifiedInput {
2439
+ sourceIds: string[];
2440
+ tables: string[];
2441
+ parentId?: string;
2442
+ forkDepth?: number;
2443
+ forkReason?: string;
2444
+ components?: ScriptComponentSpec[];
2445
+ }
2446
+ interface ScriptStoreOptions {
2447
+ /** Explicit metadata store, or resolved from `collections['script-recipes']`. */
2448
+ store?: ScriptRecipeStore | null;
2449
+ collections?: any;
2450
+ /** Body directory (defaults to <cwd>/scripts-store). */
2451
+ baseDir?: string;
2452
+ /** Project scope stamped on every row. */
2453
+ projectId?: string;
2454
+ }
2455
+ declare function normalizeScriptBody(scriptBody: string): string;
2456
+ declare class ScriptStore {
2457
+ private store;
2458
+ private storeDir;
2459
+ private projectId?;
2460
+ constructor(opts?: ScriptStoreOptions);
2461
+ /** Whether a metadata store is wired (matcher / authoring are gated on this). */
2462
+ hasStore(): boolean;
2463
+ /** Number of healthy verified recipes (gates the script-matching path). */
2464
+ count(): Promise<number>;
2465
+ /**
2466
+ * FTS shortlist for the matcher (metadata only — bodies are loaded lazily by
2467
+ * `get()` once the LLM picks one). Returns verified, healthy recipes ranked
2468
+ * by relevance.
2469
+ */
2470
+ search(prompt: string, limit?: number): Promise<ScriptRecipe[]>;
2471
+ /** Fetch one recipe by id with its body loaded from disk. */
2472
+ get(id: string): Promise<ScriptRecipe | null>;
2473
+ /** Create or update a recipe (metadata upsert + body write when changed). */
2474
+ save(recipe: ScriptRecipe): Promise<void>;
2475
+ /**
2476
+ * Persist (or update) a draft. Within a turn, retries that pass the same
2477
+ * `recipeId` overwrite the same row + file; a fresh `recipeId` mints a new
2478
+ * draft. The body is visible at scripts-store/<fileBase>.ts immediately.
2479
+ */
2480
+ saveDraft(input: SaveDraftInput): Promise<ScriptRecipe>;
2481
+ /** Stamp a draft's last execution error (metadata only). */
2482
+ recordDraftError(recipeId: string, err: {
2483
+ phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
2484
+ message: string;
2485
+ attempt: number;
2486
+ }): Promise<void>;
2487
+ /**
2488
+ * Promote a successfully-executed draft into a verified script.
2489
+ * The on-disk body already exists at <fileBase>.ts (written at write_script
2490
+ * time) and keeps its name — only the DB row flips status + provenance.
2491
+ */
2492
+ promoteToVerified(recipeId: string, input: PromoteToVerifiedInput): Promise<ScriptRecipe | null>;
2493
+ /**
2494
+ * Commit a user-directed edit: merge a VERIFIED shadow draft back onto the
2495
+ * recipe it was editing, as version N+1 under the SAME recipe id.
2496
+ *
2497
+ * Keeping the id stable is the point of the whole feature — every cached
2498
+ * conversation, `script_dataset` regeneration descriptor and persisted
2499
+ * component spec already points at it, so the correction applies retroactively
2500
+ * to replays instead of stranding them on the old body.
2501
+ *
2502
+ * Steps (see backend/docs/SCRIPT-EDIT-DESIGN.md § Component 5):
2503
+ * 1. archive the current body as `<fileBase>.v<version>.ts`
2504
+ * 2. write the edited body to the original fileBase (atomic)
2505
+ * 3. version++, copy name/description/params, append a history record
2506
+ * 4. reset health counters — the pre-edit failure history is stale
2507
+ * 5. clear component specs — they were validated against the OLD shape
2508
+ * 6. delete the shadow draft (row + file)
2509
+ *
2510
+ * Returns the updated recipe, or null when the commit could not be applied
2511
+ * (caller should then fall back to treating the draft as a new script).
2512
+ */
2513
+ commitEdit(targetId: string, draft: {
2514
+ recipeId: string;
2515
+ name?: string;
2516
+ intentDescription?: string;
2517
+ tags?: string[];
2518
+ parameters?: ScriptParameter[];
2519
+ scriptBody: string;
2520
+ sourceIds?: string[];
2521
+ tables?: string[];
2522
+ }, meta: {
2523
+ instruction?: string;
2524
+ changeSummary?: string;
2525
+ editedBy?: string;
2526
+ }): Promise<ScriptRecipe | null>;
2527
+ /**
2528
+ * Drop a draft (row + body file). MainAgent calls this at end-of-turn when a
2529
+ * draft was authored but never verified — failed drafts are never matched, so
2530
+ * deleting them immediately avoids unbounded accumulation (#5). No-op if the
2531
+ * recipe isn't a draft (so a promoted/verified script is never removed here).
2532
+ */
2533
+ discardDraft(recipeId: string): Promise<void>;
2534
+ /** Delete a recipe (row + body file). */
2535
+ delete(id: string): Promise<void>;
2536
+ /** Record a successful execution (atomic counter bump). */
2537
+ recordSuccess(id: string): Promise<void>;
2538
+ /** Record a failed execution (atomic counter bump). */
2539
+ recordFailure(id: string): Promise<void>;
2540
+ /** Absolute path to the .ts body for a recipe (used by the runner/MainAgent). */
2541
+ getScriptPath(recipe: ScriptRecipe): string;
2542
+ private removeById;
2543
+ private rowToRecipe;
2544
+ private recipeToRow;
2545
+ /** slug of name, with a short id suffix when the bare slug is already taken. */
2546
+ private computeFileBase;
2547
+ private toSlug;
2548
+ private hash;
2549
+ /**
2550
+ * Append (or replace) the `getAnalysis` export on an existing draft body.
2551
+ *
2552
+ * The narrative is authored AFTER execution, so it lands on a file that
2553
+ * already holds a working getData — rewriting the whole body would risk
2554
+ * losing the query that just succeeded. Replaces any earlier getAnalysis so
2555
+ * a retry does not stack duplicate exports.
2556
+ */
2557
+ attachAnalysis(recipeId: string, analysisBody: string): Promise<boolean>;
2558
+ private bodyPath;
2559
+ private readBody;
2560
+ /** Directory holding superseded bodies. Dot-prefixed so IDEs/`ls` hide it. */
2561
+ private get archiveDir();
2562
+ /**
2563
+ * Archive a superseded body as `.versions/<fileBase>.v<n>.ts`.
2564
+ *
2565
+ * Kept out of the main store directory on purpose — see commitEdit step 1.
2566
+ * To roll back: copy the file back over `scripts-store/<fileBase>.ts`.
2567
+ */
2568
+ private writeArchive;
1544
2569
  /**
1545
- * Set when the LLM routed the question to a registered workflow component.
1546
- * When present, the upstream caller should skip component generation and
1547
- * return this workflow as the response.
2570
+ * Move a recipe's archived versions to a new prefix when its fileBase changes,
2571
+ * so all versions of one recipe stay grouped. Without this, two renames would
2572
+ * scatter a single recipe's history across three prefixes in `.versions/` with
2573
+ * nothing linking them back to the live script.
1548
2574
  */
1549
- workflow?: SelectedWorkflow;
1550
- }
1551
- /**
1552
- * Configuration for the multi-agent system.
1553
- * Controls limits, models, and behavior.
1554
- */
1555
- interface AgentConfig {
1556
- /** Max rows a source agent can return (default: 50) */
1557
- maxRowsPerSource: number;
1558
- /** Model for the main agent (routing + analysis in one LLM call) */
1559
- mainAgentModel: string;
1560
- /** Model for source agent query generation */
1561
- sourceAgentModel: string;
1562
- /** API key for LLM calls */
1563
- apiKey?: string;
1564
- /** Max retry attempts per source agent */
1565
- maxRetries: number;
1566
- /** Max tool calling iterations for the main agent loop */
1567
- maxIterations: number;
1568
- /** Global knowledge base context (static, same for all users/questions — cached in system prompt) */
1569
- globalKnowledgeBase?: string;
1570
- /** Per-request knowledge base context (user-specific + query-matched — dynamic, not cached) */
1571
- knowledgeBaseContext?: string;
2575
+ private renameArchives;
2576
+ /** Atomic body write (temp + rename) so concurrent reads never see a partial file. */
2577
+ private writeBody;
2578
+ private unlinkBody;
1572
2579
  }
1573
- /**
1574
- * Default agent configuration
1575
- */
1576
- declare const DEFAULT_AGENT_CONFIG: AgentConfig;
1577
2580
 
1578
2581
  /**
1579
2582
  * Main Agent (Orchestrator)
@@ -1595,7 +2598,60 @@ declare class MainAgent {
1595
2598
  private workflows;
1596
2599
  private config;
1597
2600
  private streamBuffer;
1598
- constructor(externalTools: ExternalTool[], config: AgentConfig, streamBuffer?: StreamBuffer, workflows?: WorkflowDescriptor[]);
2601
+ /**
2602
+ * Optional: when provided, MainAgent exposes the `write_script` /
2603
+ * `execute_script` tools to the LLM and persists drafts to disk via the
2604
+ * store. Headless callers (alert analyzer, metric resolver) omit these to
2605
+ * suppress script authoring entirely — drafts would otherwise leak onto
2606
+ * disk with no caller to promote or clean them up.
2607
+ */
2608
+ private scriptStore;
2609
+ private turnId;
2610
+ private createdFromPrompt;
2611
+ private scriptState;
2612
+ /** Answer-flow component catalog (filtered + projected by the caller). */
2613
+ private componentCatalog;
2614
+ /** Specs accepted by `render_components` this turn; empty until it succeeds. */
2615
+ /**
2616
+ * Entities resolved this turn, appended verbatim to every source dispatch.
2617
+ *
2618
+ * Not left to the model to copy into its intent prose: observed live, the
2619
+ * MainAgent wrote names instead of ids on the first dispatch AND re-spelled
2620
+ * 'F D C LIMITED' as 'FDC Limited', so the source agent fell back to
2621
+ * `PartyName LIKE '%FDC%'` and found 2 of 3 customers. Carrying the ids
2622
+ * structurally makes that impossible rather than instruction-dependent.
2623
+ */
2624
+ private resolvedEntities;
2625
+ private componentSpecs;
2626
+ private componentLayout;
2627
+ private renderComponentAttempts;
2628
+ /**
2629
+ * Fork mode — set when this turn is adapting a near-matching parent script.
2630
+ * In fork mode there is no legitimate "answer with bare text" outcome: the
2631
+ * only correct first move is a tool call (write_script, or a source tool for
2632
+ * schema discovery). We therefore force tool use on the first LLM iteration
2633
+ * so the model can't end its turn with a bare "I'll adapt…" preamble and zero
2634
+ * tool calls. Never set on the fresh-authoring / general-question path.
2635
+ */
2636
+ private forkMode;
2637
+ /**
2638
+ * Edit mode — set when this turn applies a user-directed change to an
2639
+ * existing script. Swaps the system prompt to `agent-main-edit` and stamps
2640
+ * the shadow draft's parentId. Like fork mode there is no legitimate
2641
+ * "answer with bare text" outcome, so tool use is forced on iteration 1.
2642
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 4.
2643
+ */
2644
+ private editContext;
2645
+ /**
2646
+ * Per-turn cancellation signal (user hit "Stop"). Set at the top of
2647
+ * handleQuestion and read by the tool handler, the SourceAgent dispatch, and
2648
+ * the script subprocess so an abort tears down every layer of the turn.
2649
+ */
2650
+ private abortSignal?;
2651
+ constructor(externalTools: ExternalTool[], config: AgentConfig, scriptStore?: ScriptStore, turnId?: string, streamBuffer?: StreamBuffer, workflows?: WorkflowDescriptor[], forkMode?: boolean, editContext?: EditContext, componentCatalog?: Component[]);
2652
+ /** True when the turn is applying a user-directed script edit. */
2653
+ private get editMode();
2654
+ private get scriptingEnabled();
1599
2655
  /**
1600
2656
  * Handle a user question using the multi-agent system.
1601
2657
  *
@@ -1606,7 +2662,92 @@ declare class MainAgent {
1606
2662
  * 4. Direct tools → fn() called directly with LLM params → returns data
1607
2663
  * 5. Generates final analysis text
1608
2664
  */
1609
- handleQuestion(userPrompt: string, apiKey?: string, conversationHistory?: string, streamCallback?: (chunk: string) => void): Promise<AgentResponse>;
2665
+ handleQuestion(userPrompt: string, apiKey?: string, conversationHistory?: string, streamCallback?: (chunk: string) => void, signal?: AbortSignal): Promise<AgentResponse>;
2666
+ /**
2667
+ * Tool definition, with the catalog rendered from the registered `Metadata`
2668
+ * the caller passed in. Generated rather than hand-written so it cannot drift
2669
+ * from what the frontend actually ships.
2670
+ */
2671
+ private buildRenderComponentsToolDef;
2672
+ /**
2673
+ * Validate the agent's component picks against the REAL columns of each bound
2674
+ * dataset. All-or-nothing: any error rejects the whole call and returns the
2675
+ * findings, because a partially-accepted dashboard makes the retry ambiguous.
2676
+ */
2677
+ private handleRenderComponents;
2678
+ /**
2679
+ * Author the answer as a program. Runs after execute_script, so the body is
2680
+ * written with the result already known and can react to what it found.
2681
+ *
2682
+ * Rejects a body that types numbers rather than computing them — see
2683
+ * analysis-lint. A hardcoded figure is worse than prose: it freezes into the
2684
+ * recipe and replays unchanged while the data moves underneath it.
2685
+ */
2686
+ private handleWriteAnalysis;
2687
+ private handleWriteScript;
2688
+ private handleExecuteScript;
2689
+ /**
2690
+ * Build the AgentWrittenScript payload the caller will hand to
2691
+ * `ScriptStore.promoteToVerified()`. Only returned when a verified
2692
+ * successful execution is on record.
2693
+ */
2694
+ private buildSavedScript;
2695
+ private normalizeParameterList;
2696
+ /**
2697
+ * Use the schema embedding collection to pre-select relevant tables for
2698
+ * this source + intent. Returns a formatted schema block if confidence is
2699
+ * high (top match ≥ 0.55 and ≥3 candidates), otherwise null.
2700
+ *
2701
+ * When this returns a block, we can skip the SourceAgent's `search_schema`
2702
+ * loop and reduce iteration budget. When it returns null, the SourceAgent
2703
+ * falls back to the existing LLM-driven keyword search (same as today).
2704
+ */
2705
+ /**
2706
+ * Ids + verbatim names for everything resolved this turn, appended by CODE to
2707
+ * each source dispatch so neither omission nor re-spelling by the model can
2708
+ * lose them. Empty string when nothing was resolved, so intents are untouched
2709
+ * for questions that name no entities.
2710
+ *
2711
+ * Also appends `attrs.scopeSql` when the resolved instance carries one —
2712
+ * same "guaranteed by code" reasoning as the ids/names above, extended to
2713
+ * the one attrs field with a documented, cross-entity-type meaning: a
2714
+ * ready-made SQL predicate an entity's index precomputed (see
2715
+ * ENTITY-SEARCH-DESIGN.md's hierarchy-broadening pattern — branch/region/
2716
+ * state entity types attach it so a lane/route question never needs its
2717
+ * own city-matching join). Confirmed real cost of not doing this: a
2718
+ * branch/state resolved with scopeSql sat unused while the source agent
2719
+ * spent 4 failed exploratory queries rediscovering the exact same
2720
+ * branch-id list by hand (Invalid column name 'StateName'/'CityId'/
2721
+ * 'OrganizationLocationId', in that order, ~144s, before landing on ids
2722
+ * that were sitting in scopeSql from the very first resolve_entities
2723
+ * call). Other attrs (code, counts, etc.) are not dumped here — they are
2724
+ * descriptive, not actionable the way a precomputed predicate is, and
2725
+ * bloating every dispatch with all of them for no clear gain isn't worth
2726
+ * it; scopeSql is the one thing worth this guarantee today.
2727
+ */
2728
+ private buildResolvedEntitiesBlock;
2729
+ /** True when the backend registered the entity-search collection. */
2730
+ private get entitySearchEnabled();
2731
+ /**
2732
+ * Compact catalog (type + gloss) for the system prompt. This is what lets the
2733
+ * LLM NAME a concept instead of guessing at our vocabulary — it can infer
2734
+ * that a question is about unpaid money, but not that we call that
2735
+ * `outstanding-balance`. Full `resolution` detail is fetched by the tool.
2736
+ *
2737
+ * Deliberately NOT cached in-process. The token cost is already handled by
2738
+ * prompt caching (the system prompt carries a cache_control breakpoint), and
2739
+ * re-reading a tiny PK-ordered table costs ~nothing. Fetching fresh does not
2740
+ * weaken the prompt cache either: unchanged data renders byte-identical text,
2741
+ * so the prefix still hits. A process cache would only buy a staleness window
2742
+ * in which a newly indexed entity is invisible to the agent.
2743
+ */
2744
+ private loadEntityCatalog;
2745
+ /**
2746
+ * Never throws: a resolver outage must degrade to "nothing resolved" and
2747
+ * leave the agent exactly as it was, not take the whole answer down.
2748
+ */
2749
+ private handleResolveEntities;
2750
+ private preResolveSchema;
1610
2751
  /**
1611
2752
  * Execute a direct tool — call fn() with LLM-provided params, no SourceAgent.
1612
2753
  */
@@ -1672,7 +2813,45 @@ interface LLMOptions {
1672
2813
  temperature?: number;
1673
2814
  topP?: number;
1674
2815
  apiKey?: string;
2816
+ baseURL?: string;
1675
2817
  partial?: (chunk: string) => void;
2818
+ /**
2819
+ * Per-request cancellation. When the caller aborts this signal (user hit
2820
+ * "Stop"), the underlying provider request is cancelled and the call throws
2821
+ * a RequestAbortedError. Threaded into the provider `messages.create` request
2822
+ * options and checked between tool-loop iterations. Currently honored on the
2823
+ * Anthropic path (the agent flow's default provider).
2824
+ */
2825
+ signal?: AbortSignal;
2826
+ /**
2827
+ * Forces a tool call on the FIRST iteration of streamWithTools only
2828
+ * (subsequent iterations revert to auto). Used by fork mode to stop the
2829
+ * model from ending its turn with a bare "I'll adapt the script…" preamble
2830
+ * and zero tool calls. `{ type: 'any' }` lets the model pick which tool
2831
+ * (write_script in the common case, a source tool for schema discovery);
2832
+ * `{ type: 'tool', name }` pins a specific tool. Honored on both the
2833
+ * Anthropic path and the OpenAI/OpenRouter path (mapped to OpenAI's
2834
+ * tool_choice: 'required' / a named function).
2835
+ */
2836
+ firstIterationToolChoice?: {
2837
+ type: 'any';
2838
+ } | {
2839
+ type: 'tool';
2840
+ name: string;
2841
+ };
2842
+ /**
2843
+ * Internal — set only by the OpenRouter wrappers when the target is a Claude
2844
+ * model. Tells the OpenAI-wire path to emit Anthropic `cache_control`
2845
+ * breakpoints (OpenRouter forwards them to Anthropic for prompt caching).
2846
+ * Never set for direct OpenAI/Groq calls, so their requests are unchanged.
2847
+ */
2848
+ _openrouterClaudeCaching?: boolean;
2849
+ /**
2850
+ * Internal — OpenRouter provider-routing preferences (forwarded as the
2851
+ * `provider` body field). Set by the OpenRouter wrappers to steer routing to
2852
+ * a fast backend (e.g. {sort:'throughput'}). Never set for direct OpenAI/Groq.
2853
+ */
2854
+ _openrouterProvider?: Record<string, unknown>;
1676
2855
  }
1677
2856
  interface Tool {
1678
2857
  name: string;
@@ -1694,6 +2873,16 @@ declare class LLM {
1694
2873
  * @returns Normalized system prompt for Anthropic API
1695
2874
  */
1696
2875
  private static _normalizeSystemPrompt;
2876
+ /**
2877
+ * Strip unpaired UTF-16 surrogates from every text field of a message set.
2878
+ *
2879
+ * A lone surrogate (from mid-pair string slicing or corrupt source data)
2880
+ * serializes to a bare `\udXXX` escape that strict JSON parsers — including
2881
+ * the one on Anthropic's API — reject with "no low surrogate in string",
2882
+ * failing the whole request. Sanitizing here, at the single boundary every
2883
+ * provider call flows through, guarantees no request can carry one.
2884
+ */
2885
+ private static _sanitizeMessages;
1697
2886
  /**
1698
2887
  * Log cache usage metrics from Anthropic API response
1699
2888
  * Shows cache hits, costs, and savings
@@ -1710,11 +2899,46 @@ declare class LLM {
1710
2899
  * "claude-sonnet-4-5" → ["anthropic", "claude-sonnet-4-5"] (default)
1711
2900
  */
1712
2901
  private static _parseModel;
2902
+ /**
2903
+ * Map an Anthropic model id (e.g. "claude-sonnet-4-5-20250929") to the OpenRouter slug
2904
+ * (e.g. "claude-sonnet-4.5"). OpenRouter slugs drop the date suffix and use dotted versions.
2905
+ */
2906
+ private static _toOpenRouterSlug;
2907
+ /**
2908
+ * Per-provider proxy base URL. Returns `${SUPERATOM_LLM_PROXY_URL}/<provider>`
2909
+ * when our Cloudflare LLM proxy is configured, else undefined (→ talk to the
2910
+ * provider directly, legacy behaviour). An explicit options.baseURL (e.g.
2911
+ * OpenRouter) always wins and is never overridden. See backend/docs/llm-proxy.md.
2912
+ */
2913
+ private static _proxyBaseURL;
2914
+ private static _openrouterOptions;
2915
+ private static _isRetryableProviderError;
2916
+ private static _withOpenrouterRetry;
2917
+ private static _openrouterText;
2918
+ private static _openrouterStream;
2919
+ private static _openrouterStreamWithTools;
2920
+ /**
2921
+ * Build an Anthropic client. Routes through our Cloudflare LLM proxy when
2922
+ * SUPERATOM_LLM_PROXY_URL is set (each client ships a per-client proxy key as
2923
+ * ANTHROPIC_API_KEY and never holds the real key); otherwise talks to
2924
+ * api.anthropic.com directly. See backend/docs/llm-proxy.md.
2925
+ */
2926
+ private static _anthropicClient;
2927
+ /** True when OpenRouter is configured as a fail-open fallback for Claude. */
2928
+ private static _openrouterAvailable;
2929
+ /** Remap an Anthropic model id to the OpenRouter model path for fail-open. */
2930
+ private static _anthropicFallbackModel;
1713
2931
  private static _anthropicText;
1714
2932
  private static _anthropicStream;
1715
2933
  private static _anthropicStreamWithTools;
1716
2934
  private static _groqText;
1717
2935
  private static _groqStream;
2936
+ /**
2937
+ * Gemini request options carrying the proxy base URL, or undefined → talk to
2938
+ * generativelanguage.googleapis.com directly. The Google SDK takes baseUrl as a
2939
+ * per-model request option, not a constructor arg. See backend/docs/llm-proxy.md.
2940
+ */
2941
+ private static _geminiRequestOptions;
1718
2942
  private static _geminiText;
1719
2943
  private static _geminiStream;
1720
2944
  /**
@@ -1723,8 +2947,28 @@ declare class LLM {
1723
2947
  */
1724
2948
  private static _cleanSchemaForGemini;
1725
2949
  private static _geminiStreamWithTools;
2950
+ /** True for Anthropic/Claude model ids — gates OpenRouter prompt caching. */
2951
+ private static _isClaudeModel;
2952
+ /**
2953
+ * Build the OpenAI-wire system message. For OpenRouter + Claude
2954
+ * (cacheClaude=true) it emits content parts carrying Anthropic
2955
+ * `cache_control` breakpoints (preserving any the caller set, else marking
2956
+ * the last block), so OpenRouter forwards them to Anthropic for prompt
2957
+ * caching. Otherwise it returns a plain flattened string — unchanged for
2958
+ * direct OpenAI/Groq.
2959
+ */
2960
+ private static _openaiSystemMessage;
2961
+ /**
2962
+ * Split an OpenAI-wire usage object. `prompt_tokens` INCLUDES cached tokens,
2963
+ * so we subtract them out (Anthropic-style: input excludes cache reads) and
2964
+ * report cached separately — this makes calculateCost price cache reads at
2965
+ * the discounted rate and reflects OpenRouter prompt-cache savings in logs.
2966
+ */
2967
+ private static _openaiUsage;
1726
2968
  private static _openaiText;
1727
2969
  private static _openaiStream;
2970
+ /** Map the Anthropic-style firstIterationToolChoice to OpenAI's tool_choice. */
2971
+ private static _openaiToolChoice;
1728
2972
  private static _openaiStreamWithTools;
1729
2973
  /**
1730
2974
  * Parse JSON string, handling markdown code blocks and surrounding text
@@ -1826,6 +3070,13 @@ declare class UIBlock {
1826
3070
  private textResponse;
1827
3071
  private actions;
1828
3072
  private createdAt;
3073
+ /**
3074
+ * Which script recipe produced this answer, when a script did. Read on the
3075
+ * NEXT turn so the user can say "use mode instead" and have the matcher
3076
+ * resolve it to a concrete script (the `edit` tier is unreachable without it).
3077
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 1.
3078
+ */
3079
+ private scriptBinding;
1829
3080
  /**
1830
3081
  * Creates a new UIBlock instance
1831
3082
  * @param userQuestion - The user's question or input
@@ -1917,9 +3168,22 @@ declare class UIBlock {
1917
3168
  * Clear all actions
1918
3169
  */
1919
3170
  clearActions(): void;
3171
+ /** The program-generated half of the answer — see setProgramAnswer. */
3172
+ programAnswer?: string | null;
1920
3173
  /**
1921
- * Get creation timestamp
3174
+ * Bind this block to the script recipe that produced its answer.
1922
3175
  */
3176
+ setScriptBinding(binding: Record<string, any> | null): void;
3177
+ /**
3178
+ * The program-generated half of the answer, kept verbatim so a reload can
3179
+ * replace exactly that text without disturbing the streamed narration.
3180
+ */
3181
+ setProgramAnswer(text: string | null): void;
3182
+ getProgramAnswer(): string | null;
3183
+ /**
3184
+ * The script recipe bound to this block, if any.
3185
+ */
3186
+ getScriptBinding(): Record<string, any> | null;
1923
3187
  getCreatedAt(): Date;
1924
3188
  /**
1925
3189
  * Convert UIBlock to JSON-serializable object
@@ -1987,6 +3251,32 @@ declare class Thread {
1987
3251
  * @param currentUIBlockId - ID of current UIBlock to exclude from context (optional)
1988
3252
  * @returns Formatted conversation history string
1989
3253
  */
3254
+ /**
3255
+ * The script recipe bound to the most recent completed UIBlock — i.e. the
3256
+ * script behind the answer the user is currently looking at. Drives the
3257
+ * matcher's `edit` tier: without it, "use mode instead" has no target and
3258
+ * falls through to regeneration.
3259
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 1.
3260
+ */
3261
+ getActiveScriptBinding(currentUIBlockId?: string): Record<string, any> | null;
3262
+ /**
3263
+ * The recent script-backed answers in this thread, newest first — the
3264
+ * candidate set a user-directed edit can target.
3265
+ *
3266
+ * Returning several rather than only the newest is what lets an instruction
3267
+ * name its own target ("use the mode for the WSP one"). With a single
3268
+ * candidate every edit lands on the most recent script, which silently edits
3269
+ * the wrong recipe whenever the user meant an earlier one.
3270
+ *
3271
+ * Deduped by recipeId (newest occurrence wins) so a long editing session on
3272
+ * one script doesn't crowd out the others. Each entry carries the question
3273
+ * that produced it — without that the candidates are indistinguishable.
3274
+ *
3275
+ * In-memory only: dies with the process. The caller falls back to the
3276
+ * persisted bindings when this comes back empty.
3277
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 1.
3278
+ */
3279
+ getScriptBindings(limit?: number, currentUIBlockId?: string): Record<string, any>[];
1990
3280
  getConversationContext(limit?: number, currentUIBlockId?: string): string;
1991
3281
  /**
1992
3282
  * Convert Thread to JSON-serializable object
@@ -2649,7 +3939,7 @@ declare abstract class BaseLLM {
2649
3939
  * This helps provide intelligent suggestions for follow-up queries
2650
3940
  * For general/conversational questions without components, pass textResponse instead
2651
3941
  */
2652
- generateNextQuestions(originalUserPrompt: string, component?: Component | null, componentData?: Record<string, unknown>, apiKey?: string, conversationHistory?: string, textResponse?: string): Promise<string[]>;
3942
+ generateNextQuestions(originalUserPrompt: string, component?: Component | null, componentData?: Record<string, unknown>, apiKey?: string, conversationHistory?: string, textResponse?: string, signal?: AbortSignal): Promise<string[]>;
2653
3943
  }
2654
3944
 
2655
3945
  interface AnthropicLLMConfig extends BaseLLMConfig {
@@ -2855,6 +4145,187 @@ declare class DashboardConversationHistory {
2855
4145
  }
2856
4146
  declare const dashboardConversationHistory: DashboardConversationHistory;
2857
4147
 
4148
+ /**
4149
+ * Whole-dashboard generation via Pi, a terminal coding agent — as opposed to
4150
+ * DASH_COMP_REQ's single-widget-at-a-time flow. Runs Pi in-process via its
4151
+ * SDK (createAgentSession), not as a subprocess: no shell, no argument
4152
+ * quoting, no stdin/stdout piping, none of the Windows-specific subprocess
4153
+ * issues that came with spawning the `pi` CLI directly.
4154
+ *
4155
+ * Called from sdk-nodejs/src/dashboardAgent/index.ts (DASHBOARD_AGENT_REQ),
4156
+ * which owns the generic streaming/abort machinery (mirrors USER_PROMPT_REQ)
4157
+ * and passes `signal`/`onProgress` alongside the normal params — this stays
4158
+ * within CollectionHandler's loose (params) => Promise<result> typing, no
4159
+ * change needed to that shared type.
4160
+ *
4161
+ * This mechanism is generic and reusable across any deployment. What's
4162
+ * genuinely project-specific — where AGENTS.md lives, which model to use —
4163
+ * is supplied via `DashboardAgentCollectionConfig`, with defaults sensible
4164
+ * enough that most callers don't need to override them (see below). The
4165
+ * data-source tool list and the dashboard's current state both come from
4166
+ * things sdk-nodejs already exposes generically: `sdk.getTools()` (whatever
4167
+ * this deployment registered via `sdk.setTools()`) and `sdk.callCollection
4168
+ * ('dashboards', 'query', ...)` (whatever this deployment already registered
4169
+ * under that name/shape) — no per-deployment callback needed for either.
4170
+ * Same convention on the way out: after a successful run, the prompt and the
4171
+ * full response text are handed to `sdk.callCollection('dashboard-agent-
4172
+ * conversations', 'create', ...)` if this deployment has registered one —
4173
+ * skipped silently otherwise, since conversation history is optional.
4174
+ *
4175
+ * Pi verifies every query against the live database itself (via whatever
4176
+ * local tool-execution bridge the deployment exposes, e.g. an HTTP bridge
4177
+ * on localhost), but does NOT persist the result itself — it writes the
4178
+ * finished DSL to an absolute path inside `runtimeDir`, told to it explicitly
4179
+ * in the prompt, and stops there. This handler reads that file after the run
4180
+ * finishes and returns its content as `dashboard` in the result. The caller
4181
+ * (frontend) is the one that actually saves it, via whatever authenticated
4182
+ * create/update path any other dashboard edit goes through — Pi has no user
4183
+ * session/auth context of its own, so persistence shouldn't happen from
4184
+ * inside it.
4185
+ *
4186
+ * Session persistence: the FIRST call for a dashboardId pays the full cost
4187
+ * (explore KB, discover schema, plan, verify, build). Every call after that
4188
+ * resumes the same session file (SessionManager.open) so Pi has everything
4189
+ * it already learned — it only needs to reason about the new, smaller ask,
4190
+ * not rediscover the whole dashboard from scratch. The session's file path
4191
+ * (AgentSession.sessionFile) is captured right after creation and persisted
4192
+ * in a small local file, keyed by dashboardId, inside `runtimeDir`.
4193
+ */
4194
+ interface DashboardAgentCollectionConfig {
4195
+ /**
4196
+ * Working directory Pi runs from — must contain AGENTS.md. This is a
4197
+ * version-controlled prompt file, so `cwd` is expected to live somewhere
4198
+ * like a `.prompts/` folder alongside the deployment's other prompts.
4199
+ * Default: `<process.cwd()>/.prompts/dashboard-agent` — the same
4200
+ * process.cwd()-based convention PromptLoader already uses for the main
4201
+ * agent's prompts, which needs no explicit override in the common case
4202
+ * (the backend process's own cwd already is its project root).
4203
+ */
4204
+ cwd?: string;
4205
+ /**
4206
+ * Where drafts/, the session-id map, and dashboard.log get written —
4207
+ * separate from `cwd` deliberately, so this deployment's runtime state
4208
+ * (regenerated per session, safe to gitignore) doesn't sit inside the
4209
+ * same folder as the version-controlled AGENTS.md prompt.
4210
+ * Default: `<process.cwd()>/.pi-dashboard-agent-runtime`.
4211
+ */
4212
+ runtimeDir?: string;
4213
+ /** Model provider (default: process.env.PI_AGENT_PROVIDER || 'openrouter'). */
4214
+ provider?: string;
4215
+ /** Model id (default: process.env.PI_AGENT_MODEL || 'anthropic/claude-sonnet-4.5'). */
4216
+ model?: string;
4217
+ /**
4218
+ * true (default): every request starts a brand-new pi session, with the 2
4219
+ * most recent prior responses (if any) injected into the prompt as
4220
+ * context — bounded cost per request, but pi re-explores schema/KB facts
4221
+ * it already verified in an earlier turn on this same dashboard.
4222
+ * false: resumes the same session file across requests on a given
4223
+ * dashboard — pi keeps everything it already learned, but context (and
4224
+ * cost) grows unbounded across turns (one observed turn: 2M+ cache-read
4225
+ * tokens after a handful of edits on the same dashboard).
4226
+ * Default: process.env.PI_AGENT_FRESH_SESSION !== 'false'.
4227
+ */
4228
+ freshSession?: boolean;
4229
+ }
4230
+ declare function registerDashboardAgentCollection(sdk: SuperatomSDK, config?: DashboardAgentCollectionConfig): void;
4231
+
4232
+ /**
4233
+ * ScriptMatcher — LLM-Based Script Matching + Parameter Extraction
4234
+ *
4235
+ * Uses ONE LLM call to:
4236
+ * 1. Pick the best matching script from the library (or "none")
4237
+ * 2. Extract parameter values from the user question
4238
+ *
4239
+ * Why LLM over embeddings:
4240
+ * - Embeddings capture topic similarity ("overstock" ≈ "inventory" ≈ "revenue")
4241
+ * but can't distinguish structurally different questions about the same domain
4242
+ * - LLM understands that "overstock by warehouse" needs a different script than
4243
+ * "revenue by warehouse" even though they're semantically close
4244
+ * - One call does both matching AND parameter extraction
4245
+ *
4246
+ * When script library grows past ~50, add an embedding pre-filter
4247
+ * (ChromaDB narrows to top 10 → LLM picks from those 10).
4248
+ */
4249
+
4250
+ declare class ScriptMatcher {
4251
+ private store;
4252
+ constructor(store: ScriptStore);
4253
+ /**
4254
+ * Find the best matching script for a user question.
4255
+ * Uses ONE LLM call that picks the script AND extracts parameters.
4256
+ * Returns null if no script matches.
4257
+ */
4258
+ match(userPrompt: string, apiKey?: string, model?: string, signal?: AbortSignal,
4259
+ /**
4260
+ * Recent script-backed answers in this thread, newest first. Presence of at
4261
+ * least one is what makes the `edit` tier reachable at all (see the guards
4262
+ * below), and handing over SEVERAL is what lets an instruction name its own
4263
+ * target instead of always hitting the most recent script.
4264
+ */
4265
+ activeBindings?: ScriptBinding[],
4266
+ /**
4267
+ * Recent conversation turns, most recent last. The gate needs this to tell
4268
+ * "this reply is an edit instruction for the active script" apart from
4269
+ * "this reply is answering a clarifying question about a DIFFERENT,
4270
+ * unresolved topic that never became a script" — a bare parameter-shaped
4271
+ * reply ("April 2025 to March 2026") is textually indistinguishable
4272
+ * between those two cases without seeing what the assistant just asked.
4273
+ * Once this tier is decided, nothing downstream re-checks it — the edit
4274
+ * path hands MainAgent a system prompt that explicitly instructs it to
4275
+ * trust the premise and not treat the turn as a new question — so this
4276
+ * gate is the only place that can catch a mismatch.
4277
+ */
4278
+ conversationHistory?: string): Promise<ScriptMatch | null>;
4279
+ /**
4280
+ * Build the script catalog string for the LLM prompt.
4281
+ * Each script gets: index, ID, name, description, and parameter definitions.
4282
+ */
4283
+ private buildScriptCatalog;
4284
+ /**
4285
+ * The recent script-backed answers in this thread — the bounded set an edit
4286
+ * may target. Rendered as its own prompt section (never merged into the
4287
+ * ranked catalog) so the `edit` rules have an unambiguous referent set, and
4288
+ * numbered newest-first so the prompt's "prefer the most recent when the
4289
+ * instruction is ambiguous" tie-break has something to point at.
4290
+ *
4291
+ * Each entry carries the QUESTION that produced it plus the columns it
4292
+ * returned — that is what lets the matcher resolve "use the mode for the WSP
4293
+ * one" instead of blindly taking the newest.
4294
+ */
4295
+ private buildActiveScriptBlock;
4296
+ }
4297
+
4298
+ /**
4299
+ * ScriptRunner — Execute scripts in an isolated tsx subprocess.
4300
+ *
4301
+ * The subprocess approach replaces the earlier `new Function()` eval and gives us:
4302
+ * - Real sandbox (separate process, SIGKILL on timeout).
4303
+ * - Real TypeScript (tsx transpiles on the fly).
4304
+ * - npm imports available to scripts (clustering, stats, geo, etc.).
4305
+ *
4306
+ * Protocol: NDJSON over the child's stdin/stdout. See script-ipc.ts + backend/docs/SCRIPT-FLOW-IMPLEMENTATION.md.
4307
+ */
4308
+
4309
+ interface RunScriptOptions {
4310
+ /** Data sources the script is allowed to query via ctx.query */
4311
+ externalTools: ExternalTool[];
4312
+ /** Optional — for propagating per-query UI progress to the user */
4313
+ streamBuffer?: StreamBuffer;
4314
+ /** Override the wall-clock timeout (default `SCRIPT_TIMEOUT_MS`, 60s). */
4315
+ timeoutMs?: number;
4316
+ /**
4317
+ * Per-turn cancellation signal. When the user hits "Stop" mid-run, the child
4318
+ * process group is SIGKILLed and the run resolves as an aborted failure (the
4319
+ * caller is already unwinding, so the result is discarded).
4320
+ */
4321
+ signal?: AbortSignal;
4322
+ }
4323
+ /**
4324
+ * Execute a recipe by spawning a tsx child on the script's .ts file.
4325
+ * `scriptPath` is the absolute path to the saved `.ts` body.
4326
+ */
4327
+ declare function runScript(recipe: ScriptRecipe, scriptPath: string, params: Record<string, any>, options: RunScriptOptions): Promise<ScriptResult>;
4328
+
2858
4329
  type MessageTypeHandler = (message: IncomingMessage) => void | Promise<void>;
2859
4330
  declare class SuperatomSDK {
2860
4331
  private ws;
@@ -2890,6 +4361,8 @@ declare class SuperatomSDK {
2890
4361
  private lastPong;
2891
4362
  private readonly PING_INTERVAL_MS;
2892
4363
  private readonly PONG_TIMEOUT_MS;
4364
+ private pendingOutbox;
4365
+ private readonly MAX_OUTBOX_SIZE;
2893
4366
  constructor(config: SuperatomSDKConfig);
2894
4367
  /**
2895
4368
  * Initialize PromptLoader and load prompts into memory
@@ -2934,6 +4407,20 @@ declare class SuperatomSDK {
2934
4407
  * Does NOT throw on closed connections — callers can check the return value if needed.
2935
4408
  */
2936
4409
  send(message: Message): boolean;
4410
+ /**
4411
+ * Queue a message that couldn't be delivered because the socket was down,
4412
+ * to be resent once it reconnects. Drops the oldest entry once the bound is
4413
+ * hit — an outage long enough to fill this queue means the oldest queued
4414
+ * responses are for requests the caller has likely already given up on.
4415
+ */
4416
+ private queuePendingMessage;
4417
+ /**
4418
+ * Resend everything queued while the socket was down, now that it's back
4419
+ * up. Uses this.ws.send() directly (not send()) so a message that fails
4420
+ * again goes back through queuePendingMessage() rather than being silently
4421
+ * dropped a second time.
4422
+ */
4423
+ private flushPendingOutbox;
2937
4424
  /**
2938
4425
  * Register a message handler to receive all messages
2939
4426
  */
@@ -2973,6 +4460,14 @@ declare class SuperatomSDK {
2973
4460
  */
2974
4461
  private handlePong;
2975
4462
  private storeComponents;
4463
+ /**
4464
+ * The live, frontend-registered component catalog (name, type, description,
4465
+ * and full prop schema per component) — the same authoritative source
4466
+ * DASH_COMP_REQ's LLM prompt is built from. Exposed so other integrations
4467
+ * (e.g. the dashboard-agent script bridge) can read real component
4468
+ * contracts instead of maintaining a separate, driftable hand-written copy.
4469
+ */
4470
+ getComponents(): Component[];
2976
4471
  /**
2977
4472
  * Set tools for the SDK instance
2978
4473
  */
@@ -2981,6 +4476,16 @@ declare class SuperatomSDK {
2981
4476
  * Get the stored tools
2982
4477
  */
2983
4478
  getTools(): Tool$1[];
4479
+ /**
4480
+ * Call a registered collection operation in-process — no WebSocket
4481
+ * round-trip, since the caller is already running inside this same SDK
4482
+ * instance. Lets SDK-internal features (e.g. the dashboard agent) reuse
4483
+ * whatever collection a deployment has already registered (e.g.
4484
+ * 'dashboards'.'query') by name/convention, instead of requiring a
4485
+ * separate callback purely to re-expose data a collection already serves.
4486
+ * Throws if the collection or operation isn't registered.
4487
+ */
4488
+ callCollection<TResult = any>(collectionName: string, operation: string, params?: any): Promise<TResult>;
2984
4489
  /**
2985
4490
  * Register workflow components for the SDK instance.
2986
4491
  *
@@ -3024,4 +4529,4 @@ declare class SuperatomSDK {
3024
4529
  getConversationSimilarityThreshold(): number;
3025
4530
  }
3026
4531
 
3027
- export { type Action, type AgentConfig, type AgentResponse, BM25L, type BM25LOptions, type BaseLLMConfig, CONTEXT_CONFIG, type CapturedLog, CleanupService, type CollectionHandler, type CollectionOperation, type DBUIBlock, DEFAULT_AGENT_CONFIG, type DatabaseType, type HybridSearchOptions, type IncomingMessage, type KbNodesQueryFilters, type KbNodesRequestPayload, LLM, type LLMUsageEntry, type LogLevel, MainAgent, type Message, type ModelStrategy, type OutputField, type RerankedResult, STORAGE_CONFIG, type SelectedWorkflow, SuperatomSDK, type SuperatomSDKConfig, type TaskType, Thread, ThreadManager, type Tool$1 as Tool, type ToolOutputSchema, UIBlock, UILogCollector, type User, UserManager, type UsersData, type WorkflowDescriptor, anthropicLLM, dashboardConversationHistory, geminiLLM, groqLLM, hybridRerank, llmUsageLogger, logger, openaiLLM, queryCache, rerankChromaResults, rerankConversationResults, userPromptErrorLogger };
4532
+ export { type Action, type AgentConfig, type AgentResponse, BM25L, type BM25LOptions, type BaseLLMConfig, CONTEXT_CONFIG, type CapturedLog, CleanupService, type CollectionHandler, type CollectionOperation, type DBUIBlock, DEFAULT_AGENT_CONFIG, type DashboardAgentCollectionConfig, type DatabaseType, type HybridSearchOptions, type IncomingMessage, type KbNodesQueryFilters, type KbNodesRequestPayload, LLM, type LLMUsageEntry, type LogLevel, MainAgent, type Message, type ModelStrategy, type OutputField, type RerankedResult, STORAGE_CONFIG, type ScriptComponentSpec, ScriptMatcher, type ScriptParameter, type ScriptRecipe, type ScriptRecipeMetaRow, type ScriptRecipeStore, type ScriptResult, ScriptStore, type ScriptStoreOptions, type SelectedWorkflow, SuperatomSDK, type SuperatomSDKConfig, type TaskType, Thread, ThreadManager, type Tool$1 as Tool, type ToolOutputSchema, UIBlock, UILogCollector, type User, UserManager, type UsersData, type WorkflowDescriptor, anthropicLLM, dashboardConversationHistory, geminiLLM, groqLLM, hybridRerank, llmUsageLogger, logger, normalizeScriptBody, openaiLLM, queryCache, registerDashboardAgentCollection, rerankChromaResults, rerankConversationResults, resolveScriptRecipeStore, runScript, userPromptErrorLogger };