DeepSeek Harness 插件
准备环境
dsh 从源码运行,见 dsh 说明。或见之前分享的 DeepSeek Harness 开始。
开发插件
依照 dsh 文档,动手做一遍:
- 第一个插件: https://deepseek-harness.github.io/deepseek-harness/develop/basic/
- 第一个工具: https://deepseek-harness.github.io/deepseek-harness/develop/basic/tool
在 dsh 仓库根目录创建插件目录:
mkdir -p start-dsh-plugin/src
写 start-dsh-plugin/src/index.ts:
import type { Context } from '@deepseek-ai/cordis'
import { defineTool } from '@deepseek-ai/dsh-tools'
import Schema from '@deepseek-ai/schemastery'
// 插件名称
export const name = 'start-dsh-plugin'
// 注册就绪
// inject 让 Cordis 等待工具注册表就绪
export const inject = ['tools']
// 插件配置
// 对应 cordis.yml 中 config
export interface Config {
greeting: string
maxRetries: number
verbose?: boolean
}
// Schema 校验
// 在插件加载时执行校验。如果配置不合法,插件会加载失败并给出明确错误信息
export const Config: Schema<Config> = Schema.object({
greeting: Schema.string().default('Hello'),
maxRetries: Schema.number().default(3),
verbose: Schema.boolean().default(false),
})
// 插件实现
// 插件是一个导出 apply 函数的 TypeScript 模块,通过 ctx 注册能力
export function apply(ctx: Context, config: Config) {
console.log(`[${name}] plugin loaded!`)
// 注册一个 greet 工具
ctx.tools.register(defineTool({
name: 'greet',
description: 'Greet someone by name.',
parameters: {
name: { type: 'string', required: true, description: 'The name to greet' },
},
output: {
schema: { type: 'string' },
render: (_args, value) => [{ type: 'text', text: value }],
},
async execute(args) {
return `${config.greeting}, ${args.name}!`
},
}))
}
写 start-dsh-plugin/cordis.yml:
- insert:
- id: start-dsh-plugin
name: './src/index.ts'
config:
greeting: 'Hi there'
maxRetries: 5
重新运行 dsh web:
pnpm dsh web --patch ./start-dsh-plugin/cordis.yml
对话输入 Use the greet tool to greet Ada.,模型会调用 greet 工具,回复 Hi there, Ada! 👋。
打包插件
上述通过 --patch overlay 加载本地插件。
这里再动手打包成组合包(bundle),用 dsh plugin add 来安装。
写 start-dsh-plugin/package.json:
{
"name": "start-dsh-plugin",
"version": "0.1.0",
"type": "module",
"main": "src/index.ts",
"files": ["src/index.ts", "cordis.patch.yml"],
"dsh": { "bundle": { "patch": "./cordis.patch.yml" } }
}
写 start-dsh-plugin/cordis.patch.yml:
- insert:
- id: start-dsh-plugin
name: start-dsh-plugin
config:
greeting: 'Hi'
maxRetries: 5
安装进 dsh web:
# 从本地安装
pnpm dsh plugin --profile web add ./start-dsh-plugin
# 通过名称移除
pnpm dsh plugin --profile web remove start-dsh-plugin
dsh web 是预装的一个 profile,都位于 $DSH_HOME/profiles/<name>。
之后,重新运行 dsh web(web 是 --profile web 的别名):
# 查看配置树
pnpm dsh web --dump-config | grep start-dsh-plugin
# 重新运行
pnpm dsh web
发布脚本
要发布,先得补齐再构建:
构建 lib/:
cd start-dsh-plugin/
# 在 harness 仓库内构建
pnpm exec tsc -p tsconfig.local.json
# 或,clone 项目安装依赖再构建
pnpm install && pnpm run build
pnpm pack --dry-run
发布 tar:
rm -r lib/
# 打包
# 若在 harness 仓库内打包,把 package.json 里改为 "build": "tsc -p tsconfig.local.json"
pnpm pack
# 从 tar 包安装
pnpm dsh plugin --profile web add ./start-dsh-plugin-0.1.0.tgz
发布 npm:
pnpm login
pnpm publish --access public
# 从 npm 安装
pnpm dsh plugin --profile web add start-dsh-plugin
发布 github:
# 从 github 安装
# 官方文档要让允许从源码构建,但看其他插件就直接 package.json 指向源码、或把 lib/ 上传
pnpm dsh plugin --profile web add github:ikuokuo/start-dsh-plugin
一些问题
该 dsh 工具插件在其最新源码上不能正常启动,报错导入不了 'CallId'。
因其 dsh-tools 版本到了 0.1.2-alpha.1,而 package.json 里用的最新发布只到 "^0.1.1-rc.2"。
需在 harness 仓库内构建或打包,再安装。
浙公网安备 33010602011771号