@superatomai/sdk-node 0.0.34-mds → 0.0.35-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.ts CHANGED
@@ -3,13 +3,32 @@ import Anthropic from '@anthropic-ai/sdk';
3
3
 
4
4
  /**
5
5
  * Unified UIBlock structure for database storage
6
- * Used in both bookmarks and user-conversations tables
6
+ * Used in both bookmarks and user-conversations tables.
7
+ *
8
+ * `analysis` always holds whatever real narration exists — the full answer on
9
+ * success, or whatever was actually streamed before a failure (may be empty).
10
+ * `error` is a dedicated field, null on success — it can be a plain string OR
11
+ * a structured object/array (e.g. the agent's raw `errors` list), whatever
12
+ * shape the failure naturally has; it's never coerced into `analysis`.
7
13
  */
8
14
  interface DBUIBlock {
9
15
  id: string;
10
16
  component: Record<string, any> | null;
11
17
  analysis: string | null;
12
18
  user_prompt: string;
19
+ error?: unknown | null;
20
+ /**
21
+ * The script recipe that produced this answer, when one did. Read back via
22
+ * `conversation-history.exactMatch` so an edit follow-up still has a target
23
+ * after a reload (the in-memory thread is gone by then), and indexed so
24
+ * committing an edit can purge every cached answer bound to the recipe.
25
+ */
26
+ scriptBinding?: {
27
+ recipeId: string;
28
+ params?: Record<string, any>;
29
+ name?: string;
30
+ columns?: string[];
31
+ };
13
32
  }
14
33
 
15
34
  /**
@@ -777,6 +796,14 @@ declare const ToolSchema: z.ZodObject<{
777
796
  description: string;
778
797
  }[];
779
798
  }>>;
799
+ /** Cache policy. `false` = never cache (live data, write ops). Mirrors HTTP `Cache-Control: no-store`. */
800
+ cache: z.ZodOptional<z.ZodUnion<[z.ZodLiteral<false>, z.ZodObject<{
801
+ ttlMs: z.ZodOptional<z.ZodNumber>;
802
+ }, "strip", z.ZodTypeAny, {
803
+ ttlMs?: number | undefined;
804
+ }, {
805
+ ttlMs?: number | undefined;
806
+ }>]>>;
780
807
  }, "strip", z.ZodTypeAny, {
781
808
  id: string;
782
809
  params: Record<string, string>;
@@ -793,6 +820,9 @@ declare const ToolSchema: z.ZodObject<{
793
820
  description: string;
794
821
  }[];
795
822
  } | undefined;
823
+ cache?: false | {
824
+ ttlMs?: number | undefined;
825
+ } | undefined;
796
826
  }, {
797
827
  id: string;
798
828
  params: Record<string, string>;
@@ -809,6 +839,9 @@ declare const ToolSchema: z.ZodObject<{
809
839
  description: string;
810
840
  }[];
811
841
  } | undefined;
842
+ cache?: false | {
843
+ ttlMs?: number | undefined;
844
+ } | undefined;
812
845
  }>;
813
846
  type Tool$1 = z.infer<typeof ToolSchema>;
814
847
  type CollectionOperation = 'getMany' | 'getOne' | 'query' | 'mutation' | 'updateOne' | 'deleteOne' | 'createOne';
@@ -1306,208 +1339,1529 @@ declare class ReportManager {
1306
1339
  getReportCount(): number;
1307
1340
  }
1308
1341
 
1309
- type SystemPrompt = string | Anthropic.Messages.TextBlockParam[];
1310
- interface LLMMessages {
1311
- sys: SystemPrompt;
1312
- user: string;
1313
- prefill?: string;
1314
- }
1315
- interface LLMOptions {
1316
- model?: string;
1317
- maxTokens?: number;
1318
- temperature?: number;
1319
- topP?: number;
1320
- apiKey?: string;
1321
- partial?: (chunk: string) => void;
1322
- }
1323
- interface Tool {
1324
- name: string;
1325
- description: string;
1326
- input_schema: {
1327
- type: string;
1328
- properties: Record<string, any>;
1329
- required?: string[];
1330
- };
1331
- }
1332
- declare class LLM {
1333
- static text(messages: LLMMessages, options?: LLMOptions): Promise<string>;
1334
- static stream<T = string>(messages: LLMMessages, options?: LLMOptions, json?: boolean): Promise<T extends string ? string : any>;
1335
- static streamWithTools(messages: LLMMessages, tools: Tool[], toolHandler: (toolName: string, toolInput: any) => Promise<any>, options?: LLMOptions, maxIterations?: number): Promise<string>;
1336
- /**
1337
- * Normalize system prompt to Anthropic format
1338
- * Converts string to array format if needed
1339
- * @param sys - System prompt (string or array of blocks)
1340
- * @returns Normalized system prompt for Anthropic API
1341
- */
1342
- private static _normalizeSystemPrompt;
1343
- /**
1344
- * Log cache usage metrics from Anthropic API response
1345
- * Shows cache hits, costs, and savings
1346
- */
1347
- private static _logCacheUsage;
1348
- /**
1349
- * Parse model string to extract provider and model name
1350
- * @param modelString - Format: "provider/model-name" or just "model-name"
1351
- * @returns [provider, modelName]
1352
- *
1353
- * @example
1354
- * "anthropic/claude-sonnet-4-5" → ["anthropic", "claude-sonnet-4-5"]
1355
- * "groq/openai/gpt-oss-120b" → ["groq", "openai/gpt-oss-120b"]
1356
- * "claude-sonnet-4-5" → ["anthropic", "claude-sonnet-4-5"] (default)
1357
- */
1358
- private static _parseModel;
1359
- private static _anthropicText;
1360
- private static _anthropicStream;
1361
- private static _anthropicStreamWithTools;
1362
- private static _groqText;
1363
- private static _groqStream;
1364
- private static _geminiText;
1365
- private static _geminiStream;
1366
- /**
1367
- * Recursively strip unsupported JSON Schema properties for Gemini
1368
- * Gemini doesn't support: additionalProperties, $schema, etc.
1369
- */
1370
- private static _cleanSchemaForGemini;
1371
- private static _geminiStreamWithTools;
1372
- private static _openaiText;
1373
- private static _openaiStream;
1374
- private static _openaiStreamWithTools;
1375
- /**
1376
- * Parse JSON string, handling markdown code blocks and surrounding text
1377
- * Enhanced version with jsonrepair to handle malformed JSON from LLMs
1378
- * @param text - Text that may contain JSON wrapped in ```json...``` or with surrounding text
1379
- * @returns Parsed JSON object or array
1380
- */
1381
- private static _parseJSON;
1382
- }
1383
-
1384
- interface CapturedLog {
1385
- timestamp: number;
1386
- level: 'info' | 'error' | 'warn' | 'debug';
1387
- message: string;
1388
- type?: 'explanation' | 'query' | 'general';
1389
- data?: Record<string, any>;
1390
- }
1391
1342
  /**
1392
- * UILogCollector captures logs during user prompt processing
1393
- * and sends them to runtime via ui_logs message with uiBlockId as the message id
1394
- * Logs are sent in real-time for streaming effect in the UI
1395
- * Respects the global log level configuration
1343
+ * StreamBuffer - Buffered streaming utility for smoother text delivery
1344
+ * Batches small chunks together and flushes at regular intervals
1396
1345
  */
