dsh-zdiary-kit 3.8.0 → 3.9.0
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 +29 -10
- package/dist/cli.js +51 -4
- package/dist/features/zk-tools/feature.js +229 -14
- package/dist/lib/agents-template.js +6 -5
- package/dist/lib/archive-probe.js +159 -0
- package/dist/lib/edge-context.js +95 -0
- package/dist/lib/entry-points.js +204 -0
- package/dist/lib/recall.js +30 -1
- package/dist/lib/usage.js +60 -0
- package/dist/lib/zk.js +145 -3
- package/docs/features/memory.md +20 -3
- package/docs/features/zk-tools.md +63 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# dsh-zdiary-kit 3.
|
|
1
|
+
# dsh-zdiary-kit 3.9.0
|
|
2
2
|
|
|
3
3
|
> 给 [DSH](https://github.com/deepseek-ai/deepseek-harness) 装一个**内置 [zk](https://github.com/zk-org/zk) 的知识库 / 日记本**:
|
|
4
4
|
> 插件提供一键安装 zk(官方 release;设置页按钮或 `zk-kit install`)、自动建笔记本(zk init + 日记模板 + 日记 group),
|
|
@@ -10,9 +10,10 @@
|
|
|
10
10
|
|
|
11
11
|
| 工具 | 干什么 |
|
|
12
12
|
|---|---|
|
|
13
|
-
| `zk_search` | 全文 / 标签 / 日期 / 断链 / 孤立笔记检索,返回标题、路径、标签、摘要与命中片段 `snippet`;中文自动并用子串、多词按覆盖度合并(`matchStrategy` 说明用了 `fts` / `regex` / `mixed`);有 query 时默认按 FTS 相关性排序,显式 `sort` 才改顺序;`total` 只在结果完整(未截断)时给,触顶看 `hasMore` / `completeness` |
|
|
14
|
-
| `zk_query` | 结构化检索:按 type / status / 标签 / 目录 / 日期过滤,默认只看 active 且不含归档;`include_archived` 连归档一起查(此时未显式给 status 则不再默认过滤 active
|
|
15
|
-
| `zk_recall` | 召回装配:先按 query / 过滤条件检索(与 `zk_search` 同一套 R1–R3),再按词数预算 `budget`(默认 500)把命中笔记拼成一段可直接读的上下文;每条带标题、路径、`absPath` 与全文词数,放不下就整条丢弃、不切半句,被挡下的原因在 `notes[].reason`;`used` 一定不超过 `budget`(中文 2 字 1 词);不读全文、不新增 zk 进程、不做重排 / 跨回合去重;`completeness` 沿用检索三态,装配过的 ≠
|
|
13
|
+
| `zk_search` | 全文 / 标签 / 日期 / 断链 / 孤立笔记检索,返回标题、路径、标签、摘要与命中片段 `snippet`;中文自动并用子串、多词按覆盖度合并(`matchStrategy` 说明用了 `fts` / `regex` / `mixed`);有 query 时默认按 FTS 相关性排序,显式 `sort` 才改顺序;`total` 只在结果完整(未截断)时给,触顶看 `hasMore` / `completeness`;带 `entryPoints`(MEMORY 的活条目:`via=query` 是「这条结论跟你的查询有关」,来源可以不在结果集里;`via=result` 是「这条结论指向本批结果」)与 `counterEntries`(归档里同名旧结论:`supersede` 的原因 + 被谁取代 —— 先看它,别把废掉的结论重新提出来) |
|
|
14
|
+
| `zk_query` | 结构化检索:按 type / status / 标签 / 目录 / 日期过滤,默认只看 active 且不含归档;`include_archived` 连归档一起查(此时未显式给 status 则不再默认过滤 active,也不再重复报死胡同 —— 归档已经在结果里);中文多词同样按覆盖度合并、`snippet` 与 `total` 口径同 `zk_search`;`entryPoints` / `counterEntries` 与 `zk_search` 同口径 |
|
|
15
|
+
| `zk_recall` | 召回装配:先按 query / 过滤条件检索(与 `zk_search` 同一套 R1–R3),再按词数预算 `budget`(默认 500)把命中笔记拼成一段可直接读的上下文;每条带标题、路径、`absPath` 与全文词数,放不下就整条丢弃、不切半句,被挡下的原因在 `notes[].reason`;`used` 一定不超过 `budget`(中文 2 字 1 词);不读全文、不新增 zk 进程、不做重排 / 跨回合去重;`completeness` 沿用检索三态,装配过的 ≠ 全部命中;`entryPoints` / `counterEntries` 与 `zk_search` 共用同一份计算,`via=query` 的入口卡会排在装配正文之前(占同一份预算),放不下就置 `entryPointsInText=false` 并在正文外单列(不静默丢)。命令行对应 `zk-kit recall` |
|
|
16
|
+
| `zk_walk` | 从一篇笔记出发走一跳邻居(只读):`direction=both` 看反链 + 出链,`in` 只看谁链到它,`out` 只看它链到谁;种子写相对 / 绝对路径或精确标题 / 稳定 id(`[[标题]]` / `[[id]]` 也认),不精确命中就返回空、不会退回全库;按篇反查、不建全图(路径种子 2 个子进程),`limit` 硬顶 200,触顶 `completeness=partial`(邻居没列全,不代表没有);每条邻居带 `section` / `linkLine`(那条边所在的小节与那一行 —— 没有它,同一篇种子的出链只有一串标题),只回标题 / 路径 / `absPath` / 摘要 / 那一行,全文仍用原生 read,**0 个额外子进程**。命令行对应 `zk-kit walk` |
|
|
16
17
|
| `zk_new` | 新建原子笔记(zk 生成文件名、套模板、写索引),可带摘要、标签与双链;`summary` 填进模板的 `## Summary`(正文里已写了这一节就不动,返回 `summaryApplied: false`);`name` 可直接指定短文件名(不给则按标题生成,中文标题得到拼音长名);layered 下不给 `dir` 就按 `type` 落位(source → notes/sources),两者都没有、或给的 `dir` 不在任何 group 里都直接报错;`tags` 只补正文里没有的;flat 下按标题查重,重复直接报错;归档里有同名旧结论只警告并带出废弃原因;type / authority 只有 layered 模板会写进 frontmatter;`raw/`、`inbox/`、`archive/` 与根目录 meta 文件一律拒写(写进去会被排除出索引 / 被当成人写的工作面;归档只放 `zk_supersede` 搬进去的旧结论);同目录里有「改一两个字」的相似标题时在 `similar` 里提示(带分数,只提示、不阻断);中文笔记没有任何标签时在 `tagWarning` 里提示(只提示,不阻断) |
|
|
17
18
|
| `zk_append` | 往已有笔记末尾续写(自动补空行,可先起二级标题);根目录 meta 文件拒写 |
|
|
18
19
|
| `zk_daily` | 打开/创建某天的日记,并追加一条带时间的记录(layered 模板写进 `## Events` 的 `### HH:MM`,flat 追加到文件末尾的 `## HH:MM`) |
|
|
@@ -22,7 +23,7 @@
|
|
|
22
23
|
| `zk_context` | 读知识库根目录的 `AGENTS.md` / `USER.md` / `MEMORY.md`(按注入预算截断)与角色人格 `PERSONA.md`(单独成段、不截断),并列出已配置的笔记本(名字 / 路径 / 结构 / 当前);宿主支持注入时这些内容每轮已自动带进系统提示。改 MEMORY 前用它看余量:`headroom` / `wouldTruncate` / `dropCandidates` 说明「再加一条会丢谁」,给 `proposed_entry` 或 `extra_words` 就能预估;同日条目按位置决胜(`sameDateCount`),不假装有使用频率 |
|
|
23
24
|
| `zk_lint` | 体检:解析失败 / 断链 / 孤儿 / 无标签 / 单向链接 / 陈旧 / 重名标题 / 疑似重复标题 / 记忆链接失效 / frontmatter 缺字段 / 标签卫生 / 模板版本 / 人格(缺失·旧版·比插件新·标记与内容不符·未填占位符)/ 日记时间轴(最近 90 天的空块·倒序·格式·位置),每类最多 50 条;非日记笔记到 150 篇且没有 `INDEX.md` 时提示一句;另给 `missingId`(缺稳定 id 的非日记笔记数,只提示不回填);只报告不改写 |
|
|
24
25
|
| `zk_dream` | 晋升材料:窗口新增(机械筛掉 `informal` 与无标签,带正文摘要与入链来源)、已在 MEMORY 的、窗口外积压、daily 未完成项、被引最多(先按「被日记提到几天」排);带 `missingId`(缺稳定 id 的非日记笔记数);只报告 |
|
|
25
|
-
| `zk_stats` | 笔记数 / 标签数 / 断链 / 孤立 / 归档(superseded
|
|
26
|
+
| `zk_stats` | 笔记数 / 标签数 / 断链 / 孤立 / 归档(superseded),顶层目录与前 10 个标签的实时分布;外加 `usage`:本地聚合计数(`walk` / `recall` / `entryPoints` / `counterEntries` 各用了几次,自某天起)—— 用来看「入口 + 一跳」这条路到底有没有被用上。只存计数,不记查询词、不记路径 |
|
|
26
27
|
| `zk_use` | 切换**本会话**用的笔记本(只影响这个会话,不改全局默认);不带参数就列出现有的与当前固定的 |
|
|
27
28
|
| `zk_<别名>` | 笔记本 `.zk/config.toml` 的 `[alias]` 里定义的自定义命令(例如 `lucky = "zk list --quiet --format full --sort random --limit 1"`)注册成工具:调用等于 `zk <别名> [args]`,`args` 追加到命令里的 `$@`;输出按原样返回。紧邻别名上方的注释(或行尾注释)作为工具说明;改完 config.toml 要重载插件才会出现新工具。**默认关闭**,见下 |
|
|
28
29
|
|
|
@@ -77,8 +78,6 @@ DSH 自带的 AGENTS.md 加载只看「项目根 → 当前目录」链,**不
|
|
|
77
78
|
**边界**:M1 是「一本笔记本一份」人格;多个 agent 共用一个库时先别启用,等 M4 的 per-agent persona ——
|
|
78
79
|
否则 A 的人格会串到 B。
|
|
79
80
|
|
|
80
|
-
**从 1.x 升级**:根目录的 `POLICY.md` 不再解析或注入(2.0.0 移除);文件不会被删,但会按普通笔记处理
|
|
81
|
-
(可检索、计入统计,不再受 meta 写保护)。建议删除,或把「别自动发布 / 别花钱」这类边界写进 `AGENTS.md` / `PERSONA.md`。
|
|
82
81
|
**AGENTS.md 模板升到 v7**(精简模板):已有文件不会被覆盖,
|
|
83
82
|
`zk_lint` 会把它报成旧版、并把新版全文附在报告里 —— 自己挑要哪几条搬过去。
|
|
84
83
|
|
|
@@ -307,6 +306,25 @@ zk-kit groups --fix # 只改 dsh-zdiary-kit 自己写的 group:paths 行
|
|
|
307
306
|
与根目录的 `AGENTS.md` / `MEMORY.md` / `USER.md` / `DREAMS.md` / `INDEX.md` / `WORKFLOW.md` 不进检索与笔记 / 标签 / 断链 / 孤立统计;
|
|
308
307
|
`archive/` 的归档数由 `zk_stats` 单独统计(`archived` / `superseded`)。`-x` 传绝对路径:它是相对 cwd 解析的,传相对名在换工作目录后会静默失效。
|
|
309
308
|
|
|
309
|
+
## 检索路径:入口、一跳、死胡同
|
|
310
|
+
|
|
311
|
+
插件借了卢曼卡片盒的**写法规约**(一笔记一件事 / 标题即结论 / 只连真实概念关联 / supersede 不删),
|
|
312
|
+
检索这一侧只补了三件「纸质约束不成立、但机制可移植」的事,其余一律加法:
|
|
313
|
+
|
|
314
|
+
- **入口不只有全文索引**:`MEMORY.md` 每条活条目就是一张「概念 → 入口卡」。
|
|
315
|
+
`zk_search` / `zk_query` / `zk_recall` 的 `entryPoints` 里,`via=query` 表示这条结论跟你的查询有关 ——
|
|
316
|
+
它指向的笔记**可以不在本次结果里**(路径式来源直接落地;标题式只给入口名,用 `zk_walk` 跟进去)。
|
|
317
|
+
- **一跳**:`zk_walk` 从任意一篇出发看邻居,每条带 `section` / `linkLine`,即「这条边为什么存在」。
|
|
318
|
+
不做 depth 2 / 递归 / 全图游走:agent 拿到 `absPath` 就能自己连走,把深度做进工具只是省一次往返,
|
|
319
|
+
却要重新过一遍成本表。
|
|
320
|
+
- **死胡同**:`counterEntries` 把 `archive/` 里同名(或标题高度相似)的旧结论带出来,附 `supersede` 的原因与
|
|
321
|
+
被谁取代。`zk_supersede` 的旧结论默认不进检索 —— 这条提醒就是补那个洞:**「库里没有」不等于「从来没出现过」**。
|
|
322
|
+
0 命中时也会查一次(拿查询词当标题比),文案会明说「没有活笔记命中,但归档里有同名旧结论」。
|
|
323
|
+
|
|
324
|
+
这三件都是本地读 / 已付过费的字节:`zk_walk` 的子进程数与 3.9.0 之前一样(路径种子 2 个),
|
|
325
|
+
`entryPoints` / `counterEntries` **不新增 zk 进程**(10k 夹具上死胡同探针 p50 约 50 ms,入口计算 < 5 ms)。
|
|
326
|
+
`zk_stats.usage` 的计数落在 `<笔记本>/.zk/dsh-usage.json`,只有键与数字。
|
|
327
|
+
|
|
310
328
|
## 索引与「写后可读」
|
|
311
329
|
|
|
312
330
|
zk 的索引(`.zk/notebook.db`)是缓存:文件落盘不等于能搜到。插件在写入**落盘之后**记一个 dirty 标记
|
|
@@ -370,6 +388,7 @@ zk-kit doctor # 自检:zk / 笔记本 / 模板 / group
|
|
|
370
388
|
| 读 | `zk-kit read notes/f2sj.md`,或 `zk-kit read --query 搜索词`;`--max-chars` 可调 |
|
|
371
389
|
| 续写 | `zk-kit append notes/f2sj.md --text "追一段" --heading 后续` |
|
|
372
390
|
| 双链 | `zk-kit link notes/a.md "某篇标题"` |
|
|
391
|
+
| 一跳 | `zk-kit walk notes/a.md --direction both --limit 10`(`--limit` 是**每个方向**的上限;种子也可写精确标题 / 稳定 id;按篇反查、不建全图) |
|
|
373
392
|
| 指定目录 | `zk-kit new --title 标题 --dir notes/concepts`(layered 下 type 由目录所属 group 决定,目录必须在 group 下);也可以只给 `--type concept`,目录跟着类型走;两者都没有、或目录不在 group 里都会报错 |
|
|
374
393
|
| 指定文件名 | `zk-kit new --title 很长的中文结论标题 --name 短文件名`(只换文件名,标题与正文不动;不给就是拼音 slug) |
|
|
375
394
|
| 带摘要 | `zk-kit new --title 标题 --summary "一句话"`(写进模板的 `## Summary`;正文里已写了就不覆盖并告警) |
|
|
@@ -442,13 +461,13 @@ zk-kit doctor # 自检:zk / 笔记本 / 模板 / group
|
|
|
442
461
|
浏览器半边 `client.ts` 零 import,`postbuild` 把 tsc 补的 `export {}` 删掉,并检查没有顶层声明
|
|
443
462
|
(DSH 会把多个插件的 `client.js` 拼进同一个 combo 脚本,顶层 `const` / `function` 会撞名导致插件不激活)。
|
|
444
463
|
- `bun run typecheck` · `bun run build` · `bun run test`(真起 zk)·
|
|
445
|
-
`bun run smoke`(加载 dist:
|
|
464
|
+
`bun run smoke`(加载 dist:15 个内置工具各跑一遍 + 12 条路由(设置 8 + 只读 4)都发真实请求(`/install` 只验非法版本,不下载),并检查输出 schema 在 DSH 支持的子集内)·
|
|
446
465
|
`bun run docs`(把 README / docs 里 `zk-kit …` 代码块在临时 vault 上逐条真跑;`smoke` 末尾会带上)·
|
|
447
466
|
`bun run fixture`(生成 1k / 10k 规模夹具;库规模 / 链接密度与各用例的 10k 实测、复跑方法见 `docs/bench-baseline.md`;
|
|
448
467
|
`--links-per-note` 决定链接密度 —— 建索引成本主要花在链接上,产出目录自带 `.zk/config.toml`,进去 `zk index` 即可)·
|
|
449
468
|
`bun run bench`(成本表:逐用例真起 zk,记子进程数 / 回传字节 / 峰值 RSS / p50 / p95 与库的篇数 / 链接数;默认 1k、3 轮;
|
|
450
469
|
全量 10k 实测与复跑方法见 `docs/bench-baseline.md`)
|
|
451
|
-
- **耗时差两个数量级**:`bun run test` 约
|
|
470
|
+
- **耗时差两个数量级**:`bun run test` 约 110 s(248 个用例,真起 zk;机器快慢能差一倍);`fixture` 百毫秒级;`bench` 默认 1k 约十几秒,
|
|
452
471
|
10k 上光一条 `graph.full` 就是 30 s、冷建索引几分钟。所以 `bench` **不在** `check` / CI 里,跟测试完全分开,不会因为你跑测试而变慢。
|
|
453
472
|
- `bun run test` 会先按插件自己的顺序找 zk(配置 → `ZK_BIN` → PATH → `~/.dsh/zdiary/bin`),跑完要求 **skip = 0**:
|
|
454
473
|
集成测试跳过一律按失败处理,否则「全绿」可能是「什么都没跑」。要换 zk 就 `ZK_BIN=/path/to/zk bun run check`。
|
|
@@ -478,7 +497,7 @@ src/
|
|
|
478
497
|
│ ├── dsh.ts # 宿主契约(纯类型,编译后为空)
|
|
479
498
|
│ └── exec.ts / http.ts / json-file.ts
|
|
480
499
|
├── features/
|
|
481
|
-
│ ├── zk-tools/feature.ts #
|
|
500
|
+
│ ├── zk-tools/feature.ts # 15 个内置工具 + [alias] 别名工具
|
|
482
501
|
│ ├── settings/ # 宿主 API + 浏览器 section
|
|
483
502
|
│ └── readonly/ # 只读视图:GET-only 路由 + Web 1.0 渲染(无 JS)
|
|
484
503
|
└── cli.ts # zk-kit 命令行
|
package/dist/cli.js
CHANGED
|
@@ -9,9 +9,11 @@ import { CFG_FILE } from './config/paths.js';
|
|
|
9
9
|
import { NOTE_KINDS, ensureNotebook, fixGroupConfig, groupOverlapNotes, newNoteDir, structureStatus } from './lib/notebook.js';
|
|
10
10
|
import { buildMemoryPayload, formatFileShares } from './lib/memory.js';
|
|
11
11
|
import { installZk, sourceLabel } from './lib/install.js';
|
|
12
|
-
import { resolveZk, searchNotesDetailed, queryNotes, createNote, readNote, findNote, appendToNote, ensureDailyNote, appendDaily, linkNotes, notebookStats, indexNotebook, indexPolicy, withTags, taglessCjkWarning, hasNotebook, localDate, ZkError, allNotes, parseErrorWarnings, indexLagHint, } from './lib/zk.js';
|
|
12
|
+
import { resolveZk, searchNotesDetailed, queryNotes, createNote, readNote, findNote, appendToNote, ensureDailyNote, appendDaily, linkNotes, notebookStats, indexNotebook, indexPolicy, withTags, taglessCjkWarning, hasNotebook, localDate, ZkError, allNotes, parseErrorWarnings, indexLagHint, walkFrom, } from './lib/zk.js';
|
|
13
13
|
import { formatLintExtras, formatMemoryLinks, lintNotebook, personaStatus, timelineStatus } from './lib/lint.js';
|
|
14
14
|
import { assembleRecall } from './lib/recall.js';
|
|
15
|
+
import { entryPointLines, entryPointsOf } from './lib/entry-points.js';
|
|
16
|
+
import { archivedCounterparts, describeCounterEntries } from './lib/archive-probe.js';
|
|
15
17
|
import { dreamNotebook, STALE_DAYS } from './lib/dream.js';
|
|
16
18
|
const HELP = `zk-kit —— dsh-zdiary-kit 的命令行
|
|
17
19
|
|
|
@@ -25,6 +27,8 @@ const HELP = `zk-kit —— dsh-zdiary-kit 的命令行
|
|
|
25
27
|
zk-kit search [词] [--tags a,b] [--since 日期] [--limit 5] [--json]
|
|
26
28
|
zk-kit recall [词] [--budget 500] [--limit 30] [--tags a,b] [--json]
|
|
27
29
|
检索 + 按词数预算装配成一段上下文(只读)
|
|
30
|
+
zk-kit walk <种子> [--direction both|in|out] [--limit 10] [--json]
|
|
31
|
+
一跳邻居(按篇反查,不建全图;--limit 是每个方向的上限;种子可为路径 / 精确标题 / id,只读)
|
|
28
32
|
zk-kit read <路径> | --query 词 [--max-chars n] [--json]
|
|
29
33
|
zk-kit daily [--entry 一段] [--date yesterday]
|
|
30
34
|
zk-kit append <路径> --text 一段 [--heading 标题]
|
|
@@ -549,7 +553,12 @@ async function main() {
|
|
|
549
553
|
const how = result.matchStrategy === 'mixed'
|
|
550
554
|
? '\n(中文查询:全文 + 子串合并,已补上全文索引漏掉的)'
|
|
551
555
|
: result.matchStrategy === 'regex' ? '\n(全文无结果,已退回子串匹配)' : '';
|
|
552
|
-
|
|
556
|
+
// 与 zk_search 同口径:MEMORY 入口 + 死胡同提醒(都是零子进程的本地读),措辞共用一份
|
|
557
|
+
const entryPoints = entryPointsOf(cfg, result.notes, { query });
|
|
558
|
+
const entryLines = entryPointLines(entryPoints.points, { truncated: entryPoints.truncated });
|
|
559
|
+
const counters = describeCounterEntries(archivedCounterparts(cfg, result.notes, { query }), { zeroHit: !result.notes.length });
|
|
560
|
+
const counterLines = counters ? '\n\n' + counters : '';
|
|
561
|
+
print(json ? result.notes : noteList(result.notes) + how + entryLines + counterLines, json);
|
|
553
562
|
return;
|
|
554
563
|
}
|
|
555
564
|
case 'recall': {
|
|
@@ -563,8 +572,46 @@ async function main() {
|
|
|
563
572
|
budget: Number(str(flags.budget)) || undefined,
|
|
564
573
|
});
|
|
565
574
|
const kept = r.notes.filter((n) => n.included).length;
|
|
566
|
-
|
|
567
|
-
|
|
575
|
+
const counters = describeCounterEntries({ entries: r.counterEntries, truncated: r.counterEntriesTruncated }, { zeroHit: !r.notes.length });
|
|
576
|
+
// 入口块放不进预算时(entryPointsInText === false)在正文外补回来:与工具同口径,不静默丢
|
|
577
|
+
const entries = entryPointLines(r.entryPoints, { truncated: r.entryPointsTruncated, skipQuery: r.entryPointsInText });
|
|
578
|
+
// 命中却全被预算挡下 ≠ 没有匹配:别把「装不下」说成「库里没有」
|
|
579
|
+
const body = r.text || (r.notes.length
|
|
580
|
+
? `命中 ${r.notes.length} 篇,但都放不进 ${r.budget} 词预算(--json 的 notes[].reason 说明每条为什么被挡)`
|
|
581
|
+
: '没有匹配的笔记');
|
|
582
|
+
print(json ? r : body +
|
|
583
|
+
`\n\n(${r.used}/${r.budget} 词 · 收录 ${kept}/${r.notes.length} 篇)` +
|
|
584
|
+
entries + (counters ? '\n\n' + counters : ''), json);
|
|
585
|
+
return;
|
|
586
|
+
}
|
|
587
|
+
case 'walk': {
|
|
588
|
+
const seed = pos[0] || str(flags.path);
|
|
589
|
+
if (!seed)
|
|
590
|
+
throw new ZkError('用法:zk-kit walk <路径 / 精确标题 / id> [--direction both|in|out] [--limit 10]');
|
|
591
|
+
await ensureNotebook(cfg);
|
|
592
|
+
const direction = (str(flags.direction) || 'both');
|
|
593
|
+
if (!['both', 'in', 'out'].includes(direction))
|
|
594
|
+
throw new ZkError(`方向只能是 both / in / out:${direction}`);
|
|
595
|
+
const r = await walkFrom(cfg, {
|
|
596
|
+
seed,
|
|
597
|
+
direction,
|
|
598
|
+
limit: Number(str(flags.limit)) || undefined,
|
|
599
|
+
includeArchived: flags.include_archived === true,
|
|
600
|
+
});
|
|
601
|
+
if (json) {
|
|
602
|
+
print(r, true);
|
|
603
|
+
return;
|
|
604
|
+
}
|
|
605
|
+
if (!r.seed) {
|
|
606
|
+
print(`没找到种子笔记:${seed}\n要写路径,或精确标题 / id;不精确匹配时不猜`, false);
|
|
607
|
+
return;
|
|
608
|
+
}
|
|
609
|
+
const head = `${r.notes.length} 篇 1 跳邻居(种子 ${r.seed.path})`;
|
|
610
|
+
const lines = r.notes.map((n) => {
|
|
611
|
+
const why = n.linkLine ? `\n 〔${n.section || '无小节'}〕 ${n.linkLine}` : '';
|
|
612
|
+
return `${n.relation === 'in' ? '←' : '→'} ${n.path} ${n.title}${why}`;
|
|
613
|
+
});
|
|
614
|
+
print([head, ...lines, r.truncated ? '(邻居没列全:触到 limit)' : ''].filter(Boolean).join('\n'), false);
|
|
568
615
|
return;
|
|
569
616
|
}
|
|
570
617
|
case 'read': {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* zk 工具(feature 的公共入口):
|
|
2
|
+
* zk 工具(feature 的公共入口):15 个内置工具 + `[alias]` 别名工具,全部经 lib/zk.ts。
|
|
3
3
|
*
|
|
4
4
|
* 设计要点:
|
|
5
5
|
* · 除 zk_context(只读 meta 文件,不依赖 zk)外,每个工具进来先 ensureNotebook —— 第一次调用就自动建好笔记本,用户不用先点设置页;
|
|
@@ -15,10 +15,13 @@ import { configFor, pinSession, sessionNotebook, sessionState } from '../../conf
|
|
|
15
15
|
import { defaultNotebook, expandHome, processWorkspace, resolveInWorkspace } from '../../config/paths.js';
|
|
16
16
|
import { S, validateArgs } from '../../lib/tool.js';
|
|
17
17
|
import { ensureNotebook, newNoteDir } from '../../lib/notebook.js';
|
|
18
|
-
import { searchNotes, searchNotesDetailed, createNote, appendToNote, ensureDailyNote, appendDaily, linkNotes, withTags, taglessCjkWarning, notebookStats, supersedeNote, renameNote, hasNotebook, isUnderDir, localDate as dateOnly, runAlias, hasCjk, ZkError, } from '../../lib/zk.js';
|
|
18
|
+
import { searchNotes, searchNotesDetailed, createNote, appendToNote, ensureDailyNote, appendDaily, linkNotes, withTags, taglessCjkWarning, notebookStats, supersedeNote, renameNote, hasNotebook, isUnderDir, localDate as dateOnly, runAlias, hasCjk, walkFrom, ZkError, } from '../../lib/zk.js';
|
|
19
19
|
import { configuredAliases, readAliases } from '../../lib/alias.js';
|
|
20
20
|
import { buildMemoryPayload, memoryCapacity, META_FILES } from '../../lib/memory.js';
|
|
21
21
|
import { assembleRecall } from '../../lib/recall.js';
|
|
22
|
+
import { entryPointLines, entryPointsOf } from '../../lib/entry-points.js';
|
|
23
|
+
import { archivedCounterparts, describeCounterEntries } from '../../lib/archive-probe.js';
|
|
24
|
+
import { bumpUsage, readUsage } from '../../lib/usage.js';
|
|
22
25
|
import { META_NAMES } from '../../lib/meta.js';
|
|
23
26
|
import { formatLintExtras, formatMemoryLinks, lintNotebook } from '../../lib/lint.js';
|
|
24
27
|
import { dreamNotebook, STALE_DAYS } from '../../lib/dream.js';
|
|
@@ -39,6 +42,26 @@ const objSchema = (properties, required) => ({
|
|
|
39
42
|
});
|
|
40
43
|
/** lint 输出的三个共用形状(每类最多 50 条 + 总数) */
|
|
41
44
|
const LINT_ENTRY = objSchema({ title: { type: 'string' }, path: { type: 'string' } }, ['title', 'path']);
|
|
45
|
+
/** R9:MEMORY.md 的活条目 → 入口(`via: query` 的来源可以不在本次结果里) */
|
|
46
|
+
const ENTRY_POINT = objSchema({
|
|
47
|
+
entry: { type: 'string' },
|
|
48
|
+
title: { type: 'string' },
|
|
49
|
+
path: { type: 'string' },
|
|
50
|
+
absPath: { type: 'string' },
|
|
51
|
+
via: { type: 'string', enum: ['query', 'result'] },
|
|
52
|
+
/** false = 标题 / id 写法且不在本批结果里:只有入口名,落地交给 zk_walk */
|
|
53
|
+
resolved: { type: 'boolean' },
|
|
54
|
+
}, ['entry', 'title', 'via', 'resolved']);
|
|
55
|
+
/** 死胡同探针:归档里同名的废弃结论(不是第二份结果列表) */
|
|
56
|
+
const COUNTER_ENTRY = objSchema({
|
|
57
|
+
matched: { type: 'string' },
|
|
58
|
+
title: { type: 'string' },
|
|
59
|
+
path: { type: 'string' },
|
|
60
|
+
absPath: { type: 'string' },
|
|
61
|
+
reason: { type: 'string' },
|
|
62
|
+
supersededBy: { type: 'string' },
|
|
63
|
+
score: { type: 'number' },
|
|
64
|
+
}, ['matched', 'title', 'path', 'absPath', 'reason', 'supersededBy', 'score']);
|
|
42
65
|
const LINT_BUCKET = objSchema({ total: { type: 'number' }, items: { type: 'array', items: LINT_ENTRY } }, ['total', 'items']);
|
|
43
66
|
const LINT_ONE_WAY_ITEM = objSchema({
|
|
44
67
|
title: { type: 'string' },
|
|
@@ -274,6 +297,11 @@ function tagSuffix(record) {
|
|
|
274
297
|
function detailOf(n) {
|
|
275
298
|
return n.snippet && n.snippet !== n.lead ? n.snippet : (n.lead ?? '');
|
|
276
299
|
}
|
|
300
|
+
/** 死胡同提醒(措辞与 `zk-kit search` 共用一份);没有就一个字都不加 */
|
|
301
|
+
function counterBlockOf(entries, truncated, zeroHit) {
|
|
302
|
+
const body = describeCounterEntries({ entries: entries ?? [], truncated: truncated === true }, { zeroHit });
|
|
303
|
+
return body ? '\n\n' + body : '';
|
|
304
|
+
}
|
|
277
305
|
function noteLine(n, index) {
|
|
278
306
|
const head = `${index + 1}. ${n.title || n.path}`;
|
|
279
307
|
const meta = [n.absPath || n.path, dateOnly(n.created)].filter(Boolean).join(' · ');
|
|
@@ -378,7 +406,9 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
|
|
|
378
406
|
description: '在 zk 知识库里搜索笔记(全文 / 标签 / 日期 / 断链 / 孤立笔记),返回标题、相对路径、绝对路径、标签、创建日、摘要与命中片段。' +
|
|
379
407
|
'找到后用原生 read 读全文:notes[].absPath 可以直接传给 read。写笔记前先用它查重。' +
|
|
380
408
|
'中文会自动并用子串,多词按覆盖度合并(FTS 不切中文):matchStrategy 说明这次用了 fts / regex / mixed,mixed 表示补到了全文索引漏掉的笔记。' +
|
|
381
|
-
'有 query 时默认按 FTS 相关性排序,显式给 sort 才改顺序。'
|
|
409
|
+
'有 query 时默认按 FTS 相关性排序,显式给 sort 才改顺序。' +
|
|
410
|
+
'结果还带 entryPoints(MEMORY 的活条目:via=query 表示这条结论跟查询有关,来源可以不在本次结果里)' +
|
|
411
|
+
'与 counterEntries(归档里同名旧结论 + 废弃原因,先看它别把废掉的结论重新提出来)。',
|
|
382
412
|
parameters: {
|
|
383
413
|
type: 'object',
|
|
384
414
|
properties: {
|
|
@@ -417,13 +447,21 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
|
|
|
417
447
|
snippet: { type: 'string' },
|
|
418
448
|
}, ['title', 'path', 'absPath', 'tags', 'created', 'lead']),
|
|
419
449
|
},
|
|
450
|
+
/** R9:只在有命中且 MEMORY 里有对应活条目时出现 */
|
|
451
|
+
entryPoints: { type: 'array', items: ENTRY_POINT },
|
|
452
|
+
/** 入口超过上限时出现(true),不静默少列 */
|
|
453
|
+
entryPointsTruncated: { type: 'boolean' },
|
|
454
|
+
/** 死胡同探针:归档里同名的废弃结论(零子进程;只提示,不改结果集) */
|
|
455
|
+
counterEntries: { type: 'array', items: COUNTER_ENTRY },
|
|
456
|
+
counterEntriesTruncated: { type: 'boolean' },
|
|
420
457
|
}, ['notebook', 'count', 'matchStrategy', 'completeness', 'hasMore', 'scanned', 'notes']),
|
|
421
458
|
},
|
|
422
459
|
async run(args, env) {
|
|
423
460
|
const cfg = configFor(env.workspace, env.sessionId);
|
|
424
461
|
await ensureNotebook(cfg);
|
|
462
|
+
const query = args.query ? String(args.query) : undefined;
|
|
425
463
|
const result = await searchNotesDetailed(cfg, {
|
|
426
|
-
query
|
|
464
|
+
query,
|
|
427
465
|
tags: Array.isArray(args.tags) ? args.tags.map(String) : undefined,
|
|
428
466
|
since: args.since ? String(args.since) : undefined,
|
|
429
467
|
until: args.until ? String(args.until) : undefined,
|
|
@@ -433,6 +471,14 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
|
|
|
433
471
|
// R1:有 query 且没显式给 sort → relevance(不传 --sort,保住 FTS 相关性顺序)
|
|
434
472
|
sort: args.sort ? String(args.sort) : (args.query ? 'relevance' : 'created-desc'),
|
|
435
473
|
});
|
|
474
|
+
// R9:查询命中的 MEMORY 条目 → 入口(来源可以不在本批结果里);先只读,不新增 zk 进程
|
|
475
|
+
const { points: entryPoints, truncated: entryPointsTruncated } = entryPointsOf(cfg, result.notes, { query });
|
|
476
|
+
const counters = archivedCounterparts(cfg, result.notes, { query });
|
|
477
|
+
bumpUsage(cfg, [
|
|
478
|
+
'search',
|
|
479
|
+
...(entryPoints.length ? ['entryPoints'] : []),
|
|
480
|
+
...(counters.entries.length ? ['counterEntries'] : []),
|
|
481
|
+
]);
|
|
436
482
|
return {
|
|
437
483
|
notebook: cfg.notebook,
|
|
438
484
|
count: result.notes.length,
|
|
@@ -446,14 +492,19 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
|
|
|
446
492
|
created: dateOnly(n.created), lead: n.lead, words: n.words,
|
|
447
493
|
...(n.snippet ? { snippet: n.snippet } : {}),
|
|
448
494
|
})),
|
|
495
|
+
...(entryPoints.length ? { entryPoints } : {}),
|
|
496
|
+
...(entryPointsTruncated ? { entryPointsTruncated } : {}),
|
|
497
|
+
...(counters.entries.length ? { counterEntries: counters.entries } : {}),
|
|
498
|
+
...(counters.truncated ? { counterEntriesTruncated: true } : {}),
|
|
449
499
|
};
|
|
450
500
|
},
|
|
451
501
|
render(args, value) {
|
|
452
502
|
const v = value;
|
|
503
|
+
const counters = counterBlockOf(v.counterEntries, v.counterEntriesTruncated, v.count === 0);
|
|
453
504
|
if (!v.count) {
|
|
454
505
|
return text(`没有匹配的笔记(笔记本 ${v.notebook})` +
|
|
455
506
|
zeroHitHint(args, '或按 tags 查') +
|
|
456
|
-
(v.completeness === 'unknown' ? ';但这次检索结果无法完整解析,不能当作「库里没有」' : ''));
|
|
507
|
+
(v.completeness === 'unknown' ? ';但这次检索结果无法完整解析,不能当作「库里没有」' : '') + entryPointLines(v.entryPoints, { truncated: v.entryPointsTruncated }) + counters);
|
|
457
508
|
}
|
|
458
509
|
const how = v.matchStrategy === 'mixed'
|
|
459
510
|
? '(中文:全文 + 子串合并,已补上全文索引漏掉的)'
|
|
@@ -461,7 +512,8 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
|
|
|
461
512
|
const tail = v.completeness === 'unknown'
|
|
462
513
|
? '\n⚠️ 无法确认结果是否完整(解析失败 / 补充检索失败),先别把它当成全部命中'
|
|
463
514
|
: v.hasMore ? `\n⚠️ 还有更多匹配没返回(已检查 ${v.scanned} 条候选);要更全就提高 limit 或加过滤条件` : '';
|
|
464
|
-
return text(`${v.count} 条(笔记本 ${v.notebook})${how}\n` + v.notes.map(noteLine).join('\n') +
|
|
515
|
+
return text(`${v.count} 条(笔记本 ${v.notebook})${how}\n` + v.notes.map(noteLine).join('\n') +
|
|
516
|
+
entryPointLines(v.entryPoints, { truncated: v.entryPointsTruncated }) + tail + counters);
|
|
465
517
|
},
|
|
466
518
|
}),
|
|
467
519
|
t({
|
|
@@ -469,7 +521,7 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
|
|
|
469
521
|
description: '结构化检索记忆:按 type(daily/source/concept/entity/pattern)、status(active/superseded)、标签、目录、日期、断链、孤立过滤。' +
|
|
470
522
|
'默认只看 active 且不含归档;传 include_archived: true 会连归档一起查,此时未显式给 status 就不再默认过滤 active。' +
|
|
471
523
|
'中文会自动并用子串,多词按覆盖度合并;有 query 时默认按 FTS 相关性排序。' +
|
|
472
|
-
'这是查「记忆里有什么」的主入口。',
|
|
524
|
+
'这是查「记忆里有什么」的主入口。entryPoints / counterEntries 与 zk_search 同口径。',
|
|
473
525
|
parameters: {
|
|
474
526
|
type: 'object',
|
|
475
527
|
properties: {
|
|
@@ -514,14 +566,22 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
|
|
|
514
566
|
archived: { type: 'boolean' },
|
|
515
567
|
}, ['title', 'path', 'absPath', 'type', 'status', 'authority', 'tags', 'created', 'lead', 'archived']),
|
|
516
568
|
},
|
|
569
|
+
/** R9:只在有命中且 MEMORY 里有对应活条目时出现 */
|
|
570
|
+
entryPoints: { type: 'array', items: ENTRY_POINT },
|
|
571
|
+
/** 入口超过上限时出现(true),不静默少列 */
|
|
572
|
+
entryPointsTruncated: { type: 'boolean' },
|
|
573
|
+
/** 死胡同探针:归档里同名的废弃结论(include_archived 时不重复报) */
|
|
574
|
+
counterEntries: { type: 'array', items: COUNTER_ENTRY },
|
|
575
|
+
counterEntriesTruncated: { type: 'boolean' },
|
|
517
576
|
}, ['notebook', 'count', 'completeness', 'hasMore', 'scanned', 'notes']),
|
|
518
577
|
},
|
|
519
578
|
async run(args, env) {
|
|
520
579
|
const cfg = configFor(env.workspace, env.sessionId);
|
|
521
580
|
await ensureNotebook(cfg, env.signal);
|
|
522
581
|
const includeArchived = args.include_archived === true;
|
|
582
|
+
const query = args.query ? String(args.query) : undefined;
|
|
523
583
|
const result = await searchNotesDetailed(cfg, {
|
|
524
|
-
query
|
|
584
|
+
query,
|
|
525
585
|
tags: Array.isArray(args.tags) ? args.tags.map(String) : undefined,
|
|
526
586
|
paths: Array.isArray(args.dirs) ? args.dirs.map(String) : undefined,
|
|
527
587
|
type: args.type ? [String(args.type)] : undefined,
|
|
@@ -537,6 +597,16 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
|
|
|
537
597
|
sort: args.sort ? String(args.sort) : (args.query ? 'relevance' : 'created-desc'),
|
|
538
598
|
}, env.signal);
|
|
539
599
|
const notes = result.notes;
|
|
600
|
+
// R9:与 zk_search 共用同一份计算(查询命中的来源可以不在本批结果里;零子进程)
|
|
601
|
+
const { points: entryPoints, truncated: entryPointsTruncated } = entryPointsOf(cfg, notes, { query });
|
|
602
|
+
const counters = includeArchived
|
|
603
|
+
? { entries: [], scanned: 0, truncated: false }
|
|
604
|
+
: archivedCounterparts(cfg, notes, { query });
|
|
605
|
+
bumpUsage(cfg, [
|
|
606
|
+
'query',
|
|
607
|
+
...(entryPoints.length ? ['entryPoints'] : []),
|
|
608
|
+
...(counters.entries.length ? ['counterEntries'] : []),
|
|
609
|
+
]);
|
|
540
610
|
return {
|
|
541
611
|
notebook: cfg.notebook,
|
|
542
612
|
count: notes.length,
|
|
@@ -559,14 +629,19 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
|
|
|
559
629
|
...(n.snippet ? { snippet: n.snippet } : {}),
|
|
560
630
|
archived: n.path.startsWith(cfg.archiveDir + '/'),
|
|
561
631
|
})),
|
|
632
|
+
...(entryPoints.length ? { entryPoints } : {}),
|
|
633
|
+
...(entryPointsTruncated ? { entryPointsTruncated } : {}),
|
|
634
|
+
...(counters.entries.length ? { counterEntries: counters.entries } : {}),
|
|
635
|
+
...(counters.truncated ? { counterEntriesTruncated: true } : {}),
|
|
562
636
|
};
|
|
563
637
|
},
|
|
564
638
|
render(args, value) {
|
|
565
639
|
const v = value;
|
|
640
|
+
const counters = counterBlockOf(v.counterEntries, v.counterEntriesTruncated, v.count === 0);
|
|
566
641
|
if (!v.count) {
|
|
567
642
|
return text(`没有匹配的笔记(笔记本 ${v.notebook})` +
|
|
568
643
|
zeroHitHint(args, '或给 tags 参数按标签查') +
|
|
569
|
-
(v.completeness === 'unknown' ? ';但这次检索结果无法完整解析,不能当作「记忆里没有」' : ''));
|
|
644
|
+
(v.completeness === 'unknown' ? ';但这次检索结果无法完整解析,不能当作「记忆里没有」' : '') + entryPointLines(v.entryPoints, { truncated: v.entryPointsTruncated }) + counters);
|
|
570
645
|
}
|
|
571
646
|
const tail = v.completeness === 'unknown'
|
|
572
647
|
? '\n⚠️ 无法确认结果是否完整(解析失败 / 补充检索失败),先别把它当成全部命中'
|
|
@@ -576,7 +651,8 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
|
|
|
576
651
|
const detail = detailOf({ lead: String(n.lead ?? ''), snippet: String(n.snippet ?? '') });
|
|
577
652
|
return `${i + 1}. ${String(n.title)} [${badges.join(' · ')}]\n ${String(n.absPath || n.path)} · ${String(n.created)}${detail ? '\n ' + detail : ''}`;
|
|
578
653
|
});
|
|
579
|
-
return text(`${v.count} 条(笔记本 ${v.notebook})\n` + lines.join('\n') +
|
|
654
|
+
return text(`${v.count} 条(笔记本 ${v.notebook})\n` + lines.join('\n') +
|
|
655
|
+
entryPointLines(v.entryPoints, { truncated: v.entryPointsTruncated }) + tail + counters);
|
|
580
656
|
},
|
|
581
657
|
}),
|
|
582
658
|
t({
|
|
@@ -621,7 +697,17 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
|
|
|
621
697
|
reason: { type: 'string', enum: ['included', 'duplicate', 'empty', 'over-budget'] },
|
|
622
698
|
}, ['title', 'path', 'absPath', 'included', 'reason']),
|
|
623
699
|
},
|
|
624
|
-
|
|
700
|
+
/** R9:与 zk_search / zk_query 共用同一份计算(没有就是空数组,不另算) */
|
|
701
|
+
entryPoints: { type: 'array', items: ENTRY_POINT },
|
|
702
|
+
/** 入口超过上限(true),不静默少列 */
|
|
703
|
+
entryPointsTruncated: { type: 'boolean' },
|
|
704
|
+
/** true = via=query 的入口已写进 text(占同一份预算);false 时正文里没有它们 */
|
|
705
|
+
entryPointsInText: { type: 'boolean' },
|
|
706
|
+
/** 死胡同探针:归档里同名的废弃结论 */
|
|
707
|
+
counterEntries: { type: 'array', items: COUNTER_ENTRY },
|
|
708
|
+
counterEntriesTruncated: { type: 'boolean' },
|
|
709
|
+
}, ['notebook', 'text', 'used', 'budget', 'completeness', 'hasMore', 'notes',
|
|
710
|
+
'entryPoints', 'entryPointsTruncated', 'counterEntries', 'counterEntriesTruncated']),
|
|
625
711
|
},
|
|
626
712
|
async run(args, env) {
|
|
627
713
|
const cfg = configFor(env.workspace, env.sessionId);
|
|
@@ -648,24 +734,128 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
|
|
|
648
734
|
const v = value;
|
|
649
735
|
const kept = v.notes.filter((n) => n.included).length;
|
|
650
736
|
const dropped = v.notes.length - kept;
|
|
737
|
+
const counters = counterBlockOf(v.counterEntries, v.counterEntriesTruncated, v.notes.length === 0);
|
|
651
738
|
if (!v.notes.length) {
|
|
652
739
|
return text(`没有匹配的笔记(笔记本 ${v.notebook})` +
|
|
653
740
|
zeroHitHint(args, '或按 tags 查') +
|
|
654
|
-
(v.completeness === 'unknown' ? ';但这次检索结果无法完整解析,不能当作「库里没有」' : ''));
|
|
741
|
+
(v.completeness === 'unknown' ? ';但这次检索结果无法完整解析,不能当作「库里没有」' : '') + entryPointLines(v.entryPoints, { truncated: v.entryPointsTruncated }) + counters);
|
|
655
742
|
}
|
|
656
743
|
const lines = [`召回装配:${v.used}/${v.budget} 词 · 收录 ${kept}/${v.notes.length} 篇(笔记本 ${v.notebook})`];
|
|
657
744
|
if (v.text)
|
|
658
745
|
lines.push('', v.text);
|
|
659
746
|
if (dropped)
|
|
660
747
|
lines.push(`(${dropped} 篇没放进去:整条丢弃,没有切半句;原因见 notes[].reason)`);
|
|
748
|
+
// `via: 'query'` 的入口已经写进 text(排在装配正文之前);只有预算放不下时才会在这里补回。
|
|
749
|
+
// 状态取自 assembleRecall 的 `entryPointsInText`,不从 text 里嗅字符串 —— 笔记正文也可能含那串字。
|
|
750
|
+
const points = entryPointLines(v.entryPoints, {
|
|
751
|
+
truncated: v.entryPointsTruncated,
|
|
752
|
+
skipQuery: v.entryPointsInText,
|
|
753
|
+
});
|
|
754
|
+
if (points)
|
|
755
|
+
lines.push(points.trimStart());
|
|
661
756
|
const tail = v.completeness === 'unknown'
|
|
662
757
|
? '⚠️ 无法确认结果是否完整(解析失败 / 补充检索失败),先别把它当成全部命中'
|
|
663
758
|
: v.hasMore ? '⚠️ 还有更多匹配没返回;要提高 limit 或缩小过滤范围' : '';
|
|
664
759
|
if (tail)
|
|
665
760
|
lines.push(tail);
|
|
761
|
+
if (counters)
|
|
762
|
+
lines.push(counters.trimStart());
|
|
666
763
|
return text(lines.join('\n'));
|
|
667
764
|
},
|
|
668
765
|
}),
|
|
766
|
+
t({
|
|
767
|
+
name: 'zk_walk',
|
|
768
|
+
description: '从一篇笔记出发走一跳邻居(只读):direction=both 同时看反链与出链,in=谁链到它,out=它链到谁。' +
|
|
769
|
+
'种子写相对笔记本的路径、绝对路径,或精确标题 / 稳定 id([[标题]] / [[id]] 也认);不精确命中就返回空,不会退回全库。' +
|
|
770
|
+
'按篇反查、不建全图,成本与库规模基本无关;limit 硬顶 200,触顶 completeness=partial(邻居没列全,不代表没有)。' +
|
|
771
|
+
'每条邻居带 section / linkLine:那条边所在的小节与那一行 —— 没有它,同一篇种子的出链只有一串标题。' +
|
|
772
|
+
'只回标题 / 路径 / absPath / 摘要 / 那一行,全文仍用原生 read 读 absPath;不改标题、不自动 link、不写任何文件。',
|
|
773
|
+
parameters: {
|
|
774
|
+
type: 'object',
|
|
775
|
+
properties: {
|
|
776
|
+
path: S.str('种子笔记:相对笔记本的路径、绝对路径,或精确标题 / 稳定 id(也认 [[标题]] / [[id]])'),
|
|
777
|
+
direction: S.enum('走哪个方向:both 反链 + 出链,in 只看谁链到它,out 只看它链到谁(默认 both)', ['both', 'in', 'out']),
|
|
778
|
+
limit: S.num('每个方向最多几条(1-200,默认 10)'),
|
|
779
|
+
include_archived: S.bool('连 archive/ 里的废弃笔记一起看(默认 false)'),
|
|
780
|
+
},
|
|
781
|
+
required: ['path'],
|
|
782
|
+
additionalProperties: false,
|
|
783
|
+
},
|
|
784
|
+
output: {
|
|
785
|
+
schema: objSchema({
|
|
786
|
+
notebook: { type: 'string' },
|
|
787
|
+
/** 种子解析失败时整个键不出现(null 过不了 object 校验,不拿空对象冒充) */
|
|
788
|
+
seed: objSchema({ title: { type: 'string' }, path: { type: 'string' }, absPath: { type: 'string' } }, ['title', 'path', 'absPath']),
|
|
789
|
+
direction: { type: 'string', enum: ['both', 'in', 'out'] },
|
|
790
|
+
count: { type: 'number' },
|
|
791
|
+
truncated: { type: 'boolean' },
|
|
792
|
+
/** 只有 complete / partial:按篇反查没有产生 unknown 的路径 */
|
|
793
|
+
completeness: { type: 'string', enum: ['complete', 'partial'] },
|
|
794
|
+
notes: {
|
|
795
|
+
type: 'array',
|
|
796
|
+
items: objSchema({
|
|
797
|
+
title: { type: 'string' },
|
|
798
|
+
path: { type: 'string' },
|
|
799
|
+
absPath: { type: 'string' },
|
|
800
|
+
relation: { type: 'string', enum: ['in', 'out'] },
|
|
801
|
+
lead: { type: 'string' },
|
|
802
|
+
/** 这条边所在的 `##` / `###` 小节;找不到时键不出现(plan §14.1) */
|
|
803
|
+
section: { type: 'string' },
|
|
804
|
+
/** 这条边的那一行(压空白、截 160 码点):没有它,同一篇种子的出链长得一模一样 */
|
|
805
|
+
linkLine: { type: 'string' },
|
|
806
|
+
}, ['title', 'path', 'absPath', 'relation', 'lead']),
|
|
807
|
+
},
|
|
808
|
+
}, ['notebook', 'direction', 'count', 'truncated', 'completeness', 'notes']),
|
|
809
|
+
},
|
|
810
|
+
async run(args, env) {
|
|
811
|
+
const cfg = configFor(env.workspace, env.sessionId);
|
|
812
|
+
await ensureNotebook(cfg, env.signal);
|
|
813
|
+
const seed = args.path === undefined || args.path === null ? '' : String(args.path);
|
|
814
|
+
if (!seed.trim())
|
|
815
|
+
throw new ZkError('种子不能为空', '给 path:相对笔记本的路径,或精确标题 / id');
|
|
816
|
+
const direction = (args.direction === undefined ? 'both' : String(args.direction));
|
|
817
|
+
const r = await walkFrom(cfg, {
|
|
818
|
+
seed,
|
|
819
|
+
direction,
|
|
820
|
+
limit: args.limit === undefined ? 10 : Number(args.limit),
|
|
821
|
+
includeArchived: args.include_archived === true,
|
|
822
|
+
}, env.signal);
|
|
823
|
+
bumpUsage(cfg, ['walk']);
|
|
824
|
+
return {
|
|
825
|
+
notebook: cfg.notebook,
|
|
826
|
+
...(r.seed ? { seed: r.seed } : {}),
|
|
827
|
+
direction,
|
|
828
|
+
count: r.notes.length,
|
|
829
|
+
truncated: r.truncated,
|
|
830
|
+
completeness: r.completeness,
|
|
831
|
+
notes: r.notes,
|
|
832
|
+
};
|
|
833
|
+
},
|
|
834
|
+
render(args, value) {
|
|
835
|
+
const v = value;
|
|
836
|
+
if (!v.seed) {
|
|
837
|
+
return text(`没找到种子笔记:${String(args.path ?? '')}\n` +
|
|
838
|
+
'要写相对 / 绝对路径,或精确标题 / 稳定 id;不精确匹配时不猜(不会退回全库)');
|
|
839
|
+
}
|
|
840
|
+
const label = v.seed.title && v.seed.title !== v.seed.path ? `${v.seed.title} · ${v.seed.path}` : v.seed.path;
|
|
841
|
+
if (!v.count) {
|
|
842
|
+
return text(`「${label}」没有 ${v.direction === 'both' ? '入链或出链' : v.direction === 'in' ? '入链' : '出链'}邻居(笔记本 ${v.notebook})`);
|
|
843
|
+
}
|
|
844
|
+
const lines = v.notes.map((n, i) => {
|
|
845
|
+
const arrow = n.relation === 'in' ? '← 链向本篇' : '→ 本篇链到';
|
|
846
|
+
const detail = detailOf({ lead: n.lead });
|
|
847
|
+
// 先给「为什么这条边存在」,再给摘要:同一篇种子的出链摘要往往逐字相同
|
|
848
|
+
const why = n.linkLine || n.section
|
|
849
|
+
? `\n ${n.section ? `〔${n.section}〕` : '〔无小节〕'} ${n.linkLine ?? ''}`.trimEnd()
|
|
850
|
+
: '';
|
|
851
|
+
return `${i + 1}. ${arrow} ${n.title || n.path}${why}\n ${n.absPath || n.path}${detail ? '\n ' + detail : ''}`;
|
|
852
|
+
});
|
|
853
|
+
const tail = v.truncated
|
|
854
|
+
? '\n⚠️ 邻居没列全(触到 limit);这不代表只有这些,要提高 limit 再走一次'
|
|
855
|
+
: '';
|
|
856
|
+
return text(`${v.count} 篇 1 跳邻居(种子 ${label} · 笔记本 ${v.notebook})\n` + lines.join('\n') + tail);
|
|
857
|
+
},
|
|
858
|
+
}),
|
|
669
859
|
t({
|
|
670
860
|
name: 'zk_new',
|
|
671
861
|
description: '新建一篇笔记(zk 负责生成文件名、套用模板、写入索引)。适合记下一个概念、决定、来源或想法;' +
|
|
@@ -1565,18 +1755,39 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
|
|
|
1565
1755
|
},
|
|
1566
1756
|
completeness: { type: 'string', enum: ['complete', 'partial', 'unknown'] },
|
|
1567
1757
|
scanLimit: { type: 'number' },
|
|
1568
|
-
|
|
1758
|
+
/** 本地聚合计数(plan §14.4):只看「入口 / 一跳」这条路有没有被用上,不记查询词 */
|
|
1759
|
+
usage: objSchema({
|
|
1760
|
+
since: { type: 'string' },
|
|
1761
|
+
search: { type: 'number' },
|
|
1762
|
+
query: { type: 'number' },
|
|
1763
|
+
recall: { type: 'number' },
|
|
1764
|
+
walk: { type: 'number' },
|
|
1765
|
+
entryPoints: { type: 'number' },
|
|
1766
|
+
counterEntries: { type: 'number' },
|
|
1767
|
+
}, ['since', 'search', 'query', 'recall', 'walk', 'entryPoints', 'counterEntries']),
|
|
1768
|
+
}, ['notebook', 'defaultNotebook', 'pinned', 'notes', 'tags', 'brokenLinks', 'orphans', 'archived', 'superseded', 'byDir', 'byTag', 'completeness', 'scanLimit', 'usage']),
|
|
1569
1769
|
},
|
|
1570
1770
|
async run(_args, env) {
|
|
1571
1771
|
const cfg = configFor(env.workspace, env.sessionId);
|
|
1572
1772
|
await ensureNotebook(cfg, env.signal);
|
|
1573
1773
|
const stats = await notebookStats(cfg, env.signal);
|
|
1574
1774
|
const state = sessionState(env.workspace, env.sessionId);
|
|
1775
|
+
const usage = readUsage(cfg);
|
|
1776
|
+
const count = (key) => usage.counts[key] ?? 0;
|
|
1575
1777
|
return {
|
|
1576
1778
|
notebook: cfg.notebook,
|
|
1577
1779
|
defaultNotebook: state.defaultName,
|
|
1578
1780
|
pinned: state.diverged ? state.pinned : '',
|
|
1579
1781
|
...stats,
|
|
1782
|
+
usage: {
|
|
1783
|
+
since: usage.since,
|
|
1784
|
+
search: count('search'),
|
|
1785
|
+
query: count('query'),
|
|
1786
|
+
recall: count('recall'),
|
|
1787
|
+
walk: count('walk'),
|
|
1788
|
+
entryPoints: count('entryPoints'),
|
|
1789
|
+
counterEntries: count('counterEntries'),
|
|
1790
|
+
},
|
|
1580
1791
|
};
|
|
1581
1792
|
},
|
|
1582
1793
|
render(_args, value) {
|
|
@@ -1594,9 +1805,13 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
|
|
|
1594
1805
|
const partial = v.completeness === 'partial'
|
|
1595
1806
|
? `\n⚠️ 只扫描了前 ${v.scanLimit} 篇:上面的数字是下限`
|
|
1596
1807
|
: v.completeness === 'unknown' ? '\n⚠️ 扫描结果无法完整解析:上面的数字可能偏少' : '';
|
|
1808
|
+
const usage = v.usage && (v.usage.walk || v.usage.entryPoints || v.usage.counterEntries || v.usage.recall)
|
|
1809
|
+
? `\n入口 / 一跳:walk ${v.usage.walk} 次 · 召回 ${v.usage.recall} 次 · 入口回显 ${v.usage.entryPoints} 次 · 死胡同提醒 ${v.usage.counterEntries} 次` +
|
|
1810
|
+
(v.usage.since ? `(自 ${v.usage.since.slice(0, 10)})` : '')
|
|
1811
|
+
: '\n入口 / 一跳:还没有计数(zk_walk / zk_recall / 入口与死胡同提醒都没被用过)';
|
|
1597
1812
|
return text(`笔记本 ${v.notebook}${drift}\n` +
|
|
1598
1813
|
`笔记 ${atLeast}${v.notes} 篇 · 标签 ${atLeast}${v.tags} 个 · 断链 ${v.brokenLinks} 篇 · 孤立 ${v.orphans} 篇 · 归档 ${atLeast}${v.archived} 篇(superseded ${atLeast}${v.superseded})` +
|
|
1599
|
-
dirs + tags + partial);
|
|
1814
|
+
dirs + tags + partial + usage);
|
|
1600
1815
|
},
|
|
1601
1816
|
}),
|
|
1602
1817
|
t({
|