Skip to main content

dsh-tool-calculator/packages/dsh-tool-calculator

24Stars1Forks1Issues0Watchers

Offers a zero-dependency calculator tool with deterministic operations for arithmetic and unit conversions.

Evidence5/5methodologySourceInstallMaintenanceDSH versionSecurity scan
Machine-auditedInstall commandRepo verifieddsh-plugin topicLicenseREADMEAI wiki

ⓘ This plugin is a sub-package of the omdsh-dev/dsh-toolkit monorepo — stars and activity count the whole repository.

Language
TypeScript
License
MIT
Branch
main
collectiondshdsh-plugintoolkitzero-dependency

Install

cmdweb profile
$ dsh plugin --profile web add github:omdsh-dev/dsh-toolkit#path:packages/dsh-tool-calculator

Run the command above in your terminal to install this plugin via the dsh CLI. You can switch Profile in the top-right corner. New to dsh? Read the beginner tutorial

Install via your agent

Install the DeepSeek Harness plugin omdsh-dev/dsh-toolkit/packages/dsh-tool-calculator for me: review the repository at https://github.com/omdsh-dev/dsh-toolkit first, then run the install command and verify the plugin loads successfully.

Paste this instruction to the DSH Web GUI assistant — it will install and verify for you.

一句话定位

DSH 的安全数学计算工具,让 AI 在做算术时不必每次都起一个 bash 进程,覆盖四则运算与常用初等数学函数,纯函数毫秒级返回结果。

核心能力

  • 让 AI 调用 calculator 工具直接对数学表达式求值,避免用 bash 算术每次起子进程
  • 支持四则运算 + - * / %、幂运算 **(右结合)、一元正负、括号分组
  • 提供 13 个单参函数(abs ceil floor round sqrt log log2 log10 exp sin cos tan)和 2 个变参函数(pow(x, y) max/min)
  • 内置两个数学常量 PI 和 E
  • 严格安全模型:不使用 eval 与 new Function,手写递归下降解析器只接受白名单标识符与运算符
  • 错误输入全部抛错:未知标识符、参数个数错误、非有限结果(NaN/Infinity)、非法字符、超长表达式都会失败而非静默返回

技术实现

  • 语言: TypeScript(ESM 模式,tsc 编译到 lib/)
  • 关键依赖: @deepseek-ai/cordis、@deepseek-ai/dsh-tools、@deepseek-ai/dsh-invariants
  • 架构模式: Cordis 插件模型(name + inject: ['tools'] + apply(ctx)),在 apply 中通过 ctx.tools.register(defineTool(...)) 注册名为 calculator 的工具;插件安装由 cordis.patch.yml 用 - insert: 列表把 tool-calculator 条目插入目标 profile 的 layer 栈
  • 入口文件: src/index.ts(Cordis 插件入口与工具声明)/ src/evaluate.ts(手写词法+递归下降求值器)/ src/invariant.ts(包级 invariant companion)

适用场景

AI 频繁做算术、对一组数取最大/最小、或者需要做开方、对数、三角函数等超出 bash 算术能力的计算时,本工具以纯函数方式毫秒级返回结果,避免每个调用都付一次进程启动开销;尤其在 Windows 上替代 bash 算术收益明显。返回结果必须是有理意义的数字(NaN/Infinity 会被拒绝),适合做确定性计算的中间环节。

前置依赖与兼容性

依赖最低版本说明
DSH0.1.0-rc.8+peerDependencies 要求 @deepseek-ai/dsh-tools 与 @deepseek-ai/dsh-invariants 在 [0.0.1-rc.1, 0.2.0),@deepseek-ai/cordis ^4.0.1;README 明确在 @deepseek-ai/[email protected] 隔离 consumer 完成全链路验证
Node.js>=22.19.0package.json#engines 声明 ^22.19.0 || >=24.0.0
平台macOS / Windows / Linux纯 TypeScript 实现,未声明任何原生模块;README 提示 Windows 路径使用正斜杠
原生模块无仅依赖运行时 JS 包,不引入 node-pty、node:sqlite 等原生模块

安装方式

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

配置项

本插件无需额外配置。calculator 工具的唯一输入是单次调用的 expression 字符串,源码中没有读取 config、process.env、options 等外部配置。

配置类型说明默认值
expressionstring待求值的数学表达式,例如 "15 + 27 * sqrt(9)"(每次调用必填,无默认值)

常见问题

Q: 这个计算器安全吗?会不会执行用户输入的代码?

