Changesets · Branches · Tags · Compare
| Changeset | d26a7e2819fe5e05903254099848df1592c60f2bcae8c42a050fedf357d9422f |
|---|---|
| Author | Nate <sestinj@gmail.com> · 2025-09-09 23:53 -07:00 |
| Committer | Nate <sestinj@gmail.com> · 2025-09-09 23:53 -07:00 |
| Parents | c1b3fd06027c |
1 file changed
modified extensions/vscode/src/apply/ApplyManager.ts +127 −8
| @@ -7,6 +7,9 @@ | ||
| 7 | 7 | import { myersDiff } from "core/diff/myers"; |
| 8 | 8 | import { generateLines } from "core/diff/util"; |
| 9 | 9 | 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"; | |
| 10 | 13 | import { VerticalDiffManager } from "../diff/vertical/manager"; |
| 11 | 14 | import { VsCodeIde } from "../VsCodeIde"; |
| 12 | 15 | import { VsCodeWebviewProtocol } from "../webviewProtocol"; |
| @@ -92,6 +95,10 @@ | ||
| 92 | 95 | await this.ide.openFile(filepath); |
| 93 | 96 | } |
| 94 | 97 | |
| 98 | + private modelIsTooFastForStreaming(model: string): boolean { | |
| 99 | + return [/mercury/].some((r) => r.test(model)); | |
| 100 | + } | |
| 101 | + | |
| 95 | 102 | private async handleEmptyDocument( |
| 96 | 103 | editor: vscode.TextEditor, |
| 97 | 104 | text: string, |
| @@ -159,6 +166,7 @@ | ||
| 159 | 166 | streamId, |
| 160 | 167 | this.verticalDiffManager, |
| 161 | 168 | toolCallId, |
| 169 | + !this.modelIsTooFastForStreaming(llm.model), | |
| 162 | 170 | ); |
| 163 | 171 | } |
| 164 | 172 | } |
| @@ -170,6 +178,37 @@ | ||
| 170 | 178 | return `The following code was suggested as an edit:\n\`\`\`\n${text}\n\`\`\`\nPlease apply it to the previous code.`; |
| 171 | 179 | } |
| 172 | 180 | |
| 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 | + | |
| 173 | 212 | private async handleNonInstantDiff( |
| 174 | 213 | editor: vscode.TextEditor, |
| 175 | 214 | text: string, |
| @@ -177,6 +216,7 @@ | ||
| 177 | 216 | streamId: string, |
| 178 | 217 | verticalDiffManager: VerticalDiffManager, |
| 179 | 218 | toolCallId?: string, |
| 219 | + streaming: boolean = true, | |
| 180 | 220 | ) { |
| 181 | 221 | const { config } = await this.configHandler.loadConfig(); |
| 182 | 222 | if (!config) { |
| @@ -195,14 +235,93 @@ | ||
| 195 | 235 | ? fullEditorRange |
| 196 | 236 | : editor.selection; |
| 197 | 237 | |
| 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, | |
| 200 | 288 | 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 | + } | |
| 207 | 326 | } |
| 208 | 327 | } |