@superatomai/sdk-node 0.0.5 → 0.0.6-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.ts CHANGED
@@ -1,4 +1,16 @@
1
1
  import { z } from 'zod';
2
+ import Anthropic from '@anthropic-ai/sdk';
3
+
4
+ /**
5
+ * Unified UIBlock structure for database storage
6
+ * Used in both bookmarks and user-conversations tables
7
+ */
8
+ interface DBUIBlock {
9
+ id: string;
10
+ component: Record<string, any> | null;
11
+ analysis: string | null;
12
+ user_prompt: string;
13
+ }
2
14
 
3
15
  /**
4
16
  * Log levels in hierarchical order
@@ -47,6 +59,18 @@ declare class Logger {
47
59
  * Log debug message (only shown for verbose level)
48
60
  */
49
61
  debug(...args: any[]): void;
62
+ /**
63
+ * Write to log file
64
+ */
65
+ file(...args: any[]): void;
66
+ /**
67
+ * Clear the log file (call at start of new user request)
68
+ */
69
+ clearFile(): void;
70
+ /**
71
+ * Log LLM method prompts with clear labeling
72
+ */
73
+ logLLMPrompt(methodName: string, promptType: 'system' | 'user', content: string | object | any[]): void;
50
74
  }
51
75
  declare const logger: Logger;
52
76
 
@@ -77,7 +101,27 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
77
101
  deps?: string[] | undefined;
78
102
  }>, "many">>;
79
103
  data: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
80
- render: z.ZodType<any, z.ZodTypeDef, any>;
104
+ render: z.ZodOptional<z.ZodType<any, z.ZodTypeDef, any>>;
105
+ pages: z.ZodOptional<z.ZodArray<z.ZodObject<{
106
+ id: z.ZodString;
107
+ name: z.ZodString;
108
+ order: z.ZodNumber;
109
+ icon: z.ZodOptional<z.ZodString>;
110
+ render: z.ZodType<any, z.ZodTypeDef, any>;
111
+ }, "strip", z.ZodTypeAny, {
112
+ id: string;
113
+ name: string;
114
+ order: number;
115
+ icon?: string | undefined;
116
+ render?: any;
117
+ }, {
118
+ id: string;
119
+ name: string;
120
+ order: number;
121
+ icon?: string | undefined;
122
+ render?: any;
123
+ }>, "many">>;
124
+ defaultPageId: z.ZodOptional<z.ZodString>;
81
125
  query: z.ZodOptional<z.ZodObject<{
82
126
  graphql: z.ZodOptional<z.ZodString>;
83
127
  sql: z.ZodOptional<z.ZodString>;
@@ -116,6 +160,7 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
116
160
  dependencies?: string[] | undefined;
117
161
  } | undefined;
118
162
  props?: Record<string, any> | undefined;
163
+ render?: any;
119
164
  states?: Record<string, any> | undefined;
120
165
  methods?: Record<string, {
121
166
  fn: string;
@@ -126,7 +171,14 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
126
171
  deps?: string[] | undefined;
127
172
  }[] | undefined;
128
173
  data?: Record<string, any> | undefined;
129
- 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;
130
182
  }, {
131
183
  id: string;
132
184
  name?: string | undefined;
@@ -140,6 +192,7 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
140
192
  dependencies?: string[] | undefined;
141
193
  } | undefined;
142
194
  props?: Record<string, any> | undefined;
195
+ render?: any;
143
196
  states?: Record<string, any> | undefined;
144
197
  methods?: Record<string, {
145
198
  fn: string;
@@ -150,7 +203,14 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
150
203
  deps?: string[] | undefined;
151
204
  }[] | undefined;
152
205
  data?: Record<string, any> | undefined;
153
- 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;
154
214
  }>;
155
215
  data: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
156
216
  context: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
@@ -168,6 +228,7 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
168
228
  dependencies?: string[] | undefined;
169
229
  } | undefined;
170
230
  props?: Record<string, any> | undefined;
231
+ render?: any;
171
232
  states?: Record<string, any> | undefined;
172
233
  methods?: Record<string, {
173
234
  fn: string;
@@ -178,7 +239,14 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
178
239
  deps?: string[] | undefined;
179
240
  }[] | undefined;
180
241
  data?: Record<string, any> | undefined;
181
- 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;
182
250
  };
183
251
  data?: Record<string, any> | undefined;
184
252
  context?: Record<string, any> | undefined;
@@ -196,6 +264,7 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
196
264
  dependencies?: string[] | undefined;
197
265
  } | undefined;
198
266
  props?: Record<string, any> | undefined;
267
+ render?: any;
199
268
  states?: Record<string, any> | undefined;
200
269
  methods?: Record<string, {
201
270
  fn: string;
@@ -206,7 +275,14 @@ declare const DSLRendererPropsSchema$1: z.ZodObject<{
206
275
  deps?: string[] | undefined;
207
276
  }[] | undefined;
208
277
  data?: Record<string, any> | undefined;
209
- 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;
210
286
  };
211
287
  data?: Record<string, any> | undefined;
212
288
  context?: Record<string, any> | undefined;
@@ -279,6 +355,7 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
279
355
  dependencies?: string[] | undefined;
280
356
  } | undefined;
281
357
  props?: Record<string, any> | undefined;
358
+ render?: any;
282
359
  states?: Record<string, any> | undefined;
283
360
  methods?: Record<string, {
284
361
  fn: string;
@@ -289,7 +366,6 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
289
366
  deps?: string[] | undefined;
290
367
  }[] | undefined;
291
368
  data?: Record<string, any> | undefined;
292
- render?: any;
293
369
  }, {
294
370
  id: string;
295
371
  name?: string | undefined;
@@ -303,6 +379,7 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
303
379
  dependencies?: string[] | undefined;
304
380
  } | undefined;
305
381
  props?: Record<string, any> | undefined;
382
+ render?: any;
306
383
  states?: Record<string, any> | undefined;
307
384
  methods?: Record<string, {
308
385
  fn: string;
@@ -313,7 +390,6 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
313
390
  deps?: string[] | undefined;
314
391
  }[] | undefined;
315
392
  data?: Record<string, any> | undefined;
316
- render?: any;
317
393
  }>;
318
394
  data: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
319
395
  context: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
@@ -331,6 +407,7 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
331
407
  dependencies?: string[] | undefined;
332
408
  } | undefined;
333
409
  props?: Record<string, any> | undefined;
410
+ render?: any;
334
411
  states?: Record<string, any> | undefined;
335
412
  methods?: Record<string, {
336
413
  fn: string;
@@ -341,7 +418,6 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
341
418
  deps?: string[] | undefined;
342
419
  }[] | undefined;
343
420
  data?: Record<string, any> | undefined;
344
- render?: any;
345
421
  };
346
422
  data?: Record<string, any> | undefined;
347
423
  context?: Record<string, any> | undefined;
@@ -359,6 +435,7 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
359
435
  dependencies?: string[] | undefined;
360
436
  } | undefined;
361
437
  props?: Record<string, any> | undefined;
438
+ render?: any;
362
439
  states?: Record<string, any> | undefined;
363
440
  methods?: Record<string, {
364
441
  fn: string;
@@ -369,7 +446,6 @@ declare const DSLRendererPropsSchema: z.ZodObject<{
369
446
  deps?: string[] | undefined;
370
447
  }[] | undefined;
371
448
  data?: Record<string, any> | undefined;
372
- render?: any;
373
449
  };
374
450
  data?: Record<string, any> | undefined;
375
451
  context?: Record<string, any> | undefined;
