正则测试/提取/安全替换/静态解释(不执行代码)。
安装
# GitHub 源码(首次需按提示配置 allowBuilds 构建授权后重试)
dsh plugin --profile web add github:omdsh-dev/dsh-tool-regex
装任何插件都等于在你的机器上跑第三方代码,权限和你本人一样大——能读你的文件、用你的凭据、访问网络,工具审批管不到它。GitHub 来源的插件还会在安装时执行构建脚本——pnpm 默认拦截,所以安装可能停在 ERR_PNPM_GIT_DEP_PREPARE_NOT_ALLOWED 或 ERR_PNPM_IGNORED_BUILDS;dsh 会打印出需要添加的确切键名,把它加进该 profile 的 pnpm-workspace.yaml 的 allowBuilds 下,重跑一次即可装上。放行构建本身就是一次信任判断:请只安装可信来源,并尽量锁定 commit(github:owner/repo#sha)。
README
DSH 正则工具插件 —— 测试匹配、提取捕获组、安全替换、静态解释正则含义(不执行任何代码)。零依赖、纯函数。
动机
模型经常需要验证用户给的 pattern、从日志/文本中提取字段、做文本替换。"心算"正则结果错误率极高,且无法给用户展示可验证的过程。现有替代是起 bash 进程跑 node -e 或 python——进程开销 + 模型现写脚本的正确性风险。内置 grep 只能做文件域搜索,无法对任意文本测试/提取/替换/解释。
本插件提供确定性正则工具,其中 explain 是差异化能力:静态解析 pattern 结构并给出人读解释,不执行匹配,天然免疫 ReDoS。
安全模型(ReDoS 多层防线)
JS 正则的灾难性回溯是真实威胁(如 (a+)+$ 配合超长输入)。防线:
- worker 硬超时:test/find/replace 在可终止的 worker 线程内同步执行,1,000ms 预算到期
worker.terminate()并返回regex: execution timed out——灾难性回溯不再能阻塞宿主进程(工具管道的timeoutMs对同步阻塞体是协作式,仅靠它不够;worker 内会再次执行全部上限校验) - 输入长度上限:64,000 字节(UTF-8)——超限在入口直接拒绝,不进入回溯
- 资源上限:pattern ≤ 16KB、replacement ≤ 16KB、输出 ≤ 1MB、匹配数 ≤ 1,000(limit 钳制)
- explain 零执行:只做静态 tokenizer,不构造
RegExp实例,任何 pattern 都即时返回
⚠️ 工具描述与 README 均明确警告模型:不要对不可信的大输入使用无锚点的嵌套量词 pattern(如
(a+)+、(.*)*)。
其余边界:无效 pattern 捕获 SyntaxError 报错(含位置信息);无效/重复 flag 逐字符校验;replace 使用 String.replace 字符串替换路径(JS 原生 $-语义,无 new Function、无 eval)。
工具声明
注册 regex 工具(@deepseek-ai/dsh-tool-regex,row id tool-regex),统一输出 JSON 文本字符串。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
action |
string | ✅ | test / find / replace / explain |
pattern |
string | ✅ | 正则(JavaScript 语法,不含外围 /);≤ 16KB |
input |
string | 待匹配文本(test/find/replace 必需);≤ 64KB | |
flags |
string | 如 "gi";支持 g i m s u y d v,需唯一且合法 |
|
replacement |
string | replace 的替换文本,支持 $1/$2/$<name>/$$;≤ 16KB |
|
limit |
integer | find 最大报告匹配数,默认 50,上限 1,000 |
Actions
| action | 功能 | 输出示例 |
|---|---|---|
test |
判断是否匹配(整串语义由模型自行用 ^...$ 表达) |
{"matched":true} |
find |
全部匹配:index / 完整匹配 / 编号捕获组 captures / 命名组 groups(无 g 自动补 g) |
[{"index":0,"match":"a@b","captures":["a","b"],"groups":{"name":"a"}}] |
replace |
全局安全替换($1/$<name>/$$),返回结果与替换次数 |
{"result":"world hello","replaced":1} |
explain |
静态解析 pattern → 人读节点序列(不执行匹配;节点数 ≤ 4,096) | [{"kind":"escape","text":"\\d","meaning":"A digit [0-9]"}] |
示例
regex { action: "find", pattern: "(\\w+)@(\\w+)", input: "a@b x c@d" }
→ [{"index":0,"match":"a@b","captures":["a","b"],"groups":null},{"index":6,"match":"c@d","captures":["c","d"],"groups":null}]
regex { action: "replace", pattern: "(\\w+) (\\w+)", input: "hello world", replacement: "$2 $1" }
→ {"result":"world hello","replaced":1}
regex { action: "explain", pattern: "\\d{4}-\\d{2}" }
→ [{"kind":"escape","text":"\\d","meaning":"A digit [0-9]"},{"kind":"quantifier","text":"{4}",...},...]
边界行为
| 情况 | 处理 |
|---|---|
| 无效 pattern | regex: invalid pattern: <SyntaxError 信息(含位置)>,不崩溃 |
| 无效 flag / 重复 flag | regex: invalid flag "q" / regex: duplicate flag "g" |
| 空 pattern | 合法(匹配空串);u/v 下空匹配按 code point 推进(surrogate pair 不会重复命中) |
| ReDoS(病理 pattern) | worker 硬超时:regex: execution timed out (1000ms),宿主不阻塞 |
| 命名组 / 编号组 | find 输出 groups: {name: value} 与 captures: [...];replace 支持 $<name>/$n |
| 零匹配 | find 返回 [];replace 返回原文本 + replaced: 0 |
| 输入超 64KB / pattern 超 16KB / replacement 超 16KB | 入口拒绝(不截断) |
输出超 1MB(替换放大如 $`` / $'`) |
regex: result/output exceeds 1000000 bytes,拒绝而非截断 |
| find limit | 默认 50,钳制到 1,000(防输出膨胀) |
| explain 节点超 4,096 | regex: explain: pattern too complex |
$ 引用 |
走 JS 原生字符串替换路径:$$→$、$n→组(未参与→空串)、$<name>→命名组、未知引用字面保留($0/$<foo> 与 V8 一致) |
DSH 0.1.5-rc.1 兼容(已验证)
本插件已迁移到 DSH 0.1.5-rc.1 依赖线,并在 local harness 0.1.5-rc.1(npm 私有包)的隔离 consumer 中完成全链路验证:
- 类型/运行时:peer 为
@deepseek-ai/cordis: ^4.0.1+@deepseek-ai/dsh-tools: >=0.0.1-rc.1 <0.2.0+@deepseek-ai/dsh-invariants: >=0.0.1-rc.1 <0.2.0;不再依赖 unscopedcordis - 独立构建:
npm install(devDependencies 自包含 typescript/vitest/@types/node)→npm run typecheck→npm test→npm run build→npm pack - 消费验证:tarball 装入 DSH 0.1.5-rc.1(npm)consumer →
dsh --profile compat --dump-config出现本插件 row → 工具真实注册与执行通过 - 启动方式:
npx -p @deepseek-ai/dsh@next dsh web(lib 生产模式;勿install -g全局安装)
安装
Profile Bundle(推荐)
将本插件作为独立 bundle 安装到 profile(DSH 0.1.5-rc.1(npm))。本仓库位于 omdsh-dev 组织,公开可访问:
# 交互式(web)profile —— 从 GitHub 仓库安装
dsh plugin --profile web add github:omdsh-dev/dsh-tool-regex
# 一次性任务(headless)profile —— dsh run 默认使用 headless
dsh plugin --profile headless add github:omdsh-dev/dsh-tool-regex
或使用 npm pack 生成的 tarball 安装:
npm pack # 生成 dsh-tool-regex-<version>.tgz
# 交互式(web)profile
dsh plugin --profile web add ./dsh-tool-regex-<version>.tgz
# 一次性任务(headless)profile
dsh plugin --profile headless add ./dsh-tool-regex-<version>.tgz
包内 dsh.bundle.patch 会在安装后自动把插件加入 profile 的 layer stack(row id:tool-regex)。插件缺失的 peer 依赖(@deepseek-ai/cordis、@deepseek-ai/dsh-tools)由 profile 的 healed profiles/node_modules 回退安装提供。
⚠️ web 与 headless 是不同 profile:web 安装不会自动覆盖 headless;
dsh run默认使用 headless profile。Windows 路径使用正斜杠(C:/...)。
验证安装
dsh --profile web --dump-config | grep tool-regex
运行验证
dsh run "使用 regex 工具测试 d+ 是否匹配 abc123"
手动安装与旧版本兼容(monorepo 旧场景)
monorepo 方式仅适用于旧场景:不支持 Profile Bundle 的旧快照或插件开发调试环境(本地 junction/symlink、手动编辑 profile 层)。
测试
node <monorepo>/node_modules/vitest/vitest.mjs run tests
engine.spec.ts:test/find/replace 全分支 + flags/pattern 错误 + 64KB 上限 + ReDoS worker 用例(病理 pattern 在 3s 预算内被取消,不挂死测试进程)explain.spec.ts:字面量/字符类/分组/量词/转义/锚点/交替 + 未闭合报错register.spec.ts:注册契约(AUDIT-CROSS-02 风格)
许可
MIT
链接
同类插件
Tencent/WeKnora#dsh-weknora★ 30675
把 WeKnora 知识库接入 dsh 的四个只读工具:列出知识库、混合检索原文片段、按顺序还原单篇文档,以及直接取用 WeKnora 自己带引用的 RAG 或 ReAct agent 回答(含可续聊的 session id)。
superdesigndev/treg★ 3596
给 Agent 的工具目录:按「要做的事」检索约 2,600 个外部接口(SEO 与 SERP、外链、社交、人物与公司信息补全、广告库、抓取),查看参数与单次调用价格后直接调用,凭据由服务端注入。附带技能,MCP 行在未设置 TREG_TOKEN 前保持禁用。
TencentCloudBase/CloudBase-AI-Toolkit#dsh-plugin★ 1126
把腾讯云 CloudBase 后端接入 DeepSeek Harness——在对话里搭好并部署全栈应用,查询结果渲染为表格卡片(分页、排序、导出 CSV),部署后可预览真实域名,并提供 CloudBase MCP 工具集(`mcp__cloudbase__*`),登录走 device-code 流程。
gitroomhq/postiz-agent#dsh-postiz★ 496
通过 MCP 将 DeepSeek Harness 连接到 Postiz:列出已连接的社交媒体渠道、获取各平台发帖规则,并向 X、LinkedIn、Instagram、Facebook、Threads、TikTok、YouTube、Reddit、Bluesky、Mastodon、Discord、Slack、Telegram 等平台排期、存草稿或发布帖子;附带 postiz 工作流技能。
EthanYoQ/Invoice-Downloader#dsh-invoice-downloader★ 446
面向 DeepSeek Harness 的本地 IMAP 发票下载、OCR 识别、归档与 Excel 报销汇总。
anysearch-team/anysearch-dsh★ 430
基于 AnySearch 的实时网页与垂直搜索插件,为 DeepSeek Harness 提供搜索工具。
社区评论
评论公开保存在 GitHub Discussions。加载评论会连接 GitHub 和 Giscus;发表内容需要 GitHub 账号。