AIRI 与 VS Code 深度集成:airi-plugin-vscode 宿主插件与编码上下文桥接机制解析
AIRI 与 VS Code 深度集成airi-plugin-vscode 宿主插件与编码上下文桥接机制解析【免费下载链接】airi Self hosted, you-owned Grok Companion, a container of souls of waifu, cyber livings to bring them into our worlds, wishing to achieve Neuro-samas altitude. Capable of realtime voice chat, Minecraft, Factorio playing. Web / macOS / Windows supported.项目地址: https://gitcode.com/GitHub_Trending/ai/airi导读本文基于仓库 integrations/vscode/airi-plugin-vscode/README.md 展开系统讲解 AIRI 的 VS Code 集成方案以 host 侧插件proj-airi/airi-plugin-vscode为桥接入口将 VS Code 中正在编辑的代码上下文实时回流给 AIRI 的 Server Channel为 AI 伙伴理解开发者工作现场提供上下文通道。读完本文你将掌握 AIRI 与 VS Code 之间的双层插件架构、上下文采集与context:update协议细节、扩展的配置项与命令用法以及当前实现状态与后续演进方向。AIRI 的 VS Code 集成两个包、一座桥integrations/vscode/目录下并存两个职责互补的组件共同构成 VS Code 上下文 → AIRI 的完整链路组件角色一句话职责airi-plugin-vscodeHost 侧插件运行在 AIRI 内部Host-side plugin that bridges VS Code context and exposes UI contributions in AIRI.——桥接 VS Code 上下文并在 AIRI 中暴露 UI 贡献点vscode-airiVS Code 官方扩展运行在编辑器侧streaming your current working at stuff back to AIRI——把当前正在处理的工作内容流式回传给 AIRI指定文档对airi-plugin-vscode的定义只有一句话但这句话点出了整个集成的两个关键能力维度bridges VS Code context把 VS Code 中的文件路径、光标位置、选区、光标上下文行等编码现场信息桥接到 AIRI 侧exposes UI contributions in AIRI作为 host 插件未来将在 AIRI 内贡献 UI 元素如展示 VS Code 状态的视图。其中第 1 点已由仓库内的 vscode-airi 扩展完整落地实现本文将以该实现作为源码级佐证第 2 点当前处于脚手架阶段见下文src/index.ts的 TODO属于规划中的能力。host 侧插件 airi-plugin-vscode 的包结构与生命周期包元信息与构建配置package.json 展示了该插件的包定义包名proj-airi/airi-plugin-vscode当前版本0.12.0-beta.5标记为private仅工作区内部使用以 ESM 形式对外暴露exports[.]指向./dist/index.mjs类型声明指向./dist/index.d.mtsmain与types字段同步声明唯一开发依赖为工作区包proj-airi/plugin-sdkworkspace:*即 AIRI 的 插件 SDK用于承载 host 插件的运行协议脚本仅保留buildtsdown与typechecktsc --noEmitdev直接复用build。tsdown.config.ts 的构建配置非常精简import { defineConfig } from tsdown export default defineConfig({ entry: [src/index.ts], dts: true, format: esm, })即以src/index.ts为唯一入口产出 ESM 格式并生成.d.ts类型声明——这与 package.json 中的exports配置一一对应。生命周期脚手架activate / deactivatesrc/index.ts 目前是完整的生命周期骨架export function activate() { // TODO: bind to vscode-airi bridge over control plane } export function deactivate() { // TODO: clean up subscriptions }从源码结构可以推断该插件采用与 VS Code 扩展一致的activate/deactivate生命周期约定但内部逻辑尚未填充——activate中预留了 通过 control plane 绑定 vscode-airi 桥接 的 TODOdeactivate预留了清理订阅的 TODO。也就是说当前仓库中 host 侧桥接逻辑是规划态而非完成态真正可运行的上下文回流能力位于编辑器侧的 vscode-airi 扩展中。这是阅读与二次开发时需要特别留意的事实边界。编辑器侧 vscode-airi上下文桥接的实际实现extension.ts 是 vscode-airi 扩展的入口完整实现了 bridges VS Code context 这一能力。其激活流程可拆解为读取配置 → DI 容器装配 → 连接 AIRI Server Channel → 注册命令与事件监听。激活流程与配置读取export async function activate(context: vscode.ExtensionContext) { initLogger(LoggerLevel.Debug, LoggerFormat.Pretty) const config workspace.getConfiguration(airi-vscode) const isEnabled config.getboolean(enabled, true) const contextLines config.getnumber(contextLines, 5) const sendInterval config.getnumber(sendInterval, 3000) const vscodeContext injeca.provide(vscode:context, () context) const client injeca.provide(proj-airi:client, () new Client()) const contextCollector injeca.provide(self:context-collector, () new ContextCollector(contextLines)) // ...其余 provide 装配 await injeca.start() }要点扩展使用 injecaguiiai/injeca风格的轻量 DI 容器进行依赖装配通过provide注册vscode:context、proj-airi:client、self:context-collector、self:event-listeners、self:control-loop:interval:send等键最后由injeca.invokeinjeca.start()触发setup日志系统采用guiiai/logg初始化级别为LoggerLevel.Debug、格式为LoggerFormat.Pretty三组配置项在激活时即读取详见下文配置参数速查小节。连接 AIRI Server Channelairi.ts 中的Client封装了与 AIRI Server Channel 的通信async connect(): Promiseboolean { try { this.client new ServerClientEvents({ name: proj-airi:plugin-vscode }) await this.client.connect() useLogger().log(AIRI connected to Server Channel) return true } catch (error) { useLogger().errorWithError(Failed to connect to AIRI Server Channel:, error) return false } }实现事实Client内部包装了来自proj-airi/server-sdk工作区对应 packages/server-sdk的ServerClient并以proj-airi:plugin-vscode作为连接标识setup阶段根据isEnabled尝试连接连接成功弹出AIRI Server Channel connected!信息失败则弹出警告每次send前会再次调用client.connect()兜底保证未连接或连接中断时可自动重连isConnected()用于查询当前连接状态。上下文采集ContextCollector 的六大信息维度context-collector.ts 中的ContextCollector负责把当前活动编辑器翻译成结构化上下文。其构造函数接收contextLines默认 5即光标前后各取多少行作为上下文窗口。collect(editor)一次采集返回一个CodingContext对象类型定义见 types.ts包含以下维度字段内容来源file文件路径fsPath、语言 IDlanguageId、文件名fileName、所属工作区workspaceFolder当前文档的 URI 与工作区解析cursor光标所在行line与列charactereditor.selection.activeselection选中文本及起止行列无选区时为undefinededitor.selectioncurrentLine当前行行号与文本document.lineAt(position.line)context光标前before、后after各contextLines行的文本数组getContext(document, position.line)git分支名branch与工作区是否脏isDirtyVS Code 内置 Git 扩展的 API 1timestamp采集时间戳Date.now()getContext的实现要点context-collector.tsprivate getContext(document: vscode.TextDocument, currentLine: number) { const before: string[] [] const after: string[] [] const startLine Math.max(0, currentLine - this.contextLines) for (let i startLine; i currentLine; i) before.push(document.lineAt(i).text) const endLine Math.min(document.lineCount - 1, currentLine this.contextLines) for (let i currentLine 1; i endLine; i) after.push(document.lineAt(i).text) return { before, after } }边界处理before从max(0, currentLine - contextLines)开始after截止到min(lineCount - 1, currentLine contextLines)保证在文件首尾不会越界。Git 信息通过vscode.extensions.getExtension(vscode.git)?.exports.getAPI(1)获取仓库状态取repo.state.HEAD?.name作为分支名HEAD 不存在时为unknown以repo.state.workingTreeChanges.length 0判断工作区是否脏任何异常都会被吞掉并返回undefined源码注释标注为 simplified, can be extended later。三种上下文推送时机事件驱动 定时轮询上下文并非只推一次而是由三种触发时机协同维持最新现场extension.ts1. 保存文件时workspace.onDidSaveTextDocument仅在window.activeTextEditor与保存的文档一致时触发采集上下文并通过client.replaceContext推送消息前缀为User saved the file: {fileName} (located at {path}). Here is the context around the cursor after saving:2. 切换编辑器时window.onDidChangeActiveTextEditor每当活动编辑器切换推送新的文件与光标上下文User switched to file: {fileName} (located at {path}). Here is the context around the cursor after switching:3. 定时监控轮询sendInterval控制若sendInterval 0启动一个setInterval控制循环周期性地采集并推送当前活动编辑器上下文User opened file is: {fileName} (located at {path}), and current cursor is at line {line}, character {character}. Here is the context around the cursor:所有推送文本均按before 行当前行after 行拼接把原始行文本组织成语义清晰的上下文块。监听器统一存入eventListeners数组unregisterListeners负责dispose()全部监听并停止轮询保证enable/disable/deactivate的幂等清理。上下文协议context:update 与 ContextUpdateStrategy上下文如何说给 AIRI答案在 airi.ts 的两个方法中async replaceContext(context: string): Promisevoid { const id nanoid() this.send({ type: context:update, data: { strategy: ContextUpdateStrategy.ReplaceSelf, text: context, id, contextId: id, }, }) } async appendContext(context: string): Promisevoid { const id nanoid() this.send({ type: context:update, data: { strategy: ContextUpdateStrategy.AppendSelf, text: context, id, contextId: id, }, }) }协议要点消息类型统一为context:update由proj-airi/server-sdk的ContextUpdateStrategy枚举约束更新策略ReplaceSelf表示替换自身上下文当前 vscode-airi 的主路径保证 AIRI 侧始终只有最新现场AppendSelf表示追加自身上下文每条消息通过nanoid()生成唯一id与contextId用于服务端定位上下文片段。从源码结构看context:update消息经由WebSocketEventOptionalSourceEvents类型的 WS 通道发出事件类型Events定义于 types.ts与 packages/server-sdk 的通道协议对齐。配置参数速查与命令一览配置项VS Code settings 键名前缀airi-vscode配置键默认值作用airi-vscode.enabledtrue扩展激活时是否自动连接 Server Channel 并注册监听airi-vscode.contextLines5光标前后各采集多少行作为上下文窗口注入ContextCollector构造参数airi-vscode.sendInterval3000毫秒定时轮询推送间隔设为0可关闭轮询仅保留保存/切换事件命令Command Palette 中执行命令行为airi-vscode.enable置isEnabled true连接 Server Channel注册事件监听与轮询提示AIRI enabled!airi-vscode.disable置isEnabled false注销监听与轮询client.disconnect()提示AIRI disabled!airi-vscode.status根据isEnabled client显示Connected或Disconnected状态信息扩展停用时deactivate通过 DI 容器injeca.resolve取出client、eventListeners、controlLoopInterval统一注销监听、停止轮询并断开连接extension.ts。现状边界与演进方向基于仓库内可核实的代码需要明确以下事实边界已实现vscode-airi 扩展的 Server Channel 连接、上下文采集、三种推送时机、enable/disable/status 命令与配置项这是 bridges VS Code context 的落地形态。规划态host 侧插件 airi-plugin-vscode/src/index.ts 仅含activate/deactivate骨架与 TODObind to vscode-airi bridge over control plane、clean up subscriptionsexposes UI contributions in AIRI 的 UI 贡献能力尚未实现——从源码结构可以推断其意图是通过 control plane 与 vscode-airi 建立桥接但具体协议与 UI 形态有待后续版本落地。扩展空间ContextCollector中 Git 信息被注释为 simplified, can be extended later选区selection字段已采集但当前三种推送消息中尚未使用均是可继续深化的点位。相关资源索引指定文档integrations/vscode/airi-plugin-vscode/README.mdhost 侧插件入口integrations/vscode/airi-plugin-vscode/src/index.tshost 侧插件构建integrations/vscode/airi-plugin-vscode/tsdown.config.ts、integrations/vscode/airi-plugin-vscode/package.json编辑器侧扩展文档integrations/vscode/vscode-airi/README.md扩展入口与事件/轮询实现integrations/vscode/vscode-airi/src/extension.ts上下文采集器integrations/vscode/vscode-airi/src/context-collector.tsServer Channel 客户端封装integrations/vscode/vscode-airi/src/airi.ts事件与上下文类型定义integrations/vscode/vscode-airi/src/types.ts依赖的协议包packages/server-sdk/README.md、packages/plugin-sdk/README.md【免费下载链接】airi Self hosted, you-owned Grok Companion, a container of souls of waifu, cyber livings to bring them into our worlds, wishing to achieve Neuro-samas altitude. Capable of realtime voice chat, Minecraft, Factorio playing. Web / macOS / Windows supported.项目地址: https://gitcode.com/GitHub_Trending/ai/airi创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
上一篇/下一篇内容由系统自动关联
返回资讯列表 →