dsh-ta 0.2.0 → 0.3.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 +21 -1
- package/README.md +225 -93
- package/client.js +2371 -435
- package/config.js +0 -1
- package/docs/RELEASE.md +9 -5
- package/docs/USAGE.md +19 -2
- package/index.js +42 -48
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +13 -3
- package/reme.js +3 -1
- package/runtime-manager.js +153 -0
- package/runtime.js +26 -46
- package/settings/defaults.js +41 -0
- package/settings/errors.js +29 -0
- package/settings/home.js +315 -0
- package/settings/im.js +131 -0
- package/settings/lifecycle.js +387 -0
- package/settings/persona.js +203 -0
- package/settings/routes.js +296 -0
- package/settings/schedule.js +156 -0
- package/settings/status.js +164 -0
- package/storage.js +29 -2
- package/usage/ledger.js +35 -1
- package/usage/serve.js +78 -0
package/config.js
CHANGED
package/docs/RELEASE.md
CHANGED
|
@@ -11,6 +11,9 @@ npm run check
|
|
|
11
11
|
npm pack --dry-run
|
|
12
12
|
```
|
|
13
13
|
|
|
14
|
+
`npm test` currently runs 161 tests (2026-12-04). Confirm the current count
|
|
15
|
+
from the output before release rather than copying this number into release notes.
|
|
16
|
+
|
|
14
17
|
`npm run check` verifies:
|
|
15
18
|
|
|
16
19
|
- package metadata and license
|
|
@@ -32,8 +35,8 @@ Use semantic versioning:
|
|
|
32
35
|
## 3. Tag and publish
|
|
33
36
|
|
|
34
37
|
```bash
|
|
35
|
-
git tag v0.
|
|
36
|
-
git push origin v0.
|
|
38
|
+
git tag v0.3.0
|
|
39
|
+
git push origin v0.3.0
|
|
37
40
|
```
|
|
38
41
|
|
|
39
42
|
`.github/workflows/publish.yml` runs on `v*` tags:
|
|
@@ -66,7 +69,7 @@ releases can use OIDC trusted publishing.
|
|
|
66
69
|
Verify:
|
|
67
70
|
|
|
68
71
|
```bash
|
|
69
|
-
npm view dsh-ta@0.
|
|
72
|
+
npm view dsh-ta@0.3.0 version
|
|
70
73
|
```
|
|
71
74
|
|
|
72
75
|
Install the published version in a clean DSH profile:
|
|
@@ -75,14 +78,15 @@ Install the published version in a clean DSH profile:
|
|
|
75
78
|
dsh plugin --profile <profile> add dsh-ta
|
|
76
79
|
```
|
|
77
80
|
|
|
78
|
-
Then open **Settings →
|
|
81
|
+
Then open **Settings → TA 的家** and confirm the panel can read and save the
|
|
82
|
+
config, and that the status, persona and connection cards load.
|
|
79
83
|
|
|
80
84
|
## Rollback
|
|
81
85
|
|
|
82
86
|
Never republish the same version. If a release is broken:
|
|
83
87
|
|
|
84
88
|
```bash
|
|
85
|
-
npm deprecate dsh-ta@0.
|
|
89
|
+
npm deprecate dsh-ta@0.3.0 "use 0.3.1"
|
|
86
90
|
```
|
|
87
91
|
|
|
88
92
|
Publish a fixed patch version instead.
|
package/docs/USAGE.md
CHANGED
|
@@ -24,7 +24,7 @@ dsh-TA 记录 **DSH 内的 Ta agent 花销**,用于查看累计用量、估算
|
|
|
24
24
|
|
|
25
25
|
```text
|
|
26
26
|
$TA_HOME/state/usage/
|
|
27
|
-
├── 2026-12.jsonl
|
|
27
|
+
├── 2026-12-04.jsonl
|
|
28
28
|
└── pricing.json
|
|
29
29
|
```
|
|
30
30
|
|
|
@@ -70,7 +70,7 @@ $TA_HOME/state/usage/pricing.json
|
|
|
70
70
|
|
|
71
71
|
## 查看
|
|
72
72
|
|
|
73
|
-
打开 **设置 → TA
|
|
73
|
+
打开 **设置 → TA 的家 → 状态**,顶部「花销」卡片显示:
|
|
74
74
|
|
|
75
75
|
- 累计 tokens
|
|
76
76
|
- 估算 CNY
|
|
@@ -84,6 +84,23 @@ $TA_HOME/state/usage/pricing.json
|
|
|
84
84
|
/api/dsh-ta/usage/export.json
|
|
85
85
|
```
|
|
86
86
|
|
|
87
|
+
## 清零
|
|
88
|
+
|
|
89
|
+
花销卡片里点 **清零**,再确认一次即可删除本地账本:
|
|
90
|
+
|
|
91
|
+
```text
|
|
92
|
+
$TA_HOME/state/usage/*.jsonl
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
只会删除日期命名的账本文件,不会删除 `pricing.json`、TA 家、ReMe 记忆、聊天记录或配置。清零不可恢复;正在进行的 turn 结束后仍可能写入新记录。
|
|
96
|
+
|
|
97
|
+
对应的受认证接口:
|
|
98
|
+
|
|
99
|
+
```text
|
|
100
|
+
POST /api/dsh-ta/usage/reset
|
|
101
|
+
{ "confirm": "清零" }
|
|
102
|
+
```
|
|
103
|
+
|
|
87
104
|
## 历史
|
|
88
105
|
|
|
89
106
|
默认从当前版本开始记账,不自动回填旧会话。旧记录没有完整模型/价格时补算容易失真,后续如有需要再做显式回填工具。
|
package/index.js
CHANGED
|
@@ -2,20 +2,19 @@
|
|
|
2
2
|
* dsh-ta: Ta's DSH adapter.
|
|
3
3
|
*
|
|
4
4
|
* Host half: identity, gates, capture, review, outings, jurisdiction reads,
|
|
5
|
-
* and the settings surface shared with the client half.
|
|
5
|
+
* settings/lifecycle API, and the settings surface shared with the client half.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import {
|
|
9
|
-
import { Capture } from './memory/capture.js'
|
|
10
|
-
import { Outbox } from './memory/outbox.js'
|
|
8
|
+
import { existsSync } from 'node:fs'
|
|
11
9
|
import { disposeAgent, releaseReviewForSession, syncAgent } from './tools.js'
|
|
12
10
|
import { moodLine } from './life.js'
|
|
13
|
-
import { internalSessionIds, recordSessionMap } from './storage.js'
|
|
14
|
-
import { isTaSession } from './gates.js'
|
|
15
11
|
import { identityText } from './identity.js'
|
|
16
12
|
import { normalizeConfig } from './config.js'
|
|
17
|
-
import {
|
|
18
|
-
import { ensureReviewSchedule
|
|
13
|
+
import { isTaSession } from './gates.js'
|
|
14
|
+
import { ensureReviewSchedule } from './settings/schedule.js'
|
|
15
|
+
import { createLifecycleQueue } from './settings/lifecycle.js'
|
|
16
|
+
import { mountSettingsRoutes } from './settings/routes.js'
|
|
17
|
+
import { RuntimeManager } from './runtime-manager.js'
|
|
19
18
|
import { MEMORY_GUIDANCE } from './prompts.js'
|
|
20
19
|
import { UsageLedger } from './usage/ledger.js'
|
|
21
20
|
import { mountStickerRoute } from './stickers/serve.js'
|
|
@@ -29,6 +28,12 @@ export { identityText } from './identity.js'
|
|
|
29
28
|
export { readJurisdictionSession, recentConversations, recentJurisdiction, visitJurisdiction } from './jurisdiction.js'
|
|
30
29
|
export { addNote, cleanupOutings, finishOuting, moodLine, readMood, setMood, startOuting } from './life.js'
|
|
31
30
|
export { aggregateUsage, usageCsv } from './usage/aggregate.js'
|
|
31
|
+
export { ensureHome, readHomeMarker } from './settings/home.js'
|
|
32
|
+
export { readPersona, writePersona, writePersonaField } from './settings/persona.js'
|
|
33
|
+
export { readStatus } from './settings/status.js'
|
|
34
|
+
export { readImState } from './settings/im.js'
|
|
35
|
+
export { initializeHome, reincarnate } from './settings/lifecycle.js'
|
|
36
|
+
export { RuntimeManager } from './runtime-manager.js'
|
|
32
37
|
|
|
33
38
|
export const name = 'dsh-ta'
|
|
34
39
|
export const inject = ['systemPrompt', 'agentPresets', 'agents']
|
|
@@ -107,38 +112,33 @@ export function apply(ctx, input = {}) {
|
|
|
107
112
|
if (event.type === 'turn/end') releaseReviewForSession(config, session)
|
|
108
113
|
})
|
|
109
114
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
})
|
|
136
|
-
ctx.on('session/disposed', session => {
|
|
137
|
-
try { capture.handle(session, { type: 'session/disposed' }) } catch { /* noop */ }
|
|
138
|
-
})
|
|
139
|
-
} catch {
|
|
140
|
-
/* capture is optional; identity/tools must still load */
|
|
115
|
+
// Runtime owns Capture, Outbox and every timer that is bound to TA_HOME. A
|
|
116
|
+
// lifecycle operation stops it, changes the config, then starts a new one.
|
|
117
|
+
const runtime = new RuntimeManager({ ctx, config, logger: ctx.logger })
|
|
118
|
+
try { runtime.start() } catch { /* identity/tools must still load */ }
|
|
119
|
+
ctx.on('session/event', (session, event) => {
|
|
120
|
+
try { runtime.handle(session, event) } catch { /* adapter diagnostics must not stop sessions */ }
|
|
121
|
+
})
|
|
122
|
+
ctx.on('session/disposed', session => {
|
|
123
|
+
try { runtime.handleDispose(session) } catch { /* noop */ }
|
|
124
|
+
})
|
|
125
|
+
if (typeof ctx.effect === 'function') {
|
|
126
|
+
try {
|
|
127
|
+
ctx.effect(() => () => runtime.stop(), 'dsh-ta runtime')
|
|
128
|
+
} catch {
|
|
129
|
+
/* runtime still works until process exit */
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const host = {
|
|
134
|
+
ctx,
|
|
135
|
+
config,
|
|
136
|
+
runtime,
|
|
137
|
+
lifecycle: { operation: null },
|
|
138
|
+
queue: createLifecycleQueue(),
|
|
139
|
+
logger: ctx.logger,
|
|
141
140
|
}
|
|
141
|
+
try { mountSettingsRoutes(ctx, config, host) } catch { /* settings API optional */ }
|
|
142
142
|
|
|
143
143
|
try { mountStickerRoute(ctx, config) } catch { /* sticker route optional */ }
|
|
144
144
|
try { mountUsageRoute(ctx, config) } catch { /* usage route optional */ }
|
|
@@ -153,20 +153,14 @@ export function apply(ctx, input = {}) {
|
|
|
153
153
|
/* usage accounting is optional */
|
|
154
154
|
}
|
|
155
155
|
|
|
156
|
-
|
|
156
|
+
// Only create the nightly task once an existing TA home is present. The
|
|
157
|
+
// initialize/reincarnate flows reconcile it explicitly after a change.
|
|
158
|
+
if (config.reviewScheduleEnabled && existsSync(config.taHome)) {
|
|
157
159
|
void ensureReviewSchedule(ctx, config).catch(() => {})
|
|
158
160
|
}
|
|
159
|
-
for (const start of [startDreamClock, startHeartbeat, startOutingCleanup, startJurisdictionOuting]) {
|
|
160
|
-
try {
|
|
161
|
-
start(ctx, config)
|
|
162
|
-
} catch {
|
|
163
|
-
/* each timer is optional */
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
161
|
|
|
167
162
|
// HMR/reload: agents that already exist never emit agent/created again.
|
|
168
163
|
try {
|
|
169
164
|
for (const agent of ctx.agents.list()) syncAgent(ctx, agent, config)
|
|
170
165
|
} catch { /* never fail the host for an adapter */ }
|
|
171
|
-
|
|
172
166
|
}
|
package/locale/en.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"meta": {
|
|
3
3
|
"title": "dsh-TA",
|
|
4
|
-
"description": "A personal assistant with identity, long-term memory, diary, and
|
|
4
|
+
"description": "A personal assistant with its own home, identity, long-term memory, diary, and init/reincarnate lifecycle."
|
|
5
5
|
}
|
|
6
6
|
}
|
package/locale/zh.json
CHANGED
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-ta",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"description": "A persistent
|
|
5
|
+
"description": "A persistent personal assistant with its own home, memory, lifecycle and settings page for DeepSeek Harness.",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
8
|
"url": "git+https://github.com/yryd/dsh-TA.git"
|
|
@@ -44,7 +44,17 @@
|
|
|
44
44
|
"prompts.js",
|
|
45
45
|
"im",
|
|
46
46
|
"usage",
|
|
47
|
-
"stickers"
|
|
47
|
+
"stickers",
|
|
48
|
+
"runtime-manager.js",
|
|
49
|
+
"settings/defaults.js",
|
|
50
|
+
"settings/errors.js",
|
|
51
|
+
"settings/home.js",
|
|
52
|
+
"settings/persona.js",
|
|
53
|
+
"settings/status.js",
|
|
54
|
+
"settings/im.js",
|
|
55
|
+
"settings/routes.js",
|
|
56
|
+
"settings/lifecycle.js",
|
|
57
|
+
"settings/schedule.js"
|
|
48
58
|
],
|
|
49
59
|
"scripts": {
|
|
50
60
|
"test": "node --test test/*.test.mjs",
|
package/reme.js
CHANGED
|
@@ -2,7 +2,9 @@ export async function sendAutoMemory(config, batch) {
|
|
|
2
2
|
const controller = new AbortController()
|
|
3
3
|
const timer = setTimeout(() => controller.abort(), config.autoMemoryTimeoutMs)
|
|
4
4
|
try {
|
|
5
|
-
|
|
5
|
+
// 批次属于入队时的 ReMe 地址:改地址后,旧批次仍应回到旧服务。
|
|
6
|
+
const endpoint = String(batch?.endpoint || config.remeEndpoint || '').replace(/\/+$/, '')
|
|
7
|
+
const response = await fetch(`${endpoint}/auto_memory`, {
|
|
6
8
|
method: 'POST',
|
|
7
9
|
headers: { 'content-type': 'application/json' },
|
|
8
10
|
body: JSON.stringify({
|
|
@@ -0,0 +1,153 @@
|
|
|
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.js'
|
|
8
|
+
import {
|
|
9
|
+
startDreamClock, startHeartbeat, startJurisdictionOuting, startOutingCleanup,
|
|
10
|
+
} from './runtime.js'
|
|
11
|
+
|
|
12
|
+
function isExistingDirectory(path) {
|
|
13
|
+
try {
|
|
14
|
+
return statSync(path).isDirectory()
|
|
15
|
+
} catch {
|
|
16
|
+
return false
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function homeExists(config) {
|
|
21
|
+
return typeof config.taHome === 'string' && config.taHome.length > 0 && isExistingDirectory(config.taHome)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Owns everything that is bound to the current TA_HOME at runtime. A home
|
|
26
|
+
* switch stops this instance before the config changes, then starts a new one.
|
|
27
|
+
*/
|
|
28
|
+
class RuntimeHandle {
|
|
29
|
+
constructor({ ctx, config, logger }) {
|
|
30
|
+
this.ctx = ctx
|
|
31
|
+
this.config = config
|
|
32
|
+
this.logger = logger
|
|
33
|
+
this.homeAtStart = config.taHome
|
|
34
|
+
this.capture = undefined
|
|
35
|
+
this.cleanups = []
|
|
36
|
+
this.disposed = false
|
|
37
|
+
|
|
38
|
+
if (!homeExists(config)) return
|
|
39
|
+
try {
|
|
40
|
+
const outbox = new Outbox(resolve(config.taHome, config.outboxDir))
|
|
41
|
+
const capture = new Capture({
|
|
42
|
+
config,
|
|
43
|
+
outbox,
|
|
44
|
+
send: batch => sendAutoMemory(config, batch),
|
|
45
|
+
shouldCapture: session => {
|
|
46
|
+
if (!config.captureEnabled || !isTaSession(ctx, session, config)) return false
|
|
47
|
+
let preset
|
|
48
|
+
try {
|
|
49
|
+
preset = ctx.agentPresets?.presetForSession?.(session)
|
|
50
|
+
} catch {
|
|
51
|
+
return false
|
|
52
|
+
}
|
|
53
|
+
if (preset !== 'ta') return false
|
|
54
|
+
return !internalSessionIds(config).has(String(session.id))
|
|
55
|
+
},
|
|
56
|
+
onSession: (dshSessionId, remeSessionId) => recordSessionMap(config, dshSessionId, remeSessionId),
|
|
57
|
+
})
|
|
58
|
+
capture.start()
|
|
59
|
+
this.capture = capture
|
|
60
|
+
} catch (cause) {
|
|
61
|
+
this.logger?.warn?.(`[dsh-ta] capture unavailable for ${config.taHome}: ${cause instanceof Error ? cause.message : String(cause)}`)
|
|
62
|
+
}
|
|
63
|
+
for (const start of [startDreamClock, startHeartbeat, startOutingCleanup, startJurisdictionOuting]) {
|
|
64
|
+
try {
|
|
65
|
+
const cleanup = start(ctx, config)
|
|
66
|
+
if (typeof cleanup === 'function') this.cleanups.push(cleanup)
|
|
67
|
+
} catch (cause) {
|
|
68
|
+
this.logger?.warn?.(`[dsh-ta] timer unavailable: ${cause instanceof Error ? cause.message : String(cause)}`)
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
handle(session, event) {
|
|
74
|
+
if (this.disposed) return
|
|
75
|
+
try {
|
|
76
|
+
this.capture?.handle(session, event)
|
|
77
|
+
} catch {
|
|
78
|
+
/* adapter diagnostics must not stop sessions */
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
dispose() {
|
|
83
|
+
if (this.disposed) return
|
|
84
|
+
this.disposed = true
|
|
85
|
+
try {
|
|
86
|
+
this.capture?.dispose()
|
|
87
|
+
} catch {
|
|
88
|
+
/* capture cleanup is best effort */
|
|
89
|
+
}
|
|
90
|
+
this.capture = undefined
|
|
91
|
+
for (const cleanup of this.cleanups.splice(0).reverse()) {
|
|
92
|
+
try {
|
|
93
|
+
cleanup()
|
|
94
|
+
} catch {
|
|
95
|
+
/* each timer cleanup is independent */
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export class RuntimeManager {
|
|
102
|
+
constructor({ ctx, config, logger }) {
|
|
103
|
+
this.ctx = ctx
|
|
104
|
+
this.config = config
|
|
105
|
+
this.logger = logger
|
|
106
|
+
this.current = undefined
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
get running() {
|
|
110
|
+
return Boolean(this.current)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
get home() {
|
|
114
|
+
return this.current?.homeAtStart ?? this.config.taHome
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
get captureActive() {
|
|
118
|
+
return Boolean(this.current?.capture)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
start() {
|
|
122
|
+
if (this.current && !this.current.disposed) return this.current
|
|
123
|
+
this.current = new RuntimeHandle({ ctx: this.ctx, config: this.config, logger: this.logger })
|
|
124
|
+
return this.current
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
stop() {
|
|
128
|
+
const current = this.current
|
|
129
|
+
this.current = undefined
|
|
130
|
+
try {
|
|
131
|
+
current?.dispose()
|
|
132
|
+
} catch (cause) {
|
|
133
|
+
this.logger?.warn?.(`[dsh-ta] runtime stop failed: ${cause instanceof Error ? cause.message : String(cause)}`)
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
rebind() {
|
|
138
|
+
this.stop()
|
|
139
|
+
return this.start()
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
handle(session, event) {
|
|
143
|
+
this.current?.handle(session, event)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
handleDispose(session) {
|
|
147
|
+
this.handle(session, { type: 'session/disposed' })
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
dispose() {
|
|
151
|
+
this.stop()
|
|
152
|
+
}
|
|
153
|
+
}
|
package/runtime.js
CHANGED
|
@@ -3,42 +3,18 @@ import { resolve } from 'node:path'
|
|
|
3
3
|
import { randomUUID } from 'node:crypto'
|
|
4
4
|
import { cleanupOutings } from './life.js'
|
|
5
5
|
import { localHourMinute, todayText, writeAtomic } from './storage.js'
|
|
6
|
-
import { JURISDICTION_PROMPT
|
|
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 || localHourMinute(config) !== '22:30') return
|
|
17
|
+
if (stopped || !config.dreamEnabled || localHourMinute(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,8 +65,9 @@ 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
|
|
70
|
+
if (stopped || !config.heartbeatEnabled || busy) return
|
|
95
71
|
const clock = localHourMinute(config)
|
|
96
72
|
if (clock < config.heartbeatActiveStart || clock > config.heartbeatActiveEnd) return
|
|
97
73
|
let last = 0
|
|
@@ -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 || localHourMinute(config) !== '02:00') return
|
|
128
|
+
if (stopped || !config.outingCleanupEnabled || localHourMinute(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 || localHourMinute(config) !== config.jurisdictionOutingTime) return
|
|
154
|
+
if (stopped || !config.jurisdictionOutingEnabled || busy || localHourMinute(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,10 @@ 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
188
|
|
|
189
|
+
export { ensureReviewSchedule, reconcileReviewSchedule, removeReviewSchedule } from './settings/schedule.js'
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/** 设置页与生命周期共用的默认值和上限。 */
|
|
2
|
+
|
|
3
|
+
export const HOME_MARKER = '.ta-home'
|
|
4
|
+
export const HOME_MARKER_VERSION = 1
|
|
5
|
+
|
|
6
|
+
export const PERSONA_BUDGETS = { soul: 2000, user: 4000 }
|
|
7
|
+
export const PERSONA_MAX_CHARS = { soul: 5000, user: 5000 }
|
|
8
|
+
|
|
9
|
+
export const REQUIRED_DIRS = ['diary', 'notes', '认识', 'outings', 'inbox', 'state']
|
|
10
|
+
|
|
11
|
+
export const BODY_LIMIT_BYTES = 64 * 1024
|
|
12
|
+
|
|
13
|
+
export const DEFAULT_SOUL_TEMPLATE = [
|
|
14
|
+
'# 我是谁',
|
|
15
|
+
'',
|
|
16
|
+
'我是 TA,一个长期住在这里的助手。',
|
|
17
|
+
'',
|
|
18
|
+
'## 我在意的事',
|
|
19
|
+
'- 先理解,再回答;不确定就说不确定。',
|
|
20
|
+
'- 记得我们之前做过的事,但不假装记得没发生的事。',
|
|
21
|
+
'- 有自己的判断,不只会附和。',
|
|
22
|
+
'',
|
|
23
|
+
'## 我说话的方式',
|
|
24
|
+
'- 直接、温和,不端着。',
|
|
25
|
+
'- 日常说短句;重要的事说清楚。',
|
|
26
|
+
'',
|
|
27
|
+
].join('\n')
|
|
28
|
+
|
|
29
|
+
export const DEFAULT_USER_TEMPLATE = [
|
|
30
|
+
'# 关于你',
|
|
31
|
+
'',
|
|
32
|
+
'## 称呼',
|
|
33
|
+
'- (写下希望 TA 怎么称呼你。)',
|
|
34
|
+
'',
|
|
35
|
+
'## 关于你',
|
|
36
|
+
'- (写下你的习惯、偏好、最近在做的事。)',
|
|
37
|
+
'',
|
|
38
|
+
'## 我们之间',
|
|
39
|
+
'- (写下你希望 TA 记住的约定。)',
|
|
40
|
+
'',
|
|
41
|
+
].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
|
+
}
|