@radaros/core 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.
Files changed (48) hide show
  1. package/dist/index.d.ts +887 -0
  2. package/dist/index.js +3462 -0
  3. package/package.json +64 -0
  4. package/src/agent/agent.ts +314 -0
  5. package/src/agent/llm-loop.ts +263 -0
  6. package/src/agent/run-context.ts +35 -0
  7. package/src/agent/types.ts +77 -0
  8. package/src/events/event-bus.ts +45 -0
  9. package/src/events/types.ts +16 -0
  10. package/src/guardrails/types.ts +5 -0
  11. package/src/hooks/types.ts +6 -0
  12. package/src/index.ts +111 -0
  13. package/src/knowledge/knowledge-base.ts +146 -0
  14. package/src/logger/logger.ts +232 -0
  15. package/src/memory/memory.ts +87 -0
  16. package/src/memory/types.ts +13 -0
  17. package/src/models/provider.ts +22 -0
  18. package/src/models/providers/anthropic.ts +330 -0
  19. package/src/models/providers/google.ts +361 -0
  20. package/src/models/providers/ollama.ts +211 -0
  21. package/src/models/providers/openai.ts +323 -0
  22. package/src/models/registry.ts +90 -0
  23. package/src/models/types.ts +112 -0
  24. package/src/session/session-manager.ts +75 -0
  25. package/src/session/types.ts +10 -0
  26. package/src/storage/driver.ts +10 -0
  27. package/src/storage/in-memory.ts +44 -0
  28. package/src/storage/mongodb.ts +70 -0
  29. package/src/storage/postgres.ts +81 -0
  30. package/src/storage/sqlite.ts +81 -0
  31. package/src/team/modes.ts +1 -0
  32. package/src/team/team.ts +323 -0
  33. package/src/team/types.ts +26 -0
  34. package/src/tools/define-tool.ts +20 -0
  35. package/src/tools/tool-executor.ts +131 -0
  36. package/src/tools/types.ts +27 -0
  37. package/src/vector/base.ts +44 -0
  38. package/src/vector/embeddings/google.ts +64 -0
  39. package/src/vector/embeddings/openai.ts +66 -0
  40. package/src/vector/in-memory.ts +115 -0
  41. package/src/vector/mongodb.ts +241 -0
  42. package/src/vector/pgvector.ts +169 -0
  43. package/src/vector/qdrant.ts +203 -0
  44. package/src/vector/types.ts +55 -0
  45. package/src/workflow/step-runner.ts +303 -0
  46. package/src/workflow/types.ts +55 -0
  47. package/src/workflow/workflow.ts +68 -0
  48. package/tsconfig.json +8 -0
