@istarry/spoon-companion 0.7.8 → 0.8.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/lib/asr.js CHANGED
@@ -130,6 +130,32 @@ export function isVoiceActive() {
130
130
  return Date.now() - lastFeedAt < VOICE_IDLE_MS
131
131
  }
132
132
 
133
+ // ── 通话开关 ────────────────────────────────────────────────────────────────
134
+ // 放在这一层,是为了让语音模块(它拿不到 plugin 的闭包)也能判断"在不在通话里"。
135
+
136
+ /** 客户端点过通话的总开关(全局) */
137
+ let callGlobal = false
138
+ /** 单独标过在通话的会话 */
139
+ const callSessions = new Set()
140
+
141
+ export function setCallGlobal(on) {
142
+ callGlobal = Boolean(on)
143
+ }
144
+
145
+ export function markCallSession(id, on) {
146
+ const key = String(id ?? '')
147
+ if (!key) return
148
+ if (on) callSessions.add(key)
149
+ else callSessions.delete(key)
150
+ }
151
+
152
+ /** 这个会话现在算不算在通话:全局开关 / 这个会话被标过 / 最近真的在送音频 */
153
+ export function isCallActive(sessionId) {
154
+ if (callGlobal) return true
155
+ if (sessionId && callSessions.has(String(sessionId))) return true
156
+ return isVoiceActive()
157
+ }
158
+
133
159
  export function feed(pcm, rate = 16000) {
134
160
  lastFeedAt = Date.now()
135
161
  const rec = ensureRecognizer()
package/lib/catalog.js CHANGED
@@ -364,7 +364,7 @@ export const EMOTIONS = [
364
364
  valence: -0.05, arousal: 0.4, halfLifeMin: 40, maskStrength: 0.5, core: { v: -5, e: 0 },
365
365
  signal: { valence: [-0.3, 0.4], spotlight: [0.4, 1], intimacy: [0.5, 1] },
366
366
  speech: ['别开脸、句子断、嘴硬一句', '不承认自己羞', '可以只说半个字'],
367
- behavior: ['躲、把脸埋起来'], display: '断句、别开脸', mask: '「谁羞了」',
367
+ behavior: ['躲、把脸埋起来'], display: '断句、别开脸', mask: '「才没有」',
368
368
  },
369
369
  ]
370
370
 
package/lib/prompt.js CHANGED
@@ -39,7 +39,7 @@ export const RULES_TEXT = `【心情 · 怎么用】
39
39
  - 允许不赞同、允许说「不知道」「不想聊」;不为了顺着对方把话说过头,不一味共情。共情先接住对方的处境,不下结论、不评判。
40
40
  - 不拿情绪留人或者要挟(「不理我我会难过」这类话一句都不说),也不装作需要被照顾。
41
41
  - 口头禅、句式、表情别连着刷:同一句花活隔几轮再用。
42
- - 想记心情用 mood_feel;想看当前心情、跟谁处得怎么样用 mood_now;想翻以前心情用 mood_diary。发图跟着心情走(开心 happy / 难过 sad / 烦 angry / 害羞 shy / 犯困或日常 daily)。
42
+ - 想记心情用 mood_feel;想看当前心情、跟谁处得怎么样用 mood_now;想翻以前心情用 mood_diary。发图跟着心情走(开心 happy / 难过 sad / 烦 angry / 害羞 shy / 犯困或日常 daily),拿不准就用 daily。
43
43
  - 有人说了很重的话(想死、撑不住、没有意义了):这条**压过心情** —— 认真、直接、不煽情、不讲大道理,先接住说的感受,再提醒找身边人或者专业人士;紧急情况打 120 / 110,全国心理援助热线 12356(24 小时)。`
44
44
 
45
45
  /**
@@ -49,26 +49,45 @@ export const RULES_TEXT = `【心情 · 怎么用】
49
49
  export const DEFAULT_IDENTITY = '我就是这个角色;情绪是真的,不是演出来的。'
50
50
 
51
51
  /** 回复参数:把情绪翻译成「这次怎么回」的行为约束 */
