| 1 | import { |
| 2 | ChatMessage, |
| 3 | DiffLine, |
| 4 | ILLM, |
| 5 | Prediction, |
| 6 | RuleWithSource, |
| 7 | ToolResultChatMessage, |
| 8 | UserChatMessage, |
| 9 | } from "../"; |
| 10 | import { |
| 11 | filterCodeBlockLines, |
| 12 | filterEnglishLinesAtEnd, |
| 13 | filterEnglishLinesAtStart, |
| 14 | filterLeadingAndTrailingNewLineInsertion, |
| 15 | removeTrailingWhitespace, |
| 16 | skipLines, |
| 17 | stopAtLines, |
| 18 | } from "../autocomplete/filtering/streamTransforms/lineStream"; |
| 19 | import { streamDiff } from "../diff/streamDiff"; |
| 20 | import { streamLines } from "../diff/util"; |
| 21 | import { getSystemMessageWithRules } from "../llm/rules/getSystemMessageWithRules"; |
| 22 | import { gptEditPrompt } from "../llm/templates/edit"; |
| 23 | import { findLast } from "../util/findLast"; |
| 24 | import { Telemetry } from "../util/posthog"; |
| 25 | import { recursiveStream } from "./recursiveStream"; |
| 26 | |
| 27 | function constructEditPrompt( |
| 28 | prefix: string, |
| 29 | highlighted: string, |
| 30 | suffix: string, |
| 31 | llm: ILLM, |
| 32 | userInput: string, |
| 33 | language: string | undefined, |
| 34 | ): string | ChatMessage[] { |
| 35 | const template = llm.promptTemplates?.edit ?? gptEditPrompt; |
| 36 | return llm.renderPromptTemplate(template, [], { |
| 37 | userInput, |
| 38 | prefix, |
| 39 | codeToEdit: highlighted, |
| 40 | suffix, |
| 41 | language: language ?? "", |
| 42 | }); |
| 43 | } |
| 44 | |
| 45 | export async function* addIndentation( |
| 46 | diffLineGenerator: AsyncGenerator<DiffLine>, |
| 47 | indentation: string, |
| 48 | ): AsyncGenerator<DiffLine> { |
| 49 | for await (const diffLine of diffLineGenerator) { |
| 50 | yield { |
| 51 | ...diffLine, |
| 52 | line: indentation + diffLine.line, |
| 53 | }; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | function modelIsInept(model: string): boolean { |
| 58 | return !(model.includes("gpt") || model.includes("claude")); |
| 59 | } |
| 60 | |
| 61 | export async function* streamDiffLines({ |
| 62 | prefix, |
| 63 | highlighted, |
| 64 | suffix, |
| 65 | llm, |
| 66 | abortController, |
| 67 | input, |
| 68 | language, |
| 69 | overridePrompt, |
| 70 | rulesToInclude, |
| 71 | }: { |
| 72 | prefix: string; |
| 73 | highlighted: string; |
| 74 | suffix: string; |
| 75 | llm: ILLM; |
| 76 | abortController: AbortController; |
| 77 | input: string; |
| 78 | language: string | undefined; |
| 79 | overridePrompt: ChatMessage[] | undefined; |
| 80 | rulesToInclude: RuleWithSource[] | undefined; |
| 81 | }): AsyncGenerator<DiffLine> { |
| 82 | void Telemetry.capture( |
| 83 | "inlineEdit", |
| 84 | { |
| 85 | model: llm.model, |
| 86 | provider: llm.providerName, |
| 87 | }, |
| 88 | true, |
| 89 | ); |
| 90 | |
| 91 | // Strip common indentation for the LLM, then add back after generation |
| 92 | let oldLines = |
| 93 | highlighted.length > 0 |
| 94 | ? highlighted.split("\n") |
| 95 | : // When highlighted is empty, we need to combine last line of prefix and first line of suffix to determine the line being edited |
| 96 | [(prefix + suffix).split("\n")[prefix.split("\n").length - 1]]; |
| 97 | |
| 98 | // But if that line is empty, we can assume we are insertion-only |
| 99 | if (oldLines.length === 1 && oldLines[0].trim() === "") { |
| 100 | oldLines = []; |
| 101 | } |
| 102 | |
| 103 | // Defaults to creating an edit prompt |
| 104 | // For apply can be overridden with simply apply prompt |
| 105 | let prompt = |
| 106 | overridePrompt ?? |
| 107 | constructEditPrompt(prefix, highlighted, suffix, llm, input, language); |
| 108 | |
| 109 | // Rules can be included with edit prompt |
| 110 | // If any rules are present this will result in using chat instead of legacy completion |
| 111 | const systemMessage = |
| 112 | rulesToInclude || llm.baseChatSystemMessage |
| 113 | ? getSystemMessageWithRules({ |
| 114 | availableRules: rulesToInclude ?? [], |
| 115 | userMessage: |
| 116 | typeof prompt === "string" |
| 117 | ? ({ |
| 118 | role: "user", |
| 119 | content: prompt, |
| 120 | } as UserChatMessage) |
| 121 | : (findLast( |
| 122 | prompt, |
| 123 | (msg) => msg.role === "user" || msg.role === "tool", |
| 124 | ) as UserChatMessage | ToolResultChatMessage | undefined), |
| 125 | baseSystemMessage: llm.baseChatSystemMessage, |
| 126 | contextItems: [], |
| 127 | }).systemMessage |
| 128 | : undefined; |
| 129 | |
| 130 | if (systemMessage) { |
| 131 | if (typeof prompt === "string") { |
| 132 | prompt = [ |
| 133 | { |
| 134 | role: "system", |
| 135 | content: systemMessage, |
| 136 | }, |
| 137 | { |
| 138 | role: "user", |
| 139 | content: prompt, |
| 140 | }, |
| 141 | ]; |
| 142 | } else { |
| 143 | const curSysMsg = prompt.find((msg) => msg.role === "system"); |
| 144 | if (curSysMsg) { |
| 145 | curSysMsg.content = systemMessage + "\n\n" + curSysMsg.content; |
| 146 | } else { |
| 147 | prompt.unshift({ |
| 148 | role: "system", |
| 149 | content: systemMessage, |
| 150 | }); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | const inept = modelIsInept(llm.model); |
| 156 | |
| 157 | const prediction: Prediction = { |
| 158 | type: "content", |
| 159 | content: highlighted, |
| 160 | }; |
| 161 | |
| 162 | const completion = recursiveStream(llm, abortController, prompt, prediction); |
| 163 | |
| 164 | let lines = streamLines(completion); |
| 165 | |
| 166 | lines = filterEnglishLinesAtStart(lines); |
| 167 | lines = filterCodeBlockLines(lines); |
| 168 | lines = stopAtLines(lines, () => {}); |
| 169 | lines = skipLines(lines); |
| 170 | lines = removeTrailingWhitespace(lines); |
| 171 | if (inept) { |
| 172 | // lines = fixCodeLlamaFirstLineIndentation(lines); |
| 173 | lines = filterEnglishLinesAtEnd(lines); |
| 174 | } |
| 175 | |
| 176 | let diffLines = streamDiff(oldLines, lines); |
| 177 | diffLines = filterLeadingAndTrailingNewLineInsertion(diffLines); |
| 178 | if (highlighted.length === 0) { |
| 179 | const line = prefix.split("\n").slice(-1)[0]; |
| 180 | const indentation = line.slice(0, line.length - line.trimStart().length); |
| 181 | diffLines = addIndentation(diffLines, indentation); |
| 182 | } |
| 183 | |
| 184 | for await (const diffLine of diffLines) { |
| 185 | yield diffLine; |
| 186 | } |
| 187 | } |