@superatomai/sdk-node 0.0.11 → 0.0.12-s

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -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,7 +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
+ */
50
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;
51
74
  }
52
75
  declare const logger: Logger;
53
76
 
@@ -78,7 +101,27 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
78
101
  deps?: string[] | undefined;
79
102
  }>, "many">>;
80
103
  data: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
81
- 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>;
82
125
  query: z.ZodOptional<z.ZodObject<{
83
126
  graphql: z.ZodOptional<z.ZodString>;
84
127
  sql: z.ZodOptional<z.ZodString>;
@@ -117,6 +160,7 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
117
160
  dependencies?: string[] | undefined;
118
161
  } | undefined;
119
162
  props?: Record<string, any> | undefined;
163
+ render?: any;
120
164
  states?: Record<string, any> | undefined;
121
165
  methods?: Record<string, {
122
166
  fn: string;
@@ -127,7 +171,14 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
127
171
  deps?: string[] | undefined;
128
172
  }[] | undefined;
129
173
  data?: Record<string, any> | undefined;
130
- render?: any;
174
+ pages?: {
175
+ id: string;
176
+ name: string;
177
+ order: number;
178
+ icon?: string | undefined;
179
+ render?: any;
180
+ }[] | undefined;
181
+ defaultPageId?: string | undefined;
131
182
  }, {
132
183
  id: string;
133
184
  name?: string | undefined;
@@ -141,6 +192,7 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
141
192
  dependencies?: string[] | undefined;
142
193
  } | undefined;
143
194
  props?: Record<string, any> | undefined;
195
+ render?: any;
144
196
  states?: Record<string, any> | undefined;
145
197
  methods?: Record<string, {
146
198
  fn: string;
@@ -151,7 +203,14 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
151
203
  deps?: string[] | undefined;
152
204
  }[] | undefined;
153
205
  data?: Record<string, any> | undefined;
154
- render?: any;
206
+ pages?: {
207
+ id: string;
208
+ name: string;
209
+ order: number;
210
+ icon?: string | undefined;
211
+ render?: any;
212
+ }[] | undefined;
213
+ defaultPageId?: string | undefined;
155
214
  }>;
156
215
  data: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
157
216
  context: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
@@ -169,6 +228,7 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
169
228
  dependencies?: string[] | undefined;
170
229
  } | undefined;
171
230
  props?: Record<string, any> | undefined;
231
+ render?: any;
172
232
  states?: Record<string, any> | undefined;
173
233
  methods?: Record<string, {
174
234
  fn: string;
@@ -179,7 +239,14 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
179
239
  deps?: string[] | undefined;
180
240
  }[] | undefined;
181
241
  data?: Record<string, any> | undefined;
182
- render?: any;
242
+ pages?: {
243
+ id: string;
244
+ name: string;
245
+ order: number;
246
+ icon?: string | undefined;
247
+ render?: any;
248
+ }[] | undefined;
249
+ defaultPageId?: string | undefined;
183
250
  };
184
251
  data?: Record<string, any> | undefined;
185
252
  context?: Record<string, any> | undefined;
@@ -197,6 +264,7 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
197
264
  dependencies?: string[] | undefined;
198
265
  } | undefined;
199
266
  props?: Record<string, any> | undefined;
267
+ render?: any;
200
268
  states?: Record<string, any> | undefined;
201
269
  methods?: Record<string, {
202
270
  fn: string;
@@ -207,7 +275,14 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
207
275
  deps?: string[] | undefined;
208
276
  }[] | undefined;
209
277
  data?: Record<string, any> | undefined;
210
- render?: any;
278
+ pages?: {
279
+ id: string;
280
+ name: string;
281
+ order: number;
282
+ icon?: string | undefined;
283
+ render?: any;
284
+ }[] | undefined;
285
+ defaultPageId?: string | undefined;
211
286
  };
212
287
  data?: Record<string, any> | undefined;
213
288
  context?: Record<string, any> | undefined;
@@ -280,6 +355,7 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
280
355
  dependencies?: string[] | undefined;
281
356
  } | undefined;
282
357
  props?: Record<string, any> | undefined;
358
+ render?: any;
283
359
  states?: Record<string, any> | undefined;
284
360
  methods?: Record<string, {
285
361
  fn: string;
@@ -290,7 +366,6 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
290
366
  deps?: string[] | undefined;
291
367
  }[] | undefined;
292
368
  data?: Record<string, any> | undefined;
293
- render?: any;
294
369
  }, {
295
370
  id: string;
296
371
  name?: string | undefined;
@@ -304,6 +379,7 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
304
379
  dependencies?: string[] | undefined;
305
380
  } | undefined;
306
381
  props?: Record<string, any> | undefined;
382
+ render?: any;
307
383
  states?: Record<string, any> | undefined;
308
384
  methods?: Record<string, {
309
385
  fn: string;
@@ -314,7 +390,6 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
314
390
  deps?: string[] | undefined;
315
391
  }[] | undefined;
316
392
  data?: Record<string, any> | undefined;
317
- render?: any;
318
393
  }>;
319
394
  data: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
320
395
  context: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
@@ -332,6 +407,7 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
332
407
  dependencies?: string[] | undefined;
333
408
  } | undefined;
334
409
  props?: Record<string, any> | undefined;
410
+ render?: any;
335
411
  states?: Record<string, any> | undefined;
336
412
  methods?: Record<string, {
337
413
  fn: string;
@@ -342,7 +418,6 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
342
418
  deps?: string[] | undefined;
343
419
  }[] | undefined;
344
420
  data?: Record<string, any> | undefined;
345
- render?: any;
346
421
  };
347
422
  data?: Record<string, any> | undefined;
348
423
  context?: Record<string, any> | undefined;
@@ -360,6 +435,7 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
360
435
  dependencies?: string[] | undefined;
361
436
  } | undefined;
362
437
  props?: Record<string, any> | undefined;
438
+ render?: any;
363
439
  states?: Record<string, any> | undefined;
364
440
  methods?: Record<string, {
365
441
  fn: string;
@@ -370,7 +446,6 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
370
446
  deps?: string[] | undefined;
371
447
  }[] | undefined;
372
448
  data?: Record<string, any> | undefined;
373
- render?: any;
374
449
  };
375
450
  data?: Record<string, any> | undefined;
376
451
  context?: Record<string, any> | undefined;
