jbc-dssi 1.1.145 → 1.1.147

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/index.js CHANGED
@@ -25,6 +25,7 @@ import { createRestartAutocheck } from './lib/restart-autocheck.js';
25
25
  import { createGroupsEventBuffer, shouldBufferGroupEvent } from './lib/groups-event-buffer.js';
26
26
  import { createScheduleEngine } from './lib/schedules.js';
27
27
  import { createProcEngine } from './lib/proc-commands.js';
28
+ import { createTtsEngine } from './lib/tts.js';
28
29
  import { createAppReleaseWatch } from './lib/app-release-watch.js';
29
30
  import { PLUGIN_FEATURES } from './lib/plugin-features.js';
30
31
  import { MAX_FILE_BYTES, mimeOfFile, resolveWorkspaceFilePath } from './lib/workspace-bytes.js';
@@ -1953,6 +1954,8 @@ class BridgeConnection {
1953
1954
  this.schedules = opts.schedules ?? null;
1954
1955
  /** 程序指令执行器(2026-11-09):手机 proc_* 命令的电脑侧实现。 */
1955
1956
  this.proc = opts.proc ?? null;
1957
+ /** DeepSeek 语音合成(2026-11-25):首次用到才创建,连接关闭时停掉。 */
1958
+ this.tts = null;
1956
1959
  /** 群聊能力(GROUPS_* / ROOM_SNAPSHOT 帧);随群模块/配置开关启用。 */
1957
1960
  this.groups = opts.groups ?? null;
1958
1961
  this.botsSeq = 0;
@@ -2007,6 +2010,8 @@ class BridgeConnection {
2007
2010
  ...(this.ctx.get?.('commands') && this.ctx.get?.('sessionController') ? ['compact'] : []),
2008
2011
  // 向上翻更早的历史:需要会话控制器(官方 page() 分页)
2009
2012
  ...(this.ctx.get?.('sessionController') ? ['earlier'] : []),
2013
+ // DeepSeek 语音合成:电脑侧代持 Key,手机只发文本
2014
+ ...(this.ctx.get?.('credentials') ? ['tts'] : []),
2010
2015
  'rename',
2011
2016
  ],
2012
2017
  });
@@ -2155,6 +2160,48 @@ class BridgeConnection {
2155
2160
  this.#fire('bots-refresh', this.#refreshBots({ force: true }));
2156
2161
  }
2157
2162
 
2163
+ /**
2164
+ * DeepSeek 语音合成(2026-11-25 老谢定):手机只发文本和音色,
2165
+ * 电脑侧用 DeepSeek Key 调 `/v1/tools/tts`,PCM 边下边回、手机边收边播。
2166
+ *
2167
+ * 为什么放在电脑侧:Key 一旦打进 App 就等于公开;而智能体本来就在电脑上跑,
2168
+ * 语音跟它同一条链路,电脑离线时语音本来也没意义。
2169
+ */
2170
+ async #ttsCommand(payload) {
2171
+ const op = payload?.op;
2172
+ const id = typeof payload?.id === 'string' ? payload.id : '';
2173
+ if (id === '') return;
2174
+ if (op === 'stop') {
2175
+ this.tts?.stop(id);
2176
+ return;
2177
+ }
2178
+ if (op !== 'start' || this.closed) return;
2179
+ this.tts ??= createTtsEngine({
2180
+ log: (message) => this.opts.log(message),
2181
+ resolveKey: () => this.#deepseekKey(),
2182
+ });
2183
+ await this.tts.speak({
2184
+ id,
2185
+ text: payload?.text,
2186
+ voice: payload?.voice,
2187
+ onChunk: (frame) => this.send(FRAME.TTS_AUDIO, frame),
2188
+ onDone: (frame) => this.send(FRAME.TTS_AUDIO, { id, done: true, bytes: frame?.bytes ?? 0 }),
2189
+ onError: (error) => this.send(FRAME.TTS_AUDIO, { id, error }),
2190
+ });
2191
+ }
2192
+
2193
+ async #deepseekKey() {
2194
+ try {
2195
+ const credentials = this.ctx.get('credentials');
2196
+ const hit = await credentials?.resolve?.('DEEPSEEK_API_KEY');
2197
+ if (hit?.value) return hit.value;
2198
+ } catch (err) {
2199
+ this.opts.log(`[tts] 读取 DeepSeek Key 失败: ${err?.message ?? err}`);
2200
+ }
2201
+ const env = process.env.DEEPSEEK_API_KEY;
2202
+ return typeof env === 'string' && env !== '' ? env : null;
2203
+ }
2204
+
2158
2205
  #onClose() {
