@superatomai/sdk-node 0.0.14-mds → 0.0.14-s

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -777,6 +777,14 @@ declare const ToolSchema: z.ZodObject<{
777
777
  description: string;
778
778
  }[];
779
779
  }>>;
780
+ /** Cache policy. `false` = never cache (live data, write ops). Mirrors HTTP `Cache-Control: no-store`. */
781
+ cache: z.ZodOptional<z.ZodUnion<[z.ZodLiteral<false>, z.ZodObject<{
782
+ ttlMs: z.ZodOptional<z.ZodNumber>;
783
+ }, "strip", z.ZodTypeAny, {
784
+ ttlMs?: number | undefined;
785
+ }, {
786
+ ttlMs?: number | undefined;
787
+ }>]>>;
780
788
  }, "strip", z.ZodTypeAny, {
781
789
  id: string;
782
790
  params: Record<string, string>;
@@ -793,6 +801,9 @@ declare const ToolSchema: z.ZodObject<{
793
801
  description: string;
794
802
  }[];
795
803
  } | undefined;
804
+ cache?: false | {
805
+ ttlMs?: number | undefined;
806
+ } | undefined;
796
807
  }, {
797
808
  id: string;
798
809
  params: Record<string, string>;
@@ -809,6 +820,9 @@ declare const ToolSchema: z.ZodObject<{
809
820
  description: string;
810
821
  }[];
811
822
  } | undefined;
823
+ cache?: false | {
824
+ ttlMs?: number | undefined;
825
+ } | undefined;
812
826
  }>;
813
827
  type Tool$1 = z.infer<typeof ToolSchema>;
814
828
  type CollectionOperation = 'getMany' | 'getOne' | 'query' | 'mutation' | 'updateOne' | 'deleteOne' | 'createOne';
