Skip to content

feat(ask): start chat from selected code range - #1553

Open
nikhil008-git wants to merge 2 commits into
sourcebot-dev:mainfrom
nikhil008-git:feat/ask-selected-code
Open

feat(ask): start chat from selected code range#1553
nikhil008-git wants to merge 2 commits into
sourcebot-dev:mainfrom
nikhil008-git:feat/ask-selected-code

Conversation

@nikhil008-git

@nikhil008-git nikhil008-git commented Aug 7, 2026

Copy link
Copy Markdown

Summary

Adds an “Ask Sourcebot” action to the existing code-selection popover in Browse and search previews.

When code is selected, Ask Sourcebot:

  • Creates a new chat.
  • Seeds the prompt with Explain this selected code.
  • Attaches the repository, revision, file path, and selected line range.
  • Resolves the file server-side at the selected revision.
  • Sends only the selected lines to the model with original line numbers.
  • Keeps Share selection behavior unchanged.

This implements issue #1534 Option A only. Side panels and inline conversations are out of scope.

Screenshots

Screenshot 2026-08-07 at 2 19 34 PM

Testing

  • Focused web tests pass: 11 files, 251 tests.
  • Web lint passes.
  • Manual Ask testing is blocked locally because this deployment has no Ask Sourcebot entitlement; the button correctly
    reports that limitation.

Use the first screenshot for Screenshots. Put the entitlement-error screenshot under Testing or omit it if you want
the PR to look cleaner.


Note

Low Risk
Changes are scoped to Ask UX and prompt assembly for optional line ranges; invalid ranges fail safe and existing full-file behavior remains when no range is set.

Overview
Adds Ask SourceBot to the code-selection popover in Browse (and shared code previews): alongside Share selection, users can open a new Ask chat seeded with Explain this selected code. and a ranged file source for the current repo, revision, path, and selected lines.

Chat creation uses new createChatFromSource on useCreateNewChatThread, which builds the user message via createUIMessage with optional explicitSources (not only file mentions).

Agent / prompt path introduces sliceFileSourceForPrompt so the server still loads the file at the chosen revision but injects only the selected slice into the dynamic <files> block, with selected_lines metadata and addLineNumbers using the real start line. Invalid ranges are dropped with a warning.

Types extend FileSource with an optional validated range (startLine / endLine) in chat and tools schemas.

Reviewed by Cursor Bugbot for commit eedef3a. Bugbot is set up for automated code reviews on this repo. Configure here.

Summary by CodeRabbit

  • New Features

    • Added an “Ask SourceBot” option to the file editor context menu.
    • Start chats from a selected file or specific line range for focused code explanations.
    • Preserve selected ranges and original line numbering in chat prompts.
  • Bug Fixes

    • Safely handle invalid or reversed line ranges.
  • Tests

    • Added coverage for ranged sources, offsets, full-file prompts, and invalid selections.

@coderabbitai

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

The editor context menu now sends selected file ranges to a new SourceBot chat. File-source validation, prompt slicing, line offsets, range metadata, explicit source propagation, and chat creation support the flow.

Changes

Selected SourceBot Chat

Layer / File(s) Summary
Source range contract and prompt handling
packages/web/src/features/tools/types.ts, packages/web/src/ee/features/chat/agent.ts, packages/web/src/ee/features/chat/agent.test.ts
FileSource supports validated line ranges. Prompt generation slices selected lines, preserves offsets, includes range metadata, and excludes invalid ranges.
Explicit source chat creation
packages/web/src/features/chat/utils.ts, packages/web/src/features/chat/utils.test.ts, packages/web/src/features/chat/useCreateNewChatThread.ts
createUIMessage accepts explicit sources. The chat thread hook creates and navigates to a chat containing the selected source.
Editor SourceBot action
packages/web/src/app/(app)/components/editorContextMenu.tsx
The context menu converts the editor selection into file and range metadata and adds an “Ask SourceBot” action.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant EditorContextMenu
  participant useCreateNewChatThread
  participant createUIMessage
  participant ChatNavigation
  EditorContextMenu->>useCreateNewChatThread: createChatFromSource(selected file range)
  useCreateNewChatThread->>createUIMessage: build message with explicit source
  createUIMessage-->>useCreateNewChatThread: data-source message part
  useCreateNewChatThread->>ChatNavigation: persist chat state and navigate
Loading

Possibly related issues

Possibly related PRs

Suggested reviewers: brendan-kellam

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
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.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes starting a chat from a selected code range, which is the main change.
✨ 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.

@nikhil008-git

Copy link
Copy Markdown
Author

closes #1534

@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/web/src/ee/features/chat/agent.ts`:
- Around line 1033-1034: Update the files parameter type in createPrompt to
include the range and lineOffset properties accessed by the prompt template,
using types consistent with their existing usage in the files.map rendering and
addLineNumbers call.
🪄 Autofix

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: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: ed24b0e1-853e-4d18-ac94-7a2ce45a6963

📥 Commits

Reviewing files that changed from the base of the PR and between 472692a and 865aac8.

📒 Files selected for processing (7)
  • packages/web/src/app/(app)/components/editorContextMenu.tsx
  • packages/web/src/ee/features/chat/agent.test.ts
  • packages/web/src/ee/features/chat/agent.ts
  • packages/web/src/features/chat/useCreateNewChatThread.ts
  • packages/web/src/features/chat/utils.test.ts
  • packages/web/src/features/chat/utils.ts
  • packages/web/src/features/tools/types.ts

Comment thread packages/web/src/ee/features/chat/agent.ts

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Want reviews to match your repository better? Bugbot Learning can learn team-specific rules from PR activity. A team admin can enable Learning in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit eedef3a. Configure here.

});

router.push(`/chat/${response.id}`);
}, [router, setChatState, toast]);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

MCP prefs ignored on Ask

Medium Severity

createChatFromSource hardcodes disabledMcpServerIds to [] in both the message metadata and session state, so chats started from a code selection always re-enable every MCP server. createNewChatThread still honors DISABLED_MCP_SERVER_IDS_LOCAL_STORAGE_KEY, so Ask-from-selection bypasses the user’s saved connector preferences.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit eedef3a. Configure here.

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.

1 participant