🚀 在 VS Code 中

教程:使用语言模型 API 生成 AI 驱动的代码注释

在本教程中,你将学习如何创建一个 VS Code 扩展来构建 AI 驱动的代码导师。你将使用语言模型 (LM) API 生成改进代码的建议,并利用 VS Code 扩展 API 将其无缝集成到编辑器中,作为用户可以悬停以获取更多信息的内联注释。完成本教程后,你将了解如何在 VS Code 中实现自定义 AI 功能。

VS Code displaying custom annotations from GitHub Copilot as annotations

先决条件

你需要以下工具和帐户才能完成本教程

搭建扩展

首先,使用 Yeoman 和 VS Code 扩展生成器来搭建一个为开发准备就绪的 TypeScript 或 JavaScript 项目。

npx --package yo --package generator-code -- yo code

选择以下选项以完成新的扩展向导...

# ? What type of extension do you want to create? New Extension (TypeScript)
# ? What's the name of your extension? Code Tutor

### Press <Enter> to choose default for all options below ###

# ? What's the identifier of your extension? code-tutor
# ? What's the description of your extension? LEAVE BLANK
# ? Initialize a git repository? Yes
# ? Bundle the source code with webpack? No
# ? Which package manager to use? npm

# ? Do you want to open the new folder with Visual Studio Code? Open with `code`

修改 package.json 文件以包含正确的命令

搭建的项目在 package.json 文件中包含一个 “helloWorld” 命令。此命令是在安装扩展时出现在命令面板中的内容。

"contributes": {
  "commands": [
      {
      "command": "code-tutor.helloWorld",
      "title": "Hello World"
      }
  ]
}

由于我们正在构建一个代码导师扩展,它将在行中添加注释,因此我们需要一个命令来允许用户打开和关闭这些注释。更新 commandtitle 属性

"contributes": {
  "commands": [
      {
      "command": "code-tutor.annotate",
      "title": "Toggle Tutor Annotations"
      }
  ]
}

虽然 package.json 定义了扩展的命令和 UI 元素,但 src/extension.ts 文件是你放置应该为这些命令执行的代码的地方。

打开 src/extension.ts 文件并更改 registerCommand 方法,使其与 package.json 文件中的 command 属性匹配。

