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