从使用者走向扩展者

写出第一个 Cordis 插件

从 apply(ctx)、Patch 挂载、依赖注入到 effect 清理,完成最小可运行插件。

官方事实适用 0.1.0-rc.514 分钟核验于 2026-08-14

最小 Cordis 插件是导出 apply(ctx) 的 TypeScript 模块。下面路径面向已经完成源码构建的 DeepSeek Harness 仓库。

创建目录

mkdir -p scratch-plugin/src

scratch-plugin/src/my-plugin.ts

import type { Context } from '@deepseek-ai/cordis'

export const name = 'hello-plugin'

export function apply(ctx: Context) {
  console.log('[hello-plugin] plugin loaded!')
}

用 Patch 挂载

scratch-plugin/cordis.yml

- insert:
    - id: hello
      name: '/absolute/path/to/deepseek-harness/scratch-plugin/src/my-plugin.ts'

name 必须换成实际绝对路径。Patch 文件所在目录不会改变 Profile 的模块解析起点。

pnpm dsh web --patch ./scratch-plugin/cordis.yml

预期终端输出:

[hello-plugin] plugin loaded!

声明服务依赖

不要依赖配置文件中的条目顺序。用 inject 声明依赖:

import type { Context } from '@deepseek-ai/cordis'

export const name = 'my-tool-plugin'
export const inject = ['tools']

export function apply(ctx: Context) {
  // ctx.tools is ready here.
}

清理外部资源

注册在 ctx 上的事件和工具会随插件卸载撤销。定时器、网络连接等外部资源应交给 ctx.effect(),并返回 disposer:

import type { Context } from '@deepseek-ai/cordis'

export function apply(ctx: Context) {
  ctx.effect(() => {
    const timer = setInterval(() => {
      console.log('heartbeat')
    }, 5000)

    return () => clearInterval(timer)
  })
}

插件代码进入 Harness 进程和生命周期。不要加载未审计源码,也不要把 GitHub Topic 当作安全背书。

证据与修订

一手来源

本页叙事经过压缩;命令、行为与风险边界以以下官方源码或文档为准。