| 1 | import Handlebars from "handlebars"; |
| 2 | |
| 3 | import { CompletionOptions } from "../.."; |
| 4 | import { AutocompleteLanguageInfo } from "../constants/AutocompleteLanguageInfo"; |
| 5 | import { HelperVars } from "../util/HelperVars"; |
| 6 | |
| 7 | import { getUriPathBasename } from "../../util/uri"; |
| 8 | import { SnippetPayload } from "../snippets"; |
| 9 | import { |
| 10 | AutocompleteTemplate, |
| 11 | getTemplateForModel, |
| 12 | } from "./AutocompleteTemplate"; |
| 13 | import { getSnippets } from "./filtering"; |
| 14 | import { formatSnippets } from "./formatting"; |
| 15 | import { getStopTokens } from "./getStopTokens"; |
| 16 | |
| 17 | function getTemplate(helper: HelperVars): AutocompleteTemplate { |
| 18 | if (helper.options.template) { |
| 19 | return { |
| 20 | template: helper.options.template, |
| 21 | completionOptions: {}, |
| 22 | compilePrefixSuffix: undefined, |
| 23 | }; |
| 24 | } |
| 25 | return getTemplateForModel(helper.modelName); |
| 26 | } |
| 27 | |
| 28 | function renderStringTemplate( |
| 29 | template: string, |
| 30 | prefix: string, |
| 31 | suffix: string, |
| 32 | lang: AutocompleteLanguageInfo, |
| 33 | filepath: string, |
| 34 | reponame: string, |
| 35 | ) { |
| 36 | const filename = getUriPathBasename(filepath); |
| 37 | const compiledTemplate = Handlebars.compile(template); |
| 38 | |
| 39 | return compiledTemplate({ |
| 40 | prefix, |
| 41 | suffix, |
| 42 | filename, |
| 43 | reponame, |
| 44 | language: lang.name, |
| 45 | }); |
| 46 | } |
| 47 | |
| 48 | export function renderPrompt({ |
| 49 | snippetPayload, |
| 50 | workspaceDirs, |
| 51 | helper, |
| 52 | }: { |
| 53 | snippetPayload: SnippetPayload; |
| 54 | workspaceDirs: string[]; |
| 55 | helper: HelperVars; |
| 56 | }): { |
| 57 | prompt: string; |
| 58 | prefix: string; |
| 59 | suffix: string; |
| 60 | completionOptions: Partial<CompletionOptions> | undefined; |
| 61 | } { |
| 62 | // If prefix is manually passed |
| 63 | let prefix = helper.input.manuallyPassPrefix || helper.prunedPrefix; |
| 64 | let suffix = helper.input.manuallyPassPrefix ? "" : helper.prunedSuffix; |
| 65 | if (suffix === "") { |
| 66 | suffix = "\n"; |
| 67 | } |
| 68 | |
| 69 | const reponame = getUriPathBasename(workspaceDirs[0] ?? "myproject"); |
| 70 | |
| 71 | const { template, compilePrefixSuffix, completionOptions } = |
| 72 | getTemplate(helper); |
| 73 | |
| 74 | const snippets = getSnippets(helper, snippetPayload); |
| 75 | |
| 76 | // Some models have prompts that need two passes. This lets us pass the compiled prefix/suffix |
| 77 | // into either the 2nd template to generate a raw string, or to pass prefix, suffix to a FIM endpoint |
| 78 | if (compilePrefixSuffix) { |
| 79 | [prefix, suffix] = compilePrefixSuffix( |
| 80 | prefix, |
| 81 | suffix, |
| 82 | helper.filepath, |
| 83 | reponame, |
| 84 | snippets, |
| 85 | helper.workspaceUris, |
| 86 | ); |
| 87 | } else { |
| 88 | const formattedSnippets = formatSnippets(helper, snippets, workspaceDirs); |
| 89 | prefix = [formattedSnippets, prefix].join("\n"); |
| 90 | } |
| 91 | |
| 92 | const prompt = |
| 93 | // Templates can be passed as a Handlebars template string or a function |
| 94 | typeof template === "string" |
| 95 | ? renderStringTemplate( |
| 96 | template, |
| 97 | prefix, |
| 98 | suffix, |
| 99 | helper.lang, |
| 100 | helper.filepath, |
| 101 | reponame, |
| 102 | ) |
| 103 | : template( |
| 104 | prefix, |
| 105 | suffix, |
| 106 | helper.filepath, |
| 107 | reponame, |
| 108 | helper.lang.name, |
| 109 | snippets, |
| 110 | helper.workspaceUris, |
| 111 | ); |
| 112 | |
| 113 | const stopTokens = getStopTokens( |
| 114 | completionOptions, |
| 115 | helper.lang, |
| 116 | helper.modelName, |
| 117 | ); |
| 118 | |
| 119 | return { |
| 120 | prompt, |
| 121 | prefix, |
| 122 | suffix, |
| 123 | completionOptions: { |
| 124 | ...completionOptions, |
| 125 | stop: stopTokens, |
| 126 | }, |
| 127 | }; |
| 128 | } |