Elffuss BraidHub

kikocis/continue public

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