Elffuss BraidHub

kikocis/continue public

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