Elffuss BraidHub

kikocis/continue public

a36aa2a5e5e6 · 2.4 KiBRawPermalink
1import { CustomCommand, SlashCommand, SlashCommandDescription } from "../";
2import { renderTemplatedString } from "../promptFiles/v1/renderTemplatedString";
3import { replaceSlashCommandWithPromptInChatHistory } from "../promptFiles/v1/updateChatHistory";
4import { renderPromptFileV2 } from "../promptFiles/v2/renderPromptFile";
5import { renderChatMessage } from "../util/messageContent";
6
7import SlashCommands from "./slash";
8
9export function slashFromCustomCommand(
10 customCommand: CustomCommand,
11): SlashCommand {
12 const commandName = customCommand.name.startsWith("/")
13 ? customCommand.name.substring(1)
14 : customCommand.name;
15 return {
16 name: commandName,
17 description: customCommand.description ?? "",
18 prompt: customCommand.prompt,
19 run: async function* ({
20 input,
21 llm,
22 history,
23 ide,
24 completionOptions,
25 config,
26 selectedCode,
27 fetch,
28 abortController,
29 }) {
30 // Render prompt template
31 let renderedPrompt: string;
32 if (customCommand.prompt.includes("{{{ input }}}")) {
33 renderedPrompt = await renderTemplatedString(
34 customCommand.prompt,
35 ide.readFile.bind(ide),
36 { input },
37 );
38 } else {
39 const renderedPromptFile = await renderPromptFileV2(
40 customCommand.prompt,
41 {
42 config,
43 llm,
44 ide,
45 selectedCode,
46 fetch,
47 fullInput: input,
48 embeddingsProvider: config.modelsByRole.embed[0],
49 reranker: config.modelsByRole.rerank[0],
50 },
51 );
52
53 renderedPrompt = renderedPromptFile[1];
54 }
55
56 // Replaces slash command messages with the rendered prompt
57 // which INCLUDES the input
58 const messages = replaceSlashCommandWithPromptInChatHistory(
59 history,
60 commandName,
61 renderedPrompt,
62 undefined,
63 );
64
65 for await (const chunk of llm.streamChat(
66 messages,
67 abortController.signal,
68 completionOptions,
69 )) {
70 yield renderChatMessage(chunk);
71 }
72 },
73 };
74}
75
76export function slashCommandFromDescription(
77 desc: SlashCommandDescription,
78): SlashCommand | undefined {
79 const cmd = SlashCommands.find((cmd) => cmd.name === desc.name);
80 if (!cmd) {
81 return undefined;
82 }
83 return {
84 ...cmd,
85 params: desc.params,
86 description: desc.description ?? cmd.description,
87 };
88}