Repository navigation
fix: AI settings changes are only applied in new sessions - #356
Conversation
Signed-off-by: Ilya Boyandin <[email protected]>
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
📝 WalkthroughWalkthroughRefactored API key and base URL retrieval from static configuration at transport creation time to dynamic runtime retrieval within the transport's fetch implementation. Settings are now accessed on each API call rather than once during initialization. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Tip Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
packages/ai-core/src/chatTransport.ts (1)
162-170:⚠️ Potential issue | 🟠 Major
baseURLparameter requires a valid URL; both empty string and undefined will cause failures.When
getBaseUrlFromSettings()returnsundefined, passing''asbaseURLproduces malformed request URLs. However, the suggested fix of omitting the fallback (passingundefined) is also problematic:@ai-sdk/openai-compatiblerequiresbaseURLto be a string and will fail at runtime ifundefinedis passed.Either ensure
baseUrlis always configured, or provide a sensible default (e.g.,'https://api.openai.com/v1'for OpenAI-compatible endpoints).Suggested fix
const openai = createOpenAICompatible({ apiKey, name: provider, - baseURL: baseUrl ?? '', + baseURL: baseUrl || 'https://api.openai.com/v1', headers, });
fixes #310
Summary
The issue was that AI settings changes (like API keys) were only applied in new sessions because the API key and base URL were captured at transport creation time, not fetched dynamically when making requests.
Root Cause
In
chatTransport.ts, thecreateLocalChatTransportFactoryfunction receivedapiKeyandbaseUrlas parameters and captured them in a closure. These values were only read once when the transport was created, not when each request was made.Changes Made
packages/ai-core/src/chatTransport.ts:apiKeyandbaseUrlfromChatTransportConfigtype (lines 35-49)createLocalChatTransportFactoryto fetchapiKeyandbaseUrldynamically from the store at request time usingstate.ai.getApiKeyFromSettings()andstate.ai.getBaseUrlFromSettings()(lines 154-156)packages/ai-core/src/AiSlice.ts:apiKeyandbaseUrlparameters from thecreateLocalChatTransportFactorycall ingetLocalChatTransport(lines 1079-1087)packages/ai-core/src/types.ts:getApiKeyFromSettingsandgetBaseUrlFromSettingsmethods to theAiStateForTransportinterface so they can be called from the transport (lines 68-71)How It Works Now
When a chat request is made, the transport now:
state.ai.getApiKeyFromSettings()which reads from the latest settingsstate.ai.getBaseUrlFromSettings()which reads from the latest settingsThis means that if a user adds or changes their API key in settings, the next chat message in any existing session will use the updated credentials without needing to create a new session.
Summary by CodeRabbit