refactor(provider): deduplicate provider client boilerplate#3398
Merged
Conversation
Extract the code every provider client repeated into shared helpers, so adding a new provider needs less copy-paste: - options.Apply: opt-accumulation loop (6 constructors + factories) - options.ModelOptions.WrapTransport: HTTP transport wrapping (7 blocks) - base.Config.TrackUsageEnabled: track_usage default-on check (7 sites) - base.VerifyDockerGatewayAuth / base.GatewayAuthToken: Docker AI Gateway fail-fast check and per-request short-lived token fetch (6 blocks across anthropic, gemini, openai) - base.GatewayHTTPOptions: gateway httpclient option list (3 copies) - providerutil.ParseRerankScores: byte-identical rerank JSON parsing from openai and anthropic; now also surfaces score-count mismatches instead of a generic parse error All extractions are behavior-preserving (same error strings, log lines, and side-effect timing); new helpers are covered by direct unit tests.
rumpl
reviewed
Jul 1, 2026
|
|
||
| // parseRerankScoresAnthropic parses a JSON payload of the form {"scores":[...]} and validates length. | ||
| // This helper is local to the Anthropic provider to avoid cyclic dependencies. | ||
| func parseRerankScoresAnthropic(raw string, expected int) ([]float64, error) { |
Member
There was a problem hiding this comment.
This was moved to some util thing, but it's only used by anthropic, why would we move it and make it public?
Member
Author
There was a problem hiding this comment.
Because it's used by openai too?
trungutt
approved these changes
Jul 2, 2026
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.
The five provider clients (openai, anthropic, gemini, bedrock, dmr) each
contained several identical blocks of setup code, making every new provider a
copy-paste exercise and any cross-cutting fix a multi-file hunt. This PR
extracts those repeated patterns into shared helpers with no observable
behavior change.
Six kinds of duplication were collapsed: the option-accumulation loop that all
constructors and factories ran (
options.Apply); the HTTP transport-wrappingblock repeated in seven places (
options.ModelOptions.WrapTransport); thetrack_usageflag check present at seven call sites (base.Config.TrackUsageEnabled);the Docker AI Gateway fail-fast and per-request short-lived token fetch that
appeared in six constructors (
base.VerifyDockerGatewayAuthandbase.GatewayAuthToken); the gatewayhttpclientoption list copied threetimes (
base.GatewayHTTPOptions); and the rerank-score JSON parser that wasbyte-identical in openai and anthropic (
providerutil.ParseRerankScores). Onesmall behavior improvement is folded in:
ParseRerankScoresnow returns"expected N scores, got M" when the JSON is valid but the score count is wrong,
instead of the generic "invalid rerank JSON" error. All other error strings,
log lines, and side-effect timing are preserved exactly — the per-request token
fetch still happens inside its gateway closure.
New unit tests cover every extracted helper directly, and the full test suite,
linter, race detector, and
GOOS=js GOARCH=wasmbuild all pass.