读见

「Claude Code with mods in TypeScript | Claude by Anthropic」

原题Customize Claude Code with mods in TypeScript | Claude by Anthropic

约 3 分钟约 737 字原文英文已翻译

推荐理由Anthropic 推出 Claude Code mods:小型 TypeScript 函数挂钩工具调用、权限请求、界面渲染等事件,可在事件前后或代替事件运行,实现重写 prompt、拦截重试工具调用、对输出脱敏,甚至替换内置功能、增改 UI,部分内置功能本身已改由 mods 交付。它不跑沙箱、拥有与 Claude Code 相同的机器权限,只应安装可信来源。让开发者直接重塑智能体的工作方式,这是工具走向「属于你」的又一步。

读前速览

5 个要点

Anthropic 推出 mods——即挂钩到 Claude Code 事件的小型 TypeScript 函数,用于重写提示词、修改 UI 或替换内置功能——这些 mods 随插件一起发布,并附带企业级控制措施,如 sec-default。

  1. mods 是挂钩到 Claude Code 事件的 TypeScript 函数,在事件之前、之后、取而代之或环绕它们运行。

    Claude Code 每采取一次行动都会释放一个事件——调用工具、请求权限、绘制屏幕。mod 可以在提示词到达模型之前重写它们,阻止或重试工具调用,批准或拒绝权限,并在 Claude 读取之前从工具输出中清除机密信息。

  2. mods 可以修改界面,而不仅仅是行为,并且多个 mods 按加载顺序堆叠。

    mod 可以编辑或替换界面部分,如工具结果或 Claude 的提问,并添加其他 mods 可以响应的按钮和输入。当几个 mods 钩取同一个事件时,最先加载的 mod 首先看到事件并最后看到结果,从而实现跨作者的组合。

  3. mods 随插件发布,并继承现有的插件分发和企业级控制措施。

    它们通过 Claude 目录或 /plugin 安装和共享,就像任何插件一样。管理员可以允许或阻止市场,从 Team 和 Enterprise 计划上的内置 sec-default mod 优先加载,以阻止用户安装的 mods 执行高风险操作,如覆盖权限拒绝规则。

  4. 内置功能正在迁移到 mods,让用户可以将 Claude Code 精简到核心功能。

    内置的 /diff 功能现在是一个 mod,可以在 /plugin 中关闭或替换为自定义版本。Anthropic 计划随着时间推移将更多内置功能迁移到 mods,以便用户仅添加他们想要的功能。

  5. mods 以与 Claude Code 相同的机器访问权限运行,因此对来源的信任至关重要。

    Anthropic 明确警告 mods 不受沙盒保护,应该仅从受信任的来源安装,就像对待计算机上的任何代码一样。

完整摘要

Anthropic 宣布推出 Claude Code 的 mods:小型 TypeScript 函数挂钩到操作过程中释放的事件(工具调用、权限请求、UI 渲染)之前、之后、取而代之或环绕它们运行。单个 mod 可以在提示词到达模型之前重写它们,阻止或重试工具调用,批准或拒绝权限,从工具输出中清除机密信息,并通过按钮和输入编辑或扩展界面。mods 随插件一起发布,因此它们的安装和共享方式与任何插件相同,并且它们以与 Claude Code 本身相同的非沙盒机器访问权限运行。钩取同一事件的多个 mods 按加载顺序依次执行,允许跨作者堆叠。一些内置功能(如 /diff)现在也作为 mods 发布,可以被禁用或替换,后续还有更多功能计划这样做。对于团队和企业,现有的插件控制措施适用,且一个内置的 sec-default mod 优先加载,以阻止用户安装的 mods 除非管理员覆盖它。mods 今天已在 CLI 和桌面应用中提供。

目录 · 5 节
查看原文

