dsh-watch-together 0.2.12 → 0.2.13

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.
@@ -0,0 +1,75 @@
1
+ /**
2
+ * playhead.js — "现在播到第几秒"对**外部**可见的那一层。
3
+ *
4
+ * ## 为什么需要它(而不是让调用方自己读)
5
+ *
6
+ * 位置有三条来路:扩展每秒上报(`position-bridge.js`)、调试端口每 15 秒探一次
7
+ * (`watch-browser.js`)、用户自己报的 cursor。**判断哪条可信、怎么外推、什么时候
8
+ * 承认不知道**,全在 `position.js` 那套锚点逻辑里。绕过去自己读 `position.json`
9
+ * 的调用方,等于把那套规则重写一遍 —— 十有八九会写出"位置停了还在往前推"。
10
+ *
11
+ * ## 为什么不做成从 watch-loop 里直接读
12
+ *
13
+ * `watch-loop → runtime-ports → voice-adapter → companion-api`,如果
14
+ * `companion-api` 再 import `watch-loop` 就成一个环。环在 ESM 里多数时候能跑,
15
+ * 但"多数时候"不是工程约定:一旦哪天有人在 watch-loop 顶层求值点什么,就会炸在
16
+ * 某个和本次改动毫无关系的测试里。所以循环**把结论推到这个小 store**,别人只读它。
17
+ *
18
+ * 写入方只有一个(`watch-loop.js` 的 `syncPlayhead()`),读取方可以有任意多个。
19
+ */
20
+
21
+ import { estimate } from './position.js';
22
+
23
+ /** 没有陪看时的固定形状 —— 字段齐全,调用方不用到处判空 */
24
+ const IDLE = Object.freeze({
25
+ active: false, input: null, dur: null, pos: null, confidence: 'unknown', staleMs: null,
26
+ });
27
+
28
+ let state = { active: false, input: null, dur: null, anchor: null };
29
+
30
+ /**
31
+ * 循环每一拍把当前状态推过来。
32
+ *
33
+ * `anchor` 是 `position.js` 的锚点原样对象:**不要把 pos 单独传**,
34
+ * 那样"读数多久以前读的"就丢了,外推和降级全都没法算。
35
+ */
36
+ export function setPlayhead({ active = true, input = null, dur = null, anchor = null } = {}) {
37
+ state = {
38
+ active: Boolean(active),
39
+ input: input ?? null,
40
+ dur: Number.isFinite(dur) ? Number(dur) : null,
41
+ anchor: anchor ?? null,
42
+ };
43
+ }
44
+
45
+ /** 停表 / 换片时清掉,别让适配器拿着上一支片子的位置去丢音频 */
46
+ export function clearPlayhead() {
47
+ state = { active: false, input: null, dur: null, anchor: null };
48
+ }
49
+
50
+ /**
51
+ * 读当前位置。
52
+ *
53
+ * **`confidence === 'unknown'` 时 `pos` 是 `null`,不是 0。**
54
+ * 这条和仓里其它地方反复踩的 `Number(null) === 0` 是同一个坑:
55
+ * 给个 0,调用方就会算出"我合成的音频比当前位置晚了 300 秒",然后默默丢光。
56
+ * 不知道就该是不知道。
57
+ *
58
+ * @param {number} [now] 注入当前时刻(测试/仿真用虚拟时间)
59
+ * @returns {{active:boolean, input:string|null, dur:number|null,
60
+ * pos:number|null, confidence:'fresh'|'extrapolated'|'unknown', staleMs:number|null}}
61
+ */
62
+ export function readPlayhead(now = Date.now()) {
63
+ if (!state.active || !state.anchor) return { ...IDLE, input: state.input, dur: state.dur };
64
+ const est = estimate(state.anchor, now);
65
+ const dur = state.dur ?? (Number.isFinite(state.anchor.dur) ? state.anchor.dur : null);
66
+ return {
67
+ active: true,
68
+ input: state.input,
69
+ dur,
70
+ // 承认不知道时不给数字:见上面那段
71
+ pos: est.confidence === 'unknown' || !Number.isFinite(est.pos) ? null : +est.pos.toFixed(1),
72
+ confidence: est.confidence,
73
+ staleMs: Number.isFinite(est.staleMs) ? Math.round(est.staleMs) : null,
74
+ };
75
+ }
package/src/public-api.js CHANGED
@@ -16,6 +16,13 @@ export {
16
16
  export { createRecognitionInterface } from './recognition-interface.js';
17
17
  export { createTtsInterface } from './tts-interface.js';
18
18
  export { createWindowInterface } from './window-interface.js';
19
+ export {
20
+ DEFAULT_HORIZON_SEC,
21
+ MAX_HORIZON_SEC,
22
+ companionApi,
23
+ position,
24
+ upcoming,
25
+ } from './companion-api.js';
19
26
  export {
20
27
  WINDOW_PROTOCOL_VERSION,
21
28
  WINDOW_EVENT_TYPES,
@@ -11,8 +11,10 @@
11
11
  * ```
12
12
  *
13
13
  * 适配器模块导出 `attach(api)`(或 default 导出同名函数),`api` 是
14
- * `{ onSpeech, speechSubscribers, emitSpeech, emitSpoken, lastSpeech }` ——
14
+ * `{ onSpeech, speechSubscribers, emitSpeech, emitSpoken, lastSpeech, upcoming, position }` ——
15
15
  * 合成完真的要出声时调一次 `emitSpoken(e)`,展示层才知道"字可以出现了"(见 `speech-bus.js`)。
16
+ * `upcoming()` / `position()` 是**只读**的两个问题"接下来念什么 / 现在播到哪"
17
+ * (形状见 `companion-api.js`,issue #4 定的边界:核心不做合成)。
16
18
  * `attach` 可以返回一个 detach 函数;不返回也行,但要自己管好退订。
17
19
  *
18
20
  * 和 `speech-bus.js` 的分工:总线不认识"文件路径""import""配置文件",
@@ -27,6 +29,7 @@ import { pathToFileURL } from 'node:url';
27
29
  import {
28
30
  emitSpeech, emitSpoken, lastSpeech, onSpeech, speechSubscribers,
29
31
  } from './speech-bus.js';
32
+ import { companionApi } from './companion-api.js';
30
33
 
31
34
  /**
32
35
  * 已挂载的适配器:specifier → { detach, subscriptions }。
@@ -122,6 +125,10 @@ export async function loadVoiceAdapter(spec, { log = () => {} } = {}) {
122
125
  try {
123
126
  const detach = await attach(trackedApi({
124
127
  onSpeech, speechSubscribers, emitSpeech, emitSpoken, lastSpeech,
128
+ // 只读的两个问题:接下来念什么、现在播到哪。**不是**可选的装饰 ——
129
+ // 适配器靠它们决定预不预合成、seek 之后丢不丢已经合成好的音频。
130
+ upcoming: companionApi.upcoming,
131
+ position: companionApi.position,
125
132
  }, subscriptions));
126
133
  attached.set(raw, {
127
134
  detach: typeof detach === 'function' ? detach : null,
package/src/voice.js CHANGED
@@ -31,3 +31,12 @@ export {
31
31
  detachAdapters,
32
32
  loadVoiceAdapter,
33
33
  } from './voice-adapter.js';
34
+
35
+ // 适配器"拉"的那两个只读方法。平时从 `attach(api)` 拿就行;
36
+ // 导出是给测试和同进程的伴生插件直接用的(形状见 companion-api.js)。
37
+ export {
38
+ DEFAULT_HORIZON_SEC,
39
+ MAX_HORIZON_SEC,
40
+ position,
41
+ upcoming,
42
+ } from './companion-api.js';
package/src/watch-loop.js CHANGED
@@ -22,6 +22,7 @@ import { readBrowserPosition, looksLikeVideoUrl } from 'dsh-video-storyboard/bro
22
22
  import { readBridge } from './position-bridge.js';
23
23
  import { getTtsPort, getWindowPort } from './runtime-ports.js';
24
24
  import { readPositionOrNull } from './watch-browser.js';
25
+ import { clearPlayhead, setPlayhead } from './playhead.js';
25
26
 
26
27
  import { speechSeconds } from './speech.js';
27
28
  import { consumeLine, dueLine, hasLine, loadLine, upcomingLines, WATCH_LEAD } from './watch.js';
@@ -277,9 +278,27 @@ export function stopWatchLoop(reason = 'stopped') {
277
278
  if (loop.active) loop.log(`[watch] loop stopped (${reason})`);
278
279
  loop.active = false;
279
280
  loop.lastReason = reason;
281
+ // 停表之后"现在播到哪"就没有意义了。留着的话,适配器会按上一支片子的位置
282
+ // 去丢音频 / 预合成,而且 `pos` 会一直外推到 60 秒上限才降级成 unknown。
283
+ clearPlayhead();
280
284
  return loopStatus();
281
285
  }
282
286
 
287
+ /**
288
+ * 把当前锚点推给 `playhead.js`(声音适配器的 `api.position()` 读它)。
289
+ *
290
+ * **走 store 而不是让适配器 import 本模块**:`watch-loop → runtime-ports →
291
+ * voice-adapter → companion-api`,再反向 import 就成环了(见 playhead.js 文件头)。
292
+ *
293
+ * 每 2 秒一拍推一次理论上冗余,但这样"锚点变了"的每一条路径(扩展上报、CDP 探针、
294
+ * 用户自报 cursor)都自动覆盖到,不会漏掉某一条 —— 漏一条的表现是"适配器以为
295
+ * 没 seek,其实已经跳了",很难从现象反推回来。
296
+ */
297
+ function syncPlayhead() {
298
+ if (!loop.active) { clearPlayhead(); return; }
299
+ setPlayhead({ active: true, input: loop.input, dur: loop.dur, anchor: loop.anchor });
300
+ }
301
+
283
302
  /**
284
303
  * 起循环。重复调用先停旧的(同一时刻只允许一段陪看)。
285
304
  *
@@ -491,6 +510,7 @@ export function startWatchLoop({
491
510
  if (Number.isFinite(cursor)) {
492
511
  loop.anchor = makeAnchor({ pos: cursor, dur: loop.dur, status, updated: Date.now() }, Date.now());
493
512
  }
513
+ syncPlayhead();
494
514
  note('resumed (same input + same batch plan)');
495
515
  return { ok: true, resumed: true, ...loopStatus() };
496
516
  }
@@ -562,6 +582,9 @@ export function startWatchLoop({
562
582
  loop.anchor = Number.isFinite(cursor)
563
583
  ? makeAnchor({ pos: cursor, dur: null, status, updated: Date.now() })
564
584
  : null;
585
+ // 新一场:哪怕还没有锚点也要先推一次 —— 适配器这时候问 `position()` 应该看到
586
+ // `active: true / pos: null`("在陪看,但位置还不知道"),而不是上一支片子的残留值。
587
+ syncPlayhead();
565
588
 
566
589
  void readLatency().then((st) => { loop.aheadSec = aheadSeconds(st); });
567
590
 
@@ -748,6 +771,9 @@ export function startWatchLoop({
748
771
  loop.aheadSec = aheadSeconds(st);
749
772
  }
750
773
 
774
+ // 这一拍的位置已经落定(桥上报 / CDP 探针都跑完了),推给适配器那边
775
+ syncPlayhead();
776
+
751
777
  if (!loop.anchor) {
752
778
  loop.lastReason = 'no-anchor';
753
779
  // 从来没读到过位置:没有锚点可变旧,只能靠这个计时器兜底。