Elffuss BraidHub

kikocis/continue public

6e7dba04c3ba · 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 !(
59 model.includes("gpt") ||
60 model.includes("claude") ||
61 model.includes("nova")
62 );
63}
64
65export async function* streamDiffLines({
66 prefix,
67 highlighted,
68 suffix,
69 llm,
70 abortController,
71 input,
72 language,
73 overridePrompt,
74 rulesToInclude,
75}: {
76 prefix: string;
77 highlighted: string;
78 suffix: string;
79 llm: ILLM;
80 abortController: AbortController;
81 input: string;
82 language: string | undefined;
83 overridePrompt: ChatMessage[] | undefined;
84 rulesToInclude: RuleWithSource[] | undefined;
85}): AsyncGenerator<DiffLine> {
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 constructEditPrompt(prefix, highlighted, suffix, llm, input, language);
112
113 // Rules can be included with edit prompt
114 // If any rules are present this will result in using chat instead of legacy completion
115 const systemMessage = rulesToInclude
116 ? getSystemMessageWithRules({
117 availableRules: rulesToInclude,
118 userMessage:
119 typeof prompt === "string"
120 ? ({
121 role: "user",
122 content: prompt,
123 } as UserChatMessage)
124 : (findLast(
125 prompt,
126 (msg) => msg.role === "user" || msg.role === "tool",
127 ) as UserChatMessage | ToolResultChatMessage | undefined),
128 baseSystemMessage: undefined,
129 contextItems: [],
130 }).systemMessage
131 : undefined;
132
133 if (systemMessage) {
134 if (typeof prompt === "string") {
135 prompt = [
136 {
137 role: "system",
138 content: systemMessage,
139 },
140 {
141 role: "user",
142 content: prompt,
143 },
144 ];
145 } else {
146 const curSysMsg = prompt.find((msg) => msg.role === "system");
147 if (curSysMsg) {
148 curSysMsg.content = systemMessage + "\n\n" + curSysMsg.content;
149 } else {
150 prompt.unshift({
151 role: "system",
152 content: systemMessage,
153 });
154 }
155 }
156 }
157
158 const inept = modelIsInept(llm.model);
159
160 const prediction: Prediction = {
161 type: "content",
162 content: highlighted,
163 };
164
165 const completion = recursiveStream(llm, abortController, prompt, prediction);
166
167 let lines = streamLines(completion);
168
169 lines = filterEnglishLinesAtStart(lines);
170 lines = filterCodeBlockLines(lines);
171 lines = stopAtLines(lines, () => {});
172 lines = skipLines(lines);
173 lines = removeTrailingWhitespace(lines);
174 if (inept) {
175 // lines = fixCodeLlamaFirstLineIndentation(lines);
176 lines = filterEnglishLinesAtEnd(lines);
177 }
178
179 let diffLines = streamDiff(oldLines, lines);
180 diffLines = filterLeadingAndTrailingNewLineInsertion(diffLines);
181 if (highlighted.length === 0) {
182 const line = prefix.split("\n").slice(-1)[0];
183 const indentation = line.slice(0, line.length - line.trimStart().length);
184 diffLines = addIndentation(diffLines, indentation);
185 }
186
187 for await (const diffLine of diffLines) {
188 yield diffLine;
189 }
190}