Elffuss BraidHub

kikocis/continue public

Merge pull request #7681 from continuedev/nate/apply-updates

feat: add filepath to apply prompt template vars
Changesete10d18bdede85e2850543d1432489a95c4ede2340d920ba88251f3a7646cbe0e
AuthorNate Sesti <33237525+sestinj@users.noreply.github.com> · 2025-09-10 02:07 -07:00
CommitterGitHub <noreply@github.com> · 2025-09-10 02:07 -07:00
Parentsf3410973ba02, d26a7e2819fe

Browse the files at this changeset

4 files changed

modified core/edit/streamDiffLines.ts +18 −17
@@ -108,23 +108,24 @@
108108
109109 // Rules can be included with edit prompt
110110 // If any rules are present this will result in using chat instead of legacy completion
111- const systemMessage = rulesToInclude
112- ? getSystemMessageWithRules({
113- availableRules: rulesToInclude,
114- userMessage:
115- typeof prompt === "string"
116- ? ({
117- role: "user",
118- content: prompt,
119- } as UserChatMessage)
120- : (findLast(
121- prompt,
122- (msg) => msg.role === "user" || msg.role === "tool",
123- ) as UserChatMessage | ToolResultChatMessage | undefined),
124- baseSystemMessage: undefined,
125- contextItems: [],
126- }).systemMessage
127- : undefined;
111+ const systemMessage =
112+ rulesToInclude || llm.baseChatSystemMessage
113+ ? getSystemMessageWithRules({
114+ availableRules: rulesToInclude ?? [],
115+ userMessage:
116+ typeof prompt === "string"
117+ ? ({
118+ role: "user",
119+ content: prompt,
120+ } as UserChatMessage)
121+ : (findLast(
122+ prompt,
123+ (msg) => msg.role === "user" || msg.role === "tool",
124+ ) as UserChatMessage | ToolResultChatMessage | undefined),
125+ baseSystemMessage: llm.baseChatSystemMessage,
126+ contextItems: [],
127+ }).systemMessage
128+ : undefined;
128129
129130 if (systemMessage) {
130131 if (typeof prompt === "string") {
modified extensions/vscode/src/apply/ApplyManager.ts +127 −8
@@ -7,6 +7,9 @@
77 import { myersDiff } from "core/diff/myers";
88 import { generateLines } from "core/diff/util";
99 import { ApplyAbortManager } from "core/edit/applyAbortManager";
10+import { streamDiffLines } from "core/edit/streamDiffLines";
11+import { pruneLinesFromBottom, pruneLinesFromTop } from "core/llm/countTokens";
12+import { getMarkdownLanguageTagForFile } from "core/util";
1013 import { VerticalDiffManager } from "../diff/vertical/manager";
1114 import { VsCodeIde } from "../VsCodeIde";
1215 import { VsCodeWebviewProtocol } from "../webviewProtocol";
@@ -92,6 +95,10 @@
9295 await this.ide.openFile(filepath);
9396 }
9497
98+ private modelIsTooFastForStreaming(model: string): boolean {
99+ return [/mercury/].some((r) => r.test(model));
100+ }
101+
95102 private async handleEmptyDocument(
96103 editor: vscode.TextEditor,
97104 text: string,
@@ -159,6 +166,7 @@
159166 streamId,
160167 this.verticalDiffManager,
161168 toolCallId,
169+ !this.modelIsTooFastForStreaming(llm.model),
162170 );
163171 }
164172 }
@@ -170,6 +178,37 @@
170178 return `The following code was suggested as an edit:\n\`\`\`\n${text}\n\`\`\`\nPlease apply it to the previous code.`;
171179 }
172180
181+ /**
182+ * Calculates prefix and suffix for a given range, shared between streaming and non-streaming modes
183+ */
184+ private calculatePrefixSuffix(
185+ editor: vscode.TextEditor,
186+ range: vscode.Range,
187+ llm: any,
188+ ): { prefix: string; suffix: string; rangeContent: string } {
189+ const rangeContent = editor.document.getText(range);
190+
191+ const prefix = pruneLinesFromTop(
192+ editor.document.getText(
193+ new vscode.Range(new vscode.Position(0, 0), range.start),
194+ ),
195+ llm.contextLength / 4,
196+ llm.model,
197+ );
198+ const suffix = pruneLinesFromBottom(
199+ editor.document.getText(
200+ new vscode.Range(
201+ range.end,
202+ new vscode.Position(editor.document.lineCount, 0),
203+ ),
204+ ),
205+ llm.contextLength / 4,
206+ llm.model,
207+ );
208+
209+ return { prefix, suffix, rangeContent };
210+ }
211+
173212 private async handleNonInstantDiff(
174213 editor: vscode.TextEditor,
175214 text: string,
@@ -177,6 +216,7 @@
177216 streamId: string,
178217 verticalDiffManager: VerticalDiffManager,
179218 toolCallId?: string,
219+ streaming: boolean = true,
180220 ) {
181221 const { config } = await this.configHandler.loadConfig();
182222 if (!config) {
@@ -195,14 +235,93 @@
195235 ? fullEditorRange
196236 : editor.selection;
197237
198- await verticalDiffManager.streamEdit({
199- input: prompt,
238+ if (streaming) {
239+ await verticalDiffManager.streamEdit({
240+ input: prompt,
241+ llm,
242+ streamId,
243+ range: rangeToApplyTo,
244+ newCode: text,
245+ toolCallId,
246+ rulesToInclude: undefined, // No rules for apply
247+ });
248+ } else {
249+ // Non-streaming: accumulate LLM output, then apply via Myers diff
250+ const finalContent = await this.generateAppliedContent(
251+ editor,
252+ prompt,
253+ llm,
254+ rangeToApplyTo,
255+ text,
256+ );
257+
258+ if (finalContent) {
259+ const diffLinesGenerator = generateLines(
260+ myersDiff(editor.document.getText(), finalContent),
261+ );
262+
263+ await verticalDiffManager.streamDiffLines(
264+ diffLinesGenerator,
265+ true, // Apply instantly since we accumulated all content
266+ streamId,
267+ toolCallId,
268+ );
269+ }
270+ }
271+ }
272+
273+ /**
274+ * Generates the final applied content by accumulating all LLM output
275+ * Similar to streamEdit but collects all output before applying
276+ */
277+ private async generateAppliedContent(
278+ editor: vscode.TextEditor,
279+ prompt: string,
280+ llm: any,
281+ range: vscode.Range,
282+ newCode: string,
283+ ): Promise<string | undefined> {
284+ const fileUri = editor.document.uri.toString();
285+ const { prefix, suffix, rangeContent } = this.calculatePrefixSuffix(
286+ editor,
287+ range,
200288 llm,
201- streamId,
202- range: rangeToApplyTo,
203- newCode: text,
204- toolCallId,
205- rulesToInclude: undefined, // No rules for apply
206- });
289+ );
290+
291+ const abortManager = ApplyAbortManager.getInstance();
292+ const abortController = abortManager.get(fileUri);
293+
294+ try {
295+ const streamedLines: string[] = [];
296+
297+ // Use streamDiffLines to get the LLM output
298+ const stream = streamDiffLines({
299+ highlighted: rangeContent,
300+ prefix,
301+ suffix,
302+ llm,
303+ rulesToInclude: undefined, // No rules for apply
304+ input: prompt,
305+ language: getMarkdownLanguageTagForFile(fileUri),
306+ overridePrompt: undefined,
307+ abortController,
308+ });
309+
310+ // Accumulate all the streamed content
311+ for await (const line of stream) {
312+ if (abortController.signal.aborted) {
313+ return undefined;
314+ }
315+ if (line.type === "new" || line.type === "same") {
316+ streamedLines.push(line.line);
317+ }
318+ }
319+
320+ // Return the complete file content
321+ return `${prefix}${streamedLines.join("\n")}${suffix}`;
322+ } catch (error) {
323+ console.error("Error generating applied content:", error);
324+ return undefined;
325+ }
207326 }
208327 }
modified extensions/vscode/src/diff/vertical/manager.ts +3 −0
@@ -13,6 +13,7 @@
1313 import { ApplyAbortManager } from "core/edit/applyAbortManager";
1414 import { EDIT_MODE_STREAM_ID } from "core/edit/constants";
1515 import { stripImages } from "core/util/messageContent";
16+import { getLastNPathParts } from "core/util/uri";
1617 import { editOutcomeTracker } from "../../extension/EditOutcomeTracker";
1718 import { VerticalDiffHandler, VerticalDiffHandlerOptions } from "./handler";
1819
@@ -432,9 +433,11 @@
432433
433434 let overridePrompt: ChatMessage[] | undefined;
434435 if (llm.promptTemplates?.apply) {
436+ const filepath = getLastNPathParts(fileUri, 1);
435437 const rendered = llm.renderPromptTemplate(llm.promptTemplates.apply, [], {
436438 original_code: rangeContent,
437439 new_code: newCode ?? "",
440+ filepath,
438441 });
439442 overridePrompt =
440443 typeof rendered === "string"
modified gui/src/util/clientTools/editImpl.ts +27 −5
@@ -12,12 +12,28 @@
1212 "`filepath` and `changes` arguments are required to edit an existing file.",
1313 );
1414 }
15- const firstUriMatch = await resolveRelativePathInDir(
16- args.filepath,
15+ let filepath = args.filepath;
16+ if (filepath.startsWith("./")) {
17+ filepath = filepath.slice(2);
18+ }
19+
20+ const openFiles = await extras.ideMessenger.ide.getOpenFiles();
21+ let firstUriMatch = await resolveRelativePathInDir(
22+ filepath,
1723 extras.ideMessenger.ide,
1824 );
25+
26+ if (!firstUriMatch) {
27+ for (const uri of openFiles) {
28+ if (uri.endsWith(filepath)) {
29+ firstUriMatch = uri;
30+ break;
31+ }
32+ }
33+ }
34+
1935 if (!firstUriMatch) {
20- throw new Error(`${args.filepath} does not exist`);
36+ throw new Error(`${filepath} does not exist`);
2137 }
2238 const streamId = uuid();
2339 void extras.dispatch(
@@ -30,7 +46,13 @@
3046 );
3147
3248 return {
33- respondImmediately: false,
34- output: undefined, // No immediate output.
49+ respondImmediately: true,
50+ output: [
51+ {
52+ name: "File edited successfully",
53+ content: `Successfully initiated edit for ${firstUriMatch}`,
54+ description: "File edit operation started",
55+ },
56+ ],
3557 };
3658 };