@superatomai/sdk-node 0.0.12-mds → 0.0.12-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
@@ -878,6 +904,12 @@ interface SuperatomSDKConfig {
878
904
  * Default: 5 minutes
879
905
  */
880
906
  queryCacheTTL?: number;
907
+ /**
908
+ * Dashboard conversation history TTL (Time To Live) in minutes
909
+ * Per-dashboard conversation histories expire after this duration
910
+ * Default: 30 minutes
911
+ */
912
+ dashboardHistoryTTL?: number;
881
913
  }
882
914
 
883
915
  declare const KbNodesQueryFiltersSchema: z.ZodObject<{
@@ -1288,6 +1320,888 @@ declare class ReportManager {
1288
1320
  getReportCount(): number;
1289
1321
  }
1290
1322
 
1323
+ /**
1324
+ * StreamBuffer - Buffered streaming utility for smoother text delivery
1325
+ * Batches small chunks together and flushes at regular intervals
1326
+ */
1327
+ type StreamCallback = (chunk: string) => void;
1328
+ /**
1329
+ * StreamBuffer class for managing buffered streaming output
1330
+ * Provides smooth text delivery by batching small chunks
1331
+ */
1332
+ declare class StreamBuffer {
1333
+ private buffer;
1334
+ private flushTimer;
1335
+ private callback;
1336
+ private fullText;
1337
+ constructor(callback?: StreamCallback);
1338
+ /**
1339
+ * Check if the buffer has a callback configured
1340
+ */
1341
+ hasCallback(): boolean;
1342
+ /**
1343
+ * Get all text that has been written (including already flushed)
1344
+ */
1345
+ getFullText(): string;
1346
+ /**
1347
+ * Write a chunk to the buffer
1348
+ * Large chunks or chunks with newlines are flushed immediately
1349
+ * Small chunks are batched and flushed after a short interval
1350
+ *
1351
+ * @param chunk - Text chunk to write
1352
+ */
1353
+ write(chunk: string): void;
1354
+ /**
1355
+ * Flush the buffer immediately
1356
+ * Call this before tool execution or other operations that need clean output
1357
+ */
1358
+ flush(): void;
1359
+ /**
1360
+ * Internal flush implementation
1361
+ */
1362
+ private flushNow;
1363
+ /**
1364
+ * Clean up resources
1365
+ * Call this when done with the buffer
1366
+ */
1367
+ dispose(): void;
1368
+ }
1369
+
1370
+ /**
1371
+ * ToolExecutorService - Handles execution of SQL queries and external tools
1372
+ * Extracted from BaseLLM.generateTextResponse for better separation of concerns
1373
+ */
1374
+
1375
+ /**
1376
+ * External tool definition
1377
+ */
1378
+ interface ExternalTool {
1379
+ id: string;
1380
+ name: string;
1381
+ description?: string;
1382
+ /** Tool type: "source" = routed through SourceAgent, "direct" = called directly by MainAgent */
1383
+ toolType?: 'source' | 'direct';
1384
+ /** Full untruncated schema for source agent (all columns visible) */
1385
+ fullSchema?: string;
1386
+ /** Schema size tier: small (≤50 tables), medium (51-200), large (201-500), very_large (500+) */
1387
+ schemaTier?: string;
1388
+ /** Schema search function for very_large tier — keyword search over entities */
1389
+ schemaSearchFn?: (keywords: string[]) => string;
1390
+ fn: (input: any) => Promise<any>;
1391
+ limit?: number;
1392
+ outputSchema?: any;
1393
+ executionType?: 'immediate' | 'deferred';
1394
+ userProvidedData?: any;
1395
+ params?: Record<string, any>;
1396
+ }
1397
+ /**
1398
+ * Executed tool tracking info
1399
+ */
1400
+ interface ExecutedToolInfo {
1401
+ id: string;
1402
+ name: string;
1403
+ params: any;
1404
+ result: {
1405
+ _totalRecords: number;
1406
+ _recordsShown: number;
1407
+ _metadata?: any;
1408
+ _sampleData: any[];
1409
+ };
1410
+ outputSchema?: any;
1411
+ sourceSchema?: string;
1412
+ sourceType?: string;
1413
+ }
1414
+
1415
+ /**
1416
+ * Multi-Agent Architecture Types
1417
+ *
1418
+ * Defines interfaces for the hierarchical agent system:
1419
+ * - Main Agent: ONE LLM.streamWithTools() call with source agent tools
1420
+ * - Source Agents: independent agents that query individual data sources
1421
+ *
1422
+ * The main agent sees only source summaries. When it calls a source tool,
1423
+ * the SourceAgent runs independently (own LLM, own retries) and returns clean data.
1424
+ */
1425
+
1426
+ /**
1427
+ * Per-entity detail: name, row count, and column names.
1428
+ * Gives the main agent enough context to route to the right source.
1429
+ */
1430
+ interface EntityDetail {
1431
+ /** Entity name (table, sheet, endpoint) */
1432
+ name: string;
1433
+ /** Approximate row count */
1434
+ rowCount?: number;
1435
+ /** Column/field names */
1436
+ columns: string[];
1437
+ }
1438
+ /**
1439
+ * Representation of a data source for the main agent.
1440
+ * Contains entity names WITH column names so the LLM can route accurately.
1441
+ */
1442
+ interface SourceSummary {
1443
+ /** Source ID (matches tool ID prefix) */
1444
+ id: string;
1445
+ /** Human-readable source name */
1446
+ name: string;
1447
+ /** Source type: postgres, excel, rest_api, etc. */
1448
+ type: string;
1449
+ /** Brief description of what data this source contains */
1450
+ description: string;
1451
+ /** Detailed entity info with column names for routing */
1452
+ entityDetails: EntityDetail[];
1453
+ /** The tool ID associated with this source */
1454
+ toolId: string;
1455
+ }
1456
+ /**
1457
+ * What a source agent returns after querying its data source.
1458
+ * The main agent uses this to analyze and compose the final response.
1459
+ */
1460
+ interface SourceAgentResult {
1461
+ /** Source ID */
1462
+ sourceId: string;
1463
+ /** Source name */
1464
+ sourceName: string;
1465
+ /** Whether the query succeeded */
1466
+ success: boolean;
1467
+ /** Result data rows */
1468
+ data: any[];
1469
+ /** Metadata about the query execution */
1470
+ metadata: SourceAgentMetadata;
1471
+ /** Tool execution info for the last successful query (backward compat) */
1472
+ executedTool: ExecutedToolInfo;
1473
+ /** All successful tool executions (primary + follow-up queries) */
1474
+ allExecutedTools?: ExecutedToolInfo[];
1475
+ /** Error message if failed */
1476
+ error?: string;
1477
+ }
1478
+ interface SourceAgentMetadata {
1479
+ /** Total rows that matched the query (before limit) */
1480
+ totalRowsMatched: number;
1481
+ /** Rows actually returned (after limit) */
1482
+ rowsReturned: number;
1483
+ /** Whether the result was truncated by the row limit */
1484
+ isLimited: boolean;
1485
+ /** The query/params that were executed */
1486
+ queryExecuted?: string;
1487
+ /** Execution time in milliseconds */
1488
+ executionTimeMs: number;
1489
+ }
1490
+ /**
1491
+ * A pre-built, multi-step UI flow registered with the SDK.
1492
+ *
1493
+ * When the main agent decides a user's question matches a workflow's whenToUse
1494
+ * trigger, it picks the workflow instead of running source agents / generating
1495
+ * dashboard components. The LLM extracts the workflow's required props from the
1496
+ * prompt (using `propsSchema` as the tool input_schema) and the SDK returns the
1497
+ * workflow component directly — no analysis text, no chart generation. The
1498
+ * frontend renders the registered workflow component with the LLM-extracted
1499
+ * props.
1500
+ */
1501
+ interface WorkflowDescriptor {
1502
+ /** Unique workflow id (used as the LLM tool name) */
1503
+ id: string;
1504
+ /** Component name on the frontend (matches the registered React component) */
1505
+ name: string;
1506
+ /** Short human-readable description of what this workflow does */
1507
+ description: string;
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
+
1291
2205
  type SystemPrompt = string | Anthropic.Messages.TextBlockParam[];
1292
2206
  interface LLMMessages {
1293
2207
  sys: SystemPrompt;
@@ -1322,6 +2236,16 @@ declare class LLM {
1322
2236
  * @returns Normalized system prompt for Anthropic API
1323
2237
  */
1324
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;
1325
2249
  /**
1326
2250
  * Log cache usage metrics from Anthropic API response
1327
2251
  * Shows cache hits, costs, and savings
@@ -1442,16 +2366,6 @@ declare class UILogCollector {
1442
2366
  setUIBlockId(uiBlockId: string): void;
1443
2367
  }
1444
2368
 
1445
- /**
1446
- * Represents an action that can be performed on a UIBlock
1447
- */
1448
- interface Action {
1449
- id: string;
1450
- name: string;
1451
- type: string;
1452
- [key: string]: any;
1453
- }
1454
-
1455
2369
  /**
1456
2370
  * UIBlock represents a single user and assistant message block in a thread
1457
2371
  * Contains user question, component metadata, component data, text response, and available actions
@@ -1634,12 +2548,20 @@ declare class Thread {
1634
2548
 
1635
2549
  /**
1636
2550
  * ThreadManager manages all threads globally
1637
- * 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.
1638
2553
  */
1639
2554
  declare class ThreadManager {
1640
2555
  private static instance;
1641
2556
  private threads;
2557
+ private cleanupInterval;
2558
+ private readonly threadTtlMs;
1642
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;
1643
2565
  /**
1644
2566
  * Get singleton instance of ThreadManager
1645
2567
  */
@@ -2339,18 +3261,30 @@ declare class OpenAILLM extends BaseLLM {
2339
3261
  declare const openaiLLM: OpenAILLM;
2340
3262
 
2341
3263
  /**
2342
- * Query Cache - Stores query results with configurable TTL
2343
- * 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).
2344
3276
  */
2345
3277
  declare class QueryCache {
2346
3278
  private cache;
2347
- private queryIdCache;
2348
3279
  private ttlMs;
3280
+ private maxCacheSize;
2349
3281
  private cleanupInterval;
3282
+ private readonly algorithm;
3283
+ private encryptionKey;
2350
3284
  constructor();
2351
3285
  /**
2352
3286
  * Set the cache TTL (Time To Live)
2353
- * @param minutes - TTL in minutes (default: 5)
3287
+ * @param minutes - TTL in minutes (default: 10)
2354
3288
  */
2355
3289
  setTTL(minutes: number): void;
2356
3290
  /**
@@ -2358,12 +3292,16 @@ declare class QueryCache {
2358
3292
  */
2359
3293
  getTTL(): number;
2360
3294
  /**
2361
- * Store query result in cache
2362
- * 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.
2363
3299
  */
2364
3300
  set(query: string, data: any): void;
2365
3301
  /**
2366
- * 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.
2367
3305
  */
2368
3306
  get(query: string): any | null;
2369
3307
  /**
@@ -2383,30 +3321,37 @@ declare class QueryCache {
2383
3321
  */
2384
3322
  getStats(): {
2385
3323
  size: number;
3324
+ queryIdCount: number;
2386
3325
  oldestEntryAge: number | null;
2387
3326
  };
2388
3327
  /**
2389
- * Start periodic cleanup of expired entries
3328
+ * Start periodic cleanup of expired data cache entries.
2390
3329
  */
2391
3330
  private startCleanup;
2392
3331
  /**
2393
- * Generate a unique query ID
3332
+ * Encrypt a payload into a self-contained token.
3333
+ */
3334
+ private encrypt;
3335
+ /**
3336
+ * Decrypt a token back to the original payload.
2394
3337
  */
2395
- private generateQueryId;
3338
+ private decrypt;
2396
3339
  /**
2397
- * Store a query by ID. Returns the generated queryId.
2398
- * The query is stored server-side; only the queryId is sent to the frontend.
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.
2399
3343
  */
2400
3344
  storeQuery(query: any, data?: any): string;
2401
3345
  /**
2402
- * 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.
2403
3348
  */
2404
3349
  getQuery(queryId: string): {
2405
3350
  query: any;
2406
3351
  data: any;
2407
3352
  } | null;
2408
3353
  /**
2409
- * Update cached data for a queryId
3354
+ * Update cached data for a queryId token
2410
3355
  */
2411
3356
  setQueryData(queryId: string, data: any): void;
2412
3357
  /**
@@ -2416,6 +3361,111 @@ declare class QueryCache {
2416
3361
  }
2417
3362
  declare const queryCache: QueryCache;
2418
3363
 
3364
+ /**
3365
+ * Manages conversation history scoped per user + dashboard.
3366
+ * Each user-dashboard pair has its own isolated history that expires after a configurable TTL.
3367
+ */
3368
+ declare class DashboardConversationHistory {
3369
+ private histories;
3370
+ private ttlMs;
3371
+ private maxEntries;
3372
+ private cleanupInterval;
3373
+ constructor();
3374
+ /**
3375
+ * Set the TTL for dashboard histories
3376
+ * @param minutes - TTL in minutes
3377
+ */
3378
+ setTTL(minutes: number): void;
3379
+ /**
3380
+ * Set max entries per dashboard
3381
+ */
3382
+ setMaxEntries(max: number): void;
3383
+ /**
3384
+ * Add a conversation entry for a user's dashboard
3385
+ */
3386
+ addEntry(dashboardId: string, userPrompt: string, componentSummary: string, userId?: string): void;
3387
+ /**
3388
+ * Get formatted conversation history for a user's dashboard
3389
+ */
3390
+ getHistory(dashboardId: string, userId?: string): string;
3391
+ /**
3392
+ * Clear history for a specific user's dashboard
3393
+ */
3394
+ clearDashboard(dashboardId: string, userId?: string): void;
3395
+ /**
3396
+ * Clear all dashboard histories
3397
+ */
3398
+ clearAll(): void;
3399
+ /**
3400
+ * Start periodic cleanup of expired histories
3401
+ */
3402
+ private startCleanup;
3403
+ /**
3404
+ * Stop cleanup interval (for graceful shutdown)
3405
+ */
3406
+ destroy(): void;
3407
+ }
3408
+ declare const dashboardConversationHistory: DashboardConversationHistory;
3409
+
3410
+ /**
3411
+ * ScriptMatcher — LLM-Based Script Matching + Parameter Extraction
3412
+ *
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
3416
+ *
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).
3426
+ */
3427
+
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;
3442
+ }
3443
+
3444
+ /**
3445
+ * ScriptRunner — Execute scripts in an isolated tsx subprocess.
3446
+ *
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.).
3451
+ *
3452
+ * Protocol: NDJSON over the child's stdin/stdout. See script-ipc.ts + backend/docs/SCRIPT-FLOW-IMPLEMENTATION.md.
3453
+ */
3454
+
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;
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>;
3468
+
2419
3469
  type MessageTypeHandler = (message: IncomingMessage) => void | Promise<void>;
2420
3470
  declare class SuperatomSDK {
2421
3471
  private ws;
@@ -2432,6 +3482,7 @@ declare class SuperatomSDK {
2432
3482
  private collections;
2433
3483
  private components;
2434
3484
  private tools;
3485
+ private workflows;
2435
3486
  private anthropicApiKey;
2436
3487
  private groqApiKey;
2437
3488
  private geminiApiKey;
@@ -2439,6 +3490,9 @@ declare class SuperatomSDK {
2439
3490
  private llmProviders;
2440
3491
  private databaseType;
2441
3492
  private modelStrategy;
3493
+ private mainAgentModel;
3494
+ private sourceAgentModel;
3495
+ private dashCompModels?;
2442
3496
  private conversationSimilarityThreshold;
2443
3497
  private userManager;
2444
3498
  private dashboardManager;
@@ -2538,6 +3592,19 @@ declare class SuperatomSDK {
2538
3592
  * Get the stored tools
2539
3593
  */
2540
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[];
2541
3608
  /**
2542
3609
  * Apply model strategy to all LLM provider singletons
2543
3610
  * @param strategy - 'best', 'fast', or 'balanced'
@@ -2568,4 +3635,4 @@ declare class SuperatomSDK {
2568
3635
  getConversationSimilarityThreshold(): number;
2569
3636
  }
2570
3637
 
2571
- export { type Action, BM25L, type BM25LOptions, type BaseLLMConfig, CONTEXT_CONFIG, type CapturedLog, CleanupService, type CollectionHandler, type CollectionOperation, type DBUIBlock, type DatabaseType, type HybridSearchOptions, type IncomingMessage, type KbNodesQueryFilters, type KbNodesRequestPayload, LLM, type LLMUsageEntry, type LogLevel, 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, 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 };