@superatomai/sdk-node 0.0.66 → 0.0.67-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,1266 @@
1
1
  import { z } from 'zod';
2
2
  import Anthropic from '@anthropic-ai/sdk';
3
3
 
4
+ /**
5
+ * Script Flow Types
6
+ *
7
+ * Defines interfaces for the script-based query architecture:
8
+ * - ScriptRecipe: metadata for matching, validation, and quality tracking
9
+ * - ScriptResult: output from executing a script
10
+ * - ScriptMatch: result from the LLM-based script matcher
11
+ */
12
+
13
+ /**
14
+ * Recipe metadata stored alongside each script.
15
+ * Used for matching, validation, and quality tracking.
16
+ */
17
+ interface ScriptRecipe {
18
+ /** Unique script identifier */
19
+ id: string;
20
+ /** Version number (incremented on regeneration) */
21
+ version: number;
22
+ /** Human-readable name (e.g., "Revenue by Dimension") */
23
+ name: string;
24
+ /** Natural language description of what this script does */
25
+ intentDescription: string;
26
+ /** Keyword tags for quick filtering */
27
+ tags: string[];
28
+ /** Source tool IDs this script queries (e.g., ["mssql-abc123_query"]) */
29
+ sourceIds: string[];
30
+ /** Table names used (for future schema drift detection) */
31
+ tables: string[];
32
+ /** Parameter definitions — what can vary */
33
+ parameters: ScriptParameter[];
34
+ /** The script function body as a string. Loaded from disk (scripts-store/<fileBase>.ts). */
35
+ scriptBody: string;
36
+ /**
37
+ * On-disk filename stem for the body: scripts-store/<fileBase>.ts.
38
+ * Editable in the IDE. Decided at authoring time (slug of `name`, with a
39
+ * short id suffix on collision) and stable across promotion.
40
+ */
41
+ fileBase?: string;
42
+ /** sha256 of the on-disk body — lets the runtime detect manual edits. */
43
+ bodyHash?: string;
44
+ /** Project scope (single-VM deployments may leave this undefined). */
45
+ projectId?: string;
46
+ /** Times this script was used successfully */
47
+ successCount: number;
48
+ /** Times this script failed */
49
+ failureCount: number;
50
+ /** ISO timestamp of last usage */
51
+ lastUsed: string;
52
+ /** Original user question that created this script */
53
+ createdFrom: string;
54
+ /** ISO timestamp */
55
+ createdAt: string;
56
+ /** ISO timestamp */
57
+ updatedAt: string;
58
+ /**
59
+ * `recipe.id` of the parent this script was forked from.
60
+ * Undefined for root scripts (those written from scratch by MainAgent).
61
+ * See backend/docs/SCRIPT-FLOW-FORK-ADAPT.md.
62
+ */
63
+ parentId?: string;
64
+ /** 0 for root scripts; `parent.forkDepth + 1` for forks. Capped at 3. */
65
+ forkDepth?: number;
66
+ /**
67
+ * Brief description of what this fork changed vs its parent
68
+ * (sourced from the matcher's `modificationHint`).
69
+ */
70
+ forkReason?: string;
71
+ /**
72
+ * Validated component specs captured at authoring time. On a tier-high
73
+ * replay these are rebound to fresh queryIds deterministically — no
74
+ * component-generation LLM call, and the rendered columns can't drift from
75
+ * what was validated when the script was authored. Absent on recipes
76
+ * authored before this landed; those fall back to LLM component generation.
77
+ * See backend/docs/SCRIPT-COMPONENT-CONSISTENCY.md.
78
+ */
79
+ components?: ScriptComponentSpec[];
80
+ /**
81
+ * What the user explicitly asked the output to LOOK like, set by a display
82
+ * edit ("show that as a bar chart", "make the bars horizontal").
83
+ *
84
+ * Deliberately SEPARATE from `components`. Those are validated bindings that
85
+ * must be cleared on a data edit — after "break it down by brand" the axis
86
+ * keys are wrong, and after "use the mode" the stored title still says
87
+ * "Average…". A rendering CHOICE, by contrast, is still valid afterwards.
88
+ * Keeping them in one field meant every data edit silently discarded the
89
+ * user's chart type.
90
+ *
91
+ * Fed to the component generator as a hint whenever specs are (re)generated,
92
+ * so the chosen rendering comes back even after the SQL changes.
93
+ */
94
+ displayPreference?: ScriptDisplayPreference;
95
+ /**
96
+ * Lifecycle stage of this recipe on disk.
97
+ * - 'draft': written by MainAgent's write_script during a turn; filtered out
98
+ * of FTS results (status='verified' only) so the matcher never picks it.
99
+ * Filename is suffixed with `turnId` to keep concurrent turns
100
+ * from clobbering each other's drafts.
101
+ * - 'verified': promoted after `execute_script` succeeded; the matcher sees it.
102
+ * Filename drops the turn suffix unless a verified file with
103
+ * the same slug already exists (collision case keeps the suffix).
104
+ *
105
+ * Recipes loaded from disk without this field default to 'verified' so
106
+ * existing scripts keep working unchanged.
107
+ */
108
+ status?: 'draft' | 'verified';
109
+ /**
110
+ * Per-turn unique suffix used for draft filenames (e.g. `1714745623-x9k2`).
111
+ * Set when the draft is saved; carried until the recipe is promoted.
112
+ */
113
+ turnId?: string;
114
+ /**
115
+ * Last execution error captured by `recordDraftError` while the recipe was
116
+ * still a draft. Lets users open the draft .json file and see why it failed
117
+ * without grepping logs. Cleared on promotion to 'verified'.
118
+ */
119
+ lastError?: {
120
+ phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
121
+ message: string;
122
+ at: string;
123
+ attempt: number;
124
+ };
125
+ /** userId who committed the most recent edit (audit trail — recipes are project-scoped). */
126
+ editedBy?: string;
127
+ /** The instruction that produced the current version. */
128
+ editedFrom?: string;
129
+ /**
130
+ * Prior versions, newest last. The body itself is archived on disk as
131
+ * `<fileBase>.v<version>.ts`; this records what changed and who did it.
132
+ */
133
+ history?: ScriptVersionRecord[];
134
+ }
135
+ /**
136
+ * A durable, user-stated rendering choice for a recipe. Survives data edits.
137
+ */
138
+ interface ScriptDisplayPreference {
139
+ /** Component types the user's chosen rendering resolved to, e.g. ["DynamicBarChart"]. */
140
+ componentTypes: string[];
141
+ /** The instruction itself — carries nuance the types can't ("horizontal", "sorted descending"). */
142
+ instruction: string;
143
+ /** ISO timestamp of the display edit that set this. */
144
+ at: string;
145
+ }
146
+ /** One superseded version of a recipe body (see ScriptStore.commitEdit). */
147
+ interface ScriptVersionRecord {
148
+ /** Version number this record superseded (i.e. the OLD version). */
149
+ version: number;
150
+ /** ISO timestamp of the edit that superseded it. */
151
+ at: string;
152
+ /** userId who made the edit, when known. */
153
+ by?: string;
154
+ /** The edit instruction that caused the supersede. */
155
+ instruction?: string;
156
+ /** One-line summary of what changed, for the version picker. */
157
+ changeSummary?: string;
158
+ /** sha256 of the superseded body — pairs with `<fileBase>.v<version>.ts`. */
159
+ bodyHash?: string;
160
+ }
161
+ interface ScriptParameter {
162
+ /** Parameter name (used in script body as params.name) */
163
+ name: string;
164
+ /** Parameter type */
165
+ type: 'string' | 'number' | 'date' | 'date_range' | 'enum' | 'boolean';
166
+ /** Whether this parameter is required */
167
+ required: boolean;
168
+ /** Default value if not provided */
169
+ default?: any;
170
+ /** For enum type — maps user-facing values to internal values */
171
+ enumValues?: Record<string, string>;
172
+ /** Human-readable description (used in the matcher LLM prompt) */
173
+ description: string;
174
+ }
175
+ /**
176
+ * A reusable component binding captured when a script is authored. Stored on
177
+ * the recipe so tier-high replays rebuild components deterministically (rebind
178
+ * to fresh queryIds) instead of re-running the component-picker LLM.
179
+ */
180
+ interface ScriptComponentSpec {
181
+ /** Registered component name (e.g. "DynamicBarChart") — matched against the available component library. */
182
+ componentType: string;
183
+ /** `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). */
184
+ sourceRef: string;
185
+ /** Present only when sourceRef === 'federation' — the DuckDB SQL to re-execute on replay. */
186
+ federationSql?: string;
187
+ /** Present only when sourceRef === 'markdown' — the narrative text to render on replay (markdown has no data source, so its content must be persisted). */
188
+ content?: string;
189
+ title?: string;
190
+ description?: string;
191
+ /** Validated axis/value keys + aggregation — all referencing real columns of the bound source. */
192
+ config: Record<string, any>;
193
+ }
194
+ /**
195
+ * Result from executing a script via ScriptRunner.
196
+ */
197
+ interface ScriptResult {
198
+ /** Whether the script executed successfully */
199
+ success: boolean;
200
+ /** Combined data from all queries */
201
+ data: any[];
202
+ /** Program-generated narrative, when the script exports getAnalysis. */
203
+ analysis?: AnalysisOutput;
204
+ /**
205
+ * getAnalysis threw. `success` stays true — the data is valid, so the caller
206
+ * falls back to LLM prose rather than losing the answer.
207
+ */
208
+ analysisError?: string;
209
+ /** Program-generated component specs, when the script exports getComponents. */
210
+ components?: ScriptComponentSpec[];
211
+ /**
212
+ * getComponents threw or returned a non-array. `success` stays true — the data
213
+ * is valid, so the caller falls back to the recipe's stored specs and then to
214
+ * LLM generation rather than losing the dashboard.
215
+ */
216
+ componentsError?: string;
217
+ /** Individual query results tracked during execution */
218
+ executedQueries: ScriptQueryResult[];
219
+ /** Error message if failed */
220
+ error?: string;
221
+ /**
222
+ * Where in the lifecycle the error occurred. Lets MainAgent's fix-loop
223
+ * decide between "rewrite the whole draft" (compile) and "patch the
224
+ * specific line" (runtime).
225
+ */
226
+ errorPhase?: 'compile' | 'runtime' | 'timeout' | 'ipc';
227
+ /** Total execution time in milliseconds */
228
+ executionTimeMs: number;
229
+ }
230
+ /**
231
+ * A single query executed during script runtime.
232
+ * Tracked by ScriptContext for component generation and debugging.
233
+ */
234
+ interface ScriptQueryResult {
235
+ /** Source tool ID */
236
+ sourceId: string;
237
+ /** Human-readable source name */
238
+ sourceName: string;
239
+ /** The SQL that was executed */
240
+ sql: string;
241
+ /** Result data rows */
242
+ data: any[];
243
+ /** Number of rows returned */
244
+ count: number;
245
+ /** Total rows that matched before limit (if available) */
246
+ totalCount?: number;
247
+ /** Query execution time in milliseconds */
248
+ executionTimeMs: number;
249
+ /**
250
+ * True for rows that did NOT come from a real SQL execution — either a
251
+ * ctx.emit() dataset or the synthesized "computed:_final" entry that
252
+ * carries the script's post-JS returned data. The component generator
253
+ * uses this to route the resulting component through the script_dataset
254
+ * sentinel toolId so the frontend resolves it via the queryCache short-circuit.
255
+ */
256
+ virtual?: boolean;
257
+ /**
258
+ * Restrictions an authorization policy injected into THIS query before it
259
+ * ran. Handed to getAnalysis/getComponents as `scope` so the answer can name
260
+ * the restriction without the recipe hardcoding a value that goes stale.
261
+ */
262
+ appliedPolicies?: string[];
263
+ }
264
+ /**
265
+ * Match tier returned by the LLM script matcher.
266
+ *
267
+ * - 'high': the script answers the question directly; only parameter values
268
+ * may differ. The runtime replays it with extracted params (cheapest path).
269
+ * - 'near': the script answers a STRUCTURALLY similar question but needs
270
+ * body modification (different metric, dimension, table, filter shape).
271
+ * The runtime forks the parent and adapts the body via MainAgent's normal
272
+ * write_script + execute_script loop — no SourceAgent dispatch needed.
273
+ * See backend/docs/SCRIPT-FLOW-FORK-ADAPT.md for the full design.
274
+ * - 'edit': the user is INSTRUCTING a change to the script that produced the
275
+ * previous answer (not asking a new question). Only reachable when the turn
276
+ * carries a ScriptBinding — without one the matcher coerces it to 'none', so
277
+ * a prompt regression can't turn this into a loose similarity path. The
278
+ * runtime runs MainAgent in edit mode and commits the result in place.
279
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md.
280
+ * - 'none': no script is relevant; full agent flow runs.
281
+ */
282
+ type MatchTier = 'high' | 'near' | 'edit' | 'none';
283
+ /**
284
+ * Which recipe produced the answer the user is currently looking at.
285
+ *
286
+ * Set whenever a turn's answer came from a script (replay, fresh authoring, or
287
+ * a committed edit) and carried on the UIBlock + saved conversation row. Two
288
+ * consumers:
289
+ * 1. The matcher — the 'edit' tier is ONLY reachable when a binding exists, so
290
+ * "use mode instead of mean" resolves to a concrete script instead of being
291
+ * matched on keywords it shares with no script name.
292
+ * 2. Cache invalidation — after an edit commits, conversations bound to that
293
+ * recipeId must be dropped, or the exact-match cache replays the pre-edit
294
+ * answer and the edit looks like a no-op.
295
+ *
296
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 1.
297
+ */
298
+ interface ScriptBinding {
299
+ recipeId: string;
300
+ /**
301
+ * The recipe's own version number at the moment THIS turn ran — not
302
+ * necessarily the recipe's current version. A later edit bumps the live
303
+ * version, so without this a past turn's binding would silently point at
304
+ * whatever the recipe looks like today instead of what actually produced
305
+ * that answer. Absent on bindings created before this field existed.
306
+ */
307
+ version?: number;
308
+ /** Params the script ran with — the edit's starting point. */
309
+ params: Record<string, any>;
310
+ /** Recipe name at bind time (matcher catalog + user-facing confirmation). */
311
+ name: string;
312
+ /** Columns the last run returned — grounds the editor without a re-query. */
313
+ columns?: string[];
314
+ /**
315
+ * The question that produced this answer. Required for disambiguation when a
316
+ * thread ran several scripts — without it the candidates are just names and
317
+ * the matcher cannot resolve "use the mode for the WSP one".
318
+ */
319
+ userPrompt?: string;
320
+ /**
321
+ * The polished analysis text this script actually produced last time it ran
322
+ * (the same programAnswer Thread.getConversationContext() shows elsewhere).
323
+ * Carried alongside the script's own shape (columns, params) so the matcher
324
+ * sees "what did this script actually say" right next to "what can I ask it
325
+ * to change", instead of needing a separate conversation-history transcript
326
+ * that would otherwise just repeat the same question this block already
327
+ * states. This is why ScriptMatcher's own prompt no longer receives a raw
328
+ * conversation-history transcript at all — this field is what replaced it.
329
+ */
330
+ answerSummary?: string;
331
+ }
332
+ /**
333
+ * Result from the LLM-based script matcher.
334
+ *
335
+ * For `tier: 'high'`, `extractedParams` carries the values to pass to the
336
+ * existing script. For `tier: 'near'`, `gaps` and `modificationHint` describe
337
+ * what the fork-author needs to change in the parent body.
338
+ */
339
+ interface ScriptMatch {
340
+ /** The matched script recipe */
341
+ recipe: ScriptRecipe;
342
+ /** Match tier — see MatchTier docs */
343
+ tier: MatchTier;
344
+ /** Similarity score (0-1, derived from LLM tier) */
345
+ similarity: number;
346
+ /**
347
+ * Legacy confidence level. Mirrors `tier === 'high'`/`'near'` for now;
348
+ * kept so existing callers compile while we migrate to tier-based logic.
349
+ */
350
+ confidence: 'high' | 'medium';
351
+ /** Parameters extracted from the user question by the LLM (tier='high') */
352
+ extractedParams?: Record<string, any>;
353
+ /**
354
+ * Proper nouns the question named, VERBATIM, with the parameter each fills.
355
+ *
356
+ * The matcher must NOT resolve these itself. Observed live: it substituted the
357
+ * customer name "PREMER ENRGES PHOTOOTAIC" into an id parameter believing that
358
+ * "the runtime will resolve the customer name to its PartyId(s)" — nothing on
359
+ * the replay path does, and `WHERE BillingPartyId IN (PREMER ENRGES
360
+ * PHOTOOTAIC)` reached SQL. Reported here, the runtime resolves them before
361
+ * replay and rejects the match when one cannot be resolved.
362
+ */
363
+ mentions?: Array<{
364
+ text: string;
365
+ param?: string;
366
+ }>;
367
+ /** Business concepts the question relies on (e.g. "outstanding balance"). */
368
+ entityTypes?: string[];
369
+ /** What the user question needs that the parent doesn't cover (tier='near') */
370
+ gaps?: string[];
371
+ /** One-sentence description of the change the fork-author should make (tier='near') */
372
+ modificationHint?: string;
373
+ /**
374
+ * Which HALF of the recipe the edit targets (tier='edit'). A recipe has two
375
+ * independently editable halves:
376
+ * - 'data' — the scriptBody: how the rows are produced (aggregation,
377
+ * filters, joins, grouping). Runs MainAgent in edit mode.
378
+ * - 'display' — the component specs: how those SAME rows are shown (chart
379
+ * type, orientation, columns, labels). Replays the proven SQL
380
+ * and regenerates the specs — never authors a script.
381
+ * Defaults to 'data' when the matcher omits it.
382
+ */
383
+ editTarget?: 'data' | 'display';
384
+ /**
385
+ * Self-contained restatement of the change the user asked for (tier='edit').
386
+ * MUST have pronouns/deixis resolved ("this", "it", "that column") — the edit
387
+ * prompt never sees the conversation history, so an unresolved instruction is
388
+ * unusable downstream.
389
+ */
390
+ editInstruction?: string;
391
+ /** Why the matcher made this choice (for logs and telemetry) */
392
+ reasoning?: string;
393
+ }
394
+
395
+ /**
396
+ * script-ipc.ts — Protocol definitions for parent/child IPC.
397
+ *
398
+ * Transport: newline-delimited JSON over the child's stdin/stdout.
399
+ * stderr is passed through for uncaught exceptions and compile errors.
400
+ *
401
+ * See backend/docs/SCRIPT-FLOW-IMPLEMENTATION.md § IPC Bridge.
402
+ */
403
+
404
+ /**
405
+ * A name the user said, and what it resolved to.
406
+ *
407
+ * Handed to getAnalysis/getComponents so a narrative can NAME the thing the
408
+ * question was about without typing the name in. Before this, the runtime knew
409
+ * "Fusion5 Pty Ltd" and used that knowledge only to REJECT scripts that typed
410
+ * it — with no sanctioned way to obtain it, the only legal escape was to add a
411
+ * column to getData and re-run the query, spending an execute attempt to learn
412
+ * something the runtime already knew.
413
+ */
414
+ interface ResolvedEntityRef {
415
+ /** The text the user actually wrote. */
416
+ mention: string;
417
+ entityType: string;
418
+ instanceId: string;
419
+ displayName: string;
420
+ }
421
+ /**
422
+ * What getAnalysis returns. `analysis` is the narrative and `summary` the short
423
+ * bullets the markdown block binds to — siblings over one dataset, never a
424
+ * summary OF the narrative. See backend/docs/ANALYSIS-AS-PROGRAM-DESIGN.md § 4.3.
425
+ */
426
+ interface AnalysisOutput {
427
+ analysis?: string;
428
+ summary?: string[];
429
+ method?: string;
430
+ }
431
+
432
+ /**
433
+ * StreamBuffer - Buffered streaming utility for smoother text delivery
434
+ * Batches small chunks together and flushes at regular intervals
435
+ */
436
+ type StreamCallback = (chunk: string) => void;
437
+ /**
438
+ * StreamBuffer class for managing buffered streaming output
439
+ * Provides smooth text delivery by batching small chunks
440
+ */
441
+ declare class StreamBuffer {
442
+ private buffer;
443
+ private flushTimer;
444
+ private callback;
445
+ private fullText;
446
+ constructor(callback?: StreamCallback);
447
+ /**
448
+ * Check if the buffer has a callback configured
449
+ */
450
+ hasCallback(): boolean;
451
+ /**
452
+ * Get all text that has been written (including already flushed)
453
+ */
454
+ getFullText(): string;
455
+ /**
456
+ * Write a chunk to the buffer
457
+ * Large chunks or chunks with newlines are flushed immediately
458
+ * Small chunks are batched and flushed after a short interval
459
+ *
460
+ * @param chunk - Text chunk to write
461
+ */
462
+ write(chunk: string): void;
463
+ /**
464
+ * Flush the buffer immediately
465
+ * Call this before tool execution or other operations that need clean output
466
+ */
467
+ flush(): void;
468
+ /**
469
+ * Internal flush implementation
470
+ */
471
+ private flushNow;
472
+ /**
473
+ * Clean up resources
474
+ * Call this when done with the buffer
475
+ */
476
+ dispose(): void;
477
+ }
478
+
479
+ /**
480
+ * ToolExecutorService - Handles execution of SQL queries and external tools
481
+ * Extracted from BaseLLM.generateTextResponse for better separation of concerns
482
+ */
483
+
484
+ /**
485
+ * External tool definition
486
+ */
487
+ interface ExternalTool {
488
+ id: string;
489
+ name: string;
490
+ description?: string;
491
+ /** Tool type: "source" = routed through SourceAgent, "direct" = called directly by MainAgent */
492
+ toolType?: 'source' | 'direct';
493
+ /** Full untruncated schema for source agent (all columns visible) */
494
+ fullSchema?: string;
495
+ /** Schema size tier: small (≤50 tables), medium (51-200), large (201-500), very_large (500+) */
496
+ schemaTier?: string;
497
+ /** Schema search function for very_large tier — keyword search over entities */
498
+ schemaSearchFn?: (keywords: string[]) => string;
499
+ /** `signal` fires when the user hits Stop mid-query — passed through to the backend's SourceTool.fn. */
500
+ fn: (input: any, signal?: AbortSignal) => Promise<any>;
501
+ limit?: number;
502
+ outputSchema?: any;
503
+ executionType?: 'immediate' | 'deferred';
504
+ userProvidedData?: any;
505
+ params?: Record<string, any>;
506
+ }
507
+ /**
508
+ * Executed tool tracking info
509
+ */
510
+ interface ExecutedToolInfo {
511
+ id: string;
512
+ name: string;
513
+ params: any;
514
+ result: {
515
+ _totalRecords: number;
516
+ _recordsShown: number;
517
+ _metadata?: any;
518
+ _sampleData: any[];
519
+ /** Bounded summary over the FULL fetched result (complete structure). */
520
+ _summary?: any;
521
+ /** Up to MAIN_AGENT_COMPLETE_ROWS rows — the complete result when small. */
522
+ _mainAgentRows?: any[];
523
+ /**
524
+ * Set when an authorization policy restricted the query before it ran.
525
+ * Read by BOTH agents: the source agent's tool response and the main
526
+ * agent's result block, because the main agent is the one that writes the
527
+ * titles, analysis and method line that must state the restriction.
528
+ */
529
+ _ACCESS_POLICY_APPLIED?: string;
530
+ };
531
+ /**
532
+ * THIS call's rows, at render size — deliberately a sibling of `result`, not a
533
+ * field inside it, because `result` is the object that gets picked apart for
534
+ * prompts (`_sampleData`, `_mainAgentRows`, `_summary`). Never put these in a
535
+ * prompt.
536
+ *
537
+ * Exists because a SourceAgentResult keeps only the LAST query's rows for a
538
+ * whole dispatch (see main-agent's "a follow-up query overwrites result.data"
539
+ * note). A component bound to an EARLIER query therefore rendered the later
540
+ * query's data — confirmed live: a revenue chart and a budget chart both drew
541
+ * budget, because both queries ran through one tool.
542
+ */
543
+ renderRows?: any[];
544
+ outputSchema?: any;
545
+ sourceSchema?: string;
546
+ sourceType?: string;
547
+ }
548
+
549
+ /**
550
+ * Multi-Agent Architecture Types
551
+ *
552
+ * Defines interfaces for the hierarchical agent system:
553
+ * - Main Agent: ONE LLM.streamWithTools() call with source agent tools
554
+ * - Source Agents: independent agents that query individual data sources
555
+ *
556
+ * The main agent sees only source summaries. When it calls a source tool,
557
+ * the SourceAgent runs independently (own LLM, own retries) and returns clean data.
558
+ */
559
+
560
+ /**
561
+ * Per-entity detail: name, row count, and column names.
562
+ * Gives the main agent enough context to route to the right source.
563
+ */
564
+ interface EntityDetail {
565
+ /** Entity name (table, sheet, endpoint) */
566
+ name: string;
567
+ /** Approximate row count */
568
+ rowCount?: number;
569
+ /** Column/field names */
570
+ columns: string[];
571
+ /** Entity-level semantic summary (what the table means) — for main-agent routing. */
572
+ summary?: string;
573
+ }
574
+ /**
575
+ * Representation of a data source for the main agent.
576
+ * Contains entity names WITH column names so the LLM can route accurately.
577
+ */
578
+ interface SourceSummary {
579
+ /** Source ID (matches tool ID prefix) */
580
+ id: string;
581
+ /** Human-readable source name */
582
+ name: string;
583
+ /** Source type: postgres, excel, rest_api, etc. */
584
+ type: string;
585
+ /** Brief description of what data this source contains */
586
+ description: string;
587
+ /** Detailed entity info with column names for routing */
588
+ entityDetails: EntityDetail[];
589
+ /** The tool ID associated with this source */
590
+ toolId: string;
591
+ }
592
+ /**
593
+ * What a source agent returns after querying its data source.
594
+ * The main agent uses this to analyze and compose the final response.
595
+ */
596
+ interface SourceAgentResult {
597
+ /** Source ID */
598
+ sourceId: string;
599
+ /** Source name */
600
+ sourceName: string;
601
+ /** Whether the query succeeded */
602
+ success: boolean;
603
+ /** Result data rows — bounded to MAIN_AGENT_COMPLETE_ROWS (Stage 2: what the LLM
604
+ * sees verbatim in its own prompt). NOT render-sized — do not bind a UI
605
+ * component's dataset to this field; use renderData instead. */
606
+ data: any[];
607
+ /** Result data rows bounded to MAX_ROWS_RENDERED (Stage 3: render-appropriate).
608
+ * This is what already-fetched (Stage 1, <=MAX_ROWS_FETCHED) rows look like
609
+ * before Stage 2 further truncates `data` down for the LLM's own context —
610
+ * use this, not `data`, whenever binding a UI component's dataset. */
611
+ renderData: any[];
612
+ /** Metadata about the query execution */
613
+ metadata: SourceAgentMetadata;
614
+ /** Tool execution info for the last successful query (backward compat) */
615
+ executedTool: ExecutedToolInfo;
616
+ /** All successful tool executions (primary + follow-up queries) */
617
+ allExecutedTools?: ExecutedToolInfo[];
618
+ /** Error message if failed */
619
+ error?: string;
620
+ }
621
+ interface SourceAgentMetadata {
622
+ /** Total rows that matched the query (before limit) */
623
+ totalRowsMatched: number;
624
+ /** Rows actually returned (after limit) */
625
+ rowsReturned: number;
626
+ /** Whether the result was truncated by the row limit */
627
+ isLimited: boolean;
628
+ /** The query/params that were executed */
629
+ queryExecuted?: string;
630
+ /** Execution time in milliseconds */
631
+ executionTimeMs: number;
632
+ }
633
+ /**
634
+ * A pre-built, multi-step UI flow registered with the SDK.
635
+ *
636
+ * When the main agent decides a user's question matches a workflow's whenToUse
637
+ * trigger, it picks the workflow instead of running source agents / generating
638
+ * dashboard components. The LLM extracts the workflow's required props from the
639
+ * prompt (using `propsSchema` as the tool input_schema) and the SDK returns the
640
+ * workflow component directly — no analysis text, no chart generation. The
641
+ * frontend renders the registered workflow component with the LLM-extracted
642
+ * props.
643
+ */
644
+ interface WorkflowDescriptor {
645
+ /** Unique workflow id (used as the LLM tool name) */
646
+ id: string;
647
+ /** Component name on the frontend (matches the registered React component) */
648
+ name: string;
649
+ /** Short human-readable description of what this workflow does */
650
+ description: string;
651
+ /**
652
+ * 1–2 sentence trigger condition. The LLM uses this to decide if the
653
+ * user's prompt matches this workflow. Be specific — e.g.
654
+ * "User wants to *initiate* an inventory transfer (review + submit POs),
655
+ * not just see analysis or charts."
656
+ */
657
+ whenToUse: string;
658
+ /**
659
+ * JSON-schema-style description of the props the workflow needs. Becomes
660
+ * the LLM tool's input_schema, so the model fills these from the prompt.
661
+ * Use the same shape as `params` on direct tools — string descriptors with
662
+ * an optional "(optional)" suffix.
663
+ *
664
+ * Example:
665
+ * ```
666
+ * {
667
+ * selectedStore: 'object — { id, name } of the source branch',
668
+ * minROI: 'number (optional) — only show transfers with ROI ≥ this',
669
+ * }
670
+ * ```
671
+ */
672
+ propsSchema: Record<string, string>;
673
+ /**
674
+ * Optional: static prop defaults merged with LLM-extracted props before
675
+ * the component is returned. Useful for things like the embedded
676
+ * `externalTool` config that the workflow uses to fetch its own data.
677
+ */
678
+ defaultProps?: Record<string, any>;
679
+ }
680
+ /**
681
+ * The workflow selection captured during a routing call.
682
+ * Set on AgentResponse when the LLM picks a workflow tool.
683
+ */
684
+ interface SelectedWorkflow {
685
+ /** Component name (matches WorkflowDescriptor.name) */
686
+ name: string;
687
+ /** Props extracted from the prompt + merged with workflow.defaultProps */
688
+ props: Record<string, any>;
689
+ }
690
+ /**
691
+ * Set when this turn applies a user-directed edit to an existing script instead
692
+ * of authoring a new one. MainAgent keeps the SAME harness (tools, loop,
693
+ * write_script/execute_script verification) and swaps only the system prompt —
694
+ * `agent-main-edit` instead of `agent-main`.
695
+ *
696
+ * The two framings contradict each other on whether to query a source first, so
697
+ * they must never be resident in one rendered prompt.
698
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § D3.
699
+ */
700
+ interface EditContext {
701
+ /** Recipe being edited — the shadow draft records it as parentId. */
702
+ recipeId: string;
703
+ parentName: string;
704
+ parentBody: string;
705
+ /** Self-contained restatement of the change (from the matcher). */
706
+ instruction: string;
707
+ /** Columns the last run returned — grounds the edit without a re-query. */
708
+ lastResultColumns?: string[];
709
+ /** Rendered parameter list of the parent, for the prompt. */
710
+ parentParams?: string;
711
+ }
712
+ /**
713
+ * The complete response from the multi-agent system.
714
+ * Contains everything needed for text display + component generation.
715
+ */
716
+ interface AgentResponse {
717
+ /** Generated text response (analysis of the data) */
718
+ text: string;
719
+ /**
720
+ * Program-generated narrative when the script exported getAnalysis. `text`
721
+ * already carries `analysis.analysis`; this also exposes `summary`, which
722
+ * the markdown block binds to instead of being an LLM summary of the prose.
723
+ */
724
+ analysis?: AnalysisOutput;
725
+ /** All executed tools across all source agents (for component generation) */
726
+ executedTools: ExecutedToolInfo[];
727
+ /** Individual results from each source agent */
728
+ sourceResults: SourceAgentResult[];
729
+ /**
730
+ * Populated when MainAgent wrote AND successfully executed a script during its turn.
731
+ * Caller (agent-user-response.ts) persists it via ScriptStore.save().
732
+ * Absent when MainAgent didn't write one (trivial question / all attempts failed).
733
+ */
734
+ savedScript?: AgentWrittenScript;
735
+ /** Failed execute_script tool_result texts this turn, in order — see RunTrace.script.failedAttempts. */
736
+ failedScriptAttempts?: string[];
737
+ /**
738
+ * Set when the LLM routed the question to a registered workflow component.
739
+ * When present, the upstream caller should skip component generation and
740
+ * return this workflow as the response.
741
+ */
742
+ workflow?: SelectedWorkflow;
743
+ /**
744
+ * Validated component specs the agent authored via `write_components`.
745
+ * When present the caller assembles the dashboard from these instead of
746
+ * running the component-generation LLM. Absent when the agent didn't call
747
+ * the tool — the caller then falls back to `generateScriptComponents`.
748
+ * See backend/docs/COMPONENT-GENERATION-V2.md §2.2.
749
+ */
750
+ componentSpecs?: ScriptComponentSpec[];
751
+ /** Layout title/description from the same `write_components` call. */
752
+ componentLayout?: {
753
+ title: string;
754
+ description?: string;
755
+ };
756
+ }
757
+ /**
758
+ * A script MainAgent authored + verified during its turn. Shape aligns with
759
+ * what ScriptStore.save() needs — minus store-assigned fields (id, timestamps, counts).
760
+ */
761
+ interface AgentWrittenScript {
762
+ /**
763
+ * `ScriptRecipe.id` of the draft that was authored + verified during this turn.
764
+ * The caller passes this to `ScriptStore.promoteToVerified(recipeId, …)` to
765
+ * flip the draft to verified status and (when possible) drop the turn-suffix
766
+ * from its filename.
767
+ */
768
+ recipeId: string;
769
+ name: string;
770
+ intentDescription: string;
771
+ tags: string[];
772
+ parameters: Array<{
773
+ name: string;
774
+ type: 'string' | 'number' | 'date' | 'date_range' | 'enum' | 'boolean';
775
+ required: boolean;
776
+ default?: any;
777
+ enumValues?: Record<string, string>;
778
+ description: string;
779
+ }>;
780
+ scriptBody: string;
781
+ /** Source IDs referenced by the script (extracted from ctx.query calls) */
782
+ sourceIds: string[];
783
+ /** Tables referenced in the script's SQL (regex-extracted) */
784
+ tables: string[];
785
+ /** Executed queries from the verified run — fed to component generation */
786
+ executedQueries: Array<{
787
+ sourceId: string;
788
+ sourceName: string;
789
+ sql: string;
790
+ data: any[];
791
+ count: number;
792
+ totalCount?: number;
793
+ executionTimeMs: number;
794
+ /**
795
+ * True for synthetic entries (ctx.emit datasets, the computed:_final
796
+ * post-JS data). The component generator routes virtual sources through
797
+ * the script_dataset sentinel toolId so the frontend resolves them via
798
+ * queryCache instead of attempting to re-execute SQL.
799
+ */
800
+ virtual?: boolean;
801
+ }>;
802
+ /** The tool_result JSON MainAgent's LLM got back from execute_script — see RunTrace.script.executionResult. */
803
+ executionResult?: string;
804
+ }
805
+ /**
806
+ * Debug trace for a single chat_agent turn — which KB/schema nodes were
807
+ * retrieved and considered, and which script (if any) produced the answer.
808
+ * Persisted separately from the answer itself (fusion-5's
809
+ * `conversation_traces` table, written via `saveTrace`), never sent through
810
+ * `analyticsClient` (data-residency — see design doc §5).
811
+ */
812
+ interface RunTrace {
813
+ version: 1;
814
+ kbNodes: {
815
+ /**
816
+ * Always-on nodes included regardless of the question (getGlobalKnowledgeBase).
817
+ * `content` is the node's full text AS IT WAS at retrieval time — captured so a
818
+ * later edit/delete of the KB node doesn't erase what the LLM actually saw.
819
+ */
820
+ global: Array<{
821
+ kbId: string;
822
+ title: string;
823
+ content?: string;
824
+ }>;
825
+ /** Semantically matched for this specific question (getKnowledgeBase). Same `content` reasoning as above. */
826
+ query: Array<{
827
+ kbId: string;
828
+ title: string;
829
+ similarity?: number;
830
+ content?: string;
831
+ }>;
832
+ };
833
+ schemaNodes: {
834
+ /** MainAgent's cross-source prefilter — which sources were even considered. */
835
+ routing: Array<{
836
+ sourceId: string;
837
+ sourceName?: string;
838
+ similarity: number;
839
+ }>;
840
+ /** SourceAgent-scope per-source table resolution (MainAgent.preResolveSchema). */
841
+ preResolve: Array<{
842
+ sourceId: string;
843
+ table?: string;
844
+ description?: string;
845
+ similarity: number;
846
+ }>;
847
+ /**
848
+ * The exact {{SOURCE_SUMMARIES}} text injected into MainAgent's system prompt
849
+ * (agent-prompt-builder.ts's formatSummariesForPrompt output) — every
850
+ * registered source's catalog entry, not retrieval-specific, captured once per
851
+ * turn. This is what the main agent actually saw when deciding which source(s)
852
+ * to call.
853
+ */
854
+ mainAgentCatalog?: string;
855
+ /**
856
+ * One entry per SourceAgent dispatch (a source called twice in one turn gets
857
+ * two entries) — the exact {{FULL_SCHEMA}} text injected into that
858
+ * SourceAgent's own system prompt, however it was sourced, plus any
859
+ * search_schema follow-up lookups it made mid-conversation when the schema
860
+ * shown wasn't enough.
861
+ */
862
+ sourceAgentSchemas: Array<{
863
+ sourceId: string;
864
+ sourceName?: string;
865
+ /**
866
+ * The exact intent text this dispatch's own prompt was built from —
867
+ * captured in SourceAgent.buildPrompt, so it ALREADY includes the
868
+ * resolved-entities and resolved-concepts blocks MainAgent appends by
869
+ * code before calling execute() (see buildResolvedEntitiesBlock /
870
+ * buildResolvedConceptsBlock) — not just what the LLM itself wrote.
871
+ */
872
+ intent: string;
873
+ /** How `schemaText` was sourced — see SourceAgent.buildPrompt's fullSchema fallback chain. */
874
+ method: 'preresolved-embedding' | 'tool-full-schema' | 'tool-description' | 'none';
875
+ schemaText: string;
876
+ /** In call order — each search_schema tool call this SourceAgent made, and the exact result text handed back to its own LLM. */
877
+ searchSchemaLookups: Array<{
878
+ keywords: string[];
879
+ result: string;
880
+ }>;
881
+ /** The tool_result text MainAgent's LLM read back for this dispatch (formatResultForMainAgent output). */
882
+ result?: string;
883
+ /**
884
+ * The full resolved-concept bodies this dispatch's intent was built
885
+ * with (the same content already baked into `intent` via
886
+ * buildResolvedConceptsBlock, surfaced here structured/separately so a
887
+ * trace reader doesn't have to parse it back out of the raw text).
888
+ * Stamped AFTER execute() resolves — same timing as `result` above —
889
+ * specifically so its presence here is proof the concepts were part
890
+ * of the prompt actually sent, not just of what MainAgent intended to
891
+ * send before the call happened.
892
+ */
893
+ resolvedConcepts?: Array<{
894
+ name: string;
895
+ kind: string;
896
+ body: string;
897
+ }>;
898
+ /** This dispatch's `SourceToolInput.aggregation` ('raw' | 'pre-aggregate' | 'summary') — decides which AGGREGATION_MODE text this SourceAgent's own prompt got. */
899
+ aggregation?: string;
900
+ /** The exact DATABASE_RULES text this SourceAgent's own prompt got (promptLoader.loadDatabaseRulesForType). */
901
+ databaseRulesText?: string;
902
+ /** The exact ROW_LIMIT_SYNTAX text this SourceAgent's own prompt got. */
903
+ rowLimitSyntaxText?: string;
904
+ /** The exact SCHEMA_SEARCH_INSTRUCTIONS text this SourceAgent's own prompt got — the guidance, not the runtime searchSchemaLookups results above. */
905
+ schemaSearchInstructionsText?: string;
906
+ /** The literal tool definitions (query tool + search_schema tool, when offered) handed to this SourceAgent's own LLM.streamWithTools call. */
907
+ toolsSent?: any[];
908
+ /** This SourceAgent's own LLM.streamWithTools call config. */
909
+ callConfig?: {
910
+ model?: string;
911
+ maxTokens?: number;
912
+ temperature?: number;
913
+ maxIterations?: number;
914
+ };
915
+ /** The exact USER_ACCESS_CONTEXT text this SourceAgent's own prompt got — its own copy of the same value captured once at the turn level in userAccessContext. */
916
+ userAccessContextText?: string;
917
+ }>;
918
+ };
919
+ /**
920
+ * Concept-catalog activity this turn (see main-agent.ts's loadConceptCatalog /
921
+ * loadConceptInvariants / RESOLVE_CONCEPTS_TOOL_DEF). Parallel to
922
+ * `entityResolution` above. Absent/empty for a project with no concepts
923
+ * collection registered.
924
+ */
925
+ concepts: {
926
+ /** The exact {{CONCEPT_CATALOG}} text injected into MainAgent's system prompt (names + aliases only — bodies come back via resolve_concepts). */
927
+ catalogText?: string;
928
+ /** The exact {{CONCEPT_INVARIANTS}} text injected into MainAgent's system prompt — always-on, never requested by the LLM. Distinct from the resolved-concept bodies below, which mix invariants and explicitly-resolved concepts together. */
929
+ invariantsText?: string;
930
+ /** One entry per resolve_concepts-driven resolution this turn — explicit tool calls, resolve_entities-triggered concept lookups, and (for the dashboard flow) its own concept tool. */
931
+ calls: Array<{
932
+ via: 'resolve_concepts' | 'resolve_entities' | 'dashboard-agent';
933
+ asked?: string[];
934
+ implied?: string[];
935
+ returned?: Array<{
936
+ kind: string;
937
+ name: string;
938
+ returns?: any;
939
+ definition: string;
940
+ viaRequires?: boolean;
941
+ }>;
942
+ viaFloor?: string[];
943
+ corrected?: Array<{
944
+ asked: string;
945
+ matched: string;
946
+ }>;
947
+ unmatched?: string[];
948
+ suggestions?: string[];
949
+ error?: string;
950
+ note?: string;
951
+ }>;
952
+ };
953
+ /** The script that produced this answer, if any. Null for a general (no-tool) answer. */
954
+ script: {
955
+ /** True when an existing recipe was replayed unchanged (ScriptMatcher tier='high'). */
956
+ reused: boolean;
957
+ recipeId?: string | null;
958
+ body?: string | null;
959
+ /** Set when reused — the matcher's reasoning for picking this recipe. */
960
+ reasoning?: string | null;
961
+ /** The winning execute_script tool_result text. Only set when reused=false — a replay never re-runs execute_script. Failed attempts aren't kept. */
962
+ executionResult?: string | null;
963
+ /** Failed execute_script tool_result texts this turn, in order (before the winning one, if any). */
964
+ failedAttempts?: string[];
965
+ } | null;
966
+ /**
967
+ * Everything ScriptMatcher.match() built and sent to its one LLM call this
968
+ * turn — Step 2 of the request flow, immediately after Step 1's exact-match
969
+ * check misses. Captured even when short-circuited before any LLM call, or
970
+ * when the call itself errored, so "why didn't this reuse a script" can
971
+ * always be answered from the trace alone, not just the outcome in `script`.
972
+ * See backend/docs/user-prompt-req-flow/SCRIPT-MATCH-IMPROVEMENTS.md.
973
+ */
974
+ scriptMatcher: {
975
+ /**
976
+ * Exactly what went INTO the one LLM call — every placeholder value
977
+ * substituted into `.prompts/script-match/system.md`, plus the routing
978
+ * facts (isExplicitEdit, the two counts) that aren't literal prompt text
979
+ * but explain WHY the catalog/active-script blocks look the way they do.
980
+ */
981
+ sent: {
982
+ /** Whether the raw prompt carried the explicit "edit :" prefix (see parseEditPrefix). */
983
+ isExplicitEdit: boolean;
984
+ /** The prefix-stripped question actually shown to the LLM (the USER_PROMPT placeholder). */
985
+ userPrompt: string;
986
+ /** The Edit Signal text handed to the LLM this turn (CONFIRMED/NOT CONFIRMED wording). */
987
+ editSignal: string;
988
+ /** The rendered "Scripts From Your Past Questions" catalog text (SCRIPT_CATALOG placeholder). */
989
+ scriptCatalog: string;
990
+ /** Distinct scripts actually in scriptCatalog, after health-filtering and the MAX_CATALOG_RECIPES cap. */
991
+ catalogRecipeCount: number;
992
+ /** The rendered "EDITABLE SCRIPTS" block text (ACTIVE_SCRIPT placeholder) — '' when the thread has no active bindings. */
993
+ activeScript: string;
994
+ /** Active bindings offered as edit candidates this turn. */
995
+ activeBindingCount: number;
996
+ /** This call's LLM.stream config. */
997
+ model?: string;
998
+ maxTokens?: number;
999
+ temperature?: number;
1000
+ };
1001
+ /**
1002
+ * Everything that came back OUT — the LLM's raw answer plus what the
1003
+ * runtime decided to do with it. `tier` here is the FINAL tier after
1004
+ * every coercion (near→none, the edit-prefix gate, candidate checks) —
1005
+ * it can legitimately differ from `rawResponse.tier`.
1006
+ */
1007
+ received: {
1008
+ /** False when short-circuited before any LLM call (no catalog match AND no active bindings) — `rawResponse`/`tier` below don't apply. */
1009
+ calledLLM: boolean;
1010
+ /** The raw JSON the LLM returned, when calledLLM is true and the call didn't throw. */
1011
+ rawResponse?: any;
1012
+ /** Set when calledLLM is true and the call threw, or the response named an unknown script id. */
1013
+ error?: string;
1014
+ /** Final tier after ALL coercions. Null when short-circuited, errored, or the call/coercions landed on no match. */
1015
+ tier: MatchTier | null;
1016
+ };
1017
+ } | null;
1018
+ /**
1019
+ * `resolve_entities` tool activity this turn (see main-agent.ts's
1020
+ * RESOLVE_ENTITIES_TOOL_DEF / handleResolveEntities) — names/identifiers the
1021
+ * user mentioned, resolved to database ids before any SQL was written.
1022
+ * Absent/empty entirely for a project with no entity-search collection
1023
+ * registered, or a turn that named nothing.
1024
+ */
1025
+ entityResolution: {
1026
+ /**
1027
+ * The entity-type catalog text injected into MainAgent's system prompt
1028
+ * (loadEntityCatalog's output) — what let the LLM name a concept instead
1029
+ * of guessing the project's vocabulary. Captured once per turn.
1030
+ */
1031
+ catalogText?: string;
1032
+ /**
1033
+ * One entry per `resolve_entities` tool call this turn (a turn can call it
1034
+ * more than once) — the FULL raw response from the entity-search
1035
+ * collection, not just the trimmed {mention,entityType,instanceId,
1036
+ * displayName} MainAgent keeps for itself to build the dispatch block.
1037
+ * `score`/`ambiguous`/`alternatives` only exist here, nowhere else.
1038
+ */
1039
+ calls: Array<{
1040
+ mentions: Array<{
1041
+ text: string;
1042
+ entityType?: string;
1043
+ }>;
1044
+ entityTypes: string[];
1045
+ resolved: Array<{
1046
+ mention: string;
1047
+ entityType: string;
1048
+ instanceId: string;
1049
+ displayName: string;
1050
+ attrs?: Record<string, any>;
1051
+ score?: number;
1052
+ ambiguous?: boolean;
1053
+ alternatives?: Array<{
1054
+ instanceId: string;
1055
+ displayName: string;
1056
+ score: number;
1057
+ }>;
1058
+ }>;
1059
+ unresolved: string[];
1060
+ entityMap: Record<string, any>;
1061
+ unmatchedEntityTypes?: Array<{
1062
+ requested: string;
1063
+ candidates?: string[];
1064
+ }>;
1065
+ /** Set when the collection call itself failed (fails open — see handleResolveEntities). */
1066
+ error?: string;
1067
+ /** Set on an early return (unavailable / nothing named) so the call is still visible in the trace, not indistinguishable from never having run. */
1068
+ note?: string;
1069
+ }>;
1070
+ };
1071
+ /**
1072
+ * The prior-turn conversation context injected into this turn's prompt as
1073
+ * CONVERSATION_HISTORY — exactly what each flow already builds for itself
1074
+ * (chat: thread.getConversationContext(); report/dashboard: their own
1075
+ * equivalents), captured as-is. Every call site sets this unconditionally
1076
+ * (empty string '' when there's no prior context — new thread, or history
1077
+ * trimmed to nothing), so an empty turn is visibly "no history" in the
1078
+ * saved trace rather than a silently missing key. Optional only because
1079
+ * traces saved before this field existed won't have it at all.
1080
+ */
1081
+ conversationHistory?: string;
1082
+ /**
1083
+ * The exact {{USER_ACCESS_CONTEXT}} text injected into MainAgent's system
1084
+ * prompt — the per-user authorization config (sa-api users.config)
1085
+ * rendered by buildUserAccessContext, as-is. Captured once (MainAgent's
1086
+ * prompt is what SourceAgent's identical value is derived from — no need
1087
+ * to duplicate it per source dispatch). Set unconditionally (empty string
1088
+ * '' when there's no config for this user), so "no restrictions" is
1089
+ * visible in the saved trace rather than a silently missing key. Optional
1090
+ * only because traces saved before this field existed won't have it.
1091
+ */
1092
+ userAccessContext?: string;
1093
+ /**
1094
+ * The exact per-user knowledge-base text injected into MainAgent's system
1095
+ * prompt (getUserKnowledgeBase's output, folded into KNOWLEDGE_BASE_CONTEXT
1096
+ * alongside the query-matched half already captured in kbNodes.query).
1097
+ * Captured separately because it's a distinct fetch with no node-level
1098
+ * structure to list, unlike kbNodes.
1099
+ */
1100
+ userKbContext?: string;
1101
+ /** The literal tool definitions (source, direct, workflow, resolve_entities, resolve_concepts, write_script, execute_script, write_analysis, write_components — whichever were actually offered this turn) handed to MainAgent's LLM.streamWithTools call. */
1102
+ toolsSent?: any[];
1103
+ /** The exact {{DIRECT_TOOLS}} text injected into MainAgent's system prompt. */
1104
+ directToolsText?: string;
1105
+ /** The exact {{WORKFLOW_COMPONENTS}} text injected into MainAgent's system prompt. */
1106
+ workflowsText?: string;
1107
+ /** The exact {{SCRIPT_BODY_CONTRACT}} text injected into MainAgent's system prompt (promptLoader.loadScriptBodyContract). */
1108
+ scriptBodyContractText?: string;
1109
+ /**
1110
+ * Full edit-mode context as sent to MainAgent this turn — every EditContext
1111
+ * field, not just the instruction already folded into `script.reasoning`.
1112
+ * Null for a non-edit turn.
1113
+ */
1114
+ editContext?: {
1115
+ recipeId?: string;
1116
+ parentName?: string;
1117
+ parentBody?: string;
1118
+ instruction?: string;
1119
+ lastResultColumns?: string[];
1120
+ parentParams?: string;
1121
+ } | null;
1122
+ /**
1123
+ * The fork-mode preamble as actually assembled and prepended to the user
1124
+ * prompt (agent-user-response.ts) — parentBody/gaps/modificationHint as
1125
+ * sent, plus the lineage fields also written to the script store separately
1126
+ * (parentRecipeId/forkDepth/forkReason). Null for a non-fork turn.
1127
+ */
1128
+ forkContext?: {
1129
+ parentBody?: string;
1130
+ gaps?: string;
1131
+ modificationHint?: string;
1132
+ parentRecipeId?: string;
1133
+ forkDepth?: number;
1134
+ forkReason?: string;
1135
+ } | null;
1136
+ /** The write_analysis tool call's analysisBody argument this turn, when called. */
1137
+ writeAnalysisBody?: string;
1138
+ /** The write_components tool call's componentsBody argument this turn, when called. */
1139
+ writeComponentsBody?: string;
1140
+ /**
1141
+ * Component-generation/re-authoring activity this turn — one entry per call
1142
+ * to `authorComponentsProgram` (script-component-generator.ts — the
1143
+ * display-edit path, reauthoring getComponents as a program),
1144
+ * `generateScriptComponents` (the script-replay fallback, spec/analysis-based
1145
+ * generation), or `summarizeToMarkdown` (its own small analysis-summary LLM
1146
+ * call, reached via `assembleComponentsFromSpecs`). A turn can hit more than
1147
+ * one of these across its fallback chain, so this is an array, not a
1148
+ * singleton — same pattern as `concepts.calls`/`entityResolution.calls`.
1149
+ */
1150
+ componentGen: Array<{
1151
+ via: 'authorComponentsProgram' | 'generateScriptComponents' | 'summarizeToMarkdown';
1152
+ /** authorComponentsProgram only: the edit instruction driving the re-author. */
1153
+ instruction?: string;
1154
+ /** authorComponentsProgram only: the component body being revised, before this attempt. */
1155
+ currentBody?: string;
1156
+ /** Dataset shapes/schemas the LLM was shown (both generation paths, different derivation). */
1157
+ datasetShapes?: string;
1158
+ /** The component catalog text/list handed to the LLM. */
1159
+ componentCatalog?: string;
1160
+ /** generateScriptComponents only: source name(s) shown (SOURCE_NAME). */
1161
+ sourceName?: string;
1162
+ /** generateScriptComponents only: row count(s) shown (ROW_COUNT). */
1163
+ rowCount?: string;
1164
+ /** generateScriptComponents only: column descriptions text shown. */
1165
+ columnDescriptions?: string;
1166
+ /** generateScriptComponents/summarizeToMarkdown: the analysis text handed to the LLM. */
1167
+ analysisText?: string;
1168
+ /** This call's LLM config. */
1169
+ callConfig?: {
1170
+ model?: string;
1171
+ maxTokens?: number;
1172
+ temperature?: number;
1173
+ };
1174
+ /** authorComponentsProgram only: one entry per retry attempt — the feedback fed back in and the raw response for that attempt, in order. */
1175
+ attempts?: Array<{
1176
+ feedback?: string;
1177
+ rawResponse?: string;
1178
+ }>;
1179
+ /** The final raw LLM response text for this call (last attempt, for authorComponentsProgram's retry loop; the single response otherwise). */
1180
+ rawResponse?: string;
1181
+ }>;
1182
+ }
1183
+ /**
1184
+ * Configuration for the multi-agent system.
1185
+ * Controls limits, models, and behavior.
1186
+ */
1187
+ interface AgentConfig {
1188
+ /** Max rows shown to the UI preview / inlined per source (default: 10) */
1189
+ maxRowsPerSource: number;
1190
+ /**
1191
+ * Max rows a source query may FETCH from the DB server-side (default: 2000).
1192
+ * Decoupled from what the main agent is shown: the full result is fetched and
1193
+ * summarized (bounded), but only a small/complete slice enters LLM context.
1194
+ * This lets small lookups (benchmark maps) arrive COMPLETE without letting
1195
+ * large results blow up context.
1196
+ */
1197
+ maxRowsFetched: number;
1198
+ /** Model for the main agent (routing + analysis in one LLM call) */
1199
+ mainAgentModel: string;
1200
+ /** Model for source agent query generation */
1201
+ sourceAgentModel: string;
1202
+ /** API key for LLM calls */
1203
+ apiKey?: string;
1204
+ /** Max retry attempts per source agent */
1205
+ maxRetries: number;
1206
+ /** Max tool calling iterations for the main agent loop */
1207
+ maxIterations: number;
1208
+ /** Global knowledge base context (static, same for all users/questions — cached in system prompt) */
1209
+ globalKnowledgeBase?: string;
1210
+ /** Per-request knowledge base context (user-specific + query-matched — dynamic, not cached) */
1211
+ knowledgeBaseContext?: string;
1212
+ /** Per-user authorization config (sa-api users.config), rendered to text — see buildUserAccessContext */
1213
+ userAccessContext?: string;
1214
+ /** Collections registry (ChromaDB search hooks) for embedding-based schema + source search */
1215
+ collections?: any;
1216
+ /** Optional project ID for scoping embedding searches */
1217
+ projectId?: string;
1218
+ /**
1219
+ * Optional debug-trace accumulator for this turn (Phase 1, chat_agent only).
1220
+ * When present, MainAgent-scope retrieval call sites (e.g. preResolveSchema)
1221
+ * push their structured results into it. Absent for callers that don't care
1222
+ * about trace capture (e.g. headless flows) — every push site must treat
1223
+ * this as optional and never let a capture failure affect the real answer.
1224
+ */
1225
+ trace?: RunTrace;
1226
+ }
1227
+ /**
1228
+ * Default agent configuration
1229
+ */
1230
+ declare const DEFAULT_AGENT_CONFIG: AgentConfig;
1231
+
4
1232
  /**
5
1233
  * Unified UIBlock structure for database storage
6
- * Used in both bookmarks and user-conversations tables
1234
+ * Used in both bookmarks and user-conversations tables.
1235
+ *
1236
+ * `analysis` always holds whatever real narration exists — the full answer on
1237
+ * success, or whatever was actually streamed before a failure (may be empty).
1238
+ * `error` is a dedicated field, null on success — it can be a plain string OR
1239
+ * a structured object/array (e.g. the agent's raw `errors` list), whatever
1240
+ * shape the failure naturally has; it's never coerced into `analysis`.
7
1241
  */
