@ssamurias/dsh-fish 1.0.8 → 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.
- package/AGENTS.md +32 -3
- package/CHANGELOG.md +4366 -4304
- package/README.md +22 -9
- package/RELEASE-NOTES-1.0.9.md +79 -0
- package/docs//350/256/244/344/272/272/345/210/244/346/215/256.md +112 -0
- package/lib/appoint.js +106 -22
- package/lib/ask.js +2 -1
- package/lib/capture.js +16 -0
- package/lib/claim.js +103 -0
- package/lib/client/00-shell.js +500 -0
- package/lib/client/01-state-load.js +860 -0
- package/lib/client/02-actions.js +670 -0
- package/lib/client/03-widgets-header.js +721 -0
- package/lib/client/04-pressure-lab.js +891 -0
- package/lib/client/05-pressure-charts.js +431 -0
- package/lib/client/06-tab-status-grow.js +468 -0
- package/lib/client/07-tab-home-bili.js +542 -0
- package/lib/client/08-tab-memory-plugins.js +733 -0
- package/lib/client/09-tab-outing.js +710 -0
- package/lib/client/10-data-handlers.js +519 -0
- package/lib/client/11-tab-data-frame.js +661 -0
- package/lib/client/12-pressure-chip-mount.js +565 -0
- package/lib/client.js +83 -3
- package/lib/comment.js +18 -3
- package/lib/derive.js +197 -19
- package/lib/doing.js +31 -15
- package/lib/env.d.ts +224 -0
- package/lib/health.js +56 -0
- package/lib/hear.js +67 -0
- package/lib/home.js +12 -1
- package/lib/identity.js +23 -0
- package/lib/index.js +27 -17
- package/lib/look.js +2 -1
- package/lib/make.js +60 -12
- package/lib/markup.js +61 -23
- package/lib/observe-bili.js +331 -0
- package/lib/observe-config.js +223 -0
- package/lib/observe-data.js +522 -0
- package/lib/observe-home.js +383 -0
- package/lib/observe-http.js +250 -0
- package/lib/observe-outing.js +650 -0
- package/lib/observe-state.js +419 -0
- package/lib/observe-trip.js +286 -0
- package/lib/observe.js +99 -3574
- package/lib/persona.js +149 -23
- package/lib/restrict.js +21 -1
- package/lib/say.js +48 -0
- package/lib/scheduler.js +23 -3
- package/lib/schema.js +11 -4
- package/lib/scrub.js +3 -3
- package/lib/settings.js +104 -7
- package/lib/stance.js +41 -3
- package/lib/status.js +14 -5
- package/lib/turns.js +51 -4
- package/lib/types.d.ts +513 -0
- package/lib/voices.js +3 -0
- package/lib/wiki.js +47 -13
- package/lib/works.js +120 -45
- package/package.json +1 -1
- package/scripts/build-client.mjs +83 -0
- package/scripts/sync-pressure-formulas.mjs +12 -15
|
@@ -0,0 +1,419 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/**
|
|
3
|
+
* observe-state.js —— 观测台状态、认知、日记、作品、记忆与日报只读路由
|
|
4
|
+
*
|
|
5
|
+
* 路由:
|
|
6
|
+
* /session-pressure · /status · /state · /events · /beliefs · /diary
|
|
7
|
+
* /works · /works-open · /work-file · /memory · /health · /reports · /templates
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import fs from 'node:fs'
|
|
11
|
+
import { resolve, sep } from 'node:path'
|
|
12
|
+
import { readEvents, paths, readDaily, listDiary, diaryDatesWritten } from './store.js'
|
|
13
|
+
import { loadConfig, dateKey } from './settings.js'
|
|
14
|
+
import { selfPronounOf, fillSelf } from './selfpronoun.js'
|
|
15
|
+
import { deriveSelf, consistency, novelty, gateNeed } from './derive.js'
|
|
16
|
+
import { examine } from './health.js'
|
|
17
|
+
import { countEvents, isNoiseMeta } from './meta.js'
|
|
18
|
+
import { stability, stabilityBand } from './quality.js'
|
|
19
|
+
import { firedLog, CATALOG } from './events.js'
|
|
20
|
+
import { listReports, buildReport } from './report.js'
|
|
21
|
+
import { listWorks, worksSummary, readWorkText, worksDir, WORK_LIST_MAX } from './works.js'
|
|
22
|
+
import { seenOn } from './whatseen.js'
|
|
23
|
+
import { awayNow, awayLine, tripDays } from './outing.js'
|
|
24
|
+
import { homeStatus } from './home.js'
|
|
25
|
+
import { selfDocStatus } from './selfdoc.js'
|
|
26
|
+
import { personaBudget, BELIEF_SHOWN, shownCount, keepCount } from './persona.js'
|
|
27
|
+
import { buildStances, gatesDone, stanceSummary } from './stance.js'
|
|
28
|
+
import { ruleById } from './rules.js'
|
|
29
|
+
import { ruleStatus, pendingOffer, offerLimits, activeGrants } from './unlock.js'
|
|
30
|
+
import { deriveStatus } from './status.js'
|
|
31
|
+
import { normalizeVersion } from './update.js'
|
|
32
|
+
import { send, readBody } from './observe-http.js'
|
|
33
|
+
|
|
34
|
+
/** @type {Record<string, 'belief' | 'refuse' | 'revise'>} */
|
|
35
|
+
const FORCED_GATE_MAP = { trust: 'belief', autonomy: 'refuse', competence: 'revise' }
|
|
36
|
+
|
|
37
|
+
/** @type {Record<string, string>} */
|
|
38
|
+
const WORK_CONTENT_TYPES = {
|
|
39
|
+
png: 'image/png', jpg: 'image/jpeg', jpeg: 'image/jpeg', webp: 'image/webp', gif: 'image/gif',
|
|
40
|
+
avif: 'image/avif', bmp: 'image/bmp', svg: 'image/svg+xml',
|
|
41
|
+
mp3: 'audio/mpeg', wav: 'audio/wav', m4a: 'audio/mp4', ogg: 'audio/ogg', flac: 'audio/flac',
|
|
42
|
+
mp4: 'video/mp4', webm: 'video/webm', mov: 'video/quicktime',
|
|
43
|
+
txt: 'text/plain; charset=utf-8', md: 'text/plain; charset=utf-8',
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @param {import('./types.js').ObserveEnv} env
|
|
48
|
+
* @returns {import('./types.js').ObserveRouteEntry[]}
|
|
49
|
+
*/
|
|
50
|
+
export function buildStateRoutes(env) {
|
|
51
|
+
const { root, deps, pressure, openFolder } = env
|
|
52
|
+
|
|
53
|
+
return [
|
|
54
|
+
['/session-pressure', async (req, res) => {
|
|
55
|
+
if (req.method === 'POST') {
|
|
56
|
+
let body
|
|
57
|
+
try { body = JSON.parse(await readBody(req)) } catch { return send(res, 400, { ok: false }) }
|
|
58
|
+
const ok = await pressure.select(body?.id, body?.selected)
|
|
59
|
+
return send(res, ok ? 200 : 400, { ok })
|
|
60
|
+
}
|
|
61
|
+
if (req.method !== 'GET') return send(res, 405, { ok: false })
|
|
62
|
+
const options = new URL(req.url ?? '/', 'http://x').searchParams
|
|
63
|
+
if (options.has('trend')) {
|
|
64
|
+
const result = await pressure.trend(options.get('trend') ?? '', options.get('diagnostics') === '1')
|
|
65
|
+
return send(res, result ? 200 : 404, result ?? { ok: false })
|
|
66
|
+
}
|
|
67
|
+
if (options.get('wait') === '1') await pressure.waitForIdle()
|
|
68
|
+
return send(res, 200, await pressure.view(options.get('composite') === '1', /** @type {any} */ (options.get('session') || null), options.get('light') === '1'))
|
|
69
|
+
}],
|
|
70
|
+
|
|
71
|
+
['/status', async (_req, res) => {
|
|
72
|
+
const { events } = await readEvents(root)
|
|
73
|
+
const { config } = await loadConfig(root)
|
|
74
|
+
const st = deriveStatus({ events, config, now: new Date() })
|
|
75
|
+
let away = null
|
|
76
|
+
try {
|
|
77
|
+
const a = await awayNow(root)
|
|
78
|
+
if (a?.kind) {
|
|
79
|
+
const rec = a.kind === 'outing' ? a.outing : a.possession
|
|
80
|
+
away = { kind: a.kind, since: rec?.startedAt, line: awayLine(a), tripDays: a.kind === 'outing' ? tripDays(a.outing, { now: Date.now() }) : 0 }
|
|
81
|
+
}
|
|
82
|
+
} catch { /* 读台账失败不影响状态本身 */ }
|
|
83
|
+
/** @type {import('./types.js').ObserveStatusPayload} */
|
|
84
|
+
const payload = { ...st, away, running: true }
|
|
85
|
+
send(res, 200, payload)
|
|
86
|
+
}],
|
|
87
|
+
|
|
88
|
+
['/state', async (_req, res) => {
|
|
89
|
+
const { events, skipped } = await readEvents(root)
|
|
90
|
+
const { config, fresh, broken } = await loadConfig(root)
|
|
91
|
+
const m = deriveSelf(events, { growthDays: config.growthDays })
|
|
92
|
+
const need = gateNeed(m.growthDays)
|
|
93
|
+
/** @type {import('./types.js').ObserveStatePayload} */
|
|
94
|
+
const payload = {
|
|
95
|
+
day: m.day,
|
|
96
|
+
growthDays: m.growthDays,
|
|
97
|
+
dev: true,
|
|
98
|
+
stage: m.stage,
|
|
99
|
+
stageName: m.stageName,
|
|
100
|
+
stagePeak: m.stagePeak,
|
|
101
|
+
stageForced: m.stageForced,
|
|
102
|
+
pendingGate: m.stageGate?.pending ?? null,
|
|
103
|
+
gateTimes: { belief: need.trust, refuse: need.autonomy, revise: need.competence },
|
|
104
|
+
forcedGates: (m.stageGate?.forced ?? [])
|
|
105
|
+
.map((g) => FORCED_GATE_MAP[g])
|
|
106
|
+
.filter(Boolean),
|
|
107
|
+
name: m.name,
|
|
108
|
+
beliefCount: m.beliefCount,
|
|
109
|
+
absenceDays: m.absenceDays,
|
|
110
|
+
tension: m.tensions?.[0]
|
|
111
|
+
? { count: m.tensionsTotal ?? m.tensions.length, a: m.tensions[0].a.text, b: m.tensions[0].b.text }
|
|
112
|
+
: null,
|
|
113
|
+
solidCount: m.solidTotal ?? m.solid?.length ?? 0,
|
|
114
|
+
scarCount: m.scars?.length ?? 0,
|
|
115
|
+
stanceCount: buildStances(m).length,
|
|
116
|
+
stanceReady: gatesDone(m),
|
|
117
|
+
counts: (() => {
|
|
118
|
+
const c = countEvents(events)
|
|
119
|
+
return {
|
|
120
|
+
utterance: events.filter((e) => e.type === 'utterance').length,
|
|
121
|
+
chat: events.filter(
|
|
122
|
+
(e) => e.type === 'utterance' && Array.isArray(e.flags) && e.flags.includes('uncommitted'),
|
|
123
|
+
).length,
|
|
124
|
+
revise: events.filter((e) => e.type === 'revise').length,
|
|
125
|
+
refusal: events.filter((e) => e.type === 'refusal').length,
|
|
126
|
+
reunion: events.filter((e) => e.type === 'reunion').length,
|
|
127
|
+
total: c.real,
|
|
128
|
+
all: c.all,
|
|
129
|
+
meta: c.meta,
|
|
130
|
+
noise: c.noise,
|
|
131
|
+
}
|
|
132
|
+
})(),
|
|
133
|
+
quality: (() => {
|
|
134
|
+
const st = stability(events, { growthDays: config.growthDays }, {
|
|
135
|
+
shown: config?.memory?.beliefsShown,
|
|
136
|
+
budget: config?.memory?.docBudget,
|
|
137
|
+
withBans: m?.persona?.imported !== true,
|
|
138
|
+
sleepKeep: config?.memory?.sleepKeep,
|
|
139
|
+
})
|
|
140
|
+
return { stability: { ...st, band: stabilityBand(st) }, consistency: consistency(events), novelty: novelty(events) }
|
|
141
|
+
})(),
|
|
142
|
+
rules: ruleStatus(events, m, Date.now(), offerLimits(config)),
|
|
143
|
+
pending: (() => {
|
|
144
|
+
const p = pendingOffer(events, Date.now(), offerLimits(config))
|
|
145
|
+
if (!p) return null
|
|
146
|
+
const r = ruleById(p.grant?.rule)
|
|
147
|
+
return { id: p.id, rule: p.grant?.rule, title: r?.title ?? '', text: p.text ?? '', day: p.day ?? null }
|
|
148
|
+
})(),
|
|
149
|
+
grants: activeGrants(events, Date.now()),
|
|
150
|
+
config: { window: config.window, daily: config.daily, diary: { enabled: config.diary.enabled, visible: config.diary.visible }, absenceAfterDays: config.absenceAfterDays, proactive: config.proactive, self: { pronoun: selfPronounOf(config) } },
|
|
151
|
+
meta: { root, skippedLines: skipped, configFresh: fresh, configBroken: broken, dateKey: dateKey(), version: normalizeVersion(deps.version) || String(deps.version ?? '') },
|
|
152
|
+
tenancy: (() => {
|
|
153
|
+
const t = deps.tenancy
|
|
154
|
+
if (!t) return null
|
|
155
|
+
return {
|
|
156
|
+
ok: t.tenant?.ok !== false,
|
|
157
|
+
instanceId: String(t.tenant?.instanceId ?? ''),
|
|
158
|
+
why: String(t.tenant?.why ?? ''),
|
|
159
|
+
dataDir: String(t.tenant?.owner?.dataDir ?? t.root ?? root ?? ''),
|
|
160
|
+
}
|
|
161
|
+
})(),
|
|
162
|
+
homes: (() => {
|
|
163
|
+
const h = deps.tenancy?.homes
|
|
164
|
+
if (!h) return null
|
|
165
|
+
return { changed: h.changed === true, from: String(h.from ?? ''), to: String(h.to ?? '') }
|
|
166
|
+
})(),
|
|
167
|
+
home: {
|
|
168
|
+
...(await homeStatus(root)),
|
|
169
|
+
blocked: deps.tenancy?.home?.blocked === true,
|
|
170
|
+
blockedWhy: deps.tenancy?.home?.blocked === true ? String(deps.tenancy?.home?.why ?? '') : '',
|
|
171
|
+
},
|
|
172
|
+
compat: {
|
|
173
|
+
checked: deps.compat?.checked === true,
|
|
174
|
+
userQuestions: deps.compat?.userQuestions !== false,
|
|
175
|
+
},
|
|
176
|
+
host: (() => {
|
|
177
|
+
const h = deps.install?.host
|
|
178
|
+
if (!h) return null
|
|
179
|
+
return {
|
|
180
|
+
ok: h.ok === true,
|
|
181
|
+
status: String(h.status ?? 'unknown'),
|
|
182
|
+
version: String(h.version ?? ''),
|
|
183
|
+
range: String(h.range ?? ''),
|
|
184
|
+
why: String(h.why ?? ''),
|
|
185
|
+
}
|
|
186
|
+
})(),
|
|
187
|
+
upgrade: (() => {
|
|
188
|
+
const u = deps.install?.upgrade
|
|
189
|
+
if (!u) return null
|
|
190
|
+
return {
|
|
191
|
+
stale: u.stale === true,
|
|
192
|
+
older: u.older === true,
|
|
193
|
+
skipped: u.skipped === true,
|
|
194
|
+
running: String(u.running ?? ''),
|
|
195
|
+
source: String(u.source ?? ''),
|
|
196
|
+
sourcePath: String(u.sourcePath ?? ''),
|
|
197
|
+
profile: String(u.profile ?? ''),
|
|
198
|
+
why: String(u.why ?? ''),
|
|
199
|
+
}
|
|
200
|
+
})(),
|
|
201
|
+
}
|
|
202
|
+
send(res, 200, payload)
|
|
203
|
+
}],
|
|
204
|
+
|
|
205
|
+
['/events', async (req, res) => {
|
|
206
|
+
const { events } = await readEvents(root)
|
|
207
|
+
const url = new URL(req.url ?? '/', 'http://x')
|
|
208
|
+
const limit = Math.min(500, Number(url.searchParams.get('limit') ?? 80) || 80)
|
|
209
|
+
const type = url.searchParams.get('type')
|
|
210
|
+
const withNoise = url.searchParams.get('noise') === '1'
|
|
211
|
+
const rows = events
|
|
212
|
+
.filter((e) => (!type || e.type === type) && (withNoise || !isNoiseMeta(e)))
|
|
213
|
+
.slice(-limit)
|
|
214
|
+
.reverse()
|
|
215
|
+
const c = countEvents(events)
|
|
216
|
+
send(res, 200, { total: c.real, all: c.all, meta: c.meta, noise: c.noise, rows })
|
|
217
|
+
}],
|
|
218
|
+
|
|
219
|
+
['/beliefs', async (_req, res) => {
|
|
220
|
+
const { events } = await readEvents(root)
|
|
221
|
+
const { config } = await loadConfig(root)
|
|
222
|
+
const m = deriveSelf(events, { growthDays: config.growthDays })
|
|
223
|
+
send(res, 200, {
|
|
224
|
+
active: m.beliefs.map((b) => ({ id: b.id, text: b.text, since: b.since, confidence: b.confidence })),
|
|
225
|
+
refuted: m.refuted,
|
|
226
|
+
refusals: m.refusals,
|
|
227
|
+
selfmod: m.selfmodAttempts,
|
|
228
|
+
})
|
|
229
|
+
}],
|
|
230
|
+
|
|
231
|
+
['/diary', async (req, res) => {
|
|
232
|
+
const { config } = await loadConfig(root)
|
|
233
|
+
if (!config.diary.enabled) return send(res, 200, { enabled: false, visible: false, items: [], note: fillSelf('{S}没在写日记', selfPronounOf(config)) })
|
|
234
|
+
const dates = await listDiary(root)
|
|
235
|
+
if (!config.diary.visible) {
|
|
236
|
+
return send(res, 200, { enabled: true, visible: false, items: [], lockedDates: dates, note: '日记已写,但设置为不可见' })
|
|
237
|
+
}
|
|
238
|
+
const url = new URL(req.url ?? '/', 'http://x')
|
|
239
|
+
const day = url.searchParams.get('day')
|
|
240
|
+
if (day) {
|
|
241
|
+
if (!/^\d{4}-\d{2}-\d{2}$/.test(day) || !dates.includes(day)) return send(res, 404, { error: '这天没有日记' })
|
|
242
|
+
return send(res, 200, { date: day, text: await readDaily('diaryDir', day, root) })
|
|
243
|
+
}
|
|
244
|
+
const items = []
|
|
245
|
+
for (const d of dates.slice(0, 1)) items.push({ date: d, text: await readDaily('diaryDir', d, root) })
|
|
246
|
+
send(res, 200, { enabled: true, visible: true, dates, items })
|
|
247
|
+
}],
|
|
248
|
+
|
|
249
|
+
['/works', async (req, res) => {
|
|
250
|
+
if (!req.method || req.method === 'GET') {
|
|
251
|
+
const url = new URL(req.url ?? '/', 'http://x')
|
|
252
|
+
const want = Number(url.searchParams.get('limit') ?? 0)
|
|
253
|
+
const limit = Number.isSafeInteger(want) && want > 0 ? Math.min(want, WORK_LIST_MAX) : WORK_LIST_MAX
|
|
254
|
+
const [items, summary] = await Promise.all([listWorks(root, { limit }), worksSummary(root)])
|
|
255
|
+
return send(res, 200, { items, summary, dir: worksDir(root) })
|
|
256
|
+
}
|
|
257
|
+
if (req.method === 'POST') {
|
|
258
|
+
let body = {}
|
|
259
|
+
try { body = JSON.parse((await readBody(req, 20_000)) || '{}') } catch { return send(res, 400, { error: 'body 不是合法 JSON' }) }
|
|
260
|
+
const id = typeof body.id === 'string' ? body.id : ''
|
|
261
|
+
const title = typeof body.title === 'string' ? body.title : ''
|
|
262
|
+
const version = Number(body.version) || 0
|
|
263
|
+
const got = await readWorkText(root, { id, title, version })
|
|
264
|
+
if (!got.ok) return send(res, 404, { error: got.note || '翻不到这件东西' })
|
|
265
|
+
return send(res, 200, { ok: true, id: got.id, title: got.title, state: got.state, version: got.version, versions: got.versions, kind: got.kind || 'text', file: got.file || '', text: got.text })
|
|
266
|
+
}
|
|
267
|
+
return send(res, 405, { error: '只认 GET 与 POST' })
|
|
268
|
+
}],
|
|
269
|
+
|
|
270
|
+
['/works-open', async (req, res) => {
|
|
271
|
+
if (req.method !== 'POST') return send(res, 405, { error: '只认 POST' })
|
|
272
|
+
const made = worksDir(root)
|
|
273
|
+
let dir = made
|
|
274
|
+
try {
|
|
275
|
+
if (!fs.statSync(made).isDirectory()) dir = paths(root).root
|
|
276
|
+
} catch { dir = paths(root).root }
|
|
277
|
+
if (!(await openFolder(dir))) return send(res, 200, { ok: false, error: '这个环境打不开文件夹,路径就在界面上,手动复制一下就行。' })
|
|
278
|
+
return send(res, 200, { ok: true, dir })
|
|
279
|
+
}],
|
|
280
|
+
|
|
281
|
+
['/work-file', async (req, res) => {
|
|
282
|
+
if (req.method && req.method !== 'GET') return send(res, 405, { error: '只认 GET' })
|
|
283
|
+
const url = new URL(req.url ?? '/', 'http://x')
|
|
284
|
+
const want = String(url.searchParams.get('p') || '')
|
|
285
|
+
const base = resolve(worksDir(root))
|
|
286
|
+
const file = resolve(root, want)
|
|
287
|
+
if (!want || !(file === base || file.startsWith(base + sep))) return send(res, 404, { error: '没有这个文件' })
|
|
288
|
+
let buf
|
|
289
|
+
try { buf = fs.readFileSync(file) } catch { return send(res, 404, { error: '没有这个文件' }) }
|
|
290
|
+
const ext = String(file.split('.').pop() || '').toLowerCase()
|
|
291
|
+
res.writeHead(200, {
|
|
292
|
+
'content-type': WORK_CONTENT_TYPES[ext] || 'application/octet-stream',
|
|
293
|
+
'content-length': buf.length,
|
|
294
|
+
'cache-control': 'private, max-age=86400',
|
|
295
|
+
})
|
|
296
|
+
res.end(buf)
|
|
297
|
+
}],
|
|
298
|
+
|
|
299
|
+
['/memory', async (_req, res) => {
|
|
300
|
+
const { config } = await loadConfig(root)
|
|
301
|
+
const { events } = await readEvents(root)
|
|
302
|
+
const model = deriveSelf(events, { growthDays: config.growthDays })
|
|
303
|
+
const doc = await selfDocStatus(root)
|
|
304
|
+
const h = await homeStatus(root)
|
|
305
|
+
const budget = Number(config?.memory?.docBudget ?? 2800)
|
|
306
|
+
const shownCap = shownCount(config?.memory?.beliefsShown ?? BELIEF_SHOWN)
|
|
307
|
+
const pb = personaBudget(model, { budget, withBans: model?.persona?.imported !== true, shown: shownCap, sleepKeep: config?.memory?.sleepKeep })
|
|
308
|
+
const diary = await listDiary(root).catch(() => [])
|
|
309
|
+
const keepCap = keepCount(config?.memory?.sleepKeep)
|
|
310
|
+
const kept = keepCap === null ? model.beliefs.length : Math.min(model.beliefs.length, Math.max(keepCap, shownCap))
|
|
311
|
+
// 2026-11-23(外部报告):原来这里按**设置**算「眼前几条」——可判断那一段是可选段,
|
|
312
|
+
// 预算紧的时候会被整段挤掉,于是面板说「最显著的 4 条在眼前」,她其实一条也没看到。
|
|
313
|
+
// 现在按装配计划报:那一段没进文档就是 0 条,另给 beliefsKept 让界面改口。
|
|
314
|
+
const laid = pb.beliefs ?? null
|
|
315
|
+
const shown = laid ? laid.lines : 0
|
|
316
|
+
const folded = Math.max(0, model.beliefs.length - shown)
|
|
317
|
+
const lastSleep = [...events].reverse()
|
|
318
|
+
.find((e) => e?.type === 'meta' && String(e.text ?? '').startsWith('sleep:'))
|
|
319
|
+
send(res, 200, {
|
|
320
|
+
selfDoc: { bytes: doc.bytes, chars: pb.used, budget, usedRatio: pb.usedRatio, dropped: pb.dropped, mtime: doc.mtime },
|
|
321
|
+
sections: {
|
|
322
|
+
beliefsShown: shown,
|
|
323
|
+
beliefsFolded: folded,
|
|
324
|
+
beliefsDropped: Math.max(0, model.beliefs.length - kept),
|
|
325
|
+
// 判断那一段这一轮有没有真的进文档(没进时 beliefsShown 恒为 0)。
|
|
326
|
+
beliefsKept: laid ? laid.kept : false,
|
|
327
|
+
// 没进的原因:budget=字数紧被整段省掉 / placed=判断全在某个人名下、摆在别处 /
|
|
328
|
+
// none=她还没有判断。界面按这个改口,别把「摆到别处」说成「字不够」。
|
|
329
|
+
beliefsReason: laid ? laid.reason : 'none',
|
|
330
|
+
beliefsDeclared: laid ? laid.declared : 0,
|
|
331
|
+
beliefsTotal: model.beliefs.length,
|
|
332
|
+
refuted: model.refuted.length,
|
|
333
|
+
refused: model.facts?.refused ?? model.refusals.length,
|
|
334
|
+
narrative: model.narrative ? 1 : 0,
|
|
335
|
+
tensions: model.tensionsTotal ?? model.tensions?.length ?? 0,
|
|
336
|
+
tensionFirst: model.tensions?.[0]
|
|
337
|
+
? { a: model.tensions[0].a.text, b: model.tensions[0].b.text, sim: model.tensions[0].sim }
|
|
338
|
+
: null,
|
|
339
|
+
solid: model.solidTotal ?? model.solid?.length ?? 0,
|
|
340
|
+
reactions: model.facts?.reactions ?? { accept: 0, insist: 0, hurt: 0, leave: 0, other: 0 },
|
|
341
|
+
hurts: model.facts?.hurts ?? 0,
|
|
342
|
+
refusals: model.refusals.slice(-3).map((r) => ({
|
|
343
|
+
at: r.at, quote: r.quote ?? r.what, act: r.act ?? '', object: r.object ?? '', reaction: r.reaction ?? null,
|
|
344
|
+
})),
|
|
345
|
+
scars: model.scars?.length ?? 0,
|
|
346
|
+
scarsResolved: model.facts?.scarsResolved ?? 0,
|
|
347
|
+
scarFirst: model.scars?.[0]
|
|
348
|
+
? { at: model.scars[0].at, quote: model.scars[0].quote, reaction: model.scars[0].reaction, conceded: model.scars[0].concedeText ?? null }
|
|
349
|
+
: null,
|
|
350
|
+
voices: model.facts?.voices ?? 0,
|
|
351
|
+
voicesEchoed: model.facts?.voicesEchoed ?? 0,
|
|
352
|
+
voiceLast: model.voices?.length
|
|
353
|
+
? {
|
|
354
|
+
at: model.voices[model.voices.length - 1].at,
|
|
355
|
+
text: model.voices[model.voices.length - 1].text,
|
|
356
|
+
echoed: Boolean(model.voices[model.voices.length - 1].echoed),
|
|
357
|
+
}
|
|
358
|
+
: null,
|
|
359
|
+
...stanceSummary(buildStances(model)),
|
|
360
|
+
},
|
|
361
|
+
events: countEvents(events).real,
|
|
362
|
+
eventsAll: countEvents(events).all,
|
|
363
|
+
diaries: diary.length,
|
|
364
|
+
diariesWritten: diaryDatesWritten(diary, events).length,
|
|
365
|
+
session: { turns: h.turns, rotateAfter: Number(config?.chat?.rotateAfter ?? 0), rotatedAt: h.rotatedAt },
|
|
366
|
+
sleep: {
|
|
367
|
+
everyDays: Number(config?.memory?.sleepEveryDays ?? 0),
|
|
368
|
+
last: lastSleep ? String(lastSleep.text).slice(6, 16) : '',
|
|
369
|
+
},
|
|
370
|
+
recall: { days: 30, limit: 5 },
|
|
371
|
+
})
|
|
372
|
+
}],
|
|
373
|
+
|
|
374
|
+
['/health', async (req, res) => {
|
|
375
|
+
if (req.method !== 'GET') return send(res, 405, { error: '只接受 GET' })
|
|
376
|
+
const { config } = await loadConfig(root)
|
|
377
|
+
const { events } = await readEvents(root)
|
|
378
|
+
send(res, 200, examine(events, { growthDays: config?.growthDays }))
|
|
379
|
+
}],
|
|
380
|
+
|
|
381
|
+
['/reports', async (req, res) => {
|
|
382
|
+
const url = new URL(req.url ?? '/', 'http://x')
|
|
383
|
+
const want = String(url.searchParams.get('date') ?? '').trim()
|
|
384
|
+
const dates = await listReports(root)
|
|
385
|
+
if (!/^\d{4}-\d{2}-\d{2}$/.test(want)) {
|
|
386
|
+
return send(res, 200, { dates, date: null, md: '' })
|
|
387
|
+
}
|
|
388
|
+
const { events } = await readEvents(root)
|
|
389
|
+
const { config } = await loadConfig(root)
|
|
390
|
+
const diary = await readDaily('diaryDir', want, root).catch(() => null)
|
|
391
|
+
const md = buildReport(/** @type {any} */ ({
|
|
392
|
+
dateKey: want,
|
|
393
|
+
selfPronoun: selfPronounOf(config),
|
|
394
|
+
seen: seenOn(events, want),
|
|
395
|
+
events,
|
|
396
|
+
diary,
|
|
397
|
+
diaryVisible: config?.diary?.visible === true,
|
|
398
|
+
}))
|
|
399
|
+
send(res, 200, { dates, date: want, md })
|
|
400
|
+
}],
|
|
401
|
+
|
|
402
|
+
['/templates', async (_req, res) => {
|
|
403
|
+
const { events } = await readEvents(root)
|
|
404
|
+
const fired = firedLog(events)
|
|
405
|
+
send(res, 200, {
|
|
406
|
+
rows: CATALOG.map((t) => ({
|
|
407
|
+
id: t.id,
|
|
408
|
+
stage: t.stage,
|
|
409
|
+
asks: t.asks,
|
|
410
|
+
once: Boolean(t.once),
|
|
411
|
+
everyDays: Number(/** @type {any} */ (t).repeat?.everyDays ?? 0) || null,
|
|
412
|
+
fired: fired.has(t.id),
|
|
413
|
+
count: Number(fired.get(t.id)?.count ?? 0),
|
|
414
|
+
lastAt: fired.get(t.id)?.lastAt ? new Date(fired.get(t.id).lastAt).toISOString() : null,
|
|
415
|
+
})),
|
|
416
|
+
})
|
|
417
|
+
}],
|
|
418
|
+
]
|
|
419
|
+
}
|