USEGEAR

导航

AIGC标识 学习harness【1】Harness 插件开发入门:从配置到网页调用

Harness 插件开发入门:从配置到网页调用

图片


本教程以 hello 插件为例,完成:

编写插件 → 配置加载 → 独立测试 → 网页中由模型调用。

环境:Windows、Node.js,已安装 DeepSeek Harness 0.2.2-rc.1。学习目录为 G:\Harness_test\AgentLab。

1. 认识三个文件

文件作用
plugins\hello\index.mjs 插件程序,定义并注册 greet 工具
data\harness-home\profiles\web\cordis.patch.yml Web Profile 的自定义配置,用于加载插件、调整参数
scripts\try-hello.mjs 独立测试插件,不依赖网页或模型

两条运行路径:

网页运行:cordis.patch.yml → hello/index.mjs
独立测试:try-hello.mjs    → hello/index.mjs

2. 编写插件

创建 plugins\hello\index.mjs:

import { defineTool } from '@deepseek-ai/dsh-tools';

export const name = 'hello-plugin';
export const inject = ['tools'];

export function apply(ctx) {
  console.log('[hello] 插件已加载,正在注册 greet 工具');

  ctx.tools.register(defineTool({
    name: 'greet',
    description: '向指定名字的人问好。',
    parameters: {
      name: {
        type: 'string',
        required: true,
        description: '要问候的人的名字',
      },
    },
    output: {
      schema: { type: 'string' },
      render: (_args, value) => [
        { type: 'text', text: value },
      ],
    },
    async execute(args) {
      return `你好,${args.name}!我是☁️启禾usegear`;
    },
  }));
}

关键区别:

  • apply(ctx):加载插件时执行,注册工具。
  • execute(args):调用工具时执行,返回业务结果。
  • description、`parameters`:说明工具用途和输入规则,供框架及模型使用。
  • output:定义返回值类型及文本呈现方式。

加载插件不会自动调用工具,也不会自动生成网页按钮。

3. 配置加载

修改前先备份 cordis.patch.yml。保留其他已有配置,加入:

- id: webserver
  config:
    host: '127.0.0.1'
    port: 3081

- insert:
    - id: hello
      name: 'file:///G:/Harness_test/AgentLab/plugins/hello/index.mjs'

如果已有 webserver 配置,不要重复添加。

  • id: webserver:修改已有插件的参数。
  • insert:添加新插件。
  • name: file:///…:指定本地插件文件。

YAML 用缩进表达层级。 - insert: 顶格,不能放进 webserver 的 config 内。此处绝对路径用于学习,发布时需要调整为可搬移的引用方式。

检查最终配置:

cd G:\Harness_test\AgentLab
$env:DSH_HOME = 'G:\Harness_test\AgentLab\data\harness-home'

.\node_modules\.bin\dsh.cmd --profile web --dump-config |
    Select-String -Pattern 'id: hello' -Context 0,3

看到 hello 表示配置已包含插件,还不代表插件已经加载或执行。

4. 独立测试

本项目的测试脚本已准备好,执行:

node .\scripts\try-hello.mjs

它独立加载真实 Harness 组件和 hello,通过工具执行器提交调用。

已验证六种情况:

  • 正常中文姓名。
  • 另一姓名与特殊字符。
  • 参数类型错误。
  • 缺少必填参数。
  • 无效 JSON。
  • 未知工具。

预期错误被正确拒绝,也属于测试通过。结果保存在 evidence\hello-test-report.json。

测试脚本不读取 Web Profile、不调用模型,结果只显示在终端和报告中。

5. 启动网页

在指定了 DSH_HOME 的 PowerShell 窗口运行:

.\node_modules\.bin\dsh.cmd web

如果服务正在运行,先按 Ctrl+C 停止,再重新启动。

观察终端中的:

[hello] 插件已加载,正在注册 greet 工具

使用本次启动打印的完整地址打开浏览器,包括 ?token=...。这个 token 是本地服务访问凭据,不是模型 API Key。

6. 在网页中调用

配置好模型连接后,新建会话并输入:

请调用 greet 工具,向小明问好,并展示工具返回的原文。

预期返回:

你好,小明!我是☁️启禾usegear

同时查看会话中的 greet 工具调用记录。不能仅凭模型文字“已调用”判断工具真的执行。

真实调用过程:

用户自然语言
→ 模型选择 greet,填写 name 参数
→ Harness 校验参数并执行 execute()
→ 插件返回结果
→ 模型根据结果回答
→ 网页显示

7. 本次掌握的开发思路

配置负责加载,插件负责提供能力,测试负责验证,模型负责选择调用。

以后开发订单查询工具,结构基本不变:把 greet 换成 query_order,把姓名参数换成订单号,把问候函数换成查询函数。

本教程完成的是工具开发基础练习。默认 Web 配置仍可能包含 Shell 等其他工具;仅开放指定工具的权限配置、业务接入和正式打包发布,需要另行完成。

posted on 2026-11-16 15:33  USEGEAR  阅读(29)  评论(0)    收藏  举报