dsh-apply-patch:为 DSH 模型注入 Codex 风格的 apply_patch 工具

前言

GPT 系列模型在训练时大量使用 OpenAI Codex 的 apply_patch 工具。当在 DSH 中使用这些模型时,模型会本能地尝试调用该工具,但 DSH 默认不提供该工具,导致报错。本插件通过 DSH 强大的插件系统补上这个工具,并且完全走 DSH 自带的沙盒系统,绝不绕过沙盒直接修改文件。

核心功能

该插件为 DSH 模型注入了 apply_patch 工具,具备以下特性:

  • 工具定义:与 OpenAI Codex 完全一致的补丁格式(*** Begin Patch / *** Update File: / *** Add File: / *** Delete File: / *** End of File / *** End Patch),并支持 gpt-4.1 风格 heredoc(<<'EOF' ... EOF)的宽松解析。
  • 沙盒写入:所有文件写入都通过 ctx.fs(DSH 沙盒文件系统)执行,删除通过沙盒 bash 执行。沙盒拒绝时返回 [sandbox: ...] 错误,绝不绕过。
  • 按模型条件注入:设置里的配置卡片只有一个下拉选择:关闭、仅 GPT 模型(默认)、所有模型。
  • 设置卡片:设置 → 插件 → 插件配置 → Apply Patch,实时生效。

安装

通过 dsh plugin 命令安装到目标 profile(默认 web):

dsh plugin --profile web add @fonlan/dsh-apply-patch

也可以从 GitHub 安装(支持默认分支或指定 tag):

# 默认分支
dsh plugin --profile web add github:fonlan/dsh-apply-patch

# 指定 release tag
dsh plugin --profile web add github:fonlan/dsh-apply-patch#v0.1.0

从本地源码安装(开发用途):

pnpm build
dsh plugin --profile web add .

注意:安装后需要重启 dsh web 进程才能生效。

典型用法

模型(尤其是 GPT 系列)会像在 Codex 中一样直接调用 apply_patch。工具返回 codex 风格的摘要:

*** Begin Patch
*** Update File: hello.py
@@
-print("hello")
+print("hello, world")
*** Add File: new.py
+x = 1
*** Delete File: old.py
*** End Patch

执行后返回:

Success. Updated the following files:
M /path/to/hello.py
A /path/to/new.py
D /path/to/old.py

沙盒说明

所有变更都通过 DSH 内置沙盒执行,具体策略如下:

操作 沙盒路径 说明
Add / Update / Move 写入 ctx.fs.writeText 遵守会话 sandbox mode 与 workspace root
Delete / Move 删除原文件 ctx.shell 遵守同一策略
  • read-only 模式下任何写入都会被沙盒拒绝,返回 [sandbox: file access denied under read-only mode]。
  • workspace-write 模式下只允许写入 workspace root(以及沙盒策略允许的 /tmp)。
  • 观察策略要求先读后写,工具内部自动完成 stat/read/observe/write 流程。

配置

在 DSH 界面中进入设置 → 插件 → 插件配置 → Apply Patch,即可配置注入策略:
* 关闭:完全不注入 apply_patch。
* 仅 GPT 模型(默认):只对模型 id 以 gpt- 开头的模型注入。
* 所有模型:对所有模型注入。

配置更改通过设置卡片实时生效,无需重启进程。

总结

dsh-apply-patch 插件解决了 GPT 系列模型在 DSH 环境下因工具缺失导致的报错问题。它不仅提供了与 Codex 一致的补丁格式,还通过 DSH 原生的沙盒机制保证了操作的安全性。适合需要在 DSH 中使用 GPT 模型进行文件操作的智能体开发者。

插件源码及文档:https://github.com/fonlan/dsh-apply-patch

羽毛球分组比赛记分
小程序二维码

欢迎使用《羽毛球分组比赛记分》微信小程序

小夜