refactor(agent): decouple ai-proxy via factory injection pattern#1455
refactor(agent): decouple ai-proxy via factory injection pattern#1455
Conversation
1 new issue
|
Bundle size impactBy making Dependencies removed from default install:
This represents a ~9% reduction of the total monorepo Users who need AI features simply add |
|
Coverage Impact ⬆️ Merging this pull request will increase total coverage on Modified Files with Diff Coverage (7)
🛟 Help
|
adb2381 to
b77d9af
Compare
Breaking change:
|
Code Review SummaryReviewed 22 changed files across Result: No issues found ✅The refactoring cleanly decouples
🤖 Generated with Claude Code |
| liana_version: string; | ||
| liana_features: Record<string, string> | null; | ||
| ai_llms?: Array<{ provider: string }> | null; | ||
| ai_llms?: Array<{ name: string; provider: string }> | null; |
| @@ -0,0 +1,29 @@ | |||
| import type { Logger } from '../factory'; | |||
There was a problem hiding this comment.
Not very relevant to have ai concern here because it's not a DS but I don't want to create an other package just for this.
…ateAiProvider) Move @forestadmin/ai-proxy from hard dependency to optional peer dependency by introducing a factory injection pattern. Users who need AI features now explicitly install ai-proxy and inject it via createAiProvider(), following the same pattern as addDataSource(createSqlDataSource(...)). This eliminates ~6 langchain packages from the default install for users who don't use AI features. Co-Authored-By: Claude Opus 4.6 <[email protected]>
…re multi-provider support
Change AiProviderDefinition interface from single name/provider to
providers: Array<{name, provider}> so that createAiProvider can later
accept variadic configs without breaking changes.
Co-Authored-By: Claude Opus 4.6 <[email protected]>
Co-Authored-By: Claude Opus 4.6 <[email protected]>
- Remove redundant .map() identity transform in schema generator - Add runtime validation for mcpServerConfigs before unsafe cast - Add JSDoc documenting AiRouter error contract - Fix lint/prettier formatting issues - Add tests for resolveMcpConfigs path (OAuth injection, backward compat, invalid input) Co-Authored-By: Claude Opus 4.6 <[email protected]>
Replace inline `{ name: string; provider: string }` with a named
`AiProviderMeta` interface in datasource-toolkit. Use it consistently
in AiProviderDefinition, SchemaGenerator, and ForestSchema.
Co-Authored-By: Claude Opus 4.6 <[email protected]>
…r handling AIError and subclasses now extend BusinessError from datasource-toolkit (BadRequestError, NotFoundError, UnprocessableError). The agent's error middleware handles HTTP status mapping natively, removing the need for duck-typed status checks and error re-wrapping in the route handler. Co-Authored-By: Claude Opus 4.6 <[email protected]>
Co-Authored-By: Claude Opus 4.6 <[email protected]>
Include the model name in AiProviderMeta alongside name and provider, so the schema metadata and agent logs accurately reflect the full AI configuration (provider, model, name). Co-Authored-By: Claude Opus 4.6 <[email protected]>
Keep ai_llms schema payload consistent with main: only { name, provider }
are sent to the server. The model field remains in AiProviderMeta for
agent-side logging only.
Co-Authored-By: Claude Opus 4.6 <[email protected]>
- Restore "Please call addAi()" guidance for AINotConfiguredError - Add error inheritance tests verifying correct BusinessError subclasses - Add missing tests: addAi logging, mcpServerConfigs null, error types - Improve error hierarchy comment with tree diagram Co-Authored-By: Claude Opus 4.6 <[email protected]>
Co-Authored-By: Claude Opus 4.6 <[email protected]>
…der wrapper Keep router.ts unchanged from main. The OAuth token extraction and injection logic now lives in the createAiProvider wrapper instead. Co-Authored-By: Claude Opus 4.6 <[email protected]>
…oxy route The route is only registered when addAi() is called, so this error path is unreachable. Co-Authored-By: Claude Opus 4.6 <[email protected]>
Co-Authored-By: Claude Opus 4.6 <[email protected]>
Use RouteArgs type assertion instead of any. Co-Authored-By: Claude Opus 4.6 <[email protected]>
The agent no longer imports anything from ai-proxy at runtime. Co-Authored-By: Claude Opus 4.6 <[email protected]>
Co-Authored-By: Claude Opus 4.6 <[email protected]>
Co-Authored-By: Claude Opus 4.6 <[email protected]>
Co-Authored-By: Claude Opus 4.6 <[email protected]>
Co-Authored-By: Claude Opus 4.6 <[email protected]>
Co-Authored-By: Claude Opus 4.6 <[email protected]>
Co-Authored-By: Claude Opus 4.6 <[email protected]>
AI provider interfaces (AiProviderMeta, AiRouter, AiProviderDefinition) belong in agent-toolkit rather than datasource-toolkit, which is a lower-level package that shouldn't contain AI-specific contracts. Co-Authored-By: Claude Opus 4.6 <[email protected]>
df10793 to
71ff0a4
Compare
- Add comment explaining RouterRouteArgs cast safety (Zod runtime validation) - Strengthen Logger/LoggerLevel inlining comment in agent-toolkit - Add tests for invalid JSON and absent x-mcp-oauth-tokens header - Add test for addAi logging with multiple providers - Remove redundant weak AiRouter type-check test - Add comment on init() called per-restart in agent.ts Co-Authored-By: Claude Opus 4.6 <[email protected]>

Summary
createAiProvider()factory inai-proxyfollowing the existingaddDataSource(createSqlDataSource(...))patternAiRouter/AiProviderDefinitioninterfaces indatasource-toolkit@forestadmin/ai-proxyimports from agent source codecreateAiProviderwrapper@forestadmin/ai-proxyfully removed from agent dependencies (not even a peer dep)Before:
After:
This eliminates ~6 langchain packages from the default install for users who don't use AI features.
Multiple AI support (future)
When we need to support multiple AI configurations, only
createAiProviderneeds to change:No changes needed in agent -
addAialready acceptsAiProviderDefinitionwhich hasproviders: AiProviderMeta[]. TheRouteralready handles config selection via theai-namequery parameter.Test plan
datasource-toolkit,ai-proxy,agent)datasource-toolkittests passai-proxytests pass (99 tests) includingcreateAiProviderwrapper testsagenttests pass (883 tests)ai-proxynot present in agent package.json at allpackages/ai-proxy/src/router.tsGenerated with Claude Code