Repository navigation
feat: AI features into preferences and Redux store - #8048
Conversation
WalkthroughAdds end-to-end AI support: Electron main IPC and provider registry with encrypted key storage, renderer IPC helpers (including streaming), and a Formik-based AI Preferences UI with ProviderCard components and themed styling. ChangesAI Feature Implementation
Sequence Diagram(s)sequenceDiagram
participant User
participant UI as Preferences UI
participant Renderer as Renderer IPC
participant Main as Electron Main
participant Store as AiKeyStore
participant Sdk as AI SDK
User->>UI: Enter API key in ProviderCard
UI->>Renderer: setAiApiKey({providerId, apiKey})
Renderer->>Main: renderer:set-ai-api-key
Main->>Store: encrypt and persist key
Main->>Renderer: success
Renderer->>UI: toast confirmation
User->>UI: Start streaming generation
UI->>Renderer: aiStreamText({streamId, modelId, messages})
Renderer->>Main: renderer:ai-stream-text
Main->>Sdk: streamText(model, messages)
Sdk-->>Main: text chunk
Main->>Renderer: main:ai-stream-chunk
Renderer->>UI: onChunk callback
Sdk-->>Main: complete
Main->>Renderer: main:ai-stream-complete
Renderer->>UI: done resolved
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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.
Actionable comments posted: 6
🧹 Nitpick comments (2)
packages/bruno-app/src/utils/ai/index.js (1)
34-36: ⚡ Quick winRemove the renderer-side
ipcRendererexistence guard.In this codebase’s Electron renderer path, this guard is discouraged and can hide integration regressions.
Based on learnings: In the Bruno Electron renderer code (packages/bruno-app), assume window.ipcRenderer is always available and skip existence checks.🔧 Suggested fix
const { ipcRenderer } = window; - if (!ipcRenderer) { - return { stop: () => {}, done: Promise.reject(new Error('IPC not available')) }; - }🤖 Prompt for 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. In `@packages/bruno-app/src/utils/ai/index.js` around lines 34 - 36, Remove the renderer-side ipcRenderer existence guard: delete the conditional that checks if (!ipcRenderer) and the fallback return ({ stop: () => {}, done: Promise.reject(new Error('IPC not available')) }); and instead use window.ipcRenderer (or ipcRenderer) directly throughout this module so code always assumes the renderer IPC is present; update any callers expecting the fallback to handle actual runtime errors from real ipc calls rather than a synthetic Promise.reject.packages/bruno-app/src/components/Preferences/AI/ProviderCard.js (1)
34-34: ⚡ Quick winWrap single-parameter arrow function params in parentheses.
stopBubbleshould follow the repo’s arrow-function formatting rule.Proposed fix
-const stopBubble = (e) => e.stopPropagation(); +const stopBubble = ((e) => e.stopPropagation());As per coding guidelines, "Always use parentheses around parameters in arrow functions, even for single parameters."
🤖 Prompt for 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. In `@packages/bruno-app/src/components/Preferences/AI/ProviderCard.js` at line 34, The arrow function stopBubble uses an unparenthesized single parameter; update its declaration to include parentheses around the parameter (i.e., change stopBubble = (e) => ... to use (e) explicitly) so it follows the repo's arrow-function formatting rule and is consistent across the component; locate the stopBubble function in ProviderCard.js to make this one-line change.
🤖 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 `@packages/bruno-app/src/components/Preferences/AI/index.js`:
- Around line 92-99: The effect currently calls debouncedSave.flush() in its
cleanup which forces pending saves to run on every re-render and defeats the
400ms debounce; change the cleanup to call debouncedSave.cancel() instead so
pending saves are cancelled on unmount/re-render rather than flushed, and keep
the effect body using debouncedSave(formik.values) when formik.dirty &&
formik.isValid; update the cleanup reference in the useEffect that uses
formik.values, formik.dirty, formik.isValid, debouncedSave to use
debouncedSave.cancel().
In `@packages/bruno-electron/package.json`:
- Around line 33-35: Add the missing peer dependency zod to the bruno-electron
package by adding "zod": "^4.1.8" to the dependencies section of package.json;
this ensures compatibility with the existing "`@ai-sdk/openai`" and
"`@ai-sdk/anthropic`" entries and satisfies their peer requirement (refer to the
dependency list containing "`@ai-sdk/anthropic`" and "`@ai-sdk/openai`"). Make sure
to run the package manager install afterwards to update the lockfile.
In `@packages/bruno-electron/src/ipc/ai/index.js`:
- Around line 136-161: Before creating and storing a new AbortController in the
ipcMain.on('renderer:ai-stream-text') handler, check activeStreams.has(streamId)
and reject the duplicate (send 'main:ai-stream-error' with a clear message) or
choose to abort the existing controller first; when cleaning up later (where
activeStreams.delete/controller.abort are used) only delete/abort if the stored
controller === controller you created to avoid tearing down a different stream.
Reference symbols: ipcMain.on('renderer:ai-stream-text'), streamId,
activeStreams, controller.
- Around line 77-82: The handler ipcMain.handle('renderer:get-ai-api-key', which
currently returns the raw decrypted key from aiKeyStore.getKey(providerId),
should not send secrets to the renderer; instead change this handler to only
return a non-secret indicator (e.g., boolean hasKey or a masked string) and
remove any path that sends the full key, and implement a main-side action
handler (e.g., ipcMain.handle('renderer:perform-ai-request' or similar) that
accepts providerId and request parameters and performs the external API call in
the main process using aiKeyStore.getKey(providerId) so the key never leaves
main; update references to 'renderer:get-ai-api-key' to use the new
presence/masked response and call the new main-side request handler for any
operation requiring the actual key.
In `@packages/bruno-electron/src/ipc/ai/providers.js`:
- Around line 27-35: Update the Anthropic modelId values in the providers
mapping: change the mapping for key 'claude-sonnet-4-5' (the object with
provider: 'anthropic') to use modelId 'claude-sonnet-4-5-20250929' instead of
the old '-20250514' value, and replace the modelId for key 'claude-opus-4-5'
with the correct Opus 4.5 model ID from the Anthropic docs/SDK (update the
object with provider: 'anthropic' and modelId for 'claude-opus-4-5'); leave the
OpenAI entries (gpt-4o-mini, gpt-4o, gpt-5, gpt-5-mini) unchanged. Ensure both
updated modelId strings are exact and valid per Anthropic's current API.
In `@packages/bruno-electron/src/store/ai-keys.js`:
- Around line 32-45: hasKey and getKeyStatus currently treat any stored
encrypted blob as "configured" even if decryption fails; update
hasKey(providerId) and getKeyStatus() to attempt to decrypt the stored key blob
(the same decryption used when reading keys earlier) and only return configured:
true or true from hasKey if decryption succeeds and yields a usable key; for
getKeyStatus iterate Object.keys(this.store.get('keys', {}) || {}) and try to
decrypt each stored[providerId] and set status[providerId] = { configured:
Boolean(decryptedKey) } (handle decryption errors quietly and treat them as not
configured). Ensure you reference the existing decryption routine used elsewhere
in this module rather than just checking blob presence.
---
Nitpick comments:
In `@packages/bruno-app/src/components/Preferences/AI/ProviderCard.js`:
- Line 34: The arrow function stopBubble uses an unparenthesized single
parameter; update its declaration to include parentheses around the parameter
(i.e., change stopBubble = (e) => ... to use (e) explicitly) so it follows the
repo's arrow-function formatting rule and is consistent across the component;
locate the stopBubble function in ProviderCard.js to make this one-line change.
In `@packages/bruno-app/src/utils/ai/index.js`:
- Around line 34-36: Remove the renderer-side ipcRenderer existence guard:
delete the conditional that checks if (!ipcRenderer) and the fallback return ({
stop: () => {}, done: Promise.reject(new Error('IPC not available')) }); and
instead use window.ipcRenderer (or ipcRenderer) directly throughout this module
so code always assumes the renderer IPC is present; update any callers expecting
the fallback to handle actual runtime errors from real ipc calls rather than a
synthetic Promise.reject.
🪄 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: CHILL
Plan: Pro
Run ID: f677c554-a726-43c6-8634-35b7a225f013
⛔ Files ignored due to path filters (1)
package-lock.jsonis excluded by!**/package-lock.json
📒 Files selected for processing (13)
packages/bruno-app/src/components/Preferences/AI/ProviderCard.jspackages/bruno-app/src/components/Preferences/AI/StyledWrapper.jspackages/bruno-app/src/components/Preferences/AI/index.jspackages/bruno-app/src/components/Preferences/index.jspackages/bruno-app/src/components/ToggleSwitch/index.jspackages/bruno-app/src/providers/ReduxStore/slices/app.jspackages/bruno-app/src/utils/ai/index.jspackages/bruno-electron/package.jsonpackages/bruno-electron/src/index.jspackages/bruno-electron/src/ipc/ai/index.jspackages/bruno-electron/src/ipc/ai/providers.jspackages/bruno-electron/src/store/ai-keys.jspackages/bruno-electron/src/store/preferences.js
There was a problem hiding this comment.
♻️ Duplicate comments (1)
packages/bruno-electron/src/ipc/ai/index.js (1)
146-149:⚠️ Potential issue | 🟠 Major | ⚡ Quick winProtect cleanup with controller ownership check.
The duplicate guard helps, but Line 204 still deletes by
streamIdunconditionally. If a stopped stream ID is quickly reused, the old handler’sfinallycan clear the newer active controller.🔧 Suggested fix
} finally { - activeStreams.delete(streamId); + if (activeStreams.get(streamId) === controller) { + activeStreams.delete(streamId); + } }Also applies to: 204-205
🤖 Prompt for 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. In `@packages/bruno-electron/src/ipc/ai/index.js` around lines 146 - 149, The finally cleanup currently deletes activeStreams by streamId unconditionally, which can remove a newly created controller if streamId was reused; modify the cleanup in the async handler so it only removes the entry if the stored controller instance matches the activeStreams entry (i.e., capture the controller you set into activeStreams and in the finally check that activeStreams.get(streamId) === controller before calling activeStreams.delete(streamId)); reference activeStreams, streamId and the controller variable used when registering the stream to implement this ownership check.
🤖 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.
Duplicate comments:
In `@packages/bruno-electron/src/ipc/ai/index.js`:
- Around line 146-149: The finally cleanup currently deletes activeStreams by
streamId unconditionally, which can remove a newly created controller if
streamId was reused; modify the cleanup in the async handler so it only removes
the entry if the stored controller instance matches the activeStreams entry
(i.e., capture the controller you set into activeStreams and in the finally
check that activeStreams.get(streamId) === controller before calling
activeStreams.delete(streamId)); reference activeStreams, streamId and the
controller variable used when registering the stream to implement this ownership
check.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 75b9baf8-ab45-4325-be8b-63703f1509b4
⛔ Files ignored due to path filters (1)
package-lock.jsonis excluded by!**/package-lock.json
📒 Files selected for processing (5)
packages/bruno-app/src/components/Preferences/AI/index.jspackages/bruno-electron/package.jsonpackages/bruno-electron/src/ipc/ai/index.jspackages/bruno-electron/src/ipc/ai/providers.jspackages/bruno-electron/src/store/ai-keys.js
| const OpenAiLogo = (props) => ( | ||
| <svg viewBox="0 0 24 24" fill="currentColor" {...props}> | ||
| <path d="M22.2819 9.8211a5.9847 5.9847 0 0 0-.5157-4.9108 6.0462 6.0462 0 0 0-6.5098-2.9A6.0651 6.0651 0 0 0 4.9807 4.1818a5.9847 5.9847 0 0 0-3.9977 2.9 6.0462 6.0462 0 0 0 .7427 7.0966 5.98 5.98 0 0 0 .511 4.9107 6.051 6.051 0 0 0 6.5146 2.9001A5.9847 5.9847 0 0 0 13.2599 24a6.0557 6.0557 0 0 0 5.7718-4.2058 5.9894 5.9894 0 0 0 3.9977-2.9001 6.0557 6.0557 0 0 0-.7475-7.0729zm-9.022 12.6081a4.4755 4.4755 0 0 1-2.8764-1.0408l.1419-.0804 4.7783-2.7582a.7948.7948 0 0 0 .3927-.6813v-6.7369l2.02 1.1686a.071.071 0 0 1 .038.052v5.5826a4.504 4.504 0 0 1-4.4945 4.4944zm-9.6607-4.1254a4.4708 4.4708 0 0 1-.5346-3.0137l.142.0852 4.783 2.7582a.7712.7712 0 0 0 .7806 0l5.8428-3.3685v2.3324a.0804.0804 0 0 1-.0332.0615L9.74 19.9502a4.4992 4.4992 0 0 1-6.1408-1.6464zM2.3408 7.8956a4.485 4.485 0 0 1 2.3655-1.9728V11.6a.7664.7664 0 0 0 .3879.6765l5.8144 3.3543-2.0201 1.1685a.0757.0757 0 0 1-.071 0l-4.8303-2.7865A4.504 4.504 0 0 1 2.3408 7.8956zm16.5963 3.8558L13.1038 8.364 15.1192 7.2a.0757.0757 0 0 1 .071 0l4.8303 2.7913a4.4944 4.4944 0 0 1-.6765 8.1042v-5.6772a.79.79 0 0 0-.407-.667zm2.0107-3.0231l-.142-.0852-4.7735-2.7818a.7759.7759 0 0 0-.7854 0L9.409 9.2297V6.8974a.0662.0662 0 0 1 .0284-.0615l4.8303-2.7866a4.4992 4.4992 0 0 1 6.6802 4.66zM8.3065 12.863l-2.02-1.1638a.0804.0804 0 0 1-.038-.0567V6.0742a4.4992 4.4992 0 0 1 7.3757-3.4537l-.142.0805L8.704 5.459a.7948.7948 0 0 0-.3927.6813zm1.0976-2.3654l2.602-1.4998 2.6069 1.4998v2.9994l-2.5974 1.4997-2.6067-1.4997Z" /> | ||
| </svg> | ||
| ); | ||
|
|
||
| const AnthropicLogo = (props) => ( | ||
| <svg viewBox="0 0 24 24" fill="currentColor" {...props}> | ||
| <path d="M17.304 3.541h-3.672l6.696 16.918h3.672l-6.696-16.918Zm-10.608 0L0 20.459h3.744l1.368-3.584h6.624l1.368 3.584h3.744L10.152 3.54H6.696Zm.432 10.418 2.208-5.784 2.208 5.784H7.128Z" /> | ||
| </svg> | ||
| ); |
There was a problem hiding this comment.
Move them to their own files
| getKey(providerId) { | ||
| const encrypted = this.store.get(`keys.${providerId}`); | ||
| if (!encrypted) return null; | ||
| try { | ||
| return decryptString(encrypted); | ||
| } catch (err) { | ||
| console.error(`Failed to decrypt AI key for ${providerId}:`, err.message); | ||
| return null; | ||
| } | ||
| } |
There was a problem hiding this comment.
Make sure this is not fetched on the client
Description
JIRA
Contribution Checklist:
Note: Keeping the PR small and focused helps make it easier to review and merge. If you have multiple changes you want to make, please consider submitting them as separate pull requests.
Publishing to New Package Managers
Please see here for more information.
Summary by CodeRabbit
New Features
Improvements