@superatomai/sdk-node 0.0.38 → 0.0.39-dsp

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -1,15 +1,916 @@
1
1
  import { z } from 'zod';
2
2
  import Anthropic from '@anthropic-ai/sdk';
3
3
 
4
+ /**
5
+ * StreamBuffer - Buffered streaming utility for smoother text delivery
6
+ * Batches small chunks together and flushes at regular intervals
7
+ */
8
+ type StreamCallback = (chunk: string) => void;
9
+ /**
10
+ * StreamBuffer class for managing buffered streaming output
11
+ * Provides smooth text delivery by batching small chunks
12
+ */
13
+ declare class StreamBuffer {
14
+ private buffer;
15
+ private flushTimer;
16
+ private callback;
17
+ private fullText;
18
+ constructor(callback?: StreamCallback);
19
+ /**
20
+ * Check if the buffer has a callback configured
21
+ */
22
+ hasCallback(): boolean;
23
+ /**
24
+ * Get all text that has been written (including already flushed)
25
+ */
26
+ getFullText(): string;
27
+ /**
28
+ * Write a chunk to the buffer
29
+ * Large chunks or chunks with newlines are flushed immediately
30
+ * Small chunks are batched and flushed after a short interval
31
+ *
32
+ * @param chunk - Text chunk to write
33
+ */
34
+ write(chunk: string): void;
35
+ /**
36
+ * Flush the buffer immediately
37
+ * Call this before tool execution or other operations that need clean output
38
+ */
39
+ flush(): void;
40
+ /**
41
+ * Internal flush implementation
42
+ */
43
+ private flushNow;
44
+ /**
45
+ * Clean up resources
46
+ * Call this when done with the buffer
47
+ */
48
+ dispose(): void;
49
+ }
50
+
51
+ /**
52
+ * ToolExecutorService - Handles execution of SQL queries and external tools
53
+ * Extracted from BaseLLM.generateTextResponse for better separation of concerns
54
+ */
55
+
56
+ /**
57
+ * External tool definition
58
+ */
59
+ interface ExternalTool {
60
+ id: string;
61
+ name: string;
62
+ description?: string;
63
+ /** Tool type: "source" = routed through SourceAgent, "direct" = called directly by MainAgent */
64
+ toolType?: 'source' | 'direct';
65
+ /** Full untruncated schema for source agent (all columns visible) */
66
+ fullSchema?: string;
67
+ /** Schema size tier: small (≤50 tables), medium (51-200), large (201-500), very_large (500+) */
68
+ schemaTier?: string;
69
+ /** Schema search function for very_large tier — keyword search over entities */
70
+ schemaSearchFn?: (keywords: string[]) => string;
71
+ /** `signal` fires when the user hits Stop mid-query — passed through to the backend's SourceTool.fn. */
72
+ fn: (input: any, signal?: AbortSignal) => Promise<any>;
73
+ limit?: number;
74
+ outputSchema?: any;
75
+ executionType?: 'immediate' | 'deferred';
76
+ userProvidedData?: any;
77
+ params?: Record<string, any>;
78
+ }
79
+ /**
80
+ * Executed tool tracking info
81
+ */
82
+ interface ExecutedToolInfo {
83
+ id: string;
84
+ name: string;
85
+ params: any;
86
+ result: {
87
+ _totalRecords: number;
88
+ _recordsShown: number;
89
+ _metadata?: any;
90
+ _sampleData: any[];
91
+ /** Bounded summary over the FULL fetched result (complete structure). */
92
+ _summary?: any;
93
+ /** Up to MAIN_AGENT_COMPLETE_ROWS rows — the complete result when small. */
94
+ _mainAgentRows?: any[];
95
+ };
96
+ outputSchema?: any;
97
+ sourceSchema?: string;
98
+ sourceType?: string;
99
+ }
100
+
101
+ /**
102
+ * Script Flow Types
103
+ *
104
+ * Defines interfaces for the script-based query architecture:
105
+ * - ScriptRecipe: metadata for matching, validation, and quality tracking
106
+ * - ScriptResult: output from executing a script
107
+ * - ScriptMatch: result from the LLM-based script matcher
108
+ */
109
+ /**
110
+ * Recipe metadata stored alongside each script.
111
+ * Used for matching, validation, and quality tracking.
112
+ */
113
+ interface ScriptRecipe {
114
+ /** Unique script identifier */
115
+ id: string;
116
+ /** Version number (incremented on regeneration) */
117
+ version: number;
118
+ /** Human-readable name (e.g., "Revenue by Dimension") */
119
+ name: string;
120
+ /** Natural language description of what this script does */
121
+ intentDescription: string;
122
+ /** Keyword tags for quick filtering */
123
+ tags: string[];
124
+ /** Source tool IDs this script queries (e.g., ["mssql-abc123_query"]) */
125
+ sourceIds: string[];
126
+ /** Table names used (for future schema drift detection) */
127
+ tables: string[];
128
+ /** Parameter definitions — what can vary */
129
+ parameters: ScriptParameter[];
130
+ /** The script function body as a string. Loaded from disk (scripts-store/<fileBase>.ts). */
131
+ scriptBody: string;
132
+ /**
133
+ * On-disk filename stem for the body: scripts-store/<fileBase>.ts.
134
+ * Editable in the IDE. Decided at authoring time (slug of `name`, with a
135
+ * short id suffix on collision) and stable across promotion.
136
+ */
137
+ fileBase?: string;
138
+ /** sha256 of the on-disk body — lets the runtime detect manual edits. */
139
+ bodyHash?: string;
140
+ /** Project scope (single-VM deployments may leave this undefined). */
141
+ projectId?: string;
142
+ /** Times this script was used successfully */
143
+ successCount: number;
144
+ /** Times this script failed */
145
+ failureCount: number;
146
+ /** ISO timestamp of last usage */
147
+ lastUsed: string;
148
+ /** Original user question that created this script */
149
+ createdFrom: string;
150
+ /** ISO timestamp */
151
+ createdAt: string;
152
+ /** ISO timestamp */
153
+ updatedAt: string;
154
+ /**
155
+ * `recipe.id` of the parent this script was forked from.
156
+ * Undefined for root scripts (those written from scratch by MainAgent).
157
+ * See backend/docs/SCRIPT-FLOW-FORK-ADAPT.md.
158
+ */
159
+ parentId?: string;
160
+ /** 0 for root scripts; `parent.forkDepth + 1` for forks. Capped at 3. */
161
+ forkDepth?: number;
162
+ /**
163
+ * Brief description of what this fork changed vs its parent
164
+ * (sourced from the matcher's `modificationHint`).
165
+ */
166
+ forkReason?: string;
167
+ /**
168
+ * Validated component specs captured at authoring time. On a tier-high
169
+ * replay these are rebound to fresh queryIds deterministically — no
170
+ * component-generation LLM call, and the rendered columns can't drift from
171
+ * what was validated when the script was authored. Absent on recipes
172
+ * authored before this landed; those fall back to LLM component generation.
173
+ * See backend/docs/SCRIPT-COMPONENT-CONSISTENCY.md.
174
+ */
175
+ components?: ScriptComponentSpec[];
176
+ /**
177
+ * What the user explicitly asked the output to LOOK like, set by a display
178
+ * edit ("show that as a bar chart", "make the bars horizontal").
179
+ *
180
+ * Deliberately SEPARATE from `components`. Those are validated bindings that
181
+ * must be cleared on a data edit — after "break it down by brand" the axis
182
+ * keys are wrong, and after "use the mode" the stored title still says
183
+ * "Average…". A rendering CHOICE, by contrast, is still valid afterwards.
184
+ * Keeping them in one field meant every data edit silently discarded the
185
+ * user's chart type.
186
+ *
187
+ * Fed to the component generator as a hint whenever specs are (re)generated,
188
+ * so the chosen rendering comes back even after the SQL changes.
189
+ */
190
+ displayPreference?: ScriptDisplayPreference;
191
+ /**
192
+ * Lifecycle stage of this recipe on disk.
193
+ * - 'draft': written by MainAgent's write_script during a turn; filtered out
194
+ * of FTS results (status='verified' only) so the matcher never picks it.
195
+ * Filename is suffixed with `turnId` to keep concurrent turns
196
+ * from clobbering each other's drafts.
197
+ * - 'verified': promoted after `execute_script` succeeded; the matcher sees it.
198
+ * Filename drops the turn suffix unless a verified file with
199
+ * the same slug already exists (collision case keeps the suffix).
200
+ *
201
+ * Recipes loaded from disk without this field default to 'verified' so
202
+ * existing scripts keep working unchanged.
203
+ */
204
+ status?: 'draft' | 'verified';
205
+ /**
206
+ * Per-turn unique suffix used for draft filenames (e.g. `1714745623-x9k2`).
207
+ * Set when the draft is saved; carried until the recipe is promoted.
208
+ */
209
+ turnId?: string;
210
+ /**
211
+ * Last execution error captured by `recordDraftError` while the recipe was
212
+ * still a draft. Lets users open the draft .json file and see why it failed
213
+ * without grepping logs. Cleared on promotion to 'verified'.
214
+ */
215
+ lastError?: {
216
+ phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
217
+ message: string;
218
+ at: string;
219
+ attempt: number;
220
+ };
221
+ /** userId who committed the most recent edit (audit trail — recipes are project-scoped). */
222
+ editedBy?: string;
223
+ /** The instruction that produced the current version. */
224
+ editedFrom?: string;
225
+ /**
226
+ * Prior versions, newest last. The body itself is archived on disk as
227
+ * `<fileBase>.v<version>.ts`; this records what changed and who did it.
228
+ */
229
+ history?: ScriptVersionRecord[];
230
+ }
231
+ /**
232
+ * A durable, user-stated rendering choice for a recipe. Survives data edits.
233
+ */
234
+ interface ScriptDisplayPreference {
235
+ /** Component types the user's chosen rendering resolved to, e.g. ["DynamicBarChart"]. */
236
+ componentTypes: string[];
237
+ /** The instruction itself — carries nuance the types can't ("horizontal", "sorted descending"). */
238
+ instruction: string;
239
+ /** ISO timestamp of the display edit that set this. */
240
+ at: string;
241
+ }
242
+ /** One superseded version of a recipe body (see ScriptStore.commitEdit). */
243
+ interface ScriptVersionRecord {
244
+ /** Version number this record superseded (i.e. the OLD version). */
245
+ version: number;
246
+ /** ISO timestamp of the edit that superseded it. */
247
+ at: string;
248
+ /** userId who made the edit, when known. */
249
+ by?: string;
250
+ /** The edit instruction that caused the supersede. */
251
+ instruction?: string;
252
+ /** One-line summary of what changed, for the version picker. */
253
+ changeSummary?: string;
254
+ /** sha256 of the superseded body — pairs with `<fileBase>.v<version>.ts`. */
255
+ bodyHash?: string;
256
+ }
257
+ interface ScriptParameter {
258
+ /** Parameter name (used in script body as params.name) */
259
+ name: string;
260
+ /** Parameter type */
261
+ type: 'string' | 'number' | 'date' | 'date_range' | 'enum' | 'boolean';
262
+ /** Whether this parameter is required */
263
+ required: boolean;
264
+ /** Default value if not provided */
265
+ default?: any;
266
+ /** For enum type — maps user-facing values to internal values */
267
+ enumValues?: Record<string, string>;
268
+ /** Human-readable description (used in the matcher LLM prompt) */
269
+ description: string;
270
+ }
271
+ /**
272
+ * A reusable component binding captured when a script is authored. Stored on
273
+ * the recipe so tier-high replays rebuild components deterministically (rebind
274
+ * to fresh queryIds) instead of re-running the component-picker LLM.
275
+ */
276
+ interface ScriptComponentSpec {
277
+ /** Registered component name (e.g. "DynamicBarChart") — matched against the available component library. */
278
+ componentType: string;
279
+ /** `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). */
280
+ sourceRef: string;
281
+ /** Present only when sourceRef === 'federation' — the DuckDB SQL to re-execute on replay. */
282
+ federationSql?: string;
283
+ /** Present only when sourceRef === 'markdown' — the narrative text to render on replay (markdown has no data source, so its content must be persisted). */
284
+ content?: string;
285
+ title?: string;
286
+ description?: string;
287
+ /** Validated axis/value keys + aggregation — all referencing real columns of the bound source. */
288
+ config: Record<string, any>;
289
+ }
290
+ /**
291
+ * Result from executing a script via ScriptRunner.
292
+ */
293
+ interface ScriptResult {
294
+ /** Whether the script executed successfully */
295
+ success: boolean;
296
+ /** Combined data from all queries */
297
+ data: any[];
298
+ /** Individual query results tracked during execution */
299
+ executedQueries: ScriptQueryResult[];
300
+ /** Error message if failed */
301
+ error?: string;
302
+ /**
303
+ * Where in the lifecycle the error occurred. Lets MainAgent's fix-loop
304
+ * decide between "rewrite the whole draft" (compile) and "patch the
305
+ * specific line" (runtime).
306
+ */
307
+ errorPhase?: 'compile' | 'runtime' | 'timeout' | 'ipc';
308
+ /** Total execution time in milliseconds */
309
+ executionTimeMs: number;
310
+ }
311
+ /**
312
+ * A single query executed during script runtime.
313
+ * Tracked by ScriptContext for component generation and debugging.
314
+ */
315
+ interface ScriptQueryResult {
316
+ /** Source tool ID */
317
+ sourceId: string;
318
+ /** Human-readable source name */
319
+ sourceName: string;
320
+ /** The SQL that was executed */
321
+ sql: string;
322
+ /** Result data rows */
323
+ data: any[];
324
+ /** Number of rows returned */
325
+ count: number;
326
+ /** Total rows that matched before limit (if available) */
327
+ totalCount?: number;
328
+ /** Query execution time in milliseconds */
329
+ executionTimeMs: number;
330
+ /**
331
+ * True for rows that did NOT come from a real SQL execution — either a
332
+ * ctx.emit() dataset or the synthesized "computed:_final" entry that
333
+ * carries the script's post-JS returned data. The component generator
334
+ * uses this to route the resulting component through the script_dataset
335
+ * sentinel toolId so the frontend resolves it via the queryCache short-circuit.
336
+ */
337
+ virtual?: boolean;
338
+ }
339
+ /**
340
+ * Match tier returned by the LLM script matcher.
341
+ *
342
+ * - 'high': the script answers the question directly; only parameter values
343
+ * may differ. The runtime replays it with extracted params (cheapest path).
344
+ * - 'near': the script answers a STRUCTURALLY similar question but needs
345
+ * body modification (different metric, dimension, table, filter shape).
346
+ * The runtime forks the parent and adapts the body via MainAgent's normal
347
+ * write_script + execute_script loop — no SourceAgent dispatch needed.
348
+ * See backend/docs/SCRIPT-FLOW-FORK-ADAPT.md for the full design.
349
+ * - 'edit': the user is INSTRUCTING a change to the script that produced the
350
+ * previous answer (not asking a new question). Only reachable when the turn
351
+ * carries a ScriptBinding — without one the matcher coerces it to 'none', so
352
+ * a prompt regression can't turn this into a loose similarity path. The
353
+ * runtime runs MainAgent in edit mode and commits the result in place.
354
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md.
355
+ * - 'none': no script is relevant; full agent flow runs.
356
+ */
357
+ type MatchTier = 'high' | 'near' | 'edit' | 'none';
358
+ /**
359
+ * Which recipe produced the answer the user is currently looking at.
360
+ *
361
+ * Set whenever a turn's answer came from a script (replay, fresh authoring, or
362
+ * a committed edit) and carried on the UIBlock + saved conversation row. Two
363
+ * consumers:
364
+ * 1. The matcher — the 'edit' tier is ONLY reachable when a binding exists, so
365
+ * "use mode instead of mean" resolves to a concrete script instead of being
366
+ * matched on keywords it shares with no script name.
367
+ * 2. Cache invalidation — after an edit commits, conversations bound to that
368
+ * recipeId must be dropped, or the exact-match cache replays the pre-edit
369
+ * answer and the edit looks like a no-op.
370
+ *
371
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 1.
372
+ */
373
+ interface ScriptBinding {
374
+ recipeId: string;
375
+ /** Params the script ran with — the edit's starting point. */
376
+ params: Record<string, any>;
377
+ /** Recipe name at bind time (matcher catalog + user-facing confirmation). */
378
+ name: string;
379
+ /** Columns the last run returned — grounds the editor without a re-query. */
380
+ columns?: string[];
381
+ /**
382
+ * The question that produced this answer. Required for disambiguation when a
383
+ * thread ran several scripts — without it the candidates are just names and
384
+ * the matcher cannot resolve "use the mode for the WSP one".
385
+ */
386
+ userPrompt?: string;
387
+ }
388
+ /**
389
+ * Result from the LLM-based script matcher.
390
+ *
391
+ * For `tier: 'high'`, `extractedParams` carries the values to pass to the
392
+ * existing script. For `tier: 'near'`, `gaps` and `modificationHint` describe
393
+ * what the fork-author needs to change in the parent body.
394
+ */
395
+ interface ScriptMatch {
396
+ /** The matched script recipe */
397
+ recipe: ScriptRecipe;
398
+ /** Match tier — see MatchTier docs */
399
+ tier: MatchTier;
400
+ /** Similarity score (0-1, derived from LLM tier) */
401
+ similarity: number;
402
+ /**
403
+ * Legacy confidence level. Mirrors `tier === 'high'`/`'near'` for now;
404
+ * kept so existing callers compile while we migrate to tier-based logic.
405
+ */
406
+ confidence: 'high' | 'medium';
407
+ /** Parameters extracted from the user question by the LLM (tier='high') */
408
+ extractedParams?: Record<string, any>;
409
+ /**
410
+ * Proper nouns the question named, VERBATIM, with the parameter each fills.
411
+ *
412
+ * The matcher must NOT resolve these itself. Observed live: it substituted the
413
+ * customer name "PREMER ENRGES PHOTOOTAIC" into an id parameter believing that
414
+ * "the runtime will resolve the customer name to its PartyId(s)" — nothing on
415
+ * the replay path does, and `WHERE BillingPartyId IN (PREMER ENRGES
416
+ * PHOTOOTAIC)` reached SQL. Reported here, the runtime resolves them before
417
+ * replay and rejects the match when one cannot be resolved.
418
+ */
419
+ mentions?: Array<{
420
+ text: string;
421
+ param?: string;
422
+ }>;
423
+ /** Business concepts the question relies on (e.g. "outstanding balance"). */
424
+ entityTypes?: string[];
425
+ /** What the user question needs that the parent doesn't cover (tier='near') */
426
+ gaps?: string[];
427
+ /** One-sentence description of the change the fork-author should make (tier='near') */
428
+ modificationHint?: string;
429
+ /**
430
+ * Which HALF of the recipe the edit targets (tier='edit'). A recipe has two
431
+ * independently editable halves:
432
+ * - 'data' — the scriptBody: how the rows are produced (aggregation,
433
+ * filters, joins, grouping). Runs MainAgent in edit mode.
434
+ * - 'display' — the component specs: how those SAME rows are shown (chart
435
+ * type, orientation, columns, labels). Replays the proven SQL
436
+ * and regenerates the specs — never authors a script.
437
+ * Defaults to 'data' when the matcher omits it.
438
+ */
439
+ editTarget?: 'data' | 'display';
440
+ /**
441
+ * Self-contained restatement of the change the user asked for (tier='edit').
442
+ * MUST have pronouns/deixis resolved ("this", "it", "that column") — the edit
443
+ * prompt never sees the conversation history, so an unresolved instruction is
444
+ * unusable downstream.
445
+ */
446
+ editInstruction?: string;
447
+ /** Why the matcher made this choice (for logs and telemetry) */
448
+ reasoning?: string;
449
+ }
450
+
451
+ /**
452
+ * Multi-Agent Architecture Types
453
+ *
454
+ * Defines interfaces for the hierarchical agent system:
455
+ * - Main Agent: ONE LLM.streamWithTools() call with source agent tools
456
+ * - Source Agents: independent agents that query individual data sources
457
+ *
458
+ * The main agent sees only source summaries. When it calls a source tool,
459
+ * the SourceAgent runs independently (own LLM, own retries) and returns clean data.
460
+ */
461
+
462
+ /**
463
+ * Per-entity detail: name, row count, and column names.
464
+ * Gives the main agent enough context to route to the right source.
465
+ */
466
+ interface EntityDetail {
467
+ /** Entity name (table, sheet, endpoint) */
468
+ name: string;
469
+ /** Approximate row count */
470
+ rowCount?: number;
471
+ /** Column/field names */
472
+ columns: string[];
473
+ /** Entity-level semantic summary (what the table means) — for main-agent routing. */
474
+ summary?: string;
475
+ }
476
+ /**
477
+ * Representation of a data source for the main agent.
478
+ * Contains entity names WITH column names so the LLM can route accurately.
479
+ */
480
+ interface SourceSummary {
481
+ /** Source ID (matches tool ID prefix) */
482
+ id: string;
483
+ /** Human-readable source name */
484
+ name: string;
485
+ /** Source type: postgres, excel, rest_api, etc. */
486
+ type: string;
487
+ /** Brief description of what data this source contains */
488
+ description: string;
489
+ /** Detailed entity info with column names for routing */
490
+ entityDetails: EntityDetail[];
491
+ /** The tool ID associated with this source */
492
+ toolId: string;
493
+ }
494
+ /**
495
+ * What a source agent returns after querying its data source.
496
+ * The main agent uses this to analyze and compose the final response.
497
+ */
498
+ interface SourceAgentResult {
499
+ /** Source ID */
500
+ sourceId: string;
501
+ /** Source name */
502
+ sourceName: string;
503
+ /** Whether the query succeeded */
504
+ success: boolean;
505
+ /** Result data rows */
506
+ data: any[];
507
+ /** Metadata about the query execution */
508
+ metadata: SourceAgentMetadata;
509
+ /** Tool execution info for the last successful query (backward compat) */
510
+ executedTool: ExecutedToolInfo;
511
+ /** All successful tool executions (primary + follow-up queries) */
512
+ allExecutedTools?: ExecutedToolInfo[];
513
+ /** Error message if failed */
514
+ error?: string;
515
+ }
516
+ interface SourceAgentMetadata {
517
+ /** Total rows that matched the query (before limit) */
518
+ totalRowsMatched: number;
519
+ /** Rows actually returned (after limit) */
520
+ rowsReturned: number;
521
+ /** Whether the result was truncated by the row limit */
522
+ isLimited: boolean;
523
+ /** The query/params that were executed */
524
+ queryExecuted?: string;
525
+ /** Execution time in milliseconds */
526
+ executionTimeMs: number;
527
+ }
528
+ /**
529
+ * A pre-built, multi-step UI flow registered with the SDK.
530
+ *
531
+ * When the main agent decides a user's question matches a workflow's whenToUse
532
+ * trigger, it picks the workflow instead of running source agents / generating
533
+ * dashboard components. The LLM extracts the workflow's required props from the
534
+ * prompt (using `propsSchema` as the tool input_schema) and the SDK returns the
535
+ * workflow component directly — no analysis text, no chart generation. The
536
+ * frontend renders the registered workflow component with the LLM-extracted
537
+ * props.
538
+ */
539
+ interface WorkflowDescriptor {
540
+ /** Unique workflow id (used as the LLM tool name) */
541
+ id: string;
542
+ /** Component name on the frontend (matches the registered React component) */
543
+ name: string;
544
+ /** Short human-readable description of what this workflow does */
545
+ description: string;
546
+ /**
547
+ * 1–2 sentence trigger condition. The LLM uses this to decide if the
548
+ * user's prompt matches this workflow. Be specific — e.g.
549
+ * "User wants to *initiate* an inventory transfer (review + submit POs),
550
+ * not just see analysis or charts."
551
+ */
552
+ whenToUse: string;
553
+ /**
554
+ * JSON-schema-style description of the props the workflow needs. Becomes
555
+ * the LLM tool's input_schema, so the model fills these from the prompt.
556
+ * Use the same shape as `params` on direct tools — string descriptors with
557
+ * an optional "(optional)" suffix.
558
+ *
559
+ * Example:
560
+ * ```
561
+ * {
562
+ * selectedStore: 'object — { id, name } of the source branch',
563
+ * minROI: 'number (optional) — only show transfers with ROI ≥ this',
564
+ * }
565
+ * ```
566
+ */
567
+ propsSchema: Record<string, string>;
568
+ /**
569
+ * Optional: static prop defaults merged with LLM-extracted props before
570
+ * the component is returned. Useful for things like the embedded
571
+ * `externalTool` config that the workflow uses to fetch its own data.
572
+ */
573
+ defaultProps?: Record<string, any>;
574
+ }
575
+ /**
576
+ * The workflow selection captured during a routing call.
577
+ * Set on AgentResponse when the LLM picks a workflow tool.
578
+ */
579
+ interface SelectedWorkflow {
580
+ /** Component name (matches WorkflowDescriptor.name) */
581
+ name: string;
582
+ /** Props extracted from the prompt + merged with workflow.defaultProps */
583
+ props: Record<string, any>;
584
+ }
585
+ /**
586
+ * Set when this turn applies a user-directed edit to an existing script instead
587
+ * of authoring a new one. MainAgent keeps the SAME harness (tools, loop,
588
+ * write_script/execute_script verification) and swaps only the system prompt —
589
+ * `agent-main-edit` instead of `agent-main`.
590
+ *
591
+ * The two framings contradict each other on whether to query a source first, so
592
+ * they must never be resident in one rendered prompt.
593
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § D3.
594
+ */
595
+ interface EditContext {
596
+ /** Recipe being edited — the shadow draft records it as parentId. */
597
+ recipeId: string;
598
+ parentName: string;
599
+ parentBody: string;
600
+ /** Self-contained restatement of the change (from the matcher). */
601
+ instruction: string;
602
+ /** Columns the last run returned — grounds the edit without a re-query. */
603
+ lastResultColumns?: string[];
604
+ /** Rendered parameter list of the parent, for the prompt. */
605
+ parentParams?: string;
606
+ }
607
+ /**
608
+ * The complete response from the multi-agent system.
609
+ * Contains everything needed for text display + component generation.
610
+ */
611
+ interface AgentResponse {
612
+ /** Generated text response (analysis of the data) */
613
+ text: string;
614
+ /** All executed tools across all source agents (for component generation) */
615
+ executedTools: ExecutedToolInfo[];
616
+ /** Individual results from each source agent */
617
+ sourceResults: SourceAgentResult[];
618
+ /**
619
+ * Populated when MainAgent wrote AND successfully executed a script during its turn.
620
+ * Caller (agent-user-response.ts) persists it via ScriptStore.save().
621
+ * Absent when MainAgent didn't write one (trivial question / all attempts failed).
622
+ */
623
+ savedScript?: AgentWrittenScript;
624
+ /**
625
+ * Set when the LLM routed the question to a registered workflow component.
626
+ * When present, the upstream caller should skip component generation and
627
+ * return this workflow as the response.
628
+ */
629
+ workflow?: SelectedWorkflow;
630
+ /**
631
+ * Validated component specs the agent authored via `render_components`.
632
+ * When present the caller assembles the dashboard from these instead of
633
+ * running the component-generation LLM. Absent when the agent didn't call
634
+ * the tool — the caller then falls back to `generateScriptComponents`.
635
+ * See backend/docs/COMPONENT-GENERATION-V2.md §2.2.
636
+ */
637
+ componentSpecs?: ScriptComponentSpec[];
638
+ /** Layout title/description from the same `render_components` call. */
639
+ componentLayout?: {
640
+ title: string;
641
+ description?: string;
642
+ };
643
+ }
644
+ /**
645
+ * A script MainAgent authored + verified during its turn. Shape aligns with
646
+ * what ScriptStore.save() needs — minus store-assigned fields (id, timestamps, counts).
647
+ */
648
+ interface AgentWrittenScript {
649
+ /**
650
+ * `ScriptRecipe.id` of the draft that was authored + verified during this turn.
651
+ * The caller passes this to `ScriptStore.promoteToVerified(recipeId, …)` to
652
+ * flip the draft to verified status and (when possible) drop the turn-suffix
653
+ * from its filename.
654
+ */
655
+ recipeId: string;
656
+ name: string;
657
+ intentDescription: string;
658
+ tags: string[];
659
+ parameters: Array<{
660
+ name: string;
661
+ type: 'string' | 'number' | 'date' | 'date_range' | 'enum' | 'boolean';
662
+ required: boolean;
663
+ default?: any;
664
+ enumValues?: Record<string, string>;
665
+ description: string;
666
+ }>;
667
+ scriptBody: string;
668
+ /** Source IDs referenced by the script (extracted from ctx.query calls) */
669
+ sourceIds: string[];
670
+ /** Tables referenced in the script's SQL (regex-extracted) */
671
+ tables: string[];
672
+ /** Executed queries from the verified run — fed to component generation */
673
+ executedQueries: Array<{
674
+ sourceId: string;
675
+ sourceName: string;
676
+ sql: string;
677
+ data: any[];
678
+ count: number;
679
+ totalCount?: number;
680
+ executionTimeMs: number;
681
+ /**
682
+ * True for synthetic entries (ctx.emit datasets, the computed:_final
683
+ * post-JS data). The component generator routes virtual sources through
684
+ * the script_dataset sentinel toolId so the frontend resolves them via
685
+ * queryCache instead of attempting to re-execute SQL.
686
+ */
687
+ virtual?: boolean;
688
+ }>;
689
+ }
690
+ /**
691
+ * Debug trace for a single chat_agent turn — which KB/schema nodes were
692
+ * retrieved and considered, and which script (if any) produced the answer.
693
+ * Persisted separately from the answer itself (fusion-5's
694
+ * `conversation_traces` table, written via `saveTrace`), never sent through
695
+ * `analyticsClient` (data-residency — see design doc §5).
696
+ */
697
+ interface RunTrace {
698
+ version: 1;
699
+ kbNodes: {
700
+ /**
701
+ * Always-on nodes included regardless of the question (getGlobalKnowledgeBase).
702
+ * `content` is the node's full text AS IT WAS at retrieval time — captured so a
703
+ * later edit/delete of the KB node doesn't erase what the LLM actually saw.
704
+ */
705
+ global: Array<{
706
+ kbId: string;
707
+ title: string;
708
+ content?: string;
709
+ }>;
710
+ /** Semantically matched for this specific question (getKnowledgeBase). Same `content` reasoning as above. */
711
+ query: Array<{
712
+ kbId: string;
713
+ title: string;
714
+ similarity?: number;
715
+ content?: string;
716
+ }>;
717
+ };
718
+ schemaNodes: {
719
+ /** MainAgent's cross-source prefilter — which sources were even considered. */
720
+ routing: Array<{
721
+ sourceId: string;
722
+ sourceName?: string;
723
+ similarity: number;
724
+ }>;
725
+ /** SourceAgent-scope per-source table resolution (MainAgent.preResolveSchema). */
726
+ preResolve: Array<{
727
+ sourceId: string;
728
+ table?: string;
729
+ description?: string;
730
+ similarity: number;
731
+ }>;
732
+ /**
733
+ * The exact {{SOURCE_SUMMARIES}} text injected into MainAgent's system prompt
734
+ * (agent-prompt-builder.ts's formatSummariesForPrompt output) — every
735
+ * registered source's catalog entry, not retrieval-specific, captured once per
736
+ * turn. This is what the main agent actually saw when deciding which source(s)
737
+ * to call.
738
+ */
739
+ mainAgentCatalog?: string;
740
+ /**
741
+ * One entry per SourceAgent dispatch (a source called twice in one turn gets
742
+ * two entries) — the exact {{FULL_SCHEMA}} text injected into that
743
+ * SourceAgent's own system prompt, however it was sourced, plus any
744
+ * search_schema follow-up lookups it made mid-conversation when the schema
745
+ * shown wasn't enough.
746
+ */
747
+ sourceAgentSchemas: Array<{
748
+ sourceId: string;
749
+ sourceName?: string;
750
+ /**
751
+ * The exact intent text MainAgent wrote for this dispatch (before the
752
+ * resolved-entities block gets appended by code — see
753
+ * buildResolvedEntitiesBlock) — what MainAgent actually asked this
754
+ * source agent to go find.
755
+ */
756
+ intent: string;
757
+ /** How `schemaText` was sourced — see SourceAgent.buildPrompt's fullSchema fallback chain. */
758
+ method: 'preresolved-embedding' | 'tool-full-schema' | 'tool-description' | 'none';
759
+ schemaText: string;
760
+ /** In call order — each search_schema tool call this SourceAgent made, and the exact result text handed back to its own LLM. */
761
+ searchSchemaLookups: Array<{
762
+ keywords: string[];
763
+ result: string;
764
+ }>;
765
+ }>;
766
+ };
767
+ /** The script that produced this answer, if any. Null for a general (no-tool) answer. */
768
+ script: {
769
+ /** True when an existing recipe was replayed unchanged (ScriptMatcher tier='high'). */
770
+ reused: boolean;
771
+ recipeId?: string | null;
772
+ body?: string | null;
773
+ /** Set when reused — the matcher's reasoning for picking this recipe. */
774
+ reasoning?: string | null;
775
+ } | null;
776
+ /**
777
+ * `resolve_entities` tool activity this turn (see main-agent.ts's
778
+ * RESOLVE_ENTITIES_TOOL_DEF / handleResolveEntities) — names/identifiers the
779
+ * user mentioned, resolved to database ids before any SQL was written.
780
+ * Absent/empty entirely for a project with no entity-search collection
781
+ * registered, or a turn that named nothing.
782
+ */
783
+ entityResolution: {
784
+ /**
785
+ * The entity-type catalog text injected into MainAgent's system prompt
786
+ * (loadEntityCatalog's output) — what let the LLM name a concept instead
787
+ * of guessing the project's vocabulary. Captured once per turn.
788
+ */
789
+ catalogText?: string;
790
+ /**
791
+ * One entry per `resolve_entities` tool call this turn (a turn can call it
792
+ * more than once) — the FULL raw response from the entity-search
793
+ * collection, not just the trimmed {mention,entityType,instanceId,
794
+ * displayName} MainAgent keeps for itself to build the dispatch block.
795
+ * `score`/`ambiguous`/`alternatives` only exist here, nowhere else.
796
+ */
797
+ calls: Array<{
798
+ mentions: Array<{
799
+ text: string;
800
+ entityType?: string;
801
+ }>;
802
+ entityTypes: string[];
803
+ resolved: Array<{
804
+ mention: string;
805
+ entityType: string;
806
+ instanceId: string;
807
+ displayName: string;
808
+ attrs?: Record<string, any>;
809
+ score?: number;
810
+ ambiguous?: boolean;
811
+ alternatives?: Array<{
812
+ instanceId: string;
813
+ displayName: string;
814
+ score: number;
815
+ }>;
816
+ }>;
817
+ unresolved: string[];
818
+ entityMap: Record<string, any>;
819
+ unmatchedEntityTypes?: Array<{
820
+ requested: string;
821
+ candidates?: string[];
822
+ }>;
823
+ /** Set when the collection call itself failed (fails open — see handleResolveEntities). */
824
+ error?: string;
825
+ }>;
826
+ };
827
+ /**
828
+ * The prior-turn conversation context injected into this turn's prompt as
829
+ * CONVERSATION_HISTORY — exactly what each flow already builds for itself
830
+ * (chat: thread.getConversationContext(); report/dashboard: their own
831
+ * equivalents), captured as-is. Every call site sets this unconditionally
832
+ * (empty string '' when there's no prior context — new thread, or history
833
+ * trimmed to nothing), so an empty turn is visibly "no history" in the
834
+ * saved trace rather than a silently missing key. Optional only because
835
+ * traces saved before this field existed won't have it at all.
836
+ */
837
+ conversationHistory?: string;
838
+ }
839
+ /**
840
+ * Configuration for the multi-agent system.
841
+ * Controls limits, models, and behavior.
842
+ */
843
+ interface AgentConfig {
844
+ /** Max rows shown to the UI preview / inlined per source (default: 10) */
845
+ maxRowsPerSource: number;
846
+ /**
847
+ * Max rows a source query may FETCH from the DB server-side (default: 2000).
848
+ * Decoupled from what the main agent is shown: the full result is fetched and
849
+ * summarized (bounded), but only a small/complete slice enters LLM context.
850
+ * This lets small lookups (benchmark maps) arrive COMPLETE without letting
851
+ * large results blow up context.
852
+ */
853
+ maxRowsFetched: number;
854
+ /** Model for the main agent (routing + analysis in one LLM call) */
855
+ mainAgentModel: string;
856
+ /** Model for source agent query generation */
857
+ sourceAgentModel: string;
858
+ /** API key for LLM calls */
859
+ apiKey?: string;
860
+ /** Max retry attempts per source agent */
861
+ maxRetries: number;
862
+ /** Max tool calling iterations for the main agent loop */
863
+ maxIterations: number;
864
+ /** Global knowledge base context (static, same for all users/questions — cached in system prompt) */
865
+ globalKnowledgeBase?: string;
866
+ /** Per-request knowledge base context (user-specific + query-matched — dynamic, not cached) */
867
+ knowledgeBaseContext?: string;
868
+ /** Collections registry (ChromaDB search hooks) for embedding-based schema + source search */
869
+ collections?: any;
870
+ /** Optional project ID for scoping embedding searches */
871
+ projectId?: string;
872
+ /**
873
+ * Optional debug-trace accumulator for this turn (Phase 1, chat_agent only).
874
+ * When present, MainAgent-scope retrieval call sites (e.g. preResolveSchema)
875
+ * push their structured results into it. Absent for callers that don't care
876
+ * about trace capture (e.g. headless flows) — every push site must treat
877
+ * this as optional and never let a capture failure affect the real answer.
878
+ */
879
+ trace?: RunTrace;
880
+ }
881
+ /**
882
+ * Default agent configuration
883
+ */
884
+ declare const DEFAULT_AGENT_CONFIG: AgentConfig;
885
+
4
886
  /**
5
887
  * Unified UIBlock structure for database storage
6
- * Used in both bookmarks and user-conversations tables
888
+ * Used in both bookmarks and user-conversations tables.
889
+ *
890
+ * `analysis` always holds whatever real narration exists — the full answer on
891
+ * success, or whatever was actually streamed before a failure (may be empty).
892
+ * `error` is a dedicated field, null on success — it can be a plain string OR
893
+ * a structured object/array (e.g. the agent's raw `errors` list), whatever
894
+ * shape the failure naturally has; it's never coerced into `analysis`.
7
895
  */
