@superatomai/sdk-node 0.0.4-s → 0.0.5-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.mts 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;
@@ -1055,6 +2320,59 @@ declare const KbNodesRequestPayloadSchema: z.ZodObject<{
1055
2320
  } | undefined;
1056
2321
  }>;
1057
2322
  type KbNodesRequestPayload = z.infer<typeof KbNodesRequestPayloadSchema>;
2323
+ declare const DashComponentSchema: z.ZodObject<{
2324
+ name: z.ZodString;
2325
+ kind: z.ZodString;
2326
+ purpose: z.ZodString;
2327
+ props: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodString, z.ZodObject<{
2328
+ description: z.ZodString;
2329
+ importance: z.ZodOptional<z.ZodEnum<["required", "recommended", "optional"]>>;
2330
+ default: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean]>>;
2331
+ caution: z.ZodOptional<z.ZodString>;
2332
+ group: z.ZodOptional<z.ZodString>;
2333
+ }, "strip", z.ZodTypeAny, {
2334
+ description: string;
2335
+ importance?: "required" | "recommended" | "optional" | undefined;
2336
+ default?: string | number | boolean | undefined;
2337
+ caution?: string | undefined;
2338
+ group?: string | undefined;
2339
+ }, {
2340
+ description: string;
2341
+ importance?: "required" | "recommended" | "optional" | undefined;
2342
+ default?: string | number | boolean | undefined;
2343
+ caution?: string | undefined;
2344
+ group?: string | undefined;
2345
+ }>]>>>;
2346
+ useWhen: z.ZodOptional<z.ZodString>;
2347
+ notes: z.ZodOptional<z.ZodString>;
2348
+ }, "strip", z.ZodTypeAny, {
2349
+ name: string;
2350
+ kind: string;
2351
+ purpose: string;
2352
+ props?: Record<string, string | {
2353
+ description: string;
2354
+ importance?: "required" | "recommended" | "optional" | undefined;
2355
+ default?: string | number | boolean | undefined;
2356
+ caution?: string | undefined;
2357
+ group?: string | undefined;
2358
+ }> | undefined;
2359
+ useWhen?: string | undefined;
2360
+ notes?: string | undefined;
2361
+ }, {
2362
+ name: string;
2363
+ kind: string;
2364
+ purpose: string;
2365
+ props?: Record<string, string | {
2366
+ description: string;
2367
+ importance?: "required" | "recommended" | "optional" | undefined;
2368
+ default?: string | number | boolean | undefined;
2369
+ caution?: string | undefined;
2370
+ group?: string | undefined;
2371
+ }> | undefined;
2372
+ useWhen?: string | undefined;
2373
+ notes?: string | undefined;
2374
+ }>;
2375
+ type DashComponent = z.infer<typeof DashComponentSchema>;
1058
2376
  interface T_RESPONSE {
1059
2377
  success: boolean;
1060
2378
  data?: any;
@@ -1105,666 +2423,364 @@ declare class UserManager {
1105
2423
  /**
1106
2424
  * Create a new user
1107
2425
  * @param user - User object to create
1108
- * @returns The created user
1109
- */
1110
- createUser(user: User): User;
1111
- /**
1112
- * Read a user by username
1113
- * @param username - Username to retrieve
1114
- * @returns The user if found, undefined otherwise
1115
- */
1116
- getUser(username: string): User | undefined;
1117
- /**
1118
- * Read a user by email
1119
- * @param email - Email to retrieve
1120
- * @returns The user if found, undefined otherwise
1121
- */
1122
- getUserByEmail(email: string): User | undefined;
1123
- /**
1124
- * Find user by username or email
1125
- * @param identifier - Username or email to search for
1126
- * @returns The user if found, undefined otherwise
1127
- */
1128
- getUserByUsernameOrEmail(identifier: string): User | undefined;
1129
- /**
1130
- * Read all users
1131
- * @returns Array of all users
1132
- */
1133
- getAllUsers(): User[];
1134
- /**
1135
- * Find users by a predicate function
1136
- * @param predicate - Function to filter users
1137
- * @returns Array of matching users
1138
- */
1139
- findUsers(predicate: (user: User) => boolean): User[];
1140
- /**
1141
- * Update an existing user by username
1142
- * @param username - Username of user to update
1143
- * @param updates - Partial user object with fields to update
1144
- * @returns The updated user
1145
- */
1146
- updateUser(username: string, updates: Partial<User>): User;
1147
- /**
1148
- * Delete a user by username
1149
- * @param username - Username of user to delete
1150
- * @returns true if user was deleted, false if not found
1151
- */
1152
- deleteUser(username: string): boolean;
1153
- /**
1154
- * Delete all users
1155
- */
1156
- deleteAllUsers(): void;
1157
- /**
1158
- * Get the count of users
1159
- * @returns Number of users in memory
1160
- */
1161
- getUserCount(): number;
1162
- /**
1163
- * Check if a user exists
1164
- * @param username - Username to check
1165
- * @returns true if user exists, false otherwise
1166
- */
1167
- userExists(username: string): boolean;
1168
- /**
1169
- * Add a WebSocket ID to a user's wsIds array
1170
- * @param username - Username to update
1171
- * @param wsId - WebSocket ID to add
1172
- * @returns true if successful, false if user not found
1173
- */
1174
- addWsId(username: string, wsId: string): boolean;
1175
- /**
1176
- * Remove a WebSocket ID from a user's wsIds array
1177
- * @param username - Username to update
1178
- * @param wsId - WebSocket ID to remove
1179
- * @returns true if successful, false if user not found
1180
- */
1181
- removeWsId(username: string, wsId: string): boolean;
1182
- /**
1183
- * Get the change status
1184
- * @returns true if there are unsaved changes, false otherwise
1185
- */
1186
- hasUnsavedChanges(): boolean;
1187
- /**
1188
- * Cleanup resources and stop sync interval
1189
- */
1190
- destroy(): Promise<void>;
1191
- }
1192
-
1193
- /**
1194
- * DashboardManager class to handle CRUD operations on dashboards
1195
- * All operations read/write directly to files (no in-memory caching)
1196
- */
1197
- declare class DashboardManager {
1198
- private dashboardsBasePath;
1199
- private projectId;
1200
- /**
1201
- * Initialize DashboardManager with project ID
1202
- * @param projectId - Project ID to use in file path
1203
- */
1204
- constructor(projectId?: string);
1205
- /**
1206
- * Get the file path for a specific dashboard
1207
- * @param dashboardId - Dashboard ID
1208
- * @returns Full path to dashboard data.json file
1209
- */
1210
- private getDashboardPath;
1211
- /**
1212
- * Create a new dashboard
1213
- * @param dashboardId - Unique dashboard ID
1214
- * @param dashboard - Dashboard data
1215
- * @returns Created dashboard with metadata
1216
- */
1217
- createDashboard(dashboardId: string, dashboard: DSLRendererProps$1): DSLRendererProps$1;
1218
- /**
1219
- * Get a specific dashboard by ID
1220
- * @param dashboardId - Dashboard ID
1221
- * @returns Dashboard data or null if not found
1222
- */
1223
- getDashboard(dashboardId: string): DSLRendererProps$1 | null;
1224
- /**
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
- }>;
2426
+ * @returns The created user
2427
+ */
2428
+ createUser(user: User): User;
1297
2429
  /**
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
2430
+ * Read a user by username
2431
+ * @param username - Username to retrieve
2432
+ * @returns The user if found, undefined otherwise
1302
2433
  */
1303
- updateReport(reportId: string, report: DSLRendererProps): DSLRendererProps | null;
2434
+ getUser(username: string): User | undefined;
1304
2435
  /**
1305
- * Delete a report
1306
- * @param reportId - Report ID
1307
- * @returns True if deleted, false if not found
2436
+ * Read a user by email
2437
+ * @param email - Email to retrieve
2438
+ * @returns The user if found, undefined otherwise
1308
2439
  */
1309
- deleteReport(reportId: string): boolean;
2440
+ getUserByEmail(email: string): User | undefined;
1310
2441
  /**
1311
- * Check if a report exists
1312
- * @param reportId - Report ID
1313
- * @returns True if report exists, false otherwise
2442
+ * Find user by username or email
2443
+ * @param identifier - Username or email to search for
2444
+ * @returns The user if found, undefined otherwise
1314
2445
  */
1315
- reportExists(reportId: string): boolean;
2446
+ getUserByUsernameOrEmail(identifier: string): User | undefined;
1316
2447
  /**
1317
- * Get report count
1318
- * @returns Number of reports
2448
+ * Read all users
2449
+ * @returns Array of all users
1319
2450
  */
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);
2451
+ getAllUsers(): User[];
1338
2452
  /**
1339
- * Check if the buffer has a callback configured
2453
+ * Find users by a predicate function
2454
+ * @param predicate - Function to filter users
2455
+ * @returns Array of matching users
1340
2456
  */
1341
- hasCallback(): boolean;
2457
+ findUsers(predicate: (user: User) => boolean): User[];
1342
2458
  /**
1343
- * Get all text that has been written (including already flushed)
2459
+ * Update an existing user by username
2460
+ * @param username - Username of user to update
2461
+ * @param updates - Partial user object with fields to update
2462
+ * @returns The updated user
1344
2463
  */
1345
- getFullText(): string;
2464
+ updateUser(username: string, updates: Partial<User>): User;
1346
2465
  /**
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
2466
+ * Delete a user by username
2467
+ * @param username - Username of user to delete
2468
+ * @returns true if user was deleted, false if not found
1352
2469
  */
1353
- write(chunk: string): void;
2470
+ deleteUser(username: string): boolean;
1354
2471
  /**
1355
- * Flush the buffer immediately
1356
- * Call this before tool execution or other operations that need clean output
2472
+ * Delete all users
1357
2473
  */
1358
- flush(): void;
2474
+ deleteAllUsers(): void;
1359
2475
  /**
1360
- * Internal flush implementation
2476
+ * Get the count of users
2477
+ * @returns Number of users in memory
1361
2478
  */
1362
- private flushNow;
2479
+ getUserCount(): number;
1363
2480
  /**
1364
- * Clean up resources
1365
- * Call this when done with the buffer
2481
+ * Check if a user exists
2482
+ * @param username - Username to check
2483
+ * @returns true if user exists, false otherwise
1366
2484
  */
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;
1413
- }
1414
-
1415
- /**
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.
1500
- */
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;
2485
+ userExists(username: string): boolean;
1508
2486
  /**
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."
2487
+ * Add a WebSocket ID to a user's wsIds array
2488
+ * @param username - Username to update
2489
+ * @param wsId - WebSocket ID to add
2490
+ * @returns true if successful, false if user not found
1513
2491
  */
1514
- whenToUse: string;
2492
+ addWsId(username: string, wsId: string): boolean;
1515
2493
  /**
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
- * ```
2494
+ * Remove a WebSocket ID from a user's wsIds array
2495
+ * @param username - Username to update
2496
+ * @param wsId - WebSocket ID to remove
2497
+ * @returns true if successful, false if user not found
1528
2498
  */
1529
- propsSchema: Record<string, string>;
2499
+ removeWsId(username: string, wsId: string): boolean;
1530
2500
  /**
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.
2501
+ * Get the change status
2502
+ * @returns true if there are unsaved changes, false otherwise
1534
2503
  */
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>;
2504
+ hasUnsavedChanges(): boolean;
2505
+ /**
2506
+ * Cleanup resources and stop sync interval
2507
+ */
2508
+ destroy(): Promise<void>;
1546
2509
  }
2510
+
1547
2511
  /**
1548
- * The complete response from the multi-agent system.
1549
- * Contains everything needed for text display + component generation.
2512
+ * DashboardManager class to handle CRUD operations on dashboards
2513
+ * All operations read/write directly to files (no in-memory caching)
1550
2514
  */
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[];
2515
+ declare class DashboardManager {
2516
+ private dashboardsBasePath;
2517
+ private projectId;
1558
2518
  /**
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).
2519
+ * Initialize DashboardManager with project ID
2520
+ * @param projectId - Project ID to use in file path
1562
2521
  */
1563
- savedScript?: AgentWrittenScript;
2522
+ constructor(projectId?: string);
1564
2523
  /**
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.
2524
+ * Get the file path for a specific dashboard
2525
+ * @param dashboardId - Dashboard ID
2526
+ * @returns Full path to dashboard data.json file
1568
2527
  */
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 {
2528
+ private getDashboardPath;
1576
2529
  /**
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.
2530
+ * Create a new dashboard
2531
+ * @param dashboardId - Unique dashboard ID
2532
+ * @param dashboard - Dashboard data
2533
+ * @returns Created dashboard with metadata
1581
2534
  */
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;
2535
+ createDashboard(dashboardId: string, dashboard: DSLRendererProps$1): DSLRendererProps$1;
2536
+ /**
2537
+ * Get a specific dashboard by ID
2538
+ * @param dashboardId - Dashboard ID
2539
+ * @returns Dashboard data or null if not found
2540
+ */
2541
+ getDashboard(dashboardId: string): DSLRendererProps$1 | null;
2542
+ /**
2543
+ * Get all dashboards
2544
+ * @returns Array of dashboard objects with their IDs
2545
+ */
2546
+ getAllDashboards(): Array<{
2547
+ dashboardId: string;
2548
+ dashboard: DSLRendererProps$1;
1615
2549
  }>;
2550
+ /**
2551
+ * Update an existing dashboard
2552
+ * @param dashboardId - Dashboard ID
2553
+ * @param dashboard - Updated dashboard data
2554
+ * @returns Updated dashboard or null if not found
2555
+ */
2556
+ updateDashboard(dashboardId: string, dashboard: DSLRendererProps$1): DSLRendererProps$1 | null;
2557
+ /**
2558
+ * Delete a dashboard
2559
+ * @param dashboardId - Dashboard ID
2560
+ * @returns True if deleted, false if not found
2561
+ */
2562
+ deleteDashboard(dashboardId: string): boolean;
2563
+ /**
2564
+ * Check if a dashboard exists
2565
+ * @param dashboardId - Dashboard ID
2566
+ * @returns True if dashboard exists, false otherwise
2567
+ */
2568
+ dashboardExists(dashboardId: string): boolean;
2569
+ /**
2570
+ * Get dashboard count
2571
+ * @returns Number of dashboards
2572
+ */
2573
+ getDashboardCount(): number;
1616
2574
  }
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;
1642
- }
1643
- /**
1644
- * Default agent configuration
1645
- */
1646
- declare const DEFAULT_AGENT_CONFIG: AgentConfig;
1647
2575
 
1648
2576
  /**
1649
- * Script Flow Types
1650
- *
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.
2577
+ * ReportManager class to handle CRUD operations on reports
2578
+ * All operations read/write directly to files (no in-memory caching)
1659
2579
  */
