@ssamurias/dsh-fish 1.0.7 → 1.0.9

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.
Files changed (77) hide show
  1. package/AGENTS.md +32 -3
  2. package/CHANGELOG.md +4366 -4265
  3. package/README.md +243 -627
  4. package/RELEASE-NOTES-1.0.8.md +81 -0
  5. package/RELEASE-NOTES-1.0.9.md +79 -0
  6. package/THIRD-PARTY-NOTICES.md +1 -1
  7. package/docs/troubleshooting.md +79 -1
  8. package/docs//350/256/244/344/272/272/345/210/244/346/215/256.md +112 -0
  9. package/lib/appoint.js +106 -22
  10. package/lib/ask.js +8 -3
  11. package/lib/at.js +16 -1
  12. package/lib/capture.js +51 -1
  13. package/lib/claim.js +103 -0
  14. package/lib/client/00-shell.js +500 -0
  15. package/lib/client/01-state-load.js +860 -0
  16. package/lib/client/02-actions.js +670 -0
  17. package/lib/client/03-widgets-header.js +721 -0
  18. package/lib/client/04-pressure-lab.js +891 -0
  19. package/lib/client/05-pressure-charts.js +431 -0
  20. package/lib/client/06-tab-status-grow.js +468 -0
  21. package/lib/client/07-tab-home-bili.js +542 -0
  22. package/lib/client/08-tab-memory-plugins.js +733 -0
  23. package/lib/client/09-tab-outing.js +710 -0
  24. package/lib/client/10-data-handlers.js +519 -0
  25. package/lib/client/11-tab-data-frame.js +661 -0
  26. package/lib/client/12-pressure-chip-mount.js +565 -0
  27. package/lib/client.js +109 -24
  28. package/lib/comment.js +18 -3
  29. package/lib/cue.js +102 -5
  30. package/lib/deliver.js +3 -1
  31. package/lib/derive.js +308 -41
  32. package/lib/doing.js +31 -15
  33. package/lib/drive.js +3 -5
  34. package/lib/env.d.ts +224 -0
  35. package/lib/health.js +56 -0
  36. package/lib/hear.js +70 -0
  37. package/lib/home.js +48 -20
  38. package/lib/identity.js +25 -2
  39. package/lib/index.js +32 -18
  40. package/lib/look.js +2 -1
  41. package/lib/loops.js +39 -0
  42. package/lib/make.js +60 -12
  43. package/lib/markup.js +77 -1
  44. package/lib/match.js +262 -0
  45. package/lib/observe-bili.js +331 -0
  46. package/lib/observe-config.js +223 -0
  47. package/lib/observe-data.js +522 -0
  48. package/lib/observe-home.js +383 -0
  49. package/lib/observe-http.js +250 -0
  50. package/lib/observe-outing.js +650 -0
  51. package/lib/observe-state.js +419 -0
  52. package/lib/observe-trip.js +286 -0
  53. package/lib/observe.js +99 -3574
  54. package/lib/persona.js +187 -40
  55. package/lib/proactive.js +48 -47
  56. package/lib/pronounask.js +15 -1
  57. package/lib/restrict.js +146 -9
  58. package/lib/say.js +68 -4
  59. package/lib/scheduler.js +63 -25
  60. package/lib/schema.js +57 -4
  61. package/lib/scrub.js +16 -1
  62. package/lib/settings.js +128 -12
  63. package/lib/stance.js +131 -19
  64. package/lib/status.js +14 -5
  65. package/lib/store.js +24 -2
  66. package/lib/sub.js +7 -1
  67. package/lib/talkmem.js +12 -8
  68. package/lib/transcript.js +205 -0
  69. package/lib/turns.js +147 -19
  70. package/lib/types.d.ts +513 -0
  71. package/lib/voices.js +26 -1
  72. package/lib/wiki.js +47 -13
  73. package/lib/works.js +200 -49
  74. package/media/banner.jpg +0 -0
  75. package/package.json +1 -1
  76. package/scripts/build-client.mjs +83 -0
  77. package/scripts/sync-pressure-formulas.mjs +12 -15
