@agentuity/runtime 0.0.60 → 0.0.62
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/_context.d.ts +11 -7
- package/dist/_context.d.ts.map +1 -1
- package/dist/_context.js +9 -2
- package/dist/_context.js.map +1 -1
- package/dist/_server.d.ts +4 -2
- package/dist/_server.d.ts.map +1 -1
- package/dist/_server.js +79 -31
- package/dist/_server.js.map +1 -1
- package/dist/_services.d.ts +1 -1
- package/dist/_services.d.ts.map +1 -1
- package/dist/_services.js +4 -2
- package/dist/_services.js.map +1 -1
- package/dist/_waituntil.d.ts.map +1 -1
- package/dist/_waituntil.js +5 -2
- package/dist/_waituntil.js.map +1 -1
- package/dist/agent.d.ts +647 -19
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +55 -6
- package/dist/agent.js.map +1 -1
- package/dist/app.d.ts +205 -28
- package/dist/app.d.ts.map +1 -1
- package/dist/app.js +181 -13
- package/dist/app.js.map +1 -1
- package/dist/index.d.ts +41 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/io/email.d.ts.map +1 -1
- package/dist/io/email.js +11 -3
- package/dist/io/email.js.map +1 -1
- package/dist/router.d.ts +282 -32
- package/dist/router.d.ts.map +1 -1
- package/dist/router.js +110 -35
- package/dist/router.js.map +1 -1
- package/dist/services/evalrun/http.d.ts.map +1 -1
- package/dist/services/evalrun/http.js +7 -5
- package/dist/services/evalrun/http.js.map +1 -1
- package/dist/services/local/_util.d.ts.map +1 -1
- package/dist/services/local/_util.js +3 -1
- package/dist/services/local/_util.js.map +1 -1
- package/dist/services/session/http.d.ts.map +1 -1
- package/dist/services/session/http.js +4 -3
- package/dist/services/session/http.js.map +1 -1
- package/dist/session.d.ts +308 -6
- package/dist/session.d.ts.map +1 -1
- package/dist/session.js +331 -23
- package/dist/session.js.map +1 -1
- package/package.json +8 -5
- package/src/_context.ts +37 -9
- package/src/_server.ts +96 -36
- package/src/_services.ts +9 -2
- package/src/_waituntil.ts +13 -2
- package/src/agent.ts +856 -68
- package/src/app.ts +238 -38
- package/src/index.ts +42 -2
- package/src/io/email.ts +23 -5
- package/src/router.ts +359 -83
- package/src/services/evalrun/http.ts +15 -4
- package/src/services/local/_util.ts +7 -1
- package/src/services/session/http.ts +5 -2
- package/src/session.ts +686 -26
package/dist/agent.d.ts
CHANGED
|
@@ -1,27 +1,219 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type KeyValueStorage, type ObjectStorage, type StandardSchemaV1, type StreamStorage, type VectorStorage } from '@agentuity/core';
|
|
2
2
|
import { type Tracer } from '@opentelemetry/api';
|
|
3
3
|
import type { Context, MiddlewareHandler } from 'hono';
|
|
4
4
|
import type { Logger } from './logger';
|
|
5
5
|
import type { Eval, EvalFunction, ExternalEvalMetadata } from './eval';
|
|
6
6
|
import type { Thread, Session } from './session';
|
|
7
|
+
import type { AppState } from './index';
|
|
7
8
|
export type AgentEventName = 'started' | 'completed' | 'errored';
|
|
8
9
|
export type AgentEventCallback<TAgent extends Agent<any, any, any>> = ((eventName: 'started', agent: TAgent, context: AgentContext<any, any, any>) => Promise<void> | void) | ((eventName: 'completed', agent: TAgent, context: AgentContext<any, any, any>) => Promise<void> | void) | ((eventName: 'errored', agent: TAgent, context: AgentContext<any, any, any>, data: Error) => Promise<void> | void);
|
|
9
|
-
|
|
10
|
+
/**
|
|
11
|
+
* Context object passed to every agent handler providing access to runtime services and state.
|
|
12
|
+
*
|
|
13
|
+
* @template TAgentRegistry - Registry of all available agents (auto-generated, strongly-typed)
|
|
14
|
+
* @template TCurrent - Current agent runner type
|
|
15
|
+
* @template TParent - Parent agent runner type (if called from another agent)
|
|
16
|
+
* @template TConfig - Agent-specific configuration type from setup function
|
|
17
|
+
* @template TAppState - Application-wide state type from createApp
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* const agent = createAgent({
|
|
22
|
+
* handler: async (ctx, input) => {
|
|
23
|
+
* // Logging
|
|
24
|
+
* ctx.logger.info('Processing request', { input });
|
|
25
|
+
*
|
|
26
|
+
* // Call another agent
|
|
27
|
+
* const result = await ctx.agent.otherAgent.run({ data: input });
|
|
28
|
+
*
|
|
29
|
+
* // Store data
|
|
30
|
+
* await ctx.kv.set('key', { value: result });
|
|
31
|
+
*
|
|
32
|
+
* // Access config from setup
|
|
33
|
+
* const cache = ctx.config.cache;
|
|
34
|
+
*
|
|
35
|
+
* // Background task
|
|
36
|
+
* ctx.waitUntil(async () => {
|
|
37
|
+
* await ctx.logger.info('Cleanup complete');
|
|
38
|
+
* });
|
|
39
|
+
*
|
|
40
|
+
* return result;
|
|
41
|
+
* }
|
|
42
|
+
* });
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export interface AgentContext<TAgentRegistry extends AgentRegistry = AgentRegistry, TCurrent extends AgentRunner<any, any, any> | undefined = AgentRunner<any, any, any> | undefined, TParent extends AgentRunner<any, any, any> | undefined = AgentRunner<any, any, any> | undefined, TConfig = unknown, TAppState = Record<string, never>> {
|
|
46
|
+
/**
|
|
47
|
+
* Schedule a background task that continues after the response is sent.
|
|
48
|
+
* Useful for cleanup, logging, or async operations that don't block the response.
|
|
49
|
+
*
|
|
50
|
+
* @param promise - Promise or function that returns void or Promise<void>
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* ctx.waitUntil(async () => {
|
|
55
|
+
* await ctx.kv.set('processed', Date.now());
|
|
56
|
+
* ctx.logger.info('Background task complete');
|
|
57
|
+
* });
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
10
60
|
waitUntil: (promise: Promise<void> | (() => void | Promise<void>)) => void;
|
|
61
|
+
/**
|
|
62
|
+
* Registry of all agents in the application. Strongly-typed and auto-generated.
|
|
63
|
+
* Use to call other agents from within your handler.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```typescript
|
|
67
|
+
* const emailResult = await ctx.agent.email.run({ to: 'user@example.com' });
|
|
68
|
+
* const smsResult = await ctx.agent.sms.run({ phone: '+1234567890' });
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
11
71
|
agent: TAgentRegistry;
|
|
72
|
+
/**
|
|
73
|
+
* Information about the currently executing agent.
|
|
74
|
+
*/
|
|
12
75
|
current: TCurrent;
|
|
76
|
+
/**
|
|
77
|
+
* Information about the parent agent (if this agent was called by another agent).
|
|
78
|
+
* Use ctx.agent.parentName for strongly-typed access.
|
|
79
|
+
*/
|
|
13
80
|
parent: TParent;
|
|
81
|
+
/**
|
|
82
|
+
* Name of the current agent being executed.
|
|
83
|
+
*/
|
|
14
84
|
agentName: AgentName;
|
|
85
|
+
/**
|
|
86
|
+
* Structured logger with OpenTelemetry integration.
|
|
87
|
+
* Logs are automatically correlated with traces.
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```typescript
|
|
91
|
+
* ctx.logger.info('Processing started', { userId: input.id });
|
|
92
|
+
* ctx.logger.warn('Rate limit approaching', { remaining: 10 });
|
|
93
|
+
* ctx.logger.error('Operation failed', { error: err.message });
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
15
96
|
logger: Logger;
|
|
97
|
+
/**
|
|
98
|
+
* Unique session identifier for this request. Consistent across agent calls in the same session.
|
|
99
|
+
*/
|
|
16
100
|
sessionId: string;
|
|
101
|
+
/**
|
|
102
|
+
* OpenTelemetry tracer for creating custom spans and tracking performance.
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```typescript
|
|
106
|
+
* const span = ctx.tracer.startSpan('database-query');
|
|
107
|
+
* try {
|
|
108
|
+
* const result = await database.query();
|
|
109
|
+
* span.setStatus({ code: SpanStatusCode.OK });
|
|
110
|
+
* return result;
|
|
111
|
+
* } finally {
|
|
112
|
+
* span.end();
|
|
113
|
+
* }
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
17
116
|
tracer: Tracer;
|
|
117
|
+
/**
|
|
118
|
+
* Key-value storage for simple data persistence.
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* ```typescript
|
|
122
|
+
* await ctx.kv.set('user:123', { name: 'Alice', age: 30 });
|
|
123
|
+
* const user = await ctx.kv.get('user:123');
|
|
124
|
+
* await ctx.kv.delete('user:123');
|
|
125
|
+
* const keys = await ctx.kv.list('user:*');
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
18
128
|
kv: KeyValueStorage;
|
|
129
|
+
/**
|
|
130
|
+
* Object storage for files and blobs (S3-compatible).
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* ```typescript
|
|
134
|
+
* await ctx.objectstore.put('images/photo.jpg', buffer);
|
|
135
|
+
* const file = await ctx.objectstore.get('images/photo.jpg');
|
|
136
|
+
* await ctx.objectstore.delete('images/photo.jpg');
|
|
137
|
+
* const objects = await ctx.objectstore.list('images/');
|
|
138
|
+
* ```
|
|
139
|
+
*/
|
|
19
140
|
objectstore: ObjectStorage;
|
|
141
|
+
/**
|
|
142
|
+
* Stream storage for real-time data streams and logs.
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```typescript
|
|
146
|
+
* const stream = await ctx.stream.create('agent-logs');
|
|
147
|
+
* await ctx.stream.write(stream.id, 'Processing step 1');
|
|
148
|
+
* await ctx.stream.write(stream.id, 'Processing step 2');
|
|
149
|
+
* ```
|
|
150
|
+
*/
|
|
20
151
|
stream: StreamStorage;
|
|
152
|
+
/**
|
|
153
|
+
* Vector storage for embeddings and similarity search.
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* ```typescript
|
|
157
|
+
* await ctx.vector.upsert('docs', [
|
|
158
|
+
* { id: '1', values: [0.1, 0.2, 0.3], metadata: { text: 'Hello' } }
|
|
159
|
+
* ]);
|
|
160
|
+
* const results = await ctx.vector.query('docs', [0.1, 0.2, 0.3], { topK: 5 });
|
|
161
|
+
* ```
|
|
162
|
+
*/
|
|
21
163
|
vector: VectorStorage;
|
|
164
|
+
/**
|
|
165
|
+
* In-memory state storage scoped to the current request.
|
|
166
|
+
* Use for passing data between middleware and handlers.
|
|
167
|
+
*
|
|
168
|
+
* @example
|
|
169
|
+
* ```typescript
|
|
170
|
+
* ctx.state.set('startTime', Date.now());
|
|
171
|
+
* const duration = Date.now() - (ctx.state.get('startTime') as number);
|
|
172
|
+
* ```
|
|
173
|
+
*/
|
|
22
174
|
state: Map<string, unknown>;
|
|
175
|
+
/**
|
|
176
|
+
* Thread information for multi-turn conversations.
|
|
177
|
+
*/
|
|
23
178
|
thread: Thread;
|
|
179
|
+
/**
|
|
180
|
+
* Session information for the current request.
|
|
181
|
+
*/
|
|
24
182
|
session: Session;
|
|
183
|
+
/**
|
|
184
|
+
* Agent-specific configuration returned from the setup function.
|
|
185
|
+
* Type is inferred from your setup function's return value.
|
|
186
|
+
*
|
|
187
|
+
* @example
|
|
188
|
+
* ```typescript
|
|
189
|
+
* createAgent({
|
|
190
|
+
* setup: async () => ({ cache: new Map(), db: await connectDB() }),
|
|
191
|
+
* handler: async (ctx, input) => {
|
|
192
|
+
* ctx.config.cache.set('key', 'value'); // Strongly typed!
|
|
193
|
+
* await ctx.config.db.query('SELECT * FROM users');
|
|
194
|
+
* }
|
|
195
|
+
* });
|
|
196
|
+
* ```
|
|
197
|
+
*/
|
|
198
|
+
config: TConfig;
|
|
199
|
+
/**
|
|
200
|
+
* Application-wide state returned from createApp setup function.
|
|
201
|
+
* Shared across all agents in the application.
|
|
202
|
+
*
|
|
203
|
+
* @example
|
|
204
|
+
* ```typescript
|
|
205
|
+
* const app = createApp({
|
|
206
|
+
* setup: async () => ({ db: await connectDB(), redis: await connectRedis() })
|
|
207
|
+
* });
|
|
208
|
+
*
|
|
209
|
+
* // Later in any agent:
|
|
210
|
+
* handler: async (ctx, input) => {
|
|
211
|
+
* await ctx.app.db.query('SELECT 1');
|
|
212
|
+
* await ctx.app.redis.set('key', 'value');
|
|
213
|
+
* }
|
|
214
|
+
* ```
|
|
215
|
+
*/
|
|
216
|
+
app: TAppState;
|
|
25
217
|
}
|
|
26
218
|
type InternalAgentMetadata = {
|
|
27
219
|
/**
|
|
@@ -56,24 +248,207 @@ type ExternalAgentMetadata = {
|
|
|
56
248
|
description?: string;
|
|
57
249
|
};
|
|
58
250
|
type AgentMetadata = InternalAgentMetadata & ExternalAgentMetadata;
|
|
59
|
-
|
|
251
|
+
/**
|
|
252
|
+
* Configuration object for creating an agent evaluation function.
|
|
253
|
+
*
|
|
254
|
+
* @template TInput - Input schema type from the parent agent
|
|
255
|
+
* @template TOutput - Output schema type from the parent agent
|
|
256
|
+
*/
|
|
257
|
+
export interface CreateEvalConfig<TInput extends StandardSchemaV1 | undefined = any, TOutput extends StandardSchemaV1 | undefined = any> {
|
|
258
|
+
/**
|
|
259
|
+
* Optional metadata for the evaluation function.
|
|
260
|
+
*
|
|
261
|
+
* @example
|
|
262
|
+
* ```typescript
|
|
263
|
+
* metadata: {
|
|
264
|
+
* name: 'Validate positive output',
|
|
265
|
+
* description: 'Ensures output is greater than zero'
|
|
266
|
+
* }
|
|
267
|
+
* ```
|
|
268
|
+
*/
|
|
60
269
|
metadata?: Partial<ExternalEvalMetadata>;
|
|
270
|
+
/**
|
|
271
|
+
* Evaluation handler function that tests the agent's behavior.
|
|
272
|
+
* Return true if the evaluation passes, false if it fails.
|
|
273
|
+
*
|
|
274
|
+
* @param run - Evaluation run context containing input and metadata
|
|
275
|
+
* @param result - The output from the agent handler
|
|
276
|
+
* @returns Boolean indicating pass/fail, or evaluation result object
|
|
277
|
+
*
|
|
278
|
+
* @example
|
|
279
|
+
* ```typescript
|
|
280
|
+
* handler: async (run, result) => {
|
|
281
|
+
* // Assert that output is positive
|
|
282
|
+
* if (result <= 0) {
|
|
283
|
+
* return false; // Evaluation failed
|
|
284
|
+
* }
|
|
285
|
+
* return true; // Evaluation passed
|
|
286
|
+
* }
|
|
287
|
+
* ```
|
|
288
|
+
*
|
|
289
|
+
* @example
|
|
290
|
+
* ```typescript
|
|
291
|
+
* // With detailed result
|
|
292
|
+
* handler: async (run, result) => {
|
|
293
|
+
* const passed = result.length > 5;
|
|
294
|
+
* return {
|
|
295
|
+
* passed,
|
|
296
|
+
* score: passed ? 1.0 : 0.0,
|
|
297
|
+
* message: passed ? 'Output length is valid' : 'Output too short'
|
|
298
|
+
* };
|
|
299
|
+
* }
|
|
300
|
+
* ```
|
|
301
|
+
*/
|
|
61
302
|
handler: EvalFunction<TInput extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<TInput> : undefined, TOutput extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<TOutput> : undefined>;
|
|
62
|
-
}
|
|
303
|
+
}
|
|
304
|
+
type CreateEvalMethod<TInput extends StandardSchemaV1 | undefined = any, TOutput extends StandardSchemaV1 | undefined = any> = (config: CreateEvalConfig<TInput, TOutput>) => Eval<TInput, TOutput>;
|
|
63
305
|
/**
|
|
64
|
-
*
|
|
306
|
+
* Agent instance type returned by createAgent().
|
|
307
|
+
* Represents a fully configured agent with metadata, handler, lifecycle hooks, and event listeners.
|
|
308
|
+
*
|
|
309
|
+
* @template TInput - Input schema type (StandardSchemaV1 or undefined)
|
|
310
|
+
* @template TOutput - Output schema type (StandardSchemaV1 or undefined)
|
|
311
|
+
* @template TStream - Whether the agent returns a stream (true/false)
|
|
312
|
+
* @template TConfig - Agent-specific configuration type from setup function
|
|
313
|
+
* @template TAppState - Application state type from createApp
|
|
314
|
+
*
|
|
315
|
+
* @example
|
|
316
|
+
* ```typescript
|
|
317
|
+
* const agent = createAgent({
|
|
318
|
+
* metadata: { name: 'My Agent' },
|
|
319
|
+
* schema: { input: z.string(), output: z.number() },
|
|
320
|
+
* handler: async (ctx, input) => input.length
|
|
321
|
+
* });
|
|
322
|
+
*
|
|
323
|
+
* // Access agent properties
|
|
324
|
+
* console.log(agent.metadata.name); // "My Agent"
|
|
325
|
+
*
|
|
326
|
+
* // Add event listeners
|
|
327
|
+
* agent.addEventListener('started', (eventName, agent, ctx) => {
|
|
328
|
+
* console.log('Agent started:', ctx.sessionId);
|
|
329
|
+
* });
|
|
330
|
+
*
|
|
331
|
+
* // Create evals for testing
|
|
332
|
+
* const eval1 = agent.createEval({
|
|
333
|
+
* handler: async (run, result) => {
|
|
334
|
+
* return result > 5; // Assert output is greater than 5
|
|
335
|
+
* }
|
|
336
|
+
* });
|
|
337
|
+
* ```
|
|
65
338
|
*/
|
|
66
|
-
export type Agent<TInput extends StandardSchemaV1 | undefined = any, TOutput extends StandardSchemaV1 | undefined = any, TStream extends boolean = false
|
|
339
|
+
export type Agent<TInput extends StandardSchemaV1 | undefined = any, TOutput extends StandardSchemaV1 | undefined = any, TStream extends boolean = false, TConfig = unknown, TAppState = Record<string, never>> = {
|
|
340
|
+
/**
|
|
341
|
+
* Agent metadata including name, description, id, version, and filename.
|
|
342
|
+
*/
|
|
67
343
|
metadata: AgentMetadata;
|
|
68
|
-
|
|
344
|
+
/**
|
|
345
|
+
* The main handler function that processes agent requests.
|
|
346
|
+
* Receives AgentContext and validated input, returns output or stream.
|
|
347
|
+
*/
|
|
348
|
+
handler: (ctx: AgentContext<any, any, any, TConfig, TAppState>, ...args: any[]) => any | Promise<any>;
|
|
349
|
+
/**
|
|
350
|
+
* Array of evaluation functions created via agent.createEval().
|
|
351
|
+
* Used for testing and validating agent behavior.
|
|
352
|
+
*/
|
|
69
353
|
evals?: Eval[];
|
|
354
|
+
/**
|
|
355
|
+
* Create an evaluation function for testing this agent.
|
|
356
|
+
* Evals can assert correctness of agent input/output during test runs.
|
|
357
|
+
*
|
|
358
|
+
* @param config - Eval configuration
|
|
359
|
+
* @param config.metadata - Optional eval metadata (name, description)
|
|
360
|
+
* @param config.handler - Eval handler function receiving run context and result
|
|
361
|
+
*
|
|
362
|
+
* @example
|
|
363
|
+
* ```typescript
|
|
364
|
+
* const agent = createAgent({
|
|
365
|
+
* schema: { input: z.string(), output: z.number() },
|
|
366
|
+
* handler: async (ctx, input) => input.length
|
|
367
|
+
* });
|
|
368
|
+
*
|
|
369
|
+
* // Create eval to validate output
|
|
370
|
+
* agent.createEval({
|
|
371
|
+
* metadata: { name: 'Check positive output' },
|
|
372
|
+
* handler: async (run, result) => {
|
|
373
|
+
* return result > 0; // Assert output is positive
|
|
374
|
+
* }
|
|
375
|
+
* });
|
|
376
|
+
* ```
|
|
377
|
+
*/
|
|
70
378
|
createEval: CreateEvalMethod<TInput, TOutput>;
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
379
|
+
/**
|
|
380
|
+
* Optional setup function called once when app starts.
|
|
381
|
+
* Returns agent-specific configuration available via ctx.config.
|
|
382
|
+
*/
|
|
383
|
+
setup?: (app: TAppState) => Promise<TConfig> | TConfig;
|
|
384
|
+
/**
|
|
385
|
+
* Optional shutdown function called when app stops.
|
|
386
|
+
* Receives app state and agent config for cleanup.
|
|
387
|
+
*/
|
|
388
|
+
shutdown?: (app: TAppState, config: TConfig) => Promise<void> | void;
|
|
389
|
+
/**
|
|
390
|
+
* Register an event listener for when the agent starts execution.
|
|
391
|
+
*
|
|
392
|
+
* @param eventName - Must be 'started'
|
|
393
|
+
* @param callback - Function called when agent execution begins
|
|
394
|
+
*
|
|
395
|
+
* @example
|
|
396
|
+
* ```typescript
|
|
397
|
+
* agent.addEventListener('started', (eventName, agent, ctx) => {
|
|
398
|
+
* console.log(`${agent.metadata.name} started at ${new Date()}`);
|
|
399
|
+
* });
|
|
400
|
+
* ```
|
|
401
|
+
*/
|
|
402
|
+
addEventListener(eventName: 'started', callback: (eventName: 'started', agent: Agent<TInput, TOutput, TStream, TConfig, TAppState>, context: AgentContext<any, any, any, TConfig, TAppState>) => Promise<void> | void): void;
|
|
403
|
+
/**
|
|
404
|
+
* Register an event listener for when the agent completes successfully.
|
|
405
|
+
*
|
|
406
|
+
* @param eventName - Must be 'completed'
|
|
407
|
+
* @param callback - Function called when agent execution completes
|
|
408
|
+
*
|
|
409
|
+
* @example
|
|
410
|
+
* ```typescript
|
|
411
|
+
* agent.addEventListener('completed', (eventName, agent, ctx) => {
|
|
412
|
+
* console.log(`${agent.metadata.name} completed successfully`);
|
|
413
|
+
* });
|
|
414
|
+
* ```
|
|
415
|
+
*/
|
|
416
|
+
addEventListener(eventName: 'completed', callback: (eventName: 'completed', agent: Agent<TInput, TOutput, TStream, TConfig, TAppState>, context: AgentContext<any, any, any, TConfig, TAppState>) => Promise<void> | void): void;
|
|
417
|
+
/**
|
|
418
|
+
* Register an event listener for when the agent throws an error.
|
|
419
|
+
*
|
|
420
|
+
* @param eventName - Must be 'errored'
|
|
421
|
+
* @param callback - Function called when agent execution fails
|
|
422
|
+
*
|
|
423
|
+
* @example
|
|
424
|
+
* ```typescript
|
|
425
|
+
* agent.addEventListener('errored', (eventName, agent, ctx, error) => {
|
|
426
|
+
* console.error(`${agent.metadata.name} failed:`, error.message);
|
|
427
|
+
* });
|
|
428
|
+
* ```
|
|
429
|
+
*/
|
|
430
|
+
addEventListener(eventName: 'errored', callback: (eventName: 'errored', agent: Agent<TInput, TOutput, TStream, TConfig, TAppState>, context: AgentContext<any, any, any, TConfig, TAppState>, data: Error) => Promise<void> | void): void;
|
|
431
|
+
/**
|
|
432
|
+
* Remove a previously registered 'started' event listener.
|
|
433
|
+
*
|
|
434
|
+
* @param eventName - Must be 'started'
|
|
435
|
+
* @param callback - The callback function to remove
|
|
436
|
+
*/
|
|
437
|
+
removeEventListener(eventName: 'started', callback: (eventName: 'started', agent: Agent<TInput, TOutput, TStream, TConfig, TAppState>, context: AgentContext<any, any, any, TConfig, TAppState>) => Promise<void> | void): void;
|
|
438
|
+
/**
|
|
439
|
+
* Remove a previously registered 'completed' event listener.
|
|
440
|
+
*
|
|
441
|
+
* @param eventName - Must be 'completed'
|
|
442
|
+
* @param callback - The callback function to remove
|
|
443
|
+
*/
|
|
444
|
+
removeEventListener(eventName: 'completed', callback: (eventName: 'completed', agent: Agent<TInput, TOutput, TStream, TConfig, TAppState>, context: AgentContext<any, any, any, TConfig, TAppState>) => Promise<void> | void): void;
|
|
445
|
+
/**
|
|
446
|
+
* Remove a previously registered 'errored' event listener.
|
|
447
|
+
*
|
|
448
|
+
* @param eventName - Must be 'errored'
|
|
449
|
+
* @param callback - The callback function to remove
|
|
450
|
+
*/
|
|
451
|
+
removeEventListener(eventName: 'errored', callback: (eventName: 'errored', agent: Agent<TInput, TOutput, TStream, TConfig, TAppState>, context: AgentContext<any, any, any, TConfig, TAppState>, data: Error) => Promise<void> | void): void;
|
|
77
452
|
} & (TInput extends StandardSchemaV1 ? {
|
|
78
453
|
inputSchema: TInput;
|
|
79
454
|
} : {
|
|
@@ -89,6 +464,105 @@ export type Agent<TInput extends StandardSchemaV1 | undefined = any, TOutput ext
|
|
|
89
464
|
});
|
|
90
465
|
type InferSchemaInput<T> = T extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<T> : never;
|
|
91
466
|
type InferStreamOutput<TOutput, TStream extends boolean> = TStream extends true ? TOutput extends StandardSchemaV1 ? ReadableStream<StandardSchemaV1.InferOutput<TOutput>> : ReadableStream<unknown> : TOutput extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<TOutput> : void;
|
|
467
|
+
type SchemaInput<TSchema> = TSchema extends {
|
|
468
|
+
input: infer I;
|
|
469
|
+
} ? I : undefined;
|
|
470
|
+
type SchemaOutput<TSchema> = TSchema extends {
|
|
471
|
+
output: infer O;
|
|
472
|
+
} ? O : undefined;
|
|
473
|
+
type SchemaStream<TSchema> = TSchema extends {
|
|
474
|
+
stream: infer S;
|
|
475
|
+
} ? S extends boolean ? S : false : false;
|
|
476
|
+
type SchemaHandlerReturn<TSchema> = SchemaStream<TSchema> extends true ? SchemaOutput<TSchema> extends StandardSchemaV1 ? ReadableStream<StandardSchemaV1.InferOutput<SchemaOutput<TSchema>>> : ReadableStream<unknown> : SchemaOutput<TSchema> extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<SchemaOutput<TSchema>> : void;
|
|
477
|
+
type AgentHandlerFromConfig<TSchema, TSetupReturn, TAppState = AppState> = SchemaInput<TSchema> extends infer I ? I extends StandardSchemaV1 ? (ctx: AgentContext<any, any, any, TSetupReturn, TAppState>, input: StandardSchemaV1.InferOutput<I>) => Promise<SchemaHandlerReturn<TSchema>> | SchemaHandlerReturn<TSchema> : (ctx: AgentContext<any, any, any, TSetupReturn, TAppState>) => Promise<SchemaHandlerReturn<TSchema>> | SchemaHandlerReturn<TSchema> : (ctx: AgentContext<any, any, any, TSetupReturn, TAppState>) => Promise<SchemaHandlerReturn<TSchema>> | SchemaHandlerReturn<TSchema>;
|
|
478
|
+
/**
|
|
479
|
+
* Configuration object for creating an agent with automatic type inference.
|
|
480
|
+
*
|
|
481
|
+
* @template TSchema - Schema definition object containing optional input, output, and stream properties
|
|
482
|
+
* @template TConfig - Function type that returns agent-specific configuration from setup
|
|
483
|
+
*/
|
|
484
|
+
export interface CreateAgentConfig<TSchema extends {
|
|
485
|
+
input?: StandardSchemaV1;
|
|
486
|
+
output?: StandardSchemaV1;
|
|
487
|
+
stream?: boolean;
|
|
488
|
+
} | undefined = undefined, TConfig extends (app: AppState) => any = any> {
|
|
489
|
+
/**
|
|
490
|
+
* Optional schema validation using Zod or any StandardSchemaV1 compatible library.
|
|
491
|
+
*
|
|
492
|
+
* @example
|
|
493
|
+
* ```typescript
|
|
494
|
+
* schema: {
|
|
495
|
+
* input: z.object({ name: z.string(), age: z.number() }),
|
|
496
|
+
* output: z.string(),
|
|
497
|
+
* stream: false
|
|
498
|
+
* }
|
|
499
|
+
* ```
|
|
500
|
+
*/
|
|
501
|
+
schema?: TSchema;
|
|
502
|
+
/**
|
|
503
|
+
* Agent metadata visible in the Agentuity platform.
|
|
504
|
+
*
|
|
505
|
+
* @example
|
|
506
|
+
* ```typescript
|
|
507
|
+
* metadata: {
|
|
508
|
+
* name: 'Greeting Agent',
|
|
509
|
+
* description: 'Returns personalized greetings'
|
|
510
|
+
* }
|
|
511
|
+
* ```
|
|
512
|
+
*/
|
|
513
|
+
metadata: ExternalAgentMetadata;
|
|
514
|
+
/**
|
|
515
|
+
* Optional async function called once on app startup to initialize agent-specific resources.
|
|
516
|
+
* The returned value is available in the handler via `ctx.config`.
|
|
517
|
+
*
|
|
518
|
+
* @param app - Application state from createApp setup function
|
|
519
|
+
* @returns Agent-specific configuration object
|
|
520
|
+
*
|
|
521
|
+
* @example
|
|
522
|
+
* ```typescript
|
|
523
|
+
* setup: async (app) => {
|
|
524
|
+
* const cache = new Map();
|
|
525
|
+
* const db = await connectDB();
|
|
526
|
+
* return { cache, db };
|
|
527
|
+
* }
|
|
528
|
+
* ```
|
|
529
|
+
*/
|
|
530
|
+
setup?: TConfig;
|
|
531
|
+
/**
|
|
532
|
+
* The main agent logic that processes requests.
|
|
533
|
+
* Receives AgentContext and validated input (if schema.input is defined), returns output or stream.
|
|
534
|
+
*
|
|
535
|
+
* @param ctx - Agent context with logger, storage, and other runtime services
|
|
536
|
+
* @param input - Validated input (only present if schema.input is defined)
|
|
537
|
+
* @returns Output matching schema.output type, or ReadableStream if schema.stream is true
|
|
538
|
+
*
|
|
539
|
+
* @example
|
|
540
|
+
* ```typescript
|
|
541
|
+
* handler: async (ctx, { name, age }) => {
|
|
542
|
+
* ctx.logger.info(`Processing for ${name}`);
|
|
543
|
+
* await ctx.kv.set('lastUser', name);
|
|
544
|
+
* return `Hello, ${name}! You are ${age} years old.`;
|
|
545
|
+
* }
|
|
546
|
+
* ```
|
|
547
|
+
*/
|
|
548
|
+
handler: AgentHandlerFromConfig<TSchema, TConfig extends (app: AppState) => infer R ? Awaited<R> : undefined, AppState>;
|
|
549
|
+
/**
|
|
550
|
+
* Optional async cleanup function called on app shutdown.
|
|
551
|
+
* Use this to close connections, flush buffers, etc.
|
|
552
|
+
*
|
|
553
|
+
* @param app - Application state from createApp
|
|
554
|
+
* @param config - Agent config returned from setup function
|
|
555
|
+
*
|
|
556
|
+
* @example
|
|
557
|
+
* ```typescript
|
|
558
|
+
* shutdown: async (app, config) => {
|
|
559
|
+
* await config.db.close();
|
|
560
|
+
* config.cache.clear();
|
|
561
|
+
* }
|
|
562
|
+
* ```
|
|
563
|
+
*/
|
|
564
|
+
shutdown?: (app: AppState, config: TConfig extends (app: AppState) => infer R ? Awaited<R> : undefined) => Promise<void> | void;
|
|
565
|
+
}
|
|
92
566
|
export interface AgentRunner<TInput extends StandardSchemaV1 | undefined = any, TOutput extends StandardSchemaV1 | undefined = any, TStream extends boolean = false> {
|
|
93
567
|
metadata: AgentMetadata;
|
|
94
568
|
run: undefined extends TInput ? () => Promise<InferStreamOutput<Exclude<TOutput, undefined>, TStream>> : (input: InferSchemaInput<Exclude<TInput, undefined>>) => Promise<InferStreamOutput<Exclude<TOutput, undefined>, TStream>>;
|
|
@@ -105,21 +579,175 @@ export type AgentName = keyof AgentRegistry extends never ? string : keyof Agent
|
|
|
105
579
|
*/
|
|
106
580
|
export interface AgentRegistry {
|
|
107
581
|
}
|
|
108
|
-
export declare const registerAgent: (name: AgentName, agent: Agent) => void;
|
|
109
|
-
export declare
|
|
582
|
+
export declare const registerAgent: (name: AgentName, agent: Agent<any, any, any, any, any>) => void;
|
|
583
|
+
export declare const setAgentConfig: (name: AgentName, config: unknown) => void;
|
|
584
|
+
export declare const getAgentConfig: (name: AgentName) => unknown;
|
|
585
|
+
/**
|
|
586
|
+
* Configuration object for creating an agent with explicit type parameters.
|
|
587
|
+
*
|
|
588
|
+
* @template TInput - Input schema type (StandardSchemaV1 or undefined)
|
|
589
|
+
* @template TOutput - Output schema type (StandardSchemaV1 or undefined)
|
|
590
|
+
* @template TStream - Whether agent returns a stream (true/false)
|
|
591
|
+
* @template TConfig - Type returned by setup function
|
|
592
|
+
* @template TAppState - Custom app state type from createApp
|
|
593
|
+
*/
|
|
594
|
+
export interface CreateAgentConfigExplicit<TInput extends StandardSchemaV1 | undefined = undefined, TOutput extends StandardSchemaV1 | undefined = undefined, TStream extends boolean = false, TConfig = unknown, TAppState = AppState> {
|
|
595
|
+
/**
|
|
596
|
+
* Optional schema validation.
|
|
597
|
+
*
|
|
598
|
+
* @example
|
|
599
|
+
* ```typescript
|
|
600
|
+
* schema: {
|
|
601
|
+
* input: z.object({ name: z.string() }),
|
|
602
|
+
* output: z.string(),
|
|
603
|
+
* stream: false
|
|
604
|
+
* }
|
|
605
|
+
* ```
|
|
606
|
+
*/
|
|
110
607
|
schema?: {
|
|
608
|
+
/** Input validation schema */
|
|
111
609
|
input?: TInput;
|
|
610
|
+
/** Output validation schema */
|
|
112
611
|
output?: TOutput;
|
|
612
|
+
/** Whether the agent returns a ReadableStream */
|
|
113
613
|
stream?: TStream;
|
|
114
614
|
};
|
|
615
|
+
/**
|
|
616
|
+
* Agent metadata.
|
|
617
|
+
*
|
|
618
|
+
* @example
|
|
619
|
+
* ```typescript
|
|
620
|
+
* metadata: {
|
|
621
|
+
* name: 'My Agent',
|
|
622
|
+
* description: 'Does something useful'
|
|
623
|
+
* }
|
|
624
|
+
* ```
|
|
625
|
+
*/
|
|
115
626
|
metadata: ExternalAgentMetadata;
|
|
116
|
-
|
|
117
|
-
|
|
627
|
+
/**
|
|
628
|
+
* Optional setup function receiving app state, returns agent config.
|
|
629
|
+
* The returned value is available in the handler via `ctx.config`.
|
|
630
|
+
*
|
|
631
|
+
* @param app - Application state from createApp
|
|
632
|
+
* @returns Agent-specific configuration
|
|
633
|
+
*
|
|
634
|
+
* @example
|
|
635
|
+
* ```typescript
|
|
636
|
+
* setup: async (app) => ({ cache: new Map() })
|
|
637
|
+
* ```
|
|
638
|
+
*/
|
|
639
|
+
setup?: (app: TAppState) => Promise<TConfig> | TConfig;
|
|
640
|
+
/**
|
|
641
|
+
* Optional cleanup function called on app shutdown.
|
|
642
|
+
*
|
|
643
|
+
* @param app - Application state from createApp
|
|
644
|
+
* @param config - Agent config returned from setup
|
|
645
|
+
*
|
|
646
|
+
* @example
|
|
647
|
+
* ```typescript
|
|
648
|
+
* shutdown: async (app, config) => {
|
|
649
|
+
* config.cache.clear();
|
|
650
|
+
* }
|
|
651
|
+
* ```
|
|
652
|
+
*/
|
|
653
|
+
shutdown?: (app: TAppState, config: TConfig) => Promise<void> | void;
|
|
654
|
+
/**
|
|
655
|
+
* Agent handler function.
|
|
656
|
+
* Type is automatically inferred based on schema definitions.
|
|
657
|
+
*
|
|
658
|
+
* @param ctx - Agent context
|
|
659
|
+
* @param input - Validated input (only present if schema.input is defined)
|
|
660
|
+
* @returns Output or ReadableStream based on schema
|
|
661
|
+
*
|
|
662
|
+
* @example
|
|
663
|
+
* ```typescript
|
|
664
|
+
* handler: async (ctx, input) => {
|
|
665
|
+
* return `Hello, ${input.name}!`;
|
|
666
|
+
* }
|
|
667
|
+
* ```
|
|
668
|
+
*/
|
|
669
|
+
handler: TInput extends StandardSchemaV1 ? TStream extends true ? TOutput extends StandardSchemaV1 ? (c: AgentContext<any, any, any, TConfig, TAppState>, input: StandardSchemaV1.InferOutput<TInput>) => Promise<ReadableStream<StandardSchemaV1.InferOutput<TOutput>>> | ReadableStream<StandardSchemaV1.InferOutput<TOutput>> : (c: AgentContext<any, any, any, TConfig, TAppState>, input: StandardSchemaV1.InferOutput<TInput>) => Promise<ReadableStream<unknown>> | ReadableStream<unknown> : TOutput extends StandardSchemaV1 ? (c: AgentContext<any, any, any, TConfig, TAppState>, input: StandardSchemaV1.InferOutput<TInput>) => Promise<StandardSchemaV1.InferOutput<TOutput>> | StandardSchemaV1.InferOutput<TOutput> : (c: AgentContext<any, any, any, TConfig, TAppState>, input: StandardSchemaV1.InferOutput<TInput>) => Promise<void> | void : TStream extends true ? TOutput extends StandardSchemaV1 ? (c: AgentContext<any, any, any, TConfig, TAppState>) => Promise<ReadableStream<StandardSchemaV1.InferOutput<TOutput>>> | ReadableStream<StandardSchemaV1.InferOutput<TOutput>> : (c: AgentContext<any, any, any, TConfig, TAppState>) => Promise<ReadableStream<unknown>> | ReadableStream<unknown> : TOutput extends StandardSchemaV1 ? (c: AgentContext<any, any, any, TConfig, TAppState>) => Promise<StandardSchemaV1.InferOutput<TOutput>> | StandardSchemaV1.InferOutput<TOutput> : (c: AgentContext<any, any, any, TConfig, TAppState>) => Promise<void> | void;
|
|
670
|
+
}
|
|
671
|
+
/**
|
|
672
|
+
* Creates an agent with schema validation and lifecycle hooks.
|
|
673
|
+
*
|
|
674
|
+
* This is the recommended way to create agents with automatic type inference from schemas.
|
|
675
|
+
*
|
|
676
|
+
* @template TSchema - Schema definition object containing optional input, output, and stream properties
|
|
677
|
+
* @template TConfig - Function type that returns agent-specific configuration from setup
|
|
678
|
+
*
|
|
679
|
+
* @param config - Agent configuration object
|
|
680
|
+
*
|
|
681
|
+
* @returns Agent instance that can be registered with the runtime
|
|
682
|
+
*
|
|
683
|
+
* @example
|
|
684
|
+
* ```typescript
|
|
685
|
+
* const agent = createAgent({
|
|
686
|
+
* metadata: {
|
|
687
|
+
* name: 'Greeting Agent',
|
|
688
|
+
* description: 'Returns personalized greetings'
|
|
689
|
+
* },
|
|
690
|
+
* schema: {
|
|
691
|
+
* input: z.object({ name: z.string(), age: z.number() }),
|
|
692
|
+
* output: z.string()
|
|
693
|
+
* },
|
|
694
|
+
* handler: async (ctx, { name, age }) => {
|
|
695
|
+
* ctx.logger.info(`Processing greeting for ${name}`);
|
|
696
|
+
* return `Hello, ${name}! You are ${age} years old.`;
|
|
697
|
+
* }
|
|
698
|
+
* });
|
|
699
|
+
* ```
|
|
700
|
+
*/
|
|
701
|
+
export declare function createAgent<TSchema extends {
|
|
702
|
+
input?: StandardSchemaV1;
|
|
703
|
+
output?: StandardSchemaV1;
|
|
704
|
+
stream?: boolean;
|
|
705
|
+
} | undefined = undefined, TConfig extends (app: AppState) => any = any>(config: CreateAgentConfig<TSchema, TConfig>): Agent<SchemaInput<TSchema>, SchemaOutput<TSchema>, SchemaStream<TSchema>, TConfig extends (app: AppState) => infer R ? Awaited<R> : undefined, AppState>;
|
|
706
|
+
/**
|
|
707
|
+
* Creates an agent with explicit generic type parameters.
|
|
708
|
+
*
|
|
709
|
+
* Use this overload when you need explicit control over types or working with custom app state.
|
|
710
|
+
*
|
|
711
|
+
* @template TInput - Input schema type (StandardSchemaV1 or undefined)
|
|
712
|
+
* @template TOutput - Output schema type (StandardSchemaV1 or undefined)
|
|
713
|
+
* @template TStream - Whether agent returns a stream (true/false)
|
|
714
|
+
* @template TConfig - Type returned by setup function
|
|
715
|
+
* @template TAppState - Custom app state type from createApp
|
|
716
|
+
*
|
|
717
|
+
* @param config - Agent configuration object
|
|
718
|
+
*
|
|
719
|
+
* @returns Agent instance with explicit types
|
|
720
|
+
*
|
|
721
|
+
* @example
|
|
722
|
+
* ```typescript
|
|
723
|
+
* interface MyAppState { db: Database }
|
|
724
|
+
* interface MyConfig { cache: Map<string, any> }
|
|
725
|
+
*
|
|
726
|
+
* const agent = createAgent<
|
|
727
|
+
* z.ZodObject<any>, // TInput
|
|
728
|
+
* z.ZodString, // TOutput
|
|
729
|
+
* false, // TStream
|
|
730
|
+
* MyConfig, // TConfig
|
|
731
|
+
* MyAppState // TAppState
|
|
732
|
+
* >({
|
|
733
|
+
* metadata: { name: 'Custom Agent' },
|
|
734
|
+
* setup: async (app) => ({ cache: new Map() }),
|
|
735
|
+
* handler: async (ctx, input) => {
|
|
736
|
+
* const db = ctx.app.db;
|
|
737
|
+
* const cache = ctx.config.cache;
|
|
738
|
+
* return 'result';
|
|
739
|
+
* }
|
|
740
|
+
* });
|
|
741
|
+
* ```
|
|
742
|
+
*/
|
|
743
|
+
export declare function createAgent<TInput extends StandardSchemaV1 | undefined = undefined, TOutput extends StandardSchemaV1 | undefined = undefined, TStream extends boolean = false, TConfig = unknown, TAppState = AppState>(config: CreateAgentConfigExplicit<TInput, TOutput, TStream, TConfig, TAppState>): Agent<TInput, TOutput, TStream, TConfig, TAppState>;
|
|
118
744
|
/**
|
|
119
745
|
* Populate the agents object with all registered agents
|
|
120
746
|
*/
|
|
121
747
|
export declare const populateAgentsRegistry: (ctx: Context) => any;
|
|
122
|
-
export declare const createAgentMiddleware: (agentName: AgentName) => MiddlewareHandler;
|
|
123
|
-
export declare const getAgents: () => Map<string, Agent<any, any,
|
|
748
|
+
export declare const createAgentMiddleware: (agentName: AgentName | "") => MiddlewareHandler;
|
|
749
|
+
export declare const getAgents: () => Map<string, Agent<any, any, any, any, any>>;
|
|
750
|
+
export declare const runAgentSetups: (appState: AppState) => Promise<void>;
|
|
751
|
+
export declare const runAgentShutdowns: (appState: AppState) => Promise<void>;
|
|
124
752
|
export {};
|
|
125
753
|
//# sourceMappingURL=agent.d.ts.map
|