Elffuss BraidHub

kikocis/continue public

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