@zifeng1023/dsh-bot 0.3.4 → 0.3.5
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 +17 -4
- package/dsh-plugin.json +13 -5
- package/lib/client.js +4351 -4159
- package/lib/index.js +403 -48
- package/package.json +88 -90
package/lib/index.js
CHANGED
|
@@ -2,6 +2,7 @@ import crypto from "node:crypto";
|
|
|
2
2
|
import fs from "node:fs";
|
|
3
3
|
import os from "node:os";
|
|
4
4
|
import path from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
5
6
|
import fsp from "node:fs/promises";
|
|
6
7
|
import { defineTool } from "@deepseek-ai/dsh-tools";
|
|
7
8
|
|
|
@@ -813,7 +814,7 @@ const FREE_PATTERNS = [
|
|
|
813
814
|
* 高峰价是空闲价的 2 倍,所以这个判断直接影响账单。
|
|
814
815
|
*/
|
|
815
816
|
function isPeakHour(now = /* @__PURE__ */ new Date()) {
|
|
816
|
-
const bj =
|
|
817
|
+
const bj = toZonedDate(now, "Asia/Shanghai");
|
|
817
818
|
const day = bj.getDay();
|
|
818
819
|
if (day === 0 || day === 6) return false;
|
|
819
820
|
const minutes = bj.getHours() * 60 + bj.getMinutes();
|
|
@@ -1204,12 +1205,29 @@ function createToolInvoker(ctx) {
|
|
|
1204
1205
|
const tools = ctx?.tools;
|
|
1205
1206
|
if (typeof tools?.call === "function") return unwrapToolResult(await tools.call(name$1, args, signal ? { signal } : void 0));
|
|
1206
1207
|
if (typeof tools?.execute === "function") {
|
|
1207
|
-
const
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1208
|
+
const sig = signal ?? new AbortController().signal;
|
|
1209
|
+
try {
|
|
1210
|
+
const batch = await tools.execute({
|
|
1211
|
+
calls: [{
|
|
1212
|
+
type: "tool-call",
|
|
1213
|
+
id: `dsh-bot-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`,
|
|
1214
|
+
name: name$1,
|
|
1215
|
+
arguments: JSON.stringify(args ?? {})
|
|
1216
|
+
}],
|
|
1217
|
+
signal: sig
|
|
1218
|
+
});
|
|
1219
|
+
return unwrapToolResult(Array.isArray(batch?.results) ? batch.results[0]?.result : batch);
|
|
1220
|
+
} catch (batchErr) {
|
|
1221
|
+
try {
|
|
1222
|
+
return unwrapToolResult(await tools.execute({
|
|
1223
|
+
name: name$1,
|
|
1224
|
+
arguments: args ?? {},
|
|
1225
|
+
signal: sig
|
|
1226
|
+
}));
|
|
1227
|
+
} catch {
|
|
1228
|
+
throw batchErr;
|
|
1229
|
+
}
|
|
1230
|
+
}
|
|
1213
1231
|
}
|
|
1214
1232
|
throw new Error(`当前 DSH 运行环境不支持程序化调用工具:${name$1}`);
|
|
1215
1233
|
}
|
|
@@ -1218,6 +1236,7 @@ function createToolInvoker(ctx) {
|
|
|
1218
1236
|
/** 工具返回可能是 { content, value } 形态,尽量取 value */
|
|
1219
1237
|
function unwrapToolResult(res) {
|
|
1220
1238
|
if (res && typeof res === "object") {
|
|
1239
|
+
if (Array.isArray(res.results)) return unwrapToolResult(res.results[0]?.result);
|
|
1221
1240
|
if ("value" in res && res.value !== void 0) return res.value;
|
|
1222
1241
|
if ("content" in res) return res.content;
|
|
1223
1242
|
}
|
|
@@ -2676,7 +2695,7 @@ function inQuietHours(quiet, now = /* @__PURE__ */ new Date()) {
|
|
|
2676
2695
|
const [h, m] = s.split(":").map(Number);
|
|
2677
2696
|
return h * 60 + m;
|
|
2678
2697
|
};
|
|
2679
|
-
const bj =
|
|
2698
|
+
const bj = toZonedDate(now, "Asia/Shanghai");
|
|
2680
2699
|
const cur = bj.getHours() * 60 + bj.getMinutes();
|
|
2681
2700
|
const start = toMin(quiet.start);
|
|
2682
2701
|
const end = toMin(quiet.end);
|
|
@@ -4747,27 +4766,60 @@ const MAX_TOOL_ROUNDS = 8;
|
|
|
4747
4766
|
*/
|
|
4748
4767
|
function parseToolCallMarkers(text) {
|
|
4749
4768
|
const calls = [];
|
|
4769
|
+
let unparsed = 0;
|
|
4750
4770
|
return {
|
|
4751
4771
|
calls,
|
|
4752
|
-
cleaned: text.replace(
|
|
4753
|
-
|
|
4754
|
-
|
|
4755
|
-
|
|
4756
|
-
|
|
4772
|
+
cleaned: text.replace(/\`\`\`[\w-]*[ \t]*\r?\n([\s\S]*?)\`\`\`/g, (full, body) => {
|
|
4773
|
+
const trimmed = String(body).trim();
|
|
4774
|
+
if (!trimmed) return full;
|
|
4775
|
+
const whole = tryParseJson(trimmed);
|
|
4776
|
+
if (whole.ok) {
|
|
4777
|
+
if ((Array.isArray(whole.value) ? whole.value : [whole.value]).map((o) => pushCall(o, calls)).filter(Boolean).length > 0) return "";
|
|
4757
4778
|
return full;
|
|
4758
4779
|
}
|
|
4759
|
-
|
|
4760
|
-
const
|
|
4761
|
-
|
|
4762
|
-
|
|
4763
|
-
|
|
4764
|
-
|
|
4765
|
-
|
|
4766
|
-
|
|
4767
|
-
|
|
4768
|
-
|
|
4780
|
+
const lineItems = [];
|
|
4781
|
+
for (const line of trimmed.split(/\r?\n/)) {
|
|
4782
|
+
const s = line.trim().replace(/,$/, "");
|
|
4783
|
+
if (!s) continue;
|
|
4784
|
+
const one = tryParseJson(s);
|
|
4785
|
+
if (!one.ok) {
|
|
4786
|
+
lineItems.length = 0;
|
|
4787
|
+
break;
|
|
4788
|
+
}
|
|
4789
|
+
if (Array.isArray(one.value)) lineItems.push(...one.value);
|
|
4790
|
+
else lineItems.push(one.value);
|
|
4791
|
+
}
|
|
4792
|
+
if (lineItems.map((o) => pushCall(o, calls)).filter(Boolean).length > 0) return "";
|
|
4793
|
+
if (looksLikeToolCall(trimmed)) unparsed++;
|
|
4794
|
+
return full;
|
|
4795
|
+
}).trim(),
|
|
4796
|
+
unparsed
|
|
4769
4797
|
};
|
|
4770
4798
|
}
|
|
4799
|
+
function tryParseJson(s) {
|
|
4800
|
+
try {
|
|
4801
|
+
return {
|
|
4802
|
+
ok: true,
|
|
4803
|
+
value: JSON.parse(s)
|
|
4804
|
+
};
|
|
4805
|
+
} catch {
|
|
4806
|
+
return { ok: false };
|
|
4807
|
+
}
|
|
4808
|
+
}
|
|
4809
|
+
function pushCall(obj, out) {
|
|
4810
|
+
if (!obj || typeof obj !== "object" || Array.isArray(obj)) return false;
|
|
4811
|
+
const o = obj;
|
|
4812
|
+
const name$1 = typeof o.tool === "string" ? o.tool : typeof o.name === "string" ? o.name : void 0;
|
|
4813
|
+
if (!name$1 || /\s/.test(name$1)) return false;
|
|
4814
|
+
out.push({
|
|
4815
|
+
name: name$1,
|
|
4816
|
+
args: o.args ?? o.input ?? o.arguments ?? {}
|
|
4817
|
+
});
|
|
4818
|
+
return true;
|
|
4819
|
+
}
|
|
4820
|
+
function looksLikeToolCall(body) {
|
|
4821
|
+
return /"tool"\s*:/.test(body) || /"name"\s*:/.test(body);
|
|
4822
|
+
}
|
|
4771
4823
|
/** 把工具目录渲染进系统提示(每行截断,防 prompt 失控) */
|
|
4772
4824
|
function renderToolDirectory(tools) {
|
|
4773
4825
|
return tools.map((t) => {
|
|
@@ -5222,6 +5274,8 @@ var RoutineRunner = class {
|
|
|
5222
5274
|
const native = out.toolUses ?? [];
|
|
5223
5275
|
let uses;
|
|
5224
5276
|
let cleanedText;
|
|
5277
|
+
/** 本轮「看起来有工具调用、但解析器没认出任何调用」的块数 */
|
|
5278
|
+
let unparsedBlocks = 0;
|
|
5225
5279
|
if (native.length > 0) {
|
|
5226
5280
|
uses = native.map((u) => ({
|
|
5227
5281
|
id: u.id,
|
|
@@ -5236,8 +5290,16 @@ var RoutineRunner = class {
|
|
|
5236
5290
|
input: c.args
|
|
5237
5291
|
}));
|
|
5238
5292
|
cleanedText = parsed.cleaned;
|
|
5293
|
+
unparsedBlocks = parsed.unparsed;
|
|
5239
5294
|
}
|
|
5240
5295
|
if (uses.length === 0) {
|
|
5296
|
+
if (unparsedBlocks > 0) {
|
|
5297
|
+
record.status = "failed";
|
|
5298
|
+
record.error = {
|
|
5299
|
+
code: "UNPARSED_TOOL_CALL",
|
|
5300
|
+
message: "模型产出了工具块的语法,但解析器没有认出任何调用,本轮实际什么都没执行。常见原因:一个代码块里写了多个 JSON 对象,或 JSON 非法。"
|
|
5301
|
+
};
|
|
5302
|
+
}
|
|
5241
5303
|
finalText = cleanedText;
|
|
5242
5304
|
break;
|
|
5243
5305
|
}
|
|
@@ -5663,6 +5725,40 @@ function toErrorPayload(e) {
|
|
|
5663
5725
|
};
|
|
5664
5726
|
}
|
|
5665
5727
|
/**
|
|
5728
|
+
* 错误码 → HTTP status(v0.3.5 刀 4)
|
|
5729
|
+
*
|
|
5730
|
+
* 响应体结构完全不变(仍是 { ok:false, code, message }),只改 status。
|
|
5731
|
+
* 面板侧都是「先解析 body 再判 ok」,因此兼容;好处是监控/重试能按状态码工作。
|
|
5732
|
+
*/
|
|
5733
|
+
const STATUS_BY_CODE = {
|
|
5734
|
+
INVALID_ARGS: 400,
|
|
5735
|
+
INVALID_ROUTINE: 400,
|
|
5736
|
+
UNSUPPORTED_STEP: 400,
|
|
5737
|
+
ASSERT_FAILED: 400,
|
|
5738
|
+
GOAL_INVALID_TRANSITION: 400,
|
|
5739
|
+
GOAL_FORBIDDEN_WRITE: 400,
|
|
5740
|
+
UNKNOWN_TOOL: 400,
|
|
5741
|
+
PRECHECK_FAILED: 400,
|
|
5742
|
+
CONFIRM_REQUIRED: 400,
|
|
5743
|
+
BOT_NOT_FOUND: 404,
|
|
5744
|
+
GOAL_NOT_FOUND: 404,
|
|
5745
|
+
ROUTINE_NOT_FOUND: 404,
|
|
5746
|
+
BOT_EXISTS: 409,
|
|
5747
|
+
GOAL_ALREADY_EXISTS: 409,
|
|
5748
|
+
RUN_IN_PROGRESS: 409,
|
|
5749
|
+
DRY_RUN_BLOCKED: 409,
|
|
5750
|
+
BUDGET_EXCEEDED: 409,
|
|
5751
|
+
SANDBOX_VIOLATION: 403
|
|
5752
|
+
};
|
|
5753
|
+
function statusForCode(code) {
|
|
5754
|
+
return STATUS_BY_CODE[code] ?? 500;
|
|
5755
|
+
}
|
|
5756
|
+
/** 错误响应:status 按错误码映射,body 与旧版逐字节一致 */
|
|
5757
|
+
function writeError(res, e) {
|
|
5758
|
+
const payload = toErrorPayload(e);
|
|
5759
|
+
writeJson(res, statusForCode(payload.code), payload);
|
|
5760
|
+
}
|
|
5761
|
+
/**
|
|
5666
5762
|
* 构造全部路由。
|
|
5667
5763
|
*
|
|
5668
5764
|
* @param service BotService 实例
|
|
@@ -5713,7 +5809,7 @@ function makeBotApiRoutes(service, runAction) {
|
|
|
5713
5809
|
}
|
|
5714
5810
|
});
|
|
5715
5811
|
} catch (e) {
|
|
5716
|
-
|
|
5812
|
+
writeError(res, e);
|
|
5717
5813
|
}
|
|
5718
5814
|
}
|
|
5719
5815
|
},
|
|
@@ -5768,7 +5864,7 @@ function makeBotApiRoutes(service, runAction) {
|
|
|
5768
5864
|
}
|
|
5769
5865
|
});
|
|
5770
5866
|
} catch (e) {
|
|
5771
|
-
|
|
5867
|
+
writeError(res, e);
|
|
5772
5868
|
}
|
|
5773
5869
|
}
|
|
5774
5870
|
},
|
|
@@ -5801,7 +5897,7 @@ function makeBotApiRoutes(service, runAction) {
|
|
|
5801
5897
|
})
|
|
5802
5898
|
});
|
|
5803
5899
|
} catch (e) {
|
|
5804
|
-
|
|
5900
|
+
writeError(res, e);
|
|
5805
5901
|
}
|
|
5806
5902
|
}
|
|
5807
5903
|
},
|
|
@@ -5843,7 +5939,7 @@ function makeBotApiRoutes(service, runAction) {
|
|
|
5843
5939
|
else if (hit.models && hit.models.length > 0 && !hit.models.includes(bot.model.model)) problems.push(`provider「${bot.model.provider}」下没有模型「${bot.model.model}」(可用:${hit.models.join(", ")})`);
|
|
5844
5940
|
}
|
|
5845
5941
|
if (problems.length > 0) {
|
|
5846
|
-
writeJson(res,
|
|
5942
|
+
writeJson(res, 400, {
|
|
5847
5943
|
ok: false,
|
|
5848
5944
|
code: "PRECHECK_FAILED",
|
|
5849
5945
|
message: "执行前检查未通过:\n· " + problems.join("\n· ")
|
|
@@ -5861,7 +5957,7 @@ function makeBotApiRoutes(service, runAction) {
|
|
|
5861
5957
|
})
|
|
5862
5958
|
});
|
|
5863
5959
|
} catch (e) {
|
|
5864
|
-
|
|
5960
|
+
writeError(res, e);
|
|
5865
5961
|
}
|
|
5866
5962
|
}
|
|
5867
5963
|
},
|
|
@@ -5899,7 +5995,7 @@ function makeBotApiRoutes(service, runAction) {
|
|
|
5899
5995
|
value: await runAction.rollback(body.botId, body.snapshotId)
|
|
5900
5996
|
});
|
|
5901
5997
|
} catch (e) {
|
|
5902
|
-
|
|
5998
|
+
writeError(res, e);
|
|
5903
5999
|
}
|
|
5904
6000
|
}
|
|
5905
6001
|
},
|
|
@@ -5929,7 +6025,7 @@ function makeBotApiRoutes(service, runAction) {
|
|
|
5929
6025
|
value: await runAction.snapshot(body.botId)
|
|
5930
6026
|
});
|
|
5931
6027
|
} catch (e) {
|
|
5932
|
-
|
|
6028
|
+
writeError(res, e);
|
|
5933
6029
|
}
|
|
5934
6030
|
}
|
|
5935
6031
|
},
|
|
@@ -5959,7 +6055,7 @@ function makeBotApiRoutes(service, runAction) {
|
|
|
5959
6055
|
value: await actOnGoal(service, body.botId, "accept", body)
|
|
5960
6056
|
});
|
|
5961
6057
|
} catch (e) {
|
|
5962
|
-
|
|
6058
|
+
writeError(res, e);
|
|
5963
6059
|
}
|
|
5964
6060
|
}
|
|
5965
6061
|
},
|
|
@@ -5989,7 +6085,7 @@ function makeBotApiRoutes(service, runAction) {
|
|
|
5989
6085
|
value: await actOnGoal(service, body.botId, "dismiss", body)
|
|
5990
6086
|
});
|
|
5991
6087
|
} catch (e) {
|
|
5992
|
-
|
|
6088
|
+
writeError(res, e);
|
|
5993
6089
|
}
|
|
5994
6090
|
}
|
|
5995
6091
|
},
|
|
@@ -6027,7 +6123,7 @@ function makeBotApiRoutes(service, runAction) {
|
|
|
6027
6123
|
value: await actOnGoal(service, body.botId, "confirm", body)
|
|
6028
6124
|
});
|
|
6029
6125
|
} catch (e) {
|
|
6030
|
-
|
|
6126
|
+
writeError(res, e);
|
|
6031
6127
|
}
|
|
6032
6128
|
}
|
|
6033
6129
|
},
|
|
@@ -6057,7 +6153,7 @@ function makeBotApiRoutes(service, runAction) {
|
|
|
6057
6153
|
value: await actOnGoal(service, body.botId, "reject", body)
|
|
6058
6154
|
});
|
|
6059
6155
|
} catch (e) {
|
|
6060
|
-
|
|
6156
|
+
writeError(res, e);
|
|
6061
6157
|
}
|
|
6062
6158
|
}
|
|
6063
6159
|
},
|
|
@@ -6087,7 +6183,7 @@ function makeBotApiRoutes(service, runAction) {
|
|
|
6087
6183
|
value: await actOnGoal(service, body.botId, "set", body)
|
|
6088
6184
|
});
|
|
6089
6185
|
} catch (e) {
|
|
6090
|
-
|
|
6186
|
+
writeError(res, e);
|
|
6091
6187
|
}
|
|
6092
6188
|
}
|
|
6093
6189
|
},
|
|
@@ -6125,7 +6221,7 @@ function makeBotApiRoutes(service, runAction) {
|
|
|
6125
6221
|
value: await actOnGoal(service, body.botId, "abandon", body)
|
|
6126
6222
|
});
|
|
6127
6223
|
} catch (e) {
|
|
6128
|
-
|
|
6224
|
+
writeError(res, e);
|
|
6129
6225
|
}
|
|
6130
6226
|
}
|
|
6131
6227
|
},
|
|
@@ -6155,7 +6251,7 @@ function makeBotApiRoutes(service, runAction) {
|
|
|
6155
6251
|
value: { archives: await service.listGoalArchives(botId) }
|
|
6156
6252
|
});
|
|
6157
6253
|
} catch (e) {
|
|
6158
|
-
|
|
6254
|
+
writeError(res, e);
|
|
6159
6255
|
}
|
|
6160
6256
|
}
|
|
6161
6257
|
},
|
|
@@ -6197,7 +6293,7 @@ function makeBotApiRoutes(service, runAction) {
|
|
|
6197
6293
|
value: bot
|
|
6198
6294
|
});
|
|
6199
6295
|
} catch (e) {
|
|
6200
|
-
|
|
6296
|
+
writeError(res, e);
|
|
6201
6297
|
}
|
|
6202
6298
|
}
|
|
6203
6299
|
},
|
|
@@ -6236,7 +6332,7 @@ function makeBotApiRoutes(service, runAction) {
|
|
|
6236
6332
|
value: { deleted: true }
|
|
6237
6333
|
});
|
|
6238
6334
|
} catch (e) {
|
|
6239
|
-
|
|
6335
|
+
writeError(res, e);
|
|
6240
6336
|
}
|
|
6241
6337
|
}
|
|
6242
6338
|
},
|
|
@@ -6260,7 +6356,7 @@ function makeBotApiRoutes(service, runAction) {
|
|
|
6260
6356
|
}
|
|
6261
6357
|
});
|
|
6262
6358
|
} catch (e) {
|
|
6263
|
-
|
|
6359
|
+
writeError(res, e);
|
|
6264
6360
|
}
|
|
6265
6361
|
}
|
|
6266
6362
|
},
|
|
@@ -6304,7 +6400,7 @@ function makeBotApiRoutes(service, runAction) {
|
|
|
6304
6400
|
} })
|
|
6305
6401
|
});
|
|
6306
6402
|
} catch (e) {
|
|
6307
|
-
|
|
6403
|
+
writeError(res, e);
|
|
6308
6404
|
}
|
|
6309
6405
|
}
|
|
6310
6406
|
},
|
|
@@ -6339,7 +6435,7 @@ function makeBotApiRoutes(service, runAction) {
|
|
|
6339
6435
|
value: await runAction.saveSettings?.(target, body?.settings ?? {}) ?? null
|
|
6340
6436
|
});
|
|
6341
6437
|
} catch (e) {
|
|
6342
|
-
|
|
6438
|
+
writeError(res, e);
|
|
6343
6439
|
}
|
|
6344
6440
|
}
|
|
6345
6441
|
},
|
|
@@ -6419,7 +6515,7 @@ function makeBotApiRoutes(service, runAction) {
|
|
|
6419
6515
|
}
|
|
6420
6516
|
});
|
|
6421
6517
|
} catch (e) {
|
|
6422
|
-
|
|
6518
|
+
writeError(res, e);
|
|
6423
6519
|
}
|
|
6424
6520
|
}
|
|
6425
6521
|
},
|
|
@@ -6457,7 +6553,7 @@ function makeBotApiRoutes(service, runAction) {
|
|
|
6457
6553
|
value: { removed: await service.clearRuns(String(body.botId)) }
|
|
6458
6554
|
});
|
|
6459
6555
|
} catch (e) {
|
|
6460
|
-
|
|
6556
|
+
writeError(res, e);
|
|
6461
6557
|
}
|
|
6462
6558
|
}
|
|
6463
6559
|
},
|
|
@@ -6487,7 +6583,7 @@ function makeBotApiRoutes(service, runAction) {
|
|
|
6487
6583
|
value: { deleted: await service.deleteRun(String(body.botId), String(body.runId)) }
|
|
6488
6584
|
});
|
|
6489
6585
|
} catch (e) {
|
|
6490
|
-
|
|
6586
|
+
writeError(res, e);
|
|
6491
6587
|
}
|
|
6492
6588
|
}
|
|
6493
6589
|
},
|
|
@@ -6526,7 +6622,7 @@ function makeBotApiRoutes(service, runAction) {
|
|
|
6526
6622
|
value: next
|
|
6527
6623
|
});
|
|
6528
6624
|
} catch (e) {
|
|
6529
|
-
|
|
6625
|
+
writeError(res, e);
|
|
6530
6626
|
}
|
|
6531
6627
|
}
|
|
6532
6628
|
},
|
|
@@ -6563,7 +6659,7 @@ function makeBotApiRoutes(service, runAction) {
|
|
|
6563
6659
|
}
|
|
6564
6660
|
});
|
|
6565
6661
|
} catch (e) {
|
|
6566
|
-
|
|
6662
|
+
writeError(res, e);
|
|
6567
6663
|
}
|
|
6568
6664
|
}
|
|
6569
6665
|
},
|
|
@@ -6629,6 +6725,38 @@ function makeBotApiRoutes(service, runAction) {
|
|
|
6629
6725
|
}, null, 2), "utf8");
|
|
6630
6726
|
} catch {}
|
|
6631
6727
|
}
|
|
6728
|
+
},
|
|
6729
|
+
{
|
|
6730
|
+
kind: "exact",
|
|
6731
|
+
path: `${API_PREFIX}/doctor`,
|
|
6732
|
+
handler: async (_req, res) => {
|
|
6733
|
+
try {
|
|
6734
|
+
if (!runAction.doctor) {
|
|
6735
|
+
writeJson(res, 200, {
|
|
6736
|
+
ok: true,
|
|
6737
|
+
value: {
|
|
6738
|
+
ok: true,
|
|
6739
|
+
checks: [],
|
|
6740
|
+
text: "诊断未启用(插件入口未注入)"
|
|
6741
|
+
}
|
|
6742
|
+
});
|
|
6743
|
+
return;
|
|
6744
|
+
}
|
|
6745
|
+
writeJson(res, 200, {
|
|
6746
|
+
ok: true,
|
|
6747
|
+
value: await runAction.doctor()
|
|
6748
|
+
});
|
|
6749
|
+
} catch (e) {
|
|
6750
|
+
writeJson(res, 200, {
|
|
6751
|
+
ok: true,
|
|
6752
|
+
value: {
|
|
6753
|
+
ok: true,
|
|
6754
|
+
checks: [],
|
|
6755
|
+
text: `诊断自身失败:${e instanceof Error ? e.message : String(e)}`
|
|
6756
|
+
}
|
|
6757
|
+
});
|
|
6758
|
+
}
|
|
6759
|
+
}
|
|
6632
6760
|
}
|
|
6633
6761
|
].map((r) => ({
|
|
6634
6762
|
...r,
|
|
@@ -6725,6 +6853,192 @@ async function archiveGoalFor(service, botId, goal, outcome) {
|
|
|
6725
6853
|
return archive;
|
|
6726
6854
|
}
|
|
6727
6855
|
|
|
6856
|
+
//#endregion
|
|
6857
|
+
//#region src/doctor.ts
|
|
6858
|
+
const LEVEL_LABEL = {
|
|
6859
|
+
ok: "正常",
|
|
6860
|
+
warn: "注意",
|
|
6861
|
+
error: "故障"
|
|
6862
|
+
};
|
|
6863
|
+
function readJson(file) {
|
|
6864
|
+
try {
|
|
6865
|
+
return JSON.parse(fs.readFileSync(file, "utf8"));
|
|
6866
|
+
} catch {
|
|
6867
|
+
return null;
|
|
6868
|
+
}
|
|
6869
|
+
}
|
|
6870
|
+
function safeString(v, max = 200) {
|
|
6871
|
+
const s = typeof v === "string" ? v : v === void 0 || v === null ? "" : String(v);
|
|
6872
|
+
return s.length > max ? s.slice(0, max) + "…" : s;
|
|
6873
|
+
}
|
|
6874
|
+
async function buildDoctorReport(input) {
|
|
6875
|
+
const checks = [];
|
|
6876
|
+
const loadedPath = path.join(input.homeDir, ".plugin-loaded.json");
|
|
6877
|
+
const statusPath = path.join(input.homeDir, ".client-status.json");
|
|
6878
|
+
const crashPath = path.join(input.homeDir, ".plugin-crash.json");
|
|
6879
|
+
try {
|
|
6880
|
+
const j = readJson(loadedPath);
|
|
6881
|
+
if (!j) checks.push({
|
|
6882
|
+
id: "host",
|
|
6883
|
+
level: "error",
|
|
6884
|
+
title: "宿主注册",
|
|
6885
|
+
detail: "没有找到 .plugin-loaded.json:宿主侧没有加载过这个插件。",
|
|
6886
|
+
hint: "重启 DSH;仍无效则确认插件在「插件」页已启用,并检查 ~/.dsh/bot 目录是否可写。"
|
|
6887
|
+
});
|
|
6888
|
+
else {
|
|
6889
|
+
const count = Number(j.registeredCount ?? 0);
|
|
6890
|
+
const regErrors = Array.isArray(j.registerErrors) ? j.registerErrors : [];
|
|
6891
|
+
const llmReady = j.llmReady !== false;
|
|
6892
|
+
const hasWeb = j.hasWebServer !== false;
|
|
6893
|
+
let level = "ok";
|
|
6894
|
+
const problems = [];
|
|
6895
|
+
if (regErrors.length > 0) {
|
|
6896
|
+
level = "error";
|
|
6897
|
+
problems.push(`${regErrors.length} 个注册错误`);
|
|
6898
|
+
}
|
|
6899
|
+
if (!llmReady) {
|
|
6900
|
+
level = "error";
|
|
6901
|
+
problems.push("模型服务未就绪(llmReady=false)");
|
|
6902
|
+
}
|
|
6903
|
+
if (!hasWeb) {
|
|
6904
|
+
level = "error";
|
|
6905
|
+
problems.push("webServer 不可用,面板 API 无法注册");
|
|
6906
|
+
}
|
|
6907
|
+
if (count !== input.expectedTools && level === "ok") {
|
|
6908
|
+
level = "warn";
|
|
6909
|
+
problems.push(`工具数 ${count}/${input.expectedTools}`);
|
|
6910
|
+
}
|
|
6911
|
+
checks.push({
|
|
6912
|
+
id: "host",
|
|
6913
|
+
level,
|
|
6914
|
+
title: "宿主注册",
|
|
6915
|
+
detail: `${count}/${input.expectedTools} 个工具 · llmReady=${llmReady} · 路由 ${j.apiRoutes ?? "?"} · 加载于 ${safeString(j.loadedAt, 40) || "未知"}` + (problems.length ? ` · ${problems.join(";")}` : ""),
|
|
6916
|
+
hint: level === "ok" ? void 0 : regErrors.length > 0 ? `先看 registerErrors:${safeString(JSON.stringify(regErrors[0]))}` : !llmReady ? "检查 DSH 的模型渠道配置;模型服务不可用时「执行」会返回空结果。" : `工具数不符:确认插件版本与宿主版本匹配(当前 ${input.version})。`
|
|
6917
|
+
});
|
|
6918
|
+
}
|
|
6919
|
+
} catch (e) {
|
|
6920
|
+
checks.push({
|
|
6921
|
+
id: "host",
|
|
6922
|
+
level: "warn",
|
|
6923
|
+
title: "宿主注册",
|
|
6924
|
+
detail: `读取失败:${safeString(e?.message)}`
|
|
6925
|
+
});
|
|
6926
|
+
}
|
|
6927
|
+
try {
|
|
6928
|
+
const j = readJson(statusPath);
|
|
6929
|
+
const history = Array.isArray(j?.history) ? j.history : [];
|
|
6930
|
+
const last = history[history.length - 1];
|
|
6931
|
+
if (!last) checks.push({
|
|
6932
|
+
id: "client",
|
|
6933
|
+
level: "warn",
|
|
6934
|
+
title: "客户端面板",
|
|
6935
|
+
detail: "浏览器端从未上报过加载状态。",
|
|
6936
|
+
hint: "刷新页面;若面板仍然不见,重装插件(可能是 client bundle 未随版本更新)。"
|
|
6937
|
+
});
|
|
6938
|
+
else {
|
|
6939
|
+
const status = safeString(last.status, 60) || "unknown";
|
|
6940
|
+
const version = typeof last.version === "string" ? last.version : null;
|
|
6941
|
+
const stale = version !== null && input.version !== "unknown" && version !== input.version;
|
|
6942
|
+
const bad = /error|fail/i.test(status);
|
|
6943
|
+
const level = bad || stale ? "warn" : "ok";
|
|
6944
|
+
checks.push({
|
|
6945
|
+
id: "client",
|
|
6946
|
+
level,
|
|
6947
|
+
title: "客户端面板",
|
|
6948
|
+
detail: `最近上报:${status} · 版本 ${version ?? "未上报"} · ${safeString(last.at, 40) || "未知时间"}`,
|
|
6949
|
+
hint: stale ? `面板跑的构建是 ${version},当前是 ${input.version}:重装插件并刷新页面。` : bad ? "面板加载过程报过错,看 detail 字段;必要时强制刷新页面。" : void 0
|
|
6950
|
+
});
|
|
6951
|
+
}
|
|
6952
|
+
} catch (e) {
|
|
6953
|
+
checks.push({
|
|
6954
|
+
id: "client",
|
|
6955
|
+
level: "warn",
|
|
6956
|
+
title: "客户端面板",
|
|
6957
|
+
detail: `读取失败:${safeString(e?.message)}`
|
|
6958
|
+
});
|
|
6959
|
+
}
|
|
6960
|
+
try {
|
|
6961
|
+
const j = readJson(crashPath);
|
|
6962
|
+
if (!j) checks.push({
|
|
6963
|
+
id: "crash",
|
|
6964
|
+
level: "ok",
|
|
6965
|
+
title: "启动崩溃",
|
|
6966
|
+
detail: "没有崩溃记录。"
|
|
6967
|
+
});
|
|
6968
|
+
else checks.push({
|
|
6969
|
+
id: "crash",
|
|
6970
|
+
level: "error",
|
|
6971
|
+
title: "启动崩溃",
|
|
6972
|
+
detail: `上次启动抛过错:${safeString(j.at ?? j.time, 40) || "未知时间"} · ${safeString(j.message ?? j.error)}`,
|
|
6973
|
+
hint: "把这条贴到 issue;重启用崩溃对话框的「禁用第三方插件」进入,再按诊断建议重装。"
|
|
6974
|
+
});
|
|
6975
|
+
} catch (e) {
|
|
6976
|
+
checks.push({
|
|
6977
|
+
id: "crash",
|
|
6978
|
+
level: "warn",
|
|
6979
|
+
title: "启动崩溃",
|
|
6980
|
+
detail: `读取失败:${safeString(e?.message)}`
|
|
6981
|
+
});
|
|
6982
|
+
}
|
|
6983
|
+
try {
|
|
6984
|
+
if (input.modelProbe) {
|
|
6985
|
+
const r = await input.modelProbe();
|
|
6986
|
+
checks.push({
|
|
6987
|
+
id: "model",
|
|
6988
|
+
level: r.level,
|
|
6989
|
+
title: "模型",
|
|
6990
|
+
detail: r.detail,
|
|
6991
|
+
hint: r.hint
|
|
6992
|
+
});
|
|
6993
|
+
} else checks.push({
|
|
6994
|
+
id: "model",
|
|
6995
|
+
level: "ok",
|
|
6996
|
+
title: "模型",
|
|
6997
|
+
detail: "未探测(宿主未提供探测入口)。"
|
|
6998
|
+
});
|
|
6999
|
+
} catch (e) {
|
|
7000
|
+
checks.push({
|
|
7001
|
+
id: "model",
|
|
7002
|
+
level: "warn",
|
|
7003
|
+
title: "模型",
|
|
7004
|
+
detail: `探测失败:${safeString(e?.message)}`
|
|
7005
|
+
});
|
|
7006
|
+
}
|
|
7007
|
+
const firstBad = checks.find((c) => c.level !== "ok");
|
|
7008
|
+
const lines = [
|
|
7009
|
+
`dsh-bot 诊断 · 版本 ${input.version} · ${(/* @__PURE__ */ new Date()).toISOString()}`,
|
|
7010
|
+
...checks.map((c) => `[${LEVEL_LABEL[c.level]}] ${c.title}:${c.detail}` + (c.hint ? `\n 下一步:${c.hint}` : "")),
|
|
7011
|
+
firstBad ? `\n结论:先处理「${firstBad.title}」——${firstBad.hint ?? "见上"}` : "\n结论:四层都正常。"
|
|
7012
|
+
].join("\n");
|
|
7013
|
+
return {
|
|
7014
|
+
ok: true,
|
|
7015
|
+
checkedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
7016
|
+
version: input.version,
|
|
7017
|
+
checks,
|
|
7018
|
+
text: lines
|
|
7019
|
+
};
|
|
7020
|
+
}
|
|
7021
|
+
|
|
7022
|
+
//#endregion
|
|
7023
|
+
//#region src/tools/doctor.ts
|
|
7024
|
+
function registerDoctorTool(ctx, deps) {
|
|
7025
|
+
ctx.tools.register(defineTool({
|
|
7026
|
+
name: "bot_doctor",
|
|
7027
|
+
description: "只读自检:查看「宿主注册 / 客户端面板 / 启动崩溃 / 模型可用性」四层状态,返回可直接贴到 issue 的诊断文本。不修改任何文件、不调用模型。",
|
|
7028
|
+
parameters: {},
|
|
7029
|
+
output: {
|
|
7030
|
+
schema: { type: "string" },
|
|
7031
|
+
render: (_args, value) => [{
|
|
7032
|
+
type: "text",
|
|
7033
|
+
text: String(value)
|
|
7034
|
+
}]
|
|
7035
|
+
},
|
|
7036
|
+
async execute() {
|
|
7037
|
+
return (await deps.report()).text;
|
|
7038
|
+
}
|
|
7039
|
+
}));
|
|
7040
|
+
}
|
|
7041
|
+
|
|
6728
7042
|
//#endregion
|
|
6729
7043
|
//#region src/index.ts
|
|
6730
7044
|
const name = "dsh-bot";
|
|
@@ -7177,6 +7491,34 @@ function applyInner(ctx, rawConfig) {
|
|
|
7177
7491
|
}
|
|
7178
7492
|
});
|
|
7179
7493
|
const llmInvoker = llmDispatch;
|
|
7494
|
+
const PLUGIN_VERSION = pluginVersion();
|
|
7495
|
+
const EXPECTED_TOOL_COUNT = 25;
|
|
7496
|
+
diag.pluginVersion = PLUGIN_VERSION;
|
|
7497
|
+
const doctorReport = () => buildDoctorReport({
|
|
7498
|
+
homeDir: service.homeDir,
|
|
7499
|
+
version: PLUGIN_VERSION,
|
|
7500
|
+
expectedTools: EXPECTED_TOOL_COUNT,
|
|
7501
|
+
modelProbe: async () => {
|
|
7502
|
+
try {
|
|
7503
|
+
const providers = await listAvailableModels(ctx);
|
|
7504
|
+
const names = (Array.isArray(providers) ? providers : []).map((x) => x?.provider).filter((x) => typeof x === "string" && x);
|
|
7505
|
+
if (!names.length) return {
|
|
7506
|
+
level: "error",
|
|
7507
|
+
detail: "没有可用的模型 provider",
|
|
7508
|
+
hint: "检查 DSH 的模型渠道配置;没有可用渠道时 Bot 执行会返回空结果。"
|
|
7509
|
+
};
|
|
7510
|
+
return {
|
|
7511
|
+
level: "ok",
|
|
7512
|
+
detail: `可用 provider:${names.join(", ")}`
|
|
7513
|
+
};
|
|
7514
|
+
} catch (e) {
|
|
7515
|
+
return {
|
|
7516
|
+
level: "warn",
|
|
7517
|
+
detail: `探测失败:${e?.message ?? e}`
|
|
7518
|
+
};
|
|
7519
|
+
}
|
|
7520
|
+
}
|
|
7521
|
+
});
|
|
7180
7522
|
const regErrors = [];
|
|
7181
7523
|
let regCount = 0;
|
|
7182
7524
|
const origTools = ctx.tools;
|
|
@@ -7213,6 +7555,7 @@ function applyInner(ctx, rawConfig) {
|
|
|
7213
7555
|
registerRollbackTools(fakeCtx, service);
|
|
7214
7556
|
registerScheduleTools(fakeCtx, service);
|
|
7215
7557
|
registerHttpTools(fakeCtx);
|
|
7558
|
+
registerDoctorTool(fakeCtx, { report: doctorReport });
|
|
7216
7559
|
registerDraftTool(fakeCtx, {
|
|
7217
7560
|
service,
|
|
7218
7561
|
llm: llmInvoker,
|
|
@@ -7313,7 +7656,8 @@ function applyInner(ctx, rawConfig) {
|
|
|
7313
7656
|
saveSettings: async (botId, patch) => {
|
|
7314
7657
|
if (botId) return settingsStore.setForBot(botId, patch);
|
|
7315
7658
|
return settingsStore.setGlobal(patch);
|
|
7316
|
-
}
|
|
7659
|
+
},
|
|
7660
|
+
doctor: doctorReport
|
|
7317
7661
|
});
|
|
7318
7662
|
let webServer;
|
|
7319
7663
|
try {
|
|
@@ -7338,6 +7682,17 @@ function applyInner(ctx, rawConfig) {
|
|
|
7338
7682
|
writeMarker();
|
|
7339
7683
|
ctx.logger?.info?.(`[dsh-bot] 已加载:注册 ${regCount} 个工具,数据目录 ${service.homeDir}`);
|
|
7340
7684
|
}
|
|
7685
|
+
/** 本插件构建版本:优先环境变量,其次读 package.json(绕开打包路径差异)。 */
|
|
7686
|
+
function pluginVersion() {
|
|
7687
|
+
const fromEnv = process.env.DSH_BOT_VERSION;
|
|
7688
|
+
if (fromEnv) return fromEnv;
|
|
7689
|
+
try {
|
|
7690
|
+
const pkgPath = fileURLToPath(new URL("../package.json", import.meta.url));
|
|
7691
|
+
return JSON.parse(fs.readFileSync(pkgPath, "utf8")).version;
|
|
7692
|
+
} catch {
|
|
7693
|
+
return "unknown";
|
|
7694
|
+
}
|
|
7695
|
+
}
|
|
7341
7696
|
|
|
7342
7697
|
//#endregion
|
|
7343
7698
|
export { ASSESSMENT_CLOSE, ASSESSMENT_OPEN, BotError, BotService, DEFAULT_CONFIG, DEFAULT_SCHEDULER_OPTIONS, DEFAULT_SETTINGS, DEFAULT_SNAPSHOT_OPTIONS, RoutineRunner, Scheduler, SettingsStore, SnapshotManager, TOOL_SIDE_EFFECTS, adjustForCost, appendJsonl, apply, applyAssessment, assertInsideWorkspace, assertTransition, buildSystemPrompt, bumpCycle, canTransition, computeCost, computeCostCny, createGoal, createLlmInvoker, createProposal, createToolInvoker, describeCron, describeLlmError, describeTimeSlot, detectProvider, detectSessionModel, detectSessionModelTraced, diffSnapshots, evalAssert, expandHome, formatBytes, goalStatusLabel, goalTypeLabel, inQuietHours, inject, interpolate, isFreeModel, isInside, isLive, isPeakHour, isTerminal, isValidCron, listAvailableModels, listToolNames, matchesCron, matchesCronAt, mergeSettings, name, needsAttention, nextRunAt, normalizeConfig, parseAssessment, parseCadence, parseCron, parseYaml, proposeSuccessors, readJsonl, recallMemories, sanitizeSettings, scoreMemory, shiftOutOfPeak, shouldBlockInDryRun, sideEffectOf, stringifyYaml, toMeta, toZonedDate, transitionGoal, updateProgress, validateRoutine, writeJsonAtomic };
|