Skip to content

feat(ai): Improve ai context#8404

Open
naman-bruno wants to merge 2 commits into
usebruno:mainfrom
naman-bruno:feat/improve-context
Open

feat(ai): Improve ai context#8404
naman-bruno wants to merge 2 commits into
usebruno:mainfrom
naman-bruno:feat/improve-context

Conversation

@naman-bruno

@naman-bruno naman-bruno commented Jun 29, 2026

Copy link
Copy Markdown
Collaborator

Description

  • Enhanced AIAssist to accept and utilize a new variables prop for improved context handling.
  • Updated multiple components (CollectionApp, AiChatSidebar, Documentation, etc.) to build and pass AI variable payloads, ensuring consistent access to environment and collection variables.
  • Refactored AI context building utilities to streamline variable handling and maintain security by redacting sensitive information.
  • Added support for searching variables in the chat prompts, enhancing user experience and functionality.

Contribution Checklist:

  • I've used AI significantly to create this pull request
  • The pull request only addresses one issue or adds one feature.
  • The pull request does not introduce any breaking changes
  • I have added screenshots or gifs to help explain the change if applicable.
  • I have read the contribution guidelines.
  • Create an issue and link to the pull request.

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
    • AI assistance now includes variable payloads derived from the current collection, folder, request, and environments.
    • AI chat and script generation can show an “Available Variables” section and search variables by name.
    • AI-assisted script generation can use a richer request context that includes recent response details/shape.
  • Bug Fixes
    • Improved handling of sensitive data: secret and token-like values are masked/redacted consistently across AI prompts and outputs.
    • AI generation updates correctly when variable inputs change.

@coderabbitai

coderabbitai Bot commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

Adds AI variable payloads in the renderer, shared AI context formatting and search helpers in Electron, and variable forwarding through chat and script-generation IPC. The AI assist entry points and their consumer components now pass variables, and script generation uses streamed tool calling.

Changes

AI Variables Pipeline

Layer / File(s) Summary
Renderer payload builders
packages/bruno-app/src/utils/ai/index.js, packages/bruno-app/src/utils/ai/index.spec.js
Adds buildAiRequestContext, buildAiVariablesPayload, and buildAiContextPayload with variable redaction, scope aggregation, and tests.
Shared IPC context formatting
packages/bruno-electron/src/ipc/ai/context.js, packages/bruno-electron/src/ipc/ai/context.spec.js
Adds request/response redaction, variable listing, variable search, and matching test coverage.
Script prompts and generation IPC
packages/bruno-electron/src/ipc/ai/script-prompts.js, packages/bruno-electron/src/ipc/ai/index.js
Updates script prompts, adds system tool instructions, and switches script generation to streamed tool-calling with response and variable search support.
Chat prompt and thunk wiring
packages/bruno-electron/src/ipc/ai/chat-prompts.js, packages/bruno-electron/src/ipc/ai/chat.js, packages/bruno-app/src/providers/ReduxStore/slices/chat.js
Adds search_variables prompt/tool support and forwards variables through the chat stream payload and thunk.
AIAssist and consumer wiring
packages/bruno-app/src/components/AIAssist/index.js, packages/bruno-app/src/components/AiChatSidebar/index.js, packages/bruno-app/src/components/RequestPane/..., packages/bruno-app/src/components/CollectionSettings/..., packages/bruno-app/src/components/FolderSettings/..., packages/bruno-app/src/components/CollectionApp/index.js, packages/bruno-app/src/components/Documentation/index.js
Passes computed variables into AIAssist and chat submission flows from request, collection, folder, and documentation screens.

Sequence Diagram(s)

