@superatomai/sdk-node 0.0.1-s → 0.0.2-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 +1795 -1428
- package/dist/index.d.ts +1795 -1428
- package/dist/index.js +1767 -738
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1782 -758
- package/dist/index.mjs.map +1 -1
- package/dist/userResponse/scripts/script-bootstrap.js +103 -34
- package/dist/userResponse/scripts/script-bootstrap.js.map +1 -1
- package/dist/userResponse/scripts/script-bootstrap.mjs +103 -34
- package/dist/userResponse/scripts/script-bootstrap.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
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,1824 +1320,2163 @@ 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
|
+
}
|
|
1442
|
+
/**
|
|
1443
|
+
* Representation of a data source for the main agent.
|
|
1444
|
+
* Contains entity names WITH column names so the LLM can route accurately.
|
|
1445
|
+
*/
|
|
1446
|
+
interface SourceSummary {
|
|
1447
|
+
/** Source ID (matches tool ID prefix) */
|
|
1467
1448
|
id: string;
|
|
1449
|
+
/** Human-readable source name */
|
|
1468
1450
|
name: string;
|
|
1451
|
+
/** Source type: postgres, excel, rest_api, etc. */
|
|
1469
1452
|
type: string;
|
|
1470
|
-
|
|
1453
|
+
/** Brief description of what data this source contains */
|
|
1454
|
+
description: string;
|
|
1455
|
+
/** Detailed entity info with column names for routing */
|
|
1456
|
+
entityDetails: EntityDetail[];
|
|
1457
|
+
/** The tool ID associated with this source */
|
|
1458
|
+
toolId: string;
|
|
1471
1459
|
}
|
|
1472
|
-
|
|
1473
1460
|
/**
|
|
1474
|
-
*
|
|
1475
|
-
*
|
|
1476
|
-
*/
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1461
|
+
* What a source agent returns after querying its data source.
|
|
1462
|
+
* The main agent uses this to analyze and compose the final response.
|
|
1463
|
+
*/
|
|
1464
|
+
interface SourceAgentResult {
|
|
1465
|
+
/** Source ID */
|
|
1466
|
+
sourceId: string;
|
|
1467
|
+
/** Source name */
|
|
1468
|
+
sourceName: string;
|
|
1469
|
+
/** Whether the query succeeded */
|
|
1470
|
+
success: boolean;
|
|
1471
|
+
/** Result data rows */
|
|
1472
|
+
data: any[];
|
|
1473
|
+
/** Metadata about the query execution */
|
|
1474
|
+
metadata: SourceAgentMetadata;
|
|
1475
|
+
/** Tool execution info for the last successful query (backward compat) */
|
|
1476
|
+
executedTool: ExecutedToolInfo;
|
|
1477
|
+
/** All successful tool executions (primary + follow-up queries) */
|
|
1478
|
+
allExecutedTools?: ExecutedToolInfo[];
|
|
1479
|
+
/** Error message if failed */
|
|
1480
|
+
error?: string;
|
|
1481
|
+
}
|
|
1482
|
+
interface SourceAgentMetadata {
|
|
1483
|
+
/** Total rows that matched the query (before limit) */
|
|
1484
|
+
totalRowsMatched: number;
|
|
1485
|
+
/** Rows actually returned (after limit) */
|
|
1486
|
+
rowsReturned: number;
|
|
1487
|
+
/** Whether the result was truncated by the row limit */
|
|
1488
|
+
isLimited: boolean;
|
|
1489
|
+
/** The query/params that were executed */
|
|
1490
|
+
queryExecuted?: string;
|
|
1491
|
+
/** Execution time in milliseconds */
|
|
1492
|
+
executionTimeMs: number;
|
|
1493
|
+
}
|
|
1494
|
+
/**
|
|
1495
|
+
* A pre-built, multi-step UI flow registered with the SDK.
|
|
1496
|
+
*
|
|
1497
|
+
* When the main agent decides a user's question matches a workflow's whenToUse
|
|
1498
|
+
* trigger, it picks the workflow instead of running source agents / generating
|
|
1499
|
+
* dashboard components. The LLM extracts the workflow's required props from the
|
|
1500
|
+
* prompt (using `propsSchema` as the tool input_schema) and the SDK returns the
|
|
1501
|
+
* workflow component directly — no analysis text, no chart generation. The
|
|
1502
|
+
* frontend renders the registered workflow component with the LLM-extracted
|
|
1503
|
+
* props.
|
|
1504
|
+
*/
|
|
1505
|
+
interface WorkflowDescriptor {
|
|
1506
|
+
/** Unique workflow id (used as the LLM tool name) */
|
|
1507
|
+
id: string;
|
|
1508
|
+
/** Component name on the frontend (matches the registered React component) */
|
|
1509
|
+
name: string;
|
|
1510
|
+
/** Short human-readable description of what this workflow does */
|
|
1511
|
+
description: string;
|
|
1495
1512
|
/**
|
|
1496
|
-
*
|
|
1513
|
+
* 1–2 sentence trigger condition. The LLM uses this to decide if the
|
|
1514
|
+
* user's prompt matches this workflow. Be specific — e.g.
|
|
1515
|
+
* "User wants to *initiate* an inventory transfer (review + submit POs),
|
|
1516
|
+
* not just see analysis or charts."
|
|
1497
1517
|
*/
|
|
1498
|
-
|
|
1518
|
+
whenToUse: string;
|
|
1499
1519
|
/**
|
|
1500
|
-
*
|
|
1520
|
+
* JSON-schema-style description of the props the workflow needs. Becomes
|
|
1521
|
+
* the LLM tool's input_schema, so the model fills these from the prompt.
|
|
1522
|
+
* Use the same shape as `params` on direct tools — string descriptors with
|
|
1523
|
+
* an optional "(optional)" suffix.
|
|
1524
|
+
*
|
|
1525
|
+
* Example:
|
|
1526
|
+
* ```
|
|
1527
|
+
* {
|
|
1528
|
+
* selectedStore: 'object — { id, name } of the source branch',
|
|
1529
|
+
* minROI: 'number (optional) — only show transfers with ROI ≥ this',
|
|
1530
|
+
* }
|
|
1531
|
+
* ```
|
|
1501
1532
|
*/
|
|
1502
|
-
|
|
1533
|
+
propsSchema: Record<string, string>;
|
|
1503
1534
|
/**
|
|
1504
|
-
*
|
|
1535
|
+
* Optional: static prop defaults merged with LLM-extracted props before
|
|
1536
|
+
* the component is returned. Useful for things like the embedded
|
|
1537
|
+
* `externalTool` config that the workflow uses to fetch its own data.
|
|
1505
1538
|
*/
|
|
1506
|
-
|
|
1539
|
+
defaultProps?: Record<string, any>;
|
|
1540
|
+
}
|
|
1541
|
+
/**
|
|
1542
|
+
* The workflow selection captured during a routing call.
|
|
1543
|
+
* Set on AgentResponse when the LLM picks a workflow tool.
|
|
1544
|
+
*/
|
|
1545
|
+
interface SelectedWorkflow {
|
|
1546
|
+
/** Component name (matches WorkflowDescriptor.name) */
|
|
1547
|
+
name: string;
|
|
1548
|
+
/** Props extracted from the prompt + merged with workflow.defaultProps */
|
|
1549
|
+
props: Record<string, any>;
|
|
1550
|
+
}
|
|
1551
|
+
/**
|
|
1552
|
+
* The complete response from the multi-agent system.
|
|
1553
|
+
* Contains everything needed for text display + component generation.
|
|
1554
|
+
*/
|
|
1555
|
+
interface AgentResponse {
|
|
1556
|
+
/** Generated text response (analysis of the data) */
|
|
1557
|
+
text: string;
|
|
1558
|
+
/** All executed tools across all source agents (for component generation) */
|
|
1559
|
+
executedTools: ExecutedToolInfo[];
|
|
1560
|
+
/** Individual results from each source agent */
|
|
1561
|
+
sourceResults: SourceAgentResult[];
|
|
1507
1562
|
/**
|
|
1508
|
-
*
|
|
1563
|
+
* Populated when MainAgent wrote AND successfully executed a script during its turn.
|
|
1564
|
+
* Caller (agent-user-response.ts) persists it via ScriptStore.save().
|
|
1565
|
+
* Absent when MainAgent didn't write one (trivial question / all attempts failed).
|
|
1509
1566
|
*/
|
|
1510
|
-
|
|
1511
|
-
getTextResponse(): string;
|
|
1567
|
+
savedScript?: AgentWrittenScript;
|
|
1512
1568
|
/**
|
|
1513
|
-
* Set
|
|
1569
|
+
* Set when the LLM routed the question to a registered workflow component.
|
|
1570
|
+
* When present, the upstream caller should skip component generation and
|
|
1571
|
+
* return this workflow as the response.
|
|
1514
1572
|
*/
|
|
1515
|
-
|
|
1573
|
+
workflow?: SelectedWorkflow;
|
|
1574
|
+
}
|
|
1575
|
+
/**
|
|
1576
|
+
* A script MainAgent authored + verified during its turn. Shape aligns with
|
|
1577
|
+
* what ScriptStore.save() needs — minus store-assigned fields (id, timestamps, counts).
|
|
1578
|
+
*/
|
|
1579
|
+
interface AgentWrittenScript {
|
|
1516
1580
|
/**
|
|
1517
|
-
*
|
|
1581
|
+
* `ScriptRecipe.id` of the draft that was authored + verified during this turn.
|
|
1582
|
+
* The caller passes this to `ScriptStore.promoteToVerified(recipeId, …)` to
|
|
1583
|
+
* flip the draft to verified status and (when possible) drop the turn-suffix
|
|
1584
|
+
* from its filename.
|
|
1518
1585
|
*/
|
|
1519
|
-
|
|
1586
|
+
recipeId: string;
|
|
1587
|
+
name: string;
|
|
1588
|
+
intentDescription: string;
|
|
1589
|
+
tags: string[];
|
|
1590
|
+
parameters: Array<{
|
|
1591
|
+
name: string;
|
|
1592
|
+
type: 'string' | 'number' | 'date' | 'date_range' | 'enum' | 'boolean';
|
|
1593
|
+
required: boolean;
|
|
1594
|
+
default?: any;
|
|
1595
|
+
enumValues?: Record<string, string>;
|
|
1596
|
+
description: string;
|
|
1597
|
+
}>;
|
|
1598
|
+
scriptBody: string;
|
|
1599
|
+
/** Source IDs referenced by the script (extracted from ctx.query calls) */
|
|
1600
|
+
sourceIds: string[];
|
|
1601
|
+
/** Tables referenced in the script's SQL (regex-extracted) */
|
|
1602
|
+
tables: string[];
|
|
1603
|
+
/** Executed queries from the verified run — fed to component generation */
|
|
1604
|
+
executedQueries: Array<{
|
|
1605
|
+
sourceId: string;
|
|
1606
|
+
sourceName: string;
|
|
1607
|
+
sql: string;
|
|
1608
|
+
data: any[];
|
|
1609
|
+
count: number;
|
|
1610
|
+
totalCount?: number;
|
|
1611
|
+
executionTimeMs: number;
|
|
1612
|
+
/**
|
|
1613
|
+
* True for synthetic entries (ctx.emit datasets, the computed:_final
|
|
1614
|
+
* post-JS data). The component generator routes virtual sources through
|
|
1615
|
+
* the script_dataset sentinel toolId so the frontend resolves them via
|
|
1616
|
+
* queryCache instead of attempting to re-execute SQL.
|
|
1617
|
+
*/
|
|
1618
|
+
virtual?: boolean;
|
|
1619
|
+
}>;
|
|
1620
|
+
}
|
|
1621
|
+
/**
|
|
1622
|
+
* Configuration for the multi-agent system.
|
|
1623
|
+
* Controls limits, models, and behavior.
|
|
1624
|
+
*/
|
|
1625
|
+
interface AgentConfig {
|
|
1626
|
+
/** Max rows shown to the UI preview / inlined per source (default: 10) */
|
|
1627
|
+
maxRowsPerSource: number;
|
|
1520
1628
|
/**
|
|
1521
|
-
*
|
|
1629
|
+
* Max rows a source query may FETCH from the DB server-side (default: 2000).
|
|
1630
|
+
* Decoupled from what the main agent is shown: the full result is fetched and
|
|
1631
|
+
* summarized (bounded), but only a small/complete slice enters LLM context.
|
|
1632
|
+
* This lets small lookups (benchmark maps) arrive COMPLETE without letting
|
|
1633
|
+
* large results blow up context.
|
|
1522
1634
|
*/
|
|
1523
|
-
|
|
1635
|
+
maxRowsFetched: number;
|
|
1636
|
+
/** Model for the main agent (routing + analysis in one LLM call) */
|
|
1637
|
+
mainAgentModel: string;
|
|
1638
|
+
/** Model for source agent query generation */
|
|
1639
|
+
sourceAgentModel: string;
|
|
1640
|
+
/** API key for LLM calls */
|
|
1641
|
+
apiKey?: string;
|
|
1642
|
+
/** Max retry attempts per source agent */
|
|
1643
|
+
maxRetries: number;
|
|
1644
|
+
/** Max tool calling iterations for the main agent loop */
|
|
1645
|
+
maxIterations: number;
|
|
1646
|
+
/** Global knowledge base context (static, same for all users/questions — cached in system prompt) */
|
|
1647
|
+
globalKnowledgeBase?: string;
|
|
1648
|
+
/** Per-request knowledge base context (user-specific + query-matched — dynamic, not cached) */
|
|
1649
|
+
knowledgeBaseContext?: string;
|
|
1650
|
+
/** Collections registry (ChromaDB search hooks) for embedding-based schema + source search */
|
|
1651
|
+
collections?: any;
|
|
1652
|
+
/** Optional project ID for scoping embedding searches */
|
|
1653
|
+
projectId?: string;
|
|
1654
|
+
}
|
|
1655
|
+
/**
|
|
1656
|
+
* Default agent configuration
|
|
1657
|
+
*/
|
|
1658
|
+
declare const DEFAULT_AGENT_CONFIG: AgentConfig;
|
|
1659
|
+
|
|
1660
|
+
/**
|
|
1661
|
+
* Script Flow Types
|
|
1662
|
+
*
|
|
1663
|
+
* Defines interfaces for the script-based query architecture:
|
|
1664
|
+
* - ScriptRecipe: metadata for matching, validation, and quality tracking
|
|
1665
|
+
* - ScriptResult: output from executing a script
|
|
1666
|
+
* - ScriptMatch: result from the LLM-based script matcher
|
|
1667
|
+
*/
|
|
1668
|
+
/**
|
|
1669
|
+
* Recipe metadata stored alongside each script.
|
|
1670
|
+
* Used for matching, validation, and quality tracking.
|
|
1671
|
+
*/
|
|
1672
|
+
interface ScriptRecipe {
|
|
1673
|
+
/** Unique script identifier */
|
|
1674
|
+
id: string;
|
|
1675
|
+
/** Version number (incremented on regeneration) */
|
|
1676
|
+
version: number;
|
|
1677
|
+
/** Human-readable name (e.g., "Revenue by Dimension") */
|
|
1678
|
+
name: string;
|
|
1679
|
+
/** Natural language description of what this script does */
|
|
1680
|
+
intentDescription: string;
|
|
1681
|
+
/** Keyword tags for quick filtering */
|
|
1682
|
+
tags: string[];
|
|
1683
|
+
/** Source tool IDs this script queries (e.g., ["mssql-abc123_query"]) */
|
|
1684
|
+
sourceIds: string[];
|
|
1685
|
+
/** Table names used (for future schema drift detection) */
|
|
1686
|
+
tables: string[];
|
|
1687
|
+
/** Parameter definitions — what can vary */
|
|
1688
|
+
parameters: ScriptParameter[];
|
|
1689
|
+
/** The script function body as a string. Loaded from disk (scripts-store/<fileBase>.ts). */
|
|
1690
|
+
scriptBody: string;
|
|
1524
1691
|
/**
|
|
1525
|
-
*
|
|
1692
|
+
* On-disk filename stem for the body: scripts-store/<fileBase>.ts.
|
|
1693
|
+
* Editable in the IDE. Decided at authoring time (slug of `name`, with a
|
|
1694
|
+
* short id suffix on collision) and stable across promotion.
|
|
1526
1695
|
*/
|
|
1527
|
-
|
|
1696
|
+
fileBase?: string;
|
|
1697
|
+
/** sha256 of the on-disk body — lets the runtime detect manual edits. */
|
|
1698
|
+
bodyHash?: string;
|
|
1699
|
+
/** Project scope (single-VM deployments may leave this undefined). */
|
|
1700
|
+
projectId?: string;
|
|
1701
|
+
/** Times this script was used successfully */
|
|
1702
|
+
successCount: number;
|
|
1703
|
+
/** Times this script failed */
|
|
1704
|
+
failureCount: number;
|
|
1705
|
+
/** ISO timestamp of last usage */
|
|
1706
|
+
lastUsed: string;
|
|
1707
|
+
/** Original user question that created this script */
|
|
1708
|
+
createdFrom: string;
|
|
1709
|
+
/** ISO timestamp */
|
|
1710
|
+
createdAt: string;
|
|
1711
|
+
/** ISO timestamp */
|
|
1712
|
+
updatedAt: string;
|
|
1528
1713
|
/**
|
|
1529
|
-
*
|
|
1714
|
+
* `recipe.id` of the parent this script was forked from.
|
|
1715
|
+
* Undefined for root scripts (those written from scratch by MainAgent).
|
|
1716
|
+
* See backend/docs/SCRIPT-FLOW-FORK-ADAPT.md.
|
|
1530
1717
|
*/
|
|
1531
|
-
|
|
1718
|
+
parentId?: string;
|
|
1719
|
+
/** 0 for root scripts; `parent.forkDepth + 1` for forks. Capped at 3. */
|
|
1720
|
+
forkDepth?: number;
|
|
1532
1721
|
/**
|
|
1533
|
-
*
|
|
1722
|
+
* Brief description of what this fork changed vs its parent
|
|
1723
|
+
* (sourced from the matcher's `modificationHint`).
|
|
1534
1724
|
*/
|
|
1535
|
-
|
|
1725
|
+
forkReason?: string;
|
|
1536
1726
|
/**
|
|
1537
|
-
*
|
|
1727
|
+
* Validated component specs captured at authoring time. On a tier-high
|
|
1728
|
+
* replay these are rebound to fresh queryIds deterministically — no
|
|
1729
|
+
* component-generation LLM call, and the rendered columns can't drift from
|
|
1730
|
+
* what was validated when the script was authored. Absent on recipes
|
|
1731
|
+
* authored before this landed; those fall back to LLM component generation.
|
|
1732
|
+
* See backend/docs/SCRIPT-COMPONENT-CONSISTENCY.md.
|
|
1538
1733
|
*/
|
|
1539
|
-
|
|
1734
|
+
components?: ScriptComponentSpec[];
|
|
1540
1735
|
/**
|
|
1541
|
-
*
|
|
1736
|
+
* Lifecycle stage of this recipe on disk.
|
|
1737
|
+
* - 'draft': written by MainAgent's write_script during a turn; filtered out
|
|
1738
|
+
* of FTS results (status='verified' only) so the matcher never picks it.
|
|
1739
|
+
* Filename is suffixed with `turnId` to keep concurrent turns
|
|
1740
|
+
* from clobbering each other's drafts.
|
|
1741
|
+
* - 'verified': promoted after `execute_script` succeeded; the matcher sees it.
|
|
1742
|
+
* Filename drops the turn suffix unless a verified file with
|
|
1743
|
+
* the same slug already exists (collision case keeps the suffix).
|
|
1744
|
+
*
|
|
1745
|
+
* Recipes loaded from disk without this field default to 'verified' so
|
|
1746
|
+
* existing scripts keep working unchanged.
|
|
1542
1747
|
*/
|
|
1543
|
-
|
|
1748
|
+
status?: 'draft' | 'verified';
|
|
1544
1749
|
/**
|
|
1545
|
-
*
|
|
1750
|
+
* Per-turn unique suffix used for draft filenames (e.g. `1714745623-x9k2`).
|
|
1751
|
+
* Set when the draft is saved; carried until the recipe is promoted.
|
|
1546
1752
|
*/
|
|
1547
|
-
|
|
1753
|
+
turnId?: string;
|
|
1548
1754
|
/**
|
|
1549
|
-
*
|
|
1550
|
-
*
|
|
1551
|
-
*
|
|
1552
|
-
* @param generateFn - Async function to generate actions
|
|
1553
|
-
* @returns Promise resolving to Action[]
|
|
1755
|
+
* Last execution error captured by `recordDraftError` while the recipe was
|
|
1756
|
+
* still a draft. Lets users open the draft .json file and see why it failed
|
|
1757
|
+
* without grepping logs. Cleared on promotion to 'verified'.
|
|
1554
1758
|
*/
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1759
|
+
lastError?: {
|
|
1760
|
+
phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
|
|
1761
|
+
message: string;
|
|
1762
|
+
at: string;
|
|
1763
|
+
attempt: number;
|
|
1764
|
+
};
|
|
1765
|
+
}
|
|
1766
|
+
interface ScriptParameter {
|
|
1767
|
+
/** Parameter name (used in script body as params.name) */
|
|
1768
|
+
name: string;
|
|
1769
|
+
/** Parameter type */
|
|
1770
|
+
type: 'string' | 'number' | 'date' | 'date_range' | 'enum' | 'boolean';
|
|
1771
|
+
/** Whether this parameter is required */
|
|
1772
|
+
required: boolean;
|
|
1773
|
+
/** Default value if not provided */
|
|
1774
|
+
default?: any;
|
|
1775
|
+
/** For enum type — maps user-facing values to internal values */
|
|
1776
|
+
enumValues?: Record<string, string>;
|
|
1777
|
+
/** Human-readable description (used in the matcher LLM prompt) */
|
|
1778
|
+
description: string;
|
|
1779
|
+
}
|
|
1780
|
+
/**
|
|
1781
|
+
* A reusable component binding captured when a script is authored. Stored on
|
|
1782
|
+
* the recipe so tier-high replays rebuild components deterministically (rebind
|
|
1783
|
+
* to fresh queryIds) instead of re-running the component-picker LLM.
|
|
1784
|
+
*/
|
|
1785
|
+
interface ScriptComponentSpec {
|
|
1786
|
+
/** Registered component name (e.g. "DynamicBarChart") — matched against the available component library. */
|
|
1787
|
+
componentType: string;
|
|
1788
|
+
/** `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). */
|
|
1789
|
+
sourceRef: string;
|
|
1790
|
+
/** Present only when sourceRef === 'federation' — the DuckDB SQL to re-execute on replay. */
|
|
1791
|
+
federationSql?: string;
|
|
1792
|
+
/** Present only when sourceRef === 'markdown' — the narrative text to render on replay (markdown has no data source, so its content must be persisted). */
|
|
1793
|
+
content?: string;
|
|
1794
|
+
title?: string;
|
|
1795
|
+
description?: string;
|
|
1796
|
+
/** Validated axis/value keys + aggregation — all referencing real columns of the bound source. */
|
|
1797
|
+
config: Record<string, any>;
|
|
1798
|
+
}
|
|
1799
|
+
/**
|
|
1800
|
+
* Result from executing a script via ScriptRunner.
|
|
1801
|
+
*/
|
|
1802
|
+
interface ScriptResult {
|
|
1803
|
+
/** Whether the script executed successfully */
|
|
1804
|
+
success: boolean;
|
|
1805
|
+
/** Combined data from all queries */
|
|
1806
|
+
data: any[];
|
|
1807
|
+
/** Individual query results tracked during execution */
|
|
1808
|
+
executedQueries: ScriptQueryResult[];
|
|
1809
|
+
/** Error message if failed */
|
|
1810
|
+
error?: string;
|
|
1576
1811
|
/**
|
|
1577
|
-
*
|
|
1812
|
+
* Where in the lifecycle the error occurred. Lets MainAgent's fix-loop
|
|
1813
|
+
* decide between "rewrite the whole draft" (compile) and "patch the
|
|
1814
|
+
* specific line" (runtime).
|
|
1578
1815
|
*/
|
|
1579
|
-
|
|
1816
|
+
errorPhase?: 'compile' | 'runtime' | 'timeout' | 'ipc';
|
|
1817
|
+
/** Total execution time in milliseconds */
|
|
1818
|
+
executionTimeMs: number;
|
|
1819
|
+
}
|
|
1820
|
+
/**
|
|
1821
|
+
* A single query executed during script runtime.
|
|
1822
|
+
* Tracked by ScriptContext for component generation and debugging.
|
|
1823
|
+
*/
|
|
1824
|
+
interface ScriptQueryResult {
|
|
1825
|
+
/** Source tool ID */
|
|
1826
|
+
sourceId: string;
|
|
1827
|
+
/** Human-readable source name */
|
|
1828
|
+
sourceName: string;
|
|
1829
|
+
/** The SQL that was executed */
|
|
1830
|
+
sql: string;
|
|
1831
|
+
/** Result data rows */
|
|
1832
|
+
data: any[];
|
|
1833
|
+
/** Number of rows returned */
|
|
1834
|
+
count: number;
|
|
1835
|
+
/** Total rows that matched before limit (if available) */
|
|
1836
|
+
totalCount?: number;
|
|
1837
|
+
/** Query execution time in milliseconds */
|
|
1838
|
+
executionTimeMs: number;
|
|
1580
1839
|
/**
|
|
1581
|
-
*
|
|
1840
|
+
* True for rows that did NOT come from a real SQL execution — either a
|
|
1841
|
+
* ctx.emit() dataset or the synthesized "computed:_final" entry that
|
|
1842
|
+
* carries the script's post-JS returned data. The component generator
|
|
1843
|
+
* uses this to route the resulting component through the script_dataset
|
|
1844
|
+
* sentinel toolId so the frontend resolves it via the queryCache short-circuit.
|
|
1582
1845
|
*/
|
|
1583
|
-
|
|
1846
|
+
virtual?: boolean;
|
|
1847
|
+
}
|
|
1848
|
+
/**
|
|
1849
|
+
* Match tier returned by the LLM script matcher.
|
|
1850
|
+
*
|
|
1851
|
+
* - 'high': the script answers the question directly; only parameter values
|
|
1852
|
+
* may differ. The runtime replays it with extracted params (cheapest path).
|
|
1853
|
+
* - 'near': the script answers a STRUCTURALLY similar question but needs
|
|
1854
|
+
* body modification (different metric, dimension, table, filter shape).
|
|
1855
|
+
* The runtime forks the parent and adapts the body via MainAgent's normal
|
|
1856
|
+
* write_script + execute_script loop — no SourceAgent dispatch needed.
|
|
1857
|
+
* See backend/docs/SCRIPT-FLOW-FORK-ADAPT.md for the full design.
|
|
1858
|
+
* - 'none': no script is relevant; full agent flow runs.
|
|
1859
|
+
*/
|
|
1860
|
+
type MatchTier = 'high' | 'near' | 'none';
|
|
1861
|
+
/**
|
|
1862
|
+
* Result from the LLM-based script matcher.
|
|
1863
|
+
*
|
|
1864
|
+
* For `tier: 'high'`, `extractedParams` carries the values to pass to the
|
|
1865
|
+
* existing script. For `tier: 'near'`, `gaps` and `modificationHint` describe
|
|
1866
|
+
* what the fork-author needs to change in the parent body.
|
|
1867
|
+
*/
|
|
1868
|
+
interface ScriptMatch {
|
|
1869
|
+
/** The matched script recipe */
|
|
1870
|
+
recipe: ScriptRecipe;
|
|
1871
|
+
/** Match tier — see MatchTier docs */
|
|
1872
|
+
tier: MatchTier;
|
|
1873
|
+
/** Similarity score (0-1, derived from LLM tier) */
|
|
1874
|
+
similarity: number;
|
|
1875
|
+
/**
|
|
1876
|
+
* Legacy confidence level. Mirrors `tier === 'high'`/`'near'` for now;
|
|
1877
|
+
* kept so existing callers compile while we migrate to tier-based logic.
|
|
1878
|
+
*/
|
|
1879
|
+
confidence: 'high' | 'medium';
|
|
1880
|
+
/** Parameters extracted from the user question by the LLM (tier='high') */
|
|
1881
|
+
extractedParams?: Record<string, any>;
|
|
1882
|
+
/** What the user question needs that the parent doesn't cover (tier='near') */
|
|
1883
|
+
gaps?: string[];
|
|
1884
|
+
/** One-sentence description of the change the fork-author should make (tier='near') */
|
|
1885
|
+
modificationHint?: string;
|
|
1886
|
+
/** Why the matcher made this choice (for logs and telemetry) */
|
|
1887
|
+
reasoning?: string;
|
|
1584
1888
|
}
|
|
1585
1889
|
|
|
1586
1890
|
/**
|
|
1587
|
-
*
|
|
1588
|
-
*
|
|
1891
|
+
* ScriptRecipeStore — injected metadata backend for the script flow.
|
|
1892
|
+
*
|
|
1893
|
+
* The SDK is standalone (no DB dependency). The backend implements this
|
|
1894
|
+
* interface over Postgres (full-text search + atomic counters) and injects it
|
|
1895
|
+
* via `collections['script-recipes']`, exactly like `collections['source-embeddings']`.
|
|
1896
|
+
* `ScriptStore` consumes it for all METADATA operations while keeping the
|
|
1897
|
+
* executable body on disk as scripts-store/<fileBase>.ts.
|
|
1898
|
+
*
|
|
1899
|
+
* All metadata rows are plain JSON (no scriptBody — that lives on disk).
|
|
1900
|
+
* See backend/docs/SCRIPT-FLOW-SCALING-ISSUES.md (#1, #3, #7).
|
|
1589
1901
|
*/
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1902
|
+
|
|
1903
|
+
/** One recipe's metadata as stored in Postgres (mirrors the script_recipes table). */
|
|
1904
|
+
interface ScriptRecipeMetaRow {
|
|
1905
|
+
id: string;
|
|
1906
|
+
projectId?: string | null;
|
|
1907
|
+
version: number;
|
|
1908
|
+
name: string;
|
|
1909
|
+
intentDescription: string;
|
|
1910
|
+
tags: string[] | null;
|
|
1911
|
+
createdFrom: string | null;
|
|
1912
|
+
sourceIds: string[] | null;
|
|
1913
|
+
tables: string[] | null;
|
|
1914
|
+
parameters: ScriptParameter[] | null;
|
|
1915
|
+
components?: ScriptComponentSpec[] | null;
|
|
1916
|
+
fileBase: string;
|
|
1917
|
+
bodyHash?: string | null;
|
|
1918
|
+
successCount: number;
|
|
1919
|
+
failureCount: number;
|
|
1920
|
+
lastUsed: string | null;
|
|
1921
|
+
parentId?: string | null;
|
|
1922
|
+
forkDepth?: number | null;
|
|
1923
|
+
forkReason?: string | null;
|
|
1924
|
+
status: 'draft' | 'verified' | string;
|
|
1925
|
+
turnId?: string | null;
|
|
1926
|
+
lastError?: {
|
|
1927
|
+
phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
|
|
1928
|
+
message: string;
|
|
1929
|
+
at: string;
|
|
1930
|
+
attempt: number;
|
|
1931
|
+
} | null;
|
|
1932
|
+
createdAt?: string | null;
|
|
1933
|
+
updatedAt?: string | null;
|
|
1934
|
+
}
|
|
1935
|
+
interface ScriptRecipeStore {
|
|
1936
|
+
/** FTS shortlist of healthy verified recipes for the matcher (metadata only). */
|
|
1937
|
+
search(params: {
|
|
1938
|
+
prompt: string;
|
|
1939
|
+
projectId?: string;
|
|
1940
|
+
limit?: number;
|
|
1941
|
+
}): Promise<ScriptRecipeMetaRow[]>;
|
|
1942
|
+
/** Fetch one recipe by id (any status). */
|
|
1943
|
+
getById(id: string): Promise<ScriptRecipeMetaRow | null>;
|
|
1944
|
+
/** Count healthy verified recipes (drives the "any scripts?" gate). */
|
|
1945
|
+
count(params?: {
|
|
1946
|
+
projectId?: string;
|
|
1947
|
+
}): Promise<number>;
|
|
1948
|
+
/** Insert or update a recipe row (keyed by id). */
|
|
1949
|
+
upsert(row: ScriptRecipeMetaRow): Promise<void>;
|
|
1950
|
+
/** Atomically bump counters / last-used. */
|
|
1951
|
+
updateStats(id: string, patch: {
|
|
1952
|
+
successDelta?: number;
|
|
1953
|
+
failureDelta?: number;
|
|
1954
|
+
lastUsed?: string;
|
|
1955
|
+
}): Promise<void>;
|
|
1956
|
+
/** Flip a draft to verified, applying provenance + optional fork lineage. */
|
|
1957
|
+
promote(id: string, patch: {
|
|
1958
|
+
sourceIds: string[];
|
|
1959
|
+
tables: string[];
|
|
1960
|
+
fileBase?: string;
|
|
1961
|
+
parentId?: string;
|
|
1962
|
+
forkDepth?: number;
|
|
1963
|
+
forkReason?: string;
|
|
1964
|
+
components?: ScriptComponentSpec[];
|
|
1965
|
+
}): Promise<ScriptRecipeMetaRow | null>;
|
|
1966
|
+
/** Stamp a draft's last execution error. */
|
|
1967
|
+
recordDraftError(id: string, err: {
|
|
1968
|
+
phase: string;
|
|
1969
|
+
message: string;
|
|
1970
|
+
attempt: number;
|
|
1971
|
+
at: string;
|
|
1972
|
+
}): Promise<void>;
|
|
1973
|
+
/** Delete a recipe row (body file removed separately). */
|
|
1974
|
+
remove(id: string): Promise<void>;
|
|
1975
|
+
/** True if `fileBase` is taken by a different recipe in this project. */
|
|
1976
|
+
fileBaseTaken(fileBase: string, excludeId: string, projectId?: string): Promise<boolean>;
|
|
1977
|
+
}
|
|
1978
|
+
/** Pull the injected store off the collections bag (or null if not wired). */
|
|
1979
|
+
declare function resolveScriptRecipeStore(collections: any): ScriptRecipeStore | null;
|
|
1980
|
+
|
|
1981
|
+
/**
|
|
1982
|
+
* ScriptStore — Postgres metadata + on-disk body for script recipes.
|
|
1983
|
+
*
|
|
1984
|
+
* Split of responsibilities:
|
|
1985
|
+
* - METADATA → injected `ScriptRecipeStore` (Postgres FTS + atomic counters),
|
|
1986
|
+
* resolved from `collections['script-recipes']`.
|
|
1987
|
+
* - BODY → scripts-store/<fileBase>.ts, editable in your IDE. Written
|
|
1988
|
+
* atomically (temp + rename); `bodyHash` (sha256) detects edits.
|
|
1989
|
+
*
|
|
1990
|
+
* The old "read every file every turn + send the whole catalog to the LLM"
|
|
1991
|
+
* matcher is gone — matching is `store.search(prompt)` (FTS shortlist). The
|
|
1992
|
+
* draft/verified filename dance is gone too: `status` is a DB column and the
|
|
1993
|
+
* file keeps a stable `<fileBase>.ts` name across promotion.
|
|
1994
|
+
*
|
|
1995
|
+
* When no metadata store is injected, the store degrades to a safe no-op
|
|
1996
|
+
* (count 0 → script flow disabled) instead of crashing.
|
|
1997
|
+
*
|
|
1998
|
+
* See backend/docs/SCRIPT-FLOW-SCALING-ISSUES.md.
|
|
1999
|
+
*/
|
|
2000
|
+
|
|
2001
|
+
interface SaveDraftInput {
|
|
2002
|
+
/** Reuse an existing draft (retry); omit to mint a new one. */
|
|
2003
|
+
recipeId?: string;
|
|
2004
|
+
/** Per-turn unique suffix, stable across retries within the turn. */
|
|
2005
|
+
turnId: string;
|
|
2006
|
+
name: string;
|
|
2007
|
+
intentDescription: string;
|
|
2008
|
+
tags: string[];
|
|
2009
|
+
parameters: ScriptParameter[];
|
|
2010
|
+
scriptBody: string;
|
|
2011
|
+
createdFrom: string;
|
|
2012
|
+
}
|
|
2013
|
+
interface PromoteToVerifiedInput {
|
|
2014
|
+
sourceIds: string[];
|
|
2015
|
+
tables: string[];
|
|
2016
|
+
parentId?: string;
|
|
2017
|
+
forkDepth?: number;
|
|
2018
|
+
forkReason?: string;
|
|
2019
|
+
components?: ScriptComponentSpec[];
|
|
2020
|
+
}
|
|
2021
|
+
interface ScriptStoreOptions {
|
|
2022
|
+
/** Explicit metadata store, or resolved from `collections['script-recipes']`. */
|
|
2023
|
+
store?: ScriptRecipeStore | null;
|
|
2024
|
+
collections?: any;
|
|
2025
|
+
/** Body directory (defaults to <cwd>/scripts-store). */
|
|
2026
|
+
baseDir?: string;
|
|
2027
|
+
/** Project scope stamped on every row. */
|
|
2028
|
+
projectId?: string;
|
|
2029
|
+
}
|
|
2030
|
+
/**
|
|
2031
|
+
* Normalize a scriptBody into the on-disk form (strip a leading comment block,
|
|
2032
|
+
* ensure `export async function getData`). Exported for MainAgent.
|
|
2033
|
+
*/
|
|
2034
|
+
declare function normalizeScriptBody(scriptBody: string): string;
|
|
2035
|
+
declare class ScriptStore {
|
|
2036
|
+
private store;
|
|
2037
|
+
private storeDir;
|
|
2038
|
+
private projectId?;
|
|
2039
|
+
constructor(opts?: ScriptStoreOptions);
|
|
2040
|
+
/** Whether a metadata store is wired (matcher / authoring are gated on this). */
|
|
2041
|
+
hasStore(): boolean;
|
|
2042
|
+
/** Number of healthy verified recipes (gates the script-matching path). */
|
|
2043
|
+
count(): Promise<number>;
|
|
2044
|
+
/**
|
|
2045
|
+
* FTS shortlist for the matcher (metadata only — bodies are loaded lazily by
|
|
2046
|
+
* `get()` once the LLM picks one). Returns verified, healthy recipes ranked
|
|
2047
|
+
* by relevance.
|
|
2048
|
+
*/
|
|
2049
|
+
search(prompt: string, limit?: number): Promise<ScriptRecipe[]>;
|
|
2050
|
+
/** Fetch one recipe by id with its body loaded from disk. */
|
|
2051
|
+
get(id: string): Promise<ScriptRecipe | null>;
|
|
2052
|
+
/** Create or update a recipe (metadata upsert + body write when changed). */
|
|
2053
|
+
save(recipe: ScriptRecipe): Promise<void>;
|
|
2054
|
+
/**
|
|
2055
|
+
* Persist (or update) a draft. Within a turn, retries that pass the same
|
|
2056
|
+
* `recipeId` overwrite the same row + file; a fresh `recipeId` mints a new
|
|
2057
|
+
* draft. The body is visible at scripts-store/<fileBase>.ts immediately.
|
|
2058
|
+
*/
|
|
2059
|
+
saveDraft(input: SaveDraftInput): Promise<ScriptRecipe>;
|
|
2060
|
+
/** Stamp a draft's last execution error (metadata only). */
|
|
2061
|
+
recordDraftError(recipeId: string, err: {
|
|
2062
|
+
phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
|
|
2063
|
+
message: string;
|
|
2064
|
+
attempt: number;
|
|
2065
|
+
}): Promise<void>;
|
|
1594
2066
|
/**
|
|
1595
|
-
*
|
|
1596
|
-
*
|
|
1597
|
-
|
|
1598
|
-
|
|
2067
|
+
* Promote a successfully-executed draft into a verified script.
|
|
2068
|
+
* The on-disk body already exists at <fileBase>.ts (written at write_script
|
|
2069
|
+
* time) and keeps its name — only the DB row flips status + provenance.
|
|
2070
|
+
*/
|
|
2071
|
+
promoteToVerified(recipeId: string, input: PromoteToVerifiedInput): Promise<ScriptRecipe | null>;
|
|
2072
|
+
/**
|
|
2073
|
+
* Drop a draft (row + body file). MainAgent calls this at end-of-turn when a
|
|
2074
|
+
* draft was authored but never verified — failed drafts are never matched, so
|
|
2075
|
+
* deleting them immediately avoids unbounded accumulation (#5). No-op if the
|
|
2076
|
+
* recipe isn't a draft (so a promoted/verified script is never removed here).
|
|
2077
|
+
*/
|
|
2078
|
+
discardDraft(recipeId: string): Promise<void>;
|
|
2079
|
+
/** Delete a recipe (row + body file). */
|
|
2080
|
+
delete(id: string): Promise<void>;
|
|
2081
|
+
/** Record a successful execution (atomic counter bump). */
|
|
2082
|
+
recordSuccess(id: string): Promise<void>;
|
|
2083
|
+
/** Record a failed execution (atomic counter bump). */
|
|
2084
|
+
recordFailure(id: string): Promise<void>;
|
|
2085
|
+
/** Absolute path to the .ts body for a recipe (used by the runner/MainAgent). */
|
|
2086
|
+
getScriptPath(recipe: ScriptRecipe): string;
|
|
2087
|
+
private removeById;
|
|
2088
|
+
private rowToRecipe;
|
|
2089
|
+
private recipeToRow;
|
|
2090
|
+
/** slug of name, with a short id suffix when the bare slug is already taken. */
|
|
2091
|
+
private computeFileBase;
|
|
2092
|
+
private toSlug;
|
|
2093
|
+
private hash;
|
|
2094
|
+
private bodyPath;
|
|
2095
|
+
private readBody;
|
|
2096
|
+
/** Atomic body write (temp + rename) so concurrent reads never see a partial file. */
|
|
2097
|
+
private writeBody;
|
|
2098
|
+
private unlinkBody;
|
|
2099
|
+
}
|
|
2100
|
+
|
|
2101
|
+
/**
|
|
2102
|
+
* Main Agent (Orchestrator)
|
|
2103
|
+
*
|
|
2104
|
+
* A single LLM.streamWithTools() call that handles everything:
|
|
2105
|
+
* - Routing: decides which source(s) to query based on summaries
|
|
2106
|
+
* - Querying: calls source tools (each wraps an independent SourceAgent)
|
|
2107
|
+
* - Direct tools: calls pre-built function tools directly with LLM-provided params
|
|
2108
|
+
* - Re-querying: if data is wrong/incomplete, calls tools again with modified intent
|
|
2109
|
+
* - Analysis: generates final text response from the data
|
|
2110
|
+
*
|
|
2111
|
+
* Two tool types:
|
|
2112
|
+
* - "source" tools: main agent sees summaries, SourceAgent handles SQL generation independently
|
|
2113
|
+
* - "direct" tools: main agent calls fn() directly with structured params (no SourceAgent)
|
|
2114
|
+
*/
|
|
2115
|
+
|
|
2116
|
+
declare class MainAgent {
|
|
2117
|
+
private externalTools;
|
|
2118
|
+
private workflows;
|
|
2119
|
+
private config;
|
|
2120
|
+
private streamBuffer;
|
|
1599
2121
|
/**
|
|
1600
|
-
*
|
|
2122
|
+
* Optional: when provided, MainAgent exposes the `write_script` /
|
|
2123
|
+
* `execute_script` tools to the LLM and persists drafts to disk via the
|
|
2124
|
+
* store. Headless callers (alert analyzer, metric resolver) omit these to
|
|
2125
|
+
* suppress script authoring entirely — drafts would otherwise leak onto
|
|
2126
|
+
* disk with no caller to promote or clean them up.
|
|
1601
2127
|
*/
|
|
1602
|
-
|
|
2128
|
+
private scriptStore;
|
|
2129
|
+
private turnId;
|
|
2130
|
+
private createdFromPrompt;
|
|
2131
|
+
private scriptState;
|
|
2132
|
+
constructor(externalTools: ExternalTool[], config: AgentConfig, scriptStore?: ScriptStore, turnId?: string, streamBuffer?: StreamBuffer, workflows?: WorkflowDescriptor[]);
|
|
2133
|
+
private get scriptingEnabled();
|
|
1603
2134
|
/**
|
|
1604
|
-
*
|
|
2135
|
+
* Handle a user question using the multi-agent system.
|
|
2136
|
+
*
|
|
2137
|
+
* This is ONE LLM.streamWithTools() call. The LLM:
|
|
2138
|
+
* 1. Sees source summaries + direct tool descriptions in system prompt
|
|
2139
|
+
* 2. Decides which tool(s) to call (routing)
|
|
2140
|
+
* 3. Source tools → SourceAgent runs independently → returns data
|
|
2141
|
+
* 4. Direct tools → fn() called directly with LLM params → returns data
|
|
2142
|
+
* 5. Generates final analysis text
|
|
1605
2143
|
*/
|
|
1606
|
-
|
|
2144
|
+
handleQuestion(userPrompt: string, apiKey?: string, conversationHistory?: string, streamCallback?: (chunk: string) => void): Promise<AgentResponse>;
|
|
2145
|
+
private handleWriteScript;
|
|
2146
|
+
private handleExecuteScript;
|
|
1607
2147
|
/**
|
|
1608
|
-
*
|
|
2148
|
+
* Build the AgentWrittenScript payload the caller will hand to
|
|
2149
|
+
* `ScriptStore.promoteToVerified()`. Only returned when a verified
|
|
2150
|
+
* successful execution is on record.
|
|
1609
2151
|
*/
|
|
1610
|
-
|
|
2152
|
+
private buildSavedScript;
|
|
2153
|
+
private normalizeParameterList;
|
|
1611
2154
|
/**
|
|
1612
|
-
*
|
|
2155
|
+
* Use the schema embedding collection to pre-select relevant tables for
|
|
2156
|
+
* this source + intent. Returns a formatted schema block if confidence is
|
|
2157
|
+
* high (top match ≥ 0.55 and ≥3 candidates), otherwise null.
|
|
2158
|
+
*
|
|
2159
|
+
* When this returns a block, we can skip the SourceAgent's `search_schema`
|
|
2160
|
+
* loop and reduce iteration budget. When it returns null, the SourceAgent
|
|
2161
|
+
* falls back to the existing LLM-driven keyword search (same as today).
|
|
1613
2162
|
*/
|
|
1614
|
-
|
|
2163
|
+
private preResolveSchema;
|
|
1615
2164
|
/**
|
|
1616
|
-
*
|
|
2165
|
+
* Execute a direct tool — call fn() with LLM-provided params, no SourceAgent.
|
|
1617
2166
|
*/
|
|
1618
|
-
|
|
2167
|
+
private handleDirectTool;
|
|
1619
2168
|
/**
|
|
1620
|
-
*
|
|
2169
|
+
* Build the main agent's system prompt with source summaries, direct tool descriptions,
|
|
2170
|
+
* and workflow component descriptions.
|
|
1621
2171
|
*/
|
|
1622
|
-
|
|
2172
|
+
private buildSystemPrompt;
|
|
1623
2173
|
/**
|
|
1624
|
-
*
|
|
2174
|
+
* Build tool definitions for source tools — summary-only descriptions.
|
|
2175
|
+
* The full schema is inside the SourceAgent which runs independently.
|
|
1625
2176
|
*/
|
|
1626
|
-
|
|
2177
|
+
private buildSourceToolDefinitions;
|
|
1627
2178
|
/**
|
|
1628
|
-
*
|
|
2179
|
+
* Build tool definitions for direct tools — expose their actual params.
|
|
2180
|
+
* These are called directly by the main agent LLM, no SourceAgent.
|
|
1629
2181
|
*/
|
|
1630
|
-
|
|
2182
|
+
private buildDirectToolDefinitions;
|
|
1631
2183
|
/**
|
|
1632
|
-
*
|
|
2184
|
+
* Capture a workflow selection. We do NOT execute anything — the LLM has
|
|
2185
|
+
* already extracted the props it wants the workflow rendered with. We
|
|
2186
|
+
* record the selection (via the capture callback) and return a short
|
|
2187
|
+
* acknowledgement so the LLM ends its turn cleanly without writing
|
|
2188
|
+
* analysis text or calling more tools.
|
|
1633
2189
|
*/
|
|
1634
|
-
|
|
2190
|
+
private handleWorkflow;
|
|
1635
2191
|
/**
|
|
1636
|
-
*
|
|
2192
|
+
* Build LLM tool definitions for workflow components. The workflow's
|
|
2193
|
+
* propsSchema becomes the tool's input_schema so the LLM extracts props
|
|
2194
|
+
* directly from the prompt — same mechanic as direct tools.
|
|
1637
2195
|
*/
|
|
1638
|
-
|
|
2196
|
+
private buildWorkflowToolDefinitions;
|
|
1639
2197
|
/**
|
|
1640
|
-
*
|
|
1641
|
-
* Returns formatted string with previous questions and component summaries
|
|
1642
|
-
* @param limit - Maximum number of previous UIBlocks to include (default: 5)
|
|
1643
|
-
* @param currentUIBlockId - ID of current UIBlock to exclude from context (optional)
|
|
1644
|
-
* @returns Formatted conversation history string
|
|
2198
|
+
* Format a source agent's result as a clean string for the main agent LLM.
|
|
1645
2199
|
*/
|
|
1646
|
-
|
|
2200
|
+
private formatResultForMainAgent;
|
|
1647
2201
|
/**
|
|
1648
|
-
*
|
|
2202
|
+
* Get source summaries (for external inspection/debugging).
|
|
1649
2203
|
*/
|
|
1650
|
-
|
|
2204
|
+
getSourceSummaries(): SourceSummary[];
|
|
1651
2205
|
}
|
|
1652
2206
|
|
|
1653
2207
|
/**
|
|
1654
|
-
*
|
|
1655
|
-
* Provides methods to create, retrieve, and delete threads.
|
|
1656
|
-
* Includes automatic cleanup to prevent unbounded memory growth.
|
|
2208
|
+
* Represents an action that can be performed on a UIBlock
|
|
1657
2209
|
*/
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
2210
|
+
interface Action {
|
|
2211
|
+
id: string;
|
|
2212
|
+
name: string;
|
|
2213
|
+
type: string;
|
|
2214
|
+
[key: string]: any;
|
|
2215
|
+
}
|
|
2216
|
+
|
|
2217
|
+
type SystemPrompt = string | Anthropic.Messages.TextBlockParam[];
|
|
2218
|
+
interface LLMMessages {
|
|
2219
|
+
sys: SystemPrompt;
|
|
2220
|
+
user: string;
|
|
2221
|
+
prefill?: string;
|
|
2222
|
+
}
|
|
2223
|
+
interface LLMOptions {
|
|
2224
|
+
model?: string;
|
|
2225
|
+
maxTokens?: number;
|
|
2226
|
+
temperature?: number;
|
|
2227
|
+
topP?: number;
|
|
2228
|
+
apiKey?: string;
|
|
2229
|
+
partial?: (chunk: string) => void;
|
|
2230
|
+
}
|
|
2231
|
+
interface Tool {
|
|
2232
|
+
name: string;
|
|
2233
|
+
description: string;
|
|
2234
|
+
input_schema: {
|
|
2235
|
+
type: string;
|
|
2236
|
+
properties: Record<string, any>;
|
|
2237
|
+
required?: string[];
|
|
2238
|
+
};
|
|
2239
|
+
}
|
|
2240
|
+
declare class LLM {
|
|
2241
|
+
static text(messages: LLMMessages, options?: LLMOptions): Promise<string>;
|
|
2242
|
+
static stream<T = string>(messages: LLMMessages, options?: LLMOptions, json?: boolean): Promise<T extends string ? string : any>;
|
|
2243
|
+
static streamWithTools(messages: LLMMessages, tools: Tool[], toolHandler: (toolName: string, toolInput: any) => Promise<any>, options?: LLMOptions, maxIterations?: number): Promise<string>;
|
|
1673
2244
|
/**
|
|
1674
|
-
*
|
|
1675
|
-
*
|
|
1676
|
-
* @
|
|
2245
|
+
* Normalize system prompt to Anthropic format
|
|
2246
|
+
* Converts string to array format if needed
|
|
2247
|
+
* @param sys - System prompt (string or array of blocks)
|
|
2248
|
+
* @returns Normalized system prompt for Anthropic API
|
|
1677
2249
|
*/
|
|
1678
|
-
|
|
2250
|
+
private static _normalizeSystemPrompt;
|
|
1679
2251
|
/**
|
|
1680
|
-
*
|
|
2252
|
+
* Strip unpaired UTF-16 surrogates from every text field of a message set.
|
|
2253
|
+
*
|
|
2254
|
+
* A lone surrogate (from mid-pair string slicing or corrupt source data)
|
|
2255
|
+
* serializes to a bare `\udXXX` escape that strict JSON parsers — including
|
|
2256
|
+
* the one on Anthropic's API — reject with "no low surrogate in string",
|
|
2257
|
+
* failing the whole request. Sanitizing here, at the single boundary every
|
|
2258
|
+
* provider call flows through, guarantees no request can carry one.
|
|
1681
2259
|
*/
|
|
1682
|
-
|
|
2260
|
+
private static _sanitizeMessages;
|
|
1683
2261
|
/**
|
|
1684
|
-
*
|
|
2262
|
+
* Log cache usage metrics from Anthropic API response
|
|
2263
|
+
* Shows cache hits, costs, and savings
|
|
1685
2264
|
*/
|
|
1686
|
-
|
|
2265
|
+
private static _logCacheUsage;
|
|
1687
2266
|
/**
|
|
1688
|
-
*
|
|
2267
|
+
* Parse model string to extract provider and model name
|
|
2268
|
+
* @param modelString - Format: "provider/model-name" or just "model-name"
|
|
2269
|
+
* @returns [provider, modelName]
|
|
2270
|
+
*
|
|
2271
|
+
* @example
|
|
2272
|
+
* "anthropic/claude-sonnet-4-5" → ["anthropic", "claude-sonnet-4-5"]
|
|
2273
|
+
* "groq/openai/gpt-oss-120b" → ["groq", "openai/gpt-oss-120b"]
|
|
2274
|
+
* "claude-sonnet-4-5" → ["anthropic", "claude-sonnet-4-5"] (default)
|
|
1689
2275
|
*/
|
|
1690
|
-
|
|
2276
|
+
private static _parseModel;
|
|
2277
|
+
private static _anthropicText;
|
|
2278
|
+
private static _anthropicStream;
|
|
2279
|
+
private static _anthropicStreamWithTools;
|
|
2280
|
+
private static _groqText;
|
|
2281
|
+
private static _groqStream;
|
|
2282
|
+
private static _geminiText;
|
|
2283
|
+
private static _geminiStream;
|
|
1691
2284
|
/**
|
|
1692
|
-
*
|
|
2285
|
+
* Recursively strip unsupported JSON Schema properties for Gemini
|
|
2286
|
+
* Gemini doesn't support: additionalProperties, $schema, etc.
|
|
1693
2287
|
*/
|
|
1694
|
-
|
|
2288
|
+
private static _cleanSchemaForGemini;
|
|
2289
|
+
private static _geminiStreamWithTools;
|
|
2290
|
+
private static _openaiText;
|
|
2291
|
+
private static _openaiStream;
|
|
2292
|
+
private static _openaiStreamWithTools;
|
|
1695
2293
|
/**
|
|
1696
|
-
*
|
|
2294
|
+
* Parse JSON string, handling markdown code blocks and surrounding text
|
|
2295
|
+
* Enhanced version with jsonrepair to handle malformed JSON from LLMs
|
|
2296
|
+
* @param text - Text that may contain JSON wrapped in ```json...``` or with surrounding text
|
|
2297
|
+
* @returns Parsed JSON object or array
|
|
1697
2298
|
*/
|
|
1698
|
-
|
|
2299
|
+
private static _parseJSON;
|
|
2300
|
+
}
|
|
2301
|
+
|
|
2302
|
+
interface CapturedLog {
|
|
2303
|
+
timestamp: number;
|
|
2304
|
+
level: 'info' | 'error' | 'warn' | 'debug';
|
|
2305
|
+
message: string;
|
|
2306
|
+
type?: 'explanation' | 'query' | 'general';
|
|
2307
|
+
data?: Record<string, any>;
|
|
2308
|
+
}
|
|
2309
|
+
/**
|
|
2310
|
+
* UILogCollector captures logs during user prompt processing
|
|
2311
|
+
* and sends them to runtime via ui_logs message with uiBlockId as the message id
|
|
2312
|
+
* Logs are sent in real-time for streaming effect in the UI
|
|
2313
|
+
* Respects the global log level configuration
|
|
2314
|
+
*/
|
|
2315
|
+
declare class UILogCollector {
|
|
2316
|
+
private logs;
|
|
2317
|
+
private uiBlockId;
|
|
2318
|
+
private clientId;
|
|
2319
|
+
private sendMessage;
|
|
2320
|
+
private currentLogLevel;
|
|
2321
|
+
constructor(clientId: string, sendMessage: (message: Message) => void, uiBlockId?: string);
|
|
1699
2322
|
/**
|
|
1700
|
-
*
|
|
2323
|
+
* Check if logging is enabled (uiBlockId is provided)
|
|
1701
2324
|
*/
|
|
1702
|
-
|
|
2325
|
+
isEnabled(): boolean;
|
|
1703
2326
|
/**
|
|
1704
|
-
*
|
|
2327
|
+
* Check if a message should be logged based on current log level
|
|
1705
2328
|
*/
|
|
1706
|
-
|
|
2329
|
+
private shouldLog;
|
|
1707
2330
|
/**
|
|
1708
|
-
*
|
|
1709
|
-
*
|
|
1710
|
-
* @returns Object with thread and uiBlock if found, undefined otherwise
|
|
2331
|
+
* Add a log entry with timestamp and immediately send to runtime
|
|
2332
|
+
* Only logs that pass the log level filter are captured and sent
|
|
1711
2333
|
*/
|
|
1712
|
-
|
|
1713
|
-
thread: Thread;
|
|
1714
|
-
uiBlock: UIBlock;
|
|
1715
|
-
} | undefined;
|
|
2334
|
+
private addLog;
|
|
1716
2335
|
/**
|
|
1717
|
-
*
|
|
2336
|
+
* Send a single log to runtime immediately
|
|
1718
2337
|
*/
|
|
1719
|
-
|
|
1720
|
-
}
|
|
1721
|
-
|
|
1722
|
-
/**
|
|
1723
|
-
* CleanupService handles cleanup of old threads and UIBlocks
|
|
1724
|
-
* to prevent memory bloat and maintain optimal performance
|
|
1725
|
-
*/
|
|
1726
|
-
declare class CleanupService {
|
|
1727
|
-
private static instance;
|
|
1728
|
-
private cleanupInterval;
|
|
1729
|
-
private constructor();
|
|
2338
|
+
private sendLogImmediately;
|
|
1730
2339
|
/**
|
|
1731
|
-
*
|
|
2340
|
+
* Log info message
|
|
1732
2341
|
*/
|
|
1733
|
-
|
|
2342
|
+
info(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
|
|
1734
2343
|
/**
|
|
1735
|
-
*
|
|
1736
|
-
* @param retentionDays - Number of days to keep threads (defaults to config)
|
|
1737
|
-
* @returns Number of threads deleted
|
|
2344
|
+
* Log error message
|
|
1738
2345
|
*/
|
|
1739
|
-
|
|
2346
|
+
error(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
|
|
1740
2347
|
/**
|
|
1741
|
-
*
|
|
1742
|
-
* @param retentionDays - Number of days to keep UIBlocks (defaults to config)
|
|
1743
|
-
* @returns Object with number of UIBlocks deleted per thread
|
|
2348
|
+
* Log warning message
|
|
1744
2349
|
*/
|
|
1745
|
-
|
|
1746
|
-
[threadId: string]: number;
|
|
1747
|
-
};
|
|
2350
|
+
warn(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
|
|
1748
2351
|
/**
|
|
1749
|
-
*
|
|
1750
|
-
* Keeps metadata but removes the actual data
|
|
1751
|
-
* @param retentionDays - Number of days to keep full data (defaults to config)
|
|
1752
|
-
* @returns Number of UIBlocks whose data was cleared
|
|
2352
|
+
* Log debug message
|
|
1753
2353
|
*/
|
|
1754
|
-
|
|
2354
|
+
debug(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
|
|
1755
2355
|
/**
|
|
1756
|
-
*
|
|
1757
|
-
* @returns Cleanup statistics
|
|
2356
|
+
* Log LLM explanation with typed metadata
|
|
1758
2357
|
*/
|
|
1759
|
-
|
|
1760
|
-
threadsDeleted: number;
|
|
1761
|
-
uiblocksDeleted: {
|
|
1762
|
-
[threadId: string]: number;
|
|
1763
|
-
};
|
|
1764
|
-
dataCleared: number;
|
|
1765
|
-
};
|
|
2358
|
+
logExplanation(message: string, explanation: string, data?: Record<string, any>): void;
|
|
1766
2359
|
/**
|
|
1767
|
-
*
|
|
1768
|
-
* @param intervalHours - Hours between cleanup runs (default: 24)
|
|
2360
|
+
* Log generated query with typed metadata
|
|
1769
2361
|
*/
|
|
1770
|
-
|
|
2362
|
+
logQuery(message: string, query: string, data?: Record<string, any>): void;
|
|
1771
2363
|
/**
|
|
1772
|
-
*
|
|
2364
|
+
* Send all collected logs at once (optional, for final summary)
|
|
1773
2365
|
*/
|
|
1774
|
-
|
|
2366
|
+
sendAllLogs(): void;
|
|
1775
2367
|
/**
|
|
1776
|
-
*
|
|
2368
|
+
* Get all collected logs
|
|
1777
2369
|
*/
|
|
1778
|
-
|
|
2370
|
+
getLogs(): CapturedLog[];
|
|
1779
2371
|
/**
|
|
1780
|
-
*
|
|
2372
|
+
* Clear all logs
|
|
1781
2373
|
*/
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
2374
|
+
clearLogs(): void;
|
|
2375
|
+
/**
|
|
2376
|
+
* Set uiBlockId (in case it's provided later)
|
|
2377
|
+
*/
|
|
2378
|
+
setUIBlockId(uiBlockId: string): void;
|
|
1787
2379
|
}
|
|
1788
2380
|
|
|
1789
2381
|
/**
|
|
1790
|
-
*
|
|
2382
|
+
* UIBlock represents a single user and assistant message block in a thread
|
|
2383
|
+
* Contains user question, component metadata, component data, text response, and available actions
|
|
1791
2384
|
*/
|
|
1792
|
-
declare
|
|
2385
|
+
declare class UIBlock {
|
|
2386
|
+
private id;
|
|
2387
|
+
private userQuestion;
|
|
2388
|
+
private generatedComponentMetadata;
|
|
2389
|
+
private componentData;
|
|
2390
|
+
private textResponse;
|
|
2391
|
+
private actions;
|
|
2392
|
+
private createdAt;
|
|
1793
2393
|
/**
|
|
1794
|
-
*
|
|
2394
|
+
* Creates a new UIBlock instance
|
|
2395
|
+
* @param userQuestion - The user's question or input
|
|
2396
|
+
* @param componentData - The component data object
|
|
2397
|
+
* @param generatedComponentMetadata - Optional metadata about the generated component
|
|
2398
|
+
* @param actions - Optional array of available actions
|
|
2399
|
+
* @param id - Optional custom ID, generates UUID if not provided
|
|
2400
|
+
* @param textResponse - Optional text response from LLM
|
|
1795
2401
|
*/
|
|
1796
|
-
|
|
2402
|
+
constructor(userQuestion: string, componentData?: Record<string, any>, generatedComponentMetadata?: Record<string, any>, actions?: Action[], id?: string, textResponse?: string | null);
|
|
1797
2403
|
/**
|
|
1798
|
-
*
|
|
2404
|
+
* Get the UIBlock ID
|
|
1799
2405
|
*/
|
|
1800
|
-
|
|
2406
|
+
getId(): string;
|
|
1801
2407
|
/**
|
|
1802
|
-
*
|
|
1803
|
-
* Note: This is for in-memory storage. Conversations are also persisted to database.
|
|
2408
|
+
* Get the user question
|
|
1804
2409
|
*/
|
|
1805
|
-
|
|
2410
|
+
getUserQuestion(): string;
|
|
1806
2411
|
/**
|
|
1807
|
-
*
|
|
1808
|
-
* Note: This is for in-memory storage. Data is also persisted to database.
|
|
2412
|
+
* Set or update the user question
|
|
1809
2413
|
*/
|
|
1810
|
-
|
|
1811
|
-
};
|
|
1812
|
-
|
|
1813
|
-
/**
|
|
1814
|
-
* Configuration for conversation context and history management
|
|
1815
|
-
*/
|
|
1816
|
-
declare const CONTEXT_CONFIG: {
|
|
2414
|
+
setUserQuestion(question: string): void;
|
|
1817
2415
|
/**
|
|
1818
|
-
*
|
|
1819
|
-
* Set to 0 to disable conversation history
|
|
1820
|
-
* Higher values provide more context but may increase token usage
|
|
2416
|
+
* Get component metadata
|
|
1821
2417
|
*/
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
/**
|
|
1826
|
-
* LLM Usage Logger - Tracks token usage, costs, and timing for all LLM API calls
|
|
1827
|
-
*/
|
|
1828
|
-
interface LLMUsageEntry {
|
|
1829
|
-
timestamp: string;
|
|
1830
|
-
requestId: string;
|
|
1831
|
-
provider: string;
|
|
1832
|
-
model: string;
|
|
1833
|
-
method: string;
|
|
1834
|
-
inputTokens: number;
|
|
1835
|
-
outputTokens: number;
|
|
1836
|
-
cacheReadTokens?: number;
|
|
1837
|
-
cacheWriteTokens?: number;
|
|
1838
|
-
totalTokens: number;
|
|
1839
|
-
costUSD: number;
|
|
1840
|
-
durationMs: number;
|
|
1841
|
-
toolCalls?: number;
|
|
1842
|
-
success: boolean;
|
|
1843
|
-
error?: string;
|
|
1844
|
-
}
|
|
1845
|
-
declare class LLMUsageLogger {
|
|
1846
|
-
private logStream;
|
|
1847
|
-
private logPath;
|
|
1848
|
-
private enabled;
|
|
1849
|
-
private sessionStats;
|
|
1850
|
-
constructor();
|
|
1851
|
-
private initLogStream;
|
|
1852
|
-
private writeHeader;
|
|
2418
|
+
getComponentMetadata(): Record<string, any>;
|
|
2419
|
+
getTextResponse(): string;
|
|
1853
2420
|
/**
|
|
1854
|
-
*
|
|
2421
|
+
* Set or update component metadata
|
|
1855
2422
|
*/
|
|
1856
|
-
|
|
2423
|
+
setComponentMetadata(metadata: Record<string, any>): void;
|
|
1857
2424
|
/**
|
|
1858
|
-
*
|
|
2425
|
+
* Get component data
|
|
1859
2426
|
*/
|
|
1860
|
-
|
|
2427
|
+
getComponentData(): Record<string, any>;
|
|
1861
2428
|
/**
|
|
1862
|
-
*
|
|
2429
|
+
* Calculate size of data in bytes
|
|
1863
2430
|
*/
|
|
1864
|
-
|
|
2431
|
+
private getDataSizeInBytes;
|
|
1865
2432
|
/**
|
|
1866
|
-
*
|
|
2433
|
+
* Limit array data to maximum rows
|
|
1867
2434
|
*/
|
|
1868
|
-
|
|
2435
|
+
private limitArrayData;
|
|
1869
2436
|
/**
|
|
1870
|
-
*
|
|
1871
|
-
* Call this at the start of each USER_PROMPT_REQ
|
|
2437
|
+
* Check if data exceeds size limit
|
|
1872
2438
|
*/
|
|
1873
|
-
|
|
2439
|
+
private exceedsSizeLimit;
|
|
1874
2440
|
/**
|
|
1875
|
-
*
|
|
2441
|
+
* Process and limit data before storing
|
|
1876
2442
|
*/
|
|
1877
|
-
|
|
1878
|
-
totalCalls: number;
|
|
1879
|
-
totalInputTokens: number;
|
|
1880
|
-
totalOutputTokens: number;
|
|
1881
|
-
totalCacheReadTokens: number;
|
|
1882
|
-
totalCacheWriteTokens: number;
|
|
1883
|
-
totalCostUSD: number;
|
|
1884
|
-
totalDurationMs: number;
|
|
1885
|
-
};
|
|
2443
|
+
private processDataForStorage;
|
|
1886
2444
|
/**
|
|
1887
|
-
*
|
|
2445
|
+
* Set or update component data with size and row limits
|
|
1888
2446
|
*/
|
|
1889
|
-
|
|
2447
|
+
setComponentData(data: Record<string, any>): void;
|
|
2448
|
+
/**
|
|
2449
|
+
* Set or update text response
|
|
2450
|
+
*/
|
|
2451
|
+
setTextResponse(textResponse: string | null): void;
|
|
2452
|
+
/**
|
|
2453
|
+
* Get all actions (only if they are resolved, not if fetching)
|
|
2454
|
+
*/
|
|
2455
|
+
getActions(): Action[] | null | Promise<Action[]>;
|
|
2456
|
+
/**
|
|
2457
|
+
* Get or fetch actions
|
|
2458
|
+
* If actions don't exist or are a Promise, calls the generateFn and stores the promise
|
|
2459
|
+
* If actions already exist, returns them
|
|
2460
|
+
* @param generateFn - Async function to generate actions
|
|
2461
|
+
* @returns Promise resolving to Action[]
|
|
2462
|
+
*/
|
|
2463
|
+
getOrFetchActions(generateFn: () => Promise<Action[]>): Promise<Action[]>;
|
|
2464
|
+
/**
|
|
2465
|
+
* Set or replace all actions
|
|
2466
|
+
*/
|
|
2467
|
+
setActions(actions: Action[]): void;
|
|
2468
|
+
/**
|
|
2469
|
+
* Add a single action (only if actions are resolved)
|
|
2470
|
+
*/
|
|
2471
|
+
addAction(action: Action): void;
|
|
2472
|
+
/**
|
|
2473
|
+
* Add multiple actions (only if actions are resolved)
|
|
2474
|
+
*/
|
|
2475
|
+
addActions(actions: Action[]): void;
|
|
2476
|
+
/**
|
|
2477
|
+
* Remove an action by ID (only if actions are resolved)
|
|
2478
|
+
*/
|
|
2479
|
+
removeAction(actionId: string): boolean;
|
|
2480
|
+
/**
|
|
2481
|
+
* Clear all actions
|
|
2482
|
+
*/
|
|
2483
|
+
clearActions(): void;
|
|
2484
|
+
/**
|
|
2485
|
+
* Get creation timestamp
|
|
2486
|
+
*/
|
|
2487
|
+
getCreatedAt(): Date;
|
|
2488
|
+
/**
|
|
2489
|
+
* Convert UIBlock to JSON-serializable object
|
|
2490
|
+
*/
|
|
2491
|
+
toJSON(): Record<string, any>;
|
|
1890
2492
|
}
|
|
1891
|
-
declare const llmUsageLogger: LLMUsageLogger;
|
|
1892
2493
|
|
|
1893
2494
|
/**
|
|
1894
|
-
*
|
|
1895
|
-
*
|
|
2495
|
+
* Thread represents a conversation thread containing multiple UIBlocks
|
|
2496
|
+
* Each UIBlock in a thread represents a user question and assistant response pair
|
|
1896
2497
|
*/
|
|
1897
|
-
declare class
|
|
1898
|
-
private
|
|
1899
|
-
private
|
|
1900
|
-
private
|
|
1901
|
-
private hasErrors;
|
|
1902
|
-
constructor();
|
|
2498
|
+
declare class Thread {
|
|
2499
|
+
private id;
|
|
2500
|
+
private uiblocks;
|
|
2501
|
+
private createdAt;
|
|
1903
2502
|
/**
|
|
1904
|
-
*
|
|
2503
|
+
* Creates a new Thread instance
|
|
2504
|
+
* @param id - Optional custom ID, generates UUID if not provided
|
|
1905
2505
|
*/
|
|
1906
|
-
|
|
2506
|
+
constructor(id?: string);
|
|
1907
2507
|
/**
|
|
1908
|
-
*
|
|
2508
|
+
* Get the thread ID
|
|
1909
2509
|
*/
|
|
1910
|
-
|
|
2510
|
+
getId(): string;
|
|
1911
2511
|
/**
|
|
1912
|
-
*
|
|
2512
|
+
* Add a UIBlock to the thread
|
|
1913
2513
|
*/
|
|
1914
|
-
|
|
2514
|
+
addUIBlock(uiblock: UIBlock): void;
|
|
1915
2515
|
/**
|
|
1916
|
-
*
|
|
2516
|
+
* Get a UIBlock by ID
|
|
1917
2517
|
*/
|
|
1918
|
-
|
|
2518
|
+
getUIBlock(id: string): UIBlock | undefined;
|
|
1919
2519
|
/**
|
|
1920
|
-
*
|
|
2520
|
+
* Get all UIBlocks in the thread
|
|
1921
2521
|
*/
|
|
1922
|
-
|
|
2522
|
+
getUIBlocks(): UIBlock[];
|
|
1923
2523
|
/**
|
|
1924
|
-
*
|
|
2524
|
+
* Get UIBlocks as a Map
|
|
1925
2525
|
*/
|
|
1926
|
-
|
|
2526
|
+
getUIBlocksMap(): Map<string, UIBlock>;
|
|
1927
2527
|
/**
|
|
1928
|
-
*
|
|
2528
|
+
* Remove a UIBlock by ID
|
|
1929
2529
|
*/
|
|
1930
|
-
|
|
2530
|
+
removeUIBlock(id: string): boolean;
|
|
1931
2531
|
/**
|
|
1932
|
-
* Check if
|
|
2532
|
+
* Check if UIBlock exists
|
|
1933
2533
|
*/
|
|
1934
|
-
|
|
1935
|
-
|
|
2534
|
+
hasUIBlock(id: string): boolean;
|
|
2535
|
+
/**
|
|
2536
|
+
* Get number of UIBlocks in the thread
|
|
2537
|
+
*/
|
|
2538
|
+
getUIBlockCount(): number;
|
|
2539
|
+
/**
|
|
2540
|
+
* Clear all UIBlocks from the thread
|
|
2541
|
+
*/
|
|
2542
|
+
clear(): void;
|
|
2543
|
+
/**
|
|
2544
|
+
* Get creation timestamp
|
|
2545
|
+
*/
|
|
2546
|
+
getCreatedAt(): Date;
|
|
2547
|
+
/**
|
|
2548
|
+
* Get conversation context from recent UIBlocks (excluding current one)
|
|
2549
|
+
* Returns formatted string with previous questions and component summaries
|
|
2550
|
+
* @param limit - Maximum number of previous UIBlocks to include (default: 5)
|
|
2551
|
+
* @param currentUIBlockId - ID of current UIBlock to exclude from context (optional)
|
|
2552
|
+
* @returns Formatted conversation history string
|
|
2553
|
+
*/
|
|
2554
|
+
getConversationContext(limit?: number, currentUIBlockId?: string): string;
|
|
2555
|
+
/**
|
|
2556
|
+
* Convert Thread to JSON-serializable object
|
|
2557
|
+
*/
|
|
2558
|
+
toJSON(): Record<string, any>;
|
|
1936
2559
|
}
|
|
1937
|
-
declare const userPromptErrorLogger: UserPromptErrorLogger;
|
|
1938
2560
|
|
|
1939
2561
|
/**
|
|
1940
|
-
*
|
|
1941
|
-
*
|
|
1942
|
-
*
|
|
1943
|
-
* long documents and term frequency saturation. This implementation is
|
|
1944
|
-
* designed to rerank semantic search results from ChromaDB.
|
|
1945
|
-
*
|
|
1946
|
-
* The hybrid approach combines:
|
|
1947
|
-
* 1. Semantic similarity from ChromaDB embeddings (dense vectors)
|
|
1948
|
-
* 2. Lexical matching from BM25L (sparse, keyword-based)
|
|
1949
|
-
*
|
|
1950
|
-
* This addresses the weakness of pure semantic search which may miss
|
|
1951
|
-
* exact keyword matches that are important for user intent.
|
|
1952
|
-
*/
|
|
1953
|
-
interface BM25LOptions {
|
|
1954
|
-
/** Term frequency saturation parameter (default: 1.5) */
|
|
1955
|
-
k1?: number;
|
|
1956
|
-
/** Length normalization parameter (default: 0.75) */
|
|
1957
|
-
b?: number;
|
|
1958
|
-
/** Lower-bound adjustment from BM25L paper (default: 0.5) */
|
|
1959
|
-
delta?: number;
|
|
1960
|
-
}
|
|
1961
|
-
interface RerankedResult<T> {
|
|
1962
|
-
item: T;
|
|
1963
|
-
originalIndex: number;
|
|
1964
|
-
semanticScore: number;
|
|
1965
|
-
bm25Score: number;
|
|
1966
|
-
hybridScore: number;
|
|
1967
|
-
}
|
|
1968
|
-
interface HybridSearchOptions extends BM25LOptions {
|
|
1969
|
-
/** Weight for semantic score (0-1, default: 0.7) */
|
|
1970
|
-
semanticWeight?: number;
|
|
1971
|
-
/** Weight for BM25 score (0-1, default: 0.3) */
|
|
1972
|
-
bm25Weight?: number;
|
|
1973
|
-
/** Minimum hybrid score threshold (0-1, default: 0) */
|
|
1974
|
-
minScore?: number;
|
|
1975
|
-
}
|
|
1976
|
-
/**
|
|
1977
|
-
* BM25L implementation for lexical scoring
|
|
2562
|
+
* ThreadManager manages all threads globally
|
|
2563
|
+
* Provides methods to create, retrieve, and delete threads.
|
|
2564
|
+
* Includes automatic cleanup to prevent unbounded memory growth.
|
|
1978
2565
|
*/
|
|
1979
|
-
declare class
|
|
1980
|
-
private
|
|
1981
|
-
private
|
|
1982
|
-
private
|
|
1983
|
-
private
|
|
1984
|
-
private
|
|
1985
|
-
private avgDocLength;
|
|
1986
|
-
private termDocFreq;
|
|
2566
|
+
declare class ThreadManager {
|
|
2567
|
+
private static instance;
|
|
2568
|
+
private threads;
|
|
2569
|
+
private cleanupInterval;
|
|
2570
|
+
private readonly threadTtlMs;
|
|
2571
|
+
private constructor();
|
|
1987
2572
|
/**
|
|
1988
|
-
*
|
|
1989
|
-
*
|
|
2573
|
+
* Periodically remove threads older than 7 days.
|
|
2574
|
+
* Runs every hour to avoid frequent iteration over the map.
|
|
1990
2575
|
*/
|
|
1991
|
-
|
|
2576
|
+
private startCleanup;
|
|
1992
2577
|
/**
|
|
1993
|
-
*
|
|
2578
|
+
* Get singleton instance of ThreadManager
|
|
1994
2579
|
*/
|
|
1995
|
-
|
|
2580
|
+
static getInstance(): ThreadManager;
|
|
1996
2581
|
/**
|
|
1997
|
-
*
|
|
2582
|
+
* Create a new thread
|
|
2583
|
+
* @param id - Optional custom ID, generates UUID if not provided
|
|
2584
|
+
* @returns The created Thread instance
|
|
1998
2585
|
*/
|
|
1999
|
-
|
|
2586
|
+
createThread(id?: string): Thread;
|
|
2000
2587
|
/**
|
|
2001
|
-
*
|
|
2588
|
+
* Get a thread by ID
|
|
2002
2589
|
*/
|
|
2003
|
-
|
|
2590
|
+
getThread(id: string): Thread | undefined;
|
|
2004
2591
|
/**
|
|
2005
|
-
*
|
|
2592
|
+
* Get all threads
|
|
2006
2593
|
*/
|
|
2007
|
-
|
|
2008
|
-
index: number;
|
|
2009
|
-
score: number;
|
|
2010
|
-
}>;
|
|
2011
|
-
}
|
|
2012
|
-
/**
|
|
2013
|
-
* Hybrid reranker that combines semantic and BM25L scores
|
|
2014
|
-
*
|
|
2015
|
-
* @param query - The search query
|
|
2016
|
-
* @param items - Array of items to rerank
|
|
2017
|
-
* @param getDocument - Function to extract document text from an item
|
|
2018
|
-
* @param getSemanticScore - Function to extract semantic similarity score from an item
|
|
2019
|
-
* @param options - Hybrid search options
|
|
2020
|
-
* @returns Reranked items with hybrid scores
|
|
2021
|
-
*/
|
|
2022
|
-
declare function hybridRerank<T>(query: string, items: T[], getDocument: (item: T) => string, getSemanticScore: (item: T) => number, options?: HybridSearchOptions): RerankedResult<T>[];
|
|
2023
|
-
/**
|
|
2024
|
-
* Simple reranking function for ChromaDB results
|
|
2025
|
-
*
|
|
2026
|
-
* This is a convenience wrapper for reranking ChromaDB query results
|
|
2027
|
-
* that follow the standard { ids, documents, metadatas, distances } format.
|
|
2028
|
-
*
|
|
2029
|
-
* @param query - The search query
|
|
2030
|
-
* @param chromaResults - ChromaDB query results
|
|
2031
|
-
* @param options - Hybrid search options
|
|
2032
|
-
* @returns Reranked results with hybrid scores
|
|
2033
|
-
*/
|
|
2034
|
-
declare function rerankChromaResults(query: string, chromaResults: {
|
|
2035
|
-
ids: string[][];
|
|
2036
|
-
documents: (string | null)[][];
|
|
2037
|
-
metadatas: Record<string, any>[][];
|
|
2038
|
-
distances: number[][];
|
|
2039
|
-
}, options?: HybridSearchOptions): Array<{
|
|
2040
|
-
id: string;
|
|
2041
|
-
document: string | null;
|
|
2042
|
-
metadata: Record<string, any>;
|
|
2043
|
-
distance: number;
|
|
2044
|
-
semanticScore: number;
|
|
2045
|
-
bm25Score: number;
|
|
2046
|
-
hybridScore: number;
|
|
2047
|
-
}>;
|
|
2048
|
-
/**
|
|
2049
|
-
* Rerank conversation search results specifically
|
|
2050
|
-
*
|
|
2051
|
-
* This function is designed to work with the conversation-history.search collection
|
|
2052
|
-
* where we need to fetch more results initially and then rerank them.
|
|
2053
|
-
*
|
|
2054
|
-
* @param query - The user's search query
|
|
2055
|
-
* @param results - Array of conversation search results from ChromaDB
|
|
2056
|
-
* @param options - Hybrid search options
|
|
2057
|
-
* @returns Reranked results sorted by hybrid score
|
|
2058
|
-
*/
|
|
2059
|
-
declare function rerankConversationResults<T extends {
|
|
2060
|
-
userPrompt?: string;
|
|
2061
|
-
similarity?: number;
|
|
2062
|
-
}>(query: string, results: T[], options?: HybridSearchOptions): Array<T & {
|
|
2063
|
-
hybridScore: number;
|
|
2064
|
-
bm25Score: number;
|
|
2065
|
-
}>;
|
|
2066
|
-
|
|
2067
|
-
/**
|
|
2068
|
-
* QueryExecutionService - Handles all query execution, validation, and retry logic
|
|
2069
|
-
* Extracted from BaseLLM for better separation of concerns
|
|
2070
|
-
*/
|
|
2071
|
-
|
|
2072
|
-
/**
|
|
2073
|
-
* Context for component when requesting query fix
|
|
2074
|
-
*/
|
|
2075
|
-
interface ComponentContext {
|
|
2076
|
-
name: string;
|
|
2077
|
-
type: string;
|
|
2078
|
-
title?: string;
|
|
2079
|
-
}
|
|
2080
|
-
/**
|
|
2081
|
-
* Result of query validation
|
|
2082
|
-
*/
|
|
2083
|
-
interface QueryValidationResult {
|
|
2084
|
-
component: Component | null;
|
|
2085
|
-
queryKey: string;
|
|
2086
|
-
result: any;
|
|
2087
|
-
validated: boolean;
|
|
2088
|
-
}
|
|
2089
|
-
/**
|
|
2090
|
-
* Result of batch query validation
|
|
2091
|
-
*/
|
|
2092
|
-
interface BatchValidationResult {
|
|
2093
|
-
components: Component[];
|
|
2094
|
-
queryResults: Map<string, any>;
|
|
2095
|
-
}
|
|
2096
|
-
/**
|
|
2097
|
-
* Configuration for QueryExecutionService
|
|
2098
|
-
*/
|
|
2099
|
-
interface QueryExecutionServiceConfig {
|
|
2100
|
-
defaultLimit: number;
|
|
2101
|
-
getModelForTask: (taskType: 'simple' | 'complex') => string;
|
|
2102
|
-
getApiKey: (apiKey?: string) => string | undefined;
|
|
2103
|
-
providerName: string;
|
|
2104
|
-
}
|
|
2105
|
-
/**
|
|
2106
|
-
* QueryExecutionService handles all query-related operations
|
|
2107
|
-
*/
|
|
2108
|
-
declare class QueryExecutionService {
|
|
2109
|
-
private config;
|
|
2110
|
-
constructor(config: QueryExecutionServiceConfig);
|
|
2594
|
+
getAllThreads(): Thread[];
|
|
2111
2595
|
/**
|
|
2112
|
-
* Get
|
|
2113
|
-
* This ensures the cache key matches what the frontend will send
|
|
2596
|
+
* Get threads as a Map
|
|
2114
2597
|
*/
|
|
2115
|
-
|
|
2598
|
+
getThreadsMap(): Map<string, Thread>;
|
|
2116
2599
|
/**
|
|
2117
|
-
*
|
|
2118
|
-
* @param query - The SQL query to execute (string or object with sql/values)
|
|
2119
|
-
* @param collections - Collections object containing database execute function
|
|
2120
|
-
* @returns Object with result data and cache key
|
|
2600
|
+
* Delete a thread by ID
|
|
2121
2601
|
*/
|
|
2122
|
-
|
|
2123
|
-
result: any;
|
|
2124
|
-
cacheKey: string;
|
|
2125
|
-
}>;
|
|
2602
|
+
deleteThread(id: string): boolean;
|
|
2126
2603
|
/**
|
|
2127
|
-
*
|
|
2128
|
-
* @param failedQuery - The query that failed execution
|
|
2129
|
-
* @param errorMessage - The error message from the failed execution
|
|
2130
|
-
* @param componentContext - Context about the component
|
|
2131
|
-
* @param apiKey - Optional API key
|
|
2132
|
-
* @returns Fixed query string
|
|
2604
|
+
* Check if thread exists
|
|
2133
2605
|
*/
|
|
2134
|
-
|
|
2606
|
+
hasThread(id: string): boolean;
|
|
2135
2607
|
/**
|
|
2136
|
-
*
|
|
2137
|
-
* @param component - The component to validate
|
|
2138
|
-
* @param collections - Collections object containing database execute function
|
|
2139
|
-
* @param apiKey - Optional API key for LLM calls
|
|
2140
|
-
* @returns Validation result with component, query key, and result
|
|
2608
|
+
* Get number of threads
|
|
2141
2609
|
*/
|
|
2142
|
-
|
|
2610
|
+
getThreadCount(): number;
|
|
2143
2611
|
/**
|
|
2144
|
-
*
|
|
2145
|
-
* @param components - Array of components with potential queries
|
|
2146
|
-
* @param collections - Collections object containing database execute function
|
|
2147
|
-
* @param apiKey - Optional API key for LLM calls
|
|
2148
|
-
* @returns Object with validated components and query results map
|
|
2612
|
+
* Clear all threads
|
|
2149
2613
|
*/
|
|
2150
|
-
|
|
2614
|
+
clearAll(): void;
|
|
2615
|
+
/**
|
|
2616
|
+
* Find a UIBlock by ID across all threads
|
|
2617
|
+
* @param uiBlockId - The UIBlock ID to search for
|
|
2618
|
+
* @returns Object with thread and uiBlock if found, undefined otherwise
|
|
2619
|
+
*/
|
|
2620
|
+
findUIBlockById(uiBlockId: string): {
|
|
2621
|
+
thread: Thread;
|
|
2622
|
+
uiBlock: UIBlock;
|
|
2623
|
+
} | undefined;
|
|
2624
|
+
/**
|
|
2625
|
+
* Convert all threads to JSON-serializable object
|
|
2626
|
+
*/
|
|
2627
|
+
toJSON(): Record<string, any>;
|
|
2151
2628
|
}
|
|
2152
2629
|
|
|
2153
2630
|
/**
|
|
2154
|
-
*
|
|
2155
|
-
*
|
|
2156
|
-
*/
|
|
2157
|
-
type StreamCallback = (chunk: string) => void;
|
|
2158
|
-
/**
|
|
2159
|
-
* StreamBuffer class for managing buffered streaming output
|
|
2160
|
-
* Provides smooth text delivery by batching small chunks
|
|
2631
|
+
* CleanupService handles cleanup of old threads and UIBlocks
|
|
2632
|
+
* to prevent memory bloat and maintain optimal performance
|
|
2161
2633
|
*/
|
|
2162
|
-
declare class
|
|
2163
|
-
private
|
|
2164
|
-
private
|
|
2165
|
-
private
|
|
2166
|
-
private fullText;
|
|
2167
|
-
constructor(callback?: StreamCallback);
|
|
2634
|
+
declare class CleanupService {
|
|
2635
|
+
private static instance;
|
|
2636
|
+
private cleanupInterval;
|
|
2637
|
+
private constructor();
|
|
2168
2638
|
/**
|
|
2169
|
-
*
|
|
2639
|
+
* Get singleton instance of CleanupService
|
|
2170
2640
|
*/
|
|
2171
|
-
|
|
2641
|
+
static getInstance(): CleanupService;
|
|
2172
2642
|
/**
|
|
2173
|
-
*
|
|
2643
|
+
* Clean up old threads based on retention period
|
|
2644
|
+
* @param retentionDays - Number of days to keep threads (defaults to config)
|
|
2645
|
+
* @returns Number of threads deleted
|
|
2174
2646
|
*/
|
|
2175
|
-
|
|
2647
|
+
cleanupOldThreads(retentionDays?: number): number;
|
|
2176
2648
|
/**
|
|
2177
|
-
*
|
|
2178
|
-
*
|
|
2179
|
-
*
|
|
2180
|
-
*
|
|
2181
|
-
* @param chunk - Text chunk to write
|
|
2649
|
+
* Clean up old UIBlocks within threads based on retention period
|
|
2650
|
+
* @param retentionDays - Number of days to keep UIBlocks (defaults to config)
|
|
2651
|
+
* @returns Object with number of UIBlocks deleted per thread
|
|
2182
2652
|
*/
|
|
2183
|
-
|
|
2653
|
+
cleanupOldUIBlocks(retentionDays?: number): {
|
|
2654
|
+
[threadId: string]: number;
|
|
2655
|
+
};
|
|
2184
2656
|
/**
|
|
2185
|
-
*
|
|
2186
|
-
*
|
|
2657
|
+
* Clear all component data from UIBlocks to free memory
|
|
2658
|
+
* Keeps metadata but removes the actual data
|
|
2659
|
+
* @param retentionDays - Number of days to keep full data (defaults to config)
|
|
2660
|
+
* @returns Number of UIBlocks whose data was cleared
|
|
2187
2661
|
*/
|
|
2188
|
-
|
|
2662
|
+
clearOldUIBlockData(retentionDays?: number): number;
|
|
2189
2663
|
/**
|
|
2190
|
-
*
|
|
2664
|
+
* Run full cleanup (threads, UIBlocks, and data)
|
|
2665
|
+
* @returns Cleanup statistics
|
|
2191
2666
|
*/
|
|
2192
|
-
|
|
2667
|
+
runFullCleanup(): {
|
|
2668
|
+
threadsDeleted: number;
|
|
2669
|
+
uiblocksDeleted: {
|
|
2670
|
+
[threadId: string]: number;
|
|
2671
|
+
};
|
|
2672
|
+
dataCleared: number;
|
|
2673
|
+
};
|
|
2193
2674
|
/**
|
|
2194
|
-
*
|
|
2195
|
-
*
|
|
2675
|
+
* Start automatic cleanup at regular intervals
|
|
2676
|
+
* @param intervalHours - Hours between cleanup runs (default: 24)
|
|
2196
2677
|
*/
|
|
2197
|
-
|
|
2678
|
+
startAutoCleanup(intervalHours?: number): void;
|
|
2679
|
+
/**
|
|
2680
|
+
* Stop automatic cleanup
|
|
2681
|
+
*/
|
|
2682
|
+
stopAutoCleanup(): void;
|
|
2683
|
+
/**
|
|
2684
|
+
* Check if auto cleanup is running
|
|
2685
|
+
*/
|
|
2686
|
+
isAutoCleanupRunning(): boolean;
|
|
2687
|
+
/**
|
|
2688
|
+
* Get current memory usage statistics
|
|
2689
|
+
*/
|
|
2690
|
+
getMemoryStats(): {
|
|
2691
|
+
threadCount: number;
|
|
2692
|
+
totalUIBlocks: number;
|
|
2693
|
+
avgUIBlocksPerThread: number;
|
|
2694
|
+
};
|
|
2198
2695
|
}
|
|
2199
2696
|
|
|
2200
2697
|
/**
|
|
2201
|
-
*
|
|
2202
|
-
* Extracted from BaseLLM.generateTextResponse for better separation of concerns
|
|
2203
|
-
*/
|
|
2204
|
-
|
|
2205
|
-
/**
|
|
2206
|
-
* External tool definition
|
|
2698
|
+
* Configuration for data storage limits in UIBlocks
|
|
2207
2699
|
*/
|
|
2208
|
-
|
|
2209
|
-
|
|
2210
|
-
|
|
2211
|
-
|
|
2212
|
-
|
|
2213
|
-
|
|
2214
|
-
|
|
2215
|
-
|
|
2216
|
-
|
|
2217
|
-
|
|
2218
|
-
|
|
2219
|
-
|
|
2220
|
-
|
|
2221
|
-
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
* Executed tool tracking info
|
|
2229
|
-
*/
|
|
2230
|
-
interface ExecutedToolInfo {
|
|
2231
|
-
id: string;
|
|
2232
|
-
name: string;
|
|
2233
|
-
params: any;
|
|
2234
|
-
result: {
|
|
2235
|
-
_totalRecords: number;
|
|
2236
|
-
_recordsShown: number;
|
|
2237
|
-
_metadata?: any;
|
|
2238
|
-
_sampleData: any[];
|
|
2239
|
-
};
|
|
2240
|
-
outputSchema?: any;
|
|
2241
|
-
sourceSchema?: string;
|
|
2242
|
-
sourceType?: string;
|
|
2243
|
-
}
|
|
2700
|
+
declare const STORAGE_CONFIG: {
|
|
2701
|
+
/**
|
|
2702
|
+
* Maximum number of rows to store in UIBlock data
|
|
2703
|
+
*/
|
|
2704
|
+
MAX_ROWS_PER_BLOCK: number;
|
|
2705
|
+
/**
|
|
2706
|
+
* Maximum size in bytes per UIBlock (500KB - reduced to save memory)
|
|
2707
|
+
*/
|
|
2708
|
+
MAX_SIZE_PER_BLOCK_BYTES: number;
|
|
2709
|
+
/**
|
|
2710
|
+
* Number of days to keep threads before cleanup
|
|
2711
|
+
* Note: This is for in-memory storage. Conversations are also persisted to database.
|
|
2712
|
+
*/
|
|
2713
|
+
THREAD_RETENTION_DAYS: number;
|
|
2714
|
+
/**
|
|
2715
|
+
* Number of days to keep UIBlocks before cleanup
|
|
2716
|
+
* Note: This is for in-memory storage. Data is also persisted to database.
|
|
2717
|
+
*/
|
|
2718
|
+
UIBLOCK_RETENTION_DAYS: number;
|
|
2719
|
+
};
|
|
2244
2720
|
|
|
2245
2721
|
/**
|
|
2246
|
-
*
|
|
2247
|
-
* - 'complex': Text generation, component matching, parameter adaptation (uses best model in balanced mode)
|
|
2248
|
-
* - 'simple': Classification, action generation (uses fast model in balanced mode)
|
|
2722
|
+
* Configuration for conversation context and history management
|
|
2249
2723
|
*/
|
|
2250
|
-
|
|
2251
|
-
interface BaseLLMConfig {
|
|
2252
|
-
model?: string;
|
|
2253
|
-
fastModel?: string;
|
|
2254
|
-
defaultLimit?: number;
|
|
2255
|
-
apiKey?: string;
|
|
2724
|
+
declare const CONTEXT_CONFIG: {
|
|
2256
2725
|
/**
|
|
2257
|
-
*
|
|
2258
|
-
*
|
|
2259
|
-
*
|
|
2260
|
-
* - 'balanced': Use best model for complex tasks, fast model for simple tasks (default)
|
|
2726
|
+
* Maximum number of previous UIBlocks to include as conversation context
|
|
2727
|
+
* Set to 0 to disable conversation history
|
|
2728
|
+
* Higher values provide more context but may increase token usage
|
|
2261
2729
|
*/
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2730
|
+
MAX_CONVERSATION_CONTEXT_BLOCKS: number;
|
|
2731
|
+
};
|
|
2732
|
+
|
|
2265
2733
|
/**
|
|
2266
|
-
*
|
|
2267
|
-
* Provides common functionality for all LLM providers
|
|
2734
|
+
* LLM Usage Logger - Tracks token usage, costs, and timing for all LLM API calls
|
|
2268
2735
|
*/
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
|
|
2273
|
-
|
|
2274
|
-
|
|
2275
|
-
|
|
2276
|
-
|
|
2277
|
-
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
|
|
2285
|
-
|
|
2286
|
-
|
|
2287
|
-
|
|
2288
|
-
|
|
2736
|
+
interface LLMUsageEntry {
|
|
2737
|
+
timestamp: string;
|
|
2738
|
+
requestId: string;
|
|
2739
|
+
provider: string;
|
|
2740
|
+
model: string;
|
|
2741
|
+
method: string;
|
|
2742
|
+
inputTokens: number;
|
|
2743
|
+
outputTokens: number;
|
|
2744
|
+
cacheReadTokens?: number;
|
|
2745
|
+
cacheWriteTokens?: number;
|
|
2746
|
+
totalTokens: number;
|
|
2747
|
+
costUSD: number;
|
|
2748
|
+
durationMs: number;
|
|
2749
|
+
toolCalls?: number;
|
|
2750
|
+
success: boolean;
|
|
2751
|
+
error?: string;
|
|
2752
|
+
}
|
|
2753
|
+
declare class LLMUsageLogger {
|
|
2754
|
+
private logStream;
|
|
2755
|
+
private logPath;
|
|
2756
|
+
private enabled;
|
|
2757
|
+
private sessionStats;
|
|
2758
|
+
constructor();
|
|
2759
|
+
private initLogStream;
|
|
2760
|
+
private writeHeader;
|
|
2289
2761
|
/**
|
|
2290
|
-
*
|
|
2291
|
-
* @returns The current model strategy
|
|
2762
|
+
* Calculate cost based on token usage and model
|
|
2292
2763
|
*/
|
|
2293
|
-
|
|
2764
|
+
calculateCost(model: string, inputTokens: number, outputTokens: number, cacheReadTokens?: number, cacheWriteTokens?: number): number;
|
|
2294
2765
|
/**
|
|
2295
|
-
*
|
|
2296
|
-
* @param threshold - Value between 0 and 1 (e.g., 0.8 = 80% similarity required)
|
|
2766
|
+
* Log an LLM API call
|
|
2297
2767
|
*/
|
|
2298
|
-
|
|
2768
|
+
log(entry: LLMUsageEntry): void;
|
|
2299
2769
|
/**
|
|
2300
|
-
*
|
|
2301
|
-
* @returns The current threshold value
|
|
2770
|
+
* Log session summary (call at end of request)
|
|
2302
2771
|
*/
|
|
2303
|
-
|
|
2772
|
+
logSessionSummary(requestContext?: string): void;
|
|
2304
2773
|
/**
|
|
2305
|
-
*
|
|
2774
|
+
* Reset session stats (call at start of new user request)
|
|
2306
2775
|
*/
|
|
2307
|
-
|
|
2776
|
+
resetSession(): void;
|
|
2308
2777
|
/**
|
|
2309
|
-
*
|
|
2310
|
-
*
|
|
2778
|
+
* Reset the log file for a new request (clears previous logs)
|
|
2779
|
+
* Call this at the start of each USER_PROMPT_REQ
|
|
2311
2780
|
*/
|
|
2312
|
-
|
|
2781
|
+
resetLogFile(requestContext?: string): void;
|
|
2313
2782
|
/**
|
|
2314
|
-
* Get
|
|
2783
|
+
* Get current session stats
|
|
2315
2784
|
*/
|
|
2316
|
-
|
|
2785
|
+
getSessionStats(): {
|
|
2786
|
+
totalCalls: number;
|
|
2787
|
+
totalInputTokens: number;
|
|
2788
|
+
totalOutputTokens: number;
|
|
2789
|
+
totalCacheReadTokens: number;
|
|
2790
|
+
totalCacheWriteTokens: number;
|
|
2791
|
+
totalCostUSD: number;
|
|
2792
|
+
totalDurationMs: number;
|
|
2793
|
+
};
|
|
2317
2794
|
/**
|
|
2318
|
-
*
|
|
2795
|
+
* Generate a unique request ID
|
|
2319
2796
|
*/
|
|
2320
|
-
|
|
2797
|
+
generateRequestId(): string;
|
|
2798
|
+
}
|
|
2799
|
+
declare const llmUsageLogger: LLMUsageLogger;
|
|
2800
|
+
|
|
2801
|
+
/**
|
|
2802
|
+
* User Prompt Error Logger - Captures detailed errors for USER_PROMPT_REQ
|
|
2803
|
+
* Logs full error details including raw strings for parse failures
|
|
2804
|
+
*/
|
|
2805
|
+
declare class UserPromptErrorLogger {
|
|
2806
|
+
private logStream;
|
|
2807
|
+
private logPath;
|
|
2808
|
+
private enabled;
|
|
2809
|
+
private hasErrors;
|
|
2810
|
+
constructor();
|
|
2321
2811
|
/**
|
|
2322
|
-
*
|
|
2812
|
+
* Reset the error log file for a new request
|
|
2323
2813
|
*/
|
|
2324
|
-
|
|
2814
|
+
resetLogFile(requestContext?: string): void;
|
|
2325
2815
|
/**
|
|
2326
|
-
*
|
|
2327
|
-
* Forms have hardcoded defaultValues that become stale when cached
|
|
2328
|
-
* This checks both single Form components and Forms inside MultiComponentContainer
|
|
2816
|
+
* Log a JSON parse error with the raw string that failed
|
|
2329
2817
|
*/
|
|
2330
|
-
|
|
2818
|
+
logJsonParseError(context: string, rawString: string, error: Error): void;
|
|
2331
2819
|
/**
|
|
2332
|
-
*
|
|
2333
|
-
* Takes a text response with component suggestions (c1:type format) and matches with available components
|
|
2334
|
-
* Also generates title, description, and intelligent follow-up questions (actions) based on the analysis
|
|
2335
|
-
* All components are placed in a default MultiComponentContainer layout
|
|
2336
|
-
* @param analysisContent - The text response containing component suggestions
|
|
2337
|
-
* @param components - List of available components
|
|
2338
|
-
* @param apiKey - Optional API key
|
|
2339
|
-
* @param componentStreamCallback - Optional callback to stream primary KPI component as soon as it's identified
|
|
2340
|
-
* @returns Object containing matched components, layout title/description, and follow-up actions
|
|
2820
|
+
* Log a general error with full details
|
|
2341
2821
|
*/
|
|
2342
|
-
|
|
2343
|
-
components: Component[];
|
|
2344
|
-
layoutTitle: string;
|
|
2345
|
-
layoutDescription: string;
|
|
2346
|
-
actions: Action[];
|
|
2347
|
-
}>;
|
|
2822
|
+
logError(context: string, error: Error | string, additionalData?: Record<string, any>): void;
|
|
2348
2823
|
/**
|
|
2349
|
-
*
|
|
2350
|
-
* Determines if question is for data analysis, requires external tools, or needs text response
|
|
2824
|
+
* Log a SQL query error with the full query
|
|
2351
2825
|
*/
|
|
2352
|
-
|
|
2353
|
-
category: 'data_analysis' | 'data_modification' | 'general';
|
|
2354
|
-
externalTools: Array<{
|
|
2355
|
-
type: string;
|
|
2356
|
-
name: string;
|
|
2357
|
-
description: string;
|
|
2358
|
-
parameters: Record<string, any>;
|
|
2359
|
-
}>;
|
|
2360
|
-
dataAnalysisType?: 'visualization' | 'calculation' | 'comparison' | 'trend';
|
|
2361
|
-
reasoning: string;
|
|
2362
|
-
confidence: number;
|
|
2363
|
-
}>;
|
|
2826
|
+
logSqlError(query: string, error: Error | string, params?: any[]): void;
|
|
2364
2827
|
/**
|
|
2365
|
-
*
|
|
2366
|
-
* Takes a matched UI block from semantic search and modifies its props to answer the new question
|
|
2367
|
-
* Also adapts the cached text response to match the new question
|
|
2828
|
+
* Log an LLM API error
|
|
2368
2829
|
*/
|
|
2369
|
-
|
|
2370
|
-
success: boolean;
|
|
2371
|
-
adaptedComponent?: Component;
|
|
2372
|
-
adaptedTextResponse?: string;
|
|
2373
|
-
parametersChanged?: Array<{
|
|
2374
|
-
field: string;
|
|
2375
|
-
reason: string;
|
|
2376
|
-
}>;
|
|
2377
|
-
explanation: string;
|
|
2378
|
-
}>;
|
|
2830
|
+
logLlmError(provider: string, model: string, method: string, error: Error | string, requestData?: any): void;
|
|
2379
2831
|
/**
|
|
2380
|
-
*
|
|
2381
|
-
* This provides conversational text responses instead of component generation
|
|
2382
|
-
* Supports tool calling for query execution with automatic retry on errors (max 3 attempts)
|
|
2383
|
-
* After generating text response, if components are provided, matches suggested components
|
|
2832
|
+
* Log tool execution error
|
|
2384
2833
|
*/
|
|
2385
|
-
|
|
2834
|
+
logToolError(toolName: string, toolInput: any, error: Error | string): void;
|
|
2386
2835
|
/**
|
|
2387
|
-
*
|
|
2388
|
-
* NEW FLOW (Recommended):
|
|
2389
|
-
* 1. Semantic search: Check previous conversations (>60% match)
|
|
2390
|
-
* - If match found → Adapt UI block parameters and return
|
|
2391
|
-
* 2. Category classification: Determine if data_analysis, requires_external_tools, or text_response
|
|
2392
|
-
* 3. Route appropriately based on category and response mode
|
|
2836
|
+
* Write final summary if there were errors
|
|
2393
2837
|
*/
|
|
2394
|
-
|
|
2838
|
+
writeSummary(): void;
|
|
2395
2839
|
/**
|
|
2396
|
-
*
|
|
2397
|
-
* This helps provide intelligent suggestions for follow-up queries
|
|
2398
|
-
* For general/conversational questions without components, pass textResponse instead
|
|
2840
|
+
* Check if any errors were logged
|
|
2399
2841
|
*/
|
|
2400
|
-
|
|
2842
|
+
hadErrors(): boolean;
|
|
2843
|
+
private write;
|
|
2401
2844
|
}
|
|
2845
|
+
declare const userPromptErrorLogger: UserPromptErrorLogger;
|
|
2402
2846
|
|
|
2403
|
-
interface AnthropicLLMConfig extends BaseLLMConfig {
|
|
2404
|
-
}
|
|
2405
2847
|
/**
|
|
2406
|
-
*
|
|
2848
|
+
* BM25L Reranker for hybrid semantic search
|
|
2849
|
+
*
|
|
2850
|
+
* BM25L is an improved variant of BM25 that provides better handling of
|
|
2851
|
+
* long documents and term frequency saturation. This implementation is
|
|
2852
|
+
* designed to rerank semantic search results from ChromaDB.
|
|
2853
|
+
*
|
|
2854
|
+
* The hybrid approach combines:
|
|
2855
|
+
* 1. Semantic similarity from ChromaDB embeddings (dense vectors)
|
|
2856
|
+
* 2. Lexical matching from BM25L (sparse, keyword-based)
|
|
2857
|
+
*
|
|
2858
|
+
* This addresses the weakness of pure semantic search which may miss
|
|
2859
|
+
* exact keyword matches that are important for user intent.
|
|
2407
2860
|
*/
|
|
2408
|
-
|
|
2409
|
-
|
|
2410
|
-
|
|
2411
|
-
|
|
2412
|
-
|
|
2413
|
-
|
|
2861
|
+
interface BM25LOptions {
|
|
2862
|
+
/** Term frequency saturation parameter (default: 1.5) */
|
|
2863
|
+
k1?: number;
|
|
2864
|
+
/** Length normalization parameter (default: 0.75) */
|
|
2865
|
+
b?: number;
|
|
2866
|
+
/** Lower-bound adjustment from BM25L paper (default: 0.5) */
|
|
2867
|
+
delta?: number;
|
|
2414
2868
|
}
|
|
2415
|
-
|
|
2416
|
-
|
|
2417
|
-
|
|
2869
|
+
interface RerankedResult<T> {
|
|
2870
|
+
item: T;
|
|
2871
|
+
originalIndex: number;
|
|
2872
|
+
semanticScore: number;
|
|
2873
|
+
bm25Score: number;
|
|
2874
|
+
hybridScore: number;
|
|
2875
|
+
}
|
|
2876
|
+
interface HybridSearchOptions extends BM25LOptions {
|
|
2877
|
+
/** Weight for semantic score (0-1, default: 0.7) */
|
|
2878
|
+
semanticWeight?: number;
|
|
2879
|
+
/** Weight for BM25 score (0-1, default: 0.3) */
|
|
2880
|
+
bm25Weight?: number;
|
|
2881
|
+
/** Minimum hybrid score threshold (0-1, default: 0) */
|
|
2882
|
+
minScore?: number;
|
|
2418
2883
|
}
|
|
2419
2884
|
/**
|
|
2420
|
-
*
|
|
2885
|
+
* BM25L implementation for lexical scoring
|
|
2421
2886
|
*/
|
|
2422
|
-
declare class
|
|
2423
|
-
|
|
2424
|
-
|
|
2425
|
-
|
|
2426
|
-
|
|
2427
|
-
|
|
2887
|
+
declare class BM25L {
|
|
2888
|
+
private k1;
|
|
2889
|
+
private b;
|
|
2890
|
+
private delta;
|
|
2891
|
+
private documents;
|
|
2892
|
+
private docLengths;
|
|
2893
|
+
private avgDocLength;
|
|
2894
|
+
private termDocFreq;
|
|
2895
|
+
/**
|
|
2896
|
+
* @param documents - Array of raw documents (strings)
|
|
2897
|
+
* @param opts - Optional BM25L parameters
|
|
2898
|
+
*/
|
|
2899
|
+
constructor(documents?: string[], opts?: BM25LOptions);
|
|
2900
|
+
/**
|
|
2901
|
+
* Tokenize text into lowercase alphanumeric tokens
|
|
2902
|
+
*/
|
|
2903
|
+
tokenize(text: string): string[];
|
|
2904
|
+
/**
|
|
2905
|
+
* Compute IDF (Inverse Document Frequency) with smoothing
|
|
2906
|
+
*/
|
|
2907
|
+
private idf;
|
|
2908
|
+
/**
|
|
2909
|
+
* Compute BM25L score for a single document
|
|
2910
|
+
*/
|
|
2911
|
+
score(query: string, docIndex: number): number;
|
|
2912
|
+
/**
|
|
2913
|
+
* Search and rank all documents
|
|
2914
|
+
*/
|
|
2915
|
+
search(query: string): Array<{
|
|
2916
|
+
index: number;
|
|
2917
|
+
score: number;
|
|
2918
|
+
}>;
|
|
2428
2919
|
}
|
|
2429
|
-
|
|
2920
|
+
/**
|
|
2921
|
+
* Hybrid reranker that combines semantic and BM25L scores
|
|
2922
|
+
*
|
|
2923
|
+
* @param query - The search query
|
|
2924
|
+
* @param items - Array of items to rerank
|
|
2925
|
+
* @param getDocument - Function to extract document text from an item
|
|
2926
|
+
* @param getSemanticScore - Function to extract semantic similarity score from an item
|
|
2927
|
+
* @param options - Hybrid search options
|
|
2928
|
+
* @returns Reranked items with hybrid scores
|
|
2929
|
+
*/
|
|
2930
|
+
declare function hybridRerank<T>(query: string, items: T[], getDocument: (item: T) => string, getSemanticScore: (item: T) => number, options?: HybridSearchOptions): RerankedResult<T>[];
|
|
2931
|
+
/**
|
|
2932
|
+
* Simple reranking function for ChromaDB results
|
|
2933
|
+
*
|
|
2934
|
+
* This is a convenience wrapper for reranking ChromaDB query results
|
|
2935
|
+
* that follow the standard { ids, documents, metadatas, distances } format.
|
|
2936
|
+
*
|
|
2937
|
+
* @param query - The search query
|
|
2938
|
+
* @param chromaResults - ChromaDB query results
|
|
2939
|
+
* @param options - Hybrid search options
|
|
2940
|
+
* @returns Reranked results with hybrid scores
|
|
2941
|
+
*/
|
|
2942
|
+
declare function rerankChromaResults(query: string, chromaResults: {
|
|
2943
|
+
ids: string[][];
|
|
2944
|
+
documents: (string | null)[][];
|
|
2945
|
+
metadatas: Record<string, any>[][];
|
|
2946
|
+
distances: number[][];
|
|
2947
|
+
}, options?: HybridSearchOptions): Array<{
|
|
2948
|
+
id: string;
|
|
2949
|
+
document: string | null;
|
|
2950
|
+
metadata: Record<string, any>;
|
|
2951
|
+
distance: number;
|
|
2952
|
+
semanticScore: number;
|
|
2953
|
+
bm25Score: number;
|
|
2954
|
+
hybridScore: number;
|
|
2955
|
+
}>;
|
|
2956
|
+
/**
|
|
2957
|
+
* Rerank conversation search results specifically
|
|
2958
|
+
*
|
|
2959
|
+
* This function is designed to work with the conversation-history.search collection
|
|
2960
|
+
* where we need to fetch more results initially and then rerank them.
|
|
2961
|
+
*
|
|
2962
|
+
* @param query - The user's search query
|
|
2963
|
+
* @param results - Array of conversation search results from ChromaDB
|
|
2964
|
+
* @param options - Hybrid search options
|
|
2965
|
+
* @returns Reranked results sorted by hybrid score
|
|
2966
|
+
*/
|
|
2967
|
+
declare function rerankConversationResults<T extends {
|
|
2968
|
+
userPrompt?: string;
|
|
2969
|
+
similarity?: number;
|
|
2970
|
+
}>(query: string, results: T[], options?: HybridSearchOptions): Array<T & {
|
|
2971
|
+
hybridScore: number;
|
|
2972
|
+
bm25Score: number;
|
|
2973
|
+
}>;
|
|
2430
2974
|
|
|
2431
|
-
|
|
2975
|
+
/**
|
|
2976
|
+
* QueryExecutionService - Handles all query execution, validation, and retry logic
|
|
2977
|
+
* Extracted from BaseLLM for better separation of concerns
|
|
2978
|
+
*/
|
|
2979
|
+
|
|
2980
|
+
/**
|
|
2981
|
+
* Context for component when requesting query fix
|
|
2982
|
+
*/
|
|
2983
|
+
interface ComponentContext {
|
|
2984
|
+
name: string;
|
|
2985
|
+
type: string;
|
|
2986
|
+
title?: string;
|
|
2432
2987
|
}
|
|
2433
2988
|
/**
|
|
2434
|
-
*
|
|
2989
|
+
* Result of query validation
|
|
2435
2990
|
*/
|
|
2436
|
-
|
|
2437
|
-
|
|
2438
|
-
|
|
2439
|
-
|
|
2440
|
-
|
|
2441
|
-
protected getProviderName(): string;
|
|
2991
|
+
interface QueryValidationResult {
|
|
2992
|
+
component: Component | null;
|
|
2993
|
+
queryKey: string;
|
|
2994
|
+
result: any;
|
|
2995
|
+
validated: boolean;
|
|
2442
2996
|
}
|
|
2443
|
-
|
|
2444
|
-
|
|
2445
|
-
|
|
2997
|
+
/**
|
|
2998
|
+
* Result of batch query validation
|
|
2999
|
+
*/
|
|
3000
|
+
interface BatchValidationResult {
|
|
3001
|
+
components: Component[];
|
|
3002
|
+
queryResults: Map<string, any>;
|
|
2446
3003
|
}
|
|
2447
3004
|
/**
|
|
2448
|
-
*
|
|
3005
|
+
* Configuration for QueryExecutionService
|
|
2449
3006
|
*/
|
|
2450
|
-
|
|
2451
|
-
|
|
2452
|
-
|
|
2453
|
-
|
|
2454
|
-
|
|
2455
|
-
protected getProviderName(): string;
|
|
3007
|
+
interface QueryExecutionServiceConfig {
|
|
3008
|
+
defaultLimit: number;
|
|
3009
|
+
getModelForTask: (taskType: 'simple' | 'complex') => string;
|
|
3010
|
+
getApiKey: (apiKey?: string) => string | undefined;
|
|
3011
|
+
providerName: string;
|
|
2456
3012
|
}
|
|
2457
|
-
declare const openaiLLM: OpenAILLM;
|
|
2458
|
-
|
|
2459
3013
|
/**
|
|
2460
|
-
*
|
|
2461
|
-
*
|
|
2462
|
-
* 1. `cache` (query string → result data) — TTL-based with max size, for avoiding re-execution
|
|
2463
|
-
* of recently validated queries. LRU eviction when max size exceeded.
|
|
2464
|
-
*
|
|
2465
|
-
* 2. Encrypted queryId tokens — SQL is encrypted into the queryId itself (self-contained).
|
|
2466
|
-
* No server-side storage needed for SQL mappings. The token is decrypted on each request.
|
|
2467
|
-
* This eliminates the unbounded queryIdCache that previously grew forever and caused
|
|
2468
|
-
* memory bloat (hundreds of MBs after thousands of queries).
|
|
2469
|
-
*
|
|
2470
|
-
* Result data can still be cached temporarily via the data cache (mechanism 1).
|
|
3014
|
+
* QueryExecutionService handles all query-related operations
|
|
2471
3015
|
*/
|
|
2472
|
-
declare class
|
|
2473
|
-
private
|
|
2474
|
-
|
|
2475
|
-
private maxCacheSize;
|
|
2476
|
-
private cleanupInterval;
|
|
2477
|
-
private readonly algorithm;
|
|
2478
|
-
private encryptionKey;
|
|
2479
|
-
constructor();
|
|
2480
|
-
/**
|
|
2481
|
-
* Set the cache TTL (Time To Live)
|
|
2482
|
-
* @param minutes - TTL in minutes (default: 10)
|
|
2483
|
-
*/
|
|
2484
|
-
setTTL(minutes: number): void;
|
|
2485
|
-
/**
|
|
2486
|
-
* Get the current TTL in minutes
|
|
2487
|
-
*/
|
|
2488
|
-
getTTL(): number;
|
|
3016
|
+
declare class QueryExecutionService {
|
|
3017
|
+
private config;
|
|
3018
|
+
constructor(config: QueryExecutionServiceConfig);
|
|
2489
3019
|
/**
|
|
2490
|
-
*
|
|
3020
|
+
* Get the cache key for a query
|
|
3021
|
+
* This ensures the cache key matches what the frontend will send
|
|
2491
3022
|
*/
|
|
2492
|
-
|
|
3023
|
+
getQueryCacheKey(query: any): string;
|
|
2493
3024
|
/**
|
|
2494
|
-
*
|
|
3025
|
+
* Execute a query against the database
|
|
3026
|
+
* @param query - The SQL query to execute (string or object with sql/values)
|
|
3027
|
+
* @param collections - Collections object containing database execute function
|
|
3028
|
+
* @returns Object with result data and cache key
|
|
2495
3029
|
*/
|
|
2496
|
-
|
|
3030
|
+
executeQuery(query: any, collections: any): Promise<{
|
|
3031
|
+
result: any;
|
|
3032
|
+
cacheKey: string;
|
|
3033
|
+
}>;
|
|
2497
3034
|
/**
|
|
2498
|
-
*
|
|
3035
|
+
* Request the LLM to fix a failed SQL query
|
|
3036
|
+
* @param failedQuery - The query that failed execution
|
|
3037
|
+
* @param errorMessage - The error message from the failed execution
|
|
3038
|
+
* @param componentContext - Context about the component
|
|
3039
|
+
* @param apiKey - Optional API key
|
|
3040
|
+
* @returns Fixed query string
|
|
2499
3041
|
*/
|
|
2500
|
-
|
|
3042
|
+
requestQueryFix(failedQuery: string, errorMessage: string, componentContext: ComponentContext, apiKey?: string): Promise<string>;
|
|
2501
3043
|
/**
|
|
2502
|
-
*
|
|
3044
|
+
* Validate a single component's query with retry logic
|
|
3045
|
+
* @param component - The component to validate
|
|
3046
|
+
* @param collections - Collections object containing database execute function
|
|
3047
|
+
* @param apiKey - Optional API key for LLM calls
|
|
3048
|
+
* @returns Validation result with component, query key, and result
|
|
2503
3049
|
*/
|
|
2504
|
-
|
|
3050
|
+
validateSingleQuery(component: Component, collections: any, apiKey?: string): Promise<QueryValidationResult>;
|
|
2505
3051
|
/**
|
|
2506
|
-
*
|
|
3052
|
+
* Validate multiple component queries in parallel
|
|
3053
|
+
* @param components - Array of components with potential queries
|
|
3054
|
+
* @param collections - Collections object containing database execute function
|
|
3055
|
+
* @param apiKey - Optional API key for LLM calls
|
|
3056
|
+
* @returns Object with validated components and query results map
|
|
2507
3057
|
*/
|
|
2508
|
-
|
|
2509
|
-
|
|
2510
|
-
|
|
2511
|
-
|
|
2512
|
-
|
|
2513
|
-
|
|
2514
|
-
|
|
2515
|
-
|
|
2516
|
-
|
|
2517
|
-
|
|
2518
|
-
|
|
2519
|
-
|
|
2520
|
-
|
|
2521
|
-
|
|
2522
|
-
* Encrypt a payload into a self-contained token.
|
|
2523
|
-
*/
|
|
2524
|
-
private encrypt;
|
|
2525
|
-
/**
|
|
2526
|
-
* Decrypt a token back to the original payload.
|
|
2527
|
-
*/
|
|
2528
|
-
private decrypt;
|
|
2529
|
-
/**
|
|
2530
|
-
* Store a query by generating an encrypted token as queryId.
|
|
2531
|
-
* The SQL is encrypted INTO the token — nothing stored in memory.
|
|
2532
|
-
* If data is provided, it's cached temporarily in the data cache.
|
|
2533
|
-
*/
|
|
2534
|
-
storeQuery(query: any, data?: any): string;
|
|
3058
|
+
validateComponentQueries(components: Component[], collections: any, apiKey?: string): Promise<BatchValidationResult>;
|
|
3059
|
+
}
|
|
3060
|
+
|
|
3061
|
+
/**
|
|
3062
|
+
* Task types for model selection
|
|
3063
|
+
* - 'complex': Text generation, component matching, parameter adaptation (uses best model in balanced mode)
|
|
3064
|
+
* - 'simple': Classification, action generation (uses fast model in balanced mode)
|
|
3065
|
+
*/
|
|
3066
|
+
type TaskType = 'complex' | 'simple';
|
|
3067
|
+
interface BaseLLMConfig {
|
|
3068
|
+
model?: string;
|
|
3069
|
+
fastModel?: string;
|
|
3070
|
+
defaultLimit?: number;
|
|
3071
|
+
apiKey?: string;
|
|
2535
3072
|
/**
|
|
2536
|
-
*
|
|
2537
|
-
*
|
|
3073
|
+
* Model selection strategy:
|
|
3074
|
+
* - 'best': Use best model for all tasks (highest quality, higher cost)
|
|
3075
|
+
* - 'fast': Use fast model for all tasks (lower quality, lower cost)
|
|
3076
|
+
* - 'balanced': Use best model for complex tasks, fast model for simple tasks (default)
|
|
2538
3077
|
*/
|
|
2539
|
-
|
|
2540
|
-
|
|
2541
|
-
|
|
2542
|
-
|
|
3078
|
+
modelStrategy?: ModelStrategy;
|
|
3079
|
+
conversationSimilarityThreshold?: number;
|
|
3080
|
+
}
|
|
3081
|
+
/**
|
|
3082
|
+
* BaseLLM abstract class for AI-powered component generation and matching
|
|
3083
|
+
* Provides common functionality for all LLM providers
|
|
3084
|
+
*/
|
|
3085
|
+
declare abstract class BaseLLM {
|
|
3086
|
+
protected model: string;
|
|
3087
|
+
protected fastModel: string;
|
|
3088
|
+
protected defaultLimit: number;
|
|
3089
|
+
protected apiKey?: string;
|
|
3090
|
+
protected modelStrategy: ModelStrategy;
|
|
3091
|
+
protected conversationSimilarityThreshold: number;
|
|
3092
|
+
protected queryService: QueryExecutionService;
|
|
3093
|
+
constructor(config?: BaseLLMConfig);
|
|
2543
3094
|
/**
|
|
2544
|
-
*
|
|
3095
|
+
* Get the appropriate model based on task type and model strategy
|
|
3096
|
+
* @param taskType - 'complex' for text generation/matching, 'simple' for classification/actions
|
|
3097
|
+
* @returns The model string to use for this task
|
|
2545
3098
|
*/
|
|
2546
|
-
|
|
3099
|
+
protected getModelForTask(taskType: TaskType): string;
|
|
2547
3100
|
/**
|
|
2548
|
-
*
|
|
3101
|
+
* Set the model strategy at runtime
|
|
3102
|
+
* @param strategy - 'best', 'fast', or 'balanced'
|
|
2549
3103
|
*/
|
|
2550
|
-
|
|
2551
|
-
}
|
|
2552
|
-
declare const queryCache: QueryCache;
|
|
2553
|
-
|
|
2554
|
-
/**
|
|
2555
|
-
* Manages conversation history scoped per user + dashboard.
|
|
2556
|
-
* Each user-dashboard pair has its own isolated history that expires after a configurable TTL.
|
|
2557
|
-
*/
|
|
2558
|
-
declare class DashboardConversationHistory {
|
|
2559
|
-
private histories;
|
|
2560
|
-
private ttlMs;
|
|
2561
|
-
private maxEntries;
|
|
2562
|
-
private cleanupInterval;
|
|
2563
|
-
constructor();
|
|
3104
|
+
setModelStrategy(strategy: ModelStrategy): void;
|
|
2564
3105
|
/**
|
|
2565
|
-
*
|
|
2566
|
-
* @
|
|
3106
|
+
* Get the current model strategy
|
|
3107
|
+
* @returns The current model strategy
|
|
2567
3108
|
*/
|
|
2568
|
-
|
|
3109
|
+
getModelStrategy(): ModelStrategy;
|
|
2569
3110
|
/**
|
|
2570
|
-
* Set
|
|
3111
|
+
* Set the conversation similarity threshold at runtime
|
|
3112
|
+
* @param threshold - Value between 0 and 1 (e.g., 0.8 = 80% similarity required)
|
|
2571
3113
|
*/
|
|
2572
|
-
|
|
3114
|
+
setConversationSimilarityThreshold(threshold: number): void;
|
|
2573
3115
|
/**
|
|
2574
|
-
*
|
|
3116
|
+
* Get the current conversation similarity threshold
|
|
3117
|
+
* @returns The current threshold value
|
|
2575
3118
|
*/
|
|
2576
|
-
|
|
3119
|
+
getConversationSimilarityThreshold(): number;
|
|
2577
3120
|
/**
|
|
2578
|
-
* Get
|
|
3121
|
+
* Get the default model for this provider (used for complex tasks like text generation)
|
|
2579
3122
|
*/
|
|
2580
|
-
|
|
3123
|
+
protected abstract getDefaultModel(): string;
|
|
2581
3124
|
/**
|
|
2582
|
-
*
|
|
3125
|
+
* Get the default fast model for this provider (used for simple tasks: classification, matching, actions)
|
|
3126
|
+
* Should return a cheaper/faster model like Haiku for Anthropic
|
|
2583
3127
|
*/
|
|
2584
|
-
|
|
3128
|
+
protected abstract getDefaultFastModel(): string;
|
|
2585
3129
|
/**
|
|
2586
|
-
*
|
|
3130
|
+
* Get the default API key from environment
|
|
2587
3131
|
*/
|
|
2588
|
-
|
|
3132
|
+
protected abstract getDefaultApiKey(): string | undefined;
|
|
2589
3133
|
/**
|
|
2590
|
-
*
|
|
3134
|
+
* Get the provider name (for logging)
|
|
2591
3135
|
*/
|
|
2592
|
-
|
|
3136
|
+
protected abstract getProviderName(): string;
|
|
2593
3137
|
/**
|
|
2594
|
-
*
|
|
3138
|
+
* Get the API key (from instance, parameter, or environment)
|
|
2595
3139
|
*/
|
|
2596
|
-
|
|
2597
|
-
}
|
|
2598
|
-
declare const dashboardConversationHistory: DashboardConversationHistory;
|
|
2599
|
-
|
|
2600
|
-
/**
|
|
2601
|
-
* Multi-Agent Architecture Types
|
|
2602
|
-
*
|
|
2603
|
-
* Defines interfaces for the hierarchical agent system:
|
|
2604
|
-
* - Main Agent: ONE LLM.streamWithTools() call with source agent tools
|
|
2605
|
-
* - Source Agents: independent agents that query individual data sources
|
|
2606
|
-
*
|
|
2607
|
-
* The main agent sees only source summaries. When it calls a source tool,
|
|
2608
|
-
* the SourceAgent runs independently (own LLM, own retries) and returns clean data.
|
|
2609
|
-
*/
|
|
2610
|
-
|
|
2611
|
-
/**
|
|
2612
|
-
* Per-entity detail: name, row count, and column names.
|
|
2613
|
-
* Gives the main agent enough context to route to the right source.
|
|
2614
|
-
*/
|
|
2615
|
-
interface EntityDetail {
|
|
2616
|
-
/** Entity name (table, sheet, endpoint) */
|
|
2617
|
-
name: string;
|
|
2618
|
-
/** Approximate row count */
|
|
2619
|
-
rowCount?: number;
|
|
2620
|
-
/** Column/field names */
|
|
2621
|
-
columns: string[];
|
|
2622
|
-
}
|
|
2623
|
-
/**
|
|
2624
|
-
* Representation of a data source for the main agent.
|
|
2625
|
-
* Contains entity names WITH column names so the LLM can route accurately.
|
|
2626
|
-
*/
|
|
2627
|
-
interface SourceSummary {
|
|
2628
|
-
/** Source ID (matches tool ID prefix) */
|
|
2629
|
-
id: string;
|
|
2630
|
-
/** Human-readable source name */
|
|
2631
|
-
name: string;
|
|
2632
|
-
/** Source type: postgres, excel, rest_api, etc. */
|
|
2633
|
-
type: string;
|
|
2634
|
-
/** Brief description of what data this source contains */
|
|
2635
|
-
description: string;
|
|
2636
|
-
/** Detailed entity info with column names for routing */
|
|
2637
|
-
entityDetails: EntityDetail[];
|
|
2638
|
-
/** The tool ID associated with this source */
|
|
2639
|
-
toolId: string;
|
|
2640
|
-
}
|
|
2641
|
-
/**
|
|
2642
|
-
* What a source agent returns after querying its data source.
|
|
2643
|
-
* The main agent uses this to analyze and compose the final response.
|
|
2644
|
-
*/
|
|
2645
|
-
interface SourceAgentResult {
|
|
2646
|
-
/** Source ID */
|
|
2647
|
-
sourceId: string;
|
|
2648
|
-
/** Source name */
|
|
2649
|
-
sourceName: string;
|
|
2650
|
-
/** Whether the query succeeded */
|
|
2651
|
-
success: boolean;
|
|
2652
|
-
/** Result data rows */
|
|
2653
|
-
data: any[];
|
|
2654
|
-
/** Metadata about the query execution */
|
|
2655
|
-
metadata: SourceAgentMetadata;
|
|
2656
|
-
/** Tool execution info for the last successful query (backward compat) */
|
|
2657
|
-
executedTool: ExecutedToolInfo;
|
|
2658
|
-
/** All successful tool executions (primary + follow-up queries) */
|
|
2659
|
-
allExecutedTools?: ExecutedToolInfo[];
|
|
2660
|
-
/** Error message if failed */
|
|
2661
|
-
error?: string;
|
|
2662
|
-
}
|
|
2663
|
-
interface SourceAgentMetadata {
|
|
2664
|
-
/** Total rows that matched the query (before limit) */
|
|
2665
|
-
totalRowsMatched: number;
|
|
2666
|
-
/** Rows actually returned (after limit) */
|
|
2667
|
-
rowsReturned: number;
|
|
2668
|
-
/** Whether the result was truncated by the row limit */
|
|
2669
|
-
isLimited: boolean;
|
|
2670
|
-
/** The query/params that were executed */
|
|
2671
|
-
queryExecuted?: string;
|
|
2672
|
-
/** Execution time in milliseconds */
|
|
2673
|
-
executionTimeMs: number;
|
|
2674
|
-
}
|
|
2675
|
-
/**
|
|
2676
|
-
* The complete response from the multi-agent system.
|
|
2677
|
-
* Contains everything needed for text display + component generation.
|
|
2678
|
-
*/
|
|
2679
|
-
interface AgentResponse {
|
|
2680
|
-
/** Generated text response (analysis of the data) */
|
|
2681
|
-
text: string;
|
|
2682
|
-
/** All executed tools across all source agents (for component generation) */
|
|
2683
|
-
executedTools: ExecutedToolInfo[];
|
|
2684
|
-
/** Individual results from each source agent */
|
|
2685
|
-
sourceResults: SourceAgentResult[];
|
|
3140
|
+
protected getApiKey(apiKey?: string): string | undefined;
|
|
2686
3141
|
/**
|
|
2687
|
-
*
|
|
2688
|
-
*
|
|
2689
|
-
*
|
|
3142
|
+
* Check if a component contains a Form (data_modification component)
|
|
3143
|
+
* Forms have hardcoded defaultValues that become stale when cached
|
|
3144
|
+
* This checks both single Form components and Forms inside MultiComponentContainer
|
|
2690
3145
|
*/
|
|
2691
|
-
|
|
2692
|
-
}
|
|
2693
|
-
/**
|
|
2694
|
-
* A script MainAgent authored + verified during its turn. Shape aligns with
|
|
2695
|
-
* what ScriptStore.save() needs — minus store-assigned fields (id, timestamps, counts).
|
|
2696
|
-
*/
|
|
2697
|
-
interface AgentWrittenScript {
|
|
3146
|
+
protected containsFormComponent(component: any): boolean;
|
|
2698
3147
|
/**
|
|
2699
|
-
*
|
|
2700
|
-
*
|
|
2701
|
-
*
|
|
2702
|
-
*
|
|
3148
|
+
* Match components from text response suggestions and generate follow-up questions
|
|
3149
|
+
* Takes a text response with component suggestions (c1:type format) and matches with available components
|
|
3150
|
+
* Also generates title, description, and intelligent follow-up questions (actions) based on the analysis
|
|
3151
|
+
* All components are placed in a default MultiComponentContainer layout
|
|
3152
|
+
* @param analysisContent - The text response containing component suggestions
|
|
3153
|
+
* @param components - List of available components
|
|
3154
|
+
* @param apiKey - Optional API key
|
|
3155
|
+
* @param componentStreamCallback - Optional callback to stream primary KPI component as soon as it's identified
|
|
3156
|
+
* @returns Object containing matched components, layout title/description, and follow-up actions
|
|
2703
3157
|
*/
|
|
2704
|
-
|
|
2705
|
-
|
|
2706
|
-
|
|
2707
|
-
|
|
2708
|
-
|
|
2709
|
-
name: string;
|
|
2710
|
-
type: 'string' | 'number' | 'date' | 'date_range' | 'enum' | 'boolean';
|
|
2711
|
-
required: boolean;
|
|
2712
|
-
default?: any;
|
|
2713
|
-
enumValues?: Record<string, string>;
|
|
2714
|
-
description: string;
|
|
2715
|
-
}>;
|
|
2716
|
-
scriptBody: string;
|
|
2717
|
-
/** Source IDs referenced by the script (extracted from ctx.query calls) */
|
|
2718
|
-
sourceIds: string[];
|
|
2719
|
-
/** Tables referenced in the script's SQL (regex-extracted) */
|
|
2720
|
-
tables: string[];
|
|
2721
|
-
/** Executed queries from the verified run — fed to component generation */
|
|
2722
|
-
executedQueries: Array<{
|
|
2723
|
-
sourceId: string;
|
|
2724
|
-
sourceName: string;
|
|
2725
|
-
sql: string;
|
|
2726
|
-
data: any[];
|
|
2727
|
-
count: number;
|
|
2728
|
-
totalCount?: number;
|
|
2729
|
-
executionTimeMs: number;
|
|
2730
|
-
/**
|
|
2731
|
-
* True for synthetic entries (ctx.emit datasets, the computed:_final
|
|
2732
|
-
* post-JS data). The component generator routes virtual sources through
|
|
2733
|
-
* the script_dataset sentinel toolId so the frontend resolves them via
|
|
2734
|
-
* queryCache instead of attempting to re-execute SQL.
|
|
2735
|
-
*/
|
|
2736
|
-
virtual?: boolean;
|
|
3158
|
+
matchComponentsFromAnalysis(analysisContent: string, components: Component[], userPrompt: string, apiKey?: string, componentStreamCallback?: (component: Component) => void, deferredTools?: any[], executedTools?: any[], collections?: any, userId?: string): Promise<{
|
|
3159
|
+
components: Component[];
|
|
3160
|
+
layoutTitle: string;
|
|
3161
|
+
layoutDescription: string;
|
|
3162
|
+
actions: Action[];
|
|
2737
3163
|
}>;
|
|
2738
|
-
}
|
|
2739
|
-
/**
|
|
2740
|
-
* Configuration for the multi-agent system.
|
|
2741
|
-
* Controls limits, models, and behavior.
|
|
2742
|
-
*/
|
|
2743
|
-
interface AgentConfig {
|
|
2744
|
-
/** Max rows a source agent can return (default: 50) */
|
|
2745
|
-
maxRowsPerSource: number;
|
|
2746
|
-
/** Model for the main agent (routing + analysis in one LLM call) */
|
|
2747
|
-
mainAgentModel: string;
|
|
2748
|
-
/** Model for source agent query generation */
|
|
2749
|
-
sourceAgentModel: string;
|
|
2750
|
-
/** API key for LLM calls */
|
|
2751
|
-
apiKey?: string;
|
|
2752
|
-
/** Max retry attempts per source agent */
|
|
2753
|
-
maxRetries: number;
|
|
2754
|
-
/** Max tool calling iterations for the main agent loop */
|
|
2755
|
-
maxIterations: number;
|
|
2756
|
-
/** Global knowledge base context (static, same for all users/questions — cached in system prompt) */
|
|
2757
|
-
globalKnowledgeBase?: string;
|
|
2758
|
-
/** Per-request knowledge base context (user-specific + query-matched — dynamic, not cached) */
|
|
2759
|
-
knowledgeBaseContext?: string;
|
|
2760
|
-
/** Collections registry (ChromaDB search hooks) for embedding-based schema + source search */
|
|
2761
|
-
collections?: any;
|
|
2762
|
-
/** Optional project ID for scoping embedding searches */
|
|
2763
|
-
projectId?: string;
|
|
2764
|
-
}
|
|
2765
|
-
/**
|
|
2766
|
-
* Default agent configuration
|
|
2767
|
-
*/
|
|
2768
|
-
declare const DEFAULT_AGENT_CONFIG: AgentConfig;
|
|
2769
|
-
|
|
2770
|
-
/**
|
|
2771
|
-
* Script Flow Types
|
|
2772
|
-
*
|
|
2773
|
-
* Defines interfaces for the script-based query architecture:
|
|
2774
|
-
* - ScriptRecipe: metadata for matching, validation, and quality tracking
|
|
2775
|
-
* - ScriptResult: output from executing a script
|
|
2776
|
-
* - ScriptMatch: result from the LLM-based script matcher
|
|
2777
|
-
*/
|
|
2778
|
-
/**
|
|
2779
|
-
* Recipe metadata stored alongside each script.
|
|
2780
|
-
* Used for matching, validation, and quality tracking.
|
|
2781
|
-
*/
|
|
2782
|
-
interface ScriptRecipe {
|
|
2783
|
-
/** Unique script identifier */
|
|
2784
|
-
id: string;
|
|
2785
|
-
/** Version number (incremented on regeneration) */
|
|
2786
|
-
version: number;
|
|
2787
|
-
/** Human-readable name (e.g., "Revenue by Dimension") */
|
|
2788
|
-
name: string;
|
|
2789
|
-
/** Natural language description of what this script does */
|
|
2790
|
-
intentDescription: string;
|
|
2791
|
-
/** Keyword tags for quick filtering */
|
|
2792
|
-
tags: string[];
|
|
2793
|
-
/** Source tool IDs this script queries (e.g., ["mssql-abc123_query"]) */
|
|
2794
|
-
sourceIds: string[];
|
|
2795
|
-
/** Table names used (for future schema drift detection) */
|
|
2796
|
-
tables: string[];
|
|
2797
|
-
/** Parameter definitions — what can vary */
|
|
2798
|
-
parameters: ScriptParameter[];
|
|
2799
|
-
/** The script function body as a string */
|
|
2800
|
-
scriptBody: string;
|
|
2801
|
-
/** Times this script was used successfully */
|
|
2802
|
-
successCount: number;
|
|
2803
|
-
/** Times this script failed */
|
|
2804
|
-
failureCount: number;
|
|
2805
|
-
/** ISO timestamp of last usage */
|
|
2806
|
-
lastUsed: string;
|
|
2807
|
-
/** Original user question that created this script */
|
|
2808
|
-
createdFrom: string;
|
|
2809
|
-
/** ISO timestamp */
|
|
2810
|
-
createdAt: string;
|
|
2811
|
-
/** ISO timestamp */
|
|
2812
|
-
updatedAt: string;
|
|
2813
3164
|
/**
|
|
2814
|
-
*
|
|
2815
|
-
*
|
|
2816
|
-
* See backend/docs/SCRIPT-FLOW-FORK-ADAPT.md.
|
|
3165
|
+
* Classify user question into category and detect external tools needed
|
|
3166
|
+
* Determines if question is for data analysis, requires external tools, or needs text response
|
|
2817
3167
|
*/
|
|
2818
|
-
|
|
2819
|
-
|
|
2820
|
-
|
|
3168
|
+
classifyQuestionCategory(userPrompt: string, apiKey?: string, conversationHistory?: string, externalTools?: any[]): Promise<{
|
|
3169
|
+
category: 'data_analysis' | 'data_modification' | 'general';
|
|
3170
|
+
externalTools: Array<{
|
|
3171
|
+
type: string;
|
|
3172
|
+
name: string;
|
|
3173
|
+
description: string;
|
|
3174
|
+
parameters: Record<string, any>;
|
|
3175
|
+
}>;
|
|
3176
|
+
dataAnalysisType?: 'visualization' | 'calculation' | 'comparison' | 'trend';
|
|
3177
|
+
reasoning: string;
|
|
3178
|
+
confidence: number;
|
|
3179
|
+
}>;
|
|
2821
3180
|
/**
|
|
2822
|
-
*
|
|
2823
|
-
*
|
|
3181
|
+
* Adapt UI block parameters based on current user question
|
|
3182
|
+
* Takes a matched UI block from semantic search and modifies its props to answer the new question
|
|
3183
|
+
* Also adapts the cached text response to match the new question
|
|
2824
3184
|
*/
|
|
2825
|
-
|
|
3185
|
+
adaptUIBlockParameters(currentUserPrompt: string, originalUserPrompt: string, matchedUIBlock: any, apiKey?: string, cachedTextResponse?: string): Promise<{
|
|
3186
|
+
success: boolean;
|
|
3187
|
+
adaptedComponent?: Component;
|
|
3188
|
+
adaptedTextResponse?: string;
|
|
3189
|
+
parametersChanged?: Array<{
|
|
3190
|
+
field: string;
|
|
3191
|
+
reason: string;
|
|
3192
|
+
}>;
|
|
3193
|
+
explanation: string;
|
|
3194
|
+
}>;
|
|
2826
3195
|
/**
|
|
2827
|
-
*
|
|
2828
|
-
*
|
|
2829
|
-
*
|
|
2830
|
-
*
|
|
2831
|
-
* from clobbering each other's drafts.
|
|
2832
|
-
* - 'verified': promoted after `execute_script` succeeded; the matcher sees it.
|
|
2833
|
-
* Filename drops the turn suffix unless a verified file with
|
|
2834
|
-
* the same slug already exists (collision case keeps the suffix).
|
|
2835
|
-
*
|
|
2836
|
-
* Recipes loaded from disk without this field default to 'verified' so
|
|
2837
|
-
* existing scripts keep working unchanged.
|
|
3196
|
+
* Generate text-based response for user question
|
|
3197
|
+
* This provides conversational text responses instead of component generation
|
|
3198
|
+
* Supports tool calling for query execution with automatic retry on errors (max 3 attempts)
|
|
3199
|
+
* After generating text response, if components are provided, matches suggested components
|
|
2838
3200
|
*/
|
|
2839
|
-
|
|
3201
|
+
generateTextResponse(userPrompt: string, apiKey?: string, conversationHistory?: string, streamCallback?: (chunk: string) => void, collections?: any, components?: Component[], externalTools?: any[], category?: 'data_analysis' | 'data_modification' | 'general', userId?: string): Promise<T_RESPONSE>;
|
|
2840
3202
|
/**
|
|
2841
|
-
*
|
|
2842
|
-
*
|
|
3203
|
+
* Main orchestration function with semantic search and multi-step classification
|
|
3204
|
+
* NEW FLOW (Recommended):
|
|
3205
|
+
* 1. Semantic search: Check previous conversations (>60% match)
|
|
3206
|
+
* - If match found → Adapt UI block parameters and return
|
|
3207
|
+
* 2. Category classification: Determine if data_analysis, requires_external_tools, or text_response
|
|
3208
|
+
* 3. Route appropriately based on category and response mode
|
|
2843
3209
|
*/
|
|
2844
|
-
|
|
3210
|
+
handleUserRequest(userPrompt: string, components: Component[], apiKey?: string, conversationHistory?: string, responseMode?: 'component' | 'text', streamCallback?: (chunk: string) => void, collections?: any, externalTools?: any[], userId?: string): Promise<T_RESPONSE>;
|
|
2845
3211
|
/**
|
|
2846
|
-
*
|
|
2847
|
-
*
|
|
2848
|
-
*
|
|
3212
|
+
* Generate next questions that the user might ask based on the original prompt and generated component
|
|
3213
|
+
* This helps provide intelligent suggestions for follow-up queries
|
|
3214
|
+
* For general/conversational questions without components, pass textResponse instead
|
|
2849
3215
|
*/
|
|
2850
|
-
|
|
2851
|
-
phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
|
|
2852
|
-
message: string;
|
|
2853
|
-
at: string;
|
|
2854
|
-
attempt: number;
|
|
2855
|
-
};
|
|
3216
|
+
generateNextQuestions(originalUserPrompt: string, component?: Component | null, componentData?: Record<string, unknown>, apiKey?: string, conversationHistory?: string, textResponse?: string): Promise<string[]>;
|
|
2856
3217
|
}
|
|
2857
|
-
|
|
2858
|
-
|
|
2859
|
-
|
|
2860
|
-
|
|
2861
|
-
|
|
2862
|
-
|
|
2863
|
-
|
|
2864
|
-
|
|
2865
|
-
|
|
2866
|
-
|
|
2867
|
-
|
|
2868
|
-
|
|
2869
|
-
description: string;
|
|
3218
|
+
|
|
3219
|
+
interface AnthropicLLMConfig extends BaseLLMConfig {
|
|
3220
|
+
}
|
|
3221
|
+
/**
|
|
3222
|
+
* AnthropicLLM class for handling AI-powered component generation and matching using Anthropic Claude
|
|
3223
|
+
*/
|
|
3224
|
+
declare class AnthropicLLM extends BaseLLM {
|
|
3225
|
+
constructor(config?: AnthropicLLMConfig);
|
|
3226
|
+
protected getDefaultModel(): string;
|
|
3227
|
+
protected getDefaultFastModel(): string;
|
|
3228
|
+
protected getDefaultApiKey(): string | undefined;
|
|
3229
|
+
protected getProviderName(): string;
|
|
2870
3230
|
}
|
|
3231
|
+
declare const anthropicLLM: AnthropicLLM;
|
|
2871
3232
|
|
|
3233
|
+
interface GroqLLMConfig extends BaseLLMConfig {
|
|
3234
|
+
}
|
|
2872
3235
|
/**
|
|
2873
|
-
*
|
|
2874
|
-
*
|
|
2875
|
-
* Layout:
|
|
2876
|
-
* scripts-store/
|
|
2877
|
-
* metadata/<name>.json ← recipe metadata (params, tables, counts, ...)
|
|
2878
|
-
* <name>.ts ← the getData() function body, editable in your IDE
|
|
2879
|
-
*
|
|
2880
|
-
* Each VM deployment is a single project, so no project ID prefix needed.
|
|
2881
|
-
* Legacy single-file format (JSON with embedded scriptBody) is still loadable
|
|
2882
|
-
* for backwards compatibility and auto-migrated to the split format on next save.
|
|
3236
|
+
* GroqLLM class for handling AI-powered component generation and matching using Groq
|
|
2883
3237
|
*/
|
|
3238
|
+
declare class GroqLLM extends BaseLLM {
|
|
3239
|
+
constructor(config?: GroqLLMConfig);
|
|
3240
|
+
protected getDefaultModel(): string;
|
|
3241
|
+
protected getDefaultFastModel(): string;
|
|
3242
|
+
protected getDefaultApiKey(): string | undefined;
|
|
3243
|
+
protected getProviderName(): string;
|
|
3244
|
+
}
|
|
3245
|
+
declare const groqLLM: GroqLLM;
|
|
2884
3246
|
|
|
3247
|
+
interface GeminiLLMConfig extends BaseLLMConfig {
|
|
3248
|
+
}
|
|
2885
3249
|
/**
|
|
2886
|
-
*
|
|
2887
|
-
* retries within the same turn overwrite the same files (or omit `recipeId`
|
|
2888
|
-
* on the first call to mint a fresh draft).
|
|
3250
|
+
* GeminiLLM class for handling AI-powered component generation and matching using Google Gemini
|
|
2889
3251
|
*/
|
|
2890
|
-
|
|
2891
|
-
|
|
2892
|
-
|
|
2893
|
-
|
|
2894
|
-
|
|
2895
|
-
|
|
2896
|
-
|
|
2897
|
-
|
|
2898
|
-
|
|
2899
|
-
|
|
2900
|
-
createdFrom: string;
|
|
3252
|
+
declare class GeminiLLM extends BaseLLM {
|
|
3253
|
+
constructor(config?: GeminiLLMConfig);
|
|
3254
|
+
protected getDefaultModel(): string;
|
|
3255
|
+
protected getDefaultFastModel(): string;
|
|
3256
|
+
protected getDefaultApiKey(): string | undefined;
|
|
3257
|
+
protected getProviderName(): string;
|
|
3258
|
+
}
|
|
3259
|
+
declare const geminiLLM: GeminiLLM;
|
|
3260
|
+
|
|
3261
|
+
interface OpenAILLMConfig extends BaseLLMConfig {
|
|
2901
3262
|
}
|
|
2902
3263
|
/**
|
|
2903
|
-
*
|
|
2904
|
-
* MainAgent gathers `sourceIds` and `tables` from the verified execution; the
|
|
2905
|
-
* caller (agent-user-response.ts) supplies the optional fork lineage.
|
|
3264
|
+
* OpenAILLM class for handling AI-powered component generation and matching using OpenAI GPT models
|
|
2906
3265
|
*/
|
|
2907
|
-
|
|
2908
|
-
|
|
2909
|
-
|
|
2910
|
-
|
|
2911
|
-
|
|
2912
|
-
|
|
3266
|
+
declare class OpenAILLM extends BaseLLM {
|
|
3267
|
+
constructor(config?: OpenAILLMConfig);
|
|
3268
|
+
protected getDefaultModel(): string;
|
|
3269
|
+
protected getDefaultFastModel(): string;
|
|
3270
|
+
protected getDefaultApiKey(): string | undefined;
|
|
3271
|
+
protected getProviderName(): string;
|
|
2913
3272
|
}
|
|
2914
|
-
declare
|
|
2915
|
-
|
|
2916
|
-
|
|
2917
|
-
|
|
2918
|
-
|
|
2919
|
-
|
|
2920
|
-
|
|
2921
|
-
|
|
2922
|
-
|
|
2923
|
-
|
|
2924
|
-
|
|
2925
|
-
|
|
2926
|
-
|
|
2927
|
-
|
|
2928
|
-
|
|
2929
|
-
|
|
2930
|
-
|
|
2931
|
-
|
|
2932
|
-
|
|
2933
|
-
|
|
2934
|
-
private
|
|
2935
|
-
|
|
2936
|
-
|
|
2937
|
-
|
|
2938
|
-
*/
|
|
2939
|
-
getAll(): ScriptRecipe[];
|
|
3273
|
+
declare const openaiLLM: OpenAILLM;
|
|
3274
|
+
|
|
3275
|
+
/**
|
|
3276
|
+
* Query Cache — Two mechanisms:
|
|
3277
|
+
*
|
|
3278
|
+
* 1. `cache` (query string → result data) — TTL-based with max size, for avoiding re-execution
|
|
3279
|
+
* of recently validated queries. True LRU eviction: reads bubble entries to the back via
|
|
3280
|
+
* delete+re-set so the oldest *unused* entry is evicted, not the oldest *inserted*.
|
|
3281
|
+
*
|
|
3282
|
+
* 2. Encrypted queryId tokens — SQL is encrypted into the queryId itself (self-contained).
|
|
3283
|
+
* No server-side storage needed for SQL mappings. The token is decrypted on each request.
|
|
3284
|
+
* This eliminates the unbounded queryIdCache that previously grew forever and caused
|
|
3285
|
+
* memory bloat (hundreds of MBs after thousands of queries).
|
|
3286
|
+
*
|
|
3287
|
+
* Result data can still be cached temporarily via the data cache (mechanism 1).
|
|
3288
|
+
*/
|
|
3289
|
+
declare class QueryCache {
|
|
3290
|
+
private cache;
|
|
3291
|
+
private ttlMs;
|
|
3292
|
+
private maxCacheSize;
|
|
3293
|
+
private cleanupInterval;
|
|
3294
|
+
private readonly algorithm;
|
|
3295
|
+
private encryptionKey;
|
|
3296
|
+
constructor();
|
|
2940
3297
|
/**
|
|
2941
|
-
*
|
|
2942
|
-
*
|
|
3298
|
+
* Set the cache TTL (Time To Live)
|
|
3299
|
+
* @param minutes - TTL in minutes (default: 10)
|
|
2943
3300
|
*/
|
|
2944
|
-
|
|
3301
|
+
setTTL(minutes: number): void;
|
|
2945
3302
|
/**
|
|
2946
|
-
*
|
|
3303
|
+
* Get the current TTL in minutes
|
|
2947
3304
|
*/
|
|
2948
|
-
|
|
3305
|
+
getTTL(): number;
|
|
2949
3306
|
/**
|
|
2950
|
-
*
|
|
2951
|
-
*
|
|
3307
|
+
* Store query result in data cache.
|
|
3308
|
+
* If the key already exists, it's removed first so the re-insert places it
|
|
3309
|
+
* at the back of the iteration order (LRU). Eviction only fires when adding
|
|
3310
|
+
* a genuinely new key past the size limit.
|
|
2952
3311
|
*/
|
|
2953
|
-
|
|
3312
|
+
set(query: string, data: any): void;
|
|
2954
3313
|
/**
|
|
2955
|
-
*
|
|
2956
|
-
* the
|
|
2957
|
-
*
|
|
2958
|
-
* overwrite the same files (the LLM rewriting itself); a fresh `recipeId`
|
|
2959
|
-
* is minted only on the first call of the turn.
|
|
2960
|
-
*
|
|
2961
|
-
* Filename includes the `turnId` suffix so concurrent turns never collide.
|
|
3314
|
+
* Get cached result if exists and not expired.
|
|
3315
|
+
* On hit, re-inserts the entry so it moves to the back of the Map's
|
|
3316
|
+
* iteration order — turning FIFO eviction into true LRU.
|
|
2962
3317
|
*/
|
|
2963
|
-
|
|
3318
|
+
get(query: string): any | null;
|
|
2964
3319
|
/**
|
|
2965
|
-
*
|
|
2966
|
-
* in the metadata JSON without grepping logs. No-op if the recipe doesn't
|
|
2967
|
-
* exist or has already been promoted.
|
|
3320
|
+
* Check if query exists in cache (not expired)
|
|
2968
3321
|
*/
|
|
2969
|
-
|
|
2970
|
-
phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
|
|
2971
|
-
message: string;
|
|
2972
|
-
attempt: number;
|
|
2973
|
-
}): void;
|
|
3322
|
+
has(query: string): boolean;
|
|
2974
3323
|
/**
|
|
2975
|
-
*
|
|
2976
|
-
*
|
|
2977
|
-
* - Renames the on-disk files from `<slug>-<turnId>.{ts,json}` to the bare
|
|
2978
|
-
* `<slug>.{ts,json}`. If a verified file with the same slug already exists
|
|
2979
|
-
* (concurrent turn won the race, or a prior session has it), the recipe
|
|
2980
|
-
* keeps its turn-suffixed filename so we never overwrite verified work —
|
|
2981
|
-
* the matcher keys on `recipe.id`, so two siblings with similar slugs is fine.
|
|
2982
|
-
* - Flips `status: 'verified'`, clears `lastError`, applies provenance
|
|
2983
|
-
* (`sourceIds`, `tables`) and optional fork lineage.
|
|
3324
|
+
* Remove a specific query from cache
|
|
2984
3325
|
*/
|
|
2985
|
-
|
|
3326
|
+
delete(query: string): void;
|
|
2986
3327
|
/**
|
|
2987
|
-
*
|
|
2988
|
-
* clean up). Per the agreed policy, MainAgent's normal failure path does
|
|
2989
|
-
* NOT call this — failed drafts are kept on disk for the user to inspect.
|
|
3328
|
+
* Clear all cached entries
|
|
2990
3329
|
*/
|
|
2991
|
-
|
|
3330
|
+
clear(): void;
|
|
2992
3331
|
/**
|
|
2993
|
-
*
|
|
3332
|
+
* Get cache statistics
|
|
2994
3333
|
*/
|
|
2995
|
-
|
|
3334
|
+
getStats(): {
|
|
3335
|
+
size: number;
|
|
3336
|
+
queryIdCount: number;
|
|
3337
|
+
oldestEntryAge: number | null;
|
|
3338
|
+
};
|
|
2996
3339
|
/**
|
|
2997
|
-
*
|
|
3340
|
+
* Start periodic cleanup of expired data cache entries.
|
|
2998
3341
|
*/
|
|
2999
|
-
|
|
3342
|
+
private startCleanup;
|
|
3000
3343
|
/**
|
|
3001
|
-
*
|
|
3344
|
+
* Encrypt a payload into a self-contained token.
|
|
3002
3345
|
*/
|
|
3003
|
-
|
|
3346
|
+
private encrypt;
|
|
3004
3347
|
/**
|
|
3005
|
-
*
|
|
3006
|
-
* Returns 0 if the recipe has fewer than 5 total uses.
|
|
3348
|
+
* Decrypt a token back to the original payload.
|
|
3007
3349
|
*/
|
|
3008
|
-
|
|
3009
|
-
private ensureLoaded;
|
|
3350
|
+
private decrypt;
|
|
3010
3351
|
/**
|
|
3011
|
-
*
|
|
3012
|
-
*
|
|
3352
|
+
* Store a query by generating an encrypted token as queryId.
|
|
3353
|
+
* The SQL is encrypted INTO the token — nothing stored in memory.
|
|
3354
|
+
* If data is provided, it's cached temporarily in the data cache.
|
|
3013
3355
|
*/
|
|
3014
|
-
|
|
3356
|
+
storeQuery(query: any, data?: any): string;
|
|
3015
3357
|
/**
|
|
3016
|
-
*
|
|
3017
|
-
*
|
|
3018
|
-
* Supports two layouts:
|
|
3019
|
-
* - New (preferred): metadata/<name>.json + <name>.ts
|
|
3020
|
-
* - Legacy: <name>.json with embedded scriptBody (auto-migrated on next save)
|
|
3358
|
+
* Get a stored query by decrypting its token.
|
|
3359
|
+
* Returns the SQL + any cached result data.
|
|
3021
3360
|
*/
|
|
3022
|
-
|
|
3361
|
+
getQuery(queryId: string): {
|
|
3362
|
+
query: any;
|
|
3363
|
+
data: any;
|
|
3364
|
+
} | null;
|
|
3023
3365
|
/**
|
|
3024
|
-
*
|
|
3025
|
-
* metadata/<name>.json (metadata, no scriptBody)
|
|
3026
|
-
* <name>.ts (just the function body)
|
|
3027
|
-
*
|
|
3028
|
-
* If a legacy top-level <name>.json exists for the same recipe, remove it
|
|
3029
|
-
* so we don't end up with duplicate sources of truth.
|
|
3366
|
+
* Update cached data for a queryId token
|
|
3030
3367
|
*/
|
|
3031
|
-
|
|
3368
|
+
setQueryData(queryId: string, data: any): void;
|
|
3032
3369
|
/**
|
|
3033
|
-
*
|
|
3370
|
+
* Stop cleanup interval (for graceful shutdown)
|
|
3034
3371
|
*/
|
|
3035
|
-
|
|
3372
|
+
destroy(): void;
|
|
3036
3373
|
}
|
|
3374
|
+
declare const queryCache: QueryCache;
|
|
3037
3375
|
|
|
3038
3376
|
/**
|
|
3039
|
-
*
|
|
3040
|
-
*
|
|
3041
|
-
* A single LLM.streamWithTools() call that handles everything:
|
|
3042
|
-
* - Routing: decides which source(s) to query based on summaries
|
|
3043
|
-
* - Querying: calls source tools (each wraps an independent SourceAgent)
|
|
3044
|
-
* - Direct tools: calls pre-built function tools directly with LLM-provided params
|
|
3045
|
-
* - Re-querying: if data is wrong/incomplete, calls tools again with modified intent
|
|
3046
|
-
* - Analysis: generates final text response from the data
|
|
3047
|
-
*
|
|
3048
|
-
* Two tool types:
|
|
3049
|
-
* - "source" tools: main agent sees summaries, SourceAgent handles SQL generation independently
|
|
3050
|
-
* - "direct" tools: main agent calls fn() directly with structured params (no SourceAgent)
|
|
3377
|
+
* Manages conversation history scoped per user + dashboard.
|
|
3378
|
+
* Each user-dashboard pair has its own isolated history that expires after a configurable TTL.
|
|
3051
3379
|
*/
|
|
3052
|
-
|
|
3053
|
-
|
|
3054
|
-
private
|
|
3055
|
-
private
|
|
3056
|
-
private
|
|
3380
|
+
declare class DashboardConversationHistory {
|
|
3381
|
+
private histories;
|
|
3382
|
+
private ttlMs;
|
|
3383
|
+
private maxEntries;
|
|
3384
|
+
private cleanupInterval;
|
|
3385
|
+
constructor();
|
|
3057
3386
|
/**
|
|
3058
|
-
*
|
|
3059
|
-
*
|
|
3060
|
-
* store. Headless callers (alert analyzer, metric resolver) omit these to
|
|
3061
|
-
* suppress script authoring entirely — drafts would otherwise leak onto
|
|
3062
|
-
* disk with no caller to promote or clean them up.
|
|
3387
|
+
* Set the TTL for dashboard histories
|
|
3388
|
+
* @param minutes - TTL in minutes
|
|
3063
3389
|
*/
|
|
3064
|
-
|
|
3065
|
-
private turnId;
|
|
3066
|
-
private createdFromPrompt;
|
|
3067
|
-
private scriptState;
|
|
3068
|
-
constructor(externalTools: ExternalTool[], config: AgentConfig, scriptStore?: ScriptStore, turnId?: string, streamBuffer?: StreamBuffer);
|
|
3069
|
-
private get scriptingEnabled();
|
|
3390
|
+
setTTL(minutes: number): void;
|
|
3070
3391
|
/**
|
|
3071
|
-
*
|
|
3072
|
-
*
|
|
3073
|
-
* This is ONE LLM.streamWithTools() call. The LLM:
|
|
3074
|
-
* 1. Sees source summaries + direct tool descriptions in system prompt
|
|
3075
|
-
* 2. Decides which tool(s) to call (routing)
|
|
3076
|
-
* 3. Source tools → SourceAgent runs independently → returns data
|
|
3077
|
-
* 4. Direct tools → fn() called directly with LLM params → returns data
|
|
3078
|
-
* 5. Generates final analysis text
|
|
3392
|
+
* Set max entries per dashboard
|
|
3079
3393
|
*/
|
|
3080
|
-
|
|
3081
|
-
private handleWriteScript;
|
|
3082
|
-
private handleExecuteScript;
|
|
3394
|
+
setMaxEntries(max: number): void;
|
|
3083
3395
|
/**
|
|
3084
|
-
*
|
|
3085
|
-
* `ScriptStore.promoteToVerified()`. Only returned when a verified
|
|
3086
|
-
* successful execution is on record.
|
|
3396
|
+
* Add a conversation entry for a user's dashboard
|
|
3087
3397
|
*/
|
|
3088
|
-
|
|
3089
|
-
private normalizeParameterList;
|
|
3398
|
+
addEntry(dashboardId: string, userPrompt: string, componentSummary: string, userId?: string): void;
|
|
3090
3399
|
/**
|
|
3091
|
-
*
|
|
3092
|
-
* this source + intent. Returns a formatted schema block if confidence is
|
|
3093
|
-
* high (top match ≥ 0.55 and ≥3 candidates), otherwise null.
|
|
3094
|
-
*
|
|
3095
|
-
* When this returns a block, we can skip the SourceAgent's `search_schema`
|
|
3096
|
-
* loop and reduce iteration budget. When it returns null, the SourceAgent
|
|
3097
|
-
* falls back to the existing LLM-driven keyword search (same as today).
|
|
3400
|
+
* Get formatted conversation history for a user's dashboard
|
|
3098
3401
|
*/
|
|
3099
|
-
|
|
3402
|
+
getHistory(dashboardId: string, userId?: string): string;
|
|
3100
3403
|
/**
|
|
3101
|
-
*
|
|
3404
|
+
* Clear history for a specific user's dashboard
|
|
3102
3405
|
*/
|
|
3103
|
-
|
|
3406
|
+
clearDashboard(dashboardId: string, userId?: string): void;
|
|
3104
3407
|
/**
|
|
3105
|
-
*
|
|
3408
|
+
* Clear all dashboard histories
|
|
3106
3409
|
*/
|
|
3107
|
-
|
|
3410
|
+
clearAll(): void;
|
|
3108
3411
|
/**
|
|
3109
|
-
*
|
|
3110
|
-
* The full schema is inside the SourceAgent which runs independently.
|
|
3412
|
+
* Start periodic cleanup of expired histories
|
|
3111
3413
|
*/
|
|
3112
|
-
private
|
|
3414
|
+
private startCleanup;
|
|
3113
3415
|
/**
|
|
3114
|
-
*
|
|
3115
|
-
* These are called directly by the main agent LLM, no SourceAgent.
|
|
3416
|
+
* Stop cleanup interval (for graceful shutdown)
|
|
3116
3417
|
*/
|
|
3117
|
-
|
|
3418
|
+
destroy(): void;
|
|
3419
|
+
}
|
|
3420
|
+
declare const dashboardConversationHistory: DashboardConversationHistory;
|
|
3421
|
+
|
|
3422
|
+
/**
|
|
3423
|
+
* ScriptMatcher — LLM-Based Script Matching + Parameter Extraction
|
|
3424
|
+
*
|
|
3425
|
+
* Uses ONE LLM call to:
|
|
3426
|
+
* 1. Pick the best matching script from the library (or "none")
|
|
3427
|
+
* 2. Extract parameter values from the user question
|
|
3428
|
+
*
|
|
3429
|
+
* Why LLM over embeddings:
|
|
3430
|
+
* - Embeddings capture topic similarity ("overstock" ≈ "inventory" ≈ "revenue")
|
|
3431
|
+
* but can't distinguish structurally different questions about the same domain
|
|
3432
|
+
* - LLM understands that "overstock by warehouse" needs a different script than
|
|
3433
|
+
* "revenue by warehouse" even though they're semantically close
|
|
3434
|
+
* - One call does both matching AND parameter extraction
|
|
3435
|
+
*
|
|
3436
|
+
* When script library grows past ~50, add an embedding pre-filter
|
|
3437
|
+
* (ChromaDB narrows to top 10 → LLM picks from those 10).
|
|
3438
|
+
*/
|
|
3439
|
+
|
|
3440
|
+
declare class ScriptMatcher {
|
|
3441
|
+
private store;
|
|
3442
|
+
constructor(store: ScriptStore);
|
|
3118
3443
|
/**
|
|
3119
|
-
*
|
|
3444
|
+
* Find the best matching script for a user question.
|
|
3445
|
+
* Uses ONE LLM call that picks the script AND extracts parameters.
|
|
3446
|
+
* Returns null if no script matches.
|
|
3120
3447
|
*/
|
|
3121
|
-
|
|
3448
|
+
match(userPrompt: string, apiKey?: string, model?: string): Promise<ScriptMatch | null>;
|
|
3122
3449
|
/**
|
|
3123
|
-
*
|
|
3450
|
+
* Build the script catalog string for the LLM prompt.
|
|
3451
|
+
* Each script gets: index, ID, name, description, and parameter definitions.
|
|
3124
3452
|
*/
|
|
3125
|
-
|
|
3453
|
+
private buildScriptCatalog;
|
|
3454
|
+
}
|
|
3455
|
+
|
|
3456
|
+
/**
|
|
3457
|
+
* ScriptRunner — Execute scripts in an isolated tsx subprocess.
|
|
3458
|
+
*
|
|
3459
|
+
* The subprocess approach replaces the earlier `new Function()` eval and gives us:
|
|
3460
|
+
* - Real sandbox (separate process, SIGKILL on timeout).
|
|
3461
|
+
* - Real TypeScript (tsx transpiles on the fly).
|
|
3462
|
+
* - npm imports available to scripts (clustering, stats, geo, etc.).
|
|
3463
|
+
*
|
|
3464
|
+
* Protocol: NDJSON over the child's stdin/stdout. See script-ipc.ts + backend/docs/SCRIPT-FLOW-IMPLEMENTATION.md.
|
|
3465
|
+
*/
|
|
3466
|
+
|
|
3467
|
+
interface RunScriptOptions {
|
|
3468
|
+
/** Data sources the script is allowed to query via ctx.query */
|
|
3469
|
+
externalTools: ExternalTool[];
|
|
3470
|
+
/** Optional — for propagating per-query UI progress to the user */
|
|
3471
|
+
streamBuffer?: StreamBuffer;
|
|
3472
|
+
/** Override the wall-clock timeout (default `SCRIPT_TIMEOUT_MS`, 60s). */
|
|
3473
|
+
timeoutMs?: number;
|
|
3126
3474
|
}
|
|
3475
|
+
/**
|
|
3476
|
+
* Execute a recipe by spawning a tsx child on the script's .ts file.
|
|
3477
|
+
* `scriptPath` is the absolute path to the saved `.ts` body.
|
|
3478
|
+
*/
|
|
3479
|
+
declare function runScript(recipe: ScriptRecipe, scriptPath: string, params: Record<string, any>, options: RunScriptOptions): Promise<ScriptResult>;
|
|
3127
3480
|
|
|
3128
3481
|
type MessageTypeHandler = (message: IncomingMessage) => void | Promise<void>;
|
|
3129
3482
|
declare class SuperatomSDK {
|
|
@@ -3141,6 +3494,7 @@ declare class SuperatomSDK {
|
|
|
3141
3494
|
private collections;
|
|
3142
3495
|
private components;
|
|
3143
3496
|
private tools;
|
|
3497
|
+
private workflows;
|
|
3144
3498
|
private anthropicApiKey;
|
|
3145
3499
|
private groqApiKey;
|
|
3146
3500
|
private geminiApiKey;
|
|
@@ -3250,6 +3604,19 @@ declare class SuperatomSDK {
|
|
|
3250
3604
|
* Get the stored tools
|
|
3251
3605
|
*/
|
|
3252
3606
|
getTools(): Tool$1[];
|
|
3607
|
+
/**
|
|
3608
|
+
* Register workflow components for the SDK instance.
|
|
3609
|
+
*
|
|
3610
|
+
* Workflows are pre-built multi-step UI flows the main agent can pick when
|
|
3611
|
+
* the user's prompt matches a workflow's `whenToUse` trigger. Picking a
|
|
3612
|
+
* workflow short-circuits analysis text + dashboard component generation —
|
|
3613
|
+
* the workflow component is returned directly, with the LLM-extracted props.
|
|
3614
|
+
*/
|
|
3615
|
+
setWorkflows(workflows: WorkflowDescriptor[]): void;
|
|
3616
|
+
/**
|
|
3617
|
+
* Get the registered workflow components.
|
|
3618
|
+
*/
|
|
3619
|
+
getWorkflows(): WorkflowDescriptor[];
|
|
3253
3620
|
/**
|
|
3254
3621
|
* Apply model strategy to all LLM provider singletons
|
|
3255
3622
|
* @param strategy - 'best', 'fast', or 'balanced'
|
|
@@ -3280,4 +3647,4 @@ declare class SuperatomSDK {
|
|
|
3280
3647
|
getConversationSimilarityThreshold(): number;
|
|
3281
3648
|
}
|
|
3282
3649
|
|
|
3283
|
-
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 };
|
|
3650
|
+
export { type Action, type AgentConfig, type AgentResponse, BM25L, type BM25LOptions, type BaseLLMConfig, CONTEXT_CONFIG, type CapturedLog, CleanupService, type CollectionHandler, type CollectionOperation, type DBUIBlock, DEFAULT_AGENT_CONFIG, type DatabaseType, type HybridSearchOptions, type IncomingMessage, type KbNodesQueryFilters, type KbNodesRequestPayload, LLM, type LLMUsageEntry, type LogLevel, MainAgent, type Message, type ModelStrategy, type OutputField, type RerankedResult, STORAGE_CONFIG, type ScriptComponentSpec, ScriptMatcher, type ScriptParameter, type ScriptRecipe, type ScriptRecipeMetaRow, type ScriptRecipeStore, type ScriptResult, ScriptStore, type ScriptStoreOptions, type SelectedWorkflow, SuperatomSDK, type SuperatomSDKConfig, type TaskType, Thread, ThreadManager, type Tool$1 as Tool, type ToolOutputSchema, UIBlock, UILogCollector, type User, UserManager, type UsersData, type WorkflowDescriptor, anthropicLLM, dashboardConversationHistory, geminiLLM, groqLLM, hybridRerank, llmUsageLogger, logger, normalizeScriptBody, openaiLLM, queryCache, rerankChromaResults, rerankConversationResults, resolveScriptRecipeStore, runScript, userPromptErrorLogger };
|