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/schema.js
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
// lib/schema.js —— 最小 standard-schema 门面(零外部依赖)。
|
|
2
|
+
//
|
|
3
|
+
// 为什么需要它:cordis 在插件启动前调用 Config["~standard"].validate(config)
|
|
4
|
+
// (C:\Users\me\AppData\Roaming\npm\node_modules\@deepseek-ai\dsh\node_modules\
|
|
5
|
+
// @deepseek-ai\cordis\lib\index.js:955 resolveConfig)。schemastery 的对象天生带
|
|
6
|
+
// ~standard,但 profile 以 link: junction 链到本 bundle 源码目录,ESM bare-specifier
|
|
7
|
+
// 从源码目录向上走不到 profile/D SH 的 node_modules —— 与 @deepseek-ai/dsh-tools
|
|
8
|
+
// 是同一堵墙。所以自带一个调用面兼容 schemastery 的最小实现。
|
|
9
|
+
//
|
|
10
|
+
// 对齐的宿主行为(schemastery 3.18.2 实测,message 逐字对齐):
|
|
11
|
+
// validate(ok) → { value: <归一化后的值> }
|
|
12
|
+
// validate(bad) → { issues: [{ message: "$.accounts[0].email missing required value", path: [...] }] }
|
|
13
|
+
// 实测样例:
|
|
14
|
+
// {accounts:[{}]} → "$.accounts[0].email missing required value"
|
|
15
|
+
// {accounts:"x"} → "$.accounts expected array but got x"
|
|
16
|
+
// {} → "$.accounts missing required value"
|
|
17
|
+
// {accounts:[{email:5}]}→ "$.accounts[0].email expected string but got 5"
|
|
18
|
+
// {accounts:[]} → OK
|
|
19
|
+
// 注意 schemastery 对「未知键」是宽松的(不报错,归一化时丢弃),这里保持一致。
|
|
20
|
+
|
|
21
|
+
// schemastery 在 Schema.prototype 上挂 Symbol.for("schemastery")=true(Cordis
|
|
22
|
+
// isNativeConfigSchema 判:Reflect.get(value,Symbol.for("schemastery"))===true)。
|
|
23
|
+
// 宿主把我们的 Node 实例当作 runtime.Config,也必须带上这个 Symbol 标记,否则
|
|
24
|
+
// isNativeConfigSchema 返回 false → entry.status="unsupported" → fiber 启动时
|
|
25
|
+
// runtime.Config["~standard"] 为 undefined → resolveConfig 报
|
|
26
|
+
// "Cannot read properties of undefined (reading 'validate')"。
|
|
27
|
+
const SCHEMASTERY_KEY = Symbol.for("schemastery");
|
|
28
|
+
|
|
29
|
+
const VALIDATORS = {
|
|
30
|
+
string: (v) => typeof v === "string",
|
|
31
|
+
number: (v) => typeof v === "number" && Number.isFinite(v),
|
|
32
|
+
boolean: (v) => typeof v === "boolean",
|
|
33
|
+
any: () => true,
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
function typeGuard(type) {
|
|
37
|
+
if (VALIDATORS[type]) return VALIDATORS[type];
|
|
38
|
+
return (v) => v === undefined || typeof v === type;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
class Node {
|
|
42
|
+
constructor(impl) {
|
|
43
|
+
this._impl = impl;
|
|
44
|
+
this._required = false;
|
|
45
|
+
this._description = undefined;
|
|
46
|
+
// isNativeConfigSchema 要求: Symbol.for("schemastery")===true,
|
|
47
|
+
// typeof type==="string",meta 为非空对象。实测 schemastery 的每个 schema
|
|
48
|
+
// 实例都带有这三个属性(Prototype=kSchema,true;type 从 options.type 继承;
|
|
49
|
+
// meta 默认 {})。这里在实例上自有定义,绕过 prototype getter 问题。
|
|
50
|
+
this[SCHEMASTERY_KEY] = true;
|
|
51
|
+
this.type = impl.kind;
|
|
52
|
+
this.meta = {};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
required() { this._required = true; return this; }
|
|
56
|
+
|
|
57
|
+
description(text) { this._description = text; return this; }
|
|
58
|
+
|
|
59
|
+
/** schemastery 兼容的配置回写钩子:cordis 在 internal/update 里调用
|
|
60
|
+
* runtime.Config?.["simplify"] 把生效 config 写回 loader 行(asar cordis-plugin-loader
|
|
61
|
+
* lib/index.js:612)。缺失只是不能回写,不影响激活;给了更贴近宿主行为。 */
|
|
62
|
+
simplify(value) { return value; }
|
|
63
|
+
|
|
64
|
+
/** 递归校验;issues 为空表示通过。value 仅在通过时被构造。 */
|
|
65
|
+
_check(value, path, issues) {
|
|
66
|
+
this._impl(value, path, issues);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
get ["~standard"]() {
|
|
70
|
+
const self = this;
|
|
71
|
+
return {
|
|
72
|
+
version: 1,
|
|
73
|
+
// 必须与 schemastery 的 vendor 一致:cordis-plugin-loader 的
|
|
74
|
+
// isSchemastery(schema) 判的是 schema?.["~standard"].vendor === "schemastery"
|
|
75
|
+
// (asar cordis-plugin-loader lib/index.js:251-253),这个判定决定改动是否
|
|
76
|
+
// 被当作「仅 volatile」的配置更新。vendor 不对会让所有 config 更新走全量
|
|
77
|
+
// reload 路径,虽然不致命,但偏离宿主预期。
|
|
78
|
+
vendor: "schemastery",
|
|
79
|
+
validate(input) {
|
|
80
|
+
if (typeof input !== "object" || input === null || Array.isArray(input)) {
|
|
81
|
+
return { issues: [{ message: "root must be an object", path: [] }] };
|
|
82
|
+
}
|
|
83
|
+
const issues = [];
|
|
84
|
+
self._check(input, [], issues);
|
|
85
|
+
if (issues.length > 0) return { issues };
|
|
86
|
+
return { value: stripUnknown(self, input) };
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** 去掉 schema 未声明的顶层键(与 schemastery 的默认行为一致:只保留已知键)。 */
|
|
93
|
+
function stripUnknown(node, input) {
|
|
94
|
+
if (node._impl.kind === "object" && input && typeof input === "object" && !Array.isArray(input)) {
|
|
95
|
+
const out = {};
|
|
96
|
+
for (const [k, child] of Object.entries(node._impl.properties)) {
|
|
97
|
+
if (!Object.hasOwn(input, k)) continue;
|
|
98
|
+
out[k] = child._required || input[k] !== undefined ? stripUnknown(child, input[k]) : input[k];
|
|
99
|
+
}
|
|
100
|
+
return out;
|
|
101
|
+
}
|
|
102
|
+
if (node._impl.kind === "array" && Array.isArray(input)) {
|
|
103
|
+
return input.map((entry) => stripUnknown(node._impl.inner, entry));
|
|
104
|
+
}
|
|
105
|
+
return input;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function makeScalar(type) {
|
|
109
|
+
const guard = typeGuard(type);
|
|
110
|
+
const impl = function (value, path, issues) {
|
|
111
|
+
if (value === undefined) return; // 可选且未提供
|
|
112
|
+
if (!guard(value)) issues.push({ message: `$.${fmt(path)} expected ${type} but got ${preview(value)}`, path });
|
|
113
|
+
};
|
|
114
|
+
impl.kind = type;
|
|
115
|
+
return new Node(impl);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** schemastery 的 got 值预览策略:标量直接 String,对象/数组给类型名。 */
|
|
119
|
+
function preview(value) {
|
|
120
|
+
if (value === null) return "null";
|
|
121
|
+
if (Array.isArray(value)) return "array";
|
|
122
|
+
if (typeof value === "object") return "object";
|
|
123
|
+
return String(value);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function fmt(path) {
|
|
127
|
+
return path.length === 0 ? "(root)" : path.join(".").replace(/\.(\d+)/g, "[$1]");
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
class SchemaNamespace {
|
|
131
|
+
string() { return makeScalar("string"); }
|
|
132
|
+
number() { return makeScalar("number"); }
|
|
133
|
+
boolean() { return makeScalar("boolean"); }
|
|
134
|
+
any() { return makeScalar("any"); }
|
|
135
|
+
|
|
136
|
+
array(inner) {
|
|
137
|
+
const impl = function (value, path, issues) {
|
|
138
|
+
if (value === undefined) return;
|
|
139
|
+
if (!Array.isArray(value)) {
|
|
140
|
+
issues.push({ message: `$.${fmt(path)} expected array but got ${preview(value)}`, path });
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
value.forEach((entry, index) => inner._check(entry, [...path, String(index)], issues));
|
|
144
|
+
};
|
|
145
|
+
impl.kind = "array";
|
|
146
|
+
impl.inner = inner;
|
|
147
|
+
return new Node(impl);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
object(properties) {
|
|
151
|
+
const entries = Object.entries(properties);
|
|
152
|
+
const impl = function (value, path, issues) {
|
|
153
|
+
if (value === undefined) return;
|
|
154
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
155
|
+
issues.push({ message: `$.${fmt(path)} must be an object`, path });
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
for (const [key, child] of entries) {
|
|
159
|
+
if (!Object.hasOwn(value, key)) {
|
|
160
|
+
if (child._required) {
|
|
161
|
+
issues.push({ message: `$.${fmt([...path, key])} missing required value`, path: [...path, key] });
|
|
162
|
+
}
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
child._check(value[key], [...path, key], issues);
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
impl.kind = "object";
|
|
169
|
+
impl.properties = properties;
|
|
170
|
+
const node = new Node(impl);
|
|
171
|
+
// required 信息在 object 校验时才用到,这里保持 Node 上的默认 false。
|
|
172
|
+
return node;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export const Schema = new SchemaNamespace();
|
|
177
|
+
export default Schema;
|
package/lib/search.js
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// lib/search.js — 本地归档检索(M4/M5 共用的一层)。
|
|
2
|
+
//
|
|
3
|
+
// 为什么单独一层:`mailbox_search(source=archive)` 和 `mailbox_archive_search` 是**同一件事**,
|
|
4
|
+
// 分头实现必然会漂移(一个支持 tag、另一个忘了日期)。这里只有一条路径:
|
|
5
|
+
// ① 元数据过滤(账户/文件夹/未读/日期/发件人/主题/tag)→ SQL 缩成"允许集合";
|
|
6
|
+
// ② 有关键词 → 倒排索引打分(ft.rankRows),再与允许集合求交;
|
|
7
|
+
// ③ **AND 优先**:全词命中的排前面;一个都没有才退回部分命中,并把 partial 标出来。
|
|
8
|
+
//
|
|
9
|
+
// 与 ROADMAP M5 的耦合点:`archive.mode` 决定正文有没有入索引。
|
|
10
|
+
// · full → subject / from / to / 附件文本 / 正文 全部可搜
|
|
11
|
+
// · metadata → 只有 subject / from / to(正文**没有**入索引)
|
|
12
|
+
// · online → 没有归档表,直接回"当前模式不支持本地检索"
|
|
13
|
+
// 工具必须把这件事**明确说出来**,不能让用户以为"搜不到 = 没收过"。
|
|
14
|
+
|
|
15
|
+
import { queryTerms, rankRows, explainMiss, refKeyOf } from "./ft.js";
|
|
16
|
+
|
|
17
|
+
/** 把 sqlite 行转成检索结果里统一的邮件视图(不暴露 eml_path 之类的内部列)。 */
|
|
18
|
+
export function toMailView(row) {
|
|
19
|
+
return {
|
|
20
|
+
account: row.account,
|
|
21
|
+
folder: row.folder,
|
|
22
|
+
uid: row.uid,
|
|
23
|
+
messageId: row.message_id ?? null,
|
|
24
|
+
subject: row.subject ?? null,
|
|
25
|
+
from: row.from_addr ?? null,
|
|
26
|
+
to: row.to_addr ?? null,
|
|
27
|
+
dateTs: row.date_ts ?? row.first_seen_at ?? null,
|
|
28
|
+
seen: row.seen === 1,
|
|
29
|
+
hasAttachment: row.has_attachment === 1,
|
|
30
|
+
importance: row.importance ?? null,
|
|
31
|
+
snippet: row.snippet ?? null,
|
|
32
|
+
tags: row.tags ?? undefined,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* 归档检索。
|
|
38
|
+
* @returns { mail, hits, terms, perTerm, partial, indexed, mode, notes, total }
|
|
39
|
+
*/
|
|
40
|
+
export function archiveSearch(store, {
|
|
41
|
+
q = null, account = null, folder = null, unreadOnly = false,
|
|
42
|
+
sinceTs = null, untilTs = null, from = null, subject = null, tag = null,
|
|
43
|
+
limit = 25, mode = "metadata", withTags = false,
|
|
44
|
+
} = {}) {
|
|
45
|
+
const notes = [];
|
|
46
|
+
const terms = q ? queryTerms(q) : [];
|
|
47
|
+
const wantQ = !!(q && String(q).trim());
|
|
48
|
+
if (wantQ && !terms.length) notes.push("查询里没有可检索的词(太短或全是停用词)");
|
|
49
|
+
|
|
50
|
+
// ---- 允许集合(元数据过滤)----
|
|
51
|
+
const hasMeta = !!(account || folder || unreadOnly || Number.isFinite(sinceTs) || Number.isFinite(untilTs) || from || subject);
|
|
52
|
+
let allowed = null;
|
|
53
|
+
if (hasMeta) {
|
|
54
|
+
allowed = new Set(store.filterRefs({ account, folder, unreadOnly, sinceTs, untilTs, fromLike: from, subjectLike: subject }).map(refKeyOf));
|
|
55
|
+
}
|
|
56
|
+
if (tag) {
|
|
57
|
+
const tagSet = new Set(store.messagesByTag(tag, { limit: 100000 }).map(refKeyOf));
|
|
58
|
+
notes.push(`按标签「${tag}」过滤(${tagSet.size} 封)`);
|
|
59
|
+
allowed = allowed ? new Set([...allowed].filter((k) => tagSet.has(k))) : tagSet;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const index = store.indexStats();
|
|
63
|
+
const hits = [];
|
|
64
|
+
let partial = false;
|
|
65
|
+
let perTerm = {};
|
|
66
|
+
let matchedTotal = index.messages;
|
|
67
|
+
|
|
68
|
+
if (wantQ && !terms.length) {
|
|
69
|
+
// 有关键词但一个可用词条都没有(全是停用词/单字母):**不能**退化成"列出全部",
|
|
70
|
+
// 那会让用户以为"搜 the 搜到了所有邮件"。
|
|
71
|
+
notes.push("查询里没有可检索的词(太短或全是停用词);换个实词再试");
|
|
72
|
+
matchedTotal = 0;
|
|
73
|
+
if (mode !== "full") notes.push(`当前 archive.mode=${mode}:正文未入索引(改 full 才能搜到正文里的词)`);
|
|
74
|
+
} else if (wantQ) {
|
|
75
|
+
const ranked = rankRows(store.tokenRows(terms), terms, { total: index.messages });
|
|
76
|
+
let list = ranked.hits;
|
|
77
|
+
if (allowed) list = list.filter((h) => allowed.has(refKeyOf(h.ref)));
|
|
78
|
+
const exact = list.filter((h) => h.coverage >= 1);
|
|
79
|
+
partial = exact.length === 0 && list.length > 0;
|
|
80
|
+
const picked = (exact.length ? exact : list).slice(0, limit);
|
|
81
|
+
hits.push(...picked);
|
|
82
|
+
perTerm = ranked.perTerm;
|
|
83
|
+
matchedTotal = exact.length || list.length;
|
|
84
|
+
if (!picked.length) {
|
|
85
|
+
notes.push(`本地索引(${index.messages} 封)里没有命中:${explainMiss(ranked.perTerm, terms)}`);
|
|
86
|
+
} else if (partial) {
|
|
87
|
+
notes.push("没有一封命中全部关键词,下面是**部分命中**(按命中比例排序)");
|
|
88
|
+
} else if (list.length > picked.length) {
|
|
89
|
+
notes.push(`共命中 ${list.length} 封,只返回前 ${picked.length} 封`);
|
|
90
|
+
}
|
|
91
|
+
if (mode !== "full") notes.push(`当前 archive.mode=${mode}:正文未入索引(改 full 才能搜到正文里的词)`);
|
|
92
|
+
} else {
|
|
93
|
+
// 没有关键词:纯元数据列表(日期倒序)。metadata 过滤一律走 SQL,不把全库拉进内存。
|
|
94
|
+
const refs = store.filterRefs({ account, folder, unreadOnly, sinceTs, untilTs, fromLike: from, subjectLike: subject });
|
|
95
|
+
const filtered = allowed ? refs.filter((r) => allowed.has(refKeyOf(r))) : refs;
|
|
96
|
+
matchedTotal = filtered.length;
|
|
97
|
+
hits.push(...filtered.slice(0, limit).map((ref) => ({
|
|
98
|
+
ref: { account: ref.account, folder: ref.folder, uid: ref.uid },
|
|
99
|
+
score: 0, coverage: 0, matched: [], fields: [],
|
|
100
|
+
})));
|
|
101
|
+
if (filtered.length > hits.length) notes.push(`共命中 ${filtered.length} 封,只返回前 ${hits.length} 封`);
|
|
102
|
+
// 空归档时说清楚"是没邮件"而不是"条件太严";非 full 模式也如实标注正文不可搜
|
|
103
|
+
if (!filtered.length && index.messages === 0 && !hasMeta && !tag) notes.push("本地归档还没有邮件(watcher 还没抓到,或 archive.mode=online 不落归档)");
|
|
104
|
+
if (mode !== "full") notes.push(`当前 archive.mode=${mode}:正文未入索引(改 full 才能搜到正文里的词)`);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const rows = store.getMessages(hits.map((h) => h.ref));
|
|
108
|
+
const byKey = new Map(hits.map((h) => [refKeyOf(h.ref), h]));
|
|
109
|
+
const mail = rows.map((row) => {
|
|
110
|
+
const view = toMailView(row);
|
|
111
|
+
const hit = byKey.get(refKeyOf(row));
|
|
112
|
+
if (hit) { view.score = hit.score; view.matched = hit.matched; view.fields = hit.fields; }
|
|
113
|
+
if (withTags) { try { view.tags = store.tagsOf(row); } catch { /* 标签表不可用 */ } }
|
|
114
|
+
return view;
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
return {
|
|
118
|
+
mail,
|
|
119
|
+
hits,
|
|
120
|
+
terms,
|
|
121
|
+
perTerm,
|
|
122
|
+
partial,
|
|
123
|
+
indexed: index.messages,
|
|
124
|
+
entries: index.entries,
|
|
125
|
+
mode,
|
|
126
|
+
notes,
|
|
127
|
+
total: matchedTotal,
|
|
128
|
+
};
|
|
129
|
+
}
|