Repository navigation
refactor(llm): share client setup across providers - #1568
sisantaChhatoi wants to merge 1 commit into
Conversation
|
✅ OpenCodeReview: Review complete: 0 finding(s) across 3 selected item(s). |
ffb5776 to
52bd215
Compare
52bd215 to
3e3122b
Compare
Qiyuanqiii
left a comment
There was a problem hiding this comment.
I did a second pass including documentation impact, comments, naming, and maintainability.
The extracted helpers preserve the existing client behavior: protocol-specific URL normalization remains at the individual constructors, the middleware order is unchanged, per-request session-key expansion still follows the same precedence, and finalizeOnExit preserves both the named return error and panic propagation.
This is an internal refactor, so I don't see a user-facing documentation update that is required.
The comments are concise and useful. One minor naming debt is that sdkMiddlewares returns []retryObserver even though that common function type is also used for retry-code and raw-capture middleware. A future rename of the alias to something like sdkMiddleware would describe its role more accurately, but that alias predates this change and I would not block this refactor on it.
One merge-order note: #1565 changes the same client-construction area. If #1565 lands first, please make sure the rebase preserves its key-failover middleware and ordering; I would re-review the resulting head after that conflict is resolved.
For the current head against the current main, I have no blocking findings. Approved.
Extract the client defaults, SDK middleware stack, OpenAI SDK options, per-request session key and extra_body handling, and the deferred retry-report finalizer that the Chat Completions, Responses and Anthropic clients each duplicated.
3e3122b to
5f8dcd5
Compare
|
Heads up on the latest push: it's a rebase onto current |
Description
The Chat Completions, Responses and Anthropic/Bedrock clients each carried their own copy of the same setup code. This extracts the shared pieces into helpers. The behavior should not change. Outside tests the diff is +93/−148, and the only new comments are the ones that explain a constraint.
applyClientDefaults: the timeout and session-key defaults repeated in all four constructors.sdkMiddlewares: the retry-codes → raw → retry-observer middleware list. Both SDKs'option.Middlewarealias the same function type, so one list serves every constructor. The raw-before-observer ordering rule, which three constructors pointed back to with "see NewOpenAIClient", now lives in one place. In the Bedrock client, the middleware is still installed beforebedrock.WithConfig, so request signing still runs innermost.newOpenAISDK: the OpenAI SDK options shared by the Chat Completions and Responses clients. Callers pass the retry count and HTTP client, so the Responses constructor keeps its ChatGPT-plan setup (no retries, no redirect following) and the Chat client passes the same defaults it used before. URL normalization stays in each constructor, because the two keep different trailing-slash contracts oncfg.URL(seeTestNewOpenAIClient_URLNormalization).openAIRequestOptions: per-requestextra_headers/extra_bodyexpansion. Each client passes its own skipped keys (streamandstream_optionsfor Chat Completions,streamfor Responses), and each explanatory comment stays at its call site. In ChatGPT-plan mode the Responses client still sends its plan token first. The Anthropic loop keeps its own body handling because of itsthinkingrules, and only shares the session-key lookup (requestSessionKey).finalizeOnExit: the deferred finalize-and-recover closure copied into all threeCompletionsWithCtxmethods. It is deferred directly, sorecoverstill takes effect, and it appliesdescribeTimeoutto the returned error before finalizing, as each copy did. The reason a panic has to finalize stays documented onerrRequestPanicked.The request builders and response mappers are untouched, because that is where the Chat Completions and Responses wire formats actually differ.
Type of Change
How Has This Been Tested?
make testpasses locallymake checkpasses andmake coveragereports 92.1%. New tests forfinalizeOnExit: a panic is recorded as a failed request and still propagates, and the deferred call reads the error the function actually returned, not the valueerrheld when thedeferline ran. Until now the panic path was only tested at thefinalizeRequestlevel.TestNewOpenAIResponsesClientPlanModeSDKSettingsbuilds the Responses client in API-key and ChatGPT-plan mode against a local server and checks the retry count and redirect handling. The existing ChatGPT tests replace the SDK client, so the settings the constructor passes tonewOpenAISDKwere otherwise untested.TestRequestTimeoutAndSDKRetryBehaviorcoversdescribeTimeoutthrough all three clients.Checklist
go fmt,go vet)AI disclosure: prepared with Claude Code (Claude Opus 5.5). I reviewed every line and can explain each change.
Related Issues
None.
🤖 Generated with Claude Code