Skip to content

feat(agent): add addRouter method for custom HTTP routes#1454

Open
Scra3 wants to merge 2 commits intomainfrom
feat/agent-add-router
Open

feat(agent): add addRouter method for custom HTTP routes#1454
Scra3 wants to merge 2 commits intomainfrom
feat/agent-add-router

Conversation

@Scra3
Copy link
Member

@Scra3 Scra3 commented Feb 6, 2026

Summary

  • Add agent.addRouter() method allowing developers to define custom HTTP routes
  • Support authenticated routes (default) that run after Forest Admin auth middleware
  • Support public routes (authenticated: false) that run before auth
  • Provide context with dataSource, services, options, and logger
  • Optional prefix option for route prefixing

Usage Examples

Simple authenticated route

agent.addRouter((router, context) => {
  router.get('/stats', async (ctx) => {
    const users = await context.dataSource
      .getCollection('users')
      .list(caller, new PaginatedFilter({}), new Projection('id'));
    ctx.body = { count: users.length };
  });
});

Public route (no auth)

agent.addRouter(
  (router) => {
    router.get('/health', (ctx) => {
      ctx.body = { status: 'ok' };
    });
  },
  { authenticated: false }
);

Test plan

  • Test route type returns CustomRoute when authenticated (default)
  • Test route type returns PublicCustomRoute when authenticated=false
  • Test callback receives router and context
  • Test context provides dataSource, services, options, logger
  • Test prefix is applied correctly
  • Test addRouter returns agent for chaining
  • Test multiple addRouter calls work

🤖 Generated with Claude Code

Allow developers to add custom HTTP routes to the agent via addRouter().

Features:
- Authenticated routes (default) run after Forest Admin auth middleware
- Public routes (authenticated: false) run before auth middleware
- Optional prefix for all routes in a router
- Context provides dataSource, services, options, and logger

Example usage:
```typescript
agent.addRouter((router, context) => {
  router.get('/stats', async (ctx) => {
    const users = await context.dataSource
      .getCollection('users')
      .list(caller, filter, projection);
    ctx.body = { count: users.length };
  });
});
```

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@qltysh
Copy link

qltysh bot commented Feb 6, 2026

2 new issues

Tool Category Rule Count
qlty Structure Function with high complexity (count = 19): addAi 1
qlty Structure Function with many parameters (count = 5): constructor 1

@qltysh
Copy link

qltysh bot commented Feb 6, 2026

Qlty

Coverage Impact

⬆️ Merging this pull request will increase total coverage on main by 0.01%.

Modified Files with Diff Coverage (4)

RatingFile% DiffUncovered Line #s
Coverage rating: A Coverage rating: A
packages/agent/src/agent.ts100.0%
Coverage rating: A Coverage rating: A
packages/agent/src/types.ts100.0%
New file Coverage rating: A
packages/agent/src/routes/custom/custom-route.ts100.0%
New file Coverage rating: A
packages/agent/src/routes/custom/custom-router-context.ts100.0%
Total100.0%
🚦 See full report on Qlty Cloud »

🛟 Help
  • Diff Coverage: Coverage for added or modified lines of code (excludes deleted files). Learn more.

  • Total Coverage: Coverage for the whole repository, calculated as the sum of all File Coverage. Learn more.

  • File Coverage: Covered Lines divided by Covered Lines plus Missed Lines. (Excludes non-executable lines including blank lines and comments.)

    • Indirect Changes: Changes to File Coverage for files that were not modified in this PR. Learn more.

- Move AI proxy route logic from AiProxyRoute class to addAi() method
- addAi() now uses addRouter() internally to register the AI proxy route
- Remove AiProxyRoute class and its tests (no longer needed)
- Simplify makeRoutes() by removing aiConfigurations parameter

This proves that addRouter() can be used for internal routes,
making the architecture more consistent.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant