@istarry/spoon-companion 0.7.9 → 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/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.9",
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) })
@@ -995,6 +1003,8 @@ export function apply(ctx, config = {}) {
995
1003
  // 语音识别:客户端录音 → 本地 sherpa-onnx → 文字。挂不上不影响朗读。
996
1004
  ctx.effect(() => applyAsrRoutes(ctx, log), 'spoon-companion: asr routes')
997
1005
 
1006
+ // 语音播报格式那段只在"通话中"注入:平时不朗读,写一堆标签规则只会干扰正常聊天。
1007
+ voicePlugin.setCallProbe?.(() => isCallActive())
998
1008
  voicePlugin.apply(ctx)
999
1009
  } catch (error) {
1000
1010
  log('warn', `语音半边挂载失败(情绪系统继续工作):${error instanceof Error ? error.message : String(error)}`)