sequenceDiagram
  participant ReactComponent
  participant AIAssist
  participant RendererAI as utils/ai
  participant IPC as renderer:ai-generate-script
  participant Context as context.js
  participant LLM

  ReactComponent->>AIAssist: variables={aiVariables}
  AIAssist->>RendererAI: aiGenerateScript(..., variables)
  RendererAI->>IPC: request payload with variables
  IPC->>Context: formatRequestContext, formatVariablesList
  IPC->>LLM: streamText with read_response and search_variables
  LLM-->>IPC: text deltas + tool calls
  IPC-->>AIAssist: { content, modelId }
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

  • usebruno/bruno#8220: Introduces the same AIAssist and renderer:ai-generate-script flow that this PR extends with variables and tool support.
  • usebruno/bruno#8246: Touches the same AIAssist props and generation wiring that this PR extends with variables.
  • usebruno/bruno#8358: Modifies the same chat sidebar and sendAiMessage path that now forwards AI variables.

Suggested reviewers

  • helloanoop
  • bijin-bruno
  • lohit-bruno
  • sid-bruno
  • vijayh-bruno
  • utkarsh-bruno

Poem

Tiny secrets, tucked away,
Now stream in redacted shades of gray.
Variables hum, prompts take flight,
Scripts and chats both shine more bright.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is directly related to the PR’s main theme of improving AI context handling.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@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.

Actionable comments posted: 12

🧹 Nitpick comments (3)
packages/bruno-electron/src/ipc/ai/context.js (1)

1-300: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add backend tests for the context helpers.

This new module owns provider-facing redaction, response shaping, and variable search formatting; it should have direct tests for secret masking, body redaction, search limits, and empty-query behavior.

As per coding guidelines, “Add tests for any new functionality or meaningful changes.”

🤖 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/context.js` around lines 1 - 300, The new
backend context helpers in context.js need direct test coverage for the
redaction and formatting behavior they introduced. Add tests for the key symbols
formatRequestContext, redactResponseValues, formatResponseShape,
isSecretVariable, formatVariablesList, searchVariables, and
formatSearchVariablesResult, verifying secret masking, response body shaping,
search limit truncation, and empty-query handling. Place the tests alongside the
existing backend test suite so these shared AI-facing rules stay covered as the
module evolves.

Source: Coding guidelines

packages/bruno-app/src/utils/ai/index.spec.js (1)

154-221: 🔒 Security & Privacy | 🔵 Trivial | ⚡ Quick win

Add regression coverage for OAuth2 redaction and override precedence.

These tests don’t cover OAuth2 credentials being present via getAllVariables(), or higher-precedence runtime/request values overriding env/global names. Those are the risky paths for this new payload builder.

As per coding guidelines, “Add tests for any new functionality or meaningful changes.”

🤖 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.spec.js` around lines 154 - 221, The
current buildAiVariablesPayload test suite is missing regression coverage for
OAuth2 credentials coming through getAllVariables and for runtime/request values
overriding env/global entries. Add focused tests in buildAiVariablesPayload to
verify OAuth2 fields are redacted like other secrets and that higher-precedence
runtime/request names win over environment/global values without duplication,
using the existing buildAiVariablesPayload and getAllVariables paths as the
reference points.

Source: Coding guidelines

packages/bruno-electron/src/ipc/ai/index.js (1)

230-260: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add tests for the new script-generation path in packages/bruno-electron/src/ipc/ai/index.js. The streamText + tools flow needs coverage for tool calls, the no-variable branch, empty output, and response-shape lookup so regressions in script generation are caught.

🤖 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 230 - 260, Add
tests covering the script-generation flow in the AI IPC module by exercising the
streamText-based path in the handler that builds scripts, including tool-call
execution, the no-variable branch, empty output handling, and response-shape
lookup. Focus on the relevant symbols around streamText,
buildScriptSystemPrompt, buildScriptUserPrompt, stripCodeFences, and the
returned { content, modelId } shape so regressions in the script-generation path
are caught.

Source: Coding guidelines

