refactor(openai-shim): extract stream lifecycle and response dispatch - #2009
Conversation
📝 WalkthroughWalkthroughThe OpenAI shim now delegates request dispatch, stream cancellation, response conversion, error handling, and response metadata to a new ChangesOpenAI shim dispatch
Estimated code review effort: 4 (Complex) | ~45 minutes Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
6af5f54 to
dfa8d82
Compare
3f6383d to
dd8abd8
Compare
bf77acc to
aee66a9
Compare
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/services/api/openaiShim/clientDispatch.ts`:
- Around line 41-59: Update ClientDispatchDependencies in
src/services/api/openaiShim/clientDispatch.ts lines 41-59 to inject
codexStreamToAnthropic, collectCodexCompletedResponse, and
convertCodexResponseToAnthropicMessage, then use those dependencies in
createShimRequest instead of direct imports. In
src/services/api/openaiShim/clientDispatch.test.ts lines 131-150, extend the
dispatch table with a codex/responses streaming case and add a non-streaming
case covering the output or incomplete_details detection branch.
- Around line 201-220: Extract the nested stream-converter selection from the
OpenAIShimStream constructor into a small selectStreamConverter(request,
response) helper. Preserve the existing routing precedence and arguments for
codex/responses, messages, Gemini, and OpenAI/Ollama streams, then invoke the
helper from the constructor callback so the routing logic is centralized and
testable.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: bc366074-fead-4584-9a6b-6fb16f7e0321
📒 Files selected for processing (3)
src/services/api/openaiShim.tssrc/services/api/openaiShim/clientDispatch.test.tssrc/services/api/openaiShim/clientDispatch.ts
📜 Review details
⏰ Context from checks skipped due to timeout. (1)
- GitHub Check: smoke-and-tests (24.11.x)
🧰 Additional context used
📓 Path-based instructions (4)
**/*.{ts,tsx}
📄 CodeRabbit inference engine (AGENTS.md)
TypeScript code in this repository must use strict mode and ESM imports.
**/*.{ts,tsx}: Add or update tests when a TypeScript or TSX change affects behavior.
Run the relevant TypeScript validation checks for changed code, includingbun run typecheckand, when applicable,bun run typecheck:type-tests.
Files:
src/services/api/openaiShim/clientDispatch.test.tssrc/services/api/openaiShim.tssrc/services/api/openaiShim/clientDispatch.ts
**/*
📄 CodeRabbit inference engine (CONTRIBUTING.md)
**/*: Preserve existing repository patterns unless intentionally refactoring them.
Keep changes small, readable, and focused; avoid broad rewrites or unrelated cleanup.
Do not reformat unrelated files, and keep comments useful and concise.
Update documentation when setup, commands, or user-facing behavior changes.
Review AI-generated changes for correctness, style consistency, unnecessary noise, and adherence to project architecture before submitting them.
Provider changes must follow the documented integration patterns indocs/integrations/overview.mdand the focused guides underdocs/integrations/how-to/.
When changing provider behavior, avoid breaking third-party providers and test the exact provider/model path changed when possible.
Provider pull requests must explicitly identify affected providers, limitations, and follow-up work.
Run the narrowest meaningful validation command for the touched area, and ensure relevant CI checks pass before merging.
Usebun installto install dependencies and the repository's Bun scripts for building, testing, smoke testing, and development.
Dependency changes require a concrete project benefit such as a bug fix, security issue, or approved feature; preference alone is insufficient.
Do not change the project's language, core runtime, or dependency stack, or introduce a new runtime, without prior maintainer agreement.
Keep each pull request focused on one issue or clearly scoped improvement and avoid bundling unrelated fixes, features, or refactors.
Files:
src/services/api/openaiShim/clientDispatch.test.tssrc/services/api/openaiShim.tssrc/services/api/openaiShim/clientDispatch.ts
⚙️ CodeRabbit configuration file
**/*: Apply the OpenClaude maintainer review rubric from AGENTS.md. Review the current diff, not stale discussion context. Separate real blockers from suggestions. Do not request changes for vague style churn. Treat approval as merge-ready from CodeRabbit's side, pending required human review and GitHub Checks. If checks are failing or unavailable, say so clearly instead of implying the PR is fully ready.
Files:
src/services/api/openaiShim/clientDispatch.test.tssrc/services/api/openaiShim.tssrc/services/api/openaiShim/clientDispatch.ts
{src/services/api/**,src/integrations/**,src/utils/model/**,src/utils/provider*.ts,src/commands/provider/**}
⚙️ CodeRabbit configuration file
{src/services/api/**,src/integrations/**,src/utils/model/**,src/utils/provider*.ts,src/commands/provider/**}: Review provider routing, model selection, env precedence, auth/token handling, OpenAI-compatible shims, retries, proxy behavior, and outbound HTTP behavior with high scrutiny. Block on silent default changes, hidden fallback expansion, credential reuse mistakes, hardcoded provider assumptions, or new network reach that is not intentional and documented.
Files:
src/services/api/openaiShim/clientDispatch.test.tssrc/services/api/openaiShim.tssrc/services/api/openaiShim/clientDispatch.ts
{src/**/*.test.ts,src/**/*.test.tsx,tests/**,scripts/**/*.test.ts,vscode-extension/**/*.test.js}
⚙️ CodeRabbit configuration file
{src/**/*.test.ts,src/**/*.test.tsx,tests/**,scripts/**/*.test.ts,vscode-extension/**/*.test.js}: Review tests for meaningful coverage of the changed behavior, isolation of global/env/config state, async cleanup, fake timers, provider profile leaks, and Windows-compatible assumptions. Block when risky runtime changes lack focused regression coverage or tests assert implementation details while missing the user-visible behavior.
Files:
src/services/api/openaiShim/clientDispatch.test.ts
🔇 Additional comments (11)
src/services/api/openaiShim/clientDispatch.ts (5)
1-14: LGTM!
61-79: LGTM!
80-160: Cancellation lifecycle is correctly synchronized.Combined-signal creation, pre-iteration abort wiring, and the early-return/error abort path in the
finallyblock all check out — traced through consumer-break, parent-abort, and pre-iteration-abort scenarios and each converges on the expected controller state. Well covered by the accompanying tests.
223-287: LGTM on the non-streaming dispatch/conversion logic — see the DI/coverage comment above regarding the codex branch (Lines 223-229, 234-251) specifically.
289-301: LGTM!src/services/api/openaiShim.ts (3)
961-965: LGTM!
1010-1010: LGTM!
1041-1062: Clean delegation tocreateShimRequest; dependency wiring matches the extracted contract.
requestProcessEnv,providerOverride,reasoningEffort, and all requiredClientDispatchDependenciesfields are supplied correctly, and_doRequest/_convertNonStreamingResponse/_convertGeminiToAnthropicResponseare bound appropriately.src/services/api/openaiShim/clientDispatch.test.ts (3)
1-61: LGTM!
63-129: LGTM! Good targeted coverage ofheadersWithRequestUrland theOpenAIShimStreamcancellation lifecycle (parent-abort, pre-iteration cancel, early-return abort).
152-236: LGTM!withResponse, Anthropic Messages passthrough, Gemini/OpenAI non-streaming conversion, and the unexpected-content-type rejection path are all well covered.
relda88
left a comment
There was a problem hiding this comment.
Logic looks correct. One suggestion: the early return on line prevents the cleanup function from running — worth adding a finally block.
|
Thanks — the iterator already uses finally to clean the combined signal, and the early return only returns the existing generator after pre-iteration cleanup has been cleared. |
Summary
Extracts client response dispatch into
openaiShim/clientDispatch.ts.The module owns stream wrapper cancellation, response-URL routing, Anthropic/Gemini/OpenAI stream selection, non-streaming dispatch,
withResponse, and invalid-content rejection. The façade supplies converters and request metadata.Rebase
Rebased and conflict-resolved onto current
main(10a9190bea37bf8574bdb1b040180bf3b84ba2b3), retaining the intentional de-monolithing. Concurrent extractions already inmainwere preserved; this PR remains focused on client dispatch.Current head:
aee66a93ba2843b2a4eb6dba9e638d36c963cab1.Source-file budget
openaiShim.ts: +21 / -212, below 1,500 changed lines; tests excluded.Test migration
clientDispatch.test.ts: 236 lines / 12 focused cases. The focused dispatch suite and retained façade suite cover routing, cancellation, non-streaming conversion,withResponse, and unexpected-content rejection.Validation
bun test ./src/services/api/openaiShim/clientDispatch.test.ts ./src/services/api/openaiShim.test.ts— 270 passed / 754 assertionsbun run typecheckbun run buildgit diff --checkPrepared according to
CONTRIBUTING.mdandAGENTS.md.Summary by CodeRabbit