1660
- interface ScriptRecipe {
1661
- /** Unique script identifier */
1662
- id: string;
1663
- /** Version number (incremented on regeneration) */
1664
- version: number;
1665
- /** Human-readable name (e.g., "Revenue by Dimension") */
1666
- name: string;
1667
- /** Natural language description of what this script does */
1668
- 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 */
1680
- successCount: number;
1681
- /** Times this script failed */
1682
- 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;
2580
+ declare class ReportManager {
2581
+ private reportsBasePath;
2582
+ private projectId;
1691
2583
  /**
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.
2584
+ * Initialize ReportManager with project ID
2585
+ * @param projectId - Project ID to use in file path
1695
2586
  */
1696
- parentId?: string;
1697
- /** 0 for root scripts; `parent.forkDepth + 1` for forks. Capped at 3. */
1698
- forkDepth?: number;
2587
+ constructor(projectId?: string);
1699
2588
  /**
1700
- * Brief description of what this fork changed vs its parent
1701
- * (sourced from the matcher's `modificationHint`).
2589
+ * Get the file path for a specific report
2590
+ * @param reportId - Report ID
2591
+ * @returns Full path to report data.json file
1702
2592
  */
1703
- forkReason?: string;
2593
+ private getReportPath;
2594
+ /**
2595
+ * Create a new report
2596
+ * @param reportId - Unique report ID
2597
+ * @param report - Report data
2598
+ * @returns Created report with metadata
2599
+ */
2600
+ createReport(reportId: string, report: DSLRendererProps): DSLRendererProps;
2601
+ /**
2602
+ * Get a specific report by ID
2603
+ * @param reportId - Report ID
2604
+ * @returns Report data or null if not found
2605
+ */
2606
+ getReport(reportId: string): DSLRendererProps | null;
2607
+ /**
2608
+ * Get all reports
2609
+ * @returns Array of report objects with their IDs
2610
+ */
2611
+ getAllReports(): Array<{
2612
+ reportId: string;
2613
+ report: DSLRendererProps;
2614
+ }>;
2615
+ /**
2616
+ * Update an existing report
2617
+ * @param reportId - Report ID
2618
+ * @param report - Updated report data
2619
+ * @returns Updated report or null if not found
2620
+ */
2621
+ updateReport(reportId: string, report: DSLRendererProps): DSLRendererProps | null;
1704
2622
  /**
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.
2623
+ * Delete a report
2624
+ * @param reportId - Report ID
2625
+ * @returns True if deleted, false if not found
1716
2626
  */
1717
- status?: 'draft' | 'verified';
2627
+ deleteReport(reportId: string): boolean;
1718
2628
  /**
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.
2629
+ * Check if a report exists
2630
+ * @param reportId - Report ID
2631
+ * @returns True if report exists, false otherwise
1721
2632
  */
1722
- turnId?: string;
2633
+ reportExists(reportId: string): boolean;
1723
2634
  /**
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'.
2635
+ * Get report count
2636
+ * @returns Number of reports
1727
2637
  */
2638
+ getReportCount(): number;
2639
+ }
2640
+
2641
+ /**
2642
+ * ScriptRecipeStore — injected metadata backend for the script flow.
2643
+ *
2644
+ * The SDK is standalone (no DB dependency). The backend implements this
2645
+ * interface over Postgres (full-text search + atomic counters) and injects it
2646
+ * via `collections['script-recipes']`, exactly like `collections['source-embeddings']`.
2647
+ * `ScriptStore` consumes it for all METADATA operations while keeping the
2648
+ * executable body on disk as scripts-store/<fileBase>.ts.
2649
+ *
2650
+ * All metadata rows are plain JSON (no scriptBody — that lives on disk).
2651
+ * See backend/docs/SCRIPT-FLOW-SCALING-ISSUES.md (#1, #3, #7).
2652
+ */
2653
+
2654
+ /** One recipe's metadata as stored in Postgres (mirrors the script_recipes table). */
2655
+ interface ScriptRecipeMetaRow {
2656
+ id: string;
2657
+ projectId?: string | null;
2658
+ version: number;
2659
+ name: string;
2660
+ intentDescription: string;
2661
+ tags: string[] | null;
2662
+ createdFrom: string | null;
2663
+ sourceIds: string[] | null;
2664
+ tables: string[] | null;
2665
+ parameters: ScriptParameter[] | null;
2666
+ components?: ScriptComponentSpec[] | null;
2667
+ displayPreference?: ScriptDisplayPreference | null;
2668
+ fileBase: string;
2669
+ bodyHash?: string | null;
2670
+ successCount: number;
2671
+ failureCount: number;
2672
+ lastUsed: string | null;
2673
+ parentId?: string | null;
2674
+ forkDepth?: number | null;
2675
+ forkReason?: string | null;
2676
+ status: 'draft' | 'verified' | string;
2677
+ turnId?: string | null;
2678
+ editedBy?: string | null;
2679
+ editedFrom?: string | null;
2680
+ history?: ScriptVersionRecord[] | null;
1728
2681
  lastError?: {
1729
2682
  phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
1730
2683
  message: string;
1731
2684
  at: string;
1732
2685
  attempt: number;
1733
- };
2686
+ } | null;
2687
+ createdAt?: string | null;
2688
+ updatedAt?: string | null;
1734
2689
  }
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;
2690
+ interface ScriptRecipeStore {
2691
+ /** FTS shortlist of healthy verified recipes for the matcher (metadata only). */
2692
+ search(params: {
2693
+ prompt: string;
2694
+ projectId?: string;
2695
+ limit?: number;
2696
+ }): Promise<ScriptRecipeMetaRow[]>;
2697
+ /** Fetch one recipe by id (any status). */
2698
+ getById(id: string): Promise<ScriptRecipeMetaRow | null>;
2699
+ /** Count healthy verified recipes (drives the "any scripts?" gate). */
2700
+ count(params?: {
2701
+ projectId?: string;
2702
+ }): Promise<number>;
2703
+ /** Insert or update a recipe row (keyed by id). */
2704
+ upsert(row: ScriptRecipeMetaRow): Promise<void>;
2705
+ /** Atomically bump counters / last-used. */
2706
+ updateStats(id: string, patch: {
2707
+ successDelta?: number;
2708
+ failureDelta?: number;
2709
+ lastUsed?: string;
2710
+ }): Promise<void>;
2711
+ /** Flip a draft to verified, applying provenance + optional fork lineage. */
2712
+ promote(id: string, patch: {
2713
+ sourceIds: string[];
2714
+ tables: string[];
2715
+ fileBase?: string;
2716
+ parentId?: string;
2717
+ forkDepth?: number;
2718
+ forkReason?: string;
2719
+ components?: ScriptComponentSpec[];
2720
+ }): Promise<ScriptRecipeMetaRow | null>;
2721
+ /**
2722
+ * Commit a verified edit onto an EXISTING recipe: bump `version`, replace the
2723
+ * body-bearing metadata, append a history record, reset health counters, and
2724
+ * clear the component specs (they were validated against the old shape).
2725
+ * Returns the updated row, or null when the target is gone.
2726
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 5.
2727
+ */
2728
+ commitEdit?(id: string, patch: {
2729
+ name?: string;
2730
+ intentDescription?: string;
2731
+ tags?: string[];
2732
+ parameters?: ScriptParameter[];
2733
+ bodyHash: string;
2734
+ /** New on-disk stem when the edit renamed the script; omitted otherwise. */
2735
+ fileBase?: string;
2736
+ sourceIds?: string[];
2737
+ tables?: string[];
2738
+ editedBy?: string;
2739
+ editedFrom?: string;
2740
+ historyEntry: {
2741
+ version: number;
2742
+ at: string;
2743
+ by?: string;
2744
+ instruction?: string;
2745
+ changeSummary?: string;
2746
+ bodyHash?: string;
2747
+ };
2748
+ }): Promise<ScriptRecipeMetaRow | null>;
2749
+ /** Stamp a draft's last execution error. */
2750
+ recordDraftError(id: string, err: {
2751
+ phase: string;
2752
+ message: string;
2753
+ attempt: number;
2754
+ at: string;
2755
+ }): Promise<void>;
2756
+ /** Delete a recipe row (body file removed separately). */
2757
+ remove(id: string): Promise<void>;
2758
+ /** True if `fileBase` is taken by a different recipe in this project. */
2759
+ fileBaseTaken(fileBase: string, excludeId: string, projectId?: string): Promise<boolean>;
1748
2760
  }
2761
+ /** Pull the injected store off the collections bag (or null if not wired). */
2762
+ declare function resolveScriptRecipeStore(collections: any): ScriptRecipeStore | null;
1749
2763
 
1750
2764
  /**
1751
- * ScriptStore — Persistent Storage for Script Recipes
2765
+ * ScriptStore — Postgres metadata + on-disk body for script recipes.
1752
2766
  *
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
2767
+ * Split of responsibilities:
2768
+ * - METADATA → injected `ScriptRecipeStore` (Postgres FTS + atomic counters),
2769
+ * resolved from `collections['script-recipes']`.
2770
+ * - BODY → scripts-store/<fileBase>.ts, editable in your IDE. Written
2771
+ * atomically (temp + rename); `bodyHash` (sha256) detects edits.
1757
2772
  *
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.
2773
+ * The old "read every file every turn + send the whole catalog to the LLM"
2774
+ * matcher is gone — matching is `store.search(prompt)` (FTS shortlist). The
2775
+ * draft/verified filename dance is gone too: `status` is a DB column and the
2776
+ * file keeps a stable `<fileBase>.ts` name across promotion.
2777
+ *
2778
+ * When no metadata store is injected, the store degrades to a safe no-op
2779
+ * (count 0 → script flow disabled) instead of crashing.
2780
+ *
2781
+ * See backend/docs/SCRIPT-FLOW-SCALING-ISSUES.md.
1761
2782
  */
1762
2783
 
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
2784
  interface SaveDraftInput {
1769
2785
  /** Reuse an existing draft (retry); omit to mint a new one. */
1770
2786
  recipeId?: string;
@@ -1776,141 +2792,197 @@ interface SaveDraftInput {
1776
2792
  parameters: ScriptParameter[];
1777
2793
  scriptBody: string;
1778
2794
  createdFrom: string;
2795
+ /**
2796
+ * Set when this draft is a SHADOW of an existing recipe being edited. The
2797
+ * draft is verified independently and merged back onto the parent via
2798
+ * `commitEdit`, so the working script is never clobbered by an edit that
2799
+ * turns out not to run. See backend/docs/SCRIPT-EDIT-DESIGN.md § D5.
2800
+ */
2801
+ parentId?: string;
1779
2802
  }
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
2803
  interface PromoteToVerifiedInput {
1786
2804
  sourceIds: string[];
1787
2805
  tables: string[];
1788
2806
  parentId?: string;
1789
2807
  forkDepth?: number;
1790
2808
  forkReason?: string;
2809
+ components?: ScriptComponentSpec[];
2810
+ }
2811
+ interface ScriptStoreOptions {
2812
+ /** Explicit metadata store, or resolved from `collections['script-recipes']`. */
2813
+ store?: ScriptRecipeStore | null;
2814
+ collections?: any;
2815
+ /** Body directory (defaults to <cwd>/scripts-store). */
2816
+ baseDir?: string;
2817
+ /** Project scope stamped on every row. */
2818
+ projectId?: string;
1791
2819
  }
2820
+ /**
2821
+ * Fingerprint of everything that decides what `getData` PRODUCES.
2822
+ *
2823
+ * A recipe holds three programs in one file — getData, getAnalysis,
2824
+ * getComponents — but only getData costs a source query, and only getData's
2825
+ * output can invalidate a verified run. Editing the narrative or a chart title
2826
+ * cannot change a single row, yet a whole-body comparison says it did, because
2827
+ * `write_script` resubmits the entire file. That is what made a cosmetic edit
2828
+ * indistinguishable from a SQL change and cost a scarce execute attempt.
2829
+ *
2830
+ * Parameters are part of the fingerprint, not an afterthought: `execute_script`
2831
+ * runs with `{}`, so the DEFAULTS are the values getData actually sees. Same
2832
+ * getData text with a changed default is a genuinely different result, and
2833
+ * hashing the body alone would have called it unchanged.
2834
+ *
2835
+ * Deliberately conservative — trimmed, not whitespace-collapsed. The two ways
2836
+ * to be wrong are NOT symmetric: a false "unchanged" reuses a stale result and
2837
+ * reports wrong numbers, while a false "changed" merely costs an attempt. So it
2838
+ * biases toward detecting change, and a reformat is allowed to look like an edit.
2839
+ */
2840
+ declare function dataFingerprint(scriptBody: string, parameters?: ScriptParameter[]): string;
2841
+ /** Inverse of `stripNamedExport` — pulls the named export block OUT instead of removing it. Null when absent. */
2842
+ declare function extractNamedExport(body: string, fnName: string): string | null;
2843
+ declare function normalizeScriptBody(scriptBody: string): string;
1792
2844
  declare class ScriptStore {
1793
- private recipes;
2845
+ private store;
1794
2846
  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.
2847
+ private projectId?;
2848
+ constructor(opts?: ScriptStoreOptions);
2849
+ /** Whether a metadata store is wired (matcher / authoring are gated on this). */
2850
+ hasStore(): boolean;
2851
+ /** Number of healthy verified recipes (gates the script-matching path). */
2852
+ count(): Promise<number>;
2853
+ /**
2854
+ * FTS shortlist for the matcher (metadata only — bodies are loaded lazily by
2855
+ * `get()` once the LLM picks one). Returns verified, healthy recipes ranked
2856
+ * by relevance.
2857
+ */
2858
+ search(prompt: string, limit?: number): Promise<ScriptRecipe[]>;
2859
+ /** Fetch one recipe by id with its body loaded from disk. */
2860
+ get(id: string): Promise<ScriptRecipe | null>;
2861
+ /**
2862
+ * Fetch one recipe by id, pinned to a SPECIFIC past version rather than
2863
+ * whatever is live today. Needed to render a past turn's conversation
2864
+ * history accurately after the script has since been edited — get() alone
2865
+ * always returns the CURRENT body, which may no longer be what that turn
2866
+ * actually ran.
1838
2867
  *
1839
- * Filename includes the `turnId` suffix so concurrent turns never collide.
2868
+ * Returns null (never a silently-wrong version) when the requested version
2869
+ * isn't the current one and its archived body can't be found — e.g. the
2870
+ * archive was cleaned up after the whole recipe was later deleted.
1840
2871
  */
1841
- saveDraft(input: SaveDraftInput): ScriptRecipe;
2872
+ getVersion(id: string, version: number): Promise<ScriptRecipe | null>;
2873
+ /** Create or update a recipe (metadata upsert + body write when changed). */
2874
+ save(recipe: ScriptRecipe): Promise<void>;
1842
2875
  /**
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.
2876
+ * Persist (or update) a draft. Within a turn, retries that pass the same
2877
+ * `recipeId` overwrite the same row + file; a fresh `recipeId` mints a new
2878
+ * draft. The body is visible at scripts-store/<fileBase>.ts immediately.
1846
2879
  */
2880
+ saveDraft(input: SaveDraftInput): Promise<ScriptRecipe>;
2881
+ /** Stamp a draft's last execution error (metadata only). */
1847
2882
  recordDraftError(recipeId: string, err: {
1848
2883
  phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
1849
2884
  message: string;
1850
2885
  attempt: number;
1851
- }): void;
2886
+ }): Promise<void>;
1852
2887
  /**
1853
2888
  * 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.
2889
+ * The on-disk body already exists at <fileBase>.ts (written at write_script
2890
+ * time) and keeps its name — only the DB row flips status + provenance.
1868
2891
  */
1869
- discardDraft(recipeId: string): void;
2892
+ promoteToVerified(recipeId: string, input: PromoteToVerifiedInput): Promise<ScriptRecipe | null>;
1870
2893
  /**
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.
1885
- */
1886
- getFailureRate(id: string): number;
1887
- private ensureLoaded;
1888
- /**
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.
2894
+ * Commit a user-directed edit: merge a VERIFIED shadow draft back onto the
2895
+ * recipe it was editing, as version N+1 under the SAME recipe id.
1895
2896
  *
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)
2897
+ * Keeping the id stable is the point of the whole feature — every cached
2898
+ * conversation, `script_dataset` regeneration descriptor and persisted
2899
+ * component spec already points at it, so the correction applies retroactively
2900
+ * to replays instead of stranding them on the old body.
2901
+ *
2902
+ * Steps (see backend/docs/SCRIPT-EDIT-DESIGN.md § Component 5):
2903
+ * 1. archive the current body as `<fileBase>.v<version>.ts`
2904
+ * 2. write the edited body to the original fileBase (atomic)
2905
+ * 3. version++, copy name/description/params, append a history record
2906
+ * 4. reset health counters — the pre-edit failure history is stale
2907
+ * 5. clear component specs — they were validated against the OLD shape
2908
+ * 6. delete the shadow draft (row + file)
2909
+ *
2910
+ * Returns the updated recipe, or null when the commit could not be applied
2911
+ * (caller should then fall back to treating the draft as a new script).
2912
+ */
2913
+ commitEdit(targetId: string, draft: {
2914
+ recipeId: string;
2915
+ name?: string;
2916
+ intentDescription?: string;
2917
+ tags?: string[];
2918
+ parameters?: ScriptParameter[];
2919
+ scriptBody: string;
2920
+ sourceIds?: string[];
2921
+ tables?: string[];
2922
+ }, meta: {
2923
+ instruction?: string;
2924
+ changeSummary?: string;
2925
+ editedBy?: string;
2926
+ }): Promise<ScriptRecipe | null>;
2927
+ /**
2928
+ * Drop a draft (row + body file). MainAgent calls this at end-of-turn when a
2929
+ * draft was authored but never verified — failed drafts are never matched, so
2930
+ * deleting them immediately avoids unbounded accumulation (#5). No-op if the
2931
+ * recipe isn't a draft (so a promoted/verified script is never removed here).
2932
+ */
2933
+ discardDraft(recipeId: string): Promise<void>;
2934
+ /** Delete a recipe (row + body file). */
2935
+ delete(id: string): Promise<void>;
2936
+ /** Record a successful execution (atomic counter bump). */
2937
+ recordSuccess(id: string): Promise<void>;
2938
+ /** Record a failed execution (atomic counter bump). */
2939
+ recordFailure(id: string): Promise<void>;
2940
+ /** Absolute path to the .ts body for a recipe (used by the runner/MainAgent). */
2941
+ getScriptPath(recipe: ScriptRecipe): string;
2942
+ private removeById;
2943
+ private rowToRecipe;
2944
+ private recipeToRow;
2945
+ /** slug of name, with a short id suffix when the bare slug is already taken. */
2946
+ private computeFileBase;
2947
+ private toSlug;
2948
+ private hash;
2949
+ /**
2950
+ * Attach getComponents to the recipe body. The stored `components` column is
2951
+ * cleared at promotion (see promoteToVerified): once the body computes its own
2952
+ * specs, a stale spec array beside a live program is how the wrong one gets
2953
+ * read later. See backend/docs/COMPONENTS-AS-PROGRAM-DESIGN.md § 4.4.
2954
+ */
2955
+ attachComponents(recipeId: string, componentsBody: string): Promise<boolean>;
2956
+ /**
2957
+ * Append (or replace) the `getAnalysis` export on an existing draft body.
2958
+ *
2959
+ * The narrative is authored AFTER execution, so it lands on a file that
2960
+ * already holds a working getData — rewriting the whole body would risk
2961
+ * losing the query that just succeeded. Replaces any earlier getAnalysis so
2962
+ * a retry does not stack duplicate exports.
2963
+ */
2964
+ attachAnalysis(recipeId: string, analysisBody: string): Promise<boolean>;
2965
+ private bodyPath;
2966
+ private readBody;
2967
+ /** Directory holding superseded bodies. Dot-prefixed so IDEs/`ls` hide it. */
2968
+ private get archiveDir();
2969
+ /**
2970
+ * Archive a superseded body as `.versions/<fileBase>.v<n>.ts`.
1905
2971
  *
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.
2972
+ * Kept out of the main store directory on purpose — see commitEdit step 1.
2973
+ * To roll back: copy the file back over `scripts-store/<fileBase>.ts`.
1908
2974
  */
