Elffuss BraidHub

kikocis/continue public

9c6973727fab · 9.7 KiBSourceRawPermalink

title: 构建你自己的上下文提供者

入门示例

要编写你自己的上下文提供者,你只需要实现 CustomContextProvider 接口:

interface CustomContextProvider {
  title: string;
  displayTitle?: string;
  description?: string;
  renderInlineAs?: string;
  type?: ContextProviderType;
  getContextItems(
    query: string,
    extras: ContextProviderExtras,
  ): Promise<ContextItem[]>;
  loadSubmenuItems?: (
    args: LoadSubmenuItemsArgs,
  ) => Promise<ContextSubmenuItem[]>;
}

作为一个例子,比如你有一系列内部文件索引到一个向量数据库中。你设置了一个简单的 REST API ,允许内部用户查询和获取相关的片段。这个上下文提供者将会发送请求到这个服务器,并从向量数据库返回结果。返回的类型 getContextItems 必须 是一个包含所有以下属性的实例的列表:

const RagContextProvider: CustomContextProvider = {
  title: "rag",
  displayTitle: "RAG",
  description:
    "Retrieve snippets from our vector database of internal documents",

  getContextItems: async (
    query: string,
    extras: ContextProviderExtras,
  ): Promise<ContextItem[]> => {
    const response = await fetch("https://internal_rag_server.com/retrieve", {
      method: "POST",
      body: JSON.stringify({ query }),
    });

    const results = await response.json();

    return results.map((result) => ({
      name: result.title,
      description: result.title,
      content: result.contents,
    }));
  },
};

它可以被添加到 config.ts 像这样:

export function modifyConfig(config: Config): Config {
  if (!config.contextProviders) {
    config.contextProviders = [];
  }
  config.contextProviders.push(RagContextProvider);
  return config;
}

不需要对 config.json 做修改。

定制有 submenu 或 query 的上下文提供者

这里有 3 种类型的上下文提供者:"normal", "query" 和 "submenu" 。 "normal" 类型是默认的,就是你目前所看到的。

"query" 类型用来当你想要显示文本框给用户,然后使用那个文本框的内容生成上下文条目。内置示例包含 "search" 和 "google" 。这个文本是传递给 getContextItems 中的 "query" 参数。为了实现一个 "query" 上下文提供者,简单地设置 "type": "query" 在你的定制上下文提供者实例中。

"submenu" 类型用来当你想要在下拉框显示一个可搜索条目列表。内置示例包含 "issue" 和 "folder" 。为了实现一个 "submenu" 上下文提供者,设置 "type": "submenu" 并实现 loadSubmenuItems 和 getContextItems 函数。这是一个例子,显示当前工作区所有 README 文件的列表:

const ReadMeContextProvider: CustomContextProvider = {
  title: "readme",
  displayTitle: "README",
  description: "Reference README.md files in your workspace",
  type: "submenu",

  getContextItems: async (
    query: string,
    extras: ContextProviderExtras,
  ): Promise<ContextItem[]> => {
    // '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<ContextSubmenuItem[]> => {
    // 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("/");
}

以上示例的信息流如下:

  1. 用户输入 @readme 并从下拉框中选择,现在显示子菜单,他们可以搜索任何 loadSubmenuItems 返回的条目。
  2. 用户在子菜单中选择 README 之一,输入其他输入,并按下回车。
  3. 选择的 ContextSubmenuItem 的 id 传递给 getContextItems 作为 query 参数。在这个例子中,它是 README 的文件路径。
  4. 然后 getContextItems 函数使用 query 获取 README 的完整内容,并在返回上下文条目之前格式化内容,它会包含在提示词之中。

导入外部模块

为了包含外部的 Node 模块在你的 config.ts 中,在 ~/.continue 文件夹运行 npm install <module_name> ,然后在 config.ts 中导入它们。

Continue 将使用 esbuild 打包你的 config.ts 和任何依赖到一个单独的 Javascript 文件。确切使用的配置可以在 这里 找到。

CustomContextProvider 参考

使用其他语言编写上下文提供者

如果你想要用除了 TypeScript 的其他语言编写上下文提供者,你可以使用 "http" 上下文提供者,来调用一个托管你自己代码的服务器。添加上下文提供者到 config.json ,像这样:

{
  "name": "http",
  "params": {
    "url": "https://myserver.com/context-provider",
    "title": "http",
    "description": "Custom HTTP Context Provider",
    "displayTitle": "My Custom Context"
  }
}

VSCode 的扩展 API

Continue 暴露一个 API ,从第三方 VSCode 扩展注册上下文提供者。这是有用的,如果你有一个 VSCode 扩展,提供一些额外的上下文,你想要在 Continue 中使用。为了使用这个 API ,添加以下内容到你的 package.json :

{
  "extensionDependencies": ["continue.continue"]
}

或者复制 ~/.continue/type/core/index.d.ts 到你的扩展仓库。

然后,你可以使用 registerCustomContextProvider 函数注册你的上下文提供者。你定制的上下文提供者必须实现 IContextProvider 接口。 这是一个例子:

import * as vscode from "vscode";

class MyCustomProvider implements IContextProvider {
  get description(): ContextProviderDescription {
    return {
      title: "custom",
      displayTitle: "Custom",
      description: "Custom description",
      type: "normal",
    };
  }

  async getContextItems(
    query: string,
    extras: ContextProviderExtras,
  ): Promise<ContextItem[]> {
    return [
      {
        name: "Custom",
        description: "Custom description",
        content: "Custom content",
      },
    ];
  }

  async loadSubmenuItems(
    args: LoadSubmenuItemsArgs,
  ): Promise<ContextSubmenuItem[]> {
    return [];
  }
}

// create an instance of your custom provider
const customProvider = new MyCustomProvider();

// get Continue extension using vscode API
const continueExt = vscode.extensions.getExtension("continue.continue");

// get the API from the extension
const continueApi = continueExt?.exports;

// register your custom provider
continueApi?.registerCustomContextProvider(customProvider);