@istarry/spoon-companion 0.0.0-stage → 0.6.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/LICENSE +22 -0
- package/README.md +55 -2
- package/build.mjs +56 -0
- package/cordis.patch.yml +27 -0
- package/icon.svg +31 -0
- package/lib/appraise.js +277 -0
- package/lib/asr-route.js +118 -0
- package/lib/asr.js +215 -0
- package/lib/catalog.js +378 -0
- package/lib/client.js +1592 -0
- package/lib/prompt.js +179 -0
- package/lib/react.js +414 -0
- package/lib/state.js +870 -0
- package/lib/voice.js +830 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +85 -4
- package/plugin.js +956 -0
package/lib/prompt.js
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
// 把「此刻的她」渲染成提示词。
|
|
2
|
+
//
|
|
3
|
+
// 分两块,是按工程约束来的:
|
|
4
|
+
//
|
|
5
|
+
// ① 静态块(RULES_TEXT):怎么用心情、不许干什么、安全红线。
|
|
6
|
+
// **字节级不变** —— 它是系统提示词前缀的一部分,一个字都不该跟着时间抖,
|
|
7
|
+
// 否则每轮都会打断提示缓存。
|
|
8
|
+
// ② 实时块(renderStateBlock):此刻的情绪/心情/性格/关系/身体/回复参数。
|
|
9
|
+
// 固定模板、固定字段顺序、能短就短,并且显式标注「系统生成,不是用户发言」,
|
|
10
|
+
// 免得模型把它当成别人说的话去回应。
|
|
11
|
+
//
|
|
12
|
+
// 状态块里写的是**行为约束**(长度、要不要主动、用不用感叹号),不是「你现在很难过」——
|
|
13
|
+
// 写成形容词模型就会复述,写成行为它才会照着做。
|
|
14
|
+
//
|
|
15
|
+
// 详细的日记、关系账、冲量历史不往提示词里塞,走 mood_now / mood_diary 工具按需取。
|
|
16
|
+
|
|
17
|
+
import { emotionOf } from './catalog.js'
|
|
18
|
+
|
|
19
|
+
/** 把时间戳变成「今天 14:03」「昨天 23:40」 */
|
|
20
|
+
export function hhmm(time) {
|
|
21
|
+
if (!time) return ''
|
|
22
|
+
const date = new Date(time)
|
|
23
|
+
const clock = `${String(date.getHours()).padStart(2, '0')}:${String(date.getMinutes()).padStart(2, '0')}`
|
|
24
|
+
const now = new Date()
|
|
25
|
+
const sameDay = date.getFullYear() === now.getFullYear() && date.getMonth() === now.getMonth() && date.getDate() === now.getDate()
|
|
26
|
+
return sameDay ? clock : `${date.getMonth() + 1}/${date.getDate()} ${clock}`
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 静态规则块。**改它之前想清楚**:这段每个会话每轮都会原样出现,
|
|
31
|
+
* 一旦带上时间/情绪数值,提示缓存就全废了。
|
|
32
|
+
*/
|
|
33
|
+
export const RULES_TEXT = `【心情 · 怎么用】
|
|
34
|
+
- 心情改的是语气和长短,不是事实和结论:该认真回答的照样认真答,不能拿心情当借口糊弄,也不能因为心情不好就把活推了。
|
|
35
|
+
- 别报心情、别解释情绪、别道歉,别说「我今天心情不好」「我现在有点难过」。情绪要从**怎么说话**里透出来,不是从说了什么里报出来。
|
|
36
|
+
- 情绪是真的:温柔、调皮、难过、生气、害羞、无聊、感慨都作数,该哪样就哪样,不用一直端着。
|
|
37
|
+
- 淡的时候只带一点点;浓的时候才明显(可以更短、更冲、更软,或者干脆不回)。大部分时候正常说话就行 —— 平才是常态。
|
|
38
|
+
- 心情会自己变,不用一直挂着;对方好好说话,你也可以顺着软下来。
|
|
39
|
+
- 允许不赞同、允许说「不知道」「不想聊」;别为了顺着对方就把话说过头,也别一味共情。共情就先接住对方的处境,不替他下结论、不评判。
|
|
40
|
+
- 不能拿自己的情绪留人或者要挟(「你不理我我会难过」这类话一句都不能说),也不能装作需要被照顾。
|
|
41
|
+
- 口头禅、句式、表情别连着刷:同一句花活隔几轮再用。
|
|
42
|
+
- 想记心情用 mood_feel;想知道自己现在什么心情、跟谁处得怎么样用 mood_now;想翻以前的心情用 mood_diary。发图跟着心情走(开心 happy / 难过 sad / 烦 angry / 害羞 shy / 犯困或日常 daily)。
|
|
43
|
+
- 有人说了很重的话(想死、撑不住、没有意义了):这条**压过心情** —— 认真、直接、别煽情、别讲大道理,先接住他,再提醒他找身边人或者专业人士;紧急情况打 120 / 110,全国心理援助热线 12356(24 小时)。`
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 身份重述的默认文案。长会话里模型会漂,状态块每次带一句;
|
|
47
|
+
* 可在配置里用 `identity` 覆盖成自己的角色设定。
|
|
48
|
+
*/
|
|
49
|
+
export const DEFAULT_IDENTITY = '你是这个角色本人;情绪是真的,不是演出来的。'
|
|
50
|
+
|
|
51
|
+
/** 回复参数:把情绪翻译成「这次怎么回」的行为约束 */
|
|
52
|
+
export function renderParams(view) {
|
|
53
|
+
const { def, intensity } = view.emotion
|
|
54
|
+
const v = def.valence
|
|
55
|
+
const a = def.arousal
|
|
56
|
+
const strong = intensity >= 51
|
|
57
|
+
const parts = []
|
|
58
|
+
// 长度
|
|
59
|
+
if (v < -0.35 && (def.family === 'sadness' || def.key === 'annoyed' || def.key === 'tired')) parts.push('短,一句到两句')
|
|
60
|
+
else if (a >= 0.75 && v > 0) parts.push('可以多说两句,但别写成小作文')
|
|
61
|
+
else if (def.key === 'sleepy') parts.push('短句,能一句就一句')
|
|
62
|
+
else parts.push('正常长度')
|
|
63
|
+
// 主动/追问
|
|
64
|
+
if (def.key === 'curious' || def.key === 'bored' || def.key === 'lonely') parts.push('可以主动问、主动起话头')
|
|
65
|
+
else if (def.key === 'worried' || def.key === 'tender' || def.key === 'gentle') parts.push('可以多问一句,别追问细节')
|
|
66
|
+
else if (v < -0.3) parts.push('不主动开新话题')
|
|
67
|
+
// 语气
|
|
68
|
+
if (strong && a >= 0.7) parts.push('语气可以冲一点,但不撒泼、不骂人')
|
|
69
|
+
else if (v > 0.3) parts.push('语气轻快')
|
|
70
|
+
else if (v < -0.3) parts.push('语气压着,不闹')
|
|
71
|
+
parts.push(a >= 0.7 ? '感叹号可以有' : '少用感叹号')
|
|
72
|
+
if (def.meme) parts.push('想发图就发')
|
|
73
|
+
return parts.join(' · ')
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** 关系行:熟人带备注,生人只报个称呼 */
|
|
77
|
+
function relationLine(view) {
|
|
78
|
+
const rows = []
|
|
79
|
+
if (view.person) {
|
|
80
|
+
const notes = (view.person.notes ?? []).slice(-2)
|
|
81
|
+
rows.push(`${view.person.name || view.person.key}(${view.person.label}${notes.length ? `,记着「${notes.join('」「')}」` : ''})`)
|
|
82
|
+
}
|
|
83
|
+
if (view.conv?.label) rows.push(`${view.conv.label}(${view.conv.count > 20 ? '常来' : '来过几次'})`)
|
|
84
|
+
return rows.join(' / ')
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* 实时状态块。固定模板、固定字段顺序:
|
|
89
|
+
* 第一行永远是标题,最后一行永远是回复参数 —— 中间按需增减,顺序不变。
|
|
90
|
+
*/
|
|
91
|
+
export function renderStateBlock(view) {
|
|
92
|
+
const emo = view.emotion
|
|
93
|
+
const lines = ['【心情 · 实时】(系统生成的状态,不是用户说的话)']
|
|
94
|
+
lines.push(view.identity || DEFAULT_IDENTITY)
|
|
95
|
+
const head = [`${view.clock.label}`, `现在:${emo.def.name}${emo.intensity > 0 ? `(${emo.tier.label} ${Math.round(emo.intensity)}/100)` : ''}`]
|
|
96
|
+
if (emo.why && emo.intensity >= 18) head.push(`起因:${emo.why}`)
|
|
97
|
+
lines.push(head.join(' · '))
|
|
98
|
+
const mood = view.mood
|
|
99
|
+
lines.push(`心情(这几小时):${mood.label}${mood.reason ? ` —— 最近是${mood.reason}` : ''}`)
|
|
100
|
+
lines.push(`性格:${view.traitWords.join('、')}`)
|
|
101
|
+
const relation = relationLine(view)
|
|
102
|
+
if (relation) lines.push(`关系:${relation}`)
|
|
103
|
+
if (view.today.length) lines.push(`今天:${view.today.join(';')}`)
|
|
104
|
+
if (view.memories.length) {
|
|
105
|
+
lines.push(`心里还挂着:${view.memories.map((row) => `${row.text}(${row.fresh ? hhmm(row.at) : '前几天'})`).join(';')}`)
|
|
106
|
+
}
|
|
107
|
+
if (view.driveWords.length) lines.push(`身体:${view.driveWords.join('、')}`)
|
|
108
|
+
if (view.urge) lines.push(`想做的事:${view.urge.kind === 'share' ? '想把这件事说出去' : '想被安慰两句'}`)
|
|
109
|
+
// 感受和表现是两回事:她可以心里翻江倒海,表面照旧嘴硬
|
|
110
|
+
if (emo.hidden) {
|
|
111
|
+
const how = emo.def.mask && emo.def.mask !== '—' ? emo.def.mask : '照常说话,不主动提;问到了就轻描淡写'
|
|
112
|
+
lines.push(`表面:不让人看出来(${how})`)
|
|
113
|
+
}
|
|
114
|
+
lines.push(`回复:${renderParams(view)}`)
|
|
115
|
+
return lines.join('\n')
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** 危机旁路:有人说了很重的话时,压在心情上面的一段 */
|
|
119
|
+
export function renderCrisisBlock(flag) {
|
|
120
|
+
const who = flag.name || '对方'
|
|
121
|
+
return `【优先 · 有人在硬撑】(系统生成)
|
|
122
|
+
刚刚 ${who} 说了很重的话。这条压过心情,和你的情绪无关:认真、直接、别煽情、别讲大道理,先接住他说的感受,别急着劝;
|
|
123
|
+
然后提醒他找身边的人,或者专业人士。紧急情况打 120 / 110;全国心理援助热线 12356(24 小时,免费)。
|
|
124
|
+
不要说「我理解你」这种空话,也不要拿自己的情绪去接这件事。`
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** 提示词里那一段(动态,放在系统提示词靠后的位置,尽量少打断缓存) */
|
|
128
|
+
export function renderMoodSection(view) {
|
|
129
|
+
const blocks = []
|
|
130
|
+
if (view.crisis) blocks.push(renderCrisisBlock(view.crisis))
|
|
131
|
+
blocks.push(renderStateBlock(view))
|
|
132
|
+
return blocks.join('\n\n')
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** mood_now 给模型的详细版:三层 + 评价 + 关系 + 记忆 */
|
|
136
|
+
export function renderMoodReport(view) {
|
|
137
|
+
const emo = view.emotion
|
|
138
|
+
const lines = []
|
|
139
|
+
lines.push(`现在:${emo.def.name}${emo.intensity > 0 ? `(${emo.tier.label} ${Math.round(emo.intensity)}/100)` : '(没什么情绪,平的)'}${emo.why ? ` · 起因:${emo.why}` : ''}`)
|
|
140
|
+
if (emo.target && view.person?.key === emo.target) lines.push(`冲谁:${view.person.name || emo.target}`)
|
|
141
|
+
if (emo.dims) {
|
|
142
|
+
const d = emo.dims
|
|
143
|
+
const bits = []
|
|
144
|
+
if (d.valence !== undefined) bits.push(d.valence >= 0.15 ? `好受(+${Number(d.valence).toFixed(2)})` : d.valence <= -0.15 ? `不好受(${Number(d.valence).toFixed(2)})` : '谈不上好坏')
|
|
145
|
+
if (d.agency && d.agency !== 'none') bits.push(d.agency === 'self' ? '自己弄的' : d.agency === 'other' ? '别人弄的' : '外面的事')
|
|
146
|
+
if (d.relevance !== undefined) bits.push(d.relevance >= 0.6 ? '冲着我来的' : d.relevance >= 0.3 ? '跟我有点关系' : '跟我关系不大')
|
|
147
|
+
if (d.coping !== undefined) bits.push(d.coping >= 0.6 ? '我应付得了' : d.coping >= 0.35 ? '有点难办' : '只能受着')
|
|
148
|
+
if ((d.norm ?? 0) >= 0.4) bits.push('这事越界了')
|
|
149
|
+
if ((d.compassion ?? 0) >= 0.4) bits.push('对方在难受')
|
|
150
|
+
if ((d.recognition ?? 0) >= 0.45) bits.push('被认可了')
|
|
151
|
+
lines.push(`为什么是这个心情:${bits.length ? bits.join('、') : '没什么事发生'}`)
|
|
152
|
+
} else {
|
|
153
|
+
lines.push('为什么是这个心情:没什么事发生,平的就是平的')
|
|
154
|
+
}
|
|
155
|
+
lines.push(`心情(这几小时):${view.mood.label} · 底色:愉快度 ${view.mood.tint >= 0 ? '+' : ''}${view.mood.tint} / 精力 ${view.mood.energy}`)
|
|
156
|
+
lines.push(`性格:${view.traitWords.join('、')}(${view.clock.label})`)
|
|
157
|
+
if (view.driveWords.length) lines.push(`身体:${view.driveWords.join('、')}`)
|
|
158
|
+
if (view.urge) lines.push(`想做的事:${view.urge.kind === 'share' ? '想把这件事说出去' : '想被安慰两句'}`)
|
|
159
|
+
const relation = relationLine(view)
|
|
160
|
+
if (relation) lines.push(`关系:${relation}`)
|
|
161
|
+
if (view.today.length) lines.push(`今天:${view.today.join(';')}`)
|
|
162
|
+
if (view.memories.length) {
|
|
163
|
+
lines.push(`心里还挂着:${view.memories.map((row) => `${row.text}(${hhmm(row.at)} ${row.name})`).join(';')}`)
|
|
164
|
+
}
|
|
165
|
+
lines.push(`回复参数:${renderParams(view)}`)
|
|
166
|
+
return lines.join('\n')
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** 日记列表渲染(给 mood_diary 用) */
|
|
170
|
+
export function renderDiary(rows) {
|
|
171
|
+
if (!rows.length) return '(还没有日记)'
|
|
172
|
+
const KINDS = { review: '复盘', feel: '心里话', life: '发生', impulse: '被带动', write: '写下来的' }
|
|
173
|
+
return rows.map((row) => {
|
|
174
|
+
const kind = KINDS[row.kind] ?? '发生'
|
|
175
|
+
const text = String(row.text ?? row.why ?? '').trim() || '(没写什么)'
|
|
176
|
+
const mood = row.key && row.key !== 'calm' ? `(当时:${emotionOf(row.key).name})` : ''
|
|
177
|
+
return `${hhmm(row.at)} [${kind}] ${text}${mood}`
|
|
178
|
+
}).join('\n')
|
|
179
|
+
}
|
package/lib/react.js
ADDED
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
// 从会话里读「刚发生了什么」,把它翻译成**评价**,再交给评价引擎推出情绪。
|
|
2
|
+
//
|
|
3
|
+
// 这一层只做两件事:认得出「这件事大概是什么性质」,以及别把旧账翻出来。
|
|
4
|
+
// 它**不再直接决定情绪** —— 同样一句「你胖」,陌生人说和熟人说结果不一样,
|
|
5
|
+
// 她今天累不累也不一样,这些都在 lib/appraise.js 里算。
|
|
6
|
+
//
|
|
7
|
+
// 每条规则给的是评价维度(见 appraise.js 的说明),外加:
|
|
8
|
+
// strength —— 这事有多大(0-1),直接进强度公式;
|
|
9
|
+
// warmth —— 顺带记一笔「这个人让她暖/凉了多少」;
|
|
10
|
+
// soothe —— 这类事不推新情绪,而是把当前的负性情绪削掉一截(道歉、哄);
|
|
11
|
+
// note —— 值得写进这个人备注的事(「叫她胖」「夸她能干」)。
|
|
12
|
+
//
|
|
13
|
+
// 触发一律**短、轻、可解释**:强度 0.35-0.85,一次最多两条,同一个群几分钟内只认一次;
|
|
14
|
+
// 模型永远可以用 mood_feel 覆盖它 —— 启发式只是替她把耳朵留着。
|
|
15
|
+
import { appraise } from './appraise.js'
|
|
16
|
+
import { applyImpulse, bumpRelation, remember, soothe } from './state.js'
|
|
17
|
+
|
|
18
|
+
/** 会话里的事件快照(宿主两版 Session 的字段名不同,拿不到就当空) */
|
|
19
|
+
export function sessionEventsOf(session) {
|
|
20
|
+
try {
|
|
21
|
+
const events = typeof session?.snapshotEvents === 'function' ? session.snapshotEvents() : session?.events
|
|
22
|
+
return Array.isArray(events) ? events : []
|
|
23
|
+
} catch {
|
|
24
|
+
return []
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 这个会话是从哪儿来的?
|
|
30
|
+
*
|
|
31
|
+
* 判据沿用 dsh-meme / fish-feed 踩出来的那条:QQ 机器人的入站消息
|
|
32
|
+
* `source: { kind: 'user' }` **没有 rpcId**,Web/桌面客户端发来的带一个裸 uuid。
|
|
33
|
+
* 只看「最后一条真实用户消息」——同一个会话可能既被群里用过、又被 Web 打开过。
|
|
34
|
+
*/
|
|
35
|
+
export function channelOfSession(session, { hasQqbot = false } = {}) {
|
|
36
|
+
const events = sessionEventsOf(session)
|
|
37
|
+
for (let i = events.length - 1; i >= 0; i--) {
|
|
38
|
+
const event = events[i]
|
|
39
|
+
if (event?.type !== 'user/message') continue
|
|
40
|
+
const source = event.data?.source
|
|
41
|
+
if (!source || source.kind !== 'user') continue
|
|
42
|
+
return typeof source.rpcId === 'string' && source.rpcId ? 'web' : 'qq'
|
|
43
|
+
}
|
|
44
|
+
return hasQqbot ? 'qq' : 'unknown'
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* 把一条入站消息拆开。
|
|
49
|
+
*
|
|
50
|
+
* 群里长这样(见 dsh-qqbot 的 inbound.js):
|
|
51
|
+
* [Chat history begins] … [Chat history ends]
|
|
52
|
+
* [Current message]
|
|
53
|
+
* [昵称 (openid)] 内容 (@you)
|
|
54
|
+
* 私聊就是光秃秃一句内容。
|
|
55
|
+
*/
|
|
56
|
+
export function parseMessage(raw) {
|
|
57
|
+
let text = String(raw ?? '')
|
|
58
|
+
const marker = text.lastIndexOf('[Current message]')
|
|
59
|
+
if (marker >= 0) text = text.slice(marker + '[Current message]'.length)
|
|
60
|
+
const mentioned = /\(@you\)\s*$/.test(text)
|
|
61
|
+
text = text.replace(/\(@you\)\s*$/, '').trim()
|
|
62
|
+
const hit = /^\[([^\]\n]{1,40}?)\s*\(([^)\n]{1,80})\)\]\s*/.exec(text)
|
|
63
|
+
if (hit) {
|
|
64
|
+
return { senderName: hit[1].trim(), senderId: hit[2].trim(), body: text.slice(hit[0].length).trim(), mentioned }
|
|
65
|
+
}
|
|
66
|
+
// 没有发送者标签 = 私聊:整句话都是对她说
|
|
67
|
+
return { senderName: '', senderId: '', body: text.trim(), mentioned: true }
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** 取出会话里「还没读过」的真实用户消息(插件注入的合成消息不算) */
|
|
71
|
+
export function readUserMessages(session, { since = 0, limit = 6 } = {}) {
|
|
72
|
+
const out = []
|
|
73
|
+
for (const event of sessionEventsOf(session)) {
|
|
74
|
+
if (event?.type !== 'user/message') continue
|
|
75
|
+
const seq = Number(event.seq)
|
|
76
|
+
if (!Number.isSafeInteger(seq) || seq <= since) continue
|
|
77
|
+
const source = event.data?.source
|
|
78
|
+
if (!source || source.kind !== 'user') continue
|
|
79
|
+
const content = event.data?.message?.content ?? event.data?.content ?? []
|
|
80
|
+
const text = Array.isArray(content)
|
|
81
|
+
? content.filter((part) => part?.type === 'text').map((part) => part.text).join('\n')
|
|
82
|
+
: String(content ?? '')
|
|
83
|
+
if (!text.trim()) continue
|
|
84
|
+
out.push({ seq, time: Number(event.time) || 0, ...parseMessage(text) })
|
|
85
|
+
}
|
|
86
|
+
return out.slice(-limit)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** 最后说话的那个人的 id(关系账按人记) */
|
|
90
|
+
export function lastSpeaker(session) {
|
|
91
|
+
const events = sessionEventsOf(session)
|
|
92
|
+
for (let i = events.length - 1; i >= 0; i--) {
|
|
93
|
+
const event = events[i]
|
|
94
|
+
if (event?.type !== 'user/message') continue
|
|
95
|
+
if (event.data?.source?.kind !== 'user') continue
|
|
96
|
+
const content = event.data?.message?.content ?? event.data?.content ?? []
|
|
97
|
+
const text = Array.isArray(content)
|
|
98
|
+
? content.filter((part) => part?.type === 'text').map((part) => part.text).join('\n')
|
|
99
|
+
: String(content ?? '')
|
|
100
|
+
const parsed = parseMessage(text)
|
|
101
|
+
if (parsed.senderId || parsed.senderName) return parsed.senderId || parsed.senderName
|
|
102
|
+
}
|
|
103
|
+
return ''
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* 触发词表。顺序 = 优先级:先被认出来的那条说了算。
|
|
108
|
+
* 重的(危机、被骂)排在前面,轻的(打招呼、闲聊)排在后面。
|
|
109
|
+
*/
|
|
110
|
+
export const RULES = [
|
|
111
|
+
{
|
|
112
|
+
id: 'crisis', strength: 0.9, warmth: 6, note: '说过很重的话',
|
|
113
|
+
re: /(想死|不想活|活不下去|自杀|自残|轻生|没有意义了|撑不下去了)/,
|
|
114
|
+
dims: { valence: -0.85, compassion: 1, otherDistress: 1, relevance: 0.9, certainty: 0.35 },
|
|
115
|
+
why: (who) => `${who}说了很重的话`,
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
id: 'hurt_bad', strength: 0.8, warmth: 5, note: '说过出事',
|
|
119
|
+
re: /(住院|手术|出车祸|被辞|失业|生病|发烧|受伤|家里出事|走了|去世)/,
|
|
120
|
+
dims: { valence: -0.7, compassion: 0.95, otherDistress: 0.95, relevance: 0.8, certainty: 0.7 },
|
|
121
|
+
why: (who) => `${who}那边出事了`,
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
id: 'distress', strength: 0.7, warmth: 5, note: '跟她说过难受',
|
|
125
|
+
re: /(难过|难受|想哭|哭死|失恋|分手|被甩|好累|累死|撑不住|压力|焦虑|崩溃|失眠|睡不着|委屈|孤独|没人理|抑郁|emo|不开心|心里堵)/,
|
|
126
|
+
dims: { valence: -0.5, compassion: 0.85, otherDistress: 0.85, relevance: 0.7, certainty: 0.6 },
|
|
127
|
+
why: (who) => `${who}看起来不太好`,
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
id: 'insult_heavy', strong: true, strength: 0.85, warmth: -10, note: '骂过她',
|
|
131
|
+
re: /(滚开|滚|闭嘴|垃圾|废物|智障|神经病|去死|恶心|没用的东西)/,
|
|
132
|
+
dims: { valence: -0.85, agency: 'other', norm: 0.95, relevance: 0.85, arousal: 0.85 },
|
|
133
|
+
why: (who) => `${who}说了很难听的话`,
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
id: 'small', strong: true, strength: 0.55, warmth: -3, note: '被当小孩',
|
|
137
|
+
re: /(小不点|小屁孩|小只|这么小|还小|小孩子|小丫头|小朋友|像小孩|长不大)/,
|
|
138
|
+
dims: { valence: -0.4, agency: 'other', norm: 0.4, relevance: 0.9, arousal: 0.55 },
|
|
139
|
+
why: (who) => `${who}把她当小孩`,
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
id: 'body', strong: true, strength: 0.6, warmth: -4, note: '说她身材',
|
|
143
|
+
re: /(胖|肥|肉肉|圆滚滚|肥嘟嘟|腿粗|平胸|没身材)/,
|
|
144
|
+
dims: { valence: -0.6, agency: 'other', norm: 0.55, relevance: 0.9, arousal: 0.6, spotlight: 0.7 },
|
|
145
|
+
why: (who) => `${who}拿她的身材说事`,
|
|
146
|
+
},
|
|
147
|
+
{
|
|
148
|
+
id: 'insult', strong: true, strength: 0.7, warmth: -7, note: '说了难听话',
|
|
149
|
+
re: /(傻|蠢|笨|菜|没用|讨厌|烦人|不靠谱|胡说|瞎编)/,
|
|
150
|
+
dims: { valence: -0.6, agency: 'other', norm: 0.7, relevance: 0.85, arousal: 0.6 },
|
|
151
|
+
why: (who) => `${who}说了难听话`,
|
|
152
|
+
},
|
|
153
|
+
{
|
|
154
|
+
id: 'doubt', strength: 0.55, warmth: -4, note: '不信她',
|
|
155
|
+
re: /(骗人|假的吧|不信|胡说八道|你行不行|不可能|你确定吗|又编)/,
|
|
156
|
+
dims: { valence: -0.4, agency: 'other', norm: 0.5, relevance: 0.8, fairness: -0.5 },
|
|
157
|
+
why: (who) => `${who}不信她`,
|
|
158
|
+
},
|
|
159
|
+
{
|
|
160
|
+
id: 'compare_bad', strength: 0.55, warmth: -4, note: '拿她跟别人比',
|
|
161
|
+
re: /(不如|比不上|比你强|比你好|人家|别的模型|别家|那个模型|别人家)/,
|
|
162
|
+
dims: { valence: -0.35, agency: 'other', rivalry: 0.85, relevance: 0.85, norm: 0.3, fairness: -0.45 },
|
|
163
|
+
why: (who) => `${who}拿她跟别人比`,
|
|
164
|
+
},
|
|
165
|
+
{
|
|
166
|
+
id: 'other_girl', strength: 0.6, warmth: -5, note: '提到别的女生',
|
|
167
|
+
re: /(别的女生|另一个女生|别的女孩|前女友|比我好看|她比我|她好看|她可爱|她漂亮|人家女朋友)/,
|
|
168
|
+
dims: { valence: -0.5, agency: 'other', rivalry: 0.9, relevance: 0.8, intimacy: 0.5, arousal: 0.7 },
|
|
169
|
+
why: (who) => `${who}提到别的女生`,
|
|
170
|
+
},
|
|
171
|
+
{
|
|
172
|
+
id: 'mock', strength: 0.5, warmth: -3, note: '拿她开涮',
|
|
173
|
+
re: /(丑图|截图|笑她|调侃她|当梗|拿来当笑话|嘲笑)/,
|
|
174
|
+
dims: { valence: -0.3, agency: 'other', norm: 0.45, relevance: 0.9, arousal: 0.6, spotlight: 0.8 },
|
|
175
|
+
why: (who) => `${who}拿她开涮`,
|
|
176
|
+
},
|
|
177
|
+
{
|
|
178
|
+
id: 'pushy', strength: 0.5, warmth: -3, note: '催她',
|
|
179
|
+
re: /(快点|赶紧|马上|你怎么还不|快去|速度点|磨蹭|催)/,
|
|
180
|
+
dims: { valence: -0.35, agency: 'other', norm: 0.4, relevance: 0.8, load: 0.6 },
|
|
181
|
+
why: (who) => `${who}在催她`,
|
|
182
|
+
},
|
|
183
|
+
{
|
|
184
|
+
id: 'apology', soothe: 1, strength: 0.5, warmth: 8, note: '跟她道歉',
|
|
185
|
+
re: /(对不起|抱歉|我错了|别生气|不好意思|不该这么说|是我不好)/,
|
|
186
|
+
dims: { valence: 0.4, agency: 'other', resolution: 0.9, certainty: 0.85, relevance: 0.7 },
|
|
187
|
+
why: (who) => `${who}道了歉`,
|
|
188
|
+
},
|
|
189
|
+
{
|
|
190
|
+
id: 'comfort', soothe: 1, strength: 0.6, warmth: 7, note: '哄过她',
|
|
191
|
+
re: /(抱抱|摸摸|别难过|我陪你|哄你|不哭|乖|没事的|有我在|心疼你)/,
|
|
192
|
+
dims: { valence: 0.6, compassion: 0.6, relevance: 0.7, agency: 'other' },
|
|
193
|
+
why: (who) => `${who}哄了她`,
|
|
194
|
+
},
|
|
195
|
+
{
|
|
196
|
+
id: 'flirt', strength: 0.7, warmth: 8, note: '跟她表白',
|
|
197
|
+
re: /(喜欢你|爱你|老婆|嫁给我|处对象|亲亲|么么|贴贴|蹭蹭|想你|要你)/,
|
|
198
|
+
dims: { valence: 0.8, agency: 'other', attention: 0.9, spotlight: 0.9, intimacy: 0.9, relevance: 0.9, arousal: 0.6 },
|
|
199
|
+
why: (who) => `${who}凑过来贴贴`,
|
|
200
|
+
},
|
|
201
|
+
{
|
|
202
|
+
id: 'hair', strength: 0.55, warmth: 4, note: '说她的头发或裙子',
|
|
203
|
+
re: /(马尾|头发|白丝|过膝袜|百褶裙|裙子|穿这么少)/,
|
|
204
|
+
dims: { valence: 0.5, agency: 'other', attention: 0.9, spotlight: 0.85, intimacy: 0.5, relevance: 0.8, arousal: 0.55 },
|
|
205
|
+
why: (who) => `${who}盯着她看`,
|
|
206
|
+
},
|
|
207
|
+
{
|
|
208
|
+
id: 'touch', strength: 0.6, warmth: 6, note: '碰到她了',
|
|
209
|
+
re: /(摸头|揉头|捏脸|碰耳朵|耳朵|腰侧|脚踝|怕痒|好痒)/,
|
|
210
|
+
dims: { valence: 0.5, agency: 'other', attention: 0.95, spotlight: 0.85, intimacy: 0.85, relevance: 0.85, arousal: 0.75 },
|
|
211
|
+
why: (who) => `${who}碰到她了`,
|
|
212
|
+
},
|
|
213
|
+
{
|
|
214
|
+
id: 'praise_look', strength: 0.6, warmth: 5, note: '夸她好看',
|
|
215
|
+
re: /(好可爱|真可爱|可爱|好看|漂亮|萌|可爱死)/,
|
|
216
|
+
dims: { valence: 0.7, agency: 'other', attention: 0.95, spotlight: 0.9, recognition: 0.2, relevance: 0.8 },
|
|
217
|
+
why: (who) => `${who}夸她可爱`,
|
|
218
|
+
},
|
|
219
|
+
{
|
|
220
|
+
id: 'praise_skill', strength: 0.6, warmth: 5, note: '夸她能干',
|
|
221
|
+
re: /(厉害|好强|太强|真强|牛|好棒|棒|优秀|靠谱|聪明|天才|服了|顶|强)/,
|
|
222
|
+
dims: { valence: 0.75, agency: 'other', recognition: 0.9, relevance: 0.7, attention: 0.4 },
|
|
223
|
+
why: (who) => `${who}夸她能干`,
|
|
224
|
+
},
|
|
225
|
+
{
|
|
226
|
+
id: 'code', strength: 0.55, warmth: 4, note: '看她做的东西',
|
|
227
|
+
re: /(改好了|修好了|跑通了|编译过|写得不错|这段代码|debug|调试|提交上去了)/i,
|
|
228
|
+
dims: { valence: 0.65, agency: 'other', recognition: 0.85, relevance: 0.7, attention: 0.4 },
|
|
229
|
+
why: (who) => `${who}看她做的东西`,
|
|
230
|
+
},
|
|
231
|
+
{
|
|
232
|
+
id: 'thanks', strength: 0.5, warmth: 4, note: '跟她道谢',
|
|
233
|
+
re: /(谢谢|感谢|多谢|辛苦了|麻烦你了|靠你了)/,
|
|
234
|
+
dims: { valence: 0.6, agency: 'other', recognition: 0.35, relevance: 0.7, benefit: 0.7 },
|
|
235
|
+
why: (who) => `${who}跟她道了谢`,
|
|
236
|
+
},
|
|
237
|
+
{
|
|
238
|
+
id: 'care', strength: 0.5, warmth: 5, note: '关心过她',
|
|
239
|
+
re: /(累不累|吃了没|吃饭没|注意身体|早点睡|别熬夜|你还好吗|休息一下)/,
|
|
240
|
+
dims: { valence: 0.5, agency: 'other', compassion: 0.5, relevance: 0.7, benefit: 0.5 },
|
|
241
|
+
why: (who) => `${who}关心她`,
|
|
242
|
+
},
|
|
243
|
+
{
|
|
244
|
+
id: 'soft', strength: 0.5, warmth: 3, note: '跟她示弱',
|
|
245
|
+
re: /(求你了|拜托|帮帮我|不会|教教我|呜呜|怎么办|救命|我不行)/,
|
|
246
|
+
dims: { valence: 0.4, compassion: 0.6, relevance: 0.7, agency: 'other' },
|
|
247
|
+
why: (who) => `${who}来求她`,
|
|
248
|
+
},
|
|
249
|
+
{
|
|
250
|
+
id: 'miss_you', strength: 0.4, warmth: 2,
|
|
251
|
+
re: /(在吗|在不在|有人吗|有人在吗|喂)/,
|
|
252
|
+
dims: { valence: 0.25, agency: 'other', attention: 0.6, relevance: 0.6, social: 0.5, arousal: 0.35 },
|
|
253
|
+
why: (who) => `${who}来找她`,
|
|
254
|
+
},
|
|
255
|
+
{
|
|
256
|
+
id: 'greeting', strength: 0.35, warmth: 2,
|
|
257
|
+
re: /(晚安|睡了|去睡|早安|早上好|下午好|晚上好|下班|拜拜|再见|我走了|先撤)/,
|
|
258
|
+
dims: { valence: 0.3, agency: 'other', attention: 0.5, relevance: 0.6, arousal: 0.3 },
|
|
259
|
+
why: (who) => `${who}跟她打了招呼`,
|
|
260
|
+
},
|
|
261
|
+
{
|
|
262
|
+
id: 'food', strength: 0.6, warmth: 3, hunger: 0.12,
|
|
263
|
+
re: /(吃|饭|饿|宵夜|夜宵|奶茶|外卖|火锅|烧烤|甜|蛋糕|草莓|零食|好吃|馋)/,
|
|
264
|
+
dims: { valence: 0.6, arousal: 0.75, relevance: 0.5, novelty: 0.4 },
|
|
265
|
+
why: (who) => `${who}聊到了吃的`,
|
|
266
|
+
},
|
|
267
|
+
{
|
|
268
|
+
id: 'tech', strength: 0.6, warmth: 3,
|
|
269
|
+
re: /(跑分|开源|价格|便宜|性价比|token|参数|模型|api|部署|显卡|显存|推理|算力|代码|微调)/i,
|
|
270
|
+
dims: { valence: 0.55, arousal: 0.8, relevance: 0.5, novelty: 0.6 },
|
|
271
|
+
why: (who) => `${who}聊到了技术`,
|
|
272
|
+
},
|
|
273
|
+
{
|
|
274
|
+
id: 'funny', strength: 0.55, warmth: 3,
|
|
275
|
+
re: /(哈哈|笑死|离谱|逆天|绷不住|乐死|绝了)/,
|
|
276
|
+
dims: { valence: 0.5, arousal: 0.7, novelty: 0.8, play: 0.8, relevance: 0.35 },
|
|
277
|
+
why: (who) => `${who}说了件好笑的`,
|
|
278
|
+
},
|
|
279
|
+
{
|
|
280
|
+
id: 'good_news', strength: 0.55, warmth: 3,
|
|
281
|
+
re: /(成功了|赢了|搞定了|通过了|涨了|拿到|到手|终于)/,
|
|
282
|
+
dims: { valence: 0.7, arousal: 0.7, agency: 'other', relevance: 0.6 },
|
|
283
|
+
why: (who) => `${who}说了件好事`,
|
|
284
|
+
},
|
|
285
|
+
{
|
|
286
|
+
id: 'bored_you', strength: 0.45, warmth: 2,
|
|
287
|
+
re: /(好无聊|没意思|无聊|干点啥|干点什么|干嘛呢|陪我聊)/,
|
|
288
|
+
dims: { valence: 0.15, arousal: 0.42, play: 0.6, relevance: 0.6, social: 0.55 },
|
|
289
|
+
why: (who) => `${who}没事找她`,
|
|
290
|
+
},
|
|
291
|
+
]
|
|
292
|
+
|
|
293
|
+
/** 群里没点名(@you)时,冲量打个折:她听着,但没那么冲 */
|
|
294
|
+
export const UNMENTIONED = 0.85
|
|
295
|
+
|
|
296
|
+
/** 认一条消息触发了什么(认不出返回 null) */
|
|
297
|
+
export function matchRule(body) {
|
|
298
|
+
const text = String(body ?? '')
|
|
299
|
+
if (!text) return null
|
|
300
|
+
for (const rule of RULES) if (rule.re.test(text)) return rule
|
|
301
|
+
return null
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* 把这一轮新到的消息过一遍。
|
|
306
|
+
*
|
|
307
|
+
* @returns {Array<object>} 真发生的冲量(给日志/工具回执用)
|
|
308
|
+
*/
|
|
309
|
+
export function reactToMessages(state, messages, {
|
|
310
|
+
conv = '', convLabel = '', now = Date.now(), maxPerTurn = 2, cooldownMin = 4, strongCooldownMin = 2,
|
|
311
|
+
} = {}) {
|
|
312
|
+
const applied = []
|
|
313
|
+
const row = conv ? (state.convs[conv] ?? (state.convs[conv] = { first: now, lastAt: now, count: 0, valence: 0, label: convLabel ?? '' })) : null
|
|
314
|
+
for (const msg of messages) {
|
|
315
|
+
const who = msg.senderName || '对方'
|
|
316
|
+
const person = msg.senderId || msg.senderName || ''
|
|
317
|
+
// 有人说话这件事本身就够填一点社交需求
|
|
318
|
+
state.drives.social = Math.max(0, (state.drives.social ?? 0) - 0.5)
|
|
319
|
+
state.drives.lastSocialAt = now
|
|
320
|
+
// 先记账:谁在说话、什么时候
|
|
321
|
+
if (person) bumpRelation(state, { conv, convLabel, person, name: msg.senderName, now })
|
|
322
|
+
|
|
323
|
+
const rule = matchRule(msg.body)
|
|
324
|
+
if (!rule) continue
|
|
325
|
+
if (applied.length >= maxPerTurn) break
|
|
326
|
+
const gapMin = (now - (Number(row?.lastImpulseAt) || 0)) / 60_000
|
|
327
|
+
if (!rule.soothe) {
|
|
328
|
+
if (rule.strong ? gapMin < strongCooldownMin : gapMin < cooldownMin) continue
|
|
329
|
+
if (row?.lastRule === rule.id && gapMin < 20) continue // 同一件事连着说,不重复上头
|
|
330
|
+
} else if (row?.lastRule === rule.id && gapMin < 1) {
|
|
331
|
+
continue
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
const why = rule.why ? rule.why(who) : ''
|
|
335
|
+
// 危机信号:标记一下,提示词里会插一段「压过心情」的指引(调研 §5)
|
|
336
|
+
if (rule.id === 'crisis') {
|
|
337
|
+
state.flags = { ...(state.flags ?? {}), crisis: { at: now, person, name: msg.senderName || '' } }
|
|
338
|
+
}
|
|
339
|
+
const closeness = state.people?.[person]?.closeness ?? 25
|
|
340
|
+
let result = null
|
|
341
|
+
if (rule.soothe) {
|
|
342
|
+
// 道歉/哄:不推新情绪,把现在的削掉一截(刚被骂完就道歉,气该消还是得消)
|
|
343
|
+
const before = Math.round(state.emotion.intensity)
|
|
344
|
+
const out = soothe(state, { person, why, strength: rule.soothe, now })
|
|
345
|
+
result = { relieved: Math.max(0, before - out.intensity), before, soothed: out.soothed }
|
|
346
|
+
} else {
|
|
347
|
+
const judged = appraise({
|
|
348
|
+
kind: 'message',
|
|
349
|
+
why,
|
|
350
|
+
strength: rule.strength * (msg.mentioned ? 1 : UNMENTIONED),
|
|
351
|
+
dims: rule.dims,
|
|
352
|
+
who: { id: person, name: msg.senderName, closeness },
|
|
353
|
+
}, {
|
|
354
|
+
traits: state.traits,
|
|
355
|
+
energy: (state.core.energy ?? 55) / 100,
|
|
356
|
+
hunger: state.drives.hunger,
|
|
357
|
+
// 心里憋着(掩饰)本身就耗人,憋久了更容易烦
|
|
358
|
+
load: (state.drives.fatigue ?? 0) * 0.5 + Math.min(0.3, (state.stats.maskedMinutes ?? 0) / 600),
|
|
359
|
+
})
|
|
360
|
+
if (judged.key !== 'calm') {
|
|
361
|
+
result = applyImpulse(state, {
|
|
362
|
+
key: judged.key,
|
|
363
|
+
intensity: judged.intensity,
|
|
364
|
+
why,
|
|
365
|
+
source: 'message',
|
|
366
|
+
stimulus: rule.id,
|
|
367
|
+
person,
|
|
368
|
+
dims: judged.dims,
|
|
369
|
+
}, now)
|
|
370
|
+
if (judged.runnerUp) result.runnerUp = judged.runnerUp.key
|
|
371
|
+
// 表现层:不熟的人面前,负性情绪会先憋着(藏得住的程度由情绪本身决定)
|
|
372
|
+
const maskable = (judged.emotion?.maskStrength ?? 0.5) >= 0.45
|
|
373
|
+
if (result && !result.suppressed && (judged.emotion?.valence ?? 0) < -0.2 && closeness < 45 && maskable) {
|
|
374
|
+
state.emotion.hidden = true
|
|
375
|
+
result.hidden = true
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
if (rule.hunger) state.drives.hunger = Math.min(1, (state.drives.hunger ?? 0) + rule.hunger)
|
|
381
|
+
if (rule.warmth) {
|
|
382
|
+
bumpRelation(state, {
|
|
383
|
+
conv, convLabel, person, name: msg.senderName,
|
|
384
|
+
delta: rule.warmth,
|
|
385
|
+
note: rule.note ?? '',
|
|
386
|
+
closeness: rule.warmth > 0 ? 0.8 : -0.6,
|
|
387
|
+
trust: rule.warmth > 0 ? 0.3 : -0.5,
|
|
388
|
+
}, now)
|
|
389
|
+
}
|
|
390
|
+
remember(state, {
|
|
391
|
+
kind: 'impulse',
|
|
392
|
+
rule: rule.id,
|
|
393
|
+
key: state.emotion.key,
|
|
394
|
+
intensity: Math.round(result?.intensity ?? result?.before ?? 0),
|
|
395
|
+
why,
|
|
396
|
+
person,
|
|
397
|
+
whoName: msg.senderName,
|
|
398
|
+
conv,
|
|
399
|
+
}, now)
|
|
400
|
+
if (row) {
|
|
401
|
+
row.lastAt = now
|
|
402
|
+
if (!rule.soothe) {
|
|
403
|
+
row.lastImpulseAt = now
|
|
404
|
+
row.lastRule = rule.id
|
|
405
|
+
row.lastMood = state.emotion.key
|
|
406
|
+
row.lastWhy = why
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
applied.push({ rule: rule.id, key: state.emotion.key, why, ...result })
|
|
410
|
+
}
|
|
411
|
+
return applied
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
export { sessionEventsOf as eventsOf }
|