@superatomai/sdk-node 0.0.2-s → 0.0.3-cp

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.ts CHANGED
@@ -2,99 +2,1365 @@ import { z } from 'zod';
2
2
  import Anthropic from '@anthropic-ai/sdk';
3
3
 
4
4
  /**
5
- * Unified UIBlock structure for database storage
6
- * Used in both bookmarks and user-conversations tables
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
7
11
  */
8
- interface DBUIBlock {
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 */
9
19
  id: string;
10
- component: Record<string, any> | null;
11
- analysis: string | null;
12
- user_prompt: 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[];
13
134
  }
14
-
15
135
  /**
16
- * Log levels in hierarchical order
17
- * - errors: only error logs
18
- * - warnings: warning + error logs
19
- * - info: info + warning + error logs
20
- * - verbose: all logs including debug
136
+ * A durable, user-stated rendering choice for a recipe. Survives data edits.
21
137
  */
22
- type LogLevel = 'errors' | 'warnings' | 'info' | 'verbose';
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
+ }
23
175
  /**
24
- * Logger class with environment-based log level support
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.
25
179
  */
26
- declare class Logger {
27
- private currentLevel;
28
- private currentLevelPriority;
29
- constructor();
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;
30
204
  /**
31
- * Check if a string is a valid log level
205
+ * getAnalysis threw. `success` stays true — the data is valid, so the caller
206
+ * falls back to LLM prose rather than losing the answer.
32
207
  */
33
- private isValidLogLevel;
208
+ analysisError?: string;
209
+ /** Program-generated component specs, when the script exports getComponents. */
210
+ components?: ScriptComponentSpec[];
34
211
  /**
35
- * Check if a message should be logged based on current log level
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.
36
215
  */
37
- private shouldLog;
216
+ componentsError?: string;
217
+ /** Individual query results tracked during execution */
218
+ executedQueries: ScriptQueryResult[];
219
+ /** Error message if failed */
220
+ error?: string;
38
221
  /**
39
- * Get current log level
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).
40
225
  */
41
- getLogLevel(): LogLevel;
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;
42
249
  /**
43
- * Set log level programmatically
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.
44
255
  */
45
- setLogLevel(level: LogLevel): void;
256
+ virtual?: boolean;
257
+ }
258
+ /**
259
+ * Match tier returned by the LLM script matcher.
260
+ *
261
+ * - 'high': the script answers the question directly; only parameter values
262
+ * may differ. The runtime replays it with extracted params (cheapest path).
263
+ * - 'near': the script answers a STRUCTURALLY similar question but needs
264
+ * body modification (different metric, dimension, table, filter shape).
265
+ * The runtime forks the parent and adapts the body via MainAgent's normal
266
+ * write_script + execute_script loop — no SourceAgent dispatch needed.
267
+ * See backend/docs/SCRIPT-FLOW-FORK-ADAPT.md for the full design.
268
+ * - 'edit': the user is INSTRUCTING a change to the script that produced the
269
+ * previous answer (not asking a new question). Only reachable when the turn
270
+ * carries a ScriptBinding — without one the matcher coerces it to 'none', so
271
+ * a prompt regression can't turn this into a loose similarity path. The
272
+ * runtime runs MainAgent in edit mode and commits the result in place.
273
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md.
274
+ * - 'none': no script is relevant; full agent flow runs.
275
+ */
276
+ type MatchTier = 'high' | 'near' | 'edit' | 'none';
277
+ /**
278
+ * Which recipe produced the answer the user is currently looking at.
279
+ *
280
+ * Set whenever a turn's answer came from a script (replay, fresh authoring, or
281
+ * a committed edit) and carried on the UIBlock + saved conversation row. Two
282
+ * consumers:
283
+ * 1. The matcher — the 'edit' tier is ONLY reachable when a binding exists, so
284
+ * "use mode instead of mean" resolves to a concrete script instead of being
285
+ * matched on keywords it shares with no script name.
286
+ * 2. Cache invalidation — after an edit commits, conversations bound to that
287
+ * recipeId must be dropped, or the exact-match cache replays the pre-edit
288
+ * answer and the edit looks like a no-op.
289
+ *
290
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 1.
291
+ */
292
+ interface ScriptBinding {
293
+ recipeId: string;
46
294
  /**
47
- * Log info message (shown for info and verbose levels)
295
+ * The recipe's own version number at the moment THIS turn ran — not
296
+ * necessarily the recipe's current version. A later edit bumps the live
297
+ * version, so without this a past turn's binding would silently point at
298
+ * whatever the recipe looks like today instead of what actually produced
299
+ * that answer. Absent on bindings created before this field existed.
48
300
  */
49
- info(...args: any[]): void;
301
+ version?: number;
302
+ /** Params the script ran with — the edit's starting point. */
303
+ params: Record<string, any>;
304
+ /** Recipe name at bind time (matcher catalog + user-facing confirmation). */
305
+ name: string;
306
+ /** Columns the last run returned — grounds the editor without a re-query. */
307
+ columns?: string[];
50
308
  /**
51
- * Log error message (shown for all levels)
309
+ * The question that produced this answer. Required for disambiguation when a
310
+ * thread ran several scripts — without it the candidates are just names and
311
+ * the matcher cannot resolve "use the mode for the WSP one".
52
312
  */
53
- error(...args: any[]): void;
313
+ userPrompt?: string;
54
314
  /**
55
- * Log warning message (shown for warnings, info, and verbose levels)
315
+ * The polished analysis text this script actually produced last time it ran
316
+ * (the same programAnswer Thread.getConversationContext() shows elsewhere).
317
+ * Carried alongside the script's own shape (columns, params) so the matcher
318
+ * sees "what did this script actually say" right next to "what can I ask it
319
+ * to change", instead of needing a separate conversation-history transcript
320
+ * that would otherwise just repeat the same question this block already
321
+ * states. This is why ScriptMatcher's own prompt no longer receives a raw
322
+ * conversation-history transcript at all — this field is what replaced it.
56
323
  */
57
- warn(...args: any[]): void;
324
+ answerSummary?: string;
325
+ }
326
+ /**
327
+ * Result from the LLM-based script matcher.
328
+ *
329
+ * For `tier: 'high'`, `extractedParams` carries the values to pass to the
330
+ * existing script. For `tier: 'near'`, `gaps` and `modificationHint` describe
331
+ * what the fork-author needs to change in the parent body.
332
+ */
333
+ interface ScriptMatch {
334
+ /** The matched script recipe */
335
+ recipe: ScriptRecipe;
336
+ /** Match tier — see MatchTier docs */
337
+ tier: MatchTier;
338
+ /** Similarity score (0-1, derived from LLM tier) */
339
+ similarity: number;
340
+ /**
341
+ * Legacy confidence level. Mirrors `tier === 'high'`/`'near'` for now;
342
+ * kept so existing callers compile while we migrate to tier-based logic.
343
+ */
344
+ confidence: 'high' | 'medium';
345
+ /** Parameters extracted from the user question by the LLM (tier='high') */
346
+ extractedParams?: Record<string, any>;
347
+ /**
348
+ * Proper nouns the question named, VERBATIM, with the parameter each fills.
349
+ *
350
+ * The matcher must NOT resolve these itself. Observed live: it substituted the
351
+ * customer name "PREMER ENRGES PHOTOOTAIC" into an id parameter believing that
352
+ * "the runtime will resolve the customer name to its PartyId(s)" — nothing on
353
+ * the replay path does, and `WHERE BillingPartyId IN (PREMER ENRGES
354
+ * PHOTOOTAIC)` reached SQL. Reported here, the runtime resolves them before
355
+ * replay and rejects the match when one cannot be resolved.
356
+ */
357
+ mentions?: Array<{
358
+ text: string;
359
+ param?: string;
360
+ }>;
361
+ /** Business concepts the question relies on (e.g. "outstanding balance"). */
362
+ entityTypes?: string[];
363
+ /** What the user question needs that the parent doesn't cover (tier='near') */
364
+ gaps?: string[];
365
+ /** One-sentence description of the change the fork-author should make (tier='near') */
366
+ modificationHint?: string;
367
+ /**
368
+ * Which HALF of the recipe the edit targets (tier='edit'). A recipe has two
369
+ * independently editable halves:
370
+ * - 'data' — the scriptBody: how the rows are produced (aggregation,
371
+ * filters, joins, grouping). Runs MainAgent in edit mode.
372
+ * - 'display' — the component specs: how those SAME rows are shown (chart
373
+ * type, orientation, columns, labels). Replays the proven SQL
374
+ * and regenerates the specs — never authors a script.
375
+ * Defaults to 'data' when the matcher omits it.
376
+ */
377
+ editTarget?: 'data' | 'display';
378
+ /**
379
+ * Self-contained restatement of the change the user asked for (tier='edit').
380
+ * MUST have pronouns/deixis resolved ("this", "it", "that column") — the edit
381
+ * prompt never sees the conversation history, so an unresolved instruction is
382
+ * unusable downstream.
383
+ */
384
+ editInstruction?: string;
385
+ /** Why the matcher made this choice (for logs and telemetry) */
386
+ reasoning?: string;
387
+ }
388
+
389
+ /**
390
+ * script-ipc.ts — Protocol definitions for parent/child IPC.
391
+ *
392
+ * Transport: newline-delimited JSON over the child's stdin/stdout.
393
+ * stderr is passed through for uncaught exceptions and compile errors.
394
+ *
395
+ * See backend/docs/SCRIPT-FLOW-IMPLEMENTATION.md § IPC Bridge.
396
+ */
397
+
398
+ /**
399
+ * A name the user said, and what it resolved to.
400
+ *
401
+ * Handed to getAnalysis/getComponents so a narrative can NAME the thing the
402
+ * question was about without typing the name in. Before this, the runtime knew
403
+ * "Fusion5 Pty Ltd" and used that knowledge only to REJECT scripts that typed
404
+ * it — with no sanctioned way to obtain it, the only legal escape was to add a
405
+ * column to getData and re-run the query, spending an execute attempt to learn
406
+ * something the runtime already knew.
407
+ */
408
+ interface ResolvedEntityRef {
409
+ /** The text the user actually wrote. */
410
+ mention: string;
411
+ entityType: string;
412
+ instanceId: string;
413
+ displayName: string;
414
+ }
415
+ /**
416
+ * What getAnalysis returns. `analysis` is the narrative and `summary` the short
417
+ * bullets the markdown block binds to — siblings over one dataset, never a
418
+ * summary OF the narrative. See backend/docs/ANALYSIS-AS-PROGRAM-DESIGN.md § 4.3.
419
+ */
420
+ interface AnalysisOutput {
421
+ analysis?: string;
422
+ summary?: string[];
423
+ method?: string;
424
+ }
425
+
426
+ /**
427
+ * StreamBuffer - Buffered streaming utility for smoother text delivery
428
+ * Batches small chunks together and flushes at regular intervals
429
+ */
430
+ type StreamCallback = (chunk: string) => void;
431
+ /**
432
+ * StreamBuffer class for managing buffered streaming output
433
+ * Provides smooth text delivery by batching small chunks
434
+ */
435
+ declare class StreamBuffer {
436
+ private buffer;
437
+ private flushTimer;
438
+ private callback;
439
+ private fullText;
440
+ constructor(callback?: StreamCallback);
58
441
  /**
59
- * Log debug message (only shown for verbose level)
442
+ * Check if the buffer has a callback configured
60
443
  */
61
- debug(...args: any[]): void;
444
+ hasCallback(): boolean;
62
445
  /**
63
- * Write to log file
446
+ * Get all text that has been written (including already flushed)
64
447
  */
65
- file(...args: any[]): void;
448
+ getFullText(): string;
66
449
  /**
67
- * Clear the log file (call at start of new user request)
450
+ * Write a chunk to the buffer
451
+ * Large chunks or chunks with newlines are flushed immediately
452
+ * Small chunks are batched and flushed after a short interval
453
+ *
454
+ * @param chunk - Text chunk to write
68
455
  */
69
- clearFile(): void;
456
+ write(chunk: string): void;
70
457
  /**
71
- * Log LLM method prompts with clear labeling
458
+ * Flush the buffer immediately
459
+ * Call this before tool execution or other operations that need clean output
72
460
  */
73
- logLLMPrompt(methodName: string, promptType: 'system' | 'user', content: string | object | any[]): void;
461
+ flush(): void;
462
+ /**
463
+ * Internal flush implementation
464
+ */
465
+ private flushNow;
466
+ /**
467
+ * Clean up resources
468
+ * Call this when done with the buffer
469
+ */
470
+ dispose(): void;
74
471
  }
75
- declare const logger: Logger;
76
472
 
