dsh-deepseek-web-login 0.5.3 → 0.6.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/CHANGELOG.md CHANGED
@@ -2,6 +2,48 @@
2
2
 
3
3
  本项目遵循大致语义化版本;日期为本地时间。
4
4
 
5
+ ## 0.6.0 — 2026-09-27
6
+
7
+ > 工具目录改用**紧凑类型签名**下发,不再给模型贴原始 JSON。
8
+
9
+ ### 改进
10
+
11
+ - **每个工具的参数不再原样贴 JSON**。原先那行是
12
+ `Parameters (JSON Schema): {"type":"object","properties":{…}}`,现在渲染成类型签名:
13
+
14
+ ```
15
+ ### read_file
16
+ Read a file.
17
+ read_file(file_path: string, offset?: number, limit?: number)
18
+ file_path: Path to read, resolved by the filesystem backend.
19
+ offset: 1-based first line to return. Defaults to 1.
20
+ ```
21
+
22
+ 模型要写出 `arguments`,真正需要的只是**参数名、类型、必填性**;那串 JSON 里
23
+ `"type":"…"`、键名的引号、每个参数各套一层对象,全是结构性样板。
24
+ - **两种参数形态都认**。DSH 自家工具是**扁平**写法(顶层键直接是参数名、`required: true`
25
+ 挂在参数自己身上),而 `@deepseek-ai/dsh-tools` 的 `schemaOf()` 把它**原样透传**、不做规范化;
26
+ 标准 JSON Schema 包裹形态(`{type:'object',properties:{…}}`)一并支持。
27
+ - 嵌套对象/数组/枚举/`anyOf` 都能渲染:`{content: string, status: string}[]`、
28
+ `"view" | "create" | "str_replace" | "insert"`。
29
+
30
+ ### 实测(2026-09-27,取自 13 个真实 DSH 工具定义)
31
+
32
+ - 参数段 **7,588 → 4,186 字符(省 45%)**,`required` 与参数描述全部保留、零回退。
33
+ - 折算到**整个工具目录约省 18%** —— 目录里更大的一块是工具级描述(同批样本 10,369 字符),
34
+ 那块**没动**:它藏的是"遇错怎么办"的指引,不许砍。
35
+ - 按 61 个工具 / 50,942 字符的目录估,约 **省 9,500 字符 ≈ 2,400 token**,占单轮输入 **3%** 左右。
36
+ ⇒ 别指望它解决"提示词太长":真正的大头是消息历史与环境注入,不在工具目录。
37
+
38
+ ### 说明 / 风险
39
+
40
+ - ⚠️ **这是 head 的变化**:升级后**第一个任务会全量重发一次**(投喂链断),之后照常。
41
+ - ⚠️ 模型看到的参数形态变了(JSON → 类型签名)。签名是模型最熟的形态,预期更好读;
42
+ 但**首次实测若发现参数写错,请立刻反馈** —— 回退只改一处:`buildToolSection` 里不用
43
+ `buildToolSignature` 即退回原始 JSON(回退分支与"认不出就不许发空参数"由
44
+ `tests/check-tool-signature.mjs` 守着)。
45
+ - 参数描述超过 160 字符会被截断;**工具级描述仍是 3200 上限,不受影响**。
46
+
5
47
  ## 0.5.3 — 2026-09-27
6
48
 
7
49
  > 修掉一个"重启后前 3 分钟里限流不换号"的窗口 —— 0.5.2 的漏洞。
package/lib/index.js CHANGED
@@ -3443,24 +3443,167 @@ function truncate(text, max) {
3443
3443
  if (text.length <= max) return text;
3444
3444
  return `${text.slice(0, max - 3)}...`;
3445
3445
  }
