-
Notifications
You must be signed in to change notification settings - Fork 10
refactor(agent): decouple ai-proxy via factory injection pattern #1455
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Scra3
wants to merge
25
commits into
main
Choose a base branch
from
refactor/decouple-ai-proxy-factory-injection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+549
−391
Open
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
806e172
refactor(agent): decouple ai-proxy via factory injection pattern (cre…
1ac8cee
refactor(agent): use providers array in AiProviderDefinition for futu…
2ee88d2
docs(agent): document @forestadmin/ai-proxy install requirement in addAi
6e9b286
fix(ai-proxy): address PR review findings
195ab8b
refactor(datasource-toolkit): extract AiProviderMeta named type
48f2dc0
refactor(ai-proxy): make AIError extend BusinessError for native erro…
69a07da
feat(agent): log warning when AI configuration is added via addAi()
9fb2ab2
refactor(datasource-toolkit): add model to AiProviderMeta
2e61944
fix(agent): send only name and provider in ai_llms schema metadata
47be2b6
fix(ai-proxy): address PR review findings
8eef1f0
fix(agent): fix prettier formatting issues
6e8aecb
refactor(ai-proxy): move OAuth injection from Router to createAiProvi…
11c282b
refactor(agent): remove dead AINotConfiguredError handling from ai-pr…
7010f07
fix(ai-proxy): fix lint and prettier errors
af9f3b4
refactor(ai-proxy): remove any cast from createAiProvider
e51c18f
refactor(agent): remove @forestadmin/ai-proxy from peerDependencies
ee1dcc7
refactor(ai-proxy): extract resolveMcpConfigs to improve readability
83a0a87
refactor(ai-proxy): extract RouterRouteArgs type alias
dbd9d2e
refactor: rename requestHeaders to headers in AiRouter interface
0d6f622
refactor(ai-proxy): remove error hierarchy comment
f4ad26b
docs(agent): add documentation link to addAi() JSDoc
0083fe1
docs(agent): add documentation link to mountAiMcpServer() JSDoc
4002d49
refactor: move AI types from datasource-toolkit to agent-toolkit
e5b507a
fix: address PR review findings
bcba958
feat: add model field to ai_llms schema metadata
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| export {}; | ||
| export type { AiProviderDefinition, AiProviderMeta, AiRouter } from './interfaces/ai'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| /** | ||
| * Inlined from datasource-toolkit/factory.ts to avoid a dependency on a lower-level package. | ||
| * Must stay structurally compatible with datasource-toolkit's Logger & LoggerLevel. | ||
| * If those types change, update these copies accordingly. | ||
| */ | ||
| export type LoggerLevel = 'Debug' | 'Info' | 'Warn' | 'Error'; | ||
| export type Logger = (level: LoggerLevel, message: string, error?: Error) => void; | ||
|
|
||
| /** Metadata describing a configured AI provider, used in schema reporting and logging. */ | ||
| export interface AiProviderMeta { | ||
| name: string; | ||
| provider: string; | ||
| model: string; | ||
| } | ||
|
|
||
| export interface AiRouter { | ||
| /** | ||
| * Route a request to the AI proxy. | ||
| * | ||
| * Implementations should throw BusinessError subclasses (BadRequestError, NotFoundError, | ||
| * UnprocessableError) for proper HTTP status mapping by the agent's error middleware. | ||
| */ | ||
| route(args: { | ||
| route: string; | ||
| body?: unknown; | ||
| query?: Record<string, string | string[] | undefined>; | ||
| mcpServerConfigs?: unknown; | ||
| headers?: Record<string, string | string[] | undefined>; | ||
| }): Promise<unknown>; | ||
| } | ||
|
|
||
| export interface AiProviderDefinition { | ||
| providers: AiProviderMeta[]; | ||
| init(logger: Logger): AiRouter; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,74 +1,40 @@ | ||
| import type { ForestAdminHttpDriverServices } from '../../services'; | ||
| import type { AgentOptionsWithDefaults, AiConfiguration } from '../../types'; | ||
| import type { AgentOptionsWithDefaults } from '../../types'; | ||
| import type { AiRouter } from '@forestadmin/agent-toolkit'; | ||
| import type KoaRouter from '@koa/router'; | ||
| import type { Context } from 'koa'; | ||
|
|
||
| import { | ||
| AIBadRequestError, | ||
| AIError, | ||
| AINotConfiguredError, | ||
| AINotFoundError, | ||
| Router as AiProxyRouter, | ||
| extractMcpOauthTokensFromHeaders, | ||
| injectOauthTokens, | ||
| } from '@forestadmin/ai-proxy'; | ||
| import { | ||
| BadRequestError, | ||
| NotFoundError, | ||
| UnprocessableError, | ||
| } from '@forestadmin/datasource-toolkit'; | ||
|
|
||
| import { HttpCode, RouteType } from '../../types'; | ||
| import BaseRoute from '../base-route'; | ||
|
|
||
| export default class AiProxyRoute extends BaseRoute { | ||
| readonly type = RouteType.PrivateRoute; | ||
| private readonly aiProxyRouter: AiProxyRouter; | ||
| private readonly aiRouter: AiRouter; | ||
|
|
||
| constructor( | ||
| services: ForestAdminHttpDriverServices, | ||
| options: AgentOptionsWithDefaults, | ||
| aiConfigurations: AiConfiguration[], | ||
| aiRouter: AiRouter, | ||
| ) { | ||
| super(services, options); | ||
| this.aiProxyRouter = new AiProxyRouter({ | ||
| aiConfigurations, | ||
| logger: this.options.logger, | ||
| }); | ||
| this.aiRouter = aiRouter; | ||
| } | ||
|
|
||
| setupRoutes(router: KoaRouter): void { | ||
| router.post('/_internal/ai-proxy/:route', this.handleAiProxy.bind(this)); | ||
| } | ||
|
|
||
| private async handleAiProxy(context: Context): Promise<void> { | ||
| try { | ||
| const tokensByMcpServerName = extractMcpOauthTokensFromHeaders(context.request.headers); | ||
|
|
||
| const mcpConfigs = | ||
| await this.options.forestAdminClient.mcpServerConfigService.getConfiguration(); | ||
|
|
||
| context.response.body = await this.aiProxyRouter.route({ | ||
| route: context.params.route, | ||
| body: context.request.body, | ||
| query: context.query, | ||
| mcpConfigs: injectOauthTokens({ mcpConfigs, tokensByMcpServerName }), | ||
| }); | ||
| context.response.status = HttpCode.Ok; | ||
| } catch (error) { | ||
| if (error instanceof AIError) { | ||
| this.options.logger('Error', `AI proxy error: ${error.message}`, error); | ||
|
|
||
| if (error instanceof AINotConfiguredError) { | ||
| throw new UnprocessableError('AI is not configured. Please call addAi() on your agent.'); | ||
| } | ||
|
|
||
| if (error instanceof AIBadRequestError) throw new BadRequestError(error.message); | ||
| if (error instanceof AINotFoundError) throw new NotFoundError(error.message); | ||
| throw new UnprocessableError(error.message); | ||
| } | ||
|
|
||
| throw error; | ||
| } | ||
| const mcpServerConfigs = | ||
| await this.options.forestAdminClient.mcpServerConfigService.getConfiguration(); | ||
|
|
||
| context.response.body = await this.aiRouter.route({ | ||
| route: context.params.route, | ||
| body: context.request.body, | ||
| query: context.query, | ||
| headers: context.request.headers, | ||
| mcpServerConfigs, | ||
| }); | ||
| context.response.status = HttpCode.Ok; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
model could be interesting for analyze