From 2616a35e292f4da9e4a2d36f59ce3d4517009a71 Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Wed, 24 Jan 2024 13:36:31 -0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=9D=20submenu=20context=20provider=20e?= =?UTF-8?q?xample?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/docs/customization/context-providers.md | 79 ++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/docs/docs/customization/context-providers.md b/docs/docs/customization/context-providers.md index 5d710f1c3..a757346ee 100644 --- a/docs/docs/customization/context-providers.md +++ b/docs/docs/customization/context-providers.md @@ -172,6 +172,83 @@ export function modifyConfig(config: Config): Config { No modification in `config.json` is necessary. +### Custom Context Providers with Submenu or Query + +There are 3 types of context providers: "normal", "query", and "submenu". The "normal" type is the default, and is what we've seen so far. + +The **"query"** type is used when you want to display a text box to the user, and then use the contents of that text box to generate the context items. Built-in examples include ["search"](#exact-search) and ["google"](#google). This text is what gets passed to the "query" argument in `getContextItems`. To implement a "query" context provider, simply set `"type": "query"` in your custom context provider object. + +The **"submenu"** type is used when you want to display a list of searchable items in the dropdown. Built-in examples include ["issue"](#github-issues) and ["folder"](#folders). To implement a "submenu" context provider, set `"type": "submenu"` and implement the `loadSubmenuItems` and `getContextItems` functions. Here is an example that shows a list of all README files in the current workspace: + +```typescript title="~/.continue/config.ts" +const ReadMeContextProvider: CustomContextProvider = { + title: "readme", + displayTitle: "README", + description: "Reference README.md files in your workspace", + type: "submenu", + + getContextItems: async ( + query: string, + extras: ContextProviderExtras + ): Promise => { + // 'query' is the filepath of the README selected from the dropdown + const content = await extras.ide.readFile(query); + return [ + { + name: getFolder(query), + description: getFolderAndBasename(query), + content, + }, + ]; + }, + + loadSubmenuItems: async ( + args: LoadSubmenuItemsArgs + ): Promise => { + // Filter all workspace files for READMEs + const allFiles = await args.ide.listWorkspaceContents(); + const readmes = allFiles.filter((filepath) => + filepath.endsWith("README.md") + ); + + // Return the items that will be shown in the dropdown + return readmes.map((filepath) => { + return { + id: filepath, + title: getFolder(filepath), + description: getFolderAndBasename(filepath), + }; + }); + }, +}; + +export function modifyConfig(config: Config): Config { + if (!config.contextProviders) { + config.contextProviders = []; + } + config.contextProviders.push(ReadMeContextProvider); + return config; +} + +function getFolder(path: string): string { + return path.split(/[\/\\]/g).slice(-2)[0]; +} + +function getFolderAndBasename(path: string): string { + return path + .split(/[\/\\]/g) + .slice(-2) + .join("/"); +} +``` + +The flow of information in the above example is as follows: + +1. The user types `@readme` and selects it from the dropdown, now displaying the submenu where they can search for any item returned by `loadSubmenuItems`. +2. The user selects one of the READMEs in the submenu, enters the rest of their input, and presses enter. +3. The `id` of the chosen `ContextSubmenuItem` is passed to `getContextItems` as the `query` argument. In this case it is the filepath of the README. +4. The `getContextItems` function can then use the `query` to retrieve the full contents of the README and format the content before returning the context item which will be included in the prompt. + ### Importing outside modules > Context providers run in a NodeJS environment, but for the time being the `modifyConfig` function will be called both from NodeJS _and_ the browser. This means that if you are importing packages requiring NodeJS, you should dynamically import them inside of the context provider (e.g. `const moduleA = await import('./moduleA');`). @@ -185,9 +262,11 @@ Continue will use [esbuild](https://esbuild.github.io/) to bundle your `config.t - `title`: An identifier for the context provider - `displayTitle` (optional): The title displayed in the dropdown - `description` (optional): The longer description displayed in the dropdown when hovered +- `type` (optional): The type of context provider. Options are "normal", "query", and "submenu". Defaults to "normal". - `getContextItems`: A function that returns the documents to include in the prompt. It should return a list of `ContextItem`s, and is given access to the following arguments: - `extras.fullInput`: A string representing the user's full input to the text box. This can be used for example to generate an embedding to compare against a set of other embedded documents - `extras.embeddingsProvider`: The embeddings provider has an `embed` function that will convert text (such as `fullInput`) to an embedding - `extras.llm`: The current default LLM, which you can use to make completion requests - `extras.ide`: An instance of the `IDE` class, which lets you gather various sources of information from the IDE, including the contents of the terminal, the list of open files, or any warnings in the currently open file. - `query`: (not currently used) A string representing the query +- `loadSubmenuItems` (optional): A function that returns a list of `ContextSubmenuItem`s to display in a submenu. It is given access to an `IDE`, the same that is passed to `getContextItems`.