@genex-ai/embed-sdk 0.36.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,26 +13,44 @@ 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,
27
+ foldRuntimeConversation,
18
28
  hasNativeMarker,
19
29
  isGenerationId,
20
30
  isGenerationTerminal,
31
+ isLoopbackHttpOrigin,
21
32
  isPlatformUrl,
22
33
  isSaveName,
34
+ isVoiceLanguage,
35
+ isVoiceTier,
23
36
  parseGenerationStreamEvent,
24
37
  parsePlatformRequest,
25
38
  parsePlatformResult,
39
+ parseSpeechStreamEvent,
40
+ parseWavPcm16,
41
+ practiceOrigin,
26
42
  readBridgeNativeCommand,
27
43
  readNativeEntry,
28
44
  readPlatformCaps,
45
+ readPracticeKey,
46
+ runtimeConversationError,
29
47
  sanitizeEntryHash,
30
48
  sanitizeEntryReferrer,
31
49
  sanitizePassthroughQuery,
32
50
  savableMime,
33
51
  stripNativeEntry,
34
52
  stripReturnMarker
35
- } from "./chunk-QBCWKMVQ.js";
53
+ } from "./chunk-YQY2WLAJ.js";
36
54
 
37
55
  // src/platform.ts
38
56
  var ctx = null;
