Elffuss BraidHub

kikocis/continue public

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