DSH 插件 dsh-tool-calculator:安全求值数学表达式

前言

DSH 插件生态中,工具插件可以独立安装到指定 profile。下面介绍 omdsh-dev 维护的 dsh-tool-calculator

这个插件注册 calculator 工具,用于安全求值数学表达式。插件说明将其描述为“零依赖、零进程、纯函数”;从已核实资料看,它适配 DSH 0.1.2-alpha.1,可作为独立 bundle 安装到 profile。许可证为 MIT。

插件定位

先确认几个基础信息:

  • npm 包名:@deepseek-ai/dsh-tool-calculator
  • 注册工具名:calculator
  • 仓库地址:https://github.com/omdsh-dev/dsh-tool-calculator
  • 许可证:MIT
  • package.jsonprivatetrue
  • package.json 声明 Node 引擎为 ^22.19.0 || >=24.0.0

工具输入为 expression 字符串,输出为有限数字;NaN / Infinity 会被拒绝。

核心功能

支持的运算符

支持以下运算:

  • +
  • -
  • *
  • /
  • %
  • **
  • 括号 ( )
  • 一元正负,例如 +5-5

其中 ** 为幂运算,且是右结合:

2 ** 3 ** 2 = 512

支持的函数与常量

支持这些函数:

  • abs
  • ceil
  • floor
  • round
  • sqrt
  • log
  • log2
  • log10
  • exp
  • sin
  • cos
  • tan
  • pow
  • max
  • min

支持常量:

  • PI
  • E

三角函数使用弧度。如果习惯角度,需要在表达式里换算,例如:

sin(30 * PI / 180)

安全边界

已核实资料显示,该插件不使用 eval,也不使用 new Function。它使用手写递归下降解析器,只接受白名单数字字面量、标识符和运算符。

下面这些写法会被拒绝或报错:

  • 引号
  • 分号
  • 反引号
  • { }
  • [ ]
  • 非白名单标识符
  • 求值结果为 NaNInfinity 的表达式

也就是说,它只把输入当作数学表达式处理,而不是当作代码执行。

安装与启用

安装到 profile

安装命令如下。webheadless 是不同 profile,按需选择。

交互式或 web profile:

dsh plugin --profile web add github:omdsh-dev/dsh-tool-calculator

headless profile:

dsh plugin --profile headless add github:omdsh-dev/dsh-tool-calculator

注意:web 安装不会自动覆盖 headless;dsh run 默认使用 headless profile。如果你主要用 dsh run,应安装到 headless profile。

验证安装

安装后先检查配置里是否出现该插件:

dsh --profile web --dump-config | grep tool-calculator

如果安装到 headless profile,对应命令是:

dsh --profile headless --dump-config | grep tool-calculator

运行验证

用一句自然语言让 agent 调用 calculator 工具:

dsh run "使用 calculator 工具计算 1+2*3"

环境与依赖注意

下面几点在安装前值得确认:

  • 适配 DSH 0.1.2-alpha.1
  • 启动方式:
npx -p @deepseek-ai/dsh@next dsh web
  • 不要使用 install -g 全局安装
  • peer 依赖 @deepseek-ai/cordis@deepseek-ai/dsh-tools@deepseek-ai/dsh-invariants 由 profile 的 healed profiles/node_modules 回退安装提供
  • DSH 0.1.2-alpha.1 的 patch 是 id-targeted 语义,必须用 - insert: 列表包裹

典型用法

安装后,agent 可以使用 calculator 工具处理数学表达式。

示例:

calculator { expression: "15 + 27 * sqrt(9)" }  →  96

通过命令行运行:

dsh run "使用 calculator 工具计算 1+2*3"

幂运算:

2 ** 3 ** 2 = 512

角度换算后的三角函数:

sin(30 * PI / 180)

适用场景与注意

这个插件适合需要给 agent 增加确定性数学求值能力的场景。它把计算限制在表达式层,避免把输入交给代码执行环境。

使用前建议确认:

  • 插件以当前 dsh 进程权限运行
  • 安装前应检查源码与许可证
  • 许可证为 MIT
  • 插件介绍称“零依赖、零进程、纯函数”,但已核实资料也显示它依赖 DSH 0.1.2-alpha.1 profile 环境,并由 profile 的 healed profiles/node_modules 回退安装提供 peer 依赖

已知限制包括:

  • 求值结果必须是有限数字,NaN / Infinity 统一拒绝
  • 三角函数使用弧度
  • 不支持大整数:JS number 是 IEEE 754 double,超出安全整数范围 ±9e15 有精度损失
  • 不支持科学计数法:1e5 会被词法层拒绝
  • web 与 headless 是不同 profile,web 安装不会自动覆盖 headless

结尾

dsh-tool-calculator 提供了一个边界较窄、用途明确的 calculator 工具:输入数学表达式,输出有限数字,并拒绝代码执行类输入。

仓库地址:

https://github.com/omdsh-dev/dsh-tool-calculator
羽毛球分组比赛记分
小程序二维码

欢迎使用《羽毛球分组比赛记分》微信小程序

小夜