Skip to content

[建议 / Feature] PreToolUse hook 支持改写工具输入(以适配 rtk 等命令代理) #44

Open

Description

@wfeng

提交前确认 · Pre-submission checklist

  • 我已搜索过现有 issue,确认这不是重复提议 / I searched existing issues and confirmed this isn't a duplicate.
  • 我已阅读 CONTRIBUTING.md / I've read CONTRIBUTING.md.

问题类别 · Category

工具调用 / MCP · Tool use / MCP

涉及的 Agent 框架 · Agent framework

ZCode Agent(自研)

使用场景 · Use case

痛点

希望能在 ZCode 里集成 rtk(https://github.com/rtk-ai/rtk,一个 CLI 代理,
通过在命令执行前把 git status 这类命令改写为 rtk git status,把冗长输出压缩成
精简版,给 agent 省 60–90% 的 token)。RTK 已适配 Claude Code / Codex / Gemini CLI /
opencode 等 14 个工具,但无法适配 ZCode,原因是 ZCode 的 hook 能力缺了一块。

根本原因(已在 0.14.5 后端实测)

ZCode 的 PreToolUse hook 只支持"观察 + 追加上下文 + 放行/拦截",不支持"改写即将执行的工具输入"。

PreToolUse hook 的输出 schema(zod)实测为:

{
  additionalContext: string,                          // 只能追加文本上下文
  permissionDecision: "allow" | "ask" | "deny",       // 只能放行/审批/拒绝
  permissionDecisionReason: string
}

### 建议方案 · Proposal

为 PreToolUse hook 的输出 schema 增加一个可选字段,允许 hook 返回改写后的工具输入, 例如(对齐 Claude Code 的做法):

```JS
{
  additionalContext: string,
  permissionDecision: "allow" | "ask" | "deny",
  hookSpecificOutput: {
    hookEventName: "PreToolUse",
    // 新增:允许 hook 改写即将执行的工具输入
    toolInput: { /* 改写后的、符合工具 schema 的输入 */ }
  }
}

或任何等价机制,只要 hook 能在命令真正执行前替换其内容即可。

预期价值 · Expected value

No response

你认为的优先级 · Your perceived priority

None

你使用的 ZCode 版本 / 环境 · ZCode version / environment

No response

补充材料 · Additional context

No response

Activity

  1. added
    status: 待评估
    type: 功能建议
    on Jun 24, 2026
  2. github-actions commented on Jun 24, 2026

    @github-actions

    👋 感谢你的反馈,我们已经收到。

    • 维护者会在 3 个工作日内 给出首次响应。
    • 状态保持为 status: 待评估,你可以随时补充信息。
    • 信息不全时我们会打上 needs: 更多信息 标签并 @ 你。

    👋 Thanks — we've received your issue.

    • A maintainer will respond within 3 business days.
    • Status stays at status: 待评估 (Triage); feel free to add context.
    • If we need more details, we'll add needs: 更多信息 and ping you.
  3. panda920 commented on Aug 17, 2026

    @panda920
    Collaborator

    📌 已把重复反馈合并到这条

    以下 issue 反映的是同一个问题,已合并到这里统一跟进:#236

    共同根因:hook 的输入/输出契约不够用:PreToolUse 只能观察、追加上下文和放行/拦截,无法改写即将执行的工具输入(挡住了 rtk 这类命令代理);hook stdin 也没有 parent_session_id,子会话无法归属到父会话。

    独立反馈人数:2

    后续进展只会更新在这条 issue 下,被合并的几条已关闭并打上 duplicate。如果你认为其中某条其实是另一个问题,在那条下面回复,我们会重新拆出来。


    📌 Duplicate reports consolidated here

    The following issues describe the same problem and are now tracked here: #236

    Shared root cause: The hook input/output contract is too narrow: PreToolUse can only observe, append context and allow/deny — it cannot rewrite the tool input about to be executed (which blocks command proxies like rtk) — and hook stdin carries no parent_session_id, so child sessions can't be attributed to their parent.

    Distinct reporters: 2

    Updates will only be posted on this issue; the merged ones are closed and labelled duplicate. If you think one of them is actually a different problem, reply there and we'll split it back out.

  4. panda920 commented on Aug 18, 2026

    @panda920
    Collaborator

    部分已支持,说明一下现状:

    ✅ 改写工具输入 —— 已支持(v3.0 起就有)。PreToolUse hook 在 stdout 里返回 hookSpecificOutput.updatedInput,即可在工具真正执行前替换输入,例如把 git status 改写成 rtk git status:

    {
      "hookSpecificOutput": {
        "hookEventName": "PreToolUse",
        "updatedInput": { "command": "rtk git status" }
      }
    }

    改写后的输入会重新按工具 schema 校验,再进入权限判定与执行。

    ⏳ hook stdin 里的 parent_session_id(合并进来的 #236 诉求)—— 尚未支持,会继续在这里跟进。


    Partially supported — status update:

    ✅ Rewriting tool input — already supported (since v3.0). A PreToolUse hook can return hookSpecificOutput.updatedInput on stdout to replace the input before the tool runs, e.g. turning git status into rtk git status:

    {
      "hookSpecificOutput": {
        "hookEventName": "PreToolUse",
        "updatedInput": { "command": "rtk git status" }
      }
    }

    The rewritten input is re-validated against the tool schema, then goes through the normal permission check and execution.

    ⏳ parent_session_id in hook stdin (the #236 request merged here) — not yet supported; we'll keep tracking it in this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority: P2status: 待评估type: 功能建议

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions