Skip to content

fix: AI settings changes are only applied in new sessions - #356

Merged
ilyabo merged 1 commit into
mainfrom
fix--AI-settings-changes-are-only-applied-in-new-sessions
Feb 11, 2026
Merged

ilyabo merged 1 commit into
mainfrom
fix--AI-settings-changes-are-only-applied-in-new-sessions

Conversation

@ilyabo

@ilyabo ilyabo commented Feb 11, 2026 •

Copy link
Copy Markdown
Collaborator

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, the createLocalChatTransportFactory function received apiKey and baseUrl as 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

  1. packages/ai-core/src/chatTransport.ts:

    • Removed apiKey and baseUrl from ChatTransportConfig type (lines 35-49)
    • Modified createLocalChatTransportFactory to fetch apiKey and baseUrl dynamically from the store at request time using state.ai.getApiKeyFromSettings() and state.ai.getBaseUrlFromSettings() (lines 154-156)
  2. packages/ai-core/src/AiSlice.ts:

    • Removed the apiKey and baseUrl parameters from the createLocalChatTransportFactory call in getLocalChatTransport (lines 1079-1087)
  3. packages/ai-core/src/types.ts:

    • Added getApiKeyFromSettings and getBaseUrlFromSettings methods to the AiStateForTransport interface so they can be called from the transport (lines 68-71)

How It Works Now

When a chat request is made, the transport now:

  1. Gets the current state from the store
  2. Fetches the API key using state.ai.getApiKeyFromSettings() which reads from the latest settings
  3. Fetches the base URL using state.ai.getBaseUrlFromSettings() which reads from the latest settings
  4. Creates the OpenAI-compatible client with the fresh credentials

This 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

  • Refactor
    • Enhanced API credential management in chat transport operations.

@ilyabo

ilyabo commented Feb 11, 2026

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Feb 11, 2026

Copy link
Copy Markdown
Contributor
✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai

coderabbitai Bot commented Feb 11, 2026 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

Refactored 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

Cohort / File(s) Summary
Transport Configuration
packages/ai-core/src/chatTransport.ts
Removed apiKey and baseUrl parameters from ChatTransportConfig. Introduced runtime retrieval of credentials inside the fetchImpl path to access current settings at call time instead of cached values.
State Interface
packages/ai-core/src/types.ts
Added getApiKeyFromSettings() and getBaseUrlFromSettings() methods to AiStateForTransport interface to provide access to provider-specific settings for the current session.
Session Initialization
packages/ai-core/src/AiSlice.ts
Removed static injection of API key and base URL into the local transport factory during session creation, allowing settings to be resolved dynamically at request time.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 Settings once frozen at the start of the day,
Now flow like fresh carrots, responsive and gay!
No session restart to see changes take hold—
Each message now fetches the truth, fresh and bold! 🥕✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: AI settings changes are only applied in new sessions' directly describes the main issue being addressed and matches the core problem fixed in the changeset.
Linked Issues check ✅ Passed The changes fully address issue #310 by modifying the transport layer to fetch API key and base URL dynamically at request time rather than at transport creation time, enabling settings updates to apply to existing sessions.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the settings application issue: modifications to ChatTransportConfig, createLocalChatTransportFactory, AiSlice transport initialization, and type definitions align precisely with the linked issue requirements.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix--AI-settings-changes-are-only-applied-in-new-sessions

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

baseURL parameter requires a valid URL; both empty string and undefined will cause failures.

When getBaseUrlFromSettings() returns undefined, passing '' as baseURL produces malformed request URLs. However, the suggested fix of omitting the fallback (passing undefined) is also problematic: @ai-sdk/openai-compatible requires baseURL to be a string and will fail at runtime if undefined is passed.

Either ensure baseUrl is 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,
         });

@ilyabo
ilyabo merged commit d1799d0 into main Feb 11, 2026
5 checks passed
@ilyabo
ilyabo deleted the fix--AI-settings-changes-are-only-applied-in-new-sessions branch February 11, 2026 13:11
@coderabbitai coderabbitai Bot mentioned this pull request Aug 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AI: AI settings changes are only applied in new sessions

1 participant