@@ -860,6 +874,18 @@ interface SuperatomSDKConfig {
860
874
  * - 'balanced': Use best model for complex tasks, fast model for simple tasks (default)
861
875
  */
862
876
  modelStrategy?: ModelStrategy;
877
+ /**
878
+ * Model for the main agent (routing + analysis).
879
+ * Format: "provider/model-name" (e.g., "anthropic/claude-haiku-4-5-20251001")
880
+ * If not set, uses the provider's default model.
881
+ */
882
+ mainAgentModel?: string;
883
+ /**
884
+ * Model for source agents (per-source query generation).
885
+ * Format: "provider/model-name" (e.g., "anthropic/claude-haiku-4-5-20251001")
886
+ * If not set, uses the provider's default model.
887
+ */
888
+ sourceAgentModel?: string;
863
889
  /**
864
890
  * Separate model configuration for DASH_COMP flow (dashboard component picking)
865
891
  * If not provided, falls back to provider-based model selection
@@ -1294,173 +1320,1055 @@ declare class ReportManager {
1294
1320
  getReportCount(): number;
1295
1321
  }
1296
1322
 
1297
- type SystemPrompt = string | Anthropic.Messages.TextBlockParam[];
1298
- interface LLMMessages {
1299
- sys: SystemPrompt;
1300
- user: string;
1301
- prefill?: string;
1302
- }
1303
- interface LLMOptions {
1304
- model?: string;
1305
- maxTokens?: number;
1306
- temperature?: number;
1307
- topP?: number;
1308
- apiKey?: string;
1309
- partial?: (chunk: string) => void;
1310
- }
1311
- interface Tool {
1312
- name: string;
1313
- description: string;
1314
- input_schema: {
1315
- type: string;
1316
- properties: Record<string, any>;
1317
- required?: string[];
1318
- };
1319
- }
1320
- declare class LLM {
1321
- static text(messages: LLMMessages, options?: LLMOptions): Promise<string>;
1322
- static stream<T = string>(messages: LLMMessages, options?: LLMOptions, json?: boolean): Promise<T extends string ? string : any>;
1323
- static streamWithTools(messages: LLMMessages, tools: Tool[], toolHandler: (toolName: string, toolInput: any) => Promise<any>, options?: LLMOptions, maxIterations?: number): Promise<string>;
1323
+ /**
1324
+ * StreamBuffer - Buffered streaming utility for smoother text delivery
1325
+ * Batches small chunks together and flushes at regular intervals
1326
+ */
1327
+ type StreamCallback = (chunk: string) => void;
1328
+ /**
1329
+ * StreamBuffer class for managing buffered streaming output
1330
+ * Provides smooth text delivery by batching small chunks
1331
+ */
1332
+ declare class StreamBuffer {
1333
+ private buffer;
1334
+ private flushTimer;
1335
+ private callback;
1336
+ private fullText;
1337
+ constructor(callback?: StreamCallback);
1324
1338
  /**
1325
- * Normalize system prompt to Anthropic format
1326
- * Converts string to array format if needed
1327
- * @param sys - System prompt (string or array of blocks)
1328
- * @returns Normalized system prompt for Anthropic API
1339
+ * Check if the buffer has a callback configured
1329
1340
  */
1330
- private static _normalizeSystemPrompt;
1341
+ hasCallback(): boolean;
1331
1342
  /**
1332
- * Log cache usage metrics from Anthropic API response
1333
- * Shows cache hits, costs, and savings
1343
+ * Get all text that has been written (including already flushed)
1334
1344
  */
1335
- private static _logCacheUsage;
1345
+ getFullText(): string;
1336
1346
  /**
1337
- * Parse model string to extract provider and model name
1338
- * @param modelString - Format: "provider/model-name" or just "model-name"
1339
- * @returns [provider, modelName]
1347
+ * Write a chunk to the buffer
1348
+ * Large chunks or chunks with newlines are flushed immediately
1349
+ * Small chunks are batched and flushed after a short interval
1340
1350
  *
1341
- * @example
1342
- * "anthropic/claude-sonnet-4-5" → ["anthropic", "claude-sonnet-4-5"]
1343
- * "groq/openai/gpt-oss-120b" → ["groq", "openai/gpt-oss-120b"]
1344
- * "claude-sonnet-4-5" → ["anthropic", "claude-sonnet-4-5"] (default)
1351
+ * @param chunk - Text chunk to write
1345
1352
  */
1346
- private static _parseModel;
1347
- private static _anthropicText;
1348
- private static _anthropicStream;
1349
- private static _anthropicStreamWithTools;
1350
- private static _groqText;
1351
- private static _groqStream;
1352
- private static _geminiText;
1353
- private static _geminiStream;
1353
+ write(chunk: string): void;
1354
1354
  /**
1355
- * Recursively strip unsupported JSON Schema properties for Gemini
1356
- * Gemini doesn't support: additionalProperties, $schema, etc.
1355
+ * Flush the buffer immediately
1356
+ * Call this before tool execution or other operations that need clean output
1357
1357
  */
1358
- private static _cleanSchemaForGemini;
1359
- private static _geminiStreamWithTools;
1360
- private static _openaiText;
1361
- private static _openaiStream;
1362
- private static _openaiStreamWithTools;
1358
+ flush(): void;
1363
1359
  /**
1364
- * Parse JSON string, handling markdown code blocks and surrounding text
1365
- * Enhanced version with jsonrepair to handle malformed JSON from LLMs
1366
- * @param text - Text that may contain JSON wrapped in ```json...``` or with surrounding text
1367
- * @returns Parsed JSON object or array
1360
+ * Internal flush implementation
1368
1361
  */
1369
- private static _parseJSON;
1362
+ private flushNow;
1363
+ /**
1364
+ * Clean up resources
1365
+ * Call this when done with the buffer
1366
+ */
1367
+ dispose(): void;
1370
1368
  }
1371
1369
 
1372
- interface CapturedLog {
1373
- timestamp: number;
1374
- level: 'info' | 'error' | 'warn' | 'debug';
1375
- message: string;
1376
- type?: 'explanation' | 'query' | 'general';
1377
- data?: Record<string, any>;
1370
+ /**
1371
+ * ToolExecutorService - Handles execution of SQL queries and external tools
1372
+ * Extracted from BaseLLM.generateTextResponse for better separation of concerns
1373
+ */
1374
+
1375
+ /**
1376
+ * External tool definition
1377
+ */
1378
+ interface ExternalTool {
1379
+ id: string;
1380
+ name: string;
1381
+ description?: string;
1382
+ /** Tool type: "source" = routed through SourceAgent, "direct" = called directly by MainAgent */
1383
+ toolType?: 'source' | 'direct';
1384
+ /** Full untruncated schema for source agent (all columns visible) */
1385
+ fullSchema?: string;
1386
+ /** Schema size tier: small (≤50 tables), medium (51-200), large (201-500), very_large (500+) */
1387
+ schemaTier?: string;
1388
+ /** Schema search function for very_large tier — keyword search over entities */
1389
+ schemaSearchFn?: (keywords: string[]) => string;
1390
+ fn: (input: any) => Promise<any>;
1391
+ limit?: number;
1392
+ outputSchema?: any;
1393
+ executionType?: 'immediate' | 'deferred';
1394
+ userProvidedData?: any;
1395
+ params?: Record<string, any>;
1378
1396
  }
1379
1397
  /**
1380
- * UILogCollector captures logs during user prompt processing
1381
- * and sends them to runtime via ui_logs message with uiBlockId as the message id
1382
- * Logs are sent in real-time for streaming effect in the UI
1383
- * Respects the global log level configuration
1398
+ * Executed tool tracking info
1384
1399
  */
1385
- declare class UILogCollector {
1386
- private logs;
1387
- private uiBlockId;
1388
- private clientId;
1389
- private sendMessage;
1390
- private currentLogLevel;
1391
- constructor(clientId: string, sendMessage: (message: Message) => void, uiBlockId?: string);
1392
- /**
1393
- * Check if logging is enabled (uiBlockId is provided)
1394
- */
1395
- isEnabled(): boolean;
1396
- /**
1397
- * Check if a message should be logged based on current log level
1398
- */
1399
- private shouldLog;
1400
- /**
1401
- * Add a log entry with timestamp and immediately send to runtime
1402
- * Only logs that pass the log level filter are captured and sent
1403
- */
1404
- private addLog;
1405
- /**
1406
- * Send a single log to runtime immediately
1407
- */
1408
- private sendLogImmediately;
1409
- /**
1410
- * Log info message
1411
- */
1412
- info(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
1413
- /**
1414
- * Log error message
1415
- */
1416
- error(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
1417
- /**
1418
- * Log warning message
1419
- */
1420
- warn(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
1421
- /**
1422
- * Log debug message
1423
- */
1424
- debug(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
1425
- /**
1426
- * Log LLM explanation with typed metadata
1427
- */
1428
- logExplanation(message: string, explanation: string, data?: Record<string, any>): void;
1429
- /**
1430
- * Log generated query with typed metadata
1431
- */
1432
- logQuery(message: string, query: string, data?: Record<string, any>): void;
1433
- /**
1434
- * Send all collected logs at once (optional, for final summary)
1435
- */
1436
- sendAllLogs(): void;
1437
- /**
1438
- * Get all collected logs
1439
- */
1440
- getLogs(): CapturedLog[];
1441
- /**
1442
- * Clear all logs
1443
- */
1444
- clearLogs(): void;
1445
- /**
1446
- * Set uiBlockId (in case it's provided later)
1447
- */
1448
- setUIBlockId(uiBlockId: string): void;
1400
+ interface ExecutedToolInfo {
1401
+ id: string;
1402
+ name: string;
1403
+ params: any;
1404
+ result: {
1405
+ _totalRecords: number;
1406
+ _recordsShown: number;
1407
+ _metadata?: any;
1408
+ _sampleData: any[];
1409
+ };
1410
+ outputSchema?: any;
1411
+ sourceSchema?: string;
1412
+ sourceType?: string;
1449
1413
  }
1450
1414
 
1451
1415
  /**
1452
- * Represents an action that can be performed on a UIBlock
1416
+ * Multi-Agent Architecture Types
1417
+ *
1418
+ * Defines interfaces for the hierarchical agent system:
1419
+ * - Main Agent: ONE LLM.streamWithTools() call with source agent tools
1420
+ * - Source Agents: independent agents that query individual data sources
1421
+ *
1422
+ * The main agent sees only source summaries. When it calls a source tool,
1423
+ * the SourceAgent runs independently (own LLM, own retries) and returns clean data.
1453
1424
  */
1454
- interface Action {
1425
+
1426
+ /**
1427
+ * Per-entity detail: name, row count, and column names.
1428
+ * Gives the main agent enough context to route to the right source.
1429
+ */
1430
+ interface EntityDetail {
1431
+ /** Entity name (table, sheet, endpoint) */
1432
+ name: string;
1433
+ /** Approximate row count */
1434
+ rowCount?: number;
1435
+ /** Column/field names */
1436
+ columns: string[];
1437
+ }
1438
+ /**
1439
+ * Representation of a data source for the main agent.
1440
+ * Contains entity names WITH column names so the LLM can route accurately.
1441
+ */
1442
+ interface SourceSummary {
1443
+ /** Source ID (matches tool ID prefix) */
1455
1444
  id: string;
1445
+ /** Human-readable source name */
1456
1446
  name: string;
1447
+ /** Source type: postgres, excel, rest_api, etc. */
1457
1448
  type: string;
1458
- [key: string]: any;
1449
+ /** Brief description of what data this source contains */
1450
+ description: string;
1451
+ /** Detailed entity info with column names for routing */
1452
+ entityDetails: EntityDetail[];
1453
+ /** The tool ID associated with this source */
1454
+ toolId: string;
1459
1455
  }
1460
-
1461
1456
  /**
1462
- * UIBlock represents a single user and assistant message block in a thread
1463
- * Contains user question, component metadata, component data, text response, and available actions
1457
+ * What a source agent returns after querying its data source.
1458
+ * The main agent uses this to analyze and compose the final response.
1459
+ */
1460
+ interface SourceAgentResult {
1461
+ /** Source ID */
1462
+ sourceId: string;
1463
+ /** Source name */
1464
+ sourceName: string;
1465
+ /** Whether the query succeeded */
1466
+ success: boolean;
1467
+ /** Result data rows */
1468
+ data: any[];
1469
+ /** Metadata about the query execution */
1470
+ metadata: SourceAgentMetadata;
1471
+ /** Tool execution info for the last successful query (backward compat) */
1472
+ executedTool: ExecutedToolInfo;
1473
+ /** All successful tool executions (primary + follow-up queries) */
1474
+ allExecutedTools?: ExecutedToolInfo[];
1475
+ /** Error message if failed */
1476
+ error?: string;
1477
+ }
1478
+ interface SourceAgentMetadata {
1479
+ /** Total rows that matched the query (before limit) */
1480
+ totalRowsMatched: number;
1481
+ /** Rows actually returned (after limit) */
1482
+ rowsReturned: number;
1483
+ /** Whether the result was truncated by the row limit */
1484
+ isLimited: boolean;
1485
+ /** The query/params that were executed */
1486
+ queryExecuted?: string;
1487
+ /** Execution time in milliseconds */
1488
+ executionTimeMs: number;
1489
+ }
1490
+ /**
1491
+ * A pre-built, multi-step UI flow registered with the SDK.
1492
+ *
1493
+ * When the main agent decides a user's question matches a workflow's whenToUse
1494
+ * trigger, it picks the workflow instead of running source agents / generating
1495
+ * dashboard components. The LLM extracts the workflow's required props from the
1496
+ * prompt (using `propsSchema` as the tool input_schema) and the SDK returns the
1497
+ * workflow component directly — no analysis text, no chart generation. The
1498
+ * frontend renders the registered workflow component with the LLM-extracted
1499
+ * props.
1500
+ */
1501
+ interface WorkflowDescriptor {
1502
+ /** Unique workflow id (used as the LLM tool name) */
1503
+ id: string;
1504
+ /** Component name on the frontend (matches the registered React component) */
1505
+ name: string;
1506
+ /** Short human-readable description of what this workflow does */
1507
+ description: string;
1508
+ /**
1509
+ * 1–2 sentence trigger condition. The LLM uses this to decide if the
1510
+ * user's prompt matches this workflow. Be specific — e.g.
1511
+ * "User wants to *initiate* an inventory transfer (review + submit POs),
1512
+ * not just see analysis or charts."
1513
+ */
1514
+ whenToUse: string;
1515
+ /**
1516
+ * JSON-schema-style description of the props the workflow needs. Becomes
1517
+ * the LLM tool's input_schema, so the model fills these from the prompt.
1518
+ * Use the same shape as `params` on direct tools — string descriptors with
1519
+ * an optional "(optional)" suffix.
1520
+ *
1521
+ * Example:
1522
+ * ```
1523
+ * {
1524
+ * selectedStore: 'object — { id, name } of the source branch',
1525
+ * minROI: 'number (optional) — only show transfers with ROI ≥ this',
1526
+ * }
1527
+ * ```
1528
+ */
1529
+ propsSchema: Record<string, string>;
1530
+ /**
1531
+ * Optional: static prop defaults merged with LLM-extracted props before
1532
+ * the component is returned. Useful for things like the embedded
1533
+ * `externalTool` config that the workflow uses to fetch its own data.
1534
+ */
1535
+ defaultProps?: Record<string, any>;
1536
+ }
1537
+ /**
1538
+ * The workflow selection captured during a routing call.
1539
+ * Set on AgentResponse when the LLM picks a workflow tool.
1540
+ */
1541
+ interface SelectedWorkflow {
1542
+ /** Component name (matches WorkflowDescriptor.name) */
1543
+ name: string;
1544
+ /** Props extracted from the prompt + merged with workflow.defaultProps */
1545
+ props: Record<string, any>;
1546
+ }
1547
+ /**
1548
+ * The complete response from the multi-agent system.
1549
+ * Contains everything needed for text display + component generation.
1550
+ */
1551
+ interface AgentResponse {
1552
+ /** Generated text response (analysis of the data) */
1553
+ text: string;
1554
+ /** All executed tools across all source agents (for component generation) */
1555
+ executedTools: ExecutedToolInfo[];
1556
+ /** Individual results from each source agent */
1557
+ sourceResults: SourceAgentResult[];
1558
+ /**
1559
+ * Populated when MainAgent wrote AND successfully executed a script during its turn.
1560
+ * Caller (agent-user-response.ts) persists it via ScriptStore.save().
1561
+ * Absent when MainAgent didn't write one (trivial question / all attempts failed).
1562
+ */
1563
+ savedScript?: AgentWrittenScript;
1564
+ /**
1565
+ * Set when the LLM routed the question to a registered workflow component.
1566
+ * When present, the upstream caller should skip component generation and
1567
+ * return this workflow as the response.
1568
+ */
1569
+ workflow?: SelectedWorkflow;
1570
+ }
1571
+ /**
1572
+ * A script MainAgent authored + verified during its turn. Shape aligns with
1573
+ * what ScriptStore.save() needs — minus store-assigned fields (id, timestamps, counts).
1574
+ */
1575
+ interface AgentWrittenScript {
1576
+ /**
1577
+ * `ScriptRecipe.id` of the draft that was authored + verified during this turn.
1578
+ * The caller passes this to `ScriptStore.promoteToVerified(recipeId, …)` to
1579
+ * flip the draft to verified status and (when possible) drop the turn-suffix
1580
+ * from its filename.
1581
+ */
1582
+ recipeId: string;
1583
+ name: string;
1584
+ intentDescription: string;
1585
+ tags: string[];
1586
+ parameters: Array<{
1587
+ name: string;
1588
+ type: 'string' | 'number' | 'date' | 'date_range' | 'enum' | 'boolean';
1589
+ required: boolean;
1590
+ default?: any;
1591
+ enumValues?: Record<string, string>;
1592
+ description: string;
1593
+ }>;
1594
+ scriptBody: string;
1595
+ /** Source IDs referenced by the script (extracted from ctx.query calls) */
1596
+ sourceIds: string[];
1597
+ /** Tables referenced in the script's SQL (regex-extracted) */
1598
+ tables: string[];
1599
+ /** Executed queries from the verified run — fed to component generation */
1600
+ executedQueries: Array<{
1601
+ sourceId: string;
1602
+ sourceName: string;
1603
+ sql: string;
1604
+ data: any[];
1605
+ count: number;
1606
+ totalCount?: number;
1607
+ executionTimeMs: number;
1608
+ /**
1609
+ * True for synthetic entries (ctx.emit datasets, the computed:_final
1610
+ * post-JS data). The component generator routes virtual sources through
1611
+ * the script_dataset sentinel toolId so the frontend resolves them via
1612
+ * queryCache instead of attempting to re-execute SQL.
1613
+ */
1614
+ virtual?: boolean;
1615
+ }>;
1616
+ }
1617
+ /**
1618
+ * Configuration for the multi-agent system.
1619
+ * Controls limits, models, and behavior.
1620
+ */
1621
+ interface AgentConfig {
1622
+ /** Max rows a source agent can return (default: 50) */
1623
+ maxRowsPerSource: number;
1624
+ /** Model for the main agent (routing + analysis in one LLM call) */
1625
+ mainAgentModel: string;
1626
+ /** Model for source agent query generation */
1627
+ sourceAgentModel: string;
1628
+ /** API key for LLM calls */
1629
+ apiKey?: string;
1630
+ /** Max retry attempts per source agent */
1631
+ maxRetries: number;
1632
+ /** Max tool calling iterations for the main agent loop */
1633
+ maxIterations: number;
1634
+ /** Global knowledge base context (static, same for all users/questions — cached in system prompt) */
1635
+ globalKnowledgeBase?: string;
1636
+ /** Per-request knowledge base context (user-specific + query-matched — dynamic, not cached) */
1637
+ knowledgeBaseContext?: string;
1638
+ /** Collections registry (ChromaDB search hooks) for embedding-based schema + source search */
1639
+ collections?: any;
1640
+ /** Optional project ID for scoping embedding searches */
1641
+ projectId?: string;
1642
+ }
1643
+ /**
1644
+ * Default agent configuration
1645
+ */
1646
+ declare const DEFAULT_AGENT_CONFIG: AgentConfig;
1647
+
1648
+ /**
1649
+ * Script Flow Types
1650
+ *
1651
+ * Defines interfaces for the script-based query architecture:
1652
+ * - ScriptRecipe: metadata for matching, validation, and quality tracking
1653
+ * - ScriptResult: output from executing a script
1654
+ * - ScriptMatch: result from the LLM-based script matcher
1655
+ */
1656
+ /**
1657
+ * Recipe metadata stored alongside each script.
1658
+ * Used for matching, validation, and quality tracking.
1659
+ */
1660
+ interface ScriptRecipe {
1661
+ /** Unique script identifier */
1662
+ id: string;
1663
+ /** Version number (incremented on regeneration) */
1664
+ version: number;
1665
+ /** Human-readable name (e.g., "Revenue by Dimension") */
1666
+ name: string;
1667
+ /** Natural language description of what this script does */
1668
+ intentDescription: string;
1669
+ /** Keyword tags for quick filtering */
1670
+ tags: string[];
1671
+ /** Source tool IDs this script queries (e.g., ["mssql-abc123_query"]) */
1672
+ sourceIds: string[];
1673
+ /** Table names used (for future schema drift detection) */
1674
+ tables: string[];
1675
+ /** Parameter definitions — what can vary */
1676
+ parameters: ScriptParameter[];
1677
+ /** The script function body as a string. Loaded from disk (scripts-store/<fileBase>.ts). */
1678
+ scriptBody: string;
1679
+ /**
1680
+ * On-disk filename stem for the body: scripts-store/<fileBase>.ts.
1681
+ * Editable in the IDE. Decided at authoring time (slug of `name`, with a
1682
+ * short id suffix on collision) and stable across promotion.
1683
+ */
1684
+ fileBase?: string;
1685
+ /** sha256 of the on-disk body — lets the runtime detect manual edits. */
1686
+ bodyHash?: string;
1687
+ /** Project scope (single-VM deployments may leave this undefined). */
1688
+ projectId?: string;
1689
+ /** Times this script was used successfully */
1690
+ successCount: number;
1691
+ /** Times this script failed */
1692
+ failureCount: number;
1693
+ /** ISO timestamp of last usage */
1694
+ lastUsed: string;
1695
+ /** Original user question that created this script */
1696
+ createdFrom: string;
1697
+ /** ISO timestamp */
1698
+ createdAt: string;
1699
+ /** ISO timestamp */
1700
+ updatedAt: string;
1701
+ /**
1702
+ * `recipe.id` of the parent this script was forked from.
1703
+ * Undefined for root scripts (those written from scratch by MainAgent).
1704
+ * See backend/docs/SCRIPT-FLOW-FORK-ADAPT.md.
1705
+ */
1706
+ parentId?: string;
1707
+ /** 0 for root scripts; `parent.forkDepth + 1` for forks. Capped at 3. */
1708
+ forkDepth?: number;
1709
+ /**
1710
+ * Brief description of what this fork changed vs its parent
1711
+ * (sourced from the matcher's `modificationHint`).
1712
+ */
1713
+ forkReason?: string;
1714
+ /**
1715
+ * Validated component specs captured at authoring time. On a tier-high
1716
+ * replay these are rebound to fresh queryIds deterministically — no
1717
+ * component-generation LLM call, and the rendered columns can't drift from
1718
+ * what was validated when the script was authored. Absent on recipes
1719
+ * authored before this landed; those fall back to LLM component generation.
1720
+ * See backend/docs/SCRIPT-COMPONENT-CONSISTENCY.md.
1721
+ */
1722
+ components?: ScriptComponentSpec[];
1723
+ /**
1724
+ * Lifecycle stage of this recipe on disk.
1725
+ * - 'draft': written by MainAgent's write_script during a turn; filtered out
1726
+ * of FTS results (status='verified' only) so the matcher never picks it.
1727
+ * Filename is suffixed with `turnId` to keep concurrent turns
1728
+ * from clobbering each other's drafts.
1729
+ * - 'verified': promoted after `execute_script` succeeded; the matcher sees it.
1730
+ * Filename drops the turn suffix unless a verified file with
1731
+ * the same slug already exists (collision case keeps the suffix).
1732
+ *
1733
+ * Recipes loaded from disk without this field default to 'verified' so
1734
+ * existing scripts keep working unchanged.
1735
+ */
1736
+ status?: 'draft' | 'verified';
1737
+ /**
1738
+ * Per-turn unique suffix used for draft filenames (e.g. `1714745623-x9k2`).
1739
+ * Set when the draft is saved; carried until the recipe is promoted.
1740
+ */
1741
+ turnId?: string;
1742
+ /**
1743
+ * Last execution error captured by `recordDraftError` while the recipe was
1744
+ * still a draft. Lets users open the draft .json file and see why it failed
1745
+ * without grepping logs. Cleared on promotion to 'verified'.
1746
+ */
1747
+ lastError?: {
1748
+ phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
1749
+ message: string;
1750
+ at: string;
1751
+ attempt: number;
1752
+ };
1753
+ }
1754
+ interface ScriptParameter {
1755
+ /** Parameter name (used in script body as params.name) */
1756
+ name: string;
1757
+ /** Parameter type */
1758
+ type: 'string' | 'number' | 'date' | 'date_range' | 'enum' | 'boolean';
1759
+ /** Whether this parameter is required */
1760
+ required: boolean;
1761
+ /** Default value if not provided */
1762
+ default?: any;
1763
+ /** For enum type — maps user-facing values to internal values */
1764
+ enumValues?: Record<string, string>;
1765
+ /** Human-readable description (used in the matcher LLM prompt) */
1766
+ description: string;
1767
+ }
1768
+ /**
1769
+ * A reusable component binding captured when a script is authored. Stored on
1770
+ * the recipe so tier-high replays rebuild components deterministically (rebind
1771
+ * to fresh queryIds) instead of re-running the component-picker LLM.
1772
+ */
1773
+ interface ScriptComponentSpec {
1774
+ /** Registered component name (e.g. "DynamicBarChart") — matched against the available component library. */
1775
+ componentType: string;
1776
+ /** `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). */
1777
+ sourceRef: string;
1778
+ /** Present only when sourceRef === 'federation' — the DuckDB SQL to re-execute on replay. */
1779
+ federationSql?: string;
1780
+ /** Present only when sourceRef === 'markdown' — the narrative text to render on replay (markdown has no data source, so its content must be persisted). */
1781
+ content?: string;
1782
+ title?: string;
1783
+ description?: string;
1784
+ /** Validated axis/value keys + aggregation — all referencing real columns of the bound source. */
1785
+ config: Record<string, any>;
1786
+ }
1787
+ /**
1788
+ * Result from executing a script via ScriptRunner.
1789
+ */
1790
+ interface ScriptResult {
1791
+ /** Whether the script executed successfully */
1792
+ success: boolean;
1793
+ /** Combined data from all queries */
1794
+ data: any[];
1795
+ /** Individual query results tracked during execution */
1796
+ executedQueries: ScriptQueryResult[];
1797
+ /** Error message if failed */
1798
+ error?: string;
1799
+ /**
1800
+ * Where in the lifecycle the error occurred. Lets MainAgent's fix-loop
1801
+ * decide between "rewrite the whole draft" (compile) and "patch the
1802
+ * specific line" (runtime).
1803
+ */
1804
+ errorPhase?: 'compile' | 'runtime' | 'timeout' | 'ipc';
1805
+ /** Total execution time in milliseconds */
1806
+ executionTimeMs: number;
1807
+ }
1808
+ /**
1809
+ * A single query executed during script runtime.
1810
+ * Tracked by ScriptContext for component generation and debugging.
1811
+ */
1812
+ interface ScriptQueryResult {
1813
+ /** Source tool ID */
1814
+ sourceId: string;
1815
+ /** Human-readable source name */
1816
+ sourceName: string;
1817
+ /** The SQL that was executed */
1818
+ sql: string;
1819
+ /** Result data rows */
1820
+ data: any[];
1821
+ /** Number of rows returned */
1822
+ count: number;
1823
+ /** Total rows that matched before limit (if available) */
1824
+ totalCount?: number;
1825
+ /** Query execution time in milliseconds */
1826
+ executionTimeMs: number;
1827
+ /**
1828
+ * True for rows that did NOT come from a real SQL execution — either a
1829
+ * ctx.emit() dataset or the synthesized "computed:_final" entry that
1830
+ * carries the script's post-JS returned data. The component generator
1831
+ * uses this to route the resulting component through the script_dataset
1832
+ * sentinel toolId so the frontend resolves it via the queryCache short-circuit.
1833
+ */
1834
+ virtual?: boolean;
1835
+ }
1836
+ /**
1837
+ * Match tier returned by the LLM script matcher.
1838
+ *
1839
+ * - 'high': the script answers the question directly; only parameter values
1840
+ * may differ. The runtime replays it with extracted params (cheapest path).
1841
+ * - 'near': the script answers a STRUCTURALLY similar question but needs
1842
+ * body modification (different metric, dimension, table, filter shape).
1843
+ * The runtime forks the parent and adapts the body via MainAgent's normal
1844
+ * write_script + execute_script loop — no SourceAgent dispatch needed.
1845
+ * See backend/docs/SCRIPT-FLOW-FORK-ADAPT.md for the full design.
1846
+ * - 'none': no script is relevant; full agent flow runs.
1847
+ */
1848
+ type MatchTier = 'high' | 'near' | 'none';
1849
+ /**
1850
+ * Result from the LLM-based script matcher.
1851
+ *
1852
+ * For `tier: 'high'`, `extractedParams` carries the values to pass to the
1853
+ * existing script. For `tier: 'near'`, `gaps` and `modificationHint` describe
1854
+ * what the fork-author needs to change in the parent body.
1855
+ */
1856
+ interface ScriptMatch {
1857
+ /** The matched script recipe */
1858
+ recipe: ScriptRecipe;
1859
+ /** Match tier — see MatchTier docs */
1860
+ tier: MatchTier;
1861
+ /** Similarity score (0-1, derived from LLM tier) */
1862
+ similarity: number;
1863
+ /**
1864
+ * Legacy confidence level. Mirrors `tier === 'high'`/`'near'` for now;
1865
+ * kept so existing callers compile while we migrate to tier-based logic.
1866
+ */
1867
+ confidence: 'high' | 'medium';
1868
+ /** Parameters extracted from the user question by the LLM (tier='high') */
1869
+ extractedParams?: Record<string, any>;
1870
+ /** What the user question needs that the parent doesn't cover (tier='near') */
1871
+ gaps?: string[];
1872
+ /** One-sentence description of the change the fork-author should make (tier='near') */
1873
+ modificationHint?: string;
1874
+ /** Why the matcher made this choice (for logs and telemetry) */
1875
+ reasoning?: string;
1876
+ }
1877
+
1878
+ /**
1879
+ * ScriptRecipeStore — injected metadata backend for the script flow.
1880
+ *
1881
+ * The SDK is standalone (no DB dependency). The backend implements this
1882
+ * interface over Postgres (full-text search + atomic counters) and injects it
1883
+ * via `collections['script-recipes']`, exactly like `collections['source-embeddings']`.
1884
+ * `ScriptStore` consumes it for all METADATA operations while keeping the
1885
+ * executable body on disk as scripts-store/<fileBase>.ts.
1886
+ *
1887
+ * All metadata rows are plain JSON (no scriptBody — that lives on disk).
1888
+ * See backend/docs/SCRIPT-FLOW-SCALING-ISSUES.md (#1, #3, #7).
1889
+ */
1890
+
1891
+ /** One recipe's metadata as stored in Postgres (mirrors the script_recipes table). */
1892
+ interface ScriptRecipeMetaRow {
1893
+ id: string;
1894
+ projectId?: string | null;
1895
+ version: number;
1896
+ name: string;
1897
+ intentDescription: string;
1898
+ tags: string[] | null;
1899
+ createdFrom: string | null;
1900
+ sourceIds: string[] | null;
1901
+ tables: string[] | null;
1902
+ parameters: ScriptParameter[] | null;
1903
+ components?: ScriptComponentSpec[] | null;
1904
+ fileBase: string;
1905
+ bodyHash?: string | null;
1906
+ successCount: number;
1907
+ failureCount: number;
1908
+ lastUsed: string | null;
1909
+ parentId?: string | null;
1910
+ forkDepth?: number | null;
1911
+ forkReason?: string | null;
1912
+ status: 'draft' | 'verified' | string;
1913
+ turnId?: string | null;
1914
+ lastError?: {
1915
+ phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
1916
+ message: string;
1917
+ at: string;
1918
+ attempt: number;
1919
+ } | null;
1920
+ createdAt?: string | null;
1921
+ updatedAt?: string | null;
1922
+ }
1923
+ interface ScriptRecipeStore {
1924
+ /** FTS shortlist of healthy verified recipes for the matcher (metadata only). */
1925
+ search(params: {
1926
+ prompt: string;
1927
+ projectId?: string;
1928
+ limit?: number;
1929
+ }): Promise<ScriptRecipeMetaRow[]>;
1930
+ /** Fetch one recipe by id (any status). */
1931
+ getById(id: string): Promise<ScriptRecipeMetaRow | null>;
1932
+ /** Count healthy verified recipes (drives the "any scripts?" gate). */
1933
+ count(params?: {
1934
+ projectId?: string;
1935
+ }): Promise<number>;
1936
+ /** Insert or update a recipe row (keyed by id). */
1937
+ upsert(row: ScriptRecipeMetaRow): Promise<void>;
1938
+ /** Atomically bump counters / last-used. */
1939
+ updateStats(id: string, patch: {
1940
+ successDelta?: number;
1941
+ failureDelta?: number;
1942
+ lastUsed?: string;
1943
+ }): Promise<void>;
1944
+ /** Flip a draft to verified, applying provenance + optional fork lineage. */
1945
+ promote(id: string, patch: {
1946
+ sourceIds: string[];
1947
+ tables: string[];
1948
+ fileBase?: string;
1949
+ parentId?: string;
1950
+ forkDepth?: number;
1951
+ forkReason?: string;
1952
+ components?: ScriptComponentSpec[];
1953
+ }): Promise<ScriptRecipeMetaRow | null>;
1954
+ /** Stamp a draft's last execution error. */
1955
+ recordDraftError(id: string, err: {
1956
+ phase: string;
1957
+ message: string;
1958
+ attempt: number;
1959
+ at: string;
1960
+ }): Promise<void>;
1961
+ /** Delete a recipe row (body file removed separately). */
1962
+ remove(id: string): Promise<void>;
1963
+ /** True if `fileBase` is taken by a different recipe in this project. */
1964
+ fileBaseTaken(fileBase: string, excludeId: string, projectId?: string): Promise<boolean>;
1965
+ }
1966
+ /** Pull the injected store off the collections bag (or null if not wired). */
1967
+ declare function resolveScriptRecipeStore(collections: any): ScriptRecipeStore | null;
1968
+
1969
+ /**
1970
+ * ScriptStore — Postgres metadata + on-disk body for script recipes.
1971
+ *
1972
+ * Split of responsibilities:
1973
+ * - METADATA → injected `ScriptRecipeStore` (Postgres FTS + atomic counters),
1974
+ * resolved from `collections['script-recipes']`.
1975
+ * - BODY → scripts-store/<fileBase>.ts, editable in your IDE. Written
1976
+ * atomically (temp + rename); `bodyHash` (sha256) detects edits.
1977
+ *
1978
+ * The old "read every file every turn + send the whole catalog to the LLM"
1979
+ * matcher is gone — matching is `store.search(prompt)` (FTS shortlist). The
1980
+ * draft/verified filename dance is gone too: `status` is a DB column and the
1981
+ * file keeps a stable `<fileBase>.ts` name across promotion.
1982
+ *
1983
+ * When no metadata store is injected, the store degrades to a safe no-op
1984
+ * (count 0 → script flow disabled) instead of crashing.
1985
+ *
1986
+ * See backend/docs/SCRIPT-FLOW-SCALING-ISSUES.md.
1987
+ */
1988
+
1989
+ interface SaveDraftInput {
1990
+ /** Reuse an existing draft (retry); omit to mint a new one. */
1991
+ recipeId?: string;
1992
+ /** Per-turn unique suffix, stable across retries within the turn. */
1993
+ turnId: string;
1994
+ name: string;
1995
+ intentDescription: string;
1996
+ tags: string[];
1997
+ parameters: ScriptParameter[];
1998
+ scriptBody: string;
1999
+ createdFrom: string;
2000
+ }
2001
+ interface PromoteToVerifiedInput {
2002
+ sourceIds: string[];
2003
+ tables: string[];
2004
+ parentId?: string;
2005
+ forkDepth?: number;
2006
+ forkReason?: string;
2007
+ components?: ScriptComponentSpec[];
2008
+ }
2009
+ interface ScriptStoreOptions {
2010
+ /** Explicit metadata store, or resolved from `collections['script-recipes']`. */
2011
+ store?: ScriptRecipeStore | null;
2012
+ collections?: any;
2013
+ /** Body directory (defaults to <cwd>/scripts-store). */
2014
+ baseDir?: string;
2015
+ /** Project scope stamped on every row. */
2016
+ projectId?: string;
2017
+ }
2018
+ /**
2019
+ * Normalize a scriptBody into the on-disk form (strip a leading comment block,
2020
+ * ensure `export async function getData`). Exported for MainAgent.
2021
+ */
2022
+ declare function normalizeScriptBody(scriptBody: string): string;
2023
+ declare class ScriptStore {
2024
+ private store;
2025
+ private storeDir;
2026
+ private projectId?;
2027
+ constructor(opts?: ScriptStoreOptions);
2028
+ /** Whether a metadata store is wired (matcher / authoring are gated on this). */
2029
+ hasStore(): boolean;
2030
+ /** Number of healthy verified recipes (gates the script-matching path). */
2031
+ count(): Promise<number>;
2032
+ /**
2033
+ * FTS shortlist for the matcher (metadata only — bodies are loaded lazily by
2034
+ * `get()` once the LLM picks one). Returns verified, healthy recipes ranked
2035
+ * by relevance.
2036
+ */
2037
+ search(prompt: string, limit?: number): Promise<ScriptRecipe[]>;
2038
+ /** Fetch one recipe by id with its body loaded from disk. */
2039
+ get(id: string): Promise<ScriptRecipe | null>;
2040
+ /** Create or update a recipe (metadata upsert + body write when changed). */
2041
+ save(recipe: ScriptRecipe): Promise<void>;
2042
+ /**
2043
+ * Persist (or update) a draft. Within a turn, retries that pass the same
2044
+ * `recipeId` overwrite the same row + file; a fresh `recipeId` mints a new
2045
+ * draft. The body is visible at scripts-store/<fileBase>.ts immediately.
2046
+ */
2047
+ saveDraft(input: SaveDraftInput): Promise<ScriptRecipe>;
2048
+ /** Stamp a draft's last execution error (metadata only). */
2049
+ recordDraftError(recipeId: string, err: {
2050
+ phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
2051
+ message: string;
2052
+ attempt: number;
2053
+ }): Promise<void>;
2054
+ /**
2055
+ * Promote a successfully-executed draft into a verified script.
2056
+ * The on-disk body already exists at <fileBase>.ts (written at write_script
2057
+ * time) and keeps its name — only the DB row flips status + provenance.
2058
+ */
2059
+ promoteToVerified(recipeId: string, input: PromoteToVerifiedInput): Promise<ScriptRecipe | null>;
2060
+ /**
2061
+ * Drop a draft (row + body file). MainAgent calls this at end-of-turn when a
2062
+ * draft was authored but never verified — failed drafts are never matched, so
2063
+ * deleting them immediately avoids unbounded accumulation (#5). No-op if the
2064
+ * recipe isn't a draft (so a promoted/verified script is never removed here).
2065
+ */
2066
+ discardDraft(recipeId: string): Promise<void>;
2067
+ /** Delete a recipe (row + body file). */
2068
+ delete(id: string): Promise<void>;
2069
+ /** Record a successful execution (atomic counter bump). */
2070
+ recordSuccess(id: string): Promise<void>;
2071
+ /** Record a failed execution (atomic counter bump). */
2072
+ recordFailure(id: string): Promise<void>;
2073
+ /** Absolute path to the .ts body for a recipe (used by the runner/MainAgent). */
2074
+ getScriptPath(recipe: ScriptRecipe): string;
2075
+ private removeById;
2076
+ private rowToRecipe;
2077
+ private recipeToRow;
2078
+ /** slug of name, with a short id suffix when the bare slug is already taken. */
2079
+ private computeFileBase;
2080
+ private toSlug;
2081
+ private hash;
2082
+ private bodyPath;
2083
+ private readBody;
2084
+ /** Atomic body write (temp + rename) so concurrent reads never see a partial file. */
2085
+ private writeBody;
2086
+ private unlinkBody;
2087
+ }
2088
+
2089
+ /**
2090
+ * Main Agent (Orchestrator)
2091
+ *
2092
+ * A single LLM.streamWithTools() call that handles everything:
2093
+ * - Routing: decides which source(s) to query based on summaries
2094
+ * - Querying: calls source tools (each wraps an independent SourceAgent)
2095
+ * - Direct tools: calls pre-built function tools directly with LLM-provided params
2096
+ * - Re-querying: if data is wrong/incomplete, calls tools again with modified intent
2097
+ * - Analysis: generates final text response from the data
2098
+ *
2099
+ * Two tool types:
2100
+ * - "source" tools: main agent sees summaries, SourceAgent handles SQL generation independently
2101
+ * - "direct" tools: main agent calls fn() directly with structured params (no SourceAgent)
2102
+ */
2103
+
2104
+ declare class MainAgent {
2105
+ private externalTools;
2106
+ private workflows;
2107
+ private config;
2108
+ private streamBuffer;
2109
+ /**
2110
+ * Optional: when provided, MainAgent exposes the `write_script` /
2111
+ * `execute_script` tools to the LLM and persists drafts to disk via the
2112
+ * store. Headless callers (alert analyzer, metric resolver) omit these to
2113
+ * suppress script authoring entirely — drafts would otherwise leak onto
2114
+ * disk with no caller to promote or clean them up.
2115
+ */
2116
+ private scriptStore;
2117
+ private turnId;
2118
+ private createdFromPrompt;
2119
+ private scriptState;
2120
+ constructor(externalTools: ExternalTool[], config: AgentConfig, scriptStore?: ScriptStore, turnId?: string, streamBuffer?: StreamBuffer, workflows?: WorkflowDescriptor[]);
2121
+ private get scriptingEnabled();
2122
+ /**
2123
+ * Handle a user question using the multi-agent system.
2124
+ *
2125
+ * This is ONE LLM.streamWithTools() call. The LLM:
2126
+ * 1. Sees source summaries + direct tool descriptions in system prompt
2127
+ * 2. Decides which tool(s) to call (routing)
2128
+ * 3. Source tools → SourceAgent runs independently → returns data
2129
+ * 4. Direct tools → fn() called directly with LLM params → returns data
2130
+ * 5. Generates final analysis text
2131
+ */
2132
+ handleQuestion(userPrompt: string, apiKey?: string, conversationHistory?: string, streamCallback?: (chunk: string) => void): Promise<AgentResponse>;
2133
+ private handleWriteScript;
2134
+ private handleExecuteScript;
2135
+ /**
2136
+ * Build the AgentWrittenScript payload the caller will hand to
2137
+ * `ScriptStore.promoteToVerified()`. Only returned when a verified
2138
+ * successful execution is on record.
2139
+ */
2140
+ private buildSavedScript;
2141
+ private normalizeParameterList;
2142
+ /**
2143
+ * Use the schema embedding collection to pre-select relevant tables for
2144
+ * this source + intent. Returns a formatted schema block if confidence is
2145
+ * high (top match ≥ 0.55 and ≥3 candidates), otherwise null.
2146
+ *
2147
+ * When this returns a block, we can skip the SourceAgent's `search_schema`
2148
+ * loop and reduce iteration budget. When it returns null, the SourceAgent
2149
+ * falls back to the existing LLM-driven keyword search (same as today).
2150
+ */
2151
+ private preResolveSchema;
2152
+ /**
2153
+ * Execute a direct tool — call fn() with LLM-provided params, no SourceAgent.
2154
+ */
2155
+ private handleDirectTool;
2156
+ /**
2157
+ * Build the main agent's system prompt with source summaries, direct tool descriptions,
2158
+ * and workflow component descriptions.
2159
+ */
2160
+ private buildSystemPrompt;
2161
+ /**
2162
+ * Build tool definitions for source tools — summary-only descriptions.
2163
+ * The full schema is inside the SourceAgent which runs independently.
2164
+ */
2165
+ private buildSourceToolDefinitions;
2166
+ /**
2167
+ * Build tool definitions for direct tools — expose their actual params.
2168
+ * These are called directly by the main agent LLM, no SourceAgent.
2169
+ */
2170
+ private buildDirectToolDefinitions;
2171
+ /**
2172
+ * Capture a workflow selection. We do NOT execute anything — the LLM has
2173
+ * already extracted the props it wants the workflow rendered with. We
2174
+ * record the selection (via the capture callback) and return a short
2175
+ * acknowledgement so the LLM ends its turn cleanly without writing
2176
+ * analysis text or calling more tools.
2177
+ */
2178
+ private handleWorkflow;
2179
+ /**
2180
+ * Build LLM tool definitions for workflow components. The workflow's
2181
+ * propsSchema becomes the tool's input_schema so the LLM extracts props
2182
+ * directly from the prompt — same mechanic as direct tools.
2183
+ */
2184
+ private buildWorkflowToolDefinitions;
2185
+ /**
2186
+ * Format a source agent's result as a clean string for the main agent LLM.
2187
+ */
2188
+ private formatResultForMainAgent;
2189
+ /**
2190
+ * Get source summaries (for external inspection/debugging).
2191
+ */
2192
+ getSourceSummaries(): SourceSummary[];
2193
+ }
2194
+
2195
+ /**
2196
+ * Represents an action that can be performed on a UIBlock
2197
+ */
2198
+ interface Action {
2199
+ id: string;
2200
+ name: string;
2201
+ type: string;
2202
+ [key: string]: any;
2203
+ }
2204
+
2205
+ type SystemPrompt = string | Anthropic.Messages.TextBlockParam[];
2206
+ interface LLMMessages {
2207
+ sys: SystemPrompt;
2208
+ user: string;
2209
+ prefill?: string;
2210
+ }
2211
+ interface LLMOptions {
2212
+ model?: string;
2213
+ maxTokens?: number;
2214
+ temperature?: number;
2215
+ topP?: number;
2216
+ apiKey?: string;
2217
+ partial?: (chunk: string) => void;
2218
+ }
2219
+ interface Tool {
2220
+ name: string;
2221
+ description: string;
2222
+ input_schema: {
2223
+ type: string;
2224
+ properties: Record<string, any>;
2225
+ required?: string[];
2226
+ };
2227
+ }
2228
+ declare class LLM {
2229
+ static text(messages: LLMMessages, options?: LLMOptions): Promise<string>;
2230
+ static stream<T = string>(messages: LLMMessages, options?: LLMOptions, json?: boolean): Promise<T extends string ? string : any>;
2231
+ static streamWithTools(messages: LLMMessages, tools: Tool[], toolHandler: (toolName: string, toolInput: any) => Promise<any>, options?: LLMOptions, maxIterations?: number): Promise<string>;
2232
+ /**
2233
+ * Normalize system prompt to Anthropic format
2234
+ * Converts string to array format if needed
2235
+ * @param sys - System prompt (string or array of blocks)
2236
+ * @returns Normalized system prompt for Anthropic API
2237
+ */
2238
+ private static _normalizeSystemPrompt;
2239
+ /**
2240
+ * Strip unpaired UTF-16 surrogates from every text field of a message set.
2241
+ *
2242
+ * A lone surrogate (from mid-pair string slicing or corrupt source data)
2243
+ * serializes to a bare `\udXXX` escape that strict JSON parsers — including
2244
+ * the one on Anthropic's API — reject with "no low surrogate in string",
2245
+ * failing the whole request. Sanitizing here, at the single boundary every
2246
+ * provider call flows through, guarantees no request can carry one.
2247
+ */
2248
+ private static _sanitizeMessages;
2249
+ /**
2250
+ * Log cache usage metrics from Anthropic API response
2251
+ * Shows cache hits, costs, and savings
2252
+ */
2253
+ private static _logCacheUsage;
2254
+ /**
2255
+ * Parse model string to extract provider and model name
2256
+ * @param modelString - Format: "provider/model-name" or just "model-name"
2257
+ * @returns [provider, modelName]
2258
+ *
2259
+ * @example
2260
+ * "anthropic/claude-sonnet-4-5" → ["anthropic", "claude-sonnet-4-5"]
2261
+ * "groq/openai/gpt-oss-120b" → ["groq", "openai/gpt-oss-120b"]
2262
+ * "claude-sonnet-4-5" → ["anthropic", "claude-sonnet-4-5"] (default)
2263
+ */
2264
+ private static _parseModel;
2265
+ private static _anthropicText;
2266
+ private static _anthropicStream;
2267
+ private static _anthropicStreamWithTools;
2268
+ private static _groqText;
2269
+ private static _groqStream;
2270
+ private static _geminiText;
2271
+ private static _geminiStream;
2272
+ /**
2273
+ * Recursively strip unsupported JSON Schema properties for Gemini
2274
+ * Gemini doesn't support: additionalProperties, $schema, etc.
2275
+ */
2276
+ private static _cleanSchemaForGemini;
2277
+ private static _geminiStreamWithTools;
2278
+ private static _openaiText;
2279
+ private static _openaiStream;
2280
+ private static _openaiStreamWithTools;
2281
+ /**
2282
+ * Parse JSON string, handling markdown code blocks and surrounding text
2283
+ * Enhanced version with jsonrepair to handle malformed JSON from LLMs
2284
+ * @param text - Text that may contain JSON wrapped in ```json...``` or with surrounding text
2285
+ * @returns Parsed JSON object or array
2286
+ */
2287
+ private static _parseJSON;
2288
+ }
2289
+
2290
+ interface CapturedLog {
2291
+ timestamp: number;
2292
+ level: 'info' | 'error' | 'warn' | 'debug';
2293
+ message: string;
2294
+ type?: 'explanation' | 'query' | 'general';
2295
+ data?: Record<string, any>;
2296
+ }
2297
+ /**
2298
+ * UILogCollector captures logs during user prompt processing
2299
+ * and sends them to runtime via ui_logs message with uiBlockId as the message id
2300
+ * Logs are sent in real-time for streaming effect in the UI
2301
+ * Respects the global log level configuration
2302
+ */
2303
+ declare class UILogCollector {
2304
+ private logs;
2305
+ private uiBlockId;
2306
+ private clientId;
2307
+ private sendMessage;
2308
+ private currentLogLevel;
2309
+ constructor(clientId: string, sendMessage: (message: Message) => void, uiBlockId?: string);
2310
+ /**
2311
+ * Check if logging is enabled (uiBlockId is provided)
2312
+ */
2313
+ isEnabled(): boolean;
2314
+ /**
2315
+ * Check if a message should be logged based on current log level
2316
+ */
2317
+ private shouldLog;
2318
+ /**
2319
+ * Add a log entry with timestamp and immediately send to runtime
2320
+ * Only logs that pass the log level filter are captured and sent
2321
+ */
2322
+ private addLog;
2323
+ /**
2324
+ * Send a single log to runtime immediately
2325
+ */
2326
+ private sendLogImmediately;
2327
+ /**
2328
+ * Log info message
2329
+ */
2330
+ info(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
2331
+ /**
2332
+ * Log error message
2333
+ */
2334
+ error(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
2335
+ /**
2336
+ * Log warning message
2337
+ */
2338
+ warn(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
2339
+ /**
2340
+ * Log debug message
2341
+ */
2342
+ debug(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
2343
+ /**
2344
+ * Log LLM explanation with typed metadata
2345
+ */
2346
+ logExplanation(message: string, explanation: string, data?: Record<string, any>): void;
2347
+ /**
2348
+ * Log generated query with typed metadata
2349
+ */
2350
+ logQuery(message: string, query: string, data?: Record<string, any>): void;
2351
+ /**
2352
+ * Send all collected logs at once (optional, for final summary)
2353
+ */
2354
+ sendAllLogs(): void;
2355
+ /**
2356
+ * Get all collected logs
2357
+ */
2358
+ getLogs(): CapturedLog[];
2359
+ /**
2360
+ * Clear all logs
2361
+ */
2362
+ clearLogs(): void;
2363
+ /**
2364
+ * Set uiBlockId (in case it's provided later)
2365
+ */
2366
+ setUIBlockId(uiBlockId: string): void;
2367
+ }
2368
+
2369
+ /**
2370
+ * UIBlock represents a single user and assistant message block in a thread
2371
+ * Contains user question, component metadata, component data, text response, and available actions
1464
2372
  */
1465
2373
  declare class UIBlock {
1466
2374
  private id;
@@ -1640,12 +2548,20 @@ declare class Thread {
1640
2548
 
1641
2549
  /**
1642
2550
  * ThreadManager manages all threads globally
1643
- * Provides methods to create, retrieve, and delete threads
2551
+ * Provides methods to create, retrieve, and delete threads.
2552
+ * Includes automatic cleanup to prevent unbounded memory growth.
1644
2553
  */
1645
2554
  declare class ThreadManager {
1646
2555
  private static instance;
1647
2556
  private threads;
2557
+ private cleanupInterval;
2558
+ private readonly threadTtlMs;
1648
2559
  private constructor();
2560
+ /**
2561
+ * Periodically remove threads older than 7 days.
2562
+ * Runs every hour to avoid frequent iteration over the map.
2563
+ */
2564
+ private startCleanup;
1649
2565
  /**
1650
2566
  * Get singleton instance of ThreadManager
1651
2567
  */
@@ -2089,132 +3005,45 @@ declare class QueryExecutionService {
2089
3005
  private config;
2090
3006
  constructor(config: QueryExecutionServiceConfig);
2091
3007
  /**
2092
- * Get the cache key for a query
2093
- * This ensures the cache key matches what the frontend will send
2094
- */
2095
- getQueryCacheKey(query: any): string;
2096
- /**
2097
- * Execute a query against the database
2098
- * @param query - The SQL query to execute (string or object with sql/values)
2099
- * @param collections - Collections object containing database execute function
2100
- * @returns Object with result data and cache key
2101
- */
2102
- executeQuery(query: any, collections: any): Promise<{
2103
- result: any;
2104
- cacheKey: string;
2105
- }>;
2106
- /**
2107
- * Request the LLM to fix a failed SQL query
2108
- * @param failedQuery - The query that failed execution
2109
- * @param errorMessage - The error message from the failed execution
2110
- * @param componentContext - Context about the component
2111
- * @param apiKey - Optional API key
2112
- * @returns Fixed query string
2113
- */
2114
- requestQueryFix(failedQuery: string, errorMessage: string, componentContext: ComponentContext, apiKey?: string): Promise<string>;
2115
- /**
2116
- * Validate a single component's query with retry logic
2117
- * @param component - The component to validate
2118
- * @param collections - Collections object containing database execute function
2119
- * @param apiKey - Optional API key for LLM calls
2120
- * @returns Validation result with component, query key, and result
2121
- */
2122
- validateSingleQuery(component: Component, collections: any, apiKey?: string): Promise<QueryValidationResult>;
2123
- /**
2124
- * Validate multiple component queries in parallel
2125
- * @param components - Array of components with potential queries
2126
- * @param collections - Collections object containing database execute function
2127
- * @param apiKey - Optional API key for LLM calls
2128
- * @returns Object with validated components and query results map
2129
- */
2130
- validateComponentQueries(components: Component[], collections: any, apiKey?: string): Promise<BatchValidationResult>;
2131
- }
2132
-
2133
- /**
2134
- * StreamBuffer - Buffered streaming utility for smoother text delivery
2135
- * Batches small chunks together and flushes at regular intervals
2136
- */
2137
- type StreamCallback = (chunk: string) => void;
2138
- /**
2139
- * StreamBuffer class for managing buffered streaming output
2140
- * Provides smooth text delivery by batching small chunks
2141
- */
2142
- declare class StreamBuffer {
2143
- private buffer;
2144
- private flushTimer;
2145
- private callback;
2146
- private fullText;
2147
- constructor(callback?: StreamCallback);
2148
- /**
2149
- * Check if the buffer has a callback configured
2150
- */
2151
- hasCallback(): boolean;
2152
- /**
2153
- * Get all text that has been written (including already flushed)
3008
+ * Get the cache key for a query
3009
+ * This ensures the cache key matches what the frontend will send
2154
3010
  */
2155
- getFullText(): string;
3011
+ getQueryCacheKey(query: any): string;
2156
3012
  /**
2157
- * Write a chunk to the buffer
2158
- * Large chunks or chunks with newlines are flushed immediately
2159
- * Small chunks are batched and flushed after a short interval
2160
- *
2161
- * @param chunk - Text chunk to write
3013
+ * Execute a query against the database
3014
+ * @param query - The SQL query to execute (string or object with sql/values)
3015
+ * @param collections - Collections object containing database execute function
3016
+ * @returns Object with result data and cache key
2162
3017
  */
2163
- write(chunk: string): void;
3018
+ executeQuery(query: any, collections: any): Promise<{
3019
+ result: any;
3020
+ cacheKey: string;
3021
+ }>;
2164
3022
  /**
2165
- * Flush the buffer immediately
2166
- * Call this before tool execution or other operations that need clean output
3023
+ * Request the LLM to fix a failed SQL query
3024
+ * @param failedQuery - The query that failed execution
3025
+ * @param errorMessage - The error message from the failed execution
3026
+ * @param componentContext - Context about the component
3027
+ * @param apiKey - Optional API key
3028
+ * @returns Fixed query string
2167
3029
  */
2168
- flush(): void;
3030
+ requestQueryFix(failedQuery: string, errorMessage: string, componentContext: ComponentContext, apiKey?: string): Promise<string>;
2169
3031
  /**
2170
- * Internal flush implementation
3032
+ * Validate a single component's query with retry logic
3033
+ * @param component - The component to validate
3034
+ * @param collections - Collections object containing database execute function
3035
+ * @param apiKey - Optional API key for LLM calls
3036
+ * @returns Validation result with component, query key, and result
2171
3037
  */
2172
- private flushNow;
3038
+ validateSingleQuery(component: Component, collections: any, apiKey?: string): Promise<QueryValidationResult>;
2173
3039
  /**
2174
- * Clean up resources
2175
- * Call this when done with the buffer
3040
+ * Validate multiple component queries in parallel
3041
+ * @param components - Array of components with potential queries
3042
+ * @param collections - Collections object containing database execute function
3043
+ * @param apiKey - Optional API key for LLM calls
3044
+ * @returns Object with validated components and query results map
2176
3045
  */
2177
- dispose(): void;
2178
- }
2179
-
2180
- /**
2181
- * ToolExecutorService - Handles execution of SQL queries and external tools
2182
- * Extracted from BaseLLM.generateTextResponse for better separation of concerns
2183
- */
2184
-
2185
- /**
2186
- * External tool definition
2187
- */
2188
- interface ExternalTool {
2189
- id: string;
2190
- name: string;
2191
- description?: string;
2192
- /** Tool type: "source" = routed through SourceAgent, "direct" = called directly by MainAgent */
2193
- toolType?: 'source' | 'direct';
2194
- /** Full untruncated schema for source agent (all columns visible) */
2195
- fullSchema?: string;
2196
- fn: (input: any) => Promise<any>;
2197
- limit?: number;
2198
- outputSchema?: any;
2199
- executionType?: 'immediate' | 'deferred';
2200
- userProvidedData?: any;
2201
- }
2202
- /**
2203
- * Executed tool tracking info
2204
- */
2205
- interface ExecutedToolInfo {
2206
- id: string;
2207
- name: string;
2208
- params: any;
2209
- result: {
2210
- _totalRecords: number;
2211
- _recordsShown: number;
2212
- _metadata?: any;
2213
- _sampleData: any[];
2214
- };
2215
- outputSchema?: any;
2216
- sourceSchema?: string;
2217
- sourceType?: string;
3046
+ validateComponentQueries(components: Component[], collections: any, apiKey?: string): Promise<BatchValidationResult>;
2218
3047
  }
2219
3048
 
2220
3049
  /**
@@ -2432,18 +3261,30 @@ declare class OpenAILLM extends BaseLLM {
2432
3261
  declare const openaiLLM: OpenAILLM;
2433
3262
 
2434
3263
  /**
2435
- * Query Cache - Stores query results with configurable TTL
2436
- * Used to avoid re-executing queries that were already validated
3264
+ * Query Cache Two mechanisms:
3265
+ *
3266
+ * 1. `cache` (query string → result data) — TTL-based with max size, for avoiding re-execution
3267
+ * of recently validated queries. True LRU eviction: reads bubble entries to the back via
3268
+ * delete+re-set so the oldest *unused* entry is evicted, not the oldest *inserted*.
3269
+ *
3270
+ * 2. Encrypted queryId tokens — SQL is encrypted into the queryId itself (self-contained).
3271
+ * No server-side storage needed for SQL mappings. The token is decrypted on each request.
3272
+ * This eliminates the unbounded queryIdCache that previously grew forever and caused
3273
+ * memory bloat (hundreds of MBs after thousands of queries).
3274
+ *
3275
+ * Result data can still be cached temporarily via the data cache (mechanism 1).
2437
3276
  */
2438
3277
  declare class QueryCache {
2439
3278
  private cache;
2440
- private queryIdCache;
2441
3279
  private ttlMs;
3280
+ private maxCacheSize;
2442
3281
  private cleanupInterval;
3282
+ private readonly algorithm;
3283
+ private encryptionKey;
2443
3284
  constructor();
2444
3285
  /**
2445
3286
  * Set the cache TTL (Time To Live)
2446
- * @param minutes - TTL in minutes (default: 5)
3287
+ * @param minutes - TTL in minutes (default: 10)
2447
3288
  */
2448
3289
  setTTL(minutes: number): void;
2449
3290
  /**
@@ -2451,12 +3292,16 @@ declare class QueryCache {
2451
3292
  */
2452
3293
  getTTL(): number;
2453
3294
  /**
2454
- * Store query result in cache
2455
- * Key is the exact query string (or JSON for parameterized queries)
3295
+ * Store query result in data cache.
3296
+ * If the key already exists, it's removed first so the re-insert places it
3297
+ * at the back of the iteration order (LRU). Eviction only fires when adding
3298
+ * a genuinely new key past the size limit.
2456
3299
  */
2457
3300
  set(query: string, data: any): void;
2458
3301
  /**
2459
- * Get cached result if exists and not expired
3302
+ * Get cached result if exists and not expired.
3303
+ * On hit, re-inserts the entry so it moves to the back of the Map's
3304
+ * iteration order — turning FIFO eviction into true LRU.
2460
3305
  */
2461
3306
  get(query: string): any | null;
2462
3307
  /**
@@ -2476,30 +3321,37 @@ declare class QueryCache {
2476
3321
  */
2477
3322
  getStats(): {
2478
3323
  size: number;
3324
+ queryIdCount: number;
2479
3325
  oldestEntryAge: number | null;
2480
3326
  };
2481
3327
  /**
2482
- * Start periodic cleanup of expired entries
3328
+ * Start periodic cleanup of expired data cache entries.
2483
3329
  */
2484
3330
  private startCleanup;
2485
3331
  /**
2486
- * Generate a unique query ID
3332
+ * Encrypt a payload into a self-contained token.
2487
3333
  */
2488
- private generateQueryId;
3334
+ private encrypt;
2489
3335
  /**
2490
- * Store a query by ID. Returns the generated queryId.
2491
- * The query is stored server-side; only the queryId is sent to the frontend.
3336
+ * Decrypt a token back to the original payload.
3337
+ */
3338
+ private decrypt;
3339
+ /**
3340
+ * Store a query by generating an encrypted token as queryId.
3341
+ * The SQL is encrypted INTO the token — nothing stored in memory.
3342
+ * If data is provided, it's cached temporarily in the data cache.
2492
3343
  */
2493
3344
  storeQuery(query: any, data?: any): string;
2494
3345
  /**
2495
- * Get a stored query by its ID (not expired)
3346
+ * Get a stored query by decrypting its token.
3347
+ * Returns the SQL + any cached result data.
2496
3348
  */
2497
3349
  getQuery(queryId: string): {
2498
3350
  query: any;
2499
3351
  data: any;
2500
3352
  } | null;
2501
3353
  /**
2502
- * Update cached data for a queryId
3354
+ * Update cached data for a queryId token
2503
3355
  */
2504
3356
  setQueryData(queryId: string, data: any): void;
2505
3357
  /**
@@ -2556,175 +3408,63 @@ declare class DashboardConversationHistory {
2556
3408
  declare const dashboardConversationHistory: DashboardConversationHistory;
2557
3409
 
2558
3410
  /**
2559
- * Multi-Agent Architecture Types
3411
+ * ScriptMatcher — LLM-Based Script Matching + Parameter Extraction
2560
3412
  *
2561
- * Defines interfaces for the hierarchical agent system:
2562
- * - Main Agent: ONE LLM.streamWithTools() call with source agent tools
2563
- * - Source Agents: independent agents that query individual data sources
3413
+ * Uses ONE LLM call to:
3414
+ * 1. Pick the best matching script from the library (or "none")
3415
+ * 2. Extract parameter values from the user question
2564
3416
  *
2565
- * The main agent sees only source summaries. When it calls a source tool,
2566
- * the SourceAgent runs independently (own LLM, own retries) and returns clean data.
3417
+ * Why LLM over embeddings:
3418
+ * - Embeddings capture topic similarity ("overstock" "inventory" ≈ "revenue")
3419
+ * but can't distinguish structurally different questions about the same domain
3420
+ * - LLM understands that "overstock by warehouse" needs a different script than
3421
+ * "revenue by warehouse" even though they're semantically close
3422
+ * - One call does both matching AND parameter extraction
3423
+ *
3424
+ * When script library grows past ~50, add an embedding pre-filter
3425
+ * (ChromaDB narrows to top 10 → LLM picks from those 10).
2567
3426
  */
2568
3427
 
2569
- /**
2570
- * Per-entity detail: name, row count, and column names.
2571
- * Gives the main agent enough context to route to the right source.
2572
- */
2573
- interface EntityDetail {
2574
- /** Entity name (table, sheet, endpoint) */
2575
- name: string;
2576
- /** Approximate row count */
2577
- rowCount?: number;
2578
- /** Column/field names */
2579
- columns: string[];
2580
- }
2581
- /**
2582
- * Representation of a data source for the main agent.
2583
- * Contains entity names WITH column names so the LLM can route accurately.
2584
- */
2585
- interface SourceSummary {
2586
- /** Source ID (matches tool ID prefix) */
2587
- id: string;
2588
- /** Human-readable source name */
2589
- name: string;
2590
- /** Source type: postgres, excel, rest_api, etc. */
2591
- type: string;
2592
- /** Brief description of what data this source contains */
2593
- description: string;
2594
- /** Detailed entity info with column names for routing */
2595
- entityDetails: EntityDetail[];
2596
- /** The tool ID associated with this source */
2597
- toolId: string;
2598
- }
2599
- /**
2600
- * What a source agent returns after querying its data source.
2601
- * The main agent uses this to analyze and compose the final response.
2602
- */
2603
- interface SourceAgentResult {
2604
- /** Source ID */
2605
- sourceId: string;
2606
- /** Source name */
2607
- sourceName: string;
2608
- /** Whether the query succeeded */
2609
- success: boolean;
2610
- /** Result data rows */
2611
- data: any[];
2612
- /** Metadata about the query execution */
2613
- metadata: SourceAgentMetadata;
2614
- /** Tool execution info (reused for component generation) */
2615
- executedTool: ExecutedToolInfo;
2616
- /** Error message if failed */
2617
- error?: string;
2618
- }
2619
- interface SourceAgentMetadata {
2620
- /** Total rows that matched the query (before limit) */
2621
- totalRowsMatched: number;
2622
- /** Rows actually returned (after limit) */
2623
- rowsReturned: number;
2624
- /** Whether the result was truncated by the row limit */
2625
- isLimited: boolean;
2626
- /** The query/params that were executed */
2627
- queryExecuted?: string;
2628
- /** Execution time in milliseconds */
2629
- executionTimeMs: number;
2630
- }
2631
- /**
2632
- * The complete response from the multi-agent system.
2633
- * Contains everything needed for text display + component generation.
2634
- */
2635
- interface AgentResponse {
2636
- /** Generated text response (analysis of the data) */
2637
- text: string;
2638
- /** All executed tools across all source agents (for component generation) */
2639
- executedTools: ExecutedToolInfo[];
2640
- /** Individual results from each source agent */
2641
- sourceResults: SourceAgentResult[];
2642
- }
2643
- /**
2644
- * Configuration for the multi-agent system.
2645
- * Controls limits, models, and behavior.
2646
- */
2647
- interface AgentConfig {
2648
- /** Max rows a source agent can return (default: 50) */
2649
- maxRowsPerSource: number;
2650
- /** Model for the main agent (routing + analysis in one LLM call) */
2651
- mainAgentModel: string;
2652
- /** Model for source agent query generation */
2653
- sourceAgentModel: string;
2654
- /** API key for LLM calls */
2655
- apiKey?: string;
2656
- /** Max retry attempts per source agent */
2657
- maxRetries: number;
2658
- /** Max tool calling iterations for the main agent loop */
2659
- maxIterations: number;
2660
- /** Global knowledge base context (static, same for all users/questions — cached in system prompt) */
2661
- globalKnowledgeBase?: string;
2662
- /** Per-request knowledge base context (user-specific + query-matched — dynamic, not cached) */
2663
- knowledgeBaseContext?: string;
3428
+ declare class ScriptMatcher {
3429
+ private store;
3430
+ constructor(store: ScriptStore);
3431
+ /**
3432
+ * Find the best matching script for a user question.
3433
+ * Uses ONE LLM call that picks the script AND extracts parameters.
3434
+ * Returns null if no script matches.
3435
+ */
3436
+ match(userPrompt: string, apiKey?: string, model?: string): Promise<ScriptMatch | null>;
3437
+ /**
3438
+ * Build the script catalog string for the LLM prompt.
3439
+ * Each script gets: index, ID, name, description, and parameter definitions.
3440
+ */
3441
+ private buildScriptCatalog;
2664
3442
  }
2665
- /**
2666
- * Default agent configuration
2667
- */
2668
- declare const DEFAULT_AGENT_CONFIG: AgentConfig;
2669
3443
 
2670
3444
  /**
2671
- * Main Agent (Orchestrator)
3445
+ * ScriptRunner Execute scripts in an isolated tsx subprocess.
2672
3446
  *
2673
- * A single LLM.streamWithTools() call that handles everything:
2674
- * - Routing: decides which source(s) to query based on summaries
2675
- * - Querying: calls source tools (each wraps an independent SourceAgent)
2676
- * - Direct tools: calls pre-built function tools directly with LLM-provided params
2677
- * - Re-querying: if data is wrong/incomplete, calls tools again with modified intent
2678
- * - Analysis: generates final text response from the data
3447
+ * The subprocess approach replaces the earlier `new Function()` eval and gives us:
3448
+ * - Real sandbox (separate process, SIGKILL on timeout).
3449
+ * - Real TypeScript (tsx transpiles on the fly).
3450
+ * - npm imports available to scripts (clustering, stats, geo, etc.).
2679
3451
  *
2680
- * Two tool types:
2681
- * - "source" tools: main agent sees summaries, SourceAgent handles SQL generation independently
2682
- * - "direct" tools: main agent calls fn() directly with structured params (no SourceAgent)
3452
+ * Protocol: NDJSON over the child's stdin/stdout. See script-ipc.ts + backend/docs/SCRIPT-FLOW-IMPLEMENTATION.md.
2683
3453
  */
2684
3454
 
2685
- declare class MainAgent {
2686
- private externalTools;
2687
- private config;
2688
- private streamBuffer;
2689
- constructor(externalTools: ExternalTool[], config: AgentConfig, streamBuffer?: StreamBuffer);
2690
- /**
2691
- * Handle a user question using the multi-agent system.
2692
- *
2693
- * This is ONE LLM.streamWithTools() call. The LLM:
2694
- * 1. Sees source summaries + direct tool descriptions in system prompt
2695
- * 2. Decides which tool(s) to call (routing)
2696
- * 3. Source tools → SourceAgent runs independently → returns data
2697
- * 4. Direct tools → fn() called directly with LLM params → returns data
2698
- * 5. Generates final analysis text
2699
- */
2700
- handleQuestion(userPrompt: string, apiKey?: string, conversationHistory?: string, streamCallback?: (chunk: string) => void): Promise<AgentResponse>;
2701
- /**
2702
- * Execute a direct tool — call fn() with LLM-provided params, no SourceAgent.
2703
- */
2704
- private handleDirectTool;
2705
- /**
2706
- * Build the main agent's system prompt with source summaries and direct tool descriptions.
2707
- */
2708
- private buildSystemPrompt;
2709
- /**
2710
- * Build tool definitions for source tools — summary-only descriptions.
2711
- * The full schema is inside the SourceAgent which runs independently.
2712
- */
2713
- private buildSourceToolDefinitions;
2714
- /**
2715
- * Build tool definitions for direct tools — expose their actual params.
2716
- * These are called directly by the main agent LLM, no SourceAgent.
2717
- */
2718
- private buildDirectToolDefinitions;
2719
- /**
2720
- * Format a source agent's result as a clean string for the main agent LLM.
2721
- */
2722
- private formatResultForMainAgent;
2723
- /**
2724
- * Get source summaries (for external inspection/debugging).
2725
- */
2726
- getSourceSummaries(): SourceSummary[];
3455
+ interface RunScriptOptions {
3456
+ /** Data sources the script is allowed to query via ctx.query */
3457
+ externalTools: ExternalTool[];
3458
+ /** Optional — for propagating per-query UI progress to the user */
3459
+ streamBuffer?: StreamBuffer;
3460
+ /** Override the wall-clock timeout (default `SCRIPT_TIMEOUT_MS`, 60s). */
3461
+ timeoutMs?: number;
2727
3462
  }
3463
+ /**
3464
+ * Execute a recipe by spawning a tsx child on the script's .ts file.
3465
+ * `scriptPath` is the absolute path to the saved `.ts` body.
3466
+ */
3467
+ declare function runScript(recipe: ScriptRecipe, scriptPath: string, params: Record<string, any>, options: RunScriptOptions): Promise<ScriptResult>;
2728
3468
 
2729
3469
  type MessageTypeHandler = (message: IncomingMessage) => void | Promise<void>;
2730
3470
  declare class SuperatomSDK {
@@ -2742,6 +3482,7 @@ declare class SuperatomSDK {
2742
3482
  private collections;
2743
3483
  private components;
2744
3484
  private tools;
3485
+ private workflows;
2745
3486
  private anthropicApiKey;
2746
3487
  private groqApiKey;
2747
3488
  private geminiApiKey;
@@ -2749,6 +3490,9 @@ declare class SuperatomSDK {
2749
3490
  private llmProviders;
2750
3491
  private databaseType;
2751
3492
  private modelStrategy;
3493
+ private mainAgentModel;
3494
+ private sourceAgentModel;
3495
+ private dashCompModels?;
2752
3496
  private conversationSimilarityThreshold;
2753
3497
  private userManager;
2754
3498
  private dashboardManager;
@@ -2848,6 +3592,19 @@ declare class SuperatomSDK {
2848
3592
  * Get the stored tools
2849
3593
  */
2850
3594
  getTools(): Tool$1[];
3595
+ /**
3596
+ * Register workflow components for the SDK instance.
3597
+ *
3598
+ * Workflows are pre-built multi-step UI flows the main agent can pick when
3599
+ * the user's prompt matches a workflow's `whenToUse` trigger. Picking a
3600
+ * workflow short-circuits analysis text + dashboard component generation —
3601
+ * the workflow component is returned directly, with the LLM-extracted props.
3602
+ */
3603
+ setWorkflows(workflows: WorkflowDescriptor[]): void;
3604
+ /**
3605
+ * Get the registered workflow components.
3606
+ */
3607
+ getWorkflows(): WorkflowDescriptor[];
2851
3608
  /**
2852
3609
  * Apply model strategy to all LLM provider singletons
2853
3610
  * @param strategy - 'best', 'fast', or 'balanced'
@@ -2878,4 +3635,4 @@ declare class SuperatomSDK {
2878
3635
  getConversationSimilarityThreshold(): number;
2879
3636
  }
2880
3637
 
2881
- 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 };
3638
+ export { type Action, type AgentConfig, type AgentResponse, BM25L, type BM25LOptions, type BaseLLMConfig, CONTEXT_CONFIG, type CapturedLog, CleanupService, type CollectionHandler, type CollectionOperation, type DBUIBlock, DEFAULT_AGENT_CONFIG, type DatabaseType, type HybridSearchOptions, type IncomingMessage, type KbNodesQueryFilters, type KbNodesRequestPayload, LLM, type LLMUsageEntry, type LogLevel, MainAgent, type Message, type ModelStrategy, type OutputField, type RerankedResult, STORAGE_CONFIG, type 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, rerankChromaResults, rerankConversationResults, resolveScriptRecipeStore, runScript, userPromptErrorLogger };