3446
- /** 渲染工具目录(含 JSON Schema)。 */
3446
+ /**
3447
+ * 参数级描述的截断长度。
3448
+ *
3449
+ * 紧凑签名保留的是「参数名 + 类型 + 必填性」—— 那是模型写出正确 arguments 的**最小信息集**。
3450
+ * 参数描述是锦上添花:第 160 字符之后通常是举例或边角说明(`path` 这类参数压根没有),
3451
+ * 砍掉它比砍掉工具级描述划算得多 —— 工具级描述里藏的是「遇错怎么办」。
3452
+ */
3453
+ const MAX_PARAM_DESC_CHARS = 160;
3454
+ /** 嵌套展开的深度上限:异常 schema(自引用、深层嵌套)不该把工具目录撑爆。 */
3455
+ const MAX_SIGNATURE_DEPTH = 4;
3456
+ /** `parameters` 是对象时才当映射用(`parameters: []` 这类异常值直接忽略)。 */
3457
+ function asRecord(value) {
3458
+ return value && typeof value === "object" && !Array.isArray(value) ? value : null;
3459
+ }
3460
+ /**
3461
+ * 「标准 JSON Schema 包裹」形态的顶层关键字。顶层**只**出现这些键 ⇒ 是
3462
+ * `{type:'object',properties:{…},required:[…]}` 包裹;出现别的键 ⇒ 那些键就是参数名。
3463
+ */
3464
+ const SCHEMA_TOP_KEYS = /* @__PURE__ */ new Set([
3465
+ "type",
3466
+ "properties",
3467
+ "required",
3468
+ "additionalProperties",
3469
+ "description",
3470
+ "title",
3471
+ "default",
3472
+ "examples",
3473
+ "$schema",
3474
+ "definitions",
3475
+ "$defs"
3476
+ ]);
3477
+ /**
3478
+ * `parameters` 是「标准包裹」还是「DSH 扁平写法」。
3479
+ *
3480
+ * ⚠️ 两种都得认(2026-09-27 读 `@deepseek-ai/dsh-tools/lib/index.js` 的 `schemaOf()` 确认):
3481
+ * 它把工具定义里的 `parameters` **原样透传**给 provider,不做任何规范化。而 DSH 自家工具
3482
+ * 清一色写**扁平**形态 —— 顶层键直接是参数名、`required: true` 写在参数自己身上:
3483
+ *
3484
+ * parameters: { command: { type: 'string', required: true, description: '…' },
3485
+ * description: { type: 'string', required: true, description: '…' } }
3486
+ *
3487
+ * 常见的标准包裹形态仍要支持(第三方工具可能按 OpenAI 惯例写)。
3488
+ */
3489
+ function isWrappedSchema(node) {
3490
+ const properties = asRecord(node.properties);
3491
+ if (!properties || Object.keys(properties).length === 0) return false;
3492
+ if (!Object.values(properties).every((value) => asRecord(value) !== null)) return false;
3493
+ return Object.keys(node).every((key) => SCHEMA_TOP_KEYS.has(key));
3494
+ }
3495
+ /** 参数自己身上的必填标记(DSH 扁平写法)。 */
3496
+ function isRequiredParam(def) {
3497
+ return asRecord(def)?.required === true;
3498
+ }
3499
+ /** 读参数描述并压平空白。 */
3500
+ function paramDescription(schema) {
3501
+ const description = asRecord(schema)?.description;
3502
+ return typeof description === "string" ? description.replace(/\s+/g, " ").trim() : "";
3503
+ }
3504
+ /**
3505
+ * 渲染**一层参数映射**(`{参数名: 定义}`)→ `['a: string', 'b?: number']`。
3506
+ *
3507
+ * ⚠️ `depth` 必须**由调用方透传**。第一版在这里硬编码了 `1`,于是每下沉一层深度就重置,
3508
+ * `MAX_SIGNATURE_DEPTH` 完全失效 —— 深层嵌套(甚至自引用 schema)会一路展开到栈溢出,
3509
+ * 再被 catch 吞掉,表现为"莫名其妙回退到原始 JSON"。
3510
+ */
3511
+ function renderParamMap(map, requiredList, depth) {
3512
+ const fromList = new Set(requiredList);
3513
+ return Object.entries(map).map(([name, def]) => {
3514
+ return `${name}${fromList.has(name) || isRequiredParam(def) ? "" : "?"}: ${renderParamType(def, depth)}`;
3515
+ });
3516
+ }
3517
+ /**
3518
+ * 渲染一个参数的类型表达式:`string` / `T[]` / `{a: T, b?: U}` / `"x" | "y"`。
3519
+ *
3520
+ * 认不出来的形态一律退化成 `any` —— 签名本身永远合法。只有整份 parameters
3521
+ * 都不敢降级时,才由 `buildToolSignature` 返回 null 让调用方回退到原始 JSON。
3522
+ */
3523
+ function renderParamType(schema, depth) {
3524
+ const node = asRecord(schema);
3525
+ if (!node) return "any";
3526
+ if (depth > MAX_SIGNATURE_DEPTH) return "any";
3527
+ if (Array.isArray(node.enum) && node.enum.length > 0) return node.enum.map((value) => JSON.stringify(value)).join(" | ");
3528
+ const variants = Array.isArray(node.anyOf) ? node.anyOf : Array.isArray(node.oneOf) ? node.oneOf : null;
3529
+ if (variants && variants.length > 0) {
3530
+ const rendered = variants.map((item) => renderParamType(item, depth + 1));
3531
+ return [...new Set(rendered)].join(" | ");
3532
+ }
3533
+ const declared = typeof node.type === "string" ? node.type : "";
3534
+ if (declared === "array" || !declared && node.items !== void 0) {
3535
+ const item = renderParamType(node.items, depth + 1);
3536
+ return item.includes("|") ? `(${item})[]` : `${item}[]`;
3537
+ }
3538
+ const properties = asRecord(node.properties);
3539
+ if (declared === "object" || properties) {
3540
+ const inner = properties ? renderParamMap(properties, [], depth + 1) : [];
3541
+ const extra = asRecord(node.additionalProperties);
3542
+ if (extra) inner.push(`[key: string]: ${renderParamType(extra, depth + 1)}`);
3543
+ if (inner.length === 0) return "object";
3544
+ return `{${inner.join(", ")}}`;
3545
+ }
3546
+ return declared || "any";
3547
+ }
3548
+ /**
3549
+ * 把工具参数渲染成紧凑签名(0.6.0):
3550
+ *
3551
+ * read_file(file_path: string, offset?: number, limit?: number)
3552
+ * file_path: Path to read, resolved by the filesystem backend.
3553
+ *
3554
+ * 为什么值得做:原先直接贴 `JSON.stringify(parameters)`。那串文本里**结构性样板**占了大头
3555
+ * —— 每个参数都要套一层 `{"type":"…","description":"…"}`、键名与类型值全带引号,
3556
+ * 而模型写出 arguments 真正需要的只是**参数名、类型、必填性**。
3557
+ *
3558
+ * ⚠️ 返回 `null` = "这份 parameters 不敢降级",调用方必须回退到原始 JSON:
3559
+ * 宁可多花字符,也不能让模型看不见参数。
3560
+ */
3561
+ function buildToolSignature(tool) {
3562
+ try {
3563
+ const node = asRecord(tool.parameters);
3564
+ if (!node) return `${tool.name}()`;
3565
+ const wrapped = isWrappedSchema(node);
3566
+ const map = wrapped ? asRecord(node.properties) : node;
3567
+ const args = renderParamMap(map, wrapped && Array.isArray(node.required) ? node.required.filter((key) => typeof key === "string") : [], 1);
3568
+ if (args.length === 0) return Object.keys(node).length === 0 ? `${tool.name}()` : null;
3569
+ const lines = [`${tool.name}(${args.join(", ")})`];
3570
+ for (const [name, def] of Object.entries(map)) {
3571
+ const description = paramDescription(def);
3572
+ if (description) lines.push(` ${name}: ${truncate(description, MAX_PARAM_DESC_CHARS)}`);
3573
+ }
3574
+ return lines.join("\n");
3575
+ } catch {
3576
+ return null;
3577
+ }
3578
+ }
3579
+ /**
3580
+ * 渲染工具目录。
3581
+ *
3582
+ * 每个工具占一段:`### 名字` / 工具描述(≤3200 字符,不轻易砍)/ **参数紧凑签名**。
3583
+ * 签名由 `buildToolSignature` 产出;它只在 schema 不敢降级时返回 null,那时才贴原始 JSON Schema。
3584
+ */
3447
3585
  function buildToolSection(tools, maxChars = MAX_TOOLS_SECTION_CHARS) {
3448
3586
  if (!tools || tools.length === 0) return "";
3449
3587
  const parts = ["", "## Available tools"];
3450
3588
  let budget = Math.max(0, Math.min(MAX_TOOLS_SECTION_CHARS, maxChars));
3451
3589
  for (let index = 0; index < tools.length; index += 1) {
3452
3590
  const tool = tools[index];
3453
- let schemaText = "";
3454
- try {
3455
- schemaText = JSON.stringify(tool.parameters ?? {});
3456
- } catch {
3457
- schemaText = "{}";
3591
+ const signature = buildToolSignature(tool);
3592
+ let parametersText = signature ?? "";
3593
+ if (signature === null) {
3594
+ let schemaText = "";
3595
+ try {
3596
+ schemaText = JSON.stringify(tool.parameters ?? {});
3597
+ } catch {
3598
+ schemaText = "{}";
3599
+ }
3600
+ parametersText = `Parameters (JSON Schema): ${schemaText}`;
3458
3601
  }
3459
3602
  const block = [
3460
3603
  "",
3461
3604
  `### ${tool.name}`,
3462
3605
  truncate(String(tool.description ?? "").replace(/\s+/g, " ").trim(), MAX_DESCRIPTION_CHARS),
3463
- `Parameters (JSON Schema): ${schemaText}`
3606
+ parametersText
3464
3607
  ].join("\n");
3465
3608
  if (budget - block.length < 0) {
3466
3609
  const rest = tools.slice(index).map((item) => String(item?.name ?? "")).filter(Boolean);
@@ -8648,7 +8791,7 @@ async function checkForUpdate(current, fetchImpl) {
8648
8791
  * 兜底常量与 package.json 的一致性由 `tests/check-smoke.mjs` 守着,不会漂。
8649
8792
  */
8650
8793
  /** 与 package.json 保持一致的兜底版本(由测试保证不会漂)。 */
8651
- const FALLBACK_VERSION = "0.5.3";
8794
+ const FALLBACK_VERSION = "0.6.0";
8652
8795
  let cached;
8653
8796
  /** 本插件版本(如 `0.1.26`)。 */
8654
8797
  function pluginVersion() {