@kat-ai/sdk 0.1.0

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.
@@ -0,0 +1,2829 @@
1
+ import { S as Slots, T as TraceStep, b as TraceEvent, A as AgentContext, I as Intent, C as ConversationMessage, E as ExecutionTrace, M as MultiChoiceOption, c as ChatResponse, d as ChatCitation, P as Plan, e as StepResult, D as DisambiguationResult, f as PlanStepKBCall, K as KBRetrieval, g as PlanStepSynthesis } from './core-pLiFHTBe.mjs';
2
+ export { y as ChatOutcome, x as ChatRequest, q as DisambiguationAction, r as DisambiguationDetails, u as KBRelevanceInfo, t as LLMExtractionFacts, a as LogEntry, L as LogLevel, p as PendingQuestion, w as PlanModification, n as PlanStep, m as PlanStepType, v as PlanningReasoning, o as TraceStepType, l as clearSessionLogs, h as createSessionLogger, j as emitTraceStep, k as getSessionLogs, s as subscribeToLogs, i as subscribeToTraceSteps } from './core-pLiFHTBe.mjs';
3
+ import { z } from 'zod';
4
+
5
+ /**
6
+ * Configuration Types
7
+ *
8
+ * Zod schemas and TypeScript types for agent configuration.
9
+ */
10
+
11
+ /** Slot definition schema */
12
+ declare const SlotDefinitionSchema: z.ZodObject<{
13
+ name: z.ZodString;
14
+ type: z.ZodEnum<["string", "boolean", "enum"]>;
15
+ required: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
16
+ description: z.ZodString;
17
+ prompt: z.ZodString;
18
+ examples: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
19
+ normalization: z.ZodOptional<z.ZodEnum<["lower_snake", "lowercase", "uppercase", "none"]>>;
20
+ enumValues: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
21
+ format: z.ZodOptional<z.ZodObject<{
22
+ true: z.ZodString;
23
+ false: z.ZodString;
24
+ }, "strip", z.ZodTypeAny, {
25
+ true: string;
26
+ false: string;
27
+ }, {
28
+ true: string;
29
+ false: string;
30
+ }>>;
31
+ }, "strip", z.ZodTypeAny, {
32
+ type: "string" | "boolean" | "enum";
33
+ name: string;
34
+ required: boolean;
35
+ description: string;
36
+ prompt: string;
37
+ examples?: string[] | undefined;
38
+ normalization?: "lower_snake" | "lowercase" | "uppercase" | "none" | undefined;
39
+ enumValues?: string[] | undefined;
40
+ format?: {
41
+ true: string;
42
+ false: string;
43
+ } | undefined;
44
+ }, {
45
+ type: "string" | "boolean" | "enum";
46
+ name: string;
47
+ description: string;
48
+ prompt: string;
49
+ required?: boolean | undefined;
50
+ examples?: string[] | undefined;
51
+ normalization?: "lower_snake" | "lowercase" | "uppercase" | "none" | undefined;
52
+ enumValues?: string[] | undefined;
53
+ format?: {
54
+ true: string;
55
+ false: string;
56
+ } | undefined;
57
+ }>;
58
+ type SlotDefinition$1 = z.infer<typeof SlotDefinitionSchema>;
59
+ /** LLM configuration schema */
60
+ declare const LLMConfigSchema: z.ZodObject<{
61
+ provider: z.ZodEnum<["openai", "anthropic"]>;
62
+ models: z.ZodObject<{
63
+ planning: z.ZodString;
64
+ extraction: z.ZodString;
65
+ synthesis: z.ZodString;
66
+ questionGeneration: z.ZodOptional<z.ZodString>;
67
+ }, "strip", z.ZodTypeAny, {
68
+ planning: string;
69
+ extraction: string;
70
+ synthesis: string;
71
+ questionGeneration?: string | undefined;
72
+ }, {
73
+ planning: string;
74
+ extraction: string;
75
+ synthesis: string;
76
+ questionGeneration?: string | undefined;
77
+ }>;
78
+ }, "strip", z.ZodTypeAny, {
79
+ provider: "openai" | "anthropic";
80
+ models: {
81
+ planning: string;
82
+ extraction: string;
83
+ synthesis: string;
84
+ questionGeneration?: string | undefined;
85
+ };
86
+ }, {
87
+ provider: "openai" | "anthropic";
88
+ models: {
89
+ planning: string;
90
+ extraction: string;
91
+ synthesis: string;
92
+ questionGeneration?: string | undefined;
93
+ };
94
+ }>;
95
+ type LLMConfig = z.infer<typeof LLMConfigSchema>;
96
+ /** Prompts configuration schema */
97
+ declare const PromptsConfigSchema: z.ZodObject<{
98
+ plannerSystemRole: z.ZodString;
99
+ synthesisSystemRole: z.ZodString;
100
+ planningInstructions: z.ZodOptional<z.ZodString>;
101
+ synthesisInstructions: z.ZodOptional<z.ZodString>;
102
+ extractionInstructions: z.ZodOptional<z.ZodString>;
103
+ }, "strip", z.ZodTypeAny, {
104
+ plannerSystemRole: string;
105
+ synthesisSystemRole: string;
106
+ planningInstructions?: string | undefined;
107
+ synthesisInstructions?: string | undefined;
108
+ extractionInstructions?: string | undefined;
109
+ }, {
110
+ plannerSystemRole: string;
111
+ synthesisSystemRole: string;
112
+ planningInstructions?: string | undefined;
113
+ synthesisInstructions?: string | undefined;
114
+ extractionInstructions?: string | undefined;
115
+ }>;
116
+ type PromptsConfig = z.infer<typeof PromptsConfigSchema>;
117
+ /** Goal examples for extraction */
118
+ declare const GoalExamplesSchema: z.ZodArray<z.ZodObject<{
119
+ name: z.ZodString;
120
+ description: z.ZodOptional<z.ZodString>;
121
+ }, "strip", z.ZodTypeAny, {
122
+ name: string;
123
+ description?: string | undefined;
124
+ }, {
125
+ name: string;
126
+ description?: string | undefined;
127
+ }>, "many">;
128
+ type GoalExamples = z.infer<typeof GoalExamplesSchema>;
129
+ /** Messages configuration schema */
130
+ declare const MessagesConfigSchema: z.ZodObject<{
131
+ allKBsFailed: z.ZodString;
132
+ noRelevantInfo: z.ZodString;
133
+ synthesisError: z.ZodString;
134
+ }, "strip", z.ZodTypeAny, {
135
+ allKBsFailed: string;
136
+ noRelevantInfo: string;
137
+ synthesisError: string;
138
+ }, {
139
+ allKBsFailed: string;
140
+ noRelevantInfo: string;
141
+ synthesisError: string;
142
+ }>;
143
+ type MessagesConfig = z.infer<typeof MessagesConfigSchema>;
144
+ /** Citations configuration schema */
145
+ declare const CitationsConfigSchema: z.ZodObject<{
146
+ format: z.ZodEnum<["brackets", "footnotes", "superscript"]>;
147
+ labelPrefix: z.ZodString;
148
+ }, "strip", z.ZodTypeAny, {
149
+ format: "brackets" | "footnotes" | "superscript";
150
+ labelPrefix: string;
151
+ }, {
152
+ format: "brackets" | "footnotes" | "superscript";
153
+ labelPrefix: string;
154
+ }>;
155
+ type CitationsConfig = z.infer<typeof CitationsConfigSchema>;
156
+ /** Behavior configuration schema */
157
+ declare const BehaviorConfigSchema: z.ZodObject<{
158
+ maxDisambiguationAttempts: z.ZodDefault<z.ZodNumber>;
159
+ fallbackStrategy: z.ZodDefault<z.ZodEnum<["use_first", "use_all", "fail"]>>;
160
+ enablePlanModification: z.ZodDefault<z.ZodBoolean>;
161
+ }, "strip", z.ZodTypeAny, {
162
+ maxDisambiguationAttempts: number;
163
+ fallbackStrategy: "use_first" | "use_all" | "fail";
164
+ enablePlanModification: boolean;
165
+ }, {
166
+ maxDisambiguationAttempts?: number | undefined;
167
+ fallbackStrategy?: "use_first" | "use_all" | "fail" | undefined;
168
+ enablePlanModification?: boolean | undefined;
169
+ }>;
170
+ type BehaviorConfig = z.infer<typeof BehaviorConfigSchema>;
171
+ /** KB Provider configuration schema */
172
+ declare const KBProviderConfigSchema: z.ZodObject<{
173
+ type: z.ZodDefault<z.ZodEnum<["pinecone", "elasticsearch", "custom"]>>;
174
+ topK: z.ZodDefault<z.ZodNumber>;
175
+ normalizeNames: z.ZodDefault<z.ZodBoolean>;
176
+ }, "strip", z.ZodTypeAny, {
177
+ type: "custom" | "pinecone" | "elasticsearch";
178
+ topK: number;
179
+ normalizeNames: boolean;
180
+ }, {
181
+ type?: "custom" | "pinecone" | "elasticsearch" | undefined;
182
+ topK?: number | undefined;
183
+ normalizeNames?: boolean | undefined;
184
+ }>;
185
+ type KBProviderConfig = z.infer<typeof KBProviderConfigSchema>;
186
+ /** Session/logging configuration schema */
187
+ declare const SessionConfigSchema: z.ZodObject<{
188
+ maxLogsPerSession: z.ZodDefault<z.ZodNumber>;
189
+ maxTraceStepsPerSession: z.ZodDefault<z.ZodNumber>;
190
+ }, "strip", z.ZodTypeAny, {
191
+ maxLogsPerSession: number;
192
+ maxTraceStepsPerSession: number;
193
+ }, {
194
+ maxLogsPerSession?: number | undefined;
195
+ maxTraceStepsPerSession?: number | undefined;
196
+ }>;
197
+ type SessionConfig = z.infer<typeof SessionConfigSchema>;
198
+ /** Providers configuration schema (aggregates all provider settings) */
199
+ declare const ProvidersConfigSchema: z.ZodObject<{
200
+ kb: z.ZodDefault<z.ZodOptional<z.ZodObject<{
201
+ type: z.ZodDefault<z.ZodEnum<["pinecone", "elasticsearch", "custom"]>>;
202
+ topK: z.ZodDefault<z.ZodNumber>;
203
+ normalizeNames: z.ZodDefault<z.ZodBoolean>;
204
+ }, "strip", z.ZodTypeAny, {
205
+ type: "custom" | "pinecone" | "elasticsearch";
206
+ topK: number;
207
+ normalizeNames: boolean;
208
+ }, {
209
+ type?: "custom" | "pinecone" | "elasticsearch" | undefined;
210
+ topK?: number | undefined;
211
+ normalizeNames?: boolean | undefined;
212
+ }>>>;
213
+ session: z.ZodDefault<z.ZodOptional<z.ZodObject<{
214
+ maxLogsPerSession: z.ZodDefault<z.ZodNumber>;
215
+ maxTraceStepsPerSession: z.ZodDefault<z.ZodNumber>;
216
+ }, "strip", z.ZodTypeAny, {
217
+ maxLogsPerSession: number;
218
+ maxTraceStepsPerSession: number;
219
+ }, {
220
+ maxLogsPerSession?: number | undefined;
221
+ maxTraceStepsPerSession?: number | undefined;
222
+ }>>>;
223
+ }, "strip", z.ZodTypeAny, {
224
+ kb: {
225
+ type: "custom" | "pinecone" | "elasticsearch";
226
+ topK: number;
227
+ normalizeNames: boolean;
228
+ };
229
+ session: {
230
+ maxLogsPerSession: number;
231
+ maxTraceStepsPerSession: number;
232
+ };
233
+ }, {
234
+ kb?: {
235
+ type?: "custom" | "pinecone" | "elasticsearch" | undefined;
236
+ topK?: number | undefined;
237
+ normalizeNames?: boolean | undefined;
238
+ } | undefined;
239
+ session?: {
240
+ maxLogsPerSession?: number | undefined;
241
+ maxTraceStepsPerSession?: number | undefined;
242
+ } | undefined;
243
+ }>;
244
+ type ProvidersConfig = z.infer<typeof ProvidersConfigSchema>;
245
+ /** Main agent configuration schema */
246
+ declare const AgentConfigSchema: z.ZodObject<{
247
+ name: z.ZodString;
248
+ domain: z.ZodString;
249
+ llm: z.ZodObject<{
250
+ provider: z.ZodEnum<["openai", "anthropic"]>;
251
+ models: z.ZodObject<{
252
+ planning: z.ZodString;
253
+ extraction: z.ZodString;
254
+ synthesis: z.ZodString;
255
+ questionGeneration: z.ZodOptional<z.ZodString>;
256
+ }, "strip", z.ZodTypeAny, {
257
+ planning: string;
258
+ extraction: string;
259
+ synthesis: string;
260
+ questionGeneration?: string | undefined;
261
+ }, {
262
+ planning: string;
263
+ extraction: string;
264
+ synthesis: string;
265
+ questionGeneration?: string | undefined;
266
+ }>;
267
+ }, "strip", z.ZodTypeAny, {
268
+ provider: "openai" | "anthropic";
269
+ models: {
270
+ planning: string;
271
+ extraction: string;
272
+ synthesis: string;
273
+ questionGeneration?: string | undefined;
274
+ };
275
+ }, {
276
+ provider: "openai" | "anthropic";
277
+ models: {
278
+ planning: string;
279
+ extraction: string;
280
+ synthesis: string;
281
+ questionGeneration?: string | undefined;
282
+ };
283
+ }>;
284
+ prompts: z.ZodObject<{
285
+ plannerSystemRole: z.ZodString;
286
+ synthesisSystemRole: z.ZodString;
287
+ planningInstructions: z.ZodOptional<z.ZodString>;
288
+ synthesisInstructions: z.ZodOptional<z.ZodString>;
289
+ extractionInstructions: z.ZodOptional<z.ZodString>;
290
+ }, "strip", z.ZodTypeAny, {
291
+ plannerSystemRole: string;
292
+ synthesisSystemRole: string;
293
+ planningInstructions?: string | undefined;
294
+ synthesisInstructions?: string | undefined;
295
+ extractionInstructions?: string | undefined;
296
+ }, {
297
+ plannerSystemRole: string;
298
+ synthesisSystemRole: string;
299
+ planningInstructions?: string | undefined;
300
+ synthesisInstructions?: string | undefined;
301
+ extractionInstructions?: string | undefined;
302
+ }>;
303
+ messages: z.ZodObject<{
304
+ allKBsFailed: z.ZodString;
305
+ noRelevantInfo: z.ZodString;
306
+ synthesisError: z.ZodString;
307
+ }, "strip", z.ZodTypeAny, {
308
+ allKBsFailed: string;
309
+ noRelevantInfo: string;
310
+ synthesisError: string;
311
+ }, {
312
+ allKBsFailed: string;
313
+ noRelevantInfo: string;
314
+ synthesisError: string;
315
+ }>;
316
+ slots: z.ZodArray<z.ZodObject<{
317
+ name: z.ZodString;
318
+ type: z.ZodEnum<["string", "boolean", "enum"]>;
319
+ required: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
320
+ description: z.ZodString;
321
+ prompt: z.ZodString;
322
+ examples: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
323
+ normalization: z.ZodOptional<z.ZodEnum<["lower_snake", "lowercase", "uppercase", "none"]>>;
324
+ enumValues: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
325
+ format: z.ZodOptional<z.ZodObject<{
326
+ true: z.ZodString;
327
+ false: z.ZodString;
328
+ }, "strip", z.ZodTypeAny, {
329
+ true: string;
330
+ false: string;
331
+ }, {
332
+ true: string;
333
+ false: string;
334
+ }>>;
335
+ }, "strip", z.ZodTypeAny, {
336
+ type: "string" | "boolean" | "enum";
337
+ name: string;
338
+ required: boolean;
339
+ description: string;
340
+ prompt: string;
341
+ examples?: string[] | undefined;
342
+ normalization?: "lower_snake" | "lowercase" | "uppercase" | "none" | undefined;
343
+ enumValues?: string[] | undefined;
344
+ format?: {
345
+ true: string;
346
+ false: string;
347
+ } | undefined;
348
+ }, {
349
+ type: "string" | "boolean" | "enum";
350
+ name: string;
351
+ description: string;
352
+ prompt: string;
353
+ required?: boolean | undefined;
354
+ examples?: string[] | undefined;
355
+ normalization?: "lower_snake" | "lowercase" | "uppercase" | "none" | undefined;
356
+ enumValues?: string[] | undefined;
357
+ format?: {
358
+ true: string;
359
+ false: string;
360
+ } | undefined;
361
+ }>, "many">;
362
+ slotPriority: z.ZodArray<z.ZodString, "many">;
363
+ goals: z.ZodOptional<z.ZodArray<z.ZodObject<{
364
+ name: z.ZodString;
365
+ description: z.ZodOptional<z.ZodString>;
366
+ }, "strip", z.ZodTypeAny, {
367
+ name: string;
368
+ description?: string | undefined;
369
+ }, {
370
+ name: string;
371
+ description?: string | undefined;
372
+ }>, "many">>;
373
+ defaultKbId: z.ZodString;
374
+ citations: z.ZodObject<{
375
+ format: z.ZodEnum<["brackets", "footnotes", "superscript"]>;
376
+ labelPrefix: z.ZodString;
377
+ }, "strip", z.ZodTypeAny, {
378
+ format: "brackets" | "footnotes" | "superscript";
379
+ labelPrefix: string;
380
+ }, {
381
+ format: "brackets" | "footnotes" | "superscript";
382
+ labelPrefix: string;
383
+ }>;
384
+ behavior: z.ZodObject<{
385
+ maxDisambiguationAttempts: z.ZodDefault<z.ZodNumber>;
386
+ fallbackStrategy: z.ZodDefault<z.ZodEnum<["use_first", "use_all", "fail"]>>;
387
+ enablePlanModification: z.ZodDefault<z.ZodBoolean>;
388
+ }, "strip", z.ZodTypeAny, {
389
+ maxDisambiguationAttempts: number;
390
+ fallbackStrategy: "use_first" | "use_all" | "fail";
391
+ enablePlanModification: boolean;
392
+ }, {
393
+ maxDisambiguationAttempts?: number | undefined;
394
+ fallbackStrategy?: "use_first" | "use_all" | "fail" | undefined;
395
+ enablePlanModification?: boolean | undefined;
396
+ }>;
397
+ providers: z.ZodDefault<z.ZodOptional<z.ZodObject<{
398
+ kb: z.ZodDefault<z.ZodOptional<z.ZodObject<{
399
+ type: z.ZodDefault<z.ZodEnum<["pinecone", "elasticsearch", "custom"]>>;
400
+ topK: z.ZodDefault<z.ZodNumber>;
401
+ normalizeNames: z.ZodDefault<z.ZodBoolean>;
402
+ }, "strip", z.ZodTypeAny, {
403
+ type: "custom" | "pinecone" | "elasticsearch";
404
+ topK: number;
405
+ normalizeNames: boolean;
406
+ }, {
407
+ type?: "custom" | "pinecone" | "elasticsearch" | undefined;
408
+ topK?: number | undefined;
409
+ normalizeNames?: boolean | undefined;
410
+ }>>>;
411
+ session: z.ZodDefault<z.ZodOptional<z.ZodObject<{
412
+ maxLogsPerSession: z.ZodDefault<z.ZodNumber>;
413
+ maxTraceStepsPerSession: z.ZodDefault<z.ZodNumber>;
414
+ }, "strip", z.ZodTypeAny, {
415
+ maxLogsPerSession: number;
416
+ maxTraceStepsPerSession: number;
417
+ }, {
418
+ maxLogsPerSession?: number | undefined;
419
+ maxTraceStepsPerSession?: number | undefined;
420
+ }>>>;
421
+ }, "strip", z.ZodTypeAny, {
422
+ kb: {
423
+ type: "custom" | "pinecone" | "elasticsearch";
424
+ topK: number;
425
+ normalizeNames: boolean;
426
+ };
427
+ session: {
428
+ maxLogsPerSession: number;
429
+ maxTraceStepsPerSession: number;
430
+ };
431
+ }, {
432
+ kb?: {
433
+ type?: "custom" | "pinecone" | "elasticsearch" | undefined;
434
+ topK?: number | undefined;
435
+ normalizeNames?: boolean | undefined;
436
+ } | undefined;
437
+ session?: {
438
+ maxLogsPerSession?: number | undefined;
439
+ maxTraceStepsPerSession?: number | undefined;
440
+ } | undefined;
441
+ }>>>;
442
+ slotMappings: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodRecord<z.ZodString, z.ZodString>>>;
443
+ }, "strip", z.ZodTypeAny, {
444
+ name: string;
445
+ domain: string;
446
+ llm: {
447
+ provider: "openai" | "anthropic";
448
+ models: {
449
+ planning: string;
450
+ extraction: string;
451
+ synthesis: string;
452
+ questionGeneration?: string | undefined;
453
+ };
454
+ };
455
+ prompts: {
456
+ plannerSystemRole: string;
457
+ synthesisSystemRole: string;
458
+ planningInstructions?: string | undefined;
459
+ synthesisInstructions?: string | undefined;
460
+ extractionInstructions?: string | undefined;
461
+ };
462
+ messages: {
463
+ allKBsFailed: string;
464
+ noRelevantInfo: string;
465
+ synthesisError: string;
466
+ };
467
+ slots: {
468
+ type: "string" | "boolean" | "enum";
469
+ name: string;
470
+ required: boolean;
471
+ description: string;
472
+ prompt: string;
473
+ examples?: string[] | undefined;
474
+ normalization?: "lower_snake" | "lowercase" | "uppercase" | "none" | undefined;
475
+ enumValues?: string[] | undefined;
476
+ format?: {
477
+ true: string;
478
+ false: string;
479
+ } | undefined;
480
+ }[];
481
+ slotPriority: string[];
482
+ defaultKbId: string;
483
+ citations: {
484
+ format: "brackets" | "footnotes" | "superscript";
485
+ labelPrefix: string;
486
+ };
487
+ behavior: {
488
+ maxDisambiguationAttempts: number;
489
+ fallbackStrategy: "use_first" | "use_all" | "fail";
490
+ enablePlanModification: boolean;
491
+ };
492
+ providers: {
493
+ kb: {
494
+ type: "custom" | "pinecone" | "elasticsearch";
495
+ topK: number;
496
+ normalizeNames: boolean;
497
+ };
498
+ session: {
499
+ maxLogsPerSession: number;
500
+ maxTraceStepsPerSession: number;
501
+ };
502
+ };
503
+ goals?: {
504
+ name: string;
505
+ description?: string | undefined;
506
+ }[] | undefined;
507
+ slotMappings?: Record<string, Record<string, string>> | undefined;
508
+ }, {
509
+ name: string;
510
+ domain: string;
511
+ llm: {
512
+ provider: "openai" | "anthropic";
513
+ models: {
514
+ planning: string;
515
+ extraction: string;
516
+ synthesis: string;
517
+ questionGeneration?: string | undefined;
518
+ };
519
+ };
520
+ prompts: {
521
+ plannerSystemRole: string;
522
+ synthesisSystemRole: string;
523
+ planningInstructions?: string | undefined;
524
+ synthesisInstructions?: string | undefined;
525
+ extractionInstructions?: string | undefined;
526
+ };
527
+ messages: {
528
+ allKBsFailed: string;
529
+ noRelevantInfo: string;
530
+ synthesisError: string;
531
+ };
532
+ slots: {
533
+ type: "string" | "boolean" | "enum";
534
+ name: string;
535
+ description: string;
536
+ prompt: string;
537
+ required?: boolean | undefined;
538
+ examples?: string[] | undefined;
539
+ normalization?: "lower_snake" | "lowercase" | "uppercase" | "none" | undefined;
540
+ enumValues?: string[] | undefined;
541
+ format?: {
542
+ true: string;
543
+ false: string;
544
+ } | undefined;
545
+ }[];
546
+ slotPriority: string[];
547
+ defaultKbId: string;
548
+ citations: {
549
+ format: "brackets" | "footnotes" | "superscript";
550
+ labelPrefix: string;
551
+ };
552
+ behavior: {
553
+ maxDisambiguationAttempts?: number | undefined;
554
+ fallbackStrategy?: "use_first" | "use_all" | "fail" | undefined;
555
+ enablePlanModification?: boolean | undefined;
556
+ };
557
+ goals?: {
558
+ name: string;
559
+ description?: string | undefined;
560
+ }[] | undefined;
561
+ providers?: {
562
+ kb?: {
563
+ type?: "custom" | "pinecone" | "elasticsearch" | undefined;
564
+ topK?: number | undefined;
565
+ normalizeNames?: boolean | undefined;
566
+ } | undefined;
567
+ session?: {
568
+ maxLogsPerSession?: number | undefined;
569
+ maxTraceStepsPerSession?: number | undefined;
570
+ } | undefined;
571
+ } | undefined;
572
+ slotMappings?: Record<string, Record<string, string>> | undefined;
573
+ }>;
574
+ type AgentConfig = z.infer<typeof AgentConfigSchema>;
575
+ /** Helper type for dynamic Slots based on config */
576
+ type DynamicSlots = {
577
+ [key: string]: string | boolean | undefined;
578
+ };
579
+
580
+ /**
581
+ * Composition and Manifest Types
582
+ *
583
+ * Type definitions for knowledge base manifests and composition configuration.
584
+ */
585
+
586
+ type SlotValuePrimitive = 'string' | 'boolean' | 'enum';
587
+ type SlotNormalization = 'lower_snake' | 'lowercase' | 'uppercase' | 'none';
588
+ /**
589
+ * Evidence supporting a manifest component decision.
590
+ * Contains the LLM's reasoning plus citations to source material.
591
+ */
592
+ interface ComponentEvidence$1 {
593
+ /** LLM explanation of why this component exists */
594
+ reasoning: string;
595
+ /** Actual KB content (document snippets) that informed this decision */
596
+ documentSnippets: Array<{
597
+ content: string;
598
+ sourceFile: string;
599
+ score: number;
600
+ }>;
601
+ /** Q&A exchanges that surfaced the evidence during introspection */
602
+ qaExchanges: Array<{
603
+ questionNumber: number;
604
+ question: string;
605
+ answerExcerpt: string;
606
+ }>;
607
+ }
608
+ /**
609
+ * Describes how the extractor should handle vague/ambiguous input for this slot.
610
+ * - 'identifier': Requires a specific identifier (model number, serial, product name). Device types alone are not sufficient.
611
+ * - 'categorization': Requires explicit indication of a category. Vague descriptions should leave slot empty for clarification.
612
+ * - 'freeform': Accept any relevant text the user provides.
613
+ */
614
+ type SlotExtractionBehavior = 'identifier' | 'categorization' | 'freeform';
615
+ interface SlotDefinition {
616
+ name: string;
617
+ type: SlotValuePrimitive;
618
+ description: string;
619
+ /** Whether this slot is required to answer queries */
620
+ required?: boolean;
621
+ prompt?: string;
622
+ examples?: string[];
623
+ normalization?: SlotNormalization;
624
+ enumValues?: string[];
625
+ /** Evidence supporting why this slot was identified */
626
+ evidence?: ComponentEvidence$1;
627
+ /** How the extractor should handle vague/ambiguous input for this slot */
628
+ extractionBehavior?: SlotExtractionBehavior;
629
+ /**
630
+ * LLM-generated guidance for the extractor explaining how to handle this slot.
631
+ * This is domain-specific advice about what constitutes a valid value vs. ambiguous input.
632
+ */
633
+ extractionGuidance?: string;
634
+ }
635
+ interface KBInputField {
636
+ slot: string;
637
+ required?: boolean;
638
+ description?: string;
639
+ /** Evidence supporting why this input was identified */
640
+ evidence?: ComponentEvidence$1;
641
+ }
642
+ interface QueryTemplateSection {
643
+ text: string;
644
+ requires?: string[];
645
+ optional?: boolean;
646
+ fallback?: string;
647
+ }
648
+ interface KBQueryTemplate {
649
+ preamble?: string;
650
+ sections?: QueryTemplateSection[];
651
+ closing?: string[];
652
+ }
653
+ interface KBRelevanceHints {
654
+ goalKeywords?: string[];
655
+ slotTriggers?: string[];
656
+ domains?: string[];
657
+ }
658
+ /**
659
+ * Defines the scope boundaries for a knowledge base.
660
+ * Used by the LLM to determine if a query is within scope.
661
+ */
662
+ interface SlotDependency {
663
+ slot: string;
664
+ dependsOn: string[];
665
+ condition?: string;
666
+ required?: boolean;
667
+ }
668
+ interface SlotValidation {
669
+ format?: string;
670
+ pattern?: string;
671
+ minLength?: number;
672
+ maxLength?: number;
673
+ enum?: string[];
674
+ examples?: string[];
675
+ errorMessage?: string;
676
+ }
677
+ interface KBTerminology {
678
+ synonyms?: Record<string, string[]>;
679
+ aliases?: Record<string, string[]>;
680
+ }
681
+ interface KBResponsePatterns {
682
+ alwaysInclude?: string[];
683
+ safetyWarnings?: string[];
684
+ disclaimers?: string[];
685
+ format?: string;
686
+ }
687
+ interface KBConversationPatterns {
688
+ commonFollowUps?: string[];
689
+ clarificationNeeded?: string[];
690
+ multiTurnExamples?: string[];
691
+ }
692
+ interface KBEdgeCases {
693
+ scenarios?: string[];
694
+ handling?: string;
695
+ additionalContext?: string[];
696
+ }
697
+ interface KBQualityIndicators {
698
+ highConfidenceTriggers?: string[];
699
+ lowConfidenceTriggers?: string[];
700
+ accuracyFactors?: string[];
701
+ }
702
+ interface KBScopeDefinition {
703
+ /** Human-readable description of what this KB helps with */
704
+ description: string;
705
+ /** Example queries that ARE within scope */
706
+ inScopeExamples?: string[];
707
+ /** Example queries that are OUT of scope (even if they mention valid products) */
708
+ outOfScopeExamples?: string[];
709
+ /** Intent categories that this KB explicitly cannot help with */
710
+ excludedIntents?: string[];
711
+ /** Edge cases that are in scope */
712
+ edgeCaseExamples?: string[];
713
+ /** Cases that might be in scope (ambiguous) */
714
+ ambiguousCases?: string[];
715
+ /** Evidence supporting the scope boundaries */
716
+ evidence?: ComponentEvidence$1;
717
+ }
718
+ /**
719
+ * A capability with optional evidence.
720
+ * Capabilities are typically strings, but can include evidence when introspected.
721
+ */
722
+ interface KBCapability {
723
+ text: string;
724
+ /** Evidence supporting why this capability was identified */
725
+ evidence?: ComponentEvidence$1;
726
+ }
727
+ interface KBGoal {
728
+ name: string;
729
+ description: string;
730
+ /** Evidence supporting why this goal was identified */
731
+ evidence?: ComponentEvidence$1;
732
+ }
733
+ interface KBManifest {
734
+ id: string;
735
+ assistantName: string;
736
+ domain: string;
737
+ description: string;
738
+ /** Capabilities can be simple strings OR objects with evidence */
739
+ capabilities: (string | KBCapability)[];
740
+ outputs: string[];
741
+ /**
742
+ * Declarative KB inputs (used when {@link slots} is empty or omitted in JSON manifests).
743
+ * Converted to {@link SlotDefinition}s at catalog build time.
744
+ */
745
+ inputs?: KBInputField[];
746
+ slots: SlotDefinition[];
747
+ relevance?: KBRelevanceHints;
748
+ queryTemplate?: KBQueryTemplate;
749
+ scope?: KBScopeDefinition;
750
+ goals?: KBGoal[];
751
+ slotDependencies?: SlotDependency[];
752
+ slotPriority?: string[];
753
+ slotValidations?: Record<string, SlotValidation>;
754
+ terminology?: KBTerminology;
755
+ responsePatterns?: KBResponsePatterns;
756
+ conversationPatterns?: KBConversationPatterns;
757
+ edgeCases?: KBEdgeCases;
758
+ qualityIndicators?: KBQualityIndicators;
759
+ /** Evidence for the overall description */
760
+ descriptionEvidence?: ComponentEvidence$1;
761
+ }
762
+ interface CompositionRegistry {
763
+ manifests: KBManifest[];
764
+ slotMap: Map<string, SlotDefinition>;
765
+ }
766
+ type SlotSchemaShape = z.ZodRawShape;
767
+
768
+ /**
769
+ * KB Metadata Types
770
+ *
771
+ * Type definitions for knowledge base metadata used throughout the framework.
772
+ */
773
+
774
+ interface KBMeta {
775
+ id: string;
776
+ assistantName: string;
777
+ domain: string;
778
+ description: string;
779
+ inputEntities: string[];
780
+ outputEntities: string[];
781
+ capabilities: string[];
782
+ inputPrompts: Record<string, string>;
783
+ relevance?: KBRelevanceHints;
784
+ queryTemplate?: KBQueryTemplate;
785
+ inputRequirements: KBInputField[];
786
+ scope?: KBScopeDefinition;
787
+ slots?: SlotDefinition[];
788
+ }
789
+
790
+ /**
791
+ * Options for LLM generation
792
+ */
793
+ interface LLMOptions {
794
+ /** Model identifier (e.g., 'gpt-4o-mini', 'gpt-4o') */
795
+ model: string;
796
+ /** Temperature for generation (0-1) */
797
+ temperature?: number;
798
+ /** Maximum tokens to generate */
799
+ maxTokens?: number;
800
+ }
801
+ /**
802
+ * Result from text generation
803
+ */
804
+ interface LLMTextResult {
805
+ text: string;
806
+ usage?: {
807
+ promptTokens: number;
808
+ completionTokens: number;
809
+ totalTokens: number;
810
+ };
811
+ }
812
+ /**
813
+ * Result from structured object generation
814
+ */
815
+ interface LLMObjectResult<T> {
816
+ object: T;
817
+ usage?: {
818
+ promptTokens: number;
819
+ completionTokens: number;
820
+ totalTokens: number;
821
+ };
822
+ }
823
+ /**
824
+ * Result from streaming text generation
825
+ */
826
+ interface LLMStreamResult {
827
+ /** The readable stream of text chunks */
828
+ stream: ReadableStream<Uint8Array>;
829
+ /** Promise that resolves to the full text when streaming completes */
830
+ textPromise: Promise<string>;
831
+ /** Promise that resolves to usage information when streaming completes */
832
+ usagePromise?: Promise<{
833
+ promptTokens: number;
834
+ completionTokens: number;
835
+ totalTokens: number;
836
+ }>;
837
+ }
838
+ /**
839
+ * Interface for LLM providers (OpenAI, Anthropic, etc.)
840
+ *
841
+ * Implementations must provide methods for generating text, structured objects, and streaming.
842
+ */
843
+ interface LLMProvider {
844
+ /** Provider name for logging/debugging */
845
+ readonly name: string;
846
+ /**
847
+ * Generate text from a prompt
848
+ *
849
+ * @param prompt - The prompt to send to the LLM
850
+ * @param options - Generation options (model, temperature, etc.)
851
+ * @returns Generated text and usage information
852
+ */
853
+ generateText(prompt: string, options: LLMOptions): Promise<LLMTextResult>;
854
+ /**
855
+ * Generate a structured object from a prompt using a Zod schema
856
+ *
857
+ * @param schema - Zod schema defining the expected output structure
858
+ * @param prompt - The prompt to send to the LLM
859
+ * @param options - Generation options (model, temperature, etc.)
860
+ * @returns Parsed object matching the schema and usage information
861
+ */
862
+ generateObject<T>(schema: z.ZodSchema<T>, prompt: string, options: LLMOptions): Promise<LLMObjectResult<T>>;
863
+ /**
864
+ * Stream text generation from a prompt
865
+ *
866
+ * @param prompt - The prompt to send to the LLM
867
+ * @param options - Generation options (model, temperature, etc.)
868
+ * @returns Stream result with readable stream and promises for full text/usage
869
+ */
870
+ streamText(prompt: string, options: LLMOptions): Promise<LLMStreamResult>;
871
+ }
872
+ /**
873
+ * A context snippet retrieved from a knowledge base
874
+ */
875
+ interface KBSnippet {
876
+ /** Content of the snippet */
877
+ content: string;
878
+ /** Reference information for citations */
879
+ reference?: {
880
+ id?: string;
881
+ source?: string;
882
+ file?: {
883
+ name?: string;
884
+ signedUrl?: string;
885
+ signed_url?: string;
886
+ metadata?: Record<string, any>;
887
+ };
888
+ pages?: number[];
889
+ };
890
+ /** Label for citation (e.g., 'S1', 'S2') */
891
+ label?: string;
892
+ /** Short preview text */
893
+ preview?: string;
894
+ /** Human-friendly reference summary */
895
+ referenceSummary?: string;
896
+ /** MIME type of the source content */
897
+ contentType?: string;
898
+ /** Name of the source file */
899
+ fileName?: string;
900
+ }
901
+ /**
902
+ * Query options for knowledge base retrieval
903
+ */
904
+ interface KBQueryOptions {
905
+ /** Number of top results to retrieve */
906
+ topK?: number;
907
+ /** Optional filter criteria */
908
+ filter?: Record<string, any>;
909
+ }
910
+ /**
911
+ * Result from a knowledge base query
912
+ */
913
+ interface KBQueryResult {
914
+ /** Retrieved context snippets */
915
+ snippets: KBSnippet[];
916
+ /** Generated answer from the KB (if applicable) */
917
+ answer: string;
918
+ }
919
+ /**
920
+ * Metadata about a knowledge base
921
+ */
922
+ interface KBMetadata {
923
+ /** Unique identifier */
924
+ id: string;
925
+ /** Human-readable name */
926
+ name: string;
927
+ /** Description of the KB's purpose */
928
+ description: string;
929
+ /** What the KB can do */
930
+ capabilities: string[];
931
+ /** Required input entities/slots */
932
+ inputEntities: string[];
933
+ /** Entities the KB can provide */
934
+ outputEntities: string[];
935
+ }
936
+ /**
937
+ * Interface for knowledge base providers (Pinecone, Elasticsearch, etc.)
938
+ *
939
+ * Implementations handle querying specific KB backends and returning context snippets.
940
+ */
941
+ interface KBProvider {
942
+ /** Provider type for logging (e.g., 'pinecone') */
943
+ readonly type: string;
944
+ /**
945
+ * Query the knowledge base for relevant context
946
+ *
947
+ * @param kbId - Identifier of the specific KB to query
948
+ * @param query - The query string
949
+ * @param options - Query options (topK, filters)
950
+ * @returns Snippets and generated answer
951
+ */
952
+ query(kbId: string, query: string, options?: KBQueryOptions): Promise<KBQueryResult>;
953
+ /**
954
+ * Get metadata about a specific knowledge base
955
+ *
956
+ * @param kbId - Identifier of the KB
957
+ * @returns KB metadata or undefined if not found
958
+ */
959
+ getMetadata(kbId: string): KBMetadata | undefined;
960
+ /**
961
+ * List all available knowledge bases
962
+ *
963
+ * @returns Array of KB metadata
964
+ */
965
+ listKBs(): KBMetadata[];
966
+ }
967
+ /**
968
+ * Context for template rendering
969
+ */
970
+ interface TemplateContext {
971
+ /** Slot values to interpolate */
972
+ slots: Partial<Slots>;
973
+ /** User's original utterance */
974
+ userUtterance: string;
975
+ /** Additional context variables */
976
+ [key: string]: any;
977
+ }
978
+ /**
979
+ * Interface for template engines (Mustache, Handlebars, etc.)
980
+ *
981
+ * Implementations render query templates with slot values and context.
982
+ */
983
+ interface TemplateEngine {
984
+ /** Engine name for logging */
985
+ readonly name: string;
986
+ /**
987
+ * Render a template string with context
988
+ *
989
+ * @param template - Template string with placeholders
990
+ * @param context - Context containing slot values and other variables
991
+ * @returns Rendered string
992
+ */
993
+ render(template: string, context: TemplateContext): string;
994
+ }
995
+ /**
996
+ * Agent thinking/reasoning entry
997
+ */
998
+ interface AgentThinking {
999
+ phase: 'disambiguation' | 'planning' | 'execution' | 'synthesis';
1000
+ timestamp: string;
1001
+ reasoning: string;
1002
+ details?: any;
1003
+ }
1004
+ /**
1005
+ * Interface for collecting execution traces
1006
+ *
1007
+ * Implementations handle recording trace steps and agent thinking for observability.
1008
+ * This separates tracing concerns from business logic.
1009
+ */
1010
+ interface TraceCollector {
1011
+ /** Session ID for this trace */
1012
+ readonly sessionId: string;
1013
+ /**
1014
+ * Start a new trace step
1015
+ *
1016
+ * @param step - The trace step to record
1017
+ */
1018
+ addStep(step: TraceStep): void;
1019
+ /**
1020
+ * Update the most recent trace step
1021
+ *
1022
+ * @param updates - Partial updates to apply to the step
1023
+ */
1024
+ updateLastStep(updates: Partial<TraceStep>): void;
1025
+ /**
1026
+ * Record agent thinking/reasoning
1027
+ *
1028
+ * @param thinking - The thinking entry to record
1029
+ */
1030
+ recordThinking(thinking: AgentThinking): void;
1031
+ /**
1032
+ * Get all collected trace steps
1033
+ */
1034
+ getSteps(): TraceStep[];
1035
+ /**
1036
+ * Get all recorded thinking entries
1037
+ */
1038
+ getThinking(): AgentThinking[];
1039
+ /**
1040
+ * Emit a trace step for real-time streaming
1041
+ *
1042
+ * @param step - The trace step to emit
1043
+ */
1044
+ emit(step: TraceStep): void;
1045
+ /**
1046
+ * Add a step and emit it in one call
1047
+ *
1048
+ * @param step - The trace step to add and emit
1049
+ */
1050
+ addAndEmit(step: TraceStep): void;
1051
+ }
1052
+ /**
1053
+ * Interface for session-scoped logging
1054
+ */
1055
+ interface Logger {
1056
+ log(message: string, data?: any): void;
1057
+ info(message: string, data?: any): void;
1058
+ warn(message: string, data?: any): void;
1059
+ error(message: string, error?: Error, data?: any): void;
1060
+ debug(message: string, data?: any): void;
1061
+ }
1062
+ /**
1063
+ * Normalization rules for slot values
1064
+ */
1065
+ type NormalizationRule = 'lowercase' | 'uppercase' | 'lower_snake' | 'none';
1066
+ /**
1067
+ * Interface for normalizing slot values
1068
+ */
1069
+ interface SlotNormalizer {
1070
+ /**
1071
+ * Normalize a slot value according to its rules
1072
+ *
1073
+ * @param slotName - Name of the slot
1074
+ * @param value - Raw value to normalize
1075
+ * @returns Normalized value
1076
+ */
1077
+ normalize(slotName: string, value: string | boolean): string;
1078
+ /**
1079
+ * Format a slot value for display
1080
+ *
1081
+ * @param slotName - Name of the slot
1082
+ * @param value - Value to format
1083
+ * @returns Formatted string
1084
+ */
1085
+ format(slotName: string, value: string | boolean | undefined): string;
1086
+ }
1087
+ /**
1088
+ * Container for all framework providers and services
1089
+ *
1090
+ * This is passed to engines to provide access to all dependencies.
1091
+ */
1092
+ interface FrameworkContext {
1093
+ /** LLM provider for text/object generation */
1094
+ llm: LLMProvider;
1095
+ /** Knowledge base provider for context retrieval */
1096
+ kb: KBProvider;
1097
+ /** Template engine for query building */
1098
+ template: TemplateEngine;
1099
+ /** Logger for the current session */
1100
+ logger: Logger;
1101
+ /** Trace collector for observability */
1102
+ trace: TraceCollector;
1103
+ /** Slot normalizer */
1104
+ normalizer: SlotNormalizer;
1105
+ /** KB catalog (dynamic for SaaS, static fallback otherwise) */
1106
+ kbCatalog?: KBMeta[];
1107
+ }
1108
+
1109
+ /**
1110
+ * Trace Adapter System
1111
+ *
1112
+ * Pluggable adapters for emitting trace events to different outputs.
1113
+ * Implement the TraceAdapter interface to create custom trace UIs.
1114
+ */
1115
+
1116
+ /**
1117
+ * TraceAdapter Interface
1118
+ *
1119
+ * Implement this interface to create custom trace outputs (e.g., custom UI, logging, analytics).
1120
+ * Uses a single emit() method with a discriminated union for all event types.
1121
+ *
1122
+ * @example
1123
+ * ```typescript
1124
+ * class MyCustomTraceAdapter implements TraceAdapter {
1125
+ * emit(event: TraceEvent) {
1126
+ * switch (event.type) {
1127
+ * case 'step':
1128
+ * myUI.showStep(event.step);
1129
+ * break;
1130
+ * case 'trace_complete':
1131
+ * myAnalytics.track('agent_complete', event.trace);
1132
+ * break;
1133
+ * case 'follow_up':
1134
+ * myUI.showQuestions(event.questions);
1135
+ * break;
1136
+ * case 'session_id':
1137
+ * myLogger.setSessionId(event.sessionId);
1138
+ * break;
1139
+ * }
1140
+ * }
1141
+ * }
1142
+ * ```
1143
+ */
1144
+ interface TraceAdapter {
1145
+ /**
1146
+ * Emit a trace event.
1147
+ * Handle different event types by switching on event.type.
1148
+ */
1149
+ emit(event: TraceEvent): void | Promise<void>;
1150
+ }
1151
+ /**
1152
+ * Data stream writer interface for Vercel AI SDK compatibility
1153
+ */
1154
+ interface DataStreamWriter {
1155
+ writeData(data: unknown): void;
1156
+ }
1157
+ /**
1158
+ * SSE Trace Adapter
1159
+ *
1160
+ * Emits trace steps via Server-Sent Events using the existing logger infrastructure.
1161
+ * This is the default adapter for the standard (non-streaming) app.
1162
+ */
1163
+ declare class SSETraceAdapter implements TraceAdapter {
1164
+ private readonly sessionId;
1165
+ constructor(sessionId: string);
1166
+ emit(event: TraceEvent): void;
1167
+ }
1168
+ /**
1169
+ * Stream annotation for Vercel AI SDK data stream protocol
1170
+ */
1171
+ interface StreamAnnotation$1 {
1172
+ type: 'trace_step' | 'trace_complete' | 'follow_up' | 'session_id';
1173
+ step?: unknown;
1174
+ trace?: unknown;
1175
+ questions?: string[];
1176
+ sessionId?: string;
1177
+ }
1178
+ /**
1179
+ * Vercel Data Stream Trace Adapter
1180
+ *
1181
+ * Emits trace steps as data stream annotations for Vercel AI SDK streaming responses.
1182
+ * Use with createDataStreamResponse() from the 'ai' package.
1183
+ */
1184
+ declare class VercelDataStreamAdapter implements TraceAdapter {
1185
+ private dataStream;
1186
+ /**
1187
+ * Set the data stream to write to.
1188
+ * Must be called before any trace methods.
1189
+ */
1190
+ setDataStream(dataStream: DataStreamWriter): void;
1191
+ emit(event: TraceEvent): void;
1192
+ }
1193
+ /**
1194
+ * Buffering Vercel Data Stream Trace Adapter
1195
+ *
1196
+ * Buffers trace events for progressive streaming. Events are stored in a buffer
1197
+ * and can be flushed to a response stream on demand. This enables true progressive
1198
+ * streaming where trace annotations are interleaved with text chunks.
1199
+ *
1200
+ * @example
1201
+ * ```typescript
1202
+ * const adapter = new BufferingVercelDataStreamAdapter();
1203
+ * const streamingResult = await runAgentStreaming({ message, traceAdapter: adapter });
1204
+ * return toVercelAIStreamingResponse(streamingResult, adapter);
1205
+ * ```
1206
+ */
1207
+ declare class BufferingVercelDataStreamAdapter implements TraceAdapter {
1208
+ private buffer;
1209
+ private onFlush;
1210
+ /**
1211
+ * Set a callback to be invoked when annotations are ready to flush.
1212
+ * Used internally by toVercelAIStreamingResponse.
1213
+ */
1214
+ setFlushCallback(callback: (annotations: StreamAnnotation$1[]) => void): void;
1215
+ /**
1216
+ * Get and clear all buffered annotations.
1217
+ */
1218
+ flush(): StreamAnnotation$1[];
1219
+ /**
1220
+ * Check if there are buffered annotations.
1221
+ */
1222
+ hasBufferedAnnotations(): boolean;
1223
+ emit(event: TraceEvent): void;
1224
+ }
1225
+ /**
1226
+ * No-Op Trace Adapter
1227
+ *
1228
+ * Does nothing with trace events. Useful for testing or when tracing is not needed.
1229
+ */
1230
+ declare class NoOpTraceAdapter implements TraceAdapter {
1231
+ emit(_event: TraceEvent): void;
1232
+ }
1233
+ /**
1234
+ * Callback Trace Adapter
1235
+ *
1236
+ * Allows passing a callback function for trace events.
1237
+ * Convenient for simple custom integrations without creating a full class.
1238
+ */
1239
+ declare class CallbackTraceAdapter implements TraceAdapter {
1240
+ private readonly callback;
1241
+ constructor(callback: (event: TraceEvent) => void | Promise<void>);
1242
+ emit(event: TraceEvent): void | Promise<void>;
1243
+ }
1244
+ /**
1245
+ * Create an SSE trace adapter for a session
1246
+ */
1247
+ declare function createSSETraceAdapter(sessionId: string): TraceAdapter;
1248
+ /**
1249
+ * Create a Vercel data stream trace adapter
1250
+ */
1251
+ declare function createVercelDataStreamAdapter(): VercelDataStreamAdapter;
1252
+ /**
1253
+ * Create a buffering Vercel data stream trace adapter for progressive streaming
1254
+ */
1255
+ declare function createBufferingVercelDataStreamAdapter(): BufferingVercelDataStreamAdapter;
1256
+ /**
1257
+ * Create a no-op trace adapter
1258
+ */
1259
+ declare function createNoOpTraceAdapter(): TraceAdapter;
1260
+ /**
1261
+ * Create a callback-based trace adapter
1262
+ */
1263
+ declare function createCallbackTraceAdapter(callback: (event: TraceEvent) => void | Promise<void>): TraceAdapter;
1264
+
1265
+ /**
1266
+ * Agent Types
1267
+ *
1268
+ * Unified types for the high-level agent API.
1269
+ * These types simplify the response handling by providing a discriminated union
1270
+ * for all possible agent outcomes.
1271
+ */
1272
+
1273
+ /**
1274
+ * Citation reference from a KB snippet
1275
+ */
1276
+ interface Citation {
1277
+ /** Citation label (e.g., 'S1', 'S2') */
1278
+ label: string;
1279
+ /** KB that provided this citation */
1280
+ kbId: string;
1281
+ /** Human-readable summary of the reference */
1282
+ referenceSummary: string;
1283
+ /** Preview of the content */
1284
+ preview?: string;
1285
+ /** Full content */
1286
+ content?: string;
1287
+ /** Reference metadata including signed URLs */
1288
+ reference?: {
1289
+ id?: string;
1290
+ file?: {
1291
+ name?: string;
1292
+ signedUrl?: string;
1293
+ metadata?: Record<string, unknown>;
1294
+ };
1295
+ };
1296
+ /** MIME type of the source content */
1297
+ contentType?: string;
1298
+ /** Name of the source file */
1299
+ fileName?: string;
1300
+ }
1301
+ /**
1302
+ * Outcome when the agent provides a complete answer
1303
+ */
1304
+ interface AnswerOutcome {
1305
+ type: 'answer';
1306
+ /** The synthesized answer text */
1307
+ answer: string;
1308
+ /** Citations for the answer */
1309
+ citations: Citation[];
1310
+ /** Full execution trace */
1311
+ trace: ExecutionTrace;
1312
+ }
1313
+ /**
1314
+ * Outcome when the agent needs more information
1315
+ */
1316
+ interface FollowUpOutcome {
1317
+ type: 'follow_up';
1318
+ /** The question to ask the user */
1319
+ question: string;
1320
+ /** The slot that needs to be filled */
1321
+ slot: string;
1322
+ /** Optional multi-choice options for the user to select from */
1323
+ options?: MultiChoiceOption[];
1324
+ /** Full execution trace */
1325
+ trace: ExecutionTrace;
1326
+ }
1327
+ /**
1328
+ * Outcome when the request is blocked
1329
+ */
1330
+ interface BlockedOutcome {
1331
+ type: 'blocked';
1332
+ /** Reason why the request was blocked */
1333
+ reason: string;
1334
+ /** Full execution trace */
1335
+ trace: ExecutionTrace;
1336
+ }
1337
+ /**
1338
+ * Outcome when the query is outside the agent's scope
1339
+ */
1340
+ interface OutOfScopeOutcome {
1341
+ type: 'out_of_scope';
1342
+ /** Redirect message for the user */
1343
+ message: string;
1344
+ /** Available capabilities the agent can help with */
1345
+ capabilities: string[];
1346
+ /** Full execution trace */
1347
+ trace: ExecutionTrace;
1348
+ }
1349
+ /**
1350
+ * All possible agent outcomes as a discriminated union.
1351
+ * Use `outcome.type` to determine which outcome occurred.
1352
+ *
1353
+ * @example
1354
+ * ```typescript
1355
+ * const result = await runAgent({ message: 'Help with my toaster' });
1356
+ *
1357
+ * switch (result.outcome.type) {
1358
+ * case 'answer':
1359
+ * console.log(result.outcome.answer);
1360
+ * break;
1361
+ * case 'follow_up':
1362
+ * console.log('Need more info:', result.outcome.question);
1363
+ * break;
1364
+ * case 'blocked':
1365
+ * console.log('Cannot help:', result.outcome.reason);
1366
+ * break;
1367
+ * case 'out_of_scope':
1368
+ * console.log('Out of scope:', result.outcome.message);
1369
+ * break;
1370
+ * }
1371
+ * ```
1372
+ */
1373
+ type AgentOutcome = AnswerOutcome | FollowUpOutcome | BlockedOutcome | OutOfScopeOutcome;
1374
+ /**
1375
+ * Complete result from running the agent.
1376
+ * Contains the outcome, updated context, and session information.
1377
+ */
1378
+ interface AgentResult {
1379
+ /** The outcome of the agent execution */
1380
+ outcome: AgentOutcome;
1381
+ /** Updated agent context for multi-turn conversations */
1382
+ context: AgentContext;
1383
+ /** Session ID for tracking the conversation */
1384
+ sessionId: string;
1385
+ /** The extracted intent from the user's message */
1386
+ intent: Intent;
1387
+ }
1388
+ /**
1389
+ * Options for running the agent
1390
+ */
1391
+ interface RunAgentOptions {
1392
+ /** The user's message */
1393
+ message: string;
1394
+ /** Optional session ID (generated if not provided) */
1395
+ sessionId?: string;
1396
+ /** Previous agent context for multi-turn conversations */
1397
+ previousContext?: AgentContext;
1398
+ /** Previous intent (alternative to previousContext) */
1399
+ previousIntent?: Intent;
1400
+ /** Full conversation history */
1401
+ conversationHistory?: ConversationMessage[];
1402
+ /** Custom trace adapter for observability */
1403
+ traceAdapter?: TraceAdapter;
1404
+ /**
1405
+ * Dynamic agent configuration (for multi-tenant SaaS).
1406
+ * If provided, overrides the static agent.config.json.
1407
+ */
1408
+ config?: AgentConfig;
1409
+ /**
1410
+ * Dynamic KB manifests (for multi-tenant SaaS).
1411
+ * If provided, overrides manifests loaded from composition/manifests/.
1412
+ */
1413
+ manifests?: KBManifest[];
1414
+ }
1415
+ /**
1416
+ * Options for running the agent with streaming
1417
+ */
1418
+ interface RunAgentStreamingOptions extends RunAgentOptions {
1419
+ /** Callback for each text chunk during streaming synthesis */
1420
+ onChunk?: (chunk: string) => void;
1421
+ }
1422
+ /**
1423
+ * Result from streaming agent execution
1424
+ */
1425
+ interface StreamingAgentResult {
1426
+ /** Readable stream of the answer text (only for 'answer' outcomes) */
1427
+ stream: ReadableStream<string> | null;
1428
+ /** Promise that resolves to the complete agent result */
1429
+ result: Promise<AgentResult>;
1430
+ }
1431
+
1432
+ /**
1433
+ * Agent Runner
1434
+ *
1435
+ * High-level function that orchestrates the complete agent flow:
1436
+ * 1. Context initialization
1437
+ * 2. Disambiguation (understanding user intent)
1438
+ * 3. Planning (determining which KBs to query)
1439
+ * 4. Execution (querying KBs and synthesizing answer)
1440
+ *
1441
+ * This replaces the 300-500 lines of boilerplate in example apps.
1442
+ */
1443
+
1444
+ /**
1445
+ * Run the agent with a user message.
1446
+ *
1447
+ * This is the main entry point for the high-level API. It handles:
1448
+ * - Context initialization and restoration
1449
+ * - Disambiguation (understanding intent)
1450
+ * - Planning (selecting KBs)
1451
+ * - Execution (querying KBs and synthesizing)
1452
+ *
1453
+ * @example
1454
+ * ```typescript
1455
+ * // Simple usage
1456
+ * const result = await runAgent({ message: 'My toaster is broken' });
1457
+ *
1458
+ * // Multi-turn conversation
1459
+ * const result2 = await runAgent({
1460
+ * message: 'It's a T100',
1461
+ * previousContext: result.context,
1462
+ * });
1463
+ *
1464
+ * // With custom trace adapter
1465
+ * const result3 = await runAgent({
1466
+ * message: 'Help me fix it',
1467
+ * traceAdapter: new MyCustomTraceAdapter(),
1468
+ * });
1469
+ * ```
1470
+ */
1471
+ declare function runAgent(options: RunAgentOptions): Promise<AgentResult>;
1472
+
1473
+ /**
1474
+ * Streaming Agent Runner
1475
+ *
1476
+ * High-level function that orchestrates the complete agent flow with streaming synthesis:
1477
+ * 1. Context initialization
1478
+ * 2. Disambiguation (understanding user intent)
1479
+ * 3. Planning (determining which KBs to query)
1480
+ * 4. Execution with streaming synthesis
1481
+ *
1482
+ * Use this for Vercel AI SDK integrations that need streaming responses.
1483
+ */
1484
+
1485
+ declare function runAgentStreaming(options: RunAgentStreamingOptions): Promise<StreamingAgentResult>;
1486
+
1487
+ /**
1488
+ * Next.js Response Builder
1489
+ *
1490
+ * Converts AgentResult to Next.js API responses (NextResponse.json).
1491
+ * Use this for standard (non-streaming) Next.js API routes.
1492
+ */
1493
+
1494
+ /**
1495
+ * Convert an AgentResult to a ChatResponse object.
1496
+ *
1497
+ * This is the raw response body, which you can return with NextResponse.json()
1498
+ * or use with any other framework.
1499
+ *
1500
+ * The response includes:
1501
+ * - sessionId: For conversation tracking
1502
+ * - outcome: The type of result ('answer', 'follow_up', 'blocked', 'out_of_scope')
1503
+ * - answer/followUpQuestion: The response text
1504
+ * - options: Multi-choice options for follow-up questions
1505
+ * - citations: References from KB responses
1506
+ * - context/intent: For multi-turn conversations
1507
+ * - trace: Full execution trace (optional)
1508
+ */
1509
+ declare function toChatResponse(result: AgentResult): ChatResponse;
1510
+ /**
1511
+ * Convert an AgentResult to a Next.js Response.
1512
+ *
1513
+ * This returns a standard Response object that works with Next.js API routes.
1514
+ * For frameworks that need NextResponse, you can import NextResponse and use:
1515
+ * `NextResponse.json(toChatResponse(result))`
1516
+ *
1517
+ * @example
1518
+ * ```typescript
1519
+ * // In a Next.js API route
1520
+ * import { runAgent, toNextResponse } from '@/lib';
1521
+ *
1522
+ * export async function POST(request: NextRequest) {
1523
+ * const { message } = await request.json();
1524
+ * const result = await runAgent({ message });
1525
+ * return toNextResponse(result);
1526
+ * }
1527
+ * ```
1528
+ */
1529
+ declare function toNextResponse(result: AgentResult): Response;
1530
+ /**
1531
+ * Convert an error to a Next.js error response.
1532
+ */
1533
+ declare function toNextErrorResponse(error: Error | string, sessionId?: string): Response;
1534
+
1535
+ /**
1536
+ * Citation Utilities
1537
+ *
1538
+ * Shared utilities for extracting and processing citations from execution traces.
1539
+ * These utilities are used by both the example app and SaaS backend.
1540
+ */
1541
+
1542
+ /**
1543
+ * Extract citations from an execution trace.
1544
+ *
1545
+ * This extracts all contextSnippets from trace steps and converts them
1546
+ * to ChatCitation format. Uses global sequential labeling (S1, S2, S3...)
1547
+ * to ensure unique labels across all KBs.
1548
+ *
1549
+ * @param trace - The execution trace to extract citations from
1550
+ * @returns Array of ChatCitation objects
1551
+ *
1552
+ * @example
1553
+ * ```typescript
1554
+ * const citations = extractCitationsFromTrace(result.outcome.trace);
1555
+ * ```
1556
+ */
1557
+ declare function extractCitationsFromTrace(trace?: ExecutionTrace): ChatCitation[];
1558
+ /**
1559
+ * Filter citations to only those that are actually referenced in the answer text.
1560
+ *
1561
+ * Looks for citation markers like [S1], [S2], etc. in the text and returns
1562
+ * only the citations that match those labels.
1563
+ *
1564
+ * @param citations - All available citations
1565
+ * @param answerText - The answer text that may contain citation markers
1566
+ * @returns Filtered array of citations that are referenced in the text
1567
+ *
1568
+ * @example
1569
+ * ```typescript
1570
+ * const allCitations = extractCitationsFromTrace(trace);
1571
+ * const usedCitations = filterCitedOnly(allCitations, response.answer);
1572
+ * ```
1573
+ */
1574
+ declare function filterCitedOnly<T extends {
1575
+ label: string;
1576
+ }>(citations: T[], answerText: string): T[];
1577
+ /**
1578
+ * Convert a full Citation (from agent types) to a ChatCitation (for API responses).
1579
+ *
1580
+ * @param citation - The full Citation object
1581
+ * @returns A ChatCitation suitable for API responses
1582
+ */
1583
+ declare function toChatCitation(citation: Citation): ChatCitation;
1584
+ /**
1585
+ * Convert an array of full Citations to ChatCitations.
1586
+ *
1587
+ * @param citations - Array of full Citation objects
1588
+ * @returns Array of ChatCitation objects
1589
+ */
1590
+ declare function toChatCitations(citations: Citation[]): ChatCitation[];
1591
+
1592
+ /**
1593
+ * Streaming Utilities for Vercel AI SDK
1594
+ *
1595
+ * Provides utilities for streaming chat responses with trace step integration.
1596
+ * These types represent the wire format for Vercel AI SDK data streams.
1597
+ *
1598
+ * Note: The wire format uses 'trace_step' while the internal TraceEvent uses 'step'.
1599
+ * The adapter handles the mapping between internal events and wire format.
1600
+ */
1601
+
1602
+ /**
1603
+ * Wire format type for Vercel AI SDK data stream annotations.
1604
+ * These match the format expected by the Vercel AI SDK client.
1605
+ */
1606
+ type StreamAnnotation = {
1607
+ type: 'trace_step';
1608
+ step: TraceStep;
1609
+ } | {
1610
+ type: 'trace_complete';
1611
+ trace: ExecutionTrace;
1612
+ } | {
1613
+ type: 'session_id';
1614
+ sessionId: string;
1615
+ } | {
1616
+ type: 'follow_up';
1617
+ questions: string[];
1618
+ };
1619
+ /**
1620
+ * Convert a TraceEvent to wire format StreamAnnotation
1621
+ */
1622
+ declare function traceEventToAnnotation(event: TraceEvent): StreamAnnotation;
1623
+ /**
1624
+ * Create a trace step annotation to append to a StreamData instance
1625
+ */
1626
+ declare function createTraceStepAnnotation(step: TraceStep): StreamAnnotation;
1627
+ /**
1628
+ * Create a trace complete annotation to append to a StreamData instance
1629
+ */
1630
+ declare function createTraceCompleteAnnotation(trace: ExecutionTrace): StreamAnnotation;
1631
+ /**
1632
+ * Create a session ID annotation to append to a StreamData instance
1633
+ */
1634
+ declare function createSessionIdAnnotation(sessionId: string): StreamAnnotation;
1635
+ /**
1636
+ * Parse stream data annotation from the stream
1637
+ */
1638
+ declare function parseStreamAnnotation(data: unknown): StreamAnnotation | null;
1639
+
1640
+ /**
1641
+ * Vercel AI SDK Response Builder
1642
+ *
1643
+ * Converts AgentResult and streaming results to Vercel AI SDK responses.
1644
+ * Use this for streaming responses with the `ai` package.
1645
+ */
1646
+
1647
+ /**
1648
+ * Options for creating a Vercel AI streaming response
1649
+ */
1650
+ interface VercelAIResponseOptions {
1651
+ /** Include trace data in the stream annotations */
1652
+ includeTrace?: boolean;
1653
+ /** Custom error message handler */
1654
+ onError?: (error: Error) => string;
1655
+ }
1656
+ /**
1657
+ * Create a Vercel AI SDK compatible streaming response.
1658
+ *
1659
+ * This creates a Response with a ReadableStream that includes:
1660
+ * - Data stream annotations for trace steps
1661
+ * - The streamed text content
1662
+ * - Trace complete annotation at the end
1663
+ *
1664
+ * @example
1665
+ * ```typescript
1666
+ * import { runAgentStreaming, toVercelAIResponse } from '@/lib';
1667
+ *
1668
+ * export async function POST(request: NextRequest) {
1669
+ * const { message } = await request.json();
1670
+ * const streamingResult = await runAgentStreaming({ message });
1671
+ * return toVercelAIResponse(streamingResult);
1672
+ * }
1673
+ * ```
1674
+ */
1675
+ declare function toVercelAIResponse(streamingResult: StreamingAgentResult, options?: VercelAIResponseOptions): Promise<Response>;
1676
+ /**
1677
+ * Create a simple Vercel AI response for non-streaming results.
1678
+ *
1679
+ * Use this when you have an AgentResult from runAgent() (non-streaming)
1680
+ * but want to return it in a Vercel AI compatible format.
1681
+ */
1682
+ declare function toVercelAISimpleResponse(result: AgentResult, options?: VercelAIResponseOptions): Response;
1683
+ /**
1684
+ * Utility to extract the text answer from an AgentResult
1685
+ */
1686
+ declare function getAnswerText(result: AgentResult): string;
1687
+ /**
1688
+ * Create a truly streaming Vercel AI SDK response.
1689
+ *
1690
+ * Unlike `toVercelAIResponse()`, this function does NOT await the full result.
1691
+ * Instead, it interleaves trace annotations with text chunks as they become available,
1692
+ * providing a truly progressive streaming experience.
1693
+ *
1694
+ * IMPORTANT: The `traceAdapter` passed to `runAgentStreaming()` must be a
1695
+ * `BufferingVercelDataStreamAdapter` for progressive trace delivery.
1696
+ *
1697
+ * @example
1698
+ * ```typescript
1699
+ * import {
1700
+ * runAgentStreaming,
1701
+ * toVercelAIStreamingResponse,
1702
+ * createBufferingVercelDataStreamAdapter,
1703
+ * } from '@/lib';
1704
+ *
1705
+ * export async function POST(request: NextRequest) {
1706
+ * const { message } = await request.json();
1707
+ * const traceAdapter = createBufferingVercelDataStreamAdapter();
1708
+ * const streamingResult = await runAgentStreaming({ message, traceAdapter });
1709
+ * return toVercelAIStreamingResponse(streamingResult, traceAdapter);
1710
+ * }
1711
+ * ```
1712
+ */
1713
+ declare function toVercelAIStreamingResponse(streamingResult: StreamingAgentResult, traceAdapter: BufferingVercelDataStreamAdapter, options?: VercelAIResponseOptions): Response;
1714
+
1715
+ /**
1716
+ * Trace Collector Implementation
1717
+ *
1718
+ * Implementation of the TraceCollector interface for collecting execution traces.
1719
+ * Separates tracing concerns from business logic.
1720
+ */
1721
+
1722
+ /**
1723
+ * Step counter for generating unique step IDs.
1724
+ *
1725
+ * Tracks counts per step type (e.g., disambiguation, planning, slot_filling, synthesis)
1726
+ * and generates incrementing IDs like `disambiguation_1`, `disambiguation_2`, etc.
1727
+ *
1728
+ * Can be initialized from AgentContext.stepCounts to persist across conversation turns.
1729
+ */
1730
+ declare class StepCounter {
1731
+ private counts;
1732
+ /**
1733
+ * Create a new StepCounter, optionally initializing from existing counts.
1734
+ *
1735
+ * @param initialCounts - Optional initial counts (e.g., from AgentContext.stepCounts)
1736
+ */
1737
+ constructor(initialCounts?: Record<string, number>);
1738
+ /**
1739
+ * Create a StepCounter from an AgentContext.
1740
+ * Use this to continue counting from where a previous request left off.
1741
+ */
1742
+ static fromContext(context: {
1743
+ stepCounts?: Record<string, number>;
1744
+ }): StepCounter;
1745
+ /**
1746
+ * Get the next step ID for a given step type.
1747
+ * Increments the counter and returns the new ID.
1748
+ *
1749
+ * @param stepType - The type of step (e.g., 'disambiguation', 'planning')
1750
+ * @returns A unique step ID like 'disambiguation_1'
1751
+ */
1752
+ next(stepType: string): string;
1753
+ /**
1754
+ * Get the current count for a step type (without incrementing).
1755
+ */
1756
+ current(stepType: string): number;
1757
+ /**
1758
+ * Reset the counter for a specific step type or all step types.
1759
+ */
1760
+ reset(stepType?: string): void;
1761
+ /**
1762
+ * Get all counts as a plain object.
1763
+ * Use this to persist the counter state to AgentContext.stepCounts.
1764
+ */
1765
+ toObject(): Record<string, number>;
1766
+ /**
1767
+ * Update an AgentContext with the current step counts.
1768
+ * Call this before returning the context to the client.
1769
+ */
1770
+ syncToContext(context: {
1771
+ stepCounts?: Record<string, number>;
1772
+ }): void;
1773
+ }
1774
+ /**
1775
+ * Create a new trace collector instance
1776
+ */
1777
+ declare function createTraceCollector(sessionId: string): TraceCollector;
1778
+
1779
+ /**
1780
+ * Middleware Module
1781
+ *
1782
+ * Composable middleware functions for building custom agent pipelines.
1783
+ * Use these when you need more control than the high-level runAgent() provides.
1784
+ *
1785
+ * @example
1786
+ * ```typescript
1787
+ * import {
1788
+ * createPipelineContext,
1789
+ * withDisambiguation,
1790
+ * withPlanning,
1791
+ * withExecution,
1792
+ * compose,
1793
+ * } from '@/lib/middleware';
1794
+ *
1795
+ * // Create a custom pipeline
1796
+ * const myPipeline = compose(
1797
+ * withDisambiguation(),
1798
+ * withPlanning(),
1799
+ * withExecution(),
1800
+ * );
1801
+ *
1802
+ * // Run it
1803
+ * const context = createPipelineContext({ message: 'Help me' });
1804
+ * const result = await myPipeline(context);
1805
+ * ```
1806
+ */
1807
+
1808
+ /**
1809
+ * Pipeline context passed through middleware functions
1810
+ */
1811
+ interface PipelineContext {
1812
+ message: string;
1813
+ sessionId: string;
1814
+ conversationHistory?: ConversationMessage[];
1815
+ agentContext: AgentContext;
1816
+ stepCounter: StepCounter;
1817
+ traceSteps: TraceStep[];
1818
+ frameworkContext: FrameworkContext;
1819
+ traceAdapter: TraceAdapter;
1820
+ intent?: Intent;
1821
+ disambiguationAction?: {
1822
+ type: 'ASK_SLOT' | 'CALL_KB' | 'PARTIAL_PROCEED' | 'BLOCKED' | 'OUT_OF_SCOPE';
1823
+ [key: string]: unknown;
1824
+ };
1825
+ relevantKBs?: KBMeta[];
1826
+ plan?: Plan;
1827
+ stepResults?: StepResult[];
1828
+ answer?: string;
1829
+ shouldStop?: boolean;
1830
+ stopReason?: 'follow_up' | 'blocked' | 'out_of_scope';
1831
+ }
1832
+ /**
1833
+ * Middleware function type
1834
+ */
1835
+ type Middleware = (context: PipelineContext, next: () => Promise<PipelineContext>) => Promise<PipelineContext>;
1836
+ /**
1837
+ * Create an initial pipeline context
1838
+ */
1839
+ interface CreatePipelineContextOptions {
1840
+ message: string;
1841
+ sessionId?: string;
1842
+ previousContext?: AgentContext;
1843
+ previousIntent?: Intent;
1844
+ conversationHistory?: ConversationMessage[];
1845
+ traceAdapter?: TraceAdapter;
1846
+ }
1847
+ declare function createPipelineContext(options: CreatePipelineContextOptions): PipelineContext;
1848
+ /**
1849
+ * Compose multiple middleware functions into a single function
1850
+ */
1851
+ declare function compose(...middlewares: Middleware[]): (context: PipelineContext) => Promise<PipelineContext>;
1852
+ /**
1853
+ * Disambiguation middleware
1854
+ *
1855
+ * Extracts intent and determines routing.
1856
+ * Sets: intent, disambiguationAction, relevantKBs
1857
+ * May set: shouldStop, stopReason
1858
+ */
1859
+ declare function withDisambiguation(): Middleware;
1860
+ /**
1861
+ * Planning middleware
1862
+ *
1863
+ * Creates an execution plan based on intent and relevant KBs.
1864
+ * Requires: intent, relevantKBs
1865
+ * Sets: plan
1866
+ */
1867
+ declare function withPlanning(): Middleware;
1868
+ /**
1869
+ * Execution middleware
1870
+ *
1871
+ * Executes the plan (KB calls + synthesis).
1872
+ * Requires: intent, plan
1873
+ * Sets: stepResults, answer
1874
+ */
1875
+ declare function withExecution(): Middleware;
1876
+ /**
1877
+ * Build the final execution trace from pipeline context
1878
+ */
1879
+ declare function buildTrace(context: PipelineContext): ExecutionTrace;
1880
+ /**
1881
+ * Logging middleware - logs each phase
1882
+ */
1883
+ declare function withLogging(): Middleware;
1884
+ /**
1885
+ * Error handling middleware - catches and logs errors
1886
+ */
1887
+ declare function withErrorHandling(onError?: (error: Error, context: PipelineContext) => void): Middleware;
1888
+
1889
+ /**
1890
+ * Pure state reducer for disambiguation.
1891
+ * No LLM calls - just deterministic state transitions based on facts.
1892
+ */
1893
+
1894
+ /** Details about why a KB was selected during routing */
1895
+ interface KBRoutingReason {
1896
+ kbId: string;
1897
+ selectedBy: 'llm_hint' | 'slot_trigger' | 'input_match' | 'fallback';
1898
+ trigger?: string;
1899
+ }
1900
+ /** Details about the routing decision for tracing */
1901
+ interface RoutingDecision {
1902
+ kbHintsReceived: string[];
1903
+ kbsFromHints: string[];
1904
+ slotTriggersMatched: Array<{
1905
+ kbId: string;
1906
+ trigger: string;
1907
+ }>;
1908
+ kbsFromSlotTriggers: string[];
1909
+ kbsFromFallback: string[];
1910
+ finalKBs: string[];
1911
+ routingReasons: KBRoutingReason[];
1912
+ }
1913
+
1914
+ /**
1915
+ * Disambiguation orchestrator.
1916
+ * Thin layer that coordinates between:
1917
+ * - LLM extractor (facts)
1918
+ * - State reducer (decisions)
1919
+ * - Question generator (phrasing)
1920
+ */
1921
+
1922
+ /**
1923
+ * Main disambiguation function.
1924
+ * Orchestrates the three-layer architecture:
1925
+ * 1. Extract facts from LLM (handles all reasoning including yes/no, confirmations, etc.)
1926
+ * 2. Run state reducer to get action
1927
+ * 3. Generate question if needed
1928
+ *
1929
+ * @param utterance - The user's current message
1930
+ * @param kbCatalog - Available knowledge bases
1931
+ * @param context - Current agent context (optional)
1932
+ * @param conversationHistory - Full conversation history (optional)
1933
+ * @param frameworkContext - Framework context with LLM provider
1934
+ */
1935
+ declare function disambiguate(utterance: string, kbCatalog: KBMeta[], context: AgentContext | undefined, conversationHistory: ConversationMessage[] | undefined, frameworkContext: FrameworkContext): Promise<DisambiguationResult>;
1936
+
1937
+ /**
1938
+ * Creates an execution plan to answer a user's query using available knowledge bases.
1939
+ *
1940
+ * This is the main planning function that uses an LLM to intelligently select and sequence
1941
+ * knowledge base calls to answer the user's question. The planner:
1942
+ * 1. Analyzes the user's intent and goals
1943
+ * 2. Selects relevant knowledge bases from the catalog
1944
+ * 3. Determines the optimal sequence of KB calls
1945
+ * 4. Adds synthesis steps when multiple KBs are needed
1946
+ * 5. Provides reasoning and confidence scores
1947
+ *
1948
+ * The function respects disambiguation results: if `relevantKBs` is provided (from the
1949
+ * disambiguation/reducer phase), it only considers those KBs, ensuring the plan aligns
1950
+ * with the user's current goals rather than the original query.
1951
+ *
1952
+ * @param intent - The user's intent containing their query, goals, available slots, and missing information
1953
+ * @param kbCatalog - Full catalog of available knowledge bases with their metadata
1954
+ * @param context - Optional agent context containing conversation history, collected information, and previous thinking
1955
+ * @param relevantKBs - Optional filtered list of KBs from disambiguation. If provided, only these KBs will be considered.
1956
+ * This allows the planner to adapt to evolving user goals during multi-turn conversations.
1957
+ *
1958
+ * @returns A promise resolving to:
1959
+ * - plan: The execution plan with steps, reasoning, and confidence
1960
+ * - updatedContext: Updated agent context with planning reasoning added to agentThinking
1961
+ *
1962
+ * @example
1963
+ * ```typescript
1964
+ * const { plan, updatedContext } = await plan(
1965
+ * { userUtterance: "How do I resolve this issue?", goals: ["help"], ... },
1966
+ * kbCatalog,
1967
+ * existingContext,
1968
+ * relevantKBs // Only consider specific KBs if disambiguation narrowed it down
1969
+ * );
1970
+ * ```
1971
+ */
1972
+ declare function plan(intent: Intent, kbCatalog: KBMeta[], context?: AgentContext, relevantKBs?: KBMeta[], frameworkContext?: FrameworkContext): Promise<{
1973
+ plan: Plan;
1974
+ updatedContext: AgentContext;
1975
+ }>;
1976
+
1977
+ /**
1978
+ * Core Executor Module
1979
+ *
1980
+ * Main entry point for plan execution using the framework context.
1981
+ * This is the refactored version that uses dependency injection.
1982
+ */
1983
+
1984
+ /**
1985
+ * Execute a plan using the framework context
1986
+ *
1987
+ * This is the main entry point for plan execution with dependency injection.
1988
+ *
1989
+ * @param intent - User intent containing the original utterance and goals
1990
+ * @param plan - The execution plan with steps to run
1991
+ * @param context - Optional agent context with prior conversation state
1992
+ * @param frameworkContext - Framework context with all providers
1993
+ * @returns Object containing the generated answer and execution trace
1994
+ */
1995
+ declare function executePlanWithContext(intent: Intent, plan: Plan, context: AgentContext | undefined, frameworkContext: FrameworkContext): Promise<{
1996
+ answer: string;
1997
+ trace: ExecutionTrace;
1998
+ }>;
1999
+
2000
+ /**
2001
+ * KB Runner Module
2002
+ *
2003
+ * Handles execution of KB call steps in the plan.
2004
+ */
2005
+
2006
+ /**
2007
+ * Result from running a single KB call step
2008
+ */
2009
+ interface KBCallResult {
2010
+ stepResult: StepResult;
2011
+ kbRetrieval?: KBRetrieval;
2012
+ success: boolean;
2013
+ }
2014
+ /**
2015
+ * Execute a single KB call step
2016
+ *
2017
+ * @param step - The KB call step to execute
2018
+ * @param intent - User intent containing the original utterance and goals
2019
+ * @param previousKBCalls - Array of previously executed KB calls for context
2020
+ * @param frameworkContext - Framework context with all providers
2021
+ * @returns Result object containing stepResult, optional kbRetrieval, and success status
2022
+ */
2023
+ declare function executeKBCallStep(step: PlanStepKBCall, intent: Intent, previousKBCalls: Array<{
2024
+ kbId: string;
2025
+ stepId: string;
2026
+ success: boolean;
2027
+ }>, frameworkContext: FrameworkContext): Promise<KBCallResult>;
2028
+
2029
+ /**
2030
+ * Synthesis Module
2031
+ *
2032
+ * Handles the synthesis of final answers from KB responses.
2033
+ */
2034
+
2035
+ /**
2036
+ * Result from synthesis step
2037
+ */
2038
+ interface SynthesisResult {
2039
+ answer: string;
2040
+ stepResult: StepResult;
2041
+ }
2042
+ /**
2043
+ * Result from streaming synthesis step
2044
+ */
2045
+ interface StreamingSynthesisResult {
2046
+ stream: ReadableStream<Uint8Array>;
2047
+ stepResult: StepResult;
2048
+ }
2049
+ /**
2050
+ * Synthesize a final answer from KB responses
2051
+ *
2052
+ * This is the main synthesis function that orchestrates the answer generation process.
2053
+ * It uses helper functions to:
2054
+ * 1. Collect and organize KB responses
2055
+ * 2. Gather and label context snippets for citation
2056
+ * 3. Check for missing/insufficient information
2057
+ * 4. Generate the final synthesized answer
2058
+ * 5. Process citations and format the response
2059
+ */
2060
+ declare function synthesizeAnswer(synthesisStep: PlanStepSynthesis, intent: Intent, stepResults: StepResult[], frameworkContext: FrameworkContext): Promise<SynthesisResult>;
2061
+ /**
2062
+ * Synthesize a final answer from KB responses with streaming support
2063
+ *
2064
+ * This version uses streamText for progressive text generation.
2065
+ */
2066
+ declare function synthesizeAnswerStreaming(synthesisStep: PlanStepSynthesis, intent: Intent, stepResults: StepResult[], frameworkContext: FrameworkContext): Promise<StreamingSynthesisResult>;
2067
+ /**
2068
+ * Handle the case when all KB calls failed
2069
+ */
2070
+ declare function handleAllKBsFailed(stepResults: StepResult[], kbCount: number, frameworkContext: FrameworkContext): SynthesisResult;
2071
+
2072
+ /**
2073
+ * Snippet Formatter Module
2074
+ *
2075
+ * Handles formatting and summarizing snippet references for display
2076
+ */
2077
+ /**
2078
+ * Formats a snippet reference into a human-readable string
2079
+ * Extracts metadata like title, name, category, row_id, and source
2080
+ * Produces concise titles suitable for citation display chips
2081
+ *
2082
+ * @param snippet - Snippet object with reference metadata
2083
+ * @returns Formatted string like "FAQ 106 - DrySwift 600" or "Warranty 28 - Toaster"
2084
+ */
2085
+ declare function summarizeSnippetReference(snippet: any): string;
2086
+
2087
+ /**
2088
+ * Plan Executor Mediator
2089
+ *
2090
+ * Orchestrates plan creation, execution, and adaptive modification.
2091
+ * This module breaks the potential circular dependency between planner and executor
2092
+ * by acting as a mediator layer.
2093
+ *
2094
+ * Architecture:
2095
+ * ```
2096
+ * Agent Runner
2097
+ * │
2098
+ * ▼
2099
+ * Plan Executor (this module)
2100
+ * │
2101
+ * ┌────┴────┐
2102
+ * ▼ ▼
2103
+ * Planner Executor
2104
+ * ```
2105
+ *
2106
+ * The planner and executor don't import each other directly - all coordination
2107
+ * happens through this module. This enables:
2108
+ * - Adaptive planning (modifying plans based on execution results)
2109
+ * - Clean dependency graph with no cycles
2110
+ * - Single point of control for plan lifecycle
2111
+ */
2112
+
2113
+ /**
2114
+ * Options for adaptive plan execution
2115
+ */
2116
+ interface AdaptiveExecutionOptions {
2117
+ /** Whether to attempt plan modification on failure (default: true) */
2118
+ enableAdaptivePlanning?: boolean;
2119
+ /** Maximum number of plan modification attempts (default: 1) */
2120
+ maxModificationAttempts?: number;
2121
+ }
2122
+ /**
2123
+ * Execute a plan with adaptive modification support
2124
+ *
2125
+ * This function provides intelligent plan execution with the ability to
2126
+ * modify the plan mid-execution if things don't go as expected.
2127
+ *
2128
+ * Adaptive planning scenarios:
2129
+ * 1. All KB calls fail → Try alternative KBs if available
2130
+ * 2. Partial information → Adjust synthesis to work with what we have
2131
+ * 3. Unexpected results → Re-route to different KBs
2132
+ *
2133
+ * @param intent - User intent containing the original utterance and goals
2134
+ * @param plan - Initial execution plan
2135
+ * @param context - Agent context with conversation state
2136
+ * @param frameworkContext - Framework context with all providers
2137
+ * @param options - Optional adaptive execution settings
2138
+ * @returns Object containing the generated answer and execution trace
2139
+ *
2140
+ * @example
2141
+ * ```typescript
2142
+ * const { answer, trace } = await executePlanAdaptively(
2143
+ * intent,
2144
+ * plan,
2145
+ * context,
2146
+ * frameworkContext,
2147
+ * { enableAdaptivePlanning: true }
2148
+ * );
2149
+ * ```
2150
+ */
2151
+ declare function executePlanAdaptively(intent: Intent, plan: Plan, context: AgentContext | undefined, frameworkContext: FrameworkContext, options?: AdaptiveExecutionOptions): Promise<{
2152
+ answer: string;
2153
+ trace: ExecutionTrace;
2154
+ }>;
2155
+ /**
2156
+ * Create and execute a plan in one step
2157
+ *
2158
+ * Convenience function that combines plan creation and execution.
2159
+ * Useful for simple cases where you don't need to inspect the plan
2160
+ * before execution.
2161
+ *
2162
+ * @param intent - User intent
2163
+ * @param kbCatalog - Available knowledge bases
2164
+ * @param context - Agent context
2165
+ * @param frameworkContext - Framework context
2166
+ * @returns Execution result with answer and trace
2167
+ */
2168
+ declare function planAndExecute(intent: Intent, kbCatalog: any[], context: AgentContext | undefined, frameworkContext: FrameworkContext): Promise<{
2169
+ answer: string;
2170
+ trace: ExecutionTrace;
2171
+ plan: Plan;
2172
+ }>;
2173
+
2174
+ /**
2175
+ * Agent Context Factory
2176
+ *
2177
+ * Creates and manages the conversation state context for agents.
2178
+ */
2179
+
2180
+ /**
2181
+ * Initialize a new AgentContext
2182
+ *
2183
+ * @param userUtterance - The user's initial message or current utterance
2184
+ * @param previousContext - Optional previous context to merge with (for multi-turn conversations)
2185
+ * @returns A new or merged AgentContext
2186
+ */
2187
+ declare function createAgentContext(userUtterance: string, previousContext?: AgentContext): AgentContext;
2188
+
2189
+ /**
2190
+ * KB Catalog Module
2191
+ *
2192
+ * Manages the catalog of available knowledge bases by converting
2193
+ * manifest definitions into runtime KBMeta objects.
2194
+ */
2195
+
2196
+ /** Catalog of all available knowledge bases (static, from files) */
2197
+ declare const kbCatalog: KBMeta[];
2198
+ /**
2199
+ * Build a KB catalog from manifests (for dynamic/SaaS use)
2200
+ *
2201
+ * @param manifests - Array of KB manifests
2202
+ * @param config - Optional config to use for slot definitions
2203
+ * @returns Array of KBMeta objects
2204
+ */
2205
+ declare function buildKBCatalog(manifests: KBManifest[], config?: AgentConfig): KBMeta[];
2206
+ /**
2207
+ * Get metadata for a specific knowledge base from the static catalog
2208
+ *
2209
+ * @param kbId - The unique identifier of the knowledge base
2210
+ * @returns KBMeta object if found, undefined otherwise
2211
+ */
2212
+ declare function getKBMeta(kbId: string): KBMeta | undefined;
2213
+ /**
2214
+ * Get metadata for a specific knowledge base from a dynamic catalog
2215
+ *
2216
+ * @param kbId - The unique identifier of the knowledge base
2217
+ * @param catalog - The catalog to search in
2218
+ * @returns KBMeta object if found, undefined otherwise
2219
+ */
2220
+ declare function getKBMetaFromCatalog(kbId: string, catalog: KBMeta[]): KBMeta | undefined;
2221
+
2222
+ declare function loadCompositionRegistry(): CompositionRegistry;
2223
+ declare function getSlotDefinitions(): SlotDefinition[];
2224
+ declare function getSlotDefinition$1(name: string): SlotDefinition | undefined;
2225
+ declare function buildSlotsSchema$1(): z.ZodObject<SlotSchemaShape>;
2226
+ declare function getKBManifests(): KBManifest[];
2227
+ declare function getKBManifestById(kbId: string): KBManifest | undefined;
2228
+ /**
2229
+ * Get all unique capabilities across all knowledge base manifests.
2230
+ * Used for generating redirect messages that show what the agent can help with.
2231
+ */
2232
+ declare function getAllCapabilities(): string[];
2233
+ /**
2234
+ * Get all unique domains across all knowledge base manifests.
2235
+ * Used for generating redirect messages.
2236
+ */
2237
+ declare function getAllDomains(): string[];
2238
+
2239
+ /**
2240
+ * Load and validate the agent configuration from agent.config.json
2241
+ */
2242
+ declare function loadConfig(configPath?: string): AgentConfig;
2243
+ /**
2244
+ * Get the current config (loads if not already loaded)
2245
+ * If a runtime config is set, returns that instead.
2246
+ */
2247
+ declare function getConfig(): AgentConfig;
2248
+ /**
2249
+ * Set a runtime config override (for SaaS multi-tenant use).
2250
+ * This allows dynamic config without a static agent.config.json file.
2251
+ *
2252
+ * @param config - The config to use, or null to clear
2253
+ */
2254
+ declare function setRuntimeConfig(config: AgentConfig | null): void;
2255
+ /**
2256
+ * Get the current runtime config (if set)
2257
+ */
2258
+ declare function getRuntimeConfig(): AgentConfig | null;
2259
+ /**
2260
+ * Build a Zod schema for slots based on config
2261
+ */
2262
+ declare function buildSlotsSchema(config?: AgentConfig): z.ZodObject<any>;
2263
+ /**
2264
+ * Get slot definition by name
2265
+ */
2266
+ declare function getSlotDefinition(slotName: string, config?: AgentConfig): SlotDefinition$1 | undefined;
2267
+ /**
2268
+ * Format a slot value based on its type
2269
+ */
2270
+ declare function formatSlotValue(slotName: string, value: string | boolean | undefined, config?: AgentConfig): string | undefined;
2271
+ /**
2272
+ * Get the LLM model for a specific purpose
2273
+ */
2274
+ declare function getModel(purpose: 'planning' | 'extraction' | 'synthesis' | 'questionGeneration', config?: AgentConfig): string;
2275
+ /**
2276
+ * Format citation label based on config
2277
+ */
2278
+ declare function formatCitationLabel(index: number, config?: AgentConfig): string;
2279
+ /**
2280
+ * Clear cached config (useful for testing)
2281
+ *
2282
+ * @param resetBasePath - If true, also resets the base path to default (cwd)
2283
+ * @param clearRuntime - If true, also clears the runtime config override
2284
+ */
2285
+ declare function clearConfigCache(resetBasePath?: boolean, clearRuntime?: boolean): void;
2286
+ /**
2287
+ * Get prompt template with substitutions
2288
+ */
2289
+ declare function getPrompt(key: 'plannerSystemRole' | 'synthesisSystemRole' | 'planningInstructions' | 'synthesisInstructions', config?: AgentConfig): string;
2290
+ /**
2291
+ * Get fallback message
2292
+ */
2293
+ declare function getMessage(key: 'allKBsFailed' | 'noRelevantInfo' | 'synthesisError', config?: AgentConfig): string;
2294
+ /**
2295
+ * Get session configuration
2296
+ */
2297
+ declare function getSessionConfig(config?: AgentConfig): {
2298
+ maxLogsPerSession: number;
2299
+ maxTraceStepsPerSession: number;
2300
+ };
2301
+
2302
+ /**
2303
+ * Framework Context Factory
2304
+ *
2305
+ * Creates and manages the dependency injection container for the framework.
2306
+ */
2307
+
2308
+ /**
2309
+ * Options for creating a framework context
2310
+ */
2311
+ interface ContextOptions {
2312
+ sessionId: string;
2313
+ }
2314
+ /**
2315
+ * Create a framework context with all providers initialized
2316
+ *
2317
+ * This is the main entry point for dependency injection.
2318
+ * All engines and components should receive this context.
2319
+ */
2320
+ declare function createFrameworkContext(options: ContextOptions): FrameworkContext;
2321
+
2322
+ /**
2323
+ * Default OpenAI chat model IDs and env-based resolution.
2324
+ *
2325
+ * Agent runtime continues to use `agent.config.json` (`llm.models`) via `getModel()`.
2326
+ * These helpers apply to introspection, CLI helpers, and other code paths that
2327
+ * do not load an agent config.
2328
+ *
2329
+ * Env precedence for introspection:
2330
+ * 1. Explicit `ProgressiveIntrospectionConfig.llmModel` / API base field
2331
+ * 2. `KBS_INTROSPECTION_MODEL`
2332
+ * 3. `KBS_DEFAULT_OPENAI_MODEL`
2333
+ * 4. `OPENAI_DEFAULT_CHAT_MODEL`
2334
+ * 5. {@link DEFAULT_OPENAI_CHAT_MODEL_ID}
2335
+ */
2336
+ declare const DEFAULT_OPENAI_CHAT_MODEL_ID: "gpt-4o-mini";
2337
+ /**
2338
+ * Model id for progressive introspection and manifest generation (OpenAI).
2339
+ */
2340
+ declare function resolveIntrospectionOpenAiModelId(explicit?: string | null): string;
2341
+ /**
2342
+ * Generic default chat model when no agent config applies (CLI tools, eval grader, etc.).
2343
+ */
2344
+ declare function resolveDefaultOpenAiChatModelId(explicit?: string | null): string;
2345
+ /** Stronger model for structured JSON / complex generation (`KBS_STRONG_OPENAI_MODEL`, default gpt-4o). */
2346
+ declare function resolveStrongOpenAiModelId(explicit?: string | null): string;
2347
+
2348
+ /**
2349
+ * Usage Tracker
2350
+ *
2351
+ * Tracks token usage and dollar cost across all LLM calls during introspection.
2352
+ * Supports both OpenAI (generateObject) and Pinecone (assistant.chat) providers.
2353
+ */
2354
+ interface TokenUsage {
2355
+ promptTokens: number;
2356
+ completionTokens: number;
2357
+ totalTokens: number;
2358
+ }
2359
+ interface UsageEntry {
2360
+ phase: string;
2361
+ step: string;
2362
+ model: string;
2363
+ provider: 'openai' | 'pinecone';
2364
+ usage: TokenUsage;
2365
+ cost: {
2366
+ input: number;
2367
+ output: number;
2368
+ total: number;
2369
+ };
2370
+ timestamp: number;
2371
+ }
2372
+ interface PhaseUsage {
2373
+ tokens: TokenUsage;
2374
+ cost: number;
2375
+ calls: number;
2376
+ }
2377
+ interface UsageSummary {
2378
+ totalTokens: TokenUsage;
2379
+ totalCost: number;
2380
+ byPhase: Record<string, PhaseUsage>;
2381
+ byProvider: Record<string, PhaseUsage>;
2382
+ entries: UsageEntry[];
2383
+ }
2384
+ interface UsageRecordInput {
2385
+ phase?: string;
2386
+ step: string;
2387
+ model: string;
2388
+ provider: 'openai' | 'pinecone';
2389
+ usage: {
2390
+ promptTokens: number;
2391
+ completionTokens: number;
2392
+ totalTokens?: number;
2393
+ };
2394
+ }
2395
+ declare class UsageTracker {
2396
+ private entries;
2397
+ private onUpdate?;
2398
+ private _currentPhase;
2399
+ constructor(onUpdate?: (summary: UsageSummary) => void);
2400
+ /**
2401
+ * Set the current phase. Subsequent record() calls will use this phase
2402
+ * unless an explicit phase is provided.
2403
+ */
2404
+ setPhase(phase: string): void;
2405
+ record(input: UsageRecordInput): void;
2406
+ getSummary(): UsageSummary;
2407
+ }
2408
+
2409
+ /**
2410
+ * Progressive Introspection Types
2411
+ *
2412
+ * Type definitions for the progressive introspection engine that generates
2413
+ * KB manifests by iteratively querying Pinecone assistants.
2414
+ */
2415
+
2416
+ /**
2417
+ * Schema extraction result for L2 level
2418
+ */
2419
+ interface SchemaExtraction {
2420
+ intent: string;
2421
+ slots: Array<{
2422
+ name: string;
2423
+ type: string;
2424
+ required: boolean;
2425
+ validation_rule?: string;
2426
+ enumValues?: string[];
2427
+ }>;
2428
+ is_deterministic: boolean;
2429
+ reasoning?: string;
2430
+ }
2431
+ /**
2432
+ * Configuration for the progressive introspection process
2433
+ */
2434
+ interface ProgressiveIntrospectionConfig {
2435
+ /** Maximum depth to drill down into categories/entities (default: 3) */
2436
+ maxDepth: number;
2437
+ /** Maximum expansions per discovered entity (default: 5) */
2438
+ maxBranchFactor: number;
2439
+ /** Minimum new entities needed to continue drilling (default: 2) */
2440
+ expansionThreshold: number;
2441
+ /** Total question budget cap (default: 50) */
2442
+ maxTotalQuestions: number;
2443
+ /** Maximum retries per question when getting generic responses (default: 3) */
2444
+ maxRetries: number;
2445
+ /** Minimum confidence threshold to accept a response (default: 0.4) */
2446
+ minConfidenceThreshold: number;
2447
+ /**
2448
+ * OpenAI model id for introspection LLM calls.
2449
+ * Override via config, or env: `KBS_INTROSPECTION_MODEL`, `KBS_DEFAULT_OPENAI_MODEL`, `OPENAI_DEFAULT_CHAT_MODEL`.
2450
+ */
2451
+ llmModel: string;
2452
+ }
2453
+ /**
2454
+ * Default configuration for progressive introspection
2455
+ */
2456
+ declare const DEFAULT_PROGRESSIVE_CONFIG: ProgressiveIntrospectionConfig;
2457
+ /**
2458
+ * Objective types for question formulation
2459
+ */
2460
+ type QuestionObjective = 'seed_discovery' | 'expand_category' | 'drill_entity' | 'find_relationships' | 'validate_scope' | 'gather_examples';
2461
+ /**
2462
+ * Context provided to the question formulator
2463
+ */
2464
+ interface QuestionFormulationContext {
2465
+ /** What we're trying to discover */
2466
+ objective: QuestionObjective;
2467
+ /** The specific category/entity we're exploring (if applicable) */
2468
+ target?: string;
2469
+ /** Parent context for hierarchical drilling */
2470
+ parentContext?: string;
2471
+ /** Current depth in the exploration tree */
2472
+ depth: number;
2473
+ /** What we already know (to avoid redundant questions) */
2474
+ knownEntities: string[];
2475
+ knownCategories: string[];
2476
+ /** Recent conversation history for context */
2477
+ recentQA: {
2478
+ question: string;
2479
+ answer: string;
2480
+ }[];
2481
+ /** Questions that got generic/evasive responses */
2482
+ failedApproaches: string[];
2483
+ /** Domain hint if available */
2484
+ domain?: string;
2485
+ }
2486
+ /**
2487
+ * Expected response type from a question
2488
+ */
2489
+ type ExpectedResponseType = 'list' | 'explanation' | 'examples' | 'boundaries' | 'relationships';
2490
+ /**
2491
+ * Output from the question formulator
2492
+ */
2493
+ interface FormulatedQuestion {
2494
+ /** The actual question to ask */
2495
+ question: string;
2496
+ /** Why this question should work */
2497
+ rationale: string;
2498
+ /** What kind of response we expect */
2499
+ expectedResponseType: ExpectedResponseType;
2500
+ /** Alternative questions if this one fails */
2501
+ fallbackQuestions: string[];
2502
+ }
2503
+ /**
2504
+ * Classification of a response from the validator
2505
+ */
2506
+ type ResponseClassification = 'substantive' | 'generic' | 'evasive' | 'off_topic' | 'repetitive';
2507
+ /**
2508
+ * Suggested strategy when a response isn't valid
2509
+ */
2510
+ type RetryStrategy = 'rephrase' | 'simplify' | 'anchor_with_examples' | 'try_different_angle' | 'skip';
2511
+ /**
2512
+ * Output from the response validator
2513
+ */
2514
+ interface ResponseValidation {
2515
+ /** Whether the response is useful */
2516
+ isValid: boolean;
2517
+ /** Classification of the response */
2518
+ classification: ResponseClassification;
2519
+ /** Confidence in the classification (0-1) */
2520
+ confidence: number;
2521
+ /** What's wrong if not valid */
2522
+ issue?: string;
2523
+ /** Suggested approach for retry */
2524
+ suggestedRetryStrategy?: RetryStrategy;
2525
+ /** New entities extracted from the response */
2526
+ extractedEntities: string[];
2527
+ /** New categories extracted from the response */
2528
+ extractedCategories: string[];
2529
+ /** Summary of what we learned */
2530
+ newInformation?: string;
2531
+ }
2532
+ /**
2533
+ * A task in the expansion queue
2534
+ */
2535
+ interface ExpansionTask {
2536
+ type: 'category' | 'entity' | 'relationship' | 'examples';
2537
+ target: string;
2538
+ depth: number;
2539
+ parentContext?: string;
2540
+ context: {
2541
+ question: string;
2542
+ answer: string;
2543
+ }[];
2544
+ }
2545
+ /**
2546
+ * Category discovered during introspection
2547
+ */
2548
+ interface DiscoveredCategory {
2549
+ name: string;
2550
+ depth: number;
2551
+ parent?: string;
2552
+ children: string[];
2553
+ entities: string[];
2554
+ explored: boolean;
2555
+ }
2556
+ /**
2557
+ * Entity discovered during introspection
2558
+ */
2559
+ interface DiscoveredEntity {
2560
+ name: string;
2561
+ category?: string;
2562
+ depth: number;
2563
+ relatedEntities: string[];
2564
+ explored: boolean;
2565
+ }
2566
+ /**
2567
+ * Relationship between entities
2568
+ */
2569
+ interface DiscoveredRelationship {
2570
+ from: string;
2571
+ to: string;
2572
+ type: string;
2573
+ }
2574
+ /**
2575
+ * A single conversation exchange with validation
2576
+ */
2577
+ interface ConversationEntry {
2578
+ question: string;
2579
+ answer: string;
2580
+ validation?: ResponseValidation;
2581
+ }
2582
+ /**
2583
+ * Document snippet from Context API
2584
+ */
2585
+ interface DocumentSnippet {
2586
+ content: string;
2587
+ score: number;
2588
+ sourceFile: string;
2589
+ }
2590
+ /**
2591
+ * Evidence supporting a manifest component decision.
2592
+ * Contains the LLM's reasoning plus citations to source material.
2593
+ */
2594
+ interface ComponentEvidence {
2595
+ /** LLM explanation of why this component exists */
2596
+ reasoning: string;
2597
+ /** Actual KB content (document snippets) that informed this decision */
2598
+ documentSnippets: Array<{
2599
+ content: string;
2600
+ sourceFile: string;
2601
+ score: number;
2602
+ }>;
2603
+ /** Q&A exchanges that surfaced the evidence during introspection */
2604
+ qaExchanges: Array<{
2605
+ questionNumber: number;
2606
+ question: string;
2607
+ answerExcerpt: string;
2608
+ }>;
2609
+ }
2610
+ /**
2611
+ * State of the progressive introspection process
2612
+ */
2613
+ interface IntrospectionState {
2614
+ /** Discovered categories with their exploration status */
2615
+ categories: Map<string, DiscoveredCategory>;
2616
+ /** Discovered entities */
2617
+ entities: Map<string, DiscoveredEntity>;
2618
+ /** Discovered relationships between entities */
2619
+ relationships: DiscoveredRelationship[];
2620
+ /** Full conversation history */
2621
+ conversationHistory: ConversationEntry[];
2622
+ /** Queue of tasks to process */
2623
+ expansionQueue: ExpansionTask[];
2624
+ /** Total questions asked */
2625
+ questionsAsked: number;
2626
+ /** Capabilities discovered (with optional evidence) */
2627
+ capabilities: Array<{
2628
+ text: string;
2629
+ evidence?: ComponentEvidence;
2630
+ }>;
2631
+ /** Scope information */
2632
+ scopeInfo: {
2633
+ inScope: string[];
2634
+ outOfScope: string[];
2635
+ };
2636
+ /** Example questions discovered */
2637
+ exampleQuestions: string[];
2638
+ /** Source files discovered from context API */
2639
+ sourceFiles: Set<string>;
2640
+ /** Raw snippets from documents (actual content) */
2641
+ documentSnippets: DocumentSnippet[];
2642
+ /** Extracted schemas from L2 discovery (intents with slots) */
2643
+ extractedSchemas?: SchemaExtraction[];
2644
+ }
2645
+ /**
2646
+ * Result from asking a question and validating the response
2647
+ */
2648
+ interface AskResult {
2649
+ success: boolean;
2650
+ question: string;
2651
+ response?: string;
2652
+ validation?: ResponseValidation;
2653
+ attempts: number;
2654
+ failedApproaches: string[];
2655
+ /** Raw snippets from context API (actual document content) */
2656
+ snippets?: Array<{
2657
+ content: string;
2658
+ score: number;
2659
+ reference: unknown;
2660
+ }>;
2661
+ /** Source files discovered */
2662
+ sourceFiles?: string[];
2663
+ }
2664
+ /**
2665
+ * Callbacks for reporting introspection progress.
2666
+ * Used by CLI and SaaS to provide their own presentation layer.
2667
+ */
2668
+ interface IntrospectionCallbacks {
2669
+ /** Called when a new phase starts */
2670
+ onPhaseStart?: (phase: 'seed_discovery' | 'entity_expansion' | 'relationship_discovery' | 'abstraction_refinement' | 'enum_extraction' | 'manifest_generation', assistantName: string) => void;
2671
+ /** Called with phase progress updates */
2672
+ onPhaseProgress?: (phase: string, message: string) => void;
2673
+ /** Called when a question is being asked */
2674
+ onQuestion?: (questionNum: number, totalBudget: number, question: string, objective: QuestionObjective) => void;
2675
+ /** Called when an answer is received */
2676
+ onAnswer?: (questionNum: number, answer: string) => void;
2677
+ /** Called when a response is validated */
2678
+ onValidation?: (questionNum: number, validation: ResponseValidation) => void;
2679
+ /** Called when entities/categories are discovered */
2680
+ onDiscovery?: (categories: number, entities: number, sourceFiles: number) => void;
2681
+ /** Called when a step in manifest generation completes */
2682
+ onManifestStep?: (step: number, totalSteps: number, description: string) => void;
2683
+ /** Called on error */
2684
+ onError?: (assistantName: string, error: string) => void;
2685
+ /** Called when introspection completes for an assistant */
2686
+ onComplete?: (assistantName: string, manifest: KBManifest) => void;
2687
+ /** Called when LLM usage is recorded (fires after each LLM call) */
2688
+ onUsageUpdate?: (summary: UsageSummary) => void;
2689
+ }
2690
+ /**
2691
+ * Options for running progressive introspection
2692
+ */
2693
+ interface ProgressiveIntrospectionOptions {
2694
+ /** Pinecone assistant name to introspect */
2695
+ assistantName: string;
2696
+ /** KB ID for the generated manifest (defaults to assistant name) */
2697
+ kbId?: string;
2698
+ /** Domain hint (will be inferred if not provided) */
2699
+ domain?: string;
2700
+ /** Progressive introspection configuration */
2701
+ config?: Partial<ProgressiveIntrospectionConfig>;
2702
+ /** Callbacks for progress reporting */
2703
+ callbacks?: IntrospectionCallbacks;
2704
+ /** Pinecone API key (uses env var if not provided) */
2705
+ pineconeApiKey?: string;
2706
+ /** OpenAI API key (uses env var if not provided) */
2707
+ openaiApiKey?: string;
2708
+ }
2709
+ /**
2710
+ * Result from progressive introspection
2711
+ */
2712
+ interface ProgressiveIntrospectionResult {
2713
+ /** Generated manifest */
2714
+ manifest: KBManifest;
2715
+ /** Introspection state with all discovered information */
2716
+ state: IntrospectionState;
2717
+ /** Summary statistics */
2718
+ stats: {
2719
+ questionsAsked: number;
2720
+ categoriesDiscovered: number;
2721
+ entitiesDiscovered: number;
2722
+ sourceFilesFound: number;
2723
+ documentSnippetsCaptured: number;
2724
+ /** LLM token usage and cost breakdown */
2725
+ usage?: UsageSummary;
2726
+ };
2727
+ }
2728
+
2729
+ /**
2730
+ * Progressive Introspection Engine
2731
+ *
2732
+ * Core orchestrator for the 4-phase progressive introspection process:
2733
+ * 1. Seed Discovery - Initial exploration of KB content
2734
+ * 2. Entity Expansion - Drill down into discovered categories/entities
2735
+ * 3. Relationship Discovery - Find connections between entities
2736
+ * 4. Manifest Inference - Extract intents, slots, capabilities from conversation
2737
+ */
2738
+
2739
+ /**
2740
+ * Initialize a fresh introspection state
2741
+ */
2742
+ declare function initializeState(): IntrospectionState;
2743
+ /**
2744
+ * Run progressive introspection on a Pinecone assistant
2745
+ */
2746
+ declare function progressiveIntrospect(options: ProgressiveIntrospectionOptions): Promise<ProgressiveIntrospectionResult>;
2747
+
2748
+ /**
2749
+ * Question Formulator
2750
+ *
2751
+ * LLM-assisted question generation for progressive introspection.
2752
+ * Generates targeted questions to discover KB content and capabilities.
2753
+ */
2754
+
2755
+ /**
2756
+ * Generate a targeted question using LLM assistance
2757
+ */
2758
+ declare function formulateQuestion(context: QuestionFormulationContext, tracker?: UsageTracker, llmModel?: string): Promise<FormulatedQuestion>;
2759
+ /**
2760
+ * Seed objectives for initial discovery phase
2761
+ */
2762
+ declare const SEED_OBJECTIVES: QuestionObjective[];
2763
+
2764
+ /**
2765
+ * Response Validator
2766
+ *
2767
+ * LLM-assisted validation of assistant responses to detect generic,
2768
+ * evasive, or unhelpful responses during introspection.
2769
+ */
2770
+
2771
+ /**
2772
+ * Validate a response from the assistant
2773
+ */
2774
+ declare function validateResponse(question: string, response: string, expectedType: ExpectedResponseType, target: string | undefined, knownInformation: string[], tracker?: UsageTracker, llmModel?: string): Promise<ResponseValidation>;
2775
+
2776
+ /**
2777
+ * Manifest Generator
2778
+ *
2779
+ * Converts introspection state into a KB manifest using multi-step
2780
+ * LLM-assisted generation for each manifest component.
2781
+ */
2782
+
2783
+ /**
2784
+ * Generate a complete manifest from introspection state
2785
+ */
2786
+ declare function generateManifestFromState(base: {
2787
+ kbId: string;
2788
+ assistantName: string;
2789
+ domain?: string;
2790
+ llmModel?: string;
2791
+ }, state: IntrospectionState, callbacks?: IntrospectionCallbacks, tracker?: UsageTracker): Promise<KBManifest>;
2792
+
2793
+ /**
2794
+ * Multi-KB Orchestration Framework
2795
+ *
2796
+ * Main entry point for the framework. Import from here for the public API.
2797
+ *
2798
+ * ## High-Level API (Recommended)
2799
+ *
2800
+ * Use `runAgent()` or `runAgentStreaming()` for simple integrations:
2801
+ *
2802
+ * @example
2803
+ * ```ts
2804
+ * import { runAgent, toNextResponse } from '@/lib';
2805
+ *
2806
+ * const result = await runAgent({ message: 'Help me with my toaster' });
2807
+ * return toNextResponse(result);
2808
+ * ```
2809
+ *
2810
+ * ## Low-Level API (Advanced)
2811
+ *
2812
+ * For custom pipelines, use the individual orchestration functions:
2813
+ *
2814
+ * @example
2815
+ * ```ts
2816
+ * import {
2817
+ * disambiguate,
2818
+ * plan,
2819
+ * executePlanWithContext,
2820
+ * kbCatalog,
2821
+ * } from '@/lib';
2822
+ * ```
2823
+ */
2824
+ /** Agent runners - single function to run the complete agent pipeline */
2825
+
2826
+ /** Smoke-test constant to verify local linking is working. */
2827
+ declare const KAT_LINK_CHECK: "@kat/core is locally linked!";
2828
+
2829
+ export { type StreamAnnotation as AdapterStreamAnnotation, type AdaptiveExecutionOptions, type AgentConfig, AgentConfigSchema, AgentContext, type AgentOutcome, type AgentResult, type AgentThinking, type AnswerOutcome, type AskResult, type BehaviorConfig, BehaviorConfigSchema, type BlockedOutcome, BufferingVercelDataStreamAdapter, CallbackTraceAdapter, ChatCitation, ChatResponse, type Citation, type CitationsConfig, CitationsConfigSchema, type ComponentEvidence, type CompositionRegistry, type SlotDefinition$1 as ConfigSlotDefinition, type ContextOptions, type ConversationEntry, ConversationMessage, type CreatePipelineContextOptions, DEFAULT_OPENAI_CHAT_MODEL_ID, DEFAULT_PROGRESSIVE_CONFIG, type DataStreamWriter, DisambiguationResult, type DiscoveredCategory, type DiscoveredEntity, type DiscoveredRelationship, type DocumentSnippet, type DynamicSlots, ExecutionTrace, type ExpansionTask, type ExpectedResponseType, type FollowUpOutcome, type FormulatedQuestion, type FrameworkContext, type GoalExamples, GoalExamplesSchema, Intent, type IntrospectionCallbacks, type IntrospectionState, KAT_LINK_CHECK, type KBCapability, type KBConversationPatterns, type KBEdgeCases, type KBGoal, type KBInputField, type KBManifest, type KBMeta, type KBMetadata, type KBProvider, type KBProviderConfig, KBProviderConfigSchema, type KBQualityIndicators, type KBQueryOptions, type KBQueryResult, type KBQueryTemplate, type KBRelevanceHints, type KBResponsePatterns, KBRetrieval, type KBRoutingReason, type KBScopeDefinition, type KBSnippet, type KBTerminology, type LLMConfig, LLMConfigSchema, type LLMObjectResult, type LLMOptions, type LLMProvider, type LLMStreamResult, type LLMTextResult, type Logger, type MessagesConfig, MessagesConfigSchema, type Middleware, MultiChoiceOption, NoOpTraceAdapter, type NormalizationRule, type OutOfScopeOutcome, type PhaseUsage, type PipelineContext, Plan, PlanStepKBCall, PlanStepSynthesis, type ProgressiveIntrospectionConfig, type ProgressiveIntrospectionOptions, type ProgressiveIntrospectionResult, type PromptsConfig, PromptsConfigSchema, type ProvidersConfig, ProvidersConfigSchema, type QueryTemplateSection, type QuestionFormulationContext, type QuestionObjective, type ResponseClassification, type ResponseValidation, type RetryStrategy, type RoutingDecision, type RunAgentOptions, type RunAgentStreamingOptions, SEED_OBJECTIVES, SSETraceAdapter, type SessionConfig, SessionConfigSchema, type SlotDefinition, SlotDefinitionSchema, type SlotDependency, type SlotExtractionBehavior, type SlotNormalization, type SlotNormalizer, type SlotSchemaShape, type SlotValidation, type SlotValuePrimitive, Slots, StepCounter, StepResult, type StreamAnnotation, type StreamingAgentResult, type StreamingSynthesisResult, type SynthesisResult, type TemplateContext, type TemplateEngine, type TokenUsage, type TraceAdapter, type TraceCollector, TraceEvent, TraceStep, type UsageEntry, type UsageRecordInput, type UsageSummary, UsageTracker, type VercelAIResponseOptions, VercelDataStreamAdapter, buildKBCatalog, buildSlotsSchema, buildSlotsSchema$1 as buildSlotsSchemaFromRegistry, buildTrace, clearConfigCache, compose, createAgentContext, createBufferingVercelDataStreamAdapter, createCallbackTraceAdapter, createFrameworkContext, createNoOpTraceAdapter, createPipelineContext, createSSETraceAdapter, createSessionIdAnnotation, createTraceCollector, createTraceCompleteAnnotation, createTraceStepAnnotation, createVercelDataStreamAdapter, disambiguate, executeKBCallStep, executePlanAdaptively, executePlanWithContext, extractCitationsFromTrace, filterCitedOnly, formatCitationLabel, formatSlotValue, formulateQuestion, generateManifestFromState, getAllCapabilities, getAllDomains, getAnswerText, getConfig, getKBManifestById, getKBManifests, getKBMeta, getKBMetaFromCatalog, getMessage, getModel, getPrompt, getRuntimeConfig, getSessionConfig, getSlotDefinition, getSlotDefinition$1 as getSlotDefinitionFromRegistry, getSlotDefinitions, handleAllKBsFailed, initializeState, kbCatalog, loadCompositionRegistry, loadConfig, parseStreamAnnotation, plan, planAndExecute, progressiveIntrospect, resolveDefaultOpenAiChatModelId, resolveIntrospectionOpenAiModelId, resolveStrongOpenAiModelId, runAgent, runAgentStreaming, setRuntimeConfig, summarizeSnippetReference, synthesizeAnswer, synthesizeAnswerStreaming, toChatCitation, toChatCitations, toChatResponse, toNextErrorResponse, toNextResponse, toVercelAIResponse, toVercelAISimpleResponse, toVercelAIStreamingResponse, traceEventToAnnotation, validateResponse, withDisambiguation, withErrorHandling, withExecution, withLogging, withPlanning };