dsh-shlr-bilitoolkit 0.1.5 → 0.1.6
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/dist/dsh.d.ts +7 -0
- package/dist/dsh.js +30 -1
- package/package.json +1 -1
package/dist/dsh.d.ts
CHANGED
|
@@ -22,6 +22,13 @@ export interface DshTool {
|
|
|
22
22
|
presentationMeta?: (args: any, value: any) => unknown;
|
|
23
23
|
presentResult?: (args: any, result: any) => any;
|
|
24
24
|
}
|
|
25
|
+
/** 把任意返回值规整成「可无损序列化」的 JSON。
|
|
26
|
+
*
|
|
27
|
+
* dsh 要求工具的规范值必须能无损 JSON 序列化:对象里不能带 `undefined`
|
|
28
|
+
* (`JSON.stringify` 会静默丢键,导致 round-trip 丢失),也不能有 NaN/Infinity。
|
|
29
|
+
* 这里统一处理:丢 undefined、NaN/Infinity 转 null、bigint 转字符串。
|
|
30
|
+
*/
|
|
31
|
+
export declare function toLosslessJson(value: unknown): unknown;
|
|
25
32
|
export declare const TOOLS: DshTool[];
|
|
26
33
|
export declare function apply(ctx: any): void;
|
|
27
34
|
export * from "./index.js";
|
package/dist/dsh.js
CHANGED
|
@@ -64,6 +64,34 @@ function toDshParameters(schema) {
|
|
|
64
64
|
function textOf(value) {
|
|
65
65
|
return typeof value === "string" ? value : JSON.stringify(value, null, 2);
|
|
66
66
|
}
|
|
67
|
+
/** 把任意返回值规整成「可无损序列化」的 JSON。
|
|
68
|
+
*
|
|
69
|
+
* dsh 要求工具的规范值必须能无损 JSON 序列化:对象里不能带 `undefined`
|
|
70
|
+
* (`JSON.stringify` 会静默丢键,导致 round-trip 丢失),也不能有 NaN/Infinity。
|
|
71
|
+
* 这里统一处理:丢 undefined、NaN/Infinity 转 null、bigint 转字符串。
|
|
72
|
+
*/
|
|
73
|
+
export function toLosslessJson(value) {
|
|
74
|
+
if (typeof value === "number")
|
|
75
|
+
return Number.isFinite(value) ? value : null;
|
|
76
|
+
if (typeof value === "bigint")
|
|
77
|
+
return value.toString();
|
|
78
|
+
if (Array.isArray(value)) {
|
|
79
|
+
return value.map((v) => {
|
|
80
|
+
const sv = toLosslessJson(v);
|
|
81
|
+
return sv === undefined ? null : sv;
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
if (value && typeof value === "object") {
|
|
85
|
+
const out = {};
|
|
86
|
+
for (const [k, v] of Object.entries(value)) {
|
|
87
|
+
const sv = toLosslessJson(v);
|
|
88
|
+
if (sv !== undefined)
|
|
89
|
+
out[k] = sv;
|
|
90
|
+
}
|
|
91
|
+
return out;
|
|
92
|
+
}
|
|
93
|
+
return value; // string / boolean / null / undefined
|
|
94
|
+
}
|
|
67
95
|
export const TOOLS = [
|
|
68
96
|
{
|
|
69
97
|
id: "bili_recommend",
|
|
@@ -567,7 +595,8 @@ export function apply(ctx) {
|
|
|
567
595
|
parameters: toDshParameters(tool.parameters),
|
|
568
596
|
output,
|
|
569
597
|
async execute(args) {
|
|
570
|
-
|
|
598
|
+
// 统一规整为无损 JSON,避免 toDict 里的 undefined / NaN 触发序列化错误
|
|
599
|
+
return toLosslessJson(await tool.handler(args));
|
|
571
600
|
},
|
|
572
601
|
};
|
|
573
602
|
if (tool.presentResult) {
|