8
896
  interface DBUIBlock {
9
897
  id: string;
10
898
  component: Record<string, any> | null;
11
899
  analysis: string | null;
12
900
  user_prompt: string;
901
+ error?: unknown | null;
902
+ /**
903
+ * The script recipe that produced this answer, when one did. Read back via
904
+ * `conversation-history.exactMatch` so an edit follow-up still has a target
905
+ * after a reload (the in-memory thread is gone by then), and indexed so
906
+ * committing an edit can purge every cached answer bound to the recipe.
907
+ */
908
+ scriptBinding?: {
909
+ recipeId: string;
910
+ params?: Record<string, any>;
911
+ name?: string;
912
+ columns?: string[];
913
+ };
13
914
  }
14
915
 
15
916
  /**
@@ -59,7 +960,18 @@ declare class Logger {
59
960
  * Log debug message (only shown for verbose level)
60
961
  */
61
962
  debug(...args: any[]): void;
963
+ /**
964
+ * Write to log file
965
+ */
62
966
  file(...args: any[]): void;
967
+ /**
968
+ * Clear the log file (call at start of new user request)
969
+ */
970
+ clearFile(): void;
971
+ /**
972
+ * Log LLM method prompts with clear labeling
973
+ */
974
+ logLLMPrompt(methodName: string, promptType: 'system' | 'user', content: string | object | any[]): void;
63
975
  }
