feat(router): smart router for unified LLM provider endpoints#551
Open
khoaminh2957 wants to merge 1 commit into
Open
feat(router): smart router for unified LLM provider endpoints#551khoaminh2957 wants to merge 1 commit into
khoaminh2957 wants to merge 1 commit into
Conversation
…koodev#286) Adds a litellm-style smart router under JS/edgechains/arakoodev/src/ai/src/lib/router that exposes a single, provider-agnostic chat-completion API and dispatches to the appropriate provider based on the model prefix (gpt-* -> OpenAI, claude-* -> Anthropic, gemini-* -> Google, command* -> Cohere). Explicit "provider/model" overrides and runtime adapter registration are also supported. Includes 20 vitest cases covering provider resolution, per-adapter request/response shaping (with axios mocked), router dispatch, default provider fallback, error wrapping, and request validation. Closes arakoodev#286
|
CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅ |
Author
|
I have read the Arakoo CLA Document and I hereby sign the CLA |
Author
|
recheck |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #286
/claim #286
Summary
Adds a
SmartRouterclass (inspired by Python's litellm) that exposes a single, provider-agnostic chat-completion API and dispatches requests to the appropriate provider based on the model prefix.Lives at
JS/edgechains/arakoodev/src/ai/src/lib/router/and is re-exported from@arakoodev/edgechains.js/aiso it sits alongside the existingOpenAI/GeminiAI/LlamaAIclients rather than replacing them.API
Routing rules
gpt-*,o1-*,o3-*openaiclaude-*anthropicgemini-*,models/gemini-*googlecommand*coherellama-*,llama3-*llama(registerable)Explicit
"provider/model"syntax (e.g."openai/my-finetune") overrides the prefix inference. An optionaldefaultProviderhandles unknown prefixes.Implementation
SmartRouter.ts— dispatcher withcomplete(),register(),supports(),listProviders(), plus a typedRouterErrorthat preserves provider name, HTTP status, and original cause.providerResolver.ts— pure prefix/explicit resolution logic.adapters/{openai,anthropic,google,cohere}Adapter.ts— per-provider adapters that translate the normalized request to the provider's wire format and normalize the response back. Anthropic'ssystemfield, Gemini'ssystemInstruction, and Cohere'spreamble+chat_historyare derived automatically from the flat router message list.axios, which is already a dependency of theaipackage — no new deps added.router.register(name, adapter).Tests
20 vitest cases in
src/ai/src/tests/smartRouter.test.tscovering:provider/modelform + unknown handling.axios.postmocked — no real network calls).RouterErrorpaths for unknown models, missing adapters, invalid input, and upstream failures.Scope of this MVP
register()— built-in adapter intentionally deferred to a follow-up.Test plan
npm run buildpasses (tsc -bclean)npx vitest run src/ai/src/tests/smartRouter.test.ts— 20/20)src/ai/src/index.ts🤖 Generated with Claude Code