8
1242
  interface DBUIBlock {
9
1243
  id: string;
10
1244
  component: Record<string, any> | null;
11
1245
  analysis: string | null;
12
1246
  user_prompt: string;
1247
+ error?: unknown | null;
1248
+ /**
1249
+ * The script recipe that produced this answer, when one did. Read back via
1250
+ * `conversation-history.exactMatch` so an edit follow-up still has a target
1251
+ * after a reload (the in-memory thread is gone by then), and indexed so
1252
+ * committing an edit can purge every cached answer bound to the recipe.
1253
+ */
1254
+ scriptBinding?: {
1255
+ recipeId: string;
1256
+ params?: Record<string, any>;
1257
+ name?: string;
1258
+ columns?: string[];
1259
+ };
1260
+ /** Progress updates written while this answer was produced, in order. */
1261
+ narration?: NarrationUpdate[];
1262
+ /** How long the answer took, in milliseconds. */
1263
+ durationMs?: number;
13
1264
  }
14
1265
 
15
1266
  /**
@@ -160,6 +1411,7 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
160
1411
  dependencies?: string[] | undefined;
161
1412
  } | undefined;
162
1413
  props?: Record<string, any> | undefined;
1414
+ data?: Record<string, any> | undefined;
163
1415
  render?: any;
164
1416
  states?: Record<string, any> | undefined;
165
1417
  methods?: Record<string, {
@@ -170,7 +1422,6 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
170
1422
  fn: string;
171
1423
  deps?: string[] | undefined;
172
1424
  }[] | undefined;
173
- data?: Record<string, any> | undefined;
174
1425
  pages?: {
175
1426
  id: string;
176
1427
  name: string;
@@ -192,6 +1443,7 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
192
1443
  dependencies?: string[] | undefined;
193
1444
  } | undefined;
194
1445
  props?: Record<string, any> | undefined;
1446
+ data?: Record<string, any> | undefined;
195
1447
  render?: any;
196
1448
  states?: Record<string, any> | undefined;
197
1449
  methods?: Record<string, {
@@ -202,7 +1454,6 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
202
1454
  fn: string;
203
1455
  deps?: string[] | undefined;
204
1456
  }[] | undefined;
205
- data?: Record<string, any> | undefined;
206
1457
  pages?: {
207
1458
  id: string;
208
1459
  name: string;
@@ -228,6 +1479,7 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
228
1479
  dependencies?: string[] | undefined;
229
1480
  } | undefined;
230
1481
  props?: Record<string, any> | undefined;
1482
+ data?: Record<string, any> | undefined;
231
1483
  render?: any;
232
1484
  states?: Record<string, any> | undefined;
233
1485
  methods?: Record<string, {
@@ -238,7 +1490,6 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
238
1490
  fn: string;
239
1491
  deps?: string[] | undefined;
240
1492
  }[] | undefined;
241
- data?: Record<string, any> | undefined;
242
1493
  pages?: {
243
1494
  id: string;
244
1495
  name: string;
@@ -264,6 +1515,7 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
264
1515
  dependencies?: string[] | undefined;
265
1516
  } | undefined;
266
1517
  props?: Record<string, any> | undefined;
1518
+ data?: Record<string, any> | undefined;
267
1519
  render?: any;
268
1520
  states?: Record<string, any> | undefined;
269
1521
  methods?: Record<string, {
@@ -274,7 +1526,6 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
274
1526
  fn: string;
275
1527
  deps?: string[] | undefined;
276
1528
  }[] | undefined;
277
- data?: Record<string, any> | undefined;
278
1529
  pages?: {
279
1530
  id: string;
280
1531
  name: string;
@@ -355,6 +1606,7 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
355
1606
  dependencies?: string[] | undefined;
356
1607
  } | undefined;
357
1608
  props?: Record<string, any> | undefined;
1609
+ data?: Record<string, any> | undefined;
358
1610
  render?: any;
359
1611
  states?: Record<string, any> | undefined;
360
1612
  methods?: Record<string, {
@@ -365,7 +1617,6 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
365
1617
  fn: string;
366
1618
  deps?: string[] | undefined;
367
1619
  }[] | undefined;
368
- data?: Record<string, any> | undefined;
369
1620
  }, {
370
1621
  id: string;
371
1622
  name?: string | undefined;
@@ -379,6 +1630,7 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
379
1630
  dependencies?: string[] | undefined;
380
1631
  } | undefined;
381
1632
  props?: Record<string, any> | undefined;
1633
+ data?: Record<string, any> | undefined;
382
1634
  render?: any;
383
1635
  states?: Record<string, any> | undefined;
384
1636
  methods?: Record<string, {
@@ -389,7 +1641,6 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
389
1641
  fn: string;
390
1642
  deps?: string[] | undefined;
391
1643
  }[] | undefined;
392
- data?: Record<string, any> | undefined;
393
1644
  }>;
394
1645
  data: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
395
1646
  context: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
@@ -407,6 +1658,7 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
407
1658
  dependencies?: string[] | undefined;
408
1659
  } | undefined;
409
1660
  props?: Record<string, any> | undefined;
1661
+ data?: Record<string, any> | undefined;
410
1662
  render?: any;
411
1663
  states?: Record<string, any> | undefined;
412
1664
  methods?: Record<string, {
@@ -417,7 +1669,6 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
417
1669
  fn: string;
418
1670
  deps?: string[] | undefined;
419
1671
  }[] | undefined;
420
- data?: Record<string, any> | undefined;
421
1672
  };
422
1673
  data?: Record<string, any> | undefined;
423
1674
  context?: Record<string, any> | undefined;
@@ -435,6 +1686,7 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
435
1686
  dependencies?: string[] | undefined;
436
1687
  } | undefined;
437
1688
  props?: Record<string, any> | undefined;
1689
+ data?: Record<string, any> | undefined;
438
1690
  render?: any;
439
1691
  states?: Record<string, any> | undefined;
440
1692
  methods?: Record<string, {
@@ -445,7 +1697,6 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
445
1697
  fn: string;
446
1698
  deps?: string[] | undefined;
447
1699
  }[] | undefined;
448
- data?: Record<string, any> | undefined;
449
1700
  };
450
1701
  data?: Record<string, any> | undefined;
451
1702
  context?: Record<string, any> | undefined;
@@ -634,23 +1885,20 @@ declare const ComponentSchema: z.ZodObject<{
634
1885
  type: z.ZodString;
635
1886
  description: z.ZodString;
636
1887
  props: z.ZodObject<{
637
- query: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>]>>;
1888
+ query: z.ZodOptional<z.ZodNullable<z.ZodUnion<[z.ZodString, z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>]>>>;
638
1889
  title: z.ZodOptional<z.ZodString>;
639
1890
  description: z.ZodOptional<z.ZodString>;
640
1891
  config: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
641
- actions: z.ZodOptional<z.ZodArray<z.ZodAny, "many">>;
642
1892
  }, "passthrough", z.ZodTypeAny, z.objectOutputType<{
643
- query: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>]>>;
1893
+ query: z.ZodOptional<z.ZodNullable<z.ZodUnion<[z.ZodString, z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>]>>>;
644
1894
  title: z.ZodOptional<z.ZodString>;
645
1895
  description: z.ZodOptional<z.ZodString>;
646
1896
  config: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
647
- actions: z.ZodOptional<z.ZodArray<z.ZodAny, "many">>;
648
1897
  }, z.ZodTypeAny, "passthrough">, z.objectInputType<{
649
- query: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>]>>;
1898
+ query: z.ZodOptional<z.ZodNullable<z.ZodUnion<[z.ZodString, z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>]>>>;
650
1899
  title: z.ZodOptional<z.ZodString>;
651
1900
  description: z.ZodOptional<z.ZodString>;
652
1901
  config: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
653
- actions: z.ZodOptional<z.ZodArray<z.ZodAny, "many">>;
654
1902
  }, z.ZodTypeAny, "passthrough">>;
655
1903
  category: z.ZodOptional<z.ZodString>;
656
1904
  keywords: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
@@ -661,10 +1909,9 @@ declare const ComponentSchema: z.ZodObject<{
661
1909
  description: string;
662
1910
  props: {
663
1911
  description?: string | undefined;
664
- query?: string | {} | undefined;
1912
+ query?: string | {} | null | undefined;
665
1913
  title?: string | undefined;
666
1914
  config?: Record<string, unknown> | undefined;
667
- actions?: any[] | undefined;
668
1915
  } & {
669
1916
  [k: string]: unknown;
670
1917
  };
@@ -679,10 +1926,9 @@ declare const ComponentSchema: z.ZodObject<{
679
1926
  description: string;
680
1927
  props: {
681
1928
  description?: string | undefined;
682
- query?: string | {} | undefined;
1929
+ query?: string | {} | null | undefined;
683
1930
  title?: string | undefined;
684
1931
  config?: Record<string, unknown> | undefined;
685
- actions?: any[] | undefined;
686
1932
  } & {
687
1933
  [k: string]: unknown;
688
1934
  };
@@ -741,6 +1987,10 @@ declare const ToolSchema: z.ZodObject<{
741
1987
  id: z.ZodString;
742
1988
  name: z.ZodString;
743
1989
  description: z.ZodString;
1990
+ /** Tool type: "source" = routed through SourceAgent, "direct" = called directly by MainAgent */
1991
+ toolType: z.ZodOptional<z.ZodEnum<["source", "direct"]>>;
1992
+ /** Full untruncated schema for source agent (all columns visible) */
1993
+ fullSchema: z.ZodOptional<z.ZodString>;
744
1994
  params: z.ZodRecord<z.ZodString, z.ZodString>;
