@superatomai/sdk-node 0.0.40-mds → 0.0.41-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';
@@ -994,7 +1957,7 @@ declare const KbNodesRequestPayloadSchema: z.ZodObject<{
994
1957
  content?: string | undefined;
995
1958
  }>>;
996
1959
  }, "strip", z.ZodTypeAny, {
997
- operation: "create" | "getOne" | "update" | "delete" | "getAll" | "search" | "getByCategory" | "getByUser" | "getCategories" | "getTags";
1960
+ operation: "create" | "getOne" | "search" | "update" | "delete" | "getAll" | "getByCategory" | "getByUser" | "getCategories" | "getTags";
998
1961
  data?: {
999
1962
  id?: number | undefined;
1000
1963
  type?: "query" | "user" | "global" | undefined;
@@ -1017,7 +1980,7 @@ declare const KbNodesRequestPayloadSchema: z.ZodObject<{
1017
1980
  content?: string | undefined;
1018
1981
  } | undefined;
1019
1982
  }, {
1020
- operation: "create" | "getOne" | "update" | "delete" | "getAll" | "search" | "getByCategory" | "getByUser" | "getCategories" | "getTags";
1983
+ operation: "create" | "getOne" | "search" | "update" | "delete" | "getAll" | "getByCategory" | "getByUser" | "getCategories" | "getTags";
1021
1984
  data?: {
1022
1985
  id?: number | undefined;
1023
1986
  type?: "query" | "user" | "global" | undefined;
@@ -1232,348 +2195,384 @@ declare class DashboardManager {
1232
2195
  * Check if a dashboard exists
1233
2196
  * @param dashboardId - Dashboard ID
1234
2197
  * @returns True if dashboard exists, false otherwise
1235
- */
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;
2198
+ */
2199
+ dashboardExists(dashboardId: string): boolean;
2200
+ /**
2201
+ * Get dashboard count
2202
+ * @returns Number of dashboards
2203
+ */
2204
+ getDashboardCount(): number;
1475
2205
  }
2206
+
1476
2207
  /**
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.
2208
+ * ReportManager class to handle CRUD operations on reports
2209
+ * All operations read/write directly to files (no in-memory caching)
1486
2210
  */
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;
2211
+ declare class ReportManager {
2212
+ private reportsBasePath;
2213
+ private projectId;
1494
2214
  /**
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."
2215
+ * Initialize ReportManager with project ID
2216
+ * @param projectId - Project ID to use in file path
1499
2217
  */
1500
- whenToUse: string;
2218
+ constructor(projectId?: string);
1501
2219
  /**
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
- * ```
2220
+ * Get the file path for a specific report
2221
+ * @param reportId - Report ID
2222
+ * @returns Full path to report data.json file
1514
2223
  */
1515
- propsSchema: Record<string, string>;
2224
+ private getReportPath;
1516
2225
  /**
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.
2226
+ * Create a new report
2227
+ * @param reportId - Unique report ID
2228
+ * @param report - Report data
2229
+ * @returns Created report with metadata
1520
2230
  */
1521
- defaultProps?: Record<string, any>;
2231
+ createReport(reportId: string, report: DSLRendererProps): DSLRendererProps;
2232
+ /**
2233
+ * Get a specific report by ID
2234
+ * @param reportId - Report ID
2235
+ * @returns Report data or null if not found
2236
+ */
2237
+ getReport(reportId: string): DSLRendererProps | null;
2238
+ /**
2239
+ * Get all reports
2240
+ * @returns Array of report objects with their IDs
2241
+ */
2242
+ getAllReports(): Array<{
2243
+ reportId: string;
2244
+ report: DSLRendererProps;
2245
+ }>;
2246
+ /**
2247
+ * Update an existing report
2248
+ * @param reportId - Report ID
2249
+ * @param report - Updated report data
2250
+ * @returns Updated report or null if not found
2251
+ */
2252
+ updateReport(reportId: string, report: DSLRendererProps): DSLRendererProps | null;
2253
+ /**
2254
+ * Delete a report
2255
+ * @param reportId - Report ID
2256
+ * @returns True if deleted, false if not found
2257
+ */
2258
+ deleteReport(reportId: string): boolean;
2259
+ /**
2260
+ * Check if a report exists
2261
+ * @param reportId - Report ID
2262
+ * @returns True if report exists, false otherwise
2263
+ */
2264
+ reportExists(reportId: string): boolean;
2265
+ /**
2266
+ * Get report count
2267
+ * @returns Number of reports
2268
+ */
2269
+ getReportCount(): number;
1522
2270
  }
2271
+
1523
2272
  /**
1524
- * The workflow selection captured during a routing call.
1525
- * Set on AgentResponse when the LLM picks a workflow tool.
2273
+ * ScriptRecipeStore — injected metadata backend for the script flow.
2274
+ *
2275
+ * The SDK is standalone (no DB dependency). The backend implements this
2276
+ * interface over Postgres (full-text search + atomic counters) and injects it
2277
+ * via `collections['script-recipes']`, exactly like `collections['source-embeddings']`.
2278
+ * `ScriptStore` consumes it for all METADATA operations while keeping the
2279
+ * executable body on disk as scripts-store/<fileBase>.ts.
2280
+ *
2281
+ * All metadata rows are plain JSON (no scriptBody — that lives on disk).
2282
+ * See backend/docs/SCRIPT-FLOW-SCALING-ISSUES.md (#1, #3, #7).
1526
2283
  */
1527
- interface SelectedWorkflow {
1528
- /** Component name (matches WorkflowDescriptor.name) */
2284
+
2285
+ /** One recipe's metadata as stored in Postgres (mirrors the script_recipes table). */
2286
+ interface ScriptRecipeMetaRow {
2287
+ id: string;
2288
+ projectId?: string | null;
2289
+ version: number;
1529
2290
  name: string;
1530
- /** Props extracted from the prompt + merged with workflow.defaultProps */
1531
- props: Record<string, any>;
2291
+ intentDescription: string;
2292
+ tags: string[] | null;
2293
+ createdFrom: string | null;
2294
+ sourceIds: string[] | null;
2295
+ tables: string[] | null;
2296
+ parameters: ScriptParameter[] | null;
2297
+ components?: ScriptComponentSpec[] | null;
2298
+ displayPreference?: ScriptDisplayPreference | null;
2299
+ fileBase: string;
2300
+ bodyHash?: string | null;
2301
+ successCount: number;
2302
+ failureCount: number;
2303
+ lastUsed: string | null;
2304
+ parentId?: string | null;
2305
+ forkDepth?: number | null;
2306
+ forkReason?: string | null;
2307
+ status: 'draft' | 'verified' | string;
2308
+ turnId?: string | null;
2309
+ editedBy?: string | null;
2310
+ editedFrom?: string | null;
2311
+ history?: ScriptVersionRecord[] | null;
2312
+ lastError?: {
2313
+ phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
2314
+ message: string;
2315
+ at: string;
2316
+ attempt: number;
2317
+ } | null;
2318
+ createdAt?: string | null;
2319
+ updatedAt?: string | null;
2320
+ }
2321
+ interface ScriptRecipeStore {
2322
+ /** FTS shortlist of healthy verified recipes for the matcher (metadata only). */
2323
+ search(params: {
2324
+ prompt: string;
2325
+ projectId?: string;
2326
+ limit?: number;
2327
+ }): Promise<ScriptRecipeMetaRow[]>;
2328
+ /** Fetch one recipe by id (any status). */
2329
+ getById(id: string): Promise<ScriptRecipeMetaRow | null>;
2330
+ /** Count healthy verified recipes (drives the "any scripts?" gate). */
2331
+ count(params?: {
2332
+ projectId?: string;
2333
+ }): Promise<number>;
2334
+ /** Insert or update a recipe row (keyed by id). */
2335
+ upsert(row: ScriptRecipeMetaRow): Promise<void>;
2336
+ /** Atomically bump counters / last-used. */
2337
+ updateStats(id: string, patch: {
2338
+ successDelta?: number;
2339
+ failureDelta?: number;
2340
+ lastUsed?: string;
2341
+ }): Promise<void>;
2342
+ /** Flip a draft to verified, applying provenance + optional fork lineage. */
2343
+ promote(id: string, patch: {
2344
+ sourceIds: string[];
2345
+ tables: string[];
2346
+ fileBase?: string;
2347
+ parentId?: string;
2348
+ forkDepth?: number;
2349
+ forkReason?: string;
2350
+ components?: ScriptComponentSpec[];
2351
+ }): Promise<ScriptRecipeMetaRow | null>;
2352
+ /**
2353
+ * Commit a verified edit onto an EXISTING recipe: bump `version`, replace the
2354
+ * body-bearing metadata, append a history record, reset health counters, and
2355
+ * clear the component specs (they were validated against the old shape).
2356
+ * Returns the updated row, or null when the target is gone.
2357
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 5.
2358
+ */
2359
+ commitEdit?(id: string, patch: {
2360
+ name?: string;
2361
+ intentDescription?: string;
2362
+ tags?: string[];
2363
+ parameters?: ScriptParameter[];
2364
+ bodyHash: string;
2365
+ /** New on-disk stem when the edit renamed the script; omitted otherwise. */
2366
+ fileBase?: string;
2367
+ sourceIds?: string[];
2368
+ tables?: string[];
2369
+ editedBy?: string;
2370
+ editedFrom?: string;
2371
+ historyEntry: {
2372
+ version: number;
2373
+ at: string;
2374
+ by?: string;
2375
+ instruction?: string;
2376
+ changeSummary?: string;
2377
+ bodyHash?: string;
2378
+ };
2379
+ }): Promise<ScriptRecipeMetaRow | null>;
2380
+ /** Stamp a draft's last execution error. */
2381
+ recordDraftError(id: string, err: {
2382
+ phase: string;
2383
+ message: string;
2384
+ attempt: number;
2385
+ at: string;
2386
+ }): Promise<void>;
2387
+ /** Delete a recipe row (body file removed separately). */
2388
+ remove(id: string): Promise<void>;
2389
+ /** True if `fileBase` is taken by a different recipe in this project. */
2390
+ fileBaseTaken(fileBase: string, excludeId: string, projectId?: string): Promise<boolean>;
1532
2391
  }
2392
+ /** Pull the injected store off the collections bag (or null if not wired). */
2393
+ declare function resolveScriptRecipeStore(collections: any): ScriptRecipeStore | null;
2394
+
1533
2395
  /**
1534
- * The complete response from the multi-agent system.
1535
- * Contains everything needed for text display + component generation.
2396
+ * ScriptStore — Postgres metadata + on-disk body for script recipes.
2397
+ *
2398
+ * Split of responsibilities:
2399
+ * - METADATA → injected `ScriptRecipeStore` (Postgres FTS + atomic counters),
2400
+ * resolved from `collections['script-recipes']`.
2401
+ * - BODY → scripts-store/<fileBase>.ts, editable in your IDE. Written
2402
+ * atomically (temp + rename); `bodyHash` (sha256) detects edits.
2403
+ *
2404
+ * The old "read every file every turn + send the whole catalog to the LLM"
2405
+ * matcher is gone — matching is `store.search(prompt)` (FTS shortlist). The
2406
+ * draft/verified filename dance is gone too: `status` is a DB column and the
2407
+ * file keeps a stable `<fileBase>.ts` name across promotion.
2408
+ *
2409
+ * When no metadata store is injected, the store degrades to a safe no-op
2410
+ * (count 0 → script flow disabled) instead of crashing.
2411
+ *
2412
+ * See backend/docs/SCRIPT-FLOW-SCALING-ISSUES.md.
1536
2413
  */
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[];
2414
+
2415
+ interface SaveDraftInput {
2416
+ /** Reuse an existing draft (retry); omit to mint a new one. */
2417
+ recipeId?: string;
2418
+ /** Per-turn unique suffix, stable across retries within the turn. */
2419
+ turnId: string;
2420
+ name: string;
2421
+ intentDescription: string;
2422
+ tags: string[];
2423
+ parameters: ScriptParameter[];
2424
+ scriptBody: string;
2425
+ createdFrom: string;
2426
+ /**
2427
+ * Set when this draft is a SHADOW of an existing recipe being edited. The
2428
+ * draft is verified independently and merged back onto the parent via
2429
+ * `commitEdit`, so the working script is never clobbered by an edit that
2430
+ * turns out not to run. See backend/docs/SCRIPT-EDIT-DESIGN.md § D5.
2431
+ */
2432
+ parentId?: string;
2433
+ }
2434
+ interface PromoteToVerifiedInput {
2435
+ sourceIds: string[];
2436
+ tables: string[];
2437
+ parentId?: string;
2438
+ forkDepth?: number;
2439
+ forkReason?: string;
2440
+ components?: ScriptComponentSpec[];
2441
+ }
2442
+ interface ScriptStoreOptions {
2443
+ /** Explicit metadata store, or resolved from `collections['script-recipes']`. */
2444
+ store?: ScriptRecipeStore | null;
2445
+ collections?: any;
2446
+ /** Body directory (defaults to <cwd>/scripts-store). */
2447
+ baseDir?: string;
2448
+ /** Project scope stamped on every row. */
2449
+ projectId?: string;
2450
+ }
2451
+ declare function normalizeScriptBody(scriptBody: string): string;
2452
+ declare class ScriptStore {
2453
+ private store;
2454
+ private storeDir;
2455
+ private projectId?;
2456
+ constructor(opts?: ScriptStoreOptions);
2457
+ /** Whether a metadata store is wired (matcher / authoring are gated on this). */
2458
+ hasStore(): boolean;
2459
+ /** Number of healthy verified recipes (gates the script-matching path). */
2460
+ count(): Promise<number>;
2461
+ /**
2462
+ * FTS shortlist for the matcher (metadata only — bodies are loaded lazily by
2463
+ * `get()` once the LLM picks one). Returns verified, healthy recipes ranked
2464
+ * by relevance.
2465
+ */
2466
+ search(prompt: string, limit?: number): Promise<ScriptRecipe[]>;
2467
+ /** Fetch one recipe by id with its body loaded from disk. */
2468
+ get(id: string): Promise<ScriptRecipe | null>;
2469
+ /** Create or update a recipe (metadata upsert + body write when changed). */
2470
+ save(recipe: ScriptRecipe): Promise<void>;
2471
+ /**
2472
+ * Persist (or update) a draft. Within a turn, retries that pass the same
2473
+ * `recipeId` overwrite the same row + file; a fresh `recipeId` mints a new
2474
+ * draft. The body is visible at scripts-store/<fileBase>.ts immediately.
2475
+ */
2476
+ saveDraft(input: SaveDraftInput): Promise<ScriptRecipe>;
2477
+ /** Stamp a draft's last execution error (metadata only). */
2478
+ recordDraftError(recipeId: string, err: {
2479
+ phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
2480
+ message: string;
2481
+ attempt: number;
2482
+ }): Promise<void>;
2483
+ /**
2484
+ * Promote a successfully-executed draft into a verified script.
2485
+ * The on-disk body already exists at <fileBase>.ts (written at write_script
2486
+ * time) and keeps its name — only the DB row flips status + provenance.
2487
+ */
2488
+ promoteToVerified(recipeId: string, input: PromoteToVerifiedInput): Promise<ScriptRecipe | null>;
2489
+ /**
2490
+ * Commit a user-directed edit: merge a VERIFIED shadow draft back onto the
2491
+ * recipe it was editing, as version N+1 under the SAME recipe id.
2492
+ *
2493
+ * Keeping the id stable is the point of the whole feature — every cached
2494
+ * conversation, `script_dataset` regeneration descriptor and persisted
2495
+ * component spec already points at it, so the correction applies retroactively
2496
+ * to replays instead of stranding them on the old body.
2497
+ *
2498
+ * Steps (see backend/docs/SCRIPT-EDIT-DESIGN.md § Component 5):
2499
+ * 1. archive the current body as `<fileBase>.v<version>.ts`
2500
+ * 2. write the edited body to the original fileBase (atomic)
2501
+ * 3. version++, copy name/description/params, append a history record
2502
+ * 4. reset health counters — the pre-edit failure history is stale
2503
+ * 5. clear component specs — they were validated against the OLD shape
2504
+ * 6. delete the shadow draft (row + file)
2505
+ *
2506
+ * Returns the updated recipe, or null when the commit could not be applied
2507
+ * (caller should then fall back to treating the draft as a new script).
2508
+ */
2509
+ commitEdit(targetId: string, draft: {
2510
+ recipeId: string;
2511
+ name?: string;
2512
+ intentDescription?: string;
2513
+ tags?: string[];
2514
+ parameters?: ScriptParameter[];
2515
+ scriptBody: string;
2516
+ sourceIds?: string[];
2517
+ tables?: string[];
2518
+ }, meta: {
2519
+ instruction?: string;
2520
+ changeSummary?: string;
2521
+ editedBy?: string;
2522
+ }): Promise<ScriptRecipe | null>;
2523
+ /**
2524
+ * Drop a draft (row + body file). MainAgent calls this at end-of-turn when a
2525
+ * draft was authored but never verified — failed drafts are never matched, so
2526
+ * deleting them immediately avoids unbounded accumulation (#5). No-op if the
2527
+ * recipe isn't a draft (so a promoted/verified script is never removed here).
2528
+ */
2529
+ discardDraft(recipeId: string): Promise<void>;
2530
+ /** Delete a recipe (row + body file). */
2531
+ delete(id: string): Promise<void>;
2532
+ /** Record a successful execution (atomic counter bump). */
2533
+ recordSuccess(id: string): Promise<void>;
2534
+ /** Record a failed execution (atomic counter bump). */
2535
+ recordFailure(id: string): Promise<void>;
2536
+ /** Absolute path to the .ts body for a recipe (used by the runner/MainAgent). */
2537
+ getScriptPath(recipe: ScriptRecipe): string;
2538
+ private removeById;
2539
+ private rowToRecipe;
2540
+ private recipeToRow;
2541
+ /** slug of name, with a short id suffix when the bare slug is already taken. */
2542
+ private computeFileBase;
2543
+ private toSlug;
2544
+ private hash;
2545
+ /**
2546
+ * Append (or replace) the `getAnalysis` export on an existing draft body.
2547
+ *
2548
+ * The narrative is authored AFTER execution, so it lands on a file that
2549
+ * already holds a working getData — rewriting the whole body would risk
2550
+ * losing the query that just succeeded. Replaces any earlier getAnalysis so
2551
+ * a retry does not stack duplicate exports.
2552
+ */
2553
+ attachAnalysis(recipeId: string, analysisBody: string): Promise<boolean>;
2554
+ private bodyPath;
2555
+ private readBody;
2556
+ /** Directory holding superseded bodies. Dot-prefixed so IDEs/`ls` hide it. */
2557
+ private get archiveDir();
2558
+ /**
2559
+ * Archive a superseded body as `.versions/<fileBase>.v<n>.ts`.
2560
+ *
2561
+ * Kept out of the main store directory on purpose — see commitEdit step 1.
2562
+ * To roll back: copy the file back over `scripts-store/<fileBase>.ts`.
2563
+ */
2564
+ private writeArchive;
1544
2565
  /**
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.
2566
+ * Move a recipe's archived versions to a new prefix when its fileBase changes,
2567
+ * so all versions of one recipe stay grouped. Without this, two renames would
2568
+ * scatter a single recipe's history across three prefixes in `.versions/` with
2569
+ * nothing linking them back to the live script.
1548
2570
  */
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;
2571
+ private renameArchives;
2572
+ /** Atomic body write (temp + rename) so concurrent reads never see a partial file. */
2573
+ private writeBody;
2574
+ private unlinkBody;
1572
2575
  }
1573
- /**
1574
- * Default agent configuration
1575
- */
1576
- declare const DEFAULT_AGENT_CONFIG: AgentConfig;
1577
2576
 
1578
2577
  /**
1579
2578
  * Main Agent (Orchestrator)
@@ -1595,7 +2594,60 @@ declare class MainAgent {
1595
2594
  private workflows;
1596
2595
  private config;
1597
2596
  private streamBuffer;
1598
- constructor(externalTools: ExternalTool[], config: AgentConfig, streamBuffer?: StreamBuffer, workflows?: WorkflowDescriptor[]);
2597
+ /**
2598
+ * Optional: when provided, MainAgent exposes the `write_script` /
2599
+ * `execute_script` tools to the LLM and persists drafts to disk via the
2600
+ * store. Headless callers (alert analyzer, metric resolver) omit these to
2601
+ * suppress script authoring entirely — drafts would otherwise leak onto
2602
+ * disk with no caller to promote or clean them up.
2603
+ */
2604
+ private scriptStore;
2605
+ private turnId;
2606
+ private createdFromPrompt;
2607
+ private scriptState;
2608
+ /** Answer-flow component catalog (filtered + projected by the caller). */
2609
+ private componentCatalog;
2610
+ /** Specs accepted by `render_components` this turn; empty until it succeeds. */
2611
+ /**
2612
+ * Entities resolved this turn, appended verbatim to every source dispatch.
2613
+ *
2614
+ * Not left to the model to copy into its intent prose: observed live, the
2615
+ * MainAgent wrote names instead of ids on the first dispatch AND re-spelled
2616
+ * 'F D C LIMITED' as 'FDC Limited', so the source agent fell back to
2617
+ * `PartyName LIKE '%FDC%'` and found 2 of 3 customers. Carrying the ids
2618
+ * structurally makes that impossible rather than instruction-dependent.
2619
+ */
2620
+ private resolvedEntities;
2621
+ private componentSpecs;
2622
+ private componentLayout;
2623
+ private renderComponentAttempts;
2624
+ /**
2625
+ * Fork mode — set when this turn is adapting a near-matching parent script.
2626
+ * In fork mode there is no legitimate "answer with bare text" outcome: the
2627
+ * only correct first move is a tool call (write_script, or a source tool for
2628
+ * schema discovery). We therefore force tool use on the first LLM iteration
2629
+ * so the model can't end its turn with a bare "I'll adapt…" preamble and zero
2630
+ * tool calls. Never set on the fresh-authoring / general-question path.
2631
+ */
2632
+ private forkMode;
2633
+ /**
2634
+ * Edit mode — set when this turn applies a user-directed change to an
2635
+ * existing script. Swaps the system prompt to `agent-main-edit` and stamps
2636
+ * the shadow draft's parentId. Like fork mode there is no legitimate
2637
+ * "answer with bare text" outcome, so tool use is forced on iteration 1.
2638
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 4.
2639
+ */
2640
+ private editContext;
2641
+ /**
2642
+ * Per-turn cancellation signal (user hit "Stop"). Set at the top of
2643
+ * handleQuestion and read by the tool handler, the SourceAgent dispatch, and
2644
+ * the script subprocess so an abort tears down every layer of the turn.
2645
+ */
2646
+ private abortSignal?;
2647
+ constructor(externalTools: ExternalTool[], config: AgentConfig, scriptStore?: ScriptStore, turnId?: string, streamBuffer?: StreamBuffer, workflows?: WorkflowDescriptor[], forkMode?: boolean, editContext?: EditContext, componentCatalog?: Component[]);
2648
+ /** True when the turn is applying a user-directed script edit. */
2649
+ private get editMode();
2650
+ private get scriptingEnabled();
1599
2651
  /**
1600
2652
  * Handle a user question using the multi-agent system.
1601
2653
  *
@@ -1606,7 +2658,92 @@ declare class MainAgent {
1606
2658
  * 4. Direct tools → fn() called directly with LLM params → returns data
1607
2659
  * 5. Generates final analysis text
1608
2660
  */
1609
- handleQuestion(userPrompt: string, apiKey?: string, conversationHistory?: string, streamCallback?: (chunk: string) => void): Promise<AgentResponse>;
2661
+ handleQuestion(userPrompt: string, apiKey?: string, conversationHistory?: string, streamCallback?: (chunk: string) => void, signal?: AbortSignal): Promise<AgentResponse>;
2662
+ /**
2663
+ * Tool definition, with the catalog rendered from the registered `Metadata`
2664
+ * the caller passed in. Generated rather than hand-written so it cannot drift
2665
+ * from what the frontend actually ships.
2666
+ */
2667
+ private buildRenderComponentsToolDef;
2668
+ /**
2669
+ * Validate the agent's component picks against the REAL columns of each bound
2670
+ * dataset. All-or-nothing: any error rejects the whole call and returns the
2671
+ * findings, because a partially-accepted dashboard makes the retry ambiguous.
2672
+ */
2673
+ private handleRenderComponents;
2674
+ /**
2675
+ * Author the answer as a program. Runs after execute_script, so the body is
2676
+ * written with the result already known and can react to what it found.
2677
+ *
2678
+ * Rejects a body that types numbers rather than computing them — see
2679
+ * analysis-lint. A hardcoded figure is worse than prose: it freezes into the
2680
+ * recipe and replays unchanged while the data moves underneath it.
2681
+ */
2682
+ private handleWriteAnalysis;
2683
+ private handleWriteScript;
2684
+ private handleExecuteScript;
2685
+ /**
2686
+ * Build the AgentWrittenScript payload the caller will hand to
2687
+ * `ScriptStore.promoteToVerified()`. Only returned when a verified
2688
+ * successful execution is on record.
2689
+ */
2690
+ private buildSavedScript;
2691
+ private normalizeParameterList;
2692
+ /**
2693
+ * Use the schema embedding collection to pre-select relevant tables for
2694
+ * this source + intent. Returns a formatted schema block if confidence is
2695
+ * high (top match ≥ 0.55 and ≥3 candidates), otherwise null.
2696
+ *
2697
+ * When this returns a block, we can skip the SourceAgent's `search_schema`
2698
+ * loop and reduce iteration budget. When it returns null, the SourceAgent
2699
+ * falls back to the existing LLM-driven keyword search (same as today).
2700
+ */
2701
+ /**
2702
+ * Ids + verbatim names for everything resolved this turn, appended by CODE to
2703
+ * each source dispatch so neither omission nor re-spelling by the model can
2704
+ * lose them. Empty string when nothing was resolved, so intents are untouched
2705
+ * for questions that name no entities.
2706
+ *
2707
+ * Also appends `attrs.scopeSql` when the resolved instance carries one —
2708
+ * same "guaranteed by code" reasoning as the ids/names above, extended to
2709
+ * the one attrs field with a documented, cross-entity-type meaning: a
2710
+ * ready-made SQL predicate an entity's index precomputed (see
2711
+ * ENTITY-SEARCH-DESIGN.md's hierarchy-broadening pattern — branch/region/
2712
+ * state entity types attach it so a lane/route question never needs its
2713
+ * own city-matching join). Confirmed real cost of not doing this: a
2714
+ * branch/state resolved with scopeSql sat unused while the source agent
2715
+ * spent 4 failed exploratory queries rediscovering the exact same
2716
+ * branch-id list by hand (Invalid column name 'StateName'/'CityId'/
2717
+ * 'OrganizationLocationId', in that order, ~144s, before landing on ids
2718
+ * that were sitting in scopeSql from the very first resolve_entities
2719
+ * call). Other attrs (code, counts, etc.) are not dumped here — they are
2720
+ * descriptive, not actionable the way a precomputed predicate is, and
2721
+ * bloating every dispatch with all of them for no clear gain isn't worth
2722
+ * it; scopeSql is the one thing worth this guarantee today.
2723
+ */
2724
+ private buildResolvedEntitiesBlock;
2725
+ /** True when the backend registered the entity-search collection. */
2726
+ private get entitySearchEnabled();
2727
+ /**
2728
+ * Compact catalog (type + gloss) for the system prompt. This is what lets the
2729
+ * LLM NAME a concept instead of guessing at our vocabulary — it can infer
2730
+ * that a question is about unpaid money, but not that we call that
2731
+ * `outstanding-balance`. Full `resolution` detail is fetched by the tool.
2732
+ *
2733
+ * Deliberately NOT cached in-process. The token cost is already handled by
2734
+ * prompt caching (the system prompt carries a cache_control breakpoint), and
2735
+ * re-reading a tiny PK-ordered table costs ~nothing. Fetching fresh does not
2736
+ * weaken the prompt cache either: unchanged data renders byte-identical text,
2737
+ * so the prefix still hits. A process cache would only buy a staleness window
2738
+ * in which a newly indexed entity is invisible to the agent.
2739
+ */
2740
+ private loadEntityCatalog;
2741
+ /**
2742
+ * Never throws: a resolver outage must degrade to "nothing resolved" and
2743
+ * leave the agent exactly as it was, not take the whole answer down.
2744
+ */
2745
+ private handleResolveEntities;
2746
+ private preResolveSchema;
1610
2747
  /**
1611
2748
  * Execute a direct tool — call fn() with LLM-provided params, no SourceAgent.
1612
2749
  */
@@ -1672,7 +2809,45 @@ interface LLMOptions {
1672
2809
  temperature?: number;
1673
2810
  topP?: number;
1674
2811
  apiKey?: string;
2812
+ baseURL?: string;
1675
2813
  partial?: (chunk: string) => void;
2814
+ /**
2815
+ * Per-request cancellation. When the caller aborts this signal (user hit
2816
+ * "Stop"), the underlying provider request is cancelled and the call throws
2817
+ * a RequestAbortedError. Threaded into the provider `messages.create` request
2818
+ * options and checked between tool-loop iterations. Currently honored on the
2819
+ * Anthropic path (the agent flow's default provider).
2820
+ */
2821
+ signal?: AbortSignal;
2822
+ /**
2823
+ * Forces a tool call on the FIRST iteration of streamWithTools only
2824
+ * (subsequent iterations revert to auto). Used by fork mode to stop the
2825
+ * model from ending its turn with a bare "I'll adapt the script…" preamble
2826
+ * and zero tool calls. `{ type: 'any' }` lets the model pick which tool
2827
+ * (write_script in the common case, a source tool for schema discovery);
2828
+ * `{ type: 'tool', name }` pins a specific tool. Honored on both the
2829
+ * Anthropic path and the OpenAI/OpenRouter path (mapped to OpenAI's
2830
+ * tool_choice: 'required' / a named function).
2831
+ */
2832
+ firstIterationToolChoice?: {
2833
+ type: 'any';
2834
+ } | {
2835
+ type: 'tool';
2836
+ name: string;
2837
+ };
2838
+ /**
2839
+ * Internal — set only by the OpenRouter wrappers when the target is a Claude
2840
+ * model. Tells the OpenAI-wire path to emit Anthropic `cache_control`
2841
+ * breakpoints (OpenRouter forwards them to Anthropic for prompt caching).
2842
+ * Never set for direct OpenAI/Groq calls, so their requests are unchanged.
2843
+ */
2844
+ _openrouterClaudeCaching?: boolean;
2845
+ /**
2846
+ * Internal — OpenRouter provider-routing preferences (forwarded as the
2847
+ * `provider` body field). Set by the OpenRouter wrappers to steer routing to
2848
+ * a fast backend (e.g. {sort:'throughput'}). Never set for direct OpenAI/Groq.
2849
+ */
2850
+ _openrouterProvider?: Record<string, unknown>;
1676
2851
  }
1677
2852
  interface Tool {
1678
2853
  name: string;
@@ -1694,6 +2869,16 @@ declare class LLM {
1694
2869
  * @returns Normalized system prompt for Anthropic API
1695
2870
  */
1696
2871
  private static _normalizeSystemPrompt;
2872
+ /**
2873
+ * Strip unpaired UTF-16 surrogates from every text field of a message set.
2874
+ *
2875
+ * A lone surrogate (from mid-pair string slicing or corrupt source data)
2876
+ * serializes to a bare `\udXXX` escape that strict JSON parsers — including
2877
+ * the one on Anthropic's API — reject with "no low surrogate in string",
2878
+ * failing the whole request. Sanitizing here, at the single boundary every
2879
+ * provider call flows through, guarantees no request can carry one.
2880
+ */
2881
+ private static _sanitizeMessages;
1697
2882
  /**
1698
2883
  * Log cache usage metrics from Anthropic API response
1699
2884
  * Shows cache hits, costs, and savings
@@ -1710,11 +2895,46 @@ declare class LLM {
1710
2895
  * "claude-sonnet-4-5" → ["anthropic", "claude-sonnet-4-5"] (default)
1711
2896
  */
1712
2897
  private static _parseModel;
2898
+ /**
2899
+ * Map an Anthropic model id (e.g. "claude-sonnet-4-5-20250929") to the OpenRouter slug
2900
+ * (e.g. "claude-sonnet-4.5"). OpenRouter slugs drop the date suffix and use dotted versions.
2901
+ */
2902
+ private static _toOpenRouterSlug;
2903
+ /**
2904
+ * Per-provider proxy base URL. Returns `${SUPERATOM_LLM_PROXY_URL}/<provider>`
2905
+ * when our Cloudflare LLM proxy is configured, else undefined (→ talk to the
2906
+ * provider directly, legacy behaviour). An explicit options.baseURL (e.g.
2907
+ * OpenRouter) always wins and is never overridden. See backend/docs/llm-proxy.md.
2908
+ */
2909
+ private static _proxyBaseURL;
2910
+ private static _openrouterOptions;
2911
+ private static _isRetryableProviderError;
2912
+ private static _withOpenrouterRetry;
2913
+ private static _openrouterText;
2914
+ private static _openrouterStream;
2915
+ private static _openrouterStreamWithTools;
2916
+ /**
2917
+ * Build an Anthropic client. Routes through our Cloudflare LLM proxy when
2918
+ * SUPERATOM_LLM_PROXY_URL is set (each client ships a per-client proxy key as
2919
+ * ANTHROPIC_API_KEY and never holds the real key); otherwise talks to
2920
+ * api.anthropic.com directly. See backend/docs/llm-proxy.md.
2921
+ */
2922
+ private static _anthropicClient;
2923
+ /** True when OpenRouter is configured as a fail-open fallback for Claude. */
2924
+ private static _openrouterAvailable;
2925
+ /** Remap an Anthropic model id to the OpenRouter model path for fail-open. */
2926
+ private static _anthropicFallbackModel;
1713
2927
  private static _anthropicText;
1714
2928
  private static _anthropicStream;
1715
2929
  private static _anthropicStreamWithTools;
1716
2930
  private static _groqText;
1717
2931
  private static _groqStream;
2932
+ /**
2933
+ * Gemini request options carrying the proxy base URL, or undefined → talk to
2934
+ * generativelanguage.googleapis.com directly. The Google SDK takes baseUrl as a
2935
+ * per-model request option, not a constructor arg. See backend/docs/llm-proxy.md.
2936
+ */
2937
+ private static _geminiRequestOptions;
1718
2938
  private static _geminiText;
1719
2939
  private static _geminiStream;
1720
2940
  /**
@@ -1723,8 +2943,28 @@ declare class LLM {
1723
2943
  */
1724
2944
  private static _cleanSchemaForGemini;
1725
2945
  private static _geminiStreamWithTools;
2946
+ /** True for Anthropic/Claude model ids — gates OpenRouter prompt caching. */
2947
+ private static _isClaudeModel;
2948
+ /**
2949
+ * Build the OpenAI-wire system message. For OpenRouter + Claude
2950
+ * (cacheClaude=true) it emits content parts carrying Anthropic
2951
+ * `cache_control` breakpoints (preserving any the caller set, else marking
2952
+ * the last block), so OpenRouter forwards them to Anthropic for prompt
2953
+ * caching. Otherwise it returns a plain flattened string — unchanged for
2954
+ * direct OpenAI/Groq.
2955
+ */
2956
+ private static _openaiSystemMessage;
2957
+ /**
2958
+ * Split an OpenAI-wire usage object. `prompt_tokens` INCLUDES cached tokens,
2959
+ * so we subtract them out (Anthropic-style: input excludes cache reads) and
2960
+ * report cached separately — this makes calculateCost price cache reads at
2961
+ * the discounted rate and reflects OpenRouter prompt-cache savings in logs.
2962
+ */
2963
+ private static _openaiUsage;
1726
2964
  private static _openaiText;
1727
2965
  private static _openaiStream;
2966
+ /** Map the Anthropic-style firstIterationToolChoice to OpenAI's tool_choice. */
2967
+ private static _openaiToolChoice;
1728
2968
  private static _openaiStreamWithTools;
1729
2969
  /**
1730
2970
  * Parse JSON string, handling markdown code blocks and surrounding text
@@ -1826,6 +3066,13 @@ declare class UIBlock {
1826
3066
  private textResponse;
1827
3067
  private actions;
1828
3068
  private createdAt;
3069
+ /**
3070
+ * Which script recipe produced this answer, when a script did. Read on the
3071
+ * NEXT turn so the user can say "use mode instead" and have the matcher
3072
+ * resolve it to a concrete script (the `edit` tier is unreachable without it).
3073
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 1.
3074
+ */
3075
+ private scriptBinding;
1829
3076
  /**
1830
3077
  * Creates a new UIBlock instance
1831
3078
  * @param userQuestion - The user's question or input
@@ -1920,6 +3167,14 @@ declare class UIBlock {
1920
3167
  /**
1921
3168
  * Get creation timestamp
1922
3169
  */
3170
+ /**
3171
+ * Bind this block to the script recipe that produced its answer.
3172
+ */
3173
+ setScriptBinding(binding: Record<string, any> | null): void;
3174
+ /**
3175
+ * The script recipe bound to this block, if any.
3176
+ */
3177
+ getScriptBinding(): Record<string, any> | null;
1923
3178
  getCreatedAt(): Date;
1924
3179
  /**
1925
3180
  * Convert UIBlock to JSON-serializable object
@@ -1987,6 +3242,32 @@ declare class Thread {
1987
3242
  * @param currentUIBlockId - ID of current UIBlock to exclude from context (optional)
1988
3243
  * @returns Formatted conversation history string
1989
3244
  */
3245
+ /**
3246
+ * The script recipe bound to the most recent completed UIBlock — i.e. the
3247
+ * script behind the answer the user is currently looking at. Drives the
3248
+ * matcher's `edit` tier: without it, "use mode instead" has no target and
3249
+ * falls through to regeneration.
3250
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 1.
3251
+ */
3252
+ getActiveScriptBinding(currentUIBlockId?: string): Record<string, any> | null;
3253
+ /**
3254
+ * The recent script-backed answers in this thread, newest first — the
3255
+ * candidate set a user-directed edit can target.
3256
+ *
3257
+ * Returning several rather than only the newest is what lets an instruction
3258
+ * name its own target ("use the mode for the WSP one"). With a single
3259
+ * candidate every edit lands on the most recent script, which silently edits
3260
+ * the wrong recipe whenever the user meant an earlier one.
3261
+ *
3262
+ * Deduped by recipeId (newest occurrence wins) so a long editing session on
3263
+ * one script doesn't crowd out the others. Each entry carries the question
3264
+ * that produced it — without that the candidates are indistinguishable.
3265
+ *
3266
+ * In-memory only: dies with the process. The caller falls back to the
3267
+ * persisted bindings when this comes back empty.
3268
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 1.
3269
+ */
3270
+ getScriptBindings(limit?: number, currentUIBlockId?: string): Record<string, any>[];
1990
3271
  getConversationContext(limit?: number, currentUIBlockId?: string): string;
1991
3272
  /**
1992
3273
  * Convert Thread to JSON-serializable object
@@ -2649,7 +3930,7 @@ declare abstract class BaseLLM {
2649
3930
  * This helps provide intelligent suggestions for follow-up queries
2650
3931
  * For general/conversational questions without components, pass textResponse instead
2651
3932
  */
2652
- generateNextQuestions(originalUserPrompt: string, component?: Component | null, componentData?: Record<string, unknown>, apiKey?: string, conversationHistory?: string, textResponse?: string): Promise<string[]>;
3933
+ generateNextQuestions(originalUserPrompt: string, component?: Component | null, componentData?: Record<string, unknown>, apiKey?: string, conversationHistory?: string, textResponse?: string, signal?: AbortSignal): Promise<string[]>;
2653
3934
  }
2654
3935
 
2655
3936
  interface AnthropicLLMConfig extends BaseLLMConfig {
@@ -2712,7 +3993,8 @@ declare const openaiLLM: OpenAILLM;
2712
3993
  * Query Cache — Two mechanisms:
2713
3994
  *
2714
3995
  * 1. `cache` (query string → result data) — TTL-based with max size, for avoiding re-execution
2715
- * of recently validated queries. LRU eviction when max size exceeded.
3996
+ * of recently validated queries. True LRU eviction: reads bubble entries to the back via
3997
+ * delete+re-set so the oldest *unused* entry is evicted, not the oldest *inserted*.
2716
3998
  *
2717
3999
  * 2. Encrypted queryId tokens — SQL is encrypted into the queryId itself (self-contained).
2718
4000
  * No server-side storage needed for SQL mappings. The token is decrypted on each request.
@@ -2739,11 +4021,16 @@ declare class QueryCache {
2739
4021
  */
2740
4022
  getTTL(): number;
2741
4023
  /**
2742
- * Store query result in data cache
4024
+ * Store query result in data cache.
4025
+ * If the key already exists, it's removed first so the re-insert places it
4026
+ * at the back of the iteration order (LRU). Eviction only fires when adding
4027
+ * a genuinely new key past the size limit.
2743
4028
  */
2744
4029
  set(query: string, data: any): void;
2745
4030
  /**
2746
- * Get cached result if exists and not expired
4031
+ * Get cached result if exists and not expired.
4032
+ * On hit, re-inserts the entry so it moves to the back of the Map's
4033
+ * iteration order — turning FIFO eviction into true LRU.
2747
4034
  */
2748
4035
  get(query: string): any | null;
2749
4036
  /**
@@ -2849,6 +4136,187 @@ declare class DashboardConversationHistory {
2849
4136
  }
2850
4137
  declare const dashboardConversationHistory: DashboardConversationHistory;
2851
4138
 
4139
+ /**
4140
+ * Whole-dashboard generation via Pi, a terminal coding agent — as opposed to
4141
+ * DASH_COMP_REQ's single-widget-at-a-time flow. Runs Pi in-process via its
4142
+ * SDK (createAgentSession), not as a subprocess: no shell, no argument
4143
+ * quoting, no stdin/stdout piping, none of the Windows-specific subprocess
4144
+ * issues that came with spawning the `pi` CLI directly.
4145
+ *
4146
+ * Called from sdk-nodejs/src/dashboardAgent/index.ts (DASHBOARD_AGENT_REQ),
4147
+ * which owns the generic streaming/abort machinery (mirrors USER_PROMPT_REQ)
4148
+ * and passes `signal`/`onProgress` alongside the normal params — this stays
4149
+ * within CollectionHandler's loose (params) => Promise<result> typing, no
4150
+ * change needed to that shared type.
4151
+ *
4152
+ * This mechanism is generic and reusable across any deployment. What's
4153
+ * genuinely project-specific — where AGENTS.md lives, which model to use —
4154
+ * is supplied via `DashboardAgentCollectionConfig`, with defaults sensible
4155
+ * enough that most callers don't need to override them (see below). The
4156
+ * data-source tool list and the dashboard's current state both come from
4157
+ * things sdk-nodejs already exposes generically: `sdk.getTools()` (whatever
4158
+ * this deployment registered via `sdk.setTools()`) and `sdk.callCollection
4159
+ * ('dashboards', 'query', ...)` (whatever this deployment already registered
4160
+ * under that name/shape) — no per-deployment callback needed for either.
4161
+ * Same convention on the way out: after a successful run, the prompt and the
4162
+ * full response text are handed to `sdk.callCollection('dashboard-agent-
4163
+ * conversations', 'create', ...)` if this deployment has registered one —
4164
+ * skipped silently otherwise, since conversation history is optional.
4165
+ *
4166
+ * Pi verifies every query against the live database itself (via whatever
4167
+ * local tool-execution bridge the deployment exposes, e.g. an HTTP bridge
4168
+ * on localhost), but does NOT persist the result itself — it writes the
4169
+ * finished DSL to an absolute path inside `runtimeDir`, told to it explicitly
4170
+ * in the prompt, and stops there. This handler reads that file after the run
4171
+ * finishes and returns its content as `dashboard` in the result. The caller
4172
+ * (frontend) is the one that actually saves it, via whatever authenticated
4173
+ * create/update path any other dashboard edit goes through — Pi has no user
4174
+ * session/auth context of its own, so persistence shouldn't happen from
4175
+ * inside it.
4176
+ *
4177
+ * Session persistence: the FIRST call for a dashboardId pays the full cost
4178
+ * (explore KB, discover schema, plan, verify, build). Every call after that
4179
+ * resumes the same session file (SessionManager.open) so Pi has everything
4180
+ * it already learned — it only needs to reason about the new, smaller ask,
4181
+ * not rediscover the whole dashboard from scratch. The session's file path
4182
+ * (AgentSession.sessionFile) is captured right after creation and persisted
4183
+ * in a small local file, keyed by dashboardId, inside `runtimeDir`.
4184
+ */
4185
+ interface DashboardAgentCollectionConfig {
4186
+ /**
4187
+ * Working directory Pi runs from — must contain AGENTS.md. This is a
4188
+ * version-controlled prompt file, so `cwd` is expected to live somewhere
4189
+ * like a `.prompts/` folder alongside the deployment's other prompts.
4190
+ * Default: `<process.cwd()>/.prompts/dashboard-agent` — the same
4191
+ * process.cwd()-based convention PromptLoader already uses for the main
4192
+ * agent's prompts, which needs no explicit override in the common case
4193
+ * (the backend process's own cwd already is its project root).
4194
+ */
4195
+ cwd?: string;
4196
+ /**
4197
+ * Where drafts/, the session-id map, and dashboard.log get written —
4198
+ * separate from `cwd` deliberately, so this deployment's runtime state
4199
+ * (regenerated per session, safe to gitignore) doesn't sit inside the
4200
+ * same folder as the version-controlled AGENTS.md prompt.
4201
+ * Default: `<process.cwd()>/.pi-dashboard-agent-runtime`.
4202
+ */
4203
+ runtimeDir?: string;
4204
+ /** Model provider (default: process.env.PI_AGENT_PROVIDER || 'openrouter'). */
4205
+ provider?: string;
4206
+ /** Model id (default: process.env.PI_AGENT_MODEL || 'anthropic/claude-sonnet-4.5'). */
4207
+ model?: string;
4208
+ /**
4209
+ * true (default): every request starts a brand-new pi session, with the 2
4210
+ * most recent prior responses (if any) injected into the prompt as
4211
+ * context — bounded cost per request, but pi re-explores schema/KB facts
4212
+ * it already verified in an earlier turn on this same dashboard.
4213
+ * false: resumes the same session file across requests on a given
4214
+ * dashboard — pi keeps everything it already learned, but context (and
4215
+ * cost) grows unbounded across turns (one observed turn: 2M+ cache-read
4216
+ * tokens after a handful of edits on the same dashboard).
4217
+ * Default: process.env.PI_AGENT_FRESH_SESSION !== 'false'.
4218
+ */
4219
+ freshSession?: boolean;
4220
+ }
4221
+ declare function registerDashboardAgentCollection(sdk: SuperatomSDK, config?: DashboardAgentCollectionConfig): void;
4222
+
4223
+ /**
4224
+ * ScriptMatcher — LLM-Based Script Matching + Parameter Extraction
4225
+ *
4226
+ * Uses ONE LLM call to:
4227
+ * 1. Pick the best matching script from the library (or "none")
4228
+ * 2. Extract parameter values from the user question
4229
+ *
4230
+ * Why LLM over embeddings:
4231
+ * - Embeddings capture topic similarity ("overstock" ≈ "inventory" ≈ "revenue")
4232
+ * but can't distinguish structurally different questions about the same domain
4233
+ * - LLM understands that "overstock by warehouse" needs a different script than
4234
+ * "revenue by warehouse" even though they're semantically close
4235
+ * - One call does both matching AND parameter extraction
4236
+ *
4237
+ * When script library grows past ~50, add an embedding pre-filter
4238
+ * (ChromaDB narrows to top 10 → LLM picks from those 10).
4239
+ */
4240
+
4241
+ declare class ScriptMatcher {
4242
+ private store;
4243
+ constructor(store: ScriptStore);
4244
+ /**
4245
+ * Find the best matching script for a user question.
4246
+ * Uses ONE LLM call that picks the script AND extracts parameters.
4247
+ * Returns null if no script matches.
4248
+ */
4249
+ match(userPrompt: string, apiKey?: string, model?: string, signal?: AbortSignal,
4250
+ /**
4251
+ * Recent script-backed answers in this thread, newest first. Presence of at
4252
+ * least one is what makes the `edit` tier reachable at all (see the guards
4253
+ * below), and handing over SEVERAL is what lets an instruction name its own
4254
+ * target instead of always hitting the most recent script.
4255
+ */
4256
+ activeBindings?: ScriptBinding[],
4257
+ /**
4258
+ * Recent conversation turns, most recent last. The gate needs this to tell
4259
+ * "this reply is an edit instruction for the active script" apart from
4260
+ * "this reply is answering a clarifying question about a DIFFERENT,
4261
+ * unresolved topic that never became a script" — a bare parameter-shaped
4262
+ * reply ("April 2025 to March 2026") is textually indistinguishable
4263
+ * between those two cases without seeing what the assistant just asked.
4264
+ * Once this tier is decided, nothing downstream re-checks it — the edit
4265
+ * path hands MainAgent a system prompt that explicitly instructs it to
4266
+ * trust the premise and not treat the turn as a new question — so this
4267
+ * gate is the only place that can catch a mismatch.
4268
+ */
4269
+ conversationHistory?: string): Promise<ScriptMatch | null>;
4270
+ /**
4271
+ * Build the script catalog string for the LLM prompt.
4272
+ * Each script gets: index, ID, name, description, and parameter definitions.
4273
+ */
4274
+ private buildScriptCatalog;
4275
+ /**
4276
+ * The recent script-backed answers in this thread — the bounded set an edit
4277
+ * may target. Rendered as its own prompt section (never merged into the
4278
+ * ranked catalog) so the `edit` rules have an unambiguous referent set, and
4279
+ * numbered newest-first so the prompt's "prefer the most recent when the
4280
+ * instruction is ambiguous" tie-break has something to point at.
4281
+ *
4282
+ * Each entry carries the QUESTION that produced it plus the columns it
4283
+ * returned — that is what lets the matcher resolve "use the mode for the WSP
4284
+ * one" instead of blindly taking the newest.
4285
+ */
4286
+ private buildActiveScriptBlock;
4287
+ }
4288
+
4289
+ /**
4290
+ * ScriptRunner — Execute scripts in an isolated tsx subprocess.
4291
+ *
4292
+ * The subprocess approach replaces the earlier `new Function()` eval and gives us:
4293
+ * - Real sandbox (separate process, SIGKILL on timeout).
4294
+ * - Real TypeScript (tsx transpiles on the fly).
4295
+ * - npm imports available to scripts (clustering, stats, geo, etc.).
4296
+ *
4297
+ * Protocol: NDJSON over the child's stdin/stdout. See script-ipc.ts + backend/docs/SCRIPT-FLOW-IMPLEMENTATION.md.
4298
+ */
4299
+
4300
+ interface RunScriptOptions {
4301
+ /** Data sources the script is allowed to query via ctx.query */
4302
+ externalTools: ExternalTool[];
4303
+ /** Optional — for propagating per-query UI progress to the user */
4304
+ streamBuffer?: StreamBuffer;
4305
+ /** Override the wall-clock timeout (default `SCRIPT_TIMEOUT_MS`, 60s). */
4306
+ timeoutMs?: number;
4307
+ /**
4308
+ * Per-turn cancellation signal. When the user hits "Stop" mid-run, the child
4309
+ * process group is SIGKILLed and the run resolves as an aborted failure (the
4310
+ * caller is already unwinding, so the result is discarded).
4311
+ */
4312
+ signal?: AbortSignal;
4313
+ }
4314
+ /**
4315
+ * Execute a recipe by spawning a tsx child on the script's .ts file.
4316
+ * `scriptPath` is the absolute path to the saved `.ts` body.
4317
+ */
4318
+ declare function runScript(recipe: ScriptRecipe, scriptPath: string, params: Record<string, any>, options: RunScriptOptions): Promise<ScriptResult>;
4319
+
2852
4320
  type MessageTypeHandler = (message: IncomingMessage) => void | Promise<void>;
2853
4321
  declare class SuperatomSDK {
2854
4322
  private ws;
@@ -2884,6 +4352,8 @@ declare class SuperatomSDK {
2884
4352
  private lastPong;
2885
4353
  private readonly PING_INTERVAL_MS;
2886
4354
  private readonly PONG_TIMEOUT_MS;
4355
+ private pendingOutbox;
4356
+ private readonly MAX_OUTBOX_SIZE;
2887
4357
  constructor(config: SuperatomSDKConfig);
2888
4358
  /**
2889
4359
  * Initialize PromptLoader and load prompts into memory
@@ -2928,6 +4398,20 @@ declare class SuperatomSDK {
2928
4398
  * Does NOT throw on closed connections — callers can check the return value if needed.
2929
4399
  */
2930
4400
  send(message: Message): boolean;
4401
+ /**
4402
+ * Queue a message that couldn't be delivered because the socket was down,
4403
+ * to be resent once it reconnects. Drops the oldest entry once the bound is
4404
+ * hit — an outage long enough to fill this queue means the oldest queued
4405
+ * responses are for requests the caller has likely already given up on.
4406
+ */
4407
+ private queuePendingMessage;
4408
+ /**
4409
+ * Resend everything queued while the socket was down, now that it's back
4410
+ * up. Uses this.ws.send() directly (not send()) so a message that fails
4411
+ * again goes back through queuePendingMessage() rather than being silently
4412
+ * dropped a second time.
4413
+ */
4414
+ private flushPendingOutbox;
2931
4415
  /**
2932
4416
  * Register a message handler to receive all messages
2933
4417
  */
@@ -2967,6 +4451,14 @@ declare class SuperatomSDK {
2967
4451
  */
2968
4452
  private handlePong;
2969
4453
  private storeComponents;
4454
+ /**
4455
+ * The live, frontend-registered component catalog (name, type, description,
4456
+ * and full prop schema per component) — the same authoritative source
4457
+ * DASH_COMP_REQ's LLM prompt is built from. Exposed so other integrations
4458
+ * (e.g. the dashboard-agent script bridge) can read real component
4459
+ * contracts instead of maintaining a separate, driftable hand-written copy.
4460
+ */
4461
+ getComponents(): Component[];
2970
4462
  /**
2971
4463
  * Set tools for the SDK instance
2972
4464
  */
@@ -2975,6 +4467,16 @@ declare class SuperatomSDK {
2975
4467
  * Get the stored tools
2976
4468
  */
2977
4469
  getTools(): Tool$1[];
4470
+ /**
4471
+ * Call a registered collection operation in-process — no WebSocket
4472
+ * round-trip, since the caller is already running inside this same SDK
4473
+ * instance. Lets SDK-internal features (e.g. the dashboard agent) reuse
4474
+ * whatever collection a deployment has already registered (e.g.
4475
+ * 'dashboards'.'query') by name/convention, instead of requiring a
4476
+ * separate callback purely to re-expose data a collection already serves.
4477
+ * Throws if the collection or operation isn't registered.
4478
+ */
4479
+ callCollection<TResult = any>(collectionName: string, operation: string, params?: any): Promise<TResult>;
2978
4480
  /**
2979
4481
  * Register workflow components for the SDK instance.
2980
4482
  *
@@ -3018,4 +4520,4 @@ declare class SuperatomSDK {
3018
4520
  getConversationSimilarityThreshold(): number;
3019
4521
  }
3020
4522
 
3021
- 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 };
4523
+ 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 };