64
976
  declare const logger: Logger;
65
977
 
@@ -149,6 +1061,7 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
149
1061
  dependencies?: string[] | undefined;
150
1062
  } | undefined;
151
1063
  props?: Record<string, any> | undefined;
1064
+ data?: Record<string, any> | undefined;
152
1065
  render?: any;
153
1066
  states?: Record<string, any> | undefined;
154
1067
  methods?: Record<string, {
@@ -159,7 +1072,6 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
159
1072
  fn: string;
160
1073
  deps?: string[] | undefined;
161
1074
  }[] | undefined;
162
- data?: Record<string, any> | undefined;
163
1075
  pages?: {
164
1076
  id: string;
165
1077
  name: string;
@@ -181,6 +1093,7 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
181
1093
  dependencies?: string[] | undefined;
182
1094
  } | undefined;
183
1095
  props?: Record<string, any> | undefined;
1096
+ data?: Record<string, any> | undefined;
184
1097
  render?: any;
185
1098
  states?: Record<string, any> | undefined;
186
1099
  methods?: Record<string, {
@@ -191,7 +1104,6 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
191
1104
  fn: string;
192
1105
  deps?: string[] | undefined;
193
1106
  }[] | undefined;
194
- data?: Record<string, any> | undefined;
195
1107
  pages?: {
196
1108
  id: string;
197
1109
  name: string;
@@ -217,6 +1129,7 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
217
1129
  dependencies?: string[] | undefined;
218
1130
  } | undefined;
219
1131
  props?: Record<string, any> | undefined;
1132
+ data?: Record<string, any> | undefined;
220
1133
  render?: any;
221
1134
  states?: Record<string, any> | undefined;
222
1135
  methods?: Record<string, {
@@ -227,7 +1140,6 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
227
1140
  fn: string;
228
1141
  deps?: string[] | undefined;
229
1142
  }[] | undefined;
230
- data?: Record<string, any> | undefined;
231
1143
  pages?: {
232
1144
  id: string;
233
1145
  name: string;
@@ -253,6 +1165,7 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
253
1165
  dependencies?: string[] | undefined;
254
1166
  } | undefined;
255
1167
  props?: Record<string, any> | undefined;
1168
+ data?: Record<string, any> | undefined;
256
1169
  render?: any;
257
1170
  states?: Record<string, any> | undefined;
258
1171
  methods?: Record<string, {
@@ -263,7 +1176,6 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
263
1176
  fn: string;
264
1177
  deps?: string[] | undefined;
265
1178
  }[] | undefined;
266
- data?: Record<string, any> | undefined;
267
1179
  pages?: {
268
1180
  id: string;
269
1181
  name: string;
@@ -344,6 +1256,7 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
344
1256
  dependencies?: string[] | undefined;
345
1257
  } | undefined;
346
1258
  props?: Record<string, any> | undefined;
1259
+ data?: Record<string, any> | undefined;
347
1260
  render?: any;
348
1261
  states?: Record<string, any> | undefined;
349
1262
  methods?: Record<string, {
@@ -354,7 +1267,6 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
354
1267
  fn: string;
355
1268
  deps?: string[] | undefined;
356
1269
  }[] | undefined;
357
- data?: Record<string, any> | undefined;
358
1270
  }, {
359
1271
  id: string;
360
1272
  name?: string | undefined;
@@ -368,6 +1280,7 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
368
1280
  dependencies?: string[] | undefined;
369
1281
  } | undefined;
370
1282
  props?: Record<string, any> | undefined;
1283
+ data?: Record<string, any> | undefined;
371
1284
  render?: any;
372
1285
  states?: Record<string, any> | undefined;
373
1286
  methods?: Record<string, {
@@ -378,7 +1291,6 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
378
1291
  fn: string;
379
1292
  deps?: string[] | undefined;
380
1293
  }[] | undefined;
381
- data?: Record<string, any> | undefined;
382
1294
  }>;
383
1295
  data: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
384
1296
  context: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
@@ -396,6 +1308,7 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
396
1308
  dependencies?: string[] | undefined;
397
1309
  } | undefined;
398
1310
  props?: Record<string, any> | undefined;
1311
+ data?: Record<string, any> | undefined;
399
1312
  render?: any;
400
1313
  states?: Record<string, any> | undefined;
401
1314
  methods?: Record<string, {
@@ -406,7 +1319,6 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
406
1319
  fn: string;
407
1320
  deps?: string[] | undefined;
408
1321
  }[] | undefined;
409
- data?: Record<string, any> | undefined;
410
1322
  };
411
1323
  data?: Record<string, any> | undefined;
412
1324
  context?: Record<string, any> | undefined;
@@ -424,6 +1336,7 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
424
1336
  dependencies?: string[] | undefined;
425
1337
  } | undefined;
426
1338
  props?: Record<string, any> | undefined;
1339
+ data?: Record<string, any> | undefined;
427
1340
  render?: any;
428
1341
  states?: Record<string, any> | undefined;
429
1342
  methods?: Record<string, {
@@ -434,7 +1347,6 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
434
1347
  fn: string;
435
1348
  deps?: string[] | undefined;
436
1349
  }[] | undefined;
437
- data?: Record<string, any> | undefined;
438
1350
  };
439
1351
  data?: Record<string, any> | undefined;
440
1352
  context?: Record<string, any> | undefined;
@@ -615,24 +1527,203 @@ declare const IncomingMessageSchema: z.ZodObject<{
615
1527
  payload?: unknown;
616
1528
  }>;
617
1529
  type IncomingMessage = z.infer<typeof IncomingMessageSchema>;
1530
+ declare const ComponentSchema: z.ZodObject<{
1531
+ id: z.ZodString;
1532
+ name: z.ZodString;
1533
+ displayName: z.ZodOptional<z.ZodString>;
1534
+ isDisplayComp: z.ZodOptional<z.ZodBoolean>;
1535
+ type: z.ZodString;
1536
+ description: z.ZodString;
1537
+ props: z.ZodObject<{
1538
+ query: z.ZodOptional<z.ZodNullable<z.ZodUnion<[z.ZodString, z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>]>>>;
1539
+ title: z.ZodOptional<z.ZodString>;
1540
+ description: z.ZodOptional<z.ZodString>;
1541
+ config: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
1542
+ actions: z.ZodOptional<z.ZodArray<z.ZodAny, "many">>;
1543
+ }, "passthrough", z.ZodTypeAny, z.objectOutputType<{
1544
+ query: z.ZodOptional<z.ZodNullable<z.ZodUnion<[z.ZodString, z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>]>>>;
1545
+ title: z.ZodOptional<z.ZodString>;
1546
+ description: z.ZodOptional<z.ZodString>;
1547
+ config: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
1548
+ actions: z.ZodOptional<z.ZodArray<z.ZodAny, "many">>;
1549
+ }, z.ZodTypeAny, "passthrough">, z.objectInputType<{
1550
+ query: z.ZodOptional<z.ZodNullable<z.ZodUnion<[z.ZodString, z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>]>>>;
1551
+ title: z.ZodOptional<z.ZodString>;
1552
+ description: z.ZodOptional<z.ZodString>;
1553
+ config: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
1554
+ actions: z.ZodOptional<z.ZodArray<z.ZodAny, "many">>;
1555
+ }, z.ZodTypeAny, "passthrough">>;
1556
+ category: z.ZodOptional<z.ZodString>;
1557
+ keywords: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
1558
+ }, "strip", z.ZodTypeAny, {
1559
+ id: string;
1560
+ type: string;
1561
+ name: string;
1562
+ description: string;
1563
+ props: {
1564
+ description?: string | undefined;
1565
+ query?: string | {} | null | undefined;
1566
+ title?: string | undefined;
1567
+ config?: Record<string, unknown> | undefined;
1568
+ actions?: any[] | undefined;
1569
+ } & {
1570
+ [k: string]: unknown;
1571
+ };
1572
+ displayName?: string | undefined;
1573
+ isDisplayComp?: boolean | undefined;
1574
+ category?: string | undefined;
1575
+ keywords?: string[] | undefined;
1576
+ }, {
1577
+ id: string;
1578
+ type: string;
1579
+ name: string;
1580
+ description: string;
1581
+ props: {
1582
+ description?: string | undefined;
1583
+ query?: string | {} | null | undefined;
1584
+ title?: string | undefined;
1585
+ config?: Record<string, unknown> | undefined;
1586
+ actions?: any[] | undefined;
1587
+ } & {
1588
+ [k: string]: unknown;
1589
+ };
1590
+ displayName?: string | undefined;
1591
+ isDisplayComp?: boolean | undefined;
1592
+ category?: string | undefined;
1593
+ keywords?: string[] | undefined;
1594
+ }>;
1595
+ type Component = z.infer<typeof ComponentSchema>;
1596
+ declare const OutputFieldSchema: z.ZodObject<{
1597
+ name: z.ZodString;
1598
+ type: z.ZodEnum<["string", "number", "boolean", "date"]>;
1599
+ description: z.ZodString;
1600
+ }, "strip", z.ZodTypeAny, {
1601
+ type: "string" | "number" | "boolean" | "date";
1602
+ name: string;
1603
+ description: string;
1604
+ }, {
1605
+ type: "string" | "number" | "boolean" | "date";
1606
+ name: string;
1607
+ description: string;
1608
+ }>;
1609
+ type OutputField = z.infer<typeof OutputFieldSchema>;
1610
+ declare const OutputSchema: z.ZodObject<{
1611
+ description: z.ZodString;
1612
+ fields: z.ZodArray<z.ZodObject<{
1613
+ name: z.ZodString;
1614
+ type: z.ZodEnum<["string", "number", "boolean", "date"]>;
1615
+ description: z.ZodString;
1616
+ }, "strip", z.ZodTypeAny, {
1617
+ type: "string" | "number" | "boolean" | "date";
1618
+ name: string;
1619
+ description: string;
1620
+ }, {
1621
+ type: "string" | "number" | "boolean" | "date";
1622
+ name: string;
1623
+ description: string;
1624
+ }>, "many">;
1625
+ }, "strip", z.ZodTypeAny, {
1626
+ description: string;
1627
+ fields: {
1628
+ type: "string" | "number" | "boolean" | "date";
1629
+ name: string;
1630
+ description: string;
1631
+ }[];
1632
+ }, {
1633
+ description: string;
1634
+ fields: {
1635
+ type: "string" | "number" | "boolean" | "date";
1636
+ name: string;
1637
+ description: string;
1638
+ }[];
1639
+ }>;
1640
+ type ToolOutputSchema = z.infer<typeof OutputSchema>;
618
1641
  declare const ToolSchema: z.ZodObject<{
619
1642
  id: z.ZodString;
620
1643
  name: z.ZodString;
621
1644
  description: z.ZodString;
1645
+ /** Tool type: "source" = routed through SourceAgent, "direct" = called directly by MainAgent */
1646
+ toolType: z.ZodOptional<z.ZodEnum<["source", "direct"]>>;
1647
+ /** Full untruncated schema for source agent (all columns visible) */
1648
+ fullSchema: z.ZodOptional<z.ZodString>;
622
1649
  params: z.ZodRecord<z.ZodString, z.ZodString>;
623
1650
  fn: z.ZodFunction<z.ZodTuple<[z.ZodAny], z.ZodUnknown>, z.ZodAny>;
1651
+ outputSchema: z.ZodOptional<z.ZodObject<{
1652
+ description: z.ZodString;
1653
+ fields: z.ZodArray<z.ZodObject<{
1654
+ name: z.ZodString;
1655
+ type: z.ZodEnum<["string", "number", "boolean", "date"]>;
1656
+ description: z.ZodString;
1657
+ }, "strip", z.ZodTypeAny, {
1658
+ type: "string" | "number" | "boolean" | "date";
1659
+ name: string;
1660
+ description: string;
1661
+ }, {
1662
+ type: "string" | "number" | "boolean" | "date";
1663
+ name: string;
1664
+ description: string;
1665
+ }>, "many">;
1666
+ }, "strip", z.ZodTypeAny, {
1667
+ description: string;
1668
+ fields: {
1669
+ type: "string" | "number" | "boolean" | "date";
1670
+ name: string;
1671
+ description: string;
1672
+ }[];
1673
+ }, {
1674
+ description: string;
1675
+ fields: {
1676
+ type: "string" | "number" | "boolean" | "date";
1677
+ name: string;
1678
+ description: string;
1679
+ }[];
1680
+ }>>;
1681
+ /** Cache policy. `false` = never cache (live data, write ops). Mirrors HTTP `Cache-Control: no-store`. */
1682
+ cache: z.ZodOptional<z.ZodUnion<[z.ZodLiteral<false>, z.ZodObject<{
1683
+ ttlMs: z.ZodOptional<z.ZodNumber>;
1684
+ }, "strip", z.ZodTypeAny, {
1685
+ ttlMs?: number | undefined;
1686
+ }, {
1687
+ ttlMs?: number | undefined;
1688
+ }>]>>;
624
1689
  }, "strip", z.ZodTypeAny, {
625
1690
  id: string;
626
1691
  params: Record<string, string>;
627
1692
  name: string;
628
1693
  description: string;
629
1694
  fn: (args_0: any, ...args: unknown[]) => any;
1695
+ toolType?: "source" | "direct" | undefined;
1696
+ fullSchema?: string | undefined;
1697
+ outputSchema?: {
1698
+ description: string;
1699
+ fields: {
1700
+ type: "string" | "number" | "boolean" | "date";
1701
+ name: string;
1702
+ description: string;
1703
+ }[];
1704
+ } | undefined;
1705
+ cache?: false | {
1706
+ ttlMs?: number | undefined;
1707
+ } | undefined;
630
1708
  }, {
631
1709
  id: string;
632
1710
  params: Record<string, string>;
633
1711
  name: string;
634
1712
  description: string;
635
1713
  fn: (args_0: any, ...args: unknown[]) => any;
1714
+ toolType?: "source" | "direct" | undefined;
1715
+ fullSchema?: string | undefined;
1716
+ outputSchema?: {
1717
+ description: string;
1718
+ fields: {
1719
+ type: "string" | "number" | "boolean" | "date";
1720
+ name: string;
1721
+ description: string;
1722
+ }[];
1723
+ } | undefined;
1724
+ cache?: false | {
1725
+ ttlMs?: number | undefined;
1726
+ } | undefined;
636
1727
  }>;
637
1728
  type Tool$1 = z.infer<typeof ToolSchema>;
638
1729
  type CollectionOperation = 'getMany' | 'getOne' | 'query' | 'mutation' | 'updateOne' | 'deleteOne' | 'createOne';
@@ -640,11 +1731,33 @@ type CollectionHandler<TParams = any, TResult = any> = (params: TParams) => Prom
640
1731
  type LLMProvider = 'anthropic' | 'groq' | 'gemini' | 'openai';
641
1732
 
642
1733
  type DatabaseType = 'postgresql' | 'mssql' | 'snowflake' | 'mysql';
