Skip to content

Commit 61eba66

Browse files
committed
fix: cap inline selection context to 500 chars, send preview for large selections
Selections over 500 chars no longer send full text to the AI prompt. Instead, a head/tail preview (first and last 3 lines, trimmed to 80 chars each) is sent with file path and line range, instructing the AI to use the Read tool for full content.
1 parent 0699917 commit 61eba66

2 files changed

Lines changed: 38 additions & 8 deletions

File tree

src-node/claude-code-agent.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,25 @@ exports.sendPrompt = async function (params) {
147147

148148
// Prepend selection context to the prompt if available
149149
let enrichedPrompt = prompt;
150-
if (selectionContext && selectionContext.selectedText) {
151-
enrichedPrompt =
152-
"The user has selected the following text in " + selectionContext.filePath +
153-
" (lines " + selectionContext.startLine + "-" + selectionContext.endLine + "):\n" +
154-
"```\n" + selectionContext.selectedText + "\n```\n\n" + prompt;
150+
if (selectionContext) {
151+
if (selectionContext.selectedText) {
152+
enrichedPrompt =
153+
"The user has selected the following text in " + selectionContext.filePath +
154+
" (lines " + selectionContext.startLine + "-" + selectionContext.endLine + "):\n" +
155+
"```\n" + selectionContext.selectedText + "\n```\n\n" + prompt;
156+
} else {
157+
let previewSnippet = "";
158+
if (selectionContext.selectionPreview) {
159+
previewSnippet = "\nPreview of selection:\n```\n" +
160+
selectionContext.selectionPreview + "\n```\n";
161+
}
162+
enrichedPrompt =
163+
"The user has selected lines " + selectionContext.startLine + "-" +
164+
selectionContext.endLine + " in " + selectionContext.filePath +
165+
". Use the Read tool with offset=" + (selectionContext.startLine - 1) +
166+
" and limit=" + (selectionContext.endLine - selectionContext.startLine + 1) +
167+
" to read the selected content if needed." + previewSnippet + "\n" + prompt;
168+
}
155169
}
156170

157171
// Run the query asynchronously — don't await here so we return requestId immediately

src/core-ai/AIChatPanel.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -506,15 +506,31 @@ define(function (require, exports, module) {
506506
// Gather selection context if available and not dismissed
507507
let selectionContext = null;
508508
if (_lastSelectionInfo && !_selectionDismissed && _lastSelectionInfo.selectedText) {
509+
const MAX_INLINE_SELECTION = 500;
510+
const MAX_PREVIEW_LINES = 3;
511+
const MAX_PREVIEW_LINE_LEN = 80;
509512
let selectedText = _lastSelectionInfo.selectedText;
510-
if (selectedText.length > 10000) {
511-
selectedText = selectedText.slice(0, 10000);
513+
let selectionPreview = null;
514+
if (selectedText.length > MAX_INLINE_SELECTION) {
515+
const lines = selectedText.split("\n");
516+
const headLines = lines.slice(0, MAX_PREVIEW_LINES).map(function (l) {
517+
return l.length > MAX_PREVIEW_LINE_LEN ? l.slice(0, MAX_PREVIEW_LINE_LEN) + "..." : l;
518+
});
519+
const tailLines = lines.length > MAX_PREVIEW_LINES * 2
520+
? lines.slice(-MAX_PREVIEW_LINES).map(function (l) {
521+
return l.length > MAX_PREVIEW_LINE_LEN ? l.slice(0, MAX_PREVIEW_LINE_LEN) + "..." : l;
522+
})
523+
: [];
524+
selectionPreview = headLines.join("\n") +
525+
(tailLines.length ? "\n...\n" + tailLines.join("\n") : "");
526+
selectedText = null;
512527
}
513528
selectionContext = {
514529
filePath: _lastSelectionInfo.filePath,
515530
startLine: _lastSelectionInfo.startLine,
516531
endLine: _lastSelectionInfo.endLine,
517-
selectedText: selectedText
532+
selectedText: selectedText,
533+
selectionPreview: selectionPreview
518534
};
519535
}
520536

0 commit comments

Comments
 (0)