@microfox/ai-router 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,34 @@
1
+ # Changelog
2
+
3
+ ## 1.0.1
4
+
5
+ ### Patch Changes
6
+
7
+ - de2e3cc: Changes from PR #559: suno
8
+
9
+ All notable changes to this project will be documented in this file.
10
+
11
+ ## [1.0.0] - 2025-07-02
12
+
13
+ ### Added
14
+
15
+ - Initial release
16
+ - Basic SDK structure
17
+ - TypeScript support
18
+
19
+ <!-- Add your changes here using this format:
20
+
21
+ ## [1.1.0] - YYYY-MM-DD
22
+
23
+ ### Added
24
+ - New feature
25
+
26
+ ### Changed
27
+ - Updated feature
28
+
29
+ ### Fixed
30
+ - Bug fix
31
+
32
+ ### Removed
33
+ - Deprecated feature
34
+ -->
package/README.md ADDED
@@ -0,0 +1,227 @@
1
+ # AiRouter: An Agentic Application Framework
2
+
3
+ `AiRouter` is a lightweight, powerful framework for building complex, multi-agent applications in TypeScript. Inspired by web frameworks like Express or Fastify, it provides a robust routing system for creating, composing, and calling AI agents and tools in a structured and predictable way.
4
+
5
+ Stop thinking in single-shot API calls and start building hierarchies of agents that can call each other, use tools, and stream responses back to the user from any point in the chain.
6
+
7
+ ## Quick Start: Your First Agent
8
+
9
+ See how easy it is to get started. In just a few lines of code, you can create a router, define an agent, and handle a request.
10
+
11
+ ```typescript
12
+ import { AiRouter } from './router';
13
+
14
+ // 1. Create a new agent router
15
+ const app = new AiRouter();
16
+
17
+ // 2. Define an agent that streams a response
18
+ app.agent('/hello', async (ctx) => {
19
+ ctx.response.write({ type: 'text', text: 'Hello, ' });
20
+ await new Promise((resolve) => setTimeout(resolve, 500));
21
+ ctx.response.write({ type: 'text', text: 'world!' });
22
+ });
23
+
24
+ // 3. Handle an incoming request and get a standard Response object
25
+ const request = new Request('http://localhost/hello');
26
+ const response = app.handle(new URL(request.url).pathname, { request });
27
+
28
+ // You can now return this response from your API endpoint.
29
+ ```
30
+
31
+ ---
32
+
33
+ ## Core Concepts
34
+
35
+ - **`AiRouter`**: The main class, which acts as a router. You create an instance of this to register all your agents, tools, and middleware.
36
+ - **Agents**: The primary actors in the system. An agent is a function that receives a `context` (`ctx`) object and can perform actions, such as streaming text, calling other agents, using tools, or modifying state.
37
+ - **Tools**: Schema-defined functions that an agent can call. Tools are typically used for deterministic operations or for interacting with external APIs. They receive the `context` and a parsed, schema-validated input object.
38
+ - **The Context (`ctx`)**: A per-request object that is passed to every handler. It contains everything needed for an agent to do its work:
39
+ - `ctx.response`: The stream writer to send data back to the end-user.
40
+ - `ctx.next`: An object with methods for internal dispatching (`callAgent`, `callTool`, etc.).
41
+ - `ctx.state`: A mutable object that lives for the duration of a single request, allowing agents and middleware to share data.
42
+ - `ctx.logger`: A structured logger that automatically includes a unique `requestId` and the current agent `path` for easy debugging.
43
+ - `ctx.request`: The initial request object.
44
+ - **Hierarchical Routing**: Just like in web frameworks, you can mount entire `AiRouter` instances on a path, allowing you to build modular and reusable collections of agents.
45
+
46
+ ---
47
+
48
+ ## Installation
49
+
50
+ ```bash
51
+ # This package is internal to the project for now.
52
+ # To use it, import it directly from your local path.
53
+ import { AiRouter } from "../packages/ai-utils/src/router";
54
+ ```
55
+
56
+ ---
57
+
58
+ ## Step-by-Step Documentation
59
+
60
+ ### 1. Initialization
61
+
62
+ Create a new `AiRouter` router. You can create as many as you need to organize your application.
63
+
64
+ ```typescript
65
+ const app = new AiRouter();
66
+ ```
67
+
68
+ ### 2. Defining an Agent
69
+
70
+ Use the `.agent()` method to define a handler for a specific path. The handler is an async function that receives the `ctx` object.
71
+
72
+ ```typescript
73
+ app.agent('/simple', async (ctx) => {
74
+ ctx.response.write({ type: 'text', text: 'This is a simple agent.' });
75
+ });
76
+ ```
77
+
78
+ ### 3. Defining a Tool
79
+
80
+ Use the `.tool()` method to define a tool. You must provide a Zod schema for the input `parameters`, which will be automatically validated. The handler receives the `ctx` and the parsed parameters.
81
+
82
+ ```typescript
83
+ import { z } from 'zod';
84
+
85
+ app.tool(
86
+ '/getWeather',
87
+ {
88
+ description: 'Gets the weather for a given location.',
89
+ schema: z.object({
90
+ city: z.string().describe('The city to get the weather for.'),
91
+ }),
92
+ },
93
+ async (ctx, { city }) => {
94
+ // In a real app, you'd call a weather API here.
95
+ const weather = `The weather in ${city} is 72°F and sunny.`;
96
+ ctx.logger.log('Weather tool executed successfully.');
97
+ return { weather };
98
+ }
99
+ );
100
+ ```
101
+
102
+ ### 4. Mounting Routers with `.use()`
103
+
104
+ Organize your code by creating separate routers and mounting them on a main router using `.use()`. This prefixes all routes in the mounted router.
105
+
106
+ ```typescript
107
+ const adminRouter = new AiRouter();
108
+ adminRouter.agent("/dashboard", async (ctx) => { ... });
109
+
110
+ const mainApp = new AiRouter();
111
+ // All routes from adminRouter will now be prefixed with '/admin'
112
+ // e.g., the above agent is now available at '/admin/dashboard'
113
+ mainApp.use("/admin", adminRouter);
114
+ ```
115
+
116
+ You can also use `.use()` to register middleware, which is useful for authentication, logging, or modifying the state before an agent runs.
117
+
118
+ ```typescript
119
+ app.use('*', async (ctx, next) => {
120
+ ctx.state.user = await getUserFromDb(ctx.request);
121
+ ctx.logger.log('User authenticated.');
122
+ await next(); // Don't forget to call next() to continue!
123
+ });
124
+ ```
125
+
126
+ ---
127
+
128
+ ## Advanced Usage
129
+
130
+ ### Internal Calling & Error Handling
131
+
132
+ Agents can call other agents and tools using `ctx.next`. This is the key to building complex, chained behaviors. These calls are resilient; they return a result object (`{ ok, data }` or `{ ok, error }`) instead of throwing, allowing the calling agent to handle failures gracefully.
133
+
134
+ ```typescript
135
+ app.agent('/weather-reporter', async (ctx) => {
136
+ ctx.response.write({ type: 'text', text: 'Checking the weather...' });
137
+
138
+ const result = await ctx.next.callTool('/getWeather', {
139
+ city: 'San Francisco',
140
+ });
141
+
142
+ if (result.ok) {
143
+ ctx.response.write({
144
+ type: 'text',
145
+ text: `\nReport: ${result.data.weather}`,
146
+ });
147
+ } else {
148
+ ctx.logger.error('Failed to get weather:', result.error);
149
+ ctx.response.write({
150
+ type: 'text',
151
+ text: `\nSorry, I couldn't get the weather.`,
152
+ });
153
+ }
154
+ });
155
+ ```
156
+
157
+ ### Agent as a Tool
158
+
159
+ For an LLM to call one of your agents, it must be presented as a `Tool`. `AiRouter` makes this easy with a two-step pattern:
160
+
161
+ **Step 1: Define the Tool Schema with `.actAsTool()`**
162
+ When creating an agent, specify how it should look to an LLM.
163
+
164
+ ```typescript
165
+ import { z } from 'zod';
166
+ const casualRouter = new AiRouter();
167
+
168
+ casualRouter
169
+ .actAsTool('/casual', {
170
+ description: 'A friendly, casual greeting.',
171
+ inputSchema: z.object({ name: z.string() }),
172
+ })
173
+ .agent('/casual', async ({ response, request }) => {
174
+ // The 'params' come from the LLM tool call
175
+ const name = (request.params as any)?.toolInfo?.params?.name || 'there';
176
+ response.write({ type: 'text', text: `Yo, what's up ${name}! 👋` });
177
+ });
178
+ ```
179
+
180
+ **Step 2: Provide the Tool to the LLM with `next.agentAsTool()`**
181
+ Inside another agent, use this method to create the final Vercel AI SDK `Tool` object.
182
+
183
+ ```typescript
184
+ import { streamText } from 'ai';
185
+
186
+ greetingRouter.agent('/decide', async ({ response, next }) => {
187
+ const { textStream } = await streamText({
188
+ model: openai('gpt-4-turbo'),
189
+ prompt: 'Should I greet Chairman Meow casually?',
190
+ tools: {
191
+ // The framework handles connecting this to the agent's implementation
192
+ casualChat: next.agentAsTool('/greeting/casual'),
193
+ },
194
+ });
195
+
196
+ for await (const textPart of textStream) {
197
+ response.write({ type: 'text', text: textPart });
198
+ }
199
+ });
200
+ ```
201
+
202
+ ### Dynamic Tool Attachment with `next.attachTool()`
203
+
204
+ Similarly, you can provide any registered `.tool()` handler to an LLM using `next.attachTool()`.
205
+
206
+ ```typescript
207
+ const { textStream } = await streamText({
208
+ model: openai('gpt-4-turbo'),
209
+ prompt: '...',
210
+ tools: {
211
+ getWeather: next.attachTool('/getWeather'),
212
+ },
213
+ });
214
+ ```
215
+
216
+ ### Logging and Debugging
217
+
218
+ The built-in logger (`ctx.logger`) is your best friend for debugging. It automatically tags every log message with a unique `requestId` and the `path` of the currently executing handler. This makes it easy to trace the flow of a request, even through multiple internal calls.
219
+
220
+ ```
221
+ [aK4xLp8VnI2C] [/decide] -> Running middleware
222
+ [aK4xLp8VnI2C] [/decide] User authenticated.
223
+ [aK4xLp8VnI2C] [/decide] -> Executing agent handler
224
+ [aK4xLp8VnI2C] [/greeting/casual] -> Executing agent handler
225
+ [aK4xLp8VnI2C] [/greeting/casual] <- Finished agent
226
+ [aK4xLp8VnI2C] [/decide] <- Finished agent
227
+ ```
@@ -0,0 +1,300 @@
1
+ import { UIMessageStreamWriter, UIMessage, GenerateObjectResult, UIDataTypes, generateId, Tool, ToolCallOptions, JSONValue } from 'ai';
2
+ import { z, ZodObject } from 'zod';
3
+
4
+ type UITools = Record<string, {
5
+ input: unknown;
6
+ output: unknown | undefined;
7
+ }>;
8
+
9
+ declare const findLastElement: <T>(array: T[]) => T;
10
+ declare const findFirstElement: <T>(array: T[]) => T;
11
+ declare class StreamWriter<METADATA, TOOLS extends UITools> {
12
+ private writer;
13
+ constructor(writer: UIMessageStreamWriter<UIMessage<METADATA, any, TOOLS>>);
14
+ generateId: () => string;
15
+ writeMessageMetadata: <NEW_METADATA extends METADATA>(metadata: NEW_METADATA) => void;
16
+ writeCustomTool: <K extends keyof TOOLS>(tool: {
17
+ toolCallId?: string;
18
+ toolName: K;
19
+ inputTextDelta?: string[];
20
+ input?: any;
21
+ output?: any;
22
+ } & Omit<TOOLS[K], "toolCallId">) => void;
23
+ writeObjectAsTool: <K extends keyof TOOLS>(tool: {
24
+ toolName: K;
25
+ result: GenerateObjectResult<TOOLS[K]["output"]>;
26
+ }) => void;
27
+ }
28
+ declare const getTextParts: (message: UIMessage | null | undefined) => string[];
29
+ declare const getTextPartsContent: (message: UIMessage | null | undefined) => string;
30
+ declare const findLastMessageWith: <T>(message: UIMessage[] | null | undefined, filters: {
31
+ role?: "user" | "assistant" | "system";
32
+ metadata?: Record<string, any>;
33
+ }) => UIMessage<unknown, UIDataTypes, {
34
+ [x: string]: {
35
+ input: unknown;
36
+ output: unknown | undefined;
37
+ };
38
+ }> | null | undefined;
39
+
40
+ declare class AiKitError extends Error {
41
+ constructor(message: string);
42
+ }
43
+ declare class AgentNotFoundError extends AiKitError {
44
+ constructor(path: string);
45
+ }
46
+ declare class ToolNotFoundError extends AiKitError {
47
+ constructor(path: string);
48
+ }
49
+ declare class ToolValidationError extends AiKitError {
50
+ constructor(path: string, validationError: z.ZodError);
51
+ }
52
+ declare class MaxCallDepthExceededError extends AiKitError {
53
+ constructor(maxDepth: number);
54
+ }
55
+ declare class AgentDefinitionMissingError extends AiKitError {
56
+ constructor(path: string);
57
+ }
58
+ type AiStreamWriter<METADATA, PARTS extends UIDataTypes = UIDataTypes, TOOLS extends UITools = UITools> = UIMessageStreamWriter<UIMessage<METADATA, PARTS, TOOLS>> & Omit<StreamWriter<METADATA, TOOLS>, 'writer'> & {
59
+ generateId: typeof generateId;
60
+ };
61
+ /**
62
+ * The context object passed to every agent, tool, and middleware. It contains
63
+ * all the necessary information and utilities for a handler to perform its work.
64
+ * @template METADATA - The type for custom metadata in UI messages.
65
+ * @template PARTS - The type for custom parts in UI messages.
66
+ * @template TOOLS - The type for custom tools in UI messages.
67
+ * @template ContextState - The type for the shared state object.
68
+ */
69
+ type AiContext<METADATA, PARTS extends UIDataTypes = UIDataTypes, TOOLS extends UITools = UITools, ContextState extends Record<string, any> = Record<string, any>> = {
70
+ request: {
71
+ /** The message history for the current request. */
72
+ messages: UIMessage<METADATA, PARTS, TOOLS>[];
73
+ /** Parameters passed from an internal tool or agent call. */
74
+ params?: Record<string, any>;
75
+ [key: string]: any;
76
+ };
77
+ /** A shared, mutable state object that persists for the lifetime of a single request. */
78
+ state: ContextState;
79
+ /**
80
+ * Internal execution context for the router. Should not be modified by user code.
81
+ * @internal
82
+ */
83
+ executionContext: {
84
+ handlerPathStack?: string[];
85
+ currentPath?: string;
86
+ callDepth?: number;
87
+ [key: string]: any;
88
+ };
89
+ /**
90
+ * A unique ID for the top-level request, useful for logging and tracing.
91
+ */
92
+ requestId: string;
93
+ /**
94
+ * A structured logger that automatically includes the `requestId` and current handler path.
95
+ */
96
+ logger: AiLogger;
97
+ /**
98
+ * The stream writer to send data back to the end-user's UI.
99
+ * Includes helpers for writing structured data like tool calls and metadata.
100
+ */
101
+ response: AiStreamWriter<METADATA, PARTS, TOOLS>;
102
+ /**
103
+ * Provides functions for an agent to dispatch calls to other agents or tools.
104
+ * @internal
105
+ */
106
+ next: NextHandler<METADATA, PARTS, TOOLS, ContextState>;
107
+ _onExecutionStart?: () => void;
108
+ _onExecutionEnd?: () => void;
109
+ };
110
+ /** Represents the `next` function in a middleware chain, used to pass control to the next handler. */
111
+ type NextFunction = () => Promise<void>;
112
+ /** A function that handles a request for a specific agent path. */
113
+ type AiHandler<METADATA, PARTS extends UIDataTypes = UIDataTypes, TOOLS extends UITools = UITools, ContextState extends Record<string, any> = Record<string, any>> = (ctx: AiContext<METADATA, PARTS, TOOLS, ContextState>) => Promise<void>;
114
+ /** A function that handles a request for a specific tool path, with validated parameters. */
115
+ type AiToolHandler<METADATA, PARTS extends UIDataTypes = UIDataTypes, TOOLS extends UITools = UITools, ContextState extends Record<string, any> = Record<string, any>> = ((ctx: AiContext<METADATA, PARTS, TOOLS, ContextState>, params: Record<string, any>) => Promise<any>) | ((ctx: AiContext<METADATA, PARTS, TOOLS, ContextState>) => Tool<any, any>);
116
+ /** A function that creates a Tool on-demand with access to the request context. */
117
+ type AiToolFactory<METADATA, PARTS extends UIDataTypes, TOOLS extends UITools, ContextState extends Record<string, any>> = (ctx: AiContext<METADATA, PARTS, TOOLS, ContextState>) => Tool<any, any>;
118
+ /** A function that acts as middleware, processing a request and optionally passing control to the next handler. */
119
+ type AiMiddleware<METADATA, PARTS extends UIDataTypes = UIDataTypes, TOOLS extends UITools = UITools, ContextState extends Record<string, any> = Record<string, any>> = (ctx: AiContext<METADATA, PARTS, TOOLS, ContextState>, next: NextFunction) => Promise<void>;
120
+ /** A simple structured logger interface. */
121
+ type AiLogger = {
122
+ log: (...args: any[]) => void;
123
+ warn: (...args: any[]) => void;
124
+ error: (...args: any[]) => void;
125
+ };
126
+ /** Internal representation of a registered handler in the router's stack. */
127
+ type Layer<METADATA, PARTS extends UIDataTypes = UIDataTypes, TOOLS extends UITools = UITools, ContextState extends Record<string, any> = Record<string, any>> = {
128
+ path: string | RegExp;
129
+ handler: AiMiddleware<METADATA, PARTS, TOOLS, ContextState>;
130
+ isTool: boolean;
131
+ toolOptions?: {
132
+ type: 'static';
133
+ schema: ZodObject<any>;
134
+ description?: string;
135
+ handler: AiToolHandler<METADATA, PARTS, TOOLS, ContextState>;
136
+ } | {
137
+ type: 'factory';
138
+ factory: AiToolFactory<METADATA, PARTS, TOOLS, ContextState>;
139
+ };
140
+ isAgent: boolean;
141
+ hasDynamicParams?: boolean;
142
+ paramNames?: string[];
143
+ };
144
+ /**
145
+ * A composable router for building structured, multi-agent AI applications.
146
+ * It allows you to define agents and tools, compose them together, and handle
147
+ * requests in a predictable, middleware-style pattern.
148
+ *
149
+ * @template KIT_METADATA - The base metadata type for all UI messages in this router.
150
+ * @template PARTS - The base custom parts type for all UI messages.
151
+ * @template TOOLS - The base custom tools type for all UI messages.
152
+ * @template ContextState - The base type for the shared state object.
153
+ */
154
+ declare class AiRouter<KIT_METADATA, PARTS extends UIDataTypes = UIDataTypes, TOOLS extends UITools = UITools, ContextState extends Record<string, any> = {}> {
155
+ private stack;
156
+ private actAsToolDefinitions;
157
+ private logger;
158
+ /** Configuration options for the router instance. */
159
+ options: {
160
+ /** The maximum number of agent-to-agent calls allowed in a single request to prevent infinite loops. */
161
+ maxCallDepth: number;
162
+ };
163
+ /**
164
+ * Constructs a new AiAgentKit router.
165
+ * @param stack An optional initial stack of layers, used for composing routers.
166
+ * @param options Optional configuration for the router.
167
+ */
168
+ constructor(stack?: Layer<KIT_METADATA, PARTS, TOOLS, any>[], options?: {
169
+ maxCallDepth?: number;
170
+ logger?: AiLogger;
171
+ });
172
+ /**
173
+ * Registers a middleware-style agent that runs for a specific path prefix, regex pattern, or wildcard.
174
+ * Agents can modify the context and must call `next()` to pass control to the next handler in the chain.
175
+ * This method is primarily for middleware. For terminal agents, see `.agent()` on an instance.
176
+ *
177
+ * @param path The path prefix, regex pattern, or "*" for wildcard matching.
178
+ * @param agents The agent middleware function(s).
179
+ */
180
+ agent<MW_METADATA, MW_TOOLS extends UITools, MW_STATE extends Record<string, any>>(path: string | RegExp | AiMiddleware<MW_METADATA, PARTS, MW_TOOLS, ContextState & MW_STATE>, ...agents: (AiMiddleware<MW_METADATA, PARTS, MW_TOOLS, ContextState & MW_STATE> | AiRouter<MW_METADATA, PARTS, MW_TOOLS, ContextState & MW_STATE>)[]): AiRouter<KIT_METADATA | MW_METADATA, PARTS, TOOLS & MW_TOOLS, ContextState & MW_STATE>;
181
+ /**
182
+ * Mounts a middleware function or another AiAgentKit router at a specific path.
183
+ * This is the primary method for composing routers and applying cross-cutting middleware.
184
+ *
185
+ * @param path The path prefix to mount the handler on.
186
+ * @param handler The middleware function or AiAgentKit router instance to mount.
187
+ */
188
+ use(mountPathArg: string | RegExp, handler: AiMiddleware<KIT_METADATA, PARTS, TOOLS, ContextState> | AiRouter<any, any, any, any>): this;
189
+ /**
190
+ * Pre-defines the schema and description for an agent when it is used as a tool by an LLM.
191
+ * This allows `next.agentAsTool()` to create a valid `Tool` object without needing the definition at call time.
192
+ * @param path The path of the agent being defined.
193
+ * @param options The tool definition, including a Zod schema and description.
194
+ */
195
+ actAsTool(path: string | RegExp, options: {
196
+ description?: string;
197
+ inputSchema: ZodObject<any>;
198
+ }): this;
199
+ tool<TOOL_METADATA, TOOL_TOOLS extends UITools, TOOL_STATE extends Record<string, any>>(path: string | RegExp, factory: AiToolFactory<TOOL_METADATA, PARTS, TOOL_TOOLS, ContextState & TOOL_STATE>): AiRouter<KIT_METADATA | TOOL_METADATA, PARTS, TOOLS & TOOL_TOOLS, ContextState & TOOL_STATE>;
200
+ tool<TOOL_METADATA, TOOL_TOOLS extends UITools, TOOL_STATE extends Record<string, any>, TOOL_PARAMS extends ZodObject<any>>(path: string | RegExp, options: {
201
+ schema: TOOL_PARAMS;
202
+ description?: string;
203
+ }, handler: (ctx: AiContext<TOOL_METADATA, PARTS, TOOL_TOOLS, ContextState & TOOL_STATE>, params: z.infer<TOOL_PARAMS>) => Promise<any>): AiRouter<KIT_METADATA | TOOL_METADATA, PARTS, TOOLS & TOOL_TOOLS, ContextState & TOOL_STATE>;
204
+ /**
205
+ * Returns the internal stack of layers. Primarily used for manual router composition.
206
+ * @deprecated Prefer using `.use()` for router composition.
207
+ */
208
+ getStack(): Layer<KIT_METADATA, PARTS, TOOLS, any>[];
209
+ /**
210
+ * Returns the internal stack with a path prefix applied to each layer.
211
+ * @param prefix The prefix to add to each path.
212
+ * @deprecated Prefer using `.use()` for router composition.
213
+ */
214
+ getStackWithPrefix(prefix: string): {
215
+ path: string | RegExp;
216
+ handler: AiMiddleware<KIT_METADATA, PARTS, TOOLS, any>;
217
+ isTool: boolean;
218
+ toolOptions?: {
219
+ type: "static";
220
+ schema: ZodObject<any>;
221
+ description?: string;
222
+ handler: AiToolHandler<KIT_METADATA, PARTS, TOOLS, any>;
223
+ } | {
224
+ type: "factory";
225
+ factory: AiToolFactory<KIT_METADATA, PARTS, TOOLS, any>;
226
+ } | undefined;
227
+ isAgent: boolean;
228
+ hasDynamicParams?: boolean;
229
+ paramNames?: string[];
230
+ }[];
231
+ /**
232
+ * Resolves a path based on the parent path and the requested path.
233
+ * - If path starts with `@/`, it's an absolute path from the root.
234
+ * - Otherwise, it's a relative path.
235
+ * @internal
236
+ */
237
+ private _resolvePath;
238
+ /**
239
+ * Creates a new context for an internal agent or tool call.
240
+ * It inherits from the parent context but gets a new logger and call depth.
241
+ * @internal
242
+ */
243
+ private _createSubContext;
244
+ /**
245
+ * Creates a new logger instance with a structured prefix.
246
+ * @internal
247
+ */
248
+ private _createLogger;
249
+ /**
250
+ * Calculates a specificity score for a layer to enable Express-style routing.
251
+ * Higher score means more specific.
252
+ * - Middleware is less specific than an agent/tool.
253
+ * - Deeper paths are more specific.
254
+ * - Static segments are more specific than dynamic segments.
255
+ * @internal
256
+ */
257
+ private _getSpecificityScore;
258
+ /**
259
+ * The core execution engine. It finds all matching layers for a given path
260
+ * and runs them in a middleware-style chain.
261
+ * @internal
262
+ */
263
+ private _execute;
264
+ private pendingExecutions;
265
+ /**
266
+ * The main public entry point for the router. It handles an incoming request,
267
+ * sets up the response stream, creates the root context, and starts the execution chain.
268
+ *
269
+ * @param path The path of the agent or tool to execute.
270
+ * @param initialContext The initial context for the request, typically containing messages.
271
+ * @returns A standard `Response` object containing the rich UI stream.
272
+ */
273
+ handle(path: string, initialContext: Omit<AiContext<KIT_METADATA, PARTS, TOOLS, ContextState>, 'state' | 'response' | 'next' | 'requestId' | 'logger' | 'executionContext'>): Response;
274
+ }
275
+ declare class NextHandler<METADATA, PARTS extends UIDataTypes, TOOLS extends UITools, ContextState extends Record<string, any>> {
276
+ private ctx;
277
+ private router;
278
+ private onExecutionStart;
279
+ private onExecutionEnd;
280
+ maxCallDepth: number;
281
+ constructor(ctx: AiContext<METADATA, PARTS, TOOLS, ContextState>, router: AiRouter<METADATA, PARTS, TOOLS, ContextState>, onExecutionStart: () => void, onExecutionEnd: () => void, parentNext?: NextHandler<METADATA, PARTS, TOOLS, ContextState>);
282
+ callAgent(agentPath: string, params?: Record<string, any>, options?: ToolCallOptions): Promise<{
283
+ ok: true;
284
+ data: any;
285
+ } | {
286
+ ok: false;
287
+ error: Error;
288
+ }>;
289
+ callTool<T extends z.ZodObject<any>>(toolPath: string, params: z.infer<T>, options?: ToolCallOptions): Promise<{
290
+ ok: true;
291
+ data: any;
292
+ } | {
293
+ ok: false;
294
+ error: Error;
295
+ }>;
296
+ attachTool<SCHEMA extends z.ZodObject<any>>(toolPath: string, _tool?: Omit<Tool<z.infer<SCHEMA>, any>, 'description'>): Tool<z.infer<SCHEMA>, any>;
297
+ agentAsTool<INPUT extends JSONValue | unknown | never = any, OUTPUT = any>(agentPath: string, toolDefinition?: Tool<INPUT, OUTPUT>): Tool<INPUT, OUTPUT>;
298
+ }
299
+
300
+ export { AgentDefinitionMissingError, AgentNotFoundError, type AiContext, type AiHandler, AiKitError, type AiLogger, type AiMiddleware, AiRouter, type AiStreamWriter, type AiToolFactory, type AiToolHandler, MaxCallDepthExceededError, type NextFunction, StreamWriter, ToolNotFoundError, ToolValidationError, type UITools, findFirstElement, findLastElement, findLastMessageWith, getTextParts, getTextPartsContent };