Elffuss BraidHub

kikocis/continue public

c657d7bd6e88 · 3.1 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 abortController: AbortController,
63): AsyncGenerator<string> {
64 const userPrompt = dedent`
65 ORIGINAL CODE:
66 \`\`\`
67 ${oldCode}
68 \`\`\`
69
70 UPDATED CODE:
71 \`\`\`
72 ${linesBefore.join("\n")}
73 ${REPLACE_HERE}
74 ${linesAfter.join("\n")}
75 \`\`\`
76
77 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.
78
79 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.
80 `;
81
82 const assistantPrompt = dedent`
83 Here is the snippet of code that will replace "${REPLACE_HERE}" in the new version:
84 \`\`\`
85 `;
86
87 const completion = await llm.streamChat(
88 [
89 { role: "user", content: userPrompt },
90 { role: "assistant", content: assistantPrompt },
91 ],
92 abortController.signal,
93 );
94
95 let lines = streamLines(completion);
96 lines = filterLeadingNewline(lines);
97 lines = stopAtLines(lines, () => {}, ["```"]);
98
99 for await (const line of lines) {
100 yield line;
101 }
102}