@cloudbase/agent-agents 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +543 -0
- package/dist/abstract/index.d.mts +11 -0
- package/dist/abstract/index.d.ts +11 -0
- package/dist/abstract/index.js +37 -0
- package/dist/abstract/index.js.map +1 -0
- package/dist/abstract/index.mjs +16 -0
- package/dist/abstract/index.mjs.map +1 -0
- package/dist/chunk-NYRGWMQB.mjs +36 -0
- package/dist/chunk-NYRGWMQB.mjs.map +1 -0
- package/dist/index.d.mts +4031 -0
- package/dist/index.d.ts +4031 -0
- package/dist/index.js +10085 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +9960 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +73 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,4031 @@
|
|
|
1
|
+
import { z } from 'zod/v4';
|
|
2
|
+
import * as _cloudbase_agent_tools from '@cloudbase/agent-tools';
|
|
3
|
+
import { BaseTool, ToolExecutionContext } from '@cloudbase/agent-tools';
|
|
4
|
+
export { ToolExecutionContext } from '@cloudbase/agent-tools';
|
|
5
|
+
import { TiktokenEncoding } from 'tiktoken';
|
|
6
|
+
import { EventEmitter } from 'events';
|
|
7
|
+
import { AbstractAgent, AgentConfig as AgentConfig$1, RunAgentInput, BaseEvent as BaseEvent$2 } from '@ag-ui/client';
|
|
8
|
+
import { Observable } from 'rxjs';
|
|
9
|
+
import { Db, MongoClientOptions } from 'mongodb';
|
|
10
|
+
import { MysqlConnectionOptions } from 'typeorm/driver/mysql/MysqlConnectionOptions.js';
|
|
11
|
+
export { MysqlConnectionOptions } from 'typeorm/driver/mysql/MysqlConnectionOptions.js';
|
|
12
|
+
import { DataSource, Repository } from 'typeorm';
|
|
13
|
+
import { MemoryClient as MemoryClient$1 } from 'mem0ai';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Enhanced error handling system for AG-Kit
|
|
17
|
+
*/
|
|
18
|
+
declare abstract class AGKitError extends Error {
|
|
19
|
+
abstract readonly code: string;
|
|
20
|
+
abstract readonly category: ErrorCategory;
|
|
21
|
+
readonly timestamp: Date;
|
|
22
|
+
readonly context?: Record<string, unknown>;
|
|
23
|
+
readonly cause?: Error;
|
|
24
|
+
constructor(message: string, context?: Record<string, unknown>, cause?: Error);
|
|
25
|
+
toJSON(): {
|
|
26
|
+
name: string;
|
|
27
|
+
code: string;
|
|
28
|
+
category: ErrorCategory;
|
|
29
|
+
message: string;
|
|
30
|
+
timestamp: string;
|
|
31
|
+
context: Record<string, unknown> | undefined;
|
|
32
|
+
stack: string | undefined;
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
declare enum ErrorCategory {
|
|
36
|
+
CONFIGURATION = "CONFIGURATION",
|
|
37
|
+
NETWORK = "NETWORK",
|
|
38
|
+
MODEL = "MODEL",
|
|
39
|
+
TOOL = "TOOL",
|
|
40
|
+
VALIDATION = "VALIDATION",
|
|
41
|
+
RUNTIME = "RUNTIME",
|
|
42
|
+
MEMORY = "MEMORY",
|
|
43
|
+
CONTROL_FLOW = "CONTROL_FLOW"
|
|
44
|
+
}
|
|
45
|
+
declare class ConfigurationError extends AGKitError {
|
|
46
|
+
readonly code = "CONFIG_ERROR";
|
|
47
|
+
readonly category = ErrorCategory.CONFIGURATION;
|
|
48
|
+
}
|
|
49
|
+
declare class InvalidModelProviderError extends AGKitError {
|
|
50
|
+
readonly code = "INVALID_MODEL_PROVIDER";
|
|
51
|
+
readonly category = ErrorCategory.CONFIGURATION;
|
|
52
|
+
}
|
|
53
|
+
declare class MissingRequiredConfigError extends AGKitError {
|
|
54
|
+
readonly code = "MISSING_REQUIRED_CONFIG";
|
|
55
|
+
readonly category = ErrorCategory.CONFIGURATION;
|
|
56
|
+
}
|
|
57
|
+
declare class NetworkError extends AGKitError {
|
|
58
|
+
readonly code = "NETWORK_ERROR";
|
|
59
|
+
readonly category = ErrorCategory.NETWORK;
|
|
60
|
+
}
|
|
61
|
+
declare class TimeoutError extends AGKitError {
|
|
62
|
+
readonly code = "TIMEOUT_ERROR";
|
|
63
|
+
readonly category = ErrorCategory.NETWORK;
|
|
64
|
+
}
|
|
65
|
+
declare class RateLimitError extends AGKitError {
|
|
66
|
+
readonly code = "RATE_LIMIT_ERROR";
|
|
67
|
+
readonly category = ErrorCategory.NETWORK;
|
|
68
|
+
}
|
|
69
|
+
declare class ModelError extends AGKitError {
|
|
70
|
+
readonly code = "MODEL_ERROR";
|
|
71
|
+
readonly category = ErrorCategory.MODEL;
|
|
72
|
+
}
|
|
73
|
+
declare class ModelProviderError$1 extends AGKitError {
|
|
74
|
+
readonly code = "MODEL_PROVIDER_ERROR";
|
|
75
|
+
readonly category = ErrorCategory.MODEL;
|
|
76
|
+
}
|
|
77
|
+
declare class TokenLimitError extends AGKitError {
|
|
78
|
+
readonly code = "TOKEN_LIMIT_ERROR";
|
|
79
|
+
readonly category = ErrorCategory.MODEL;
|
|
80
|
+
}
|
|
81
|
+
declare class InvalidModelResponseError extends AGKitError {
|
|
82
|
+
readonly code = "INVALID_MODEL_RESPONSE";
|
|
83
|
+
readonly category = ErrorCategory.MODEL;
|
|
84
|
+
}
|
|
85
|
+
declare class ToolError extends AGKitError {
|
|
86
|
+
readonly code = "TOOL_ERROR";
|
|
87
|
+
readonly category = ErrorCategory.TOOL;
|
|
88
|
+
}
|
|
89
|
+
declare class ToolExecutionError extends AGKitError {
|
|
90
|
+
readonly code = "TOOL_EXECUTION_ERROR";
|
|
91
|
+
readonly category = ErrorCategory.TOOL;
|
|
92
|
+
}
|
|
93
|
+
declare class ToolNotFoundError extends AGKitError {
|
|
94
|
+
readonly code = "TOOL_NOT_FOUND";
|
|
95
|
+
readonly category = ErrorCategory.TOOL;
|
|
96
|
+
}
|
|
97
|
+
declare class ToolValidationError extends AGKitError {
|
|
98
|
+
readonly code = "TOOL_VALIDATION_ERROR";
|
|
99
|
+
readonly category = ErrorCategory.TOOL;
|
|
100
|
+
}
|
|
101
|
+
declare class ValidationError extends AGKitError {
|
|
102
|
+
readonly code = "VALIDATION_ERROR";
|
|
103
|
+
readonly category = ErrorCategory.VALIDATION;
|
|
104
|
+
}
|
|
105
|
+
declare class SchemaValidationError extends AGKitError {
|
|
106
|
+
readonly code = "SCHEMA_VALIDATION_ERROR";
|
|
107
|
+
readonly category = ErrorCategory.VALIDATION;
|
|
108
|
+
}
|
|
109
|
+
declare class RuntimeError extends AGKitError {
|
|
110
|
+
readonly code = "RUNTIME_ERROR";
|
|
111
|
+
readonly category = ErrorCategory.RUNTIME;
|
|
112
|
+
}
|
|
113
|
+
declare class StateError extends AGKitError {
|
|
114
|
+
readonly code = "STATE_ERROR";
|
|
115
|
+
readonly category = ErrorCategory.RUNTIME;
|
|
116
|
+
}
|
|
117
|
+
declare class ExecutionError extends AGKitError {
|
|
118
|
+
readonly code = "EXECUTION_ERROR";
|
|
119
|
+
readonly category = ErrorCategory.RUNTIME;
|
|
120
|
+
}
|
|
121
|
+
declare class MemoryError extends AGKitError {
|
|
122
|
+
readonly code = "MEMORY_ERROR";
|
|
123
|
+
readonly category = ErrorCategory.MEMORY;
|
|
124
|
+
}
|
|
125
|
+
declare class MemoryLimitError extends AGKitError {
|
|
126
|
+
readonly code = "MEMORY_LIMIT_ERROR";
|
|
127
|
+
readonly category = ErrorCategory.MEMORY;
|
|
128
|
+
}
|
|
129
|
+
declare class ControlFlowError extends AGKitError {
|
|
130
|
+
readonly code = "CONTROL_FLOW_ERROR";
|
|
131
|
+
readonly category = ErrorCategory.CONTROL_FLOW;
|
|
132
|
+
}
|
|
133
|
+
declare class HumanApprovalRequiredError extends AGKitError {
|
|
134
|
+
readonly code = "HUMAN_APPROVAL_REQUIRED";
|
|
135
|
+
readonly category = ErrorCategory.CONTROL_FLOW;
|
|
136
|
+
}
|
|
137
|
+
interface ErrorHandler {
|
|
138
|
+
handle(error: AGKitError): Promise<void> | void;
|
|
139
|
+
canHandle(error: AGKitError): boolean;
|
|
140
|
+
}
|
|
141
|
+
declare class LoggingErrorHandler implements ErrorHandler {
|
|
142
|
+
canHandle(error: AGKitError): boolean;
|
|
143
|
+
handle(error: AGKitError): void;
|
|
144
|
+
}
|
|
145
|
+
declare class RetryErrorHandler implements ErrorHandler {
|
|
146
|
+
private maxRetries;
|
|
147
|
+
private retryableCategories;
|
|
148
|
+
constructor(maxRetries?: number, retryableCategories?: ErrorCategory[]);
|
|
149
|
+
canHandle(error: AGKitError): boolean;
|
|
150
|
+
handle(error: AGKitError): Promise<void>;
|
|
151
|
+
}
|
|
152
|
+
interface ErrorRecoveryStrategy {
|
|
153
|
+
canRecover(error: AGKitError): boolean;
|
|
154
|
+
recover(error: AGKitError): Promise<unknown>;
|
|
155
|
+
}
|
|
156
|
+
declare class FallbackModelStrategy implements ErrorRecoveryStrategy {
|
|
157
|
+
canRecover(error: AGKitError): boolean;
|
|
158
|
+
recover(error: AGKitError): Promise<unknown>;
|
|
159
|
+
}
|
|
160
|
+
declare class ErrorContextBuilder {
|
|
161
|
+
private context;
|
|
162
|
+
withAgentId(agentId: string): this;
|
|
163
|
+
withRunId(runId: string): this;
|
|
164
|
+
withToolName(toolName: string): this;
|
|
165
|
+
withModelName(modelName: string): this;
|
|
166
|
+
withRetryCount(count: number): this;
|
|
167
|
+
withCustom(key: string, value: unknown): this;
|
|
168
|
+
build(): Record<string, unknown>;
|
|
169
|
+
}
|
|
170
|
+
declare function isRetryableError(error: AGKitError): boolean;
|
|
171
|
+
declare function isConfigurationError(error: AGKitError): boolean;
|
|
172
|
+
declare function isToolError(error: AGKitError): boolean;
|
|
173
|
+
declare function createErrorContext(): ErrorContextBuilder;
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* @fileoverview Token utilities for text processing and token management
|
|
177
|
+
*/
|
|
178
|
+
/**
|
|
179
|
+
* Tokenizer interface for encoding, decoding, and counting tokens
|
|
180
|
+
*/
|
|
181
|
+
interface ITokenizer {
|
|
182
|
+
/** Encode text into tokens */
|
|
183
|
+
encode(text: string): Uint32Array;
|
|
184
|
+
/** Decode tokens back to text */
|
|
185
|
+
decode(tokens: Uint32Array): string;
|
|
186
|
+
/** Count the number of tokens in text */
|
|
187
|
+
countTokens(text: string): number;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Default tokenizer implementation based on tiktoken
|
|
191
|
+
* Provides accurate token counting for OpenAI models
|
|
192
|
+
*/
|
|
193
|
+
declare class TiktokenTokenizer implements ITokenizer {
|
|
194
|
+
/** Tiktoken encoding instance for token operations */
|
|
195
|
+
private encoding;
|
|
196
|
+
/**
|
|
197
|
+
* Creates a new TiktokenTokenizer instance
|
|
198
|
+
* @param encodingName - Tiktoken encoding name, defaults to 'o200k_base'
|
|
199
|
+
*/
|
|
200
|
+
constructor(encodingName?: TiktokenEncoding);
|
|
201
|
+
/**
|
|
202
|
+
* Encode text into token array
|
|
203
|
+
* @param text - Text to encode
|
|
204
|
+
* @returns Array of token IDs
|
|
205
|
+
*/
|
|
206
|
+
encode(text: string): Uint32Array;
|
|
207
|
+
/**
|
|
208
|
+
* Decode tokens back to text
|
|
209
|
+
* @param tokens - Token array to decode
|
|
210
|
+
* @returns Decoded text string
|
|
211
|
+
*/
|
|
212
|
+
decode(tokens: Uint32Array): string;
|
|
213
|
+
/**
|
|
214
|
+
* Count the number of tokens in text
|
|
215
|
+
* @param text - Text to count tokens for
|
|
216
|
+
* @returns Number of tokens
|
|
217
|
+
*/
|
|
218
|
+
countTokens(text: string): number;
|
|
219
|
+
/**
|
|
220
|
+
* Free the encoding resources
|
|
221
|
+
*/
|
|
222
|
+
free(): void;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Token trimming utility class for managing message token limits
|
|
226
|
+
* Similar to LangChain's trimMessage functionality
|
|
227
|
+
*/
|
|
228
|
+
declare class TokenTrimmer {
|
|
229
|
+
/** Tokenizer instance for token counting */
|
|
230
|
+
private tokenizer;
|
|
231
|
+
/**
|
|
232
|
+
* Creates a new TokenTrimmer instance
|
|
233
|
+
* @param tokenizer - Optional tokenizer, defaults to TiktokenTokenizer
|
|
234
|
+
*/
|
|
235
|
+
constructor(tokenizer?: ITokenizer);
|
|
236
|
+
/**
|
|
237
|
+
* Calculate the token count for a message including role and content
|
|
238
|
+
* @param message - Message object with role and content
|
|
239
|
+
* @returns Total token count including formatting overhead
|
|
240
|
+
*/
|
|
241
|
+
countMessageTokens(message: {
|
|
242
|
+
role: string;
|
|
243
|
+
content: string;
|
|
244
|
+
}): number;
|
|
245
|
+
/**
|
|
246
|
+
* Trim message list to fit within token limits
|
|
247
|
+
* Preserves messages starting from newest or oldest based on strategy
|
|
248
|
+
* @param events - Array of events with message objects
|
|
249
|
+
* @param maxTokens - Maximum token limit
|
|
250
|
+
* @param strategy - Trimming strategy: 'newest_first' or 'oldest_first'
|
|
251
|
+
* @returns Trimmed array of events within token limit
|
|
252
|
+
*/
|
|
253
|
+
trimMessages<T extends {
|
|
254
|
+
message: {
|
|
255
|
+
role: string;
|
|
256
|
+
content: string;
|
|
257
|
+
};
|
|
258
|
+
}>(events: T[], maxTokens: number, strategy?: "newest_first" | "oldest_first"): T[];
|
|
259
|
+
/**
|
|
260
|
+
* Truncate a single message to fit within token limits
|
|
261
|
+
* @param event - Event to truncate
|
|
262
|
+
* @param maxTokens - Maximum token limit
|
|
263
|
+
* @returns Truncated event or null if cannot fit
|
|
264
|
+
*/
|
|
265
|
+
private truncateMessage;
|
|
266
|
+
/**
|
|
267
|
+
* Free tokenizer resources
|
|
268
|
+
*/
|
|
269
|
+
free(): void;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* @fileoverview Base memory interfaces and abstract class for short-term memory management
|
|
274
|
+
*/
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Message interface representing a conversation message
|
|
278
|
+
*/
|
|
279
|
+
interface Message$2 {
|
|
280
|
+
/** Unique identifier for the message */
|
|
281
|
+
id: string;
|
|
282
|
+
/** Role of the message sender */
|
|
283
|
+
role: "user" | "assistant" | "system" | "tool";
|
|
284
|
+
/** Content of the message */
|
|
285
|
+
content: string;
|
|
286
|
+
/** Optional timestamp when the message was created */
|
|
287
|
+
timestamp?: Date;
|
|
288
|
+
/** Optional tool calls associated with the message */
|
|
289
|
+
toolCalls?: any[];
|
|
290
|
+
/** Optional tool call identifier */
|
|
291
|
+
toolCallId?: string;
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Memory event interface combining message and state information
|
|
295
|
+
*/
|
|
296
|
+
interface IMemoryEvent {
|
|
297
|
+
/** The message content */
|
|
298
|
+
message: Message$2;
|
|
299
|
+
/** Additional state information */
|
|
300
|
+
state: Record<string, any>;
|
|
301
|
+
}
|
|
302
|
+
type TInputMemoryEvent = Omit<IMemoryEvent, "message"> & {
|
|
303
|
+
message: Omit<Message$2, "id"> & {
|
|
304
|
+
id?: Message$2["id"];
|
|
305
|
+
};
|
|
306
|
+
};
|
|
307
|
+
/**
|
|
308
|
+
* Options for listing and querying memory events
|
|
309
|
+
*/
|
|
310
|
+
interface ListOptions {
|
|
311
|
+
/** Maximum number of tokens to include in results */
|
|
312
|
+
maxTokens?: number;
|
|
313
|
+
/** Maximum number of events to return */
|
|
314
|
+
limit?: number;
|
|
315
|
+
/** Number of events to skip (for pagination) */
|
|
316
|
+
offset?: number;
|
|
317
|
+
/** Sort order by timestamp */
|
|
318
|
+
order?: "asc" | "desc";
|
|
319
|
+
/** Session identifier for multi-session support */
|
|
320
|
+
sessionId?: string;
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* Options for adding memory events
|
|
324
|
+
*/
|
|
325
|
+
interface AddOptions {
|
|
326
|
+
/** Session identifier for multi-session support */
|
|
327
|
+
sessionId?: string;
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Options for deleting memory events
|
|
331
|
+
*/
|
|
332
|
+
interface DeleteOptions {
|
|
333
|
+
/** Session identifier for multi-session support */
|
|
334
|
+
sessionId?: string;
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Options for retrieving memory events
|
|
338
|
+
*/
|
|
339
|
+
interface RetrieveOptions {
|
|
340
|
+
/** Session identifier for multi-session support */
|
|
341
|
+
sessionId?: string;
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* Options for clearing memory events
|
|
345
|
+
*/
|
|
346
|
+
interface ClearOptions {
|
|
347
|
+
/** Session identifier for multi-session support. If not provided, clears all sessions */
|
|
348
|
+
sessionId?: string;
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Branch information for session management
|
|
352
|
+
*/
|
|
353
|
+
interface BranchInfo {
|
|
354
|
+
/** Branch name */
|
|
355
|
+
name: string;
|
|
356
|
+
/** When the branch was created */
|
|
357
|
+
createdAt: Date;
|
|
358
|
+
/** Event ID this branch was created from */
|
|
359
|
+
fromEventId?: string;
|
|
360
|
+
/** Whether this is the currently active branch */
|
|
361
|
+
isActive: boolean;
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* Compaction metadata stored in event.state.__compaction__
|
|
365
|
+
*/
|
|
366
|
+
interface CompactionMetadata {
|
|
367
|
+
/** Original full message content before compaction */
|
|
368
|
+
originalContent: string;
|
|
369
|
+
/** Original token count */
|
|
370
|
+
originalTokens: number;
|
|
371
|
+
/** Compacted token count */
|
|
372
|
+
compactedTokens: number;
|
|
373
|
+
}
|
|
374
|
+
/**
|
|
375
|
+
* Structured summary schema (not free-form text)
|
|
376
|
+
*/
|
|
377
|
+
interface StructuredSummary {
|
|
378
|
+
content: string;
|
|
379
|
+
count: number;
|
|
380
|
+
timeRange: {
|
|
381
|
+
start?: Date;
|
|
382
|
+
end?: Date;
|
|
383
|
+
};
|
|
384
|
+
timestamp: Date;
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* Context thresholds configuration
|
|
388
|
+
*/
|
|
389
|
+
interface ContextThresholds {
|
|
390
|
+
/** Pre-rot threshold - where performance starts degrading */
|
|
391
|
+
preRotThreshold: number;
|
|
392
|
+
/** Trigger compaction at this percentage of pre-rot threshold */
|
|
393
|
+
compactionTrigger: number;
|
|
394
|
+
/** Trigger summarization at this percentage of pre-rot threshold */
|
|
395
|
+
summarizationTrigger: number;
|
|
396
|
+
/** Number of recent events to keep */
|
|
397
|
+
recentToKeep: number;
|
|
398
|
+
}
|
|
399
|
+
interface IBaseMemoryOptions {
|
|
400
|
+
tokenizer?: ITokenizer;
|
|
401
|
+
thresholds?: Partial<ContextThresholds>;
|
|
402
|
+
summarizer?: (events: IMemoryEvent[]) => Promise<StructuredSummary>;
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* Abstract base class for short-term memory implementations
|
|
406
|
+
*
|
|
407
|
+
* Provides the core interface for storing, retrieving, and managing
|
|
408
|
+
* conversation events in short-term memory systems with built-in
|
|
409
|
+
* context engineering capabilities.
|
|
410
|
+
*/
|
|
411
|
+
declare abstract class BaseMemory {
|
|
412
|
+
tokenTrimmer: TokenTrimmer;
|
|
413
|
+
protected thresholds?: ContextThresholds;
|
|
414
|
+
protected compactionCount: number;
|
|
415
|
+
protected summarizationCount: number;
|
|
416
|
+
protected lastCompactionGain: number;
|
|
417
|
+
constructor(config?: IBaseMemoryOptions);
|
|
418
|
+
/**
|
|
419
|
+
* Returns a list of events stored in the memory
|
|
420
|
+
* @param options - Optional filtering and pagination options
|
|
421
|
+
* @returns Promise resolving to array of memory events
|
|
422
|
+
*/
|
|
423
|
+
abstract list(options?: ListOptions): Promise<IMemoryEvent[]>;
|
|
424
|
+
/**
|
|
425
|
+
* Add a single memory event to storage
|
|
426
|
+
* @param event - The memory event to store
|
|
427
|
+
* @param options - Optional configuration including session ID
|
|
428
|
+
* @returns Promise that resolves when the event is stored
|
|
429
|
+
*/
|
|
430
|
+
abstract add(event: TInputMemoryEvent, options?: AddOptions): Promise<void>;
|
|
431
|
+
/**
|
|
432
|
+
* Add multiple memory events efficiently
|
|
433
|
+
*
|
|
434
|
+
* Implementations should override this method to handle bulk addition
|
|
435
|
+
* efficiently and avoid unnecessary round-trips to the underlying store.
|
|
436
|
+
*
|
|
437
|
+
* @param list - Array of memory events to store
|
|
438
|
+
* @returns Promise that resolves when all events are stored
|
|
439
|
+
*/
|
|
440
|
+
addList(list: TInputMemoryEvent[], options?: AddOptions): Promise<void>;
|
|
441
|
+
abstract update(params: {
|
|
442
|
+
sessionId?: string;
|
|
443
|
+
event: Partial<IMemoryEvent>;
|
|
444
|
+
}): Promise<void>;
|
|
445
|
+
/**
|
|
446
|
+
* Delete memory events from storage
|
|
447
|
+
* @param args - Implementation-specific arguments for deletion
|
|
448
|
+
* @returns Promise that resolves when deletion is complete
|
|
449
|
+
*/
|
|
450
|
+
abstract delete(...args: any[]): Promise<void>;
|
|
451
|
+
/**
|
|
452
|
+
* Retrieve memory events based on search criteria
|
|
453
|
+
* @param args - Implementation-specific arguments for retrieval
|
|
454
|
+
* @returns Promise resolving to array of matching memory events
|
|
455
|
+
*/
|
|
456
|
+
abstract retrieve(...args: any[]): Promise<IMemoryEvent[]>;
|
|
457
|
+
/**
|
|
458
|
+
* Remove all events from storage
|
|
459
|
+
* @returns Promise that resolves when all events are cleared
|
|
460
|
+
* @throws Error if not implemented by subclass
|
|
461
|
+
*/
|
|
462
|
+
clear(): Promise<void>;
|
|
463
|
+
/**
|
|
464
|
+
* Create a new branch from current session state
|
|
465
|
+
* @param branchName - Name for the new branch
|
|
466
|
+
* @param fromEventId - Optional event ID to branch from
|
|
467
|
+
* @returns Promise resolving to branch ID
|
|
468
|
+
* @throws Error if not implemented by subclass
|
|
469
|
+
*/
|
|
470
|
+
branch(branchName: string, fromEventId?: string): Promise<string>;
|
|
471
|
+
/**
|
|
472
|
+
* Switch to a different branch or checkout to a specific event
|
|
473
|
+
* @param target - Branch name or event ID to checkout
|
|
474
|
+
* @param options - Optional configuration
|
|
475
|
+
* @param options.type - Type of checkout: 'branch' or 'event' (default: auto-detect)
|
|
476
|
+
* @param options.sessionId - Session ID when checking out to an event
|
|
477
|
+
* @returns Promise that resolves when checkout is complete
|
|
478
|
+
* @throws Error if not implemented by subclass
|
|
479
|
+
*
|
|
480
|
+
* @example
|
|
481
|
+
* // Checkout to a branch
|
|
482
|
+
* await memory.checkout('experiment-1');
|
|
483
|
+
*
|
|
484
|
+
* // Checkout to a specific event (deletes all events after it)
|
|
485
|
+
* await memory.checkout('event-123', { type: 'event', sessionId: 'session-1' });
|
|
486
|
+
*/
|
|
487
|
+
checkout(target: string, options?: {
|
|
488
|
+
type?: "branch" | "event";
|
|
489
|
+
sessionId?: string;
|
|
490
|
+
}): Promise<void>;
|
|
491
|
+
/**
|
|
492
|
+
* List all available branches
|
|
493
|
+
* @returns Promise resolving to array of branch information
|
|
494
|
+
* @throws Error if not implemented by subclass
|
|
495
|
+
*/
|
|
496
|
+
listBranches(): Promise<BranchInfo[]>;
|
|
497
|
+
/**
|
|
498
|
+
* Delete a branch
|
|
499
|
+
* @param branchName - Name of the branch to delete
|
|
500
|
+
* @returns Promise that resolves when deletion is complete
|
|
501
|
+
* @throws Error if not implemented by subclass or trying to delete active branch
|
|
502
|
+
*/
|
|
503
|
+
deleteBranch(branchName: string): Promise<void>;
|
|
504
|
+
/**
|
|
505
|
+
* Clean up all inactive branches, keeping only the main branch
|
|
506
|
+
* This is useful for maintaining a single trunk after experimentation
|
|
507
|
+
* @param keepBranches - Optional list of branch names to keep (in addition to active branch)
|
|
508
|
+
* @returns Promise that resolves when cleanup is complete
|
|
509
|
+
* @throws Error if not implemented by subclass
|
|
510
|
+
*/
|
|
511
|
+
cleanupBranches(keepBranches?: string[]): Promise<void>;
|
|
512
|
+
/**
|
|
513
|
+
* Check if an event is compacted
|
|
514
|
+
* @param event - Event to check
|
|
515
|
+
* @returns True if event has compaction metadata
|
|
516
|
+
*/
|
|
517
|
+
isCompacted(event: IMemoryEvent): boolean;
|
|
518
|
+
/**
|
|
519
|
+
* Decompress a compacted event
|
|
520
|
+
* @param event - Event to decompress
|
|
521
|
+
* @returns Decompressed event with original content and state
|
|
522
|
+
*/
|
|
523
|
+
decompressEvent(event: IMemoryEvent): IMemoryEvent;
|
|
524
|
+
/**
|
|
525
|
+
* Decompress multiple events
|
|
526
|
+
* @param events - Events to decompress
|
|
527
|
+
* @returns Array of decompressed events
|
|
528
|
+
*/
|
|
529
|
+
decompressEvents(events: IMemoryEvent[]): IMemoryEvent[];
|
|
530
|
+
/**
|
|
531
|
+
* Manage context automatically when adding events
|
|
532
|
+
* Subclasses should call this method after adding events if context management is enabled
|
|
533
|
+
*/
|
|
534
|
+
protected manageContext({ sessionId, events, }: {
|
|
535
|
+
sessionId: string;
|
|
536
|
+
events: IMemoryEvent[];
|
|
537
|
+
}): Promise<{
|
|
538
|
+
events: IMemoryEvent[];
|
|
539
|
+
}>;
|
|
540
|
+
/**
|
|
541
|
+
* Perform compaction (reversible externalization to event.state)
|
|
542
|
+
* Subclasses should override this method to implement compaction logic
|
|
543
|
+
* @returns Token gain from compaction
|
|
544
|
+
*/
|
|
545
|
+
protected performCompaction({ sessionId, events, }: {
|
|
546
|
+
sessionId?: string;
|
|
547
|
+
events: IMemoryEvent[];
|
|
548
|
+
}): Promise<{
|
|
549
|
+
events: IMemoryEvent[];
|
|
550
|
+
gain: number;
|
|
551
|
+
}>;
|
|
552
|
+
/**
|
|
553
|
+
* Compact a single event - store full content in state
|
|
554
|
+
*/
|
|
555
|
+
protected compactEvent({ sessionId, event, }: {
|
|
556
|
+
sessionId?: string;
|
|
557
|
+
event: IMemoryEvent;
|
|
558
|
+
}): Promise<IMemoryEvent>;
|
|
559
|
+
/**
|
|
560
|
+
* Perform summarization (irreversible refinement)
|
|
561
|
+
* @param sessionId - Optional session identifier (implementation-specific)
|
|
562
|
+
*/
|
|
563
|
+
protected performSummarization({ sessionId, events, }: {
|
|
564
|
+
sessionId?: string;
|
|
565
|
+
events: IMemoryEvent[];
|
|
566
|
+
}): Promise<{
|
|
567
|
+
events: IMemoryEvent[];
|
|
568
|
+
summary?: StructuredSummary;
|
|
569
|
+
}>;
|
|
570
|
+
/**
|
|
571
|
+
* Store summary (to be implemented by subclasses)
|
|
572
|
+
* @param summary - The structured summary to store
|
|
573
|
+
*/
|
|
574
|
+
protected storeSummary(params: {
|
|
575
|
+
sessionId?: string;
|
|
576
|
+
summary: StructuredSummary;
|
|
577
|
+
}): Promise<void>;
|
|
578
|
+
/**
|
|
579
|
+
* Clear summarized events (to be implemented by subclasses)
|
|
580
|
+
* @param sessionId - Optional session identifier (implementation-specific)
|
|
581
|
+
* @param recentToKeep - Number of recent events to keep
|
|
582
|
+
*/
|
|
583
|
+
protected clearSummarizedEvents(params: {
|
|
584
|
+
sessionId?: string;
|
|
585
|
+
recentToKeep: number;
|
|
586
|
+
}): Promise<void>;
|
|
587
|
+
/**
|
|
588
|
+
* Get current token count for a session
|
|
589
|
+
*/
|
|
590
|
+
protected getCurrentTokenCount(events: IMemoryEvent[]): Promise<number>;
|
|
591
|
+
/**
|
|
592
|
+
* Create default structured summary
|
|
593
|
+
*/
|
|
594
|
+
summarizer(events: IMemoryEvent[]): Promise<StructuredSummary>;
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
/**
|
|
598
|
+
* @fileoverview In-memory implementation using unified adapter architecture
|
|
599
|
+
*
|
|
600
|
+
* In-memory Memory implementation providing volatile storage using
|
|
601
|
+
* JavaScript arrays with support for CRUD operations, session management,
|
|
602
|
+
* branch isolation, and soft delete using cursor-based approach.
|
|
603
|
+
*
|
|
604
|
+
* @example
|
|
605
|
+
* ```typescript
|
|
606
|
+
* const memory = new InMemoryMemory({
|
|
607
|
+
* sessionId: 'user-session-123'
|
|
608
|
+
* });
|
|
609
|
+
*
|
|
610
|
+
* await memory.add({
|
|
611
|
+
* message: { id: '1', role: 'user', content: 'Hello' },
|
|
612
|
+
* state: { userId: 'user123' }
|
|
613
|
+
* });
|
|
614
|
+
*
|
|
615
|
+
* const events = await memory.list({ limit: 10 });
|
|
616
|
+
* ```
|
|
617
|
+
*/
|
|
618
|
+
|
|
619
|
+
/**
|
|
620
|
+
* In-Memory Memory implementation using unified adapter architecture
|
|
621
|
+
*
|
|
622
|
+
* Provides volatile storage using JavaScript arrays with support for
|
|
623
|
+
* session-based memory management, CRUD operations, branch isolation, optional context engineering,
|
|
624
|
+
* and efficient soft delete using cursor-based approach.
|
|
625
|
+
*/
|
|
626
|
+
declare class InMemoryMemory extends BaseMemory {
|
|
627
|
+
/** Session identifier for memory isolation */
|
|
628
|
+
sessionId: string;
|
|
629
|
+
/** Whether to enable automatic context management */
|
|
630
|
+
private enableContextManagement;
|
|
631
|
+
private collectionManager;
|
|
632
|
+
private documentConverter;
|
|
633
|
+
private stateManager;
|
|
634
|
+
private cursorManager;
|
|
635
|
+
private branchManager;
|
|
636
|
+
private summaryManager;
|
|
637
|
+
/**
|
|
638
|
+
* Creates a new InMemoryMemory instance
|
|
639
|
+
* @param options - Configuration options
|
|
640
|
+
* @param options.sessionId - Unique session identifier (default: 'default')
|
|
641
|
+
* @param options.enableContextManagement - Whether to automatically manage context (default: true if thresholds provided)
|
|
642
|
+
*/
|
|
643
|
+
constructor(options?: {
|
|
644
|
+
sessionId?: string;
|
|
645
|
+
enableContextManagement?: boolean;
|
|
646
|
+
} & ConstructorParameters<typeof BaseMemory>[0]);
|
|
647
|
+
/**
|
|
648
|
+
* Returns events from in-memory storage with filtering, pagination, and token limiting
|
|
649
|
+
* Respects branch isolation using branch path and snapshot time
|
|
650
|
+
* @param options - Optional filtering and pagination options
|
|
651
|
+
* @returns Promise resolving to filtered array of memory events
|
|
652
|
+
*/
|
|
653
|
+
list(options?: ListOptions): Promise<IMemoryEvent[]>;
|
|
654
|
+
/**
|
|
655
|
+
* Adds a new event to in-memory storage
|
|
656
|
+
* @param event - The memory event to add
|
|
657
|
+
* @param options - Optional session and branch options
|
|
658
|
+
*/
|
|
659
|
+
add(event: TInputMemoryEvent, options?: AddOptions): Promise<void>;
|
|
660
|
+
/**
|
|
661
|
+
* Adds multiple events to in-memory storage
|
|
662
|
+
* @param list - Array of memory events to add
|
|
663
|
+
* @param options - Optional session and branch options
|
|
664
|
+
*/
|
|
665
|
+
addList(list: TInputMemoryEvent[], options?: AddOptions): Promise<void>;
|
|
666
|
+
/**
|
|
667
|
+
* Update an existing event in memory storage
|
|
668
|
+
* @param params - Update parameters including session ID and event
|
|
669
|
+
*/
|
|
670
|
+
update(params: {
|
|
671
|
+
sessionId: string;
|
|
672
|
+
event: Partial<IMemoryEvent> & {
|
|
673
|
+
message: {
|
|
674
|
+
id: string;
|
|
675
|
+
};
|
|
676
|
+
};
|
|
677
|
+
}): Promise<void>;
|
|
678
|
+
/**
|
|
679
|
+
* Deletes an event from in-memory storage by message ID or event object
|
|
680
|
+
* @param params - Delete parameters including event reference or message ID
|
|
681
|
+
*/
|
|
682
|
+
delete(params: {
|
|
683
|
+
event: IMemoryEvent;
|
|
684
|
+
sessionId?: string;
|
|
685
|
+
} | string | number, options?: {
|
|
686
|
+
sessionId?: string;
|
|
687
|
+
}): Promise<void>;
|
|
688
|
+
/**
|
|
689
|
+
* Deletes an event by ID or index
|
|
690
|
+
* @param idOrIndex - Event message ID or array index
|
|
691
|
+
* @param options - Optional session options
|
|
692
|
+
*/
|
|
693
|
+
deleteByIdOrIndex(idOrIndex: string | number, options?: DeleteOptions | {
|
|
694
|
+
sessionId?: string;
|
|
695
|
+
}): Promise<void>;
|
|
696
|
+
/**
|
|
697
|
+
* Clears events from in-memory storage
|
|
698
|
+
* @param options - Optional session-specific clearing
|
|
699
|
+
*/
|
|
700
|
+
clear(options?: ClearOptions): Promise<void>;
|
|
701
|
+
/**
|
|
702
|
+
* Gets the count of events in in-memory storage
|
|
703
|
+
* @param options - Optional session filtering
|
|
704
|
+
* @returns Promise resolving to the count of events
|
|
705
|
+
*/
|
|
706
|
+
getCount(options?: {
|
|
707
|
+
sessionId?: string;
|
|
708
|
+
}): Promise<number>;
|
|
709
|
+
/**
|
|
710
|
+
* Retrieve events using content-based search
|
|
711
|
+
* @param query - Search query text to match against message content
|
|
712
|
+
* @param options - Optional search and session options
|
|
713
|
+
* @returns Promise resolving to array of matching memory events
|
|
714
|
+
*/
|
|
715
|
+
retrieve(query: string, options?: RetrieveOptions): Promise<IMemoryEvent[]>;
|
|
716
|
+
/**
|
|
717
|
+
* Get summaries for a session
|
|
718
|
+
* @param sessionId - Optional session identifier
|
|
719
|
+
* @returns Array of structured summaries
|
|
720
|
+
*/
|
|
721
|
+
getSummaries(sessionId?: string): Promise<StructuredSummary[]>;
|
|
722
|
+
/**
|
|
723
|
+
* Store summary implementation
|
|
724
|
+
* @param params - Summary storage parameters
|
|
725
|
+
*/
|
|
726
|
+
protected storeSummary(params: {
|
|
727
|
+
sessionId?: string;
|
|
728
|
+
summary: StructuredSummary;
|
|
729
|
+
}): Promise<void>;
|
|
730
|
+
/**
|
|
731
|
+
* Clear summarized events implementation using soft delete
|
|
732
|
+
* Sets cursor to hide old events without actually deleting them
|
|
733
|
+
* @param params - Clear parameters
|
|
734
|
+
*/
|
|
735
|
+
protected clearSummarizedEvents(params: {
|
|
736
|
+
sessionId?: string;
|
|
737
|
+
recentToKeep: number;
|
|
738
|
+
}): Promise<void>;
|
|
739
|
+
/**
|
|
740
|
+
* Create a new branch from the current state
|
|
741
|
+
* @param branchName - Name of the new branch
|
|
742
|
+
* @param fromEventId - Optional event ID to branch from
|
|
743
|
+
* @returns Promise resolving to the branch name
|
|
744
|
+
*/
|
|
745
|
+
branch(branchName: string, fromEventId?: string): Promise<string>;
|
|
746
|
+
/**
|
|
747
|
+
* Switch to a different branch or checkout to a specific event
|
|
748
|
+
* @param target - Branch name or event ID
|
|
749
|
+
* @param options - Optional checkout options
|
|
750
|
+
*/
|
|
751
|
+
checkout(target: string, options?: {
|
|
752
|
+
type?: "branch" | "event";
|
|
753
|
+
sessionId?: string;
|
|
754
|
+
}): Promise<void>;
|
|
755
|
+
/**
|
|
756
|
+
* Delete a branch
|
|
757
|
+
* @param branchName - Name of the branch to delete
|
|
758
|
+
*/
|
|
759
|
+
deleteBranch(branchName: string): Promise<void>;
|
|
760
|
+
/**
|
|
761
|
+
* List all branches for the current session
|
|
762
|
+
* @returns Promise resolving to array of branch information
|
|
763
|
+
*/
|
|
764
|
+
listBranches(): Promise<BranchInfo[]>;
|
|
765
|
+
/**
|
|
766
|
+
* Get current branch name
|
|
767
|
+
* @returns Promise resolving to current branch name
|
|
768
|
+
*/
|
|
769
|
+
getCurrentBranch(): Promise<string>;
|
|
770
|
+
/**
|
|
771
|
+
* Clean up branches, keeping only specified branches and the current active branch
|
|
772
|
+
* @param keepBranches - Optional list of branch names to keep (in addition to active branch)
|
|
773
|
+
*/
|
|
774
|
+
cleanupBranches(keepBranches?: string[]): Promise<void>;
|
|
775
|
+
/**
|
|
776
|
+
* Clear all collections (useful for testing)
|
|
777
|
+
*/
|
|
778
|
+
clearAll(): void;
|
|
779
|
+
/**
|
|
780
|
+
* Check if the memory storage is empty
|
|
781
|
+
* @param options - Optional session options
|
|
782
|
+
* @returns Promise resolving to true if no events are stored
|
|
783
|
+
*/
|
|
784
|
+
isEmpty(options?: {
|
|
785
|
+
sessionId?: string;
|
|
786
|
+
}): Promise<boolean>;
|
|
787
|
+
/**
|
|
788
|
+
* Get all session IDs currently stored in memory
|
|
789
|
+
* @returns Array of session identifiers
|
|
790
|
+
*/
|
|
791
|
+
getSessionIds(): string[];
|
|
792
|
+
/**
|
|
793
|
+
* Check if a specific session exists
|
|
794
|
+
* @param sessionId - The session identifier to check
|
|
795
|
+
* @returns True if the session exists
|
|
796
|
+
*/
|
|
797
|
+
hasSession(sessionId: string): boolean;
|
|
798
|
+
/**
|
|
799
|
+
* Get raw session events (for testing purposes)
|
|
800
|
+
* @param sessionId - The session identifier
|
|
801
|
+
* @returns Array of raw events without decompression
|
|
802
|
+
*/
|
|
803
|
+
getSessionEvents(sessionId: string): IMemoryEvent[];
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
/**
|
|
807
|
+
* Abstract base class for API communication stubs
|
|
808
|
+
*/
|
|
809
|
+
declare abstract class Stub {
|
|
810
|
+
abstract post(path: string, body: Record<string, any>, timeout?: number): Promise<any>;
|
|
811
|
+
abstract close(): void;
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
/**
|
|
815
|
+
* Sort order constants
|
|
816
|
+
*/
|
|
817
|
+
declare class Order {
|
|
818
|
+
/** Ascending sort order */
|
|
819
|
+
static readonly ASCENDING = 1;
|
|
820
|
+
/** Descending sort order */
|
|
821
|
+
static readonly DESCENDING = -1;
|
|
822
|
+
}
|
|
823
|
+
/**
|
|
824
|
+
* Common types used across the SDK
|
|
825
|
+
*/
|
|
826
|
+
interface QueryOptions$1 {
|
|
827
|
+
orderBy?: Record<string, number>;
|
|
828
|
+
limit?: number;
|
|
829
|
+
offset?: number;
|
|
830
|
+
}
|
|
831
|
+
interface SearchOptions extends QueryOptions$1 {
|
|
832
|
+
content: string;
|
|
833
|
+
}
|
|
834
|
+
interface WhereClause {
|
|
835
|
+
[key: string]: any;
|
|
836
|
+
}
|
|
837
|
+
interface Message$1 {
|
|
838
|
+
role: string;
|
|
839
|
+
content: string;
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
/**
|
|
843
|
+
* Configuration options for MemoryClient
|
|
844
|
+
*/
|
|
845
|
+
interface IMemoryClientOptions {
|
|
846
|
+
endpoint: string;
|
|
847
|
+
apiKey: string;
|
|
848
|
+
memoryId: string;
|
|
849
|
+
defaultActorId?: string;
|
|
850
|
+
timeout?: number;
|
|
851
|
+
stub?: Stub;
|
|
852
|
+
}
|
|
853
|
+
interface IEvent extends Record<string, any> {
|
|
854
|
+
event_id: string;
|
|
855
|
+
created_at: number;
|
|
856
|
+
updated_at: number;
|
|
857
|
+
}
|
|
858
|
+
/**
|
|
859
|
+
* High-level Agent Memory client with essential operations
|
|
860
|
+
*/
|
|
861
|
+
declare class MemoryClient {
|
|
862
|
+
private defaultMemoryId;
|
|
863
|
+
private defaultActorId?;
|
|
864
|
+
private stub;
|
|
865
|
+
constructor(options: IMemoryClientOptions);
|
|
866
|
+
/**
|
|
867
|
+
* Close the client connection
|
|
868
|
+
*/
|
|
869
|
+
close(): void;
|
|
870
|
+
private getMemoryId;
|
|
871
|
+
private getActorId;
|
|
872
|
+
/**
|
|
873
|
+
* Create a new session
|
|
874
|
+
*/
|
|
875
|
+
createSession(options?: {
|
|
876
|
+
name?: string;
|
|
877
|
+
memoryId?: string;
|
|
878
|
+
actorId?: string;
|
|
879
|
+
}): Promise<any>;
|
|
880
|
+
/**
|
|
881
|
+
* Query sessions based on conditions
|
|
882
|
+
*/
|
|
883
|
+
querySessions(options?: {
|
|
884
|
+
memoryId?: string;
|
|
885
|
+
actorId?: string;
|
|
886
|
+
} & QueryOptions$1): Promise<any>;
|
|
887
|
+
/**
|
|
888
|
+
* Update an existing session
|
|
889
|
+
*/
|
|
890
|
+
updateSession(options: {
|
|
891
|
+
sessionId: string;
|
|
892
|
+
name: string;
|
|
893
|
+
memoryId?: string;
|
|
894
|
+
actorId?: string;
|
|
895
|
+
}): Promise<any>;
|
|
896
|
+
/**
|
|
897
|
+
* Delete an existing session
|
|
898
|
+
*/
|
|
899
|
+
deleteSession(options: {
|
|
900
|
+
sessionId: string;
|
|
901
|
+
memoryId?: string;
|
|
902
|
+
actorId?: string;
|
|
903
|
+
}): Promise<any>;
|
|
904
|
+
/**
|
|
905
|
+
* Search sessions based on conditions and keywords
|
|
906
|
+
*/
|
|
907
|
+
searchSessions(options: {
|
|
908
|
+
content: string;
|
|
909
|
+
memoryId?: string;
|
|
910
|
+
actorId?: string;
|
|
911
|
+
} & Omit<QueryOptions$1, "offset">): Promise<any>;
|
|
912
|
+
/**
|
|
913
|
+
* Append events to a session
|
|
914
|
+
*/
|
|
915
|
+
appendEvent(options: {
|
|
916
|
+
sessionId: string;
|
|
917
|
+
messages: Record<string, any>;
|
|
918
|
+
memoryId?: string;
|
|
919
|
+
actorId?: string;
|
|
920
|
+
}): Promise<{
|
|
921
|
+
event_id: string;
|
|
922
|
+
}>;
|
|
923
|
+
/**
|
|
924
|
+
* Query events based on conditions
|
|
925
|
+
*/
|
|
926
|
+
queryEvents(options?: {
|
|
927
|
+
memoryId?: string;
|
|
928
|
+
actorId?: string;
|
|
929
|
+
sessionId?: string;
|
|
930
|
+
where?: WhereClause;
|
|
931
|
+
} & QueryOptions$1): Promise<{
|
|
932
|
+
events: IEvent[];
|
|
933
|
+
total_count: number;
|
|
934
|
+
}>;
|
|
935
|
+
/**
|
|
936
|
+
* Delete an event from a session
|
|
937
|
+
*/
|
|
938
|
+
deleteEvent(options: {
|
|
939
|
+
sessionId: string;
|
|
940
|
+
eventId: string;
|
|
941
|
+
memoryId?: string;
|
|
942
|
+
actorId?: string;
|
|
943
|
+
}): Promise<any>;
|
|
944
|
+
/**
|
|
945
|
+
* Set state for a session
|
|
946
|
+
*/
|
|
947
|
+
setState(options: {
|
|
948
|
+
sessionId: string;
|
|
949
|
+
state: Record<string, any>;
|
|
950
|
+
memoryId?: string;
|
|
951
|
+
actorId?: string;
|
|
952
|
+
}): Promise<any>;
|
|
953
|
+
/**
|
|
954
|
+
* Get state for a session
|
|
955
|
+
*/
|
|
956
|
+
getStates(options: {
|
|
957
|
+
sessionId: string;
|
|
958
|
+
keys?: string[];
|
|
959
|
+
memoryId?: string;
|
|
960
|
+
actorId?: string;
|
|
961
|
+
}): Promise<any>;
|
|
962
|
+
/**
|
|
963
|
+
* Get all states for an actor in a memory
|
|
964
|
+
*/
|
|
965
|
+
getAllStates(options: {
|
|
966
|
+
sessionId: string;
|
|
967
|
+
memoryId?: string;
|
|
968
|
+
actorId?: string;
|
|
969
|
+
}): Promise<any>;
|
|
970
|
+
/**
|
|
971
|
+
* Delete specified state keys for a session
|
|
972
|
+
*/
|
|
973
|
+
deleteStates(options: {
|
|
974
|
+
sessionId: string;
|
|
975
|
+
keys?: string[];
|
|
976
|
+
memoryId?: string;
|
|
977
|
+
actorId?: string;
|
|
978
|
+
}): Promise<any>;
|
|
979
|
+
/**
|
|
980
|
+
* Flush all states for a session
|
|
981
|
+
*/
|
|
982
|
+
flushStates(options: {
|
|
983
|
+
sessionId: string;
|
|
984
|
+
memoryId?: string;
|
|
985
|
+
actorId?: string;
|
|
986
|
+
}): Promise<any>;
|
|
987
|
+
/**
|
|
988
|
+
* Append records to a session
|
|
989
|
+
*/
|
|
990
|
+
appendRecord(options: {
|
|
991
|
+
sessionId: string;
|
|
992
|
+
content: string;
|
|
993
|
+
strategy: string;
|
|
994
|
+
memoryId?: string;
|
|
995
|
+
actorId?: string;
|
|
996
|
+
}): Promise<any>;
|
|
997
|
+
/**
|
|
998
|
+
* Query records based on conditions
|
|
999
|
+
*/
|
|
1000
|
+
queryRecords(options?: {
|
|
1001
|
+
memoryId?: string;
|
|
1002
|
+
actorId?: string;
|
|
1003
|
+
sessionId?: string;
|
|
1004
|
+
strategies?: string[];
|
|
1005
|
+
where?: WhereClause;
|
|
1006
|
+
} & QueryOptions$1): Promise<{
|
|
1007
|
+
records: {
|
|
1008
|
+
created_at: number;
|
|
1009
|
+
updated_at: number;
|
|
1010
|
+
event_ids: string;
|
|
1011
|
+
record_content: string;
|
|
1012
|
+
record_id: string;
|
|
1013
|
+
score: number;
|
|
1014
|
+
strategy_name: string;
|
|
1015
|
+
}[];
|
|
1016
|
+
total_count: number;
|
|
1017
|
+
}>;
|
|
1018
|
+
/**
|
|
1019
|
+
* Update an existing record
|
|
1020
|
+
*/
|
|
1021
|
+
updateRecord(options: {
|
|
1022
|
+
sessionId: string;
|
|
1023
|
+
recordId: string;
|
|
1024
|
+
content: string;
|
|
1025
|
+
memoryId?: string;
|
|
1026
|
+
actorId?: string;
|
|
1027
|
+
}): Promise<any>;
|
|
1028
|
+
/**
|
|
1029
|
+
* Delete an existing record
|
|
1030
|
+
*/
|
|
1031
|
+
deleteRecord(options: {
|
|
1032
|
+
sessionId: string;
|
|
1033
|
+
recordId: string;
|
|
1034
|
+
memoryId?: string;
|
|
1035
|
+
actorId?: string;
|
|
1036
|
+
}): Promise<any>;
|
|
1037
|
+
/**
|
|
1038
|
+
* Search records based on conditions and keywords
|
|
1039
|
+
*/
|
|
1040
|
+
searchRecords(options: {
|
|
1041
|
+
content: string;
|
|
1042
|
+
memoryId?: string;
|
|
1043
|
+
actorId?: string;
|
|
1044
|
+
sessionId?: string;
|
|
1045
|
+
strategies?: string[];
|
|
1046
|
+
where?: WhereClause;
|
|
1047
|
+
} & Omit<QueryOptions$1, "offset">): Promise<{
|
|
1048
|
+
records: {
|
|
1049
|
+
created_at: number;
|
|
1050
|
+
updated_at: number;
|
|
1051
|
+
event_ids: string;
|
|
1052
|
+
record_content: string;
|
|
1053
|
+
record_id: string;
|
|
1054
|
+
score: number;
|
|
1055
|
+
strategy_name: string;
|
|
1056
|
+
}[];
|
|
1057
|
+
total_count: number;
|
|
1058
|
+
}>;
|
|
1059
|
+
}
|
|
1060
|
+
|
|
1061
|
+
/**
|
|
1062
|
+
* @fileoverview TDAI cloud-based memory implementation for production environments
|
|
1063
|
+
*
|
|
1064
|
+
* TDAI Memory implementation providing cloud-based persistent storage with semantic search,
|
|
1065
|
+
* advanced querying capabilities, and production-grade scalability through TDAI services.
|
|
1066
|
+
*
|
|
1067
|
+
* @example
|
|
1068
|
+
* ```typescript
|
|
1069
|
+
* const memory = new TDAIMemory({
|
|
1070
|
+
* sessionId: 'user-session-123',
|
|
1071
|
+
* clientOptions: { apiKey: 'your-api-key' },
|
|
1072
|
+
* useCache: true
|
|
1073
|
+
* });
|
|
1074
|
+
* await memory.add({ message: { id: '1', role: 'user', content: 'Hello' } });
|
|
1075
|
+
* const results = await memory.retrieve('greeting');
|
|
1076
|
+
* ```
|
|
1077
|
+
*/
|
|
1078
|
+
declare class TDAIMemory extends BaseMemory {
|
|
1079
|
+
/** TDAI client instance for cloud operations */
|
|
1080
|
+
private client;
|
|
1081
|
+
/** Session identifier for memory isolation */
|
|
1082
|
+
private sessionId;
|
|
1083
|
+
/** Cache storage for improved performance */
|
|
1084
|
+
private cachedMap;
|
|
1085
|
+
/** Whether local caching is enabled */
|
|
1086
|
+
private useCache;
|
|
1087
|
+
/** Whether to enable automatic context management */
|
|
1088
|
+
private enableContextManagement;
|
|
1089
|
+
/**
|
|
1090
|
+
* Creates a new TDAIMemory instance
|
|
1091
|
+
* @param options - Configuration options
|
|
1092
|
+
* @param options.sessionId - Unique session identifier
|
|
1093
|
+
* @param options.client - Optional pre-configured TDAI client
|
|
1094
|
+
* @param options.clientOptions - TDAI client configuration if client not provided
|
|
1095
|
+
* @param options.useCache - Enable local caching for better performance
|
|
1096
|
+
* @param options.enableContextManagement - Whether to automatically manage context after adding events (default: true)
|
|
1097
|
+
*/
|
|
1098
|
+
constructor(options: {
|
|
1099
|
+
sessionId: string;
|
|
1100
|
+
client?: MemoryClient;
|
|
1101
|
+
clientOptions?: IMemoryClientOptions;
|
|
1102
|
+
useCache?: boolean;
|
|
1103
|
+
enableContextManagement?: boolean;
|
|
1104
|
+
} & ConstructorParameters<typeof BaseMemory>[0]);
|
|
1105
|
+
/**
|
|
1106
|
+
* Returns events from TDAI service with filtering, pagination, and token limiting
|
|
1107
|
+
* @param options - Optional filtering and pagination options
|
|
1108
|
+
* @returns Promise resolving to filtered array of memory events
|
|
1109
|
+
*/
|
|
1110
|
+
list(options?: ListOptions): Promise<IMemoryEvent[]>;
|
|
1111
|
+
/**
|
|
1112
|
+
* Add a single event to TDAI cloud storage
|
|
1113
|
+
* @param event - The memory event to store
|
|
1114
|
+
* @param options - Optional session options
|
|
1115
|
+
*/
|
|
1116
|
+
add(event: IMemoryEvent, options?: AddOptions): Promise<void>;
|
|
1117
|
+
/**
|
|
1118
|
+
* Add multiple events efficiently with sequential processing
|
|
1119
|
+
* @param list - Array of memory events to store
|
|
1120
|
+
*/
|
|
1121
|
+
addList(list: IMemoryEvent[], options?: AddOptions): Promise<void>;
|
|
1122
|
+
update({ sessionId, event }: {
|
|
1123
|
+
sessionId?: string;
|
|
1124
|
+
event: IMemoryEvent;
|
|
1125
|
+
}): Promise<void>;
|
|
1126
|
+
/**
|
|
1127
|
+
* Delete an event from TDAI cloud storage
|
|
1128
|
+
* @param idOrIndex - Event ID (string) or array index (number, not yet implemented)
|
|
1129
|
+
*/
|
|
1130
|
+
delete(idOrIndex: string | number): Promise<void>;
|
|
1131
|
+
/**
|
|
1132
|
+
* Retrieve events using TDAI's search capabilities
|
|
1133
|
+
* @param query - Optional search query for content matching
|
|
1134
|
+
* @returns Promise resolving to array of matching events
|
|
1135
|
+
*/
|
|
1136
|
+
retrieve(query?: string): Promise<IMemoryEvent[]>;
|
|
1137
|
+
/**
|
|
1138
|
+
* Clear all events.
|
|
1139
|
+
*/
|
|
1140
|
+
/**
|
|
1141
|
+
* Check if the session contains any events
|
|
1142
|
+
* @returns Promise resolving to true if no events are stored
|
|
1143
|
+
*/
|
|
1144
|
+
isEmpty(): Promise<boolean>;
|
|
1145
|
+
/**
|
|
1146
|
+
* Get the current number of events stored in the session
|
|
1147
|
+
* @returns Promise resolving to the count of stored events
|
|
1148
|
+
*/
|
|
1149
|
+
getCount(): Promise<number>;
|
|
1150
|
+
/**
|
|
1151
|
+
* Convert TDAI events to IMemoryEvent format for compatibility
|
|
1152
|
+
* @param events - Array of TDAI events to convert
|
|
1153
|
+
* @returns Array of converted memory events
|
|
1154
|
+
*/
|
|
1155
|
+
private convertTDAIEventsToMemoryEvents;
|
|
1156
|
+
/**
|
|
1157
|
+
* Extract state from metadata, excluding message-specific fields
|
|
1158
|
+
* @param metadata - Event metadata object
|
|
1159
|
+
* @returns Cleaned state object without message fields
|
|
1160
|
+
*/
|
|
1161
|
+
private extractStateFromMetadata;
|
|
1162
|
+
/**
|
|
1163
|
+
* Clean up branches - not supported by TDAI Memory
|
|
1164
|
+
* @param keepBranches - Ignored parameter
|
|
1165
|
+
* @throws Error indicating branch operations are not supported
|
|
1166
|
+
*/
|
|
1167
|
+
cleanupBranches(keepBranches?: string[]): Promise<void>;
|
|
1168
|
+
}
|
|
1169
|
+
|
|
1170
|
+
/**
|
|
1171
|
+
* CloudBase Memory Types
|
|
1172
|
+
*
|
|
1173
|
+
* This file contains all type definitions used by CloudBase memory implementation.
|
|
1174
|
+
*/
|
|
1175
|
+
/**
|
|
1176
|
+
* CloudBase app interface
|
|
1177
|
+
*/
|
|
1178
|
+
interface CloudBaseApp {
|
|
1179
|
+
database(): CloudBaseDatabase;
|
|
1180
|
+
}
|
|
1181
|
+
/**
|
|
1182
|
+
* CloudBase database interface
|
|
1183
|
+
*/
|
|
1184
|
+
interface CloudBaseDatabase {
|
|
1185
|
+
collection(name: string): CloudBaseCollection;
|
|
1186
|
+
command: any;
|
|
1187
|
+
RegExp(options: {
|
|
1188
|
+
regexp: string;
|
|
1189
|
+
options?: string;
|
|
1190
|
+
}): any;
|
|
1191
|
+
}
|
|
1192
|
+
/**
|
|
1193
|
+
* CloudBase collection interface
|
|
1194
|
+
*/
|
|
1195
|
+
interface CloudBaseCollection {
|
|
1196
|
+
add(data: any): Promise<any>;
|
|
1197
|
+
doc(id: string): CloudBaseDocument;
|
|
1198
|
+
where(condition: any): CloudBaseQuery;
|
|
1199
|
+
get(): Promise<CloudBaseQueryResult>;
|
|
1200
|
+
}
|
|
1201
|
+
/**
|
|
1202
|
+
* CloudBase document interface
|
|
1203
|
+
*/
|
|
1204
|
+
interface CloudBaseDocument {
|
|
1205
|
+
get(): Promise<CloudBaseQueryResult>;
|
|
1206
|
+
update(data: any): Promise<any>;
|
|
1207
|
+
remove(): Promise<any>;
|
|
1208
|
+
}
|
|
1209
|
+
/**
|
|
1210
|
+
* CloudBase query interface
|
|
1211
|
+
*/
|
|
1212
|
+
interface CloudBaseQuery {
|
|
1213
|
+
where(condition: any): CloudBaseQuery;
|
|
1214
|
+
orderBy(field: string, order: "asc" | "desc"): CloudBaseQuery;
|
|
1215
|
+
skip(offset: number): CloudBaseQuery;
|
|
1216
|
+
limit(limit: number): CloudBaseQuery;
|
|
1217
|
+
get(): Promise<CloudBaseQueryResult>;
|
|
1218
|
+
count(): Promise<{
|
|
1219
|
+
total: number;
|
|
1220
|
+
}>;
|
|
1221
|
+
remove(options?: any): Promise<any>;
|
|
1222
|
+
}
|
|
1223
|
+
/**
|
|
1224
|
+
* CloudBase query result interface
|
|
1225
|
+
*/
|
|
1226
|
+
interface CloudBaseQueryResult {
|
|
1227
|
+
data: any[];
|
|
1228
|
+
}
|
|
1229
|
+
|
|
1230
|
+
/**
|
|
1231
|
+
* Query Builder Interface
|
|
1232
|
+
*
|
|
1233
|
+
* Database-agnostic query builder for constructing queries
|
|
1234
|
+
*/
|
|
1235
|
+
/**
|
|
1236
|
+
* Comparison operators
|
|
1237
|
+
*/
|
|
1238
|
+
type ComparisonOperator = "eq" | "ne" | "gt" | "gte" | "lt" | "lte" | "in" | "nin";
|
|
1239
|
+
/**
|
|
1240
|
+
* Query builder interface
|
|
1241
|
+
*/
|
|
1242
|
+
interface IQueryBuilder {
|
|
1243
|
+
/**
|
|
1244
|
+
* Add a WHERE condition
|
|
1245
|
+
*/
|
|
1246
|
+
where(field: string, operator: ComparisonOperator, value: any): this;
|
|
1247
|
+
/**
|
|
1248
|
+
* Add an AND condition
|
|
1249
|
+
*/
|
|
1250
|
+
and(field: string, operator: ComparisonOperator, value: any): this;
|
|
1251
|
+
/**
|
|
1252
|
+
* Add an OR condition with multiple sub-conditions
|
|
1253
|
+
*/
|
|
1254
|
+
or(conditions: IQueryBuilder[]): this;
|
|
1255
|
+
/**
|
|
1256
|
+
* Add an IN condition
|
|
1257
|
+
*/
|
|
1258
|
+
in(field: string, values: any[]): this;
|
|
1259
|
+
/**
|
|
1260
|
+
* Add a regex/pattern matching condition
|
|
1261
|
+
*/
|
|
1262
|
+
regex(field: string, pattern: string, options?: string): this;
|
|
1263
|
+
/**
|
|
1264
|
+
* Build the final query object for the specific database
|
|
1265
|
+
*/
|
|
1266
|
+
build(): any;
|
|
1267
|
+
/**
|
|
1268
|
+
* Clone the query builder
|
|
1269
|
+
*/
|
|
1270
|
+
clone(): IQueryBuilder;
|
|
1271
|
+
}
|
|
1272
|
+
/**
|
|
1273
|
+
* Update builder interface
|
|
1274
|
+
*/
|
|
1275
|
+
interface IUpdateBuilder {
|
|
1276
|
+
/**
|
|
1277
|
+
* Set a field value
|
|
1278
|
+
*/
|
|
1279
|
+
set(field: string, value: any): this;
|
|
1280
|
+
/**
|
|
1281
|
+
* Set multiple field values
|
|
1282
|
+
*/
|
|
1283
|
+
setMultiple(updates: Record<string, any>): this;
|
|
1284
|
+
/**
|
|
1285
|
+
* Unset/remove a field
|
|
1286
|
+
*/
|
|
1287
|
+
unset(field: string): this;
|
|
1288
|
+
/**
|
|
1289
|
+
* Increment a numeric field
|
|
1290
|
+
*/
|
|
1291
|
+
increment(field: string, value: number): this;
|
|
1292
|
+
/**
|
|
1293
|
+
* Build the final update object for the specific database
|
|
1294
|
+
*/
|
|
1295
|
+
build(): any;
|
|
1296
|
+
}
|
|
1297
|
+
|
|
1298
|
+
/**
|
|
1299
|
+
* Common Memory Types
|
|
1300
|
+
*
|
|
1301
|
+
* Shared type definitions for database-backed memory implementations
|
|
1302
|
+
*/
|
|
1303
|
+
/**
|
|
1304
|
+
* Generic database document structure
|
|
1305
|
+
*/
|
|
1306
|
+
interface BaseMemoryDocument {
|
|
1307
|
+
_id?: string;
|
|
1308
|
+
sessionId: string;
|
|
1309
|
+
messageId: string;
|
|
1310
|
+
branchPath: string;
|
|
1311
|
+
role: string;
|
|
1312
|
+
content: string;
|
|
1313
|
+
toolCalls?: any[];
|
|
1314
|
+
toolCallId?: string;
|
|
1315
|
+
state: Record<string, any>;
|
|
1316
|
+
createdAt: Date;
|
|
1317
|
+
updatedAt: Date;
|
|
1318
|
+
}
|
|
1319
|
+
/**
|
|
1320
|
+
* Database adapter interface
|
|
1321
|
+
* Abstracts database-specific operations
|
|
1322
|
+
*/
|
|
1323
|
+
interface IDatabaseAdapter {
|
|
1324
|
+
getCollection(name: string): any;
|
|
1325
|
+
find<T = any>(collection: any, query: any, options?: QueryOptions): Promise<T[]>;
|
|
1326
|
+
findOne<T = any>(collection: any, query: any): Promise<T | null>;
|
|
1327
|
+
insertOne<T = any>(collection: any, document: Partial<T>): Promise<T>;
|
|
1328
|
+
insertMany<T = any>(collection: any, documents: Partial<T>[]): Promise<{
|
|
1329
|
+
insertedCount: number;
|
|
1330
|
+
}>;
|
|
1331
|
+
updateOne(collection: any, filter: any, update: any): Promise<{
|
|
1332
|
+
modifiedCount: number;
|
|
1333
|
+
}>;
|
|
1334
|
+
deleteOne(collection: any, filter: any): Promise<{
|
|
1335
|
+
deletedCount: number;
|
|
1336
|
+
}>;
|
|
1337
|
+
deleteMany(collection: any, filter: any): Promise<{
|
|
1338
|
+
deletedCount: number;
|
|
1339
|
+
}>;
|
|
1340
|
+
count(collection: any, query: any): Promise<number>;
|
|
1341
|
+
createQueryBuilder(): IQueryBuilder;
|
|
1342
|
+
createUpdateBuilder(): IUpdateBuilder;
|
|
1343
|
+
}
|
|
1344
|
+
|
|
1345
|
+
/**
|
|
1346
|
+
* Query options
|
|
1347
|
+
*/
|
|
1348
|
+
interface QueryOptions {
|
|
1349
|
+
sort?: Record<string, 1 | -1>;
|
|
1350
|
+
skip?: number;
|
|
1351
|
+
limit?: number;
|
|
1352
|
+
}
|
|
1353
|
+
|
|
1354
|
+
/**
|
|
1355
|
+
* @fileoverview CloudBase cloud-based memory implementation for Tencent CloudBase
|
|
1356
|
+
*
|
|
1357
|
+
* CloudBase Memory implementation providing cloud-based persistent storage using
|
|
1358
|
+
* Tencent CloudBase database service with support for CRUD operations, session management,
|
|
1359
|
+
* branch isolation, and soft delete using cursor-based approach.
|
|
1360
|
+
*
|
|
1361
|
+
* @example
|
|
1362
|
+
* ```typescript
|
|
1363
|
+
* import cloudbase from '@cloudbase/node-sdk';
|
|
1364
|
+
*
|
|
1365
|
+
* const app = cloudbase.init({
|
|
1366
|
+
* env: 'your-env-id',
|
|
1367
|
+
* secretId: 'your-secret-id',
|
|
1368
|
+
* secretKey: 'your-secret-key'
|
|
1369
|
+
* });
|
|
1370
|
+
*
|
|
1371
|
+
* const memory = new CloudBaseMemory({
|
|
1372
|
+
* app,
|
|
1373
|
+
* collectionName: 'memory_events',
|
|
1374
|
+
* sessionId: 'user-session-123'
|
|
1375
|
+
* });
|
|
1376
|
+
*
|
|
1377
|
+
* await memory.add({
|
|
1378
|
+
* message: { id: '1', role: 'user', content: 'Hello' },
|
|
1379
|
+
* state: { userId: 'user123' }
|
|
1380
|
+
* });
|
|
1381
|
+
*
|
|
1382
|
+
* const events = await memory.list({ limit: 10 });
|
|
1383
|
+
* ```
|
|
1384
|
+
*/
|
|
1385
|
+
|
|
1386
|
+
/**
|
|
1387
|
+
* CloudBase Memory implementation for Tencent CloudBase
|
|
1388
|
+
*
|
|
1389
|
+
* Provides persistent storage using CloudBase database service with support for
|
|
1390
|
+
* session-based memory management, CRUD operations, branch isolation, optional context engineering,
|
|
1391
|
+
* and efficient soft delete using cursor-based approach.
|
|
1392
|
+
*/
|
|
1393
|
+
declare class CloudBaseMemory extends BaseMemory {
|
|
1394
|
+
/** Session identifier for memory isolation */
|
|
1395
|
+
sessionId: string;
|
|
1396
|
+
/** Whether to enable automatic context management */
|
|
1397
|
+
private enableContextManagement;
|
|
1398
|
+
private collectionManager;
|
|
1399
|
+
private documentConverter;
|
|
1400
|
+
private stateManager;
|
|
1401
|
+
private cursorManager;
|
|
1402
|
+
private branchManager;
|
|
1403
|
+
private summaryManager;
|
|
1404
|
+
/**
|
|
1405
|
+
* Creates a new CloudBaseMemory instance
|
|
1406
|
+
* @param options - Configuration options
|
|
1407
|
+
* @param options.app - CloudBase app instance from @cloudbase/node-sdk
|
|
1408
|
+
* @param options.collectionName - Collection name for storing events (default: 'memory_events')
|
|
1409
|
+
* @param options.summaryCollectionName - Collection name for storing summaries (default: 'memory_summaries')
|
|
1410
|
+
* @param options.stateCollectionName - Collection name for storing session state (default: 'memory_state')
|
|
1411
|
+
* @param options.sessionId - Unique session identifier (default: 'default')
|
|
1412
|
+
* @param options.enableContextManagement - Whether to automatically manage context (default: true if thresholds provided)
|
|
1413
|
+
*/
|
|
1414
|
+
constructor(options: {
|
|
1415
|
+
app: CloudBaseApp;
|
|
1416
|
+
collectionName?: string;
|
|
1417
|
+
summaryCollectionName?: string;
|
|
1418
|
+
stateCollectionName?: string;
|
|
1419
|
+
sessionId?: string;
|
|
1420
|
+
enableContextManagement?: boolean;
|
|
1421
|
+
} & ConstructorParameters<typeof BaseMemory>[0]);
|
|
1422
|
+
/**
|
|
1423
|
+
* Returns events from CloudBase database with filtering, pagination, and token limiting
|
|
1424
|
+
* Respects branch isolation using branch path and snapshot time
|
|
1425
|
+
* @param options - Optional filtering and pagination options
|
|
1426
|
+
* @returns Promise resolving to filtered array of memory events
|
|
1427
|
+
*/
|
|
1428
|
+
list(options?: ListOptions): Promise<IMemoryEvent[]>;
|
|
1429
|
+
/**
|
|
1430
|
+
* Add a single event to CloudBase database
|
|
1431
|
+
* @param event - The memory event to store
|
|
1432
|
+
* @param options - Optional session options
|
|
1433
|
+
*/
|
|
1434
|
+
add(event: IMemoryEvent, options?: AddOptions): Promise<void>;
|
|
1435
|
+
/**
|
|
1436
|
+
* Add multiple events efficiently
|
|
1437
|
+
* @param list - Array of memory events to store
|
|
1438
|
+
* @param options - Optional session options
|
|
1439
|
+
*/
|
|
1440
|
+
addList(list: IMemoryEvent[], options?: AddOptions): Promise<void>;
|
|
1441
|
+
/**
|
|
1442
|
+
* Update an event in CloudBase database
|
|
1443
|
+
* @param params - Update parameters
|
|
1444
|
+
*/
|
|
1445
|
+
update(params: {
|
|
1446
|
+
sessionId?: string;
|
|
1447
|
+
event: IMemoryEvent;
|
|
1448
|
+
}): Promise<void>;
|
|
1449
|
+
/**
|
|
1450
|
+
* Delete an event from CloudBase database
|
|
1451
|
+
* @param idOrIndex - Message ID (string) to delete
|
|
1452
|
+
* @param options - Optional session options
|
|
1453
|
+
*/
|
|
1454
|
+
delete(idOrIndex: string | number, options?: DeleteOptions): Promise<void>;
|
|
1455
|
+
/**
|
|
1456
|
+
* Clear all events from CloudBase database
|
|
1457
|
+
* @param options - Optional session options. If sessionId provided, clears only that session using soft delete.
|
|
1458
|
+
*/
|
|
1459
|
+
clear(options?: ClearOptions): Promise<void>;
|
|
1460
|
+
/**
|
|
1461
|
+
* Check if the session contains any events (respecting soft delete)
|
|
1462
|
+
* @param options - Optional session options
|
|
1463
|
+
* @returns Promise resolving to true if no events are stored
|
|
1464
|
+
*/
|
|
1465
|
+
isEmpty(options?: {
|
|
1466
|
+
sessionId?: string;
|
|
1467
|
+
}): Promise<boolean>;
|
|
1468
|
+
/**
|
|
1469
|
+
* Get the current number of events stored in the session (respecting soft delete)
|
|
1470
|
+
* @param options - Optional session options
|
|
1471
|
+
* @returns Promise resolving to the count of stored events
|
|
1472
|
+
*/
|
|
1473
|
+
getCount(options?: {
|
|
1474
|
+
sessionId?: string;
|
|
1475
|
+
}): Promise<number>;
|
|
1476
|
+
/**
|
|
1477
|
+
* Retrieve events using content-based search
|
|
1478
|
+
* @param query - Search query text to match against message content
|
|
1479
|
+
* @param options - Optional session options
|
|
1480
|
+
* @returns Promise resolving to array of matching events
|
|
1481
|
+
*/
|
|
1482
|
+
retrieve(query: string, options?: RetrieveOptions): Promise<IMemoryEvent[]>;
|
|
1483
|
+
/**
|
|
1484
|
+
* Get summaries for a session
|
|
1485
|
+
* @param sessionId - Optional session identifier
|
|
1486
|
+
* @returns Array of structured summaries
|
|
1487
|
+
*/
|
|
1488
|
+
getSummaries(sessionId?: string): Promise<StructuredSummary[]>;
|
|
1489
|
+
/**
|
|
1490
|
+
* Store summary implementation
|
|
1491
|
+
* @param params - Summary storage parameters
|
|
1492
|
+
*/
|
|
1493
|
+
protected storeSummary(params: {
|
|
1494
|
+
sessionId?: string;
|
|
1495
|
+
summary: StructuredSummary;
|
|
1496
|
+
}): Promise<void>;
|
|
1497
|
+
/**
|
|
1498
|
+
* Clear summarized events implementation using soft delete
|
|
1499
|
+
* Sets cursor to hide old events without actually deleting them
|
|
1500
|
+
* @param params - Clear parameters
|
|
1501
|
+
*/
|
|
1502
|
+
protected clearSummarizedEvents(params: {
|
|
1503
|
+
sessionId: string | undefined;
|
|
1504
|
+
recentToKeep: number;
|
|
1505
|
+
}): Promise<void>;
|
|
1506
|
+
/**
|
|
1507
|
+
* Create a new branch from current session state
|
|
1508
|
+
* @param branchName - Name for the new branch
|
|
1509
|
+
* @param fromEventId - Optional event ID to branch from
|
|
1510
|
+
* @returns Promise resolving to branch ID
|
|
1511
|
+
*/
|
|
1512
|
+
branch(branchName: string, fromEventId?: string): Promise<string>;
|
|
1513
|
+
/**
|
|
1514
|
+
* Switch to a different branch or checkout to a specific event
|
|
1515
|
+
* @param target - Branch name or event ID to checkout
|
|
1516
|
+
* @param options - Optional configuration
|
|
1517
|
+
*/
|
|
1518
|
+
checkout(target: string, options?: {
|
|
1519
|
+
type?: "branch" | "event";
|
|
1520
|
+
sessionId?: string;
|
|
1521
|
+
}): Promise<void>;
|
|
1522
|
+
/**
|
|
1523
|
+
* Delete a branch
|
|
1524
|
+
* @param branchName - Name of the branch to delete
|
|
1525
|
+
*/
|
|
1526
|
+
deleteBranch(branchName: string): Promise<void>;
|
|
1527
|
+
/**
|
|
1528
|
+
* List all branches
|
|
1529
|
+
* @returns Promise resolving to array of branch information
|
|
1530
|
+
*/
|
|
1531
|
+
listBranches(): Promise<BranchInfo[]>;
|
|
1532
|
+
/**
|
|
1533
|
+
* Get current branch name
|
|
1534
|
+
* @returns Promise resolving to current branch name
|
|
1535
|
+
*/
|
|
1536
|
+
getCurrentBranch(): Promise<string>;
|
|
1537
|
+
/**
|
|
1538
|
+
* Clean up branches, keeping only specified branches and the current active branch
|
|
1539
|
+
* @param keepBranches - Optional list of branch names to keep (in addition to active branch)
|
|
1540
|
+
*/
|
|
1541
|
+
cleanupBranches(keepBranches?: string[]): Promise<void>;
|
|
1542
|
+
}
|
|
1543
|
+
|
|
1544
|
+
/**
|
|
1545
|
+
* @fileoverview MongoDB memory implementation for persistent storage
|
|
1546
|
+
*
|
|
1547
|
+
* MongoDB Memory implementation providing persistent storage using MongoDB
|
|
1548
|
+
* with support for CRUD operations, session management, branch isolation,
|
|
1549
|
+
* and soft delete using cursor-based approach.
|
|
1550
|
+
*
|
|
1551
|
+
* @example
|
|
1552
|
+
* ```typescript
|
|
1553
|
+
* import { MongoClient } from 'mongodb';
|
|
1554
|
+
*
|
|
1555
|
+
* const client = new MongoClient('mongodb://localhost:27017');
|
|
1556
|
+
* await client.connect();
|
|
1557
|
+
* const db = client.db('ag_kit_memory');
|
|
1558
|
+
*
|
|
1559
|
+
* const memory = new MongoDBMemory({
|
|
1560
|
+
* db,
|
|
1561
|
+
* sessionId: 'user-session-123',
|
|
1562
|
+
* collectionName: 'memory_events'
|
|
1563
|
+
* });
|
|
1564
|
+
*
|
|
1565
|
+
* await memory.add({
|
|
1566
|
+
* message: { id: '1', role: 'user', content: 'Hello' },
|
|
1567
|
+
* state: { userId: 'user123' }
|
|
1568
|
+
* });
|
|
1569
|
+
*
|
|
1570
|
+
* const events = await memory.list({ limit: 10 });
|
|
1571
|
+
* ```
|
|
1572
|
+
*/
|
|
1573
|
+
|
|
1574
|
+
/**
|
|
1575
|
+
* Configuration options for MongoDBMemory
|
|
1576
|
+
*/
|
|
1577
|
+
interface MongoDBMemoryConfig extends IBaseMemoryOptions {
|
|
1578
|
+
db?: Db;
|
|
1579
|
+
connectionString?: string;
|
|
1580
|
+
databaseName?: string;
|
|
1581
|
+
clientOptions?: MongoClientOptions;
|
|
1582
|
+
collectionName?: string;
|
|
1583
|
+
stateCollectionName?: string;
|
|
1584
|
+
summaryCollectionName?: string;
|
|
1585
|
+
sessionId?: string;
|
|
1586
|
+
enableContextManagement?: boolean;
|
|
1587
|
+
}
|
|
1588
|
+
/**
|
|
1589
|
+
* MongoDB Memory implementation for persistent storage
|
|
1590
|
+
*
|
|
1591
|
+
* Provides persistent storage using MongoDB with support for
|
|
1592
|
+
* session-based memory management, CRUD operations, branch isolation, optional context engineering,
|
|
1593
|
+
* and efficient soft delete using cursor-based approach.
|
|
1594
|
+
*/
|
|
1595
|
+
declare class MongoDBMemory extends BaseMemory {
|
|
1596
|
+
/** Session identifier for memory isolation */
|
|
1597
|
+
sessionId: string;
|
|
1598
|
+
/** Whether to enable automatic context management */
|
|
1599
|
+
private enableContextManagement;
|
|
1600
|
+
private db;
|
|
1601
|
+
private client?;
|
|
1602
|
+
private collectionManager;
|
|
1603
|
+
private documentConverter;
|
|
1604
|
+
private stateManager;
|
|
1605
|
+
private cursorManager;
|
|
1606
|
+
private branchManager;
|
|
1607
|
+
private summaryManager;
|
|
1608
|
+
/**
|
|
1609
|
+
* Creates a new MongoDBMemory instance
|
|
1610
|
+
* @param config - Configuration options
|
|
1611
|
+
* @param config.db - MongoDB database instance
|
|
1612
|
+
* @param config.connectionString - MongoDB connection string for auto-initialization
|
|
1613
|
+
* @param config.databaseName - Database name (default: 'ag_kit_memory')
|
|
1614
|
+
* @param config.collectionName - Collection name for storing events (default: 'memory_events')
|
|
1615
|
+
* @param config.summaryCollectionName - Collection name for storing summaries (default: 'memory_summaries')
|
|
1616
|
+
* @param config.stateCollectionName - Collection name for storing session state (default: 'memory_state')
|
|
1617
|
+
* @param config.sessionId - Unique session identifier (default: 'default')
|
|
1618
|
+
* @param config.enableContextManagement - Whether to automatically manage context (default: true if thresholds provided)
|
|
1619
|
+
*/
|
|
1620
|
+
constructor(config: MongoDBMemoryConfig);
|
|
1621
|
+
/**
|
|
1622
|
+
* Initialize MongoDB connection and managers
|
|
1623
|
+
*/
|
|
1624
|
+
initialize(config: MongoDBMemoryConfig): Promise<void>;
|
|
1625
|
+
/**
|
|
1626
|
+
* Initialize all manager instances
|
|
1627
|
+
*/
|
|
1628
|
+
private _initializeManagers;
|
|
1629
|
+
/**
|
|
1630
|
+
* Add a single event to MongoDB database
|
|
1631
|
+
* @param event - The memory event to store
|
|
1632
|
+
* @param options - Optional session options
|
|
1633
|
+
*/
|
|
1634
|
+
add(event: IMemoryEvent, options?: AddOptions): Promise<void>;
|
|
1635
|
+
/**
|
|
1636
|
+
* Add multiple events efficiently
|
|
1637
|
+
* @param list - Array of memory events to store
|
|
1638
|
+
* @param options - Optional session options
|
|
1639
|
+
*/
|
|
1640
|
+
addList(list: IMemoryEvent[], options?: AddOptions): Promise<void>;
|
|
1641
|
+
/**
|
|
1642
|
+
* Returns events from MongoDB database with filtering, pagination, and token limiting
|
|
1643
|
+
* Respects branch isolation using branch path and snapshot time
|
|
1644
|
+
* @param options - Optional filtering and pagination options
|
|
1645
|
+
* @returns Promise resolving to filtered array of memory events
|
|
1646
|
+
*/
|
|
1647
|
+
list(options?: ListOptions): Promise<IMemoryEvent[]>;
|
|
1648
|
+
/**
|
|
1649
|
+
* Update an event in MongoDB database
|
|
1650
|
+
* @param params - Update parameters
|
|
1651
|
+
*/
|
|
1652
|
+
update(params: {
|
|
1653
|
+
sessionId?: string;
|
|
1654
|
+
event: IMemoryEvent;
|
|
1655
|
+
}): Promise<void>;
|
|
1656
|
+
/**
|
|
1657
|
+
* Delete an event from MongoDB database
|
|
1658
|
+
* @param messageId - Message ID (string) to delete
|
|
1659
|
+
* @param options - Optional session options
|
|
1660
|
+
*/
|
|
1661
|
+
delete(messageId: string): Promise<void>;
|
|
1662
|
+
/**
|
|
1663
|
+
* Clear all events from MongoDB database
|
|
1664
|
+
* @param options - Optional session options. If sessionId provided, clears only that session using soft delete.
|
|
1665
|
+
*/
|
|
1666
|
+
clear(options?: ClearOptions): Promise<void>;
|
|
1667
|
+
/**
|
|
1668
|
+
* Check if the session contains any events (respecting soft delete)
|
|
1669
|
+
* @param options - Optional session options
|
|
1670
|
+
* @returns Promise resolving to true if no events are stored
|
|
1671
|
+
*/
|
|
1672
|
+
isEmpty(options?: {
|
|
1673
|
+
sessionId?: string;
|
|
1674
|
+
}): Promise<boolean>;
|
|
1675
|
+
/**
|
|
1676
|
+
* Get the current number of events stored in the session (respecting soft delete)
|
|
1677
|
+
* @param options - Optional session options
|
|
1678
|
+
* @returns Promise resolving to the count of stored events
|
|
1679
|
+
*/
|
|
1680
|
+
getCount(options?: {
|
|
1681
|
+
sessionId?: string;
|
|
1682
|
+
}): Promise<number>;
|
|
1683
|
+
/**
|
|
1684
|
+
* Retrieve events using content-based search
|
|
1685
|
+
* @param query - Search query text to match against message content
|
|
1686
|
+
* @param options - Optional session options
|
|
1687
|
+
* @returns Promise resolving to array of matching events
|
|
1688
|
+
*/
|
|
1689
|
+
retrieve(query: string, options?: RetrieveOptions): Promise<IMemoryEvent[]>;
|
|
1690
|
+
/**
|
|
1691
|
+
* Get summaries for a session
|
|
1692
|
+
* @param sessionId - Optional session identifier
|
|
1693
|
+
* @returns Array of structured summaries
|
|
1694
|
+
*/
|
|
1695
|
+
getSummaries(sessionId?: string): Promise<StructuredSummary[]>;
|
|
1696
|
+
/**
|
|
1697
|
+
* Store summary implementation
|
|
1698
|
+
* @param params - Summary storage parameters
|
|
1699
|
+
*/
|
|
1700
|
+
protected storeSummary(params: {
|
|
1701
|
+
sessionId?: string;
|
|
1702
|
+
summary: StructuredSummary;
|
|
1703
|
+
}): Promise<void>;
|
|
1704
|
+
/**
|
|
1705
|
+
* Clear summarized events implementation using soft delete
|
|
1706
|
+
* Sets cursor to hide old events without actually deleting them
|
|
1707
|
+
* @param params - Clear parameters
|
|
1708
|
+
*/
|
|
1709
|
+
protected clearSummarizedEvents(params: {
|
|
1710
|
+
sessionId: string | undefined;
|
|
1711
|
+
recentToKeep: number;
|
|
1712
|
+
}): Promise<void>;
|
|
1713
|
+
/**
|
|
1714
|
+
* Create a new branch from current session state
|
|
1715
|
+
* @param branchName - Name for the new branch
|
|
1716
|
+
* @param fromEventId - Optional event ID to branch from
|
|
1717
|
+
* @returns Promise resolving to branch ID
|
|
1718
|
+
*/
|
|
1719
|
+
branch(branchName: string, fromEventId?: string): Promise<string>;
|
|
1720
|
+
/**
|
|
1721
|
+
* Switch to a different branch or checkout to a specific event
|
|
1722
|
+
* @param target - Branch name or event ID to checkout
|
|
1723
|
+
* @param options - Optional configuration
|
|
1724
|
+
*/
|
|
1725
|
+
checkout(target: string, options?: {
|
|
1726
|
+
type?: "branch" | "event";
|
|
1727
|
+
sessionId?: string;
|
|
1728
|
+
}): Promise<void>;
|
|
1729
|
+
/**
|
|
1730
|
+
* Delete a branch
|
|
1731
|
+
* @param branchName - Name of the branch to delete
|
|
1732
|
+
*/
|
|
1733
|
+
deleteBranch(branchName: string): Promise<void>;
|
|
1734
|
+
/**
|
|
1735
|
+
* List all branches
|
|
1736
|
+
* @returns Promise resolving to array of branch information
|
|
1737
|
+
*/
|
|
1738
|
+
listBranches(): Promise<BranchInfo[]>;
|
|
1739
|
+
/**
|
|
1740
|
+
* Get current branch name
|
|
1741
|
+
* @returns Promise resolving to current branch name
|
|
1742
|
+
*/
|
|
1743
|
+
getCurrentBranch(): Promise<string>;
|
|
1744
|
+
/**
|
|
1745
|
+
* Clean up branches, keeping only specified branches and the current active branch
|
|
1746
|
+
* @param keepBranches - Optional list of branch names to keep (in addition to active branch)
|
|
1747
|
+
*/
|
|
1748
|
+
cleanupBranches(keepBranches?: string[]): Promise<void>;
|
|
1749
|
+
/**
|
|
1750
|
+
* Close MongoDB connection (if managed by this instance)
|
|
1751
|
+
*/
|
|
1752
|
+
close(): Promise<void>;
|
|
1753
|
+
/**
|
|
1754
|
+
* Check if MongoDB connection is ready
|
|
1755
|
+
*/
|
|
1756
|
+
isReady(): boolean;
|
|
1757
|
+
/**
|
|
1758
|
+
* Get database instance (for advanced usage)
|
|
1759
|
+
*/
|
|
1760
|
+
getDatabase(): Db;
|
|
1761
|
+
/**
|
|
1762
|
+
* Ensure MongoDB is initialized before operations
|
|
1763
|
+
*/
|
|
1764
|
+
private ensureInitialized;
|
|
1765
|
+
}
|
|
1766
|
+
|
|
1767
|
+
/**
|
|
1768
|
+
* Document Converter
|
|
1769
|
+
*
|
|
1770
|
+
* Converts between memory events and database documents
|
|
1771
|
+
*/
|
|
1772
|
+
|
|
1773
|
+
declare class DocumentConverter {
|
|
1774
|
+
/**
|
|
1775
|
+
* Convert memory event to database document
|
|
1776
|
+
*/
|
|
1777
|
+
toDocument(event: IMemoryEvent, sessionId: string, branchPath: string): Omit<BaseMemoryDocument, "_id">;
|
|
1778
|
+
/**
|
|
1779
|
+
* Convert database document to memory event
|
|
1780
|
+
*/
|
|
1781
|
+
fromDocument(doc: BaseMemoryDocument): IMemoryEvent;
|
|
1782
|
+
}
|
|
1783
|
+
|
|
1784
|
+
/**
|
|
1785
|
+
* @fileoverview TypeORM memory implementation for persistent storage
|
|
1786
|
+
*
|
|
1787
|
+
* TypeORM Memory implementation providing persistent storage using TypeORM
|
|
1788
|
+
* with support for CRUD operations, session management, branch isolation,
|
|
1789
|
+
* and soft delete using cursor-based approach. Supports multiple databases
|
|
1790
|
+
* including MySQL, PostgreSQL, and SQLite.
|
|
1791
|
+
*
|
|
1792
|
+
* @example
|
|
1793
|
+
* ```typescript
|
|
1794
|
+
* import { DataSource } from 'typeorm';
|
|
1795
|
+
*
|
|
1796
|
+
* const dataSource = new DataSource({
|
|
1797
|
+
* type: 'sqlite',
|
|
1798
|
+
* database: ':memory:',
|
|
1799
|
+
* entities: [MemoryEvent, MemoryState, MemorySummary],
|
|
1800
|
+
* synchronize: true
|
|
1801
|
+
* });
|
|
1802
|
+
* await dataSource.initialize();
|
|
1803
|
+
*
|
|
1804
|
+
* const memory = new TypeORMMemory({
|
|
1805
|
+
* dataSource,
|
|
1806
|
+
* sessionId: 'user-session-123',
|
|
1807
|
+
* eventTableName: 'memory_events'
|
|
1808
|
+
* });
|
|
1809
|
+
*
|
|
1810
|
+
* await memory.add({
|
|
1811
|
+
* message: { id: '1', role: 'user', content: 'Hello' },
|
|
1812
|
+
* state: { userId: 'user123' }
|
|
1813
|
+
* });
|
|
1814
|
+
*
|
|
1815
|
+
* const events = await memory.list({ limit: 10 });
|
|
1816
|
+
* ```
|
|
1817
|
+
*/
|
|
1818
|
+
|
|
1819
|
+
/**
|
|
1820
|
+
* Configuration options for TypeORMMemory
|
|
1821
|
+
*/
|
|
1822
|
+
interface TypeORMMemoryConfig extends IBaseMemoryOptions {
|
|
1823
|
+
dataSource: DataSource;
|
|
1824
|
+
eventTableName?: string;
|
|
1825
|
+
stateTableName?: string;
|
|
1826
|
+
summaryTableName?: string;
|
|
1827
|
+
sessionId?: string;
|
|
1828
|
+
enableContextManagement?: boolean;
|
|
1829
|
+
documentConverter?: DocumentConverter;
|
|
1830
|
+
}
|
|
1831
|
+
/**
|
|
1832
|
+
* TypeORM Memory implementation for persistent storage
|
|
1833
|
+
*
|
|
1834
|
+
* Provides persistent storage using TypeORM with support for
|
|
1835
|
+
* session-based memory management, CRUD operations, branch isolation, optional context engineering,
|
|
1836
|
+
* and efficient soft delete using cursor-based approach.
|
|
1837
|
+
*/
|
|
1838
|
+
declare class TypeORMMemory extends BaseMemory {
|
|
1839
|
+
/** Session identifier for memory isolation */
|
|
1840
|
+
sessionId: string;
|
|
1841
|
+
/** Whether to enable automatic context management */
|
|
1842
|
+
private enableContextManagement;
|
|
1843
|
+
private dataSource;
|
|
1844
|
+
private collectionManager;
|
|
1845
|
+
private documentConverter;
|
|
1846
|
+
private stateManager;
|
|
1847
|
+
private cursorManager;
|
|
1848
|
+
private branchManager;
|
|
1849
|
+
private summaryManager;
|
|
1850
|
+
/**
|
|
1851
|
+
* Creates a new TypeORMMemory instance
|
|
1852
|
+
* @param config - Configuration options
|
|
1853
|
+
* @param config.dataSource - TypeORM DataSource instance
|
|
1854
|
+
* @param config.eventTableName - Table name for storing events (default: 'memory_events')
|
|
1855
|
+
* @param config.summaryTableName - Table name for storing summaries (default: 'memory_summaries')
|
|
1856
|
+
* @param config.stateTableName - Table name for storing session state (default: 'memory_state')
|
|
1857
|
+
* @param config.sessionId - Unique session identifier (default: 'default')
|
|
1858
|
+
* @param config.enableContextManagement - Whether to automatically manage context (default: true if thresholds provided)
|
|
1859
|
+
*/
|
|
1860
|
+
constructor(config: TypeORMMemoryConfig);
|
|
1861
|
+
private _initializationPromise;
|
|
1862
|
+
/**
|
|
1863
|
+
* Initialize entities and managers asynchronously
|
|
1864
|
+
*/
|
|
1865
|
+
private _initialize;
|
|
1866
|
+
/**
|
|
1867
|
+
* Initialize entities based on table names and ensure DataSource uses them
|
|
1868
|
+
* This approach ensures that the same entity classes are used for both
|
|
1869
|
+
* DataSource registration and repository creation
|
|
1870
|
+
*/
|
|
1871
|
+
private _initializeEntities;
|
|
1872
|
+
/**
|
|
1873
|
+
* Initialize all manager instances
|
|
1874
|
+
*/
|
|
1875
|
+
private _initializeManagers;
|
|
1876
|
+
/**
|
|
1877
|
+
* Add a single event to TypeORM database
|
|
1878
|
+
* @param event - The memory event to store
|
|
1879
|
+
* @param options - Optional session options
|
|
1880
|
+
*/
|
|
1881
|
+
add(event: IMemoryEvent, options?: AddOptions): Promise<void>;
|
|
1882
|
+
/**
|
|
1883
|
+
* Add multiple events efficiently
|
|
1884
|
+
* @param list - Array of memory events to store
|
|
1885
|
+
* @param options - Optional session options
|
|
1886
|
+
*/
|
|
1887
|
+
addList(list: IMemoryEvent[], options?: AddOptions): Promise<void>;
|
|
1888
|
+
/**
|
|
1889
|
+
* Returns events from TypeORM database with filtering, pagination, and token limiting
|
|
1890
|
+
* Respects branch isolation using branch path and snapshot time
|
|
1891
|
+
* @param options - Optional filtering and pagination options
|
|
1892
|
+
* @returns Promise resolving to filtered array of memory events
|
|
1893
|
+
*/
|
|
1894
|
+
list(options?: ListOptions): Promise<IMemoryEvent[]>;
|
|
1895
|
+
/**
|
|
1896
|
+
* Update an event in TypeORM database
|
|
1897
|
+
* @param params - Update parameters
|
|
1898
|
+
*/
|
|
1899
|
+
update(params: {
|
|
1900
|
+
sessionId?: string;
|
|
1901
|
+
event: IMemoryEvent;
|
|
1902
|
+
}): Promise<void>;
|
|
1903
|
+
/**
|
|
1904
|
+
* Delete an event from TypeORM database
|
|
1905
|
+
* @param idOrIndex - Message ID (string) to delete
|
|
1906
|
+
* @param options - Optional session options
|
|
1907
|
+
*/
|
|
1908
|
+
delete(idOrIndex: string | number, options?: DeleteOptions): Promise<void>;
|
|
1909
|
+
/**
|
|
1910
|
+
* Clear all events from TypeORM database
|
|
1911
|
+
* @param options - Optional session options. If sessionId provided, clears only that session using soft delete.
|
|
1912
|
+
*/
|
|
1913
|
+
clear(options?: ClearOptions): Promise<void>;
|
|
1914
|
+
/**
|
|
1915
|
+
* Check if the session contains any events (respecting soft delete)
|
|
1916
|
+
* @param options - Optional session options
|
|
1917
|
+
* @returns Promise resolving to true if no events are stored
|
|
1918
|
+
*/
|
|
1919
|
+
isEmpty(options?: {
|
|
1920
|
+
sessionId?: string;
|
|
1921
|
+
}): Promise<boolean>;
|
|
1922
|
+
/**
|
|
1923
|
+
* Get the current number of events stored in the session (respecting soft delete)
|
|
1924
|
+
* @param options - Optional session options
|
|
1925
|
+
* @returns Promise resolving to the count of stored events
|
|
1926
|
+
*/
|
|
1927
|
+
getCount(options?: {
|
|
1928
|
+
sessionId?: string;
|
|
1929
|
+
}): Promise<number>;
|
|
1930
|
+
/**
|
|
1931
|
+
* Retrieve events using content-based search
|
|
1932
|
+
* @param query - Search query text to match against message content
|
|
1933
|
+
* @param options - Optional session options
|
|
1934
|
+
* @returns Promise resolving to array of matching events
|
|
1935
|
+
*/
|
|
1936
|
+
retrieve(query: string, options?: RetrieveOptions): Promise<IMemoryEvent[]>;
|
|
1937
|
+
/**
|
|
1938
|
+
* Get summaries for a session
|
|
1939
|
+
* @param sessionId - Optional session identifier
|
|
1940
|
+
* @returns Array of structured summaries
|
|
1941
|
+
*/
|
|
1942
|
+
getSummaries(sessionId?: string): Promise<StructuredSummary[]>;
|
|
1943
|
+
/**
|
|
1944
|
+
* Store summary implementation
|
|
1945
|
+
* @param params - Summary storage parameters
|
|
1946
|
+
*/
|
|
1947
|
+
protected storeSummary(params: {
|
|
1948
|
+
sessionId?: string;
|
|
1949
|
+
summary: StructuredSummary;
|
|
1950
|
+
}): Promise<void>;
|
|
1951
|
+
/**
|
|
1952
|
+
* Clear summarized events implementation using soft delete
|
|
1953
|
+
* Sets cursor to hide old events without actually deleting them
|
|
1954
|
+
* @param params - Clear parameters
|
|
1955
|
+
*/
|
|
1956
|
+
protected clearSummarizedEvents(params: {
|
|
1957
|
+
sessionId: string | undefined;
|
|
1958
|
+
recentToKeep: number;
|
|
1959
|
+
}): Promise<void>;
|
|
1960
|
+
/**
|
|
1961
|
+
* Create a new branch from current session state
|
|
1962
|
+
* @param branchName - Name for the new branch
|
|
1963
|
+
* @param fromEventId - Optional event ID to branch from
|
|
1964
|
+
* @returns Promise resolving to branch ID
|
|
1965
|
+
*/
|
|
1966
|
+
branch(branchName: string, fromEventId?: string): Promise<string>;
|
|
1967
|
+
/**
|
|
1968
|
+
* Switch to a different branch or checkout to a specific event
|
|
1969
|
+
* @param target - Branch name or event ID to checkout
|
|
1970
|
+
* @param options - Optional configuration
|
|
1971
|
+
*/
|
|
1972
|
+
checkout(target: string, options?: {
|
|
1973
|
+
type?: "branch" | "event";
|
|
1974
|
+
sessionId?: string;
|
|
1975
|
+
}): Promise<void>;
|
|
1976
|
+
/**
|
|
1977
|
+
* Delete a branch
|
|
1978
|
+
* @param branchName - Name of the branch to delete
|
|
1979
|
+
*/
|
|
1980
|
+
deleteBranch(branchName: string): Promise<void>;
|
|
1981
|
+
/**
|
|
1982
|
+
* List all branches
|
|
1983
|
+
* @returns Promise resolving to array of branch information
|
|
1984
|
+
*/
|
|
1985
|
+
listBranches(): Promise<BranchInfo[]>;
|
|
1986
|
+
/**
|
|
1987
|
+
* Get current branch name
|
|
1988
|
+
* @returns Promise resolving to current branch name
|
|
1989
|
+
*/
|
|
1990
|
+
getCurrentBranch(): Promise<string>;
|
|
1991
|
+
/**
|
|
1992
|
+
* Clean up branches, keeping only specified branches and the current active branch
|
|
1993
|
+
* @param keepBranches - Optional list of branch names to keep (in addition to active branch)
|
|
1994
|
+
*/
|
|
1995
|
+
cleanupBranches(keepBranches?: string[]): Promise<void>;
|
|
1996
|
+
/**
|
|
1997
|
+
* Check if TypeORM connection is ready
|
|
1998
|
+
*/
|
|
1999
|
+
isReady(): boolean;
|
|
2000
|
+
/**
|
|
2001
|
+
* Close TypeORM DataSource connection and clean up resources
|
|
2002
|
+
* This method should be called when the memory instance is no longer needed
|
|
2003
|
+
* to properly release database connections and prevent connection leaks
|
|
2004
|
+
*
|
|
2005
|
+
* @throws Error if there's an issue closing the connection
|
|
2006
|
+
*
|
|
2007
|
+
* @example
|
|
2008
|
+
* ```typescript
|
|
2009
|
+
* const memory = new TypeORMMemory({ dataSource, sessionId: 'test' });
|
|
2010
|
+
* // ... use memory
|
|
2011
|
+
* await memory.close(); // Clean up when done
|
|
2012
|
+
* ```
|
|
2013
|
+
*/
|
|
2014
|
+
close(): Promise<void>;
|
|
2015
|
+
/**
|
|
2016
|
+
* Get DataSource instance (for advanced usage)
|
|
2017
|
+
*/
|
|
2018
|
+
getDataSource(): DataSource;
|
|
2019
|
+
/**
|
|
2020
|
+
* Ensure TypeORM is initialized before operations
|
|
2021
|
+
*/
|
|
2022
|
+
ensureInitialized(): Promise<void>;
|
|
2023
|
+
/**
|
|
2024
|
+
* Create a TypeORM DataSource configuration
|
|
2025
|
+
*/
|
|
2026
|
+
static createDataSource(config: {
|
|
2027
|
+
type: "mysql" | "postgres" | "sqlite";
|
|
2028
|
+
host?: string;
|
|
2029
|
+
port?: number;
|
|
2030
|
+
username?: string;
|
|
2031
|
+
password?: string;
|
|
2032
|
+
database?: string;
|
|
2033
|
+
filename?: string;
|
|
2034
|
+
synchronize?: boolean;
|
|
2035
|
+
logging?: boolean;
|
|
2036
|
+
}): DataSource;
|
|
2037
|
+
}
|
|
2038
|
+
|
|
2039
|
+
/**
|
|
2040
|
+
* @fileoverview MySQL Memory implementation for persistent storage
|
|
2041
|
+
*
|
|
2042
|
+
* MySQL Memory implementation providing persistent storage using TypeORM
|
|
2043
|
+
* with MySQL-specific optimizations. Extends TypeORMMemory to provide
|
|
2044
|
+
* all advanced features including CRUD operations, session management,
|
|
2045
|
+
* branch isolation, context engineering, and soft delete using cursor-based approach.
|
|
2046
|
+
*
|
|
2047
|
+
* @example
|
|
2048
|
+
* ```typescript
|
|
2049
|
+
* // Basic usage
|
|
2050
|
+
* const memory = MySQLMemory.create({
|
|
2051
|
+
* host: 'localhost',
|
|
2052
|
+
* port: 3306,
|
|
2053
|
+
* username: 'root',
|
|
2054
|
+
* password: 'password',
|
|
2055
|
+
* database: 'myapp',
|
|
2056
|
+
* sessionId: 'user-session-123'
|
|
2057
|
+
* });
|
|
2058
|
+
*
|
|
2059
|
+
* await memory.add({
|
|
2060
|
+
* message: { id: '1', role: 'user', content: 'Hello' },
|
|
2061
|
+
* state: { userId: 'user123' }
|
|
2062
|
+
* });
|
|
2063
|
+
*
|
|
2064
|
+
* const events = await memory.list({ limit: 10 });
|
|
2065
|
+
*
|
|
2066
|
+
* // Clean up when done
|
|
2067
|
+
* await memory.close();
|
|
2068
|
+
* ```
|
|
2069
|
+
*
|
|
2070
|
+
* @example
|
|
2071
|
+
* ```typescript
|
|
2072
|
+
* // Advanced configuration with connection pooling and context management
|
|
2073
|
+
* const memory = new MySQLMemory({
|
|
2074
|
+
* connection: {
|
|
2075
|
+
* host: 'localhost',
|
|
2076
|
+
* port: 3306,
|
|
2077
|
+
* username: 'root',
|
|
2078
|
+
* password: 'password',
|
|
2079
|
+
* database: 'myapp',
|
|
2080
|
+
* pool: {
|
|
2081
|
+
* max: 20,
|
|
2082
|
+
* min: 5,
|
|
2083
|
+
* acquireTimeoutMillis: 30000,
|
|
2084
|
+
* idleTimeoutMillis: 300000
|
|
2085
|
+
* }
|
|
2086
|
+
* },
|
|
2087
|
+
* sessionId: 'user-session-123',
|
|
2088
|
+
* eventTableName: 'custom_events',
|
|
2089
|
+
* stateTableName:
|
|
2090
|
+
* maxTokens: 8000,
|
|
2091
|
+
* targetTokens: 6000,
|
|
2092
|
+
* summaryTokens: 1000
|
|
2093
|
+
* },
|
|
2094
|
+
* enableContextManagement: true
|
|
2095
|
+
* });
|
|
2096
|
+
* ```
|
|
2097
|
+
*
|
|
2098
|
+
* @example
|
|
2099
|
+
* ```typescript
|
|
2100
|
+
* // Using connection string
|
|
2101
|
+
* const memory = MySQLMemory.fromConnectionString(
|
|
2102
|
+
* 'mysql://user:pass@localhost:3306/mydb',
|
|
2103
|
+
* 'session-123',
|
|
2104
|
+
* {
|
|
2105
|
+
* thresholds: { maxTokens: 10000 },
|
|
2106
|
+
* enableContextManagement: true
|
|
2107
|
+
* }
|
|
2108
|
+
* );
|
|
2109
|
+
* ```
|
|
2110
|
+
*/
|
|
2111
|
+
|
|
2112
|
+
/**
|
|
2113
|
+
* MySQL-specific connection configuration
|
|
2114
|
+
* Extends TypeORM's MysqlConnectionOptions with additional AG-Kit specific options
|
|
2115
|
+
*/
|
|
2116
|
+
interface MySQLConnectionConfig extends Omit<MysqlConnectionOptions, "type" | "entities" | "synchronize" | "logging"> {
|
|
2117
|
+
/** MySQL host */
|
|
2118
|
+
host: string;
|
|
2119
|
+
/** MySQL port (default: 3306) */
|
|
2120
|
+
port?: number;
|
|
2121
|
+
/** MySQL username */
|
|
2122
|
+
username: string;
|
|
2123
|
+
/** MySQL password */
|
|
2124
|
+
password: string;
|
|
2125
|
+
/** MySQL database name */
|
|
2126
|
+
database: string;
|
|
2127
|
+
}
|
|
2128
|
+
/**
|
|
2129
|
+
* Configuration options for MySQLMemory
|
|
2130
|
+
*/
|
|
2131
|
+
type MySQLMemoryConfig = {
|
|
2132
|
+
/** Database connection configuration */
|
|
2133
|
+
connection: MySQLConnectionConfig;
|
|
2134
|
+
/** Whether to automatically initialize database schema (default: true) */
|
|
2135
|
+
autoInitialize?: boolean;
|
|
2136
|
+
/** Enable SQL query logging for debugging (default: false) */
|
|
2137
|
+
enableLogging?: boolean;
|
|
2138
|
+
} & Omit<ConstructorParameters<typeof TypeORMMemory>[0], "dataSource">;
|
|
2139
|
+
/**
|
|
2140
|
+
* MySQL Memory implementation using TypeORM
|
|
2141
|
+
*
|
|
2142
|
+
* This class extends TypeORMMemory and provides MySQL-specific configuration
|
|
2143
|
+
* and optimizations while leveraging all the advanced features including:
|
|
2144
|
+
* - Session-based memory isolation
|
|
2145
|
+
* - CRUD operations with soft delete
|
|
2146
|
+
* - Branch management and checkout
|
|
2147
|
+
* - Context engineering and automatic summarization
|
|
2148
|
+
* - Connection pooling and performance optimization
|
|
2149
|
+
* - Transaction support
|
|
2150
|
+
* - Schema synchronization
|
|
2151
|
+
* - Migration support
|
|
2152
|
+
* - MySQL-specific query optimizations
|
|
2153
|
+
*/
|
|
2154
|
+
declare class MySQLMemory extends TypeORMMemory {
|
|
2155
|
+
/**
|
|
2156
|
+
* Creates a new MySQLMemory instance
|
|
2157
|
+
* @param config - MySQL-specific configuration options
|
|
2158
|
+
*/
|
|
2159
|
+
constructor(config: MySQLMemoryConfig);
|
|
2160
|
+
/**
|
|
2161
|
+
* Create a MySQL DataSource configuration with optimizations
|
|
2162
|
+
*/
|
|
2163
|
+
private static createMySQLDataSource;
|
|
2164
|
+
/**
|
|
2165
|
+
* Create a MySQL Memory instance with connection string
|
|
2166
|
+
*
|
|
2167
|
+
* @param connectionString - MySQL connection string (mysql://username:password@host:port/database)
|
|
2168
|
+
* @param sessionId - Session identifier for memory isolation
|
|
2169
|
+
* @param options - Additional configuration options
|
|
2170
|
+
* @returns MySQLMemory instance
|
|
2171
|
+
* 'custom_state',
|
|
2172
|
+
* summaryTableName: 'custom_summaries',
|
|
2173
|
+
* thresholds: {
|
|
2174
|
+
* @example
|
|
2175
|
+
* ```typescript
|
|
2176
|
+
* const memory = MySQLMemory.fromConnectionString(
|
|
2177
|
+
* 'mysql://user:pass@localhost:3306/mydb',
|
|
2178
|
+
* 'session-123',
|
|
2179
|
+
* {
|
|
2180
|
+
* thresholds: { maxTokens: 10000 },
|
|
2181
|
+
* enableContextManagement: true
|
|
2182
|
+
* }
|
|
2183
|
+
* );
|
|
2184
|
+
* ```
|
|
2185
|
+
*/
|
|
2186
|
+
static fromConnectionString(connectionString: string, sessionId: string, options?: Partial<MySQLMemoryConfig>): MySQLMemory;
|
|
2187
|
+
/**
|
|
2188
|
+
* Create a MySQL Memory instance with simple configuration
|
|
2189
|
+
*
|
|
2190
|
+
* @param config - Simple configuration object
|
|
2191
|
+
* @returns MySQLMemory instance
|
|
2192
|
+
*
|
|
2193
|
+
* @example
|
|
2194
|
+
* ```typescript
|
|
2195
|
+
* const memory = MySQLMemory.create({
|
|
2196
|
+
* host: 'localhost',
|
|
2197
|
+
* port: 3306,
|
|
2198
|
+
* username: 'root',
|
|
2199
|
+
* password: 'password',
|
|
2200
|
+
* database: 'myapp',
|
|
2201
|
+
* sessionId: 'user-session-123'
|
|
2202
|
+
* });
|
|
2203
|
+
* ```
|
|
2204
|
+
*/
|
|
2205
|
+
static create(config: MySQLMemoryConfig): MySQLMemory;
|
|
2206
|
+
/**
|
|
2207
|
+
* Get MySQL-specific connection information
|
|
2208
|
+
* @returns Connection information object
|
|
2209
|
+
*/
|
|
2210
|
+
getConnectionInfo(): {
|
|
2211
|
+
host: string;
|
|
2212
|
+
port: number;
|
|
2213
|
+
database: string;
|
|
2214
|
+
username: string;
|
|
2215
|
+
};
|
|
2216
|
+
}
|
|
2217
|
+
|
|
2218
|
+
/**
|
|
2219
|
+
* Simplified TypeORM Adapter
|
|
2220
|
+
*
|
|
2221
|
+
* Simple adapter that works with TypeORM repositories
|
|
2222
|
+
*/
|
|
2223
|
+
|
|
2224
|
+
declare class TypeORMAdapter implements IDatabaseAdapter {
|
|
2225
|
+
/**
|
|
2226
|
+
* Get collection (for TypeORM, this is handled by CollectionManager)
|
|
2227
|
+
* This method is required by IDatabaseAdapter but not used directly
|
|
2228
|
+
*/
|
|
2229
|
+
getCollection(name: string): any;
|
|
2230
|
+
/**
|
|
2231
|
+
* Find documents using TypeORM repository
|
|
2232
|
+
*/
|
|
2233
|
+
find<T = any>(repository: Repository<any>, query: any, options?: QueryOptions): Promise<T[]>;
|
|
2234
|
+
/**
|
|
2235
|
+
* Check if query is a complex query from our query builder
|
|
2236
|
+
*/
|
|
2237
|
+
private isComplexQuery;
|
|
2238
|
+
/**
|
|
2239
|
+
* Handle complex queries with custom where clauses
|
|
2240
|
+
*/
|
|
2241
|
+
private handleComplexQuery;
|
|
2242
|
+
/**
|
|
2243
|
+
* Handle array queries (OR conditions)
|
|
2244
|
+
*/
|
|
2245
|
+
private handleArrayQuery;
|
|
2246
|
+
/**
|
|
2247
|
+
* Handle array queries containing complex queries
|
|
2248
|
+
*/
|
|
2249
|
+
private handleComplexArrayQuery;
|
|
2250
|
+
/**
|
|
2251
|
+
* Build OR conditions from mixed query array
|
|
2252
|
+
*/
|
|
2253
|
+
private buildOrConditions;
|
|
2254
|
+
/**
|
|
2255
|
+
* Process complex query condition
|
|
2256
|
+
*/
|
|
2257
|
+
private processComplexCondition;
|
|
2258
|
+
/**
|
|
2259
|
+
* Process simple query condition
|
|
2260
|
+
*/
|
|
2261
|
+
private processSimpleCondition;
|
|
2262
|
+
/**
|
|
2263
|
+
* Handle simple array queries using TypeORM's built-in OR handling
|
|
2264
|
+
*/
|
|
2265
|
+
private handleSimpleArrayQuery;
|
|
2266
|
+
/**
|
|
2267
|
+
* Handle simple single queries
|
|
2268
|
+
*/
|
|
2269
|
+
private handleSimpleQuery;
|
|
2270
|
+
/**
|
|
2271
|
+
* Apply query options to QueryBuilder
|
|
2272
|
+
*/
|
|
2273
|
+
private applyQueryOptions;
|
|
2274
|
+
/**
|
|
2275
|
+
* Apply query options to FindManyOptions
|
|
2276
|
+
*/
|
|
2277
|
+
private applyFindOptions;
|
|
2278
|
+
/**
|
|
2279
|
+
* Find one document
|
|
2280
|
+
*/
|
|
2281
|
+
findOne<T = any>(repository: Repository<any>, query: any): Promise<T | null>;
|
|
2282
|
+
/**
|
|
2283
|
+
* Insert one document
|
|
2284
|
+
*/
|
|
2285
|
+
insertOne<T = any>(repository: Repository<any>, document: Partial<T>): Promise<T>;
|
|
2286
|
+
/**
|
|
2287
|
+
* Insert many documents
|
|
2288
|
+
*/
|
|
2289
|
+
insertMany<T = any>(repository: Repository<any>, documents: Partial<T>[]): Promise<{
|
|
2290
|
+
insertedCount: number;
|
|
2291
|
+
}>;
|
|
2292
|
+
/**
|
|
2293
|
+
* Update one document
|
|
2294
|
+
*/
|
|
2295
|
+
updateOne(repository: Repository<any>, filter: any, update: any): Promise<{
|
|
2296
|
+
modifiedCount: number;
|
|
2297
|
+
}>;
|
|
2298
|
+
/**
|
|
2299
|
+
* Delete one document
|
|
2300
|
+
*/
|
|
2301
|
+
deleteOne(repository: Repository<any>, filter: any): Promise<{
|
|
2302
|
+
deletedCount: number;
|
|
2303
|
+
}>;
|
|
2304
|
+
/**
|
|
2305
|
+
* Delete many documents
|
|
2306
|
+
*/
|
|
2307
|
+
deleteMany(repository: Repository<any>, filter: any): Promise<{
|
|
2308
|
+
deletedCount: number;
|
|
2309
|
+
}>;
|
|
2310
|
+
/**
|
|
2311
|
+
* Execute delete operation with proper query handling
|
|
2312
|
+
*/
|
|
2313
|
+
private executeDelete;
|
|
2314
|
+
/**
|
|
2315
|
+
* Count documents
|
|
2316
|
+
*/
|
|
2317
|
+
count(repository: Repository<any>, query: any): Promise<number>;
|
|
2318
|
+
/**
|
|
2319
|
+
* Create query builder
|
|
2320
|
+
*/
|
|
2321
|
+
createQueryBuilder(): IQueryBuilder;
|
|
2322
|
+
/**
|
|
2323
|
+
* Create update builder
|
|
2324
|
+
*/
|
|
2325
|
+
createUpdateBuilder(): IUpdateBuilder;
|
|
2326
|
+
}
|
|
2327
|
+
|
|
2328
|
+
/**
|
|
2329
|
+
* TypeORM Query Builder Implementation
|
|
2330
|
+
*
|
|
2331
|
+
* Provides type-safe query building for TypeORM with SQL injection protection
|
|
2332
|
+
*/
|
|
2333
|
+
|
|
2334
|
+
declare class TypeORMQueryBuilder implements IQueryBuilder {
|
|
2335
|
+
private conditions;
|
|
2336
|
+
private parameters;
|
|
2337
|
+
private paramCounter;
|
|
2338
|
+
private orConditions;
|
|
2339
|
+
where(field: string, operator: any, value: any): this;
|
|
2340
|
+
and(field: string, operator: any, value: any): this;
|
|
2341
|
+
or(conditions: IQueryBuilder[]): this;
|
|
2342
|
+
in(field: string, values: any[]): this;
|
|
2343
|
+
regex(field: string, pattern: string, options?: string): this;
|
|
2344
|
+
clone(): IQueryBuilder;
|
|
2345
|
+
build(): any;
|
|
2346
|
+
private addCondition;
|
|
2347
|
+
}
|
|
2348
|
+
declare class TypeORMUpdateBuilder implements IUpdateBuilder {
|
|
2349
|
+
private updates;
|
|
2350
|
+
set(field: string, value: any): this;
|
|
2351
|
+
setMultiple(updates: Record<string, any>): this;
|
|
2352
|
+
unset(field: string): this;
|
|
2353
|
+
increment(field: string, value: number): this;
|
|
2354
|
+
build(): any;
|
|
2355
|
+
}
|
|
2356
|
+
|
|
2357
|
+
/**
|
|
2358
|
+
* TypeORM Entity for Memory Events using Decorator Syntax
|
|
2359
|
+
*
|
|
2360
|
+
* More standard TypeORM approach with better IDE support and type safety
|
|
2361
|
+
*/
|
|
2362
|
+
|
|
2363
|
+
/**
|
|
2364
|
+
* Memory Event Entity using TypeORM decorators
|
|
2365
|
+
*
|
|
2366
|
+
* This approach provides:
|
|
2367
|
+
* - Better IDE support and autocomplete
|
|
2368
|
+
* - Type safety at compile time
|
|
2369
|
+
* - Standard TypeORM patterns
|
|
2370
|
+
* - Wider compatibility across databases
|
|
2371
|
+
*/
|
|
2372
|
+
declare class MemoryEvent implements BaseMemoryDocument {
|
|
2373
|
+
_id: string;
|
|
2374
|
+
sessionId: string;
|
|
2375
|
+
messageId: string;
|
|
2376
|
+
branchPath: string;
|
|
2377
|
+
role: string;
|
|
2378
|
+
content: string;
|
|
2379
|
+
toolCalls?: any[];
|
|
2380
|
+
toolCallId?: string;
|
|
2381
|
+
state: Record<string, any>;
|
|
2382
|
+
createdAt: Date;
|
|
2383
|
+
updatedAt: Date;
|
|
2384
|
+
}
|
|
2385
|
+
type MemoryEventEntity = MemoryEvent;
|
|
2386
|
+
|
|
2387
|
+
/**
|
|
2388
|
+
* Memory entity interface representing a stored memory item
|
|
2389
|
+
*/
|
|
2390
|
+
interface MemoryEntity {
|
|
2391
|
+
/** Unique identifier for the memory */
|
|
2392
|
+
id: string;
|
|
2393
|
+
/** Memory strategy/category for classification */
|
|
2394
|
+
strategy: string;
|
|
2395
|
+
/** Role of the message creator */
|
|
2396
|
+
role?: "user" | "assistant";
|
|
2397
|
+
/** Memory content text */
|
|
2398
|
+
content: string;
|
|
2399
|
+
/** Additional metadata for the memory */
|
|
2400
|
+
metadata: Record<string, any>;
|
|
2401
|
+
/** Creation timestamp */
|
|
2402
|
+
createdAt: Date;
|
|
2403
|
+
/** Last update timestamp */
|
|
2404
|
+
updatedAt?: Date;
|
|
2405
|
+
}
|
|
2406
|
+
/**
|
|
2407
|
+
* Memory query interface for searching and filtering memories
|
|
2408
|
+
*/
|
|
2409
|
+
interface MemoryQuery {
|
|
2410
|
+
/** Semantic query text for content matching */
|
|
2411
|
+
query?: string;
|
|
2412
|
+
/** Strategy filter - single strategy or array of strategies */
|
|
2413
|
+
strategy?: string | string[];
|
|
2414
|
+
/** Maximum number of results to return */
|
|
2415
|
+
limit?: number;
|
|
2416
|
+
/** Number of results to skip for pagination */
|
|
2417
|
+
offset?: number;
|
|
2418
|
+
/** Sorting criteria with field names and directions */
|
|
2419
|
+
orderBy?: Record<string, 1 | -1>;
|
|
2420
|
+
/** Custom filtering conditions - implementers can add any query conditions */
|
|
2421
|
+
[key: string]: any;
|
|
2422
|
+
}
|
|
2423
|
+
/**
|
|
2424
|
+
* @fileoverview Abstract base class for long-term memory implementations
|
|
2425
|
+
*
|
|
2426
|
+
* This base class defines the core interface and common functionality for long-term memory systems.
|
|
2427
|
+
* Concrete implementations can be based on different storage backends such as vector databases,
|
|
2428
|
+
* graph databases, or other persistent storage solutions.
|
|
2429
|
+
*
|
|
2430
|
+
* @example
|
|
2431
|
+
* ```typescript
|
|
2432
|
+
* class CustomLongTermMemory extends BaseLongTermMemory {
|
|
2433
|
+
* async record(memory: MemoryEntity): Promise<void> {
|
|
2434
|
+
* // Custom implementation
|
|
2435
|
+
* }
|
|
2436
|
+
* // ... implement other required methods
|
|
2437
|
+
* }
|
|
2438
|
+
* ```
|
|
2439
|
+
*/
|
|
2440
|
+
declare abstract class BaseLongTermMemory {
|
|
2441
|
+
/** Configuration object for the memory implementation */
|
|
2442
|
+
protected config: any;
|
|
2443
|
+
/**
|
|
2444
|
+
* Creates a new BaseLongTermMemory instance
|
|
2445
|
+
* @param config - Configuration object for the memory implementation
|
|
2446
|
+
*/
|
|
2447
|
+
constructor(config?: any);
|
|
2448
|
+
/**
|
|
2449
|
+
* Record a new memory entity
|
|
2450
|
+
* @param memory - The memory entity to store
|
|
2451
|
+
*/
|
|
2452
|
+
abstract record(memory: MemoryEntity): Promise<void>;
|
|
2453
|
+
/**
|
|
2454
|
+
* Record multiple memory entities in batch
|
|
2455
|
+
* @param memories - Array of memory entities to store
|
|
2456
|
+
*/
|
|
2457
|
+
abstract recordBatch(memories: MemoryEntity[]): Promise<void>;
|
|
2458
|
+
/**
|
|
2459
|
+
* Retrieve memories based on query conditions
|
|
2460
|
+
* @param query - Query conditions for filtering and searching
|
|
2461
|
+
*/
|
|
2462
|
+
abstract retrieve(query: MemoryQuery): Promise<MemoryEntity[]>;
|
|
2463
|
+
/**
|
|
2464
|
+
* Delete memory by ID or query conditions
|
|
2465
|
+
* @param memoryId - Memory ID (string) or query conditions (MemoryQuery)
|
|
2466
|
+
*/
|
|
2467
|
+
abstract delete(memoryId: string | MemoryQuery): Promise<void>;
|
|
2468
|
+
/**
|
|
2469
|
+
* Update an existing memory entity
|
|
2470
|
+
* @param memoryId - ID of the memory to update
|
|
2471
|
+
* @param updates - Partial memory entity with fields to update
|
|
2472
|
+
*/
|
|
2473
|
+
abstract update(memoryId: string, updates: Partial<MemoryEntity>): Promise<void>;
|
|
2474
|
+
/**
|
|
2475
|
+
* Clear all memories for a specific strategy or all memories
|
|
2476
|
+
* @param strategy - Optional strategy filter; if not specified, clears all memories
|
|
2477
|
+
*/
|
|
2478
|
+
abstract clear(strategy?: string): Promise<void>;
|
|
2479
|
+
/**
|
|
2480
|
+
* Extract and record memories from conversation messages
|
|
2481
|
+
* @param messages - Array of conversation messages to analyze
|
|
2482
|
+
* @param context - Additional context information for extraction
|
|
2483
|
+
* @returns Promise resolving to array of extracted memory entities
|
|
2484
|
+
*/
|
|
2485
|
+
abstract extractAndRecord(messages: Message[], context: Record<string, any>): Promise<MemoryEntity[]>;
|
|
2486
|
+
/**
|
|
2487
|
+
* Perform semantic search on stored memories
|
|
2488
|
+
* @param query - Search query text for semantic matching
|
|
2489
|
+
* @param options - Optional search parameters and filters
|
|
2490
|
+
* @returns Promise resolving to array of semantically similar memories
|
|
2491
|
+
*/
|
|
2492
|
+
abstract semanticSearch(query: string, options?: MemoryQuery): Promise<MemoryEntity[]>;
|
|
2493
|
+
/**
|
|
2494
|
+
* Get memories related to a specific memory entity
|
|
2495
|
+
* @param memoryId - ID of the memory to find relations for
|
|
2496
|
+
* @param depth - Relationship depth to traverse, defaults to 1
|
|
2497
|
+
* @returns Promise resolving to array of related memory entities
|
|
2498
|
+
*/
|
|
2499
|
+
abstract getRelatedMemories(memoryId: string, depth?: number): Promise<MemoryEntity[]>;
|
|
2500
|
+
/**
|
|
2501
|
+
* Consolidate and optimize stored memories
|
|
2502
|
+
* Includes operations like deduplication, merging, decay, and reinforcement
|
|
2503
|
+
* @returns Promise that resolves when consolidation is complete
|
|
2504
|
+
*/
|
|
2505
|
+
abstract consolidate(): Promise<void>;
|
|
2506
|
+
/**
|
|
2507
|
+
* Generate a unique memory ID
|
|
2508
|
+
* Implementers can override this method to customize ID generation strategy
|
|
2509
|
+
* @returns Unique memory identifier string
|
|
2510
|
+
*/
|
|
2511
|
+
protected generateMemoryId(): string;
|
|
2512
|
+
}
|
|
2513
|
+
|
|
2514
|
+
/**
|
|
2515
|
+
* TDAI 长期记忆配置接口
|
|
2516
|
+
*/
|
|
2517
|
+
interface TDAILongTermMemoryConfig {
|
|
2518
|
+
client: MemoryClient;
|
|
2519
|
+
sessionId?: string;
|
|
2520
|
+
}
|
|
2521
|
+
/**
|
|
2522
|
+
* 基于 TDAI 客户端的长期记忆实现
|
|
2523
|
+
*
|
|
2524
|
+
* 使用 TDAI 的 records API 来存储和管理长期记忆
|
|
2525
|
+
*/
|
|
2526
|
+
declare class TDAILongTermMemory extends BaseLongTermMemory {
|
|
2527
|
+
private client;
|
|
2528
|
+
private sessionId;
|
|
2529
|
+
constructor(config: TDAILongTermMemoryConfig);
|
|
2530
|
+
/**
|
|
2531
|
+
* 记录新的记忆实体
|
|
2532
|
+
*/
|
|
2533
|
+
record(memory: Pick<MemoryEntity, "content" | "strategy" | "metadata">): Promise<void>;
|
|
2534
|
+
/**
|
|
2535
|
+
* 批量记录记忆
|
|
2536
|
+
*/
|
|
2537
|
+
recordBatch(memories: MemoryEntity[]): Promise<void>;
|
|
2538
|
+
/**
|
|
2539
|
+
* 检索记忆
|
|
2540
|
+
*/
|
|
2541
|
+
retrieve(query: MemoryQuery): Promise<MemoryEntity[]>;
|
|
2542
|
+
/**
|
|
2543
|
+
* 删除记忆
|
|
2544
|
+
*/
|
|
2545
|
+
delete(recordId: string | MemoryQuery): Promise<void>;
|
|
2546
|
+
/**
|
|
2547
|
+
* 更新记忆
|
|
2548
|
+
*/
|
|
2549
|
+
update(memoryId: string, updates: Partial<MemoryEntity>): Promise<void>;
|
|
2550
|
+
/**
|
|
2551
|
+
* 语义搜索
|
|
2552
|
+
*/
|
|
2553
|
+
semanticSearch(query: string, options?: MemoryQuery): Promise<MemoryEntity[]>;
|
|
2554
|
+
/**
|
|
2555
|
+
* 清空指定策略的所有记忆
|
|
2556
|
+
*/
|
|
2557
|
+
clear(strategy?: string): Promise<void>;
|
|
2558
|
+
/**
|
|
2559
|
+
* 从对话中提取并记录记忆
|
|
2560
|
+
* TODO: 引入 LLM 进行提取
|
|
2561
|
+
*/
|
|
2562
|
+
extractAndRecord(messages: Message[], context: Record<string, any>): Promise<MemoryEntity[]>;
|
|
2563
|
+
/**
|
|
2564
|
+
* 获取相关记忆
|
|
2565
|
+
*/
|
|
2566
|
+
getRelatedMemories(memoryId: string, depth?: number): Promise<MemoryEntity[]>;
|
|
2567
|
+
/**
|
|
2568
|
+
* TODO: imply 记忆整理和优化
|
|
2569
|
+
* 查找重复记忆
|
|
2570
|
+
* 合并相似记忆
|
|
2571
|
+
* 删除过期记忆
|
|
2572
|
+
*/
|
|
2573
|
+
consolidate(): Promise<void>;
|
|
2574
|
+
}
|
|
2575
|
+
|
|
2576
|
+
type ClientOptions = ConstructorParameters<typeof MemoryClient$1>[0];
|
|
2577
|
+
/**
|
|
2578
|
+
* Mem0 Long Term Memory Configuration
|
|
2579
|
+
* Based on MemoryClient's ClientOptions with additional agent-specific options
|
|
2580
|
+
*/
|
|
2581
|
+
interface Mem0LongTermMemoryConfig extends ClientOptions {
|
|
2582
|
+
/** Default user ID for memory operations */
|
|
2583
|
+
userId?: string;
|
|
2584
|
+
/** Default agent ID for memory operations */
|
|
2585
|
+
agentId?: string;
|
|
2586
|
+
/** Default app ID for memory operations */
|
|
2587
|
+
appId?: string;
|
|
2588
|
+
}
|
|
2589
|
+
/**
|
|
2590
|
+
* Mem0LongTermMemory - Implementation using official Mem0 SDK
|
|
2591
|
+
*
|
|
2592
|
+
* This implementation provides intelligent memory management using Mem0's
|
|
2593
|
+
* advanced features including automatic memory extraction, semantic search,
|
|
2594
|
+
* and intelligent memory consolidation.
|
|
2595
|
+
*/
|
|
2596
|
+
declare class Mem0LongTermMemory extends BaseLongTermMemory {
|
|
2597
|
+
private client;
|
|
2598
|
+
private defaultOptions;
|
|
2599
|
+
constructor(config: Mem0LongTermMemoryConfig);
|
|
2600
|
+
/**
|
|
2601
|
+
* Record a new memory entity
|
|
2602
|
+
*/
|
|
2603
|
+
record(memory: MemoryEntity): Promise<void>;
|
|
2604
|
+
/**
|
|
2605
|
+
* Record multiple memories in batch
|
|
2606
|
+
*/
|
|
2607
|
+
recordBatch(memories: MemoryEntity[]): Promise<void>;
|
|
2608
|
+
/**
|
|
2609
|
+
* Retrieve memories based on query
|
|
2610
|
+
*/
|
|
2611
|
+
retrieve(query: MemoryQuery): Promise<MemoryEntity[]>;
|
|
2612
|
+
/**
|
|
2613
|
+
* Delete memory by ID or query
|
|
2614
|
+
*/
|
|
2615
|
+
delete(memoryId: string | MemoryQuery): Promise<void>;
|
|
2616
|
+
/**
|
|
2617
|
+
* Update memory content or metadata
|
|
2618
|
+
*/
|
|
2619
|
+
update(memoryId: string, updates: Partial<MemoryEntity>): Promise<void>;
|
|
2620
|
+
/**
|
|
2621
|
+
* Clear memories by strategy
|
|
2622
|
+
*/
|
|
2623
|
+
clear(strategy?: string): Promise<void>;
|
|
2624
|
+
/**
|
|
2625
|
+
* Extract and record memories from conversation messages
|
|
2626
|
+
*/
|
|
2627
|
+
extractAndRecord(messages: any[], context: Record<string, any>): Promise<MemoryEntity[]>;
|
|
2628
|
+
/**
|
|
2629
|
+
* Semantic search using Mem0's intelligent search
|
|
2630
|
+
*/
|
|
2631
|
+
semanticSearch(query: string, options?: MemoryQuery): Promise<MemoryEntity[]>;
|
|
2632
|
+
/**
|
|
2633
|
+
* Get related memories using Mem0's graph-based relationships
|
|
2634
|
+
*/
|
|
2635
|
+
getRelatedMemories(memoryId: string, depth?: number): Promise<MemoryEntity[]>;
|
|
2636
|
+
/**
|
|
2637
|
+
* Consolidate memories - Mem0 handles this automatically, but we can trigger it
|
|
2638
|
+
*/
|
|
2639
|
+
consolidate(): Promise<void>;
|
|
2640
|
+
/**
|
|
2641
|
+
* Get memory history for a specific memory
|
|
2642
|
+
*/
|
|
2643
|
+
getMemoryHistory(memoryId: string): Promise<any[]>;
|
|
2644
|
+
/**
|
|
2645
|
+
* Get all users (if available)
|
|
2646
|
+
*/
|
|
2647
|
+
getUsers(): Promise<any>;
|
|
2648
|
+
/**
|
|
2649
|
+
* Provide feedback on a memory
|
|
2650
|
+
*/
|
|
2651
|
+
provideFeedback(memoryId: string, feedback: "POSITIVE" | "NEGATIVE" | "VERY_NEGATIVE", reason?: string): Promise<void>;
|
|
2652
|
+
/**
|
|
2653
|
+
* Test connection to Mem0
|
|
2654
|
+
*/
|
|
2655
|
+
ping(): Promise<void>;
|
|
2656
|
+
/**
|
|
2657
|
+
* Convert Mem0 Memory objects to MemoryEntity
|
|
2658
|
+
*/
|
|
2659
|
+
private convertToMemoryEntities;
|
|
2660
|
+
/**
|
|
2661
|
+
* Convert single Mem0 Memory to MemoryEntity
|
|
2662
|
+
*/
|
|
2663
|
+
private convertMem0ToMemoryEntity;
|
|
2664
|
+
}
|
|
2665
|
+
|
|
2666
|
+
/**
|
|
2667
|
+
* Error thrown when parameters are incorrect
|
|
2668
|
+
*/
|
|
2669
|
+
declare class ParamError extends Error {
|
|
2670
|
+
constructor(message: string);
|
|
2671
|
+
}
|
|
2672
|
+
/**
|
|
2673
|
+
* TDAI API Exception
|
|
2674
|
+
*/
|
|
2675
|
+
declare class TDAIException extends Error {
|
|
2676
|
+
code?: string | number;
|
|
2677
|
+
message: string;
|
|
2678
|
+
requestId?: string;
|
|
2679
|
+
constructor(data: {
|
|
2680
|
+
code?: string | number;
|
|
2681
|
+
message?: string;
|
|
2682
|
+
req_id?: string;
|
|
2683
|
+
}, requestId?: string);
|
|
2684
|
+
toString(): string;
|
|
2685
|
+
}
|
|
2686
|
+
|
|
2687
|
+
type StateConstraint = Record<string, unknown>;
|
|
2688
|
+
type OutputConstraint = string | Record<string, unknown> | Array<unknown>;
|
|
2689
|
+
|
|
2690
|
+
interface AgentConfig<TState extends StateConstraint = StateConstraint, TOutput extends OutputConstraint = string> {
|
|
2691
|
+
name: string;
|
|
2692
|
+
description?: string;
|
|
2693
|
+
model: ModelProvider$1 | string;
|
|
2694
|
+
instructions?: string | InstructionProvider<TState>;
|
|
2695
|
+
stateType?: new () => TState;
|
|
2696
|
+
outputType?: OutputType<TOutput>;
|
|
2697
|
+
modelSettings?: ModelSettings;
|
|
2698
|
+
tools?: BaseTool<any, TState>[];
|
|
2699
|
+
memory?: BaseMemory;
|
|
2700
|
+
humanInTheLoop?: HumanInTheLoopConfig;
|
|
2701
|
+
controlFlow?: ControlFlowConfig;
|
|
2702
|
+
}
|
|
2703
|
+
type InstructionProvider<TState extends StateConstraint> = (state: TState, context: RunContext<TState>) => string | Promise<string>;
|
|
2704
|
+
type OutputType<TOutput extends OutputConstraint> = "string" | "structured" | {
|
|
2705
|
+
schema: z.ZodObject<any>;
|
|
2706
|
+
};
|
|
2707
|
+
interface ModelSettings {
|
|
2708
|
+
temperature?: number;
|
|
2709
|
+
maxTokens?: number;
|
|
2710
|
+
topP?: number;
|
|
2711
|
+
frequencyPenalty?: number;
|
|
2712
|
+
presencePenalty?: number;
|
|
2713
|
+
stop?: string[];
|
|
2714
|
+
}
|
|
2715
|
+
interface HumanInTheLoopConfig {
|
|
2716
|
+
enabled: boolean;
|
|
2717
|
+
requireApproval?: string[];
|
|
2718
|
+
highRiskTools?: string[];
|
|
2719
|
+
timeout?: number;
|
|
2720
|
+
defaultAction?: "approve" | "reject" | "pause";
|
|
2721
|
+
}
|
|
2722
|
+
interface ControlFlowConfig {
|
|
2723
|
+
maxSteps?: number;
|
|
2724
|
+
errorRetryLimit?: number;
|
|
2725
|
+
pauseOnHumanInput?: boolean;
|
|
2726
|
+
customHandler?: ControlFlowHandler<any>;
|
|
2727
|
+
}
|
|
2728
|
+
interface RunOptions<TState extends StateConstraint = StateConstraint> {
|
|
2729
|
+
state?: TState;
|
|
2730
|
+
conversationId?: string;
|
|
2731
|
+
runId?: string;
|
|
2732
|
+
userId?: string;
|
|
2733
|
+
model?: string;
|
|
2734
|
+
temperature?: number;
|
|
2735
|
+
maxTokens?: number;
|
|
2736
|
+
stream?: boolean;
|
|
2737
|
+
tools?: BaseTool<any, TState>[];
|
|
2738
|
+
timeout?: number;
|
|
2739
|
+
retryAttempts?: number;
|
|
2740
|
+
metadata?: Record<string, any>;
|
|
2741
|
+
requireApproval?: boolean;
|
|
2742
|
+
resume?: {
|
|
2743
|
+
interruptId: string;
|
|
2744
|
+
payload: string;
|
|
2745
|
+
};
|
|
2746
|
+
onEvent?: (event: any) => void;
|
|
2747
|
+
}
|
|
2748
|
+
interface RunContext<TState = unknown> {
|
|
2749
|
+
conversationId: string;
|
|
2750
|
+
runId: string;
|
|
2751
|
+
userId?: string;
|
|
2752
|
+
requestId?: string;
|
|
2753
|
+
messages: Message[];
|
|
2754
|
+
state?: TState;
|
|
2755
|
+
metadata?: Record<string, any>;
|
|
2756
|
+
}
|
|
2757
|
+
interface MessageMetadata {
|
|
2758
|
+
state?: Record<string, unknown>;
|
|
2759
|
+
conversationId?: string;
|
|
2760
|
+
runId?: string;
|
|
2761
|
+
userId?: string;
|
|
2762
|
+
[key: string]: unknown;
|
|
2763
|
+
}
|
|
2764
|
+
interface Message {
|
|
2765
|
+
id: string;
|
|
2766
|
+
role: "user" | "assistant" | "system" | "tool";
|
|
2767
|
+
content: string;
|
|
2768
|
+
toolCalls?: ToolCall$1[];
|
|
2769
|
+
toolCallId?: string;
|
|
2770
|
+
timestamp?: Date;
|
|
2771
|
+
metadata?: MessageMetadata;
|
|
2772
|
+
}
|
|
2773
|
+
interface ToolCall$1 {
|
|
2774
|
+
id: string;
|
|
2775
|
+
function: {
|
|
2776
|
+
name: string;
|
|
2777
|
+
arguments: string;
|
|
2778
|
+
};
|
|
2779
|
+
}
|
|
2780
|
+
interface AgentState<TState = unknown> {
|
|
2781
|
+
businessState: TState;
|
|
2782
|
+
context: RunContext<TState>;
|
|
2783
|
+
conversationId?: string;
|
|
2784
|
+
runId?: string;
|
|
2785
|
+
status: "idle" | "running" | "paused" | "error";
|
|
2786
|
+
lastActivity?: Date;
|
|
2787
|
+
}
|
|
2788
|
+
interface AgentResult<TOutput = string> {
|
|
2789
|
+
success: boolean;
|
|
2790
|
+
data?: TOutput;
|
|
2791
|
+
messages: Message[];
|
|
2792
|
+
toolCalls?: ToolCall$1[];
|
|
2793
|
+
error?: AgentError;
|
|
2794
|
+
metadata: {
|
|
2795
|
+
conversationId: string;
|
|
2796
|
+
runId: string;
|
|
2797
|
+
executionTime: number;
|
|
2798
|
+
tokenUsage?: TokenUsage;
|
|
2799
|
+
};
|
|
2800
|
+
}
|
|
2801
|
+
type AgentError = AGKitError;
|
|
2802
|
+
interface TokenUsage {
|
|
2803
|
+
promptTokens: number;
|
|
2804
|
+
completionTokens: number;
|
|
2805
|
+
totalTokens: number;
|
|
2806
|
+
}
|
|
2807
|
+
interface ModelProvider$1 {
|
|
2808
|
+
chat: {
|
|
2809
|
+
completions: {
|
|
2810
|
+
create: (params: any) => Promise<any>;
|
|
2811
|
+
stream: (params: any) => AsyncIterable<any>;
|
|
2812
|
+
};
|
|
2813
|
+
};
|
|
2814
|
+
}
|
|
2815
|
+
interface MemoryProvider {
|
|
2816
|
+
saveMessage(conversationId: string, message: Message): Promise<void>;
|
|
2817
|
+
getMessages(conversationId: string, limit?: number): Promise<Message[]>;
|
|
2818
|
+
clearHistory(conversationId: string): Promise<void>;
|
|
2819
|
+
saveMemory(conversationId: string, key: string, value: any): Promise<void>;
|
|
2820
|
+
getMemory(conversationId: string, key: string): Promise<any>;
|
|
2821
|
+
searchMemory(conversationId: string, query: string): Promise<any[]>;
|
|
2822
|
+
}
|
|
2823
|
+
interface ControlFlowHandler<TState = unknown> {
|
|
2824
|
+
handleNextStep(context: any, state?: TState): Promise<ControlFlowDecision>;
|
|
2825
|
+
handleToolCall(toolCall: ToolCall$1, context: any, state?: TState): Promise<ToolCallDecision>;
|
|
2826
|
+
handleError(error: AgentError, context: any, state?: TState): Promise<ErrorHandlingDecision>;
|
|
2827
|
+
}
|
|
2828
|
+
type ControlFlowDecision = {
|
|
2829
|
+
action: "continue";
|
|
2830
|
+
nextStep: any;
|
|
2831
|
+
} | {
|
|
2832
|
+
action: "pause";
|
|
2833
|
+
reason: string;
|
|
2834
|
+
resumeData?: any;
|
|
2835
|
+
} | {
|
|
2836
|
+
action: "complete";
|
|
2837
|
+
result: any;
|
|
2838
|
+
} | {
|
|
2839
|
+
action: "interrupt";
|
|
2840
|
+
reason: string;
|
|
2841
|
+
payload?: any;
|
|
2842
|
+
} | {
|
|
2843
|
+
action: "escalate";
|
|
2844
|
+
target: "human" | "agent";
|
|
2845
|
+
context: any;
|
|
2846
|
+
};
|
|
2847
|
+
type ToolCallDecision = {
|
|
2848
|
+
action: "execute";
|
|
2849
|
+
immediate: boolean;
|
|
2850
|
+
} | {
|
|
2851
|
+
action: "request_approval";
|
|
2852
|
+
approver: "human" | "agent";
|
|
2853
|
+
} | {
|
|
2854
|
+
action: "reject";
|
|
2855
|
+
reason: string;
|
|
2856
|
+
} | {
|
|
2857
|
+
action: "modify";
|
|
2858
|
+
newParams: any;
|
|
2859
|
+
};
|
|
2860
|
+
type ErrorHandlingDecision = {
|
|
2861
|
+
action: "retry";
|
|
2862
|
+
maxAttempts: number;
|
|
2863
|
+
} | {
|
|
2864
|
+
action: "recover";
|
|
2865
|
+
recoveryStep: any;
|
|
2866
|
+
} | {
|
|
2867
|
+
action: "escalate";
|
|
2868
|
+
target: "human" | "agent";
|
|
2869
|
+
} | {
|
|
2870
|
+
action: "abort";
|
|
2871
|
+
reason: string;
|
|
2872
|
+
};
|
|
2873
|
+
interface ConversationConfig {
|
|
2874
|
+
metadata?: Record<string, any>;
|
|
2875
|
+
ttl?: number;
|
|
2876
|
+
maxHistoryLength?: number;
|
|
2877
|
+
}
|
|
2878
|
+
|
|
2879
|
+
declare enum EventType$1 {
|
|
2880
|
+
TEXT_MESSAGE_START = "TEXT_MESSAGE_START",
|
|
2881
|
+
TEXT_MESSAGE_CONTENT = "TEXT_MESSAGE_CONTENT",
|
|
2882
|
+
TEXT_MESSAGE_END = "TEXT_MESSAGE_END",
|
|
2883
|
+
TOOL_CALL_START = "TOOL_CALL_START",
|
|
2884
|
+
TOOL_CALL_ARGS = "TOOL_CALL_ARGS",
|
|
2885
|
+
TOOL_CALL_END = "TOOL_CALL_END",
|
|
2886
|
+
STATE_SNAPSHOT = "STATE_SNAPSHOT",
|
|
2887
|
+
STATE_DELTA = "STATE_DELTA",
|
|
2888
|
+
MESSAGES_SNAPSHOT = "MESSAGES_SNAPSHOT",
|
|
2889
|
+
RAW = "RAW",
|
|
2890
|
+
CUSTOM = "CUSTOM",
|
|
2891
|
+
RUN_STARTED = "RUN_STARTED",
|
|
2892
|
+
RUN_FINISHED = "RUN_FINISHED",
|
|
2893
|
+
RUN_ERROR = "RUN_ERROR",
|
|
2894
|
+
STEP_STARTED = "STEP_STARTED",
|
|
2895
|
+
STEP_FINISHED = "STEP_FINISHED",
|
|
2896
|
+
TEXT_MESSAGE_CHUNK = "TEXT_MESSAGE_CHUNK",
|
|
2897
|
+
TOOL_CALL_CHUNK = "TOOL_CALL_CHUNK",
|
|
2898
|
+
TOOL_CALL_RESULT = "TOOL_CALL_RESULT",
|
|
2899
|
+
TOOL_REGISTERED = "TOOL_REGISTERED",
|
|
2900
|
+
TOOL_UNREGISTERED = "TOOL_UNREGISTERED",
|
|
2901
|
+
TOOL_VALIDATION_FAILED = "TOOL_VALIDATION_FAILED",
|
|
2902
|
+
STATE_UPDATE = "STATE_UPDATE",
|
|
2903
|
+
APPROVAL_REQUIRED = "APPROVAL_REQUIRED",
|
|
2904
|
+
APPROVAL_RECEIVED = "APPROVAL_RECEIVED",
|
|
2905
|
+
CONVERSATION_CREATED = "CONVERSATION_CREATED",
|
|
2906
|
+
CONVERSATION_UPDATED = "CONVERSATION_UPDATED",
|
|
2907
|
+
CONVERSATION_DELETED = "CONVERSATION_DELETED",
|
|
2908
|
+
CONTROL_FLOW_DECISION = "CONTROL_FLOW_DECISION",
|
|
2909
|
+
EXECUTION_PAUSED = "EXECUTION_PAUSED",
|
|
2910
|
+
EXECUTION_RESUMED = "EXECUTION_RESUMED"
|
|
2911
|
+
}
|
|
2912
|
+
interface BaseEvent$1 {
|
|
2913
|
+
type: EventType$1;
|
|
2914
|
+
timestamp?: number;
|
|
2915
|
+
rawEvent?: any;
|
|
2916
|
+
}
|
|
2917
|
+
interface RunStartedEvent extends BaseEvent$1 {
|
|
2918
|
+
type: EventType$1.RUN_STARTED;
|
|
2919
|
+
threadId: string;
|
|
2920
|
+
runId: string;
|
|
2921
|
+
input?: string | Message[];
|
|
2922
|
+
options?: any;
|
|
2923
|
+
}
|
|
2924
|
+
interface RunFinishedEvent extends BaseEvent$1 {
|
|
2925
|
+
type: EventType$1.RUN_FINISHED;
|
|
2926
|
+
threadId: string;
|
|
2927
|
+
runId: string;
|
|
2928
|
+
result?: any;
|
|
2929
|
+
executionTime?: number;
|
|
2930
|
+
}
|
|
2931
|
+
interface RunErrorEvent extends BaseEvent$1 {
|
|
2932
|
+
type: EventType$1.RUN_ERROR;
|
|
2933
|
+
threadId: string;
|
|
2934
|
+
runId: string;
|
|
2935
|
+
code?: string;
|
|
2936
|
+
message: string;
|
|
2937
|
+
executionTime?: number;
|
|
2938
|
+
}
|
|
2939
|
+
interface StepStartedEvent extends BaseEvent$1 {
|
|
2940
|
+
type: EventType$1.STEP_STARTED;
|
|
2941
|
+
stepName: string;
|
|
2942
|
+
}
|
|
2943
|
+
interface StepFinishedEvent extends BaseEvent$1 {
|
|
2944
|
+
type: EventType$1.STEP_FINISHED;
|
|
2945
|
+
stepName: string;
|
|
2946
|
+
}
|
|
2947
|
+
interface TextMessageStartEvent extends BaseEvent$1 {
|
|
2948
|
+
type: EventType$1.TEXT_MESSAGE_START;
|
|
2949
|
+
messageId: string;
|
|
2950
|
+
role?: string;
|
|
2951
|
+
}
|
|
2952
|
+
interface TextMessageContentEvent extends BaseEvent$1 {
|
|
2953
|
+
type: EventType$1.TEXT_MESSAGE_CONTENT;
|
|
2954
|
+
messageId: string;
|
|
2955
|
+
delta: string;
|
|
2956
|
+
}
|
|
2957
|
+
interface TextMessageEndEvent extends BaseEvent$1 {
|
|
2958
|
+
type: EventType$1.TEXT_MESSAGE_END;
|
|
2959
|
+
messageId: string;
|
|
2960
|
+
}
|
|
2961
|
+
interface TextMessageChunkEvent extends BaseEvent$1 {
|
|
2962
|
+
type: EventType$1.TEXT_MESSAGE_CHUNK;
|
|
2963
|
+
messageId?: string;
|
|
2964
|
+
role?: string;
|
|
2965
|
+
delta?: string;
|
|
2966
|
+
}
|
|
2967
|
+
interface MessagesSnapshotEvent extends BaseEvent$1 {
|
|
2968
|
+
type: EventType$1.MESSAGES_SNAPSHOT;
|
|
2969
|
+
messages: Message[];
|
|
2970
|
+
}
|
|
2971
|
+
interface ToolCallStartEvent extends BaseEvent$1 {
|
|
2972
|
+
type: EventType$1.TOOL_CALL_START;
|
|
2973
|
+
toolCallId: string;
|
|
2974
|
+
toolCallName: string;
|
|
2975
|
+
parentMessageId?: string;
|
|
2976
|
+
}
|
|
2977
|
+
interface ToolCallArgsEvent extends BaseEvent$1 {
|
|
2978
|
+
type: EventType$1.TOOL_CALL_ARGS;
|
|
2979
|
+
toolCallId: string;
|
|
2980
|
+
delta: string;
|
|
2981
|
+
}
|
|
2982
|
+
interface ToolCallEndEvent extends BaseEvent$1 {
|
|
2983
|
+
type: EventType$1.TOOL_CALL_END;
|
|
2984
|
+
toolCallId: string;
|
|
2985
|
+
}
|
|
2986
|
+
interface ToolCallChunkEvent extends BaseEvent$1 {
|
|
2987
|
+
type: EventType$1.TOOL_CALL_CHUNK;
|
|
2988
|
+
toolCallId?: string;
|
|
2989
|
+
toolCallName?: string;
|
|
2990
|
+
parentMessageId?: string;
|
|
2991
|
+
delta?: string;
|
|
2992
|
+
}
|
|
2993
|
+
interface ToolCallResultEvent extends BaseEvent$1 {
|
|
2994
|
+
type: EventType$1.TOOL_CALL_RESULT;
|
|
2995
|
+
toolCallId: string;
|
|
2996
|
+
content: string;
|
|
2997
|
+
messageId: string;
|
|
2998
|
+
conversationId: string;
|
|
2999
|
+
runId: string;
|
|
3000
|
+
}
|
|
3001
|
+
interface ToolRegisteredEvent extends BaseEvent$1 {
|
|
3002
|
+
type: EventType$1.TOOL_REGISTERED;
|
|
3003
|
+
toolName: string;
|
|
3004
|
+
}
|
|
3005
|
+
interface ToolUnregisteredEvent extends BaseEvent$1 {
|
|
3006
|
+
type: EventType$1.TOOL_UNREGISTERED;
|
|
3007
|
+
toolName: string;
|
|
3008
|
+
}
|
|
3009
|
+
interface ToolValidationFailedEvent extends BaseEvent$1 {
|
|
3010
|
+
type: EventType$1.TOOL_VALIDATION_FAILED;
|
|
3011
|
+
toolName: string;
|
|
3012
|
+
input: any;
|
|
3013
|
+
error: string;
|
|
3014
|
+
}
|
|
3015
|
+
interface StateUpdateEvent extends BaseEvent$1 {
|
|
3016
|
+
type: EventType$1.STATE_UPDATE;
|
|
3017
|
+
state: any;
|
|
3018
|
+
conversationId: string;
|
|
3019
|
+
runId: string;
|
|
3020
|
+
}
|
|
3021
|
+
interface ApprovalRequiredEvent extends BaseEvent$1 {
|
|
3022
|
+
type: EventType$1.APPROVAL_REQUIRED;
|
|
3023
|
+
interruptId: string;
|
|
3024
|
+
toolName: string;
|
|
3025
|
+
params: any;
|
|
3026
|
+
conversationId: string;
|
|
3027
|
+
runId: string;
|
|
3028
|
+
message: string;
|
|
3029
|
+
context: any;
|
|
3030
|
+
}
|
|
3031
|
+
interface ApprovalReceivedEvent extends BaseEvent$1 {
|
|
3032
|
+
type: EventType$1.APPROVAL_RECEIVED;
|
|
3033
|
+
interruptId: string;
|
|
3034
|
+
action: 'approve' | 'reject' | 'modify';
|
|
3035
|
+
value?: any;
|
|
3036
|
+
reason?: string;
|
|
3037
|
+
conversationId: string;
|
|
3038
|
+
runId: string;
|
|
3039
|
+
}
|
|
3040
|
+
interface ErrorEvent extends BaseEvent$1 {
|
|
3041
|
+
type: EventType$1.RUN_ERROR;
|
|
3042
|
+
error: AgentError;
|
|
3043
|
+
context?: any;
|
|
3044
|
+
conversationId?: string;
|
|
3045
|
+
runId?: string;
|
|
3046
|
+
}
|
|
3047
|
+
interface StateSnapshotEvent extends BaseEvent$1 {
|
|
3048
|
+
type: EventType$1.STATE_SNAPSHOT;
|
|
3049
|
+
snapshot: any;
|
|
3050
|
+
}
|
|
3051
|
+
interface StateDeltaEvent extends BaseEvent$1 {
|
|
3052
|
+
type: EventType$1.STATE_DELTA;
|
|
3053
|
+
delta: any[];
|
|
3054
|
+
}
|
|
3055
|
+
interface ConversationCreatedEvent extends BaseEvent$1 {
|
|
3056
|
+
type: EventType$1.CONVERSATION_CREATED;
|
|
3057
|
+
conversation: any;
|
|
3058
|
+
conversationId: string;
|
|
3059
|
+
}
|
|
3060
|
+
interface ConversationUpdatedEvent extends BaseEvent$1 {
|
|
3061
|
+
type: EventType$1.CONVERSATION_UPDATED;
|
|
3062
|
+
conversation: any;
|
|
3063
|
+
conversationId: string;
|
|
3064
|
+
updates: any;
|
|
3065
|
+
}
|
|
3066
|
+
interface ConversationDeletedEvent extends BaseEvent$1 {
|
|
3067
|
+
type: EventType$1.CONVERSATION_DELETED;
|
|
3068
|
+
conversationId: string;
|
|
3069
|
+
}
|
|
3070
|
+
interface ControlFlowDecisionEvent extends BaseEvent$1 {
|
|
3071
|
+
type: EventType$1.CONTROL_FLOW_DECISION;
|
|
3072
|
+
decision: any;
|
|
3073
|
+
context: any;
|
|
3074
|
+
conversationId: string;
|
|
3075
|
+
runId: string;
|
|
3076
|
+
}
|
|
3077
|
+
interface ExecutionPausedEvent extends BaseEvent$1 {
|
|
3078
|
+
type: EventType$1.EXECUTION_PAUSED;
|
|
3079
|
+
reason: string;
|
|
3080
|
+
resumeData?: any;
|
|
3081
|
+
conversationId: string;
|
|
3082
|
+
runId: string;
|
|
3083
|
+
}
|
|
3084
|
+
interface ExecutionResumedEvent extends BaseEvent$1 {
|
|
3085
|
+
type: EventType$1.EXECUTION_RESUMED;
|
|
3086
|
+
resumeData?: any;
|
|
3087
|
+
conversationId: string;
|
|
3088
|
+
runId: string;
|
|
3089
|
+
}
|
|
3090
|
+
interface RawEvent extends BaseEvent$1 {
|
|
3091
|
+
type: EventType$1.RAW;
|
|
3092
|
+
event: any;
|
|
3093
|
+
source?: string;
|
|
3094
|
+
}
|
|
3095
|
+
interface CustomEvent extends BaseEvent$1 {
|
|
3096
|
+
type: EventType$1.CUSTOM;
|
|
3097
|
+
name: string;
|
|
3098
|
+
value?: any;
|
|
3099
|
+
}
|
|
3100
|
+
type AgentEvent = RunStartedEvent | RunFinishedEvent | RunErrorEvent | StepStartedEvent | StepFinishedEvent | TextMessageStartEvent | TextMessageContentEvent | TextMessageEndEvent | TextMessageChunkEvent | MessagesSnapshotEvent | ToolCallStartEvent | ToolCallArgsEvent | ToolCallEndEvent | ToolCallChunkEvent | ToolCallResultEvent | ToolRegisteredEvent | ToolUnregisteredEvent | ToolValidationFailedEvent | StateUpdateEvent | StateSnapshotEvent | StateDeltaEvent | ApprovalRequiredEvent | ApprovalReceivedEvent | ErrorEvent | ConversationCreatedEvent | ConversationUpdatedEvent | ConversationDeletedEvent | ControlFlowDecisionEvent | ExecutionPausedEvent | ExecutionResumedEvent | RawEvent | CustomEvent;
|
|
3101
|
+
interface EventData$1 {
|
|
3102
|
+
[EventType$1.TEXT_MESSAGE_START]: TextMessageStartEvent;
|
|
3103
|
+
[EventType$1.TEXT_MESSAGE_CONTENT]: TextMessageContentEvent;
|
|
3104
|
+
[EventType$1.TEXT_MESSAGE_END]: TextMessageEndEvent;
|
|
3105
|
+
[EventType$1.TOOL_CALL_START]: ToolCallStartEvent;
|
|
3106
|
+
[EventType$1.TOOL_CALL_ARGS]: ToolCallArgsEvent;
|
|
3107
|
+
[EventType$1.TOOL_CALL_END]: ToolCallEndEvent;
|
|
3108
|
+
[EventType$1.STATE_SNAPSHOT]: StateSnapshotEvent;
|
|
3109
|
+
[EventType$1.STATE_DELTA]: StateDeltaEvent;
|
|
3110
|
+
[EventType$1.MESSAGES_SNAPSHOT]: MessagesSnapshotEvent;
|
|
3111
|
+
[EventType$1.RAW]: RawEvent;
|
|
3112
|
+
[EventType$1.CUSTOM]: CustomEvent;
|
|
3113
|
+
[EventType$1.RUN_STARTED]: RunStartedEvent;
|
|
3114
|
+
[EventType$1.RUN_FINISHED]: RunFinishedEvent;
|
|
3115
|
+
[EventType$1.RUN_ERROR]: RunErrorEvent;
|
|
3116
|
+
[EventType$1.STEP_STARTED]: StepStartedEvent;
|
|
3117
|
+
[EventType$1.STEP_FINISHED]: StepFinishedEvent;
|
|
3118
|
+
[EventType$1.TEXT_MESSAGE_CHUNK]: TextMessageChunkEvent;
|
|
3119
|
+
[EventType$1.TOOL_CALL_CHUNK]: ToolCallChunkEvent;
|
|
3120
|
+
[EventType$1.TOOL_CALL_RESULT]: ToolCallResultEvent;
|
|
3121
|
+
[EventType$1.TOOL_REGISTERED]: ToolRegisteredEvent;
|
|
3122
|
+
[EventType$1.TOOL_UNREGISTERED]: ToolUnregisteredEvent;
|
|
3123
|
+
[EventType$1.TOOL_VALIDATION_FAILED]: ToolValidationFailedEvent;
|
|
3124
|
+
[EventType$1.STATE_UPDATE]: StateUpdateEvent;
|
|
3125
|
+
[EventType$1.APPROVAL_REQUIRED]: ApprovalRequiredEvent;
|
|
3126
|
+
[EventType$1.APPROVAL_RECEIVED]: ApprovalReceivedEvent;
|
|
3127
|
+
[EventType$1.RUN_ERROR]: RunErrorEvent;
|
|
3128
|
+
[EventType$1.CONVERSATION_CREATED]: ConversationCreatedEvent;
|
|
3129
|
+
[EventType$1.CONVERSATION_UPDATED]: ConversationUpdatedEvent;
|
|
3130
|
+
[EventType$1.CONVERSATION_DELETED]: ConversationDeletedEvent;
|
|
3131
|
+
[EventType$1.CONTROL_FLOW_DECISION]: ControlFlowDecisionEvent;
|
|
3132
|
+
[EventType$1.EXECUTION_PAUSED]: ExecutionPausedEvent;
|
|
3133
|
+
[EventType$1.EXECUTION_RESUMED]: ExecutionResumedEvent;
|
|
3134
|
+
}
|
|
3135
|
+
type EventHandler$1<T extends EventType$1> = (data: EventData$1[T]) => void;
|
|
3136
|
+
declare class EventSystem extends EventEmitter {
|
|
3137
|
+
constructor();
|
|
3138
|
+
emit<T extends EventType$1>(event: T, data: EventData$1[T]): boolean;
|
|
3139
|
+
on<T extends EventType$1>(event: T, handler: EventHandler$1<T>): this;
|
|
3140
|
+
once<T extends EventType$1>(event: T, handler: EventHandler$1<T>): this;
|
|
3141
|
+
off<T extends EventType$1>(event: T, handler: EventHandler$1<T>): this;
|
|
3142
|
+
removeAllListeners<T extends EventType$1>(event?: T): this;
|
|
3143
|
+
listenerCount<T extends EventType$1>(event: T): number;
|
|
3144
|
+
getEventNames(): EventType$1[];
|
|
3145
|
+
hasListeners<T extends EventType$1>(event: T): boolean;
|
|
3146
|
+
safeEmit<T extends EventType$1>(event: T, data: EventData$1[T]): boolean;
|
|
3147
|
+
waitFor<T extends EventType$1>(event: T, timeout?: number): Promise<EventData$1[T]>;
|
|
3148
|
+
waitForAny<T extends EventType$1>(events: T[], timeout?: number): Promise<{
|
|
3149
|
+
event: T;
|
|
3150
|
+
data: EventData$1[T];
|
|
3151
|
+
}>;
|
|
3152
|
+
}
|
|
3153
|
+
|
|
3154
|
+
declare class Agent<TState extends StateConstraint = StateConstraint, TOutput extends OutputConstraint = string> {
|
|
3155
|
+
private config;
|
|
3156
|
+
private modelProvider;
|
|
3157
|
+
private eventSystem;
|
|
3158
|
+
private toolProxy;
|
|
3159
|
+
private toolExecutionHandler;
|
|
3160
|
+
private messageBuilder;
|
|
3161
|
+
private agentExecutor;
|
|
3162
|
+
private state;
|
|
3163
|
+
private isRunning;
|
|
3164
|
+
private isPaused;
|
|
3165
|
+
private memory;
|
|
3166
|
+
/**
|
|
3167
|
+
* Create a message with state metadata for memory storage
|
|
3168
|
+
*/
|
|
3169
|
+
private createMessageWithState;
|
|
3170
|
+
/**
|
|
3171
|
+
* Get conversation messages from memory
|
|
3172
|
+
*/
|
|
3173
|
+
private getConversationMessages;
|
|
3174
|
+
/**
|
|
3175
|
+
* Add messages to memory
|
|
3176
|
+
*/
|
|
3177
|
+
private addMessages;
|
|
3178
|
+
/**
|
|
3179
|
+
* Extract current state from messages
|
|
3180
|
+
*/
|
|
3181
|
+
private extractStateFromMessages;
|
|
3182
|
+
constructor(config: AgentConfig<TState, TOutput>);
|
|
3183
|
+
run(input: string | Message[], state?: TState, options?: RunOptions<TState>): Promise<AgentResult<TOutput>>;
|
|
3184
|
+
stream(input: string | Message[], state?: TState, options?: RunOptions<TState>): AsyncIterableIterator<AgentEvent>;
|
|
3185
|
+
addTool<T extends BaseTool<any, TState>>(tool: T): void;
|
|
3186
|
+
removeTool(toolName: string): boolean;
|
|
3187
|
+
getTool(toolName: string): BaseTool<any, TState> | undefined;
|
|
3188
|
+
getTools(): BaseTool<any, TState>[];
|
|
3189
|
+
getToolCount(): number;
|
|
3190
|
+
hasTool(toolName: string): boolean;
|
|
3191
|
+
getToolNames(): string[];
|
|
3192
|
+
getToolsRequiringApproval(): BaseTool<any, TState>[];
|
|
3193
|
+
executeTool(toolName: string, input: unknown, context?: Partial<ToolExecutionContext>): Promise<unknown>;
|
|
3194
|
+
validateToolInput(toolName: string, input: unknown): boolean;
|
|
3195
|
+
getToolMetadata(toolName: string): {
|
|
3196
|
+
name: string;
|
|
3197
|
+
description?: string;
|
|
3198
|
+
hasSchema: boolean;
|
|
3199
|
+
requiresApproval: boolean;
|
|
3200
|
+
} | null;
|
|
3201
|
+
createConversation(userId?: string, config?: any): Promise<any>;
|
|
3202
|
+
getConversation(conversationId: string): Promise<any>;
|
|
3203
|
+
deleteConversation(conversationId: string): Promise<void>;
|
|
3204
|
+
getState(): AgentState<TState>;
|
|
3205
|
+
setState(state: Partial<AgentState<TState>>): void;
|
|
3206
|
+
start(): Promise<void>;
|
|
3207
|
+
pause(): Promise<void>;
|
|
3208
|
+
resume(resumeData?: any): Promise<void>;
|
|
3209
|
+
stop(): Promise<void>;
|
|
3210
|
+
on<T extends EventType$1>(event: T, handler: (data: any) => void): void;
|
|
3211
|
+
off<T extends EventType$1>(event: T, handler: (data: any) => void): void;
|
|
3212
|
+
private initializeState;
|
|
3213
|
+
private initializeModelProvider;
|
|
3214
|
+
private initializeTools;
|
|
3215
|
+
private initializeEventListeners;
|
|
3216
|
+
private createRunContext;
|
|
3217
|
+
private emitEvent;
|
|
3218
|
+
private executeAgentCore;
|
|
3219
|
+
private createAgentError;
|
|
3220
|
+
get name(): string;
|
|
3221
|
+
get description(): string | undefined;
|
|
3222
|
+
get isActive(): boolean;
|
|
3223
|
+
get isPausedState(): boolean;
|
|
3224
|
+
toAGUIAgent(): any;
|
|
3225
|
+
destroy(): void;
|
|
3226
|
+
}
|
|
3227
|
+
|
|
3228
|
+
/**
|
|
3229
|
+
* Type validation utilities for enhanced type safety
|
|
3230
|
+
*/
|
|
3231
|
+
|
|
3232
|
+
declare function isStateConstraint(value: unknown): value is StateConstraint;
|
|
3233
|
+
declare function isOutputConstraint(value: unknown): value is OutputConstraint;
|
|
3234
|
+
declare const StateConstraintSchema: z.ZodRecord<z.ZodString, z.ZodUnknown>;
|
|
3235
|
+
declare const OutputConstraintSchema: z.ZodUnion<readonly [z.ZodString, z.ZodRecord<z.ZodString, z.ZodUnknown>, z.ZodArray<z.ZodUnknown>]>;
|
|
3236
|
+
declare function validateState<T extends StateConstraint>(value: unknown, schema?: z.ZodSchema<T>): T;
|
|
3237
|
+
declare function validateOutput<T extends OutputConstraint>(value: unknown, schema?: z.ZodSchema<T>): T;
|
|
3238
|
+
declare class TypeChecker {
|
|
3239
|
+
private static instance;
|
|
3240
|
+
static getInstance(): TypeChecker;
|
|
3241
|
+
checkType<T>(value: unknown, typeName: string): value is T;
|
|
3242
|
+
validateObjectStructure(obj: unknown, requiredKeys: string[], optionalKeys?: string[]): obj is Record<string, unknown>;
|
|
3243
|
+
validateDeepType(value: unknown, expectedType: string): boolean;
|
|
3244
|
+
}
|
|
3245
|
+
declare function createTypeSafeState<T extends StateConstraint>(initialState: T): T;
|
|
3246
|
+
declare function createTypeSafeOutput<T extends OutputConstraint>(output: T): T;
|
|
3247
|
+
declare function validateTypes<T extends StateConstraint, U extends OutputConstraint>(stateSchema?: z.ZodSchema<T>, outputSchema?: z.ZodSchema<U>): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
|
|
3248
|
+
declare const typeChecker: TypeChecker;
|
|
3249
|
+
|
|
3250
|
+
/**
|
|
3251
|
+
* Optimized Event System with batching and async processing
|
|
3252
|
+
*/
|
|
3253
|
+
|
|
3254
|
+
declare enum EventType {
|
|
3255
|
+
RUN_STARTED = "run_started",
|
|
3256
|
+
RUN_FINISHED = "run_finished",
|
|
3257
|
+
RUN_FAILED = "run_failed",
|
|
3258
|
+
TEXT_MESSAGE_START = "text_message_start",
|
|
3259
|
+
TEXT_MESSAGE_CONTENT = "text_message_content",
|
|
3260
|
+
TEXT_MESSAGE_END = "text_message_end",
|
|
3261
|
+
MESSAGES_SNAPSHOT = "messages_snapshot",
|
|
3262
|
+
TOOL_CALL_START = "tool_call_start",
|
|
3263
|
+
TOOL_CALL_ARGS = "tool_call_args",
|
|
3264
|
+
TOOL_CALL_END = "tool_call_end",
|
|
3265
|
+
TOOL_CALL_RESULT = "tool_call_result",
|
|
3266
|
+
STATE_UPDATE = "state_update",
|
|
3267
|
+
APPROVAL_REQUIRED = "approval_required",
|
|
3268
|
+
APPROVAL_RECEIVED = "approval_received",
|
|
3269
|
+
ERROR = "error",
|
|
3270
|
+
CONVERSATION_CREATED = "conversation_created",
|
|
3271
|
+
CONVERSATION_UPDATED = "conversation_updated",
|
|
3272
|
+
CONVERSATION_DELETED = "conversation_deleted",
|
|
3273
|
+
CONTROL_FLOW_DECISION = "control_flow_decision",
|
|
3274
|
+
EXECUTION_PAUSED = "execution_paused",
|
|
3275
|
+
EXECUTION_RESUMED = "execution_resumed"
|
|
3276
|
+
}
|
|
3277
|
+
interface BaseEvent {
|
|
3278
|
+
type: EventType;
|
|
3279
|
+
conversationId?: string;
|
|
3280
|
+
runId?: string;
|
|
3281
|
+
timestamp?: number;
|
|
3282
|
+
}
|
|
3283
|
+
type EventData = {
|
|
3284
|
+
[EventType.RUN_STARTED]: BaseEvent & {
|
|
3285
|
+
conversationId: string;
|
|
3286
|
+
runId: string;
|
|
3287
|
+
};
|
|
3288
|
+
[EventType.RUN_FINISHED]: BaseEvent & {
|
|
3289
|
+
conversationId: string;
|
|
3290
|
+
runId: string;
|
|
3291
|
+
executionTime: number;
|
|
3292
|
+
};
|
|
3293
|
+
[EventType.RUN_FAILED]: BaseEvent & {
|
|
3294
|
+
conversationId: string;
|
|
3295
|
+
runId: string;
|
|
3296
|
+
error: AgentError;
|
|
3297
|
+
};
|
|
3298
|
+
[EventType.TEXT_MESSAGE_START]: BaseEvent & {
|
|
3299
|
+
messageId: string;
|
|
3300
|
+
content: string;
|
|
3301
|
+
};
|
|
3302
|
+
[EventType.TEXT_MESSAGE_CONTENT]: BaseEvent & {
|
|
3303
|
+
messageId: string;
|
|
3304
|
+
content: string;
|
|
3305
|
+
delta: string;
|
|
3306
|
+
};
|
|
3307
|
+
[EventType.TEXT_MESSAGE_END]: BaseEvent & {
|
|
3308
|
+
messageId: string;
|
|
3309
|
+
content: string;
|
|
3310
|
+
};
|
|
3311
|
+
[EventType.MESSAGES_SNAPSHOT]: BaseEvent & {
|
|
3312
|
+
messages: Message[];
|
|
3313
|
+
};
|
|
3314
|
+
[EventType.TOOL_CALL_START]: BaseEvent & {
|
|
3315
|
+
toolCallId: string;
|
|
3316
|
+
toolName: string;
|
|
3317
|
+
};
|
|
3318
|
+
[EventType.TOOL_CALL_ARGS]: BaseEvent & {
|
|
3319
|
+
toolCallId: string;
|
|
3320
|
+
args: any;
|
|
3321
|
+
};
|
|
3322
|
+
[EventType.TOOL_CALL_END]: BaseEvent & {
|
|
3323
|
+
toolCallId: string;
|
|
3324
|
+
result: any;
|
|
3325
|
+
};
|
|
3326
|
+
[EventType.TOOL_CALL_RESULT]: BaseEvent & {
|
|
3327
|
+
toolCallId: string;
|
|
3328
|
+
result: any;
|
|
3329
|
+
};
|
|
3330
|
+
[EventType.STATE_UPDATE]: BaseEvent & {
|
|
3331
|
+
state: any;
|
|
3332
|
+
};
|
|
3333
|
+
[EventType.APPROVAL_REQUIRED]: BaseEvent & {
|
|
3334
|
+
toolName: string;
|
|
3335
|
+
args: any;
|
|
3336
|
+
};
|
|
3337
|
+
[EventType.APPROVAL_RECEIVED]: BaseEvent & {
|
|
3338
|
+
toolName: string;
|
|
3339
|
+
approved: boolean;
|
|
3340
|
+
};
|
|
3341
|
+
[EventType.ERROR]: BaseEvent & {
|
|
3342
|
+
error: AgentError;
|
|
3343
|
+
};
|
|
3344
|
+
[EventType.CONVERSATION_CREATED]: BaseEvent & {
|
|
3345
|
+
conversationId: string;
|
|
3346
|
+
};
|
|
3347
|
+
[EventType.CONVERSATION_UPDATED]: BaseEvent & {
|
|
3348
|
+
conversationId: string;
|
|
3349
|
+
messages: Message[];
|
|
3350
|
+
};
|
|
3351
|
+
[EventType.CONVERSATION_DELETED]: BaseEvent & {
|
|
3352
|
+
conversationId: string;
|
|
3353
|
+
};
|
|
3354
|
+
[EventType.CONTROL_FLOW_DECISION]: BaseEvent & {
|
|
3355
|
+
decision: string;
|
|
3356
|
+
context: any;
|
|
3357
|
+
};
|
|
3358
|
+
[EventType.EXECUTION_PAUSED]: BaseEvent & {
|
|
3359
|
+
reason: string;
|
|
3360
|
+
};
|
|
3361
|
+
[EventType.EXECUTION_RESUMED]: BaseEvent & {
|
|
3362
|
+
reason: string;
|
|
3363
|
+
};
|
|
3364
|
+
};
|
|
3365
|
+
type EventHandler<T extends EventType> = (data: EventData[T]) => void | Promise<void>;
|
|
3366
|
+
interface EventProcessingOptions {
|
|
3367
|
+
batchSize: number;
|
|
3368
|
+
batchTimeout: number;
|
|
3369
|
+
maxConcurrentHandlers: number;
|
|
3370
|
+
enableBatching: boolean;
|
|
3371
|
+
enableAsyncProcessing: boolean;
|
|
3372
|
+
}
|
|
3373
|
+
declare class OptimizedEventSystem extends EventEmitter {
|
|
3374
|
+
private eventQueue;
|
|
3375
|
+
private batchTimer?;
|
|
3376
|
+
private processingHandlers;
|
|
3377
|
+
private options;
|
|
3378
|
+
private isProcessing;
|
|
3379
|
+
constructor(options?: Partial<EventProcessingOptions>);
|
|
3380
|
+
emit<T extends EventType>(event: T, data: EventData[T]): boolean;
|
|
3381
|
+
private addToBatch;
|
|
3382
|
+
private processBatch;
|
|
3383
|
+
private processBatchAsync;
|
|
3384
|
+
private processBatchSync;
|
|
3385
|
+
private processEventImmediately;
|
|
3386
|
+
private getEventPriority;
|
|
3387
|
+
on<T extends EventType>(event: T, handler: EventHandler<T>): this;
|
|
3388
|
+
once<T extends EventType>(event: T, handler: EventHandler<T>): this;
|
|
3389
|
+
getPerformanceMetrics(): {
|
|
3390
|
+
queueSize: number;
|
|
3391
|
+
processingHandlers: number;
|
|
3392
|
+
isProcessing: boolean;
|
|
3393
|
+
};
|
|
3394
|
+
flush(): Promise<void>;
|
|
3395
|
+
destroy(): void;
|
|
3396
|
+
}
|
|
3397
|
+
declare function createOptimizedEventSystem(options?: Partial<EventProcessingOptions>): OptimizedEventSystem;
|
|
3398
|
+
declare const optimizedEventSystem: OptimizedEventSystem;
|
|
3399
|
+
|
|
3400
|
+
interface ToolCall {
|
|
3401
|
+
id: string;
|
|
3402
|
+
function: {
|
|
3403
|
+
name: string;
|
|
3404
|
+
arguments: string;
|
|
3405
|
+
};
|
|
3406
|
+
}
|
|
3407
|
+
interface ChatCompletionParams {
|
|
3408
|
+
model: string;
|
|
3409
|
+
messages: Message[];
|
|
3410
|
+
temperature?: number;
|
|
3411
|
+
max_tokens?: number;
|
|
3412
|
+
top_p?: number;
|
|
3413
|
+
frequency_penalty?: number;
|
|
3414
|
+
presence_penalty?: number;
|
|
3415
|
+
tools?: ToolDefinition[];
|
|
3416
|
+
tool_choice?: 'auto' | 'none' | {
|
|
3417
|
+
type: 'function';
|
|
3418
|
+
function: {
|
|
3419
|
+
name: string;
|
|
3420
|
+
};
|
|
3421
|
+
};
|
|
3422
|
+
stream?: boolean;
|
|
3423
|
+
stop?: string[];
|
|
3424
|
+
}
|
|
3425
|
+
interface ToolDefinition {
|
|
3426
|
+
type: 'function';
|
|
3427
|
+
function: {
|
|
3428
|
+
name: string;
|
|
3429
|
+
description?: string;
|
|
3430
|
+
parameters: any;
|
|
3431
|
+
};
|
|
3432
|
+
}
|
|
3433
|
+
interface ChatCompletion {
|
|
3434
|
+
id: string;
|
|
3435
|
+
object: 'chat.completion';
|
|
3436
|
+
created: number;
|
|
3437
|
+
model: string;
|
|
3438
|
+
choices: Array<{
|
|
3439
|
+
index: number;
|
|
3440
|
+
message: {
|
|
3441
|
+
role: 'assistant';
|
|
3442
|
+
content: string | null;
|
|
3443
|
+
tool_calls?: ToolCall[];
|
|
3444
|
+
};
|
|
3445
|
+
finish_reason: 'stop' | 'length' | 'content_filter' | 'tool_calls';
|
|
3446
|
+
}>;
|
|
3447
|
+
usage: {
|
|
3448
|
+
prompt_tokens: number;
|
|
3449
|
+
completion_tokens: number;
|
|
3450
|
+
total_tokens: number;
|
|
3451
|
+
};
|
|
3452
|
+
}
|
|
3453
|
+
interface ChatCompletionChunk {
|
|
3454
|
+
id: string;
|
|
3455
|
+
object: 'chat.completion.chunk';
|
|
3456
|
+
created: number;
|
|
3457
|
+
model: string;
|
|
3458
|
+
choices: Array<{
|
|
3459
|
+
index: number;
|
|
3460
|
+
delta: {
|
|
3461
|
+
role?: 'assistant';
|
|
3462
|
+
content?: string;
|
|
3463
|
+
tool_calls?: ToolCall[];
|
|
3464
|
+
};
|
|
3465
|
+
finish_reason?: 'stop' | 'length' | 'content_filter' | 'tool_calls';
|
|
3466
|
+
}>;
|
|
3467
|
+
}
|
|
3468
|
+
interface ModelProvider {
|
|
3469
|
+
chat: {
|
|
3470
|
+
completions: {
|
|
3471
|
+
create: (params: ChatCompletionParams) => Promise<ChatCompletion>;
|
|
3472
|
+
stream: (params: ChatCompletionParams) => AsyncIterable<ChatCompletionChunk>;
|
|
3473
|
+
};
|
|
3474
|
+
};
|
|
3475
|
+
supportsTools(): boolean;
|
|
3476
|
+
supportsStreaming(): boolean;
|
|
3477
|
+
formatTools(tools: any[]): ToolDefinition[];
|
|
3478
|
+
parseToolCalls(response: ChatCompletion): ToolCall[];
|
|
3479
|
+
getProviderName(): string;
|
|
3480
|
+
getDefaultModel(): string;
|
|
3481
|
+
validateConfig(config: any): boolean;
|
|
3482
|
+
}
|
|
3483
|
+
interface ModelProviderConfig {
|
|
3484
|
+
apiKey: string;
|
|
3485
|
+
baseURL?: string;
|
|
3486
|
+
defaultModel?: string;
|
|
3487
|
+
timeout?: number;
|
|
3488
|
+
maxRetries?: number;
|
|
3489
|
+
retryDelay?: number;
|
|
3490
|
+
proxy?: string;
|
|
3491
|
+
rejectUnauthorized?: boolean;
|
|
3492
|
+
}
|
|
3493
|
+
interface ModelProviderError extends Error {
|
|
3494
|
+
type: 'authentication' | 'rate_limit' | 'quota_exceeded' | 'invalid_request' | 'server_error' | 'timeout' | 'unknown';
|
|
3495
|
+
status?: number;
|
|
3496
|
+
code?: string;
|
|
3497
|
+
details?: any;
|
|
3498
|
+
}
|
|
3499
|
+
interface ModelProviderFactory {
|
|
3500
|
+
createOpenAI(config: ModelProviderConfig): ModelProvider;
|
|
3501
|
+
createAnthropic(config: ModelProviderConfig): ModelProvider;
|
|
3502
|
+
createCustom(config: ModelProviderConfig & {
|
|
3503
|
+
providerType: string;
|
|
3504
|
+
}): ModelProvider;
|
|
3505
|
+
}
|
|
3506
|
+
declare abstract class BaseModelProvider implements ModelProvider {
|
|
3507
|
+
protected config: ModelProviderConfig;
|
|
3508
|
+
protected defaultModel: string;
|
|
3509
|
+
constructor(config: ModelProviderConfig);
|
|
3510
|
+
abstract get chat(): {
|
|
3511
|
+
completions: {
|
|
3512
|
+
create: (params: ChatCompletionParams) => Promise<ChatCompletion>;
|
|
3513
|
+
stream: (params: ChatCompletionParams) => AsyncIterable<ChatCompletionChunk>;
|
|
3514
|
+
};
|
|
3515
|
+
};
|
|
3516
|
+
abstract getProviderName(): string;
|
|
3517
|
+
abstract getDefaultModel(): string;
|
|
3518
|
+
abstract formatTools(tools: any[]): ToolDefinition[];
|
|
3519
|
+
abstract parseToolCalls(response: ChatCompletion): ToolCall[];
|
|
3520
|
+
supportsTools(): boolean;
|
|
3521
|
+
supportsStreaming(): boolean;
|
|
3522
|
+
validateConfig(config: any): boolean;
|
|
3523
|
+
protected createError(message: string, type: ModelProviderError['type'], details?: any): ModelProviderError;
|
|
3524
|
+
protected withRetry<T>(operation: () => Promise<T>, maxRetries?: number, delay?: number): Promise<T>;
|
|
3525
|
+
protected isNonRetryableError(error: Error): boolean;
|
|
3526
|
+
protected formatMessages(messages: Message[]): any[];
|
|
3527
|
+
protected parseMessages(response: any): Message[];
|
|
3528
|
+
}
|
|
3529
|
+
declare class ModelProviderRegistry {
|
|
3530
|
+
private providers;
|
|
3531
|
+
private defaultProvider?;
|
|
3532
|
+
register(name: string, provider: ModelProvider): this;
|
|
3533
|
+
unregister(name: string): boolean;
|
|
3534
|
+
get(name: string): ModelProvider | undefined;
|
|
3535
|
+
getAll(): Map<string, ModelProvider>;
|
|
3536
|
+
setDefault(name: string): this;
|
|
3537
|
+
getDefault(): ModelProvider | undefined;
|
|
3538
|
+
listNames(): string[];
|
|
3539
|
+
has(name: string): boolean;
|
|
3540
|
+
clear(): void;
|
|
3541
|
+
}
|
|
3542
|
+
declare const modelProviderRegistry: ModelProviderRegistry;
|
|
3543
|
+
|
|
3544
|
+
interface OpenAIFetchOptions {
|
|
3545
|
+
fetch?: typeof fetch;
|
|
3546
|
+
dispatcher?: any;
|
|
3547
|
+
[key: string]: any;
|
|
3548
|
+
}
|
|
3549
|
+
interface OpenAIProviderConfig extends ModelProviderConfig {
|
|
3550
|
+
organization?: string;
|
|
3551
|
+
project?: string;
|
|
3552
|
+
fetchOptions?: OpenAIFetchOptions;
|
|
3553
|
+
}
|
|
3554
|
+
declare class OpenAIProvider extends BaseModelProvider implements ModelProvider {
|
|
3555
|
+
private client;
|
|
3556
|
+
protected config: OpenAIProviderConfig;
|
|
3557
|
+
constructor(config: OpenAIProviderConfig);
|
|
3558
|
+
get chat(): {
|
|
3559
|
+
completions: {
|
|
3560
|
+
create: (params: ChatCompletionParams) => Promise<ChatCompletion>;
|
|
3561
|
+
stream: (params: ChatCompletionParams) => AsyncIterable<ChatCompletionChunk>;
|
|
3562
|
+
};
|
|
3563
|
+
};
|
|
3564
|
+
getProviderName(): string;
|
|
3565
|
+
getDefaultModel(): string;
|
|
3566
|
+
validateConfig(config: any): boolean;
|
|
3567
|
+
formatTools(tools: any[]): ToolDefinition[];
|
|
3568
|
+
parseToolCalls(response: ChatCompletion): ToolCall[];
|
|
3569
|
+
createCompletion(params: ChatCompletionParams): Promise<ChatCompletion>;
|
|
3570
|
+
createStream(params: ChatCompletionParams): AsyncIterable<ChatCompletionChunk>;
|
|
3571
|
+
private convertToOpenAIParams;
|
|
3572
|
+
private convertFromOpenAIResponse;
|
|
3573
|
+
private convertFromOpenAIChunk;
|
|
3574
|
+
private handleOpenAIError;
|
|
3575
|
+
getAvailableModels(): Promise<string[]>;
|
|
3576
|
+
isModelAvailable(model: string): Promise<boolean>;
|
|
3577
|
+
getModelInfo(model: string): Promise<any>;
|
|
3578
|
+
estimateTokens(params: ChatCompletionParams): Promise<number>;
|
|
3579
|
+
getUsage(startDate?: Date, endDate?: Date): Promise<any>;
|
|
3580
|
+
}
|
|
3581
|
+
declare function createOpenAIProvider(config: OpenAIProviderConfig): OpenAIProvider;
|
|
3582
|
+
declare function createDefaultOpenAIProvider(apiKey: string, options?: Partial<OpenAIProviderConfig>): OpenAIProvider;
|
|
3583
|
+
|
|
3584
|
+
interface AnthropicProviderConfig extends ModelProviderConfig {
|
|
3585
|
+
anthropicVersion?: string;
|
|
3586
|
+
}
|
|
3587
|
+
declare class AnthropicProvider extends BaseModelProvider implements ModelProvider {
|
|
3588
|
+
private client;
|
|
3589
|
+
protected config: AnthropicProviderConfig;
|
|
3590
|
+
constructor(config: AnthropicProviderConfig);
|
|
3591
|
+
get chat(): {
|
|
3592
|
+
completions: {
|
|
3593
|
+
create: (params: ChatCompletionParams) => Promise<ChatCompletion>;
|
|
3594
|
+
stream: (params: ChatCompletionParams) => AsyncIterable<ChatCompletionChunk>;
|
|
3595
|
+
};
|
|
3596
|
+
};
|
|
3597
|
+
getProviderName(): string;
|
|
3598
|
+
getDefaultModel(): string;
|
|
3599
|
+
validateConfig(config: any): boolean;
|
|
3600
|
+
formatTools(tools: any[]): ToolDefinition[];
|
|
3601
|
+
parseToolCalls(response: ChatCompletion): ToolCall[];
|
|
3602
|
+
createCompletion(params: ChatCompletionParams): Promise<ChatCompletion>;
|
|
3603
|
+
createStream(params: ChatCompletionParams): AsyncIterable<ChatCompletionChunk>;
|
|
3604
|
+
private convertToAnthropicParams;
|
|
3605
|
+
private convertFromAnthropicResponse;
|
|
3606
|
+
private convertFromAnthropicChunk;
|
|
3607
|
+
private mapFinishReason;
|
|
3608
|
+
private handleAnthropicError;
|
|
3609
|
+
getAvailableModels(): Promise<string[]>;
|
|
3610
|
+
isModelAvailable(model: string): Promise<boolean>;
|
|
3611
|
+
getModelInfo(model: string): Promise<any>;
|
|
3612
|
+
estimateTokens(params: ChatCompletionParams): Promise<number>;
|
|
3613
|
+
getUsage(startDate?: Date, endDate?: Date): Promise<any>;
|
|
3614
|
+
}
|
|
3615
|
+
declare function createAnthropicProvider(config: AnthropicProviderConfig): AnthropicProvider;
|
|
3616
|
+
declare function createDefaultAnthropicProvider(apiKey: string, options?: Partial<AnthropicProviderConfig>): AnthropicProvider;
|
|
3617
|
+
|
|
3618
|
+
declare class ProviderFactory implements ModelProviderFactory {
|
|
3619
|
+
private registry;
|
|
3620
|
+
constructor(registry?: ModelProviderRegistry);
|
|
3621
|
+
createOpenAI(config: ModelProviderConfig): ModelProvider;
|
|
3622
|
+
createAnthropic(config: ModelProviderConfig): ModelProvider;
|
|
3623
|
+
createCustom(config: ModelProviderConfig & {
|
|
3624
|
+
providerType: string;
|
|
3625
|
+
}): ModelProvider;
|
|
3626
|
+
createFromEnv(providerType?: string): ModelProvider;
|
|
3627
|
+
private getApiKeyFromEnv;
|
|
3628
|
+
private getBaseURLFromEnv;
|
|
3629
|
+
private getDefaultModelFromEnv;
|
|
3630
|
+
private getTimeoutFromEnv;
|
|
3631
|
+
private getMaxRetriesFromEnv;
|
|
3632
|
+
private getRetryDelayFromEnv;
|
|
3633
|
+
createWithValidation(config: ModelProviderConfig & {
|
|
3634
|
+
providerType: string;
|
|
3635
|
+
}): ModelProvider;
|
|
3636
|
+
private validateConfig;
|
|
3637
|
+
getRegistry(): ModelProviderRegistry;
|
|
3638
|
+
}
|
|
3639
|
+
declare function createOpenAIProviderFromEnv(): ModelProvider;
|
|
3640
|
+
declare function createAnthropicProviderFromEnv(): ModelProvider;
|
|
3641
|
+
declare function createProviderFromConfig(config: {
|
|
3642
|
+
type: 'openai' | 'anthropic';
|
|
3643
|
+
apiKey: string;
|
|
3644
|
+
baseURL?: string;
|
|
3645
|
+
defaultModel?: string;
|
|
3646
|
+
timeout?: number;
|
|
3647
|
+
maxRetries?: number;
|
|
3648
|
+
retryDelay?: number;
|
|
3649
|
+
}): ModelProvider;
|
|
3650
|
+
declare function createProviderFromApiKey(apiKey: string, options?: Partial<ModelProviderConfig>): ModelProvider;
|
|
3651
|
+
declare const ProviderPresets: {
|
|
3652
|
+
openai: {
|
|
3653
|
+
gpt4: (apiKey: string) => ModelProvider;
|
|
3654
|
+
gpt4Turbo: (apiKey: string) => ModelProvider;
|
|
3655
|
+
gpt35Turbo: (apiKey: string) => ModelProvider;
|
|
3656
|
+
};
|
|
3657
|
+
anthropic: {
|
|
3658
|
+
claude3Opus: (apiKey: string) => ModelProvider;
|
|
3659
|
+
claude3Sonnet: (apiKey: string) => ModelProvider;
|
|
3660
|
+
claude3Haiku: (apiKey: string) => ModelProvider;
|
|
3661
|
+
};
|
|
3662
|
+
};
|
|
3663
|
+
declare const providerFactory: ProviderFactory;
|
|
3664
|
+
|
|
3665
|
+
/**
|
|
3666
|
+
* Configuration validation using Zod schemas
|
|
3667
|
+
*/
|
|
3668
|
+
|
|
3669
|
+
declare const ModelSettingsSchema: z.ZodObject<{
|
|
3670
|
+
temperature: z.ZodOptional<z.ZodNumber>;
|
|
3671
|
+
maxTokens: z.ZodOptional<z.ZodNumber>;
|
|
3672
|
+
topP: z.ZodOptional<z.ZodNumber>;
|
|
3673
|
+
frequencyPenalty: z.ZodOptional<z.ZodNumber>;
|
|
3674
|
+
presencePenalty: z.ZodOptional<z.ZodNumber>;
|
|
3675
|
+
stop: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
3676
|
+
}, z.core.$strip>;
|
|
3677
|
+
declare const HumanInTheLoopConfigSchema: z.ZodObject<{
|
|
3678
|
+
enabled: z.ZodBoolean;
|
|
3679
|
+
requireApproval: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
3680
|
+
highRiskTools: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
3681
|
+
timeout: z.ZodOptional<z.ZodNumber>;
|
|
3682
|
+
defaultAction: z.ZodOptional<z.ZodEnum<{
|
|
3683
|
+
approve: "approve";
|
|
3684
|
+
reject: "reject";
|
|
3685
|
+
pause: "pause";
|
|
3686
|
+
}>>;
|
|
3687
|
+
}, z.core.$strip>;
|
|
3688
|
+
declare const ControlFlowConfigSchema: z.ZodObject<{
|
|
3689
|
+
maxSteps: z.ZodOptional<z.ZodNumber>;
|
|
3690
|
+
errorRetryLimit: z.ZodOptional<z.ZodNumber>;
|
|
3691
|
+
pauseOnHumanInput: z.ZodOptional<z.ZodBoolean>;
|
|
3692
|
+
customHandler: z.ZodOptional<z.ZodObject<{
|
|
3693
|
+
handleNextStep: z.ZodCustom<CallableFunction, CallableFunction>;
|
|
3694
|
+
handleToolCall: z.ZodCustom<CallableFunction, CallableFunction>;
|
|
3695
|
+
handleError: z.ZodCustom<CallableFunction, CallableFunction>;
|
|
3696
|
+
}, z.core.$strip>>;
|
|
3697
|
+
}, z.core.$strip>;
|
|
3698
|
+
declare const ToolDefinitionSchema: z.ZodObject<{
|
|
3699
|
+
name: z.ZodString;
|
|
3700
|
+
description: z.ZodOptional<z.ZodString>;
|
|
3701
|
+
schema: z.ZodOptional<z.ZodAny>;
|
|
3702
|
+
invoke: z.ZodOptional<z.ZodCustom<CallableFunction, CallableFunction>>;
|
|
3703
|
+
requiresApproval: z.ZodOptional<z.ZodBoolean>;
|
|
3704
|
+
}, z.core.$strip>;
|
|
3705
|
+
declare const OutputTypeSchema: z.ZodUnion<readonly [z.ZodLiteral<"string">, z.ZodLiteral<"structured">, z.ZodObject<{
|
|
3706
|
+
schema: z.ZodAny;
|
|
3707
|
+
}, z.core.$strip>]>;
|
|
3708
|
+
declare const AgentConfigSchema: z.ZodObject<{
|
|
3709
|
+
name: z.ZodString;
|
|
3710
|
+
description: z.ZodOptional<z.ZodString>;
|
|
3711
|
+
model: z.ZodUnion<readonly [z.ZodString, z.ZodAny]>;
|
|
3712
|
+
instructions: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodCustom<CallableFunction, CallableFunction>]>>;
|
|
3713
|
+
stateType: z.ZodOptional<z.ZodAny>;
|
|
3714
|
+
outputType: z.ZodOptional<z.ZodUnion<readonly [z.ZodLiteral<"string">, z.ZodLiteral<"structured">, z.ZodObject<{
|
|
3715
|
+
schema: z.ZodAny;
|
|
3716
|
+
}, z.core.$strip>]>>;
|
|
3717
|
+
modelSettings: z.ZodOptional<z.ZodObject<{
|
|
3718
|
+
temperature: z.ZodOptional<z.ZodNumber>;
|
|
3719
|
+
maxTokens: z.ZodOptional<z.ZodNumber>;
|
|
3720
|
+
topP: z.ZodOptional<z.ZodNumber>;
|
|
3721
|
+
frequencyPenalty: z.ZodOptional<z.ZodNumber>;
|
|
3722
|
+
presencePenalty: z.ZodOptional<z.ZodNumber>;
|
|
3723
|
+
stop: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
3724
|
+
}, z.core.$strip>>;
|
|
3725
|
+
tools: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
3726
|
+
name: z.ZodString;
|
|
3727
|
+
description: z.ZodOptional<z.ZodString>;
|
|
3728
|
+
schema: z.ZodOptional<z.ZodAny>;
|
|
3729
|
+
invoke: z.ZodOptional<z.ZodCustom<CallableFunction, CallableFunction>>;
|
|
3730
|
+
requiresApproval: z.ZodOptional<z.ZodBoolean>;
|
|
3731
|
+
}, z.core.$strip>>>;
|
|
3732
|
+
memory: z.ZodOptional<z.ZodAny>;
|
|
3733
|
+
humanInTheLoop: z.ZodOptional<z.ZodObject<{
|
|
3734
|
+
enabled: z.ZodBoolean;
|
|
3735
|
+
requireApproval: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
3736
|
+
highRiskTools: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
3737
|
+
timeout: z.ZodOptional<z.ZodNumber>;
|
|
3738
|
+
defaultAction: z.ZodOptional<z.ZodEnum<{
|
|
3739
|
+
approve: "approve";
|
|
3740
|
+
reject: "reject";
|
|
3741
|
+
pause: "pause";
|
|
3742
|
+
}>>;
|
|
3743
|
+
}, z.core.$strip>>;
|
|
3744
|
+
controlFlow: z.ZodOptional<z.ZodObject<{
|
|
3745
|
+
maxSteps: z.ZodOptional<z.ZodNumber>;
|
|
3746
|
+
errorRetryLimit: z.ZodOptional<z.ZodNumber>;
|
|
3747
|
+
pauseOnHumanInput: z.ZodOptional<z.ZodBoolean>;
|
|
3748
|
+
customHandler: z.ZodOptional<z.ZodObject<{
|
|
3749
|
+
handleNextStep: z.ZodCustom<CallableFunction, CallableFunction>;
|
|
3750
|
+
handleToolCall: z.ZodCustom<CallableFunction, CallableFunction>;
|
|
3751
|
+
handleError: z.ZodCustom<CallableFunction, CallableFunction>;
|
|
3752
|
+
}, z.core.$strip>>;
|
|
3753
|
+
}, z.core.$strip>>;
|
|
3754
|
+
}, z.core.$strip>;
|
|
3755
|
+
declare const RunOptionsSchema: z.ZodObject<{
|
|
3756
|
+
state: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
3757
|
+
conversationId: z.ZodOptional<z.ZodString>;
|
|
3758
|
+
runId: z.ZodOptional<z.ZodString>;
|
|
3759
|
+
userId: z.ZodOptional<z.ZodString>;
|
|
3760
|
+
model: z.ZodOptional<z.ZodString>;
|
|
3761
|
+
temperature: z.ZodOptional<z.ZodNumber>;
|
|
3762
|
+
maxTokens: z.ZodOptional<z.ZodNumber>;
|
|
3763
|
+
stream: z.ZodOptional<z.ZodBoolean>;
|
|
3764
|
+
tools: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
3765
|
+
name: z.ZodString;
|
|
3766
|
+
description: z.ZodOptional<z.ZodString>;
|
|
3767
|
+
schema: z.ZodOptional<z.ZodAny>;
|
|
3768
|
+
invoke: z.ZodOptional<z.ZodCustom<CallableFunction, CallableFunction>>;
|
|
3769
|
+
requiresApproval: z.ZodOptional<z.ZodBoolean>;
|
|
3770
|
+
}, z.core.$strip>>>;
|
|
3771
|
+
humanInTheLoop: z.ZodOptional<z.ZodObject<{
|
|
3772
|
+
enabled: z.ZodBoolean;
|
|
3773
|
+
requireApproval: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
3774
|
+
highRiskTools: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
3775
|
+
timeout: z.ZodOptional<z.ZodNumber>;
|
|
3776
|
+
defaultAction: z.ZodOptional<z.ZodEnum<{
|
|
3777
|
+
approve: "approve";
|
|
3778
|
+
reject: "reject";
|
|
3779
|
+
pause: "pause";
|
|
3780
|
+
}>>;
|
|
3781
|
+
}, z.core.$strip>>;
|
|
3782
|
+
controlFlow: z.ZodOptional<z.ZodObject<{
|
|
3783
|
+
maxSteps: z.ZodOptional<z.ZodNumber>;
|
|
3784
|
+
errorRetryLimit: z.ZodOptional<z.ZodNumber>;
|
|
3785
|
+
pauseOnHumanInput: z.ZodOptional<z.ZodBoolean>;
|
|
3786
|
+
customHandler: z.ZodOptional<z.ZodObject<{
|
|
3787
|
+
handleNextStep: z.ZodCustom<CallableFunction, CallableFunction>;
|
|
3788
|
+
handleToolCall: z.ZodCustom<CallableFunction, CallableFunction>;
|
|
3789
|
+
handleError: z.ZodCustom<CallableFunction, CallableFunction>;
|
|
3790
|
+
}, z.core.$strip>>;
|
|
3791
|
+
}, z.core.$strip>>;
|
|
3792
|
+
}, z.core.$strip>;
|
|
3793
|
+
declare class ConfigValidator {
|
|
3794
|
+
private static instance;
|
|
3795
|
+
static getInstance(): ConfigValidator;
|
|
3796
|
+
validateAgentConfig<T extends Record<string, unknown>>(config: unknown, context?: Record<string, unknown>): T;
|
|
3797
|
+
validateRunOptions<T extends Record<string, unknown>>(options: unknown, context?: Record<string, unknown>): T;
|
|
3798
|
+
validateModelSettings<T extends Record<string, unknown>>(settings: unknown): T;
|
|
3799
|
+
validateToolDefinitions<T extends Record<string, unknown>[]>(tools: unknown): T;
|
|
3800
|
+
validateHumanInTheLoopConfig<T extends Record<string, unknown>>(config: unknown): T;
|
|
3801
|
+
validateControlFlowConfig<T extends Record<string, unknown>>(config: unknown): T;
|
|
3802
|
+
checkRequiredFields(config: Record<string, unknown>, requiredFields: string[]): void;
|
|
3803
|
+
private formatZodErrors;
|
|
3804
|
+
validateWithCustomSchema<T>(data: unknown, schema: z.ZodSchema<T>, errorMessage: string): T;
|
|
3805
|
+
}
|
|
3806
|
+
declare function validateConfig(schema: z.ZodSchema): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
|
|
3807
|
+
declare const configValidator: ConfigValidator;
|
|
3808
|
+
declare function validateAgentName(name: unknown): string;
|
|
3809
|
+
declare function validateModelProvider(provider: unknown): string | object;
|
|
3810
|
+
declare function validateInstructions(instructions: unknown): string | Function;
|
|
3811
|
+
|
|
3812
|
+
/**
|
|
3813
|
+
* Tool Proxy System for managing and executing tools
|
|
3814
|
+
* Provides a unified interface for tool management and execution
|
|
3815
|
+
*/
|
|
3816
|
+
declare class ToolProxy<TState extends StateConstraint = StateConstraint> {
|
|
3817
|
+
private tools;
|
|
3818
|
+
private eventSystem;
|
|
3819
|
+
private executionContext;
|
|
3820
|
+
constructor(eventSystem: EventSystem, context?: Partial<ToolExecutionContext>);
|
|
3821
|
+
/**
|
|
3822
|
+
* Register a tool with the proxy
|
|
3823
|
+
*/
|
|
3824
|
+
registerTool(tool: BaseTool<any, TState>): void;
|
|
3825
|
+
/**
|
|
3826
|
+
* Remove a tool from the proxy
|
|
3827
|
+
*/
|
|
3828
|
+
unregisterTool(toolName: string): boolean;
|
|
3829
|
+
/**
|
|
3830
|
+
* Get a tool by name
|
|
3831
|
+
*/
|
|
3832
|
+
getTool(toolName: string): BaseTool<any, TState> | undefined;
|
|
3833
|
+
/**
|
|
3834
|
+
* Get all registered tools
|
|
3835
|
+
*/
|
|
3836
|
+
getAllTools(): BaseTool<any, TState>[];
|
|
3837
|
+
/**
|
|
3838
|
+
* Get the number of registered tools
|
|
3839
|
+
*/
|
|
3840
|
+
getToolCount(): number;
|
|
3841
|
+
/**
|
|
3842
|
+
* Get tool names
|
|
3843
|
+
*/
|
|
3844
|
+
getToolNames(): string[];
|
|
3845
|
+
/**
|
|
3846
|
+
* Check if a tool is registered
|
|
3847
|
+
*/
|
|
3848
|
+
hasTool(toolName: string): boolean;
|
|
3849
|
+
/**
|
|
3850
|
+
* Execute a tool with the given input
|
|
3851
|
+
*/
|
|
3852
|
+
executeTool(toolName: string, input: unknown, context?: Partial<ToolExecutionContext>, state?: TState): Promise<unknown>;
|
|
3853
|
+
/**
|
|
3854
|
+
* Execute multiple tools in parallel
|
|
3855
|
+
*/
|
|
3856
|
+
executeToolsParallel(executions: Array<{
|
|
3857
|
+
toolName: string;
|
|
3858
|
+
input: unknown;
|
|
3859
|
+
context?: Partial<ToolExecutionContext>;
|
|
3860
|
+
}>, state?: TState): Promise<Array<{
|
|
3861
|
+
toolName: string;
|
|
3862
|
+
result: unknown;
|
|
3863
|
+
error?: Error;
|
|
3864
|
+
}>>;
|
|
3865
|
+
/**
|
|
3866
|
+
* Execute multiple tools sequentially
|
|
3867
|
+
*/
|
|
3868
|
+
executeToolsSequential(executions: Array<{
|
|
3869
|
+
toolName: string;
|
|
3870
|
+
input: unknown;
|
|
3871
|
+
context?: Partial<ToolExecutionContext>;
|
|
3872
|
+
}>, state?: TState): Promise<Array<{
|
|
3873
|
+
toolName: string;
|
|
3874
|
+
result: unknown;
|
|
3875
|
+
error?: Error;
|
|
3876
|
+
}>>;
|
|
3877
|
+
/**
|
|
3878
|
+
* Validate tool input against schema if available
|
|
3879
|
+
*/
|
|
3880
|
+
validateToolInput(toolName: string, input: unknown): boolean;
|
|
3881
|
+
/**
|
|
3882
|
+
* Get tool metadata
|
|
3883
|
+
*/
|
|
3884
|
+
getToolMetadata(toolName: string): {
|
|
3885
|
+
name: string;
|
|
3886
|
+
description?: string;
|
|
3887
|
+
hasSchema: boolean;
|
|
3888
|
+
requiresApproval: boolean;
|
|
3889
|
+
} | null;
|
|
3890
|
+
/**
|
|
3891
|
+
* Update execution context
|
|
3892
|
+
*/
|
|
3893
|
+
updateExecutionContext(context: Partial<ToolExecutionContext>): void;
|
|
3894
|
+
/**
|
|
3895
|
+
* Get current execution context
|
|
3896
|
+
*/
|
|
3897
|
+
getExecutionContext(): ToolExecutionContext;
|
|
3898
|
+
/**
|
|
3899
|
+
* Clear all tools
|
|
3900
|
+
*/
|
|
3901
|
+
clearTools(): void;
|
|
3902
|
+
/**
|
|
3903
|
+
* Get tools that require approval
|
|
3904
|
+
*/
|
|
3905
|
+
getToolsRequiringApproval(): BaseTool<any, TState>[];
|
|
3906
|
+
/**
|
|
3907
|
+
* Validate tool definition
|
|
3908
|
+
*/
|
|
3909
|
+
private validateToolDefinition;
|
|
3910
|
+
}
|
|
3911
|
+
|
|
3912
|
+
/**
|
|
3913
|
+
* Create a tool with Zod schema validation
|
|
3914
|
+
*/
|
|
3915
|
+
declare function createTypedTool<TInput = any, TState extends StateConstraint = StateConstraint>(name: string, description: string, schema: z.ZodObject<any>, handler: (input: TInput, context: ToolExecutionContext & {
|
|
3916
|
+
state?: TState;
|
|
3917
|
+
}) => Promise<unknown>, options?: {
|
|
3918
|
+
requiresApproval?: boolean;
|
|
3919
|
+
}): _cloudbase_agent_tools.DynamicTool<any, Record<string, unknown>, any>;
|
|
3920
|
+
/**
|
|
3921
|
+
* Create a tool with input/output logging
|
|
3922
|
+
*/
|
|
3923
|
+
declare function createLoggedTool<TState extends StateConstraint = StateConstraint>(name: string, description: string, handler: (input: any, context: ToolExecutionContext & {
|
|
3924
|
+
state?: TState;
|
|
3925
|
+
}) => Promise<any>, options?: {
|
|
3926
|
+
schema?: z.ZodObject<any>;
|
|
3927
|
+
requiresApproval?: boolean;
|
|
3928
|
+
logLevel?: "debug" | "info" | "warn" | "error";
|
|
3929
|
+
}): BaseTool<any, TState>;
|
|
3930
|
+
/**
|
|
3931
|
+
* Create a tool with retry logic
|
|
3932
|
+
*/
|
|
3933
|
+
declare function createRetryTool<TState extends StateConstraint = StateConstraint>(name: string, description: string, handler: (input: any, context: ToolExecutionContext & {
|
|
3934
|
+
state?: TState;
|
|
3935
|
+
}) => Promise<any>, options?: {
|
|
3936
|
+
schema?: z.ZodObject<any>;
|
|
3937
|
+
requiresApproval?: boolean;
|
|
3938
|
+
maxRetries?: number;
|
|
3939
|
+
retryDelay?: number;
|
|
3940
|
+
retryCondition?: (error: Error) => boolean;
|
|
3941
|
+
}): BaseTool<any, TState>;
|
|
3942
|
+
/**
|
|
3943
|
+
* Create a tool with timeout
|
|
3944
|
+
*/
|
|
3945
|
+
declare function createTimeoutTool<TState extends StateConstraint = StateConstraint>(name: string, description: string, handler: (input: any, context: ToolExecutionContext & {
|
|
3946
|
+
state?: TState;
|
|
3947
|
+
}) => Promise<any>, options?: {
|
|
3948
|
+
schema?: z.ZodObject<any>;
|
|
3949
|
+
requiresApproval?: boolean;
|
|
3950
|
+
timeout?: number;
|
|
3951
|
+
}): BaseTool<any, TState>;
|
|
3952
|
+
/**
|
|
3953
|
+
* Create a tool that combines multiple tools
|
|
3954
|
+
*/
|
|
3955
|
+
declare function createCompositeTool<TState extends StateConstraint = StateConstraint>(name: string, description: string, tools: BaseTool<any, TState>[], options?: {
|
|
3956
|
+
requiresApproval?: boolean;
|
|
3957
|
+
executionMode?: "sequential" | "parallel";
|
|
3958
|
+
}): BaseTool<any, TState>;
|
|
3959
|
+
|
|
3960
|
+
/**
|
|
3961
|
+
* Conversation management utilities for handling conversations and state in message metadata
|
|
3962
|
+
*/
|
|
3963
|
+
declare class ConversationManager<TState extends StateConstraint = StateConstraint> {
|
|
3964
|
+
conversations: Map<string, Message[]>;
|
|
3965
|
+
private currentState;
|
|
3966
|
+
/**
|
|
3967
|
+
* Create a message with state metadata
|
|
3968
|
+
*/
|
|
3969
|
+
createMessageWithState(role: Message['role'], content: string, conversationId: string, options?: {
|
|
3970
|
+
runId?: string;
|
|
3971
|
+
userId?: string;
|
|
3972
|
+
toolCalls?: any[];
|
|
3973
|
+
toolCallId?: string;
|
|
3974
|
+
}): Message;
|
|
3975
|
+
/**
|
|
3976
|
+
* Extract current state from messages
|
|
3977
|
+
*/
|
|
3978
|
+
extractStateFromMessages(messages: Message[]): TState;
|
|
3979
|
+
/**
|
|
3980
|
+
* Get conversation messages
|
|
3981
|
+
*/
|
|
3982
|
+
getConversationMessages(conversationId: string): Message[];
|
|
3983
|
+
/**
|
|
3984
|
+
* Add messages to conversation
|
|
3985
|
+
*/
|
|
3986
|
+
addMessages(conversationId: string, messages: Message[]): void;
|
|
3987
|
+
/**
|
|
3988
|
+
* Get current state for conversation
|
|
3989
|
+
*/
|
|
3990
|
+
getCurrentState(conversationId: string): TState;
|
|
3991
|
+
/**
|
|
3992
|
+
* Update state for conversation
|
|
3993
|
+
*/
|
|
3994
|
+
updateState(conversationId: string, stateChanges: Partial<TState>): void;
|
|
3995
|
+
/**
|
|
3996
|
+
* Clear conversation data
|
|
3997
|
+
*/
|
|
3998
|
+
clearConversation(conversationId: string): void;
|
|
3999
|
+
/**
|
|
4000
|
+
* Deep clone an object to prevent reference sharing
|
|
4001
|
+
*/
|
|
4002
|
+
private deepClone;
|
|
4003
|
+
}
|
|
4004
|
+
|
|
4005
|
+
/**
|
|
4006
|
+
* AGKitAgent - AG-UI protocol implementation for AG-Kit Agent
|
|
4007
|
+
*/
|
|
4008
|
+
|
|
4009
|
+
declare class AGKitAgent extends AbstractAgent {
|
|
4010
|
+
private agent;
|
|
4011
|
+
constructor(agent: Agent, config?: Partial<AgentConfig$1>);
|
|
4012
|
+
run(input: RunAgentInput): Observable<BaseEvent$2>;
|
|
4013
|
+
private _run;
|
|
4014
|
+
/**
|
|
4015
|
+
* Bridge a single AG-Kit event to AG-UI event format
|
|
4016
|
+
* This is called per-event for a specific run (no global listeners)
|
|
4017
|
+
*/
|
|
4018
|
+
private bridgeEvent;
|
|
4019
|
+
destroy(): void;
|
|
4020
|
+
}
|
|
4021
|
+
|
|
4022
|
+
/**
|
|
4023
|
+
* Converter utility to transform AG-Kit Agent to AG-UI AbstractAgent
|
|
4024
|
+
*/
|
|
4025
|
+
|
|
4026
|
+
/**
|
|
4027
|
+
* Convert AG-Kit Agent to AbstractAgent for AG-UI server integration
|
|
4028
|
+
*/
|
|
4029
|
+
declare function toAGUIAgent(agent: Agent | AbstractAgent, config?: Partial<AgentConfig$1>): AbstractAgent;
|
|
4030
|
+
|
|
4031
|
+
export { AGKitAgent, AGKitError, Agent, type AgentConfig, AgentConfigSchema, type AgentError, type AgentEvent, type AgentResult, type AgentState, AnthropicProvider, type AnthropicProviderConfig, type ApprovalReceivedEvent, type ApprovalRequiredEvent, type BaseEvent$1 as BaseEvent, BaseLongTermMemory, BaseMemory, BaseModelProvider, type BranchInfo, type ChatCompletion, type ChatCompletionChunk, type ChatCompletionParams, CloudBaseMemory, type CompactionMetadata, ConfigValidator, ConfigurationError, type ContextThresholds, type ControlFlowConfig, ControlFlowConfigSchema, type ControlFlowDecision, type ControlFlowDecisionEvent, ControlFlowError, type ControlFlowHandler, type ConversationConfig, type ConversationCreatedEvent, type ConversationDeletedEvent, ConversationManager, type ConversationUpdatedEvent, type CustomEvent, ErrorCategory, ErrorContextBuilder, type ErrorEvent, type ErrorHandler, type ErrorHandlingDecision, type ErrorRecoveryStrategy, type EventData$1 as EventData, type EventHandler$1 as EventHandler, EventSystem, EventType$1 as EventType, ExecutionError, type ExecutionPausedEvent, type ExecutionResumedEvent, FallbackModelStrategy, HumanApprovalRequiredError, type HumanInTheLoopConfig, HumanInTheLoopConfigSchema, type IMemoryClientOptions, type IMemoryEvent, type ITokenizer, InMemoryMemory, type InstructionProvider, InvalidModelProviderError, InvalidModelResponseError, type ListOptions, LoggingErrorHandler, Mem0LongTermMemory, type Mem0LongTermMemoryConfig, MemoryClient, type MemoryEntity, MemoryError, type MemoryEventEntity, MemoryLimitError, type MemoryProvider, type MemoryQuery, type Message, type MessageMetadata, type MessagesSnapshotEvent, MissingRequiredConfigError, ModelError, type ModelProvider$1 as ModelProvider, type ModelProviderConfig, ModelProviderError$1 as ModelProviderError, ModelProviderRegistry, type ModelSettings, ModelSettingsSchema, MongoDBMemory, type MongoDBMemoryConfig, type MySQLConnectionConfig, MySQLMemory, type MySQLMemoryConfig, NetworkError, OpenAIProvider, type OpenAIProviderConfig, OptimizedEventSystem, Order, type OutputConstraint, OutputConstraintSchema, type OutputType, OutputTypeSchema, ParamError, ProviderFactory, ProviderPresets, type QueryOptions$1 as QueryOptions, RateLimitError, type RawEvent, RetryErrorHandler, type RunContext, type RunErrorEvent, type RunFinishedEvent, type RunOptions, RunOptionsSchema, type RunStartedEvent, RuntimeError, SchemaValidationError, type SearchOptions, type StateConstraint, StateConstraintSchema, type StateDeltaEvent, StateError, type StateSnapshotEvent, type StateUpdateEvent, type StepFinishedEvent, type StepStartedEvent, type StructuredSummary, TDAIException, TDAILongTermMemory, type TDAILongTermMemoryConfig, TDAIMemory, type Message$1 as TDAIMessage, type TextMessageChunkEvent, type TextMessageContentEvent, type TextMessageEndEvent, type TextMessageStartEvent, TiktokenTokenizer, TimeoutError, TokenLimitError, TokenTrimmer, type TokenUsage, type ToolCall$1 as ToolCall, type ToolCallArgsEvent, type ToolCallChunkEvent, type ToolCallDecision, type ToolCallEndEvent, type ToolCallResultEvent, type ToolCallStartEvent, ToolDefinitionSchema, ToolError, ToolExecutionError, ToolNotFoundError, ToolProxy, type ToolRegisteredEvent, type ToolUnregisteredEvent, ToolValidationError, type ToolValidationFailedEvent, TypeChecker, TypeORMAdapter, TypeORMMemory, type TypeORMMemoryConfig, TypeORMQueryBuilder, TypeORMUpdateBuilder, ValidationError, type WhereClause, configValidator, createAnthropicProvider, createAnthropicProviderFromEnv, createCompositeTool, createDefaultAnthropicProvider, createDefaultOpenAIProvider, createErrorContext, createLoggedTool, createOpenAIProvider, createOpenAIProviderFromEnv, createOptimizedEventSystem, createProviderFromApiKey, createProviderFromConfig, createRetryTool, createTimeoutTool, createTypeSafeOutput, createTypeSafeState, createTypedTool, isConfigurationError, isOutputConstraint, isRetryableError, isStateConstraint, isToolError, modelProviderRegistry, optimizedEventSystem, providerFactory, toAGUIAgent, typeChecker, validateAgentName, validateConfig, validateInstructions, validateModelProvider, validateOutput, validateState, validateTypes };
|