1909
- private saveRecipeToDisk;
2975
+ private writeArchive;
1910
2976
  /**
1911
- * Delete a recipe's files from disk (both metadata + body, plus any legacy file).
2977
+ * Move a recipe's archived versions to a new prefix when its fileBase changes,
2978
+ * so all versions of one recipe stay grouped. Without this, two renames would
2979
+ * scatter a single recipe's history across three prefixes in `.versions/` with
2980
+ * nothing linking them back to the live script.
1912
2981
  */
1913
- private deleteRecipeFromDisk;
2982
+ private renameArchives;
2983
+ /** Atomic body write (temp + rename) so concurrent reads never see a partial file. */
2984
+ private writeBody;
2985
+ private unlinkBody;
1914
2986
  }
1915
2987
 
1916
2988
  /**
@@ -1944,7 +3016,75 @@ declare class MainAgent {
1944
3016
  private turnId;
1945
3017
  private createdFromPrompt;
1946
3018
  private scriptState;
1947
- constructor(externalTools: ExternalTool[], config: AgentConfig, scriptStore?: ScriptStore, turnId?: string, streamBuffer?: StreamBuffer, workflows?: WorkflowDescriptor[]);
3019
+ /** Answer-flow component catalog (filtered + projected by the caller). */
3020
+ private componentCatalog;
3021
+ /** Specs accepted by `write_components` this turn; empty until it succeeds. */
3022
+ /**
3023
+ * Entities resolved this turn, appended verbatim to every source dispatch.
3024
+ *
3025
+ * Not left to the model to copy into its intent prose: observed live, the
3026
+ * MainAgent wrote names instead of ids on the first dispatch AND re-spelled
3027
+ * 'F D C LIMITED' as 'FDC Limited', so the source agent fell back to
3028
+ * `PartyName LIKE '%FDC%'` and found 2 of 3 customers. Carrying the ids
3029
+ * structurally makes that impossible rather than instruction-dependent.
3030
+ */
3031
+ private resolvedEntities;
3032
+ /**
3033
+ * Concept bodies resolved this turn (currently: invariants only, via
3034
+ * loadConceptInvariants — concept resolution is no longer part of
3035
+ * resolve_entities or any other MainAgent-owned tool) — retained the same
3036
+ * way as resolvedEntities above, so they can be attached to the SourceAgent
3037
+ * dispatch.
3038
+ *
3039
+ * Without this, a concept resolved HERE (MainAgent's own tool loop) never
3040
+ * reaches SourceAgent at all: SourceAgent only gets the natural-language
3041
+ * `intent` MainAgent writes, which is a paraphrase, not the concept's
3042
+ * verbatim, authoritative body. Deduped on name so a mid-turn re-resolve
3043
+ * doesn't repeat an already-attached concept.
3044
+ */
3045
+ private resolvedConcepts;
3046
+ private componentSpecs;
3047
+ private componentLayout;
3048
+ private renderComponentAttempts;
3049
+ /** Same repeated-violation tracking as ScriptState.lastAnalysisViolationTexts, for write_components. */
3050
+ private lastComponentsViolationTexts;
3051
+ /**
3052
+ * Fork mode — set when this turn is adapting a near-matching parent script.
3053
+ * In fork mode there is no legitimate "answer with bare text" outcome: the
3054
+ * only correct first move is a tool call (write_script, or a source tool for
3055
+ * schema discovery). We therefore force tool use on the first LLM iteration
3056
+ * so the model can't end its turn with a bare "I'll adapt…" preamble and zero
3057
+ * tool calls. Never set on the fresh-authoring / general-question path.
3058
+ */
3059
+ private forkMode;
3060
+ /**
3061
+ * Edit mode — set when this turn applies a user-directed change to an
3062
+ * existing script. Swaps the system prompt to `agent-main-edit` and stamps
3063
+ * the shadow draft's parentId. Like fork mode there is no legitimate
3064
+ * "answer with bare text" outcome, so tool use is forced on iteration 1.
3065
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 4.
3066
+ */
3067
+ private editContext;
3068
+ /** The recipe's durable rendering choice — see the constructor. */
3069
+ private displayPreference;
3070
+ /**
3071
+ * Per-turn cancellation signal (user hit "Stop"). Set at the top of
3072
+ * handleQuestion and read by the tool handler, the SourceAgent dispatch, and
3073
+ * the script subprocess so an abort tears down every layer of the turn.
3074
+ */
3075
+ private abortSignal?;
3076
+ constructor(externalTools: ExternalTool[], config: AgentConfig, scriptStore?: ScriptStore, turnId?: string, streamBuffer?: StreamBuffer, workflows?: WorkflowDescriptor[], forkMode?: boolean, editContext?: EditContext, componentCatalog?: Component[],
3077
+ /**
3078
+ * The recipe's durable rendering choice, when it has one. A DATA edit
3079
+ * re-authors getComponents from scratch, so without this the user's chart
3080
+ * type is silently dropped whenever anyone changes the SQL.
3081
+ */
3082
+ displayPreference?: {
3083
+ componentTypes: string[];
3084
+ instruction: string;
3085
+ });
3086
+ /** True when the turn is applying a user-directed script edit. */
3087
+ private get editMode();
1948
3088
  private get scriptingEnabled();
1949
3089
  /**
1950
3090
  * Handle a user question using the multi-agent system.
@@ -1956,7 +3096,34 @@ declare class MainAgent {
1956
3096
  * 4. Direct tools → fn() called directly with LLM params → returns data
1957
3097
  * 5. Generates final analysis text
1958
3098
  */
1959
- handleQuestion(userPrompt: string, apiKey?: string, conversationHistory?: string, streamCallback?: (chunk: string) => void): Promise<AgentResponse>;
3099
+ handleQuestion(userPrompt: string, apiKey?: string, conversationHistory?: string, streamCallback?: (chunk: string) => void, signal?: AbortSignal): Promise<AgentResponse>;
3100
+ /**
3101
+ * Tool definition, with the catalog rendered from the registered `Metadata`
3102
+ * the caller passed in. Generated rather than hand-written so it cannot drift
3103
+ * from what the frontend actually ships.
3104
+ */
3105
+ private buildWriteComponentsToolDef;
3106
+ /**
3107
+ * Validate the agent's component picks against the REAL columns of each bound
3108
+ * dataset. All-or-nothing: any error rejects the whole call and returns the
3109
+ * findings, because a partially-accepted dashboard makes the retry ambiguous.
3110
+ */
3111
+ private validateSpecs;
3112
+ /**
3113
+ * Author the dashboard as a program. Attaches getComponents to the draft, runs
3114
+ * it over the verified rows, then validates the specs it RETURNED — the program
3115
+ * is never trusted because it is a program, exactly as an authored spec is not.
3116
+ */
3117
+ private handleWriteComponents;
3118
+ /**
3119
+ * Author the answer as a program. Runs after execute_script, so the body is
3120
+ * written with the result already known and can react to what it found.
3121
+ *
3122
+ * Rejects a body that types numbers rather than computing them — see
3123
+ * analysis-lint. A hardcoded figure is worse than prose: it freezes into the
3124
+ * recipe and replays unchanged while the data moves underneath it.
3125
+ */
3126
+ private handleWriteAnalysis;
1960
3127
  private handleWriteScript;
1961
3128
  private handleExecuteScript;
1962
3129
  /**
@@ -1966,6 +3133,17 @@ declare class MainAgent {
1966
3133
  */
1967
3134
  private buildSavedScript;
1968
3135
  private normalizeParameterList;
3136
+ /**
3137
+ * Which declared parameter NAMES never actually appear wired into the
3138
+ * script body — checked as plain text, not a real parser, so it only
3139
+ * catches the worst case (completely unreferenced) rather than every
3140
+ * subtler variant (e.g. used for display text while the SQL itself still
3141
+ * hardcodes the value). That worst case is exactly what's been observed:
3142
+ * a parameter declared purely for show, with the actual filter hardcoded.
3143
+ * Accepts `params.name`, `params['name']`/`params["name"]`, and a
3144
+ * destructuring binding (`const { name } = params`) as valid wiring.
3145
+ */
3146
+ private findUnwiredParams;
1969
3147
  /**
1970
3148
  * Use the schema embedding collection to pre-select relevant tables for
1971
3149
  * this source + intent. Returns a formatted schema block if confidence is
@@ -1975,6 +3153,134 @@ declare class MainAgent {
1975
3153
  * loop and reduce iteration budget. When it returns null, the SourceAgent
1976
3154
  * falls back to the existing LLM-driven keyword search (same as today).
1977
3155
  */
3156
+ /**
3157
+ * Ids + verbatim names for everything resolved this turn, appended by CODE to
3158
+ * each source dispatch so neither omission nor re-spelling by the model can
3159
+ * lose them. Empty string when nothing was resolved, so intents are untouched
3160
+ * for questions that name no entities.
3161
+ *
3162
+ * Also appends `attrs.scopeSql` when the resolved instance carries one —
3163
+ * same "guaranteed by code" reasoning as the ids/names above, extended to
3164
+ * the one attrs field with a documented, cross-entity-type meaning: a
3165
+ * ready-made SQL predicate an entity's index precomputed (see
3166
+ * ENTITY-SEARCH-DESIGN.md's hierarchy-broadening pattern — branch/region/
3167
+ * state entity types attach it so a lane/route question never needs its
3168
+ * own city-matching join). Confirmed real cost of not doing this: a
3169
+ * branch/state resolved with scopeSql sat unused while the source agent
3170
+ * spent 4 failed exploratory queries rediscovering the exact same
3171
+ * branch-id list by hand (Invalid column name 'StateName'/'CityId'/
3172
+ * 'OrganizationLocationId', in that order, ~144s, before landing on ids
3173
+ * that were sitting in scopeSql from the very first resolve_entities
3174
+ * call). Other attrs (code, counts, etc.) are not dumped here — they are
3175
+ * descriptive, not actionable the way a precomputed predicate is, and
3176
+ * bloating every dispatch with all of them for no clear gain isn't worth
3177
+ * it; scopeSql is the one thing worth this guarantee today.
3178
+ */
3179
+ private buildResolvedEntitiesBlock;
3180
+ /**
3181
+ * Dedupe-and-retain helper shared by loadConceptInvariants and the generic
3182
+ * direct-tool `resolvedConcepts` opt-in hook. `alwaysInclude` marks entries
3183
+ * that buildResolvedConceptsBlock must attach to EVERY dispatch regardless
3184
+ * of that dispatch's `concepts` param — true only for invariants, which are
3185
+ * meant to apply everywhere by definition. Concepts retained without it
3186
+ * (e.g. via the `concept` tool's `get`) are scoped per-dispatch instead.
3187
+ */
3188
+ private retainResolvedConcepts;
3189
+ /**
3190
+ * Same purpose as buildResolvedEntitiesBlock, for concept bodies instead of
3191
+ * entity ids — appended to the dispatch intent so SourceAgent's own,
3192
+ * independent LLM call receives the resolved definitions verbatim rather
3193
+ * than only MainAgent's paraphrased intent text.
3194
+ *
3195
+ * Unlike buildResolvedEntitiesBlock, non-invariant entries here are SCOPED
3196
+ * to `names` (this dispatch's SourceToolInput.concepts) rather than
3197
+ * dumping the whole-turn `this.resolvedConcepts` accumulator into every
3198
+ * dispatch. A concept body is phrased as an authoritative override
3199
+ * ("overrides the schema... your own reasoning") — fetching several via
3200
+ * `get` to compare/discard, or a turn needing different concepts for
3201
+ * different sub-questions, must not leak an irrelevant concept's filters
3202
+ * into an unrelated dispatch. An entry not named in `names` and not
3203
+ * `alwaysInclude` is left out — a concept the model forgot to name is a
3204
+ * visible, fixable miss; one that leaks in uninvited is not.
3205
+ *
3206
+ * `alwaysInclude` entries (invariants — see loadConceptInvariants) bypass
3207
+ * `names` entirely and attach to every dispatch regardless, same as before
3208
+ * this method took a `names` param — they are meant to apply everywhere,
3209
+ * not just when the model happens to ask for them.
3210
+ */
3211
+ private buildResolvedConceptsBlock;
3212
+ /** True when the backend registered the entity-search collection. */
3213
+ private get entitySearchEnabled();
3214
+ /** True when the backend registered the concepts collection. */
3215
+ private get conceptsEnabled();
3216
+ /**
3217
+ * The concept catalog for the system prompt: one line per concept — name,
3218
+ * aliases, and what it is for. No bodies; those are fetched once the LLM has
3219
+ * named one, via whatever concept-lookup tool the project offers (MainAgent
3220
+ * itself no longer resolves concept bodies). Putting bodies here would
3221
+ * rebuild the always-on context problem concepts exist to solve.
3222
+ *
3223
+ * The description is what makes the list usable. It used to be names only,
3224
+ * which asked the model to judge from a name alone whether `ref-data null
3225
+ * sentinel` bears on the question in front of it — and a wrong judgement is
3226
+ * silent, since it simply does not fetch. Search was the earlier answer and
3227
+ * was worse: a lexical match cannot connect "how much did we spend on staff"
3228
+ * to a concept called `fte`, and its miss is indistinguishable from there
3229
+ * being nothing to find. A line of prose per concept costs a few tokens and
3230
+ * is read rather than matched.
3231
+ *
3232
+ * Aliases stay, because they are how a user is likely to phrase it, and
3233
+ * showing them is what stops the LLM inventing a near-miss name.
3234
+ *
3235
+ * Not cached, for the same reason loadEntityCatalog isn't: unchanged rows
3236
+ * render byte-identical text so the prompt-cache prefix still hits, and a
3237
+ * process cache would only buy a window where a new concept is invisible.
3238
+ */
3239
+ private loadConceptCatalog;
3240
+ /**
3241
+ * The ALWAYS-INJECTED tier: preconditions no question is semantically close
3242
+ * to — date encodings, valid-row filters. These cannot be retrieval-gated.
3243
+ * "What was revenue last quarter?" has no similarity to "dates are DD/MM/YYYY
3244
+ * text", so leaving them to be requested means they are silently dropped and
3245
+ * the answer is confidently wrong rather than visibly missing.
3246
+ *
3247
+ * Bodies ARE included here — unlike the catalog — because there is nothing to
3248
+ * request: being unrequestable is what makes them invariants.
3249
+ *
3250
+ * Also retained into resolvedConcepts so these bodies reach SourceAgent's
3251
+ * dispatch too — an invariant is meant to apply everywhere, not just
3252
+ * wherever the model happens to ask for something whose `requires` chain
3253
+ * reaches it. This is now the ONLY way a concept body reaches
3254
+ * resolvedConcepts/SourceAgent's dispatch automatically — a non-invariant
3255
+ * concept fetched through a project's own concept-lookup tool (if any) is
3256
+ * NOT retained here, and will not reach a later SourceAgent dispatch unless
3257
+ * the model itself restates it in the intent it writes.
3258
+ */
3259
+ private loadConceptInvariants;
3260
+ /**
3261
+ * Never throws: a concept-store outage must degrade to "no definition found"
3262
+ * and leave the agent deriving the formula as it does today, not take the
3263
+ * whole answer down.
3264
+ */
3265
+ /**
3266
+ * Compact catalog (type + gloss) for the system prompt. This is what lets the
3267
+ * LLM NAME a concept instead of guessing at our vocabulary — it can infer
3268
+ * that a question is about unpaid money, but not that we call that
3269
+ * `outstanding-balance`. Full `resolution` detail is fetched by the tool.
3270
+ *
3271
+ * Deliberately NOT cached in-process. The token cost is already handled by
3272
+ * prompt caching (the system prompt carries a cache_control breakpoint), and
3273
+ * re-reading a tiny PK-ordered table costs ~nothing. Fetching fresh does not
3274
+ * weaken the prompt cache either: unchanged data renders byte-identical text,
3275
+ * so the prefix still hits. A process cache would only buy a staleness window
3276
+ * in which a newly indexed entity is invisible to the agent.
3277
+ */
3278
+ private loadEntityCatalog;
3279
+ /**
3280
+ * Never throws: a resolver outage must degrade to "nothing resolved" and
3281
+ * leave the agent exactly as it was, not take the whole answer down.
3282
+ */
3283
+ private handleResolveEntities;
1978
3284
  private preResolveSchema;
