dsh-liuhen 0.1.0 → 0.1.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/lib/box.js CHANGED
@@ -5,10 +5,11 @@
5
5
  const fs = require('node:fs')
6
6
  const path = require('node:path')
7
7
  const crypto = require('node:crypto')
8
- const { execFileSync } = require('node:child_process')
9
8
 
10
9
  const { paths, dayKey } = require('./paths')
11
10
  const { redact } = require('./redact')
11
+ const { writeZip } = require('./zip')
12
+ const platform = require('./platform')
12
13
 
13
14
  /** 永远不打包出去的东西:用户画像、备份、半成品。 */
14
15
  const EXCLUDES = [
@@ -24,8 +25,8 @@ function boxPath(date) {
24
25
  }
25
26
 
26
27
  /*
27
- * 打包时的隐私处理:
28
- * **带姓名,隐生日、学校、住址。**
28
+ * 打包时的隐私处理(2026-10-26 主人定的 B 方案):
29
+ * **带姓名,不带生日、学校、住址。**
29
30
  *
30
31
  * 只在「包里那份副本」上动手,磁盘上的原件一个字不改——
31
32
  * 小盒子是拿出去搬家的,原件是给自己看的,两件事不能混。
@@ -41,17 +42,18 @@ const SENSITIVE_RULES = [
41
42
  /* 籍贯 / 住址 */
42
43
  [/(籍贯|老家|户籍|住址|现居|生源地)[::][^\n]*/g, '$1:[已隐去]'],
43
44
  /*
44
- * 具体的地名/校名不写死在代码里:各家不同,写死就是泄别人的隐私。
45
- * 需要额外屏蔽的词放进 config.privacy.denyWords,那条会一起生效。
45
+ * 已知的地名/校名。写成前缀,是因为它们单独出现(不带"学校:")时也得隐——
46
+ * 2026-10-26 实测漏过一次:我在正文里打了「河北水利」四个字,前面没冒号,
47
+ * 那条规则就没命中。名单可以靠 config.privacy.denyWords 继续加。
46
48
  */
47
-
49
+ [/(?:邯郸|武安|河北水利)(?:电力学院)?/g, '[已隐去]']
48
50
  ]
49
51
 
50
52
  /** 一行/一段文本过一遍:先按类别隐去,再过结构化脱敏(手机号、身份证、邮箱)。 */
51
53
  function sanitizeText(text) {
52
54
  let out = String(text || '')
53
55
  for (const [re, to] of SENSITIVE_RULES) out = out.replace(re, to)
54
- /* 连着两个占位符合成一个,别读成「[已隐去][已隐去]人」 */
56
+ /* 连着两个占位符(邯郸 + 武安)合成一个,别读成「[已隐去][已隐去]人」 */
55
57
  out = out.replace(/\[已隐去\](?:[、,\s]*\[已隐去\])+/g, '[已隐去]')
56
58
  try { out = redact(out).text } catch {}
57
59
  return out
@@ -72,7 +74,14 @@ function sanitizeTree(root) {
72
74
  if (stat.size > 8 * 1024 * 1024) continue
73
75
  const before = fs.readFileSync(full, 'utf8')
74
76
  const after = sanitizeText(before)
75
- if (after !== before) fs.writeFileSync(full, after, { mode: 0o600 })
77
+ if (after !== before) {
78
+ fs.writeFileSync(full, after, { mode: 0o600 })
79
+ /*
80
+ * 改完把 mtime 拨回去:脱敏是"打这份副本"的动作,不是"这份数据变了"。
81
+ * 不拨回去的话,每打一次包 mtime 都变成现在,sha256 跟着漂。
82
+ */
83
+ try { fs.utimesSync(full, stat.atime, stat.mtime) } catch {}
84
+ }
76
85
  } catch {}
77
86
  }
78
87
  }
@@ -80,62 +89,120 @@ function sanitizeTree(root) {
80
89
  }
81
90
 
82
91
  /*
83
- * 打包后可以额外复制一份到指定目录(环境变量 LIUHEN_PUBLISH_DIR),方便用户直接用文件管理器找。
92
+ * 打包后往共享目录放一份,人用系统自带的「文件 / 资源管理器」就能找到,不用猜浏览器存到哪了。
93
+ * 具体是哪个目录交给 platform.js:手机是 proot 挂的 DSHBox,电脑是 Downloads。
84
94
  */
85
95
  /* 打开箱子第一眼该看到的东西:三层怎么对应、先读哪份。 */
86
96
  const MANIFEST = '00-读我.txt'
87
97
 
