realtime-avatar 0.38.0 → 0.39.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/README.md CHANGED
@@ -259,6 +259,24 @@ and fetch, including a pending `next()`. The smaller of `timeoutMs` and `totalTi
259
259
  covers the entire request/body, including pauses between iterations. **Never automatically
260
260
  retry** an ambiguous paid request. Cancellation does not prove that billing stopped.
261
261
 
262
+ ### Live speech session
263
+
264
+ `openSpeechSession(voiceId, { maxTextBytes, latency?, chunkLength?, language? }, { signal? })`
265
+ opens `GET /voices/{voiceId}/speech/live` as a WebSocket and resolves once the platform admitted
266
+ the session, so every refusal rejects the promise. Send `text(utterance, text)` (utterance 0 first,
267
+ at most `chunkLength` characters each), `flush(utterance)` to speak it and open the next, `end()`
268
+ and `cancel()`. Iterate it once for `started`, per-utterance `audio` (`sampleOffset` from the
269
+ utterance's start), `alignment` (replaces that utterance's snapshot, may arrive late) and
270
+ `utterance-end`, then one `completed` or `failed`. Frames are validated by the generated
271
+ `speechLiveClientMessageSchema` and `speechLiveServerMessageSchema` in both directions;
272
+ non-contiguous audio or a skipped utterance fails the session.
273
+
274
+ Billing is for the UTF-8 bytes sent: cancel, disconnect, `break` and abort still bill them, and a
275
+ provider failure refunds. Never retried. In Workers the upgrade is a `fetch` with the
276
+ `Authorization` header and a refusal is a `RealtimeAvatarHttpError`; in Node 22 it is the global
277
+ `WebSocket`, which reports no status, so a refusal is a `RealtimeAvatarError`. `timeoutMs` bounds
278
+ opening and `totalTimeoutMs`, if set, the whole session.
279
+
262
280
  ## Custom voice (webhook)
263
281
 
264
282
  Speak a call in your own voice. Each sentence she is about to say is POSTed, signed, to an
@@ -482,6 +500,8 @@ rta.getConnectionHistory(sessionId) // bounded LiveKit observations; requires
482
500
  rta.createSpeech(voiceId, { text, format?, speed?, language? }, { signal? })
483
501
  // unreleased streaming candidate; generated SSE events, never retried
484
502
  rta.createSpeechStream(voiceId, { text, speed?, language? }, { signal? })
503
+ // live session: text in while it is written, PCM + alignment per utterance; WebSocket, never retried
504
+ rta.openSpeechSession(voiceId, { maxTextBytes, latency?, chunkLength?, language? }, { signal? })
485
505
 
486
506
  // pre-rendered video (MP4, no live call); server only, requires renders:write / renders:read
487
507
  rta.createRender({ avatarId, voiceId? | voice?, lipGain?, shots }) // 202 queued; one Idempotency-Key across retries
@@ -1,4 +1,4 @@
1
- import { RealtimeAvatar, ROUTE_TIMEOUT_MS, proxyEndRequestSchema, proxyConnectRequestSchema, isQueued, RealtimeAvatarHttpError, RealtimeAvatarError } from './chunk-7NZ7U5HN.js';
1
+ import { RealtimeAvatar, ROUTE_TIMEOUT_MS, proxyEndRequestSchema, proxyConnectRequestSchema, isQueued, RealtimeAvatarHttpError, RealtimeAvatarError } from './chunk-YBKE7SDI.js';
2
2
 
3
3
  // ../http-client/src/generated/error-semantics.ts
4
4
  var ERROR_SEMANTICS = {
@@ -942,12 +942,74 @@ var schema51 = z.strictObject({
942
942
  "error": schema50
943
943
  });
944
944
  var schema522 = z.union([schema202, schema252, schema292, schema332, schema51]);
945
+ var schema532 = z.int().min(100).max(300);
946
+ var schema542 = z.int().min(1).max(6e3);
947
+ var schema552 = z.literal("text");
948
+ var schema562 = z.int().min(0).max(127);
949
+ var schema57 = z.string().min(1).max(300);
950
+ var schema58 = z.strictObject({
951
+ "type": schema552,
952
+ "utterance": schema562,
953
+ "text": schema57
954
+ });
955
+ var schema59 = z.literal("flush");
956
+ var schema60 = z.strictObject({
957
+ "type": schema59,
958
+ "utterance": schema272
959
+ });
960
+ var schema61 = z.literal("end");
961
+ var schema622 = z.strictObject({
962
+ "type": schema61
963
+ });
964
+ var schema632 = z.literal("cancel");
965
+ var schema642 = z.strictObject({
966
+ "type": schema632
967
+ });
968
+ var schema65 = z.union([schema58, schema60, schema622, schema642]);
969
+ var schema66 = z.int().min(0).max(432e4);
970
+ var schema67 = z.strictObject({
971
+ "requestId": schema154,
972
+ "type": schema213,
973
+ "utterance": schema272,
974
+ "sampleOffset": schema66,
975
+ "audioBase64": schema242
976
+ });
977
+ var schema68 = z.array(speechAlignedTextSchema).max(2048);
978
+ var schema69 = z.strictObject({
979
+ "requestId": schema154,
980
+ "type": schema262,
981
+ "utterance": schema272,
982
+ "segments": schema68
983
+ });
984
+ var schema70 = z.literal("utterance-end");
985
+ var schema71 = z.strictObject({
986
+ "requestId": schema154,
987
+ "type": schema70,
988
+ "utterance": schema272,
989
+ "samples": schema31
990
+ });
991
+ var schema722 = z.int().min(0).max(6e3);
992
+ var schema732 = z.strictObject({
993
+ "requestId": schema154,
994
+ "type": schema30,
995
+ "billedBytes": schema722,
996
+ "billedCredits": schema64
997
+ });
998
+ var schema742 = z.union([schema202, schema67, schema69, schema71, schema732, schema51]);
945
999
  var createSpeechRequestSchema = schema47.extend({
946
1000
  format: schema47.shape.format.unwrap().default("mp3")
947
1001
  });
948
1002
  var createSpeechStreamRequestSchema = schema104;
949
1003
  var speechStreamEventSchema = schema522;
950
1004
  var SPEECH_STREAM_CONTRACT = { "maxAudioEventBytes": 16384, "maxSamples": 432e4};
1005
+ var speechLiveOpenSchema = z.strictObject({
1006
+ "language": schema37.optional(),
1007
+ "latency": schema84.optional(),
1008
+ "chunkLength": schema532.optional(),
1009
+ "maxTextBytes": schema542
1010
+ });
1011
+ var speechLiveClientMessageSchema = schema65;
1012
+ var speechLiveServerMessageSchema = schema742;
951
1013
  var speechVoiceIdSchema = schema56;
952
1014
  var speechContentTypeSchema = z.enum(["audio/mpeg", "audio/wav"]);
953
1015
  var speechResponseHeadersSchema = z.strictObject({
@@ -1001,13 +1063,13 @@ var schema119 = z.custom((value) => typeof value === "object" && value !== null
1001
1063
  var schema214 = z.enum(["cartesia", "fish"]);
1002
1064
  var schema310 = z.string().min(1).max(240);
1003
1065
  var schema410 = z.string().min(1).max(120);
1004
- var schema57 = z.number().min(0.5).max(2);
1005
- var schema65 = z.null();
1006
- var schema75 = z.union([schema57, schema65]);
1066
+ var schema510 = z.number().min(0.5).max(2);
1067
+ var schema610 = z.null();
1068
+ var schema75 = z.union([schema510, schema610]);
1007
1069
  var schema85 = z.string().min(1).max(80);
1008
- var schema95 = z.union([schema85, schema65]);
1070
+ var schema95 = z.union([schema85, schema610]);
1009
1071
  var schema105 = z.string().min(2).max(16);
1010
- var schema1110 = z.union([schema105, schema65]);
1072
+ var schema1110 = z.union([schema105, schema610]);
1011
1073
  var schema125 = z.strictObject({
1012
1074
  "provider": schema214,
1013
1075
  "voice_id": schema310,
@@ -1021,18 +1083,18 @@ var schema144 = z.string();
1021
1083
  var schema155 = z.enum(["video", "anima"]);
1022
1084
  var schema164 = z.enum(["image", "video"]);
1023
1085
  var schema174 = z.enum(["realtime-avatar-live-v1", "realtime-avatar-video-v1"]);
1024
- var schema184 = z.union([schema135, schema65]);
1086
+ var schema184 = z.union([schema135, schema610]);
1025
1087
  var schema194 = z.enum(["draft", "preprocessing", "ready", "failed", "disabled", "deleted"]);
1026
- var schema203 = z.union([schema144, schema65]);
1088
+ var schema203 = z.union([schema144, schema610]);
1027
1089
  var schema215 = z.enum(["none", "queued", "generating", "ready", "failed"]);
1028
1090
  var schema223 = z.enum(["local", "gemini", "openai"]);
1029
1091
  var schema233 = z.string().max(200);
1030
- var schema243 = z.union([schema233, schema65]);
1092
+ var schema243 = z.union([schema233, schema610]);
1031
1093
  var schema253 = z.strictObject({
1032
1094
  "provider": schema223,
1033
1095
  "model": schema243.optional()
1034
1096
  });
1035
- var schema263 = z.union([schema253, schema65]);
1097
+ var schema263 = z.union([schema253, schema610]);
1036
1098
  var schema273 = z.strictObject({
1037
1099
  "id": schema135,
1038
1100
  "tenantId": schema135,
@@ -1379,19 +1441,243 @@ async function* parseEvents(response, signal) {
1379
1441
  throw new RealtimeAvatarError(MISMATCH);
1380
1442
  }
1381
1443
  }
1444
+
1445
+ // ../http-client/src/speech-session.ts
1446
+ var MISMATCH2 = "Speech session message did not match the SDK contract";
1447
+ var CLOSED = "Speech session is closed";
1448
+ var UNOPENED = "Speech session could not be opened";
1449
+ function isUpgradedSocket(value) {
1450
+ return typeof value === "object" && value !== null && "accept" in value && typeof value.accept === "function" && "send" in value && typeof value.send === "function" && "close" in value && typeof value.close === "function" && "addEventListener" in value && typeof value.addEventListener === "function";
1451
+ }
1452
+ function connectStandardSocket(url, headers) {
1453
+ if (typeof WebSocket === "undefined") {
1454
+ throw new RealtimeAvatarError("openSpeechSession needs a global WebSocket (Node 22 or later) or Cloudflare Workers");
1455
+ }
1456
+ const target = new URL(url);
1457
+ target.protocol = target.protocol === "http:" ? "ws:" : "wss:";
1458
+ const socket = Reflect.construct(WebSocket, [target.href, { headers }]);
1459
+ if (!(socket instanceof WebSocket)) throw new RealtimeAvatarError(UNOPENED);
1460
+ const ready = new Promise((resolve, reject) => {
1461
+ socket.addEventListener("open", () => resolve(), { once: true });
1462
+ socket.addEventListener("error", () => reject(new RealtimeAvatarError(UNOPENED)), { once: true });
1463
+ socket.addEventListener("close", () => reject(new RealtimeAvatarError(UNOPENED)), { once: true });
1464
+ });
1465
+ return { socket, ready };
1466
+ }
1467
+ async function openSpeechSocketSession(connect, timing, callerSignal) {
1468
+ callerSignal?.throwIfAborted();
1469
+ const handshake = new AbortController();
1470
+ const timer = setTimeout(() => handshake.abort(new DOMException("The operation timed out.", "TimeoutError")), timing.handshakeMs);
1471
+ const forward = () => handshake.abort(callerSignal?.reason);
1472
+ callerSignal?.addEventListener("abort", forward, { once: true });
1473
+ let connection;
1474
+ try {
1475
+ connection = await connect(handshake.signal);
1476
+ const session = new LiveSession(connection.socket);
1477
+ connection.socket.accept?.();
1478
+ const opened = connection.ready;
1479
+ opened.catch(() => {
1480
+ });
1481
+ await new Promise((resolve, reject) => {
1482
+ const abort = () => reject(handshake.signal.reason);
1483
+ if (handshake.signal.aborted) {
1484
+ abort();
1485
+ return;
1486
+ }
1487
+ handshake.signal.addEventListener("abort", abort, { once: true });
1488
+ opened.then(resolve, reject).finally(() => handshake.signal.removeEventListener("abort", abort));
1489
+ });
1490
+ session.start(callerSignal, timing.sessionMs);
1491
+ return session;
1492
+ } catch (error) {
1493
+ if (connection) closeQuietly(connection.socket);
1494
+ callerSignal?.throwIfAborted();
1495
+ throw error;
1496
+ } finally {
1497
+ clearTimeout(timer);
1498
+ callerSignal?.removeEventListener("abort", forward);
1499
+ }
1500
+ }
1501
+ function closeQuietly(socket) {
1502
+ try {
1503
+ socket.close(1e3);
1504
+ } catch {
1505
+ }
1506
+ }
1507
+ function messageText(event) {
1508
+ if (typeof event !== "object" || event === null || !("data" in event)) return void 0;
1509
+ return typeof event.data === "string" ? event.data : void 0;
1510
+ }
1511
+ var LiveSession = class {
1512
+ #socket;
1513
+ #queue = [];
1514
+ #waiting;
1515
+ #failure;
1516
+ #over = false;
1517
+ #iterated = false;
1518
+ #stop;
1519
+ // What has been received, checked against the order the platform promises.
1520
+ #received = 0;
1521
+ #requestId;
1522
+ #terminal = false;
1523
+ #ended = 0;
1524
+ #open;
1525
+ #samples = 0;
1526
+ constructor(socket) {
1527
+ this.#socket = socket;
1528
+ socket.addEventListener("message", (event) => this.#receive(event));
1529
+ socket.addEventListener("close", () => this.#closed());
1530
+ socket.addEventListener("error", () => this.#closed());
1531
+ }
1532
+ start(signal, sessionMs) {
1533
+ const stops = [];
1534
+ if (signal) {
1535
+ const abort = () => this.#fail(signal.reason);
1536
+ signal.addEventListener("abort", abort, { once: true });
1537
+ stops.push(() => signal.removeEventListener("abort", abort));
1538
+ }
1539
+ if (sessionMs !== void 0) {
1540
+ const timer = setTimeout(() => this.#fail(new DOMException("The operation timed out.", "TimeoutError")), sessionMs);
1541
+ stops.push(() => clearTimeout(timer));
1542
+ }
1543
+ this.#stop = () => {
1544
+ for (const stop of stops) stop();
1545
+ };
1546
+ }
1547
+ text(utterance, text) {
1548
+ this.#send({ type: "text", utterance, text });
1549
+ }
1550
+ flush(utterance) {
1551
+ this.#send({ type: "flush", utterance });
1552
+ }
1553
+ end() {
1554
+ if (!this.#over) this.#send({ type: "end" });
1555
+ }
1556
+ cancel() {
1557
+ if (!this.#over) this.#send({ type: "cancel" });
1558
+ }
1559
+ [Symbol.asyncIterator]() {
1560
+ if (this.#iterated) throw new RealtimeAvatarError("A speech session can be iterated only once");
1561
+ this.#iterated = true;
1562
+ return {
1563
+ next: () => this.#next(),
1564
+ return: async () => {
1565
+ if (!this.#over) {
1566
+ this.cancel();
1567
+ this.#finish();
1568
+ }
1569
+ this.#queue.length = 0;
1570
+ return { value: void 0, done: true };
1571
+ }
1572
+ };
1573
+ }
1574
+ #send(message) {
1575
+ const parsed = speechLiveClientMessageSchema.parse(message);
1576
+ if (this.#over) throw new RealtimeAvatarError(CLOSED);
1577
+ this.#socket.send(JSON.stringify(parsed));
1578
+ }
1579
+ #next() {
1580
+ const message = this.#queue.shift();
1581
+ if (message) return Promise.resolve({ value: message, done: false });
1582
+ if (this.#failure) return Promise.reject(this.#failure.error);
1583
+ if (this.#over) return Promise.resolve({ value: void 0, done: true });
1584
+ return new Promise((resolve, reject) => {
1585
+ this.#waiting = { resolve, reject };
1586
+ });
1587
+ }
1588
+ #deliver(message) {
1589
+ const waiting = this.#waiting;
1590
+ this.#waiting = void 0;
1591
+ if (waiting) waiting.resolve({ value: message, done: false });
1592
+ else this.#queue.push(message);
1593
+ }
1594
+ /** Ends the session: no more messages are accepted and the socket is closed. */
1595
+ #finish() {
1596
+ if (this.#over) return;
1597
+ this.#over = true;
1598
+ this.#stop?.();
1599
+ closeQuietly(this.#socket);
1600
+ const waiting = this.#waiting;
1601
+ this.#waiting = void 0;
1602
+ if (!waiting) return;
1603
+ if (this.#failure) waiting.reject(this.#failure.error);
1604
+ else waiting.resolve({ value: void 0, done: true });
1605
+ }
1606
+ #fail(error) {
1607
+ if (this.#over) return;
1608
+ this.#failure = { error };
1609
+ this.#queue.length = 0;
1610
+ this.#finish();
1611
+ }
1612
+ #closed() {
1613
+ if (!this.#terminal) this.#fail(new RealtimeAvatarError("Speech session closed before it completed"));
1614
+ this.#finish();
1615
+ }
1616
+ #receive(event) {
1617
+ if (this.#over) return;
1618
+ try {
1619
+ const data = messageText(event);
1620
+ if (data === void 0 || this.#terminal || data.length > MAX_EVENT_BYTES) throw new RealtimeAvatarError(MISMATCH2);
1621
+ this.#received += data.length;
1622
+ if (this.#received > MAX_WIRE_BYTES) throw new RealtimeAvatarError(MISMATCH2);
1623
+ const raw = JSON.parse(data);
1624
+ const message = speechLiveServerMessageSchema.parse(raw);
1625
+ this.#check(message);
1626
+ this.#deliver(message);
1627
+ if (this.#terminal) this.#finish();
1628
+ } catch {
1629
+ this.#fail(new RealtimeAvatarError(MISMATCH2));
1630
+ }
1631
+ }
1632
+ #check(message) {
1633
+ if (this.#requestId === void 0) {
1634
+ if (message.type !== "started") throw new RealtimeAvatarError(MISMATCH2);
1635
+ this.#requestId = message.requestId;
1636
+ return;
1637
+ }
1638
+ if (message.type === "started" || message.requestId !== this.#requestId) throw new RealtimeAvatarError(MISMATCH2);
1639
+ switch (message.type) {
1640
+ case "audio": {
1641
+ const bytes = atob(message.audioBase64).length;
1642
+ if (bytes === 0 || bytes % 2 !== 0) throw new RealtimeAvatarError(MISMATCH2);
1643
+ if (this.#open === void 0) {
1644
+ if (message.utterance !== this.#ended || message.sampleOffset !== 0) throw new RealtimeAvatarError(MISMATCH2);
1645
+ this.#open = message.utterance;
1646
+ this.#samples = 0;
1647
+ } else if (message.utterance !== this.#open || message.sampleOffset !== this.#samples) {
1648
+ throw new RealtimeAvatarError(MISMATCH2);
1649
+ }
1650
+ this.#samples += bytes / 2;
1651
+ return;
1652
+ }
1653
+ case "utterance-end":
1654
+ if (message.utterance !== this.#open || message.samples !== this.#samples) throw new RealtimeAvatarError(MISMATCH2);
1655
+ this.#open = void 0;
1656
+ this.#ended++;
1657
+ return;
1658
+ case "alignment":
1659
+ if (message.utterance > this.#ended) throw new RealtimeAvatarError(MISMATCH2);
1660
+ return;
1661
+ case "completed":
1662
+ case "failed":
1663
+ this.#terminal = true;
1664
+ return;
1665
+ }
1666
+ }
1667
+ };
1382
1668
  var schema010 = z.url().regex(new RegExp("^https:\\/\\/")).max(500);
1383
1669
  var schema120 = z.string().min(16).max(200);
1384
1670
  var schema216 = z.string().min(1).max(200);
1385
1671
  var schema311 = z.string().min(2).max(35);
1386
1672
  var schema411 = z.literal([8e3, 16e3, 22050, 24e3, 44100, 48e3]);
1387
- var schema58 = z.int().min(1e3).max(15e3);
1388
- var schema66 = z.strictObject({
1673
+ var schema511 = z.int().min(1e3).max(15e3);
1674
+ var schema611 = z.strictObject({
1389
1675
  "url": schema010,
1390
1676
  "secret": schema120,
1391
1677
  "voice_id": schema216.optional(),
1392
1678
  "language": schema311.optional(),
1393
1679
  "sample_rate": schema411.optional(),
1394
- "timeout_ms": schema58.optional()
1680
+ "timeout_ms": schema511.optional()
1395
1681
  });
1396
1682
  var schema76 = z.literal("voice.synthesize");
1397
1683
  var schema86 = z.literal(1);
@@ -1431,7 +1717,7 @@ var schema264 = z.object({
1431
1717
  "output": schema224,
1432
1718
  "client_metadata": schema254
1433
1719
  });
1434
- var voiceWebhookWireSchema = schema66;
1720
+ var voiceWebhookWireSchema = schema611;
1435
1721
  var voiceWebhookOptionsSchema = z.strictObject({
1436
1722
  url: voiceWebhookWireSchema.shape.url,
1437
1723
  secret: voiceWebhookWireSchema.shape.secret,
@@ -1459,8 +1745,8 @@ var schema312 = z.literal("external");
1459
1745
  var schema412 = z.strictObject({
1460
1746
  "renderer": schema312
1461
1747
  });
1462
- var schema59 = z.literal("v1");
1463
- var schema67 = z.literal("correlated_pcm_v1");
1748
+ var schema512 = z.literal("v1");
1749
+ var schema612 = z.literal("correlated_pcm_v1");
1464
1750
  var schema77 = z.literal("speech_segments_v1");
1465
1751
  var schema87 = z.literal("v1");
1466
1752
  var schema97 = z.literal(true);
@@ -1479,8 +1765,8 @@ var schema137 = z.strictObject({
1479
1765
  "token": schema121,
1480
1766
  "fail_closed": schema218.optional(),
1481
1767
  "visible_character": schema412.optional(),
1482
- "performance": schema59.optional(),
1483
- "audio_output": schema67.optional(),
1768
+ "performance": schema512.optional(),
1769
+ "audio_output": schema612.optional(),
1484
1770
  "text_output": schema77.optional()
1485
1771
  }).superRefine((value, ctx) => {
1486
1772
  if (value["audio_output"] !== void 0) {
@@ -1520,14 +1806,14 @@ var schema128 = z.enum(["eou", "llm", "tts", "decision", "hold", "e2e"]);
1520
1806
  var schema219 = z.literal("interrupt_stop");
1521
1807
  var schema313 = z.union([schema128, schema219]);
1522
1808
  var schema413 = z.int().max(9007199254740991).gt(0);
1523
- var schema510 = z.number().min(0);
1524
- var schema68 = z.strictObject({
1809
+ var schema513 = z.number().min(0);
1810
+ var schema613 = z.strictObject({
1525
1811
  "count": schema413,
1526
- "max": schema510,
1527
- "p50": schema510,
1528
- "p90": schema510
1812
+ "max": schema513,
1813
+ "p50": schema513,
1814
+ "p90": schema513
1529
1815
  });
1530
- var schema78 = z.partialRecord(schema313, schema68);
1816
+ var schema78 = z.partialRecord(schema313, schema613);
1531
1817
  var schema88 = z.literal("available");
1532
1818
  var schema98 = z.literal("dialogue");
1533
1819
  var schema108 = z.string().regex(new RegExp("^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))T(?:(?:[01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d(?:\\.\\d+)?(?:Z|([+-](?:[01]\\d|2[0-3]):[0-5]\\d))|(?:[01]\\d|2[0-3]):[0-5]\\d(?::[0-5]\\d(?:\\.\\d+)?)?)$"));
@@ -1579,7 +1865,7 @@ var schema333 = z.strictObject({
1579
1865
  var schema343 = z.string();
1580
1866
  var schema353 = z.union([schema343, schema1113]);
1581
1867
  var schema363 = z.enum(["reserved", "started", "released", "failed"]);
1582
- var schema373 = z.union([schema510, schema1113]);
1868
+ var schema373 = z.union([schema513, schema1113]);
1583
1869
  var schema382 = z.custom((value) => typeof value === "object" && value !== null && !Array.isArray(value));
1584
1870
  var schema392 = z.strictObject({
1585
1871
  "sessionId": schema012,
@@ -1614,7 +1900,7 @@ var DEFAULT_BASE_URL = "https://realtimeavatar.ai/api/v1";
1614
1900
  var RENDER_MISMATCH = "Render response did not match the SDK contract";
1615
1901
  var SPEECH_MISMATCH = "Speech response did not match the SDK contract";
1616
1902
  var MAX_SPEECH_RESPONSE_BYTES = 24 * 1024 * 1024;
1617
- var SDK_VERSION = "0.38.0";
1903
+ var SDK_VERSION = "0.39.0";
1618
1904
  var RealtimeAvatar = class {
1619
1905
  #apiKey;
1620
1906
  #baseUrl;
@@ -2236,12 +2522,43 @@ var RealtimeAvatar = class {
2236
2522
  return response;
2237
2523
  }, Math.max(0, Math.min(this.#timeoutMs, this.#totalTimeoutMs ?? Infinity)), options.signal);
2238
2524
  }
2525
+ /**
2526
+ * A live speech session: text in while it is written, PCM and alignment out per utterance.
2527
+ * Resolves once the platform admitted the session (voice, slot, credit hold, provider socket),
2528
+ * so every refusal before that rejects here. In Workers the upgrade is a fetch and a refusal is
2529
+ * a RealtimeAvatarHttpError; Node's standard WebSocket reports no status, so there it is a
2530
+ * RealtimeAvatarError. Never retried. `timeoutMs` bounds opening, `totalTimeoutMs` (if set) the
2531
+ * whole session; abort or `break` closes it, which bills what was already sent.
2532
+ */
2533
+ async openSpeechSession(voiceId, input, options = {}) {
2534
+ const id = speechVoiceIdSchema.parse(voiceId);
2535
+ const open = speechLiveOpenSchema.parse(input);
2536
+ const query = new URLSearchParams();
2537
+ for (const [name, value] of Object.entries(open)) if (value !== void 0) query.set(name, String(value));
2538
+ const url = `${this.#baseUrl}/voices/${encodeURIComponent(id)}/speech/live?${query}`;
2539
+ const headers = this.#identityHeaders();
2540
+ return openSpeechSocketSession(
2541
+ isWorkerd() ? (signal) => this.#upgrade(url, headers, signal) : async () => connectStandardSocket(url, headers),
2542
+ { handshakeMs: Math.max(0, Math.min(this.#timeoutMs, this.#totalTimeoutMs ?? Infinity)), sessionMs: this.#totalTimeoutMs },
2543
+ options.signal
2544
+ );
2545
+ }
2546
+ /** Workers open an outbound WebSocket with a fetch upgrade; it is the only way to send headers there. */
2547
+ async #upgrade(url, headers, signal) {
2548
+ const response = await this.#fetch(url, { headers: { ...headers, upgrade: "websocket" }, signal });
2549
+ const socket = "webSocket" in response ? response.webSocket : null;
2550
+ if (response.status === 101 && isUpgradedSocket(socket)) return { socket, ready: Promise.resolve() };
2551
+ if (!response.ok) await this.#json(await boundedSpeechErrorResponse(response, signal));
2552
+ await response.body?.cancel().catch(() => {
2553
+ });
2554
+ throw new RealtimeAvatarError("Speech session upgrade did not match the SDK contract");
2555
+ }
2239
2556
  // ── internals ────────────────────────────────────────────────────────────
2557
+ #identityHeaders() {
2558
+ return { authorization: `Bearer ${this.#apiKey}`, "user-agent": this.#userAgent };
2559
+ }
2240
2560
  async #request(method, path, init = {}) {
2241
- const headers = {
2242
- authorization: `Bearer ${this.#apiKey}`,
2243
- "user-agent": this.#userAgent
2244
- };
2561
+ const headers = this.#identityHeaders();
2245
2562
  if (init.headers?.["X-RTA-Speech-Diagnostics"] === "1") {
2246
2563
  headers["X-RTA-Speech-Diagnostics"] = "1";
2247
2564
  }
@@ -2407,11 +2724,15 @@ function toAsset(raw) {
2407
2724
  sizeBytes: typeof a.sizeBytes === "number" ? a.sizeBytes : null
2408
2725
  };
2409
2726
  }
2727
+ function isWorkerd() {
2728
+ const g = globalThis;
2729
+ return "navigator" in g && typeof navigator?.userAgent === "string" && navigator.userAgent.includes("Cloudflare-Workers");
2730
+ }
2410
2731
  function runtimeTag() {
2411
2732
  const g = globalThis;
2412
2733
  if ("Deno" in g) return "deno";
2413
2734
  if ("Bun" in g) return "bun";
2414
- if ("navigator" in g && typeof navigator?.userAgent === "string" && navigator.userAgent.includes("Cloudflare-Workers")) return "workerd";
2735
+ if (isWorkerd()) return "workerd";
2415
2736
  if ("process" in g && typeof process?.versions?.node === "string") return `node/${process.versions.node}`;
2416
2737
  return "unknown";
2417
2738
  }
@@ -2489,4 +2810,4 @@ var proxyEndRequestSchema = z.object({
2489
2810
  message: "session_id or queue_ticket_id is required"
2490
2811
  });
2491
2812
 
2492
- export { DEFAULT_FISH_TTS_MODEL, FISH_DELIVERY_MODELS, ROUTE_TIMEOUT_MS, RealtimeAvatar, RealtimeAvatarError, RealtimeAvatarHttpError, RealtimeAvatarWebhookError, capacityBusyResponseSchema, clipLibraryDeclarationSchema, createSpeechRequestSchema, createSpeechStreamRequestSchema, externalSpeechSegmentSchema, externalTurnContextSchema, externalUtteranceSchema, fishTtsModelSchema, isQueued, listRecordingsResponseSchema, listRendersResponseSchema, liveKitSessionGrantSchema, proxyConnectRequestSchema, proxyEndRequestSchema, recordingAccessResponseSchema, recordingArtifactSchema, recordingModeSchema, renderAccessResponseSchema, renderSchema, speechAlignedTextSchema, speechStreamEventSchema, verifyTranscript, verifyVoiceWebhook };
2813
+ export { DEFAULT_FISH_TTS_MODEL, FISH_DELIVERY_MODELS, ROUTE_TIMEOUT_MS, RealtimeAvatar, RealtimeAvatarError, RealtimeAvatarHttpError, RealtimeAvatarWebhookError, capacityBusyResponseSchema, clipLibraryDeclarationSchema, createSpeechRequestSchema, createSpeechStreamRequestSchema, externalSpeechSegmentSchema, externalTurnContextSchema, externalUtteranceSchema, fishTtsModelSchema, isQueued, listRecordingsResponseSchema, listRendersResponseSchema, liveKitSessionGrantSchema, proxyConnectRequestSchema, proxyEndRequestSchema, recordingAccessResponseSchema, recordingArtifactSchema, recordingModeSchema, renderAccessResponseSchema, renderSchema, speechAlignedTextSchema, speechLiveClientMessageSchema, speechLiveOpenSchema, speechLiveServerMessageSchema, speechStreamEventSchema, verifyTranscript, verifyVoiceWebhook };
package/dist/express.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { P as ProxyConfig } from './types-CYti2_bv.js';
2
- import './types-niYtoW5A.js';
1
+ import { P as ProxyConfig } from './types-CpfJZaxo.js';
2
+ import './types-B8MMMQ_V.js';
3
3
  import 'zod';
4
4
 
5
5
  type Expressish = {
package/dist/express.js CHANGED
@@ -1,5 +1,5 @@
1
- import { createProxyHandler } from './chunk-T4WKWENM.js';
2
- import './chunk-7NZ7U5HN.js';
1
+ import { createProxyHandler } from './chunk-PZNCGKOB.js';
2
+ import './chunk-YBKE7SDI.js';
3
3
  import './chunk-MCIXU6XP.js';
4
4
 
5
5
  // ../proxy/src/express.ts
package/dist/hono.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { P as ProxyConfig } from './types-CYti2_bv.js';
2
- import './types-niYtoW5A.js';
1
+ import { P as ProxyConfig } from './types-CpfJZaxo.js';
2
+ import './types-B8MMMQ_V.js';
3
3
  import 'zod';
4
4
 
5
5
  /**
package/dist/hono.js CHANGED
@@ -1,5 +1,5 @@
1
- import { createProxyHandler } from './chunk-T4WKWENM.js';
2
- import './chunk-7NZ7U5HN.js';
1
+ import { createProxyHandler } from './chunk-PZNCGKOB.js';
2
+ import './chunk-YBKE7SDI.js';
3
3
  import './chunk-MCIXU6XP.js';
4
4
 
5
5
  // ../proxy/src/hono.ts
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { a as CallPolicy, C as CallMode, S as StartCallResult, E as EndCallOptions, A as Avatar, b as CreateAnimaAvatarInput, R as RegisteredAvatar, c as AvatarUpdate, d as AvatarSourceSwap, e as ClipLibraryDeclaration, f as ClipLibraryUpdate, L as LoopRedirect, g as ClipLibrary, h as AssetKind, i as Asset, j as ListSessionsOptions, U as UsageSessionPage, k as UsageSession, l as ListRecordingsQuery, m as ListRecordingsResponse, n as RecordingArtifact, o as RecordingAccessResponse, p as CreateRenderInput, q as CreditBalance, r as operations, s as CreateSpeechInput, t as SpeechResult, u as CreateSpeechStreamRequest, v as SpeechStreamEvent, T as TranscriptPayload, V as VoiceWebhookRequest } from './types-niYtoW5A.js';
2
- export { w as AvatarClip, x as CallConnection, y as CallQueued, z as ClipDeclaration, B as ClipLibraryPlan, D as ClipSource, F as ContextMessage, G as EndCallReason, H as ExternalLlm, I as RecordingArtifactStatus, J as RecordingMode, K as RenderShot, M as SpeechAlignedText, N as VideoPolicy, O as VoiceWebhook, P as VoiceWebhookErrorCode, Q as createSpeechRequestSchema, W as createSpeechStreamRequestSchema, X as isQueued, Y as listRecordingsResponseSchema, Z as recordingAccessResponseSchema, _ as recordingArtifactSchema, $ as recordingModeSchema, a0 as speechAlignedTextSchema, a1 as speechStreamEventSchema } from './types-niYtoW5A.js';
1
+ import { S as SpeechLiveServerMessage, b as SpeechLiveClientMessage, a as CallPolicy, C as CallMode, c as StartCallResult, E as EndCallOptions, A as Avatar, d as CreateAnimaAvatarInput, R as RegisteredAvatar, e as AvatarUpdate, f as AvatarSourceSwap, g as ClipLibraryDeclaration, h as ClipLibraryUpdate, L as LoopRedirect, i as ClipLibrary, j as AssetKind, k as Asset, l as ListSessionsOptions, U as UsageSessionPage, m as UsageSession, n as ListRecordingsQuery, o as ListRecordingsResponse, p as RecordingArtifact, q as RecordingAccessResponse, r as CreateRenderInput, s as CreditBalance, t as operations, u as CreateSpeechInput, v as SpeechResult, w as CreateSpeechStreamRequest, x as SpeechStreamEvent, y as SpeechLiveOpen, T as TranscriptPayload, V as VoiceWebhookRequest } from './types-B8MMMQ_V.js';
2
+ export { z as AvatarClip, B as CallConnection, D as CallQueued, F as ClipDeclaration, G as ClipLibraryPlan, H as ClipSource, I as ContextMessage, J as EndCallReason, K as ExternalLlm, M as RecordingArtifactStatus, N as RecordingMode, O as RenderShot, P as SpeechAlignedText, Q as VideoPolicy, W as VoiceWebhook, X as VoiceWebhookErrorCode, Y as createSpeechRequestSchema, Z as createSpeechStreamRequestSchema, _ as isQueued, $ as listRecordingsResponseSchema, a0 as recordingAccessResponseSchema, a1 as recordingArtifactSchema, a2 as recordingModeSchema, a3 as speechAlignedTextSchema, a4 as speechLiveClientMessageSchema, a5 as speechLiveOpenSchema, a6 as speechLiveServerMessageSchema, a7 as speechStreamEventSchema } from './types-B8MMMQ_V.js';
3
3
  import { ConnectionHistoryResponse } from './connection-history.js';
4
4
  import { z } from 'zod';
5
5
 
@@ -9,6 +9,29 @@ type PollOptions = {
9
9
  pollMs?: number;
10
10
  };
11
11
 
12
+ type TextMessage = Extract<SpeechLiveClientMessage, {
13
+ type: "text";
14
+ }>;
15
+ /**
16
+ * One live speech session: send text while it is written, read PCM and alignment per utterance.
17
+ *
18
+ * Iterate it once. Every message is validated against the generated `SpeechLiveServerMessage`
19
+ * schema and the session's ordering before it is yielded; a terminal `completed` or `failed` ends
20
+ * the iteration. `break`/`return()` and the caller's signal close the socket, which the platform
21
+ * bills like `cancel`: the bytes already sent are paid for.
22
+ */
23
+ interface SpeechSession extends AsyncIterable<SpeechLiveServerMessage> {
24
+ /** Append text to the open utterance: 0 first, then one more after each flush. */
25
+ text(utterance: TextMessage["utterance"], text: TextMessage["text"]): void;
26
+ /** Speak the open utterance now and open the next. */
27
+ flush(utterance: TextMessage["utterance"]): void;
28
+ /** No more text: the platform speaks what is buffered, then completes. A no-op once the session is over. */
29
+ end(): void;
30
+ /** Stop now. Bytes already sent are billed. A no-op once the session is over. */
31
+ cancel(): void;
32
+ [Symbol.asyncIterator](): AsyncIterator<SpeechLiveServerMessage, void, undefined>;
33
+ }
34
+
12
35
  declare const renderStatusSchema: z.ZodEnum<{
13
36
  ready: "ready";
14
37
  failed: "failed";
@@ -483,6 +506,17 @@ declare class RealtimeAvatar {
483
506
  createSpeechStream(voiceId: operations["createSpeechStream"]["parameters"]["path"]["voiceId"], input: CreateSpeechStreamRequest, options?: {
484
507
  signal?: AbortSignal;
485
508
  }): AsyncGenerator<SpeechStreamEvent, void, unknown>;
509
+ /**
510
+ * A live speech session: text in while it is written, PCM and alignment out per utterance.
511
+ * Resolves once the platform admitted the session (voice, slot, credit hold, provider socket),
512
+ * so every refusal before that rejects here. In Workers the upgrade is a fetch and a refusal is
513
+ * a RealtimeAvatarHttpError; Node's standard WebSocket reports no status, so there it is a
514
+ * RealtimeAvatarError. Never retried. `timeoutMs` bounds opening, `totalTimeoutMs` (if set) the
515
+ * whole session; abort or `break` closes it, which bills what was already sent.
516
+ */
517
+ openSpeechSession(voiceId: operations["openSpeechLive"]["parameters"]["path"]["voiceId"], input: SpeechLiveOpen, options?: {
518
+ signal?: AbortSignal;
519
+ }): Promise<SpeechSession>;
486
520
  }
487
521
 
488
522
  /** Base for everything this SDK throws, so `catch (e) { if (e instanceof RealtimeAvatarError) }` works. */
@@ -962,4 +996,4 @@ declare const proxyEndRequestSchema: z.ZodObject<{
962
996
  }>>>;
963
997
  }, z.core.$strict>;
964
998
 
965
- export { Asset, AssetKind, Avatar, AvatarSourceSwap, AvatarUpdate, CallMode, CallPolicy, ClipLibrary, ClipLibraryDeclaration, ClipLibraryUpdate, CreateAnimaAvatarInput, CreateRenderInput, CreateSpeechInput, CreateSpeechStreamRequest, CreditBalance, DEFAULT_FISH_TTS_MODEL, EndCallOptions, type ExternalSpeechSegment, type ExternalTurnContext, type ExternalUtterance, FISH_DELIVERY_MODELS, type FishDelivery, type FishTtsModel, ListRecordingsQuery, ListRecordingsResponse, type ListRendersQuery, type ListRendersResponse, ListSessionsOptions, LoopRedirect, RealtimeAvatar, RealtimeAvatarError, RealtimeAvatarHttpError, type RealtimeAvatarOptions, RealtimeAvatarWebhookError, RecordingAccessResponse, RecordingArtifact, RegisteredAvatar, type Render, type RenderAccessResponse, type RenderFailureCode, type RenderPlayback, type RenderStatus, type ShotTiming, SpeechResult, SpeechStreamEvent, type StartCallOptions, StartCallResult, TranscriptPayload, UsageSession, UsageSessionPage, VoiceWebhookRequest, type WebhookVerificationErrorCode, capacityBusyResponseSchema, clipLibraryDeclarationSchema, externalSpeechSegmentSchema, externalTurnContextSchema, externalUtteranceSchema, fishTtsModelSchema, listRendersResponseSchema, liveKitSessionGrantSchema, proxyConnectRequestSchema, proxyEndRequestSchema, renderAccessResponseSchema, renderSchema, verifyTranscript, verifyVoiceWebhook };
999
+ export { Asset, AssetKind, Avatar, AvatarSourceSwap, AvatarUpdate, CallMode, CallPolicy, ClipLibrary, ClipLibraryDeclaration, ClipLibraryUpdate, CreateAnimaAvatarInput, CreateRenderInput, CreateSpeechInput, CreateSpeechStreamRequest, CreditBalance, DEFAULT_FISH_TTS_MODEL, EndCallOptions, type ExternalSpeechSegment, type ExternalTurnContext, type ExternalUtterance, FISH_DELIVERY_MODELS, type FishDelivery, type FishTtsModel, ListRecordingsQuery, ListRecordingsResponse, type ListRendersQuery, type ListRendersResponse, ListSessionsOptions, LoopRedirect, RealtimeAvatar, RealtimeAvatarError, RealtimeAvatarHttpError, type RealtimeAvatarOptions, RealtimeAvatarWebhookError, RecordingAccessResponse, RecordingArtifact, RegisteredAvatar, type Render, type RenderAccessResponse, type RenderFailureCode, type RenderPlayback, type RenderStatus, type ShotTiming, SpeechLiveClientMessage, SpeechLiveOpen, SpeechLiveServerMessage, SpeechResult, type SpeechSession, SpeechStreamEvent, type StartCallOptions, StartCallResult, TranscriptPayload, UsageSession, UsageSessionPage, VoiceWebhookRequest, type WebhookVerificationErrorCode, capacityBusyResponseSchema, clipLibraryDeclarationSchema, externalSpeechSegmentSchema, externalTurnContextSchema, externalUtteranceSchema, fishTtsModelSchema, listRendersResponseSchema, liveKitSessionGrantSchema, proxyConnectRequestSchema, proxyEndRequestSchema, renderAccessResponseSchema, renderSchema, verifyTranscript, verifyVoiceWebhook };
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
1
1
  import './chunk-6KQS7CTR.js';
2
- export { DEFAULT_FISH_TTS_MODEL, FISH_DELIVERY_MODELS, RealtimeAvatar, RealtimeAvatarError, RealtimeAvatarHttpError, RealtimeAvatarWebhookError, capacityBusyResponseSchema, clipLibraryDeclarationSchema, createSpeechRequestSchema, createSpeechStreamRequestSchema, externalSpeechSegmentSchema, externalTurnContextSchema, externalUtteranceSchema, fishTtsModelSchema, isQueued, listRecordingsResponseSchema, listRendersResponseSchema, liveKitSessionGrantSchema, proxyConnectRequestSchema, proxyEndRequestSchema, recordingAccessResponseSchema, recordingArtifactSchema, recordingModeSchema, renderAccessResponseSchema, renderSchema, speechAlignedTextSchema, speechStreamEventSchema, verifyTranscript, verifyVoiceWebhook } from './chunk-7NZ7U5HN.js';
2
+ export { DEFAULT_FISH_TTS_MODEL, FISH_DELIVERY_MODELS, RealtimeAvatar, RealtimeAvatarError, RealtimeAvatarHttpError, RealtimeAvatarWebhookError, capacityBusyResponseSchema, clipLibraryDeclarationSchema, createSpeechRequestSchema, createSpeechStreamRequestSchema, externalSpeechSegmentSchema, externalTurnContextSchema, externalUtteranceSchema, fishTtsModelSchema, isQueued, listRecordingsResponseSchema, listRendersResponseSchema, liveKitSessionGrantSchema, proxyConnectRequestSchema, proxyEndRequestSchema, recordingAccessResponseSchema, recordingArtifactSchema, recordingModeSchema, renderAccessResponseSchema, renderSchema, speechAlignedTextSchema, speechLiveClientMessageSchema, speechLiveOpenSchema, speechLiveServerMessageSchema, speechStreamEventSchema, verifyTranscript, verifyVoiceWebhook } from './chunk-YBKE7SDI.js';
3
3
  import './chunk-MCIXU6XP.js';