745
1995
  fn: z.ZodFunction<z.ZodTuple<[z.ZodAny], z.ZodUnknown>, z.ZodAny>;
746
1996
  outputSchema: z.ZodOptional<z.ZodObject<{
@@ -773,12 +2023,22 @@ declare const ToolSchema: z.ZodObject<{
773
2023
  description: string;
774
2024
  }[];
775
2025
  }>>;
2026
+ /** Cache policy. `false` = never cache (live data, write ops). Mirrors HTTP `Cache-Control: no-store`. */
2027
+ cache: z.ZodOptional<z.ZodUnion<[z.ZodLiteral<false>, z.ZodObject<{
2028
+ ttlMs: z.ZodOptional<z.ZodNumber>;
2029
+ }, "strip", z.ZodTypeAny, {
2030
+ ttlMs?: number | undefined;
2031
+ }, {
2032
+ ttlMs?: number | undefined;
2033
+ }>]>>;
776
2034
  }, "strip", z.ZodTypeAny, {
777
2035
  id: string;
778
2036
  params: Record<string, string>;
779
2037
  name: string;
780
2038
  description: string;
781
2039
  fn: (args_0: any, ...args: unknown[]) => any;
2040
+ toolType?: "source" | "direct" | undefined;
2041
+ fullSchema?: string | undefined;
782
2042
  outputSchema?: {
783
2043
  description: string;
784
2044
  fields: {
@@ -787,12 +2047,17 @@ declare const ToolSchema: z.ZodObject<{
787
2047
  description: string;
788
2048
  }[];
789
2049
  } | undefined;
2050
+ cache?: false | {
2051
+ ttlMs?: number | undefined;
2052
+ } | undefined;
790
2053
  }, {
791
2054
  id: string;
792
2055
  params: Record<string, string>;
793
2056
  name: string;
794
2057
  description: string;
795
2058
  fn: (args_0: any, ...args: unknown[]) => any;
2059
+ toolType?: "source" | "direct" | undefined;
2060
+ fullSchema?: string | undefined;
796
2061
  outputSchema?: {
797
2062
  description: string;
798
2063
  fields: {
@@ -801,6 +2066,9 @@ declare const ToolSchema: z.ZodObject<{
801
2066
  description: string;
802
2067
  }[];
803
2068
  } | undefined;
2069
+ cache?: false | {
2070
+ ttlMs?: number | undefined;
2071
+ } | undefined;
804
2072
  }>;
805
2073
  type Tool$1 = z.infer<typeof ToolSchema>;
806
2074
  type CollectionOperation = 'getMany' | 'getOne' | 'query' | 'mutation' | 'updateOne' | 'deleteOne' | 'createOne';
@@ -831,6 +2099,69 @@ interface DashCompModelConfig {
831
2099
  */
832
2100
  fastModel?: string;
833
2101
  }
2102
+ /**
2103
+ * Everything a replacement answer flow is handed for one question. Deliberately
2104
+ * the same material the built-in flow gets, so a provider is a drop-in and not a
2105
+ * second-class path.
2106
+ */
2107
+ interface AnswerProviderContext {
2108
+ prompt: string;
2109
+ /** The live frontend component catalog. */
2110
+ components: Component[];
2111
+ collections?: any;
2112
+ externalTools?: any[];
2113
+ userId?: string;
2114
+ threadId?: string;
2115
+ /** Per-user authorization config (users.config) — row policies come from here. */
2116
+ userConfig?: any;
2117
+ conversationHistory?: string;
2118
+ /** Stream user-facing prose. Do NOT stream tool/command output through this. */
2119
+ onStream?: (chunk: string) => void;
2120
+ /**
2121
+ * Send one narrator update to the client live, as its OWN message
2122
+ * (USER_PROMPT_NARRATION) — never into the answer stream, so it is not part of
2123
+ * the saved answer text. Every response mode. The saved copy travels
2124
+ * separately, as `data.narration`.
2125
+ */
2126
+ onNarration?: (update: {
2127
+ elapsedMs: number;
2128
+ text: string;
2129
+ }) => void;
2130
+ signal?: AbortSignal;
2131
+ }
2132
+ /**
2133
+ * One narrator tick while an answer was being produced: what the narrator was
2134
+ * given, and what it wrote. Returned as `data.narration` (an array, in order)
2135
+ * and saved with the answer as `response.narration`, unchanged. A UI shows the
2136
+ * `shown` entries; the rest are kept for judging the narrator against its input.
2137
+ */
2138
+ interface NarrationUpdate {
2139
+ /** Milliseconds since the question arrived. */
2140
+ elapsedMs: number;
2141
+ /** shown | skipped | dropped (refused by an output filter) | failed. */
2142
+ status?: 'shown' | 'skipped' | 'dropped' | 'failed';
2143
+ /** The update itself; null when nothing was shown. */
2144
+ text: string | null;
2145
+ /** Why an update was dropped or failed. */
2146
+ reason?: string;
2147
+ /**
2148
+ * What the narrator was given for this tick — the agent's notes, commands
2149
+ * with their output, queries with SQL and sample rows. Shape is the
2150
+ * deployment's; the SDK stores it as-is.
2151
+ */
2152
+ activity?: unknown[];
2153
+ }
2154
+ /**
2155
+ * An alternative to the built-in MainAgent/SourceAgent flow.
2156
+ *
2157
+ * Returns the same T_RESPONSE the built-in flow does, so everything downstream —
2158
+ * the UIBlock, streaming, abort, the conversation save, component rendering — is
2159
+ * unchanged and the two flows stay comparable.
2160
+ *
2161
+ * `data.narration` (NarrationUpdate[]) is optional and saved with the answer on
2162
+ * every outcome — success, error and abort — so return it on each of them.
2163
+ */
2164
+ type AnswerProvider = (ctx: AnswerProviderContext) => Promise<T_RESPONSE>;
834
2165
  interface SuperatomSDKConfig {
835
2166
  url?: string;
836
2167
  apiKey?: string;
@@ -839,6 +2170,10 @@ interface SuperatomSDKConfig {
839
2170
  bundleDir?: string;
840
2171
  promptsDir?: string;
841
2172
  databaseType?: DatabaseType;
2173
+ /** BCP-47 locale for `fmt` in generated analyses, e.g. 'en-IN', 'en-US'. Defaults to 'en-IN'. */
2174
+ locale?: string;
2175
+ /** ISO-4217 currency for `fmt`, e.g. 'INR', 'USD'. Defaults to 'INR'. */
2176
+ currency?: string;
842
2177
  ANTHROPIC_API_KEY?: string;
843
2178
  GROQ_API_KEY?: string;
844
2179
  GEMINI_API_KEY?: string;
@@ -852,6 +2187,28 @@ interface SuperatomSDKConfig {
852
2187
  * - 'balanced': Use best model for complex tasks, fast model for simple tasks (default)
853
2188
  */
854
2189
  modelStrategy?: ModelStrategy;
2190
+ /**
2191
+ * Model for the main agent (routing + analysis).
2192
+ * Format: "provider/model-name" (e.g., "anthropic/claude-haiku-4-5-20251001")
2193
+ * If not set, uses the provider's default model.
2194
+ */
2195
+ mainAgentModel?: string;
2196
+ /**
2197
+ * Model for source agents (per-source query generation).
2198
+ * Format: "provider/model-name" (e.g., "anthropic/claude-haiku-4-5-20251001")
2199
+ * If not set, uses the provider's default model.
2200
+ */
2201
+ sourceAgentModel?: string;
2202
+ /**
2203
+ * Which flow answers a user question.
2204
+ * 'agents' — the built-in MainAgent + SourceAgent flow (default).
2205
+ * 'custom' — call `answerProvider` instead.
2206
+ * Set 'custom' WITHOUT a provider and the SDK warns and uses 'agents', rather
2207
+ * than failing every question.
2208
+ */
2209
+ answerFlow?: 'agents' | 'custom';
2210
+ /** Required when answerFlow is 'custom'. See AnswerProvider. */
2211
+ answerProvider?: AnswerProvider;
855
2212
  /**
856
2213
  * Separate model configuration for DASH_COMP flow (dashboard component picking)
857
2214
  * If not provided, falls back to provider-based model selection
@@ -870,6 +2227,12 @@ interface SuperatomSDKConfig {
870
2227
  * Default: 5 minutes
871
2228
  */
872
2229
  queryCacheTTL?: number;
2230
+ /**
2231
+ * Dashboard conversation history TTL (Time To Live) in minutes
2232
+ * Per-dashboard conversation histories expire after this duration
2233
+ * Default: 30 minutes
2234
+ */
2235
+ dashboardHistoryTTL?: number;
873
2236
  }
874
2237
 
875
2238
  declare const KbNodesQueryFiltersSchema: z.ZodObject<{
@@ -877,18 +2240,18 @@ declare const KbNodesQueryFiltersSchema: z.ZodObject<{
877
2240
  category: z.ZodOptional<z.ZodString>;
878
2241
  tags: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
879
2242
  type: z.ZodOptional<z.ZodEnum<["global", "user", "query"]>>;
880
- createdBy: z.ZodOptional<z.ZodNumber>;
2243
+ createdBy: z.ZodOptional<z.ZodString>;
881
2244
  }, "strip", z.ZodTypeAny, {
882
2245
  type?: "query" | "user" | "global" | undefined;
883
2246
  query?: string | undefined;
884
2247
  category?: string | undefined;
885
- createdBy?: number | undefined;
2248
+ createdBy?: string | undefined;
886
2249
  tags?: string[] | undefined;
887
2250
  }, {
888
2251
  type?: "query" | "user" | "global" | undefined;
889
2252
  query?: string | undefined;
890
2253
  category?: string | undefined;
891
- createdBy?: number | undefined;
2254
+ createdBy?: string | undefined;
892
2255
  tags?: string[] | undefined;
893
2256
  }>;
894
2257
  type KbNodesQueryFilters = z.infer<typeof KbNodesQueryFiltersSchema>;
@@ -901,27 +2264,27 @@ declare const KbNodesRequestPayloadSchema: z.ZodObject<{
901
2264
  category: z.ZodOptional<z.ZodString>;
902
2265
  tags: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
903
2266
  type: z.ZodOptional<z.ZodEnum<["global", "user", "query"]>>;
904
- createdBy: z.ZodOptional<z.ZodNumber>;
905
- updatedBy: z.ZodOptional<z.ZodNumber>;
906
- userId: z.ZodOptional<z.ZodNumber>;
2267
+ createdBy: z.ZodOptional<z.ZodString>;
2268
+ updatedBy: z.ZodOptional<z.ZodString>;
2269
+ userId: z.ZodOptional<z.ZodString>;
907
2270
  query: z.ZodOptional<z.ZodString>;
908
2271
  filters: z.ZodOptional<z.ZodObject<{
909
2272
  query: z.ZodOptional<z.ZodString>;
910
2273
  category: z.ZodOptional<z.ZodString>;
911
2274
  tags: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
912
2275
  type: z.ZodOptional<z.ZodEnum<["global", "user", "query"]>>;
913
- createdBy: z.ZodOptional<z.ZodNumber>;
2276
+ createdBy: z.ZodOptional<z.ZodString>;
914
2277
  }, "strip", z.ZodTypeAny, {
915
2278
  type?: "query" | "user" | "global" | undefined;
916
2279
  query?: string | undefined;
917
2280
  category?: string | undefined;
918
- createdBy?: number | undefined;
2281
+ createdBy?: string | undefined;
919
2282
  tags?: string[] | undefined;
920
2283
  }, {
921
2284
  type?: "query" | "user" | "global" | undefined;
922
2285
  query?: string | undefined;
923
2286
  category?: string | undefined;
924
- createdBy?: number | undefined;
2287
+ createdBy?: string | undefined;
925
2288
  tags?: string[] | undefined;
926
2289
  }>>;
927
2290
  limit: z.ZodOptional<z.ZodNumber>;
@@ -932,17 +2295,17 @@ declare const KbNodesRequestPayloadSchema: z.ZodObject<{
932
2295
  query?: string | undefined;
933
2296
  title?: string | undefined;
934
2297
  category?: string | undefined;
935
- userId?: number | undefined;
2298
+ userId?: string | undefined;
936
2299
  limit?: number | undefined;
937
2300
  filters?: {
938
2301
  type?: "query" | "user" | "global" | undefined;
939
2302
  query?: string | undefined;
940
2303
  category?: string | undefined;
941
- createdBy?: number | undefined;
2304
+ createdBy?: string | undefined;
942
2305
  tags?: string[] | undefined;
943
2306
  } | undefined;
944
- createdBy?: number | undefined;
945
- updatedBy?: number | undefined;
2307
+ createdBy?: string | undefined;
2308
+ updatedBy?: string | undefined;
946
2309
  offset?: number | undefined;
947
2310
  tags?: string[] | undefined;
948
2311
  content?: string | undefined;
@@ -952,63 +2315,63 @@ declare const KbNodesRequestPayloadSchema: z.ZodObject<{
952
2315
  query?: string | undefined;
953
2316
  title?: string | undefined;
954
2317
  category?: string | undefined;
955
- userId?: number | undefined;
2318
+ userId?: string | undefined;
956
2319
  limit?: number | undefined;
957
2320
  filters?: {
958
2321
  type?: "query" | "user" | "global" | undefined;
959
2322
  query?: string | undefined;
960
2323
  category?: string | undefined;
961
- createdBy?: number | undefined;
2324
+ createdBy?: string | undefined;
962
2325
  tags?: string[] | undefined;
963
2326
  } | undefined;
964
- createdBy?: number | undefined;
965
- updatedBy?: number | undefined;
2327
+ createdBy?: string | undefined;
2328
+ updatedBy?: string | undefined;
966
2329
  offset?: number | undefined;
967
2330
  tags?: string[] | undefined;
968
2331
  content?: string | undefined;
969
2332
  }>>;
970
2333
  }, "strip", z.ZodTypeAny, {
971
- operation: "create" | "getOne" | "update" | "delete" | "getAll" | "search" | "getByCategory" | "getByUser" | "getCategories" | "getTags";
2334
+ operation: "create" | "getOne" | "search" | "update" | "delete" | "getAll" | "getByCategory" | "getByUser" | "getCategories" | "getTags";
972
2335
  data?: {
973
2336
  id?: number | undefined;
974
2337
  type?: "query" | "user" | "global" | undefined;
975
2338
  query?: string | undefined;
976
2339
  title?: string | undefined;
977
2340
  category?: string | undefined;
978
- userId?: number | undefined;
2341
+ userId?: string | undefined;
979
2342
  limit?: number | undefined;
980
2343
  filters?: {
981
2344
  type?: "query" | "user" | "global" | undefined;
982
2345
  query?: string | undefined;
983
2346
  category?: string | undefined;
984
- createdBy?: number | undefined;
2347
+ createdBy?: string | undefined;
985
2348
  tags?: string[] | undefined;
986
2349
  } | undefined;
987
- createdBy?: number | undefined;
988
- updatedBy?: number | undefined;
2350
+ createdBy?: string | undefined;
2351
+ updatedBy?: string | undefined;
989
2352
  offset?: number | undefined;
990
2353
  tags?: string[] | undefined;
991
2354
  content?: string | undefined;
992
2355
  } | undefined;
993
2356
  }, {
994
- operation: "create" | "getOne" | "update" | "delete" | "getAll" | "search" | "getByCategory" | "getByUser" | "getCategories" | "getTags";
2357
+ operation: "create" | "getOne" | "search" | "update" | "delete" | "getAll" | "getByCategory" | "getByUser" | "getCategories" | "getTags";
995
2358
  data?: {
996
2359
  id?: number | undefined;
997
2360
  type?: "query" | "user" | "global" | undefined;
998
2361
  query?: string | undefined;
999
2362
  title?: string | undefined;
1000
2363
  category?: string | undefined;
1001
- userId?: number | undefined;
2364
+ userId?: string | undefined;
1002
2365
  limit?: number | undefined;
1003
2366
  filters?: {
1004
2367
  type?: "query" | "user" | "global" | undefined;
1005
2368
  query?: string | undefined;
1006
2369
  category?: string | undefined;
1007
- createdBy?: number | undefined;
2370
+ createdBy?: string | undefined;
1008
2371
  tags?: string[] | undefined;
1009
2372
  } | undefined;
1010
- createdBy?: number | undefined;
1011
- updatedBy?: number | undefined;
2373
+ createdBy?: string | undefined;
2374
+ updatedBy?: string | undefined;
1012
2375
  offset?: number | undefined;
1013
2376
  tags?: string[] | undefined;
1014
2377
  content?: string | undefined;
@@ -1280,19 +2643,769 @@ declare class ReportManager {
1280
2643
  getReportCount(): number;
1281
2644
  }
1282
2645
 
1283
- type SystemPrompt = string | Anthropic.Messages.TextBlockParam[];
1284
- interface LLMMessages {
1285
- sys: SystemPrompt;
1286
- user: string;
1287
- prefill?: string;
2646
+ /**
2647
+ * ScriptRecipeStore — injected metadata backend for the script flow.
2648
+ *
2649
+ * The SDK is standalone (no DB dependency). The backend implements this
2650
+ * interface over Postgres (full-text search + atomic counters) and injects it
2651
+ * via `collections['script-recipes']`, exactly like `collections['source-embeddings']`.
2652
+ * `ScriptStore` consumes it for all METADATA operations while keeping the
2653
+ * executable body on disk as scripts-store/<fileBase>.ts.
2654
+ *
2655
+ * All metadata rows are plain JSON (no scriptBody — that lives on disk).
2656
+ * See backend/docs/SCRIPT-FLOW-SCALING-ISSUES.md (#1, #3, #7).
2657
+ */
2658
+
2659
+ /** One recipe's metadata as stored in Postgres (mirrors the script_recipes table). */
2660
+ interface ScriptRecipeMetaRow {
2661
+ id: string;
2662
+ projectId?: string | null;
2663
+ version: number;
2664
+ name: string;
2665
+ intentDescription: string;
2666
+ tags: string[] | null;
2667
+ createdFrom: string | null;
2668
+ sourceIds: string[] | null;
2669
+ tables: string[] | null;
2670
+ parameters: ScriptParameter[] | null;
2671
+ components?: ScriptComponentSpec[] | null;
2672
+ displayPreference?: ScriptDisplayPreference | null;
2673
+ fileBase: string;
2674
+ bodyHash?: string | null;
2675
+ successCount: number;
2676
+ failureCount: number;
2677
+ lastUsed: string | null;
2678
+ parentId?: string | null;
2679
+ forkDepth?: number | null;
2680
+ forkReason?: string | null;
2681
+ status: 'draft' | 'verified' | string;
2682
+ turnId?: string | null;
2683
+ editedBy?: string | null;
2684
+ editedFrom?: string | null;
2685
+ history?: ScriptVersionRecord[] | null;
2686
+ lastError?: {
2687
+ phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
2688
+ message: string;
2689
+ at: string;
2690
+ attempt: number;
2691
+ } | null;
2692
+ createdAt?: string | null;
2693
+ updatedAt?: string | null;
1288
2694
  }
1289
- interface LLMOptions {
1290
- model?: string;
1291
- maxTokens?: number;
1292
- temperature?: number;
1293
- topP?: number;
1294
- apiKey?: string;
2695
+ interface ScriptRecipeStore {
2696
+ /** FTS shortlist of healthy verified recipes for the matcher (metadata only). */
2697
+ search(params: {
2698
+ prompt: string;
2699
+ projectId?: string;
2700
+ limit?: number;
2701
+ }): Promise<ScriptRecipeMetaRow[]>;
2702
+ /** Fetch one recipe by id (any status). */
2703
+ getById(id: string): Promise<ScriptRecipeMetaRow | null>;
2704
+ /** Count healthy verified recipes (drives the "any scripts?" gate). */
2705
+ count(params?: {
2706
+ projectId?: string;
2707
+ }): Promise<number>;
2708
+ /** Insert or update a recipe row (keyed by id). */
2709
+ upsert(row: ScriptRecipeMetaRow): Promise<void>;
2710
+ /** Atomically bump counters / last-used. */
2711
+ updateStats(id: string, patch: {
2712
+ successDelta?: number;
2713
+ failureDelta?: number;
2714
+ lastUsed?: string;
2715
+ }): Promise<void>;
2716
+ /** Flip a draft to verified, applying provenance + optional fork lineage. */
2717
+ promote(id: string, patch: {
2718
+ sourceIds: string[];
2719
+ tables: string[];
2720
+ fileBase?: string;
2721
+ parentId?: string;
2722
+ forkDepth?: number;
2723
+ forkReason?: string;
2724
+ components?: ScriptComponentSpec[];
2725
+ }): Promise<ScriptRecipeMetaRow | null>;
2726
+ /**
2727
+ * Commit a verified edit onto an EXISTING recipe: bump `version`, replace the
2728
+ * body-bearing metadata, append a history record, reset health counters, and
2729
+ * clear the component specs (they were validated against the old shape).
2730
+ * Returns the updated row, or null when the target is gone.
2731
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 5.
2732
+ */
2733
+ commitEdit?(id: string, patch: {
2734
+ name?: string;
2735
+ intentDescription?: string;
2736
+ tags?: string[];
2737
+ parameters?: ScriptParameter[];
2738
+ bodyHash: string;
2739
+ /** New on-disk stem when the edit renamed the script; omitted otherwise. */
2740
+ fileBase?: string;
2741
+ sourceIds?: string[];
2742
+ tables?: string[];
2743
+ editedBy?: string;
2744
+ editedFrom?: string;
2745
+ historyEntry: {
2746
+ version: number;
2747
+ at: string;
2748
+ by?: string;
2749
+ instruction?: string;
2750
+ changeSummary?: string;
2751
+ bodyHash?: string;
2752
+ };
2753
+ }): Promise<ScriptRecipeMetaRow | null>;
2754
+ /** Stamp a draft's last execution error. */
2755
+ recordDraftError(id: string, err: {
2756
+ phase: string;
2757
+ message: string;
2758
+ attempt: number;
2759
+ at: string;
2760
+ }): Promise<void>;
2761
+ /** Delete a recipe row (body file removed separately). */
2762
+ remove(id: string): Promise<void>;
2763
+ /** True if `fileBase` is taken by a different recipe in this project. */
2764
+ fileBaseTaken(fileBase: string, excludeId: string, projectId?: string): Promise<boolean>;
2765
+ }
2766
+ /** Pull the injected store off the collections bag (or null if not wired). */
2767
+ declare function resolveScriptRecipeStore(collections: any): ScriptRecipeStore | null;
2768
+
2769
+ /**
2770
+ * ScriptStore — Postgres metadata + on-disk body for script recipes.
2771
+ *
2772
+ * Split of responsibilities:
2773
+ * - METADATA → injected `ScriptRecipeStore` (Postgres FTS + atomic counters),
2774
+ * resolved from `collections['script-recipes']`.
2775
+ * - BODY → scripts-store/<fileBase>.ts, editable in your IDE. Written
2776
+ * atomically (temp + rename); `bodyHash` (sha256) detects edits.
2777
+ *
2778
+ * The old "read every file every turn + send the whole catalog to the LLM"
2779
+ * matcher is gone — matching is `store.search(prompt)` (FTS shortlist). The
2780
+ * draft/verified filename dance is gone too: `status` is a DB column and the
2781
+ * file keeps a stable `<fileBase>.ts` name across promotion.
2782
+ *
2783
+ * When no metadata store is injected, the store degrades to a safe no-op
2784
+ * (count 0 → script flow disabled) instead of crashing.
2785
+ *
2786
+ * See backend/docs/SCRIPT-FLOW-SCALING-ISSUES.md.
2787
+ */
2788
+
2789
+ interface SaveDraftInput {
2790
+ /** Reuse an existing draft (retry); omit to mint a new one. */
2791
+ recipeId?: string;
2792
+ /** Per-turn unique suffix, stable across retries within the turn. */
2793
+ turnId: string;
2794
+ name: string;
2795
+ intentDescription: string;
2796
+ tags: string[];
2797
+ parameters: ScriptParameter[];
2798
+ scriptBody: string;
2799
+ createdFrom: string;
2800
+ /**
2801
+ * Set when this draft is a SHADOW of an existing recipe being edited. The
2802
+ * draft is verified independently and merged back onto the parent via
2803
+ * `commitEdit`, so the working script is never clobbered by an edit that
2804
+ * turns out not to run. See backend/docs/SCRIPT-EDIT-DESIGN.md § D5.
2805
+ */
2806
+ parentId?: string;
2807
+ }
2808
+ interface PromoteToVerifiedInput {
2809
+ sourceIds: string[];
2810
+ tables: string[];
2811
+ parentId?: string;
2812
+ forkDepth?: number;
2813
+ forkReason?: string;
2814
+ components?: ScriptComponentSpec[];
2815
+ }
2816
+ interface ScriptStoreOptions {
2817
+ /** Explicit metadata store, or resolved from `collections['script-recipes']`. */
2818
+ store?: ScriptRecipeStore | null;
2819
+ collections?: any;
2820
+ /** Body directory (defaults to <cwd>/scripts-store). */
2821
+ baseDir?: string;
2822
+ /** Project scope stamped on every row. */
2823
+ projectId?: string;
2824
+ }
2825
+ /**
2826
+ * Fingerprint of everything that decides what `getData` PRODUCES.
2827
+ *
2828
+ * A recipe holds three programs in one file — getData, getAnalysis,
2829
+ * getComponents — but only getData costs a source query, and only getData's
2830
+ * output can invalidate a verified run. Editing the narrative or a chart title
2831
+ * cannot change a single row, yet a whole-body comparison says it did, because
2832
+ * `write_script` resubmits the entire file. That is what made a cosmetic edit
2833
+ * indistinguishable from a SQL change and cost a scarce execute attempt.
2834
+ *
2835
+ * Parameters are part of the fingerprint, not an afterthought: `execute_script`
2836
+ * runs with `{}`, so the DEFAULTS are the values getData actually sees. Same
2837
+ * getData text with a changed default is a genuinely different result, and
2838
+ * hashing the body alone would have called it unchanged.
2839
+ *
2840
+ * Deliberately conservative — trimmed, not whitespace-collapsed. The two ways
2841
+ * to be wrong are NOT symmetric: a false "unchanged" reuses a stale result and
2842
+ * reports wrong numbers, while a false "changed" merely costs an attempt. So it
2843
+ * biases toward detecting change, and a reformat is allowed to look like an edit.
2844
+ */
2845
+ declare function dataFingerprint(scriptBody: string, parameters?: ScriptParameter[]): string;
2846
+ /** Inverse of `stripNamedExport` — pulls the named export block OUT instead of removing it. Null when absent. */
2847
+ declare function extractNamedExport(body: string, fnName: string): string | null;
2848
+ declare function normalizeScriptBody(scriptBody: string): string;
2849
+ declare class ScriptStore {
2850
+ private store;
2851
+ private storeDir;
2852
+ private projectId?;
2853
+ constructor(opts?: ScriptStoreOptions);
2854
+ /** Whether a metadata store is wired (matcher / authoring are gated on this). */
2855
+ hasStore(): boolean;
2856
+ /** Number of healthy verified recipes (gates the script-matching path). */
2857
+ count(): Promise<number>;
2858
+ /**
2859
+ * FTS shortlist for the matcher (metadata only — bodies are loaded lazily by
2860
+ * `get()` once the LLM picks one). Returns verified, healthy recipes ranked
2861
+ * by relevance.
2862
+ */
2863
+ search(prompt: string, limit?: number): Promise<ScriptRecipe[]>;
2864
+ /** Fetch one recipe by id with its body loaded from disk. */
2865
+ get(id: string): Promise<ScriptRecipe | null>;
2866
+ /**
2867
+ * Fetch one recipe by id, pinned to a SPECIFIC past version rather than
2868
+ * whatever is live today. Needed to render a past turn's conversation
2869
+ * history accurately after the script has since been edited — get() alone
2870
+ * always returns the CURRENT body, which may no longer be what that turn
2871
+ * actually ran.
2872
+ *
2873
+ * Returns null (never a silently-wrong version) when the requested version
2874
+ * isn't the current one and its archived body can't be found — e.g. the
2875
+ * archive was cleaned up after the whole recipe was later deleted.
2876
+ */
2877
+ getVersion(id: string, version: number): Promise<ScriptRecipe | null>;
2878
+ /** Create or update a recipe (metadata upsert + body write when changed). */
2879
+ save(recipe: ScriptRecipe): Promise<void>;
2880
+ /**
2881
+ * Persist (or update) a draft. Within a turn, retries that pass the same
2882
+ * `recipeId` overwrite the same row + file; a fresh `recipeId` mints a new
2883
+ * draft. The body is visible at scripts-store/<fileBase>.ts immediately.
2884
+ */
2885
+ saveDraft(input: SaveDraftInput): Promise<ScriptRecipe>;
2886
+ /** Stamp a draft's last execution error (metadata only). */
2887
+ recordDraftError(recipeId: string, err: {
2888
+ phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
2889
+ message: string;
2890
+ attempt: number;
2891
+ }): Promise<void>;
2892
+ /**
2893
+ * Promote a successfully-executed draft into a verified script.
2894
+ * The on-disk body already exists at <fileBase>.ts (written at write_script
2895
+ * time) and keeps its name — only the DB row flips status + provenance.
2896
+ */
2897
+ promoteToVerified(recipeId: string, input: PromoteToVerifiedInput): Promise<ScriptRecipe | null>;
2898
+ /**
2899
+ * Commit a user-directed edit: merge a VERIFIED shadow draft back onto the
2900
+ * recipe it was editing, as version N+1 under the SAME recipe id.
2901
+ *
2902
+ * Keeping the id stable is the point of the whole feature — every cached
2903
+ * conversation, `script_dataset` regeneration descriptor and persisted
2904
+ * component spec already points at it, so the correction applies retroactively
2905
+ * to replays instead of stranding them on the old body.
2906
+ *
2907
+ * Steps (see backend/docs/SCRIPT-EDIT-DESIGN.md § Component 5):
2908
+ * 1. archive the current body as `<fileBase>.v<version>.ts`
2909
+ * 2. write the edited body to the original fileBase (atomic)
2910
+ * 3. version++, copy name/description/params, append a history record
2911
+ * 4. reset health counters — the pre-edit failure history is stale
2912
+ * 5. clear component specs — they were validated against the OLD shape
2913
+ * 6. delete the shadow draft (row + file)
2914
+ *
2915
+ * Returns the updated recipe, or null when the commit could not be applied
2916
+ * (caller should then fall back to treating the draft as a new script).
2917
+ */
2918
+ commitEdit(targetId: string, draft: {
2919
+ recipeId: string;
2920
+ name?: string;
2921
+ intentDescription?: string;
2922
+ tags?: string[];
2923
+ parameters?: ScriptParameter[];
2924
+ scriptBody: string;
2925
+ sourceIds?: string[];
2926
+ tables?: string[];
2927
+ }, meta: {
2928
+ instruction?: string;
2929
+ changeSummary?: string;
2930
+ editedBy?: string;
2931
+ }): Promise<ScriptRecipe | null>;
2932
+ /**
2933
+ * Drop a draft (row + body file). MainAgent calls this at end-of-turn when a
2934
+ * draft was authored but never verified — failed drafts are never matched, so
2935
+ * deleting them immediately avoids unbounded accumulation (#5). No-op if the
2936
+ * recipe isn't a draft (so a promoted/verified script is never removed here).
2937
+ */
2938
+ discardDraft(recipeId: string): Promise<void>;
2939
+ /** Delete a recipe (row + body file). */
2940
+ delete(id: string): Promise<void>;
2941
+ /** Record a successful execution (atomic counter bump). */
2942
+ recordSuccess(id: string): Promise<void>;
2943
+ /** Record a failed execution (atomic counter bump). */
2944
+ recordFailure(id: string): Promise<void>;
2945
+ /** Absolute path to the .ts body for a recipe (used by the runner/MainAgent). */
2946
+ getScriptPath(recipe: ScriptRecipe): string;
2947
+ private removeById;
2948
+ private rowToRecipe;
2949
+ private recipeToRow;
2950
+ /** slug of name, with a short id suffix when the bare slug is already taken. */
2951
+ private computeFileBase;
2952
+ private toSlug;
2953
+ private hash;
2954
+ /**
2955
+ * Attach getComponents to the recipe body. The stored `components` column is
2956
+ * cleared at promotion (see promoteToVerified): once the body computes its own
2957
+ * specs, a stale spec array beside a live program is how the wrong one gets
2958
+ * read later. See backend/docs/COMPONENTS-AS-PROGRAM-DESIGN.md § 4.4.
2959
+ */
2960
+ /**
2961
+ * Body text as it stands now, for reverting an attach that turns out to be
2962
+ * invalid.
2963
+ *
2964
+ * getComponents can only be VALIDATED by running it, and it can only be run
2965
+ * once it is on disk — so attachComponents necessarily writes before anyone
2966
+ * knows whether the body is good. Without a revert, a rejected body stays
2967
+ * attached: promote() then sees the recipe "exports getComponents", clears
2968
+ * the specs the fallback generator produced, and the recipe is verified with
2969
+ * component code that was already rejected. Confirmed live on
2970
+ * "Total Bookings by Branch", which shipped a sourceRef of 'computed:_final'
2971
+ * that validateSpecs had refused.
2972
+ */
2973
+ snapshotBody(recipeId: string): Promise<string | null>;
2974
+ /** Put back a body captured by {@link snapshotBody}. */
2975
+ restoreBody(recipeId: string, body: string): Promise<boolean>;
2976
+ attachComponents(recipeId: string, componentsBody: string): Promise<boolean>;
2977
+ /**
2978
+ * Append (or replace) the `getAnalysis` export on an existing draft body.
2979
+ *
2980
+ * The narrative is authored AFTER execution, so it lands on a file that
2981
+ * already holds a working getData — rewriting the whole body would risk
2982
+ * losing the query that just succeeded. Replaces any earlier getAnalysis so
2983
+ * a retry does not stack duplicate exports.
2984
+ */
2985
+ attachAnalysis(recipeId: string, analysisBody: string): Promise<boolean>;
2986
+ private bodyPath;
2987
+ private readBody;
2988
+ /** Directory holding superseded bodies. Dot-prefixed so IDEs/`ls` hide it. */
2989
+ private get archiveDir();
2990
+ /**
2991
+ * Archive a superseded body as `.versions/<fileBase>.v<n>.ts`.
2992
+ *
2993
+ * Kept out of the main store directory on purpose — see commitEdit step 1.
2994
+ * To roll back: copy the file back over `scripts-store/<fileBase>.ts`.
2995
+ */
2996
+ private writeArchive;
2997
+ /**
2998
+ * Move a recipe's archived versions to a new prefix when its fileBase changes,
2999
+ * so all versions of one recipe stay grouped. Without this, two renames would
3000
+ * scatter a single recipe's history across three prefixes in `.versions/` with
3001
+ * nothing linking them back to the live script.
3002
+ */
3003
+ private renameArchives;
3004
+ /** Atomic body write (temp + rename) so concurrent reads never see a partial file. */
3005
+ private writeBody;
3006
+ private unlinkBody;
3007
+ }
3008
+
3009
+ /**
3010
+ * Main Agent (Orchestrator)
3011
+ *
3012
+ * A single LLM.streamWithTools() call that handles everything:
3013
+ * - Routing: decides which source(s) to query based on summaries
3014
+ * - Querying: calls source tools (each wraps an independent SourceAgent)
3015
+ * - Direct tools: calls pre-built function tools directly with LLM-provided params
3016
+ * - Re-querying: if data is wrong/incomplete, calls tools again with modified intent
3017
+ * - Analysis: generates final text response from the data
3018
+ *
3019
+ * Two tool types:
3020
+ * - "source" tools: main agent sees summaries, SourceAgent handles SQL generation independently
3021
+ * - "direct" tools: main agent calls fn() directly with structured params (no SourceAgent)
3022
+ */
3023
+
3024
+ declare class MainAgent {
3025
+ private externalTools;
3026
+ private workflows;
3027
+ private config;
3028
+ private streamBuffer;
3029
+ /**
3030
+ * Optional: when provided, MainAgent exposes the `write_script` /
3031
+ * `execute_script` tools to the LLM and persists drafts to disk via the
3032
+ * store. Headless callers (alert analyzer, metric resolver) omit these to
3033
+ * suppress script authoring entirely — drafts would otherwise leak onto
3034
+ * disk with no caller to promote or clean them up.
3035
+ */
3036
+ private scriptStore;
3037
+ private turnId;
3038
+ private createdFromPrompt;
3039
+ private scriptState;
3040
+ /** Answer-flow component catalog (filtered + projected by the caller). */
3041
+ private componentCatalog;
3042
+ /** Specs accepted by `write_components` this turn; empty until it succeeds. */
3043
+ /**
3044
+ * Entities resolved this turn, appended verbatim to every source dispatch.
3045
+ *
3046
+ * Not left to the model to copy into its intent prose: observed live, the
3047
+ * MainAgent wrote names instead of ids on the first dispatch AND re-spelled
3048
+ * 'F D C LIMITED' as 'FDC Limited', so the source agent fell back to
3049
+ * `PartyName LIKE '%FDC%'` and found 2 of 3 customers. Carrying the ids
3050
+ * structurally makes that impossible rather than instruction-dependent.
3051
+ */
3052
+ private resolvedEntities;
3053
+ /**
3054
+ * Concept bodies resolved this turn (via resolve_entities' embedded concept
3055
+ * lookup or a standalone resolve_concepts call) — retained the same way as
3056
+ * resolvedEntities above, so they can be attached to the SourceAgent dispatch.
3057
+ *
3058
+ * Without this, a concept resolved HERE (MainAgent's own tool loop) never
3059
+ * reaches SourceAgent at all: SourceAgent only gets the natural-language
3060
+ * `intent` MainAgent writes, which is a paraphrase, not the concept's
3061
+ * verbatim, authoritative body. Deduped on name so a mid-turn re-resolve
3062
+ * doesn't repeat an already-attached concept.
3063
+ */
3064
+ private resolvedConcepts;
3065
+ private componentSpecs;
3066
+ private componentLayout;
3067
+ private renderComponentAttempts;
3068
+ /** Same repeated-violation tracking as ScriptState.lastAnalysisViolationTexts, for write_components. */
3069
+ private lastComponentsViolationTexts;
3070
+ /**
3071
+ * Fork mode — set when this turn is adapting a near-matching parent script.
3072
+ * In fork mode there is no legitimate "answer with bare text" outcome: the
3073
+ * only correct first move is a tool call (write_script, or a source tool for
3074
+ * schema discovery). We therefore force tool use on the first LLM iteration
3075
+ * so the model can't end its turn with a bare "I'll adapt…" preamble and zero
3076
+ * tool calls. Never set on the fresh-authoring / general-question path.
3077
+ */
3078
+ private forkMode;
3079
+ /**
3080
+ * Edit mode — set when this turn applies a user-directed change to an
3081
+ * existing script. Swaps the system prompt to `agent-main-edit` and stamps
3082
+ * the shadow draft's parentId. Like fork mode there is no legitimate
3083
+ * "answer with bare text" outcome, so tool use is forced on iteration 1.
3084
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 4.
3085
+ */
3086
+ private editContext;
3087
+ /** The recipe's durable rendering choice — see the constructor. */
3088
+ private displayPreference;
3089
+ /**
3090
+ * Per-turn cancellation signal (user hit "Stop"). Set at the top of
3091
+ * handleQuestion and read by the tool handler, the SourceAgent dispatch, and
3092
+ * the script subprocess so an abort tears down every layer of the turn.
3093
+ */
3094
+ private abortSignal?;
3095
+ constructor(externalTools: ExternalTool[], config: AgentConfig, scriptStore?: ScriptStore, turnId?: string, streamBuffer?: StreamBuffer, workflows?: WorkflowDescriptor[], forkMode?: boolean, editContext?: EditContext, componentCatalog?: Component[],
3096
+ /**
3097
+ * The recipe's durable rendering choice, when it has one. A DATA edit
3098
+ * re-authors getComponents from scratch, so without this the user's chart
3099
+ * type is silently dropped whenever anyone changes the SQL.
3100
+ */
3101
+ displayPreference?: {
3102
+ componentTypes: string[];
3103
+ instruction: string;
3104
+ });
3105
+ /** True when the turn is applying a user-directed script edit. */
3106
+ private get editMode();
3107
+ private get scriptingEnabled();
3108
+ /**
3109
+ * Handle a user question using the multi-agent system.
3110
+ *
3111
+ * This is ONE LLM.streamWithTools() call. The LLM:
3112
+ * 1. Sees source summaries + direct tool descriptions in system prompt
3113
+ * 2. Decides which tool(s) to call (routing)
3114
+ * 3. Source tools → SourceAgent runs independently → returns data
3115
+ * 4. Direct tools → fn() called directly with LLM params → returns data
3116
+ * 5. Generates final analysis text
3117
+ */
3118
+ handleQuestion(userPrompt: string, apiKey?: string, conversationHistory?: string, streamCallback?: (chunk: string) => void, signal?: AbortSignal): Promise<AgentResponse>;
3119
+ /**
3120
+ * Tool definition, with the catalog rendered from the registered `Metadata`
3121
+ * the caller passed in. Generated rather than hand-written so it cannot drift
3122
+ * from what the frontend actually ships.
3123
+ */
3124
+ private buildWriteComponentsToolDef;
3125
+ /**
3126
+ * Validate the agent's component picks against the REAL columns of each bound
3127
+ * dataset. All-or-nothing: any error rejects the whole call and returns the
3128
+ * findings, because a partially-accepted dashboard makes the retry ambiguous.
3129
+ */
3130
+ private validateSpecs;
3131
+ /**
3132
+ * Author the dashboard as a program. Attaches getComponents to the draft, runs
3133
+ * it over the verified rows, then validates the specs it RETURNED — the program
3134
+ * is never trusted because it is a program, exactly as an authored spec is not.
3135
+ */
3136
+ private handleWriteComponents;
3137
+ /**
3138
+ * Restrictions the seam reported while this run's queries executed.
3139
+ *
3140
+ * getData does NOT re-run for write_components/write_analysis, so the child
3141
+ * cannot observe these itself — the parent hands them across, exactly as it
3142
+ * does for `entities`.
3143
+ */
3144
+ private observedPolicies;
3145
+ /**
3146
+ * Author the answer as a program. Runs after execute_script, so the body is
3147
+ * written with the result already known and can react to what it found.
3148
+ *
3149
+ * Rejects a body that types numbers rather than computing them — see
3150
+ * analysis-lint. A hardcoded figure is worse than prose: it freezes into the
3151
+ * recipe and replays unchanged while the data moves underneath it.
3152
+ */
3153
+ private handleWriteAnalysis;
3154
+ private handleWriteScript;
3155
+ private handleExecuteScript;
3156
+ /**
3157
+ * Build the AgentWrittenScript payload the caller will hand to
3158
+ * `ScriptStore.promoteToVerified()`. Only returned when a verified
3159
+ * successful execution is on record.
3160
+ */
3161
+ private buildSavedScript;
3162
+ private normalizeParameterList;
3163
+ /**
3164
+ * Which declared parameter NAMES never actually appear wired into the
3165
+ * script body — checked as plain text, not a real parser, so it only
3166
+ * catches the worst case (completely unreferenced) rather than every
3167
+ * subtler variant (e.g. used for display text while the SQL itself still
3168
+ * hardcodes the value). That worst case is exactly what's been observed:
3169
+ * a parameter declared purely for show, with the actual filter hardcoded.
3170
+ * Accepts `params.name`, `params['name']`/`params["name"]`, and a
3171
+ * destructuring binding (`const { name } = params`) as valid wiring.
3172
+ */
3173
+ private findUnwiredParams;
3174
+ /**
3175
+ * Use the schema embedding collection to pre-select relevant tables for
3176
+ * this source + intent. Returns a formatted schema block if confidence is
3177
+ * high (top match ≥ 0.55 and ≥3 candidates), otherwise null.
3178
+ *
3179
+ * When this returns a block, we can skip the SourceAgent's `search_schema`
3180
+ * loop and reduce iteration budget. When it returns null, the SourceAgent
3181
+ * falls back to the existing LLM-driven keyword search (same as today).
3182
+ */
3183
+ /**
3184
+ * Ids + verbatim names for everything resolved this turn, appended by CODE to
3185
+ * each source dispatch so neither omission nor re-spelling by the model can
3186
+ * lose them. Empty string when nothing was resolved, so intents are untouched
3187
+ * for questions that name no entities.
3188
+ *
3189
+ * Also appends `attrs.scopeSql` when the resolved instance carries one —
3190
+ * same "guaranteed by code" reasoning as the ids/names above, extended to
3191
+ * the one attrs field with a documented, cross-entity-type meaning: a
3192
+ * ready-made SQL predicate an entity's index precomputed (see
3193
+ * ENTITY-SEARCH-DESIGN.md's hierarchy-broadening pattern — branch/region/
3194
+ * state entity types attach it so a lane/route question never needs its
3195
+ * own city-matching join). Confirmed real cost of not doing this: a
3196
+ * branch/state resolved with scopeSql sat unused while the source agent
3197
+ * spent 4 failed exploratory queries rediscovering the exact same
3198
+ * branch-id list by hand (Invalid column name 'StateName'/'CityId'/
3199
+ * 'OrganizationLocationId', in that order, ~144s, before landing on ids
3200
+ * that were sitting in scopeSql from the very first resolve_entities
3201
+ * call). Other attrs (code, counts, etc.) are not dumped here — they are
3202
+ * descriptive, not actionable the way a precomputed predicate is, and
3203
+ * bloating every dispatch with all of them for no clear gain isn't worth
3204
+ * it; scopeSql is the one thing worth this guarantee today.
3205
+ */
3206
+ private buildResolvedEntitiesBlock;
3207
+ /** Dedupe-and-retain helper shared by resolveConceptsForTurn and handleResolveConcepts. */
3208
+ private retainResolvedConcepts;
3209
+ /**
3210
+ * Same purpose as buildResolvedEntitiesBlock, for concept bodies instead of
3211
+ * entity ids — appended to the dispatch intent so SourceAgent's own,
3212
+ * independent LLM call receives the resolved definitions verbatim rather
3213
+ * than only MainAgent's paraphrased intent text.
3214
+ */
3215
+ private buildResolvedConceptsBlock;
3216
+ /** True when the backend registered the entity-search collection. */
3217
+ private get entitySearchEnabled();
3218
+ /** True when the backend registered the concepts collection. */
3219
+ private get conceptsEnabled();
3220
+ /**
3221
+ * Concept names for the system prompt. Names ONLY — the body comes back from
3222
+ * resolve_concepts once the LLM has named one, exactly as with the entity
3223
+ * catalog. Putting bodies here would rebuild the always-on context problem
3224
+ * concepts exist to solve.
3225
+ *
3226
+ * Not cached, for the same reason loadEntityCatalog isn't: unchanged rows
3227
+ * render byte-identical text so the prompt-cache prefix still hits, and a
3228
+ * process cache would only buy a window where a new concept is invisible.
3229
+ */
3230
+ private loadConceptCatalog;
3231
+ /**
3232
+ * The ALWAYS-INJECTED tier: preconditions no question is semantically close
3233
+ * to — date encodings, valid-row filters. These cannot be retrieval-gated.
3234
+ * "What was revenue last quarter?" has no similarity to "dates are DD/MM/YYYY
3235
+ * text", so leaving them to be requested means they are silently dropped and
3236
+ * the answer is confidently wrong rather than visibly missing.
3237
+ *
3238
+ * Bodies ARE included here — unlike the catalog — because there is nothing to
3239
+ * request: being unrequestable is what makes them invariants.
3240
+ *
3241
+ * Also retained into resolvedConcepts (same as an explicit resolve_concepts
3242
+ * call) so these bodies reach SourceAgent's dispatch too — an invariant is
3243
+ * meant to apply everywhere, not just wherever the model happens to ask for
3244
+ * something whose `requires` chain reaches it.
3245
+ */
3246
+ private loadConceptInvariants;
3247
+ /**
3248
+ * Resolve the concepts for a turn, with a SERVER-SIDE FLOOR.
3249
+ *
3250
+ * Pinning a tool guarantees it is called; it does not guarantee the model
3251
+ * fills in the arguments. Observed: a question containing the exact words
3252
+ * "revenue forecast" produced a call with no concepts at all, and the agent
3253
+ * then used an incomplete copy of the formula from the knowledge base — the
3254
+ * resulting query filtered a customer id against a project column and
3255
+ * returned zero rows on every attempt.
3256
+ *
3257
+ * So when the model names nothing, scan the question itself for catalog names
3258
+ * and aliases on word boundaries. Precise (a literal name is evidence, not a
3259
+ * guess) and asymmetric in the safe direction: an unneeded definition costs a
3260
+ * few hundred ignorable tokens, a missing one costs a wrong answer.
3261
+ */
3262
+ /**
3263
+ * Returns the tool-facing fields to spread into the resolve_entities JSON
3264
+ * response, PLUS a `_trace` field with everything the trace needs that the
3265
+ * model does not (asked/viaFloor/unmatched). Callers must pull `_trace` out
3266
+ * before spreading the rest into what the model sees — see handleResolveEntities.
3267
+ */
3268
+ private resolveConceptsForTurn;
3269
+ /**
3270
+ * Never throws: a concept-store outage must degrade to "no definition found"
3271
+ * and leave the agent deriving the formula as it does today, not take the
3272
+ * whole answer down.
3273
+ */
3274
+ private handleResolveConcepts;
3275
+ /**
3276
+ * Compact catalog (type + gloss) for the system prompt. This is what lets the
3277
+ * LLM NAME a concept instead of guessing at our vocabulary — it can infer
3278
+ * that a question is about unpaid money, but not that we call that
3279
+ * `outstanding-balance`. Full `resolution` detail is fetched by the tool.
3280
+ *
3281
+ * Deliberately NOT cached in-process. The token cost is already handled by
3282
+ * prompt caching (the system prompt carries a cache_control breakpoint), and
3283
+ * re-reading a tiny PK-ordered table costs ~nothing. Fetching fresh does not
3284
+ * weaken the prompt cache either: unchanged data renders byte-identical text,
3285
+ * so the prefix still hits. A process cache would only buy a staleness window
3286
+ * in which a newly indexed entity is invisible to the agent.
3287
+ */
3288
+ private loadEntityCatalog;
3289
+ /**
3290
+ * Never throws: a resolver outage must degrade to "nothing resolved" and
3291
+ * leave the agent exactly as it was, not take the whole answer down.
3292
+ */
3293
+ private handleResolveEntities;
3294
+ private preResolveSchema;
3295
+ /**
3296
+ * Execute a direct tool — call fn() with LLM-provided params, no SourceAgent.
3297
+ */
3298
+ private handleDirectTool;
3299
+ /**
3300
+ * Build the main agent's system prompt with source summaries, direct tool descriptions,
3301
+ * and workflow component descriptions.
3302
+ */
3303
+ private buildSystemPrompt;
3304
+ /**
3305
+ * Build tool definitions for source tools — summary-only descriptions.
3306
+ * The full schema is inside the SourceAgent which runs independently.
3307
+ */
3308
+ private buildSourceToolDefinitions;
3309
+ /**
3310
+ * Build tool definitions for direct tools — expose their actual params.
3311
+ * These are called directly by the main agent LLM, no SourceAgent.
3312
+ */
3313
+ private buildDirectToolDefinitions;
3314
+ /**
3315
+ * Capture a workflow selection. We do NOT execute anything — the LLM has
3316
+ * already extracted the props it wants the workflow rendered with. We
3317
+ * record the selection (via the capture callback) and return a short
3318
+ * acknowledgement so the LLM ends its turn cleanly without writing
3319
+ * analysis text or calling more tools.
3320
+ */
3321
+ private handleWorkflow;
3322
+ /**
3323
+ * Build LLM tool definitions for workflow components. The workflow's
3324
+ * propsSchema becomes the tool's input_schema so the LLM extracts props
3325
+ * directly from the prompt — same mechanic as direct tools.
3326
+ */
3327
+ private buildWorkflowToolDefinitions;
3328
+ /**
3329
+ * Format a source agent's result as a clean string for the main agent LLM.
3330
+ */
3331
+ private formatResultForMainAgent;
3332
+ /**
3333
+ * Get source summaries (for external inspection/debugging).
3334
+ */
3335
+ getSourceSummaries(): SourceSummary[];
3336
+ }
3337
+
3338
+ interface FmtConfig {
3339
+ /** BCP-47 tag driving grouping and the scale vocabulary, e.g. 'en-IN', 'en-US'. */
3340
+ locale: string;
3341
+ /** ISO-4217 code for money, e.g. 'INR', 'USD'. */
3342
+ currency: string;
3343
+ }
3344
+ declare const DEFAULT_FMT_CONFIG: FmtConfig;
3345
+ interface Fmt {
3346
+ money(v: unknown, decimals?: number): string;
3347
+ num(v: unknown, decimals?: number): string;
3348
+ count(v: unknown, decimals?: number): string;
3349
+ pct(part: unknown, whole: unknown, decimals?: number): string;
3350
+ delta(current: unknown, previous: unknown, decimals?: number): string;
3351
+ date(v: unknown): string;
3352
+ range(from: unknown, to: unknown): string;
3353
+ list(items: unknown[], conjunction?: string): string;
3354
+ }
3355
+ /** Build a `fmt` bound to one locale/currency. */
3356
+ declare function makeFmt(cfg?: Partial<FmtConfig>): Fmt;
3357
+
3358
+ type SystemPrompt = string | Anthropic.Messages.TextBlockParam[];
3359
+ interface LLMMessages {
3360
+ sys: SystemPrompt;
3361
+ user: string;
3362
+ prefill?: string;
3363
+ }
3364
+ interface LLMOptions {
3365
+ model?: string;
3366
+ maxTokens?: number;
3367
+ temperature?: number;
3368
+ topP?: number;
3369
+ apiKey?: string;
3370
+ baseURL?: string;
1295
3371
  partial?: (chunk: string) => void;
3372
+ /**
3373
+ * Per-request cancellation. When the caller aborts this signal (user hit
3374
+ * "Stop"), the underlying provider request is cancelled and the call throws
3375
+ * a RequestAbortedError. Threaded into the provider `messages.create` request
3376
+ * options and checked between tool-loop iterations. Currently honored on the
3377
+ * Anthropic path (the agent flow's default provider).
3378
+ */
3379
+ signal?: AbortSignal;
3380
+ /**
3381
+ * Forces a tool call on the FIRST iteration of streamWithTools only
3382
+ * (subsequent iterations revert to auto). Used by fork mode to stop the
3383
+ * model from ending its turn with a bare "I'll adapt the script…" preamble
3384
+ * and zero tool calls. `{ type: 'any' }` lets the model pick which tool
3385
+ * (write_script in the common case, a source tool for schema discovery);
3386
+ * `{ type: 'tool', name }` pins a specific tool. Honored on both the
3387
+ * Anthropic path and the OpenAI/OpenRouter path (mapped to OpenAI's
3388
+ * tool_choice: 'required' / a named function).
3389
+ */
3390
+ firstIterationToolChoice?: {
3391
+ type: 'any';
3392
+ } | {
3393
+ type: 'tool';
3394
+ name: string;
3395
+ };
3396
+ /**
3397
+ * Internal — set only by the OpenRouter wrappers when the target is a Claude
3398
+ * model. Tells the OpenAI-wire path to emit Anthropic `cache_control`
3399
+ * breakpoints (OpenRouter forwards them to Anthropic for prompt caching).
3400
+ * Never set for direct OpenAI/Groq calls, so their requests are unchanged.
3401
+ */
3402
+ _openrouterClaudeCaching?: boolean;
3403
+ /**
3404
+ * Internal — OpenRouter provider-routing preferences (forwarded as the
3405
+ * `provider` body field). Set by the OpenRouter wrappers to steer routing to
3406
+ * a fast backend (e.g. {sort:'throughput'}). Never set for direct OpenAI/Groq.
3407
+ */
3408
+ _openrouterProvider?: Record<string, unknown>;
1296
3409
  }
1297
3410
  interface Tool {
1298
3411
  name: string;
@@ -1314,6 +3427,16 @@ declare class LLM {
1314
3427
  * @returns Normalized system prompt for Anthropic API
1315
3428
  */
1316
3429
  private static _normalizeSystemPrompt;
3430
+ /**
3431
+ * Strip unpaired UTF-16 surrogates from every text field of a message set.
3432
+ *
3433
+ * A lone surrogate (from mid-pair string slicing or corrupt source data)
3434
+ * serializes to a bare `\udXXX` escape that strict JSON parsers — including
3435
+ * the one on Anthropic's API — reject with "no low surrogate in string",
3436
+ * failing the whole request. Sanitizing here, at the single boundary every
3437
+ * provider call flows through, guarantees no request can carry one.
3438
+ */
3439
+ private static _sanitizeMessages;
1317
3440
  /**
1318
3441
  * Log cache usage metrics from Anthropic API response
1319
3442
  * Shows cache hits, costs, and savings
@@ -1330,16 +3453,76 @@ declare class LLM {
1330
3453
  * "claude-sonnet-4-5" → ["anthropic", "claude-sonnet-4-5"] (default)
1331
3454
  */
1332
3455
  private static _parseModel;
3456
+ /**
3457
+ * Map an Anthropic model id (e.g. "claude-sonnet-4-5-20250929") to the OpenRouter slug
3458
+ * (e.g. "claude-sonnet-4.5"). OpenRouter slugs drop the date suffix and use dotted versions.
3459
+ */
3460
+ private static _toOpenRouterSlug;
3461
+ /**
3462
+ * Per-provider proxy base URL. Returns `${SUPERATOM_LLM_PROXY_URL}/<provider>`
3463
+ * when our Cloudflare LLM proxy is configured, else undefined (→ talk to the
3464
+ * provider directly, legacy behaviour). An explicit options.baseURL (e.g.
3465
+ * OpenRouter) always wins and is never overridden. See backend/docs/llm-proxy.md.
3466
+ */
3467
+ private static _proxyBaseURL;
3468
+ private static _openrouterOptions;
3469
+ private static _isRetryableProviderError;
3470
+ private static _withOpenrouterRetry;
3471
+ private static _openrouterText;
3472
+ private static _openrouterStream;
3473
+ private static _openrouterStreamWithTools;
3474
+ /**
3475
+ * Build an Anthropic client. Routes through our Cloudflare LLM proxy when
3476
+ * SUPERATOM_LLM_PROXY_URL is set (each client ships a per-client proxy key as
3477
+ * ANTHROPIC_API_KEY and never holds the real key); otherwise talks to
3478
+ * api.anthropic.com directly. See backend/docs/llm-proxy.md.
3479
+ */
3480
+ private static _anthropicClient;
3481
+ /** True when OpenRouter is configured as a fail-open fallback for Claude. */
3482
+ private static _openrouterAvailable;
3483
+ /** Remap an Anthropic model id to the OpenRouter model path for fail-open. */
3484
+ private static _anthropicFallbackModel;
1333
3485
  private static _anthropicText;
1334
3486
  private static _anthropicStream;
1335
3487
  private static _anthropicStreamWithTools;
1336
3488
  private static _groqText;
1337
3489
  private static _groqStream;
3490
+ /**
3491
+ * Gemini request options carrying the proxy base URL, or undefined → talk to
3492
+ * generativelanguage.googleapis.com directly. The Google SDK takes baseUrl as a
3493
+ * per-model request option, not a constructor arg. See backend/docs/llm-proxy.md.
3494
+ */
3495
+ private static _geminiRequestOptions;
1338
3496
  private static _geminiText;
1339
3497
  private static _geminiStream;
3498
+ /**
3499
+ * Recursively strip unsupported JSON Schema properties for Gemini
3500
+ * Gemini doesn't support: additionalProperties, $schema, etc.
3501
+ */
3502
+ private static _cleanSchemaForGemini;
1340
3503
  private static _geminiStreamWithTools;
3504
+ /** True for Anthropic/Claude model ids — gates OpenRouter prompt caching. */
3505
+ private static _isClaudeModel;
3506
+ /**
3507
+ * Build the OpenAI-wire system message. For OpenRouter + Claude
3508
+ * (cacheClaude=true) it emits content parts carrying Anthropic
3509
+ * `cache_control` breakpoints (preserving any the caller set, else marking
3510
+ * the last block), so OpenRouter forwards them to Anthropic for prompt
3511
+ * caching. Otherwise it returns a plain flattened string — unchanged for
3512
+ * direct OpenAI/Groq.
3513
+ */
3514
+ private static _openaiSystemMessage;
3515
+ /**
3516
+ * Split an OpenAI-wire usage object. `prompt_tokens` INCLUDES cached tokens,
3517
+ * so we subtract them out (Anthropic-style: input excludes cache reads) and
3518
+ * report cached separately — this makes calculateCost price cache reads at
3519
+ * the discounted rate and reflects OpenRouter prompt-cache savings in logs.
3520
+ */
3521
+ private static _openaiUsage;
1341
3522
  private static _openaiText;
1342
3523
  private static _openaiStream;
3524
+ /** Map the Anthropic-style firstIterationToolChoice to OpenAI's tool_choice. */
3525
+ private static _openaiToolChoice;
1343
3526
  private static _openaiStreamWithTools;
1344
3527
  /**
1345
3528
  * Parse JSON string, handling markdown code blocks and surrounding text
@@ -1429,19 +3612,9 @@ declare class UILogCollector {
1429
3612
  setUIBlockId(uiBlockId: string): void;
1430
3613
  }
1431
3614
 
1432
- /**
1433
- * Represents an action that can be performed on a UIBlock
1434
- */
1435
- interface Action {
1436
- id: string;
1437
- name: string;
1438
- type: string;
1439
- [key: string]: any;
1440
- }
1441
-
1442
3615
  /**
1443
3616
  * UIBlock represents a single user and assistant message block in a thread
1444
- * Contains user question, component metadata, component data, text response, and available actions
3617
+ * Contains user question, component metadata, component data, and text response
1445
3618
  */
1446
3619
  declare class UIBlock {
1447
3620
  private id;
@@ -1449,18 +3622,23 @@ declare class UIBlock {
1449
3622
  private generatedComponentMetadata;
1450
3623
  private componentData;
1451
3624
  private textResponse;
1452
- private actions;
1453
3625
  private createdAt;
3626
+ /**
3627
+ * Which script recipe produced this answer, when a script did. Read on the
3628
+ * NEXT turn so the user can say "use mode instead" and have the matcher
3629
+ * resolve it to a concrete script (the `edit` tier is unreachable without it).
3630
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 1.
3631
+ */
3632
+ private scriptBinding;
1454
3633
  /**
1455
3634
  * Creates a new UIBlock instance
1456
3635
  * @param userQuestion - The user's question or input
1457
3636
  * @param componentData - The component data object
1458
3637
  * @param generatedComponentMetadata - Optional metadata about the generated component
1459
- * @param actions - Optional array of available actions
1460
3638
  * @param id - Optional custom ID, generates UUID if not provided
1461
3639
  * @param textResponse - Optional text response from LLM
1462
3640
  */
1463
- constructor(userQuestion: string, componentData?: Record<string, any>, generatedComponentMetadata?: Record<string, any>, actions?: Action[], id?: string, textResponse?: string | null);
3641
+ constructor(userQuestion: string, componentData?: Record<string, any>, generatedComponentMetadata?: Record<string, any>, id?: string, textResponse?: string | null);
1464
3642
  /**
1465
3643
  * Get the UIBlock ID
1466
3644
  */
@@ -1510,41 +3688,34 @@ declare class UIBlock {
1510
3688
  * Set or update text response
1511
3689
  */
1512
3690
  setTextResponse(textResponse: string | null): void;
3691
+ /** The program-generated half of the answer — see setProgramAnswer. */
3692
+ programAnswer?: string | null;
3693
+ /** Progress updates written while the answer was produced — see setNarration. */
3694
+ narration?: NarrationUpdate[] | null;
1513
3695
  /**
1514
- * Get all actions (only if they are resolved, not if fetching)
1515
- */
1516
- getActions(): Action[] | null | Promise<Action[]>;
1517
- /**
1518
- * Get or fetch actions
1519
- * If actions don't exist or are a Promise, calls the generateFn and stores the promise
1520
- * If actions already exist, returns them
1521
- * @param generateFn - Async function to generate actions
1522
- * @returns Promise resolving to Action[]
1523
- */
1524
- getOrFetchActions(generateFn: () => Promise<Action[]>): Promise<Action[]>;
1525
- /**
1526
- * Set or replace all actions
1527
- */
1528
- setActions(actions: Action[]): void;
1529
- /**
1530
- * Add a single action (only if actions are resolved)
1531
- */
1532
- addAction(action: Action): void;
1533
- /**
1534
- * Add multiple actions (only if actions are resolved)
3696
+ * Progress updates a narrator wrote while this answer was being produced,
3697
+ * in order. Not part of the answer text: kept beside it so it is saved with
3698
+ * the answer it describes, and can be shown or left out on its own.
1535
3699
  */
1536
- addActions(actions: Action[]): void;
3700
+ setNarration(updates: NarrationUpdate[] | null): void;
3701
+ getNarration(): NarrationUpdate[] | null;
3702
+ /** How long the answer took, from the request arriving to the flow returning. */
3703
+ durationMs?: number | null;
3704
+ setDurationMs(ms: number | null): void;
1537
3705
  /**
1538
- * Remove an action by ID (only if actions are resolved)
3706
+ * Bind this block to the script recipe that produced its answer.
1539
3707
  */
1540
- removeAction(actionId: string): boolean;
3708
+ setScriptBinding(binding: Record<string, any> | null): void;
1541
3709
  /**
1542
- * Clear all actions
3710
+ * The program-generated half of the answer, kept verbatim so a reload can
3711
+ * replace exactly that text without disturbing the streamed narration.
1543
3712
  */
1544
- clearActions(): void;
3713
+ setProgramAnswer(text: string | null): void;
3714
+ getProgramAnswer(): string | null;
1545
3715
  /**
1546
- * Get creation timestamp
3716
+ * The script recipe bound to this block, if any.
1547
3717
  */
3718
+ getScriptBinding(): Record<string, any> | null;
1548
3719
  getCreatedAt(): Date;
1549
3720
  /**
1550
3721
  * Convert UIBlock to JSON-serializable object
@@ -1612,7 +3783,55 @@ declare class Thread {
1612
3783
  * @param currentUIBlockId - ID of current UIBlock to exclude from context (optional)
1613
3784
  * @returns Formatted conversation history string
1614
3785
  */
1615
- getConversationContext(limit?: number, currentUIBlockId?: string): string;
3786
+ /**
3787
+ * The script recipe bound to the most recent completed UIBlock — i.e. the
3788
+ * script behind the answer the user is currently looking at. Drives the
3789
+ * matcher's `edit` tier: without it, "use mode instead" has no target and
3790
+ * falls through to regeneration.
3791
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 1.
3792
+ */
3793
+ getActiveScriptBinding(currentUIBlockId?: string): Record<string, any> | null;
3794
+ /**
3795
+ * The recent script-backed answers in this thread, newest first — the
3796
+ * candidate set a user-directed edit can target.
3797
+ *
3798
+ * Returning several rather than only the newest is what lets an instruction
3799
+ * name its own target ("use the mode for the WSP one"). With a single
3800
+ * candidate every edit lands on the most recent script, which silently edits
3801
+ * the wrong recipe whenever the user meant an earlier one.
3802
+ *
3803
+ * Deduped by recipeId (newest occurrence wins) so a long editing session on
3804
+ * one script doesn't crowd out the others. Each entry carries the question
3805
+ * that produced it — without that the candidates are indistinguishable.
3806
+ *
3807
+ * In-memory only: dies with the process. The caller falls back to the
3808
+ * persisted bindings when this comes back empty.
3809
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 1.
3810
+ */
3811
+ getScriptBindings(limit?: number, currentUIBlockId?: string): Record<string, any>[];
3812
+ /**
3813
+ * @param getScriptBody Resolves a script-backed turn's EXACT script, pinned
3814
+ * to the version that turn actually ran — never today's (possibly
3815
+ * since-edited) live body. Owned by the caller (needs a ScriptStore, which
3816
+ * Thread doesn't hold) — omit it and every turn falls back to the
3817
+ * no-script summary below.
3818
+ * @param fetchRecentFromDb When provided, this is now the SOURCE OF TRUTH
3819
+ * for history instead of this process's in-memory `uiblocks` map — reads
3820
+ * the persisted `user_conversations` rows (DB `response` jsonb shape, see
3821
+ * transformUIBlockForDB in conversation-saver.ts) via the caller's own
3822
+ * `user-conversations.getRecentForContext` collection. Rows come back
3823
+ * newest-first (already excluding status='aborted' — the query itself
3824
+ * filters that, matching what the in-memory path achieved via
3825
+ * thread.removeUIBlock() on abort). Fixes the standing gap where a backend
3826
+ * restart lost every thread's history (ThreadManager has no DB-backed
3827
+ * rehydration) — DB-backed history survives that, and is correct even if
3828
+ * a different process instance saved the row. Omit it and this falls back
3829
+ * to the in-memory map, for any caller not yet updated.
3830
+ */
3831
+ getConversationContext(limit?: number, currentUIBlockId?: string, getScriptBody?: (recipeId: string, version: number) => Promise<string | null>, fetchRecentFromDb?: (limit: number) => Promise<Array<{
3832
+ userprompt?: string;
3833
+ response?: Record<string, any>;
3834
+ }>>): Promise<string>;
1616
3835
  /**
1617
3836
  * Convert Thread to JSON-serializable object
1618
3837
  */
@@ -1621,12 +3840,20 @@ declare class Thread {
1621
3840
 
1622
3841
  /**
1623
3842
  * ThreadManager manages all threads globally
1624
- * Provides methods to create, retrieve, and delete threads
3843
+ * Provides methods to create, retrieve, and delete threads.
3844
+ * Includes automatic cleanup to prevent unbounded memory growth.
1625
3845
  */
1626
3846
  declare class ThreadManager {
1627
3847
  private static instance;
1628
3848
  private threads;
3849
+ private cleanupInterval;
3850
+ private readonly threadTtlMs;
1629
3851
  private constructor();
3852
+ /**
3853
+ * Periodically remove threads older than 7 days.
3854
+ * Runs every hour to avoid frequent iteration over the map.
3855
+ */
3856
+ private startCleanup;
1630
3857
  /**
1631
3858
  * Get singleton instance of ThreadManager
1632
3859
  */
@@ -2025,6 +4252,92 @@ declare function rerankConversationResults<T extends {
2025
4252
  bm25Score: number;
2026
4253
  }>;
2027
4254
 
4255
+ /**
4256
+ * QueryExecutionService - Handles all query execution, validation, and retry logic
4257
+ * Extracted from BaseLLM for better separation of concerns
4258
+ */
4259
+
4260
+ /**
4261
+ * Context for component when requesting query fix
4262
+ */
4263
+ interface ComponentContext {
4264
+ name: string;
4265
+ type: string;
4266
+ title?: string;
4267
+ }
4268
+ /**
4269
+ * Result of query validation
4270
+ */
4271
+ interface QueryValidationResult {
4272
+ component: Component | null;
4273
+ queryKey: string;
4274
+ result: any;
4275
+ validated: boolean;
4276
+ }
4277
+ /**
4278
+ * Result of batch query validation
4279
+ */
4280
+ interface BatchValidationResult {
4281
+ components: Component[];
4282
+ queryResults: Map<string, any>;
4283
+ }
4284
+ /**
4285
+ * Configuration for QueryExecutionService
4286
+ */
4287
+ interface QueryExecutionServiceConfig {
4288
+ defaultLimit: number;
4289
+ getModelForTask: (taskType: 'simple' | 'complex') => string;
4290
+ getApiKey: (apiKey?: string) => string | undefined;
4291
+ providerName: string;
4292
+ }
4293
+ /**
4294
+ * QueryExecutionService handles all query-related operations
4295
+ */
4296
+ declare class QueryExecutionService {
4297
+ private config;
4298
+ constructor(config: QueryExecutionServiceConfig);
4299
+ /**
4300
+ * Get the cache key for a query
4301
+ * This ensures the cache key matches what the frontend will send
4302
+ */
4303
+ getQueryCacheKey(query: any): string;
4304
+ /**
4305
+ * Execute a query against the database
4306
+ * @param query - The SQL query to execute (string or object with sql/values)
4307
+ * @param collections - Collections object containing database execute function
4308
+ * @returns Object with result data and cache key
4309
+ */
4310
+ executeQuery(query: any, collections: any): Promise<{
4311
+ result: any;
4312
+ cacheKey: string;
4313
+ }>;
4314
+ /**
4315
+ * Request the LLM to fix a failed SQL query
4316
+ * @param failedQuery - The query that failed execution
4317
+ * @param errorMessage - The error message from the failed execution
4318
+ * @param componentContext - Context about the component
4319
+ * @param apiKey - Optional API key
4320
+ * @returns Fixed query string
4321
+ */
4322
+ requestQueryFix(failedQuery: string, errorMessage: string, componentContext: ComponentContext, apiKey?: string): Promise<string>;
4323
+ /**
4324
+ * Validate a single component's query with retry logic
4325
+ * @param component - The component to validate
4326
+ * @param collections - Collections object containing database execute function
4327
+ * @param apiKey - Optional API key for LLM calls
4328
+ * @returns Validation result with component, query key, and result
4329
+ */
4330
+ validateSingleQuery(component: Component, collections: any, apiKey?: string): Promise<QueryValidationResult>;
4331
+ /**
4332
+ * Validate multiple component queries in parallel
4333
+ * @param components - Array of components with potential queries
4334
+ * @param collections - Collections object containing database execute function
4335
+ * @param apiKey - Optional API key for LLM calls
4336
+ * @returns Object with validated components and query results map
4337
+ */
4338
+ validateComponentQueries(components: Component[], collections: any, apiKey?: string): Promise<BatchValidationResult>;
4339
+ }
4340
+
2028
4341
  /**
2029
4342
  * Task types for model selection
2030
4343
  * - 'complex': Text generation, component matching, parameter adaptation (uses best model in balanced mode)
@@ -2056,6 +4369,7 @@ declare abstract class BaseLLM {
2056
4369
  protected apiKey?: string;
2057
4370
  protected modelStrategy: ModelStrategy;
2058
4371
  protected conversationSimilarityThreshold: number;
4372
+ protected queryService: QueryExecutionService;
2059
4373
  constructor(config?: BaseLLMConfig);
2060
4374
  /**
2061
4375
  * Get the appropriate model based on task type and model strategy
@@ -2111,60 +4425,26 @@ declare abstract class BaseLLM {
2111
4425
  */
2112
4426
  protected containsFormComponent(component: any): boolean;
2113
4427
  /**
2114
- * Get the cache key for a query (the exact sql param that would be sent to execute)
2115
- * This ensures the cache key matches what the frontend will send
2116
- * Used for both caching and internal deduplication
2117
- */
2118
- private getQueryCacheKey;
2119
- /**
2120
- * Execute a query against the database for validation and caching
2121
- * @param query - The SQL query to execute (string or object with sql/values)
2122
- * @param collections - Collections object containing database execute function
2123
- * @returns Object with result data and cache key
2124
- * @throws Error if query execution fails
2125
- */
2126
- private executeQueryForValidation;
2127
- /**
2128
- * Request the LLM to fix a failed SQL query
2129
- * @param failedQuery - The query that failed execution
2130
- * @param errorMessage - The error message from the failed execution
2131
- * @param componentContext - Context about the component (name, type, title)
2132
- * @param apiKey - Optional API key
2133
- * @returns Fixed query string
2134
- */
2135
- private requestQueryFix;
2136
- /**
2137
- * Match components from text response suggestions and generate follow-up questions
4428
+ * Match components from text response suggestions
2138
4429
  * Takes a text response with component suggestions (c1:type format) and matches with available components
2139
- * Also generates title, description, and intelligent follow-up questions (actions) based on the analysis
4430
+ * Also generates title and description based on the analysis
2140
4431
  * All components are placed in a default MultiComponentContainer layout
2141
4432
  * @param analysisContent - The text response containing component suggestions
2142
4433
  * @param components - List of available components
2143
4434
  * @param apiKey - Optional API key
2144
- * @param logCollector - Optional log collector
2145
4435
  * @param componentStreamCallback - Optional callback to stream primary KPI component as soon as it's identified
2146
- * @returns Object containing matched components, layout title/description, and follow-up actions
4436
+ * @returns Object containing matched components and layout title/description
2147
4437
  */
2148
- matchComponentsFromAnalysis(analysisContent: string, components: Component[], apiKey?: string, logCollector?: any, componentStreamCallback?: (component: Component) => void, deferredTools?: any[], executedTools?: any[], collections?: any, userId?: string, userPrompt?: string): Promise<{
4438
+ matchComponentsFromAnalysis(analysisContent: string, components: Component[], userPrompt: string, apiKey?: string, componentStreamCallback?: (component: Component) => void, deferredTools?: any[], executedTools?: any[], collections?: any, userId?: string): Promise<{
2149
4439
  components: Component[];
2150
4440
  layoutTitle: string;
2151
4441
  layoutDescription: string;
2152
- actions: Action[];
2153
4442
  }>;
2154
- /**
2155
- * Validate component queries against the database and retry with LLM fixes if they fail
2156
- * @param components - Array of components with potential queries
2157
- * @param collections - Collections object containing database execute function
2158
- * @param apiKey - Optional API key for LLM calls
2159
- * @param logCollector - Optional log collector for logging
2160
- * @returns Object with validated components and a map of query results
2161
- */
2162
- private validateAndRetryComponentQueries;
2163
4443
  /**
2164
4444
  * Classify user question into category and detect external tools needed
2165
4445
  * Determines if question is for data analysis, requires external tools, or needs text response
2166
4446
  */
2167
- classifyQuestionCategory(userPrompt: string, apiKey?: string, logCollector?: any, conversationHistory?: string, externalTools?: any[]): Promise<{
4447
+ classifyQuestionCategory(userPrompt: string, apiKey?: string, conversationHistory?: string, externalTools?: any[]): Promise<{
2168
4448
  category: 'data_analysis' | 'data_modification' | 'general';
2169
4449
  externalTools: Array<{
2170
4450
  type: string;
@@ -2181,7 +4461,7 @@ declare abstract class BaseLLM {
2181
4461
  * Takes a matched UI block from semantic search and modifies its props to answer the new question
2182
4462
  * Also adapts the cached text response to match the new question
2183
4463
  */
2184
- adaptUIBlockParameters(currentUserPrompt: string, originalUserPrompt: string, matchedUIBlock: any, apiKey?: string, logCollector?: any, cachedTextResponse?: string): Promise<{
4464
+ adaptUIBlockParameters(currentUserPrompt: string, originalUserPrompt: string, matchedUIBlock: any, apiKey?: string, cachedTextResponse?: string): Promise<{
2185
4465
  success: boolean;
2186
4466
  adaptedComponent?: Component;
2187
4467
  adaptedTextResponse?: string;
@@ -2197,7 +4477,7 @@ declare abstract class BaseLLM {
2197
4477
  * Supports tool calling for query execution with automatic retry on errors (max 3 attempts)
2198
4478
  * After generating text response, if components are provided, matches suggested components
2199
4479
  */
2200
- generateTextResponse(userPrompt: string, apiKey?: string, logCollector?: any, conversationHistory?: string, streamCallback?: (chunk: string) => void, collections?: any, components?: Component[], externalTools?: any[], category?: 'data_analysis' | 'data_modification' | 'general', userId?: string): Promise<T_RESPONSE>;
4480
+ generateTextResponse(userPrompt: string, apiKey?: string, conversationHistory?: string, streamCallback?: (chunk: string) => void, collections?: any, components?: Component[], externalTools?: any[], category?: 'data_analysis' | 'data_modification' | 'general', userId?: string): Promise<T_RESPONSE>;
2201
4481
  /**
2202
4482
  * Main orchestration function with semantic search and multi-step classification
2203
4483
  * NEW FLOW (Recommended):
@@ -2206,13 +4486,7 @@ declare abstract class BaseLLM {
2206
4486
  * 2. Category classification: Determine if data_analysis, requires_external_tools, or text_response
2207
4487
  * 3. Route appropriately based on category and response mode
2208
4488
  */
2209
- handleUserRequest(userPrompt: string, components: Component[], apiKey?: string, logCollector?: any, conversationHistory?: string, responseMode?: 'component' | 'text', streamCallback?: (chunk: string) => void, collections?: any, externalTools?: any[], userId?: string): Promise<T_RESPONSE>;
2210
- /**
2211
- * Generate next questions that the user might ask based on the original prompt and generated component
2212
- * This helps provide intelligent suggestions for follow-up queries
2213
- * For general/conversational questions without components, pass textResponse instead
2214
- */
2215
- generateNextQuestions(originalUserPrompt: string, component?: Component | null, componentData?: Record<string, unknown>, apiKey?: string, logCollector?: any, conversationHistory?: string, textResponse?: string): Promise<string[]>;
4489
+ handleUserRequest(userPrompt: string, components: Component[], apiKey?: string, conversationHistory?: string, responseMode?: 'component' | 'text', streamCallback?: (chunk: string) => void, collections?: any, externalTools?: any[], userId?: string): Promise<T_RESPONSE>;
2216
4490
  }
2217
4491
 
2218
4492
  interface AnthropicLLMConfig extends BaseLLMConfig {
@@ -2272,17 +4546,30 @@ declare class OpenAILLM extends BaseLLM {
2272
4546
  declare const openaiLLM: OpenAILLM;
2273
4547
 
2274
4548
  /**
2275
- * Query Cache - Stores query results with configurable TTL
2276
- * Used to avoid re-executing queries that were already validated
4549
+ * Query Cache — Two mechanisms:
4550
+ *
4551
+ * 1. `cache` (query string → result data) — TTL-based with max size, for avoiding re-execution
4552
+ * of recently validated queries. True LRU eviction: reads bubble entries to the back via
4553
+ * delete+re-set so the oldest *unused* entry is evicted, not the oldest *inserted*.
4554
+ *
4555
+ * 2. Encrypted queryId tokens — SQL is encrypted into the queryId itself (self-contained).
4556
+ * No server-side storage needed for SQL mappings. The token is decrypted on each request.
4557
+ * This eliminates the unbounded queryIdCache that previously grew forever and caused
4558
+ * memory bloat (hundreds of MBs after thousands of queries).
4559
+ *
4560
+ * Result data can still be cached temporarily via the data cache (mechanism 1).
2277
4561
  */
2278
4562
  declare class QueryCache {
2279
4563
  private cache;
2280
4564
  private ttlMs;
4565
+ private maxCacheSize;
2281
4566
  private cleanupInterval;
4567
+ private readonly algorithm;
4568
+ private encryptionKey;
2282
4569
  constructor();
2283
4570
  /**
2284
4571
  * Set the cache TTL (Time To Live)
2285
- * @param minutes - TTL in minutes (default: 5)
4572
+ * @param minutes - TTL in minutes (default: 10)
2286
4573
  */
2287
4574
  setTTL(minutes: number): void;
2288
4575
  /**
@@ -2290,12 +4577,25 @@ declare class QueryCache {
2290
4577
  */
2291
4578
  getTTL(): number;
2292
4579
  /**
2293
- * Store query result in cache
2294
- * Key is the exact query string (or JSON for parameterized queries)
4580
+ * Which access bucket this call reads and writes.
4581
+ *
4582
+ * `''` means the unrestricted bucket and produces a key identical to the one
4583
+ * used before authorization existed. `null` means do not touch the cache.
4584
+ */
4585
+ private scope;
4586
+ /** The stored key: the scope namespace, then the caller's key. NUL cannot occur in SQL. */
4587
+ private scopedKey;
4588
+ /**
4589
+ * Store query result in data cache.
4590
+ * If the key already exists, it's removed first so the re-insert places it
4591
+ * at the back of the iteration order (LRU). Eviction only fires when adding
4592
+ * a genuinely new key past the size limit.
2295
4593
  */
2296
4594
  set(query: string, data: any): void;
2297
4595
  /**
2298
- * Get cached result if exists and not expired
4596
+ * Get cached result if exists and not expired.
4597
+ * On hit, re-inserts the entry so it moves to the back of the Map's
4598
+ * iteration order — turning FIFO eviction into true LRU.
2299
4599
  */
2300
4600
  get(query: string): any | null;
2301
4601
  /**
@@ -2315,12 +4615,39 @@ declare class QueryCache {
2315
4615
  */
2316
4616
  getStats(): {
2317
4617
  size: number;
4618
+ queryIdCount: number;
2318
4619
  oldestEntryAge: number | null;
2319
4620
  };
2320
4621
  /**
2321
- * Start periodic cleanup of expired entries
4622
+ * Start periodic cleanup of expired data cache entries.
2322
4623
  */
2323
4624
  private startCleanup;
4625
+ /**
4626
+ * Encrypt a payload into a self-contained token.
4627
+ */
4628
+ private encrypt;
4629
+ /**
4630
+ * Decrypt a token back to the original payload.
4631
+ */
4632
+ private decrypt;
4633
+ /**
4634
+ * Store a query by generating an encrypted token as queryId.
4635
+ * The SQL is encrypted INTO the token — nothing stored in memory.
4636
+ * If data is provided, it's cached temporarily in the data cache.
4637
+ */
4638
+ storeQuery(query: any, data?: any): string;
4639
+ /**
4640
+ * Get a stored query by decrypting its token.
4641
+ * Returns the SQL + any cached result data.
4642
+ */
4643
+ getQuery(queryId: string): {
4644
+ query: any;
4645
+ data: any;
4646
+ } | null;
4647
+ /**
4648
+ * Update cached data for a queryId token
4649
+ */
4650
+ setQueryData(queryId: string, data: any): void;
2324
4651
  /**
2325
4652
  * Stop cleanup interval (for graceful shutdown)
2326
4653
  */
@@ -2328,23 +4655,1316 @@ declare class QueryCache {
2328
4655
  }
2329
4656
  declare const queryCache: QueryCache;
2330
4657
 
2331
- declare const SDK_VERSION = "0.0.8";
2332
- type MessageTypeHandler = (message: IncomingMessage) => void | Promise<void>;
2333
- declare class SuperatomSDK {
2334
- private ws;
2335
- private url;
2336
- private apiKey?;
2337
- private projectId;
2338
- private type;
2339
- private bundleDir;
2340
- private messageHandlers;
2341
- private messageTypeHandlers;
2342
- private connected;
2343
- private reconnectAttempts;
2344
- private maxReconnectAttempts;
2345
- private collections;
2346
- private components;
2347
- private tools;
4658
+ /**
4659
+ * Request context — who is asking, carried to wherever it is needed.
4660
+ *
4661
+ * The data seam (`BaseSource.prepareSql()` in the backend) has to know which
4662
+ * user a query is for, so the SQL rewriter has a policy to inject. Identity
4663
+ * arrives here, in the SDK's message handlers; the seam is several layers and
4664
+ * one package away. This module is the carrier for the paths where the async
4665
+ * chain stays intact.
4666
+ *
4667
+ * Design and the paths where it does NOT reach:
4668
+ * backend/docs/REQUEST-CONTEXT-DESIGN.md
4669
+ *
4670
+ * Two rules the rest of the system depends on:
4671
+ *
4672
+ * 1. **Absence is not `system`.** No context means we do not know who is
4673
+ * asking, which is a different thing from knowing this is a trusted job.
4674
+ * Conflating them turns every call site anyone forgets into an unpoliced
4675
+ * one. Trust has to be written down, by name, via `runAsSystem()`.
4676
+ *
4677
+ * 2. **This is not a security boundary on its own.** It carries whatever it
4678
+ * is given. It is only worth something once the value it carries comes
4679
+ * from a verified identity rather than the request payload — see the
4680
+ * Prerequisite section of SQL-AUTHORIZATION-DESIGN.md.
4681
+ */
4682
+ /**
4683
+ * Who a unit of work is running on behalf of.
4684
+ *
4685
+ * `system` requires a reason because it appears in the audit log and because
4686
+ * an unexplained escalation is indistinguishable from a bug.
4687
+ */
4688
+ type Principal = {
4689
+ kind: 'user';
4690
+ userId: string;
4691
+ orgId?: string;
4692
+ role?: string;
4693
+ } | {
4694
+ kind: 'system';
4695
+ reason: string;
4696
+ };
4697
+ /**
4698
+ * One row restriction, in the shape the rewriter's inject_policies() consumes
4699
+ * directly — no translation layer, deliberately: a mapping step between what an
4700
+ * admin writes and what gets injected is somewhere for the two to drift apart.
4701
+ */
4702
+ interface RowFilter {
4703
+ /** Unqualified matches on table name alone; qualified ("dw.FactSales") must match exactly. */
4704
+ table?: string;
4705
+ column: string;
4706
+ op?: 'in' | 'not_in' | 'eq' | 'ne';
4707
+ values: unknown[];
4708
+ /**
4709
+ * How to SAY this restriction to an end user, e.g. "Hyderabad branch".
4710
+ *
4711
+ * Without it the agent has only the predicate, and pastes
4712
+ * `com_trn_documentstamp.LocationId IN (110)` into the answer — accurate and
4713
+ * meaningless to the person reading it. Only whoever writes the policy knows
4714
+ * that 110 is Hyderabad, so only they can supply this.
4715
+ */
4716
+ label?: string;
4717
+ }
4718
+ /**
4719
+ * What one user may see in one source. `sourceId` is not optional in spirit: a
4720
+ * filter naming a column of one database must never be injected into another's
4721
+ * SQL. Omitting it means "every source", which is only correct for a column that
4722
+ * genuinely exists everywhere.
4723
+ */
4724
+ interface SourcePolicy {
4725
+ sourceId?: string | null;
4726
+ /** Default {@link RowFilter.label} for every filter in this policy that lacks its own. */
4727
+ label?: string;
4728
+ rowFilters?: RowFilter[];
4729
+ /** Gate 3. Empty means no table restriction — see SQL-AUTHORIZATION-DESIGN.md. */
4730
+ readableTables?: string[];
4731
+ }
4732
+ interface RequestContext {
4733
+ principal: Principal;
4734
+ projectId?: string;
4735
+ /**
4736
+ * Access policies resolved ONCE per request, not per query. Empty means
4737
+ * "no restrictions apply". Narrowed to the source being queried at the seam
4738
+ * — see the backend's policy-gate.
4739
+ */
4740
+ policies?: SourcePolicy[];
4741
+ /** Correlates the audit log with a chat turn. */
4742
+ requestId?: string;
4743
+ }
4744
+ /** The context for the current async chain, or `undefined` if none was established. */
4745
+ declare function getRequestContext(): RequestContext | undefined;
4746
+ /**
4747
+ * Like `getRequestContext()`, but throws when there is none.
4748
+ *
4749
+ * For call sites that must not proceed anonymously. Prefer this over
4750
+ * `getRequestContext() ?? somethingPermissive` — the fallback is exactly the
4751
+ * silent bypass this module exists to prevent.
4752
+ */
4753
+ declare function requireRequestContext(what?: string): RequestContext;
4754
+ declare class MissingRequestContextError extends Error {
4755
+ constructor(message: string);
4756
+ }
4757
+ /** Run `fn` with `ctx` visible to everything it awaits. */
4758
+ declare function runWithContext<T>(ctx: RequestContext, fn: () => T): T;
4759
+ /** Convenience for the common case: a request on behalf of a signed-in user. */
4760
+ declare function runAsUser<T>(user: {
4761
+ userId: string;
4762
+ orgId?: string;
4763
+ role?: string;
4764
+ projectId?: string;
4765
+ requestId?: string;
4766
+ policies?: SourcePolicy[];
4767
+ }, fn: () => T): T;
4768
+ /**
4769
+ * Declare a unit of work as trusted, naming why.
4770
+ *
4771
+ * For entry points with no user behind them: schedulers, admin tooling, the
4772
+ * cross-machine proxy. `reason` is not decoration — it is what the audit log
4773
+ * shows in place of a user, and it is what makes these paths greppable when
4774
+ * someone asks "what runs unpoliced?".
4775
+ *
4776
+ * Nesting inside a user context deliberately REPLACES it: a system subtask is
4777
+ * an escalation, and it should look like one in the log rather than silently
4778
+ * inheriting a user's restrictions.
4779
+ */
4780
+ declare function runAsSystem<T>(reason: string, fn: () => T, opts?: {
4781
+ projectId?: string;
4782
+ requestId?: string;
4783
+ }): T;
4784
+ /**
4785
+ * Capture the current context so it can be re-entered later.
4786
+ *
4787
+ * For boundaries where the async chain breaks but a reference survives — an
4788
+ * event listener registered outside the request, a callback stored in a
4789
+ * registry. Does NOT help across a process or HTTP boundary (the script
4790
+ * bridge); that needs a token, see the design doc.
4791
+ */
4792
+ declare function captureContext(): RequestContext | undefined;
4793
+ /** Re-enter a captured context. A no-op wrapper when `ctx` is undefined. */
4794
+ declare function withCapturedContext<T>(ctx: RequestContext | undefined, fn: () => T): T;
4795
+ /**
4796
+ * Bind a function to the context active at bind time.
4797
+ *
4798
+ * The trap this exists for, stated the right way round (verified, because it
4799
+ * is easy to get backwards): context propagates through ASYNC RESOURCE
4800
+ * creation, and `EventEmitter.emit` is an ordinary synchronous call. So a
4801
+ * plain listener runs in the context active at EMIT time, whatever it was
4802
+ * registered inside. A handler registered during a request and fired later
4803
+ * from another chain therefore sees nothing at all.
4804
+ *
4805
+ * Bind it when the registering context is the one that matters.
4806
+ */
4807
+ declare function bindContext<A extends unknown[], R>(fn: (...args: A) => R): (...args: A) => R;
4808
+ /** Short label for logs and audit rows: `user:u_123` / `system:alert-scheduler` / `anonymous`. */
4809
+ declare function describeContext(ctx?: RequestContext | undefined): string;
4810
+ /** True when the context represents a signed-in user rather than a trusted job. */
4811
+ declare function isUserContext(ctx?: RequestContext | undefined): boolean;
4812
+
4813
+ /**
4814
+ * Deriving "who is asking" from an incoming WebSocket message.
4815
+ *
4816
+ * Separate from `utils/request-context.ts` on purpose: that module is a pure
4817
+ * carrier with no opinion about where identity comes from, this one knows the
4818
+ * message shape and the trust level of each field.
4819
+ *
4820
+ * See backend/docs/REQUEST-CONTEXT-DESIGN.md and the Prerequisite section of
4821
+ * backend/docs/SQL-AUTHORIZATION-DESIGN.md.
4822
+ */
4823
+
4824
+ /**
4825
+ * Read access policies out of the admin-authored per-user config.
4826
+ *
4827
+ * The config is free-form JSON typed by a human into an admin field, so every
4828
+ * step here is defensive: a malformed entry is DROPPED, never guessed at. That
4829
+ * is a deliberate trade-off with a sharp edge — a typo'd policy silently
4830
+ * becomes no policy, i.e. unrestricted. Validating shapes against the source
4831
+ * schema (and refusing, rather than dropping) is the fix, and is not built yet.
4832
+ *
4833
+ * Accepts either a single object or an array, so one user can be scoped across
4834
+ * several sources.
4835
+ */
4836
+ declare function policiesFromConfig(config: unknown): SourcePolicy[];
4837
+ /**
4838
+ * Run `fn` with the caller's identity established.
4839
+ *
4840
+ * Trust order:
4841
+ *
4842
+ * 1. `message.authContext` — stamped by the relay from a verified session.
4843
+ * Trustworthy. Does not exist yet.
4844
+ * 2. `payload.userId` — sent by the browser, which can put anything there.
4845
+ * Enough to prove the plumbing works end to end; NOT enough to enforce
4846
+ * anything on. Used because it is what exists today.
4847
+ * 3. Neither — no context is established at all.
4848
+ *
4849
+ * Case 3 is deliberately not a `system` context. "We don't know who this is"
4850
+ * and "this is a trusted job" must stay distinguishable, or every path that
4851
+ * loses identity silently becomes an unpoliced one. With no context, the data
4852
+ * seam decides what to do (see SA_SQL_POLICY_ENFORCE), and the honest answer
4853
+ * there is a refusal rather than an unfiltered read.
4854
+ */
4855
+ declare function runWithMessageIdentity<T>(message: any, fn: () => T): T;
4856
+
4857
+ /**
4858
+ * Context tokens — carrying identity across a boundary AsyncLocalStorage cannot.
4859
+ *
4860
+ * The script flow is the one path where the async chain genuinely breaks:
4861
+ * scripts run in a separate OS process and call back into the backend over
4862
+ * loopback HTTP (`script-bootstrap.ts` → `script-bridge.ts`), so the bridge's
4863
+ * request handler starts a fresh async context with no relationship to the
4864
+ * chat turn that launched the script. Nothing ambient crosses that; the
4865
+ * identity has to be handed over explicitly.
4866
+ *
4867
+ * So: the parent mints a token bound to the live context before spawning, the
4868
+ * child presents it on every bridge call, and the bridge resolves it back and
4869
+ * runs the query inside that context.
4870
+ *
4871
+ * The registry is in-memory and process-local, which is exactly right here —
4872
+ * the minting side (script-runner) and the resolving side (script-bridge) are
4873
+ * both in the backend process; only the script itself is elsewhere. A token is
4874
+ * therefore never valid anywhere but the process that issued it, and never
4875
+ * outlives that process.
4876
+ *
4877
+ * See backend/docs/REQUEST-CONTEXT-DESIGN.md.
4878
+ */
4879
+
4880
+ /**
4881
+ * Mint a token for the context active right now.
4882
+ *
4883
+ * Returns `undefined` when there is no context — deliberately, so that an
4884
+ * unidentified script run stays unidentified rather than being handed a token
4885
+ * that vouches for nothing. The bridge then sees no token and the data seam
4886
+ * treats it as anonymous, which is the truth.
4887
+ */
4888
+ declare function mintContextToken(ttlMs?: number): string | undefined;
4889
+ /** Resolve a token back to its context, or `undefined` if unknown or expired. */
4890
+ declare function resolveContextToken(token: string | undefined | null): RequestContext | undefined;
4891
+ /** Drop a token. Call when the work it was minted for is finished. */
4892
+ declare function revokeContextToken(token: string | undefined | null): void;
4893
+ /** Diagnostics only. */
4894
+ declare function contextTokenCount(): number;
4895
+ /** Test seam. */
4896
+ declare function clearContextTokens(): void;
4897
+
4898
+ /**
4899
+ * Manages conversation history scoped per user + dashboard.
4900
+ * Each user-dashboard pair has its own isolated history that expires after a configurable TTL.
4901
+ */
4902
+ declare class DashboardConversationHistory {
4903
+ private histories;
4904
+ private ttlMs;
4905
+ private maxEntries;
4906
+ private cleanupInterval;
4907
+ constructor();
4908
+ /**
4909
+ * Set the TTL for dashboard histories
4910
+ * @param minutes - TTL in minutes
4911
+ */
4912
+ setTTL(minutes: number): void;
4913
+ /**
4914
+ * Set max entries per dashboard
4915
+ */
4916
+ setMaxEntries(max: number): void;
4917
+ /**
4918
+ * Add a conversation entry for a user's dashboard
4919
+ */
4920
+ addEntry(dashboardId: string, userPrompt: string, componentSummary: string, userId?: string): void;
4921
+ /**
4922
+ * Get formatted conversation history for a user's dashboard
4923
+ */
4924
+ getHistory(dashboardId: string, userId?: string): string;
4925
+ /**
4926
+ * Clear history for a specific user's dashboard
4927
+ */
4928
+ clearDashboard(dashboardId: string, userId?: string): void;
4929
+ /**
4930
+ * Clear all dashboard histories
4931
+ */
4932
+ clearAll(): void;
4933
+ /**
4934
+ * Start periodic cleanup of expired histories
4935
+ */
4936
+ private startCleanup;
4937
+ /**
4938
+ * Stop cleanup interval (for graceful shutdown)
4939
+ */
4940
+ destroy(): void;
4941
+ }
4942
+ declare const dashboardConversationHistory: DashboardConversationHistory;
4943
+
4944
+ /**
4945
+ * Whole-dashboard generation via Pi, a terminal coding agent — as opposed to
4946
+ * DASH_COMP_REQ's single-widget-at-a-time flow. Runs Pi in-process via its
4947
+ * SDK (createAgentSession), not as a subprocess: no shell, no argument
4948
+ * quoting, no stdin/stdout piping, none of the Windows-specific subprocess
4949
+ * issues that came with spawning the `pi` CLI directly.
4950
+ *
4951
+ * Called from sdk-nodejs/src/dashboardAgent/index.ts (DASHBOARD_AGENT_REQ),
4952
+ * which owns the generic streaming/abort machinery (mirrors USER_PROMPT_REQ)
4953
+ * and passes `signal`/`onProgress` alongside the normal params — this stays
4954
+ * within CollectionHandler's loose (params) => Promise<result> typing, no
4955
+ * change needed to that shared type.
4956
+ *
4957
+ * This mechanism is generic and reusable across any deployment. What's
4958
+ * genuinely project-specific — where AGENTS.md lives, which model to use —
4959
+ * is supplied via `DashboardAgentCollectionConfig`, with defaults sensible
4960
+ * enough that most callers don't need to override them (see below). The
4961
+ * data-source tool list and the dashboard's current state both come from
4962
+ * things sdk-nodejs already exposes generically: `sdk.getTools()` (whatever
4963
+ * this deployment registered via `sdk.setTools()`) and `sdk.callCollection
4964
+ * ('dashboards', 'query', ...)` (whatever this deployment already registered
4965
+ * under that name/shape) — no per-deployment callback needed for either.
4966
+ * Same convention on the way out: after a successful run, the prompt and the
4967
+ * full response text are handed to `sdk.callCollection('dashboard-agent-
4968
+ * conversations', 'create', ...)` if this deployment has registered one —
4969
+ * skipped silently otherwise, since conversation history is optional.
4970
+ *
4971
+ * Pi verifies every query against the live database itself (via whatever
4972
+ * local tool-execution bridge the deployment exposes, e.g. an HTTP bridge
4973
+ * on localhost), but does NOT persist the result itself — it writes the
4974
+ * finished DSL to an absolute path inside `runtimeDir`, told to it explicitly
4975
+ * in the prompt, and stops there. This handler reads that file after the run
4976
+ * finishes and returns its content as `dashboard` in the result. The caller
4977
+ * (frontend) is the one that actually saves it, via whatever authenticated
4978
+ * create/update path any other dashboard edit goes through — Pi has no user
4979
+ * session/auth context of its own, so persistence shouldn't happen from
4980
+ * inside it.
4981
+ *
4982
+ * Session persistence: the FIRST call for a dashboardId pays the full cost
4983
+ * (explore KB, discover schema, plan, verify, build). Every call after that
4984
+ * resumes the same session file (SessionManager.open) so Pi has everything
4985
+ * it already learned — it only needs to reason about the new, smaller ask,
4986
+ * not rediscover the whole dashboard from scratch. The session's file path
4987
+ * (AgentSession.sessionFile) is captured right after creation and persisted
4988
+ * in a small local file, keyed by dashboardId, inside `runtimeDir`.
4989
+ */
4990
+ interface DashboardAgentCollectionConfig {
4991
+ /**
4992
+ * Working directory Pi runs from — must contain AGENTS.md. This is a
4993
+ * version-controlled prompt file, so `cwd` is expected to live somewhere
4994
+ * like a `.prompts/` folder alongside the deployment's other prompts.
4995
+ * Default: `<process.cwd()>/.prompts/dashboard-agent` — the same
4996
+ * process.cwd()-based convention PromptLoader already uses for the main
4997
+ * agent's prompts, which needs no explicit override in the common case
4998
+ * (the backend process's own cwd already is its project root).
4999
+ */
5000
+ cwd?: string;
5001
+ /**
5002
+ * Where drafts/, the session-id map, and dashboard.log get written —
5003
+ * separate from `cwd` deliberately, so this deployment's runtime state
5004
+ * (regenerated per session, safe to gitignore) doesn't sit inside the
5005
+ * same folder as the version-controlled AGENTS.md prompt.
5006
+ * Default: `<process.cwd()>/.pi-dashboard-agent-runtime`.
5007
+ */
5008
+ runtimeDir?: string;
5009
+ /** Model provider (default: process.env.PI_AGENT_PROVIDER || 'openrouter'). */
5010
+ provider?: string;
5011
+ /** Model id (default: process.env.PI_AGENT_MODEL || 'anthropic/claude-sonnet-4.5'). */
5012
+ model?: string;
5013
+ /**
5014
+ * true (default): every request starts a brand-new pi session, with the 2
5015
+ * most recent prior responses (if any) injected into the prompt as
5016
+ * context — bounded cost per request, but pi re-explores schema/KB facts
5017
+ * it already verified in an earlier turn on this same dashboard.
5018
+ * false: resumes the same session file across requests on a given
5019
+ * dashboard — pi keeps everything it already learned, but context (and
5020
+ * cost) grows unbounded across turns (one observed turn: 2M+ cache-read
5021
+ * tokens after a handful of edits on the same dashboard).
5022
+ * Default: process.env.PI_AGENT_FRESH_SESSION !== 'false'.
5023
+ */
5024
+ freshSession?: boolean;
5025
+ }
5026
+ declare function registerDashboardAgentCollection(sdk: SuperatomSDK, config?: DashboardAgentCollectionConfig): void;
5027
+
5028
+ /**
5029
+ * Coding-agent driver contract — one shape over every harness (codex today;
5030
+ * claude-code / opencode / pi next). Callers depend on this file only, never on
5031
+ * a harness SDK, so swapping the agent behind a task is a config change.
5032
+ *
5033
+ * Deliberately narrower than the harness SDKs it wraps: the three things a
5034
+ * production run needs and that harness abstractions usually omit — real abort,
5035
+ * real token accounting, and structured tool args — are part of the contract,
5036
+ * not per-driver extras.
5037
+ */
5038
+ type AgentEventKind = 'turn' | 'message' | 'reasoning' | 'command' | 'tool' | 'file' | 'todo' | 'error';
5039
+ interface AgentEvent {
5040
+ kind: AgentEventKind;
5041
+ /** Stable across started→updated→completed, so a UI updates one block in place. */
5042
+ id?: string;
5043
+ text?: string;
5044
+ command?: string;
5045
+ output?: string;
5046
+ /** Tool calls carry real parsed args — never SQL scraped back out of a shell string. */
5047
+ tool?: {
5048
+ server?: string;
5049
+ name: string;
5050
+ args?: unknown;
5051
+ };
5052
+ status?: 'in_progress' | 'completed' | 'failed';
5053
+ done?: boolean;
5054
+ }
5055
+ interface AgentUsage {
5056
+ inputTokens: number;
5057
+ outputTokens: number;
5058
+ reasoningTokens: number;
5059
+ cacheReadTokens: number;
5060
+ cacheWriteTokens: number;
5061
+ /** Tool + command invocations. With tokens, separates "silently did nothing" from a real run. */
5062
+ toolCalls: number;
5063
+ /** Only when the harness reports it — codex reports tokens but not cost. */
5064
+ costUSD?: number;
5065
+ }
5066
+ interface AgentRunHandlers {
5067
+ /** Formatted live stream: assistant text deltas plus one-line tool/command markers. */
5068
+ onOutput?: (chunk: string) => void;
5069
+ onEvent?: (event: AgentEvent) => void;
5070
+ /** Assistant prose only — safe to show a non-technical user as progress. */
5071
+ onNarration?: (text: string) => void;
5072
+ onUsage?: (usage: AgentUsage) => void;
5073
+ /**
5074
+ * Polled once the turn is live. When it returns true the run ends immediately —
5075
+ * for tasks whose deliverable is a file, the moment it exists there is nothing
5076
+ * left to wait for.
5077
+ */
5078
+ doneWhen?: () => boolean | Promise<boolean>;
5079
+ }
5080
+ type AgentRunStatus = 'completed' | 'early' | 'aborted' | 'error';
5081
+ interface AgentRunResult {
5082
+ status: AgentRunStatus;
5083
+ /** The final assistant message. */
5084
+ answer: string;
5085
+ ms: number;
5086
+ usage: AgentUsage;
5087
+ /**
5088
+ * Set when status is 'error'. Covers stream-reported failures too, which arrive
5089
+ * as events rather than thrown exceptions — a swallowed provider error is the
5090
+ * failure mode that otherwise looks like a successful empty run.
5091
+ */
5092
+ error?: string;
5093
+ sessionId?: string;
5094
+ }
5095
+ interface McpServerSpec {
5096
+ command: string;
5097
+ args?: string[];
5098
+ env?: Record<string, string>;
5099
+ }
5100
+ interface CodingAgentSessionOptions {
5101
+ /** Working directory the agent operates in. Scope it to a runtime dir, not a source checkout. */
5102
+ cwd: string;
5103
+ model?: string;
5104
+ /** Resume a prior conversation — the value a previous run returned as sessionId. */
5105
+ resumeId?: string;
5106
+ /** Tools, as MCP servers. Keyed by server name, which shows up on every tool event. */
5107
+ mcpServers?: Record<string, McpServerSpec>;
5108
+ /**
5109
+ * Extra env for the agent process. By default only a small allowlist is passed
5110
+ * through (see ENV_ALLOWLIST) so an agent with shell access can't read the
5111
+ * backend's own secrets out of its environment.
5112
+ */
5113
+ env?: Record<string, string>;
5114
+ /** Opt out of env scrubbing. Only for local debugging. */
5115
+ inheritEnv?: boolean;
5116
+ /**
5117
+ * Workspace file holding the standing instructions. The driver must make its
5118
+ * harness read it: codex only finds AGENTS.md by itself.
5119
+ */
5120
+ instructionsFile?: string;
5121
+ }
5122
+ interface CodingAgentSession {
5123
+ readonly harness: string;
5124
+ /** Structured event log vs. a raw terminal stream — the UI picks its renderer from this. */
5125
+ readonly kind: 'events' | 'pty';
5126
+ run(prompt: string, handlers?: AgentRunHandlers, signal?: AbortSignal): Promise<AgentRunResult>;
5127
+ /** Rolling structured events, for replay when a client reconnects mid-run. */
5128
+ events(): AgentEvent[];
5129
+ /** Rolling formatted text, same purpose as events() for text-oriented views. */
5130
+ buffer(): string;
5131
+ busy(): boolean;
5132
+ /** Persist this and pass it back as resumeId to continue the conversation. */
5133
+ sessionId(): string | undefined;
5134
+ /** Abandon the conversation; the next run starts fresh. */
5135
+ reset(): void;
5136
+ dispose(): void;
5137
+ }
5138
+
5139
+ /**
5140
+ * Harness registry. Kept separate from index.ts so collection.ts can reach the
5141
+ * factory without importing the module's own barrel (a cycle tsup would have to
5142
+ * resolve at bundle time).
5143
+ */
5144
+ type Harness = 'codex';
5145
+ declare function createCodingAgentSession(harness: Harness, opts: CodingAgentSessionOptions): CodingAgentSession;
5146
+
5147
+ /**
5148
+ * Codex driver — OpenAI Codex via @openai/codex-sdk.
5149
+ *
5150
+ * FORMAT CONTRACT: written against @openai/codex-sdk 0.149.1. Harness event
5151
+ * shapes drift; if the event log goes blank after an SDK bump, check normEvent
5152
+ * against the package's dist/index.d.ts first.
5153
+ *
5154
+ * Three things this leans on that the SDK gives natively, so we don't reinvent
5155
+ * them: TurnOptions.signal (real cancellation), TurnCompletedEvent.usage (token
5156
+ * accounting), and mcp_tool_call items (tool args arrive parsed, not scraped
5157
+ * out of a shell command string).
5158
+ *
5159
+ * The package is an optionalDependency: every deployment gets it, but an
5160
+ * install that cannot fetch the ~309MB platform binary skips it rather than
5161
+ * failing. It is therefore loaded dynamically and its types mirrored
5162
+ * structurally below, so a VM without it still builds, starts, and runs
5163
+ * everything else — only the coding agent reports itself unavailable.
5164
+ */
5165
+ type CodexSandboxMode = 'read-only' | 'workspace-write' | 'danger-full-access';
5166
+ type CodexReasoningEffort = 'minimal' | 'low' | 'medium' | 'high' | 'xhigh' | 'max' | 'ultra';
5167
+ interface CodexSessionOptions extends CodingAgentSessionOptions {
5168
+ reasoningEffort?: CodexReasoningEffort;
5169
+ /**
5170
+ * Filesystem reach. Default 'workspace-write' — the agent writes its
5171
+ * deliverable into cwd and nothing else. 'danger-full-access' hands a
5172
+ * shell-capable agent the whole box; don't use it on a client VM.
5173
+ */
5174
+ sandboxMode?: CodexSandboxMode;
5175
+ /** Network for commands the agent runs. Off by default; MCP tools don't need it. */
5176
+ networkAccessEnabled?: boolean;
5177
+ apiKey?: string;
5178
+ baseUrl?: string;
5179
+ /** Raw `--config` overrides merged into the codex config. */
5180
+ extraConfig?: Record<string, unknown>;
5181
+ approvalPolicy?: 'never' | 'on-request' | 'on-failure' | 'untrusted';
5182
+ }
5183
+ declare function createCodexSession(opts: CodexSessionOptions): CodingAgentSession;
5184
+
5185
+ /**
5186
+ * What makes an agent fire.
5187
+ *
5188
+ * Declared ON the definition, deliberately. If triggers lived at the call sites
5189
+ * instead — a counter in the conversation handler, a hook in the kb-nodes
5190
+ * collection — then adding an agent would mean editing unrelated files across
5191
+ * the backend, and no one file would tell you when an agent actually runs.
5192
+ * Here, an agent's folder plus its definition is the whole story.
5193
+ *
5194
+ * The deployment's only job is to announce that something happened:
5195
+ *
5196
+ * emitAgentEvent('conversation.answered', { threadId })
5197
+ * emitAgentEvent('kb-node.changed', { id, op: 'update' })
5198
+ *
5199
+ * Which agents care, and how many events it takes, is the agents' business.
5200
+ * The emitter therefore speaks the WORLD's vocabulary, never any one agent's —
5201
+ * translating an event into a particular agent's input is `buildInput` below.
5202
+ */
5203
+ /** One announced fact, as it reaches a trigger. */
5204
+ interface TriggerEvent {
5205
+ /** The event name passed to emitAgentEvent. */
5206
+ name: string;
5207
+ payload?: unknown;
5208
+ }
5209
+ /**
5210
+ * Turns the events that caused this run into the agent's `input`.
5211
+ *
5212
+ * Takes the whole list, not one payload, because every batching trigger here
5213
+ * fires for MANY events: a debounced burst, or N counted occurrences. Handing
5214
+ * over only the last one silently discarded the rest — five edited nodes became
5215
+ * one.
5216
+ *
5217
+ * Omit it and the agent receives the most recent payload unchanged, which is
5218
+ * the historical behaviour. Any agent that batches should declare one.
5219
+ */
5220
+ type BuildTriggerInput = (events: TriggerEvent[]) => unknown;
5221
+ type AgentTrigger =
5222
+ /** Only runCodingAgent(). The default. */
5223
+ {
5224
+ type: 'manual';
5225
+ }
5226
+ /** Every N milliseconds while the process is up. Fires with no events. */
5227
+ | {
5228
+ type: 'interval';
5229
+ everyMs: number;
5230
+ buildInput?: BuildTriggerInput;
5231
+ }
5232
+ /**
5233
+ * On a named event. `debounceMs` collapses a burst into one run — editing
5234
+ * five knowledge nodes in a row is one reason to rebuild, not five. The run
5235
+ * still receives all five events; only the RUNS are collapsed.
5236
+ */
5237
+ | {
5238
+ type: 'event';
5239
+ on: string | string[];
5240
+ debounceMs?: number;
5241
+ buildInput?: BuildTriggerInput;
5242
+ }
5243
+ /**
5244
+ * Every Nth occurrence of an event. The events are PERSISTED, so "after 10
5245
+ * questions" survives a restart instead of silently starting over.
5246
+ */
5247
+ | {
5248
+ type: 'count';
5249
+ on: string;
5250
+ every: number;
5251
+ buildInput?: BuildTriggerInput;
5252
+ };
5253
+ interface TriggerFireInfo {
5254
+ agentId: string;
5255
+ /** What set it off, for the log line. */
5256
+ reason: string;
5257
+ /** Everything that caused this run, in arrival order. */
5258
+ events: TriggerEvent[];
5259
+ /** What buildInput produced — exactly what the agent was handed. */
5260
+ input?: unknown;
5261
+ }
5262
+ /** Notified after every triggered run — for recording history or alerting. */
5263
+ type RunListener = (record: AgentRunRecord, info: TriggerFireInfo) => void;
5264
+ declare function onAgentRun(fn: RunListener): () => void;
5265
+ /**
5266
+ * Tell the agents something happened. Safe to call from anywhere, including a
5267
+ * request path — it never throws and never blocks: matching runs are started,
5268
+ * not awaited.
5269
+ */
5270
+ declare function emitAgentEvent(event: string, payload?: unknown): void;
5271
+ /**
5272
+ * Start interval triggers. Call once at startup, after registerCodingAgents.
5273
+ * Event and count triggers need nothing started — they wake on emitAgentEvent.
5274
+ */
5275
+ declare function startAgentTriggers(): void;
5276
+ declare function stopAgentTriggers(): void;
5277
+ /** What fires each agent — for a startup log or an admin screen. */
5278
+ declare function describeTriggers(): Array<{
5279
+ agentId: string;
5280
+ triggers: string[];
5281
+ }>;
5282
+
5283
+ /**
5284
+ * What a coding agent IS.
5285
+ *
5286
+ * An agent is a job description, not code that does the job: instructions, the
5287
+ * inputs it should be handed, the tools it may reach, the deliverable it must
5288
+ * produce, and what we do with that deliverable. The agent itself decides HOW —
5289
+ * it can read the inputs, write and run scripts over them, check its own work,
5290
+ * and iterate. That is the whole reason for using a coding agent rather than a
5291
+ * single LLM call: the job's data never has to fit in a context window.
5292
+ *
5293
+ * Adding an agent is a folder with a system.md, one file implementing this
5294
+ * interface, and one line in the registry. Nothing else changes.
5295
+ */
5296
+ /** The disposable directory one run happens in. */
5297
+ interface Workspace {
5298
+ /** Absolute path the agent runs in. Everything below is relative to it. */
5299
+ dir: string;
5300
+ /** Unique per run — also the folder name, so runs never collide. */
5301
+ runId: string;
5302
+ /** Write a file into the workspace, creating parent directories. */
5303
+ write(relPath: string, contents: string): Promise<void>;
5304
+ /** Write JSON (pretty-printed — the agent reads these by eye as often as by script). */
5305
+ writeJson(relPath: string, value: unknown): Promise<void>;
5306
+ /** Write one JSON object per line. The right shape for anything the agent will stream or grep. */
5307
+ writeJsonl(relPath: string, rows: unknown[]): Promise<void>;
5308
+ /** Read a file the agent produced. Returns null when it isn't there. */
5309
+ read(relPath: string): Promise<string | null>;
5310
+ /** Absolute path for a workspace-relative path — for telling the agent where to write. */
5311
+ pathTo(relPath: string): string;
5312
+ }
5313
+ /** Minimal validator shape, so this file needn't depend on a schema library. */
5314
+ interface OutputValidator<T> {
5315
+ /** Throw with a useful message, or return the parsed value. Zod's `.parse` satisfies this. */
5316
+ parse(value: unknown): T;
5317
+ }
5318
+ interface OutputContract<T> {
5319
+ /**
5320
+ * Workspace-relative path the agent must write, e.g. 'out/concepts.json'.
5321
+ * Nothing else counts as the deliverable: the run ends the moment this
5322
+ * appears, and a run that finishes without it is a failure, not a partial
5323
+ * success.
5324
+ */
5325
+ file: string;
5326
+ /** Validated before anything is persisted, so a malformed run fails loudly and applies nothing. */
5327
+ schema: OutputValidator<T>;
5328
+ }
5329
+ /** What apply() reports back — surfaced in the run record and to the caller. */
5330
+ interface ApplySummary {
5331
+ /** One line a human can read in a log or the UI. */
5332
+ summary: string;
5333
+ /** Whatever counts for this agent: {created, updated, skipped}, etc. */
5334
+ counts?: Record<string, number>;
5335
+ /** Anything the caller may want to inspect or show. */
5336
+ details?: unknown;
5337
+ }
5338
+ interface AgentRunContext {
5339
+ projectId?: string;
5340
+ userId?: string;
5341
+ /**
5342
+ * This run's id — also the workspace directory name. Passed so apply() can
5343
+ * stamp whatever it persists with the run that produced it, which is the
5344
+ * only link from a stored result back to the inputs and scripts behind it.
5345
+ */
5346
+ runId?: string;
5347
+ /** The run's workspace, for an apply() that needs a file the agent left. */
5348
+ workspaceDir?: string;
5349
+ /** Cancels the run; forwarded to the driver's real abort. */
5350
+ signal?: AbortSignal;
5351
+ /** Live progress. Populated for chat mode; a task run may also stream to a log. */
5352
+ onOutput?: (chunk: string) => void;
5353
+ onEvent?: (event: AgentEvent) => void;
5354
+ /**
5355
+ * Assistant prose only — safe to show a non-technical user as progress.
5356
+ * Distinct from onOutput, which also carries command and tool lines and so
5357
+ * would leak SQL and shell text into a user-facing stream.
5358
+ */
5359
+ onNarration?: (text: string) => void;
5360
+ onUsage?: (usage: AgentUsage) => void;
5361
+ }
5362
+ /**
5363
+ * How much an agent carries between runs.
5364
+ *
5365
+ * Codex manages its own context: it tracks the window and compacts when it
5366
+ * crosses `auto_compact_token_limit`, emitting a ContextCompacted event. That
5367
+ * happens with no involvement from us, so a persistent session survives
5368
+ * indefinitely on its own. (The mechanism is in the engine but not the
5369
+ * TypeScript SDK — Thread exposes only id/run/runStreamed — which is why there
5370
+ * is nothing here to call.)
5371
+ *
5372
+ * What compaction cannot do is make the session authoritative. It summarises to
5373
+ * make room, so detail is lost by design; and ~/.codex/sessions is a directory
5374
+ * on one machine, not the database. So durable facts belong in `memory/` files
5375
+ * and in the tables the agent writes through apply(), with the session free to
5376
+ * compact itself as it likes. Memory on disk, context managed by codex.
5377
+ */
5378
+ interface SessionPolicy {
5379
+ /**
5380
+ * 'per-run' a fresh harness session each time (default).
5381
+ * 'persistent' resume the same session across runs, so the agent keeps its
5382
+ * working context. Always paired with durable files — never rely
5383
+ * on it alone.
5384
+ */
5385
+ scope?: 'per-run' | 'persistent';
5386
+ /**
5387
+ * 'per-run' a throwaway directory per run (default); pruned by keepRuns.
5388
+ * 'persistent' one directory the agent owns across runs. `memory/` survives
5389
+ * untouched; `input/` is cleared each run so a stale input can
5390
+ * never be mistaken for this run's.
5391
+ */
5392
+ workspace?: 'per-run' | 'persistent';
5393
+ /**
5394
+ * Safety valve, not the compaction mechanism — codex already compacts on its
5395
+ * own. Starts a fresh session after this many runs while keeping the
5396
+ * workspace, for when you want a hard ceiling on how much summarised history
5397
+ * an agent drags along, or a clean slate on a cadence. Omit by default.
5398
+ */
5399
+ resetAfterTurns?: number;
5400
+ }
5401
+ interface CodingAgentDefinition<TInput = unknown, TOutput = unknown> {
5402
+ /** Stable id: the folder name under .prompts/coding-agents, and the key callers use. */
5403
+ id: string;
5404
+ /** Human-readable, for the UI and logs. */
5405
+ title: string;
5406
+ /** One line: what this agent is for. */
5407
+ description?: string;
5408
+ /**
5409
+ * 'task' — headless. Runs from a cron, an event, or a command; produces the
5410
+ * deliverable and applies it. Nobody is watching.
5411
+ * 'chat' — a person is typing. Streams, keeps its session across turns, and
5412
+ * is not required to produce a deliverable at all.
5413
+ */
5414
+ mode: 'task' | 'chat';
5415
+ /**
5416
+ * Directory holding this agent's system.md. Defaults to
5417
+ * `<promptRoot>/<id>`. The runner copies it into the workspace under the
5418
+ * same name, which is what the harness reads from its working directory —
5419
+ * the source stays version-controlled, the workspace stays disposable.
5420
+ */
5421
+ promptDir?: string;
5422
+ /** Override the deployment default for this agent alone. */
5423
+ harness?: Harness;
5424
+ model?: string;
5425
+ /**
5426
+ * Let this agent's shell reach the network. Off by default.
5427
+ *
5428
+ * Needed by any agent given bridge shims (writeBridgeShims), which reach the
5429
+ * backend over localhost HTTP. Note the sandbox has no host allowlist, so
5430
+ * this grants the whole internet, not just the bridge — scope it to agents
5431
+ * that genuinely need it.
5432
+ */
5433
+ networkAccessEnabled?: boolean;
5434
+ /**
5435
+ * Filesystem reach. Defaults to the driver's 'workspace-write'.
5436
+ * 'danger-full-access' removes the sandbox entirely — it is the only mode in
5437
+ * which codex permits MCP tool calls headlessly, which is exactly why the
5438
+ * shims exist instead.
5439
+ */
5440
+ sandboxMode?: 'read-only' | 'workspace-write' | 'danger-full-access';
5441
+ /**
5442
+ * Tools this agent may reach, as MCP servers. Deliberately per-agent: an
5443
+ * agent that reads the knowledge base has no business holding a database
5444
+ * connection. Omit for an agent that only needs the files it is given.
5445
+ */
5446
+ tools?: Record<string, McpServerSpec>;
5447
+ /**
5448
+ * Environment for this run's session — how a SHARED executable learns what is
5449
+ * specific to this run (bridge URL, identity token, source id).
5450
+ *
5451
+ * The alternative is a per-workspace copy of each tool with those values
5452
+ * baked in, which makes a tool edit reach only new workspaces and leaves the
5453
+ * run's identity token on disk after it is revoked.
5454
+ */
5455
+ env?(input: TInput, ctx: AgentRunContext): Record<string, string> | Promise<Record<string, string>>;
5456
+ /**
5457
+ * Put the job's inputs in the workspace as FILES, not prompt text. A coding
5458
+ * agent can grep, aggregate and script over a file; it cannot do any of that
5459
+ * with text pasted into its prompt, and a large input would not fit there
5460
+ * anyway.
5461
+ */
5462
+ prepare?(ws: Workspace, input: TInput, ctx: AgentRunContext): Promise<void>;
5463
+ /**
5464
+ * The short framing sent as the turn's prompt. Keep it short — the standing
5465
+ * instructions belong in system.md and the data belongs in files. Defaults
5466
+ * to a generic instruction naming the output path.
5467
+ */
5468
+ buildPrompt?(ws: Workspace, input: TInput, ctx: AgentRunContext): string | Promise<string>;
5469
+ /** Required for 'task' mode; meaningless for 'chat'. */
5470
+ output?: OutputContract<TOutput>;
5471
+ /**
5472
+ * Persist the validated deliverable — through existing services, never raw
5473
+ * SQL from inside the agent. Keeping the write on this side is what makes a
5474
+ * run auditable and replayable: the artifact on disk is the same input
5475
+ * apply() would get again tomorrow.
5476
+ */
5477
+ apply?(result: TOutput, input: TInput, ctx: AgentRunContext): Promise<ApplySummary>;
5478
+ /**
5479
+ * What makes this agent run. Declared here so an agent's folder plus this
5480
+ * file is the complete story of when it fires. See triggers.ts.
5481
+ */
5482
+ trigger?: AgentTrigger | AgentTrigger[];
5483
+ /**
5484
+ * WHO this agent's accumulated state belongs to. Distinct from `mode`, which
5485
+ * is only how a run was triggered — the two were conflated before, so a
5486
+ * project agent someone chatted with silently got a private per-thread
5487
+ * workspace and lost its memory.
5488
+ *
5489
+ * 'project' ONE workspace and session for the whole deployment, whoever
5490
+ * invokes it. A concept builder is project-wide: everything it
5491
+ * learns should accumulate in one place, and a chat turn joins
5492
+ * that same workspace rather than forking a copy. Concurrent
5493
+ * runs serialise.
5494
+ * 'user' one per user, shared across that user's threads.
5495
+ * 'thread' one per conversation — an ordinary chat assistant.
5496
+ *
5497
+ * Default: 'thread' for mode 'chat', 'project' for mode 'task'.
5498
+ */
5499
+ scope?: 'project' | 'user' | 'thread';
5500
+ /**
5501
+ * Kill the turn the moment the deliverable file appears, instead of letting
5502
+ * the agent finish.
5503
+ *
5504
+ * Off by default, and the reason is measured: an agent typically writes its
5505
+ * output and then keeps going — verifying, re-reading, summarising — so
5506
+ * stopping at the write cuts the turn well before codex emits
5507
+ * turn.completed, which is the only event carrying token usage. Early stop
5508
+ * therefore costs you all cost tracking, and skips the agent's own
5509
+ * self-check. Turn it on for a long-tailed agent where the file really is
5510
+ * the last thing that matters.
5511
+ */
5512
+ stopOnDeliverable?: boolean;
5513
+ /** What this agent carries between runs. Default: nothing (fresh session and workspace). */
5514
+ session?: SessionPolicy;
5515
+ /** Hard cap for one run. Default 15 minutes. */
5516
+ timeoutMs?: number;
5517
+ }
5518
+ /** What a completed run reports. */
5519
+ interface AgentRunRecord<TOutput = unknown> {
5520
+ agentId: string;
5521
+ runId: string;
5522
+ /** The run's directory — instructions, inputs, scripts and output all still there. */
5523
+ workspaceDir: string;
5524
+ /**
5525
+ * 'early' the deliverable landed and we stopped the turn — a success, kept
5526
+ * distinct from 'completed' so a masked early exit is visible.
5527
+ * 'skipped' another run of this agent held the lock; nothing was done.
5528
+ */
5529
+ status: 'completed' | 'early' | 'aborted' | 'error' | 'skipped';
5530
+ /** The agent's closing message. */
5531
+ answer: string;
5532
+ ms: number;
5533
+ usage: AgentUsage;
5534
+ /** Present when the agent produced and validated its deliverable. */
5535
+ output?: TOutput;
5536
+ /** Present when apply() ran. */
5537
+ applied?: ApplySummary;
5538
+ error?: string;
5539
+ /** Harness session id, for resuming a chat. */
5540
+ sessionId?: string;
5541
+ }
5542
+
5543
+ /**
5544
+ * The agent registry — one place that knows which agents this deployment has.
5545
+ *
5546
+ * Deliberately a plain module-level map rather than something on the SDK: a
5547
+ * cron job, a CLI command and the websocket handler all need to reach the same
5548
+ * agents, and only one of those has an SDK instance in hand.
5549
+ */
5550
+ interface CodingAgentsConfig {
5551
+ /**
5552
+ * Where per-agent instruction folders live. Each agent reads
5553
+ * `<promptRoot>/<agent.id>/system.md`.
5554
+ * Default: `<cwd>/.prompts/coding-agents`, matching the .prompts convention
5555
+ * the rest of the deployment already uses.
5556
+ */
5557
+ promptRoot?: string;
5558
+ /**
5559
+ * Root for run workspaces: `<runtimeDir>/<agentId>/<runId>/`. Regenerated
5560
+ * per run and safe to gitignore — deliberately not inside promptRoot, which
5561
+ * is version-controlled.
5562
+ * Default: `<cwd>/.coding-agent-runtime`.
5563
+ */
5564
+ runtimeDir?: string;
5565
+ /** Fallback harness for agents that don't name one. */
5566
+ harness?: Harness;
5567
+ /** Fallback model. */
5568
+ model?: string;
5569
+ /** Tools every agent gets, merged under each agent's own (the agent's win on a name clash). */
5570
+ sharedTools?: Record<string, McpServerSpec>;
5571
+ /** Keep this many run directories per agent; older ones are pruned. 0 keeps everything. */
5572
+ keepRuns?: number;
5573
+ /**
5574
+ * Keep this many THREAD workspaces per thread-scoped agent; the least
5575
+ * recently used are deleted. A persistent workspace is never pruned by
5576
+ * keepRuns, so without this one directory per UI thread accumulates forever
5577
+ * (~3.4 MB each for the answer agent). 0 keeps everything.
5578
+ * Env: CODING_AGENT_KEEP_THREADS.
5579
+ */
5580
+ keepThreads?: number;
5581
+ }
5582
+ /**
5583
+ * Register this deployment's agents. Call once at startup.
5584
+ *
5585
+ * Re-registering an id replaces it — that keeps hot-reload working in dev and
5586
+ * makes a double registration a no-op rather than a silent duplicate.
5587
+ */
5588
+ declare function registerCodingAgents(definitions: CodingAgentDefinition<any, any>[], options?: CodingAgentsConfig): void;
5589
+ declare function getCodingAgent(id: string): CodingAgentDefinition<any, any> | undefined;
5590
+ declare function listCodingAgents(): Array<{
5591
+ id: string;
5592
+ title: string;
5593
+ description?: string;
5594
+ mode: 'task' | 'chat';
5595
+ }>;
5596
+ declare function getCodingAgentsConfig(): Required<Pick<CodingAgentsConfig, "harness" | "promptRoot" | "runtimeDir" | "keepRuns" | "keepThreads">> & CodingAgentsConfig;
5597
+
5598
+ /**
5599
+ * The workspace file the harness reads as its standing instructions.
5600
+ *
5601
+ * Codex discovers its project doc BY NAME and only knows AGENTS.md, so every
5602
+ * session is given this as `instructionsFile` and the driver registers it.
5603
+ * Keeping the workspace copy called system.md, like the version-controlled
5604
+ * source, removes the one-name-two-meanings indirection.
5605
+ */
5606
+ declare const INSTRUCTIONS_FILE = "system.md";
5607
+ interface RunAgentOptions<TInput> extends AgentRunContext {
5608
+ input?: TInput;
5609
+ /** Scopes a 'thread' agent. Ignored by a project-scoped one. */
5610
+ threadId?: string;
5611
+ /**
5612
+ * Escape hatch that pins the workspace directory outright. Almost never
5613
+ * right — it defeats the agent's own scope, which is how a project agent
5614
+ * ends up with a private copy per caller. Left for tests and one-offs.
5615
+ */
5616
+ workspaceDir?: string;
5617
+ /** Resume the harness session — chat continuity across turns and restarts. */
5618
+ resumeId?: string;
5619
+ /** Overrides the definition's prompt entirely. Chat mode: this is the user's message. */
5620
+ prompt?: string;
5621
+ /**
5622
+ * A person is typing, rather than a job running.
5623
+ *
5624
+ * For an agent whose mode is 'task' this turns off the JOB — prepare(), the
5625
+ * deliverable contract and apply() are one unit that sets work up and
5626
+ * consumes it, and a conversation should do neither. The turn joins the
5627
+ * workspace as it stands and its answer is the whole result.
5628
+ *
5629
+ * Without this, chatting with a task agent could only ever fail: the user's
5630
+ * message replaces the prompt that names the output path, so the agent never
5631
+ * writes the deliverable, and the run is then reported as an error despite
5632
+ * having answered perfectly well. Worse, prepare() ran on every turn — for a
5633
+ * batching agent that means a chat turn quietly consumed a batch of work.
5634
+ *
5635
+ * An agent whose mode is 'chat' is unaffected: its prepare() is per-turn
5636
+ * context setup, not job setup, so it still runs.
5637
+ */
5638
+ chat?: boolean;
5639
+ }
5640
+ /**
5641
+ * Run one agent, end to end.
5642
+ *
5643
+ * Never throws for an agent-side failure — a bad run comes back as a record
5644
+ * with status 'error', because callers are usually crons and message handlers
5645
+ * that must keep going. Genuinely programmer errors (unknown agent id) do throw.
5646
+ */
5647
+ declare function runCodingAgent<TInput = unknown, TOutput = unknown>(agentId: string, options?: RunAgentOptions<TInput>): Promise<AgentRunRecord<TOutput>>;
5648
+
5649
+ /**
5650
+ * Write `./sources` and `./query` into an agent's workspace: read-only data
5651
+ * access as plain executables it runs with bash.
5652
+ *
5653
+ * Why not MCP: codex gates MCP tool calls on the sandbox. Under any sandboxed
5654
+ * mode they need approval, and a headless run must set approvalPolicy 'never',
5655
+ * which denies rather than grants — measured, not assumed. Only
5656
+ * danger-full-access permits them, and that removes the filesystem sandbox
5657
+ * entirely. Bash commands under workspace-write need no approval, so a shim
5658
+ * buys the same capability while the agent stays confined to its workspace.
5659
+ *
5660
+ * An MCP server for this was written and then removed: the same approval gate
5661
+ * applies to the Agents chat tab, which is equally headless from codex's point
5662
+ * of view, so it was unreachable everywhere. `mcpServers` stays plumbed through
5663
+ * the driver for third-party servers; see git history for the removed one.
5664
+ *
5665
+ * The cost is structured tool events: calls arrive as `bash → ./query ...`
5666
+ * rather than typed items with parsed arguments. That matters for a chat UI
5667
+ * with a human watching; for a headless task agent it does not.
5668
+ *
5669
+ * Requires networkAccessEnabled on the session — the shim reaches the bridge
5670
+ * over HTTP, and that is the whole point of it.
5671
+ */
5672
+ declare function writeBridgeShims(ws: Workspace, opts: {
5673
+ bridgeUrl: string;
5674
+ sources: Array<{
5675
+ id: string;
5676
+ name?: string;
5677
+ description?: string;
5678
+ }>;
5679
+ maxRows?: number;
5680
+ /**
5681
+ * Identity for the queries this agent runs, from mintContextToken().
5682
+ *
5683
+ * The agent is a separate OS process calling the bridge over loopback
5684
+ * HTTP, so nothing ambient crosses that gap — the same break the script
5685
+ * flow has (see utils/context-token.ts). Without this the bridge sees an
5686
+ * anonymous query, and under SA_SQL_REWRITE_MODE=enforce the data seam
5687
+ * refuses it outright rather than guessing whose data it is.
5688
+ *
5689
+ * Omit ONLY when the caller genuinely has no identity to grant; the
5690
+ * queries are then anonymous, which is the truth rather than a default.
5691
+ */
5692
+ contextToken?: string;
5693
+ }): void;
5694
+
5695
+ /**
5696
+ * Read-only SQL guard for any path that hands an agent a database.
5697
+ *
5698
+ * Defence in depth, NOT a boundary. The agent has a shell and can reach the
5699
+ * bridge directly, so anything enforced only here is advisory — it stops
5700
+ * mistakes, not determination. The real boundary is a database user without
5701
+ * write permission; this exists so a confused agent gets a clear correction
5702
+ * instead of a silent partial write.
5703
+ */
5704
+ /** Throws with a message written for the agent to act on. */
5705
+ declare function assertReadOnlySql(sql: string): void;
5706
+
5707
+ /**
5708
+ * The 'coding-agent'.'run' collection — a chat turn against a coding agent.
5709
+ *
5710
+ * Generic on purpose: nothing here knows about dashboards, reports, or any
5711
+ * particular deliverable. It owns what every harness-backed chat needs — one
5712
+ * session per thread, a per-thread workspace, a run lock, and forwarding the
5713
+ * driver's callbacks to the transport. Task-specific flows (prompt assembly, a
5714
+ * draft-file contract, persistence) build on top of this rather than inside it.
5715
+ */
5716
+ interface CodingAgentCollectionConfig {
5717
+ /**
5718
+ * Root for per-thread agent workspaces. Each thread gets its own directory
5719
+ * underneath, so one chat can't read or clobber another's files, and none of
5720
+ * them sit inside the backend's own source tree.
5721
+ * Default: `<process.cwd()>/.coding-agent-runtime`.
5722
+ */
5723
+ runtimeDir?: string;
5724
+ /** Default harness when the request doesn't name one. */
5725
+ harness?: Harness;
5726
+ model?: string;
5727
+ /** Tools, as MCP servers, handed to every session. */
5728
+ mcpServers?: Record<string, McpServerSpec>;
5729
+ }
5730
+ declare function registerCodingAgentCollection(sdk: SuperatomSDK, config?: CodingAgentCollectionConfig): void;
5731
+
5732
+ declare class ScriptMatcher {
5733
+ private store;
5734
+ private collections?;
5735
+ private userId?;
5736
+ private trace?;
5737
+ constructor(store: ScriptStore, opts?: {
5738
+ collections?: any;
5739
+ userId?: string;
5740
+ trace?: RunTrace;
5741
+ });
5742
+ /**
5743
+ * Find the best matching script for a user question.
5744
+ * Uses ONE LLM call that picks the script AND extracts parameters.
5745
+ * Returns null if no script matches.
5746
+ *
5747
+ * Thin wrapper around `doMatch` whose only job is guaranteeing the trace
5748
+ * capture happens exactly once, on every exit path (short-circuit, normal
5749
+ * return, or the LLM call erroring) — see RunTrace.scriptMatcher.
5750
+ */
5751
+ match(userPrompt: string, apiKey?: string, model?: string, signal?: AbortSignal, activeBindings?: ScriptBinding[]): Promise<ScriptMatch | null>;
5752
+ private doMatch;
5753
+ /**
5754
+ * Renders a recipe's declared parameters the same way in every prompt
5755
+ * section (catalog, active-script, edit candidates) so the LLM sees one
5756
+ * consistent format regardless of which block a script came from.
5757
+ */
5758
+ private formatParamList;
5759
+ /**
5760
+ * Semantic search over THIS user's own past prompts (`conversation-history`
5761
+ * ChromaDB embeddings, already user-scoped) — not a project-wide name/tag
5762
+ * search. Groups hits by the recipe that answered them, so a script gets
5763
+ * ONE catalog entry carrying every matched past question, not one per hit.
5764
+ *
5765
+ * Scope is deliberate (see SCRIPT-MATCH-IMPROVEMENTS.md, Change 2): a
5766
+ * script only shows up here because THIS user has, before, asked something
5767
+ * similar that it answered — not because some other user's question did.
5768
+ */
5769
+ private searchPromptHistory;
5770
+ /**
5771
+ * Build the script catalog string for the LLM prompt from prompt-history
5772
+ * matches: each script's declared parameters PLUS the actual past questions
5773
+ * of this user's that it answered (real precedent, not a hand-written
5774
+ * description) and what params they ran with.
5775
+ */
5776
+ private buildPromptHistoryCatalog;
5777
+ /**
5778
+ * The most recent script-backed answer in this thread — the one an `edit`
5779
+ * targets. Rendered as its own prompt section (never merged into the ranked
5780
+ * catalog) so the `edit` rules have an unambiguous referent.
5781
+ *
5782
+ * `candidates` holds at most one entry (see doMatch) — `edit` always means
5783
+ * "the answer I just got", so there is nothing else to show here.
5784
+ *
5785
+ * Carries the QUESTION that produced it plus the columns it returned — that
5786
+ * is what lets the matcher resolve "use the mode for the WSP one" against
5787
+ * this one script's own context.
5788
+ */
5789
+ private buildActiveScriptBlock;
5790
+ }
5791
+
5792
+ /**
5793
+ * ScriptRunner — Execute scripts in an isolated tsx subprocess.
5794
+ *
5795
+ * The subprocess approach replaces the earlier `new Function()` eval and gives us:
5796
+ * - Real sandbox (separate process, SIGKILL on timeout).
5797
+ * - Real TypeScript (tsx transpiles on the fly).
5798
+ * - npm imports available to scripts (clustering, stats, geo, etc.).
5799
+ *
5800
+ * Protocol: NDJSON over the child's stdin/stdout. See script-ipc.ts + backend/docs/SCRIPT-FLOW-IMPLEMENTATION.md.
5801
+ */
5802
+
5803
+ interface RunScriptOptions {
5804
+ /** Data sources the script is allowed to query via ctx.query */
5805
+ externalTools: ExternalTool[];
5806
+ /** Optional — for propagating per-query UI progress to the user */
5807
+ streamBuffer?: StreamBuffer;
5808
+ /** Override the wall-clock timeout (default `SCRIPT_TIMEOUT_MS`, 60s). */
5809
+ timeoutMs?: number;
5810
+ /**
5811
+ * Per-turn cancellation signal. When the user hits "Stop" mid-run, the child
5812
+ * process group is SIGKILLed and the run resolves as an aborted failure (the
5813
+ * caller is already unwinding, so the result is discarded).
5814
+ */
5815
+ signal?: AbortSignal;
5816
+ /**
5817
+ * Names resolved for this run, handed to getAnalysis/getComponents as their
5818
+ * 5th argument. Supplying these is what stops "the narrative needs the
5819
+ * subsidiary's name" from being a reason to rewrite getData and re-query.
5820
+ */
5821
+ entities?: ResolvedEntityRef[];
5822
+ /**
5823
+ * Restrictions an authorization policy imposed on this run, handed to
5824
+ * getAnalysis/getComponents as their 6th argument.
5825
+ *
5826
+ * Supplied at RUN time rather than written into the recipe: a policy is
5827
+ * per-request and mutable, a recipe is durable. Baked in, it goes stale the
5828
+ * moment the user's scope changes — the recipe would keep naming the old
5829
+ * scope while the seam injects the new one. Supplied here, a replay always
5830
+ * describes what was actually applied to THAT run.
5831
+ */
5832
+ appliedPolicies?: string[];
5833
+ }
5834
+ /**
5835
+ * Execute a recipe by spawning a tsx child on the script's .ts file.
5836
+ * `scriptPath` is the absolute path to the saved `.ts` body.
5837
+ */
5838
+ declare function runScript(recipe: ScriptRecipe, scriptPath: string, params: Record<string, any>, options: RunScriptOptions): Promise<ScriptResult>;
5839
+
5840
+ /**
5841
+ * Answer-flow component catalog (§2.6 a/e).
5842
+ *
5843
+ * The frontend sends ONE catalog to the SDK; five consumers read it and four of
5844
+ * them narrow it for their own purpose (dashComp strips the container,
5845
+ * create-filter selects Filter*, suggestions selects isDisplayComp). The answer
5846
+ * flow was the only one that didn't. These helpers are its narrowing.
5847
+ *
5848
+ * Nothing here mutates the stored `Metadata` — dashboard and report generation
5849
+ * legitimately need the SQL-authoring contract that the answer flow must not see,
5850
+ * so the answer flow gets a PROJECTION instead.
5851
+ *
5852
+ * See backend/docs/COMPONENT-GENERATION-V2.md §2.6.
5853
+ */
5854
+
5855
+ /**
5856
+ * The only components that can answer a data question. Everything else in the
5857
+ * catalog belongs to dashboards (filters, forms), is a whole page (Home,
5858
+ * DatabaseOverview), is a UI primitive, or is the wrapper this flow builds.
5859
+ *
5860
+ * DynamicWaterfallChart is deliberately excluded — see §2.6(e). Adding it back
5861
+ * is a one-line change here.
5862
+ */
5863
+ declare const ANSWER_COMPONENT_NAMES: readonly string[];
5864
+ /**
5865
+ * Narrow the registered catalog to the components that can answer a question.
5866
+ * Safe for every answer-flow generator — none of them should ever nominate a
5867
+ * filter widget, a text input, or the MultiComponentContainer the flow itself
5868
+ * builds around their output.
5869
+ */
5870
+ declare function answerComponents(components: Component[] | undefined): Component[];
5871
+ /**
5872
+ * Rewrite the catalog for a flow that binds pre-executed datasets rather than
5873
+ * authoring SQL. Apply ONLY where the flow discards component-authored SQL —
5874
+ * `generateScriptComponents` and the `write_components` tool.
5875
+ *
5876
+ * Do NOT apply to `generateAgentComponents`: that path really does write SQL
5877
+ * into externalTool.parameters.sql and validates it by executing it.
5878
+ */
5879
+ declare function projectForDatasetBinding(components: Component[]): Component[];
5880
+ /** Filter + project in one call — the catalog the tool and script generator see. */
5881
+ declare function answerCatalogForBinding(components: Component[] | undefined): Component[];
5882
+
5883
+ /**
5884
+ * Component Spec Validator — V1–V8
5885
+ *
5886
+ * Deterministic checks of a component spec against the REAL columns of the
5887
+ * dataset it binds. Pure: no LLM, no mutation, no I/O. Shared by the fallback
5888
+ * generator (report-only today) and the `write_components` tool, which hands the
5889
+ * findings back to the agent so it can fix getComponents and retry.
5890
+ *
5891
+ * See backend/docs/COMPONENT-GENERATION-V2.md §2.3.
5892
+ */
5893
+ type ColType = 'NUMBER' | 'TEXT' | 'DATE' | 'BOOLEAN';
5894
+ interface ColInfo {
5895
+ name: string;
5896
+ type: ColType;
5897
+ /** Every sampled non-null value is identical — a per-row invariant (e.g. a
5898
+ * total the script computed the same on every row). Must not be summed. */
5899
+ invariant: boolean;
5900
+ }
5901
+ /** Infer column name + type + invariance from a sample of rows. */
5902
+ declare function inferColumns(rows: any[]): Map<string, ColInfo>;
5903
+ /** Keys that must resolve to a NUMBER column. */
5904
+ declare const NUMERIC_KEYS: readonly ["yAxisKey", "valueKey", "aggregationField", "sizeKey", "latitudeField", "longitudeField", "orderField"];
5905
+ /** Keys that must resolve to a TEXT or DATE column. */
5906
+ declare const CATEGORY_KEYS: readonly ["xAxisKey", "nameKey", "seriesKey", "groupBy", "categoryKey", "dimensionKey", "labelField", "stageField"];
5907
+ /**
5908
+ * Every config key that names a column. Superset of the generator's original
5909
+ * list — it omitted dimensionKey (Radar), sizeKey (Scatter) and the five
5910
+ * MapView geo fields, so those passed through unvalidated. §2.6(d).
5911
+ */
5912
+ declare const ALL_COLUMN_KEYS: string[];
5913
+ type FindingCode = 'V1' | 'V2' | 'V3' | 'V4' | 'V5' | 'V6' | 'V7' | 'V8';
5914
+ interface ValidationFinding {
5915
+ code: FindingCode;
5916
+ /** `error` blocks the spec; `warn` is advisory (logged, never rejects). */
5917
+ severity: 'error' | 'warn';
5918
+ componentType: string;
5919
+ /** Message written FOR THE AGENT — states the problem and the fix. */
5920
+ message: string;
5921
+ }
5922
+ interface SpecToValidate {
5923
+ componentType: string;
5924
+ sourceRef?: string;
5925
+ title?: string;
5926
+ description?: string;
5927
+ content?: string;
5928
+ config?: Record<string, any>;
5929
+ }
5930
+ interface ValidationContext {
5931
+ /** Columns of the bound dataset. */
5932
+ cols: Map<string, ColInfo>;
5933
+ /** UNCAPPED row count of the bound dataset — the cap is applied later, so
5934
+ * validating on the capped count would hide exactly the truncation we check. */
5935
+ rowCount: number;
5936
+ /** Rows for the mark-identity check (V4). Pass every row: a sample that happens
5937
+ * to hold no duplicate would report a collapsing chart as sound. */
5938
+ rows: any[];
5939
+ /** Valid `sourceRef` values for this run. */
5940
+ availableSourceRefs: string[];
5941
+ renderCap?: number;
5942
+ }
5943
+ /**
5944
+ * Validate one component spec. Returns every finding; the caller decides
5945
+ * whether to reject (tool path) or log (fallback path).
5946
+ */
5947
+ declare function validateComponentSpec(spec: SpecToValidate, ctx: ValidationContext): ValidationFinding[];
5948
+ /** Render findings as the text tool-result the agent reads on a rejection. */
5949
+ declare function formatFindingsForAgent(findings: ValidationFinding[]): string;
5950
+
5951
+ type MessageTypeHandler = (message: IncomingMessage) => void | Promise<void>;
5952
+ declare class SuperatomSDK {
5953
+ private ws;
5954
+ private url;
5955
+ private apiKey?;
5956
+ private projectId;
5957
+ private type;
5958
+ private bundleDir;
5959
+ private messageHandlers;
5960
+ private messageTypeHandlers;
5961
+ private connected;
5962
+ private reconnectAttempts;
5963
+ private maxReconnectAttempts;
5964
+ private collections;
5965
+ private components;
5966
+ private tools;
5967
+ private workflows;
2348
5968
  private anthropicApiKey;
2349
5969
  private groqApiKey;
2350
5970
  private geminiApiKey;
@@ -2352,6 +5972,11 @@ declare class SuperatomSDK {
2352
5972
  private llmProviders;
2353
5973
  private databaseType;
2354
5974
  private modelStrategy;
5975
+ private mainAgentModel;
5976
+ private sourceAgentModel;
5977
+ private answerFlow;
5978
+ private answerProvider?;
5979
+ private dashCompModels?;
2355
5980
  private conversationSimilarityThreshold;
2356
5981
  private userManager;
2357
5982
  private dashboardManager;
@@ -2360,6 +5985,8 @@ declare class SuperatomSDK {
2360
5985
  private lastPong;
2361
5986
  private readonly PING_INTERVAL_MS;
2362
5987
  private readonly PONG_TIMEOUT_MS;
5988
+ private pendingOutbox;
5989
+ private readonly MAX_OUTBOX_SIZE;
2363
5990
  constructor(config: SuperatomSDKConfig);
2364
5991
  /**
2365
5992
  * Initialize PromptLoader and load prompts into memory
@@ -2399,9 +6026,25 @@ declare class SuperatomSDK {
2399
6026
  */
2400
6027
  private handleMessage;
2401
6028
  /**
2402
- * Send a message to the Superatom service
6029
+ * Send a message to the Superatom service.
6030
+ * Returns true if the message was sent, false if the WebSocket is not connected.
6031
+ * Does NOT throw on closed connections — callers can check the return value if needed.
6032
+ */
6033
+ send(message: Message): boolean;
6034
+ /**
6035
+ * Queue a message that couldn't be delivered because the socket was down,
6036
+ * to be resent once it reconnects. Drops the oldest entry once the bound is
6037
+ * hit — an outage long enough to fill this queue means the oldest queued
6038
+ * responses are for requests the caller has likely already given up on.
2403
6039
  */
2404
- send(message: Message): void;
6040
+ private queuePendingMessage;
6041
+ /**
6042
+ * Resend everything queued while the socket was down, now that it's back
6043
+ * up. Uses this.ws.send() directly (not send()) so a message that fails
6044
+ * again goes back through queuePendingMessage() rather than being silently
6045
+ * dropped a second time.
6046
+ */
6047
+ private flushPendingOutbox;
2405
6048
  /**
2406
6049
  * Register a message handler to receive all messages
2407
6050
  */
@@ -2441,6 +6084,14 @@ declare class SuperatomSDK {
2441
6084
  */
2442
6085
  private handlePong;
2443
6086
  private storeComponents;
6087
+ /**
6088
+ * The live, frontend-registered component catalog (name, type, description,
6089
+ * and full prop schema per component) — the same authoritative source
6090
+ * DASH_COMP_REQ's LLM prompt is built from. Exposed so other integrations
6091
+ * (e.g. the dashboard-agent script bridge) can read real component
6092
+ * contracts instead of maintaining a separate, driftable hand-written copy.
6093
+ */
6094
+ getComponents(): Component[];
2444
6095
  /**
2445
6096
  * Set tools for the SDK instance
2446
6097
  */
@@ -2449,6 +6100,29 @@ declare class SuperatomSDK {
2449
6100
  * Get the stored tools
2450
6101
  */
2451
6102
  getTools(): Tool$1[];
6103
+ /**
6104
+ * Call a registered collection operation in-process — no WebSocket
6105
+ * round-trip, since the caller is already running inside this same SDK
6106
+ * instance. Lets SDK-internal features (e.g. the dashboard agent) reuse
6107
+ * whatever collection a deployment has already registered (e.g.
6108
+ * 'dashboards'.'query') by name/convention, instead of requiring a
6109
+ * separate callback purely to re-expose data a collection already serves.
6110
+ * Throws if the collection or operation isn't registered.
6111
+ */
6112
+ callCollection<TResult = any>(collectionName: string, operation: string, params?: any): Promise<TResult>;
6113
+ /**
6114
+ * Register workflow components for the SDK instance.
6115
+ *
6116
+ * Workflows are pre-built multi-step UI flows the main agent can pick when
6117
+ * the user's prompt matches a workflow's `whenToUse` trigger. Picking a
6118
+ * workflow short-circuits analysis text + dashboard component generation —
6119
+ * the workflow component is returned directly, with the LLM-extracted props.
6120
+ */
6121
+ setWorkflows(workflows: WorkflowDescriptor[]): void;
6122
+ /**
6123
+ * Get the registered workflow components.
6124
+ */
6125
+ getWorkflows(): WorkflowDescriptor[];
2452
6126
  /**
2453
6127
  * Apply model strategy to all LLM provider singletons
2454
6128
  * @param strategy - 'best', 'fast', or 'balanced'
@@ -2479,4 +6153,4 @@ declare class SuperatomSDK {
2479
6153
  getConversationSimilarityThreshold(): number;
2480
6154
  }
2481
6155
 
2482
- export { type Action, BM25L, type BM25LOptions, type BaseLLMConfig, CONTEXT_CONFIG, type CapturedLog, CleanupService, type CollectionHandler, type CollectionOperation, type DBUIBlock, type DatabaseType, type HybridSearchOptions, type IncomingMessage, type KbNodesQueryFilters, type KbNodesRequestPayload, LLM, type LLMUsageEntry, type LogLevel, type Message, type ModelStrategy, type OutputField, type RerankedResult, SDK_VERSION, STORAGE_CONFIG, SuperatomSDK, type SuperatomSDKConfig, type TaskType, Thread, ThreadManager, type Tool$1 as Tool, type ToolOutputSchema, UIBlock, UILogCollector, type User, UserManager, type UsersData, anthropicLLM, geminiLLM, groqLLM, hybridRerank, llmUsageLogger, logger, openaiLLM, queryCache, rerankChromaResults, rerankConversationResults, userPromptErrorLogger };
6156
+ export { ALL_COLUMN_KEYS, ANSWER_COMPONENT_NAMES, type AgentConfig, type AgentEvent, type AgentResponse, type AgentRunContext, type AgentRunHandlers, type AgentRunRecord, type AgentRunResult, type AgentTrigger, type AgentUsage, type AnswerProvider, type AnswerProviderContext, type ApplySummary, BM25L, type BM25LOptions, type BaseLLMConfig, type BuildTriggerInput, CATEGORY_KEYS, CONTEXT_CONFIG, type CapturedLog, CleanupService, type CodexSessionOptions, type CodingAgentCollectionConfig, type CodingAgentDefinition, type CodingAgentSession, type CodingAgentSessionOptions, type CodingAgentsConfig, type ColInfo, type ColType, type CollectionHandler, type CollectionOperation, type Component, type DBUIBlock, DEFAULT_AGENT_CONFIG, DEFAULT_FMT_CONFIG, type DashboardAgentCollectionConfig, type DatabaseType, type FindingCode, type Fmt, type FmtConfig, type Harness, type HybridSearchOptions, INSTRUCTIONS_FILE, type IncomingMessage, type KbNodesQueryFilters, type KbNodesRequestPayload, LLM, type LLMUsageEntry, type LogLevel, MainAgent, type McpServerSpec, type Message, MissingRequestContextError, type ModelStrategy, NUMERIC_KEYS, type NarrationUpdate, type OutputContract, type OutputField, type OutputValidator, type Principal, type RequestContext, type RerankedResult, type RowFilter, type RunAgentOptions, STORAGE_CONFIG, type ScriptComponentSpec, ScriptMatcher, type ScriptParameter, type ScriptRecipe, type ScriptRecipeMetaRow, type ScriptRecipeStore, type ScriptResult, ScriptStore, type ScriptStoreOptions, type SelectedWorkflow, type SourcePolicy, type SpecToValidate, SuperatomSDK, type SuperatomSDKConfig, type T_RESPONSE, type TaskType, Thread, ThreadManager, type Tool$1 as Tool, type ToolOutputSchema, type TriggerEvent, type TriggerFireInfo, UIBlock, UILogCollector, type User, UserManager, type UsersData, type ValidationContext, type ValidationFinding, type WorkflowDescriptor, type Workspace, answerCatalogForBinding, answerComponents, anthropicLLM, assertReadOnlySql, bindContext, captureContext, clearContextTokens, contextTokenCount, createCodexSession, createCodingAgentSession, dashboardConversationHistory, dataFingerprint, describeContext, describeTriggers, emitAgentEvent, extractNamedExport, formatFindingsForAgent, geminiLLM, getCodingAgent, getCodingAgentsConfig, getRequestContext, groqLLM, hybridRerank, inferColumns, isUserContext, listCodingAgents, llmUsageLogger, logger, makeFmt, mintContextToken, normalizeScriptBody, onAgentRun, openaiLLM, policiesFromConfig, projectForDatasetBinding, queryCache, registerCodingAgentCollection, registerCodingAgents, registerDashboardAgentCollection, requireRequestContext, rerankChromaResults, rerankConversationResults, resolveContextToken, resolveScriptRecipeStore, revokeContextToken, runAsSystem, runAsUser, runCodingAgent, runScript, runWithContext, runWithMessageIdentity, startAgentTriggers, stopAgentTriggers, userPromptErrorLogger, validateComponentSpec, withCapturedContext, writeBridgeShims };