@burdenoff/fe-libs 2026.1117.5 → 2026.1117.7

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.
@@ -251,6 +251,14 @@ function W(e = {}) {
251
251
  listVoices: () => e.listVoices(),
252
252
  subscribeVoices: (t) => e.subscribeVoices(t),
253
253
  wouldSpeakOnAvatar: (e) => t(e).target === "avatar",
254
+ prefetch: (n, r) => {
255
+ let { voice: i, target: a } = t(r);
256
+ e.prefetch?.(n, a === "avatar" && i !== null ? {
257
+ ...r,
258
+ voice: i
259
+ } : r);
260
+ },
261
+ cancelPrefetch: () => e.cancelPrefetch?.(),
254
262
  speak: (n, a, o) => {
255
263
  r();
256
264
  let { voice: s, target: c } = t(a);
@@ -422,6 +422,7 @@ export * from './avatar/live/avatarVoice';
422
422
  export * from './promptDelivery';
423
423
  export * from './speech/appendSpeechQueue';
424
424
  export * from './voice/phraseSpeechConfig';
425
+ export * from './voice/firstClipPrefetchConfig';
425
426
  export * from './voice/phraseGate';
426
427
  export * from './voice/livePhraseSpeech';
427
428
  export * from './voice/phrasePlayback';
@@ -25,6 +25,12 @@ export interface LivePartState {
25
25
  export interface LiveMessageState {
26
26
  readonly created?: number;
27
27
  readonly completed?: number;
28
+ /**
29
+ * How the step ended (`stop`, `tool-calls`, `error`, ...). The bridge writes
30
+ * it on the frame that completes the step and not before: while a step is
31
+ * being written, nothing says whether it will call a tool.
32
+ */
33
+ readonly finish?: string;
28
34
  }
29
35
  export interface LiveDeltaState {
30
36
  /** +1 for every re-mint. */
@@ -36,12 +36,13 @@ function l(e) {
36
36
  function u(e, t) {
37
37
  let n = i(t.info), r = a(n?.id);
38
38
  if (!n || !r || n.role !== "assistant" || !e.messages.has(r) && e.messages.size >= 256) return e;
39
- let c = i(n.time), l = o(c?.created), u = o(c?.completed), d = e.messages.get(r);
39
+ let c = i(n.time), l = o(c?.created), u = o(c?.completed), d = a(n.finish)?.slice(0, 64), f = e.messages.get(r);
40
40
  return {
41
41
  ...e,
42
42
  messages: s(e.messages, r, {
43
- ...l === void 0 ? d?.created === void 0 ? {} : { created: d.created } : { created: l },
44
- ...u === void 0 ? d?.completed === void 0 ? {} : { completed: d.completed } : { completed: u }
43
+ ...l === void 0 ? f?.created === void 0 ? {} : { created: f.created } : { created: l },
44
+ ...u === void 0 ? f?.completed === void 0 ? {} : { completed: f.completed } : { completed: u },
45
+ ...d === void 0 ? f?.finish === void 0 ? {} : { finish: f.finish } : { finish: d }
45
46
  })
46
47
  };
47
48
  }
@@ -66,14 +66,14 @@ function E(e) {
66
66
  }
67
67
  };
68
68
  }
69
- var D = "data:audio/wav;base64,UklGRiQAAABXQVZFZm10IBAAAAABAAEARKwAAIhYAQACABAAZGF0YQAAAAA=", O = [], k = 4;
70
- function A() {
71
- for (let e of [...O]) e();
69
+ var ee = "data:audio/wav;base64,UklGRiQAAABXQVZFZm10IBAAAAABAAEARKwAAIhYAQACABAAZGF0YQAAAAA=", D = [], te = 4;
70
+ function O() {
71
+ for (let e of [...D]) e();
72
72
  }
73
- function j() {
74
- O.length = 0;
73
+ function k() {
74
+ D.length = 0;
75
75
  }
76
- function M(e, t) {
76
+ function ne(e, t) {
77
77
  return new Promise((n) => {
78
78
  if (t.aborted) {
79
79
  n();
@@ -88,41 +88,41 @@ function M(e, t) {
88
88
  t.addEventListener("abort", i, { once: !0 });
89
89
  });
90
90
  }
91
- function N(v) {
91
+ function A(v) {
92
92
  let y = v.fetchImpl ?? ((...e) => fetch(...e)), x = v.now ?? (() => Date.now()), S = v.setTimer ?? ((e, t) => {
93
93
  let n = setTimeout(e, t);
94
94
  return () => clearTimeout(n);
95
- }), C = null, w = null, A = null, j = !1, N = () => {
96
- if (!(j || typeof Audio != "function")) try {
97
- let e = A ?? new Audio();
98
- if (A = e, C === e) return;
99
- e.muted = !0, e.src = D, Promise.resolve(e.play()).then(() => {
100
- A === e && C !== e && e.pause();
95
+ }), C = null, w = null, O = null, k = !1, A = () => {
96
+ if (!(k || typeof Audio != "function")) try {
97
+ let e = O ?? new Audio();
98
+ if (O = e, C === e) return;
99
+ e.muted = !0, e.src = ee, Promise.resolve(e.play()).then(() => {
100
+ O === e && C !== e && e.pause();
101
101
  }, () => {});
102
102
  } catch {}
103
103
  };
104
- O.push(N), O.length > k && O.shift();
105
- let P = 0, F = null, I = null, L = null, R = null, z = 0, B = null, V = null, H = () => {
104
+ D.push(A), D.length > te && D.shift();
105
+ let j = 0, M = null, N = null, P = null, F = null, I = null, L = 0, R = null, z = null, re = () => {
106
106
  try {
107
107
  return v.getHeaders?.() ?? {};
108
108
  } catch {
109
109
  return {};
110
110
  }
111
- }, U = () => x() >= z ? !1 : V === null || h(H()) === V, W = /* @__PURE__ */ new Set(), G = () => {
112
- for (let e of [...W]) e();
113
- }, K = () => {
114
- B !== null && clearTimeout(B), B = null;
115
- }, q = (e) => {
116
- K(), B = setTimeout(() => {
117
- B = null;
118
- let e = z - x();
119
- e > 0 ? q(e) : G();
111
+ }, B = () => x() >= L ? !1 : z === null || h(re()) === z, V = /* @__PURE__ */ new Set(), H = () => {
112
+ for (let e of [...V]) e();
113
+ }, U = () => {
114
+ R !== null && clearTimeout(R), R = null;
115
+ }, W = (e) => {
116
+ U(), R = setTimeout(() => {
117
+ R = null;
118
+ let e = L - x();
119
+ e > 0 ? W(e) : H();
120
120
  }, e);
121
- }, J = (e, t) => {
122
- z = x() + e, V = t, q(e), G();
123
- }, Y = () => {
121
+ }, G = (e, t) => {
122
+ L = x() + e, z = t, W(e), H();
123
+ }, K = () => {
124
124
  w &&= (URL.revokeObjectURL(w), null);
125
- }, X = () => {
125
+ }, q = () => {
126
126
  if (C) {
127
127
  C.onended = null, C.onerror = null;
128
128
  try {
@@ -130,54 +130,64 @@ function N(v) {
130
130
  } catch {}
131
131
  C = null;
132
132
  }
133
- Y();
133
+ K();
134
+ };
135
+ function J(e, t) {
136
+ let n = a(t.lang) ?? t.lang;
137
+ return o(e, n, r, t.voice?.name ?? null, t.rate);
138
+ }
139
+ let Y = (e, t) => JSON.stringify(J(e, t)), X = () => {
140
+ I?.controller.abort(), I = null;
141
+ }, Z = (e, t, n) => {
142
+ let r = I;
143
+ return I = null, r ? r.key === Y(e, t) ? (n.aborted ? r.controller.abort() : n.addEventListener("abort", () => r.controller.abort(), { once: !0 }), r.request) : (r.controller.abort(), null) : null;
134
144
  };
135
- async function Z(t, n, i, l = !1) {
136
- let u = a(n.lang) ?? n.lang, d = o(t, u, r, n.voice?.name ?? null, n.rate), f = v.getHeaders?.() ?? {}, p;
137
- for (let t = 1; e(i), p = await y(v.endpoint, {
145
+ async function Q(t, n, r, i = !1) {
146
+ let a = J(t, n), o = v.getHeaders?.() ?? {}, l;
147
+ for (let t = 1; e(r), l = await y(v.endpoint, {
138
148
  method: "POST",
139
- signal: i,
149
+ signal: r,
140
150
  headers: {
141
151
  "content-type": "application/json",
142
- ...f
152
+ ...o
143
153
  },
144
- body: JSON.stringify(d)
145
- }), !p.ok; t += 1) {
146
- if (!(g.has(p.status) && t < _ && !i.aborted)) {
147
- let e = /* @__PURE__ */ Error(`hosted tts ${p.status}`);
148
- throw e.reason = l && p.status === 429 ? "rate-limited" : s(p.status), e.headers = h(f), e;
154
+ body: JSON.stringify(a)
155
+ }), !l.ok; t += 1) {
156
+ if (!(g.has(l.status) && t < _ && !r.aborted)) {
157
+ let e = /* @__PURE__ */ Error(`hosted tts ${l.status}`);
158
+ throw e.reason = i && l.status === 429 ? "rate-limited" : s(l.status), e.headers = h(o), e;
149
159
  }
150
- await M(b(p.headers.get("retry-after"), t), i);
160
+ await ne(b(l.headers.get("retry-after"), t), r);
151
161
  }
152
- let m = p.headers.get("content-type"), x = m?.includes("json") ? await p.json() : await p.arrayBuffer(), S = c(m, x);
153
- if (!S) {
162
+ let u = l.headers.get("content-type"), d = u?.includes("json") ? await l.json() : await l.arrayBuffer(), f = c(u, d);
163
+ if (!f) {
154
164
  let e = /* @__PURE__ */ Error("hosted tts returned no audio");
155
165
  throw e.reason = "unknown", e;
156
166
  }
157
- return S;
167
+ return f;
158
168
  }
159
- function Q(e, r, i) {
169
+ function $(e, r, i) {
160
170
  return new Promise((a, o) => {
161
- if (r !== P) {
171
+ if (r !== j) {
162
172
  a();
163
173
  return;
164
174
  }
165
175
  let s = new Blob([e.slice().buffer], { type: "audio/mpeg" });
166
- Y(), w = URL.createObjectURL(s);
176
+ K(), w = URL.createObjectURL(s);
167
177
  let c;
168
- t() ? (c = new Audio(w), n(c)) : (c = A ?? new Audio(), A = c, c.muted = !1, c.src = w), C = c, c.onended = () => {
169
- r === P && a();
178
+ t() ? (c = new Audio(w), n(c)) : (c = O ?? new Audio(), O = c, c.muted = !1, c.src = w), C = c, c.onended = () => {
179
+ r === j && a();
170
180
  }, c.onerror = () => {
171
- if (r !== P) {
181
+ if (r !== j) {
172
182
  a();
173
183
  return;
174
184
  }
175
185
  let e = /* @__PURE__ */ Error("audio playback failed");
176
186
  e.reason = "unknown", o(e);
177
187
  }, c.play().then(() => {
178
- c === A && !c.muted && (j = !0), r === P && i?.();
188
+ c === O && !c.muted && (k = !0), r === j && i?.();
179
189
  }, (e) => {
180
- if (r !== P) {
190
+ if (r !== j) {
181
191
  a();
182
192
  return;
183
193
  }
@@ -186,100 +196,109 @@ function N(v) {
186
196
  });
187
197
  });
188
198
  }
189
- let $ = {
199
+ let ie = {
190
200
  id: "hosted-tts",
191
201
  isSupported() {
192
- return !U() && typeof window < "u" && typeof window.Audio == "function" && typeof fetch == "function";
202
+ return !B() && typeof window < "u" && typeof window.Audio == "function" && typeof fetch == "function";
193
203
  },
194
204
  listVoices() {
195
- return U() ? [] : [...i];
205
+ return B() ? [] : [...i];
196
206
  },
197
207
  subscribeVoices(e) {
198
- return W.add(e), () => {
199
- W.delete(e);
208
+ return V.add(e), () => {
209
+ V.delete(e);
200
210
  };
201
211
  },
202
212
  speak(e, t, n = {}) {
203
- I?.cancel(), I = null;
213
+ N?.cancel(), N = null;
204
214
  let r = d(e);
205
215
  if (r.length === 0) {
206
216
  n.onEnd?.();
207
217
  return;
208
218
  }
209
- P += 1;
210
- let i = P;
211
- X(), L?.cancel(), R?.stop(), N();
219
+ j += 1;
220
+ let i = j;
221
+ q(), P?.cancel(), F?.stop(), A();
212
222
  let a = new AbortController();
213
- F = a;
223
+ M = a;
214
224
  let o = !1, s = () => {
215
225
  o || (o = !0, n.onStart?.());
216
226
  }, c = (e) => {
217
- let n = Z(e, t, a.signal);
227
+ let n = Q(e, t, a.signal);
218
228
  return n.catch(() => {}), n;
219
229
  }, f = t.audioOutput ? E(S) : null, h = t.audioOutput ? p(t.audioOutput, {
220
- play: (e) => Q(e, i, s),
221
- isCurrent: () => i === P,
230
+ play: (e) => $(e, i, s),
231
+ isCurrent: () => i === j,
222
232
  onChunkStart: (e) => {
223
233
  f?.progress(), n.onChunkStart?.(e);
224
234
  },
225
235
  onStart: s
226
236
  }) : null;
227
- L = h, R = f;
237
+ P = h, F = f;
228
238
  let g = (e) => f ? f.guard(e) : e, _ = () => {
229
- h && (f?.stop(), R === f && (R = null), L === h && (L = null));
239
+ h && (f?.stop(), F === f && (F = null), P === h && (P = null));
230
240
  };
231
241
  (async () => {
232
242
  try {
233
- let e = c(r[0]);
234
- for (let t = 0; t < r.length; t += 1) {
235
- if (i !== P) return;
236
- let a = await e;
237
- if (i !== P) return;
238
- let o = r[t + 1];
239
- o !== void 0 && (e = c(o)), h ? (f?.handed(a.byteLength), await g(h.write(a))) : (n.onChunkStart?.(t), await Q(a, i, s));
243
+ let e = r[0], o = Z(e, t, a.signal), l = o ? o.catch(() => c(e)) : c(e);
244
+ for (let e = 0; e < r.length; e += 1) {
245
+ if (i !== j) return;
246
+ let t = await l;
247
+ if (i !== j) return;
248
+ let a = r[e + 1];
249
+ a !== void 0 && (l = c(a)), h ? (f?.handed(t.byteLength), await g(h.write(t))) : (n.onChunkStart?.(e), await $(t, i, s));
240
250
  }
241
251
  if (h) {
242
252
  let e = await g(h.drain());
243
- if (i !== P) return;
253
+ if (i !== j) return;
244
254
  if (!e) throw Object.assign(/* @__PURE__ */ Error("audio output did not finish"), { reason: "unknown" });
245
255
  }
246
- i === P && (_(), n.onEnd?.());
256
+ i === j && (_(), n.onEnd?.());
247
257
  } catch (e) {
248
- if (a.abort(), i !== P) return;
258
+ if (a.abort(), i !== j) return;
249
259
  let t = null;
250
260
  if (h) {
251
261
  let n = e instanceof T ? null : await g(h.drain()).then(() => null, (e) => e);
252
- if (i !== P) return;
262
+ if (i !== j) return;
253
263
  h.cancel(), m(e) ? t = e : m(n) && (t = n);
254
264
  }
255
- if (P += 1, X(), _(), t) {
265
+ if (j += 1, q(), _(), t) {
256
266
  n.onError?.("output-not-cleared");
257
267
  return;
258
268
  }
259
269
  let r = e?.reason;
260
- u(r, e?.transient === !0) && J(l(r), r === "not-allowed" ? e.headers ?? null : null), n.onError?.(r === "not-allowed" || r === "unsupported" || r === "cancelled" ? r : "unknown");
270
+ u(r, e?.transient === !0) && G(l(r), r === "not-allowed" ? e.headers ?? null : null), n.onError?.(r === "not-allowed" || r === "unsupported" || r === "cancelled" ? r : "unknown");
261
271
  }
262
272
  })();
263
273
  },
264
274
  open(e, t = {}) {
265
- $.cancel();
266
- let n = ++P;
267
- N();
275
+ ie.cancel();
276
+ let n = ++j;
277
+ A();
268
278
  let r = e.audioOutput ? E(S) : null, i, a = -1, o = -1, s = -1, c = null, d = e.audioOutput ? p(e.audioOutput, {
269
- play: (e) => Q(e, n, () => i?.()),
270
- isCurrent: () => n === P,
279
+ play: (e) => $(e, n, () => i?.()),
280
+ isCurrent: () => n === j,
271
281
  onChunkStart: () => {},
272
282
  onStart: () => i?.()
273
283
  }) : null;
274
- L = d, R = r;
284
+ P = d, F = r;
275
285
  let h = (e) => r ? r.guard(e) : e, g = async () => {
276
286
  if (d && !await h(d.drain())) throw Error("audio output did not finish");
277
287
  }, _ = () => {
278
- r?.stop(), R === r && (R = null), L === d && (L = null), I === v && (I = null);
279
- }, v = f({
288
+ r?.stop(), F === r && (F = null), P === d && (P = null), N === y && (N = null);
289
+ }, v = !0, y = f({
280
290
  prepare: async (t, n) => {
281
291
  try {
282
- return await Z(t, e, n, !0);
292
+ if (v) {
293
+ v = !1;
294
+ let r = Z(t, e, n);
295
+ if (r) try {
296
+ return await r;
297
+ } catch (e) {
298
+ if (n.aborted) throw e;
299
+ }
300
+ }
301
+ return await Q(t, e, n, !0);
283
302
  } catch (e) {
284
303
  if (n.aborted) throw e;
285
304
  try {
@@ -297,20 +316,20 @@ function N(v) {
297
316
  let r = new Promise((e) => {
298
317
  c = e;
299
318
  });
300
- await Promise.race([Q(e, n, l), r]), c = null;
319
+ await Promise.race([$(e, n, l), r]), c = null;
301
320
  }
302
321
  },
303
322
  drain: g,
304
323
  retract: (e) => {
305
- d && o >= e ? d.retract(a) : !d && s >= e && (X(), c?.());
324
+ d && o >= e ? d.retract(a) : !d && s >= e && (q(), c?.());
306
325
  },
307
326
  cancel: () => {
308
- P += 1, X(), c?.(), d?.cancel(), _();
327
+ j += 1, q(), c?.(), d?.cancel(), _();
309
328
  },
310
329
  failed: (e) => {
311
330
  if (m(e)) return "output-not-cleared";
312
331
  let t = e, n = t?.reason;
313
- return u(n, t?.transient === !0) && J(l(n), n === "not-allowed" ? t?.headers ?? null : null), n === "not-allowed" || n === "unsupported" || n === "cancelled" || n === "rate-limited" ? n : "unknown";
332
+ return u(n, t?.transient === !0) && G(l(n), n === "not-allowed" ? t?.headers ?? null : null), n === "not-allowed" || n === "unsupported" || n === "cancelled" || n === "rate-limited" ? n : "unknown";
314
333
  }
315
334
  }, {
316
335
  ...t,
@@ -318,16 +337,31 @@ function N(v) {
318
337
  _(), t.onEnd?.();
319
338
  }
320
339
  });
321
- return I = v, v;
340
+ return N = y, y;
322
341
  },
323
342
  headersChanged() {
324
- x() >= z || V === null || U() || (z = 0, V = null, K(), G());
343
+ x() >= L || z === null || B() || (L = 0, z = null, U(), H());
344
+ },
345
+ prefetch(e, t) {
346
+ if (B() || e.trim() === "") return;
347
+ let n = Y(e, t);
348
+ if (I?.key === n) return;
349
+ X();
350
+ let r = new AbortController(), i = Q(e, t, r.signal);
351
+ i.catch(() => {}), I = {
352
+ key: n,
353
+ controller: r,
354
+ request: i
355
+ };
356
+ },
357
+ cancelPrefetch() {
358
+ X();
325
359
  },
326
360
  cancel() {
327
- let e = I;
328
- I = null, e?.cancel(), P += 1, F?.abort(), F = null, X(), L?.cancel(), L = null, R?.stop(), R = null;
361
+ let e = N;
362
+ N = null, e?.cancel(), j += 1, M?.abort(), M = null, q(), P?.cancel(), P = null, F?.stop(), F = null;
329
363
  }
330
364
  };
331
- return $;
365
+ return ie;
332
366
  }
333
- export { C as HOSTED_OUTPUT_MIN_AUDIO_BYTES_PER_SECOND, S as HOSTED_OUTPUT_STALL_AUDIO_RATIO, x as HOSTED_OUTPUT_STALL_BASE_MS, D as SILENT_WAV_DATA_URI, N as createHostedTtsEngine, w as hostedOutputStallMs, A as primeHostedPlayback, j as resetHostedPlaybackForTest, b as retryDelayMs };
367
+ export { C as HOSTED_OUTPUT_MIN_AUDIO_BYTES_PER_SECOND, S as HOSTED_OUTPUT_STALL_AUDIO_RATIO, x as HOSTED_OUTPUT_STALL_BASE_MS, ee as SILENT_WAV_DATA_URI, A as createHostedTtsEngine, w as hostedOutputStallMs, O as primeHostedPlayback, k as resetHostedPlaybackForTest, b as retryDelayMs };
@@ -48,6 +48,12 @@ function i({ device: i, hosted: a }) {
48
48
  t(), n();
49
49
  };
50
50
  },
51
+ prefetch(e, t) {
52
+ u(t) === a && a.prefetch?.(e, t);
53
+ },
54
+ cancelPrefetch() {
55
+ a.cancelPrefetch?.();
56
+ },
51
57
  speak(e, t, s) {
52
58
  if (o?.cancel(), o = null, i.cancel(), a.cancel(), u(t) !== a) {
53
59
  l(e, t, s);
@@ -49,6 +49,12 @@ export type SpeechFeedEvent = {
49
49
  } | {
50
50
  type: 'retract';
51
51
  messageId: string;
52
+ }
53
+ /** P1 (BOFF-12936): the answer so far, still being written; nothing is spoken. */
54
+ | {
55
+ type: 'prefetch';
56
+ messageId: string;
57
+ item: SpeechFeedItem;
52
58
  } | {
53
59
  type: 'audio';
54
60
  messageId: string;
@@ -63,6 +69,14 @@ export declare const speechFeed: {
63
69
  publish(messageId: string, item: SpeechFeedItem): void;
64
70
  append(messageId: string, item: SpeechFeedItem, phrases: readonly string[]): void;
65
71
  retract(messageId: string): void;
72
+ /**
73
+ * ★ P1 (BOFF-12936, decision D1): the answer as the person sees it so far,
74
+ * while it is still being written (voice turns of an opted-in app only). The
75
+ * subscriber may fetch the clip the finished answer will start with; nothing
76
+ * is spoken, so the "never speak what is not on screen" rule is untouched.
77
+ * Not remembered: `peek` stays about published answers.
78
+ */
79
+ prefetch(messageId: string, item: SpeechFeedItem): void;
66
80
  audio(messageId: string, state: "queued" | "started" | "paused" | "ended" | "retrying"): void;
67
81
  /** Forget a published message and tell the subscriber to stop speaking it. */
68
82
  abort(messageId: string, retrying?: boolean): void;
@@ -34,6 +34,13 @@ var i = {
34
34
  messageId: e
35
35
  });
36
36
  },
37
+ prefetch(e, t) {
38
+ n({
39
+ type: "prefetch",
40
+ messageId: e,
41
+ item: t
42
+ });
43
+ },
37
44
  audio(e, t) {
38
45
  n({
39
46
  type: "audio",
@@ -233,6 +233,16 @@ export interface SpeechEngine {
233
233
  * and only then.
234
234
  */
235
235
  wouldSpeakOnAvatar?(options: SpeechSpeakOptions): boolean;
236
+ /**
237
+ * ★ P1 (BOFF-12936, decision D1): fetch a reply's first piece before the
238
+ * reply is released, so it can start the moment it is. Plays nothing; the
239
+ * clip is used only by the next reply whose first piece is exactly this
240
+ * request. Optional: engines that synthesise nothing (the device voice)
241
+ * omit it, and callers read that as "nothing to prefetch".
242
+ */
243
+ prefetch?(text: string, options: SpeechSpeakOptions): void;
244
+ /** Drop a prefetched piece that will not be used (the reply was stopped or replaced). */
245
+ cancelPrefetch?(): void;
236
246
  }
237
247
  /** One reply. Appends never pre-empt the piece currently playing. */
238
248
  export interface SpeechQueue {