chep/gh-copilot-chat.el
GitHub: chep/gh-copilot-chat.el
一个Emacs插件,让用户在编辑器中与GitHub Copilot聊天以辅助编码和Git操作。
Stars: 401 | Forks: 46
#+TITLE: Emacs 的 Copilot Chat
#+AUTHOR: Cédric Chépied
[[https://melpa.org/#/gh-copilot-chat][https://melpa.org/packages/gh-copilot-chat-badge.svg]]
[[https://stable.melpa.org/#/gh-copilot-chat][https://stable.melpa.org/packages/gh-copilot-chat-badge.svg]]
[[https://github.com/chep/gh-copilot-chat.el/graphs/contributors][https://img.shields.io/github/contributors/chep/gh-copilot-chat.el.svg]]
* 描述
此插件允许您与 GitHub Copilot 进行对话。
[[file:chat.gif]]
欢迎贡献代码、报告问题或讨论新功能。
* 新闻
** 仓库重命名
为了与 ~copilot.el~ 避免冲突, ~copilot-chat~ 已重命名为 ~gh-copilot-chat~。
一切照旧,只是函数和变量名添加了 ~gh-~ 前缀。
MELPA 更新正在进行中。
如有问题,请提交 issue。
** 寻求帮助
非常欢迎错误报告、PR 和新想法。
* 使用技巧
很多人不会阅读这个 README。很多开始阅读的人也不会读太多行。所以我把一些技巧写在这里,放在最前面。
** MCP 服务器
要启用 MCP 服务器,你需要配置 ~mcp~ 包。以下是一个简单的 GitHub 示例:
#+begin_src emacs-lisp
(setopt mcp-hub-servers
'(("github" .
(:command
"docker"
:args
("run"
"-i"
"--rm"
"-e"
"GITHUB_PERSONAL_ACCESS_TOKEN"
"ghcr.io/github/github-mcp-server")
:env
(:GITHUB_PERSONAL_ACCESS_TOKEN
"Your PAT")))))
#+end_src
使用 ~mcp-hub~ 检查你的服务器是否工作。在 ~gh-copilot-chat~ 实例中,使用 ~gh-copilot-chat-set-mcp-servers~ 为该实例启用或禁用服务器。并非所有模型都能使用此功能,请谨慎选择。
** 粘贴
使用 org 前端时,你可以像使用剪切环一样粘贴 Copilot 提供的代码块。这是一个简单的配置:
#+begin_src emacs-lisp
(use-package gh-copilot-chat
…
:bind (:map global-map
("C-c C-y" . gh-copilot-chat-yank)
("C-c M-y" . gh-copilot-chat-yank-pop)
("C-c C-M-y" . (lambda () (interactive) (gh-copilot-chat-yank-pop -1))))
…)
#+end_src
** 查找输入区域
在 gh-copilot-chat 缓冲区中,你可以使用(或绑定到快捷键)交互式 ~gh-copilot-chat-goto-input~ 函数将光标置于提示区域的开头。
** 启动新实例
带前缀参数调用 ~gh-copilot-chat-display~ 可以让你选择实例或创建新实例。
** 保存实例
你可以使用 ~gh-copilot-chat-save~ 和 ~gh-copilot-chat-load~ 来保存/恢复带有历史记录的对话。
* 安装
** MELPA
Copilot-chat 已在 MELPA 上可用:
#+begin_example
M-x package-install RET gh-copilot-chat RET
#+end_example
使用 =use-package= :
#+begin_src emacs-lisp
(use-package gh-copilot-chat)
#+end_src
** Straight
#+begin_src emacs-lisp
(use-package gh-copilot-chat
:straight (:host github :repo "chep/gh-copilot-chat.el" :files ("*.el"))
:after (request org markdown-mode))
#+end_src
** 手动安装
克隆仓库并在 Emacs 中加载文件。
* 配置
** GitHub
你需要一个可以访问 Copilot API 的 GitHub 账户。发送第一个提示词时,你需要进行 GitHub 认证。按照说明操作即可。
** 后端
~gh-copilot-chat-backend~ 只能设置为 ~'curl~。
使用 curl 时,答案会逐 token 写入。可以通过 `gh-copilot-chat-curl-program` 变量设置 curl 路径。
** 前端
有几个可用的前端。你可以通过将 ~gh-copilot-chat-frontend~ 变量设置为 ~'org~(默认)、 ~'markdown~ 或 ~'shell-maker~ 来选择你喜欢的前端。
尝试一下,然后明智地选择。
** Magit 提交信息
你可以使用 Copilot 生成提交信息:
#+begin_src emacs-lisp
(add-hook 'git-commit-setup-hook 'gh-copilot-chat-insert-commit-message)
#+end_src
或者在提交信息缓冲区中手动调用 ~(gh-copilot-chat-insert-commit-message)~。
如果你希望在缓冲区已有提交信息时跳过生成(例如在 amend、rebase 或 squash 期间),请使用不覆盖的变体:
#+begin_src emacs-lisp
(add-hook 'git-commit-setup-hook 'gh-copilot-chat-insert-commit-message-no-clobber)
#+end_src
** 代理
使用变量 ~gh-copilot-chat-curl-proxy~、 ~gh-copilot-chat-curl-proxy-user-pass~ 和 ~gh-copilot-chat-curl-proxy-insecure~ 来设置代理。请参考每个变量的文档了解如何使用它们。
代理尚未经过彻底测试,因此可能存在 bug。HTTPS 代理缺少许多选项。如果你需要额外功能,请提交 issue。
* 用法
** 基本用法
调用 ~(gh-copilot-chat-display)~ (别名 ~(gh-copilot-chat)~)开始聊天。在提示区域输入你的问题,然后按 ~C-c C-c~ 或 ~C-c RET~。
你可能需要进行 GitHub 认证。按照说明操作。
你可以选择要添加到提示词上下文的缓冲区。使用 ~gh-copilot-chat-add-current-buffer~ 和 ~gh-copilot-chat-del-current-buffer~。你也可以使用 ~(gh-copilot-chat-list)~ 管理缓冲区。在列表中,选中的缓冲区将使用继承自 ~font-lock-keyword-face~ 的 ~gh-copilot-chat-list-selected-buffer-face~ 高亮显示。
选中的缓冲区将随每个提示词一起发送,直到你移除它们。
** Transient 菜单
你可以调用 ~(gh-copilot-chat-transient)~ 打开 transient 菜单。几乎所有函数都可在此访问。
** 函数
*** 基本函数
- ~(gh-copilot-chat-display)~ (别名 ~(gh-copilot-chat)~)显示 Copilot 聊天缓冲区。带前缀参数使用时,会提示你选择使用哪个实例。
- ~(gh-copilot-chat-hide)~ 隐藏 Copilot 聊天缓冲区。
- ~(gh-copilot-chat-reset)~ 重置当前实例,包括历史记录、前端和已包含的缓冲区(默认)。使用前缀参数可保留选中的缓冲区。
- ~(gh-copilot-chat-switch-to-buffer)~ 切换到 Copilot Chat 缓冲区,与当前代码编辑缓冲区并排显示。
- ~(gh-copilot-chat-set-model)~ 为当前实例选择要使用的 AI 模型。
- ~(gh-copilot-chat-kill-instance)~ 终止选定的 Copilot 聊天实例并关闭其关联缓冲区。
- ~(gh-copilot-chat-save)~ 将当前实例保存到文件。
- ~(gh-copilot-chat-load)~ 从之前的保存中恢复实例。
*** 提示词相关
- ~gh-copilot-chat-goto-input~ 将光标置于提示区域的开头。
- ~(gh-copilot-chat-custom-mini-buffer)~ 在 minibuffer 中请求提示词并将其发送给 Copilot。
- ~(gh-copilot-chat-custom-prompt-selection)~ 使用提供的提示词或在 minibuffer 中请求提示词,然后附加选区(或整个缓冲区),将结果发送给 Copilot。
- ~(gh-copilot-chat-prompt-history-previous)~ 在提示缓冲区中插入历史记录中的上一个提示词。
- ~(gh-copilot-chat-prompt-history-next)~ 在提示缓冲区中插入历史记录中的下一个提示词。
- ~(gh-copilot-chat-ask-and-insert)~ 请求自定义提示词,并将答案写入当前缓冲区的光标位置。
*** MCP 服务器
- ~(gh-copilot-chat-set-mcp-servers)~ 打开 MCP 服务器菜单。
*** 答案操作
- ~(gh-copilot-chat-yank)~ 在光标处粘贴最后一个代码块(仅限 org 前端)。
- ~(gh-copilot-chat-yank-pop)~ 用前一个代码块替换上次粘贴的内容,或使用参数选择代码块。行为类似于 ~(yank-pop)~(仅限 org 前端)。
- ~(gh-copilot-chat-send-to-buffer)~ 将代码块发送到选定的缓冲区。如有选区则替换之(仅限 org 和 markdown 前端)。
- ~(gh-copilot-chat-copy-code-at-point)~ 将当前代码块复制到剪切环。
*** 缓冲区管理
- ~(gh-copilot-chat-add-current-buffer)~ 将当前缓冲区添加到 Copilot 聊天上下文中。其内容将随每个请求发送。
- ~(gh-copilot-chat-del-current-buffer)~ 移除当前缓冲区。
- ~(gh-copilot-chat-list)~ 打开缓冲区列表。
- ~(gh-copilot-chat-add-workspace)~ 将当前工作区中与实例目录匹配的所有文件添加到上下文中,如果存在则遵循 .gitignore 模式。处理前会清除缓冲区列表。
- ~(gh-copilot-chat-add-buffers)~ 请求缓冲区并将其添加到 Copilot 聊天上下文中。
- ~(gh-copilot-chat-del-buffers)~ 请求缓冲区并从 Copilot 聊天上下文中移除它们。
- ~(gh-copilot-chat-add-buffers-in-current-window)~ 将当前 Emacs 窗口中的所有缓冲区添加到 Copilot 聊天上下文中。
- ~(gh-copilot-chat-add-file)~ 将特定文件添加到 gh-copilot-chat 缓冲区中。
- ~(gh-copilot-chat-add-files-under-dir)~ 将当前目录下与当前文件后缀名相同的所有文件添加到上下文中。如果文件超过 40 个,则拒绝添加并显示警告消息。
*** 代码辅助函数
- ~(gh-copilot-chat-explain-symbol-at-line)~ 请 Copilot 解释光标下的符号。
- ~(gh-copilot-chat-explain)~ 请 Copilot 解释选中的代码。
- ~(gh-copilot-chat-review)~ 请 Copilot 审查选中的代码。
- ~(gh-copilot-chat-doc)~ 请 Copilot 为选中的代码生成文档。
- ~(gh-copilot-chat-fix)~ 请 Copilot 修复选中的代码。
- ~(gh-copilot-chat-optimize)~ 请 Copilot 优化选中的代码。
- ~(gh-copilot-chat-test)~ 请 Copilot 为选中的代码编写测试。
- ~(gh-copilot-chat-explain-defun)~ 请 Copilot 解释光标下的当前函数。
- ~(gh-copilot-chat-custom-prompt-function)~ 请 Copilot 对光标下的函数体应用自定义提示词。例如,指示如何重构该函数。
- ~(gh-copilot-chat-review-whole-buffer)~ 请 Copilot 审查当前整个缓冲区。可用于审查整个类,或审查 magit diff 以查看我的更改或他人的更改。
*** Magit 相关
- ~(gh-copilot-chat-insert-commit-message)~ 在当前缓冲区中插入由 Copilot 生成的提交信息。
- ~(gh-copilot-chat-insert-commit-message-no-clobber)~ 类似于 ~gh-copilot-chat-insert-commit-message~,但如果缓冲区已有非注释信息,则跳过生成。适用于 ~git-commit-setup-hook~,以避免在 amend、rebase 或 squash 期间覆盖现有信息。
- ~(gh-copilot-chat-regenerate-commit-message)~ 重新生成提交信息,可选地提供额外指令或自定义用户提示词。这会重用之前的上下文和历史记录以获得更好的结果。
*** Transient 菜单
- ~(gh-copilot-chat-transient)~ 显示主 transient 菜单以访问所有函数。
- ~(gh-copilot-chat-transient-buffers)~ 显示用于缓冲区管理的 transient 菜单。
- ~(gh-copilot-chat-transient-code)~ 显示用于代码辅助函数的 transient 菜单。
- ~(gh-copilot-chat-transient-magit)~ 显示用于 magit 函数的 transient 菜单。
*** 故障排除
- ~(gh-copilot-chat-quotas)~ 显示 GitHub Copilot 当前的配额。
- ~(gh-copilot-chat-frontend-clean)~ 清理前端。如果你想更换前端时很有用。
- ~(gh-copilot-chat-clear-auth-cache)~ 清除 Copilot Chat 的认证缓存。你将需要重新登录。
- ~(gh-copilot-chat-reset-models)~:重置模型缓存并重新获取模型。当 GitHub 添加新模型或更新模型功能时很有用。
- 如果在 set-model 菜单中看不到你预期的模型,请访问 https://github.com/copilot 并检查该模型是否为你的账户启用。
** 可自定义变量
所有变量都可以使用 ~M-x customize-group RET gh-copilot-chat RET~ 进行自定义。
*** 后端
- ~gh-copilot-chat-backend~ - 用于 API 调用的后端。可以是 ~'curl~。
- ~gh-copilot-chat-curl-program~ - 使用 curl 后端时 curl 可执行文件的路径。
- ~gh-copilot-chat-curl-proxy~ - curl 后端的代理配置。支持 HTTP/HTTPS/SOCKS 协议。
- ~gh-copilot-chat-curl-proxy-insecure~ - 在 curl 后端中跳过代理连接的 SSL 验证。
- ~gh-copilot-chat-curl-proxy-user-pass~ - curl 后端的代理认证凭据。
*** 前端
- ~gh-copilot-chat-frontend~ - 要使用的前端接口。可以是 ~'org~(默认)或 ~'markdown~。
*** 显示
- ~gh-copilot-chat-list-added-buffers-only~ - 默认情况下,缓冲区列表显示所有缓冲区并使用面(face)来高亮已添加的缓冲区。如果设置为 ~t~,则只显示已添加的缓冲区。
- ~gh-copilot-chat-follow~ - 如果为 ~t~(默认为 ~nil~),光标将跟随聊天缓冲区中的答案。
- ~gh-copilot-chat-list-show-path~ - 如果为 ~t~(默认),在缓冲区列表中显示文件路径而不是缓冲区名称。
- ~gh-copilot-chat-list-show-relative-path~ - 如果为 ~t~(默认),在缓冲区列表中仅显示相对路径。
*** 存储与缓存
- ~gh-copilot-chat-default-save-dir~ - 保存聊天的默认目录。
- ~gh-copilot-chat-github-token-file~ - 存储 GitHub 认证令牌的文件路径。
- ~gh-copilot-chat-token-cache~ - 存储会话令牌缓存的文件路径。
*** 模型设置
- ~gh-copilot-chat-default-model~ - 默认使用的 AI 模型。使用 ~(gh-copilot-chat-set-model)~ 为特定实例设置。
- ~gh-copilot-chat-model-ignore-picker~ - 如果为 ~nil~(默认),则在 ~(gh-copilot-chat-set-model)~ 中添加一个过滤器,仅显示具有 ~model_picker_enabled~ 属性的模型。
- ~gh-copilot-chat-models-cache-file~ - 缓存已获取模型的文件路径。
- ~gh-copilot-chat-models-fetch-cooldown~ - 模型获取尝试之间的最小时间间隔(秒)(默认:5 分钟)。
*** 提示词
各种命令使用的默认提示词:
- ~gh-copilot-chat-org-prompt~ - 基础 org 系统提示词,用于配置 Copilot 的行为。
- ~gh-copilot-chat-markdown-prompt~ - 基础 markdown 系统提示词,用于配置 Copilot 的行为。
- ~gh-copilot-chat-prompt-explain~ - 用于“解释”命令的提示词。
- ~gh-copilot-chat-prompt-review~ - 用于“代码审查”命令的提示词。
- ~gh-copilot-chat-prompt-doc~ - 用于“文档生成”命令的提示词。
- ~gh-copilot-chat-prompt-fix~ - 用于“修复”命令的提示词。
- ~gh-copilot-chat-prompt-optimize~ - 用于“优化”命令的提示词。
- ~gh-copilot-chat-prompt-test~ - 用于“测试生成”命令的提示词。
- ~gh-copilot-chat-commit-prompt~ - 用于生成提交信息的提示词。
*** 指令文件
Copilot Chat 支持来自 GitHub 仓库的自定义指令:
- ~gh-copilot-chat-use-copilot-instruction-files~ - 使用来自 ~.github/copilot-instructions.md~ 的自定义指令。默认为 ~t~。
- ~gh-copilot-chat-use-git-commit-instruction-files~ - 使用来自 ~.github/git-commit-instructions.md~ 的自定义 git commit 指令。默认为 ~t~。
- ~gh-copilot-chat-max-instruction-size~ - 指令文件的最大大小(字节)。默认为 65536(64KB)。超过此限制的文件将被忽略。设置为 ~nil~ 表示不限大小。
*** Git
- ~gh-copilot-chat-ignored-commit-files~ - 生成提交信息时要忽略的文件模式列表。
- ~gh-copilot-chat-use-difftastic~ - 是否使用 difftastic 生成差异。*实验性*:difftastic 是为人类审查者设计的;LLM 可能更理解标准的 git diff 输出。因此默认为 ~nil~。如果你想尝试并安装了 ~difft~,可以启用它。
- ~gh-copilot-chat-git-wait-message-format~ - 生成提交信息时显示的消息的格式字符串。
- ~gh-copilot-chat-git-regenerate-wait-message-format~ - 重新生成提交信息时显示的消息的格式字符串。
** 面(Faces)
你可以通过修改以下面来自定义缓冲区列表的外观:
- ~gh-copilot-chat-list-default-face~ - 用于缓冲区列表中未选中缓冲区的面。
- ~gh-copilot-chat-list-selected-buffer-face~ - 用于缓冲区列表中选中缓冲区的面。
** 快捷键绑定
*** 提示缓冲区
- ~C-c C-c~ 发送提示词。答案将写入聊天缓冲区。
- ~C-c RET~ 发送提示词。答案将写入聊天缓冲区。
- ~M-p~ 上一个提示词。
- ~M-n~ 下一个提示词。
- ~C-c C-l~ 打开缓冲区列表。
- ~C-c C-q~ 隐藏缓冲区并删除窗口。
- ~C-c C-t~ 打开 transient 菜单。
*** 缓冲区列表缓冲区
- ~RET~ 选择或取消选择光标处的缓冲区。
- ~space~ 选择或取消选择光标处的缓冲区。
- ~C-c C-c~ 清除缓冲区列表。
- ~g~ 刷新列表。
- ~q~ 隐藏缓冲区并删除窗口。
* 备注
此插件是非官方的,基于 Copilot Chat for neovim 仓库:https://github.com/CopilotC-Nvim/CopilotChat.nvim
用于 git commit 消息的提示词来自 [[https://github.com/ywkim/gpt-commit][gpt-commit]]。
要在 Emacs 中使用 GitHub Copilot 代码补全,请查看 [[https://github.com/copilot-emacs/copilot.el][copilot.el]]。
标签:AI 编程, Docker, Emacs, GitHub Copilot, GitHub 集成, Lisp, MCP, SOC Prime, 人工智能, 代码生成, 代码辅助, 安全防御评估, 工具链, 开发工具, 插件, 渗透测试工具, 用户模式Hook绕过, 编程辅助, 网络调试, 自动化, 配置