| 1 | import { |
| 2 | filterLeadingAndTrailingNewLineInsertion, |
| 3 | filterLeadingNewline, |
| 4 | removeTrailingWhitespace, |
| 5 | } from "../../autocomplete/filtering/streamTransforms/lineStream.js"; |
| 6 | import { streamDiff } from "../../diff/streamDiff.js"; |
| 7 | import { LineStream, streamLines } from "../../diff/util.js"; |
| 8 | import { DiffLine, ILLM } from "../../index.js"; |
| 9 | import { stopAtLinesWithMarkdownSupport } from "../../utils/streamMarkdownUtils.js"; |
| 10 | |
| 11 | import { lazyApplyPromptForModel, UNCHANGED_CODE } from "./prompts.js"; |
| 12 | import { BUFFER_LINES_BELOW, getReplacementWithLlm } from "./replace.js"; |
| 13 | |
| 14 | export async function* streamLazyApply( |
| 15 | oldCode: string, |
| 16 | filename: string, |
| 17 | newCode: string, |
| 18 | llm: ILLM, |
| 19 | abortController: AbortController, |
| 20 | ): AsyncGenerator<DiffLine> { |
| 21 | const promptFactory = lazyApplyPromptForModel(llm.model, llm.providerName); |
| 22 | if (!promptFactory) { |
| 23 | throw new Error(`Lazy apply not supported for model ${llm.model}`); |
| 24 | } |
| 25 | |
| 26 | const promptMessages = promptFactory(oldCode, filename, newCode); |
| 27 | const lazyCompletion = llm.streamChat(promptMessages, abortController.signal); |
| 28 | |
| 29 | // Do find and replace over the lazy edit response |
| 30 | async function* replacementFunction( |
| 31 | oldCode: string, |
| 32 | linesBefore: string[], |
| 33 | linesAfter: string[], |
| 34 | ): AsyncGenerator<string> { |
| 35 | for await (const line of getReplacementWithLlm( |
| 36 | oldCode, |
| 37 | linesBefore, |
| 38 | linesAfter, |
| 39 | llm, |
| 40 | abortController, |
| 41 | )) { |
| 42 | yield line; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | let lazyCompletionLines = streamLines(lazyCompletion, true); |
| 47 | |
| 48 | lazyCompletionLines = stopAtLinesWithMarkdownSupport( |
| 49 | lazyCompletionLines, |
| 50 | filename, |
| 51 | ); |
| 52 | |
| 53 | lazyCompletionLines = filterLeadingNewline(lazyCompletionLines); |
| 54 | lazyCompletionLines = removeTrailingWhitespace(lazyCompletionLines); |
| 55 | |
| 56 | // Fill in unchanged code |
| 57 | let lines = streamFillUnchangedCode( |
| 58 | lazyCompletionLines, |
| 59 | oldCode, |
| 60 | replacementFunction, |
| 61 | ); |
| 62 | |
| 63 | // Convert output to diff |
| 64 | const oldLines = oldCode.split(/\r?\n/); |
| 65 | let diffLines = streamDiff(oldLines, lines); |
| 66 | diffLines = filterLeadingAndTrailingNewLineInsertion(diffLines); |
| 67 | for await (const diffLine of diffLines) { |
| 68 | yield diffLine; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | async function* streamFillUnchangedCode( |
| 73 | lines: LineStream, |
| 74 | oldCode: string, |
| 75 | replacementFunction: ( |
| 76 | oldCode: string, |
| 77 | linesBefore: string[], |
| 78 | linesAfter: string[], |
| 79 | ) => AsyncGenerator<string>, |
| 80 | ): LineStream { |
| 81 | const newLines = []; |
| 82 | let buffer = []; |
| 83 | let waitingForBuffer = false; |
| 84 | |
| 85 | for await (const line of lines) { |
| 86 | if (waitingForBuffer) { |
| 87 | buffer.push(line); |
| 88 | |
| 89 | if (buffer.length >= BUFFER_LINES_BELOW) { |
| 90 | // Find the replacement and continue streaming once we have it |
| 91 | const replacementLines = replacementFunction(oldCode, newLines, buffer); |
| 92 | let replacement = ""; |
| 93 | for await (const replacementLine of replacementLines) { |
| 94 | yield replacementLine; |
| 95 | newLines.push(replacementLine); |
| 96 | replacement += replacementLine + "\n"; |
| 97 | } |
| 98 | // Yield the buffered lines |
| 99 | for (const bufferedLine of buffer) { |
| 100 | yield bufferedLine; |
| 101 | newLines.push(bufferedLine); |
| 102 | } |
| 103 | |
| 104 | waitingForBuffer = false; |
| 105 | buffer = []; |
| 106 | continue; |
| 107 | } else { |
| 108 | continue; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | if (line.includes(UNCHANGED_CODE)) { |
| 113 | // Buffer so we can give the context of BUFFER_LINES_BELOW lines below |
| 114 | waitingForBuffer = true; |
| 115 | // TODO: If the UNCHANGED CODE is at the very top of the file we need to handle a bit differently |
| 116 | } else { |
| 117 | yield line; |
| 118 | newLines.push(line); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | if (waitingForBuffer) { |
| 123 | // If we're still waiting for a buffer, we've reached the end of the stream |
| 124 | // and we should just look for the replacement with what we have |
| 125 | const replacementLines = replacementFunction(oldCode, newLines, buffer); |
| 126 | for await (const replacementLine of replacementLines) { |
| 127 | yield replacementLine; |
| 128 | newLines.push(replacementLine); |
| 129 | } |
| 130 | // Yield the buffered lines |
| 131 | for (const bufferedLine of buffer) { |
| 132 | yield bufferedLine; |
| 133 | newLines.push(bufferedLine); |
| 134 | } |
| 135 | } |
| 136 | } |