43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
import { CustomCommand, SlashCommand, SlashCommandDescription } from "..";
|
|
import { stripImages } from "../llm/countTokens";
|
|
import SlashCommands from "./slash";
|
|
|
|
export function slashFromCustomCommand(
|
|
customCommand: CustomCommand,
|
|
): SlashCommand {
|
|
return {
|
|
name: customCommand.name,
|
|
description: customCommand.description,
|
|
run: async function* ({ input, llm, history }) {
|
|
const promptUserInput = `Task: ${customCommand.prompt}. Additional info: ${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--) {
|
|
if (
|
|
messages[i].role === "user" &&
|
|
stripImages(messages[i].content).startsWith(`/${customCommand.name}`)
|
|
) {
|
|
messages[i] = { ...messages[i], content: promptUserInput };
|
|
break;
|
|
}
|
|
}
|
|
|
|
for await (const chunk of llm.streamChat(messages)) {
|
|
yield stripImages(chunk.content);
|
|
}
|
|
},
|
|
};
|
|
}
|
|
|
|
export function slashCommandFromDescription(
|
|
desc: SlashCommandDescription,
|
|
): SlashCommand | undefined {
|
|
const cmd = SlashCommands.find((cmd) => cmd.name === desc.name);
|
|
if (!cmd) {
|
|
return undefined;
|
|
}
|
|
return {
|
|
...cmd,
|
|
params: desc.params,
|
|
};
|
|
}
|