@agentuity/runtime 0.0.68 → 0.0.70
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/AGENTS.md +130 -10
- package/README.md +6 -8
- package/dist/_config.d.ts +16 -0
- package/dist/_config.d.ts.map +1 -1
- package/dist/_config.js +16 -0
- package/dist/_config.js.map +1 -1
- package/dist/_context.d.ts +17 -15
- package/dist/_context.d.ts.map +1 -1
- package/dist/_context.js +17 -8
- package/dist/_context.js.map +1 -1
- package/dist/_server.d.ts.map +1 -1
- package/dist/_server.js +27 -14
- package/dist/_server.js.map +1 -1
- package/dist/_services.d.ts.map +1 -1
- package/dist/_services.js +2 -29
- package/dist/_services.js.map +1 -1
- package/dist/_validation.d.ts +89 -0
- package/dist/_validation.d.ts.map +1 -0
- package/dist/_validation.js +29 -0
- package/dist/_validation.js.map +1 -0
- package/dist/agent.d.ts +476 -104
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +293 -97
- package/dist/agent.js.map +1 -1
- package/dist/app.d.ts +6 -18
- package/dist/app.d.ts.map +1 -1
- package/dist/app.js +1 -1
- package/dist/app.js.map +1 -1
- package/dist/eval.d.ts +4 -4
- package/dist/eval.d.ts.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/otel/config.d.ts.map +1 -1
- package/dist/otel/config.js +5 -2
- package/dist/otel/config.js.map +1 -1
- package/dist/otel/console.d.ts +10 -6
- package/dist/otel/console.d.ts.map +1 -1
- package/dist/otel/console.js +31 -13
- package/dist/otel/console.js.map +1 -1
- package/dist/otel/logger.d.ts.map +1 -1
- package/dist/otel/logger.js +0 -19
- package/dist/otel/logger.js.map +1 -1
- package/dist/otel/otel.d.ts +2 -1
- package/dist/otel/otel.d.ts.map +1 -1
- package/dist/otel/otel.js +28 -15
- package/dist/otel/otel.js.map +1 -1
- package/dist/services/local/_db.d.ts.map +1 -1
- package/dist/services/local/_db.js +1 -22
- package/dist/services/local/_db.js.map +1 -1
- package/dist/services/local/_router.d.ts.map +1 -1
- package/dist/services/local/_router.js +0 -32
- package/dist/services/local/_router.js.map +1 -1
- package/dist/services/local/index.d.ts +0 -1
- package/dist/services/local/index.d.ts.map +1 -1
- package/dist/services/local/index.js +0 -1
- package/dist/services/local/index.js.map +1 -1
- package/dist/session.d.ts +18 -2
- package/dist/session.d.ts.map +1 -1
- package/dist/session.js +6 -0
- package/dist/session.js.map +1 -1
- package/dist/validator.d.ts +140 -0
- package/dist/validator.d.ts.map +1 -0
- package/dist/validator.js +146 -0
- package/dist/validator.js.map +1 -0
- package/dist/workbench.d.ts.map +1 -1
- package/dist/workbench.js +38 -29
- package/dist/workbench.js.map +1 -1
- package/package.json +6 -6
- package/src/_config.ts +19 -0
- package/src/_context.ts +25 -31
- package/src/_server.ts +30 -14
- package/src/_services.ts +0 -28
- package/src/_validation.ts +119 -0
- package/src/agent.ts +872 -272
- package/src/app.ts +5 -18
- package/src/eval.ts +6 -6
- package/src/index.ts +3 -1
- package/src/otel/config.ts +5 -2
- package/src/otel/console.ts +34 -20
- package/src/otel/logger.ts +0 -18
- package/src/otel/otel.ts +43 -29
- package/src/services/local/_db.ts +1 -27
- package/src/services/local/_router.ts +0 -46
- package/src/services/local/index.ts +0 -1
- package/src/session.ts +22 -2
- package/src/validator.ts +277 -0
- package/src/workbench.ts +38 -32
- package/dist/services/local/objectstore.d.ts +0 -19
- package/dist/services/local/objectstore.d.ts.map +0 -1
- package/dist/services/local/objectstore.js +0 -117
- package/dist/services/local/objectstore.js.map +0 -1
- package/src/services/local/objectstore.ts +0 -177
package/dist/agent.d.ts
CHANGED
|
@@ -1,30 +1,40 @@
|
|
|
1
|
-
import { type KeyValueStorage, type
|
|
1
|
+
import { type KeyValueStorage, type StandardSchemaV1, type StreamStorage, type VectorStorage, type InferOutput } from '@agentuity/core';
|
|
2
2
|
import { type Tracer } from '@opentelemetry/api';
|
|
3
3
|
import type { Context, MiddlewareHandler } from 'hono';
|
|
4
|
+
import type { Handler } from 'hono/types';
|
|
5
|
+
import { AGENT_RUNTIME } from './_config';
|
|
4
6
|
import type { Logger } from './logger';
|
|
5
|
-
import type { Eval, EvalFunction
|
|
7
|
+
import type { Eval, EvalFunction } from './eval';
|
|
6
8
|
import type { Thread, Session } from './session';
|
|
7
9
|
import type { AppState } from './index';
|
|
8
10
|
export type AgentEventName = 'started' | 'completed' | 'errored';
|
|
9
11
|
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);
|
|
12
|
+
/**
|
|
13
|
+
* Runtime state container for agents and event listeners.
|
|
14
|
+
* Isolates global state into context for better testing.
|
|
15
|
+
*/
|
|
16
|
+
export interface AgentRuntimeState {
|
|
17
|
+
agents: Map<string, Agent<any, any, any, any, any>>;
|
|
18
|
+
agentConfigs: Map<string, unknown>;
|
|
19
|
+
agentEventListeners: WeakMap<Agent<any, any, any, any, any>, Map<AgentEventName, Set<AgentEventCallback<any>>>>;
|
|
20
|
+
}
|
|
10
21
|
/**
|
|
11
22
|
* Context object passed to every agent handler providing access to runtime services and state.
|
|
12
23
|
*
|
|
13
24
|
* @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
25
|
* @template TConfig - Agent-specific configuration type from setup function
|
|
17
26
|
* @template TAppState - Application-wide state type from createApp
|
|
18
27
|
*
|
|
19
28
|
* @example
|
|
20
29
|
* ```typescript
|
|
21
|
-
* const agent = createAgent({
|
|
30
|
+
* const agent = createAgent('my-agent', {
|
|
22
31
|
* handler: async (ctx, input) => {
|
|
23
32
|
* // Logging
|
|
24
33
|
* ctx.logger.info('Processing request', { input });
|
|
25
34
|
*
|
|
26
|
-
* // Call another agent
|
|
27
|
-
*
|
|
35
|
+
* // Call another agent (import it directly)
|
|
36
|
+
* import otherAgent from './other-agent';
|
|
37
|
+
* const result = await otherAgent.run({ data: input });
|
|
28
38
|
*
|
|
29
39
|
* // Store data
|
|
30
40
|
* await ctx.kv.set('key', { value: result });
|
|
@@ -42,7 +52,14 @@ export type AgentEventCallback<TAgent extends Agent<any, any, any>> = ((eventNam
|
|
|
42
52
|
* });
|
|
43
53
|
* ```
|
|
44
54
|
*/
|
|
45
|
-
export interface AgentContext<
|
|
55
|
+
export interface AgentContext<_TAgentRegistry extends AgentRegistry = AgentRegistry, TConfig = unknown, TAppState = Record<string, never>> {
|
|
56
|
+
/**
|
|
57
|
+
* Internal runtime state (agents, configs, event listeners).
|
|
58
|
+
* Stored with Symbol key to prevent accidental access.
|
|
59
|
+
* Use getAgentRuntime(ctx) to access.
|
|
60
|
+
* @internal
|
|
61
|
+
*/
|
|
62
|
+
[AGENT_RUNTIME]: AgentRuntimeState;
|
|
46
63
|
/**
|
|
47
64
|
* Schedule a background task that continues after the response is sent.
|
|
48
65
|
* Useful for cleanup, logging, or async operations that don't block the response.
|
|
@@ -58,30 +75,6 @@ export interface AgentContext<TAgentRegistry extends AgentRegistry = AgentRegist
|
|
|
58
75
|
* ```
|
|
59
76
|
*/
|
|
60
77
|
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
|
-
*/
|
|
71
|
-
agent: TAgentRegistry;
|
|
72
|
-
/**
|
|
73
|
-
* Information about the currently executing agent.
|
|
74
|
-
*/
|
|
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
|
-
*/
|
|
80
|
-
parent: TParent;
|
|
81
|
-
/**
|
|
82
|
-
* Name of the current agent being executed.
|
|
83
|
-
*/
|
|
84
|
-
agentName: AgentName;
|
|
85
78
|
/**
|
|
86
79
|
* Structured logger with OpenTelemetry integration.
|
|
87
80
|
* Logs are automatically correlated with traces.
|
|
@@ -126,18 +119,6 @@ export interface AgentContext<TAgentRegistry extends AgentRegistry = AgentRegist
|
|
|
126
119
|
* ```
|
|
127
120
|
*/
|
|
128
121
|
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
|
-
*/
|
|
140
|
-
objectstore: ObjectStorage;
|
|
141
122
|
/**
|
|
142
123
|
* Stream storage for real-time data streams and logs.
|
|
143
124
|
*
|
|
@@ -217,13 +198,13 @@ export interface AgentContext<TAgentRegistry extends AgentRegistry = AgentRegist
|
|
|
217
198
|
}
|
|
218
199
|
type InternalAgentMetadata = {
|
|
219
200
|
/**
|
|
220
|
-
* the unique
|
|
201
|
+
* the unique name for the agent (user-provided).
|
|
221
202
|
*/
|
|
222
|
-
|
|
203
|
+
name: string;
|
|
223
204
|
/**
|
|
224
|
-
* the unique identifier for this project
|
|
205
|
+
* the unique identifier for this project, agent and deployment.
|
|
225
206
|
*/
|
|
226
|
-
|
|
207
|
+
id: string;
|
|
227
208
|
/**
|
|
228
209
|
* the unique identifier for this agent across multiple deployments
|
|
229
210
|
*/
|
|
@@ -246,10 +227,6 @@ type InternalAgentMetadata = {
|
|
|
246
227
|
outputSchemaCode?: string;
|
|
247
228
|
};
|
|
248
229
|
type ExternalAgentMetadata = {
|
|
249
|
-
/**
|
|
250
|
-
* the human readable name for the agent.
|
|
251
|
-
*/
|
|
252
|
-
name: string;
|
|
253
230
|
/**
|
|
254
231
|
* the human readable description for the agent
|
|
255
232
|
*/
|
|
@@ -259,22 +236,19 @@ type AgentMetadata = InternalAgentMetadata & ExternalAgentMetadata;
|
|
|
259
236
|
/**
|
|
260
237
|
* Configuration object for creating an agent evaluation function.
|
|
261
238
|
*
|
|
262
|
-
* @template TInput - Input schema type from the
|
|
263
|
-
* @template TOutput - Output schema type from the
|
|
239
|
+
* @template TInput - Input schema type from the agent
|
|
240
|
+
* @template TOutput - Output schema type from the agent
|
|
264
241
|
*/
|
|
265
242
|
export interface CreateEvalConfig<TInput extends StandardSchemaV1 | undefined = any, TOutput extends StandardSchemaV1 | undefined = any> {
|
|
266
243
|
/**
|
|
267
|
-
* Optional
|
|
244
|
+
* Optional description of what this evaluation does.
|
|
268
245
|
*
|
|
269
246
|
* @example
|
|
270
247
|
* ```typescript
|
|
271
|
-
*
|
|
272
|
-
* name: 'Validate positive output',
|
|
273
|
-
* description: 'Ensures output is greater than zero'
|
|
274
|
-
* }
|
|
248
|
+
* description: 'Ensures output is greater than zero'
|
|
275
249
|
* ```
|
|
276
250
|
*/
|
|
277
|
-
|
|
251
|
+
description?: string;
|
|
278
252
|
/**
|
|
279
253
|
* Evaluation handler function that tests the agent's behavior.
|
|
280
254
|
* Return true if the evaluation passes, false if it fails.
|
|
@@ -307,9 +281,128 @@ export interface CreateEvalConfig<TInput extends StandardSchemaV1 | undefined =
|
|
|
307
281
|
* }
|
|
308
282
|
* ```
|
|
309
283
|
*/
|
|
310
|
-
handler: EvalFunction<TInput extends StandardSchemaV1 ?
|
|
284
|
+
handler: EvalFunction<TInput extends StandardSchemaV1 ? InferOutput<TInput> : undefined, TOutput extends StandardSchemaV1 ? InferOutput<TOutput> : undefined>;
|
|
285
|
+
}
|
|
286
|
+
type CreateEvalMethod<TInput extends StandardSchemaV1 | undefined = any, TOutput extends StandardSchemaV1 | undefined = any> = (name: string, config: CreateEvalConfig<TInput, TOutput>) => Eval<TInput, TOutput>;
|
|
287
|
+
/**
|
|
288
|
+
* Validator function type with method overloads for different validation scenarios.
|
|
289
|
+
* Provides type-safe validation middleware that integrates with Hono's type system.
|
|
290
|
+
*
|
|
291
|
+
* This validator automatically validates incoming JSON request bodies using StandardSchema-compatible
|
|
292
|
+
* schemas (Zod, Valibot, ArkType, etc.) and provides full TypeScript type inference for validated data
|
|
293
|
+
* accessible via `c.req.valid('json')`.
|
|
294
|
+
*
|
|
295
|
+
* The validator returns 400 Bad Request with validation error details if validation fails.
|
|
296
|
+
*
|
|
297
|
+
* @template TInput - Agent's input schema type (StandardSchemaV1 or undefined)
|
|
298
|
+
* @template _TOutput - Agent's output schema type (reserved for future output validation)
|
|
299
|
+
*
|
|
300
|
+
* @example Basic usage with agent's schema
|
|
301
|
+
* ```typescript
|
|
302
|
+
* router.post('/', agent.validator(), async (c) => {
|
|
303
|
+
* const data = c.req.valid('json'); // Fully typed from agent's input schema
|
|
304
|
+
* return c.json(data);
|
|
305
|
+
* });
|
|
306
|
+
* ```
|
|
307
|
+
*
|
|
308
|
+
* @example Override with custom input schema
|
|
309
|
+
* ```typescript
|
|
310
|
+
* router.post('/custom', agent.validator({ input: z.object({ id: z.string() }) }), async (c) => {
|
|
311
|
+
* const data = c.req.valid('json'); // Typed as { id: string }
|
|
312
|
+
* return c.json(data);
|
|
313
|
+
* });
|
|
314
|
+
* ```
|
|
315
|
+
*/
|
|
316
|
+
export interface AgentValidator<TInput extends StandardSchemaV1 | undefined, _TOutput extends StandardSchemaV1 | undefined> {
|
|
317
|
+
/**
|
|
318
|
+
* Validates using the agent's input schema (no override).
|
|
319
|
+
* Returns Hono middleware handler that validates JSON request body.
|
|
320
|
+
*
|
|
321
|
+
* @returns Middleware handler with type inference for validated data
|
|
322
|
+
*
|
|
323
|
+
* @example
|
|
324
|
+
* ```typescript
|
|
325
|
+
* // Agent has schema: { input: z.object({ name: z.string() }) }
|
|
326
|
+
* router.post('/', agent.validator(), async (c) => {
|
|
327
|
+
* const data = c.req.valid('json'); // { name: string }
|
|
328
|
+
* return c.json({ received: data.name });
|
|
329
|
+
* });
|
|
330
|
+
* ```
|
|
331
|
+
*/
|
|
332
|
+
(): TInput extends StandardSchemaV1 ? Handler<any, any, {
|
|
333
|
+
in: {};
|
|
334
|
+
out: {
|
|
335
|
+
json: InferOutput<TInput>;
|
|
336
|
+
};
|
|
337
|
+
}> : Handler<any, any, any>;
|
|
338
|
+
/**
|
|
339
|
+
* Output-only validation override.
|
|
340
|
+
* Validates only the response body (no input validation).
|
|
341
|
+
*
|
|
342
|
+
* Useful for GET routes or routes where input validation is handled elsewhere.
|
|
343
|
+
* The middleware validates the JSON response body and throws 500 Internal Server Error
|
|
344
|
+
* if validation fails.
|
|
345
|
+
*
|
|
346
|
+
* @template TOverrideOutput - Custom output schema type
|
|
347
|
+
* @param override - Object containing output schema
|
|
348
|
+
* @returns Middleware handler that validates response output
|
|
349
|
+
*
|
|
350
|
+
* @example GET route with output validation
|
|
351
|
+
* ```typescript
|
|
352
|
+
* router.get('/', agent.validator({ output: z.array(z.object({ id: z.string() })) }), async (c) => {
|
|
353
|
+
* // Returns array of objects - validated against schema
|
|
354
|
+
* return c.json([{ id: '123' }, { id: '456' }]);
|
|
355
|
+
* });
|
|
356
|
+
* ```
|
|
357
|
+
*/
|
|
358
|
+
<TOverrideOutput extends StandardSchemaV1>(override: {
|
|
359
|
+
output: TOverrideOutput;
|
|
360
|
+
}): Handler<any, any, {
|
|
361
|
+
in: {};
|
|
362
|
+
out: {
|
|
363
|
+
json: InferOutput<TOverrideOutput>;
|
|
364
|
+
};
|
|
365
|
+
}>;
|
|
366
|
+
/**
|
|
367
|
+
* Validates with custom input and optional output schemas (POST/PUT/PATCH/DELETE).
|
|
368
|
+
* Overrides the agent's schema for this specific route.
|
|
369
|
+
*
|
|
370
|
+
* @template TOverrideInput - Custom input schema type
|
|
371
|
+
* @template TOverrideOutput - Optional custom output schema type
|
|
372
|
+
* @param override - Object containing input (required) and output (optional) schemas
|
|
373
|
+
* @returns Middleware handler with type inference from custom schemas
|
|
374
|
+
*
|
|
375
|
+
* @example Custom input schema
|
|
376
|
+
* ```typescript
|
|
377
|
+
* router.post('/users', agent.validator({
|
|
378
|
+
* input: z.object({ email: z.string().email(), name: z.string() })
|
|
379
|
+
* }), async (c) => {
|
|
380
|
+
* const data = c.req.valid('json'); // { email: string, name: string }
|
|
381
|
+
* return c.json({ id: '123', ...data });
|
|
382
|
+
* });
|
|
383
|
+
* ```
|
|
384
|
+
*
|
|
385
|
+
* @example Custom input and output schemas
|
|
386
|
+
* ```typescript
|
|
387
|
+
* router.post('/convert', agent.validator({
|
|
388
|
+
* input: z.string(),
|
|
389
|
+
* output: z.number()
|
|
390
|
+
* }), async (c) => {
|
|
391
|
+
* const data = c.req.valid('json'); // string
|
|
392
|
+
* return c.json(123);
|
|
393
|
+
* });
|
|
394
|
+
* ```
|
|
395
|
+
*/
|
|
396
|
+
<TOverrideInput extends StandardSchemaV1, TOverrideOutput extends StandardSchemaV1 | undefined = undefined>(override: {
|
|
397
|
+
input: TOverrideInput;
|
|
398
|
+
output?: TOverrideOutput;
|
|
399
|
+
}): Handler<any, any, {
|
|
400
|
+
in: {};
|
|
401
|
+
out: {
|
|
402
|
+
json: InferOutput<TOverrideInput>;
|
|
403
|
+
};
|
|
404
|
+
}>;
|
|
311
405
|
}
|
|
312
|
-
type CreateEvalMethod<TInput extends StandardSchemaV1 | undefined = any, TOutput extends StandardSchemaV1 | undefined = any> = (config: CreateEvalConfig<TInput, TOutput>) => Eval<TInput, TOutput>;
|
|
313
406
|
/**
|
|
314
407
|
* Agent instance type returned by createAgent().
|
|
315
408
|
* Represents a fully configured agent with metadata, handler, lifecycle hooks, and event listeners.
|
|
@@ -337,7 +430,8 @@ type CreateEvalMethod<TInput extends StandardSchemaV1 | undefined = any, TOutput
|
|
|
337
430
|
* });
|
|
338
431
|
*
|
|
339
432
|
* // Create evals for testing
|
|
340
|
-
* const eval1 = agent.createEval({
|
|
433
|
+
* const eval1 = agent.createEval('check-positive', {
|
|
434
|
+
* description: 'Ensures result is greater than 5',
|
|
341
435
|
* handler: async (run, result) => {
|
|
342
436
|
* return result > 5; // Assert output is greater than 5
|
|
343
437
|
* }
|
|
@@ -351,9 +445,78 @@ export type Agent<TInput extends StandardSchemaV1 | undefined = any, TOutput ext
|
|
|
351
445
|
metadata: AgentMetadata;
|
|
352
446
|
/**
|
|
353
447
|
* The main handler function that processes agent requests.
|
|
354
|
-
*
|
|
448
|
+
* Gets AgentContext from AsyncLocalStorage, receives validated input, returns output or stream.
|
|
355
449
|
*/
|
|
356
|
-
handler: (
|
|
450
|
+
handler: (...args: any[]) => any | Promise<any>;
|
|
451
|
+
/**
|
|
452
|
+
* Creates a type-safe validation middleware for routes using StandardSchema validation.
|
|
453
|
+
*
|
|
454
|
+
* This method validates incoming JSON request bodies against the agent's **input schema**
|
|
455
|
+
* and optionally validates outgoing JSON responses against the **output schema**.
|
|
456
|
+
* Provides full TypeScript type inference for validated input data accessible via `c.req.valid('json')`.
|
|
457
|
+
*
|
|
458
|
+
* **Validation behavior:**
|
|
459
|
+
* - **Input**: Validates request JSON body, returns 400 Bad Request on failure
|
|
460
|
+
* - **Output**: Validates response JSON body (if output schema provided), throws 500 on failure
|
|
461
|
+
* - Passes validated input data to handler via `c.req.valid('json')`
|
|
462
|
+
* - Full TypeScript type inference for validated input data
|
|
463
|
+
*
|
|
464
|
+
* **Supported schema libraries:**
|
|
465
|
+
* - Zod (z.object, z.string, etc.)
|
|
466
|
+
* - Valibot (v.object, v.string, etc.)
|
|
467
|
+
* - ArkType (type({ ... }))
|
|
468
|
+
* - Any StandardSchema-compatible library
|
|
469
|
+
*
|
|
470
|
+
* **Method overloads:**
|
|
471
|
+
* - `agent.validator()` - Validates using agent's input/output schemas
|
|
472
|
+
* - `agent.validator({ output: schema })` - Output-only validation (no input validation)
|
|
473
|
+
* - `agent.validator({ input: schema })` - Custom input schema override
|
|
474
|
+
* - `agent.validator({ input: schema, output: schema })` - Both input and output validated
|
|
475
|
+
*
|
|
476
|
+
* @returns Hono middleware handler with proper type inference
|
|
477
|
+
*
|
|
478
|
+
* @example Automatic validation using agent's schema
|
|
479
|
+
* ```typescript
|
|
480
|
+
* // Agent defined with: schema: { input: z.object({ name: z.string(), age: z.number() }) }
|
|
481
|
+
* router.post('/', agent.validator(), async (c) => {
|
|
482
|
+
* const data = c.req.valid('json'); // Fully typed: { name: string, age: number }
|
|
483
|
+
* return c.json({ greeting: `Hello ${data.name}, age ${data.age}` });
|
|
484
|
+
* });
|
|
485
|
+
* ```
|
|
486
|
+
*
|
|
487
|
+
* @example Override with custom schema per-route
|
|
488
|
+
* ```typescript
|
|
489
|
+
* router.post('/email', agent.validator({
|
|
490
|
+
* input: z.object({ email: z.string().email() })
|
|
491
|
+
* }), async (c) => {
|
|
492
|
+
* const data = c.req.valid('json'); // Typed as { email: string }
|
|
493
|
+
* return c.json({ sent: data.email });
|
|
494
|
+
* });
|
|
495
|
+
* ```
|
|
496
|
+
*
|
|
497
|
+
* @example Works with any StandardSchema library
|
|
498
|
+
* ```typescript
|
|
499
|
+
* import * as v from 'valibot';
|
|
500
|
+
*
|
|
501
|
+
* router.post('/valibot', agent.validator({
|
|
502
|
+
* input: v.object({ count: v.number() })
|
|
503
|
+
* }), async (c) => {
|
|
504
|
+
* const data = c.req.valid('json'); // Typed correctly
|
|
505
|
+
* return c.json({ count: data.count });
|
|
506
|
+
* });
|
|
507
|
+
* ```
|
|
508
|
+
*
|
|
509
|
+
* @example Validation error response (400)
|
|
510
|
+
* ```typescript
|
|
511
|
+
* // Request: { "name": "Bob" } (missing 'age')
|
|
512
|
+
* // Response: {
|
|
513
|
+
* // "error": "Validation failed",
|
|
514
|
+
* // "message": "age: Invalid input: expected number, received undefined",
|
|
515
|
+
* // "issues": [{ "message": "...", "path": ["age"] }]
|
|
516
|
+
* // }
|
|
517
|
+
* ```
|
|
518
|
+
*/
|
|
519
|
+
validator: AgentValidator<TInput, TOutput>;
|
|
357
520
|
/**
|
|
358
521
|
* Array of evaluation functions created via agent.createEval().
|
|
359
522
|
* Used for testing and validating agent behavior.
|
|
@@ -375,8 +538,8 @@ export type Agent<TInput extends StandardSchemaV1 | undefined = any, TOutput ext
|
|
|
375
538
|
* });
|
|
376
539
|
*
|
|
377
540
|
* // Create eval to validate output
|
|
378
|
-
* agent.createEval({
|
|
379
|
-
*
|
|
541
|
+
* agent.createEval('check-positive', {
|
|
542
|
+
* description: 'Ensures output is a positive number',
|
|
380
543
|
* handler: async (run, result) => {
|
|
381
544
|
* return result > 0; // Assert output is positive
|
|
382
545
|
* }
|
|
@@ -407,7 +570,7 @@ export type Agent<TInput extends StandardSchemaV1 | undefined = any, TOutput ext
|
|
|
407
570
|
* });
|
|
408
571
|
* ```
|
|
409
572
|
*/
|
|
410
|
-
addEventListener(eventName: 'started', callback: (eventName: 'started', agent: Agent<TInput, TOutput, TStream, TConfig, TAppState>, context: AgentContext<any,
|
|
573
|
+
addEventListener(eventName: 'started', callback: (eventName: 'started', agent: Agent<TInput, TOutput, TStream, TConfig, TAppState>, context: AgentContext<any, TConfig, TAppState>) => Promise<void> | void): void;
|
|
411
574
|
/**
|
|
412
575
|
* Register an event listener for when the agent completes successfully.
|
|
413
576
|
*
|
|
@@ -421,7 +584,7 @@ export type Agent<TInput extends StandardSchemaV1 | undefined = any, TOutput ext
|
|
|
421
584
|
* });
|
|
422
585
|
* ```
|
|
423
586
|
*/
|
|
424
|
-
addEventListener(eventName: 'completed', callback: (eventName: 'completed', agent: Agent<TInput, TOutput, TStream, TConfig, TAppState>, context: AgentContext<any,
|
|
587
|
+
addEventListener(eventName: 'completed', callback: (eventName: 'completed', agent: Agent<TInput, TOutput, TStream, TConfig, TAppState>, context: AgentContext<any, TConfig, TAppState>) => Promise<void> | void): void;
|
|
425
588
|
/**
|
|
426
589
|
* Register an event listener for when the agent throws an error.
|
|
427
590
|
*
|
|
@@ -435,28 +598,28 @@ export type Agent<TInput extends StandardSchemaV1 | undefined = any, TOutput ext
|
|
|
435
598
|
* });
|
|
436
599
|
* ```
|
|
437
600
|
*/
|
|
438
|
-
addEventListener(eventName: 'errored', callback: (eventName: 'errored', agent: Agent<TInput, TOutput, TStream, TConfig, TAppState>, context: AgentContext<any,
|
|
601
|
+
addEventListener(eventName: 'errored', callback: (eventName: 'errored', agent: Agent<TInput, TOutput, TStream, TConfig, TAppState>, context: AgentContext<any, TConfig, TAppState>, data: Error) => Promise<void> | void): void;
|
|
439
602
|
/**
|
|
440
603
|
* Remove a previously registered 'started' event listener.
|
|
441
604
|
*
|
|
442
605
|
* @param eventName - Must be 'started'
|
|
443
606
|
* @param callback - The callback function to remove
|
|
444
607
|
*/
|
|
445
|
-
removeEventListener(eventName: 'started', callback: (eventName: 'started', agent: Agent<TInput, TOutput, TStream, TConfig, TAppState>, context: AgentContext<any,
|
|
608
|
+
removeEventListener(eventName: 'started', callback: (eventName: 'started', agent: Agent<TInput, TOutput, TStream, TConfig, TAppState>, context: AgentContext<any, TConfig, TAppState>) => Promise<void> | void): void;
|
|
446
609
|
/**
|
|
447
610
|
* Remove a previously registered 'completed' event listener.
|
|
448
611
|
*
|
|
449
612
|
* @param eventName - Must be 'completed'
|
|
450
613
|
* @param callback - The callback function to remove
|
|
451
614
|
*/
|
|
452
|
-
removeEventListener(eventName: 'completed', callback: (eventName: 'completed', agent: Agent<TInput, TOutput, TStream, TConfig, TAppState>, context: AgentContext<any,
|
|
615
|
+
removeEventListener(eventName: 'completed', callback: (eventName: 'completed', agent: Agent<TInput, TOutput, TStream, TConfig, TAppState>, context: AgentContext<any, TConfig, TAppState>) => Promise<void> | void): void;
|
|
453
616
|
/**
|
|
454
617
|
* Remove a previously registered 'errored' event listener.
|
|
455
618
|
*
|
|
456
619
|
* @param eventName - Must be 'errored'
|
|
457
620
|
* @param callback - The callback function to remove
|
|
458
621
|
*/
|
|
459
|
-
removeEventListener(eventName: 'errored', callback: (eventName: 'errored', agent: Agent<TInput, TOutput, TStream, TConfig, TAppState>, context: AgentContext<any,
|
|
622
|
+
removeEventListener(eventName: 'errored', callback: (eventName: 'errored', agent: Agent<TInput, TOutput, TStream, TConfig, TAppState>, context: AgentContext<any, TConfig, TAppState>, data: Error) => Promise<void> | void): void;
|
|
460
623
|
} & (TInput extends StandardSchemaV1 ? {
|
|
461
624
|
inputSchema: TInput;
|
|
462
625
|
} : {
|
|
@@ -470,8 +633,8 @@ export type Agent<TInput extends StandardSchemaV1 | undefined = any, TOutput ext
|
|
|
470
633
|
} : {
|
|
471
634
|
stream?: false;
|
|
472
635
|
});
|
|
473
|
-
type InferSchemaInput<T> = T extends StandardSchemaV1 ?
|
|
474
|
-
type InferStreamOutput<TOutput, TStream extends boolean> = TStream extends true ? TOutput extends StandardSchemaV1 ? ReadableStream<
|
|
636
|
+
type InferSchemaInput<T> = T extends StandardSchemaV1 ? InferOutput<T> : never;
|
|
637
|
+
type InferStreamOutput<TOutput, TStream extends boolean> = TStream extends true ? TOutput extends StandardSchemaV1 ? ReadableStream<InferOutput<TOutput>> : ReadableStream<unknown> : TOutput extends StandardSchemaV1 ? InferOutput<TOutput> : void;
|
|
475
638
|
type SchemaInput<TSchema> = TSchema extends {
|
|
476
639
|
input: infer I;
|
|
477
640
|
} ? I : undefined;
|
|
@@ -481,13 +644,29 @@ type SchemaOutput<TSchema> = TSchema extends {
|
|
|
481
644
|
type SchemaStream<TSchema> = TSchema extends {
|
|
482
645
|
stream: infer S;
|
|
483
646
|
} ? S extends boolean ? S : false : false;
|
|
484
|
-
type SchemaHandlerReturn<TSchema> = SchemaStream<TSchema> extends true ? SchemaOutput<TSchema> extends StandardSchemaV1 ? ReadableStream<
|
|
485
|
-
type AgentHandlerFromConfig<TSchema, TSetupReturn, TAppState = AppState> = SchemaInput<TSchema> extends infer I ? I extends StandardSchemaV1 ? (ctx: AgentContext<any,
|
|
647
|
+
type SchemaHandlerReturn<TSchema> = SchemaStream<TSchema> extends true ? SchemaOutput<TSchema> extends StandardSchemaV1 ? ReadableStream<InferOutput<SchemaOutput<TSchema>>> : ReadableStream<unknown> : SchemaOutput<TSchema> extends StandardSchemaV1 ? InferOutput<SchemaOutput<TSchema>> : void;
|
|
648
|
+
type AgentHandlerFromConfig<TSchema, TSetupReturn, TAppState = AppState> = SchemaInput<TSchema> extends infer I ? I extends StandardSchemaV1 ? (ctx: AgentContext<any, TSetupReturn, TAppState>, input: InferOutput<I>) => Promise<SchemaHandlerReturn<TSchema>> | SchemaHandlerReturn<TSchema> : (ctx: AgentContext<any, TSetupReturn, TAppState>) => Promise<SchemaHandlerReturn<TSchema>> | SchemaHandlerReturn<TSchema> : (ctx: AgentContext<any, TSetupReturn, TAppState>) => Promise<SchemaHandlerReturn<TSchema>> | SchemaHandlerReturn<TSchema>;
|
|
486
649
|
/**
|
|
487
650
|
* Configuration object for creating an agent with automatic type inference.
|
|
488
651
|
*
|
|
652
|
+
* Passed as the second parameter to createAgent(name, config).
|
|
653
|
+
*
|
|
489
654
|
* @template TSchema - Schema definition object containing optional input, output, and stream properties
|
|
490
655
|
* @template TConfig - Function type that returns agent-specific configuration from setup
|
|
656
|
+
*
|
|
657
|
+
* @example
|
|
658
|
+
* ```typescript
|
|
659
|
+
* const agent = createAgent('greeting', {
|
|
660
|
+
* description: 'Generates personalized greetings',
|
|
661
|
+
* schema: {
|
|
662
|
+
* input: z.object({ name: z.string(), age: z.number() }),
|
|
663
|
+
* output: z.string()
|
|
664
|
+
* },
|
|
665
|
+
* handler: async (ctx, { name, age }) => {
|
|
666
|
+
* return `Hello, ${name}! You are ${age} years old.`;
|
|
667
|
+
* }
|
|
668
|
+
* });
|
|
669
|
+
* ```
|
|
491
670
|
*/
|
|
492
671
|
export interface CreateAgentConfig<TSchema extends {
|
|
493
672
|
input?: StandardSchemaV1;
|
|
@@ -508,17 +687,21 @@ export interface CreateAgentConfig<TSchema extends {
|
|
|
508
687
|
*/
|
|
509
688
|
schema?: TSchema;
|
|
510
689
|
/**
|
|
511
|
-
*
|
|
690
|
+
* Optional description of what this agent does, visible in the Agentuity platform.
|
|
512
691
|
*
|
|
513
692
|
* @example
|
|
514
693
|
* ```typescript
|
|
515
|
-
*
|
|
516
|
-
* name: 'Greeting Agent',
|
|
517
|
-
* description: 'Returns personalized greetings'
|
|
518
|
-
* }
|
|
694
|
+
* description: 'Returns personalized greetings'
|
|
519
695
|
* ```
|
|
520
696
|
*/
|
|
521
|
-
|
|
697
|
+
description?: string;
|
|
698
|
+
/**
|
|
699
|
+
* Optional metadata object (typically injected by build plugin during compilation).
|
|
700
|
+
* Contains agent identification and versioning information.
|
|
701
|
+
*
|
|
702
|
+
* @internal - Usually populated by build tooling, not manually set
|
|
703
|
+
*/
|
|
704
|
+
metadata?: Partial<AgentMetadata>;
|
|
522
705
|
/**
|
|
523
706
|
* Optional async function called once on app startup to initialize agent-specific resources.
|
|
524
707
|
* The returned value is available in the handler via `ctx.config`.
|
|
@@ -571,10 +754,150 @@ export interface CreateAgentConfig<TSchema extends {
|
|
|
571
754
|
*/
|
|
572
755
|
shutdown?: (app: AppState, config: TConfig extends (app: AppState) => infer R ? Awaited<R> : undefined) => Promise<void> | void;
|
|
573
756
|
}
|
|
757
|
+
/**
|
|
758
|
+
* The public interface returned by createAgent().
|
|
759
|
+
* Provides methods to run the agent, create evaluations, and manage event listeners.
|
|
760
|
+
*
|
|
761
|
+
* @template TInput - Input schema type (StandardSchemaV1 or undefined if no input)
|
|
762
|
+
* @template TOutput - Output schema type (StandardSchemaV1 or undefined if no output)
|
|
763
|
+
* @template TStream - Whether the agent returns a stream (true/false)
|
|
764
|
+
*
|
|
765
|
+
* @example
|
|
766
|
+
* ```typescript
|
|
767
|
+
* const agent = createAgent('greeting', {
|
|
768
|
+
* schema: {
|
|
769
|
+
* input: z.object({ name: z.string() }),
|
|
770
|
+
* output: z.string()
|
|
771
|
+
* },
|
|
772
|
+
* handler: async (ctx, { name }) => `Hello, ${name}!`
|
|
773
|
+
* });
|
|
774
|
+
*
|
|
775
|
+
* // Run the agent
|
|
776
|
+
* const result = await agent.run({ name: 'Alice' });
|
|
777
|
+
*
|
|
778
|
+
* // Create evaluation
|
|
779
|
+
* const evalDef = agent.createEval('greeting-accuracy', {
|
|
780
|
+
* description: 'Checks if greeting includes the user name',
|
|
781
|
+
* handler: async (ctx, input, output) => {
|
|
782
|
+
* return { score: output.includes(input.name) ? 1 : 0 };
|
|
783
|
+
* }
|
|
784
|
+
* });
|
|
785
|
+
*
|
|
786
|
+
* // Listen to events
|
|
787
|
+
* agent.addEventListener('completed', async (eventName, agent, context) => {
|
|
788
|
+
* console.log('Agent completed successfully');
|
|
789
|
+
* });
|
|
790
|
+
* ```
|
|
791
|
+
*/
|
|
574
792
|
export interface AgentRunner<TInput extends StandardSchemaV1 | undefined = any, TOutput extends StandardSchemaV1 | undefined = any, TStream extends boolean = false> {
|
|
793
|
+
/** Agent metadata (id, name, description, etc.) */
|
|
575
794
|
metadata: AgentMetadata;
|
|
795
|
+
/**
|
|
796
|
+
* Execute the agent with validated input.
|
|
797
|
+
* If agent has no input schema, call with no arguments.
|
|
798
|
+
* If agent has input schema, pass validated input object.
|
|
799
|
+
*
|
|
800
|
+
* @example
|
|
801
|
+
* ```typescript
|
|
802
|
+
* // Agent with input
|
|
803
|
+
* const result = await agent.run({ name: 'Alice' });
|
|
804
|
+
*
|
|
805
|
+
* // Agent without input
|
|
806
|
+
* const result = await agent.run();
|
|
807
|
+
* ```
|
|
808
|
+
*/
|
|
576
809
|
run: undefined extends TInput ? () => Promise<InferStreamOutput<Exclude<TOutput, undefined>, TStream>> : (input: InferSchemaInput<Exclude<TInput, undefined>>) => Promise<InferStreamOutput<Exclude<TOutput, undefined>, TStream>>;
|
|
810
|
+
/**
|
|
811
|
+
* Create Hono validator middleware for this agent.
|
|
812
|
+
* Automatically validates request input against the agent's schema.
|
|
813
|
+
*
|
|
814
|
+
* @example
|
|
815
|
+
* ```typescript
|
|
816
|
+
* import myAgent from './my-agent';
|
|
817
|
+
* router.post('/', myAgent.validator(), async (c) => {
|
|
818
|
+
* const data = c.req.valid('json'); // Fully typed!
|
|
819
|
+
* return c.json(await myAgent.run(data));
|
|
820
|
+
* });
|
|
821
|
+
* ```
|
|
822
|
+
*/
|
|
823
|
+
validator: AgentValidator<TInput, TOutput>;
|
|
824
|
+
/** Input schema (if defined) */
|
|
825
|
+
inputSchema?: TInput;
|
|
826
|
+
/** Output schema (if defined) */
|
|
827
|
+
outputSchema?: TOutput;
|
|
828
|
+
/** Whether agent returns a stream */
|
|
829
|
+
stream?: TStream;
|
|
830
|
+
/**
|
|
831
|
+
* Create an evaluation for this agent.
|
|
832
|
+
* Evaluations run automatically after the agent completes.
|
|
833
|
+
*
|
|
834
|
+
* @example
|
|
835
|
+
* ```typescript
|
|
836
|
+
* const accuracyEval = agent.createEval('accuracy', {
|
|
837
|
+
* description: 'Validates output length is non-zero',
|
|
838
|
+
* handler: async (ctx, input, output) => ({
|
|
839
|
+
* score: output.length > 0 ? 1 : 0,
|
|
840
|
+
* metadata: { outputLength: output.length }
|
|
841
|
+
* })
|
|
842
|
+
* });
|
|
843
|
+
* ```
|
|
844
|
+
*/
|
|
845
|
+
createEval: CreateEvalMethod<TInput, TOutput>;
|
|
846
|
+
/**
|
|
847
|
+
* Add event listener for 'started' or 'completed' events.
|
|
848
|
+
* Listeners fire sequentially in the order they were added.
|
|
849
|
+
*
|
|
850
|
+
* @param eventName - 'started' or 'completed'
|
|
851
|
+
* @param callback - Function to call when event fires
|
|
852
|
+
*
|
|
853
|
+
* @example
|
|
854
|
+
* ```typescript
|
|
855
|
+
* agent.addEventListener('started', async (eventName, agent, context) => {
|
|
856
|
+
* context.logger.info('Agent execution started');
|
|
857
|
+
* });
|
|
858
|
+
* ```
|
|
859
|
+
*/
|
|
860
|
+
addEventListener(eventName: 'started' | 'completed', callback: (eventName: 'started' | 'completed', agent: Agent<TInput, TOutput, TStream, any, any>, context: AgentContext<any, any, any>) => Promise<void> | void): void;
|
|
861
|
+
/**
|
|
862
|
+
* Add event listener for 'errored' event.
|
|
863
|
+
* Fires when agent handler throws an error.
|
|
864
|
+
*
|
|
865
|
+
* @param eventName - 'errored'
|
|
866
|
+
* @param callback - Function to call when error occurs
|
|
867
|
+
*
|
|
868
|
+
* @example
|
|
869
|
+
* ```typescript
|
|
870
|
+
* agent.addEventListener('errored', async (eventName, agent, context, error) => {
|
|
871
|
+
* context.logger.error('Agent failed', { error: error.message });
|
|
872
|
+
* });
|
|
873
|
+
* ```
|
|
874
|
+
*/
|
|
875
|
+
addEventListener(eventName: 'errored', callback: (eventName: 'errored', agent: Agent<TInput, TOutput, TStream, any, any>, context: AgentContext<any, any, any>, error: Error) => Promise<void> | void): void;
|
|
876
|
+
/**
|
|
877
|
+
* Remove event listener for 'started' or 'completed' events.
|
|
878
|
+
*
|
|
879
|
+
* @param eventName - 'started' or 'completed'
|
|
880
|
+
* @param callback - The same callback function that was added
|
|
881
|
+
*/
|
|
882
|
+
removeEventListener(eventName: 'started' | 'completed', callback: (eventName: 'started' | 'completed', agent: Agent<TInput, TOutput, TStream, any, any>, context: AgentContext<any, any, any>) => Promise<void> | void): void;
|
|
883
|
+
/**
|
|
884
|
+
* Remove event listener for 'errored' event.
|
|
885
|
+
*
|
|
886
|
+
* @param eventName - 'errored'
|
|
887
|
+
* @param callback - The same callback function that was added
|
|
888
|
+
*/
|
|
889
|
+
removeEventListener(eventName: 'errored', callback: (eventName: 'errored', agent: Agent<TInput, TOutput, TStream, any, any>, context: AgentContext<any, any, any>, error: Error) => Promise<void> | void): void;
|
|
577
890
|
}
|
|
891
|
+
/**
|
|
892
|
+
* Get the global runtime state (for production use).
|
|
893
|
+
* In tests, use TestAgentContext which has isolated runtime state.
|
|
894
|
+
*/
|
|
895
|
+
export declare function getGlobalRuntimeState(): AgentRuntimeState;
|
|
896
|
+
/**
|
|
897
|
+
* Get the runtime state from an AgentContext.
|
|
898
|
+
* @internal
|
|
899
|
+
*/
|
|
900
|
+
export declare function getAgentRuntime(ctx: AgentContext<any, any, any>): AgentRuntimeState;
|
|
578
901
|
/**
|
|
579
902
|
* Union type of all registered agent names.
|
|
580
903
|
* Falls back to `string` when no agents are registered (before augmentation).
|
|
@@ -621,17 +944,21 @@ export interface CreateAgentConfigExplicit<TInput extends StandardSchemaV1 | und
|
|
|
621
944
|
stream?: TStream;
|
|
622
945
|
};
|
|
623
946
|
/**
|
|
624
|
-
*
|
|
947
|
+
* Optional description of what this agent does.
|
|
625
948
|
*
|
|
626
949
|
* @example
|
|
627
950
|
* ```typescript
|
|
628
|
-
*
|
|
629
|
-
* name: 'My Agent',
|
|
630
|
-
* description: 'Does something useful'
|
|
631
|
-
* }
|
|
951
|
+
* description: 'Does something useful'
|
|
632
952
|
* ```
|
|
633
953
|
*/
|
|
634
|
-
|
|
954
|
+
description?: string;
|
|
955
|
+
/**
|
|
956
|
+
* Optional metadata object (typically injected by build plugin during compilation).
|
|
957
|
+
* Contains agent identification and versioning information.
|
|
958
|
+
*
|
|
959
|
+
* @internal - Usually populated by build tooling, not manually set
|
|
960
|
+
*/
|
|
961
|
+
metadata?: Partial<AgentMetadata>;
|
|
635
962
|
/**
|
|
636
963
|
* Optional setup function receiving app state, returns agent config.
|
|
637
964
|
* The returned value is available in the handler via `ctx.config`.
|
|
@@ -674,7 +1001,7 @@ export interface CreateAgentConfigExplicit<TInput extends StandardSchemaV1 | und
|
|
|
674
1001
|
* }
|
|
675
1002
|
* ```
|
|
676
1003
|
*/
|
|
677
|
-
handler: TInput extends StandardSchemaV1 ? TStream extends true ? TOutput extends StandardSchemaV1 ? (c: AgentContext<any,
|
|
1004
|
+
handler: TInput extends StandardSchemaV1 ? TStream extends true ? TOutput extends StandardSchemaV1 ? (c: AgentContext<any, TConfig, TAppState>, input: InferOutput<TInput>) => Promise<ReadableStream<InferOutput<TOutput>>> | ReadableStream<InferOutput<TOutput>> : (c: AgentContext<any, TConfig, TAppState>, input: InferOutput<TInput>) => Promise<ReadableStream<unknown>> | ReadableStream<unknown> : TOutput extends StandardSchemaV1 ? (c: AgentContext<any, TConfig, TAppState>, input: InferOutput<TInput>) => Promise<InferOutput<TOutput>> | InferOutput<TOutput> : (c: AgentContext<any, TConfig, TAppState>, input: InferOutput<TInput>) => Promise<void> | void : TStream extends true ? TOutput extends StandardSchemaV1 ? (c: AgentContext<any, TConfig, TAppState>) => Promise<ReadableStream<InferOutput<TOutput>>> | ReadableStream<InferOutput<TOutput>> : (c: AgentContext<any, TConfig, TAppState>) => Promise<ReadableStream<unknown>> | ReadableStream<unknown> : TOutput extends StandardSchemaV1 ? (c: AgentContext<any, TConfig, TAppState>) => Promise<InferOutput<TOutput>> | InferOutput<TOutput> : (c: AgentContext<any, TConfig, TAppState>) => Promise<void> | void;
|
|
678
1005
|
}
|
|
679
1006
|
/**
|
|
680
1007
|
* Creates an agent with schema validation and lifecycle hooks.
|
|
@@ -684,17 +1011,15 @@ export interface CreateAgentConfigExplicit<TInput extends StandardSchemaV1 | und
|
|
|
684
1011
|
* @template TSchema - Schema definition object containing optional input, output, and stream properties
|
|
685
1012
|
* @template TConfig - Function type that returns agent-specific configuration from setup
|
|
686
1013
|
*
|
|
1014
|
+
* @param name - Unique agent name (must be unique within the project)
|
|
687
1015
|
* @param config - Agent configuration object
|
|
688
1016
|
*
|
|
689
|
-
* @returns
|
|
1017
|
+
* @returns AgentRunner with a run method for executing the agent
|
|
690
1018
|
*
|
|
691
1019
|
* @example
|
|
692
1020
|
* ```typescript
|
|
693
|
-
* const agent = createAgent({
|
|
694
|
-
*
|
|
695
|
-
* name: 'Greeting Agent',
|
|
696
|
-
* description: 'Returns personalized greetings'
|
|
697
|
-
* },
|
|
1021
|
+
* const agent = createAgent('greeting-agent', {
|
|
1022
|
+
* description: 'Returns personalized greetings',
|
|
698
1023
|
* schema: {
|
|
699
1024
|
* input: z.object({ name: z.string(), age: z.number() }),
|
|
700
1025
|
* output: z.string()
|
|
@@ -704,13 +1029,16 @@ export interface CreateAgentConfigExplicit<TInput extends StandardSchemaV1 | und
|
|
|
704
1029
|
* return `Hello, ${name}! You are ${age} years old.`;
|
|
705
1030
|
* }
|
|
706
1031
|
* });
|
|
1032
|
+
*
|
|
1033
|
+
* // Call the agent directly
|
|
1034
|
+
* const result = await agent.run({ name: 'Alice', age: 30 });
|
|
707
1035
|
* ```
|
|
708
1036
|
*/
|
|
709
1037
|
export declare function createAgent<TSchema extends {
|
|
710
1038
|
input?: StandardSchemaV1;
|
|
711
1039
|
output?: StandardSchemaV1;
|
|
712
1040
|
stream?: boolean;
|
|
713
|
-
} | undefined = undefined, TConfig extends (app: AppState) => any = any>(config: CreateAgentConfig<TSchema, TConfig>):
|
|
1041
|
+
} | undefined = undefined, TConfig extends (app: AppState) => any = any>(name: string, config: CreateAgentConfig<TSchema, TConfig>): AgentRunner<SchemaInput<TSchema>, SchemaOutput<TSchema>, SchemaStream<TSchema>>;
|
|
714
1042
|
/**
|
|
715
1043
|
* Creates an agent with explicit generic type parameters.
|
|
716
1044
|
*
|
|
@@ -722,9 +1050,10 @@ export declare function createAgent<TSchema extends {
|
|
|
722
1050
|
* @template TConfig - Type returned by setup function
|
|
723
1051
|
* @template TAppState - Custom app state type from createApp
|
|
724
1052
|
*
|
|
1053
|
+
* @param name - Unique agent name (must be unique within the project)
|
|
725
1054
|
* @param config - Agent configuration object
|
|
726
1055
|
*
|
|
727
|
-
* @returns
|
|
1056
|
+
* @returns AgentRunner with explicit types and a run method
|
|
728
1057
|
*
|
|
729
1058
|
* @example
|
|
730
1059
|
* ```typescript
|
|
@@ -734,11 +1063,8 @@ export declare function createAgent<TSchema extends {
|
|
|
734
1063
|
* const agent = createAgent<
|
|
735
1064
|
* z.ZodObject<any>, // TInput
|
|
736
1065
|
* z.ZodString, // TOutput
|
|
737
|
-
* false
|
|
738
|
-
*
|
|
739
|
-
* MyAppState // TAppState
|
|
740
|
-
* >({
|
|
741
|
-
* metadata: { name: 'Custom Agent' },
|
|
1066
|
+
* false // TStream
|
|
1067
|
+
* >('custom-agent', {
|
|
742
1068
|
* setup: async (app) => ({ cache: new Map() }),
|
|
743
1069
|
* handler: async (ctx, input) => {
|
|
744
1070
|
* const db = ctx.app.db;
|
|
@@ -748,14 +1074,60 @@ export declare function createAgent<TSchema extends {
|
|
|
748
1074
|
* });
|
|
749
1075
|
* ```
|
|
750
1076
|
*/
|
|
751
|
-
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>):
|
|
1077
|
+
export declare function createAgent<TInput extends StandardSchemaV1 | undefined = undefined, TOutput extends StandardSchemaV1 | undefined = undefined, TStream extends boolean = false, TConfig = unknown, TAppState = AppState>(name: string, config: CreateAgentConfigExplicit<TInput, TOutput, TStream, TConfig, TAppState>): AgentRunner<TInput, TOutput, TStream>;
|
|
752
1078
|
/**
|
|
753
1079
|
* Populate the agents object with all registered agents
|
|
1080
|
+
* Keys are converted to camelCase to match the generated TypeScript types
|
|
754
1081
|
*/
|
|
755
1082
|
export declare const populateAgentsRegistry: (ctx: Context) => any;
|
|
756
1083
|
export declare const createAgentMiddleware: (agentName: AgentName | "") => MiddlewareHandler;
|
|
757
1084
|
export declare const getAgents: () => Map<string, Agent<any, any, any, any, any>>;
|
|
758
1085
|
export declare const runAgentSetups: (appState: AppState) => Promise<void>;
|
|
759
1086
|
export declare const runAgentShutdowns: (appState: AppState) => Promise<void>;
|
|
1087
|
+
/**
|
|
1088
|
+
* Run an agent within a specific AgentContext.
|
|
1089
|
+
* Sets up AsyncLocalStorage with the provided context and executes the agent.
|
|
1090
|
+
*
|
|
1091
|
+
* This is the recommended way to test agents in unit tests. It automatically:
|
|
1092
|
+
* - Registers the agent in the runtime state so event listeners fire
|
|
1093
|
+
* - Sets up AsyncLocalStorage so getAgentContext() works inside the agent
|
|
1094
|
+
* - Handles both agents with input and agents without input
|
|
1095
|
+
*
|
|
1096
|
+
* **Use cases:**
|
|
1097
|
+
* - Unit testing agents with TestAgentContext
|
|
1098
|
+
* - Running agents outside HTTP request flow
|
|
1099
|
+
* - Custom agent execution environments
|
|
1100
|
+
* - Testing event listeners and evaluations
|
|
1101
|
+
*
|
|
1102
|
+
* @template TInput - Type of the input parameter
|
|
1103
|
+
* @template TOutput - Type of the return value
|
|
1104
|
+
*
|
|
1105
|
+
* @param ctx - The AgentContext to use (typically TestAgentContext in tests)
|
|
1106
|
+
* @param agent - The AgentRunner to execute (returned from createAgent)
|
|
1107
|
+
* @param input - Input data (required if agent has input schema, omit otherwise)
|
|
1108
|
+
*
|
|
1109
|
+
* @returns Promise resolving to the agent's output
|
|
1110
|
+
*
|
|
1111
|
+
* @example
|
|
1112
|
+
* ```typescript
|
|
1113
|
+
* import { runInAgentContext, TestAgentContext } from '@agentuity/runtime/test';
|
|
1114
|
+
*
|
|
1115
|
+
* test('greeting agent', async () => {
|
|
1116
|
+
* const ctx = new TestAgentContext();
|
|
1117
|
+
* const result = await runInAgentContext(ctx, greetingAgent, {
|
|
1118
|
+
* name: 'Alice',
|
|
1119
|
+
* age: 30
|
|
1120
|
+
* });
|
|
1121
|
+
* expect(result).toBe('Hello, Alice! You are 30 years old.');
|
|
1122
|
+
* });
|
|
1123
|
+
*
|
|
1124
|
+
* test('no-input agent', async () => {
|
|
1125
|
+
* const ctx = new TestAgentContext();
|
|
1126
|
+
* const result = await runInAgentContext(ctx, statusAgent);
|
|
1127
|
+
* expect(result).toEqual({ status: 'ok' });
|
|
1128
|
+
* });
|
|
1129
|
+
* ```
|
|
1130
|
+
*/
|
|
1131
|
+
export declare function runInAgentContext<TInput, TOutput>(ctx: AgentContext<any, any, any>, agent: AgentRunner<any, any, any>, input?: TInput): Promise<TOutput>;
|
|
760
1132
|
export {};
|
|
761
1133
|
//# sourceMappingURL=agent.d.ts.map
|