dsh-ta 0.3.0 → 0.4.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/CHANGELOG.md +90 -0
- package/README.md +160 -107
- package/cordis.patch.yml +5 -35
- package/docs/RELEASE.md +62 -49
- package/docs/USAGE.md +6 -7
- package/docs/presets.example.yml +7 -43
- package/index.js +3 -164
- package/package.json +5 -28
- package/src/client/client.js +3176 -0
- package/{config.js → src/host/config.js} +39 -3
- package/src/host/constants.js +7 -0
- package/src/host/gates.js +34 -0
- package/src/host/identity.js +145 -0
- package/{im → src/host/im}/bindings.js +1 -1
- package/src/host/index.js +173 -0
- package/{jurisdiction.js → src/host/jurisdiction.js} +1 -1
- package/src/host/maintenance/collect.js +362 -0
- package/src/host/maintenance/completions.js +56 -0
- package/src/host/maintenance/consumption.js +67 -0
- package/src/host/maintenance/cursors.js +53 -0
- package/src/host/maintenance/engine.js +392 -0
- package/src/host/maintenance/llm.js +381 -0
- package/src/host/maintenance/lock.js +105 -0
- package/src/host/maintenance/prompts.js +255 -0
- package/src/host/maintenance/runs.js +47 -0
- package/src/host/maintenance/schedule.js +173 -0
- package/src/host/maintenance/session-cleanup.js +289 -0
- package/src/host/maintenance/session-watcher.js +107 -0
- package/src/host/maintenance/sleep.js +3 -0
- package/src/host/maintenance/stages.js +241 -0
- package/src/host/maintenance/state.js +192 -0
- package/src/host/prompts.js +96 -0
- package/src/host/records.js +175 -0
- package/src/host/runtime-manager.js +281 -0
- package/{runtime.js → src/host/runtime.js} +6 -8
- package/src/host/settings/defaults.js +55 -0
- package/{settings → src/host/settings}/home.js +4 -3
- package/{settings/routes.js → src/host/settings/http.js} +5 -137
- package/{settings → src/host/settings}/im.js +18 -1
- package/{settings → src/host/settings}/lifecycle.js +29 -43
- package/src/host/settings/persona.js +113 -0
- package/src/host/settings/routes.js +229 -0
- package/{settings → src/host/settings}/status.js +70 -42
- package/src/host/state/appraisals.js +56 -0
- package/src/host/state/feel.js +78 -0
- package/src/host/state/index.js +9 -0
- package/src/host/state/mood.js +103 -0
- package/src/host/state/notes.js +18 -0
- package/{life.js → src/host/state/outing.js} +13 -136
- package/src/host/state/values.js +9 -0
- package/{stickers → src/host/stickers}/tool.js +1 -1
- package/src/host/storage/fs.js +29 -0
- package/src/host/storage/index.js +8 -0
- package/src/host/storage/paths.js +20 -0
- package/src/host/storage/revision.js +5 -0
- package/src/host/storage/session-state.js +38 -0
- package/src/host/storage/time.js +127 -0
- package/src/host/tools/index.js +152 -0
- package/src/host/tools/read.js +252 -0
- package/src/host/tools/shared.js +23 -0
- package/src/host/tools/wiki.js +35 -0
- package/src/host/tools/write.js +164 -0
- package/{usage → src/host/usage}/aggregate.js +4 -1
- package/{usage → src/host/usage}/ledger.js +47 -21
- package/client.js +0 -2554
- package/gates.js +0 -18
- package/identity.js +0 -97
- package/prompts.js +0 -28
- package/runtime-manager.js +0 -153
- package/settings/defaults.js +0 -41
- package/settings/persona.js +0 -203
- package/settings/schedule.js +0 -156
- package/storage.js +0 -122
- package/tools.js +0 -540
- /package/{im → src/host/im}/say.js +0 -0
- /package/{memory → src/host/memory}/capture.js +0 -0
- /package/{memory → src/host/memory}/messages.js +0 -0
- /package/{memory → src/host/memory}/outbox.js +0 -0
- /package/{reme.js → src/host/reme.js} +0 -0
- /package/{settings → src/host/settings}/errors.js +0 -0
- /package/{stickers → src/host/stickers}/load.js +0 -0
- /package/{stickers → src/host/stickers}/select.js +0 -0
- /package/{stickers → src/host/stickers}/serve.js +0 -0
- /package/{usage → src/host/usage}/pricing.js +0 -0
- /package/{usage → src/host/usage}/serve.js +0 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto'
|
|
2
|
+
import { readFileSync } from 'node:fs'
|
|
3
|
+
import { resolve } from 'node:path'
|
|
4
|
+
import { localDateText, writeAtomic } from '../storage/index.js'
|
|
5
|
+
import { clamp01, clampSigned } from './values.js'
|
|
6
|
+
|
|
7
|
+
export const APPRAISAL_FILE = 'state/appraisals.jsonl'
|
|
8
|
+
export const APPRAISAL_LIMIT = 500
|
|
9
|
+
|
|
10
|
+
export function appraisalsPath(config) {
|
|
11
|
+
return resolve(config.taHome, APPRAISAL_FILE)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function readAppraisals(config) {
|
|
15
|
+
let text = ''
|
|
16
|
+
try {
|
|
17
|
+
text = readFileSync(appraisalsPath(config), 'utf8')
|
|
18
|
+
} catch {
|
|
19
|
+
return []
|
|
20
|
+
}
|
|
21
|
+
const records = []
|
|
22
|
+
for (const line of text.split(/\r?\n/)) {
|
|
23
|
+
if (!line.trim()) continue
|
|
24
|
+
try {
|
|
25
|
+
const value = JSON.parse(line)
|
|
26
|
+
if (value && typeof value === 'object') records.push(value)
|
|
27
|
+
} catch {
|
|
28
|
+
/* 损坏行跳过 */
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return records
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function appendAppraisal(config, entry) {
|
|
35
|
+
const record = {
|
|
36
|
+
id: typeof entry?.id === 'string' && entry.id ? entry.id : `appraisal:${randomUUID()}`,
|
|
37
|
+
at: typeof entry?.at === 'string' && entry.at ? entry.at : new Date().toISOString(),
|
|
38
|
+
event: String(entry?.event ?? '').trim(),
|
|
39
|
+
why: String(entry?.why ?? '').trim(),
|
|
40
|
+
intensity: clamp01(entry?.intensity, 0),
|
|
41
|
+
valence: clampSigned(entry?.valence, 0),
|
|
42
|
+
energy: clamp01(entry?.energy, 0.6),
|
|
43
|
+
attention: clamp01(entry?.attention, 0.6),
|
|
44
|
+
label: typeof entry?.label === 'string' ? entry.label.slice(0, 20) : '',
|
|
45
|
+
sources: Array.isArray(entry?.sources) ? entry.sources.map(String).slice(0, 10) : [],
|
|
46
|
+
provenance: entry?.provenance === 'explicit' ? 'explicit' : 'inferred',
|
|
47
|
+
}
|
|
48
|
+
const kept = [...readAppraisals(config), record].slice(-APPRAISAL_LIMIT)
|
|
49
|
+
writeAtomic(appraisalsPath(config), kept.map(item => JSON.stringify(item)).join('\n') + '\n')
|
|
50
|
+
return record
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** 某天(默认今天)的 appraisal 条数;情绪整合只在该天有条目时运行。 */
|
|
54
|
+
export function appraisalCountForDay(config, dateText = localDateText(config)) {
|
|
55
|
+
return readAppraisals(config).filter(item => localDateText(config, Date.parse(item.at ?? '')) === dateText).length
|
|
56
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs'
|
|
2
|
+
import { resolve } from 'node:path'
|
|
3
|
+
import { localDateText, writeAtomic } from '../storage/index.js'
|
|
4
|
+
import { appendAppraisal } from './appraisals.js'
|
|
5
|
+
import { readMood, writeMood } from './mood.js'
|
|
6
|
+
import { clamp01, clampSigned } from './values.js'
|
|
7
|
+
|
|
8
|
+
export const FEEL_LIMITS_FILE = 'state/feel-limits.json'
|
|
9
|
+
|
|
10
|
+
function feelLimitsPath(config) {
|
|
11
|
+
return resolve(config.taHome, FEEL_LIMITS_FILE)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function readFeelLimits(config, date) {
|
|
15
|
+
try {
|
|
16
|
+
const value = JSON.parse(readFileSync(feelLimitsPath(config), 'utf8'))
|
|
17
|
+
if (value?.day === date) {
|
|
18
|
+
return {
|
|
19
|
+
day: date,
|
|
20
|
+
globalCount: Number.isSafeInteger(value.globalCount) ? value.globalCount : 0,
|
|
21
|
+
sessions: value.sessions && typeof value.sessions === 'object' ? value.sessions : {},
|
|
22
|
+
turns: value.turns && typeof value.turns === 'object' ? value.turns : {},
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
} catch {
|
|
26
|
+
/* fall through */
|
|
27
|
+
}
|
|
28
|
+
return { day: date, globalCount: 0, sessions: {}, turns: {} }
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* ta_feel:写 appraisal + 当前情绪,并执行限频。
|
|
33
|
+
* 限频:每轮 1 次、每会话 3 次/日、全局 6 次/日。
|
|
34
|
+
*/
|
|
35
|
+
export function recordFeel(config, {
|
|
36
|
+
event, why, valence, energy, attention, label, sources = [], sessionId, turn, nowMs = Date.now(),
|
|
37
|
+
} = {}) {
|
|
38
|
+
const eventText = String(event ?? '').trim()
|
|
39
|
+
const whyText = String(why ?? '').trim()
|
|
40
|
+
if (!eventText || !whyText) throw new Error('event and why are required')
|
|
41
|
+
const date = localDateText(config, nowMs)
|
|
42
|
+
const limits = readFeelLimits(config, date)
|
|
43
|
+
const sessionKey = String(sessionId ?? 'unknown')
|
|
44
|
+
const turnKey = `${sessionKey}:${turn ?? 'unknown'}`
|
|
45
|
+
if (limits.turns[turnKey]) throw new Error('这一轮已经记录过情绪了')
|
|
46
|
+
const sessionCount = Number(limits.sessions[sessionKey]) || 0
|
|
47
|
+
if (sessionCount >= 3) throw new Error('这个会话今天记录情绪的次数已用完')
|
|
48
|
+
if (limits.globalCount >= 6) throw new Error('今天记录情绪的次数已用完')
|
|
49
|
+
|
|
50
|
+
const previous = readMood(config, nowMs)
|
|
51
|
+
const nextValence = clampSigned(valence, previous.valence)
|
|
52
|
+
const nextEnergy = clamp01(energy, previous.energy)
|
|
53
|
+
const nextAttention = clamp01(attention, previous.attention)
|
|
54
|
+
const intensity = Math.max(
|
|
55
|
+
Math.abs(nextValence - previous.valence),
|
|
56
|
+
Math.abs(nextEnergy - previous.energy),
|
|
57
|
+
Math.abs(nextAttention - previous.attention),
|
|
58
|
+
)
|
|
59
|
+
const nextLabel = typeof label === 'string' && label.trim() ? label.trim().slice(0, 20) : previous.label
|
|
60
|
+
const appraisal = appendAppraisal(config, {
|
|
61
|
+
at: new Date(nowMs).toISOString(),
|
|
62
|
+
event: eventText,
|
|
63
|
+
why: whyText,
|
|
64
|
+
intensity: clamp01(intensity, 0),
|
|
65
|
+
valence: nextValence,
|
|
66
|
+
energy: nextEnergy,
|
|
67
|
+
attention: nextAttention,
|
|
68
|
+
label: nextLabel,
|
|
69
|
+
sources,
|
|
70
|
+
provenance: 'explicit',
|
|
71
|
+
})
|
|
72
|
+
writeMood(config, { label: nextLabel, valence: nextValence, energy: nextEnergy, attention: nextAttention }, nowMs)
|
|
73
|
+
limits.globalCount += 1
|
|
74
|
+
limits.sessions[sessionKey] = sessionCount + 1
|
|
75
|
+
limits.turns[turnKey] = true
|
|
76
|
+
writeAtomic(feelLimitsPath(config), JSON.stringify(limits) + '\n')
|
|
77
|
+
return { appraisal, intensity }
|
|
78
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { readNoteLines, addNote } from './notes.js'
|
|
2
|
+
export {
|
|
3
|
+
readOutingActive, startOuting, finishOuting, cleanupOutings,
|
|
4
|
+
} from './outing.js'
|
|
5
|
+
export { readMood, writeMood, applyEmotionUpdate, moodLine } from './mood.js'
|
|
6
|
+
export {
|
|
7
|
+
readAppraisals, appendAppraisal, appraisalCountForDay,
|
|
8
|
+
} from './appraisals.js'
|
|
9
|
+
export { recordFeel } from './feel.js'
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs'
|
|
2
|
+
import { resolve } from 'node:path'
|
|
3
|
+
import { writeAtomic } from '../storage/index.js'
|
|
4
|
+
import { clamp01, clampSigned } from './values.js'
|
|
5
|
+
|
|
6
|
+
const MOOD_BASELINE = { label: '平静', valence: 0, energy: 0.6, attention: 0.6 }
|
|
7
|
+
const MOOD_TAU_HOURS = { valence: 8, energy: 6, attention: 8 }
|
|
8
|
+
const MOOD_LABEL_TTL_HOURS = 12
|
|
9
|
+
|
|
10
|
+
function moodPath(config) {
|
|
11
|
+
return resolve(config.taHome, 'state/mood.json')
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function labelFor(valence, energy) {
|
|
15
|
+
if (valence <= -0.35) return energy < 0.4 ? '低落' : '烦躁'
|
|
16
|
+
if (valence >= 0.45) return energy >= 0.6 ? '愉快' : '温暖'
|
|
17
|
+
if (energy < 0.35) return '疲惫'
|
|
18
|
+
if (energy >= 0.75) return '有精神'
|
|
19
|
+
return '平静'
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function readStoredMood(config) {
|
|
23
|
+
try {
|
|
24
|
+
const value = JSON.parse(readFileSync(moodPath(config), 'utf8'))
|
|
25
|
+
return value && typeof value === 'object' ? value : {}
|
|
26
|
+
} catch {
|
|
27
|
+
return {}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** 读取时按 tau 衰减到 baseline,不写文件。 */
|
|
32
|
+
export function readMood(config, nowMs = Date.now()) {
|
|
33
|
+
const stored = readStoredMood(config)
|
|
34
|
+
const baselineStored = stored.baseline && typeof stored.baseline === 'object' ? stored.baseline : {}
|
|
35
|
+
const baseline = {
|
|
36
|
+
label: typeof baselineStored.label === 'string' && baselineStored.label ? baselineStored.label : MOOD_BASELINE.label,
|
|
37
|
+
valence: clampSigned(baselineStored.valence, MOOD_BASELINE.valence),
|
|
38
|
+
energy: clamp01(baselineStored.energy, MOOD_BASELINE.energy),
|
|
39
|
+
attention: clamp01(baselineStored.attention, MOOD_BASELINE.attention),
|
|
40
|
+
}
|
|
41
|
+
const current = stored.current && typeof stored.current === 'object' ? stored.current : {}
|
|
42
|
+
const label = typeof current.label === 'string' && current.label ? current.label : baseline.label
|
|
43
|
+
const rawValence = clampSigned(current.valence, baseline.valence)
|
|
44
|
+
const rawEnergy = clamp01(current.energy, baseline.energy)
|
|
45
|
+
const rawAttention = clamp01(current.attention, baseline.attention)
|
|
46
|
+
const updatedAt = typeof current.updatedAt === 'string' ? current.updatedAt : null
|
|
47
|
+
const updated = Date.parse(updatedAt ?? '')
|
|
48
|
+
const hours = Number.isFinite(updated) ? Math.max(0, (nowMs - updated) / 3_600_000) : 24
|
|
49
|
+
const decay = (value, base, tau) => base + (value - base) * Math.exp(-hours / tau)
|
|
50
|
+
const valence = clampSigned(decay(rawValence, baseline.valence, MOOD_TAU_HOURS.valence))
|
|
51
|
+
const energy = clamp01(decay(rawEnergy, baseline.energy, MOOD_TAU_HOURS.energy))
|
|
52
|
+
const attention = clamp01(decay(rawAttention, baseline.attention, MOOD_TAU_HOURS.attention))
|
|
53
|
+
return {
|
|
54
|
+
label: hours <= MOOD_LABEL_TTL_HOURS ? label : labelFor(valence, energy),
|
|
55
|
+
valence,
|
|
56
|
+
energy,
|
|
57
|
+
attention,
|
|
58
|
+
baseline,
|
|
59
|
+
updatedAt,
|
|
60
|
+
hours,
|
|
61
|
+
raw: { label, valence: rawValence, energy: rawEnergy, attention: rawAttention },
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** 写当前情绪;baseline 不变。 */
|
|
66
|
+
export function writeMood(config, patch = {}, nowMs = Date.now()) {
|
|
67
|
+
const previous = readMood(config, nowMs)
|
|
68
|
+
const label = typeof patch.label === 'string' && patch.label.trim()
|
|
69
|
+
? patch.label.trim().slice(0, 20)
|
|
70
|
+
: previous.raw.label
|
|
71
|
+
const current = {
|
|
72
|
+
label,
|
|
73
|
+
valence: clampSigned(patch.valence, previous.raw.valence),
|
|
74
|
+
energy: clamp01(patch.energy, previous.raw.energy),
|
|
75
|
+
attention: clamp01(patch.attention, previous.raw.attention),
|
|
76
|
+
updatedAt: new Date(nowMs).toISOString(),
|
|
77
|
+
}
|
|
78
|
+
const next = { baseline: previous.baseline, current }
|
|
79
|
+
writeAtomic(moodPath(config), JSON.stringify(next, null, 2) + '\n')
|
|
80
|
+
return readMood(config, nowMs)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** 维护情绪阶段:把整合结果写回 current。 */
|
|
84
|
+
export function applyEmotionUpdate(config, update, nowMs = Date.now()) {
|
|
85
|
+
return writeMood(config, {
|
|
86
|
+
label: update.label,
|
|
87
|
+
valence: update.valence,
|
|
88
|
+
energy: update.energy,
|
|
89
|
+
attention: update.attention,
|
|
90
|
+
}, nowMs)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function moodWord(value) {
|
|
94
|
+
if (value >= 0.7) return '充足'
|
|
95
|
+
if (value >= 0.4) return '一般'
|
|
96
|
+
return '偏低'
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function moodLine(config) {
|
|
100
|
+
const mood = readMood(config)
|
|
101
|
+
const line = `你现在的心情:${mood.label};精力${moodWord(mood.energy)},注意力${moodWord(mood.attention)}。`
|
|
102
|
+
return line.length > 200 ? line.slice(0, 197) + '…' : line
|
|
103
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs'
|
|
2
|
+
import { withinHome, writeAtomic } from '../storage/index.js'
|
|
3
|
+
|
|
4
|
+
export function readNoteLines(config) {
|
|
5
|
+
try {
|
|
6
|
+
return readFileSync(withinHome('notes/note.md', config), 'utf8').split(/\r?\n/).filter(Boolean)
|
|
7
|
+
} catch {
|
|
8
|
+
return []
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function addNote(config, text) {
|
|
13
|
+
const line = String(text ?? '').replace(/\s+/g, ' ').trim()
|
|
14
|
+
if (!line) throw new Error('text is required')
|
|
15
|
+
const lines = [...readNoteLines(config), line].slice(-80)
|
|
16
|
+
writeAtomic(withinHome('notes/note.md', config), lines.join('\n') + '\n')
|
|
17
|
+
return `notes/note.md (${lines.length}/80)`
|
|
18
|
+
}
|
|
@@ -1,65 +1,8 @@
|
|
|
1
1
|
import { readFileSync, readdirSync, unlinkSync } from 'node:fs'
|
|
2
|
-
import { resolve } from 'node:path'
|
|
3
2
|
import { randomUUID } from 'node:crypto'
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
try {
|
|
8
|
-
return readFileSync(withinHome('notes/note.md', config), 'utf8').split(/\r?\n/).filter(Boolean)
|
|
9
|
-
} catch {
|
|
10
|
-
return []
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export function addNote(config, text) {
|
|
15
|
-
const line = String(text ?? '').replace(/\s+/g, ' ').trim()
|
|
16
|
-
if (!line) throw new Error('text is required')
|
|
17
|
-
const lines = [...readNoteLines(config), line].slice(-80)
|
|
18
|
-
writeAtomic(withinHome('notes/note.md', config), lines.join('\n') + '\n')
|
|
19
|
-
return `notes/note.md (${lines.length}/80)`
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export function reviewLockPath(config) {
|
|
23
|
-
return resolve(config.taHome, 'state/review.lock')
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export function acquireReviewLock(config, sessionId) {
|
|
27
|
-
const path = reviewLockPath(config)
|
|
28
|
-
try {
|
|
29
|
-
const existing = JSON.parse(readFileSync(path, 'utf8'))
|
|
30
|
-
if (existing?.pid === process.pid && existing?.sessionId !== String(sessionId)) return false
|
|
31
|
-
if (existing?.pid !== process.pid && Number.isSafeInteger(existing?.pid)) {
|
|
32
|
-
try {
|
|
33
|
-
process.kill(existing.pid, 0)
|
|
34
|
-
return false
|
|
35
|
-
} catch {
|
|
36
|
-
/* stale */
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
} catch {
|
|
40
|
-
/* no lock */
|
|
41
|
-
}
|
|
42
|
-
try {
|
|
43
|
-
writeAtomic(path, JSON.stringify({
|
|
44
|
-
pid: process.pid,
|
|
45
|
-
sessionId: String(sessionId),
|
|
46
|
-
startedAt: new Date().toISOString(),
|
|
47
|
-
}) + '\n')
|
|
48
|
-
return true
|
|
49
|
-
} catch {
|
|
50
|
-
return false
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export function releaseReviewLock(config, sessionId) {
|
|
55
|
-
try {
|
|
56
|
-
const path = reviewLockPath(config)
|
|
57
|
-
const existing = JSON.parse(readFileSync(path, 'utf8'))
|
|
58
|
-
if (existing?.pid === process.pid && existing?.sessionId === String(sessionId)) unlinkSync(path)
|
|
59
|
-
} catch {
|
|
60
|
-
/* no lock */
|
|
61
|
-
}
|
|
62
|
-
}
|
|
3
|
+
import { resolve } from 'node:path'
|
|
4
|
+
import { todayText, withinHome, writeAtomic } from '../storage/index.js'
|
|
5
|
+
import { readMaintenanceLock } from '../maintenance/lock.js'
|
|
63
6
|
|
|
64
7
|
const OUTING_LIMIT_PER_DAY = 4
|
|
65
8
|
const OUTING_TIMEOUT_MS = 20 * 60 * 1000
|
|
@@ -92,6 +35,11 @@ function outingSlug(value) {
|
|
|
92
35
|
return slug || 'trip'
|
|
93
36
|
}
|
|
94
37
|
|
|
38
|
+
/** 当前是否有进行中的出门;供维护 D9 等待/跳过判断。 */
|
|
39
|
+
export function readOutingActive(config) {
|
|
40
|
+
return readOutingState(config).active
|
|
41
|
+
}
|
|
42
|
+
|
|
95
43
|
export function startOuting(config, args = {}) {
|
|
96
44
|
const state = readOutingState(config)
|
|
97
45
|
const now = Date.now()
|
|
@@ -118,7 +66,7 @@ export function startOuting(config, args = {}) {
|
|
|
118
66
|
return `出门开始:${destination}`
|
|
119
67
|
}
|
|
120
68
|
|
|
121
|
-
export function finishOuting(config, args = {}) {
|
|
69
|
+
export function finishOuting(config, args = {}, { sessionId } = {}) {
|
|
122
70
|
const state = readOutingState(config)
|
|
123
71
|
if (!state.active) throw new Error('当前没有进行中的出门')
|
|
124
72
|
const trip = state.active
|
|
@@ -144,10 +92,12 @@ export function finishOuting(config, args = {}) {
|
|
|
144
92
|
const timeout = Number.isFinite(elapsed) && elapsed > OUTING_TIMEOUT_MS ? '(超时)' : ''
|
|
145
93
|
const body = [
|
|
146
94
|
'---',
|
|
95
|
+
`id: ${trip.id}`,
|
|
147
96
|
`date: ${date}`,
|
|
148
97
|
`destination: ${trip.destination}`,
|
|
149
98
|
`started_at: ${trip.startedAt}`,
|
|
150
99
|
`finished_at: ${new Date().toISOString()}`,
|
|
100
|
+
...(sessionId ? [`session_id: ${String(sessionId)}`] : []),
|
|
151
101
|
'untrusted: true',
|
|
152
102
|
'---',
|
|
153
103
|
`# 出门:${trip.destination}${timeout}`,
|
|
@@ -171,15 +121,10 @@ export function finishOuting(config, args = {}) {
|
|
|
171
121
|
|
|
172
122
|
/**
|
|
173
123
|
* Delete outings from earlier local days only when that day's diary exists
|
|
174
|
-
* (evidence that
|
|
124
|
+
* (evidence that maintenance consumed the day) and no maintenance run is live.
|
|
175
125
|
*/
|
|
176
126
|
export function cleanupOutings(config) {
|
|
177
|
-
|
|
178
|
-
readFileSync(reviewLockPath(config), 'utf8')
|
|
179
|
-
return 0
|
|
180
|
-
} catch {
|
|
181
|
-
/* no active review lock */
|
|
182
|
-
}
|
|
127
|
+
if (readMaintenanceLock(config)) return 0
|
|
183
128
|
const today = todayText(config)
|
|
184
129
|
const directory = resolve(config.taHome, 'outings')
|
|
185
130
|
let names
|
|
@@ -207,71 +152,3 @@ export function cleanupOutings(config) {
|
|
|
207
152
|
}
|
|
208
153
|
return removed
|
|
209
154
|
}
|
|
210
|
-
|
|
211
|
-
const MOOD_DEFAULT = {
|
|
212
|
-
mood: '平静',
|
|
213
|
-
energy: 0.6,
|
|
214
|
-
attention: 0.6,
|
|
215
|
-
baseline: { mood: '平静', energy: 0.6, attention: 0.6 },
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
function moodPath(config) {
|
|
219
|
-
return resolve(config.taHome, 'state/mood.json')
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
function moodNumber(value, fallback) {
|
|
223
|
-
return Number.isFinite(value) ? Math.max(0, Math.min(1, value)) : fallback
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
/** Read-only decay toward baseline; no file side effect. */
|
|
227
|
-
export function readMood(config) {
|
|
228
|
-
let stored
|
|
229
|
-
try {
|
|
230
|
-
stored = JSON.parse(readFileSync(moodPath(config), 'utf8'))
|
|
231
|
-
} catch {
|
|
232
|
-
stored = {}
|
|
233
|
-
}
|
|
234
|
-
const baseline = {
|
|
235
|
-
mood: typeof stored?.baseline?.mood === 'string' ? stored.baseline.mood : MOOD_DEFAULT.baseline.mood,
|
|
236
|
-
energy: moodNumber(stored?.baseline?.energy, MOOD_DEFAULT.baseline.energy),
|
|
237
|
-
attention: moodNumber(stored?.baseline?.attention, MOOD_DEFAULT.baseline.attention),
|
|
238
|
-
}
|
|
239
|
-
const updated = Date.parse(stored?.updatedAt ?? '')
|
|
240
|
-
const hours = Number.isFinite(updated) ? Math.max(0, (Date.now() - updated) / 3_600_000) : 24
|
|
241
|
-
const decay = Math.exp(-hours / 8)
|
|
242
|
-
return {
|
|
243
|
-
mood: hours > 12 ? baseline.mood : (typeof stored.mood === 'string' ? stored.mood : baseline.mood),
|
|
244
|
-
energy: baseline.energy + (moodNumber(stored?.energy, baseline.energy) - baseline.energy) * decay,
|
|
245
|
-
attention: baseline.attention + (moodNumber(stored?.attention, baseline.attention) - baseline.attention) * decay,
|
|
246
|
-
baseline,
|
|
247
|
-
updatedAt: stored?.updatedAt,
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
function moodWord(value) {
|
|
252
|
-
if (value >= 0.7) return '充足'
|
|
253
|
-
if (value >= 0.4) return '一般'
|
|
254
|
-
return '偏低'
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
export function moodLine(config) {
|
|
258
|
-
const mood = readMood(config)
|
|
259
|
-
const line = `你现在的心情:${mood.mood};精力${moodWord(mood.energy)},注意力${moodWord(mood.attention)}。`
|
|
260
|
-
return line.length > 200 ? line.slice(0, 197) + '…' : line
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
export function setMood(config, args = {}) {
|
|
264
|
-
const previous = readMood(config)
|
|
265
|
-
const mood = String(args.mood ?? '').trim().slice(0, 20)
|
|
266
|
-
if (!mood) throw new Error('mood is required')
|
|
267
|
-
const next = {
|
|
268
|
-
mood,
|
|
269
|
-
energy: moodNumber(args.energy, previous.energy),
|
|
270
|
-
attention: moodNumber(args.attention, previous.attention),
|
|
271
|
-
baseline: previous.baseline,
|
|
272
|
-
updatedAt: new Date().toISOString(),
|
|
273
|
-
}
|
|
274
|
-
writeAtomic(moodPath(config), JSON.stringify(next, null, 2) + '\n')
|
|
275
|
-
return next.mood
|
|
276
|
-
}
|
|
277
|
-
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export function clamp01(value, fallback = 0) {
|
|
2
|
+
const number = Number(value)
|
|
3
|
+
return Number.isFinite(number) ? Math.max(0, Math.min(1, number)) : fallback
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export function clampSigned(value, fallback = 0) {
|
|
7
|
+
const number = Number(value)
|
|
8
|
+
return Number.isFinite(number) ? Math.max(-1, Math.min(1, number)) : fallback
|
|
9
|
+
}
|
|
@@ -3,7 +3,7 @@ import { extname, resolve } from 'node:path'
|
|
|
3
3
|
import { randomUUID } from 'node:crypto'
|
|
4
4
|
import { activePack, readStickerState, resolveStickerFile, stickerStatePath } from './load.js'
|
|
5
5
|
import { selectSticker, stickerEmotions } from './select.js'
|
|
6
|
-
import { todayText, writeAtomic } from '../storage.js'
|
|
6
|
+
import { todayText, writeAtomic } from '../storage/index.js'
|
|
7
7
|
|
|
8
8
|
const DEFAULT_MAX_PER_DAY = 12
|
|
9
9
|
const DEFAULT_MIN_GAP_MINUTES = 30
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { accessSync, constants, mkdirSync, readFileSync, renameSync, statSync, writeFileSync } from 'node:fs'
|
|
2
|
+
import { dirname } from 'node:path'
|
|
3
|
+
|
|
4
|
+
/** 原子写:临时文件 + rename,失败不破坏原文件。 */
|
|
5
|
+
export function writeAtomic(path, text) {
|
|
6
|
+
mkdirSync(dirname(path), { recursive: true })
|
|
7
|
+
const temporary = `${path}.tmp-${process.pid}-${Date.now()}`
|
|
8
|
+
writeFileSync(temporary, text)
|
|
9
|
+
renameSync(temporary, path)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function readTextIfExists(path) {
|
|
13
|
+
try {
|
|
14
|
+
if (!statSync(path).isFile()) return undefined
|
|
15
|
+
return readFileSync(path, 'utf8')
|
|
16
|
+
} catch {
|
|
17
|
+
return undefined
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function isWritableDirectory(path) {
|
|
22
|
+
try {
|
|
23
|
+
if (!statSync(path).isDirectory()) return false
|
|
24
|
+
accessSync(path, constants.W_OK)
|
|
25
|
+
return true
|
|
26
|
+
} catch {
|
|
27
|
+
return false
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { realPath, withinHome } from './paths.js'
|
|
2
|
+
export { writeAtomic, readTextIfExists, isWritableDirectory } from './fs.js'
|
|
3
|
+
export { sha256Revision } from './revision.js'
|
|
4
|
+
export { recordSessionMap, readInternalSessions, internalSessionIds } from './session-state.js'
|
|
5
|
+
export {
|
|
6
|
+
todayText, zonedMidnightUtc, localDateText, localClockText, zonedIso, startOfLocalDay,
|
|
7
|
+
addLocalDays, localWeekday,
|
|
8
|
+
} from './time.js'
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { realpathSync } from 'node:fs'
|
|
2
|
+
import { resolve, sep } from 'node:path'
|
|
3
|
+
|
|
4
|
+
export function realPath(value) {
|
|
5
|
+
if (typeof value !== 'string' || value.length === 0) return ''
|
|
6
|
+
try {
|
|
7
|
+
return realpathSync.native(value)
|
|
8
|
+
} catch {
|
|
9
|
+
return resolve(value)
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Resolve a path inside TA_HOME; throw when it escapes (symlinks included). */
|
|
14
|
+
export function withinHome(relative, config) {
|
|
15
|
+
if (typeof relative !== 'string' || relative.length === 0) throw new Error('path is required')
|
|
16
|
+
const base = realPath(config.taHome)
|
|
17
|
+
const target = realPath(resolve(base, relative))
|
|
18
|
+
if (target !== base && !target.startsWith(base + sep)) throw new Error('path is outside TA_HOME')
|
|
19
|
+
return target
|
|
20
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs'
|
|
2
|
+
import { resolve } from 'node:path'
|
|
3
|
+
|
|
4
|
+
/** ReMe 会话 id → DSH 会话 id;只作诊断映射,不阻塞 capture。 */
|
|
5
|
+
export function recordSessionMap(config, dshSessionId, remeSessionId) {
|
|
6
|
+
try {
|
|
7
|
+
const path = resolve(config.taHome, config.sessionMap)
|
|
8
|
+
let value = {}
|
|
9
|
+
try {
|
|
10
|
+
value = JSON.parse(readFileSync(path, 'utf8'))
|
|
11
|
+
} catch {
|
|
12
|
+
value = {}
|
|
13
|
+
}
|
|
14
|
+
if (value[remeSessionId] === dshSessionId) return
|
|
15
|
+
value[remeSessionId] = dshSessionId
|
|
16
|
+
mkdirSync(resolve(config.taHome, 'state'), { recursive: true })
|
|
17
|
+
const temporary = `${path}.tmp`
|
|
18
|
+
writeFileSync(temporary, JSON.stringify(value, null, 2) + '\n')
|
|
19
|
+
renameSync(temporary, path)
|
|
20
|
+
} catch {
|
|
21
|
+
/* mapping is diagnostic; capture must not fail for it */
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function readInternalSessions(config) {
|
|
26
|
+
try {
|
|
27
|
+
const value = JSON.parse(readFileSync(resolve(config.taHome, config.internalSessions), 'utf8'))
|
|
28
|
+
return Array.isArray(value) ? value : (value && typeof value === 'object' ? value : {})
|
|
29
|
+
} catch {
|
|
30
|
+
return {}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function internalSessionIds(config) {
|
|
35
|
+
const value = readInternalSessions(config)
|
|
36
|
+
const ids = Array.isArray(value) ? value : Object.keys(value)
|
|
37
|
+
return new Set(ids.filter(id => typeof id === 'string'))
|
|
38
|
+
}
|