| 1 | import { MessagePart, RangeInFile, SlashCommandDescWithSource } from "core"; |
| 2 | import { stripImages } from "core/util/messageContent"; |
| 3 | import { IIdeMessenger } from "../../../../context/IdeMessenger"; |
| 4 | import { renderMcpPrompt } from "./renderMcpPrompt"; |
| 5 | import { getRenderedV1Prompt } from "./renderPromptv1"; |
| 6 | import { getPromptV2ContextRequests } from "./renderPromptv2"; |
| 7 | import { GetContextRequest } from "./types"; |
| 8 | |
| 9 | /* |
| 10 | Slash commands can come from many sources. Render based on the source. |
| 11 | */ |
| 12 | export async function renderSlashCommandPrompt( |
| 13 | ideMessenger: IIdeMessenger, |
| 14 | commandName: string | undefined, |
| 15 | parts: MessagePart[], |
| 16 | availableSlashCommands: SlashCommandDescWithSource[], |
| 17 | selectedCode: RangeInFile[], |
| 18 | ): Promise<{ |
| 19 | slashedParts: MessagePart[]; |
| 20 | legacyCommandWithInput?: { |
| 21 | command: SlashCommandDescWithSource; |
| 22 | input: string; |
| 23 | }; |
| 24 | contextRequests: GetContextRequest[]; |
| 25 | }> { |
| 26 | const NO_COMMAND = { |
| 27 | slashedParts: parts, |
| 28 | legacyCommandWithInput: undefined, |
| 29 | contextRequests: [], |
| 30 | }; |
| 31 | if (!commandName) { |
| 32 | return NO_COMMAND; |
| 33 | } |
| 34 | const command = availableSlashCommands.find((c) => c.name === commandName); |
| 35 | if (!command) { |
| 36 | return NO_COMMAND; |
| 37 | } |
| 38 | |
| 39 | const nonTextParts = parts.filter((part) => part.type !== "text"); |
| 40 | const textParts = parts.filter((part) => part.type === "text"); |
| 41 | const slashedParts: MessagePart[] = [...nonTextParts]; |
| 42 | |
| 43 | const userInput = stripImages(textParts).trimStart(); |
| 44 | |
| 45 | const legacyCommandWithInput = command.isLegacy |
| 46 | ? { |
| 47 | command, |
| 48 | input: userInput, |
| 49 | } |
| 50 | : undefined; |
| 51 | |
| 52 | const contextRequests: GetContextRequest[] = []; |
| 53 | |
| 54 | switch (command.source) { |
| 55 | case "built-in-legacy": |
| 56 | case "config-ts-slash-command": |
| 57 | /** |
| 58 | * For legacy slash commands, we simply insert the text "/{name}" in front of the message |
| 59 | * And then parsing for this is done in core |
| 60 | */ |
| 61 | slashedParts.push({ |
| 62 | type: "text", |
| 63 | text: `/${command.name}${command.prompt ? " " + command.prompt : ""}${userInput ? " " + userInput : ""}`, |
| 64 | }); |
| 65 | break; |
| 66 | case "mcp-prompt": |
| 67 | const renderedMcpPrompt = await renderMcpPrompt( |
| 68 | command, |
| 69 | ideMessenger, |
| 70 | userInput, |
| 71 | ); |
| 72 | slashedParts.push({ |
| 73 | type: "text", |
| 74 | text: renderedMcpPrompt, |
| 75 | }); |
| 76 | break; |
| 77 | case "prompt-file-v1": |
| 78 | case "prompt-file-v2": |
| 79 | case "yaml-prompt-block": |
| 80 | case "invokable-rule": |
| 81 | if (!command.prompt) { |
| 82 | console.warn(`Invalid/empty prompt from slash command ${command.name}`); |
| 83 | break; |
| 84 | } |
| 85 | let renderedPrompt: string; |
| 86 | if ( |
| 87 | command.source === "prompt-file-v1" || |
| 88 | command.prompt.includes("{{{ input }}}") |
| 89 | ) { |
| 90 | renderedPrompt = await getRenderedV1Prompt( |
| 91 | ideMessenger, |
| 92 | command, |
| 93 | userInput, |
| 94 | selectedCode, |
| 95 | ); |
| 96 | } else { |
| 97 | const promptFileCtxRequests = await getPromptV2ContextRequests( |
| 98 | ideMessenger, |
| 99 | command, |
| 100 | ); |
| 101 | contextRequests.push(...promptFileCtxRequests); |
| 102 | renderedPrompt = [command.prompt, userInput].join("\n\n").trim(); |
| 103 | } |
| 104 | |
| 105 | if (renderedPrompt) { |
| 106 | slashedParts.push({ |
| 107 | type: "text", |
| 108 | text: renderedPrompt, // Includes user input |
| 109 | }); |
| 110 | } else { |
| 111 | console.warn( |
| 112 | `Invalid/empty prompt + input from slash command ${command.name}`, |
| 113 | ); |
| 114 | } |
| 115 | |
| 116 | break; |
| 117 | case "built-in": |
| 118 | case "json-custom-command": |
| 119 | if (!command.prompt) { |
| 120 | console.warn(`Slash command ${command.name} is missing prompt`); |
| 121 | break; |
| 122 | } |
| 123 | let rendered = command.prompt; |
| 124 | if (userInput) { |
| 125 | rendered += `\n\n${userInput}`; |
| 126 | } |
| 127 | if (rendered) { |
| 128 | slashedParts.push({ |
| 129 | type: "text", |
| 130 | text: rendered, |
| 131 | }); |
| 132 | } else { |
| 133 | console.warn( |
| 134 | `Invalid/empty prompt + input from slash command ${command.name}`, |
| 135 | ); |
| 136 | } |
| 137 | default: |
| 138 | break; |
| 139 | } |
| 140 | return { |
| 141 | slashedParts, |
| 142 | legacyCommandWithInput, |
| 143 | contextRequests, |
| 144 | }; |
| 145 | } |