@@ -383,6 +458,7 @@ declare const UserSchema: z.ZodObject<{
383
458
  password: z.ZodString;
384
459
  fullname: z.ZodOptional<z.ZodString>;
385
460
  role: z.ZodOptional<z.ZodString>;
461
+ userInfo: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
386
462
  wsIds: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
387
463
  }, "strip", z.ZodTypeAny, {
388
464
  username: string;
@@ -390,6 +466,7 @@ declare const UserSchema: z.ZodObject<{
390
466
  email?: string | undefined;
391
467
  fullname?: string | undefined;
392
468
  role?: string | undefined;
469
+ userInfo?: Record<string, unknown> | undefined;
393
470
  wsIds?: string[] | undefined;
394
471
  }, {
395
472
  username: string;
@@ -397,6 +474,7 @@ declare const UserSchema: z.ZodObject<{
397
474
  email?: string | undefined;
398
475
  fullname?: string | undefined;
399
476
  role?: string | undefined;
477
+ userInfo?: Record<string, unknown> | undefined;
400
478
  wsIds?: string[] | undefined;
401
479
  }>;
402
480
  type User = z.infer<typeof UserSchema>;
@@ -407,6 +485,7 @@ declare const UsersDataSchema: z.ZodObject<{
407
485
  password: z.ZodString;
408
486
  fullname: z.ZodOptional<z.ZodString>;
409
487
  role: z.ZodOptional<z.ZodString>;
488
+ userInfo: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
410
489
  wsIds: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
411
490
  }, "strip", z.ZodTypeAny, {
412
491
  username: string;
@@ -414,6 +493,7 @@ declare const UsersDataSchema: z.ZodObject<{
414
493
  email?: string | undefined;
415
494
  fullname?: string | undefined;
416
495
  role?: string | undefined;
496
+ userInfo?: Record<string, unknown> | undefined;
417
497
  wsIds?: string[] | undefined;
418
498
  }, {
419
499
  username: string;
@@ -421,6 +501,7 @@ declare const UsersDataSchema: z.ZodObject<{
421
501
  email?: string | undefined;
422
502
  fullname?: string | undefined;
423
503
  role?: string | undefined;
504
+ userInfo?: Record<string, unknown> | undefined;
424
505
  wsIds?: string[] | undefined;
425
506
  }>, "many">;
426
507
  }, "strip", z.ZodTypeAny, {
@@ -430,6 +511,7 @@ declare const UsersDataSchema: z.ZodObject<{
430
511
  email?: string | undefined;
431
512
  fullname?: string | undefined;
432
513
  role?: string | undefined;
514
+ userInfo?: Record<string, unknown> | undefined;
433
515
  wsIds?: string[] | undefined;
434
516
  }[];
435
517
  }, {
@@ -439,6 +521,7 @@ declare const UsersDataSchema: z.ZodObject<{
439
521
  email?: string | undefined;
440
522
  fullname?: string | undefined;
441
523
  role?: string | undefined;
524
+ userInfo?: Record<string, unknown> | undefined;
442
525
  wsIds?: string[] | undefined;
443
526
  }[];
444
527
  }>;
@@ -543,22 +626,439 @@ declare const IncomingMessageSchema: z.ZodObject<{
543
626
  payload?: unknown;
544
627
  }>;
545
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>;
546
828
  type CollectionOperation = 'getMany' | 'getOne' | 'query' | 'mutation' | 'updateOne' | 'deleteOne' | 'createOne';
547
829
  type CollectionHandler<TParams = any, TResult = any> = (params: TParams) => Promise<TResult> | TResult;
548
- type LLMProvider = 'anthropic' | 'groq';
830
+ type LLMProvider = 'anthropic' | 'groq' | 'gemini' | 'openai';
549
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
+ }
550
856
  interface SuperatomSDKConfig {
551
857
  url?: string;
552
- apiKey: string;
858
+ apiKey?: string;
553
859
  projectId: string;
554
- userId?: string;
555
860
  type?: string;
556
861
  bundleDir?: string;
557
862
  promptsDir?: string;
863
+ databaseType?: DatabaseType;
558
864
  ANTHROPIC_API_KEY?: string;
559
865
  GROQ_API_KEY?: string;
866
+ GEMINI_API_KEY?: string;
867
+ OPENAI_API_KEY?: string;
560
868
  LLM_PROVIDERS?: LLMProvider[];
561
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[];
562
1062
  }
563
1063
 
564
1064
  /**
@@ -820,148 +1320,1055 @@ declare class ReportManager {
820
1320
  getReportCount(): number;
821
1321
  }
822
1322
 
823
- interface LLMMessages {
824
- sys: string;
825
- user: string;
826
- }
827
- interface LLMOptions {
828
- model?: string;
829
- maxTokens?: number;
830
- temperature?: number;
831
- topP?: number;
832
- apiKey?: string;
833
- partial?: (chunk: string) => void;
834
- }
835
- interface Tool {
836
- name: string;
837
- description: string;
838
- input_schema: {
839
- type: string;
840
- properties: Record<string, any>;
841
- required?: string[];
842
- };
843
- }
844
- declare class LLM {
845
- static text(messages: LLMMessages, options?: LLMOptions): Promise<string>;
846
- static stream<T = string>(messages: LLMMessages, options?: LLMOptions, json?: boolean): Promise<T extends string ? string : any>;
847
- static streamWithTools(messages: LLMMessages, tools: Tool[], toolHandler: (toolName: string, toolInput: any) => Promise<any>, options?: LLMOptions, maxIterations?: number): Promise<string>;
848
- /**
849
- * Parse model string to extract provider and model name
850
- * @param modelString - Format: "provider/model-name" or just "model-name"
851
- * @returns [provider, modelName]
852
- *
853
- * @example
854
- * "anthropic/claude-sonnet-4-5" → ["anthropic", "claude-sonnet-4-5"]
855
- * "groq/openai/gpt-oss-120b" → ["groq", "openai/gpt-oss-120b"]
856
- * "claude-sonnet-4-5" → ["anthropic", "claude-sonnet-4-5"] (default)
857
- */
858
- private static _parseModel;
859
- private static _anthropicText;
860
- private static _anthropicStream;
861
- private static _anthropicStreamWithTools;
862
- private static _groqText;
863
- private static _groqStream;
864
- /**
865
- * Parse JSON string, handling markdown code blocks and surrounding text
866
- * Enhanced version with jsonrepair to handle malformed JSON from LLMs
867
- * @param text - Text that may contain JSON wrapped in ```json...``` or with surrounding text
868
- * @returns Parsed JSON object or array
869
- */
870
- private static _parseJSON;
871
- }
872
-
873
- interface CapturedLog {
874
- timestamp: number;
875
- level: 'info' | 'error' | 'warn' | 'debug';
876
- message: string;
877
- type?: 'explanation' | 'query' | 'general';
878
- data?: Record<string, any>;
879
- }
880
1323
  /**
881
- * UILogCollector captures logs during user prompt processing
882
- * and sends them to runtime via ui_logs message with uiBlockId as the message id
883
- * Logs are sent in real-time for streaming effect in the UI
884
- * 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
885
1326
  */
886
- declare class UILogCollector {
887
- private logs;
888
- private uiBlockId;
889
- private clientId;
890
- private sendMessage;
891
- private currentLogLevel;
892
- constructor(clientId: string, sendMessage: (message: Message) => void, uiBlockId?: string);
893
- /**
894
- * Check if logging is enabled (uiBlockId is provided)
895
- */
896
- isEnabled(): boolean;
897
- /**
898
- * Check if a message should be logged based on current log level
899
- */
900
- private shouldLog;
901
- /**
902
- * Add a log entry with timestamp and immediately send to runtime
903
- * Only logs that pass the log level filter are captured and sent
904
- */
905
- private addLog;
906
- /**
907
- * Send a single log to runtime immediately
908
- */
909
- private sendLogImmediately;
910
- /**
911
- * Log info message
912
- */
913
- info(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
914
- /**
915
- * Log error message
916
- */
917
- error(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
918
- /**
919
- * Log warning message
920
- */
921
- warn(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
922
- /**
923
- * Log debug message
924
- */
925
- debug(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
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);
926
1338
  /**
927
- * Log LLM explanation with typed metadata
1339
+ * Check if the buffer has a callback configured
928
1340
  */
929
- logExplanation(message: string, explanation: string, data?: Record<string, any>): void;
1341
+ hasCallback(): boolean;
930
1342
  /**
931
- * Log generated query with typed metadata
1343
+ * Get all text that has been written (including already flushed)
932
1344
  */
933
- logQuery(message: string, query: string, data?: Record<string, any>): void;
1345
+ getFullText(): string;
934
1346
  /**
935
- * Send all collected logs at once (optional, for final summary)
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
936
1352
  */
937
- sendAllLogs(): void;
1353
+ write(chunk: string): void;
938
1354
  /**
939
- * Get all collected logs
1355
+ * Flush the buffer immediately
1356
+ * Call this before tool execution or other operations that need clean output
940
1357
  */
941
- getLogs(): CapturedLog[];
1358
+ flush(): void;
942
1359
  /**
943
- * Clear all logs
1360
+ * Internal flush implementation
944
1361
  */
945
- clearLogs(): void;
1362
+ private flushNow;
946
1363
  /**
947
- * Set uiBlockId (in case it's provided later)
1364
+ * Clean up resources
1365
+ * Call this when done with the buffer
948
1366
  */
949
- setUIBlockId(uiBlockId: string): void;
1367
+ dispose(): void;
950
1368
  }
951
1369
 
952
1370
  /**
953
- * Represents an action that can be performed on a UIBlock
1371
+ * ToolExecutorService - Handles execution of SQL queries and external tools
1372
+ * Extracted from BaseLLM.generateTextResponse for better separation of concerns
954
1373
  */
955
- interface Action {
1374
+
1375
+ /**
1376
+ * External tool definition
1377
+ */
1378
+ interface ExternalTool {
956
1379
  id: string;
957
1380
  name: string;
958
- type: string;
959
- [key: string]: any;
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>;
960
1396
  }
961
-
962
1397
  /**
963
- * UIBlock represents a single user and assistant message block in a thread
964
- * Contains user question, component metadata, component data, text response, and available actions
1398
+ * Executed tool tracking info
1399
+ */
1400
+ interface ExecutedToolInfo {
1401
+ id: string;
1402
+ name: string;
1403
+ params: any;
1404
+ result: {
1405
+ _totalRecords: number;
1406
+ _recordsShown: number;
1407
+ _metadata?: any;
1408
+ _sampleData: any[];
1409
+ };
1410
+ outputSchema?: any;
1411
+ sourceSchema?: string;
1412
+ sourceType?: string;
1413
+ }
1414
+
1415
+ /**
1416
+ * Multi-Agent Architecture Types
1417
+ *
1418
+ * Defines interfaces for the hierarchical agent system:
1419
+ * - Main Agent: ONE LLM.streamWithTools() call with source agent tools
1420
+ * - Source Agents: independent agents that query individual data sources
1421
+ *
1422
+ * The main agent sees only source summaries. When it calls a source tool,
1423
+ * the SourceAgent runs independently (own LLM, own retries) and returns clean data.
1424
+ */
1425
+
1426
+ /**
1427
+ * Per-entity detail: name, row count, and column names.
1428
+ * Gives the main agent enough context to route to the right source.
1429
+ */
1430
+ interface EntityDetail {
1431
+ /** Entity name (table, sheet, endpoint) */
1432
+ name: string;
1433
+ /** Approximate row count */
1434
+ rowCount?: number;
1435
+ /** Column/field names */
1436
+ columns: string[];
1437
+ }
1438
+ /**
1439
+ * Representation of a data source for the main agent.
1440
+ * Contains entity names WITH column names so the LLM can route accurately.
1441
+ */
1442
+ interface SourceSummary {
1443
+ /** Source ID (matches tool ID prefix) */
1444
+ id: string;
1445
+ /** Human-readable source name */
1446
+ name: string;
1447
+ /** Source type: postgres, excel, rest_api, etc. */
1448
+ type: string;
1449
+ /** Brief description of what data this source contains */
1450
+ description: string;
1451
+ /** Detailed entity info with column names for routing */
1452
+ entityDetails: EntityDetail[];
1453
+ /** The tool ID associated with this source */
1454
+ toolId: string;
1455
+ }
1456
+ /**
1457
+ * What a source agent returns after querying its data source.
1458
+ * The main agent uses this to analyze and compose the final response.
1459
+ */
1460
+ interface SourceAgentResult {
1461
+ /** Source ID */
1462
+ sourceId: string;
1463
+ /** Source name */
1464
+ sourceName: string;
1465
+ /** Whether the query succeeded */
1466
+ success: boolean;
1467
+ /** Result data rows */
1468
+ data: any[];
1469
+ /** Metadata about the query execution */
1470
+ metadata: SourceAgentMetadata;
1471
+ /** Tool execution info for the last successful query (backward compat) */
1472
+ executedTool: ExecutedToolInfo;
1473
+ /** All successful tool executions (primary + follow-up queries) */
1474
+ allExecutedTools?: ExecutedToolInfo[];
1475
+ /** Error message if failed */
1476
+ error?: string;
1477
+ }
1478
+ interface SourceAgentMetadata {
1479
+ /** Total rows that matched the query (before limit) */
1480
+ totalRowsMatched: number;
1481
+ /** Rows actually returned (after limit) */
1482
+ rowsReturned: number;
1483
+ /** Whether the result was truncated by the row limit */
1484
+ isLimited: boolean;
1485
+ /** The query/params that were executed */
1486
+ queryExecuted?: string;
1487
+ /** Execution time in milliseconds */
1488
+ executionTimeMs: number;
1489
+ }
1490
+ /**
1491
+ * A pre-built, multi-step UI flow registered with the SDK.
1492
+ *
1493
+ * When the main agent decides a user's question matches a workflow's whenToUse
1494
+ * trigger, it picks the workflow instead of running source agents / generating
1495
+ * dashboard components. The LLM extracts the workflow's required props from the
1496
+ * prompt (using `propsSchema` as the tool input_schema) and the SDK returns the
1497
+ * workflow component directly — no analysis text, no chart generation. The
1498
+ * frontend renders the registered workflow component with the LLM-extracted
1499
+ * props.
1500
+ */
1501
+ interface WorkflowDescriptor {
1502
+ /** Unique workflow id (used as the LLM tool name) */
1503
+ id: string;
1504
+ /** Component name on the frontend (matches the registered React component) */
1505
+ name: string;
1506
+ /** Short human-readable description of what this workflow does */
1507
+ description: string;
1508
+ /**
1509
+ * 1–2 sentence trigger condition. The LLM uses this to decide if the
1510
+ * user's prompt matches this workflow. Be specific — e.g.
1511
+ * "User wants to *initiate* an inventory transfer (review + submit POs),
1512
+ * not just see analysis or charts."
1513
+ */
1514
+ whenToUse: string;
1515
+ /**
1516
+ * JSON-schema-style description of the props the workflow needs. Becomes
1517
+ * the LLM tool's input_schema, so the model fills these from the prompt.
1518
+ * Use the same shape as `params` on direct tools — string descriptors with
1519
+ * an optional "(optional)" suffix.
1520
+ *
1521
+ * Example:
1522
+ * ```
1523
+ * {
1524
+ * selectedStore: 'object — { id, name } of the source branch',
1525
+ * minROI: 'number (optional) — only show transfers with ROI ≥ this',
1526
+ * }
1527
+ * ```
1528
+ */
1529
+ propsSchema: Record<string, string>;
1530
+ /**
1531
+ * Optional: static prop defaults merged with LLM-extracted props before
1532
+ * the component is returned. Useful for things like the embedded
1533
+ * `externalTool` config that the workflow uses to fetch its own data.
1534
+ */
1535
+ defaultProps?: Record<string, any>;
1536
+ }
1537
+ /**
1538
+ * The workflow selection captured during a routing call.
1539
+ * Set on AgentResponse when the LLM picks a workflow tool.
1540
+ */
1541
+ interface SelectedWorkflow {
1542
+ /** Component name (matches WorkflowDescriptor.name) */
1543
+ name: string;
1544
+ /** Props extracted from the prompt + merged with workflow.defaultProps */
1545
+ props: Record<string, any>;
1546
+ }
1547
+ /**
1548
+ * The complete response from the multi-agent system.
1549
+ * Contains everything needed for text display + component generation.
1550
+ */
1551
+ interface AgentResponse {
1552
+ /** Generated text response (analysis of the data) */
1553
+ text: string;
1554
+ /** All executed tools across all source agents (for component generation) */
1555
+ executedTools: ExecutedToolInfo[];
1556
+ /** Individual results from each source agent */
1557
+ sourceResults: SourceAgentResult[];
1558
+ /**
1559
+ * Populated when MainAgent wrote AND successfully executed a script during its turn.
1560
+ * Caller (agent-user-response.ts) persists it via ScriptStore.save().
1561
+ * Absent when MainAgent didn't write one (trivial question / all attempts failed).
1562
+ */
1563
+ savedScript?: AgentWrittenScript;
1564
+ /**
1565
+ * Set when the LLM routed the question to a registered workflow component.
1566
+ * When present, the upstream caller should skip component generation and
1567
+ * return this workflow as the response.
1568
+ */
1569
+ workflow?: SelectedWorkflow;
1570
+ }
1571
+ /**
1572
+ * A script MainAgent authored + verified during its turn. Shape aligns with
1573
+ * what ScriptStore.save() needs — minus store-assigned fields (id, timestamps, counts).
1574
+ */
1575
+ interface AgentWrittenScript {
1576
+ /**
1577
+ * `ScriptRecipe.id` of the draft that was authored + verified during this turn.
1578
+ * The caller passes this to `ScriptStore.promoteToVerified(recipeId, …)` to
1579
+ * flip the draft to verified status and (when possible) drop the turn-suffix
1580
+ * from its filename.
1581
+ */
1582
+ recipeId: string;
1583
+ name: string;
1584
+ intentDescription: string;
1585
+ tags: string[];
1586
+ parameters: Array<{
1587
+ name: string;
1588
+ type: 'string' | 'number' | 'date' | 'date_range' | 'enum' | 'boolean';
1589
+ required: boolean;
1590
+ default?: any;
1591
+ enumValues?: Record<string, string>;
1592
+ description: string;
1593
+ }>;
1594
+ scriptBody: string;
1595
+ /** Source IDs referenced by the script (extracted from ctx.query calls) */
1596
+ sourceIds: string[];
1597
+ /** Tables referenced in the script's SQL (regex-extracted) */
1598
+ tables: string[];
1599
+ /** Executed queries from the verified run — fed to component generation */
1600
+ executedQueries: Array<{
1601
+ sourceId: string;
1602
+ sourceName: string;
1603
+ sql: string;
1604
+ data: any[];
1605
+ count: number;
1606
+ totalCount?: number;
1607
+ executionTimeMs: number;
1608
+ /**
1609
+ * True for synthetic entries (ctx.emit datasets, the computed:_final
1610
+ * post-JS data). The component generator routes virtual sources through
1611
+ * the script_dataset sentinel toolId so the frontend resolves them via
1612
+ * queryCache instead of attempting to re-execute SQL.
1613
+ */
1614
+ virtual?: boolean;
1615
+ }>;
1616
+ }
1617
+ /**
1618
+ * Configuration for the multi-agent system.
1619
+ * Controls limits, models, and behavior.
1620
+ */
1621
+ interface AgentConfig {
1622
+ /** Max rows a source agent can return (default: 50) */
1623
+ maxRowsPerSource: number;
1624
+ /** Model for the main agent (routing + analysis in one LLM call) */
1625
+ mainAgentModel: string;
1626
+ /** Model for source agent query generation */
1627
+ sourceAgentModel: string;
1628
+ /** API key for LLM calls */
1629
+ apiKey?: string;
1630
+ /** Max retry attempts per source agent */
1631
+ maxRetries: number;
1632
+ /** Max tool calling iterations for the main agent loop */
1633
+ maxIterations: number;
1634
+ /** Global knowledge base context (static, same for all users/questions — cached in system prompt) */
1635
+ globalKnowledgeBase?: string;
1636
+ /** Per-request knowledge base context (user-specific + query-matched — dynamic, not cached) */
1637
+ knowledgeBaseContext?: string;
1638
+ /** Collections registry (ChromaDB search hooks) for embedding-based schema + source search */
1639
+ collections?: any;
1640
+ /** Optional project ID for scoping embedding searches */
1641
+ projectId?: string;
1642
+ }
1643
+ /**
1644
+ * Default agent configuration
1645
+ */
1646
+ declare const DEFAULT_AGENT_CONFIG: AgentConfig;
1647
+
1648
+ /**
1649
+ * Script Flow Types
1650
+ *
1651
+ * Defines interfaces for the script-based query architecture:
1652
+ * - ScriptRecipe: metadata for matching, validation, and quality tracking
1653
+ * - ScriptResult: output from executing a script
1654
+ * - ScriptMatch: result from the LLM-based script matcher
1655
+ */
1656
+ /**
1657
+ * Recipe metadata stored alongside each script.
1658
+ * Used for matching, validation, and quality tracking.
1659
+ */
1660
+ interface ScriptRecipe {
1661
+ /** Unique script identifier */
1662
+ id: string;
1663
+ /** Version number (incremented on regeneration) */
1664
+ version: number;
1665
+ /** Human-readable name (e.g., "Revenue by Dimension") */
1666
+ name: string;
1667
+ /** Natural language description of what this script does */
1668
+ intentDescription: string;
1669
+ /** Keyword tags for quick filtering */
1670
+ tags: string[];
1671
+ /** Source tool IDs this script queries (e.g., ["mssql-abc123_query"]) */
1672
+ sourceIds: string[];
1673
+ /** Table names used (for future schema drift detection) */
1674
+ tables: string[];
1675
+ /** Parameter definitions — what can vary */
1676
+ parameters: ScriptParameter[];
1677
+ /** The script function body as a string. Loaded from disk (scripts-store/<fileBase>.ts). */
1678
+ scriptBody: string;
1679
+ /**
1680
+ * On-disk filename stem for the body: scripts-store/<fileBase>.ts.
1681
+ * Editable in the IDE. Decided at authoring time (slug of `name`, with a
1682
+ * short id suffix on collision) and stable across promotion.
1683
+ */
1684
+ fileBase?: string;
1685
+ /** sha256 of the on-disk body — lets the runtime detect manual edits. */
1686
+ bodyHash?: string;
1687
+ /** Project scope (single-VM deployments may leave this undefined). */
1688
+ projectId?: string;
1689
+ /** Times this script was used successfully */
1690
+ successCount: number;
1691
+ /** Times this script failed */
1692
+ failureCount: number;
1693
+ /** ISO timestamp of last usage */
1694
+ lastUsed: string;
1695
+ /** Original user question that created this script */
1696
+ createdFrom: string;
1697
+ /** ISO timestamp */
1698
+ createdAt: string;
1699
+ /** ISO timestamp */
1700
+ updatedAt: string;
1701
+ /**
1702
+ * `recipe.id` of the parent this script was forked from.
1703
+ * Undefined for root scripts (those written from scratch by MainAgent).
1704
+ * See backend/docs/SCRIPT-FLOW-FORK-ADAPT.md.
1705
+ */
1706
+ parentId?: string;
1707
+ /** 0 for root scripts; `parent.forkDepth + 1` for forks. Capped at 3. */
1708
+ forkDepth?: number;
1709
+ /**
1710
+ * Brief description of what this fork changed vs its parent
1711
+ * (sourced from the matcher's `modificationHint`).
1712
+ */
1713
+ forkReason?: string;
1714
+ /**
1715
+ * Validated component specs captured at authoring time. On a tier-high
1716
+ * replay these are rebound to fresh queryIds deterministically — no
1717
+ * component-generation LLM call, and the rendered columns can't drift from
1718
+ * what was validated when the script was authored. Absent on recipes
1719
+ * authored before this landed; those fall back to LLM component generation.
1720
+ * See backend/docs/SCRIPT-COMPONENT-CONSISTENCY.md.
1721
+ */
1722
+ components?: ScriptComponentSpec[];
1723
+ /**
1724
+ * Lifecycle stage of this recipe on disk.
1725
+ * - 'draft': written by MainAgent's write_script during a turn; filtered out
1726
+ * of FTS results (status='verified' only) so the matcher never picks it.
1727
+ * Filename is suffixed with `turnId` to keep concurrent turns
1728
+ * from clobbering each other's drafts.
1729
+ * - 'verified': promoted after `execute_script` succeeded; the matcher sees it.
1730
+ * Filename drops the turn suffix unless a verified file with
1731
+ * the same slug already exists (collision case keeps the suffix).
1732
+ *
1733
+ * Recipes loaded from disk without this field default to 'verified' so
1734
+ * existing scripts keep working unchanged.
1735
+ */
1736
+ status?: 'draft' | 'verified';
1737
+ /**
1738
+ * Per-turn unique suffix used for draft filenames (e.g. `1714745623-x9k2`).
1739
+ * Set when the draft is saved; carried until the recipe is promoted.
1740
+ */
1741
+ turnId?: string;
1742
+ /**
1743
+ * Last execution error captured by `recordDraftError` while the recipe was
1744
+ * still a draft. Lets users open the draft .json file and see why it failed
1745
+ * without grepping logs. Cleared on promotion to 'verified'.
1746
+ */
1747
+ lastError?: {
1748
+ phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
1749
+ message: string;
1750
+ at: string;
1751
+ attempt: number;
1752
+ };
1753
+ }
1754
+ interface ScriptParameter {
1755
+ /** Parameter name (used in script body as params.name) */
1756
+ name: string;
1757
+ /** Parameter type */
1758
+ type: 'string' | 'number' | 'date' | 'date_range' | 'enum' | 'boolean';
1759
+ /** Whether this parameter is required */
1760
+ required: boolean;
1761
+ /** Default value if not provided */
1762
+ default?: any;
1763
+ /** For enum type — maps user-facing values to internal values */
1764
+ enumValues?: Record<string, string>;
1765
+ /** Human-readable description (used in the matcher LLM prompt) */
1766
+ description: string;
1767
+ }
1768
+ /**
1769
+ * A reusable component binding captured when a script is authored. Stored on
1770
+ * the recipe so tier-high replays rebuild components deterministically (rebind
1771
+ * to fresh queryIds) instead of re-running the component-picker LLM.
1772
+ */
1773
+ interface ScriptComponentSpec {
1774
+ /** Registered component name (e.g. "DynamicBarChart") — matched against the available component library. */
1775
+ componentType: string;
1776
+ /** `executedQuery.sourceId` to bind to (e.g. a tool id or 'computed:_final'), 'federation' for a cross-source component, or 'markdown' for a content-only narrative block (no data source). */
1777
+ sourceRef: string;
1778
+ /** Present only when sourceRef === 'federation' — the DuckDB SQL to re-execute on replay. */
1779
+ federationSql?: string;
1780
+ /** Present only when sourceRef === 'markdown' — the narrative text to render on replay (markdown has no data source, so its content must be persisted). */
1781
+ content?: string;
1782
+ title?: string;
1783
+ description?: string;
1784
+ /** Validated axis/value keys + aggregation — all referencing real columns of the bound source. */
1785
+ config: Record<string, any>;
1786
+ }
1787
+ /**
1788
+ * Result from executing a script via ScriptRunner.
1789
+ */
1790
+ interface ScriptResult {
1791
+ /** Whether the script executed successfully */
1792
+ success: boolean;
1793
+ /** Combined data from all queries */
1794
+ data: any[];
1795
+ /** Individual query results tracked during execution */
1796
+ executedQueries: ScriptQueryResult[];
1797
+ /** Error message if failed */
1798
+ error?: string;
1799
+ /**
1800
+ * Where in the lifecycle the error occurred. Lets MainAgent's fix-loop
1801
+ * decide between "rewrite the whole draft" (compile) and "patch the
1802
+ * specific line" (runtime).
1803
+ */
1804
+ errorPhase?: 'compile' | 'runtime' | 'timeout' | 'ipc';
1805
+ /** Total execution time in milliseconds */
1806
+ executionTimeMs: number;
1807
+ }
1808
+ /**
1809
+ * A single query executed during script runtime.
1810
+ * Tracked by ScriptContext for component generation and debugging.
1811
+ */
1812
+ interface ScriptQueryResult {
1813
+ /** Source tool ID */
1814
+ sourceId: string;
1815
+ /** Human-readable source name */
1816
+ sourceName: string;
1817
+ /** The SQL that was executed */
1818
+ sql: string;
1819
+ /** Result data rows */
1820
+ data: any[];
1821
+ /** Number of rows returned */
1822
+ count: number;
1823
+ /** Total rows that matched before limit (if available) */
1824
+ totalCount?: number;
1825
+ /** Query execution time in milliseconds */
1826
+ executionTimeMs: number;
1827
+ /**
1828
+ * True for rows that did NOT come from a real SQL execution — either a
1829
+ * ctx.emit() dataset or the synthesized "computed:_final" entry that
1830
+ * carries the script's post-JS returned data. The component generator
1831
+ * uses this to route the resulting component through the script_dataset
1832
+ * sentinel toolId so the frontend resolves it via the queryCache short-circuit.
1833
+ */
1834
+ virtual?: boolean;
1835
+ }
1836
+ /**
1837
+ * Match tier returned by the LLM script matcher.
1838
+ *
1839
+ * - 'high': the script answers the question directly; only parameter values
1840
+ * may differ. The runtime replays it with extracted params (cheapest path).
1841
+ * - 'near': the script answers a STRUCTURALLY similar question but needs
1842
+ * body modification (different metric, dimension, table, filter shape).
1843
+ * The runtime forks the parent and adapts the body via MainAgent's normal
1844
+ * write_script + execute_script loop — no SourceAgent dispatch needed.
1845
+ * See backend/docs/SCRIPT-FLOW-FORK-ADAPT.md for the full design.
1846
+ * - 'none': no script is relevant; full agent flow runs.
1847
+ */
1848
+ type MatchTier = 'high' | 'near' | 'none';
1849
+ /**
1850
+ * Result from the LLM-based script matcher.
1851
+ *
1852
+ * For `tier: 'high'`, `extractedParams` carries the values to pass to the
1853
+ * existing script. For `tier: 'near'`, `gaps` and `modificationHint` describe
1854
+ * what the fork-author needs to change in the parent body.
1855
+ */
1856
+ interface ScriptMatch {
1857
+ /** The matched script recipe */
1858
+ recipe: ScriptRecipe;
1859
+ /** Match tier — see MatchTier docs */
1860
+ tier: MatchTier;
1861
+ /** Similarity score (0-1, derived from LLM tier) */
1862
+ similarity: number;
1863
+ /**
1864
+ * Legacy confidence level. Mirrors `tier === 'high'`/`'near'` for now;
1865
+ * kept so existing callers compile while we migrate to tier-based logic.
1866
+ */
1867
+ confidence: 'high' | 'medium';
1868
+ /** Parameters extracted from the user question by the LLM (tier='high') */
1869
+ extractedParams?: Record<string, any>;
1870
+ /** What the user question needs that the parent doesn't cover (tier='near') */
1871
+ gaps?: string[];
1872
+ /** One-sentence description of the change the fork-author should make (tier='near') */
1873
+ modificationHint?: string;
1874
+ /** Why the matcher made this choice (for logs and telemetry) */
1875
+ reasoning?: string;
1876
+ }
1877
+
1878
+ /**
1879
+ * ScriptRecipeStore — injected metadata backend for the script flow.
1880
+ *
1881
+ * The SDK is standalone (no DB dependency). The backend implements this
1882
+ * interface over Postgres (full-text search + atomic counters) and injects it
1883
+ * via `collections['script-recipes']`, exactly like `collections['source-embeddings']`.
1884
+ * `ScriptStore` consumes it for all METADATA operations while keeping the
1885
+ * executable body on disk as scripts-store/<fileBase>.ts.
1886
+ *
1887
+ * All metadata rows are plain JSON (no scriptBody — that lives on disk).
1888
+ * See backend/docs/SCRIPT-FLOW-SCALING-ISSUES.md (#1, #3, #7).
1889
+ */
1890
+
1891
+ /** One recipe's metadata as stored in Postgres (mirrors the script_recipes table). */
1892
+ interface ScriptRecipeMetaRow {
1893
+ id: string;
1894
+ projectId?: string | null;
1895
+ version: number;
1896
+ name: string;
1897
+ intentDescription: string;
1898
+ tags: string[] | null;
1899
+ createdFrom: string | null;
1900
+ sourceIds: string[] | null;
1901
+ tables: string[] | null;
1902
+ parameters: ScriptParameter[] | null;
1903
+ components?: ScriptComponentSpec[] | null;
1904
+ fileBase: string;
1905
+ bodyHash?: string | null;
1906
+ successCount: number;
1907
+ failureCount: number;
1908
+ lastUsed: string | null;
1909
+ parentId?: string | null;
1910
+ forkDepth?: number | null;
1911
+ forkReason?: string | null;
1912
+ status: 'draft' | 'verified' | string;
1913
+ turnId?: string | null;
1914
+ lastError?: {
1915
+ phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
1916
+ message: string;
1917
+ at: string;
1918
+ attempt: number;
1919
+ } | null;
1920
+ createdAt?: string | null;
1921
+ updatedAt?: string | null;
1922
+ }
1923
+ interface ScriptRecipeStore {
1924
+ /** FTS shortlist of healthy verified recipes for the matcher (metadata only). */
1925
+ search(params: {
1926
+ prompt: string;
1927
+ projectId?: string;
1928
+ limit?: number;
1929
+ }): Promise<ScriptRecipeMetaRow[]>;
1930
+ /** Fetch one recipe by id (any status). */
1931
+ getById(id: string): Promise<ScriptRecipeMetaRow | null>;
1932
+ /** Count healthy verified recipes (drives the "any scripts?" gate). */
1933
+ count(params?: {
1934
+ projectId?: string;
1935
+ }): Promise<number>;
1936
+ /** Insert or update a recipe row (keyed by id). */
1937
+ upsert(row: ScriptRecipeMetaRow): Promise<void>;
1938
+ /** Atomically bump counters / last-used. */
1939
+ updateStats(id: string, patch: {
1940
+ successDelta?: number;
1941
+ failureDelta?: number;
1942
+ lastUsed?: string;
1943
+ }): Promise<void>;
1944
+ /** Flip a draft to verified, applying provenance + optional fork lineage. */
1945
+ promote(id: string, patch: {
1946
+ sourceIds: string[];
1947
+ tables: string[];
1948
+ fileBase?: string;
1949
+ parentId?: string;
1950
+ forkDepth?: number;
1951
+ forkReason?: string;
1952
+ components?: ScriptComponentSpec[];
1953
+ }): Promise<ScriptRecipeMetaRow | null>;
1954
+ /** Stamp a draft's last execution error. */
1955
+ recordDraftError(id: string, err: {
1956
+ phase: string;
1957
+ message: string;
1958
+ attempt: number;
1959
+ at: string;
1960
+ }): Promise<void>;
1961
+ /** Delete a recipe row (body file removed separately). */
1962
+ remove(id: string): Promise<void>;
1963
+ /** True if `fileBase` is taken by a different recipe in this project. */
1964
+ fileBaseTaken(fileBase: string, excludeId: string, projectId?: string): Promise<boolean>;
1965
+ }
1966
+ /** Pull the injected store off the collections bag (or null if not wired). */
1967
+ declare function resolveScriptRecipeStore(collections: any): ScriptRecipeStore | null;
1968
+
1969
+ /**
1970
+ * ScriptStore — Postgres metadata + on-disk body for script recipes.
1971
+ *
1972
+ * Split of responsibilities:
1973
+ * - METADATA → injected `ScriptRecipeStore` (Postgres FTS + atomic counters),
1974
+ * resolved from `collections['script-recipes']`.
1975
+ * - BODY → scripts-store/<fileBase>.ts, editable in your IDE. Written
1976
+ * atomically (temp + rename); `bodyHash` (sha256) detects edits.
1977
+ *
1978
+ * The old "read every file every turn + send the whole catalog to the LLM"
1979
+ * matcher is gone — matching is `store.search(prompt)` (FTS shortlist). The
1980
+ * draft/verified filename dance is gone too: `status` is a DB column and the
1981
+ * file keeps a stable `<fileBase>.ts` name across promotion.
1982
+ *
1983
+ * When no metadata store is injected, the store degrades to a safe no-op
1984
+ * (count 0 → script flow disabled) instead of crashing.
1985
+ *
1986
+ * See backend/docs/SCRIPT-FLOW-SCALING-ISSUES.md.
1987
+ */
1988
+
1989
+ interface SaveDraftInput {
1990
+ /** Reuse an existing draft (retry); omit to mint a new one. */
1991
+ recipeId?: string;
1992
+ /** Per-turn unique suffix, stable across retries within the turn. */
1993
+ turnId: string;
1994
+ name: string;
1995
+ intentDescription: string;
1996
+ tags: string[];
1997
+ parameters: ScriptParameter[];
1998
+ scriptBody: string;
1999
+ createdFrom: string;
2000
+ }
2001
+ interface PromoteToVerifiedInput {
2002
+ sourceIds: string[];
2003
+ tables: string[];
2004
+ parentId?: string;
2005
+ forkDepth?: number;
2006
+ forkReason?: string;
2007
+ components?: ScriptComponentSpec[];
2008
+ }
2009
+ interface ScriptStoreOptions {
2010
+ /** Explicit metadata store, or resolved from `collections['script-recipes']`. */
2011
+ store?: ScriptRecipeStore | null;
2012
+ collections?: any;
2013
+ /** Body directory (defaults to <cwd>/scripts-store). */
2014
+ baseDir?: string;
2015
+ /** Project scope stamped on every row. */
2016
+ projectId?: string;
2017
+ }
2018
+ /**
2019
+ * Normalize a scriptBody into the on-disk form (strip a leading comment block,
2020
+ * ensure `export async function getData`). Exported for MainAgent.
2021
+ */
2022
+ declare function normalizeScriptBody(scriptBody: string): string;
2023
+ declare class ScriptStore {
2024
+ private store;
2025
+ private storeDir;
2026
+ private projectId?;
2027
+ constructor(opts?: ScriptStoreOptions);
2028
+ /** Whether a metadata store is wired (matcher / authoring are gated on this). */
2029
+ hasStore(): boolean;
2030
+ /** Number of healthy verified recipes (gates the script-matching path). */
2031
+ count(): Promise<number>;
2032
+ /**
2033
+ * FTS shortlist for the matcher (metadata only — bodies are loaded lazily by
2034
+ * `get()` once the LLM picks one). Returns verified, healthy recipes ranked
2035
+ * by relevance.
2036
+ */
2037
+ search(prompt: string, limit?: number): Promise<ScriptRecipe[]>;
2038
+ /** Fetch one recipe by id with its body loaded from disk. */
2039
+ get(id: string): Promise<ScriptRecipe | null>;
2040
+ /** Create or update a recipe (metadata upsert + body write when changed). */
2041
+ save(recipe: ScriptRecipe): Promise<void>;
2042
+ /**
2043
+ * Persist (or update) a draft. Within a turn, retries that pass the same
2044
+ * `recipeId` overwrite the same row + file; a fresh `recipeId` mints a new
2045
+ * draft. The body is visible at scripts-store/<fileBase>.ts immediately.
2046
+ */
2047
+ saveDraft(input: SaveDraftInput): Promise<ScriptRecipe>;
2048
+ /** Stamp a draft's last execution error (metadata only). */
2049
+ recordDraftError(recipeId: string, err: {
2050
+ phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
2051
+ message: string;
2052
+ attempt: number;
2053
+ }): Promise<void>;
2054
+ /**
2055
+ * Promote a successfully-executed draft into a verified script.
2056
+ * The on-disk body already exists at <fileBase>.ts (written at write_script
2057
+ * time) and keeps its name — only the DB row flips status + provenance.
2058
+ */
2059
+ promoteToVerified(recipeId: string, input: PromoteToVerifiedInput): Promise<ScriptRecipe | null>;
2060
+ /**
2061
+ * Drop a draft (row + body file). MainAgent calls this at end-of-turn when a
2062
+ * draft was authored but never verified — failed drafts are never matched, so
2063
+ * deleting them immediately avoids unbounded accumulation (#5). No-op if the
2064
+ * recipe isn't a draft (so a promoted/verified script is never removed here).
2065
+ */
2066
+ discardDraft(recipeId: string): Promise<void>;
2067
+ /** Delete a recipe (row + body file). */
2068
+ delete(id: string): Promise<void>;
2069
+ /** Record a successful execution (atomic counter bump). */
2070
+ recordSuccess(id: string): Promise<void>;
2071
+ /** Record a failed execution (atomic counter bump). */
2072
+ recordFailure(id: string): Promise<void>;
2073
+ /** Absolute path to the .ts body for a recipe (used by the runner/MainAgent). */
2074
+ getScriptPath(recipe: ScriptRecipe): string;
2075
+ private removeById;
2076
+ private rowToRecipe;
2077
+ private recipeToRow;
2078
+ /** slug of name, with a short id suffix when the bare slug is already taken. */
2079
+ private computeFileBase;
2080
+ private toSlug;
2081
+ private hash;
2082
+ private bodyPath;
2083
+ private readBody;
2084
+ /** Atomic body write (temp + rename) so concurrent reads never see a partial file. */
2085
+ private writeBody;
2086
+ private unlinkBody;
2087
+ }
2088
+
2089
+ /**
2090
+ * Main Agent (Orchestrator)
2091
+ *
2092
+ * A single LLM.streamWithTools() call that handles everything:
2093
+ * - Routing: decides which source(s) to query based on summaries
2094
+ * - Querying: calls source tools (each wraps an independent SourceAgent)
2095
+ * - Direct tools: calls pre-built function tools directly with LLM-provided params
2096
+ * - Re-querying: if data is wrong/incomplete, calls tools again with modified intent
2097
+ * - Analysis: generates final text response from the data
2098
+ *
2099
+ * Two tool types:
2100
+ * - "source" tools: main agent sees summaries, SourceAgent handles SQL generation independently
2101
+ * - "direct" tools: main agent calls fn() directly with structured params (no SourceAgent)
2102
+ */
2103
+
2104
+ declare class MainAgent {
2105
+ private externalTools;
2106
+ private workflows;
2107
+ private config;
2108
+ private streamBuffer;
2109
+ /**
2110
+ * Optional: when provided, MainAgent exposes the `write_script` /
2111
+ * `execute_script` tools to the LLM and persists drafts to disk via the
2112
+ * store. Headless callers (alert analyzer, metric resolver) omit these to
2113
+ * suppress script authoring entirely — drafts would otherwise leak onto
2114
+ * disk with no caller to promote or clean them up.
2115
+ */
2116
+ private scriptStore;
2117
+ private turnId;
2118
+ private createdFromPrompt;
2119
+ private scriptState;
2120
+ constructor(externalTools: ExternalTool[], config: AgentConfig, scriptStore?: ScriptStore, turnId?: string, streamBuffer?: StreamBuffer, workflows?: WorkflowDescriptor[]);
2121
+ private get scriptingEnabled();
2122
+ /**
2123
+ * Handle a user question using the multi-agent system.
2124
+ *
2125
+ * This is ONE LLM.streamWithTools() call. The LLM:
2126
+ * 1. Sees source summaries + direct tool descriptions in system prompt
2127
+ * 2. Decides which tool(s) to call (routing)
2128
+ * 3. Source tools → SourceAgent runs independently → returns data
2129
+ * 4. Direct tools → fn() called directly with LLM params → returns data
2130
+ * 5. Generates final analysis text
2131
+ */
2132
+ handleQuestion(userPrompt: string, apiKey?: string, conversationHistory?: string, streamCallback?: (chunk: string) => void): Promise<AgentResponse>;
2133
+ private handleWriteScript;
2134
+ private handleExecuteScript;
2135
+ /**
2136
+ * Build the AgentWrittenScript payload the caller will hand to
2137
+ * `ScriptStore.promoteToVerified()`. Only returned when a verified
2138
+ * successful execution is on record.
2139
+ */
2140
+ private buildSavedScript;
2141
+ private normalizeParameterList;
2142
+ /**
2143
+ * Use the schema embedding collection to pre-select relevant tables for
2144
+ * this source + intent. Returns a formatted schema block if confidence is
2145
+ * high (top match ≥ 0.55 and ≥3 candidates), otherwise null.
2146
+ *
2147
+ * When this returns a block, we can skip the SourceAgent's `search_schema`
2148
+ * loop and reduce iteration budget. When it returns null, the SourceAgent
2149
+ * falls back to the existing LLM-driven keyword search (same as today).
2150
+ */
2151
+ private preResolveSchema;
2152
+ /**
2153
+ * Execute a direct tool — call fn() with LLM-provided params, no SourceAgent.
2154
+ */
2155
+ private handleDirectTool;
2156
+ /**
2157
+ * Build the main agent's system prompt with source summaries, direct tool descriptions,
2158
+ * and workflow component descriptions.
2159
+ */
2160
+ private buildSystemPrompt;
2161
+ /**
2162
+ * Build tool definitions for source tools — summary-only descriptions.
2163
+ * The full schema is inside the SourceAgent which runs independently.
2164
+ */
2165
+ private buildSourceToolDefinitions;
2166
+ /**
2167
+ * Build tool definitions for direct tools — expose their actual params.
2168
+ * These are called directly by the main agent LLM, no SourceAgent.
2169
+ */
2170
+ private buildDirectToolDefinitions;
2171
+ /**
2172
+ * Capture a workflow selection. We do NOT execute anything — the LLM has
2173
+ * already extracted the props it wants the workflow rendered with. We
2174
+ * record the selection (via the capture callback) and return a short
2175
+ * acknowledgement so the LLM ends its turn cleanly without writing
2176
+ * analysis text or calling more tools.
2177
+ */
2178
+ private handleWorkflow;
2179
+ /**
2180
+ * Build LLM tool definitions for workflow components. The workflow's
2181
+ * propsSchema becomes the tool's input_schema so the LLM extracts props
2182
+ * directly from the prompt — same mechanic as direct tools.
2183
+ */
2184
+ private buildWorkflowToolDefinitions;
2185
+ /**
2186
+ * Format a source agent's result as a clean string for the main agent LLM.
2187
+ */
2188
+ private formatResultForMainAgent;
2189
+ /**
2190
+ * Get source summaries (for external inspection/debugging).
2191
+ */
2192
+ getSourceSummaries(): SourceSummary[];
2193
+ }
2194
+
2195
+ /**
2196
+ * Represents an action that can be performed on a UIBlock
2197
+ */
2198
+ interface Action {
2199
+ id: string;
2200
+ name: string;
2201
+ type: string;
2202
+ [key: string]: any;
2203
+ }
2204
+
2205
+ type SystemPrompt = string | Anthropic.Messages.TextBlockParam[];
2206
+ interface LLMMessages {
2207
+ sys: SystemPrompt;
2208
+ user: string;
2209
+ prefill?: string;
2210
+ }
2211
+ interface LLMOptions {
2212
+ model?: string;
2213
+ maxTokens?: number;
2214
+ temperature?: number;
2215
+ topP?: number;
2216
+ apiKey?: string;
2217
+ partial?: (chunk: string) => void;
2218
+ }
2219
+ interface Tool {
2220
+ name: string;
2221
+ description: string;
2222
+ input_schema: {
2223
+ type: string;
2224
+ properties: Record<string, any>;
2225
+ required?: string[];
2226
+ };
2227
+ }
2228
+ declare class LLM {
2229
+ static text(messages: LLMMessages, options?: LLMOptions): Promise<string>;
2230
+ static stream<T = string>(messages: LLMMessages, options?: LLMOptions, json?: boolean): Promise<T extends string ? string : any>;
2231
+ static streamWithTools(messages: LLMMessages, tools: Tool[], toolHandler: (toolName: string, toolInput: any) => Promise<any>, options?: LLMOptions, maxIterations?: number): Promise<string>;
2232
+ /**
2233
+ * Normalize system prompt to Anthropic format
2234
+ * Converts string to array format if needed
2235
+ * @param sys - System prompt (string or array of blocks)
2236
+ * @returns Normalized system prompt for Anthropic API
2237
+ */
2238
+ private static _normalizeSystemPrompt;
2239
+ /**
2240
+ * Strip unpaired UTF-16 surrogates from every text field of a message set.
2241
+ *
2242
+ * A lone surrogate (from mid-pair string slicing or corrupt source data)
2243
+ * serializes to a bare `\udXXX` escape that strict JSON parsers — including
2244
+ * the one on Anthropic's API — reject with "no low surrogate in string",
2245
+ * failing the whole request. Sanitizing here, at the single boundary every
2246
+ * provider call flows through, guarantees no request can carry one.
2247
+ */
2248
+ private static _sanitizeMessages;
2249
+ /**
2250
+ * Log cache usage metrics from Anthropic API response
2251
+ * Shows cache hits, costs, and savings
2252
+ */
2253
+ private static _logCacheUsage;
2254
+ /**
2255
+ * Parse model string to extract provider and model name
2256
+ * @param modelString - Format: "provider/model-name" or just "model-name"
2257
+ * @returns [provider, modelName]
2258
+ *
2259
+ * @example
2260
+ * "anthropic/claude-sonnet-4-5" → ["anthropic", "claude-sonnet-4-5"]
2261
+ * "groq/openai/gpt-oss-120b" → ["groq", "openai/gpt-oss-120b"]
2262
+ * "claude-sonnet-4-5" → ["anthropic", "claude-sonnet-4-5"] (default)
2263
+ */
2264
+ private static _parseModel;
2265
+ private static _anthropicText;
2266
+ private static _anthropicStream;
2267
+ private static _anthropicStreamWithTools;
2268
+ private static _groqText;
2269
+ private static _groqStream;
2270
+ private static _geminiText;
2271
+ private static _geminiStream;
2272
+ /**
2273
+ * Recursively strip unsupported JSON Schema properties for Gemini
2274
+ * Gemini doesn't support: additionalProperties, $schema, etc.
2275
+ */
2276
+ private static _cleanSchemaForGemini;
2277
+ private static _geminiStreamWithTools;
2278
+ private static _openaiText;
2279
+ private static _openaiStream;
2280
+ private static _openaiStreamWithTools;
2281
+ /**
2282
+ * Parse JSON string, handling markdown code blocks and surrounding text
2283
+ * Enhanced version with jsonrepair to handle malformed JSON from LLMs
2284
+ * @param text - Text that may contain JSON wrapped in ```json...``` or with surrounding text
2285
+ * @returns Parsed JSON object or array
2286
+ */
2287
+ private static _parseJSON;
2288
+ }
2289
+
2290
+ interface CapturedLog {
2291
+ timestamp: number;
2292
+ level: 'info' | 'error' | 'warn' | 'debug';
2293
+ message: string;
2294
+ type?: 'explanation' | 'query' | 'general';
2295
+ data?: Record<string, any>;
2296
+ }
2297
+ /**
2298
+ * UILogCollector captures logs during user prompt processing
2299
+ * and sends them to runtime via ui_logs message with uiBlockId as the message id
2300
+ * Logs are sent in real-time for streaming effect in the UI
2301
+ * Respects the global log level configuration
2302
+ */
2303
+ declare class UILogCollector {
2304
+ private logs;
2305
+ private uiBlockId;
2306
+ private clientId;
2307
+ private sendMessage;
2308
+ private currentLogLevel;
2309
+ constructor(clientId: string, sendMessage: (message: Message) => void, uiBlockId?: string);
2310
+ /**
2311
+ * Check if logging is enabled (uiBlockId is provided)
2312
+ */
2313
+ isEnabled(): boolean;
2314
+ /**
2315
+ * Check if a message should be logged based on current log level
2316
+ */
2317
+ private shouldLog;
2318
+ /**
2319
+ * Add a log entry with timestamp and immediately send to runtime
2320
+ * Only logs that pass the log level filter are captured and sent
2321
+ */
2322
+ private addLog;
2323
+ /**
2324
+ * Send a single log to runtime immediately
2325
+ */
2326
+ private sendLogImmediately;
2327
+ /**
2328
+ * Log info message
2329
+ */
2330
+ info(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
2331
+ /**
2332
+ * Log error message
2333
+ */
2334
+ error(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
2335
+ /**
2336
+ * Log warning message
2337
+ */
2338
+ warn(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
2339
+ /**
2340
+ * Log debug message
2341
+ */
2342
+ debug(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
2343
+ /**
2344
+ * Log LLM explanation with typed metadata
2345
+ */
2346
+ logExplanation(message: string, explanation: string, data?: Record<string, any>): void;
2347
+ /**
2348
+ * Log generated query with typed metadata
2349
+ */
2350
+ logQuery(message: string, query: string, data?: Record<string, any>): void;
2351
+ /**
2352
+ * Send all collected logs at once (optional, for final summary)
2353
+ */
2354
+ sendAllLogs(): void;
2355
+ /**
2356
+ * Get all collected logs
2357
+ */
2358
+ getLogs(): CapturedLog[];
2359
+ /**
2360
+ * Clear all logs
2361
+ */
2362
+ clearLogs(): void;
2363
+ /**
2364
+ * Set uiBlockId (in case it's provided later)
2365
+ */
2366
+ setUIBlockId(uiBlockId: string): void;
2367
+ }
2368
+
2369
+ /**
2370
+ * UIBlock represents a single user and assistant message block in a thread
2371
+ * Contains user question, component metadata, component data, text response, and available actions
965
2372
  */
966
2373
  declare class UIBlock {
967
2374
  private id;
@@ -972,343 +2379,1099 @@ declare class UIBlock {
972
2379
  private actions;
973
2380
  private createdAt;
974
2381
  /**
975
- * Creates a new UIBlock instance
976
- * @param userQuestion - The user's question or input
977
- * @param componentData - The component data object
978
- * @param generatedComponentMetadata - Optional metadata about the generated component
979
- * @param actions - Optional array of available actions
2382
+ * Creates a new UIBlock instance
2383
+ * @param userQuestion - The user's question or input
2384
+ * @param componentData - The component data object
2385
+ * @param generatedComponentMetadata - Optional metadata about the generated component
2386
+ * @param actions - Optional array of available actions
2387
+ * @param id - Optional custom ID, generates UUID if not provided
2388
+ * @param textResponse - Optional text response from LLM
2389
+ */
2390
+ constructor(userQuestion: string, componentData?: Record<string, any>, generatedComponentMetadata?: Record<string, any>, actions?: Action[], id?: string, textResponse?: string | null);
2391
+ /**
2392
+ * Get the UIBlock ID
2393
+ */
2394
+ getId(): string;
2395
+ /**
2396
+ * Get the user question
2397
+ */
2398
+ getUserQuestion(): string;
2399
+ /**
2400
+ * Set or update the user question
2401
+ */
2402
+ setUserQuestion(question: string): void;
2403
+ /**
2404
+ * Get component metadata
2405
+ */
2406
+ getComponentMetadata(): Record<string, any>;
2407
+ getTextResponse(): string;
2408
+ /**
2409
+ * Set or update component metadata
2410
+ */
2411
+ setComponentMetadata(metadata: Record<string, any>): void;
2412
+ /**
2413
+ * Get component data
2414
+ */
2415
+ getComponentData(): Record<string, any>;
2416
+ /**
2417
+ * Calculate size of data in bytes
2418
+ */
2419
+ private getDataSizeInBytes;
2420
+ /**
2421
+ * Limit array data to maximum rows
2422
+ */
2423
+ private limitArrayData;
2424
+ /**
2425
+ * Check if data exceeds size limit
2426
+ */
2427
+ private exceedsSizeLimit;
2428
+ /**
2429
+ * Process and limit data before storing
2430
+ */
2431
+ private processDataForStorage;
2432
+ /**
2433
+ * Set or update component data with size and row limits
2434
+ */
2435
+ setComponentData(data: Record<string, any>): void;
2436
+ /**
2437
+ * Set or update text response
2438
+ */
2439
+ setTextResponse(textResponse: string | null): void;
2440
+ /**
2441
+ * Get all actions (only if they are resolved, not if fetching)
2442
+ */
2443
+ getActions(): Action[] | null | Promise<Action[]>;
2444
+ /**
2445
+ * Get or fetch actions
2446
+ * If actions don't exist or are a Promise, calls the generateFn and stores the promise
2447
+ * If actions already exist, returns them
2448
+ * @param generateFn - Async function to generate actions
2449
+ * @returns Promise resolving to Action[]
2450
+ */
2451
+ getOrFetchActions(generateFn: () => Promise<Action[]>): Promise<Action[]>;
2452
+ /**
2453
+ * Set or replace all actions
2454
+ */
2455
+ setActions(actions: Action[]): void;
2456
+ /**
2457
+ * Add a single action (only if actions are resolved)
2458
+ */
2459
+ addAction(action: Action): void;
2460
+ /**
2461
+ * Add multiple actions (only if actions are resolved)
2462
+ */
2463
+ addActions(actions: Action[]): void;
2464
+ /**
2465
+ * Remove an action by ID (only if actions are resolved)
2466
+ */
2467
+ removeAction(actionId: string): boolean;
2468
+ /**
2469
+ * Clear all actions
2470
+ */
2471
+ clearActions(): void;
2472
+ /**
2473
+ * Get creation timestamp
2474
+ */
2475
+ getCreatedAt(): Date;
2476
+ /**
2477
+ * Convert UIBlock to JSON-serializable object
2478
+ */
2479
+ toJSON(): Record<string, any>;
2480
+ }
2481
+
2482
+ /**
2483
+ * Thread represents a conversation thread containing multiple UIBlocks
2484
+ * Each UIBlock in a thread represents a user question and assistant response pair
2485
+ */
2486
+ declare class Thread {
2487
+ private id;
2488
+ private uiblocks;
2489
+ private createdAt;
2490
+ /**
2491
+ * Creates a new Thread instance
2492
+ * @param id - Optional custom ID, generates UUID if not provided
2493
+ */
2494
+ constructor(id?: string);
2495
+ /**
2496
+ * Get the thread ID
2497
+ */
2498
+ getId(): string;
2499
+ /**
2500
+ * Add a UIBlock to the thread
2501
+ */
2502
+ addUIBlock(uiblock: UIBlock): void;
2503
+ /**
2504
+ * Get a UIBlock by ID
2505
+ */
2506
+ getUIBlock(id: string): UIBlock | undefined;
2507
+ /**
2508
+ * Get all UIBlocks in the thread
2509
+ */
2510
+ getUIBlocks(): UIBlock[];
2511
+ /**
2512
+ * Get UIBlocks as a Map
2513
+ */
2514
+ getUIBlocksMap(): Map<string, UIBlock>;
2515
+ /**
2516
+ * Remove a UIBlock by ID
2517
+ */
2518
+ removeUIBlock(id: string): boolean;
2519
+ /**
2520
+ * Check if UIBlock exists
2521
+ */
2522
+ hasUIBlock(id: string): boolean;
2523
+ /**
2524
+ * Get number of UIBlocks in the thread
2525
+ */
2526
+ getUIBlockCount(): number;
2527
+ /**
2528
+ * Clear all UIBlocks from the thread
2529
+ */
2530
+ clear(): void;
2531
+ /**
2532
+ * Get creation timestamp
2533
+ */
2534
+ getCreatedAt(): Date;
2535
+ /**
2536
+ * Get conversation context from recent UIBlocks (excluding current one)
2537
+ * Returns formatted string with previous questions and component summaries
2538
+ * @param limit - Maximum number of previous UIBlocks to include (default: 5)
2539
+ * @param currentUIBlockId - ID of current UIBlock to exclude from context (optional)
2540
+ * @returns Formatted conversation history string
2541
+ */
2542
+ getConversationContext(limit?: number, currentUIBlockId?: string): string;
2543
+ /**
2544
+ * Convert Thread to JSON-serializable object
2545
+ */
2546
+ toJSON(): Record<string, any>;
2547
+ }
2548
+
2549
+ /**
2550
+ * ThreadManager manages all threads globally
2551
+ * Provides methods to create, retrieve, and delete threads.
2552
+ * Includes automatic cleanup to prevent unbounded memory growth.
2553
+ */
2554
+ declare class ThreadManager {
2555
+ private static instance;
2556
+ private threads;
2557
+ private cleanupInterval;
2558
+ private readonly threadTtlMs;
2559
+ private constructor();
2560
+ /**
2561
+ * Periodically remove threads older than 7 days.
2562
+ * Runs every hour to avoid frequent iteration over the map.
2563
+ */
2564
+ private startCleanup;
2565
+ /**
2566
+ * Get singleton instance of ThreadManager
2567
+ */
2568
+ static getInstance(): ThreadManager;
2569
+ /**
2570
+ * Create a new thread
980
2571
  * @param id - Optional custom ID, generates UUID if not provided
981
- * @param textResponse - Optional text response from LLM
2572
+ * @returns The created Thread instance
982
2573
  */
983
- constructor(userQuestion: string, componentData?: Record<string, any>, generatedComponentMetadata?: Record<string, any>, actions?: Action[], id?: string, textResponse?: string | null);
2574
+ createThread(id?: string): Thread;
984
2575
  /**
985
- * Get the UIBlock ID
2576
+ * Get a thread by ID
986
2577
  */
987
- getId(): string;
2578
+ getThread(id: string): Thread | undefined;
988
2579
  /**
989
- * Get the user question
2580
+ * Get all threads
990
2581
  */
991
- getUserQuestion(): string;
2582
+ getAllThreads(): Thread[];
992
2583
  /**
993
- * Set or update the user question
2584
+ * Get threads as a Map
994
2585
  */
995
- setUserQuestion(question: string): void;
2586
+ getThreadsMap(): Map<string, Thread>;
996
2587
  /**
997
- * Get component metadata
2588
+ * Delete a thread by ID
998
2589
  */
999
- getComponentMetadata(): Record<string, any>;
1000
- getTextResponse(): string;
2590
+ deleteThread(id: string): boolean;
1001
2591
  /**
1002
- * Set or update component metadata
2592
+ * Check if thread exists
1003
2593
  */
1004
- setComponentMetadata(metadata: Record<string, any>): void;
2594
+ hasThread(id: string): boolean;
1005
2595
  /**
1006
- * Get component data
2596
+ * Get number of threads
1007
2597
  */
1008
- getComponentData(): Record<string, any>;
2598
+ getThreadCount(): number;
1009
2599
  /**
1010
- * Calculate size of data in bytes
2600
+ * Clear all threads
1011
2601
  */
1012
- private getDataSizeInBytes;
2602
+ clearAll(): void;
1013
2603
  /**
1014
- * Limit array data to maximum rows
2604
+ * Find a UIBlock by ID across all threads
2605
+ * @param uiBlockId - The UIBlock ID to search for
2606
+ * @returns Object with thread and uiBlock if found, undefined otherwise
1015
2607
  */
1016
- private limitArrayData;
2608
+ findUIBlockById(uiBlockId: string): {
2609
+ thread: Thread;
2610
+ uiBlock: UIBlock;
2611
+ } | undefined;
2612
+ /**
2613
+ * Convert all threads to JSON-serializable object
2614
+ */
2615
+ toJSON(): Record<string, any>;
2616
+ }
2617
+
2618
+ /**
2619
+ * CleanupService handles cleanup of old threads and UIBlocks
2620
+ * to prevent memory bloat and maintain optimal performance
2621
+ */
2622
+ declare class CleanupService {
2623
+ private static instance;
2624
+ private cleanupInterval;
2625
+ private constructor();
2626
+ /**
2627
+ * Get singleton instance of CleanupService
2628
+ */
2629
+ static getInstance(): CleanupService;
2630
+ /**
2631
+ * Clean up old threads based on retention period
2632
+ * @param retentionDays - Number of days to keep threads (defaults to config)
2633
+ * @returns Number of threads deleted
2634
+ */
2635
+ cleanupOldThreads(retentionDays?: number): number;
2636
+ /**
2637
+ * Clean up old UIBlocks within threads based on retention period
2638
+ * @param retentionDays - Number of days to keep UIBlocks (defaults to config)
2639
+ * @returns Object with number of UIBlocks deleted per thread
2640
+ */
2641
+ cleanupOldUIBlocks(retentionDays?: number): {
2642
+ [threadId: string]: number;
2643
+ };
2644
+ /**
2645
+ * Clear all component data from UIBlocks to free memory
2646
+ * Keeps metadata but removes the actual data
2647
+ * @param retentionDays - Number of days to keep full data (defaults to config)
2648
+ * @returns Number of UIBlocks whose data was cleared
2649
+ */
2650
+ clearOldUIBlockData(retentionDays?: number): number;
2651
+ /**
2652
+ * Run full cleanup (threads, UIBlocks, and data)
2653
+ * @returns Cleanup statistics
2654
+ */
2655
+ runFullCleanup(): {
2656
+ threadsDeleted: number;
2657
+ uiblocksDeleted: {
2658
+ [threadId: string]: number;
2659
+ };
2660
+ dataCleared: number;
2661
+ };
2662
+ /**
2663
+ * Start automatic cleanup at regular intervals
2664
+ * @param intervalHours - Hours between cleanup runs (default: 24)
2665
+ */
2666
+ startAutoCleanup(intervalHours?: number): void;
2667
+ /**
2668
+ * Stop automatic cleanup
2669
+ */
2670
+ stopAutoCleanup(): void;
2671
+ /**
2672
+ * Check if auto cleanup is running
2673
+ */
2674
+ isAutoCleanupRunning(): boolean;
2675
+ /**
2676
+ * Get current memory usage statistics
2677
+ */
2678
+ getMemoryStats(): {
2679
+ threadCount: number;
2680
+ totalUIBlocks: number;
2681
+ avgUIBlocksPerThread: number;
2682
+ };
2683
+ }
2684
+
2685
+ /**
2686
+ * Configuration for data storage limits in UIBlocks
2687
+ */
2688
+ declare const STORAGE_CONFIG: {
2689
+ /**
2690
+ * Maximum number of rows to store in UIBlock data
2691
+ */
2692
+ MAX_ROWS_PER_BLOCK: number;
2693
+ /**
2694
+ * Maximum size in bytes per UIBlock (500KB - reduced to save memory)
2695
+ */
2696
+ MAX_SIZE_PER_BLOCK_BYTES: number;
2697
+ /**
2698
+ * Number of days to keep threads before cleanup
2699
+ * Note: This is for in-memory storage. Conversations are also persisted to database.
2700
+ */
2701
+ THREAD_RETENTION_DAYS: number;
2702
+ /**
2703
+ * Number of days to keep UIBlocks before cleanup
2704
+ * Note: This is for in-memory storage. Data is also persisted to database.
2705
+ */
2706
+ UIBLOCK_RETENTION_DAYS: number;
2707
+ };
2708
+
2709
+ /**
2710
+ * Configuration for conversation context and history management
2711
+ */
2712
+ declare const CONTEXT_CONFIG: {
2713
+ /**
2714
+ * Maximum number of previous UIBlocks to include as conversation context
2715
+ * Set to 0 to disable conversation history
2716
+ * Higher values provide more context but may increase token usage
2717
+ */
2718
+ MAX_CONVERSATION_CONTEXT_BLOCKS: number;
2719
+ };
2720
+
2721
+ /**
2722
+ * LLM Usage Logger - Tracks token usage, costs, and timing for all LLM API calls
2723
+ */
2724
+ interface LLMUsageEntry {
2725
+ timestamp: string;
2726
+ requestId: string;
2727
+ provider: string;
2728
+ model: string;
2729
+ method: string;
2730
+ inputTokens: number;
2731
+ outputTokens: number;
2732
+ cacheReadTokens?: number;
2733
+ cacheWriteTokens?: number;
2734
+ totalTokens: number;
2735
+ costUSD: number;
2736
+ durationMs: number;
2737
+ toolCalls?: number;
2738
+ success: boolean;
2739
+ error?: string;
2740
+ }
2741
+ declare class LLMUsageLogger {
2742
+ private logStream;
2743
+ private logPath;
2744
+ private enabled;
2745
+ private sessionStats;
2746
+ constructor();
2747
+ private initLogStream;
2748
+ private writeHeader;
2749
+ /**
2750
+ * Calculate cost based on token usage and model
2751
+ */
2752
+ calculateCost(model: string, inputTokens: number, outputTokens: number, cacheReadTokens?: number, cacheWriteTokens?: number): number;
2753
+ /**
2754
+ * Log an LLM API call
2755
+ */
2756
+ log(entry: LLMUsageEntry): void;
2757
+ /**
2758
+ * Log session summary (call at end of request)
2759
+ */
2760
+ logSessionSummary(requestContext?: string): void;
2761
+ /**
2762
+ * Reset session stats (call at start of new user request)
2763
+ */
2764
+ resetSession(): void;
2765
+ /**
2766
+ * Reset the log file for a new request (clears previous logs)
2767
+ * Call this at the start of each USER_PROMPT_REQ
2768
+ */
2769
+ resetLogFile(requestContext?: string): void;
2770
+ /**
2771
+ * Get current session stats
2772
+ */
2773
+ getSessionStats(): {
2774
+ totalCalls: number;
2775
+ totalInputTokens: number;
2776
+ totalOutputTokens: number;
2777
+ totalCacheReadTokens: number;
2778
+ totalCacheWriteTokens: number;
2779
+ totalCostUSD: number;
2780
+ totalDurationMs: number;
2781
+ };
2782
+ /**
2783
+ * Generate a unique request ID
2784
+ */
2785
+ generateRequestId(): string;
2786
+ }
2787
+ declare const llmUsageLogger: LLMUsageLogger;
2788
+
2789
+ /**
2790
+ * User Prompt Error Logger - Captures detailed errors for USER_PROMPT_REQ
2791
+ * Logs full error details including raw strings for parse failures
2792
+ */
2793
+ declare class UserPromptErrorLogger {
2794
+ private logStream;
2795
+ private logPath;
2796
+ private enabled;
2797
+ private hasErrors;
2798
+ constructor();
2799
+ /**
2800
+ * Reset the error log file for a new request
2801
+ */
2802
+ resetLogFile(requestContext?: string): void;
2803
+ /**
2804
+ * Log a JSON parse error with the raw string that failed
2805
+ */
2806
+ logJsonParseError(context: string, rawString: string, error: Error): void;
2807
+ /**
2808
+ * Log a general error with full details
2809
+ */
2810
+ logError(context: string, error: Error | string, additionalData?: Record<string, any>): void;
2811
+ /**
2812
+ * Log a SQL query error with the full query
2813
+ */
2814
+ logSqlError(query: string, error: Error | string, params?: any[]): void;
2815
+ /**
2816
+ * Log an LLM API error
2817
+ */
2818
+ logLlmError(provider: string, model: string, method: string, error: Error | string, requestData?: any): void;
2819
+ /**
2820
+ * Log tool execution error
2821
+ */
2822
+ logToolError(toolName: string, toolInput: any, error: Error | string): void;
2823
+ /**
2824
+ * Write final summary if there were errors
2825
+ */
2826
+ writeSummary(): void;
2827
+ /**
2828
+ * Check if any errors were logged
2829
+ */
2830
+ hadErrors(): boolean;
2831
+ private write;
2832
+ }
2833
+ declare const userPromptErrorLogger: UserPromptErrorLogger;
2834
+
2835
+ /**
2836
+ * BM25L Reranker for hybrid semantic search
2837
+ *
2838
+ * BM25L is an improved variant of BM25 that provides better handling of
2839
+ * long documents and term frequency saturation. This implementation is
2840
+ * designed to rerank semantic search results from ChromaDB.
2841
+ *
2842
+ * The hybrid approach combines:
2843
+ * 1. Semantic similarity from ChromaDB embeddings (dense vectors)
2844
+ * 2. Lexical matching from BM25L (sparse, keyword-based)
2845
+ *
2846
+ * This addresses the weakness of pure semantic search which may miss
2847
+ * exact keyword matches that are important for user intent.
2848
+ */
2849
+ interface BM25LOptions {
2850
+ /** Term frequency saturation parameter (default: 1.5) */
2851
+ k1?: number;
2852
+ /** Length normalization parameter (default: 0.75) */
2853
+ b?: number;
2854
+ /** Lower-bound adjustment from BM25L paper (default: 0.5) */
2855
+ delta?: number;
2856
+ }
2857
+ interface RerankedResult<T> {
2858
+ item: T;
2859
+ originalIndex: number;
2860
+ semanticScore: number;
2861
+ bm25Score: number;
2862
+ hybridScore: number;
2863
+ }
2864
+ interface HybridSearchOptions extends BM25LOptions {
2865
+ /** Weight for semantic score (0-1, default: 0.7) */
2866
+ semanticWeight?: number;
2867
+ /** Weight for BM25 score (0-1, default: 0.3) */
2868
+ bm25Weight?: number;
2869
+ /** Minimum hybrid score threshold (0-1, default: 0) */
2870
+ minScore?: number;
2871
+ }
2872
+ /**
2873
+ * BM25L implementation for lexical scoring
2874
+ */
2875
+ declare class BM25L {
2876
+ private k1;
2877
+ private b;
2878
+ private delta;
2879
+ private documents;
2880
+ private docLengths;
2881
+ private avgDocLength;
2882
+ private termDocFreq;
2883
+ /**
2884
+ * @param documents - Array of raw documents (strings)
2885
+ * @param opts - Optional BM25L parameters
2886
+ */
2887
+ constructor(documents?: string[], opts?: BM25LOptions);
2888
+ /**
2889
+ * Tokenize text into lowercase alphanumeric tokens
2890
+ */
2891
+ tokenize(text: string): string[];
2892
+ /**
2893
+ * Compute IDF (Inverse Document Frequency) with smoothing
2894
+ */
2895
+ private idf;
2896
+ /**
2897
+ * Compute BM25L score for a single document
2898
+ */
2899
+ score(query: string, docIndex: number): number;
2900
+ /**
2901
+ * Search and rank all documents
2902
+ */
2903
+ search(query: string): Array<{
2904
+ index: number;
2905
+ score: number;
2906
+ }>;
2907
+ }
2908
+ /**
2909
+ * Hybrid reranker that combines semantic and BM25L scores
2910
+ *
2911
+ * @param query - The search query
2912
+ * @param items - Array of items to rerank
2913
+ * @param getDocument - Function to extract document text from an item
2914
+ * @param getSemanticScore - Function to extract semantic similarity score from an item
2915
+ * @param options - Hybrid search options
2916
+ * @returns Reranked items with hybrid scores
2917
+ */
2918
+ declare function hybridRerank<T>(query: string, items: T[], getDocument: (item: T) => string, getSemanticScore: (item: T) => number, options?: HybridSearchOptions): RerankedResult<T>[];
2919
+ /**
2920
+ * Simple reranking function for ChromaDB results
2921
+ *
2922
+ * This is a convenience wrapper for reranking ChromaDB query results
2923
+ * that follow the standard { ids, documents, metadatas, distances } format.
2924
+ *
2925
+ * @param query - The search query
2926
+ * @param chromaResults - ChromaDB query results
2927
+ * @param options - Hybrid search options
2928
+ * @returns Reranked results with hybrid scores
2929
+ */
2930
+ declare function rerankChromaResults(query: string, chromaResults: {
2931
+ ids: string[][];
2932
+ documents: (string | null)[][];
2933
+ metadatas: Record<string, any>[][];
2934
+ distances: number[][];
2935
+ }, options?: HybridSearchOptions): Array<{
2936
+ id: string;
2937
+ document: string | null;
2938
+ metadata: Record<string, any>;
2939
+ distance: number;
2940
+ semanticScore: number;
2941
+ bm25Score: number;
2942
+ hybridScore: number;
2943
+ }>;
2944
+ /**
2945
+ * Rerank conversation search results specifically
2946
+ *
2947
+ * This function is designed to work with the conversation-history.search collection
2948
+ * where we need to fetch more results initially and then rerank them.
2949
+ *
2950
+ * @param query - The user's search query
2951
+ * @param results - Array of conversation search results from ChromaDB
2952
+ * @param options - Hybrid search options
2953
+ * @returns Reranked results sorted by hybrid score
2954
+ */
2955
+ declare function rerankConversationResults<T extends {
2956
+ userPrompt?: string;
2957
+ similarity?: number;
2958
+ }>(query: string, results: T[], options?: HybridSearchOptions): Array<T & {
2959
+ hybridScore: number;
2960
+ bm25Score: number;
2961
+ }>;
2962
+
2963
+ /**
2964
+ * QueryExecutionService - Handles all query execution, validation, and retry logic
2965
+ * Extracted from BaseLLM for better separation of concerns
2966
+ */
2967
+
2968
+ /**
2969
+ * Context for component when requesting query fix
2970
+ */
2971
+ interface ComponentContext {
2972
+ name: string;
2973
+ type: string;
2974
+ title?: string;
2975
+ }
2976
+ /**
2977
+ * Result of query validation
2978
+ */
2979
+ interface QueryValidationResult {
2980
+ component: Component | null;
2981
+ queryKey: string;
2982
+ result: any;
2983
+ validated: boolean;
2984
+ }
2985
+ /**
2986
+ * Result of batch query validation
2987
+ */
2988
+ interface BatchValidationResult {
2989
+ components: Component[];
2990
+ queryResults: Map<string, any>;
2991
+ }
2992
+ /**
2993
+ * Configuration for QueryExecutionService
2994
+ */
2995
+ interface QueryExecutionServiceConfig {
2996
+ defaultLimit: number;
2997
+ getModelForTask: (taskType: 'simple' | 'complex') => string;
2998
+ getApiKey: (apiKey?: string) => string | undefined;
2999
+ providerName: string;
3000
+ }
3001
+ /**
3002
+ * QueryExecutionService handles all query-related operations
3003
+ */
3004
+ declare class QueryExecutionService {
3005
+ private config;
3006
+ constructor(config: QueryExecutionServiceConfig);
1017
3007
  /**
1018
- * Check if data exceeds size limit
3008
+ * Get the cache key for a query
3009
+ * This ensures the cache key matches what the frontend will send
1019
3010
  */
1020
- private exceedsSizeLimit;
3011
+ getQueryCacheKey(query: any): string;
1021
3012
  /**
1022
- * Process and limit data before storing
3013
+ * Execute a query against the database
3014
+ * @param query - The SQL query to execute (string or object with sql/values)
3015
+ * @param collections - Collections object containing database execute function
3016
+ * @returns Object with result data and cache key
1023
3017
  */
1024
- private processDataForStorage;
3018
+ executeQuery(query: any, collections: any): Promise<{
3019
+ result: any;
3020
+ cacheKey: string;
3021
+ }>;
1025
3022
  /**
1026
- * Set or update component data with size and row limits
3023
+ * Request the LLM to fix a failed SQL query
3024
+ * @param failedQuery - The query that failed execution
3025
+ * @param errorMessage - The error message from the failed execution
3026
+ * @param componentContext - Context about the component
3027
+ * @param apiKey - Optional API key
3028
+ * @returns Fixed query string
1027
3029
  */
1028
- setComponentData(data: Record<string, any>): void;
3030
+ requestQueryFix(failedQuery: string, errorMessage: string, componentContext: ComponentContext, apiKey?: string): Promise<string>;
1029
3031
  /**
1030
- * Set or update text response
3032
+ * Validate a single component's query with retry logic
3033
+ * @param component - The component to validate
3034
+ * @param collections - Collections object containing database execute function
3035
+ * @param apiKey - Optional API key for LLM calls
3036
+ * @returns Validation result with component, query key, and result
1031
3037
  */
1032
- setTextResponse(textResponse: string | null): void;
3038
+ validateSingleQuery(component: Component, collections: any, apiKey?: string): Promise<QueryValidationResult>;
1033
3039
  /**
1034
- * Get all actions (only if they are resolved, not if fetching)
3040
+ * Validate multiple component queries in parallel
3041
+ * @param components - Array of components with potential queries
3042
+ * @param collections - Collections object containing database execute function
3043
+ * @param apiKey - Optional API key for LLM calls
3044
+ * @returns Object with validated components and query results map
1035
3045
  */
1036
- getActions(): Action[] | null | Promise<Action[]>;
3046
+ validateComponentQueries(components: Component[], collections: any, apiKey?: string): Promise<BatchValidationResult>;
3047
+ }
3048
+
3049
+ /**
3050
+ * Task types for model selection
3051
+ * - 'complex': Text generation, component matching, parameter adaptation (uses best model in balanced mode)
3052
+ * - 'simple': Classification, action generation (uses fast model in balanced mode)
3053
+ */
3054
+ type TaskType = 'complex' | 'simple';
3055
+ interface BaseLLMConfig {
3056
+ model?: string;
3057
+ fastModel?: string;
3058
+ defaultLimit?: number;
3059
+ apiKey?: string;
1037
3060
  /**
1038
- * Get or fetch actions
1039
- * If actions don't exist or are a Promise, calls the generateFn and stores the promise
1040
- * If actions already exist, returns them
1041
- * @param generateFn - Async function to generate actions
1042
- * @returns Promise resolving to Action[]
3061
+ * Model selection strategy:
3062
+ * - 'best': Use best model for all tasks (highest quality, higher cost)
3063
+ * - 'fast': Use fast model for all tasks (lower quality, lower cost)
3064
+ * - 'balanced': Use best model for complex tasks, fast model for simple tasks (default)
1043
3065
  */
1044
- getOrFetchActions(generateFn: () => Promise<Action[]>): Promise<Action[]>;
3066
+ modelStrategy?: ModelStrategy;
3067
+ conversationSimilarityThreshold?: number;
3068
+ }
3069
+ /**
3070
+ * BaseLLM abstract class for AI-powered component generation and matching
3071
+ * Provides common functionality for all LLM providers
3072
+ */
3073
+ declare abstract class BaseLLM {
3074
+ protected model: string;
3075
+ protected fastModel: string;
3076
+ protected defaultLimit: number;
3077
+ protected apiKey?: string;
3078
+ protected modelStrategy: ModelStrategy;
3079
+ protected conversationSimilarityThreshold: number;
3080
+ protected queryService: QueryExecutionService;
3081
+ constructor(config?: BaseLLMConfig);
1045
3082
  /**
1046
- * Set or replace all actions
3083
+ * Get the appropriate model based on task type and model strategy
3084
+ * @param taskType - 'complex' for text generation/matching, 'simple' for classification/actions
3085
+ * @returns The model string to use for this task
1047
3086
  */
1048
- setActions(actions: Action[]): void;
3087
+ protected getModelForTask(taskType: TaskType): string;
1049
3088
  /**
1050
- * Add a single action (only if actions are resolved)
3089
+ * Set the model strategy at runtime
3090
+ * @param strategy - 'best', 'fast', or 'balanced'
1051
3091
  */
1052
- addAction(action: Action): void;
3092
+ setModelStrategy(strategy: ModelStrategy): void;
1053
3093
  /**
1054
- * Add multiple actions (only if actions are resolved)
3094
+ * Get the current model strategy
3095
+ * @returns The current model strategy
1055
3096
  */
1056
- addActions(actions: Action[]): void;
3097
+ getModelStrategy(): ModelStrategy;
1057
3098
  /**
1058
- * Remove an action by ID (only if actions are resolved)
3099
+ * Set the conversation similarity threshold at runtime
3100
+ * @param threshold - Value between 0 and 1 (e.g., 0.8 = 80% similarity required)
1059
3101
  */
1060
- removeAction(actionId: string): boolean;
3102
+ setConversationSimilarityThreshold(threshold: number): void;
1061
3103
  /**
1062
- * Clear all actions
3104
+ * Get the current conversation similarity threshold
3105
+ * @returns The current threshold value
1063
3106
  */
1064
- clearActions(): void;
3107
+ getConversationSimilarityThreshold(): number;
1065
3108
  /**
1066
- * Get creation timestamp
3109
+ * Get the default model for this provider (used for complex tasks like text generation)
1067
3110
  */
1068
- getCreatedAt(): Date;
3111
+ protected abstract getDefaultModel(): string;
1069
3112
  /**
1070
- * Convert UIBlock to JSON-serializable object
3113
+ * Get the default fast model for this provider (used for simple tasks: classification, matching, actions)
3114
+ * Should return a cheaper/faster model like Haiku for Anthropic
1071
3115
  */
1072
- toJSON(): Record<string, any>;
1073
- }
1074
-
1075
- /**
1076
- * Thread represents a conversation thread containing multiple UIBlocks
1077
- * Each UIBlock in a thread represents a user question and assistant response pair
1078
- */
1079
- declare class Thread {
1080
- private id;
1081
- private uiblocks;
1082
- private createdAt;
3116
+ protected abstract getDefaultFastModel(): string;
1083
3117
  /**
1084
- * Creates a new Thread instance
1085
- * @param id - Optional custom ID, generates UUID if not provided
3118
+ * Get the default API key from environment
1086
3119
  */
1087
- constructor(id?: string);
3120
+ protected abstract getDefaultApiKey(): string | undefined;
1088
3121
  /**
1089
- * Get the thread ID
3122
+ * Get the provider name (for logging)
1090
3123
  */
1091
- getId(): string;
3124
+ protected abstract getProviderName(): string;
1092
3125
  /**
1093
- * Add a UIBlock to the thread
3126
+ * Get the API key (from instance, parameter, or environment)
1094
3127
  */
1095
- addUIBlock(uiblock: UIBlock): void;
3128
+ protected getApiKey(apiKey?: string): string | undefined;
1096
3129
  /**
1097
- * Get a UIBlock by ID
3130
+ * Check if a component contains a Form (data_modification component)
3131
+ * Forms have hardcoded defaultValues that become stale when cached
3132
+ * This checks both single Form components and Forms inside MultiComponentContainer
1098
3133
  */
1099
- getUIBlock(id: string): UIBlock | undefined;
3134
+ protected containsFormComponent(component: any): boolean;
1100
3135
  /**
1101
- * Get all UIBlocks in the thread
3136
+ * Match components from text response suggestions and generate follow-up questions
3137
+ * Takes a text response with component suggestions (c1:type format) and matches with available components
3138
+ * Also generates title, description, and intelligent follow-up questions (actions) based on the analysis
3139
+ * All components are placed in a default MultiComponentContainer layout
3140
+ * @param analysisContent - The text response containing component suggestions
3141
+ * @param components - List of available components
3142
+ * @param apiKey - Optional API key
3143
+ * @param componentStreamCallback - Optional callback to stream primary KPI component as soon as it's identified
3144
+ * @returns Object containing matched components, layout title/description, and follow-up actions
1102
3145
  */
1103
- getUIBlocks(): UIBlock[];
3146
+ matchComponentsFromAnalysis(analysisContent: string, components: Component[], userPrompt: string, apiKey?: string, componentStreamCallback?: (component: Component) => void, deferredTools?: any[], executedTools?: any[], collections?: any, userId?: string): Promise<{
3147
+ components: Component[];
3148
+ layoutTitle: string;
3149
+ layoutDescription: string;
3150
+ actions: Action[];
3151
+ }>;
1104
3152
  /**
1105
- * Get UIBlocks as a Map
1106
- */
1107
- getUIBlocksMap(): Map<string, UIBlock>;
3153
+ * Classify user question into category and detect external tools needed
3154
+ * Determines if question is for data analysis, requires external tools, or needs text response
3155
+ */
3156
+ classifyQuestionCategory(userPrompt: string, apiKey?: string, conversationHistory?: string, externalTools?: any[]): Promise<{
3157
+ category: 'data_analysis' | 'data_modification' | 'general';
3158
+ externalTools: Array<{
3159
+ type: string;
3160
+ name: string;
3161
+ description: string;
3162
+ parameters: Record<string, any>;
3163
+ }>;
3164
+ dataAnalysisType?: 'visualization' | 'calculation' | 'comparison' | 'trend';
3165
+ reasoning: string;
3166
+ confidence: number;
3167
+ }>;
1108
3168
  /**
1109
- * Remove a UIBlock by ID
3169
+ * Adapt UI block parameters based on current user question
3170
+ * Takes a matched UI block from semantic search and modifies its props to answer the new question
3171
+ * Also adapts the cached text response to match the new question
3172
+ */
3173
+ adaptUIBlockParameters(currentUserPrompt: string, originalUserPrompt: string, matchedUIBlock: any, apiKey?: string, cachedTextResponse?: string): Promise<{
3174
+ success: boolean;
3175
+ adaptedComponent?: Component;
3176
+ adaptedTextResponse?: string;
3177
+ parametersChanged?: Array<{
3178
+ field: string;
3179
+ reason: string;
3180
+ }>;
3181
+ explanation: string;
3182
+ }>;
3183
+ /**
3184
+ * Generate text-based response for user question
3185
+ * This provides conversational text responses instead of component generation
3186
+ * Supports tool calling for query execution with automatic retry on errors (max 3 attempts)
3187
+ * After generating text response, if components are provided, matches suggested components
1110
3188
  */
1111
- removeUIBlock(id: string): boolean;
3189
+ 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>;
1112
3190
  /**
1113
- * Check if UIBlock exists
3191
+ * Main orchestration function with semantic search and multi-step classification
3192
+ * NEW FLOW (Recommended):
3193
+ * 1. Semantic search: Check previous conversations (>60% match)
3194
+ * - If match found → Adapt UI block parameters and return
3195
+ * 2. Category classification: Determine if data_analysis, requires_external_tools, or text_response
3196
+ * 3. Route appropriately based on category and response mode
1114
3197
  */
1115
- hasUIBlock(id: string): boolean;
3198
+ 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>;
1116
3199
  /**
1117
- * Get number of UIBlocks in the thread
3200
+ * Generate next questions that the user might ask based on the original prompt and generated component
3201
+ * This helps provide intelligent suggestions for follow-up queries
3202
+ * For general/conversational questions without components, pass textResponse instead
1118
3203
  */
1119
- getUIBlockCount(): number;
3204
+ generateNextQuestions(originalUserPrompt: string, component?: Component | null, componentData?: Record<string, unknown>, apiKey?: string, conversationHistory?: string, textResponse?: string): Promise<string[]>;
3205
+ }
3206
+
3207
+ interface AnthropicLLMConfig extends BaseLLMConfig {
3208
+ }
3209
+ /**
3210
+ * AnthropicLLM class for handling AI-powered component generation and matching using Anthropic Claude
3211
+ */
3212
+ declare class AnthropicLLM extends BaseLLM {
3213
+ constructor(config?: AnthropicLLMConfig);
3214
+ protected getDefaultModel(): string;
3215
+ protected getDefaultFastModel(): string;
3216
+ protected getDefaultApiKey(): string | undefined;
3217
+ protected getProviderName(): string;
3218
+ }
3219
+ declare const anthropicLLM: AnthropicLLM;
3220
+
3221
+ interface GroqLLMConfig extends BaseLLMConfig {
3222
+ }
3223
+ /**
3224
+ * GroqLLM class for handling AI-powered component generation and matching using Groq
3225
+ */
3226
+ declare class GroqLLM extends BaseLLM {
3227
+ constructor(config?: GroqLLMConfig);
3228
+ protected getDefaultModel(): string;
3229
+ protected getDefaultFastModel(): string;
3230
+ protected getDefaultApiKey(): string | undefined;
3231
+ protected getProviderName(): string;
3232
+ }
3233
+ declare const groqLLM: GroqLLM;
3234
+
3235
+ interface GeminiLLMConfig extends BaseLLMConfig {
3236
+ }
3237
+ /**
3238
+ * GeminiLLM class for handling AI-powered component generation and matching using Google Gemini
3239
+ */
3240
+ declare class GeminiLLM extends BaseLLM {
3241
+ constructor(config?: GeminiLLMConfig);
3242
+ protected getDefaultModel(): string;
3243
+ protected getDefaultFastModel(): string;
3244
+ protected getDefaultApiKey(): string | undefined;
3245
+ protected getProviderName(): string;
3246
+ }
3247
+ declare const geminiLLM: GeminiLLM;
3248
+
3249
+ interface OpenAILLMConfig extends BaseLLMConfig {
3250
+ }
3251
+ /**
3252
+ * OpenAILLM class for handling AI-powered component generation and matching using OpenAI GPT models
3253
+ */
3254
+ declare class OpenAILLM extends BaseLLM {
3255
+ constructor(config?: OpenAILLMConfig);
3256
+ protected getDefaultModel(): string;
3257
+ protected getDefaultFastModel(): string;
3258
+ protected getDefaultApiKey(): string | undefined;
3259
+ protected getProviderName(): string;
3260
+ }
3261
+ declare const openaiLLM: OpenAILLM;
3262
+
3263
+ /**
3264
+ * Query Cache — Two mechanisms:
3265
+ *
3266
+ * 1. `cache` (query string → result data) — TTL-based with max size, for avoiding re-execution
3267
+ * of recently validated queries. True LRU eviction: reads bubble entries to the back via
3268
+ * delete+re-set so the oldest *unused* entry is evicted, not the oldest *inserted*.
3269
+ *
3270
+ * 2. Encrypted queryId tokens — SQL is encrypted into the queryId itself (self-contained).
3271
+ * No server-side storage needed for SQL mappings. The token is decrypted on each request.
3272
+ * This eliminates the unbounded queryIdCache that previously grew forever and caused
3273
+ * memory bloat (hundreds of MBs after thousands of queries).
3274
+ *
3275
+ * Result data can still be cached temporarily via the data cache (mechanism 1).
3276
+ */
3277
+ declare class QueryCache {
3278
+ private cache;
3279
+ private ttlMs;
3280
+ private maxCacheSize;
3281
+ private cleanupInterval;
3282
+ private readonly algorithm;
3283
+ private encryptionKey;
3284
+ constructor();
1120
3285
  /**
1121
- * Clear all UIBlocks from the thread
3286
+ * Set the cache TTL (Time To Live)
3287
+ * @param minutes - TTL in minutes (default: 10)
1122
3288
  */
1123
- clear(): void;
3289
+ setTTL(minutes: number): void;
1124
3290
  /**
1125
- * Get creation timestamp
3291
+ * Get the current TTL in minutes
1126
3292
  */
1127
- getCreatedAt(): Date;
3293
+ getTTL(): number;
1128
3294
  /**
1129
- * Get conversation context from recent UIBlocks (excluding current one)
1130
- * Returns formatted string with previous questions and component summaries
1131
- * @param limit - Maximum number of previous UIBlocks to include (default: 5)
1132
- * @param currentUIBlockId - ID of current UIBlock to exclude from context (optional)
1133
- * @returns Formatted conversation history string
3295
+ * Store query result in data cache.
3296
+ * If the key already exists, it's removed first so the re-insert places it
3297
+ * at the back of the iteration order (LRU). Eviction only fires when adding
3298
+ * a genuinely new key past the size limit.
1134
3299
  */
1135
- getConversationContext(limit?: number, currentUIBlockId?: string): string;
3300
+ set(query: string, data: any): void;
1136
3301
  /**
1137
- * Convert Thread to JSON-serializable object
3302
+ * Get cached result if exists and not expired.
3303
+ * On hit, re-inserts the entry so it moves to the back of the Map's
3304
+ * iteration order — turning FIFO eviction into true LRU.
1138
3305
  */
1139
- toJSON(): Record<string, any>;
1140
- }
1141
-
1142
- /**
1143
- * ThreadManager manages all threads globally
1144
- * Provides methods to create, retrieve, and delete threads
1145
- */
1146
- declare class ThreadManager {
1147
- private static instance;
1148
- private threads;
1149
- private constructor();
3306
+ get(query: string): any | null;
1150
3307
  /**
1151
- * Get singleton instance of ThreadManager
3308
+ * Check if query exists in cache (not expired)
1152
3309
  */
1153
- static getInstance(): ThreadManager;
3310
+ has(query: string): boolean;
1154
3311
  /**
1155
- * Create a new thread
1156
- * @param id - Optional custom ID, generates UUID if not provided
1157
- * @returns The created Thread instance
3312
+ * Remove a specific query from cache
1158
3313
  */
1159
- createThread(id?: string): Thread;
3314
+ delete(query: string): void;
1160
3315
  /**
1161
- * Get a thread by ID
3316
+ * Clear all cached entries
1162
3317
  */
1163
- getThread(id: string): Thread | undefined;
3318
+ clear(): void;
1164
3319
  /**
1165
- * Get all threads
3320
+ * Get cache statistics
1166
3321
  */
1167
- getAllThreads(): Thread[];
3322
+ getStats(): {
3323
+ size: number;
3324
+ queryIdCount: number;
3325
+ oldestEntryAge: number | null;
3326
+ };
1168
3327
  /**
1169
- * Get threads as a Map
3328
+ * Start periodic cleanup of expired data cache entries.
1170
3329
  */
1171
- getThreadsMap(): Map<string, Thread>;
3330
+ private startCleanup;
1172
3331
  /**
1173
- * Delete a thread by ID
3332
+ * Encrypt a payload into a self-contained token.
1174
3333
  */
1175
- deleteThread(id: string): boolean;
3334
+ private encrypt;
1176
3335
  /**
1177
- * Check if thread exists
3336
+ * Decrypt a token back to the original payload.
1178
3337
  */
1179
- hasThread(id: string): boolean;
3338
+ private decrypt;
1180
3339
  /**
1181
- * Get number of threads
3340
+ * Store a query by generating an encrypted token as queryId.
3341
+ * The SQL is encrypted INTO the token — nothing stored in memory.
3342
+ * If data is provided, it's cached temporarily in the data cache.
1182
3343
  */
1183
- getThreadCount(): number;
3344
+ storeQuery(query: any, data?: any): string;
1184
3345
  /**
1185
- * Clear all threads
3346
+ * Get a stored query by decrypting its token.
3347
+ * Returns the SQL + any cached result data.
1186
3348
  */
1187
- clearAll(): void;
3349
+ getQuery(queryId: string): {
3350
+ query: any;
3351
+ data: any;
3352
+ } | null;
1188
3353
  /**
1189
- * Find a UIBlock by ID across all threads
1190
- * @param uiBlockId - The UIBlock ID to search for
1191
- * @returns Object with thread and uiBlock if found, undefined otherwise
3354
+ * Update cached data for a queryId token
1192
3355
  */
1193
- findUIBlockById(uiBlockId: string): {
1194
- thread: Thread;
1195
- uiBlock: UIBlock;
1196
- } | undefined;
3356
+ setQueryData(queryId: string, data: any): void;
1197
3357
  /**
1198
- * Convert all threads to JSON-serializable object
3358
+ * Stop cleanup interval (for graceful shutdown)
1199
3359
  */
1200
- toJSON(): Record<string, any>;
3360
+ destroy(): void;
1201
3361
  }
3362
+ declare const queryCache: QueryCache;
1202
3363
 
1203
3364
  /**
1204
- * CleanupService handles cleanup of old threads and UIBlocks
1205
- * to prevent memory bloat and maintain optimal performance
3365
+ * Manages conversation history scoped per user + dashboard.
3366
+ * Each user-dashboard pair has its own isolated history that expires after a configurable TTL.
1206
3367
  */
1207
- declare class CleanupService {
1208
- private static instance;
3368
+ declare class DashboardConversationHistory {
3369
+ private histories;
3370
+ private ttlMs;
3371
+ private maxEntries;
1209
3372
  private cleanupInterval;
1210
- private constructor();
1211
- /**
1212
- * Get singleton instance of CleanupService
1213
- */
1214
- static getInstance(): CleanupService;
3373
+ constructor();
1215
3374
  /**
1216
- * Clean up old threads based on retention period
1217
- * @param retentionDays - Number of days to keep threads (defaults to config)
1218
- * @returns Number of threads deleted
3375
+ * Set the TTL for dashboard histories
3376
+ * @param minutes - TTL in minutes
1219
3377
  */
1220
- cleanupOldThreads(retentionDays?: number): number;
3378
+ setTTL(minutes: number): void;
1221
3379
  /**
1222
- * Clean up old UIBlocks within threads based on retention period
1223
- * @param retentionDays - Number of days to keep UIBlocks (defaults to config)
1224
- * @returns Object with number of UIBlocks deleted per thread
3380
+ * Set max entries per dashboard
1225
3381
  */
1226
- cleanupOldUIBlocks(retentionDays?: number): {
1227
- [threadId: string]: number;
1228
- };
3382
+ setMaxEntries(max: number): void;
1229
3383
  /**
1230
- * Clear all component data from UIBlocks to free memory
1231
- * Keeps metadata but removes the actual data
1232
- * @param retentionDays - Number of days to keep full data (defaults to config)
1233
- * @returns Number of UIBlocks whose data was cleared
3384
+ * Add a conversation entry for a user's dashboard
1234
3385
  */
1235
- clearOldUIBlockData(retentionDays?: number): number;
3386
+ addEntry(dashboardId: string, userPrompt: string, componentSummary: string, userId?: string): void;
1236
3387
  /**
1237
- * Run full cleanup (threads, UIBlocks, and data)
1238
- * @returns Cleanup statistics
3388
+ * Get formatted conversation history for a user's dashboard
1239
3389
  */
1240
- runFullCleanup(): {
1241
- threadsDeleted: number;
1242
- uiblocksDeleted: {
1243
- [threadId: string]: number;
1244
- };
1245
- dataCleared: number;
1246
- };
3390
+ getHistory(dashboardId: string, userId?: string): string;
1247
3391
  /**
1248
- * Start automatic cleanup at regular intervals
1249
- * @param intervalHours - Hours between cleanup runs (default: 24)
3392
+ * Clear history for a specific user's dashboard
1250
3393
  */
1251
- startAutoCleanup(intervalHours?: number): void;
3394
+ clearDashboard(dashboardId: string, userId?: string): void;
1252
3395
  /**
1253
- * Stop automatic cleanup
3396
+ * Clear all dashboard histories
1254
3397
  */
1255
- stopAutoCleanup(): void;
3398
+ clearAll(): void;
1256
3399
  /**
1257
- * Check if auto cleanup is running
3400
+ * Start periodic cleanup of expired histories
1258
3401
  */
1259
- isAutoCleanupRunning(): boolean;
3402
+ private startCleanup;
1260
3403
  /**
1261
- * Get current memory usage statistics
3404
+ * Stop cleanup interval (for graceful shutdown)
1262
3405
  */
1263
- getMemoryStats(): {
1264
- threadCount: number;
1265
- totalUIBlocks: number;
1266
- avgUIBlocksPerThread: number;
1267
- };
3406
+ destroy(): void;
1268
3407
  }
3408
+ declare const dashboardConversationHistory: DashboardConversationHistory;
1269
3409
 
1270
3410
  /**
1271
- * Configuration for data storage limits in UIBlocks
3411
+ * ScriptMatcher LLM-Based Script Matching + Parameter Extraction
3412
+ *
3413
+ * Uses ONE LLM call to:
3414
+ * 1. Pick the best matching script from the library (or "none")
3415
+ * 2. Extract parameter values from the user question
3416
+ *
3417
+ * Why LLM over embeddings:
3418
+ * - Embeddings capture topic similarity ("overstock" ≈ "inventory" ≈ "revenue")
3419
+ * but can't distinguish structurally different questions about the same domain
3420
+ * - LLM understands that "overstock by warehouse" needs a different script than
3421
+ * "revenue by warehouse" even though they're semantically close
3422
+ * - One call does both matching AND parameter extraction
3423
+ *
3424
+ * When script library grows past ~50, add an embedding pre-filter
3425
+ * (ChromaDB narrows to top 10 → LLM picks from those 10).
1272
3426
  */
1273
- declare const STORAGE_CONFIG: {
1274
- /**
1275
- * Maximum number of rows to store in UIBlock data
1276
- */
1277
- MAX_ROWS_PER_BLOCK: number;
1278
- /**
1279
- * Maximum size in bytes per UIBlock (1MB)
1280
- */
1281
- MAX_SIZE_PER_BLOCK_BYTES: number;
3427
+
3428
+ declare class ScriptMatcher {
3429
+ private store;
3430
+ constructor(store: ScriptStore);
1282
3431
  /**
1283
- * Number of days to keep threads before cleanup
3432
+ * Find the best matching script for a user question.
3433
+ * Uses ONE LLM call that picks the script AND extracts parameters.
3434
+ * Returns null if no script matches.
1284
3435
  */
1285
- THREAD_RETENTION_DAYS: number;
3436
+ match(userPrompt: string, apiKey?: string, model?: string): Promise<ScriptMatch | null>;
1286
3437
  /**
1287
- * Number of days to keep UIBlocks before cleanup
3438
+ * Build the script catalog string for the LLM prompt.
3439
+ * Each script gets: index, ID, name, description, and parameter definitions.
1288
3440
  */
1289
- UIBLOCK_RETENTION_DAYS: number;
1290
- };
3441
+ private buildScriptCatalog;
3442
+ }
1291
3443
 
1292
3444
  /**
1293
- * Configuration for conversation context and history management
3445
+ * ScriptRunner Execute scripts in an isolated tsx subprocess.
3446
+ *
3447
+ * The subprocess approach replaces the earlier `new Function()` eval and gives us:
3448
+ * - Real sandbox (separate process, SIGKILL on timeout).
3449
+ * - Real TypeScript (tsx transpiles on the fly).
3450
+ * - npm imports available to scripts (clustering, stats, geo, etc.).
3451
+ *
3452
+ * Protocol: NDJSON over the child's stdin/stdout. See script-ipc.ts + backend/docs/SCRIPT-FLOW-IMPLEMENTATION.md.
1294
3453
  */
1295
- declare const CONTEXT_CONFIG: {
1296
- /**
1297
- * Maximum number of previous UIBlocks to include as conversation context
1298
- * Set to 0 to disable conversation history
1299
- * Higher values provide more context but may increase token usage
1300
- */
1301
- MAX_CONVERSATION_CONTEXT_BLOCKS: number;
1302
- };
1303
3454
 
1304
- declare const SDK_VERSION = "0.0.8";
3455
+ interface RunScriptOptions {
3456
+ /** Data sources the script is allowed to query via ctx.query */
3457
+ externalTools: ExternalTool[];
3458
+ /** Optional — for propagating per-query UI progress to the user */
3459
+ streamBuffer?: StreamBuffer;
3460
+ /** Override the wall-clock timeout (default `SCRIPT_TIMEOUT_MS`, 60s). */
3461
+ timeoutMs?: number;
3462
+ }
3463
+ /**
3464
+ * Execute a recipe by spawning a tsx child on the script's .ts file.
3465
+ * `scriptPath` is the absolute path to the saved `.ts` body.
3466
+ */
3467
+ declare function runScript(recipe: ScriptRecipe, scriptPath: string, params: Record<string, any>, options: RunScriptOptions): Promise<ScriptResult>;
3468
+
1305
3469
  type MessageTypeHandler = (message: IncomingMessage) => void | Promise<void>;
1306
3470
  declare class SuperatomSDK {
1307
3471
  private ws;
1308
3472
  private url;
1309
- private apiKey;
3473
+ private apiKey?;
1310
3474
  private projectId;
1311
- private userId;
1312
3475
  private type;
1313
3476
  private bundleDir;
1314
3477
  private messageHandlers;
@@ -1318,12 +3481,26 @@ declare class SuperatomSDK {
1318
3481
  private maxReconnectAttempts;
1319
3482
  private collections;
1320
3483
  private components;
3484
+ private tools;
3485
+ private workflows;
1321
3486
  private anthropicApiKey;
1322
3487
  private groqApiKey;
3488
+ private geminiApiKey;
3489
+ private openaiApiKey;
1323
3490
  private llmProviders;
3491
+ private databaseType;
3492
+ private modelStrategy;
3493
+ private mainAgentModel;
3494
+ private sourceAgentModel;
3495
+ private dashCompModels?;
3496
+ private conversationSimilarityThreshold;
1324
3497
  private userManager;
1325
3498
  private dashboardManager;
1326
3499
  private reportManager;
3500
+ private pingInterval;
3501
+ private lastPong;
3502
+ private readonly PING_INTERVAL_MS;
3503
+ private readonly PONG_TIMEOUT_MS;
1327
3504
  constructor(config: SuperatomSDKConfig);
1328
3505
  /**
1329
3506
  * Initialize PromptLoader and load prompts into memory
@@ -1363,9 +3540,11 @@ declare class SuperatomSDK {
1363
3540
  */
1364
3541
  private handleMessage;
1365
3542
  /**
1366
- * Send a message to the Superatom service
3543
+ * Send a message to the Superatom service.
3544
+ * Returns true if the message was sent, false if the WebSocket is not connected.
3545
+ * Does NOT throw on closed connections — callers can check the return value if needed.
1367
3546
  */
1368
- send(message: Message): void;
3547
+ send(message: Message): boolean;
1369
3548
  /**
1370
3549
  * Register a message handler to receive all messages
1371
3550
  */
@@ -1391,7 +3570,69 @@ declare class SuperatomSDK {
1391
3570
  */
1392
3571
  addCollection<TParams = any, TResult = any>(collectionName: string, operation: CollectionOperation | string, handler: CollectionHandler<TParams, TResult>): void;
1393
3572
  private handleReconnect;
3573
+ /**
3574
+ * Start heartbeat to keep WebSocket connection alive
3575
+ * Sends PING every 3 minutes to prevent idle timeout from cloud infrastructure
3576
+ */
3577
+ private startHeartbeat;
3578
+ /**
3579
+ * Stop the heartbeat interval
3580
+ */
3581
+ private stopHeartbeat;
3582
+ /**
3583
+ * Handle PONG response from server
3584
+ */
3585
+ private handlePong;
1394
3586
  private storeComponents;
3587
+ /**
3588
+ * Set tools for the SDK instance
3589
+ */
3590
+ setTools(tools: Tool$1[]): void;
3591
+ /**
3592
+ * Get the stored tools
3593
+ */
3594
+ getTools(): Tool$1[];
3595
+ /**
3596
+ * Register workflow components for the SDK instance.
3597
+ *
3598
+ * Workflows are pre-built multi-step UI flows the main agent can pick when
3599
+ * the user's prompt matches a workflow's `whenToUse` trigger. Picking a
3600
+ * workflow short-circuits analysis text + dashboard component generation —
3601
+ * the workflow component is returned directly, with the LLM-extracted props.
3602
+ */
3603
+ setWorkflows(workflows: WorkflowDescriptor[]): void;
3604
+ /**
3605
+ * Get the registered workflow components.
3606
+ */
3607
+ getWorkflows(): WorkflowDescriptor[];
3608
+ /**
3609
+ * Apply model strategy to all LLM provider singletons
3610
+ * @param strategy - 'best', 'fast', or 'balanced'
3611
+ */
3612
+ private applyModelStrategy;
3613
+ /**
3614
+ * Set model strategy at runtime
3615
+ * @param strategy - 'best', 'fast', or 'balanced'
3616
+ */
3617
+ setModelStrategy(strategy: ModelStrategy): void;
3618
+ /**
3619
+ * Get current model strategy
3620
+ */
3621
+ getModelStrategy(): ModelStrategy;
3622
+ /**
3623
+ * Apply conversation similarity threshold to all LLM provider singletons
3624
+ * @param threshold - Value between 0 and 1 (e.g., 0.8 = 80% similarity required)
3625
+ */
3626
+ private applyConversationSimilarityThreshold;
3627
+ /**
3628
+ * Set conversation similarity threshold at runtime
3629
+ * @param threshold - Value between 0 and 1 (e.g., 0.8 = 80% similarity required)
3630
+ */
3631
+ setConversationSimilarityThreshold(threshold: number): void;
3632
+ /**
3633
+ * Get current conversation similarity threshold
3634
+ */
3635
+ getConversationSimilarityThreshold(): number;
1395
3636
  }
1396
3637
 
1397
- export { type Action, CONTEXT_CONFIG, type CapturedLog, CleanupService, type CollectionHandler, type CollectionOperation, type IncomingMessage, LLM, type LogLevel, type Message, SDK_VERSION, STORAGE_CONFIG, SuperatomSDK, type SuperatomSDKConfig, Thread, ThreadManager, UIBlock, UILogCollector, type User, UserManager, type UsersData, logger };
3638
+ export { type Action, type AgentConfig, type AgentResponse, BM25L, type BM25LOptions, type BaseLLMConfig, CONTEXT_CONFIG, type CapturedLog, CleanupService, type CollectionHandler, type CollectionOperation, type DBUIBlock, DEFAULT_AGENT_CONFIG, type DatabaseType, type HybridSearchOptions, type IncomingMessage, type KbNodesQueryFilters, type KbNodesRequestPayload, LLM, type LLMUsageEntry, type LogLevel, MainAgent, type Message, type ModelStrategy, type OutputField, type RerankedResult, STORAGE_CONFIG, type ScriptComponentSpec, ScriptMatcher, type ScriptParameter, type ScriptRecipe, type ScriptRecipeMetaRow, type ScriptRecipeStore, type ScriptResult, ScriptStore, type ScriptStoreOptions, type SelectedWorkflow, SuperatomSDK, type SuperatomSDKConfig, type TaskType, Thread, ThreadManager, type Tool$1 as Tool, type ToolOutputSchema, UIBlock, UILogCollector, type User, UserManager, type UsersData, type WorkflowDescriptor, anthropicLLM, dashboardConversationHistory, geminiLLM, groqLLM, hybridRerank, llmUsageLogger, logger, normalizeScriptBody, openaiLLM, queryCache, rerankChromaResults, rerankConversationResults, resolveScriptRecipeStore, runScript, userPromptErrorLogger };