如何为Continue.dev自定义斜杠命令传递参数以生成带任务ID的Git提交信息
如何为Continue.dev自定义斜杠命令传递参数以生成带任务ID的Git提交信息
我来帮你搞定这个问题!要让你的Continue.dev斜杠命令支持传递任务ID(比如FA-92),同时解决那个TypeScript报错,只需要调整命令定义的几个小地方,很简单的:
核心问题分析
你之前直接给run函数加第二个参数导致TypeScript报错,是因为Continue的SDK里,斜杠命令的run函数类型只接受sdk这一个参数,参数需要从sdk对象内部获取,不能额外加参数。
解决方案步骤
- 从SDK中获取传入的参数:通过
sdk.args可以拿到用户调用命令时传入的所有参数,我们取第一个参数作为任务ID即可。 - 调整Prompt模板:把任务ID融入到给LLM的提示里,让生成的提交信息自动包含这个ID。
- 兼容无参数的情况:保留原来的功能,没传参数时也能正常生成提交信息。
修改后的完整代码
export function modifyConfig(config: Config): Config { config.slashCommands?.push({ name: "commit_message_any_changes", description: "Write a commit message for all local changes (optional: pass a ticket ID like FA-92)", run: async function* (sdk) { // 获取第一个传入的参数作为任务ID,没传的话留空 const ticketId = sdk.args?.[0] || ""; const diff = await sdk.ide.getDiff(true); // 构建提示词,根据是否有任务ID调整格式要求 let prompt = `${diff}\n\nWrite a commit message for the above changes. Write it all in one line and make it very concise and adhere to the following style: `; if (ticketId) { prompt += `git commit -m "[${ticketId} | Categories] Affected files or feature: more details of the change."`; } else { prompt += `git commit -m "[Categories] Affected files or feature: more details of the change."`; } prompt += `\n Categories should be similar or one of these: [Cleanup|Documentation|Refactoring|UI|Navigation|Content Update|Functionality|New Feature|BugFix]. Ideally, it should only be one category, but if the changes are bigger, a max of 2 categories can be used.\n\nIf there are no changes, write "No changes" and stop.`; const controller = new AbortController(); for await (const message of sdk.llm.streamComplete(prompt, controller.signal, { maxTokens: 50 })) { yield message; } }, }); return config; }
怎么用
现在你可以两种方式调用这个命令:
- 不带参数:
/commit_message_any_changes(和原来一样正常工作) - 带任务ID:
/commit_message_any_changes FA-92,生成的提交信息就会自动包含[FA-92 | ...]的格式啦。
额外说明
如果用户传了多个参数,你也可以通过sdk.args数组的其他元素获取,但根据你的需求,只取第一个参数就足够了。这样修改后,既解决了类型报错,又完美实现了你想要的功能~
备注:内容来源于stack exchange,提问作者Patrick Michiels
相关产品推荐
相关产品推荐