1979
3285
  /**
1980
3286
  * Execute a direct tool — call fn() with LLM-provided params, no SourceAgent.
@@ -2019,16 +3325,6 @@ declare class MainAgent {
2019
3325
  getSourceSummaries(): SourceSummary[];
2020
3326
  }
2021
3327
 
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
3328
  type SystemPrompt = string | Anthropic.Messages.TextBlockParam[];
2033
3329
  interface LLMMessages {
2034
3330
  sys: SystemPrompt;
@@ -2041,7 +3337,62 @@ interface LLMOptions {
2041
3337
  temperature?: number;
2042
3338
  topP?: number;
2043
3339
  apiKey?: string;
3340
+ baseURL?: string;
2044
3341
  partial?: (chunk: string) => void;
3342
+ /**
3343
+ * Per-request cancellation. When the caller aborts this signal (user hit
3344
+ * "Stop"), the underlying provider request is cancelled and the call throws
3345
+ * a RequestAbortedError. Threaded into the provider `messages.create` request
3346
+ * options and checked between tool-loop iterations. Currently honored on the
3347
+ * Anthropic path (the agent flow's default provider).
3348
+ */
3349
+ signal?: AbortSignal;
3350
+ /**
3351
+ * Forces a tool call on the FIRST iteration of streamWithTools only
3352
+ * (subsequent iterations revert to auto). Used by fork mode to stop the
3353
+ * model from ending its turn with a bare "I'll adapt the script…" preamble
3354
+ * and zero tool calls. `{ type: 'any' }` lets the model pick which tool
3355
+ * (write_script in the common case, a source tool for schema discovery);
3356
+ * `{ type: 'tool', name }` pins a specific tool. Honored on both the
3357
+ * Anthropic path and the OpenAI/OpenRouter path (mapped to OpenAI's
3358
+ * tool_choice: 'required' / a named function).
3359
+ */
3360
+ firstIterationToolChoice?: {
3361
+ type: 'any';
3362
+ } | {
3363
+ type: 'tool';
3364
+ name: string;
3365
+ };
3366
+ /**
3367
+ * Same forcing as `firstIterationToolChoice`, for the SECOND iteration only
3368
+ * (iteration 3+ reverts to auto, same as iteration 2+ does for the first-
3369
+ * iteration option). Added for MainAgent's `concept` tool: `resolve_entities`
3370
+ * being pinned to iteration 1 doesn't, by itself, make `concept` reliable —
3371
+ * confirmed live, repeatedly, that a model can skip it even with a CRITICAL
3372
+ * prompt instruction telling it not to. Pinning iteration 2 the same way
3373
+ * `resolve_entities` pins iteration 1 makes it structurally guaranteed
3374
+ * instead of prose-dependent. Honored on both the Anthropic and OpenAI/
3375
+ * OpenRouter paths, same as `firstIterationToolChoice`.
3376
+ */
3377
+ secondIterationToolChoice?: {
3378
+ type: 'any';
3379
+ } | {
3380
+ type: 'tool';
3381
+ name: string;
3382
+ };
3383
+ /**
3384
+ * Internal — set only by the OpenRouter wrappers when the target is a Claude
3385
+ * model. Tells the OpenAI-wire path to emit Anthropic `cache_control`
3386
+ * breakpoints (OpenRouter forwards them to Anthropic for prompt caching).
3387
+ * Never set for direct OpenAI/Groq calls, so their requests are unchanged.
3388
+ */
3389
+ _openrouterClaudeCaching?: boolean;
3390
+ /**
3391
+ * Internal — OpenRouter provider-routing preferences (forwarded as the
3392
+ * `provider` body field). Set by the OpenRouter wrappers to steer routing to
3393
+ * a fast backend (e.g. {sort:'throughput'}). Never set for direct OpenAI/Groq.
3394
+ */
3395
+ _openrouterProvider?: Record<string, unknown>;
2045
3396
  }
