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