@@ -65,9 +83,9 @@ function localTest() {
65
83
  function fail(error) {
66
84
  return { ok: false, error };
67
85
  }
68
- async function settled(run, fallback) {
86
+ async function settled(run2, fallback) {
69
87
  try {
70
- return await run();
88
+ return await run2();
71
89
  } catch {
72
90
  return fallback;
73
91
  }
@@ -894,6 +912,26 @@ function createSseParser(onEvent) {
894
912
  }
895
913
  };
896
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
+ }
897
935
  function firstStringProperty(schema) {
898
936
  if (!schema || typeof schema !== "object") return void 0;
899
937
  const properties = schema.properties;
@@ -973,25 +1011,52 @@ function partialStringField(text, name) {
973
1011
  return void 0;
974
1012
  }
975
1013
 
1014
+ // src/practice.ts
1015
+ function _readPracticeTarget() {
1016
+ if (typeof window === "undefined") return null;
1017
+ try {
1018
+ const location = window.location;
1019
+ if (!location || !isLoopbackHttpOrigin(location.origin)) return null;
1020
+ const key = readPracticeKey(location.search);
1021
+ return key ? { origin: practiceOrigin(key.port), token: key.token } : null;
1022
+ } catch {
1023
+ return null;
1024
+ }
1025
+ }
1026
+
976
1027
  // src/generation.ts
977
1028
  var config = null;
1029
+ var practiceTarget = null;
978
1030
  function _initGeneration(value) {
979
1031
  config = value;
1032
+ messagesFeature = void 0;
1033
+ practiceTarget = _readPracticeTarget();
980
1034
  }
981
1035
  function must2() {
982
1036
  if (!config) throw new Error("genex generation: call initEmbed() first");
983
1037
  return config;
984
1038
  }
985
- async function request(path, body) {
1039
+ function practice() {
986
1040
  const c = must2();
987
- const token = c.getToken();
1041
+ return c.isLocalTest() && !c.isEmbedded() && !c.isNative() ? practiceTarget : null;
1042
+ }
1043
+ function endpoint() {
1044
+ const target = practice();
1045
+ return target ? { base: target.origin, token: target.token } : { base: must2().apiUrl, token: must2().getToken() };
1046
+ }
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 = {}) {
1051
+ const { base, token } = endpoint();
988
1052
  if (!token) throw new Error("no_player_session");
989
- const response = await fetch(`${c.apiUrl}/api/runtime${path}`, {
990
- method: body === void 0 ? "GET" : "POST",
1053
+ const response = await fetch(`${base}/api/runtime${path}`, {
1054
+ method: init.body === void 0 ? "GET" : "POST",
991
1055
  credentials: "omit",
992
1056
  cache: "no-store",
993
- headers: { Authorization: `Bearer ${token}`, ...body === void 0 ? {} : { "Content-Type": "application/json" } },
994
- ...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 } : {}
995
1060
  });
996
1061
  const result = await response.json().catch(() => ({}));
997
1062
  if (!response.ok) {
@@ -1019,6 +1084,37 @@ var GENERATION_WAIT_MS = 12e5;
1019
1084
  function getGenerationModels() {
1020
1085
  return request("/models");
1021
1086
  }
1087
+ var RICH_FIELDS = ["system", "messages", "temperature", "topP", "stop", "seed", "reasoningEffort", "cacheSystem"];
1088
+ var FEATURE_RECHECK_MS = 3e5;
1089
+ var messagesFeature;
1090
+ function standTakesMessages() {
1091
+ const known = messagesFeature;
1092
+ if (known && (known.yes || Date.now() - known.at < FEATURE_RECHECK_MS)) return known.answer;
1093
+ const asked = { at: Date.now(), yes: false, answer: Promise.resolve(false) };
1094
+ asked.answer = getGenerationModels().then(
1095
+ (models) => asked.yes = models.features?.messages === true,
1096
+ () => {
1097
+ if (messagesFeature === asked) messagesFeature = void 0;
1098
+ return false;
1099
+ }
1100
+ );
1101
+ messagesFeature = asked;
1102
+ return asked.answer;
1103
+ }
1104
+ function hasRichFields(body) {
1105
+ return RICH_FIELDS.some((key) => body[key] !== void 0);
1106
+ }
1107
+ function foldGenerationRequest(request2) {
1108
+ return foldRuntimeConversation(request2);
1109
+ }
1110
+ async function wireBody(body) {
1111
+ if (!hasRichFields(body) || await standTakesMessages()) return body;
1112
+ const { system, messages, temperature: _temperature, topP: _topP, stop: _stop, seed: _seed, reasoningEffort: _effort, cacheSystem: _cache, ...rest } = body;
1113
+ return { ...rest, prompt: foldRuntimeConversation({ system, messages, prompt: body.prompt }) };
1114
+ }
1115
+ function invalidConversation(options) {
1116
+ return hasRichFields(options) ? runtimeConversationError(options) : void 0;
1117
+ }
1022
1118
  function isWorkflowIdentifier(value) {
1023
1119
  return typeof value === "string" && /^[A-Za-z0-9_.:-]{1,120}$/.test(value) && value !== "." && value !== "..";
1024
1120
  }
@@ -1041,7 +1137,8 @@ function outcome(view) {
1041
1137
  source: view.source,
1042
1138
  ...view.modelProvenance ? { modelProvenance: view.modelProvenance } : {}
1043
1139
  } : {},
1044
- ...view.error ? { error: view.error } : {}
1140
+ ...view.error ? { error: view.error } : {},
1141
+ ...view.contentBlocked ? { contentBlocked: view.contentBlocked } : {}
1045
1142
  };
1046
1143
  }
1047
1144
  function billingReceipt(view) {
@@ -1095,6 +1192,8 @@ async function generate(options) {
1095
1192
  const ceiling = ceilingField(options);
1096
1193
  if ("error" in ceiling) return { status: "failed", error: ceiling.error };
1097
1194
  if (!validMaxOutputTokens(options.maxOutputTokens)) return { status: "failed", error: "invalid_max_output_tokens" };
1195
+ const malformed = invalidConversation(options);
1196
+ if (malformed) return { status: "failed", error: malformed };
1098
1197
  const { timeoutMs, idempotencyKey, grantId, maxCredits: _credits, estimateCoins: _coins, ...rest } = options;
1099
1198
  const input = { ...rest, ...ceiling.field };
1100
1199
  if (grantId !== void 0) return generateUnderGrant(grantId, input, idempotencyKey, timeoutMs ?? GENERATION_WAIT_MS);
@@ -1124,7 +1223,7 @@ async function resumeWorkflow(generationId, options = {}) {
1124
1223
  }
1125
1224
  function approvalGuards() {
1126
1225
  const c = must2();
1127
- if (c.isLocalTest()) return { status: "failed", error: "local_test_unsupported" };
1226
+ if (c.isLocalTest()) return practice() ? { parentOrigin: null, practice: true } : { status: "failed", error: "local_test_unsupported" };
1128
1227
  if (c.isNative()) return { status: "failed", error: "native_unsupported" };
1129
1228
  if (typeof window === "undefined") return { status: "failed", error: "no_window" };
1130
1229
  if (!c.getToken()) return { status: "failed", error: "no_player_session" };
@@ -1137,6 +1236,7 @@ function approvalGuards() {
1137
1236
  function reserveApprovalSurface(placeholder) {
1138
1237
  const guarded = approvalGuards();
1139
1238
  if ("status" in guarded) return guarded;
1239
+ if (guarded.practice) return { popup: null, parentOrigin: null };
1140
1240
  const c = must2();
1141
1241
  if (c.isEmbedded()) return { popup: null, parentOrigin: guarded.parentOrigin };
1142
1242
  let dashboard;
@@ -1159,7 +1259,7 @@ async function startGeneration(path, body, timeoutMs, readOutcome, workflow, res
1159
1259
  if ("status" in surface) return surface;
1160
1260
  const { popup, dashboard, parentOrigin: parentOrigin2 } = surface;
1161
1261
  try {
1162
- const view = resumeId ? await getGeneration(resumeId) : await request(path, { ...body, idempotencyKey: body.idempotencyKey ?? crypto.randomUUID() });
1262
+ const view = resumeId ? await getGeneration(resumeId) : await request(path, await wireBody({ ...body, idempotencyKey: body.idempotencyKey ?? crypto.randomUUID() }));
1163
1263
  if (resumeId && (view.id !== resumeId || view.kind !== "workflow")) throw new Error("invalid_generation_response");
1164
1264
  if (!isGenerationId(view.id) || workflow && (view.kind !== "workflow" || view.workflow?.id !== workflow.workflowId || view.workflow.offeringId !== workflow.offeringId)) {
1165
1265
  throw new Error("invalid_generation_response");
@@ -1193,7 +1293,10 @@ function validCount(value, min, max) {
1193
1293
  function invalidGrantOptions(options) {
1194
1294
  const judge2 = options.judge;
1195
1295
  if (judge2 !== void 0 && (typeof judge2 !== "object" || judge2 === null || typeof judge2.enabled !== "boolean" || !validCount(judge2.estimatedCallsPerPeriod, 1, 1e5))) return "invalid_judge_terms";
1196
- 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";
1197
1300
  const max = options.perCallMaxCredits ?? options.perCallMaxCoins;
1198
1301
  const estimate = options.perCallEstimateCredits ?? options.perCallEstimateCoins;
1199
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";
@@ -1204,6 +1307,7 @@ function invalidGrantOptions(options) {
1204
1307
  if (options.maxCallsPerMinute !== void 0 && !validCount(options.maxCallsPerMinute, 1, 120)) return "invalid_grant_terms";
1205
1308
  if (options.maxPendingExternal !== void 0 && !validCount(options.maxPendingExternal, 1, 8)) return "invalid_grant_terms";
1206
1309
  if (options.allowExternal !== void 0 && typeof options.allowExternal !== "boolean") return "invalid_grant_terms";
1310
+ if (options.shareWithRooms !== void 0 && typeof options.shareWithRooms !== "boolean") return "invalid_grant_terms";
1207
1311
  if (options.idempotencyKey !== void 0 && (typeof options.idempotencyKey !== "string" || options.idempotencyKey.length < 8 || options.idempotencyKey.length > 100)) return "invalid_idempotency_key";
1208
1312
  return void 0;
1209
1313
  }
@@ -1242,6 +1346,37 @@ function stopSpendGrant(grantId) {
1242
1346
  if (!isGrantId(grantId)) return Promise.reject(new Error("invalid_grant_id"));
1243
1347
  return request(`/grants/${encodeURIComponent(grantId)}/stop`, {});
1244
1348
  }
1349
+ async function roomCallsServed() {
1350
+ const models = await getGenerationModels();
1351
+ return models.features?.roomCalls === true;
1352
+ }
1353
+ function isRoomId(value) {
1354
+ return typeof value === "string" && /^[A-Za-z0-9_-]{1,64}$/.test(value);
1355
+ }
1356
+ function roomDoorError(error) {
1357
+ const status = error.status;
1358
+ const code = error instanceof Error ? error.message : "";
1359
+ if (status === 404 && (code === "generation_unavailable" || code === "not_found")) return new Error("room_calls_unavailable");
1360
+ return error instanceof Error ? error : new Error("generation_unavailable");
1361
+ }
1362
+ async function lendSpendGrantToRoom(grantId, roomId) {
1363
+ if (!isGrantId(grantId)) throw new Error("invalid_grant_id");
1364
+ if (!isRoomId(roomId)) throw new Error("invalid_room_id");
1365
+ try {
1366
+ return await request(`/grants/${encodeURIComponent(grantId)}/rooms`, { roomId });
1367
+ } catch (error) {
1368
+ throw roomDoorError(error);
1369
+ }
1370
+ }
1371
+ async function returnSpendGrantFromRoom(grantId, roomId) {
1372
+ if (!isGrantId(grantId)) throw new Error("invalid_grant_id");
1373
+ if (!isRoomId(roomId)) throw new Error("invalid_room_id");
1374
+ try {
1375
+ return await request(`/grants/${encodeURIComponent(grantId)}/rooms/${encodeURIComponent(roomId)}/end`, {});
1376
+ } catch (error) {
1377
+ throw roomDoorError(error);
1378
+ }
1379
+ }
1245
1380
  async function pollGrant(grantId, timeoutMs, popup) {
1246
1381
  const deadline = Date.now() + timeoutMs;
1247
1382
  while (Date.now() < deadline) {
@@ -1261,12 +1396,17 @@ async function pollGrant(grantId, timeoutMs, popup) {
1261
1396
  return { status: "pending", grantId, error: "wait_timeout" };
1262
1397
  }
1263
1398
  async function requestSpendGrant(options) {
1399
+ if (options.sponsored === true) return requestSponsoredGrant(options);
1264
1400
  const invalid = invalidGrantOptions(options);
1265
1401
  if (invalid) return { status: "failed", error: invalid };
1266
1402
  const surface = reserveApprovalSurface("/play/grant");
1267
1403
  if ("status" in surface) return surface;
1268
1404
  const { popup, dashboard, parentOrigin: parentOrigin2 } = surface;
1269
1405
  try {
1406
+ if (options.shareWithRooms === true && !await roomCallsServed()) {
1407
+ popup?.close();
1408
+ return { status: "failed", error: "room_calls_unavailable" };
1409
+ }
1270
1410
  const estimate = options.perCallEstimateCredits ?? options.perCallEstimateCoins;
1271
1411
  const view = await request("/grants", {
1272
1412
  idempotencyKey: options.idempotencyKey ?? crypto.randomUUID(),
@@ -1282,7 +1422,9 @@ async function requestSpendGrant(options) {
1282
1422
  ...options.maxCallsPerMinute !== void 0 ? { maxCallsPerMinute: options.maxCallsPerMinute } : {},
1283
1423
  ...options.maxPendingExternal !== void 0 ? { maxPendingExternal: options.maxPendingExternal } : {},
1284
1424
  ...options.allowExternal !== void 0 ? { allowExternal: options.allowExternal } : {},
1285
- ...options.judge !== void 0 ? { judge: { enabled: options.judge.enabled, estimatedCallsPerPeriod: options.judge.estimatedCallsPerPeriod } } : {}
1425
+ ...options.judge !== void 0 ? { judge: { enabled: options.judge.enabled, estimatedCallsPerPeriod: options.judge.estimatedCallsPerPeriod } } : {},
1426
+ ...options.voice !== void 0 ? { voice: options.voice } : {},
1427
+ ...options.shareWithRooms === true ? { shareWithRooms: true } : {}
1286
1428
  });
1287
1429
  if (!isGrantId(view.id)) throw new Error("invalid_grant_response");
1288
1430
  const settled2 = grantOutcome(view);
@@ -1302,6 +1444,56 @@ async function requestSpendGrant(options) {
1302
1444
  return { status: "failed", error: error instanceof Error ? error.message : "grant_unavailable" };
1303
1445
  }
1304
1446
  }
1447
+ var FEATURES_TTL_MS = 3e5;
1448
+ var knownFeatures;
1449
+ function runtimeFeatures() {
1450
+ const apiUrl = must2().apiUrl;
1451
+ if (!knownFeatures || knownFeatures.apiUrl !== apiUrl || Date.now() - knownFeatures.at > FEATURES_TTL_MS) {
1452
+ const value = getGenerationModels().then((models) => models.features ?? {}, () => null);
1453
+ const entry = { at: Date.now(), apiUrl, value };
1454
+ knownFeatures = entry;
1455
+ void value.then((read) => {
1456
+ if (read === null && knownFeatures === entry) knownFeatures = void 0;
1457
+ });
1458
+ }
1459
+ return knownFeatures.value;
1460
+ }
1461
+ function guestToken(token) {
1462
+ try {
1463
+ const payload = token.split(".")[1];
1464
+ if (!payload) return false;
1465
+ return JSON.parse(atob(payload.replace(/-/g, "+").replace(/_/g, "/"))).guest === true;
1466
+ } catch {
1467
+ return false;
1468
+ }
1469
+ }
1470
+ var unavailable = (error) => ({ status: "unavailable", error });
1471
+ async function requestSponsoredGrant(options) {
1472
+ const judge2 = options.judge;
1473
+ if (judge2 !== void 0 && (typeof judge2 !== "object" || judge2 === null || typeof judge2.enabled !== "boolean")) return { status: "failed", error: "invalid_judge_terms" };
1474
+ 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 { status: "failed", error: "invalid_models" };
1475
+ const max = options.perCallMaxCredits ?? options.perCallMaxCoins;
1476
+ if (!validCount(max, 1, 1e6)) return { status: "failed", error: "invalid_declared_price" };
1477
+ if (options.maxCallsPerMinute !== void 0 && !validCount(options.maxCallsPerMinute, 1, 120)) return { status: "failed", error: "invalid_grant_terms" };
1478
+ const guarded = approvalGuards();
1479
+ if ("status" in guarded) return unavailable(guarded.error);
1480
+ const features = await runtimeFeatures();
1481
+ if (!features?.sponsor) return unavailable("sponsor_not_available");
1482
+ if (!features.sponsorGuests && guestToken(must2().getToken() ?? "")) return unavailable("sponsor_sign_in_required");
1483
+ try {
1484
+ const view = await request("/sponsor/grants", {
1485
+ models: options.models,
1486
+ perCallMaxCredits: max,
1487
+ ...judge2?.enabled ? { judge: true } : {},
1488
+ ...options.maxCallsPerMinute !== void 0 ? { maxCallsPerMinute: options.maxCallsPerMinute } : {}
1489
+ });
1490
+ if (!isGrantId(view.id) || view.sponsored !== true || !LIVE_GRANT_STATUSES.includes(view.status)) return unavailable("sponsor_not_available");
1491
+ return { status: "active", grantId: view.id, grant: view };
1492
+ } catch (error) {
1493
+ const code = error instanceof Error ? error.message : "";
1494
+ return unavailable(/^[a-z][a-z0-9_]{2,63}$/.test(code) && code !== "not_found" && code !== "generation_unavailable" ? code : "sponsor_not_available");
1495
+ }
1496
+ }
1305
1497
  async function generateUnderGrant(grantId, input, idempotencyKey, timeoutMs) {
1306
1498
  if (!isGrantId(grantId)) return { status: "failed", error: "invalid_grant_id" };
1307
1499
  const guarded = approvalGuards();
@@ -1310,7 +1502,7 @@ async function generateUnderGrant(grantId, input, idempotencyKey, timeoutMs) {
1310
1502
  try {
1311
1503
  const view = await request(
1312
1504
  `/grants/${encodeURIComponent(grantId)}/generations`,
1313
- { ...body, idempotencyKey: idempotencyKey ?? crypto.randomUUID() }
1505
+ await wireBody({ ...body, idempotencyKey: idempotencyKey ?? crypto.randomUUID() })
1314
1506
  );
1315
1507
  if (!isGenerationId(view.id) || view.grantId !== grantId) throw new Error("invalid_generation_response");
1316
1508
  const result = outcome(view);
@@ -1320,17 +1512,36 @@ async function generateUnderGrant(grantId, input, idempotencyKey, timeoutMs) {
1320
1512
  return { status: "failed", error: error instanceof Error ? error.message : "generation_unavailable" };
1321
1513
  }
1322
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
+ }
1323
1533
  async function generateStream(options) {
1324
1534
  const ceiling = ceilingField(options);
1325
1535
  if ("error" in ceiling) return { status: "failed", error: ceiling.error };
1326
1536
  if (!validMaxOutputTokens(options.maxOutputTokens)) return { status: "failed", error: "invalid_max_output_tokens" };
1537
+ const malformed = invalidConversation(options);
1538
+ if (malformed) return { status: "failed", error: malformed };
1327
1539
  const { timeoutMs, idempotencyKey, grantId, onDelta, onPartialField, signal, allowExternal: _unused, maxCredits: _credits, estimateCoins: _coins, ...rest } = options;
1328
1540
  const body = { ...rest, ...ceiling.field };
1329
1541
  if (grantId === void 0 || grantId === null) return { status: "failed", error: "stream_requires_grant" };
1330
1542
  if (!isGrantId(grantId)) return { status: "failed", error: "invalid_grant_id" };
1331
1543
  const guarded = approvalGuards();
1332
1544
  if ("status" in guarded) return guarded;
1333
- const c = must2();
1334
1545
  const field = body.outputFormat === "json" && onPartialField ? firstStringProperty(body.schema) : void 0;
1335
1546
  let generationId;
1336
1547
  let soFar = "";
@@ -1364,46 +1575,22 @@ async function generateStream(options) {
1364
1575
  };
1365
1576
  const aborted = () => ({ status: "pending", ...generationId ? { generationId } : {}, error: "stream_aborted" });
1366
1577
  try {
1367
- const token = c.getToken();
1368
- if (!token) return { status: "failed", error: "no_player_session" };
1369
- const response = await fetch(`${c.apiUrl}/api/runtime/grants/${encodeURIComponent(grantId)}/generations/stream`, {
1370
- method: "POST",
1371
- credentials: "omit",
1372
- cache: "no-store",
1373
- headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json", Accept: "text/event-stream" },
1374
- body: JSON.stringify({ ...body, idempotencyKey: idempotencyKey ?? crypto.randomUUID() }),
1375
- ...signal ? { signal } : {}
1376
- });
1377
- if (!response.ok) {
1378
- const refusal = await response.json().catch(() => ({}));
1379
- return { status: "failed", error: typeof refusal.error === "string" ? refusal.error : "generation_unavailable" };
1380
- }
1381
- 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 };
1382
1584
  let done;
1383
- const parser = createSseParser(({ event, data }) => {
1384
- const parsed = parseGenerationStreamEvent(event, data);
1385
- if (!parsed || done) return;
1386
- if (parsed.event === "start") generationId ??= parsed.data.generationId;
1387
- else if (parsed.event === "delta") deliver(parsed.data.text);
1388
- else done = parsed.data;
1389
- });
1390
- const reader = response.body.getReader();
1391
- const decoder = new TextDecoder();
1392
1585
  try {
1393
- for (; ; ) {
1394
- const { value, done: ended } = await reader.read();
1395
- if (ended) break;
1396
- parser.push(decoder.decode(value, { stream: true }));
1397
- if (done) {
1398
- reader.cancel().catch(() => {
1399
- });
1400
- break;
1401
- }
1402
- }
1403
- if (!done) {
1404
- parser.push(decoder.decode());
1405
- parser.end();
1406
- }
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
+ });
1407
1594
  } catch {
1408
1595
  if (signal?.aborted) return aborted();
1409
1596
  }
@@ -1465,7 +1652,8 @@ async function judge(options) {
1465
1652
  generationId: view.generationId,
1466
1653
  billing: view.billing,
1467
1654
  ...view.answers ? { answers: view.answers } : {},
1468
- ...view.error ? { error: view.error } : {}
1655
+ ...view.error ? { error: view.error } : {},
1656
+ ...view.source ? { source: view.source } : {}
1469
1657
  };
1470
1658
  } catch (error) {
1471
1659
  return { status: "failed", error: error instanceof Error ? error.message : "judge_unavailable" };
@@ -1511,10 +1699,38 @@ function generationErrorMessage(code) {
1511
1699
  return "Requests are coming too quickly for this budget. Wait a moment and try again.";
1512
1700
  case "grant_already_active":
1513
1701
  return "A budget is already running here. Stop it on Genex before setting up another.";
1702
+ // --- lending a budget to a multiplayer room -------------------------------
1703
+ case "room_calls_unavailable":
1704
+ return "Shared AI in multiplayer rooms isn\u2019t available here yet.";
1705
+ case "grant_not_shareable":
1706
+ return "This budget wasn\u2019t approved for the game\u2019s multiplayer server. Set up a new one that says so.";
1707
+ case "room_not_found":
1708
+ return "That multiplayer room isn\u2019t running any more.";
1709
+ case "room_funding_unsupported":
1710
+ return "A budget on your own Claude or ChatGPT plan can\u2019t be used by the game\u2019s multiplayer server. Use one paid from Genex credits.";
1711
+ case "lend_not_found":
1712
+ return "This budget isn\u2019t lent to that room.";
1514
1713
  case "external_grant_active":
1515
1714
  return "A budget on your own plan is already running. Stop it on Genex before setting up another.";
1516
1715
  case "invalid_grant_limit":
1517
1716
  return "That limit is outside the range Genex offers. Choose one on the slider.";
1717
+ // --- a budget the game's creator pays for --------------------------------
1718
+ case "sponsor_not_available":
1719
+ case "sponsor_terms_mismatch":
1720
+ return "This game isn\u2019t covering model use right now.";
1721
+ case "sponsor_exhausted":
1722
+ case "sponsor_stopped":
1723
+ case "sponsor_expired":
1724
+ return "What this game\u2019s creator set aside for model use has run out or ended. Nothing was charged to you.";
1725
+ case "sponsor_player_limit_reached":
1726
+ case "sponsor_address_limit_reached":
1727
+ return "You\u2019ve used what this game\u2019s creator covers for today. It starts again tomorrow.";
1728
+ case "sponsor_rate_limited":
1729
+ return "Requests are coming too quickly right now. Wait a moment and try again.";
1730
+ case "sponsor_sign_in_required":
1731
+ return "Sign in to Genex to use what this game\u2019s creator covers.";
1732
+ case "sponsor_email_unverified":
1733
+ return "Verify your email on Genex to use what this game\u2019s creator covers.";
1518
1734
  // --- the judge ------------------------------------------------------------
1519
1735
  case "judge_requires_grant":
1520
1736
  return "This game\u2019s quick checks run under a budget you approve. Set one up to keep going.";
@@ -1531,6 +1747,33 @@ function generationErrorMessage(code) {
1531
1747
  return "The game\u2019s quick checks are busy right now. Try again in a moment.";
1532
1748
  case "judge_paused":
1533
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.";
1534
1777
  // --- streamed replies -----------------------------------------------------
1535
1778
  case "stream_requires_grant":
1536
1779
  return "Live replies run under a budget you approve. Set one up to keep going.";
@@ -1619,6 +1862,8 @@ function generationErrorMessage(code) {
1619
1862
  case "invalid_request":
1620
1863
  case "invalid_schema":
1621
1864
  case "invalid_max_output_tokens":
1865
+ case "invalid_messages":
1866
+ case "unsupported_parameter":
1622
1867
  return "This request wasn\u2019t something Genex could accept.";
1623
1868
  case "generation_limit":
1624
1869
  return "As many requests are running as Genex allows at once. Wait for one to finish.";
@@ -1632,6 +1877,11 @@ function generationErrorMessage(code) {
1632
1877
  case "validation_failed":
1633
1878
  case "invalid_provider_response":
1634
1879
  return "The answer didn\u2019t match what this game asked for.";
1880
+ // One sentence for both sides (`contentBlocked` says which): a request
1881
+ // refused before the model ran is not charged, a withheld answer is, and
1882
+ // the Genex receipt is what says so — never this line.
1883
+ case "content_blocked":
1884
+ return "That isn\u2019t allowed here, so nothing was shown. Try saying it another way.";
1635
1885
  // TWO DIFFERENT ENDINGS, and the difference is money. A budget that was
1636
1886
  // stopped, expired or revoked cancels what it admitted but never
1637
1887
  // dispatched, at zero cost — that is the one case where "before it ran" is
@@ -1660,6 +1910,656 @@ function generationErrorMessage(code) {
1660
1910
  }
1661
1911
  }
1662
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
+
1663
2563
  // src/index.ts
1664
2564
  var PROTOCOL_VERSION = 1;
1665
2565
  var RETRY_FLAG = "genex:embed:retry";
@@ -1668,7 +2568,7 @@ var LOCAL_AUTH_FLAG = "genex:embed:local-auth";
1668
2568
  var ENTRY_REFERRER_MEMO = "genex:embed:entry-referrer";
1669
2569
  var ENTRY_REFERRER_NONE = "none";
1670
2570
  var PLAYER_ID_KEY = "genex:player";
1671
- var SDK_VERSION = "0.36.0";
2571
+ var SDK_VERSION = "0.38.0";
1672
2572
  var config2 = null;
1673
2573
  var state = "pending";
1674
2574
  var user = null;
@@ -3264,6 +4164,7 @@ export {
3264
4164
  getPayments,
3265
4165
  requestPayment,
3266
4166
  getGenerationModels,
4167
+ foldGenerationRequest,
3267
4168
  getWorkflowOfferings,
3268
4169
  getGeneration,
3269
4170
  waitForGeneration,
@@ -3273,10 +4174,17 @@ export {
3273
4174
  getSpendGrant,
3274
4175
  getActiveSpendGrant,
3275
4176
  stopSpendGrant,
4177
+ lendSpendGrantToRoom,
4178
+ returnSpendGrantFromRoom,
3276
4179
  requestSpendGrant,
3277
4180
  generateStream,
3278
4181
  judge,
3279
4182
  generationErrorMessage,
4183
+ getVoiceCatalog,
4184
+ transcribe,
4185
+ speak,
4186
+ createVoiceRecorder,
4187
+ createSpeaker,
3280
4188
  initEmbed,
3281
4189
  isEmbedded,
3282
4190
  getEntry,