Elffuss BraidHub

kikocis/continue public

main · 3.3 KiBRawPermalink
1import { ILLM } from "../..";
2import { filterLeadingNewline } from "../../autocomplete/filtering/streamTransforms/lineStream";
3import { streamLines } from "../../diff/util";
4import { dedent } from "../../util";
5
6export const BUFFER_LINES_BELOW = 3;
7
8const MATCH_LINES_ABOVE = 1;
9export function getReplacementByMatching(
10 oldCode: string,
11 linesBefore: string[],
12 linesAfter: string[],
13): string | undefined {
14 const oldLines = oldCode.split("\n");
15 const linesToMatchAbove = MATCH_LINES_ABOVE;
16 const linesToMatchBelow = Math.min(BUFFER_LINES_BELOW, linesAfter.length);
17
18 // Get surrounding lines around the gap
19 const beforeContext = linesBefore.slice(-linesToMatchAbove).join("\n");
20 const afterContext = linesAfter.slice(0, linesToMatchBelow).join("\n");
21
22 // Find the start index in the old code
23 const startIndex = oldLines.findIndex((line, index) => {
24 const chunk = oldLines.slice(index, index + linesToMatchAbove).join("\n");
25 return chunk === beforeContext;
26 });
27
28 if (startIndex === -1) {
29 return undefined; // Couldn't find matching start
30 }
31
32 // Find the end index in the old code
33 const endIndex = oldLines.findIndex((line, index) => {
34 if (index <= startIndex + linesToMatchBelow) {
35 return false;
36 }
37 const chunk = oldLines.slice(index, index + linesToMatchBelow).join("\n");
38 return chunk === afterContext;
39 });
40
41 if (endIndex === -1) {
42 return undefined; // Couldn't find matching end
43 }
44
45 // Extract the replacement code
46 const replacement = oldLines
47 .slice(startIndex + linesToMatchAbove, endIndex)
48 .join("\n");
49
50 return replacement;
51}
52
53const REPLACE_HERE = "// REPLACE HERE //";
54export async function* getReplacementWithLlm(
55 oldCode: string,
56 linesBefore: string[],
57 linesAfter: string[],
58 llm: ILLM,
59 abortController: AbortController,
60): AsyncGenerator<string> {
61 const userPrompt = dedent`
62 ORIGINAL CODE:
63 \`\`\`
64 ${oldCode}
65 \`\`\`
66
67 UPDATED CODE:
68 \`\`\`
69 ${linesBefore.join("\n")}
70 ${REPLACE_HERE}
71 ${linesAfter.join("\n")}
72 \`\`\`
73
74 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.
75
76 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.
77 `;
78
79 const assistantPrompt = dedent`
80 Here is the snippet of code that will replace "${REPLACE_HERE}" in the new version:
81 \`\`\`
82 `;
83
84 const completion = await llm.streamChat(
85 [
86 { role: "user", content: userPrompt },
87 { role: "assistant", content: assistantPrompt },
88 ],
89 abortController.signal,
90 {
91 raw: true,
92 prediction: undefined,
93 reasoning: false,
94 },
95 );
96
97 let lines = streamLines(completion);
98 lines = filterLeadingNewline(lines);
99 // We want to retrive everything from the llm
100 // then let the filterCodeBlocks function clean up for any trailing text.
101 // if we stop here early, we run the risk of loosing inner markdown content.
102
103 for await (const line of lines) {
104 yield line;
105 }
106}