77
- declare const DSLRendererPropsSchema$1: z.ZodObject<{
78
- dsl: z.ZodObject<{
79
- id: z.ZodString;
80
- name: z.ZodOptional<z.ZodString>;
81
- props: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
82
- states: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
83
- methods: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{
84
- fn: z.ZodString;
85
- params: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
86
- }, "strip", z.ZodTypeAny, {
87
- fn: string;
88
- params?: Record<string, any> | undefined;
89
- }, {
90
- fn: string;
91
- params?: Record<string, any> | undefined;
92
- }>>>;
93
- effects: z.ZodOptional<z.ZodArray<z.ZodObject<{
94
- fn: z.ZodString;
95
- deps: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
96
- }, "strip", z.ZodTypeAny, {
97
- fn: string;
473
+ /**
474
+ * ToolExecutorService - Handles execution of SQL queries and external tools
475
+ * Extracted from BaseLLM.generateTextResponse for better separation of concerns
476
+ */
477
+
478
+ /**
479
+ * External tool definition
480
+ */
481
+ interface ExternalTool {
482
+ id: string;
483
+ name: string;
484
+ description?: string;
485
+ /** Tool type: "source" = routed through SourceAgent, "direct" = called directly by MainAgent */
486
+ toolType?: 'source' | 'direct';
487
+ /** Full untruncated schema for source agent (all columns visible) */
488
+ fullSchema?: string;
489
+ /** Schema size tier: small (≤50 tables), medium (51-200), large (201-500), very_large (500+) */
490
+ schemaTier?: string;
491
+ /** Schema search function for very_large tier — keyword search over entities */
492
+ schemaSearchFn?: (keywords: string[]) => string;
493
+ /** `signal` fires when the user hits Stop mid-query — passed through to the backend's SourceTool.fn. */
494
+ fn: (input: any, signal?: AbortSignal) => Promise<any>;
495
+ limit?: number;
496
+ outputSchema?: any;
497
+ executionType?: 'immediate' | 'deferred';
498
+ userProvidedData?: any;
499
+ params?: Record<string, any>;
500
+ }
501
+ /**
502
+ * Executed tool tracking info
503
+ */
504
+ interface ExecutedToolInfo {
505
+ id: string;
506
+ name: string;
507
+ params: any;
508
+ result: {
509
+ _totalRecords: number;
510
+ _recordsShown: number;
511
+ _metadata?: any;
512
+ _sampleData: any[];
513
+ /** Bounded summary over the FULL fetched result (complete structure). */
514
+ _summary?: any;
515
+ /** Up to MAIN_AGENT_COMPLETE_ROWS rows — the complete result when small. */
516
+ _mainAgentRows?: any[];
517
+ };
518
+ /**
519
+ * THIS call's rows, at render size — deliberately a sibling of `result`, not a
520
+ * field inside it, because `result` is the object that gets picked apart for
521
+ * prompts (`_sampleData`, `_mainAgentRows`, `_summary`). Never put these in a
522
+ * prompt.
523
+ *
524
+ * Exists because a SourceAgentResult keeps only the LAST query's rows for a
525
+ * whole dispatch (see main-agent's "a follow-up query overwrites result.data"
526
+ * note). A component bound to an EARLIER query therefore rendered the later
527
+ * query's data — confirmed live: a revenue chart and a budget chart both drew
528
+ * budget, because both queries ran through one tool.
529
+ */
530
+ renderRows?: any[];
531
+ outputSchema?: any;
532
+ sourceSchema?: string;
533
+ sourceType?: string;
534
+ }
535
+
536
+ /**
537
+ * Multi-Agent Architecture Types
538
+ *
539
+ * Defines interfaces for the hierarchical agent system:
540
+ * - Main Agent: ONE LLM.streamWithTools() call with source agent tools
541
+ * - Source Agents: independent agents that query individual data sources
542
+ *
543
+ * The main agent sees only source summaries. When it calls a source tool,
544
+ * the SourceAgent runs independently (own LLM, own retries) and returns clean data.
545
+ */
546
+
547
+ /**
548
+ * Per-entity detail: name, row count, and column names.
549
+ * Gives the main agent enough context to route to the right source.
550
+ */
551
+ interface EntityDetail {
552
+ /** Entity name (table, sheet, endpoint) */
553
+ name: string;
554
+ /** Approximate row count */
555
+ rowCount?: number;
556
+ /** Column/field names */
557
+ columns: string[];
558
+ /** Entity-level semantic summary (what the table means) — for main-agent routing. */
559
+ summary?: string;
560
+ }
561
+ /**
562
+ * Representation of a data source for the main agent.
563
+ * Contains entity names WITH column names so the LLM can route accurately.
564
+ */
565
+ interface SourceSummary {
566
+ /** Source ID (matches tool ID prefix) */
567
+ id: string;
568
+ /** Human-readable source name */
569
+ name: string;
570
+ /** Source type: postgres, excel, rest_api, etc. */
571
+ type: string;
572
+ /** Brief description of what data this source contains */
573
+ description: string;
574
+ /** Detailed entity info with column names for routing */
575
+ entityDetails: EntityDetail[];
576
+ /** The tool ID associated with this source */
577
+ toolId: string;
578
+ }
579
+ /**
580
+ * What a source agent returns after querying its data source.
581
+ * The main agent uses this to analyze and compose the final response.
582
+ */
583
+ interface SourceAgentResult {
584
+ /** Source ID */
585
+ sourceId: string;
586
+ /** Source name */
587
+ sourceName: string;
588
+ /** Whether the query succeeded */
589
+ success: boolean;
590
+ /** Result data rows — bounded to MAIN_AGENT_COMPLETE_ROWS (Stage 2: what the LLM
591
+ * sees verbatim in its own prompt). NOT render-sized — do not bind a UI
592
+ * component's dataset to this field; use renderData instead. */
593
+ data: any[];
594
+ /** Result data rows bounded to MAX_ROWS_RENDERED (Stage 3: render-appropriate).
595
+ * This is what already-fetched (Stage 1, <=MAX_ROWS_FETCHED) rows look like
596
+ * before Stage 2 further truncates `data` down for the LLM's own context —
597
+ * use this, not `data`, whenever binding a UI component's dataset. */
598
+ renderData: any[];
599
+ /** Metadata about the query execution */
600
+ metadata: SourceAgentMetadata;
601
+ /** Tool execution info for the last successful query (backward compat) */
602
+ executedTool: ExecutedToolInfo;
603
+ /** All successful tool executions (primary + follow-up queries) */
604
+ allExecutedTools?: ExecutedToolInfo[];
605
+ /** Error message if failed */
606
+ error?: string;
607
+ }
608
+ interface SourceAgentMetadata {
609
+ /** Total rows that matched the query (before limit) */
610
+ totalRowsMatched: number;
611
+ /** Rows actually returned (after limit) */
612
+ rowsReturned: number;
613
+ /** Whether the result was truncated by the row limit */
614
+ isLimited: boolean;
615
+ /** The query/params that were executed */
616
+ queryExecuted?: string;
617
+ /** Execution time in milliseconds */
618
+ executionTimeMs: number;
619
+ }
620
+ /**
621
+ * A pre-built, multi-step UI flow registered with the SDK.
622
+ *
623
+ * When the main agent decides a user's question matches a workflow's whenToUse
624
+ * trigger, it picks the workflow instead of running source agents / generating
625
+ * dashboard components. The LLM extracts the workflow's required props from the
626
+ * prompt (using `propsSchema` as the tool input_schema) and the SDK returns the
627
+ * workflow component directly — no analysis text, no chart generation. The
628
+ * frontend renders the registered workflow component with the LLM-extracted
629
+ * props.
630
+ */
631
+ interface WorkflowDescriptor {
632
+ /** Unique workflow id (used as the LLM tool name) */
633
+ id: string;
634
+ /** Component name on the frontend (matches the registered React component) */
635
+ name: string;
636
+ /** Short human-readable description of what this workflow does */
637
+ description: string;
638
+ /**
639
+ * 1–2 sentence trigger condition. The LLM uses this to decide if the
640
+ * user's prompt matches this workflow. Be specific — e.g.
641
+ * "User wants to *initiate* an inventory transfer (review + submit POs),
642
+ * not just see analysis or charts."
643
+ */
644
+ whenToUse: string;
645
+ /**
646
+ * JSON-schema-style description of the props the workflow needs. Becomes
647
+ * the LLM tool's input_schema, so the model fills these from the prompt.
648
+ * Use the same shape as `params` on direct tools — string descriptors with
649
+ * an optional "(optional)" suffix.
650
+ *
651
+ * Example:
652
+ * ```
653
+ * {
654
+ * selectedStore: 'object — { id, name } of the source branch',
655
+ * minROI: 'number (optional) — only show transfers with ROI ≥ this',
656
+ * }
657
+ * ```
658
+ */
659
+ propsSchema: Record<string, string>;
660
+ /**
661
+ * Optional: static prop defaults merged with LLM-extracted props before
662
+ * the component is returned. Useful for things like the embedded
663
+ * `externalTool` config that the workflow uses to fetch its own data.
664
+ */
665
+ defaultProps?: Record<string, any>;
666
+ }
667
+ /**
668
+ * The workflow selection captured during a routing call.
669
+ * Set on AgentResponse when the LLM picks a workflow tool.
670
+ */
671
+ interface SelectedWorkflow {
672
+ /** Component name (matches WorkflowDescriptor.name) */
673
+ name: string;
674
+ /** Props extracted from the prompt + merged with workflow.defaultProps */
675
+ props: Record<string, any>;
676
+ }
677
+ /**
678
+ * Set when this turn applies a user-directed edit to an existing script instead
679
+ * of authoring a new one. MainAgent keeps the SAME harness (tools, loop,
680
+ * write_script/execute_script verification) and swaps only the system prompt —
681
+ * `agent-main-edit` instead of `agent-main`.
682
+ *
683
+ * The two framings contradict each other on whether to query a source first, so
684
+ * they must never be resident in one rendered prompt.
685
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § D3.
686
+ */
687
+ interface EditContext {
688
+ /** Recipe being edited — the shadow draft records it as parentId. */
689
+ recipeId: string;
690
+ parentName: string;
691
+ parentBody: string;
692
+ /** Self-contained restatement of the change (from the matcher). */
693
+ instruction: string;
694
+ /** Columns the last run returned — grounds the edit without a re-query. */
695
+ lastResultColumns?: string[];
696
+ /** Rendered parameter list of the parent, for the prompt. */
697
+ parentParams?: string;
698
+ }
699
+ /**
700
+ * The complete response from the multi-agent system.
701
+ * Contains everything needed for text display + component generation.
702
+ */
703
+ interface AgentResponse {
704
+ /** Generated text response (analysis of the data) */
705
+ text: string;
706
+ /**
707
+ * Program-generated narrative when the script exported getAnalysis. `text`
708
+ * already carries `analysis.analysis`; this also exposes `summary`, which
709
+ * the markdown block binds to instead of being an LLM summary of the prose.
710
+ */
711
+ analysis?: AnalysisOutput;
712
+ /** All executed tools across all source agents (for component generation) */
713
+ executedTools: ExecutedToolInfo[];
714
+ /** Individual results from each source agent */
715
+ sourceResults: SourceAgentResult[];
716
+ /**
717
+ * Populated when MainAgent wrote AND successfully executed a script during its turn.
718
+ * Caller (agent-user-response.ts) persists it via ScriptStore.save().
719
+ * Absent when MainAgent didn't write one (trivial question / all attempts failed).
720
+ */
721
+ savedScript?: AgentWrittenScript;
722
+ /** Failed execute_script tool_result texts this turn, in order — see RunTrace.script.failedAttempts. */
723
+ failedScriptAttempts?: string[];
724
+ /**
725
+ * Set when the LLM routed the question to a registered workflow component.
726
+ * When present, the upstream caller should skip component generation and
727
+ * return this workflow as the response.
728
+ */
729
+ workflow?: SelectedWorkflow;
730
+ /**
731
+ * Validated component specs the agent authored via `write_components`.
732
+ * When present the caller assembles the dashboard from these instead of
733
+ * running the component-generation LLM. Absent when the agent didn't call
734
+ * the tool — the caller then falls back to `generateScriptComponents`.
735
+ * See backend/docs/COMPONENT-GENERATION-V2.md §2.2.
736
+ */
737
+ componentSpecs?: ScriptComponentSpec[];
738
+ /** Layout title/description from the same `write_components` call. */
739
+ componentLayout?: {
740
+ title: string;
741
+ description?: string;
742
+ };
743
+ }
744
+ /**
745
+ * A script MainAgent authored + verified during its turn. Shape aligns with
746
+ * what ScriptStore.save() needs — minus store-assigned fields (id, timestamps, counts).
747
+ */
748
+ interface AgentWrittenScript {
749
+ /**
750
+ * `ScriptRecipe.id` of the draft that was authored + verified during this turn.
751
+ * The caller passes this to `ScriptStore.promoteToVerified(recipeId, …)` to
752
+ * flip the draft to verified status and (when possible) drop the turn-suffix
753
+ * from its filename.
754
+ */
755
+ recipeId: string;
756
+ name: string;
757
+ intentDescription: string;
758
+ tags: string[];
759
+ parameters: Array<{
760
+ name: string;
761
+ type: 'string' | 'number' | 'date' | 'date_range' | 'enum' | 'boolean';
762
+ required: boolean;
763
+ default?: any;
764
+ enumValues?: Record<string, string>;
765
+ description: string;
766
+ }>;
767
+ scriptBody: string;
768
+ /** Source IDs referenced by the script (extracted from ctx.query calls) */
769
+ sourceIds: string[];
770
+ /** Tables referenced in the script's SQL (regex-extracted) */
771
+ tables: string[];
772
+ /** Executed queries from the verified run — fed to component generation */
773
+ executedQueries: Array<{
774
+ sourceId: string;
775
+ sourceName: string;
776
+ sql: string;
777
+ data: any[];
778
+ count: number;
779
+ totalCount?: number;
780
+ executionTimeMs: number;
781
+ /**
782
+ * True for synthetic entries (ctx.emit datasets, the computed:_final
783
+ * post-JS data). The component generator routes virtual sources through
784
+ * the script_dataset sentinel toolId so the frontend resolves them via
785
+ * queryCache instead of attempting to re-execute SQL.
786
+ */
787
+ virtual?: boolean;
788
+ }>;
789
+ /** The tool_result JSON MainAgent's LLM got back from execute_script — see RunTrace.script.executionResult. */
790
+ executionResult?: string;
791
+ }
792
+ /**
793
+ * Debug trace for a single chat_agent turn — which KB/schema nodes were
794
+ * retrieved and considered, and which script (if any) produced the answer.
795
+ * Persisted separately from the answer itself (fusion-5's
796
+ * `conversation_traces` table, written via `saveTrace`), never sent through
797
+ * `analyticsClient` (data-residency — see design doc §5).
798
+ */
799
+ interface RunTrace {
800
+ version: 1;
801
+ kbNodes: {
802
+ /**
803
+ * Always-on nodes included regardless of the question (getGlobalKnowledgeBase).
804
+ * `content` is the node's full text AS IT WAS at retrieval time — captured so a
805
+ * later edit/delete of the KB node doesn't erase what the LLM actually saw.
806
+ */
807
+ global: Array<{
808
+ kbId: string;
809
+ title: string;
810
+ content?: string;
811
+ }>;
812
+ /** Semantically matched for this specific question (getKnowledgeBase). Same `content` reasoning as above. */
813
+ query: Array<{
814
+ kbId: string;
815
+ title: string;
816
+ similarity?: number;
817
+ content?: string;
818
+ }>;
819
+ };
820
+ schemaNodes: {
821
+ /** MainAgent's cross-source prefilter — which sources were even considered. */
822
+ routing: Array<{
823
+ sourceId: string;
824
+ sourceName?: string;
825
+ similarity: number;
826
+ }>;
827
+ /** SourceAgent-scope per-source table resolution (MainAgent.preResolveSchema). */
828
+ preResolve: Array<{
829
+ sourceId: string;
830
+ table?: string;
831
+ description?: string;
832
+ similarity: number;
833
+ }>;
834
+ /**
835
+ * The exact {{SOURCE_SUMMARIES}} text injected into MainAgent's system prompt
836
+ * (agent-prompt-builder.ts's formatSummariesForPrompt output) — every
837
+ * registered source's catalog entry, not retrieval-specific, captured once per
838
+ * turn. This is what the main agent actually saw when deciding which source(s)
839
+ * to call.
840
+ */
841
+ mainAgentCatalog?: string;
842
+ /**
843
+ * One entry per SourceAgent dispatch (a source called twice in one turn gets
844
+ * two entries) — the exact {{FULL_SCHEMA}} text injected into that
845
+ * SourceAgent's own system prompt, however it was sourced, plus any
846
+ * search_schema follow-up lookups it made mid-conversation when the schema
847
+ * shown wasn't enough.
848
+ */
849
+ sourceAgentSchemas: Array<{
850
+ sourceId: string;
851
+ sourceName?: string;
852
+ /**
853
+ * The exact intent text this dispatch's own prompt was built from —
854
+ * captured in SourceAgent.buildPrompt, so it ALREADY includes the
855
+ * resolved-entities and resolved-concepts blocks MainAgent appends by
856
+ * code before calling execute() (see buildResolvedEntitiesBlock /
857
+ * buildResolvedConceptsBlock) — not just what the LLM itself wrote.
858
+ */
859
+ intent: string;
860
+ /** How `schemaText` was sourced — see SourceAgent.buildPrompt's fullSchema fallback chain. */
861
+ method: 'preresolved-embedding' | 'tool-full-schema' | 'tool-description' | 'none';
862
+ schemaText: string;
863
+ /** In call order — each search_schema tool call this SourceAgent made, and the exact result text handed back to its own LLM. */
864
+ searchSchemaLookups: Array<{
865
+ keywords: string[];
866
+ result: string;
867
+ }>;
868
+ /** The tool_result text MainAgent's LLM read back for this dispatch (formatResultForMainAgent output). */
869
+ result?: string;
870
+ /**
871
+ * The full resolved-concept bodies this dispatch's intent was built
872
+ * with (the same content already baked into `intent` via
873
+ * buildResolvedConceptsBlock, surfaced here structured/separately so a
874
+ * trace reader doesn't have to parse it back out of the raw text).
875
+ * Stamped AFTER execute() resolves — same timing as `result` above —
876
+ * specifically so its presence here is proof the concepts were part
877
+ * of the prompt actually sent, not just of what MainAgent intended to
878
+ * send before the call happened.
879
+ */
880
+ resolvedConcepts?: Array<{
881
+ name: string;
882
+ kind: string;
883
+ body: string;
884
+ }>;
885
+ /** This dispatch's `SourceToolInput.aggregation` ('raw' | 'pre-aggregate' | 'summary') — decides which AGGREGATION_MODE text this SourceAgent's own prompt got. */
886
+ aggregation?: string;
887
+ /** The exact DATABASE_RULES text this SourceAgent's own prompt got (promptLoader.loadDatabaseRulesForType). */
888
+ databaseRulesText?: string;
889
+ /** The exact ROW_LIMIT_SYNTAX text this SourceAgent's own prompt got. */
890
+ rowLimitSyntaxText?: string;
891
+ /** The exact SCHEMA_SEARCH_INSTRUCTIONS text this SourceAgent's own prompt got — the guidance, not the runtime searchSchemaLookups results above. */
892
+ schemaSearchInstructionsText?: string;
893
+ /** The literal tool definitions (query tool + search_schema tool, when offered) handed to this SourceAgent's own LLM.streamWithTools call. */
894
+ toolsSent?: any[];
895
+ /** This SourceAgent's own LLM.streamWithTools call config. */
896
+ callConfig?: {
897
+ model?: string;
898
+ maxTokens?: number;
899
+ temperature?: number;
900
+ maxIterations?: number;
901
+ };
902
+ /** 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. */
903
+ userAccessContextText?: string;
904
+ }>;
905
+ };
906
+ /**
907
+ * Concept-catalog activity this turn (see main-agent.ts's loadConceptCatalog /
908
+ * loadConceptInvariants / RESOLVE_CONCEPTS_TOOL_DEF). Parallel to
909
+ * `entityResolution` above. Absent/empty for a project with no concepts
910
+ * collection registered.
911
+ */
912
+ concepts: {
913
+ /** The exact {{CONCEPT_CATALOG}} text injected into MainAgent's system prompt (names + aliases only — bodies come back via resolve_concepts). */
914
+ catalogText?: string;
915
+ /** 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. */
916
+ invariantsText?: string;
917
+ /** 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. */
918
+ calls: Array<{
919
+ via: 'resolve_concepts' | 'resolve_entities' | 'dashboard-agent';
920
+ asked?: string[];
921
+ implied?: string[];
922
+ returned?: Array<{
923
+ kind: string;
924
+ name: string;
925
+ returns?: any;
926
+ definition: string;
927
+ viaRequires?: boolean;
928
+ }>;
929
+ viaFloor?: string[];
930
+ corrected?: Array<{
931
+ asked: string;
932
+ matched: string;
933
+ }>;
934
+ unmatched?: string[];
935
+ suggestions?: string[];
936
+ error?: string;
937
+ note?: string;
938
+ }>;
939
+ };
940
+ /** The script that produced this answer, if any. Null for a general (no-tool) answer. */
941
+ script: {
942
+ /** True when an existing recipe was replayed unchanged (ScriptMatcher tier='high'). */
943
+ reused: boolean;
944
+ recipeId?: string | null;
945
+ body?: string | null;
946
+ /** Set when reused — the matcher's reasoning for picking this recipe. */
947
+ reasoning?: string | null;
948
+ /** The winning execute_script tool_result text. Only set when reused=false — a replay never re-runs execute_script. Failed attempts aren't kept. */
949
+ executionResult?: string | null;
950
+ /** Failed execute_script tool_result texts this turn, in order (before the winning one, if any). */
951
+ failedAttempts?: string[];
952
+ } | null;
953
+ /**
954
+ * Everything ScriptMatcher.match() built and sent to its one LLM call this
955
+ * turn — Step 2 of the request flow, immediately after Step 1's exact-match
956
+ * check misses. Captured even when short-circuited before any LLM call, or
957
+ * when the call itself errored, so "why didn't this reuse a script" can
958
+ * always be answered from the trace alone, not just the outcome in `script`.
959
+ * See backend/docs/user-prompt-req-flow/SCRIPT-MATCH-IMPROVEMENTS.md.
960
+ */
961
+ scriptMatcher: {
962
+ /**
963
+ * Exactly what went INTO the one LLM call — every placeholder value
964
+ * substituted into `.prompts/script-match/system.md`, plus the routing
965
+ * facts (isExplicitEdit, the two counts) that aren't literal prompt text
966
+ * but explain WHY the catalog/active-script blocks look the way they do.
967
+ */
968
+ sent: {
969
+ /** Whether the raw prompt carried the explicit "edit :" prefix (see parseEditPrefix). */
970
+ isExplicitEdit: boolean;
971
+ /** The prefix-stripped question actually shown to the LLM (the USER_PROMPT placeholder). */
972
+ userPrompt: string;
973
+ /** The Edit Signal text handed to the LLM this turn (CONFIRMED/NOT CONFIRMED wording). */
974
+ editSignal: string;
975
+ /** The rendered "Scripts From Your Past Questions" catalog text (SCRIPT_CATALOG placeholder). */
976
+ scriptCatalog: string;
977
+ /** Distinct scripts actually in scriptCatalog, after health-filtering and the MAX_CATALOG_RECIPES cap. */
978
+ catalogRecipeCount: number;
979
+ /** The rendered "EDITABLE SCRIPTS" block text (ACTIVE_SCRIPT placeholder) — '' when the thread has no active bindings. */
980
+ activeScript: string;
981
+ /** Active bindings offered as edit candidates this turn. */
982
+ activeBindingCount: number;
983
+ /** This call's LLM.stream config. */
984
+ model?: string;
985
+ maxTokens?: number;
986
+ temperature?: number;
987
+ };
988
+ /**
989
+ * Everything that came back OUT — the LLM's raw answer plus what the
990
+ * runtime decided to do with it. `tier` here is the FINAL tier after
991
+ * every coercion (near→none, the edit-prefix gate, candidate checks) —
992
+ * it can legitimately differ from `rawResponse.tier`.
993
+ */
994
+ received: {
995
+ /** False when short-circuited before any LLM call (no catalog match AND no active bindings) — `rawResponse`/`tier` below don't apply. */
996
+ calledLLM: boolean;
997
+ /** The raw JSON the LLM returned, when calledLLM is true and the call didn't throw. */
998
+ rawResponse?: any;
999
+ /** Set when calledLLM is true and the call threw, or the response named an unknown script id. */
1000
+ error?: string;
1001
+ /** Final tier after ALL coercions. Null when short-circuited, errored, or the call/coercions landed on no match. */
1002
+ tier: MatchTier | null;
1003
+ };
1004
+ } | null;
1005
+ /**
1006
+ * `resolve_entities` tool activity this turn (see main-agent.ts's
1007
+ * RESOLVE_ENTITIES_TOOL_DEF / handleResolveEntities) — names/identifiers the
1008
+ * user mentioned, resolved to database ids before any SQL was written.
1009
+ * Absent/empty entirely for a project with no entity-search collection
1010
+ * registered, or a turn that named nothing.
1011
+ */
1012
+ entityResolution: {
1013
+ /**
1014
+ * The entity-type catalog text injected into MainAgent's system prompt
1015
+ * (loadEntityCatalog's output) — what let the LLM name a concept instead
1016
+ * of guessing the project's vocabulary. Captured once per turn.
1017
+ */
1018
+ catalogText?: string;
1019
+ /**
1020
+ * One entry per `resolve_entities` tool call this turn (a turn can call it
1021
+ * more than once) — the FULL raw response from the entity-search
1022
+ * collection, not just the trimmed {mention,entityType,instanceId,
1023
+ * displayName} MainAgent keeps for itself to build the dispatch block.
1024
+ * `score`/`ambiguous`/`alternatives` only exist here, nowhere else.
1025
+ */
1026
+ calls: Array<{
1027
+ mentions: Array<{
1028
+ text: string;
1029
+ entityType?: string;
1030
+ }>;
1031
+ entityTypes: string[];
1032
+ resolved: Array<{
1033
+ mention: string;
1034
+ entityType: string;
1035
+ instanceId: string;
1036
+ displayName: string;
1037
+ attrs?: Record<string, any>;
1038
+ score?: number;
1039
+ ambiguous?: boolean;
1040
+ alternatives?: Array<{
1041
+ instanceId: string;
1042
+ displayName: string;
1043
+ score: number;
1044
+ }>;
1045
+ }>;
1046
+ unresolved: string[];
1047
+ entityMap: Record<string, any>;
1048
+ unmatchedEntityTypes?: Array<{
1049
+ requested: string;
1050
+ candidates?: string[];
1051
+ }>;
1052
+ /** Set when the collection call itself failed (fails open — see handleResolveEntities). */
1053
+ error?: string;
1054
+ /** Set on an early return (unavailable / nothing named) so the call is still visible in the trace, not indistinguishable from never having run. */
1055
+ note?: string;
1056
+ }>;
1057
+ };
1058
+ /**
1059
+ * Direct-tool activity this turn (main-agent.ts's handleDirectTool) — one
1060
+ * entry per direct-tool call, unconditional and generic across every
1061
+ * project's direct tools, not specific to one. For fusion-5's `concept`
1062
+ * tool this is what actually traces find/get/run (distinguished via
1063
+ * `input.method`) — unlike `concepts.calls` above, which only the
1064
+ * dashboard-agent flow's own, separate concept tool still populates;
1065
+ * MainAgent no longer resolves concepts itself, so nothing else writes
1066
+ * here for a chat/report turn.
1067
+ */
1068
+ directTools: {
1069
+ calls: Array<{
1070
+ toolId: string;
1071
+ /** Exact params the LLM called this tool with. */
1072
+ input: any;
1073
+ /** The tool's raw return value. Absent when `error` is set. */
1074
+ output?: any;
1075
+ /** Set whether the tool's own result carried an `error` field or the call threw. */
1076
+ error?: string;
1077
+ durationMs?: number;
1078
+ }>;
1079
+ };
1080
+ /**
1081
+ * The prior-turn conversation context injected into this turn's prompt as
1082
+ * CONVERSATION_HISTORY — exactly what each flow already builds for itself
1083
+ * (chat: thread.getConversationContext(); report/dashboard: their own
1084
+ * equivalents), captured as-is. Every call site sets this unconditionally
1085
+ * (empty string '' when there's no prior context — new thread, or history
1086
+ * trimmed to nothing), so an empty turn is visibly "no history" in the
1087
+ * saved trace rather than a silently missing key. Optional only because
1088
+ * traces saved before this field existed won't have it at all.
1089
+ */
1090
+ conversationHistory?: string;
1091
+ /**
1092
+ * The exact {{USER_ACCESS_CONTEXT}} text injected into MainAgent's system
1093
+ * prompt — the per-user authorization config (sa-api users.config)
1094
+ * rendered by buildUserAccessContext, as-is. Captured once (MainAgent's
1095
+ * prompt is what SourceAgent's identical value is derived from — no need
1096
+ * to duplicate it per source dispatch). Set unconditionally (empty string
1097
+ * '' when there's no config for this user), so "no restrictions" is
1098
+ * visible in the saved trace rather than a silently missing key. Optional
1099
+ * only because traces saved before this field existed won't have it.
1100
+ */
1101
+ userAccessContext?: string;
1102
+ /**
1103
+ * The exact per-user knowledge-base text injected into MainAgent's system
1104
+ * prompt (getUserKnowledgeBase's output, folded into KNOWLEDGE_BASE_CONTEXT
1105
+ * alongside the query-matched half already captured in kbNodes.query).
1106
+ * Captured separately because it's a distinct fetch with no node-level
1107
+ * structure to list, unlike kbNodes.
1108
+ */
1109
+ userKbContext?: string;
1110
+ /** 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. */
1111
+ toolsSent?: any[];
1112
+ /** The exact {{DIRECT_TOOLS}} text injected into MainAgent's system prompt. */
1113
+ directToolsText?: string;
1114
+ /** The exact {{WORKFLOW_COMPONENTS}} text injected into MainAgent's system prompt. */
1115
+ workflowsText?: string;
1116
+ /** The exact {{SCRIPT_BODY_CONTRACT}} text injected into MainAgent's system prompt (promptLoader.loadScriptBodyContract). */
1117
+ scriptBodyContractText?: string;
1118
+ /**
1119
+ * Full edit-mode context as sent to MainAgent this turn — every EditContext
1120
+ * field, not just the instruction already folded into `script.reasoning`.
1121
+ * Null for a non-edit turn.
1122
+ */
1123
+ editContext?: {
1124
+ recipeId?: string;
1125
+ parentName?: string;
1126
+ parentBody?: string;
1127
+ instruction?: string;
1128
+ lastResultColumns?: string[];
1129
+ parentParams?: string;
1130
+ } | null;
1131
+ /**
1132
+ * The fork-mode preamble as actually assembled and prepended to the user
1133
+ * prompt (agent-user-response.ts) — parentBody/gaps/modificationHint as
1134
+ * sent, plus the lineage fields also written to the script store separately
1135
+ * (parentRecipeId/forkDepth/forkReason). Null for a non-fork turn.
1136
+ */
1137
+ forkContext?: {
1138
+ parentBody?: string;
1139
+ gaps?: string;
1140
+ modificationHint?: string;
1141
+ parentRecipeId?: string;
1142
+ forkDepth?: number;
1143
+ forkReason?: string;
1144
+ } | null;
1145
+ /** The write_analysis tool call's analysisBody argument this turn, when called. */
1146
+ writeAnalysisBody?: string;
1147
+ /** The write_components tool call's componentsBody argument this turn, when called. */
1148
+ writeComponentsBody?: string;
1149
+ /**
1150
+ * Component-generation/re-authoring activity this turn — one entry per call
1151
+ * to `authorComponentsProgram` (script-component-generator.ts — the
1152
+ * display-edit path, reauthoring getComponents as a program),
1153
+ * `generateScriptComponents` (the script-replay fallback, spec/analysis-based
1154
+ * generation), or `summarizeToMarkdown` (its own small analysis-summary LLM
1155
+ * call, reached via `assembleComponentsFromSpecs`). A turn can hit more than
1156
+ * one of these across its fallback chain, so this is an array, not a
1157
+ * singleton — same pattern as `concepts.calls`/`entityResolution.calls`.
1158
+ */
1159
+ componentGen: Array<{
1160
+ via: 'authorComponentsProgram' | 'generateScriptComponents' | 'summarizeToMarkdown';
1161
+ /** authorComponentsProgram only: the edit instruction driving the re-author. */
1162
+ instruction?: string;
1163
+ /** authorComponentsProgram only: the component body being revised, before this attempt. */
1164
+ currentBody?: string;
1165
+ /** Dataset shapes/schemas the LLM was shown (both generation paths, different derivation). */
1166
+ datasetShapes?: string;
1167
+ /** The component catalog text/list handed to the LLM. */
1168
+ componentCatalog?: string;
1169
+ /** generateScriptComponents only: source name(s) shown (SOURCE_NAME). */
1170
+ sourceName?: string;
1171
+ /** generateScriptComponents only: row count(s) shown (ROW_COUNT). */
1172
+ rowCount?: string;
1173
+ /** generateScriptComponents only: column descriptions text shown. */
1174
+ columnDescriptions?: string;
1175
+ /** generateScriptComponents/summarizeToMarkdown: the analysis text handed to the LLM. */
1176
+ analysisText?: string;
1177
+ /** This call's LLM config. */
1178
+ callConfig?: {
1179
+ model?: string;
1180
+ maxTokens?: number;
1181
+ temperature?: number;
1182
+ };
1183
+ /** authorComponentsProgram only: one entry per retry attempt — the feedback fed back in and the raw response for that attempt, in order. */
1184
+ attempts?: Array<{
1185
+ feedback?: string;
1186
+ rawResponse?: string;
1187
+ }>;
1188
+ /** The final raw LLM response text for this call (last attempt, for authorComponentsProgram's retry loop; the single response otherwise). */
1189
+ rawResponse?: string;
1190
+ }>;
1191
+ }
1192
+ /**
1193
+ * Configuration for the multi-agent system.
1194
+ * Controls limits, models, and behavior.
1195
+ */
1196
+ interface AgentConfig {
1197
+ /** Max rows shown to the UI preview / inlined per source (default: 10) */
1198
+ maxRowsPerSource: number;
1199
+ /**
1200
+ * Max rows a source query may FETCH from the DB server-side (default: 2000).
1201
+ * Decoupled from what the main agent is shown: the full result is fetched and
1202
+ * summarized (bounded), but only a small/complete slice enters LLM context.
1203
+ * This lets small lookups (benchmark maps) arrive COMPLETE without letting
1204
+ * large results blow up context.
1205
+ */
1206
+ maxRowsFetched: number;
1207
+ /** Model for the main agent (routing + analysis in one LLM call) */
1208
+ mainAgentModel: string;
1209
+ /** Model for source agent query generation */
1210
+ sourceAgentModel: string;
1211
+ /** API key for LLM calls */
1212
+ apiKey?: string;
1213
+ /** Max retry attempts per source agent */
1214
+ maxRetries: number;
1215
+ /** Max tool calling iterations for the main agent loop */
1216
+ maxIterations: number;
1217
+ /** Global knowledge base context (static, same for all users/questions — cached in system prompt) */
1218
+ globalKnowledgeBase?: string;
1219
+ /** Per-request knowledge base context (user-specific + query-matched — dynamic, not cached) */
1220
+ knowledgeBaseContext?: string;
1221
+ /** Per-user authorization config (sa-api users.config), rendered to text — see buildUserAccessContext */
1222
+ userAccessContext?: string;
1223
+ /** Collections registry (ChromaDB search hooks) for embedding-based schema + source search */
1224
+ collections?: any;
1225
+ /** Optional project ID for scoping embedding searches */
1226
+ projectId?: string;
1227
+ /**
1228
+ * Optional debug-trace accumulator for this turn (Phase 1, chat_agent only).
1229
+ * When present, MainAgent-scope retrieval call sites (e.g. preResolveSchema)
1230
+ * push their structured results into it. Absent for callers that don't care
1231
+ * about trace capture (e.g. headless flows) — every push site must treat
1232
+ * this as optional and never let a capture failure affect the real answer.
1233
+ */
1234
+ trace?: RunTrace;
1235
+ }
1236
+ /**
1237
+ * Default agent configuration
1238
+ */
1239
+ declare const DEFAULT_AGENT_CONFIG: AgentConfig;
1240
+
1241
+ /**
1242
+ * Unified UIBlock structure for database storage
1243
+ * Used in both bookmarks and user-conversations tables.
1244
+ *
1245
+ * `analysis` always holds whatever real narration exists — the full answer on
1246
+ * success, or whatever was actually streamed before a failure (may be empty).
1247
+ * `error` is a dedicated field, null on success — it can be a plain string OR
1248
+ * a structured object/array (e.g. the agent's raw `errors` list), whatever
1249
+ * shape the failure naturally has; it's never coerced into `analysis`.
1250
+ */
1251
+ interface DBUIBlock {
1252
+ id: string;
1253
+ component: Record<string, any> | null;
1254
+ analysis: string | null;
1255
+ user_prompt: string;
1256
+ error?: unknown | null;
1257
+ /**
1258
+ * The script recipe that produced this answer, when one did. Read back via
1259
+ * `conversation-history.exactMatch` so an edit follow-up still has a target
1260
+ * after a reload (the in-memory thread is gone by then), and indexed so
1261
+ * committing an edit can purge every cached answer bound to the recipe.
1262
+ */
1263
+ scriptBinding?: {
1264
+ recipeId: string;
1265
+ params?: Record<string, any>;
1266
+ name?: string;
1267
+ columns?: string[];
1268
+ };
1269
+ }
1270
+
1271
+ /**
1272
+ * Log levels in hierarchical order
1273
+ * - errors: only error logs
1274
+ * - warnings: warning + error logs
1275
+ * - info: info + warning + error logs
1276
+ * - verbose: all logs including debug
1277
+ */
1278
+ type LogLevel = 'errors' | 'warnings' | 'info' | 'verbose';
1279
+ /**
1280
+ * Logger class with environment-based log level support
1281
+ */
1282
+ declare class Logger {
1283
+ private currentLevel;
1284
+ private currentLevelPriority;
1285
+ constructor();
1286
+ /**
1287
+ * Check if a string is a valid log level
1288
+ */
1289
+ private isValidLogLevel;
1290
+ /**
1291
+ * Check if a message should be logged based on current log level
1292
+ */
1293
+ private shouldLog;
1294
+ /**
1295
+ * Get current log level
1296
+ */
1297
+ getLogLevel(): LogLevel;
1298
+ /**
1299
+ * Set log level programmatically
1300
+ */
1301
+ setLogLevel(level: LogLevel): void;
1302
+ /**
1303
+ * Log info message (shown for info and verbose levels)
1304
+ */
1305
+ info(...args: any[]): void;
1306
+ /**
1307
+ * Log error message (shown for all levels)
1308
+ */
1309
+ error(...args: any[]): void;
1310
+ /**
1311
+ * Log warning message (shown for warnings, info, and verbose levels)
1312
+ */
1313
+ warn(...args: any[]): void;
1314
+ /**
1315
+ * Log debug message (only shown for verbose level)
1316
+ */
1317
+ debug(...args: any[]): void;
1318
+ /**
1319
+ * Write to log file — each line timestamped so a request that made many
1320
+ * calls (LLM turns, tool dispatches, direct-tool runs) can be read back in
1321
+ * order, matching "know what happened when" rather than just "what
1322
+ * happened".
1323
+ */
1324
+ file(...args: any[]): void;
1325
+ /** The log file the current request is writing to — print/return this so it's easy to find afterward. */
1326
+ getCurrentLogFilePath(): string;
1327
+ /**
1328
+ * Start a fresh, uniquely-named log file for a new top-level request
1329
+ * (chat turn, dashComp request, reportComp request, ...) instead of
1330
+ * truncating one shared file — pass a short label (a prompt snippet, a
1331
+ * request type) so the resulting filename is identifiable at a glance.
1332
+ * Falls back to the untimestamped legacy path when `label` is omitted, for
1333
+ * any caller not yet updated to pass one.
1334
+ */
1335
+ clearFile(label?: string): void;
1336
+ /**
1337
+ * Log LLM method prompts with clear labeling
1338
+ */
1339
+ logLLMPrompt(methodName: string, promptType: 'system' | 'user', content: string | object | any[]): void;
1340
+ }
1341
+ declare const logger: Logger;
1342
+
1343
+ declare const DSLRendererPropsSchema$1: z.ZodObject<{
1344
+ dsl: z.ZodObject<{
1345
+ id: z.ZodString;
1346
+ name: z.ZodOptional<z.ZodString>;
1347
+ props: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
1348
+ states: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
1349
+ methods: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{
1350
+ fn: z.ZodString;
1351
+ params: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
1352
+ }, "strip", z.ZodTypeAny, {
1353
+ fn: string;
1354
+ params?: Record<string, any> | undefined;
1355
+ }, {
1356
+ fn: string;
1357
+ params?: Record<string, any> | undefined;
1358
+ }>>>;
1359
+ effects: z.ZodOptional<z.ZodArray<z.ZodObject<{
1360
+ fn: z.ZodString;
1361
+ deps: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
1362
+ }, "strip", z.ZodTypeAny, {
1363
+ fn: string;
98
1364
  deps?: string[] | undefined;
99
1365
  }, {
100
1366
  fn: string;
@@ -160,6 +1426,7 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
160
1426
  dependencies?: string[] | undefined;
161
1427
  } | undefined;
162
1428
  props?: Record<string, any> | undefined;
1429
+ data?: Record<string, any> | undefined;
163
1430
  render?: any;
164
1431
  states?: Record<string, any> | undefined;
165
1432
  methods?: Record<string, {
@@ -170,7 +1437,6 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
170
1437
  fn: string;
171
1438
  deps?: string[] | undefined;
172
1439
  }[] | undefined;
173
- data?: Record<string, any> | undefined;
174
1440
  pages?: {
175
1441
  id: string;
176
1442
  name: string;
@@ -192,6 +1458,7 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
192
1458
  dependencies?: string[] | undefined;
193
1459
  } | undefined;
194
1460
  props?: Record<string, any> | undefined;
1461
+ data?: Record<string, any> | undefined;
195
1462
  render?: any;
196
1463
  states?: Record<string, any> | undefined;
197
1464
  methods?: Record<string, {
@@ -202,7 +1469,6 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
202
1469
  fn: string;
203
1470
  deps?: string[] | undefined;
204
1471
  }[] | undefined;
205
- data?: Record<string, any> | undefined;
206
1472
  pages?: {
207
1473
  id: string;
208
1474
  name: string;
@@ -228,6 +1494,7 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
228
1494
  dependencies?: string[] | undefined;
229
1495
  } | undefined;
230
1496
  props?: Record<string, any> | undefined;
1497
+ data?: Record<string, any> | undefined;
231
1498
  render?: any;
232
1499
  states?: Record<string, any> | undefined;
233
1500
  methods?: Record<string, {
@@ -238,7 +1505,6 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
238
1505
  fn: string;
239
1506
  deps?: string[] | undefined;
240
1507
  }[] | undefined;
241
- data?: Record<string, any> | undefined;
242
1508
  pages?: {
243
1509
  id: string;
244
1510
  name: string;
@@ -264,6 +1530,7 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
264
1530
  dependencies?: string[] | undefined;
265
1531
  } | undefined;
266
1532
  props?: Record<string, any> | undefined;
1533
+ data?: Record<string, any> | undefined;
267
1534
  render?: any;
268
1535
  states?: Record<string, any> | undefined;
269
1536
  methods?: Record<string, {
@@ -274,7 +1541,6 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
274
1541
  fn: string;
275
1542
  deps?: string[] | undefined;
276
1543
  }[] | undefined;
277
- data?: Record<string, any> | undefined;
278
1544
  pages?: {
279
1545
  id: string;
280
1546
  name: string;
@@ -355,6 +1621,7 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
355
1621
  dependencies?: string[] | undefined;
356
1622
  } | undefined;
357
1623
  props?: Record<string, any> | undefined;
1624
+ data?: Record<string, any> | undefined;
358
1625
  render?: any;
359
1626
  states?: Record<string, any> | undefined;
360
1627
  methods?: Record<string, {
@@ -365,7 +1632,6 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
365
1632
  fn: string;
366
1633
  deps?: string[] | undefined;
367
1634
  }[] | undefined;
368
- data?: Record<string, any> | undefined;
369
1635
  }, {
370
1636
  id: string;
371
1637
  name?: string | undefined;
@@ -379,6 +1645,7 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
379
1645
  dependencies?: string[] | undefined;
380
1646
  } | undefined;
381
1647
  props?: Record<string, any> | undefined;
1648
+ data?: Record<string, any> | undefined;
382
1649
  render?: any;
383
1650
  states?: Record<string, any> | undefined;
384
1651
  methods?: Record<string, {
@@ -389,7 +1656,6 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
389
1656
  fn: string;
390
1657
  deps?: string[] | undefined;
391
1658
  }[] | undefined;
392
- data?: Record<string, any> | undefined;
393
1659
  }>;
394
1660
  data: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
395
1661
  context: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
@@ -407,6 +1673,7 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
407
1673
  dependencies?: string[] | undefined;
408
1674
  } | undefined;
409
1675
  props?: Record<string, any> | undefined;
1676
+ data?: Record<string, any> | undefined;
410
1677
  render?: any;
411
1678
  states?: Record<string, any> | undefined;
412
1679
  methods?: Record<string, {
@@ -417,7 +1684,6 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
417
1684
  fn: string;
418
1685
  deps?: string[] | undefined;
419
1686
  }[] | undefined;
420
- data?: Record<string, any> | undefined;
421
1687
  };
422
1688
  data?: Record<string, any> | undefined;
423
1689
  context?: Record<string, any> | undefined;
@@ -435,6 +1701,7 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
435
1701
  dependencies?: string[] | undefined;
436
1702
  } | undefined;
