dsh-postbox 0.0.0-stage → 0.2.2
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/LICENSE +21 -0
- package/README.md +531 -2
- package/client.js +2717 -0
- package/cordis.patch.yml +83 -0
- package/docs/DETAIL.md +56 -0
- package/icon.svg +1 -0
- package/index.js +2312 -0
- package/lib/backend-sqlite.js +65 -0
- package/lib/bridge.js +940 -0
- package/lib/config.js +289 -0
- package/lib/digest.js +213 -0
- package/lib/errors.js +83 -0
- package/lib/ft.js +211 -0
- package/lib/host-settings.js +237 -0
- package/lib/imap.js +635 -0
- package/lib/importance.js +83 -0
- package/lib/llm.js +411 -0
- package/lib/log.js +75 -0
- package/lib/mime.js +381 -0
- package/lib/mutf7.js +54 -0
- package/lib/notify.js +328 -0
- package/lib/oauth.js +275 -0
- package/lib/schema.js +177 -0
- package/lib/search.js +129 -0
- package/lib/settings.js +395 -0
- package/lib/smtp.js +153 -0
- package/lib/store.js +1068 -0
- package/lib/sync.js +899 -0
- package/lib/tool.js +232 -0
- package/lib/tools-m4.js +863 -0
- package/lib/update.js +642 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +60 -4
package/lib/tool.js
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
// lib/tool.js —— defineTool 的本地等价实现(零外部依赖)。
|
|
2
|
+
//
|
|
3
|
+
// 为什么不用 @deepseek-ai/dsh-tools:该包只存在于 DSH 宿主/共享 store,
|
|
4
|
+
// 且 profile 以 link: junction 方式链到本 bundle 源码目录,ESM bare-specifier
|
|
5
|
+
// 解析从源码目录向上走不到 store;在 package.json 声明依赖也不会被安装
|
|
6
|
+
// (pnpm 对 link: 包不解析其 dependencies)。因此完全自包含实现。
|
|
7
|
+
//
|
|
8
|
+
// 语义与 dsh-tools 的 defineTool 一致(0.1.5-rc.2 定稿;⚠️ 0.2.1-rc.2 起运行时还会按
|
|
9
|
+
// output.schema 强制校验工具**返回值**:未声明字段与 null 都会让整次调用失败,
|
|
10
|
+
// 所以每个 execute 的返回键必须与 output 声明完全对齐):
|
|
11
|
+
// parameters —— 参数规格属性表({param:{type, required?, description?, items?, enum?, const?}})
|
|
12
|
+
// 编译为 {type:"object", properties, required:[...]},required 取内联 required:true 的键
|
|
13
|
+
// output —— 值规格;本插件只用 {type:"object", additionalProperties:false, properties:{...}} + render
|
|
14
|
+
// 输出 definition 保持 {name, description, parameters, output:{schema, render, presentationMeta?}, execute, timeoutMs?}
|
|
15
|
+
|
|
16
|
+
const ANNOTATION_KEYS = ["description", "title", "default", "examples", "deprecated", "readOnly", "writeOnly"];
|
|
17
|
+
|
|
18
|
+
function copyAnnotations(src, dst) {
|
|
19
|
+
for (const key of ANNOTATION_KEYS) {
|
|
20
|
+
if (Object.hasOwn(src, key)) dst[key] = src[key];
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** 把单个值规格节点编译成受支持的 JSON Schema 子集(与 dsh-tools 编译器同语义)。 */
|
|
25
|
+
function compileValue(input, path, allowRequired) {
|
|
26
|
+
if (input === null || typeof input !== "object" || Array.isArray(input)) {
|
|
27
|
+
throw new Error(`${path} must be a value schema object`);
|
|
28
|
+
}
|
|
29
|
+
const authorKeys = [...ANNOTATION_KEYS, ...allowRequired ? ["required"] : []];
|
|
30
|
+
const node = {};
|
|
31
|
+
|
|
32
|
+
if (Object.hasOwn(input, "oneOf")) {
|
|
33
|
+
const allowed = [...authorKeys, "oneOf", "type"];
|
|
34
|
+
for (const k of Object.keys(input)) {
|
|
35
|
+
if (!allowed.includes(k)) throw new Error(`${path}.${k} is not allowed alongside oneOf`);
|
|
36
|
+
}
|
|
37
|
+
if (Object.hasOwn(input, "type")) throw new Error(`${path} cannot declare both type and oneOf`);
|
|
38
|
+
if (!Array.isArray(input.oneOf) || input.oneOf.length < 2) throw new Error(`${path}.oneOf must be an array of at least two value schemas`);
|
|
39
|
+
node.oneOf = input.oneOf.map((branch, i) => compileValue(branch, `${path}.oneOf[${i}]`, false));
|
|
40
|
+
copyAnnotations(input, node);
|
|
41
|
+
return node;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const t = input.type;
|
|
45
|
+
switch (t) {
|
|
46
|
+
case "json":
|
|
47
|
+
copyAnnotations(input, node);
|
|
48
|
+
return node;
|
|
49
|
+
case "object": {
|
|
50
|
+
const allowed = [...authorKeys, "type", "properties", "additionalProperties"];
|
|
51
|
+
for (const k of Object.keys(input)) {
|
|
52
|
+
if (!allowed.includes(k)) throw new Error(`${path}.${k} is not allowed on object`);
|
|
53
|
+
}
|
|
54
|
+
if (!Object.hasOwn(input, "additionalProperties") || typeof input.additionalProperties !== "boolean") {
|
|
55
|
+
throw new Error(`${path}.additionalProperties must be explicitly true or false`);
|
|
56
|
+
}
|
|
57
|
+
node.type = "object";
|
|
58
|
+
copyAnnotations(input, node);
|
|
59
|
+
node.additionalProperties = input.additionalProperties;
|
|
60
|
+
if (Object.hasOwn(input, "properties")) {
|
|
61
|
+
node.properties = compilePropertyMap(input.properties, `${path}.properties`).properties;
|
|
62
|
+
}
|
|
63
|
+
return node;
|
|
64
|
+
}
|
|
65
|
+
case "array": {
|
|
66
|
+
const allowed = [...authorKeys, "type", "items"];
|
|
67
|
+
for (const k of Object.keys(input)) {
|
|
68
|
+
if (!allowed.includes(k)) throw new Error(`${path}.${k} is not allowed on array`);
|
|
69
|
+
}
|
|
70
|
+
node.type = "array";
|
|
71
|
+
copyAnnotations(input, node);
|
|
72
|
+
if (Object.hasOwn(input, "items")) node.items = compileValue(input.items, `${path}.items`, false);
|
|
73
|
+
return node;
|
|
74
|
+
}
|
|
75
|
+
case "string":
|
|
76
|
+
case "number":
|
|
77
|
+
case "integer":
|
|
78
|
+
case "boolean":
|
|
79
|
+
case "null": {
|
|
80
|
+
const allowed = [...authorKeys, "type", "enum", "const"];
|
|
81
|
+
for (const k of Object.keys(input)) {
|
|
82
|
+
if (!allowed.includes(k)) throw new Error(`${path}.${k} is not allowed on ${t}`);
|
|
83
|
+
}
|
|
84
|
+
node.type = t;
|
|
85
|
+
copyAnnotations(input, node);
|
|
86
|
+
if (Object.hasOwn(input, "enum")) {
|
|
87
|
+
if (!Array.isArray(input.enum) || input.enum.length === 0) throw new Error(`${path}.enum must be a non-empty array of scalars`);
|
|
88
|
+
node.enum = [...input.enum];
|
|
89
|
+
}
|
|
90
|
+
if (Object.hasOwn(input, "const")) node.const = input.const;
|
|
91
|
+
return node;
|
|
92
|
+
}
|
|
93
|
+
default:
|
|
94
|
+
throw new Error(`${path}.type must be string/number/integer/boolean/null/array/object/json, or use oneOf`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** 参数属性表 → {properties, required: [...]};键存在且值为 true 才算 required。
|
|
99
|
+
* 注意 mutates 输入(删掉属性上的 required 标记)以复用宿主行为,调用方传的都是字面量对象。 */
|
|
100
|
+
function compilePropertyMap(input, path) {
|
|
101
|
+
if (input === null || typeof input !== "object" || Array.isArray(input)) {
|
|
102
|
+
throw new Error(`${path} must be an object of value schemas`);
|
|
103
|
+
}
|
|
104
|
+
if (!Object.hasOwn(input, "required")) input = { ...input };
|
|
105
|
+
// 逐属性浅拷贝后再删 required 标记,避免 mutate 调用方传入的字面量对象。
|
|
106
|
+
input = Object.fromEntries(Object.entries(input).map(([k, v]) => [k, v && typeof v === "object" && !Array.isArray(v) ? { ...v } : v]));
|
|
107
|
+
const required = [];
|
|
108
|
+
for (const [key, property] of Object.entries(input)) {
|
|
109
|
+
if (Object.hasOwn(property, "required")) {
|
|
110
|
+
// required 可以是 boolean(false 表示可选)或缺失;只有显式 true 才是必填。
|
|
111
|
+
if (typeof property.required !== "boolean") throw new Error(`${path}.${key}.required must be a boolean when present`);
|
|
112
|
+
if (property.required === true) required.push(key);
|
|
113
|
+
delete property.required;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return {
|
|
117
|
+
type: "object",
|
|
118
|
+
properties: Object.fromEntries(Object.entries(input).map(([key, property]) => [key, compileValue(property, `${path}.${key}`, true)])),
|
|
119
|
+
...(required.length > 0 ? { required } : {}),
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* defineTool 本地等价物。options:
|
|
125
|
+
* { name, description, parameters(参数规格属性表),
|
|
126
|
+
* output: { schema(值规格), render(args, value) → [{type:"text",text}], presentationMeta? },
|
|
127
|
+
* execute(args, exec), timeoutMs? }
|
|
128
|
+
* 返回可直接交给 ctx.tools.register 的 definition。
|
|
129
|
+
*/
|
|
130
|
+
export function defineTool(options) {
|
|
131
|
+
if (!options || typeof options !== "object") throw new Error("defineTool: options must be an object");
|
|
132
|
+
const { name, description, parameters, output, execute } = options;
|
|
133
|
+
if (typeof name !== "string" || name === "") throw new Error("defineTool: name must be a non-empty string");
|
|
134
|
+
if (name === "run_code") throw new Error('tool name "run_code" is reserved');
|
|
135
|
+
if (typeof execute !== "function") throw new Error(`defineTool(${name}): execute must be a function`);
|
|
136
|
+
if (!output || typeof output !== "object" || typeof output.render !== "function") {
|
|
137
|
+
throw new TypeError(`tool "${name}" must declare output { schema, render, presentationMeta? }`);
|
|
138
|
+
}
|
|
139
|
+
if (options.timeoutMs !== undefined && (!Number.isFinite(options.timeoutMs) || options.timeoutMs <= 0)) {
|
|
140
|
+
throw new Error(`defineTool(${name}): timeoutMs must be a positive finite number`);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const parametersSchema = compilePropertyMap(parameters ?? {}, "parameters");
|
|
144
|
+
const outputSchema = {
|
|
145
|
+
type: "object",
|
|
146
|
+
additionalProperties: false,
|
|
147
|
+
properties: Object.fromEntries(Object.entries(output.schema.properties ?? {}).map(([k, v]) => [k, compileValue(v, `output.schema.properties.${k}`, false)])),
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
const definition = {
|
|
151
|
+
name,
|
|
152
|
+
description,
|
|
153
|
+
parameters: parametersSchema,
|
|
154
|
+
output: {
|
|
155
|
+
schema: outputSchema,
|
|
156
|
+
render(args, value) { return output.render(args, value); },
|
|
157
|
+
...(output.presentationMeta !== undefined ? { presentationMeta(args, value) { return output.presentationMeta(args, value); } } : {}),
|
|
158
|
+
},
|
|
159
|
+
async execute(args, exec) {
|
|
160
|
+
// 与官方 defineTool 一致:参数不合规时抛 ToolArgsError,不进用户 execute。
|
|
161
|
+
const violations = validateValue(parametersSchema, args, "");
|
|
162
|
+
if (violations.length > 0) throw toolArgsError(violations);
|
|
163
|
+
return execute(args, exec);
|
|
164
|
+
},
|
|
165
|
+
};
|
|
166
|
+
if (options.timeoutMs !== undefined) definition.timeoutMs = options.timeoutMs;
|
|
167
|
+
return definition;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** 收集 violations;官方抛的 ToolArgsError 带 name="ToolArgsError" 与同形消息。 */
|
|
171
|
+
function toolArgsError(violations) {
|
|
172
|
+
const err = new Error(`invalid arguments: ${violations.join("; ")}`);
|
|
173
|
+
err.name = "ToolArgsError";
|
|
174
|
+
err.violations = violations;
|
|
175
|
+
return err;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** 按已编译 JSON Schema 校验 plain 值(参数;测试里也用它校验工具**输出**),
|
|
179
|
+
* 返回 violation 文本数组(空 = 通过)。 */
|
|
180
|
+
export function validateValue(schema, value, path) {
|
|
181
|
+
const out = [];
|
|
182
|
+
const root = path ? `${path}.` : "";
|
|
183
|
+
if (Object.hasOwn(schema, "oneOf")) {
|
|
184
|
+
for (const branch of schema.oneOf) {
|
|
185
|
+
if (validateValue(branch, value, path).length === 0) return out;
|
|
186
|
+
}
|
|
187
|
+
out.push(`${path || "(value)"}: no oneOf branch matched`);
|
|
188
|
+
return out;
|
|
189
|
+
}
|
|
190
|
+
const t = schema.type;
|
|
191
|
+
if (t === "object") {
|
|
192
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
193
|
+
out.push(`${root || "(root)"} must be an object`);
|
|
194
|
+
return out;
|
|
195
|
+
}
|
|
196
|
+
for (const key of Object.keys(value)) {
|
|
197
|
+
if (schema.additionalProperties === false && !Object.hasOwn(schema.properties ?? {}, key)) {
|
|
198
|
+
out.push(`${root}${key} is not allowed`);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
for (const key of schema.required ?? []) {
|
|
202
|
+
if (!Object.hasOwn(value, key)) out.push(`${root}${key} is required`);
|
|
203
|
+
}
|
|
204
|
+
for (const [key, sub] of Object.entries(schema.properties ?? {})) {
|
|
205
|
+
if (Object.hasOwn(value, key)) out.push(...validateValue(sub, value[key], `${root}${key}`));
|
|
206
|
+
}
|
|
207
|
+
return out;
|
|
208
|
+
}
|
|
209
|
+
if (t === "array") {
|
|
210
|
+
if (!Array.isArray(value)) { out.push(`${root || "(root)"} must be an array`); return out; }
|
|
211
|
+
if (schema.items) value.forEach((entry, i) => out.push(...validateValue(schema.items, entry, `${root}${i}`)));
|
|
212
|
+
return out;
|
|
213
|
+
}
|
|
214
|
+
if (t === "string") {
|
|
215
|
+
if (typeof value !== "string") { out.push(`${root || "(root)"} must be a string`); return out; }
|
|
216
|
+
} else if (t === "number") {
|
|
217
|
+
if (typeof value !== "number" || Number.isNaN(value)) { out.push(`${root || "(root)"} must be a number`); return out; }
|
|
218
|
+
} else if (t === "integer") {
|
|
219
|
+
if (typeof value !== "number" || !Number.isInteger(value)) { out.push(`${root || "(root)"} must be an integer`); return out; }
|
|
220
|
+
} else if (t === "boolean") {
|
|
221
|
+
if (typeof value !== "boolean") { out.push(`${root || "(root)"} must be a boolean`); return out; }
|
|
222
|
+
} else if (t === "null") {
|
|
223
|
+
if (value !== null) { out.push(`${root || "(root)"} must be null`); return out; }
|
|
224
|
+
}
|
|
225
|
+
if (Array.isArray(schema.enum) && !schema.enum.includes(value)) {
|
|
226
|
+
out.push(`${root || "(root)"} must be one of ${schema.enum.join(", ")}`);
|
|
227
|
+
}
|
|
228
|
+
if (Object.hasOwn(schema, "const") && schema.const !== value) {
|
|
229
|
+
out.push(`${root || "(root)"} must be ${JSON.stringify(schema.const)}`);
|
|
230
|
+
}
|
|
231
|
+
return out;
|
|
232
|
+
}
|