今天我们将介绍 mods:一些能够改变 Claude Code 工作方式的小型 TypeScript 函数。mod 可以重写提示词、添加新的 UI、替换内置功能,或完全添加新的功能。你可以自己编写 mod,也可以让 Claude Code 帮你编写。Mods 包含在插件中,因此你可以像安装和分享任何插件一样安装和分享它们。它们可在 Claude Code CLI 和桌面应用中运行。Today we're introducing mods, small TypeScript functions that change how Claude Code works. A mod can rewrite a prompt, add new UI, replace a built-in feature, or add entirely new functionality. You can write a mod yourself, or ask Claude Code to write one for you. Mods ship inside plugins, so you install and share them like any plugin. They work in the Claude Code CLI and desktop app.

Mods 对你机器的访问权限与 Claude Code 本身相同。它们没有经过沙盒隔离,因此你只应从信任的来源安装 mods,就像在电脑上安装任何代码一样。Mods run with the same access to your machine as Claude Code itself. They aren’t sandboxed, and you should only install mods from sources you trust, the same way you'd install any code on your computer.

要了解模组能实现什么,请阅读我们的《构建你的第一个模组》指南。To see what mods can do, read our guide to building your first mod.

我们为何构建模组Why we built mods

开发者们希望在无需等待我们推出功能的情况下,更好地控制 Claude Code 的工作方式。Hooks 帮助用户获得了部分控制权,但 Hooks 无法重写事件、绘制新的 UI 或替代现有功能。而 Mods 可以。Developers have asked for more control over how Claude Code works, without waiting for us to ship a feature. Hooks helped give users some of this control, but hooks can't rewrite events, draw new UI, or replace features. Mods can.

我们希望 Claude Code 用起来像是为你量身打造的,因此你可以按照自己的工作方式对其进行定制。我们在发布前就在 GitHub 上分享了 mods 的设计,以听取开发者的反馈。感谢每一位提出意见的人。We want Claude Code to feel like yours, so you can shape it to the way you work. We shared the design for mods on GitHub before launch to get feedback from developers. Thank you to everyone who weighed in.

模组的工作原理How mods work

Claude Code 每次执行某项操作时,都会发出一个事件。示例包括调用工具、请求权限以及绘制屏幕的一部分。mod 是一个挂钩到这些事件之一的函数。mod 可以在事件之前、之后或代替事件运行。它还可以包装事件,在前后都运行代码。通过一个函数,mod 可以:Each time Claude Code does something, it emits an event. Examples include calling a tool, asking for permission, and drawing part of the screen. A mod is a function that hooks into one of these events. A mod can run before the event, after it, or instead of it. It can also wrap the event, running code both before and after. With one function, a mod can:

  • 在提示词到达模型之前重写它。
    Rewrite a prompt before it reaches the model.
  • 拦截、重写或重试工具调用。
    Block, rewrite, or retry a tool call.
  • 批准或拒绝权限请求。
    Approve or deny a permission request.
  • 在 Claude 读取工具输出之前,先从中删去机密信息。
    Redact secrets from tool output before Claude reads it.

模组还可以改变你所看到的内容。它可以编辑或替换 Claude Code 绘制的界面的一部分,例如工具结果或 Claude 提出的问题。它可以添加按钮和输入框,其他模组则可以在你按下这些按钮时做出响应。目前,模组可以针对终端、桌面应用,或同时针对两者。A mod can also change what you see. It can edit or replace part of the interface Claude Code draws, like a tool result or a question from Claude. It can add buttons and inputs, and other mods can respond when you press them. Today, a mod can target the terminal, the desktop app, or both.

当多个模组挂钩同一个事件时,它们会按照加载顺序运行。最先加载的模组最先看到该事件,最后获得结果。这样一来,你就可以叠加来自不同作者的模组。When several mods hook the same event, they run in the order they load. The first mod to load sees the event first and the result last. This lets you stack mods from different authors.

你还可以使用 Claude Code 来为 Claude Code 制作模组。让 Claude 创建一个模组,它就能编写 TypeScript、安装模组,并在你的会话中热重载该模组。You can also use Claude Code to mod Claude Code. Ask Claude to create a mod, and it can write the TypeScript, install it, and hot reload it in your session.

