构建您自己的斜杠命令
注意
目前只能使用 config.json
或 config.ts
添加斜杠命令。新的首选格式是 YAML Config Format
。我们建议查看 提示文件 (Prompt Files) 以实现类似功能。
有两种方法可以添加自定义斜杠命令
- 使用自然语言提示 - 这更简单,只需要编写字符串或字符串模板。
- 使用自定义函数 - 这使您可以完全访问 Continue SDK,并允许您编写任意 Typescript 代码。
“自定义命令”(使用自然语言)
您可以通过在 config.json
中添加 customCommands
属性来添加自定义斜杠命令。
name
:命令的名称,将使用/name
调用description
:命令的简短描述,将显示在下拉菜单中prompt
:发送给 LLM 的模板化提示
当您频繁重用某个提示时,自定义命令非常有用。例如,如果您精心制作了一个很棒的提示,并经常要求 LLM 检查代码中的错误,您可以添加如下命令
config.json
{
"customCommands": [
{
"name": "check",
"description": "Check for mistakes in my code",
"prompt": "{{{ input }}}\n\nPlease read the highlighted code and check for any mistakes. You should look for the following, and be extremely vigilant:\n- Syntax errors\n- Logic errors\n- Security vulnerabilities\n- Performance issues\n- Anything else that looks wrong\n\nOnce you find an error, please explain it as clearly as possible, but without using extra words. For example, instead of saying 'I think there is a syntax error on line 5', you should say 'Syntax error on line 5'. Give your answer as one bullet point per mistake found."
}
]
}
模板化
prompt
属性支持使用 Handlebars 语法进行模板化。您可以使用以下变量
input
(在上面的示例中使用):与斜杠命令一起输入的任何附加输入。例如,如果您输入/test only write one test
,input
将是only write one test
。这还将包括高亮显示的代码块。- 文件名:您可以通过提供绝对路径或相对于当前工作目录的路径来引用任何文件。
自定义斜杠命令
如果您想在编写自然语言自定义命令的基础上更进一步,您可以编写一个返回响应的自定义函数。这需要使用 config.ts
而不是 config.json
。
为此,将一个新的 SlashCommand
对象添加到 slashCommands
列表中。此对象包含“name”,即您将键入以调用斜杠命令的名称,“description”,即在下拉菜单中看到的描述,以及“run”。run
函数是任何异步生成器,它应该根据您希望字符串流式传输到 UI 的方式来产生字符串。作为函数的一个参数,您可以访问一个 ContinueSDK
对象,该对象提供了各种实用工具,例如访问 IDE 中的特定信息/操作、当前语言模型以及一些其他实用工具。例如,这是一个生成提交消息的斜杠命令
~/.continue/config.ts
export function modifyConfig(config: Config): Config {
config.slashCommands?.push({
name: "commit",
description: "Write a commit message",
run: async function* (sdk) {
// The getDiff function takes a boolean parameter that indicates whether
// to include unstaged changes in the diff or not.
const diff = await sdk.ide.getDiff(false); // Pass false to exclude unstaged changes
for await (const message of sdk.llm.streamComplete(
`${diff}\n\nWrite a commit message for the above changes. Use no more than 20 tokens to give a brief description in the imperative mood (e.g. 'Add feature' not 'Added feature'):`,
new AbortController().signal,
{
maxTokens: 20,
},
)) {
yield message;
}
},
});
return config;
}