@microfox/ai-router 2.0.1-beta.2 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/README.md +94 -59
  3. package/package.json +5 -2
package/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # @microfox/ai-router
2
2
 
3
+ ## 2.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 58300f5: Deletion of Tools & Zod Issues fixed
8
+
9
+ ### Patch Changes
10
+
11
+ - 07d4a18: beta release
12
+ - 2a62227: added filter options to agentAsTool
13
+
14
+ ## 2.1.0-beta.3
15
+
16
+ ### Minor Changes
17
+
18
+ - 58300f5: Deletion of Tools & Zod Issues fixed
19
+
3
20
  ## 2.0.1-beta.2
4
21
 
5
22
  ### Patch Changes
package/README.md CHANGED
@@ -254,24 +254,17 @@ router.agent('/path', async (ctx) => {
254
254
  });
255
255
  ```
256
256
 
257
- ### Tools
257
+ ### Agent-as-Tools
258
258
 
259
- Tools are functions that can be called by agents or other tools. They support both static and factory-based definitions.
259
+ Agents can be exposed as tools for LLM integration using the `.actAsTool()` method. This provides better type safety and seamless integration with AI SDK functions.
260
260
 
261
261
  ```typescript
262
- // Static tool
263
- router.tool(
264
- '/calculator',
265
- {
266
- schema: z.object({
267
- a: z.number(),
268
- b: z.number(),
269
- operation: z.enum(['add', 'subtract', 'multiply', 'divide']),
270
- }),
271
- description: 'Performs basic arithmetic operations',
272
- },
273
- async (ctx, params) => {
274
- const { a, b, operation } = params;
262
+ // Create a calculator agent
263
+ const calculatorAgent = new AiRouter();
264
+
265
+ calculatorAgent
266
+ .agent('/', async (ctx) => {
267
+ const { a, b, operation } = ctx.request.params;
275
268
  let result;
276
269
 
277
270
  switch (operation) {
@@ -289,20 +282,30 @@ router.tool(
289
282
  break;
290
283
  }
291
284
 
292
- return result;
293
- }
294
- );
295
-
296
- // Factory-based tool
297
- router.tool('/dynamic-tool', (ctx) => {
298
- return {
299
- description: 'A dynamic tool',
300
- inputSchema: z.object({ message: z.string() }),
301
- execute: async (params) => {
302
- return `Processed: ${params.message}`;
285
+ return { result };
286
+ })
287
+ .actAsTool('/', {
288
+ id: 'calculator',
289
+ name: 'Calculator',
290
+ description: 'Performs basic arithmetic operations',
291
+ inputSchema: z.object({
292
+ a: z.number().describe('First number'),
293
+ b: z.number().describe('Second number'),
294
+ operation: z
295
+ .enum(['add', 'subtract', 'multiply', 'divide'])
296
+ .describe('Operation to perform'),
297
+ }),
298
+ outputSchema: z.object({
299
+ result: z.number().describe('The calculation result'),
300
+ }),
301
+ metadata: {
302
+ icon: '🧮',
303
+ category: 'math',
303
304
  },
304
- };
305
- });
305
+ });
306
+
307
+ // Mount the agent
308
+ router.agent('/calculator', calculatorAgent);
306
309
  ```
307
310
 
308
311
  ### Middleware
@@ -399,21 +402,40 @@ router.agent('/users/:userId/profile', async (ctx) => {
399
402
  });
400
403
  });
401
404
 
402
- router.tool(
403
- '/posts/:postId/comments/:commentId',
404
- {
405
- schema: z.object({
406
- action: z.enum(['read', 'update', 'delete']),
407
- }),
408
- description: 'Manage post comments',
409
- },
410
- async (ctx, params) => {
405
+ // Create a comment management agent
406
+ const commentAgent = new AiRouter();
407
+
408
+ commentAgent
409
+ .agent('/:commentId', async (ctx) => {
411
410
  const { postId, commentId } = ctx.request.params;
412
- const { action } = params;
411
+ const { action } = ctx.request.params;
412
+
413
+ return {
414
+ message: `Performing ${action} on comment ${commentId} of post ${postId}`,
415
+ postId,
416
+ commentId,
417
+ action,
418
+ };
419
+ })
420
+ .actAsTool('/:commentId', {
421
+ id: 'comment-manager',
422
+ name: 'Comment Manager',
423
+ description: 'Manage post comments',
424
+ inputSchema: z.object({
425
+ action: z
426
+ .enum(['read', 'update', 'delete'])
427
+ .describe('Action to perform'),
428
+ }),
429
+ outputSchema: z.object({
430
+ message: z.string().describe('Result message'),
431
+ postId: z.string().describe('Post ID'),
432
+ commentId: z.string().describe('Comment ID'),
433
+ action: z.string().describe('Action performed'),
434
+ }),
435
+ });
413
436
 
414
- return `Performing ${action} on comment ${commentId} of post ${postId}`;
415
- }
416
- );
437
+ // Mount the agent with dynamic path
438
+ router.agent('/posts/:postId/comments', commentAgent);
417
439
  ```
418
440
 
419
441
  ### State Management
@@ -686,8 +708,6 @@ The `next` object provides methods for agent-to-agent communication:
686
708
  ```typescript
687
709
  interface NextHandler<Metadata, Parts, Tools, State> {
688
710
  callAgent(path: string, params?: Record<string, any>): Promise<Result>;
689
- callTool(path: string, params: any): Promise<Result>;
690
- attachTool(path: string): Tool<any, any>; // Use to expose a standard .tool() to an LLM.
691
711
  agentAsTool(
692
712
  path: string,
693
713
  options?: Record<string, any> & { disableAllInputs?: boolean }
@@ -736,8 +756,6 @@ The `next.agentAsTool()` method allows you to expose an agent as a tool to an LL
736
756
  The router provides several error types for different scenarios:
737
757
 
738
758
  - `AgentNotFoundError`: No agent found for the requested path
739
- - `ToolNotFoundError`: No tool found for the requested path
740
- - `ToolValidationError`: Tool parameter validation failed
741
759
  - `MaxCallDepthExceededError`: Agent call depth limit exceeded
742
760
  - `AgentDefinitionMissingError`: Agent used as tool without definition
743
761
 
@@ -802,20 +820,37 @@ api.use('*', async (ctx, next) => {
802
820
  });
803
821
 
804
822
  // A specific tool for searching users, which is better for the LLM to use
805
- api.tool(
806
- '/search/users',
807
- {
808
- schema: z.object({
823
+ // Create a user search agent
824
+ const userSearchAgent = new AiRouter();
825
+
826
+ userSearchAgent
827
+ .agent('/', async (ctx) => {
828
+ const { query } = ctx.request.params;
829
+ // In a real app, you would have database logic here
830
+ // const users = await db.searchUsers(query);
831
+ const users = [{ id: '123', name: 'John Doe' }];
832
+ return {
833
+ users,
834
+ query,
835
+ count: users.length,
836
+ };
837
+ })
838
+ .actAsTool('/', {
839
+ id: 'user-search',
840
+ name: 'User Search',
841
+ description: 'Searches for users based on a query',
842
+ inputSchema: z.object({
809
843
  query: z.string().describe('The search query for users'),
810
844
  }),
811
- description: 'Searches for users based on a query.',
812
- },
813
- async (ctx, params) => {
814
- // In a real app, you would have database logic here
815
- // return await db.searchUsers(params.query);
816
- return { users: [{ id: '123', name: 'John Doe' }] };
817
- }
818
- );
845
+ outputSchema: z.object({
846
+ users: z.array(z.object({ id: z.string(), name: z.string() })),
847
+ query: z.string(),
848
+ count: z.number(),
849
+ }),
850
+ });
851
+
852
+ // Mount the user search agent
853
+ api.agent('/search/users', userSearchAgent);
819
854
 
820
855
  // AI-powered search
821
856
  api.agent('/search', async (ctx) => {
@@ -825,11 +860,11 @@ api.agent('/search', async (ctx) => {
825
860
  model: openai('gpt-4'),
826
861
  prompt: `Fulfill this search request: ${query}`,
827
862
  tools: {
828
- userSearch: ctx.next.attachTool('/search/users'),
863
+ userSearch: ctx.next.agentAsTool('/search/users'),
829
864
  },
830
865
  });
831
866
 
832
- ctx.response.write({ type: 'text', text });
867
+ return { response: text };
833
868
  });
834
869
  ```
835
870
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@microfox/ai-router",
3
- "version": "2.0.1-beta.2",
3
+ "version": "2.1.0",
4
4
  "description": "A Router-based Framework built on top of ai-sdk to support Multi-Agentic Workflows.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
@@ -32,10 +32,13 @@
32
32
  }
33
33
  },
34
34
  "dependencies": {
35
- "ai": "^5.0.30",
35
+ "ai": "^5.0.51",
36
36
  "async-lock": "^1.4.1",
37
37
  "zod": "^4.1.5"
38
38
  },
39
+ "peerDependencies": {
40
+ "ai": ">=5.0.47"
41
+ },
39
42
  "devDependencies": {
40
43
  "@types/node": "^20.14.2",
41
44
  "@typescript-eslint/eslint-plugin": "^6.0.0",