用你自己的功能替换内置功能Swap built-in features for your own

Claude Code 的一些内置功能现在以模块形式提供。例如,内置的 /diff 功能现在就是一个模块,因此你可以将其关闭(在 /plugin 中),或用自己的版本替换它。我们计划随着时间的推移将更多内置功能移至模块中,这样你就可以将 Claude Code 精简为一个小型核心,并只添加你想要的功能。Some built-in features of Claude Code now ship as mods. For example, the built-in /diff feature is now a mod, so you can turn it off (in /plugin) or replace it with your own version. We plan to move more built-in features to mods over time, so you can pare Claude Code down to a small core and add back only what you want.

面向团队和企业的 ModsMods for teams and enterprises

Mods 随插件一起提供,因此您现有的插件控制措施同样适用。管理员可以允许或阻止插件市场。在 Team 和 Enterprise 方案中,所有者可在管理控制台中进行设置。在 Claude API 和第三方 API 方案中,管理员会将托管设置推送到用户的设备。Mods ship inside plugins, so your existing plugin controls apply. Admins can allow or block plugin marketplaces. On Team and Enterprise plans, an owner sets this in the admin console. On Claude API and third-party API plans, admins push managed settings to users' machines.

在 Team 和 Enterprise 方案中,以及任何具有托管设置的机器上,一个名为 sec-default(“安全默认设置”)的内置 mod 会首先加载。它会阻止用户安装的 mod 执行高风险操作,例如覆盖你的权限拒绝规则。你可以查看源代码,了解它限制了哪些操作。管理员也可以改为先加载自己的 mod。如果这样做,请将 sec-default 添加到你的列表中,以保留其限制。On Team and Enterprise plans, and on any machine with managed settings, a built-in mod called sec-default (“security default”) loads first. It stops mods that users install from doing risky things, like overriding your permission deny rules. You can view the source code to see what it restricts. Admins can load their own mods first instead. If you do, add sec-default to your list to keep its restrictions.

团队还可以使用模组来构建自己的控件和功能。例如:Teams can also use mods to build their own controls and functionality. For example:

  • CI/CD 状态:一个 mod 可以在对话旁边的窗格中显示你的流水线状态,并随着构建成功或失败而更新。
    CI/CD status: a mod can show your pipeline’s status in a pane beside the conversation, and update it as builds pass or fail.
  • 生产环境安全措施:mod 可以要求在任何命令修改生产环境配置之前进行确认。
    Production safeguards: a mod can require confirmation before any command touches production config.
  • 审计日志:最先加载的模组可以记录其他每个模组发出的所有调用。
    Audit logging: a mod that loads first can record every call that every other mod makes.

入门Getting started

Mods 今日已在 Claude Code CLI 和桌面应用中推出。你可以从 Claude 目录安装包含 mods 的插件,也可以在 CLI 中运行 /plugin。要分享 mod,请将其打包到插件中并提交到目录。Mods are available today in the Claude Code CLI and desktop app. Install plugins that include mods from the Claude directory or by running /plugin in the CLI. To share a mod, package it in a plugin and submit it to the directory. 

要构建你自己的模组,请阅读我们的入门指南或查看文档。To build your own mods, read our getting started guide or see the documentation.

值得记下

mods 与 Claude Code 本身具有完全相同的机器访问权限。它们不受沙盒保护,你应该仅从你信任的来源安装 mods,就像安装计算机上的任何代码一样。
mod 可以在事件之前运行、之后运行,或取而代之运行。它还可以包装事件,在之前和之后都运行代码。
我们计划随着时间推移将更多内置功能迁移到 mods,以便你可以将 Claude Code 精简到核心功能并仅添加你想要的功能。
你还可以使用 Claude Code 来为 Claude Code 创建 mod。向 Claude 请求创建一个 mod,它可以编写 TypeScript 代码,安装它,并在你的会话中进行热重载。