diff --git a/core/commands/index.ts b/core/commands/index.ts index b6f233485..7115d22be 100644 --- a/core/commands/index.ts +++ b/core/commands/index.ts @@ -1,5 +1,6 @@ import { CustomCommand, SlashCommand, SlashCommandDescription } from "../"; import { renderTemplatedString } from "../promptFiles/v1/renderTemplatedString"; +import { replaceSlashCommandWithPromptInChatHistory } from "../promptFiles/v1/updateChatHistory"; import { renderChatMessage } from "../util/messageContent"; import SlashCommands from "./slash"; @@ -15,58 +16,26 @@ export function slashFromCustomCommand( description: customCommand.description ?? "", prompt: customCommand.prompt, run: async function* ({ input, llm, history, ide, completionOptions }) { - console.log("SLASH COMMAND RUN", input, prompt, commandName); - // Remove slash command prefix from input - let userInput = input; - if (userInput.startsWith(commandName)) { - userInput = userInput.substring(commandName.length).trimStart(); - } - // Render prompt template - let promptUserInput: string; + let renderedPrompt: string; if (customCommand.prompt.includes("{{{ input }}}")) { - promptUserInput = await renderTemplatedString( + renderedPrompt = await renderTemplatedString( customCommand.prompt, ide.readFile.bind(ide), - { input: userInput }, + { input }, ); } else { - promptUserInput = customCommand.prompt + "\n\n" + userInput; + renderedPrompt = customCommand.prompt + "\n\n" + input; } - const messages = [...history]; - // Find the last chat message with this slash command and replace it with the user input - for (let i = messages.length - 1; i >= 0; i--) { - const message = messages[i]; - const { role, content } = message; - if (role !== "user") { - continue; - } - - if ( - Array.isArray(content) && - content.some( - (part) => "text" in part && part.text?.startsWith(commandName), - ) - ) { - messages[i] = { - ...message, - content: content.map((part) => { - if ("text" in part && part.text.startsWith(commandName)) { - return { type: "text", text: promptUserInput }; - } - return part; - }), - }; - break; - } else if ( - typeof content === "string" && - content.startsWith(commandName) - ) { - messages[i] = { ...message, content: promptUserInput }; - break; - } - } + // Replaces slash command messages with the rendered prompt + // which INCLUDES the input + const messages = replaceSlashCommandWithPromptInChatHistory( + history, + commandName, + renderedPrompt, + undefined, + ); for await (const chunk of llm.streamChat( messages, diff --git a/core/commands/slash/http.ts b/core/commands/slash/http.ts index 7a662561c..1c0d38626 100644 --- a/core/commands/slash/http.ts +++ b/core/commands/slash/http.ts @@ -1,11 +1,11 @@ import { SlashCommand } from "../../index.js"; -import { removeQuotesAndEscapes } from "../../util/index.js"; import { streamResponse } from "../../llm/stream.js"; +import { removeQuotesAndEscapes } from "../../util/index.js"; const HttpSlashCommand: SlashCommand = { name: "http", description: "Call an HTTP endpoint to serve response", - run: async function* ({ ide, llm, input, params, fetch }) { + run: async function* ({ input, params, fetch }) { const url = params?.url; if (!url) { throw new Error("URL is not defined in params"); diff --git a/core/promptFiles/v1/slashCommandFromPromptFile.ts b/core/promptFiles/v1/slashCommandFromPromptFile.ts index b9d4e88c8..6abd4927f 100644 --- a/core/promptFiles/v1/slashCommandFromPromptFile.ts +++ b/core/promptFiles/v1/slashCommandFromPromptFile.ts @@ -5,7 +5,7 @@ import { parsePromptFileV1V2 } from "../v2/parsePromptFileV1V2"; import { getContextProviderHelpers } from "./getContextProviderHelpers"; import { renderTemplatedString } from "./renderTemplatedString"; -import { updateChatHistory } from "./updateChatHistory"; +import { replaceSlashCommandWithPromptInChatHistory } from "./updateChatHistory"; export function extractName(preamble: { name?: string }, path: string): string { return preamble.name ?? getLastNPathParts(path, 1).split(".prompt")[0]; @@ -68,7 +68,7 @@ export function slashCommandFromPromptFileV1( const userInput = extractUserInput(context.input, name); const renderedPrompt = await renderPromptV1(prompt, context, userInput); - const messages = updateChatHistory( + const messages = replaceSlashCommandWithPromptInChatHistory( context.history, name, renderedPrompt, diff --git a/core/promptFiles/v1/updateChatHistory.ts b/core/promptFiles/v1/updateChatHistory.ts index 8ebc6f038..04cbcd91b 100644 --- a/core/promptFiles/v1/updateChatHistory.ts +++ b/core/promptFiles/v1/updateChatHistory.ts @@ -1,4 +1,4 @@ -export function updateChatHistory( +export function replaceSlashCommandWithPromptInChatHistory( history: any[], commandName: string, renderedPrompt: string, diff --git a/gui/src/components/CodeToEditCard/CodeToEditCard.tsx b/gui/src/components/CodeToEditCard/CodeToEditCard.tsx index 99149ba5f..772777337 100644 --- a/gui/src/components/CodeToEditCard/CodeToEditCard.tsx +++ b/gui/src/components/CodeToEditCard/CodeToEditCard.tsx @@ -1,16 +1,16 @@ +import { PlusIcon, XMarkIcon } from "@heroicons/react/24/outline"; +import type { CodeToEdit } from "core"; import { useContext, useState } from "react"; import { useDispatch } from "react-redux"; import { IdeMessengerContext } from "../../context/IdeMessenger"; -import CodeToEditListItem from "./CodeToEditListItem"; -import type { CodeToEdit } from "core"; -import AddFileButton from "./AddFileButton"; -import AddFileCombobox from "./AddFileCombobox"; -import { PlusIcon, XMarkIcon } from "@heroicons/react/24/outline"; import { useAppSelector } from "../../redux/hooks"; import { addCodeToEdit, removeCodeToEdit, } from "../../redux/slices/sessionSlice"; +import AddFileButton from "./AddFileButton"; +import AddFileCombobox from "./AddFileCombobox"; +import CodeToEditListItem from "./CodeToEditListItem"; export default function CodeToEditCard() { const dispatch = useDispatch(); @@ -56,7 +56,7 @@ export default function CodeToEditCard() { return (