@superatomai/sdk-node 0.0.6 → 0.0.7-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,151 +1320,913 @@ 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 from anthropic.ts implementation
867
- * @param text - Text that may contain JSON wrapped in ```json...``` or with surrounding text
868
- * @returns Parsed JSON object
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
965
1399
  */
966
- declare class UIBlock {
967
- private id;
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 */
1678
+ scriptBody: string;
1679
+ /** Times this script was used successfully */
1680
+ successCount: number;
1681
+ /** Times this script failed */
1682
+ failureCount: number;
1683
+ /** ISO timestamp of last usage */
1684
+ lastUsed: string;
1685
+ /** Original user question that created this script */
1686
+ createdFrom: string;
1687
+ /** ISO timestamp */
1688
+ createdAt: string;
1689
+ /** ISO timestamp */
1690
+ updatedAt: string;
1691
+ /**
1692
+ * `recipe.id` of the parent this script was forked from.
1693
+ * Undefined for root scripts (those written from scratch by MainAgent).
1694
+ * See backend/docs/SCRIPT-FLOW-FORK-ADAPT.md.
1695
+ */
1696
+ parentId?: string;
1697
+ /** 0 for root scripts; `parent.forkDepth + 1` for forks. Capped at 3. */
1698
+ forkDepth?: number;
1699
+ /**
1700
+ * Brief description of what this fork changed vs its parent
1701
+ * (sourced from the matcher's `modificationHint`).
1702
+ */
1703
+ forkReason?: string;
1704
+ /**
1705
+ * Validated component specs captured at authoring time. On a tier-high
1706
+ * replay these are rebound to fresh queryIds deterministically — no
1707
+ * component-generation LLM call, and the rendered columns can't drift from
1708
+ * what was validated when the script was authored. Absent on recipes
1709
+ * authored before this landed; those fall back to LLM component generation.
1710
+ * See backend/docs/SCRIPT-COMPONENT-CONSISTENCY.md.
1711
+ */
1712
+ components?: ScriptComponentSpec[];
1713
+ /**
1714
+ * Lifecycle stage of this recipe on disk.
1715
+ * - 'draft': written by MainAgent's write_script during a turn; filtered out
1716
+ * of `ScriptStore.getAll()` so the matcher never picks it.
1717
+ * Filename is suffixed with `turnId` to keep concurrent turns
1718
+ * from clobbering each other's drafts.
1719
+ * - 'verified': promoted after `execute_script` succeeded; the matcher sees it.
1720
+ * Filename drops the turn suffix unless a verified file with
1721
+ * the same slug already exists (collision case keeps the suffix).
1722
+ *
1723
+ * Recipes loaded from disk without this field default to 'verified' so
1724
+ * existing scripts keep working unchanged.
1725
+ */
1726
+ status?: 'draft' | 'verified';
1727
+ /**
1728
+ * Per-turn unique suffix used for draft filenames (e.g. `1714745623-x9k2`).
1729
+ * Set when the draft is saved; carried until the recipe is promoted.
1730
+ */
1731
+ turnId?: string;
1732
+ /**
1733
+ * Last execution error captured by `recordDraftError` while the recipe was
1734
+ * still a draft. Lets users open the draft .json file and see why it failed
1735
+ * without grepping logs. Cleared on promotion to 'verified'.
1736
+ */
1737
+ lastError?: {
1738
+ phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
1739
+ message: string;
1740
+ at: string;
1741
+ attempt: number;
1742
+ };
1743
+ }
1744
+ interface ScriptParameter {
1745
+ /** Parameter name (used in script body as params.name) */
1746
+ name: string;
1747
+ /** Parameter type */
1748
+ type: 'string' | 'number' | 'date' | 'date_range' | 'enum' | 'boolean';
1749
+ /** Whether this parameter is required */
1750
+ required: boolean;
1751
+ /** Default value if not provided */
1752
+ default?: any;
1753
+ /** For enum type — maps user-facing values to internal values */
1754
+ enumValues?: Record<string, string>;
1755
+ /** Human-readable description (used in the matcher LLM prompt) */
1756
+ description: string;
1757
+ }
1758
+ /**
1759
+ * A reusable component binding captured when a script is authored. Stored on
1760
+ * the recipe so tier-high replays rebuild components deterministically (rebind
1761
+ * to fresh queryIds) instead of re-running the component-picker LLM.
1762
+ */
1763
+ interface ScriptComponentSpec {
1764
+ /** Registered component name (e.g. "DynamicBarChart") — matched against the available component library. */
1765
+ componentType: string;
1766
+ /** `executedQuery.sourceId` to bind to (e.g. a tool id or 'computed:_final'), or 'federation' for a cross-source component. */
1767
+ sourceRef: string;
1768
+ /** Present only when sourceRef === 'federation' — the DuckDB SQL to re-execute on replay. */
1769
+ federationSql?: string;
1770
+ title?: string;
1771
+ description?: string;
1772
+ /** Validated axis/value keys + aggregation — all referencing real columns of the bound source. */
1773
+ config: Record<string, any>;
1774
+ }
1775
+
1776
+ /**
1777
+ * ScriptStore — Persistent Storage for Script Recipes
1778
+ *
1779
+ * Layout:
1780
+ * scripts-store/
1781
+ * metadata/<name>.json ← recipe metadata (params, tables, counts, ...)
1782
+ * <name>.ts ← the getData() function body, editable in your IDE
1783
+ *
1784
+ * Each VM deployment is a single project, so no project ID prefix needed.
1785
+ * Legacy single-file format (JSON with embedded scriptBody) is still loadable
1786
+ * for backwards compatibility and auto-migrated to the split format on next save.
1787
+ */
1788
+
1789
+ /**
1790
+ * Input for `ScriptStore.saveDraft()`. Identifies the draft by `recipeId` so
1791
+ * retries within the same turn overwrite the same files (or omit `recipeId`
1792
+ * on the first call to mint a fresh draft).
1793
+ */
1794
+ interface SaveDraftInput {
1795
+ /** Reuse an existing draft (retry); omit to mint a new one. */
1796
+ recipeId?: string;
1797
+ /** Per-turn unique suffix, stable across retries within the turn. */
1798
+ turnId: string;
1799
+ name: string;
1800
+ intentDescription: string;
1801
+ tags: string[];
1802
+ parameters: ScriptParameter[];
1803
+ scriptBody: string;
1804
+ createdFrom: string;
1805
+ }
1806
+ /**
1807
+ * Lineage + provenance fields applied when a draft is promoted to verified.
1808
+ * MainAgent gathers `sourceIds` and `tables` from the verified execution; the
1809
+ * caller (agent-user-response.ts) supplies the optional fork lineage.
1810
+ */
1811
+ interface PromoteToVerifiedInput {
1812
+ sourceIds: string[];
1813
+ tables: string[];
1814
+ parentId?: string;
1815
+ forkDepth?: number;
1816
+ forkReason?: string;
1817
+ /** Validated component specs to persist on the recipe (component recipe). */
1818
+ components?: ScriptComponentSpec[];
1819
+ }
1820
+ declare class ScriptStore {
1821
+ private recipes;
1822
+ private storeDir;
1823
+ private loaded;
1824
+ constructor(baseDir?: string);
1825
+ private metadataDir;
1826
+ /**
1827
+ * Filename base for a recipe. Drafts include the per-turn suffix so two
1828
+ * concurrent turns can never clobber each other's draft files; verified
1829
+ * recipes use the bare slug.
1830
+ */
1831
+ private fileBaseName;
1832
+ /**
1833
+ * Absolute path to the .ts file for a recipe. Callers (e.g. ScriptRunner,
1834
+ * MainAgent's execute_script) use this to hand off the path to the tsx child.
1835
+ */
1836
+ getScriptPath(recipe: ScriptRecipe): string;
1837
+ /**
1838
+ * Absolute path to the metadata JSON for a recipe.
1839
+ */
1840
+ private getMetadataPath;
1841
+ /**
1842
+ * Get all VERIFIED recipes — drafts are filtered out so the matcher never
1843
+ * considers an unverified script. Loads from disk on first access.
1844
+ */
1845
+ getAll(): ScriptRecipe[];
1846
+ /**
1847
+ * Get a recipe by ID — returns drafts as well as verified scripts.
1848
+ * Used by MainAgent and the promotion path.
1849
+ */
1850
+ get(id: string): ScriptRecipe | null;
1851
+ /**
1852
+ * Number of verified recipes (matches `getAll().length`).
1853
+ */
1854
+ count(): number;
1855
+ /**
1856
+ * Save a recipe (create or update).
1857
+ * File is named after the script: "order-status-distribution.json"
1858
+ */
1859
+ save(recipe: ScriptRecipe): void;
1860
+ /**
1861
+ * Persist (or update) a draft recipe to disk. Always writes immediately so
1862
+ * the `.ts` body is visible in the IDE the moment MainAgent calls
1863
+ * `write_script`. Within one turn, retries that pass the same `recipeId`
1864
+ * overwrite the same files (the LLM rewriting itself); a fresh `recipeId`
1865
+ * is minted only on the first call of the turn.
1866
+ *
1867
+ * Filename includes the `turnId` suffix so concurrent turns never collide.
1868
+ */
1869
+ saveDraft(input: SaveDraftInput): ScriptRecipe;
1870
+ /**
1871
+ * Stamp the draft with the most recent execution failure so it is visible
1872
+ * in the metadata JSON without grepping logs. No-op if the recipe doesn't
1873
+ * exist or has already been promoted.
1874
+ */
1875
+ recordDraftError(recipeId: string, err: {
1876
+ phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
1877
+ message: string;
1878
+ attempt: number;
1879
+ }): void;
1880
+ /**
1881
+ * Promote a successfully-executed draft into a verified script.
1882
+ *
1883
+ * - Renames the on-disk files from `<slug>-<turnId>.{ts,json}` to the bare
1884
+ * `<slug>.{ts,json}`. If a verified file with the same slug already exists
1885
+ * (concurrent turn won the race, or a prior session has it), the recipe
1886
+ * keeps its turn-suffixed filename so we never overwrite verified work —
1887
+ * the matcher keys on `recipe.id`, so two siblings with similar slugs is fine.
1888
+ * - Flips `status: 'verified'`, clears `lastError`, applies provenance
1889
+ * (`sourceIds`, `tables`) and optional fork lineage.
1890
+ */
1891
+ promoteToVerified(recipeId: string, input: PromoteToVerifiedInput): ScriptRecipe | null;
1892
+ /**
1893
+ * Drop a draft from disk + memory (e.g. when an outer error path wants to
1894
+ * clean up). Per the agreed policy, MainAgent's normal failure path does
1895
+ * NOT call this — failed drafts are kept on disk for the user to inspect.
1896
+ */
1897
+ discardDraft(recipeId: string): void;
1898
+ /**
1899
+ * Delete a recipe by ID.
1900
+ */
1901
+ delete(id: string): void;
1902
+ /**
1903
+ * Record a successful execution for a recipe.
1904
+ */
1905
+ recordSuccess(id: string): void;
1906
+ /**
1907
+ * Record a failed execution for a recipe.
1908
+ */
1909
+ recordFailure(id: string): void;
1910
+ /**
1911
+ * Get the failure rate for a recipe (0 to 1).
1912
+ * Returns 0 if the recipe has fewer than 5 total uses.
1913
+ */
1914
+ getFailureRate(id: string): number;
1915
+ private ensureLoaded;
1916
+ /**
1917
+ * Convert a script name to a safe filename.
1918
+ * "Order Status Distribution" → "order-status-distribution"
1919
+ */
1920
+ private toFileName;
1921
+ /**
1922
+ * Load all script files from disk.
1923
+ *
1924
+ * Supports two layouts:
1925
+ * - New (preferred): metadata/<name>.json + <name>.ts
1926
+ * - Legacy: <name>.json with embedded scriptBody (auto-migrated on next save)
1927
+ */
1928
+ private loadAllFromDisk;
1929
+ /**
1930
+ * Save a recipe to disk in split format:
1931
+ * metadata/<name>.json (metadata, no scriptBody)
1932
+ * <name>.ts (just the function body)
1933
+ *
1934
+ * If a legacy top-level <name>.json exists for the same recipe, remove it
1935
+ * so we don't end up with duplicate sources of truth.
1936
+ */
1937
+ private saveRecipeToDisk;
1938
+ /**
1939
+ * Delete a recipe's files from disk (both metadata + body, plus any legacy file).
1940
+ */
1941
+ private deleteRecipeFromDisk;
1942
+ }
1943
+
1944
+ /**
1945
+ * Main Agent (Orchestrator)
1946
+ *
1947
+ * A single LLM.streamWithTools() call that handles everything:
1948
+ * - Routing: decides which source(s) to query based on summaries
1949
+ * - Querying: calls source tools (each wraps an independent SourceAgent)
1950
+ * - Direct tools: calls pre-built function tools directly with LLM-provided params
1951
+ * - Re-querying: if data is wrong/incomplete, calls tools again with modified intent
1952
+ * - Analysis: generates final text response from the data
1953
+ *
1954
+ * Two tool types:
1955
+ * - "source" tools: main agent sees summaries, SourceAgent handles SQL generation independently
1956
+ * - "direct" tools: main agent calls fn() directly with structured params (no SourceAgent)
1957
+ */
1958
+
1959
+ declare class MainAgent {
1960
+ private externalTools;
1961
+ private workflows;
1962
+ private config;
1963
+ private streamBuffer;
1964
+ /**
1965
+ * Optional: when provided, MainAgent exposes the `write_script` /
1966
+ * `execute_script` tools to the LLM and persists drafts to disk via the
1967
+ * store. Headless callers (alert analyzer, metric resolver) omit these to
1968
+ * suppress script authoring entirely — drafts would otherwise leak onto
1969
+ * disk with no caller to promote or clean them up.
1970
+ */
1971
+ private scriptStore;
1972
+ private turnId;
1973
+ private createdFromPrompt;
1974
+ private scriptState;
1975
+ constructor(externalTools: ExternalTool[], config: AgentConfig, scriptStore?: ScriptStore, turnId?: string, streamBuffer?: StreamBuffer, workflows?: WorkflowDescriptor[]);
1976
+ private get scriptingEnabled();
1977
+ /**
1978
+ * Handle a user question using the multi-agent system.
1979
+ *
1980
+ * This is ONE LLM.streamWithTools() call. The LLM:
1981
+ * 1. Sees source summaries + direct tool descriptions in system prompt
1982
+ * 2. Decides which tool(s) to call (routing)
1983
+ * 3. Source tools → SourceAgent runs independently → returns data
1984
+ * 4. Direct tools → fn() called directly with LLM params → returns data
1985
+ * 5. Generates final analysis text
1986
+ */
1987
+ handleQuestion(userPrompt: string, apiKey?: string, conversationHistory?: string, streamCallback?: (chunk: string) => void): Promise<AgentResponse>;
1988
+ private handleWriteScript;
1989
+ private handleExecuteScript;
1990
+ /**
1991
+ * Build the AgentWrittenScript payload the caller will hand to
1992
+ * `ScriptStore.promoteToVerified()`. Only returned when a verified
1993
+ * successful execution is on record.
1994
+ */
1995
+ private buildSavedScript;
1996
+ private normalizeParameterList;
1997
+ /**
1998
+ * Use the schema embedding collection to pre-select relevant tables for
1999
+ * this source + intent. Returns a formatted schema block if confidence is
2000
+ * high (top match ≥ 0.55 and ≥3 candidates), otherwise null.
2001
+ *
2002
+ * When this returns a block, we can skip the SourceAgent's `search_schema`
2003
+ * loop and reduce iteration budget. When it returns null, the SourceAgent
2004
+ * falls back to the existing LLM-driven keyword search (same as today).
2005
+ */
2006
+ private preResolveSchema;
2007
+ /**
2008
+ * Execute a direct tool — call fn() with LLM-provided params, no SourceAgent.
2009
+ */
2010
+ private handleDirectTool;
2011
+ /**
2012
+ * Build the main agent's system prompt with source summaries, direct tool descriptions,
2013
+ * and workflow component descriptions.
2014
+ */
2015
+ private buildSystemPrompt;
2016
+ /**
2017
+ * Build tool definitions for source tools — summary-only descriptions.
2018
+ * The full schema is inside the SourceAgent which runs independently.
2019
+ */
2020
+ private buildSourceToolDefinitions;
2021
+ /**
2022
+ * Build tool definitions for direct tools — expose their actual params.
2023
+ * These are called directly by the main agent LLM, no SourceAgent.
2024
+ */
2025
+ private buildDirectToolDefinitions;
2026
+ /**
2027
+ * Capture a workflow selection. We do NOT execute anything — the LLM has
2028
+ * already extracted the props it wants the workflow rendered with. We
2029
+ * record the selection (via the capture callback) and return a short
2030
+ * acknowledgement so the LLM ends its turn cleanly without writing
2031
+ * analysis text or calling more tools.
2032
+ */
2033
+ private handleWorkflow;
2034
+ /**
2035
+ * Build LLM tool definitions for workflow components. The workflow's
2036
+ * propsSchema becomes the tool's input_schema so the LLM extracts props
2037
+ * directly from the prompt — same mechanic as direct tools.
2038
+ */
2039
+ private buildWorkflowToolDefinitions;
2040
+ /**
2041
+ * Format a source agent's result as a clean string for the main agent LLM.
2042
+ */
2043
+ private formatResultForMainAgent;
2044
+ /**
2045
+ * Get source summaries (for external inspection/debugging).
2046
+ */
2047
+ getSourceSummaries(): SourceSummary[];
2048
+ }
2049
+
2050
+ /**
2051
+ * Represents an action that can be performed on a UIBlock
2052
+ */
2053
+ interface Action {
2054
+ id: string;
2055
+ name: string;
2056
+ type: string;
2057
+ [key: string]: any;
2058
+ }
2059
+
2060
+ type SystemPrompt = string | Anthropic.Messages.TextBlockParam[];
2061
+ interface LLMMessages {
2062
+ sys: SystemPrompt;
2063
+ user: string;
2064
+ prefill?: string;
2065
+ }
2066
+ interface LLMOptions {
2067
+ model?: string;
2068
+ maxTokens?: number;
2069
+ temperature?: number;
2070
+ topP?: number;
2071
+ apiKey?: string;
2072
+ partial?: (chunk: string) => void;
2073
+ }
2074
+ interface Tool {
2075
+ name: string;
2076
+ description: string;
2077
+ input_schema: {
2078
+ type: string;
2079
+ properties: Record<string, any>;
2080
+ required?: string[];
2081
+ };
2082
+ }
2083
+ declare class LLM {
2084
+ static text(messages: LLMMessages, options?: LLMOptions): Promise<string>;
2085
+ static stream<T = string>(messages: LLMMessages, options?: LLMOptions, json?: boolean): Promise<T extends string ? string : any>;
2086
+ static streamWithTools(messages: LLMMessages, tools: Tool[], toolHandler: (toolName: string, toolInput: any) => Promise<any>, options?: LLMOptions, maxIterations?: number): Promise<string>;
2087
+ /**
2088
+ * Normalize system prompt to Anthropic format
2089
+ * Converts string to array format if needed
2090
+ * @param sys - System prompt (string or array of blocks)
2091
+ * @returns Normalized system prompt for Anthropic API
2092
+ */
2093
+ private static _normalizeSystemPrompt;
2094
+ /**
2095
+ * Strip unpaired UTF-16 surrogates from every text field of a message set.
2096
+ *
2097
+ * A lone surrogate (from mid-pair string slicing or corrupt source data)
2098
+ * serializes to a bare `\udXXX` escape that strict JSON parsers — including
2099
+ * the one on Anthropic's API — reject with "no low surrogate in string",
2100
+ * failing the whole request. Sanitizing here, at the single boundary every
2101
+ * provider call flows through, guarantees no request can carry one.
2102
+ */
2103
+ private static _sanitizeMessages;
2104
+ /**
2105
+ * Log cache usage metrics from Anthropic API response
2106
+ * Shows cache hits, costs, and savings
2107
+ */
2108
+ private static _logCacheUsage;
2109
+ /**
2110
+ * Parse model string to extract provider and model name
2111
+ * @param modelString - Format: "provider/model-name" or just "model-name"
2112
+ * @returns [provider, modelName]
2113
+ *
2114
+ * @example
2115
+ * "anthropic/claude-sonnet-4-5" → ["anthropic", "claude-sonnet-4-5"]
2116
+ * "groq/openai/gpt-oss-120b" → ["groq", "openai/gpt-oss-120b"]
2117
+ * "claude-sonnet-4-5" → ["anthropic", "claude-sonnet-4-5"] (default)
2118
+ */
2119
+ private static _parseModel;
2120
+ private static _anthropicText;
2121
+ private static _anthropicStream;
2122
+ private static _anthropicStreamWithTools;
2123
+ private static _groqText;
2124
+ private static _groqStream;
2125
+ private static _geminiText;
2126
+ private static _geminiStream;
2127
+ /**
2128
+ * Recursively strip unsupported JSON Schema properties for Gemini
2129
+ * Gemini doesn't support: additionalProperties, $schema, etc.
2130
+ */
2131
+ private static _cleanSchemaForGemini;
2132
+ private static _geminiStreamWithTools;
2133
+ private static _openaiText;
2134
+ private static _openaiStream;
2135
+ private static _openaiStreamWithTools;
2136
+ /**
2137
+ * Parse JSON string, handling markdown code blocks and surrounding text
2138
+ * Enhanced version with jsonrepair to handle malformed JSON from LLMs
2139
+ * @param text - Text that may contain JSON wrapped in ```json...``` or with surrounding text
2140
+ * @returns Parsed JSON object or array
2141
+ */
2142
+ private static _parseJSON;
2143
+ }
2144
+
2145
+ interface CapturedLog {
2146
+ timestamp: number;
2147
+ level: 'info' | 'error' | 'warn' | 'debug';
2148
+ message: string;
2149
+ type?: 'explanation' | 'query' | 'general';
2150
+ data?: Record<string, any>;
2151
+ }
2152
+ /**
2153
+ * UILogCollector captures logs during user prompt processing
2154
+ * and sends them to runtime via ui_logs message with uiBlockId as the message id
2155
+ * Logs are sent in real-time for streaming effect in the UI
2156
+ * Respects the global log level configuration
2157
+ */
2158
+ declare class UILogCollector {
2159
+ private logs;
2160
+ private uiBlockId;
2161
+ private clientId;
2162
+ private sendMessage;
2163
+ private currentLogLevel;
2164
+ constructor(clientId: string, sendMessage: (message: Message) => void, uiBlockId?: string);
2165
+ /**
2166
+ * Check if logging is enabled (uiBlockId is provided)
2167
+ */
2168
+ isEnabled(): boolean;
2169
+ /**
2170
+ * Check if a message should be logged based on current log level
2171
+ */
2172
+ private shouldLog;
2173
+ /**
2174
+ * Add a log entry with timestamp and immediately send to runtime
2175
+ * Only logs that pass the log level filter are captured and sent
2176
+ */
2177
+ private addLog;
2178
+ /**
2179
+ * Send a single log to runtime immediately
2180
+ */
2181
+ private sendLogImmediately;
2182
+ /**
2183
+ * Log info message
2184
+ */
2185
+ info(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
2186
+ /**
2187
+ * Log error message
2188
+ */
2189
+ error(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
2190
+ /**
2191
+ * Log warning message
2192
+ */
2193
+ warn(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
2194
+ /**
2195
+ * Log debug message
2196
+ */
2197
+ debug(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
2198
+ /**
2199
+ * Log LLM explanation with typed metadata
2200
+ */
2201
+ logExplanation(message: string, explanation: string, data?: Record<string, any>): void;
2202
+ /**
2203
+ * Log generated query with typed metadata
2204
+ */
2205
+ logQuery(message: string, query: string, data?: Record<string, any>): void;
2206
+ /**
2207
+ * Send all collected logs at once (optional, for final summary)
2208
+ */
2209
+ sendAllLogs(): void;
2210
+ /**
2211
+ * Get all collected logs
2212
+ */
2213
+ getLogs(): CapturedLog[];
2214
+ /**
2215
+ * Clear all logs
2216
+ */
2217
+ clearLogs(): void;
2218
+ /**
2219
+ * Set uiBlockId (in case it's provided later)
2220
+ */
2221
+ setUIBlockId(uiBlockId: string): void;
2222
+ }
2223
+
2224
+ /**
2225
+ * UIBlock represents a single user and assistant message block in a thread
2226
+ * Contains user question, component metadata, component data, text response, and available actions
2227
+ */
2228
+ declare class UIBlock {
2229
+ private id;
968
2230
  private userQuestion;
969
2231
  private generatedComponentMetadata;
970
2232
  private componentData;
@@ -972,339 +2234,1040 @@ declare class UIBlock {
972
2234
  private actions;
973
2235
  private createdAt;
974
2236
  /**
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
2237
+ * Creates a new UIBlock instance
2238
+ * @param userQuestion - The user's question or input
2239
+ * @param componentData - The component data object
2240
+ * @param generatedComponentMetadata - Optional metadata about the generated component
2241
+ * @param actions - Optional array of available actions
2242
+ * @param id - Optional custom ID, generates UUID if not provided
2243
+ * @param textResponse - Optional text response from LLM
2244
+ */
2245
+ constructor(userQuestion: string, componentData?: Record<string, any>, generatedComponentMetadata?: Record<string, any>, actions?: Action[], id?: string, textResponse?: string | null);
2246
+ /**
2247
+ * Get the UIBlock ID
2248
+ */
2249
+ getId(): string;
2250
+ /**
2251
+ * Get the user question
2252
+ */
2253
+ getUserQuestion(): string;
2254
+ /**
2255
+ * Set or update the user question
2256
+ */
2257
+ setUserQuestion(question: string): void;
2258
+ /**
2259
+ * Get component metadata
2260
+ */
2261
+ getComponentMetadata(): Record<string, any>;
2262
+ getTextResponse(): string;
2263
+ /**
2264
+ * Set or update component metadata
2265
+ */
2266
+ setComponentMetadata(metadata: Record<string, any>): void;
2267
+ /**
2268
+ * Get component data
2269
+ */
2270
+ getComponentData(): Record<string, any>;
2271
+ /**
2272
+ * Calculate size of data in bytes
2273
+ */
2274
+ private getDataSizeInBytes;
2275
+ /**
2276
+ * Limit array data to maximum rows
2277
+ */
2278
+ private limitArrayData;
2279
+ /**
2280
+ * Check if data exceeds size limit
2281
+ */
2282
+ private exceedsSizeLimit;
2283
+ /**
2284
+ * Process and limit data before storing
2285
+ */
2286
+ private processDataForStorage;
2287
+ /**
2288
+ * Set or update component data with size and row limits
2289
+ */
2290
+ setComponentData(data: Record<string, any>): void;
2291
+ /**
2292
+ * Set or update text response
2293
+ */
2294
+ setTextResponse(textResponse: string | null): void;
2295
+ /**
2296
+ * Get all actions (only if they are resolved, not if fetching)
2297
+ */
2298
+ getActions(): Action[] | null | Promise<Action[]>;
2299
+ /**
2300
+ * Get or fetch actions
2301
+ * If actions don't exist or are a Promise, calls the generateFn and stores the promise
2302
+ * If actions already exist, returns them
2303
+ * @param generateFn - Async function to generate actions
2304
+ * @returns Promise resolving to Action[]
2305
+ */
2306
+ getOrFetchActions(generateFn: () => Promise<Action[]>): Promise<Action[]>;
2307
+ /**
2308
+ * Set or replace all actions
2309
+ */
2310
+ setActions(actions: Action[]): void;
2311
+ /**
2312
+ * Add a single action (only if actions are resolved)
2313
+ */
2314
+ addAction(action: Action): void;
2315
+ /**
2316
+ * Add multiple actions (only if actions are resolved)
2317
+ */
2318
+ addActions(actions: Action[]): void;
2319
+ /**
2320
+ * Remove an action by ID (only if actions are resolved)
2321
+ */
2322
+ removeAction(actionId: string): boolean;
2323
+ /**
2324
+ * Clear all actions
2325
+ */
2326
+ clearActions(): void;
2327
+ /**
2328
+ * Get creation timestamp
2329
+ */
2330
+ getCreatedAt(): Date;
2331
+ /**
2332
+ * Convert UIBlock to JSON-serializable object
2333
+ */
2334
+ toJSON(): Record<string, any>;
2335
+ }
2336
+
2337
+ /**
2338
+ * Thread represents a conversation thread containing multiple UIBlocks
2339
+ * Each UIBlock in a thread represents a user question and assistant response pair
2340
+ */
2341
+ declare class Thread {
2342
+ private id;
2343
+ private uiblocks;
2344
+ private createdAt;
2345
+ /**
2346
+ * Creates a new Thread instance
2347
+ * @param id - Optional custom ID, generates UUID if not provided
2348
+ */
2349
+ constructor(id?: string);
2350
+ /**
2351
+ * Get the thread ID
2352
+ */
2353
+ getId(): string;
2354
+ /**
2355
+ * Add a UIBlock to the thread
2356
+ */
2357
+ addUIBlock(uiblock: UIBlock): void;
2358
+ /**
2359
+ * Get a UIBlock by ID
2360
+ */
2361
+ getUIBlock(id: string): UIBlock | undefined;
2362
+ /**
2363
+ * Get all UIBlocks in the thread
2364
+ */
2365
+ getUIBlocks(): UIBlock[];
2366
+ /**
2367
+ * Get UIBlocks as a Map
2368
+ */
2369
+ getUIBlocksMap(): Map<string, UIBlock>;
2370
+ /**
2371
+ * Remove a UIBlock by ID
2372
+ */
2373
+ removeUIBlock(id: string): boolean;
2374
+ /**
2375
+ * Check if UIBlock exists
2376
+ */
2377
+ hasUIBlock(id: string): boolean;
2378
+ /**
2379
+ * Get number of UIBlocks in the thread
2380
+ */
2381
+ getUIBlockCount(): number;
2382
+ /**
2383
+ * Clear all UIBlocks from the thread
2384
+ */
2385
+ clear(): void;
2386
+ /**
2387
+ * Get creation timestamp
2388
+ */
2389
+ getCreatedAt(): Date;
2390
+ /**
2391
+ * Get conversation context from recent UIBlocks (excluding current one)
2392
+ * Returns formatted string with previous questions and component summaries
2393
+ * @param limit - Maximum number of previous UIBlocks to include (default: 5)
2394
+ * @param currentUIBlockId - ID of current UIBlock to exclude from context (optional)
2395
+ * @returns Formatted conversation history string
2396
+ */
2397
+ getConversationContext(limit?: number, currentUIBlockId?: string): string;
2398
+ /**
2399
+ * Convert Thread to JSON-serializable object
2400
+ */
2401
+ toJSON(): Record<string, any>;
2402
+ }
2403
+
2404
+ /**
2405
+ * ThreadManager manages all threads globally
2406
+ * Provides methods to create, retrieve, and delete threads.
2407
+ * Includes automatic cleanup to prevent unbounded memory growth.
2408
+ */
2409
+ declare class ThreadManager {
2410
+ private static instance;
2411
+ private threads;
2412
+ private cleanupInterval;
2413
+ private readonly threadTtlMs;
2414
+ private constructor();
2415
+ /**
2416
+ * Periodically remove threads older than 7 days.
2417
+ * Runs every hour to avoid frequent iteration over the map.
2418
+ */
2419
+ private startCleanup;
2420
+ /**
2421
+ * Get singleton instance of ThreadManager
2422
+ */
2423
+ static getInstance(): ThreadManager;
2424
+ /**
2425
+ * Create a new thread
980
2426
  * @param id - Optional custom ID, generates UUID if not provided
981
- * @param textResponse - Optional text response from LLM
2427
+ * @returns The created Thread instance
982
2428
  */
983
- constructor(userQuestion: string, componentData?: Record<string, any>, generatedComponentMetadata?: Record<string, any>, actions?: Action[], id?: string, textResponse?: string | null);
2429
+ createThread(id?: string): Thread;
984
2430
  /**
985
- * Get the UIBlock ID
2431
+ * Get a thread by ID
986
2432
  */
987
- getId(): string;
2433
+ getThread(id: string): Thread | undefined;
988
2434
  /**
989
- * Get the user question
2435
+ * Get all threads
990
2436
  */
991
- getUserQuestion(): string;
2437
+ getAllThreads(): Thread[];
992
2438
  /**
993
- * Set or update the user question
2439
+ * Get threads as a Map
994
2440
  */
995
- setUserQuestion(question: string): void;
2441
+ getThreadsMap(): Map<string, Thread>;
996
2442
  /**
997
- * Get component metadata
2443
+ * Delete a thread by ID
998
2444
  */
999
- getComponentMetadata(): Record<string, any>;
1000
- getTextResponse(): string;
2445
+ deleteThread(id: string): boolean;
1001
2446
  /**
1002
- * Set or update component metadata
2447
+ * Check if thread exists
1003
2448
  */
1004
- setComponentMetadata(metadata: Record<string, any>): void;
2449
+ hasThread(id: string): boolean;
1005
2450
  /**
1006
- * Get component data
2451
+ * Get number of threads
1007
2452
  */
1008
- getComponentData(): Record<string, any>;
2453
+ getThreadCount(): number;
1009
2454
  /**
1010
- * Calculate size of data in bytes
2455
+ * Clear all threads
1011
2456
  */
1012
- private getDataSizeInBytes;
2457
+ clearAll(): void;
1013
2458
  /**
1014
- * Limit array data to maximum rows
2459
+ * Find a UIBlock by ID across all threads
2460
+ * @param uiBlockId - The UIBlock ID to search for
2461
+ * @returns Object with thread and uiBlock if found, undefined otherwise
1015
2462
  */
1016
- private limitArrayData;
2463
+ findUIBlockById(uiBlockId: string): {
2464
+ thread: Thread;
2465
+ uiBlock: UIBlock;
2466
+ } | undefined;
1017
2467
  /**
1018
- * Check if data exceeds size limit
2468
+ * Convert all threads to JSON-serializable object
1019
2469
  */
1020
- private exceedsSizeLimit;
2470
+ toJSON(): Record<string, any>;
2471
+ }
2472
+
2473
+ /**
2474
+ * CleanupService handles cleanup of old threads and UIBlocks
2475
+ * to prevent memory bloat and maintain optimal performance
2476
+ */
2477
+ declare class CleanupService {
2478
+ private static instance;
2479
+ private cleanupInterval;
2480
+ private constructor();
1021
2481
  /**
1022
- * Process and limit data before storing
2482
+ * Get singleton instance of CleanupService
1023
2483
  */
1024
- private processDataForStorage;
2484
+ static getInstance(): CleanupService;
2485
+ /**
2486
+ * Clean up old threads based on retention period
2487
+ * @param retentionDays - Number of days to keep threads (defaults to config)
2488
+ * @returns Number of threads deleted
2489
+ */
2490
+ cleanupOldThreads(retentionDays?: number): number;
2491
+ /**
2492
+ * Clean up old UIBlocks within threads based on retention period
2493
+ * @param retentionDays - Number of days to keep UIBlocks (defaults to config)
2494
+ * @returns Object with number of UIBlocks deleted per thread
2495
+ */
2496
+ cleanupOldUIBlocks(retentionDays?: number): {
2497
+ [threadId: string]: number;
2498
+ };
2499
+ /**
2500
+ * Clear all component data from UIBlocks to free memory
2501
+ * Keeps metadata but removes the actual data
2502
+ * @param retentionDays - Number of days to keep full data (defaults to config)
2503
+ * @returns Number of UIBlocks whose data was cleared
2504
+ */
2505
+ clearOldUIBlockData(retentionDays?: number): number;
2506
+ /**
2507
+ * Run full cleanup (threads, UIBlocks, and data)
2508
+ * @returns Cleanup statistics
2509
+ */
2510
+ runFullCleanup(): {
2511
+ threadsDeleted: number;
2512
+ uiblocksDeleted: {
2513
+ [threadId: string]: number;
2514
+ };
2515
+ dataCleared: number;
2516
+ };
2517
+ /**
2518
+ * Start automatic cleanup at regular intervals
2519
+ * @param intervalHours - Hours between cleanup runs (default: 24)
2520
+ */
2521
+ startAutoCleanup(intervalHours?: number): void;
2522
+ /**
2523
+ * Stop automatic cleanup
2524
+ */
2525
+ stopAutoCleanup(): void;
2526
+ /**
2527
+ * Check if auto cleanup is running
2528
+ */
2529
+ isAutoCleanupRunning(): boolean;
2530
+ /**
2531
+ * Get current memory usage statistics
2532
+ */
2533
+ getMemoryStats(): {
2534
+ threadCount: number;
2535
+ totalUIBlocks: number;
2536
+ avgUIBlocksPerThread: number;
2537
+ };
2538
+ }
2539
+
2540
+ /**
2541
+ * Configuration for data storage limits in UIBlocks
2542
+ */
2543
+ declare const STORAGE_CONFIG: {
2544
+ /**
2545
+ * Maximum number of rows to store in UIBlock data
2546
+ */
2547
+ MAX_ROWS_PER_BLOCK: number;
2548
+ /**
2549
+ * Maximum size in bytes per UIBlock (500KB - reduced to save memory)
2550
+ */
2551
+ MAX_SIZE_PER_BLOCK_BYTES: number;
2552
+ /**
2553
+ * Number of days to keep threads before cleanup
2554
+ * Note: This is for in-memory storage. Conversations are also persisted to database.
2555
+ */
2556
+ THREAD_RETENTION_DAYS: number;
2557
+ /**
2558
+ * Number of days to keep UIBlocks before cleanup
2559
+ * Note: This is for in-memory storage. Data is also persisted to database.
2560
+ */
2561
+ UIBLOCK_RETENTION_DAYS: number;
2562
+ };
2563
+
2564
+ /**
2565
+ * Configuration for conversation context and history management
2566
+ */
2567
+ declare const CONTEXT_CONFIG: {
2568
+ /**
2569
+ * Maximum number of previous UIBlocks to include as conversation context
2570
+ * Set to 0 to disable conversation history
2571
+ * Higher values provide more context but may increase token usage
2572
+ */
2573
+ MAX_CONVERSATION_CONTEXT_BLOCKS: number;
2574
+ };
2575
+
2576
+ /**
2577
+ * LLM Usage Logger - Tracks token usage, costs, and timing for all LLM API calls
2578
+ */
2579
+ interface LLMUsageEntry {
2580
+ timestamp: string;
2581
+ requestId: string;
2582
+ provider: string;
2583
+ model: string;
2584
+ method: string;
2585
+ inputTokens: number;
2586
+ outputTokens: number;
2587
+ cacheReadTokens?: number;
2588
+ cacheWriteTokens?: number;
2589
+ totalTokens: number;
2590
+ costUSD: number;
2591
+ durationMs: number;
2592
+ toolCalls?: number;
2593
+ success: boolean;
2594
+ error?: string;
2595
+ }
2596
+ declare class LLMUsageLogger {
2597
+ private logStream;
2598
+ private logPath;
2599
+ private enabled;
2600
+ private sessionStats;
2601
+ constructor();
2602
+ private initLogStream;
2603
+ private writeHeader;
2604
+ /**
2605
+ * Calculate cost based on token usage and model
2606
+ */
2607
+ calculateCost(model: string, inputTokens: number, outputTokens: number, cacheReadTokens?: number, cacheWriteTokens?: number): number;
2608
+ /**
2609
+ * Log an LLM API call
2610
+ */
2611
+ log(entry: LLMUsageEntry): void;
2612
+ /**
2613
+ * Log session summary (call at end of request)
2614
+ */
2615
+ logSessionSummary(requestContext?: string): void;
2616
+ /**
2617
+ * Reset session stats (call at start of new user request)
2618
+ */
2619
+ resetSession(): void;
2620
+ /**
2621
+ * Reset the log file for a new request (clears previous logs)
2622
+ * Call this at the start of each USER_PROMPT_REQ
2623
+ */
2624
+ resetLogFile(requestContext?: string): void;
2625
+ /**
2626
+ * Get current session stats
2627
+ */
2628
+ getSessionStats(): {
2629
+ totalCalls: number;
2630
+ totalInputTokens: number;
2631
+ totalOutputTokens: number;
2632
+ totalCacheReadTokens: number;
2633
+ totalCacheWriteTokens: number;
2634
+ totalCostUSD: number;
2635
+ totalDurationMs: number;
2636
+ };
2637
+ /**
2638
+ * Generate a unique request ID
2639
+ */
2640
+ generateRequestId(): string;
2641
+ }
2642
+ declare const llmUsageLogger: LLMUsageLogger;
2643
+
2644
+ /**
2645
+ * User Prompt Error Logger - Captures detailed errors for USER_PROMPT_REQ
2646
+ * Logs full error details including raw strings for parse failures
2647
+ */
2648
+ declare class UserPromptErrorLogger {
2649
+ private logStream;
2650
+ private logPath;
2651
+ private enabled;
2652
+ private hasErrors;
2653
+ constructor();
2654
+ /**
2655
+ * Reset the error log file for a new request
2656
+ */
2657
+ resetLogFile(requestContext?: string): void;
2658
+ /**
2659
+ * Log a JSON parse error with the raw string that failed
2660
+ */
2661
+ logJsonParseError(context: string, rawString: string, error: Error): void;
2662
+ /**
2663
+ * Log a general error with full details
2664
+ */
2665
+ logError(context: string, error: Error | string, additionalData?: Record<string, any>): void;
2666
+ /**
2667
+ * Log a SQL query error with the full query
2668
+ */
2669
+ logSqlError(query: string, error: Error | string, params?: any[]): void;
2670
+ /**
2671
+ * Log an LLM API error
2672
+ */
2673
+ logLlmError(provider: string, model: string, method: string, error: Error | string, requestData?: any): void;
2674
+ /**
2675
+ * Log tool execution error
2676
+ */
2677
+ logToolError(toolName: string, toolInput: any, error: Error | string): void;
2678
+ /**
2679
+ * Write final summary if there were errors
2680
+ */
2681
+ writeSummary(): void;
2682
+ /**
2683
+ * Check if any errors were logged
2684
+ */
2685
+ hadErrors(): boolean;
2686
+ private write;
2687
+ }
2688
+ declare const userPromptErrorLogger: UserPromptErrorLogger;
2689
+
2690
+ /**
2691
+ * BM25L Reranker for hybrid semantic search
2692
+ *
2693
+ * BM25L is an improved variant of BM25 that provides better handling of
2694
+ * long documents and term frequency saturation. This implementation is
2695
+ * designed to rerank semantic search results from ChromaDB.
2696
+ *
2697
+ * The hybrid approach combines:
2698
+ * 1. Semantic similarity from ChromaDB embeddings (dense vectors)
2699
+ * 2. Lexical matching from BM25L (sparse, keyword-based)
2700
+ *
2701
+ * This addresses the weakness of pure semantic search which may miss
2702
+ * exact keyword matches that are important for user intent.
2703
+ */
2704
+ interface BM25LOptions {
2705
+ /** Term frequency saturation parameter (default: 1.5) */
2706
+ k1?: number;
2707
+ /** Length normalization parameter (default: 0.75) */
2708
+ b?: number;
2709
+ /** Lower-bound adjustment from BM25L paper (default: 0.5) */
2710
+ delta?: number;
2711
+ }
2712
+ interface RerankedResult<T> {
2713
+ item: T;
2714
+ originalIndex: number;
2715
+ semanticScore: number;
2716
+ bm25Score: number;
2717
+ hybridScore: number;
2718
+ }
2719
+ interface HybridSearchOptions extends BM25LOptions {
2720
+ /** Weight for semantic score (0-1, default: 0.7) */
2721
+ semanticWeight?: number;
2722
+ /** Weight for BM25 score (0-1, default: 0.3) */
2723
+ bm25Weight?: number;
2724
+ /** Minimum hybrid score threshold (0-1, default: 0) */
2725
+ minScore?: number;
2726
+ }
2727
+ /**
2728
+ * BM25L implementation for lexical scoring
2729
+ */
2730
+ declare class BM25L {
2731
+ private k1;
2732
+ private b;
2733
+ private delta;
2734
+ private documents;
2735
+ private docLengths;
2736
+ private avgDocLength;
2737
+ private termDocFreq;
2738
+ /**
2739
+ * @param documents - Array of raw documents (strings)
2740
+ * @param opts - Optional BM25L parameters
2741
+ */
2742
+ constructor(documents?: string[], opts?: BM25LOptions);
2743
+ /**
2744
+ * Tokenize text into lowercase alphanumeric tokens
2745
+ */
2746
+ tokenize(text: string): string[];
2747
+ /**
2748
+ * Compute IDF (Inverse Document Frequency) with smoothing
2749
+ */
2750
+ private idf;
2751
+ /**
2752
+ * Compute BM25L score for a single document
2753
+ */
2754
+ score(query: string, docIndex: number): number;
2755
+ /**
2756
+ * Search and rank all documents
2757
+ */
2758
+ search(query: string): Array<{
2759
+ index: number;
2760
+ score: number;
2761
+ }>;
2762
+ }
2763
+ /**
2764
+ * Hybrid reranker that combines semantic and BM25L scores
2765
+ *
2766
+ * @param query - The search query
2767
+ * @param items - Array of items to rerank
2768
+ * @param getDocument - Function to extract document text from an item
2769
+ * @param getSemanticScore - Function to extract semantic similarity score from an item
2770
+ * @param options - Hybrid search options
2771
+ * @returns Reranked items with hybrid scores
2772
+ */
2773
+ declare function hybridRerank<T>(query: string, items: T[], getDocument: (item: T) => string, getSemanticScore: (item: T) => number, options?: HybridSearchOptions): RerankedResult<T>[];
2774
+ /**
2775
+ * Simple reranking function for ChromaDB results
2776
+ *
2777
+ * This is a convenience wrapper for reranking ChromaDB query results
2778
+ * that follow the standard { ids, documents, metadatas, distances } format.
2779
+ *
2780
+ * @param query - The search query
2781
+ * @param chromaResults - ChromaDB query results
2782
+ * @param options - Hybrid search options
2783
+ * @returns Reranked results with hybrid scores
2784
+ */
2785
+ declare function rerankChromaResults(query: string, chromaResults: {
2786
+ ids: string[][];
2787
+ documents: (string | null)[][];
2788
+ metadatas: Record<string, any>[][];
2789
+ distances: number[][];
2790
+ }, options?: HybridSearchOptions): Array<{
2791
+ id: string;
2792
+ document: string | null;
2793
+ metadata: Record<string, any>;
2794
+ distance: number;
2795
+ semanticScore: number;
2796
+ bm25Score: number;
2797
+ hybridScore: number;
2798
+ }>;
2799
+ /**
2800
+ * Rerank conversation search results specifically
2801
+ *
2802
+ * This function is designed to work with the conversation-history.search collection
2803
+ * where we need to fetch more results initially and then rerank them.
2804
+ *
2805
+ * @param query - The user's search query
2806
+ * @param results - Array of conversation search results from ChromaDB
2807
+ * @param options - Hybrid search options
2808
+ * @returns Reranked results sorted by hybrid score
2809
+ */
2810
+ declare function rerankConversationResults<T extends {
2811
+ userPrompt?: string;
2812
+ similarity?: number;
2813
+ }>(query: string, results: T[], options?: HybridSearchOptions): Array<T & {
2814
+ hybridScore: number;
2815
+ bm25Score: number;
2816
+ }>;
2817
+
2818
+ /**
2819
+ * QueryExecutionService - Handles all query execution, validation, and retry logic
2820
+ * Extracted from BaseLLM for better separation of concerns
2821
+ */
2822
+
2823
+ /**
2824
+ * Context for component when requesting query fix
2825
+ */
2826
+ interface ComponentContext {
2827
+ name: string;
2828
+ type: string;
2829
+ title?: string;
2830
+ }
2831
+ /**
2832
+ * Result of query validation
2833
+ */
2834
+ interface QueryValidationResult {
2835
+ component: Component | null;
2836
+ queryKey: string;
2837
+ result: any;
2838
+ validated: boolean;
2839
+ }
2840
+ /**
2841
+ * Result of batch query validation
2842
+ */
2843
+ interface BatchValidationResult {
2844
+ components: Component[];
2845
+ queryResults: Map<string, any>;
2846
+ }
2847
+ /**
2848
+ * Configuration for QueryExecutionService
2849
+ */
2850
+ interface QueryExecutionServiceConfig {
2851
+ defaultLimit: number;
2852
+ getModelForTask: (taskType: 'simple' | 'complex') => string;
2853
+ getApiKey: (apiKey?: string) => string | undefined;
2854
+ providerName: string;
2855
+ }
2856
+ /**
2857
+ * QueryExecutionService handles all query-related operations
2858
+ */
2859
+ declare class QueryExecutionService {
2860
+ private config;
2861
+ constructor(config: QueryExecutionServiceConfig);
1025
2862
  /**
1026
- * Set or update component data with size and row limits
2863
+ * Get the cache key for a query
2864
+ * This ensures the cache key matches what the frontend will send
1027
2865
  */
1028
- setComponentData(data: Record<string, any>): void;
2866
+ getQueryCacheKey(query: any): string;
1029
2867
  /**
1030
- * Set or update text response
2868
+ * Execute a query against the database
2869
+ * @param query - The SQL query to execute (string or object with sql/values)
2870
+ * @param collections - Collections object containing database execute function
2871
+ * @returns Object with result data and cache key
1031
2872
  */
1032
- setTextResponse(textResponse: string | null): void;
2873
+ executeQuery(query: any, collections: any): Promise<{
2874
+ result: any;
2875
+ cacheKey: string;
2876
+ }>;
1033
2877
  /**
1034
- * Get all actions (only if they are resolved, not if fetching)
2878
+ * Request the LLM to fix a failed SQL query
2879
+ * @param failedQuery - The query that failed execution
2880
+ * @param errorMessage - The error message from the failed execution
2881
+ * @param componentContext - Context about the component
2882
+ * @param apiKey - Optional API key
2883
+ * @returns Fixed query string
1035
2884
  */
1036
- getActions(): Action[] | null | Promise<Action[]>;
2885
+ requestQueryFix(failedQuery: string, errorMessage: string, componentContext: ComponentContext, apiKey?: string): Promise<string>;
1037
2886
  /**
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[]
2887
+ * Validate a single component's query with retry logic
2888
+ * @param component - The component to validate
2889
+ * @param collections - Collections object containing database execute function
2890
+ * @param apiKey - Optional API key for LLM calls
2891
+ * @returns Validation result with component, query key, and result
1043
2892
  */
1044
- getOrFetchActions(generateFn: () => Promise<Action[]>): Promise<Action[]>;
2893
+ validateSingleQuery(component: Component, collections: any, apiKey?: string): Promise<QueryValidationResult>;
1045
2894
  /**
1046
- * Add a single action (only if actions are resolved)
2895
+ * Validate multiple component queries in parallel
2896
+ * @param components - Array of components with potential queries
2897
+ * @param collections - Collections object containing database execute function
2898
+ * @param apiKey - Optional API key for LLM calls
2899
+ * @returns Object with validated components and query results map
1047
2900
  */
1048
- addAction(action: Action): void;
2901
+ validateComponentQueries(components: Component[], collections: any, apiKey?: string): Promise<BatchValidationResult>;
2902
+ }
2903
+
2904
+ /**
2905
+ * Task types for model selection
2906
+ * - 'complex': Text generation, component matching, parameter adaptation (uses best model in balanced mode)
2907
+ * - 'simple': Classification, action generation (uses fast model in balanced mode)
2908
+ */
2909
+ type TaskType = 'complex' | 'simple';
2910
+ interface BaseLLMConfig {
2911
+ model?: string;
2912
+ fastModel?: string;
2913
+ defaultLimit?: number;
2914
+ apiKey?: string;
1049
2915
  /**
1050
- * Add multiple actions (only if actions are resolved)
2916
+ * Model selection strategy:
2917
+ * - 'best': Use best model for all tasks (highest quality, higher cost)
2918
+ * - 'fast': Use fast model for all tasks (lower quality, lower cost)
2919
+ * - 'balanced': Use best model for complex tasks, fast model for simple tasks (default)
1051
2920
  */
1052
- addActions(actions: Action[]): void;
2921
+ modelStrategy?: ModelStrategy;
2922
+ conversationSimilarityThreshold?: number;
2923
+ }
2924
+ /**
2925
+ * BaseLLM abstract class for AI-powered component generation and matching
2926
+ * Provides common functionality for all LLM providers
2927
+ */
2928
+ declare abstract class BaseLLM {
2929
+ protected model: string;
2930
+ protected fastModel: string;
2931
+ protected defaultLimit: number;
2932
+ protected apiKey?: string;
2933
+ protected modelStrategy: ModelStrategy;
2934
+ protected conversationSimilarityThreshold: number;
2935
+ protected queryService: QueryExecutionService;
2936
+ constructor(config?: BaseLLMConfig);
1053
2937
  /**
1054
- * Remove an action by ID (only if actions are resolved)
2938
+ * Get the appropriate model based on task type and model strategy
2939
+ * @param taskType - 'complex' for text generation/matching, 'simple' for classification/actions
2940
+ * @returns The model string to use for this task
1055
2941
  */
1056
- removeAction(actionId: string): boolean;
2942
+ protected getModelForTask(taskType: TaskType): string;
1057
2943
  /**
1058
- * Clear all actions
2944
+ * Set the model strategy at runtime
2945
+ * @param strategy - 'best', 'fast', or 'balanced'
1059
2946
  */
1060
- clearActions(): void;
2947
+ setModelStrategy(strategy: ModelStrategy): void;
1061
2948
  /**
1062
- * Get creation timestamp
2949
+ * Get the current model strategy
2950
+ * @returns The current model strategy
1063
2951
  */
1064
- getCreatedAt(): Date;
2952
+ getModelStrategy(): ModelStrategy;
1065
2953
  /**
1066
- * Convert UIBlock to JSON-serializable object
2954
+ * Set the conversation similarity threshold at runtime
2955
+ * @param threshold - Value between 0 and 1 (e.g., 0.8 = 80% similarity required)
1067
2956
  */
1068
- toJSON(): Record<string, any>;
1069
- }
1070
-
1071
- /**
1072
- * Thread represents a conversation thread containing multiple UIBlocks
1073
- * Each UIBlock in a thread represents a user question and assistant response pair
1074
- */
1075
- declare class Thread {
1076
- private id;
1077
- private uiblocks;
1078
- private createdAt;
2957
+ setConversationSimilarityThreshold(threshold: number): void;
1079
2958
  /**
1080
- * Creates a new Thread instance
1081
- * @param id - Optional custom ID, generates UUID if not provided
2959
+ * Get the current conversation similarity threshold
2960
+ * @returns The current threshold value
1082
2961
  */
1083
- constructor(id?: string);
2962
+ getConversationSimilarityThreshold(): number;
1084
2963
  /**
1085
- * Get the thread ID
2964
+ * Get the default model for this provider (used for complex tasks like text generation)
1086
2965
  */
1087
- getId(): string;
2966
+ protected abstract getDefaultModel(): string;
1088
2967
  /**
1089
- * Add a UIBlock to the thread
2968
+ * Get the default fast model for this provider (used for simple tasks: classification, matching, actions)
2969
+ * Should return a cheaper/faster model like Haiku for Anthropic
1090
2970
  */
1091
- addUIBlock(uiblock: UIBlock): void;
2971
+ protected abstract getDefaultFastModel(): string;
1092
2972
  /**
1093
- * Get a UIBlock by ID
2973
+ * Get the default API key from environment
1094
2974
  */
1095
- getUIBlock(id: string): UIBlock | undefined;
2975
+ protected abstract getDefaultApiKey(): string | undefined;
1096
2976
  /**
1097
- * Get all UIBlocks in the thread
2977
+ * Get the provider name (for logging)
1098
2978
  */
1099
- getUIBlocks(): UIBlock[];
2979
+ protected abstract getProviderName(): string;
1100
2980
  /**
1101
- * Get UIBlocks as a Map
2981
+ * Get the API key (from instance, parameter, or environment)
1102
2982
  */
1103
- getUIBlocksMap(): Map<string, UIBlock>;
2983
+ protected getApiKey(apiKey?: string): string | undefined;
1104
2984
  /**
1105
- * Remove a UIBlock by ID
2985
+ * Check if a component contains a Form (data_modification component)
2986
+ * Forms have hardcoded defaultValues that become stale when cached
2987
+ * This checks both single Form components and Forms inside MultiComponentContainer
1106
2988
  */
1107
- removeUIBlock(id: string): boolean;
2989
+ protected containsFormComponent(component: any): boolean;
1108
2990
  /**
1109
- * Check if UIBlock exists
2991
+ * Match components from text response suggestions and generate follow-up questions
2992
+ * Takes a text response with component suggestions (c1:type format) and matches with available components
2993
+ * Also generates title, description, and intelligent follow-up questions (actions) based on the analysis
2994
+ * All components are placed in a default MultiComponentContainer layout
2995
+ * @param analysisContent - The text response containing component suggestions
2996
+ * @param components - List of available components
2997
+ * @param apiKey - Optional API key
2998
+ * @param componentStreamCallback - Optional callback to stream primary KPI component as soon as it's identified
2999
+ * @returns Object containing matched components, layout title/description, and follow-up actions
1110
3000
  */
1111
- hasUIBlock(id: string): boolean;
3001
+ matchComponentsFromAnalysis(analysisContent: string, components: Component[], userPrompt: string, apiKey?: string, componentStreamCallback?: (component: Component) => void, deferredTools?: any[], executedTools?: any[], collections?: any, userId?: string): Promise<{
3002
+ components: Component[];
3003
+ layoutTitle: string;
3004
+ layoutDescription: string;
3005
+ actions: Action[];
3006
+ }>;
1112
3007
  /**
1113
- * Get number of UIBlocks in the thread
1114
- */
1115
- getUIBlockCount(): number;
3008
+ * Classify user question into category and detect external tools needed
3009
+ * Determines if question is for data analysis, requires external tools, or needs text response
3010
+ */
3011
+ classifyQuestionCategory(userPrompt: string, apiKey?: string, conversationHistory?: string, externalTools?: any[]): Promise<{
3012
+ category: 'data_analysis' | 'data_modification' | 'general';
3013
+ externalTools: Array<{
3014
+ type: string;
3015
+ name: string;
3016
+ description: string;
3017
+ parameters: Record<string, any>;
3018
+ }>;
3019
+ dataAnalysisType?: 'visualization' | 'calculation' | 'comparison' | 'trend';
3020
+ reasoning: string;
3021
+ confidence: number;
3022
+ }>;
1116
3023
  /**
1117
- * Clear all UIBlocks from the thread
1118
- */
1119
- clear(): void;
3024
+ * Adapt UI block parameters based on current user question
3025
+ * Takes a matched UI block from semantic search and modifies its props to answer the new question
3026
+ * Also adapts the cached text response to match the new question
3027
+ */
3028
+ adaptUIBlockParameters(currentUserPrompt: string, originalUserPrompt: string, matchedUIBlock: any, apiKey?: string, cachedTextResponse?: string): Promise<{
3029
+ success: boolean;
3030
+ adaptedComponent?: Component;
3031
+ adaptedTextResponse?: string;
3032
+ parametersChanged?: Array<{
3033
+ field: string;
3034
+ reason: string;
3035
+ }>;
3036
+ explanation: string;
3037
+ }>;
1120
3038
  /**
1121
- * Get creation timestamp
3039
+ * Generate text-based response for user question
3040
+ * This provides conversational text responses instead of component generation
3041
+ * Supports tool calling for query execution with automatic retry on errors (max 3 attempts)
3042
+ * After generating text response, if components are provided, matches suggested components
1122
3043
  */
1123
- getCreatedAt(): Date;
3044
+ 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>;
1124
3045
  /**
1125
- * Get conversation context from recent UIBlocks (excluding current one)
1126
- * Returns formatted string with previous questions and component summaries
1127
- * @param limit - Maximum number of previous UIBlocks to include (default: 5)
1128
- * @param currentUIBlockId - ID of current UIBlock to exclude from context (optional)
1129
- * @returns Formatted conversation history string
3046
+ * Main orchestration function with semantic search and multi-step classification
3047
+ * NEW FLOW (Recommended):
3048
+ * 1. Semantic search: Check previous conversations (>60% match)
3049
+ * - If match found Adapt UI block parameters and return
3050
+ * 2. Category classification: Determine if data_analysis, requires_external_tools, or text_response
3051
+ * 3. Route appropriately based on category and response mode
1130
3052
  */
1131
- getConversationContext(limit?: number, currentUIBlockId?: string): string;
3053
+ 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>;
1132
3054
  /**
1133
- * Convert Thread to JSON-serializable object
3055
+ * Generate next questions that the user might ask based on the original prompt and generated component
3056
+ * This helps provide intelligent suggestions for follow-up queries
3057
+ * For general/conversational questions without components, pass textResponse instead
1134
3058
  */
1135
- toJSON(): Record<string, any>;
3059
+ generateNextQuestions(originalUserPrompt: string, component?: Component | null, componentData?: Record<string, unknown>, apiKey?: string, conversationHistory?: string, textResponse?: string): Promise<string[]>;
1136
3060
  }
1137
3061
 
3062
+ interface AnthropicLLMConfig extends BaseLLMConfig {
3063
+ }
1138
3064
  /**
1139
- * ThreadManager manages all threads globally
1140
- * Provides methods to create, retrieve, and delete threads
3065
+ * AnthropicLLM class for handling AI-powered component generation and matching using Anthropic Claude
1141
3066
  */
1142
- declare class ThreadManager {
1143
- private static instance;
1144
- private threads;
1145
- private constructor();
1146
- /**
1147
- * Get singleton instance of ThreadManager
1148
- */
1149
- static getInstance(): ThreadManager;
1150
- /**
1151
- * Create a new thread
1152
- * @param id - Optional custom ID, generates UUID if not provided
1153
- * @returns The created Thread instance
1154
- */
1155
- createThread(id?: string): Thread;
3067
+ declare class AnthropicLLM extends BaseLLM {
3068
+ constructor(config?: AnthropicLLMConfig);
3069
+ protected getDefaultModel(): string;
3070
+ protected getDefaultFastModel(): string;
3071
+ protected getDefaultApiKey(): string | undefined;
3072
+ protected getProviderName(): string;
3073
+ }
3074
+ declare const anthropicLLM: AnthropicLLM;
3075
+
3076
+ interface GroqLLMConfig extends BaseLLMConfig {
3077
+ }
3078
+ /**
3079
+ * GroqLLM class for handling AI-powered component generation and matching using Groq
3080
+ */
3081
+ declare class GroqLLM extends BaseLLM {
3082
+ constructor(config?: GroqLLMConfig);
3083
+ protected getDefaultModel(): string;
3084
+ protected getDefaultFastModel(): string;
3085
+ protected getDefaultApiKey(): string | undefined;
3086
+ protected getProviderName(): string;
3087
+ }
3088
+ declare const groqLLM: GroqLLM;
3089
+
3090
+ interface GeminiLLMConfig extends BaseLLMConfig {
3091
+ }
3092
+ /**
3093
+ * GeminiLLM class for handling AI-powered component generation and matching using Google Gemini
3094
+ */
3095
+ declare class GeminiLLM extends BaseLLM {
3096
+ constructor(config?: GeminiLLMConfig);
3097
+ protected getDefaultModel(): string;
3098
+ protected getDefaultFastModel(): string;
3099
+ protected getDefaultApiKey(): string | undefined;
3100
+ protected getProviderName(): string;
3101
+ }
3102
+ declare const geminiLLM: GeminiLLM;
3103
+
3104
+ interface OpenAILLMConfig extends BaseLLMConfig {
3105
+ }
3106
+ /**
3107
+ * OpenAILLM class for handling AI-powered component generation and matching using OpenAI GPT models
3108
+ */
3109
+ declare class OpenAILLM extends BaseLLM {
3110
+ constructor(config?: OpenAILLMConfig);
3111
+ protected getDefaultModel(): string;
3112
+ protected getDefaultFastModel(): string;
3113
+ protected getDefaultApiKey(): string | undefined;
3114
+ protected getProviderName(): string;
3115
+ }
3116
+ declare const openaiLLM: OpenAILLM;
3117
+
3118
+ /**
3119
+ * Query Cache — Two mechanisms:
3120
+ *
3121
+ * 1. `cache` (query string → result data) — TTL-based with max size, for avoiding re-execution
3122
+ * of recently validated queries. True LRU eviction: reads bubble entries to the back via
3123
+ * delete+re-set so the oldest *unused* entry is evicted, not the oldest *inserted*.
3124
+ *
3125
+ * 2. Encrypted queryId tokens — SQL is encrypted into the queryId itself (self-contained).
3126
+ * No server-side storage needed for SQL mappings. The token is decrypted on each request.
3127
+ * This eliminates the unbounded queryIdCache that previously grew forever and caused
3128
+ * memory bloat (hundreds of MBs after thousands of queries).
3129
+ *
3130
+ * Result data can still be cached temporarily via the data cache (mechanism 1).
3131
+ */
3132
+ declare class QueryCache {
3133
+ private cache;
3134
+ private ttlMs;
3135
+ private maxCacheSize;
3136
+ private cleanupInterval;
3137
+ private readonly algorithm;
3138
+ private encryptionKey;
3139
+ constructor();
1156
3140
  /**
1157
- * Get a thread by ID
3141
+ * Set the cache TTL (Time To Live)
3142
+ * @param minutes - TTL in minutes (default: 10)
1158
3143
  */
1159
- getThread(id: string): Thread | undefined;
3144
+ setTTL(minutes: number): void;
1160
3145
  /**
1161
- * Get all threads
3146
+ * Get the current TTL in minutes
1162
3147
  */
1163
- getAllThreads(): Thread[];
3148
+ getTTL(): number;
1164
3149
  /**
1165
- * Get threads as a Map
3150
+ * Store query result in data cache.
3151
+ * If the key already exists, it's removed first so the re-insert places it
3152
+ * at the back of the iteration order (LRU). Eviction only fires when adding
3153
+ * a genuinely new key past the size limit.
1166
3154
  */
1167
- getThreadsMap(): Map<string, Thread>;
3155
+ set(query: string, data: any): void;
1168
3156
  /**
1169
- * Delete a thread by ID
3157
+ * Get cached result if exists and not expired.
3158
+ * On hit, re-inserts the entry so it moves to the back of the Map's
3159
+ * iteration order — turning FIFO eviction into true LRU.
1170
3160
  */
1171
- deleteThread(id: string): boolean;
3161
+ get(query: string): any | null;
1172
3162
  /**
1173
- * Check if thread exists
3163
+ * Check if query exists in cache (not expired)
1174
3164
  */
1175
- hasThread(id: string): boolean;
3165
+ has(query: string): boolean;
1176
3166
  /**
1177
- * Get number of threads
3167
+ * Remove a specific query from cache
1178
3168
  */
1179
- getThreadCount(): number;
3169
+ delete(query: string): void;
1180
3170
  /**
1181
- * Clear all threads
3171
+ * Clear all cached entries
1182
3172
  */
1183
- clearAll(): void;
3173
+ clear(): void;
1184
3174
  /**
1185
- * Find a UIBlock by ID across all threads
1186
- * @param uiBlockId - The UIBlock ID to search for
1187
- * @returns Object with thread and uiBlock if found, undefined otherwise
3175
+ * Get cache statistics
1188
3176
  */
1189
- findUIBlockById(uiBlockId: string): {
1190
- thread: Thread;
1191
- uiBlock: UIBlock;
1192
- } | undefined;
3177
+ getStats(): {
3178
+ size: number;
3179
+ queryIdCount: number;
3180
+ oldestEntryAge: number | null;
3181
+ };
1193
3182
  /**
1194
- * Convert all threads to JSON-serializable object
3183
+ * Start periodic cleanup of expired data cache entries.
1195
3184
  */
1196
- toJSON(): Record<string, any>;
1197
- }
1198
-
1199
- /**
1200
- * CleanupService handles cleanup of old threads and UIBlocks
1201
- * to prevent memory bloat and maintain optimal performance
1202
- */
1203
- declare class CleanupService {
1204
- private static instance;
1205
- private cleanupInterval;
1206
- private constructor();
3185
+ private startCleanup;
1207
3186
  /**
1208
- * Get singleton instance of CleanupService
3187
+ * Encrypt a payload into a self-contained token.
1209
3188
  */
1210
- static getInstance(): CleanupService;
3189
+ private encrypt;
1211
3190
  /**
1212
- * Clean up old threads based on retention period
1213
- * @param retentionDays - Number of days to keep threads (defaults to config)
1214
- * @returns Number of threads deleted
3191
+ * Decrypt a token back to the original payload.
1215
3192
  */
1216
- cleanupOldThreads(retentionDays?: number): number;
3193
+ private decrypt;
1217
3194
  /**
1218
- * Clean up old UIBlocks within threads based on retention period
1219
- * @param retentionDays - Number of days to keep UIBlocks (defaults to config)
1220
- * @returns Object with number of UIBlocks deleted per thread
3195
+ * Store a query by generating an encrypted token as queryId.
3196
+ * The SQL is encrypted INTO the token nothing stored in memory.
3197
+ * If data is provided, it's cached temporarily in the data cache.
1221
3198
  */
1222
- cleanupOldUIBlocks(retentionDays?: number): {
1223
- [threadId: string]: number;
1224
- };
3199
+ storeQuery(query: any, data?: any): string;
1225
3200
  /**
1226
- * Clear all component data from UIBlocks to free memory
1227
- * Keeps metadata but removes the actual data
1228
- * @param retentionDays - Number of days to keep full data (defaults to config)
1229
- * @returns Number of UIBlocks whose data was cleared
3201
+ * Get a stored query by decrypting its token.
3202
+ * Returns the SQL + any cached result data.
1230
3203
  */
1231
- clearOldUIBlockData(retentionDays?: number): number;
3204
+ getQuery(queryId: string): {
3205
+ query: any;
3206
+ data: any;
3207
+ } | null;
1232
3208
  /**
1233
- * Run full cleanup (threads, UIBlocks, and data)
1234
- * @returns Cleanup statistics
3209
+ * Update cached data for a queryId token
1235
3210
  */
1236
- runFullCleanup(): {
1237
- threadsDeleted: number;
1238
- uiblocksDeleted: {
1239
- [threadId: string]: number;
1240
- };
1241
- dataCleared: number;
1242
- };
3211
+ setQueryData(queryId: string, data: any): void;
1243
3212
  /**
1244
- * Start automatic cleanup at regular intervals
1245
- * @param intervalHours - Hours between cleanup runs (default: 24)
3213
+ * Stop cleanup interval (for graceful shutdown)
1246
3214
  */
1247
- startAutoCleanup(intervalHours?: number): void;
3215
+ destroy(): void;
3216
+ }
3217
+ declare const queryCache: QueryCache;
3218
+
3219
+ /**
3220
+ * Manages conversation history scoped per user + dashboard.
3221
+ * Each user-dashboard pair has its own isolated history that expires after a configurable TTL.
3222
+ */
3223
+ declare class DashboardConversationHistory {
3224
+ private histories;
3225
+ private ttlMs;
3226
+ private maxEntries;
3227
+ private cleanupInterval;
3228
+ constructor();
1248
3229
  /**
1249
- * Stop automatic cleanup
3230
+ * Set the TTL for dashboard histories
3231
+ * @param minutes - TTL in minutes
1250
3232
  */
1251
- stopAutoCleanup(): void;
3233
+ setTTL(minutes: number): void;
1252
3234
  /**
1253
- * Check if auto cleanup is running
3235
+ * Set max entries per dashboard
1254
3236
  */
1255
- isAutoCleanupRunning(): boolean;
3237
+ setMaxEntries(max: number): void;
1256
3238
  /**
1257
- * Get current memory usage statistics
3239
+ * Add a conversation entry for a user's dashboard
1258
3240
  */
1259
- getMemoryStats(): {
1260
- threadCount: number;
1261
- totalUIBlocks: number;
1262
- avgUIBlocksPerThread: number;
1263
- };
1264
- }
1265
-
1266
- /**
1267
- * Configuration for data storage limits in UIBlocks
1268
- */
1269
- declare const STORAGE_CONFIG: {
3241
+ addEntry(dashboardId: string, userPrompt: string, componentSummary: string, userId?: string): void;
1270
3242
  /**
1271
- * Maximum number of rows to store in UIBlock data
3243
+ * Get formatted conversation history for a user's dashboard
1272
3244
  */
1273
- MAX_ROWS_PER_BLOCK: number;
3245
+ getHistory(dashboardId: string, userId?: string): string;
1274
3246
  /**
1275
- * Maximum size in bytes per UIBlock (1MB)
3247
+ * Clear history for a specific user's dashboard
1276
3248
  */
1277
- MAX_SIZE_PER_BLOCK_BYTES: number;
3249
+ clearDashboard(dashboardId: string, userId?: string): void;
1278
3250
  /**
1279
- * Number of days to keep threads before cleanup
3251
+ * Clear all dashboard histories
1280
3252
  */
1281
- THREAD_RETENTION_DAYS: number;
3253
+ clearAll(): void;
1282
3254
  /**
1283
- * Number of days to keep UIBlocks before cleanup
3255
+ * Start periodic cleanup of expired histories
1284
3256
  */
1285
- UIBLOCK_RETENTION_DAYS: number;
1286
- };
1287
-
1288
- /**
1289
- * Configuration for conversation context and history management
1290
- */
1291
- declare const CONTEXT_CONFIG: {
3257
+ private startCleanup;
1292
3258
  /**
1293
- * Maximum number of previous UIBlocks to include as conversation context
1294
- * Set to 0 to disable conversation history
1295
- * Higher values provide more context but may increase token usage
3259
+ * Stop cleanup interval (for graceful shutdown)
1296
3260
  */
1297
- MAX_CONVERSATION_CONTEXT_BLOCKS: number;
1298
- };
3261
+ destroy(): void;
3262
+ }
3263
+ declare const dashboardConversationHistory: DashboardConversationHistory;
1299
3264
 
1300
- declare const SDK_VERSION = "0.0.8";
1301
3265
  type MessageTypeHandler = (message: IncomingMessage) => void | Promise<void>;
1302
3266
  declare class SuperatomSDK {
1303
3267
  private ws;
1304
3268
  private url;
1305
- private apiKey;
3269
+ private apiKey?;
1306
3270
  private projectId;
1307
- private userId;
1308
3271
  private type;
1309
3272
  private bundleDir;
1310
3273
  private messageHandlers;
@@ -1314,15 +3277,30 @@ declare class SuperatomSDK {
1314
3277
  private maxReconnectAttempts;
1315
3278
  private collections;
1316
3279
  private components;
3280
+ private tools;
3281
+ private workflows;
1317
3282
  private anthropicApiKey;
1318
3283
  private groqApiKey;
3284
+ private geminiApiKey;
3285
+ private openaiApiKey;
1319
3286
  private llmProviders;
3287
+ private databaseType;
3288
+ private modelStrategy;
3289
+ private mainAgentModel;
3290
+ private sourceAgentModel;
3291
+ private dashCompModels?;
3292
+ private conversationSimilarityThreshold;
1320
3293
  private userManager;
1321
3294
  private dashboardManager;
1322
3295
  private reportManager;
3296
+ private pingInterval;
3297
+ private lastPong;
3298
+ private readonly PING_INTERVAL_MS;
3299
+ private readonly PONG_TIMEOUT_MS;
1323
3300
  constructor(config: SuperatomSDKConfig);
1324
3301
  /**
1325
3302
  * Initialize PromptLoader and load prompts into memory
3303
+ * Tries to load from file system first, then falls back to hardcoded prompts
1326
3304
  */
1327
3305
  private initializePromptLoader;
1328
3306
  /**
@@ -1358,9 +3336,11 @@ declare class SuperatomSDK {
1358
3336
  */
1359
3337
  private handleMessage;
1360
3338
  /**
1361
- * Send a message to the Superatom service
3339
+ * Send a message to the Superatom service.
3340
+ * Returns true if the message was sent, false if the WebSocket is not connected.
3341
+ * Does NOT throw on closed connections — callers can check the return value if needed.
1362
3342
  */
1363
- send(message: Message): void;
3343
+ send(message: Message): boolean;
1364
3344
  /**
1365
3345
  * Register a message handler to receive all messages
1366
3346
  */
@@ -1386,7 +3366,69 @@ declare class SuperatomSDK {
1386
3366
  */
1387
3367
  addCollection<TParams = any, TResult = any>(collectionName: string, operation: CollectionOperation | string, handler: CollectionHandler<TParams, TResult>): void;
1388
3368
  private handleReconnect;
3369
+ /**
3370
+ * Start heartbeat to keep WebSocket connection alive
3371
+ * Sends PING every 3 minutes to prevent idle timeout from cloud infrastructure
3372
+ */
3373
+ private startHeartbeat;
3374
+ /**
3375
+ * Stop the heartbeat interval
3376
+ */
3377
+ private stopHeartbeat;
3378
+ /**
3379
+ * Handle PONG response from server
3380
+ */
3381
+ private handlePong;
1389
3382
  private storeComponents;
3383
+ /**
3384
+ * Set tools for the SDK instance
3385
+ */
3386
+ setTools(tools: Tool$1[]): void;
3387
+ /**
3388
+ * Get the stored tools
3389
+ */
3390
+ getTools(): Tool$1[];
3391
+ /**
3392
+ * Register workflow components for the SDK instance.
3393
+ *
3394
+ * Workflows are pre-built multi-step UI flows the main agent can pick when
3395
+ * the user's prompt matches a workflow's `whenToUse` trigger. Picking a
3396
+ * workflow short-circuits analysis text + dashboard component generation —
3397
+ * the workflow component is returned directly, with the LLM-extracted props.
3398
+ */
3399
+ setWorkflows(workflows: WorkflowDescriptor[]): void;
3400
+ /**
3401
+ * Get the registered workflow components.
3402
+ */
3403
+ getWorkflows(): WorkflowDescriptor[];
3404
+ /**
3405
+ * Apply model strategy to all LLM provider singletons
3406
+ * @param strategy - 'best', 'fast', or 'balanced'
3407
+ */
3408
+ private applyModelStrategy;
3409
+ /**
3410
+ * Set model strategy at runtime
3411
+ * @param strategy - 'best', 'fast', or 'balanced'
3412
+ */
3413
+ setModelStrategy(strategy: ModelStrategy): void;
3414
+ /**
3415
+ * Get current model strategy
3416
+ */
3417
+ getModelStrategy(): ModelStrategy;
3418
+ /**
3419
+ * Apply conversation similarity threshold to all LLM provider singletons
3420
+ * @param threshold - Value between 0 and 1 (e.g., 0.8 = 80% similarity required)
3421
+ */
3422
+ private applyConversationSimilarityThreshold;
3423
+ /**
3424
+ * Set conversation similarity threshold at runtime
3425
+ * @param threshold - Value between 0 and 1 (e.g., 0.8 = 80% similarity required)
3426
+ */
3427
+ setConversationSimilarityThreshold(threshold: number): void;
3428
+ /**
3429
+ * Get current conversation similarity threshold
3430
+ */
3431
+ getConversationSimilarityThreshold(): number;
1390
3432
  }
1391
3433
 
1392
- 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 };
3434
+ 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 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, openaiLLM, queryCache, rerankChromaResults, rerankConversationResults, userPromptErrorLogger };