Repository navigation
refactor(gateway): simplify openai router internals - #737
Conversation
- Share SharedComponents via Arc between OpenAIRouter and ResponsesComponents, eliminating a redundant reqwest::Client clone in context.rs and router.rs - Remove derivable `client` field from ResponsesRouterContext; access it through responses_components.shared.client instead (route.rs, router.rs) - Simplify load_input_history signature: replace the confusing two-ResponsesRequest-refs pattern (body + request_body) with an explicit `conversation: Option<&str>` parameter (history.rs, route.rs) - Consolidate redundant clone()+take() of previous_response_id into a single take() in history.rs - Use ItemType::FUNCTION_CALL constant instead of raw "function_call" string literal in history.rs - Replace O(n*m) Vec::insert(0, ...) loops with Vec::splice in inject_mcp_metadata_streaming and build_incomplete_response (tool_loop.rs) Signed-off-by: Simo Lin <linsimo.mark@gmail.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (5)
📝 WalkthroughWalkthroughThis PR refactors shared component management by wrapping Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Comment |
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request refactors the internal architecture of the OpenAI router within the model gateway, focusing on improving efficiency and reducing structural redundancies. It addresses issues such as duplicate Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request refactors the OpenAI router internals to simplify state management and improve performance. Key changes include sharing SharedComponents via an Arc to eliminate redundant allocations, removing the derivable client field from ResponsesRouterContext, and simplifying the load_input_history function signature. Additionally, an O(n*m) performance issue in metadata injection has been resolved by replacing repeated Vec::insert calls with a more efficient splice operation. These changes improve code clarity and efficiency.
Summary
Cleanup pass on the OpenAI router internals following the recent extraction refactors (#732, #735). Eliminates redundant state, simplifies function signatures, and fixes an O(n*m) performance issue in MCP metadata injection.
Refs: #732, #735
What changed
ResponsesComponents.sharedchanged from ownedSharedComponentstoArc<SharedComponents>, sharing the same allocation withOpenAIRouter.shared_componentsResponsesComponentsnow clones the existingshared_componentsArc instead of constructing a secondSharedComponents; removed redundantclientfield fromResponsesRouterContextconstructionclientfield fromResponsesRouterContextstruct; extractedconversationasOption<&str>before passing toload_input_historyinstead of passing twoResponsesRequestreferencesResponsesRequest-refs signature with explicitconversation: Option<&str>parameter; consolidated redundant.clone().filter()+.take().filter()ofprevious_response_idinto a single.take(); replaced raw"function_call"string withItemType::FUNCTION_CALLconstantVec::insert(0, ...)loops ininject_mcp_metadata_streamingandbuild_incomplete_responsewithVec::splice(0..0, prefix)using a pre-built prefix vectorWhy
The recent extraction refactors (#732, #735) faithfully moved code but left some structural redundancies:
reqwest::Clientwas stored in two separateSharedComponentsinstancesResponsesRouterContextcarried aclientfield always derivable fromresponses_componentsload_input_historytook two references toResponsesRequest(original + mutable clone) just to read theconversationfield from the originalprevious_response_idwas processed twice with the same filter predicate (clone+filter, then take+filter)Vec::insert(0, ...)causing O(n) shifts per insertHow
ResponsesComponents.sharedtoArc<SharedComponents>so both the chat and responses paths share the same allocation (transparent via Deref)clientfield and inlined the access path at the single call siteconversationinroute_responsesbefore callingload_input_history, passing it asOption<&str>to eliminate the need for the original body reference.take().filter()and borrowed the result for the chain-loading branchsplice(0..0, ...)for O(1) amortized prependTest plan
cargo check -p smgpassescargo clippy -p smg --all-targets --all-features -- -D warningspasses cleanSummary by CodeRabbit