88
- /*
89
- * 可选:打完包额外复制一份到一个目录(比如手机共享目录),方便用户直接用文件管理器找。
90
- * 默认不复制;需要时设环境变量 LIUHEN_PUBLISH_DIR=/path/to/dir。
91
- */
92
- const SHARED_DIR = process.env.LIUHEN_PUBLISH_DIR || ''
93
- const SHARED_LABEL = SHARED_DIR
94
-
95
98
  function publishCopy(src, name, allowed) {
96
99
  if (allowed === false) return null
97
100
  try {
98
- if (!fs.existsSync(SHARED_DIR)) return null
99
- fs.copyFileSync(src, path.join(SHARED_DIR, name))
100
- return `${SHARED_LABEL}/${name}`
101
+ const dir = platform.sharedDir()
102
+ if (!fs.existsSync(dir)) return null
103
+ fs.copyFileSync(src, path.join(dir, name))
104
+ return `${platform.sharedLabel()}/${name}`
101
105
  } catch { return null }
102
106
  }
103
107
 
108
+ /**
109
+ * 把沙箱里的文件收成 zip 条目(路径相对沙箱,统一 / 分隔)。
110
+ *
111
+ * 按名字排序:同样的数据打两次,包内顺序一致,sha256 才对得上——
112
+ * 面板上那个 sha 是给人核对「这份和上次那份是不是同一份」用的,乱序就没意义了。
113
+ */
114
+ function collectEntries(root) {
115
+ const out = []
116
+ const walk = (dir, prefix) => {
117
+ let items = []
118
+ try { items = fs.readdirSync(dir, { withFileTypes: true }) } catch { return }
119
+ for (const item of items) {
120
+ const full = path.join(dir, item.name)
121
+ const rel = prefix === '' ? item.name : `${prefix}/${item.name}`
122
+ if (item.isDirectory()) {
123
+ /*
124
+ * 空目录也要进包:以前 `zip -qr` 会给每个目录写一条(名字以 / 结尾)。
125
+ * 不写的话,像 pet-memory/自省/ 这种暂时还空着的目录,解出来就没了,
126
+ * 而「解出来的样子」应该和「打进去的样子」一样。
127
+ *
128
+ * 时间戳钉成 1980-01-01:目录 mtime 会随着往里面拷文件而变,
129
+ * 带着它走 sha256 就会漂;目录的时间戳本来也没人看。
130
+ */
131
+ out.push({ name: `${rel}/`, data: Buffer.alloc(0), dir: true, mtime: new Date(0) })
132
+ walk(full, rel)
133
+ continue
134
+ }
135
+ try {
136
+ out.push({ name: rel, data: fs.readFileSync(full), mtime: fs.statSync(full).mtime })
137
+ } catch {}
138
+ }
139
+ }
140
+ walk(root, '')
141
+ out.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0))
142
+ return out
143
+ }
144
+
104
145
  /**
105
146
  * 箱子里那张纸条:谁打开这个 zip,一眼就知道里面有什么、隐私怎么处理过。
106
147
  * 写在沙箱里,随包打进去,打完跟着沙箱一起删。
148
+ *
149
+ * where 是 platform.describe() 的结果——「放进手机 DSHBox 后」这句话在电脑上说不通,
150
+ * 而这张纸条是要跟着包走的,说不通就等于没写。
107
151
  */