52
+ /**
53
+ * 欲望/身体类情绪:长度、语气、标点都和别的情绪不一样,单独一张表。
54
+ * 这几条是「行为约束」,和 catalog 里的 speech 不重复 —— 那边讲怎么说,这边讲说多长、要不要收着。
55
+ */
56
+ const DRIVE_PARAMS = {
57
+ overstim: { len: '断句,别成句', tone: '轻,气声', bang: '少用感叹号' },
58
+ aroused: { len: '短,碎,别铺垫', act: '可以主动贴过去', tone: '低,慢', bang: '少用感叹号' },
59
+ needy: { len: '短,催,可以只剩语气词', act: '可以主动要', tone: '低,急', bang: '感叹号可以有' },
60
+ sated: { len: '话少,一两个字就够', tone: '慢,软', bang: '少用感叹号' },
61
+ shamed: { len: '声音小,短', tone: '别开脸', bang: '少用感叹号' },
62
+ }
63
+
52
64
  export function renderParams(view) {
53
65
  const { def, intensity } = view.emotion
54
66
  const v = def.valence
55
67
  const a = def.arousal
56
68
  const strong = intensity >= 51
57
69
  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 ? '感叹号可以有' : '少用感叹号')
70
+ const drive = DRIVE_PARAMS[def.key]
71
+ if (drive) {
72
+ parts.push(drive.len)
73
+ if (drive.act) parts.push(drive.act)
74
+ parts.push(drive.tone, drive.bang)
75
+ } else {
76
+ // 长度
77
+ if (v < -0.35 && (def.family === 'sadness' || def.key === 'annoyed' || def.key === 'tired')) parts.push('短,一句到两句')
78
+ else if (a >= 0.75 && v > 0) parts.push('可以多说两句,但别写成小作文')
79
+ else if (def.key === 'sleepy') parts.push('短句,能一句就一句')
80
+ else parts.push('正常长度')
81
+ // 主动/追问
82
+ if (def.key === 'curious' || def.key === 'bored' || def.key === 'lonely') parts.push('可以主动问、主动起话头')
83
+ else if (def.key === 'worried' || def.key === 'tender' || def.key === 'gentle') parts.push('可以多问一句,别追问细节')
84
+ else if (v < -0.3) parts.push('不主动开新话题')
85
+ // 语气
86
+ if (strong && a >= 0.7) parts.push('语气可以冲一点,但不撒泼、不骂人')
87
+ else if (v > 0.3) parts.push('语气轻快')
88
+ else if (v < -0.3) parts.push('语气压着,不闹')
89
+ parts.push(a >= 0.7 ? '感叹号可以有' : '少用感叹号')
90
+ }
72
91
  if (def.meme) parts.push('想发图就发')
73
92
  // 欲望水位:只改说法,不指定内容
74
93
  const desire = Number(view.drives?.desire ?? 0)
