diff --git a/core/config/load.ts b/core/config/load.ts index ad5a07734..59fc3d3ea 100644 --- a/core/config/load.ts +++ b/core/config/load.ts @@ -154,15 +154,15 @@ async function serializedToIntermediateConfig( const promptFolder = initial.experimental?.promptPath; let promptFiles: { path: string; content: string }[] = []; - if (promptFolder) { - promptFiles = ( - await Promise.all( - workspaceDirs.map((dir) => getPromptFiles(ide, promptFolder)), - ) + promptFiles = ( + await Promise.all( + workspaceDirs.map((dir) => + getPromptFiles(ide, path.join(dir, promptFolder ?? ".prompts")), + ), ) - .flat() - .filter(({ path }) => path.endsWith(".prompt")); - } + ) + .flat() + .filter(({ path }) => path.endsWith(".prompt")); // Also read from ~/.continue/.prompts promptFiles.push(...readAllGlobalPromptFiles()); diff --git a/core/config/promptFile.ts b/core/config/promptFile.ts index dfe8c76f1..9fa66c892 100644 --- a/core/config/promptFile.ts +++ b/core/config/promptFile.ts @@ -1,5 +1,5 @@ -import type { IDE, SlashCommand } from ".."; import * as YAML from "yaml"; +import type { IDE, SlashCommand } from ".."; import { stripImages } from "../llm/countTokens.js"; import { renderTemplatedString } from "../llm/llms/index.js"; import { getBasename } from "../util/index.js"; @@ -44,7 +44,16 @@ export function slashCommandFromPromptFile( return { name, description, - run: async function* ({ input, llm, history, ide }) { + run: async function* ({ + input, + llm, + history, + ide, + config, + fetch, + selectedCode, + addContextItem, + }) { // Remove slash command prefix from input let userInput = input; if (userInput.startsWith(`/${name}`)) { @@ -54,6 +63,35 @@ export function slashCommandFromPromptFile( } // Render prompt template + const helpers: [string, Handlebars.HelperDelegate][] | undefined = + config.contextProviders?.map((provider) => { + return [ + provider.description.title, + async (context: any) => { + const items = await provider.getContextItems(context, { + embeddingsProvider: config.embeddingsProvider, + fetch, + fullInput: userInput, + ide, + llm, + reranker: config.reranker, + selectedCode, + }); + items.forEach((item) => + addContextItem({ + ...item, + id: { + itemId: item.description, + providerTitle: provider.description.title, + }, + }), + ); + return items.map((item) => item.content).join("\n\n"); + }, + ]; + }); + + // A few context providers that don't need to be in config.json to work in .prompt files const diff = await ide.getDiff(); const currentFilePath = await ide.getCurrentFile(); const promptUserInput = await renderTemplatedString( @@ -66,6 +104,7 @@ export function slashCommandFromPromptFile( ? await ide.readFile(currentFilePath) : undefined, }, + helpers, ); const messages = [...history]; diff --git a/core/core.ts b/core/core.ts index fbbfdbf78..8aa577251 100644 --- a/core/core.ts +++ b/core/core.ts @@ -371,6 +371,7 @@ export class Core { configHandler: ConfigHandler, abortedMessageIds: Set, msg: Message, + messenger: IMessenger, ) { const { input, @@ -404,11 +405,10 @@ export class Core { params, ide, addContextItem: (item) => { - // TODO - // protocol.request("addContextItem", { - // item, - // historyIndex, - // }); + messenger.request("addContextItem", { + item, + historyIndex, + }); }, selectedCode, config, @@ -422,7 +422,12 @@ export class Core { yield { done: true, content: "" }; } on("command/run", (msg) => - runNodeJsSlashCommand(this.configHandler, this.abortedMessageIds, msg), + runNodeJsSlashCommand( + this.configHandler, + this.abortedMessageIds, + msg, + this.messenger, + ), ); // Autocomplete diff --git a/core/llm/llms/index.ts b/core/llm/llms/index.ts index f85c81087..4fbf29d8b 100644 --- a/core/llm/llms/index.ts +++ b/core/llm/llms/index.ts @@ -1,4 +1,5 @@ import Handlebars from "handlebars"; +import { v4 as uuidv4 } from "uuid"; import { BaseCompletionOptions, ILLM, @@ -67,7 +68,19 @@ export async function renderTemplatedString( template: string, readFile: (filepath: string) => Promise, inputData: any, + helpers?: [string, Handlebars.HelperDelegate][], ): Promise { + const promises: { [key: string]: Promise } = {}; + if (helpers) { + for (const [name, helper] of helpers) { + Handlebars.registerHelper(name, (...args) => { + const id = uuidv4(); + promises[id] = helper(...args); + return `__${id}__`; + }); + } + } + const [newTemplate, vars] = getHandlebarsVars(template); const data: any = { ...inputData }; for (const key in vars) { @@ -75,7 +88,13 @@ export async function renderTemplatedString( data[key] = fileContents || (inputData[vars[key]] ?? vars[key]); } const templateFn = Handlebars.compile(newTemplate); - const final = templateFn(data); + let final = templateFn(data); + + await Promise.all(Object.values(promises)); + for (const id in promises) { + final = final.replace(`__${id}__`, await promises[id]); + } + return final; } diff --git a/core/protocol/ideWebview.ts b/core/protocol/ideWebview.ts index ca80fc482..3683d9ec0 100644 --- a/core/protocol/ideWebview.ts +++ b/core/protocol/ideWebview.ts @@ -1,4 +1,4 @@ -import type { ContextItemWithId, ContextSubmenuItem } from ".."; +import type { ContextSubmenuItem } from ".."; import type { RangeInFileWithContents } from "../commands/util"; import { ToIdeFromWebviewOrCoreProtocol } from "./ide.js"; import { ToWebviewFromIdeOrCoreProtocol } from "./webview.js"; @@ -38,13 +38,6 @@ export type ToIdeFromWebviewProtocol = ToIdeFromWebviewOrCoreProtocol & { export type ToWebviewFromIdeProtocol = ToWebviewFromIdeOrCoreProtocol & { setInactive: [undefined, void]; submitMessage: [{ message: any }, void]; // any -> JSONContent from TipTap - addContextItem: [ - { - historyIndex: number; - item: ContextItemWithId; - }, - void, - ]; updateSubmenuItems: [ { provider: string; submenuItems: ContextSubmenuItem[] }, void, diff --git a/core/protocol/passThrough.ts b/core/protocol/passThrough.ts index 806812c9a..441659823 100644 --- a/core/protocol/passThrough.ts +++ b/core/protocol/passThrough.ts @@ -43,4 +43,4 @@ export const WEBVIEW_TO_CORE_PASS_THROUGH: (keyof ToCoreFromWebviewProtocol)[] = // Message types to pass through from core to webview export const CORE_TO_WEBVIEW_PASS_THROUGH: (keyof ToWebviewFromCoreProtocol)[] = - ["configUpdate", "getDefaultModelTitle", "indexProgress"]; + ["configUpdate", "getDefaultModelTitle", "indexProgress", "addContextItem"]; diff --git a/core/protocol/webview.ts b/core/protocol/webview.ts index a2bf157f7..c0326bfa0 100644 --- a/core/protocol/webview.ts +++ b/core/protocol/webview.ts @@ -1,8 +1,15 @@ -import type { IndexingProgressUpdate } from ".."; +import type { ContextItemWithId, IndexingProgressUpdate } from ".."; export type ToWebviewFromIdeOrCoreProtocol = { configUpdate: [undefined, void]; getDefaultModelTitle: [undefined, string]; indexProgress: [IndexingProgressUpdate, void]; refreshSubmenuItems: [undefined, void]; + addContextItem: [ + { + historyIndex: number; + item: ContextItemWithId; + }, + void, + ]; }; diff --git a/docs/docs/walkthroughs/prompt-files.md b/docs/docs/walkthroughs/prompt-files.md index 8def96d06..90faff0d4 100644 --- a/docs/docs/walkthroughs/prompt-files.md +++ b/docs/docs/walkthroughs/prompt-files.md @@ -64,9 +64,13 @@ To add a system message, start the body with `` tags like in th The body also supports templating with [Handlebars syntax](https://handlebarsjs.com/guide/). The following variables are currently available: - `input`: The full text from the input box in the sidebar that is sent along with the slash command -- `diff`: The current git diff in your workspace +- `currentFile`: The currently open file in your IDE -We plan to add support soon for templating with all [context providers](../customization/context-providers.md). +#### Context providers + +The body of a .prompt file also supports any [context provider](../customization/context-providers.md) that you have added to your config by referencing the name of the context provider. + +For example, if you wanted to include the contents of the terminal in your prompt, then you would use `{{{terminal}}}` in your prompt file. If you wanted to use the "url" context provider to include the contents of https://github.com/continuedev/continue, you would use `{{{url "https://github.com/continuedev/continue"}}}`, where the second part is the argument to the context provider, separated by a space. ## Feedback