A: 安全。src/evaluate.ts 自带手写递归下降解析器(词法层+语法层),完全不使用 eval 也不使用 new Function。所有标识符通过 Object.hasOwn 查白名单(13 个单参函数 + 2 个变参函数 + 2 个常量),不在白名单的立即抛 Unknown identifier。词法层对引号、分号、反引号、{}、[]、.(独立形式)均直接报错;constructor.constructor(...)、process.exit(0)、globalThis、__proto__ 等攻击载荷在测试用例中被实测拒绝。

Q: 支持哪些运算和函数?

A: 算术:+ - * / % 和幂 **(右结合,例如 2 ** 3 ** 2 = 512);一元正负与括号分组;单参函数 abs ceil floor round sqrt log log2 log10 exp sin cos tan;多参函数 pow(x, y) 与 max/min(可变参数);常量 PI 与 E。

Q: 三角函数用的是角度还是弧度?

A: 弧度,与 Math.sin/Math.cos 行为一致;需要角度时写 sin(30 * PI / 180) 自行换算。

Q: 表达式长度有上限吗?

A: 有。evaluate() 入口处对 expression 长度做硬限制(MAX_EXPRESSION_LENGTH = 500),超过直接抛 Expression too long,避免被恶意大输入阻塞。

Q: 哪些输入会失败?会返回 NaN 吗?

A: 不会。parse() 求值后,若结果不是有限数字(NaN、Infinity,例如除零、sqrt(-1)、log(0)),evaluate() 会抛 Expression did not evaluate to a finite number;未知标识符、参数个数错误(sqrt(9, 1) / pow(2))、非法字符、引号/分号、科学计数法(1e5、6.02e23)、超长表达式也都会抛错,工具不会静默返回垃圾数字。

Q: 需要写配置吗?需要联网或读文件吗?

A: 不需要任何配置,所有逻辑都基于单次工具调用传入的 expression 字符串;实现是纯函数,不读文件、不写文件、不联网、不调子进程。也不依赖任何外部配置或环境变量。

Q: 跟 DSH 内置的 bash 算术(echo $((1+2)))比有什么差别?

A: bash 算术每次都要起一个 shell 进程,Windows 上进程创建+shell 加载代价明显,且 bash 算术不支持 sqrt sin cos log pow 等函数;本工具是纯函数毫秒级调用,覆盖常用初等数学函数,AI 不必为求一个开方再写脚本。

Q: 装到 web profile 后,dsh run 能直接用到吗?

A: 不能。web 与 headless 是两个独立的 profile,本插件需要单独装到 headless(dsh run 默认使用),或者用集合仓库自带的批量脚本一次性同时装到两边。

上手难度

入门 — 安装一行命令即生效,AI 在对话里按需调用 calculator 工具并传入表达式即可,无需写配置或脚本;理解白名单运算与三角函数用弧度即可避免踩坑。

已知问题与限制

  • 表达式长度上限 500 字符,超出直接拒绝(src/evaluate.ts:42、src/evaluate.ts:168-170)
  • 数字结果受 IEEE 754 双精度浮点限制,安全整数范围约 ±9e15,超出会有精度损失,不适合做大整数运算(README.md:185)
  • 不支持科学计数法,1e5、6.02e23 会被词法层专门拒绝并提示 'Scientific notation is not supported'(src/evaluate.ts:53-56、tests/evaluate.spec.ts:140-146)
  • 三角函数使用弧度而非角度,需要角度需自行乘 PI/180 换算(README.md:184)
  • 工具调用超时 timeoutMs: 1000(src/index.ts:36),单次求值超过 1 秒会被宿主中断
  • 函数参数个数由白名单严格约束,pow 必须恰好 2 个、sqrt 必须 1 个,max/min 至少 1 个,否则抛 Invalid argument count(src/evaluate.ts:142-147)
  • 仓库内未发现 TODO/FIXME/HACK/XXX 注释

Read the usage guide →

Install steps, key points, FAQ and compatibility for this plugin — auto-derived from indexed fields.

Listing badge

Listed on deepseek-plugin.org
[![Listed on deepseek-plugin.org](https://img.shields.io/badge/listed_on-deepseek--plugin.org-007EC6)](https://deepseek-plugin.org/plugins/omdsh-dev/dsh-toolkit/packages/dsh-tool-calculator)

Paste this markdown into your GitHub README to link back to this listing. The badge only states the listing — not a security endorsement.

← Back to plugin directory