| 1 | import { longestCommonSubsequence } from "../../util/lcs.js"; |
| 2 | import { lineIsRepeated } from "../filtering/streamTransforms/lineStream.js"; |
| 3 | |
| 4 | import type { ILLM } from "../../index.js"; |
| 5 | |
| 6 | function 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 | |
| 24 | const MAX_REPETITION_FREQ_TO_CHECK = 3; |
| 25 | function 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 | } |
| 46 | function isOnlyWhitespace(completion: string): boolean { |
| 47 | const whitespaceRegex = /^[\s]+$/; |
| 48 | return whitespaceRegex.test(completion); |
| 49 | } |
| 50 | |
| 51 | function isBlank(completion: string): boolean { |
| 52 | return completion.trim().length === 0; |
| 53 | } |
| 54 | |
| 55 | export 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("qwen3")) { |
| 107 | // Qwen3 always starts from special thinking markers, and we don't want them to output these contents |
| 108 | // Remove all content from " |
| 109 | completion = completion.replace(/<think>.*?<\/think>/s, ""); |
| 110 | completion = completion.replace(/<\/think>/, ""); |
| 111 | |
| 112 | // Remove any number of newline characters at the beginning and end |
| 113 | completion = completion.replace(/^\n+|\n+$/g, ""); |
| 114 | } |
| 115 | |
| 116 | if (llm.model.includes("mercury") || llm.model.includes("granite")) { |
| 117 | // Granite tends to repeat the start of the line in the completion output |
| 118 | let prefixEnd = prefix.split("\n").pop(); |
| 119 | if (prefixEnd) { |
| 120 | if (completion.startsWith(prefixEnd)) { |
| 121 | completion = completion.slice(prefixEnd.length); |
| 122 | } else { |
| 123 | const trimmedPrefix = prefixEnd.trim(); |
| 124 | const lastWord = trimmedPrefix.split(/\s+/).pop(); |
| 125 | if (lastWord && completion.startsWith(lastWord)) { |
| 126 | completion = completion.slice(lastWord.length); |
| 127 | } else if (completion.startsWith(trimmedPrefix)) { |
| 128 | completion = completion.slice(trimmedPrefix.length); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // // If completion starts with multiple whitespaces, but the cursor is at the end of the line |
| 135 | // // then it should probably be on a new line |
| 136 | if ( |
| 137 | llm.model.includes("mercury") && |
| 138 | (completion.startsWith(" ") || completion.startsWith("\t")) && |
| 139 | !prefix.endsWith("\n") && |
| 140 | (suffix.startsWith("\n") || suffix.trim().length === 0) |
| 141 | ) { |
| 142 | completion = "\n" + completion; |
| 143 | } |
| 144 | |
| 145 | if ( |
| 146 | (llm.model.includes("gemini") || llm.model.includes("gemma")) && |
| 147 | completion.endsWith("<|file_separator|>") |
| 148 | ) { |
| 149 | // "<|file_separator|>" is 18 characters long |
| 150 | completion = completion.slice(0, -18); |
| 151 | } |
| 152 | |
| 153 | // If prefix ends with space and so does completion, then remove the space from completion |
| 154 | |
| 155 | if (prefix.endsWith(" ") && completion.startsWith(" ")) { |
| 156 | completion = completion.slice(1); |
| 157 | } |
| 158 | |
| 159 | return completion; |
| 160 | } |