用途:为开发 VS Code 聊天扩展提供 Chat API 模式参考,覆盖从注册参与者到响应流式输出的常见写法。
主要能力:在 package.json 的 contributes.chatParticipants 中声明参与者(id、name、fullName、description、isSticky、commands),并在 activate() 中用 vscode.chat.createChatParticipant 创建实例、设置 iconPath。处理器签名为 vscode.ChatRequestHandler,接收 request、context、stream、token。
响应操作:stream.markdown() 输出文本(支持 CommonMark 与代码块)、stream.progress() 显示进度、stream.button() 添加按钮、stream.filetree() 展示文件树、stream.reference() 与 stream.anchor() 添加引用和锚点;可通过 MarkdownString 的 command: 链接触发命令,并设置 isTrusted.enabledCommands。
上下文与模型:context.history 访问历史,request.references 获取引用,request.model 获取模型,request.command 检查命令,request.location 获取聊天位置。LM 集成使用 vscode.lm.selectChatModels 选择模型并 sendRequest 流式读取。工具调用推荐 @vscode/chat-extension-utils 的 sendChatParticipantRequest,工具注册用 vscode.lm.registerTool。
其他模式:disambiguation 实现自动路由,followupProvider 提供后续建议,context.history 过滤 ChatRequestTurn 获取历史请求。
最佳实践:增量流式响应、处理取消令牌、捕获错误、长操作显示进度、每个扩展限制一个参与者、高成本操作先征得同意。
限制:资料明确提示 Chat API 随 VS Code 版本演进,最后验证为 2026 年 2 月(VS Code 1.108+),使用前应核对 Chat API、LM API、Tools API 的当前状态。