const disposable = vscode.commands.registerCommand('code-tutor.annotate', () => {

F5 运行扩展。这将打开一个新的 VS Code 实例并安装该扩展。按 ⇧⌘P (Windows, Linux Ctrl+Shift+P) 打开命令面板,并搜索 “tutor”。你应该看到 “Tutor Annotations” 命令。

The "Toggle Tutor Annotations" command in the VS Code Command Palette

如果你选择 “Tutor Annotations” 命令,你将看到 “Hello World” 通知消息。

The message 'Hello World from Code Tutor' displayed in a notification

实现 “annotate” 命令

为了使我们的代码导师注释工作,我们需要向其发送一些代码并要求它提供注释。我们将分三个步骤完成此操作

  1. 从用户打开的当前标签页中获取带有行号的代码。
  2. 将该代码连同自定义提示一起发送到语言模型 API,该提示指示模型如何提供注释。
  3. 解析注释并在编辑器中显示它们。

步骤 1:获取带有行号的代码

要从当前标签页获取代码,我们需要引用用户打开的标签页。我们可以通过修改 registerCommand 方法使其成为 registerTextEditorCommand 来获取该引用。这两个命令之间的区别在于,后者为我们提供了对用户打开的标签页的引用,称为 TextEditor

const disposable = vscode.commands.registerTextEditorCommand('code-tutor.annotate', async (textEditor: vscode.TextEditor) => {

现在我们可以使用 textEditor 引用来获取 “可查看编辑器空间” 中的所有代码。这是可以在屏幕上看到的代码 - 它不包括编辑器空间中可见内容上方或下方的代码。

将以下方法直接添加到 extension.ts 文件底部 export function deactivate() { } 行的上方。

function getVisibleCodeWithLineNumbers(textEditor: vscode.TextEditor) {
  // get the position of the first and last visible lines
  let currentLine = textEditor.visibleRanges[0].start.line;
  const endLine = textEditor.visibleRanges[0].end.line;

  let code = '';

  // get the text from the line at the current position.
  // The line number is 0-based, so we add 1 to it to make it 1-based.
  while (currentLine < endLine) {
    code += `${currentLine + 1}: ${textEditor.document.lineAt(currentLine).text} \n`;
    // move to the next line position
    currentLine++;
  }
  return code;
}

此代码使用 TextEditor 的 visibleRanges 属性来获取编辑器中当前可见行的位置。然后,它从第一行位置开始,移动到最后一行位置,并将每行代码以及行号添加到字符串中。最后,它返回包含所有带有行号的可查看代码的字符串。

现在我们可以从 code-tutor.annotate 命令调用此方法。修改命令的实现,使其如下所示

const disposable = vscode.commands.registerTextEditorCommand(
  'code-tutor.annotate',
  async (textEditor: vscode.TextEditor) => {
    // Get the code with line numbers from the current editor
    const codeWithLineNumbers = getVisibleCodeWithLineNumbers(textEditor);
  }
);

步骤 2:将代码和提示发送到语言模型 API

下一步是调用 GitHub Copilot 语言模型,并将用户的代码连同创建注释的指令一起发送给它。

为此,我们首先需要指定我们要使用哪个聊天模型。我们在此处选择 4o,因为对于我们正在构建的交互类型来说,它是一个快速且功能强大的模型。

const disposable = vscode.commands.registerTextEditorCommand(
  'code-tutor.annotate',
  async (textEditor: vscode.TextEditor) => {
    // Get the code with line numbers from the current editor
    const codeWithLineNumbers = getVisibleCodeWithLineNumbers(textEditor);

    // select the 4o chat model
    let [model] = await vscode.lm.selectChatModels({
      vendor: 'copilot',
      family: 'gpt-4o'
    });
  }
);

我们需要指令 - 或 “提示” - 这将告诉模型创建注释以及我们希望响应的格式。将以下代码添加到文件顶部,紧挨着导入下方。

const ANNOTATION_PROMPT = `You are a code tutor who helps students learn how to write better code. Your job is to evaluate a block of code that the user gives you and then annotate any lines that could be improved with a brief suggestion and the reason why you are making that suggestion. Only make suggestions when you feel the severity is enough that it will impact the readability and maintainability of the code. Be friendly with your suggestions and remember that these are students so they need gentle guidance. Format each suggestion as a single JSON object. It is not necessary to wrap your response in triple backticks. Here is an example of what your response should look like:

{ "line": 1, "suggestion": "I think you should use a for loop instead of a while loop. A for loop is more concise and easier to read." }{ "line": 12, "suggestion": "I think you should use a for loop instead of a while loop. A for loop is more concise and easier to read." }
`;

这是一个特殊的提示,它指示语言模型如何生成注释。它还包括模型应如何格式化其响应的示例。这些示例(也称为 “多样本”)使我们能够定义响应将采用的格式,以便我们可以解析它并将其显示为注释。

我们将消息传递给数组中的模型。此数组可以包含任意数量的消息。在我们的例子中,它包含提示,后跟带有行号的用户代码。

const disposable = vscode.commands.registerTextEditorCommand(
  'code-tutor.annotate',
  async (textEditor: vscode.TextEditor) => {
    // Get the code with line numbers from the current editor
    const codeWithLineNumbers = getVisibleCodeWithLineNumbers(textEditor);

    // select the 4o chat model
    let [model] = await vscode.lm.selectChatModels({
      vendor: 'copilot',
      family: 'gpt-4o'
    });

    // init the chat message
    const messages = [
      vscode.LanguageModelChatMessage.User(ANNOTATION_PROMPT),
      vscode.LanguageModelChatMessage.User(codeWithLineNumbers)
    ];
  }
);

要将消息发送到模型,我们首先需要确保所选模型可用。这处理扩展未准备就绪或用户未登录到 GitHub Copilot 的情况。然后我们将消息发送到模型。

const disposable = vscode.commands.registerTextEditorCommand(
  'code-tutor.annotate',
  async (textEditor: vscode.TextEditor) => {
    // Get the code with line numbers from the current editor
    const codeWithLineNumbers = getVisibleCodeWithLineNumbers(textEditor);

    // select the 4o chat model
    let [model] = await vscode.lm.selectChatModels({
      vendor: 'copilot',
      family: 'gpt-4o'
    });

    // init the chat message
    const messages = [
      vscode.LanguageModelChatMessage.User(ANNOTATION_PROMPT),
      vscode.LanguageModelChatMessage.User(codeWithLineNumbers)
    ];

    // make sure the model is available
    if (model) {
      // send the messages array to the model and get the response
      let chatResponse = await model.sendRequest(
        messages,
        {},
        new vscode.CancellationTokenSource().token
      );

      // handle chat response
      await parseChatResponse(chatResponse, textEditor);
    }
  }
);

聊天响应以片段形式出现。这些片段通常包含单个单词,但有时它们仅包含标点符号。为了在响应流式传输时显示注释,我们希望等到我们拥有完整的注释后再显示它。由于我们已指示模型返回其响应的方式,我们知道当我们看到结束 } 时,我们有一个完整的注释。然后我们可以解析注释并在编辑器中显示它。

extension.ts 文件中的 getVisibleCodeWithLineNumbers 方法上方添加缺少的 parseChatResponse 函数。

async function parseChatResponse(
  chatResponse: vscode.LanguageModelChatResponse,
  textEditor: vscode.TextEditor
) {
  let accumulatedResponse = '';

  for await (const fragment of chatResponse.text) {
    accumulatedResponse += fragment;

    // if the fragment is a }, we can try to parse the whole line
    if (fragment.includes('}')) {
      try {
        const annotation = JSON.parse(accumulatedResponse);
        applyDecoration(textEditor, annotation.line, annotation.suggestion);
        // reset the accumulator for the next line
        accumulatedResponse = '';
      } catch (e) {
        // do nothing
      }
    }
  }
}

我们需要最后一个方法来实际显示注释。VS Code 将这些称为 “装饰器”。在 extension.ts 文件中的 parseChatResponse 方法上方添加以下方法。

function applyDecoration(editor: vscode.TextEditor, line: number, suggestion: string) {
  const decorationType = vscode.window.createTextEditorDecorationType({
    after: {
      contentText: ` ${suggestion.substring(0, 25) + '...'}`,
      color: 'grey'
    }
  });

  // get the end of the line with the specified line number
  const lineLength = editor.document.lineAt(line - 1).text.length;
  const range = new vscode.Range(
    new vscode.Position(line - 1, lineLength),
    new vscode.Position(line - 1, lineLength)
  );

  const decoration = { range: range, hoverMessage: suggestion };

  vscode.window.activeTextEditor?.setDecorations(decorationType, [decoration]);
}

此方法接收来自模型的已解析注释,并使用它来创建装饰器。这是通过首先创建 TextEditorDecorationType 来完成的,该类型指定装饰器的外观。在本例中,我们只是添加一个灰色注释,并将其截断为 25 个字符。当用户悬停在消息上时,我们将显示完整消息。

然后,我们设置装饰器应出现的位置。我们需要它位于注释中指定的行号上,并在行尾。

最后,我们在活动文本编辑器上设置装饰器,这会导致注释出现在编辑器中。

如果你的扩展仍在运行,请从调试栏中选择绿色箭头重新启动它。如果你关闭了调试会话,请按 F5 运行扩展。在新打开的 VS Code 窗口实例中打开一个代码文件。当你从命令面板中选择 “Toggle Tutor Annotations” 时,你应该看到代码注释出现在编辑器中。

A code file with annotations from GitHub Copilot

在编辑器标题栏中添加按钮

你可以启用从命令面板以外的其他位置调用命令。在我们的例子中,我们可以向当前标签页的顶部添加一个按钮,允许用户轻松切换注释。

为此,请按如下方式修改 package.json 的 “contributes” 部分

"contributes": {
  "commands": [
    {
      "command": "code-tutor.annotate",
      "title": "Toggle Tutor Annotations",
      "icon": "$(comment)"
    }
  ],
  "menus": {
    "editor/title": [
      {
        "command": "code-tutor.annotate",
        "group": "navigation"
      }
    ]
  }
}

这会导致一个按钮出现在编辑器标题栏的导航区域(右侧)。“icon” 来自 产品图标参考

使用绿色箭头重新启动扩展,或者如果扩展尚未运行,请按 F5。你现在应该看到一个注释图标,它将触发 “Toggle Tutor Annotations” 命令。

A comment icon appears in the title bar of the active tab in VS Code

后续步骤

在本教程中,你学习了如何创建一个 VS Code 扩展,该扩展使用语言模型 API 将 AI 集成到编辑器中。你使用了 VS Code 扩展 API 从当前标签页获取代码,将其与自定义提示一起发送到模型,然后使用装饰器在编辑器中解析和显示模型结果。

接下来,你可以扩展你的代码导师扩展,包括一个聊天参与者,这也将允许用户通过 GitHub Copilot 聊天界面直接与你的扩展进行交互。你还可以探索 VS Code 中的全部 API,以探索构建自定义 AI 编辑器体验的新方法。

你可以在 vscode-extensions-sample 存储库中找到本教程的完整源代码。