@istarry/spoon-companion 0.7.9 → 0.8.1

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,35 @@ 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
+ * 这个会话现在算不算在通话。
154
+ * **只认客户端那把开关** —— 点了通话才算,点挂断就不算。
155
+ * 音频活动不参与判断:录音可能在页面一打开就在跑,拿它当证据会把普通聊天误判成通话。
156
+ */
157
+ export function isCallActive(sessionId) {
158
+ if (callGlobal) return true
159
+ return Boolean(sessionId && callSessions.has(String(sessionId)))
160
+ }
161
+
133
162
  export function feed(pcm, rate = 16000) {
134
163
  lastFeedAt = Date.now()
135
164
  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() {
@@ -479,7 +483,9 @@ function apply(ctx) {
479
483
  order: 200,
480
484
  text: () => {
481
485
  const cfg = readConfig();
486
+ if (cfg.enabled === false) return "";
482
487
  if (cfg.engine !== "mimo") return "";
488
+ if (!callProbe()) return "";
483
489
  return mimoSpeechGuidance(cfg.mimoStyle);
484
490
  }
485
491
  }),
@@ -825,6 +831,7 @@ data: ${JSON.stringify(data)}
825
831
  export {
826
832
  apply,
827
833
  inject,
828
- name
834
+ name,
835
+ setCallProbe
829
836
  };
830
837
  //# 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.1",
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 { 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) })
@@ -496,21 +504,15 @@ export function apply(ctx, config = {}) {
496
504
 
497
505
  /**
498
506
  * 这个会话现在在不在通话里。
499
- * 三条任意一条成立就算:真的在送音频 / 客户端报了全局开关 / 这个会话被单独标过。
507
+ * **只认客户端那把开关** —— 点了通话才算。不拿"有没有在送音频"当证据:
508
+ * 录音可能在页面一打开就在跑,拿它判断会把普通打字聊天误判成通话。
500
509
  */
501
510
  const inCallOf = (context) => {
502
511
  try {
503
512
  if ((settings.callMode ?? {}).enabled === false) return false
504
513
  const session = sessionOf(context)
505
514
  if (!session) return false
506
- const id = String(session.id ?? '')
507
- let voiceActive = false
508
- try {
509
- voiceActive = isVoiceActive()
510
- } catch {
511
- voiceActive = false
512
- }
513
- return voiceActive || callGlobal || Boolean(id && callSessions.has(id))
515
+ return isCallActive(String(session.id ?? ''))
514
516
  } catch {
515
517
  return false
516
518
  }
@@ -995,6 +997,8 @@ export function apply(ctx, config = {}) {
995
997
  // 语音识别:客户端录音 → 本地 sherpa-onnx → 文字。挂不上不影响朗读。
996
998
  ctx.effect(() => applyAsrRoutes(ctx, log), 'spoon-companion: asr routes')
997
999
 
1000
+ // 语音播报格式那段只在"通话中"注入:平时不朗读,写一堆标签规则只会干扰正常聊天。
1001
+ voicePlugin.setCallProbe?.(() => isCallActive())
998
1002
  voicePlugin.apply(ctx)
999
1003
  } catch (error) {
1000
1004
  log('warn', `语音半边挂载失败(情绪系统继续工作):${error instanceof Error ? error.message : String(error)}`)