2046
3397
  interface Tool {
2047
3398
  name: string;
@@ -2063,6 +3414,16 @@ declare class LLM {
2063
3414
  * @returns Normalized system prompt for Anthropic API
2064
3415
  */
2065
3416
  private static _normalizeSystemPrompt;
3417
+ /**
3418
+ * Strip unpaired UTF-16 surrogates from every text field of a message set.
3419
+ *
3420
+ * A lone surrogate (from mid-pair string slicing or corrupt source data)
3421
+ * serializes to a bare `\udXXX` escape that strict JSON parsers — including
3422
+ * the one on Anthropic's API — reject with "no low surrogate in string",
3423
+ * failing the whole request. Sanitizing here, at the single boundary every
3424
+ * provider call flows through, guarantees no request can carry one.
3425
+ */
3426
+ private static _sanitizeMessages;
2066
3427
  /**
2067
3428
  * Log cache usage metrics from Anthropic API response
2068
3429
  * Shows cache hits, costs, and savings
@@ -2079,11 +3440,46 @@ declare class LLM {
2079
3440
  * "claude-sonnet-4-5" → ["anthropic", "claude-sonnet-4-5"] (default)
2080
3441
  */
2081
3442
  private static _parseModel;
3443
+ /**
3444
+ * Map an Anthropic model id (e.g. "claude-sonnet-4-5-20250929") to the OpenRouter slug
3445
+ * (e.g. "claude-sonnet-4.5"). OpenRouter slugs drop the date suffix and use dotted versions.
3446
+ */
3447
+ private static _toOpenRouterSlug;
3448
+ /**
3449
+ * Per-provider proxy base URL. Returns `${SUPERATOM_LLM_PROXY_URL}/<provider>`
3450
+ * when our Cloudflare LLM proxy is configured, else undefined (→ talk to the
3451
+ * provider directly, legacy behaviour). An explicit options.baseURL (e.g.
3452
+ * OpenRouter) always wins and is never overridden. See backend/docs/llm-proxy.md.
3453
+ */
3454
+ private static _proxyBaseURL;
3455
+ private static _openrouterOptions;
3456
+ private static _isRetryableProviderError;
3457
+ private static _withOpenrouterRetry;
3458
+ private static _openrouterText;
3459
+ private static _openrouterStream;
3460
+ private static _openrouterStreamWithTools;
3461
+ /**
3462
+ * Build an Anthropic client. Routes through our Cloudflare LLM proxy when
3463
+ * SUPERATOM_LLM_PROXY_URL is set (each client ships a per-client proxy key as
3464
+ * ANTHROPIC_API_KEY and never holds the real key); otherwise talks to
3465
+ * api.anthropic.com directly. See backend/docs/llm-proxy.md.
3466
+ */
3467
+ private static _anthropicClient;
3468
+ /** True when OpenRouter is configured as a fail-open fallback for Claude. */
3469
+ private static _openrouterAvailable;
3470
+ /** Remap an Anthropic model id to the OpenRouter model path for fail-open. */
3471
+ private static _anthropicFallbackModel;
2082
3472
  private static _anthropicText;
2083
3473
  private static _anthropicStream;
2084
3474
  private static _anthropicStreamWithTools;
2085
3475
  private static _groqText;
2086
3476
  private static _groqStream;
3477
+ /**
3478
+ * Gemini request options carrying the proxy base URL, or undefined → talk to
3479
+ * generativelanguage.googleapis.com directly. The Google SDK takes baseUrl as a
3480
+ * per-model request option, not a constructor arg. See backend/docs/llm-proxy.md.
3481
+ */
3482
+ private static _geminiRequestOptions;
2087
3483
  private static _geminiText;
2088
3484
  private static _geminiStream;
2089
3485
  /**
@@ -2092,8 +3488,28 @@ declare class LLM {
2092
3488
  */
2093
3489
  private static _cleanSchemaForGemini;
2094
3490
  private static _geminiStreamWithTools;
3491
+ /** True for Anthropic/Claude model ids — gates OpenRouter prompt caching. */
3492
+ private static _isClaudeModel;
3493
+ /**
3494
+ * Build the OpenAI-wire system message. For OpenRouter + Claude
3495
+ * (cacheClaude=true) it emits content parts carrying Anthropic
3496
+ * `cache_control` breakpoints (preserving any the caller set, else marking
3497
+ * the last block), so OpenRouter forwards them to Anthropic for prompt
3498
+ * caching. Otherwise it returns a plain flattened string — unchanged for
3499
+ * direct OpenAI/Groq.
3500
+ */
3501
+ private static _openaiSystemMessage;
3502
+ /**
3503
+ * Split an OpenAI-wire usage object. `prompt_tokens` INCLUDES cached tokens,
3504
+ * so we subtract them out (Anthropic-style: input excludes cache reads) and
3505
+ * report cached separately — this makes calculateCost price cache reads at
3506
+ * the discounted rate and reflects OpenRouter prompt-cache savings in logs.
3507
+ */
3508
+ private static _openaiUsage;
2095
3509
  private static _openaiText;
2096
3510
  private static _openaiStream;
3511
+ /** Map the Anthropic-style firstIterationToolChoice to OpenAI's tool_choice. */
3512
+ private static _openaiToolChoice;
2097
3513
  private static _openaiStreamWithTools;
2098
3514
  /**
2099
3515
  * Parse JSON string, handling markdown code blocks and surrounding text
@@ -2185,7 +3601,7 @@ declare class UILogCollector {
2185
3601
 
2186
3602
  /**
2187
3603
  * 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
3604
+ * Contains user question, component metadata, component data, and text response
2189
3605
  */
2190
3606
  declare class UIBlock {
2191
3607
  private id;
@@ -2193,18 +3609,23 @@ declare class UIBlock {
2193
3609
  private generatedComponentMetadata;
2194
3610
  private componentData;
2195
3611
  private textResponse;
2196
- private actions;
2197
3612
  private createdAt;
3613
+ /**
3614
+ * Which script recipe produced this answer, when a script did. Read on the
3615
+ * NEXT turn so the user can say "use mode instead" and have the matcher
3616
+ * resolve it to a concrete script (the `edit` tier is unreachable without it).
3617
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 1.
3618
+ */
3619
+ private scriptBinding;
2198
3620
  /**
2199
3621
  * Creates a new UIBlock instance
2200
3622
  * @param userQuestion - The user's question or input
2201
3623
  * @param componentData - The component data object
2202
3624
  * @param generatedComponentMetadata - Optional metadata about the generated component
2203
- * @param actions - Optional array of available actions
2204
3625
  * @param id - Optional custom ID, generates UUID if not provided
2205
3626
  * @param textResponse - Optional text response from LLM
2206
3627
  */
2207
- constructor(userQuestion: string, componentData?: Record<string, any>, generatedComponentMetadata?: Record<string, any>, actions?: Action[], id?: string, textResponse?: string | null);
3628
+ constructor(userQuestion: string, componentData?: Record<string, any>, generatedComponentMetadata?: Record<string, any>, id?: string, textResponse?: string | null);
2208
3629
  /**
2209
3630
  * Get the UIBlock ID
2210
3631
  */
@@ -2254,41 +3675,22 @@ declare class UIBlock {
2254
3675
  * Set or update text response
2255
3676
  */
2256
3677
  setTextResponse(textResponse: string | null): void;
3678
+ /** The program-generated half of the answer — see setProgramAnswer. */
3679
+ programAnswer?: string | null;
2257
3680
  /**
2258
- * Get all actions (only if they are resolved, not if fetching)
2259
- */
2260
- getActions(): Action[] | null | Promise<Action[]>;
2261
- /**
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[]
2267
- */
2268
- getOrFetchActions(generateFn: () => Promise<Action[]>): Promise<Action[]>;
2269
- /**
2270
- * Set or replace all actions
2271
- */
2272
- setActions(actions: Action[]): void;
2273
- /**
2274
- * Add a single action (only if actions are resolved)
3681
+ * Bind this block to the script recipe that produced its answer.
2275
3682
  */
2276
- addAction(action: Action): void;
3683
+ setScriptBinding(binding: Record<string, any> | null): void;
2277
3684
  /**
2278
- * Add multiple actions (only if actions are resolved)
3685
+ * The program-generated half of the answer, kept verbatim so a reload can
3686
+ * replace exactly that text without disturbing the streamed narration.
2279
3687
  */
2280
- addActions(actions: Action[]): void;
3688
+ setProgramAnswer(text: string | null): void;
3689
+ getProgramAnswer(): string | null;
2281
3690
  /**
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
3691
+ * The script recipe bound to this block, if any.
2291
3692
  */
3693
+ getScriptBinding(): Record<string, any> | null;
2292
3694
  getCreatedAt(): Date;
2293
3695
  /**
2294
3696
  * Convert UIBlock to JSON-serializable object
@@ -2356,7 +3758,55 @@ declare class Thread {
2356
3758
  * @param currentUIBlockId - ID of current UIBlock to exclude from context (optional)
2357
3759
  * @returns Formatted conversation history string
2358
3760
  */
2359
- getConversationContext(limit?: number, currentUIBlockId?: string): string;
3761
+ /**
3762
+ * The script recipe bound to the most recent completed UIBlock — i.e. the
3763
+ * script behind the answer the user is currently looking at. Drives the
3764
+ * matcher's `edit` tier: without it, "use mode instead" has no target and
3765
+ * falls through to regeneration.
3766
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 1.
3767
+ */
3768
+ getActiveScriptBinding(currentUIBlockId?: string): Record<string, any> | null;
3769
+ /**
3770
+ * The recent script-backed answers in this thread, newest first — the
3771
+ * candidate set a user-directed edit can target.
3772
+ *
3773
+ * Returning several rather than only the newest is what lets an instruction
3774
+ * name its own target ("use the mode for the WSP one"). With a single
3775
+ * candidate every edit lands on the most recent script, which silently edits
3776
+ * the wrong recipe whenever the user meant an earlier one.
3777
+ *
3778
+ * Deduped by recipeId (newest occurrence wins) so a long editing session on
3779
+ * one script doesn't crowd out the others. Each entry carries the question
3780
+ * that produced it — without that the candidates are indistinguishable.
3781
+ *
3782
+ * In-memory only: dies with the process. The caller falls back to the
3783
+ * persisted bindings when this comes back empty.
3784
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 1.
3785
+ */
3786
+ getScriptBindings(limit?: number, currentUIBlockId?: string): Record<string, any>[];
3787
+ /**
3788
+ * @param getScriptBody Resolves a script-backed turn's EXACT script, pinned
3789
+ * to the version that turn actually ran — never today's (possibly
3790
+ * since-edited) live body. Owned by the caller (needs a ScriptStore, which
3791
+ * Thread doesn't hold) — omit it and every turn falls back to the
3792
+ * no-script summary below.
3793
+ * @param fetchRecentFromDb When provided, this is now the SOURCE OF TRUTH
3794
+ * for history instead of this process's in-memory `uiblocks` map — reads
3795
+ * the persisted `user_conversations` rows (DB `response` jsonb shape, see
3796
+ * transformUIBlockForDB in conversation-saver.ts) via the caller's own
3797
+ * `user-conversations.getRecentForContext` collection. Rows come back
3798
+ * newest-first (already excluding status='aborted' — the query itself
3799
+ * filters that, matching what the in-memory path achieved via
3800
+ * thread.removeUIBlock() on abort). Fixes the standing gap where a backend
3801
+ * restart lost every thread's history (ThreadManager has no DB-backed
3802
+ * rehydration) — DB-backed history survives that, and is correct even if
3803
+ * a different process instance saved the row. Omit it and this falls back
3804
+ * to the in-memory map, for any caller not yet updated.
3805
+ */
3806
+ getConversationContext(limit?: number, currentUIBlockId?: string, getScriptBody?: (recipeId: string, version: number) => Promise<string | null>, fetchRecentFromDb?: (limit: number) => Promise<Array<{
3807
+ userprompt?: string;
3808
+ response?: Record<string, any>;
3809
+ }>>): Promise<string>;
2360
3810
  /**
2361
3811
  * Convert Thread to JSON-serializable object
2362
3812
  */
@@ -2614,40 +4064,248 @@ declare class UserPromptErrorLogger {
2614
4064
  private hasErrors;
2615
4065
  constructor();
2616
4066
  /**
2617
- * Reset the error log file for a new request
4067
+ * Reset the error log file for a new request
4068
+ */
4069
+ resetLogFile(requestContext?: string): void;
4070
+ /**
4071
+ * Log a JSON parse error with the raw string that failed
4072
+ */
4073
+ logJsonParseError(context: string, rawString: string, error: Error): void;
4074
+ /**
4075
+ * Log a general error with full details
4076
+ */
4077
+ logError(context: string, error: Error | string, additionalData?: Record<string, any>): void;
4078
+ /**
4079
+ * Log a SQL query error with the full query
4080
+ */
4081
+ logSqlError(query: string, error: Error | string, params?: any[]): void;
4082
+ /**
4083
+ * Log an LLM API error
4084
+ */
4085
+ logLlmError(provider: string, model: string, method: string, error: Error | string, requestData?: any): void;
4086
+ /**
4087
+ * Log tool execution error
4088
+ */
4089
+ logToolError(toolName: string, toolInput: any, error: Error | string): void;
4090
+ /**
4091
+ * Write final summary if there were errors
4092
+ */
4093
+ writeSummary(): void;
4094
+ /**
4095
+ * Check if any errors were logged
4096
+ */
4097
+ hadErrors(): boolean;
4098
+ private write;
4099
+ }
4100
+ declare const userPromptErrorLogger: UserPromptErrorLogger;
4101
+
4102
+ /**
4103
+ * Chat Analytics Client
4104
+ * Posts analytics events to the global SA API after each user prompt request.
4105
+ *
4106
+ * Required env vars:
4107
+ * SA_API_URL — Base URL of the SA API (e.g. https://api.superatom.ai)
4108
+ *
4109
+ * Optional:
4110
+ * SA_ORG_ID — Organization ID (nullable, for multi-tenant filtering)
4111
+ * SA_ANALYTICS_ENABLED — Set to 'false' to disable (default: true)
4112
+ */
4113
+ type AnalyticsEventType = 'chat_agent' | 'dashboard' | 'report';
4114
+ type AnalyticsStatus = 'success' | 'error' | 'aborted';
4115
+ type AnalyticsResponse = DBUIBlock | {
4116
+ response_text: string;
4117
+ dsl: any;
4118
+ error?: any;
4119
+ };
4120
+ interface ChatAnalyticsEvent {
4121
+ type?: AnalyticsEventType;
4122
+ userId: string;
4123
+ orgId?: string;
4124
+ projectId: string;
4125
+ threadId?: string;
4126
+ messageIndex?: number;
4127
+ question?: string;
4128
+ sourcesUsed?: {
4129
+ sourceId: string;
4130
+ sourceName: string;
4131
+ sourceType: string;
4132
+ }[];
4133
+ sqlGenerated?: string;
4134
+ appId?: string;
4135
+ conversationId?: number;
4136
+ model: string;
4137
+ inputTokens: number;
4138
+ outputTokens: number;
4139
+ cost: string;
4140
+ latencyMs: number;
4141
+ status: AnalyticsStatus;
4142
+ errorMessage?: string;
4143
+ response?: AnalyticsResponse | null;
4144
+ }
4145
+ declare class AnalyticsClient {
4146
+ private apiUrl;
4147
+ private orgId;
4148
+ private enabled;
4149
+ private initialized;
4150
+ private init;
4151
+ /**
4152
+ * Track a chat analytics event. Fire-and-forget — never blocks or throws.
4153
+ */
4154
+ track(event: ChatAnalyticsEvent): Promise<void>;
4155
+ /**
4156
+ * Build a ChatAnalyticsEvent from the available request context.
4157
+ * Reads session stats from llmUsageLogger by default — correct for chat and
4158
+ * report, since both funnel their LLM calls through llm.ts, which is what
4159
+ * updates llmUsageLogger. Dashboard-agent (Pi) does NOT go through llm.ts —
4160
+ * it tracks its own cost/tokens via session.getSessionStats() entirely
4161
+ * separately, so llmUsageLogger stays at zero for those calls. Passing
4162
+ * `usage` explicitly overrides the llmUsageLogger read for exactly that
4163
+ * case; chat/report call sites omit it and keep the default behavior.
4164
+ */
4165
+ buildEvent(params: {
4166
+ type?: AnalyticsEventType;
4167
+ userId: string;
4168
+ projectId: string;
4169
+ threadId?: string;
4170
+ messageIndex?: number;
4171
+ question?: string;
4172
+ status: AnalyticsStatus;
4173
+ latencyMs: number;
4174
+ model: string;
4175
+ errorMessage?: string;
4176
+ sourcesUsed?: {
4177
+ sourceId: string;
4178
+ sourceName: string;
4179
+ sourceType: string;
4180
+ }[];
4181
+ sqlGenerated?: string;
4182
+ appId?: string;
4183
+ conversationId?: number;
4184
+ usage?: {
4185
+ inputTokens: number;
4186
+ outputTokens: number;
4187
+ costUSD: number;
4188
+ };
4189
+ response?: AnalyticsResponse | null;
4190
+ }): ChatAnalyticsEvent;
4191
+ }
4192
+ declare const analyticsClient: AnalyticsClient;
4193
+
4194
+ interface PromptLoaderConfig {
4195
+ promptsDir?: string;
4196
+ }
4197
+ /**
4198
+ * PromptLoader class for loading and processing prompt templates
4199
+ * Tries to load from file system first, then falls back to hardcoded prompts
4200
+ */
4201
+ declare class PromptLoader {
4202
+ private promptsDir;
4203
+ private promptCache;
4204
+ private databaseRulesCache;
4205
+ private isInitialized;
4206
+ private databaseType;
4207
+ constructor(config?: PromptLoaderConfig);
4208
+ /**
4209
+ * Load a prompt template from file system OR fallback to hardcoded prompts
4210
+ * @param promptName - Name of the prompt folder
4211
+ * @returns Template with system and user prompts
4212
+ */
4213
+ private loadPromptTemplate;
4214
+ /**
4215
+ * Initialize and cache all prompts into memory
4216
+ * Tries file system first, then falls back to hardcoded prompts
4217
+ */
4218
+ initialize(): Promise<void>;
4219
+ /**
4220
+ * Replace variables in a template string using {{VARIABLE_NAME}} pattern
4221
+ * @param template - Template string with placeholders
4222
+ * @param variables - Variables to replace in the template
4223
+ * @returns Processed string
4224
+ */
4225
+ private replaceVariables;
4226
+ /**
4227
+ * Load both system and user prompts from cache and replace variables
4228
+ * Supports prompt caching by splitting static and dynamic content
4229
+ * @param promptName - Name of the prompt
4230
+ * @param variables - Variables to replace in the templates
4231
+ * @returns Object containing both system and user prompts (system can be string or array for caching)
4232
+ */
4233
+ loadPrompts(promptName: string, variables: Record<string, string | number | boolean | any[]>): Promise<{
4234
+ system: string | any[];
4235
+ user: string;
4236
+ }>;
4237
+ /**
4238
+ * DEPRECATED: Use loadPrompts instead
4239
+ * Load a single prompt file and replace variables using {{VARIABLE_NAME}} pattern
2618
4240
  */
2619
- resetLogFile(requestContext?: string): void;
4241
+ loadPrompt(promptName: string, promptType: 'system' | 'user', variables: Record<string, string | number | boolean | any[]>): Promise<string | any[]>;
2620
4242
  /**
2621
- * Log a JSON parse error with the raw string that failed
4243
+ * Set custom prompts directory (requires re-initialization)
4244
+ * @param dir - Path to the prompts directory
2622
4245
  */
2623
- logJsonParseError(context: string, rawString: string, error: Error): void;
4246
+ setPromptsDir(dir: string): void;
2624
4247
  /**
2625
- * Log a general error with full details
4248
+ * Get current prompts directory
4249
+ * @returns Path to the prompts directory
2626
4250
  */
2627
- logError(context: string, error: Error | string, additionalData?: Record<string, any>): void;
4251
+ getPromptsDir(): string;
2628
4252
  /**
2629
- * Log a SQL query error with the full query
4253
+ * Check if prompts are loaded in memory
2630
4254
  */
2631
- logSqlError(query: string, error: Error | string, params?: any[]): void;
4255
+ isReady(): boolean;
2632
4256
  /**
2633
- * Log an LLM API error
4257
+ * Get the number of cached prompts
2634
4258
  */
2635
- logLlmError(provider: string, model: string, method: string, error: Error | string, requestData?: any): void;
4259
+ getCacheSize(): number;
2636
4260
  /**
2637
- * Log tool execution error
4261
+ * Set the database type for SQL rules loading
4262
+ * @param type - Database type ('postgresql' | 'mssql')
2638
4263
  */
2639
- logToolError(toolName: string, toolInput: any, error: Error | string): void;
4264
+ setDatabaseType(type: string): void;
2640
4265
  /**
2641
- * Write final summary if there were errors
4266
+ * Get current database type
4267
+ * @returns Database type string
2642
4268
  */
2643
- writeSummary(): void;
4269
+ getDatabaseType(): string;
2644
4270
  /**
2645
- * Check if any errors were logged
4271
+ * Load database-specific SQL rules from file system
4272
+ * Falls back to minimal default rules if file not found
4273
+ * @returns Database rules as a string
2646
4274
  */
2647
- hadErrors(): boolean;
2648
- private write;
4275
+ loadDatabaseRules(): Promise<string>;
4276
+ /**
4277
+ * Load database-specific SQL rules for a given source type.
4278
+ * Used by the agent architecture where multiple source types can coexist.
4279
+ * @param sourceType - Source type from tool ID (e.g., 'postgres', 'mssql', 'mysql')
4280
+ * @returns Database rules as a string
4281
+ */
4282
+ loadDatabaseRulesForType(sourceType: string): Promise<string>;
4283
+ /**
4284
+ * Load the shared Script Body Contract — the rules that define what a valid
4285
+ * script body IS. Injected as {{SCRIPT_BODY_CONTRACT}} into BOTH `agent-main`
4286
+ * (generate) and `agent-main-edit` (edit) so the two paths can never drift on
4287
+ * script shape. See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 3.
4288
+ *
4289
+ * IMPORTANT: the contract carries its OWN placeholders ({{MAX_ROWS_RENDERED}},
4290
+ * {{MAX_ROWS_FETCHED}}). loadPrompts' substitution is single-pass and
4291
+ * non-recursive by design (see replaceVariables), so an injected value is
4292
+ * never re-scanned — we must render the contract's placeholders HERE, before
4293
+ * it becomes a variable value, or they reach the LLM as literal `{{...}}`.
4294
+ */
4295
+ loadScriptBodyContract(variables?: Record<string, string | number | boolean | any[]>): Promise<string>;
4296
+ /**
4297
+ * Get default database rules as fallback (mirror of
4298
+ * backend/.prompts/database-rules/<engine>.md, embedded via DATABASE_RULES).
4299
+ * @returns Database rules for the configured databaseType
4300
+ */
4301
+ private getDefaultDatabaseRules;
4302
+ /**
4303
+ * Get default database rules for a specific engine type (mirror of
4304
+ * backend/.prompts/database-rules/<engine>.md, embedded via DATABASE_RULES).
4305
+ */
4306
+ private getDefaultDatabaseRulesForType;
2649
4307
  }
2650
- declare const userPromptErrorLogger: UserPromptErrorLogger;
4308
+ declare const promptLoader: PromptLoader;
2651
4309
 
2652
4310
  /**
2653
4311
  * BM25L Reranker for hybrid semantic search
@@ -2950,21 +4608,20 @@ declare abstract class BaseLLM {
2950
4608
  */
2951
4609
  protected containsFormComponent(component: any): boolean;
2952
4610
  /**
2953
- * Match components from text response suggestions and generate follow-up questions
4611
+ * Match components from text response suggestions
2954
4612
  * 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
4613
+ * Also generates title and description based on the analysis
2956
4614
  * All components are placed in a default MultiComponentContainer layout
2957
4615
  * @param analysisContent - The text response containing component suggestions
2958
4616
  * @param components - List of available components
2959
4617
  * @param apiKey - Optional API key
2960
4618
  * @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
4619
+ * @returns Object containing matched components and layout title/description
2962
4620
  */
2963
4621
  matchComponentsFromAnalysis(analysisContent: string, components: Component[], userPrompt: string, apiKey?: string, componentStreamCallback?: (component: Component) => void, deferredTools?: any[], executedTools?: any[], collections?: any, userId?: string): Promise<{
2964
4622
  components: Component[];
2965
4623
  layoutTitle: string;
2966
4624
  layoutDescription: string;
2967
- actions: Action[];
2968
4625
  }>;
2969
4626
  /**
2970
4627
  * Classify user question into category and detect external tools needed
@@ -3013,12 +4670,6 @@ declare abstract class BaseLLM {
3013
4670
  * 3. Route appropriately based on category and response mode
3014
4671
  */
3015
4672
  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
4673
  }
3023
4674
 
3024
4675
  interface AnthropicLLMConfig extends BaseLLMConfig {
@@ -3224,6 +4875,916 @@ declare class DashboardConversationHistory {
3224
4875
  }
3225
4876
  declare const dashboardConversationHistory: DashboardConversationHistory;
3226
4877
 
4878
+ /**
4879
+ * Whole-dashboard generation via Pi, a terminal coding agent — as opposed to
4880
+ * DASH_COMP_REQ's single-widget-at-a-time flow. Runs Pi in-process via its
4881
+ * SDK (createAgentSession), not as a subprocess: no shell, no argument
4882
+ * quoting, no stdin/stdout piping, none of the Windows-specific subprocess
4883
+ * issues that came with spawning the `pi` CLI directly.
4884
+ *
4885
+ * Called from sdk-nodejs/src/dashboardAgent/index.ts (DASHBOARD_AGENT_REQ),
4886
+ * which owns the generic streaming/abort machinery (mirrors USER_PROMPT_REQ)
4887
+ * and passes `signal`/`onProgress` alongside the normal params — this stays
4888
+ * within CollectionHandler's loose (params) => Promise<result> typing, no
4889
+ * change needed to that shared type.
4890
+ *
4891
+ * This mechanism is generic and reusable across any deployment. What's
4892
+ * genuinely project-specific — where AGENTS.md lives, which model to use —
4893
+ * is supplied via `DashboardAgentCollectionConfig`, with defaults sensible
4894
+ * enough that most callers don't need to override them (see below). The
4895
+ * data-source tool list and the dashboard's current state both come from
4896
+ * things sdk-nodejs already exposes generically: `sdk.getTools()` (whatever
4897
+ * this deployment registered via `sdk.setTools()`) and `sdk.callCollection
4898
+ * ('dashboards', 'query', ...)` (whatever this deployment already registered
4899
+ * under that name/shape) — no per-deployment callback needed for either.
4900
+ * Same convention on the way out: after a successful run, the prompt and the
4901
+ * full response text are handed to `sdk.callCollection('dashboard-agent-
4902
+ * conversations', 'create', ...)` if this deployment has registered one —
4903
+ * skipped silently otherwise, since conversation history is optional.
4904
+ *
4905
+ * Pi verifies every query against the live database itself (via whatever
4906
+ * local tool-execution bridge the deployment exposes, e.g. an HTTP bridge
4907
+ * on localhost), but does NOT persist the result itself — it writes the
4908
+ * finished DSL to an absolute path inside `runtimeDir`, told to it explicitly
4909
+ * in the prompt, and stops there. This handler reads that file after the run
4910
+ * finishes and returns its content as `dashboard` in the result. The caller
4911
+ * (frontend) is the one that actually saves it, via whatever authenticated
4912
+ * create/update path any other dashboard edit goes through — Pi has no user
4913
+ * session/auth context of its own, so persistence shouldn't happen from
4914
+ * inside it.
4915
+ *
4916
+ * Session persistence: the FIRST call for a dashboardId pays the full cost
4917
+ * (explore KB, discover schema, plan, verify, build). Every call after that
4918
+ * resumes the same session file (SessionManager.open) so Pi has everything
4919
+ * it already learned — it only needs to reason about the new, smaller ask,
4920
+ * not rediscover the whole dashboard from scratch. The session's file path
4921
+ * (AgentSession.sessionFile) is captured right after creation and persisted
4922
+ * in a small local file, keyed by dashboardId, inside `runtimeDir`.
4923
+ */
4924
+ interface DashboardAgentCollectionConfig {
4925
+ /**
4926
+ * Working directory Pi runs from — must contain AGENTS.md. This is a
4927
+ * version-controlled prompt file, so `cwd` is expected to live somewhere
4928
+ * like a `.prompts/` folder alongside the deployment's other prompts.
4929
+ * Default: `<process.cwd()>/.prompts/dashboard-agent` — the same
4930
+ * process.cwd()-based convention PromptLoader already uses for the main
4931
+ * agent's prompts, which needs no explicit override in the common case
4932
+ * (the backend process's own cwd already is its project root).
4933
+ */
4934
+ cwd?: string;
4935
+ /**
4936
+ * Where drafts/, the session-id map, and dashboard.log get written —
4937
+ * separate from `cwd` deliberately, so this deployment's runtime state
4938
+ * (regenerated per session, safe to gitignore) doesn't sit inside the
4939
+ * same folder as the version-controlled AGENTS.md prompt.
4940
+ * Default: `<process.cwd()>/.pi-dashboard-agent-runtime`.
4941
+ */
4942
+ runtimeDir?: string;
4943
+ /** Model provider (default: process.env.PI_AGENT_PROVIDER || 'openrouter'). */
4944
+ provider?: string;
4945
+ /** Model id (default: process.env.PI_AGENT_MODEL || 'anthropic/claude-sonnet-4.5'). */
4946
+ model?: string;
4947
+ /**
4948
+ * true (default): every request starts a brand-new pi session, with the 2
4949
+ * most recent prior responses (if any) injected into the prompt as
4950
+ * context — bounded cost per request, but pi re-explores schema/KB facts
4951
+ * it already verified in an earlier turn on this same dashboard.
4952
+ * false: resumes the same session file across requests on a given
4953
+ * dashboard — pi keeps everything it already learned, but context (and
4954
+ * cost) grows unbounded across turns (one observed turn: 2M+ cache-read
4955
+ * tokens after a handful of edits on the same dashboard).
4956
+ * Default: process.env.PI_AGENT_FRESH_SESSION !== 'false'.
4957
+ */
4958
+ freshSession?: boolean;
4959
+ }
4960
+ declare function registerDashboardAgentCollection(sdk: SuperatomSDK, config?: DashboardAgentCollectionConfig): void;
4961
+
4962
+ /**
4963
+ * Coding-agent driver contract — one shape over every harness (codex today;
4964
+ * claude-code / opencode / pi next). Callers depend on this file only, never on
4965
+ * a harness SDK, so swapping the agent behind a task is a config change.
4966
+ *
4967
+ * Deliberately narrower than the harness SDKs it wraps: the three things a
4968
+ * production run needs and that harness abstractions usually omit — real abort,
4969
+ * real token accounting, and structured tool args — are part of the contract,
4970
+ * not per-driver extras.
4971
+ */
4972
+ type AgentEventKind = 'turn' | 'message' | 'reasoning' | 'command' | 'tool' | 'file' | 'todo' | 'error';
4973
+ interface AgentEvent {
4974
+ kind: AgentEventKind;
4975
+ /** Stable across started→updated→completed, so a UI updates one block in place. */
4976
+ id?: string;
4977
+ text?: string;
4978
+ command?: string;
4979
+ output?: string;
4980
+ /** Tool calls carry real parsed args — never SQL scraped back out of a shell string. */
4981
+ tool?: {
4982
+ server?: string;
4983
+ name: string;
4984
+ args?: unknown;
4985
+ };
4986
+ status?: 'in_progress' | 'completed' | 'failed';
4987
+ done?: boolean;
4988
+ }
4989
+ interface AgentUsage {
4990
+ inputTokens: number;
4991
+ outputTokens: number;
4992
+ reasoningTokens: number;
4993
+ cacheReadTokens: number;
4994
+ cacheWriteTokens: number;
4995
+ /** Tool + command invocations. With tokens, separates "silently did nothing" from a real run. */
4996
+ toolCalls: number;
4997
+ /** Only when the harness reports it — codex reports tokens but not cost. */
4998
+ costUSD?: number;
4999
+ }
5000
+ interface AgentRunHandlers {
5001
+ /** Formatted live stream: assistant text deltas plus one-line tool/command markers. */
5002
+ onOutput?: (chunk: string) => void;
5003
+ onEvent?: (event: AgentEvent) => void;
5004
+ /** Assistant prose only — safe to show a non-technical user as progress. */
5005
+ onNarration?: (text: string) => void;
5006
+ onUsage?: (usage: AgentUsage) => void;
5007
+ /**
5008
+ * Polled once the turn is live. When it returns true the run ends immediately —
5009
+ * for tasks whose deliverable is a file, the moment it exists there is nothing
5010
+ * left to wait for.
5011
+ */
5012
+ doneWhen?: () => boolean | Promise<boolean>;
5013
+ }
5014
+ type AgentRunStatus = 'completed' | 'early' | 'aborted' | 'error';
5015
+ interface AgentRunResult {
5016
+ status: AgentRunStatus;
5017
+ /** The final assistant message. */
5018
+ answer: string;
5019
+ ms: number;
5020
+ usage: AgentUsage;
5021
+ /**
5022
+ * Set when status is 'error'. Covers stream-reported failures too, which arrive
5023
+ * as events rather than thrown exceptions — a swallowed provider error is the
5024
+ * failure mode that otherwise looks like a successful empty run.
5025
+ */
5026
+ error?: string;
5027
+ sessionId?: string;
5028
+ }
5029
+ interface McpServerSpec {
5030
+ command: string;
5031
+ args?: string[];
5032
+ env?: Record<string, string>;
5033
+ }
5034
+ interface CodingAgentSessionOptions {
5035
+ /** Working directory the agent operates in. Scope it to a runtime dir, not a source checkout. */
5036
+ cwd: string;
5037
+ model?: string;
5038
+ /** Resume a prior conversation — the value a previous run returned as sessionId. */
5039
+ resumeId?: string;
5040
+ /** Tools, as MCP servers. Keyed by server name, which shows up on every tool event. */
5041
+ mcpServers?: Record<string, McpServerSpec>;
5042
+ /**
5043
+ * Extra env for the agent process. By default only a small allowlist is passed
5044
+ * through (see ENV_ALLOWLIST) so an agent with shell access can't read the
5045
+ * backend's own secrets out of its environment.
5046
+ */
5047
+ env?: Record<string, string>;
5048
+ /** Opt out of env scrubbing. Only for local debugging. */
5049
+ inheritEnv?: boolean;
5050
+ }
5051
+ interface CodingAgentSession {
5052
+ readonly harness: string;
5053
+ /** Structured event log vs. a raw terminal stream — the UI picks its renderer from this. */
5054
+ readonly kind: 'events' | 'pty';
5055
+ run(prompt: string, handlers?: AgentRunHandlers, signal?: AbortSignal): Promise<AgentRunResult>;
5056
+ /** Rolling structured events, for replay when a client reconnects mid-run. */
5057
+ events(): AgentEvent[];
5058
+ /** Rolling formatted text, same purpose as events() for text-oriented views. */
5059
+ buffer(): string;
5060
+ busy(): boolean;
5061
+ /** Persist this and pass it back as resumeId to continue the conversation. */
5062
+ sessionId(): string | undefined;
5063
+ /** Abandon the conversation; the next run starts fresh. */
5064
+ reset(): void;
5065
+ dispose(): void;
5066
+ }
5067
+
5068
+ /**
5069
+ * Harness registry. Kept separate from index.ts so collection.ts can reach the
5070
+ * factory without importing the module's own barrel (a cycle tsup would have to
5071
+ * resolve at bundle time).
5072
+ */
5073
+ type Harness = 'codex' | 'pi';
5074
+ declare function createCodingAgentSession(harness: Harness, opts: CodingAgentSessionOptions): CodingAgentSession;
5075
+
5076
+ /**
5077
+ * Codex driver — OpenAI Codex via @openai/codex-sdk.
5078
+ *
5079
+ * FORMAT CONTRACT: written against @openai/codex-sdk 0.149.1. Harness event
5080
+ * shapes drift; if the event log goes blank after an SDK bump, check normEvent
5081
+ * against the package's dist/index.d.ts first.
5082
+ *
5083
+ * Three things this leans on that the SDK gives natively, so we don't reinvent
5084
+ * them: TurnOptions.signal (real cancellation), TurnCompletedEvent.usage (token
5085
+ * accounting), and mcp_tool_call items (tool args arrive parsed, not scraped
5086
+ * out of a shell command string).
5087
+ *
5088
+ * The package is an optionalDependency: every deployment gets it, but an
5089
+ * install that cannot fetch the ~309MB platform binary skips it rather than
5090
+ * failing. It is therefore loaded dynamically and its types mirrored
5091
+ * structurally below, so a VM without it still builds, starts, and runs
5092
+ * everything else — only the coding agent reports itself unavailable.
5093
+ */
5094
+ type CodexSandboxMode = 'read-only' | 'workspace-write' | 'danger-full-access';
5095
+ type CodexReasoningEffort = 'minimal' | 'low' | 'medium' | 'high' | 'xhigh' | 'max' | 'ultra';
5096
+ interface CodexSessionOptions extends CodingAgentSessionOptions {
5097
+ reasoningEffort?: CodexReasoningEffort;
5098
+ /**
5099
+ * Filesystem reach. Default 'workspace-write' — the agent writes its
5100
+ * deliverable into cwd and nothing else. 'danger-full-access' hands a
5101
+ * shell-capable agent the whole box; don't use it on a client VM.
5102
+ */
5103
+ sandboxMode?: CodexSandboxMode;
5104
+ /** Network for commands the agent runs. Off by default; MCP tools don't need it. */
5105
+ networkAccessEnabled?: boolean;
5106
+ apiKey?: string;
5107
+ baseUrl?: string;
5108
+ /** Raw `--config` overrides merged into the codex config. */
5109
+ extraConfig?: Record<string, unknown>;
5110
+ approvalPolicy?: 'never' | 'on-request' | 'on-failure' | 'untrusted';
5111
+ }
5112
+ declare function createCodexSession(opts: CodexSessionOptions): CodingAgentSession;
5113
+
5114
+ /**
5115
+ * One transcript entry — everything worth replaying to a caller that reconnects
5116
+ * mid-run or after the fact, without having sent the prompt itself. Deliberately
5117
+ * a separate, richer shape from AgentEvent: that one only carries tool/command
5118
+ * activity (what events()/buffer() below still expose, unchanged), not prompts
5119
+ * or finished answers — which a reconnecting client needs too, per-turn, not as
5120
+ * a flat concatenated buffer.
5121
+ */
5122
+ interface PiTranscriptEntry {
5123
+ at: number;
5124
+ kind: 'user' | 'assistant' | 'activity' | 'error';
5125
+ /** user/assistant/error text. */
5126
+ text?: string;
5127
+ /** 'activity' entries carry the underlying tool/command event. */
5128
+ event?: AgentEvent;
5129
+ }
5130
+ interface PiSessionState {
5131
+ running: boolean;
5132
+ transcript: PiTranscriptEntry[];
5133
+ }
5134
+ type PiSessionListener = (sessionKey: string, entry: PiTranscriptEntry) => void;
5135
+ /** Watch every live pi session's activity, not just one you started yourself — same broadcast idea dashboard-pi uses so a second, later caller can watch a session already in progress. */
5136
+ declare function onPiSessionUpdate(listener: PiSessionListener): () => void;
5137
+ /**
5138
+ * Current state of a live session — for a caller that reconnects mid-run or
5139
+ * wants to replay what happened, without sending a prompt itself.
5140
+ *
5141
+ * undefined when the session isn't live: never started, evicted by the idle
5142
+ * sweeper, or the backend restarted. Same "lost the transcript, not the work"
5143
+ * tradeoff as everywhere else in this driver — nothing here survives a restart.
5144
+ */
5145
+ declare function getPiSessionState(sessionKey: string): PiSessionState | undefined;
5146
+
5147
+ /**
5148
+ * What makes an agent fire.
5149
+ *
5150
+ * Declared ON the definition, deliberately. If triggers lived at the call sites
5151
+ * instead — a counter in the conversation handler, a hook in the kb-nodes
5152
+ * collection — then adding an agent would mean editing unrelated files across
5153
+ * the backend, and no one file would tell you when an agent actually runs.
5154
+ * Here, an agent's folder plus its definition is the whole story.
5155
+ *
5156
+ * The deployment's only job is to announce that something happened:
5157
+ *
5158
+ * emitAgentEvent('conversation.answered', { threadId })
5159
+ * emitAgentEvent('kb-node.changed', { id, op: 'update' })
5160
+ *
5161
+ * Which agents care, and how many events it takes, is the agents' business.
5162
+ * The emitter therefore speaks the WORLD's vocabulary, never any one agent's —
5163
+ * translating an event into a particular agent's input is `buildInput` below.
5164
+ */
5165
+ /** One announced fact, as it reaches a trigger. */
5166
+ interface TriggerEvent {
5167
+ /** The event name passed to emitAgentEvent. */
5168
+ name: string;
5169
+ payload?: unknown;
5170
+ }
5171
+ /**
5172
+ * Turns the events that caused this run into the agent's `input`.
5173
+ *
5174
+ * Takes the whole list, not one payload, because every batching trigger here
5175
+ * fires for MANY events: a debounced burst, or N counted occurrences. Handing
5176
+ * over only the last one silently discarded the rest — five edited nodes became
5177
+ * one.
5178
+ *
5179
+ * Omit it and the agent receives the most recent payload unchanged, which is
5180
+ * the historical behaviour. Any agent that batches should declare one.
5181
+ */
5182
+ type BuildTriggerInput = (events: TriggerEvent[]) => unknown;
5183
+ type AgentTrigger =
5184
+ /** Only runCodingAgent(). The default. */
5185
+ {
5186
+ type: 'manual';
5187
+ }
5188
+ /** Every N milliseconds while the process is up. Fires with no events. */
5189
+ | {
5190
+ type: 'interval';
5191
+ everyMs: number;
5192
+ buildInput?: BuildTriggerInput;
5193
+ }
5194
+ /**
5195
+ * On a named event. `debounceMs` collapses a burst into one run — editing
5196
+ * five knowledge nodes in a row is one reason to rebuild, not five. The run
5197
+ * still receives all five events; only the RUNS are collapsed.
5198
+ */
5199
+ | {
5200
+ type: 'event';
5201
+ on: string | string[];
5202
+ debounceMs?: number;
5203
+ buildInput?: BuildTriggerInput;
5204
+ }
5205
+ /**
5206
+ * Every Nth occurrence of an event. The events are PERSISTED, so "after 10
5207
+ * questions" survives a restart instead of silently starting over.
5208
+ */
5209
+ | {
5210
+ type: 'count';
5211
+ on: string;
5212
+ every: number;
5213
+ buildInput?: BuildTriggerInput;
5214
+ };
5215
+ interface TriggerFireInfo {
5216
+ agentId: string;
5217
+ /** What set it off, for the log line. */
5218
+ reason: string;
5219
+ /** Everything that caused this run, in arrival order. */
5220
+ events: TriggerEvent[];
5221
+ /** What buildInput produced — exactly what the agent was handed. */
5222
+ input?: unknown;
5223
+ }
5224
+ /** Notified after every triggered run — for recording history or alerting. */
5225
+ type RunListener = (record: AgentRunRecord, info: TriggerFireInfo) => void;
5226
+ declare function onAgentRun(fn: RunListener): () => void;
5227
+ /**
5228
+ * Tell the agents something happened. Safe to call from anywhere, including a
5229
+ * request path — it never throws and never blocks: matching runs are started,
5230
+ * not awaited.
5231
+ */
5232
+ declare function emitAgentEvent(event: string, payload?: unknown): void;
5233
+ /**
5234
+ * Start interval triggers. Call once at startup, after registerCodingAgents.
5235
+ * Event and count triggers need nothing started — they wake on emitAgentEvent.
5236
+ */
5237
+ declare function startAgentTriggers(): void;
5238
+ declare function stopAgentTriggers(): void;
5239
+ /** What fires each agent — for a startup log or an admin screen. */
5240
+ declare function describeTriggers(): Array<{
5241
+ agentId: string;
5242
+ triggers: string[];
5243
+ }>;
5244
+
5245
+ /**
5246
+ * What a coding agent IS.
5247
+ *
5248
+ * An agent is a job description, not code that does the job: instructions, the
5249
+ * inputs it should be handed, the tools it may reach, the deliverable it must
5250
+ * produce, and what we do with that deliverable. The agent itself decides HOW —
5251
+ * it can read the inputs, write and run scripts over them, check its own work,
5252
+ * and iterate. That is the whole reason for using a coding agent rather than a
5253
+ * single LLM call: the job's data never has to fit in a context window.
5254
+ *
5255
+ * Adding an agent is a folder with a system.md, one file implementing this
5256
+ * interface, and one line in the registry. Nothing else changes.
5257
+ */
5258
+ /** The disposable directory one run happens in. */
5259
+ interface Workspace {
5260
+ /** Absolute path the agent runs in. Everything below is relative to it. */
5261
+ dir: string;
5262
+ /** Unique per run — also the folder name, so runs never collide. */
5263
+ runId: string;
5264
+ /** Write a file into the workspace, creating parent directories. */
5265
+ write(relPath: string, contents: string): Promise<void>;
5266
+ /** Write JSON (pretty-printed — the agent reads these by eye as often as by script). */
5267
+ writeJson(relPath: string, value: unknown): Promise<void>;
5268
+ /** Write one JSON object per line. The right shape for anything the agent will stream or grep. */
5269
+ writeJsonl(relPath: string, rows: unknown[]): Promise<void>;
5270
+ /** Read a file the agent produced. Returns null when it isn't there. */
5271
+ read(relPath: string): Promise<string | null>;
5272
+ /** Absolute path for a workspace-relative path — for telling the agent where to write. */
5273
+ pathTo(relPath: string): string;
5274
+ }
5275
+ /** Minimal validator shape, so this file needn't depend on a schema library. */
5276
+ interface OutputValidator<T> {
5277
+ /** Throw with a useful message, or return the parsed value. Zod's `.parse` satisfies this. */
5278
+ parse(value: unknown): T;
5279
+ }
5280
+ interface OutputContract<T> {
5281
+ /**
5282
+ * Workspace-relative path the agent must write, e.g. 'out/concepts.json'.
5283
+ * Nothing else counts as the deliverable: the run ends the moment this
5284
+ * appears, and a run that finishes without it is a failure, not a partial
5285
+ * success.
5286
+ */
5287
+ file: string;
5288
+ /** Validated before anything is persisted, so a malformed run fails loudly and applies nothing. */
5289
+ schema: OutputValidator<T>;
5290
+ }
5291
+ /** What apply() reports back — surfaced in the run record and to the caller. */
5292
+ interface ApplySummary {
5293
+ /** One line a human can read in a log or the UI. */
5294
+ summary: string;
5295
+ /** Whatever counts for this agent: {created, updated, skipped}, etc. */
5296
+ counts?: Record<string, number>;
5297
+ /** Anything the caller may want to inspect or show. */
5298
+ details?: unknown;
5299
+ }
5300
+ interface AgentRunContext {
5301
+ projectId?: string;
5302
+ userId?: string;
5303
+ /**
5304
+ * This run's id — also the workspace directory name. Passed so apply() can
5305
+ * stamp whatever it persists with the run that produced it, which is the
5306
+ * only link from a stored result back to the inputs and scripts behind it.
5307
+ */
5308
+ runId?: string;
5309
+ /** The run's workspace, for an apply() that needs a file the agent left. */
5310
+ workspaceDir?: string;
5311
+ /** Cancels the run; forwarded to the driver's real abort. */
5312
+ signal?: AbortSignal;
5313
+ /** Live progress. Populated for chat mode; a task run may also stream to a log. */
5314
+ onOutput?: (chunk: string) => void;
5315
+ onEvent?: (event: AgentEvent) => void;
5316
+ onUsage?: (usage: AgentUsage) => void;
5317
+ }
5318
+ /**
5319
+ * How much an agent carries between runs.
5320
+ *
5321
+ * Codex manages its own context: it tracks the window and compacts when it
5322
+ * crosses `auto_compact_token_limit`, emitting a ContextCompacted event. That
5323
+ * happens with no involvement from us, so a persistent session survives
5324
+ * indefinitely on its own. (The mechanism is in the engine but not the
5325
+ * TypeScript SDK — Thread exposes only id/run/runStreamed — which is why there
5326
+ * is nothing here to call.)
5327
+ *
5328
+ * What compaction cannot do is make the session authoritative. It summarises to
5329
+ * make room, so detail is lost by design; and ~/.codex/sessions is a directory
5330
+ * on one machine, not the database. So durable facts belong in `memory/` files
5331
+ * and in the tables the agent writes through apply(), with the session free to
5332
+ * compact itself as it likes. Memory on disk, context managed by codex.
5333
+ */
5334
+ interface SessionPolicy {
5335
+ /**
5336
+ * 'per-run' a fresh harness session each time (default).
5337
+ * 'persistent' resume the same session across runs, so the agent keeps its
5338
+ * working context. Always paired with durable files — never rely
5339
+ * on it alone.
5340
+ */
5341
+ scope?: 'per-run' | 'persistent';
5342
+ /**
5343
+ * 'per-run' a throwaway directory per run (default); pruned by keepRuns.
5344
+ * 'persistent' one directory the agent owns across runs. `memory/` survives
5345
+ * untouched; `input/` is cleared each run so a stale input can
5346
+ * never be mistaken for this run's.
5347
+ */
5348
+ workspace?: 'per-run' | 'persistent';
5349
+ /**
5350
+ * Safety valve, not the compaction mechanism — codex already compacts on its
5351
+ * own. Starts a fresh session after this many runs while keeping the
5352
+ * workspace, for when you want a hard ceiling on how much summarised history
5353
+ * an agent drags along, or a clean slate on a cadence. Omit by default.
5354
+ */
5355
+ resetAfterTurns?: number;
5356
+ }
5357
+ interface CodingAgentDefinition<TInput = unknown, TOutput = unknown> {
5358
+ /** Stable id: the folder name under .prompts/coding-agents, and the key callers use. */
5359
+ id: string;
5360
+ /** Human-readable, for the UI and logs. */
5361
+ title: string;
5362
+ /** One line: what this agent is for. */
5363
+ description?: string;
5364
+ /**
5365
+ * 'task' — headless. Runs from a cron, an event, or a command; produces the
5366
+ * deliverable and applies it. Nobody is watching.
5367
+ * 'chat' — a person is typing. Streams, keeps its session across turns, and
5368
+ * is not required to produce a deliverable at all.
5369
+ */
5370
+ mode: 'task' | 'chat';
5371
+ /**
5372
+ * Directory holding this agent's system.md. Defaults to
5373
+ * `<promptRoot>/<id>`. The runner copies system.md into the workspace as
5374
+ * AGENTS.md, which is the filename the harness actually reads from its
5375
+ * working directory — that indirection is what lets the instructions stay
5376
+ * version-controlled while the workspace stays disposable.
5377
+ */
5378
+ promptDir?: string;
5379
+ /** Override the deployment default for this agent alone. */
5380
+ harness?: Harness;
5381
+ model?: string;
5382
+ /**
5383
+ * Let this agent's shell reach the network. Off by default.
5384
+ *
5385
+ * Needed by any agent given bridge shims (writeBridgeShims), which reach the
5386
+ * backend over localhost HTTP. Note the sandbox has no host allowlist, so
5387
+ * this grants the whole internet, not just the bridge — scope it to agents
5388
+ * that genuinely need it.
5389
+ */
5390
+ networkAccessEnabled?: boolean;
5391
+ /**
5392
+ * Filesystem reach. Defaults to the driver's 'workspace-write'.
5393
+ * 'danger-full-access' removes the sandbox entirely — it is the only mode in
5394
+ * which codex permits MCP tool calls headlessly, which is exactly why the
5395
+ * shims exist instead.
5396
+ */
5397
+ sandboxMode?: 'read-only' | 'workspace-write' | 'danger-full-access';
5398
+ /**
5399
+ * Tools this agent may reach, as MCP servers. Deliberately per-agent: an
5400
+ * agent that reads the knowledge base has no business holding a database
5401
+ * connection. Omit for an agent that only needs the files it is given.
5402
+ */
5403
+ tools?: Record<string, McpServerSpec>;
5404
+ /**
5405
+ * Put the job's inputs in the workspace as FILES, not prompt text. A coding
5406
+ * agent can grep, aggregate and script over a file; it cannot do any of that
5407
+ * with text pasted into its prompt, and a large input would not fit there
5408
+ * anyway.
5409
+ */
5410
+ prepare?(ws: Workspace, input: TInput, ctx: AgentRunContext): Promise<void>;
5411
+ /**
5412
+ * The short framing sent as the turn's prompt. Keep it short — the standing
5413
+ * instructions belong in system.md and the data belongs in files. Defaults
5414
+ * to a generic instruction naming the output path.
5415
+ */
5416
+ buildPrompt?(ws: Workspace, input: TInput, ctx: AgentRunContext): string | Promise<string>;
5417
+ /** Required for 'task' mode; meaningless for 'chat'. */
5418
+ output?: OutputContract<TOutput>;
5419
+ /**
5420
+ * Persist the validated deliverable — through existing services, never raw
5421
+ * SQL from inside the agent. Keeping the write on this side is what makes a
5422
+ * run auditable and replayable: the artifact on disk is the same input
5423
+ * apply() would get again tomorrow.
5424
+ */
5425
+ apply?(result: TOutput, input: TInput, ctx: AgentRunContext): Promise<ApplySummary>;
5426
+ /**
5427
+ * What makes this agent run. Declared here so an agent's folder plus this
5428
+ * file is the complete story of when it fires. See triggers.ts.
5429
+ */
5430
+ trigger?: AgentTrigger | AgentTrigger[];
5431
+ /**
5432
+ * WHO this agent's accumulated state belongs to. Distinct from `mode`, which
5433
+ * is only how a run was triggered — the two were conflated before, so a
5434
+ * project agent someone chatted with silently got a private per-thread
5435
+ * workspace and lost its memory.
5436
+ *
5437
+ * 'project' ONE workspace and session for the whole deployment, whoever
5438
+ * invokes it. A concept builder is project-wide: everything it
5439
+ * learns should accumulate in one place, and a chat turn joins
5440
+ * that same workspace rather than forking a copy. Concurrent
5441
+ * runs serialise.
5442
+ * 'user' one per user, shared across that user's threads.
5443
+ * 'thread' one per conversation — an ordinary chat assistant.
5444
+ *
5445
+ * Default: 'thread' for mode 'chat', 'project' for mode 'task'.
5446
+ */
5447
+ scope?: 'project' | 'user' | 'thread';
5448
+ /**
5449
+ * Kill the turn the moment the deliverable file appears, instead of letting
5450
+ * the agent finish.
5451
+ *
5452
+ * Off by default, and the reason is measured: an agent typically writes its
5453
+ * output and then keeps going — verifying, re-reading, summarising — so
5454
+ * stopping at the write cuts the turn well before codex emits
5455
+ * turn.completed, which is the only event carrying token usage. Early stop
5456
+ * therefore costs you all cost tracking, and skips the agent's own
5457
+ * self-check. Turn it on for a long-tailed agent where the file really is
5458
+ * the last thing that matters.
5459
+ */
5460
+ stopOnDeliverable?: boolean;
5461
+ /** What this agent carries between runs. Default: nothing (fresh session and workspace). */
5462
+ session?: SessionPolicy;
5463
+ /** Hard cap for one run. Default 15 minutes. */
5464
+ timeoutMs?: number;
5465
+ }
5466
+ /** What a completed run reports. */
5467
+ interface AgentRunRecord<TOutput = unknown> {
5468
+ agentId: string;
5469
+ runId: string;
5470
+ /** The run's directory — instructions, inputs, scripts and output all still there. */
5471
+ workspaceDir: string;
5472
+ /**
5473
+ * 'early' the deliverable landed and we stopped the turn — a success, kept
5474
+ * distinct from 'completed' so a masked early exit is visible.
5475
+ * 'skipped' another run of this agent held the lock; nothing was done.
5476
+ */
5477
+ status: 'completed' | 'early' | 'aborted' | 'error' | 'skipped';
5478
+ /** The agent's closing message. */
5479
+ answer: string;
5480
+ ms: number;
5481
+ usage: AgentUsage;
5482
+ /** Present when the agent produced and validated its deliverable. */
5483
+ output?: TOutput;
5484
+ /** Present when apply() ran. */
5485
+ applied?: ApplySummary;
5486
+ error?: string;
5487
+ /** Harness session id, for resuming a chat. */
5488
+ sessionId?: string;
5489
+ }
5490
+
5491
+ /**
5492
+ * The agent registry — one place that knows which agents this deployment has.
5493
+ *
5494
+ * Deliberately a plain module-level map rather than something on the SDK: a
5495
+ * cron job, a CLI command and the websocket handler all need to reach the same
5496
+ * agents, and only one of those has an SDK instance in hand.
5497
+ */
5498
+ interface CodingAgentsConfig {
5499
+ /**
5500
+ * Where per-agent instruction folders live. Each agent reads
5501
+ * `<promptRoot>/<agent.id>/system.md`.
5502
+ * Default: `<cwd>/.prompts/coding-agents`, matching the .prompts convention
5503
+ * the rest of the deployment already uses.
5504
+ */
5505
+ promptRoot?: string;
5506
+ /**
5507
+ * Root for run workspaces: `<runtimeDir>/<agentId>/<runId>/`. Regenerated
5508
+ * per run and safe to gitignore — deliberately not inside promptRoot, which
5509
+ * is version-controlled.
5510
+ * Default: `<cwd>/.coding-agent-runtime`.
5511
+ */
5512
+ runtimeDir?: string;
5513
+ /** Fallback harness for agents that don't name one. */
5514
+ harness?: Harness;
5515
+ /** Fallback model. */
5516
+ model?: string;
5517
+ /** Tools every agent gets, merged under each agent's own (the agent's win on a name clash). */
5518
+ sharedTools?: Record<string, McpServerSpec>;
5519
+ /** Keep this many run directories per agent; older ones are pruned. 0 keeps everything. */
5520
+ keepRuns?: number;
5521
+ }
5522
+ /**
5523
+ * Register this deployment's agents. Call once at startup.
5524
+ *
5525
+ * Re-registering an id replaces it — that keeps hot-reload working in dev and
5526
+ * makes a double registration a no-op rather than a silent duplicate.
5527
+ */
5528
+ declare function registerCodingAgents(definitions: CodingAgentDefinition<any, any>[], options?: CodingAgentsConfig): void;
5529
+ declare function getCodingAgent(id: string): CodingAgentDefinition<any, any> | undefined;
5530
+ declare function listCodingAgents(): Array<{
5531
+ id: string;
5532
+ title: string;
5533
+ description?: string;
5534
+ mode: 'task' | 'chat';
5535
+ }>;
5536
+ declare function getCodingAgentsConfig(): Required<Pick<CodingAgentsConfig, "harness" | "promptRoot" | "runtimeDir" | "keepRuns">> & CodingAgentsConfig;
5537
+
5538
+ interface RunAgentOptions<TInput> extends AgentRunContext {
5539
+ input?: TInput;
5540
+ /** Scopes a 'thread' agent. Ignored by a project-scoped one. */
5541
+ threadId?: string;
5542
+ /**
5543
+ * Escape hatch that pins the workspace directory outright. Almost never
5544
+ * right — it defeats the agent's own scope, which is how a project agent
5545
+ * ends up with a private copy per caller. Left for tests and one-offs.
5546
+ */
5547
+ workspaceDir?: string;
5548
+ /** Resume the harness session — chat continuity across turns and restarts. */
5549
+ resumeId?: string;
5550
+ /** Overrides the definition's prompt entirely. Chat mode: this is the user's message. */
5551
+ prompt?: string;
5552
+ /**
5553
+ * A person is typing, rather than a job running.
5554
+ *
5555
+ * For an agent whose mode is 'task' this turns off the JOB — prepare(), the
5556
+ * deliverable contract and apply() are one unit that sets work up and
5557
+ * consumes it, and a conversation should do neither. The turn joins the
5558
+ * workspace as it stands and its answer is the whole result.
5559
+ *
5560
+ * Without this, chatting with a task agent could only ever fail: the user's
5561
+ * message replaces the prompt that names the output path, so the agent never
5562
+ * writes the deliverable, and the run is then reported as an error despite
5563
+ * having answered perfectly well. Worse, prepare() ran on every turn — for a
5564
+ * batching agent that means a chat turn quietly consumed a batch of work.
5565
+ *
5566
+ * An agent whose mode is 'chat' is unaffected: its prepare() is per-turn
5567
+ * context setup, not job setup, so it still runs.
5568
+ */
5569
+ chat?: boolean;
5570
+ }
5571
+ /**
5572
+ * Run one agent, end to end.
5573
+ *
5574
+ * Never throws for an agent-side failure — a bad run comes back as a record
5575
+ * with status 'error', because callers are usually crons and message handlers
5576
+ * that must keep going. Genuinely programmer errors (unknown agent id) do throw.
5577
+ */
5578
+ declare function runCodingAgent<TInput = unknown, TOutput = unknown>(agentId: string, options?: RunAgentOptions<TInput>): Promise<AgentRunRecord<TOutput>>;
5579
+
5580
+ /**
5581
+ * One shim that was written, and the one line describing it.
5582
+ *
5583
+ * Returned rather than left for the caller to infer, because the caller's only
5584
+ * other option is to scan the directory — which yields names and nothing else.
5585
+ * A separate filename→description table would drift the moment a shim changed,
5586
+ * and a stale description is worse than none: it is authoritative and wrong.
5587
+ * The summary is written beside the shim body it describes, so the two are
5588
+ * edited in one place.
5589
+ */
5590
+ interface ShimSummary {
5591
+ /** The executable's name, as the agent invokes it: `./sources`, `./query`. */
5592
+ name: string;
5593
+ /** What it does and when to reach for it. One line — usage lives in the shim's own `--help` path. */
5594
+ summary: string;
5595
+ }
5596
+ /**
5597
+ * Write `./sources`, `./query` and (when the caller offers any) `./run-tool`
5598
+ * into an agent's workspace: data access as plain executables it runs with
5599
+ * bash.
5600
+ *
5601
+ * Why not MCP: codex gates MCP tool calls on the sandbox. Under any sandboxed
5602
+ * mode they need approval, and a headless run must set approvalPolicy 'never',
5603
+ * which denies rather than grants — measured, not assumed. Only
5604
+ * danger-full-access permits them, and that removes the filesystem sandbox
5605
+ * entirely. Bash commands under workspace-write need no approval, so a shim
5606
+ * buys the same capability while the agent stays confined to its workspace.
5607
+ *
5608
+ * An MCP server for this was written and then removed: the same approval gate
5609
+ * applies to the Agents chat tab, which is equally headless from codex's point
5610
+ * of view, so it was unreachable everywhere. `mcpServers` stays plumbed through
5611
+ * the driver for third-party servers; see git history for the removed one.
5612
+ *
5613
+ * The cost is structured tool events: calls arrive as `bash → ./query ...`
5614
+ * rather than typed items with parsed arguments. That matters for a chat UI
5615
+ * with a human watching; for a headless task agent it does not.
5616
+ *
5617
+ * Requires networkAccessEnabled on the session — the shim reaches the bridge
5618
+ * over HTTP, and that is the whole point of it.
5619
+ */
5620
+ declare function writeBridgeShims(ws: Workspace, opts: {
5621
+ bridgeUrl: string;
5622
+ /** `searchable: true` marks a source ./schema-search actually works against — see schemaSearchFn below. */
5623
+ sources: Array<{
5624
+ id: string;
5625
+ name?: string;
5626
+ description?: string;
5627
+ searchable?: boolean;
5628
+ }>;
5629
+ /**
5630
+ * Pre-built business-logic tools (e.g. a business-concept catalog) exposed
5631
+ * as ./run-tool, distinct from ./query's raw SQL. Omit for an agent that
5632
+ * should only ever read data, never invoke one of these directly.
5633
+ */
5634
+ directTools?: Array<{
5635
+ id: string;
5636
+ name?: string;
5637
+ description?: string;
5638
+ params?: Record<string, string>;
5639
+ }>;
5640
+ /** Entity types ./resolve-entities can resolve names against (e.g. "customer", "project"). Omit for an agent that has no entity-search index to reach. */
5641
+ entityTypes?: string[];
5642
+ /** Every verified concept — name, aliases and what it is for — as ./concept-catalog returns it. Omit for an agent that has no concept store to reach. */
5643
+ conceptCatalog?: Array<{
5644
+ name: string;
5645
+ kind?: string;
5646
+ aliases?: string[];
5647
+ description?: string | null;
5648
+ }>;
5649
+ /** Whether ./knowledge-base is offered. Omit for an agent that has no knowledge base to reach. */
5650
+ knowledgeBase?: boolean;
5651
+ maxRows?: number;
5652
+ }): ShimSummary[];
5653
+
5654
+ /**
5655
+ * Read-only SQL guard for any path that hands an agent a database.
5656
+ *
5657
+ * Defence in depth, NOT a boundary. The agent has a shell and can reach the
5658
+ * bridge directly, so anything enforced only here is advisory — it stops
5659
+ * mistakes, not determination. The real boundary is a database user without
5660
+ * write permission; this exists so a confused agent gets a clear correction
5661
+ * instead of a silent partial write.
5662
+ */
5663
+ /** Throws with a message written for the agent to act on. */
5664
+ declare function assertReadOnlySql(sql: string): void;
5665
+
5666
+ /**
5667
+ * The 'coding-agent'.'run' collection — a chat turn against a coding agent.
5668
+ *
5669
+ * Generic on purpose: nothing here knows about dashboards, reports, or any
5670
+ * particular deliverable. It owns what every harness-backed chat needs — one
5671
+ * session per thread, a per-thread workspace, a run lock, and forwarding the
5672
+ * driver's callbacks to the transport. Task-specific flows (prompt assembly, a
5673
+ * draft-file contract, persistence) build on top of this rather than inside it.
5674
+ */
5675
+ interface CodingAgentCollectionConfig {
5676
+ /**
5677
+ * Root for per-thread agent workspaces. Each thread gets its own directory
5678
+ * underneath, so one chat can't read or clobber another's files, and none of
5679
+ * them sit inside the backend's own source tree.
5680
+ * Default: `<process.cwd()>/.coding-agent-runtime`.
5681
+ */
5682
+ runtimeDir?: string;
5683
+ /** Default harness when the request doesn't name one. */
5684
+ harness?: Harness;
5685
+ model?: string;
5686
+ /** Tools, as MCP servers, handed to every session. */
5687
+ mcpServers?: Record<string, McpServerSpec>;
5688
+ }
5689
+ declare function registerCodingAgentCollection(sdk: SuperatomSDK, config?: CodingAgentCollectionConfig): void;
5690
+
5691
+ declare class ScriptMatcher {
5692
+ private store;
5693
+ private collections?;
5694
+ private userId?;
5695
+ private trace?;
5696
+ constructor(store: ScriptStore, opts?: {
5697
+ collections?: any;
5698
+ userId?: string;
5699
+ trace?: RunTrace;
5700
+ });
5701
+ /**
5702
+ * Find the best matching script for a user question.
5703
+ * Uses ONE LLM call that picks the script AND extracts parameters.
5704
+ * Returns null if no script matches.
5705
+ *
5706
+ * Thin wrapper around `doMatch` whose only job is guaranteeing the trace
5707
+ * capture happens exactly once, on every exit path (short-circuit, normal
5708
+ * return, or the LLM call erroring) — see RunTrace.scriptMatcher.
5709
+ */
5710
+ match(userPrompt: string, apiKey?: string, model?: string, signal?: AbortSignal, activeBindings?: ScriptBinding[]): Promise<ScriptMatch | null>;
5711
+ private doMatch;
5712
+ /**
5713
+ * Renders a recipe's declared parameters the same way in every prompt
5714
+ * section (catalog, active-script, edit candidates) so the LLM sees one
5715
+ * consistent format regardless of which block a script came from.
5716
+ */
5717
+ private formatParamList;
5718
+ /**
5719
+ * Semantic search over THIS user's own past prompts (`conversation-history`
5720
+ * ChromaDB embeddings, already user-scoped) — not a project-wide name/tag
5721
+ * search. Groups hits by the recipe that answered them, so a script gets
5722
+ * ONE catalog entry carrying every matched past question, not one per hit.
5723
+ *
5724
+ * Scope is deliberate (see SCRIPT-MATCH-IMPROVEMENTS.md, Change 2): a
5725
+ * script only shows up here because THIS user has, before, asked something
5726
+ * similar that it answered — not because some other user's question did.
5727
+ */
5728
+ private searchPromptHistory;
5729
+ /**
5730
+ * Build the script catalog string for the LLM prompt from prompt-history
5731
+ * matches: each script's declared parameters PLUS the actual past questions
5732
+ * of this user's that it answered (real precedent, not a hand-written
5733
+ * description) and what params they ran with.
5734
+ */
5735
+ private buildPromptHistoryCatalog;
5736
+ /**
5737
+ * The most recent script-backed answer in this thread — the one an `edit`
5738
+ * targets. Rendered as its own prompt section (never merged into the ranked
5739
+ * catalog) so the `edit` rules have an unambiguous referent.
5740
+ *
5741
+ * `candidates` holds at most one entry (see doMatch) — `edit` always means
5742
+ * "the answer I just got", so there is nothing else to show here.
5743
+ *
5744
+ * Carries the QUESTION that produced it plus the columns it returned — that
5745
+ * is what lets the matcher resolve "use the mode for the WSP one" against
5746
+ * this one script's own context.
5747
+ */
5748
+ private buildActiveScriptBlock;
5749
+ }
5750
+
5751
+ /**
5752
+ * ScriptRunner — Execute scripts in an isolated tsx subprocess.
5753
+ *
5754
+ * The subprocess approach replaces the earlier `new Function()` eval and gives us:
5755
+ * - Real sandbox (separate process, SIGKILL on timeout).
5756
+ * - Real TypeScript (tsx transpiles on the fly).
5757
+ * - npm imports available to scripts (clustering, stats, geo, etc.).
5758
+ *
5759
+ * Protocol: NDJSON over the child's stdin/stdout. See script-ipc.ts + backend/docs/SCRIPT-FLOW-IMPLEMENTATION.md.
5760
+ */
5761
+
5762
+ interface RunScriptOptions {
5763
+ /** Data sources the script is allowed to query via ctx.query */
5764
+ externalTools: ExternalTool[];
5765
+ /** Optional — for propagating per-query UI progress to the user */
5766
+ streamBuffer?: StreamBuffer;
5767
+ /** Override the wall-clock timeout (default `SCRIPT_TIMEOUT_MS`, 60s). */
5768
+ timeoutMs?: number;
5769
+ /**
5770
+ * Per-turn cancellation signal. When the user hits "Stop" mid-run, the child
5771
+ * process group is SIGKILLed and the run resolves as an aborted failure (the
5772
+ * caller is already unwinding, so the result is discarded).
5773
+ */
5774
+ signal?: AbortSignal;
5775
+ /**
5776
+ * Names resolved for this run, handed to getAnalysis/getComponents as their
5777
+ * 5th argument. Supplying these is what stops "the narrative needs the
5778
+ * subsidiary's name" from being a reason to rewrite getData and re-query.
5779
+ */
5780
+ entities?: ResolvedEntityRef[];
5781
+ }
5782
+ /**
5783
+ * Execute a recipe by spawning a tsx child on the script's .ts file.
5784
+ * `scriptPath` is the absolute path to the saved `.ts` body.
5785
+ */
5786
+ declare function runScript(recipe: ScriptRecipe, scriptPath: string, params: Record<string, any>, options: RunScriptOptions): Promise<ScriptResult>;
5787
+
3227
5788
  type MessageTypeHandler = (message: IncomingMessage) => void | Promise<void>;
3228
5789
  declare class SuperatomSDK {
3229
5790
  private ws;
@@ -3259,6 +5820,8 @@ declare class SuperatomSDK {
3259
5820
  private lastPong;
3260
5821
  private readonly PING_INTERVAL_MS;
3261
5822
  private readonly PONG_TIMEOUT_MS;
5823
+ private pendingOutbox;
5824
+ private readonly MAX_OUTBOX_SIZE;
3262
5825
  constructor(config: SuperatomSDKConfig);
3263
5826
  /**
3264
5827
  * Initialize PromptLoader and load prompts into memory
@@ -3303,6 +5866,20 @@ declare class SuperatomSDK {
3303
5866
  * Does NOT throw on closed connections — callers can check the return value if needed.
3304
5867
  */
3305
5868
  send(message: Message): boolean;
5869
+ /**
5870
+ * Queue a message that couldn't be delivered because the socket was down,
5871
+ * to be resent once it reconnects. Drops the oldest entry once the bound is
5872
+ * hit — an outage long enough to fill this queue means the oldest queued
5873
+ * responses are for requests the caller has likely already given up on.
5874
+ */
5875
+ private queuePendingMessage;
5876
+ /**
5877
+ * Resend everything queued while the socket was down, now that it's back
5878
+ * up. Uses this.ws.send() directly (not send()) so a message that fails
5879
+ * again goes back through queuePendingMessage() rather than being silently
5880
+ * dropped a second time.
5881
+ */
5882
+ private flushPendingOutbox;
3306
5883
  /**
3307
5884
  * Register a message handler to receive all messages
3308
5885
  */
@@ -3342,6 +5919,26 @@ declare class SuperatomSDK {
3342
5919
  */
3343
5920
  private handlePong;
3344
5921
  private storeComponents;
5922
+ /**
5923
+ * The live, frontend-registered component catalog (name, type, description,
5924
+ * and full prop schema per component) — the same authoritative source
5925
+ * DASH_COMP_REQ's LLM prompt is built from. Exposed so other integrations
5926
+ * (e.g. the dashboard-agent script bridge) can read real component
5927
+ * contracts instead of maintaining a separate, driftable hand-written copy.
5928
+ */
5929
+ getComponents(): Component[];
5930
+ /**
5931
+ * The dashboard contract, as registered by the browser bundle that holds it.
5932
+ *
5933
+ * Empty until a client has connected and pushed it. That is a real state and
5934
+ * callers must handle it rather than treat it as an empty contract: an empty
5935
+ * list means "nobody has told us yet", not "there are no components", and
5936
+ * building against it would produce a dashboard that can import nothing.
5937
+ */
5938
+ private dashContract;
5939
+ private storeDashContract;
5940
+ /** The registered dashboard contract. Empty means "not registered yet". */
5941
+ getDashContract(): DashComponent[];
3345
5942
  /**
3346
5943
  * Set tools for the SDK instance
3347
5944
  */
@@ -3350,6 +5947,16 @@ declare class SuperatomSDK {
3350
5947
  * Get the stored tools
3351
5948
  */
3352
5949
  getTools(): Tool$1[];
5950
+ /**
5951
+ * Call a registered collection operation in-process — no WebSocket
5952
+ * round-trip, since the caller is already running inside this same SDK
5953
+ * instance. Lets SDK-internal features (e.g. the dashboard agent) reuse
5954
+ * whatever collection a deployment has already registered (e.g.
5955
+ * 'dashboards'.'query') by name/convention, instead of requiring a
5956
+ * separate callback purely to re-expose data a collection already serves.
5957
+ * Throws if the collection or operation isn't registered.
5958
+ */
5959
+ callCollection<TResult = any>(collectionName: string, operation: string, params?: any): Promise<TResult>;
3353
5960
  /**
3354
5961
  * Register workflow components for the SDK instance.
3355
5962
  *
@@ -3393,4 +6000,4 @@ declare class SuperatomSDK {
3393
6000
  getConversationSimilarityThreshold(): number;
3394
6001
  }
3395
6002
 
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 };
6003
+ export { type AgentConfig, type AgentEvent, type AgentResponse, type AgentRunContext, type AgentRunHandlers, type AgentRunRecord, type AgentRunResult, type AgentTrigger, type AgentUsage, type AnalyticsEventType, type AnalyticsStatus, 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 PiSessionListener, type PiSessionState, type PiTranscriptEntry, type RerankedResult, type RunAgentOptions, STORAGE_CONFIG, type ScriptComponentSpec, ScriptMatcher, type ScriptParameter, type ScriptRecipe, type ScriptRecipeMetaRow, type ScriptRecipeStore, type ScriptResult, ScriptStore, type ScriptStoreOptions, type SelectedWorkflow, type ShimSummary, 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, analyticsClient, anthropicLLM, assertReadOnlySql, createCodexSession, createCodingAgentSession, dashboardConversationHistory, dataFingerprint, describeTriggers, emitAgentEvent, extractNamedExport, geminiLLM, getCodingAgent, getCodingAgentsConfig, getPiSessionState, groqLLM, hybridRerank, listCodingAgents, llmUsageLogger, logger, normalizeScriptBody, onAgentRun, onPiSessionUpdate, openaiLLM, promptLoader, queryCache, registerCodingAgentCollection, registerCodingAgents, registerDashboardAgentCollection, rerankChromaResults, rerankConversationResults, resolveScriptRecipeStore, runCodingAgent, runScript, startAgentTriggers, stopAgentTriggers, userPromptErrorLogger, writeBridgeShims };