Elffuss BraidHub

kikocis/continue public

main · 7.7 KiBRawPermalink
1import Handlebars from "handlebars";
2
3import { CompletionOptions } from "../..";
4import { AutocompleteLanguageInfo } from "../constants/AutocompleteLanguageInfo";
5import { HelperVars } from "../util/HelperVars";
6
7import { ILLM } from "../../index.js";
8import { DEFAULT_MAX_TOKENS } from "../../llm/constants.js";
9import {
10 countTokens,
11 getTokenCountingBufferSafety,
12 pruneLinesFromBottom,
13 pruneLinesFromTop,
14} from "../../llm/countTokens";
15import { getUriPathBasename } from "../../util/uri";
16import { SnippetPayload } from "../snippets";
17import { AutocompleteSnippet } from "../snippets/types";
18import {
19 AutocompleteTemplate,
20 getTemplateForModel,
21} from "./AutocompleteTemplate";
22import { getSnippets } from "./filtering";
23import { formatSnippets } from "./formatting";
24import { getStopTokens } from "./getStopTokens";
25
26function getTemplate(helper: HelperVars): AutocompleteTemplate {
27 if (helper.options.template) {
28 return {
29 template: helper.options.template,
30 completionOptions: {},
31 compilePrefixSuffix: undefined,
32 };
33 }
34 return getTemplateForModel(helper.modelName);
35}
36
37function renderStringTemplate(
38 template: string,
39 prefix: string,
40 suffix: string,
41 lang: AutocompleteLanguageInfo,
42 filepath: string,
43 reponame: string,
44) {
45 const filename = getUriPathBasename(filepath);
46 const compiledTemplate = Handlebars.compile(template);
47
48 return compiledTemplate({
49 prefix,
50 suffix,
51 filename,
52 reponame,
53 language: lang.name,
54 });
55}
56
57/** Consolidates shared setup between renderPrompt and renderPromptWithTokenLimit. */
58function preparePromptContext({
59 snippetPayload,
60 workspaceDirs,
61 helper,
62}: {
63 snippetPayload: SnippetPayload;
64 workspaceDirs: string[];
65 helper: HelperVars;
66}): {
67 prefix: string;
68 suffix: string;
69 reponame: string;
70 template: AutocompleteTemplate["template"];
71 compilePrefixSuffix: AutocompleteTemplate["compilePrefixSuffix"] | undefined;
72 completionOptions: Partial<CompletionOptions> | undefined;
73 snippets: AutocompleteSnippet[];
74} {
75 // Determine base prefix/suffix, accounting for any manually supplied prefix.
76 let prefix = helper.input.manuallyPassPrefix || helper.prunedPrefix;
77 let suffix = helper.input.manuallyPassPrefix ? "" : helper.prunedSuffix;
78 if (suffix === "") {
79 suffix = "\n";
80 }
81
82 const reponame = getUriPathBasename(workspaceDirs[0] ?? "myproject");
83
84 const { template, compilePrefixSuffix, completionOptions } =
85 getTemplate(helper);
86
87 const snippets = getSnippets(helper, snippetPayload);
88
89 return {
90 prefix,
91 suffix,
92 reponame,
93 template,
94 compilePrefixSuffix,
95 completionOptions,
96 snippets,
97 };
98}
99
100export function renderPrompt({
101 snippetPayload,
102 workspaceDirs,
103 helper,
104}: {
105 snippetPayload: SnippetPayload;
106 workspaceDirs: string[];
107 helper: HelperVars;
108}): {
109 prompt: string;
110 prefix: string;
111 suffix: string;
112 completionOptions: Partial<CompletionOptions> | undefined;
113} {
114 const {
115 prefix,
116 suffix,
117 reponame,
118 template,
119 compilePrefixSuffix,
120 completionOptions,
121 snippets,
122 } = preparePromptContext({ snippetPayload, workspaceDirs, helper });
123
124 // Delegate prompt construction to buildPrompt to avoid duplication.
125 const {
126 prompt,
127 prefix: compiledPrefix,
128 suffix: compiledSuffix,
129 } = buildPrompt(
130 template,
131 compilePrefixSuffix,
132 prefix,
133 suffix,
134 helper,
135 snippets,
136 workspaceDirs,
137 reponame,
138 );
139
140 const stopTokens = getStopTokens(
141 completionOptions,
142 helper.lang,
143 helper.modelName,
144 );
145
146 return {
147 prompt,
148 prefix: compiledPrefix,
149 suffix: compiledSuffix,
150 completionOptions: {
151 ...completionOptions,
152 stop: stopTokens,
153 },
154 };
155}
156
157/** Builds the final prompt by applying prefix/suffix compilation or snippet formatting, then rendering the template. */
158function buildPrompt(
159 template: AutocompleteTemplate["template"],
160 compilePrefixSuffix: AutocompleteTemplate["compilePrefixSuffix"] | undefined,
161 prefix: string,
162 suffix: string,
163 helper: HelperVars,
164 snippets: AutocompleteSnippet[],
165 workspaceDirs: string[],
166 reponame: string,
167): { prompt: string; prefix: string; suffix: string } {
168 if (compilePrefixSuffix) {
169 [prefix, suffix] = compilePrefixSuffix(
170 prefix,
171 suffix,
172 helper.filepath,
173 reponame,
174 snippets,
175 helper.workspaceUris,
176 );
177 } else {
178 const formatted = formatSnippets(helper, snippets, workspaceDirs);
179 prefix = [formatted, prefix].join("\n");
180 }
181 const prompt =
182 typeof template === "string"
183 ? renderStringTemplate(
184 template,
185 prefix,
186 suffix,
187 helper.lang,
188 helper.filepath,
189 reponame,
190 )
191 : template(
192 prefix,
193 suffix,
194 helper.filepath,
195 reponame,
196 helper.lang.name,
197 snippets,
198 helper.workspaceUris,
199 );
200 return { prompt, prefix, suffix };
201}
202
203function pruneLength(llm: ILLM, prompt: string): number {
204 const contextLength = llm.contextLength;
205 const reservedTokens = llm.completionOptions.maxTokens ?? DEFAULT_MAX_TOKENS;
206 const safetyBuffer = getTokenCountingBufferSafety(contextLength);
207 const maxAllowedPromptTokens = contextLength - reservedTokens - safetyBuffer;
208 const promptTokenCount = countTokens(prompt, llm.model);
209 return promptTokenCount - maxAllowedPromptTokens;
210}
211
212export function renderPromptWithTokenLimit({
213 snippetPayload,
214 workspaceDirs,
215 helper,
216 llm,
217}: {
218 snippetPayload: SnippetPayload;
219 workspaceDirs: string[];
220 helper: HelperVars;
221 llm: ILLM | undefined;
222}): {
223 prompt: string;
224 prefix: string;
225 suffix: string;
226 completionOptions: Partial<CompletionOptions> | undefined;
227} {
228 const {
229 prefix: initialPrefix,
230 suffix: initialSuffix,
231 reponame,
232 template,
233 compilePrefixSuffix,
234 completionOptions,
235 snippets,
236 } = preparePromptContext({ snippetPayload, workspaceDirs, helper });
237
238 // We'll mutate prefix/suffix during pruning, so copy them.
239 let prefix = initialPrefix;
240 let suffix = initialSuffix;
241
242 let {
243 prompt,
244 prefix: compiledPrefix,
245 suffix: compiledSuffix,
246 } = buildPrompt(
247 template,
248 compilePrefixSuffix,
249 prefix,
250 suffix,
251 helper,
252 snippets,
253 workspaceDirs,
254 reponame,
255 );
256
257 // Truncate prefix and suffix if prompt tokens exceed maxAllowedPromptTokens
258 if (llm) {
259 const prune = pruneLength(llm, prompt);
260 if (prune > 0) {
261 const tokensToDrop = prune;
262 const prefixTokenCount = countTokens(prefix, helper.modelName);
263 const suffixTokenCount = countTokens(suffix, helper.modelName);
264 const totalContextTokens = prefixTokenCount + suffixTokenCount;
265 if (totalContextTokens > 0) {
266 const dropPrefix = Math.ceil(
267 tokensToDrop * (prefixTokenCount / totalContextTokens),
268 );
269 const dropSuffix = Math.ceil(tokensToDrop - dropPrefix);
270 const allowedPrefixTokens = Math.max(0, prefixTokenCount - dropPrefix);
271 const allowedSuffixTokens = Math.max(0, suffixTokenCount - dropSuffix);
272 prefix = pruneLinesFromTop(
273 prefix,
274 allowedPrefixTokens,
275 helper.modelName,
276 );
277 suffix = pruneLinesFromBottom(
278 suffix,
279 allowedSuffixTokens,
280 helper.modelName,
281 );
282 }
283 ({
284 prompt,
285 prefix: compiledPrefix,
286 suffix: compiledSuffix,
287 } = buildPrompt(
288 template,
289 compilePrefixSuffix,
290 prefix,
291 suffix,
292 helper,
293 snippets,
294 workspaceDirs,
295 reponame,
296 ));
297 }
298 }
299
300 const stopTokens = getStopTokens(
301 completionOptions,
302 helper.lang,
303 helper.modelName,
304 );
305
306 return {
307 prompt,
308 prefix: compiledPrefix,
309 suffix: compiledSuffix,
310 completionOptions: {
311 ...completionOptions,
312 stop: stopTokens,
313 },
314 };
315}