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