437
1703
  props?: Record<string, any> | undefined;
1704
+ data?: Record<string, any> | undefined;
438
1705
  render?: any;
439
1706
  states?: Record<string, any> | undefined;
440
1707
  methods?: Record<string, {
@@ -445,7 +1712,6 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
445
1712
  fn: string;
446
1713
  deps?: string[] | undefined;
447
1714
  }[] | undefined;
448
- data?: Record<string, any> | undefined;
449
1715
  };
450
1716
  data?: Record<string, any> | undefined;
451
1717
  context?: Record<string, any> | undefined;
@@ -638,19 +1904,16 @@ declare const ComponentSchema: z.ZodObject<{
638
1904
  title: z.ZodOptional<z.ZodString>;
639
1905
  description: z.ZodOptional<z.ZodString>;
640
1906
  config: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
641
- actions: z.ZodOptional<z.ZodArray<z.ZodAny, "many">>;
642
1907
  }, "passthrough", z.ZodTypeAny, z.objectOutputType<{
643
1908
  query: z.ZodOptional<z.ZodNullable<z.ZodUnion<[z.ZodString, z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>]>>>;
644
1909
  title: z.ZodOptional<z.ZodString>;
645
1910
  description: z.ZodOptional<z.ZodString>;
646
1911
  config: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
647
- actions: z.ZodOptional<z.ZodArray<z.ZodAny, "many">>;
648
1912
  }, z.ZodTypeAny, "passthrough">, z.objectInputType<{
649
1913
  query: z.ZodOptional<z.ZodNullable<z.ZodUnion<[z.ZodString, z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>]>>>;
650
1914
  title: z.ZodOptional<z.ZodString>;
651
1915
  description: z.ZodOptional<z.ZodString>;
652
1916
  config: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
653
- actions: z.ZodOptional<z.ZodArray<z.ZodAny, "many">>;
654
1917
  }, z.ZodTypeAny, "passthrough">>;
655
1918
  category: z.ZodOptional<z.ZodString>;
656
1919
  keywords: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
@@ -664,7 +1927,6 @@ declare const ComponentSchema: z.ZodObject<{
664
1927
  query?: string | {} | null | undefined;
665
1928
  title?: string | undefined;
666
1929
  config?: Record<string, unknown> | undefined;
667
- actions?: any[] | undefined;
668
1930
  } & {
669
1931
  [k: string]: unknown;
670
1932
  };
@@ -682,7 +1944,6 @@ declare const ComponentSchema: z.ZodObject<{
682
1944
  query?: string | {} | null | undefined;
683
1945
  title?: string | undefined;
684
1946
  config?: Record<string, unknown> | undefined;
685
- actions?: any[] | undefined;
686
1947
  } & {
687
1948
  [k: string]: unknown;
688
1949
  };
