Elffuss BraidHub

kikocis/continue public

94488b344dec · 3.0 KiBRawPermalink
1import { ILLM } from "../..";
2import {
3 filterLeadingNewline,
4 stopAtLines,
5} from "../../autocomplete/filtering/streamTransforms/lineStream";
6import { streamLines } from "../../diff/util";
7import { dedent } from "../../util";
8
9export const BUFFER_LINES_BELOW = 3;
10
11const MATCH_LINES_ABOVE = 1;
12export function getReplacementByMatching(
13 oldCode: string,
14 linesBefore: string[],
15 linesAfter: string[],
16): string | undefined {
17 const oldLines = oldCode.split("\n");
18 const linesToMatchAbove = MATCH_LINES_ABOVE;
19 const linesToMatchBelow = Math.min(BUFFER_LINES_BELOW, linesAfter.length);
20
21 // Get surrounding lines around the gap
22 const beforeContext = linesBefore.slice(-linesToMatchAbove).join("\n");
23 const afterContext = linesAfter.slice(0, linesToMatchBelow).join("\n");
24
25 // Find the start index in the old code
26 const startIndex = oldLines.findIndex((line, index) => {
27 const chunk = oldLines.slice(index, index + linesToMatchAbove).join("\n");
28 return chunk === beforeContext;
29 });
30
31 if (startIndex === -1) {
32 return undefined; // Couldn't find matching start
33 }
34
35 // Find the end index in the old code
36 const endIndex = oldLines.findIndex((line, index) => {
37 if (index <= startIndex + linesToMatchBelow) {
38 return false;
39 }
40 const chunk = oldLines.slice(index, index + linesToMatchBelow).join("\n");
41 return chunk === afterContext;
42 });
43
44 if (endIndex === -1) {
45 return undefined; // Couldn't find matching end
46 }
47
48 // Extract the replacement code
49 const replacement = oldLines
50 .slice(startIndex + linesToMatchAbove, endIndex)
51 .join("\n");
52
53 return replacement;
54}
55
56const REPLACE_HERE = "// REPLACE HERE //";
57export async function* getReplacementWithLlm(
58 oldCode: string,
59 linesBefore: string[],
60 linesAfter: string[],
61 llm: ILLM,
62): AsyncGenerator<string> {
63 const userPrompt = dedent`
64 ORIGINAL CODE:
65 \`\`\`
66 ${oldCode}
67 \`\`\`
68
69 UPDATED CODE:
70 \`\`\`
71 ${linesBefore.join("\n")}
72 ${REPLACE_HERE}
73 ${linesAfter.join("\n")}
74 \`\`\`
75
76 Above is an original version of a file, followed by a newer version that is in the process of being written. The new version contains a section which is exactly the same as in the original code, and has been marked with "${REPLACE_HERE}". Your task is to give the exact snippet of code from the original code that should replace "${REPLACE_HERE}" in the new version.
77
78 Your output should be a single code block. We will paste the contents of that code block directly into the new version, so make sure that it has correct indentation.
79 `;
80
81 const assistantPrompt = dedent`
82 Here is the snippet of code that will replace "${REPLACE_HERE}" in the new version:
83 \`\`\`
84 `;
85
86 const completion = await llm.streamChat(
87 [
88 { role: "user", content: userPrompt },
89 { role: "assistant", content: assistantPrompt },
90 ],
91 new AbortController().signal,
92 );
93
94 let lines = streamLines(completion);
95 lines = filterLeadingNewline(lines);
96 lines = stopAtLines(lines, () => {}, ["```"]);
97
98 for await (const line of lines) {
99 yield line;
100 }
101}