1734
+ /**
1735
+ * Model strategy for controlling which models are used for different tasks
1736
+ * - 'best': Use the best model (e.g., Sonnet) for all tasks - highest quality, higher cost
1737
+ * - 'fast': Use the fast model (e.g., Haiku) for all tasks - lower quality, lower cost
1738
+ * - 'balanced': Use best model for complex tasks, fast model for simple tasks (default)
1739
+ */
1740
+ type ModelStrategy = 'best' | 'fast' | 'balanced';
1741
+ /**
1742
+ * Model configuration for DASH_COMP flow (dashboard component picking)
1743
+ * Allows separate control of models used for component selection
1744
+ */
1745
+ interface DashCompModelConfig {
1746
+ /**
1747
+ * Primary model for DASH_COMP requests
1748
+ * Format: "provider/model-name" (e.g., "anthropic/claude-sonnet-4-5-20250929")
1749
+ */
1750
+ model?: string;
1751
+ /**
1752
+ * Fast model for simpler DASH_COMP tasks (optional)
1753
+ * Format: "provider/model-name" (e.g., "anthropic/claude-haiku-4-5-20251001")
1754
+ */
1755
+ fastModel?: string;
1756
+ }
643
1757
  interface SuperatomSDKConfig {
644
1758
  url?: string;
645
1759
  apiKey?: string;
646
1760
  projectId: string;
647
- userId?: string;
648
1761
  type?: string;
649
1762
  bundleDir?: string;
650
1763
  promptsDir?: string;
@@ -655,22 +1768,68 @@ interface SuperatomSDKConfig {
655
1768
  OPENAI_API_KEY?: string;
656
1769
  LLM_PROVIDERS?: LLMProvider[];
657
1770
  logLevel?: LogLevel;
658
- }
659
-
1771
+ /**
1772
+ * Model selection strategy for LLM API calls:
1773
+ * - 'best': Use best model for all tasks (highest quality, higher cost)
1774
+ * - 'fast': Use fast model for all tasks (lower quality, lower cost)
1775
+ * - 'balanced': Use best model for complex tasks, fast model for simple tasks (default)
1776
+ */
1777
+ modelStrategy?: ModelStrategy;
1778
+ /**
1779
+ * Model for the main agent (routing + analysis).
1780
+ * Format: "provider/model-name" (e.g., "anthropic/claude-haiku-4-5-20251001")
1781
+ * If not set, uses the provider's default model.
1782
+ */
1783
+ mainAgentModel?: string;
1784
+ /**
1785
+ * Model for source agents (per-source query generation).
1786
+ * Format: "provider/model-name" (e.g., "anthropic/claude-haiku-4-5-20251001")
1787
+ * If not set, uses the provider's default model.
1788
+ */
1789
+ sourceAgentModel?: string;
1790
+ /**
1791
+ * Separate model configuration for DASH_COMP flow (dashboard component picking)
1792
+ * If not provided, falls back to provider-based model selection
1793
+ */
1794
+ dashCompModels?: DashCompModelConfig;
1795
+ /**
1796
+ * Similarity threshold for conversation search (semantic matching)
1797
+ * Value between 0 and 1 (e.g., 0.8 = 80% similarity required)
1798
+ * Higher values require closer matches, lower values allow more distant matches
1799
+ * Default: 0.8
1800
+ */
1801
+ conversationSimilarityThreshold?: number;
1802
+ /**
1803
+ * Query cache TTL (Time To Live) in minutes
1804
+ * Cached query results expire after this duration
1805
+ * Default: 5 minutes
1806
+ */
1807
+ queryCacheTTL?: number;
1808
+ /**
1809
+ * Dashboard conversation history TTL (Time To Live) in minutes
1810
+ * Per-dashboard conversation histories expire after this duration
1811
+ * Default: 30 minutes
1812
+ */
1813
+ dashboardHistoryTTL?: number;
1814
+ }
1815
+
660
1816
  declare const KbNodesQueryFiltersSchema: z.ZodObject<{
661
1817
  query: z.ZodOptional<z.ZodString>;
662
1818
  category: z.ZodOptional<z.ZodString>;
663
1819
  tags: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
664
- createdBy: z.ZodOptional<z.ZodNumber>;
1820
+ type: z.ZodOptional<z.ZodEnum<["global", "user", "query"]>>;
1821
+ createdBy: z.ZodOptional<z.ZodString>;
665
1822
  }, "strip", z.ZodTypeAny, {
1823
+ type?: "query" | "user" | "global" | undefined;
666
1824
  query?: string | undefined;
667
1825
  category?: string | undefined;
668
- createdBy?: number | undefined;
1826
+ createdBy?: string | undefined;
669
1827
  tags?: string[] | undefined;
670
1828
  }, {
1829
+ type?: "query" | "user" | "global" | undefined;
671
1830
  query?: string | undefined;
672
1831
  category?: string | undefined;
673
- createdBy?: number | undefined;
1832
+ createdBy?: string | undefined;
674
1833
  tags?: string[] | undefined;
675
1834
  }>;
676
1835
  type KbNodesQueryFilters = z.infer<typeof KbNodesQueryFiltersSchema>;
@@ -682,109 +1841,126 @@ declare const KbNodesRequestPayloadSchema: z.ZodObject<{
682
1841
  content: z.ZodOptional<z.ZodString>;
683
1842
  category: z.ZodOptional<z.ZodString>;
684
1843
  tags: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
685
- createdBy: z.ZodOptional<z.ZodNumber>;
686
- updatedBy: z.ZodOptional<z.ZodNumber>;
687
- userId: z.ZodOptional<z.ZodNumber>;
1844
+ type: z.ZodOptional<z.ZodEnum<["global", "user", "query"]>>;
1845
+ createdBy: z.ZodOptional<z.ZodString>;
1846
+ updatedBy: z.ZodOptional<z.ZodString>;
1847
+ userId: z.ZodOptional<z.ZodString>;
688
1848
  query: z.ZodOptional<z.ZodString>;
689
1849
  filters: z.ZodOptional<z.ZodObject<{
690
1850
  query: z.ZodOptional<z.ZodString>;
691
1851
  category: z.ZodOptional<z.ZodString>;
692
1852
  tags: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
693
- createdBy: z.ZodOptional<z.ZodNumber>;
1853
+ type: z.ZodOptional<z.ZodEnum<["global", "user", "query"]>>;
1854
+ createdBy: z.ZodOptional<z.ZodString>;
694
1855
  }, "strip", z.ZodTypeAny, {
1856
+ type?: "query" | "user" | "global" | undefined;
695
1857
  query?: string | undefined;
696
1858
  category?: string | undefined;
697
- createdBy?: number | undefined;
1859
+ createdBy?: string | undefined;
698
1860
  tags?: string[] | undefined;
699
1861
  }, {
1862
+ type?: "query" | "user" | "global" | undefined;
700
1863
  query?: string | undefined;
701
1864
  category?: string | undefined;
702
- createdBy?: number | undefined;
1865
+ createdBy?: string | undefined;
703
1866
  tags?: string[] | undefined;
704
1867
  }>>;
705
1868
  limit: z.ZodOptional<z.ZodNumber>;
706
1869
  offset: z.ZodOptional<z.ZodNumber>;
707
1870
  }, "strip", z.ZodTypeAny, {
708
1871
  id?: number | undefined;
1872
+ type?: "query" | "user" | "global" | undefined;
709
1873
  query?: string | undefined;
710
1874
  title?: string | undefined;
711
1875
  category?: string | undefined;
712
- userId?: number | undefined;
1876
+ userId?: string | undefined;
713
1877
  limit?: number | undefined;
714
1878
  filters?: {
1879
+ type?: "query" | "user" | "global" | undefined;
715
1880
  query?: string | undefined;
716
1881
  category?: string | undefined;
717
- createdBy?: number | undefined;
1882
+ createdBy?: string | undefined;
718
1883
  tags?: string[] | undefined;
719
1884
  } | undefined;
720
- createdBy?: number | undefined;
721
- updatedBy?: number | undefined;
1885
+ createdBy?: string | undefined;
1886
+ updatedBy?: string | undefined;
1887
+ offset?: number | undefined;
722
1888
  tags?: string[] | undefined;
723
1889
  content?: string | undefined;
724
- offset?: number | undefined;
725
1890
  }, {
726
1891
  id?: number | undefined;
1892
+ type?: "query" | "user" | "global" | undefined;
727
1893
  query?: string | undefined;
728
1894
  title?: string | undefined;
729
1895
  category?: string | undefined;
730
- userId?: number | undefined;
1896
+ userId?: string | undefined;
731
1897
  limit?: number | undefined;
732
1898
  filters?: {
1899
+ type?: "query" | "user" | "global" | undefined;
733
1900
  query?: string | undefined;
734
1901
  category?: string | undefined;
735
- createdBy?: number | undefined;
1902
+ createdBy?: string | undefined;
736
1903
  tags?: string[] | undefined;
737
1904
  } | undefined;
738
- createdBy?: number | undefined;
739
- updatedBy?: number | undefined;
1905
+ createdBy?: string | undefined;
1906
+ updatedBy?: string | undefined;
1907
+ offset?: number | undefined;
740
1908
  tags?: string[] | undefined;
741
1909
  content?: string | undefined;
742
- offset?: number | undefined;
743
1910
  }>>;
744
1911
  }, "strip", z.ZodTypeAny, {
745
- operation: "create" | "getOne" | "update" | "delete" | "getAll" | "search" | "getByCategory" | "getByUser" | "getCategories" | "getTags";
1912
+ operation: "create" | "getOne" | "search" | "update" | "delete" | "getAll" | "getByCategory" | "getByUser" | "getCategories" | "getTags";
746
1913
  data?: {
747
1914
  id?: number | undefined;
1915
+ type?: "query" | "user" | "global" | undefined;
748
1916
  query?: string | undefined;
749
1917
  title?: string | undefined;
750
1918
  category?: string | undefined;
751
- userId?: number | undefined;
1919
+ userId?: string | undefined;
752
1920
  limit?: number | undefined;
753
1921
  filters?: {
1922
+ type?: "query" | "user" | "global" | undefined;
754
1923
  query?: string | undefined;
755
1924
  category?: string | undefined;
756
- createdBy?: number | undefined;
1925
+ createdBy?: string | undefined;
757
1926
  tags?: string[] | undefined;
758
1927
  } | undefined;
759
- createdBy?: number | undefined;
760
- updatedBy?: number | undefined;
1928
+ createdBy?: string | undefined;
1929
+ updatedBy?: string | undefined;
1930
+ offset?: number | undefined;
761
1931
  tags?: string[] | undefined;
762
1932
  content?: string | undefined;
763
- offset?: number | undefined;
764
1933
  } | undefined;
765
1934
  }, {
766
- operation: "create" | "getOne" | "update" | "delete" | "getAll" | "search" | "getByCategory" | "getByUser" | "getCategories" | "getTags";
1935
+ operation: "create" | "getOne" | "search" | "update" | "delete" | "getAll" | "getByCategory" | "getByUser" | "getCategories" | "getTags";
767
1936
  data?: {
768
1937
  id?: number | undefined;
1938
+ type?: "query" | "user" | "global" | undefined;
769
1939
  query?: string | undefined;
770
1940
  title?: string | undefined;
771
1941
  category?: string | undefined;
772
- userId?: number | undefined;
1942
+ userId?: string | undefined;
773
1943
  limit?: number | undefined;
774
1944
  filters?: {
1945
+ type?: "query" | "user" | "global" | undefined;
775
1946
  query?: string | undefined;
776
1947
  category?: string | undefined;
777
- createdBy?: number | undefined;
1948
+ createdBy?: string | undefined;
778
1949
  tags?: string[] | undefined;
779
1950
  } | undefined;
780
- createdBy?: number | undefined;
781
- updatedBy?: number | undefined;
1951
+ createdBy?: string | undefined;
1952
+ updatedBy?: string | undefined;
1953
+ offset?: number | undefined;
782
1954
  tags?: string[] | undefined;
783
1955
  content?: string | undefined;
784
- offset?: number | undefined;
785
1956
  } | undefined;
786
1957
  }>;
787
1958
  type KbNodesRequestPayload = z.infer<typeof KbNodesRequestPayloadSchema>;
1959
+ interface T_RESPONSE {
1960
+ success: boolean;
1961
+ data?: any;
1962
+ errors: string[];
1963
+ }
788
1964
 