🤖 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/AiChatSidebar/index.js`:
- Around line 365-371: The aiVariables payload is only including the active item
for request chats, so folder chats are missing folder-scoped variables. Update
the useMemo call in AiChatSidebar’s aiVariables to pass the active folder when
aiContext.kind is 'folder' (in addition to the request item when kind is
'request'), so buildAiVariablesPayload(collection, item) can fold the correct
scope into the prompt preview and search_variables results.

In `@packages/bruno-app/src/components/FolderSettings/Documentation/index.js`:
- Line 49: The FolderSettings Documentation component is building AI variables
without the current folder context, so folder-scoped variables are omitted.
Update the useMemo call in the Documentation component to pass the folder/item
object into buildAiVariablesPayload(collection, item) instead of null, ensuring
the AIAssist flow receives complete folder-doc context.

In `@packages/bruno-app/src/components/FolderSettings/Script/index.js`:
- Line 106: The aiVariables payload in the FolderSettings Script view is
omitting folder-scoped variables because buildAiVariablesPayload is called with
null instead of the current folder. Update the useMemo call in the Script
component to pass the in-scope folder value into
buildAiVariablesPayload(collection, folder) so AI assist and search_variables
include variables defined on the current folder.

In `@packages/bruno-app/src/components/FolderSettings/Tests/index.js`:
- Around line 42-43: The aiVariables payload is being built with no folder
context, so folder-local variables are omitted from the AIAssist setup in the
Tests component. Update the useMemo call in FolderSettings/Tests to build the
payload with the folder scope instead of null, using the existing
collection/folder data path so AIAssist receives the same variables context that
folder tests run with.

In `@packages/bruno-app/src/utils/ai/index.js`:
- Around line 131-170: The variable aggregation in the search helper is using
first-write wins via seen, which can return lower-precedence values instead of
the effective resolved value. Update the logic around add and the
getAllVariables collection loop to preserve variable precedence by either
starting from the final merged result of getAllVariables(item, collection) and
then annotating scope/secret metadata, or by allowing later higher-priority
sources to replace earlier entries instead of skipping them. Keep the existing
scope handling for env, globalEnvSecrets/globalEnvironmentVariables,
runtimeVariables, and the collection/folder/request variables so
search_variables reflects runtime resolution.
- Around line 167-178: `getAllVariables()` is already emitting OAuth2 entries,
so the later OAuth2 secret pass in `index.js` can skip `$oauth2.*.clientId`
after it has been added once with the wrong secret flag. Update the variable
collection flow around `getAllVariables`, `add`, and the explicit “OAuth2
credentials” block so OAuth2 names are excluded from the general pass (or
otherwise deferred) and only added in the dedicated secret-handling pass with
`secret: true`.
- Around line 100-108: Broaden the redaction logic in SENSITIVE_NAME_PATTERNS so
token-like fields such as refresh_token, id_token, csrfToken, and plain TOKEN
are matched and removed before building the renderer payload. Update the
relevant matcher in ai/index.js to cover these additional name variants, and
make the same change in the mirrored redaction list in context.js so both paths
stay consistent.

In `@packages/bruno-electron/src/ipc/ai/context.js`:
- Around line 147-174: Mask sensitive data before serializing structured request
bodies in the AI context builder: the body handling in the context.js request
formatting logic currently copies json and graphql.variables verbatim, so update
the same body-mode switch that handles json and graphql to recursively redact
secrets like password, client_secret, and refresh_token before appending to
parts. Reuse the existing masking approach used for formUrlEncoded and
multipartForm so the final formatted “Body” section still shows structure but
never exposes raw sensitive values.
- Around line 12-26: The sensitive-header redaction list in
SENSITIVE_HEADER_PATTERNS is missing generic token names, so provider-boundary
values like refresh_token, id_token, csrfToken, and TOKEN can slip through.
Update the pattern set used in the IPC AI context redaction logic to mirror the
renderer-side list and add matching cases for generic token-related names
alongside the existing authorization, cookie, api key, secret, and password
patterns.

In `@packages/bruno-electron/src/ipc/ai/index.js`:
- Line 212: The tool description for the variable search helper is too narrow
because it only points to bru.getEnvVar and bru.getVar, which can lead to
incorrect usage for collection/global/runtime scopes. Update the description in
the AI IPC tool definition to list the scope-specific Bruno getters alongside
the existing guidance, so callers know which getter to use for each returned
variable type. Use the description string in the tool metadata under the ai IPC
index to make this change.

In `@packages/bruno-electron/src/ipc/ai/script-prompts.js`:
- Around line 308-311: The AI prompt context is currently including full request
bodies through formatRequestContext, which can leak secrets or PII. Update the
prompt-building logic in script-prompts.js so the context passed to
formatRequestContext uses a redacted or capped body shape rather than the
default full body, while still keeping includeResponse enabled if needed. If
there is an existing helper or option for body masking in formatRequestContext
or related request-context formatting code, use that; otherwise disable body
inclusion until request-body redaction is available.
- Line 356: The search_variables tool instruction is too narrow because it only
mentions bru.getEnvVar and bru.getVar, which can lead generated code to use the
wrong accessor for collection, folder, request, global, or secret variables.
Update the wording in script-prompts.js around search_variables to explicitly
tell the model to use the returned scope to choose the correct runtime accessor
for the variable before referencing it in code, while still avoiding pasting
secret values.

---

Nitpick comments:
In `@packages/bruno-app/src/utils/ai/index.spec.js`:
- Around line 154-221: The current buildAiVariablesPayload test suite is missing
regression coverage for OAuth2 credentials coming through getAllVariables and
for runtime/request values overriding env/global entries. Add focused tests in
buildAiVariablesPayload to verify OAuth2 fields are redacted like other secrets
and that higher-precedence runtime/request names win over environment/global
values without duplication, using the existing buildAiVariablesPayload and
getAllVariables paths as the reference points.

In `@packages/bruno-electron/src/ipc/ai/context.js`:
- Around line 1-300: The new backend context helpers in context.js need direct
test coverage for the redaction and formatting behavior they introduced. Add
tests for the key symbols formatRequestContext, redactResponseValues,
formatResponseShape, isSecretVariable, formatVariablesList, searchVariables, and
formatSearchVariablesResult, verifying secret masking, response body shaping,
search limit truncation, and empty-query handling. Place the tests alongside the
existing backend test suite so these shared AI-facing rules stay covered as the
module evolves.

In `@packages/bruno-electron/src/ipc/ai/index.js`:
- Around line 230-260: Add tests covering the script-generation flow in the AI
IPC module by exercising the streamText-based path in the handler that builds
scripts, including tool-call execution, the no-variable branch, empty output
handling, and response-shape lookup. Focus on the relevant symbols around
streamText, buildScriptSystemPrompt, buildScriptUserPrompt, stripCodeFences, and
the returned { content, modelId } shape so regressions in the script-generation
path are caught.
🪄 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: 6c4e2b3d-466a-4a39-a010-398f2852bfde

📥 Commits

Reviewing files that changed from the base of the PR and between 87f7426 and 5935e71.

📒 Files selected for processing (21)
  • packages/bruno-app/src/components/AIAssist/index.js
  • packages/bruno-app/src/components/AiChatSidebar/index.js
  • packages/bruno-app/src/components/CollectionApp/index.js
  • packages/bruno-app/src/components/CollectionSettings/Docs/index.js
  • packages/bruno-app/src/components/CollectionSettings/Script/index.js
  • packages/bruno-app/src/components/CollectionSettings/Tests/index.js
  • packages/bruno-app/src/components/Documentation/index.js
  • packages/bruno-app/src/components/FolderSettings/Documentation/index.js
  • packages/bruno-app/src/components/FolderSettings/Script/index.js
  • packages/bruno-app/src/components/FolderSettings/Tests/index.js
  • packages/bruno-app/src/components/RequestPane/AppCodeEditor/index.js
  • packages/bruno-app/src/components/RequestPane/Script/index.js
  • packages/bruno-app/src/components/RequestPane/Tests/index.js
  • packages/bruno-app/src/providers/ReduxStore/slices/chat.js
  • packages/bruno-app/src/utils/ai/index.js
  • packages/bruno-app/src/utils/ai/index.spec.js
  • packages/bruno-electron/src/ipc/ai/chat-prompts.js
  • packages/bruno-electron/src/ipc/ai/chat.js
  • packages/bruno-electron/src/ipc/ai/context.js
  • packages/bruno-electron/src/ipc/ai/index.js
  • packages/bruno-electron/src/ipc/ai/script-prompts.js

Comment thread packages/bruno-app/src/components/AiChatSidebar/index.js Outdated
Comment thread packages/bruno-app/src/components/FolderSettings/Documentation/index.js Outdated
Comment thread packages/bruno-app/src/components/FolderSettings/Script/index.js Outdated
Comment thread packages/bruno-app/src/components/FolderSettings/Tests/index.js Outdated
Comment thread packages/bruno-app/src/utils/ai/index.js
Comment thread packages/bruno-electron/src/ipc/ai/context.js
Comment thread packages/bruno-electron/src/ipc/ai/context.js
Comment thread packages/bruno-electron/src/ipc/ai/index.js Outdated
Comment thread packages/bruno-electron/src/ipc/ai/script-prompts.js
Comment thread packages/bruno-electron/src/ipc/ai/script-prompts.js Outdated
utkarsh-bruno
utkarsh-bruno previously approved these changes Jun 29, 2026

@utkarsh-bruno utkarsh-bruno left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good to me.

@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.

Actionable comments posted: 1

🤖 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/utils/ai/index.spec.js`:
- Around line 234-252: The secret-override and OAuth2 tests in
buildAiVariablesPayload are missing assertions for the scope field, so they
don’t verify the source classification that downstream variable listing/search
relies on. Update the affected specs in index.spec.js to assert tok.scope (and
the corresponding OAuth2 variable scope) alongside value and secret, using the
existing buildAiVariablesPayload and variable lookup logic so the tests cover
the observable output. Keep the expectations aligned with the intended source
semantics for each case.
🪄 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: b5edd169-9a48-4633-a831-78f3c05fa9b9

