dsh-agent-soul 0.9.12 → 0.9.13
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 +34 -0
- package/README.md +1 -1
- package/docs/HANDOFF.md +26 -10
- package/docs/R18-BRIEF.md +212 -0
- package/lib/attest.js +6 -0
- package/lib/evidence.js +168 -0
- package/lib/health.js +11 -1
- package/lib/host/runtime/entry.js +231 -212
- package/lib/host/runtime/inject.js +235 -232
- package/lib/host/runtime/refine.js +209 -206
- package/lib/http/middleware.js +18 -0
- package/lib/http/routes/system.js +32 -1
- package/lib/index.js +5 -1
- package/lib/mood/digest.js +3 -1
- package/lib/mood/hooks.js +157 -154
- package/lib/mood.js +5 -3
- package/lib/state/cache.js +367 -358
- package/package.json +2 -2
package/lib/mood/hooks.js
CHANGED
|
@@ -1,154 +1,157 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 心情引擎 · hooks.js
|
|
3
|
-
* 由 lib/mood.js 按职责拆出(2026-11-06 模块化重构),函数体保持原样。
|
|
4
|
-
*/
|
|
5
|
-
import { DIMS, moodStepOf } from './core.js'
|
|
6
|
-
import { EXTERNAL_TRIGGER, INJECTED_CONTEXT, SELF_SPEAK } from './prompt.js'
|
|
7
|
-
import { judgeMood } from './judge.js'
|
|
8
|
-
import { lastAssistantTextOf, lastUserTurnOf, textOfMessage } from './session.js'
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
//
|
|
13
|
-
//
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
*
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
if (!agent
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
let
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
const
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
const
|
|
91
|
-
const
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
//
|
|
99
|
-
//
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
//
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
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
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
*
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* 心情引擎 · hooks.js
|
|
3
|
+
* 由 lib/mood.js 按职责拆出(2026-11-06 模块化重构),函数体保持原样。
|
|
4
|
+
*/
|
|
5
|
+
import { DIMS, moodStepOf } from './core.js'
|
|
6
|
+
import { EXTERNAL_TRIGGER, INJECTED_CONTEXT, SELF_SPEAK } from './prompt.js'
|
|
7
|
+
import { judgeMood } from './judge.js'
|
|
8
|
+
import { lastAssistantTextOf, lastUserTurnOf, textOfMessage } from './session.js'
|
|
9
|
+
import { mark } from '../evidence.js'
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
// --------------------------------------------------------------------------
|
|
13
|
+
// 每轮钩子:懒挂 agent 的 turn-stopping(挂一次就够)
|
|
14
|
+
// --------------------------------------------------------------------------
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* agent -> { offs }。**必须是 WeakMap**:agent 身上挂着整个会话的事件历史,
|
|
18
|
+
* 用普通 Map 强引用它就永远回收不掉 —— 会话开了又关,内存只涨不降。
|
|
19
|
+
* 但 WeakMap 不可遍历,unobserveAll 又需要全量拆掉,所以另留一个只存 `{ offs }`
|
|
20
|
+
* 的 Set:它不引用 agent,只是记录有哪些监听还挂着。
|
|
21
|
+
*/
|
|
22
|
+
const mounted = new WeakMap()
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 2026-11-07 R7 / P1-L1:原来这里存 `{ offs }` 的**强引用** —— 而 `offs` 里的回调
|
|
26
|
+
* 闭包直接引用 `agent`,于是 `mountedOffs → entry → offs → 闭包 → agent` 是一条
|
|
27
|
+
* 强引用链,会话关了 agent 也回收不掉(它身上挂着整个会话的事件历史)。
|
|
28
|
+
* WeakMap 的防泄漏设计被这个 Set 完全抵消了。
|
|
29
|
+
*
|
|
30
|
+
* 现在存 WeakRef:
|
|
31
|
+
* · `mounted` 是 WeakMap,只在 **agent 活着** 时才持有 entry —— 所以 entry 不可能
|
|
32
|
+
* 在 agent 存活期间被回收,不会出现「监听还挂着、off 却调不到」的静默泄漏;
|
|
33
|
+
* · agent 死了,entry 自然可回收,Set 里只留一个空壳,遍历时清掉。
|
|
34
|
+
*/
|
|
35
|
+
const mountedOffs = new Set()
|
|
36
|
+
|
|
37
|
+
let judgeChain = Promise.resolve() // 判断串行,避免多轮堆积互相覆盖
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* 观察一个 agent:每轮结束判断一次心情(异步,不阻塞对话)。
|
|
42
|
+
* 幂等:同一个 agent 只挂一次。config 是函数(现读,设置改完立刻生效)。
|
|
43
|
+
*/
|
|
44
|
+
export function observeAgent(agent, { llm, config, note }) {
|
|
45
|
+
if (!agent || mounted.has(agent)) return
|
|
46
|
+
if (!agent.ctx || typeof agent.ctx.on !== 'function') {
|
|
47
|
+
if (note) note('mood', 'agent 没有事件总线,心情判断未挂载')
|
|
48
|
+
return
|
|
49
|
+
}
|
|
50
|
+
const offs = []
|
|
51
|
+
let lastText = ''
|
|
52
|
+
let lastSrc = ''
|
|
53
|
+
const names = () => {
|
|
54
|
+
const cfg = config()
|
|
55
|
+
return {
|
|
56
|
+
self: cfg.moodSelfName,
|
|
57
|
+
// 2026-11-04:主人名字/性别/关系一律走配置,空值时提示词整句省略。
|
|
58
|
+
owner: cfg.ownerName,
|
|
59
|
+
ownerName: cfg.ownerName,
|
|
60
|
+
gender: cfg.ownerGender,
|
|
61
|
+
relationship: cfg.relationship,
|
|
62
|
+
aliases: cfg.moodSelfAliases,
|
|
63
|
+
judgeProvider: cfg.moodJudgeProvider,
|
|
64
|
+
judgeModel: cfg.moodJudgeModel,
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// 1) 收这一轮对方说的话(宿主注入的上下文不算)
|
|
69
|
+
try {
|
|
70
|
+
offs.push(agent.ctx.on('agent/inbox/inserted', (payload) => {
|
|
71
|
+
const msg = payload && payload.message ? payload.message : payload
|
|
72
|
+
const txt = textOfMessage(msg)
|
|
73
|
+
const src = msg && msg.source && msg.source.kind ? String(msg.source.kind) : ''
|
|
74
|
+
if (!txt || INJECTED_CONTEXT.has(src)) return
|
|
75
|
+
lastText = txt
|
|
76
|
+
lastSrc = src
|
|
77
|
+
}))
|
|
78
|
+
} catch (e) {
|
|
79
|
+
if (note) note('mood', `监听 inbox 失败:${e instanceof Error ? e.message : e}`)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// 2) 这轮结束 → 判断心情(异步,不阻塞对话)
|
|
83
|
+
try {
|
|
84
|
+
offs.push(agent.ctx.on('agent/turn-stopping', () => {
|
|
85
|
+
const cfg = config()
|
|
86
|
+
if (cfg.moodEnabled === false) return
|
|
87
|
+
// 以会话事件为准(带 source.kind),拿不到才退回 inbox 缓存
|
|
88
|
+
const info = lastUserTurnOf(agent.session) || (lastText ? { text: lastText, sourceKind: lastSrc } : null)
|
|
89
|
+
if (!info || !info.text) return
|
|
90
|
+
const reply = lastAssistantTextOf(agent.session)
|
|
91
|
+
const kind = String(info.sourceKind || '')
|
|
92
|
+
const mode = EXTERNAL_TRIGGER.has(kind) ? 'work' : (SELF_SPEAK.has(kind) ? 'talk' : '')
|
|
93
|
+
judgeChain = judgeChain
|
|
94
|
+
.then(async () => {
|
|
95
|
+
const delta = await judgeMood(llm, agent, info.text, reply, mode, names(), note)
|
|
96
|
+
if (!delta) return
|
|
97
|
+
const anyDim = DIMS.some(([k]) => delta.dims[k] !== 0)
|
|
98
|
+
// 2026-11-06 审计 P0-2:和 core.applyDelta 用**同一个**步长函数。
|
|
99
|
+
// 原来这里各写一遍 `Math.round(delta.moodDelta / 3)`,±1 都变成 0,
|
|
100
|
+
// 于是判定模型按 prompt 给的 -1 既不记账也不动数值 —— 两处口径必须共享。
|
|
101
|
+
const step = moodStepOf(delta.moodDelta)
|
|
102
|
+
if (step === 0 && !anyDim) return // 一点变化都没有的回合不记账
|
|
103
|
+
// 「显著」决定时间线上标不标小波动(不注入,只是展示口径)
|
|
104
|
+
const sig = Math.abs(delta.moodDelta) >= cfg.moodSigMood
|
|
105
|
+
|| DIMS.some(([k]) => Math.abs(delta.dims[k] || 0) >= cfg.moodSigDim)
|
|
106
|
+
if (typeof cfg.applyMoodDelta === 'function') {
|
|
107
|
+
cfg.applyMoodDelta(delta, { minor: !sig })
|
|
108
|
+
}
|
|
109
|
+
// 2026-11-10 R18-C:动态证据 —— 判定**真的记了账**(不是「心情开关开着」)
|
|
110
|
+
mark('moodJudge', (delta.reason || '(无理由)').slice(0, 20))
|
|
111
|
+
if (note) note('mood', `${sig ? '心情更新' : '小波动'}:${delta.reason || '(无理由)'}${mode ? `(${mode === 'talk' ? '自己搭话' : '定时任务'})` : ''}`)
|
|
112
|
+
})
|
|
113
|
+
.catch((e) => { if (note) note('mood', `判断失败:${e instanceof Error ? e.message : e}`) })
|
|
114
|
+
}))
|
|
115
|
+
} catch (e) {
|
|
116
|
+
if (note) note('mood', `监听 turn-stopping 失败:${e instanceof Error ? e.message : e}`)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const entry = { offs }
|
|
120
|
+
mounted.set(agent, entry)
|
|
121
|
+
mountedOffs.add(new WeakRef(entry))
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
/** 拆掉全部钩子(插件卸载/测试用)。 */
|
|
126
|
+
export function unobserveAll() {
|
|
127
|
+
// 遍历 mountedOffs 而不是 mounted:WeakMap 不可枚举。
|
|
128
|
+
// 2026-11-07 P1-L1:里面存的是 WeakRef,agent 已经被回收的 entry 会 deref 成 undefined,
|
|
129
|
+
// 那种直接跳过(它的监听随 ctx 一起没了,不需要拆)。
|
|
130
|
+
for (const ref of mountedOffs) {
|
|
131
|
+
const entry = typeof ref?.deref === 'function' ? ref.deref() : ref
|
|
132
|
+
if (entry === undefined || entry === null) continue
|
|
133
|
+
for (const off of entry.offs) {
|
|
134
|
+
try { if (typeof off === 'function') off() } catch { /* ignore */ }
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
mountedOffs.clear()
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* 测试用:当前还挂着的 agent 数。
|
|
143
|
+
*
|
|
144
|
+
* 2026-11-07 P1-L1:Set 里是 WeakRef,所以要数「deref 还有效」的那些 ——
|
|
145
|
+
* 否则 agent 被回收后这个数字会虚高(空壳也算一个)。
|
|
146
|
+
*/
|
|
147
|
+
export const observedCount = () => {
|
|
148
|
+
let live = 0
|
|
149
|
+
for (const ref of mountedOffs) {
|
|
150
|
+
if (typeof ref?.deref === 'function') {
|
|
151
|
+
if (ref.deref() !== undefined) live += 1
|
|
152
|
+
} else if (ref !== undefined && ref !== null) {
|
|
153
|
+
live += 1
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return live
|
|
157
|
+
}
|
package/lib/mood.js
CHANGED
|
@@ -8,10 +8,12 @@
|
|
|
8
8
|
* 五张 assets/face-*.png 同为原作者自绘,见 assets/README.md。
|
|
9
9
|
* 改动清单:THIRD-PARTY-NOTICES.md §3.1。
|
|
10
10
|
*
|
|
11
|
-
* ── 从 dsh-feiyu-mood 0.7.6
|
|
12
|
-
* · **每回合注入**(它的 systemPrompt.context 每轮把心情写进提示词)—— 本插件只记账不注入;
|
|
11
|
+
* ── 从 dsh-feiyu-mood 0.7.6 移植,砍掉两样、改造一样 ──
|
|
13
12
|
* · **右下角挂饰**(widget.js / index-inject 那套)—— 不要;
|
|
14
|
-
* · **多份绑定**(slots / 会话模式 / 工作区模式)—— 一份心情跟着一份人格走,按 cwd 的 slug
|
|
13
|
+
* · **多份绑定**(slots / 会话模式 / 工作区模式)—— 一份心情跟着一份人格走,按 cwd 的 slug 记;
|
|
14
|
+
* · **注入**—— 原版每回合把心情写进提示词(和挂饰绑在一起)。我们**留注入、去挂饰**:
|
|
15
|
+
* 2026-11-01 起有自己的 `soul:mood` 段(order 230),文案与它的 widget 无关。
|
|
16
|
+
* 这行原来写的是「本插件只记账不注入」,2026-11-10 R18 文档对齐漏改,发布前复核补正。
|
|
15
17
|
* 留下的是判断内核:一次很短的模型调用产出 moodDelta + 七情变化 + 理由 + 可信度,
|
|
16
18
|
* 心情值慢动(只吃 1/3)、七情即时动、时间衰减回稳、时间线存档。
|
|
17
19
|
*
|