789
1965
  /**
790
1966
  * UserManager class to handle CRUD operations on users with file persistence
@@ -1045,6 +2221,503 @@ declare class ReportManager {
1045
2221
  getReportCount(): number;
1046
2222
  }
1047
2223
 
2224
+ /**
2225
+ * ScriptRecipeStore — injected metadata backend for the script flow.
2226
+ *
2227
+ * The SDK is standalone (no DB dependency). The backend implements this
2228
+ * interface over Postgres (full-text search + atomic counters) and injects it
2229
+ * via `collections['script-recipes']`, exactly like `collections['source-embeddings']`.
2230
+ * `ScriptStore` consumes it for all METADATA operations while keeping the
2231
+ * executable body on disk as scripts-store/<fileBase>.ts.
2232
+ *
2233
+ * All metadata rows are plain JSON (no scriptBody — that lives on disk).
2234
+ * See backend/docs/SCRIPT-FLOW-SCALING-ISSUES.md (#1, #3, #7).
2235
+ */
2236
+
2237
+ /** One recipe's metadata as stored in Postgres (mirrors the script_recipes table). */
2238
+ interface ScriptRecipeMetaRow {
2239
+ id: string;
2240
+ projectId?: string | null;
2241
+ version: number;
2242
+ name: string;
2243
+ intentDescription: string;
2244
+ tags: string[] | null;
2245
+ createdFrom: string | null;
2246
+ sourceIds: string[] | null;
2247
+ tables: string[] | null;
2248
+ parameters: ScriptParameter[] | null;
2249
+ components?: ScriptComponentSpec[] | null;
2250
+ displayPreference?: ScriptDisplayPreference | null;
2251
+ fileBase: string;
2252
+ bodyHash?: string | null;
2253
+ successCount: number;
2254
+ failureCount: number;
2255
+ lastUsed: string | null;
2256
+ parentId?: string | null;
2257
+ forkDepth?: number | null;
2258
+ forkReason?: string | null;
2259
+ status: 'draft' | 'verified' | string;
2260
+ turnId?: string | null;
2261
+ editedBy?: string | null;
2262
+ editedFrom?: string | null;
2263
+ history?: ScriptVersionRecord[] | null;
2264
+ lastError?: {
2265
+ phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
2266
+ message: string;
2267
+ at: string;
2268
+ attempt: number;
2269
+ } | null;
2270
+ createdAt?: string | null;
2271
+ updatedAt?: string | null;
2272
+ }
2273
+ interface ScriptRecipeStore {
2274
+ /** FTS shortlist of healthy verified recipes for the matcher (metadata only). */
2275
+ search(params: {
2276
+ prompt: string;
2277
+ projectId?: string;
2278
+ limit?: number;
2279
+ }): Promise<ScriptRecipeMetaRow[]>;
2280
+ /** Fetch one recipe by id (any status). */
2281
+ getById(id: string): Promise<ScriptRecipeMetaRow | null>;
2282
+ /** Count healthy verified recipes (drives the "any scripts?" gate). */
2283
+ count(params?: {
2284
+ projectId?: string;
2285
+ }): Promise<number>;
2286
+ /** Insert or update a recipe row (keyed by id). */
2287
+ upsert(row: ScriptRecipeMetaRow): Promise<void>;
2288
+ /** Atomically bump counters / last-used. */
2289
+ updateStats(id: string, patch: {
2290
+ successDelta?: number;
2291
+ failureDelta?: number;
2292
+ lastUsed?: string;
2293
+ }): Promise<void>;
2294
+ /** Flip a draft to verified, applying provenance + optional fork lineage. */
2295
+ promote(id: string, patch: {
2296
+ sourceIds: string[];
2297
+ tables: string[];
2298
+ fileBase?: string;
2299
+ parentId?: string;
2300
+ forkDepth?: number;
2301
+ forkReason?: string;
2302
+ components?: ScriptComponentSpec[];
2303
+ }): Promise<ScriptRecipeMetaRow | null>;
2304
+ /**
2305
+ * Commit a verified edit onto an EXISTING recipe: bump `version`, replace the
2306
+ * body-bearing metadata, append a history record, reset health counters, and
2307
+ * clear the component specs (they were validated against the old shape).
2308
+ * Returns the updated row, or null when the target is gone.
2309
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 5.
2310
+ */
2311
+ commitEdit?(id: string, patch: {
2312
+ name?: string;
2313
+ intentDescription?: string;
2314
+ tags?: string[];
2315
+ parameters?: ScriptParameter[];
2316
+ bodyHash: string;
2317
+ /** New on-disk stem when the edit renamed the script; omitted otherwise. */
2318
+ fileBase?: string;
2319
+ sourceIds?: string[];
2320
+ tables?: string[];
2321
+ editedBy?: string;
2322
+ editedFrom?: string;
2323
+ historyEntry: {
2324
+ version: number;
2325
+ at: string;
2326
+ by?: string;
2327
+ instruction?: string;
2328
+ changeSummary?: string;
2329
+ bodyHash?: string;
2330
+ };
2331
+ }): Promise<ScriptRecipeMetaRow | null>;
2332
+ /** Stamp a draft's last execution error. */
2333
+ recordDraftError(id: string, err: {
2334
+ phase: string;
2335
+ message: string;
2336
+ attempt: number;
2337
+ at: string;
2338
+ }): Promise<void>;
2339
+ /** Delete a recipe row (body file removed separately). */
2340
+ remove(id: string): Promise<void>;
2341
+ /** True if `fileBase` is taken by a different recipe in this project. */
2342
+ fileBaseTaken(fileBase: string, excludeId: string, projectId?: string): Promise<boolean>;
2343
+ }
2344
+ /** Pull the injected store off the collections bag (or null if not wired). */
2345
+ declare function resolveScriptRecipeStore(collections: any): ScriptRecipeStore | null;
2346
+
2347
+ /**
2348
+ * ScriptStore — Postgres metadata + on-disk body for script recipes.
2349
+ *
2350
+ * Split of responsibilities:
2351
+ * - METADATA → injected `ScriptRecipeStore` (Postgres FTS + atomic counters),
2352
+ * resolved from `collections['script-recipes']`.
2353
+ * - BODY → scripts-store/<fileBase>.ts, editable in your IDE. Written
2354
+ * atomically (temp + rename); `bodyHash` (sha256) detects edits.
2355
+ *
2356
+ * The old "read every file every turn + send the whole catalog to the LLM"
2357
+ * matcher is gone — matching is `store.search(prompt)` (FTS shortlist). The
2358
+ * draft/verified filename dance is gone too: `status` is a DB column and the
2359
+ * file keeps a stable `<fileBase>.ts` name across promotion.
2360
+ *
2361
+ * When no metadata store is injected, the store degrades to a safe no-op
2362
+ * (count 0 → script flow disabled) instead of crashing.
2363
+ *
2364
+ * See backend/docs/SCRIPT-FLOW-SCALING-ISSUES.md.
2365
+ */
2366
+
2367
+ interface SaveDraftInput {
2368
+ /** Reuse an existing draft (retry); omit to mint a new one. */
2369
+ recipeId?: string;
2370
+ /** Per-turn unique suffix, stable across retries within the turn. */
2371
+ turnId: string;
2372
+ name: string;
2373
+ intentDescription: string;
2374
+ tags: string[];
2375
+ parameters: ScriptParameter[];
2376
+ scriptBody: string;
2377
+ createdFrom: string;
2378
+ /**
2379
+ * Set when this draft is a SHADOW of an existing recipe being edited. The
2380
+ * draft is verified independently and merged back onto the parent via
2381
+ * `commitEdit`, so the working script is never clobbered by an edit that
2382
+ * turns out not to run. See backend/docs/SCRIPT-EDIT-DESIGN.md § D5.
2383
+ */
2384
+ parentId?: string;
2385
+ }
2386
+ interface PromoteToVerifiedInput {
2387
+ sourceIds: string[];
2388
+ tables: string[];
2389
+ parentId?: string;
2390
+ forkDepth?: number;
2391
+ forkReason?: string;
2392
+ components?: ScriptComponentSpec[];
2393
+ }
2394
+ interface ScriptStoreOptions {
2395
+ /** Explicit metadata store, or resolved from `collections['script-recipes']`. */
2396
+ store?: ScriptRecipeStore | null;
2397
+ collections?: any;
2398
+ /** Body directory (defaults to <cwd>/scripts-store). */
2399
+ baseDir?: string;
2400
+ /** Project scope stamped on every row. */
2401
+ projectId?: string;
2402
+ }
2403
+ /**
2404
+ * Normalize a scriptBody into the on-disk form (strip a leading comment block,
2405
+ * ensure `export async function getData`). Exported for MainAgent.
2406
+ */
2407
+ declare function normalizeScriptBody(scriptBody: string): string;
2408
+ declare class ScriptStore {
2409
+ private store;
2410
+ private storeDir;
2411
+ private projectId?;
2412
+ constructor(opts?: ScriptStoreOptions);
2413
+ /** Whether a metadata store is wired (matcher / authoring are gated on this). */
2414
+ hasStore(): boolean;
2415
+ /** Number of healthy verified recipes (gates the script-matching path). */
2416
+ count(): Promise<number>;
2417
+ /**
2418
+ * FTS shortlist for the matcher (metadata only — bodies are loaded lazily by
2419
+ * `get()` once the LLM picks one). Returns verified, healthy recipes ranked
2420
+ * by relevance.
2421
+ */
2422
+ search(prompt: string, limit?: number): Promise<ScriptRecipe[]>;
2423
+ /** Fetch one recipe by id with its body loaded from disk. */
2424
+ get(id: string): Promise<ScriptRecipe | null>;
2425
+ /** Create or update a recipe (metadata upsert + body write when changed). */
2426
+ save(recipe: ScriptRecipe): Promise<void>;
2427
+ /**
2428
+ * Persist (or update) a draft. Within a turn, retries that pass the same
2429
+ * `recipeId` overwrite the same row + file; a fresh `recipeId` mints a new
2430
+ * draft. The body is visible at scripts-store/<fileBase>.ts immediately.
2431
+ */
2432
+ saveDraft(input: SaveDraftInput): Promise<ScriptRecipe>;
2433
+ /** Stamp a draft's last execution error (metadata only). */
2434
+ recordDraftError(recipeId: string, err: {
2435
+ phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
2436
+ message: string;
2437
+ attempt: number;
2438
+ }): Promise<void>;
2439
+ /**
2440
+ * Promote a successfully-executed draft into a verified script.
2441
+ * The on-disk body already exists at <fileBase>.ts (written at write_script
2442
+ * time) and keeps its name — only the DB row flips status + provenance.
2443
+ */
2444
+ promoteToVerified(recipeId: string, input: PromoteToVerifiedInput): Promise<ScriptRecipe | null>;
2445
+ /**
2446
+ * Commit a user-directed edit: merge a VERIFIED shadow draft back onto the
2447
+ * recipe it was editing, as version N+1 under the SAME recipe id.
2448
+ *
2449
+ * Keeping the id stable is the point of the whole feature — every cached
2450
+ * conversation, `script_dataset` regeneration descriptor and persisted
2451
+ * component spec already points at it, so the correction applies retroactively
2452
+ * to replays instead of stranding them on the old body.
2453
+ *
2454
+ * Steps (see backend/docs/SCRIPT-EDIT-DESIGN.md § Component 5):
2455
+ * 1. archive the current body as `<fileBase>.v<version>.ts`
2456
+ * 2. write the edited body to the original fileBase (atomic)
2457
+ * 3. version++, copy name/description/params, append a history record
2458
+ * 4. reset health counters — the pre-edit failure history is stale
2459
+ * 5. clear component specs — they were validated against the OLD shape
2460
+ * 6. delete the shadow draft (row + file)
2461
+ *
2462
+ * Returns the updated recipe, or null when the commit could not be applied
2463
+ * (caller should then fall back to treating the draft as a new script).
2464
+ */
2465
+ commitEdit(targetId: string, draft: {
2466
+ recipeId: string;
2467
+ name?: string;
2468
+ intentDescription?: string;
2469
+ tags?: string[];
2470
+ parameters?: ScriptParameter[];
2471
+ scriptBody: string;
2472
+ sourceIds?: string[];
2473
+ tables?: string[];
2474
+ }, meta: {
2475
+ instruction?: string;
2476
+ changeSummary?: string;
2477
+ editedBy?: string;
2478
+ }): Promise<ScriptRecipe | null>;
2479
+ /**
2480
+ * Drop a draft (row + body file). MainAgent calls this at end-of-turn when a
2481
+ * draft was authored but never verified — failed drafts are never matched, so
2482
+ * deleting them immediately avoids unbounded accumulation (#5). No-op if the
2483
+ * recipe isn't a draft (so a promoted/verified script is never removed here).
2484
+ */
2485
+ discardDraft(recipeId: string): Promise<void>;
2486
+ /** Delete a recipe (row + body file). */
2487
+ delete(id: string): Promise<void>;
2488
+ /** Record a successful execution (atomic counter bump). */
2489
+ recordSuccess(id: string): Promise<void>;
2490
+ /** Record a failed execution (atomic counter bump). */
2491
+ recordFailure(id: string): Promise<void>;
2492
+ /** Absolute path to the .ts body for a recipe (used by the runner/MainAgent). */
2493
+ getScriptPath(recipe: ScriptRecipe): string;
2494
+ private removeById;
2495
+ private rowToRecipe;
2496
+ private recipeToRow;
2497
+ /** slug of name, with a short id suffix when the bare slug is already taken. */
2498
+ private computeFileBase;
2499
+ private toSlug;
2500
+ private hash;
2501
+ private bodyPath;
2502
+ private readBody;
2503
+ /** Directory holding superseded bodies. Dot-prefixed so IDEs/`ls` hide it. */
2504
+ private get archiveDir();
2505
+ /**
2506
+ * Archive a superseded body as `.versions/<fileBase>.v<n>.ts`.
2507
+ *
2508
+ * Kept out of the main store directory on purpose — see commitEdit step 1.
2509
+ * To roll back: copy the file back over `scripts-store/<fileBase>.ts`.
2510
+ */
2511
+ private writeArchive;
2512
+ /**
2513
+ * Move a recipe's archived versions to a new prefix when its fileBase changes,
2514
+ * so all versions of one recipe stay grouped. Without this, two renames would
2515
+ * scatter a single recipe's history across three prefixes in `.versions/` with
2516
+ * nothing linking them back to the live script.
2517
+ */
2518
+ private renameArchives;
2519
+ /** Atomic body write (temp + rename) so concurrent reads never see a partial file. */
2520
+ private writeBody;
2521
+ private unlinkBody;
2522
+ }
2523
+
2524
+ /**
2525
+ * Main Agent (Orchestrator)
2526
+ *
2527
+ * A single LLM.streamWithTools() call that handles everything:
2528
+ * - Routing: decides which source(s) to query based on summaries
2529
+ * - Querying: calls source tools (each wraps an independent SourceAgent)
2530
+ * - Direct tools: calls pre-built function tools directly with LLM-provided params
2531
+ * - Re-querying: if data is wrong/incomplete, calls tools again with modified intent
2532
+ * - Analysis: generates final text response from the data
2533
+ *
2534
+ * Two tool types:
2535
+ * - "source" tools: main agent sees summaries, SourceAgent handles SQL generation independently
2536
+ * - "direct" tools: main agent calls fn() directly with structured params (no SourceAgent)
2537
+ */
2538
+
2539
+ declare class MainAgent {
2540
+ private externalTools;
2541
+ private workflows;
2542
+ private config;
2543
+ private streamBuffer;
2544
+ /**
2545
+ * Optional: when provided, MainAgent exposes the `write_script` /
2546
+ * `execute_script` tools to the LLM and persists drafts to disk via the
2547
+ * store. Headless callers (alert analyzer, metric resolver) omit these to
2548
+ * suppress script authoring entirely — drafts would otherwise leak onto
2549
+ * disk with no caller to promote or clean them up.
2550
+ */
2551
+ private scriptStore;
2552
+ private turnId;
2553
+ private createdFromPrompt;
2554
+ private scriptState;
2555
+ /** Answer-flow component catalog (filtered + projected by the caller). */
2556
+ private componentCatalog;
2557
+ /** Specs accepted by `render_components` this turn; empty until it succeeds. */
2558
+ /**
2559
+ * Entities resolved this turn, appended verbatim to every source dispatch.
2560
+ *
2561
+ * Not left to the model to copy into its intent prose: observed live, the
2562
+ * MainAgent wrote names instead of ids on the first dispatch AND re-spelled
2563
+ * 'F D C LIMITED' as 'FDC Limited', so the source agent fell back to
2564
+ * `PartyName LIKE '%FDC%'` and found 2 of 3 customers. Carrying the ids
2565
+ * structurally makes that impossible rather than instruction-dependent.
2566
+ */
2567
+ private resolvedEntities;
2568
+ private componentSpecs;
2569
+ private componentLayout;
2570
+ private renderComponentAttempts;
2571
+ /**
2572
+ * Fork mode — set when this turn is adapting a near-matching parent script.
2573
+ * In fork mode there is no legitimate "answer with bare text" outcome: the
2574
+ * only correct first move is a tool call (write_script, or a source tool for
2575
+ * schema discovery). We therefore force tool use on the first LLM iteration
2576
+ * so the model can't end its turn with a bare "I'll adapt…" preamble and zero
2577
+ * tool calls. Never set on the fresh-authoring / general-question path.
2578
+ */
2579
+ private forkMode;
2580
+ /**
2581
+ * Edit mode — set when this turn applies a user-directed change to an
2582
+ * existing script. Swaps the system prompt to `agent-main-edit` and stamps
2583
+ * the shadow draft's parentId. Like fork mode there is no legitimate
2584
+ * "answer with bare text" outcome, so tool use is forced on iteration 1.
2585
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 4.
2586
+ */
2587
+ private editContext;
2588
+ /**
2589
+ * Per-turn cancellation signal (user hit "Stop"). Set at the top of
2590
+ * handleQuestion and read by the tool handler, the SourceAgent dispatch, and
2591
+ * the script subprocess so an abort tears down every layer of the turn.
2592
+ */
2593
+ private abortSignal?;
2594
+ constructor(externalTools: ExternalTool[], config: AgentConfig, scriptStore?: ScriptStore, turnId?: string, streamBuffer?: StreamBuffer, workflows?: WorkflowDescriptor[], forkMode?: boolean, editContext?: EditContext, componentCatalog?: Component[]);
2595
+ /** True when the turn is applying a user-directed script edit. */
2596
+ private get editMode();
2597
+ private get scriptingEnabled();
2598
+ /**
2599
+ * Handle a user question using the multi-agent system.
2600
+ *
2601
+ * This is ONE LLM.streamWithTools() call. The LLM:
2602
+ * 1. Sees source summaries + direct tool descriptions in system prompt
2603
+ * 2. Decides which tool(s) to call (routing)
2604
+ * 3. Source tools → SourceAgent runs independently → returns data
2605
+ * 4. Direct tools → fn() called directly with LLM params → returns data
2606
+ * 5. Generates final analysis text
2607
+ */
2608
+ handleQuestion(userPrompt: string, apiKey?: string, conversationHistory?: string, streamCallback?: (chunk: string) => void, signal?: AbortSignal): Promise<AgentResponse>;
2609
+ /**
2610
+ * Tool definition, with the catalog rendered from the registered `Metadata`
2611
+ * the caller passed in. Generated rather than hand-written so it cannot drift
2612
+ * from what the frontend actually ships.
2613
+ */
2614
+ private buildRenderComponentsToolDef;
2615
+ /**
2616
+ * Validate the agent's component picks against the REAL columns of each bound
2617
+ * dataset. All-or-nothing: any error rejects the whole call and returns the
2618
+ * findings, because a partially-accepted dashboard makes the retry ambiguous.
2619
+ */
2620
+ private handleRenderComponents;
2621
+ private handleWriteScript;
2622
+ private handleExecuteScript;
2623
+ /**
2624
+ * Build the AgentWrittenScript payload the caller will hand to
2625
+ * `ScriptStore.promoteToVerified()`. Only returned when a verified
2626
+ * successful execution is on record.
2627
+ */
2628
+ private buildSavedScript;
2629
+ private normalizeParameterList;
2630
+ /**
2631
+ * Use the schema embedding collection to pre-select relevant tables for
2632
+ * this source + intent. Returns a formatted schema block if confidence is
2633
+ * high (top match ≥ 0.55 and ≥3 candidates), otherwise null.
2634
+ *
2635
+ * When this returns a block, we can skip the SourceAgent's `search_schema`
2636
+ * loop and reduce iteration budget. When it returns null, the SourceAgent
2637
+ * falls back to the existing LLM-driven keyword search (same as today).
2638
+ */
2639
+ /**
2640
+ * Ids + verbatim names for everything resolved this turn, appended by CODE to
2641
+ * each source dispatch so neither omission nor re-spelling by the model can
2642
+ * lose them. Empty string when nothing was resolved, so intents are untouched
2643
+ * for questions that name no entities.
2644
+ */
2645
+ private buildResolvedEntitiesBlock;
2646
+ /** True when the backend registered the entity-search collection. */
2647
+ private get entitySearchEnabled();
2648
+ /**
2649
+ * Compact catalog (type + gloss) for the system prompt. This is what lets the
2650
+ * LLM NAME a concept instead of guessing at our vocabulary — it can infer
2651
+ * that a question is about unpaid money, but not that we call that
2652
+ * `outstanding-balance`. Full `resolution` detail is fetched by the tool.
2653
+ *
2654
+ * Deliberately NOT cached in-process. The token cost is already handled by
2655
+ * prompt caching (the system prompt carries a cache_control breakpoint), and
2656
+ * re-reading a tiny PK-ordered table costs ~nothing. Fetching fresh does not
2657
+ * weaken the prompt cache either: unchanged data renders byte-identical text,
2658
+ * so the prefix still hits. A process cache would only buy a staleness window
2659
+ * in which a newly indexed entity is invisible to the agent.
2660
+ */
2661
+ private loadEntityCatalog;
2662
+ /**
2663
+ * Never throws: a resolver outage must degrade to "nothing resolved" and
2664
+ * leave the agent exactly as it was, not take the whole answer down.
2665
+ */
2666
+ private handleResolveEntities;
2667
+ private preResolveSchema;
2668
+ /**
2669
+ * Execute a direct tool — call fn() with LLM-provided params, no SourceAgent.
2670
+ */
2671
+ private handleDirectTool;
2672
+ /**
2673
+ * Build the main agent's system prompt with source summaries, direct tool descriptions,
2674
+ * and workflow component descriptions.
2675
+ */
2676
+ private buildSystemPrompt;
2677
+ /**
2678
+ * Build tool definitions for source tools — summary-only descriptions.
2679
+ * The full schema is inside the SourceAgent which runs independently.
2680
+ */
2681
+ private buildSourceToolDefinitions;
2682
+ /**
2683
+ * Build tool definitions for direct tools — expose their actual params.
2684
+ * These are called directly by the main agent LLM, no SourceAgent.
2685
+ */
2686
+ private buildDirectToolDefinitions;
2687
+ /**
2688
+ * Capture a workflow selection. We do NOT execute anything — the LLM has
2689
+ * already extracted the props it wants the workflow rendered with. We
2690
+ * record the selection (via the capture callback) and return a short
2691
+ * acknowledgement so the LLM ends its turn cleanly without writing
2692
+ * analysis text or calling more tools.
2693
+ */
2694
+ private handleWorkflow;
2695
+ /**
2696
+ * Build LLM tool definitions for workflow components. The workflow's
2697
+ * propsSchema becomes the tool's input_schema so the LLM extracts props
2698
+ * directly from the prompt — same mechanic as direct tools.
2699
+ */
2700
+ private buildWorkflowToolDefinitions;
2701
+ /**
2702
+ * Format a source agent's result as a clean string for the main agent LLM.
2703
+ */
2704
+ private formatResultForMainAgent;
2705
+ /**
2706
+ * Get source summaries (for external inspection/debugging).
2707
+ */
2708
+ getSourceSummaries(): SourceSummary[];
2709
+ }
2710
+
2711
+ /**
2712
+ * Represents an action that can be performed on a UIBlock
2713
+ */
2714
+ interface Action {
2715
+ id: string;
2716
+ name: string;
2717
+ type: string;
2718
+ [key: string]: any;
2719
+ }
2720
+
1048
2721
  type SystemPrompt = string | Anthropic.Messages.TextBlockParam[];