2159
2206
  this.closed = true;
2160
2207
  /*
@@ -2175,6 +2222,12 @@ class BridgeConnection {
2175
2222
  }
2176
2223
  this.followAbort?.abort();
2177
2224
  this.followAbort = null;
2225
+ try {
2226
+ this.tts?.stopAll();
2227
+ } catch {
2228
+ /* 语音已停 */
2229
+ }
2230
+ this.tts = null;
2178
2231
  // 连接断了,挂着没人答的审批一律交回浏览器
2179
2232
  for (const resolve of this.pending.values()) resolve(null);
2180
2233
  this.pending.clear();
@@ -2392,6 +2445,10 @@ class BridgeConnection {
2392
2445
  this.#fire('read-file-bytes', this.#readFileBytes(payload));
2393
2446
  break;
2394
2447
 
2448
+ case FRAME.TTS_CMD:
2449
+ this.#fire('tts', this.#ttsCommand(payload));
2450
+ break;
2451
+
2395
2452
  case FRAME.FOCUS: {
2396
2453
  this.focused = payload?.active === true;
2397
2454
  /*
@@ -6,6 +6,7 @@ import path from 'node:path';
6
6
  import { randomUUID } from 'node:crypto';
7
7
 
8
8
  import { FRAME } from './protocol.js';
9
+ import { jbcStateDir } from './state-dir.js';
9
10
 
10
11
  export const REQUEST_TIMEOUT_MS = 5 * 60 * 1000;
11
12
 
@@ -16,7 +17,8 @@ const MAX_DEVICE_NAME = 32;
16
17
  const MAX_DEVICE_ID = 128;
17
18
 
18
19
  export function authorizedDeviceFilePath() {
19
- return path.join(os.homedir(), '.dsh', 'jbc-dssi', 'authorized-device.json');
20
+ // 2026-11-24:按 dsh profile 隔离(桌面版 / Web 版各自的授权设备列表)
21
+ return path.join(jbcStateDir(), 'authorized-device.json');
20
22
  }
21
23
 
22
24
  export function normalizeDevice(payload) {
@@ -4,4 +4,5 @@ export const PLUGIN_FEATURES = [
4
4
  'app_release_push',
5
5
  'file_bytes',
6
6
  'workspace_products',
7
+ 'speech_tts',
7
8
  ];
package/lib/protocol.js CHANGED
@@ -126,6 +126,10 @@ export const FRAME = {
126
126
 
127
127
  COMPACT_DONE: 0x7c,
128
128
 
129
+ TTS_CMD: 0x7d,
130
+
131
+ TTS_AUDIO: 0x7e,
132
+
129
133
  PING: 0x40,
130
134
  };
131
135
 
@@ -6,6 +6,7 @@ import { get } from 'node:http';
6
6
  import { connect } from 'node:net';
7
7
  import { dirname, join } from 'node:path';
8
8
  import { homedir } from 'node:os';
9
+ import { jbcStateDir } from './state-dir.js';
9
10
 
10
11
  const DEFAULTS = {
11
12
  enabled: true,
@@ -411,7 +412,7 @@ export function baselineCheck({ markerMtimeMs, config, log }) {
411
412
  missing.push('workspaces');
412
413
  }
413
414
 
414
- const deviceFile = String(config.deviceAuthFile || join(homedir(), '.dsh', 'jbc-dssi', 'authorized-device.json'));
415
+ const deviceFile = String(config.deviceAuthFile || join(jbcStateDir(), 'authorized-device.json'));
415
416
  try {
416
417
  const raw = readFileSync(deviceFile, 'utf8');
417
418
  const json = JSON.parse(raw);
@@ -0,0 +1,59 @@
1
+ /**
2
+ * 插件状态目录(2026-11-24 老谢定:按 profile 隔离)。
3
+ *
4
+ * 【为什么需要】
5
+ * 桌面版 dsh 和 Web 版 dsh 是两个独立进程,但它们以前共用同一个配对文件
6
+ * `~/.dsh/jbc-dssi/pairing.json` —— 于是两个实例拿着**同一个 pairId** 连中继,
7
+ * 中继规则是"同一房间同一角色只留一条",两边互相踢,心跳断、手机永远连不上。
8
+ *
9
+ * 改成按 profile 分目录后:
10
+ * ~/.dsh/jbc-dssi/desktop/pairing.json ← 桌面版自己的配对
11
+ * ~/.dsh/jbc-dssi/web/pairing.json ← Web 版自己的配对
12
+ * 两个 dsh 同时开也不会再打架,用户不用记着"只能开一个"。
13
+ *
14
+ * 【怎么认出自己是哪个 profile】
15
+ * 1. 环境变量 `DSH_PROFILE`(宿主如果给了最准);
16
+ * 2. 否则从插件包自身路径反推:`<profile>/node_modules/jbc-dssi/...` → `<profile>` 的目录名;
17
+ * 3. 都拿不到 -> 退回老的全局目录(保证老环境行为不变)。
18
+ *
19
+ * 【哪些状态跟着 profile 走】
20
+ * 配对、授权设备 —— 这两个直接决定"这台 dsh 能不能和手机连"。
21
+ * 其余全局状态(鲸聊界面配置 ui-config、定时任务等)保持原位置不动,
22
+ * 避免升级后老数据"看起来丢了"。
23
+ */
24
+ import os from 'node:os';
25
+ import path from 'node:path';
26
+
27
+ const PLUGIN_MARKER = '/node_modules/jbc-dssi/';
28
+
29
+ /** 当前 dsh 的 profile 名(拿不到返回 null)。 */
30
+ export function resolveProfileTag() {
31
+ const env = String(process.env.DSH_PROFILE ?? '').trim();
32
+ if (env !== '') return env;
33
+ try {
34
+ const fileUrl = import.meta.url;
35
+ const filePath = decodeURIComponent(new URL(fileUrl).pathname);
36
+ // Windows 下 pathname 形如 /C:/Users/.../profiles/web/node_modules/...,分隔符仍是 /
37
+ const idx = filePath.indexOf(PLUGIN_MARKER);
38
+ if (idx > 0) {
39
+ const profileDir = filePath.slice(0, idx);
40
+ const tag = path.basename(profileDir);
41
+ if (tag !== '' && tag !== 'profiles' && tag !== '.dsh') return tag;
42
+ }
43
+ } catch {
44
+ /* 路径反推失败就走全局目录 */
45
+ }
46
+ return null;
47
+ }
48
+
49
+ /** 插件状态根目录:有 profile 时是 `~/.dsh/jbc-dssi/<profile>`,否则老路径。 */
50
+ export function jbcStateDir() {
51
+ const root = path.join(os.homedir(), '.dsh', 'jbc-dssi');
52
+ const tag = resolveProfileTag();
53
+ return tag === null ? root : path.join(root, tag);
54
+ }
55
+
56
+ /** 老(全局)状态目录,仅用于兼容读取/迁移提示。 */
57
+ export function legacyStateDir() {
58
+ return path.join(os.homedir(), '.dsh', 'jbc-dssi');
59
+ }
package/lib/tts.js ADDED
@@ -0,0 +1,118 @@
1
+ const TTS_URL = 'https://api.deepseek.com/v1/tools/tts';
2
+
3
+ export const TTS_VOICES = [
4
+ { id: 'stella', name: '海星', gender: '女声' },
5
+ { id: 'tide', name: '暗潮', gender: '男声' },
6
+ ];
7
+
8
+ const MAX_CHARS = 2000;
9
+ const MAX_INFLIGHT = 2;
10
+
11
+ function voiceOf(id) {
12
+ return TTS_VOICES.some((v) => v.id === id) ? id : TTS_VOICES[0].id;
13
+ }
14
+
15
+ function errorOf(status, detail) {
16
+ const text = String(detail ?? '').slice(0, 300);
17
+ if (status === 400 && /Content Exists Risk|content_filter/i.test(text)) {
18
+ return { code: 'content_filter', message: '这段内容没通过语音审核' };
19
+ }
20
+ if (status === 400) {
21
+ return { code: 'invalid_input', message: text || '这段内容无法合成语音' };
22
+ }
23
+ if (status === 401 || status === 403) {
24
+ return { code: 'auth', message: 'DeepSeek Key 无效或没有语音权限' };
25
+ }
26
+ if (status === 402) {
27
+ return { code: 'quota', message: 'DeepSeek 余额不足' };
28
+ }
29
+ if (status === 429) {
30
+ return { code: 'rate_limit', message: '语音合成太频繁,请稍后再试' };
31
+ }
32
+ if (status === 503 || status === 504) {
33
+ return { code: 'server', message: '语音服务繁忙,请重试' };
34
+ }
35
+ return { code: 'http', message: text || `语音合成失败(${status})` };
36
+ }
37
+
38
+ export function createTtsEngine({ log, resolveKey, fetchImpl = globalThis.fetch }) {
39
+ const inflight = new Map();
40
+
41
+ async function speak({ id, text, voice, onChunk, onDone, onError }) {
42
+ if (typeof id !== 'string' || id === '') return;
43
+ const body = String(text ?? '').slice(0, MAX_CHARS);
44
+ if (body.trim() === '') {
45
+ onError?.({ code: 'invalid_input', message: '没有可朗读的文本' });
46
+ return;
47
+ }
48
+ if (inflight.size >= MAX_INFLIGHT) {
49
+ onError?.({ code: 'busy', message: '语音合成排队中,请稍后再试' });
50
+ return;
51
+ }
52
+ const key = await resolveKey();
53
+ if (!key) {
54
+ onError?.({ code: 'no_key', message: '电脑上没有 DeepSeek API Key' });
55
+ return;
56
+ }
57
+
58
+ const abort = new AbortController();
59
+ inflight.set(id, abort);
60
+ let seq = 0;
61
+ try {
62
+ const res = await fetchImpl(TTS_URL, {
63
+ method: 'POST',
64
+ headers: {
65
+ Authorization: `Bearer ${key}`,
66
+ 'Content-Type': 'application/json',
67
+ },
68
+ body: JSON.stringify({ text: body, voice_id: voiceOf(voice), format: 'pcm' }),
69
+ signal: abort.signal,
70
+ });
71
+ if (!res.ok) {
72
+ const detail = await res.text().catch(() => '');
73
+ log?.(`[tts] HTTP ${res.status}: ${String(detail).slice(0, 160)}`);
74
+ onError?.(errorOf(res.status, detail));
75
+ return;
76
+ }
77
+ let bytes = 0;
78
+ for await (const chunk of res.body) {
79
+ if (abort.signal.aborted) return;
80
+ const data = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
81
+ if (data.length === 0) continue;
82
+ bytes += data.length;
83
+ onChunk?.({ id, seq: seq++, data: data.toString('base64') });
84
+ }
85
+ if (abort.signal.aborted) return;
86
+ if (bytes === 0) {
87
+ onError?.({ code: 'empty_audio', message: '语音合成没有返回音频' });
88
+ return;
89
+ }
90
+ onDone?.({ id, bytes });
91
+ } catch (err) {
92
+ if (!abort.signal.aborted) {
93
+ log?.(`[tts] 请求失败: ${err?.message ?? err}`);
94
+ onError?.({ code: 'network', message: '语音合成失败,检查电脑网络' });
95
+ }
96
+ } finally {
97
+ inflight.delete(id);
98
+ }
99
+ }
100
+
101
+ function stop(id) {
102
+ const abort = inflight.get(id);
103
+ if (abort) {
104
+ inflight.delete(id);
105
+ try {
106
+ abort.abort();
107
+ } catch {
108
+ /* 已中止 */
109
+ }
110
+ }
111
+ }
112
+
113
+ function stopAll() {
114
+ for (const id of [...inflight.keys()]) stop(id);
115
+ }
116
+
117
+ return { speak, stop, stopAll, inflight };
118
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jbc-dssi",
3
- "version": "1.1.145",
3
+ "version": "1.1.147",
4
4
  "type": "module",
5
5
  "description": "鲸聊 - 不只是手机遥控操作电脑,而是将电脑级的Agent能力装进手机",
6
6
  "keywords": [
package/pairing/store.js CHANGED
@@ -25,9 +25,10 @@ import fs from 'node:fs';
25
25
  import path from 'node:path';
26
26
  import os from 'node:os';
27
27
  import { createHash } from 'node:crypto';
28
+ import { jbcStateDir } from '../lib/state-dir.js';
28
29
 
29
- /** 数据目录。放在 DSH 家目录下,与插件代码分离。 */
30
- const DIR = path.join(os.homedir(), '.dsh', 'jbc-dssi');
30
+ /** 数据目录:按 dsh profile 隔离(桌面版 / Web 版各一套,见 lib/state-dir.js)。 */
31
+ const DIR = jbcStateDir();
31
32
 
32
33
  /** 配对信息文件。 */
33
34
  const FILE = path.join(DIR, 'pairing.json');