feat(provider): add sub-provider accessors for custom-named gateway providers - #105
Merged
Merged
Conversation
…roviders Change-Id: I8b538132266fcff1c57a1dd430be2d945ce25dee Signed-off-by: Thomas Kosiewski <tk@coder.com>
Member
Author
|
@codex review |
Member
Author
|
@codex security review |
|
Codex Review: Didn't find any major issues. What shall we delve into next? Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
Security review completed. No security issues were found in this pull request. Reviewed commit: Only the user who started this review can view the report in Codex. ℹ️ About Codex security reviews in GitHubThis is an experimental Codex feature. Security reviews are triggered when:
Once complete, Codex will leave suggestions, or a comment if no findings are found. |
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 #104.
Adds sub-provider factories to
CoderProviderso one instance can target any admin-defined AI Gateway provider, per the accepted design in #104 (explicit accessors; prefix routing rejected there):API surface
coder.openaiProvider(name)— OpenAI-compatible sub-provider bound to<base>/<gatewayPath>/<name>/v1.coder.anthropicProvider(name)— Anthropic-compatible sub-provider, same URL scheme.nameis validated at accessor call time against the gateway's provider-name grammar (^[a-z0-9]+(-[a-z0-9]+)*$); mismatch throws the AI SDK's typedInvalidArgumentError(argument: "name") before any request is emitted — same fail-fast philosophy as the embeddings guard (provider: textEmbeddingModel targets /v1/embeddings, which AI Gateway rejects with 404 #69/fix(provider): fail fast with NoSuchModelError for textEmbeddingModel #80).openaiModel(name, id)sugar —openaiProvider(name)(id)covers it; kept the surface minimal per the issue's guidance.One construction path
The default
openai/anthropicsurfaces are now built by the same factories (openaiProvider(DEFAULT_OPENAI_PROVIDER)etc.), so custom-named sub-providers inherit every default behavior: auth mode matrix (centralized/BYOK), header merging, custom fetch,includeUsage, and the fail-fast embeddings guard. Two deliberate, strictly-fail-faster consequences to note:settings.providersrename values are now validated against the same grammar atcreateCodertime (previously a bad rename surfaced as a request-time 404; such names can never be registered server-side, so no working config breaks).coder.<name>(e.g.coder.azure-openai.chat) instead of alwayscoder.openai; defaults are byte-identical to before (coder.openai/coder.anthropic), which the unit tests pin via unchanged existing assertions.README
Rewrote the provider README's "The two surfaces" section as "Named providers and the two wire protocols": the two protocols with their upstream types and default names, the new accessors with the
azure-openai/anthropic-bedrockexample, the existingsettings.providersre-point option, and an explicit note that provider discovery is admin-only server-side (GET /api/v2/ai/providers→ 403 for regular users; get names from your platform team). Updated the root README anchor and the stale~175 linesclaim in the security section. All snippets typecheck (scratch file assembled inexamples/,pnpm --filter @coder/ai-sdk-provider typecheck, then deleted).Validation
Unit (source-verified): 22 tests pass — 15 pre-existing unchanged (defaults pinned), 7 new: exact request URL for custom names on both protocols (captured fetch), custom
aiGatewayPath,openaiProvider("openai")≡ default surface, grammar rejection (7 malformed names × both accessors,InvalidArgumentError.isInstance, zero requests), embeddings fail-fast on named sub-providers,settings.providersgrammar validation.Gates:
pnpm check,pnpm -r build,pnpm -r test(494 tests),pnpm publint,pnpm attw— all green.Live (dev.coder.com, v2.36.x):
coder("gpt-4o-mini")andcoder.openaiProvider("openai")("gpt-4o-mini")emitted byte-identical request URLs (…/api/v2/aibridge/openai/v1/chat/completions) and both completed (finishReason: stop, same reply).coder.openaiProvider("does-not-exist")("gpt-4o-mini")→ cleanAI_APICallError,statusCode: 404, URL…/aibridge/does-not-exist/v1/chat/completions, bodyroute not supported: POST /does-not-exist/v1/chat/completions.coder.openaiProvider("Does_Not_Exist")→ client-sideAI_InvalidArgumentError, zero requests emitted.Honesty tier: dev.coder.com only defines the default
openai/anthropicproviders, so custom-name routing to a real custom provider is verified by unit-level URL construction plus the live 404 shape above (which proves the URL reaches the gateway's per-name router), not by a live custom-provider round-trip.Generated with
mux• Model:anthropic:claude-fable-5• Thinking:xhigh