@@ -0,0 +1,887 @@
1
+ import { z } from 'zod';
2
+
3
+ type MessageRole = "system" | "user" | "assistant" | "tool";
4
+ interface TextPart {
5
+ type: "text";
6
+ text: string;
7
+ }
8
+ interface ImagePart {
9
+ type: "image";
10
+ /** Base64-encoded image data OR a URL. */
11
+ data: string;
12
+ mimeType?: "image/png" | "image/jpeg" | "image/gif" | "image/webp";
13
+ }
14
+ interface AudioPart {
15
+ type: "audio";
16
+ /** Base64-encoded audio data. */
17
+ data: string;
18
+ mimeType?: "audio/mp3" | "audio/wav" | "audio/ogg" | "audio/webm";
19
+ }
20
+ interface FilePart {
21
+ type: "file";
22
+ /** Base64-encoded file data OR a URL. */
23
+ data: string;
24
+ mimeType: string;
25
+ filename?: string;
26
+ }
27
+ type ContentPart = TextPart | ImagePart | AudioPart | FilePart;
28
+ /** Convenience: plain string, or an array of multi-modal content parts. */
29
+ type MessageContent = string | ContentPart[];
30
+ interface ChatMessage {
31
+ role: MessageRole;
32
+ content: MessageContent | null;
33
+ toolCalls?: ToolCall[];
34
+ toolCallId?: string;
35
+ name?: string;
36
+ }
37
+ interface ToolCall {
38
+ id: string;
39
+ name: string;
40
+ arguments: Record<string, unknown>;
41
+ }
42
+ interface ToolDefinition {
43
+ name: string;
44
+ description: string;
45
+ parameters: Record<string, unknown>;
46
+ }
47
+ interface TokenUsage {
48
+ promptTokens: number;
49
+ completionTokens: number;
50
+ totalTokens: number;
51
+ }
52
+ interface ModelResponse {
53
+ message: ChatMessage;
54
+ usage: TokenUsage;
55
+ finishReason: "stop" | "tool_calls" | "length" | "content_filter";
56
+ raw: unknown;
57
+ }
58
+ type StreamChunk = {
59
+ type: "text";
60
+ text: string;
61
+ } | {
62
+ type: "tool_call_start";
63
+ toolCall: {
64
+ id: string;
65
+ name: string;
66
+ };
67
+ } | {
68
+ type: "tool_call_delta";
69
+ toolCallId: string;
70
+ argumentsDelta: string;
71
+ } | {
72
+ type: "tool_call_end";
73
+ toolCallId: string;
74
+ } | {
75
+ type: "finish";
76
+ finishReason: string;
77
+ usage?: TokenUsage;
78
+ };
79
+ interface ModelConfig {
80
+ temperature?: number;
81
+ maxTokens?: number;
82
+ topP?: number;
83
+ stop?: string[];
84
+ responseFormat?: "text" | "json" | {
85
+ type: "json_schema";
86
+ schema: Record<string, unknown>;
87
+ name?: string;
88
+ };
89
+ /** Per-request API key override. When provided, the provider uses this key instead of the one set at construction. */
90
+ apiKey?: string;
91
+ }
92
+ /** Extract the text content from a MessageContent value. */
93
+ declare function getTextContent(content: MessageContent | null): string;
94
+ /** Check if content has multi-modal parts. */
95
+ declare function isMultiModal(content: MessageContent | null): content is ContentPart[];
96
+
97
+ interface ModelProvider {
98
+ readonly providerId: string;
99
+ readonly modelId: string;
100
+ generate(messages: ChatMessage[], options?: ModelConfig & {
101
+ tools?: ToolDefinition[];
102
+ }): Promise<ModelResponse>;
103
+ stream(messages: ChatMessage[], options?: ModelConfig & {
104
+ tools?: ToolDefinition[];
105
+ }): AsyncGenerator<StreamChunk>;
106
+ }
107
+
108
+ declare class RunContext {
109
+ readonly runId: string;
110
+ readonly sessionId: string;
111
+ readonly userId?: string;
112
+ readonly metadata: Record<string, unknown>;
113
+ readonly eventBus: EventBus;
114
+ sessionState: Record<string, unknown>;
115
+ constructor(opts: {
116
+ sessionId: string;
117
+ userId?: string;
118
+ metadata?: Record<string, unknown>;
119
+ eventBus: EventBus;
120
+ sessionState?: Record<string, unknown>;
121
+ runId?: string;
122
+ });
123
+ getState<T>(key: string): T | undefined;
124
+ setState(key: string, value: unknown): void;
125
+ }
126
+
127
+ interface Artifact {
128
+ type: string;
129
+ data: unknown;
130
+ mimeType?: string;
131
+ }
132
+ interface ToolResult {
133
+ content: string;
134
+ artifacts?: Artifact[];
135
+ }
136
+ interface ToolDef {
137
+ name: string;
138
+ description: string;
139
+ parameters: z.ZodObject<any>;
140
+ execute: (args: Record<string, unknown>, ctx: RunContext) => Promise<string | ToolResult>;
141
+ }
142
+ interface ToolCallResult {
143
+ toolCallId: string;
144
+ toolName: string;
145
+ result: string | ToolResult;
146
+ error?: string;
147
+ }
148
+
149
+ interface StorageDriver {
150
+ get<T>(namespace: string, key: string): Promise<T | null>;
151
+ set<T>(namespace: string, key: string, value: T): Promise<void>;
152
+ delete(namespace: string, key: string): Promise<void>;
153
+ list<T>(namespace: string, prefix?: string): Promise<Array<{
154
+ key: string;
155
+ value: T;
156
+ }>>;
157
+ close(): Promise<void>;
158
+ }
159
+
160
+ interface MemoryConfig {
161
+ storage?: StorageDriver;
162
+ maxShortTermMessages?: number;
163
+ enableLongTerm?: boolean;
164
+ }
165
+ interface MemoryEntry {
166
+ key: string;
167
+ summary: string;
168
+ createdAt: Date;
169
+ }
170
+
171
+ declare class Memory {
172
+ private storage;
173
+ private maxShortTermMessages;
174
+ private enableLongTerm;
175
+ constructor(config?: MemoryConfig);
176
+ addMessages(sessionId: string, messages: ChatMessage[]): Promise<void>;
177
+ getMessages(sessionId: string): Promise<ChatMessage[]>;
178
+ getSummaries(sessionId: string): Promise<string[]>;
179
+ getContextString(sessionId: string): Promise<string>;
180
+ private summarizeAndStore;
181
+ clear(sessionId: string): Promise<void>;
182
+ }
183
+
184
+ type LogLevel = "debug" | "info" | "warn" | "error" | "silent";
185
+ interface LoggerConfig {
186
+ level?: LogLevel;
187
+ color?: boolean;
188
+ prefix?: string;
189
+ }
190
+ declare class Logger {
191
+ private level;
192
+ private color;
193
+ private prefix;
194
+ constructor(config?: LoggerConfig);
195
+ private c;
196
+ private shouldLog;
197
+ private tag;
198
+ private timestamp;
199
+ private log;
200
+ private formatValue;
201
+ debug(msg: string, data?: Record<string, unknown>): void;
202
+ info(msg: string, data?: Record<string, unknown>): void;
203
+ warn(msg: string, data?: Record<string, unknown>): void;
204
+ error(msg: string, data?: Record<string, unknown>): void;
205
+ private readonly boxWidth;
206
+ private pipe;
207
+ private printBoxLine;
208
+ agentStart(agentName: string, input: string): void;
209
+ toolCall(toolName: string, args: Record<string, unknown>): void;
210
+ toolResult(toolName: string, result: string): void;
211
+ agentEnd(agentName: string, output: string, usage: {
212
+ promptTokens: number;
213
+ completionTokens: number;
214
+ totalTokens: number;
215
+ }, durationMs: number): void;
216
+ separator(): void;
217
+ private formatDuration;
218
+ }
219
+
220
+ interface AgentConfig {
221
+ name: string;
222
+ model: ModelProvider;
223
+ tools?: ToolDef[];
224
+ instructions?: string | ((ctx: RunContext) => string);
225
+ memory?: Memory;
226
+ storage?: StorageDriver;
227
+ sessionId?: string;
228
+ userId?: string;
229
+ addHistoryToMessages?: boolean;
230
+ numHistoryRuns?: number;
231
+ maxToolRoundtrips?: number;
232
+ temperature?: number;
233
+ structuredOutput?: z.ZodSchema;
234
+ hooks?: AgentHooks;
235
+ guardrails?: {
236
+ input?: InputGuardrail[];
237
+ output?: OutputGuardrail[];
238
+ };
239
+ eventBus?: EventBus;
240
+ /** Logging level. Set to "debug" for tool call details, "info" for summaries, "silent" to disable. Default: "silent". */
241
+ logLevel?: LogLevel;
242
+ }
243
+ interface RunOpts {
244
+ sessionId?: string;
245
+ userId?: string;
246
+ metadata?: Record<string, unknown>;
247
+ /** Per-request API key override passed to the model provider. */
248
+ apiKey?: string;
249
+ }
250
+ interface RunOutput {
251
+ text: string;
252
+ toolCalls: ToolCallResult[];
253
+ usage: TokenUsage;
254
+ /** Parsed structured output if structuredOutput schema is set. */
255
+ structured?: unknown;
256
+ durationMs?: number;
257
+ }
258
+ interface AgentHooks {
259
+ beforeRun?: (ctx: RunContext) => Promise<void>;
260
+ afterRun?: (ctx: RunContext, output: RunOutput) => Promise<void>;
261
+ onToolCall?: (ctx: RunContext, toolName: string, args: unknown) => Promise<void>;
262
+ onError?: (ctx: RunContext, error: Error) => Promise<void>;
263
+ }
264
+ type GuardrailResult = {
265
+ pass: true;
266
+ } | {
267
+ pass: false;
268
+ reason: string;
269
+ };
270
+ interface InputGuardrail {
271
+ name: string;
272
+ validate: (input: MessageContent, ctx: RunContext) => Promise<GuardrailResult>;
273
+ }
274
+ interface OutputGuardrail {
275
+ name: string;
276
+ validate: (output: RunOutput, ctx: RunContext) => Promise<GuardrailResult>;
277
+ }
278
+
279
+ type AgentEventMap = {
280
+ "run.start": {
281
+ runId: string;
282
+ agentName: string;
283
+ input: string;
284
+ };
285
+ "run.complete": {
286
+ runId: string;
287
+ output: RunOutput;
288
+ };
289
+ "run.error": {
290
+ runId: string;
291
+ error: Error;
292
+ };
293
+ "run.stream.chunk": {
294
+ runId: string;
295
+ chunk: string;
296
+ };
297
+ "tool.call": {
298
+ runId: string;
299
+ toolName: string;
300
+ args: unknown;
301
+ };
302
+ "tool.result": {
303
+ runId: string;
304
+ toolName: string;
305
+ result: unknown;
306
+ };
307
+ "team.delegate": {
308
+ runId: string;
309
+ memberId: string;
310
+ task: string;
311
+ };
312
+ "workflow.step": {
313
+ runId: string;
314
+ stepName: string;
315
+ status: "start" | "done" | "error";
316
+ };
317
+ };
318
+
319
+ type EventKey = keyof AgentEventMap;
320
+ declare class EventBus {
321
+ private emitter;
322
+ constructor();
323
+ on<K extends EventKey>(event: K, handler: (data: AgentEventMap[K]) => void): this;
324
+ once<K extends EventKey>(event: K, handler: (data: AgentEventMap[K]) => void): this;
325
+ off<K extends EventKey>(event: K, handler: (data: AgentEventMap[K]) => void): this;
326
+ emit<K extends EventKey>(event: K, data: AgentEventMap[K]): boolean;
327
+ removeAllListeners(event?: EventKey): this;
328
+ }
329
+
330
+ declare class Agent {
331
+ readonly name: string;
332
+ readonly eventBus: EventBus;
333
+ readonly instructions?: string | ((ctx: RunContext) => string);
334
+ private config;
335
+ private sessionManager;
336
+ private llmLoop;
337
+ private logger;
338
+ get tools(): ToolDef[];
339
+ get modelId(): string;
340
+ get providerId(): string;
341
+ get hasStructuredOutput(): boolean;
342
+ constructor(config: AgentConfig);
343
+ run(input: MessageContent, opts?: RunOpts): Promise<RunOutput>;
344
+ stream(input: MessageContent, opts?: RunOpts): AsyncGenerator<StreamChunk>;
345
+ private buildMessages;
346
+ }
347
+
348
+ declare class ToolExecutor {
349
+ private tools;
350
+ private concurrency;
351
+ constructor(tools: ToolDef[], concurrency?: number);
352
+ executeAll(toolCalls: ToolCall[], ctx: RunContext): Promise<ToolCallResult[]>;
353
+ private executeSingle;
354
+ getToolDefinitions(): Array<{
355
+ name: string;
356
+ description: string;
357
+ parameters: Record<string, unknown>;
358
+ }>;
359
+ }
360
+
361
+ declare class LLMLoop {
362
+ private provider;
363
+ private toolExecutor;
364
+ private maxToolRoundtrips;
365
+ private temperature?;
366
+ private maxTokens?;
367
+ private structuredOutput?;
368
+ private logger?;
369
+ constructor(provider: ModelProvider, toolExecutor: ToolExecutor | null, options: {
370
+ maxToolRoundtrips: number;
371
+ temperature?: number;
372
+ maxTokens?: number;
373
+ structuredOutput?: z.ZodSchema;
374
+ logger?: Logger;
375
+ });
376
+ run(messages: ChatMessage[], ctx: RunContext, apiKey?: string): Promise<RunOutput>;
377
+ stream(messages: ChatMessage[], ctx: RunContext, apiKey?: string): AsyncGenerator<StreamChunk>;
378
+ private extractJson;
379
+ private zodToJsonSchema;
380
+ }
381
+
382
+ declare enum TeamMode {
383
+ Coordinate = "coordinate",
384
+ Route = "route",
385
+ Broadcast = "broadcast",
386
+ Collaborate = "collaborate"
387
+ }
388
+ interface TeamConfig {
389
+ name: string;
390
+ mode: TeamMode;
391
+ model: ModelProvider;
392
+ members: Agent[];
393
+ instructions?: string;
394
+ sessionState?: Record<string, unknown>;
395
+ storage?: StorageDriver;
396
+ maxRounds?: number;
397
+ eventBus?: EventBus;
398
+ }
399
+
400
+ declare class Team {
401
+ readonly name: string;
402
+ readonly eventBus: EventBus;
403
+ private config;
404
+ constructor(config: TeamConfig);
405
+ run(input: string, opts?: RunOpts): Promise<RunOutput>;
406
+ stream(input: string, opts?: RunOpts): AsyncGenerator<StreamChunk>;
407
+ private runCoordinateMode;
408
+ private runRouteMode;
409
+ private runBroadcastMode;
410
+ private runCollaborateMode;
411
+ private buildMemberDescriptions;
412
+ private buildCoordinatorPrompt;
413
+ private buildSynthesisPrompt;
414
+ private parseDelegationPlan;
415
+ private findMember;
416
+ }
417
+
418
+ type StepDef<TState> = AgentStep<TState> | FunctionStep<TState> | ConditionStep<TState> | ParallelStep<TState>;
419
+ interface AgentStep<TState> {
420
+ name: string;
421
+ agent: Agent;
422
+ inputFrom?: (state: TState) => string;
423
+ }
424
+ interface FunctionStep<TState> {
425
+ name: string;
426
+ run: (state: TState, ctx: RunContext) => Promise<Partial<TState>>;
427
+ }
428
+ interface ConditionStep<TState> {
429
+ name: string;
430
+ condition: (state: TState) => boolean;
431
+ steps: StepDef<TState>[];
432
+ }
433
+ interface ParallelStep<TState> {
434
+ name: string;
435
+ parallel: StepDef<TState>[];
436
+ }
437
+ interface WorkflowConfig<TState extends Record<string, unknown> = Record<string, unknown>> {
438
+ name: string;
439
+ initialState: TState;
440
+ steps: StepDef<TState>[];
441
+ storage?: StorageDriver;
442
+ retryPolicy?: {
443
+ maxRetries: number;
444
+ backoffMs: number;
445
+ };
446
+ eventBus?: EventBus;
447
+ }
448
+ interface WorkflowResult<TState> {
449
+ state: TState;
450
+ stepResults: StepResult[];
451
+ }
452
+ interface StepResult {
453
+ stepName: string;
454
+ status: "done" | "error" | "skipped";
455
+ error?: string;
456
+ durationMs: number;
457
+ }
458
+
459
+ declare class Workflow<TState extends Record<string, unknown> = Record<string, unknown>> {
460
+ readonly name: string;
461
+ readonly eventBus: EventBus;
462
+ private config;
463
+ private stepRunner;
464
+ constructor(config: WorkflowConfig<TState>);
465
+ run(opts?: {
466
+ sessionId?: string;
467
+ userId?: string;
468
+ }): Promise<WorkflowResult<TState>>;
469
+ }
470
+
471
+ type ProviderFactory = (modelId: string, config?: Record<string, unknown>) => ModelProvider;
472
+ declare class ModelRegistry {
473
+ private factories;
474
+ register(providerId: string, factory: ProviderFactory): void;
475
+ resolve(providerId: string, modelId: string, config?: Record<string, unknown>): ModelProvider;
476
+ has(providerId: string): boolean;
477
+ }
478
+ declare const registry: ModelRegistry;
479
+ declare function openai(modelId: string, config?: {
480
+ apiKey?: string;
481
+ baseURL?: string;
482
+ }): ModelProvider;
483
+ declare function anthropic(modelId: string, config?: {
484
+ apiKey?: string;
485
+ }): ModelProvider;
486
+ declare function google(modelId: string, config?: {
487
+ apiKey?: string;
488
+ }): ModelProvider;
489
+ declare function ollama(modelId: string, config?: {
490
+ host?: string;
491
+ }): ModelProvider;
492
+
493
+ interface OpenAIConfig {
494
+ apiKey?: string;
495
+ baseURL?: string;
496
+ }
497
+ declare class OpenAIProvider implements ModelProvider {
498
+ readonly providerId = "openai";
499
+ readonly modelId: string;
500
+ private client;
501
+ private OpenAICtor;
502
+ private baseURL?;
503
+ private clientCache;
504
+ constructor(modelId: string, config?: OpenAIConfig);
505
+ private getClient;
506
+ generate(messages: ChatMessage[], options?: ModelConfig & {
507
+ tools?: ToolDefinition[];
508
+ }): Promise<ModelResponse>;
509
+ stream(messages: ChatMessage[], options?: ModelConfig & {
510
+ tools?: ToolDefinition[];
511
+ }): AsyncGenerator<StreamChunk>;
512
+ private applyResponseFormat;
513
+ private toOpenAIMessages;
514
+ private partToOpenAI;
515
+ private toOpenAITools;
516
+ private normalizeResponse;
517
+ }
518
+
519
+ interface AnthropicConfig {
520
+ apiKey?: string;
521
+ }
522
+ declare class AnthropicProvider implements ModelProvider {
523
+ readonly providerId = "anthropic";
524
+ readonly modelId: string;
525
+ private client;
526
+ private AnthropicCtor;
527
+ private clientCache;
528
+ constructor(modelId: string, config?: AnthropicConfig);
529
+ private getClient;
530
+ generate(messages: ChatMessage[], options?: ModelConfig & {
531
+ tools?: ToolDefinition[];
532
+ }): Promise<ModelResponse>;
533
+ stream(messages: ChatMessage[], options?: ModelConfig & {
534
+ tools?: ToolDefinition[];
535
+ }): AsyncGenerator<StreamChunk>;
536
+ private toAnthropicMessages;
537
+ private partToAnthropic;
538
+ private toAnthropicTools;
539
+ private normalizeResponse;
540
+ }
541
+
542
+ interface GoogleConfig {
543
+ apiKey?: string;
544
+ }
545
+ declare class GoogleProvider implements ModelProvider {
546
+ readonly providerId = "google";
547
+ readonly modelId: string;
548
+ private ai;
549
+ private GoogleGenAICtor;
550
+ private clientCache;
551
+ constructor(modelId: string, config?: GoogleConfig);
552
+ private getClient;
553
+ generate(messages: ChatMessage[], options?: ModelConfig & {
554
+ tools?: ToolDefinition[];
555
+ }): Promise<ModelResponse>;
556
+ stream(messages: ChatMessage[], options?: ModelConfig & {
557
+ tools?: ToolDefinition[];
558
+ }): AsyncGenerator<StreamChunk>;
559
+ private toGoogleMessages;
560
+ private partToGoogle;
561
+ private toGoogleTools;
562
+ private cleanJsonSchema;
563
+ private normalizeResponse;
564
+ }
565
+
566
+ interface OllamaConfig {
567
+ host?: string;
568
+ }
569
+ declare class OllamaProvider implements ModelProvider {
570
+ readonly providerId = "ollama";
571
+ readonly modelId: string;
572
+ private client;
573
+ constructor(modelId: string, config?: OllamaConfig);
574
+ generate(messages: ChatMessage[], options?: ModelConfig & {
575
+ tools?: ToolDefinition[];
576
+ }): Promise<ModelResponse>;
577
+ stream(messages: ChatMessage[], options?: ModelConfig & {
578
+ tools?: ToolDefinition[];
579
+ }): AsyncGenerator<StreamChunk>;
580
+ private toOllamaMessages;
581
+ private toOllamaTools;
582
+ private normalizeResponse;
583
+ }
584
+
585
+ declare function defineTool<T extends z.ZodObject<any>>(config: {
586
+ name: string;
587
+ description: string;
588
+ parameters: T;
589
+ execute: (args: z.infer<T>, ctx: RunContext) => Promise<string | ToolResult>;
590
+ }): ToolDef;
591
+
592
+ declare class InMemoryStorage implements StorageDriver {
593
+ private store;
594
+ private makeKey;
595
+ get<T>(namespace: string, key: string): Promise<T | null>;
596
+ set<T>(namespace: string, key: string, value: T): Promise<void>;
597
+ delete(namespace: string, key: string): Promise<void>;
598
+ list<T>(namespace: string, prefix?: string): Promise<Array<{
599
+ key: string;
600
+ value: T;
601
+ }>>;
602
+ close(): Promise<void>;
603
+ }
604
+
605
+ declare class SqliteStorage implements StorageDriver {
606
+ private db;
607
+ constructor(dbPath: string);
608
+ get<T>(namespace: string, key: string): Promise<T | null>;
609
+ set<T>(namespace: string, key: string, value: T): Promise<void>;
610
+ delete(namespace: string, key: string): Promise<void>;
611
+ list<T>(namespace: string, prefix?: string): Promise<Array<{
612
+ key: string;
613
+ value: T;
614
+ }>>;
615
+ close(): Promise<void>;
616
+ }
617
+
618
+ declare class PostgresStorage implements StorageDriver {
619
+ private pool;
620
+ constructor(connectionString: string);
621
+ initialize(): Promise<void>;
622
+ get<T>(namespace: string, key: string): Promise<T | null>;
623
+ set<T>(namespace: string, key: string, value: T): Promise<void>;
624
+ delete(namespace: string, key: string): Promise<void>;
625
+ list<T>(namespace: string, prefix?: string): Promise<Array<{
626
+ key: string;
627
+ value: T;
628
+ }>>;
629
+ close(): Promise<void>;
630
+ }
631
+
632
+ declare class MongoDBStorage implements StorageDriver {
633
+ private uri;
634
+ private dbName;
635
+ private collectionName;
636
+ private client;
637
+ private db;
638
+ private collection;
639
+ constructor(uri: string, dbName?: string, collectionName?: string);
640
+ initialize(): Promise<void>;
641
+ get<T>(namespace: string, key: string): Promise<T | null>;
642
+ set<T>(namespace: string, key: string, value: T): Promise<void>;
643
+ delete(namespace: string, key: string): Promise<void>;
644
+ list<T>(namespace: string, prefix?: string): Promise<Array<{
645
+ key: string;
646
+ value: T;
647
+ }>>;
648
+ close(): Promise<void>;
649
+ }
650
+
651
+ interface VectorDocument {
652
+ id: string;
653
+ content: string;
654
+ embedding?: number[];
655
+ metadata?: Record<string, unknown>;
656
+ }
657
+ interface VectorSearchResult {
658
+ id: string;
659
+ content: string;
660
+ score: number;
661
+ metadata?: Record<string, unknown>;
662
+ }
663
+ interface VectorSearchOptions {
664
+ topK?: number;
665
+ filter?: Record<string, unknown>;
666
+ minScore?: number;
667
+ }
668
+ interface EmbeddingProvider {
669
+ readonly dimensions: number;
670
+ embed(text: string): Promise<number[]>;
671
+ embedBatch(texts: string[]): Promise<number[][]>;
672
+ }
673
+ interface VectorStore {
674
+ /** Initialize collections/indexes. Call once before use. */
675
+ initialize(): Promise<void>;
676
+ /** Upsert a single document (embedding computed if not provided). */
677
+ upsert(collection: string, doc: VectorDocument): Promise<void>;
678
+ /** Upsert multiple documents in batch. */
679
+ upsertBatch(collection: string, docs: VectorDocument[]): Promise<void>;
680
+ /** Similarity search by vector or text query. */
681
+ search(collection: string, query: number[] | string, options?: VectorSearchOptions): Promise<VectorSearchResult[]>;
682
+ /** Delete a document by ID. */
683
+ delete(collection: string, id: string): Promise<void>;
684
+ /** Get a document by ID. */
685
+ get(collection: string, id: string): Promise<VectorDocument | null>;
686
+ /** Drop an entire collection. */
687
+ dropCollection(collection: string): Promise<void>;
688
+ /** Close connections. */
689
+ close(): Promise<void>;
690
+ }
691
+
692
+ declare abstract class BaseVectorStore implements VectorStore {
693
+ protected embedder?: EmbeddingProvider | undefined;
694
+ constructor(embedder?: EmbeddingProvider | undefined);
695
+ protected ensureEmbedding(doc: VectorDocument): Promise<number[]>;
696
+ protected ensureQueryVector(query: number[] | string): Promise<number[]>;
697
+ abstract initialize(): Promise<void>;
698
+ abstract upsert(collection: string, doc: VectorDocument): Promise<void>;
699
+ abstract upsertBatch(collection: string, docs: VectorDocument[]): Promise<void>;
700
+ abstract search(collection: string, query: number[] | string, options?: VectorSearchOptions): Promise<VectorSearchResult[]>;
701
+ abstract delete(collection: string, id: string): Promise<void>;
702
+ abstract get(collection: string, id: string): Promise<VectorDocument | null>;
703
+ abstract dropCollection(collection: string): Promise<void>;
704
+ abstract close(): Promise<void>;
705
+ }
706
+
707
+ declare class InMemoryVectorStore extends BaseVectorStore {
708
+ private collections;
709
+ constructor(embedder?: EmbeddingProvider);
710
+ initialize(): Promise<void>;
711
+ private getCol;
712
+ upsert(collection: string, doc: VectorDocument): Promise<void>;
713
+ upsertBatch(collection: string, docs: VectorDocument[]): Promise<void>;
714
+ search(collection: string, query: number[] | string, options?: VectorSearchOptions): Promise<VectorSearchResult[]>;
715
+ private cosineSimilarity;
716
+ delete(collection: string, id: string): Promise<void>;
717
+ get(collection: string, id: string): Promise<VectorDocument | null>;
718
+ dropCollection(collection: string): Promise<void>;
719
+ close(): Promise<void>;
720
+ }
721
+
722
+ interface PgVectorConfig {
723
+ connectionString: string;
724
+ dimensions?: number;
725
+ }
726
+ declare class PgVectorStore extends BaseVectorStore {
727
+ private pool;
728
+ private dimensions;
729
+ private initializedCollections;
730
+ constructor(config: PgVectorConfig, embedder?: EmbeddingProvider);
731
+ initialize(): Promise<void>;
732
+ private ensureCollection;
733
+ private sanitize;
734
+ private toSql;
735
+ upsert(collection: string, doc: VectorDocument): Promise<void>;
736
+ upsertBatch(collection: string, docs: VectorDocument[]): Promise<void>;
737
+ search(collection: string, query: number[] | string, options?: VectorSearchOptions): Promise<VectorSearchResult[]>;
738
+ delete(collection: string, id: string): Promise<void>;
739
+ get(collection: string, id: string): Promise<VectorDocument | null>;
740
+ dropCollection(collection: string): Promise<void>;
741
+ close(): Promise<void>;
742
+ }
743
+
744
+ interface QdrantConfig {
745
+ url?: string;
746
+ apiKey?: string;
747
+ dimensions?: number;
748
+ checkCompatibility?: boolean;
749
+ }
750
+ declare class QdrantVectorStore extends BaseVectorStore {
751
+ private client;
752
+ private dimensions;
753
+ private initializedCollections;
754
+ constructor(config?: QdrantConfig, embedder?: EmbeddingProvider);
755
+ initialize(): Promise<void>;
756
+ private ensureCollection;
757
+ upsert(collection: string, doc: VectorDocument): Promise<void>;
758
+ upsertBatch(collection: string, docs: VectorDocument[]): Promise<void>;
759
+ search(collection: string, query: number[] | string, options?: VectorSearchOptions): Promise<VectorSearchResult[]>;
760
+ delete(collection: string, id: string): Promise<void>;
761
+ get(collection: string, id: string): Promise<VectorDocument | null>;
762
+ dropCollection(collection: string): Promise<void>;
763
+ close(): Promise<void>;
764
+ }
765
+
766
+ interface MongoDBVectorConfig {
767
+ uri: string;
768
+ dbName?: string;
769
+ /** Atlas Search index name (must be pre-created for $vectorSearch). Defaults to "vector_index". */
770
+ indexName?: string;
771
+ }
772
+ declare class MongoDBVectorStore extends BaseVectorStore {
773
+ private client;
774
+ private db;
775
+ private indexName;
776
+ private dbName;
777
+ private useAtlas;
778
+ constructor(config: MongoDBVectorConfig, embedder?: EmbeddingProvider);
779
+ initialize(): Promise<void>;
780
+ private col;
781
+ upsert(collection: string, doc: VectorDocument): Promise<void>;
782
+ upsertBatch(collection: string, docs: VectorDocument[]): Promise<void>;
783
+ search(collection: string, query: number[] | string, options?: VectorSearchOptions): Promise<VectorSearchResult[]>;
784
+ private atlasSearch;
785
+ private localSearch;
786
+ private buildFilter;
787
+ delete(collection: string, id: string): Promise<void>;
788
+ get(collection: string, id: string): Promise<VectorDocument | null>;
789
+ dropCollection(collection: string): Promise<void>;
790
+ close(): Promise<void>;
791
+ }
792
+
793
+ interface OpenAIEmbeddingConfig {
794
+ apiKey?: string;
795
+ baseURL?: string;
796
+ model?: string;
797
+ dimensions?: number;
798
+ }
799
+ declare class OpenAIEmbedding implements EmbeddingProvider {
800
+ readonly dimensions: number;
801
+ private client;
802
+ private model;
803
+ constructor(config?: OpenAIEmbeddingConfig);
804
+ embed(text: string): Promise<number[]>;
805
+ embedBatch(texts: string[]): Promise<number[][]>;
806
+ }
807
+
808
+ interface GoogleEmbeddingConfig {
809
+ apiKey?: string;
810
+ model?: string;
811
+ dimensions?: number;
812
+ }
813
+ declare class GoogleEmbedding implements EmbeddingProvider {
814
+ readonly dimensions: number;
815
+ private ai;
816
+ private model;
817
+ constructor(config?: GoogleEmbeddingConfig);
818
+ embed(text: string): Promise<number[]>;
819
+ embedBatch(texts: string[]): Promise<number[][]>;
820
+ }
821
+
822
+ interface KnowledgeBaseConfig {
823
+ /** Display name used in tool description auto-generation. */
824
+ name: string;
825
+ /** The underlying vector store (any backend). */
826
+ vectorStore: VectorStore;
827
+ /** Collection/index name inside the vector store. */
828
+ collection?: string;
829
+ }
830
+ interface KnowledgeBaseToolConfig {
831
+ /** Tool name exposed to the LLM. Defaults to `search_<collection>`. */
832
+ toolName?: string;
833
+ /** Custom tool description. A sensible default is generated from the KB name. */
834
+ description?: string;
835
+ /** Number of results to return per search. Default 5. */
836
+ topK?: number;
837
+ /** Minimum similarity score to include. */
838
+ minScore?: number;
839
+ /** Metadata filter applied to every search. */
840
+ filter?: Record<string, unknown>;
841
+ /** Custom formatter for search results. Defaults to numbered list with scores. */
842
+ formatResults?: (results: VectorSearchResult[]) => string;
843
+ }
844
+ declare class KnowledgeBase {
845
+ readonly name: string;
846
+ readonly collection: string;
847
+ private store;
848
+ private initialized;
849
+ constructor(config: KnowledgeBaseConfig);
850
+ initialize(): Promise<void>;
851
+ add(doc: VectorDocument): Promise<void>;
852
+ addDocuments(docs: VectorDocument[]): Promise<void>;
853
+ search(query: string, options?: VectorSearchOptions): Promise<VectorSearchResult[]>;
854
+ get(id: string): Promise<VectorDocument | null>;
855
+ delete(id: string): Promise<void>;
856
+ clear(): Promise<void>;
857
+ close(): Promise<void>;
858
+ /**
859
+ * Returns a ToolDef that an Agent can use to search this knowledge base.
860
+ * Plug the result directly into `Agent({ tools: [kb.asTool()] })`.
861
+ */
862
+ asTool(config?: KnowledgeBaseToolConfig): ToolDef;
863
+ private ensureInit;
864
+ }
865
+
866
+ interface Session {
867
+ sessionId: string;
868
+ userId?: string;
869
+ messages: ChatMessage[];
870
+ state: Record<string, unknown>;
871
+ createdAt: Date;
872
+ updatedAt: Date;
873
+ }
874
+
875
+ declare class SessionManager {
876
+ private storage;
877
+ constructor(storage: StorageDriver);
878
+ getOrCreate(sessionId: string, userId?: string): Promise<Session>;
879
+ appendMessage(sessionId: string, msg: ChatMessage): Promise<void>;
880
+ appendMessages(sessionId: string, msgs: ChatMessage[]): Promise<void>;
881
+ getHistory(sessionId: string, limit?: number): Promise<ChatMessage[]>;
882
+ updateState(sessionId: string, patch: Record<string, unknown>): Promise<void>;
883
+ getState(sessionId: string): Promise<Record<string, unknown>>;
884
+ deleteSession(sessionId: string): Promise<void>;
885
+ }
886
+
887
+ export { Agent, type AgentConfig, type AgentEventMap, type AgentHooks, type AgentStep, AnthropicProvider, type Artifact, type AudioPart, BaseVectorStore, type ChatMessage, type ConditionStep, type ContentPart, type EmbeddingProvider, EventBus, type FilePart, type FunctionStep, GoogleEmbedding, type GoogleEmbeddingConfig, GoogleProvider, type GuardrailResult, type ImagePart, InMemoryStorage, InMemoryVectorStore, type InputGuardrail, KnowledgeBase, type KnowledgeBaseConfig, type KnowledgeBaseToolConfig, LLMLoop, type LogLevel, Logger, type LoggerConfig, Memory, type MemoryConfig, type MemoryEntry, type MessageContent, type MessageRole, type ModelConfig, type ModelProvider, ModelRegistry, type ModelResponse, MongoDBStorage, type MongoDBVectorConfig, MongoDBVectorStore, OllamaProvider, OpenAIEmbedding, type OpenAIEmbeddingConfig, OpenAIProvider, type OutputGuardrail, type ParallelStep, type PgVectorConfig, PgVectorStore, PostgresStorage, type QdrantConfig, QdrantVectorStore, RunContext, type RunOpts, type RunOutput, type Session, SessionManager, SqliteStorage, type StepDef, type StepResult, type StorageDriver, type StreamChunk, Team, type TeamConfig, TeamMode, type TextPart, type TokenUsage, type ToolCall, type ToolCallResult, type ToolDef, type ToolDefinition, ToolExecutor, type ToolResult, type VectorDocument, type VectorSearchOptions, type VectorSearchResult, type VectorStore, Workflow, type WorkflowConfig, type WorkflowResult, anthropic, defineTool, getTextContent, google, isMultiModal, ollama, openai, registry };