Elffuss BraidHub

kikocis/continue public

a6ac770a1291 · 5.0 KiBRawPermalink
1import {
2 ChatMessage,
3 DiffLine,
4 ILLM,
5 Prediction,
6 RuleWithSource,
7 ToolResultChatMessage,
8 UserChatMessage,
9} from "../";
10import {
11 filterCodeBlockLines,
12 filterEnglishLinesAtEnd,
13 filterEnglishLinesAtStart,
14 filterLeadingAndTrailingNewLineInsertion,
15 removeTrailingWhitespace,
16 skipLines,
17 stopAtLines,
18} from "../autocomplete/filtering/streamTransforms/lineStream";
19import { streamDiff } from "../diff/streamDiff";
20import { streamLines } from "../diff/util";
21import { getSystemMessageWithRules } from "../llm/rules/getSystemMessageWithRules";
22import { gptEditPrompt } from "../llm/templates/edit";
23import { findLast } from "../util/findLast";
24import { Telemetry } from "../util/posthog";
25import { recursiveStream } from "./recursiveStream";
26
27function 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
45export 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
57function modelIsInept(model: string): boolean {
58 return !(model.includes("gpt") || model.includes("claude"));
59}
60
61export 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 = rulesToInclude
112 ? getSystemMessageWithRules({
113 availableRules: rulesToInclude,
114 userMessage:
115 typeof prompt === "string"
116 ? ({
117 role: "user",
118 content: prompt,
119 } as UserChatMessage)
120 : (findLast(
121 prompt,
122 (msg) => msg.role === "user" || msg.role === "tool",
123 ) as UserChatMessage | ToolResultChatMessage | undefined),
124 baseSystemMessage: undefined,
125 contextItems: [],
126 }).systemMessage
127 : undefined;
128
129 if (systemMessage) {
130 if (typeof prompt === "string") {
131 prompt = [
132 {
133 role: "system",
134 content: systemMessage,
135 },
136 {
137 role: "user",
138 content: prompt,
139 },
140 ];
141 } else {
142 const curSysMsg = prompt.find((msg) => msg.role === "system");
143 if (curSysMsg) {
144 curSysMsg.content = systemMessage + "\n\n" + curSysMsg.content;
145 } else {
146 prompt.unshift({
147 role: "system",
148 content: systemMessage,
149 });
150 }
151 }
152 }
153
154 const inept = modelIsInept(llm.model);
155
156 const prediction: Prediction = {
157 type: "content",
158 content: highlighted,
159 };
160
161 const completion = recursiveStream(llm, abortController, prompt, prediction);
162
163 let lines = streamLines(completion);
164
165 lines = filterEnglishLinesAtStart(lines);
166 lines = filterCodeBlockLines(lines);
167 lines = stopAtLines(lines, () => {});
168 lines = skipLines(lines);
169 lines = removeTrailingWhitespace(lines);
170 if (inept) {
171 // lines = fixCodeLlamaFirstLineIndentation(lines);
172 lines = filterEnglishLinesAtEnd(lines);
173 }
174
175 let diffLines = streamDiff(oldLines, lines);
176 diffLines = filterLeadingAndTrailingNewLineInsertion(diffLines);
177 if (highlighted.length === 0) {
178 const line = prefix.split("\n").slice(-1)[0];
179 const indentation = line.slice(0, line.length - line.trimStart().length);
180 diffLines = addIndentation(diffLines, indentation);
181 }
182
183 for await (const diffLine of diffLines) {
184 yield diffLine;
185 }
186}