@opperai/agents 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +210 -0
- package/dist/index.cjs +2656 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +1927 -0
- package/dist/index.d.ts +1927 -0
- package/dist/index.js +2616 -0
- package/dist/index.js.map +1 -0
- package/package.json +65 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,1927 @@
|
|
|
1
|
+
import { ZodType, z, ZodTypeAny, ZodIssue } from 'zod';
|
|
2
|
+
import { Opper } from 'opperai';
|
|
3
|
+
import { Implementation } from '@modelcontextprotocol/sdk/types.js';
|
|
4
|
+
import { JsonSchema7Type } from 'zod-to-json-schema';
|
|
5
|
+
|
|
6
|
+
interface Success<T> {
|
|
7
|
+
readonly ok: true;
|
|
8
|
+
readonly value: T;
|
|
9
|
+
}
|
|
10
|
+
interface Failure<E> {
|
|
11
|
+
readonly ok: false;
|
|
12
|
+
readonly error: E;
|
|
13
|
+
}
|
|
14
|
+
type Result<T, E = Error> = Success<T> | Failure<E>;
|
|
15
|
+
declare const Result: {
|
|
16
|
+
ok<T>(value: T): Success<T>;
|
|
17
|
+
err<E>(error: E): Failure<E>;
|
|
18
|
+
isOk<T, E>(result: Result<T, E>): result is Success<T>;
|
|
19
|
+
isErr<T, E>(result: Result<T, E>): result is Failure<E>;
|
|
20
|
+
map<T, E, U>(result: Result<T, E>, mapper: (value: T) => U): Result<U, E>;
|
|
21
|
+
mapError<T, E, F>(result: Result<T, E>, mapper: (error: E) => F): Result<T, F>;
|
|
22
|
+
unwrapOr<T, E>(result: Result<T, E>, fallback: T): T;
|
|
23
|
+
};
|
|
24
|
+
declare const ok: <T>(value: T) => Success<T>;
|
|
25
|
+
declare const err: <E>(error: E) => Failure<E>;
|
|
26
|
+
|
|
27
|
+
declare const ToolMetadataSchema: z.ZodRecord<z.ZodString, z.ZodUnknown>;
|
|
28
|
+
declare const ToolCallRecordSchema: z.ZodObject<{
|
|
29
|
+
id: z.ZodDefault<z.ZodOptional<z.ZodString>>;
|
|
30
|
+
toolName: z.ZodString;
|
|
31
|
+
input: z.ZodUnknown;
|
|
32
|
+
output: z.ZodOptional<z.ZodUnknown>;
|
|
33
|
+
success: z.ZodOptional<z.ZodBoolean>;
|
|
34
|
+
error: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodObject<{
|
|
35
|
+
message: z.ZodString;
|
|
36
|
+
}, "passthrough", ZodTypeAny, z.objectOutputType<{
|
|
37
|
+
message: z.ZodString;
|
|
38
|
+
}, ZodTypeAny, "passthrough">, z.objectInputType<{
|
|
39
|
+
message: z.ZodString;
|
|
40
|
+
}, ZodTypeAny, "passthrough">>]>>;
|
|
41
|
+
startedAt: z.ZodDefault<z.ZodNumber>;
|
|
42
|
+
finishedAt: z.ZodOptional<z.ZodNumber>;
|
|
43
|
+
metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
44
|
+
}, "strip", ZodTypeAny, {
|
|
45
|
+
id: string;
|
|
46
|
+
toolName: string;
|
|
47
|
+
startedAt: number;
|
|
48
|
+
metadata: Record<string, unknown>;
|
|
49
|
+
input?: unknown;
|
|
50
|
+
output?: unknown;
|
|
51
|
+
success?: boolean | undefined;
|
|
52
|
+
error?: string | z.objectOutputType<{
|
|
53
|
+
message: z.ZodString;
|
|
54
|
+
}, ZodTypeAny, "passthrough"> | undefined;
|
|
55
|
+
finishedAt?: number | undefined;
|
|
56
|
+
}, {
|
|
57
|
+
toolName: string;
|
|
58
|
+
id?: string | undefined;
|
|
59
|
+
input?: unknown;
|
|
60
|
+
output?: unknown;
|
|
61
|
+
success?: boolean | undefined;
|
|
62
|
+
error?: string | z.objectInputType<{
|
|
63
|
+
message: z.ZodString;
|
|
64
|
+
}, ZodTypeAny, "passthrough"> | undefined;
|
|
65
|
+
startedAt?: number | undefined;
|
|
66
|
+
finishedAt?: number | undefined;
|
|
67
|
+
metadata?: Record<string, unknown> | undefined;
|
|
68
|
+
}>;
|
|
69
|
+
type ToolCallRecord = z.infer<typeof ToolCallRecordSchema>;
|
|
70
|
+
declare const ToolResultSuccessSchema: z.ZodEffects<z.ZodObject<{
|
|
71
|
+
toolName: z.ZodString;
|
|
72
|
+
metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
73
|
+
startedAt: z.ZodOptional<z.ZodNumber>;
|
|
74
|
+
finishedAt: z.ZodOptional<z.ZodNumber>;
|
|
75
|
+
} & {
|
|
76
|
+
success: z.ZodLiteral<true>;
|
|
77
|
+
output: z.ZodAny;
|
|
78
|
+
}, "strip", ZodTypeAny, {
|
|
79
|
+
toolName: string;
|
|
80
|
+
success: true;
|
|
81
|
+
metadata: Record<string, unknown>;
|
|
82
|
+
output?: any;
|
|
83
|
+
startedAt?: number | undefined;
|
|
84
|
+
finishedAt?: number | undefined;
|
|
85
|
+
}, {
|
|
86
|
+
toolName: string;
|
|
87
|
+
success: true;
|
|
88
|
+
output?: any;
|
|
89
|
+
startedAt?: number | undefined;
|
|
90
|
+
finishedAt?: number | undefined;
|
|
91
|
+
metadata?: Record<string, unknown> | undefined;
|
|
92
|
+
}>, {
|
|
93
|
+
toolName: string;
|
|
94
|
+
success: true;
|
|
95
|
+
metadata: Record<string, unknown>;
|
|
96
|
+
output?: any;
|
|
97
|
+
startedAt?: number | undefined;
|
|
98
|
+
finishedAt?: number | undefined;
|
|
99
|
+
}, {
|
|
100
|
+
toolName: string;
|
|
101
|
+
success: true;
|
|
102
|
+
output?: any;
|
|
103
|
+
startedAt?: number | undefined;
|
|
104
|
+
finishedAt?: number | undefined;
|
|
105
|
+
metadata?: Record<string, unknown> | undefined;
|
|
106
|
+
}>;
|
|
107
|
+
declare const ToolResultFailureSchema: z.ZodObject<{
|
|
108
|
+
toolName: z.ZodString;
|
|
109
|
+
metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
110
|
+
startedAt: z.ZodOptional<z.ZodNumber>;
|
|
111
|
+
finishedAt: z.ZodOptional<z.ZodNumber>;
|
|
112
|
+
} & {
|
|
113
|
+
success: z.ZodLiteral<false>;
|
|
114
|
+
error: z.ZodUnion<[z.ZodString, ZodType<Error, z.ZodTypeDef, Error>]>;
|
|
115
|
+
}, "strip", ZodTypeAny, {
|
|
116
|
+
toolName: string;
|
|
117
|
+
success: false;
|
|
118
|
+
error: string | Error;
|
|
119
|
+
metadata: Record<string, unknown>;
|
|
120
|
+
startedAt?: number | undefined;
|
|
121
|
+
finishedAt?: number | undefined;
|
|
122
|
+
}, {
|
|
123
|
+
toolName: string;
|
|
124
|
+
success: false;
|
|
125
|
+
error: string | Error;
|
|
126
|
+
startedAt?: number | undefined;
|
|
127
|
+
finishedAt?: number | undefined;
|
|
128
|
+
metadata?: Record<string, unknown> | undefined;
|
|
129
|
+
}>;
|
|
130
|
+
declare const ToolResultSchema: z.ZodUnion<[z.ZodEffects<z.ZodObject<{
|
|
131
|
+
toolName: z.ZodString;
|
|
132
|
+
metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
133
|
+
startedAt: z.ZodOptional<z.ZodNumber>;
|
|
134
|
+
finishedAt: z.ZodOptional<z.ZodNumber>;
|
|
135
|
+
} & {
|
|
136
|
+
success: z.ZodLiteral<true>;
|
|
137
|
+
output: z.ZodAny;
|
|
138
|
+
}, "strip", ZodTypeAny, {
|
|
139
|
+
toolName: string;
|
|
140
|
+
success: true;
|
|
141
|
+
metadata: Record<string, unknown>;
|
|
142
|
+
output?: any;
|
|
143
|
+
startedAt?: number | undefined;
|
|
144
|
+
finishedAt?: number | undefined;
|
|
145
|
+
}, {
|
|
146
|
+
toolName: string;
|
|
147
|
+
success: true;
|
|
148
|
+
output?: any;
|
|
149
|
+
startedAt?: number | undefined;
|
|
150
|
+
finishedAt?: number | undefined;
|
|
151
|
+
metadata?: Record<string, unknown> | undefined;
|
|
152
|
+
}>, {
|
|
153
|
+
toolName: string;
|
|
154
|
+
success: true;
|
|
155
|
+
metadata: Record<string, unknown>;
|
|
156
|
+
output?: any;
|
|
157
|
+
startedAt?: number | undefined;
|
|
158
|
+
finishedAt?: number | undefined;
|
|
159
|
+
}, {
|
|
160
|
+
toolName: string;
|
|
161
|
+
success: true;
|
|
162
|
+
output?: any;
|
|
163
|
+
startedAt?: number | undefined;
|
|
164
|
+
finishedAt?: number | undefined;
|
|
165
|
+
metadata?: Record<string, unknown> | undefined;
|
|
166
|
+
}>, z.ZodObject<{
|
|
167
|
+
toolName: z.ZodString;
|
|
168
|
+
metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
169
|
+
startedAt: z.ZodOptional<z.ZodNumber>;
|
|
170
|
+
finishedAt: z.ZodOptional<z.ZodNumber>;
|
|
171
|
+
} & {
|
|
172
|
+
success: z.ZodLiteral<false>;
|
|
173
|
+
error: z.ZodUnion<[z.ZodString, ZodType<Error, z.ZodTypeDef, Error>]>;
|
|
174
|
+
}, "strip", ZodTypeAny, {
|
|
175
|
+
toolName: string;
|
|
176
|
+
success: false;
|
|
177
|
+
error: string | Error;
|
|
178
|
+
metadata: Record<string, unknown>;
|
|
179
|
+
startedAt?: number | undefined;
|
|
180
|
+
finishedAt?: number | undefined;
|
|
181
|
+
}, {
|
|
182
|
+
toolName: string;
|
|
183
|
+
success: false;
|
|
184
|
+
error: string | Error;
|
|
185
|
+
startedAt?: number | undefined;
|
|
186
|
+
finishedAt?: number | undefined;
|
|
187
|
+
metadata?: Record<string, unknown> | undefined;
|
|
188
|
+
}>]>;
|
|
189
|
+
type ToolResultData = z.infer<typeof ToolResultSchema>;
|
|
190
|
+
interface ToolExecutionContext {
|
|
191
|
+
agentContext: AgentContext;
|
|
192
|
+
signal?: AbortSignal;
|
|
193
|
+
metadata?: Record<string, unknown>;
|
|
194
|
+
/**
|
|
195
|
+
* Span ID for this tool execution (used as parent for nested operations)
|
|
196
|
+
*/
|
|
197
|
+
spanId?: string;
|
|
198
|
+
}
|
|
199
|
+
interface ToolSuccess<TOutput> {
|
|
200
|
+
success: true;
|
|
201
|
+
toolName: string;
|
|
202
|
+
output: TOutput;
|
|
203
|
+
metadata: Record<string, unknown>;
|
|
204
|
+
startedAt?: number;
|
|
205
|
+
finishedAt?: number;
|
|
206
|
+
}
|
|
207
|
+
interface ToolFailure {
|
|
208
|
+
success: false;
|
|
209
|
+
toolName: string;
|
|
210
|
+
error: Error | string;
|
|
211
|
+
metadata: Record<string, unknown>;
|
|
212
|
+
startedAt?: number;
|
|
213
|
+
finishedAt?: number;
|
|
214
|
+
}
|
|
215
|
+
type ToolResult<TOutput = unknown> = ToolSuccess<TOutput> | ToolFailure;
|
|
216
|
+
interface ToolDefinition<TInput, TOutput> {
|
|
217
|
+
name: string;
|
|
218
|
+
description?: string;
|
|
219
|
+
schema?: ZodType<TInput>;
|
|
220
|
+
execute: (input: TInput, context: ToolExecutionContext) => MaybePromise<ToolResult<TOutput>>;
|
|
221
|
+
metadata?: Record<string, unknown>;
|
|
222
|
+
timeoutMs?: number;
|
|
223
|
+
}
|
|
224
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
225
|
+
type Tool<TInput, TOutput> = ToolDefinition<TInput, TOutput>;
|
|
226
|
+
interface ToolProvider {
|
|
227
|
+
setup(agent: BaseAgent<unknown, unknown>): Promise<Array<Tool<unknown, unknown>>>;
|
|
228
|
+
teardown(): Promise<void>;
|
|
229
|
+
}
|
|
230
|
+
interface ToolResultInit {
|
|
231
|
+
metadata?: Record<string, unknown>;
|
|
232
|
+
startedAt?: number;
|
|
233
|
+
finishedAt?: number;
|
|
234
|
+
}
|
|
235
|
+
declare const ToolResultFactory: {
|
|
236
|
+
success<TOutput>(toolName: string, output: TOutput, init?: ToolResultInit): ToolSuccess<TOutput>;
|
|
237
|
+
failure(toolName: string, error: Error | string, init?: ToolResultInit): ToolFailure;
|
|
238
|
+
isSuccess<TOutput>(result: ToolResult<TOutput>): result is ToolSuccess<TOutput>;
|
|
239
|
+
isFailure<TOutput>(result: ToolResult<TOutput>): result is ToolFailure;
|
|
240
|
+
};
|
|
241
|
+
declare const createToolCallRecord: (input: Partial<ToolCallRecord>) => ToolCallRecord;
|
|
242
|
+
declare const validateToolInput: <TSchema extends ZodTypeAny>(schema: TSchema, payload: unknown) => Result<z.infer<TSchema>, z.ZodError<z.infer<TSchema>>>;
|
|
243
|
+
declare const coerceToolDefinition: <TInput, TOutput>(definition: ToolDefinition<TInput, TOutput>) => Tool<TInput, TOutput>;
|
|
244
|
+
declare const isToolProvider: (value: unknown) => value is ToolProvider;
|
|
245
|
+
declare const normalizeToolEntries: (entries: Array<Tool<unknown, unknown> | ToolProvider>) => {
|
|
246
|
+
tools: Array<Tool<unknown, unknown>>;
|
|
247
|
+
providers: Array<ToolProvider>;
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
declare const UsageSchema: z.ZodObject<{
|
|
251
|
+
requests: z.ZodDefault<z.ZodNumber>;
|
|
252
|
+
inputTokens: z.ZodDefault<z.ZodNumber>;
|
|
253
|
+
outputTokens: z.ZodDefault<z.ZodNumber>;
|
|
254
|
+
totalTokens: z.ZodDefault<z.ZodNumber>;
|
|
255
|
+
cost: z.ZodDefault<z.ZodObject<{
|
|
256
|
+
generation: z.ZodDefault<z.ZodNumber>;
|
|
257
|
+
platform: z.ZodDefault<z.ZodNumber>;
|
|
258
|
+
total: z.ZodDefault<z.ZodNumber>;
|
|
259
|
+
}, "strip", z.ZodTypeAny, {
|
|
260
|
+
generation: number;
|
|
261
|
+
platform: number;
|
|
262
|
+
total: number;
|
|
263
|
+
}, {
|
|
264
|
+
generation?: number | undefined;
|
|
265
|
+
platform?: number | undefined;
|
|
266
|
+
total?: number | undefined;
|
|
267
|
+
}>>;
|
|
268
|
+
}, "strip", z.ZodTypeAny, {
|
|
269
|
+
requests: number;
|
|
270
|
+
inputTokens: number;
|
|
271
|
+
outputTokens: number;
|
|
272
|
+
totalTokens: number;
|
|
273
|
+
cost: {
|
|
274
|
+
generation: number;
|
|
275
|
+
platform: number;
|
|
276
|
+
total: number;
|
|
277
|
+
};
|
|
278
|
+
}, {
|
|
279
|
+
requests?: number | undefined;
|
|
280
|
+
inputTokens?: number | undefined;
|
|
281
|
+
outputTokens?: number | undefined;
|
|
282
|
+
totalTokens?: number | undefined;
|
|
283
|
+
cost?: {
|
|
284
|
+
generation?: number | undefined;
|
|
285
|
+
platform?: number | undefined;
|
|
286
|
+
total?: number | undefined;
|
|
287
|
+
} | undefined;
|
|
288
|
+
}>;
|
|
289
|
+
type Usage = z.infer<typeof UsageSchema>;
|
|
290
|
+
declare const ExecutionCycleSchema: z.ZodObject<{
|
|
291
|
+
iteration: z.ZodNumber;
|
|
292
|
+
thought: z.ZodOptional<z.ZodNullable<z.ZodUnknown>>;
|
|
293
|
+
toolCalls: z.ZodDefault<z.ZodArray<z.ZodObject<{
|
|
294
|
+
id: z.ZodDefault<z.ZodOptional<z.ZodString>>;
|
|
295
|
+
toolName: z.ZodString;
|
|
296
|
+
input: z.ZodUnknown;
|
|
297
|
+
output: z.ZodOptional<z.ZodUnknown>;
|
|
298
|
+
success: z.ZodOptional<z.ZodBoolean>;
|
|
299
|
+
error: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodObject<{
|
|
300
|
+
message: z.ZodString;
|
|
301
|
+
}, "passthrough", z.ZodTypeAny, z.objectOutputType<{
|
|
302
|
+
message: z.ZodString;
|
|
303
|
+
}, z.ZodTypeAny, "passthrough">, z.objectInputType<{
|
|
304
|
+
message: z.ZodString;
|
|
305
|
+
}, z.ZodTypeAny, "passthrough">>]>>;
|
|
306
|
+
startedAt: z.ZodDefault<z.ZodNumber>;
|
|
307
|
+
finishedAt: z.ZodOptional<z.ZodNumber>;
|
|
308
|
+
metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
309
|
+
}, "strip", z.ZodTypeAny, {
|
|
310
|
+
id: string;
|
|
311
|
+
toolName: string;
|
|
312
|
+
startedAt: number;
|
|
313
|
+
metadata: Record<string, unknown>;
|
|
314
|
+
input?: unknown;
|
|
315
|
+
output?: unknown;
|
|
316
|
+
success?: boolean | undefined;
|
|
317
|
+
error?: string | z.objectOutputType<{
|
|
318
|
+
message: z.ZodString;
|
|
319
|
+
}, z.ZodTypeAny, "passthrough"> | undefined;
|
|
320
|
+
finishedAt?: number | undefined;
|
|
321
|
+
}, {
|
|
322
|
+
toolName: string;
|
|
323
|
+
id?: string | undefined;
|
|
324
|
+
input?: unknown;
|
|
325
|
+
output?: unknown;
|
|
326
|
+
success?: boolean | undefined;
|
|
327
|
+
error?: string | z.objectInputType<{
|
|
328
|
+
message: z.ZodString;
|
|
329
|
+
}, z.ZodTypeAny, "passthrough"> | undefined;
|
|
330
|
+
startedAt?: number | undefined;
|
|
331
|
+
finishedAt?: number | undefined;
|
|
332
|
+
metadata?: Record<string, unknown> | undefined;
|
|
333
|
+
}>, "many">>;
|
|
334
|
+
results: z.ZodDefault<z.ZodArray<z.ZodUnknown, "many">>;
|
|
335
|
+
timestamp: z.ZodDefault<z.ZodNumber>;
|
|
336
|
+
}, "strip", z.ZodTypeAny, {
|
|
337
|
+
iteration: number;
|
|
338
|
+
toolCalls: {
|
|
339
|
+
id: string;
|
|
340
|
+
toolName: string;
|
|
341
|
+
startedAt: number;
|
|
342
|
+
metadata: Record<string, unknown>;
|
|
343
|
+
input?: unknown;
|
|
344
|
+
output?: unknown;
|
|
345
|
+
success?: boolean | undefined;
|
|
346
|
+
error?: string | z.objectOutputType<{
|
|
347
|
+
message: z.ZodString;
|
|
348
|
+
}, z.ZodTypeAny, "passthrough"> | undefined;
|
|
349
|
+
finishedAt?: number | undefined;
|
|
350
|
+
}[];
|
|
351
|
+
results: unknown[];
|
|
352
|
+
timestamp: number;
|
|
353
|
+
thought?: unknown;
|
|
354
|
+
}, {
|
|
355
|
+
iteration: number;
|
|
356
|
+
thought?: unknown;
|
|
357
|
+
toolCalls?: {
|
|
358
|
+
toolName: string;
|
|
359
|
+
id?: string | undefined;
|
|
360
|
+
input?: unknown;
|
|
361
|
+
output?: unknown;
|
|
362
|
+
success?: boolean | undefined;
|
|
363
|
+
error?: string | z.objectInputType<{
|
|
364
|
+
message: z.ZodString;
|
|
365
|
+
}, z.ZodTypeAny, "passthrough"> | undefined;
|
|
366
|
+
startedAt?: number | undefined;
|
|
367
|
+
finishedAt?: number | undefined;
|
|
368
|
+
metadata?: Record<string, unknown> | undefined;
|
|
369
|
+
}[] | undefined;
|
|
370
|
+
results?: unknown[] | undefined;
|
|
371
|
+
timestamp?: number | undefined;
|
|
372
|
+
}>;
|
|
373
|
+
type ExecutionCycle = z.infer<typeof ExecutionCycleSchema>;
|
|
374
|
+
interface AgentContextOptions {
|
|
375
|
+
agentName: string;
|
|
376
|
+
sessionId?: string;
|
|
377
|
+
parentSpanId?: string | null;
|
|
378
|
+
goal?: unknown;
|
|
379
|
+
metadata?: Record<string, unknown>;
|
|
380
|
+
}
|
|
381
|
+
interface AgentContextSnapshot {
|
|
382
|
+
agentName: string;
|
|
383
|
+
sessionId: string;
|
|
384
|
+
parentSpanId: string | null;
|
|
385
|
+
iteration: number;
|
|
386
|
+
goal?: unknown;
|
|
387
|
+
executionHistory: ExecutionCycle[];
|
|
388
|
+
usage: Usage;
|
|
389
|
+
toolCalls: ToolCallRecord[];
|
|
390
|
+
metadata: Record<string, unknown>;
|
|
391
|
+
startedAt: number;
|
|
392
|
+
updatedAt: number;
|
|
393
|
+
}
|
|
394
|
+
interface IterationSummary {
|
|
395
|
+
iteration: number;
|
|
396
|
+
thought: Record<string, unknown>;
|
|
397
|
+
toolCalls: Array<{
|
|
398
|
+
toolName: string;
|
|
399
|
+
success?: boolean;
|
|
400
|
+
}>;
|
|
401
|
+
results: unknown[];
|
|
402
|
+
}
|
|
403
|
+
declare class AgentContext {
|
|
404
|
+
readonly agentName: string;
|
|
405
|
+
readonly sessionId: string;
|
|
406
|
+
parentSpanId: string | null;
|
|
407
|
+
iteration: number;
|
|
408
|
+
goal?: unknown;
|
|
409
|
+
readonly executionHistory: ExecutionCycle[];
|
|
410
|
+
readonly toolCalls: ToolCallRecord[];
|
|
411
|
+
usage: Usage;
|
|
412
|
+
metadata: Record<string, unknown>;
|
|
413
|
+
readonly startedAt: number;
|
|
414
|
+
updatedAt: number;
|
|
415
|
+
constructor(options: AgentContextOptions);
|
|
416
|
+
updateUsage(delta: Usage): void;
|
|
417
|
+
addCycle(cycle: ExecutionCycle): ExecutionCycle;
|
|
418
|
+
recordToolCall(call: Omit<ToolCallRecord, "id">): ToolCallRecord;
|
|
419
|
+
getContextSize(): number;
|
|
420
|
+
getLastNCycles(count?: number): ExecutionCycle[];
|
|
421
|
+
getLastIterationsSummary(count?: number): IterationSummary[];
|
|
422
|
+
setMetadata(key: string, value: unknown): void;
|
|
423
|
+
clearHistory(): void;
|
|
424
|
+
snapshot(): AgentContextSnapshot;
|
|
425
|
+
private touch;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* Log levels
|
|
430
|
+
*/
|
|
431
|
+
declare enum LogLevel {
|
|
432
|
+
DEBUG = 0,
|
|
433
|
+
INFO = 1,
|
|
434
|
+
WARN = 2,
|
|
435
|
+
ERROR = 3,
|
|
436
|
+
SILENT = 4
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* Logger interface for agent output
|
|
440
|
+
*/
|
|
441
|
+
interface AgentLogger {
|
|
442
|
+
/**
|
|
443
|
+
* Log a debug message
|
|
444
|
+
*/
|
|
445
|
+
debug(message: string, meta?: Record<string, unknown>): void;
|
|
446
|
+
/**
|
|
447
|
+
* Log an info message
|
|
448
|
+
*/
|
|
449
|
+
info(message: string, meta?: Record<string, unknown>): void;
|
|
450
|
+
/**
|
|
451
|
+
* Log a warning message
|
|
452
|
+
*/
|
|
453
|
+
warn(message: string, meta?: Record<string, unknown>): void;
|
|
454
|
+
/**
|
|
455
|
+
* Log an error message
|
|
456
|
+
*/
|
|
457
|
+
error(message: string, error?: Error | unknown, meta?: Record<string, unknown>): void;
|
|
458
|
+
/**
|
|
459
|
+
* Get current log level
|
|
460
|
+
*/
|
|
461
|
+
getLevel(): LogLevel;
|
|
462
|
+
/**
|
|
463
|
+
* Set log level
|
|
464
|
+
*/
|
|
465
|
+
setLevel(level: LogLevel): void;
|
|
466
|
+
}
|
|
467
|
+
declare class ConsoleLogger implements AgentLogger {
|
|
468
|
+
private level;
|
|
469
|
+
constructor(level?: LogLevel);
|
|
470
|
+
debug(message: string, meta?: Record<string, unknown>): void;
|
|
471
|
+
info(message: string, meta?: Record<string, unknown>): void;
|
|
472
|
+
warn(message: string, meta?: Record<string, unknown>): void;
|
|
473
|
+
error(message: string, error?: Error | unknown, meta?: Record<string, unknown>): void;
|
|
474
|
+
getLevel(): LogLevel;
|
|
475
|
+
setLevel(level: LogLevel): void;
|
|
476
|
+
}
|
|
477
|
+
/**
|
|
478
|
+
* No-op logger that suppresses all output
|
|
479
|
+
*/
|
|
480
|
+
declare class SilentLogger implements AgentLogger {
|
|
481
|
+
debug(message: string, meta?: Record<string, unknown>): void;
|
|
482
|
+
info(message: string, meta?: Record<string, unknown>): void;
|
|
483
|
+
warn(message: string, meta?: Record<string, unknown>): void;
|
|
484
|
+
error(message: string, error?: Error | unknown, meta?: Record<string, unknown>): void;
|
|
485
|
+
getLevel(): LogLevel;
|
|
486
|
+
setLevel(level: LogLevel): void;
|
|
487
|
+
}
|
|
488
|
+
/**
|
|
489
|
+
* Get the default logger
|
|
490
|
+
*/
|
|
491
|
+
declare function getDefaultLogger(): AgentLogger;
|
|
492
|
+
/**
|
|
493
|
+
* Set the default logger
|
|
494
|
+
*/
|
|
495
|
+
declare function setDefaultLogger(logger: AgentLogger): void;
|
|
496
|
+
|
|
497
|
+
declare const HookEvents: {
|
|
498
|
+
readonly AgentStart: "agent:start";
|
|
499
|
+
readonly AgentEnd: "agent:end";
|
|
500
|
+
readonly LoopStart: "loop:start";
|
|
501
|
+
readonly LoopEnd: "loop:end";
|
|
502
|
+
readonly LlmCall: "llm:call";
|
|
503
|
+
readonly LlmResponse: "llm:response";
|
|
504
|
+
readonly ThinkEnd: "think:end";
|
|
505
|
+
readonly BeforeTool: "tool:before";
|
|
506
|
+
readonly AfterTool: "tool:after";
|
|
507
|
+
readonly ToolError: "tool:error";
|
|
508
|
+
readonly MemoryRead: "memory:read";
|
|
509
|
+
readonly MemoryWrite: "memory:write";
|
|
510
|
+
readonly MemoryError: "memory:error";
|
|
511
|
+
};
|
|
512
|
+
type HookEventName = (typeof HookEvents)[keyof typeof HookEvents];
|
|
513
|
+
interface HookPayloadMap {
|
|
514
|
+
[HookEvents.AgentStart]: {
|
|
515
|
+
context: AgentContext;
|
|
516
|
+
};
|
|
517
|
+
[HookEvents.AgentEnd]: {
|
|
518
|
+
context: AgentContext;
|
|
519
|
+
result?: unknown;
|
|
520
|
+
error?: unknown;
|
|
521
|
+
};
|
|
522
|
+
[HookEvents.LoopStart]: {
|
|
523
|
+
context: AgentContext;
|
|
524
|
+
};
|
|
525
|
+
[HookEvents.LoopEnd]: {
|
|
526
|
+
context: AgentContext;
|
|
527
|
+
};
|
|
528
|
+
[HookEvents.LlmCall]: {
|
|
529
|
+
context: AgentContext;
|
|
530
|
+
callType: string;
|
|
531
|
+
};
|
|
532
|
+
[HookEvents.LlmResponse]: {
|
|
533
|
+
context: AgentContext;
|
|
534
|
+
callType: string;
|
|
535
|
+
response: unknown;
|
|
536
|
+
};
|
|
537
|
+
[HookEvents.ThinkEnd]: {
|
|
538
|
+
context: AgentContext;
|
|
539
|
+
thought: unknown;
|
|
540
|
+
};
|
|
541
|
+
[HookEvents.BeforeTool]: {
|
|
542
|
+
context: AgentContext;
|
|
543
|
+
tool: Tool<unknown, unknown>;
|
|
544
|
+
input: unknown;
|
|
545
|
+
};
|
|
546
|
+
[HookEvents.AfterTool]: {
|
|
547
|
+
context: AgentContext;
|
|
548
|
+
tool: Tool<unknown, unknown>;
|
|
549
|
+
result: ToolResult<unknown>;
|
|
550
|
+
record: ToolCallRecord;
|
|
551
|
+
};
|
|
552
|
+
[HookEvents.ToolError]: {
|
|
553
|
+
context: AgentContext;
|
|
554
|
+
toolName: string;
|
|
555
|
+
tool?: Tool<unknown, unknown>;
|
|
556
|
+
error: unknown;
|
|
557
|
+
};
|
|
558
|
+
[HookEvents.MemoryRead]: {
|
|
559
|
+
context: AgentContext;
|
|
560
|
+
key: string;
|
|
561
|
+
value: unknown;
|
|
562
|
+
};
|
|
563
|
+
[HookEvents.MemoryWrite]: {
|
|
564
|
+
context: AgentContext;
|
|
565
|
+
key: string;
|
|
566
|
+
value: unknown;
|
|
567
|
+
};
|
|
568
|
+
[HookEvents.MemoryError]: {
|
|
569
|
+
context: AgentContext;
|
|
570
|
+
operation: "read" | "write" | "delete" | "clear";
|
|
571
|
+
error: unknown;
|
|
572
|
+
};
|
|
573
|
+
}
|
|
574
|
+
type HookPayload<E extends HookEventName> = HookPayloadMap[E];
|
|
575
|
+
type HookHandler<E extends HookEventName> = (payload: HookPayload<E>) => void | Promise<void>;
|
|
576
|
+
interface HookRegistration<E extends HookEventName> {
|
|
577
|
+
event: E;
|
|
578
|
+
handler: HookHandler<E>;
|
|
579
|
+
}
|
|
580
|
+
type UnregisterHook = () => void;
|
|
581
|
+
declare class HookManager {
|
|
582
|
+
private readonly registry;
|
|
583
|
+
private readonly logger;
|
|
584
|
+
constructor(logger?: AgentLogger);
|
|
585
|
+
on<E extends HookEventName>(event: E, handler: HookHandler<E>): UnregisterHook;
|
|
586
|
+
once<E extends HookEventName>(event: E, handler: HookHandler<E>): UnregisterHook;
|
|
587
|
+
off<E extends HookEventName>(event: E, handler: HookHandler<E>): void;
|
|
588
|
+
clear(): void;
|
|
589
|
+
listenerCount(event?: HookEventName): number;
|
|
590
|
+
emit<E extends HookEventName>(event: E, payload: HookPayload<E>): Promise<void>;
|
|
591
|
+
registerMany<E extends HookEventName>(registrations: HookRegistration<E>[]): UnregisterHook;
|
|
592
|
+
}
|
|
593
|
+
declare const createHookManager: (logger?: AgentLogger) => HookManager;
|
|
594
|
+
|
|
595
|
+
/**
|
|
596
|
+
* Schema for memory entry metadata
|
|
597
|
+
*/
|
|
598
|
+
declare const MemoryEntryMetadataSchema: z.ZodObject<{
|
|
599
|
+
createdAt: z.ZodNumber;
|
|
600
|
+
updatedAt: z.ZodNumber;
|
|
601
|
+
accessCount: z.ZodDefault<z.ZodNumber>;
|
|
602
|
+
}, "strip", z.ZodTypeAny, {
|
|
603
|
+
createdAt: number;
|
|
604
|
+
updatedAt: number;
|
|
605
|
+
accessCount: number;
|
|
606
|
+
}, {
|
|
607
|
+
createdAt: number;
|
|
608
|
+
updatedAt: number;
|
|
609
|
+
accessCount?: number | undefined;
|
|
610
|
+
}>;
|
|
611
|
+
type MemoryEntryMetadata = z.infer<typeof MemoryEntryMetadataSchema>;
|
|
612
|
+
/**
|
|
613
|
+
* Schema for a single memory entry
|
|
614
|
+
*/
|
|
615
|
+
declare const MemoryEntrySchema: z.ZodObject<{
|
|
616
|
+
key: z.ZodString;
|
|
617
|
+
description: z.ZodString;
|
|
618
|
+
value: z.ZodUnknown;
|
|
619
|
+
metadata: z.ZodObject<{
|
|
620
|
+
createdAt: z.ZodNumber;
|
|
621
|
+
updatedAt: z.ZodNumber;
|
|
622
|
+
accessCount: z.ZodDefault<z.ZodNumber>;
|
|
623
|
+
}, "strip", z.ZodTypeAny, {
|
|
624
|
+
createdAt: number;
|
|
625
|
+
updatedAt: number;
|
|
626
|
+
accessCount: number;
|
|
627
|
+
}, {
|
|
628
|
+
createdAt: number;
|
|
629
|
+
updatedAt: number;
|
|
630
|
+
accessCount?: number | undefined;
|
|
631
|
+
}>;
|
|
632
|
+
}, "strip", z.ZodTypeAny, {
|
|
633
|
+
metadata: {
|
|
634
|
+
createdAt: number;
|
|
635
|
+
updatedAt: number;
|
|
636
|
+
accessCount: number;
|
|
637
|
+
};
|
|
638
|
+
key: string;
|
|
639
|
+
description: string;
|
|
640
|
+
value?: unknown;
|
|
641
|
+
}, {
|
|
642
|
+
metadata: {
|
|
643
|
+
createdAt: number;
|
|
644
|
+
updatedAt: number;
|
|
645
|
+
accessCount?: number | undefined;
|
|
646
|
+
};
|
|
647
|
+
key: string;
|
|
648
|
+
description: string;
|
|
649
|
+
value?: unknown;
|
|
650
|
+
}>;
|
|
651
|
+
type MemoryEntry = z.infer<typeof MemoryEntrySchema>;
|
|
652
|
+
/**
|
|
653
|
+
* Catalog entry (summary without value) for LLM consumption
|
|
654
|
+
*/
|
|
655
|
+
interface MemoryCatalogEntry {
|
|
656
|
+
key: string;
|
|
657
|
+
description: string;
|
|
658
|
+
metadata: Record<string, unknown>;
|
|
659
|
+
}
|
|
660
|
+
/**
|
|
661
|
+
* Memory interface for persistent key-value storage across agent invocations.
|
|
662
|
+
* Implementations must be thread-safe and handle concurrent operations gracefully.
|
|
663
|
+
*/
|
|
664
|
+
interface Memory {
|
|
665
|
+
/**
|
|
666
|
+
* Check if memory has any entries
|
|
667
|
+
*
|
|
668
|
+
* @returns True if memory contains entries
|
|
669
|
+
*/
|
|
670
|
+
hasEntries(): Promise<boolean>;
|
|
671
|
+
/**
|
|
672
|
+
* List memory catalog (summaries without values) so LLM can decide what to load
|
|
673
|
+
*
|
|
674
|
+
* @returns Array of catalog entries with key, description, metadata (no values)
|
|
675
|
+
*/
|
|
676
|
+
listEntries(): Promise<MemoryCatalogEntry[]>;
|
|
677
|
+
/**
|
|
678
|
+
* Read values from memory by keys
|
|
679
|
+
*
|
|
680
|
+
* @param keys - Optional array of keys to read (if omitted, reads all)
|
|
681
|
+
* @returns Object mapping keys to their values
|
|
682
|
+
*/
|
|
683
|
+
read(keys?: string[]): Promise<Record<string, unknown>>;
|
|
684
|
+
/**
|
|
685
|
+
* Write a value to memory with the specified key and description
|
|
686
|
+
*
|
|
687
|
+
* @param key - Memory key
|
|
688
|
+
* @param value - Value to store
|
|
689
|
+
* @param description - Human-readable description of what this memory contains
|
|
690
|
+
* @param metadata - Optional metadata
|
|
691
|
+
* @returns The created or updated memory entry
|
|
692
|
+
*/
|
|
693
|
+
write(key: string, value: unknown, description?: string, metadata?: Record<string, unknown>): Promise<MemoryEntry>;
|
|
694
|
+
/**
|
|
695
|
+
* Delete a memory entry by key
|
|
696
|
+
*
|
|
697
|
+
* @param key - Memory key to delete
|
|
698
|
+
* @returns True if entry was deleted, false if not found
|
|
699
|
+
*/
|
|
700
|
+
delete(key: string): Promise<boolean>;
|
|
701
|
+
/**
|
|
702
|
+
* Clear all memory entries
|
|
703
|
+
*
|
|
704
|
+
* @returns Number of entries cleared
|
|
705
|
+
*/
|
|
706
|
+
clear(): Promise<number>;
|
|
707
|
+
}
|
|
708
|
+
/**
|
|
709
|
+
* Default in-memory implementation of Memory interface.
|
|
710
|
+
* Stores data in a Map that persists across agent invocations within the same process.
|
|
711
|
+
*/
|
|
712
|
+
declare class InMemoryStore implements Memory {
|
|
713
|
+
private readonly store;
|
|
714
|
+
/**
|
|
715
|
+
* Check if memory has any entries
|
|
716
|
+
*/
|
|
717
|
+
hasEntries(): Promise<boolean>;
|
|
718
|
+
/**
|
|
719
|
+
* List memory catalog (summaries without values) for LLM consumption
|
|
720
|
+
*/
|
|
721
|
+
listEntries(): Promise<MemoryCatalogEntry[]>;
|
|
722
|
+
/**
|
|
723
|
+
* Read values from memory by keys
|
|
724
|
+
*/
|
|
725
|
+
read(keys?: string[]): Promise<Record<string, unknown>>;
|
|
726
|
+
/**
|
|
727
|
+
* Write a value to memory with description
|
|
728
|
+
*/
|
|
729
|
+
write(key: string, value: unknown, description?: string, metadata?: Record<string, unknown>): Promise<MemoryEntry>;
|
|
730
|
+
/**
|
|
731
|
+
* Delete a memory entry by key
|
|
732
|
+
*/
|
|
733
|
+
delete(key: string): Promise<boolean>;
|
|
734
|
+
/**
|
|
735
|
+
* Clear all memory entries
|
|
736
|
+
*/
|
|
737
|
+
clear(): Promise<number>;
|
|
738
|
+
/**
|
|
739
|
+
* Get the current size of the memory store
|
|
740
|
+
*
|
|
741
|
+
* @returns Number of entries in memory
|
|
742
|
+
*/
|
|
743
|
+
get size(): number;
|
|
744
|
+
}
|
|
745
|
+
/**
|
|
746
|
+
* Factory function to create a new in-memory store
|
|
747
|
+
*
|
|
748
|
+
* @returns New InMemoryStore instance
|
|
749
|
+
*/
|
|
750
|
+
declare const createInMemoryStore: () => InMemoryStore;
|
|
751
|
+
|
|
752
|
+
declare const DEFAULT_MODEL = "gcp/gemini-flash-latest";
|
|
753
|
+
/**
|
|
754
|
+
* Configuration options for BaseAgent
|
|
755
|
+
*/
|
|
756
|
+
interface BaseAgentConfig<TInput, TOutput> {
|
|
757
|
+
/**
|
|
758
|
+
* Unique name identifying this agent
|
|
759
|
+
*/
|
|
760
|
+
name: string;
|
|
761
|
+
/**
|
|
762
|
+
* Human-readable description of the agent's purpose
|
|
763
|
+
*/
|
|
764
|
+
description?: string;
|
|
765
|
+
/**
|
|
766
|
+
* System instructions guiding agent behavior
|
|
767
|
+
*/
|
|
768
|
+
instructions?: string;
|
|
769
|
+
/**
|
|
770
|
+
* List of tools available to the agent
|
|
771
|
+
*
|
|
772
|
+
* We intentionally accept Tool<any, any> here because tool inputs are ultimately validated
|
|
773
|
+
* at runtime before execution. Using `any` keeps the API ergonomic for tool authors while
|
|
774
|
+
* allowing the BaseAgent to store mixed tool definitions in a single registry.
|
|
775
|
+
*/
|
|
776
|
+
tools?: Array<Tool<any, any> | ToolProvider>;
|
|
777
|
+
/**
|
|
778
|
+
* Maximum iterations before terminating the agent loop
|
|
779
|
+
* @default 25
|
|
780
|
+
*/
|
|
781
|
+
maxIterations?: number;
|
|
782
|
+
/**
|
|
783
|
+
* Model identifier(s).
|
|
784
|
+
* Accepts a single model (e.g., "gpt-4", "claude-3-sonnet") or a list used as fallback.
|
|
785
|
+
* @default "gcp/gemini-flash-latest"
|
|
786
|
+
*/
|
|
787
|
+
model?: string | readonly string[];
|
|
788
|
+
/**
|
|
789
|
+
* Input schema for validation (Zod schema)
|
|
790
|
+
*/
|
|
791
|
+
inputSchema?: ZodType<TInput>;
|
|
792
|
+
/**
|
|
793
|
+
* Output schema for validation (Zod schema)
|
|
794
|
+
*/
|
|
795
|
+
outputSchema?: ZodType<TOutput>;
|
|
796
|
+
/**
|
|
797
|
+
* Enable memory subsystem
|
|
798
|
+
* @default false
|
|
799
|
+
*/
|
|
800
|
+
enableMemory?: boolean;
|
|
801
|
+
/**
|
|
802
|
+
* Custom memory implementation (defaults to InMemoryStore if enableMemory is true)
|
|
803
|
+
*/
|
|
804
|
+
memory?: Memory;
|
|
805
|
+
/**
|
|
806
|
+
* Additional metadata for the agent
|
|
807
|
+
*/
|
|
808
|
+
metadata?: Record<string, unknown>;
|
|
809
|
+
/**
|
|
810
|
+
* Opper API configuration
|
|
811
|
+
*/
|
|
812
|
+
opperConfig?: OpperClientConfig;
|
|
813
|
+
}
|
|
814
|
+
/**
|
|
815
|
+
* Opper client configuration
|
|
816
|
+
*/
|
|
817
|
+
interface OpperClientConfig {
|
|
818
|
+
/**
|
|
819
|
+
* Opper API key (defaults to process.env.OPPER_API_KEY)
|
|
820
|
+
*/
|
|
821
|
+
apiKey: string | undefined;
|
|
822
|
+
/**
|
|
823
|
+
* Opper API base URL
|
|
824
|
+
*/
|
|
825
|
+
baseUrl: string | undefined;
|
|
826
|
+
/**
|
|
827
|
+
* Additional configuration options
|
|
828
|
+
*/
|
|
829
|
+
[key: string]: unknown;
|
|
830
|
+
}
|
|
831
|
+
/**
|
|
832
|
+
* Abstract base class for all Opper agents.
|
|
833
|
+
* Provides lifecycle management, tool handling, hook system, and Opper integration.
|
|
834
|
+
*
|
|
835
|
+
* @template TInput - Type of input the agent accepts
|
|
836
|
+
* @template TOutput - Type of output the agent produces
|
|
837
|
+
*/
|
|
838
|
+
declare abstract class BaseAgent<TInput = unknown, TOutput = unknown> {
|
|
839
|
+
/**
|
|
840
|
+
* Agent name
|
|
841
|
+
*/
|
|
842
|
+
readonly name: string;
|
|
843
|
+
/**
|
|
844
|
+
* Agent description
|
|
845
|
+
*/
|
|
846
|
+
readonly description: string | undefined;
|
|
847
|
+
/**
|
|
848
|
+
* Agent instructions
|
|
849
|
+
*/
|
|
850
|
+
readonly instructions: string | undefined;
|
|
851
|
+
/**
|
|
852
|
+
* Maximum iterations for the agent loop
|
|
853
|
+
*/
|
|
854
|
+
readonly maxIterations: number;
|
|
855
|
+
/**
|
|
856
|
+
* Model identifier
|
|
857
|
+
*/
|
|
858
|
+
readonly model: string | readonly string[];
|
|
859
|
+
/**
|
|
860
|
+
* Input validation schema
|
|
861
|
+
*/
|
|
862
|
+
readonly inputSchema: ZodType<TInput> | undefined;
|
|
863
|
+
/**
|
|
864
|
+
* Output validation schema
|
|
865
|
+
*/
|
|
866
|
+
readonly outputSchema: ZodType<TOutput> | undefined;
|
|
867
|
+
/**
|
|
868
|
+
* Whether memory is enabled
|
|
869
|
+
*/
|
|
870
|
+
readonly enableMemory: boolean;
|
|
871
|
+
/**
|
|
872
|
+
* Memory instance for persistent storage (null if disabled or initialization failed)
|
|
873
|
+
*/
|
|
874
|
+
readonly memory: Memory | null;
|
|
875
|
+
/**
|
|
876
|
+
* Agent metadata
|
|
877
|
+
*/
|
|
878
|
+
readonly metadata: Record<string, unknown>;
|
|
879
|
+
/**
|
|
880
|
+
* Hook manager for lifecycle events
|
|
881
|
+
*/
|
|
882
|
+
protected readonly hooks: HookManager;
|
|
883
|
+
/**
|
|
884
|
+
* Registry of available tools
|
|
885
|
+
*/
|
|
886
|
+
protected readonly tools: Map<string, Tool<unknown, unknown>>;
|
|
887
|
+
/**
|
|
888
|
+
* Baseline tools registered directly on the agent
|
|
889
|
+
*/
|
|
890
|
+
protected readonly baseTools: Map<string, Tool<unknown, unknown>>;
|
|
891
|
+
/**
|
|
892
|
+
* Registered tool providers that can expand into tools at runtime
|
|
893
|
+
*/
|
|
894
|
+
protected readonly toolProviders: Set<ToolProvider>;
|
|
895
|
+
/**
|
|
896
|
+
* Active tools supplied by providers for the current execution
|
|
897
|
+
*/
|
|
898
|
+
private readonly providerToolRegistry;
|
|
899
|
+
/**
|
|
900
|
+
* Opper client configuration
|
|
901
|
+
*/
|
|
902
|
+
protected readonly opperConfig: OpperClientConfig;
|
|
903
|
+
constructor(config: BaseAgentConfig<TInput, TOutput>);
|
|
904
|
+
/**
|
|
905
|
+
* Initialize memory subsystem with graceful degradation
|
|
906
|
+
*
|
|
907
|
+
* @param config - Agent configuration
|
|
908
|
+
* @returns Memory instance or null
|
|
909
|
+
*/
|
|
910
|
+
private initializeMemory;
|
|
911
|
+
/**
|
|
912
|
+
* Main entry point for agent execution.
|
|
913
|
+
* Orchestrates lifecycle: context initialization, hook triggering, loop execution, teardown.
|
|
914
|
+
*
|
|
915
|
+
* @param input - Input to process
|
|
916
|
+
* @param parentSpanId - Optional parent span ID for tracing
|
|
917
|
+
* @returns Processed output
|
|
918
|
+
*/
|
|
919
|
+
process(input: TInput, parentSpanId?: string): Promise<TOutput>;
|
|
920
|
+
/**
|
|
921
|
+
* Abstract method implementing the agent's main loop logic.
|
|
922
|
+
* Must be implemented by concrete agent classes.
|
|
923
|
+
*
|
|
924
|
+
* @param input - Validated input
|
|
925
|
+
* @param context - Agent execution context
|
|
926
|
+
* @returns Processed output
|
|
927
|
+
*/
|
|
928
|
+
protected abstract runLoop(input: TInput, context: AgentContext): Promise<TOutput>;
|
|
929
|
+
/**
|
|
930
|
+
* Initialize agent context before execution.
|
|
931
|
+
* Can be overridden for custom initialization logic.
|
|
932
|
+
*
|
|
933
|
+
* @param input - Agent input
|
|
934
|
+
* @param parentSpanId - Optional parent span ID
|
|
935
|
+
* @returns Initialized context
|
|
936
|
+
*/
|
|
937
|
+
protected initializeContext(input: TInput, parentSpanId?: string): Promise<AgentContext>;
|
|
938
|
+
/**
|
|
939
|
+
* Teardown agent context after execution.
|
|
940
|
+
* Can be overridden for custom cleanup logic.
|
|
941
|
+
*
|
|
942
|
+
* @param context - Agent context to teardown
|
|
943
|
+
*/
|
|
944
|
+
protected teardownContext(context: AgentContext): Promise<void>;
|
|
945
|
+
/**
|
|
946
|
+
* Add a tool to the agent's tool registry.
|
|
947
|
+
*
|
|
948
|
+
* @param tool - Tool to add
|
|
949
|
+
*/
|
|
950
|
+
addTool<TInput = unknown, TOutput = unknown>(tool: Tool<TInput, TOutput>): void;
|
|
951
|
+
/**
|
|
952
|
+
* Remove a tool from the agent's tool registry.
|
|
953
|
+
*
|
|
954
|
+
* @param toolName - Name of tool to remove
|
|
955
|
+
* @returns True if tool was removed, false if not found
|
|
956
|
+
*/
|
|
957
|
+
removeTool(toolName: string): boolean;
|
|
958
|
+
/**
|
|
959
|
+
* Get a tool by name.
|
|
960
|
+
*
|
|
961
|
+
* @param toolName - Name of tool to retrieve
|
|
962
|
+
* @returns Tool instance or undefined
|
|
963
|
+
*/
|
|
964
|
+
getTool(toolName: string): Tool<unknown, unknown> | undefined;
|
|
965
|
+
/**
|
|
966
|
+
* Get all registered tools.
|
|
967
|
+
*
|
|
968
|
+
* @returns Array of all tools
|
|
969
|
+
*/
|
|
970
|
+
getTools(): Array<Tool<unknown, unknown>>;
|
|
971
|
+
/**
|
|
972
|
+
* Register a tool provider that expands into tools at runtime.
|
|
973
|
+
*
|
|
974
|
+
* @param provider - Tool provider to add
|
|
975
|
+
*/
|
|
976
|
+
addToolProvider(provider: ToolProvider): void;
|
|
977
|
+
/**
|
|
978
|
+
* Convert this agent into a tool that can be used by other agents.
|
|
979
|
+
*
|
|
980
|
+
* @param toolName - Optional custom name for the tool (defaults to agent name)
|
|
981
|
+
* @param toolDescription - Optional custom description (defaults to agent description)
|
|
982
|
+
* @returns Tool wrapping this agent
|
|
983
|
+
*/
|
|
984
|
+
asTool(toolName?: string, toolDescription?: string): Tool<TInput, TOutput>;
|
|
985
|
+
/**
|
|
986
|
+
* Register a hook handler for a specific event.
|
|
987
|
+
*
|
|
988
|
+
* @param event - Hook event name
|
|
989
|
+
* @param handler - Hook handler function
|
|
990
|
+
* @returns Cleanup function to unregister the hook
|
|
991
|
+
*/
|
|
992
|
+
registerHook<E extends HookEventName>(event: E, handler: HookHandler<E>): () => void;
|
|
993
|
+
/**
|
|
994
|
+
* Trigger a hook event with a payload.
|
|
995
|
+
* Swallows errors to prevent hook failures from breaking agent execution.
|
|
996
|
+
*
|
|
997
|
+
* @param event - Hook event name
|
|
998
|
+
* @param payload - Event payload
|
|
999
|
+
*/
|
|
1000
|
+
protected triggerHook<E extends HookEventName>(event: E, payload: Parameters<HookHandler<E>>[0]): Promise<void>;
|
|
1001
|
+
/**
|
|
1002
|
+
* Execute a tool with proper context, hooks, and error handling.
|
|
1003
|
+
*
|
|
1004
|
+
* @param toolName - Name of tool to execute
|
|
1005
|
+
* @param input - Tool input
|
|
1006
|
+
* @param context - Agent context
|
|
1007
|
+
* @param options - Optional execution options (signal, span ID)
|
|
1008
|
+
* @returns Tool execution result
|
|
1009
|
+
*/
|
|
1010
|
+
protected executeTool(toolName: string, input: unknown, context: AgentContext, options?: {
|
|
1011
|
+
signal?: AbortSignal;
|
|
1012
|
+
spanId?: string;
|
|
1013
|
+
}): Promise<ToolResult<unknown>>;
|
|
1014
|
+
private registerTools;
|
|
1015
|
+
private activateToolProviders;
|
|
1016
|
+
private deactivateToolProviders;
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
/**
|
|
1020
|
+
* Opper call response
|
|
1021
|
+
*/
|
|
1022
|
+
interface OpperCallResponse<TOutput = unknown> {
|
|
1023
|
+
/**
|
|
1024
|
+
* Parsed JSON output (if outputSchema provided)
|
|
1025
|
+
*/
|
|
1026
|
+
jsonPayload?: TOutput;
|
|
1027
|
+
/**
|
|
1028
|
+
* Text message response
|
|
1029
|
+
*/
|
|
1030
|
+
message?: string | null | undefined;
|
|
1031
|
+
/**
|
|
1032
|
+
* Span ID for this call
|
|
1033
|
+
*/
|
|
1034
|
+
spanId: string;
|
|
1035
|
+
/**
|
|
1036
|
+
* Token usage information
|
|
1037
|
+
*/
|
|
1038
|
+
usage: {
|
|
1039
|
+
inputTokens: number;
|
|
1040
|
+
outputTokens: number;
|
|
1041
|
+
totalTokens: number;
|
|
1042
|
+
cost: {
|
|
1043
|
+
generation: number;
|
|
1044
|
+
platform: number;
|
|
1045
|
+
total: number;
|
|
1046
|
+
};
|
|
1047
|
+
};
|
|
1048
|
+
}
|
|
1049
|
+
/**
|
|
1050
|
+
* Options for Opper call
|
|
1051
|
+
*/
|
|
1052
|
+
interface OpperCallOptions<TInput = unknown, TOutput = unknown> {
|
|
1053
|
+
/**
|
|
1054
|
+
* Unique name for this call/task
|
|
1055
|
+
*/
|
|
1056
|
+
name: string;
|
|
1057
|
+
/**
|
|
1058
|
+
* Natural language instructions
|
|
1059
|
+
*/
|
|
1060
|
+
instructions: string;
|
|
1061
|
+
/**
|
|
1062
|
+
* Input data for the call
|
|
1063
|
+
*/
|
|
1064
|
+
input: TInput;
|
|
1065
|
+
/**
|
|
1066
|
+
* Input schema (Zod or JSON Schema)
|
|
1067
|
+
*/
|
|
1068
|
+
inputSchema?: z.ZodType<TInput> | Record<string, unknown>;
|
|
1069
|
+
/**
|
|
1070
|
+
* Output schema (Zod or JSON Schema)
|
|
1071
|
+
*/
|
|
1072
|
+
outputSchema?: z.ZodType<TOutput> | Record<string, unknown>;
|
|
1073
|
+
/**
|
|
1074
|
+
* Model to use. Accepts a single model identifier or an ordered list for fallback.
|
|
1075
|
+
* Example: "anthropic/claude-3.7-sonnet" or ["openai/gpt-4o", "anthropic/claude-3.7-sonnet"].
|
|
1076
|
+
*/
|
|
1077
|
+
model?: string | readonly string[];
|
|
1078
|
+
/**
|
|
1079
|
+
* Parent span ID for tracing
|
|
1080
|
+
*/
|
|
1081
|
+
parentSpanId?: string;
|
|
1082
|
+
}
|
|
1083
|
+
/**
|
|
1084
|
+
* Span information
|
|
1085
|
+
*/
|
|
1086
|
+
interface OpperSpan {
|
|
1087
|
+
id: string;
|
|
1088
|
+
name: string;
|
|
1089
|
+
input?: unknown;
|
|
1090
|
+
output?: unknown;
|
|
1091
|
+
}
|
|
1092
|
+
/**
|
|
1093
|
+
* Options for creating a span
|
|
1094
|
+
*/
|
|
1095
|
+
interface CreateSpanOptions {
|
|
1096
|
+
name: string;
|
|
1097
|
+
input?: unknown;
|
|
1098
|
+
parentSpanId?: string;
|
|
1099
|
+
}
|
|
1100
|
+
/**
|
|
1101
|
+
* Retry configuration
|
|
1102
|
+
*/
|
|
1103
|
+
interface RetryConfig {
|
|
1104
|
+
/**
|
|
1105
|
+
* Maximum number of retry attempts
|
|
1106
|
+
*/
|
|
1107
|
+
maxRetries: number;
|
|
1108
|
+
/**
|
|
1109
|
+
* Initial delay in milliseconds
|
|
1110
|
+
*/
|
|
1111
|
+
initialDelayMs: number;
|
|
1112
|
+
/**
|
|
1113
|
+
* Backoff multiplier
|
|
1114
|
+
*/
|
|
1115
|
+
backoffMultiplier: number;
|
|
1116
|
+
/**
|
|
1117
|
+
* Maximum delay in milliseconds
|
|
1118
|
+
*/
|
|
1119
|
+
maxDelayMs: number;
|
|
1120
|
+
}
|
|
1121
|
+
/**
|
|
1122
|
+
* Default retry configuration
|
|
1123
|
+
*/
|
|
1124
|
+
declare const DEFAULT_RETRY_CONFIG: RetryConfig;
|
|
1125
|
+
/**
|
|
1126
|
+
* Opper client wrapper with retry logic and usage tracking
|
|
1127
|
+
*/
|
|
1128
|
+
declare class OpperClient {
|
|
1129
|
+
private readonly client;
|
|
1130
|
+
private readonly logger;
|
|
1131
|
+
private readonly retryConfig;
|
|
1132
|
+
constructor(apiKey?: string, options?: {
|
|
1133
|
+
logger?: AgentLogger;
|
|
1134
|
+
retryConfig?: Partial<RetryConfig>;
|
|
1135
|
+
});
|
|
1136
|
+
/**
|
|
1137
|
+
* Make a call to Opper with retry logic
|
|
1138
|
+
*/
|
|
1139
|
+
call<TInput = unknown, TOutput = unknown>(options: OpperCallOptions<TInput, TOutput>): Promise<OpperCallResponse<TOutput>>;
|
|
1140
|
+
/**
|
|
1141
|
+
* Create a span for tracing
|
|
1142
|
+
*/
|
|
1143
|
+
createSpan(options: CreateSpanOptions): Promise<OpperSpan>;
|
|
1144
|
+
/**
|
|
1145
|
+
* Update a span with output or error
|
|
1146
|
+
*/
|
|
1147
|
+
updateSpan(spanId: string, output: unknown, options?: {
|
|
1148
|
+
error?: string;
|
|
1149
|
+
}): Promise<void>;
|
|
1150
|
+
/**
|
|
1151
|
+
* Get the underlying Opper client
|
|
1152
|
+
*/
|
|
1153
|
+
getClient(): Opper;
|
|
1154
|
+
/**
|
|
1155
|
+
* Execute a function with retry logic and exponential backoff
|
|
1156
|
+
*/
|
|
1157
|
+
private withRetry;
|
|
1158
|
+
/**
|
|
1159
|
+
* Check if an error is retryable
|
|
1160
|
+
*/
|
|
1161
|
+
private isRetryableError;
|
|
1162
|
+
/**
|
|
1163
|
+
* Convert Zod schema to JSON Schema
|
|
1164
|
+
*/
|
|
1165
|
+
private toJsonSchema;
|
|
1166
|
+
/**
|
|
1167
|
+
* Sleep for specified milliseconds
|
|
1168
|
+
*/
|
|
1169
|
+
private sleep;
|
|
1170
|
+
}
|
|
1171
|
+
/**
|
|
1172
|
+
* Create an Opper client with optional configuration
|
|
1173
|
+
*/
|
|
1174
|
+
declare function createOpperClient(apiKey?: string, options?: {
|
|
1175
|
+
logger?: AgentLogger;
|
|
1176
|
+
retryConfig?: Partial<RetryConfig>;
|
|
1177
|
+
}): OpperClient;
|
|
1178
|
+
|
|
1179
|
+
/**
|
|
1180
|
+
* Configuration for the core Agent
|
|
1181
|
+
*/
|
|
1182
|
+
interface AgentConfig<TInput, TOutput> extends BaseAgentConfig<TInput, TOutput> {
|
|
1183
|
+
/**
|
|
1184
|
+
* Custom Opper client instance (for testing or custom configuration)
|
|
1185
|
+
*/
|
|
1186
|
+
opperClient?: OpperClient;
|
|
1187
|
+
/**
|
|
1188
|
+
* Logger instance
|
|
1189
|
+
*/
|
|
1190
|
+
logger?: AgentLogger;
|
|
1191
|
+
/**
|
|
1192
|
+
* Verbose logging
|
|
1193
|
+
*/
|
|
1194
|
+
verbose?: boolean;
|
|
1195
|
+
}
|
|
1196
|
+
/**
|
|
1197
|
+
* Core Agent implementation with "while tools > 0" loop.
|
|
1198
|
+
* Implements think → tool execution → memory handling cycle.
|
|
1199
|
+
*/
|
|
1200
|
+
declare class Agent<TInput = unknown, TOutput = unknown> extends BaseAgent<TInput, TOutput> {
|
|
1201
|
+
private readonly opperClient;
|
|
1202
|
+
private readonly logger;
|
|
1203
|
+
private readonly verbose;
|
|
1204
|
+
constructor(config: AgentConfig<TInput, TOutput>);
|
|
1205
|
+
/**
|
|
1206
|
+
* Serialize input for passing to LLM or spans
|
|
1207
|
+
*/
|
|
1208
|
+
private serializeInput;
|
|
1209
|
+
/**
|
|
1210
|
+
* Main agent loop: think → tool execution → memory handling → repeat until complete
|
|
1211
|
+
*/
|
|
1212
|
+
protected runLoop(input: TInput, context: AgentContext): Promise<TOutput>;
|
|
1213
|
+
/**
|
|
1214
|
+
* Think step: Call LLM to decide next action
|
|
1215
|
+
*/
|
|
1216
|
+
private think;
|
|
1217
|
+
/**
|
|
1218
|
+
* Build static instructions for the think step
|
|
1219
|
+
*/
|
|
1220
|
+
private buildThinkInstructions;
|
|
1221
|
+
/**
|
|
1222
|
+
* Build dynamic context for the think step
|
|
1223
|
+
*/
|
|
1224
|
+
private buildThinkContext;
|
|
1225
|
+
/**
|
|
1226
|
+
* Execute all tool calls from a decision
|
|
1227
|
+
*/
|
|
1228
|
+
private executeToolCalls;
|
|
1229
|
+
/**
|
|
1230
|
+
* Handle memory operations from a decision
|
|
1231
|
+
* Supports read and write operations with graceful degradation
|
|
1232
|
+
*/
|
|
1233
|
+
private handleMemoryActions;
|
|
1234
|
+
/**
|
|
1235
|
+
* Generate final result based on execution history
|
|
1236
|
+
*/
|
|
1237
|
+
private generateFinalResult;
|
|
1238
|
+
/**
|
|
1239
|
+
* Log helper
|
|
1240
|
+
*/
|
|
1241
|
+
private log;
|
|
1242
|
+
}
|
|
1243
|
+
|
|
1244
|
+
/**
|
|
1245
|
+
* Schema for agent's internal thought/reasoning
|
|
1246
|
+
*/
|
|
1247
|
+
declare const ThoughtSchema: z.ZodObject<{
|
|
1248
|
+
/**
|
|
1249
|
+
* The reasoning or internal monologue of the agent
|
|
1250
|
+
*/
|
|
1251
|
+
reasoning: z.ZodString;
|
|
1252
|
+
/**
|
|
1253
|
+
* Planned next action(s)
|
|
1254
|
+
*/
|
|
1255
|
+
plan: z.ZodOptional<z.ZodString>;
|
|
1256
|
+
/**
|
|
1257
|
+
* Confidence level (0-1)
|
|
1258
|
+
*/
|
|
1259
|
+
confidence: z.ZodOptional<z.ZodNumber>;
|
|
1260
|
+
/**
|
|
1261
|
+
* Additional metadata
|
|
1262
|
+
*/
|
|
1263
|
+
metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
1264
|
+
}, "strip", z.ZodTypeAny, {
|
|
1265
|
+
reasoning: string;
|
|
1266
|
+
metadata?: Record<string, unknown> | undefined;
|
|
1267
|
+
plan?: string | undefined;
|
|
1268
|
+
confidence?: number | undefined;
|
|
1269
|
+
}, {
|
|
1270
|
+
reasoning: string;
|
|
1271
|
+
metadata?: Record<string, unknown> | undefined;
|
|
1272
|
+
plan?: string | undefined;
|
|
1273
|
+
confidence?: number | undefined;
|
|
1274
|
+
}>;
|
|
1275
|
+
type Thought = z.infer<typeof ThoughtSchema>;
|
|
1276
|
+
/**
|
|
1277
|
+
* Schema for a tool call from the LLM
|
|
1278
|
+
*/
|
|
1279
|
+
declare const ToolCallSchema: z.ZodObject<{
|
|
1280
|
+
/**
|
|
1281
|
+
* Unique identifier for this tool call
|
|
1282
|
+
*/
|
|
1283
|
+
id: z.ZodString;
|
|
1284
|
+
/**
|
|
1285
|
+
* Name of the tool to invoke
|
|
1286
|
+
*/
|
|
1287
|
+
toolName: z.ZodString;
|
|
1288
|
+
/**
|
|
1289
|
+
* Arguments to pass to the tool
|
|
1290
|
+
*/
|
|
1291
|
+
arguments: z.ZodUnknown;
|
|
1292
|
+
}, "strip", z.ZodTypeAny, {
|
|
1293
|
+
id: string;
|
|
1294
|
+
toolName: string;
|
|
1295
|
+
arguments?: unknown;
|
|
1296
|
+
}, {
|
|
1297
|
+
id: string;
|
|
1298
|
+
toolName: string;
|
|
1299
|
+
arguments?: unknown;
|
|
1300
|
+
}>;
|
|
1301
|
+
type ToolCall = z.infer<typeof ToolCallSchema>;
|
|
1302
|
+
/**
|
|
1303
|
+
* Schema for memory updates (write operations)
|
|
1304
|
+
*/
|
|
1305
|
+
declare const MemoryUpdateSchema: z.ZodObject<{
|
|
1306
|
+
/**
|
|
1307
|
+
* Value to store for this key
|
|
1308
|
+
*/
|
|
1309
|
+
value: z.ZodUnknown;
|
|
1310
|
+
/**
|
|
1311
|
+
* Optional description of the memory entry
|
|
1312
|
+
*/
|
|
1313
|
+
description: z.ZodOptional<z.ZodString>;
|
|
1314
|
+
/**
|
|
1315
|
+
* Optional metadata for the memory entry
|
|
1316
|
+
*/
|
|
1317
|
+
metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
1318
|
+
}, "strip", z.ZodTypeAny, {
|
|
1319
|
+
value?: unknown;
|
|
1320
|
+
metadata?: Record<string, unknown> | undefined;
|
|
1321
|
+
description?: string | undefined;
|
|
1322
|
+
}, {
|
|
1323
|
+
value?: unknown;
|
|
1324
|
+
metadata?: Record<string, unknown> | undefined;
|
|
1325
|
+
description?: string | undefined;
|
|
1326
|
+
}>;
|
|
1327
|
+
type MemoryUpdate = z.infer<typeof MemoryUpdateSchema>;
|
|
1328
|
+
/**
|
|
1329
|
+
* Schema for agent's decision output from the "think" step
|
|
1330
|
+
*/
|
|
1331
|
+
declare const AgentDecisionSchema: z.ZodObject<{
|
|
1332
|
+
/**
|
|
1333
|
+
* Agent's internal reasoning
|
|
1334
|
+
*/
|
|
1335
|
+
reasoning: z.ZodString;
|
|
1336
|
+
/**
|
|
1337
|
+
* Tool calls to execute (if any)
|
|
1338
|
+
* Empty array signals task completion
|
|
1339
|
+
*/
|
|
1340
|
+
toolCalls: z.ZodDefault<z.ZodArray<z.ZodObject<{
|
|
1341
|
+
/**
|
|
1342
|
+
* Unique identifier for this tool call
|
|
1343
|
+
*/
|
|
1344
|
+
id: z.ZodString;
|
|
1345
|
+
/**
|
|
1346
|
+
* Name of the tool to invoke
|
|
1347
|
+
*/
|
|
1348
|
+
toolName: z.ZodString;
|
|
1349
|
+
/**
|
|
1350
|
+
* Arguments to pass to the tool
|
|
1351
|
+
*/
|
|
1352
|
+
arguments: z.ZodUnknown;
|
|
1353
|
+
}, "strip", z.ZodTypeAny, {
|
|
1354
|
+
id: string;
|
|
1355
|
+
toolName: string;
|
|
1356
|
+
arguments?: unknown;
|
|
1357
|
+
}, {
|
|
1358
|
+
id: string;
|
|
1359
|
+
toolName: string;
|
|
1360
|
+
arguments?: unknown;
|
|
1361
|
+
}>, "many">>;
|
|
1362
|
+
/**
|
|
1363
|
+
* Memory keys to read during this iteration
|
|
1364
|
+
*/
|
|
1365
|
+
memoryReads: z.ZodDefault<z.ZodArray<z.ZodString, "many">>;
|
|
1366
|
+
/**
|
|
1367
|
+
* Memory entries to write/update (key -> payload)
|
|
1368
|
+
*/
|
|
1369
|
+
memoryUpdates: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodObject<{
|
|
1370
|
+
/**
|
|
1371
|
+
* Value to store for this key
|
|
1372
|
+
*/
|
|
1373
|
+
value: z.ZodUnknown;
|
|
1374
|
+
/**
|
|
1375
|
+
* Optional description of the memory entry
|
|
1376
|
+
*/
|
|
1377
|
+
description: z.ZodOptional<z.ZodString>;
|
|
1378
|
+
/**
|
|
1379
|
+
* Optional metadata for the memory entry
|
|
1380
|
+
*/
|
|
1381
|
+
metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
1382
|
+
}, "strip", z.ZodTypeAny, {
|
|
1383
|
+
value?: unknown;
|
|
1384
|
+
metadata?: Record<string, unknown> | undefined;
|
|
1385
|
+
description?: string | undefined;
|
|
1386
|
+
}, {
|
|
1387
|
+
value?: unknown;
|
|
1388
|
+
metadata?: Record<string, unknown> | undefined;
|
|
1389
|
+
description?: string | undefined;
|
|
1390
|
+
}>>>;
|
|
1391
|
+
}, "strip", z.ZodTypeAny, {
|
|
1392
|
+
toolCalls: {
|
|
1393
|
+
id: string;
|
|
1394
|
+
toolName: string;
|
|
1395
|
+
arguments?: unknown;
|
|
1396
|
+
}[];
|
|
1397
|
+
reasoning: string;
|
|
1398
|
+
memoryReads: string[];
|
|
1399
|
+
memoryUpdates: Record<string, {
|
|
1400
|
+
value?: unknown;
|
|
1401
|
+
metadata?: Record<string, unknown> | undefined;
|
|
1402
|
+
description?: string | undefined;
|
|
1403
|
+
}>;
|
|
1404
|
+
}, {
|
|
1405
|
+
reasoning: string;
|
|
1406
|
+
toolCalls?: {
|
|
1407
|
+
id: string;
|
|
1408
|
+
toolName: string;
|
|
1409
|
+
arguments?: unknown;
|
|
1410
|
+
}[] | undefined;
|
|
1411
|
+
memoryReads?: string[] | undefined;
|
|
1412
|
+
memoryUpdates?: Record<string, {
|
|
1413
|
+
value?: unknown;
|
|
1414
|
+
metadata?: Record<string, unknown> | undefined;
|
|
1415
|
+
description?: string | undefined;
|
|
1416
|
+
}> | undefined;
|
|
1417
|
+
}>;
|
|
1418
|
+
type AgentDecision = z.infer<typeof AgentDecisionSchema>;
|
|
1419
|
+
/**
|
|
1420
|
+
* Schema for tool execution result summary
|
|
1421
|
+
*/
|
|
1422
|
+
declare const ToolExecutionSummarySchema: z.ZodObject<{
|
|
1423
|
+
/**
|
|
1424
|
+
* Tool name
|
|
1425
|
+
*/
|
|
1426
|
+
toolName: z.ZodString;
|
|
1427
|
+
/**
|
|
1428
|
+
* Whether execution succeeded
|
|
1429
|
+
*/
|
|
1430
|
+
success: z.ZodBoolean;
|
|
1431
|
+
/**
|
|
1432
|
+
* Output if successful
|
|
1433
|
+
*/
|
|
1434
|
+
output: z.ZodOptional<z.ZodUnknown>;
|
|
1435
|
+
/**
|
|
1436
|
+
* Error message if failed
|
|
1437
|
+
*/
|
|
1438
|
+
error: z.ZodOptional<z.ZodString>;
|
|
1439
|
+
}, "strip", z.ZodTypeAny, {
|
|
1440
|
+
toolName: string;
|
|
1441
|
+
success: boolean;
|
|
1442
|
+
output?: unknown;
|
|
1443
|
+
error?: string | undefined;
|
|
1444
|
+
}, {
|
|
1445
|
+
toolName: string;
|
|
1446
|
+
success: boolean;
|
|
1447
|
+
output?: unknown;
|
|
1448
|
+
error?: string | undefined;
|
|
1449
|
+
}>;
|
|
1450
|
+
type ToolExecutionSummary = z.infer<typeof ToolExecutionSummarySchema>;
|
|
1451
|
+
|
|
1452
|
+
declare const MCPConfigVariants: z.ZodDiscriminatedUnion<"transport", [z.ZodObject<{
|
|
1453
|
+
name: z.ZodString;
|
|
1454
|
+
timeout: z.ZodDefault<z.ZodNumber>;
|
|
1455
|
+
metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
1456
|
+
} & {
|
|
1457
|
+
transport: z.ZodLiteral<"stdio">;
|
|
1458
|
+
command: z.ZodString;
|
|
1459
|
+
args: z.ZodDefault<z.ZodArray<z.ZodString, "many">>;
|
|
1460
|
+
env: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
1461
|
+
cwd: z.ZodOptional<z.ZodString>;
|
|
1462
|
+
stderr: z.ZodOptional<z.ZodUnion<[z.ZodLiteral<"inherit">, z.ZodLiteral<"pipe">, z.ZodLiteral<"ignore">]>>;
|
|
1463
|
+
}, "strip", z.ZodTypeAny, {
|
|
1464
|
+
metadata: Record<string, unknown>;
|
|
1465
|
+
name: string;
|
|
1466
|
+
timeout: number;
|
|
1467
|
+
transport: "stdio";
|
|
1468
|
+
command: string;
|
|
1469
|
+
args: string[];
|
|
1470
|
+
env: Record<string, string>;
|
|
1471
|
+
cwd?: string | undefined;
|
|
1472
|
+
stderr?: "inherit" | "pipe" | "ignore" | undefined;
|
|
1473
|
+
}, {
|
|
1474
|
+
name: string;
|
|
1475
|
+
transport: "stdio";
|
|
1476
|
+
command: string;
|
|
1477
|
+
metadata?: Record<string, unknown> | undefined;
|
|
1478
|
+
timeout?: number | undefined;
|
|
1479
|
+
args?: string[] | undefined;
|
|
1480
|
+
env?: Record<string, string> | undefined;
|
|
1481
|
+
cwd?: string | undefined;
|
|
1482
|
+
stderr?: "inherit" | "pipe" | "ignore" | undefined;
|
|
1483
|
+
}>, z.ZodObject<{
|
|
1484
|
+
name: z.ZodString;
|
|
1485
|
+
timeout: z.ZodDefault<z.ZodNumber>;
|
|
1486
|
+
metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
1487
|
+
} & {
|
|
1488
|
+
transport: z.ZodLiteral<"http-sse">;
|
|
1489
|
+
url: z.ZodString;
|
|
1490
|
+
headers: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
1491
|
+
method: z.ZodDefault<z.ZodEnum<["GET", "POST"]>>;
|
|
1492
|
+
}, "strip", z.ZodTypeAny, {
|
|
1493
|
+
metadata: Record<string, unknown>;
|
|
1494
|
+
method: "GET" | "POST";
|
|
1495
|
+
headers: Record<string, string>;
|
|
1496
|
+
name: string;
|
|
1497
|
+
timeout: number;
|
|
1498
|
+
transport: "http-sse";
|
|
1499
|
+
url: string;
|
|
1500
|
+
}, {
|
|
1501
|
+
name: string;
|
|
1502
|
+
transport: "http-sse";
|
|
1503
|
+
url: string;
|
|
1504
|
+
metadata?: Record<string, unknown> | undefined;
|
|
1505
|
+
method?: "GET" | "POST" | undefined;
|
|
1506
|
+
headers?: Record<string, string> | undefined;
|
|
1507
|
+
timeout?: number | undefined;
|
|
1508
|
+
}>, z.ZodObject<{
|
|
1509
|
+
name: z.ZodString;
|
|
1510
|
+
timeout: z.ZodDefault<z.ZodNumber>;
|
|
1511
|
+
metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
1512
|
+
} & {
|
|
1513
|
+
transport: z.ZodLiteral<"streamable-http">;
|
|
1514
|
+
url: z.ZodString;
|
|
1515
|
+
headers: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
1516
|
+
sessionId: z.ZodOptional<z.ZodString>;
|
|
1517
|
+
}, "strip", z.ZodTypeAny, {
|
|
1518
|
+
metadata: Record<string, unknown>;
|
|
1519
|
+
headers: Record<string, string>;
|
|
1520
|
+
name: string;
|
|
1521
|
+
timeout: number;
|
|
1522
|
+
transport: "streamable-http";
|
|
1523
|
+
url: string;
|
|
1524
|
+
sessionId?: string | undefined;
|
|
1525
|
+
}, {
|
|
1526
|
+
name: string;
|
|
1527
|
+
transport: "streamable-http";
|
|
1528
|
+
url: string;
|
|
1529
|
+
metadata?: Record<string, unknown> | undefined;
|
|
1530
|
+
headers?: Record<string, string> | undefined;
|
|
1531
|
+
timeout?: number | undefined;
|
|
1532
|
+
sessionId?: string | undefined;
|
|
1533
|
+
}>]>;
|
|
1534
|
+
type MCPServerConfig = z.output<typeof MCPConfigVariants>;
|
|
1535
|
+
type MCPServerConfigInput = z.input<typeof MCPConfigVariants>;
|
|
1536
|
+
declare const MCPServerConfigSchema: z.ZodEffects<z.ZodDiscriminatedUnion<"transport", [z.ZodObject<{
|
|
1537
|
+
name: z.ZodString;
|
|
1538
|
+
timeout: z.ZodDefault<z.ZodNumber>;
|
|
1539
|
+
metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
1540
|
+
} & {
|
|
1541
|
+
transport: z.ZodLiteral<"stdio">;
|
|
1542
|
+
command: z.ZodString;
|
|
1543
|
+
args: z.ZodDefault<z.ZodArray<z.ZodString, "many">>;
|
|
1544
|
+
env: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
1545
|
+
cwd: z.ZodOptional<z.ZodString>;
|
|
1546
|
+
stderr: z.ZodOptional<z.ZodUnion<[z.ZodLiteral<"inherit">, z.ZodLiteral<"pipe">, z.ZodLiteral<"ignore">]>>;
|
|
1547
|
+
}, "strip", z.ZodTypeAny, {
|
|
1548
|
+
metadata: Record<string, unknown>;
|
|
1549
|
+
name: string;
|
|
1550
|
+
timeout: number;
|
|
1551
|
+
transport: "stdio";
|
|
1552
|
+
command: string;
|
|
1553
|
+
args: string[];
|
|
1554
|
+
env: Record<string, string>;
|
|
1555
|
+
cwd?: string | undefined;
|
|
1556
|
+
stderr?: "inherit" | "pipe" | "ignore" | undefined;
|
|
1557
|
+
}, {
|
|
1558
|
+
name: string;
|
|
1559
|
+
transport: "stdio";
|
|
1560
|
+
command: string;
|
|
1561
|
+
metadata?: Record<string, unknown> | undefined;
|
|
1562
|
+
timeout?: number | undefined;
|
|
1563
|
+
args?: string[] | undefined;
|
|
1564
|
+
env?: Record<string, string> | undefined;
|
|
1565
|
+
cwd?: string | undefined;
|
|
1566
|
+
stderr?: "inherit" | "pipe" | "ignore" | undefined;
|
|
1567
|
+
}>, z.ZodObject<{
|
|
1568
|
+
name: z.ZodString;
|
|
1569
|
+
timeout: z.ZodDefault<z.ZodNumber>;
|
|
1570
|
+
metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
1571
|
+
} & {
|
|
1572
|
+
transport: z.ZodLiteral<"http-sse">;
|
|
1573
|
+
url: z.ZodString;
|
|
1574
|
+
headers: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
1575
|
+
method: z.ZodDefault<z.ZodEnum<["GET", "POST"]>>;
|
|
1576
|
+
}, "strip", z.ZodTypeAny, {
|
|
1577
|
+
metadata: Record<string, unknown>;
|
|
1578
|
+
method: "GET" | "POST";
|
|
1579
|
+
headers: Record<string, string>;
|
|
1580
|
+
name: string;
|
|
1581
|
+
timeout: number;
|
|
1582
|
+
transport: "http-sse";
|
|
1583
|
+
url: string;
|
|
1584
|
+
}, {
|
|
1585
|
+
name: string;
|
|
1586
|
+
transport: "http-sse";
|
|
1587
|
+
url: string;
|
|
1588
|
+
metadata?: Record<string, unknown> | undefined;
|
|
1589
|
+
method?: "GET" | "POST" | undefined;
|
|
1590
|
+
headers?: Record<string, string> | undefined;
|
|
1591
|
+
timeout?: number | undefined;
|
|
1592
|
+
}>, z.ZodObject<{
|
|
1593
|
+
name: z.ZodString;
|
|
1594
|
+
timeout: z.ZodDefault<z.ZodNumber>;
|
|
1595
|
+
metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
1596
|
+
} & {
|
|
1597
|
+
transport: z.ZodLiteral<"streamable-http">;
|
|
1598
|
+
url: z.ZodString;
|
|
1599
|
+
headers: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
1600
|
+
sessionId: z.ZodOptional<z.ZodString>;
|
|
1601
|
+
}, "strip", z.ZodTypeAny, {
|
|
1602
|
+
metadata: Record<string, unknown>;
|
|
1603
|
+
headers: Record<string, string>;
|
|
1604
|
+
name: string;
|
|
1605
|
+
timeout: number;
|
|
1606
|
+
transport: "streamable-http";
|
|
1607
|
+
url: string;
|
|
1608
|
+
sessionId?: string | undefined;
|
|
1609
|
+
}, {
|
|
1610
|
+
name: string;
|
|
1611
|
+
transport: "streamable-http";
|
|
1612
|
+
url: string;
|
|
1613
|
+
metadata?: Record<string, unknown> | undefined;
|
|
1614
|
+
headers?: Record<string, string> | undefined;
|
|
1615
|
+
timeout?: number | undefined;
|
|
1616
|
+
sessionId?: string | undefined;
|
|
1617
|
+
}>]>, {
|
|
1618
|
+
metadata: Record<string, unknown>;
|
|
1619
|
+
name: string;
|
|
1620
|
+
timeout: number;
|
|
1621
|
+
transport: "stdio";
|
|
1622
|
+
command: string;
|
|
1623
|
+
args: string[];
|
|
1624
|
+
env: Record<string, string>;
|
|
1625
|
+
cwd?: string | undefined;
|
|
1626
|
+
stderr?: "inherit" | "pipe" | "ignore" | undefined;
|
|
1627
|
+
} | {
|
|
1628
|
+
metadata: Record<string, unknown>;
|
|
1629
|
+
method: "GET" | "POST";
|
|
1630
|
+
headers: Record<string, string>;
|
|
1631
|
+
name: string;
|
|
1632
|
+
timeout: number;
|
|
1633
|
+
transport: "http-sse";
|
|
1634
|
+
url: string;
|
|
1635
|
+
} | {
|
|
1636
|
+
metadata: Record<string, unknown>;
|
|
1637
|
+
headers: Record<string, string>;
|
|
1638
|
+
name: string;
|
|
1639
|
+
timeout: number;
|
|
1640
|
+
transport: "streamable-http";
|
|
1641
|
+
url: string;
|
|
1642
|
+
sessionId?: string | undefined;
|
|
1643
|
+
}, {
|
|
1644
|
+
name: string;
|
|
1645
|
+
transport: "stdio";
|
|
1646
|
+
command: string;
|
|
1647
|
+
metadata?: Record<string, unknown> | undefined;
|
|
1648
|
+
timeout?: number | undefined;
|
|
1649
|
+
args?: string[] | undefined;
|
|
1650
|
+
env?: Record<string, string> | undefined;
|
|
1651
|
+
cwd?: string | undefined;
|
|
1652
|
+
stderr?: "inherit" | "pipe" | "ignore" | undefined;
|
|
1653
|
+
} | {
|
|
1654
|
+
name: string;
|
|
1655
|
+
transport: "http-sse";
|
|
1656
|
+
url: string;
|
|
1657
|
+
metadata?: Record<string, unknown> | undefined;
|
|
1658
|
+
method?: "GET" | "POST" | undefined;
|
|
1659
|
+
headers?: Record<string, string> | undefined;
|
|
1660
|
+
timeout?: number | undefined;
|
|
1661
|
+
} | {
|
|
1662
|
+
name: string;
|
|
1663
|
+
transport: "streamable-http";
|
|
1664
|
+
url: string;
|
|
1665
|
+
metadata?: Record<string, unknown> | undefined;
|
|
1666
|
+
headers?: Record<string, string> | undefined;
|
|
1667
|
+
timeout?: number | undefined;
|
|
1668
|
+
sessionId?: string | undefined;
|
|
1669
|
+
}>;
|
|
1670
|
+
declare const MCPconfig: (config: MCPServerConfigInput) => MCPServerConfig;
|
|
1671
|
+
declare const createMCPServerConfig: (config: MCPServerConfigInput) => MCPServerConfig;
|
|
1672
|
+
|
|
1673
|
+
interface MCPTool {
|
|
1674
|
+
name: string;
|
|
1675
|
+
description: string;
|
|
1676
|
+
parameters: Record<string, unknown>;
|
|
1677
|
+
outputSchema?: Record<string, unknown>;
|
|
1678
|
+
}
|
|
1679
|
+
interface MCPClientOptions {
|
|
1680
|
+
clientInfo?: Implementation;
|
|
1681
|
+
}
|
|
1682
|
+
declare class MCPClient {
|
|
1683
|
+
private readonly config;
|
|
1684
|
+
private readonly client;
|
|
1685
|
+
private transport;
|
|
1686
|
+
private connected;
|
|
1687
|
+
private toolCache;
|
|
1688
|
+
constructor(config: MCPServerConfig, options?: MCPClientOptions);
|
|
1689
|
+
static fromConfig(config: MCPServerConfig, options?: MCPClientOptions): MCPClient;
|
|
1690
|
+
connect(): Promise<void>;
|
|
1691
|
+
disconnect(): Promise<void>;
|
|
1692
|
+
listTools(): Promise<MCPTool[]>;
|
|
1693
|
+
callTool(toolName: string, args: Record<string, unknown> | undefined): Promise<unknown>;
|
|
1694
|
+
get isConnected(): boolean;
|
|
1695
|
+
private ensureConnected;
|
|
1696
|
+
private createTransport;
|
|
1697
|
+
}
|
|
1698
|
+
|
|
1699
|
+
interface MCPToolProviderOptions {
|
|
1700
|
+
namePrefix?: string;
|
|
1701
|
+
clientOptions?: MCPClientOptions;
|
|
1702
|
+
clientFactory?: (config: MCPServerConfig, options?: MCPClientOptions) => MCPClient;
|
|
1703
|
+
logger?: {
|
|
1704
|
+
debug?: (message: string, context?: Record<string, unknown>) => void;
|
|
1705
|
+
info?: (message: string, context?: Record<string, unknown>) => void;
|
|
1706
|
+
warn?: (message: string, context?: Record<string, unknown>) => void;
|
|
1707
|
+
error?: (message: string, context?: Record<string, unknown>) => void;
|
|
1708
|
+
};
|
|
1709
|
+
}
|
|
1710
|
+
declare class MCPToolProvider implements ToolProvider {
|
|
1711
|
+
private readonly configs;
|
|
1712
|
+
private readonly namePrefix;
|
|
1713
|
+
private readonly clientOptions;
|
|
1714
|
+
private readonly clientFactory;
|
|
1715
|
+
private readonly logger;
|
|
1716
|
+
private readonly clients;
|
|
1717
|
+
constructor(configs: ReadonlyArray<MCPServerConfig>, options?: MCPToolProviderOptions);
|
|
1718
|
+
setup(agent: BaseAgent<unknown, unknown>): Promise<Array<Tool<unknown, unknown>>>;
|
|
1719
|
+
teardown(): Promise<void>;
|
|
1720
|
+
private wrapTool;
|
|
1721
|
+
}
|
|
1722
|
+
declare const mcp: (...configs: ReadonlyArray<MCPServerConfig | MCPServerConfigInput>) => MCPToolProvider;
|
|
1723
|
+
|
|
1724
|
+
declare class SchemaValidationError extends Error {
|
|
1725
|
+
readonly issues: ZodIssue[];
|
|
1726
|
+
constructor(message: string, issues: ZodIssue[]);
|
|
1727
|
+
}
|
|
1728
|
+
type Schema<T> = ZodType<T>;
|
|
1729
|
+
interface SchemaValidationOptions {
|
|
1730
|
+
message?: string;
|
|
1731
|
+
}
|
|
1732
|
+
declare const validateSchema: <T>(schema: Schema<T>, value: unknown, options?: SchemaValidationOptions) => T;
|
|
1733
|
+
declare const isSchemaValid: <T>(schema: Schema<T>, value: unknown) => value is T;
|
|
1734
|
+
interface JsonSchemaOptions {
|
|
1735
|
+
name?: string;
|
|
1736
|
+
target?: "jsonSchema7" | "openApi3";
|
|
1737
|
+
}
|
|
1738
|
+
declare const schemaToJson: (schema: ZodTypeAny, options?: JsonSchemaOptions) => JsonSchema7Type;
|
|
1739
|
+
declare const getSchemaDefault: <T>(schema: Schema<T>) => T | undefined;
|
|
1740
|
+
declare const mergeSchemaDefaults: <T extends Record<string, unknown>>(schema: Schema<T>, value: Partial<T>) => T;
|
|
1741
|
+
|
|
1742
|
+
/**
|
|
1743
|
+
* Options for creating a tool from a function
|
|
1744
|
+
*/
|
|
1745
|
+
interface ToolOptions<TInput> {
|
|
1746
|
+
/**
|
|
1747
|
+
* Tool name (defaults to function name)
|
|
1748
|
+
*/
|
|
1749
|
+
name?: string;
|
|
1750
|
+
/**
|
|
1751
|
+
* Human-readable description
|
|
1752
|
+
*/
|
|
1753
|
+
description?: string;
|
|
1754
|
+
/**
|
|
1755
|
+
* Input validation schema
|
|
1756
|
+
*/
|
|
1757
|
+
schema?: ZodType<TInput>;
|
|
1758
|
+
/**
|
|
1759
|
+
* Execution timeout in milliseconds
|
|
1760
|
+
*/
|
|
1761
|
+
timeoutMs?: number;
|
|
1762
|
+
/**
|
|
1763
|
+
* Additional metadata
|
|
1764
|
+
*/
|
|
1765
|
+
metadata?: Record<string, unknown>;
|
|
1766
|
+
}
|
|
1767
|
+
/**
|
|
1768
|
+
* Function that can be converted to a tool
|
|
1769
|
+
*/
|
|
1770
|
+
type ToolFunction<TInput, TOutput> = (input: TInput, context: ToolExecutionContext) => TOutput | Promise<TOutput>;
|
|
1771
|
+
type ToolMethodDecorator<TInput, TOutput> = {
|
|
1772
|
+
<TMethod extends ToolFunction<TInput, TOutput>>(value: TMethod, context: ClassMethodDecoratorContext<unknown, TMethod>): TMethod | void;
|
|
1773
|
+
(target: object, propertyKey: string | symbol, descriptor: PropertyDescriptor | undefined): PropertyDescriptor | void;
|
|
1774
|
+
};
|
|
1775
|
+
/**
|
|
1776
|
+
* Convert a function into a Tool.
|
|
1777
|
+
* Handles both sync and async functions, wraps execution in ToolResult.
|
|
1778
|
+
*
|
|
1779
|
+
* @param fn - Function to wrap
|
|
1780
|
+
* @param options - Tool configuration
|
|
1781
|
+
* @returns Tool definition
|
|
1782
|
+
*
|
|
1783
|
+
* @example
|
|
1784
|
+
* const addTool = createFunctionTool(
|
|
1785
|
+
* (input: { a: number; b: number }) => input.a + input.b,
|
|
1786
|
+
* {
|
|
1787
|
+
* name: "add",
|
|
1788
|
+
* description: "Add two numbers",
|
|
1789
|
+
* schema: z.object({ a: z.number(), b: z.number() })
|
|
1790
|
+
* }
|
|
1791
|
+
* );
|
|
1792
|
+
*/
|
|
1793
|
+
declare function createFunctionTool<TInput, TOutput>(fn: ToolFunction<TInput, TOutput>, options?: ToolOptions<TInput>): Tool<TInput, TOutput>;
|
|
1794
|
+
/**
|
|
1795
|
+
* Decorator factory for creating tools from methods.
|
|
1796
|
+
* Can be used with or without arguments.
|
|
1797
|
+
*
|
|
1798
|
+
* @param options - Tool configuration
|
|
1799
|
+
* @returns Method decorator
|
|
1800
|
+
*
|
|
1801
|
+
* @example
|
|
1802
|
+
* class MyTools {
|
|
1803
|
+
* @tool({ description: "Add numbers", schema: AddSchema })
|
|
1804
|
+
* add(input: { a: number; b: number }): number {
|
|
1805
|
+
* return input.a + input.b;
|
|
1806
|
+
* }
|
|
1807
|
+
* }
|
|
1808
|
+
*/
|
|
1809
|
+
declare function tool<TInput = unknown, TOutput = unknown>(options?: ToolOptions<TInput>): ToolMethodDecorator<TInput, TOutput>;
|
|
1810
|
+
/**
|
|
1811
|
+
* Extract all tools defined via @tool decorator from a class instance
|
|
1812
|
+
*
|
|
1813
|
+
* @param instance - Class instance with @tool decorated methods
|
|
1814
|
+
* @returns Array of Tool definitions
|
|
1815
|
+
*
|
|
1816
|
+
* @example
|
|
1817
|
+
* class MyTools {
|
|
1818
|
+
* @tool() greet(input: { name: string }) { return `Hello ${input.name}`; }
|
|
1819
|
+
* }
|
|
1820
|
+
*
|
|
1821
|
+
* const tools = extractTools(new MyTools());
|
|
1822
|
+
*/
|
|
1823
|
+
declare function extractTools(instance: object): Array<Tool<unknown, unknown>>;
|
|
1824
|
+
|
|
1825
|
+
/**
|
|
1826
|
+
* Options for tool execution
|
|
1827
|
+
*/
|
|
1828
|
+
interface ToolRunOptions {
|
|
1829
|
+
/**
|
|
1830
|
+
* Abort signal for cancellation
|
|
1831
|
+
*/
|
|
1832
|
+
signal?: AbortSignal;
|
|
1833
|
+
/**
|
|
1834
|
+
* Additional metadata to pass to the tool
|
|
1835
|
+
*/
|
|
1836
|
+
metadata?: Record<string, unknown>;
|
|
1837
|
+
/**
|
|
1838
|
+
* Override timeout for this execution
|
|
1839
|
+
*/
|
|
1840
|
+
timeoutMs?: number;
|
|
1841
|
+
}
|
|
1842
|
+
/**
|
|
1843
|
+
* Utility class for executing tools with validation and error handling
|
|
1844
|
+
*/
|
|
1845
|
+
declare class ToolRunner {
|
|
1846
|
+
/**
|
|
1847
|
+
* Execute a tool with the given input and context
|
|
1848
|
+
*
|
|
1849
|
+
* @param tool - Tool to execute
|
|
1850
|
+
* @param input - Input data
|
|
1851
|
+
* @param context - Agent execution context
|
|
1852
|
+
* @param options - Execution options
|
|
1853
|
+
* @returns Tool execution result
|
|
1854
|
+
*/
|
|
1855
|
+
static execute<TInput, TOutput>(tool: Tool<TInput, TOutput>, input: TInput, context: AgentContext, options?: ToolRunOptions): Promise<ToolResult<TOutput>>;
|
|
1856
|
+
/**
|
|
1857
|
+
* Execute multiple tools in parallel
|
|
1858
|
+
*
|
|
1859
|
+
* @param executions - Array of tool execution tuples [tool, input, context, options?]
|
|
1860
|
+
* @returns Array of results in same order as input
|
|
1861
|
+
*/
|
|
1862
|
+
static executeParallel<TInput, TOutput>(executions: Array<[
|
|
1863
|
+
Tool<TInput, TOutput>,
|
|
1864
|
+
TInput,
|
|
1865
|
+
AgentContext,
|
|
1866
|
+
ToolRunOptions?
|
|
1867
|
+
]>): Promise<Array<ToolResult<TOutput>>>;
|
|
1868
|
+
/**
|
|
1869
|
+
* Execute multiple tools sequentially, stopping on first failure
|
|
1870
|
+
*
|
|
1871
|
+
* @param executions - Array of tool execution tuples
|
|
1872
|
+
* @returns Array of results up to first failure (inclusive)
|
|
1873
|
+
*/
|
|
1874
|
+
static executeSequential<TInput, TOutput>(executions: Array<[
|
|
1875
|
+
Tool<TInput, TOutput>,
|
|
1876
|
+
TInput,
|
|
1877
|
+
AgentContext,
|
|
1878
|
+
ToolRunOptions?
|
|
1879
|
+
]>): Promise<Array<ToolResult<TOutput>>>;
|
|
1880
|
+
/**
|
|
1881
|
+
* Execute a tool with timeout
|
|
1882
|
+
*
|
|
1883
|
+
* @param promise - Tool execution promise
|
|
1884
|
+
* @param timeoutMs - Timeout in milliseconds
|
|
1885
|
+
* @param toolName - Tool name for error messages
|
|
1886
|
+
* @returns Result or rejects with timeout error
|
|
1887
|
+
*/
|
|
1888
|
+
private static executeWithTimeout;
|
|
1889
|
+
/**
|
|
1890
|
+
* Validate tool input without executing
|
|
1891
|
+
*
|
|
1892
|
+
* @param tool - Tool to validate input for
|
|
1893
|
+
* @param input - Input to validate
|
|
1894
|
+
* @returns true if valid, Error if invalid
|
|
1895
|
+
*/
|
|
1896
|
+
static validate<TInput>(tool: Tool<TInput, unknown>, input: TInput): true | Error;
|
|
1897
|
+
/**
|
|
1898
|
+
* Check if a result indicates success
|
|
1899
|
+
*
|
|
1900
|
+
* @param result - Tool result to check
|
|
1901
|
+
* @returns true if success, false if failure
|
|
1902
|
+
*/
|
|
1903
|
+
static isSuccess<TOutput>(result: ToolResult<TOutput>): result is {
|
|
1904
|
+
success: true;
|
|
1905
|
+
toolName: string;
|
|
1906
|
+
output: TOutput;
|
|
1907
|
+
metadata: Record<string, unknown>;
|
|
1908
|
+
startedAt?: number;
|
|
1909
|
+
finishedAt?: number;
|
|
1910
|
+
};
|
|
1911
|
+
/**
|
|
1912
|
+
* Check if a result indicates failure
|
|
1913
|
+
*
|
|
1914
|
+
* @param result - Tool result to check
|
|
1915
|
+
* @returns true if failure, false if success
|
|
1916
|
+
*/
|
|
1917
|
+
static isFailure<TOutput>(result: ToolResult<TOutput>): result is {
|
|
1918
|
+
success: false;
|
|
1919
|
+
toolName: string;
|
|
1920
|
+
error: Error | string;
|
|
1921
|
+
metadata: Record<string, unknown>;
|
|
1922
|
+
startedAt?: number;
|
|
1923
|
+
finishedAt?: number;
|
|
1924
|
+
};
|
|
1925
|
+
}
|
|
1926
|
+
|
|
1927
|
+
export { Agent, type AgentConfig, AgentContext, type AgentContextOptions, type AgentContextSnapshot, type AgentDecision, AgentDecisionSchema, type AgentLogger, BaseAgent, type BaseAgentConfig, ConsoleLogger, type CreateSpanOptions, DEFAULT_MODEL, DEFAULT_RETRY_CONFIG, type ExecutionCycle, ExecutionCycleSchema, type Failure, type HookEventName, HookEvents, type HookHandler, HookManager, type HookPayload, type HookPayloadMap, type HookRegistration, InMemoryStore, type IterationSummary, type JsonSchemaOptions, LogLevel, MCPClient, type MCPClientOptions, type MCPServerConfig, type MCPServerConfigInput, MCPServerConfigSchema, type MCPTool, MCPToolProvider, type MCPToolProviderOptions, MCPconfig, type MaybePromise, type Memory, type MemoryCatalogEntry, type MemoryEntry, type MemoryEntryMetadata, MemoryEntryMetadataSchema, MemoryEntrySchema, type MemoryUpdate, MemoryUpdateSchema, type OpperCallOptions, type OpperCallResponse, OpperClient, type OpperClientConfig, type OpperSpan, Result, type RetryConfig, type Schema, SchemaValidationError, type SchemaValidationOptions, SilentLogger, type Success, type Thought, ThoughtSchema, type Tool, type ToolCall, type ToolCallRecord, ToolCallRecordSchema, ToolCallSchema, type ToolDefinition, type ToolExecutionContext, type ToolExecutionSummary, ToolExecutionSummarySchema, type ToolFailure, type ToolFunction, ToolMetadataSchema, type ToolOptions, type ToolProvider, type ToolResult, type ToolResultData, ToolResultFactory, ToolResultFailureSchema, type ToolResultInit, ToolResultSchema, ToolResultSuccessSchema, type ToolRunOptions, ToolRunner, type ToolSuccess, type UnregisterHook, type Usage, UsageSchema, coerceToolDefinition, createFunctionTool, createHookManager, createInMemoryStore, createMCPServerConfig, createOpperClient, createToolCallRecord, err, extractTools, getDefaultLogger, getSchemaDefault, isSchemaValid, isToolProvider, mcp, mergeSchemaDefaults, normalizeToolEntries, ok, schemaToJson, setDefaultLogger, tool, validateSchema, validateToolInput };
|