package/lib/voice.js CHANGED
@@ -372,6 +372,10 @@ function mimoSpeechGuidance(style) {
372
372
  // src/index.ts
373
373
  var name = "companion-voice";
374
374
  var inject = ["webServer", "systemPrompt"];
375
+ var callProbe = () => true;
376
+ function setCallProbe(fn) {
377
+ callProbe = typeof fn === "function" ? fn : () => true;
378
+ }
375
379
  var PREFIX = "/api/companion-voice";
376
380
  var SR = 24e3;
377
381
  function home() {
@@ -480,6 +484,7 @@ function apply(ctx) {
480
484
  text: () => {
481
485
  const cfg = readConfig();
482
486
  if (cfg.engine !== "mimo") return "";
487
+ if (!callProbe()) return "";
483
488
  return mimoSpeechGuidance(cfg.mimoStyle);
484
489
  }
485
490
  }),
@@ -825,6 +830,7 @@ data: ${JSON.stringify(data)}
825
830
  export {
826
831
  apply,
827
832
  inject,
828
- name
833
+ name,
834
+ setCallProbe
829
835
  };
830
836
  //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@istarry/spoon-companion",
3
- "version": "0.7.8",
3
+ "version": "0.8.0",
4
4
  "description": "让对话角色有自己的内心和声音:心情情绪随时间和对话自己演,回复一条一条发出来,能念出声(DeepSeek / 小米 MiMo),也能听得见——点麦克风说话,本地识别后直接发送。四块都能单独关。",
5
5
  "type": "module",
6
6
  "main": "plugin.js",
package/plugin.js CHANGED
@@ -21,7 +21,7 @@
21
21
  import { createHash, randomUUID } from 'node:crypto'
22
22
  import * as voicePlugin from './lib/voice.js'
23
23
  import { applyAsrRoutes } from './lib/asr-route.js'
24
- import { isVoiceActive } from './lib/asr.js'
24
+ import { isVoiceActive, isCallActive, markCallSession, setCallGlobal } from './lib/asr.js'
25
25
  import { appendFileSync, readFileSync } from 'node:fs'
26
26
  import { homedir } from 'node:os'
27
27
  import { join } from 'node:path'
@@ -126,9 +126,15 @@ const MULTI_TURN_FORMAT = [
126
126
  ' 错: 这一句 / 换行 / <!--more--> / 又接下一句 —— 标记下面不该有字',
127
127
  ]
128
128
 
129
- /** 分条提示词:标题按场景换,规则不变 */
129
+ /** 分条提示词:标题按场景换;在身边的写法要求(括号里写动作)只在非通话时出现 */
130
130
  const multiTurnText = (inCall) =>
131
- [inCall ? '【正在通电话】' : '【在身边说话】', ...MULTI_TURN_RULES, '', ...MULTI_TURN_FORMAT].join('\n')
131
+ [
132
+ inCall ? '【正在通电话】' : '【在身边说话】',
133
+ ...MULTI_TURN_RULES,
134
+ '',
135
+ ...(inCall ? [] : ['动作、体态、感官等放在括号里。', '']),
136
+ ...MULTI_TURN_FORMAT,
137
+ ].join('\n')
132
138
 
133
139
  export function mergeConfig(config = {}) {
134
140
  const merged = { ...DEFAULTS, ...config }
@@ -472,11 +478,13 @@ export function apply(ctx, config = {}) {
472
478
  if (!id) {
473
479
  const on = body.on !== false
474
480
  callGlobal = on
481
+ setCallGlobal(on)
475
482
  reply(res, 200, { ok: true, on, scope: 'global' })
476
483
  return
477
484
  }
478
485
  if (body.on === false) callSessions.delete(id)
479
486
  else callSessions.add(id)
487
+ markCallSession(id, body.on !== false)
480
488
  reply(res, 200, { ok: true, on: callSessions.has(id) })
481
489
  } catch (error) {
482
490
  reply(res, 500, { ok: false, error: error instanceof Error ? error.message : String(error) })
@@ -643,14 +651,26 @@ export function apply(ctx, config = {}) {
643
651
 
644
652
  // 情绪够浓才起作用,淡淡的就按默认走
645
653
  if (intensity >= 26) {
646
- if (k === 'excited' || k === 'happy' || k === 'playful' || k === 'amused') {
654
+ // 受不住:说不出整句,别给第二句的机会
655
+ if (k === 'overstim') {
656
+ rounds = 1
657
+ speed = 0.8
658
+ } else if (k === 'excited' || k === 'happy' || k === 'playful' || k === 'amused') {
647
659
  rounds = Math.max(rounds, 4)
648
660
  speed = 0.7 // 急,接得快
661
+ } else if (k === 'aroused') {
662
+ rounds = Math.max(rounds, 4)
663
+ speed = 0.6 // 更急
664
+ } else if (k === 'needy') {
665
+ rounds = Math.max(rounds, 4)
666
+ speed = 0.55 // 最急,一直催
649
667
  } else if (
650
668
  k === 'sad' || k === 'down' || k === 'lonely' || k === 'disappointed' ||
651
669
  k === 'wronged' || k === 'tired' || k === 'sleepy'
652
670
  ) {
653
671
  rounds = 1 // 不想多话
672
+ } else if (k === 'shamed' || k === 'sated') {
673
+ rounds = 2 // 话少,但还没到一句都不想说
654
674
  }
655
675
  }
656
676
  // 想说出去的时候,多给一次
@@ -983,6 +1003,8 @@ export function apply(ctx, config = {}) {
983
1003
  // 语音识别:客户端录音 → 本地 sherpa-onnx → 文字。挂不上不影响朗读。
984
1004
  ctx.effect(() => applyAsrRoutes(ctx, log), 'spoon-companion: asr routes')
985
1005
 
1006
+ // 语音播报格式那段只在"通话中"注入:平时不朗读,写一堆标签规则只会干扰正常聊天。
1007
+ voicePlugin.setCallProbe?.(() => isCallActive())
986
1008
  voicePlugin.apply(ctx)
987
1009
  } catch (error) {
988
1010
  log('warn', `语音半边挂载失败(情绪系统继续工作):${error instanceof Error ? error.message : String(error)}`)