dsh-zdiary-kit 0.7.6 → 0.8.3
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 +67 -16
- package/dist/app/plugin.js +54 -20
- package/dist/cli.js +59 -15
- package/dist/features/readonly/feature.js +563 -0
- package/dist/features/readonly/html.js +255 -0
- package/dist/features/settings/client.js +11 -4
- package/dist/features/settings/feature.js +33 -5
- package/dist/features/zk-tools/feature.js +184 -41
- package/dist/lib/agents-template.js +49 -11
- package/dist/lib/dream.js +145 -16
- package/dist/lib/http.js +26 -0
- package/dist/lib/lint.js +302 -66
- package/dist/lib/memory.js +123 -14
- package/dist/lib/notebook.js +33 -14
- package/dist/lib/wikilink.js +77 -0
- package/dist/lib/zk.js +108 -17
- package/docs/features/memory.md +55 -8
- package/docs/features/provisioning.md +17 -6
- package/docs/features/readonly.md +73 -0
- package/docs/features/settings.md +12 -5
- package/docs/features/zk-tools.md +55 -6
- package/package.json +1 -1
package/dist/lib/dream.js
CHANGED
|
@@ -6,15 +6,24 @@
|
|
|
6
6
|
* · daily 里 `## Loose ends` 小节还没划掉的项;
|
|
7
7
|
* · 被引用最多的笔记(一次 `zk graph` 数入链)。
|
|
8
8
|
* 「rejected」是机械规则,不是相关性判断:authority=informal 与无标签两条,别再加。
|
|
9
|
+
*
|
|
10
|
+
* 晋升是一道**门**,门得知道门里已经有什么(P0-2):读 MEMORY.md 判「这条已经晋升过」,
|
|
11
|
+
* 已经进去的移出候选、单列一栏 —— 否则同一批笔记每周原样重复,仪式几次之后就没人再看了。
|
|
12
|
+
* 窗口默认也不再固定 7 天,而是跟着 DREAMS.md 的「上次做梦」走:漏做一周不会把笔记永久甩出窗口。
|
|
9
13
|
*/
|
|
10
14
|
import fs from 'node:fs';
|
|
11
15
|
import path from 'node:path';
|
|
12
16
|
import { allNotes, graphEdges, localDate, MAX_SCAN } from './zk.js';
|
|
13
17
|
import { parseFrontmatter } from './frontmatter.js';
|
|
18
|
+
import { normalizeTitle, resolveWikiTarget, WIKI_LINK, wikiIndex } from './wikilink.js';
|
|
14
19
|
/** 每栏最多回多少条(计划 §4 M3 的 50 条上限) */
|
|
15
20
|
export const MAX_ITEMS = 50;
|
|
16
21
|
/** topCited 只取前几名:给 agent 一个「本周被反复引用」的提示,不是排行榜 */
|
|
17
22
|
export const TOP_CITED = 10;
|
|
23
|
+
/** 窗口之外、还没晋升的笔记最多回多少条:漏做一周不该让它们永久掉队 */
|
|
24
|
+
export const MAX_BACKLOG = 10;
|
|
25
|
+
/** 标题包含匹配的最短长度:短标题(「RAG」「额度」)容易在任意句子里撞上 */
|
|
26
|
+
export const MIN_TITLE_MATCH = 6;
|
|
18
27
|
/** `## Loose ends` / `## 未完成` / `## 待办` 都算(大小写不敏感,单复数都认) */
|
|
19
28
|
const LOOSE_HEADING = /^##\s+(loose ends?|未完成|待办(?:事项)?)\s*$/i;
|
|
20
29
|
/** 周节奏:AGENTS.md 的「每周」就按这个天数算。没有定时器,只在记忆注入里提醒到期 */
|
|
@@ -73,13 +82,14 @@ export function looseEndsOf(content) {
|
|
|
73
82
|
/**
|
|
74
83
|
* 机械拒绝规则(只两条,别加语义判断):
|
|
75
84
|
* · authority=informal —— 口头/未验证来源,进长期记忆会污染结论;
|
|
76
|
-
* · 无标签 ——
|
|
85
|
+
* · 无标签 —— 标签是唯一能确定性筛选的维度;没标签的笔记晋升后只能靠全文检索找回来
|
|
86
|
+
* (P2-7 起中文检索不再漏,但「按主题找」还是得靠标签)。
|
|
77
87
|
*/
|
|
78
88
|
export function rejectionReason(note) {
|
|
79
89
|
if (note.authority === 'informal')
|
|
80
90
|
return 'authority: informal(口头/未验证来源)';
|
|
81
91
|
if (note.tags.length === 0)
|
|
82
|
-
return '
|
|
92
|
+
return '无标签(没有可筛选的标签,晋升后只能靠全文检索找回来)';
|
|
83
93
|
return null;
|
|
84
94
|
}
|
|
85
95
|
function isUnder(rel, dir) {
|
|
@@ -119,9 +129,66 @@ function excerptOf(cfg, note) {
|
|
|
119
129
|
}
|
|
120
130
|
return parts.join(' ').replace(/\s+/g, ' ').trim().slice(0, 200) || note.lead;
|
|
121
131
|
}
|
|
132
|
+
/**
|
|
133
|
+
* 窗口默认跟着 DREAMS.md 走:距上次做梦多少天,窗口就是多少天。
|
|
134
|
+
* 固定 7 天的话,漏做一周、或隔十天再整理,窗口外的笔记就再也不会出现在任何一栏 ——
|
|
135
|
+
* 那不是「这次没选上」,是「永远消失」。没有 DREAMS.md / 日期不合法 → 退回 7 天(旧行为)。
|
|
136
|
+
* 显式 `--days` 仍然优先:调用方说要看几天就看几天。
|
|
137
|
+
*/
|
|
138
|
+
function windowDaysOf(cfg, now, explicit) {
|
|
139
|
+
const clamp = (n) => Math.min(365, Math.max(1, Math.trunc(n)));
|
|
140
|
+
if (explicit !== undefined)
|
|
141
|
+
return clamp(explicit);
|
|
142
|
+
let body = '';
|
|
143
|
+
try {
|
|
144
|
+
body = fs.readFileSync(path.join(cfg.notebook, 'DREAMS.md'), 'utf8');
|
|
145
|
+
}
|
|
146
|
+
catch {
|
|
147
|
+
return DREAM_INTERVAL_DAYS;
|
|
148
|
+
}
|
|
149
|
+
const last = lastDreamDate(body);
|
|
150
|
+
const days = last ? daysSinceDream(last, now) : null;
|
|
151
|
+
return days === null ? DREAM_INTERVAL_DAYS : clamp(days);
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* 已晋升集合:MEMORY.md 里的 `[[链接]]` + 纯文字条目里出现的标题。
|
|
155
|
+
*
|
|
156
|
+
* 为什么要有第二条:真机 MEMORY.md 7 条里 6 条是纯文字(没有链接),只按链接判会漏掉它们,
|
|
157
|
+
* 而它们正是每周重复出现的候选来源。标题包含只做字符串比较、且要求标题 ≥ 6 字 ——
|
|
158
|
+
* 短标题(「RAG」「额度」)在任意句子里都可能撞上,宁可重复报,不猜。
|
|
159
|
+
*/
|
|
160
|
+
function promotionsOf(cfg, notes) {
|
|
161
|
+
const out = new Map();
|
|
162
|
+
let body = '';
|
|
163
|
+
try {
|
|
164
|
+
body = fs.readFileSync(path.join(cfg.notebook, 'MEMORY.md'), 'utf8');
|
|
165
|
+
}
|
|
166
|
+
catch {
|
|
167
|
+
return out;
|
|
168
|
+
}
|
|
169
|
+
if (!body.trim())
|
|
170
|
+
return out;
|
|
171
|
+
const index = wikiIndex(notes);
|
|
172
|
+
for (const match of body.matchAll(WIKI_LINK)) {
|
|
173
|
+
const hit = resolveWikiTarget(cfg, String(match[1] ?? '').trim(), index);
|
|
174
|
+
if (hit.status === 'live' && hit.path)
|
|
175
|
+
out.set(hit.path, 'link');
|
|
176
|
+
}
|
|
177
|
+
const lines = body.split('\n').map((line) => normalizeTitle(line)).filter(Boolean);
|
|
178
|
+
for (const note of notes) {
|
|
179
|
+
if (out.has(note.path))
|
|
180
|
+
continue;
|
|
181
|
+
const key = normalizeTitle(note.title);
|
|
182
|
+
if ([...key].length < MIN_TITLE_MATCH)
|
|
183
|
+
continue;
|
|
184
|
+
if (lines.some((line) => line.includes(key)))
|
|
185
|
+
out.set(note.path, 'title');
|
|
186
|
+
}
|
|
187
|
+
return out;
|
|
188
|
+
}
|
|
122
189
|
export async function dreamNotebook(cfg, opts = {}, signal) {
|
|
123
|
-
const windowDays = Math.min(365, Math.max(1, Math.trunc(opts.days ?? 7)));
|
|
124
190
|
const now = opts.now ?? new Date();
|
|
191
|
+
const windowDays = windowDaysOf(cfg, now, opts.days);
|
|
125
192
|
const sinceMs = now.getTime() - windowDays * 86_400_000;
|
|
126
193
|
// 顺序跑:两个 zk 进程同时补索引会撞 SQLite 写锁(lint 里同样处理)
|
|
127
194
|
const notes = await allNotes(cfg, {}, signal);
|
|
@@ -137,6 +204,48 @@ export async function dreamNotebook(cfg, opts = {}, signal) {
|
|
|
137
204
|
inbound.set(edge.targetPath, sources);
|
|
138
205
|
}
|
|
139
206
|
const citedOf = (n) => inbound.get(n.path)?.length ?? 0;
|
|
207
|
+
// 使用信号(P2-12):日记是本插件里唯一「按时间记录真实工作」的层,正好当召回日志用。
|
|
208
|
+
// 零新增状态:来源路径与日期都在已载入的 notes / edges 里。
|
|
209
|
+
const byPath = new Map(notes.map((n) => [n.path, n]));
|
|
210
|
+
const usageCache = new Map();
|
|
211
|
+
const usageOf = (rel) => {
|
|
212
|
+
const cached = usageCache.get(rel);
|
|
213
|
+
if (cached)
|
|
214
|
+
return cached;
|
|
215
|
+
const sources = (inbound.get(rel) ?? []).filter((p) => isUnder(p, cfg.dailyDir));
|
|
216
|
+
const dates = new Set();
|
|
217
|
+
for (const source of sources) {
|
|
218
|
+
// 日记的文件名就是日期(zk 的 created 是文件创建时间,补写旧日记时会全是今天);
|
|
219
|
+
// 文件名不是日期形状(自定义模板)才退回 created。
|
|
220
|
+
const stem = path.basename(source, path.extname(source));
|
|
221
|
+
const day = /^\d{4}-\d{2}-\d{2}$/.test(stem) ? stem : localDate(byPath.get(source)?.created ?? '');
|
|
222
|
+
if (day)
|
|
223
|
+
dates.add(day);
|
|
224
|
+
}
|
|
225
|
+
const sorted = [...dates].sort();
|
|
226
|
+
const value = {
|
|
227
|
+
dailyMentions: sources.length,
|
|
228
|
+
daysMentioned: dates.size,
|
|
229
|
+
lastMentioned: sorted.length ? sorted[sorted.length - 1] : '',
|
|
230
|
+
};
|
|
231
|
+
usageCache.set(rel, value);
|
|
232
|
+
return value;
|
|
233
|
+
};
|
|
234
|
+
const candidateOf = (note, promotedBy) => ({
|
|
235
|
+
title: note.title,
|
|
236
|
+
path: note.path,
|
|
237
|
+
type: note.type,
|
|
238
|
+
authority: note.authority,
|
|
239
|
+
tags: note.tags,
|
|
240
|
+
created: localDate(note.created),
|
|
241
|
+
lead: excerptOf(cfg, note),
|
|
242
|
+
cited: citedOf(note),
|
|
243
|
+
citedBy: (inbound.get(note.path) ?? []).slice(0, 5),
|
|
244
|
+
promoted: promotedBy !== '',
|
|
245
|
+
promotedBy,
|
|
246
|
+
...usageOf(note.path),
|
|
247
|
+
});
|
|
248
|
+
const promoted = promotionsOf(cfg, notes);
|
|
140
249
|
const windowNotes = notes
|
|
141
250
|
.filter((n) => {
|
|
142
251
|
const at = Date.parse(n.created);
|
|
@@ -144,28 +253,38 @@ export async function dreamNotebook(cfg, opts = {}, signal) {
|
|
|
144
253
|
})
|
|
145
254
|
.sort((a, b) => Date.parse(b.created) - Date.parse(a.created));
|
|
146
255
|
const newNotes = [];
|
|
256
|
+
const alreadyPromoted = [];
|
|
147
257
|
const rejected = [];
|
|
148
258
|
for (const note of windowNotes) {
|
|
149
259
|
// 日记不是晋升候选:它的「未完成项」走 dailyLooseEnds,报成「无标签」只会制造噪音
|
|
150
260
|
if (isUnder(note.path, cfg.dailyDir))
|
|
151
261
|
continue;
|
|
262
|
+
// 已经进 MEMORY 的先说:否则每周原样重复,仪式几次之后就没人看了
|
|
263
|
+
const by = promoted.get(note.path);
|
|
264
|
+
if (by) {
|
|
265
|
+
alreadyPromoted.push(candidateOf(note, by));
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
152
268
|
const reason = rejectionReason(note);
|
|
153
269
|
if (reason) {
|
|
154
270
|
rejected.push({ title: note.title, path: note.path, reason });
|
|
155
271
|
continue;
|
|
156
272
|
}
|
|
157
|
-
newNotes.push(
|
|
158
|
-
title: note.title,
|
|
159
|
-
path: note.path,
|
|
160
|
-
type: note.type,
|
|
161
|
-
authority: note.authority,
|
|
162
|
-
tags: note.tags,
|
|
163
|
-
created: localDate(note.created),
|
|
164
|
-
lead: excerptOf(cfg, note),
|
|
165
|
-
cited: citedOf(note),
|
|
166
|
-
citedBy: (inbound.get(note.path) ?? []).slice(0, 5),
|
|
167
|
-
});
|
|
273
|
+
newNotes.push(candidateOf(note, ''));
|
|
168
274
|
}
|
|
275
|
+
// 窗口外仍未晋升、且过机械门的:按被引数排,别让「漏做一次」变成永久掉队
|
|
276
|
+
const backlogNotes = notes
|
|
277
|
+
.filter((n) => {
|
|
278
|
+
if (isUnder(n.path, cfg.dailyDir))
|
|
279
|
+
return false;
|
|
280
|
+
const at = Date.parse(n.created);
|
|
281
|
+
if (Number.isFinite(at) && at >= sinceMs)
|
|
282
|
+
return false;
|
|
283
|
+
if (promoted.has(n.path))
|
|
284
|
+
return false;
|
|
285
|
+
return rejectionReason(n) === null;
|
|
286
|
+
})
|
|
287
|
+
.sort((a, b) => citedOf(b) - citedOf(a) || a.title.localeCompare(b.title));
|
|
169
288
|
const dailyLooseEnds = [];
|
|
170
289
|
let looseEndCount = 0;
|
|
171
290
|
for (const note of windowNotes) {
|
|
@@ -186,10 +305,16 @@ export async function dreamNotebook(cfg, opts = {}, signal) {
|
|
|
186
305
|
dailyLooseEnds.push({ path: note.path, date: localDate(note.created), items });
|
|
187
306
|
}
|
|
188
307
|
}
|
|
308
|
+
// 排序说明(P2-12):先看「被日记提过几天」—— 跨天复现比同一天多次更强;同一天多篇日记
|
|
309
|
+
// 再按 dailyMentions 排,入链数只是最后的次序。没有日记的库两项都是 0,
|
|
310
|
+
// 排序自动退回原来的 cited 降序(行为兼容)。
|
|
189
311
|
const topCited = notes
|
|
190
|
-
.map((n) => ({ title: n.title, path: n.path, cited: citedOf(n) }))
|
|
312
|
+
.map((n) => ({ title: n.title, path: n.path, cited: citedOf(n), ...usageOf(n.path) }))
|
|
191
313
|
.filter((x) => x.cited > 0)
|
|
192
|
-
.sort((a, b) => b.
|
|
314
|
+
.sort((a, b) => b.daysMentioned - a.daysMentioned ||
|
|
315
|
+
b.dailyMentions - a.dailyMentions ||
|
|
316
|
+
b.cited - a.cited ||
|
|
317
|
+
a.title.localeCompare(b.title))
|
|
193
318
|
.slice(0, TOP_CITED);
|
|
194
319
|
return {
|
|
195
320
|
notebook: cfg.notebook,
|
|
@@ -202,8 +327,12 @@ export async function dreamNotebook(cfg, opts = {}, signal) {
|
|
|
202
327
|
candidates: newNotes.length,
|
|
203
328
|
rejected: rejected.length,
|
|
204
329
|
looseEnds: looseEndCount,
|
|
330
|
+
promoted: alreadyPromoted.length,
|
|
331
|
+
backlog: backlogNotes.length,
|
|
205
332
|
},
|
|
206
333
|
newNotes: newNotes.slice(0, MAX_ITEMS),
|
|
334
|
+
alreadyPromoted: alreadyPromoted.slice(0, MAX_ITEMS),
|
|
335
|
+
backlog: backlogNotes.slice(0, MAX_BACKLOG).map((n) => candidateOf(n, '')),
|
|
207
336
|
rejected: rejected.slice(0, MAX_ITEMS),
|
|
208
337
|
topCited,
|
|
209
338
|
dailyLooseEnds,
|
package/dist/lib/http.js
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 本插件所有服务端路由的前缀(设置页与只读视图共用同一个真相)。
|
|
3
|
+
* client.ts 因「单文件零 import」的宿主约束另存了一份字面量 —— 改这里记得同步那边。
|
|
4
|
+
*/
|
|
5
|
+
export const ROUTE_BASE = '/dsh-zdiary-kit';
|
|
1
6
|
export function json(res, status, body) {
|
|
2
7
|
const payload = JSON.stringify(body);
|
|
3
8
|
res.statusCode = status;
|
|
@@ -18,3 +23,24 @@ export async function readBody(req, limit = 1024 * 1024) {
|
|
|
18
23
|
}
|
|
19
24
|
return Buffer.concat(chunks).toString('utf8');
|
|
20
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* Host 头是不是回环。只证明「地址栏里写的是 127.0.0.1」——
|
|
28
|
+
* 单独用它会被 DNS rebinding 骗(恶意域名解析到 127.0.0.1,Host 还是域名,这里挡得住,
|
|
29
|
+
* 但反过来把 Host 伪造成 127.0.0.1 的请求挡不住)。要收紧就跟 isLoopbackPeer 一起判。
|
|
30
|
+
*/
|
|
31
|
+
export function isLoopbackHost(req) {
|
|
32
|
+
const host = String(req.headers?.host ?? '').toLowerCase();
|
|
33
|
+
const h = host.startsWith('[') ? host.slice(0, host.indexOf(']') + 1) : host.split(':')[0];
|
|
34
|
+
return h === '127.0.0.1' || h === 'localhost' || h === '[::1]' || h === '::1';
|
|
35
|
+
}
|
|
36
|
+
/** 对端 socket 地址是不是回环;没有对端地址(unix socket / 测试替身)不算反证 */
|
|
37
|
+
export function isLoopbackPeer(req) {
|
|
38
|
+
const addr = req.socket?.remoteAddress;
|
|
39
|
+
if (!addr)
|
|
40
|
+
return true;
|
|
41
|
+
return addr === '127.0.0.1' || addr === '::1' || addr === '::ffff:127.0.0.1';
|
|
42
|
+
}
|
|
43
|
+
/** 两边都在回环才算「本机打开」—— 只读视图用它,比只看 Host 严格 */
|
|
44
|
+
export function isLoopback(req) {
|
|
45
|
+
return isLoopbackHost(req) && isLoopbackPeer(req);
|
|
46
|
+
}
|