@@ -0,0 +1,286 @@
1
+ // @ts-check
2
+ /**
3
+ * observe-trip.js —— 出门 / 请神的台账结算、复核与会话恢复逻辑
4
+ *
5
+ * 从 observe.js 拆出:供 /outing 与 /possession 共用,并保持对单测的命名导出。
6
+ */
7
+
8
+ import { join } from 'node:path'
9
+ import { readEvents, appendEvent } from './store.js'
10
+ import { loadConfig } from './settings.js'
11
+ import { deriveSelf } from './derive.js'
12
+ import { validateEvent } from './schema.js'
13
+ import { verifyEvents, passesReview, tripEvents, rejectNoteText, reviewPlan } from './outing.js'
14
+ import { refreshSelfDoc } from './selfdoc.js'
15
+ import { readHomeRecord } from './retire.js'
16
+ import { liveAgentOf } from './identity.js'
17
+ import { appendNote, queueNote } from './sessionnote.js'
18
+ import { logLine } from './log.js'
19
+
20
+ /**
21
+ * 把一串事件 id 按 `prefix:id id …` 拆成几行,每行 ≤1800 字(issue #68 / PR #80)。
22
+ *
23
+ * @param {string} prefix
24
+ * @param {Iterable<unknown> | null | undefined} ids
25
+ * @returns {string[]}
26
+ */
27
+ export function metaIdLines(prefix, ids) {
28
+ const lines = []
29
+ let cur = ''
30
+ for (const raw of ids ?? []) {
31
+ const id = String(raw ?? '').trim()
32
+ if (!id) continue
33
+ if (!cur) { cur = prefix + id; continue }
34
+ if (cur.length + 1 + id.length > 1800) { lines.push(cur); cur = prefix + id; continue }
35
+ cur += ' ' + id
36
+ }
37
+ if (cur) lines.push(cur)
38
+ return lines
39
+ }
40
+
41
+ /**
42
+ * 把一串 id 拆成几行 `prefix id id …` 追加进事件流(出门 / 请神的结算标记共用)。
43
+ *
44
+ * @param {string} root
45
+ * @param {string} prefix
46
+ * @param {string[]} ids
47
+ * @returns {Promise<{ ok: boolean, lines: number, total: number, why?: string }>}
48
+ */
49
+ export async function appendIdMarks(root, prefix, ids) {
50
+ const lines = metaIdLines(prefix, ids)
51
+ if (!lines.length) return { ok: true, lines: 0, total: 0 }
52
+ let written = 0
53
+ try {
54
+ const { events } = await readEvents(root)
55
+ const { config } = await loadConfig(root)
56
+ const m = deriveSelf(events, { growthDays: config.growthDays })
57
+ /** @type {import('./types.js').ValidateContext} */
58
+ const ectx = { day: m.day, stage: m.stage, source: 'system' }
59
+ for (const text of lines) {
60
+ await appendEvent(validateEvent({ type: 'meta', text, day: m.day, stage: m.stage }, ectx), ectx, root)
61
+ written += 1
62
+ }
63
+ return { ok: true, lines: written, total: lines.length }
64
+ } catch (err) {
65
+ const why = String(/** @type {any} */ (err)?.message ?? err).slice(0, 120)
66
+ logLine('warn', ['标记没写进事件流', prefix, `${written}/${lines.length}`, why], root)
67
+ return { ok: false, lines: written, total: lines.length, why }
68
+ }
69
+ }
70
+
71
+ /**
72
+ * 出门 / 请神回家那一刻的结算,两条路共用(issue #68)。
73
+ *
74
+ * @param {string} root
75
+ * @param {'outing'|'possess'} prefix
76
+ * @param {any} rec 出门或请神那条 active 台账
77
+ * @param {{ kept?: boolean, rejectedIds?: unknown[] }} b 请求体
78
+ */
79
+ export async function settleTripEnd(root, prefix, rec, b) {
80
+ const file = join(root, 'events.jsonl')
81
+ const v = verifyEvents(file, rec?.baseline)
82
+ const tampered = !passesReview(v)
83
+ const tripIds = tripEvents(file, rec).items.map((it) => String(it.id ?? '')).filter(Boolean)
84
+ const inTrip = new Set(tripIds)
85
+ let rejectedIds = [...new Set((Array.isArray(b?.rejectedIds) ? b.rejectedIds : []).map((x) => String(x ?? '')))]
86
+ .filter((id) => inTrip.has(id))
87
+ let autoRejected = false
88
+ if (!rejectedIds.length && b?.kept === false) {
89
+ rejectedIds = tripIds
90
+ autoRejected = true
91
+ }
92
+ const pendingRejectedIds = tampered ? tripIds : []
93
+ const marked = await appendIdMarks(root, `${prefix}-reject:`, tampered ? tripIds : rejectedIds)
94
+ return { v, tampered, rejectedIds, pendingRejectedIds, autoRejected, marked }
95
+ }
96
+
97
+ /**
98
+ * 回家和复核按数据目录排队,一次只结一趟(issue #68)。
99
+ * @type {Map<string, Promise<void>>}
100
+ */
101
+ const settleQueues = new Map()
102
+
103
+ /**
104
+ * @template T
105
+ * @param {string} root
106
+ * @param {() => Promise<T>} fn
107
+ * @returns {Promise<T>}
108
+ */
109
+ export function settleSerial(root, fn) {
110
+ const key = String(root)
111
+ const prev = settleQueues.get(key) ?? Promise.resolve()
112
+ const run = prev.then(fn)
113
+ const tail = run.then(() => {}, () => {})
114
+ settleQueues.set(key, tail)
115
+ void tail.then(() => { if (settleQueues.get(key) === tail) settleQueues.delete(key) })
116
+ return run
117
+ }
118
+
119
+ /**
120
+ * 台账给界面看的样子:两份 id 名单换成条数(issue #68)。
121
+ * @param {any} rec
122
+ */
123
+ export function ledgerBrief(rec) {
124
+ if (!rec || typeof rec !== 'object') return rec
125
+ const { rejectedIds, pendingRejectedIds, ...rest } = rec
126
+ if (rec.status === 'pending') {
127
+ const plan = reviewPlan(rec)
128
+ return { ...rest, dropped: plan.dropped.length, held: plan.held }
129
+ }
130
+ return {
131
+ ...rest,
132
+ dropped: Array.isArray(rejectedIds) ? rejectedIds.length : 0,
133
+ held: Array.isArray(pendingRejectedIds) ? pendingRejectedIds.length : 0,
134
+ }
135
+ }
136
+
137
+ /**
138
+ * 「还没算数」卡要的几样(出门、请神共用)。
139
+ * @param {any} pend 等复核的那条台账
140
+ * @param {any} pv 现在比对的结果(verifyEvents)
141
+ */
142
+ export function pendingCard(pend, pv) {
143
+ const plan = reviewPlan(pend)
144
+ const okNow = passesReview(pv)
145
+ const unmarked = pend.marked === false
146
+ return {
147
+ startedAt: Number(pend.startedAt) || 0,
148
+ endedAt: Number(pend.endedAt) || 0,
149
+ checkable: pv.checkable !== false,
150
+ okNow,
151
+ reason: unmarked && okNow ? '回家时「不算」的标记没写进去' : (pv.why || (okNow ? '' : '文件对不上')),
152
+ appended: Number(pv.appended ?? 0),
153
+ held: plan.held,
154
+ dropped: plan.dropped.length,
155
+ autoRejected: pend.autoRejected === true,
156
+ unmarked,
157
+ }
158
+ }
159
+
160
+ /**
161
+ * 复核通过之后的结算,两条路共用(issue #68)。
162
+ *
163
+ * @param {import('./types.js').HostContext} ctx
164
+ * @param {string} root
165
+ * @param {'outing'|'possess'} prefix
166
+ * @param {any} pend
167
+ * @param {(root: string, opts: any) => Promise<any>} settle
168
+ */
169
+ export async function settleTripReview(ctx, root, prefix, pend, settle) {
170
+ const plan = reviewPlan(pend)
171
+ if (plan.remark && plan.dropped.length) {
172
+ const re = await appendIdMarks(root, `${prefix}-reject:`, plan.dropped)
173
+ if (!re.ok) return { ok: false, code: 500, why: `「不算」的标记没写进去,这趟还等着核对:${re.why}` }
174
+ }
175
+ const marked = await appendIdMarks(root, `${prefix}-keep:`, plan.keep)
176
+ if (!marked.ok) return { ok: false, code: 500, why: `捞回来的标记没写进去,这趟还等着核对:${marked.why}` }
177
+ const rejectAll = pend.autoRejected === true
178
+ const r = await settle(root, {
179
+ sessionId: pend.sessionId,
180
+ startedAt: pend.startedAt,
181
+ status: rejectAll ? 'rejected' : 'done',
182
+ kept: !rejectAll,
183
+ rejectedIds: plan.dropped,
184
+ marked: true,
185
+ })
186
+ if (!r.ok) return { ok: false, code: 409, why: r.why }
187
+ await refreshSelfDoc(root).catch(() => {})
188
+ /** @type {{ ok: boolean, why?: string, delivered?: boolean, queued?: boolean }} */
189
+ let note = { ok: false, why: '这趟没有要驳回的' }
190
+ if (plan.dropped.length) note = await rejectNoteToHome(ctx, root, { trip: pend, rejectedIds: plan.dropped, auto: rejectAll })
191
+ return { ok: true, settled: r, note, kept: plan.keep.length, dropped: plan.dropped.length }
192
+ }
193
+
194
+ /**
195
+ * 复核请求带着卡上那一趟的 sessionId / startedAt 时,跟这次取到的那趟对一下(issue #68)。
196
+ * @param {any} pend
197
+ * @param {any} b
198
+ * @returns {string}
199
+ */
200
+ export function reviewTargetMismatch(pend, b) {
201
+ if (b?.sessionId && String(pend.sessionId) !== String(b.sessionId)) return '等着核对的不是这一趟'
202
+ if (b?.startedAt !== undefined && b?.startedAt !== null && b?.startedAt !== '' &&
203
+ Number(pend.startedAt) !== Number(b.startedAt)) {
204
+ return '等着核对的不是卡上那一趟(可能另一个窗口刚核对过),刷新后再看'
205
+ }
206
+ return ''
207
+ }
208
+
209
+ /**
210
+ * 把一条已经存在的会话恢复成活的 agent(0.6.27,用户 m03372)。
211
+ * @param {any} sc
212
+ * @param {string} sessionId
213
+ */
214
+ export async function resolveSessionAgent(sc, sessionId) {
215
+ const got = typeof sc?.resolveAgent === 'function'
216
+ ? await sc.resolveAgent(sessionId)
217
+ : (typeof sc?.agents?.resolveAgent === 'function' ? await sc.agents.resolveAgent(sessionId) : null)
218
+ if (got === null) throw new Error('宿主没有 sessionController.resolveAgent,接不回上次那条会话')
219
+ if (got?.error) throw got.error
220
+ if (!got?.agent) throw new Error('宿主说恢复好了,但没把那条会话的 agent 交出来')
221
+ return got.agent
222
+ }
223
+
224
+ /**
225
+ * 驳回要讲给她听(设计 §3.7):写进**家里**那条会话;没开着就排队。
226
+ *
227
+ * @param {import('./types.js').HostContext} ctx
228
+ * @param {string} root
229
+ * @param {{ trip?: any, rejectedIds?: string[], auto?: boolean }} [opts]
230
+ * @returns {Promise<{ ok: boolean, why?: string, delivered?: boolean, queued?: boolean }>}
231
+ */
232
+ export async function rejectNoteToHome(ctx, root, { trip = null, rejectedIds = [], auto = false } = {}) {
233
+ if (!trip?.startedAt || !Array.isArray(rejectedIds) || !rejectedIds.length) {
234
+ return { ok: false, why: '这趟没有要驳回的' }
235
+ }
236
+ try {
237
+ const want = new Set(rejectedIds.map((x) => String(x)))
238
+ const picked = tripEvents(join(root, 'events.jsonl'), trip)
239
+ .items.filter((it) => want.has(it.id))
240
+ const text = rejectNoteText(/** @type {any} */ ({ items: picked, total: rejectedIds.length, auto }))
241
+ if (!text) return { ok: false, why: '没有要驳回的' }
242
+ const rec = await readHomeRecord(root).catch(() => null)
243
+ const homeAgent = liveAgentOf(ctx, rec?.sessionId || '')
244
+ if (homeAgent) {
245
+ const w = appendNote(homeAgent, text)
246
+ return { ok: w.ok, why: w.why ?? '', delivered: Boolean(w.ok) }
247
+ }
248
+ const q = await queueNote(root, text)
249
+ return { ok: q.ok, why: q.why ?? '', delivered: false, queued: Boolean(q.ok) }
250
+ } catch (err) {
251
+ return { ok: false, why: String(/** @type {any} */ (err)?.message ?? err).slice(0, 120) }
252
+ }
253
+ }
254
+
255
+ /**
256
+ * 比对路径用:Windows 大小写不敏感。只比对,不做文件访问。
257
+ * @param {unknown} p
258
+ */
259
+ export function pathKey(p) {
260
+ const s = String(p ?? '').replace(/\\/g, '/').replace(/\/+$/, '')
261
+ return process.platform === 'win32' ? s.toLowerCase() : s
262
+ }
263
+
264
+ /**
265
+ * 目标工作区是不是本插件的源码目录。
266
+ * @param {unknown} p
267
+ */
268
+ export function isPluginSource(p) {
269
+ const k = pathKey(p)
270
+ if (!k) return false
271
+ return k.includes('/大肥鱼') || k.includes('/fish-plugin') || k.includes('/plugin')
272
+ }
273
+
274
+ /**
275
+ * 出门首轮的那句话。只交代在哪、能用什么、别动什么。
276
+ * @param {string} where
277
+ */
278
+ export function outingWakeText(where) {
279
+ return [
280
+ `你出门了。这里是「${where}」的地盘,不是你自己的屋子。`,
281
+ '这趟可以放开用手边的工具做事,做完再回去。',
282
+ '只有一件:我自己屋子里的文件和记录不要动——那是家里的账本和家谱,不是这趟该碰的。',
283
+ '工具不是屋子里的东西。这条对话给我什么,我就用什么;用它写下的东西照样记在我自己的账上,那是应该的,不是越界。',
284
+ '有人会在这里跟你说话。等家里人叫你回去,你就回去。',
285
+ ].join('\n')
286
+ }