dsh-zdiary-kit 0.0.0-stage → 0.6.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/LICENSE +21 -0
- package/README.md +345 -2
- package/cordis.patch.yml +18 -0
- package/dist/app/plugin.js +274 -0
- package/dist/cli.js +501 -0
- package/dist/config/paths.js +57 -0
- package/dist/config/session.js +108 -0
- package/dist/config/store.js +267 -0
- package/dist/features/settings/client.js +349 -0
- package/dist/features/settings/feature.js +310 -0
- package/dist/features/zk-tools/feature.js +1112 -0
- package/dist/lib/agents-template.js +79 -0
- package/dist/lib/dream.js +211 -0
- package/dist/lib/dsh.js +1 -0
- package/dist/lib/exec.js +66 -0
- package/dist/lib/file-stamp.js +17 -0
- package/dist/lib/frontmatter.js +74 -0
- package/dist/lib/http.js +20 -0
- package/dist/lib/install.js +69 -0
- package/dist/lib/json-file.js +19 -0
- package/dist/lib/lint.js +281 -0
- package/dist/lib/lock.js +80 -0
- package/dist/lib/memory.js +288 -0
- package/dist/lib/meta.js +16 -0
- package/dist/lib/notebook.js +227 -0
- package/dist/lib/platform.js +20 -0
- package/dist/lib/tool.js +70 -0
- package/dist/lib/workspace.js +22 -0
- package/dist/lib/zk.js +1235 -0
- package/docs/features/memory.md +67 -0
- package/docs/features/provisioning.md +48 -0
- package/docs/features/settings.md +63 -0
- package/docs/features/zk-tools.md +104 -0
- package/icon.svg +24 -0
- package/package.json +64 -4
package/dist/cli.js
ADDED
|
@@ -0,0 +1,501 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* 人也能用的命令行(和 agent 的 zk_* 工具走同一套 lib,不重复实现)。
|
|
4
|
+
*/
|
|
5
|
+
import fs from 'node:fs';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
import { config, writeConfig, selectNotebook, isNotebookName, CONFIG_KEYS } from './config/store.js';
|
|
8
|
+
import { CFG_FILE } from './config/paths.js';
|
|
9
|
+
import { ensureNotebook, structureStatus } from './lib/notebook.js';
|
|
10
|
+
import { buildMemoryPayload, formatFileShares } from './lib/memory.js';
|
|
11
|
+
import { installZk, sourceLabel } from './lib/install.js';
|
|
12
|
+
import { resolveZk, searchNotes, queryNotes, createNote, readNote, findNote, appendToNote, ensureDailyNote, appendDaily, linkNotes, notebookStats, indexNotebook, withTags, hasNotebook, localDate, ZkError, allNotes, parseErrorWarnings, } from './lib/zk.js';
|
|
13
|
+
import { formatMemoryLinks, lintNotebook } from './lib/lint.js';
|
|
14
|
+
import { dreamNotebook } from './lib/dream.js';
|
|
15
|
+
const HELP = `zk-kit —— dsh-zdiary-kit 的命令行
|
|
16
|
+
|
|
17
|
+
zk-kit doctor 自检:zk / 笔记本 / 模板 / group / 索引
|
|
18
|
+
zk-kit install [版本] 安装内置 zk(默认取配置里的版本)
|
|
19
|
+
zk-kit init 建好(或补齐)笔记本
|
|
20
|
+
zk-kit new --title T [--name 文件名] [--body B] [--tags a,b] [--dir notes]
|
|
21
|
+
zk-kit search [词] [--tags a,b] [--since 日期] [--limit 5] [--json]
|
|
22
|
+
zk-kit read <路径> | --query 词 [--max-chars n] [--json]
|
|
23
|
+
zk-kit daily [--entry 一段] [--date yesterday]
|
|
24
|
+
zk-kit append <路径> --text 一段 [--heading 标题]
|
|
25
|
+
zk-kit link <来源路径> <目标路径或标题>
|
|
26
|
+
zk-kit lint [--stale-days 90] 体检:断链 / 孤儿 / 无标签 / 单向链接 / 陈旧 / 重名(只读)
|
|
27
|
+
zk-kit dream [--days 7] 晋升材料:窗口新增 / daily 未完成 / 被引最多(只读)
|
|
28
|
+
zk-kit query [-- <zk list 参数>] 透传 zk list(--notebook-dir / --format / -x 会被替换掉)
|
|
29
|
+
zk-kit index 重建索引
|
|
30
|
+
zk-kit notebooks 列出所有笔记本(* 是当前那个;[flat/layered] 是各自的结构)
|
|
31
|
+
zk-kit notebooks add 名 路径 加一个笔记本(路径空 = <工作区>/zdiary;结构跟当前笔记本一致)
|
|
32
|
+
zk-kit notebooks use 名 切换默认笔记本(新会话生效;自动建好目标笔记本,幂等)
|
|
33
|
+
zk-kit notebooks remove 名 从配置移除一个笔记本(只删配置,磁盘目录与笔记保留;至少留一个;删的是当前那个就自动切到剩下的第一个)
|
|
34
|
+
zk-kit config [get [键]] 看配置(不传键 = 全量;未知键报错)
|
|
35
|
+
zk-kit config set 键=值 [键=值 …] 改配置(只落盘,不建目录;structure 改的是当前笔记本,切完跑 zk-kit init)
|
|
36
|
+
|
|
37
|
+
除 doctor / help 外,其余子命令加 --json 输出结构化结果。`;
|
|
38
|
+
function parse(argv) {
|
|
39
|
+
const out = { pos: [], flags: {} };
|
|
40
|
+
for (let i = 0; i < argv.length; i++) {
|
|
41
|
+
const a = argv[i];
|
|
42
|
+
if (a === '--') {
|
|
43
|
+
out.pos.push(...argv.slice(i + 1));
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
if (!a.startsWith('--')) {
|
|
47
|
+
out.pos.push(a);
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
const eq = a.indexOf('=');
|
|
51
|
+
if (eq > 0) {
|
|
52
|
+
out.flags[a.slice(2, eq)] = a.slice(eq + 1);
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
const key = a.slice(2);
|
|
56
|
+
const next = argv[i + 1];
|
|
57
|
+
if (next !== undefined && !next.startsWith('--')) {
|
|
58
|
+
out.flags[key] = next;
|
|
59
|
+
i++;
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
out.flags[key] = true;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return out;
|
|
66
|
+
}
|
|
67
|
+
const str = (v) => (v === true || v === undefined ? '' : String(v));
|
|
68
|
+
const list = (v) => str(v).split(/[,,]/).map((s) => s.trim()).filter(Boolean);
|
|
69
|
+
const print = (value, asJson) => {
|
|
70
|
+
console.log(asJson || typeof value !== 'string' ? JSON.stringify(value, null, 2) : value);
|
|
71
|
+
};
|
|
72
|
+
/** 配置值给人看:数组摊平、笔记本表摊成 名=路径 [结构],其它照原样(`print` 会把非字符串 JSON 化) */
|
|
73
|
+
const fmtValue = (v) => {
|
|
74
|
+
if (Array.isArray(v))
|
|
75
|
+
return v.length ? v.join(', ') : '[]';
|
|
76
|
+
if (v && typeof v === 'object') {
|
|
77
|
+
return Object.entries(v)
|
|
78
|
+
.map(([k, val]) => {
|
|
79
|
+
const entry = val;
|
|
80
|
+
if (entry && typeof entry === 'object' && 'path' in entry) {
|
|
81
|
+
return `${k}=${String(entry.path ?? '') || '(默认)'} [${String(entry.structure ?? '')}]`;
|
|
82
|
+
}
|
|
83
|
+
return `${k}=${String(val ?? '') || '(默认)'}`;
|
|
84
|
+
})
|
|
85
|
+
.join(', ');
|
|
86
|
+
}
|
|
87
|
+
return String(v);
|
|
88
|
+
};
|
|
89
|
+
const KEYS = CONFIG_KEYS;
|
|
90
|
+
const isKey = (k) => KEYS.includes(k);
|
|
91
|
+
const USAGE_CONFIG = `用法:zk-kit config [get [键]] | zk-kit config set 键=值 [键=值 …](键:${KEYS.join(' / ')})`;
|
|
92
|
+
const USAGE_NOTEBOOKS = '用法:zk-kit notebooks [add 名 路径 | use 名 | remove 名]';
|
|
93
|
+
/** 列出笔记本:解析后的路径 + 各自的结构 + 是不是 zk 笔记本(换笔记本就是换 active,子目录那些是全局的) */
|
|
94
|
+
function notebookList(cfg) {
|
|
95
|
+
const names = Object.keys(cfg.notebooks);
|
|
96
|
+
const width = Math.max(7, ...names.map((n) => n.length));
|
|
97
|
+
const lines = [`笔记本 ${names.length} 个(当前 ${cfg.active}):`];
|
|
98
|
+
for (const name of names) {
|
|
99
|
+
const target = selectNotebook(cfg, name);
|
|
100
|
+
const mark = name === cfg.active ? '*' : ' ';
|
|
101
|
+
lines.push(`${mark} ${name.padEnd(width)} ${target.notebook} [${target.structure}]${hasNotebook(target) ? '' : '(不是 zk 笔记本 —— 跑 zk-kit init)'}`);
|
|
102
|
+
}
|
|
103
|
+
return lines.join('\n');
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* 切换后自动建好目标笔记本(ensureNotebook 幂等,缺了才补)。
|
|
107
|
+
* 失败只告警不改退出码 —— 切换本身已经落盘;返回值拼进人看的那行输出。
|
|
108
|
+
*/
|
|
109
|
+
async function autoInit(next) {
|
|
110
|
+
try {
|
|
111
|
+
const r = await ensureNotebook(next);
|
|
112
|
+
return r.initialized ? '(已自动建好)' : '';
|
|
113
|
+
}
|
|
114
|
+
catch (err) {
|
|
115
|
+
console.error(`⚠️ 自动初始化失败:${err instanceof Error ? err.message : String(err)} —— 跑 zk-kit init 重试`);
|
|
116
|
+
return '';
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/** 名字不存在时的报错:顺手把现有的列出来(打错名字不用再跑一次 list) */
|
|
120
|
+
function missingNotebook(name, cfg) {
|
|
121
|
+
return new ZkError(`没有这个笔记本:${name || '(空)'}(现有:${Object.keys(cfg.notebooks).join('、')})`);
|
|
122
|
+
}
|
|
123
|
+
function noteList(notes) {
|
|
124
|
+
if (!notes.length)
|
|
125
|
+
return '没有匹配的笔记';
|
|
126
|
+
const lines = [`${notes.length} 条:`];
|
|
127
|
+
for (const [i, n] of notes.entries()) {
|
|
128
|
+
const tags = n.tags.length ? ' · ' + n.tags.map((t) => '#' + t).join(' ') : '';
|
|
129
|
+
lines.push(`${i + 1}. ${n.title || n.path}\n ${n.path} · ${localDate(n.created)}${tags}${n.lead ? '\n ' + n.lead : ''}`);
|
|
130
|
+
}
|
|
131
|
+
return lines.join('\n');
|
|
132
|
+
}
|
|
133
|
+
function lintReport(r) {
|
|
134
|
+
const sample = (items) => {
|
|
135
|
+
const head = items.slice(0, 5).map((n) => n.path).join('、');
|
|
136
|
+
return items.length > 5 ? head + ' …' : head;
|
|
137
|
+
};
|
|
138
|
+
const row = (label, total, items) => total === 0 ? `✓ ${label}:0` : `✗ ${label}:${total} 篇 — ${sample(items)}`;
|
|
139
|
+
const lines = [`体检 ${r.notebook}:${r.notes} 篇笔记(陈旧阈值 ${r.staleDays} 天)`];
|
|
140
|
+
lines.push(r.parseErrors.total === 0
|
|
141
|
+
? '✓ 解析失败:0'
|
|
142
|
+
: `✗ 解析失败:${r.parseErrors.total} 篇 — ` +
|
|
143
|
+
r.parseErrors.items.slice(0, 5).map((e) => `${e.path}(${e.reason})`).join('、') +
|
|
144
|
+
(r.parseErrors.total > 5 ? ' …' : '') +
|
|
145
|
+
'|这些笔记没进索引:搜不到,也不进别的项;修好 frontmatter 重跑一次');
|
|
146
|
+
lines.push(row('断链', r.brokenLinks.total, r.brokenLinks.items));
|
|
147
|
+
lines.push(row('孤儿', r.orphans.total, r.orphans.items));
|
|
148
|
+
lines.push(row('无标签', r.tagless.total, r.tagless.items));
|
|
149
|
+
lines.push(row('单向链接', r.oneWayLinks.total, r.oneWayLinks.items));
|
|
150
|
+
lines.push(r.stale.total === 0
|
|
151
|
+
? '✓ 陈旧:0'
|
|
152
|
+
: `✗ 陈旧:${r.stale.total} 篇 — ` + r.stale.items.slice(0, 5).map((s) => `${s.path}(${s.modified})`).join('、'));
|
|
153
|
+
lines.push(r.duplicateTitles.total === 0
|
|
154
|
+
? '✓ 重名标题:0'
|
|
155
|
+
: `✗ 重名标题:${r.duplicateTitles.total} 组 — ` + r.duplicateTitles.items.slice(0, 3).map((d) => `「${d.title}」→ ${d.paths.join(' / ')}`).join(';'));
|
|
156
|
+
const memLine = formatMemoryLinks(r.memoryLinks);
|
|
157
|
+
if (memLine)
|
|
158
|
+
lines.push(memLine);
|
|
159
|
+
if (r.truncated)
|
|
160
|
+
lines.push('⚠️ 笔记数触到扫描上限:结果不完整');
|
|
161
|
+
lines.push('(只报告,没有改动任何文件)');
|
|
162
|
+
return lines.join('\n');
|
|
163
|
+
}
|
|
164
|
+
function dreamReport(r) {
|
|
165
|
+
const lines = [
|
|
166
|
+
`做梦:近 ${r.windowDays} 天(自 ${r.since})— 窗口 ${r.counts.window} 篇(日记 ${r.counts.daily})· 候选 ${r.counts.candidates} · 拒绝 ${r.counts.rejected} · 未完成 ${r.counts.looseEnds}`,
|
|
167
|
+
];
|
|
168
|
+
if (r.newNotes.length) {
|
|
169
|
+
lines.push('晋升候选:');
|
|
170
|
+
for (const [i, n] of r.newNotes.slice(0, 10).entries()) {
|
|
171
|
+
const tags = n.tags.length ? ' · ' + n.tags.map((t) => '#' + t).join(' ') : '';
|
|
172
|
+
const lead = n.lead ? `\n ${n.lead}` : '';
|
|
173
|
+
const citedBy = n.citedBy.length ? `\n 被引:${n.citedBy.join('、')}` : '';
|
|
174
|
+
lines.push(` ${i + 1}. ${n.title || n.path} [${n.type || '—'} · ${n.authority || '—'}] ${n.path}${tags} · 被引 ${n.cited}${lead}${citedBy}`);
|
|
175
|
+
}
|
|
176
|
+
if (r.newNotes.length > 10)
|
|
177
|
+
lines.push(` …还有 ${r.newNotes.length - 10} 篇`);
|
|
178
|
+
}
|
|
179
|
+
if (r.rejected.length) {
|
|
180
|
+
lines.push('机械拒绝(不进候选):');
|
|
181
|
+
for (const x of r.rejected.slice(0, 10))
|
|
182
|
+
lines.push(` · ${x.title || x.path}(${x.path}):${x.reason}`);
|
|
183
|
+
if (r.rejected.length > 10)
|
|
184
|
+
lines.push(` …还有 ${r.rejected.length - 10} 篇`);
|
|
185
|
+
}
|
|
186
|
+
if (r.dailyLooseEnds.length) {
|
|
187
|
+
lines.push('未完成(daily):');
|
|
188
|
+
for (const d of r.dailyLooseEnds.slice(0, 10))
|
|
189
|
+
lines.push(` · ${d.path}:${d.items.join(';')}`);
|
|
190
|
+
}
|
|
191
|
+
if (r.topCited.length) {
|
|
192
|
+
lines.push('被引用最多:');
|
|
193
|
+
for (const [i, c] of r.topCited.slice(0, 5).entries())
|
|
194
|
+
lines.push(` ${i + 1}. ${c.title || c.path}(${c.path})· ${c.cited} 次`);
|
|
195
|
+
}
|
|
196
|
+
if (!r.newNotes.length && !r.dailyLooseEnds.length && !r.topCited.length)
|
|
197
|
+
lines.push('这个窗口里没有新材料。');
|
|
198
|
+
if (r.truncated)
|
|
199
|
+
lines.push('⚠️ 笔记数触到扫描上限:窗口与排名都不完整');
|
|
200
|
+
lines.push('(只报告:MEMORY.md / DREAMS.md 由你按 AGENTS.md 写)');
|
|
201
|
+
return lines.join('\n');
|
|
202
|
+
}
|
|
203
|
+
async function doctor() {
|
|
204
|
+
const cfg = config();
|
|
205
|
+
const zk = await resolveZk(cfg);
|
|
206
|
+
const rows = [];
|
|
207
|
+
rows.push(`${zk ? '✓' : '✗'} zk:${zk ? `${zk.version}(${zk.source})${zk.bin}` : '没找到 —— 跑 zk-kit install'}`);
|
|
208
|
+
const exists = hasNotebook(cfg);
|
|
209
|
+
rows.push(`${exists ? '✓' : '✗'} 笔记本:${cfg.notebook}${exists ? '' : '(不是 zk 笔记本 —— 跑 zk-kit init)'}`);
|
|
210
|
+
const template = path.join(cfg.notebook, '.zk', 'templates', 'daily.md');
|
|
211
|
+
rows.push(`${fs.existsSync(template) ? '✓' : '✗'} 日记模板:${template}`);
|
|
212
|
+
let group = false;
|
|
213
|
+
try {
|
|
214
|
+
group = new RegExp(`^\\[group\\."?${cfg.dailyGroup}"?\\]`, 'm').test(fs.readFileSync(path.join(cfg.notebook, '.zk', 'config.toml'), 'utf8'));
|
|
215
|
+
}
|
|
216
|
+
catch {
|
|
217
|
+
group = false;
|
|
218
|
+
}
|
|
219
|
+
rows.push(`${group ? '✓' : '✗'} 日记 group:${cfg.dailyGroup} → ${cfg.dailyDir}/`);
|
|
220
|
+
const st = structureStatus(cfg);
|
|
221
|
+
const structureOk = st.ready && (!st.agents.required || st.agents.present);
|
|
222
|
+
rows.push(`${structureOk ? '✓' : '✗'} 结构:${st.mode} · ` +
|
|
223
|
+
(st.ready ? `${st.dirs.length} 个目录齐全` : `缺 ${st.missing.join('、')}(跑 zk-kit init)`) +
|
|
224
|
+
(st.agents.required ? (st.agents.present ? ' · AGENTS.md 就绪' : ' · 缺 AGENTS.md(跑 zk-kit init)') : ''));
|
|
225
|
+
const payload = buildMemoryPayload(cfg);
|
|
226
|
+
const present = payload.files.filter((f) => f.present);
|
|
227
|
+
rows.push(present.length === 0
|
|
228
|
+
? '✓ 记忆:暂未启用(根目录没有 AGENTS.md / USER.md / MEMORY.md)'
|
|
229
|
+
: `${payload.words <= payload.budget ? '✓' : '✗'} 记忆:${payload.words}/${payload.budget} 词(${formatFileShares(payload.files, payload.tocWords)})` +
|
|
230
|
+
(payload.truncatedWords > 0 ? `,已截断 ${payload.truncatedWords} 词` : ''));
|
|
231
|
+
if (zk && exists) {
|
|
232
|
+
try {
|
|
233
|
+
const s = await notebookStats(cfg);
|
|
234
|
+
rows.push(`✓ 索引:${s.notes} 篇笔记 / ${s.tags} 个标签 / 断链 ${s.brokenLinks} / 孤立 ${s.orphans}`);
|
|
235
|
+
// frontmatter 写坏的笔记不在索引里:zk 只在 stderr 吭一声,不主动收就永远没人知道
|
|
236
|
+
const warnings = [];
|
|
237
|
+
await allNotes(cfg, { warnings });
|
|
238
|
+
const parseErrors = parseErrorWarnings(cfg, warnings);
|
|
239
|
+
if (parseErrors.length > 0) {
|
|
240
|
+
rows.push(`⚠️ 解析失败:${parseErrors.length} 篇 — ` +
|
|
241
|
+
parseErrors.slice(0, 3).map((e) => `${e.path}(${e.reason})`).join('、') +
|
|
242
|
+
'(不在索引里:搜不到,也不进巡检的任何一项)');
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
catch (err) {
|
|
246
|
+
rows.push('✗ 索引:' + (err instanceof Error ? err.message : String(err)));
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
console.log(rows.join('\n'));
|
|
250
|
+
// ⚠️ 是内容问题(笔记写坏了),不是装坏了 —— 别让它把 doctor 的退出码翻掉
|
|
251
|
+
return rows.every((r) => r.startsWith('✓') || r.startsWith('⚠️'));
|
|
252
|
+
}
|
|
253
|
+
async function main() {
|
|
254
|
+
const argv = process.argv.slice(2);
|
|
255
|
+
const cmd = argv[0] ?? 'doctor';
|
|
256
|
+
const raw = argv.slice(1);
|
|
257
|
+
const { pos, flags } = parse(raw);
|
|
258
|
+
const json = flags.json === true;
|
|
259
|
+
const cfg = config();
|
|
260
|
+
switch (cmd) {
|
|
261
|
+
case 'doctor':
|
|
262
|
+
case 'status':
|
|
263
|
+
if (!(await doctor()))
|
|
264
|
+
process.exitCode = 1;
|
|
265
|
+
return;
|
|
266
|
+
case 'install': {
|
|
267
|
+
const version = (str(flags.version) || pos[0] || cfg.zkVersion).replace(/^v/i, '');
|
|
268
|
+
// 与设置页 /install 同一套校验:版本号会进 URL 和缓存文件名
|
|
269
|
+
if (!/^\d+\.\d+\.\d+$/.test(version))
|
|
270
|
+
throw new ZkError('版本号不合法:' + version);
|
|
271
|
+
const result = await installZk(version);
|
|
272
|
+
print(json ? result : `已安装 zk ${result.version} → ${result.path}\n源:${sourceLabel(result.mirror)}(${result.url})`, json);
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
case 'init': {
|
|
276
|
+
const r = await ensureNotebook(cfg);
|
|
277
|
+
print(json ? r : `笔记本 ${r.notebook}(结构 ${cfg.structure})\n` +
|
|
278
|
+
`初始化:${r.initialized ? '是' : '已是笔记本'} · 目录:${r.dirsCreated.length ? '新建 ' + r.dirsCreated.join('、') : '已齐全'}\n` +
|
|
279
|
+
`模板:${r.templatesWritten.length ? r.templatesWritten.join(' / ') : '已存在'} · group:${r.groupsAdded.length ? r.groupsAdded.join(' / ') : '已存在'} · AGENTS.md:${r.agentsWritten ? '已写入' : (cfg.structure === 'layered' ? '已存在' : 'flat 不写')} · WORKFLOW.md:${r.workflowWritten ? '已写入' : (cfg.structure === 'layered' ? '已存在' : 'flat 不写')}`, json);
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
case 'new': {
|
|
283
|
+
const title = str(flags.title) || pos[0] || '';
|
|
284
|
+
if (!title)
|
|
285
|
+
throw new ZkError('用法:zk-kit new --title 标题 [--name 文件名] [--body 正文] [--tags a,b]');
|
|
286
|
+
const tags = [...new Set([...cfg.defaultTags, ...list(flags.tags)])];
|
|
287
|
+
const created = await createNote(cfg, {
|
|
288
|
+
title,
|
|
289
|
+
name: str(flags.name) || undefined,
|
|
290
|
+
content: withTags(str(flags.body), tags),
|
|
291
|
+
dir: str(flags.dir) || cfg.notesDir,
|
|
292
|
+
});
|
|
293
|
+
const archived = created.archived;
|
|
294
|
+
print(json ? { ...created, tags } : `已创建 ${created.path}${tags.length ? ' · ' + tags.map((t) => '#' + t).join(' ') : ''}`, json);
|
|
295
|
+
for (const hit of archived) {
|
|
296
|
+
console.error(`⚠️ 归档里有同名旧结论:${hit.path}${hit.reason ? `(废弃原因:${hit.reason})` : '(没有记录废弃原因)'} —— 先读它,确认不是同一个结论再复用标题`);
|
|
297
|
+
}
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
case 'search': {
|
|
301
|
+
const notes = await searchNotes(cfg, {
|
|
302
|
+
query: pos[0] || str(flags.query) || undefined,
|
|
303
|
+
tags: list(flags.tags),
|
|
304
|
+
since: str(flags.since) || undefined,
|
|
305
|
+
limit: Number(str(flags.limit)) || 10,
|
|
306
|
+
sort: str(flags.sort) || 'created-desc',
|
|
307
|
+
});
|
|
308
|
+
print(json ? notes : noteList(notes), json);
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
case 'read': {
|
|
312
|
+
const target = str(flags.query) ? '' : (pos[0] ?? '');
|
|
313
|
+
if (!target && !flags.query)
|
|
314
|
+
throw new ZkError('用法:zk-kit read <路径> 或 zk-kit read --query 搜索词');
|
|
315
|
+
const doc = target ? await readNote(cfg, target) : await findNote(cfg, str(flags.query));
|
|
316
|
+
if (!doc)
|
|
317
|
+
throw new ZkError('没找到这篇笔记');
|
|
318
|
+
const limit = Number(str(flags.maxChars)) || cfg.maxReadChars;
|
|
319
|
+
const content = doc.content.length > limit ? doc.content.slice(0, limit) + '\n\n…(已截断)' : doc.content;
|
|
320
|
+
print(json ? { ...doc, content } : `# ${doc.title}\n${doc.path}\n\n${content}`, json);
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
case 'daily': {
|
|
324
|
+
const daily = await ensureDailyNote(cfg, { date: str(flags.date) || undefined });
|
|
325
|
+
if (str(flags.entry))
|
|
326
|
+
appendDaily(cfg, daily.path, str(flags.entry));
|
|
327
|
+
print(json ? daily : `日记 ${daily.created ? '已新建' : '已存在'}:${daily.path}${str(flags.entry) ? '(已追加一段)' : ''}`, json);
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
case 'append': {
|
|
331
|
+
const p = pos[0];
|
|
332
|
+
if (!p || !str(flags.text))
|
|
333
|
+
throw new ZkError('用法:zk-kit append <路径> --text 一段');
|
|
334
|
+
const r = appendToNote(cfg, p, str(flags.text), { heading: str(flags.heading) || undefined });
|
|
335
|
+
print(json ? r : `已追加 → ${r.path}(${r.bytes} 字节)`, json);
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
case 'link': {
|
|
339
|
+
const [from, to] = [pos[0], pos[1]];
|
|
340
|
+
if (!from || !to)
|
|
341
|
+
throw new ZkError('用法:zk-kit link <来源路径> <目标路径或标题>');
|
|
342
|
+
const r = await linkNotes(cfg, from, to);
|
|
343
|
+
print(json ? r : `${r.from} → ${r.link}${r.added ? '(已添加)' : '(已存在)'}`, json);
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
case 'lint': {
|
|
347
|
+
await ensureNotebook(cfg);
|
|
348
|
+
const r = await lintNotebook(cfg, { staleDays: Number(str(flags['stale-days'])) || 90 });
|
|
349
|
+
print(json ? r : lintReport(r), json);
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
case 'dream': {
|
|
353
|
+
await ensureNotebook(cfg);
|
|
354
|
+
const r = await dreamNotebook(cfg, { days: Number(str(flags.days)) || 7 });
|
|
355
|
+
print(json ? r : dreamReport(r), json);
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
case 'query': {
|
|
359
|
+
await ensureNotebook(cfg);
|
|
360
|
+
// 透传不给 parse() 处理:-- 之后的参数要原样交给 zk
|
|
361
|
+
const notes = await queryNotes(cfg, raw.filter((a) => a !== '--'));
|
|
362
|
+
print(json ? notes : noteList(notes), json);
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
case 'index': {
|
|
366
|
+
await indexNotebook(cfg);
|
|
367
|
+
print(json ? { ok: true } : '✓ 索引已重建', json);
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
case 'notebooks': {
|
|
371
|
+
const sub = pos[0] ?? 'list';
|
|
372
|
+
if (sub === 'list') {
|
|
373
|
+
const items = Object.keys(cfg.notebooks).map((name) => {
|
|
374
|
+
const target = selectNotebook(cfg, name);
|
|
375
|
+
return { name, path: target.notebook, structure: target.structure, initialized: hasNotebook(target) };
|
|
376
|
+
});
|
|
377
|
+
print(json ? { active: cfg.active, notebooks: items } : notebookList(cfg), json);
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
if (sub === 'add') {
|
|
381
|
+
const name = pos[1] ?? '';
|
|
382
|
+
const p = pos.slice(2).join(' ');
|
|
383
|
+
if (!isNotebookName(name)) {
|
|
384
|
+
throw new ZkError(`笔记本名字不合法:${name || '(空)'}(只接受字母、数字、_ 与 -,1-32 字符)\n${USAGE_NOTEBOOKS}`);
|
|
385
|
+
}
|
|
386
|
+
// 新笔记本结构跟当前那个一致(以前结构是全局的,这就是它的迁移语义)
|
|
387
|
+
const next = writeConfig({ notebooks: { ...cfg.notebooks, [name]: { path: p, structure: cfg.structure } } });
|
|
388
|
+
const target = selectNotebook(next, name);
|
|
389
|
+
if (!hasNotebook(target))
|
|
390
|
+
console.error(`⚠️ ${target.notebook} 还不是 zk 笔记本 —— 切过去(zk-kit notebooks use ${name})会自动建好`);
|
|
391
|
+
const tail = next.active === name ? '' : `(当前还是 ${next.active})`;
|
|
392
|
+
print(json ? { name, notebook: target.notebook, structure: target.structure } : `已新增笔记本 ${name} → ${target.notebook}(结构 ${target.structure})${tail}`, json);
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
if (sub === 'use') {
|
|
396
|
+
const name = pos[1] ?? '';
|
|
397
|
+
if (!(name in cfg.notebooks))
|
|
398
|
+
throw missingNotebook(name, cfg);
|
|
399
|
+
const next = writeConfig({ active: name });
|
|
400
|
+
// 切换即就绪:ensureNotebook 幂等,缺了才补;失败只告警(配置已经落盘)
|
|
401
|
+
const tail = await autoInit(next);
|
|
402
|
+
print(json ? { active: next.active, notebook: next.notebook } : `默认笔记本 → ${next.active}(${next.notebook})${tail}\n(只影响新会话;已经跑着的会话用 zk_use 跟随)`, json);
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
if (sub === 'remove') {
|
|
406
|
+
const name = pos[1] ?? '';
|
|
407
|
+
if (!(name in cfg.notebooks))
|
|
408
|
+
throw missingNotebook(name, cfg);
|
|
409
|
+
const map = { ...cfg.notebooks };
|
|
410
|
+
delete map[name];
|
|
411
|
+
const rest = Object.keys(map);
|
|
412
|
+
if (!rest.length)
|
|
413
|
+
throw new ZkError('至少要留一个笔记本');
|
|
414
|
+
const patch = { notebooks: map };
|
|
415
|
+
const switched = cfg.active === name;
|
|
416
|
+
if (switched)
|
|
417
|
+
patch.active = rest[0];
|
|
418
|
+
const next = writeConfig(patch);
|
|
419
|
+
const tail = switched ? await autoInit(next) : '';
|
|
420
|
+
print(json ? { removed: name, active: next.active, diskKept: true } : `已从配置移除笔记本 ${name}(当前 ${next.active})· 磁盘目录与笔记保留${tail}`, json);
|
|
421
|
+
return;
|
|
422
|
+
}
|
|
423
|
+
throw new ZkError(USAGE_NOTEBOOKS);
|
|
424
|
+
}
|
|
425
|
+
case 'config': {
|
|
426
|
+
const sub = pos[0] ?? 'get';
|
|
427
|
+
if (sub === 'get') {
|
|
428
|
+
const key = pos[1] ?? '';
|
|
429
|
+
if (!key) {
|
|
430
|
+
const lines = KEYS.map((k) => `${k} = ${fmtValue(cfg[k])}`);
|
|
431
|
+
lines.push('', `配置:${CFG_FILE}`);
|
|
432
|
+
print(json ? cfg : lines.join('\n'), json);
|
|
433
|
+
return;
|
|
434
|
+
}
|
|
435
|
+
if (!isKey(key))
|
|
436
|
+
throw new ZkError(`没有配置项 ${key} —— ${USAGE_CONFIG}`);
|
|
437
|
+
print(json ? { [key]: cfg[key] } : fmtValue(cfg[key]), json);
|
|
438
|
+
return;
|
|
439
|
+
}
|
|
440
|
+
if (sub === 'set') {
|
|
441
|
+
const patch = {};
|
|
442
|
+
for (const item of pos.slice(1)) {
|
|
443
|
+
const at = item.indexOf('=');
|
|
444
|
+
const key = at > 0 ? item.slice(0, at) : '';
|
|
445
|
+
if (!key)
|
|
446
|
+
throw new ZkError(USAGE_CONFIG);
|
|
447
|
+
// 派生键有专门语义,单键赋值只会造出第二种真相 —— 直接拦下并指路
|
|
448
|
+
if (key === 'notebook') {
|
|
449
|
+
throw new ZkError('notebook 是派生值(= notebooks[active] 解析后),不落盘也不可写 —— 改路径:zk-kit notebooks add 名 路径;切换:zk-kit notebooks use 名');
|
|
450
|
+
}
|
|
451
|
+
if (key === 'notebooks') {
|
|
452
|
+
throw new ZkError('notebooks 是整体映射,不接受单键赋值 —— 用 zk-kit notebooks add / use / remove 管理');
|
|
453
|
+
}
|
|
454
|
+
if (!isKey(key))
|
|
455
|
+
throw new ZkError(USAGE_CONFIG);
|
|
456
|
+
patch[key] = item.slice(at + 1);
|
|
457
|
+
}
|
|
458
|
+
if (!Object.keys(patch).length)
|
|
459
|
+
throw new ZkError(USAGE_CONFIG);
|
|
460
|
+
const next = writeConfig(patch);
|
|
461
|
+
const touched = Object.keys(patch).filter(isKey);
|
|
462
|
+
console.log(touched.map((k) => `${k} = ${fmtValue(next[k])}`).join('\n'));
|
|
463
|
+
// 归一化会把不合法值悄悄换成安全值(例:structure=xyz → flat),不回显就等于命令"成功了但没生效"
|
|
464
|
+
const normalized = touched.filter((k) => fmtValue(next[k]) !== String(patch[k]).trim());
|
|
465
|
+
if (normalized.length) {
|
|
466
|
+
console.error('⚠️ 归一化:' + normalized.map((k) => `${k} 写入 "${String(patch[k])}" → 实际 "${fmtValue(next[k])}"`).join(';'));
|
|
467
|
+
}
|
|
468
|
+
// 与设置页同一套告警:只保存配置不自动建目录,切结构后要显式 init
|
|
469
|
+
const warn = [];
|
|
470
|
+
if (next.zkPath && !fs.existsSync(next.zkPath))
|
|
471
|
+
warn.push('zk 路径不存在:' + next.zkPath);
|
|
472
|
+
if (!hasNotebook(next)) {
|
|
473
|
+
warn.push('这个目录还不是 zk 笔记本 —— 跑 zk-kit init');
|
|
474
|
+
}
|
|
475
|
+
else {
|
|
476
|
+
const st = structureStatus(next);
|
|
477
|
+
if (st.missing.length)
|
|
478
|
+
warn.push(`结构是 ${st.mode},但还缺目录:${st.missing.join('、')} —— 跑 zk-kit init`);
|
|
479
|
+
if (st.agents.required && !st.agents.present)
|
|
480
|
+
warn.push('layered 结构缺 AGENTS.md —— 跑 zk-kit init');
|
|
481
|
+
}
|
|
482
|
+
for (const line of warn)
|
|
483
|
+
console.error('⚠️ ' + line);
|
|
484
|
+
return;
|
|
485
|
+
}
|
|
486
|
+
throw new ZkError(USAGE_CONFIG);
|
|
487
|
+
}
|
|
488
|
+
case 'help':
|
|
489
|
+
case '--help':
|
|
490
|
+
case '-h':
|
|
491
|
+
console.log(HELP);
|
|
492
|
+
return;
|
|
493
|
+
default:
|
|
494
|
+
throw new ZkError(`不认识的命令:${cmd}(zk-kit help 看用法)`);
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
main().catch((err) => {
|
|
498
|
+
const msg = err instanceof ZkError && err.hint ? `${err.message}\n提示:${err.hint}` : err instanceof Error ? err.message : String(err);
|
|
499
|
+
console.error('✗ ' + msg);
|
|
500
|
+
process.exitCode = 1;
|
|
501
|
+
});
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 路径常量(唯一真相)。
|
|
3
|
+
*
|
|
4
|
+
* 分两处落盘,理由不同:
|
|
5
|
+
* · **配置 / 内置 zk / 缓存**在 ~/.dsh/zdiary —— 跟会话无关,换项目也复用;
|
|
6
|
+
* · **笔记本**默认在工作区内的 <workspace>/zdiary —— 会话文件策略(workspace-write)只放行工作区内的
|
|
7
|
+
* 读/写,笔记本放外面的话 agent 连 Read/Edit 那些 .md 都要开 danger-full-access;放里面
|
|
8
|
+
* 就只要 workspace-write,人和别的工具也看得见。
|
|
9
|
+
* 插件自己在宿主进程里跑,用 node:fs / 子进程,不受这层策略约束 —— 但没必要让用户为了读日记去放宽权限。
|
|
10
|
+
*/
|
|
11
|
+
import os from 'node:os';
|
|
12
|
+
import path from 'node:path';
|
|
13
|
+
export const HOME = os.homedir();
|
|
14
|
+
/** DSH 自己的 home(尊重 $DSH_HOME,和宿主一致) */
|
|
15
|
+
export const DSH_HOME = process.env.DSH_HOME
|
|
16
|
+
? path.resolve(process.env.DSH_HOME)
|
|
17
|
+
: path.join(HOME, '.dsh');
|
|
18
|
+
/** 插件工作目录(配置 / zk 二进制 / 缓存) */
|
|
19
|
+
export const WORK_DIR = path.join(DSH_HOME, 'zdiary');
|
|
20
|
+
export const CFG_FILE = path.join(WORK_DIR, 'zdiary.config.json');
|
|
21
|
+
/** 写配置时的跨进程锁(读不加锁:writeJson 是临时文件 + rename,读到的要么旧要么新) */
|
|
22
|
+
export const CFG_LOCK = path.join(WORK_DIR, 'zdiary.config.lock');
|
|
23
|
+
/** 会话级笔记本选择:一个会话一份小文件,彼此不会覆盖 */
|
|
24
|
+
export const SESSION_DIR = path.join(WORK_DIR, 'sessions');
|
|
25
|
+
/** 插件托管的 zk 二进制(resolveZk 找不到系统 zk 时用这里) */
|
|
26
|
+
export const BIN_DIR = path.join(WORK_DIR, 'bin');
|
|
27
|
+
export const CACHE_DIR = path.join(WORK_DIR, 'cache');
|
|
28
|
+
/**
|
|
29
|
+
* 进程级工作区兜底:$DSH_WORKSPACE → 进程 cwd(每次现读)。
|
|
30
|
+
*
|
|
31
|
+
* 真正该用的是「调用方会话的工作区」:工具执行与提示词装配都由宿主把 agent.id 传进来,
|
|
32
|
+
* 再反查 session cwd(见 lib/workspace.ts)。这里是拿不到会话时的退路 ——
|
|
33
|
+
* 设置页、CLI、以及不带 agent 的裸装配,用的就是它。
|
|
34
|
+
*/
|
|
35
|
+
export function processWorkspace() {
|
|
36
|
+
const env = String(process.env.DSH_WORKSPACE ?? '').trim();
|
|
37
|
+
return env ? path.resolve(env) : process.cwd();
|
|
38
|
+
}
|
|
39
|
+
/** 默认笔记本位置:工作区内(可写根里),不是 ~/.dsh */
|
|
40
|
+
export function defaultNotebook(workspace = processWorkspace()) {
|
|
41
|
+
return path.join(workspace, 'zdiary');
|
|
42
|
+
}
|
|
43
|
+
/** `~` 展开;空值原样返回 */
|
|
44
|
+
export function expandHome(p) {
|
|
45
|
+
const s = String(p ?? '').trim();
|
|
46
|
+
if (!s)
|
|
47
|
+
return '';
|
|
48
|
+
if (s === '~')
|
|
49
|
+
return HOME;
|
|
50
|
+
if (s.startsWith('~/') || s.startsWith('~\\'))
|
|
51
|
+
return path.join(HOME, s.slice(2));
|
|
52
|
+
return s;
|
|
53
|
+
}
|
|
54
|
+
/** 相对路径按工作区解析(不是按进程 cwd —— 两者可能不是一回事) */
|
|
55
|
+
export function resolveInWorkspace(p, workspace = processWorkspace()) {
|
|
56
|
+
return path.isAbsolute(p) ? path.resolve(p) : path.resolve(workspace, p);
|
|
57
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 会话级笔记本:一个 DSH 会话固定看一本。
|
|
3
|
+
*
|
|
4
|
+
* 为什么全局 active 不够:多个会话共用一份配置,A 里切库会让 B 的下一句工具调用
|
|
5
|
+
* 和下一步记忆注入一起换库 —— 手里还攥着旧库路径的调用会写错地方,而且一句提示都没有。
|
|
6
|
+
*
|
|
7
|
+
* 语义:
|
|
8
|
+
* · `active` 是**默认值**:会话第一次调用时快照成自己的 pin;
|
|
9
|
+
* · 会话 pin 住之后,别处(设置页 / CLI / 别的会话)切库不再带动它;
|
|
10
|
+
* · 本会话换库只能显式来(zk_use),这样「这次切换影响谁」是可解释的;
|
|
11
|
+
* · pin 落在 sessions/<hash>.json:进程重启 / 会话 resume 还能接上,30 天没动的清掉;
|
|
12
|
+
* · pin 指向的笔记本被移除时静默回到默认值并重写 pin(跟删配置的语义一致)。
|
|
13
|
+
*
|
|
14
|
+
* 落盘是每会话一份小文件,不是一张大表 —— 多进程同时写也不会互相覆盖。
|
|
15
|
+
*/
|
|
16
|
+
import crypto from 'node:crypto';
|
|
17
|
+
import fs from 'node:fs';
|
|
18
|
+
import path from 'node:path';
|
|
19
|
+
import { SESSION_DIR } from './paths.js';
|
|
20
|
+
import { config, isNotebookName, selectNotebook } from './store.js';
|
|
21
|
+
import { readJson, writeJson } from '../lib/json-file.js';
|
|
22
|
+
/** 进程内只缓存「pin 是什么」;文件才是真相,别的进程可能刚改过 */
|
|
23
|
+
const pins = new Map();
|
|
24
|
+
let pruned = false;
|
|
25
|
+
const PIN_TTL_MS = 30 * 24 * 60 * 60 * 1000;
|
|
26
|
+
function fileOf(sessionId) {
|
|
27
|
+
const hash = crypto.createHash('sha1').update(sessionId).digest('hex').slice(0, 20);
|
|
28
|
+
return path.join(SESSION_DIR, `${hash}.json`);
|
|
29
|
+
}
|
|
30
|
+
/** 清掉很久没动过的会话文件;一个进程只扫一次,失败不影响功能 */
|
|
31
|
+
function pruneOnce() {
|
|
32
|
+
if (pruned)
|
|
33
|
+
return;
|
|
34
|
+
pruned = true;
|
|
35
|
+
let names;
|
|
36
|
+
try {
|
|
37
|
+
names = fs.readdirSync(SESSION_DIR);
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
const now = Date.now();
|
|
43
|
+
for (const name of names) {
|
|
44
|
+
const file = path.join(SESSION_DIR, name);
|
|
45
|
+
try {
|
|
46
|
+
if (now - fs.statSync(file).mtimeMs > PIN_TTL_MS)
|
|
47
|
+
fs.rmSync(file, { force: true });
|
|
48
|
+
}
|
|
49
|
+
catch { /* 并发删掉了 / 没权限:跳过 */ }
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/** 这个会话固定在哪本;没有 pin(新会话 / 老文件坏了)返回 null */
|
|
53
|
+
export function sessionNotebook(sessionId) {
|
|
54
|
+
if (!sessionId)
|
|
55
|
+
return null;
|
|
56
|
+
const cached = pins.get(sessionId);
|
|
57
|
+
if (cached !== undefined)
|
|
58
|
+
return cached;
|
|
59
|
+
pruneOnce();
|
|
60
|
+
const record = readJson(fileOf(sessionId), null);
|
|
61
|
+
const name = record && typeof record.notebook === 'string' && isNotebookName(record.notebook)
|
|
62
|
+
? record.notebook
|
|
63
|
+
: null;
|
|
64
|
+
pins.set(sessionId, name);
|
|
65
|
+
return name;
|
|
66
|
+
}
|
|
67
|
+
/** 把会话固定到某本(zk_use 与首次自动 pin 共用) */
|
|
68
|
+
export function pinSession(sessionId, name, workspace) {
|
|
69
|
+
pins.set(sessionId, name);
|
|
70
|
+
try {
|
|
71
|
+
writeJson(fileOf(sessionId), {
|
|
72
|
+
id: sessionId,
|
|
73
|
+
notebook: name,
|
|
74
|
+
...(workspace ? { workspace } : {}),
|
|
75
|
+
updatedAt: new Date().toISOString(),
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
catch { /* 只读环境:内存 pin 仍然有效,重启后退回默认值 */ }
|
|
79
|
+
}
|
|
80
|
+
/** 当前会话该用哪份配置:pin 优先,没有就快照默认值并落盘 */
|
|
81
|
+
export function configFor(workspace, sessionId) {
|
|
82
|
+
const base = config(workspace);
|
|
83
|
+
if (!sessionId)
|
|
84
|
+
return base;
|
|
85
|
+
const pinned = sessionNotebook(sessionId);
|
|
86
|
+
if (!pinned) {
|
|
87
|
+
pinSession(sessionId, base.active, workspace);
|
|
88
|
+
return base;
|
|
89
|
+
}
|
|
90
|
+
// pin 指向的笔记本被删了:回到默认值,别让整个会话卡在空配置上
|
|
91
|
+
if (!(pinned in base.notebooks)) {
|
|
92
|
+
pinSession(sessionId, base.active, workspace);
|
|
93
|
+
return base;
|
|
94
|
+
}
|
|
95
|
+
return pinned === base.active ? base : selectNotebook(base, pinned, workspace);
|
|
96
|
+
}
|
|
97
|
+
/** 给 zk_context / zk_stats 回显用:让「本会话固定在 X」是能看见的,而不是靠猜 */
|
|
98
|
+
export function sessionState(workspace, sessionId) {
|
|
99
|
+
const cfg = configFor(workspace, sessionId);
|
|
100
|
+
const defaultName = config(workspace).active;
|
|
101
|
+
const pinned = sessionNotebook(sessionId) ?? '';
|
|
102
|
+
return {
|
|
103
|
+
active: cfg.active,
|
|
104
|
+
defaultName,
|
|
105
|
+
pinned,
|
|
106
|
+
diverged: !!pinned && pinned !== defaultName,
|
|
107
|
+
};
|
|
108
|
+
}
|