@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/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ChencLi819
|
|
4
|
+
Copyright (c) 2026 istarry (modifications)
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,56 @@
|
|
|
1
|
-
#
|
|
1
|
+
# spoon-companion
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
让对话角色有自己的内心和声音。
|
|
4
|
+
|
|
5
|
+
四块能力装在一个包里,每一块都能单独关:
|
|
6
|
+
|
|
7
|
+
| 能力 | 做什么 | 开关 |
|
|
8
|
+
|---|---|---|
|
|
9
|
+
| 情绪与身体 | 心情、情绪、饿困这些状态,随时间和对话自己演 | `promptEnabled` |
|
|
10
|
+
| 分条说话 | 一条一句往外蹦,节奏跟着情绪走 | `multiTurn.enabled` |
|
|
11
|
+
| 语音输出 | 把回复念出来(DeepSeek 官方 TTS / 小米 MiMo,支持音色克隆) | 设置面板 |
|
|
12
|
+
| 语音输入 | 点麦克风说话,本地识别,说完自动发送 | 麦克风按钮 |
|
|
13
|
+
|
|
14
|
+
## 情绪与身体
|
|
15
|
+
|
|
16
|
+
三层:性格(长期)、心情(几小时)、当前情绪(几分钟)。36 种情绪各带半衰期,会自己淡;
|
|
17
|
+
底色跟着作息走;跟每个群、每个人处得怎么样都记着;没有对话的时候也有自己的小事在发生。
|
|
18
|
+
附带 `mood_now` / `mood_feel` / `mood_diary` 三个工具。
|
|
19
|
+
|
|
20
|
+
## 分条说话
|
|
21
|
+
|
|
22
|
+
模型在回复末尾留 `<!--more-->`,宿主等这一轮结束再续一句,像人发微信一样一条一条来。
|
|
23
|
+
说几句由情绪决定:兴奋时多给几轮、间隔短,低落时只给一轮。
|
|
24
|
+
标记由客户端擦掉,界面上看不到。
|
|
25
|
+
|
|
26
|
+
## 语音输出
|
|
27
|
+
|
|
28
|
+
两个引擎可选,音色、语速都在设置里调,支持流式合成(边生成边播)和音色克隆。
|
|
29
|
+
播放是排队的,上一段播完才播下一段,不互相打断。
|
|
30
|
+
|
|
31
|
+
## 语音输入
|
|
32
|
+
|
|
33
|
+
点一下开麦,之后**一直听**——说完一句自动发出去,麦克风不关,接着听下一句;
|
|
34
|
+
再点一下才停。断句靠静音 1.2 秒或引擎端点。
|
|
35
|
+
|
|
36
|
+
识别在本机跑,不联网、不花钱:
|
|
37
|
+
|
|
38
|
+
- 引擎 sherpa-onnx,模型 zipformer2 中文 int8(约 160 MB)
|
|
39
|
+
- 首次用需要下载:`node tools/fetch-asr-model.cjs`,落到 `~/.dsh/spoon-companion/models/`
|
|
40
|
+
- 开麦期间会往提示词里加一句「现在是语音通话」,模型据此改成短句口语
|
|
41
|
+
|
|
42
|
+
## 装
|
|
43
|
+
|
|
44
|
+
```yaml
|
|
45
|
+
# cordis.patch.yml
|
|
46
|
+
- insert:
|
|
47
|
+
- id: fish-mood
|
|
48
|
+
name: "@istarry/spoon-companion"
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
> 内部 id 仍叫 `fish-mood`(历史原因,改了会丢状态),界面上显示的名字在 `locale/` 里。
|
|
52
|
+
|
|
53
|
+
## 出处
|
|
54
|
+
|
|
55
|
+
情绪引擎 fork 自 [ChencLi819/dsh-fish-mood](https://github.com/ChencLi819/dsh-fish-mood)(MIT)。
|
|
56
|
+
原作者的情绪模型设计非常扎实,这个 fork 只做最小必要修改,其余部分是在它之上加的。
|
package/build.mjs
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 构建客户端半边。
|
|
3
|
+
*
|
|
4
|
+
* src/client/index.ts → lib/client.js(浏览器,CJS + __ModuleLoader__ 包装)
|
|
5
|
+
*
|
|
6
|
+
* 约定(与 dsh-client-modules 的加载契约一致):
|
|
7
|
+
* - bundle id 必须严格等于 package.json 的包名
|
|
8
|
+
* - @deepseek-ai/* 一律 external(运行时由 dsh 提供,绝不能打第二份)
|
|
9
|
+
*
|
|
10
|
+
* 宿主半边不走 esbuild:plugin.js 和 lib/*.js 都是手写/搬来的 ESM,直接跑。
|
|
11
|
+
* 例外是 lib/voice.js —— 那是语音宿主的编译产物(只依赖 node: 内置模块),
|
|
12
|
+
* 原始 TS 在 src/voice-host/ 下,要改就改那边,再用 voice 仓库的 build.mjs 重新出。
|
|
13
|
+
*/
|
|
14
|
+
import { build } from 'esbuild'
|
|
15
|
+
import { readFileSync, mkdirSync } from 'node:fs'
|
|
16
|
+
import { fileURLToPath } from 'node:url'
|
|
17
|
+
|
|
18
|
+
const root = fileURLToPath(new URL('.', import.meta.url))
|
|
19
|
+
mkdirSync(root + 'lib', { recursive: true })
|
|
20
|
+
const pkg = JSON.parse(readFileSync(root + 'package.json', 'utf8'))
|
|
21
|
+
|
|
22
|
+
const PLATFORM_MODULES = [
|
|
23
|
+
'react',
|
|
24
|
+
'react/jsx-runtime',
|
|
25
|
+
'react-dom',
|
|
26
|
+
'react-dom/client',
|
|
27
|
+
'@deepseek-ai/cordis',
|
|
28
|
+
'@deepseek-ai/dsh-client-ui-renderer/client',
|
|
29
|
+
'@deepseek-ai/dsh-client-ui-slots',
|
|
30
|
+
'@deepseek-ai/dsh-client-ui-primitives',
|
|
31
|
+
'@deepseek-ai/dsh-client-ui-chat/client',
|
|
32
|
+
'@deepseek-ai/dsh-client-ui-conversation/client',
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
const result = await build({
|
|
36
|
+
entryPoints: [root + 'src/client/index.ts'],
|
|
37
|
+
outfile: root + 'lib/client.js',
|
|
38
|
+
bundle: true,
|
|
39
|
+
format: 'cjs',
|
|
40
|
+
platform: 'browser',
|
|
41
|
+
target: ['es2020'],
|
|
42
|
+
sourcemap: true,
|
|
43
|
+
logLevel: 'warning',
|
|
44
|
+
jsx: 'automatic',
|
|
45
|
+
external: PLATFORM_MODULES,
|
|
46
|
+
define: { 'process.env.NODE_ENV': '"production"' },
|
|
47
|
+
banner: {
|
|
48
|
+
js:
|
|
49
|
+
`window.__ModuleLoader__.load({ id: ${JSON.stringify(pkg.name)}, factory: (require) => {\n` +
|
|
50
|
+
'var module = { exports: {} }; var exports = module.exports;',
|
|
51
|
+
},
|
|
52
|
+
footer: { js: 'return module.exports; } });' },
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
if (result.errors.length > 0) process.exit(1)
|
|
56
|
+
console.log(`client bundle -> lib/client.js (id=${pkg.name})`)
|
package/cordis.patch.yml
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# @istarry/spoon-companion —— bundle patch
|
|
2
|
+
#
|
|
3
|
+
# 三块能力装在一个包里:
|
|
4
|
+
# 1. 情绪与身体 —— 心情、情绪、饿困这些状态在后台自己演变
|
|
5
|
+
# (fork 自 ChencLi819/dsh-fish-mood,MIT)
|
|
6
|
+
# 2. 分条说话 —— 像发消息一样一句一句往外蹦,节奏跟着情绪走
|
|
7
|
+
# 3. 语音 —— 把回复念出来(DeepSeek 官方 TTS / 小米 MiMo,支持音色克隆)
|
|
8
|
+
#
|
|
9
|
+
# 与原版 fish-mood 的差异(见 README 的 Fork 说明):
|
|
10
|
+
# 1. 会话标签修正:剥掉 `session-` 前缀后再取 id 片段,
|
|
11
|
+
# 原版 `sessionId.slice(0,8)` 只会得到「会话 session-」。
|
|
12
|
+
# 2. 包名改为 @istarry/spoon-companion,可与原版并存。
|
|
13
|
+
#
|
|
14
|
+
# 内部服务名 / 提示词 section 名 / 状态目录**保持 fish-mood 不变**:
|
|
15
|
+
# - 状态目录沿用 ~/.dsh/fish-mood,已有情绪数据不会丢
|
|
16
|
+
# - section 名沿用 fish-mood:rules / fish-mood:now,identity 配置继续生效
|
|
17
|
+
# - 插件实例 id 沿用 fish-mood,profile 里的覆盖配置不用改
|
|
18
|
+
#
|
|
19
|
+
# 想覆盖默认值,在自己的 profile 的 cordis.patch.yml 里补一行:
|
|
20
|
+
# - id: fish-mood
|
|
21
|
+
# config:
|
|
22
|
+
# identity: 我就是我,不是在演谁;情绪是真的。
|
|
23
|
+
# heartbeat: { lifeChance: 0.05 }
|
|
24
|
+
# multiTurn: { maxRounds: 2 }
|
|
25
|
+
- insert:
|
|
26
|
+
- id: fish-mood
|
|
27
|
+
name: "@istarry/spoon-companion"
|
package/icon.svg
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 96 96" width="96" height="96" role="img" aria-label="Fish Mood">
|
|
2
|
+
<defs>
|
|
3
|
+
<linearGradient id="fm-bg" x1="0" y1="0" x2="0" y2="1">
|
|
4
|
+
<stop offset="0" stop-color="#1b3a6b"/>
|
|
5
|
+
<stop offset="1" stop-color="#0d1f3c"/>
|
|
6
|
+
</linearGradient>
|
|
7
|
+
<linearGradient id="fm-fish" x1="0" y1="0" x2="1" y2="1">
|
|
8
|
+
<stop offset="0" stop-color="#7fd3ff"/>
|
|
9
|
+
<stop offset="1" stop-color="#2f8fe0"/>
|
|
10
|
+
</linearGradient>
|
|
11
|
+
<!-- 情绪曲线:低平 → 涌起 → 落下,像一天的心情 -->
|
|
12
|
+
<linearGradient id="fm-wave" x1="0" y1="0" x2="1" y2="0">
|
|
13
|
+
<stop offset="0" stop-color="#ffd479"/>
|
|
14
|
+
<stop offset="0.55" stop-color="#ff9f7a"/>
|
|
15
|
+
<stop offset="1" stop-color="#ff7fa8"/>
|
|
16
|
+
</linearGradient>
|
|
17
|
+
</defs>
|
|
18
|
+
<rect x="2" y="2" width="92" height="92" rx="22" fill="url(#fm-bg)"/>
|
|
19
|
+
<!-- 心情的起伏 -->
|
|
20
|
+
<path d="M10 56c8 0 10-13 18-13s10 25 18 25 10-30 18-30 10 20 18 20"
|
|
21
|
+
fill="none" stroke="url(#fm-wave)" stroke-width="3.4" stroke-linecap="round" opacity="0.95"/>
|
|
22
|
+
<!-- 她本鱼(在起伏里游) -->
|
|
23
|
+
<path d="M22 76c5-9 16-14 27-13 7 1 13 4 16 8-3 4-9 7-16 8-11 1-22-4-27-13z" fill="url(#fm-fish)"/>
|
|
24
|
+
<path d="M66 71l12-9v18l-12-9z" fill="#5cc0f5"/>
|
|
25
|
+
<circle cx="37" cy="71" r="3.6" fill="#0d1f3c"/>
|
|
26
|
+
<circle cx="38.4" cy="69.8" r="1.2" fill="#ffffff"/>
|
|
27
|
+
<!-- 心里那点软(温柔/喜欢) -->
|
|
28
|
+
<path d="M72 22c2.6-3.2 8-2 8 2.6 0 3.6-5 6.4-8 8.4-3-2-8-4.8-8-8.4 0-4.6 5.4-5.8 8-2.6z" fill="#ff8fb0" opacity="0.92"/>
|
|
29
|
+
<!-- 一滴(难过的时候也有) -->
|
|
30
|
+
<path d="M24 30c1.8-2.4 4.6-2.4 6.4 0 1.6 2.2 0.4 5.4-3.2 5.4s-4.8-3.2-3.2-5.4z" fill="#8fd8ff" opacity="0.7"/>
|
|
31
|
+
</svg>
|
package/lib/appraise.js
ADDED
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
// 评价引擎:从「刚刚发生了什么」推到「她现在是什么情绪」。
|
|
2
|
+
//
|
|
3
|
+
// 这是 v2 和 v1 最大的区别。v1 是关键词 → 情绪(叫胖 = 生气),情绪像开关;
|
|
4
|
+
// 人不是这么运作的:同一句「你胖」,陌生人说是冒犯(生气),熟人说可能是玩笑(好笑/委屈),
|
|
5
|
+
// 她今天累不累、自尊高不高、之前有没有被说过,都会改结果。
|
|
6
|
+
//
|
|
7
|
+
// 所以中间加一层「评价」(appraisal,心理学里的 appraisal theory):
|
|
8
|
+
//
|
|
9
|
+
// 事件 → 打分(这几个维度)→ 情绪
|
|
10
|
+
//
|
|
11
|
+
// 维度(全部归一化,写进 lib/catalog.js 里每种情绪的 signal):
|
|
12
|
+
// valence 好受(+1)/ 难受(-1)
|
|
13
|
+
// arousal 唤醒度(平/激动)
|
|
14
|
+
// relevance 跟我有关吗:0 = 完全别人的事,1 = 冲着我来的
|
|
15
|
+
// agency 谁的锅:'other' / 'self' / 'world' / 'none'
|
|
16
|
+
// coping 我应付得了吗(能反击/能解决 = 高;只能受着 = 低)
|
|
17
|
+
// norm 越界程度(这么对我不合适)
|
|
18
|
+
// fairness 公平吗(-1 太不公平)
|
|
19
|
+
// expectation 期望落空(-1)/ 超出预期(+1)
|
|
20
|
+
// novelty 意外程度
|
|
21
|
+
// certainty 确定了没有(不确定 → 紧张/困惑)
|
|
22
|
+
// intimacy 对方跟我的亲密度(熟人的刀更疼,熟人的夸更让人脸红)
|
|
23
|
+
// attention 有没有人看着我/在说我
|
|
24
|
+
// compassion 对方在难受(共情通道:温柔/心疼/担心)
|
|
25
|
+
// play 玩笑/逗乐通道
|
|
26
|
+
// social 孤独感(没人理我)
|
|
27
|
+
// hunger/energy/load 内部驱动(饿/困/心累)
|
|
28
|
+
//
|
|
29
|
+
// 每个情绪在目录里带一份 signal(哪些维度、落在什么区间),这里负责打分选最像的那个。
|
|
30
|
+
|
|
31
|
+
import { EMOTIONS, emotionOf, tierOf } from './catalog.js'
|
|
32
|
+
|
|
33
|
+
const clamp01 = (value) => Math.max(0, Math.min(1, value))
|
|
34
|
+
const clamp = (value, lo, hi) => Math.max(lo, Math.min(hi, value))
|
|
35
|
+
|
|
36
|
+
/** 维度权重:越能决定情绪归属的越重 */
|
|
37
|
+
const WEIGHTS = {
|
|
38
|
+
valence: 2.2,
|
|
39
|
+
relevance: 1.4,
|
|
40
|
+
agency: 1.3,
|
|
41
|
+
recognition: 1.3,
|
|
42
|
+
spotlight: 1.1,
|
|
43
|
+
coping: 1.2,
|
|
44
|
+
norm: 1.1,
|
|
45
|
+
compassion: 1.2,
|
|
46
|
+
fairness: 1.0,
|
|
47
|
+
benefit: 1.2,
|
|
48
|
+
resolution: 1.0,
|
|
49
|
+
rivalry: 1.0,
|
|
50
|
+
attention: 0.9,
|
|
51
|
+
intimacy: 0.8,
|
|
52
|
+
novelty: 0.8,
|
|
53
|
+
expectation: 0.8,
|
|
54
|
+
arousal: 0.8,
|
|
55
|
+
play: 0.8,
|
|
56
|
+
certainty: 0.6,
|
|
57
|
+
selfAgency: 0.9,
|
|
58
|
+
selfEsteem: 0.6,
|
|
59
|
+
social: 1.5,
|
|
60
|
+
hunger: 1.6,
|
|
61
|
+
energy: 1.5,
|
|
62
|
+
load: 1.3,
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** 单个维度的吻合度:落在区间里 = 1,越远越掉,掉到 0 为止 */
|
|
66
|
+
function rangeFit(value, [lo, hi]) {
|
|
67
|
+
if (value === undefined || value === null || Number.isNaN(value)) return null
|
|
68
|
+
if (value >= lo && value <= hi) return 1
|
|
69
|
+
const span = Math.max(0.15, hi - lo)
|
|
70
|
+
const distance = value < lo ? lo - value : value - hi
|
|
71
|
+
return clamp01(1 - distance / span)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 每个维度的「没说的时候」是什么值。
|
|
76
|
+
*
|
|
77
|
+
* 关键规矩:**只算事件明说的维度 + 引擎从她状态里算出来的维度**。
|
|
78
|
+
* 否则像「满足」要求 `benefit: [0, 0.4]`(没占谁便宜),事件没提 benefit 时默认 0 会白送一个吻合点,
|
|
79
|
+
* 情绪到处乱冒;反过来空事件也会因为 valence 默认 0 而推出一堆「烦」。
|
|
80
|
+
*/
|
|
81
|
+
const NEUTRAL = {
|
|
82
|
+
valence: 0, arousal: 0.45, relevance: 0.5, norm: 0, fairness: 0, expectation: 0,
|
|
83
|
+
novelty: 0.3, certainty: 0.7, intimacy: 0.2, recognition: 0, spotlight: 0, benefit: 0,
|
|
84
|
+
resolution: 0, rivalry: 0, compassion: 0, play: 0, social: 0, hunger: 0, load: 0,
|
|
85
|
+
attention: 0.5, selfEsteem: 0.55, coping: 0.5, energy: 0.5,
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** 这些维度是引擎按她的状态算出来的,任何时候都算数(不是「事件没说」) */
|
|
89
|
+
export const DERIVED_DIMS = ['coping', 'selfEsteem', 'intimacy', 'energy', 'hunger', 'load', 'social']
|
|
90
|
+
|
|
91
|
+
/** 单个维度的吻合度:数值维度落在区间里 = 1,越远越掉;分类维度(agency 这类)只认相等 */
|
|
92
|
+
function fitOf(value, range) {
|
|
93
|
+
if (typeof value === 'string' || typeof range === 'string') {
|
|
94
|
+
const list = Array.isArray(range) ? range : [range]
|
|
95
|
+
return list.some((item) => String(item) === String(value)) ? 1 : 0
|
|
96
|
+
}
|
|
97
|
+
return rangeFit(value, range)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** 一个情绪跟这组评价有多像:0-1;gate 用来「一票否决」明显不像的 */
|
|
101
|
+
export function scoreEmotion(emo, dims, given = null) {
|
|
102
|
+
let weightSum = 0
|
|
103
|
+
let score = 0
|
|
104
|
+
let hits = 0
|
|
105
|
+
let evidence = 0
|
|
106
|
+
let gate = 1
|
|
107
|
+
for (const [rawDim, range] of Object.entries(emo.signal ?? {})) {
|
|
108
|
+
// `!dim` = 这条约束必须成立:事件没说,就不认这个情绪(用来防「害羞」这种需要被看着的情绪乱冒)
|
|
109
|
+
const required = rawDim.startsWith('!')
|
|
110
|
+
const dim = required ? rawDim.slice(1) : rawDim
|
|
111
|
+
const value = dims[dim]
|
|
112
|
+
if (value === undefined || value === null) continue
|
|
113
|
+
const derived = DERIVED_DIMS.includes(dim)
|
|
114
|
+
const isGiven = !given || given.has(dim) || derived
|
|
115
|
+
if (required && !isGiven) return { score: 0, hits: 0, evidence: 0 }
|
|
116
|
+
// 事件没提到、也不是引擎算出来的维度 → 这条约束没提供信息,跳过
|
|
117
|
+
if (!isGiven) continue
|
|
118
|
+
const fit = fitOf(value, range)
|
|
119
|
+
if (fit === null || Number.isNaN(fit)) continue
|
|
120
|
+
const weight = WEIGHTS[dim] ?? 0.5
|
|
121
|
+
weightSum += weight
|
|
122
|
+
score += weight * fit
|
|
123
|
+
hits += 1
|
|
124
|
+
if (!derived) evidence += 1
|
|
125
|
+
// 不符合的约束要**明显**压低分数:差得越远压得越狠
|
|
126
|
+
if (fit < 0.2) gate = Math.min(gate, 0.15)
|
|
127
|
+
else if (fit < 0.5) gate = Math.min(gate, 0.5)
|
|
128
|
+
else if (fit < 0.8) gate = Math.min(gate, 0.8)
|
|
129
|
+
}
|
|
130
|
+
if (weightSum === 0) return { score: 0, hits, evidence }
|
|
131
|
+
return { score: (score / weightSum) * gate, hits, evidence }
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** 把事件给的部分维度补全、归一化 */
|
|
135
|
+
export function normalizeDims(input = {}, context = {}) {
|
|
136
|
+
const traits = context.traits ?? {}
|
|
137
|
+
const energy = Number.isFinite(input.energy) ? input.energy : (context.energy ?? 0.5)
|
|
138
|
+
const agency = input.agency ?? 'none'
|
|
139
|
+
const intimacy = clamp01(input.intimacy ?? context.intimacy ?? 0.2)
|
|
140
|
+
const norm = clamp01(input.norm ?? 0)
|
|
141
|
+
const selfEsteem = clamp01((traits.selfEsteem ?? 55) / 100)
|
|
142
|
+
const dims = {
|
|
143
|
+
valence: clamp(Number(input.valence ?? 0), -1, 1),
|
|
144
|
+
arousal: clamp01(input.arousal ?? 0.45),
|
|
145
|
+
relevance: clamp01(input.relevance ?? 0.5),
|
|
146
|
+
agency,
|
|
147
|
+
selfAgency: agency === 'self' ? 1 : 0,
|
|
148
|
+
// 应付能力:自尊高、精力足、有理由反击(对方越界)时更高;对方越亲、越没法翻脸,越低
|
|
149
|
+
coping: clamp01(input.coping ?? (0.55 + (selfEsteem - 0.55) * 0.5 + (energy - 0.5) * 0.4 + (norm >= 0.5 ? 0.15 : 0) - intimacy * 0.3)),
|
|
150
|
+
norm,
|
|
151
|
+
// 公平默认是「谈不上」:只有事件明说了不公平(被质疑、被比较)才算数,
|
|
152
|
+
// 否则任何越界都会被读成「不服」
|
|
153
|
+
fairness: clamp(Number(input.fairness ?? 0), -1, 1),
|
|
154
|
+
expectation: clamp(Number(input.expectation ?? 0), -1, 1),
|
|
155
|
+
novelty: clamp01(input.novelty ?? 0.3),
|
|
156
|
+
certainty: clamp01(input.certainty ?? 0.7),
|
|
157
|
+
intimacy,
|
|
158
|
+
recognition: clamp01(input.recognition ?? 0),
|
|
159
|
+
spotlight: clamp01(input.spotlight ?? 0),
|
|
160
|
+
benefit: clamp01(input.benefit ?? 0),
|
|
161
|
+
resolution: clamp01(input.resolution ?? 0),
|
|
162
|
+
rivalry: clamp01(input.rivalry ?? 0),
|
|
163
|
+
attention: clamp01(input.attention ?? (input.relevance ?? 0.5)),
|
|
164
|
+
compassion: clamp01(input.compassion ?? 0),
|
|
165
|
+
play: clamp01(input.play ?? 0),
|
|
166
|
+
social: clamp01(input.social ?? 0),
|
|
167
|
+
hunger: clamp01(input.hunger ?? context.hunger ?? 0),
|
|
168
|
+
energy: clamp01(energy),
|
|
169
|
+
load: clamp01(input.load ?? context.load ?? 0),
|
|
170
|
+
selfEsteem,
|
|
171
|
+
}
|
|
172
|
+
// 对方在难受 → 默认带上共情通道(除非事件明说了别的)
|
|
173
|
+
if (input.compassion === undefined && (input.otherDistress ?? 0) > 0.4) dims.compassion = clamp01(input.otherDistress)
|
|
174
|
+
return dims
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** 按情绪家族微调:不同家族对人格特质的敏感度不一样 */
|
|
178
|
+
function traitModulator(emo, traits) {
|
|
179
|
+
const neuro = (traits.neuroticism ?? 60) / 100
|
|
180
|
+
const extra = (traits.extraversion ?? 58) / 100
|
|
181
|
+
const open = (traits.openness ?? 62) / 100
|
|
182
|
+
const agree = (traits.agreeableness ?? 52) / 100
|
|
183
|
+
switch (emo.family) {
|
|
184
|
+
case 'joy':
|
|
185
|
+
case 'anticipation':
|
|
186
|
+
return 0.8 + extra * 0.35 + open * 0.2
|
|
187
|
+
case 'sadness':
|
|
188
|
+
case 'fear':
|
|
189
|
+
return 0.75 + neuro * 0.45
|
|
190
|
+
case 'anger':
|
|
191
|
+
return 0.8 + neuro * 0.3 + (1 - agree) * 0.35
|
|
192
|
+
case 'social':
|
|
193
|
+
return 0.8 + neuro * 0.35
|
|
194
|
+
case 'trust':
|
|
195
|
+
return 0.85 + agree * 0.3
|
|
196
|
+
case 'disgust':
|
|
197
|
+
return 0.85 + (1 - agree) * 0.3
|
|
198
|
+
case 'drive':
|
|
199
|
+
return 1
|
|
200
|
+
default:
|
|
201
|
+
return 1
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* 主入口:评价 → 情绪。
|
|
207
|
+
* event: { kind, why, strength, dims, who, source }
|
|
208
|
+
* context: { traits, energy, hunger, load, emotion, mood }
|
|
209
|
+
* 返回:{ key, emotion, intensity, why, dims, score, runnerUp }
|
|
210
|
+
*/
|
|
211
|
+
export function appraise(event = {}, context = {}) {
|
|
212
|
+
// 亲密度账本里是 0-100,评价里用 0-1
|
|
213
|
+
const closeness = Number(event.who?.closeness)
|
|
214
|
+
const intimacy = Number.isFinite(closeness) ? clamp01(closeness / 100) : undefined
|
|
215
|
+
const dims = normalizeDims(event.dims ?? {}, {
|
|
216
|
+
traits: context.traits,
|
|
217
|
+
energy: context.energy,
|
|
218
|
+
hunger: context.hunger,
|
|
219
|
+
load: context.load,
|
|
220
|
+
intimacy,
|
|
221
|
+
})
|
|
222
|
+
if (intimacy !== undefined) dims.intimacy = intimacy
|
|
223
|
+
if (event.attention !== undefined) dims.attention = clamp01(event.attention)
|
|
224
|
+
|
|
225
|
+
const ranked = []
|
|
226
|
+
// 事件明说了哪些维度:没说且中性值也在区间里的约束,不算信息(见 scoreEmotion)
|
|
227
|
+
const given = new Set(Object.keys(event.dims ?? {}))
|
|
228
|
+
if (event.attention !== undefined) given.add('attention')
|
|
229
|
+
for (const emo of EMOTIONS) {
|
|
230
|
+
if (emo.key === 'calm') continue
|
|
231
|
+
const { score, hits, evidence } = scoreEmotion(emo, dims, given)
|
|
232
|
+
if (hits === 0) continue
|
|
233
|
+
// 情绪得有「事件上的证据」才能起来;只有内部状态(精力/饿/累)不算 ——
|
|
234
|
+
// 例外是困/饿这种生理驱动,它们本来就只由身体状态决定
|
|
235
|
+
if (evidence === 0 && emo.family !== 'drive') continue
|
|
236
|
+
ranked.push({ emo, score, hits })
|
|
237
|
+
}
|
|
238
|
+
// 分数接近时,「约束更多而且都符合」的那个更贴切(平局用命中数破)
|
|
239
|
+
ranked.sort((a, b) => (b.score - a.score) || (b.hits - a.hits))
|
|
240
|
+
const best = ranked[0]
|
|
241
|
+
const runnerUp = ranked[1]
|
|
242
|
+
|
|
243
|
+
// 谁都对不上 → 平静(不产生冲量)
|
|
244
|
+
if (!best || best.score < 0.42) {
|
|
245
|
+
return { key: 'calm', emotion: emotionOf('calm'), intensity: 0, why: '', dims, score: best?.score ?? 0, runnerUp: null }
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const strength = clamp01(event.strength ?? 0.5)
|
|
249
|
+
const fit = clamp01(best.score)
|
|
250
|
+
const raw = 100 * strength * (0.5 + 0.5 * fit)
|
|
251
|
+
const intensity = Math.round(clamp(raw * traitModulator(best.emo, context.traits ?? {}), 0, 100))
|
|
252
|
+
if (intensity < 8) {
|
|
253
|
+
return { key: 'calm', emotion: emotionOf('calm'), intensity: 0, why: '', dims, score: best.score, runnerUp: null }
|
|
254
|
+
}
|
|
255
|
+
return {
|
|
256
|
+
key: best.emo.key,
|
|
257
|
+
emotion: best.emo,
|
|
258
|
+
intensity,
|
|
259
|
+
why: String(event.why ?? event.text ?? '').slice(0, 40),
|
|
260
|
+
dims,
|
|
261
|
+
score: best.score,
|
|
262
|
+
runnerUp: runnerUp ? { key: runnerUp.emo.key, score: runnerUp.score } : null,
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/** 给调试/自测看:这个评价组合下排前几的是谁 */
|
|
267
|
+
export function rankEmotions(dimsInput, context = {}) {
|
|
268
|
+
const dims = normalizeDims(dimsInput, context)
|
|
269
|
+
const given = new Set(Object.keys(dimsInput ?? {}))
|
|
270
|
+
return EMOTIONS.filter((emo) => emo.key !== 'calm')
|
|
271
|
+
.map((emo) => ({ key: emo.key, name: emo.name, score: scoreEmotion(emo, dims, given).score }))
|
|
272
|
+
.filter((row) => row.score > 0)
|
|
273
|
+
.sort((a, b) => b.score - a.score)
|
|
274
|
+
.slice(0, 5)
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
export { tierOf }
|
package/lib/asr-route.js
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 语音识别的 HTTP 通道。
|
|
3
|
+
*
|
|
4
|
+
* 客户端录音 → 把 PCM 甩过来 → 这里识别 → 把文字还回去。
|
|
5
|
+
*
|
|
6
|
+
* POST /api/spoon-companion/asr/feed { pcm: base64(float32) } → { streamId, text, endpoint }
|
|
7
|
+
* POST /api/spoon-companion/asr/close { streamId } → { text }
|
|
8
|
+
* GET /api/spoon-companion/asr/status → { ready, dir, missing }
|
|
9
|
+
*
|
|
10
|
+
* 为什么是 base64 而不是 multipart:分片很小(100ms 的 16k float32 ≈ 6.4KB),
|
|
11
|
+
* base64 多一点体积换代码简单,够用了。
|
|
12
|
+
*/
|
|
13
|
+
import { activeCount, close, feed, modelStatus, reset } from './asr.js'
|
|
14
|
+
|
|
15
|
+
const PREFIX = '/api/spoon-companion/asr'
|
|
16
|
+
|
|
17
|
+
/** base64 → Float32Array */
|
|
18
|
+
function decodePcm(b64) {
|
|
19
|
+
const buf = Buffer.from(String(b64 ?? ''), 'base64')
|
|
20
|
+
// 必须按 4 字节对齐,否则下面读出来的数是错的
|
|
21
|
+
const n = Math.floor(buf.length / 4)
|
|
22
|
+
const out = new Float32Array(n)
|
|
23
|
+
for (let i = 0; i < n; i++) out[i] = buf.readFloatLE(i * 4)
|
|
24
|
+
return out
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 挂上三条路由。
|
|
29
|
+
* @param ctx - 插件上下文
|
|
30
|
+
* @param log - 日志函数(warn)
|
|
31
|
+
* @returns 卸载函数
|
|
32
|
+
*/
|
|
33
|
+
export function applyAsrRoutes(ctx, log) {
|
|
34
|
+
const webServer = ctx.webServer ?? ctx.get('webServer')
|
|
35
|
+
if (!webServer?.register) {
|
|
36
|
+
log?.('warn', 'webServer 不可用,语音识别接口未注册')
|
|
37
|
+
return () => {}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const reply = (res, code, payload) => {
|
|
41
|
+
try {
|
|
42
|
+
res.statusCode = code
|
|
43
|
+
res.setHeader('Content-Type', 'application/json')
|
|
44
|
+
res.end(JSON.stringify(payload))
|
|
45
|
+
} catch {
|
|
46
|
+
/* ignore */
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const readJson = async (req) => {
|
|
51
|
+
let raw = ''
|
|
52
|
+
for await (const chunk of req) raw += chunk
|
|
53
|
+
return raw ? JSON.parse(raw) : {}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const routes = [
|
|
57
|
+
{
|
|
58
|
+
method: 'GET',
|
|
59
|
+
path: `${PREFIX}/status`,
|
|
60
|
+
handler: async (_req, res) => {
|
|
61
|
+
const st = modelStatus()
|
|
62
|
+
reply(res, 200, { ok: true, ...st, streams: activeCount() })
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
method: 'POST',
|
|
67
|
+
path: `${PREFIX}/feed`,
|
|
68
|
+
handler: async (req, res) => {
|
|
69
|
+
try {
|
|
70
|
+
const body = await readJson(req)
|
|
71
|
+
const pcm = decodePcm(body.pcm)
|
|
72
|
+
if (!pcm.length) { reply(res, 400, { ok: false, error: '空音频' }); return }
|
|
73
|
+
const rate = Number(body.rate) > 0 ? Number(body.rate) : 16000
|
|
74
|
+
const r = feed(pcm, rate)
|
|
75
|
+
reply(res, 200, { ok: true, ...r, rate })
|
|
76
|
+
} catch (error) {
|
|
77
|
+
reply(res, 500, { ok: false, error: error instanceof Error ? error.message : String(error) })
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
method: 'POST',
|
|
83
|
+
path: `${PREFIX}/close`,
|
|
84
|
+
handler: async (req, res) => {
|
|
85
|
+
try {
|
|
86
|
+
const body = await readJson(req)
|
|
87
|
+
const r = close(String(body.streamId ?? ''))
|
|
88
|
+
reply(res, 200, { ok: true, ...r })
|
|
89
|
+
} catch (error) {
|
|
90
|
+
reply(res, 500, { ok: false, error: error instanceof Error ? error.message : String(error) })
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
method: 'POST',
|
|
96
|
+
path: `${PREFIX}/reset`,
|
|
97
|
+
handler: async (_req, res) => {
|
|
98
|
+
reset()
|
|
99
|
+
reply(res, 200, { ok: true })
|
|
100
|
+
},
|
|
101
|
+
},
|
|
102
|
+
]
|
|
103
|
+
|
|
104
|
+
const disposers = []
|
|
105
|
+
for (const route of routes) {
|
|
106
|
+
try {
|
|
107
|
+
disposers.push(webServer.register(route))
|
|
108
|
+
} catch (error) {
|
|
109
|
+
log?.('warn', `语音识别路由 ${route.path} 注册失败:${error instanceof Error ? error.message : String(error)}`)
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return () => {
|
|
114
|
+
for (const d of disposers) {
|
|
115
|
+
try { d?.() } catch { /* ignore */ }
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|