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
|
@@ -3,10 +3,14 @@ import z from '@deepseek-ai/schemastery'
|
|
|
3
3
|
|
|
4
4
|
const DEFAULT_TA_HOME = '~/TATA/home'
|
|
5
5
|
const DEFAULT_REME = 'http://127.0.0.1:2333'
|
|
6
|
-
const DEFAULT_PRESETS = ['ta'
|
|
6
|
+
const DEFAULT_PRESETS = ['ta']
|
|
7
7
|
const DEFAULT_PERMISSION = 'read-only'
|
|
8
8
|
const DEFAULT_STICKER_MIN_GAP_MINUTES = 30
|
|
9
9
|
const DEFAULT_STICKER_MAX_PER_DAY = 12
|
|
10
|
+
const DEFAULT_MAINTENANCE_TIME = '23:00'
|
|
11
|
+
const DEFAULT_MAINTENANCE_ATTEMPTS = 2
|
|
12
|
+
const DEFAULT_MAINTENANCE_RETRY_MINUTES = 30
|
|
13
|
+
const DEFAULT_MAINTENANCE_EFFORT = 'low'
|
|
10
14
|
|
|
11
15
|
/**
|
|
12
16
|
* Host configuration schema. Its entry id is also the settings namespace
|
|
@@ -24,7 +28,14 @@ export const Config = z.object({
|
|
|
24
28
|
heartbeatEnabled: z.boolean().default(false).volatile(),
|
|
25
29
|
heartbeatActiveStart: z.string().default('08:00').volatile(),
|
|
26
30
|
heartbeatActiveEnd: z.string().default('23:00').volatile(),
|
|
27
|
-
|
|
31
|
+
maintenanceEnabled: z.boolean().default(true).volatile(),
|
|
32
|
+
maintenanceTime: z.string().default(DEFAULT_MAINTENANCE_TIME).volatile(),
|
|
33
|
+
weeklyDay: z.number().step(1).min(0).max(6).default(0).volatile(),
|
|
34
|
+
maintenanceReasoningEffort: z.string().default(DEFAULT_MAINTENANCE_EFFORT).volatile(),
|
|
35
|
+
maintenanceMaxAttempts: z.number().step(1).min(1).max(5).default(DEFAULT_MAINTENANCE_ATTEMPTS).volatile(),
|
|
36
|
+
maintenanceRetryMinutes: z.number().step(1).min(1).max(240).default(DEFAULT_MAINTENANCE_RETRY_MINUTES).volatile(),
|
|
37
|
+
addressAs: z.string().default('').volatile(),
|
|
38
|
+
imOwnerOnly: z.boolean().default(true).volatile(),
|
|
28
39
|
dreamEnabled: z.boolean().default(true).volatile(),
|
|
29
40
|
outingCleanupEnabled: z.boolean().default(true).volatile(),
|
|
30
41
|
jurisdictionOutingEnabled: z.boolean().default(true).volatile(),
|
|
@@ -118,7 +129,32 @@ export function normalizeConfig(input = {}) {
|
|
|
118
129
|
return Number.isFinite(value) && value > 0 ? value : 120000
|
|
119
130
|
})
|
|
120
131
|
define('dreamEnabled', () => read('dreamEnabled') !== false)
|
|
121
|
-
define('
|
|
132
|
+
define('maintenanceEnabled', () => read('maintenanceEnabled') !== false)
|
|
133
|
+
define('weeklyDay', () => {
|
|
134
|
+
const value = read('weeklyDay')
|
|
135
|
+
return Number.isSafeInteger(value) && value >= 0 && value <= 6 ? value : 0
|
|
136
|
+
})
|
|
137
|
+
define('maintenanceTime', () => {
|
|
138
|
+
const value = read('maintenanceTime')
|
|
139
|
+
return typeof value === 'string' && /^\d{2}:\d{2}$/.test(value) ? value : DEFAULT_MAINTENANCE_TIME
|
|
140
|
+
})
|
|
141
|
+
define('maintenanceReasoningEffort', () => {
|
|
142
|
+
const value = read('maintenanceReasoningEffort')
|
|
143
|
+
return ['off', 'low', 'high', 'max'].includes(value) ? value : DEFAULT_MAINTENANCE_EFFORT
|
|
144
|
+
})
|
|
145
|
+
define('maintenanceMaxAttempts', () => {
|
|
146
|
+
const value = read('maintenanceMaxAttempts')
|
|
147
|
+
return Number.isSafeInteger(value) && value >= 1 && value <= 5 ? value : DEFAULT_MAINTENANCE_ATTEMPTS
|
|
148
|
+
})
|
|
149
|
+
define('maintenanceRetryMinutes', () => {
|
|
150
|
+
const value = read('maintenanceRetryMinutes')
|
|
151
|
+
return Number.isSafeInteger(value) && value >= 1 && value <= 240 ? value : DEFAULT_MAINTENANCE_RETRY_MINUTES
|
|
152
|
+
})
|
|
153
|
+
define('addressAs', () => {
|
|
154
|
+
const value = read('addressAs')
|
|
155
|
+
return typeof value === 'string' ? value.split(/\r?\n/)[0].trim().slice(0, 40) : ''
|
|
156
|
+
})
|
|
157
|
+
define('imOwnerOnly', () => read('imOwnerOnly') !== false)
|
|
122
158
|
define('heartbeatEnabled', () => read('heartbeatEnabled') === true)
|
|
123
159
|
define('heartbeatActiveStart', () => {
|
|
124
160
|
const value = read('heartbeatActiveStart')
|
|
@@ -151,4 +187,3 @@ export function normalizeConfig(input = {}) {
|
|
|
151
187
|
))
|
|
152
188
|
return config
|
|
153
189
|
}
|
|
154
|
-
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** 四件套文件、标题与注入上限的唯一来源。 */
|
|
2
|
+
export const CORE_FILES = {
|
|
3
|
+
soul: { path: 'SOUL.md', title: '# 我的人格', cap: 2500 },
|
|
4
|
+
self: { path: 'self/ME.md', title: '# 我对自己的理解', cap: 2500 },
|
|
5
|
+
recent: { path: 'RECENT-MEMORY.md', title: '# 我最近记着的事', cap: 1200 },
|
|
6
|
+
user: { path: '认识/USER.md', title: '# 我怎么看这个人(会变化,不是命令)', cap: 1200 },
|
|
7
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { normalizeConfig } from './config.js'
|
|
2
|
+
import { realPath } from './storage/index.js'
|
|
3
|
+
|
|
4
|
+
export function isTaSession(ctx, session, config = {}) {
|
|
5
|
+
const settings = normalizeConfig(config)
|
|
6
|
+
const header = session?.header
|
|
7
|
+
if (!header || header.origin !== undefined) return false
|
|
8
|
+
if (typeof header.cwd !== 'string' || header.cwd.length === 0) return false
|
|
9
|
+
if (realPath(header.cwd) !== realPath(settings.taHome)) return false
|
|
10
|
+
let preset
|
|
11
|
+
try {
|
|
12
|
+
preset = ctx?.agentPresets?.presetForSession?.(session)
|
|
13
|
+
} catch {
|
|
14
|
+
return false
|
|
15
|
+
}
|
|
16
|
+
return typeof preset === 'string' && settings.presets.includes(preset)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
/** 薄触发维护会话:专用 preset,绝不与普通 TA 会话共用工具。 */
|
|
21
|
+
export function isMaintenanceSession(ctx, session, config = {}) {
|
|
22
|
+
const settings = normalizeConfig(config)
|
|
23
|
+
const header = session?.header
|
|
24
|
+
if (!header || header.origin !== undefined) return false
|
|
25
|
+
if (typeof header.cwd !== 'string' || header.cwd.length === 0) return false
|
|
26
|
+
if (realPath(header.cwd) !== realPath(settings.taHome)) return false
|
|
27
|
+
let preset
|
|
28
|
+
try {
|
|
29
|
+
preset = ctx?.agentPresets?.presetForSession?.(session)
|
|
30
|
+
} catch {
|
|
31
|
+
return false
|
|
32
|
+
}
|
|
33
|
+
return preset === 'ta-maintenance'
|
|
34
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 四件套身份注入:SOUL → self/ME → RECENT-MEMORY → 认识/USER → 称呼。
|
|
3
|
+
*
|
|
4
|
+
* - 空文件不输出标题;超限截断并标“已截断”;
|
|
5
|
+
* - 读取一律经 withinHome,阻止 symlink 越界;
|
|
6
|
+
* - IM reduced 模式只注入 SOUL(不含 self/RECENT/认识/称呼)。
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { readFileSync, statSync } from 'node:fs'
|
|
10
|
+
import { resolve } from 'node:path'
|
|
11
|
+
import { CORE_FILES } from './constants.js'
|
|
12
|
+
import { isTaSession } from './gates.js'
|
|
13
|
+
import { CAPABILITY_GUIDANCE } from './prompts.js'
|
|
14
|
+
import { imModeFor } from './settings/im.js'
|
|
15
|
+
import { withinHome } from './storage/index.js'
|
|
16
|
+
|
|
17
|
+
export { CORE_FILES }
|
|
18
|
+
|
|
19
|
+
const fileCache = new Map()
|
|
20
|
+
const imCache = new Map()
|
|
21
|
+
|
|
22
|
+
const IM_CACHE_MS = 30_000
|
|
23
|
+
const IM_SESSION_PREFIX = 'dsh-im-session-'
|
|
24
|
+
|
|
25
|
+
function cacheFor(config) {
|
|
26
|
+
const key = String(config?.taHome ?? '')
|
|
27
|
+
const cached = imCache.get(key)
|
|
28
|
+
if (cached && Date.now() - cached.at < IM_CACHE_MS) return cached
|
|
29
|
+
let mode = 'owner'
|
|
30
|
+
try {
|
|
31
|
+
mode = imModeFor(config)
|
|
32
|
+
} catch {
|
|
33
|
+
mode = 'owner'
|
|
34
|
+
}
|
|
35
|
+
const entry = { at: Date.now(), mode, sessions: readImSessions(config) }
|
|
36
|
+
imCache.set(key, entry)
|
|
37
|
+
return entry
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function readImSessions(config) {
|
|
41
|
+
const ids = new Set()
|
|
42
|
+
try {
|
|
43
|
+
const dir = typeof config?.imConnectDir === 'string' ? config.imConnectDir : ''
|
|
44
|
+
if (dir) {
|
|
45
|
+
const value = JSON.parse(readFileSync(resolve(dir, 'sessions.json'), 'utf8'))
|
|
46
|
+
for (const key of Object.keys(value ?? {})) {
|
|
47
|
+
const binding = value[key]
|
|
48
|
+
if (typeof binding?.sessionId === 'string') ids.add(binding.sessionId)
|
|
49
|
+
if (typeof binding?.dshSessionId === 'string') ids.add(binding.dshSessionId)
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
} catch {
|
|
53
|
+
/* 没有 im-connect 就不是 IM 会话 */
|
|
54
|
+
}
|
|
55
|
+
return ids
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function sessionIdOf(session) {
|
|
59
|
+
const id = session?.id ?? session?.header?.id
|
|
60
|
+
return typeof id === 'string' ? id : ''
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** IM 会话识别:sessionId 前缀,或 im-connect sessions.json 里出现过。 */
|
|
64
|
+
export function isImSession(config, session) {
|
|
65
|
+
const id = sessionIdOf(session)
|
|
66
|
+
if (!id) return false
|
|
67
|
+
if (id.startsWith(IM_SESSION_PREFIX)) return true
|
|
68
|
+
return cacheFor(config).sessions.has(id)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function readText(config, relative) {
|
|
72
|
+
let target
|
|
73
|
+
try {
|
|
74
|
+
target = withinHome(relative, config)
|
|
75
|
+
} catch {
|
|
76
|
+
return ''
|
|
77
|
+
}
|
|
78
|
+
let stat
|
|
79
|
+
try {
|
|
80
|
+
stat = statSync(target)
|
|
81
|
+
} catch {
|
|
82
|
+
fileCache.delete(target)
|
|
83
|
+
return ''
|
|
84
|
+
}
|
|
85
|
+
const cached = fileCache.get(target)
|
|
86
|
+
if (cached && cached.mtimeMs === stat.mtimeMs && cached.size === stat.size) return cached.text
|
|
87
|
+
try {
|
|
88
|
+
const text = readFileSync(target, 'utf8')
|
|
89
|
+
fileCache.set(target, { mtimeMs: stat.mtimeMs, size: stat.size, text })
|
|
90
|
+
return text
|
|
91
|
+
} catch {
|
|
92
|
+
return ''
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function truncate(text, cap) {
|
|
97
|
+
const trimmed = String(text ?? '').trim()
|
|
98
|
+
if (trimmed.length <= cap) return trimmed
|
|
99
|
+
return `${trimmed.slice(0, cap)}\n…(已截断)`
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function part(config, spec) {
|
|
103
|
+
const text = truncate(readText(config, spec.path), spec.cap)
|
|
104
|
+
return text ? `${spec.title}\n${text}` : ''
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** 人格段:四件套 + 称呼;IM reduced 只给 SOUL。 */
|
|
108
|
+
export function personaText(ctx, session, config = {}) {
|
|
109
|
+
if (!isTaSession(ctx, session, config)) return ''
|
|
110
|
+
const im = isImSession(config, session)
|
|
111
|
+
const reduced = im && cacheFor(config).mode === 'reduced'
|
|
112
|
+
const parts = [part(config, CORE_FILES.soul)]
|
|
113
|
+
if (!reduced) {
|
|
114
|
+
parts.push(part(config, CORE_FILES.self))
|
|
115
|
+
parts.push(part(config, CORE_FILES.recent))
|
|
116
|
+
parts.push(part(config, CORE_FILES.user))
|
|
117
|
+
const address = typeof config.addressAs === 'string'
|
|
118
|
+
? config.addressAs.split(/\r?\n/)[0].trim().slice(0, 40)
|
|
119
|
+
: ''
|
|
120
|
+
if (address) parts.push(`对方希望你称呼 TA 为「${address}」`)
|
|
121
|
+
}
|
|
122
|
+
return parts.filter(Boolean).join('\n\n')
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** 能力段:只写事实,不假装目录隔离。 */
|
|
126
|
+
export function capabilityText(ctx, session, config = {}) {
|
|
127
|
+
if (!isTaSession(ctx, session, config)) return ''
|
|
128
|
+
return CAPABILITY_GUIDANCE
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** 设置页/状态用的四件套摘要。 */
|
|
132
|
+
export function coreFileSummary(config) {
|
|
133
|
+
const records = {}
|
|
134
|
+
for (const [key, spec] of Object.entries(CORE_FILES)) {
|
|
135
|
+
const text = readText(config, spec.path)
|
|
136
|
+
records[key] = {
|
|
137
|
+
path: spec.path,
|
|
138
|
+
exists: text.trim().length > 0,
|
|
139
|
+
chars: text.trim().length,
|
|
140
|
+
cap: spec.cap,
|
|
141
|
+
truncated: text.trim().length > spec.cap,
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return records
|
|
145
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-ta: Ta's DSH adapter.
|
|
3
|
+
*
|
|
4
|
+
* Host half wiring: persona/capability prompt sections, tools, capture/runtime,
|
|
5
|
+
* maintenance engine, and the settings/lifecycle API.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { disposeAgent, syncAgent } from './tools/index.js'
|
|
9
|
+
import { moodLine } from './state/index.js'
|
|
10
|
+
import { capabilityText, personaText } from './identity.js'
|
|
11
|
+
import { normalizeConfig } from './config.js'
|
|
12
|
+
import { isTaSession } from './gates.js'
|
|
13
|
+
import { createLifecycleQueue } from './settings/lifecycle.js'
|
|
14
|
+
import { mountSettingsRoutes } from './settings/http.js'
|
|
15
|
+
import { RuntimeManager } from './runtime-manager.js'
|
|
16
|
+
import { MaintenanceSessionWatcher } from './maintenance/session-watcher.js'
|
|
17
|
+
import { IM_GUIDANCE, memoryGuidanceText } from './prompts.js'
|
|
18
|
+
import { isImSession } from './identity.js'
|
|
19
|
+
import { UsageLedger } from './usage/ledger.js'
|
|
20
|
+
import { mountStickerRoute } from './stickers/serve.js'
|
|
21
|
+
import { mountUsageRoute } from './usage/serve.js'
|
|
22
|
+
import { stickerGuidance } from './stickers/select.js'
|
|
23
|
+
|
|
24
|
+
export { Config } from './config.js'
|
|
25
|
+
|
|
26
|
+
export const name = 'dsh-ta'
|
|
27
|
+
export const inject = ['systemPrompt', 'agentPresets', 'agents']
|
|
28
|
+
|
|
29
|
+
export function apply(ctx, input = {}) {
|
|
30
|
+
const config = normalizeConfig(input)
|
|
31
|
+
let runtime
|
|
32
|
+
|
|
33
|
+
if (typeof ctx.inject === 'function') {
|
|
34
|
+
ctx.inject(['settings'], (settingsCtx) => {
|
|
35
|
+
const settings = settingsCtx.get?.('settings')
|
|
36
|
+
if (typeof settings?.configure !== 'function') return
|
|
37
|
+
try {
|
|
38
|
+
ctx.effect(
|
|
39
|
+
() => settings.configure({ auto: false }, ctx.fiber),
|
|
40
|
+
'dsh-ta settings presentation',
|
|
41
|
+
)
|
|
42
|
+
} catch {
|
|
43
|
+
/* settings is optional */
|
|
44
|
+
}
|
|
45
|
+
})
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
ctx.systemPrompt.section({
|
|
49
|
+
name: 'ta-persona',
|
|
50
|
+
order: 10,
|
|
51
|
+
interpolate: false,
|
|
52
|
+
text: (context) => personaText(ctx, context?.session, config),
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
ctx.systemPrompt.section({
|
|
56
|
+
name: 'ta-capability',
|
|
57
|
+
order: 15,
|
|
58
|
+
interpolate: false,
|
|
59
|
+
text: (context) => capabilityText(ctx, context?.session, config),
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
ctx.systemPrompt.section({
|
|
63
|
+
name: 'ta-memory',
|
|
64
|
+
order: 20,
|
|
65
|
+
interpolate: false,
|
|
66
|
+
text: (context) => isTaSession(ctx, context?.session, config) ? memoryGuidanceText(config) : '',
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
ctx.systemPrompt.section({
|
|
70
|
+
name: 'ta-sticker-guidance',
|
|
71
|
+
order: 35,
|
|
72
|
+
interpolate: false,
|
|
73
|
+
text: (context) => {
|
|
74
|
+
if (!isTaSession(ctx, context?.session, config)) return ''
|
|
75
|
+
let preset
|
|
76
|
+
try {
|
|
77
|
+
preset = ctx.agentPresets?.presetForSession?.(context.session)
|
|
78
|
+
} catch {
|
|
79
|
+
preset = undefined
|
|
80
|
+
}
|
|
81
|
+
return preset === 'ta' ? stickerGuidance(config) : ''
|
|
82
|
+
},
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
ctx.systemPrompt.section({
|
|
86
|
+
name: 'ta-mood',
|
|
87
|
+
order: 30,
|
|
88
|
+
interpolate: false,
|
|
89
|
+
text: (context) => isTaSession(ctx, context?.session, config) ? moodLine(config) : '',
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
ctx.systemPrompt.section({
|
|
93
|
+
name: 'ta-im',
|
|
94
|
+
order: 40,
|
|
95
|
+
interpolate: false,
|
|
96
|
+
text: (context) => {
|
|
97
|
+
if (!isTaSession(ctx, context?.session, config)) return ''
|
|
98
|
+
return isImSession(config, context.session) ? IM_GUIDANCE : ''
|
|
99
|
+
},
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
ctx.on('agent/created', ({ agent }) => {
|
|
103
|
+
try { if (runtime) syncAgent(ctx, agent, config, runtime) } catch { /* never fail the host for an adapter */ }
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
ctx.on('agent-preset/selected', (sessionId) => {
|
|
107
|
+
try {
|
|
108
|
+
const agent = ctx.agents.get(sessionId)
|
|
109
|
+
if (agent && runtime) syncAgent(ctx, agent, config, runtime)
|
|
110
|
+
} catch { /* never fail the host for an adapter */ }
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
ctx.on('agent/disposed', ({ agent }) => {
|
|
114
|
+
disposeAgent(config, agent)
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
// Runtime owns Capture, Outbox and every timer that is bound to TA_HOME. A
|
|
118
|
+
// lifecycle operation stops it, changes the config, then starts a new one.
|
|
119
|
+
runtime = new RuntimeManager({ ctx, config, logger: ctx.logger })
|
|
120
|
+
try { runtime.start() } catch { /* identity/tools must still load */ }
|
|
121
|
+
const watcher = new MaintenanceSessionWatcher({ ctx, config, logger: ctx.logger })
|
|
122
|
+
try { watcher.start() } catch { /* 恢复清理失败不影响主流程 */ }
|
|
123
|
+
ctx.on('session/event', (session, event) => {
|
|
124
|
+
try { runtime.handle(session, event) } catch { /* adapter diagnostics must not stop sessions */ }
|
|
125
|
+
try { watcher.handle(session, event) } catch { /* cleanup must not stop sessions */ }
|
|
126
|
+
})
|
|
127
|
+
ctx.on('session/disposed', session => {
|
|
128
|
+
try { runtime.handleDispose(session) } catch { /* noop */ }
|
|
129
|
+
})
|
|
130
|
+
// schedule 服务可能晚于本插件就绪;就绪后把维护触发切到官方 schedule。
|
|
131
|
+
if (typeof ctx.inject === 'function') {
|
|
132
|
+
try { ctx.inject(['schedule'], () => { try { runtime.attachSchedule?.() } catch { /* fallback timer */ } }) } catch { /* optional */ }
|
|
133
|
+
}
|
|
134
|
+
if (typeof ctx.effect === 'function') {
|
|
135
|
+
try {
|
|
136
|
+
ctx.effect(() => () => {
|
|
137
|
+
watcher.dispose()
|
|
138
|
+
runtime.stop()
|
|
139
|
+
}, 'dsh-ta runtime')
|
|
140
|
+
} catch {
|
|
141
|
+
/* runtime still works until process exit */
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const host = {
|
|
146
|
+
ctx,
|
|
147
|
+
config,
|
|
148
|
+
runtime,
|
|
149
|
+
lifecycle: { operation: null },
|
|
150
|
+
queue: createLifecycleQueue(),
|
|
151
|
+
logger: ctx.logger,
|
|
152
|
+
}
|
|
153
|
+
try { mountSettingsRoutes(ctx, config, host) } catch { /* settings API optional */ }
|
|
154
|
+
|
|
155
|
+
try { mountStickerRoute(ctx, config) } catch { /* sticker route optional */ }
|
|
156
|
+
try { mountUsageRoute(ctx, config) } catch { /* usage route optional */ }
|
|
157
|
+
|
|
158
|
+
try {
|
|
159
|
+
const ledger = new UsageLedger({ ctx, config })
|
|
160
|
+
ledger.start()
|
|
161
|
+
ctx.on('session/event', (session, event) => {
|
|
162
|
+
try { ledger.handle(session, event) } catch { /* usage must not stop turns */ }
|
|
163
|
+
})
|
|
164
|
+
} catch {
|
|
165
|
+
/* usage accounting is optional */
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
// HMR/reload: agents that already exist never emit agent/created again.
|
|
170
|
+
try {
|
|
171
|
+
for (const agent of ctx.agents.list()) syncAgent(ctx, agent, config, runtime)
|
|
172
|
+
} catch { /* never fail the host for an adapter */ }
|
|
173
|
+
}
|
|
@@ -2,7 +2,7 @@ import { readFileSync, readdirSync, statSync } from 'node:fs'
|
|
|
2
2
|
import { relative, resolve, sep } from 'node:path'
|
|
3
3
|
import { expandHome } from './config.js'
|
|
4
4
|
import { captureMessage } from './memory/messages.js'
|
|
5
|
-
import { realPath, startOfLocalDay } from './storage.js'
|
|
5
|
+
import { realPath, startOfLocalDay } from './storage/index.js'
|
|
6
6
|
|
|
7
7
|
function jurisdictionRoots(config) {
|
|
8
8
|
if (!Array.isArray(config.jurisdictions)) return []
|