| 1 | import { ContinueSDK, SlashCommand } from "../.."; |
| 2 | import { renderChatMessage } from "../../util/messageContent"; |
| 3 | import { getLastNPathParts } from "../../util/uri"; |
| 4 | import { parsePromptFileV1V2 } from "../v2/parsePromptFileV1V2"; |
| 5 | import { renderPromptFileV2 } from "../v2/renderPromptFile"; |
| 6 | |
| 7 | import { getContextProviderHelpers } from "./getContextProviderHelpers"; |
| 8 | import { renderTemplatedString } from "./renderTemplatedString"; |
| 9 | import { replaceSlashCommandWithPromptInChatHistory } from "./updateChatHistory"; |
| 10 | |
| 11 | export function extractName(preamble: { name?: string }, path: string): string { |
| 12 | return preamble.name ?? getLastNPathParts(path, 1).split(".prompt")[0]; |
| 13 | } |
| 14 | |
| 15 | export function extractUserInput(input: string, commandName: string): string { |
| 16 | if (input.startsWith(`/${commandName}`)) { |
| 17 | return input.slice(commandName.length + 1).trimStart(); |
| 18 | } |
| 19 | return input; |
| 20 | } |
| 21 | |
| 22 | async function renderPromptV1( |
| 23 | prompt: string, |
| 24 | context: ContinueSDK, |
| 25 | userInput: string, |
| 26 | ) { |
| 27 | const helpers = getContextProviderHelpers(context); |
| 28 | |
| 29 | const inputData: Record<string, string> = { |
| 30 | input: userInput, |
| 31 | }; |
| 32 | |
| 33 | // A few context providers that don't need to be in config.json to work in .prompt files |
| 34 | if (helpers?.find((helper) => helper[0] === "diff")) { |
| 35 | const diff = await context.ide.getDiff(true); |
| 36 | inputData.diff = diff.join("\n"); |
| 37 | } |
| 38 | if (helpers?.find((helper) => helper[0] === "currentFile")) { |
| 39 | const currentFile = await context.ide.getCurrentFile(); |
| 40 | if (currentFile) { |
| 41 | inputData.currentFile = currentFile.path; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | return renderTemplatedString( |
| 46 | prompt, |
| 47 | context.ide.readFile.bind(context.ide), |
| 48 | inputData, |
| 49 | helpers, |
| 50 | ); |
| 51 | } |
| 52 | |
| 53 | export function slashCommandFromPromptFileV1( |
| 54 | path: string, |
| 55 | content: string, |
| 56 | ): SlashCommand | null { |
| 57 | const { name, description, systemMessage, prompt } = parsePromptFileV1V2( |
| 58 | path, |
| 59 | content, |
| 60 | ); |
| 61 | |
| 62 | return { |
| 63 | name, |
| 64 | description, |
| 65 | prompt, |
| 66 | promptFile: path, |
| 67 | run: async function* (context) { |
| 68 | const userInput = extractUserInput(context.input, name); |
| 69 | const [_, renderedPrompt] = await renderPromptFileV2(prompt, { |
| 70 | config: context.config, |
| 71 | fullInput: userInput, |
| 72 | embeddingsProvider: context.config.modelsByRole.embed[0], |
| 73 | reranker: context.config.modelsByRole.rerank[0], |
| 74 | llm: context.llm, |
| 75 | ide: context.ide, |
| 76 | selectedCode: context.selectedCode, |
| 77 | fetch: context.fetch, |
| 78 | }); |
| 79 | |
| 80 | const messages = replaceSlashCommandWithPromptInChatHistory( |
| 81 | context.history, |
| 82 | name, |
| 83 | renderedPrompt, |
| 84 | systemMessage, |
| 85 | ); |
| 86 | |
| 87 | if (systemMessage) { |
| 88 | const currentSystemMsg = messages.find((msg) => msg.role === "system"); |
| 89 | if (currentSystemMsg) { |
| 90 | currentSystemMsg.content = systemMessage; |
| 91 | } else { |
| 92 | messages.unshift({ |
| 93 | role: "system", |
| 94 | content: systemMessage, |
| 95 | }); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | for await (const chunk of context.llm.streamChat( |
| 100 | messages, |
| 101 | new AbortController().signal, |
| 102 | context.completionOptions, |
| 103 | )) { |
| 104 | yield renderChatMessage(chunk); |
| 105 | } |
| 106 | }, |
| 107 | }; |
| 108 | } |