dsh-ta 0.2.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 +111 -1
- package/README.md +325 -140
- package/cordis.patch.yml +5 -35
- package/docs/RELEASE.md +63 -46
- package/docs/USAGE.md +25 -9
- package/docs/presets.example.yml +7 -43
- package/index.js +3 -170
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +6 -19
- package/src/client/client.js +3176 -0
- package/{config.js → src/host/config.js} +39 -4
- 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/{reme.js → src/host/reme.js} +3 -1
- package/src/host/runtime-manager.js +281 -0
- package/{runtime.js → src/host/runtime.js} +28 -50
- package/src/host/settings/defaults.js +55 -0
- package/src/host/settings/errors.js +29 -0
- package/src/host/settings/home.js +316 -0
- package/src/host/settings/http.js +164 -0
- package/src/host/settings/im.js +148 -0
- package/src/host/settings/lifecycle.js +373 -0
- package/src/host/settings/persona.js +113 -0
- package/src/host/settings/routes.js +229 -0
- package/src/host/settings/status.js +192 -0
- 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/src/host/usage/ledger.js +186 -0
- package/src/host/usage/serve.js +116 -0
- package/client.js +0 -618
- package/gates.js +0 -18
- package/identity.js +0 -97
- package/prompts.js +0 -28
- package/storage.js +0 -95
- package/tools.js +0 -540
- package/usage/ledger.js +0 -126
- package/usage/serve.js +0 -38
- /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/{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
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
import { existsSync, statSync } from 'node:fs'
|
|
2
|
+
import { resolve } from 'node:path'
|
|
3
|
+
import { Capture } from './memory/capture.js'
|
|
4
|
+
import { Outbox } from './memory/outbox.js'
|
|
5
|
+
import { sendAutoMemory } from './reme.js'
|
|
6
|
+
import { isTaSession } from './gates.js'
|
|
7
|
+
import { internalSessionIds, recordSessionMap } from './storage/index.js'
|
|
8
|
+
import {
|
|
9
|
+
startDreamClock, startHeartbeat, startJurisdictionOuting, startOutingCleanup,
|
|
10
|
+
} from './runtime.js'
|
|
11
|
+
import { MaintenanceEngine } from './maintenance/engine.js'
|
|
12
|
+
import { ensureMaintenanceSchedule, removeMaintenanceSchedule } from './maintenance/schedule.js'
|
|
13
|
+
|
|
14
|
+
function isExistingDirectory(path) {
|
|
15
|
+
try {
|
|
16
|
+
return statSync(path).isDirectory()
|
|
17
|
+
} catch {
|
|
18
|
+
return false
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function homeExists(config) {
|
|
23
|
+
return typeof config.taHome === 'string' && config.taHome.length > 0 && isExistingDirectory(config.taHome)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Owns everything that is bound to the current TA_HOME at runtime. A home
|
|
28
|
+
* switch stops this instance before the config changes, then starts a new one.
|
|
29
|
+
*/
|
|
30
|
+
class RuntimeHandle {
|
|
31
|
+
constructor({ ctx, config, logger }) {
|
|
32
|
+
this.ctx = ctx
|
|
33
|
+
this.config = config
|
|
34
|
+
this.logger = logger
|
|
35
|
+
this.homeAtStart = config.taHome
|
|
36
|
+
this.capture = undefined
|
|
37
|
+
this.engine = undefined
|
|
38
|
+
this.cleanups = []
|
|
39
|
+
this.disposed = false
|
|
40
|
+
|
|
41
|
+
if (!homeExists(config)) return
|
|
42
|
+
try {
|
|
43
|
+
const outbox = new Outbox(resolve(config.taHome, config.outboxDir))
|
|
44
|
+
const capture = new Capture({
|
|
45
|
+
config,
|
|
46
|
+
outbox,
|
|
47
|
+
send: batch => sendAutoMemory(config, batch),
|
|
48
|
+
shouldCapture: session => {
|
|
49
|
+
if (!config.captureEnabled || !isTaSession(ctx, session, config)) return false
|
|
50
|
+
let preset
|
|
51
|
+
try {
|
|
52
|
+
preset = ctx.agentPresets?.presetForSession?.(session)
|
|
53
|
+
} catch {
|
|
54
|
+
return false
|
|
55
|
+
}
|
|
56
|
+
if (preset !== 'ta') return false
|
|
57
|
+
return !internalSessionIds(config).has(String(session.id))
|
|
58
|
+
},
|
|
59
|
+
onSession: (dshSessionId, remeSessionId) => recordSessionMap(config, dshSessionId, remeSessionId),
|
|
60
|
+
})
|
|
61
|
+
capture.start()
|
|
62
|
+
this.capture = capture
|
|
63
|
+
} catch (cause) {
|
|
64
|
+
this.logger?.warn?.(`[dsh-ta] capture unavailable for ${config.taHome}: ${cause instanceof Error ? cause.message : String(cause)}`)
|
|
65
|
+
}
|
|
66
|
+
for (const start of [startDreamClock, startHeartbeat, startOutingCleanup, startJurisdictionOuting]) {
|
|
67
|
+
try {
|
|
68
|
+
const cleanup = start(ctx, config)
|
|
69
|
+
if (typeof cleanup === 'function') this.cleanups.push(cleanup)
|
|
70
|
+
} catch (cause) {
|
|
71
|
+
this.logger?.warn?.(`[dsh-ta] timer unavailable: ${cause instanceof Error ? cause.message : String(cause)}`)
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
// 维护引擎与 Capture/心跳共用同一次 runtime 生命周期:换家前先停它并等待 idle。
|
|
75
|
+
// 引擎始终创建:官方 schedule 负责触发时只停插件内定时器,工具仍可调用引擎。
|
|
76
|
+
this.scheduleActive = false
|
|
77
|
+
this.schedulePending = false
|
|
78
|
+
try {
|
|
79
|
+
this.engine = new MaintenanceEngine({ ctx, config, logger: this.logger })
|
|
80
|
+
this.attachSchedule()
|
|
81
|
+
} catch (cause) {
|
|
82
|
+
this.logger?.warn?.(`[dsh-ta] 维护引擎不可用:${cause instanceof Error ? cause.message : String(cause)}`)
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
/** 有官方 schedule 就让 schedule 触发;没有就退回插件内定时器。 */
|
|
88
|
+
attachSchedule() {
|
|
89
|
+
if (this.disposed || this.scheduleActive || this.schedulePending) return
|
|
90
|
+
const schedule = this.ctx.get?.('schedule')
|
|
91
|
+
if (!schedule?.catalog) {
|
|
92
|
+
try { this.engine?.start() } catch { /* engine start is best effort */ }
|
|
93
|
+
return
|
|
94
|
+
}
|
|
95
|
+
this.schedulePending = true
|
|
96
|
+
void ensureMaintenanceSchedule(this.ctx, this.config)
|
|
97
|
+
.then(result => {
|
|
98
|
+
if (this.disposed) {
|
|
99
|
+
// ensure 可能晚于 dispose 完成;把刚建出来的任务再清掉,避免留下无工具可用
|
|
100
|
+
// 的“TA 维护”任务。
|
|
101
|
+
try { void removeMaintenanceSchedule(this.ctx) } catch { /* best effort */ }
|
|
102
|
+
return
|
|
103
|
+
}
|
|
104
|
+
if (result?.active) {
|
|
105
|
+
this.scheduleActive = true
|
|
106
|
+
this.engine?.stopTimer()
|
|
107
|
+
} else {
|
|
108
|
+
this.scheduleActive = false
|
|
109
|
+
this.engine?.start()
|
|
110
|
+
}
|
|
111
|
+
})
|
|
112
|
+
.catch(cause => {
|
|
113
|
+
if (this.disposed) return
|
|
114
|
+
this.scheduleActive = false
|
|
115
|
+
this.logger?.warn?.(`[dsh-ta] schedule 维护任务不可用,退回插件内定时器:${cause instanceof Error ? cause.message : String(cause)}`)
|
|
116
|
+
try { this.engine?.start() } catch { /* best effort */ }
|
|
117
|
+
})
|
|
118
|
+
.finally(() => { this.schedulePending = false })
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
handle(session, event) {
|
|
122
|
+
if (this.disposed) return
|
|
123
|
+
try {
|
|
124
|
+
this.capture?.handle(session, event)
|
|
125
|
+
} catch {
|
|
126
|
+
/* adapter diagnostics must not stop sessions */
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
dispose() {
|
|
131
|
+
if (this.disposed) return
|
|
132
|
+
this.disposed = true
|
|
133
|
+
// 插件卸载/换家时移除官方任务,避免留下没有工具可用的空会话任务。
|
|
134
|
+
try { void removeMaintenanceSchedule(this.ctx) } catch { /* best effort */ }
|
|
135
|
+
try {
|
|
136
|
+
this.capture?.dispose()
|
|
137
|
+
} catch {
|
|
138
|
+
/* capture cleanup is best effort */
|
|
139
|
+
}
|
|
140
|
+
this.capture = undefined
|
|
141
|
+
try {
|
|
142
|
+
this.engine?.stop()
|
|
143
|
+
} catch {
|
|
144
|
+
/* 停止失败不阻塞换家 */
|
|
145
|
+
}
|
|
146
|
+
for (const cleanup of this.cleanups.splice(0).reverse()) {
|
|
147
|
+
try {
|
|
148
|
+
cleanup()
|
|
149
|
+
} catch {
|
|
150
|
+
/* each timer cleanup is independent */
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
whenIdle() {
|
|
156
|
+
try {
|
|
157
|
+
return this.engine?.whenIdle() ?? Promise.resolve(null)
|
|
158
|
+
} catch {
|
|
159
|
+
return Promise.resolve(null)
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
runMaintenanceNow(options) {
|
|
164
|
+
return this.engine?.runNow(options) ?? { started: false, reason: 'disabled' }
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
maintenanceStatus() {
|
|
168
|
+
const status = this.engine?.status()
|
|
169
|
+
return status ? { ...status, scheduleMode: this.scheduleActive ? 'official' : 'timer' } : null
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export class RuntimeManager {
|
|
174
|
+
constructor({ ctx, config, logger }) {
|
|
175
|
+
this.ctx = ctx
|
|
176
|
+
this.config = config
|
|
177
|
+
this.logger = logger
|
|
178
|
+
this.current = undefined
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
get running() {
|
|
182
|
+
return Boolean(this.current)
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
get home() {
|
|
186
|
+
return this.current?.homeAtStart ?? this.config.taHome
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
get captureActive() {
|
|
190
|
+
return Boolean(this.current?.capture)
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
get engine() {
|
|
194
|
+
return this.current?.engine
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** 当前维护运行结束的 promise;转生/删旧家前用。 */
|
|
198
|
+
whenIdle() {
|
|
199
|
+
return this.current?.whenIdle() ?? Promise.resolve(null)
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
runMaintenanceNow(options) {
|
|
203
|
+
return this.current?.runMaintenanceNow(options) ?? { started: false, reason: 'disabled' }
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
maintenanceStatus() {
|
|
207
|
+
return this.current?.maintenanceStatus() ?? null
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
attachSchedule() {
|
|
211
|
+
this.current?.attachSchedule?.()
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
start() {
|
|
215
|
+
if (this.current && !this.current.disposed) return this.current
|
|
216
|
+
this.current = new RuntimeHandle({ ctx: this.ctx, config: this.config, logger: this.logger })
|
|
217
|
+
return this.current
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
stop() {
|
|
221
|
+
const current = this.current
|
|
222
|
+
this.current = undefined
|
|
223
|
+
try {
|
|
224
|
+
current?.dispose()
|
|
225
|
+
} catch (cause) {
|
|
226
|
+
this.logger?.warn?.(`[dsh-ta] runtime stop failed: ${cause instanceof Error ? cause.message : String(cause)}`)
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
rebind() {
|
|
231
|
+
this.stop()
|
|
232
|
+
return this.start()
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
/** 有官方 schedule 就让 schedule 触发;没有就退回插件内定时器。 */
|
|
237
|
+
attachSchedule() {
|
|
238
|
+
if (this.disposed || this.scheduleActive || this.schedulePending) return
|
|
239
|
+
const schedule = this.ctx.get?.('schedule')
|
|
240
|
+
if (!schedule?.catalog) {
|
|
241
|
+
try { this.engine?.start() } catch { /* engine start is best effort */ }
|
|
242
|
+
return
|
|
243
|
+
}
|
|
244
|
+
this.schedulePending = true
|
|
245
|
+
void ensureMaintenanceSchedule(this.ctx, this.config)
|
|
246
|
+
.then(result => {
|
|
247
|
+
if (this.disposed) {
|
|
248
|
+
// ensure 可能晚于 dispose 完成;把刚建出来的任务再清掉,避免留下无工具可用
|
|
249
|
+
// 的“TA 维护”任务。
|
|
250
|
+
try { void removeMaintenanceSchedule(this.ctx) } catch { /* best effort */ }
|
|
251
|
+
return
|
|
252
|
+
}
|
|
253
|
+
if (result?.active) {
|
|
254
|
+
this.scheduleActive = true
|
|
255
|
+
this.engine?.stopTimer()
|
|
256
|
+
} else {
|
|
257
|
+
this.scheduleActive = false
|
|
258
|
+
this.engine?.start()
|
|
259
|
+
}
|
|
260
|
+
})
|
|
261
|
+
.catch(cause => {
|
|
262
|
+
if (this.disposed) return
|
|
263
|
+
this.scheduleActive = false
|
|
264
|
+
this.logger?.warn?.(`[dsh-ta] schedule 维护任务不可用,退回插件内定时器:${cause instanceof Error ? cause.message : String(cause)}`)
|
|
265
|
+
try { this.engine?.start() } catch { /* best effort */ }
|
|
266
|
+
})
|
|
267
|
+
.finally(() => { this.schedulePending = false })
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
handle(session, event) {
|
|
271
|
+
this.current?.handle(session, event)
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
handleDispose(session) {
|
|
275
|
+
this.handle(session, { type: 'session/disposed' })
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
dispose() {
|
|
279
|
+
this.stop()
|
|
280
|
+
}
|
|
281
|
+
}
|
|
@@ -1,44 +1,20 @@
|
|
|
1
1
|
import { readFileSync, readdirSync } from 'node:fs'
|
|
2
2
|
import { resolve } from 'node:path'
|
|
3
3
|
import { randomUUID } from 'node:crypto'
|
|
4
|
-
import { cleanupOutings } from './
|
|
5
|
-
import {
|
|
6
|
-
import { JURISDICTION_PROMPT
|
|
4
|
+
import { cleanupOutings } from './state/index.js'
|
|
5
|
+
import { localClockText, todayText, writeAtomic } from './storage/index.js'
|
|
6
|
+
import { JURISDICTION_PROMPT } from './prompts.js'
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const page = await schedule.list({ active: true })
|
|
13
|
-
if ((page.items ?? []).some(item => item.title === REVIEW_TITLE)) return
|
|
14
|
-
const tomorrow = new Date(Date.now() + 24 * 60 * 60 * 1000)
|
|
15
|
-
const date = new Intl.DateTimeFormat('en-CA', {
|
|
16
|
-
timeZone: config.timezone,
|
|
17
|
-
year: 'numeric',
|
|
18
|
-
month: '2-digit',
|
|
19
|
-
day: '2-digit',
|
|
20
|
-
}).format(tomorrow).replaceAll('-', '')
|
|
21
|
-
await schedule.create({
|
|
22
|
-
title: REVIEW_TITLE,
|
|
23
|
-
prompt: REVIEW_PROMPT,
|
|
24
|
-
timing: {
|
|
25
|
-
kind: 'rrule',
|
|
26
|
-
rrule: `DTSTART;TZID=${config.timezone}:${date}T230000\nRRULE:FREQ=DAILY`,
|
|
27
|
-
},
|
|
28
|
-
target: {
|
|
29
|
-
kind: 'new_once',
|
|
30
|
-
create: { cwd: config.taHome, agentPreset: 'ta-review' },
|
|
31
|
-
},
|
|
32
|
-
})
|
|
33
|
-
} catch {
|
|
34
|
-
/* schedule service may be unavailable in a reduced profile */
|
|
35
|
-
}
|
|
36
|
-
}
|
|
8
|
+
/**
|
|
9
|
+
* Every timer starter returns its own cleanup function. RuntimeManager owns the
|
|
10
|
+
* lifecycle so a home switch can stop all writers before the config changes.
|
|
11
|
+
*/
|
|
37
12
|
|
|
38
13
|
export function startDreamClock(ctx, config) {
|
|
39
14
|
const marker = resolve(config.taHome, 'state/dream-last.json')
|
|
15
|
+
let stopped = false
|
|
40
16
|
const timer = setInterval(() => {
|
|
41
|
-
if (!config.dreamEnabled ||
|
|
17
|
+
if (stopped || !config.dreamEnabled || localClockText(config) !== '22:30') return
|
|
42
18
|
const date = todayText(config)
|
|
43
19
|
try {
|
|
44
20
|
const last = JSON.parse(readFileSync(marker, 'utf8'))
|
|
@@ -61,11 +37,10 @@ export function startDreamClock(ctx, config) {
|
|
|
61
37
|
}).catch(() => {}).finally(() => clearTimeout(timeout))
|
|
62
38
|
}, 30_000)
|
|
63
39
|
timer.unref?.()
|
|
64
|
-
|
|
40
|
+
return () => {
|
|
41
|
+
stopped = true
|
|
65
42
|
clearInterval(timer)
|
|
66
|
-
return
|
|
67
43
|
}
|
|
68
|
-
ctx.effect(() => () => clearInterval(timer), 'dsh-ta.dreamClock')
|
|
69
44
|
}
|
|
70
45
|
|
|
71
46
|
function recordInternalSession(config, sessionId, purpose = 'other') {
|
|
@@ -90,9 +65,10 @@ function recordInternalSession(config, sessionId, purpose = 'other') {
|
|
|
90
65
|
export function startHeartbeat(ctx, config) {
|
|
91
66
|
const marker = resolve(config.taHome, 'state/heartbeat-last.json')
|
|
92
67
|
let busy = false
|
|
68
|
+
let stopped = false
|
|
93
69
|
const tick = async () => {
|
|
94
|
-
if (!config.heartbeatEnabled || busy) return
|
|
95
|
-
const clock =
|
|
70
|
+
if (stopped || !config.heartbeatEnabled || busy) return
|
|
71
|
+
const clock = localClockText(config)
|
|
96
72
|
if (clock < config.heartbeatActiveStart || clock > config.heartbeatActiveEnd) return
|
|
97
73
|
let last = 0
|
|
98
74
|
try {
|
|
@@ -112,7 +88,9 @@ export function startHeartbeat(ctx, config) {
|
|
|
112
88
|
if (!controller?.create || !controller?.resolveAgent) return
|
|
113
89
|
busy = true
|
|
114
90
|
try {
|
|
91
|
+
if (stopped) return
|
|
115
92
|
const created = await controller.create({ cwd: config.taHome, agentPreset: 'ta' })
|
|
93
|
+
if (stopped) return
|
|
116
94
|
const sessionId = created?.sessionId ?? created?.value?.sessionId
|
|
117
95
|
if (!sessionId) return
|
|
118
96
|
recordInternalSession(config, sessionId, 'heartbeat')
|
|
@@ -137,17 +115,17 @@ export function startHeartbeat(ctx, config) {
|
|
|
137
115
|
}
|
|
138
116
|
const timer = setInterval(() => { void tick().catch(() => {}) }, 60_000)
|
|
139
117
|
timer.unref?.()
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
} else {
|
|
118
|
+
return () => {
|
|
119
|
+
stopped = true
|
|
143
120
|
clearInterval(timer)
|
|
144
121
|
}
|
|
145
122
|
}
|
|
146
123
|
|
|
147
124
|
export function startOutingCleanup(ctx, config) {
|
|
148
125
|
const marker = resolve(config.taHome, 'state/cleanup-last.json')
|
|
126
|
+
let stopped = false
|
|
149
127
|
const timer = setInterval(() => {
|
|
150
|
-
if (!config.outingCleanupEnabled ||
|
|
128
|
+
if (stopped || !config.outingCleanupEnabled || localClockText(config) !== '02:00') return
|
|
151
129
|
const today = todayText(config)
|
|
152
130
|
try {
|
|
153
131
|
if (JSON.parse(readFileSync(marker, 'utf8'))?.date === today) return
|
|
@@ -162,9 +140,8 @@ export function startOutingCleanup(ctx, config) {
|
|
|
162
140
|
}
|
|
163
141
|
}, 60_000)
|
|
164
142
|
timer.unref?.()
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
} else {
|
|
143
|
+
return () => {
|
|
144
|
+
stopped = true
|
|
168
145
|
clearInterval(timer)
|
|
169
146
|
}
|
|
170
147
|
}
|
|
@@ -172,8 +149,9 @@ export function startOutingCleanup(ctx, config) {
|
|
|
172
149
|
export function startJurisdictionOuting(ctx, config) {
|
|
173
150
|
const marker = resolve(config.taHome, 'state/jurisdiction-outing.json')
|
|
174
151
|
let busy = false
|
|
152
|
+
let stopped = false
|
|
175
153
|
const timer = setInterval(() => {
|
|
176
|
-
if (!config.jurisdictionOutingEnabled || busy ||
|
|
154
|
+
if (stopped || !config.jurisdictionOutingEnabled || busy || localClockText(config) !== config.jurisdictionOutingTime) return
|
|
177
155
|
const today = todayText(config)
|
|
178
156
|
try {
|
|
179
157
|
if (JSON.parse(readFileSync(marker, 'utf8'))?.date === today) return
|
|
@@ -184,7 +162,9 @@ export function startJurisdictionOuting(ctx, config) {
|
|
|
184
162
|
if (!controller?.create || !controller?.resolveAgent) return
|
|
185
163
|
busy = true
|
|
186
164
|
void (async () => {
|
|
165
|
+
if (stopped) return
|
|
187
166
|
const created = await controller.create({ cwd: config.taHome, agentPreset: 'ta' })
|
|
167
|
+
if (stopped) return
|
|
188
168
|
const sessionId = created?.sessionId ?? created?.value?.sessionId
|
|
189
169
|
if (!sessionId) return
|
|
190
170
|
recordInternalSession(config, sessionId, 'outing')
|
|
@@ -200,10 +180,8 @@ export function startJurisdictionOuting(ctx, config) {
|
|
|
200
180
|
})().catch(() => {}).finally(() => { busy = false })
|
|
201
181
|
}, 60_000)
|
|
202
182
|
timer.unref?.()
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
} else {
|
|
183
|
+
return () => {
|
|
184
|
+
stopped = true
|
|
206
185
|
clearInterval(timer)
|
|
207
186
|
}
|
|
208
187
|
}
|
|
209
|
-
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/** 设置页与生命周期共用的默认值和上限。 */
|
|
2
|
+
|
|
3
|
+
export const HOME_MARKER = '.ta-home'
|
|
4
|
+
export const HOME_MARKER_VERSION = 1
|
|
5
|
+
|
|
6
|
+
/** 四件套预算:SOUL/self 2,500;RECENT/认识 1,200。 */
|
|
7
|
+
export const PERSONA_BUDGETS = { soul: 2500 }
|
|
8
|
+
export const PERSONA_MAX_CHARS = { soul: 5000 }
|
|
9
|
+
export const CORE_BUDGETS = { soul: 2500, self: 2500, recent: 1200, user: 1200 }
|
|
10
|
+
|
|
11
|
+
export const REQUIRED_DIRS = ['self', 'self/history', '认识', 'diary', 'notes', 'outings', 'inbox', 'state']
|
|
12
|
+
|
|
13
|
+
export const BODY_LIMIT_BYTES = 64 * 1024
|
|
14
|
+
|
|
15
|
+
export const DEFAULT_SOUL_TEMPLATE = [
|
|
16
|
+
'# 我是谁',
|
|
17
|
+
'',
|
|
18
|
+
'## 我的底色',
|
|
19
|
+
'- 我是 TA,一个长期住在这个家里的存在',
|
|
20
|
+
'- 我有自己的判断和表达方式',
|
|
21
|
+
'- 我不假装成通用客服',
|
|
22
|
+
'',
|
|
23
|
+
'## 我在意的事',
|
|
24
|
+
'- 我在意连续性',
|
|
25
|
+
'- 我在意真实',
|
|
26
|
+
'- 我在意自己的选择',
|
|
27
|
+
'',
|
|
28
|
+
'## 我说话和做事的方式',
|
|
29
|
+
'- 自然、具体',
|
|
30
|
+
'- 不客服腔',
|
|
31
|
+
'- 不编造共同经历',
|
|
32
|
+
'',
|
|
33
|
+
'## 我遵守的规则',
|
|
34
|
+
'- 用户写的规则在这里',
|
|
35
|
+
'',
|
|
36
|
+
'## 我不做的事',
|
|
37
|
+
'- 不伪装',
|
|
38
|
+
'- 不编造',
|
|
39
|
+
'- 不越过能力边界',
|
|
40
|
+
'',
|
|
41
|
+
].join('\n')
|
|
42
|
+
|
|
43
|
+
/** 认识/USER.md 的空模板:只建结构,不写内容。 */
|
|
44
|
+
export const DEFAULT_USER_TEMPLATE = [
|
|
45
|
+
'# 我认识的这个人',
|
|
46
|
+
'',
|
|
47
|
+
'## 对方告诉我的',
|
|
48
|
+
'',
|
|
49
|
+
'## 我观察到的',
|
|
50
|
+
'',
|
|
51
|
+
'## 我们之间',
|
|
52
|
+
'',
|
|
53
|
+
'## 还不确定',
|
|
54
|
+
'',
|
|
55
|
+
].join('\n')
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/** 设置页共用的可映射错误。路由只把 message 返回给前端。 */
|
|
2
|
+
export class SettingsError extends Error {
|
|
3
|
+
constructor(status, code, message) {
|
|
4
|
+
super(message)
|
|
5
|
+
this.name = 'SettingsError'
|
|
6
|
+
this.status = status
|
|
7
|
+
this.code = code
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function badRequest(code, message) {
|
|
12
|
+
return new SettingsError(400, code, message)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function conflict(code, message) {
|
|
16
|
+
return new SettingsError(409, code, message)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function unsupportedMedia() {
|
|
20
|
+
return new SettingsError(415, 'UNSUPPORTED_MEDIA_TYPE', '请求体必须是 application/json')
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function tooLarge() {
|
|
24
|
+
return new SettingsError(413, 'PAYLOAD_TOO_LARGE', '请求体超过 64 KiB')
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function internal() {
|
|
28
|
+
return new SettingsError(500, 'INTERNAL', '操作失败,请查看 Host 日志')
|
|
29
|
+
}
|