@@ -382,6 +458,7 @@ declare const UserSchema: z.ZodObject<{
382
458
  password: z.ZodString;
383
459
  fullname: z.ZodOptional<z.ZodString>;
384
460
  role: z.ZodOptional<z.ZodString>;
461
+ userInfo: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
385
462
  wsIds: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
386
463
  }, "strip", z.ZodTypeAny, {
387
464
  username: string;
@@ -389,6 +466,7 @@ declare const UserSchema: z.ZodObject<{
389
466
  email?: string | undefined;
390
467
  fullname?: string | undefined;
391
468
  role?: string | undefined;
469
+ userInfo?: Record<string, unknown> | undefined;
392
470
  wsIds?: string[] | undefined;
393
471
  }, {
394
472
  username: string;
@@ -396,6 +474,7 @@ declare const UserSchema: z.ZodObject<{
396
474
  email?: string | undefined;
397
475
  fullname?: string | undefined;
398
476
  role?: string | undefined;
477
+ userInfo?: Record<string, unknown> | undefined;
399
478
  wsIds?: string[] | undefined;
400
479
  }>;
401
480
  type User = z.infer<typeof UserSchema>;
@@ -406,6 +485,7 @@ declare const UsersDataSchema: z.ZodObject<{
406
485
  password: z.ZodString;
407
486
  fullname: z.ZodOptional<z.ZodString>;
408
487
  role: z.ZodOptional<z.ZodString>;
488
+ userInfo: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
409
489
  wsIds: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
410
490
  }, "strip", z.ZodTypeAny, {
411
491
  username: string;
@@ -413,6 +493,7 @@ declare const UsersDataSchema: z.ZodObject<{
413
493
  email?: string | undefined;
414
494
  fullname?: string | undefined;
415
495
  role?: string | undefined;
496
+ userInfo?: Record<string, unknown> | undefined;
416
497
  wsIds?: string[] | undefined;
417
498
  }, {
418
499
  username: string;
@@ -420,6 +501,7 @@ declare const UsersDataSchema: z.ZodObject<{
420
501
  email?: string | undefined;
421
502
  fullname?: string | undefined;
422
503
  role?: string | undefined;
504
+ userInfo?: Record<string, unknown> | undefined;
423
505
  wsIds?: string[] | undefined;
424
506
  }>, "many">;
425
507
  }, "strip", z.ZodTypeAny, {
@@ -429,6 +511,7 @@ declare const UsersDataSchema: z.ZodObject<{
429
511
  email?: string | undefined;
430
512
  fullname?: string | undefined;
431
513
  role?: string | undefined;
514
+ userInfo?: Record<string, unknown> | undefined;
432
515
  wsIds?: string[] | undefined;
433
516
  }[];
434
517
  }, {
@@ -438,6 +521,7 @@ declare const UsersDataSchema: z.ZodObject<{
438
521
  email?: string | undefined;
439
522
  fullname?: string | undefined;
440
523
  role?: string | undefined;
524
+ userInfo?: Record<string, unknown> | undefined;
441
525
  wsIds?: string[] | undefined;
442
526
  }[];
443
527
  }>;
@@ -542,22 +626,439 @@ declare const IncomingMessageSchema: z.ZodObject<{
542
626
  payload?: unknown;
543
627
  }>;
544
628
  type IncomingMessage = z.infer<typeof IncomingMessageSchema>;
629
+ declare const ComponentSchema: z.ZodObject<{
630
+ id: z.ZodString;
631
+ name: z.ZodString;
632
+ displayName: z.ZodOptional<z.ZodString>;
633
+ isDisplayComp: z.ZodOptional<z.ZodBoolean>;
634
+ type: z.ZodString;
635
+ description: z.ZodString;
636
+ props: z.ZodObject<{
637
+ query: z.ZodOptional<z.ZodNullable<z.ZodUnion<[z.ZodString, z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>]>>>;
638
+ title: z.ZodOptional<z.ZodString>;
639
+ description: z.ZodOptional<z.ZodString>;
640
+ config: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
641
+ actions: z.ZodOptional<z.ZodArray<z.ZodAny, "many">>;
642
+ }, "passthrough", z.ZodTypeAny, z.objectOutputType<{
643
+ query: z.ZodOptional<z.ZodNullable<z.ZodUnion<[z.ZodString, z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>]>>>;
644
+ title: z.ZodOptional<z.ZodString>;
645
+ description: z.ZodOptional<z.ZodString>;
646
+ config: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
647
+ actions: z.ZodOptional<z.ZodArray<z.ZodAny, "many">>;
648
+ }, z.ZodTypeAny, "passthrough">, z.objectInputType<{
649
+ query: z.ZodOptional<z.ZodNullable<z.ZodUnion<[z.ZodString, z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>]>>>;
650
+ title: z.ZodOptional<z.ZodString>;
651
+ description: z.ZodOptional<z.ZodString>;
652
+ config: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
653
+ actions: z.ZodOptional<z.ZodArray<z.ZodAny, "many">>;
654
+ }, z.ZodTypeAny, "passthrough">>;
655
+ category: z.ZodOptional<z.ZodString>;
656
+ keywords: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
657
+ }, "strip", z.ZodTypeAny, {
658
+ id: string;
659
+ type: string;
660
+ name: string;
661
+ description: string;
662
+ props: {
663
+ description?: string | undefined;
664
+ query?: string | {} | null | undefined;
665
+ title?: string | undefined;
666
+ config?: Record<string, unknown> | undefined;
667
+ actions?: any[] | undefined;
668
+ } & {
669
+ [k: string]: unknown;
670
+ };
671
+ displayName?: string | undefined;
672
+ isDisplayComp?: boolean | undefined;
673
+ category?: string | undefined;
674
+ keywords?: string[] | undefined;
675
+ }, {
676
+ id: string;
677
+ type: string;
678
+ name: string;
679
+ description: string;
680
+ props: {
681
+ description?: string | undefined;
682
+ query?: string | {} | null | undefined;
683
+ title?: string | undefined;
684
+ config?: Record<string, unknown> | undefined;
685
+ actions?: any[] | undefined;
686
+ } & {
687
+ [k: string]: unknown;
688
+ };
689
+ displayName?: string | undefined;
690
+ isDisplayComp?: boolean | undefined;
691
+ category?: string | undefined;
692
+ keywords?: string[] | undefined;
693
+ }>;
694
+ type Component = z.infer<typeof ComponentSchema>;
695
+ declare const OutputFieldSchema: z.ZodObject<{
696
+ name: z.ZodString;
697
+ type: z.ZodEnum<["string", "number", "boolean", "date"]>;
698
+ description: z.ZodString;
699
+ }, "strip", z.ZodTypeAny, {
700
+ type: "string" | "number" | "boolean" | "date";
701
+ name: string;
702
+ description: string;
703
+ }, {
704
+ type: "string" | "number" | "boolean" | "date";
705
+ name: string;
706
+ description: string;
707
+ }>;
708
+ type OutputField = z.infer<typeof OutputFieldSchema>;
709
+ declare const OutputSchema: z.ZodObject<{
710
+ description: z.ZodString;
711
+ fields: z.ZodArray<z.ZodObject<{
712
+ name: z.ZodString;
713
+ type: z.ZodEnum<["string", "number", "boolean", "date"]>;
714
+ description: z.ZodString;
715
+ }, "strip", z.ZodTypeAny, {
716
+ type: "string" | "number" | "boolean" | "date";
717
+ name: string;
718
+ description: string;
719
+ }, {
720
+ type: "string" | "number" | "boolean" | "date";
721
+ name: string;
722
+ description: string;
723
+ }>, "many">;
724
+ }, "strip", z.ZodTypeAny, {
725
+ description: string;
726
+ fields: {
727
+ type: "string" | "number" | "boolean" | "date";
728
+ name: string;
729
+ description: string;
730
+ }[];
731
+ }, {
732
+ description: string;
733
+ fields: {
734
+ type: "string" | "number" | "boolean" | "date";
735
+ name: string;
736
+ description: string;
737
+ }[];
738
+ }>;
739
+ type ToolOutputSchema = z.infer<typeof OutputSchema>;
740
+ declare const ToolSchema: z.ZodObject<{
741
+ id: z.ZodString;
742
+ name: z.ZodString;
743
+ description: z.ZodString;
744
+ /** Tool type: "source" = routed through SourceAgent, "direct" = called directly by MainAgent */
745
+ toolType: z.ZodOptional<z.ZodEnum<["source", "direct"]>>;
746
+ /** Full untruncated schema for source agent (all columns visible) */
747
+ fullSchema: z.ZodOptional<z.ZodString>;
748
+ params: z.ZodRecord<z.ZodString, z.ZodString>;
749
+ fn: z.ZodFunction<z.ZodTuple<[z.ZodAny], z.ZodUnknown>, z.ZodAny>;
750
+ outputSchema: z.ZodOptional<z.ZodObject<{
751
+ description: z.ZodString;
752
+ fields: z.ZodArray<z.ZodObject<{
753
+ name: z.ZodString;
754
+ type: z.ZodEnum<["string", "number", "boolean", "date"]>;
755
+ description: z.ZodString;
756
+ }, "strip", z.ZodTypeAny, {
757
+ type: "string" | "number" | "boolean" | "date";
758
+ name: string;
759
+ description: string;
760
+ }, {
761
+ type: "string" | "number" | "boolean" | "date";
762
+ name: string;
763
+ description: string;
764
+ }>, "many">;
765
+ }, "strip", z.ZodTypeAny, {
766
+ description: string;
767
+ fields: {
768
+ type: "string" | "number" | "boolean" | "date";
769
+ name: string;
770
+ description: string;
771
+ }[];
772
+ }, {
773
+ description: string;
774
+ fields: {
775
+ type: "string" | "number" | "boolean" | "date";
776
+ name: string;
777
+ description: string;
778
+ }[];
779
+ }>>;
780
+ /** Cache policy. `false` = never cache (live data, write ops). Mirrors HTTP `Cache-Control: no-store`. */
781
+ cache: z.ZodOptional<z.ZodUnion<[z.ZodLiteral<false>, z.ZodObject<{
782
+ ttlMs: z.ZodOptional<z.ZodNumber>;
783
+ }, "strip", z.ZodTypeAny, {
784
+ ttlMs?: number | undefined;
785
+ }, {
786
+ ttlMs?: number | undefined;
787
+ }>]>>;
788
+ }, "strip", z.ZodTypeAny, {
789
+ id: string;
790
+ params: Record<string, string>;
791
+ name: string;
792
+ description: string;
793
+ fn: (args_0: any, ...args: unknown[]) => any;
794
+ toolType?: "source" | "direct" | undefined;
795
+ fullSchema?: string | undefined;
796
+ outputSchema?: {
797
+ description: string;
798
+ fields: {
799
+ type: "string" | "number" | "boolean" | "date";
800
+ name: string;
801
+ description: string;
802
+ }[];
803
+ } | undefined;
804
+ cache?: false | {
805
+ ttlMs?: number | undefined;
806
+ } | undefined;
807
+ }, {
808
+ id: string;
809
+ params: Record<string, string>;
810
+ name: string;
811
+ description: string;
812
+ fn: (args_0: any, ...args: unknown[]) => any;
813
+ toolType?: "source" | "direct" | undefined;
814
+ fullSchema?: string | undefined;
815
+ outputSchema?: {
816
+ description: string;
817
+ fields: {
818
+ type: "string" | "number" | "boolean" | "date";
819
+ name: string;
820
+ description: string;
821
+ }[];
822
+ } | undefined;
823
+ cache?: false | {
824
+ ttlMs?: number | undefined;
825
+ } | undefined;
826
+ }>;
827
+ type Tool$1 = z.infer<typeof ToolSchema>;
545
828
  type CollectionOperation = 'getMany' | 'getOne' | 'query' | 'mutation' | 'updateOne' | 'deleteOne' | 'createOne';
546
829
  type CollectionHandler<TParams = any, TResult = any> = (params: TParams) => Promise<TResult> | TResult;
547
- type LLMProvider = 'anthropic' | 'groq';
830
+ type LLMProvider = 'anthropic' | 'groq' | 'gemini' | 'openai';
548
831
 
832
+ type DatabaseType = 'postgresql' | 'mssql' | 'snowflake' | 'mysql';
833
+ /**
834
+ * Model strategy for controlling which models are used for different tasks
835
+ * - 'best': Use the best model (e.g., Sonnet) for all tasks - highest quality, higher cost
836
+ * - 'fast': Use the fast model (e.g., Haiku) for all tasks - lower quality, lower cost
837
+ * - 'balanced': Use best model for complex tasks, fast model for simple tasks (default)
838
+ */
839
+ type ModelStrategy = 'best' | 'fast' | 'balanced';
840
+ /**
841
+ * Model configuration for DASH_COMP flow (dashboard component picking)
842
+ * Allows separate control of models used for component selection
843
+ */
844
+ interface DashCompModelConfig {
845
+ /**
846
+ * Primary model for DASH_COMP requests
847
+ * Format: "provider/model-name" (e.g., "anthropic/claude-sonnet-4-5-20250929")
848
+ */
849
+ model?: string;
850
+ /**
851
+ * Fast model for simpler DASH_COMP tasks (optional)
852
+ * Format: "provider/model-name" (e.g., "anthropic/claude-haiku-4-5-20251001")
853
+ */
854
+ fastModel?: string;
855
+ }
549
856
  interface SuperatomSDKConfig {
550
857
  url?: string;
551
- apiKey: string;
858
+ apiKey?: string;
552
859
  projectId: string;
553
- userId?: string;
554
860
  type?: string;
555
861
  bundleDir?: string;
556
862
  promptsDir?: string;
863
+ databaseType?: DatabaseType;
557
864
  ANTHROPIC_API_KEY?: string;
558
865
  GROQ_API_KEY?: string;
866
+ GEMINI_API_KEY?: string;
867
+ OPENAI_API_KEY?: string;
559
868
  LLM_PROVIDERS?: LLMProvider[];
560
869
  logLevel?: LogLevel;
870
+ /**
871
+ * Model selection strategy for LLM API calls:
872
+ * - 'best': Use best model for all tasks (highest quality, higher cost)
873
+ * - 'fast': Use fast model for all tasks (lower quality, lower cost)
874
+ * - 'balanced': Use best model for complex tasks, fast model for simple tasks (default)
875
+ */
876
+ modelStrategy?: ModelStrategy;
877
+ /**
878
+ * Model for the main agent (routing + analysis).
879
+ * Format: "provider/model-name" (e.g., "anthropic/claude-haiku-4-5-20251001")
880
+ * If not set, uses the provider's default model.
881
+ */
882
+ mainAgentModel?: string;
883
+ /**
884
+ * Model for source agents (per-source query generation).
885
+ * Format: "provider/model-name" (e.g., "anthropic/claude-haiku-4-5-20251001")
886
+ * If not set, uses the provider's default model.
887
+ */
888
+ sourceAgentModel?: string;
889
+ /**
890
+ * Separate model configuration for DASH_COMP flow (dashboard component picking)
891
+ * If not provided, falls back to provider-based model selection
892
+ */
893
+ dashCompModels?: DashCompModelConfig;
894
+ /**
895
+ * Similarity threshold for conversation search (semantic matching)
896
+ * Value between 0 and 1 (e.g., 0.8 = 80% similarity required)
897
+ * Higher values require closer matches, lower values allow more distant matches
898
+ * Default: 0.8
899
+ */
900
+ conversationSimilarityThreshold?: number;
901
+ /**
902
+ * Query cache TTL (Time To Live) in minutes
903
+ * Cached query results expire after this duration
904
+ * Default: 5 minutes
905
+ */
906
+ queryCacheTTL?: number;
907
+ /**
908
+ * Dashboard conversation history TTL (Time To Live) in minutes
909
+ * Per-dashboard conversation histories expire after this duration
910
+ * Default: 30 minutes
911
+ */
912
+ dashboardHistoryTTL?: number;
913
+ }
914
+
915
+ declare const KbNodesQueryFiltersSchema: z.ZodObject<{
916
+ query: z.ZodOptional<z.ZodString>;
917
+ category: z.ZodOptional<z.ZodString>;
918
+ tags: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
919
+ type: z.ZodOptional<z.ZodEnum<["global", "user", "query"]>>;
920
+ createdBy: z.ZodOptional<z.ZodString>;
921
+ }, "strip", z.ZodTypeAny, {
922
+ type?: "query" | "user" | "global" | undefined;
923
+ query?: string | undefined;
924
+ category?: string | undefined;
925
+ createdBy?: string | undefined;
926
+ tags?: string[] | undefined;
927
+ }, {
928
+ type?: "query" | "user" | "global" | undefined;
929
+ query?: string | undefined;
930
+ category?: string | undefined;
931
+ createdBy?: string | undefined;
932
+ tags?: string[] | undefined;
933
+ }>;
934
+ type KbNodesQueryFilters = z.infer<typeof KbNodesQueryFiltersSchema>;
935
+ declare const KbNodesRequestPayloadSchema: z.ZodObject<{
936
+ operation: z.ZodEnum<["create", "update", "delete", "getAll", "getOne", "search", "getByCategory", "getByUser", "getCategories", "getTags"]>;
937
+ data: z.ZodOptional<z.ZodObject<{
938
+ id: z.ZodOptional<z.ZodNumber>;
939
+ title: z.ZodOptional<z.ZodString>;
940
+ content: z.ZodOptional<z.ZodString>;
941
+ category: z.ZodOptional<z.ZodString>;
942
+ tags: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
943
+ type: z.ZodOptional<z.ZodEnum<["global", "user", "query"]>>;
944
+ createdBy: z.ZodOptional<z.ZodString>;
945
+ updatedBy: z.ZodOptional<z.ZodString>;
946
+ userId: z.ZodOptional<z.ZodString>;
947
+ query: z.ZodOptional<z.ZodString>;
948
+ filters: z.ZodOptional<z.ZodObject<{
949
+ query: z.ZodOptional<z.ZodString>;
950
+ category: z.ZodOptional<z.ZodString>;
951
+ tags: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
952
+ type: z.ZodOptional<z.ZodEnum<["global", "user", "query"]>>;
953
+ createdBy: z.ZodOptional<z.ZodString>;
954
+ }, "strip", z.ZodTypeAny, {
955
+ type?: "query" | "user" | "global" | undefined;
956
+ query?: string | undefined;
957
+ category?: string | undefined;
958
+ createdBy?: string | undefined;
959
+ tags?: string[] | undefined;
960
+ }, {
961
+ type?: "query" | "user" | "global" | undefined;
962
+ query?: string | undefined;
963
+ category?: string | undefined;
964
+ createdBy?: string | undefined;
965
+ tags?: string[] | undefined;
966
+ }>>;
967
+ limit: z.ZodOptional<z.ZodNumber>;
968
+ offset: z.ZodOptional<z.ZodNumber>;
969
+ }, "strip", z.ZodTypeAny, {
970
+ id?: number | undefined;
971
+ type?: "query" | "user" | "global" | undefined;
972
+ query?: string | undefined;
973
+ title?: string | undefined;
974
+ category?: string | undefined;
975
+ userId?: string | undefined;
976
+ limit?: number | undefined;
977
+ filters?: {
978
+ type?: "query" | "user" | "global" | undefined;
979
+ query?: string | undefined;
980
+ category?: string | undefined;
981
+ createdBy?: string | undefined;
982
+ tags?: string[] | undefined;
983
+ } | undefined;
984
+ createdBy?: string | undefined;
985
+ updatedBy?: string | undefined;
986
+ offset?: number | undefined;
987
+ tags?: string[] | undefined;
988
+ content?: string | undefined;
989
+ }, {
990
+ id?: number | undefined;
991
+ type?: "query" | "user" | "global" | undefined;
992
+ query?: string | undefined;
993
+ title?: string | undefined;
994
+ category?: string | undefined;
995
+ userId?: string | undefined;
996
+ limit?: number | undefined;
997
+ filters?: {
998
+ type?: "query" | "user" | "global" | undefined;
999
+ query?: string | undefined;
1000
+ category?: string | undefined;
1001
+ createdBy?: string | undefined;
1002
+ tags?: string[] | undefined;
1003
+ } | undefined;
1004
+ createdBy?: string | undefined;
1005
+ updatedBy?: string | undefined;
1006
+ offset?: number | undefined;
1007
+ tags?: string[] | undefined;
1008
+ content?: string | undefined;
1009
+ }>>;
1010
+ }, "strip", z.ZodTypeAny, {
1011
+ operation: "create" | "getOne" | "update" | "delete" | "getAll" | "search" | "getByCategory" | "getByUser" | "getCategories" | "getTags";
1012
+ data?: {
1013
+ id?: number | undefined;
1014
+ type?: "query" | "user" | "global" | undefined;
1015
+ query?: string | undefined;
1016
+ title?: string | undefined;
1017
+ category?: string | undefined;
1018
+ userId?: string | undefined;
1019
+ limit?: number | undefined;
1020
+ filters?: {
1021
+ type?: "query" | "user" | "global" | undefined;
1022
+ query?: string | undefined;
1023
+ category?: string | undefined;
1024
+ createdBy?: string | undefined;
1025
+ tags?: string[] | undefined;
1026
+ } | undefined;
1027
+ createdBy?: string | undefined;
1028
+ updatedBy?: string | undefined;
1029
+ offset?: number | undefined;
1030
+ tags?: string[] | undefined;
1031
+ content?: string | undefined;
1032
+ } | undefined;
1033
+ }, {
1034
+ operation: "create" | "getOne" | "update" | "delete" | "getAll" | "search" | "getByCategory" | "getByUser" | "getCategories" | "getTags";
1035
+ data?: {
1036
+ id?: number | undefined;
1037
+ type?: "query" | "user" | "global" | undefined;
1038
+ query?: string | undefined;
1039
+ title?: string | undefined;
1040
+ category?: string | undefined;
1041
+ userId?: string | undefined;
1042
+ limit?: number | undefined;
1043
+ filters?: {
1044
+ type?: "query" | "user" | "global" | undefined;
1045
+ query?: string | undefined;
1046
+ category?: string | undefined;
1047
+ createdBy?: string | undefined;
1048
+ tags?: string[] | undefined;
1049
+ } | undefined;
1050
+ createdBy?: string | undefined;
1051
+ updatedBy?: string | undefined;
1052
+ offset?: number | undefined;
1053
+ tags?: string[] | undefined;
1054
+ content?: string | undefined;
1055
+ } | undefined;
1056
+ }>;
1057
+ type KbNodesRequestPayload = z.infer<typeof KbNodesRequestPayloadSchema>;
1058
+ interface T_RESPONSE {
1059
+ success: boolean;
1060
+ data?: any;
1061
+ errors: string[];
561
1062
  }
562
1063
 
563
1064
  /**
@@ -819,122 +1320,731 @@ declare class ReportManager {
819
1320
  getReportCount(): number;
820
1321
  }
821
1322
 
822
- interface LLMMessages {
823
- sys: string;
824
- user: string;
825
- }
826
- interface LLMOptions {
827
- model?: string;
828
- maxTokens?: number;
829
- temperature?: number;
830
- topP?: number;
831
- apiKey?: string;
832
- partial?: (chunk: string) => void;
833
- }
834
- declare class LLM {
835
- static text(messages: LLMMessages, options?: LLMOptions): Promise<string>;
836
- static stream<T = string>(messages: LLMMessages, options?: LLMOptions, json?: boolean): Promise<T extends string ? string : any>;
837
- /**
838
- * Parse model string to extract provider and model name
839
- * @param modelString - Format: "provider/model-name" or just "model-name"
840
- * @returns [provider, modelName]
841
- *
842
- * @example
843
- * "anthropic/claude-sonnet-4-5" → ["anthropic", "claude-sonnet-4-5"]
844
- * "groq/openai/gpt-oss-120b" → ["groq", "openai/gpt-oss-120b"]
845
- * "claude-sonnet-4-5" → ["anthropic", "claude-sonnet-4-5"] (default)
846
- */
847
- private static _parseModel;
848
- private static _anthropicText;
849
- private static _anthropicStream;
850
- private static _groqText;
851
- private static _groqStream;
852
- /**
853
- * Parse JSON string, handling markdown code blocks and surrounding text
854
- * Enhanced version from anthropic.ts implementation
855
- * @param text - Text that may contain JSON wrapped in ```json...``` or with surrounding text
856
- * @returns Parsed JSON object
857
- */
858
- private static _parseJSON;
859
- }
860
-
861
- interface CapturedLog {
862
- timestamp: number;
863
- level: 'info' | 'error' | 'warn' | 'debug';
864
- message: string;
865
- type?: 'explanation' | 'query' | 'general';
866
- data?: Record<string, any>;
867
- }
868
1323
  /**
869
- * UILogCollector captures logs during user prompt processing
870
- * and sends them to runtime via ui_logs message with uiBlockId as the message id
871
- * Logs are sent in real-time for streaming effect in the UI
872
- * Respects the global log level configuration
1324
+ * StreamBuffer - Buffered streaming utility for smoother text delivery
1325
+ * Batches small chunks together and flushes at regular intervals
873
1326
  */
874
- declare class UILogCollector {
875
- private logs;
876
- private uiBlockId;
877
- private clientId;
878
- private sendMessage;
879
- private currentLogLevel;
880
- constructor(clientId: string, sendMessage: (message: Message) => void, uiBlockId?: string);
1327
+ type StreamCallback = (chunk: string) => void;
1328
+ /**
1329
+ * StreamBuffer class for managing buffered streaming output
1330
+ * Provides smooth text delivery by batching small chunks
1331
+ */
1332
+ declare class StreamBuffer {
1333
+ private buffer;
1334
+ private flushTimer;
1335
+ private callback;
1336
+ private fullText;
1337
+ constructor(callback?: StreamCallback);
881
1338
  /**
882
- * Check if logging is enabled (uiBlockId is provided)
1339
+ * Check if the buffer has a callback configured
883
1340
  */
884
- isEnabled(): boolean;
1341
+ hasCallback(): boolean;
885
1342
  /**
886
- * Check if a message should be logged based on current log level
1343
+ * Get all text that has been written (including already flushed)
887
1344
  */
888
- private shouldLog;
1345
+ getFullText(): string;
889
1346
  /**
890
- * Add a log entry with timestamp and immediately send to runtime
891
- * Only logs that pass the log level filter are captured and sent
1347
+ * Write a chunk to the buffer
1348
+ * Large chunks or chunks with newlines are flushed immediately
1349
+ * Small chunks are batched and flushed after a short interval
1350
+ *
1351
+ * @param chunk - Text chunk to write
892
1352
  */
893
- private addLog;
1353
+ write(chunk: string): void;
894
1354
  /**
895
- * Send a single log to runtime immediately
1355
+ * Flush the buffer immediately
1356
+ * Call this before tool execution or other operations that need clean output
896
1357
  */
897
- private sendLogImmediately;
1358
+ flush(): void;
898
1359
  /**
899
- * Log info message
1360
+ * Internal flush implementation
900
1361
  */
901
- info(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
1362
+ private flushNow;
902
1363
  /**
903
- * Log error message
1364
+ * Clean up resources
1365
+ * Call this when done with the buffer
904
1366
  */
905
- error(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
1367
+ dispose(): void;
1368
+ }
1369
+
1370
+ /**
1371
+ * ToolExecutorService - Handles execution of SQL queries and external tools
1372
+ * Extracted from BaseLLM.generateTextResponse for better separation of concerns
1373
+ */
1374
+
1375
+ /**
1376
+ * External tool definition
1377
+ */
1378
+ interface ExternalTool {
1379
+ id: string;
1380
+ name: string;
1381
+ description?: string;
1382
+ /** Tool type: "source" = routed through SourceAgent, "direct" = called directly by MainAgent */
1383
+ toolType?: 'source' | 'direct';
1384
+ /** Full untruncated schema for source agent (all columns visible) */
1385
+ fullSchema?: string;
1386
+ /** Schema size tier: small (≤50 tables), medium (51-200), large (201-500), very_large (500+) */
1387
+ schemaTier?: string;
1388
+ /** Schema search function for very_large tier — keyword search over entities */
1389
+ schemaSearchFn?: (keywords: string[]) => string;
1390
+ fn: (input: any) => Promise<any>;
1391
+ limit?: number;
1392
+ outputSchema?: any;
1393
+ executionType?: 'immediate' | 'deferred';
1394
+ userProvidedData?: any;
1395
+ params?: Record<string, any>;
1396
+ }
1397
+ /**
1398
+ * Executed tool tracking info
1399
+ */
1400
+ interface ExecutedToolInfo {
1401
+ id: string;
1402
+ name: string;
1403
+ params: any;
1404
+ result: {
1405
+ _totalRecords: number;
1406
+ _recordsShown: number;
1407
+ _metadata?: any;
1408
+ _sampleData: any[];
1409
+ };
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;
906
1508
  /**
907
- * Log warning message
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."
908
1513
  */
909
- warn(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
1514
+ whenToUse: string;
910
1515
  /**
911
- * Log debug message
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
+ * ```
912
1528
  */
913
- debug(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
1529
+ propsSchema: Record<string, string>;
914
1530
  /**
915
- * Log LLM explanation with typed metadata
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.
916
1534
  */
917
- logExplanation(message: string, explanation: string, data?: Record<string, any>): void;
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[];
918
1558
  /**
919
- * Log generated query with typed metadata
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).
920
1562
  */
921
- logQuery(message: string, query: string, data?: Record<string, any>): void;
1563
+ savedScript?: AgentWrittenScript;
922
1564
  /**
923
- * Send all collected logs at once (optional, for final summary)
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.
924
1568
  */
925
- sendAllLogs(): void;
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 {
926
1576
  /**
927
- * Get all collected logs
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.
928
1581
  */
929
- getLogs(): CapturedLog[];
930
- /**
931
- * Clear all logs
1582
+ recipeId: string;
1583
+ name: string;
1584
+ intentDescription: string;
1585
+ tags: string[];
1586
+ parameters: Array<{
1587
+ name: string;
1588
+ type: 'string' | 'number' | 'date' | 'date_range' | 'enum' | 'boolean';
1589
+ required: boolean;
1590
+ default?: any;
1591
+ enumValues?: Record<string, string>;
1592
+ description: string;
1593
+ }>;
1594
+ scriptBody: string;
1595
+ /** Source IDs referenced by the script (extracted from ctx.query calls) */
1596
+ sourceIds: string[];
1597
+ /** Tables referenced in the script's SQL (regex-extracted) */
1598
+ tables: string[];
1599
+ /** Executed queries from the verified run — fed to component generation */
1600
+ executedQueries: Array<{
1601
+ sourceId: string;
1602
+ sourceName: string;
1603
+ sql: string;
1604
+ data: any[];
1605
+ count: number;
1606
+ totalCount?: number;
1607
+ executionTimeMs: number;
1608
+ /**
1609
+ * True for synthetic entries (ctx.emit datasets, the computed:_final
1610
+ * post-JS data). The component generator routes virtual sources through
1611
+ * the script_dataset sentinel toolId so the frontend resolves them via
1612
+ * queryCache instead of attempting to re-execute SQL.
1613
+ */
1614
+ virtual?: boolean;
1615
+ }>;
1616
+ }
1617
+ /**
1618
+ * Configuration for the multi-agent system.
1619
+ * Controls limits, models, and behavior.
1620
+ */
1621
+ interface AgentConfig {
1622
+ /** Max rows a source agent can return (default: 50) */
1623
+ maxRowsPerSource: number;
1624
+ /** Model for the main agent (routing + analysis in one LLM call) */
1625
+ mainAgentModel: string;
1626
+ /** Model for source agent query generation */
1627
+ sourceAgentModel: string;
1628
+ /** API key for LLM calls */
1629
+ apiKey?: string;
1630
+ /** Max retry attempts per source agent */
1631
+ maxRetries: number;
1632
+ /** Max tool calling iterations for the main agent loop */
1633
+ maxIterations: number;
1634
+ /** Global knowledge base context (static, same for all users/questions — cached in system prompt) */
1635
+ globalKnowledgeBase?: string;
1636
+ /** Per-request knowledge base context (user-specific + query-matched — dynamic, not cached) */
1637
+ knowledgeBaseContext?: string;
1638
+ /** Collections registry (ChromaDB search hooks) for embedding-based schema + source search */
1639
+ collections?: any;
1640
+ /** Optional project ID for scoping embedding searches */
1641
+ projectId?: string;
1642
+ }
1643
+ /**
1644
+ * Default agent configuration
1645
+ */
1646
+ declare const DEFAULT_AGENT_CONFIG: AgentConfig;
1647
+
1648
+ /**
1649
+ * Script Flow Types
1650
+ *
1651
+ * Defines interfaces for the script-based query architecture:
1652
+ * - ScriptRecipe: metadata for matching, validation, and quality tracking
1653
+ * - ScriptResult: output from executing a script
1654
+ * - ScriptMatch: result from the LLM-based script matcher
1655
+ */
1656
+ /**
1657
+ * Recipe metadata stored alongside each script.
1658
+ * Used for matching, validation, and quality tracking.
1659
+ */
1660
+ interface ScriptRecipe {
1661
+ /** Unique script identifier */
1662
+ id: string;
1663
+ /** Version number (incremented on regeneration) */
1664
+ version: number;
1665
+ /** Human-readable name (e.g., "Revenue by Dimension") */
1666
+ name: string;
1667
+ /** Natural language description of what this script does */
1668
+ intentDescription: string;
1669
+ /** Keyword tags for quick filtering */
1670
+ tags: string[];
1671
+ /** Source tool IDs this script queries (e.g., ["mssql-abc123_query"]) */
1672
+ sourceIds: string[];
1673
+ /** Table names used (for future schema drift detection) */
1674
+ tables: string[];
1675
+ /** Parameter definitions — what can vary */
1676
+ parameters: ScriptParameter[];
1677
+ /** The script function body as a string */
1678
+ scriptBody: string;
1679
+ /** Times this script was used successfully */
1680
+ successCount: number;
1681
+ /** Times this script failed */
1682
+ failureCount: number;
1683
+ /** ISO timestamp of last usage */
1684
+ lastUsed: string;
1685
+ /** Original user question that created this script */
1686
+ createdFrom: string;
1687
+ /** ISO timestamp */
1688
+ createdAt: string;
1689
+ /** ISO timestamp */
1690
+ updatedAt: string;
1691
+ /**
1692
+ * `recipe.id` of the parent this script was forked from.
1693
+ * Undefined for root scripts (those written from scratch by MainAgent).
1694
+ * See backend/docs/SCRIPT-FLOW-FORK-ADAPT.md.
1695
+ */
1696
+ parentId?: string;
1697
+ /** 0 for root scripts; `parent.forkDepth + 1` for forks. Capped at 3. */
1698
+ forkDepth?: number;
1699
+ /**
1700
+ * Brief description of what this fork changed vs its parent
1701
+ * (sourced from the matcher's `modificationHint`).
1702
+ */
1703
+ forkReason?: string;
1704
+ /**
1705
+ * Validated component specs captured at authoring time. On a tier-high
1706
+ * replay these are rebound to fresh queryIds deterministically — no
1707
+ * component-generation LLM call, and the rendered columns can't drift from
1708
+ * what was validated when the script was authored. Absent on recipes
1709
+ * authored before this landed; those fall back to LLM component generation.
1710
+ * See backend/docs/SCRIPT-COMPONENT-CONSISTENCY.md.
1711
+ */
1712
+ components?: ScriptComponentSpec[];
1713
+ /**
1714
+ * Lifecycle stage of this recipe on disk.
1715
+ * - 'draft': written by MainAgent's write_script during a turn; filtered out
1716
+ * of `ScriptStore.getAll()` so the matcher never picks it.
1717
+ * Filename is suffixed with `turnId` to keep concurrent turns
1718
+ * from clobbering each other's drafts.
1719
+ * - 'verified': promoted after `execute_script` succeeded; the matcher sees it.
1720
+ * Filename drops the turn suffix unless a verified file with
1721
+ * the same slug already exists (collision case keeps the suffix).
1722
+ *
1723
+ * Recipes loaded from disk without this field default to 'verified' so
1724
+ * existing scripts keep working unchanged.
932
1725
  */
933
- clearLogs(): void;
1726
+ status?: 'draft' | 'verified';
934
1727
  /**
935
- * Set uiBlockId (in case it's provided later)
1728
+ * Per-turn unique suffix used for draft filenames (e.g. `1714745623-x9k2`).
1729
+ * Set when the draft is saved; carried until the recipe is promoted.
936
1730
  */
937
- setUIBlockId(uiBlockId: string): void;
1731
+ turnId?: string;
1732
+ /**
1733
+ * Last execution error captured by `recordDraftError` while the recipe was
1734
+ * still a draft. Lets users open the draft .json file and see why it failed
1735
+ * without grepping logs. Cleared on promotion to 'verified'.
1736
+ */
1737
+ lastError?: {
1738
+ phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
1739
+ message: string;
1740
+ at: string;
1741
+ attempt: number;
1742
+ };
1743
+ }
1744
+ interface ScriptParameter {
1745
+ /** Parameter name (used in script body as params.name) */
1746
+ name: string;
1747
+ /** Parameter type */
1748
+ type: 'string' | 'number' | 'date' | 'date_range' | 'enum' | 'boolean';
1749
+ /** Whether this parameter is required */
1750
+ required: boolean;
1751
+ /** Default value if not provided */
1752
+ default?: any;
1753
+ /** For enum type — maps user-facing values to internal values */
1754
+ enumValues?: Record<string, string>;
1755
+ /** Human-readable description (used in the matcher LLM prompt) */
1756
+ description: string;
1757
+ }
1758
+ /**
1759
+ * A reusable component binding captured when a script is authored. Stored on
1760
+ * the recipe so tier-high replays rebuild components deterministically (rebind
1761
+ * to fresh queryIds) instead of re-running the component-picker LLM.
1762
+ */
1763
+ interface ScriptComponentSpec {
1764
+ /** Registered component name (e.g. "DynamicBarChart") — matched against the available component library. */
1765
+ componentType: string;
1766
+ /** `executedQuery.sourceId` to bind to (e.g. a tool id or 'computed:_final'), or 'federation' for a cross-source component. */
1767
+ sourceRef: string;
1768
+ /** Present only when sourceRef === 'federation' — the DuckDB SQL to re-execute on replay. */
1769
+ federationSql?: string;
1770
+ title?: string;
1771
+ description?: string;
1772
+ /** Validated axis/value keys + aggregation — all referencing real columns of the bound source. */
1773
+ config: Record<string, any>;
1774
+ }
1775
+
1776
+ /**
1777
+ * ScriptStore — Persistent Storage for Script Recipes
1778
+ *
1779
+ * Layout:
1780
+ * scripts-store/
1781
+ * metadata/<name>.json ← recipe metadata (params, tables, counts, ...)
1782
+ * <name>.ts ← the getData() function body, editable in your IDE
1783
+ *
1784
+ * Each VM deployment is a single project, so no project ID prefix needed.
1785
+ * Legacy single-file format (JSON with embedded scriptBody) is still loadable
1786
+ * for backwards compatibility and auto-migrated to the split format on next save.
1787
+ */
1788
+
1789
+ /**
1790
+ * Input for `ScriptStore.saveDraft()`. Identifies the draft by `recipeId` so
1791
+ * retries within the same turn overwrite the same files (or omit `recipeId`
1792
+ * on the first call to mint a fresh draft).
1793
+ */
1794
+ interface SaveDraftInput {
1795
+ /** Reuse an existing draft (retry); omit to mint a new one. */
1796
+ recipeId?: string;
1797
+ /** Per-turn unique suffix, stable across retries within the turn. */
1798
+ turnId: string;
1799
+ name: string;
1800
+ intentDescription: string;
1801
+ tags: string[];
1802
+ parameters: ScriptParameter[];
1803
+ scriptBody: string;
1804
+ createdFrom: string;
1805
+ }
1806
+ /**
1807
+ * Lineage + provenance fields applied when a draft is promoted to verified.
1808
+ * MainAgent gathers `sourceIds` and `tables` from the verified execution; the
1809
+ * caller (agent-user-response.ts) supplies the optional fork lineage.
1810
+ */
1811
+ interface PromoteToVerifiedInput {
1812
+ sourceIds: string[];
1813
+ tables: string[];
1814
+ parentId?: string;
1815
+ forkDepth?: number;
1816
+ forkReason?: string;
1817
+ /** Validated component specs to persist on the recipe (component recipe). */
1818
+ components?: ScriptComponentSpec[];
1819
+ }
1820
+ declare class ScriptStore {
1821
+ private recipes;
1822
+ private storeDir;
1823
+ private loaded;
1824
+ constructor(baseDir?: string);
1825
+ private metadataDir;
1826
+ /**
1827
+ * Filename base for a recipe. Drafts include the per-turn suffix so two
1828
+ * concurrent turns can never clobber each other's draft files; verified
1829
+ * recipes use the bare slug.
1830
+ */
1831
+ private fileBaseName;
1832
+ /**
1833
+ * Absolute path to the .ts file for a recipe. Callers (e.g. ScriptRunner,
1834
+ * MainAgent's execute_script) use this to hand off the path to the tsx child.
1835
+ */
1836
+ getScriptPath(recipe: ScriptRecipe): string;
1837
+ /**
1838
+ * Absolute path to the metadata JSON for a recipe.
1839
+ */
1840
+ private getMetadataPath;
1841
+ /**
1842
+ * Get all VERIFIED recipes — drafts are filtered out so the matcher never
1843
+ * considers an unverified script. Loads from disk on first access.
1844
+ */
1845
+ getAll(): ScriptRecipe[];
1846
+ /**
1847
+ * Get a recipe by ID — returns drafts as well as verified scripts.
1848
+ * Used by MainAgent and the promotion path.
1849
+ */
1850
+ get(id: string): ScriptRecipe | null;
1851
+ /**
1852
+ * Number of verified recipes (matches `getAll().length`).
1853
+ */
1854
+ count(): number;
1855
+ /**
1856
+ * Save a recipe (create or update).
1857
+ * File is named after the script: "order-status-distribution.json"
1858
+ */
1859
+ save(recipe: ScriptRecipe): void;
1860
+ /**
1861
+ * Persist (or update) a draft recipe to disk. Always writes immediately so
1862
+ * the `.ts` body is visible in the IDE the moment MainAgent calls
1863
+ * `write_script`. Within one turn, retries that pass the same `recipeId`
1864
+ * overwrite the same files (the LLM rewriting itself); a fresh `recipeId`
1865
+ * is minted only on the first call of the turn.
1866
+ *
1867
+ * Filename includes the `turnId` suffix so concurrent turns never collide.
1868
+ */
1869
+ saveDraft(input: SaveDraftInput): ScriptRecipe;
1870
+ /**
1871
+ * Stamp the draft with the most recent execution failure so it is visible
1872
+ * in the metadata JSON without grepping logs. No-op if the recipe doesn't
1873
+ * exist or has already been promoted.
1874
+ */
1875
+ recordDraftError(recipeId: string, err: {
1876
+ phase: 'compile' | 'runtime' | 'timeout' | 'ipc';
1877
+ message: string;
1878
+ attempt: number;
1879
+ }): void;
1880
+ /**
1881
+ * Promote a successfully-executed draft into a verified script.
1882
+ *
1883
+ * - Renames the on-disk files from `<slug>-<turnId>.{ts,json}` to the bare
1884
+ * `<slug>.{ts,json}`. If a verified file with the same slug already exists
1885
+ * (concurrent turn won the race, or a prior session has it), the recipe
1886
+ * keeps its turn-suffixed filename so we never overwrite verified work —
1887
+ * the matcher keys on `recipe.id`, so two siblings with similar slugs is fine.
1888
+ * - Flips `status: 'verified'`, clears `lastError`, applies provenance
1889
+ * (`sourceIds`, `tables`) and optional fork lineage.
1890
+ */
1891
+ promoteToVerified(recipeId: string, input: PromoteToVerifiedInput): ScriptRecipe | null;
1892
+ /**
1893
+ * Drop a draft from disk + memory (e.g. when an outer error path wants to
1894
+ * clean up). Per the agreed policy, MainAgent's normal failure path does
1895
+ * NOT call this — failed drafts are kept on disk for the user to inspect.
1896
+ */
1897
+ discardDraft(recipeId: string): void;
1898
+ /**
1899
+ * Delete a recipe by ID.
1900
+ */
1901
+ delete(id: string): void;
1902
+ /**
1903
+ * Record a successful execution for a recipe.
1904
+ */
1905
+ recordSuccess(id: string): void;
1906
+ /**
1907
+ * Record a failed execution for a recipe.
1908
+ */
1909
+ recordFailure(id: string): void;
1910
+ /**
1911
+ * Get the failure rate for a recipe (0 to 1).
1912
+ * Returns 0 if the recipe has fewer than 5 total uses.
1913
+ */
1914
+ getFailureRate(id: string): number;
1915
+ private ensureLoaded;
1916
+ /**
1917
+ * Convert a script name to a safe filename.
1918
+ * "Order Status Distribution" → "order-status-distribution"
1919
+ */
1920
+ private toFileName;
1921
+ /**
1922
+ * Load all script files from disk.
1923
+ *
1924
+ * Supports two layouts:
1925
+ * - New (preferred): metadata/<name>.json + <name>.ts
1926
+ * - Legacy: <name>.json with embedded scriptBody (auto-migrated on next save)
1927
+ */
1928
+ private loadAllFromDisk;
1929
+ /**
1930
+ * Save a recipe to disk in split format:
1931
+ * metadata/<name>.json (metadata, no scriptBody)
1932
+ * <name>.ts (just the function body)
1933
+ *
1934
+ * If a legacy top-level <name>.json exists for the same recipe, remove it
1935
+ * so we don't end up with duplicate sources of truth.
1936
+ */
1937
+ private saveRecipeToDisk;
1938
+ /**
1939
+ * Delete a recipe's files from disk (both metadata + body, plus any legacy file).
1940
+ */
1941
+ private deleteRecipeFromDisk;
1942
+ }
1943
+
1944
+ /**
1945
+ * Main Agent (Orchestrator)
1946
+ *
1947
+ * A single LLM.streamWithTools() call that handles everything:
1948
+ * - Routing: decides which source(s) to query based on summaries
1949
+ * - Querying: calls source tools (each wraps an independent SourceAgent)
1950
+ * - Direct tools: calls pre-built function tools directly with LLM-provided params
1951
+ * - Re-querying: if data is wrong/incomplete, calls tools again with modified intent
1952
+ * - Analysis: generates final text response from the data
1953
+ *
1954
+ * Two tool types:
1955
+ * - "source" tools: main agent sees summaries, SourceAgent handles SQL generation independently
1956
+ * - "direct" tools: main agent calls fn() directly with structured params (no SourceAgent)
1957
+ */
1958
+
1959
+ declare class MainAgent {
1960
+ private externalTools;
1961
+ private workflows;
1962
+ private config;
1963
+ private streamBuffer;
1964
+ /**
1965
+ * Optional: when provided, MainAgent exposes the `write_script` /
1966
+ * `execute_script` tools to the LLM and persists drafts to disk via the
1967
+ * store. Headless callers (alert analyzer, metric resolver) omit these to
1968
+ * suppress script authoring entirely — drafts would otherwise leak onto
1969
+ * disk with no caller to promote or clean them up.
1970
+ */
1971
+ private scriptStore;
1972
+ private turnId;
1973
+ private createdFromPrompt;
1974
+ private scriptState;
1975
+ constructor(externalTools: ExternalTool[], config: AgentConfig, scriptStore?: ScriptStore, turnId?: string, streamBuffer?: StreamBuffer, workflows?: WorkflowDescriptor[]);
1976
+ private get scriptingEnabled();
1977
+ /**
1978
+ * Handle a user question using the multi-agent system.
1979
+ *
1980
+ * This is ONE LLM.streamWithTools() call. The LLM:
1981
+ * 1. Sees source summaries + direct tool descriptions in system prompt
1982
+ * 2. Decides which tool(s) to call (routing)
1983
+ * 3. Source tools → SourceAgent runs independently → returns data
1984
+ * 4. Direct tools → fn() called directly with LLM params → returns data
1985
+ * 5. Generates final analysis text
1986
+ */
1987
+ handleQuestion(userPrompt: string, apiKey?: string, conversationHistory?: string, streamCallback?: (chunk: string) => void): Promise<AgentResponse>;
1988
+ private handleWriteScript;
1989
+ private handleExecuteScript;
1990
+ /**
1991
+ * Build the AgentWrittenScript payload the caller will hand to
1992
+ * `ScriptStore.promoteToVerified()`. Only returned when a verified
1993
+ * successful execution is on record.
1994
+ */
1995
+ private buildSavedScript;
1996
+ private normalizeParameterList;
1997
+ /**
1998
+ * Use the schema embedding collection to pre-select relevant tables for
1999
+ * this source + intent. Returns a formatted schema block if confidence is
2000
+ * high (top match ≥ 0.55 and ≥3 candidates), otherwise null.
2001
+ *
2002
+ * When this returns a block, we can skip the SourceAgent's `search_schema`
2003
+ * loop and reduce iteration budget. When it returns null, the SourceAgent
2004
+ * falls back to the existing LLM-driven keyword search (same as today).
2005
+ */
2006
+ private preResolveSchema;
2007
+ /**
2008
+ * Execute a direct tool — call fn() with LLM-provided params, no SourceAgent.
2009
+ */
2010
+ private handleDirectTool;
2011
+ /**
2012
+ * Build the main agent's system prompt with source summaries, direct tool descriptions,
2013
+ * and workflow component descriptions.
2014
+ */
2015
+ private buildSystemPrompt;
2016
+ /**
2017
+ * Build tool definitions for source tools — summary-only descriptions.
2018
+ * The full schema is inside the SourceAgent which runs independently.
2019
+ */
2020
+ private buildSourceToolDefinitions;
2021
+ /**
2022
+ * Build tool definitions for direct tools — expose their actual params.
2023
+ * These are called directly by the main agent LLM, no SourceAgent.
2024
+ */
2025
+ private buildDirectToolDefinitions;
2026
+ /**
2027
+ * Capture a workflow selection. We do NOT execute anything — the LLM has
2028
+ * already extracted the props it wants the workflow rendered with. We
2029
+ * record the selection (via the capture callback) and return a short
2030
+ * acknowledgement so the LLM ends its turn cleanly without writing
2031
+ * analysis text or calling more tools.
2032
+ */
2033
+ private handleWorkflow;
2034
+ /**
2035
+ * Build LLM tool definitions for workflow components. The workflow's
2036
+ * propsSchema becomes the tool's input_schema so the LLM extracts props
2037
+ * directly from the prompt — same mechanic as direct tools.
2038
+ */
2039
+ private buildWorkflowToolDefinitions;
2040
+ /**
2041
+ * Format a source agent's result as a clean string for the main agent LLM.
2042
+ */
2043
+ private formatResultForMainAgent;
2044
+ /**
2045
+ * Get source summaries (for external inspection/debugging).
2046
+ */
2047
+ getSourceSummaries(): SourceSummary[];
938
2048
  }
939
2049
 
940
2050
  /**
@@ -947,6 +2057,170 @@ interface Action {
947
2057
  [key: string]: any;
948
2058
  }
949
2059
 
2060
+ type SystemPrompt = string | Anthropic.Messages.TextBlockParam[];
2061
+ interface LLMMessages {
2062
+ sys: SystemPrompt;
2063
+ user: string;
2064
+ prefill?: string;
2065
+ }
2066
+ interface LLMOptions {
2067
+ model?: string;
2068
+ maxTokens?: number;
2069
+ temperature?: number;
2070
+ topP?: number;
2071
+ apiKey?: string;
2072
+ partial?: (chunk: string) => void;
2073
+ }
2074
+ interface Tool {
2075
+ name: string;
2076
+ description: string;
2077
+ input_schema: {
2078
+ type: string;
2079
+ properties: Record<string, any>;
2080
+ required?: string[];
2081
+ };
2082
+ }
2083
+ declare class LLM {
2084
+ static text(messages: LLMMessages, options?: LLMOptions): Promise<string>;
2085
+ static stream<T = string>(messages: LLMMessages, options?: LLMOptions, json?: boolean): Promise<T extends string ? string : any>;
2086
+ static streamWithTools(messages: LLMMessages, tools: Tool[], toolHandler: (toolName: string, toolInput: any) => Promise<any>, options?: LLMOptions, maxIterations?: number): Promise<string>;
2087
+ /**
2088
+ * Normalize system prompt to Anthropic format
2089
+ * Converts string to array format if needed
2090
+ * @param sys - System prompt (string or array of blocks)
2091
+ * @returns Normalized system prompt for Anthropic API
2092
+ */
2093
+ private static _normalizeSystemPrompt;
2094
+ /**
2095
+ * Strip unpaired UTF-16 surrogates from every text field of a message set.
2096
+ *
2097
+ * A lone surrogate (from mid-pair string slicing or corrupt source data)
2098
+ * serializes to a bare `\udXXX` escape that strict JSON parsers — including
2099
+ * the one on Anthropic's API — reject with "no low surrogate in string",
2100
+ * failing the whole request. Sanitizing here, at the single boundary every
2101
+ * provider call flows through, guarantees no request can carry one.
2102
+ */
2103
+ private static _sanitizeMessages;
2104
+ /**
2105
+ * Log cache usage metrics from Anthropic API response
2106
+ * Shows cache hits, costs, and savings
2107
+ */
2108
+ private static _logCacheUsage;
2109
+ /**
2110
+ * Parse model string to extract provider and model name
2111
+ * @param modelString - Format: "provider/model-name" or just "model-name"
2112
+ * @returns [provider, modelName]
2113
+ *
2114
+ * @example
2115
+ * "anthropic/claude-sonnet-4-5" → ["anthropic", "claude-sonnet-4-5"]
2116
+ * "groq/openai/gpt-oss-120b" → ["groq", "openai/gpt-oss-120b"]
2117
+ * "claude-sonnet-4-5" → ["anthropic", "claude-sonnet-4-5"] (default)
2118
+ */
2119
+ private static _parseModel;
2120
+ private static _anthropicText;
2121
+ private static _anthropicStream;
2122
+ private static _anthropicStreamWithTools;
2123
+ private static _groqText;
2124
+ private static _groqStream;
2125
+ private static _geminiText;
2126
+ private static _geminiStream;
2127
+ /**
2128
+ * Recursively strip unsupported JSON Schema properties for Gemini
2129
+ * Gemini doesn't support: additionalProperties, $schema, etc.
2130
+ */
2131
+ private static _cleanSchemaForGemini;
2132
+ private static _geminiStreamWithTools;
2133
+ private static _openaiText;
2134
+ private static _openaiStream;
2135
+ private static _openaiStreamWithTools;
2136
+ /**
2137
+ * Parse JSON string, handling markdown code blocks and surrounding text
2138
+ * Enhanced version with jsonrepair to handle malformed JSON from LLMs
2139
+ * @param text - Text that may contain JSON wrapped in ```json...``` or with surrounding text
2140
+ * @returns Parsed JSON object or array
2141
+ */
2142
+ private static _parseJSON;
2143
+ }
2144
+
2145
+ interface CapturedLog {
2146
+ timestamp: number;
2147
+ level: 'info' | 'error' | 'warn' | 'debug';
2148
+ message: string;
2149
+ type?: 'explanation' | 'query' | 'general';
2150
+ data?: Record<string, any>;
2151
+ }
2152
+ /**
2153
+ * UILogCollector captures logs during user prompt processing
2154
+ * and sends them to runtime via ui_logs message with uiBlockId as the message id
2155
+ * Logs are sent in real-time for streaming effect in the UI
2156
+ * Respects the global log level configuration
2157
+ */
2158
+ declare class UILogCollector {
2159
+ private logs;
2160
+ private uiBlockId;
2161
+ private clientId;
2162
+ private sendMessage;
2163
+ private currentLogLevel;
2164
+ constructor(clientId: string, sendMessage: (message: Message) => void, uiBlockId?: string);
2165
+ /**
2166
+ * Check if logging is enabled (uiBlockId is provided)
2167
+ */
2168
+ isEnabled(): boolean;
2169
+ /**
2170
+ * Check if a message should be logged based on current log level
2171
+ */
2172
+ private shouldLog;
2173
+ /**
2174
+ * Add a log entry with timestamp and immediately send to runtime
2175
+ * Only logs that pass the log level filter are captured and sent
2176
+ */
2177
+ private addLog;
2178
+ /**
2179
+ * Send a single log to runtime immediately
2180
+ */
2181
+ private sendLogImmediately;
2182
+ /**
2183
+ * Log info message
2184
+ */
2185
+ info(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
2186
+ /**
2187
+ * Log error message
2188
+ */
2189
+ error(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
2190
+ /**
2191
+ * Log warning message
2192
+ */
2193
+ warn(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
2194
+ /**
2195
+ * Log debug message
2196
+ */
2197
+ debug(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void;
2198
+ /**
2199
+ * Log LLM explanation with typed metadata
2200
+ */
2201
+ logExplanation(message: string, explanation: string, data?: Record<string, any>): void;
2202
+ /**
2203
+ * Log generated query with typed metadata
2204
+ */
2205
+ logQuery(message: string, query: string, data?: Record<string, any>): void;
2206
+ /**
2207
+ * Send all collected logs at once (optional, for final summary)
2208
+ */
2209
+ sendAllLogs(): void;
2210
+ /**
2211
+ * Get all collected logs
2212
+ */
2213
+ getLogs(): CapturedLog[];
2214
+ /**
2215
+ * Clear all logs
2216
+ */
2217
+ clearLogs(): void;
2218
+ /**
2219
+ * Set uiBlockId (in case it's provided later)
2220
+ */
2221
+ setUIBlockId(uiBlockId: string): void;
2222
+ }
2223
+
950
2224
  /**
951
2225
  * UIBlock represents a single user and assistant message block in a thread
952
2226
  * Contains user question, component metadata, component data, text response, and available actions
@@ -985,6 +2259,7 @@ declare class UIBlock {
985
2259
  * Get component metadata
986
2260
  */
987
2261
  getComponentMetadata(): Record<string, any>;
2262
+ getTextResponse(): string;
988
2263
  /**
989
2264
  * Set or update component metadata
990
2265
  */
@@ -1013,10 +2288,6 @@ declare class UIBlock {
1013
2288
  * Set or update component data with size and row limits
1014
2289
  */
1015
2290
  setComponentData(data: Record<string, any>): void;
1016
- /**
1017
- * Get text response
1018
- */
1019
- getTextResponse(): string | null;
1020
2291
  /**
1021
2292
  * Set or update text response
1022
2293
  */
@@ -1033,6 +2304,10 @@ declare class UIBlock {
1033
2304
  * @returns Promise resolving to Action[]
1034
2305
  */
1035
2306
  getOrFetchActions(generateFn: () => Promise<Action[]>): Promise<Action[]>;
2307
+ /**
2308
+ * Set or replace all actions
2309
+ */
2310
+ setActions(actions: Action[]): void;
1036
2311
  /**
1037
2312
  * Add a single action (only if actions are resolved)
1038
2313
  */
@@ -1128,12 +2403,20 @@ declare class Thread {
1128
2403
 
1129
2404
  /**
1130
2405
  * ThreadManager manages all threads globally
1131
- * Provides methods to create, retrieve, and delete threads
2406
+ * Provides methods to create, retrieve, and delete threads.
2407
+ * Includes automatic cleanup to prevent unbounded memory growth.
1132
2408
  */
1133
2409
  declare class ThreadManager {
1134
2410
  private static instance;
1135
2411
  private threads;
2412
+ private cleanupInterval;
2413
+ private readonly threadTtlMs;
1136
2414
  private constructor();
2415
+ /**
2416
+ * Periodically remove threads older than 7 days.
2417
+ * Runs every hour to avoid frequent iteration over the map.
2418
+ */
2419
+ private startCleanup;
1137
2420
  /**
1138
2421
  * Get singleton instance of ThreadManager
1139
2422
  */
@@ -1263,15 +2546,17 @@ declare const STORAGE_CONFIG: {
1263
2546
  */
1264
2547
  MAX_ROWS_PER_BLOCK: number;
1265
2548
  /**
1266
- * Maximum size in bytes per UIBlock (1MB)
2549
+ * Maximum size in bytes per UIBlock (500KB - reduced to save memory)
1267
2550
  */
1268
2551
  MAX_SIZE_PER_BLOCK_BYTES: number;
1269
2552
  /**
1270
2553
  * Number of days to keep threads before cleanup
2554
+ * Note: This is for in-memory storage. Conversations are also persisted to database.
1271
2555
  */
1272
2556
  THREAD_RETENTION_DAYS: number;
1273
2557
  /**
1274
2558
  * Number of days to keep UIBlocks before cleanup
2559
+ * Note: This is for in-memory storage. Data is also persisted to database.
1275
2560
  */
1276
2561
  UIBLOCK_RETENTION_DAYS: number;
1277
2562
  };
@@ -1288,14 +2573,701 @@ declare const CONTEXT_CONFIG: {
1288
2573
  MAX_CONVERSATION_CONTEXT_BLOCKS: number;
1289
2574
  };
1290
2575
 
1291
- declare const SDK_VERSION = "0.0.8";
2576
+ /**
2577
+ * LLM Usage Logger - Tracks token usage, costs, and timing for all LLM API calls
2578
+ */
2579
+ interface LLMUsageEntry {
2580
+ timestamp: string;
2581
+ requestId: string;
2582
+ provider: string;
2583
+ model: string;
2584
+ method: string;
2585
+ inputTokens: number;
2586
+ outputTokens: number;
2587
+ cacheReadTokens?: number;
2588
+ cacheWriteTokens?: number;
2589
+ totalTokens: number;
2590
+ costUSD: number;
2591
+ durationMs: number;
2592
+ toolCalls?: number;
2593
+ success: boolean;
2594
+ error?: string;
2595
+ }
2596
+ declare class LLMUsageLogger {
2597
+ private logStream;
2598
+ private logPath;
2599
+ private enabled;
2600
+ private sessionStats;
2601
+ constructor();
2602
+ private initLogStream;
2603
+ private writeHeader;
2604
+ /**
2605
+ * Calculate cost based on token usage and model
2606
+ */
2607
+ calculateCost(model: string, inputTokens: number, outputTokens: number, cacheReadTokens?: number, cacheWriteTokens?: number): number;
2608
+ /**
2609
+ * Log an LLM API call
2610
+ */
2611
+ log(entry: LLMUsageEntry): void;
2612
+ /**
2613
+ * Log session summary (call at end of request)
2614
+ */
2615
+ logSessionSummary(requestContext?: string): void;
2616
+ /**
2617
+ * Reset session stats (call at start of new user request)
2618
+ */
2619
+ resetSession(): void;
2620
+ /**
2621
+ * Reset the log file for a new request (clears previous logs)
2622
+ * Call this at the start of each USER_PROMPT_REQ
2623
+ */
2624
+ resetLogFile(requestContext?: string): void;
2625
+ /**
2626
+ * Get current session stats
2627
+ */
2628
+ getSessionStats(): {
2629
+ totalCalls: number;
2630
+ totalInputTokens: number;
2631
+ totalOutputTokens: number;
2632
+ totalCacheReadTokens: number;
2633
+ totalCacheWriteTokens: number;
2634
+ totalCostUSD: number;
2635
+ totalDurationMs: number;
2636
+ };
2637
+ /**
2638
+ * Generate a unique request ID
2639
+ */
2640
+ generateRequestId(): string;
2641
+ }
2642
+ declare const llmUsageLogger: LLMUsageLogger;
2643
+
2644
+ /**
2645
+ * User Prompt Error Logger - Captures detailed errors for USER_PROMPT_REQ
2646
+ * Logs full error details including raw strings for parse failures
2647
+ */
2648
+ declare class UserPromptErrorLogger {
2649
+ private logStream;
2650
+ private logPath;
2651
+ private enabled;
2652
+ private hasErrors;
2653
+ constructor();
2654
+ /**
2655
+ * Reset the error log file for a new request
2656
+ */
2657
+ resetLogFile(requestContext?: string): void;
2658
+ /**
2659
+ * Log a JSON parse error with the raw string that failed
2660
+ */
2661
+ logJsonParseError(context: string, rawString: string, error: Error): void;
2662
+ /**
2663
+ * Log a general error with full details
2664
+ */
2665
+ logError(context: string, error: Error | string, additionalData?: Record<string, any>): void;
2666
+ /**
2667
+ * Log a SQL query error with the full query
2668
+ */
2669
+ logSqlError(query: string, error: Error | string, params?: any[]): void;
2670
+ /**
2671
+ * Log an LLM API error
2672
+ */
2673
+ logLlmError(provider: string, model: string, method: string, error: Error | string, requestData?: any): void;
2674
+ /**
2675
+ * Log tool execution error
2676
+ */
2677
+ logToolError(toolName: string, toolInput: any, error: Error | string): void;
2678
+ /**
2679
+ * Write final summary if there were errors
2680
+ */
2681
+ writeSummary(): void;
2682
+ /**
2683
+ * Check if any errors were logged
2684
+ */
2685
+ hadErrors(): boolean;
2686
+ private write;
2687
+ }
2688
+ declare const userPromptErrorLogger: UserPromptErrorLogger;
2689
+
2690
+ /**
2691
+ * BM25L Reranker for hybrid semantic search
2692
+ *
2693
+ * BM25L is an improved variant of BM25 that provides better handling of
2694
+ * long documents and term frequency saturation. This implementation is
2695
+ * designed to rerank semantic search results from ChromaDB.
2696
+ *
2697
+ * The hybrid approach combines:
2698
+ * 1. Semantic similarity from ChromaDB embeddings (dense vectors)
2699
+ * 2. Lexical matching from BM25L (sparse, keyword-based)
2700
+ *
2701
+ * This addresses the weakness of pure semantic search which may miss
2702
+ * exact keyword matches that are important for user intent.
2703
+ */
2704
+ interface BM25LOptions {
2705
+ /** Term frequency saturation parameter (default: 1.5) */
2706
+ k1?: number;
2707
+ /** Length normalization parameter (default: 0.75) */
2708
+ b?: number;
2709
+ /** Lower-bound adjustment from BM25L paper (default: 0.5) */
2710
+ delta?: number;
2711
+ }
2712
+ interface RerankedResult<T> {
2713
+ item: T;
2714
+ originalIndex: number;
2715
+ semanticScore: number;
2716
+ bm25Score: number;
2717
+ hybridScore: number;
2718
+ }
2719
+ interface HybridSearchOptions extends BM25LOptions {
2720
+ /** Weight for semantic score (0-1, default: 0.7) */
2721
+ semanticWeight?: number;
2722
+ /** Weight for BM25 score (0-1, default: 0.3) */
2723
+ bm25Weight?: number;
2724
+ /** Minimum hybrid score threshold (0-1, default: 0) */
2725
+ minScore?: number;
2726
+ }
2727
+ /**
2728
+ * BM25L implementation for lexical scoring
2729
+ */
2730
+ declare class BM25L {
2731
+ private k1;
2732
+ private b;
2733
+ private delta;
2734
+ private documents;
2735
+ private docLengths;
2736
+ private avgDocLength;
2737
+ private termDocFreq;
2738
+ /**
2739
+ * @param documents - Array of raw documents (strings)
2740
+ * @param opts - Optional BM25L parameters
2741
+ */
2742
+ constructor(documents?: string[], opts?: BM25LOptions);
2743
+ /**
2744
+ * Tokenize text into lowercase alphanumeric tokens
2745
+ */
2746
+ tokenize(text: string): string[];
2747
+ /**
2748
+ * Compute IDF (Inverse Document Frequency) with smoothing
2749
+ */
2750
+ private idf;
2751
+ /**
2752
+ * Compute BM25L score for a single document
2753
+ */
2754
+ score(query: string, docIndex: number): number;
2755
+ /**
2756
+ * Search and rank all documents
2757
+ */
2758
+ search(query: string): Array<{
2759
+ index: number;
2760
+ score: number;
2761
+ }>;
2762
+ }
2763
+ /**
2764
+ * Hybrid reranker that combines semantic and BM25L scores
2765
+ *
2766
+ * @param query - The search query
2767
+ * @param items - Array of items to rerank
2768
+ * @param getDocument - Function to extract document text from an item
2769
+ * @param getSemanticScore - Function to extract semantic similarity score from an item
2770
+ * @param options - Hybrid search options
2771
+ * @returns Reranked items with hybrid scores
2772
+ */
2773
+ declare function hybridRerank<T>(query: string, items: T[], getDocument: (item: T) => string, getSemanticScore: (item: T) => number, options?: HybridSearchOptions): RerankedResult<T>[];
2774
+ /**
2775
+ * Simple reranking function for ChromaDB results
2776
+ *
2777
+ * This is a convenience wrapper for reranking ChromaDB query results
2778
+ * that follow the standard { ids, documents, metadatas, distances } format.
2779
+ *
2780
+ * @param query - The search query
2781
+ * @param chromaResults - ChromaDB query results
2782
+ * @param options - Hybrid search options
2783
+ * @returns Reranked results with hybrid scores
2784
+ */
2785
+ declare function rerankChromaResults(query: string, chromaResults: {
2786
+ ids: string[][];
2787
+ documents: (string | null)[][];
2788
+ metadatas: Record<string, any>[][];
2789
+ distances: number[][];
2790
+ }, options?: HybridSearchOptions): Array<{
2791
+ id: string;
2792
+ document: string | null;
2793
+ metadata: Record<string, any>;
2794
+ distance: number;
2795
+ semanticScore: number;
2796
+ bm25Score: number;
2797
+ hybridScore: number;
2798
+ }>;
2799
+ /**
2800
+ * Rerank conversation search results specifically
2801
+ *
2802
+ * This function is designed to work with the conversation-history.search collection
2803
+ * where we need to fetch more results initially and then rerank them.
2804
+ *
2805
+ * @param query - The user's search query
2806
+ * @param results - Array of conversation search results from ChromaDB
2807
+ * @param options - Hybrid search options
2808
+ * @returns Reranked results sorted by hybrid score
2809
+ */
2810
+ declare function rerankConversationResults<T extends {
2811
+ userPrompt?: string;
2812
+ similarity?: number;
2813
+ }>(query: string, results: T[], options?: HybridSearchOptions): Array<T & {
2814
+ hybridScore: number;
2815
+ bm25Score: number;
2816
+ }>;
2817
+
2818
+ /**
2819
+ * QueryExecutionService - Handles all query execution, validation, and retry logic
2820
+ * Extracted from BaseLLM for better separation of concerns
2821
+ */
2822
+
2823
+ /**
2824
+ * Context for component when requesting query fix
2825
+ */
2826
+ interface ComponentContext {
2827
+ name: string;
2828
+ type: string;
2829
+ title?: string;
2830
+ }
2831
+ /**
2832
+ * Result of query validation
2833
+ */
2834
+ interface QueryValidationResult {
2835
+ component: Component | null;
2836
+ queryKey: string;
2837
+ result: any;
2838
+ validated: boolean;
2839
+ }
2840
+ /**
2841
+ * Result of batch query validation
2842
+ */
2843
+ interface BatchValidationResult {
2844
+ components: Component[];
2845
+ queryResults: Map<string, any>;
2846
+ }
2847
+ /**
2848
+ * Configuration for QueryExecutionService
2849
+ */
2850
+ interface QueryExecutionServiceConfig {
2851
+ defaultLimit: number;
2852
+ getModelForTask: (taskType: 'simple' | 'complex') => string;
2853
+ getApiKey: (apiKey?: string) => string | undefined;
2854
+ providerName: string;
2855
+ }
2856
+ /**
2857
+ * QueryExecutionService handles all query-related operations
2858
+ */
2859
+ declare class QueryExecutionService {
2860
+ private config;
2861
+ constructor(config: QueryExecutionServiceConfig);
2862
+ /**
2863
+ * Get the cache key for a query
2864
+ * This ensures the cache key matches what the frontend will send
2865
+ */
2866
+ getQueryCacheKey(query: any): string;
2867
+ /**
2868
+ * Execute a query against the database
2869
+ * @param query - The SQL query to execute (string or object with sql/values)
2870
+ * @param collections - Collections object containing database execute function
2871
+ * @returns Object with result data and cache key
2872
+ */
2873
+ executeQuery(query: any, collections: any): Promise<{
2874
+ result: any;
2875
+ cacheKey: string;
2876
+ }>;
2877
+ /**
2878
+ * Request the LLM to fix a failed SQL query
2879
+ * @param failedQuery - The query that failed execution
2880
+ * @param errorMessage - The error message from the failed execution
2881
+ * @param componentContext - Context about the component
2882
+ * @param apiKey - Optional API key
2883
+ * @returns Fixed query string
2884
+ */
2885
+ requestQueryFix(failedQuery: string, errorMessage: string, componentContext: ComponentContext, apiKey?: string): Promise<string>;
2886
+ /**
2887
+ * Validate a single component's query with retry logic
2888
+ * @param component - The component to validate
2889
+ * @param collections - Collections object containing database execute function
2890
+ * @param apiKey - Optional API key for LLM calls
2891
+ * @returns Validation result with component, query key, and result
2892
+ */
2893
+ validateSingleQuery(component: Component, collections: any, apiKey?: string): Promise<QueryValidationResult>;
2894
+ /**
2895
+ * Validate multiple component queries in parallel
2896
+ * @param components - Array of components with potential queries
2897
+ * @param collections - Collections object containing database execute function
2898
+ * @param apiKey - Optional API key for LLM calls
2899
+ * @returns Object with validated components and query results map
2900
+ */
2901
+ validateComponentQueries(components: Component[], collections: any, apiKey?: string): Promise<BatchValidationResult>;
2902
+ }
2903
+
2904
+ /**
2905
+ * Task types for model selection
2906
+ * - 'complex': Text generation, component matching, parameter adaptation (uses best model in balanced mode)
2907
+ * - 'simple': Classification, action generation (uses fast model in balanced mode)
2908
+ */
2909
+ type TaskType = 'complex' | 'simple';
2910
+ interface BaseLLMConfig {
2911
+ model?: string;
2912
+ fastModel?: string;
2913
+ defaultLimit?: number;
2914
+ apiKey?: string;
2915
+ /**
2916
+ * Model selection strategy:
2917
+ * - 'best': Use best model for all tasks (highest quality, higher cost)
2918
+ * - 'fast': Use fast model for all tasks (lower quality, lower cost)
2919
+ * - 'balanced': Use best model for complex tasks, fast model for simple tasks (default)
2920
+ */
2921
+ modelStrategy?: ModelStrategy;
2922
+ conversationSimilarityThreshold?: number;
2923
+ }
2924
+ /**
2925
+ * BaseLLM abstract class for AI-powered component generation and matching
2926
+ * Provides common functionality for all LLM providers
2927
+ */
2928
+ declare abstract class BaseLLM {
2929
+ protected model: string;
2930
+ protected fastModel: string;
2931
+ protected defaultLimit: number;
2932
+ protected apiKey?: string;
2933
+ protected modelStrategy: ModelStrategy;
2934
+ protected conversationSimilarityThreshold: number;
2935
+ protected queryService: QueryExecutionService;
2936
+ constructor(config?: BaseLLMConfig);
2937
+ /**
2938
+ * Get the appropriate model based on task type and model strategy
2939
+ * @param taskType - 'complex' for text generation/matching, 'simple' for classification/actions
2940
+ * @returns The model string to use for this task
2941
+ */
2942
+ protected getModelForTask(taskType: TaskType): string;
2943
+ /**
2944
+ * Set the model strategy at runtime
2945
+ * @param strategy - 'best', 'fast', or 'balanced'
2946
+ */
2947
+ setModelStrategy(strategy: ModelStrategy): void;
2948
+ /**
2949
+ * Get the current model strategy
2950
+ * @returns The current model strategy
2951
+ */
2952
+ getModelStrategy(): ModelStrategy;
2953
+ /**
2954
+ * Set the conversation similarity threshold at runtime
2955
+ * @param threshold - Value between 0 and 1 (e.g., 0.8 = 80% similarity required)
2956
+ */
2957
+ setConversationSimilarityThreshold(threshold: number): void;
2958
+ /**
2959
+ * Get the current conversation similarity threshold
2960
+ * @returns The current threshold value
2961
+ */
2962
+ getConversationSimilarityThreshold(): number;
2963
+ /**
2964
+ * Get the default model for this provider (used for complex tasks like text generation)
2965
+ */
2966
+ protected abstract getDefaultModel(): string;
2967
+ /**
2968
+ * Get the default fast model for this provider (used for simple tasks: classification, matching, actions)
2969
+ * Should return a cheaper/faster model like Haiku for Anthropic
2970
+ */
2971
+ protected abstract getDefaultFastModel(): string;
2972
+ /**
2973
+ * Get the default API key from environment
2974
+ */
2975
+ protected abstract getDefaultApiKey(): string | undefined;
2976
+ /**
2977
+ * Get the provider name (for logging)
2978
+ */
2979
+ protected abstract getProviderName(): string;
2980
+ /**
2981
+ * Get the API key (from instance, parameter, or environment)
2982
+ */
2983
+ protected getApiKey(apiKey?: string): string | undefined;
2984
+ /**
2985
+ * Check if a component contains a Form (data_modification component)
2986
+ * Forms have hardcoded defaultValues that become stale when cached
2987
+ * This checks both single Form components and Forms inside MultiComponentContainer
2988
+ */
2989
+ protected containsFormComponent(component: any): boolean;
2990
+ /**
2991
+ * Match components from text response suggestions and generate follow-up questions
2992
+ * Takes a text response with component suggestions (c1:type format) and matches with available components
2993
+ * Also generates title, description, and intelligent follow-up questions (actions) based on the analysis
2994
+ * All components are placed in a default MultiComponentContainer layout
2995
+ * @param analysisContent - The text response containing component suggestions
2996
+ * @param components - List of available components
2997
+ * @param apiKey - Optional API key
2998
+ * @param componentStreamCallback - Optional callback to stream primary KPI component as soon as it's identified
2999
+ * @returns Object containing matched components, layout title/description, and follow-up actions
3000
+ */
3001
+ matchComponentsFromAnalysis(analysisContent: string, components: Component[], userPrompt: string, apiKey?: string, componentStreamCallback?: (component: Component) => void, deferredTools?: any[], executedTools?: any[], collections?: any, userId?: string): Promise<{
3002
+ components: Component[];
3003
+ layoutTitle: string;
3004
+ layoutDescription: string;
3005
+ actions: Action[];
3006
+ }>;
3007
+ /**
3008
+ * Classify user question into category and detect external tools needed
3009
+ * Determines if question is for data analysis, requires external tools, or needs text response
3010
+ */
3011
+ classifyQuestionCategory(userPrompt: string, apiKey?: string, conversationHistory?: string, externalTools?: any[]): Promise<{
3012
+ category: 'data_analysis' | 'data_modification' | 'general';
3013
+ externalTools: Array<{
3014
+ type: string;
3015
+ name: string;
3016
+ description: string;
3017
+ parameters: Record<string, any>;
3018
+ }>;
3019
+ dataAnalysisType?: 'visualization' | 'calculation' | 'comparison' | 'trend';
3020
+ reasoning: string;
3021
+ confidence: number;
3022
+ }>;
3023
+ /**
3024
+ * Adapt UI block parameters based on current user question
3025
+ * Takes a matched UI block from semantic search and modifies its props to answer the new question
3026
+ * Also adapts the cached text response to match the new question
3027
+ */
3028
+ adaptUIBlockParameters(currentUserPrompt: string, originalUserPrompt: string, matchedUIBlock: any, apiKey?: string, cachedTextResponse?: string): Promise<{
3029
+ success: boolean;
3030
+ adaptedComponent?: Component;
3031
+ adaptedTextResponse?: string;
3032
+ parametersChanged?: Array<{
3033
+ field: string;
3034
+ reason: string;
3035
+ }>;
3036
+ explanation: string;
3037
+ }>;
3038
+ /**
3039
+ * Generate text-based response for user question
3040
+ * This provides conversational text responses instead of component generation
3041
+ * Supports tool calling for query execution with automatic retry on errors (max 3 attempts)
3042
+ * After generating text response, if components are provided, matches suggested components
3043
+ */
3044
+ generateTextResponse(userPrompt: string, apiKey?: string, conversationHistory?: string, streamCallback?: (chunk: string) => void, collections?: any, components?: Component[], externalTools?: any[], category?: 'data_analysis' | 'data_modification' | 'general', userId?: string): Promise<T_RESPONSE>;
3045
+ /**
3046
+ * Main orchestration function with semantic search and multi-step classification
3047
+ * NEW FLOW (Recommended):
3048
+ * 1. Semantic search: Check previous conversations (>60% match)
3049
+ * - If match found → Adapt UI block parameters and return
3050
+ * 2. Category classification: Determine if data_analysis, requires_external_tools, or text_response
3051
+ * 3. Route appropriately based on category and response mode
3052
+ */
3053
+ handleUserRequest(userPrompt: string, components: Component[], apiKey?: string, conversationHistory?: string, responseMode?: 'component' | 'text', streamCallback?: (chunk: string) => void, collections?: any, externalTools?: any[], userId?: string): Promise<T_RESPONSE>;
3054
+ /**
3055
+ * Generate next questions that the user might ask based on the original prompt and generated component
3056
+ * This helps provide intelligent suggestions for follow-up queries
3057
+ * For general/conversational questions without components, pass textResponse instead
3058
+ */
3059
+ generateNextQuestions(originalUserPrompt: string, component?: Component | null, componentData?: Record<string, unknown>, apiKey?: string, conversationHistory?: string, textResponse?: string): Promise<string[]>;
3060
+ }
3061
+
3062
+ interface AnthropicLLMConfig extends BaseLLMConfig {
3063
+ }
3064
+ /**
3065
+ * AnthropicLLM class for handling AI-powered component generation and matching using Anthropic Claude
3066
+ */
3067
+ declare class AnthropicLLM extends BaseLLM {
3068
+ constructor(config?: AnthropicLLMConfig);
3069
+ protected getDefaultModel(): string;
3070
+ protected getDefaultFastModel(): string;
3071
+ protected getDefaultApiKey(): string | undefined;
3072
+ protected getProviderName(): string;
3073
+ }
3074
+ declare const anthropicLLM: AnthropicLLM;
3075
+
3076
+ interface GroqLLMConfig extends BaseLLMConfig {
3077
+ }
3078
+ /**
3079
+ * GroqLLM class for handling AI-powered component generation and matching using Groq
3080
+ */
3081
+ declare class GroqLLM extends BaseLLM {
3082
+ constructor(config?: GroqLLMConfig);
3083
+ protected getDefaultModel(): string;
3084
+ protected getDefaultFastModel(): string;
3085
+ protected getDefaultApiKey(): string | undefined;
3086
+ protected getProviderName(): string;
3087
+ }
3088
+ declare const groqLLM: GroqLLM;
3089
+
3090
+ interface GeminiLLMConfig extends BaseLLMConfig {
3091
+ }
3092
+ /**
3093
+ * GeminiLLM class for handling AI-powered component generation and matching using Google Gemini
3094
+ */
3095
+ declare class GeminiLLM extends BaseLLM {
3096
+ constructor(config?: GeminiLLMConfig);
3097
+ protected getDefaultModel(): string;
3098
+ protected getDefaultFastModel(): string;
3099
+ protected getDefaultApiKey(): string | undefined;
3100
+ protected getProviderName(): string;
3101
+ }
3102
+ declare const geminiLLM: GeminiLLM;
3103
+
3104
+ interface OpenAILLMConfig extends BaseLLMConfig {
3105
+ }
3106
+ /**
3107
+ * OpenAILLM class for handling AI-powered component generation and matching using OpenAI GPT models
3108
+ */
3109
+ declare class OpenAILLM extends BaseLLM {
3110
+ constructor(config?: OpenAILLMConfig);
3111
+ protected getDefaultModel(): string;
3112
+ protected getDefaultFastModel(): string;
3113
+ protected getDefaultApiKey(): string | undefined;
3114
+ protected getProviderName(): string;
3115
+ }
3116
+ declare const openaiLLM: OpenAILLM;
3117
+
3118
+ /**
3119
+ * Query Cache — Two mechanisms:
3120
+ *
3121
+ * 1. `cache` (query string → result data) — TTL-based with max size, for avoiding re-execution
3122
+ * of recently validated queries. True LRU eviction: reads bubble entries to the back via
3123
+ * delete+re-set so the oldest *unused* entry is evicted, not the oldest *inserted*.
3124
+ *
3125
+ * 2. Encrypted queryId tokens — SQL is encrypted into the queryId itself (self-contained).
3126
+ * No server-side storage needed for SQL mappings. The token is decrypted on each request.
3127
+ * This eliminates the unbounded queryIdCache that previously grew forever and caused
3128
+ * memory bloat (hundreds of MBs after thousands of queries).
3129
+ *
3130
+ * Result data can still be cached temporarily via the data cache (mechanism 1).
3131
+ */
3132
+ declare class QueryCache {
3133
+ private cache;
3134
+ private ttlMs;
3135
+ private maxCacheSize;
3136
+ private cleanupInterval;
3137
+ private readonly algorithm;
3138
+ private encryptionKey;
3139
+ constructor();
3140
+ /**
3141
+ * Set the cache TTL (Time To Live)
3142
+ * @param minutes - TTL in minutes (default: 10)
3143
+ */
3144
+ setTTL(minutes: number): void;
3145
+ /**
3146
+ * Get the current TTL in minutes
3147
+ */
3148
+ getTTL(): number;
3149
+ /**
3150
+ * Store query result in data cache.
3151
+ * If the key already exists, it's removed first so the re-insert places it
3152
+ * at the back of the iteration order (LRU). Eviction only fires when adding
3153
+ * a genuinely new key past the size limit.
3154
+ */
3155
+ set(query: string, data: any): void;
3156
+ /**
3157
+ * Get cached result if exists and not expired.
3158
+ * On hit, re-inserts the entry so it moves to the back of the Map's
3159
+ * iteration order — turning FIFO eviction into true LRU.
3160
+ */
3161
+ get(query: string): any | null;
3162
+ /**
3163
+ * Check if query exists in cache (not expired)
3164
+ */
3165
+ has(query: string): boolean;
3166
+ /**
3167
+ * Remove a specific query from cache
3168
+ */
3169
+ delete(query: string): void;
3170
+ /**
3171
+ * Clear all cached entries
3172
+ */
3173
+ clear(): void;
3174
+ /**
3175
+ * Get cache statistics
3176
+ */
3177
+ getStats(): {
3178
+ size: number;
3179
+ queryIdCount: number;
3180
+ oldestEntryAge: number | null;
3181
+ };
3182
+ /**
3183
+ * Start periodic cleanup of expired data cache entries.
3184
+ */
3185
+ private startCleanup;
3186
+ /**
3187
+ * Encrypt a payload into a self-contained token.
3188
+ */
3189
+ private encrypt;
3190
+ /**
3191
+ * Decrypt a token back to the original payload.
3192
+ */
3193
+ private decrypt;
3194
+ /**
3195
+ * Store a query by generating an encrypted token as queryId.
3196
+ * The SQL is encrypted INTO the token — nothing stored in memory.
3197
+ * If data is provided, it's cached temporarily in the data cache.
3198
+ */
3199
+ storeQuery(query: any, data?: any): string;
3200
+ /**
3201
+ * Get a stored query by decrypting its token.
3202
+ * Returns the SQL + any cached result data.
3203
+ */
3204
+ getQuery(queryId: string): {
3205
+ query: any;
3206
+ data: any;
3207
+ } | null;
3208
+ /**
3209
+ * Update cached data for a queryId token
3210
+ */
3211
+ setQueryData(queryId: string, data: any): void;
3212
+ /**
3213
+ * Stop cleanup interval (for graceful shutdown)
3214
+ */
3215
+ destroy(): void;
3216
+ }
3217
+ declare const queryCache: QueryCache;
3218
+
3219
+ /**
3220
+ * Manages conversation history scoped per user + dashboard.
3221
+ * Each user-dashboard pair has its own isolated history that expires after a configurable TTL.
3222
+ */
3223
+ declare class DashboardConversationHistory {
3224
+ private histories;
3225
+ private ttlMs;
3226
+ private maxEntries;
3227
+ private cleanupInterval;
3228
+ constructor();
3229
+ /**
3230
+ * Set the TTL for dashboard histories
3231
+ * @param minutes - TTL in minutes
3232
+ */
3233
+ setTTL(minutes: number): void;
3234
+ /**
3235
+ * Set max entries per dashboard
3236
+ */
3237
+ setMaxEntries(max: number): void;
3238
+ /**
3239
+ * Add a conversation entry for a user's dashboard
3240
+ */
3241
+ addEntry(dashboardId: string, userPrompt: string, componentSummary: string, userId?: string): void;
3242
+ /**
3243
+ * Get formatted conversation history for a user's dashboard
3244
+ */
3245
+ getHistory(dashboardId: string, userId?: string): string;
3246
+ /**
3247
+ * Clear history for a specific user's dashboard
3248
+ */
3249
+ clearDashboard(dashboardId: string, userId?: string): void;
3250
+ /**
3251
+ * Clear all dashboard histories
3252
+ */
3253
+ clearAll(): void;
3254
+ /**
3255
+ * Start periodic cleanup of expired histories
3256
+ */
3257
+ private startCleanup;
3258
+ /**
3259
+ * Stop cleanup interval (for graceful shutdown)
3260
+ */
3261
+ destroy(): void;
3262
+ }
3263
+ declare const dashboardConversationHistory: DashboardConversationHistory;
3264
+
1292
3265
  type MessageTypeHandler = (message: IncomingMessage) => void | Promise<void>;
1293
3266
  declare class SuperatomSDK {
1294
3267
  private ws;
1295
3268
  private url;
1296
- private apiKey;
3269
+ private apiKey?;
1297
3270
  private projectId;
1298
- private userId;
1299
3271
  private type;
1300
3272
  private bundleDir;
1301
3273
  private messageHandlers;
@@ -1305,15 +3277,30 @@ declare class SuperatomSDK {
1305
3277
  private maxReconnectAttempts;
1306
3278
  private collections;
1307
3279
  private components;
3280
+ private tools;
3281
+ private workflows;
1308
3282
  private anthropicApiKey;
1309
3283
  private groqApiKey;
3284
+ private geminiApiKey;
3285
+ private openaiApiKey;
1310
3286
  private llmProviders;
3287
+ private databaseType;
3288
+ private modelStrategy;
3289
+ private mainAgentModel;
3290
+ private sourceAgentModel;
3291
+ private dashCompModels?;
3292
+ private conversationSimilarityThreshold;
1311
3293
  private userManager;
1312
3294
  private dashboardManager;
1313
3295
  private reportManager;
3296
+ private pingInterval;
3297
+ private lastPong;
3298
+ private readonly PING_INTERVAL_MS;
3299
+ private readonly PONG_TIMEOUT_MS;
1314
3300
  constructor(config: SuperatomSDKConfig);
1315
3301
  /**
1316
3302
  * Initialize PromptLoader and load prompts into memory
3303
+ * Tries to load from file system first, then falls back to hardcoded prompts
1317
3304
  */
1318
3305
  private initializePromptLoader;
1319
3306
  /**
@@ -1349,9 +3336,11 @@ declare class SuperatomSDK {
1349
3336
  */
1350
3337
  private handleMessage;
1351
3338
  /**
1352
- * Send a message to the Superatom service
3339
+ * Send a message to the Superatom service.
3340
+ * Returns true if the message was sent, false if the WebSocket is not connected.
3341
+ * Does NOT throw on closed connections — callers can check the return value if needed.
1353
3342
  */
1354
- send(message: Message): void;
3343
+ send(message: Message): boolean;
1355
3344
  /**
1356
3345
  * Register a message handler to receive all messages
1357
3346
  */
@@ -1377,7 +3366,69 @@ declare class SuperatomSDK {
1377
3366
  */
1378
3367
  addCollection<TParams = any, TResult = any>(collectionName: string, operation: CollectionOperation | string, handler: CollectionHandler<TParams, TResult>): void;
1379
3368
  private handleReconnect;
3369
+ /**
3370
+ * Start heartbeat to keep WebSocket connection alive
3371
+ * Sends PING every 3 minutes to prevent idle timeout from cloud infrastructure
3372
+ */
3373
+ private startHeartbeat;
3374
+ /**
3375
+ * Stop the heartbeat interval
3376
+ */
3377
+ private stopHeartbeat;
3378
+ /**
3379
+ * Handle PONG response from server
3380
+ */
3381
+ private handlePong;
1380
3382
  private storeComponents;
3383
+ /**
3384
+ * Set tools for the SDK instance
3385
+ */
3386
+ setTools(tools: Tool$1[]): void;
3387
+ /**
3388
+ * Get the stored tools
3389
+ */
3390
+ getTools(): Tool$1[];
3391
+ /**
3392
+ * Register workflow components for the SDK instance.
3393
+ *
3394
+ * Workflows are pre-built multi-step UI flows the main agent can pick when
3395
+ * the user's prompt matches a workflow's `whenToUse` trigger. Picking a
3396
+ * workflow short-circuits analysis text + dashboard component generation —
3397
+ * the workflow component is returned directly, with the LLM-extracted props.
3398
+ */
3399
+ setWorkflows(workflows: WorkflowDescriptor[]): void;
3400
+ /**
3401
+ * Get the registered workflow components.
3402
+ */
3403
+ getWorkflows(): WorkflowDescriptor[];
3404
+ /**
3405
+ * Apply model strategy to all LLM provider singletons
3406
+ * @param strategy - 'best', 'fast', or 'balanced'
3407
+ */
3408
+ private applyModelStrategy;
3409
+ /**
3410
+ * Set model strategy at runtime
3411
+ * @param strategy - 'best', 'fast', or 'balanced'
3412
+ */
3413
+ setModelStrategy(strategy: ModelStrategy): void;
3414
+ /**
3415
+ * Get current model strategy
3416
+ */
3417
+ getModelStrategy(): ModelStrategy;
3418
+ /**
3419
+ * Apply conversation similarity threshold to all LLM provider singletons
3420
+ * @param threshold - Value between 0 and 1 (e.g., 0.8 = 80% similarity required)
3421
+ */
3422
+ private applyConversationSimilarityThreshold;
3423
+ /**
3424
+ * Set conversation similarity threshold at runtime
3425
+ * @param threshold - Value between 0 and 1 (e.g., 0.8 = 80% similarity required)
3426
+ */
3427
+ setConversationSimilarityThreshold(threshold: number): void;
3428
+ /**
3429
+ * Get current conversation similarity threshold
3430
+ */
3431
+ getConversationSimilarityThreshold(): number;
1381
3432
  }
1382
3433
 
1383
- export { type Action, CONTEXT_CONFIG, type CapturedLog, CleanupService, type CollectionHandler, type CollectionOperation, type IncomingMessage, LLM, type LogLevel, type Message, SDK_VERSION, STORAGE_CONFIG, SuperatomSDK, type SuperatomSDKConfig, Thread, ThreadManager, UIBlock, UILogCollector, type User, UserManager, type UsersData, logger };
3434
+ export { type Action, type AgentConfig, type AgentResponse, BM25L, type BM25LOptions, type BaseLLMConfig, CONTEXT_CONFIG, type CapturedLog, CleanupService, type CollectionHandler, type CollectionOperation, type DBUIBlock, DEFAULT_AGENT_CONFIG, type DatabaseType, type HybridSearchOptions, type IncomingMessage, type KbNodesQueryFilters, type KbNodesRequestPayload, LLM, type LLMUsageEntry, type LogLevel, MainAgent, type Message, type ModelStrategy, type OutputField, type RerankedResult, STORAGE_CONFIG, type SelectedWorkflow, SuperatomSDK, type SuperatomSDKConfig, type TaskType, Thread, ThreadManager, type Tool$1 as Tool, type ToolOutputSchema, UIBlock, UILogCollector, type User, UserManager, type UsersData, type WorkflowDescriptor, anthropicLLM, dashboardConversationHistory, geminiLLM, groqLLM, hybridRerank, llmUsageLogger, logger, openaiLLM, queryCache, rerankChromaResults, rerankConversationResults, userPromptErrorLogger };