diff --git a/core/commands/index.ts b/core/commands/index.ts index ff0dc35db..5cef3ccb7 100644 --- a/core/commands/index.ts +++ b/core/commands/index.ts @@ -1,43 +1,58 @@ import { CustomCommand, SlashCommand, SlashCommandDescription } from ".."; import { stripImages } from "../llm/countTokens"; +import { renderTemplatedString } from "../llm/llms"; import SlashCommands from "./slash"; export function slashFromCustomCommand( - customCommand: CustomCommand, + 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; - } - } + return { + name: customCommand.name, + description: customCommand.description, + run: async function* ({ input, llm, history, ide }) { + // Remove slash command prefix from input + let userInput = input; + if (userInput.startsWith(`/${customCommand.name}`)) { + userInput = userInput + .slice(customCommand.name.length + 1, userInput.length) + .trimStart(); + } - for await (const chunk of llm.streamChat(messages)) { - yield stripImages(chunk.content); - } - }, - }; + // Render prompt template + const promptUserInput = await renderTemplatedString( + customCommand.prompt, + ide.readFile.bind(ide), + { input: userInput }, + ); + + 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, + desc: SlashCommandDescription, ): SlashCommand | undefined { - const cmd = SlashCommands.find((cmd) => cmd.name === desc.name); - if (!cmd) { - return undefined; - } - return { - ...cmd, - params: desc.params, - }; + const cmd = SlashCommands.find((cmd) => cmd.name === desc.name); + if (!cmd) { + return undefined; + } + return { + ...cmd, + params: desc.params, + }; } diff --git a/core/llm/llms/index.ts b/core/llm/llms/index.ts index 1920b2844..9b6c027d4 100644 --- a/core/llm/llms/index.ts +++ b/core/llm/llms/index.ts @@ -1,10 +1,10 @@ import Handlebars from "handlebars"; import { BaseLLM } from ".."; import { - BaseCompletionOptions, - ILLM, - LLMOptions, - ModelDescription, + BaseCompletionOptions, + ILLM, + LLMOptions, + ModelDescription, } from "../.."; import { DEFAULT_MAX_TOKENS } from "../constants"; import Anthropic from "./Anthropic"; @@ -28,121 +28,121 @@ import TextGenWebUI from "./TextGenWebUI"; import Together from "./Together"; function convertToLetter(num: number): string { - let result = ""; - while (num > 0) { - const remainder = (num - 1) % 26; - result = String.fromCharCode(97 + remainder) + result; - num = Math.floor((num - 1) / 26); - } - return result; + let result = ""; + while (num > 0) { + const remainder = (num - 1) % 26; + result = String.fromCharCode(97 + remainder) + result; + num = Math.floor((num - 1) / 26); + } + return result; } const getHandlebarsVars = ( - value: string, + value: string, ): [string, { [key: string]: string }] => { - const ast = Handlebars.parse(value); + const ast = Handlebars.parse(value); - let keysToFilepath: { [key: string]: string } = {}; - let keyIndex = 1; - for (let i in ast.body) { - if (ast.body[i].type === "MustacheStatement") { - const letter = convertToLetter(keyIndex); - keysToFilepath[letter] = (ast.body[i] as any).path.original; - value = value.replace( - new RegExp("{{\\s*" + (ast.body[i] as any).path.original + "\\s*}}"), - `{{${letter}}}`, - ); - keyIndex++; - } - } - return [value, keysToFilepath]; + let keysToFilepath: { [key: string]: string } = {}; + let keyIndex = 1; + for (let i in ast.body) { + if (ast.body[i].type === "MustacheStatement") { + const letter = convertToLetter(keyIndex); + keysToFilepath[letter] = (ast.body[i] as any).path.original; + value = value.replace( + new RegExp("{{\\s*" + (ast.body[i] as any).path.original + "\\s*}}"), + `{{${letter}}}`, + ); + keyIndex++; + } + } + return [value, keysToFilepath]; }; -async function renderTemplatedString( - template: string, - readFile: (filepath: string) => Promise, +export async function renderTemplatedString( + template: string, + readFile: (filepath: string) => Promise, + inputData: any, ): Promise { - const [newTemplate, vars] = getHandlebarsVars(template); - template = newTemplate; - let data: any = {}; - for (let key in vars) { - let fileContents = await readFile(vars[key]); - data[key] = fileContents || vars[key]; - } - const templateFn = Handlebars.compile(template); - let final = templateFn(data); - return final; + const [newTemplate, vars] = getHandlebarsVars(template); + const data: any = { ...inputData }; + for (const key in vars) { + const fileContents = await readFile(vars[key]); + data[key] = fileContents || (inputData[vars[key]] ?? vars[key]); + } + const templateFn = Handlebars.compile(newTemplate); + const final = templateFn(data); + return final; } const LLMs = [ - Anthropic, - FreeTrial, - Gemini, - Llamafile, - Ollama, - Replicate, - TextGenWebUI, - Together, - HuggingFaceTGI, - HuggingFaceInferenceAPI, - LlamaCpp, - OpenAI, - LMStudio, - Mistral, - Bedrock, - DeepInfra, - OpenAIFreeTrial, - Flowise, - Groq, + Anthropic, + FreeTrial, + Gemini, + Llamafile, + Ollama, + Replicate, + TextGenWebUI, + Together, + HuggingFaceTGI, + HuggingFaceInferenceAPI, + LlamaCpp, + OpenAI, + LMStudio, + Mistral, + Bedrock, + DeepInfra, + OpenAIFreeTrial, + Flowise, + Groq, ]; export async function llmFromDescription( - desc: ModelDescription, - readFile: (filepath: string) => Promise, - completionOptions?: BaseCompletionOptions, - systemMessage?: string, + desc: ModelDescription, + readFile: (filepath: string) => Promise, + completionOptions?: BaseCompletionOptions, + systemMessage?: string, ): Promise { - const cls = LLMs.find((llm) => llm.providerName === desc.provider); + const cls = LLMs.find((llm) => llm.providerName === desc.provider); - if (!cls) { - return undefined; - } + if (!cls) { + return undefined; + } - const finalCompletionOptions = { - ...completionOptions, - ...desc.completionOptions, - }; + const finalCompletionOptions = { + ...completionOptions, + ...desc.completionOptions, + }; - systemMessage = desc.systemMessage ?? systemMessage; - if (systemMessage !== undefined) { - systemMessage = await renderTemplatedString(systemMessage, readFile); - } + systemMessage = desc.systemMessage ?? systemMessage; + if (systemMessage !== undefined) { + systemMessage = await renderTemplatedString(systemMessage, readFile, {}); + } - const options: LLMOptions = { - ...desc, - completionOptions: { - ...finalCompletionOptions, - model: (desc.model || cls.defaultOptions?.model) ?? "codellama-7b", - maxTokens: - finalCompletionOptions.maxTokens ?? - cls.defaultOptions?.completionOptions?.maxTokens ?? - DEFAULT_MAX_TOKENS, - }, - systemMessage, - }; + const options: LLMOptions = { + ...desc, + completionOptions: { + ...finalCompletionOptions, + model: (desc.model || cls.defaultOptions?.model) ?? "codellama-7b", + maxTokens: + finalCompletionOptions.maxTokens ?? + cls.defaultOptions?.completionOptions?.maxTokens ?? + DEFAULT_MAX_TOKENS, + }, + systemMessage, + }; - return new cls(options); + return new cls(options); } export function llmFromProviderAndOptions( - providerName: string, - llmOptions: LLMOptions, + providerName: string, + llmOptions: LLMOptions, ): ILLM { - const cls = LLMs.find((llm) => llm.providerName === providerName); + const cls = LLMs.find((llm) => llm.providerName === providerName); - if (!cls) { - throw new Error(`Unknown LLM provider type "${providerName}"`); - } + if (!cls) { + throw new Error(`Unknown LLM provider type "${providerName}"`); + } - return new cls(llmOptions); + return new cls(llmOptions); } diff --git a/docs/static/schemas/config.json b/docs/static/schemas/config.json index 54384feb9..591403628 100644 --- a/docs/static/schemas/config.json +++ b/docs/static/schemas/config.json @@ -1590,7 +1590,7 @@ { "name": "test", "prompt": "Write a comprehensive set of unit tests for the selected code. It should setup, run tests that check for correctness including important edge cases, and teardown. Ensure that the tests are complete and sophisticated. Give the tests just as chat output, don't edit any file.", - "description": "This is an example custom command. Use /config to edit it and create more" + "description": "This is an example custom command. Open config.json to edit it and create more" } ], "type": "array", diff --git a/extensions/intellij/src/main/resources/config_schema.json b/extensions/intellij/src/main/resources/config_schema.json index 54384feb9..591403628 100644 --- a/extensions/intellij/src/main/resources/config_schema.json +++ b/extensions/intellij/src/main/resources/config_schema.json @@ -1590,7 +1590,7 @@ { "name": "test", "prompt": "Write a comprehensive set of unit tests for the selected code. It should setup, run tests that check for correctness including important edge cases, and teardown. Ensure that the tests are complete and sophisticated. Give the tests just as chat output, don't edit any file.", - "description": "This is an example custom command. Use /config to edit it and create more" + "description": "This is an example custom command. Open config.json to edit it and create more" } ], "type": "array", diff --git a/extensions/vscode/config_schema.json b/extensions/vscode/config_schema.json index 54384feb9..591403628 100644 --- a/extensions/vscode/config_schema.json +++ b/extensions/vscode/config_schema.json @@ -1590,7 +1590,7 @@ { "name": "test", "prompt": "Write a comprehensive set of unit tests for the selected code. It should setup, run tests that check for correctness including important edge cases, and teardown. Ensure that the tests are complete and sophisticated. Give the tests just as chat output, don't edit any file.", - "description": "This is an example custom command. Use /config to edit it and create more" + "description": "This is an example custom command. Open config.json to edit it and create more" } ], "type": "array", diff --git a/extensions/vscode/continue_rc_schema.json b/extensions/vscode/continue_rc_schema.json index 79279685c..d963b8552 100644 --- a/extensions/vscode/continue_rc_schema.json +++ b/extensions/vscode/continue_rc_schema.json @@ -1780,7 +1780,7 @@ { "name": "test", "prompt": "Write a comprehensive set of unit tests for the selected code. It should setup, run tests that check for correctness including important edge cases, and teardown. Ensure that the tests are complete and sophisticated. Give the tests just as chat output, don't edit any file.", - "description": "This is an example custom command. Use /config to edit it and create more" + "description": "This is an example custom command. Open config.json to edit it and create more" } ], "type": "array", diff --git a/extensions/vscode/tsconfig.json b/extensions/vscode/tsconfig.json index 9d1481221..0a9234dae 100644 --- a/extensions/vscode/tsconfig.json +++ b/extensions/vscode/tsconfig.json @@ -15,5 +15,5 @@ "resolveJsonModule": true /* Enable importing .json files */ }, "include": ["src/**/*", "../../core/**/*.ts"], - "exclude": ["../../core/dist/**/*"] + "exclude": ["../../core/dist/**/*", "../../core/test/**/*"] }