@genex-ai/embed-sdk 0.37.0 → 0.38.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.
@@ -13,8 +13,17 @@ import {
13
13
  PLATFORM_REQUEST_TYPE,
14
14
  RESERVED_QUERY_PREFIX,
15
15
  SAVE_MAX_BYTES,
16
+ VOICE_AUDIO_CONTENT_TYPE,
17
+ VOICE_IDEMPOTENCY_HEADER,
18
+ VOICE_LANGUAGE_PARAM,
19
+ VOICE_MODEL_PARAM,
20
+ VOICE_RECORDING_SAMPLE_RATE,
21
+ VOICE_SPEECH_CHANNELS,
22
+ VOICE_SPEECH_ENCODING,
23
+ VOICE_SPEECH_SAMPLE_RATE,
16
24
  createBridgeId,
17
25
  encodeBridgeMessage,
26
+ encodeWavPcm16,
18
27
  foldRuntimeConversation,
19
28
  hasNativeMarker,
20
29
  isGenerationId,
@@ -22,9 +31,13 @@ import {
22
31
  isLoopbackHttpOrigin,
23
32
  isPlatformUrl,
24
33
  isSaveName,
34
+ isVoiceLanguage,
35
+ isVoiceTier,
25
36
  parseGenerationStreamEvent,
26
37
  parsePlatformRequest,
27
38
  parsePlatformResult,
39
+ parseSpeechStreamEvent,
40
+ parseWavPcm16,
28
41
  practiceOrigin,
29
42
  readBridgeNativeCommand,
30
43
  readNativeEntry,
@@ -37,7 +50,7 @@ import {
37
50
  savableMime,
38
51
  stripNativeEntry,
39
52
  stripReturnMarker
40
- } from "./chunk-WQJM5MJL.js";
53
+ } from "./chunk-YQY2WLAJ.js";
41
54
 
42
55
  // src/platform.ts
43
56
  var ctx = null;
@@ -70,9 +83,9 @@ function localTest() {
70
83
  function fail(error) {
71
84
  return { ok: false, error };
72
85
  }
73
- async function settled(run, fallback) {
86
+ async function settled(run2, fallback) {
74
87
  try {
75
- return await run();
88
+ return await run2();
76
89
  } catch {
77
90
  return fallback;
78
91
  }
@@ -899,6 +912,26 @@ function createSseParser(onEvent) {
899
912
  }
900
913
  };
901
914
  }
915
+ async function readEventStream(body, onEvent) {
916
+ let stopped = false;
917
+ const parser = createSseParser((event) => {
918
+ if (!stopped && onEvent(event) === true) stopped = true;
919
+ });
920
+ const reader = body.getReader();
921
+ const decoder = new TextDecoder();
922
+ for (; ; ) {
923
+ const { value, done } = await reader.read();
924
+ if (done) break;
925
+ parser.push(decoder.decode(value, { stream: true }));
926
+ if (stopped) {
927
+ reader.cancel().catch(() => {
928
+ });
929
+ return;
930
+ }
931
+ }
932
+ parser.push(decoder.decode());
933
+ parser.end();
934
+ }
902
935
  function firstStringProperty(schema) {
903
936
  if (!schema || typeof schema !== "object") return void 0;
904
937
  const properties = schema.properties;
@@ -1011,15 +1044,19 @@ function endpoint() {
1011
1044
  const target = practice();
1012
1045
  return target ? { base: target.origin, token: target.token } : { base: must2().apiUrl, token: must2().getToken() };
1013
1046
  }
1014
- async function request(path, body) {
1047
+ function request(path, body) {
1048
+ return _runtimeRequest(path, body === void 0 ? {} : { headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) });
1049
+ }
1050
+ async function _runtimeRequest(path, init = {}) {
1015
1051
  const { base, token } = endpoint();
1016
1052
  if (!token) throw new Error("no_player_session");
1017
1053
  const response = await fetch(`${base}/api/runtime${path}`, {
1018
- method: body === void 0 ? "GET" : "POST",
1054
+ method: init.body === void 0 ? "GET" : "POST",
1019
1055
  credentials: "omit",
1020
1056
  cache: "no-store",
1021
- headers: { Authorization: `Bearer ${token}`, ...body === void 0 ? {} : { "Content-Type": "application/json" } },
1022
- ...body === void 0 ? {} : { body: JSON.stringify(body) }
1057
+ headers: { Authorization: `Bearer ${token}`, ...init.headers },
1058
+ ...init.body === void 0 ? {} : { body: init.body },
1059
+ ...init.signal ? { signal: init.signal } : {}
1023
1060
  });
1024
1061
  const result = await response.json().catch(() => ({}));
1025
1062
  if (!response.ok) {
@@ -1256,7 +1293,10 @@ function validCount(value, min, max) {
1256
1293
  function invalidGrantOptions(options) {
1257
1294
  const judge2 = options.judge;
1258
1295
  if (judge2 !== void 0 && (typeof judge2 !== "object" || judge2 === null || typeof judge2.enabled !== "boolean" || !validCount(judge2.estimatedCallsPerPeriod, 1, 1e5))) return "invalid_judge_terms";
1259
- if (!Array.isArray(options.models) || options.models.length < (judge2?.enabled ? 0 : 1) || options.models.length > 8 || options.models.some((id) => typeof id !== "string" || !id || id.length > 150) || new Set(options.models).size !== options.models.length) return "invalid_models";
1296
+ const voice = options.voice;
1297
+ if (voice !== void 0 && (typeof voice !== "object" || voice === null || Array.isArray(voice))) return "invalid_grant_terms";
1298
+ const modelsOptional = judge2?.enabled === true || voice !== void 0;
1299
+ if (!Array.isArray(options.models) || options.models.length < (modelsOptional ? 0 : 1) || options.models.length > 8 || options.models.some((id) => typeof id !== "string" || !id || id.length > 150) || new Set(options.models).size !== options.models.length) return "invalid_models";
1260
1300
  const max = options.perCallMaxCredits ?? options.perCallMaxCoins;
1261
1301
  const estimate = options.perCallEstimateCredits ?? options.perCallEstimateCoins;
1262
1302
  if (!validCount(max, 1, 1e6) || estimate !== void 0 && (!validCount(estimate, 1, 1e6) || estimate > max) || options.perCallMaxCredits !== void 0 && options.perCallMaxCoins !== void 0 && options.perCallMaxCredits !== options.perCallMaxCoins || options.perCallEstimateCredits !== void 0 && options.perCallEstimateCoins !== void 0 && options.perCallEstimateCredits !== options.perCallEstimateCoins) return "invalid_declared_price";
@@ -1383,6 +1423,7 @@ async function requestSpendGrant(options) {
1383
1423
  ...options.maxPendingExternal !== void 0 ? { maxPendingExternal: options.maxPendingExternal } : {},
1384
1424
  ...options.allowExternal !== void 0 ? { allowExternal: options.allowExternal } : {},
1385
1425
  ...options.judge !== void 0 ? { judge: { enabled: options.judge.enabled, estimatedCallsPerPeriod: options.judge.estimatedCallsPerPeriod } } : {},
1426
+ ...options.voice !== void 0 ? { voice: options.voice } : {},
1386
1427
  ...options.shareWithRooms === true ? { shareWithRooms: true } : {}
1387
1428
  });
1388
1429
  if (!isGrantId(view.id)) throw new Error("invalid_grant_response");
@@ -1471,6 +1512,24 @@ async function generateUnderGrant(grantId, input, idempotencyKey, timeoutMs) {
1471
1512
  return { status: "failed", error: error instanceof Error ? error.message : "generation_unavailable" };
1472
1513
  }
1473
1514
  }
1515
+ async function _openEventStream(path, body, signal) {
1516
+ const { base, token } = endpoint();
1517
+ if (!token) return { error: "no_player_session" };
1518
+ const response = await fetch(`${base}/api/runtime${path}`, {
1519
+ method: "POST",
1520
+ credentials: "omit",
1521
+ cache: "no-store",
1522
+ headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json", Accept: "text/event-stream" },
1523
+ body: JSON.stringify(body),
1524
+ ...signal ? { signal } : {}
1525
+ });
1526
+ if (!response.ok) {
1527
+ const refusal = await response.json().catch(() => ({}));
1528
+ return { error: typeof refusal.error === "string" ? refusal.error : "generation_unavailable" };
1529
+ }
1530
+ if (!response.body || !(response.headers.get("content-type") ?? "").includes("text/event-stream")) throw new Error("invalid_generation_response");
1531
+ return { body: response.body };
1532
+ }
1474
1533
  async function generateStream(options) {
1475
1534
  const ceiling = ceilingField(options);
1476
1535
  if ("error" in ceiling) return { status: "failed", error: ceiling.error };
@@ -1516,46 +1575,22 @@ async function generateStream(options) {
1516
1575
  };
1517
1576
  const aborted = () => ({ status: "pending", ...generationId ? { generationId } : {}, error: "stream_aborted" });
1518
1577
  try {
1519
- const { base, token } = endpoint();
1520
- if (!token) return { status: "failed", error: "no_player_session" };
1521
- const response = await fetch(`${base}/api/runtime/grants/${encodeURIComponent(grantId)}/generations/stream`, {
1522
- method: "POST",
1523
- credentials: "omit",
1524
- cache: "no-store",
1525
- headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json", Accept: "text/event-stream" },
1526
- body: JSON.stringify(await wireBody({ ...body, idempotencyKey: idempotencyKey ?? crypto.randomUUID() })),
1527
- ...signal ? { signal } : {}
1528
- });
1529
- if (!response.ok) {
1530
- const refusal = await response.json().catch(() => ({}));
1531
- return { status: "failed", error: typeof refusal.error === "string" ? refusal.error : "generation_unavailable" };
1532
- }
1533
- if (!response.body || !(response.headers.get("content-type") ?? "").includes("text/event-stream")) throw new Error("invalid_generation_response");
1578
+ const opened = await _openEventStream(
1579
+ `/grants/${encodeURIComponent(grantId)}/generations/stream`,
1580
+ await wireBody({ ...body, idempotencyKey: idempotencyKey ?? crypto.randomUUID() }),
1581
+ signal
1582
+ );
1583
+ if ("error" in opened) return { status: "failed", error: opened.error };
1534
1584
  let done;
1535
- const parser = createSseParser(({ event, data }) => {
1536
- const parsed = parseGenerationStreamEvent(event, data);
1537
- if (!parsed || done) return;
1538
- if (parsed.event === "start") generationId ??= parsed.data.generationId;
1539
- else if (parsed.event === "delta") deliver(parsed.data.text);
1540
- else done = parsed.data;
1541
- });
1542
- const reader = response.body.getReader();
1543
- const decoder = new TextDecoder();
1544
1585
  try {
1545
- for (; ; ) {
1546
- const { value, done: ended } = await reader.read();
1547
- if (ended) break;
1548
- parser.push(decoder.decode(value, { stream: true }));
1549
- if (done) {
1550
- reader.cancel().catch(() => {
1551
- });
1552
- break;
1553
- }
1554
- }
1555
- if (!done) {
1556
- parser.push(decoder.decode());
1557
- parser.end();
1558
- }
1586
+ await readEventStream(opened.body, ({ event, data }) => {
1587
+ const parsed = parseGenerationStreamEvent(event, data);
1588
+ if (!parsed) return false;
1589
+ if (parsed.event === "start") generationId ??= parsed.data.generationId;
1590
+ else if (parsed.event === "delta") deliver(parsed.data.text);
1591
+ else done = parsed.data;
1592
+ return done !== void 0;
1593
+ });
1559
1594
  } catch {
1560
1595
  if (signal?.aborted) return aborted();
1561
1596
  }
@@ -1712,6 +1747,33 @@ function generationErrorMessage(code) {
1712
1747
  return "The game\u2019s quick checks are busy right now. Try again in a moment.";
1713
1748
  case "judge_paused":
1714
1749
  return "The game\u2019s quick checks are paused for now. Try again later.";
1750
+ // --- voice ----------------------------------------------------------------
1751
+ case "voice_requires_grant":
1752
+ return "This game\u2019s voice features run under a budget you approve. Set one up to keep going.";
1753
+ case "voice_not_enabled":
1754
+ return "This budget doesn\u2019t cover the game\u2019s voice features. Set up a new one to keep going.";
1755
+ case "voice_requires_credits":
1756
+ return "The game\u2019s voice features are paid from your Genex credits, not your own plan.";
1757
+ case "voice_not_found":
1758
+ return "That voice isn\u2019t available here.";
1759
+ case "voice_busy":
1760
+ return "The game\u2019s voice features are busy right now. Try again in a moment.";
1761
+ case "voice_paused":
1762
+ return "The game\u2019s voice features are paused for now. Try again later.";
1763
+ case "text_too_long":
1764
+ return "That line is too long to speak in one go. Shorten it and try again.";
1765
+ case "audio_invalid":
1766
+ return "That recording couldn\u2019t be read. Try recording again.";
1767
+ case "audio_too_long":
1768
+ return "That recording is too long. Keep it shorter and try again.";
1769
+ case "audio_too_short":
1770
+ return "That recording was too short to make out. Speak a little longer and try again.";
1771
+ case "microphone_denied":
1772
+ return "The microphone isn\u2019t allowed for this game. Allow it in your browser to talk, or type instead.";
1773
+ case "microphone_unavailable":
1774
+ return "No microphone could be used. Check that one is connected, or type instead.";
1775
+ case "request_aborted":
1776
+ return "This stopped before its answer arrived. If it had already started, it is still charged as usual.";
1715
1777
  // --- streamed replies -----------------------------------------------------
1716
1778
  case "stream_requires_grant":
1717
1779
  return "Live replies run under a budget you approve. Set one up to keep going.";
@@ -1848,6 +1910,656 @@ function generationErrorMessage(code) {
1848
1910
  }
1849
1911
  }
1850
1912
 
1913
+ // src/voice.ts
1914
+ var MAX_SPEECH_CHANNELS = 8;
1915
+ function validIdempotencyKey(value) {
1916
+ return typeof value === "string" && value.length >= 8 && value.length <= 100;
1917
+ }
1918
+ function validName(value) {
1919
+ return typeof value === "string" && value.length >= 1 && value.length <= 150;
1920
+ }
1921
+ function invalidVoiceCall(options) {
1922
+ if (options.grantId === void 0 || options.grantId === null) return "voice_requires_grant";
1923
+ if (!isGenerationId(options.grantId)) return "invalid_grant_id";
1924
+ if (options.language !== void 0 && !isVoiceLanguage(options.language)) return "invalid_request";
1925
+ if (options.idempotencyKey !== void 0 && !validIdempotencyKey(options.idempotencyKey)) return "invalid_idempotency_key";
1926
+ return void 0;
1927
+ }
1928
+ function callResult(view) {
1929
+ const status = view.status === "dispatching" ? "pending" : view.status;
1930
+ return {
1931
+ status,
1932
+ generationId: view.generationId,
1933
+ billing: view.billing,
1934
+ ...view.error ? { error: view.error } : status === "pending" ? { error: "wait_timeout" } : {},
1935
+ ...view.contentBlocked ? { contentBlocked: view.contentBlocked } : {},
1936
+ ...view.source ? { source: view.source } : {}
1937
+ };
1938
+ }
1939
+ function errorCode(error, fallback) {
1940
+ return error instanceof Error ? error.message : fallback;
1941
+ }
1942
+ async function getVoiceCatalog() {
1943
+ try {
1944
+ if ("status" in approvalGuards()) return { enabled: false };
1945
+ const catalog = await _runtimeRequest("/voice");
1946
+ return catalog.enabled === true ? catalog : { enabled: false };
1947
+ } catch {
1948
+ return { enabled: false };
1949
+ }
1950
+ }
1951
+ async function audioBytes(audio) {
1952
+ if (audio instanceof Uint8Array) return new Uint8Array(audio);
1953
+ if (audio instanceof ArrayBuffer) return new Uint8Array(audio);
1954
+ if (typeof Blob !== "undefined" && audio instanceof Blob) return new Uint8Array(await audio.arrayBuffer());
1955
+ return void 0;
1956
+ }
1957
+ function transcriptionPath(options) {
1958
+ const query = new URLSearchParams();
1959
+ if (options.language !== void 0) query.set(VOICE_LANGUAGE_PARAM, options.language);
1960
+ if (options.model !== void 0) query.set(VOICE_MODEL_PARAM, options.model);
1961
+ const search = query.toString();
1962
+ return `/grants/${encodeURIComponent(options.grantId)}/transcriptions${search ? `?${search}` : ""}`;
1963
+ }
1964
+ async function transcribe(options) {
1965
+ const invalid = invalidVoiceCall(options) ?? (options.model !== void 0 && !validName(options.model) ? "invalid_request" : void 0);
1966
+ if (invalid) return { status: "failed", error: invalid };
1967
+ const guarded = approvalGuards();
1968
+ if ("status" in guarded) return guarded;
1969
+ const audio = await audioBytes(options.audio).catch(() => void 0);
1970
+ if (!audio || !parseWavPcm16(audio)) return { status: "failed", error: "audio_invalid" };
1971
+ if (options.signal?.aborted) return { status: "failed", error: "request_aborted" };
1972
+ try {
1973
+ const view = await _runtimeRequest(transcriptionPath(options), {
1974
+ headers: { "Content-Type": VOICE_AUDIO_CONTENT_TYPE, [VOICE_IDEMPOTENCY_HEADER]: options.idempotencyKey ?? crypto.randomUUID() },
1975
+ body: audio,
1976
+ ...options.signal ? { signal: options.signal } : {}
1977
+ });
1978
+ if (!isGenerationId(view.generationId) || view.grantId !== options.grantId) throw new Error("invalid_transcription_response");
1979
+ return {
1980
+ ...callResult(view),
1981
+ durationMs: view.durationMs,
1982
+ ...typeof view.text === "string" ? { text: view.text } : {},
1983
+ ...view.language ? { language: view.language } : {}
1984
+ };
1985
+ } catch (error) {
1986
+ if (options.signal?.aborted) return { status: "unknown", error: "request_aborted" };
1987
+ return { status: "failed", error: errorCode(error, "voice_unavailable") };
1988
+ }
1989
+ }
1990
+ function pcm16Samples(bytes) {
1991
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
1992
+ const samples = new Int16Array(bytes.byteLength >> 1);
1993
+ for (let i = 0; i < samples.length; i++) samples[i] = view.getInt16(i * 2, true);
1994
+ return samples;
1995
+ }
1996
+ function joinBytes(chunks) {
1997
+ const out = new Uint8Array(chunks.reduce((total, chunk) => total + chunk.byteLength, 0));
1998
+ let at = 0;
1999
+ for (const chunk of chunks) {
2000
+ out.set(chunk, at);
2001
+ at += chunk.byteLength;
2002
+ }
2003
+ return out;
2004
+ }
2005
+ function decodeBase64(text) {
2006
+ try {
2007
+ const binary = atob(text);
2008
+ const out = new Uint8Array(binary.length);
2009
+ for (let i = 0; i < binary.length; i++) out[i] = binary.charCodeAt(i);
2010
+ return out;
2011
+ } catch {
2012
+ return void 0;
2013
+ }
2014
+ }
2015
+ function wavBlob(wav) {
2016
+ return new Blob([wav.slice()], { type: VOICE_AUDIO_CONTENT_TYPE });
2017
+ }
2018
+ var DEFAULT_SPEECH_FORMAT = { sampleRate: VOICE_SPEECH_SAMPLE_RATE, channels: VOICE_SPEECH_CHANNELS };
2019
+ function _spokenResult(fields, chunks = [], format = DEFAULT_SPEECH_FORMAT) {
2020
+ const audio = joinBytes(chunks);
2021
+ const wav = () => wavBlob(encodeWavPcm16(pcm16Samples(audio), format.sampleRate, format.channels));
2022
+ return { ...fields, audio, sampleRate: format.sampleRate, channels: format.channels, wav };
2023
+ }
2024
+ function invalidSpeakOptions(options) {
2025
+ const shared = invalidVoiceCall(options);
2026
+ if (shared) return shared;
2027
+ if (typeof options.text !== "string" || !options.text.trim()) return "invalid_request";
2028
+ if (!validName(options.voice)) return "invalid_request";
2029
+ if (options.tier !== void 0 && !isVoiceTier(options.tier)) return "invalid_request";
2030
+ if (options.style !== void 0 && typeof options.style !== "string") return "invalid_request";
2031
+ return void 0;
2032
+ }
2033
+ function speechBody(options) {
2034
+ return {
2035
+ idempotencyKey: options.idempotencyKey ?? crypto.randomUUID(),
2036
+ text: options.text,
2037
+ voice: options.voice,
2038
+ ...options.tier !== void 0 ? { tier: options.tier } : {},
2039
+ ...options.style !== void 0 ? { style: options.style } : {},
2040
+ ...options.language !== void 0 ? { language: options.language } : {}
2041
+ };
2042
+ }
2043
+ function playableFormat(start) {
2044
+ return start.encoding === VOICE_SPEECH_ENCODING && Number.isSafeInteger(start.sampleRate) && start.sampleRate > 0 && Number.isSafeInteger(start.channels) && start.channels >= 1 && start.channels <= MAX_SPEECH_CHANNELS;
2045
+ }
2046
+ function notify(handler, value) {
2047
+ try {
2048
+ handler?.(value);
2049
+ } catch {
2050
+ }
2051
+ }
2052
+ async function speak(options) {
2053
+ const invalid = invalidSpeakOptions(options);
2054
+ if (invalid) return _spokenResult({ status: "failed", error: invalid });
2055
+ const guarded = approvalGuards();
2056
+ if ("status" in guarded) return _spokenResult(guarded);
2057
+ if (options.signal?.aborted) return _spokenResult({ status: "failed", error: "request_aborted" });
2058
+ const chunks = [];
2059
+ let format = DEFAULT_SPEECH_FORMAT;
2060
+ let generationId;
2061
+ let done;
2062
+ let unplayable = false;
2063
+ const result = (fields) => _spokenResult(fields, chunks, format);
2064
+ const unsettled = (error) => result({ status: "unknown", ...generationId ? { generationId } : {}, error });
2065
+ try {
2066
+ const opened = await _openEventStream(`/grants/${encodeURIComponent(options.grantId)}/speech`, speechBody(options), options.signal);
2067
+ if ("error" in opened) return result({ status: "failed", error: opened.error });
2068
+ try {
2069
+ await readEventStream(opened.body, ({ event, data }) => {
2070
+ const parsed = parseSpeechStreamEvent(event, data);
2071
+ if (!parsed) return false;
2072
+ if (parsed.event === "done") {
2073
+ done = parsed.data;
2074
+ return true;
2075
+ }
2076
+ if (parsed.event === "start") {
2077
+ if (generationId) return false;
2078
+ if (!playableFormat(parsed.data)) {
2079
+ unplayable = true;
2080
+ return true;
2081
+ }
2082
+ ({ generationId } = parsed.data);
2083
+ format = { sampleRate: parsed.data.sampleRate, channels: parsed.data.channels };
2084
+ notify(options.onStart, { generationId: parsed.data.generationId, ...format });
2085
+ return false;
2086
+ }
2087
+ const pcm = decodeBase64(parsed.data.data);
2088
+ if (pcm?.byteLength) {
2089
+ chunks.push(pcm);
2090
+ notify(options.onAudio, pcm);
2091
+ }
2092
+ return false;
2093
+ });
2094
+ } catch {
2095
+ }
2096
+ if (options.signal?.aborted) return unsettled("request_aborted");
2097
+ if (unplayable) throw new Error("invalid_speech_response");
2098
+ const view = done;
2099
+ if (!view) return unsettled("provider_outcome_unknown");
2100
+ if (!isGenerationId(view.generationId) || view.grantId !== options.grantId || generationId !== void 0 && view.generationId !== generationId) {
2101
+ throw new Error("invalid_speech_response");
2102
+ }
2103
+ return result({
2104
+ ...callResult(view),
2105
+ voice: view.voice,
2106
+ tier: view.tier,
2107
+ characters: view.characters,
2108
+ ...view.durationMs !== void 0 ? { durationMs: view.durationMs } : {}
2109
+ });
2110
+ } catch (error) {
2111
+ if (options.signal?.aborted) return unsettled("request_aborted");
2112
+ return result({ status: "failed", error: errorCode(error, "voice_unavailable") });
2113
+ }
2114
+ }
2115
+
2116
+ // src/voice-audio.ts
2117
+ var RECORDER_DEFAULT_SECONDS = 30;
2118
+ var RECORDER_MAX_SECONDS = 60;
2119
+ var LEVEL_INTERVAL_SECONDS = 0.05;
2120
+ var SCRIPT_PROCESSOR_FRAMES = 4096;
2121
+ var SPEAKER_DEFAULT_MAX_CHARACTERS = 500;
2122
+ var PLAYBACK_LEAD_SECONDS = 0.05;
2123
+ function resample(input, fromRate, toRate) {
2124
+ if (fromRate === toRate) return input.slice();
2125
+ const ratio = fromRate / toRate;
2126
+ const out = new Float32Array(Math.floor(input.length / ratio));
2127
+ for (let i = 0; i < out.length; i++) out[i] = ratio > 1 ? average(input, i * ratio, (i + 1) * ratio) : interpolate(input, i * ratio);
2128
+ return out;
2129
+ }
2130
+ function average(input, from, to) {
2131
+ const start = Math.floor(from);
2132
+ const end = Math.min(input.length, Math.max(start + 1, Math.floor(to)));
2133
+ let sum = 0;
2134
+ for (let j = start; j < end; j++) sum += input[j] ?? 0;
2135
+ return sum / (end - start);
2136
+ }
2137
+ function interpolate(input, at) {
2138
+ const left = Math.floor(at);
2139
+ const right = Math.min(left + 1, input.length - 1);
2140
+ const t = at - left;
2141
+ return (input[left] ?? 0) * (1 - t) + (input[right] ?? 0) * t;
2142
+ }
2143
+ function floatToPcm16(samples) {
2144
+ const out = new Int16Array(samples.length);
2145
+ for (let i = 0; i < samples.length; i++) {
2146
+ const s = Math.max(-1, Math.min(1, samples[i] ?? 0));
2147
+ out[i] = s < 0 ? s * 32768 : s * 32767;
2148
+ }
2149
+ return out;
2150
+ }
2151
+ function encodeRecording(chunks, inputRate) {
2152
+ const joined = new Float32Array(chunks.reduce((total, chunk) => total + chunk.length, 0));
2153
+ let at = 0;
2154
+ for (const chunk of chunks) {
2155
+ joined.set(chunk, at);
2156
+ at += chunk.length;
2157
+ }
2158
+ return encodeWavPcm16(floatToPcm16(resample(joined, inputRate, VOICE_RECORDING_SAMPLE_RATE)), VOICE_RECORDING_SAMPLE_RATE, 1);
2159
+ }
2160
+ function createLevelMeter(frames, onLevel) {
2161
+ let squares = 0;
2162
+ let count = 0;
2163
+ return (samples) => {
2164
+ for (const s of samples) squares += s * s;
2165
+ count += samples.length;
2166
+ if (count < frames) return;
2167
+ onLevel(Math.min(1, Math.sqrt(Math.sqrt(squares / count))));
2168
+ squares = 0;
2169
+ count = 0;
2170
+ };
2171
+ }
2172
+ var SENTENCE_END = /[.!?…]+["'”’»)\]]*(?=\s)|[。!?]+[」』”’))]*|\n/g;
2173
+ function takeSentences(text) {
2174
+ const sentences = [];
2175
+ let from = 0;
2176
+ for (const match of text.matchAll(SENTENCE_END)) {
2177
+ const end = match.index + match[0].length;
2178
+ const sentence = text.slice(from, end).trim();
2179
+ if (sentence) sentences.push(sentence);
2180
+ from = end;
2181
+ }
2182
+ return { sentences, rest: text.slice(from) };
2183
+ }
2184
+ function pack(pieces, max) {
2185
+ const lines = [];
2186
+ let line = "";
2187
+ for (const piece of pieces) {
2188
+ if (!line) line = piece;
2189
+ else if (line.length + 1 + piece.length <= max) line += ` ${piece}`;
2190
+ else {
2191
+ lines.push(line);
2192
+ line = piece;
2193
+ }
2194
+ }
2195
+ if (line) lines.push(line);
2196
+ return lines;
2197
+ }
2198
+ function cut(word, max) {
2199
+ if (word.length <= max) return [word];
2200
+ const characters = Array.from(word);
2201
+ const pieces = [];
2202
+ for (let i = 0; i < characters.length; i += max) pieces.push(characters.slice(i, i + max).join(""));
2203
+ return pieces;
2204
+ }
2205
+ function splitWords(text, max) {
2206
+ return pack(text.split(/\s+/).filter(Boolean).flatMap((word) => cut(word, max)), max);
2207
+ }
2208
+ function splitLine(text, max) {
2209
+ const clean = text.trim();
2210
+ if (clean.length <= max) return clean ? [clean] : [];
2211
+ const { sentences, rest } = takeSentences(clean);
2212
+ const pieces = [...sentences, rest.trim()].filter(Boolean);
2213
+ return pack(pieces.flatMap((piece) => piece.length <= max ? [piece] : splitWords(piece, max)), max);
2214
+ }
2215
+ function spill(text, max) {
2216
+ const lines = [];
2217
+ let rest = text;
2218
+ while (rest.trim().length > max) {
2219
+ rest = rest.trimStart();
2220
+ const space = rest.slice(0, max + 1).search(/\s\S*$/);
2221
+ const at = space > 0 ? space : /[\uD800-\uDBFF]/.test(rest[max - 1] ?? "") ? max - 1 : max;
2222
+ lines.push(rest.slice(0, at));
2223
+ rest = rest.slice(at);
2224
+ }
2225
+ return { lines, rest };
2226
+ }
2227
+ function recorderError(code, cause) {
2228
+ return Object.assign(new Error(code, cause === void 0 ? void 0 : { cause }), { code });
2229
+ }
2230
+ function deniedByPermission(error) {
2231
+ const name = error?.name;
2232
+ return name === "NotAllowedError" || name === "SecurityError" || name === "PermissionDeniedError";
2233
+ }
2234
+ var CAPTURE_WORKLET = "registerProcessor('genex-voice-capture',class extends AudioWorkletProcessor{process(i){const c=i[0]&&i[0][0];if(c)this.port.postMessage(c.slice(0));return true}})";
2235
+ async function captureNode(context, onSamples) {
2236
+ if (context.audioWorklet && typeof AudioWorkletNode !== "undefined") {
2237
+ const url = URL.createObjectURL(new Blob([CAPTURE_WORKLET], { type: "text/javascript" }));
2238
+ try {
2239
+ await context.audioWorklet.addModule(url);
2240
+ const node2 = new AudioWorkletNode(context, "genex-voice-capture");
2241
+ node2.port.onmessage = (event) => onSamples(event.data);
2242
+ return { node: node2, close: () => {
2243
+ node2.port.onmessage = null;
2244
+ node2.disconnect();
2245
+ } };
2246
+ } catch {
2247
+ } finally {
2248
+ URL.revokeObjectURL(url);
2249
+ }
2250
+ }
2251
+ const node = context.createScriptProcessor(SCRIPT_PROCESSOR_FRAMES, 1, 1);
2252
+ node.onaudioprocess = (event) => onSamples(event.inputBuffer.getChannelData(0).slice());
2253
+ return { node, close: () => {
2254
+ node.onaudioprocess = null;
2255
+ node.disconnect();
2256
+ } };
2257
+ }
2258
+ async function openCapture(deviceId, onSamples) {
2259
+ const devices = typeof navigator === "undefined" ? void 0 : navigator.mediaDevices;
2260
+ if (!devices?.getUserMedia || typeof AudioContext === "undefined") throw recorderError("microphone_unavailable");
2261
+ let stream;
2262
+ try {
2263
+ stream = await devices.getUserMedia({ audio: {
2264
+ channelCount: 1,
2265
+ echoCancellation: true,
2266
+ noiseSuppression: true,
2267
+ ...deviceId ? { deviceId: { exact: deviceId } } : {}
2268
+ } });
2269
+ } catch (error) {
2270
+ throw recorderError(deniedByPermission(error) ? "microphone_denied" : "microphone_unavailable", error);
2271
+ }
2272
+ const stopTracks = () => {
2273
+ for (const track of stream.getTracks()) track.stop();
2274
+ };
2275
+ const context = new AudioContext();
2276
+ try {
2277
+ const source = context.createMediaStreamSource(stream);
2278
+ const tap = await captureNode(context, onSamples);
2279
+ source.connect(tap.node);
2280
+ tap.node.connect(context.destination);
2281
+ void context.resume().catch(() => {
2282
+ });
2283
+ return { sampleRate: context.sampleRate, release: () => {
2284
+ tap.close();
2285
+ source.disconnect();
2286
+ stopTracks();
2287
+ void context.close().catch(() => {
2288
+ });
2289
+ } };
2290
+ } catch (error) {
2291
+ stopTracks();
2292
+ void context.close().catch(() => {
2293
+ });
2294
+ throw recorderError("microphone_unavailable", error);
2295
+ }
2296
+ }
2297
+ function createVoiceRecorder(options = {}) {
2298
+ const asked = Number.isFinite(options.maxSeconds) ? options.maxSeconds : RECORDER_DEFAULT_SECONDS;
2299
+ const maxSeconds = Math.min(RECORDER_MAX_SECONDS, Math.max(1, asked));
2300
+ let capture = null;
2301
+ let chunks = [];
2302
+ let captured = 0;
2303
+ let limit = 0;
2304
+ let meter;
2305
+ let stoppedItself = null;
2306
+ let attempt = 0;
2307
+ const release = () => {
2308
+ const active = capture;
2309
+ capture = null;
2310
+ active?.release();
2311
+ };
2312
+ const finish = (rate) => {
2313
+ release();
2314
+ const wav = wavBlob(encodeRecording(chunks, rate));
2315
+ chunks = [];
2316
+ captured = 0;
2317
+ return wav;
2318
+ };
2319
+ const take = (samples) => {
2320
+ const active = capture;
2321
+ if (!active) return;
2322
+ const kept = samples.length > limit - captured ? samples.subarray(0, limit - captured) : samples;
2323
+ chunks.push(kept);
2324
+ captured += kept.length;
2325
+ try {
2326
+ meter?.(kept);
2327
+ } catch {
2328
+ }
2329
+ if (captured >= limit) stoppedItself = finish(active.sampleRate);
2330
+ };
2331
+ return {
2332
+ get recording() {
2333
+ return capture !== null;
2334
+ },
2335
+ async start() {
2336
+ if (capture) return;
2337
+ stoppedItself = null;
2338
+ chunks = [];
2339
+ captured = 0;
2340
+ const mine = ++attempt;
2341
+ const opened = await openCapture(options.deviceId, take);
2342
+ if (mine !== attempt) {
2343
+ opened.release();
2344
+ return;
2345
+ }
2346
+ limit = Math.round(maxSeconds * opened.sampleRate);
2347
+ const onLevel = options.onLevel;
2348
+ meter = onLevel ? createLevelMeter(Math.round(opened.sampleRate * LEVEL_INTERVAL_SECONDS), (level) => onLevel(level)) : void 0;
2349
+ capture = opened;
2350
+ },
2351
+ async stop() {
2352
+ if (capture) return finish(capture.sampleRate);
2353
+ attempt++;
2354
+ const kept = stoppedItself;
2355
+ stoppedItself = null;
2356
+ if (kept) return kept;
2357
+ throw recorderError("not_recording");
2358
+ },
2359
+ cancel() {
2360
+ attempt++;
2361
+ stoppedItself = null;
2362
+ release();
2363
+ chunks = [];
2364
+ captured = 0;
2365
+ }
2366
+ };
2367
+ }
2368
+ function run(fn) {
2369
+ try {
2370
+ fn();
2371
+ } catch {
2372
+ }
2373
+ }
2374
+ function createWebAudioSink() {
2375
+ let context = null;
2376
+ let nextStart = 0;
2377
+ let last = null;
2378
+ const sources = /* @__PURE__ */ new Set();
2379
+ const waiting = /* @__PURE__ */ new Map();
2380
+ const ready = () => {
2381
+ if (!context && typeof AudioContext !== "undefined") context = new AudioContext();
2382
+ if (context?.state === "suspended") void context.resume().catch(() => {
2383
+ });
2384
+ return context;
2385
+ };
2386
+ const ended = (source) => {
2387
+ sources.delete(source);
2388
+ if (last === source) last = null;
2389
+ const callbacks = waiting.get(source) ?? [];
2390
+ waiting.delete(source);
2391
+ callbacks.forEach(run);
2392
+ };
2393
+ return {
2394
+ prime() {
2395
+ ready();
2396
+ },
2397
+ play(pcm, sampleRate, channels) {
2398
+ const audio = ready();
2399
+ const samples = pcm16Samples(pcm);
2400
+ const frames = Math.floor(samples.length / channels);
2401
+ if (!audio || !frames) return;
2402
+ const buffer = audio.createBuffer(channels, frames, sampleRate);
2403
+ for (let c = 0; c < channels; c++) {
2404
+ const data = buffer.getChannelData(c);
2405
+ for (let i = 0; i < frames; i++) data[i] = (samples[i * channels + c] ?? 0) / 32768;
2406
+ }
2407
+ const source = audio.createBufferSource();
2408
+ source.buffer = buffer;
2409
+ source.connect(audio.destination);
2410
+ source.onended = () => ended(source);
2411
+ const at = Math.max(nextStart, audio.currentTime + PLAYBACK_LEAD_SECONDS);
2412
+ source.start(at);
2413
+ nextStart = at + buffer.duration;
2414
+ sources.add(source);
2415
+ last = source;
2416
+ },
2417
+ after(fn) {
2418
+ if (!last) {
2419
+ run(fn);
2420
+ return;
2421
+ }
2422
+ waiting.set(last, [...waiting.get(last) ?? [], fn]);
2423
+ },
2424
+ stop() {
2425
+ for (const source of sources) {
2426
+ source.onended = null;
2427
+ try {
2428
+ source.stop();
2429
+ } catch {
2430
+ }
2431
+ source.disconnect();
2432
+ }
2433
+ sources.clear();
2434
+ last = null;
2435
+ nextStart = 0;
2436
+ const callbacks = [...waiting.values()].flat();
2437
+ waiting.clear();
2438
+ callbacks.forEach(run);
2439
+ },
2440
+ get playing() {
2441
+ return sources.size > 0;
2442
+ }
2443
+ };
2444
+ }
2445
+ var SPEAKER_BUSY_CODES = ["grant_concurrency", "grant_rate_limited", "voice_busy"];
2446
+ var SPEAKER_BUSY_RETRIES = 3;
2447
+ var SPEAKER_BUSY_PAUSE_MS = 700;
2448
+ function busy(result) {
2449
+ return result.status === "failed" && !!result.error && SPEAKER_BUSY_CODES.includes(result.error);
2450
+ }
2451
+ function _createSpeaker(options, seams) {
2452
+ const { sink } = seams;
2453
+ const pause = seams.pause ?? ((ms) => new Promise((resolve) => {
2454
+ setTimeout(resolve, ms);
2455
+ }));
2456
+ const max = Number.isSafeInteger(options.maxCharacters) && options.maxCharacters > 0 ? options.maxCharacters : SPEAKER_DEFAULT_MAX_CHARACTERS;
2457
+ const queue = [];
2458
+ const outstanding = /* @__PURE__ */ new Set();
2459
+ let held = "";
2460
+ let nextIndex = 0;
2461
+ let epoch = 0;
2462
+ let live = 0;
2463
+ let pumping = false;
2464
+ let inflight = null;
2465
+ const report = (line, result) => {
2466
+ if (line.epoch === epoch) live--;
2467
+ run(() => options.onLineEnd?.({ text: line.text, index: line.index, status: result.status, ...result.error ? { error: result.error } : {} }));
2468
+ line.finished(result);
2469
+ };
2470
+ const speakLine = async (line) => {
2471
+ const controller = new AbortController();
2472
+ inflight = controller;
2473
+ let format = { sampleRate: VOICE_SPEECH_SAMPLE_RATE, channels: VOICE_SPEECH_CHANNELS };
2474
+ let begun = false;
2475
+ const attempt = () => seams.speak({
2476
+ grantId: options.grantId,
2477
+ voice: options.voice,
2478
+ text: line.text,
2479
+ signal: controller.signal,
2480
+ ...options.tier !== void 0 ? { tier: options.tier } : {},
2481
+ ...options.style !== void 0 ? { style: options.style } : {},
2482
+ ...options.language !== void 0 ? { language: options.language } : {},
2483
+ onStart: (info) => {
2484
+ format = info;
2485
+ },
2486
+ onAudio: (pcm) => {
2487
+ if (controller.signal.aborted) return;
2488
+ if (!begun) {
2489
+ begun = true;
2490
+ sink.after(() => {
2491
+ if (line.epoch === epoch) options.onLineStart?.({ text: line.text, index: line.index });
2492
+ });
2493
+ }
2494
+ sink.play(pcm, format.sampleRate, format.channels);
2495
+ }
2496
+ }).catch(() => _spokenResult({ status: "failed", error: "voice_unavailable" }));
2497
+ let result = await attempt();
2498
+ for (let retry = 0; retry < SPEAKER_BUSY_RETRIES && busy(result) && !controller.signal.aborted; retry++) {
2499
+ await pause(SPEAKER_BUSY_PAUSE_MS);
2500
+ if (!controller.signal.aborted) result = await attempt();
2501
+ }
2502
+ inflight = null;
2503
+ if (controller.signal.aborted) report(line, result);
2504
+ else sink.after(() => report(line, result));
2505
+ };
2506
+ const pump = async () => {
2507
+ if (pumping) return;
2508
+ pumping = true;
2509
+ for (let line = queue.shift(); line; line = queue.shift()) {
2510
+ if (line.epoch === epoch) await speakLine(line);
2511
+ else report(line, _spokenResult({ status: "canceled", error: "game_stopped" }));
2512
+ }
2513
+ pumping = false;
2514
+ };
2515
+ const enqueue = (text) => {
2516
+ const line = new Promise((finished) => {
2517
+ queue.push({ text, index: nextIndex++, epoch, finished });
2518
+ });
2519
+ live++;
2520
+ outstanding.add(line);
2521
+ void line.then(() => outstanding.delete(line));
2522
+ void pump();
2523
+ return line;
2524
+ };
2525
+ const enqueueLine = (text) => splitLine(text, max).map(enqueue);
2526
+ sink.prime();
2527
+ return {
2528
+ get speaking() {
2529
+ return live > 0 || sink.playing;
2530
+ },
2531
+ say(text) {
2532
+ sink.prime();
2533
+ const parts = typeof text === "string" ? enqueueLine(text) : [];
2534
+ if (!parts.length) return Promise.resolve(_spokenResult({ status: "failed", error: "invalid_request" }));
2535
+ return Promise.all(parts).then((results) => results.find((result) => result.status !== "succeeded") ?? results.at(-1));
2536
+ },
2537
+ feed(delta) {
2538
+ if (typeof delta !== "string") return;
2539
+ const { sentences, rest } = takeSentences(held + delta);
2540
+ const overflow = spill(rest, max);
2541
+ held = overflow.rest;
2542
+ for (const line of [...sentences, ...overflow.lines]) enqueueLine(line);
2543
+ },
2544
+ async end() {
2545
+ const rest = held;
2546
+ held = "";
2547
+ enqueueLine(rest);
2548
+ await Promise.all([...outstanding]);
2549
+ },
2550
+ stop() {
2551
+ epoch++;
2552
+ live = 0;
2553
+ held = "";
2554
+ inflight?.abort();
2555
+ sink.stop();
2556
+ }
2557
+ };
2558
+ }
2559
+ function createSpeaker(options) {
2560
+ return _createSpeaker(options, { speak, sink: createWebAudioSink() });
2561
+ }
2562
+
1851
2563
  // src/index.ts
1852
2564
  var PROTOCOL_VERSION = 1;
1853
2565
  var RETRY_FLAG = "genex:embed:retry";
@@ -1856,7 +2568,7 @@ var LOCAL_AUTH_FLAG = "genex:embed:local-auth";
1856
2568
  var ENTRY_REFERRER_MEMO = "genex:embed:entry-referrer";
1857
2569
  var ENTRY_REFERRER_NONE = "none";
1858
2570
  var PLAYER_ID_KEY = "genex:player";
1859
- var SDK_VERSION = "0.37.0";
2571
+ var SDK_VERSION = "0.38.0";
1860
2572
  var config2 = null;
1861
2573
  var state = "pending";
1862
2574
  var user = null;
@@ -3468,6 +4180,11 @@ export {
3468
4180
  generateStream,
3469
4181
  judge,
3470
4182
  generationErrorMessage,
4183
+ getVoiceCatalog,
4184
+ transcribe,
4185
+ speak,
4186
+ createVoiceRecorder,
4187
+ createSpeaker,
3471
4188
  initEmbed,
3472
4189
  isEmbedded,
3473
4190
  getEntry,