dsh-js-tools 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +71 -2
- package/cordis.patch.yml +3 -0
- package/package.json +30 -4
- package/src/index.js +173 -0
package/README.md
CHANGED
|
@@ -1,3 +1,72 @@
|
|
|
1
|
-
#
|
|
1
|
+
# dsh-js-tools
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
DSH(DeepSeek Harness)通用测量工具插件。给 DSH 会话里的模型装上"精确测量"能力:字数统计与数学表达式求值,专治判分/评测 rubric 里的目测问题。
|
|
4
|
+
|
|
5
|
+
**适配版本:DSH `0.2.1-alpha.3`**(依赖 `@deepseek-ai/dsh-tools` 同版本对齐)
|
|
6
|
+
|
|
7
|
+
## 工具清单
|
|
8
|
+
|
|
9
|
+
### 1. `text_stats` — 精确字数统计
|
|
10
|
+
|
|
11
|
+
统计一段文本的精确字数,**不要目测**。判分中涉及「字数上限/下限」「篇幅」「长度」类 Rubric 时,模型会先调用本工具拿到精确数字再下结论。
|
|
12
|
+
|
|
13
|
+
返回字段:
|
|
14
|
+
|
|
15
|
+
| 字段 | 说明 |
|
|
16
|
+
|---|---|
|
|
17
|
+
| `characters` | 字符数(含空白) |
|
|
18
|
+
| `bytes` | UTF-8 字节数 |
|
|
19
|
+
| `lines` | 行数 |
|
|
20
|
+
| `nonEmptyLines` | 非空行数 |
|
|
21
|
+
| `words` | 单词数(按空白切分) |
|
|
22
|
+
| `cjkCharacters` | 汉字数(CJK 统一表意文字) |
|
|
23
|
+
| `whitespace` | 空白字符数 |
|
|
24
|
+
| `nonAsciiCharacters` | 非 ASCII 字符数 |
|
|
25
|
+
| `estimatedTokens` | 估算 token 数(CJK 0.6/字符,其他 0.25/字符) |
|
|
26
|
+
|
|
27
|
+
超大文本(>10 万字符)请先分段再逐段调用。
|
|
28
|
+
|
|
29
|
+
### 2. `evaluate_expression` — 安全表达式求值
|
|
30
|
+
|
|
31
|
+
计算数学表达式,返回数值结果。支持四则运算、括号、幂(`^`)、百分号(`%`)。判分中涉及数字计算、比例换算、金额/权重计算的 Rubric 精确判定。
|
|
32
|
+
|
|
33
|
+
示例:`3 * 4 + 2`、`(85 + 90) / 2`、`7 * 30`、`1200 * 0.2`
|
|
34
|
+
|
|
35
|
+
安全设计:输入白名单过滤(仅数字/运算符/括号/小数点),禁止任何标识符,防注入。
|
|
36
|
+
|
|
37
|
+
## 安装
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
# 在 DSH profile 目录下
|
|
41
|
+
dsh plugin --profile xeval add -w dsh-js-tools
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
安装后可用 `dsh plugin --profile xeval dump-config` 确认插件进入依赖树。
|
|
45
|
+
|
|
46
|
+
## 使用
|
|
47
|
+
|
|
48
|
+
插件通过 `ctx.tools.register` 注册工具,DSH 会话内模型可自动调用。判分/评测场景建议在 prompt 中追加工具提示段,声明可用工具及"字数类/数值类 rubric 必须调用工具精确实测、禁止目测"的约束,模型才会主动调用。
|
|
49
|
+
|
|
50
|
+
```text
|
|
51
|
+
可用工具:
|
|
52
|
+
- text_stats:精确统计文本字数(字符/行/词/CJK/token),涉及字数类 Rubric 必须先调用再判定,禁止目测。
|
|
53
|
+
- evaluate_expression:计算数学表达式,涉及数值计算类 Rubric 必须先调用再判定。
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## 验证
|
|
57
|
+
|
|
58
|
+
已在 $OMB(OneMillion-Bench)判分链路实测通过:
|
|
59
|
+
- q_293 字数 rubric(超 2000 字扣分):模型调用 `text_stats` 实测 **2836 字符(CJK 2287)** 后精确判定,不再目测
|
|
60
|
+
- 无字数/数值类 rubric 的题目,模型按需判断不调用工具,正常判分
|
|
61
|
+
|
|
62
|
+
## 为什么自写
|
|
63
|
+
|
|
64
|
+
社区同类插件存在兼容性问题:
|
|
65
|
+
- `dsh-text-stats`:`cannot set property "textStats" without provide`(缺 provide 声明)
|
|
66
|
+
- `dsh-plugin-calculator`:依赖旧版 `@deepseek-ai/dsh-tools@0.1.1-rc.2`,与 DSH 0.2.1-alpha.3 不兼容
|
|
67
|
+
|
|
68
|
+
本插件按 DSH 0.2.1-alpha.3 的 `defineTool` API 编写,版本对齐。
|
|
69
|
+
|
|
70
|
+
## License
|
|
71
|
+
|
|
72
|
+
MIT
|
package/cordis.patch.yml
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-js-tools",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "DSH (DeepSeek Harness) 通用测量工具插件:text_stats 精确字数统计 + evaluate_expression 安全表达式求值,适配 DSH 0.2.1-alpha.3",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"dsh",
|
|
10
|
+
"deepseek-harness",
|
|
11
|
+
"plugin",
|
|
12
|
+
"judge",
|
|
13
|
+
"eval",
|
|
14
|
+
"text-stats",
|
|
15
|
+
"calculator",
|
|
16
|
+
"字数统计",
|
|
17
|
+
"判分"
|
|
18
|
+
],
|
|
19
|
+
"files": [
|
|
20
|
+
"src",
|
|
21
|
+
"cordis.patch.yml",
|
|
22
|
+
"README.md"
|
|
23
|
+
],
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@deepseek-ai/dsh-tools": "0.2.1-alpha.3"
|
|
26
|
+
},
|
|
27
|
+
"dsh": {
|
|
28
|
+
"bundle": {
|
|
29
|
+
"patch": "./cordis.patch.yml"
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
// dsh-judge-tools: $OMB 判分工具插件(适配 DSH 0.2.1-alpha.3)
|
|
2
|
+
// 注册两个工具:text_stats(字数统计)+ evaluate_expression(表达式求值)
|
|
3
|
+
import { defineTool } from "@deepseek-ai/dsh-tools";
|
|
4
|
+
|
|
5
|
+
export const name = "judge-tools";
|
|
6
|
+
export const inject = ["tools"];
|
|
7
|
+
|
|
8
|
+
/** CJK 判定(用于 token 估算) */
|
|
9
|
+
function isCjk(codePoint) {
|
|
10
|
+
return (
|
|
11
|
+
(codePoint >= 0x3400 && codePoint <= 0x4dbf) ||
|
|
12
|
+
(codePoint >= 0x4e00 && codePoint <= 0x9fff) ||
|
|
13
|
+
(codePoint >= 0xf900 && codePoint <= 0xfaff)
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function codePointLength(text) {
|
|
18
|
+
let n = 0;
|
|
19
|
+
for (const _ of text) n += 1;
|
|
20
|
+
return n;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** 估算 token:CJK 0.6/字符,其他 0.25/字符 */
|
|
24
|
+
function estimateTokens(text) {
|
|
25
|
+
let tokens = 0;
|
|
26
|
+
for (const ch of text) {
|
|
27
|
+
tokens += isCjk(ch.codePointAt(0)) ? 0.6 : 0.25;
|
|
28
|
+
}
|
|
29
|
+
return Math.ceil(tokens);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function computeStats(text) {
|
|
33
|
+
const chars = [...text].length;
|
|
34
|
+
const bytes = Buffer.byteLength(text, "utf8");
|
|
35
|
+
const lines = text.length === 0 ? 0 : text.split(/\r?\n/).length;
|
|
36
|
+
const nonEmptyLines = text.split(/\r?\n/).filter((l) => l.trim().length > 0).length;
|
|
37
|
+
const words = text.trim() ? text.trim().split(/\s+/).length : 0;
|
|
38
|
+
let cjk = 0;
|
|
39
|
+
let whitespace = 0;
|
|
40
|
+
for (const ch of text) {
|
|
41
|
+
const cp = ch.codePointAt(0);
|
|
42
|
+
if (/\s/.test(ch)) whitespace += 1;
|
|
43
|
+
else if (isCjk(cp)) cjk += 1;
|
|
44
|
+
}
|
|
45
|
+
const nonAscii = chars - [...text].filter((ch) => ch.codePointAt(0) < 0x80).length;
|
|
46
|
+
return {
|
|
47
|
+
characters: chars,
|
|
48
|
+
bytes,
|
|
49
|
+
lines,
|
|
50
|
+
nonEmptyLines,
|
|
51
|
+
words,
|
|
52
|
+
cjkCharacters: cjk,
|
|
53
|
+
whitespace,
|
|
54
|
+
nonAsciiCharacters: nonAscii,
|
|
55
|
+
estimatedTokens: estimateTokens(text),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** 安全表达式求值:只允许数字/运算符/括号/小数点,禁止任何标识符 */
|
|
60
|
+
function safeEval(expr) {
|
|
61
|
+
const cleaned = String(expr).trim();
|
|
62
|
+
if (!cleaned) throw new Error("表达式不能为空");
|
|
63
|
+
// 严格白名单:数字、四则、幂、括号、小数点、百分号、空格、逗号(千分位)
|
|
64
|
+
if (!/^[\d\s+\-*/().,%^]+$/.test(cleaned)) {
|
|
65
|
+
throw new Error(`表达式含非法字符(仅允许数字/四则/括号/幂/百分号): ${cleaned.slice(0, 40)}`);
|
|
66
|
+
}
|
|
67
|
+
// 防御:不允许连续运算符开头等明显非法形态
|
|
68
|
+
if (/[+\-*/^]{2,}/.test(cleaned.replace(/--/g, ""))) {
|
|
69
|
+
// 幂与负号可能冲突,宽松处理;交给 Function 兜底
|
|
70
|
+
}
|
|
71
|
+
// 用 Function 构造执行(输入已白名单过滤,无标识符可达)
|
|
72
|
+
// eslint-disable-next-line no-new-func
|
|
73
|
+
const fn = new Function(`"use strict"; return (${cleaned});`);
|
|
74
|
+
const value = fn();
|
|
75
|
+
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
76
|
+
throw new Error(`计算结果非法: ${value}`);
|
|
77
|
+
}
|
|
78
|
+
return value;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function renderNumber(v) {
|
|
82
|
+
// 保留最多 10 位有效数字,去掉浮点尾巴
|
|
83
|
+
const rounded = Math.abs(v) >= 1e15 || (Math.abs(v) < 1e-9 && v !== 0)
|
|
84
|
+
? v.toExponential(6)
|
|
85
|
+
: String(Math.round(v * 1e10) / 1e10);
|
|
86
|
+
return rounded;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function apply(ctx) {
|
|
90
|
+
ctx.on("ready", () => {
|
|
91
|
+
ctx.logger.info("[judge-tools] plugin ready");
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
ctx.tools.register(defineTool({
|
|
95
|
+
name: "text_stats",
|
|
96
|
+
description:
|
|
97
|
+
"统计一段文本的精确字数:字符数(含/不含空白)、字节数、行数、单词数、CJK 汉字数、估算 token 数。" +
|
|
98
|
+
"用于判分中涉及「字数上限/下限」「篇幅」「长度」类 Rubric 的精确判定——不要目测,先调用本工具拿到精确数字再下结论。" +
|
|
99
|
+
"超大文本(>10万字符)请先分段再逐段调用。",
|
|
100
|
+
parameters: {
|
|
101
|
+
text: {
|
|
102
|
+
type: "string",
|
|
103
|
+
required: true,
|
|
104
|
+
description: "要统计的文本内容(原样传入,不要截断)",
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
output: {
|
|
108
|
+
schema: {
|
|
109
|
+
type: "object",
|
|
110
|
+
additionalProperties: false,
|
|
111
|
+
properties: {
|
|
112
|
+
characters: { type: "integer" },
|
|
113
|
+
bytes: { type: "integer" },
|
|
114
|
+
lines: { type: "integer" },
|
|
115
|
+
nonEmptyLines: { type: "integer" },
|
|
116
|
+
words: { type: "integer" },
|
|
117
|
+
cjkCharacters: { type: "integer" },
|
|
118
|
+
whitespace: { type: "integer" },
|
|
119
|
+
nonAsciiCharacters: { type: "integer" },
|
|
120
|
+
estimatedTokens: { type: "integer" },
|
|
121
|
+
},
|
|
122
|
+
},
|
|
123
|
+
render: (_args, value) => [
|
|
124
|
+
{
|
|
125
|
+
type: "text",
|
|
126
|
+
text: `字符 ${value.characters}(CJK ${value.cjkCharacters})/ 行 ${value.lines} / 词 ${value.words} / 估算 ${value.estimatedTokens} token`,
|
|
127
|
+
},
|
|
128
|
+
],
|
|
129
|
+
},
|
|
130
|
+
isConcurrencySafe: () => true,
|
|
131
|
+
async execute(args) {
|
|
132
|
+
const text = String(args.text ?? "");
|
|
133
|
+
if (text.length === 0) throw new Error("text_stats: `text` 不能为空");
|
|
134
|
+
return computeStats(text);
|
|
135
|
+
},
|
|
136
|
+
presentCall: (args) => ({ card: "generic", title: "字数统计", kind: "other", rawInput: args }),
|
|
137
|
+
}));
|
|
138
|
+
|
|
139
|
+
ctx.tools.register(defineTool({
|
|
140
|
+
name: "evaluate_expression",
|
|
141
|
+
description:
|
|
142
|
+
"计算数学表达式,返回数值结果。支持四则运算、括号、幂(^)、百分号(%)。" +
|
|
143
|
+
"用于判分中涉及数字计算、比例换算、金额/权重计算的 Rubric 精确判定。" +
|
|
144
|
+
"例:'3 * 4 + 2'、'(85 + 90) / 2'、'7 * 30'、'1200 * 0.2'。",
|
|
145
|
+
parameters: {
|
|
146
|
+
expression: {
|
|
147
|
+
type: "string",
|
|
148
|
+
required: true,
|
|
149
|
+
description: "数学表达式(只支持数字、+ - * / ( ) ^ % 和空格)",
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
output: {
|
|
153
|
+
schema: {
|
|
154
|
+
type: "object",
|
|
155
|
+
additionalProperties: false,
|
|
156
|
+
properties: {
|
|
157
|
+
expression: { type: "string" },
|
|
158
|
+
result: { type: "string" },
|
|
159
|
+
},
|
|
160
|
+
},
|
|
161
|
+
render: (_args, value) => [
|
|
162
|
+
{ type: "text", text: `${value.expression} = ${value.result}` },
|
|
163
|
+
],
|
|
164
|
+
},
|
|
165
|
+
isConcurrencySafe: () => true,
|
|
166
|
+
async execute(args) {
|
|
167
|
+
const expr = String(args.expression ?? "");
|
|
168
|
+
const value = safeEval(expr);
|
|
169
|
+
return { expression: expr, result: renderNumber(value) };
|
|
170
|
+
},
|
|
171
|
+
presentCall: (args) => ({ card: "generic", title: "表达式计算", kind: "other", rawInput: args }),
|
|
172
|
+
}));
|
|
173
|
+
}
|