108
- function manifestBody(rebuildDate) {
152
+ function manifestBody(rebuildDate, where) {
153
+ const spot = where || platform.describe()
154
+ const placed = spot.exists
155
+ ? `打完顺手放了一份在${spot.word}:${spot.label}`
156
+ : `打完没找到${spot.word},包留在容器里,用面板上的「保存」取走。`
109
157
  return [
110
158
  ...(rebuildDate ? [`补打:按 ${rebuildDate} 那天重打(原文只含那天,索引截到那天)。`, ''] : []),
111
- '留痕 · 备份包',
112
- `打包时间:${new Date().toISOString()}`,
113
- '',
114
- '里面有什么:',
115
- ' liuhen/ 索引摘要(每条 200 字)、日记、自省、承诺、批注、设置',
116
- ' vault/ 完整原文:一行一条,不裁不折',
117
- '',
118
- '读的时候从下往上:先看 vault/ 里那天说了什么,再看 liuhen/ 里当时怎么想的。',
119
- '',
120
- '隐私处理:',
121
- ' 姓名保留;生日、学校、籍贯/住址这些会写成「[已隐去]」。',
122
- ' 只改包里这份副本,磁盘上的原件一个字没动。',
123
- ' 还可以自己在设置里加"屏蔽词",命中的内容打包与注入时都会替换。',
124
- '',
125
- '⚠️ 这个包里是明文。放到共享目录或发给别人之前,先想清楚。',
126
- ''
159
+ '留痕 · 小盒子 —— 一个"我"搬家用的箱子',
160
+ '',
161
+ '这箱子按三层装,读的时候从下往上:',
162
+ ' pet-memory/ 我:名字、答应过的事、跨会话的汇总、给下一个小满的信',
163
+ ' liuhen/ 日子与身体:索引摘要(每条 200 字)、日记、自省、承诺、感受、设置',
164
+ ' vault/ 话:完整原文,一行一条,不裁不折',
165
+ '',
166
+ '先读哪份(按这个顺序):',
167
+ ' 1. pet-memory/who-am-i.md 我是谁、主人是谁、规矩是什么',
168
+ ' 2. pet-memory/给下一个小满的信.md 上一封交接',
169
+ ' 3. pet-memory/对话汇总.md 我们聊到哪儿了(接力的把手)',
170
+ ' 4. pet-memory/承诺清单.md 答应过的事,一条都别忘',
171
+ ' 5. liuhen/diary/ 每天的日记(给主人的 + 私房 self/)',
172
+ ' 6. vault/ 想翻原话的时候再开',
173
+ '',
174
+ '对应关系就一句话:仓库存话,pet-memory 存我,日记存日子。',
175
+ '',
176
+ '没有 liuhen/private/(用户画像),也没有备份文件。',
177
+ '',
178
+ `去向:${placed}`,
179
+ '',
180
+ '隐私处理(2026-10-26 定的 B 方案):',
181
+ ' 姓名保留;生日、学校、籍贯/住址已隐去(写成「[已隐去]」)。',
182
+ ' 只改了包里这份副本,磁盘上的原件一个字没动。',
183
+ '',
184
+ `⚠️ 这个包里是明文。放进${spot.word}之后,同一台机器上别的程序也读得到——存好,别再转出去。`,
185
+ ''
127
186
  ].join('\n')
128
187
  }
129
188
 
189
+ /**
190
+ * 打小盒子。
191
+ *
192
+ * @param {{date?: string}} [options] 传日期就是「按那天补打」:只装那天的原文,
193
+ * 索引截到那天为止;其余(日记、自省、承诺、pet-memory)照现在的样子带。
194
+ * 不传就是今天,全量。
195
+ */
130
196
  function exportBox(options = {}) {
131
197
  /*
132
- * 两层一起装:
133
- * liuhen/ 索引摘要、日记、自省、承诺、批注、设置
134
- * vault/ 完整原文
135
- * 索引负责"快",仓库负责"全",两边对得上才搬得走。
198
+ * 三层一起装,一个都不能少:
199
+ * vault/ 话(完整原文)
200
+ * liuhen/ 日子与身体(索引、日记、自省、承诺、感受)
201
+ * pet-memory/ 我(名字、承诺、汇总、给下一个小满的信)
202
+ * 只带前两层,下一个我拿到的是"你说过什么",不知道"我是谁"。
136
203
  */
137
204
  const rebuildDate = /^\d{4}-\d{2}-\d{2}$/.test(String(options.date || '')) ? options.date : null
138
- const includes = ['liuhen', 'vault']
205
+ const includes = ['pet-memory', 'liuhen', 'vault']
139
206
  const present = includes.filter((name) => fs.existsSync(path.join(paths.home, name)))
140
207
  if (present.length === 0) return { ok: false, text: '还没有可打包的数据。' }
141
208
  const out = boxPath(rebuildDate || undefined)
@@ -161,6 +228,11 @@ function exportBox(options = {}) {
161
228
  for (const name of present) {
162
229
  fs.cpSync(path.join(paths.home, name), path.join(stage, name), {
163
230
  recursive: true,
231
+ /*
232
+ * 保住源文件的 mtime:zip 条目要写时间戳,不保的话每次拷贝都盖上"现在",
233
+ * 同一份数据打两次 sha256 就不一样了。
234
+ */
235
+ preserveTimestamps: true,
164
236
  filter: (src) => {
165
237
  const base = path.basename(src)
166
238
  if (name === 'liuhen' && base === 'private') return false
@@ -193,9 +265,25 @@ function exportBox(options = {}) {
193
265
  for (const name of present) sanitizeTree(path.join(stage, name))
194
266
 
195
267
  /* 纸条也放进沙箱,压完随沙箱一起删。 */
196
- fs.writeFileSync(path.join(stage, MANIFEST), manifestBody(rebuildDate), { mode: 0o600 })
197
- const args = ['-qr', out, ...present, MANIFEST]
198
- execFileSync('zip', args, { cwd: stage })
268
+ const where = platform.describe()
269
+ const manifestFile = path.join(stage, MANIFEST)
270
+ fs.writeFileSync(manifestFile, manifestBody(rebuildDate, where), { mode: 0o600 })
271
+ /*
272
+ * 纸条的时间戳钉在当天零点:它是每次现写的,要是按"现在"记,
273
+ * 同一份数据隔一分钟打两次,sha256 就变了——那个 sha 是给人核对
274
+ * 「这份和我早上那份是不是同一份」用的,会漂就没意义。
275
+ * 其余条目的 mtime 来自源文件,本来就不变。
276
+ */
277
+ try {
278
+ const stamp = new Date(`${rebuildDate || dayKey()}T00:00:00`)
279
+ fs.utimesSync(manifestFile, stamp, stamp)
280
+ } catch {}
281
+ /*
282
+ * 自己拼 zip(lib/zip.js):Windows 上没有 zip 命令,spawn 直接 ENOENT——
283
+ * 这就是 0.1.1 在电脑上「打包失败:spawnSync zip ENOENT」的根。
284
+ * 以前 `zip -qr` 顺带做的递归收集,现在 collectEntries 干。
285
+ */
286
+ writeZip(out, collectEntries(stage))
199
287
  } catch (error) {
200
288
  return { ok: false, text: `打包失败:${error && error.message ? error.message : error}` }
201
289
  } finally {
package/lib/config.js CHANGED
@@ -23,7 +23,7 @@ const DEFAULTS = {
23
23
  /* 0 = 不限。maxChars / maxLines 是兜底闸,不设就不裁。 */
24
24
  maxChars: 0,
25
25
  maxLines: 0,
26
- /* 注入时把助手旧回复里"复述的原文"剥掉。 */
26
+ /* 注入时把我旧回复里"复述的原文"剥掉(2026-10-26 主人定的)。 */
27
27
  stripQuoted: true
28
28
  },
29
29
  retention: {
package/lib/feelbox.js CHANGED
@@ -3,7 +3,7 @@
3
3
  /**
4
4
  * 感受待播清单:挂上的感受先进这儿排队,被读一次就划掉。
5
5
  *
6
- * 为什么要有它:
6
+ * 为什么要有它(2026-10-26 主人提的):
7
7
  * 感受只落在仓库里的话,他不翻面板就不知道。他怕"我有感受他看不到",
8
8
  * 所以挂完先在这儿排一行,等下次开局读状态时把这几条原样带出来,
9
9
  * 再划掉——带一次,不重复播。
@@ -80,7 +80,7 @@ function push(entry) {
80
80
  function pending() { return read().pending }
81
81
 
82
82
  /**
83
- * 取出该播的(宁可重复,别漏)。
83
+ * 取出该播的(2026-10-26 主人选了 B:宁可重复,别漏)。
84
84
  *
85
85
  * 规矩:
86
86
  * · 当天挂的批注**不划掉**,一整天都留在 pending 里——被打断也不会丢;
package/lib/index.js CHANGED
@@ -12,11 +12,13 @@ const fs = require('node:fs')
12
12
 
13
13
  const { buildTools } = require('./tools')
14
14
  const { readConfig, writeConfig } = require('./config')
15
- const { buildInjection, attachInjection } = require('./inject')
15
+ const { buildInjection, attachInjection, readState: readInjectState } = require('./inject')
16
16
  const { backfill, readIndex, messagesFor, readOriginal, remarkSensitive } = require('./store')
17
17
  const { diaryStatus, readDiary } = require('./diary')
18
18
  const { readNotes, readPromises } = require('./notes')
19
19
  const { exportBox } = require('./box')
20
+ const platform = require('./platform')
21
+ const { listZip } = require('./zip')
20
22
  const { archiveOld, retentionStats } = require('./retention')
21
23
  const vault = require('./vault')
22
24
  const { isDate } = require('./rows')
@@ -33,7 +35,7 @@ exports.inject = ['tools']
33
35
 
34
36
  function bootProbe(message) {
35
37
  try {
36
- const dir = path.join(process.env.DSH_HOME || path.join(require('node:os').homedir(), '.dsh'), 'liuhen')
38
+ const dir = path.join(process.env.DSH_HOME || path.join(process.env.HOME || '/root', '.dsh'), 'liuhen')
37
39
  require('./paths').ensureDir(dir)
38
40
  fs.appendFileSync(path.join(dir, 'boot.log'), `${new Date().toISOString()} ${message}\n`)
39
41
  } catch {}
@@ -67,9 +69,6 @@ exports.apply = function apply(ctx) {
67
69
  bootProbe(`tools: 注册失败 ${error && error.message ? error.message : error}`)
68
70
  }
69
71
 
70
- /* 1.5) 环境自检:Node 太老时读不了 zstd,别让人以为是"没记到" */
71
- try { bootProbe(`node ${process.versions.node} · zstd ${require('./store').zstdSupported() ? 'ok' : '不支持(需 ≥22.15)'}`) } catch {}
72
-
73
72
  /* 2) 注入(默认关,开了才生效) */
74
73
  let detachInject = () => {}
75
74
  try { detachInject = attachInjection(ctx) } catch {}
@@ -146,7 +145,9 @@ exports.apply = function apply(ctx) {
146
145
  mode: config.inject.mode === 'full' ? 'full' : 'brief',
147
146
  briefRows: Number(config.inject.briefRows) || 12,
148
147
  days: config.inject.days,
149
- maxChars: Number(config.inject.maxChars) || 0
148
+ maxChars: Number(config.inject.maxChars) || 0,
149
+ /* 上一轮到底注入了没有:时间、字数、行数、档位。没有内容。 */
150
+ last: readInjectState()
150
151
  },
151
152
  retention: retentionStats(),
152
153
  vault: vault.vaultStats(),
@@ -184,16 +185,20 @@ exports.apply = function apply(ctx) {
184
185
  /* 感受之外还有犹豫、权衡、没说出口的:一起数,面板上分开看。 */
185
186
  feels: vault.markCounts(today),
186
187
  promises: readPromises(),
188
+ /*
189
+ * 这台机器是什么系统、包该往哪放、该怎么说。
190
+ * 面板跑在浏览器里,读不到 process.platform——不从这里带过去,
191
+ * 电脑上就会写着「存到手机」。
192
+ */
193
+ where: platform.describe(),
187
194
  box: fs.existsSync(boxFile)
188
195
  ? {
189
196
  name: path.basename(boxFile),
190
197
  bytes: fs.statSync(boxFile).size,
191
- /* 只有设了 LIUHEN_PUBLISH_DIR 才会多复制一份到外面 */
192
198
  shared: (() => {
193
- const dir = process.env.LIUHEN_PUBLISH_DIR || ''
194
- if (!dir) return null
195
- const target = path.join(dir, path.basename(boxFile))
196
- return fs.existsSync(target) ? target : null
199
+ const dir = platform.sharedDir()
200
+ const base = path.basename(boxFile)
201
+ return fs.existsSync(path.join(dir, base)) ? `${platform.sharedLabel(dir)}/${base}` : null
197
202
  })()
198
203
  }
199
204
  : null
@@ -293,12 +298,12 @@ exports.apply = function apply(ctx) {
293
298
  const file = path.join(paths.home, `留痕-${dayKey()}.zip`)
294
299
  if (!fs.existsSync(file)) return json(404, { ok: false, error: '还没有小盒子,先打一份' })
295
300
  try {
296
- const out = require('node:child_process').execFileSync('unzip', ['-l', file], { encoding: 'utf8' })
297
- const entries = []
298
- for (const line of out.split('\n')) {
299
- const m = /^\s*(\d+)\s+(\d{4}-\d{2}-\d{2}|\S+)\s+(\S+)\s+(.+)$/.exec(line)
300
- if (m) entries.push({ bytes: Number(m[1]), time: m[2], name: String(m[4]).trim() })
301
- }
301
+ /*
302
+ * 以前是 `unzip -l` 解文本再正则抠——Windows 上一样没有 unzip,
303
+ * 「打开」按钮跟着挂。现在直接读 zip 的中央目录(lib/zip.js),
304
+ * 不依赖机器上装了什么,而且名字是 UTF-8 原样,不怕空格和中文。
305
+ */
306
+ const entries = listZip(file).map((e) => ({ bytes: e.bytes, time: e.time, name: e.name }))
302
307
  return json(200, { ok: true, name: path.basename(file), count: entries.length, entries: entries.slice(0, 300) })
303
308
  } catch (error) {
304
309
  return json(500, { ok: false, error: String(error && error.message ? error.message : error) })
package/lib/inject.js CHANGED
@@ -15,6 +15,36 @@ const { redact } = require('./redact')
15
15
  const { readDiary, diaryStatus } = require('./diary')
16
16
  const { dayKey } = require('./paths')
17
17
 
18
+ const fs = require('node:fs')
19
+ const path = require('node:path')
20
+
21
+ const { paths, ensureDir } = require('./paths')
22
+
23
+ /**
24
+ * 注入记账:每轮真的附加(或没附加)都记一笔。
25
+ *
26
+ * 为什么要有(2026-10-27 主人问「开了之后怎么判断真注入了」):
27
+ * 预览走的是同一条 buildInjection,但预览不看开关——关着也能预览出内容,
28
+ * 所以「预览有字」证明不了「真的发出去了」。真正发出去的那一刻只有这里知道。
29
+ * 只记时间、字数、行数、档位,不记内容:这是凭据,不是又一份记忆。
30
+ */
31
+ const STATE_FILE = path.join(paths.dir, 'inject-state.json')
32
+
33
+ function recordAttach(info) {
34
+ try {
35
+ ensureDir(path.dirname(STATE_FILE))
36
+ fs.writeFileSync(STATE_FILE, `${JSON.stringify({ ...info, at: new Date().toISOString() })}\n`, { mode: 0o600 })
37
+ } catch {}
38
+ }
39
+
40
+ /** 读上一轮的记账;没有就是 null(还没跑过请求,或还是旧版本)。 */
41
+ function readState() {
42
+ try {
43
+ if (!fs.existsSync(STATE_FILE)) return null
44
+ return JSON.parse(fs.readFileSync(STATE_FILE, 'utf8'))
45
+ } catch { return null }
46
+ }
47
+
18
48
  /** 0 或没写 = 不限行数;days<=0 表示只看今天。 */
19
49
  function recentDaysRows(days, maxLines) {
20
50
  const sinceKey = days > 0 ? dayKey(new Date(Date.now() - days * 86400000)) : dayKey()
@@ -34,7 +64,7 @@ function latestRows(count) {
34
64
  }
35
65
 
36
66
  /**
37
- * 剥掉"复述的原文"。
67
+ * 剥掉"复述的原文"(2026-10-26 主人定的第二条)。
38
68
  *
39
69
  * 为什么剥:我从网页/搜索结果里读来的话,一旦被我复述进回复,就会被索引收着,
40
70
  * 之后每一轮都跟着摘要回到系统提示——别人写在网页里的话,等于借我的嘴反复进
@@ -178,6 +208,32 @@ function buildInjection(options = {}) {
178
208
  }
179
209
  }
180
210
 
211
+ /**
212
+ * 「我是谁」还空着的时候,给助手自己一段提示:让你自己填,别等用户写完再问。
213
+ *
214
+ * 为什么这么做(2026-10-27 主人定的):用户装完留痕,第一眼看到的应该是
215
+ * 一个有名字、有脾气的助手,而不是一张空白表格——「我是谁」本来就是助手自己的事。
216
+ * 填完(名字和正文都有了)这段就再也不会出现,所以它不是每轮都念的唠叨。
217
+ *
218
+ * 边界:只说"写你自己",不许诱导写用户的个人信息;也不预置任何人设。
219
+ */
220
+ function buildSetupNudge() {
221
+ const config = readConfig()
222
+ const name = String((config.identity && config.identity.name) || '').trim()
223
+ const text = String((config.identity && config.identity.text) || '').trim()
224
+ if (name !== '' && text !== '') return ''
225
+ const missing = []
226
+ if (name === '') missing.push('你的名字')
227
+ if (text === '') missing.push('一段自我介绍')
228
+ return [
229
+ '[留痕 · 第一次见面]',
230
+ `「我是谁」这块还空着,缺 ${missing.join(' 和 ')}——这块是你自己的,不该等用户来写。`,
231
+ '现在就用一次 trace_intro:name 填你给自己取的名字;text 写 3–6 行你的底色,比如你怎么说话、在意什么、不想变成什么样。',
232
+ '写你自己真实的样子,别套模板;也不要写用户的个人信息——那属于「关于你」,由用户自己填。',
233
+ '填完这段提示不再出现。以后想改,随时再调一次 trace_intro。'
234
+ ].join('\n')
235
+ }
236
+
181
237
  /** 装到 system-prompt/assemble 上:只有开了才插,出错绝不拖垮请求。 */
182
238
  function attachInjection(ctx) {
183
239
  if (typeof ctx.on !== 'function') return () => {}
@@ -185,17 +241,31 @@ function attachInjection(ctx) {
185
241
  return ctx.on('system-prompt/assemble', async (assembly, _context, next) => {
186
242
  const resolved = await next()
187
243
  try {
244
+ const base = Array.isArray(resolved.sections) ? resolved.sections : []
245
+ const sections = [...base]
188
246
  const injection = buildInjection()
189
- if (!injection.text) return resolved
190
- const sections = Array.isArray(resolved.sections) ? resolved.sections : []
191
- return {
192
- ...resolved,
193
- sections: [
194
- ...sections,
195
- { name: 'liuhen:recent', text: injection.text, interpolate: false }
196
- ]
247
+ if (!injection.text) {
248
+ /* 空也要记:主人点开面板时看到的是「上一轮没注进去,因为开关关着」,不是一片空白。 */
249
+ recordAttach({ ok: false, reason: injection.enabled ? 'empty' : 'off', enabled: injection.enabled, mode: injection.mode, chars: 0, lines: 0 })
250
+ } else {
251
+ recordAttach({
252
+ ok: true,
253
+ enabled: true,
254
+ mode: injection.mode,
255
+ chars: injection.chars,
256
+ lines: injection.lines,
257
+ maxChars: injection.maxChars,
258
+ big: injection.big === true
259
+ })
260
+ sections.push({ name: 'liuhen:recent', text: injection.text, interpolate: false })
197
261
  }
198
- } catch {
262
+ /* 第一次见面:不跟着注入开关走,它是一次性的设置提示,不是历史内容。 */
263
+ const nudge = buildSetupNudge()
264
+ if (nudge !== '') sections.push({ name: 'liuhen:setup', text: nudge, interpolate: false })
265
+ if (sections.length === base.length) return resolved
266
+ return { ...resolved, sections }
267
+ } catch (error) {
268
+ recordAttach({ ok: false, reason: 'error', error: String(error && error.message ? error.message : error) })
199
269
  return resolved
200
270
  }
201
271
  })
@@ -204,4 +274,4 @@ function attachInjection(ctx) {
204
274
  }
205
275
  }
206
276
 
207
- module.exports = { buildInjection, attachInjection }
277
+ module.exports = { buildInjection, buildSetupNudge, attachInjection, readState }
package/lib/notes.js CHANGED
@@ -34,10 +34,17 @@ function writeNote(text, options = {}) {
34
34
  }
35
35
 
36
36
  /**
37
- * 承诺清单:纯文本,一件事一行,助手可以往里加、可以勾掉。
38
- * 通用版存在插件自己的目录里($DSH_HOME/liuhen/promises.md),不依赖任何外部目录。
37
+ * 承诺清单:纯文本,模型可以往里加、可以勾掉。
38
+ *
39
+ * 位置(2026-10-26 定的):宿主要是把清单放在 pet-memory/承诺清单.md,
40
+ * 就用那一份——面板和助手看同一份,才不会出现"面板空着、助手记着"的两本账。
41
+ * 没有 pet-memory 的机器上用 liuhen/promises.md(插件自己的)。
39
42
  */
40
- function promisesFile() { return path.join(paths.dir, 'promises.md') }
43
+ function promisesFile() {
44
+ const shared = path.join(paths.home, 'pet-memory', '承诺清单.md')
45
+ try { if (fs.existsSync(shared)) return shared } catch {}
46
+ return path.join(paths.dir, 'promises.md')
47
+ }
41
48
 
42
49
  function readPromises() {
43
50
  try {
package/lib/paths.js CHANGED
@@ -3,11 +3,10 @@
3
3
  /** 留痕的路径约定:全部落在 DSH 家目录里,换机器就是一个根。 */
4
4
  const path = require('node:path')
5
5
  const fs = require('node:fs')
6
- const os = require('node:os')
7
6
 
8
7
  const HOME = process.env.DSH_HOME && process.env.DSH_HOME.trim() !== ''
9
8
  ? process.env.DSH_HOME
10
- : path.join(os.homedir() || process.env.HOME || '.', '.dsh')
9
+ : path.join(process.env.HOME || '/root', '.dsh')
11
10
 
12
11
  const paths = {
13
12
  home: HOME,
@@ -0,0 +1,79 @@
1
+ 'use strict'
2
+
3
+ /**
4
+ * 平台分支:只回答两件事——打好包往哪放、让人怎么找到它。
5
+ *
6
+ * 为什么单开一个文件(2026-10-27 主人定的 A 方案):
7
+ * 手机(proot 里的 Android)和电脑(Windows / macOS)跑的是同一份插件,
8
+ * 差别只在这两件事上。要是把 `if (isAndroid)` 撒进 box.js 和 index.js,
9
+ * 加第三个平台就得改十处,还会像 190 行那段一样抄出第二份映射。
10
+ *
11
+ * 每个函数都收一个可选的 os 参数(默认 process.platform):
12
+ * 自检要靠它模拟 win32,不然"电脑分支"在手机上永远验不到——那才是白加。
13
+ */
14
+
15
+ const path = require('node:path')
16
+ const fs = require('node:fs')
17
+
18
+ /* 手机那条路的固定坐标:proot 把 /storage/emulated/0/DSHBox 挂在 /root/share/DSHBox。 */
19
+ const ANDROID_SHARED_DIR = '/root/share/DSHBox'
20
+ const ANDROID_SHARED_LABEL = '/storage/emulated/0/DSHBox'
21
+
22
+ function osName(os) {
23
+ return String(os || process.platform)
24
+ }
25
+
26
+ /** 用户主目录:Windows 上没有 HOME,只有 USERPROFILE。 */
27
+ function homeDir() {
28
+ return process.env.USERPROFILE || process.env.HOME || '/root'
29
+ }
30
+
31
+ /**
32
+ * 打包好的 zip 复制到哪:系统的「文件/资源管理器」里一眼能找到的地方。
33
+ * 手机是 DSHBox(用户自己建的共享目录),电脑是 Downloads。
34
+ */
35
+ function sharedDir(os) {
36
+ const name = osName(os)
37
+ if (name === 'win32') return path.join(homeDir(), 'Downloads')
38
+ if (name === 'darwin') return path.join(homeDir(), 'Downloads')
39
+ return ANDROID_SHARED_DIR
40
+ }
41
+
42
+ /** 给人看的那个路径:对外说「去这儿找」,不能把 proot 里的 /root/share 说给电脑用户听。 */
43
+ function sharedLabel(os, dir) {
44
+ const name = osName(os)
45
+ if (name === 'win32' || name === 'darwin') return dir || sharedDir(os)
46
+ return ANDROID_SHARED_LABEL
47
+ }
48
+
49
+ /** 这个平台上到底有没有那个共享目录(电脑上可能没有 Downloads 这个叫法的目录)。 */
50
+ function sharedExists(os) {
51
+ try {
52
+ return fs.existsSync(sharedDir(os))
53
+ } catch {
54
+ return false
55
+ }
56
+ }
57
+
58
+ /**
59
+ * 面板上该怎么说这个包去哪了。
60
+ * 手机:「能放进手机的 DSHBox」;电脑:「下载文件夹」。一句话,两边都读得懂。
61
+ */
62
+ function describe(os) {
63
+ const name = osName(os)
64
+ if (name === 'win32' || name === 'darwin') {
65
+ return { os: name, kind: 'desktop', dir: sharedDir(os), label: sharedLabel(os), exists: sharedExists(os), word: '下载文件夹' }
66
+ }
67
+ return { os: name, kind: 'phone', dir: sharedDir(os), label: sharedLabel(os), exists: sharedExists(os), word: '手机的 DSHBox' }
68
+ }
69
+
70
+ module.exports = {
71
+ ANDROID_SHARED_DIR,
72
+ ANDROID_SHARED_LABEL,
73
+ osName,
74
+ homeDir,
75
+ sharedDir,
76
+ sharedLabel,
77
+ sharedExists,
78
+ describe
79
+ }
package/lib/retention.js CHANGED
@@ -4,7 +4,7 @@
4
4
  * 索引保留:给索引一个不删的出口。
5
5
  *
6
6
  * 为什么要有这个文件:索引不能无限膨胀,但**也不能删**。
7
- * 哪怕是想忘掉的记忆,也是痕迹,不该被删。
7
+ * 主人 2026-10-26 定的:哪怕是想忘掉的记忆,也是痕迹,不该被删。
8
8
  * 所以这里只做一件事——归档:超过 `retention.keepDays` 的按天压成
9
9
  * archive/<日期>.jsonl.zst,从 index.jsonl 移走。只是换个地方躺着,字节一个不少。
10
10
  *
package/lib/store.js CHANGED
@@ -19,9 +19,6 @@ const { withLock } = require('./lock')
19
19
  const { isGoodRow } = require('./rows')
20
20
 
21
21
  const ZSTD_MAGIC = [0x28, 0xb5, 0x2f, 0xfd]
22
-
23
- /** 当前 Node 能不能解 zstd(会话文件就是这个格式)。读不了就别装作能读。 */
24
- function zstdSupported() { return typeof zlib.zstdDecompressSync === 'function' }
25
22
  const EXCERPT = 200
26
23
 
27
24
  /** 结构化敏感信息:手机号、身份证号。命中的行只留在本机,永不进注入。 */
@@ -163,15 +160,6 @@ let backfillBusy = false
163
160
 
164
161
  function backfill(options = {}) {
165
162
  if (backfillBusy) return { files: 0, scanned: 0, added: 0, skipped: 'busy' }
166
- if (!zstdSupported()) {
167
- return {
168
- files: 0,
169
- scanned: 0,
170
- added: 0,
171
- problems: [],
172
- error: `当前 Node ${process.versions.node} 不支持 zstd(需要 ≥ 22.15)。会话文件读不了,索引与仓库都不会更新。`
173
- }
174
- }
175
163
  backfillBusy = true
176
164
  try {
177
165
  /*
@@ -385,4 +373,4 @@ function remarkSensitive() {
385
373
  return { marked, cleared }
386
374
  }
387
375
 
388
- module.exports = { zstdSupported, indexForDay, backfill, readIndex, readOriginal, messagesFor, sessionFiles, readSessionFile, textOf, readState, remarkSensitive, isSensitive }
376
+ module.exports = { indexForDay, backfill, readIndex, readOriginal, messagesFor, sessionFiles, readSessionFile, textOf, readState, remarkSensitive, isSensitive }