Elffuss BraidHub

kikocis/continue public

6e4b7e9fc0a8 · 4.2 KiBRawPermalink
1import { longestCommonSubsequence } from "../../util/lcs.js";
2import { lineIsRepeated } from "../filtering/streamTransforms/lineStream.js";
3
4import type { ILLM } from "../../index.js";
5
6function rewritesLineAbove(completion: string, prefix: string): boolean {
7 const lineAbove = prefix
8 .split("\n")
9 .filter((line) => line.trim().length > 0)
10 .slice(-1)[0];
11 if (!lineAbove) {
12 return false;
13 }
14
15 const firstLineOfCompletion = completion
16 .split("\n")
17 .find((line) => line.trim().length > 0);
18 if (!firstLineOfCompletion) {
19 return false;
20 }
21 return lineIsRepeated(lineAbove, firstLineOfCompletion);
22}
23
24const MAX_REPETITION_FREQ_TO_CHECK = 3;
25function isExtremeRepetition(completion: string): boolean {
26 const lines = completion.split("\n");
27 if (lines.length < 6) {
28 return false;
29 }
30 for (let freq = 1; freq < MAX_REPETITION_FREQ_TO_CHECK; freq++) {
31 const lcs = longestCommonSubsequence(lines[0], lines[freq]);
32 if (lcs.length > 5 || lcs.length > lines[0].length * 0.5) {
33 let matchCount = 0;
34 for (let i = 0; i < lines.length; i += freq) {
35 if (lines[i].includes(lcs)) {
36 matchCount++;
37 }
38 }
39 if (matchCount * freq > 8 || (matchCount * freq) / lines.length > 0.8) {
40 return true;
41 }
42 }
43 }
44 return false;
45}
46function isOnlyWhitespace(completion: string): boolean {
47 const whitespaceRegex = /^[\s]+$/;
48 return whitespaceRegex.test(completion);
49}
50
51function isBlank(completion: string): boolean {
52 return completion.trim().length === 0;
53}
54
55export function postprocessCompletion({
56 completion,
57 llm,
58 prefix,
59 suffix,
60}: {
61 completion: string;
62 llm: ILLM;
63 prefix: string;
64 suffix: string;
65}): string | undefined {
66 // Don't return empty
67 if (isBlank(completion)) {
68 return undefined;
69 }
70
71 // Don't return whitespace
72 if (isOnlyWhitespace(completion)) {
73 return undefined;
74 }
75
76 // Dont return if it's just a repeat of the line above
77 if (rewritesLineAbove(completion, prefix)) {
78 return undefined;
79 }
80
81 // Filter out repetitions of many lines in a row
82 if (isExtremeRepetition(completion)) {
83 return undefined;
84 }
85
86 if (llm.model.includes("codestral")) {
87 // Codestral sometimes starts with an extra space
88 if (completion[0] === " " && completion[1] !== " ") {
89 if (prefix.endsWith(" ") && suffix.startsWith("\n")) {
90 completion = completion.slice(1);
91 }
92 }
93
94 // When there is no suffix, Codestral tends to begin with a new line
95 // We do this to avoid double new lines
96 if (
97 suffix.length === 0 &&
98 prefix.endsWith("\n\n") &&
99 completion.startsWith("\n")
100 ) {
101 // Remove a single leading \n from the completion
102 completion = completion.slice(1);
103 }
104 }
105
106 if (llm.model.includes("mercury") || llm.model.includes("granite")) {
107 // Granite tends to repeat the start of the line in the completion output
108 let prefixEnd = prefix.split("\n").pop();
109 if (prefixEnd) {
110 if (completion.startsWith(prefixEnd)) {
111 completion = completion.slice(prefixEnd.length);
112 } else {
113 const trimmedPrefix = prefixEnd.trim();
114 const lastWord = trimmedPrefix.split(/\s+/).pop();
115 if (lastWord && completion.startsWith(lastWord)) {
116 completion = completion.slice(lastWord.length);
117 } else if (completion.startsWith(trimmedPrefix)) {
118 completion = completion.slice(trimmedPrefix.length);
119 }
120 }
121 }
122 }
123
124 // // If completion starts with multiple whitespaces, but the cursor is at the end of the line
125 // // then it should probably be on a new line
126 if (
127 llm.model.includes("mercury") &&
128 (completion.startsWith(" ") || completion.startsWith("\t")) &&
129 !prefix.endsWith("\n") &&
130 (suffix.startsWith("\n") || suffix.trim().length === 0)
131 ) {
132 completion = "\n" + completion;
133 }
134
135 if (
136 (llm.model.includes("gemini") || llm.model.includes("gemma")) &&
137 completion.endsWith("<|file_separator|>")
138 ) {
139 // "<|file_separator|>" is 18 characters long
140 completion = completion.slice(0, -18);
141 }
142
143 // If prefix ends with space and so does completion, then remove the space from completion
144
145 if (prefix.endsWith(" ") && completion.startsWith(" ")) {
146 completion = completion.slice(1);
147 }
148
149 return completion;
150}