@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 +34 -0
- package/README.md +227 -0
- package/dist/index.d.mts +300 -0
- package/dist/index.d.ts +300 -0
- package/dist/index.js +960 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +916 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +54 -0
package/dist/index.d.ts
ADDED
|
@@ -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 };
|