Elffuss BraidHub

kikocis/continue public

22e692d80113 · 4.0 KiBRawPermalink
1import {
2 filterLeadingAndTrailingNewLineInsertion,
3 filterLeadingNewline,
4 removeTrailingWhitespace,
5 stopAtLines,
6} from "../../autocomplete/filtering/streamTransforms/lineStream.js";
7import { streamDiff } from "../../diff/streamDiff.js";
8import { LineStream, streamLines } from "../../diff/util.js";
9import { DiffLine, ILLM } from "../../index.js";
10
11import { lazyApplyPromptForModel, UNCHANGED_CODE } from "./prompts.js";
12import { BUFFER_LINES_BELOW, getReplacementWithLlm } from "./replace.js";
13
14export 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 // Process line output
48 // lazyCompletionLines = filterEnglishLinesAtStart(lazyCompletionLines);
49 lazyCompletionLines = stopAtLines(lazyCompletionLines, () => {}, ["```"]);
50 lazyCompletionLines = filterLeadingNewline(lazyCompletionLines);
51 lazyCompletionLines = removeTrailingWhitespace(lazyCompletionLines);
52
53 // Fill in unchanged code
54 let lines = streamFillUnchangedCode(
55 lazyCompletionLines,
56 oldCode,
57 replacementFunction,
58 );
59
60 // Convert output to diff
61 const oldLines = oldCode.split(/\r?\n/);
62 let diffLines = streamDiff(oldLines, lines);
63 diffLines = filterLeadingAndTrailingNewLineInsertion(diffLines);
64 for await (const diffLine of diffLines) {
65 yield diffLine;
66 }
67}
68
69async function* streamFillUnchangedCode(
70 lines: LineStream,
71 oldCode: string,
72 replacementFunction: (
73 oldCode: string,
74 linesBefore: string[],
75 linesAfter: string[],
76 ) => AsyncGenerator<string>,
77): LineStream {
78 const newLines = [];
79 let buffer = [];
80 let waitingForBuffer = false;
81
82 for await (const line of lines) {
83 if (waitingForBuffer) {
84 buffer.push(line);
85
86 if (buffer.length >= BUFFER_LINES_BELOW) {
87 // Find the replacement and continue streaming once we have it
88 const replacementLines = replacementFunction(oldCode, newLines, buffer);
89 let replacement = "";
90 for await (const replacementLine of replacementLines) {
91 yield replacementLine;
92 newLines.push(replacementLine);
93 replacement += replacementLine + "\n";
94 }
95
96 // Yield the buffered lines
97 for (const bufferedLine of buffer) {
98 yield bufferedLine;
99 newLines.push(bufferedLine);
100 }
101
102 waitingForBuffer = false;
103 buffer = [];
104 continue;
105 } else {
106 continue;
107 }
108 }
109
110 if (line.includes(UNCHANGED_CODE)) {
111 // Buffer so we can give the context of BUFFER_LINES_BELOW lines below
112 waitingForBuffer = true;
113 // TODO: If the UNCHANGED CODE is at the very top of the file we need to handle a bit differently
114 } else {
115 yield line;
116 newLines.push(line);
117 }
118 }
119
120 if (waitingForBuffer) {
121 // If we're still waiting for a buffer, we've reached the end of the stream
122 // and we should just look for the replacement with what we have
123 const replacementLines = replacementFunction(oldCode, newLines, buffer);
124 for await (const replacementLine of replacementLines) {
125 yield replacementLine;
126 newLines.push(replacementLine);
127 }
128 // Yield the buffered lines
129 for (const bufferedLine of buffer) {
130 yield bufferedLine;
131 newLines.push(bufferedLine);
132 }
133 }
134}