1397
- declare class UILogCollector {
1398
- private logs;
1399
- private uiBlockId;
1400
- private clientId;
1401
- private sendMessage;
1402
- private currentLogLevel;
1403
- constructor(clientId: string, sendMessage: (message: Message) => void, uiBlockId?: string);
1404
- /**
1405
- * Check if logging is enabled (uiBlockId is provided)
1406
- */
1407
- isEnabled(): boolean;
1408
- /**
1409
- * Check if a message should be logged based on current log level
1410
- */
1411
- private shouldLog;
1412
- /**
1413
- * Add a log entry with timestamp and immediately send to runtime
1414
- * Only logs that pass the log level filter are captured and sent
1415
- */
1416
- private addLog;
1417
- /**
1418
- * Send a single log to runtime immediately
1419
- */
1420
- private sendLogImmediately;
1421
- /**
1422
- * Log info message
1423
- */
1424
- info(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
1425
- /**
1426
- * Log error message
1427
- */
1428
- error(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
1429
- /**
1430
- * Log warning message
1431
- */
1432
- warn(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
1433
- /**
1434
- * Log debug message
1435
- */
1436
- debug(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
1346
+ type StreamCallback = (chunk: string) => void;
1347
+ /**
1348
+ * StreamBuffer class for managing buffered streaming output
1349
+ * Provides smooth text delivery by batching small chunks
1350
+ */
1351
+ declare class StreamBuffer {
1352
+ private buffer;
1353
+ private flushTimer;
1354
+ private callback;
1355
+ private fullText;
1356
+ constructor(callback?: StreamCallback);
1437
1357
  /**
1438
- * Log LLM explanation with typed metadata
1358
+ * Check if the buffer has a callback configured
1439
1359
  */
1440
- logExplanation(message: string, explanation: string, data?: Record<string, any>): void;
1360
+ hasCallback(): boolean;
1441
1361
  /**
1442
- * Log generated query with typed metadata
1362
+ * Get all text that has been written (including already flushed)
1443
1363
  */
1444
- logQuery(message: string, query: string, data?: Record<string, any>): void;
1364
+ getFullText(): string;
1445
1365
  /**
1446
- * Send all collected logs at once (optional, for final summary)
1366
+ * Write a chunk to the buffer
1367
+ * Large chunks or chunks with newlines are flushed immediately
1368
+ * Small chunks are batched and flushed after a short interval
1369
+ *
1370
+ * @param chunk - Text chunk to write
1447
1371
  */
1448
- sendAllLogs(): void;
1372
+ write(chunk: string): void;
1449
1373
  /**
1450
- * Get all collected logs
1374
+ * Flush the buffer immediately
1375
+ * Call this before tool execution or other operations that need clean output
1451
1376
  */
1452
- getLogs(): CapturedLog[];
1377
+ flush(): void;
1453
1378
  /**
1454
- * Clear all logs
1379
+ * Internal flush implementation
1455
1380
  */
1456
- clearLogs(): void;
1381
+ private flushNow;
1457
1382
  /**
1458
- * Set uiBlockId (in case it's provided later)
1383
+ * Clean up resources
1384
+ * Call this when done with the buffer
1459
1385
  */
1460
- setUIBlockId(uiBlockId: string): void;
1386
+ dispose(): void;
1461
1387
  }
1462
1388
 
1463
1389
  /**
1464
- * Represents an action that can be performed on a UIBlock
1390
+ * ToolExecutorService - Handles execution of SQL queries and external tools
1391
+ * Extracted from BaseLLM.generateTextResponse for better separation of concerns
1465
1392
  */
1466
- interface Action {
1393
+
1394
+ /**
1395
+ * External tool definition
1396
+ */
1397
+ interface ExternalTool {
1467
1398
  id: string;
1468
1399
  name: string;
1469
- type: string;
1470
- [key: string]: any;
1400
+ description?: string;
1401
+ /** Tool type: "source" = routed through SourceAgent, "direct" = called directly by MainAgent */
1402
+ toolType?: 'source' | 'direct';
1403
+ /** Full untruncated schema for source agent (all columns visible) */
1404
+ fullSchema?: string;
1405
+ /** Schema size tier: small (≤50 tables), medium (51-200), large (201-500), very_large (500+) */
1406
+ schemaTier?: string;
1407
+ /** Schema search function for very_large tier — keyword search over entities */
1408
+ schemaSearchFn?: (keywords: string[]) => string;
1409
+ fn: (input: any) => Promise<any>;
1410
+ limit?: number;
1411
+ outputSchema?: any;
1412
+ executionType?: 'immediate' | 'deferred';
1413
+ userProvidedData?: any;
1414
+ params?: Record<string, any>;
1415
+ }
1416
+ /**
1417
+ * Executed tool tracking info
1418
+ */
1419
+ interface ExecutedToolInfo {
1420
+ id: string;
1421
+ name: string;
1422
+ params: any;
1423
+ result: {
1424
+ _totalRecords: number;
1425
+ _recordsShown: number;
1426
+ _metadata?: any;
1427
+ _sampleData: any[];
1428
+ /** Bounded summary over the FULL fetched result (complete structure). */
1429
+ _summary?: any;
1430
+ /** Up to MAIN_AGENT_COMPLETE_ROWS rows — the complete result when small. */
1431
+ _mainAgentRows?: any[];
1432
+ };
1433
+ outputSchema?: any;
1434
+ sourceSchema?: string;
1435
+ sourceType?: string;
1471
1436
  }
1472
1437
 
1473
1438
  /**
1474
- * UIBlock represents a single user and assistant message block in a thread
1475
- * Contains user question, component metadata, component data, text response, and available actions
1439
+ * Script Flow Types
1440
+ *
1441
+ * Defines interfaces for the script-based query architecture:
1442
+ * - ScriptRecipe: metadata for matching, validation, and quality tracking
1443
+ * - ScriptResult: output from executing a script
1444
+ * - ScriptMatch: result from the LLM-based script matcher
1476
1445
  */
1477
- declare class UIBlock {
1478
- private id;
1479
- private userQuestion;
1480
- private generatedComponentMetadata;
1481
- private componentData;
1482
- private textResponse;
1483
- private actions;
1484
- private createdAt;
1485
- /**
1486
- * Creates a new UIBlock instance
1487
- * @param userQuestion - The user's question or input
1488
- * @param componentData - The component data object
1489
- * @param generatedComponentMetadata - Optional metadata about the generated component
1490
- * @param actions - Optional array of available actions
1491
- * @param id - Optional custom ID, generates UUID if not provided
1492
- * @param textResponse - Optional text response from LLM
1493
- */
1494
- constructor(userQuestion: string, componentData?: Record<string, any>, generatedComponentMetadata?: Record<string, any>, actions?: Action[], id?: string, textResponse?: string | null);
1495
- /**
1496
- * Get the UIBlock ID
1446
+ /**
1447
+ * Recipe metadata stored alongside each script.
1448
+ * Used for matching, validation, and quality tracking.
1449
+ */
1450
+ interface ScriptRecipe {
1451
+ /** Unique script identifier */
1452
+ id: string;
1453
+ /** Version number (incremented on regeneration) */
1454
+ version: number;
1455
+ /** Human-readable name (e.g., "Revenue by Dimension") */
1456
+ name: string;
1457
+ /** Natural language description of what this script does */
1458
+ intentDescription: string;
1459
+ /** Keyword tags for quick filtering */
1460
+ tags: string[];
1461
+ /** Source tool IDs this script queries (e.g., ["mssql-abc123_query"]) */
1462
+ sourceIds: string[];
1463
+ /** Table names used (for future schema drift detection) */
1464
+ tables: string[];
1465
+ /** Parameter definitions — what can vary */
1466
+ parameters: ScriptParameter[];
1467
+ /** The script function body as a string. Loaded from disk (scripts-store/<fileBase>.ts). */
1468
+ scriptBody: string;
1469
+ /**
1470
+ * On-disk filename stem for the body: scripts-store/<fileBase>.ts.
1471
+ * Editable in the IDE. Decided at authoring time (slug of `name`, with a
1472
+ * short id suffix on collision) and stable across promotion.
1473
+ */
1474
+ fileBase?: string;
1475
+ /** sha256 of the on-disk body — lets the runtime detect manual edits. */
1476
+ bodyHash?: string;
1477
+ /** Project scope (single-VM deployments may leave this undefined). */
1478
+ projectId?: string;
1479
+ /** Times this script was used successfully */
1480
+ successCount: number;
1481
+ /** Times this script failed */
1482
+ failureCount: number;
1483
+ /** ISO timestamp of last usage */
1484
+ lastUsed: string;
1485
+ /** Original user question that created this script */
1486
+ createdFrom: string;
1487
+ /** ISO timestamp */
1488
+ createdAt: string;
1489
+ /** ISO timestamp */
1490
+ updatedAt: string;
1491
+ /**
1492
+ * `recipe.id` of the parent this script was forked from.
1493
+ * Undefined for root scripts (those written from scratch by MainAgent).
1494
+ * See backend/docs/SCRIPT-FLOW-FORK-ADAPT.md.
1495
+ */
1496
+ parentId?: string;
1497
+ /** 0 for root scripts; `parent.forkDepth + 1` for forks. Capped at 3. */
1498
+ forkDepth?: number;
1499
+ /**
1500
+ * Brief description of what this fork changed vs its parent
1501
+ * (sourced from the matcher's `modificationHint`).
1502
+ */
1503
+ forkReason?: string;
1504
+ /**
1505
+ * Validated component specs captured at authoring time. On a tier-high
1506
+ * replay these are rebound to fresh queryIds deterministically — no
1507
+ * component-generation LLM call, and the rendered columns can't drift from
1508
+ * what was validated when the script was authored. Absent on recipes
1509
+ * authored before this landed; those fall back to LLM component generation.
1510
+ * See backend/docs/SCRIPT-COMPONENT-CONSISTENCY.md.
1511
+ */
1512
+ components?: ScriptComponentSpec[];
1513
+ /**
1514
+ * What the user explicitly asked the output to LOOK like, set by a display
1515
+ * edit ("show that as a bar chart", "make the bars horizontal").
1516
+ *
1517
+ * Deliberately SEPARATE from `components`. Those are validated bindings that
1518
+ * must be cleared on a data edit — after "break it down by brand" the axis
1519
+ * keys are wrong, and after "use the mode" the stored title still says
1520
+ * "Average…". A rendering CHOICE, by contrast, is still valid afterwards.
1521
+ * Keeping them in one field meant every data edit silently discarded the
1522
+ * user's chart type.
1523
+ *
1524
+ * Fed to the component generator as a hint whenever specs are (re)generated,
1525
+ * so the chosen rendering comes back even after the SQL changes.
1526
+ */
1527
+ displayPreference?: ScriptDisplayPreference;
1528
+ /**
1529
+ * Lifecycle stage of this recipe on disk.
1530
+ * - 'draft': written by MainAgent's write_script during a turn; filtered out
1531
+ * of FTS results (status='verified' only) so the matcher never picks it.
1532
+ * Filename is suffixed with `turnId` to keep concurrent turns
1533
+ * from clobbering each other's drafts.
1534
+ * - 'verified': promoted after `execute_script` succeeded; the matcher sees it.
1535
+ * Filename drops the turn suffix unless a verified file with
1536
+ * the same slug already exists (collision case keeps the suffix).
1537
+ *
1538
+ * Recipes loaded from disk without this field default to 'verified' so
1539
+ * existing scripts keep working unchanged.
1497
1540
  */
1498
- getId(): string;
1541
+ status?: 'draft' | 'verified';
1499
1542
  /**
1500
- * Get the user question
1543
+ * Per-turn unique suffix used for draft filenames (e.g. `1714745623-x9k2`).
1544
+ * Set when the draft is saved; carried until the recipe is promoted.
1501
1545
  */
1502
- getUserQuestion(): string;
1546
+ turnId?: string;
1503
1547
  /**
1504
- * Set or update the user question
1548
+ * Last execution error captured by `recordDraftError` while the recipe was
1549
+ * still a draft. Lets users open the draft .json file and see why it failed
1550
+ * without grepping logs. Cleared on promotion to 'verified'.
1505
1551
  */
1506
- setUserQuestion(question: string): void;
1552
+ lastError?: {
1553
+ phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
1554
+ message: string;
1555
+ at: string;
1556
+ attempt: number;
1557
+ };
1558
+ /** userId who committed the most recent edit (audit trail — recipes are project-scoped). */
1559
+ editedBy?: string;
1560
+ /** The instruction that produced the current version. */
1561
+ editedFrom?: string;
1507
1562
  /**
1508
- * Get component metadata
1563
+ * Prior versions, newest last. The body itself is archived on disk as
1564
+ * `<fileBase>.v<version>.ts`; this records what changed and who did it.
1509
1565
  */
1510
- getComponentMetadata(): Record<string, any>;
1566
+ history?: ScriptVersionRecord[];
1567
+ }
1568
+ /**
1569
+ * A durable, user-stated rendering choice for a recipe. Survives data edits.
1570
+ */
1571
+ interface ScriptDisplayPreference {
1572
+ /** Component types the user's chosen rendering resolved to, e.g. ["DynamicBarChart"]. */
1573
+ componentTypes: string[];
1574
+ /** The instruction itself — carries nuance the types can't ("horizontal", "sorted descending"). */
1575
+ instruction: string;
1576
+ /** ISO timestamp of the display edit that set this. */
1577
+ at: string;
1578
+ }
1579
+ /** One superseded version of a recipe body (see ScriptStore.commitEdit). */
1580
+ interface ScriptVersionRecord {
1581
+ /** Version number this record superseded (i.e. the OLD version). */
1582
+ version: number;
1583
+ /** ISO timestamp of the edit that superseded it. */
1584
+ at: string;
1585
+ /** userId who made the edit, when known. */
1586
+ by?: string;
1587
+ /** The edit instruction that caused the supersede. */
1588
+ instruction?: string;
1589
+ /** One-line summary of what changed, for the version picker. */
1590
+ changeSummary?: string;
1591
+ /** sha256 of the superseded body — pairs with `<fileBase>.v<version>.ts`. */
1592
+ bodyHash?: string;
1593
+ }
1594
+ interface ScriptParameter {
1595
+ /** Parameter name (used in script body as params.name) */
1596
+ name: string;
1597
+ /** Parameter type */
1598
+ type: 'string' | 'number' | 'date' | 'date_range' | 'enum' | 'boolean';
1599
+ /** Whether this parameter is required */
1600
+ required: boolean;
1601
+ /** Default value if not provided */
1602
+ default?: any;
1603
+ /** For enum type — maps user-facing values to internal values */
1604
+ enumValues?: Record<string, string>;
1605
+ /** Human-readable description (used in the matcher LLM prompt) */
1606
+ description: string;
1607
+ }
1608
+ /**
1609
+ * A reusable component binding captured when a script is authored. Stored on
1610
+ * the recipe so tier-high replays rebuild components deterministically (rebind
1611
+ * to fresh queryIds) instead of re-running the component-picker LLM.
1612
+ */
1613
+ interface ScriptComponentSpec {
1614
+ /** Registered component name (e.g. "DynamicBarChart") — matched against the available component library. */
1615
+ componentType: string;
1616
+ /** `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). */
1617
+ sourceRef: string;
1618
+ /** Present only when sourceRef === 'federation' — the DuckDB SQL to re-execute on replay. */
1619
+ federationSql?: string;
1620
+ /** Present only when sourceRef === 'markdown' — the narrative text to render on replay (markdown has no data source, so its content must be persisted). */
1621
+ content?: string;
1622
+ title?: string;
1623
+ description?: string;
1624
+ /** Validated axis/value keys + aggregation — all referencing real columns of the bound source. */
1625
+ config: Record<string, any>;
1626
+ }
1627
+ /**
1628
+ * Result from executing a script via ScriptRunner.
1629
+ */
1630
+ interface ScriptResult {
1631
+ /** Whether the script executed successfully */
1632
+ success: boolean;
1633
+ /** Combined data from all queries */
1634
+ data: any[];
1635
+ /** Individual query results tracked during execution */
1636
+ executedQueries: ScriptQueryResult[];
1637
+ /** Error message if failed */
1638
+ error?: string;
1639
+ /**
1640
+ * Where in the lifecycle the error occurred. Lets MainAgent's fix-loop
1641
+ * decide between "rewrite the whole draft" (compile) and "patch the
1642
+ * specific line" (runtime).
1643
+ */
1644
+ errorPhase?: 'compile' | 'runtime' | 'timeout' | 'ipc';
1645
+ /** Total execution time in milliseconds */
1646
+ executionTimeMs: number;
1647
+ }
1648
+ /**
1649
+ * A single query executed during script runtime.
1650
+ * Tracked by ScriptContext for component generation and debugging.
1651
+ */
1652
+ interface ScriptQueryResult {
1653
+ /** Source tool ID */
1654
+ sourceId: string;
1655
+ /** Human-readable source name */
1656
+ sourceName: string;
1657
+ /** The SQL that was executed */
1658
+ sql: string;
1659
+ /** Result data rows */
1660
+ data: any[];
1661
+ /** Number of rows returned */
1662
+ count: number;
1663
+ /** Total rows that matched before limit (if available) */
1664
+ totalCount?: number;
1665
+ /** Query execution time in milliseconds */
1666
+ executionTimeMs: number;
1667
+ /**
1668
+ * True for rows that did NOT come from a real SQL execution — either a
1669
+ * ctx.emit() dataset or the synthesized "computed:_final" entry that
1670
+ * carries the script's post-JS returned data. The component generator
1671
+ * uses this to route the resulting component through the script_dataset
1672
+ * sentinel toolId so the frontend resolves it via the queryCache short-circuit.
1673
+ */
1674
+ virtual?: boolean;
1675
+ }
1676
+ /**
1677
+ * Match tier returned by the LLM script matcher.
1678
+ *
1679
+ * - 'high': the script answers the question directly; only parameter values
1680
+ * may differ. The runtime replays it with extracted params (cheapest path).
1681
+ * - 'near': the script answers a STRUCTURALLY similar question but needs
1682
+ * body modification (different metric, dimension, table, filter shape).
1683
+ * The runtime forks the parent and adapts the body via MainAgent's normal
1684
+ * write_script + execute_script loop — no SourceAgent dispatch needed.
1685
+ * See backend/docs/SCRIPT-FLOW-FORK-ADAPT.md for the full design.
1686
+ * - 'edit': the user is INSTRUCTING a change to the script that produced the
1687
+ * previous answer (not asking a new question). Only reachable when the turn
1688
+ * carries a ScriptBinding — without one the matcher coerces it to 'none', so
1689
+ * a prompt regression can't turn this into a loose similarity path. The
1690
+ * runtime runs MainAgent in edit mode and commits the result in place.
1691
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md.
1692
+ * - 'none': no script is relevant; full agent flow runs.
1693
+ */
1694
+ type MatchTier = 'high' | 'near' | 'edit' | 'none';
1695
+ /**
1696
+ * Which recipe produced the answer the user is currently looking at.
1697
+ *
1698
+ * Set whenever a turn's answer came from a script (replay, fresh authoring, or
1699
+ * a committed edit) and carried on the UIBlock + saved conversation row. Two
1700
+ * consumers:
1701
+ * 1. The matcher — the 'edit' tier is ONLY reachable when a binding exists, so
1702
+ * "use mode instead of mean" resolves to a concrete script instead of being
1703
+ * matched on keywords it shares with no script name.
1704
+ * 2. Cache invalidation — after an edit commits, conversations bound to that
1705
+ * recipeId must be dropped, or the exact-match cache replays the pre-edit
1706
+ * answer and the edit looks like a no-op.
1707
+ *
1708
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 1.
1709
+ */
1710
+ interface ScriptBinding {
1711
+ recipeId: string;
1712
+ /** Params the script ran with — the edit's starting point. */
1713
+ params: Record<string, any>;
1714
+ /** Recipe name at bind time (matcher catalog + user-facing confirmation). */
1715
+ name: string;
1716
+ /** Columns the last run returned — grounds the editor without a re-query. */
1717
+ columns?: string[];
1718
+ /**
1719
+ * The question that produced this answer. Required for disambiguation when a
1720
+ * thread ran several scripts — without it the candidates are just names and
1721
+ * the matcher cannot resolve "use the mode for the WSP one".
1722
+ */
1723
+ userPrompt?: string;
1724
+ }
1725
+ /**
1726
+ * Result from the LLM-based script matcher.
1727
+ *
1728
+ * For `tier: 'high'`, `extractedParams` carries the values to pass to the
1729
+ * existing script. For `tier: 'near'`, `gaps` and `modificationHint` describe
1730
+ * what the fork-author needs to change in the parent body.
1731
+ */
1732
+ interface ScriptMatch {
1733
+ /** The matched script recipe */
1734
+ recipe: ScriptRecipe;
1735
+ /** Match tier — see MatchTier docs */
1736
+ tier: MatchTier;
1737
+ /** Similarity score (0-1, derived from LLM tier) */
1738
+ similarity: number;
1739
+ /**
1740
+ * Legacy confidence level. Mirrors `tier === 'high'`/`'near'` for now;
1741
+ * kept so existing callers compile while we migrate to tier-based logic.
1742
+ */
1743
+ confidence: 'high' | 'medium';
1744
+ /** Parameters extracted from the user question by the LLM (tier='high') */
1745
+ extractedParams?: Record<string, any>;
1746
+ /**
1747
+ * Proper nouns the question named, VERBATIM, with the parameter each fills.
1748
+ *
1749
+ * The matcher must NOT resolve these itself. Observed live: it substituted the
1750
+ * customer name "PREMER ENRGES PHOTOOTAIC" into an id parameter believing that
1751
+ * "the runtime will resolve the customer name to its PartyId(s)" — nothing on
1752
+ * the replay path does, and `WHERE BillingPartyId IN (PREMER ENRGES
1753
+ * PHOTOOTAIC)` reached SQL. Reported here, the runtime resolves them before
1754
+ * replay and rejects the match when one cannot be resolved.
1755
+ */
1756
+ mentions?: Array<{
1757
+ text: string;
1758
+ param?: string;
1759
+ }>;
1760
+ /** Business concepts the question relies on (e.g. "outstanding balance"). */
1761
+ entityTypes?: string[];
1762
+ /** What the user question needs that the parent doesn't cover (tier='near') */
1763
+ gaps?: string[];
1764
+ /** One-sentence description of the change the fork-author should make (tier='near') */
1765
+ modificationHint?: string;
1766
+ /**
1767
+ * Which HALF of the recipe the edit targets (tier='edit'). A recipe has two
1768
+ * independently editable halves:
1769
+ * - 'data' — the scriptBody: how the rows are produced (aggregation,
1770
+ * filters, joins, grouping). Runs MainAgent in edit mode.
1771
+ * - 'display' — the component specs: how those SAME rows are shown (chart
1772
+ * type, orientation, columns, labels). Replays the proven SQL
1773
+ * and regenerates the specs — never authors a script.
1774
+ * Defaults to 'data' when the matcher omits it.
1775
+ */
1776
+ editTarget?: 'data' | 'display';
1777
+ /**
1778
+ * Self-contained restatement of the change the user asked for (tier='edit').
1779
+ * MUST have pronouns/deixis resolved ("this", "it", "that column") — the edit
1780
+ * prompt never sees the conversation history, so an unresolved instruction is
1781
+ * unusable downstream.
1782
+ */
1783
+ editInstruction?: string;
1784
+ /** Why the matcher made this choice (for logs and telemetry) */
1785
+ reasoning?: string;
1786
+ }
1787
+
1788
+ /**
1789
+ * Multi-Agent Architecture Types
1790
+ *
1791
+ * Defines interfaces for the hierarchical agent system:
1792
+ * - Main Agent: ONE LLM.streamWithTools() call with source agent tools
1793
+ * - Source Agents: independent agents that query individual data sources
1794
+ *
1795
+ * The main agent sees only source summaries. When it calls a source tool,
1796
+ * the SourceAgent runs independently (own LLM, own retries) and returns clean data.
1797
+ */
1798
+
1799
+ /**
1800
+ * Per-entity detail: name, row count, and column names.
1801
+ * Gives the main agent enough context to route to the right source.
1802
+ */
1803
+ interface EntityDetail {
1804
+ /** Entity name (table, sheet, endpoint) */
1805
+ name: string;
1806
+ /** Approximate row count */
1807
+ rowCount?: number;
1808
+ /** Column/field names */
1809
+ columns: string[];
1810
+ /** Entity-level semantic summary (what the table means) — for main-agent routing. */
1811
+ summary?: string;
1812
+ }
1813
+ /**
1814
+ * Representation of a data source for the main agent.
1815
+ * Contains entity names WITH column names so the LLM can route accurately.
1816
+ */
1817
+ interface SourceSummary {
1818
+ /** Source ID (matches tool ID prefix) */
1819
+ id: string;
1820
+ /** Human-readable source name */
1821
+ name: string;
1822
+ /** Source type: postgres, excel, rest_api, etc. */
1823
+ type: string;
1824
+ /** Brief description of what data this source contains */
1825
+ description: string;
1826
+ /** Detailed entity info with column names for routing */
1827
+ entityDetails: EntityDetail[];
1828
+ /** The tool ID associated with this source */
1829
+ toolId: string;
1830
+ }
1831
+ /**
1832
+ * What a source agent returns after querying its data source.
1833
+ * The main agent uses this to analyze and compose the final response.
1834
+ */
1835
+ interface SourceAgentResult {
1836
+ /** Source ID */
1837
+ sourceId: string;
1838
+ /** Source name */
1839
+ sourceName: string;
1840
+ /** Whether the query succeeded */
1841
+ success: boolean;
1842
+ /** Result data rows */
1843
+ data: any[];
1844
+ /** Metadata about the query execution */
1845
+ metadata: SourceAgentMetadata;
1846
+ /** Tool execution info for the last successful query (backward compat) */
1847
+ executedTool: ExecutedToolInfo;
1848
+ /** All successful tool executions (primary + follow-up queries) */
1849
+ allExecutedTools?: ExecutedToolInfo[];
1850
+ /** Error message if failed */
1851
+ error?: string;
1852
+ }
1853
+ interface SourceAgentMetadata {
1854
+ /** Total rows that matched the query (before limit) */
1855
+ totalRowsMatched: number;
1856
+ /** Rows actually returned (after limit) */
1857
+ rowsReturned: number;
1858
+ /** Whether the result was truncated by the row limit */
1859
+ isLimited: boolean;
1860
+ /** The query/params that were executed */
1861
+ queryExecuted?: string;
1862
+ /** Execution time in milliseconds */
1863
+ executionTimeMs: number;
1864
+ }
1865
+ /**
1866
+ * A pre-built, multi-step UI flow registered with the SDK.
1867
+ *
1868
+ * When the main agent decides a user's question matches a workflow's whenToUse
1869
+ * trigger, it picks the workflow instead of running source agents / generating
1870
+ * dashboard components. The LLM extracts the workflow's required props from the
1871
+ * prompt (using `propsSchema` as the tool input_schema) and the SDK returns the
1872
+ * workflow component directly — no analysis text, no chart generation. The
1873
+ * frontend renders the registered workflow component with the LLM-extracted
1874
+ * props.
1875
+ */
1876
+ interface WorkflowDescriptor {
1877
+ /** Unique workflow id (used as the LLM tool name) */
1878
+ id: string;
1879
+ /** Component name on the frontend (matches the registered React component) */
1880
+ name: string;
1881
+ /** Short human-readable description of what this workflow does */
1882
+ description: string;
1883
+ /**
1884
+ * 1–2 sentence trigger condition. The LLM uses this to decide if the
1885
+ * user's prompt matches this workflow. Be specific — e.g.
1886
+ * "User wants to *initiate* an inventory transfer (review + submit POs),
1887
+ * not just see analysis or charts."
1888
+ */
1889
+ whenToUse: string;
1890
+ /**
1891
+ * JSON-schema-style description of the props the workflow needs. Becomes
1892
+ * the LLM tool's input_schema, so the model fills these from the prompt.
1893
+ * Use the same shape as `params` on direct tools — string descriptors with
1894
+ * an optional "(optional)" suffix.
1895
+ *
1896
+ * Example:
1897
+ * ```
1898
+ * {
1899
+ * selectedStore: 'object — { id, name } of the source branch',
1900
+ * minROI: 'number (optional) — only show transfers with ROI ≥ this',
1901
+ * }
1902
+ * ```
1903
+ */
1904
+ propsSchema: Record<string, string>;
1905
+ /**
1906
+ * Optional: static prop defaults merged with LLM-extracted props before
1907
+ * the component is returned. Useful for things like the embedded
1908
+ * `externalTool` config that the workflow uses to fetch its own data.
1909
+ */
1910
+ defaultProps?: Record<string, any>;
1911
+ }
1912
+ /**
1913
+ * The workflow selection captured during a routing call.
1914
+ * Set on AgentResponse when the LLM picks a workflow tool.
1915
+ */
1916
+ interface SelectedWorkflow {
1917
+ /** Component name (matches WorkflowDescriptor.name) */
1918
+ name: string;
1919
+ /** Props extracted from the prompt + merged with workflow.defaultProps */
1920
+ props: Record<string, any>;
1921
+ }
1922
+ /**
1923
+ * Set when this turn applies a user-directed edit to an existing script instead
1924
+ * of authoring a new one. MainAgent keeps the SAME harness (tools, loop,
1925
+ * write_script/execute_script verification) and swaps only the system prompt —
1926
+ * `agent-main-edit` instead of `agent-main`.
1927
+ *
1928
+ * The two framings contradict each other on whether to query a source first, so
1929
+ * they must never be resident in one rendered prompt.
1930
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § D3.
1931
+ */
1932
+ interface EditContext {
1933
+ /** Recipe being edited — the shadow draft records it as parentId. */
1934
+ recipeId: string;
1935
+ parentName: string;
1936
+ parentBody: string;
1937
+ /** Self-contained restatement of the change (from the matcher). */
1938
+ instruction: string;
1939
+ /** Columns the last run returned — grounds the edit without a re-query. */
1940
+ lastResultColumns?: string[];
1941
+ /** Rendered parameter list of the parent, for the prompt. */
1942
+ parentParams?: string;
1943
+ }
1944
+ /**
1945
+ * The complete response from the multi-agent system.
1946
+ * Contains everything needed for text display + component generation.
1947
+ */
1948
+ interface AgentResponse {
1949
+ /** Generated text response (analysis of the data) */
1950
+ text: string;
1951
+ /** All executed tools across all source agents (for component generation) */
1952
+ executedTools: ExecutedToolInfo[];
1953
+ /** Individual results from each source agent */
1954
+ sourceResults: SourceAgentResult[];
1955
+ /**
1956
+ * Populated when MainAgent wrote AND successfully executed a script during its turn.
1957
+ * Caller (agent-user-response.ts) persists it via ScriptStore.save().
1958
+ * Absent when MainAgent didn't write one (trivial question / all attempts failed).
1959
+ */
1960
+ savedScript?: AgentWrittenScript;
1961
+ /**
1962
+ * Set when the LLM routed the question to a registered workflow component.
1963
+ * When present, the upstream caller should skip component generation and
1964
+ * return this workflow as the response.
1965
+ */
1966
+ workflow?: SelectedWorkflow;
1967
+ /**
1968
+ * Validated component specs the agent authored via `render_components`.
1969
+ * When present the caller assembles the dashboard from these instead of
1970
+ * running the component-generation LLM. Absent when the agent didn't call
1971
+ * the tool — the caller then falls back to `generateScriptComponents`.
1972
+ * See backend/docs/COMPONENT-GENERATION-V2.md §2.2.
1973
+ */
1974
+ componentSpecs?: ScriptComponentSpec[];
1975
+ /** Layout title/description from the same `render_components` call. */
1976
+ componentLayout?: {
1977
+ title: string;
1978
+ description?: string;
1979
+ };
1980
+ }
1981
+ /**
1982
+ * A script MainAgent authored + verified during its turn. Shape aligns with
1983
+ * what ScriptStore.save() needs — minus store-assigned fields (id, timestamps, counts).
1984
+ */
1985
+ interface AgentWrittenScript {
1986
+ /**
1987
+ * `ScriptRecipe.id` of the draft that was authored + verified during this turn.
1988
+ * The caller passes this to `ScriptStore.promoteToVerified(recipeId, …)` to
1989
+ * flip the draft to verified status and (when possible) drop the turn-suffix
1990
+ * from its filename.
1991
+ */
1992
+ recipeId: string;
1993
+ name: string;
1994
+ intentDescription: string;
1995
+ tags: string[];
1996
+ parameters: Array<{
1997
+ name: string;
1998
+ type: 'string' | 'number' | 'date' | 'date_range' | 'enum' | 'boolean';
1999
+ required: boolean;
2000
+ default?: any;
2001
+ enumValues?: Record<string, string>;
2002
+ description: string;
2003
+ }>;
2004
+ scriptBody: string;
2005
+ /** Source IDs referenced by the script (extracted from ctx.query calls) */
2006
+ sourceIds: string[];
2007
+ /** Tables referenced in the script's SQL (regex-extracted) */
2008
+ tables: string[];
2009
+ /** Executed queries from the verified run — fed to component generation */
2010
+ executedQueries: Array<{
2011
+ sourceId: string;
2012
+ sourceName: string;
2013
+ sql: string;
2014
+ data: any[];
2015
+ count: number;
2016
+ totalCount?: number;
2017
+ executionTimeMs: number;
2018
+ /**
2019
+ * True for synthetic entries (ctx.emit datasets, the computed:_final
2020
+ * post-JS data). The component generator routes virtual sources through
2021
+ * the script_dataset sentinel toolId so the frontend resolves them via
2022
+ * queryCache instead of attempting to re-execute SQL.
2023
+ */
2024
+ virtual?: boolean;
2025
+ }>;
2026
+ }
2027
+ /**
2028
+ * Configuration for the multi-agent system.
2029
+ * Controls limits, models, and behavior.
2030
+ */
2031
+ interface AgentConfig {
2032
+ /** Max rows shown to the UI preview / inlined per source (default: 10) */
2033
+ maxRowsPerSource: number;
2034
+ /**
2035
+ * Max rows a source query may FETCH from the DB server-side (default: 2000).
2036
+ * Decoupled from what the main agent is shown: the full result is fetched and
2037
+ * summarized (bounded), but only a small/complete slice enters LLM context.
2038
+ * This lets small lookups (benchmark maps) arrive COMPLETE without letting
2039
+ * large results blow up context.
2040
+ */
2041
+ maxRowsFetched: number;
2042
+ /** Model for the main agent (routing + analysis in one LLM call) */
2043
+ mainAgentModel: string;
2044
+ /** Model for source agent query generation */
2045
+ sourceAgentModel: string;
2046
+ /** API key for LLM calls */
2047
+ apiKey?: string;
2048
+ /** Max retry attempts per source agent */
2049
+ maxRetries: number;
2050
+ /** Max tool calling iterations for the main agent loop */
2051
+ maxIterations: number;
2052
+ /** Global knowledge base context (static, same for all users/questions — cached in system prompt) */
2053
+ globalKnowledgeBase?: string;
2054
+ /** Per-request knowledge base context (user-specific + query-matched — dynamic, not cached) */
2055
+ knowledgeBaseContext?: string;
2056
+ /** Collections registry (ChromaDB search hooks) for embedding-based schema + source search */
2057
+ collections?: any;
2058
+ /** Optional project ID for scoping embedding searches */
2059
+ projectId?: string;
2060
+ }
2061
+ /**
2062
+ * Default agent configuration
2063
+ */
2064
+ declare const DEFAULT_AGENT_CONFIG: AgentConfig;
2065
+
2066
+ /**
2067
+ * ScriptRecipeStore — injected metadata backend for the script flow.
2068
+ *
2069
+ * The SDK is standalone (no DB dependency). The backend implements this
2070
+ * interface over Postgres (full-text search + atomic counters) and injects it
2071
+ * via `collections['script-recipes']`, exactly like `collections['source-embeddings']`.
2072
+ * `ScriptStore` consumes it for all METADATA operations while keeping the
2073
+ * executable body on disk as scripts-store/<fileBase>.ts.
2074
+ *
2075
+ * All metadata rows are plain JSON (no scriptBody — that lives on disk).
2076
+ * See backend/docs/SCRIPT-FLOW-SCALING-ISSUES.md (#1, #3, #7).
2077
+ */
2078
+
2079
+ /** One recipe's metadata as stored in Postgres (mirrors the script_recipes table). */
2080
+ interface ScriptRecipeMetaRow {
2081
+ id: string;
2082
+ projectId?: string | null;
2083
+ version: number;
2084
+ name: string;
2085
+ intentDescription: string;
2086
+ tags: string[] | null;
2087
+ createdFrom: string | null;
2088
+ sourceIds: string[] | null;
2089
+ tables: string[] | null;
2090
+ parameters: ScriptParameter[] | null;
2091
+ components?: ScriptComponentSpec[] | null;
2092
+ displayPreference?: ScriptDisplayPreference | null;
2093
+ fileBase: string;
2094
+ bodyHash?: string | null;
2095
+ successCount: number;
2096
+ failureCount: number;
2097
+ lastUsed: string | null;
2098
+ parentId?: string | null;
2099
+ forkDepth?: number | null;
2100
+ forkReason?: string | null;
2101
+ status: 'draft' | 'verified' | string;
2102
+ turnId?: string | null;
2103
+ editedBy?: string | null;
2104
+ editedFrom?: string | null;
2105
+ history?: ScriptVersionRecord[] | null;
2106
+ lastError?: {
2107
+ phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
2108
+ message: string;
2109
+ at: string;
2110
+ attempt: number;
2111
+ } | null;
2112
+ createdAt?: string | null;
2113
+ updatedAt?: string | null;
2114
+ }
2115
+ interface ScriptRecipeStore {
2116
+ /** FTS shortlist of healthy verified recipes for the matcher (metadata only). */
2117
+ search(params: {
2118
+ prompt: string;
2119
+ projectId?: string;
2120
+ limit?: number;
2121
+ }): Promise<ScriptRecipeMetaRow[]>;
2122
+ /** Fetch one recipe by id (any status). */
2123
+ getById(id: string): Promise<ScriptRecipeMetaRow | null>;
2124
+ /** Count healthy verified recipes (drives the "any scripts?" gate). */
2125
+ count(params?: {
2126
+ projectId?: string;
2127
+ }): Promise<number>;
2128
+ /** Insert or update a recipe row (keyed by id). */
2129
+ upsert(row: ScriptRecipeMetaRow): Promise<void>;
2130
+ /** Atomically bump counters / last-used. */
2131
+ updateStats(id: string, patch: {
2132
+ successDelta?: number;
2133
+ failureDelta?: number;
2134
+ lastUsed?: string;
2135
+ }): Promise<void>;
2136
+ /** Flip a draft to verified, applying provenance + optional fork lineage. */
2137
+ promote(id: string, patch: {
2138
+ sourceIds: string[];
2139
+ tables: string[];
2140
+ fileBase?: string;
2141
+ parentId?: string;
2142
+ forkDepth?: number;
2143
+ forkReason?: string;
2144
+ components?: ScriptComponentSpec[];
2145
+ }): Promise<ScriptRecipeMetaRow | null>;
2146
+ /**
2147
+ * Commit a verified edit onto an EXISTING recipe: bump `version`, replace the
2148
+ * body-bearing metadata, append a history record, reset health counters, and
2149
+ * clear the component specs (they were validated against the old shape).
2150
+ * Returns the updated row, or null when the target is gone.
2151
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 5.
2152
+ */
2153
+ commitEdit?(id: string, patch: {
2154
+ name?: string;
2155
+ intentDescription?: string;
2156
+ tags?: string[];
2157
+ parameters?: ScriptParameter[];
2158
+ bodyHash: string;
2159
+ /** New on-disk stem when the edit renamed the script; omitted otherwise. */
2160
+ fileBase?: string;
2161
+ sourceIds?: string[];
2162
+ tables?: string[];
2163
+ editedBy?: string;
2164
+ editedFrom?: string;
2165
+ historyEntry: {
2166
+ version: number;
2167
+ at: string;
2168
+ by?: string;
2169
+ instruction?: string;
2170
+ changeSummary?: string;
2171
+ bodyHash?: string;
2172
+ };
2173
+ }): Promise<ScriptRecipeMetaRow | null>;
2174
+ /** Stamp a draft's last execution error. */
2175
+ recordDraftError(id: string, err: {
2176
+ phase: string;
2177
+ message: string;
2178
+ attempt: number;
2179
+ at: string;
2180
+ }): Promise<void>;
2181
+ /** Delete a recipe row (body file removed separately). */
2182
+ remove(id: string): Promise<void>;
2183
+ /** True if `fileBase` is taken by a different recipe in this project. */
2184
+ fileBaseTaken(fileBase: string, excludeId: string, projectId?: string): Promise<boolean>;
2185
+ }
2186
+ /** Pull the injected store off the collections bag (or null if not wired). */
2187
+ declare function resolveScriptRecipeStore(collections: any): ScriptRecipeStore | null;
2188
+
2189
+ /**
2190
+ * ScriptStore — Postgres metadata + on-disk body for script recipes.
2191
+ *
2192
+ * Split of responsibilities:
2193
+ * - METADATA → injected `ScriptRecipeStore` (Postgres FTS + atomic counters),
2194
+ * resolved from `collections['script-recipes']`.
2195
+ * - BODY → scripts-store/<fileBase>.ts, editable in your IDE. Written
2196
+ * atomically (temp + rename); `bodyHash` (sha256) detects edits.
2197
+ *
2198
+ * The old "read every file every turn + send the whole catalog to the LLM"
2199
+ * matcher is gone — matching is `store.search(prompt)` (FTS shortlist). The
2200
+ * draft/verified filename dance is gone too: `status` is a DB column and the
2201
+ * file keeps a stable `<fileBase>.ts` name across promotion.
2202
+ *
2203
+ * When no metadata store is injected, the store degrades to a safe no-op
2204
+ * (count 0 → script flow disabled) instead of crashing.
2205
+ *
2206
+ * See backend/docs/SCRIPT-FLOW-SCALING-ISSUES.md.
2207
+ */
2208
+
2209
+ interface SaveDraftInput {
2210
+ /** Reuse an existing draft (retry); omit to mint a new one. */
2211
+ recipeId?: string;
2212
+ /** Per-turn unique suffix, stable across retries within the turn. */
2213
+ turnId: string;
2214
+ name: string;
2215
+ intentDescription: string;
2216
+ tags: string[];
2217
+ parameters: ScriptParameter[];
2218
+ scriptBody: string;
2219
+ createdFrom: string;
2220
+ /**
2221
+ * Set when this draft is a SHADOW of an existing recipe being edited. The
2222
+ * draft is verified independently and merged back onto the parent via
2223
+ * `commitEdit`, so the working script is never clobbered by an edit that
2224
+ * turns out not to run. See backend/docs/SCRIPT-EDIT-DESIGN.md § D5.
2225
+ */
2226
+ parentId?: string;
2227
+ }
2228
+ interface PromoteToVerifiedInput {
2229
+ sourceIds: string[];
2230
+ tables: string[];
2231
+ parentId?: string;
2232
+ forkDepth?: number;
2233
+ forkReason?: string;
2234
+ components?: ScriptComponentSpec[];
2235
+ }
2236
+ interface ScriptStoreOptions {
2237
+ /** Explicit metadata store, or resolved from `collections['script-recipes']`. */
2238
+ store?: ScriptRecipeStore | null;
2239
+ collections?: any;
2240
+ /** Body directory (defaults to <cwd>/scripts-store). */
2241
+ baseDir?: string;
2242
+ /** Project scope stamped on every row. */
2243
+ projectId?: string;
2244
+ }
2245
+ /**
2246
+ * Normalize a scriptBody into the on-disk form (strip a leading comment block,
2247
+ * ensure `export async function getData`). Exported for MainAgent.
2248
+ */
2249
+ declare function normalizeScriptBody(scriptBody: string): string;
2250
+ declare class ScriptStore {
2251
+ private store;
2252
+ private storeDir;
2253
+ private projectId?;
2254
+ constructor(opts?: ScriptStoreOptions);
2255
+ /** Whether a metadata store is wired (matcher / authoring are gated on this). */
2256
+ hasStore(): boolean;
2257
+ /** Number of healthy verified recipes (gates the script-matching path). */
2258
+ count(): Promise<number>;
2259
+ /**
2260
+ * FTS shortlist for the matcher (metadata only — bodies are loaded lazily by
2261
+ * `get()` once the LLM picks one). Returns verified, healthy recipes ranked
2262
+ * by relevance.
2263
+ */
2264
+ search(prompt: string, limit?: number): Promise<ScriptRecipe[]>;
2265
+ /** Fetch one recipe by id with its body loaded from disk. */
2266
+ get(id: string): Promise<ScriptRecipe | null>;
2267
+ /** Create or update a recipe (metadata upsert + body write when changed). */
2268
+ save(recipe: ScriptRecipe): Promise<void>;
2269
+ /**
2270
+ * Persist (or update) a draft. Within a turn, retries that pass the same
2271
+ * `recipeId` overwrite the same row + file; a fresh `recipeId` mints a new
2272
+ * draft. The body is visible at scripts-store/<fileBase>.ts immediately.
2273
+ */
2274
+ saveDraft(input: SaveDraftInput): Promise<ScriptRecipe>;
2275
+ /** Stamp a draft's last execution error (metadata only). */
2276
+ recordDraftError(recipeId: string, err: {
2277
+ phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
2278
+ message: string;
2279
+ attempt: number;
2280
+ }): Promise<void>;
2281
+ /**
2282
+ * Promote a successfully-executed draft into a verified script.
2283
+ * The on-disk body already exists at <fileBase>.ts (written at write_script
2284
+ * time) and keeps its name — only the DB row flips status + provenance.
2285
+ */
2286
+ promoteToVerified(recipeId: string, input: PromoteToVerifiedInput): Promise<ScriptRecipe | null>;
2287
+ /**
2288
+ * Commit a user-directed edit: merge a VERIFIED shadow draft back onto the
2289
+ * recipe it was editing, as version N+1 under the SAME recipe id.
2290
+ *
2291
+ * Keeping the id stable is the point of the whole feature — every cached
2292
+ * conversation, `script_dataset` regeneration descriptor and persisted
2293
+ * component spec already points at it, so the correction applies retroactively
2294
+ * to replays instead of stranding them on the old body.
2295
+ *
2296
+ * Steps (see backend/docs/SCRIPT-EDIT-DESIGN.md § Component 5):
2297
+ * 1. archive the current body as `<fileBase>.v<version>.ts`
2298
+ * 2. write the edited body to the original fileBase (atomic)
2299
+ * 3. version++, copy name/description/params, append a history record
2300
+ * 4. reset health counters — the pre-edit failure history is stale
2301
+ * 5. clear component specs — they were validated against the OLD shape
2302
+ * 6. delete the shadow draft (row + file)
2303
+ *
2304
+ * Returns the updated recipe, or null when the commit could not be applied
2305
+ * (caller should then fall back to treating the draft as a new script).
2306
+ */
2307
+ commitEdit(targetId: string, draft: {
2308
+ recipeId: string;
2309
+ name?: string;
2310
+ intentDescription?: string;
2311
+ tags?: string[];
2312
+ parameters?: ScriptParameter[];
2313
+ scriptBody: string;
2314
+ sourceIds?: string[];
2315
+ tables?: string[];
2316
+ }, meta: {
2317
+ instruction?: string;
2318
+ changeSummary?: string;
2319
+ editedBy?: string;
2320
+ }): Promise<ScriptRecipe | null>;
2321
+ /**
2322
+ * Drop a draft (row + body file). MainAgent calls this at end-of-turn when a
2323
+ * draft was authored but never verified — failed drafts are never matched, so
2324
+ * deleting them immediately avoids unbounded accumulation (#5). No-op if the
2325
+ * recipe isn't a draft (so a promoted/verified script is never removed here).
2326
+ */
2327
+ discardDraft(recipeId: string): Promise<void>;
2328
+ /** Delete a recipe (row + body file). */
2329
+ delete(id: string): Promise<void>;
2330
+ /** Record a successful execution (atomic counter bump). */
2331
+ recordSuccess(id: string): Promise<void>;
2332
+ /** Record a failed execution (atomic counter bump). */
2333
+ recordFailure(id: string): Promise<void>;
2334
+ /** Absolute path to the .ts body for a recipe (used by the runner/MainAgent). */
2335
+ getScriptPath(recipe: ScriptRecipe): string;
2336
+ private removeById;
2337
+ private rowToRecipe;
2338
+ private recipeToRow;
2339
+ /** slug of name, with a short id suffix when the bare slug is already taken. */
2340
+ private computeFileBase;
2341
+ private toSlug;
2342
+ private hash;
2343
+ private bodyPath;
2344
+ private readBody;
2345
+ /** Directory holding superseded bodies. Dot-prefixed so IDEs/`ls` hide it. */
2346
+ private get archiveDir();
2347
+ /**
2348
+ * Archive a superseded body as `.versions/<fileBase>.v<n>.ts`.
2349
+ *
2350
+ * Kept out of the main store directory on purpose — see commitEdit step 1.
2351
+ * To roll back: copy the file back over `scripts-store/<fileBase>.ts`.
2352
+ */
2353
+ private writeArchive;
2354
+ /**
2355
+ * Move a recipe's archived versions to a new prefix when its fileBase changes,
2356
+ * so all versions of one recipe stay grouped. Without this, two renames would
2357
+ * scatter a single recipe's history across three prefixes in `.versions/` with
2358
+ * nothing linking them back to the live script.
2359
+ */
2360
+ private renameArchives;
2361
+ /** Atomic body write (temp + rename) so concurrent reads never see a partial file. */
2362
+ private writeBody;
2363
+ private unlinkBody;
2364
+ }
2365
+
2366
+ /**
2367
+ * Main Agent (Orchestrator)
2368
+ *
2369
+ * A single LLM.streamWithTools() call that handles everything:
2370
+ * - Routing: decides which source(s) to query based on summaries
2371
+ * - Querying: calls source tools (each wraps an independent SourceAgent)
2372
+ * - Direct tools: calls pre-built function tools directly with LLM-provided params
2373
+ * - Re-querying: if data is wrong/incomplete, calls tools again with modified intent
2374
+ * - Analysis: generates final text response from the data
2375
+ *
2376
+ * Two tool types:
2377
+ * - "source" tools: main agent sees summaries, SourceAgent handles SQL generation independently
2378
+ * - "direct" tools: main agent calls fn() directly with structured params (no SourceAgent)
2379
+ */
2380
+
2381
+ declare class MainAgent {
2382
+ private externalTools;
2383
+ private workflows;
2384
+ private config;
2385
+ private streamBuffer;
2386
+ /**
2387
+ * Optional: when provided, MainAgent exposes the `write_script` /
2388
+ * `execute_script` tools to the LLM and persists drafts to disk via the
2389
+ * store. Headless callers (alert analyzer, metric resolver) omit these to
2390
+ * suppress script authoring entirely — drafts would otherwise leak onto
2391
+ * disk with no caller to promote or clean them up.
2392
+ */
2393
+ private scriptStore;
2394
+ private turnId;
2395
+ private createdFromPrompt;
2396
+ private scriptState;
2397
+ /** Answer-flow component catalog (filtered + projected by the caller). */
2398
+ private componentCatalog;
2399
+ /** Specs accepted by `render_components` this turn; empty until it succeeds. */
2400
+ /**
2401
+ * Entities resolved this turn, appended verbatim to every source dispatch.
2402
+ *
2403
+ * Not left to the model to copy into its intent prose: observed live, the
2404
+ * MainAgent wrote names instead of ids on the first dispatch AND re-spelled
2405
+ * 'F D C LIMITED' as 'FDC Limited', so the source agent fell back to
2406
+ * `PartyName LIKE '%FDC%'` and found 2 of 3 customers. Carrying the ids
2407
+ * structurally makes that impossible rather than instruction-dependent.
2408
+ */
2409
+ private resolvedEntities;
2410
+ private componentSpecs;
2411
+ private componentLayout;
2412
+ private renderComponentAttempts;
2413
+ /**
2414
+ * Fork mode — set when this turn is adapting a near-matching parent script.
2415
+ * In fork mode there is no legitimate "answer with bare text" outcome: the
2416
+ * only correct first move is a tool call (write_script, or a source tool for
2417
+ * schema discovery). We therefore force tool use on the first LLM iteration
2418
+ * so the model can't end its turn with a bare "I'll adapt…" preamble and zero
2419
+ * tool calls. Never set on the fresh-authoring / general-question path.
2420
+ */
2421
+ private forkMode;
2422
+ /**
2423
+ * Edit mode — set when this turn applies a user-directed change to an
2424
+ * existing script. Swaps the system prompt to `agent-main-edit` and stamps
2425
+ * the shadow draft's parentId. Like fork mode there is no legitimate
2426
+ * "answer with bare text" outcome, so tool use is forced on iteration 1.
2427
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 4.
2428
+ */
2429
+ private editContext;
2430
+ /**
2431
+ * Per-turn cancellation signal (user hit "Stop"). Set at the top of
2432
+ * handleQuestion and read by the tool handler, the SourceAgent dispatch, and
2433
+ * the script subprocess so an abort tears down every layer of the turn.
2434
+ */
2435
+ private abortSignal?;
2436
+ constructor(externalTools: ExternalTool[], config: AgentConfig, scriptStore?: ScriptStore, turnId?: string, streamBuffer?: StreamBuffer, workflows?: WorkflowDescriptor[], forkMode?: boolean, editContext?: EditContext, componentCatalog?: Component[]);
2437
+ /** True when the turn is applying a user-directed script edit. */
2438
+ private get editMode();
2439
+ private get scriptingEnabled();
2440
+ /**
2441
+ * Handle a user question using the multi-agent system.
2442
+ *
2443
+ * This is ONE LLM.streamWithTools() call. The LLM:
2444
+ * 1. Sees source summaries + direct tool descriptions in system prompt
2445
+ * 2. Decides which tool(s) to call (routing)
2446
+ * 3. Source tools → SourceAgent runs independently → returns data
2447
+ * 4. Direct tools → fn() called directly with LLM params → returns data
2448
+ * 5. Generates final analysis text
2449
+ */
2450
+ handleQuestion(userPrompt: string, apiKey?: string, conversationHistory?: string, streamCallback?: (chunk: string) => void, signal?: AbortSignal): Promise<AgentResponse>;
2451
+ /**
2452
+ * Tool definition, with the catalog rendered from the registered `Metadata`
2453
+ * the caller passed in. Generated rather than hand-written so it cannot drift
2454
+ * from what the frontend actually ships.
2455
+ */
2456
+ private buildRenderComponentsToolDef;
2457
+ /**
2458
+ * Validate the agent's component picks against the REAL columns of each bound
2459
+ * dataset. All-or-nothing: any error rejects the whole call and returns the
2460
+ * findings, because a partially-accepted dashboard makes the retry ambiguous.
2461
+ */
2462
+ private handleRenderComponents;
2463
+ private handleWriteScript;
2464
+ private handleExecuteScript;
2465
+ /**
2466
+ * Build the AgentWrittenScript payload the caller will hand to
2467
+ * `ScriptStore.promoteToVerified()`. Only returned when a verified
2468
+ * successful execution is on record.
2469
+ */
2470
+ private buildSavedScript;
2471
+ private normalizeParameterList;
2472
+ /**
2473
+ * Use the schema embedding collection to pre-select relevant tables for
2474
+ * this source + intent. Returns a formatted schema block if confidence is
2475
+ * high (top match ≥ 0.55 and ≥3 candidates), otherwise null.
2476
+ *
2477
+ * When this returns a block, we can skip the SourceAgent's `search_schema`
2478
+ * loop and reduce iteration budget. When it returns null, the SourceAgent
2479
+ * falls back to the existing LLM-driven keyword search (same as today).
2480
+ */
2481
+ /**
2482
+ * Ids + verbatim names for everything resolved this turn, appended by CODE to
2483
+ * each source dispatch so neither omission nor re-spelling by the model can
2484
+ * lose them. Empty string when nothing was resolved, so intents are untouched
2485
+ * for questions that name no entities.
2486
+ */
2487
+ private buildResolvedEntitiesBlock;
2488
+ /** True when the backend registered the entity-search collection. */
2489
+ private get entitySearchEnabled();
2490
+ /**
2491
+ * Compact catalog (type + gloss) for the system prompt. This is what lets the
2492
+ * LLM NAME a concept instead of guessing at our vocabulary — it can infer
2493
+ * that a question is about unpaid money, but not that we call that
2494
+ * `outstanding-balance`. Full `resolution` detail is fetched by the tool.
2495
+ *
2496
+ * Deliberately NOT cached in-process. The token cost is already handled by
2497
+ * prompt caching (the system prompt carries a cache_control breakpoint), and
2498
+ * re-reading a tiny PK-ordered table costs ~nothing. Fetching fresh does not
2499
+ * weaken the prompt cache either: unchanged data renders byte-identical text,
2500
+ * so the prefix still hits. A process cache would only buy a staleness window
2501
+ * in which a newly indexed entity is invisible to the agent.
2502
+ */
2503
+ private loadEntityCatalog;
2504
+ /**
2505
+ * Never throws: a resolver outage must degrade to "nothing resolved" and
2506
+ * leave the agent exactly as it was, not take the whole answer down.
2507
+ */
2508
+ private handleResolveEntities;
2509
+ private preResolveSchema;
2510
+ /**
2511
+ * Execute a direct tool — call fn() with LLM-provided params, no SourceAgent.
2512
+ */
2513
+ private handleDirectTool;
2514
+ /**
2515
+ * Build the main agent's system prompt with source summaries, direct tool descriptions,
2516
+ * and workflow component descriptions.
2517
+ */
2518
+ private buildSystemPrompt;
2519
+ /**
2520
+ * Build tool definitions for source tools — summary-only descriptions.
2521
+ * The full schema is inside the SourceAgent which runs independently.
2522
+ */
2523
+ private buildSourceToolDefinitions;
2524
+ /**
2525
+ * Build tool definitions for direct tools — expose their actual params.
2526
+ * These are called directly by the main agent LLM, no SourceAgent.
2527
+ */
2528
+ private buildDirectToolDefinitions;
2529
+ /**
2530
+ * Capture a workflow selection. We do NOT execute anything — the LLM has
2531
+ * already extracted the props it wants the workflow rendered with. We
2532
+ * record the selection (via the capture callback) and return a short
2533
+ * acknowledgement so the LLM ends its turn cleanly without writing
2534
+ * analysis text or calling more tools.
2535
+ */
2536
+ private handleWorkflow;
2537
+ /**
2538
+ * Build LLM tool definitions for workflow components. The workflow's
2539
+ * propsSchema becomes the tool's input_schema so the LLM extracts props
2540
+ * directly from the prompt — same mechanic as direct tools.
2541
+ */
2542
+ private buildWorkflowToolDefinitions;
2543
+ /**
2544
+ * Format a source agent's result as a clean string for the main agent LLM.
2545
+ */
2546
+ private formatResultForMainAgent;
2547
+ /**
2548
+ * Get source summaries (for external inspection/debugging).
2549
+ */
2550
+ getSourceSummaries(): SourceSummary[];
2551
+ }
2552
+
2553
+ /**
2554
+ * Represents an action that can be performed on a UIBlock
2555
+ */
2556
+ interface Action {
2557
+ id: string;
2558
+ name: string;
2559
+ type: string;
2560
+ [key: string]: any;
2561
+ }
2562
+
2563
+ type SystemPrompt = string | Anthropic.Messages.TextBlockParam[];
2564
+ interface LLMMessages {
2565
+ sys: SystemPrompt;
2566
+ user: string;
2567
+ prefill?: string;
2568
+ }
2569
+ interface LLMOptions {
2570
+ model?: string;
2571
+ maxTokens?: number;
2572
+ temperature?: number;
2573
+ topP?: number;
2574
+ apiKey?: string;
2575
+ baseURL?: string;
2576
+ partial?: (chunk: string) => void;
2577
+ /**
2578
+ * Per-request cancellation. When the caller aborts this signal (user hit
2579
+ * "Stop"), the underlying provider request is cancelled and the call throws
2580
+ * a RequestAbortedError. Threaded into the provider `messages.create` request
2581
+ * options and checked between tool-loop iterations. Currently honored on the
2582
+ * Anthropic path (the agent flow's default provider).
2583
+ */
2584
+ signal?: AbortSignal;
2585
+ /**
2586
+ * Forces a tool call on the FIRST iteration of streamWithTools only
2587
+ * (subsequent iterations revert to auto). Used by fork mode to stop the
2588
+ * model from ending its turn with a bare "I'll adapt the script…" preamble
2589
+ * and zero tool calls. `{ type: 'any' }` lets the model pick which tool
2590
+ * (write_script in the common case, a source tool for schema discovery);
2591
+ * `{ type: 'tool', name }` pins a specific tool. Honored on both the
2592
+ * Anthropic path and the OpenAI/OpenRouter path (mapped to OpenAI's
2593
+ * tool_choice: 'required' / a named function).
2594
+ */
2595
+ firstIterationToolChoice?: {
2596
+ type: 'any';
2597
+ } | {
2598
+ type: 'tool';
2599
+ name: string;
2600
+ };
2601
+ /**
2602
+ * Internal — set only by the OpenRouter wrappers when the target is a Claude
2603
+ * model. Tells the OpenAI-wire path to emit Anthropic `cache_control`
2604
+ * breakpoints (OpenRouter forwards them to Anthropic for prompt caching).
2605
+ * Never set for direct OpenAI/Groq calls, so their requests are unchanged.
2606
+ */
2607
+ _openrouterClaudeCaching?: boolean;
2608
+ /**
2609
+ * Internal — OpenRouter provider-routing preferences (forwarded as the
2610
+ * `provider` body field). Set by the OpenRouter wrappers to steer routing to
2611
+ * a fast backend (e.g. {sort:'throughput'}). Never set for direct OpenAI/Groq.
2612
+ */
2613
+ _openrouterProvider?: Record<string, unknown>;
2614
+ }
2615
+ interface Tool {
2616
+ name: string;
2617
+ description: string;
2618
+ input_schema: {
2619
+ type: string;
2620
+ properties: Record<string, any>;
2621
+ required?: string[];
2622
+ };
2623
+ }
2624
+ declare class LLM {
2625
+ static text(messages: LLMMessages, options?: LLMOptions): Promise<string>;
2626
+ static stream<T = string>(messages: LLMMessages, options?: LLMOptions, json?: boolean): Promise<T extends string ? string : any>;
2627
+ static streamWithTools(messages: LLMMessages, tools: Tool[], toolHandler: (toolName: string, toolInput: any) => Promise<any>, options?: LLMOptions, maxIterations?: number): Promise<string>;
2628
+ /**
2629
+ * Normalize system prompt to Anthropic format
2630
+ * Converts string to array format if needed
2631
+ * @param sys - System prompt (string or array of blocks)
2632
+ * @returns Normalized system prompt for Anthropic API
2633
+ */
2634
+ private static _normalizeSystemPrompt;
2635
+ /**
2636
+ * Strip unpaired UTF-16 surrogates from every text field of a message set.
2637
+ *
2638
+ * A lone surrogate (from mid-pair string slicing or corrupt source data)
2639
+ * serializes to a bare `\udXXX` escape that strict JSON parsers — including
2640
+ * the one on Anthropic's API — reject with "no low surrogate in string",
2641
+ * failing the whole request. Sanitizing here, at the single boundary every
2642
+ * provider call flows through, guarantees no request can carry one.
2643
+ */
2644
+ private static _sanitizeMessages;
2645
+ /**
2646
+ * Log cache usage metrics from Anthropic API response
2647
+ * Shows cache hits, costs, and savings
2648
+ */
2649
+ private static _logCacheUsage;
2650
+ /**
2651
+ * Parse model string to extract provider and model name
2652
+ * @param modelString - Format: "provider/model-name" or just "model-name"
2653
+ * @returns [provider, modelName]
2654
+ *
2655
+ * @example
2656
+ * "anthropic/claude-sonnet-4-5" → ["anthropic", "claude-sonnet-4-5"]
2657
+ * "groq/openai/gpt-oss-120b" → ["groq", "openai/gpt-oss-120b"]
2658
+ * "claude-sonnet-4-5" → ["anthropic", "claude-sonnet-4-5"] (default)
2659
+ */
2660
+ private static _parseModel;
2661
+ /**
2662
+ * Map an Anthropic model id (e.g. "claude-sonnet-4-5-20250929") to the OpenRouter slug
2663
+ * (e.g. "claude-sonnet-4.5"). OpenRouter slugs drop the date suffix and use dotted versions.
2664
+ */
2665
+ private static _toOpenRouterSlug;
2666
+ /**
2667
+ * Per-provider proxy base URL. Returns `${SUPERATOM_LLM_PROXY_URL}/<provider>`
2668
+ * when our Cloudflare LLM proxy is configured, else undefined (→ talk to the
2669
+ * provider directly, legacy behaviour). An explicit options.baseURL (e.g.
2670
+ * OpenRouter) always wins and is never overridden. See backend/docs/llm-proxy.md.
2671
+ */
2672
+ private static _proxyBaseURL;
2673
+ private static _openrouterOptions;
2674
+ private static _isRetryableProviderError;
2675
+ private static _withOpenrouterRetry;
2676
+ private static _openrouterText;
2677
+ private static _openrouterStream;
2678
+ private static _openrouterStreamWithTools;
2679
+ /**
2680
+ * Build an Anthropic client. Routes through our Cloudflare LLM proxy when
2681
+ * SUPERATOM_LLM_PROXY_URL is set (each client ships a per-client proxy key as
2682
+ * ANTHROPIC_API_KEY and never holds the real key); otherwise talks to
2683
+ * api.anthropic.com directly. See backend/docs/llm-proxy.md.
2684
+ */
2685
+ private static _anthropicClient;
2686
+ /** True when OpenRouter is configured as a fail-open fallback for Claude. */
2687
+ private static _openrouterAvailable;
2688
+ /** Remap an Anthropic model id to the OpenRouter model path for fail-open. */
2689
+ private static _anthropicFallbackModel;
2690
+ private static _anthropicText;
2691
+ private static _anthropicStream;
2692
+ private static _anthropicStreamWithTools;
2693
+ private static _groqText;
2694
+ private static _groqStream;
2695
+ /**
2696
+ * Gemini request options carrying the proxy base URL, or undefined → talk to
2697
+ * generativelanguage.googleapis.com directly. The Google SDK takes baseUrl as a
2698
+ * per-model request option, not a constructor arg. See backend/docs/llm-proxy.md.
2699
+ */
2700
+ private static _geminiRequestOptions;
2701
+ private static _geminiText;
2702
+ private static _geminiStream;
2703
+ /**
2704
+ * Recursively strip unsupported JSON Schema properties for Gemini
2705
+ * Gemini doesn't support: additionalProperties, $schema, etc.
2706
+ */
2707
+ private static _cleanSchemaForGemini;
2708
+ private static _geminiStreamWithTools;
2709
+ /** True for Anthropic/Claude model ids — gates OpenRouter prompt caching. */
2710
+ private static _isClaudeModel;
2711
+ /**
2712
+ * Build the OpenAI-wire system message. For OpenRouter + Claude
2713
+ * (cacheClaude=true) it emits content parts carrying Anthropic
2714
+ * `cache_control` breakpoints (preserving any the caller set, else marking
2715
+ * the last block), so OpenRouter forwards them to Anthropic for prompt
2716
+ * caching. Otherwise it returns a plain flattened string — unchanged for
2717
+ * direct OpenAI/Groq.
2718
+ */
2719
+ private static _openaiSystemMessage;
2720
+ /**
2721
+ * Split an OpenAI-wire usage object. `prompt_tokens` INCLUDES cached tokens,
2722
+ * so we subtract them out (Anthropic-style: input excludes cache reads) and
2723
+ * report cached separately — this makes calculateCost price cache reads at
2724
+ * the discounted rate and reflects OpenRouter prompt-cache savings in logs.
2725
+ */
2726
+ private static _openaiUsage;
2727
+ private static _openaiText;
2728
+ private static _openaiStream;
2729
+ /** Map the Anthropic-style firstIterationToolChoice to OpenAI's tool_choice. */
2730
+ private static _openaiToolChoice;
2731
+ private static _openaiStreamWithTools;
2732
+ /**
2733
+ * Parse JSON string, handling markdown code blocks and surrounding text
2734
+ * Enhanced version with jsonrepair to handle malformed JSON from LLMs
2735
+ * @param text - Text that may contain JSON wrapped in ```json...``` or with surrounding text
2736
+ * @returns Parsed JSON object or array
2737
+ */
2738
+ private static _parseJSON;
2739
+ }
2740
+
2741
+ interface CapturedLog {
2742
+ timestamp: number;
2743
+ level: 'info' | 'error' | 'warn' | 'debug';
2744
+ message: string;
2745
+ type?: 'explanation' | 'query' | 'general';
2746
+ data?: Record<string, any>;
2747
+ }
2748
+ /**
2749
+ * UILogCollector captures logs during user prompt processing
2750
+ * and sends them to runtime via ui_logs message with uiBlockId as the message id
2751
+ * Logs are sent in real-time for streaming effect in the UI
2752
+ * Respects the global log level configuration
2753
+ */
2754
+ declare class UILogCollector {
2755
+ private logs;
2756
+ private uiBlockId;
2757
+ private clientId;
2758
+ private sendMessage;
2759
+ private currentLogLevel;
2760
+ constructor(clientId: string, sendMessage: (message: Message) => void, uiBlockId?: string);
2761
+ /**
2762
+ * Check if logging is enabled (uiBlockId is provided)
2763
+ */
2764
+ isEnabled(): boolean;
2765
+ /**
2766
+ * Check if a message should be logged based on current log level
2767
+ */
2768
+ private shouldLog;
2769
+ /**
2770
+ * Add a log entry with timestamp and immediately send to runtime
2771
+ * Only logs that pass the log level filter are captured and sent
2772
+ */
2773
+ private addLog;
2774
+ /**
2775
+ * Send a single log to runtime immediately
2776
+ */
2777
+ private sendLogImmediately;
2778
+ /**
2779
+ * Log info message
2780
+ */
2781
+ info(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
2782
+ /**
2783
+ * Log error message
2784
+ */
2785
+ error(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
2786
+ /**
2787
+ * Log warning message
2788
+ */
2789
+ warn(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
2790
+ /**
2791
+ * Log debug message
2792
+ */
2793
+ debug(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
2794
+ /**
2795
+ * Log LLM explanation with typed metadata
2796
+ */
2797
+ logExplanation(message: string, explanation: string, data?: Record<string, any>): void;
2798
+ /**
2799
+ * Log generated query with typed metadata
2800
+ */
2801
+ logQuery(message: string, query: string, data?: Record<string, any>): void;
2802
+ /**
2803
+ * Send all collected logs at once (optional, for final summary)
2804
+ */
2805
+ sendAllLogs(): void;
2806
+ /**
2807
+ * Get all collected logs
2808
+ */
2809
+ getLogs(): CapturedLog[];
2810
+ /**
2811
+ * Clear all logs
2812
+ */
2813
+ clearLogs(): void;
2814
+ /**
2815
+ * Set uiBlockId (in case it's provided later)
2816
+ */
2817
+ setUIBlockId(uiBlockId: string): void;
2818
+ }
2819
+
2820
+ /**
2821
+ * UIBlock represents a single user and assistant message block in a thread
2822
+ * Contains user question, component metadata, component data, text response, and available actions
2823
+ */
2824
+ declare class UIBlock {
2825
+ private id;
2826
+ private userQuestion;
2827
+ private generatedComponentMetadata;
2828
+ private componentData;
2829
+ private textResponse;
2830
+ private actions;
2831
+ private createdAt;
2832
+ /**
2833
+ * Which script recipe produced this answer, when a script did. Read on the
2834
+ * NEXT turn so the user can say "use mode instead" and have the matcher
2835
+ * resolve it to a concrete script (the `edit` tier is unreachable without it).
2836
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 1.
2837
+ */
2838
+ private scriptBinding;
2839
+ /**
2840
+ * Creates a new UIBlock instance
2841
+ * @param userQuestion - The user's question or input
2842
+ * @param componentData - The component data object
2843
+ * @param generatedComponentMetadata - Optional metadata about the generated component
2844
+ * @param actions - Optional array of available actions
2845
+ * @param id - Optional custom ID, generates UUID if not provided
2846
+ * @param textResponse - Optional text response from LLM
2847
+ */
2848
+ constructor(userQuestion: string, componentData?: Record<string, any>, generatedComponentMetadata?: Record<string, any>, actions?: Action[], id?: string, textResponse?: string | null);
2849
+ /**
2850
+ * Get the UIBlock ID
2851
+ */
2852
+ getId(): string;
2853
+ /**
2854
+ * Get the user question
2855
+ */
2856
+ getUserQuestion(): string;
2857
+ /**
2858
+ * Set or update the user question
2859
+ */
2860
+ setUserQuestion(question: string): void;
2861
+ /**
2862
+ * Get component metadata
2863
+ */
2864
+ getComponentMetadata(): Record<string, any>;
1511
2865
  getTextResponse(): string;
1512
2866
  /**
1513
2867
  * Set or update component metadata
@@ -1576,6 +2930,14 @@ declare class UIBlock {
1576
2930
  /**
1577
2931
  * Get creation timestamp
1578
2932
  */
2933
+ /**
2934
+ * Bind this block to the script recipe that produced its answer.
2935
+ */
2936
+ setScriptBinding(binding: Record<string, any> | null): void;
2937
+ /**
2938
+ * The script recipe bound to this block, if any.
2939
+ */
2940
+ getScriptBinding(): Record<string, any> | null;
1579
2941
  getCreatedAt(): Date;
1580
2942
  /**
1581
2943
  * Convert UIBlock to JSON-serializable object
@@ -1643,6 +3005,32 @@ declare class Thread {
1643
3005
  * @param currentUIBlockId - ID of current UIBlock to exclude from context (optional)
1644
3006
  * @returns Formatted conversation history string
1645
3007
  */
3008
+ /**
3009
+ * The script recipe bound to the most recent completed UIBlock — i.e. the
3010
+ * script behind the answer the user is currently looking at. Drives the
3011
+ * matcher's `edit` tier: without it, "use mode instead" has no target and
3012
+ * falls through to regeneration.
3013
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 1.
3014
+ */
3015
+ getActiveScriptBinding(currentUIBlockId?: string): Record<string, any> | null;
3016
+ /**
3017
+ * The recent script-backed answers in this thread, newest first — the
3018
+ * candidate set a user-directed edit can target.
3019
+ *
3020
+ * Returning several rather than only the newest is what lets an instruction
3021
+ * name its own target ("use the mode for the WSP one"). With a single
3022
+ * candidate every edit lands on the most recent script, which silently edits
3023
+ * the wrong recipe whenever the user meant an earlier one.
3024
+ *
3025
+ * Deduped by recipeId (newest occurrence wins) so a long editing session on
3026
+ * one script doesn't crowd out the others. Each entry carries the question
3027
+ * that produced it — without that the candidates are indistinguishable.
3028
+ *
3029
+ * In-memory only: dies with the process. The caller falls back to the
3030
+ * persisted bindings when this comes back empty.
3031
+ * See backend/docs/SCRIPT-EDIT-DESIGN.md § Component 1.
3032
+ */
3033
+ getScriptBindings(limit?: number, currentUIBlockId?: string): Record<string, any>[];
1646
3034
  getConversationContext(limit?: number, currentUIBlockId?: string): string;
1647
3035
  /**
1648
3036
  * Convert Thread to JSON-serializable object
@@ -2119,127 +3507,35 @@ declare class QueryExecutionService {
2119
3507
  * @param collections - Collections object containing database execute function
2120
3508
  * @returns Object with result data and cache key
2121
3509
  */
2122
- executeQuery(query: any, collections: any): Promise<{
2123
- result: any;
2124
- cacheKey: string;
2125
- }>;
2126
- /**
2127
- * Request the LLM to fix a failed SQL query
2128
- * @param failedQuery - The query that failed execution
2129
- * @param errorMessage - The error message from the failed execution
2130
- * @param componentContext - Context about the component
2131
- * @param apiKey - Optional API key
2132
- * @returns Fixed query string
2133
- */
2134
- requestQueryFix(failedQuery: string, errorMessage: string, componentContext: ComponentContext, apiKey?: string): Promise<string>;
2135
- /**
2136
- * Validate a single component's query with retry logic
2137
- * @param component - The component to validate
2138
- * @param collections - Collections object containing database execute function
2139
- * @param apiKey - Optional API key for LLM calls
2140
- * @returns Validation result with component, query key, and result
2141
- */
2142
- validateSingleQuery(component: Component, collections: any, apiKey?: string): Promise<QueryValidationResult>;
2143
- /**
2144
- * Validate multiple component queries in parallel
2145
- * @param components - Array of components with potential queries
2146
- * @param collections - Collections object containing database execute function
2147
- * @param apiKey - Optional API key for LLM calls
2148
- * @returns Object with validated components and query results map
2149
- */
2150
- validateComponentQueries(components: Component[], collections: any, apiKey?: string): Promise<BatchValidationResult>;
2151
- }
2152
-
2153
- /**
2154
- * StreamBuffer - Buffered streaming utility for smoother text delivery
2155
- * Batches small chunks together and flushes at regular intervals
2156
- */
2157
- type StreamCallback = (chunk: string) => void;
2158
- /**
2159
- * StreamBuffer class for managing buffered streaming output
2160
- * Provides smooth text delivery by batching small chunks
2161
- */
2162
- declare class StreamBuffer {
2163
- private buffer;
2164
- private flushTimer;
2165
- private callback;
2166
- private fullText;
2167
- constructor(callback?: StreamCallback);
2168
- /**
2169
- * Check if the buffer has a callback configured
2170
- */
2171
- hasCallback(): boolean;
2172
- /**
2173
- * Get all text that has been written (including already flushed)
2174
- */
2175
- getFullText(): string;
2176
- /**
2177
- * Write a chunk to the buffer
2178
- * Large chunks or chunks with newlines are flushed immediately
2179
- * Small chunks are batched and flushed after a short interval
2180
- *
2181
- * @param chunk - Text chunk to write
2182
- */
2183
- write(chunk: string): void;
3510
+ executeQuery(query: any, collections: any): Promise<{
3511
+ result: any;
3512
+ cacheKey: string;
3513
+ }>;
2184
3514
  /**
2185
- * Flush the buffer immediately
2186
- * Call this before tool execution or other operations that need clean output
3515
+ * Request the LLM to fix a failed SQL query
3516
+ * @param failedQuery - The query that failed execution
3517
+ * @param errorMessage - The error message from the failed execution
3518
+ * @param componentContext - Context about the component
3519
+ * @param apiKey - Optional API key
3520
+ * @returns Fixed query string
2187
3521
  */
2188
- flush(): void;
3522
+ requestQueryFix(failedQuery: string, errorMessage: string, componentContext: ComponentContext, apiKey?: string): Promise<string>;
2189
3523
  /**
2190
- * Internal flush implementation
3524
+ * Validate a single component's query with retry logic
3525
+ * @param component - The component to validate
3526
+ * @param collections - Collections object containing database execute function
3527
+ * @param apiKey - Optional API key for LLM calls
3528
+ * @returns Validation result with component, query key, and result
2191
3529
  */
2192
- private flushNow;
3530
+ validateSingleQuery(component: Component, collections: any, apiKey?: string): Promise<QueryValidationResult>;
2193
3531
  /**
2194
- * Clean up resources
2195
- * Call this when done with the buffer
3532
+ * Validate multiple component queries in parallel
3533
+ * @param components - Array of components with potential queries
3534
+ * @param collections - Collections object containing database execute function
3535
+ * @param apiKey - Optional API key for LLM calls
3536
+ * @returns Object with validated components and query results map
2196
3537
  */
2197
- dispose(): void;
2198
- }
2199
-
2200
- /**
2201
- * ToolExecutorService - Handles execution of SQL queries and external tools
2202
- * Extracted from BaseLLM.generateTextResponse for better separation of concerns
2203
- */
2204
-
2205
- /**
2206
- * External tool definition
2207
- */
2208
- interface ExternalTool {
2209
- id: string;
2210
- name: string;
2211
- description?: string;
2212
- /** Tool type: "source" = routed through SourceAgent, "direct" = called directly by MainAgent */
2213
- toolType?: 'source' | 'direct';
2214
- /** Full untruncated schema for source agent (all columns visible) */
2215
- fullSchema?: string;
2216
- /** Schema size tier: small (≤50 tables), medium (51-200), large (201-500), very_large (500+) */
2217
- schemaTier?: string;
2218
- /** Schema search function for very_large tier — keyword search over entities */
2219
- schemaSearchFn?: (keywords: string[]) => string;
2220
- fn: (input: any) => Promise<any>;
2221
- limit?: number;
2222
- outputSchema?: any;
2223
- executionType?: 'immediate' | 'deferred';
2224
- userProvidedData?: any;
2225
- params?: Record<string, any>;
2226
- }
2227
- /**
2228
- * Executed tool tracking info
2229
- */
2230
- interface ExecutedToolInfo {
2231
- id: string;
2232
- name: string;
2233
- params: any;
2234
- result: {
2235
- _totalRecords: number;
2236
- _recordsShown: number;
2237
- _metadata?: any;
2238
- _sampleData: any[];
2239
- };
2240
- outputSchema?: any;
2241
- sourceSchema?: string;
2242
- sourceType?: string;
3538
+ validateComponentQueries(components: Component[], collections: any, apiKey?: string): Promise<BatchValidationResult>;
2243
3539
  }
2244
3540
 
2245
3541
  /**
@@ -2397,7 +3693,7 @@ declare abstract class BaseLLM {
2397
3693
  * This helps provide intelligent suggestions for follow-up queries
2398
3694
  * For general/conversational questions without components, pass textResponse instead
2399
3695
  */
2400
- generateNextQuestions(originalUserPrompt: string, component?: Component | null, componentData?: Record<string, unknown>, apiKey?: string, conversationHistory?: string, textResponse?: string): Promise<string[]>;
3696
+ generateNextQuestions(originalUserPrompt: string, component?: Component | null, componentData?: Record<string, unknown>, apiKey?: string, conversationHistory?: string, textResponse?: string, signal?: AbortSignal): Promise<string[]>;
2401
3697
  }
2402
3698
 
2403
3699
  interface AnthropicLLMConfig extends BaseLLMConfig {
@@ -2460,7 +3756,8 @@ declare const openaiLLM: OpenAILLM;
2460
3756
  * Query Cache — Two mechanisms:
2461
3757
  *
2462
3758
  * 1. `cache` (query string → result data) — TTL-based with max size, for avoiding re-execution
2463
- * of recently validated queries. LRU eviction when max size exceeded.
3759
+ * of recently validated queries. True LRU eviction: reads bubble entries to the back via
3760
+ * delete+re-set so the oldest *unused* entry is evicted, not the oldest *inserted*.
2464
3761
  *
2465
3762
  * 2. Encrypted queryId tokens — SQL is encrypted into the queryId itself (self-contained).
2466
3763
  * No server-side storage needed for SQL mappings. The token is decrypted on each request.
@@ -2487,11 +3784,16 @@ declare class QueryCache {
2487
3784
  */
2488
3785
  getTTL(): number;
2489
3786
  /**
2490
- * Store query result in data cache
3787
+ * Store query result in data cache.
3788
+ * If the key already exists, it's removed first so the re-insert places it
3789
+ * at the back of the iteration order (LRU). Eviction only fires when adding
3790
+ * a genuinely new key past the size limit.
2491
3791
  */
2492
3792
  set(query: string, data: any): void;
2493
3793
  /**
2494
- * Get cached result if exists and not expired
3794
+ * Get cached result if exists and not expired.
3795
+ * On hit, re-inserts the entry so it moves to the back of the Map's
3796
+ * iteration order — turning FIFO eviction into true LRU.
2495
3797
  */
2496
3798
  get(query: string): any | null;
2497
3799
  /**
@@ -2598,177 +3900,185 @@ declare class DashboardConversationHistory {
2598
3900
  declare const dashboardConversationHistory: DashboardConversationHistory;
2599
3901
 
2600
3902
  /**
2601
- * Multi-Agent Architecture Types
3903
+ * Whole-dashboard generation via Pi, a terminal coding agent — as opposed to
3904
+ * DASH_COMP_REQ's single-widget-at-a-time flow. Runs Pi in-process via its
3905
+ * SDK (createAgentSession), not as a subprocess: no shell, no argument
3906
+ * quoting, no stdin/stdout piping, none of the Windows-specific subprocess
3907
+ * issues that came with spawning the `pi` CLI directly.
2602
3908
  *
2603
- * Defines interfaces for the hierarchical agent system:
2604
- * - Main Agent: ONE LLM.streamWithTools() call with source agent tools
2605
- * - Source Agents: independent agents that query individual data sources
3909
+ * Called from sdk-nodejs/src/dashboardAgent/index.ts (DASHBOARD_AGENT_REQ),
3910
+ * which owns the generic streaming/abort machinery (mirrors USER_PROMPT_REQ)
3911
+ * and passes `signal`/`onProgress` alongside the normal params — this stays
3912
+ * within CollectionHandler's loose (params) => Promise<result> typing, no
3913
+ * change needed to that shared type.
2606
3914
  *
2607
- * The main agent sees only source summaries. When it calls a source tool,
2608
- * the SourceAgent runs independently (own LLM, own retries) and returns clean data.
2609
- */
2610
-
2611
- /**
2612
- * Per-entity detail: name, row count, and column names.
2613
- * Gives the main agent enough context to route to the right source.
2614
- */
2615
- interface EntityDetail {
2616
- /** Entity name (table, sheet, endpoint) */
2617
- name: string;
2618
- /** Approximate row count */
2619
- rowCount?: number;
2620
- /** Column/field names */
2621
- columns: string[];
2622
- }
2623
- /**
2624
- * Representation of a data source for the main agent.
2625
- * Contains entity names WITH column names so the LLM can route accurately.
2626
- */
2627
- interface SourceSummary {
2628
- /** Source ID (matches tool ID prefix) */
2629
- id: string;
2630
- /** Human-readable source name */
2631
- name: string;
2632
- /** Source type: postgres, excel, rest_api, etc. */
2633
- type: string;
2634
- /** Brief description of what data this source contains */
2635
- description: string;
2636
- /** Detailed entity info with column names for routing */
2637
- entityDetails: EntityDetail[];
2638
- /** The tool ID associated with this source */
2639
- toolId: string;
2640
- }
2641
- /**
2642
- * What a source agent returns after querying its data source.
2643
- * The main agent uses this to analyze and compose the final response.
2644
- */
2645
- interface SourceAgentResult {
2646
- /** Source ID */
2647
- sourceId: string;
2648
- /** Source name */
2649
- sourceName: string;
2650
- /** Whether the query succeeded */
2651
- success: boolean;
2652
- /** Result data rows */
2653
- data: any[];
2654
- /** Metadata about the query execution */
2655
- metadata: SourceAgentMetadata;
2656
- /** Tool execution info for the last successful query (backward compat) */
2657
- executedTool: ExecutedToolInfo;
2658
- /** All successful tool executions (primary + follow-up queries) */
2659
- allExecutedTools?: ExecutedToolInfo[];
2660
- /** Error message if failed */
2661
- error?: string;
2662
- }
2663
- interface SourceAgentMetadata {
2664
- /** Total rows that matched the query (before limit) */
2665
- totalRowsMatched: number;
2666
- /** Rows actually returned (after limit) */
2667
- rowsReturned: number;
2668
- /** Whether the result was truncated by the row limit */
2669
- isLimited: boolean;
2670
- /** The query/params that were executed */
2671
- queryExecuted?: string;
2672
- /** Execution time in milliseconds */
2673
- executionTimeMs: number;
2674
- }
2675
- /**
2676
- * The complete response from the multi-agent system.
2677
- * Contains everything needed for text display + component generation.
3915
+ * This mechanism is generic and reusable across any deployment. What's
3916
+ * genuinely project-specific — where AGENTS.md lives, which model to use —
3917
+ * is supplied via `DashboardAgentCollectionConfig`, with defaults sensible
3918
+ * enough that most callers don't need to override them (see below). The
3919
+ * data-source tool list and the dashboard's current state both come from
3920
+ * things sdk-nodejs already exposes generically: `sdk.getTools()` (whatever
3921
+ * this deployment registered via `sdk.setTools()`) and `sdk.callCollection
3922
+ * ('dashboards', 'query', ...)` (whatever this deployment already registered
3923
+ * under that name/shape) — no per-deployment callback needed for either.
3924
+ * Same convention on the way out: after a successful run, the prompt and the
3925
+ * full response text are handed to `sdk.callCollection('dashboard-agent-
3926
+ * conversations', 'create', ...)` if this deployment has registered one —
3927
+ * skipped silently otherwise, since conversation history is optional.
3928
+ *
3929
+ * Pi verifies every query against the live database itself (via whatever
3930
+ * local tool-execution bridge the deployment exposes, e.g. an HTTP bridge
3931
+ * on localhost), but does NOT persist the result itself — it writes the
3932
+ * finished DSL to an absolute path inside `runtimeDir`, told to it explicitly
3933
+ * in the prompt, and stops there. This handler reads that file after the run
3934
+ * finishes and returns its content as `dashboard` in the result. The caller
3935
+ * (frontend) is the one that actually saves it, via whatever authenticated
3936
+ * create/update path any other dashboard edit goes through — Pi has no user
3937
+ * session/auth context of its own, so persistence shouldn't happen from
3938
+ * inside it.
3939
+ *
3940
+ * Session persistence: the FIRST call for a dashboardId pays the full cost
3941
+ * (explore KB, discover schema, plan, verify, build). Every call after that
3942
+ * resumes the same session file (SessionManager.open) so Pi has everything
3943
+ * it already learned — it only needs to reason about the new, smaller ask,
3944
+ * not rediscover the whole dashboard from scratch. The session's file path
3945
+ * (AgentSession.sessionFile) is captured right after creation and persisted
3946
+ * in a small local file, keyed by dashboardId, inside `runtimeDir`.
2678
3947
  */
2679
- interface AgentResponse {
2680
- /** Generated text response (analysis of the data) */
2681
- text: string;
2682
- /** All executed tools across all source agents (for component generation) */
2683
- executedTools: ExecutedToolInfo[];
2684
- /** Individual results from each source agent */
2685
- sourceResults: SourceAgentResult[];
3948
+ interface DashboardAgentCollectionConfig {
3949
+ /**
3950
+ * Working directory Pi runs from — must contain AGENTS.md. This is a
3951
+ * version-controlled prompt file, so `cwd` is expected to live somewhere
3952
+ * like a `.prompts/` folder alongside the deployment's other prompts.
3953
+ * Default: `<process.cwd()>/.prompts/dashboard-agent` — the same
3954
+ * process.cwd()-based convention PromptLoader already uses for the main
3955
+ * agent's prompts, which needs no explicit override in the common case
3956
+ * (the backend process's own cwd already is its project root).
3957
+ */
3958
+ cwd?: string;
3959
+ /**
3960
+ * Where drafts/, the session-id map, and dashboard.log get written —
3961
+ * separate from `cwd` deliberately, so this deployment's runtime state
3962
+ * (regenerated per session, safe to gitignore) doesn't sit inside the
3963
+ * same folder as the version-controlled AGENTS.md prompt.
3964
+ * Default: `<process.cwd()>/.pi-dashboard-agent-runtime`.
3965
+ */
3966
+ runtimeDir?: string;
3967
+ /** Model provider (default: process.env.PI_AGENT_PROVIDER || 'openrouter'). */
3968
+ provider?: string;
3969
+ /** Model id (default: process.env.PI_AGENT_MODEL || 'anthropic/claude-sonnet-4.5'). */
3970
+ model?: string;
3971
+ /**
3972
+ * true (default): every request starts a brand-new pi session, with the 2
3973
+ * most recent prior responses (if any) injected into the prompt as
3974
+ * context — bounded cost per request, but pi re-explores schema/KB facts
3975
+ * it already verified in an earlier turn on this same dashboard.
3976
+ * false: resumes the same session file across requests on a given
3977
+ * dashboard — pi keeps everything it already learned, but context (and
3978
+ * cost) grows unbounded across turns (one observed turn: 2M+ cache-read
3979
+ * tokens after a handful of edits on the same dashboard).
3980
+ * Default: process.env.PI_AGENT_FRESH_SESSION !== 'false'.
3981
+ */
3982
+ freshSession?: boolean;
2686
3983
  }
3984
+ declare function registerDashboardAgentCollection(sdk: SuperatomSDK, config?: DashboardAgentCollectionConfig): void;
3985
+
2687
3986
  /**
2688
- * Configuration for the multi-agent system.
2689
- * Controls limits, models, and behavior.
3987
+ * ScriptMatcher — LLM-Based Script Matching + Parameter Extraction
3988
+ *
3989
+ * Uses ONE LLM call to:
3990
+ * 1. Pick the best matching script from the library (or "none")
3991
+ * 2. Extract parameter values from the user question
3992
+ *
3993
+ * Why LLM over embeddings:
3994
+ * - Embeddings capture topic similarity ("overstock" ≈ "inventory" ≈ "revenue")
3995
+ * but can't distinguish structurally different questions about the same domain
3996
+ * - LLM understands that "overstock by warehouse" needs a different script than
3997
+ * "revenue by warehouse" even though they're semantically close
3998
+ * - One call does both matching AND parameter extraction
3999
+ *
4000
+ * When script library grows past ~50, add an embedding pre-filter
4001
+ * (ChromaDB narrows to top 10 → LLM picks from those 10).
2690
4002
  */
2691
- interface AgentConfig {
2692
- /** Max rows a source agent can return (default: 50) */
2693
- maxRowsPerSource: number;
2694
- /** Model for the main agent (routing + analysis in one LLM call) */
2695
- mainAgentModel: string;
2696
- /** Model for source agent query generation */
2697
- sourceAgentModel: string;
2698
- /** API key for LLM calls */
2699
- apiKey?: string;
2700
- /** Max retry attempts per source agent */
2701
- maxRetries: number;
2702
- /** Max tool calling iterations for the main agent loop */
2703
- maxIterations: number;
2704
- /** Global knowledge base context (static, same for all users/questions — cached in system prompt) */
2705
- globalKnowledgeBase?: string;
2706
- /** Per-request knowledge base context (user-specific + query-matched — dynamic, not cached) */
2707
- knowledgeBaseContext?: string;
4003
+
4004
+ declare class ScriptMatcher {
4005
+ private store;
4006
+ constructor(store: ScriptStore);
4007
+ /**
4008
+ * Find the best matching script for a user question.
4009
+ * Uses ONE LLM call that picks the script AND extracts parameters.
4010
+ * Returns null if no script matches.
4011
+ */
4012
+ match(userPrompt: string, apiKey?: string, model?: string, signal?: AbortSignal,
4013
+ /**
4014
+ * Recent script-backed answers in this thread, newest first. Presence of at
4015
+ * least one is what makes the `edit` tier reachable at all (see the guards
4016
+ * below), and handing over SEVERAL is what lets an instruction name its own
4017
+ * target instead of always hitting the most recent script.
4018
+ */
4019
+ activeBindings?: ScriptBinding[],
4020
+ /**
4021
+ * Recent conversation turns, most recent last. The gate needs this to tell
4022
+ * "this reply is an edit instruction for the active script" apart from
4023
+ * "this reply is answering a clarifying question about a DIFFERENT,
4024
+ * unresolved topic that never became a script" — a bare parameter-shaped
4025
+ * reply ("April 2025 to March 2026") is textually indistinguishable
4026
+ * between those two cases without seeing what the assistant just asked.
4027
+ * Once this tier is decided, nothing downstream re-checks it — the edit
4028
+ * path hands MainAgent a system prompt that explicitly instructs it to
4029
+ * trust the premise and not treat the turn as a new question — so this
4030
+ * gate is the only place that can catch a mismatch.
4031
+ */
4032
+ conversationHistory?: string): Promise<ScriptMatch | null>;
4033
+ /**
4034
+ * Build the script catalog string for the LLM prompt.
4035
+ * Each script gets: index, ID, name, description, and parameter definitions.
4036
+ */
4037
+ private buildScriptCatalog;
4038
+ /**
4039
+ * The recent script-backed answers in this thread — the bounded set an edit
4040
+ * may target. Rendered as its own prompt section (never merged into the
4041
+ * ranked catalog) so the `edit` rules have an unambiguous referent set, and
4042
+ * numbered newest-first so the prompt's "prefer the most recent when the
4043
+ * instruction is ambiguous" tie-break has something to point at.
4044
+ *
4045
+ * Each entry carries the QUESTION that produced it plus the columns it
4046
+ * returned — that is what lets the matcher resolve "use the mode for the WSP
4047
+ * one" instead of blindly taking the newest.
4048
+ */
4049
+ private buildActiveScriptBlock;
2708
4050
  }
2709
- /**
2710
- * Default agent configuration
2711
- */
2712
- declare const DEFAULT_AGENT_CONFIG: AgentConfig;
2713
4051
 
2714
4052
  /**
2715
- * Main Agent (Orchestrator)
4053
+ * ScriptRunner — Execute scripts in an isolated tsx subprocess.
2716
4054
  *
2717
- * A single LLM.streamWithTools() call that handles everything:
2718
- * - Routing: decides which source(s) to query based on summaries
2719
- * - Querying: calls source tools (each wraps an independent SourceAgent)
2720
- * - Direct tools: calls pre-built function tools directly with LLM-provided params
2721
- * - Re-querying: if data is wrong/incomplete, calls tools again with modified intent
2722
- * - Analysis: generates final text response from the data
4055
+ * The subprocess approach replaces the earlier `new Function()` eval and gives us:
4056
+ * - Real sandbox (separate process, SIGKILL on timeout).
4057
+ * - Real TypeScript (tsx transpiles on the fly).
4058
+ * - npm imports available to scripts (clustering, stats, geo, etc.).
2723
4059
  *
2724
- * Two tool types:
2725
- * - "source" tools: main agent sees summaries, SourceAgent handles SQL generation independently
2726
- * - "direct" tools: main agent calls fn() directly with structured params (no SourceAgent)
4060
+ * Protocol: NDJSON over the child's stdin/stdout. See script-ipc.ts + backend/docs/SCRIPT-FLOW-IMPLEMENTATION.md.
2727
4061
  */
2728
4062
 
2729
- declare class MainAgent {
2730
- private externalTools;
2731
- private config;
2732
- private streamBuffer;
2733
- constructor(externalTools: ExternalTool[], config: AgentConfig, streamBuffer?: StreamBuffer);
2734
- /**
2735
- * Handle a user question using the multi-agent system.
2736
- *
2737
- * This is ONE LLM.streamWithTools() call. The LLM:
2738
- * 1. Sees source summaries + direct tool descriptions in system prompt
2739
- * 2. Decides which tool(s) to call (routing)
2740
- * 3. Source tools → SourceAgent runs independently → returns data
2741
- * 4. Direct tools → fn() called directly with LLM params → returns data
2742
- * 5. Generates final analysis text
2743
- */
2744
- handleQuestion(userPrompt: string, apiKey?: string, conversationHistory?: string, streamCallback?: (chunk: string) => void): Promise<AgentResponse>;
2745
- /**
2746
- * Execute a direct tool — call fn() with LLM-provided params, no SourceAgent.
2747
- */
2748
- private handleDirectTool;
2749
- /**
2750
- * Build the main agent's system prompt with source summaries and direct tool descriptions.
2751
- */
2752
- private buildSystemPrompt;
2753
- /**
2754
- * Build tool definitions for source tools — summary-only descriptions.
2755
- * The full schema is inside the SourceAgent which runs independently.
2756
- */
2757
- private buildSourceToolDefinitions;
2758
- /**
2759
- * Build tool definitions for direct tools — expose their actual params.
2760
- * These are called directly by the main agent LLM, no SourceAgent.
2761
- */
2762
- private buildDirectToolDefinitions;
2763
- /**
2764
- * Format a source agent's result as a clean string for the main agent LLM.
2765
- */
2766
- private formatResultForMainAgent;
2767
- /**
2768
- * Get source summaries (for external inspection/debugging).
2769
- */
2770
- getSourceSummaries(): SourceSummary[];
4063
+ interface RunScriptOptions {
4064
+ /** Data sources the script is allowed to query via ctx.query */
4065
+ externalTools: ExternalTool[];
4066
+ /** Optional — for propagating per-query UI progress to the user */
4067
+ streamBuffer?: StreamBuffer;
4068
+ /** Override the wall-clock timeout (default `SCRIPT_TIMEOUT_MS`, 60s). */
4069
+ timeoutMs?: number;
4070
+ /**
4071
+ * Per-turn cancellation signal. When the user hits "Stop" mid-run, the child
4072
+ * process group is SIGKILLed and the run resolves as an aborted failure (the
4073
+ * caller is already unwinding, so the result is discarded).
4074
+ */
4075
+ signal?: AbortSignal;
2771
4076
  }
4077
+ /**
4078
+ * Execute a recipe by spawning a tsx child on the script's .ts file.
4079
+ * `scriptPath` is the absolute path to the saved `.ts` body.
4080
+ */
4081
+ declare function runScript(recipe: ScriptRecipe, scriptPath: string, params: Record<string, any>, options: RunScriptOptions): Promise<ScriptResult>;
2772
4082
 
2773
4083
  type MessageTypeHandler = (message: IncomingMessage) => void | Promise<void>;
2774
4084
  declare class SuperatomSDK {
@@ -2786,6 +4096,7 @@ declare class SuperatomSDK {
2786
4096
  private collections;
2787
4097
  private components;
2788
4098
  private tools;
4099
+ private workflows;
2789
4100
  private anthropicApiKey;
2790
4101
  private groqApiKey;
2791
4102
  private geminiApiKey;
@@ -2804,6 +4115,8 @@ declare class SuperatomSDK {
2804
4115
  private lastPong;
2805
4116
  private readonly PING_INTERVAL_MS;
2806
4117
  private readonly PONG_TIMEOUT_MS;
4118
+ private pendingOutbox;
4119
+ private readonly MAX_OUTBOX_SIZE;
2807
4120
  constructor(config: SuperatomSDKConfig);
2808
4121
  /**
2809
4122
  * Initialize PromptLoader and load prompts into memory
@@ -2848,6 +4161,20 @@ declare class SuperatomSDK {
2848
4161
  * Does NOT throw on closed connections — callers can check the return value if needed.
2849
4162
  */
2850
4163
  send(message: Message): boolean;
4164
+ /**
4165
+ * Queue a message that couldn't be delivered because the socket was down,
4166
+ * to be resent once it reconnects. Drops the oldest entry once the bound is
4167
+ * hit — an outage long enough to fill this queue means the oldest queued
4168
+ * responses are for requests the caller has likely already given up on.
4169
+ */
4170
+ private queuePendingMessage;
4171
+ /**
4172
+ * Resend everything queued while the socket was down, now that it's back
4173
+ * up. Uses this.ws.send() directly (not send()) so a message that fails
4174
+ * again goes back through queuePendingMessage() rather than being silently
4175
+ * dropped a second time.
4176
+ */
4177
+ private flushPendingOutbox;
2851
4178
  /**
2852
4179
  * Register a message handler to receive all messages
2853
4180
  */
@@ -2887,6 +4214,14 @@ declare class SuperatomSDK {
2887
4214
  */
2888
4215
  private handlePong;
2889
4216
  private storeComponents;
4217
+ /**
4218
+ * The live, frontend-registered component catalog (name, type, description,
4219
+ * and full prop schema per component) — the same authoritative source
4220
+ * DASH_COMP_REQ's LLM prompt is built from. Exposed so other integrations
4221
+ * (e.g. the dashboard-agent script bridge) can read real component
4222
+ * contracts instead of maintaining a separate, driftable hand-written copy.
4223
+ */
4224
+ getComponents(): Component[];
2890
4225
  /**
2891
4226
  * Set tools for the SDK instance
2892
4227
  */
@@ -2895,6 +4230,29 @@ declare class SuperatomSDK {
2895
4230
  * Get the stored tools
2896
4231
  */
2897
4232
  getTools(): Tool$1[];
4233
+ /**
4234
+ * Call a registered collection operation in-process — no WebSocket
4235
+ * round-trip, since the caller is already running inside this same SDK
4236
+ * instance. Lets SDK-internal features (e.g. the dashboard agent) reuse
4237
+ * whatever collection a deployment has already registered (e.g.
4238
+ * 'dashboards'.'query') by name/convention, instead of requiring a
4239
+ * separate callback purely to re-expose data a collection already serves.
4240
+ * Throws if the collection or operation isn't registered.
4241
+ */
4242
+ callCollection<TResult = any>(collectionName: string, operation: string, params?: any): Promise<TResult>;
4243
+ /**
4244
+ * Register workflow components for the SDK instance.
4245
+ *
4246
+ * Workflows are pre-built multi-step UI flows the main agent can pick when
4247
+ * the user's prompt matches a workflow's `whenToUse` trigger. Picking a
4248
+ * workflow short-circuits analysis text + dashboard component generation —
4249
+ * the workflow component is returned directly, with the LLM-extracted props.
4250
+ */
4251
+ setWorkflows(workflows: WorkflowDescriptor[]): void;
4252
+ /**
4253
+ * Get the registered workflow components.
4254
+ */
4255
+ getWorkflows(): WorkflowDescriptor[];
2898
4256
  /**
2899
4257
  * Apply model strategy to all LLM provider singletons
2900
4258
  * @param strategy - 'best', 'fast', or 'balanced'
@@ -2925,4 +4283,4 @@ declare class SuperatomSDK {
2925
4283
  getConversationSimilarityThreshold(): number;
2926
4284
  }
2927
4285
 
2928
- export { type Action, type AgentConfig, type AgentResponse, BM25L, type BM25LOptions, type BaseLLMConfig, CONTEXT_CONFIG, type CapturedLog, CleanupService, type CollectionHandler, type CollectionOperation, type DBUIBlock, DEFAULT_AGENT_CONFIG, type DatabaseType, type HybridSearchOptions, type IncomingMessage, type KbNodesQueryFilters, type KbNodesRequestPayload, LLM, type LLMUsageEntry, type LogLevel, MainAgent, type Message, type ModelStrategy, type OutputField, type RerankedResult, STORAGE_CONFIG, SuperatomSDK, type SuperatomSDKConfig, type TaskType, Thread, ThreadManager, type Tool$1 as Tool, type ToolOutputSchema, UIBlock, UILogCollector, type User, UserManager, type UsersData, anthropicLLM, dashboardConversationHistory, geminiLLM, groqLLM, hybridRerank, llmUsageLogger, logger, openaiLLM, queryCache, rerankChromaResults, rerankConversationResults, userPromptErrorLogger };
4286
+ 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 };