@@ -861,6 +2122,10 @@ interface SuperatomSDKConfig {
861
2122
  bundleDir?: string;
862
2123
  promptsDir?: string;
863
2124
  databaseType?: DatabaseType;
2125
+ /** BCP-47 locale for `fmt` in generated analyses, e.g. 'en-IN', 'en-US'. Defaults to 'en-IN'. */
2126
+ locale?: string;
2127
+ /** ISO-4217 currency for `fmt`, e.g. 'INR', 'USD'. Defaults to 'INR'. */
2128
+ currency?: string;
864
2129
  ANTHROPIC_API_KEY?: string;
865
2130
  GROQ_API_KEY?: string;
866
2131
  GEMINI_API_KEY?: string;
@@ -1008,7 +2273,7 @@ declare const KbNodesRequestPayloadSchema: z.ZodObject<{
1008
2273
  content?: string | undefined;
1009
2274
  }>>;
1010
2275
  }, "strip", z.ZodTypeAny, {
1011
- operation: "create" | "getOne" | "update" | "delete" | "getAll" | "search" | "getByCategory" | "getByUser" | "getCategories" | "getTags";
2276
+ operation: "create" | "getOne" | "search" | "update" | "delete" | "getAll" | "getByCategory" | "getByUser" | "getCategories" | "getTags";
1012
2277
  data?: {
1013
2278
  id?: number | undefined;
1014
2279
  type?: "query" | "user" | "global" | undefined;
@@ -1031,7 +2296,7 @@ declare const KbNodesRequestPayloadSchema: z.ZodObject<{
1031
2296
  content?: string | undefined;
1032
2297
  } | undefined;
1033
2298
  }, {
1034
- operation: "create" | "getOne" | "update" | "delete" | "getAll" | "search" | "getByCategory" | "getByUser" | "getCategories" | "getTags";
2299
+ operation: "create" | "getOne" | "search" | "update" | "delete" | "getAll" | "getByCategory" | "getByUser" | "getCategories" | "getTags";
1035
2300
  data?: {
1036
2301
  id?: number | undefined;
1037
2302
  type?: "query" | "user" | "global" | undefined;
@@ -1222,549 +2487,247 @@ declare class DashboardManager {
1222
2487
  */
1223
2488
  getDashboard(dashboardId: string): DSLRendererProps$1 | null;
1224
2489
  /**
1225
- * Get all dashboards
1226
- * @returns Array of dashboard objects with their IDs
1227
- */
1228
- getAllDashboards(): Array<{
1229
- dashboardId: string;
1230
- dashboard: DSLRendererProps$1;
1231
- }>;
1232
- /**
1233
- * Update an existing dashboard
1234
- * @param dashboardId - Dashboard ID
1235
- * @param dashboard - Updated dashboard data
1236
- * @returns Updated dashboard or null if not found
1237
- */
1238
- updateDashboard(dashboardId: string, dashboard: DSLRendererProps$1): DSLRendererProps$1 | null;
1239
- /**
1240
- * Delete a dashboard
1241
- * @param dashboardId - Dashboard ID
1242
- * @returns True if deleted, false if not found
1243
- */
1244
- deleteDashboard(dashboardId: string): boolean;
1245
- /**
1246
- * Check if a dashboard exists
1247
- * @param dashboardId - Dashboard ID
1248
- * @returns True if dashboard exists, false otherwise
1249
- */
1250
- dashboardExists(dashboardId: string): boolean;
1251
- /**
1252
- * Get dashboard count
1253
- * @returns Number of dashboards
1254
- */
1255
- getDashboardCount(): number;
1256
- }
1257
-
1258
- /**
1259
- * ReportManager class to handle CRUD operations on reports
1260
- * All operations read/write directly to files (no in-memory caching)
1261
- */
1262
- declare class ReportManager {
1263
- private reportsBasePath;
1264
- private projectId;
1265
- /**
1266
- * Initialize ReportManager with project ID
1267
- * @param projectId - Project ID to use in file path
1268
- */
1269
- constructor(projectId?: string);
1270
- /**
1271
- * Get the file path for a specific report
1272
- * @param reportId - Report ID
1273
- * @returns Full path to report data.json file
1274
- */
1275
- private getReportPath;
1276
- /**
1277
- * Create a new report
1278
- * @param reportId - Unique report ID
1279
- * @param report - Report data
1280
- * @returns Created report with metadata
1281
- */
1282
- createReport(reportId: string, report: DSLRendererProps): DSLRendererProps;
1283
- /**
1284
- * Get a specific report by ID
1285
- * @param reportId - Report ID
1286
- * @returns Report data or null if not found
1287
- */
1288
- getReport(reportId: string): DSLRendererProps | null;
1289
- /**
1290
- * Get all reports
1291
- * @returns Array of report objects with their IDs
1292
- */
1293
- getAllReports(): Array<{
1294
- reportId: string;
1295
- report: DSLRendererProps;
1296
- }>;
1297
- /**
1298
- * Update an existing report
1299
- * @param reportId - Report ID
1300
- * @param report - Updated report data
1301
- * @returns Updated report or null if not found
1302
- */
1303
- updateReport(reportId: string, report: DSLRendererProps): DSLRendererProps | null;
1304
- /**
1305
- * Delete a report
1306
- * @param reportId - Report ID
1307
- * @returns True if deleted, false if not found
1308
- */
1309
- deleteReport(reportId: string): boolean;
1310
- /**
1311
- * Check if a report exists
1312
- * @param reportId - Report ID
1313
- * @returns True if report exists, false otherwise
1314
- */
1315
- reportExists(reportId: string): boolean;
1316
- /**
1317
- * Get report count
1318
- * @returns Number of reports
1319
- */
1320
- getReportCount(): number;
1321
- }
1322
-
1323
- /**
1324
- * StreamBuffer - Buffered streaming utility for smoother text delivery
1325
- * Batches small chunks together and flushes at regular intervals
1326
- */
1327
- type StreamCallback = (chunk: string) => void;
1328
- /**
1329
- * StreamBuffer class for managing buffered streaming output
1330
- * Provides smooth text delivery by batching small chunks
1331
- */
1332
- declare class StreamBuffer {
1333
- private buffer;
1334
- private flushTimer;
1335
- private callback;
1336
- private fullText;
1337
- constructor(callback?: StreamCallback);
1338
- /**
1339
- * Check if the buffer has a callback configured
1340
- */
1341
- hasCallback(): boolean;
1342
- /**
1343
- * Get all text that has been written (including already flushed)
2490
+ * Get all dashboards
2491
+ * @returns Array of dashboard objects with their IDs
1344
2492
  */
1345
- getFullText(): string;
2493
+ getAllDashboards(): Array<{
2494
+ dashboardId: string;
2495
+ dashboard: DSLRendererProps$1;
2496
+ }>;
1346
2497
  /**
1347
- * Write a chunk to the buffer
1348
- * Large chunks or chunks with newlines are flushed immediately
1349
- * Small chunks are batched and flushed after a short interval
1350
- *
1351
- * @param chunk - Text chunk to write
2498
+ * Update an existing dashboard
2499
+ * @param dashboardId - Dashboard ID
2500
+ * @param dashboard - Updated dashboard data
2501
+ * @returns Updated dashboard or null if not found
1352
2502
  */
1353
- write(chunk: string): void;
2503
+ updateDashboard(dashboardId: string, dashboard: DSLRendererProps$1): DSLRendererProps$1 | null;
1354
2504
  /**
1355
- * Flush the buffer immediately
1356
- * Call this before tool execution or other operations that need clean output
2505
+ * Delete a dashboard
2506
+ * @param dashboardId - Dashboard ID
2507
+ * @returns True if deleted, false if not found
1357
2508
  */
1358
- flush(): void;
2509
+ deleteDashboard(dashboardId: string): boolean;
1359
2510
  /**
1360
- * Internal flush implementation
2511
+ * Check if a dashboard exists
2512
+ * @param dashboardId - Dashboard ID
2513
+ * @returns True if dashboard exists, false otherwise
1361
2514
  */
1362
- private flushNow;
2515
+ dashboardExists(dashboardId: string): boolean;
1363
2516
  /**
1364
- * Clean up resources
1365
- * Call this when done with the buffer
2517
+ * Get dashboard count
2518
+ * @returns Number of dashboards
1366
2519
  */
1367
- dispose(): void;
1368
- }
1369
-
1370
- /**
1371
- * ToolExecutorService - Handles execution of SQL queries and external tools
1372
- * Extracted from BaseLLM.generateTextResponse for better separation of concerns
1373
- */
1374
-
1375
- /**
1376
- * External tool definition
1377
- */
1378
- interface ExternalTool {
1379
- id: string;
1380
- name: string;
1381
- description?: string;
1382
- /** Tool type: "source" = routed through SourceAgent, "direct" = called directly by MainAgent */
1383
- toolType?: 'source' | 'direct';
1384
- /** Full untruncated schema for source agent (all columns visible) */
1385
- fullSchema?: string;
1386
- /** Schema size tier: small (≤50 tables), medium (51-200), large (201-500), very_large (500+) */
1387
- schemaTier?: string;
1388
- /** Schema search function for very_large tier — keyword search over entities */
1389
- schemaSearchFn?: (keywords: string[]) => string;
1390
- fn: (input: any) => Promise<any>;
1391
- limit?: number;
1392
- outputSchema?: any;
1393
- executionType?: 'immediate' | 'deferred';
1394
- userProvidedData?: any;
1395
- params?: Record<string, any>;
1396
- }
1397
- /**
1398
- * Executed tool tracking info
1399
- */
1400
- interface ExecutedToolInfo {
1401
- id: string;
1402
- name: string;
1403
- params: any;
1404
- result: {
1405
- _totalRecords: number;
1406
- _recordsShown: number;
1407
- _metadata?: any;
1408
- _sampleData: any[];
1409
- };
1410
- outputSchema?: any;
1411
- sourceSchema?: string;
1412
- sourceType?: string;
2520
+ getDashboardCount(): number;
1413
2521
  }
1414
2522
 
1415
2523
  /**
1416
- * Multi-Agent Architecture Types
1417
- *
1418
- * Defines interfaces for the hierarchical agent system:
1419
- * - Main Agent: ONE LLM.streamWithTools() call with source agent tools
1420
- * - Source Agents: independent agents that query individual data sources
1421
- *
1422
- * The main agent sees only source summaries. When it calls a source tool,
1423
- * the SourceAgent runs independently (own LLM, own retries) and returns clean data.
1424
- */
1425
-
1426
- /**
1427
- * Per-entity detail: name, row count, and column names.
1428
- * Gives the main agent enough context to route to the right source.
1429
- */
1430
- interface EntityDetail {
1431
- /** Entity name (table, sheet, endpoint) */
1432
- name: string;
1433
- /** Approximate row count */
1434
- rowCount?: number;
1435
- /** Column/field names */
1436
- columns: string[];
1437
- }
1438
- /**
1439
- * Representation of a data source for the main agent.
1440
- * Contains entity names WITH column names so the LLM can route accurately.
1441
- */
1442
- interface SourceSummary {
1443
- /** Source ID (matches tool ID prefix) */
1444
- id: string;
1445
- /** Human-readable source name */
1446
- name: string;
1447
- /** Source type: postgres, excel, rest_api, etc. */
1448
- type: string;
1449
- /** Brief description of what data this source contains */
1450
- description: string;
1451
- /** Detailed entity info with column names for routing */
1452
- entityDetails: EntityDetail[];
1453
- /** The tool ID associated with this source */
1454
- toolId: string;
1455
- }
1456
- /**
1457
- * What a source agent returns after querying its data source.
1458
- * The main agent uses this to analyze and compose the final response.
1459
- */
1460
- interface SourceAgentResult {
1461
- /** Source ID */
1462
- sourceId: string;
1463
- /** Source name */
1464
- sourceName: string;
1465
- /** Whether the query succeeded */
1466
- success: boolean;
1467
- /** Result data rows */
1468
- data: any[];
1469
- /** Metadata about the query execution */
1470
- metadata: SourceAgentMetadata;
1471
- /** Tool execution info for the last successful query (backward compat) */
1472
- executedTool: ExecutedToolInfo;
1473
- /** All successful tool executions (primary + follow-up queries) */
1474
- allExecutedTools?: ExecutedToolInfo[];
1475
- /** Error message if failed */
1476
- error?: string;
1477
- }
1478
- interface SourceAgentMetadata {
1479
- /** Total rows that matched the query (before limit) */
1480
- totalRowsMatched: number;
1481
- /** Rows actually returned (after limit) */
1482
- rowsReturned: number;
1483
- /** Whether the result was truncated by the row limit */
1484
- isLimited: boolean;
1485
- /** The query/params that were executed */
1486
- queryExecuted?: string;
1487
- /** Execution time in milliseconds */
1488
- executionTimeMs: number;
1489
- }
1490
- /**
1491
- * A pre-built, multi-step UI flow registered with the SDK.
1492
- *
1493
- * When the main agent decides a user's question matches a workflow's whenToUse
1494
- * trigger, it picks the workflow instead of running source agents / generating
1495
- * dashboard components. The LLM extracts the workflow's required props from the
1496
- * prompt (using `propsSchema` as the tool input_schema) and the SDK returns the
1497
- * workflow component directly — no analysis text, no chart generation. The
1498
- * frontend renders the registered workflow component with the LLM-extracted
1499
- * props.
2524
+ * ReportManager class to handle CRUD operations on reports
2525
+ * All operations read/write directly to files (no in-memory caching)
1500
2526
  */
1501
- interface WorkflowDescriptor {
1502
- /** Unique workflow id (used as the LLM tool name) */
1503
- id: string;
1504
- /** Component name on the frontend (matches the registered React component) */
1505
- name: string;
1506
- /** Short human-readable description of what this workflow does */
1507
- description: string;
2527
+ declare class ReportManager {
2528
+ private reportsBasePath;
2529
+ private projectId;
1508
2530
  /**
1509
- * 1–2 sentence trigger condition. The LLM uses this to decide if the
1510
- * user's prompt matches this workflow. Be specific — e.g.
1511
- * "User wants to *initiate* an inventory transfer (review + submit POs),
1512
- * not just see analysis or charts."
2531
+ * Initialize ReportManager with project ID
2532
+ * @param projectId - Project ID to use in file path
1513
2533
  */
1514
- whenToUse: string;
2534
+ constructor(projectId?: string);
1515
2535
  /**
1516
- * JSON-schema-style description of the props the workflow needs. Becomes
1517
- * the LLM tool's input_schema, so the model fills these from the prompt.
1518
- * Use the same shape as `params` on direct tools — string descriptors with
1519
- * an optional "(optional)" suffix.
1520
- *
1521
- * Example:
1522
- * ```
1523
- * {
1524
- * selectedStore: 'object — { id, name } of the source branch',
1525
- * minROI: 'number (optional) — only show transfers with ROI ≥ this',
1526
- * }
1527
- * ```
2536
+ * Get the file path for a specific report
2537
+ * @param reportId - Report ID
2538
+ * @returns Full path to report data.json file
1528
2539
  */
1529
- propsSchema: Record<string, string>;
2540
+ private getReportPath;
1530
2541
  /**
1531
- * Optional: static prop defaults merged with LLM-extracted props before
1532
- * the component is returned. Useful for things like the embedded
1533
- * `externalTool` config that the workflow uses to fetch its own data.
2542
+ * Create a new report
2543
+ * @param reportId - Unique report ID
2544
+ * @param report - Report data
2545
+ * @returns Created report with metadata
1534
2546
  */
1535
- defaultProps?: Record<string, any>;
1536
- }
1537
- /**
1538
- * The workflow selection captured during a routing call.
1539
- * Set on AgentResponse when the LLM picks a workflow tool.
1540
- */
1541
- interface SelectedWorkflow {
1542
- /** Component name (matches WorkflowDescriptor.name) */
1543
- name: string;
1544
- /** Props extracted from the prompt + merged with workflow.defaultProps */
1545
- props: Record<string, any>;
1546
- }
1547
- /**
1548
- * The complete response from the multi-agent system.
1549
- * Contains everything needed for text display + component generation.
1550
- */
1551
- interface AgentResponse {
1552
- /** Generated text response (analysis of the data) */
1553
- text: string;
1554
- /** All executed tools across all source agents (for component generation) */
1555
- executedTools: ExecutedToolInfo[];
1556
- /** Individual results from each source agent */
1557
- sourceResults: SourceAgentResult[];
2547
+ createReport(reportId: string, report: DSLRendererProps): DSLRendererProps;
1558
2548
  /**
1559
- * Populated when MainAgent wrote AND successfully executed a script during its turn.
1560
- * Caller (agent-user-response.ts) persists it via ScriptStore.save().
1561
- * Absent when MainAgent didn't write one (trivial question / all attempts failed).
2549
+ * Get a specific report by ID
2550
+ * @param reportId - Report ID
2551
+ * @returns Report data or null if not found
2552
+ */
2553
+ getReport(reportId: string): DSLRendererProps | null;
2554
+ /**
2555
+ * Get all reports
2556
+ * @returns Array of report objects with their IDs
2557
+ */
2558
+ getAllReports(): Array<{
2559
+ reportId: string;
2560
+ report: DSLRendererProps;
2561
+ }>;
2562
+ /**
2563
+ * Update an existing report
2564
+ * @param reportId - Report ID
2565
+ * @param report - Updated report data
2566
+ * @returns Updated report or null if not found
2567
+ */
2568
+ updateReport(reportId: string, report: DSLRendererProps): DSLRendererProps | null;
2569
+ /**
2570
+ * Delete a report
2571
+ * @param reportId - Report ID
2572
+ * @returns True if deleted, false if not found
1562
2573
  */
1563
- savedScript?: AgentWrittenScript;
2574
+ deleteReport(reportId: string): boolean;
1564
2575
  /**
1565
- * Set when the LLM routed the question to a registered workflow component.
1566
- * When present, the upstream caller should skip component generation and
1567
- * return this workflow as the response.
2576
+ * Check if a report exists
2577
+ * @param reportId - Report ID
2578
+ * @returns True if report exists, false otherwise
1568
2579
  */
1569
- workflow?: SelectedWorkflow;
1570
- }
1571
- /**
1572
- * A script MainAgent authored + verified during its turn. Shape aligns with
1573
- * what ScriptStore.save() needs — minus store-assigned fields (id, timestamps, counts).
1574
- */
1575
- interface AgentWrittenScript {
2580
+ reportExists(reportId: string): boolean;
1576
2581
  /**
1577
- * `ScriptRecipe.id` of the draft that was authored + verified during this turn.
1578
- * The caller passes this to `ScriptStore.promoteToVerified(recipeId, …)` to
1579
- * flip the draft to verified status and (when possible) drop the turn-suffix
1580
- * from its filename.
2582
+ * Get report count
2583
+ * @returns Number of reports
1581
2584
  */
1582
- recipeId: string;
1583
- name: string;
1584
- intentDescription: string;
1585
- tags: string[];
1586
- parameters: Array<{
1587
- name: string;
1588
- type: 'string' | 'number' | 'date' | 'date_range' | 'enum' | 'boolean';
1589
- required: boolean;
1590
- default?: any;
1591
- enumValues?: Record<string, string>;
1592
- description: string;
1593
- }>;
1594
- scriptBody: string;
1595
- /** Source IDs referenced by the script (extracted from ctx.query calls) */
1596
- sourceIds: string[];
1597
- /** Tables referenced in the script's SQL (regex-extracted) */
1598
- tables: string[];
1599
- /** Executed queries from the verified run — fed to component generation */
1600
- executedQueries: Array<{
1601
- sourceId: string;
1602
- sourceName: string;
1603
- sql: string;
1604
- data: any[];
1605
- count: number;
1606
- totalCount?: number;
1607
- executionTimeMs: number;
1608
- /**
1609
- * True for synthetic entries (ctx.emit datasets, the computed:_final
1610
- * post-JS data). The component generator routes virtual sources through
1611
- * the script_dataset sentinel toolId so the frontend resolves them via
1612
- * queryCache instead of attempting to re-execute SQL.
1613
- */
1614
- virtual?: boolean;
1615
- }>;
1616
- }
1617
- /**
1618
- * Configuration for the multi-agent system.
1619
- * Controls limits, models, and behavior.
1620
- */
1621
- interface AgentConfig {
1622
- /** Max rows a source agent can return (default: 50) */
1623
- maxRowsPerSource: number;
1624
- /** Model for the main agent (routing + analysis in one LLM call) */
1625
- mainAgentModel: string;
1626
- /** Model for source agent query generation */
1627
- sourceAgentModel: string;
1628
- /** API key for LLM calls */
1629
- apiKey?: string;
1630
- /** Max retry attempts per source agent */
1631
- maxRetries: number;
1632
- /** Max tool calling iterations for the main agent loop */
1633
- maxIterations: number;
1634
- /** Global knowledge base context (static, same for all users/questions — cached in system prompt) */
1635
- globalKnowledgeBase?: string;
1636
- /** Per-request knowledge base context (user-specific + query-matched — dynamic, not cached) */
1637
- knowledgeBaseContext?: string;
1638
- /** Collections registry (ChromaDB search hooks) for embedding-based schema + source search */
1639
- collections?: any;
1640
- /** Optional project ID for scoping embedding searches */
1641
- projectId?: string;
2585
+ getReportCount(): number;
1642
2586
  }
1643
- /**
1644
- * Default agent configuration
1645
- */
1646
- declare const DEFAULT_AGENT_CONFIG: AgentConfig;
1647
2587
 
1648
2588
  /**
1649
- * Script Flow Types
2589
+ * ScriptRecipeStore — injected metadata backend for the script flow.
1650
2590
  *
1651
- * Defines interfaces for the script-based query architecture:
1652
- * - ScriptRecipe: metadata for matching, validation, and quality tracking
1653
- * - ScriptResult: output from executing a script
1654
- * - ScriptMatch: result from the LLM-based script matcher
1655
- */
1656
- /**
1657
- * Recipe metadata stored alongside each script.
1658
- * Used for matching, validation, and quality tracking.
2591
+ * The SDK is standalone (no DB dependency). The backend implements this
2592
+ * interface over Postgres (full-text search + atomic counters) and injects it
2593
+ * via `collections['script-recipes']`, exactly like `collections['source-embeddings']`.
2594
+ * `ScriptStore` consumes it for all METADATA operations while keeping the
2595
+ * executable body on disk as scripts-store/<fileBase>.ts.
2596
+ *
2597
+ * All metadata rows are plain JSON (no scriptBody — that lives on disk).
2598
+ * See backend/docs/SCRIPT-FLOW-SCALING-ISSUES.md (#1, #3, #7).
1659
2599
  */
1660
- interface ScriptRecipe {
1661
- /** Unique script identifier */
2600
+
2601
+ /** One recipe's metadata as stored in Postgres (mirrors the script_recipes table). */
2602
+ interface ScriptRecipeMetaRow {
1662
2603
  id: string;
1663
- /** Version number (incremented on regeneration) */
2604
+ projectId?: string | null;
1664
2605
  version: number;
1665
- /** Human-readable name (e.g., "Revenue by Dimension") */
1666
2606
  name: string;
1667
- /** Natural language description of what this script does */
1668
2607
  intentDescription: string;
1669
- /** Keyword tags for quick filtering */
1670
- tags: string[];
1671
- /** Source tool IDs this script queries (e.g., ["mssql-abc123_query"]) */
1672
- sourceIds: string[];
1673
- /** Table names used (for future schema drift detection) */
1674
- tables: string[];
1675
- /** Parameter definitions — what can vary */
1676
- parameters: ScriptParameter[];
1677
- /** The script function body as a string */
1678
- scriptBody: string;
1679
- /** Times this script was used successfully */
2608
+ tags: string[] | null;
2609
+ createdFrom: string | null;
2610
+ sourceIds: string[] | null;
2611
+ tables: string[] | null;
2612
+ parameters: ScriptParameter[] | null;
2613
+ components?: ScriptComponentSpec[] | null;
2614
+ displayPreference?: ScriptDisplayPreference | null;
2615
+ fileBase: string;
2616
+ bodyHash?: string | null;
1680
2617
  successCount: number;
1681
- /** Times this script failed */
1682
2618
  failureCount: number;
1683
- /** ISO timestamp of last usage */
1684
- lastUsed: string;
1685
- /** Original user question that created this script */
1686
- createdFrom: string;
1687
- /** ISO timestamp */
1688
- createdAt: string;
1689
- /** ISO timestamp */
1690
- updatedAt: string;
1691
- /**
1692
- * `recipe.id` of the parent this script was forked from.
1693
- * Undefined for root scripts (those written from scratch by MainAgent).
1694
- * See backend/docs/SCRIPT-FLOW-FORK-ADAPT.md.
1695
- */
1696
- parentId?: string;
1697
- /** 0 for root scripts; `parent.forkDepth + 1` for forks. Capped at 3. */
1698
- forkDepth?: number;
1699
- /**
1700
- * Brief description of what this fork changed vs its parent
1701
- * (sourced from the matcher's `modificationHint`).
1702
- */
1703
- forkReason?: string;
1704
- /**
1705
- * Lifecycle stage of this recipe on disk.
1706
- * - 'draft': written by MainAgent's write_script during a turn; filtered out
1707
- * of `ScriptStore.getAll()` so the matcher never picks it.
1708
- * Filename is suffixed with `turnId` to keep concurrent turns
1709
- * from clobbering each other's drafts.
1710
- * - 'verified': promoted after `execute_script` succeeded; the matcher sees it.
1711
- * Filename drops the turn suffix unless a verified file with
1712
- * the same slug already exists (collision case keeps the suffix).
1713
- *
1714
- * Recipes loaded from disk without this field default to 'verified' so
1715
- * existing scripts keep working unchanged.
1716
- */
1717
- status?: 'draft' | 'verified';
1718
- /**
1719
- * Per-turn unique suffix used for draft filenames (e.g. `1714745623-x9k2`).
1720
- * Set when the draft is saved; carried until the recipe is promoted.
1721
- */
1722
- turnId?: string;
1723
- /**
1724
- * Last execution error captured by `recordDraftError` while the recipe was
1725
- * still a draft. Lets users open the draft .json file and see why it failed
1726
- * without grepping logs. Cleared on promotion to 'verified'.
1727
- */
2619
+ lastUsed: string | null;
2620
+ parentId?: string | null;
2621
+ forkDepth?: number | null;
2622
+ forkReason?: string | null;
2623
+ status: 'draft' | 'verified' | string;
2624
+ turnId?: string | null;
2625
+ editedBy?: string | null;
2626
+ editedFrom?: string | null;
2627
+ history?: ScriptVersionRecord[] | null;
1728
2628
  lastError?: {
1729
2629
  phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
1730
2630
  message: string;
1731
2631
  at: string;
1732
2632
  attempt: number;
1733
- };
2633
+ } | null;
2634
+ createdAt?: string | null;
2635
+ updatedAt?: string | null;
1734
2636
  }
1735
- interface ScriptParameter {
1736
- /** Parameter name (used in script body as params.name) */
1737
- name: string;
1738
- /** Parameter type */
1739
- type: 'string' | 'number' | 'date' | 'date_range' | 'enum' | 'boolean';
1740
- /** Whether this parameter is required */
1741
- required: boolean;
1742
- /** Default value if not provided */
1743
- default?: any;
1744
- /** For enum type — maps user-facing values to internal values */
1745
- enumValues?: Record<string, string>;
1746
- /** Human-readable description (used in the matcher LLM prompt) */
1747
- description: string;
2637
+ interface ScriptRecipeStore {
2638
+ /** FTS shortlist of healthy verified recipes for the matcher (metadata only). */
2639
+ search(params: {
2640
+ prompt: string;
2641
+ projectId?: string;
2642
+ limit?: number;
2643
+ }): Promise<ScriptRecipeMetaRow[]>;
2644
+ /** Fetch one recipe by id (any status). */
2645
+ getById(id: string): Promise<ScriptRecipeMetaRow | null>;
2646
+ /** Count healthy verified recipes (drives the "any scripts?" gate). */
2647
+ count(params?: {
2648
+ projectId?: string;
2649
+ }): Promise<number>;
2650
+ /** Insert or update a recipe row (keyed by id). */
2651
+ upsert(row: ScriptRecipeMetaRow): Promise<void>;
2652
+ /** Atomically bump counters / last-used. */
2653
+ updateStats(id: string, patch: {
2654
+ successDelta?: number;
2655
+ failureDelta?: number;
2656
+ lastUsed?: string;
2657
+ }): Promise<void>;
2658
+ /** Flip a draft to verified, applying provenance + optional fork lineage. */
2659
+ promote(id: string, patch: {
2660
+ sourceIds: string[];
2661
+ tables: string[];
2662
+ fileBase?: string;
2663
+ parentId?: string;
2664
+ forkDepth?: number;
2665
+ forkReason?: string;
2666
+ components?: ScriptComponentSpec[];
2667
+ }): Promise<ScriptRecipeMetaRow | null>;
2668
+ /**
2669
+ * Commit a verified edit onto an EXISTING recipe: bump `version`, replace the
2670
+ * body-bearing metadata, append a history record, reset health counters, and
2671
+ * clear the component specs (they were validated against the old shape).
2672
+ * Returns the updated row, or null when the target is gone.
2673
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 5.
2674
+ */
2675
+ commitEdit?(id: string, patch: {
2676
+ name?: string;
2677
+ intentDescription?: string;
2678
+ tags?: string[];
2679
+ parameters?: ScriptParameter[];
2680
+ bodyHash: string;
2681
+ /** New on-disk stem when the edit renamed the script; omitted otherwise. */
2682
+ fileBase?: string;
2683
+ sourceIds?: string[];
2684
+ tables?: string[];
2685
+ editedBy?: string;
2686
+ editedFrom?: string;
2687
+ historyEntry: {
2688
+ version: number;
2689
+ at: string;
2690
+ by?: string;
2691
+ instruction?: string;
2692
+ changeSummary?: string;
2693
+ bodyHash?: string;
2694
+ };
2695
+ }): Promise<ScriptRecipeMetaRow | null>;
2696
+ /** Stamp a draft's last execution error. */
2697
+ recordDraftError(id: string, err: {
2698
+ phase: string;
2699
+ message: string;
2700
+ attempt: number;
2701
+ at: string;
2702
+ }): Promise<void>;
2703
+ /** Delete a recipe row (body file removed separately). */
2704
+ remove(id: string): Promise<void>;
2705
+ /** True if `fileBase` is taken by a different recipe in this project. */
2706
+ fileBaseTaken(fileBase: string, excludeId: string, projectId?: string): Promise<boolean>;
1748
2707
  }
2708
+ /** Pull the injected store off the collections bag (or null if not wired). */
2709
+ declare function resolveScriptRecipeStore(collections: any): ScriptRecipeStore | null;
1749
2710
 
1750
2711
  /**
1751
- * ScriptStore — Persistent Storage for Script Recipes
2712
+ * ScriptStore — Postgres metadata + on-disk body for script recipes.
1752
2713
  *
1753
- * Layout:
1754
- * scripts-store/
1755
- * metadata/<name>.json ← recipe metadata (params, tables, counts, ...)
1756
- * <name>.ts ← the getData() function body, editable in your IDE
2714
+ * Split of responsibilities:
2715
+ * - METADATA → injected `ScriptRecipeStore` (Postgres FTS + atomic counters),
2716
+ * resolved from `collections['script-recipes']`.
2717
+ * - BODY → scripts-store/<fileBase>.ts, editable in your IDE. Written
2718
+ * atomically (temp + rename); `bodyHash` (sha256) detects edits.
1757
2719
  *
1758
- * Each VM deployment is a single project, so no project ID prefix needed.
1759
- * Legacy single-file format (JSON with embedded scriptBody) is still loadable
1760
- * for backwards compatibility and auto-migrated to the split format on next save.
2720
+ * The old "read every file every turn + send the whole catalog to the LLM"
2721
+ * matcher is gone — matching is `store.search(prompt)` (FTS shortlist). The
2722
+ * draft/verified filename dance is gone too: `status` is a DB column and the
2723
+ * file keeps a stable `<fileBase>.ts` name across promotion.
2724
+ *
2725
+ * When no metadata store is injected, the store degrades to a safe no-op
2726
+ * (count 0 → script flow disabled) instead of crashing.
2727
+ *
2728
+ * See backend/docs/SCRIPT-FLOW-SCALING-ISSUES.md.
1761
2729
  */
1762
2730
 
1763
- /**
1764
- * Input for `ScriptStore.saveDraft()`. Identifies the draft by `recipeId` so
1765
- * retries within the same turn overwrite the same files (or omit `recipeId`
1766
- * on the first call to mint a fresh draft).
1767
- */
1768
2731
  interface SaveDraftInput {
1769
2732
  /** Reuse an existing draft (retry); omit to mint a new one. */
1770
2733
  recipeId?: string;
@@ -1776,141 +2739,197 @@ interface SaveDraftInput {
1776
2739
  parameters: ScriptParameter[];
1777
2740
  scriptBody: string;
1778
2741
  createdFrom: string;
2742
+ /**
2743
+ * Set when this draft is a SHADOW of an existing recipe being edited. The
2744
+ * draft is verified independently and merged back onto the parent via
2745
+ * `commitEdit`, so the working script is never clobbered by an edit that
2746
+ * turns out not to run. See backend/docs/SCRIPT-EDIT-DESIGN.md § D5.
2747
+ */
2748
+ parentId?: string;
1779
2749
  }
1780
- /**
1781
- * Lineage + provenance fields applied when a draft is promoted to verified.
1782
- * MainAgent gathers `sourceIds` and `tables` from the verified execution; the
1783
- * caller (agent-user-response.ts) supplies the optional fork lineage.
1784
- */
1785
2750
  interface PromoteToVerifiedInput {
1786
2751
  sourceIds: string[];
1787
2752
  tables: string[];
1788
2753
  parentId?: string;
1789
2754
  forkDepth?: number;
1790
2755
  forkReason?: string;
2756
+ components?: ScriptComponentSpec[];
2757
+ }
2758
+ interface ScriptStoreOptions {
2759
+ /** Explicit metadata store, or resolved from `collections['script-recipes']`. */
2760
+ store?: ScriptRecipeStore | null;
2761
+ collections?: any;
2762
+ /** Body directory (defaults to <cwd>/scripts-store). */
2763
+ baseDir?: string;
2764
+ /** Project scope stamped on every row. */
2765
+ projectId?: string;
1791
2766
  }
2767
+ /**
2768
+ * Fingerprint of everything that decides what `getData` PRODUCES.
2769
+ *
2770
+ * A recipe holds three programs in one file — getData, getAnalysis,
2771
+ * getComponents — but only getData costs a source query, and only getData's
2772
+ * output can invalidate a verified run. Editing the narrative or a chart title
2773
+ * cannot change a single row, yet a whole-body comparison says it did, because
2774
+ * `write_script` resubmits the entire file. That is what made a cosmetic edit
2775
+ * indistinguishable from a SQL change and cost a scarce execute attempt.
2776
+ *
2777
+ * Parameters are part of the fingerprint, not an afterthought: `execute_script`
2778
+ * runs with `{}`, so the DEFAULTS are the values getData actually sees. Same
2779
+ * getData text with a changed default is a genuinely different result, and
2780
+ * hashing the body alone would have called it unchanged.
2781
+ *
2782
+ * Deliberately conservative — trimmed, not whitespace-collapsed. The two ways
2783
+ * to be wrong are NOT symmetric: a false "unchanged" reuses a stale result and
2784
+ * reports wrong numbers, while a false "changed" merely costs an attempt. So it
2785
+ * biases toward detecting change, and a reformat is allowed to look like an edit.
2786
+ */
2787
+ declare function dataFingerprint(scriptBody: string, parameters?: ScriptParameter[]): string;
2788
+ /** Inverse of `stripNamedExport` — pulls the named export block OUT instead of removing it. Null when absent. */
2789
+ declare function extractNamedExport(body: string, fnName: string): string | null;
2790
+ declare function normalizeScriptBody(scriptBody: string): string;
1792
2791
  declare class ScriptStore {
1793
- private recipes;
2792
+ private store;
1794
2793
  private storeDir;
1795
- private loaded;
1796
- constructor(baseDir?: string);
1797
- private metadataDir;
1798
- /**
1799
- * Filename base for a recipe. Drafts include the per-turn suffix so two
1800
- * concurrent turns can never clobber each other's draft files; verified
1801
- * recipes use the bare slug.
1802
- */
1803
- private fileBaseName;
1804
- /**
1805
- * Absolute path to the .ts file for a recipe. Callers (e.g. ScriptRunner,
1806
- * MainAgent's execute_script) use this to hand off the path to the tsx child.
1807
- */
1808
- getScriptPath(recipe: ScriptRecipe): string;
1809
- /**
1810
- * Absolute path to the metadata JSON for a recipe.
1811
- */
1812
- private getMetadataPath;
1813
- /**
1814
- * Get all VERIFIED recipes — drafts are filtered out so the matcher never
1815
- * considers an unverified script. Loads from disk on first access.
1816
- */
1817
- getAll(): ScriptRecipe[];
1818
- /**
1819
- * Get a recipe by ID — returns drafts as well as verified scripts.
1820
- * Used by MainAgent and the promotion path.
1821
- */
1822
- get(id: string): ScriptRecipe | null;
1823
- /**
1824
- * Number of verified recipes (matches `getAll().length`).
1825
- */
1826
- count(): number;
1827
- /**
1828
- * Save a recipe (create or update).
1829
- * File is named after the script: "order-status-distribution.json"
1830
- */
1831
- save(recipe: ScriptRecipe): void;
1832
- /**
1833
- * Persist (or update) a draft recipe to disk. Always writes immediately so
1834
- * the `.ts` body is visible in the IDE the moment MainAgent calls
1835
- * `write_script`. Within one turn, retries that pass the same `recipeId`
1836
- * overwrite the same files (the LLM rewriting itself); a fresh `recipeId`
1837
- * is minted only on the first call of the turn.
2794
+ private projectId?;
2795
+ constructor(opts?: ScriptStoreOptions);
2796
+ /** Whether a metadata store is wired (matcher / authoring are gated on this). */
2797
+ hasStore(): boolean;
2798
+ /** Number of healthy verified recipes (gates the script-matching path). */
2799
+ count(): Promise<number>;
2800
+ /**
2801
+ * FTS shortlist for the matcher (metadata only — bodies are loaded lazily by
2802
+ * `get()` once the LLM picks one). Returns verified, healthy recipes ranked
2803
+ * by relevance.
2804
+ */
2805
+ search(prompt: string, limit?: number): Promise<ScriptRecipe[]>;
2806
+ /** Fetch one recipe by id with its body loaded from disk. */
2807
+ get(id: string): Promise<ScriptRecipe | null>;
2808
+ /**
2809
+ * Fetch one recipe by id, pinned to a SPECIFIC past version rather than
2810
+ * whatever is live today. Needed to render a past turn's conversation
2811
+ * history accurately after the script has since been edited — get() alone
2812
+ * always returns the CURRENT body, which may no longer be what that turn
2813
+ * actually ran.
1838
2814
  *
1839
- * Filename includes the `turnId` suffix so concurrent turns never collide.
2815
+ * Returns null (never a silently-wrong version) when the requested version
2816
+ * isn't the current one and its archived body can't be found — e.g. the
2817
+ * archive was cleaned up after the whole recipe was later deleted.
1840
2818
  */
1841
- saveDraft(input: SaveDraftInput): ScriptRecipe;
2819
+ getVersion(id: string, version: number): Promise<ScriptRecipe | null>;
2820
+ /** Create or update a recipe (metadata upsert + body write when changed). */
2821
+ save(recipe: ScriptRecipe): Promise<void>;
1842
2822
  /**
1843
- * Stamp the draft with the most recent execution failure so it is visible
1844
- * in the metadata JSON without grepping logs. No-op if the recipe doesn't
1845
- * exist or has already been promoted.
2823
+ * Persist (or update) a draft. Within a turn, retries that pass the same
2824
+ * `recipeId` overwrite the same row + file; a fresh `recipeId` mints a new
2825
+ * draft. The body is visible at scripts-store/<fileBase>.ts immediately.
1846
2826
  */
2827
+ saveDraft(input: SaveDraftInput): Promise<ScriptRecipe>;
2828
+ /** Stamp a draft's last execution error (metadata only). */
1847
2829
  recordDraftError(recipeId: string, err: {
1848
2830
  phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
1849
2831
  message: string;
1850
2832
  attempt: number;
1851
- }): void;
2833
+ }): Promise<void>;
1852
2834
  /**
1853
2835
  * Promote a successfully-executed draft into a verified script.
1854
- *
1855
- * - Renames the on-disk files from `<slug>-<turnId>.{ts,json}` to the bare
1856
- * `<slug>.{ts,json}`. If a verified file with the same slug already exists
1857
- * (concurrent turn won the race, or a prior session has it), the recipe
1858
- * keeps its turn-suffixed filename so we never overwrite verified work —
1859
- * the matcher keys on `recipe.id`, so two siblings with similar slugs is fine.
1860
- * - Flips `status: 'verified'`, clears `lastError`, applies provenance
1861
- * (`sourceIds`, `tables`) and optional fork lineage.
1862
- */
1863
- promoteToVerified(recipeId: string, input: PromoteToVerifiedInput): ScriptRecipe | null;
1864
- /**
1865
- * Drop a draft from disk + memory (e.g. when an outer error path wants to
1866
- * clean up). Per the agreed policy, MainAgent's normal failure path does
1867
- * NOT call this — failed drafts are kept on disk for the user to inspect.
1868
- */
1869
- discardDraft(recipeId: string): void;
1870
- /**
1871
- * Delete a recipe by ID.
1872
- */
1873
- delete(id: string): void;
1874
- /**
1875
- * Record a successful execution for a recipe.
1876
- */
1877
- recordSuccess(id: string): void;
1878
- /**
1879
- * Record a failed execution for a recipe.
1880
- */
1881
- recordFailure(id: string): void;
1882
- /**
1883
- * Get the failure rate for a recipe (0 to 1).
1884
- * Returns 0 if the recipe has fewer than 5 total uses.
2836
+ * The on-disk body already exists at <fileBase>.ts (written at write_script
2837
+ * time) and keeps its name — only the DB row flips status + provenance.
1885
2838
  */
1886
- getFailureRate(id: string): number;
1887
- private ensureLoaded;
2839
+ promoteToVerified(recipeId: string, input: PromoteToVerifiedInput): Promise<ScriptRecipe | null>;
1888
2840
  /**
1889
- * Convert a script name to a safe filename.
1890
- * "Order Status Distribution" → "order-status-distribution"
1891
- */
1892
- private toFileName;
1893
- /**
1894
- * Load all script files from disk.
2841
+ * Commit a user-directed edit: merge a VERIFIED shadow draft back onto the
2842
+ * recipe it was editing, as version N+1 under the SAME recipe id.
1895
2843
  *
1896
- * Supports two layouts:
1897
- * - New (preferred): metadata/<name>.json + <name>.ts
1898
- * - Legacy: <name>.json with embedded scriptBody (auto-migrated on next save)
1899
- */
1900
- private loadAllFromDisk;
1901
- /**
1902
- * Save a recipe to disk in split format:
1903
- * metadata/<name>.json (metadata, no scriptBody)
1904
- * <name>.ts (just the function body)
2844
+ * Keeping the id stable is the point of the whole feature — every cached
2845
+ * conversation, `script_dataset` regeneration descriptor and persisted
2846
+ * component spec already points at it, so the correction applies retroactively
2847
+ * to replays instead of stranding them on the old body.
2848
+ *
2849
+ * Steps (see backend/docs/SCRIPT-EDIT-DESIGN.md § Component 5):
2850
+ * 1. archive the current body as `<fileBase>.v<version>.ts`
2851
+ * 2. write the edited body to the original fileBase (atomic)
2852
+ * 3. version++, copy name/description/params, append a history record
2853
+ * 4. reset health counters — the pre-edit failure history is stale
2854
+ * 5. clear component specs — they were validated against the OLD shape
2855
+ * 6. delete the shadow draft (row + file)
2856
+ *
2857
+ * Returns the updated recipe, or null when the commit could not be applied
2858
+ * (caller should then fall back to treating the draft as a new script).
2859
+ */
2860
+ commitEdit(targetId: string, draft: {
2861
+ recipeId: string;
2862
+ name?: string;
2863
+ intentDescription?: string;
2864
+ tags?: string[];
2865
+ parameters?: ScriptParameter[];
2866
+ scriptBody: string;
2867
+ sourceIds?: string[];
2868
+ tables?: string[];
2869
+ }, meta: {
2870
+ instruction?: string;
2871
+ changeSummary?: string;
2872
+ editedBy?: string;
2873
+ }): Promise<ScriptRecipe | null>;
2874
+ /**
2875
+ * Drop a draft (row + body file). MainAgent calls this at end-of-turn when a
2876
+ * draft was authored but never verified — failed drafts are never matched, so
2877
+ * deleting them immediately avoids unbounded accumulation (#5). No-op if the
2878
+ * recipe isn't a draft (so a promoted/verified script is never removed here).
2879
+ */
2880
+ discardDraft(recipeId: string): Promise<void>;
2881
+ /** Delete a recipe (row + body file). */
2882
+ delete(id: string): Promise<void>;
2883
+ /** Record a successful execution (atomic counter bump). */
2884
+ recordSuccess(id: string): Promise<void>;
2885
+ /** Record a failed execution (atomic counter bump). */
2886
+ recordFailure(id: string): Promise<void>;
2887
+ /** Absolute path to the .ts body for a recipe (used by the runner/MainAgent). */
2888
+ getScriptPath(recipe: ScriptRecipe): string;
2889
+ private removeById;
2890
+ private rowToRecipe;
2891
+ private recipeToRow;
2892
+ /** slug of name, with a short id suffix when the bare slug is already taken. */
2893
+ private computeFileBase;
2894
+ private toSlug;
2895
+ private hash;
2896
+ /**
2897
+ * Attach getComponents to the recipe body. The stored `components` column is
2898
+ * cleared at promotion (see promoteToVerified): once the body computes its own
2899
+ * specs, a stale spec array beside a live program is how the wrong one gets
2900
+ * read later. See backend/docs/COMPONENTS-AS-PROGRAM-DESIGN.md § 4.4.
2901
+ */
2902
+ attachComponents(recipeId: string, componentsBody: string): Promise<boolean>;
2903
+ /**
2904
+ * Append (or replace) the `getAnalysis` export on an existing draft body.
2905
+ *
2906
+ * The narrative is authored AFTER execution, so it lands on a file that
2907
+ * already holds a working getData — rewriting the whole body would risk
2908
+ * losing the query that just succeeded. Replaces any earlier getAnalysis so
2909
+ * a retry does not stack duplicate exports.
2910
+ */
2911
+ attachAnalysis(recipeId: string, analysisBody: string): Promise<boolean>;
2912
+ private bodyPath;
2913
+ private readBody;
2914
+ /** Directory holding superseded bodies. Dot-prefixed so IDEs/`ls` hide it. */
2915
+ private get archiveDir();
2916
+ /**
2917
+ * Archive a superseded body as `.versions/<fileBase>.v<n>.ts`.
1905
2918
  *
1906
- * If a legacy top-level <name>.json exists for the same recipe, remove it
1907
- * so we don't end up with duplicate sources of truth.
2919
+ * Kept out of the main store directory on purpose — see commitEdit step 1.
2920
+ * To roll back: copy the file back over `scripts-store/<fileBase>.ts`.
1908
2921
  */
1909
- private saveRecipeToDisk;
2922
+ private writeArchive;
1910
2923
  /**
1911
- * Delete a recipe's files from disk (both metadata + body, plus any legacy file).
2924
+ * Move a recipe's archived versions to a new prefix when its fileBase changes,
2925
+ * so all versions of one recipe stay grouped. Without this, two renames would
2926
+ * scatter a single recipe's history across three prefixes in `.versions/` with
2927
+ * nothing linking them back to the live script.
1912
2928
  */
1913
- private deleteRecipeFromDisk;
2929
+ private renameArchives;
2930
+ /** Atomic body write (temp + rename) so concurrent reads never see a partial file. */
2931
+ private writeBody;
2932
+ private unlinkBody;
1914
2933
  }
1915
2934
 
1916
2935
  /**
@@ -1944,7 +2963,75 @@ declare class MainAgent {
1944
2963
  private turnId;
1945
2964
  private createdFromPrompt;
1946
2965
  private scriptState;
1947
- constructor(externalTools: ExternalTool[], config: AgentConfig, scriptStore?: ScriptStore, turnId?: string, streamBuffer?: StreamBuffer, workflows?: WorkflowDescriptor[]);
2966
+ /** Answer-flow component catalog (filtered + projected by the caller). */
2967
+ private componentCatalog;
2968
+ /** Specs accepted by `write_components` this turn; empty until it succeeds. */
2969
+ /**
2970
+ * Entities resolved this turn, appended verbatim to every source dispatch.
2971
+ *
2972
+ * Not left to the model to copy into its intent prose: observed live, the
2973
+ * MainAgent wrote names instead of ids on the first dispatch AND re-spelled
2974
+ * 'F D C LIMITED' as 'FDC Limited', so the source agent fell back to
2975
+ * `PartyName LIKE '%FDC%'` and found 2 of 3 customers. Carrying the ids
2976
+ * structurally makes that impossible rather than instruction-dependent.
2977
+ */
2978
+ private resolvedEntities;
2979
+ /**
2980
+ * Concept bodies resolved this turn (currently: invariants only, via
2981
+ * loadConceptInvariants — concept resolution is no longer part of
2982
+ * resolve_entities or any other MainAgent-owned tool) — retained the same
2983
+ * way as resolvedEntities above, so they can be attached to the SourceAgent
2984
+ * dispatch.
2985
+ *
2986
+ * Without this, a concept resolved HERE (MainAgent's own tool loop) never
2987
+ * reaches SourceAgent at all: SourceAgent only gets the natural-language
2988
+ * `intent` MainAgent writes, which is a paraphrase, not the concept's
2989
+ * verbatim, authoritative body. Deduped on name so a mid-turn re-resolve
2990
+ * doesn't repeat an already-attached concept.
2991
+ */
2992
+ private resolvedConcepts;
2993
+ private componentSpecs;
2994
+ private componentLayout;
2995
+ private renderComponentAttempts;
2996
+ /** Same repeated-violation tracking as ScriptState.lastAnalysisViolationTexts, for write_components. */
2997
+ private lastComponentsViolationTexts;
2998
+ /**
2999
+ * Fork mode — set when this turn is adapting a near-matching parent script.
3000
+ * In fork mode there is no legitimate "answer with bare text" outcome: the
3001
+ * only correct first move is a tool call (write_script, or a source tool for
3002
+ * schema discovery). We therefore force tool use on the first LLM iteration
3003
+ * so the model can't end its turn with a bare "I'll adapt…" preamble and zero
3004
+ * tool calls. Never set on the fresh-authoring / general-question path.
3005
+ */
3006
+ private forkMode;
3007
+ /**
3008
+ * Edit mode — set when this turn applies a user-directed change to an
3009
+ * existing script. Swaps the system prompt to `agent-main-edit` and stamps
3010
+ * the shadow draft's parentId. Like fork mode there is no legitimate
3011
+ * "answer with bare text" outcome, so tool use is forced on iteration 1.
3012
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 4.
3013
+ */
3014
+ private editContext;
3015
+ /** The recipe's durable rendering choice — see the constructor. */
3016
+ private displayPreference;
3017
+ /**
3018
+ * Per-turn cancellation signal (user hit "Stop"). Set at the top of
3019
+ * handleQuestion and read by the tool handler, the SourceAgent dispatch, and
3020
+ * the script subprocess so an abort tears down every layer of the turn.
3021
+ */
3022
+ private abortSignal?;
3023
+ constructor(externalTools: ExternalTool[], config: AgentConfig, scriptStore?: ScriptStore, turnId?: string, streamBuffer?: StreamBuffer, workflows?: WorkflowDescriptor[], forkMode?: boolean, editContext?: EditContext, componentCatalog?: Component[],
3024
+ /**
3025
+ * The recipe's durable rendering choice, when it has one. A DATA edit
3026
+ * re-authors getComponents from scratch, so without this the user's chart
3027
+ * type is silently dropped whenever anyone changes the SQL.
3028
+ */
3029
+ displayPreference?: {
3030
+ componentTypes: string[];
3031
+ instruction: string;
3032
+ });
3033
+ /** True when the turn is applying a user-directed script edit. */
3034
+ private get editMode();
1948
3035
  private get scriptingEnabled();
1949
3036
  /**
1950
3037
  * Handle a user question using the multi-agent system.
@@ -1956,7 +3043,34 @@ declare class MainAgent {
1956
3043
  * 4. Direct tools → fn() called directly with LLM params → returns data
1957
3044
  * 5. Generates final analysis text
1958
3045
  */
1959
- handleQuestion(userPrompt: string, apiKey?: string, conversationHistory?: string, streamCallback?: (chunk: string) => void): Promise<AgentResponse>;
3046
+ handleQuestion(userPrompt: string, apiKey?: string, conversationHistory?: string, streamCallback?: (chunk: string) => void, signal?: AbortSignal): Promise<AgentResponse>;
3047
+ /**
3048
+ * Tool definition, with the catalog rendered from the registered `Metadata`
3049
+ * the caller passed in. Generated rather than hand-written so it cannot drift
3050
+ * from what the frontend actually ships.
3051
+ */
3052
+ private buildWriteComponentsToolDef;
3053
+ /**
3054
+ * Validate the agent's component picks against the REAL columns of each bound
3055
+ * dataset. All-or-nothing: any error rejects the whole call and returns the
3056
+ * findings, because a partially-accepted dashboard makes the retry ambiguous.
3057
+ */
3058
+ private validateSpecs;
3059
+ /**
3060
+ * Author the dashboard as a program. Attaches getComponents to the draft, runs
3061
+ * it over the verified rows, then validates the specs it RETURNED — the program
3062
+ * is never trusted because it is a program, exactly as an authored spec is not.
3063
+ */
3064
+ private handleWriteComponents;
3065
+ /**
3066
+ * Author the answer as a program. Runs after execute_script, so the body is
3067
+ * written with the result already known and can react to what it found.
3068
+ *
3069
+ * Rejects a body that types numbers rather than computing them — see
3070
+ * analysis-lint. A hardcoded figure is worse than prose: it freezes into the
3071
+ * recipe and replays unchanged while the data moves underneath it.
3072
+ */
3073
+ private handleWriteAnalysis;
1960
3074
  private handleWriteScript;
1961
3075
  private handleExecuteScript;
1962
3076
  /**
@@ -1966,6 +3080,17 @@ declare class MainAgent {
1966
3080
  */
1967
3081
  private buildSavedScript;
1968
3082
  private normalizeParameterList;
3083
+ /**
3084
+ * Which declared parameter NAMES never actually appear wired into the
3085
+ * script body — checked as plain text, not a real parser, so it only
3086
+ * catches the worst case (completely unreferenced) rather than every
3087
+ * subtler variant (e.g. used for display text while the SQL itself still
3088
+ * hardcodes the value). That worst case is exactly what's been observed:
3089
+ * a parameter declared purely for show, with the actual filter hardcoded.
3090
+ * Accepts `params.name`, `params['name']`/`params["name"]`, and a
3091
+ * destructuring binding (`const { name } = params`) as valid wiring.
3092
+ */
3093
+ private findUnwiredParams;
1969
3094
  /**
1970
3095
  * Use the schema embedding collection to pre-select relevant tables for
1971
3096
  * this source + intent. Returns a formatted schema block if confidence is
@@ -1975,6 +3100,122 @@ declare class MainAgent {
1975
3100
  * loop and reduce iteration budget. When it returns null, the SourceAgent
1976
3101
  * falls back to the existing LLM-driven keyword search (same as today).
1977
3102
  */
3103
+ /**
3104
+ * Ids + verbatim names for everything resolved this turn, appended by CODE to
3105
+ * each source dispatch so neither omission nor re-spelling by the model can
3106
+ * lose them. Empty string when nothing was resolved, so intents are untouched
3107
+ * for questions that name no entities.
3108
+ *
3109
+ * Also appends `attrs.scopeSql` when the resolved instance carries one —
3110
+ * same "guaranteed by code" reasoning as the ids/names above, extended to
3111
+ * the one attrs field with a documented, cross-entity-type meaning: a
3112
+ * ready-made SQL predicate an entity's index precomputed (see
3113
+ * ENTITY-SEARCH-DESIGN.md's hierarchy-broadening pattern — branch/region/
3114
+ * state entity types attach it so a lane/route question never needs its
3115
+ * own city-matching join). Confirmed real cost of not doing this: a
3116
+ * branch/state resolved with scopeSql sat unused while the source agent
3117
+ * spent 4 failed exploratory queries rediscovering the exact same
3118
+ * branch-id list by hand (Invalid column name 'StateName'/'CityId'/
3119
+ * 'OrganizationLocationId', in that order, ~144s, before landing on ids
3120
+ * that were sitting in scopeSql from the very first resolve_entities
3121
+ * call). Other attrs (code, counts, etc.) are not dumped here — they are
3122
+ * descriptive, not actionable the way a precomputed predicate is, and
3123
+ * bloating every dispatch with all of them for no clear gain isn't worth
3124
+ * it; scopeSql is the one thing worth this guarantee today.
3125
+ */
3126
+ private buildResolvedEntitiesBlock;
3127
+ /**
3128
+ * Dedupe-and-retain helper shared by loadConceptInvariants and the generic
3129
+ * direct-tool `resolvedConcepts` opt-in hook. `alwaysInclude` marks entries
3130
+ * that buildResolvedConceptsBlock must attach to EVERY dispatch regardless
3131
+ * of that dispatch's `concepts` param — true only for invariants, which are
3132
+ * meant to apply everywhere by definition. Concepts retained without it
3133
+ * (e.g. via the `concept` tool's `get`) are scoped per-dispatch instead.
3134
+ */
3135
+ private retainResolvedConcepts;
3136
+ /**
3137
+ * Same purpose as buildResolvedEntitiesBlock, for concept bodies instead of
3138
+ * entity ids — appended to the dispatch intent so SourceAgent's own,
3139
+ * independent LLM call receives the resolved definitions verbatim rather
3140
+ * than only MainAgent's paraphrased intent text.
3141
+ *
3142
+ * Unlike buildResolvedEntitiesBlock, non-invariant entries here are SCOPED
3143
+ * to `names` (this dispatch's SourceToolInput.concepts) rather than
3144
+ * dumping the whole-turn `this.resolvedConcepts` accumulator into every
3145
+ * dispatch. A concept body is phrased as an authoritative override
3146
+ * ("overrides the schema... your own reasoning") — fetching several via
3147
+ * `get` to compare/discard, or a turn needing different concepts for
3148
+ * different sub-questions, must not leak an irrelevant concept's filters
3149
+ * into an unrelated dispatch. An entry not named in `names` and not
3150
+ * `alwaysInclude` is left out — a concept the model forgot to name is a
3151
+ * visible, fixable miss; one that leaks in uninvited is not.
3152
+ *
3153
+ * `alwaysInclude` entries (invariants — see loadConceptInvariants) bypass
3154
+ * `names` entirely and attach to every dispatch regardless, same as before
3155
+ * this method took a `names` param — they are meant to apply everywhere,
3156
+ * not just when the model happens to ask for them.
3157
+ */
3158
+ private buildResolvedConceptsBlock;
3159
+ /** True when the backend registered the entity-search collection. */
3160
+ private get entitySearchEnabled();
3161
+ /** True when the backend registered the concepts collection. */
3162
+ private get conceptsEnabled();
3163
+ /**
3164
+ * Concept names for the system prompt. Names ONLY — a concept's full body is
3165
+ * fetched separately once the LLM has named one (via whatever concept-lookup
3166
+ * tool the project offers, if any — MainAgent itself no longer resolves
3167
+ * concept bodies). Putting bodies here would rebuild the always-on context
3168
+ * problem concepts exist to solve.
3169
+ *
3170
+ * Not cached, for the same reason loadEntityCatalog isn't: unchanged rows
3171
+ * render byte-identical text so the prompt-cache prefix still hits, and a
3172
+ * process cache would only buy a window where a new concept is invisible.
3173
+ */
3174
+ private loadConceptCatalog;
3175
+ /**
3176
+ * The ALWAYS-INJECTED tier: preconditions no question is semantically close
3177
+ * to — date encodings, valid-row filters. These cannot be retrieval-gated.
3178
+ * "What was revenue last quarter?" has no similarity to "dates are DD/MM/YYYY
3179
+ * text", so leaving them to be requested means they are silently dropped and
3180
+ * the answer is confidently wrong rather than visibly missing.
3181
+ *
3182
+ * Bodies ARE included here — unlike the catalog — because there is nothing to
3183
+ * request: being unrequestable is what makes them invariants.
3184
+ *
3185
+ * Also retained into resolvedConcepts so these bodies reach SourceAgent's
3186
+ * dispatch too — an invariant is meant to apply everywhere, not just
3187
+ * wherever the model happens to ask for something whose `requires` chain
3188
+ * reaches it. This is now the ONLY way a concept body reaches
3189
+ * resolvedConcepts/SourceAgent's dispatch automatically — a non-invariant
3190
+ * concept fetched through a project's own concept-lookup tool (if any) is
3191
+ * NOT retained here, and will not reach a later SourceAgent dispatch unless
3192
+ * the model itself restates it in the intent it writes.
3193
+ */
3194
+ private loadConceptInvariants;
3195
+ /**
3196
+ * Never throws: a concept-store outage must degrade to "no definition found"
3197
+ * and leave the agent deriving the formula as it does today, not take the
3198
+ * whole answer down.
3199
+ */
3200
+ /**
3201
+ * Compact catalog (type + gloss) for the system prompt. This is what lets the
3202
+ * LLM NAME a concept instead of guessing at our vocabulary — it can infer
3203
+ * that a question is about unpaid money, but not that we call that
3204
+ * `outstanding-balance`. Full `resolution` detail is fetched by the tool.
3205
+ *
3206
+ * Deliberately NOT cached in-process. The token cost is already handled by
3207
+ * prompt caching (the system prompt carries a cache_control breakpoint), and
3208
+ * re-reading a tiny PK-ordered table costs ~nothing. Fetching fresh does not
3209
+ * weaken the prompt cache either: unchanged data renders byte-identical text,
3210
+ * so the prefix still hits. A process cache would only buy a staleness window
3211
+ * in which a newly indexed entity is invisible to the agent.
3212
+ */
3213
+ private loadEntityCatalog;
3214
+ /**
3215
+ * Never throws: a resolver outage must degrade to "nothing resolved" and
3216
+ * leave the agent exactly as it was, not take the whole answer down.
3217
+ */
3218
+ private handleResolveEntities;
1978
3219
  private preResolveSchema;
1979
3220
  /**
1980
3221
  * Execute a direct tool — call fn() with LLM-provided params, no SourceAgent.
@@ -2019,16 +3260,6 @@ declare class MainAgent {
2019
3260
  getSourceSummaries(): SourceSummary[];
2020
3261
  }
2021
3262
 
2022
- /**
2023
- * Represents an action that can be performed on a UIBlock
2024
- */
2025
- interface Action {
2026
- id: string;
2027
- name: string;
2028
- type: string;
2029
- [key: string]: any;
2030
- }
2031
-
2032
3263
  type SystemPrompt = string | Anthropic.Messages.TextBlockParam[];
2033
3264
  interface LLMMessages {
2034
3265
  sys: SystemPrompt;
@@ -2041,7 +3272,62 @@ interface LLMOptions {
2041
3272
  temperature?: number;
2042
3273
  topP?: number;
2043
3274
  apiKey?: string;
3275
+ baseURL?: string;
2044
3276
  partial?: (chunk: string) => void;
3277
+ /**
3278
+ * Per-request cancellation. When the caller aborts this signal (user hit
3279
+ * "Stop"), the underlying provider request is cancelled and the call throws
3280
+ * a RequestAbortedError. Threaded into the provider `messages.create` request
3281
+ * options and checked between tool-loop iterations. Currently honored on the
3282
+ * Anthropic path (the agent flow's default provider).
3283
+ */
3284
+ signal?: AbortSignal;
3285
+ /**
3286
+ * Forces a tool call on the FIRST iteration of streamWithTools only
3287
+ * (subsequent iterations revert to auto). Used by fork mode to stop the
3288
+ * model from ending its turn with a bare "I'll adapt the script…" preamble
3289
+ * and zero tool calls. `{ type: 'any' }` lets the model pick which tool
3290
+ * (write_script in the common case, a source tool for schema discovery);
3291
+ * `{ type: 'tool', name }` pins a specific tool. Honored on both the
3292
+ * Anthropic path and the OpenAI/OpenRouter path (mapped to OpenAI's
3293
+ * tool_choice: 'required' / a named function).
3294
+ */
3295
+ firstIterationToolChoice?: {
3296
+ type: 'any';
3297
+ } | {
3298
+ type: 'tool';
3299
+ name: string;
3300
+ };
3301
+ /**
3302
+ * Same forcing as `firstIterationToolChoice`, for the SECOND iteration only
3303
+ * (iteration 3+ reverts to auto, same as iteration 2+ does for the first-
3304
+ * iteration option). Added for MainAgent's `concept` tool: `resolve_entities`
3305
+ * being pinned to iteration 1 doesn't, by itself, make `concept` reliable —
3306
+ * confirmed live, repeatedly, that a model can skip it even with a CRITICAL
3307
+ * prompt instruction telling it not to. Pinning iteration 2 the same way
3308
+ * `resolve_entities` pins iteration 1 makes it structurally guaranteed
3309
+ * instead of prose-dependent. Honored on both the Anthropic and OpenAI/
3310
+ * OpenRouter paths, same as `firstIterationToolChoice`.
3311
+ */
3312
+ secondIterationToolChoice?: {
3313
+ type: 'any';
3314
+ } | {
3315
+ type: 'tool';
3316
+ name: string;
3317
+ };
3318
+ /**
3319
+ * Internal — set only by the OpenRouter wrappers when the target is a Claude
3320
+ * model. Tells the OpenAI-wire path to emit Anthropic `cache_control`
3321
+ * breakpoints (OpenRouter forwards them to Anthropic for prompt caching).
3322
+ * Never set for direct OpenAI/Groq calls, so their requests are unchanged.
3323
+ */
3324
+ _openrouterClaudeCaching?: boolean;
3325
+ /**
3326
+ * Internal — OpenRouter provider-routing preferences (forwarded as the
3327
+ * `provider` body field). Set by the OpenRouter wrappers to steer routing to
3328
+ * a fast backend (e.g. {sort:'throughput'}). Never set for direct OpenAI/Groq.
3329
+ */
3330
+ _openrouterProvider?: Record<string, unknown>;
2045
3331
  }
2046
3332
  interface Tool {
2047
3333
  name: string;
@@ -2063,6 +3349,16 @@ declare class LLM {
2063
3349
  * @returns Normalized system prompt for Anthropic API
2064
3350
  */
2065
3351
  private static _normalizeSystemPrompt;
3352
+ /**
3353
+ * Strip unpaired UTF-16 surrogates from every text field of a message set.
3354
+ *
3355
+ * A lone surrogate (from mid-pair string slicing or corrupt source data)
3356
+ * serializes to a bare `\udXXX` escape that strict JSON parsers — including
3357
+ * the one on Anthropic's API — reject with "no low surrogate in string",
3358
+ * failing the whole request. Sanitizing here, at the single boundary every
3359
+ * provider call flows through, guarantees no request can carry one.
3360
+ */
3361
+ private static _sanitizeMessages;
2066
3362
  /**
2067
3363
  * Log cache usage metrics from Anthropic API response
2068
3364
  * Shows cache hits, costs, and savings
@@ -2079,11 +3375,46 @@ declare class LLM {
2079
3375
  * "claude-sonnet-4-5" → ["anthropic", "claude-sonnet-4-5"] (default)
2080
3376
  */
2081
3377
  private static _parseModel;
3378
+ /**
3379
+ * Map an Anthropic model id (e.g. "claude-sonnet-4-5-20250929") to the OpenRouter slug
3380
+ * (e.g. "claude-sonnet-4.5"). OpenRouter slugs drop the date suffix and use dotted versions.
3381
+ */
3382
+ private static _toOpenRouterSlug;
3383
+ /**
3384
+ * Per-provider proxy base URL. Returns `${SUPERATOM_LLM_PROXY_URL}/<provider>`
3385
+ * when our Cloudflare LLM proxy is configured, else undefined (→ talk to the
3386
+ * provider directly, legacy behaviour). An explicit options.baseURL (e.g.
3387
+ * OpenRouter) always wins and is never overridden. See backend/docs/llm-proxy.md.
3388
+ */
3389
+ private static _proxyBaseURL;
3390
+ private static _openrouterOptions;
3391
+ private static _isRetryableProviderError;
3392
+ private static _withOpenrouterRetry;
3393
+ private static _openrouterText;
3394
+ private static _openrouterStream;
3395
+ private static _openrouterStreamWithTools;
3396
+ /**
3397
+ * Build an Anthropic client. Routes through our Cloudflare LLM proxy when
3398
+ * SUPERATOM_LLM_PROXY_URL is set (each client ships a per-client proxy key as
3399
+ * ANTHROPIC_API_KEY and never holds the real key); otherwise talks to
3400
+ * api.anthropic.com directly. See backend/docs/llm-proxy.md.
3401
+ */
3402
+ private static _anthropicClient;
3403
+ /** True when OpenRouter is configured as a fail-open fallback for Claude. */
3404
+ private static _openrouterAvailable;
3405
+ /** Remap an Anthropic model id to the OpenRouter model path for fail-open. */
3406
+ private static _anthropicFallbackModel;
2082
3407
  private static _anthropicText;
2083
3408
  private static _anthropicStream;
2084
3409
  private static _anthropicStreamWithTools;
2085
3410
  private static _groqText;
2086
3411
  private static _groqStream;
3412
+ /**
3413
+ * Gemini request options carrying the proxy base URL, or undefined → talk to
3414
+ * generativelanguage.googleapis.com directly. The Google SDK takes baseUrl as a
3415
+ * per-model request option, not a constructor arg. See backend/docs/llm-proxy.md.
3416
+ */
3417
+ private static _geminiRequestOptions;
2087
3418
  private static _geminiText;
2088
3419
  private static _geminiStream;
2089
3420
  /**
@@ -2092,8 +3423,28 @@ declare class LLM {
2092
3423
  */
2093
3424
  private static _cleanSchemaForGemini;
2094
3425
  private static _geminiStreamWithTools;
3426
+ /** True for Anthropic/Claude model ids — gates OpenRouter prompt caching. */
3427
+ private static _isClaudeModel;
3428
+ /**
3429
+ * Build the OpenAI-wire system message. For OpenRouter + Claude
3430
+ * (cacheClaude=true) it emits content parts carrying Anthropic
3431
+ * `cache_control` breakpoints (preserving any the caller set, else marking
3432
+ * the last block), so OpenRouter forwards them to Anthropic for prompt
3433
+ * caching. Otherwise it returns a plain flattened string — unchanged for
3434
+ * direct OpenAI/Groq.
3435
+ */
3436
+ private static _openaiSystemMessage;
3437
+ /**
3438
+ * Split an OpenAI-wire usage object. `prompt_tokens` INCLUDES cached tokens,
3439
+ * so we subtract them out (Anthropic-style: input excludes cache reads) and
3440
+ * report cached separately — this makes calculateCost price cache reads at
3441
+ * the discounted rate and reflects OpenRouter prompt-cache savings in logs.
3442
+ */
3443
+ private static _openaiUsage;
2095
3444
  private static _openaiText;
2096
3445
  private static _openaiStream;
3446
+ /** Map the Anthropic-style firstIterationToolChoice to OpenAI's tool_choice. */
3447
+ private static _openaiToolChoice;
2097
3448
  private static _openaiStreamWithTools;
2098
3449
  /**
2099
3450
  * Parse JSON string, handling markdown code blocks and surrounding text
@@ -2185,7 +3536,7 @@ declare class UILogCollector {
2185
3536
 
2186
3537
  /**
2187
3538
  * UIBlock represents a single user and assistant message block in a thread
2188
- * Contains user question, component metadata, component data, text response, and available actions
3539
+ * Contains user question, component metadata, component data, and text response
2189
3540
  */
2190
3541
  declare class UIBlock {
2191
3542
  private id;
@@ -2193,18 +3544,23 @@ declare class UIBlock {
2193
3544
  private generatedComponentMetadata;
2194
3545
  private componentData;
2195
3546
  private textResponse;
2196
- private actions;
2197
3547
  private createdAt;
3548
+ /**
3549
+ * Which script recipe produced this answer, when a script did. Read on the
3550
+ * NEXT turn so the user can say "use mode instead" and have the matcher
3551
+ * resolve it to a concrete script (the `edit` tier is unreachable without it).
3552
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 1.
3553
+ */
3554
+ private scriptBinding;
2198
3555
  /**
2199
3556
  * Creates a new UIBlock instance
2200
3557
  * @param userQuestion - The user's question or input
2201
3558
  * @param componentData - The component data object
2202
3559
  * @param generatedComponentMetadata - Optional metadata about the generated component
2203
- * @param actions - Optional array of available actions
2204
3560
  * @param id - Optional custom ID, generates UUID if not provided
2205
3561
  * @param textResponse - Optional text response from LLM
2206
3562
  */
2207
- constructor(userQuestion: string, componentData?: Record<string, any>, generatedComponentMetadata?: Record<string, any>, actions?: Action[], id?: string, textResponse?: string | null);
3563
+ constructor(userQuestion: string, componentData?: Record<string, any>, generatedComponentMetadata?: Record<string, any>, id?: string, textResponse?: string | null);
2208
3564
  /**
2209
3565
  * Get the UIBlock ID
2210
3566
  */
@@ -2254,41 +3610,22 @@ declare class UIBlock {
2254
3610
  * Set or update text response
2255
3611
  */
2256
3612
  setTextResponse(textResponse: string | null): void;
3613
+ /** The program-generated half of the answer — see setProgramAnswer. */
3614
+ programAnswer?: string | null;
2257
3615
  /**
2258
- * Get all actions (only if they are resolved, not if fetching)
3616
+ * Bind this block to the script recipe that produced its answer.
2259
3617
  */
2260
- getActions(): Action[] | null | Promise<Action[]>;
3618
+ setScriptBinding(binding: Record<string, any> | null): void;
2261
3619
  /**
2262
- * Get or fetch actions
2263
- * If actions don't exist or are a Promise, calls the generateFn and stores the promise
2264
- * If actions already exist, returns them
2265
- * @param generateFn - Async function to generate actions
2266
- * @returns Promise resolving to Action[]
3620
+ * The program-generated half of the answer, kept verbatim so a reload can
3621
+ * replace exactly that text without disturbing the streamed narration.
2267
3622
  */
2268
- getOrFetchActions(generateFn: () => Promise<Action[]>): Promise<Action[]>;
3623
+ setProgramAnswer(text: string | null): void;
3624
+ getProgramAnswer(): string | null;
2269
3625
  /**
2270
- * Set or replace all actions
2271
- */
2272
- setActions(actions: Action[]): void;
2273
- /**
2274
- * Add a single action (only if actions are resolved)
2275
- */
2276
- addAction(action: Action): void;
2277
- /**
2278
- * Add multiple actions (only if actions are resolved)
2279
- */
2280
- addActions(actions: Action[]): void;
2281
- /**
2282
- * Remove an action by ID (only if actions are resolved)
2283
- */
2284
- removeAction(actionId: string): boolean;
2285
- /**
2286
- * Clear all actions
2287
- */
2288
- clearActions(): void;
2289
- /**
2290
- * Get creation timestamp
3626
+ * The script recipe bound to this block, if any.
2291
3627
  */
3628
+ getScriptBinding(): Record<string, any> | null;
2292
3629
  getCreatedAt(): Date;
2293
3630
  /**
2294
3631
  * Convert UIBlock to JSON-serializable object
@@ -2356,7 +3693,55 @@ declare class Thread {
2356
3693
  * @param currentUIBlockId - ID of current UIBlock to exclude from context (optional)
2357
3694
  * @returns Formatted conversation history string
2358
3695
  */
2359
- getConversationContext(limit?: number, currentUIBlockId?: string): string;
3696
+ /**
3697
+ * The script recipe bound to the most recent completed UIBlock — i.e. the
3698
+ * script behind the answer the user is currently looking at. Drives the
3699
+ * matcher's `edit` tier: without it, "use mode instead" has no target and
3700
+ * falls through to regeneration.
3701
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 1.
3702
+ */
3703
+ getActiveScriptBinding(currentUIBlockId?: string): Record<string, any> | null;
3704
+ /**
3705
+ * The recent script-backed answers in this thread, newest first — the
3706
+ * candidate set a user-directed edit can target.
3707
+ *
3708
+ * Returning several rather than only the newest is what lets an instruction
3709
+ * name its own target ("use the mode for the WSP one"). With a single
3710
+ * candidate every edit lands on the most recent script, which silently edits
3711
+ * the wrong recipe whenever the user meant an earlier one.
3712
+ *
3713
+ * Deduped by recipeId (newest occurrence wins) so a long editing session on
3714
+ * one script doesn't crowd out the others. Each entry carries the question
3715
+ * that produced it — without that the candidates are indistinguishable.
3716
+ *
3717
+ * In-memory only: dies with the process. The caller falls back to the
3718
+ * persisted bindings when this comes back empty.
3719
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 1.
3720
+ */
3721
+ getScriptBindings(limit?: number, currentUIBlockId?: string): Record<string, any>[];
3722
+ /**
3723
+ * @param getScriptBody Resolves a script-backed turn's EXACT script, pinned
3724
+ * to the version that turn actually ran — never today's (possibly
3725
+ * since-edited) live body. Owned by the caller (needs a ScriptStore, which
3726
+ * Thread doesn't hold) — omit it and every turn falls back to the
3727
+ * no-script summary below.
3728
+ * @param fetchRecentFromDb When provided, this is now the SOURCE OF TRUTH
3729
+ * for history instead of this process's in-memory `uiblocks` map — reads
3730
+ * the persisted `user_conversations` rows (DB `response` jsonb shape, see
3731
+ * transformUIBlockForDB in conversation-saver.ts) via the caller's own
3732
+ * `user-conversations.getRecentForContext` collection. Rows come back
3733
+ * newest-first (already excluding status='aborted' — the query itself
3734
+ * filters that, matching what the in-memory path achieved via
3735
+ * thread.removeUIBlock() on abort). Fixes the standing gap where a backend
3736
+ * restart lost every thread's history (ThreadManager has no DB-backed
3737
+ * rehydration) — DB-backed history survives that, and is correct even if
3738
+ * a different process instance saved the row. Omit it and this falls back
3739
+ * to the in-memory map, for any caller not yet updated.
3740
+ */
3741
+ getConversationContext(limit?: number, currentUIBlockId?: string, getScriptBody?: (recipeId: string, version: number) => Promise<string | null>, fetchRecentFromDb?: (limit: number) => Promise<Array<{
3742
+ userprompt?: string;
3743
+ response?: Record<string, any>;
3744
+ }>>): Promise<string>;
2360
3745
  /**
2361
3746
  * Convert Thread to JSON-serializable object
2362
3747
  */
@@ -2950,21 +4335,20 @@ declare abstract class BaseLLM {
2950
4335
  */
2951
4336
  protected containsFormComponent(component: any): boolean;
2952
4337
  /**
2953
- * Match components from text response suggestions and generate follow-up questions
4338
+ * Match components from text response suggestions
2954
4339
  * Takes a text response with component suggestions (c1:type format) and matches with available components
2955
- * Also generates title, description, and intelligent follow-up questions (actions) based on the analysis
4340
+ * Also generates title and description based on the analysis
2956
4341
  * All components are placed in a default MultiComponentContainer layout
2957
4342
  * @param analysisContent - The text response containing component suggestions
2958
4343
  * @param components - List of available components
2959
4344
  * @param apiKey - Optional API key
2960
4345
  * @param componentStreamCallback - Optional callback to stream primary KPI component as soon as it's identified
2961
- * @returns Object containing matched components, layout title/description, and follow-up actions
4346
+ * @returns Object containing matched components and layout title/description
2962
4347
  */
2963
4348
  matchComponentsFromAnalysis(analysisContent: string, components: Component[], userPrompt: string, apiKey?: string, componentStreamCallback?: (component: Component) => void, deferredTools?: any[], executedTools?: any[], collections?: any, userId?: string): Promise<{
2964
4349
  components: Component[];
2965
4350
  layoutTitle: string;
2966
4351
  layoutDescription: string;
2967
- actions: Action[];
2968
4352
  }>;
2969
4353
  /**
2970
4354
  * Classify user question into category and detect external tools needed
@@ -3013,12 +4397,6 @@ declare abstract class BaseLLM {
3013
4397
  * 3. Route appropriately based on category and response mode
3014
4398
  */
3015
4399
  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>;
3016
- /**
3017
- * Generate next questions that the user might ask based on the original prompt and generated component
3018
- * This helps provide intelligent suggestions for follow-up queries
3019
- * For general/conversational questions without components, pass textResponse instead
3020
- */
3021
- generateNextQuestions(originalUserPrompt: string, component?: Component | null, componentData?: Record<string, unknown>, apiKey?: string, conversationHistory?: string, textResponse?: string): Promise<string[]>;
3022
4400
  }
3023
4401
 
3024
4402
  interface AnthropicLLMConfig extends BaseLLMConfig {
@@ -3224,6 +4602,842 @@ declare class DashboardConversationHistory {
3224
4602
  }
3225
4603
  declare const dashboardConversationHistory: DashboardConversationHistory;
3226
4604
 
4605
+ /**
4606
+ * Whole-dashboard generation via Pi, a terminal coding agent — as opposed to
4607
+ * DASH_COMP_REQ's single-widget-at-a-time flow. Runs Pi in-process via its
4608
+ * SDK (createAgentSession), not as a subprocess: no shell, no argument
4609
+ * quoting, no stdin/stdout piping, none of the Windows-specific subprocess
4610
+ * issues that came with spawning the `pi` CLI directly.
4611
+ *
4612
+ * Called from sdk-nodejs/src/dashboardAgent/index.ts (DASHBOARD_AGENT_REQ),
4613
+ * which owns the generic streaming/abort machinery (mirrors USER_PROMPT_REQ)
4614
+ * and passes `signal`/`onProgress` alongside the normal params — this stays
4615
+ * within CollectionHandler's loose (params) => Promise<result> typing, no
4616
+ * change needed to that shared type.
4617
+ *
4618
+ * This mechanism is generic and reusable across any deployment. What's
4619
+ * genuinely project-specific — where AGENTS.md lives, which model to use —
4620
+ * is supplied via `DashboardAgentCollectionConfig`, with defaults sensible
4621
+ * enough that most callers don't need to override them (see below). The
4622
+ * data-source tool list and the dashboard's current state both come from
4623
+ * things sdk-nodejs already exposes generically: `sdk.getTools()` (whatever
4624
+ * this deployment registered via `sdk.setTools()`) and `sdk.callCollection
4625
+ * ('dashboards', 'query', ...)` (whatever this deployment already registered
4626
+ * under that name/shape) — no per-deployment callback needed for either.
4627
+ * Same convention on the way out: after a successful run, the prompt and the
4628
+ * full response text are handed to `sdk.callCollection('dashboard-agent-
4629
+ * conversations', 'create', ...)` if this deployment has registered one —
4630
+ * skipped silently otherwise, since conversation history is optional.
4631
+ *
4632
+ * Pi verifies every query against the live database itself (via whatever
4633
+ * local tool-execution bridge the deployment exposes, e.g. an HTTP bridge
4634
+ * on localhost), but does NOT persist the result itself — it writes the
4635
+ * finished DSL to an absolute path inside `runtimeDir`, told to it explicitly
4636
+ * in the prompt, and stops there. This handler reads that file after the run
4637
+ * finishes and returns its content as `dashboard` in the result. The caller
4638
+ * (frontend) is the one that actually saves it, via whatever authenticated
4639
+ * create/update path any other dashboard edit goes through — Pi has no user
4640
+ * session/auth context of its own, so persistence shouldn't happen from
4641
+ * inside it.
4642
+ *
4643
+ * Session persistence: the FIRST call for a dashboardId pays the full cost
4644
+ * (explore KB, discover schema, plan, verify, build). Every call after that
4645
+ * resumes the same session file (SessionManager.open) so Pi has everything
4646
+ * it already learned — it only needs to reason about the new, smaller ask,
4647
+ * not rediscover the whole dashboard from scratch. The session's file path
4648
+ * (AgentSession.sessionFile) is captured right after creation and persisted
4649
+ * in a small local file, keyed by dashboardId, inside `runtimeDir`.
4650
+ */
4651
+ interface DashboardAgentCollectionConfig {
4652
+ /**
4653
+ * Working directory Pi runs from — must contain AGENTS.md. This is a
4654
+ * version-controlled prompt file, so `cwd` is expected to live somewhere
4655
+ * like a `.prompts/` folder alongside the deployment's other prompts.
4656
+ * Default: `<process.cwd()>/.prompts/dashboard-agent` — the same
4657
+ * process.cwd()-based convention PromptLoader already uses for the main
4658
+ * agent's prompts, which needs no explicit override in the common case
4659
+ * (the backend process's own cwd already is its project root).
4660
+ */
4661
+ cwd?: string;
4662
+ /**
4663
+ * Where drafts/, the session-id map, and dashboard.log get written —
4664
+ * separate from `cwd` deliberately, so this deployment's runtime state
4665
+ * (regenerated per session, safe to gitignore) doesn't sit inside the
4666
+ * same folder as the version-controlled AGENTS.md prompt.
4667
+ * Default: `<process.cwd()>/.pi-dashboard-agent-runtime`.
4668
+ */
4669
+ runtimeDir?: string;
4670
+ /** Model provider (default: process.env.PI_AGENT_PROVIDER || 'openrouter'). */
4671
+ provider?: string;
4672
+ /** Model id (default: process.env.PI_AGENT_MODEL || 'anthropic/claude-sonnet-4.5'). */
4673
+ model?: string;
4674
+ /**
4675
+ * true (default): every request starts a brand-new pi session, with the 2
4676
+ * most recent prior responses (if any) injected into the prompt as
4677
+ * context — bounded cost per request, but pi re-explores schema/KB facts
4678
+ * it already verified in an earlier turn on this same dashboard.
4679
+ * false: resumes the same session file across requests on a given
4680
+ * dashboard — pi keeps everything it already learned, but context (and
4681
+ * cost) grows unbounded across turns (one observed turn: 2M+ cache-read
4682
+ * tokens after a handful of edits on the same dashboard).
4683
+ * Default: process.env.PI_AGENT_FRESH_SESSION !== 'false'.
4684
+ */
4685
+ freshSession?: boolean;
4686
+ }
4687
+ declare function registerDashboardAgentCollection(sdk: SuperatomSDK, config?: DashboardAgentCollectionConfig): void;
4688
+
4689
+ /**
4690
+ * Coding-agent driver contract — one shape over every harness (codex today;
4691
+ * claude-code / opencode / pi next). Callers depend on this file only, never on
4692
+ * a harness SDK, so swapping the agent behind a task is a config change.
4693
+ *
4694
+ * Deliberately narrower than the harness SDKs it wraps: the three things a
4695
+ * production run needs and that harness abstractions usually omit — real abort,
4696
+ * real token accounting, and structured tool args — are part of the contract,
4697
+ * not per-driver extras.
4698
+ */
4699
+ type AgentEventKind = 'turn' | 'message' | 'reasoning' | 'command' | 'tool' | 'file' | 'todo' | 'error';
4700
+ interface AgentEvent {
4701
+ kind: AgentEventKind;
4702
+ /** Stable across started→updated→completed, so a UI updates one block in place. */
4703
+ id?: string;
4704
+ text?: string;
4705
+ command?: string;
4706
+ output?: string;
4707
+ /** Tool calls carry real parsed args — never SQL scraped back out of a shell string. */
4708
+ tool?: {
4709
+ server?: string;
4710
+ name: string;
4711
+ args?: unknown;
4712
+ };
4713
+ status?: 'in_progress' | 'completed' | 'failed';
4714
+ done?: boolean;
4715
+ }
4716
+ interface AgentUsage {
4717
+ inputTokens: number;
4718
+ outputTokens: number;
4719
+ reasoningTokens: number;
4720
+ cacheReadTokens: number;
4721
+ cacheWriteTokens: number;
4722
+ /** Tool + command invocations. With tokens, separates "silently did nothing" from a real run. */
4723
+ toolCalls: number;
4724
+ /** Only when the harness reports it — codex reports tokens but not cost. */
4725
+ costUSD?: number;
4726
+ }
4727
+ interface AgentRunHandlers {
4728
+ /** Formatted live stream: assistant text deltas plus one-line tool/command markers. */
4729
+ onOutput?: (chunk: string) => void;
4730
+ onEvent?: (event: AgentEvent) => void;
4731
+ /** Assistant prose only — safe to show a non-technical user as progress. */
4732
+ onNarration?: (text: string) => void;
4733
+ onUsage?: (usage: AgentUsage) => void;
4734
+ /**
4735
+ * Polled once the turn is live. When it returns true the run ends immediately —
4736
+ * for tasks whose deliverable is a file, the moment it exists there is nothing
4737
+ * left to wait for.
4738
+ */
4739
+ doneWhen?: () => boolean | Promise<boolean>;
4740
+ }
4741
+ type AgentRunStatus = 'completed' | 'early' | 'aborted' | 'error';
4742
+ interface AgentRunResult {
4743
+ status: AgentRunStatus;
4744
+ /** The final assistant message. */
4745
+ answer: string;
4746
+ ms: number;
4747
+ usage: AgentUsage;
4748
+ /**
4749
+ * Set when status is 'error'. Covers stream-reported failures too, which arrive
4750
+ * as events rather than thrown exceptions — a swallowed provider error is the
4751
+ * failure mode that otherwise looks like a successful empty run.
4752
+ */
4753
+ error?: string;
4754
+ sessionId?: string;
4755
+ }
4756
+ interface McpServerSpec {
4757
+ command: string;
4758
+ args?: string[];
4759
+ env?: Record<string, string>;
4760
+ }
4761
+ interface CodingAgentSessionOptions {
4762
+ /** Working directory the agent operates in. Scope it to a runtime dir, not a source checkout. */
4763
+ cwd: string;
4764
+ model?: string;
4765
+ /** Resume a prior conversation — the value a previous run returned as sessionId. */
4766
+ resumeId?: string;
4767
+ /** Tools, as MCP servers. Keyed by server name, which shows up on every tool event. */
4768
+ mcpServers?: Record<string, McpServerSpec>;
4769
+ /**
4770
+ * Extra env for the agent process. By default only a small allowlist is passed
4771
+ * through (see ENV_ALLOWLIST) so an agent with shell access can't read the
4772
+ * backend's own secrets out of its environment.
4773
+ */
4774
+ env?: Record<string, string>;
4775
+ /** Opt out of env scrubbing. Only for local debugging. */
4776
+ inheritEnv?: boolean;
4777
+ }
4778
+ interface CodingAgentSession {
4779
+ readonly harness: string;
4780
+ /** Structured event log vs. a raw terminal stream — the UI picks its renderer from this. */
4781
+ readonly kind: 'events' | 'pty';
4782
+ run(prompt: string, handlers?: AgentRunHandlers, signal?: AbortSignal): Promise<AgentRunResult>;
4783
+ /** Rolling structured events, for replay when a client reconnects mid-run. */
4784
+ events(): AgentEvent[];
4785
+ /** Rolling formatted text, same purpose as events() for text-oriented views. */
4786
+ buffer(): string;
4787
+ busy(): boolean;
4788
+ /** Persist this and pass it back as resumeId to continue the conversation. */
4789
+ sessionId(): string | undefined;
4790
+ /** Abandon the conversation; the next run starts fresh. */
4791
+ reset(): void;
4792
+ dispose(): void;
4793
+ }
4794
+
4795
+ /**
4796
+ * Harness registry. Kept separate from index.ts so collection.ts can reach the
4797
+ * factory without importing the module's own barrel (a cycle tsup would have to
4798
+ * resolve at bundle time).
4799
+ */
4800
+ type Harness = 'codex';
4801
+ declare function createCodingAgentSession(harness: Harness, opts: CodingAgentSessionOptions): CodingAgentSession;
4802
+
4803
+ /**
4804
+ * Codex driver — OpenAI Codex via @openai/codex-sdk.
4805
+ *
4806
+ * FORMAT CONTRACT: written against @openai/codex-sdk 0.149.1. Harness event
4807
+ * shapes drift; if the event log goes blank after an SDK bump, check normEvent
4808
+ * against the package's dist/index.d.ts first.
4809
+ *
4810
+ * Three things this leans on that the SDK gives natively, so we don't reinvent
4811
+ * them: TurnOptions.signal (real cancellation), TurnCompletedEvent.usage (token
4812
+ * accounting), and mcp_tool_call items (tool args arrive parsed, not scraped
4813
+ * out of a shell command string).
4814
+ *
4815
+ * The package is an optionalDependency: every deployment gets it, but an
4816
+ * install that cannot fetch the ~309MB platform binary skips it rather than
4817
+ * failing. It is therefore loaded dynamically and its types mirrored
4818
+ * structurally below, so a VM without it still builds, starts, and runs
4819
+ * everything else — only the coding agent reports itself unavailable.
4820
+ */
4821
+ type CodexSandboxMode = 'read-only' | 'workspace-write' | 'danger-full-access';
4822
+ type CodexReasoningEffort = 'minimal' | 'low' | 'medium' | 'high' | 'xhigh' | 'max' | 'ultra';
4823
+ interface CodexSessionOptions extends CodingAgentSessionOptions {
4824
+ reasoningEffort?: CodexReasoningEffort;
4825
+ /**
4826
+ * Filesystem reach. Default 'workspace-write' — the agent writes its
4827
+ * deliverable into cwd and nothing else. 'danger-full-access' hands a
4828
+ * shell-capable agent the whole box; don't use it on a client VM.
4829
+ */
4830
+ sandboxMode?: CodexSandboxMode;
4831
+ /** Network for commands the agent runs. Off by default; MCP tools don't need it. */
4832
+ networkAccessEnabled?: boolean;
4833
+ apiKey?: string;
4834
+ baseUrl?: string;
4835
+ /** Raw `--config` overrides merged into the codex config. */
4836
+ extraConfig?: Record<string, unknown>;
4837
+ approvalPolicy?: 'never' | 'on-request' | 'on-failure' | 'untrusted';
4838
+ }
4839
+ declare function createCodexSession(opts: CodexSessionOptions): CodingAgentSession;
4840
+
4841
+ /**
4842
+ * What makes an agent fire.
4843
+ *
4844
+ * Declared ON the definition, deliberately. If triggers lived at the call sites
4845
+ * instead — a counter in the conversation handler, a hook in the kb-nodes
4846
+ * collection — then adding an agent would mean editing unrelated files across
4847
+ * the backend, and no one file would tell you when an agent actually runs.
4848
+ * Here, an agent's folder plus its definition is the whole story.
4849
+ *
4850
+ * The deployment's only job is to announce that something happened:
4851
+ *
4852
+ * emitAgentEvent('conversation.answered', { threadId })
4853
+ * emitAgentEvent('kb-node.changed', { id, op: 'update' })
4854
+ *
4855
+ * Which agents care, and how many events it takes, is the agents' business.
4856
+ * The emitter therefore speaks the WORLD's vocabulary, never any one agent's —
4857
+ * translating an event into a particular agent's input is `buildInput` below.
4858
+ */
4859
+ /** One announced fact, as it reaches a trigger. */
4860
+ interface TriggerEvent {
4861
+ /** The event name passed to emitAgentEvent. */
4862
+ name: string;
4863
+ payload?: unknown;
4864
+ }
4865
+ /**
4866
+ * Turns the events that caused this run into the agent's `input`.
4867
+ *
4868
+ * Takes the whole list, not one payload, because every batching trigger here
4869
+ * fires for MANY events: a debounced burst, or N counted occurrences. Handing
4870
+ * over only the last one silently discarded the rest — five edited nodes became
4871
+ * one.
4872
+ *
4873
+ * Omit it and the agent receives the most recent payload unchanged, which is
4874
+ * the historical behaviour. Any agent that batches should declare one.
4875
+ */
4876
+ type BuildTriggerInput = (events: TriggerEvent[]) => unknown;
4877
+ type AgentTrigger =
4878
+ /** Only runCodingAgent(). The default. */
4879
+ {
4880
+ type: 'manual';
4881
+ }
4882
+ /** Every N milliseconds while the process is up. Fires with no events. */
4883
+ | {
4884
+ type: 'interval';
4885
+ everyMs: number;
4886
+ buildInput?: BuildTriggerInput;
4887
+ }
4888
+ /**
4889
+ * On a named event. `debounceMs` collapses a burst into one run — editing
4890
+ * five knowledge nodes in a row is one reason to rebuild, not five. The run
4891
+ * still receives all five events; only the RUNS are collapsed.
4892
+ */
4893
+ | {
4894
+ type: 'event';
4895
+ on: string | string[];
4896
+ debounceMs?: number;
4897
+ buildInput?: BuildTriggerInput;
4898
+ }
4899
+ /**
4900
+ * Every Nth occurrence of an event. The events are PERSISTED, so "after 10
4901
+ * questions" survives a restart instead of silently starting over.
4902
+ */
4903
+ | {
4904
+ type: 'count';
4905
+ on: string;
4906
+ every: number;
4907
+ buildInput?: BuildTriggerInput;
4908
+ };
4909
+ interface TriggerFireInfo {
4910
+ agentId: string;
4911
+ /** What set it off, for the log line. */
4912
+ reason: string;
4913
+ /** Everything that caused this run, in arrival order. */
4914
+ events: TriggerEvent[];
4915
+ /** What buildInput produced — exactly what the agent was handed. */
4916
+ input?: unknown;
4917
+ }
4918
+ /** Notified after every triggered run — for recording history or alerting. */
4919
+ type RunListener = (record: AgentRunRecord, info: TriggerFireInfo) => void;
4920
+ declare function onAgentRun(fn: RunListener): () => void;
4921
+ /**
4922
+ * Tell the agents something happened. Safe to call from anywhere, including a
4923
+ * request path — it never throws and never blocks: matching runs are started,
4924
+ * not awaited.
4925
+ */
4926
+ declare function emitAgentEvent(event: string, payload?: unknown): void;
4927
+ /**
4928
+ * Start interval triggers. Call once at startup, after registerCodingAgents.
4929
+ * Event and count triggers need nothing started — they wake on emitAgentEvent.
4930
+ */
4931
+ declare function startAgentTriggers(): void;
4932
+ declare function stopAgentTriggers(): void;
4933
+ /** What fires each agent — for a startup log or an admin screen. */
4934
+ declare function describeTriggers(): Array<{
4935
+ agentId: string;
4936
+ triggers: string[];
4937
+ }>;
4938
+
4939
+ /**
4940
+ * What a coding agent IS.
4941
+ *
4942
+ * An agent is a job description, not code that does the job: instructions, the
4943
+ * inputs it should be handed, the tools it may reach, the deliverable it must
4944
+ * produce, and what we do with that deliverable. The agent itself decides HOW —
4945
+ * it can read the inputs, write and run scripts over them, check its own work,
4946
+ * and iterate. That is the whole reason for using a coding agent rather than a
4947
+ * single LLM call: the job's data never has to fit in a context window.
4948
+ *
4949
+ * Adding an agent is a folder with a system.md, one file implementing this
4950
+ * interface, and one line in the registry. Nothing else changes.
4951
+ */
4952
+ /** The disposable directory one run happens in. */
4953
+ interface Workspace {
4954
+ /** Absolute path the agent runs in. Everything below is relative to it. */
4955
+ dir: string;
4956
+ /** Unique per run — also the folder name, so runs never collide. */
4957
+ runId: string;
4958
+ /** Write a file into the workspace, creating parent directories. */
4959
+ write(relPath: string, contents: string): Promise<void>;
4960
+ /** Write JSON (pretty-printed — the agent reads these by eye as often as by script). */
4961
+ writeJson(relPath: string, value: unknown): Promise<void>;
4962
+ /** Write one JSON object per line. The right shape for anything the agent will stream or grep. */
4963
+ writeJsonl(relPath: string, rows: unknown[]): Promise<void>;
4964
+ /** Read a file the agent produced. Returns null when it isn't there. */
4965
+ read(relPath: string): Promise<string | null>;
4966
+ /** Absolute path for a workspace-relative path — for telling the agent where to write. */
4967
+ pathTo(relPath: string): string;
4968
+ }
4969
+ /** Minimal validator shape, so this file needn't depend on a schema library. */
4970
+ interface OutputValidator<T> {
4971
+ /** Throw with a useful message, or return the parsed value. Zod's `.parse` satisfies this. */
4972
+ parse(value: unknown): T;
4973
+ }
4974
+ interface OutputContract<T> {
4975
+ /**
4976
+ * Workspace-relative path the agent must write, e.g. 'out/concepts.json'.
4977
+ * Nothing else counts as the deliverable: the run ends the moment this
4978
+ * appears, and a run that finishes without it is a failure, not a partial
4979
+ * success.
4980
+ */
4981
+ file: string;
4982
+ /** Validated before anything is persisted, so a malformed run fails loudly and applies nothing. */
4983
+ schema: OutputValidator<T>;
4984
+ }
4985
+ /** What apply() reports back — surfaced in the run record and to the caller. */
4986
+ interface ApplySummary {
4987
+ /** One line a human can read in a log or the UI. */
4988
+ summary: string;
4989
+ /** Whatever counts for this agent: {created, updated, skipped}, etc. */
4990
+ counts?: Record<string, number>;
4991
+ /** Anything the caller may want to inspect or show. */
4992
+ details?: unknown;
4993
+ }
4994
+ interface AgentRunContext {
4995
+ projectId?: string;
4996
+ userId?: string;
4997
+ /**
4998
+ * This run's id — also the workspace directory name. Passed so apply() can
4999
+ * stamp whatever it persists with the run that produced it, which is the
5000
+ * only link from a stored result back to the inputs and scripts behind it.
5001
+ */
5002
+ runId?: string;
5003
+ /** The run's workspace, for an apply() that needs a file the agent left. */
5004
+ workspaceDir?: string;
5005
+ /** Cancels the run; forwarded to the driver's real abort. */
5006
+ signal?: AbortSignal;
5007
+ /** Live progress. Populated for chat mode; a task run may also stream to a log. */
5008
+ onOutput?: (chunk: string) => void;
5009
+ onEvent?: (event: AgentEvent) => void;
5010
+ onUsage?: (usage: AgentUsage) => void;
5011
+ }
5012
+ /**
5013
+ * How much an agent carries between runs.
5014
+ *
5015
+ * Codex manages its own context: it tracks the window and compacts when it
5016
+ * crosses `auto_compact_token_limit`, emitting a ContextCompacted event. That
5017
+ * happens with no involvement from us, so a persistent session survives
5018
+ * indefinitely on its own. (The mechanism is in the engine but not the
5019
+ * TypeScript SDK — Thread exposes only id/run/runStreamed — which is why there
5020
+ * is nothing here to call.)
5021
+ *
5022
+ * What compaction cannot do is make the session authoritative. It summarises to
5023
+ * make room, so detail is lost by design; and ~/.codex/sessions is a directory
5024
+ * on one machine, not the database. So durable facts belong in `memory/` files
5025
+ * and in the tables the agent writes through apply(), with the session free to
5026
+ * compact itself as it likes. Memory on disk, context managed by codex.
5027
+ */
5028
+ interface SessionPolicy {
5029
+ /**
5030
+ * 'per-run' a fresh harness session each time (default).
5031
+ * 'persistent' resume the same session across runs, so the agent keeps its
5032
+ * working context. Always paired with durable files — never rely
5033
+ * on it alone.
5034
+ */
5035
+ scope?: 'per-run' | 'persistent';
5036
+ /**
5037
+ * 'per-run' a throwaway directory per run (default); pruned by keepRuns.
5038
+ * 'persistent' one directory the agent owns across runs. `memory/` survives
5039
+ * untouched; `input/` is cleared each run so a stale input can
5040
+ * never be mistaken for this run's.
5041
+ */
5042
+ workspace?: 'per-run' | 'persistent';
5043
+ /**
5044
+ * Safety valve, not the compaction mechanism — codex already compacts on its
5045
+ * own. Starts a fresh session after this many runs while keeping the
5046
+ * workspace, for when you want a hard ceiling on how much summarised history
5047
+ * an agent drags along, or a clean slate on a cadence. Omit by default.
5048
+ */
5049
+ resetAfterTurns?: number;
5050
+ }
5051
+ interface CodingAgentDefinition<TInput = unknown, TOutput = unknown> {
5052
+ /** Stable id: the folder name under .prompts/coding-agents, and the key callers use. */
5053
+ id: string;
5054
+ /** Human-readable, for the UI and logs. */
5055
+ title: string;
5056
+ /** One line: what this agent is for. */
5057
+ description?: string;
5058
+ /**
5059
+ * 'task' — headless. Runs from a cron, an event, or a command; produces the
5060
+ * deliverable and applies it. Nobody is watching.
5061
+ * 'chat' — a person is typing. Streams, keeps its session across turns, and
5062
+ * is not required to produce a deliverable at all.
5063
+ */
5064
+ mode: 'task' | 'chat';
5065
+ /**
5066
+ * Directory holding this agent's system.md. Defaults to
5067
+ * `<promptRoot>/<id>`. The runner copies system.md into the workspace as
5068
+ * AGENTS.md, which is the filename the harness actually reads from its
5069
+ * working directory — that indirection is what lets the instructions stay
5070
+ * version-controlled while the workspace stays disposable.
5071
+ */
5072
+ promptDir?: string;
5073
+ /** Override the deployment default for this agent alone. */
5074
+ harness?: Harness;
5075
+ model?: string;
5076
+ /**
5077
+ * Let this agent's shell reach the network. Off by default.
5078
+ *
5079
+ * Needed by any agent given bridge shims (writeBridgeShims), which reach the
5080
+ * backend over localhost HTTP. Note the sandbox has no host allowlist, so
5081
+ * this grants the whole internet, not just the bridge — scope it to agents
5082
+ * that genuinely need it.
5083
+ */
5084
+ networkAccessEnabled?: boolean;
5085
+ /**
5086
+ * Filesystem reach. Defaults to the driver's 'workspace-write'.
5087
+ * 'danger-full-access' removes the sandbox entirely — it is the only mode in
5088
+ * which codex permits MCP tool calls headlessly, which is exactly why the
5089
+ * shims exist instead.
5090
+ */
5091
+ sandboxMode?: 'read-only' | 'workspace-write' | 'danger-full-access';
5092
+ /**
5093
+ * Tools this agent may reach, as MCP servers. Deliberately per-agent: an
5094
+ * agent that reads the knowledge base has no business holding a database
5095
+ * connection. Omit for an agent that only needs the files it is given.
5096
+ */
5097
+ tools?: Record<string, McpServerSpec>;
5098
+ /**
5099
+ * Put the job's inputs in the workspace as FILES, not prompt text. A coding
5100
+ * agent can grep, aggregate and script over a file; it cannot do any of that
5101
+ * with text pasted into its prompt, and a large input would not fit there
5102
+ * anyway.
5103
+ */
5104
+ prepare?(ws: Workspace, input: TInput, ctx: AgentRunContext): Promise<void>;
5105
+ /**
5106
+ * The short framing sent as the turn's prompt. Keep it short — the standing
5107
+ * instructions belong in system.md and the data belongs in files. Defaults
5108
+ * to a generic instruction naming the output path.
5109
+ */
5110
+ buildPrompt?(ws: Workspace, input: TInput, ctx: AgentRunContext): string | Promise<string>;
5111
+ /** Required for 'task' mode; meaningless for 'chat'. */
5112
+ output?: OutputContract<TOutput>;
5113
+ /**
5114
+ * Persist the validated deliverable — through existing services, never raw
5115
+ * SQL from inside the agent. Keeping the write on this side is what makes a
5116
+ * run auditable and replayable: the artifact on disk is the same input
5117
+ * apply() would get again tomorrow.
5118
+ */
5119
+ apply?(result: TOutput, input: TInput, ctx: AgentRunContext): Promise<ApplySummary>;
5120
+ /**
5121
+ * What makes this agent run. Declared here so an agent's folder plus this
5122
+ * file is the complete story of when it fires. See triggers.ts.
5123
+ */
5124
+ trigger?: AgentTrigger | AgentTrigger[];
5125
+ /**
5126
+ * WHO this agent's accumulated state belongs to. Distinct from `mode`, which
5127
+ * is only how a run was triggered — the two were conflated before, so a
5128
+ * project agent someone chatted with silently got a private per-thread
5129
+ * workspace and lost its memory.
5130
+ *
5131
+ * 'project' ONE workspace and session for the whole deployment, whoever
5132
+ * invokes it. A concept builder is project-wide: everything it
5133
+ * learns should accumulate in one place, and a chat turn joins
5134
+ * that same workspace rather than forking a copy. Concurrent
5135
+ * runs serialise.
5136
+ * 'user' one per user, shared across that user's threads.
5137
+ * 'thread' one per conversation — an ordinary chat assistant.
5138
+ *
5139
+ * Default: 'thread' for mode 'chat', 'project' for mode 'task'.
5140
+ */
5141
+ scope?: 'project' | 'user' | 'thread';
5142
+ /**
5143
+ * Kill the turn the moment the deliverable file appears, instead of letting
5144
+ * the agent finish.
5145
+ *
5146
+ * Off by default, and the reason is measured: an agent typically writes its
5147
+ * output and then keeps going — verifying, re-reading, summarising — so
5148
+ * stopping at the write cuts the turn well before codex emits
5149
+ * turn.completed, which is the only event carrying token usage. Early stop
5150
+ * therefore costs you all cost tracking, and skips the agent's own
5151
+ * self-check. Turn it on for a long-tailed agent where the file really is
5152
+ * the last thing that matters.
5153
+ */
5154
+ stopOnDeliverable?: boolean;
5155
+ /** What this agent carries between runs. Default: nothing (fresh session and workspace). */
5156
+ session?: SessionPolicy;
5157
+ /** Hard cap for one run. Default 15 minutes. */
5158
+ timeoutMs?: number;
5159
+ }
5160
+ /** What a completed run reports. */
5161
+ interface AgentRunRecord<TOutput = unknown> {
5162
+ agentId: string;
5163
+ runId: string;
5164
+ /** The run's directory — instructions, inputs, scripts and output all still there. */
5165
+ workspaceDir: string;
5166
+ /**
5167
+ * 'early' the deliverable landed and we stopped the turn — a success, kept
5168
+ * distinct from 'completed' so a masked early exit is visible.
5169
+ * 'skipped' another run of this agent held the lock; nothing was done.
5170
+ */
5171
+ status: 'completed' | 'early' | 'aborted' | 'error' | 'skipped';
5172
+ /** The agent's closing message. */
5173
+ answer: string;
5174
+ ms: number;
5175
+ usage: AgentUsage;
5176
+ /** Present when the agent produced and validated its deliverable. */
5177
+ output?: TOutput;
5178
+ /** Present when apply() ran. */
5179
+ applied?: ApplySummary;
5180
+ error?: string;
5181
+ /** Harness session id, for resuming a chat. */
5182
+ sessionId?: string;
5183
+ }
5184
+
5185
+ /**
5186
+ * The agent registry — one place that knows which agents this deployment has.
5187
+ *
5188
+ * Deliberately a plain module-level map rather than something on the SDK: a
5189
+ * cron job, a CLI command and the websocket handler all need to reach the same
5190
+ * agents, and only one of those has an SDK instance in hand.
5191
+ */
5192
+ interface CodingAgentsConfig {
5193
+ /**
5194
+ * Where per-agent instruction folders live. Each agent reads
5195
+ * `<promptRoot>/<agent.id>/system.md`.
5196
+ * Default: `<cwd>/.prompts/coding-agents`, matching the .prompts convention
5197
+ * the rest of the deployment already uses.
5198
+ */
5199
+ promptRoot?: string;
5200
+ /**
5201
+ * Root for run workspaces: `<runtimeDir>/<agentId>/<runId>/`. Regenerated
5202
+ * per run and safe to gitignore — deliberately not inside promptRoot, which
5203
+ * is version-controlled.
5204
+ * Default: `<cwd>/.coding-agent-runtime`.
5205
+ */
5206
+ runtimeDir?: string;
5207
+ /** Fallback harness for agents that don't name one. */
5208
+ harness?: Harness;
5209
+ /** Fallback model. */
5210
+ model?: string;
5211
+ /** Tools every agent gets, merged under each agent's own (the agent's win on a name clash). */
5212
+ sharedTools?: Record<string, McpServerSpec>;
5213
+ /** Keep this many run directories per agent; older ones are pruned. 0 keeps everything. */
5214
+ keepRuns?: number;
5215
+ }
5216
+ /**
5217
+ * Register this deployment's agents. Call once at startup.
5218
+ *
5219
+ * Re-registering an id replaces it — that keeps hot-reload working in dev and
5220
+ * makes a double registration a no-op rather than a silent duplicate.
5221
+ */
5222
+ declare function registerCodingAgents(definitions: CodingAgentDefinition<any, any>[], options?: CodingAgentsConfig): void;
5223
+ declare function getCodingAgent(id: string): CodingAgentDefinition<any, any> | undefined;
5224
+ declare function listCodingAgents(): Array<{
5225
+ id: string;
5226
+ title: string;
5227
+ description?: string;
5228
+ mode: 'task' | 'chat';
5229
+ }>;
5230
+ declare function getCodingAgentsConfig(): Required<Pick<CodingAgentsConfig, "harness" | "promptRoot" | "runtimeDir" | "keepRuns">> & CodingAgentsConfig;
5231
+
5232
+ interface RunAgentOptions<TInput> extends AgentRunContext {
5233
+ input?: TInput;
5234
+ /** Scopes a 'thread' agent. Ignored by a project-scoped one. */
5235
+ threadId?: string;
5236
+ /**
5237
+ * Escape hatch that pins the workspace directory outright. Almost never
5238
+ * right — it defeats the agent's own scope, which is how a project agent
5239
+ * ends up with a private copy per caller. Left for tests and one-offs.
5240
+ */
5241
+ workspaceDir?: string;
5242
+ /** Resume the harness session — chat continuity across turns and restarts. */
5243
+ resumeId?: string;
5244
+ /** Overrides the definition's prompt entirely. Chat mode: this is the user's message. */
5245
+ prompt?: string;
5246
+ /**
5247
+ * A person is typing, rather than a job running.
5248
+ *
5249
+ * For an agent whose mode is 'task' this turns off the JOB — prepare(), the
5250
+ * deliverable contract and apply() are one unit that sets work up and
5251
+ * consumes it, and a conversation should do neither. The turn joins the
5252
+ * workspace as it stands and its answer is the whole result.
5253
+ *
5254
+ * Without this, chatting with a task agent could only ever fail: the user's
5255
+ * message replaces the prompt that names the output path, so the agent never
5256
+ * writes the deliverable, and the run is then reported as an error despite
5257
+ * having answered perfectly well. Worse, prepare() ran on every turn — for a
5258
+ * batching agent that means a chat turn quietly consumed a batch of work.
5259
+ *
5260
+ * An agent whose mode is 'chat' is unaffected: its prepare() is per-turn
5261
+ * context setup, not job setup, so it still runs.
5262
+ */
5263
+ chat?: boolean;
5264
+ }
5265
+ /**
5266
+ * Run one agent, end to end.
5267
+ *
5268
+ * Never throws for an agent-side failure — a bad run comes back as a record
5269
+ * with status 'error', because callers are usually crons and message handlers
5270
+ * that must keep going. Genuinely programmer errors (unknown agent id) do throw.
5271
+ */
5272
+ declare function runCodingAgent<TInput = unknown, TOutput = unknown>(agentId: string, options?: RunAgentOptions<TInput>): Promise<AgentRunRecord<TOutput>>;
5273
+
5274
+ /**
5275
+ * Write `./sources` and `./query` into an agent's workspace: read-only data
5276
+ * access as plain executables it runs with bash.
5277
+ *
5278
+ * Why not MCP: codex gates MCP tool calls on the sandbox. Under any sandboxed
5279
+ * mode they need approval, and a headless run must set approvalPolicy 'never',
5280
+ * which denies rather than grants — measured, not assumed. Only
5281
+ * danger-full-access permits them, and that removes the filesystem sandbox
5282
+ * entirely. Bash commands under workspace-write need no approval, so a shim
5283
+ * buys the same capability while the agent stays confined to its workspace.
5284
+ *
5285
+ * An MCP server for this was written and then removed: the same approval gate
5286
+ * applies to the Agents chat tab, which is equally headless from codex's point
5287
+ * of view, so it was unreachable everywhere. `mcpServers` stays plumbed through
5288
+ * the driver for third-party servers; see git history for the removed one.
5289
+ *
5290
+ * The cost is structured tool events: calls arrive as `bash → ./query ...`
5291
+ * rather than typed items with parsed arguments. That matters for a chat UI
5292
+ * with a human watching; for a headless task agent it does not.
5293
+ *
5294
+ * Requires networkAccessEnabled on the session — the shim reaches the bridge
5295
+ * over HTTP, and that is the whole point of it.
5296
+ */
5297
+ declare function writeBridgeShims(ws: Workspace, opts: {
5298
+ bridgeUrl: string;
5299
+ sources: Array<{
5300
+ id: string;
5301
+ name?: string;
5302
+ description?: string;
5303
+ }>;
5304
+ maxRows?: number;
5305
+ }): void;
5306
+
5307
+ /**
5308
+ * Read-only SQL guard for any path that hands an agent a database.
5309
+ *
5310
+ * Defence in depth, NOT a boundary. The agent has a shell and can reach the
5311
+ * bridge directly, so anything enforced only here is advisory — it stops
5312
+ * mistakes, not determination. The real boundary is a database user without
5313
+ * write permission; this exists so a confused agent gets a clear correction
5314
+ * instead of a silent partial write.
5315
+ */
5316
+ /** Throws with a message written for the agent to act on. */
5317
+ declare function assertReadOnlySql(sql: string): void;
5318
+
5319
+ /**
5320
+ * The 'coding-agent'.'run' collection — a chat turn against a coding agent.
5321
+ *
5322
+ * Generic on purpose: nothing here knows about dashboards, reports, or any
5323
+ * particular deliverable. It owns what every harness-backed chat needs — one
5324
+ * session per thread, a per-thread workspace, a run lock, and forwarding the
5325
+ * driver's callbacks to the transport. Task-specific flows (prompt assembly, a
5326
+ * draft-file contract, persistence) build on top of this rather than inside it.
5327
+ */
5328
+ interface CodingAgentCollectionConfig {
5329
+ /**
5330
+ * Root for per-thread agent workspaces. Each thread gets its own directory
5331
+ * underneath, so one chat can't read or clobber another's files, and none of
5332
+ * them sit inside the backend's own source tree.
5333
+ * Default: `<process.cwd()>/.coding-agent-runtime`.
5334
+ */
5335
+ runtimeDir?: string;
5336
+ /** Default harness when the request doesn't name one. */
5337
+ harness?: Harness;
5338
+ model?: string;
5339
+ /** Tools, as MCP servers, handed to every session. */
5340
+ mcpServers?: Record<string, McpServerSpec>;
5341
+ }
5342
+ declare function registerCodingAgentCollection(sdk: SuperatomSDK, config?: CodingAgentCollectionConfig): void;
5343
+
5344
+ declare class ScriptMatcher {
5345
+ private store;
5346
+ private collections?;
5347
+ private userId?;
5348
+ private trace?;
5349
+ constructor(store: ScriptStore, opts?: {
5350
+ collections?: any;
5351
+ userId?: string;
5352
+ trace?: RunTrace;
5353
+ });
5354
+ /**
5355
+ * Find the best matching script for a user question.
5356
+ * Uses ONE LLM call that picks the script AND extracts parameters.
5357
+ * Returns null if no script matches.
5358
+ *
5359
+ * Thin wrapper around `doMatch` whose only job is guaranteeing the trace
5360
+ * capture happens exactly once, on every exit path (short-circuit, normal
5361
+ * return, or the LLM call erroring) — see RunTrace.scriptMatcher.
5362
+ */
5363
+ match(userPrompt: string, apiKey?: string, model?: string, signal?: AbortSignal, activeBindings?: ScriptBinding[]): Promise<ScriptMatch | null>;
5364
+ private doMatch;
5365
+ /**
5366
+ * Renders a recipe's declared parameters the same way in every prompt
5367
+ * section (catalog, active-script, edit candidates) so the LLM sees one
5368
+ * consistent format regardless of which block a script came from.
5369
+ */
5370
+ private formatParamList;
5371
+ /**
5372
+ * Semantic search over THIS user's own past prompts (`conversation-history`
5373
+ * ChromaDB embeddings, already user-scoped) — not a project-wide name/tag
5374
+ * search. Groups hits by the recipe that answered them, so a script gets
5375
+ * ONE catalog entry carrying every matched past question, not one per hit.
5376
+ *
5377
+ * Scope is deliberate (see SCRIPT-MATCH-IMPROVEMENTS.md, Change 2): a
5378
+ * script only shows up here because THIS user has, before, asked something
5379
+ * similar that it answered — not because some other user's question did.
5380
+ */
5381
+ private searchPromptHistory;
5382
+ /**
5383
+ * Build the script catalog string for the LLM prompt from prompt-history
5384
+ * matches: each script's declared parameters PLUS the actual past questions
5385
+ * of this user's that it answered (real precedent, not a hand-written
5386
+ * description) and what params they ran with.
5387
+ */
5388
+ private buildPromptHistoryCatalog;
5389
+ /**
5390
+ * The most recent script-backed answer in this thread — the one an `edit`
5391
+ * targets. Rendered as its own prompt section (never merged into the ranked
5392
+ * catalog) so the `edit` rules have an unambiguous referent.
5393
+ *
5394
+ * `candidates` holds at most one entry (see doMatch) — `edit` always means
5395
+ * "the answer I just got", so there is nothing else to show here.
5396
+ *
5397
+ * Carries the QUESTION that produced it plus the columns it returned — that
5398
+ * is what lets the matcher resolve "use the mode for the WSP one" against
5399
+ * this one script's own context.
5400
+ */
5401
+ private buildActiveScriptBlock;
5402
+ }
5403
+
5404
+ /**
5405
+ * ScriptRunner — Execute scripts in an isolated tsx subprocess.
5406
+ *
5407
+ * The subprocess approach replaces the earlier `new Function()` eval and gives us:
5408
+ * - Real sandbox (separate process, SIGKILL on timeout).
5409
+ * - Real TypeScript (tsx transpiles on the fly).
5410
+ * - npm imports available to scripts (clustering, stats, geo, etc.).
5411
+ *
5412
+ * Protocol: NDJSON over the child's stdin/stdout. See script-ipc.ts + backend/docs/SCRIPT-FLOW-IMPLEMENTATION.md.
5413
+ */
5414
+
5415
+ interface RunScriptOptions {
5416
+ /** Data sources the script is allowed to query via ctx.query */
5417
+ externalTools: ExternalTool[];
5418
+ /** Optional — for propagating per-query UI progress to the user */
5419
+ streamBuffer?: StreamBuffer;
5420
+ /** Override the wall-clock timeout (default `SCRIPT_TIMEOUT_MS`, 60s). */
5421
+ timeoutMs?: number;
5422
+ /**
5423
+ * Per-turn cancellation signal. When the user hits "Stop" mid-run, the child
5424
+ * process group is SIGKILLed and the run resolves as an aborted failure (the
5425
+ * caller is already unwinding, so the result is discarded).
5426
+ */
5427
+ signal?: AbortSignal;
5428
+ /**
5429
+ * Names resolved for this run, handed to getAnalysis/getComponents as their
5430
+ * 5th argument. Supplying these is what stops "the narrative needs the
5431
+ * subsidiary's name" from being a reason to rewrite getData and re-query.
5432
+ */
5433
+ entities?: ResolvedEntityRef[];
5434
+ }
5435
+ /**
5436
+ * Execute a recipe by spawning a tsx child on the script's .ts file.
5437
+ * `scriptPath` is the absolute path to the saved `.ts` body.
5438
+ */
5439
+ declare function runScript(recipe: ScriptRecipe, scriptPath: string, params: Record<string, any>, options: RunScriptOptions): Promise<ScriptResult>;
5440
+
3227
5441
  type MessageTypeHandler = (message: IncomingMessage) => void | Promise<void>;
3228
5442
  declare class SuperatomSDK {
3229
5443
  private ws;
@@ -3259,6 +5473,8 @@ declare class SuperatomSDK {
3259
5473
  private lastPong;
3260
5474
  private readonly PING_INTERVAL_MS;
3261
5475
  private readonly PONG_TIMEOUT_MS;
5476
+ private pendingOutbox;
5477
+ private readonly MAX_OUTBOX_SIZE;
3262
5478
  constructor(config: SuperatomSDKConfig);
3263
5479
  /**
3264
5480
  * Initialize PromptLoader and load prompts into memory
@@ -3303,6 +5519,20 @@ declare class SuperatomSDK {
3303
5519
  * Does NOT throw on closed connections — callers can check the return value if needed.
3304
5520
  */
3305
5521
  send(message: Message): boolean;
5522
+ /**
5523
+ * Queue a message that couldn't be delivered because the socket was down,
5524
+ * to be resent once it reconnects. Drops the oldest entry once the bound is
5525
+ * hit — an outage long enough to fill this queue means the oldest queued
5526
+ * responses are for requests the caller has likely already given up on.
5527
+ */
5528
+ private queuePendingMessage;
5529
+ /**
5530
+ * Resend everything queued while the socket was down, now that it's back
5531
+ * up. Uses this.ws.send() directly (not send()) so a message that fails
5532
+ * again goes back through queuePendingMessage() rather than being silently
5533
+ * dropped a second time.
5534
+ */
5535
+ private flushPendingOutbox;
3306
5536
  /**
3307
5537
  * Register a message handler to receive all messages
3308
5538
  */
@@ -3342,6 +5572,14 @@ declare class SuperatomSDK {
3342
5572
  */
3343
5573
  private handlePong;
3344
5574
  private storeComponents;
5575
+ /**
5576
+ * The live, frontend-registered component catalog (name, type, description,
5577
+ * and full prop schema per component) — the same authoritative source
5578
+ * DASH_COMP_REQ's LLM prompt is built from. Exposed so other integrations
5579
+ * (e.g. the dashboard-agent script bridge) can read real component
5580
+ * contracts instead of maintaining a separate, driftable hand-written copy.
5581
+ */
5582
+ getComponents(): Component[];
3345
5583
  /**
3346
5584
  * Set tools for the SDK instance
3347
5585
  */
@@ -3350,6 +5588,16 @@ declare class SuperatomSDK {
3350
5588
  * Get the stored tools
3351
5589
  */
3352
5590
  getTools(): Tool$1[];
5591
+ /**
5592
+ * Call a registered collection operation in-process — no WebSocket
5593
+ * round-trip, since the caller is already running inside this same SDK
5594
+ * instance. Lets SDK-internal features (e.g. the dashboard agent) reuse
5595
+ * whatever collection a deployment has already registered (e.g.
5596
+ * 'dashboards'.'query') by name/convention, instead of requiring a
5597
+ * separate callback purely to re-expose data a collection already serves.
5598
+ * Throws if the collection or operation isn't registered.
5599
+ */
5600
+ callCollection<TResult = any>(collectionName: string, operation: string, params?: any): Promise<TResult>;
3353
5601
  /**
3354
5602
  * Register workflow components for the SDK instance.
3355
5603
  *
@@ -3393,4 +5641,4 @@ declare class SuperatomSDK {
3393
5641
  getConversationSimilarityThreshold(): number;
3394
5642
  }
3395
5643
 
3396
- export { type Action, type AgentConfig, type AgentResponse, BM25L, type BM25LOptions, type BaseLLMConfig, CONTEXT_CONFIG, type CapturedLog, CleanupService, type CollectionHandler, type CollectionOperation, type DBUIBlock, DEFAULT_AGENT_CONFIG, type DatabaseType, type HybridSearchOptions, type IncomingMessage, type KbNodesQueryFilters, type KbNodesRequestPayload, LLM, type LLMUsageEntry, type LogLevel, MainAgent, type Message, type ModelStrategy, type OutputField, type RerankedResult, STORAGE_CONFIG, type SelectedWorkflow, SuperatomSDK, type SuperatomSDKConfig, type TaskType, Thread, ThreadManager, type Tool$1 as Tool, type ToolOutputSchema, UIBlock, UILogCollector, type User, UserManager, type UsersData, type WorkflowDescriptor, anthropicLLM, dashboardConversationHistory, geminiLLM, groqLLM, hybridRerank, llmUsageLogger, logger, openaiLLM, queryCache, rerankChromaResults, rerankConversationResults, userPromptErrorLogger };
5644
+ export { type AgentConfig, type AgentEvent, type AgentResponse, type AgentRunContext, type AgentRunHandlers, type AgentRunRecord, type AgentRunResult, type AgentTrigger, type AgentUsage, type ApplySummary, BM25L, type BM25LOptions, type BaseLLMConfig, type BuildTriggerInput, CONTEXT_CONFIG, type CapturedLog, CleanupService, type CodexSessionOptions, type CodingAgentCollectionConfig, type CodingAgentDefinition, type CodingAgentSession, type CodingAgentSessionOptions, type CodingAgentsConfig, type CollectionHandler, type CollectionOperation, type DBUIBlock, DEFAULT_AGENT_CONFIG, type DashboardAgentCollectionConfig, type DatabaseType, type Harness, type HybridSearchOptions, type IncomingMessage, type KbNodesQueryFilters, type KbNodesRequestPayload, LLM, type LLMUsageEntry, type LogLevel, MainAgent, type McpServerSpec, type Message, type ModelStrategy, type OutputContract, type OutputField, type OutputValidator, type RerankedResult, type RunAgentOptions, STORAGE_CONFIG, type ScriptComponentSpec, ScriptMatcher, type ScriptParameter, type ScriptRecipe, type ScriptRecipeMetaRow, type ScriptRecipeStore, type ScriptResult, ScriptStore, type ScriptStoreOptions, type SelectedWorkflow, SuperatomSDK, type SuperatomSDKConfig, type TaskType, Thread, ThreadManager, type Tool$1 as Tool, type ToolOutputSchema, type TriggerEvent, type TriggerFireInfo, UIBlock, UILogCollector, type User, UserManager, type UsersData, type WorkflowDescriptor, type Workspace, anthropicLLM, assertReadOnlySql, createCodexSession, createCodingAgentSession, dashboardConversationHistory, dataFingerprint, describeTriggers, emitAgentEvent, extractNamedExport, geminiLLM, getCodingAgent, getCodingAgentsConfig, groqLLM, hybridRerank, listCodingAgents, llmUsageLogger, logger, normalizeScriptBody, onAgentRun, openaiLLM, queryCache, registerCodingAgentCollection, registerCodingAgents, registerDashboardAgentCollection, rerankChromaResults, rerankConversationResults, resolveScriptRecipeStore, runCodingAgent, runScript, startAgentTriggers, stopAgentTriggers, userPromptErrorLogger, writeBridgeShims };