@superatomai/sdk-node 0.0.5 → 0.0.6-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/README.md +251 -33
- package/dist/index.d.mts +2401 -111
- package/dist/index.d.ts +2401 -111
- package/dist/index.js +19225 -3347
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +19207 -3369
- package/dist/index.mjs.map +1 -1
- package/dist/userResponse/scripts/script-bootstrap.d.mts +2 -0
- package/dist/userResponse/scripts/script-bootstrap.d.ts +2 -0
- package/dist/userResponse/scripts/script-bootstrap.js +286 -0
- package/dist/userResponse/scripts/script-bootstrap.js.map +1 -0
- package/dist/userResponse/scripts/script-bootstrap.mjs +284 -0
- package/dist/userResponse/scripts/script-bootstrap.mjs.map +1 -0
- package/package.json +4 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,16 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
|
+
import Anthropic from '@anthropic-ai/sdk';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Unified UIBlock structure for database storage
|
|
6
|
+
* Used in both bookmarks and user-conversations tables
|
|
7
|
+
*/
|
|
8
|
+
interface DBUIBlock {
|
|
9
|
+
id: string;
|
|
10
|
+
component: Record<string, any> | null;
|
|
11
|
+
analysis: string | null;
|
|
12
|
+
user_prompt: string;
|
|
13
|
+
}
|
|
2
14
|
|
|
3
15
|
/**
|
|
4
16
|
* Log levels in hierarchical order
|
|
@@ -47,6 +59,18 @@ declare class Logger {
|
|
|
47
59
|
* Log debug message (only shown for verbose level)
|
|
48
60
|
*/
|
|
49
61
|
debug(...args: any[]): void;
|
|
62
|
+
/**
|
|
63
|
+
* Write to log file
|
|
64
|
+
*/
|
|
65
|
+
file(...args: any[]): void;
|
|
66
|
+
/**
|
|
67
|
+
* Clear the log file (call at start of new user request)
|
|
68
|
+
*/
|
|
69
|
+
clearFile(): void;
|
|
70
|
+
/**
|
|
71
|
+
* Log LLM method prompts with clear labeling
|
|
72
|
+
*/
|
|
73
|
+
logLLMPrompt(methodName: string, promptType: 'system' | 'user', content: string | object | any[]): void;
|
|
50
74
|
}
|
|
51
75
|
declare const logger: Logger;
|
|
52
76
|
|
|
@@ -77,7 +101,27 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
|
|
|
77
101
|
deps?: string[] | undefined;
|
|
78
102
|
}>, "many">>;
|
|
79
103
|
data: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
|
|
80
|
-
render: z.ZodType<any, z.ZodTypeDef, any
|
|
104
|
+
render: z.ZodOptional<z.ZodType<any, z.ZodTypeDef, any>>;
|
|
105
|
+
pages: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
106
|
+
id: z.ZodString;
|
|
107
|
+
name: z.ZodString;
|
|
108
|
+
order: z.ZodNumber;
|
|
109
|
+
icon: z.ZodOptional<z.ZodString>;
|
|
110
|
+
render: z.ZodType<any, z.ZodTypeDef, any>;
|
|
111
|
+
}, "strip", z.ZodTypeAny, {
|
|
112
|
+
id: string;
|
|
113
|
+
name: string;
|
|
114
|
+
order: number;
|
|
115
|
+
icon?: string | undefined;
|
|
116
|
+
render?: any;
|
|
117
|
+
}, {
|
|
118
|
+
id: string;
|
|
119
|
+
name: string;
|
|
120
|
+
order: number;
|
|
121
|
+
icon?: string | undefined;
|
|
122
|
+
render?: any;
|
|
123
|
+
}>, "many">>;
|
|
124
|
+
defaultPageId: z.ZodOptional<z.ZodString>;
|
|
81
125
|
query: z.ZodOptional<z.ZodObject<{
|
|
82
126
|
graphql: z.ZodOptional<z.ZodString>;
|
|
83
127
|
sql: z.ZodOptional<z.ZodString>;
|
|
@@ -116,6 +160,7 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
|
|
|
116
160
|
dependencies?: string[] | undefined;
|
|
117
161
|
} | undefined;
|
|
118
162
|
props?: Record<string, any> | undefined;
|
|
163
|
+
render?: any;
|
|
119
164
|
states?: Record<string, any> | undefined;
|
|
120
165
|
methods?: Record<string, {
|
|
121
166
|
fn: string;
|
|
@@ -126,7 +171,14 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
|
|
|
126
171
|
deps?: string[] | undefined;
|
|
127
172
|
}[] | undefined;
|
|
128
173
|
data?: Record<string, any> | undefined;
|
|
129
|
-
|
|
174
|
+
pages?: {
|
|
175
|
+
id: string;
|
|
176
|
+
name: string;
|
|
177
|
+
order: number;
|
|
178
|
+
icon?: string | undefined;
|
|
179
|
+
render?: any;
|
|
180
|
+
}[] | undefined;
|
|
181
|
+
defaultPageId?: string | undefined;
|
|
130
182
|
}, {
|
|
131
183
|
id: string;
|
|
132
184
|
name?: string | undefined;
|
|
@@ -140,6 +192,7 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
|
|
|
140
192
|
dependencies?: string[] | undefined;
|
|
141
193
|
} | undefined;
|
|
142
194
|
props?: Record<string, any> | undefined;
|
|
195
|
+
render?: any;
|
|
143
196
|
states?: Record<string, any> | undefined;
|
|
144
197
|
methods?: Record<string, {
|
|
145
198
|
fn: string;
|
|
@@ -150,7 +203,14 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
|
|
|
150
203
|
deps?: string[] | undefined;
|
|
151
204
|
}[] | undefined;
|
|
152
205
|
data?: Record<string, any> | undefined;
|
|
153
|
-
|
|
206
|
+
pages?: {
|
|
207
|
+
id: string;
|
|
208
|
+
name: string;
|
|
209
|
+
order: number;
|
|
210
|
+
icon?: string | undefined;
|
|
211
|
+
render?: any;
|
|
212
|
+
}[] | undefined;
|
|
213
|
+
defaultPageId?: string | undefined;
|
|
154
214
|
}>;
|
|
155
215
|
data: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
|
|
156
216
|
context: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
|
|
@@ -168,6 +228,7 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
|
|
|
168
228
|
dependencies?: string[] | undefined;
|
|
169
229
|
} | undefined;
|
|
170
230
|
props?: Record<string, any> | undefined;
|
|
231
|
+
render?: any;
|
|
171
232
|
states?: Record<string, any> | undefined;
|
|
172
233
|
methods?: Record<string, {
|
|
173
234
|
fn: string;
|
|
@@ -178,7 +239,14 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
|
|
|
178
239
|
deps?: string[] | undefined;
|
|
179
240
|
}[] | undefined;
|
|
180
241
|
data?: Record<string, any> | undefined;
|
|
181
|
-
|
|
242
|
+
pages?: {
|
|
243
|
+
id: string;
|
|
244
|
+
name: string;
|
|
245
|
+
order: number;
|
|
246
|
+
icon?: string | undefined;
|
|
247
|
+
render?: any;
|
|
248
|
+
}[] | undefined;
|
|
249
|
+
defaultPageId?: string | undefined;
|
|
182
250
|
};
|
|
183
251
|
data?: Record<string, any> | undefined;
|
|
184
252
|
context?: Record<string, any> | undefined;
|
|
@@ -196,6 +264,7 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
|
|
|
196
264
|
dependencies?: string[] | undefined;
|
|
197
265
|
} | undefined;
|
|
198
266
|
props?: Record<string, any> | undefined;
|
|
267
|
+
render?: any;
|
|
199
268
|
states?: Record<string, any> | undefined;
|
|
200
269
|
methods?: Record<string, {
|
|
201
270
|
fn: string;
|
|
@@ -206,7 +275,14 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
|
|
|
206
275
|
deps?: string[] | undefined;
|
|
207
276
|
}[] | undefined;
|
|
208
277
|
data?: Record<string, any> | undefined;
|
|
209
|
-
|
|
278
|
+
pages?: {
|
|
279
|
+
id: string;
|
|
280
|
+
name: string;
|
|
281
|
+
order: number;
|
|
282
|
+
icon?: string | undefined;
|
|
283
|
+
render?: any;
|
|
284
|
+
}[] | undefined;
|
|
285
|
+
defaultPageId?: string | undefined;
|
|
210
286
|
};
|
|
211
287
|
data?: Record<string, any> | undefined;
|
|
212
288
|
context?: Record<string, any> | undefined;
|
|
@@ -279,6 +355,7 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
|
|
|
279
355
|
dependencies?: string[] | undefined;
|
|
280
356
|
} | undefined;
|
|
281
357
|
props?: Record<string, any> | undefined;
|
|
358
|
+
render?: any;
|
|
282
359
|
states?: Record<string, any> | undefined;
|
|
283
360
|
methods?: Record<string, {
|
|
284
361
|
fn: string;
|
|
@@ -289,7 +366,6 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
|
|
|
289
366
|
deps?: string[] | undefined;
|
|
290
367
|
}[] | undefined;
|
|
291
368
|
data?: Record<string, any> | undefined;
|
|
292
|
-
render?: any;
|
|
293
369
|
}, {
|
|
294
370
|
id: string;
|
|
295
371
|
name?: string | undefined;
|
|
@@ -303,6 +379,7 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
|
|
|
303
379
|
dependencies?: string[] | undefined;
|
|
304
380
|
} | undefined;
|
|
305
381
|
props?: Record<string, any> | undefined;
|
|
382
|
+
render?: any;
|
|
306
383
|
states?: Record<string, any> | undefined;
|
|
307
384
|
methods?: Record<string, {
|
|
308
385
|
fn: string;
|
|
@@ -313,7 +390,6 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
|
|
|
313
390
|
deps?: string[] | undefined;
|
|
314
391
|
}[] | undefined;
|
|
315
392
|
data?: Record<string, any> | undefined;
|
|
316
|
-
render?: any;
|
|
317
393
|
}>;
|
|
318
394
|
data: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
|
|
319
395
|
context: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
|
|
@@ -331,6 +407,7 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
|
|
|
331
407
|
dependencies?: string[] | undefined;
|
|
332
408
|
} | undefined;
|
|
333
409
|
props?: Record<string, any> | undefined;
|
|
410
|
+
render?: any;
|
|
334
411
|
states?: Record<string, any> | undefined;
|
|
335
412
|
methods?: Record<string, {
|
|
336
413
|
fn: string;
|
|
@@ -341,7 +418,6 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
|
|
|
341
418
|
deps?: string[] | undefined;
|
|
342
419
|
}[] | undefined;
|
|
343
420
|
data?: Record<string, any> | undefined;
|
|
344
|
-
render?: any;
|
|
345
421
|
};
|
|
346
422
|
data?: Record<string, any> | undefined;
|
|
347
423
|
context?: Record<string, any> | undefined;
|
|
@@ -359,6 +435,7 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
|
|
|
359
435
|
dependencies?: string[] | undefined;
|
|
360
436
|
} | undefined;
|
|
361
437
|
props?: Record<string, any> | undefined;
|
|
438
|
+
render?: any;
|
|
362
439
|
states?: Record<string, any> | undefined;
|
|
363
440
|
methods?: Record<string, {
|
|
364
441
|
fn: string;
|
|
@@ -369,7 +446,6 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
|
|
|
369
446
|
deps?: string[] | undefined;
|
|
370
447
|
}[] | undefined;
|
|
371
448
|
data?: Record<string, any> | undefined;
|
|
372
|
-
render?: any;
|
|
373
449
|
};
|
|
374
450
|
data?: Record<string, any> | undefined;
|
|
375
451
|
context?: Record<string, any> | undefined;
|
|
@@ -382,6 +458,7 @@ declare const UserSchema: z.ZodObject<{
|
|
|
382
458
|
password: z.ZodString;
|
|
383
459
|
fullname: z.ZodOptional<z.ZodString>;
|
|
384
460
|
role: z.ZodOptional<z.ZodString>;
|
|
461
|
+
userInfo: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
385
462
|
wsIds: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
386
463
|
}, "strip", z.ZodTypeAny, {
|
|
387
464
|
username: string;
|
|
@@ -389,6 +466,7 @@ declare const UserSchema: z.ZodObject<{
|
|
|
389
466
|
email?: string | undefined;
|
|
390
467
|
fullname?: string | undefined;
|
|
391
468
|
role?: string | undefined;
|
|
469
|
+
userInfo?: Record<string, unknown> | undefined;
|
|
392
470
|
wsIds?: string[] | undefined;
|
|
393
471
|
}, {
|
|
394
472
|
username: string;
|
|
@@ -396,6 +474,7 @@ declare const UserSchema: z.ZodObject<{
|
|
|
396
474
|
email?: string | undefined;
|
|
397
475
|
fullname?: string | undefined;
|
|
398
476
|
role?: string | undefined;
|
|
477
|
+
userInfo?: Record<string, unknown> | undefined;
|
|
399
478
|
wsIds?: string[] | undefined;
|
|
400
479
|
}>;
|
|
401
480
|
type User = z.infer<typeof UserSchema>;
|
|
@@ -406,6 +485,7 @@ declare const UsersDataSchema: z.ZodObject<{
|
|
|
406
485
|
password: z.ZodString;
|
|
407
486
|
fullname: z.ZodOptional<z.ZodString>;
|
|
408
487
|
role: z.ZodOptional<z.ZodString>;
|
|
488
|
+
userInfo: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
409
489
|
wsIds: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
410
490
|
}, "strip", z.ZodTypeAny, {
|
|
411
491
|
username: string;
|
|
@@ -413,6 +493,7 @@ declare const UsersDataSchema: z.ZodObject<{
|
|
|
413
493
|
email?: string | undefined;
|
|
414
494
|
fullname?: string | undefined;
|
|
415
495
|
role?: string | undefined;
|
|
496
|
+
userInfo?: Record<string, unknown> | undefined;
|
|
416
497
|
wsIds?: string[] | undefined;
|
|
417
498
|
}, {
|
|
418
499
|
username: string;
|
|
@@ -420,6 +501,7 @@ declare const UsersDataSchema: z.ZodObject<{
|
|
|
420
501
|
email?: string | undefined;
|
|
421
502
|
fullname?: string | undefined;
|
|
422
503
|
role?: string | undefined;
|
|
504
|
+
userInfo?: Record<string, unknown> | undefined;
|
|
423
505
|
wsIds?: string[] | undefined;
|
|
424
506
|
}>, "many">;
|
|
425
507
|
}, "strip", z.ZodTypeAny, {
|
|
@@ -429,6 +511,7 @@ declare const UsersDataSchema: z.ZodObject<{
|
|
|
429
511
|
email?: string | undefined;
|
|
430
512
|
fullname?: string | undefined;
|
|
431
513
|
role?: string | undefined;
|
|
514
|
+
userInfo?: Record<string, unknown> | undefined;
|
|
432
515
|
wsIds?: string[] | undefined;
|
|
433
516
|
}[];
|
|
434
517
|
}, {
|
|
@@ -438,6 +521,7 @@ declare const UsersDataSchema: z.ZodObject<{
|
|
|
438
521
|
email?: string | undefined;
|
|
439
522
|
fullname?: string | undefined;
|
|
440
523
|
role?: string | undefined;
|
|
524
|
+
userInfo?: Record<string, unknown> | undefined;
|
|
441
525
|
wsIds?: string[] | undefined;
|
|
442
526
|
}[];
|
|
443
527
|
}>;
|
|
@@ -542,22 +626,439 @@ declare const IncomingMessageSchema: z.ZodObject<{
|
|
|
542
626
|
payload?: unknown;
|
|
543
627
|
}>;
|
|
544
628
|
type IncomingMessage = z.infer<typeof IncomingMessageSchema>;
|
|
629
|
+
declare const ComponentSchema: z.ZodObject<{
|
|
630
|
+
id: z.ZodString;
|
|
631
|
+
name: z.ZodString;
|
|
632
|
+
displayName: z.ZodOptional<z.ZodString>;
|
|
633
|
+
isDisplayComp: z.ZodOptional<z.ZodBoolean>;
|
|
634
|
+
type: z.ZodString;
|
|
635
|
+
description: z.ZodString;
|
|
636
|
+
props: z.ZodObject<{
|
|
637
|
+
query: z.ZodOptional<z.ZodNullable<z.ZodUnion<[z.ZodString, z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>]>>>;
|
|
638
|
+
title: z.ZodOptional<z.ZodString>;
|
|
639
|
+
description: z.ZodOptional<z.ZodString>;
|
|
640
|
+
config: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
641
|
+
actions: z.ZodOptional<z.ZodArray<z.ZodAny, "many">>;
|
|
642
|
+
}, "passthrough", z.ZodTypeAny, z.objectOutputType<{
|
|
643
|
+
query: z.ZodOptional<z.ZodNullable<z.ZodUnion<[z.ZodString, z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>]>>>;
|
|
644
|
+
title: z.ZodOptional<z.ZodString>;
|
|
645
|
+
description: z.ZodOptional<z.ZodString>;
|
|
646
|
+
config: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
647
|
+
actions: z.ZodOptional<z.ZodArray<z.ZodAny, "many">>;
|
|
648
|
+
}, z.ZodTypeAny, "passthrough">, z.objectInputType<{
|
|
649
|
+
query: z.ZodOptional<z.ZodNullable<z.ZodUnion<[z.ZodString, z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>]>>>;
|
|
650
|
+
title: z.ZodOptional<z.ZodString>;
|
|
651
|
+
description: z.ZodOptional<z.ZodString>;
|
|
652
|
+
config: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
653
|
+
actions: z.ZodOptional<z.ZodArray<z.ZodAny, "many">>;
|
|
654
|
+
}, z.ZodTypeAny, "passthrough">>;
|
|
655
|
+
category: z.ZodOptional<z.ZodString>;
|
|
656
|
+
keywords: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
657
|
+
}, "strip", z.ZodTypeAny, {
|
|
658
|
+
id: string;
|
|
659
|
+
type: string;
|
|
660
|
+
name: string;
|
|
661
|
+
description: string;
|
|
662
|
+
props: {
|
|
663
|
+
description?: string | undefined;
|
|
664
|
+
query?: string | {} | null | undefined;
|
|
665
|
+
title?: string | undefined;
|
|
666
|
+
config?: Record<string, unknown> | undefined;
|
|
667
|
+
actions?: any[] | undefined;
|
|
668
|
+
} & {
|
|
669
|
+
[k: string]: unknown;
|
|
670
|
+
};
|
|
671
|
+
displayName?: string | undefined;
|
|
672
|
+
isDisplayComp?: boolean | undefined;
|
|
673
|
+
category?: string | undefined;
|
|
674
|
+
keywords?: string[] | undefined;
|
|
675
|
+
}, {
|
|
676
|
+
id: string;
|
|
677
|
+
type: string;
|
|
678
|
+
name: string;
|
|
679
|
+
description: string;
|
|
680
|
+
props: {
|
|
681
|
+
description?: string | undefined;
|
|
682
|
+
query?: string | {} | null | undefined;
|
|
683
|
+
title?: string | undefined;
|
|
684
|
+
config?: Record<string, unknown> | undefined;
|
|
685
|
+
actions?: any[] | undefined;
|
|
686
|
+
} & {
|
|
687
|
+
[k: string]: unknown;
|
|
688
|
+
};
|
|
689
|
+
displayName?: string | undefined;
|
|
690
|
+
isDisplayComp?: boolean | undefined;
|
|
691
|
+
category?: string | undefined;
|
|
692
|
+
keywords?: string[] | undefined;
|
|
693
|
+
}>;
|
|
694
|
+
type Component = z.infer<typeof ComponentSchema>;
|
|
695
|
+
declare const OutputFieldSchema: z.ZodObject<{
|
|
696
|
+
name: z.ZodString;
|
|
697
|
+
type: z.ZodEnum<["string", "number", "boolean", "date"]>;
|
|
698
|
+
description: z.ZodString;
|
|
699
|
+
}, "strip", z.ZodTypeAny, {
|
|
700
|
+
type: "string" | "number" | "boolean" | "date";
|
|
701
|
+
name: string;
|
|
702
|
+
description: string;
|
|
703
|
+
}, {
|
|
704
|
+
type: "string" | "number" | "boolean" | "date";
|
|
705
|
+
name: string;
|
|
706
|
+
description: string;
|
|
707
|
+
}>;
|
|
708
|
+
type OutputField = z.infer<typeof OutputFieldSchema>;
|
|
709
|
+
declare const OutputSchema: z.ZodObject<{
|
|
710
|
+
description: z.ZodString;
|
|
711
|
+
fields: z.ZodArray<z.ZodObject<{
|
|
712
|
+
name: z.ZodString;
|
|
713
|
+
type: z.ZodEnum<["string", "number", "boolean", "date"]>;
|
|
714
|
+
description: z.ZodString;
|
|
715
|
+
}, "strip", z.ZodTypeAny, {
|
|
716
|
+
type: "string" | "number" | "boolean" | "date";
|
|
717
|
+
name: string;
|
|
718
|
+
description: string;
|
|
719
|
+
}, {
|
|
720
|
+
type: "string" | "number" | "boolean" | "date";
|
|
721
|
+
name: string;
|
|
722
|
+
description: string;
|
|
723
|
+
}>, "many">;
|
|
724
|
+
}, "strip", z.ZodTypeAny, {
|
|
725
|
+
description: string;
|
|
726
|
+
fields: {
|
|
727
|
+
type: "string" | "number" | "boolean" | "date";
|
|
728
|
+
name: string;
|
|
729
|
+
description: string;
|
|
730
|
+
}[];
|
|
731
|
+
}, {
|
|
732
|
+
description: string;
|
|
733
|
+
fields: {
|
|
734
|
+
type: "string" | "number" | "boolean" | "date";
|
|
735
|
+
name: string;
|
|
736
|
+
description: string;
|
|
737
|
+
}[];
|
|
738
|
+
}>;
|
|
739
|
+
type ToolOutputSchema = z.infer<typeof OutputSchema>;
|
|
740
|
+
declare const ToolSchema: z.ZodObject<{
|
|
741
|
+
id: z.ZodString;
|
|
742
|
+
name: z.ZodString;
|
|
743
|
+
description: z.ZodString;
|
|
744
|
+
/** Tool type: "source" = routed through SourceAgent, "direct" = called directly by MainAgent */
|
|
745
|
+
toolType: z.ZodOptional<z.ZodEnum<["source", "direct"]>>;
|
|
746
|
+
/** Full untruncated schema for source agent (all columns visible) */
|
|
747
|
+
fullSchema: z.ZodOptional<z.ZodString>;
|
|
748
|
+
params: z.ZodRecord<z.ZodString, z.ZodString>;
|
|
749
|
+
fn: z.ZodFunction<z.ZodTuple<[z.ZodAny], z.ZodUnknown>, z.ZodAny>;
|
|
750
|
+
outputSchema: z.ZodOptional<z.ZodObject<{
|
|
751
|
+
description: z.ZodString;
|
|
752
|
+
fields: z.ZodArray<z.ZodObject<{
|
|
753
|
+
name: z.ZodString;
|
|
754
|
+
type: z.ZodEnum<["string", "number", "boolean", "date"]>;
|
|
755
|
+
description: z.ZodString;
|
|
756
|
+
}, "strip", z.ZodTypeAny, {
|
|
757
|
+
type: "string" | "number" | "boolean" | "date";
|
|
758
|
+
name: string;
|
|
759
|
+
description: string;
|
|
760
|
+
}, {
|
|
761
|
+
type: "string" | "number" | "boolean" | "date";
|
|
762
|
+
name: string;
|
|
763
|
+
description: string;
|
|
764
|
+
}>, "many">;
|
|
765
|
+
}, "strip", z.ZodTypeAny, {
|
|
766
|
+
description: string;
|
|
767
|
+
fields: {
|
|
768
|
+
type: "string" | "number" | "boolean" | "date";
|
|
769
|
+
name: string;
|
|
770
|
+
description: string;
|
|
771
|
+
}[];
|
|
772
|
+
}, {
|
|
773
|
+
description: string;
|
|
774
|
+
fields: {
|
|
775
|
+
type: "string" | "number" | "boolean" | "date";
|
|
776
|
+
name: string;
|
|
777
|
+
description: string;
|
|
778
|
+
}[];
|
|
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
|
+
}>]>>;
|
|
788
|
+
}, "strip", z.ZodTypeAny, {
|
|
789
|
+
id: string;
|
|
790
|
+
params: Record<string, string>;
|
|
791
|
+
name: string;
|
|
792
|
+
description: string;
|
|
793
|
+
fn: (args_0: any, ...args: unknown[]) => any;
|
|
794
|
+
toolType?: "source" | "direct" | undefined;
|
|
795
|
+
fullSchema?: string | undefined;
|
|
796
|
+
outputSchema?: {
|
|
797
|
+
description: string;
|
|
798
|
+
fields: {
|
|
799
|
+
type: "string" | "number" | "boolean" | "date";
|
|
800
|
+
name: string;
|
|
801
|
+
description: string;
|
|
802
|
+
}[];
|
|
803
|
+
} | undefined;
|
|
804
|
+
cache?: false | {
|
|
805
|
+
ttlMs?: number | undefined;
|
|
806
|
+
} | undefined;
|
|
807
|
+
}, {
|
|
808
|
+
id: string;
|
|
809
|
+
params: Record<string, string>;
|
|
810
|
+
name: string;
|
|
811
|
+
description: string;
|
|
812
|
+
fn: (args_0: any, ...args: unknown[]) => any;
|
|
813
|
+
toolType?: "source" | "direct" | undefined;
|
|
814
|
+
fullSchema?: string | undefined;
|
|
815
|
+
outputSchema?: {
|
|
816
|
+
description: string;
|
|
817
|
+
fields: {
|
|
818
|
+
type: "string" | "number" | "boolean" | "date";
|
|
819
|
+
name: string;
|
|
820
|
+
description: string;
|
|
821
|
+
}[];
|
|
822
|
+
} | undefined;
|
|
823
|
+
cache?: false | {
|
|
824
|
+
ttlMs?: number | undefined;
|
|
825
|
+
} | undefined;
|
|
826
|
+
}>;
|
|
827
|
+
type Tool$1 = z.infer<typeof ToolSchema>;
|
|
545
828
|
type CollectionOperation = 'getMany' | 'getOne' | 'query' | 'mutation' | 'updateOne' | 'deleteOne' | 'createOne';
|
|
546
829
|
type CollectionHandler<TParams = any, TResult = any> = (params: TParams) => Promise<TResult> | TResult;
|
|
547
|
-
type LLMProvider = 'anthropic' | 'groq';
|
|
830
|
+
type LLMProvider = 'anthropic' | 'groq' | 'gemini' | 'openai';
|
|
548
831
|
|
|
832
|
+
type DatabaseType = 'postgresql' | 'mssql' | 'snowflake' | 'mysql';
|
|
833
|
+
/**
|
|
834
|
+
* Model strategy for controlling which models are used for different tasks
|
|
835
|
+
* - 'best': Use the best model (e.g., Sonnet) for all tasks - highest quality, higher cost
|
|
836
|
+
* - 'fast': Use the fast model (e.g., Haiku) for all tasks - lower quality, lower cost
|
|
837
|
+
* - 'balanced': Use best model for complex tasks, fast model for simple tasks (default)
|
|
838
|
+
*/
|
|
839
|
+
type ModelStrategy = 'best' | 'fast' | 'balanced';
|
|
840
|
+
/**
|
|
841
|
+
* Model configuration for DASH_COMP flow (dashboard component picking)
|
|
842
|
+
* Allows separate control of models used for component selection
|
|
843
|
+
*/
|
|
844
|
+
interface DashCompModelConfig {
|
|
845
|
+
/**
|
|
846
|
+
* Primary model for DASH_COMP requests
|
|
847
|
+
* Format: "provider/model-name" (e.g., "anthropic/claude-sonnet-4-5-20250929")
|
|
848
|
+
*/
|
|
849
|
+
model?: string;
|
|
850
|
+
/**
|
|
851
|
+
* Fast model for simpler DASH_COMP tasks (optional)
|
|
852
|
+
* Format: "provider/model-name" (e.g., "anthropic/claude-haiku-4-5-20251001")
|
|
853
|
+
*/
|
|
854
|
+
fastModel?: string;
|
|
855
|
+
}
|
|
549
856
|
interface SuperatomSDKConfig {
|
|
550
857
|
url?: string;
|
|
551
|
-
apiKey
|
|
858
|
+
apiKey?: string;
|
|
552
859
|
projectId: string;
|
|
553
|
-
userId?: string;
|
|
554
860
|
type?: string;
|
|
555
861
|
bundleDir?: string;
|
|
556
862
|
promptsDir?: string;
|
|
863
|
+
databaseType?: DatabaseType;
|
|
557
864
|
ANTHROPIC_API_KEY?: string;
|
|
558
865
|
GROQ_API_KEY?: string;
|
|
866
|
+
GEMINI_API_KEY?: string;
|
|
867
|
+
OPENAI_API_KEY?: string;
|
|
559
868
|
LLM_PROVIDERS?: LLMProvider[];
|
|
560
869
|
logLevel?: LogLevel;
|
|
870
|
+
/**
|
|
871
|
+
* Model selection strategy for LLM API calls:
|
|
872
|
+
* - 'best': Use best model for all tasks (highest quality, higher cost)
|
|
873
|
+
* - 'fast': Use fast model for all tasks (lower quality, lower cost)
|
|
874
|
+
* - 'balanced': Use best model for complex tasks, fast model for simple tasks (default)
|
|
875
|
+
*/
|
|
876
|
+
modelStrategy?: ModelStrategy;
|
|
877
|
+
/**
|
|
878
|
+
* Model for the main agent (routing + analysis).
|
|
879
|
+
* Format: "provider/model-name" (e.g., "anthropic/claude-haiku-4-5-20251001")
|
|
880
|
+
* If not set, uses the provider's default model.
|
|
881
|
+
*/
|
|
882
|
+
mainAgentModel?: string;
|
|
883
|
+
/**
|
|
884
|
+
* Model for source agents (per-source query generation).
|
|
885
|
+
* Format: "provider/model-name" (e.g., "anthropic/claude-haiku-4-5-20251001")
|
|
886
|
+
* If not set, uses the provider's default model.
|
|
887
|
+
*/
|
|
888
|
+
sourceAgentModel?: string;
|
|
889
|
+
/**
|
|
890
|
+
* Separate model configuration for DASH_COMP flow (dashboard component picking)
|
|
891
|
+
* If not provided, falls back to provider-based model selection
|
|
892
|
+
*/
|
|
893
|
+
dashCompModels?: DashCompModelConfig;
|
|
894
|
+
/**
|
|
895
|
+
* Similarity threshold for conversation search (semantic matching)
|
|
896
|
+
* Value between 0 and 1 (e.g., 0.8 = 80% similarity required)
|
|
897
|
+
* Higher values require closer matches, lower values allow more distant matches
|
|
898
|
+
* Default: 0.8
|
|
899
|
+
*/
|
|
900
|
+
conversationSimilarityThreshold?: number;
|
|
901
|
+
/**
|
|
902
|
+
* Query cache TTL (Time To Live) in minutes
|
|
903
|
+
* Cached query results expire after this duration
|
|
904
|
+
* Default: 5 minutes
|
|
905
|
+
*/
|
|
906
|
+
queryCacheTTL?: number;
|
|
907
|
+
/**
|
|
908
|
+
* Dashboard conversation history TTL (Time To Live) in minutes
|
|
909
|
+
* Per-dashboard conversation histories expire after this duration
|
|
910
|
+
* Default: 30 minutes
|
|
911
|
+
*/
|
|
912
|
+
dashboardHistoryTTL?: number;
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
declare const KbNodesQueryFiltersSchema: z.ZodObject<{
|
|
916
|
+
query: z.ZodOptional<z.ZodString>;
|
|
917
|
+
category: z.ZodOptional<z.ZodString>;
|
|
918
|
+
tags: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
919
|
+
type: z.ZodOptional<z.ZodEnum<["global", "user", "query"]>>;
|
|
920
|
+
createdBy: z.ZodOptional<z.ZodString>;
|
|
921
|
+
}, "strip", z.ZodTypeAny, {
|
|
922
|
+
type?: "query" | "user" | "global" | undefined;
|
|
923
|
+
query?: string | undefined;
|
|
924
|
+
category?: string | undefined;
|
|
925
|
+
createdBy?: string | undefined;
|
|
926
|
+
tags?: string[] | undefined;
|
|
927
|
+
}, {
|
|
928
|
+
type?: "query" | "user" | "global" | undefined;
|
|
929
|
+
query?: string | undefined;
|
|
930
|
+
category?: string | undefined;
|
|
931
|
+
createdBy?: string | undefined;
|
|
932
|
+
tags?: string[] | undefined;
|
|
933
|
+
}>;
|
|
934
|
+
type KbNodesQueryFilters = z.infer<typeof KbNodesQueryFiltersSchema>;
|
|
935
|
+
declare const KbNodesRequestPayloadSchema: z.ZodObject<{
|
|
936
|
+
operation: z.ZodEnum<["create", "update", "delete", "getAll", "getOne", "search", "getByCategory", "getByUser", "getCategories", "getTags"]>;
|
|
937
|
+
data: z.ZodOptional<z.ZodObject<{
|
|
938
|
+
id: z.ZodOptional<z.ZodNumber>;
|
|
939
|
+
title: z.ZodOptional<z.ZodString>;
|
|
940
|
+
content: z.ZodOptional<z.ZodString>;
|
|
941
|
+
category: z.ZodOptional<z.ZodString>;
|
|
942
|
+
tags: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
943
|
+
type: z.ZodOptional<z.ZodEnum<["global", "user", "query"]>>;
|
|
944
|
+
createdBy: z.ZodOptional<z.ZodString>;
|
|
945
|
+
updatedBy: z.ZodOptional<z.ZodString>;
|
|
946
|
+
userId: z.ZodOptional<z.ZodString>;
|
|
947
|
+
query: z.ZodOptional<z.ZodString>;
|
|
948
|
+
filters: z.ZodOptional<z.ZodObject<{
|
|
949
|
+
query: z.ZodOptional<z.ZodString>;
|
|
950
|
+
category: z.ZodOptional<z.ZodString>;
|
|
951
|
+
tags: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
952
|
+
type: z.ZodOptional<z.ZodEnum<["global", "user", "query"]>>;
|
|
953
|
+
createdBy: z.ZodOptional<z.ZodString>;
|
|
954
|
+
}, "strip", z.ZodTypeAny, {
|
|
955
|
+
type?: "query" | "user" | "global" | undefined;
|
|
956
|
+
query?: string | undefined;
|
|
957
|
+
category?: string | undefined;
|
|
958
|
+
createdBy?: string | undefined;
|
|
959
|
+
tags?: string[] | undefined;
|
|
960
|
+
}, {
|
|
961
|
+
type?: "query" | "user" | "global" | undefined;
|
|
962
|
+
query?: string | undefined;
|
|
963
|
+
category?: string | undefined;
|
|
964
|
+
createdBy?: string | undefined;
|
|
965
|
+
tags?: string[] | undefined;
|
|
966
|
+
}>>;
|
|
967
|
+
limit: z.ZodOptional<z.ZodNumber>;
|
|
968
|
+
offset: z.ZodOptional<z.ZodNumber>;
|
|
969
|
+
}, "strip", z.ZodTypeAny, {
|
|
970
|
+
id?: number | undefined;
|
|
971
|
+
type?: "query" | "user" | "global" | undefined;
|
|
972
|
+
query?: string | undefined;
|
|
973
|
+
title?: string | undefined;
|
|
974
|
+
category?: string | undefined;
|
|
975
|
+
userId?: string | undefined;
|
|
976
|
+
limit?: number | undefined;
|
|
977
|
+
filters?: {
|
|
978
|
+
type?: "query" | "user" | "global" | undefined;
|
|
979
|
+
query?: string | undefined;
|
|
980
|
+
category?: string | undefined;
|
|
981
|
+
createdBy?: string | undefined;
|
|
982
|
+
tags?: string[] | undefined;
|
|
983
|
+
} | undefined;
|
|
984
|
+
createdBy?: string | undefined;
|
|
985
|
+
updatedBy?: string | undefined;
|
|
986
|
+
offset?: number | undefined;
|
|
987
|
+
tags?: string[] | undefined;
|
|
988
|
+
content?: string | undefined;
|
|
989
|
+
}, {
|
|
990
|
+
id?: number | undefined;
|
|
991
|
+
type?: "query" | "user" | "global" | undefined;
|
|
992
|
+
query?: string | undefined;
|
|
993
|
+
title?: string | undefined;
|
|
994
|
+
category?: string | undefined;
|
|
995
|
+
userId?: string | undefined;
|
|
996
|
+
limit?: number | undefined;
|
|
997
|
+
filters?: {
|
|
998
|
+
type?: "query" | "user" | "global" | undefined;
|
|
999
|
+
query?: string | undefined;
|
|
1000
|
+
category?: string | undefined;
|
|
1001
|
+
createdBy?: string | undefined;
|
|
1002
|
+
tags?: string[] | undefined;
|
|
1003
|
+
} | undefined;
|
|
1004
|
+
createdBy?: string | undefined;
|
|
1005
|
+
updatedBy?: string | undefined;
|
|
1006
|
+
offset?: number | undefined;
|
|
1007
|
+
tags?: string[] | undefined;
|
|
1008
|
+
content?: string | undefined;
|
|
1009
|
+
}>>;
|
|
1010
|
+
}, "strip", z.ZodTypeAny, {
|
|
1011
|
+
operation: "create" | "getOne" | "update" | "delete" | "getAll" | "search" | "getByCategory" | "getByUser" | "getCategories" | "getTags";
|
|
1012
|
+
data?: {
|
|
1013
|
+
id?: number | undefined;
|
|
1014
|
+
type?: "query" | "user" | "global" | undefined;
|
|
1015
|
+
query?: string | undefined;
|
|
1016
|
+
title?: string | undefined;
|
|
1017
|
+
category?: string | undefined;
|
|
1018
|
+
userId?: string | undefined;
|
|
1019
|
+
limit?: number | undefined;
|
|
1020
|
+
filters?: {
|
|
1021
|
+
type?: "query" | "user" | "global" | undefined;
|
|
1022
|
+
query?: string | undefined;
|
|
1023
|
+
category?: string | undefined;
|
|
1024
|
+
createdBy?: string | undefined;
|
|
1025
|
+
tags?: string[] | undefined;
|
|
1026
|
+
} | undefined;
|
|
1027
|
+
createdBy?: string | undefined;
|
|
1028
|
+
updatedBy?: string | undefined;
|
|
1029
|
+
offset?: number | undefined;
|
|
1030
|
+
tags?: string[] | undefined;
|
|
1031
|
+
content?: string | undefined;
|
|
1032
|
+
} | undefined;
|
|
1033
|
+
}, {
|
|
1034
|
+
operation: "create" | "getOne" | "update" | "delete" | "getAll" | "search" | "getByCategory" | "getByUser" | "getCategories" | "getTags";
|
|
1035
|
+
data?: {
|
|
1036
|
+
id?: number | undefined;
|
|
1037
|
+
type?: "query" | "user" | "global" | undefined;
|
|
1038
|
+
query?: string | undefined;
|
|
1039
|
+
title?: string | undefined;
|
|
1040
|
+
category?: string | undefined;
|
|
1041
|
+
userId?: string | undefined;
|
|
1042
|
+
limit?: number | undefined;
|
|
1043
|
+
filters?: {
|
|
1044
|
+
type?: "query" | "user" | "global" | undefined;
|
|
1045
|
+
query?: string | undefined;
|
|
1046
|
+
category?: string | undefined;
|
|
1047
|
+
createdBy?: string | undefined;
|
|
1048
|
+
tags?: string[] | undefined;
|
|
1049
|
+
} | undefined;
|
|
1050
|
+
createdBy?: string | undefined;
|
|
1051
|
+
updatedBy?: string | undefined;
|
|
1052
|
+
offset?: number | undefined;
|
|
1053
|
+
tags?: string[] | undefined;
|
|
1054
|
+
content?: string | undefined;
|
|
1055
|
+
} | undefined;
|
|
1056
|
+
}>;
|
|
1057
|
+
type KbNodesRequestPayload = z.infer<typeof KbNodesRequestPayloadSchema>;
|
|
1058
|
+
interface T_RESPONSE {
|
|
1059
|
+
success: boolean;
|
|
1060
|
+
data?: any;
|
|
1061
|
+
errors: string[];
|
|
561
1062
|
}
|
|
562
1063
|
|
|
563
1064
|
/**
|
|
@@ -819,122 +1320,897 @@ declare class ReportManager {
|
|
|
819
1320
|
getReportCount(): number;
|
|
820
1321
|
}
|
|
821
1322
|
|
|
822
|
-
interface LLMMessages {
|
|
823
|
-
sys: string;
|
|
824
|
-
user: string;
|
|
825
|
-
}
|
|
826
|
-
interface LLMOptions {
|
|
827
|
-
model?: string;
|
|
828
|
-
maxTokens?: number;
|
|
829
|
-
temperature?: number;
|
|
830
|
-
topP?: number;
|
|
831
|
-
apiKey?: string;
|
|
832
|
-
partial?: (chunk: string) => void;
|
|
833
|
-
}
|
|
834
|
-
declare class LLM {
|
|
835
|
-
static text(messages: LLMMessages, options?: LLMOptions): Promise<string>;
|
|
836
|
-
static stream<T = string>(messages: LLMMessages, options?: LLMOptions, json?: boolean): Promise<T extends string ? string : any>;
|
|
837
|
-
/**
|
|
838
|
-
* Parse model string to extract provider and model name
|
|
839
|
-
* @param modelString - Format: "provider/model-name" or just "model-name"
|
|
840
|
-
* @returns [provider, modelName]
|
|
841
|
-
*
|
|
842
|
-
* @example
|
|
843
|
-
* "anthropic/claude-sonnet-4-5" → ["anthropic", "claude-sonnet-4-5"]
|
|
844
|
-
* "groq/openai/gpt-oss-120b" → ["groq", "openai/gpt-oss-120b"]
|
|
845
|
-
* "claude-sonnet-4-5" → ["anthropic", "claude-sonnet-4-5"] (default)
|
|
846
|
-
*/
|
|
847
|
-
private static _parseModel;
|
|
848
|
-
private static _anthropicText;
|
|
849
|
-
private static _anthropicStream;
|
|
850
|
-
private static _groqText;
|
|
851
|
-
private static _groqStream;
|
|
852
|
-
/**
|
|
853
|
-
* Parse JSON string, handling markdown code blocks and surrounding text
|
|
854
|
-
* Enhanced version from anthropic.ts implementation
|
|
855
|
-
* @param text - Text that may contain JSON wrapped in ```json...``` or with surrounding text
|
|
856
|
-
* @returns Parsed JSON object
|
|
857
|
-
*/
|
|
858
|
-
private static _parseJSON;
|
|
859
|
-
}
|
|
860
|
-
|
|
861
|
-
interface CapturedLog {
|
|
862
|
-
timestamp: number;
|
|
863
|
-
level: 'info' | 'error' | 'warn' | 'debug';
|
|
864
|
-
message: string;
|
|
865
|
-
type?: 'explanation' | 'query' | 'general';
|
|
866
|
-
data?: Record<string, any>;
|
|
867
|
-
}
|
|
868
1323
|
/**
|
|
869
|
-
*
|
|
870
|
-
*
|
|
871
|
-
* Logs are sent in real-time for streaming effect in the UI
|
|
872
|
-
* Respects the global log level configuration
|
|
1324
|
+
* StreamBuffer - Buffered streaming utility for smoother text delivery
|
|
1325
|
+
* Batches small chunks together and flushes at regular intervals
|
|
873
1326
|
*/
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
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);
|
|
881
1338
|
/**
|
|
882
|
-
* Check if
|
|
1339
|
+
* Check if the buffer has a callback configured
|
|
883
1340
|
*/
|
|
884
|
-
|
|
1341
|
+
hasCallback(): boolean;
|
|
885
1342
|
/**
|
|
886
|
-
*
|
|
1343
|
+
* Get all text that has been written (including already flushed)
|
|
887
1344
|
*/
|
|
888
|
-
|
|
1345
|
+
getFullText(): string;
|
|
889
1346
|
/**
|
|
890
|
-
*
|
|
891
|
-
*
|
|
1347
|
+
* Write a chunk to the buffer
|
|
1348
|
+
* Large chunks or chunks with newlines are flushed immediately
|
|
1349
|
+
* Small chunks are batched and flushed after a short interval
|
|
1350
|
+
*
|
|
1351
|
+
* @param chunk - Text chunk to write
|
|
892
1352
|
*/
|
|
893
|
-
|
|
1353
|
+
write(chunk: string): void;
|
|
894
1354
|
/**
|
|
895
|
-
*
|
|
1355
|
+
* Flush the buffer immediately
|
|
1356
|
+
* Call this before tool execution or other operations that need clean output
|
|
896
1357
|
*/
|
|
897
|
-
|
|
1358
|
+
flush(): void;
|
|
898
1359
|
/**
|
|
899
|
-
*
|
|
1360
|
+
* Internal flush implementation
|
|
900
1361
|
*/
|
|
901
|
-
|
|
1362
|
+
private flushNow;
|
|
902
1363
|
/**
|
|
903
|
-
*
|
|
1364
|
+
* Clean up resources
|
|
1365
|
+
* Call this when done with the buffer
|
|
904
1366
|
*/
|
|
905
|
-
|
|
1367
|
+
dispose(): void;
|
|
1368
|
+
}
|
|
1369
|
+
|
|
1370
|
+
/**
|
|
1371
|
+
* ToolExecutorService - Handles execution of SQL queries and external tools
|
|
1372
|
+
* Extracted from BaseLLM.generateTextResponse for better separation of concerns
|
|
1373
|
+
*/
|
|
1374
|
+
|
|
1375
|
+
/**
|
|
1376
|
+
* External tool definition
|
|
1377
|
+
*/
|
|
1378
|
+
interface ExternalTool {
|
|
1379
|
+
id: string;
|
|
1380
|
+
name: string;
|
|
1381
|
+
description?: string;
|
|
1382
|
+
/** Tool type: "source" = routed through SourceAgent, "direct" = called directly by MainAgent */
|
|
1383
|
+
toolType?: 'source' | 'direct';
|
|
1384
|
+
/** Full untruncated schema for source agent (all columns visible) */
|
|
1385
|
+
fullSchema?: string;
|
|
1386
|
+
/** Schema size tier: small (≤50 tables), medium (51-200), large (201-500), very_large (500+) */
|
|
1387
|
+
schemaTier?: string;
|
|
1388
|
+
/** Schema search function for very_large tier — keyword search over entities */
|
|
1389
|
+
schemaSearchFn?: (keywords: string[]) => string;
|
|
1390
|
+
fn: (input: any) => Promise<any>;
|
|
1391
|
+
limit?: number;
|
|
1392
|
+
outputSchema?: any;
|
|
1393
|
+
executionType?: 'immediate' | 'deferred';
|
|
1394
|
+
userProvidedData?: any;
|
|
1395
|
+
params?: Record<string, any>;
|
|
1396
|
+
}
|
|
1397
|
+
/**
|
|
1398
|
+
* Executed tool tracking info
|
|
1399
|
+
*/
|
|
1400
|
+
interface ExecutedToolInfo {
|
|
1401
|
+
id: string;
|
|
1402
|
+
name: string;
|
|
1403
|
+
params: any;
|
|
1404
|
+
result: {
|
|
1405
|
+
_totalRecords: number;
|
|
1406
|
+
_recordsShown: number;
|
|
1407
|
+
_metadata?: any;
|
|
1408
|
+
_sampleData: any[];
|
|
1409
|
+
/** 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;
|
|
1417
|
+
}
|
|
1418
|
+
|
|
1419
|
+
/**
|
|
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.
|
|
1428
|
+
*/
|
|
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) */
|
|
1448
|
+
id: string;
|
|
1449
|
+
/** Human-readable source name */
|
|
1450
|
+
name: string;
|
|
1451
|
+
/** Source type: postgres, excel, rest_api, etc. */
|
|
1452
|
+
type: string;
|
|
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;
|
|
1459
|
+
}
|
|
1460
|
+
/**
|
|
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;
|
|
906
1512
|
/**
|
|
907
|
-
*
|
|
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."
|
|
908
1517
|
*/
|
|
909
|
-
|
|
1518
|
+
whenToUse: string;
|
|
910
1519
|
/**
|
|
911
|
-
*
|
|
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
|
+
* ```
|
|
912
1532
|
*/
|
|
913
|
-
|
|
1533
|
+
propsSchema: Record<string, string>;
|
|
914
1534
|
/**
|
|
915
|
-
*
|
|
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.
|
|
916
1538
|
*/
|
|
917
|
-
|
|
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[];
|
|
918
1562
|
/**
|
|
919
|
-
*
|
|
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).
|
|
920
1566
|
*/
|
|
921
|
-
|
|
1567
|
+
savedScript?: AgentWrittenScript;
|
|
922
1568
|
/**
|
|
923
|
-
*
|
|
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.
|
|
924
1572
|
*/
|
|
925
|
-
|
|
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 {
|
|
926
1580
|
/**
|
|
927
|
-
*
|
|
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.
|
|
928
1585
|
*/
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
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;
|
|
1628
|
+
/**
|
|
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.
|
|
1634
|
+
*/
|
|
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;
|
|
1691
|
+
/**
|
|
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.
|
|
1695
|
+
*/
|
|
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;
|
|
1713
|
+
/**
|
|
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.
|
|
1717
|
+
*/
|
|
1718
|
+
parentId?: string;
|
|
1719
|
+
/** 0 for root scripts; `parent.forkDepth + 1` for forks. Capped at 3. */
|
|
1720
|
+
forkDepth?: number;
|
|
1721
|
+
/**
|
|
1722
|
+
* Brief description of what this fork changed vs its parent
|
|
1723
|
+
* (sourced from the matcher's `modificationHint`).
|
|
1724
|
+
*/
|
|
1725
|
+
forkReason?: string;
|
|
1726
|
+
/**
|
|
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.
|
|
1733
|
+
*/
|
|
1734
|
+
components?: ScriptComponentSpec[];
|
|
1735
|
+
/**
|
|
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.
|
|
932
1747
|
*/
|
|
933
|
-
|
|
1748
|
+
status?: 'draft' | 'verified';
|
|
934
1749
|
/**
|
|
935
|
-
*
|
|
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.
|
|
936
1752
|
*/
|
|
937
|
-
|
|
1753
|
+
turnId?: string;
|
|
1754
|
+
/**
|
|
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'.
|
|
1758
|
+
*/
|
|
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;
|
|
1811
|
+
/**
|
|
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).
|
|
1815
|
+
*/
|
|
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;
|
|
1839
|
+
/**
|
|
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.
|
|
1845
|
+
*/
|
|
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;
|
|
1888
|
+
}
|
|
1889
|
+
|
|
1890
|
+
/**
|
|
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).
|
|
1901
|
+
*/
|
|
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>;
|
|
2066
|
+
/**
|
|
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;
|
|
2121
|
+
/**
|
|
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.
|
|
2127
|
+
*/
|
|
2128
|
+
private scriptStore;
|
|
2129
|
+
private turnId;
|
|
2130
|
+
private createdFromPrompt;
|
|
2131
|
+
private scriptState;
|
|
2132
|
+
/**
|
|
2133
|
+
* Fork mode — set when this turn is adapting a near-matching parent script.
|
|
2134
|
+
* In fork mode there is no legitimate "answer with bare text" outcome: the
|
|
2135
|
+
* only correct first move is a tool call (write_script, or a source tool for
|
|
2136
|
+
* schema discovery). We therefore force tool use on the first LLM iteration
|
|
2137
|
+
* so the model can't end its turn with a bare "I'll adapt…" preamble and zero
|
|
2138
|
+
* tool calls. Never set on the fresh-authoring / general-question path.
|
|
2139
|
+
*/
|
|
2140
|
+
private forkMode;
|
|
2141
|
+
constructor(externalTools: ExternalTool[], config: AgentConfig, scriptStore?: ScriptStore, turnId?: string, streamBuffer?: StreamBuffer, workflows?: WorkflowDescriptor[], forkMode?: boolean);
|
|
2142
|
+
private get scriptingEnabled();
|
|
2143
|
+
/**
|
|
2144
|
+
* Handle a user question using the multi-agent system.
|
|
2145
|
+
*
|
|
2146
|
+
* This is ONE LLM.streamWithTools() call. The LLM:
|
|
2147
|
+
* 1. Sees source summaries + direct tool descriptions in system prompt
|
|
2148
|
+
* 2. Decides which tool(s) to call (routing)
|
|
2149
|
+
* 3. Source tools → SourceAgent runs independently → returns data
|
|
2150
|
+
* 4. Direct tools → fn() called directly with LLM params → returns data
|
|
2151
|
+
* 5. Generates final analysis text
|
|
2152
|
+
*/
|
|
2153
|
+
handleQuestion(userPrompt: string, apiKey?: string, conversationHistory?: string, streamCallback?: (chunk: string) => void): Promise<AgentResponse>;
|
|
2154
|
+
private handleWriteScript;
|
|
2155
|
+
private handleExecuteScript;
|
|
2156
|
+
/**
|
|
2157
|
+
* Build the AgentWrittenScript payload the caller will hand to
|
|
2158
|
+
* `ScriptStore.promoteToVerified()`. Only returned when a verified
|
|
2159
|
+
* successful execution is on record.
|
|
2160
|
+
*/
|
|
2161
|
+
private buildSavedScript;
|
|
2162
|
+
private normalizeParameterList;
|
|
2163
|
+
/**
|
|
2164
|
+
* Use the schema embedding collection to pre-select relevant tables for
|
|
2165
|
+
* this source + intent. Returns a formatted schema block if confidence is
|
|
2166
|
+
* high (top match ≥ 0.55 and ≥3 candidates), otherwise null.
|
|
2167
|
+
*
|
|
2168
|
+
* When this returns a block, we can skip the SourceAgent's `search_schema`
|
|
2169
|
+
* loop and reduce iteration budget. When it returns null, the SourceAgent
|
|
2170
|
+
* falls back to the existing LLM-driven keyword search (same as today).
|
|
2171
|
+
*/
|
|
2172
|
+
private preResolveSchema;
|
|
2173
|
+
/**
|
|
2174
|
+
* Execute a direct tool — call fn() with LLM-provided params, no SourceAgent.
|
|
2175
|
+
*/
|
|
2176
|
+
private handleDirectTool;
|
|
2177
|
+
/**
|
|
2178
|
+
* Build the main agent's system prompt with source summaries, direct tool descriptions,
|
|
2179
|
+
* and workflow component descriptions.
|
|
2180
|
+
*/
|
|
2181
|
+
private buildSystemPrompt;
|
|
2182
|
+
/**
|
|
2183
|
+
* Build tool definitions for source tools — summary-only descriptions.
|
|
2184
|
+
* The full schema is inside the SourceAgent which runs independently.
|
|
2185
|
+
*/
|
|
2186
|
+
private buildSourceToolDefinitions;
|
|
2187
|
+
/**
|
|
2188
|
+
* Build tool definitions for direct tools — expose their actual params.
|
|
2189
|
+
* These are called directly by the main agent LLM, no SourceAgent.
|
|
2190
|
+
*/
|
|
2191
|
+
private buildDirectToolDefinitions;
|
|
2192
|
+
/**
|
|
2193
|
+
* Capture a workflow selection. We do NOT execute anything — the LLM has
|
|
2194
|
+
* already extracted the props it wants the workflow rendered with. We
|
|
2195
|
+
* record the selection (via the capture callback) and return a short
|
|
2196
|
+
* acknowledgement so the LLM ends its turn cleanly without writing
|
|
2197
|
+
* analysis text or calling more tools.
|
|
2198
|
+
*/
|
|
2199
|
+
private handleWorkflow;
|
|
2200
|
+
/**
|
|
2201
|
+
* Build LLM tool definitions for workflow components. The workflow's
|
|
2202
|
+
* propsSchema becomes the tool's input_schema so the LLM extracts props
|
|
2203
|
+
* directly from the prompt — same mechanic as direct tools.
|
|
2204
|
+
*/
|
|
2205
|
+
private buildWorkflowToolDefinitions;
|
|
2206
|
+
/**
|
|
2207
|
+
* Format a source agent's result as a clean string for the main agent LLM.
|
|
2208
|
+
*/
|
|
2209
|
+
private formatResultForMainAgent;
|
|
2210
|
+
/**
|
|
2211
|
+
* Get source summaries (for external inspection/debugging).
|
|
2212
|
+
*/
|
|
2213
|
+
getSourceSummaries(): SourceSummary[];
|
|
938
2214
|
}
|
|
939
2215
|
|
|
940
2216
|
/**
|
|
@@ -947,6 +2223,184 @@ interface Action {
|
|
|
947
2223
|
[key: string]: any;
|
|
948
2224
|
}
|
|
949
2225
|
|
|
2226
|
+
type SystemPrompt = string | Anthropic.Messages.TextBlockParam[];
|
|
2227
|
+
interface LLMMessages {
|
|
2228
|
+
sys: SystemPrompt;
|
|
2229
|
+
user: string;
|
|
2230
|
+
prefill?: string;
|
|
2231
|
+
}
|
|
2232
|
+
interface LLMOptions {
|
|
2233
|
+
model?: string;
|
|
2234
|
+
maxTokens?: number;
|
|
2235
|
+
temperature?: number;
|
|
2236
|
+
topP?: number;
|
|
2237
|
+
apiKey?: string;
|
|
2238
|
+
partial?: (chunk: string) => void;
|
|
2239
|
+
/**
|
|
2240
|
+
* Forces a tool call on the FIRST iteration of streamWithTools only
|
|
2241
|
+
* (subsequent iterations revert to auto). Used by fork mode to stop the
|
|
2242
|
+
* model from ending its turn with a bare "I'll adapt the script…" preamble
|
|
2243
|
+
* and zero tool calls. `{ type: 'any' }` lets the model pick which tool
|
|
2244
|
+
* (write_script in the common case, a source tool for schema discovery);
|
|
2245
|
+
* `{ type: 'tool', name }` pins a specific tool. Anthropic only.
|
|
2246
|
+
*/
|
|
2247
|
+
firstIterationToolChoice?: {
|
|
2248
|
+
type: 'any';
|
|
2249
|
+
} | {
|
|
2250
|
+
type: 'tool';
|
|
2251
|
+
name: string;
|
|
2252
|
+
};
|
|
2253
|
+
}
|
|
2254
|
+
interface Tool {
|
|
2255
|
+
name: string;
|
|
2256
|
+
description: string;
|
|
2257
|
+
input_schema: {
|
|
2258
|
+
type: string;
|
|
2259
|
+
properties: Record<string, any>;
|
|
2260
|
+
required?: string[];
|
|
2261
|
+
};
|
|
2262
|
+
}
|
|
2263
|
+
declare class LLM {
|
|
2264
|
+
static text(messages: LLMMessages, options?: LLMOptions): Promise<string>;
|
|
2265
|
+
static stream<T = string>(messages: LLMMessages, options?: LLMOptions, json?: boolean): Promise<T extends string ? string : any>;
|
|
2266
|
+
static streamWithTools(messages: LLMMessages, tools: Tool[], toolHandler: (toolName: string, toolInput: any) => Promise<any>, options?: LLMOptions, maxIterations?: number): Promise<string>;
|
|
2267
|
+
/**
|
|
2268
|
+
* Normalize system prompt to Anthropic format
|
|
2269
|
+
* Converts string to array format if needed
|
|
2270
|
+
* @param sys - System prompt (string or array of blocks)
|
|
2271
|
+
* @returns Normalized system prompt for Anthropic API
|
|
2272
|
+
*/
|
|
2273
|
+
private static _normalizeSystemPrompt;
|
|
2274
|
+
/**
|
|
2275
|
+
* Strip unpaired UTF-16 surrogates from every text field of a message set.
|
|
2276
|
+
*
|
|
2277
|
+
* A lone surrogate (from mid-pair string slicing or corrupt source data)
|
|
2278
|
+
* serializes to a bare `\udXXX` escape that strict JSON parsers — including
|
|
2279
|
+
* the one on Anthropic's API — reject with "no low surrogate in string",
|
|
2280
|
+
* failing the whole request. Sanitizing here, at the single boundary every
|
|
2281
|
+
* provider call flows through, guarantees no request can carry one.
|
|
2282
|
+
*/
|
|
2283
|
+
private static _sanitizeMessages;
|
|
2284
|
+
/**
|
|
2285
|
+
* Log cache usage metrics from Anthropic API response
|
|
2286
|
+
* Shows cache hits, costs, and savings
|
|
2287
|
+
*/
|
|
2288
|
+
private static _logCacheUsage;
|
|
2289
|
+
/**
|
|
2290
|
+
* Parse model string to extract provider and model name
|
|
2291
|
+
* @param modelString - Format: "provider/model-name" or just "model-name"
|
|
2292
|
+
* @returns [provider, modelName]
|
|
2293
|
+
*
|
|
2294
|
+
* @example
|
|
2295
|
+
* "anthropic/claude-sonnet-4-5" → ["anthropic", "claude-sonnet-4-5"]
|
|
2296
|
+
* "groq/openai/gpt-oss-120b" → ["groq", "openai/gpt-oss-120b"]
|
|
2297
|
+
* "claude-sonnet-4-5" → ["anthropic", "claude-sonnet-4-5"] (default)
|
|
2298
|
+
*/
|
|
2299
|
+
private static _parseModel;
|
|
2300
|
+
private static _anthropicText;
|
|
2301
|
+
private static _anthropicStream;
|
|
2302
|
+
private static _anthropicStreamWithTools;
|
|
2303
|
+
private static _groqText;
|
|
2304
|
+
private static _groqStream;
|
|
2305
|
+
private static _geminiText;
|
|
2306
|
+
private static _geminiStream;
|
|
2307
|
+
/**
|
|
2308
|
+
* Recursively strip unsupported JSON Schema properties for Gemini
|
|
2309
|
+
* Gemini doesn't support: additionalProperties, $schema, etc.
|
|
2310
|
+
*/
|
|
2311
|
+
private static _cleanSchemaForGemini;
|
|
2312
|
+
private static _geminiStreamWithTools;
|
|
2313
|
+
private static _openaiText;
|
|
2314
|
+
private static _openaiStream;
|
|
2315
|
+
private static _openaiStreamWithTools;
|
|
2316
|
+
/**
|
|
2317
|
+
* Parse JSON string, handling markdown code blocks and surrounding text
|
|
2318
|
+
* Enhanced version with jsonrepair to handle malformed JSON from LLMs
|
|
2319
|
+
* @param text - Text that may contain JSON wrapped in ```json...``` or with surrounding text
|
|
2320
|
+
* @returns Parsed JSON object or array
|
|
2321
|
+
*/
|
|
2322
|
+
private static _parseJSON;
|
|
2323
|
+
}
|
|
2324
|
+
|
|
2325
|
+
interface CapturedLog {
|
|
2326
|
+
timestamp: number;
|
|
2327
|
+
level: 'info' | 'error' | 'warn' | 'debug';
|
|
2328
|
+
message: string;
|
|
2329
|
+
type?: 'explanation' | 'query' | 'general';
|
|
2330
|
+
data?: Record<string, any>;
|
|
2331
|
+
}
|
|
2332
|
+
/**
|
|
2333
|
+
* UILogCollector captures logs during user prompt processing
|
|
2334
|
+
* and sends them to runtime via ui_logs message with uiBlockId as the message id
|
|
2335
|
+
* Logs are sent in real-time for streaming effect in the UI
|
|
2336
|
+
* Respects the global log level configuration
|
|
2337
|
+
*/
|
|
2338
|
+
declare class UILogCollector {
|
|
2339
|
+
private logs;
|
|
2340
|
+
private uiBlockId;
|
|
2341
|
+
private clientId;
|
|
2342
|
+
private sendMessage;
|
|
2343
|
+
private currentLogLevel;
|
|
2344
|
+
constructor(clientId: string, sendMessage: (message: Message) => void, uiBlockId?: string);
|
|
2345
|
+
/**
|
|
2346
|
+
* Check if logging is enabled (uiBlockId is provided)
|
|
2347
|
+
*/
|
|
2348
|
+
isEnabled(): boolean;
|
|
2349
|
+
/**
|
|
2350
|
+
* Check if a message should be logged based on current log level
|
|
2351
|
+
*/
|
|
2352
|
+
private shouldLog;
|
|
2353
|
+
/**
|
|
2354
|
+
* Add a log entry with timestamp and immediately send to runtime
|
|
2355
|
+
* Only logs that pass the log level filter are captured and sent
|
|
2356
|
+
*/
|
|
2357
|
+
private addLog;
|
|
2358
|
+
/**
|
|
2359
|
+
* Send a single log to runtime immediately
|
|
2360
|
+
*/
|
|
2361
|
+
private sendLogImmediately;
|
|
2362
|
+
/**
|
|
2363
|
+
* Log info message
|
|
2364
|
+
*/
|
|
2365
|
+
info(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
|
|
2366
|
+
/**
|
|
2367
|
+
* Log error message
|
|
2368
|
+
*/
|
|
2369
|
+
error(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
|
|
2370
|
+
/**
|
|
2371
|
+
* Log warning message
|
|
2372
|
+
*/
|
|
2373
|
+
warn(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
|
|
2374
|
+
/**
|
|
2375
|
+
* Log debug message
|
|
2376
|
+
*/
|
|
2377
|
+
debug(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
|
|
2378
|
+
/**
|
|
2379
|
+
* Log LLM explanation with typed metadata
|
|
2380
|
+
*/
|
|
2381
|
+
logExplanation(message: string, explanation: string, data?: Record<string, any>): void;
|
|
2382
|
+
/**
|
|
2383
|
+
* Log generated query with typed metadata
|
|
2384
|
+
*/
|
|
2385
|
+
logQuery(message: string, query: string, data?: Record<string, any>): void;
|
|
2386
|
+
/**
|
|
2387
|
+
* Send all collected logs at once (optional, for final summary)
|
|
2388
|
+
*/
|
|
2389
|
+
sendAllLogs(): void;
|
|
2390
|
+
/**
|
|
2391
|
+
* Get all collected logs
|
|
2392
|
+
*/
|
|
2393
|
+
getLogs(): CapturedLog[];
|
|
2394
|
+
/**
|
|
2395
|
+
* Clear all logs
|
|
2396
|
+
*/
|
|
2397
|
+
clearLogs(): void;
|
|
2398
|
+
/**
|
|
2399
|
+
* Set uiBlockId (in case it's provided later)
|
|
2400
|
+
*/
|
|
2401
|
+
setUIBlockId(uiBlockId: string): void;
|
|
2402
|
+
}
|
|
2403
|
+
|
|
950
2404
|
/**
|
|
951
2405
|
* UIBlock represents a single user and assistant message block in a thread
|
|
952
2406
|
* Contains user question, component metadata, component data, text response, and available actions
|
|
@@ -985,6 +2439,7 @@ declare class UIBlock {
|
|
|
985
2439
|
* Get component metadata
|
|
986
2440
|
*/
|
|
987
2441
|
getComponentMetadata(): Record<string, any>;
|
|
2442
|
+
getTextResponse(): string;
|
|
988
2443
|
/**
|
|
989
2444
|
* Set or update component metadata
|
|
990
2445
|
*/
|
|
@@ -1013,10 +2468,6 @@ declare class UIBlock {
|
|
|
1013
2468
|
* Set or update component data with size and row limits
|
|
1014
2469
|
*/
|
|
1015
2470
|
setComponentData(data: Record<string, any>): void;
|
|
1016
|
-
/**
|
|
1017
|
-
* Get text response
|
|
1018
|
-
*/
|
|
1019
|
-
getTextResponse(): string | null;
|
|
1020
2471
|
/**
|
|
1021
2472
|
* Set or update text response
|
|
1022
2473
|
*/
|
|
@@ -1033,6 +2484,10 @@ declare class UIBlock {
|
|
|
1033
2484
|
* @returns Promise resolving to Action[]
|
|
1034
2485
|
*/
|
|
1035
2486
|
getOrFetchActions(generateFn: () => Promise<Action[]>): Promise<Action[]>;
|
|
2487
|
+
/**
|
|
2488
|
+
* Set or replace all actions
|
|
2489
|
+
*/
|
|
2490
|
+
setActions(actions: Action[]): void;
|
|
1036
2491
|
/**
|
|
1037
2492
|
* Add a single action (only if actions are resolved)
|
|
1038
2493
|
*/
|
|
@@ -1128,12 +2583,20 @@ declare class Thread {
|
|
|
1128
2583
|
|
|
1129
2584
|
/**
|
|
1130
2585
|
* ThreadManager manages all threads globally
|
|
1131
|
-
* Provides methods to create, retrieve, and delete threads
|
|
2586
|
+
* Provides methods to create, retrieve, and delete threads.
|
|
2587
|
+
* Includes automatic cleanup to prevent unbounded memory growth.
|
|
1132
2588
|
*/
|
|
1133
2589
|
declare class ThreadManager {
|
|
1134
2590
|
private static instance;
|
|
1135
2591
|
private threads;
|
|
2592
|
+
private cleanupInterval;
|
|
2593
|
+
private readonly threadTtlMs;
|
|
1136
2594
|
private constructor();
|
|
2595
|
+
/**
|
|
2596
|
+
* Periodically remove threads older than 7 days.
|
|
2597
|
+
* Runs every hour to avoid frequent iteration over the map.
|
|
2598
|
+
*/
|
|
2599
|
+
private startCleanup;
|
|
1137
2600
|
/**
|
|
1138
2601
|
* Get singleton instance of ThreadManager
|
|
1139
2602
|
*/
|
|
@@ -1263,15 +2726,17 @@ declare const STORAGE_CONFIG: {
|
|
|
1263
2726
|
*/
|
|
1264
2727
|
MAX_ROWS_PER_BLOCK: number;
|
|
1265
2728
|
/**
|
|
1266
|
-
* Maximum size in bytes per UIBlock (
|
|
2729
|
+
* Maximum size in bytes per UIBlock (500KB - reduced to save memory)
|
|
1267
2730
|
*/
|
|
1268
2731
|
MAX_SIZE_PER_BLOCK_BYTES: number;
|
|
1269
2732
|
/**
|
|
1270
2733
|
* Number of days to keep threads before cleanup
|
|
2734
|
+
* Note: This is for in-memory storage. Conversations are also persisted to database.
|
|
1271
2735
|
*/
|
|
1272
2736
|
THREAD_RETENTION_DAYS: number;
|
|
1273
2737
|
/**
|
|
1274
2738
|
* Number of days to keep UIBlocks before cleanup
|
|
2739
|
+
* Note: This is for in-memory storage. Data is also persisted to database.
|
|
1275
2740
|
*/
|
|
1276
2741
|
UIBLOCK_RETENTION_DAYS: number;
|
|
1277
2742
|
};
|
|
@@ -1288,14 +2753,760 @@ declare const CONTEXT_CONFIG: {
|
|
|
1288
2753
|
MAX_CONVERSATION_CONTEXT_BLOCKS: number;
|
|
1289
2754
|
};
|
|
1290
2755
|
|
|
1291
|
-
|
|
2756
|
+
/**
|
|
2757
|
+
* LLM Usage Logger - Tracks token usage, costs, and timing for all LLM API calls
|
|
2758
|
+
*/
|
|
2759
|
+
interface LLMUsageEntry {
|
|
2760
|
+
timestamp: string;
|
|
2761
|
+
requestId: string;
|
|
2762
|
+
provider: string;
|
|
2763
|
+
model: string;
|
|
2764
|
+
method: string;
|
|
2765
|
+
inputTokens: number;
|
|
2766
|
+
outputTokens: number;
|
|
2767
|
+
cacheReadTokens?: number;
|
|
2768
|
+
cacheWriteTokens?: number;
|
|
2769
|
+
totalTokens: number;
|
|
2770
|
+
costUSD: number;
|
|
2771
|
+
durationMs: number;
|
|
2772
|
+
toolCalls?: number;
|
|
2773
|
+
success: boolean;
|
|
2774
|
+
error?: string;
|
|
2775
|
+
}
|
|
2776
|
+
declare class LLMUsageLogger {
|
|
2777
|
+
private logStream;
|
|
2778
|
+
private logPath;
|
|
2779
|
+
private enabled;
|
|
2780
|
+
private sessionStats;
|
|
2781
|
+
constructor();
|
|
2782
|
+
private initLogStream;
|
|
2783
|
+
private writeHeader;
|
|
2784
|
+
/**
|
|
2785
|
+
* Calculate cost based on token usage and model
|
|
2786
|
+
*/
|
|
2787
|
+
calculateCost(model: string, inputTokens: number, outputTokens: number, cacheReadTokens?: number, cacheWriteTokens?: number): number;
|
|
2788
|
+
/**
|
|
2789
|
+
* Log an LLM API call
|
|
2790
|
+
*/
|
|
2791
|
+
log(entry: LLMUsageEntry): void;
|
|
2792
|
+
/**
|
|
2793
|
+
* Log session summary (call at end of request)
|
|
2794
|
+
*/
|
|
2795
|
+
logSessionSummary(requestContext?: string): void;
|
|
2796
|
+
/**
|
|
2797
|
+
* Reset session stats (call at start of new user request)
|
|
2798
|
+
*/
|
|
2799
|
+
resetSession(): void;
|
|
2800
|
+
/**
|
|
2801
|
+
* Reset the log file for a new request (clears previous logs)
|
|
2802
|
+
* Call this at the start of each USER_PROMPT_REQ
|
|
2803
|
+
*/
|
|
2804
|
+
resetLogFile(requestContext?: string): void;
|
|
2805
|
+
/**
|
|
2806
|
+
* Get current session stats
|
|
2807
|
+
*/
|
|
2808
|
+
getSessionStats(): {
|
|
2809
|
+
totalCalls: number;
|
|
2810
|
+
totalInputTokens: number;
|
|
2811
|
+
totalOutputTokens: number;
|
|
2812
|
+
totalCacheReadTokens: number;
|
|
2813
|
+
totalCacheWriteTokens: number;
|
|
2814
|
+
totalCostUSD: number;
|
|
2815
|
+
totalDurationMs: number;
|
|
2816
|
+
};
|
|
2817
|
+
/**
|
|
2818
|
+
* Generate a unique request ID
|
|
2819
|
+
*/
|
|
2820
|
+
generateRequestId(): string;
|
|
2821
|
+
}
|
|
2822
|
+
declare const llmUsageLogger: LLMUsageLogger;
|
|
2823
|
+
|
|
2824
|
+
/**
|
|
2825
|
+
* User Prompt Error Logger - Captures detailed errors for USER_PROMPT_REQ
|
|
2826
|
+
* Logs full error details including raw strings for parse failures
|
|
2827
|
+
*/
|
|
2828
|
+
declare class UserPromptErrorLogger {
|
|
2829
|
+
private logStream;
|
|
2830
|
+
private logPath;
|
|
2831
|
+
private enabled;
|
|
2832
|
+
private hasErrors;
|
|
2833
|
+
constructor();
|
|
2834
|
+
/**
|
|
2835
|
+
* Reset the error log file for a new request
|
|
2836
|
+
*/
|
|
2837
|
+
resetLogFile(requestContext?: string): void;
|
|
2838
|
+
/**
|
|
2839
|
+
* Log a JSON parse error with the raw string that failed
|
|
2840
|
+
*/
|
|
2841
|
+
logJsonParseError(context: string, rawString: string, error: Error): void;
|
|
2842
|
+
/**
|
|
2843
|
+
* Log a general error with full details
|
|
2844
|
+
*/
|
|
2845
|
+
logError(context: string, error: Error | string, additionalData?: Record<string, any>): void;
|
|
2846
|
+
/**
|
|
2847
|
+
* Log a SQL query error with the full query
|
|
2848
|
+
*/
|
|
2849
|
+
logSqlError(query: string, error: Error | string, params?: any[]): void;
|
|
2850
|
+
/**
|
|
2851
|
+
* Log an LLM API error
|
|
2852
|
+
*/
|
|
2853
|
+
logLlmError(provider: string, model: string, method: string, error: Error | string, requestData?: any): void;
|
|
2854
|
+
/**
|
|
2855
|
+
* Log tool execution error
|
|
2856
|
+
*/
|
|
2857
|
+
logToolError(toolName: string, toolInput: any, error: Error | string): void;
|
|
2858
|
+
/**
|
|
2859
|
+
* Write final summary if there were errors
|
|
2860
|
+
*/
|
|
2861
|
+
writeSummary(): void;
|
|
2862
|
+
/**
|
|
2863
|
+
* Check if any errors were logged
|
|
2864
|
+
*/
|
|
2865
|
+
hadErrors(): boolean;
|
|
2866
|
+
private write;
|
|
2867
|
+
}
|
|
2868
|
+
declare const userPromptErrorLogger: UserPromptErrorLogger;
|
|
2869
|
+
|
|
2870
|
+
/**
|
|
2871
|
+
* BM25L Reranker for hybrid semantic search
|
|
2872
|
+
*
|
|
2873
|
+
* BM25L is an improved variant of BM25 that provides better handling of
|
|
2874
|
+
* long documents and term frequency saturation. This implementation is
|
|
2875
|
+
* designed to rerank semantic search results from ChromaDB.
|
|
2876
|
+
*
|
|
2877
|
+
* The hybrid approach combines:
|
|
2878
|
+
* 1. Semantic similarity from ChromaDB embeddings (dense vectors)
|
|
2879
|
+
* 2. Lexical matching from BM25L (sparse, keyword-based)
|
|
2880
|
+
*
|
|
2881
|
+
* This addresses the weakness of pure semantic search which may miss
|
|
2882
|
+
* exact keyword matches that are important for user intent.
|
|
2883
|
+
*/
|
|
2884
|
+
interface BM25LOptions {
|
|
2885
|
+
/** Term frequency saturation parameter (default: 1.5) */
|
|
2886
|
+
k1?: number;
|
|
2887
|
+
/** Length normalization parameter (default: 0.75) */
|
|
2888
|
+
b?: number;
|
|
2889
|
+
/** Lower-bound adjustment from BM25L paper (default: 0.5) */
|
|
2890
|
+
delta?: number;
|
|
2891
|
+
}
|
|
2892
|
+
interface RerankedResult<T> {
|
|
2893
|
+
item: T;
|
|
2894
|
+
originalIndex: number;
|
|
2895
|
+
semanticScore: number;
|
|
2896
|
+
bm25Score: number;
|
|
2897
|
+
hybridScore: number;
|
|
2898
|
+
}
|
|
2899
|
+
interface HybridSearchOptions extends BM25LOptions {
|
|
2900
|
+
/** Weight for semantic score (0-1, default: 0.7) */
|
|
2901
|
+
semanticWeight?: number;
|
|
2902
|
+
/** Weight for BM25 score (0-1, default: 0.3) */
|
|
2903
|
+
bm25Weight?: number;
|
|
2904
|
+
/** Minimum hybrid score threshold (0-1, default: 0) */
|
|
2905
|
+
minScore?: number;
|
|
2906
|
+
}
|
|
2907
|
+
/**
|
|
2908
|
+
* BM25L implementation for lexical scoring
|
|
2909
|
+
*/
|
|
2910
|
+
declare class BM25L {
|
|
2911
|
+
private k1;
|
|
2912
|
+
private b;
|
|
2913
|
+
private delta;
|
|
2914
|
+
private documents;
|
|
2915
|
+
private docLengths;
|
|
2916
|
+
private avgDocLength;
|
|
2917
|
+
private termDocFreq;
|
|
2918
|
+
/**
|
|
2919
|
+
* @param documents - Array of raw documents (strings)
|
|
2920
|
+
* @param opts - Optional BM25L parameters
|
|
2921
|
+
*/
|
|
2922
|
+
constructor(documents?: string[], opts?: BM25LOptions);
|
|
2923
|
+
/**
|
|
2924
|
+
* Tokenize text into lowercase alphanumeric tokens
|
|
2925
|
+
*/
|
|
2926
|
+
tokenize(text: string): string[];
|
|
2927
|
+
/**
|
|
2928
|
+
* Compute IDF (Inverse Document Frequency) with smoothing
|
|
2929
|
+
*/
|
|
2930
|
+
private idf;
|
|
2931
|
+
/**
|
|
2932
|
+
* Compute BM25L score for a single document
|
|
2933
|
+
*/
|
|
2934
|
+
score(query: string, docIndex: number): number;
|
|
2935
|
+
/**
|
|
2936
|
+
* Search and rank all documents
|
|
2937
|
+
*/
|
|
2938
|
+
search(query: string): Array<{
|
|
2939
|
+
index: number;
|
|
2940
|
+
score: number;
|
|
2941
|
+
}>;
|
|
2942
|
+
}
|
|
2943
|
+
/**
|
|
2944
|
+
* Hybrid reranker that combines semantic and BM25L scores
|
|
2945
|
+
*
|
|
2946
|
+
* @param query - The search query
|
|
2947
|
+
* @param items - Array of items to rerank
|
|
2948
|
+
* @param getDocument - Function to extract document text from an item
|
|
2949
|
+
* @param getSemanticScore - Function to extract semantic similarity score from an item
|
|
2950
|
+
* @param options - Hybrid search options
|
|
2951
|
+
* @returns Reranked items with hybrid scores
|
|
2952
|
+
*/
|
|
2953
|
+
declare function hybridRerank<T>(query: string, items: T[], getDocument: (item: T) => string, getSemanticScore: (item: T) => number, options?: HybridSearchOptions): RerankedResult<T>[];
|
|
2954
|
+
/**
|
|
2955
|
+
* Simple reranking function for ChromaDB results
|
|
2956
|
+
*
|
|
2957
|
+
* This is a convenience wrapper for reranking ChromaDB query results
|
|
2958
|
+
* that follow the standard { ids, documents, metadatas, distances } format.
|
|
2959
|
+
*
|
|
2960
|
+
* @param query - The search query
|
|
2961
|
+
* @param chromaResults - ChromaDB query results
|
|
2962
|
+
* @param options - Hybrid search options
|
|
2963
|
+
* @returns Reranked results with hybrid scores
|
|
2964
|
+
*/
|
|
2965
|
+
declare function rerankChromaResults(query: string, chromaResults: {
|
|
2966
|
+
ids: string[][];
|
|
2967
|
+
documents: (string | null)[][];
|
|
2968
|
+
metadatas: Record<string, any>[][];
|
|
2969
|
+
distances: number[][];
|
|
2970
|
+
}, options?: HybridSearchOptions): Array<{
|
|
2971
|
+
id: string;
|
|
2972
|
+
document: string | null;
|
|
2973
|
+
metadata: Record<string, any>;
|
|
2974
|
+
distance: number;
|
|
2975
|
+
semanticScore: number;
|
|
2976
|
+
bm25Score: number;
|
|
2977
|
+
hybridScore: number;
|
|
2978
|
+
}>;
|
|
2979
|
+
/**
|
|
2980
|
+
* Rerank conversation search results specifically
|
|
2981
|
+
*
|
|
2982
|
+
* This function is designed to work with the conversation-history.search collection
|
|
2983
|
+
* where we need to fetch more results initially and then rerank them.
|
|
2984
|
+
*
|
|
2985
|
+
* @param query - The user's search query
|
|
2986
|
+
* @param results - Array of conversation search results from ChromaDB
|
|
2987
|
+
* @param options - Hybrid search options
|
|
2988
|
+
* @returns Reranked results sorted by hybrid score
|
|
2989
|
+
*/
|
|
2990
|
+
declare function rerankConversationResults<T extends {
|
|
2991
|
+
userPrompt?: string;
|
|
2992
|
+
similarity?: number;
|
|
2993
|
+
}>(query: string, results: T[], options?: HybridSearchOptions): Array<T & {
|
|
2994
|
+
hybridScore: number;
|
|
2995
|
+
bm25Score: number;
|
|
2996
|
+
}>;
|
|
2997
|
+
|
|
2998
|
+
/**
|
|
2999
|
+
* QueryExecutionService - Handles all query execution, validation, and retry logic
|
|
3000
|
+
* Extracted from BaseLLM for better separation of concerns
|
|
3001
|
+
*/
|
|
3002
|
+
|
|
3003
|
+
/**
|
|
3004
|
+
* Context for component when requesting query fix
|
|
3005
|
+
*/
|
|
3006
|
+
interface ComponentContext {
|
|
3007
|
+
name: string;
|
|
3008
|
+
type: string;
|
|
3009
|
+
title?: string;
|
|
3010
|
+
}
|
|
3011
|
+
/**
|
|
3012
|
+
* Result of query validation
|
|
3013
|
+
*/
|
|
3014
|
+
interface QueryValidationResult {
|
|
3015
|
+
component: Component | null;
|
|
3016
|
+
queryKey: string;
|
|
3017
|
+
result: any;
|
|
3018
|
+
validated: boolean;
|
|
3019
|
+
}
|
|
3020
|
+
/**
|
|
3021
|
+
* Result of batch query validation
|
|
3022
|
+
*/
|
|
3023
|
+
interface BatchValidationResult {
|
|
3024
|
+
components: Component[];
|
|
3025
|
+
queryResults: Map<string, any>;
|
|
3026
|
+
}
|
|
3027
|
+
/**
|
|
3028
|
+
* Configuration for QueryExecutionService
|
|
3029
|
+
*/
|
|
3030
|
+
interface QueryExecutionServiceConfig {
|
|
3031
|
+
defaultLimit: number;
|
|
3032
|
+
getModelForTask: (taskType: 'simple' | 'complex') => string;
|
|
3033
|
+
getApiKey: (apiKey?: string) => string | undefined;
|
|
3034
|
+
providerName: string;
|
|
3035
|
+
}
|
|
3036
|
+
/**
|
|
3037
|
+
* QueryExecutionService handles all query-related operations
|
|
3038
|
+
*/
|
|
3039
|
+
declare class QueryExecutionService {
|
|
3040
|
+
private config;
|
|
3041
|
+
constructor(config: QueryExecutionServiceConfig);
|
|
3042
|
+
/**
|
|
3043
|
+
* Get the cache key for a query
|
|
3044
|
+
* This ensures the cache key matches what the frontend will send
|
|
3045
|
+
*/
|
|
3046
|
+
getQueryCacheKey(query: any): string;
|
|
3047
|
+
/**
|
|
3048
|
+
* Execute a query against the database
|
|
3049
|
+
* @param query - The SQL query to execute (string or object with sql/values)
|
|
3050
|
+
* @param collections - Collections object containing database execute function
|
|
3051
|
+
* @returns Object with result data and cache key
|
|
3052
|
+
*/
|
|
3053
|
+
executeQuery(query: any, collections: any): Promise<{
|
|
3054
|
+
result: any;
|
|
3055
|
+
cacheKey: string;
|
|
3056
|
+
}>;
|
|
3057
|
+
/**
|
|
3058
|
+
* Request the LLM to fix a failed SQL query
|
|
3059
|
+
* @param failedQuery - The query that failed execution
|
|
3060
|
+
* @param errorMessage - The error message from the failed execution
|
|
3061
|
+
* @param componentContext - Context about the component
|
|
3062
|
+
* @param apiKey - Optional API key
|
|
3063
|
+
* @returns Fixed query string
|
|
3064
|
+
*/
|
|
3065
|
+
requestQueryFix(failedQuery: string, errorMessage: string, componentContext: ComponentContext, apiKey?: string): Promise<string>;
|
|
3066
|
+
/**
|
|
3067
|
+
* Validate a single component's query with retry logic
|
|
3068
|
+
* @param component - The component to validate
|
|
3069
|
+
* @param collections - Collections object containing database execute function
|
|
3070
|
+
* @param apiKey - Optional API key for LLM calls
|
|
3071
|
+
* @returns Validation result with component, query key, and result
|
|
3072
|
+
*/
|
|
3073
|
+
validateSingleQuery(component: Component, collections: any, apiKey?: string): Promise<QueryValidationResult>;
|
|
3074
|
+
/**
|
|
3075
|
+
* Validate multiple component queries in parallel
|
|
3076
|
+
* @param components - Array of components with potential queries
|
|
3077
|
+
* @param collections - Collections object containing database execute function
|
|
3078
|
+
* @param apiKey - Optional API key for LLM calls
|
|
3079
|
+
* @returns Object with validated components and query results map
|
|
3080
|
+
*/
|
|
3081
|
+
validateComponentQueries(components: Component[], collections: any, apiKey?: string): Promise<BatchValidationResult>;
|
|
3082
|
+
}
|
|
3083
|
+
|
|
3084
|
+
/**
|
|
3085
|
+
* Task types for model selection
|
|
3086
|
+
* - 'complex': Text generation, component matching, parameter adaptation (uses best model in balanced mode)
|
|
3087
|
+
* - 'simple': Classification, action generation (uses fast model in balanced mode)
|
|
3088
|
+
*/
|
|
3089
|
+
type TaskType = 'complex' | 'simple';
|
|
3090
|
+
interface BaseLLMConfig {
|
|
3091
|
+
model?: string;
|
|
3092
|
+
fastModel?: string;
|
|
3093
|
+
defaultLimit?: number;
|
|
3094
|
+
apiKey?: string;
|
|
3095
|
+
/**
|
|
3096
|
+
* Model selection strategy:
|
|
3097
|
+
* - 'best': Use best model for all tasks (highest quality, higher cost)
|
|
3098
|
+
* - 'fast': Use fast model for all tasks (lower quality, lower cost)
|
|
3099
|
+
* - 'balanced': Use best model for complex tasks, fast model for simple tasks (default)
|
|
3100
|
+
*/
|
|
3101
|
+
modelStrategy?: ModelStrategy;
|
|
3102
|
+
conversationSimilarityThreshold?: number;
|
|
3103
|
+
}
|
|
3104
|
+
/**
|
|
3105
|
+
* BaseLLM abstract class for AI-powered component generation and matching
|
|
3106
|
+
* Provides common functionality for all LLM providers
|
|
3107
|
+
*/
|
|
3108
|
+
declare abstract class BaseLLM {
|
|
3109
|
+
protected model: string;
|
|
3110
|
+
protected fastModel: string;
|
|
3111
|
+
protected defaultLimit: number;
|
|
3112
|
+
protected apiKey?: string;
|
|
3113
|
+
protected modelStrategy: ModelStrategy;
|
|
3114
|
+
protected conversationSimilarityThreshold: number;
|
|
3115
|
+
protected queryService: QueryExecutionService;
|
|
3116
|
+
constructor(config?: BaseLLMConfig);
|
|
3117
|
+
/**
|
|
3118
|
+
* Get the appropriate model based on task type and model strategy
|
|
3119
|
+
* @param taskType - 'complex' for text generation/matching, 'simple' for classification/actions
|
|
3120
|
+
* @returns The model string to use for this task
|
|
3121
|
+
*/
|
|
3122
|
+
protected getModelForTask(taskType: TaskType): string;
|
|
3123
|
+
/**
|
|
3124
|
+
* Set the model strategy at runtime
|
|
3125
|
+
* @param strategy - 'best', 'fast', or 'balanced'
|
|
3126
|
+
*/
|
|
3127
|
+
setModelStrategy(strategy: ModelStrategy): void;
|
|
3128
|
+
/**
|
|
3129
|
+
* Get the current model strategy
|
|
3130
|
+
* @returns The current model strategy
|
|
3131
|
+
*/
|
|
3132
|
+
getModelStrategy(): ModelStrategy;
|
|
3133
|
+
/**
|
|
3134
|
+
* Set the conversation similarity threshold at runtime
|
|
3135
|
+
* @param threshold - Value between 0 and 1 (e.g., 0.8 = 80% similarity required)
|
|
3136
|
+
*/
|
|
3137
|
+
setConversationSimilarityThreshold(threshold: number): void;
|
|
3138
|
+
/**
|
|
3139
|
+
* Get the current conversation similarity threshold
|
|
3140
|
+
* @returns The current threshold value
|
|
3141
|
+
*/
|
|
3142
|
+
getConversationSimilarityThreshold(): number;
|
|
3143
|
+
/**
|
|
3144
|
+
* Get the default model for this provider (used for complex tasks like text generation)
|
|
3145
|
+
*/
|
|
3146
|
+
protected abstract getDefaultModel(): string;
|
|
3147
|
+
/**
|
|
3148
|
+
* Get the default fast model for this provider (used for simple tasks: classification, matching, actions)
|
|
3149
|
+
* Should return a cheaper/faster model like Haiku for Anthropic
|
|
3150
|
+
*/
|
|
3151
|
+
protected abstract getDefaultFastModel(): string;
|
|
3152
|
+
/**
|
|
3153
|
+
* Get the default API key from environment
|
|
3154
|
+
*/
|
|
3155
|
+
protected abstract getDefaultApiKey(): string | undefined;
|
|
3156
|
+
/**
|
|
3157
|
+
* Get the provider name (for logging)
|
|
3158
|
+
*/
|
|
3159
|
+
protected abstract getProviderName(): string;
|
|
3160
|
+
/**
|
|
3161
|
+
* Get the API key (from instance, parameter, or environment)
|
|
3162
|
+
*/
|
|
3163
|
+
protected getApiKey(apiKey?: string): string | undefined;
|
|
3164
|
+
/**
|
|
3165
|
+
* Check if a component contains a Form (data_modification component)
|
|
3166
|
+
* Forms have hardcoded defaultValues that become stale when cached
|
|
3167
|
+
* This checks both single Form components and Forms inside MultiComponentContainer
|
|
3168
|
+
*/
|
|
3169
|
+
protected containsFormComponent(component: any): boolean;
|
|
3170
|
+
/**
|
|
3171
|
+
* Match components from text response suggestions and generate follow-up questions
|
|
3172
|
+
* Takes a text response with component suggestions (c1:type format) and matches with available components
|
|
3173
|
+
* Also generates title, description, and intelligent follow-up questions (actions) based on the analysis
|
|
3174
|
+
* All components are placed in a default MultiComponentContainer layout
|
|
3175
|
+
* @param analysisContent - The text response containing component suggestions
|
|
3176
|
+
* @param components - List of available components
|
|
3177
|
+
* @param apiKey - Optional API key
|
|
3178
|
+
* @param componentStreamCallback - Optional callback to stream primary KPI component as soon as it's identified
|
|
3179
|
+
* @returns Object containing matched components, layout title/description, and follow-up actions
|
|
3180
|
+
*/
|
|
3181
|
+
matchComponentsFromAnalysis(analysisContent: string, components: Component[], userPrompt: string, apiKey?: string, componentStreamCallback?: (component: Component) => void, deferredTools?: any[], executedTools?: any[], collections?: any, userId?: string): Promise<{
|
|
3182
|
+
components: Component[];
|
|
3183
|
+
layoutTitle: string;
|
|
3184
|
+
layoutDescription: string;
|
|
3185
|
+
actions: Action[];
|
|
3186
|
+
}>;
|
|
3187
|
+
/**
|
|
3188
|
+
* Classify user question into category and detect external tools needed
|
|
3189
|
+
* Determines if question is for data analysis, requires external tools, or needs text response
|
|
3190
|
+
*/
|
|
3191
|
+
classifyQuestionCategory(userPrompt: string, apiKey?: string, conversationHistory?: string, externalTools?: any[]): Promise<{
|
|
3192
|
+
category: 'data_analysis' | 'data_modification' | 'general';
|
|
3193
|
+
externalTools: Array<{
|
|
3194
|
+
type: string;
|
|
3195
|
+
name: string;
|
|
3196
|
+
description: string;
|
|
3197
|
+
parameters: Record<string, any>;
|
|
3198
|
+
}>;
|
|
3199
|
+
dataAnalysisType?: 'visualization' | 'calculation' | 'comparison' | 'trend';
|
|
3200
|
+
reasoning: string;
|
|
3201
|
+
confidence: number;
|
|
3202
|
+
}>;
|
|
3203
|
+
/**
|
|
3204
|
+
* Adapt UI block parameters based on current user question
|
|
3205
|
+
* Takes a matched UI block from semantic search and modifies its props to answer the new question
|
|
3206
|
+
* Also adapts the cached text response to match the new question
|
|
3207
|
+
*/
|
|
3208
|
+
adaptUIBlockParameters(currentUserPrompt: string, originalUserPrompt: string, matchedUIBlock: any, apiKey?: string, cachedTextResponse?: string): Promise<{
|
|
3209
|
+
success: boolean;
|
|
3210
|
+
adaptedComponent?: Component;
|
|
3211
|
+
adaptedTextResponse?: string;
|
|
3212
|
+
parametersChanged?: Array<{
|
|
3213
|
+
field: string;
|
|
3214
|
+
reason: string;
|
|
3215
|
+
}>;
|
|
3216
|
+
explanation: string;
|
|
3217
|
+
}>;
|
|
3218
|
+
/**
|
|
3219
|
+
* Generate text-based response for user question
|
|
3220
|
+
* This provides conversational text responses instead of component generation
|
|
3221
|
+
* Supports tool calling for query execution with automatic retry on errors (max 3 attempts)
|
|
3222
|
+
* After generating text response, if components are provided, matches suggested components
|
|
3223
|
+
*/
|
|
3224
|
+
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>;
|
|
3225
|
+
/**
|
|
3226
|
+
* Main orchestration function with semantic search and multi-step classification
|
|
3227
|
+
* NEW FLOW (Recommended):
|
|
3228
|
+
* 1. Semantic search: Check previous conversations (>60% match)
|
|
3229
|
+
* - If match found → Adapt UI block parameters and return
|
|
3230
|
+
* 2. Category classification: Determine if data_analysis, requires_external_tools, or text_response
|
|
3231
|
+
* 3. Route appropriately based on category and response mode
|
|
3232
|
+
*/
|
|
3233
|
+
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>;
|
|
3234
|
+
/**
|
|
3235
|
+
* Generate next questions that the user might ask based on the original prompt and generated component
|
|
3236
|
+
* This helps provide intelligent suggestions for follow-up queries
|
|
3237
|
+
* For general/conversational questions without components, pass textResponse instead
|
|
3238
|
+
*/
|
|
3239
|
+
generateNextQuestions(originalUserPrompt: string, component?: Component | null, componentData?: Record<string, unknown>, apiKey?: string, conversationHistory?: string, textResponse?: string): Promise<string[]>;
|
|
3240
|
+
}
|
|
3241
|
+
|
|
3242
|
+
interface AnthropicLLMConfig extends BaseLLMConfig {
|
|
3243
|
+
}
|
|
3244
|
+
/**
|
|
3245
|
+
* AnthropicLLM class for handling AI-powered component generation and matching using Anthropic Claude
|
|
3246
|
+
*/
|
|
3247
|
+
declare class AnthropicLLM extends BaseLLM {
|
|
3248
|
+
constructor(config?: AnthropicLLMConfig);
|
|
3249
|
+
protected getDefaultModel(): string;
|
|
3250
|
+
protected getDefaultFastModel(): string;
|
|
3251
|
+
protected getDefaultApiKey(): string | undefined;
|
|
3252
|
+
protected getProviderName(): string;
|
|
3253
|
+
}
|
|
3254
|
+
declare const anthropicLLM: AnthropicLLM;
|
|
3255
|
+
|
|
3256
|
+
interface GroqLLMConfig extends BaseLLMConfig {
|
|
3257
|
+
}
|
|
3258
|
+
/**
|
|
3259
|
+
* GroqLLM class for handling AI-powered component generation and matching using Groq
|
|
3260
|
+
*/
|
|
3261
|
+
declare class GroqLLM extends BaseLLM {
|
|
3262
|
+
constructor(config?: GroqLLMConfig);
|
|
3263
|
+
protected getDefaultModel(): string;
|
|
3264
|
+
protected getDefaultFastModel(): string;
|
|
3265
|
+
protected getDefaultApiKey(): string | undefined;
|
|
3266
|
+
protected getProviderName(): string;
|
|
3267
|
+
}
|
|
3268
|
+
declare const groqLLM: GroqLLM;
|
|
3269
|
+
|
|
3270
|
+
interface GeminiLLMConfig extends BaseLLMConfig {
|
|
3271
|
+
}
|
|
3272
|
+
/**
|
|
3273
|
+
* GeminiLLM class for handling AI-powered component generation and matching using Google Gemini
|
|
3274
|
+
*/
|
|
3275
|
+
declare class GeminiLLM extends BaseLLM {
|
|
3276
|
+
constructor(config?: GeminiLLMConfig);
|
|
3277
|
+
protected getDefaultModel(): string;
|
|
3278
|
+
protected getDefaultFastModel(): string;
|
|
3279
|
+
protected getDefaultApiKey(): string | undefined;
|
|
3280
|
+
protected getProviderName(): string;
|
|
3281
|
+
}
|
|
3282
|
+
declare const geminiLLM: GeminiLLM;
|
|
3283
|
+
|
|
3284
|
+
interface OpenAILLMConfig extends BaseLLMConfig {
|
|
3285
|
+
}
|
|
3286
|
+
/**
|
|
3287
|
+
* OpenAILLM class for handling AI-powered component generation and matching using OpenAI GPT models
|
|
3288
|
+
*/
|
|
3289
|
+
declare class OpenAILLM extends BaseLLM {
|
|
3290
|
+
constructor(config?: OpenAILLMConfig);
|
|
3291
|
+
protected getDefaultModel(): string;
|
|
3292
|
+
protected getDefaultFastModel(): string;
|
|
3293
|
+
protected getDefaultApiKey(): string | undefined;
|
|
3294
|
+
protected getProviderName(): string;
|
|
3295
|
+
}
|
|
3296
|
+
declare const openaiLLM: OpenAILLM;
|
|
3297
|
+
|
|
3298
|
+
/**
|
|
3299
|
+
* Query Cache — Two mechanisms:
|
|
3300
|
+
*
|
|
3301
|
+
* 1. `cache` (query string → result data) — TTL-based with max size, for avoiding re-execution
|
|
3302
|
+
* of recently validated queries. True LRU eviction: reads bubble entries to the back via
|
|
3303
|
+
* delete+re-set so the oldest *unused* entry is evicted, not the oldest *inserted*.
|
|
3304
|
+
*
|
|
3305
|
+
* 2. Encrypted queryId tokens — SQL is encrypted into the queryId itself (self-contained).
|
|
3306
|
+
* No server-side storage needed for SQL mappings. The token is decrypted on each request.
|
|
3307
|
+
* This eliminates the unbounded queryIdCache that previously grew forever and caused
|
|
3308
|
+
* memory bloat (hundreds of MBs after thousands of queries).
|
|
3309
|
+
*
|
|
3310
|
+
* Result data can still be cached temporarily via the data cache (mechanism 1).
|
|
3311
|
+
*/
|
|
3312
|
+
declare class QueryCache {
|
|
3313
|
+
private cache;
|
|
3314
|
+
private ttlMs;
|
|
3315
|
+
private maxCacheSize;
|
|
3316
|
+
private cleanupInterval;
|
|
3317
|
+
private readonly algorithm;
|
|
3318
|
+
private encryptionKey;
|
|
3319
|
+
constructor();
|
|
3320
|
+
/**
|
|
3321
|
+
* Set the cache TTL (Time To Live)
|
|
3322
|
+
* @param minutes - TTL in minutes (default: 10)
|
|
3323
|
+
*/
|
|
3324
|
+
setTTL(minutes: number): void;
|
|
3325
|
+
/**
|
|
3326
|
+
* Get the current TTL in minutes
|
|
3327
|
+
*/
|
|
3328
|
+
getTTL(): number;
|
|
3329
|
+
/**
|
|
3330
|
+
* Store query result in data cache.
|
|
3331
|
+
* If the key already exists, it's removed first so the re-insert places it
|
|
3332
|
+
* at the back of the iteration order (LRU). Eviction only fires when adding
|
|
3333
|
+
* a genuinely new key past the size limit.
|
|
3334
|
+
*/
|
|
3335
|
+
set(query: string, data: any): void;
|
|
3336
|
+
/**
|
|
3337
|
+
* Get cached result if exists and not expired.
|
|
3338
|
+
* On hit, re-inserts the entry so it moves to the back of the Map's
|
|
3339
|
+
* iteration order — turning FIFO eviction into true LRU.
|
|
3340
|
+
*/
|
|
3341
|
+
get(query: string): any | null;
|
|
3342
|
+
/**
|
|
3343
|
+
* Check if query exists in cache (not expired)
|
|
3344
|
+
*/
|
|
3345
|
+
has(query: string): boolean;
|
|
3346
|
+
/**
|
|
3347
|
+
* Remove a specific query from cache
|
|
3348
|
+
*/
|
|
3349
|
+
delete(query: string): void;
|
|
3350
|
+
/**
|
|
3351
|
+
* Clear all cached entries
|
|
3352
|
+
*/
|
|
3353
|
+
clear(): void;
|
|
3354
|
+
/**
|
|
3355
|
+
* Get cache statistics
|
|
3356
|
+
*/
|
|
3357
|
+
getStats(): {
|
|
3358
|
+
size: number;
|
|
3359
|
+
queryIdCount: number;
|
|
3360
|
+
oldestEntryAge: number | null;
|
|
3361
|
+
};
|
|
3362
|
+
/**
|
|
3363
|
+
* Start periodic cleanup of expired data cache entries.
|
|
3364
|
+
*/
|
|
3365
|
+
private startCleanup;
|
|
3366
|
+
/**
|
|
3367
|
+
* Encrypt a payload into a self-contained token.
|
|
3368
|
+
*/
|
|
3369
|
+
private encrypt;
|
|
3370
|
+
/**
|
|
3371
|
+
* Decrypt a token back to the original payload.
|
|
3372
|
+
*/
|
|
3373
|
+
private decrypt;
|
|
3374
|
+
/**
|
|
3375
|
+
* Store a query by generating an encrypted token as queryId.
|
|
3376
|
+
* The SQL is encrypted INTO the token — nothing stored in memory.
|
|
3377
|
+
* If data is provided, it's cached temporarily in the data cache.
|
|
3378
|
+
*/
|
|
3379
|
+
storeQuery(query: any, data?: any): string;
|
|
3380
|
+
/**
|
|
3381
|
+
* Get a stored query by decrypting its token.
|
|
3382
|
+
* Returns the SQL + any cached result data.
|
|
3383
|
+
*/
|
|
3384
|
+
getQuery(queryId: string): {
|
|
3385
|
+
query: any;
|
|
3386
|
+
data: any;
|
|
3387
|
+
} | null;
|
|
3388
|
+
/**
|
|
3389
|
+
* Update cached data for a queryId token
|
|
3390
|
+
*/
|
|
3391
|
+
setQueryData(queryId: string, data: any): void;
|
|
3392
|
+
/**
|
|
3393
|
+
* Stop cleanup interval (for graceful shutdown)
|
|
3394
|
+
*/
|
|
3395
|
+
destroy(): void;
|
|
3396
|
+
}
|
|
3397
|
+
declare const queryCache: QueryCache;
|
|
3398
|
+
|
|
3399
|
+
/**
|
|
3400
|
+
* Manages conversation history scoped per user + dashboard.
|
|
3401
|
+
* Each user-dashboard pair has its own isolated history that expires after a configurable TTL.
|
|
3402
|
+
*/
|
|
3403
|
+
declare class DashboardConversationHistory {
|
|
3404
|
+
private histories;
|
|
3405
|
+
private ttlMs;
|
|
3406
|
+
private maxEntries;
|
|
3407
|
+
private cleanupInterval;
|
|
3408
|
+
constructor();
|
|
3409
|
+
/**
|
|
3410
|
+
* Set the TTL for dashboard histories
|
|
3411
|
+
* @param minutes - TTL in minutes
|
|
3412
|
+
*/
|
|
3413
|
+
setTTL(minutes: number): void;
|
|
3414
|
+
/**
|
|
3415
|
+
* Set max entries per dashboard
|
|
3416
|
+
*/
|
|
3417
|
+
setMaxEntries(max: number): void;
|
|
3418
|
+
/**
|
|
3419
|
+
* Add a conversation entry for a user's dashboard
|
|
3420
|
+
*/
|
|
3421
|
+
addEntry(dashboardId: string, userPrompt: string, componentSummary: string, userId?: string): void;
|
|
3422
|
+
/**
|
|
3423
|
+
* Get formatted conversation history for a user's dashboard
|
|
3424
|
+
*/
|
|
3425
|
+
getHistory(dashboardId: string, userId?: string): string;
|
|
3426
|
+
/**
|
|
3427
|
+
* Clear history for a specific user's dashboard
|
|
3428
|
+
*/
|
|
3429
|
+
clearDashboard(dashboardId: string, userId?: string): void;
|
|
3430
|
+
/**
|
|
3431
|
+
* Clear all dashboard histories
|
|
3432
|
+
*/
|
|
3433
|
+
clearAll(): void;
|
|
3434
|
+
/**
|
|
3435
|
+
* Start periodic cleanup of expired histories
|
|
3436
|
+
*/
|
|
3437
|
+
private startCleanup;
|
|
3438
|
+
/**
|
|
3439
|
+
* Stop cleanup interval (for graceful shutdown)
|
|
3440
|
+
*/
|
|
3441
|
+
destroy(): void;
|
|
3442
|
+
}
|
|
3443
|
+
declare const dashboardConversationHistory: DashboardConversationHistory;
|
|
3444
|
+
|
|
3445
|
+
/**
|
|
3446
|
+
* ScriptMatcher — LLM-Based Script Matching + Parameter Extraction
|
|
3447
|
+
*
|
|
3448
|
+
* Uses ONE LLM call to:
|
|
3449
|
+
* 1. Pick the best matching script from the library (or "none")
|
|
3450
|
+
* 2. Extract parameter values from the user question
|
|
3451
|
+
*
|
|
3452
|
+
* Why LLM over embeddings:
|
|
3453
|
+
* - Embeddings capture topic similarity ("overstock" ≈ "inventory" ≈ "revenue")
|
|
3454
|
+
* but can't distinguish structurally different questions about the same domain
|
|
3455
|
+
* - LLM understands that "overstock by warehouse" needs a different script than
|
|
3456
|
+
* "revenue by warehouse" even though they're semantically close
|
|
3457
|
+
* - One call does both matching AND parameter extraction
|
|
3458
|
+
*
|
|
3459
|
+
* When script library grows past ~50, add an embedding pre-filter
|
|
3460
|
+
* (ChromaDB narrows to top 10 → LLM picks from those 10).
|
|
3461
|
+
*/
|
|
3462
|
+
|
|
3463
|
+
declare class ScriptMatcher {
|
|
3464
|
+
private store;
|
|
3465
|
+
constructor(store: ScriptStore);
|
|
3466
|
+
/**
|
|
3467
|
+
* Find the best matching script for a user question.
|
|
3468
|
+
* Uses ONE LLM call that picks the script AND extracts parameters.
|
|
3469
|
+
* Returns null if no script matches.
|
|
3470
|
+
*/
|
|
3471
|
+
match(userPrompt: string, apiKey?: string, model?: string): Promise<ScriptMatch | null>;
|
|
3472
|
+
/**
|
|
3473
|
+
* Build the script catalog string for the LLM prompt.
|
|
3474
|
+
* Each script gets: index, ID, name, description, and parameter definitions.
|
|
3475
|
+
*/
|
|
3476
|
+
private buildScriptCatalog;
|
|
3477
|
+
}
|
|
3478
|
+
|
|
3479
|
+
/**
|
|
3480
|
+
* ScriptRunner — Execute scripts in an isolated tsx subprocess.
|
|
3481
|
+
*
|
|
3482
|
+
* The subprocess approach replaces the earlier `new Function()` eval and gives us:
|
|
3483
|
+
* - Real sandbox (separate process, SIGKILL on timeout).
|
|
3484
|
+
* - Real TypeScript (tsx transpiles on the fly).
|
|
3485
|
+
* - npm imports available to scripts (clustering, stats, geo, etc.).
|
|
3486
|
+
*
|
|
3487
|
+
* Protocol: NDJSON over the child's stdin/stdout. See script-ipc.ts + backend/docs/SCRIPT-FLOW-IMPLEMENTATION.md.
|
|
3488
|
+
*/
|
|
3489
|
+
|
|
3490
|
+
interface RunScriptOptions {
|
|
3491
|
+
/** Data sources the script is allowed to query via ctx.query */
|
|
3492
|
+
externalTools: ExternalTool[];
|
|
3493
|
+
/** Optional — for propagating per-query UI progress to the user */
|
|
3494
|
+
streamBuffer?: StreamBuffer;
|
|
3495
|
+
/** Override the wall-clock timeout (default `SCRIPT_TIMEOUT_MS`, 60s). */
|
|
3496
|
+
timeoutMs?: number;
|
|
3497
|
+
}
|
|
3498
|
+
/**
|
|
3499
|
+
* Execute a recipe by spawning a tsx child on the script's .ts file.
|
|
3500
|
+
* `scriptPath` is the absolute path to the saved `.ts` body.
|
|
3501
|
+
*/
|
|
3502
|
+
declare function runScript(recipe: ScriptRecipe, scriptPath: string, params: Record<string, any>, options: RunScriptOptions): Promise<ScriptResult>;
|
|
3503
|
+
|
|
1292
3504
|
type MessageTypeHandler = (message: IncomingMessage) => void | Promise<void>;
|
|
1293
3505
|
declare class SuperatomSDK {
|
|
1294
3506
|
private ws;
|
|
1295
3507
|
private url;
|
|
1296
|
-
private apiKey
|
|
3508
|
+
private apiKey?;
|
|
1297
3509
|
private projectId;
|
|
1298
|
-
private userId;
|
|
1299
3510
|
private type;
|
|
1300
3511
|
private bundleDir;
|
|
1301
3512
|
private messageHandlers;
|
|
@@ -1305,15 +3516,30 @@ declare class SuperatomSDK {
|
|
|
1305
3516
|
private maxReconnectAttempts;
|
|
1306
3517
|
private collections;
|
|
1307
3518
|
private components;
|
|
3519
|
+
private tools;
|
|
3520
|
+
private workflows;
|
|
1308
3521
|
private anthropicApiKey;
|
|
1309
3522
|
private groqApiKey;
|
|
3523
|
+
private geminiApiKey;
|
|
3524
|
+
private openaiApiKey;
|
|
1310
3525
|
private llmProviders;
|
|
3526
|
+
private databaseType;
|
|
3527
|
+
private modelStrategy;
|
|
3528
|
+
private mainAgentModel;
|
|
3529
|
+
private sourceAgentModel;
|
|
3530
|
+
private dashCompModels?;
|
|
3531
|
+
private conversationSimilarityThreshold;
|
|
1311
3532
|
private userManager;
|
|
1312
3533
|
private dashboardManager;
|
|
1313
3534
|
private reportManager;
|
|
3535
|
+
private pingInterval;
|
|
3536
|
+
private lastPong;
|
|
3537
|
+
private readonly PING_INTERVAL_MS;
|
|
3538
|
+
private readonly PONG_TIMEOUT_MS;
|
|
1314
3539
|
constructor(config: SuperatomSDKConfig);
|
|
1315
3540
|
/**
|
|
1316
3541
|
* Initialize PromptLoader and load prompts into memory
|
|
3542
|
+
* Tries to load from file system first, then falls back to hardcoded prompts
|
|
1317
3543
|
*/
|
|
1318
3544
|
private initializePromptLoader;
|
|
1319
3545
|
/**
|
|
@@ -1349,9 +3575,11 @@ declare class SuperatomSDK {
|
|
|
1349
3575
|
*/
|
|
1350
3576
|
private handleMessage;
|
|
1351
3577
|
/**
|
|
1352
|
-
* Send a message to the Superatom service
|
|
3578
|
+
* Send a message to the Superatom service.
|
|
3579
|
+
* Returns true if the message was sent, false if the WebSocket is not connected.
|
|
3580
|
+
* Does NOT throw on closed connections — callers can check the return value if needed.
|
|
1353
3581
|
*/
|
|
1354
|
-
send(message: Message):
|
|
3582
|
+
send(message: Message): boolean;
|
|
1355
3583
|
/**
|
|
1356
3584
|
* Register a message handler to receive all messages
|
|
1357
3585
|
*/
|
|
@@ -1377,7 +3605,69 @@ declare class SuperatomSDK {
|
|
|
1377
3605
|
*/
|
|
1378
3606
|
addCollection<TParams = any, TResult = any>(collectionName: string, operation: CollectionOperation | string, handler: CollectionHandler<TParams, TResult>): void;
|
|
1379
3607
|
private handleReconnect;
|
|
3608
|
+
/**
|
|
3609
|
+
* Start heartbeat to keep WebSocket connection alive
|
|
3610
|
+
* Sends PING every 3 minutes to prevent idle timeout from cloud infrastructure
|
|
3611
|
+
*/
|
|
3612
|
+
private startHeartbeat;
|
|
3613
|
+
/**
|
|
3614
|
+
* Stop the heartbeat interval
|
|
3615
|
+
*/
|
|
3616
|
+
private stopHeartbeat;
|
|
3617
|
+
/**
|
|
3618
|
+
* Handle PONG response from server
|
|
3619
|
+
*/
|
|
3620
|
+
private handlePong;
|
|
1380
3621
|
private storeComponents;
|
|
3622
|
+
/**
|
|
3623
|
+
* Set tools for the SDK instance
|
|
3624
|
+
*/
|
|
3625
|
+
setTools(tools: Tool$1[]): void;
|
|
3626
|
+
/**
|
|
3627
|
+
* Get the stored tools
|
|
3628
|
+
*/
|
|
3629
|
+
getTools(): Tool$1[];
|
|
3630
|
+
/**
|
|
3631
|
+
* Register workflow components for the SDK instance.
|
|
3632
|
+
*
|
|
3633
|
+
* Workflows are pre-built multi-step UI flows the main agent can pick when
|
|
3634
|
+
* the user's prompt matches a workflow's `whenToUse` trigger. Picking a
|
|
3635
|
+
* workflow short-circuits analysis text + dashboard component generation —
|
|
3636
|
+
* the workflow component is returned directly, with the LLM-extracted props.
|
|
3637
|
+
*/
|
|
3638
|
+
setWorkflows(workflows: WorkflowDescriptor[]): void;
|
|
3639
|
+
/**
|
|
3640
|
+
* Get the registered workflow components.
|
|
3641
|
+
*/
|
|
3642
|
+
getWorkflows(): WorkflowDescriptor[];
|
|
3643
|
+
/**
|
|
3644
|
+
* Apply model strategy to all LLM provider singletons
|
|
3645
|
+
* @param strategy - 'best', 'fast', or 'balanced'
|
|
3646
|
+
*/
|
|
3647
|
+
private applyModelStrategy;
|
|
3648
|
+
/**
|
|
3649
|
+
* Set model strategy at runtime
|
|
3650
|
+
* @param strategy - 'best', 'fast', or 'balanced'
|
|
3651
|
+
*/
|
|
3652
|
+
setModelStrategy(strategy: ModelStrategy): void;
|
|
3653
|
+
/**
|
|
3654
|
+
* Get current model strategy
|
|
3655
|
+
*/
|
|
3656
|
+
getModelStrategy(): ModelStrategy;
|
|
3657
|
+
/**
|
|
3658
|
+
* Apply conversation similarity threshold to all LLM provider singletons
|
|
3659
|
+
* @param threshold - Value between 0 and 1 (e.g., 0.8 = 80% similarity required)
|
|
3660
|
+
*/
|
|
3661
|
+
private applyConversationSimilarityThreshold;
|
|
3662
|
+
/**
|
|
3663
|
+
* Set conversation similarity threshold at runtime
|
|
3664
|
+
* @param threshold - Value between 0 and 1 (e.g., 0.8 = 80% similarity required)
|
|
3665
|
+
*/
|
|
3666
|
+
setConversationSimilarityThreshold(threshold: number): void;
|
|
3667
|
+
/**
|
|
3668
|
+
* Get current conversation similarity threshold
|
|
3669
|
+
*/
|
|
3670
|
+
getConversationSimilarityThreshold(): number;
|
|
1381
3671
|
}
|
|
1382
3672
|
|
|
1383
|
-
export { type Action, CONTEXT_CONFIG, type CapturedLog, CleanupService, type CollectionHandler, type CollectionOperation, type IncomingMessage, LLM, type LogLevel, type Message,
|
|
3673
|
+
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 };
|