📥 Commits

Reviewing files that changed from the base of the PR and between 5935e71 and cea7083.

📒 Files selected for processing (12)
  • packages/bruno-app/src/components/AiChatSidebar/index.js
  • packages/bruno-app/src/components/FolderSettings/Documentation/index.js
  • packages/bruno-app/src/components/FolderSettings/Script/index.js
  • packages/bruno-app/src/components/FolderSettings/Tests/index.js
  • packages/bruno-app/src/utils/ai/index.js
  • packages/bruno-app/src/utils/ai/index.spec.js
  • packages/bruno-electron/src/ipc/ai/chat-prompts.js
  • packages/bruno-electron/src/ipc/ai/chat.js
  • packages/bruno-electron/src/ipc/ai/context.js
  • packages/bruno-electron/src/ipc/ai/context.spec.js
  • packages/bruno-electron/src/ipc/ai/index.js
  • packages/bruno-electron/src/ipc/ai/script-prompts.js
🚧 Files skipped from review as they are similar to previous changes (10)
  • packages/bruno-app/src/components/FolderSettings/Tests/index.js
  • packages/bruno-app/src/components/FolderSettings/Script/index.js
  • packages/bruno-app/src/components/FolderSettings/Documentation/index.js
  • packages/bruno-app/src/components/AiChatSidebar/index.js
  • packages/bruno-electron/src/ipc/ai/index.js
  • packages/bruno-electron/src/ipc/ai/chat-prompts.js
  • packages/bruno-electron/src/ipc/ai/script-prompts.js
  • packages/bruno-app/src/utils/ai/index.js
  • packages/bruno-electron/src/ipc/ai/context.js
  • packages/bruno-electron/src/ipc/ai/chat.js

Comment thread packages/bruno-app/src/utils/ai/index.spec.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants