Elffuss BraidHub

kikocis/continue public

main · 1.6 KiBRawPermalink
1import { DiffLine, ILLM } from "../..";
2import { generateLines } from "../../diff/util";
3import { supportedLanguages } from "../../util/treeSitter";
4import { getUriFileExtension } from "../../util/uri";
5import { deterministicApplyLazyEdit } from "./deterministic";
6import { streamLazyApply } from "./streamLazyApply";
7import { applyUnifiedDiff, isUnifiedDiffFormat } from "./unifiedDiffApply";
8
9function canUseInstantApply(filename: string) {
10 const fileExtension = getUriFileExtension(filename);
11 return supportedLanguages[fileExtension] !== undefined;
12}
13
14export async function applyCodeBlock(
15 oldFile: string,
16 newLazyFile: string,
17 filename: string,
18 llm: ILLM,
19 abortController: AbortController,
20): Promise<{
21 isInstantApply: boolean;
22 diffLinesGenerator: AsyncGenerator<DiffLine>;
23}> {
24 if (canUseInstantApply(filename)) {
25 const diffLines = await deterministicApplyLazyEdit({
26 oldFile,
27 newLazyFile,
28 filename,
29 onlyFullFileRewrite: true,
30 });
31
32 if (diffLines !== undefined) {
33 return {
34 isInstantApply: true,
35 diffLinesGenerator: generateLines(diffLines!),
36 };
37 }
38 }
39
40 // If the code block is a diff
41 if (isUnifiedDiffFormat(newLazyFile)) {
42 try {
43 const diffLines = applyUnifiedDiff(oldFile, newLazyFile);
44 return {
45 isInstantApply: true,
46 diffLinesGenerator: generateLines(diffLines!),
47 };
48 } catch (e) {
49 console.error("Failed to apply unified diff", e);
50 }
51 }
52
53 return {
54 isInstantApply: false,
55 diffLinesGenerator: streamLazyApply(
56 oldFile,
57 filename,
58 newLazyFile,
59 llm,
60 abortController,
61 ),
62 };
63}