Elffuss BraidHub

kikocis/continue public

76e6219ef348 · 5.5 KiBRawPermalink
1import { ConfigHandler } from "core/config/ConfigHandler";
2import { applyCodeBlock } from "core/edit/lazy/applyCodeBlock";
3import { getUriPathBasename } from "core/util/uri";
4import * as vscode from "vscode";
5
6import { ApplyToFilePayload } from "core";
7import { myersDiff } from "core/diff/myers";
8import { generateLines } from "core/diff/util";
9import { ApplyAbortManager } from "core/edit/applyAbortManager";
10import { VerticalDiffManager } from "../diff/vertical/manager";
11import { VsCodeIde } from "../VsCodeIde";
12import { VsCodeWebviewProtocol } from "../webviewProtocol";
13
14/**
15 * Handles applying text/code to files including diff generation and streaming
16 */
17export class ApplyManager {
18 constructor(
19 private readonly ide: VsCodeIde,
20 private readonly webviewProtocol: VsCodeWebviewProtocol,
21 private readonly verticalDiffManager: VerticalDiffManager,
22 private readonly configHandler: ConfigHandler,
23 ) {}
24
25 async applyToFile({
26 streamId,
27 filepath,
28 text,
29 toolCallId,
30 isSearchAndReplace,
31 }: ApplyToFilePayload) {
32 if (filepath) {
33 await this.ensureFileOpen(filepath);
34 }
35
36 const { activeTextEditor } = vscode.window;
37 if (!activeTextEditor) {
38 void vscode.window.showErrorMessage("No active editor to apply edits to");
39 return;
40 }
41
42 // Capture the original file content before applying changes
43 const originalFileContent = activeTextEditor.document.getText();
44
45 await this.webviewProtocol.request("updateApplyState", {
46 streamId,
47 status: "streaming",
48 fileContent: text,
49 originalFileContent,
50 toolCallId,
51 });
52
53 const hasExistingDocument = !!activeTextEditor.document.getText().trim();
54
55 if (hasExistingDocument) {
56 // Currently `isSearchAndReplace` will always provide a full file rewrite
57 // as the contents of `text`, so we can just instantly apply
58 if (isSearchAndReplace) {
59 const diffLinesGenerator = generateLines(
60 myersDiff(activeTextEditor.document.getText(), text),
61 );
62
63 await this.verticalDiffManager.streamDiffLines(
64 diffLinesGenerator,
65 true,
66 streamId,
67 toolCallId,
68 );
69 } else {
70 await this.handleExistingDocument(
71 activeTextEditor,
72 text,
73 streamId,
74 toolCallId,
75 );
76 }
77 } else {
78 await this.handleEmptyDocument(
79 activeTextEditor,
80 text,
81 streamId,
82 toolCallId,
83 );
84 }
85 }
86
87 private async ensureFileOpen(filepath: string): Promise<void> {
88 const fileExists = await this.ide.fileExists(filepath);
89 if (!fileExists) {
90 await this.ide.writeFile(filepath, "");
91 await this.ide.openFile(filepath);
92 }
93 await this.ide.openFile(filepath);
94 }
95
96 private async handleEmptyDocument(
97 editor: vscode.TextEditor,
98 text: string,
99 streamId: string,
100 toolCallId?: string,
101 ) {
102 await editor.edit((builder) =>
103 builder.insert(new vscode.Position(0, 0), text),
104 );
105
106 await this.webviewProtocol.request("updateApplyState", {
107 streamId,
108 status: "closed",
109 numDiffs: 0,
110 fileContent: text,
111 toolCallId,
112 });
113 }
114
115 private async handleExistingDocument(
116 editor: vscode.TextEditor,
117 text: string,
118 streamId: string,
119 toolCallId?: string,
120 ) {
121 const { config } = await this.configHandler.loadConfig();
122 if (!config) {
123 void vscode.window.showErrorMessage("Config not loaded");
124 return;
125 }
126
127 const llm =
128 config.selectedModelByRole.apply ?? config.selectedModelByRole.chat;
129 if (!llm) {
130 void vscode.window.showErrorMessage(
131 `No model with roles "apply" or "chat" found in config.`,
132 );
133 return;
134 }
135
136 const fileUri = editor.document.uri.toString();
137 const abortManager = ApplyAbortManager.getInstance();
138 const abortController = abortManager.get(fileUri);
139
140 const { isInstantApply, diffLinesGenerator } = await applyCodeBlock(
141 editor.document.getText(),
142 text,
143 getUriPathBasename(fileUri),
144 llm,
145 abortController,
146 );
147
148 if (isInstantApply) {
149 await this.verticalDiffManager.streamDiffLines(
150 diffLinesGenerator,
151 isInstantApply,
152 streamId,
153 toolCallId,
154 );
155 } else {
156 await this.handleNonInstantDiff(
157 editor,
158 text,
159 llm,
160 streamId,
161 this.verticalDiffManager,
162 toolCallId,
163 );
164 }
165 }
166
167 /**
168 * Creates a prompt for applying code edits
169 */
170 private getApplyPrompt(text: string): string {
171 return `The following code was suggested as an edit:\n\`\`\`\n${text}\n\`\`\`\nPlease apply it to the previous code.`;
172 }
173
174 private async handleNonInstantDiff(
175 editor: vscode.TextEditor,
176 text: string,
177 llm: any,
178 streamId: string,
179 verticalDiffManager: VerticalDiffManager,
180 toolCallId?: string,
181 ) {
182 const { config } = await this.configHandler.loadConfig();
183 if (!config) {
184 void vscode.window.showErrorMessage("Config not loaded");
185 return;
186 }
187
188 const prompt = this.getApplyPrompt(text);
189 const fullEditorRange = new vscode.Range(
190 0,
191 0,
192 editor.document.lineCount - 1,
193 editor.document.lineAt(editor.document.lineCount - 1).text.length,
194 );
195 const rangeToApplyTo = editor.selection.isEmpty
196 ? fullEditorRange
197 : editor.selection;
198
199 await verticalDiffManager.streamEdit({
200 input: prompt,
201 llm,
202 streamId,
203 range: rangeToApplyTo,
204 newCode: text,
205 toolCallId,
206 rulesToInclude: undefined, // No rules for apply
207 });
208 }
209}