@superatomai/sdk-node 0.0.23-mds → 0.0.24-dsp
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +1412 -428
- package/dist/index.d.ts +1412 -428
- package/dist/index.js +9235 -2852
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +7864 -1480
- package/dist/index.mjs.map +1 -1
- package/dist/userResponse/scripts/script-bootstrap.d.mts +2 -0
- package/dist/userResponse/scripts/script-bootstrap.d.ts +2 -0
- package/dist/userResponse/scripts/script-bootstrap.js +295 -0
- package/dist/userResponse/scripts/script-bootstrap.js.map +1 -0
- package/dist/userResponse/scripts/script-bootstrap.mjs +293 -0
- package/dist/userResponse/scripts/script-bootstrap.mjs.map +1 -0
- package/package.json +3 -1
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';
|
|
@@ -1306,174 +1320,1178 @@ declare class ReportManager {
|
|
|
1306
1320
|
getReportCount(): number;
|
|
1307
1321
|
}
|
|
1308
1322
|
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
name: string;
|
|
1325
|
-
description: string;
|
|
1326
|
-
input_schema: {
|
|
1327
|
-
type: string;
|
|
1328
|
-
properties: Record<string, any>;
|
|
1329
|
-
required?: string[];
|
|
1330
|
-
};
|
|
1331
|
-
}
|
|
1332
|
-
declare class LLM {
|
|
1333
|
-
static text(messages: LLMMessages, options?: LLMOptions): Promise<string>;
|
|
1334
|
-
static stream<T = string>(messages: LLMMessages, options?: LLMOptions, json?: boolean): Promise<T extends string ? string : any>;
|
|
1335
|
-
static streamWithTools(messages: LLMMessages, tools: Tool[], toolHandler: (toolName: string, toolInput: any) => Promise<any>, options?: LLMOptions, maxIterations?: number): Promise<string>;
|
|
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);
|
|
1336
1338
|
/**
|
|
1337
|
-
*
|
|
1338
|
-
* Converts string to array format if needed
|
|
1339
|
-
* @param sys - System prompt (string or array of blocks)
|
|
1340
|
-
* @returns Normalized system prompt for Anthropic API
|
|
1339
|
+
* Check if the buffer has a callback configured
|
|
1341
1340
|
*/
|
|
1342
|
-
|
|
1341
|
+
hasCallback(): boolean;
|
|
1343
1342
|
/**
|
|
1344
|
-
*
|
|
1345
|
-
* Shows cache hits, costs, and savings
|
|
1343
|
+
* Get all text that has been written (including already flushed)
|
|
1346
1344
|
*/
|
|
1347
|
-
|
|
1345
|
+
getFullText(): string;
|
|
1348
1346
|
/**
|
|
1349
|
-
*
|
|
1350
|
-
*
|
|
1351
|
-
*
|
|
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
|
|
1352
1350
|
*
|
|
1353
|
-
* @
|
|
1354
|
-
* "anthropic/claude-sonnet-4-5" → ["anthropic", "claude-sonnet-4-5"]
|
|
1355
|
-
* "groq/openai/gpt-oss-120b" → ["groq", "openai/gpt-oss-120b"]
|
|
1356
|
-
* "claude-sonnet-4-5" → ["anthropic", "claude-sonnet-4-5"] (default)
|
|
1351
|
+
* @param chunk - Text chunk to write
|
|
1357
1352
|
*/
|
|
1358
|
-
|
|
1359
|
-
private static _anthropicText;
|
|
1360
|
-
private static _anthropicStream;
|
|
1361
|
-
private static _anthropicStreamWithTools;
|
|
1362
|
-
private static _groqText;
|
|
1363
|
-
private static _groqStream;
|
|
1364
|
-
private static _geminiText;
|
|
1365
|
-
private static _geminiStream;
|
|
1353
|
+
write(chunk: string): void;
|
|
1366
1354
|
/**
|
|
1367
|
-
*
|
|
1368
|
-
*
|
|
1355
|
+
* Flush the buffer immediately
|
|
1356
|
+
* Call this before tool execution or other operations that need clean output
|
|
1369
1357
|
*/
|
|
1370
|
-
|
|
1371
|
-
private static _geminiStreamWithTools;
|
|
1372
|
-
private static _openaiText;
|
|
1373
|
-
private static _openaiStream;
|
|
1374
|
-
private static _openaiStreamWithTools;
|
|
1358
|
+
flush(): void;
|
|
1375
1359
|
/**
|
|
1376
|
-
*
|
|
1377
|
-
* Enhanced version with jsonrepair to handle malformed JSON from LLMs
|
|
1378
|
-
* @param text - Text that may contain JSON wrapped in ```json...``` or with surrounding text
|
|
1379
|
-
* @returns Parsed JSON object or array
|
|
1360
|
+
* Internal flush implementation
|
|
1380
1361
|
*/
|
|
1381
|
-
private
|
|
1362
|
+
private flushNow;
|
|
1363
|
+
/**
|
|
1364
|
+
* Clean up resources
|
|
1365
|
+
* Call this when done with the buffer
|
|
1366
|
+
*/
|
|
1367
|
+
dispose(): void;
|
|
1382
1368
|
}
|
|
1383
1369
|
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
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>;
|
|
1390
1396
|
}
|
|
1391
1397
|
/**
|
|
1392
|
-
*
|
|
1393
|
-
* and sends them to runtime via ui_logs message with uiBlockId as the message id
|
|
1394
|
-
* Logs are sent in real-time for streaming effect in the UI
|
|
1395
|
-
* Respects the global log level configuration
|
|
1398
|
+
* Executed tool tracking info
|
|
1396
1399
|
*/
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
* Only logs that pass the log level filter are captured and sent
|
|
1415
|
-
*/
|
|
1416
|
-
private addLog;
|
|
1417
|
-
/**
|
|
1418
|
-
* Send a single log to runtime immediately
|
|
1419
|
-
*/
|
|
1420
|
-
private sendLogImmediately;
|
|
1421
|
-
/**
|
|
1422
|
-
* Log info message
|
|
1423
|
-
*/
|
|
1424
|
-
info(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
|
|
1425
|
-
/**
|
|
1426
|
-
* Log error message
|
|
1427
|
-
*/
|
|
1428
|
-
error(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
|
|
1429
|
-
/**
|
|
1430
|
-
* Log warning message
|
|
1431
|
-
*/
|
|
1432
|
-
warn(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
|
|
1433
|
-
/**
|
|
1434
|
-
* Log debug message
|
|
1435
|
-
*/
|
|
1436
|
-
debug(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
|
|
1437
|
-
/**
|
|
1438
|
-
* Log LLM explanation with typed metadata
|
|
1439
|
-
*/
|
|
1440
|
-
logExplanation(message: string, explanation: string, data?: Record<string, any>): void;
|
|
1441
|
-
/**
|
|
1442
|
-
* Log generated query with typed metadata
|
|
1443
|
-
*/
|
|
1444
|
-
logQuery(message: string, query: string, data?: Record<string, any>): void;
|
|
1445
|
-
/**
|
|
1446
|
-
* Send all collected logs at once (optional, for final summary)
|
|
1447
|
-
*/
|
|
1448
|
-
sendAllLogs(): void;
|
|
1449
|
-
/**
|
|
1450
|
-
* Get all collected logs
|
|
1451
|
-
*/
|
|
1452
|
-
getLogs(): CapturedLog[];
|
|
1453
|
-
/**
|
|
1454
|
-
* Clear all logs
|
|
1455
|
-
*/
|
|
1456
|
-
clearLogs(): void;
|
|
1457
|
-
/**
|
|
1458
|
-
* Set uiBlockId (in case it's provided later)
|
|
1459
|
-
*/
|
|
1460
|
-
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
|
+
/** Bounded summary over the FULL fetched result (complete structure). */
|
|
1410
|
+
_summary?: any;
|
|
1411
|
+
/** Up to MAIN_AGENT_COMPLETE_ROWS rows — the complete result when small. */
|
|
1412
|
+
_mainAgentRows?: any[];
|
|
1413
|
+
};
|
|
1414
|
+
outputSchema?: any;
|
|
1415
|
+
sourceSchema?: string;
|
|
1416
|
+
sourceType?: string;
|
|
1461
1417
|
}
|
|
1462
1418
|
|
|
1463
1419
|
/**
|
|
1464
|
-
*
|
|
1420
|
+
* Multi-Agent Architecture Types
|
|
1421
|
+
*
|
|
1422
|
+
* Defines interfaces for the hierarchical agent system:
|
|
1423
|
+
* - Main Agent: ONE LLM.streamWithTools() call with source agent tools
|
|
1424
|
+
* - Source Agents: independent agents that query individual data sources
|
|
1425
|
+
*
|
|
1426
|
+
* The main agent sees only source summaries. When it calls a source tool,
|
|
1427
|
+
* the SourceAgent runs independently (own LLM, own retries) and returns clean data.
|
|
1465
1428
|
*/
|
|
1466
|
-
|
|
1429
|
+
|
|
1430
|
+
/**
|
|
1431
|
+
* Per-entity detail: name, row count, and column names.
|
|
1432
|
+
* Gives the main agent enough context to route to the right source.
|
|
1433
|
+
*/
|
|
1434
|
+
interface EntityDetail {
|
|
1435
|
+
/** Entity name (table, sheet, endpoint) */
|
|
1436
|
+
name: string;
|
|
1437
|
+
/** Approximate row count */
|
|
1438
|
+
rowCount?: number;
|
|
1439
|
+
/** Column/field names */
|
|
1440
|
+
columns: string[];
|
|
1441
|
+
/** Entity-level semantic summary (what the table means) — for main-agent routing. */
|
|
1442
|
+
summary?: string;
|
|
1443
|
+
}
|
|
1444
|
+
/**
|
|
1445
|
+
* Representation of a data source for the main agent.
|
|
1446
|
+
* Contains entity names WITH column names so the LLM can route accurately.
|
|
1447
|
+
*/
|
|
1448
|
+
interface SourceSummary {
|
|
1449
|
+
/** Source ID (matches tool ID prefix) */
|
|
1467
1450
|
id: string;
|
|
1451
|
+
/** Human-readable source name */
|
|
1468
1452
|
name: string;
|
|
1453
|
+
/** Source type: postgres, excel, rest_api, etc. */
|
|
1469
1454
|
type: string;
|
|
1470
|
-
|
|
1455
|
+
/** Brief description of what data this source contains */
|
|
1456
|
+
description: string;
|
|
1457
|
+
/** Detailed entity info with column names for routing */
|
|
1458
|
+
entityDetails: EntityDetail[];
|
|
1459
|
+
/** The tool ID associated with this source */
|
|
1460
|
+
toolId: string;
|
|
1471
1461
|
}
|
|
1472
|
-
|
|
1473
1462
|
/**
|
|
1474
|
-
*
|
|
1475
|
-
*
|
|
1476
|
-
*/
|
|
1463
|
+
* What a source agent returns after querying its data source.
|
|
1464
|
+
* The main agent uses this to analyze and compose the final response.
|
|
1465
|
+
*/
|
|
1466
|
+
interface SourceAgentResult {
|
|
1467
|
+
/** Source ID */
|
|
1468
|
+
sourceId: string;
|
|
1469
|
+
/** Source name */
|
|
1470
|
+
sourceName: string;
|
|
1471
|
+
/** Whether the query succeeded */
|
|
1472
|
+
success: boolean;
|
|
1473
|
+
/** Result data rows */
|
|
1474
|
+
data: any[];
|
|
1475
|
+
/** Metadata about the query execution */
|
|
1476
|
+
metadata: SourceAgentMetadata;
|
|
1477
|
+
/** Tool execution info for the last successful query (backward compat) */
|
|
1478
|
+
executedTool: ExecutedToolInfo;
|
|
1479
|
+
/** All successful tool executions (primary + follow-up queries) */
|
|
1480
|
+
allExecutedTools?: ExecutedToolInfo[];
|
|
1481
|
+
/** Error message if failed */
|
|
1482
|
+
error?: string;
|
|
1483
|
+
}
|
|
1484
|
+
interface SourceAgentMetadata {
|
|
1485
|
+
/** Total rows that matched the query (before limit) */
|
|
1486
|
+
totalRowsMatched: number;
|
|
1487
|
+
/** Rows actually returned (after limit) */
|
|
1488
|
+
rowsReturned: number;
|
|
1489
|
+
/** Whether the result was truncated by the row limit */
|
|
1490
|
+
isLimited: boolean;
|
|
1491
|
+
/** The query/params that were executed */
|
|
1492
|
+
queryExecuted?: string;
|
|
1493
|
+
/** Execution time in milliseconds */
|
|
1494
|
+
executionTimeMs: number;
|
|
1495
|
+
}
|
|
1496
|
+
/**
|
|
1497
|
+
* A pre-built, multi-step UI flow registered with the SDK.
|
|
1498
|
+
*
|
|
1499
|
+
* When the main agent decides a user's question matches a workflow's whenToUse
|
|
1500
|
+
* trigger, it picks the workflow instead of running source agents / generating
|
|
1501
|
+
* dashboard components. The LLM extracts the workflow's required props from the
|
|
1502
|
+
* prompt (using `propsSchema` as the tool input_schema) and the SDK returns the
|
|
1503
|
+
* workflow component directly — no analysis text, no chart generation. The
|
|
1504
|
+
* frontend renders the registered workflow component with the LLM-extracted
|
|
1505
|
+
* props.
|
|
1506
|
+
*/
|
|
1507
|
+
interface WorkflowDescriptor {
|
|
1508
|
+
/** Unique workflow id (used as the LLM tool name) */
|
|
1509
|
+
id: string;
|
|
1510
|
+
/** Component name on the frontend (matches the registered React component) */
|
|
1511
|
+
name: string;
|
|
1512
|
+
/** Short human-readable description of what this workflow does */
|
|
1513
|
+
description: string;
|
|
1514
|
+
/**
|
|
1515
|
+
* 1–2 sentence trigger condition. The LLM uses this to decide if the
|
|
1516
|
+
* user's prompt matches this workflow. Be specific — e.g.
|
|
1517
|
+
* "User wants to *initiate* an inventory transfer (review + submit POs),
|
|
1518
|
+
* not just see analysis or charts."
|
|
1519
|
+
*/
|
|
1520
|
+
whenToUse: string;
|
|
1521
|
+
/**
|
|
1522
|
+
* JSON-schema-style description of the props the workflow needs. Becomes
|
|
1523
|
+
* the LLM tool's input_schema, so the model fills these from the prompt.
|
|
1524
|
+
* Use the same shape as `params` on direct tools — string descriptors with
|
|
1525
|
+
* an optional "(optional)" suffix.
|
|
1526
|
+
*
|
|
1527
|
+
* Example:
|
|
1528
|
+
* ```
|
|
1529
|
+
* {
|
|
1530
|
+
* selectedStore: 'object — { id, name } of the source branch',
|
|
1531
|
+
* minROI: 'number (optional) — only show transfers with ROI ≥ this',
|
|
1532
|
+
* }
|
|
1533
|
+
* ```
|
|
1534
|
+
*/
|
|
1535
|
+
propsSchema: Record<string, string>;
|
|
1536
|
+
/**
|
|
1537
|
+
* Optional: static prop defaults merged with LLM-extracted props before
|
|
1538
|
+
* the component is returned. Useful for things like the embedded
|
|
1539
|
+
* `externalTool` config that the workflow uses to fetch its own data.
|
|
1540
|
+
*/
|
|
1541
|
+
defaultProps?: Record<string, any>;
|
|
1542
|
+
}
|
|
1543
|
+
/**
|
|
1544
|
+
* The workflow selection captured during a routing call.
|
|
1545
|
+
* Set on AgentResponse when the LLM picks a workflow tool.
|
|
1546
|
+
*/
|
|
1547
|
+
interface SelectedWorkflow {
|
|
1548
|
+
/** Component name (matches WorkflowDescriptor.name) */
|
|
1549
|
+
name: string;
|
|
1550
|
+
/** Props extracted from the prompt + merged with workflow.defaultProps */
|
|
1551
|
+
props: Record<string, any>;
|
|
1552
|
+
}
|
|
1553
|
+
/**
|
|
1554
|
+
* The complete response from the multi-agent system.
|
|
1555
|
+
* Contains everything needed for text display + component generation.
|
|
1556
|
+
*/
|
|
1557
|
+
interface AgentResponse {
|
|
1558
|
+
/** Generated text response (analysis of the data) */
|
|
1559
|
+
text: string;
|
|
1560
|
+
/** All executed tools across all source agents (for component generation) */
|
|
1561
|
+
executedTools: ExecutedToolInfo[];
|
|
1562
|
+
/** Individual results from each source agent */
|
|
1563
|
+
sourceResults: SourceAgentResult[];
|
|
1564
|
+
/**
|
|
1565
|
+
* Populated when MainAgent wrote AND successfully executed a script during its turn.
|
|
1566
|
+
* Caller (agent-user-response.ts) persists it via ScriptStore.save().
|
|
1567
|
+
* Absent when MainAgent didn't write one (trivial question / all attempts failed).
|
|
1568
|
+
*/
|
|
1569
|
+
savedScript?: AgentWrittenScript;
|
|
1570
|
+
/**
|
|
1571
|
+
* Set when the LLM routed the question to a registered workflow component.
|
|
1572
|
+
* When present, the upstream caller should skip component generation and
|
|
1573
|
+
* return this workflow as the response.
|
|
1574
|
+
*/
|
|
1575
|
+
workflow?: SelectedWorkflow;
|
|
1576
|
+
}
|
|
1577
|
+
/**
|
|
1578
|
+
* A script MainAgent authored + verified during its turn. Shape aligns with
|
|
1579
|
+
* what ScriptStore.save() needs — minus store-assigned fields (id, timestamps, counts).
|
|
1580
|
+
*/
|
|
1581
|
+
interface AgentWrittenScript {
|
|
1582
|
+
/**
|
|
1583
|
+
* `ScriptRecipe.id` of the draft that was authored + verified during this turn.
|
|
1584
|
+
* The caller passes this to `ScriptStore.promoteToVerified(recipeId, …)` to
|
|
1585
|
+
* flip the draft to verified status and (when possible) drop the turn-suffix
|
|
1586
|
+
* from its filename.
|
|
1587
|
+
*/
|
|
1588
|
+
recipeId: string;
|
|
1589
|
+
name: string;
|
|
1590
|
+
intentDescription: string;
|
|
1591
|
+
tags: string[];
|
|
1592
|
+
parameters: Array<{
|
|
1593
|
+
name: string;
|
|
1594
|
+
type: 'string' | 'number' | 'date' | 'date_range' | 'enum' | 'boolean';
|
|
1595
|
+
required: boolean;
|
|
1596
|
+
default?: any;
|
|
1597
|
+
enumValues?: Record<string, string>;
|
|
1598
|
+
description: string;
|
|
1599
|
+
}>;
|
|
1600
|
+
scriptBody: string;
|
|
1601
|
+
/** Source IDs referenced by the script (extracted from ctx.query calls) */
|
|
1602
|
+
sourceIds: string[];
|
|
1603
|
+
/** Tables referenced in the script's SQL (regex-extracted) */
|
|
1604
|
+
tables: string[];
|
|
1605
|
+
/** Executed queries from the verified run — fed to component generation */
|
|
1606
|
+
executedQueries: Array<{
|
|
1607
|
+
sourceId: string;
|
|
1608
|
+
sourceName: string;
|
|
1609
|
+
sql: string;
|
|
1610
|
+
data: any[];
|
|
1611
|
+
count: number;
|
|
1612
|
+
totalCount?: number;
|
|
1613
|
+
executionTimeMs: number;
|
|
1614
|
+
/**
|
|
1615
|
+
* True for synthetic entries (ctx.emit datasets, the computed:_final
|
|
1616
|
+
* post-JS data). The component generator routes virtual sources through
|
|
1617
|
+
* the script_dataset sentinel toolId so the frontend resolves them via
|
|
1618
|
+
* queryCache instead of attempting to re-execute SQL.
|
|
1619
|
+
*/
|
|
1620
|
+
virtual?: boolean;
|
|
1621
|
+
}>;
|
|
1622
|
+
}
|
|
1623
|
+
/**
|
|
1624
|
+
* Configuration for the multi-agent system.
|
|
1625
|
+
* Controls limits, models, and behavior.
|
|
1626
|
+
*/
|
|
1627
|
+
interface AgentConfig {
|
|
1628
|
+
/** Max rows shown to the UI preview / inlined per source (default: 10) */
|
|
1629
|
+
maxRowsPerSource: number;
|
|
1630
|
+
/**
|
|
1631
|
+
* Max rows a source query may FETCH from the DB server-side (default: 2000).
|
|
1632
|
+
* Decoupled from what the main agent is shown: the full result is fetched and
|
|
1633
|
+
* summarized (bounded), but only a small/complete slice enters LLM context.
|
|
1634
|
+
* This lets small lookups (benchmark maps) arrive COMPLETE without letting
|
|
1635
|
+
* large results blow up context.
|
|
1636
|
+
*/
|
|
1637
|
+
maxRowsFetched: number;
|
|
1638
|
+
/** Model for the main agent (routing + analysis in one LLM call) */
|
|
1639
|
+
mainAgentModel: string;
|
|
1640
|
+
/** Model for source agent query generation */
|
|
1641
|
+
sourceAgentModel: string;
|
|
1642
|
+
/** API key for LLM calls */
|
|
1643
|
+
apiKey?: string;
|
|
1644
|
+
/** Max retry attempts per source agent */
|
|
1645
|
+
maxRetries: number;
|
|
1646
|
+
/** Max tool calling iterations for the main agent loop */
|
|
1647
|
+
maxIterations: number;
|
|
1648
|
+
/** Global knowledge base context (static, same for all users/questions — cached in system prompt) */
|
|
1649
|
+
globalKnowledgeBase?: string;
|
|
1650
|
+
/** Per-request knowledge base context (user-specific + query-matched — dynamic, not cached) */
|
|
1651
|
+
knowledgeBaseContext?: string;
|
|
1652
|
+
/** Collections registry (ChromaDB search hooks) for embedding-based schema + source search */
|
|
1653
|
+
collections?: any;
|
|
1654
|
+
/** Optional project ID for scoping embedding searches */
|
|
1655
|
+
projectId?: string;
|
|
1656
|
+
}
|
|
1657
|
+
/**
|
|
1658
|
+
* Default agent configuration
|
|
1659
|
+
*/
|
|
1660
|
+
declare const DEFAULT_AGENT_CONFIG: AgentConfig;
|
|
1661
|
+
|
|
1662
|
+
/**
|
|
1663
|
+
* Script Flow Types
|
|
1664
|
+
*
|
|
1665
|
+
* Defines interfaces for the script-based query architecture:
|
|
1666
|
+
* - ScriptRecipe: metadata for matching, validation, and quality tracking
|
|
1667
|
+
* - ScriptResult: output from executing a script
|
|
1668
|
+
* - ScriptMatch: result from the LLM-based script matcher
|
|
1669
|
+
*/
|
|
1670
|
+
/**
|
|
1671
|
+
* Recipe metadata stored alongside each script.
|
|
1672
|
+
* Used for matching, validation, and quality tracking.
|
|
1673
|
+
*/
|
|
1674
|
+
interface ScriptRecipe {
|
|
1675
|
+
/** Unique script identifier */
|
|
1676
|
+
id: string;
|
|
1677
|
+
/** Version number (incremented on regeneration) */
|
|
1678
|
+
version: number;
|
|
1679
|
+
/** Human-readable name (e.g., "Revenue by Dimension") */
|
|
1680
|
+
name: string;
|
|
1681
|
+
/** Natural language description of what this script does */
|
|
1682
|
+
intentDescription: string;
|
|
1683
|
+
/** Keyword tags for quick filtering */
|
|
1684
|
+
tags: string[];
|
|
1685
|
+
/** Source tool IDs this script queries (e.g., ["mssql-abc123_query"]) */
|
|
1686
|
+
sourceIds: string[];
|
|
1687
|
+
/** Table names used (for future schema drift detection) */
|
|
1688
|
+
tables: string[];
|
|
1689
|
+
/** Parameter definitions — what can vary */
|
|
1690
|
+
parameters: ScriptParameter[];
|
|
1691
|
+
/** The script function body as a string. Loaded from disk (scripts-store/<fileBase>.ts). */
|
|
1692
|
+
scriptBody: string;
|
|
1693
|
+
/**
|
|
1694
|
+
* On-disk filename stem for the body: scripts-store/<fileBase>.ts.
|
|
1695
|
+
* Editable in the IDE. Decided at authoring time (slug of `name`, with a
|
|
1696
|
+
* short id suffix on collision) and stable across promotion.
|
|
1697
|
+
*/
|
|
1698
|
+
fileBase?: string;
|
|
1699
|
+
/** sha256 of the on-disk body — lets the runtime detect manual edits. */
|
|
1700
|
+
bodyHash?: string;
|
|
1701
|
+
/** Project scope (single-VM deployments may leave this undefined). */
|
|
1702
|
+
projectId?: string;
|
|
1703
|
+
/** Times this script was used successfully */
|
|
1704
|
+
successCount: number;
|
|
1705
|
+
/** Times this script failed */
|
|
1706
|
+
failureCount: number;
|
|
1707
|
+
/** ISO timestamp of last usage */
|
|
1708
|
+
lastUsed: string;
|
|
1709
|
+
/** Original user question that created this script */
|
|
1710
|
+
createdFrom: string;
|
|
1711
|
+
/** ISO timestamp */
|
|
1712
|
+
createdAt: string;
|
|
1713
|
+
/** ISO timestamp */
|
|
1714
|
+
updatedAt: string;
|
|
1715
|
+
/**
|
|
1716
|
+
* `recipe.id` of the parent this script was forked from.
|
|
1717
|
+
* Undefined for root scripts (those written from scratch by MainAgent).
|
|
1718
|
+
* See backend/docs/SCRIPT-FLOW-FORK-ADAPT.md.
|
|
1719
|
+
*/
|
|
1720
|
+
parentId?: string;
|
|
1721
|
+
/** 0 for root scripts; `parent.forkDepth + 1` for forks. Capped at 3. */
|
|
1722
|
+
forkDepth?: number;
|
|
1723
|
+
/**
|
|
1724
|
+
* Brief description of what this fork changed vs its parent
|
|
1725
|
+
* (sourced from the matcher's `modificationHint`).
|
|
1726
|
+
*/
|
|
1727
|
+
forkReason?: string;
|
|
1728
|
+
/**
|
|
1729
|
+
* Validated component specs captured at authoring time. On a tier-high
|
|
1730
|
+
* replay these are rebound to fresh queryIds deterministically — no
|
|
1731
|
+
* component-generation LLM call, and the rendered columns can't drift from
|
|
1732
|
+
* what was validated when the script was authored. Absent on recipes
|
|
1733
|
+
* authored before this landed; those fall back to LLM component generation.
|
|
1734
|
+
* See backend/docs/SCRIPT-COMPONENT-CONSISTENCY.md.
|
|
1735
|
+
*/
|
|
1736
|
+
components?: ScriptComponentSpec[];
|
|
1737
|
+
/**
|
|
1738
|
+
* Lifecycle stage of this recipe on disk.
|
|
1739
|
+
* - 'draft': written by MainAgent's write_script during a turn; filtered out
|
|
1740
|
+
* of FTS results (status='verified' only) so the matcher never picks it.
|
|
1741
|
+
* Filename is suffixed with `turnId` to keep concurrent turns
|
|
1742
|
+
* from clobbering each other's drafts.
|
|
1743
|
+
* - 'verified': promoted after `execute_script` succeeded; the matcher sees it.
|
|
1744
|
+
* Filename drops the turn suffix unless a verified file with
|
|
1745
|
+
* the same slug already exists (collision case keeps the suffix).
|
|
1746
|
+
*
|
|
1747
|
+
* Recipes loaded from disk without this field default to 'verified' so
|
|
1748
|
+
* existing scripts keep working unchanged.
|
|
1749
|
+
*/
|
|
1750
|
+
status?: 'draft' | 'verified';
|
|
1751
|
+
/**
|
|
1752
|
+
* Per-turn unique suffix used for draft filenames (e.g. `1714745623-x9k2`).
|
|
1753
|
+
* Set when the draft is saved; carried until the recipe is promoted.
|
|
1754
|
+
*/
|
|
1755
|
+
turnId?: string;
|
|
1756
|
+
/**
|
|
1757
|
+
* Last execution error captured by `recordDraftError` while the recipe was
|
|
1758
|
+
* still a draft. Lets users open the draft .json file and see why it failed
|
|
1759
|
+
* without grepping logs. Cleared on promotion to 'verified'.
|
|
1760
|
+
*/
|
|
1761
|
+
lastError?: {
|
|
1762
|
+
phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
|
|
1763
|
+
message: string;
|
|
1764
|
+
at: string;
|
|
1765
|
+
attempt: number;
|
|
1766
|
+
};
|
|
1767
|
+
}
|
|
1768
|
+
interface ScriptParameter {
|
|
1769
|
+
/** Parameter name (used in script body as params.name) */
|
|
1770
|
+
name: string;
|
|
1771
|
+
/** Parameter type */
|
|
1772
|
+
type: 'string' | 'number' | 'date' | 'date_range' | 'enum' | 'boolean';
|
|
1773
|
+
/** Whether this parameter is required */
|
|
1774
|
+
required: boolean;
|
|
1775
|
+
/** Default value if not provided */
|
|
1776
|
+
default?: any;
|
|
1777
|
+
/** For enum type — maps user-facing values to internal values */
|
|
1778
|
+
enumValues?: Record<string, string>;
|
|
1779
|
+
/** Human-readable description (used in the matcher LLM prompt) */
|
|
1780
|
+
description: string;
|
|
1781
|
+
}
|
|
1782
|
+
/**
|
|
1783
|
+
* A reusable component binding captured when a script is authored. Stored on
|
|
1784
|
+
* the recipe so tier-high replays rebuild components deterministically (rebind
|
|
1785
|
+
* to fresh queryIds) instead of re-running the component-picker LLM.
|
|
1786
|
+
*/
|
|
1787
|
+
interface ScriptComponentSpec {
|
|
1788
|
+
/** Registered component name (e.g. "DynamicBarChart") — matched against the available component library. */
|
|
1789
|
+
componentType: string;
|
|
1790
|
+
/** `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). */
|
|
1791
|
+
sourceRef: string;
|
|
1792
|
+
/** Present only when sourceRef === 'federation' — the DuckDB SQL to re-execute on replay. */
|
|
1793
|
+
federationSql?: string;
|
|
1794
|
+
/** Present only when sourceRef === 'markdown' — the narrative text to render on replay (markdown has no data source, so its content must be persisted). */
|
|
1795
|
+
content?: string;
|
|
1796
|
+
title?: string;
|
|
1797
|
+
description?: string;
|
|
1798
|
+
/** Validated axis/value keys + aggregation — all referencing real columns of the bound source. */
|
|
1799
|
+
config: Record<string, any>;
|
|
1800
|
+
}
|
|
1801
|
+
/**
|
|
1802
|
+
* Result from executing a script via ScriptRunner.
|
|
1803
|
+
*/
|
|
1804
|
+
interface ScriptResult {
|
|
1805
|
+
/** Whether the script executed successfully */
|
|
1806
|
+
success: boolean;
|
|
1807
|
+
/** Combined data from all queries */
|
|
1808
|
+
data: any[];
|
|
1809
|
+
/** Individual query results tracked during execution */
|
|
1810
|
+
executedQueries: ScriptQueryResult[];
|
|
1811
|
+
/** Error message if failed */
|
|
1812
|
+
error?: string;
|
|
1813
|
+
/**
|
|
1814
|
+
* Where in the lifecycle the error occurred. Lets MainAgent's fix-loop
|
|
1815
|
+
* decide between "rewrite the whole draft" (compile) and "patch the
|
|
1816
|
+
* specific line" (runtime).
|
|
1817
|
+
*/
|
|
1818
|
+
errorPhase?: 'compile' | 'runtime' | 'timeout' | 'ipc';
|
|
1819
|
+
/** Total execution time in milliseconds */
|
|
1820
|
+
executionTimeMs: number;
|
|
1821
|
+
}
|
|
1822
|
+
/**
|
|
1823
|
+
* A single query executed during script runtime.
|
|
1824
|
+
* Tracked by ScriptContext for component generation and debugging.
|
|
1825
|
+
*/
|
|
1826
|
+
interface ScriptQueryResult {
|
|
1827
|
+
/** Source tool ID */
|
|
1828
|
+
sourceId: string;
|
|
1829
|
+
/** Human-readable source name */
|
|
1830
|
+
sourceName: string;
|
|
1831
|
+
/** The SQL that was executed */
|
|
1832
|
+
sql: string;
|
|
1833
|
+
/** Result data rows */
|
|
1834
|
+
data: any[];
|
|
1835
|
+
/** Number of rows returned */
|
|
1836
|
+
count: number;
|
|
1837
|
+
/** Total rows that matched before limit (if available) */
|
|
1838
|
+
totalCount?: number;
|
|
1839
|
+
/** Query execution time in milliseconds */
|
|
1840
|
+
executionTimeMs: number;
|
|
1841
|
+
/**
|
|
1842
|
+
* True for rows that did NOT come from a real SQL execution — either a
|
|
1843
|
+
* ctx.emit() dataset or the synthesized "computed:_final" entry that
|
|
1844
|
+
* carries the script's post-JS returned data. The component generator
|
|
1845
|
+
* uses this to route the resulting component through the script_dataset
|
|
1846
|
+
* sentinel toolId so the frontend resolves it via the queryCache short-circuit.
|
|
1847
|
+
*/
|
|
1848
|
+
virtual?: boolean;
|
|
1849
|
+
}
|
|
1850
|
+
/**
|
|
1851
|
+
* Match tier returned by the LLM script matcher.
|
|
1852
|
+
*
|
|
1853
|
+
* - 'high': the script answers the question directly; only parameter values
|
|
1854
|
+
* may differ. The runtime replays it with extracted params (cheapest path).
|
|
1855
|
+
* - 'near': the script answers a STRUCTURALLY similar question but needs
|
|
1856
|
+
* body modification (different metric, dimension, table, filter shape).
|
|
1857
|
+
* The runtime forks the parent and adapts the body via MainAgent's normal
|
|
1858
|
+
* write_script + execute_script loop — no SourceAgent dispatch needed.
|
|
1859
|
+
* See backend/docs/SCRIPT-FLOW-FORK-ADAPT.md for the full design.
|
|
1860
|
+
* - 'none': no script is relevant; full agent flow runs.
|
|
1861
|
+
*/
|
|
1862
|
+
type MatchTier = 'high' | 'near' | 'none';
|
|
1863
|
+
/**
|
|
1864
|
+
* Result from the LLM-based script matcher.
|
|
1865
|
+
*
|
|
1866
|
+
* For `tier: 'high'`, `extractedParams` carries the values to pass to the
|
|
1867
|
+
* existing script. For `tier: 'near'`, `gaps` and `modificationHint` describe
|
|
1868
|
+
* what the fork-author needs to change in the parent body.
|
|
1869
|
+
*/
|
|
1870
|
+
interface ScriptMatch {
|
|
1871
|
+
/** The matched script recipe */
|
|
1872
|
+
recipe: ScriptRecipe;
|
|
1873
|
+
/** Match tier — see MatchTier docs */
|
|
1874
|
+
tier: MatchTier;
|
|
1875
|
+
/** Similarity score (0-1, derived from LLM tier) */
|
|
1876
|
+
similarity: number;
|
|
1877
|
+
/**
|
|
1878
|
+
* Legacy confidence level. Mirrors `tier === 'high'`/`'near'` for now;
|
|
1879
|
+
* kept so existing callers compile while we migrate to tier-based logic.
|
|
1880
|
+
*/
|
|
1881
|
+
confidence: 'high' | 'medium';
|
|
1882
|
+
/** Parameters extracted from the user question by the LLM (tier='high') */
|
|
1883
|
+
extractedParams?: Record<string, any>;
|
|
1884
|
+
/** What the user question needs that the parent doesn't cover (tier='near') */
|
|
1885
|
+
gaps?: string[];
|
|
1886
|
+
/** One-sentence description of the change the fork-author should make (tier='near') */
|
|
1887
|
+
modificationHint?: string;
|
|
1888
|
+
/** Why the matcher made this choice (for logs and telemetry) */
|
|
1889
|
+
reasoning?: string;
|
|
1890
|
+
}
|
|
1891
|
+
|
|
1892
|
+
/**
|
|
1893
|
+
* ScriptRecipeStore — injected metadata backend for the script flow.
|
|
1894
|
+
*
|
|
1895
|
+
* The SDK is standalone (no DB dependency). The backend implements this
|
|
1896
|
+
* interface over Postgres (full-text search + atomic counters) and injects it
|
|
1897
|
+
* via `collections['script-recipes']`, exactly like `collections['source-embeddings']`.
|
|
1898
|
+
* `ScriptStore` consumes it for all METADATA operations while keeping the
|
|
1899
|
+
* executable body on disk as scripts-store/<fileBase>.ts.
|
|
1900
|
+
*
|
|
1901
|
+
* All metadata rows are plain JSON (no scriptBody — that lives on disk).
|
|
1902
|
+
* See backend/docs/SCRIPT-FLOW-SCALING-ISSUES.md (#1, #3, #7).
|
|
1903
|
+
*/
|
|
1904
|
+
|
|
1905
|
+
/** One recipe's metadata as stored in Postgres (mirrors the script_recipes table). */
|
|
1906
|
+
interface ScriptRecipeMetaRow {
|
|
1907
|
+
id: string;
|
|
1908
|
+
projectId?: string | null;
|
|
1909
|
+
version: number;
|
|
1910
|
+
name: string;
|
|
1911
|
+
intentDescription: string;
|
|
1912
|
+
tags: string[] | null;
|
|
1913
|
+
createdFrom: string | null;
|
|
1914
|
+
sourceIds: string[] | null;
|
|
1915
|
+
tables: string[] | null;
|
|
1916
|
+
parameters: ScriptParameter[] | null;
|
|
1917
|
+
components?: ScriptComponentSpec[] | null;
|
|
1918
|
+
fileBase: string;
|
|
1919
|
+
bodyHash?: string | null;
|
|
1920
|
+
successCount: number;
|
|
1921
|
+
failureCount: number;
|
|
1922
|
+
lastUsed: string | null;
|
|
1923
|
+
parentId?: string | null;
|
|
1924
|
+
forkDepth?: number | null;
|
|
1925
|
+
forkReason?: string | null;
|
|
1926
|
+
status: 'draft' | 'verified' | string;
|
|
1927
|
+
turnId?: string | null;
|
|
1928
|
+
lastError?: {
|
|
1929
|
+
phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
|
|
1930
|
+
message: string;
|
|
1931
|
+
at: string;
|
|
1932
|
+
attempt: number;
|
|
1933
|
+
} | null;
|
|
1934
|
+
createdAt?: string | null;
|
|
1935
|
+
updatedAt?: string | null;
|
|
1936
|
+
}
|
|
1937
|
+
interface ScriptRecipeStore {
|
|
1938
|
+
/** FTS shortlist of healthy verified recipes for the matcher (metadata only). */
|
|
1939
|
+
search(params: {
|
|
1940
|
+
prompt: string;
|
|
1941
|
+
projectId?: string;
|
|
1942
|
+
limit?: number;
|
|
1943
|
+
}): Promise<ScriptRecipeMetaRow[]>;
|
|
1944
|
+
/** Fetch one recipe by id (any status). */
|
|
1945
|
+
getById(id: string): Promise<ScriptRecipeMetaRow | null>;
|
|
1946
|
+
/** Count healthy verified recipes (drives the "any scripts?" gate). */
|
|
1947
|
+
count(params?: {
|
|
1948
|
+
projectId?: string;
|
|
1949
|
+
}): Promise<number>;
|
|
1950
|
+
/** Insert or update a recipe row (keyed by id). */
|
|
1951
|
+
upsert(row: ScriptRecipeMetaRow): Promise<void>;
|
|
1952
|
+
/** Atomically bump counters / last-used. */
|
|
1953
|
+
updateStats(id: string, patch: {
|
|
1954
|
+
successDelta?: number;
|
|
1955
|
+
failureDelta?: number;
|
|
1956
|
+
lastUsed?: string;
|
|
1957
|
+
}): Promise<void>;
|
|
1958
|
+
/** Flip a draft to verified, applying provenance + optional fork lineage. */
|
|
1959
|
+
promote(id: string, patch: {
|
|
1960
|
+
sourceIds: string[];
|
|
1961
|
+
tables: string[];
|
|
1962
|
+
fileBase?: string;
|
|
1963
|
+
parentId?: string;
|
|
1964
|
+
forkDepth?: number;
|
|
1965
|
+
forkReason?: string;
|
|
1966
|
+
components?: ScriptComponentSpec[];
|
|
1967
|
+
}): Promise<ScriptRecipeMetaRow | null>;
|
|
1968
|
+
/** Stamp a draft's last execution error. */
|
|
1969
|
+
recordDraftError(id: string, err: {
|
|
1970
|
+
phase: string;
|
|
1971
|
+
message: string;
|
|
1972
|
+
attempt: number;
|
|
1973
|
+
at: string;
|
|
1974
|
+
}): Promise<void>;
|
|
1975
|
+
/** Delete a recipe row (body file removed separately). */
|
|
1976
|
+
remove(id: string): Promise<void>;
|
|
1977
|
+
/** True if `fileBase` is taken by a different recipe in this project. */
|
|
1978
|
+
fileBaseTaken(fileBase: string, excludeId: string, projectId?: string): Promise<boolean>;
|
|
1979
|
+
}
|
|
1980
|
+
/** Pull the injected store off the collections bag (or null if not wired). */
|
|
1981
|
+
declare function resolveScriptRecipeStore(collections: any): ScriptRecipeStore | null;
|
|
1982
|
+
|
|
1983
|
+
/**
|
|
1984
|
+
* ScriptStore — Postgres metadata + on-disk body for script recipes.
|
|
1985
|
+
*
|
|
1986
|
+
* Split of responsibilities:
|
|
1987
|
+
* - METADATA → injected `ScriptRecipeStore` (Postgres FTS + atomic counters),
|
|
1988
|
+
* resolved from `collections['script-recipes']`.
|
|
1989
|
+
* - BODY → scripts-store/<fileBase>.ts, editable in your IDE. Written
|
|
1990
|
+
* atomically (temp + rename); `bodyHash` (sha256) detects edits.
|
|
1991
|
+
*
|
|
1992
|
+
* The old "read every file every turn + send the whole catalog to the LLM"
|
|
1993
|
+
* matcher is gone — matching is `store.search(prompt)` (FTS shortlist). The
|
|
1994
|
+
* draft/verified filename dance is gone too: `status` is a DB column and the
|
|
1995
|
+
* file keeps a stable `<fileBase>.ts` name across promotion.
|
|
1996
|
+
*
|
|
1997
|
+
* When no metadata store is injected, the store degrades to a safe no-op
|
|
1998
|
+
* (count 0 → script flow disabled) instead of crashing.
|
|
1999
|
+
*
|
|
2000
|
+
* See backend/docs/SCRIPT-FLOW-SCALING-ISSUES.md.
|
|
2001
|
+
*/
|
|
2002
|
+
|
|
2003
|
+
interface SaveDraftInput {
|
|
2004
|
+
/** Reuse an existing draft (retry); omit to mint a new one. */
|
|
2005
|
+
recipeId?: string;
|
|
2006
|
+
/** Per-turn unique suffix, stable across retries within the turn. */
|
|
2007
|
+
turnId: string;
|
|
2008
|
+
name: string;
|
|
2009
|
+
intentDescription: string;
|
|
2010
|
+
tags: string[];
|
|
2011
|
+
parameters: ScriptParameter[];
|
|
2012
|
+
scriptBody: string;
|
|
2013
|
+
createdFrom: string;
|
|
2014
|
+
}
|
|
2015
|
+
interface PromoteToVerifiedInput {
|
|
2016
|
+
sourceIds: string[];
|
|
2017
|
+
tables: string[];
|
|
2018
|
+
parentId?: string;
|
|
2019
|
+
forkDepth?: number;
|
|
2020
|
+
forkReason?: string;
|
|
2021
|
+
components?: ScriptComponentSpec[];
|
|
2022
|
+
}
|
|
2023
|
+
interface ScriptStoreOptions {
|
|
2024
|
+
/** Explicit metadata store, or resolved from `collections['script-recipes']`. */
|
|
2025
|
+
store?: ScriptRecipeStore | null;
|
|
2026
|
+
collections?: any;
|
|
2027
|
+
/** Body directory (defaults to <cwd>/scripts-store). */
|
|
2028
|
+
baseDir?: string;
|
|
2029
|
+
/** Project scope stamped on every row. */
|
|
2030
|
+
projectId?: string;
|
|
2031
|
+
}
|
|
2032
|
+
/**
|
|
2033
|
+
* Normalize a scriptBody into the on-disk form (strip a leading comment block,
|
|
2034
|
+
* ensure `export async function getData`). Exported for MainAgent.
|
|
2035
|
+
*/
|
|
2036
|
+
declare function normalizeScriptBody(scriptBody: string): string;
|
|
2037
|
+
declare class ScriptStore {
|
|
2038
|
+
private store;
|
|
2039
|
+
private storeDir;
|
|
2040
|
+
private projectId?;
|
|
2041
|
+
constructor(opts?: ScriptStoreOptions);
|
|
2042
|
+
/** Whether a metadata store is wired (matcher / authoring are gated on this). */
|
|
2043
|
+
hasStore(): boolean;
|
|
2044
|
+
/** Number of healthy verified recipes (gates the script-matching path). */
|
|
2045
|
+
count(): Promise<number>;
|
|
2046
|
+
/**
|
|
2047
|
+
* FTS shortlist for the matcher (metadata only — bodies are loaded lazily by
|
|
2048
|
+
* `get()` once the LLM picks one). Returns verified, healthy recipes ranked
|
|
2049
|
+
* by relevance.
|
|
2050
|
+
*/
|
|
2051
|
+
search(prompt: string, limit?: number): Promise<ScriptRecipe[]>;
|
|
2052
|
+
/** Fetch one recipe by id with its body loaded from disk. */
|
|
2053
|
+
get(id: string): Promise<ScriptRecipe | null>;
|
|
2054
|
+
/** Create or update a recipe (metadata upsert + body write when changed). */
|
|
2055
|
+
save(recipe: ScriptRecipe): Promise<void>;
|
|
2056
|
+
/**
|
|
2057
|
+
* Persist (or update) a draft. Within a turn, retries that pass the same
|
|
2058
|
+
* `recipeId` overwrite the same row + file; a fresh `recipeId` mints a new
|
|
2059
|
+
* draft. The body is visible at scripts-store/<fileBase>.ts immediately.
|
|
2060
|
+
*/
|
|
2061
|
+
saveDraft(input: SaveDraftInput): Promise<ScriptRecipe>;
|
|
2062
|
+
/** Stamp a draft's last execution error (metadata only). */
|
|
2063
|
+
recordDraftError(recipeId: string, err: {
|
|
2064
|
+
phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
|
|
2065
|
+
message: string;
|
|
2066
|
+
attempt: number;
|
|
2067
|
+
}): Promise<void>;
|
|
2068
|
+
/**
|
|
2069
|
+
* Promote a successfully-executed draft into a verified script.
|
|
2070
|
+
* The on-disk body already exists at <fileBase>.ts (written at write_script
|
|
2071
|
+
* time) and keeps its name — only the DB row flips status + provenance.
|
|
2072
|
+
*/
|
|
2073
|
+
promoteToVerified(recipeId: string, input: PromoteToVerifiedInput): Promise<ScriptRecipe | null>;
|
|
2074
|
+
/**
|
|
2075
|
+
* Drop a draft (row + body file). MainAgent calls this at end-of-turn when a
|
|
2076
|
+
* draft was authored but never verified — failed drafts are never matched, so
|
|
2077
|
+
* deleting them immediately avoids unbounded accumulation (#5). No-op if the
|
|
2078
|
+
* recipe isn't a draft (so a promoted/verified script is never removed here).
|
|
2079
|
+
*/
|
|
2080
|
+
discardDraft(recipeId: string): Promise<void>;
|
|
2081
|
+
/** Delete a recipe (row + body file). */
|
|
2082
|
+
delete(id: string): Promise<void>;
|
|
2083
|
+
/** Record a successful execution (atomic counter bump). */
|
|
2084
|
+
recordSuccess(id: string): Promise<void>;
|
|
2085
|
+
/** Record a failed execution (atomic counter bump). */
|
|
2086
|
+
recordFailure(id: string): Promise<void>;
|
|
2087
|
+
/** Absolute path to the .ts body for a recipe (used by the runner/MainAgent). */
|
|
2088
|
+
getScriptPath(recipe: ScriptRecipe): string;
|
|
2089
|
+
private removeById;
|
|
2090
|
+
private rowToRecipe;
|
|
2091
|
+
private recipeToRow;
|
|
2092
|
+
/** slug of name, with a short id suffix when the bare slug is already taken. */
|
|
2093
|
+
private computeFileBase;
|
|
2094
|
+
private toSlug;
|
|
2095
|
+
private hash;
|
|
2096
|
+
private bodyPath;
|
|
2097
|
+
private readBody;
|
|
2098
|
+
/** Atomic body write (temp + rename) so concurrent reads never see a partial file. */
|
|
2099
|
+
private writeBody;
|
|
2100
|
+
private unlinkBody;
|
|
2101
|
+
}
|
|
2102
|
+
|
|
2103
|
+
/**
|
|
2104
|
+
* Main Agent (Orchestrator)
|
|
2105
|
+
*
|
|
2106
|
+
* A single LLM.streamWithTools() call that handles everything:
|
|
2107
|
+
* - Routing: decides which source(s) to query based on summaries
|
|
2108
|
+
* - Querying: calls source tools (each wraps an independent SourceAgent)
|
|
2109
|
+
* - Direct tools: calls pre-built function tools directly with LLM-provided params
|
|
2110
|
+
* - Re-querying: if data is wrong/incomplete, calls tools again with modified intent
|
|
2111
|
+
* - Analysis: generates final text response from the data
|
|
2112
|
+
*
|
|
2113
|
+
* Two tool types:
|
|
2114
|
+
* - "source" tools: main agent sees summaries, SourceAgent handles SQL generation independently
|
|
2115
|
+
* - "direct" tools: main agent calls fn() directly with structured params (no SourceAgent)
|
|
2116
|
+
*/
|
|
2117
|
+
|
|
2118
|
+
declare class MainAgent {
|
|
2119
|
+
private externalTools;
|
|
2120
|
+
private workflows;
|
|
2121
|
+
private config;
|
|
2122
|
+
private streamBuffer;
|
|
2123
|
+
/**
|
|
2124
|
+
* Optional: when provided, MainAgent exposes the `write_script` /
|
|
2125
|
+
* `execute_script` tools to the LLM and persists drafts to disk via the
|
|
2126
|
+
* store. Headless callers (alert analyzer, metric resolver) omit these to
|
|
2127
|
+
* suppress script authoring entirely — drafts would otherwise leak onto
|
|
2128
|
+
* disk with no caller to promote or clean them up.
|
|
2129
|
+
*/
|
|
2130
|
+
private scriptStore;
|
|
2131
|
+
private turnId;
|
|
2132
|
+
private createdFromPrompt;
|
|
2133
|
+
private scriptState;
|
|
2134
|
+
/**
|
|
2135
|
+
* Fork mode — set when this turn is adapting a near-matching parent script.
|
|
2136
|
+
* In fork mode there is no legitimate "answer with bare text" outcome: the
|
|
2137
|
+
* only correct first move is a tool call (write_script, or a source tool for
|
|
2138
|
+
* schema discovery). We therefore force tool use on the first LLM iteration
|
|
2139
|
+
* so the model can't end its turn with a bare "I'll adapt…" preamble and zero
|
|
2140
|
+
* tool calls. Never set on the fresh-authoring / general-question path.
|
|
2141
|
+
*/
|
|
2142
|
+
private forkMode;
|
|
2143
|
+
/**
|
|
2144
|
+
* Per-turn cancellation signal (user hit "Stop"). Set at the top of
|
|
2145
|
+
* handleQuestion and read by the tool handler, the SourceAgent dispatch, and
|
|
2146
|
+
* the script subprocess so an abort tears down every layer of the turn.
|
|
2147
|
+
*/
|
|
2148
|
+
private abortSignal?;
|
|
2149
|
+
constructor(externalTools: ExternalTool[], config: AgentConfig, scriptStore?: ScriptStore, turnId?: string, streamBuffer?: StreamBuffer, workflows?: WorkflowDescriptor[], forkMode?: boolean);
|
|
2150
|
+
private get scriptingEnabled();
|
|
2151
|
+
/**
|
|
2152
|
+
* Handle a user question using the multi-agent system.
|
|
2153
|
+
*
|
|
2154
|
+
* This is ONE LLM.streamWithTools() call. The LLM:
|
|
2155
|
+
* 1. Sees source summaries + direct tool descriptions in system prompt
|
|
2156
|
+
* 2. Decides which tool(s) to call (routing)
|
|
2157
|
+
* 3. Source tools → SourceAgent runs independently → returns data
|
|
2158
|
+
* 4. Direct tools → fn() called directly with LLM params → returns data
|
|
2159
|
+
* 5. Generates final analysis text
|
|
2160
|
+
*/
|
|
2161
|
+
handleQuestion(userPrompt: string, apiKey?: string, conversationHistory?: string, streamCallback?: (chunk: string) => void, signal?: AbortSignal): Promise<AgentResponse>;
|
|
2162
|
+
private handleWriteScript;
|
|
2163
|
+
private handleExecuteScript;
|
|
2164
|
+
/**
|
|
2165
|
+
* Build the AgentWrittenScript payload the caller will hand to
|
|
2166
|
+
* `ScriptStore.promoteToVerified()`. Only returned when a verified
|
|
2167
|
+
* successful execution is on record.
|
|
2168
|
+
*/
|
|
2169
|
+
private buildSavedScript;
|
|
2170
|
+
private normalizeParameterList;
|
|
2171
|
+
/**
|
|
2172
|
+
* Use the schema embedding collection to pre-select relevant tables for
|
|
2173
|
+
* this source + intent. Returns a formatted schema block if confidence is
|
|
2174
|
+
* high (top match ≥ 0.55 and ≥3 candidates), otherwise null.
|
|
2175
|
+
*
|
|
2176
|
+
* When this returns a block, we can skip the SourceAgent's `search_schema`
|
|
2177
|
+
* loop and reduce iteration budget. When it returns null, the SourceAgent
|
|
2178
|
+
* falls back to the existing LLM-driven keyword search (same as today).
|
|
2179
|
+
*/
|
|
2180
|
+
private preResolveSchema;
|
|
2181
|
+
/**
|
|
2182
|
+
* Execute a direct tool — call fn() with LLM-provided params, no SourceAgent.
|
|
2183
|
+
*/
|
|
2184
|
+
private handleDirectTool;
|
|
2185
|
+
/**
|
|
2186
|
+
* Build the main agent's system prompt with source summaries, direct tool descriptions,
|
|
2187
|
+
* and workflow component descriptions.
|
|
2188
|
+
*/
|
|
2189
|
+
private buildSystemPrompt;
|
|
2190
|
+
/**
|
|
2191
|
+
* Build tool definitions for source tools — summary-only descriptions.
|
|
2192
|
+
* The full schema is inside the SourceAgent which runs independently.
|
|
2193
|
+
*/
|
|
2194
|
+
private buildSourceToolDefinitions;
|
|
2195
|
+
/**
|
|
2196
|
+
* Build tool definitions for direct tools — expose their actual params.
|
|
2197
|
+
* These are called directly by the main agent LLM, no SourceAgent.
|
|
2198
|
+
*/
|
|
2199
|
+
private buildDirectToolDefinitions;
|
|
2200
|
+
/**
|
|
2201
|
+
* Capture a workflow selection. We do NOT execute anything — the LLM has
|
|
2202
|
+
* already extracted the props it wants the workflow rendered with. We
|
|
2203
|
+
* record the selection (via the capture callback) and return a short
|
|
2204
|
+
* acknowledgement so the LLM ends its turn cleanly without writing
|
|
2205
|
+
* analysis text or calling more tools.
|
|
2206
|
+
*/
|
|
2207
|
+
private handleWorkflow;
|
|
2208
|
+
/**
|
|
2209
|
+
* Build LLM tool definitions for workflow components. The workflow's
|
|
2210
|
+
* propsSchema becomes the tool's input_schema so the LLM extracts props
|
|
2211
|
+
* directly from the prompt — same mechanic as direct tools.
|
|
2212
|
+
*/
|
|
2213
|
+
private buildWorkflowToolDefinitions;
|
|
2214
|
+
/**
|
|
2215
|
+
* Format a source agent's result as a clean string for the main agent LLM.
|
|
2216
|
+
*/
|
|
2217
|
+
private formatResultForMainAgent;
|
|
2218
|
+
/**
|
|
2219
|
+
* Get source summaries (for external inspection/debugging).
|
|
2220
|
+
*/
|
|
2221
|
+
getSourceSummaries(): SourceSummary[];
|
|
2222
|
+
}
|
|
2223
|
+
|
|
2224
|
+
/**
|
|
2225
|
+
* Represents an action that can be performed on a UIBlock
|
|
2226
|
+
*/
|
|
2227
|
+
interface Action {
|
|
2228
|
+
id: string;
|
|
2229
|
+
name: string;
|
|
2230
|
+
type: string;
|
|
2231
|
+
[key: string]: any;
|
|
2232
|
+
}
|
|
2233
|
+
|
|
2234
|
+
type SystemPrompt = string | Anthropic.Messages.TextBlockParam[];
|
|
2235
|
+
interface LLMMessages {
|
|
2236
|
+
sys: SystemPrompt;
|
|
2237
|
+
user: string;
|
|
2238
|
+
prefill?: string;
|
|
2239
|
+
}
|
|
2240
|
+
interface LLMOptions {
|
|
2241
|
+
model?: string;
|
|
2242
|
+
maxTokens?: number;
|
|
2243
|
+
temperature?: number;
|
|
2244
|
+
topP?: number;
|
|
2245
|
+
apiKey?: string;
|
|
2246
|
+
baseURL?: string;
|
|
2247
|
+
partial?: (chunk: string) => void;
|
|
2248
|
+
/**
|
|
2249
|
+
* Per-request cancellation. When the caller aborts this signal (user hit
|
|
2250
|
+
* "Stop"), the underlying provider request is cancelled and the call throws
|
|
2251
|
+
* a RequestAbortedError. Threaded into the provider `messages.create` request
|
|
2252
|
+
* options and checked between tool-loop iterations. Currently honored on the
|
|
2253
|
+
* Anthropic path (the agent flow's default provider).
|
|
2254
|
+
*/
|
|
2255
|
+
signal?: AbortSignal;
|
|
2256
|
+
/**
|
|
2257
|
+
* Forces a tool call on the FIRST iteration of streamWithTools only
|
|
2258
|
+
* (subsequent iterations revert to auto). Used by fork mode to stop the
|
|
2259
|
+
* model from ending its turn with a bare "I'll adapt the script…" preamble
|
|
2260
|
+
* and zero tool calls. `{ type: 'any' }` lets the model pick which tool
|
|
2261
|
+
* (write_script in the common case, a source tool for schema discovery);
|
|
2262
|
+
* `{ type: 'tool', name }` pins a specific tool. Honored on both the
|
|
2263
|
+
* Anthropic path and the OpenAI/OpenRouter path (mapped to OpenAI's
|
|
2264
|
+
* tool_choice: 'required' / a named function).
|
|
2265
|
+
*/
|
|
2266
|
+
firstIterationToolChoice?: {
|
|
2267
|
+
type: 'any';
|
|
2268
|
+
} | {
|
|
2269
|
+
type: 'tool';
|
|
2270
|
+
name: string;
|
|
2271
|
+
};
|
|
2272
|
+
/**
|
|
2273
|
+
* Internal — set only by the OpenRouter wrappers when the target is a Claude
|
|
2274
|
+
* model. Tells the OpenAI-wire path to emit Anthropic `cache_control`
|
|
2275
|
+
* breakpoints (OpenRouter forwards them to Anthropic for prompt caching).
|
|
2276
|
+
* Never set for direct OpenAI/Groq calls, so their requests are unchanged.
|
|
2277
|
+
*/
|
|
2278
|
+
_openrouterClaudeCaching?: boolean;
|
|
2279
|
+
/**
|
|
2280
|
+
* Internal — OpenRouter provider-routing preferences (forwarded as the
|
|
2281
|
+
* `provider` body field). Set by the OpenRouter wrappers to steer routing to
|
|
2282
|
+
* a fast backend (e.g. {sort:'throughput'}). Never set for direct OpenAI/Groq.
|
|
2283
|
+
*/
|
|
2284
|
+
_openrouterProvider?: Record<string, unknown>;
|
|
2285
|
+
}
|
|
2286
|
+
interface Tool {
|
|
2287
|
+
name: string;
|
|
2288
|
+
description: string;
|
|
2289
|
+
input_schema: {
|
|
2290
|
+
type: string;
|
|
2291
|
+
properties: Record<string, any>;
|
|
2292
|
+
required?: string[];
|
|
2293
|
+
};
|
|
2294
|
+
}
|
|
2295
|
+
declare class LLM {
|
|
2296
|
+
static text(messages: LLMMessages, options?: LLMOptions): Promise<string>;
|
|
2297
|
+
static stream<T = string>(messages: LLMMessages, options?: LLMOptions, json?: boolean): Promise<T extends string ? string : any>;
|
|
2298
|
+
static streamWithTools(messages: LLMMessages, tools: Tool[], toolHandler: (toolName: string, toolInput: any) => Promise<any>, options?: LLMOptions, maxIterations?: number): Promise<string>;
|
|
2299
|
+
/**
|
|
2300
|
+
* Normalize system prompt to Anthropic format
|
|
2301
|
+
* Converts string to array format if needed
|
|
2302
|
+
* @param sys - System prompt (string or array of blocks)
|
|
2303
|
+
* @returns Normalized system prompt for Anthropic API
|
|
2304
|
+
*/
|
|
2305
|
+
private static _normalizeSystemPrompt;
|
|
2306
|
+
/**
|
|
2307
|
+
* Strip unpaired UTF-16 surrogates from every text field of a message set.
|
|
2308
|
+
*
|
|
2309
|
+
* A lone surrogate (from mid-pair string slicing or corrupt source data)
|
|
2310
|
+
* serializes to a bare `\udXXX` escape that strict JSON parsers — including
|
|
2311
|
+
* the one on Anthropic's API — reject with "no low surrogate in string",
|
|
2312
|
+
* failing the whole request. Sanitizing here, at the single boundary every
|
|
2313
|
+
* provider call flows through, guarantees no request can carry one.
|
|
2314
|
+
*/
|
|
2315
|
+
private static _sanitizeMessages;
|
|
2316
|
+
/**
|
|
2317
|
+
* Log cache usage metrics from Anthropic API response
|
|
2318
|
+
* Shows cache hits, costs, and savings
|
|
2319
|
+
*/
|
|
2320
|
+
private static _logCacheUsage;
|
|
2321
|
+
/**
|
|
2322
|
+
* Parse model string to extract provider and model name
|
|
2323
|
+
* @param modelString - Format: "provider/model-name" or just "model-name"
|
|
2324
|
+
* @returns [provider, modelName]
|
|
2325
|
+
*
|
|
2326
|
+
* @example
|
|
2327
|
+
* "anthropic/claude-sonnet-4-5" → ["anthropic", "claude-sonnet-4-5"]
|
|
2328
|
+
* "groq/openai/gpt-oss-120b" → ["groq", "openai/gpt-oss-120b"]
|
|
2329
|
+
* "claude-sonnet-4-5" → ["anthropic", "claude-sonnet-4-5"] (default)
|
|
2330
|
+
*/
|
|
2331
|
+
private static _parseModel;
|
|
2332
|
+
/**
|
|
2333
|
+
* Map an Anthropic model id (e.g. "claude-sonnet-4-5-20250929") to the OpenRouter slug
|
|
2334
|
+
* (e.g. "claude-sonnet-4.5"). OpenRouter slugs drop the date suffix and use dotted versions.
|
|
2335
|
+
*/
|
|
2336
|
+
private static _toOpenRouterSlug;
|
|
2337
|
+
/**
|
|
2338
|
+
* Per-provider proxy base URL. Returns `${SUPERATOM_LLM_PROXY_URL}/<provider>`
|
|
2339
|
+
* when our Cloudflare LLM proxy is configured, else undefined (→ talk to the
|
|
2340
|
+
* provider directly, legacy behaviour). An explicit options.baseURL (e.g.
|
|
2341
|
+
* OpenRouter) always wins and is never overridden. See backend/docs/llm-proxy.md.
|
|
2342
|
+
*/
|
|
2343
|
+
private static _proxyBaseURL;
|
|
2344
|
+
private static _openrouterOptions;
|
|
2345
|
+
private static _isRetryableProviderError;
|
|
2346
|
+
private static _withOpenrouterRetry;
|
|
2347
|
+
private static _openrouterText;
|
|
2348
|
+
private static _openrouterStream;
|
|
2349
|
+
private static _openrouterStreamWithTools;
|
|
2350
|
+
/**
|
|
2351
|
+
* Build an Anthropic client. Routes through our Cloudflare LLM proxy when
|
|
2352
|
+
* SUPERATOM_LLM_PROXY_URL is set (each client ships a per-client proxy key as
|
|
2353
|
+
* ANTHROPIC_API_KEY and never holds the real key); otherwise talks to
|
|
2354
|
+
* api.anthropic.com directly. See backend/docs/llm-proxy.md.
|
|
2355
|
+
*/
|
|
2356
|
+
private static _anthropicClient;
|
|
2357
|
+
/** True when OpenRouter is configured as a fail-open fallback for Claude. */
|
|
2358
|
+
private static _openrouterAvailable;
|
|
2359
|
+
/** Remap an Anthropic model id to the OpenRouter model path for fail-open. */
|
|
2360
|
+
private static _anthropicFallbackModel;
|
|
2361
|
+
private static _anthropicText;
|
|
2362
|
+
private static _anthropicStream;
|
|
2363
|
+
private static _anthropicStreamWithTools;
|
|
2364
|
+
private static _groqText;
|
|
2365
|
+
private static _groqStream;
|
|
2366
|
+
/**
|
|
2367
|
+
* Gemini request options carrying the proxy base URL, or undefined → talk to
|
|
2368
|
+
* generativelanguage.googleapis.com directly. The Google SDK takes baseUrl as a
|
|
2369
|
+
* per-model request option, not a constructor arg. See backend/docs/llm-proxy.md.
|
|
2370
|
+
*/
|
|
2371
|
+
private static _geminiRequestOptions;
|
|
2372
|
+
private static _geminiText;
|
|
2373
|
+
private static _geminiStream;
|
|
2374
|
+
/**
|
|
2375
|
+
* Recursively strip unsupported JSON Schema properties for Gemini
|
|
2376
|
+
* Gemini doesn't support: additionalProperties, $schema, etc.
|
|
2377
|
+
*/
|
|
2378
|
+
private static _cleanSchemaForGemini;
|
|
2379
|
+
private static _geminiStreamWithTools;
|
|
2380
|
+
/** True for Anthropic/Claude model ids — gates OpenRouter prompt caching. */
|
|
2381
|
+
private static _isClaudeModel;
|
|
2382
|
+
/**
|
|
2383
|
+
* Build the OpenAI-wire system message. For OpenRouter + Claude
|
|
2384
|
+
* (cacheClaude=true) it emits content parts carrying Anthropic
|
|
2385
|
+
* `cache_control` breakpoints (preserving any the caller set, else marking
|
|
2386
|
+
* the last block), so OpenRouter forwards them to Anthropic for prompt
|
|
2387
|
+
* caching. Otherwise it returns a plain flattened string — unchanged for
|
|
2388
|
+
* direct OpenAI/Groq.
|
|
2389
|
+
*/
|
|
2390
|
+
private static _openaiSystemMessage;
|
|
2391
|
+
/**
|
|
2392
|
+
* Split an OpenAI-wire usage object. `prompt_tokens` INCLUDES cached tokens,
|
|
2393
|
+
* so we subtract them out (Anthropic-style: input excludes cache reads) and
|
|
2394
|
+
* report cached separately — this makes calculateCost price cache reads at
|
|
2395
|
+
* the discounted rate and reflects OpenRouter prompt-cache savings in logs.
|
|
2396
|
+
*/
|
|
2397
|
+
private static _openaiUsage;
|
|
2398
|
+
private static _openaiText;
|
|
2399
|
+
private static _openaiStream;
|
|
2400
|
+
/** Map the Anthropic-style firstIterationToolChoice to OpenAI's tool_choice. */
|
|
2401
|
+
private static _openaiToolChoice;
|
|
2402
|
+
private static _openaiStreamWithTools;
|
|
2403
|
+
/**
|
|
2404
|
+
* Parse JSON string, handling markdown code blocks and surrounding text
|
|
2405
|
+
* Enhanced version with jsonrepair to handle malformed JSON from LLMs
|
|
2406
|
+
* @param text - Text that may contain JSON wrapped in ```json...``` or with surrounding text
|
|
2407
|
+
* @returns Parsed JSON object or array
|
|
2408
|
+
*/
|
|
2409
|
+
private static _parseJSON;
|
|
2410
|
+
}
|
|
2411
|
+
|
|
2412
|
+
interface CapturedLog {
|
|
2413
|
+
timestamp: number;
|
|
2414
|
+
level: 'info' | 'error' | 'warn' | 'debug';
|
|
2415
|
+
message: string;
|
|
2416
|
+
type?: 'explanation' | 'query' | 'general';
|
|
2417
|
+
data?: Record<string, any>;
|
|
2418
|
+
}
|
|
2419
|
+
/**
|
|
2420
|
+
* UILogCollector captures logs during user prompt processing
|
|
2421
|
+
* and sends them to runtime via ui_logs message with uiBlockId as the message id
|
|
2422
|
+
* Logs are sent in real-time for streaming effect in the UI
|
|
2423
|
+
* Respects the global log level configuration
|
|
2424
|
+
*/
|
|
2425
|
+
declare class UILogCollector {
|
|
2426
|
+
private logs;
|
|
2427
|
+
private uiBlockId;
|
|
2428
|
+
private clientId;
|
|
2429
|
+
private sendMessage;
|
|
2430
|
+
private currentLogLevel;
|
|
2431
|
+
constructor(clientId: string, sendMessage: (message: Message) => void, uiBlockId?: string);
|
|
2432
|
+
/**
|
|
2433
|
+
* Check if logging is enabled (uiBlockId is provided)
|
|
2434
|
+
*/
|
|
2435
|
+
isEnabled(): boolean;
|
|
2436
|
+
/**
|
|
2437
|
+
* Check if a message should be logged based on current log level
|
|
2438
|
+
*/
|
|
2439
|
+
private shouldLog;
|
|
2440
|
+
/**
|
|
2441
|
+
* Add a log entry with timestamp and immediately send to runtime
|
|
2442
|
+
* Only logs that pass the log level filter are captured and sent
|
|
2443
|
+
*/
|
|
2444
|
+
private addLog;
|
|
2445
|
+
/**
|
|
2446
|
+
* Send a single log to runtime immediately
|
|
2447
|
+
*/
|
|
2448
|
+
private sendLogImmediately;
|
|
2449
|
+
/**
|
|
2450
|
+
* Log info message
|
|
2451
|
+
*/
|
|
2452
|
+
info(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
|
|
2453
|
+
/**
|
|
2454
|
+
* Log error message
|
|
2455
|
+
*/
|
|
2456
|
+
error(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
|
|
2457
|
+
/**
|
|
2458
|
+
* Log warning message
|
|
2459
|
+
*/
|
|
2460
|
+
warn(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
|
|
2461
|
+
/**
|
|
2462
|
+
* Log debug message
|
|
2463
|
+
*/
|
|
2464
|
+
debug(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
|
|
2465
|
+
/**
|
|
2466
|
+
* Log LLM explanation with typed metadata
|
|
2467
|
+
*/
|
|
2468
|
+
logExplanation(message: string, explanation: string, data?: Record<string, any>): void;
|
|
2469
|
+
/**
|
|
2470
|
+
* Log generated query with typed metadata
|
|
2471
|
+
*/
|
|
2472
|
+
logQuery(message: string, query: string, data?: Record<string, any>): void;
|
|
2473
|
+
/**
|
|
2474
|
+
* Send all collected logs at once (optional, for final summary)
|
|
2475
|
+
*/
|
|
2476
|
+
sendAllLogs(): void;
|
|
2477
|
+
/**
|
|
2478
|
+
* Get all collected logs
|
|
2479
|
+
*/
|
|
2480
|
+
getLogs(): CapturedLog[];
|
|
2481
|
+
/**
|
|
2482
|
+
* Clear all logs
|
|
2483
|
+
*/
|
|
2484
|
+
clearLogs(): void;
|
|
2485
|
+
/**
|
|
2486
|
+
* Set uiBlockId (in case it's provided later)
|
|
2487
|
+
*/
|
|
2488
|
+
setUIBlockId(uiBlockId: string): void;
|
|
2489
|
+
}
|
|
2490
|
+
|
|
2491
|
+
/**
|
|
2492
|
+
* UIBlock represents a single user and assistant message block in a thread
|
|
2493
|
+
* Contains user question, component metadata, component data, text response, and available actions
|
|
2494
|
+
*/
|
|
1477
2495
|
declare class UIBlock {
|
|
1478
2496
|
private id;
|
|
1479
2497
|
private userQuestion;
|
|
@@ -1652,12 +2670,20 @@ declare class Thread {
|
|
|
1652
2670
|
|
|
1653
2671
|
/**
|
|
1654
2672
|
* ThreadManager manages all threads globally
|
|
1655
|
-
* Provides methods to create, retrieve, and delete threads
|
|
2673
|
+
* Provides methods to create, retrieve, and delete threads.
|
|
2674
|
+
* Includes automatic cleanup to prevent unbounded memory growth.
|
|
1656
2675
|
*/
|
|
1657
2676
|
declare class ThreadManager {
|
|
1658
2677
|
private static instance;
|
|
1659
2678
|
private threads;
|
|
2679
|
+
private cleanupInterval;
|
|
2680
|
+
private readonly threadTtlMs;
|
|
1660
2681
|
private constructor();
|
|
2682
|
+
/**
|
|
2683
|
+
* Periodically remove threads older than 7 days.
|
|
2684
|
+
* Runs every hour to avoid frequent iteration over the map.
|
|
2685
|
+
*/
|
|
2686
|
+
private startCleanup;
|
|
1661
2687
|
/**
|
|
1662
2688
|
* Get singleton instance of ThreadManager
|
|
1663
2689
|
*/
|
|
@@ -2112,126 +3138,34 @@ declare class QueryExecutionService {
|
|
|
2112
3138
|
* @returns Object with result data and cache key
|
|
2113
3139
|
*/
|
|
2114
3140
|
executeQuery(query: any, collections: any): Promise<{
|
|
2115
|
-
result: any;
|
|
2116
|
-
cacheKey: string;
|
|
2117
|
-
}>;
|
|
2118
|
-
/**
|
|
2119
|
-
* Request the LLM to fix a failed SQL query
|
|
2120
|
-
* @param failedQuery - The query that failed execution
|
|
2121
|
-
* @param errorMessage - The error message from the failed execution
|
|
2122
|
-
* @param componentContext - Context about the component
|
|
2123
|
-
* @param apiKey - Optional API key
|
|
2124
|
-
* @returns Fixed query string
|
|
2125
|
-
*/
|
|
2126
|
-
requestQueryFix(failedQuery: string, errorMessage: string, componentContext: ComponentContext, apiKey?: string): Promise<string>;
|
|
2127
|
-
/**
|
|
2128
|
-
* Validate a single component's query with retry logic
|
|
2129
|
-
* @param component - The component to validate
|
|
2130
|
-
* @param collections - Collections object containing database execute function
|
|
2131
|
-
* @param apiKey - Optional API key for LLM calls
|
|
2132
|
-
* @returns Validation result with component, query key, and result
|
|
2133
|
-
*/
|
|
2134
|
-
validateSingleQuery(component: Component, collections: any, apiKey?: string): Promise<QueryValidationResult>;
|
|
2135
|
-
/**
|
|
2136
|
-
* Validate multiple component queries in parallel
|
|
2137
|
-
* @param components - Array of components with potential queries
|
|
2138
|
-
* @param collections - Collections object containing database execute function
|
|
2139
|
-
* @param apiKey - Optional API key for LLM calls
|
|
2140
|
-
* @returns Object with validated components and query results map
|
|
2141
|
-
*/
|
|
2142
|
-
validateComponentQueries(components: Component[], collections: any, apiKey?: string): Promise<BatchValidationResult>;
|
|
2143
|
-
}
|
|
2144
|
-
|
|
2145
|
-
/**
|
|
2146
|
-
* StreamBuffer - Buffered streaming utility for smoother text delivery
|
|
2147
|
-
* Batches small chunks together and flushes at regular intervals
|
|
2148
|
-
*/
|
|
2149
|
-
type StreamCallback = (chunk: string) => void;
|
|
2150
|
-
/**
|
|
2151
|
-
* StreamBuffer class for managing buffered streaming output
|
|
2152
|
-
* Provides smooth text delivery by batching small chunks
|
|
2153
|
-
*/
|
|
2154
|
-
declare class StreamBuffer {
|
|
2155
|
-
private buffer;
|
|
2156
|
-
private flushTimer;
|
|
2157
|
-
private callback;
|
|
2158
|
-
private fullText;
|
|
2159
|
-
constructor(callback?: StreamCallback);
|
|
2160
|
-
/**
|
|
2161
|
-
* Check if the buffer has a callback configured
|
|
2162
|
-
*/
|
|
2163
|
-
hasCallback(): boolean;
|
|
2164
|
-
/**
|
|
2165
|
-
* Get all text that has been written (including already flushed)
|
|
2166
|
-
*/
|
|
2167
|
-
getFullText(): string;
|
|
2168
|
-
/**
|
|
2169
|
-
* Write a chunk to the buffer
|
|
2170
|
-
* Large chunks or chunks with newlines are flushed immediately
|
|
2171
|
-
* Small chunks are batched and flushed after a short interval
|
|
2172
|
-
*
|
|
2173
|
-
* @param chunk - Text chunk to write
|
|
2174
|
-
*/
|
|
2175
|
-
write(chunk: string): void;
|
|
3141
|
+
result: any;
|
|
3142
|
+
cacheKey: string;
|
|
3143
|
+
}>;
|
|
2176
3144
|
/**
|
|
2177
|
-
*
|
|
2178
|
-
*
|
|
3145
|
+
* Request the LLM to fix a failed SQL query
|
|
3146
|
+
* @param failedQuery - The query that failed execution
|
|
3147
|
+
* @param errorMessage - The error message from the failed execution
|
|
3148
|
+
* @param componentContext - Context about the component
|
|
3149
|
+
* @param apiKey - Optional API key
|
|
3150
|
+
* @returns Fixed query string
|
|
2179
3151
|
*/
|
|
2180
|
-
|
|
3152
|
+
requestQueryFix(failedQuery: string, errorMessage: string, componentContext: ComponentContext, apiKey?: string): Promise<string>;
|
|
2181
3153
|
/**
|
|
2182
|
-
*
|
|
3154
|
+
* Validate a single component's query with retry logic
|
|
3155
|
+
* @param component - The component to validate
|
|
3156
|
+
* @param collections - Collections object containing database execute function
|
|
3157
|
+
* @param apiKey - Optional API key for LLM calls
|
|
3158
|
+
* @returns Validation result with component, query key, and result
|
|
2183
3159
|
*/
|
|
2184
|
-
|
|
3160
|
+
validateSingleQuery(component: Component, collections: any, apiKey?: string): Promise<QueryValidationResult>;
|
|
2185
3161
|
/**
|
|
2186
|
-
*
|
|
2187
|
-
*
|
|
3162
|
+
* Validate multiple component queries in parallel
|
|
3163
|
+
* @param components - Array of components with potential queries
|
|
3164
|
+
* @param collections - Collections object containing database execute function
|
|
3165
|
+
* @param apiKey - Optional API key for LLM calls
|
|
3166
|
+
* @returns Object with validated components and query results map
|
|
2188
3167
|
*/
|
|
2189
|
-
|
|
2190
|
-
}
|
|
2191
|
-
|
|
2192
|
-
/**
|
|
2193
|
-
* ToolExecutorService - Handles execution of SQL queries and external tools
|
|
2194
|
-
* Extracted from BaseLLM.generateTextResponse for better separation of concerns
|
|
2195
|
-
*/
|
|
2196
|
-
|
|
2197
|
-
/**
|
|
2198
|
-
* External tool definition
|
|
2199
|
-
*/
|
|
2200
|
-
interface ExternalTool {
|
|
2201
|
-
id: string;
|
|
2202
|
-
name: string;
|
|
2203
|
-
description?: string;
|
|
2204
|
-
/** Tool type: "source" = routed through SourceAgent, "direct" = called directly by MainAgent */
|
|
2205
|
-
toolType?: 'source' | 'direct';
|
|
2206
|
-
/** Full untruncated schema for source agent (all columns visible) */
|
|
2207
|
-
fullSchema?: string;
|
|
2208
|
-
/** Schema size tier: small (≤50 tables), medium (51-200), large (201-500), very_large (500+) */
|
|
2209
|
-
schemaTier?: string;
|
|
2210
|
-
/** Schema search function for very_large tier — keyword search over entities */
|
|
2211
|
-
schemaSearchFn?: (keywords: string[]) => string;
|
|
2212
|
-
fn: (input: any) => Promise<any>;
|
|
2213
|
-
limit?: number;
|
|
2214
|
-
outputSchema?: any;
|
|
2215
|
-
executionType?: 'immediate' | 'deferred';
|
|
2216
|
-
userProvidedData?: any;
|
|
2217
|
-
params?: Record<string, any>;
|
|
2218
|
-
}
|
|
2219
|
-
/**
|
|
2220
|
-
* Executed tool tracking info
|
|
2221
|
-
*/
|
|
2222
|
-
interface ExecutedToolInfo {
|
|
2223
|
-
id: string;
|
|
2224
|
-
name: string;
|
|
2225
|
-
params: any;
|
|
2226
|
-
result: {
|
|
2227
|
-
_totalRecords: number;
|
|
2228
|
-
_recordsShown: number;
|
|
2229
|
-
_metadata?: any;
|
|
2230
|
-
_sampleData: any[];
|
|
2231
|
-
};
|
|
2232
|
-
outputSchema?: any;
|
|
2233
|
-
sourceSchema?: string;
|
|
2234
|
-
sourceType?: string;
|
|
3168
|
+
validateComponentQueries(components: Component[], collections: any, apiKey?: string): Promise<BatchValidationResult>;
|
|
2235
3169
|
}
|
|
2236
3170
|
|
|
2237
3171
|
/**
|
|
@@ -2389,7 +3323,7 @@ declare abstract class BaseLLM {
|
|
|
2389
3323
|
* This helps provide intelligent suggestions for follow-up queries
|
|
2390
3324
|
* For general/conversational questions without components, pass textResponse instead
|
|
2391
3325
|
*/
|
|
2392
|
-
generateNextQuestions(originalUserPrompt: string, component?: Component | null, componentData?: Record<string, unknown>, apiKey?: string, conversationHistory?: string, textResponse?: string): Promise<string[]>;
|
|
3326
|
+
generateNextQuestions(originalUserPrompt: string, component?: Component | null, componentData?: Record<string, unknown>, apiKey?: string, conversationHistory?: string, textResponse?: string, signal?: AbortSignal): Promise<string[]>;
|
|
2393
3327
|
}
|
|
2394
3328
|
|
|
2395
3329
|
interface AnthropicLLMConfig extends BaseLLMConfig {
|
|
@@ -2449,18 +3383,30 @@ declare class OpenAILLM extends BaseLLM {
|
|
|
2449
3383
|
declare const openaiLLM: OpenAILLM;
|
|
2450
3384
|
|
|
2451
3385
|
/**
|
|
2452
|
-
* Query Cache
|
|
2453
|
-
*
|
|
3386
|
+
* Query Cache — Two mechanisms:
|
|
3387
|
+
*
|
|
3388
|
+
* 1. `cache` (query string → result data) — TTL-based with max size, for avoiding re-execution
|
|
3389
|
+
* of recently validated queries. True LRU eviction: reads bubble entries to the back via
|
|
3390
|
+
* delete+re-set so the oldest *unused* entry is evicted, not the oldest *inserted*.
|
|
3391
|
+
*
|
|
3392
|
+
* 2. Encrypted queryId tokens — SQL is encrypted into the queryId itself (self-contained).
|
|
3393
|
+
* No server-side storage needed for SQL mappings. The token is decrypted on each request.
|
|
3394
|
+
* This eliminates the unbounded queryIdCache that previously grew forever and caused
|
|
3395
|
+
* memory bloat (hundreds of MBs after thousands of queries).
|
|
3396
|
+
*
|
|
3397
|
+
* Result data can still be cached temporarily via the data cache (mechanism 1).
|
|
2454
3398
|
*/
|
|
2455
3399
|
declare class QueryCache {
|
|
2456
3400
|
private cache;
|
|
2457
|
-
private queryIdCache;
|
|
2458
3401
|
private ttlMs;
|
|
3402
|
+
private maxCacheSize;
|
|
2459
3403
|
private cleanupInterval;
|
|
3404
|
+
private readonly algorithm;
|
|
3405
|
+
private encryptionKey;
|
|
2460
3406
|
constructor();
|
|
2461
3407
|
/**
|
|
2462
3408
|
* Set the cache TTL (Time To Live)
|
|
2463
|
-
* @param minutes - TTL in minutes (default:
|
|
3409
|
+
* @param minutes - TTL in minutes (default: 10)
|
|
2464
3410
|
*/
|
|
2465
3411
|
setTTL(minutes: number): void;
|
|
2466
3412
|
/**
|
|
@@ -2468,12 +3414,16 @@ declare class QueryCache {
|
|
|
2468
3414
|
*/
|
|
2469
3415
|
getTTL(): number;
|
|
2470
3416
|
/**
|
|
2471
|
-
* Store query result in cache
|
|
2472
|
-
*
|
|
3417
|
+
* Store query result in data cache.
|
|
3418
|
+
* If the key already exists, it's removed first so the re-insert places it
|
|
3419
|
+
* at the back of the iteration order (LRU). Eviction only fires when adding
|
|
3420
|
+
* a genuinely new key past the size limit.
|
|
2473
3421
|
*/
|
|
2474
3422
|
set(query: string, data: any): void;
|
|
2475
3423
|
/**
|
|
2476
|
-
* Get cached result if exists and not expired
|
|
3424
|
+
* Get cached result if exists and not expired.
|
|
3425
|
+
* On hit, re-inserts the entry so it moves to the back of the Map's
|
|
3426
|
+
* iteration order — turning FIFO eviction into true LRU.
|
|
2477
3427
|
*/
|
|
2478
3428
|
get(query: string): any | null;
|
|
2479
3429
|
/**
|
|
@@ -2493,30 +3443,37 @@ declare class QueryCache {
|
|
|
2493
3443
|
*/
|
|
2494
3444
|
getStats(): {
|
|
2495
3445
|
size: number;
|
|
3446
|
+
queryIdCount: number;
|
|
2496
3447
|
oldestEntryAge: number | null;
|
|
2497
3448
|
};
|
|
2498
3449
|
/**
|
|
2499
|
-
* Start periodic cleanup of expired entries
|
|
3450
|
+
* Start periodic cleanup of expired data cache entries.
|
|
2500
3451
|
*/
|
|
2501
3452
|
private startCleanup;
|
|
2502
3453
|
/**
|
|
2503
|
-
*
|
|
3454
|
+
* Encrypt a payload into a self-contained token.
|
|
3455
|
+
*/
|
|
3456
|
+
private encrypt;
|
|
3457
|
+
/**
|
|
3458
|
+
* Decrypt a token back to the original payload.
|
|
2504
3459
|
*/
|
|
2505
|
-
private
|
|
3460
|
+
private decrypt;
|
|
2506
3461
|
/**
|
|
2507
|
-
* Store a query by
|
|
2508
|
-
* The
|
|
3462
|
+
* Store a query by generating an encrypted token as queryId.
|
|
3463
|
+
* The SQL is encrypted INTO the token — nothing stored in memory.
|
|
3464
|
+
* If data is provided, it's cached temporarily in the data cache.
|
|
2509
3465
|
*/
|
|
2510
3466
|
storeQuery(query: any, data?: any): string;
|
|
2511
3467
|
/**
|
|
2512
|
-
* Get a stored query by its
|
|
3468
|
+
* Get a stored query by decrypting its token.
|
|
3469
|
+
* Returns the SQL + any cached result data.
|
|
2513
3470
|
*/
|
|
2514
3471
|
getQuery(queryId: string): {
|
|
2515
3472
|
query: any;
|
|
2516
3473
|
data: any;
|
|
2517
3474
|
} | null;
|
|
2518
3475
|
/**
|
|
2519
|
-
* Update cached data for a queryId
|
|
3476
|
+
* Update cached data for a queryId token
|
|
2520
3477
|
*/
|
|
2521
3478
|
setQueryData(queryId: string, data: any): void;
|
|
2522
3479
|
/**
|
|
@@ -2573,175 +3530,153 @@ declare class DashboardConversationHistory {
|
|
|
2573
3530
|
declare const dashboardConversationHistory: DashboardConversationHistory;
|
|
2574
3531
|
|
|
2575
3532
|
/**
|
|
2576
|
-
*
|
|
3533
|
+
* Whole-dashboard generation via Pi, a terminal coding agent — as opposed to
|
|
3534
|
+
* DASH_COMP_REQ's single-widget-at-a-time flow. Runs Pi in-process via its
|
|
3535
|
+
* SDK (createAgentSession), not as a subprocess: no shell, no argument
|
|
3536
|
+
* quoting, no stdin/stdout piping, none of the Windows-specific subprocess
|
|
3537
|
+
* issues that came with spawning the `pi` CLI directly.
|
|
2577
3538
|
*
|
|
2578
|
-
*
|
|
2579
|
-
*
|
|
2580
|
-
*
|
|
3539
|
+
* Called from sdk-nodejs/src/dashboardAgent/index.ts (DASHBOARD_AGENT_REQ),
|
|
3540
|
+
* which owns the generic streaming/abort machinery (mirrors USER_PROMPT_REQ)
|
|
3541
|
+
* and passes `signal`/`onProgress` alongside the normal params — this stays
|
|
3542
|
+
* within CollectionHandler's loose (params) => Promise<result> typing, no
|
|
3543
|
+
* change needed to that shared type.
|
|
2581
3544
|
*
|
|
2582
|
-
*
|
|
2583
|
-
*
|
|
2584
|
-
|
|
2585
|
-
|
|
2586
|
-
|
|
2587
|
-
*
|
|
2588
|
-
*
|
|
2589
|
-
|
|
2590
|
-
|
|
2591
|
-
|
|
2592
|
-
|
|
2593
|
-
|
|
2594
|
-
|
|
2595
|
-
|
|
2596
|
-
|
|
2597
|
-
|
|
2598
|
-
|
|
2599
|
-
*
|
|
2600
|
-
*
|
|
2601
|
-
|
|
2602
|
-
|
|
2603
|
-
|
|
2604
|
-
|
|
2605
|
-
|
|
2606
|
-
|
|
2607
|
-
|
|
2608
|
-
|
|
2609
|
-
|
|
2610
|
-
|
|
2611
|
-
|
|
2612
|
-
|
|
2613
|
-
|
|
2614
|
-
toolId: string;
|
|
2615
|
-
}
|
|
2616
|
-
/**
|
|
2617
|
-
* What a source agent returns after querying its data source.
|
|
2618
|
-
* The main agent uses this to analyze and compose the final response.
|
|
2619
|
-
*/
|
|
2620
|
-
interface SourceAgentResult {
|
|
2621
|
-
/** Source ID */
|
|
2622
|
-
sourceId: string;
|
|
2623
|
-
/** Source name */
|
|
2624
|
-
sourceName: string;
|
|
2625
|
-
/** Whether the query succeeded */
|
|
2626
|
-
success: boolean;
|
|
2627
|
-
/** Result data rows */
|
|
2628
|
-
data: any[];
|
|
2629
|
-
/** Metadata about the query execution */
|
|
2630
|
-
metadata: SourceAgentMetadata;
|
|
2631
|
-
/** Tool execution info (reused for component generation) */
|
|
2632
|
-
executedTool: ExecutedToolInfo;
|
|
2633
|
-
/** Error message if failed */
|
|
2634
|
-
error?: string;
|
|
2635
|
-
}
|
|
2636
|
-
interface SourceAgentMetadata {
|
|
2637
|
-
/** Total rows that matched the query (before limit) */
|
|
2638
|
-
totalRowsMatched: number;
|
|
2639
|
-
/** Rows actually returned (after limit) */
|
|
2640
|
-
rowsReturned: number;
|
|
2641
|
-
/** Whether the result was truncated by the row limit */
|
|
2642
|
-
isLimited: boolean;
|
|
2643
|
-
/** The query/params that were executed */
|
|
2644
|
-
queryExecuted?: string;
|
|
2645
|
-
/** Execution time in milliseconds */
|
|
2646
|
-
executionTimeMs: number;
|
|
2647
|
-
}
|
|
2648
|
-
/**
|
|
2649
|
-
* The complete response from the multi-agent system.
|
|
2650
|
-
* Contains everything needed for text display + component generation.
|
|
2651
|
-
*/
|
|
2652
|
-
interface AgentResponse {
|
|
2653
|
-
/** Generated text response (analysis of the data) */
|
|
2654
|
-
text: string;
|
|
2655
|
-
/** All executed tools across all source agents (for component generation) */
|
|
2656
|
-
executedTools: ExecutedToolInfo[];
|
|
2657
|
-
/** Individual results from each source agent */
|
|
2658
|
-
sourceResults: SourceAgentResult[];
|
|
2659
|
-
}
|
|
2660
|
-
/**
|
|
2661
|
-
* Configuration for the multi-agent system.
|
|
2662
|
-
* Controls limits, models, and behavior.
|
|
3545
|
+
* This mechanism is generic and reusable across any deployment. What's
|
|
3546
|
+
* genuinely project-specific — where AGENTS.md lives, which model to use —
|
|
3547
|
+
* is supplied via `DashboardAgentCollectionConfig`, with defaults sensible
|
|
3548
|
+
* enough that most callers don't need to override them (see below). The
|
|
3549
|
+
* data-source tool list and the dashboard's current state both come from
|
|
3550
|
+
* things sdk-nodejs already exposes generically: `sdk.getTools()` (whatever
|
|
3551
|
+
* this deployment registered via `sdk.setTools()`) and `sdk.callCollection
|
|
3552
|
+
* ('dashboards', 'query', ...)` (whatever this deployment already registered
|
|
3553
|
+
* under that name/shape) — no per-deployment callback needed for either.
|
|
3554
|
+
* Same convention on the way out: after a successful run, the prompt and the
|
|
3555
|
+
* full response text are handed to `sdk.callCollection('dashboard-agent-
|
|
3556
|
+
* conversations', 'create', ...)` if this deployment has registered one —
|
|
3557
|
+
* skipped silently otherwise, since conversation history is optional.
|
|
3558
|
+
*
|
|
3559
|
+
* Pi verifies every query against the live database itself (via whatever
|
|
3560
|
+
* local tool-execution bridge the deployment exposes, e.g. an HTTP bridge
|
|
3561
|
+
* on localhost), but does NOT persist the result itself — it writes the
|
|
3562
|
+
* finished DSL to an absolute path inside `runtimeDir`, told to it explicitly
|
|
3563
|
+
* in the prompt, and stops there. This handler reads that file after the run
|
|
3564
|
+
* finishes and returns its content as `dashboard` in the result. The caller
|
|
3565
|
+
* (frontend) is the one that actually saves it, via whatever authenticated
|
|
3566
|
+
* create/update path any other dashboard edit goes through — Pi has no user
|
|
3567
|
+
* session/auth context of its own, so persistence shouldn't happen from
|
|
3568
|
+
* inside it.
|
|
3569
|
+
*
|
|
3570
|
+
* Session persistence: the FIRST call for a dashboardId pays the full cost
|
|
3571
|
+
* (explore KB, discover schema, plan, verify, build). Every call after that
|
|
3572
|
+
* resumes the same session file (SessionManager.open) so Pi has everything
|
|
3573
|
+
* it already learned — it only needs to reason about the new, smaller ask,
|
|
3574
|
+
* not rediscover the whole dashboard from scratch. The session's file path
|
|
3575
|
+
* (AgentSession.sessionFile) is captured right after creation and persisted
|
|
3576
|
+
* in a small local file, keyed by dashboardId, inside `runtimeDir`.
|
|
2663
3577
|
*/
|
|
2664
|
-
interface
|
|
2665
|
-
/**
|
|
2666
|
-
|
|
2667
|
-
|
|
2668
|
-
|
|
2669
|
-
|
|
2670
|
-
|
|
2671
|
-
|
|
2672
|
-
|
|
2673
|
-
|
|
2674
|
-
|
|
2675
|
-
/**
|
|
2676
|
-
|
|
2677
|
-
|
|
2678
|
-
|
|
2679
|
-
|
|
2680
|
-
|
|
3578
|
+
interface DashboardAgentCollectionConfig {
|
|
3579
|
+
/**
|
|
3580
|
+
* Working directory Pi runs from — must contain AGENTS.md. This is a
|
|
3581
|
+
* version-controlled prompt file, so `cwd` is expected to live somewhere
|
|
3582
|
+
* like a `.prompts/` folder alongside the deployment's other prompts.
|
|
3583
|
+
* Default: `<process.cwd()>/.prompts/dashboard-agent` — the same
|
|
3584
|
+
* process.cwd()-based convention PromptLoader already uses for the main
|
|
3585
|
+
* agent's prompts, which needs no explicit override in the common case
|
|
3586
|
+
* (the backend process's own cwd already is its project root).
|
|
3587
|
+
*/
|
|
3588
|
+
cwd?: string;
|
|
3589
|
+
/**
|
|
3590
|
+
* Where drafts/, the session-id map, and dashboard.log get written —
|
|
3591
|
+
* separate from `cwd` deliberately, so this deployment's runtime state
|
|
3592
|
+
* (regenerated per session, safe to gitignore) doesn't sit inside the
|
|
3593
|
+
* same folder as the version-controlled AGENTS.md prompt.
|
|
3594
|
+
* Default: `<process.cwd()>/.pi-dashboard-agent-runtime`.
|
|
3595
|
+
*/
|
|
3596
|
+
runtimeDir?: string;
|
|
3597
|
+
/** Model provider (default: process.env.PI_AGENT_PROVIDER || 'openrouter'). */
|
|
3598
|
+
provider?: string;
|
|
3599
|
+
/** Model id (default: process.env.PI_AGENT_MODEL || 'anthropic/claude-sonnet-4.5'). */
|
|
3600
|
+
model?: string;
|
|
3601
|
+
/**
|
|
3602
|
+
* true (default): every request starts a brand-new pi session, with the 2
|
|
3603
|
+
* most recent prior responses (if any) injected into the prompt as
|
|
3604
|
+
* context — bounded cost per request, but pi re-explores schema/KB facts
|
|
3605
|
+
* it already verified in an earlier turn on this same dashboard.
|
|
3606
|
+
* false: resumes the same session file across requests on a given
|
|
3607
|
+
* dashboard — pi keeps everything it already learned, but context (and
|
|
3608
|
+
* cost) grows unbounded across turns (one observed turn: 2M+ cache-read
|
|
3609
|
+
* tokens after a handful of edits on the same dashboard).
|
|
3610
|
+
* Default: process.env.PI_AGENT_FRESH_SESSION !== 'false'.
|
|
3611
|
+
*/
|
|
3612
|
+
freshSession?: boolean;
|
|
2681
3613
|
}
|
|
2682
|
-
|
|
2683
|
-
* Default agent configuration
|
|
2684
|
-
*/
|
|
2685
|
-
declare const DEFAULT_AGENT_CONFIG: AgentConfig;
|
|
3614
|
+
declare function registerDashboardAgentCollection(sdk: SuperatomSDK, config?: DashboardAgentCollectionConfig): void;
|
|
2686
3615
|
|
|
2687
3616
|
/**
|
|
2688
|
-
*
|
|
3617
|
+
* ScriptMatcher — LLM-Based Script Matching + Parameter Extraction
|
|
2689
3618
|
*
|
|
2690
|
-
*
|
|
2691
|
-
*
|
|
2692
|
-
*
|
|
2693
|
-
* - Direct tools: calls pre-built function tools directly with LLM-provided params
|
|
2694
|
-
* - Re-querying: if data is wrong/incomplete, calls tools again with modified intent
|
|
2695
|
-
* - Analysis: generates final text response from the data
|
|
3619
|
+
* Uses ONE LLM call to:
|
|
3620
|
+
* 1. Pick the best matching script from the library (or "none")
|
|
3621
|
+
* 2. Extract parameter values from the user question
|
|
2696
3622
|
*
|
|
2697
|
-
*
|
|
2698
|
-
* -
|
|
2699
|
-
*
|
|
3623
|
+
* Why LLM over embeddings:
|
|
3624
|
+
* - Embeddings capture topic similarity ("overstock" ≈ "inventory" ≈ "revenue")
|
|
3625
|
+
* but can't distinguish structurally different questions about the same domain
|
|
3626
|
+
* - LLM understands that "overstock by warehouse" needs a different script than
|
|
3627
|
+
* "revenue by warehouse" even though they're semantically close
|
|
3628
|
+
* - One call does both matching AND parameter extraction
|
|
3629
|
+
*
|
|
3630
|
+
* When script library grows past ~50, add an embedding pre-filter
|
|
3631
|
+
* (ChromaDB narrows to top 10 → LLM picks from those 10).
|
|
2700
3632
|
*/
|
|
2701
3633
|
|
|
2702
|
-
declare class
|
|
2703
|
-
private
|
|
2704
|
-
|
|
2705
|
-
private streamBuffer;
|
|
2706
|
-
constructor(externalTools: ExternalTool[], config: AgentConfig, streamBuffer?: StreamBuffer);
|
|
2707
|
-
/**
|
|
2708
|
-
* Handle a user question using the multi-agent system.
|
|
2709
|
-
*
|
|
2710
|
-
* This is ONE LLM.streamWithTools() call. The LLM:
|
|
2711
|
-
* 1. Sees source summaries + direct tool descriptions in system prompt
|
|
2712
|
-
* 2. Decides which tool(s) to call (routing)
|
|
2713
|
-
* 3. Source tools → SourceAgent runs independently → returns data
|
|
2714
|
-
* 4. Direct tools → fn() called directly with LLM params → returns data
|
|
2715
|
-
* 5. Generates final analysis text
|
|
2716
|
-
*/
|
|
2717
|
-
handleQuestion(userPrompt: string, apiKey?: string, conversationHistory?: string, streamCallback?: (chunk: string) => void): Promise<AgentResponse>;
|
|
2718
|
-
/**
|
|
2719
|
-
* Execute a direct tool — call fn() with LLM-provided params, no SourceAgent.
|
|
2720
|
-
*/
|
|
2721
|
-
private handleDirectTool;
|
|
2722
|
-
/**
|
|
2723
|
-
* Build the main agent's system prompt with source summaries and direct tool descriptions.
|
|
2724
|
-
*/
|
|
2725
|
-
private buildSystemPrompt;
|
|
2726
|
-
/**
|
|
2727
|
-
* Build tool definitions for source tools — summary-only descriptions.
|
|
2728
|
-
* The full schema is inside the SourceAgent which runs independently.
|
|
2729
|
-
*/
|
|
2730
|
-
private buildSourceToolDefinitions;
|
|
2731
|
-
/**
|
|
2732
|
-
* Build tool definitions for direct tools — expose their actual params.
|
|
2733
|
-
* These are called directly by the main agent LLM, no SourceAgent.
|
|
2734
|
-
*/
|
|
2735
|
-
private buildDirectToolDefinitions;
|
|
3634
|
+
declare class ScriptMatcher {
|
|
3635
|
+
private store;
|
|
3636
|
+
constructor(store: ScriptStore);
|
|
2736
3637
|
/**
|
|
2737
|
-
*
|
|
3638
|
+
* Find the best matching script for a user question.
|
|
3639
|
+
* Uses ONE LLM call that picks the script AND extracts parameters.
|
|
3640
|
+
* Returns null if no script matches.
|
|
2738
3641
|
*/
|
|
2739
|
-
|
|
3642
|
+
match(userPrompt: string, apiKey?: string, model?: string, signal?: AbortSignal): Promise<ScriptMatch | null>;
|
|
2740
3643
|
/**
|
|
2741
|
-
*
|
|
3644
|
+
* Build the script catalog string for the LLM prompt.
|
|
3645
|
+
* Each script gets: index, ID, name, description, and parameter definitions.
|
|
2742
3646
|
*/
|
|
2743
|
-
|
|
3647
|
+
private buildScriptCatalog;
|
|
3648
|
+
}
|
|
3649
|
+
|
|
3650
|
+
/**
|
|
3651
|
+
* ScriptRunner — Execute scripts in an isolated tsx subprocess.
|
|
3652
|
+
*
|
|
3653
|
+
* The subprocess approach replaces the earlier `new Function()` eval and gives us:
|
|
3654
|
+
* - Real sandbox (separate process, SIGKILL on timeout).
|
|
3655
|
+
* - Real TypeScript (tsx transpiles on the fly).
|
|
3656
|
+
* - npm imports available to scripts (clustering, stats, geo, etc.).
|
|
3657
|
+
*
|
|
3658
|
+
* Protocol: NDJSON over the child's stdin/stdout. See script-ipc.ts + backend/docs/SCRIPT-FLOW-IMPLEMENTATION.md.
|
|
3659
|
+
*/
|
|
3660
|
+
|
|
3661
|
+
interface RunScriptOptions {
|
|
3662
|
+
/** Data sources the script is allowed to query via ctx.query */
|
|
3663
|
+
externalTools: ExternalTool[];
|
|
3664
|
+
/** Optional — for propagating per-query UI progress to the user */
|
|
3665
|
+
streamBuffer?: StreamBuffer;
|
|
3666
|
+
/** Override the wall-clock timeout (default `SCRIPT_TIMEOUT_MS`, 60s). */
|
|
3667
|
+
timeoutMs?: number;
|
|
3668
|
+
/**
|
|
3669
|
+
* Per-turn cancellation signal. When the user hits "Stop" mid-run, the child
|
|
3670
|
+
* process group is SIGKILLed and the run resolves as an aborted failure (the
|
|
3671
|
+
* caller is already unwinding, so the result is discarded).
|
|
3672
|
+
*/
|
|
3673
|
+
signal?: AbortSignal;
|
|
2744
3674
|
}
|
|
3675
|
+
/**
|
|
3676
|
+
* Execute a recipe by spawning a tsx child on the script's .ts file.
|
|
3677
|
+
* `scriptPath` is the absolute path to the saved `.ts` body.
|
|
3678
|
+
*/
|
|
3679
|
+
declare function runScript(recipe: ScriptRecipe, scriptPath: string, params: Record<string, any>, options: RunScriptOptions): Promise<ScriptResult>;
|
|
2745
3680
|
|
|
2746
3681
|
type MessageTypeHandler = (message: IncomingMessage) => void | Promise<void>;
|
|
2747
3682
|
declare class SuperatomSDK {
|
|
@@ -2759,6 +3694,7 @@ declare class SuperatomSDK {
|
|
|
2759
3694
|
private collections;
|
|
2760
3695
|
private components;
|
|
2761
3696
|
private tools;
|
|
3697
|
+
private workflows;
|
|
2762
3698
|
private anthropicApiKey;
|
|
2763
3699
|
private groqApiKey;
|
|
2764
3700
|
private geminiApiKey;
|
|
@@ -2768,6 +3704,7 @@ declare class SuperatomSDK {
|
|
|
2768
3704
|
private modelStrategy;
|
|
2769
3705
|
private mainAgentModel;
|
|
2770
3706
|
private sourceAgentModel;
|
|
3707
|
+
private dashCompModels?;
|
|
2771
3708
|
private conversationSimilarityThreshold;
|
|
2772
3709
|
private userManager;
|
|
2773
3710
|
private dashboardManager;
|
|
@@ -2776,6 +3713,8 @@ declare class SuperatomSDK {
|
|
|
2776
3713
|
private lastPong;
|
|
2777
3714
|
private readonly PING_INTERVAL_MS;
|
|
2778
3715
|
private readonly PONG_TIMEOUT_MS;
|
|
3716
|
+
private pendingOutbox;
|
|
3717
|
+
private readonly MAX_OUTBOX_SIZE;
|
|
2779
3718
|
constructor(config: SuperatomSDKConfig);
|
|
2780
3719
|
/**
|
|
2781
3720
|
* Initialize PromptLoader and load prompts into memory
|
|
@@ -2820,6 +3759,20 @@ declare class SuperatomSDK {
|
|
|
2820
3759
|
* Does NOT throw on closed connections — callers can check the return value if needed.
|
|
2821
3760
|
*/
|
|
2822
3761
|
send(message: Message): boolean;
|
|
3762
|
+
/**
|
|
3763
|
+
* Queue a message that couldn't be delivered because the socket was down,
|
|
3764
|
+
* to be resent once it reconnects. Drops the oldest entry once the bound is
|
|
3765
|
+
* hit — an outage long enough to fill this queue means the oldest queued
|
|
3766
|
+
* responses are for requests the caller has likely already given up on.
|
|
3767
|
+
*/
|
|
3768
|
+
private queuePendingMessage;
|
|
3769
|
+
/**
|
|
3770
|
+
* Resend everything queued while the socket was down, now that it's back
|
|
3771
|
+
* up. Uses this.ws.send() directly (not send()) so a message that fails
|
|
3772
|
+
* again goes back through queuePendingMessage() rather than being silently
|
|
3773
|
+
* dropped a second time.
|
|
3774
|
+
*/
|
|
3775
|
+
private flushPendingOutbox;
|
|
2823
3776
|
/**
|
|
2824
3777
|
* Register a message handler to receive all messages
|
|
2825
3778
|
*/
|
|
@@ -2859,6 +3812,14 @@ declare class SuperatomSDK {
|
|
|
2859
3812
|
*/
|
|
2860
3813
|
private handlePong;
|
|
2861
3814
|
private storeComponents;
|
|
3815
|
+
/**
|
|
3816
|
+
* The live, frontend-registered component catalog (name, type, description,
|
|
3817
|
+
* and full prop schema per component) — the same authoritative source
|
|
3818
|
+
* DASH_COMP_REQ's LLM prompt is built from. Exposed so other integrations
|
|
3819
|
+
* (e.g. the dashboard-agent script bridge) can read real component
|
|
3820
|
+
* contracts instead of maintaining a separate, driftable hand-written copy.
|
|
3821
|
+
*/
|
|
3822
|
+
getComponents(): Component[];
|
|
2862
3823
|
/**
|
|
2863
3824
|
* Set tools for the SDK instance
|
|
2864
3825
|
*/
|
|
@@ -2867,6 +3828,29 @@ declare class SuperatomSDK {
|
|
|
2867
3828
|
* Get the stored tools
|
|
2868
3829
|
*/
|
|
2869
3830
|
getTools(): Tool$1[];
|
|
3831
|
+
/**
|
|
3832
|
+
* Call a registered collection operation in-process — no WebSocket
|
|
3833
|
+
* round-trip, since the caller is already running inside this same SDK
|
|
3834
|
+
* instance. Lets SDK-internal features (e.g. the dashboard agent) reuse
|
|
3835
|
+
* whatever collection a deployment has already registered (e.g.
|
|
3836
|
+
* 'dashboards'.'query') by name/convention, instead of requiring a
|
|
3837
|
+
* separate callback purely to re-expose data a collection already serves.
|
|
3838
|
+
* Throws if the collection or operation isn't registered.
|
|
3839
|
+
*/
|
|
3840
|
+
callCollection<TResult = any>(collectionName: string, operation: string, params?: any): Promise<TResult>;
|
|
3841
|
+
/**
|
|
3842
|
+
* Register workflow components for the SDK instance.
|
|
3843
|
+
*
|
|
3844
|
+
* Workflows are pre-built multi-step UI flows the main agent can pick when
|
|
3845
|
+
* the user's prompt matches a workflow's `whenToUse` trigger. Picking a
|
|
3846
|
+
* workflow short-circuits analysis text + dashboard component generation —
|
|
3847
|
+
* the workflow component is returned directly, with the LLM-extracted props.
|
|
3848
|
+
*/
|
|
3849
|
+
setWorkflows(workflows: WorkflowDescriptor[]): void;
|
|
3850
|
+
/**
|
|
3851
|
+
* Get the registered workflow components.
|
|
3852
|
+
*/
|
|
3853
|
+
getWorkflows(): WorkflowDescriptor[];
|
|
2870
3854
|
/**
|
|
2871
3855
|
* Apply model strategy to all LLM provider singletons
|
|
2872
3856
|
* @param strategy - 'best', 'fast', or 'balanced'
|
|
@@ -2897,4 +3881,4 @@ declare class SuperatomSDK {
|
|
|
2897
3881
|
getConversationSimilarityThreshold(): number;
|
|
2898
3882
|
}
|
|
2899
3883
|
|
|
2900
|
-
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 };
|
|
3884
|
+
export { type Action, type AgentConfig, type AgentResponse, BM25L, type BM25LOptions, type BaseLLMConfig, CONTEXT_CONFIG, type CapturedLog, CleanupService, type CollectionHandler, type CollectionOperation, type DBUIBlock, DEFAULT_AGENT_CONFIG, type DashboardAgentCollectionConfig, type DatabaseType, type HybridSearchOptions, type IncomingMessage, type KbNodesQueryFilters, type KbNodesRequestPayload, LLM, type LLMUsageEntry, type LogLevel, MainAgent, type Message, type ModelStrategy, type OutputField, type RerankedResult, STORAGE_CONFIG, type ScriptComponentSpec, ScriptMatcher, type ScriptParameter, type ScriptRecipe, type ScriptRecipeMetaRow, type ScriptRecipeStore, type ScriptResult, ScriptStore, type ScriptStoreOptions, type SelectedWorkflow, SuperatomSDK, type SuperatomSDKConfig, type TaskType, Thread, ThreadManager, type Tool$1 as Tool, type ToolOutputSchema, UIBlock, UILogCollector, type User, UserManager, type UsersData, type WorkflowDescriptor, anthropicLLM, dashboardConversationHistory, geminiLLM, groqLLM, hybridRerank, llmUsageLogger, logger, normalizeScriptBody, openaiLLM, queryCache, registerDashboardAgentCollection, rerankChromaResults, rerankConversationResults, resolveScriptRecipeStore, runScript, userPromptErrorLogger };
|