1049
2722
  interface LLMMessages {
1050
2723
  sys: SystemPrompt;
@@ -1057,7 +2730,45 @@ interface LLMOptions {
1057
2730
  temperature?: number;
1058
2731
  topP?: number;
1059
2732
  apiKey?: string;
2733
+ baseURL?: string;
1060
2734
  partial?: (chunk: string) => void;
2735
+ /**
2736
+ * Per-request cancellation. When the caller aborts this signal (user hit
2737
+ * "Stop"), the underlying provider request is cancelled and the call throws
2738
+ * a RequestAbortedError. Threaded into the provider `messages.create` request
2739
+ * options and checked between tool-loop iterations. Currently honored on the
2740
+ * Anthropic path (the agent flow's default provider).
2741
+ */
2742
+ signal?: AbortSignal;
2743
+ /**
2744
+ * Forces a tool call on the FIRST iteration of streamWithTools only
2745
+ * (subsequent iterations revert to auto). Used by fork mode to stop the
2746
+ * model from ending its turn with a bare "I'll adapt the script…" preamble
2747
+ * and zero tool calls. `{ type: 'any' }` lets the model pick which tool
2748
+ * (write_script in the common case, a source tool for schema discovery);
2749
+ * `{ type: 'tool', name }` pins a specific tool. Honored on both the
2750
+ * Anthropic path and the OpenAI/OpenRouter path (mapped to OpenAI's
2751
+ * tool_choice: 'required' / a named function).
2752
+ */
2753
+ firstIterationToolChoice?: {
2754
+ type: 'any';
2755
+ } | {
2756
+ type: 'tool';
2757
+ name: string;
2758
+ };
2759
+ /**
2760
+ * Internal — set only by the OpenRouter wrappers when the target is a Claude
2761
+ * model. Tells the OpenAI-wire path to emit Anthropic `cache_control`
2762
+ * breakpoints (OpenRouter forwards them to Anthropic for prompt caching).
2763
+ * Never set for direct OpenAI/Groq calls, so their requests are unchanged.
2764
+ */
2765
+ _openrouterClaudeCaching?: boolean;
2766
+ /**
2767
+ * Internal — OpenRouter provider-routing preferences (forwarded as the
2768
+ * `provider` body field). Set by the OpenRouter wrappers to steer routing to
2769
+ * a fast backend (e.g. {sort:'throughput'}). Never set for direct OpenAI/Groq.
2770
+ */
2771
+ _openrouterProvider?: Record<string, unknown>;
1061
2772
  }
1062
2773
  interface Tool {
1063
2774
  name: string;
@@ -1079,6 +2790,16 @@ declare class LLM {
1079
2790
  * @returns Normalized system prompt for Anthropic API
1080
2791
  */
1081
2792
  private static _normalizeSystemPrompt;
2793
+ /**
2794
+ * Strip unpaired UTF-16 surrogates from every text field of a message set.
2795
+ *
2796
+ * A lone surrogate (from mid-pair string slicing or corrupt source data)
2797
+ * serializes to a bare `\udXXX` escape that strict JSON parsers — including
2798
+ * the one on Anthropic's API — reject with "no low surrogate in string",
2799
+ * failing the whole request. Sanitizing here, at the single boundary every
2800
+ * provider call flows through, guarantees no request can carry one.
2801
+ */
2802
+ private static _sanitizeMessages;
1082
2803
  /**
1083
2804
  * Log cache usage metrics from Anthropic API response
1084
2805
  * Shows cache hits, costs, and savings
@@ -1095,16 +2816,76 @@ declare class LLM {
1095
2816
  * "claude-sonnet-4-5" → ["anthropic", "claude-sonnet-4-5"] (default)
1096
2817
  */
1097
2818
  private static _parseModel;
2819
+ /**
2820
+ * Map an Anthropic model id (e.g. "claude-sonnet-4-5-20250929") to the OpenRouter slug
2821
+ * (e.g. "claude-sonnet-4.5"). OpenRouter slugs drop the date suffix and use dotted versions.
2822
+ */
2823
+ private static _toOpenRouterSlug;
2824
+ /**
2825
+ * Per-provider proxy base URL. Returns `${SUPERATOM_LLM_PROXY_URL}/<provider>`
2826
+ * when our Cloudflare LLM proxy is configured, else undefined (→ talk to the
2827
+ * provider directly, legacy behaviour). An explicit options.baseURL (e.g.
2828
+ * OpenRouter) always wins and is never overridden. See backend/docs/llm-proxy.md.
2829
+ */
2830
+ private static _proxyBaseURL;
2831
+ private static _openrouterOptions;
2832
+ private static _isRetryableProviderError;
2833
+ private static _withOpenrouterRetry;
2834
+ private static _openrouterText;
2835
+ private static _openrouterStream;
2836
+ private static _openrouterStreamWithTools;
2837
+ /**
2838
+ * Build an Anthropic client. Routes through our Cloudflare LLM proxy when
2839
+ * SUPERATOM_LLM_PROXY_URL is set (each client ships a per-client proxy key as
2840
+ * ANTHROPIC_API_KEY and never holds the real key); otherwise talks to
2841
+ * api.anthropic.com directly. See backend/docs/llm-proxy.md.
2842
+ */
2843
+ private static _anthropicClient;
2844
+ /** True when OpenRouter is configured as a fail-open fallback for Claude. */
2845
+ private static _openrouterAvailable;
2846
+ /** Remap an Anthropic model id to the OpenRouter model path for fail-open. */
2847
+ private static _anthropicFallbackModel;
1098
2848
  private static _anthropicText;
1099
2849
  private static _anthropicStream;
1100
2850
  private static _anthropicStreamWithTools;
1101
2851
  private static _groqText;
1102
2852
  private static _groqStream;
2853
+ /**
2854
+ * Gemini request options carrying the proxy base URL, or undefined → talk to
2855
+ * generativelanguage.googleapis.com directly. The Google SDK takes baseUrl as a
2856
+ * per-model request option, not a constructor arg. See backend/docs/llm-proxy.md.
2857
+ */
2858
+ private static _geminiRequestOptions;
1103
2859
  private static _geminiText;
1104
2860
  private static _geminiStream;
2861
+ /**
2862
+ * Recursively strip unsupported JSON Schema properties for Gemini
2863
+ * Gemini doesn't support: additionalProperties, $schema, etc.
2864
+ */
2865
+ private static _cleanSchemaForGemini;
1105
2866
  private static _geminiStreamWithTools;
2867
+ /** True for Anthropic/Claude model ids — gates OpenRouter prompt caching. */
2868
+ private static _isClaudeModel;
2869
+ /**
2870
+ * Build the OpenAI-wire system message. For OpenRouter + Claude
2871
+ * (cacheClaude=true) it emits content parts carrying Anthropic
2872
+ * `cache_control` breakpoints (preserving any the caller set, else marking
2873
+ * the last block), so OpenRouter forwards them to Anthropic for prompt
2874
+ * caching. Otherwise it returns a plain flattened string — unchanged for
2875
+ * direct OpenAI/Groq.
2876
+ */
2877
+ private static _openaiSystemMessage;
2878
+ /**
2879
+ * Split an OpenAI-wire usage object. `prompt_tokens` INCLUDES cached tokens,
2880
+ * so we subtract them out (Anthropic-style: input excludes cache reads) and
2881
+ * report cached separately — this makes calculateCost price cache reads at
2882
+ * the discounted rate and reflects OpenRouter prompt-cache savings in logs.
2883
+ */
2884
+ private static _openaiUsage;
1106
2885
  private static _openaiText;
1107
2886
  private static _openaiStream;
2887
+ /** Map the Anthropic-style firstIterationToolChoice to OpenAI's tool_choice. */
2888
+ private static _openaiToolChoice;
1108
2889
  private static _openaiStreamWithTools;
1109
2890
  /**
1110
2891
  * Parse JSON string, handling markdown code blocks and surrounding text
@@ -1194,16 +2975,6 @@ declare class UILogCollector {
1194
2975
  setUIBlockId(uiBlockId: string): void;
1195
2976
  }
1196
2977
 
1197
- /**
1198
- * Represents an action that can be performed on a UIBlock
1199
- */
1200
- interface Action {
1201
- id: string;
1202
- name: string;
1203
- type: string;
1204
- [key: string]: any;
1205
- }
1206
-
1207
2978
  /**
1208
2979
  * UIBlock represents a single user and assistant message block in a thread
1209
2980
  * Contains user question, component metadata, component data, text response, and available actions
@@ -1216,6 +2987,13 @@ declare class UIBlock {
1216
2987
  private textResponse;
1217
2988
  private actions;
1218
2989
  private createdAt;
2990
+ /**
2991
+ * Which script recipe produced this answer, when a script did. Read on the
2992
+ * NEXT turn so the user can say "use mode instead" and have the matcher
2993
+ * resolve it to a concrete script (the `edit` tier is unreachable without it).
2994
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 1.
2995
+ */
2996
+ private scriptBinding;
1219
2997
  /**
1220
2998
  * Creates a new UIBlock instance
1221
2999
  * @param userQuestion - The user's question or input
@@ -1310,6 +3088,14 @@ declare class UIBlock {
1310
3088
  /**
1311
3089
  * Get creation timestamp
1312
3090
  */
3091
+ /**
3092
+ * Bind this block to the script recipe that produced its answer.
3093
+ */
3094
+ setScriptBinding(binding: Record<string, any> | null): void;
3095
+ /**
3096
+ * The script recipe bound to this block, if any.
3097
+ */
3098
+ getScriptBinding(): Record<string, any> | null;
1313
3099
  getCreatedAt(): Date;
1314
3100
  /**
1315
3101
  * Convert UIBlock to JSON-serializable object
@@ -1377,6 +3163,32 @@ declare class Thread {
1377
3163
  * @param currentUIBlockId - ID of current UIBlock to exclude from context (optional)
1378
3164
  * @returns Formatted conversation history string
1379
3165
  */
3166
+ /**
3167
+ * The script recipe bound to the most recent completed UIBlock — i.e. the
3168
+ * script behind the answer the user is currently looking at. Drives the
3169
+ * matcher's `edit` tier: without it, "use mode instead" has no target and
3170
+ * falls through to regeneration.
3171
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 1.
3172
+ */
3173
+ getActiveScriptBinding(currentUIBlockId?: string): Record<string, any> | null;
3174
+ /**
3175
+ * The recent script-backed answers in this thread, newest first — the
3176
+ * candidate set a user-directed edit can target.
3177
+ *
3178
+ * Returning several rather than only the newest is what lets an instruction
3179
+ * name its own target ("use the mode for the WSP one"). With a single
3180
+ * candidate every edit lands on the most recent script, which silently edits
3181
+ * the wrong recipe whenever the user meant an earlier one.
3182
+ *
3183
+ * Deduped by recipeId (newest occurrence wins) so a long editing session on
3184
+ * one script doesn't crowd out the others. Each entry carries the question
3185
+ * that produced it — without that the candidates are indistinguishable.
3186
+ *
3187
+ * In-memory only: dies with the process. The caller falls back to the
3188
+ * persisted bindings when this comes back empty.
3189
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 1.
3190
+ */
3191
+ getScriptBindings(limit?: number, currentUIBlockId?: string): Record<string, any>[];
1380
3192
  getConversationContext(limit?: number, currentUIBlockId?: string): string;
1381
3193
  /**
1382
3194
  * Convert Thread to JSON-serializable object
@@ -1386,12 +3198,20 @@ declare class Thread {
1386
3198
 
1387
3199
  /**
1388
3200
  * ThreadManager manages all threads globally
1389
- * Provides methods to create, retrieve, and delete threads
3201
+ * Provides methods to create, retrieve, and delete threads.
3202
+ * Includes automatic cleanup to prevent unbounded memory growth.
1390
3203
  */
1391
3204
  declare class ThreadManager {
1392
3205
  private static instance;
1393
3206
  private threads;
3207
+ private cleanupInterval;
3208
+ private readonly threadTtlMs;
1394
3209
  private constructor();
3210
+ /**
3211
+ * Periodically remove threads older than 7 days.
3212
+ * Runs every hour to avoid frequent iteration over the map.
3213
+ */
3214
+ private startCleanup;
1395
3215
  /**
1396
3216
  * Get singleton instance of ThreadManager
1397
3217
  */
@@ -1548,6 +3368,120 @@ declare const CONTEXT_CONFIG: {
1548
3368
  MAX_CONVERSATION_CONTEXT_BLOCKS: number;
1549
3369
  };
1550
3370
 
3371
+ /**
3372
+ * LLM Usage Logger - Tracks token usage, costs, and timing for all LLM API calls
3373
+ */
3374
+ interface LLMUsageEntry {
3375
+ timestamp: string;
3376
+ requestId: string;
3377
+ provider: string;
3378
+ model: string;
3379
+ method: string;
3380
+ inputTokens: number;
3381
+ outputTokens: number;
3382
+ cacheReadTokens?: number;
3383
+ cacheWriteTokens?: number;
3384
+ totalTokens: number;
3385
+ costUSD: number;
3386
+ durationMs: number;
3387
+ toolCalls?: number;
3388
+ success: boolean;
3389
+ error?: string;
3390
+ }
3391
+ declare class LLMUsageLogger {
3392
+ private logStream;
3393
+ private logPath;
3394
+ private enabled;
3395
+ private sessionStats;
3396
+ constructor();
3397
+ private initLogStream;
3398
+ private writeHeader;
3399
+ /**
3400
+ * Calculate cost based on token usage and model
3401
+ */
3402
+ calculateCost(model: string, inputTokens: number, outputTokens: number, cacheReadTokens?: number, cacheWriteTokens?: number): number;
3403
+ /**
3404
+ * Log an LLM API call
3405
+ */
3406
+ log(entry: LLMUsageEntry): void;
3407
+ /**
3408
+ * Log session summary (call at end of request)
3409
+ */
3410
+ logSessionSummary(requestContext?: string): void;
3411
+ /**
3412
+ * Reset session stats (call at start of new user request)
3413
+ */
3414
+ resetSession(): void;
3415
+ /**
3416
+ * Reset the log file for a new request (clears previous logs)
3417
+ * Call this at the start of each USER_PROMPT_REQ
3418
+ */
3419
+ resetLogFile(requestContext?: string): void;
3420
+ /**
3421
+ * Get current session stats
3422
+ */
3423
+ getSessionStats(): {
3424
+ totalCalls: number;
3425
+ totalInputTokens: number;
3426
+ totalOutputTokens: number;
3427
+ totalCacheReadTokens: number;
3428
+ totalCacheWriteTokens: number;
3429
+ totalCostUSD: number;
3430
+ totalDurationMs: number;
3431
+ };
3432
+ /**
3433
+ * Generate a unique request ID
3434
+ */
3435
+ generateRequestId(): string;
3436
+ }
3437
+ declare const llmUsageLogger: LLMUsageLogger;
3438
+
3439
+ /**
3440
+ * User Prompt Error Logger - Captures detailed errors for USER_PROMPT_REQ
3441
+ * Logs full error details including raw strings for parse failures
3442
+ */
3443
+ declare class UserPromptErrorLogger {
3444
+ private logStream;
3445
+ private logPath;
3446
+ private enabled;
3447
+ private hasErrors;
3448
+ constructor();
3449
+ /**
3450
+ * Reset the error log file for a new request
3451
+ */
3452
+ resetLogFile(requestContext?: string): void;
3453
+ /**
3454
+ * Log a JSON parse error with the raw string that failed
3455
+ */
3456
+ logJsonParseError(context: string, rawString: string, error: Error): void;
3457
+ /**
3458
+ * Log a general error with full details
3459
+ */
3460
+ logError(context: string, error: Error | string, additionalData?: Record<string, any>): void;
3461
+ /**
3462
+ * Log a SQL query error with the full query
3463
+ */
3464
+ logSqlError(query: string, error: Error | string, params?: any[]): void;
3465
+ /**
3466
+ * Log an LLM API error
3467
+ */
3468
+ logLlmError(provider: string, model: string, method: string, error: Error | string, requestData?: any): void;
3469
+ /**
3470
+ * Log tool execution error
3471
+ */
3472
+ logToolError(toolName: string, toolInput: any, error: Error | string): void;
3473
+ /**
3474
+ * Write final summary if there were errors
3475
+ */
3476
+ writeSummary(): void;
3477
+ /**
3478
+ * Check if any errors were logged
3479
+ */
3480
+ hadErrors(): boolean;
3481
+ private write;
3482
+ }
3483
+ declare const userPromptErrorLogger: UserPromptErrorLogger;
3484
+
1551
3485
  /**
1552
3486
  * BM25L Reranker for hybrid semantic search
1553
3487
  *
@@ -1676,14 +3610,640 @@ declare function rerankConversationResults<T extends {
1676
3610
  bm25Score: number;
1677
3611
  }>;
1678
3612
 
1679
- declare const SDK_VERSION = "0.0.8";
3613
+ /**
3614
+ * QueryExecutionService - Handles all query execution, validation, and retry logic
3615
+ * Extracted from BaseLLM for better separation of concerns
3616
+ */
3617
+
3618
+ /**
3619
+ * Context for component when requesting query fix
3620
+ */
3621
+ interface ComponentContext {
3622
+ name: string;
3623
+ type: string;
3624
+ title?: string;
3625
+ }
3626
+ /**
3627
+ * Result of query validation
3628
+ */
3629
+ interface QueryValidationResult {
3630
+ component: Component | null;
3631
+ queryKey: string;
3632
+ result: any;
3633
+ validated: boolean;
3634
+ }
3635
+ /**
3636
+ * Result of batch query validation
3637
+ */
3638
+ interface BatchValidationResult {
3639
+ components: Component[];
3640
+ queryResults: Map<string, any>;
3641
+ }
3642
+ /**
3643
+ * Configuration for QueryExecutionService
3644
+ */
3645
+ interface QueryExecutionServiceConfig {
3646
+ defaultLimit: number;
3647
+ getModelForTask: (taskType: 'simple' | 'complex') => string;
3648
+ getApiKey: (apiKey?: string) => string | undefined;
3649
+ providerName: string;
3650
+ }
3651
+ /**
3652
+ * QueryExecutionService handles all query-related operations
3653
+ */
3654
+ declare class QueryExecutionService {
3655
+ private config;
3656
+ constructor(config: QueryExecutionServiceConfig);
3657
+ /**
3658
+ * Get the cache key for a query
3659
+ * This ensures the cache key matches what the frontend will send
3660
+ */
3661
+ getQueryCacheKey(query: any): string;
3662
+ /**
3663
+ * Execute a query against the database
3664
+ * @param query - The SQL query to execute (string or object with sql/values)
3665
+ * @param collections - Collections object containing database execute function
3666
+ * @returns Object with result data and cache key
3667
+ */
3668
+ executeQuery(query: any, collections: any): Promise<{
3669
+ result: any;
3670
+ cacheKey: string;
3671
+ }>;
3672
+ /**
3673
+ * Request the LLM to fix a failed SQL query
3674
+ * @param failedQuery - The query that failed execution
3675
+ * @param errorMessage - The error message from the failed execution
3676
+ * @param componentContext - Context about the component
3677
+ * @param apiKey - Optional API key
3678
+ * @returns Fixed query string
3679
+ */
3680
+ requestQueryFix(failedQuery: string, errorMessage: string, componentContext: ComponentContext, apiKey?: string): Promise<string>;
3681
+ /**
3682
+ * Validate a single component's query with retry logic
3683
+ * @param component - The component to validate
3684
+ * @param collections - Collections object containing database execute function
3685
+ * @param apiKey - Optional API key for LLM calls
3686
+ * @returns Validation result with component, query key, and result
3687
+ */
3688
+ validateSingleQuery(component: Component, collections: any, apiKey?: string): Promise<QueryValidationResult>;
3689
+ /**
3690
+ * Validate multiple component queries in parallel
3691
+ * @param components - Array of components with potential queries
3692
+ * @param collections - Collections object containing database execute function
3693
+ * @param apiKey - Optional API key for LLM calls
3694
+ * @returns Object with validated components and query results map
3695
+ */
3696
+ validateComponentQueries(components: Component[], collections: any, apiKey?: string): Promise<BatchValidationResult>;
3697
+ }
3698
+
3699
+ /**
3700
+ * Task types for model selection
3701
+ * - 'complex': Text generation, component matching, parameter adaptation (uses best model in balanced mode)
3702
+ * - 'simple': Classification, action generation (uses fast model in balanced mode)
3703
+ */
3704
+ type TaskType = 'complex' | 'simple';
3705
+ interface BaseLLMConfig {
3706
+ model?: string;
3707
+ fastModel?: string;
3708
+ defaultLimit?: number;
3709
+ apiKey?: string;
3710
+ /**
3711
+ * Model selection strategy:
3712
+ * - 'best': Use best model for all tasks (highest quality, higher cost)
3713
+ * - 'fast': Use fast model for all tasks (lower quality, lower cost)
3714
+ * - 'balanced': Use best model for complex tasks, fast model for simple tasks (default)
3715
+ */
3716
+ modelStrategy?: ModelStrategy;
3717
+ conversationSimilarityThreshold?: number;
3718
+ }
3719
+ /**
3720
+ * BaseLLM abstract class for AI-powered component generation and matching
3721
+ * Provides common functionality for all LLM providers
3722
+ */
3723
+ declare abstract class BaseLLM {
3724
+ protected model: string;
3725
+ protected fastModel: string;
3726
+ protected defaultLimit: number;
3727
+ protected apiKey?: string;
3728
+ protected modelStrategy: ModelStrategy;
3729
+ protected conversationSimilarityThreshold: number;
3730
+ protected queryService: QueryExecutionService;
3731
+ constructor(config?: BaseLLMConfig);
3732
+ /**
3733
+ * Get the appropriate model based on task type and model strategy
3734
+ * @param taskType - 'complex' for text generation/matching, 'simple' for classification/actions
3735
+ * @returns The model string to use for this task
3736
+ */
3737
+ protected getModelForTask(taskType: TaskType): string;
3738
+ /**
3739
+ * Set the model strategy at runtime
3740
+ * @param strategy - 'best', 'fast', or 'balanced'
3741
+ */
3742
+ setModelStrategy(strategy: ModelStrategy): void;
3743
+ /**
3744
+ * Get the current model strategy
3745
+ * @returns The current model strategy
3746
+ */
3747
+ getModelStrategy(): ModelStrategy;
3748
+ /**
3749
+ * Set the conversation similarity threshold at runtime
3750
+ * @param threshold - Value between 0 and 1 (e.g., 0.8 = 80% similarity required)
3751
+ */
3752
+ setConversationSimilarityThreshold(threshold: number): void;
3753
+ /**
3754
+ * Get the current conversation similarity threshold
3755
+ * @returns The current threshold value
3756
+ */
3757
+ getConversationSimilarityThreshold(): number;
3758
+ /**
3759
+ * Get the default model for this provider (used for complex tasks like text generation)
3760
+ */
3761
+ protected abstract getDefaultModel(): string;
3762
+ /**
3763
+ * Get the default fast model for this provider (used for simple tasks: classification, matching, actions)
3764
+ * Should return a cheaper/faster model like Haiku for Anthropic
3765
+ */
3766
+ protected abstract getDefaultFastModel(): string;
3767
+ /**
3768
+ * Get the default API key from environment
3769
+ */
3770
+ protected abstract getDefaultApiKey(): string | undefined;
3771
+ /**
3772
+ * Get the provider name (for logging)
3773
+ */
3774
+ protected abstract getProviderName(): string;
3775
+ /**
3776
+ * Get the API key (from instance, parameter, or environment)
3777
+ */
3778
+ protected getApiKey(apiKey?: string): string | undefined;
3779
+ /**
3780
+ * Check if a component contains a Form (data_modification component)
3781
+ * Forms have hardcoded defaultValues that become stale when cached
3782
+ * This checks both single Form components and Forms inside MultiComponentContainer
3783
+ */
3784
+ protected containsFormComponent(component: any): boolean;
3785
+ /**
3786
+ * Match components from text response suggestions and generate follow-up questions
3787
+ * Takes a text response with component suggestions (c1:type format) and matches with available components
3788
+ * Also generates title, description, and intelligent follow-up questions (actions) based on the analysis
3789
+ * All components are placed in a default MultiComponentContainer layout
3790
+ * @param analysisContent - The text response containing component suggestions
3791
+ * @param components - List of available components
3792
+ * @param apiKey - Optional API key
3793
+ * @param componentStreamCallback - Optional callback to stream primary KPI component as soon as it's identified
3794
+ * @returns Object containing matched components, layout title/description, and follow-up actions
3795
+ */
3796
+ matchComponentsFromAnalysis(analysisContent: string, components: Component[], userPrompt: string, apiKey?: string, componentStreamCallback?: (component: Component) => void, deferredTools?: any[], executedTools?: any[], collections?: any, userId?: string): Promise<{
3797
+ components: Component[];
3798
+ layoutTitle: string;
3799
+ layoutDescription: string;
3800
+ actions: Action[];
3801
+ }>;
3802
+ /**
3803
+ * Classify user question into category and detect external tools needed
3804
+ * Determines if question is for data analysis, requires external tools, or needs text response
3805
+ */
3806
+ classifyQuestionCategory(userPrompt: string, apiKey?: string, conversationHistory?: string, externalTools?: any[]): Promise<{
3807
+ category: 'data_analysis' | 'data_modification' | 'general';
3808
+ externalTools: Array<{
3809
+ type: string;
3810
+ name: string;
3811
+ description: string;
3812
+ parameters: Record<string, any>;
3813
+ }>;
3814
+ dataAnalysisType?: 'visualization' | 'calculation' | 'comparison' | 'trend';
3815
+ reasoning: string;
3816
+ confidence: number;
3817
+ }>;
3818
+ /**
3819
+ * Adapt UI block parameters based on current user question
3820
+ * Takes a matched UI block from semantic search and modifies its props to answer the new question
3821
+ * Also adapts the cached text response to match the new question
3822
+ */
3823
+ adaptUIBlockParameters(currentUserPrompt: string, originalUserPrompt: string, matchedUIBlock: any, apiKey?: string, cachedTextResponse?: string): Promise<{
3824
+ success: boolean;
3825
+ adaptedComponent?: Component;
3826
+ adaptedTextResponse?: string;
3827
+ parametersChanged?: Array<{
3828
+ field: string;
3829
+ reason: string;
3830
+ }>;
3831
+ explanation: string;
3832
+ }>;
3833
+ /**
3834
+ * Generate text-based response for user question
3835
+ * This provides conversational text responses instead of component generation
3836
+ * Supports tool calling for query execution with automatic retry on errors (max 3 attempts)
3837
+ * After generating text response, if components are provided, matches suggested components
3838
+ */
3839
+ generateTextResponse(userPrompt: string, apiKey?: string, conversationHistory?: string, streamCallback?: (chunk: string) => void, collections?: any, components?: Component[], externalTools?: any[], category?: 'data_analysis' | 'data_modification' | 'general', userId?: string): Promise<T_RESPONSE>;
3840
+ /**
3841
+ * Main orchestration function with semantic search and multi-step classification
3842
+ * NEW FLOW (Recommended):
3843
+ * 1. Semantic search: Check previous conversations (>60% match)
3844
+ * - If match found → Adapt UI block parameters and return
3845
+ * 2. Category classification: Determine if data_analysis, requires_external_tools, or text_response
3846
+ * 3. Route appropriately based on category and response mode
3847
+ */
3848
+ 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>;
3849
+ /**
3850
+ * Generate next questions that the user might ask based on the original prompt and generated component
3851
+ * This helps provide intelligent suggestions for follow-up queries
3852
+ * For general/conversational questions without components, pass textResponse instead
3853
+ */
3854
+ generateNextQuestions(originalUserPrompt: string, component?: Component | null, componentData?: Record<string, unknown>, apiKey?: string, conversationHistory?: string, textResponse?: string, signal?: AbortSignal): Promise<string[]>;
3855
+ }
3856
+
3857
+ interface AnthropicLLMConfig extends BaseLLMConfig {
3858
+ }
3859
+ /**
3860
+ * AnthropicLLM class for handling AI-powered component generation and matching using Anthropic Claude
3861
+ */
3862
+ declare class AnthropicLLM extends BaseLLM {
3863
+ constructor(config?: AnthropicLLMConfig);
3864
+ protected getDefaultModel(): string;
3865
+ protected getDefaultFastModel(): string;
3866
+ protected getDefaultApiKey(): string | undefined;
3867
+ protected getProviderName(): string;
3868
+ }
3869
+ declare const anthropicLLM: AnthropicLLM;
3870
+
3871
+ interface GroqLLMConfig extends BaseLLMConfig {
3872
+ }
3873
+ /**
3874
+ * GroqLLM class for handling AI-powered component generation and matching using Groq
3875
+ */
3876
+ declare class GroqLLM extends BaseLLM {
3877
+ constructor(config?: GroqLLMConfig);
3878
+ protected getDefaultModel(): string;
3879
+ protected getDefaultFastModel(): string;
3880
+ protected getDefaultApiKey(): string | undefined;
3881
+ protected getProviderName(): string;
3882
+ }
3883
+ declare const groqLLM: GroqLLM;
3884
+
3885
+ interface GeminiLLMConfig extends BaseLLMConfig {
3886
+ }
3887
+ /**
3888
+ * GeminiLLM class for handling AI-powered component generation and matching using Google Gemini
3889
+ */
3890
+ declare class GeminiLLM extends BaseLLM {
3891
+ constructor(config?: GeminiLLMConfig);
3892
+ protected getDefaultModel(): string;
3893
+ protected getDefaultFastModel(): string;
3894
+ protected getDefaultApiKey(): string | undefined;
3895
+ protected getProviderName(): string;
3896
+ }
3897
+ declare const geminiLLM: GeminiLLM;
3898
+
3899
+ interface OpenAILLMConfig extends BaseLLMConfig {
3900
+ }
3901
+ /**
3902
+ * OpenAILLM class for handling AI-powered component generation and matching using OpenAI GPT models
3903
+ */
3904
+ declare class OpenAILLM extends BaseLLM {
3905
+ constructor(config?: OpenAILLMConfig);
3906
+ protected getDefaultModel(): string;
3907
+ protected getDefaultFastModel(): string;
3908
+ protected getDefaultApiKey(): string | undefined;
3909
+ protected getProviderName(): string;
3910
+ }
3911
+ declare const openaiLLM: OpenAILLM;
3912
+
3913
+ /**
3914
+ * Query Cache — Two mechanisms:
3915
+ *
3916
+ * 1. `cache` (query string → result data) — TTL-based with max size, for avoiding re-execution
3917
+ * of recently validated queries. True LRU eviction: reads bubble entries to the back via
3918
+ * delete+re-set so the oldest *unused* entry is evicted, not the oldest *inserted*.
3919
+ *
3920
+ * 2. Encrypted queryId tokens — SQL is encrypted into the queryId itself (self-contained).
3921
+ * No server-side storage needed for SQL mappings. The token is decrypted on each request.
3922
+ * This eliminates the unbounded queryIdCache that previously grew forever and caused
3923
+ * memory bloat (hundreds of MBs after thousands of queries).
3924
+ *
3925
+ * Result data can still be cached temporarily via the data cache (mechanism 1).
3926
+ */
3927
+ declare class QueryCache {
3928
+ private cache;
3929
+ private ttlMs;
3930
+ private maxCacheSize;
3931
+ private cleanupInterval;
3932
+ private readonly algorithm;
3933
+ private encryptionKey;
3934
+ constructor();
3935
+ /**
3936
+ * Set the cache TTL (Time To Live)
3937
+ * @param minutes - TTL in minutes (default: 10)
3938
+ */
3939
+ setTTL(minutes: number): void;
3940
+ /**
3941
+ * Get the current TTL in minutes
3942
+ */
3943
+ getTTL(): number;
3944
+ /**
3945
+ * Store query result in data cache.
3946
+ * If the key already exists, it's removed first so the re-insert places it
3947
+ * at the back of the iteration order (LRU). Eviction only fires when adding
3948
+ * a genuinely new key past the size limit.
3949
+ */
3950
+ set(query: string, data: any): void;
3951
+ /**
3952
+ * Get cached result if exists and not expired.
3953
+ * On hit, re-inserts the entry so it moves to the back of the Map's
3954
+ * iteration order — turning FIFO eviction into true LRU.
3955
+ */
3956
+ get(query: string): any | null;
3957
+ /**
3958
+ * Check if query exists in cache (not expired)
3959
+ */
3960
+ has(query: string): boolean;
3961
+ /**
3962
+ * Remove a specific query from cache
3963
+ */
3964
+ delete(query: string): void;
3965
+ /**
3966
+ * Clear all cached entries
3967
+ */
3968
+ clear(): void;
3969
+ /**
3970
+ * Get cache statistics
3971
+ */
3972
+ getStats(): {
3973
+ size: number;
3974
+ queryIdCount: number;
3975
+ oldestEntryAge: number | null;
3976
+ };
3977
+ /**
3978
+ * Start periodic cleanup of expired data cache entries.
3979
+ */
3980
+ private startCleanup;
3981
+ /**
3982
+ * Encrypt a payload into a self-contained token.
3983
+ */
3984
+ private encrypt;
3985
+ /**
3986
+ * Decrypt a token back to the original payload.
3987
+ */
3988
+ private decrypt;
3989
+ /**
3990
+ * Store a query by generating an encrypted token as queryId.
3991
+ * The SQL is encrypted INTO the token — nothing stored in memory.
3992
+ * If data is provided, it's cached temporarily in the data cache.
3993
+ */
3994
+ storeQuery(query: any, data?: any): string;
3995
+ /**
3996
+ * Get a stored query by decrypting its token.
3997
+ * Returns the SQL + any cached result data.
3998
+ */
3999
+ getQuery(queryId: string): {
4000
+ query: any;
4001
+ data: any;
4002
+ } | null;
4003
+ /**
4004
+ * Update cached data for a queryId token
4005
+ */
4006
+ setQueryData(queryId: string, data: any): void;
4007
+ /**
4008
+ * Stop cleanup interval (for graceful shutdown)
4009
+ */
4010
+ destroy(): void;
4011
+ }
4012
+ declare const queryCache: QueryCache;
4013
+
4014
+ /**
4015
+ * Manages conversation history scoped per user + dashboard.
4016
+ * Each user-dashboard pair has its own isolated history that expires after a configurable TTL.
4017
+ */
4018
+ declare class DashboardConversationHistory {
4019
+ private histories;
4020
+ private ttlMs;
4021
+ private maxEntries;
4022
+ private cleanupInterval;
4023
+ constructor();
4024
+ /**
4025
+ * Set the TTL for dashboard histories
4026
+ * @param minutes - TTL in minutes
4027
+ */
4028
+ setTTL(minutes: number): void;
4029
+ /**
4030
+ * Set max entries per dashboard
4031
+ */
4032
+ setMaxEntries(max: number): void;
4033
+ /**
4034
+ * Add a conversation entry for a user's dashboard
4035
+ */
4036
+ addEntry(dashboardId: string, userPrompt: string, componentSummary: string, userId?: string): void;
4037
+ /**
4038
+ * Get formatted conversation history for a user's dashboard
4039
+ */
4040
+ getHistory(dashboardId: string, userId?: string): string;
4041
+ /**
4042
+ * Clear history for a specific user's dashboard
4043
+ */
4044
+ clearDashboard(dashboardId: string, userId?: string): void;
4045
+ /**
4046
+ * Clear all dashboard histories
4047
+ */
4048
+ clearAll(): void;
4049
+ /**
4050
+ * Start periodic cleanup of expired histories
4051
+ */
4052
+ private startCleanup;
4053
+ /**
4054
+ * Stop cleanup interval (for graceful shutdown)
4055
+ */
4056
+ destroy(): void;
4057
+ }
4058
+ declare const dashboardConversationHistory: DashboardConversationHistory;
4059
+
4060
+ /**
4061
+ * Whole-dashboard generation via Pi, a terminal coding agent — as opposed to
4062
+ * DASH_COMP_REQ's single-widget-at-a-time flow. Runs Pi in-process via its
4063
+ * SDK (createAgentSession), not as a subprocess: no shell, no argument
4064
+ * quoting, no stdin/stdout piping, none of the Windows-specific subprocess
4065
+ * issues that came with spawning the `pi` CLI directly.
4066
+ *
4067
+ * Called from sdk-nodejs/src/dashboardAgent/index.ts (DASHBOARD_AGENT_REQ),
4068
+ * which owns the generic streaming/abort machinery (mirrors USER_PROMPT_REQ)
4069
+ * and passes `signal`/`onProgress` alongside the normal params — this stays
4070
+ * within CollectionHandler's loose (params) => Promise<result> typing, no
4071
+ * change needed to that shared type.
4072
+ *
4073
+ * This mechanism is generic and reusable across any deployment. What's
4074
+ * genuinely project-specific — where AGENTS.md lives, which model to use —
4075
+ * is supplied via `DashboardAgentCollectionConfig`, with defaults sensible
4076
+ * enough that most callers don't need to override them (see below). The
4077
+ * data-source tool list and the dashboard's current state both come from
4078
+ * things sdk-nodejs already exposes generically: `sdk.getTools()` (whatever
4079
+ * this deployment registered via `sdk.setTools()`) and `sdk.callCollection
4080
+ * ('dashboards', 'query', ...)` (whatever this deployment already registered
4081
+ * under that name/shape) — no per-deployment callback needed for either.
4082
+ * Same convention on the way out: after a successful run, the prompt and the
4083
+ * full response text are handed to `sdk.callCollection('dashboard-agent-
4084
+ * conversations', 'create', ...)` if this deployment has registered one —
4085
+ * skipped silently otherwise, since conversation history is optional.
4086
+ *
4087
+ * Pi verifies every query against the live database itself (via whatever
4088
+ * local tool-execution bridge the deployment exposes, e.g. an HTTP bridge
4089
+ * on localhost), but does NOT persist the result itself — it writes the
4090
+ * finished DSL to an absolute path inside `runtimeDir`, told to it explicitly
4091
+ * in the prompt, and stops there. This handler reads that file after the run
4092
+ * finishes and returns its content as `dashboard` in the result. The caller
4093
+ * (frontend) is the one that actually saves it, via whatever authenticated
4094
+ * create/update path any other dashboard edit goes through — Pi has no user
4095
+ * session/auth context of its own, so persistence shouldn't happen from
4096
+ * inside it.
4097
+ *
4098
+ * Session persistence: the FIRST call for a dashboardId pays the full cost
4099
+ * (explore KB, discover schema, plan, verify, build). Every call after that
4100
+ * resumes the same session file (SessionManager.open) so Pi has everything
4101
+ * it already learned — it only needs to reason about the new, smaller ask,
4102
+ * not rediscover the whole dashboard from scratch. The session's file path
4103
+ * (AgentSession.sessionFile) is captured right after creation and persisted
4104
+ * in a small local file, keyed by dashboardId, inside `runtimeDir`.
4105
+ */
4106
+ interface DashboardAgentCollectionConfig {
4107
+ /**
4108
+ * Working directory Pi runs from — must contain AGENTS.md. This is a
4109
+ * version-controlled prompt file, so `cwd` is expected to live somewhere
4110
+ * like a `.prompts/` folder alongside the deployment's other prompts.
4111
+ * Default: `<process.cwd()>/.prompts/dashboard-agent` — the same
4112
+ * process.cwd()-based convention PromptLoader already uses for the main
4113
+ * agent's prompts, which needs no explicit override in the common case
4114
+ * (the backend process's own cwd already is its project root).
4115
+ */
4116
+ cwd?: string;
4117
+ /**
4118
+ * Where drafts/, the session-id map, and dashboard.log get written —
4119
+ * separate from `cwd` deliberately, so this deployment's runtime state
4120
+ * (regenerated per session, safe to gitignore) doesn't sit inside the
4121
+ * same folder as the version-controlled AGENTS.md prompt.
4122
+ * Default: `<process.cwd()>/.pi-dashboard-agent-runtime`.
4123
+ */
4124
+ runtimeDir?: string;
4125
+ /** Model provider (default: process.env.PI_AGENT_PROVIDER || 'openrouter'). */
4126
+ provider?: string;
4127
+ /** Model id (default: process.env.PI_AGENT_MODEL || 'anthropic/claude-sonnet-4.5'). */
4128
+ model?: string;
4129
+ /**
4130
+ * true (default): every request starts a brand-new pi session, with the 2
4131
+ * most recent prior responses (if any) injected into the prompt as
4132
+ * context — bounded cost per request, but pi re-explores schema/KB facts
4133
+ * it already verified in an earlier turn on this same dashboard.
4134
+ * false: resumes the same session file across requests on a given
4135
+ * dashboard — pi keeps everything it already learned, but context (and
4136
+ * cost) grows unbounded across turns (one observed turn: 2M+ cache-read
4137
+ * tokens after a handful of edits on the same dashboard).
4138
+ * Default: process.env.PI_AGENT_FRESH_SESSION !== 'false'.
4139
+ */
4140
+ freshSession?: boolean;
4141
+ }
4142
+ declare function registerDashboardAgentCollection(sdk: SuperatomSDK, config?: DashboardAgentCollectionConfig): void;
4143
+
4144
+ /**
4145
+ * ScriptMatcher — LLM-Based Script Matching + Parameter Extraction
4146
+ *
4147
+ * Uses ONE LLM call to:
4148
+ * 1. Pick the best matching script from the library (or "none")
4149
+ * 2. Extract parameter values from the user question
4150
+ *
4151
+ * Why LLM over embeddings:
4152
+ * - Embeddings capture topic similarity ("overstock" ≈ "inventory" ≈ "revenue")
4153
+ * but can't distinguish structurally different questions about the same domain
4154
+ * - LLM understands that "overstock by warehouse" needs a different script than
4155
+ * "revenue by warehouse" even though they're semantically close
4156
+ * - One call does both matching AND parameter extraction
4157
+ *
4158
+ * When script library grows past ~50, add an embedding pre-filter
4159
+ * (ChromaDB narrows to top 10 → LLM picks from those 10).
4160
+ */
4161
+
4162
+ declare class ScriptMatcher {
4163
+ private store;
4164
+ constructor(store: ScriptStore);
4165
+ /**
4166
+ * Find the best matching script for a user question.
4167
+ * Uses ONE LLM call that picks the script AND extracts parameters.
4168
+ * Returns null if no script matches.
4169
+ */
4170
+ match(userPrompt: string, apiKey?: string, model?: string, signal?: AbortSignal,
4171
+ /**
4172
+ * Recent script-backed answers in this thread, newest first. Presence of at
4173
+ * least one is what makes the `edit` tier reachable at all (see the guards
4174
+ * below), and handing over SEVERAL is what lets an instruction name its own
4175
+ * target instead of always hitting the most recent script.
4176
+ */
4177
+ activeBindings?: ScriptBinding[],
4178
+ /**
4179
+ * Recent conversation turns, most recent last. The gate needs this to tell
4180
+ * "this reply is an edit instruction for the active script" apart from
4181
+ * "this reply is answering a clarifying question about a DIFFERENT,
4182
+ * unresolved topic that never became a script" — a bare parameter-shaped
4183
+ * reply ("April 2025 to March 2026") is textually indistinguishable
4184
+ * between those two cases without seeing what the assistant just asked.
4185
+ * Once this tier is decided, nothing downstream re-checks it — the edit
4186
+ * path hands MainAgent a system prompt that explicitly instructs it to
4187
+ * trust the premise and not treat the turn as a new question — so this
4188
+ * gate is the only place that can catch a mismatch.
4189
+ */
4190
+ conversationHistory?: string): Promise<ScriptMatch | null>;
4191
+ /**
4192
+ * Build the script catalog string for the LLM prompt.
4193
+ * Each script gets: index, ID, name, description, and parameter definitions.
4194
+ */
4195
+ private buildScriptCatalog;
4196
+ /**
4197
+ * The recent script-backed answers in this thread — the bounded set an edit
4198
+ * may target. Rendered as its own prompt section (never merged into the
4199
+ * ranked catalog) so the `edit` rules have an unambiguous referent set, and
4200
+ * numbered newest-first so the prompt's "prefer the most recent when the
4201
+ * instruction is ambiguous" tie-break has something to point at.
4202
+ *
4203
+ * Each entry carries the QUESTION that produced it plus the columns it
4204
+ * returned — that is what lets the matcher resolve "use the mode for the WSP
4205
+ * one" instead of blindly taking the newest.
4206
+ */
4207
+ private buildActiveScriptBlock;
4208
+ }
4209
+
4210
+ /**
4211
+ * ScriptRunner — Execute scripts in an isolated tsx subprocess.
4212
+ *
4213
+ * The subprocess approach replaces the earlier `new Function()` eval and gives us:
4214
+ * - Real sandbox (separate process, SIGKILL on timeout).
4215
+ * - Real TypeScript (tsx transpiles on the fly).
4216
+ * - npm imports available to scripts (clustering, stats, geo, etc.).
4217
+ *
4218
+ * Protocol: NDJSON over the child's stdin/stdout. See script-ipc.ts + backend/docs/SCRIPT-FLOW-IMPLEMENTATION.md.
4219
+ */
4220
+
4221
+ interface RunScriptOptions {
4222
+ /** Data sources the script is allowed to query via ctx.query */
4223
+ externalTools: ExternalTool[];
4224
+ /** Optional — for propagating per-query UI progress to the user */
4225
+ streamBuffer?: StreamBuffer;
4226
+ /** Override the wall-clock timeout (default `SCRIPT_TIMEOUT_MS`, 60s). */
4227
+ timeoutMs?: number;
4228
+ /**
4229
+ * Per-turn cancellation signal. When the user hits "Stop" mid-run, the child
4230
+ * process group is SIGKILLed and the run resolves as an aborted failure (the
4231
+ * caller is already unwinding, so the result is discarded).
4232
+ */
4233
+ signal?: AbortSignal;
4234
+ }
4235
+ /**
4236
+ * Execute a recipe by spawning a tsx child on the script's .ts file.
4237
+ * `scriptPath` is the absolute path to the saved `.ts` body.
4238
+ */
4239
+ declare function runScript(recipe: ScriptRecipe, scriptPath: string, params: Record<string, any>, options: RunScriptOptions): Promise<ScriptResult>;
4240
+
1680
4241
  type MessageTypeHandler = (message: IncomingMessage) => void | Promise<void>;
1681
4242
  declare class SuperatomSDK {
1682
4243
  private ws;
1683
4244
  private url;
1684
4245
  private apiKey?;
1685
4246
  private projectId;
1686
- private userId;
1687
4247
  private type;
1688
4248
  private bundleDir;
1689
4249
  private messageHandlers;
@@ -1694,12 +4254,18 @@ declare class SuperatomSDK {
1694
4254
  private collections;
1695
4255
  private components;
1696
4256
  private tools;
4257
+ private workflows;
1697
4258
  private anthropicApiKey;
1698
4259
  private groqApiKey;
1699
4260
  private geminiApiKey;
1700
4261
  private openaiApiKey;
1701
4262
  private llmProviders;
1702
4263
  private databaseType;
4264
+ private modelStrategy;
4265
+ private mainAgentModel;
4266
+ private sourceAgentModel;
4267
+ private dashCompModels?;
4268
+ private conversationSimilarityThreshold;
1703
4269
  private userManager;
1704
4270
  private dashboardManager;
1705
4271
  private reportManager;
@@ -1707,6 +4273,8 @@ declare class SuperatomSDK {
1707
4273
  private lastPong;
1708
4274
  private readonly PING_INTERVAL_MS;
1709
4275
  private readonly PONG_TIMEOUT_MS;
4276
+ private pendingOutbox;
4277
+ private readonly MAX_OUTBOX_SIZE;
1710
4278
  constructor(config: SuperatomSDKConfig);
1711
4279
  /**
1712
4280
  * Initialize PromptLoader and load prompts into memory
@@ -1746,9 +4314,25 @@ declare class SuperatomSDK {
1746
4314
  */
1747
4315
  private handleMessage;
1748
4316
  /**
1749
- * Send a message to the Superatom service
4317
+ * Send a message to the Superatom service.
4318
+ * Returns true if the message was sent, false if the WebSocket is not connected.
4319
+ * Does NOT throw on closed connections — callers can check the return value if needed.
1750
4320
  */
1751
- send(message: Message): void;
4321
+ send(message: Message): boolean;
4322
+ /**
4323
+ * Queue a message that couldn't be delivered because the socket was down,
4324
+ * to be resent once it reconnects. Drops the oldest entry once the bound is
4325
+ * hit — an outage long enough to fill this queue means the oldest queued
4326
+ * responses are for requests the caller has likely already given up on.
4327
+ */
4328
+ private queuePendingMessage;
4329
+ /**
4330
+ * Resend everything queued while the socket was down, now that it's back
4331
+ * up. Uses this.ws.send() directly (not send()) so a message that fails
4332
+ * again goes back through queuePendingMessage() rather than being silently
4333
+ * dropped a second time.
4334
+ */
4335
+ private flushPendingOutbox;
1752
4336
  /**
1753
4337
  * Register a message handler to receive all messages
1754
4338
  */
@@ -1788,6 +4372,14 @@ declare class SuperatomSDK {
1788
4372
  */
1789
4373
  private handlePong;
1790
4374
  private storeComponents;
4375
+ /**
4376
+ * The live, frontend-registered component catalog (name, type, description,
4377
+ * and full prop schema per component) — the same authoritative source
4378
+ * DASH_COMP_REQ's LLM prompt is built from. Exposed so other integrations
4379
+ * (e.g. the dashboard-agent script bridge) can read real component
4380
+ * contracts instead of maintaining a separate, driftable hand-written copy.
4381
+ */
4382
+ getComponents(): Component[];
1791
4383
  /**
1792
4384
  * Set tools for the SDK instance
1793
4385
  */
@@ -1796,6 +4388,57 @@ declare class SuperatomSDK {
1796
4388
  * Get the stored tools
1797
4389
  */
1798
4390
  getTools(): Tool$1[];
4391
+ /**
4392
+ * Call a registered collection operation in-process — no WebSocket
4393
+ * round-trip, since the caller is already running inside this same SDK
4394
+ * instance. Lets SDK-internal features (e.g. the dashboard agent) reuse
4395
+ * whatever collection a deployment has already registered (e.g.
4396
+ * 'dashboards'.'query') by name/convention, instead of requiring a
4397
+ * separate callback purely to re-expose data a collection already serves.
4398
+ * Throws if the collection or operation isn't registered.
4399
+ */
4400
+ callCollection<TResult = any>(collectionName: string, operation: string, params?: any): Promise<TResult>;
4401
+ /**
4402
+ * Register workflow components for the SDK instance.
4403
+ *
4404
+ * Workflows are pre-built multi-step UI flows the main agent can pick when
4405
+ * the user's prompt matches a workflow's `whenToUse` trigger. Picking a
4406
+ * workflow short-circuits analysis text + dashboard component generation —
4407
+ * the workflow component is returned directly, with the LLM-extracted props.
4408
+ */
4409
+ setWorkflows(workflows: WorkflowDescriptor[]): void;
4410
+ /**
4411
+ * Get the registered workflow components.
4412
+ */
4413
+ getWorkflows(): WorkflowDescriptor[];
4414
+ /**
4415
+ * Apply model strategy to all LLM provider singletons
4416
+ * @param strategy - 'best', 'fast', or 'balanced'
4417
+ */
4418
+ private applyModelStrategy;
4419
+ /**
4420
+ * Set model strategy at runtime
4421
+ * @param strategy - 'best', 'fast', or 'balanced'
4422
+ */
4423
+ setModelStrategy(strategy: ModelStrategy): void;
4424
+ /**
4425
+ * Get current model strategy
4426
+ */
4427
+ getModelStrategy(): ModelStrategy;
4428
+ /**
4429
+ * Apply conversation similarity threshold to all LLM provider singletons
4430
+ * @param threshold - Value between 0 and 1 (e.g., 0.8 = 80% similarity required)
4431
+ */
4432
+ private applyConversationSimilarityThreshold;
4433
+ /**
4434
+ * Set conversation similarity threshold at runtime
4435
+ * @param threshold - Value between 0 and 1 (e.g., 0.8 = 80% similarity required)
4436
+ */
4437
+ setConversationSimilarityThreshold(threshold: number): void;
4438
+ /**
4439
+ * Get current conversation similarity threshold
4440
+ */
4441
+ getConversationSimilarityThreshold(): number;
1799
4442
  }
1800
4443
 
1801
- export { type Action, BM25L, type BM25LOptions, CONTEXT_CONFIG, type CapturedLog, CleanupService, type CollectionHandler, type CollectionOperation, type DBUIBlock, type DatabaseType, type HybridSearchOptions, type IncomingMessage, type KbNodesQueryFilters, type KbNodesRequestPayload, LLM, type LogLevel, type Message, type RerankedResult, SDK_VERSION, STORAGE_CONFIG, SuperatomSDK, type SuperatomSDKConfig, Thread, ThreadManager, type Tool$1 as Tool, UIBlock, UILogCollector, type User, UserManager, type UsersData, hybridRerank, logger, rerankChromaResults, rerankConversationResults };
4444
+ export { type Action, type AgentConfig, type AgentResponse, BM25L, type BM25LOptions, type BaseLLMConfig, CONTEXT_CONFIG, type CapturedLog, CleanupService, type CollectionHandler, type CollectionOperation, type DBUIBlock, DEFAULT_AGENT_CONFIG, type DashboardAgentCollectionConfig, type DatabaseType, type HybridSearchOptions, type IncomingMessage, type KbNodesQueryFilters, type KbNodesRequestPayload, LLM, type LLMUsageEntry, type LogLevel, MainAgent, type Message, type ModelStrategy, type OutputField, type RerankedResult, STORAGE_CONFIG, type ScriptComponentSpec, ScriptMatcher, type ScriptParameter, type ScriptRecipe, type ScriptRecipeMetaRow, type ScriptRecipeStore, type ScriptResult, ScriptStore, type ScriptStoreOptions, type SelectedWorkflow, SuperatomSDK, type SuperatomSDKConfig, type TaskType, Thread, ThreadManager, type Tool$1 as Tool, type ToolOutputSchema, UIBlock, UILogCollector, type User, UserManager, type UsersData, type WorkflowDescriptor, anthropicLLM, dashboardConversationHistory, geminiLLM, groqLLM, hybridRerank, llmUsageLogger, logger, normalizeScriptBody, openaiLLM, queryCache, registerDashboardAgentCollection, rerankChromaResults, rerankConversationResults, resolveScriptRecipeStore, runScript, userPromptErrorLogger };