@momoyu-ink/kit 0.6.1 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/lib.cjs CHANGED
@@ -31,6 +31,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
31
31
  // src/lib.ts
32
32
  var lib_exports = {};
33
33
  __export(lib_exports, {
34
+ DEBUG_EMPTY_PAGE: () => DEBUG_EMPTY_PAGE,
34
35
  OverlayParamsContext: () => OverlayParamsContext,
35
36
  PageParamsContext: () => PageParamsContext,
36
37
  StageContextProvider: () => StageContextProvider,
@@ -43,6 +44,7 @@ __export(lib_exports, {
43
44
  createStackNavigator: () => createStackNavigator,
44
45
  createStage: () => createStage,
45
46
  createStaticNavigation: () => createStaticNavigation,
47
+ ensureArchiveVariableDefaults: () => ensureArchiveVariableDefaults,
46
48
  executeNodeCommand: () => executeNodeCommand,
47
49
  executePluginCommand: () => executePluginCommand,
48
50
  fastForwardState: () => fastForwardState,
@@ -50,11 +52,16 @@ __export(lib_exports, {
50
52
  getNavigator: () => getNavigator,
51
53
  getSeekingType: () => getSeekingType,
52
54
  getStageSize: () => getStageSize,
55
+ getUiData: () => getUiData,
53
56
  globalEventListeners: () => globalEventListeners,
54
57
  globalRequestAnimationFrameListeners: () => globalRequestAnimationFrameListeners,
58
+ initGlobalVariableDefaults: () => initGlobalVariableDefaults,
59
+ initUiData: () => initUiData,
60
+ loadVariablesDocument: () => loadVariablesDocument,
55
61
  moyu: () => moyu_exports,
56
62
  nextLine: () => nextLine,
57
63
  registerAppStateAdapter: () => registerAppStateAdapter,
64
+ replaceUiData: () => replaceUiData,
58
65
  setDefaultAutoTailMs: () => setDefaultAutoTailMs,
59
66
  setWaiting: () => setWaiting,
60
67
  skipState: () => skipState,
@@ -81,6 +88,7 @@ __export(lib_exports, {
81
88
  useSkipCallback: () => useSkipCallback,
82
89
  useSoundEffect: () => useSoundEffect,
83
90
  useStageContext: () => useStageContext,
91
+ useUiData: () => useUiData,
84
92
  warpState: () => warpState
85
93
  });
86
94
  module.exports = __toCommonJS(lib_exports);
@@ -406,6 +414,259 @@ function createRoot(options) {
406
414
  };
407
415
  }
408
416
 
417
+ // src/variables.ts
418
+ var VARIABLES_DATA_ASSET_PATH = "assets:///data/variables.json";
419
+ function normalizeVariableDefinitions(value) {
420
+ if (!Array.isArray(value)) {
421
+ return [];
422
+ }
423
+ return value.flatMap((entry) => {
424
+ if (!entry || typeof entry !== "object") {
425
+ return [];
426
+ }
427
+ const definition = entry;
428
+ if (typeof definition.id !== "string" || definition.id.length === 0) {
429
+ return [];
430
+ }
431
+ return [{ id: definition.id, defaultValue: definition.defaultValue }];
432
+ });
433
+ }
434
+ async function loadVariablesDocument() {
435
+ let rawText;
436
+ try {
437
+ rawText = await executePluginCommand("system", {
438
+ subCommand: "readFile",
439
+ path: VARIABLES_DATA_ASSET_PATH
440
+ });
441
+ } catch {
442
+ return null;
443
+ }
444
+ let rawData;
445
+ try {
446
+ rawData = JSON.parse(rawText);
447
+ } catch (error) {
448
+ console.error("Failed to parse variable defaults data:", error);
449
+ return null;
450
+ }
451
+ if (!rawData || typeof rawData !== "object") {
452
+ return {
453
+ archive: [],
454
+ global: []
455
+ };
456
+ }
457
+ const document = rawData;
458
+ return {
459
+ archive: normalizeVariableDefinitions(document.archive),
460
+ global: normalizeVariableDefinitions(document.global)
461
+ };
462
+ }
463
+ function toVariableRecord(value) {
464
+ if (value instanceof Map) {
465
+ return Object.fromEntries(value);
466
+ }
467
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
468
+ return {};
469
+ }
470
+ return value;
471
+ }
472
+ async function ensureVariableDefaults(scope) {
473
+ const document = await loadVariablesDocument();
474
+ if (!document) {
475
+ return;
476
+ }
477
+ const definitions = document[scope];
478
+ if (definitions.length === 0) {
479
+ return;
480
+ }
481
+ const currentVariables = toVariableRecord(
482
+ await executePluginCommand(
483
+ "scenario",
484
+ scope === "global" ? { subCommand: "getPermanentVariables" } : { subCommand: "getVariables" }
485
+ )
486
+ );
487
+ const missingVariables = {};
488
+ for (const definition of definitions) {
489
+ if (!Object.hasOwn(currentVariables, definition.id)) {
490
+ missingVariables[definition.id] = definition.defaultValue;
491
+ }
492
+ }
493
+ if (Object.keys(missingVariables).length === 0) {
494
+ return;
495
+ }
496
+ await executePluginCommand(
497
+ "scenario",
498
+ scope === "global" ? {
499
+ subCommand: "setPermanentVariables",
500
+ variables: missingVariables
501
+ } : {
502
+ subCommand: "setVariables",
503
+ variables: missingVariables
504
+ }
505
+ );
506
+ reportRuntimeDebugVariablesSet(scope, missingVariables);
507
+ }
508
+ async function initGlobalVariableDefaults() {
509
+ await ensureVariableDefaults("global");
510
+ }
511
+ async function ensureArchiveVariableDefaults() {
512
+ await ensureVariableDefaults("archive");
513
+ }
514
+
515
+ // src/debug/variableMonitor.ts
516
+ var sendRuntimeDebugVariableMessage = null;
517
+ var allowlistCache = null;
518
+ var allowlistPromise = null;
519
+ var variableEmissionSuspended = false;
520
+ function createEmptyAllowlist() {
521
+ return {
522
+ archive: /* @__PURE__ */ new Set(),
523
+ global: /* @__PURE__ */ new Set()
524
+ };
525
+ }
526
+ function isJsonValue(value) {
527
+ if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
528
+ return true;
529
+ }
530
+ if (Array.isArray(value)) {
531
+ return value.every((item) => isJsonValue(item));
532
+ }
533
+ if (!value || typeof value !== "object") {
534
+ return false;
535
+ }
536
+ return Object.values(value).every((item) => isJsonValue(item));
537
+ }
538
+ async function loadRuntimeDebugVariableAllowlist() {
539
+ const document = await loadVariablesDocument();
540
+ if (!document) {
541
+ return createEmptyAllowlist();
542
+ }
543
+ return {
544
+ archive: new Set(document.archive.map((def) => def.id)),
545
+ global: new Set(document.global.map((def) => def.id))
546
+ };
547
+ }
548
+ function toJsonValueRecord(value) {
549
+ if (value instanceof Map) {
550
+ return Object.fromEntries(
551
+ Array.from(value.entries()).flatMap(([key, entryValue]) => {
552
+ if (typeof key !== "string" || !isJsonValue(entryValue)) {
553
+ return [];
554
+ }
555
+ return [[key, entryValue]];
556
+ })
557
+ );
558
+ }
559
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
560
+ return {};
561
+ }
562
+ return Object.fromEntries(
563
+ Object.entries(value).flatMap(([key, entryValue]) => {
564
+ if (!isJsonValue(entryValue)) {
565
+ return [];
566
+ }
567
+ return [[key, entryValue]];
568
+ })
569
+ );
570
+ }
571
+ function filterVariablesByAllowlist(scope, values, allowlist) {
572
+ const allowedIds = allowlist[scope];
573
+ if (allowedIds.size === 0) {
574
+ return {};
575
+ }
576
+ return Object.fromEntries(
577
+ Object.entries(values).flatMap(([key, value]) => allowedIds.has(key) ? [[key, value]] : [])
578
+ );
579
+ }
580
+ function hasRecordEntries(value) {
581
+ return !!value && Object.keys(value).length > 0;
582
+ }
583
+ async function ensureRuntimeDebugVariableAllowlist() {
584
+ if (allowlistCache) {
585
+ return allowlistCache;
586
+ }
587
+ if (!allowlistPromise) {
588
+ allowlistPromise = loadRuntimeDebugVariableAllowlist().then((allowlist) => {
589
+ allowlistCache = allowlist;
590
+ return allowlist;
591
+ }).finally(() => {
592
+ allowlistPromise = null;
593
+ });
594
+ }
595
+ return allowlistPromise;
596
+ }
597
+ async function emitRuntimeDebugVariablesChanged(scope, variables) {
598
+ const sender = sendRuntimeDebugVariableMessage;
599
+ if (!sender || variableEmissionSuspended) {
600
+ return;
601
+ }
602
+ const allowlist = await ensureRuntimeDebugVariableAllowlist();
603
+ if (!sendRuntimeDebugVariableMessage || sendRuntimeDebugVariableMessage !== sender || variableEmissionSuspended) {
604
+ return;
605
+ }
606
+ const filtered = filterVariablesByAllowlist(scope, variables, allowlist);
607
+ if (!hasRecordEntries(filtered)) {
608
+ return;
609
+ }
610
+ sender({
611
+ type: "variables:changed",
612
+ archiveSet: scope === "archive" ? filtered : void 0,
613
+ globalSet: scope === "global" ? filtered : void 0
614
+ });
615
+ }
616
+ function setRuntimeDebugVariableMessageSender(sender) {
617
+ sendRuntimeDebugVariableMessage = sender;
618
+ if (sender) {
619
+ void ensureRuntimeDebugVariableAllowlist();
620
+ }
621
+ }
622
+ function setRuntimeDebugVariableEmissionSuspended(suspended) {
623
+ variableEmissionSuspended = suspended;
624
+ }
625
+ function resetRuntimeDebugVariableMonitor() {
626
+ sendRuntimeDebugVariableMessage = null;
627
+ allowlistCache = null;
628
+ allowlistPromise = null;
629
+ variableEmissionSuspended = false;
630
+ }
631
+ function reportRuntimeDebugVariableSet(scope, key, value) {
632
+ if (key.length === 0) {
633
+ return;
634
+ }
635
+ void emitRuntimeDebugVariablesChanged(scope, {
636
+ [key]: value
637
+ });
638
+ }
639
+ function reportRuntimeDebugVariablesSet(scope, variables) {
640
+ if (Object.keys(variables).length === 0) {
641
+ return;
642
+ }
643
+ void emitRuntimeDebugVariablesChanged(scope, variables);
644
+ }
645
+ async function syncRuntimeDebugVariablesSnapshot(reason) {
646
+ const sender = sendRuntimeDebugVariableMessage;
647
+ if (!sender) {
648
+ return;
649
+ }
650
+ const [allowlist, archiveVariables2, globalVariables2] = await Promise.all([
651
+ ensureRuntimeDebugVariableAllowlist(),
652
+ executePluginCommand("scenario", {
653
+ subCommand: "getVariables"
654
+ }),
655
+ executePluginCommand("scenario", {
656
+ subCommand: "getPermanentVariables"
657
+ })
658
+ ]);
659
+ if (!sendRuntimeDebugVariableMessage || sendRuntimeDebugVariableMessage !== sender) {
660
+ return;
661
+ }
662
+ sender({
663
+ type: "variables:snapshot",
664
+ reason,
665
+ archive: filterVariablesByAllowlist("archive", toJsonValueRecord(archiveVariables2), allowlist),
666
+ global: filterVariablesByAllowlist("global", toJsonValueRecord(globalVariables2), allowlist)
667
+ });
668
+ }
669
+
409
670
  // src/sandbox.ts
410
671
  var JS_GLOBAL_KEYS = new Set(Object.getOwnPropertyNames(globalThis));
411
672
  var JS_GLOBAL_KEYS_EXCLUDED = /* @__PURE__ */ new Set(["window", "globalThis", "LOCAL", "ARCHIVE", "GLOBAL"]);
@@ -456,6 +717,7 @@ var archiveVariables = new Proxy(
456
717
  name: key,
457
718
  value
458
719
  });
720
+ reportRuntimeDebugVariableSet("archive", key, value);
459
721
  return true;
460
722
  }
461
723
  return false;
@@ -481,6 +743,7 @@ var globalVariables = new Proxy(
481
743
  key,
482
744
  value
483
745
  });
746
+ reportRuntimeDebugVariableSet("global", key, value);
484
747
  return true;
485
748
  }
486
749
  return false;
@@ -488,7 +751,7 @@ var globalVariables = new Proxy(
488
751
  }
489
752
  );
490
753
  function safeEval(code, sandbox) {
491
- const proxy4 = new Proxy(sandbox, {
754
+ const proxy5 = new Proxy(sandbox, {
492
755
  has(target, key) {
493
756
  if (typeof key === "string" && JS_GLOBAL_KEYS.has(key)) {
494
757
  return false;
@@ -496,7 +759,7 @@ function safeEval(code, sandbox) {
496
759
  return key in target || key === "LOCAL" || key === "ARCHIVE" || key === "GLOBAL" || key === "window" || key === "globalThis";
497
760
  },
498
761
  get(target, key) {
499
- if (key === "window" || key === "globalThis") return proxy4;
762
+ if (key === "window" || key === "globalThis") return proxy5;
500
763
  if (key === "LOCAL") return localVariables;
501
764
  if (key === "ARCHIVE") return archiveVariables;
502
765
  if (key === "GLOBAL") return globalVariables;
@@ -508,7 +771,7 @@ function safeEval(code, sandbox) {
508
771
  return true;
509
772
  }
510
773
  });
511
- return new Function("sandbox", `with(sandbox) { return (${code}) }`).bind(proxy4)(proxy4);
774
+ return new Function("sandbox", `with(sandbox) { return (${code}) }`).bind(proxy5)(proxy5);
512
775
  }
513
776
  globalThis.__moyu_eval_sandbox = (code) => {
514
777
  try {
@@ -695,6 +958,7 @@ var import_valtio = require("valtio");
695
958
  var import_jsx_runtime = require("react/jsx-runtime");
696
959
  var PageParamsContext = (0, import_react2.createContext)(void 0);
697
960
  var OverlayParamsContext = (0, import_react2.createContext)(void 0);
961
+ var DEBUG_EMPTY_PAGE = "debug-empty";
698
962
  var overlayIdCounter = 0;
699
963
  var generateOverlayId = () => {
700
964
  return `overlay-${++overlayIdCounter}-${Date.now()}`;
@@ -722,6 +986,12 @@ function createStackNavigator(options) {
722
986
  for (const [key, config] of Object.entries(options.pages)) {
723
987
  normalizedPages[key] = normalizeDescriptor(config);
724
988
  }
989
+ if (normalizedPages[DEBUG_EMPTY_PAGE]) {
990
+ throw new Error(`Navigation Error: Page name "${DEBUG_EMPTY_PAGE}" is reserved.`);
991
+ }
992
+ normalizedPages[DEBUG_EMPTY_PAGE] = {
993
+ component: () => null
994
+ };
725
995
  const normalizedOverlays = {};
726
996
  if (options.overlays) {
727
997
  for (const [key, config] of Object.entries(options.overlays)) {
@@ -751,6 +1021,9 @@ function createStackNavigator(options) {
751
1021
  hasPage: (page) => {
752
1022
  return normalizedPages[page] !== void 0;
753
1023
  },
1024
+ hasOverlay: (overlay) => {
1025
+ return normalizedOverlays[overlay] !== void 0;
1026
+ },
754
1027
  goBack: () => {
755
1028
  console.warn("goBack() not implemented yet");
756
1029
  },
@@ -838,6 +1111,7 @@ var debugState = (0, import_valtio2.proxy)({
838
1111
  active: false,
839
1112
  checkpoints: {},
840
1113
  currentCursor: null,
1114
+ currentStory: null,
841
1115
  restoring: false,
842
1116
  lastError: null
843
1117
  });
@@ -881,9 +1155,37 @@ function reportDebugSessionError(error) {
881
1155
  function clearLocalRuntimeState() {
882
1156
  debugState.checkpoints = {};
883
1157
  debugState.currentCursor = null;
1158
+ debugState.currentStory = null;
884
1159
  debugState.restoring = false;
885
1160
  debugState.lastError = null;
886
1161
  }
1162
+ async function restartJumpStoryFromHead(targetStory, currentStory) {
1163
+ debugState.checkpoints = {};
1164
+ debugState.currentCursor = null;
1165
+ debugState.currentStory = null;
1166
+ if (targetStory) {
1167
+ if (appStateAdapter?.restartStoryFromHead) {
1168
+ await appStateAdapter.restartStoryFromHead({
1169
+ story: targetStory,
1170
+ entry: "entry"
1171
+ });
1172
+ } else if (currentStory === targetStory && appStateAdapter?.restartCurrentStoryFromHead) {
1173
+ await appStateAdapter.restartCurrentStoryFromHead();
1174
+ } else {
1175
+ throw new Error("Restarting the target story from head is not supported by the app state adapter");
1176
+ }
1177
+ debugState.currentCursor = await executeScenarioCommand({
1178
+ subCommand: "getExecutionCursor"
1179
+ });
1180
+ debugState.currentStory = debugState.currentCursor?.story ?? targetStory;
1181
+ return;
1182
+ }
1183
+ if (!appStateAdapter?.restartCurrentStoryFromHead) {
1184
+ throw new Error("Restarting the current story from head is not supported by the app state adapter");
1185
+ }
1186
+ await appStateAdapter.restartCurrentStoryFromHead();
1187
+ debugState.currentCursor = null;
1188
+ }
887
1189
  async function doSettleActiveSeek(op, clearSlot, error) {
888
1190
  if (!op) {
889
1191
  return;
@@ -936,6 +1238,7 @@ async function syncLocalWarpCheckpoint(targetMarkerId) {
936
1238
  appState: appStateAdapter ? await appStateAdapter.capture() : void 0
937
1239
  };
938
1240
  debugState.currentCursor = checkpointCursor;
1241
+ debugState.currentStory = checkpointCursor.story;
939
1242
  debugState.checkpoints[targetMarkerId] = checkpoint;
940
1243
  await currentConfig.onMarkerEnter(checkpoint);
941
1244
  }
@@ -959,9 +1262,16 @@ function notifyMarkerEnter(checkpoint) {
959
1262
  reportDebugSessionError(error);
960
1263
  }
961
1264
  }
962
- async function prepareJumpStart(markerId) {
1265
+ async function prepareJumpStart(markerId, options) {
1266
+ const currentStory = debugState.currentStory ?? debugState.currentCursor?.story ?? null;
1267
+ const targetStory = options?.story;
1268
+ let restartedFromHead = false;
1269
+ if (targetStory && targetStory !== currentStory) {
1270
+ await restartJumpStoryFromHead(targetStory, currentStory);
1271
+ restartedFromHead = true;
1272
+ }
963
1273
  const checkpoint = debugState.checkpoints[markerId];
964
- if (checkpoint) {
1274
+ if (!restartedFromHead && checkpoint && (!targetStory || checkpoint.cursor.story === targetStory)) {
965
1275
  const restored2 = await executeScenarioCommand({
966
1276
  subCommand: "restoreCheckpoint",
967
1277
  key: markerId
@@ -973,6 +1283,7 @@ async function prepareJumpStart(markerId) {
973
1283
  await appStateAdapter.restore(checkpoint.appState);
974
1284
  }
975
1285
  debugState.currentCursor = checkpoint.cursor;
1286
+ debugState.currentStory = checkpoint.cursor.story;
976
1287
  return false;
977
1288
  }
978
1289
  const lookup = await executeScenarioCommand({
@@ -980,16 +1291,15 @@ async function prepareJumpStart(markerId) {
980
1291
  key: markerId
981
1292
  });
982
1293
  if (!lookup.markerExists) {
983
- throw new Error(`Marker ${markerId} is not available in the current story`);
1294
+ throw new Error(
1295
+ targetStory ? `Marker ${markerId} is not available in story ${targetStory}` : `Marker ${markerId} is not available in the current story`
1296
+ );
984
1297
  }
985
1298
  const startCheckpointKey = lookup.checkpointKey ?? null;
986
1299
  if (!startCheckpointKey) {
987
- if (!appStateAdapter?.restartCurrentStoryFromHead) {
988
- throw new Error("Restarting the current story from head is not supported by the app state adapter");
1300
+ if (!restartedFromHead) {
1301
+ await restartJumpStoryFromHead(targetStory, currentStory);
989
1302
  }
990
- await appStateAdapter.restartCurrentStoryFromHead();
991
- debugState.checkpoints = {};
992
- debugState.currentCursor = null;
993
1303
  return true;
994
1304
  }
995
1305
  const startCheckpoint = debugState.checkpoints[startCheckpointKey];
@@ -1007,6 +1317,7 @@ async function prepareJumpStart(markerId) {
1007
1317
  await appStateAdapter.restore(startCheckpoint.appState);
1008
1318
  }
1009
1319
  debugState.currentCursor = startCheckpoint.cursor;
1320
+ debugState.currentStory = startCheckpoint.cursor.story;
1010
1321
  return true;
1011
1322
  }
1012
1323
  function handleMarkerEnter(marker) {
@@ -1019,6 +1330,7 @@ function handleMarkerEnter(marker) {
1019
1330
  appState: captureCheckpointAppStateSync()
1020
1331
  };
1021
1332
  debugState.currentCursor = checkpoint.cursor;
1333
+ debugState.currentStory = checkpoint.cursor.story;
1022
1334
  debugState.checkpoints[marker.markerId] = checkpoint;
1023
1335
  notifyMarkerEnter(checkpoint);
1024
1336
  if (activeFastForward?.targetMarkerId === marker.markerId) {
@@ -1079,11 +1391,11 @@ async function runWarp(options) {
1079
1391
  await completion;
1080
1392
  }
1081
1393
  var debugSessionController = {
1082
- async restoreCheckpoint(markerId) {
1394
+ async restoreCheckpoint(markerId, options) {
1083
1395
  debugState.restoring = true;
1084
1396
  debugState.lastError = null;
1085
1397
  try {
1086
- const needsFastForward = await enqueueDebugOperation(async () => prepareJumpStart(markerId));
1398
+ const needsFastForward = await enqueueDebugOperation(async () => prepareJumpStart(markerId, options));
1087
1399
  if (needsFastForward) {
1088
1400
  await runFastForwardToMarker(markerId);
1089
1401
  }
@@ -1099,7 +1411,7 @@ var debugSessionController = {
1099
1411
  debugState.restoring = true;
1100
1412
  debugState.lastError = null;
1101
1413
  try {
1102
- const needsWarp = await enqueueDebugOperation(async () => prepareJumpStart(options.markerId));
1414
+ const needsWarp = await enqueueDebugOperation(async () => prepareJumpStart(options.markerId, options));
1103
1415
  if (needsWarp) {
1104
1416
  await runWarp(options);
1105
1417
  }
@@ -1110,14 +1422,26 @@ var debugSessionController = {
1110
1422
  debugState.restoring = false;
1111
1423
  }
1112
1424
  },
1113
- async switchPage(page, params) {
1425
+ async switchRoute(routeName, params) {
1114
1426
  return enqueueDebugOperation(async () => {
1115
1427
  debugState.lastError = null;
1116
1428
  try {
1117
- if (!appStateAdapter?.switchPage) {
1118
- throw new Error("Page switching is not supported by the app state adapter");
1429
+ const navigator = getNavigator();
1430
+ if (navigator.hasPage(routeName)) {
1431
+ if (!appStateAdapter?.switchPage) {
1432
+ throw new Error("Page switching is not supported by the app state adapter");
1433
+ }
1434
+ await appStateAdapter.switchPage(routeName, params);
1435
+ return;
1119
1436
  }
1120
- await appStateAdapter.switchPage(page, params);
1437
+ if (navigator.hasOverlay(routeName)) {
1438
+ if (!appStateAdapter?.switchOverlay) {
1439
+ throw new Error("Overlay switching is not supported by the app state adapter");
1440
+ }
1441
+ await appStateAdapter.switchOverlay(routeName, params);
1442
+ return;
1443
+ }
1444
+ throw new Error(`Route "${routeName}" not found`);
1121
1445
  } catch (error) {
1122
1446
  debugState.lastError = toErrorMessage(error);
1123
1447
  throw error;
@@ -1182,6 +1506,7 @@ async function startDebugSession(config = {}) {
1182
1506
  debugState.currentCursor = await executeScenarioCommand({
1183
1507
  subCommand: "getExecutionCursor"
1184
1508
  });
1509
+ debugState.currentStory = debugState.currentCursor?.story ?? null;
1185
1510
  } catch (error) {
1186
1511
  debugState.active = false;
1187
1512
  clearLocalRuntimeState();
@@ -1242,10 +1567,83 @@ function useDebugSession() {
1242
1567
  stopDebugSession,
1243
1568
  restoreCheckpoint: debugSessionController.restoreCheckpoint,
1244
1569
  warp: debugSessionController.warp,
1245
- switchPage: debugSessionController.switchPage
1570
+ switchRoute: debugSessionController.switchRoute
1246
1571
  };
1247
1572
  }
1248
1573
 
1574
+ // src/ui.ts
1575
+ var import_valtio3 = require("valtio");
1576
+ var UI_DATA_ASSET_PATH = "assets:///data/ui.json";
1577
+ var activeUiSchema = null;
1578
+ var uiDataState = (0, import_valtio3.proxy)({});
1579
+ var uiRuntimeState = (0, import_valtio3.proxy)({
1580
+ initialized: false,
1581
+ error: null
1582
+ });
1583
+ function applyUiData(nextData) {
1584
+ for (const key of Object.keys(uiDataState)) {
1585
+ delete uiDataState[key];
1586
+ }
1587
+ Object.assign(uiDataState, nextData);
1588
+ uiRuntimeState.initialized = true;
1589
+ uiRuntimeState.error = null;
1590
+ }
1591
+ async function initUiData(schema) {
1592
+ uiRuntimeState.initialized = false;
1593
+ uiRuntimeState.error = null;
1594
+ activeUiSchema = schema;
1595
+ let rawData;
1596
+ try {
1597
+ const rawText = await Promise.resolve(
1598
+ executePluginCommand("system", {
1599
+ subCommand: "readFile",
1600
+ path: UI_DATA_ASSET_PATH
1601
+ })
1602
+ );
1603
+ rawData = JSON.parse(rawText);
1604
+ } catch (error) {
1605
+ const message = error instanceof Error ? error.message : String(error);
1606
+ uiRuntimeState.error = message;
1607
+ throw new Error(`Failed to initialize UI data: ${message}`);
1608
+ }
1609
+ const parsed = schema.safeParse(rawData);
1610
+ if (!parsed.success) {
1611
+ uiRuntimeState.error = parsed.error.message;
1612
+ throw new Error(`Invalid UI data: ${parsed.error.message}`);
1613
+ }
1614
+ applyUiData(parsed.data);
1615
+ }
1616
+ function replaceUiData(rawData) {
1617
+ if (!activeUiSchema) {
1618
+ throw new Error("UI data schema has not been initialized. Call initUiData() first.");
1619
+ }
1620
+ const parsed = activeUiSchema.safeParse(rawData);
1621
+ if (!parsed.success) {
1622
+ uiRuntimeState.error = parsed.error.message;
1623
+ throw new Error(`Invalid replacement UI data: ${parsed.error.message}`);
1624
+ }
1625
+ applyUiData(parsed.data);
1626
+ }
1627
+ function getUiData(name) {
1628
+ if (!uiRuntimeState.initialized) {
1629
+ throw new Error("UI data has not been initialized. Call initUiData() before reading it.");
1630
+ }
1631
+ if (!(name in uiDataState)) {
1632
+ throw new Error(`UI data "${name}" is not available.`);
1633
+ }
1634
+ return uiDataState[name];
1635
+ }
1636
+ function useUiData(name) {
1637
+ if (!uiRuntimeState.initialized) {
1638
+ throw new Error("UI data has not been initialized. Call initUiData() before rendering UI pages.");
1639
+ }
1640
+ const snapshot = (0, import_valtio3.useSnapshot)(uiDataState);
1641
+ if (!(name in snapshot)) {
1642
+ throw new Error(`UI data "${name}" is not available.`);
1643
+ }
1644
+ return snapshot[name];
1645
+ }
1646
+
1249
1647
  // src/debug/runtime.ts
1250
1648
  var currentConnection = null;
1251
1649
  var currentController = null;
@@ -1254,11 +1652,14 @@ function getRuntimeWebSocket() {
1254
1652
  return globalThis.WebSocket ?? null;
1255
1653
  }
1256
1654
  function isJumpRequestMessage(message) {
1257
- return message.type === "jump:request" && typeof message.sessionId === "string" && typeof message.requestId === "number" && typeof message.markerId === "string" && (message.boundary === void 0 || message.boundary === "before" || message.boundary === "after") && (message.strategy === void 0 || message.strategy === "fast-forward" || message.strategy === "warp");
1655
+ return message.type === "jump:request" && typeof message.sessionId === "string" && typeof message.requestId === "number" && typeof message.markerId === "string" && (message.story === void 0 || typeof message.story === "string") && (message.boundary === void 0 || message.boundary === "before" || message.boundary === "after") && (message.strategy === void 0 || message.strategy === "fast-forward" || message.strategy === "warp");
1258
1656
  }
1259
1657
  function isRouteRequestMessage(message) {
1260
1658
  return message.type === "route:request" && typeof message.sessionId === "string" && typeof message.requestId === "number" && typeof message.page === "string" && (message.params === void 0 || typeof message.params === "object" && message.params !== null && !Array.isArray(message.params));
1261
1659
  }
1660
+ function isUiReplaceMessage(message) {
1661
+ return message.type === "ui:replace" && typeof message.sessionId === "string";
1662
+ }
1262
1663
  function sendRuntimeDebugMessage(socket, message) {
1263
1664
  const RuntimeWebSocket = getRuntimeWebSocket();
1264
1665
  if (!RuntimeWebSocket || socket.readyState !== RuntimeWebSocket.OPEN) {
@@ -1279,18 +1680,37 @@ function flushBufferedRequestMessages(socket, sessionId) {
1279
1680
  handleRuntimeDebugRequest(socket, sessionId, message);
1280
1681
  }
1281
1682
  }
1683
+ async function syncRuntimeDebugVariablesSnapshotSafe(reason) {
1684
+ try {
1685
+ await syncRuntimeDebugVariablesSnapshot(reason);
1686
+ } catch (error) {
1687
+ console.error("[debug] failed to sync runtime variables snapshot:", error);
1688
+ }
1689
+ }
1282
1690
  function handleRuntimeDebugRequest(socket, sessionId, message) {
1691
+ if (isUiReplaceMessage(message)) {
1692
+ try {
1693
+ replaceUiData(message.data);
1694
+ } catch (error) {
1695
+ console.error("[debug] failed to replace runtime UI data:", error);
1696
+ }
1697
+ return;
1698
+ }
1283
1699
  const controller = currentController;
1284
1700
  if (!controller) {
1285
1701
  bufferRequestMessage(message);
1286
1702
  return;
1287
1703
  }
1288
1704
  if (isJumpRequestMessage(message)) {
1705
+ setRuntimeDebugVariableEmissionSuspended(true);
1289
1706
  const runJump = message.strategy === "warp" ? controller.warp({
1290
1707
  markerId: message.markerId,
1708
+ story: message.story,
1291
1709
  boundary: message.boundary
1292
- }).then(() => true) : controller.restoreCheckpoint(message.markerId);
1293
- void runJump.then((restored) => {
1710
+ }).then(() => true) : controller.restoreCheckpoint(message.markerId, {
1711
+ story: message.story
1712
+ });
1713
+ void runJump.then(async (restored) => {
1294
1714
  if (!restored) {
1295
1715
  sendRuntimeDebugMessage(socket, {
1296
1716
  type: "jump:error",
@@ -1299,6 +1719,7 @@ function handleRuntimeDebugRequest(socket, sessionId, message) {
1299
1719
  markerId: message.markerId,
1300
1720
  error: "Checkpoint not found"
1301
1721
  });
1722
+ await syncRuntimeDebugVariablesSnapshotSafe("jump-error");
1302
1723
  return;
1303
1724
  }
1304
1725
  sendRuntimeDebugMessage(socket, {
@@ -1307,7 +1728,8 @@ function handleRuntimeDebugRequest(socket, sessionId, message) {
1307
1728
  requestId: message.requestId,
1308
1729
  markerId: message.markerId
1309
1730
  });
1310
- }).catch((error) => {
1731
+ await syncRuntimeDebugVariablesSnapshotSafe("jump-done");
1732
+ }).catch(async (error) => {
1311
1733
  sendRuntimeDebugMessage(socket, {
1312
1734
  type: "jump:error",
1313
1735
  sessionId,
@@ -1315,17 +1737,22 @@ function handleRuntimeDebugRequest(socket, sessionId, message) {
1315
1737
  markerId: message.markerId,
1316
1738
  error: error instanceof Error ? error.message : String(error)
1317
1739
  });
1740
+ await syncRuntimeDebugVariablesSnapshotSafe("jump-error");
1741
+ }).finally(() => {
1742
+ setRuntimeDebugVariableEmissionSuspended(false);
1318
1743
  });
1319
1744
  return;
1320
1745
  }
1321
- void controller.switchPage(message.page, message.params).then(() => {
1746
+ setRuntimeDebugVariableEmissionSuspended(true);
1747
+ void controller.switchRoute(message.page, message.params).then(async () => {
1322
1748
  sendRuntimeDebugMessage(socket, {
1323
1749
  type: "route:done",
1324
1750
  sessionId,
1325
1751
  requestId: message.requestId,
1326
1752
  page: message.page
1327
1753
  });
1328
- }).catch((error) => {
1754
+ await syncRuntimeDebugVariablesSnapshotSafe("route-done");
1755
+ }).catch(async (error) => {
1329
1756
  sendRuntimeDebugMessage(socket, {
1330
1757
  type: "route:error",
1331
1758
  sessionId,
@@ -1333,6 +1760,9 @@ function handleRuntimeDebugRequest(socket, sessionId, message) {
1333
1760
  page: message.page,
1334
1761
  error: error instanceof Error ? error.message : String(error)
1335
1762
  });
1763
+ await syncRuntimeDebugVariablesSnapshotSafe("route-error");
1764
+ }).finally(() => {
1765
+ setRuntimeDebugVariableEmissionSuspended(false);
1336
1766
  });
1337
1767
  }
1338
1768
  async function readDebugParams() {
@@ -1357,6 +1787,7 @@ async function stopRuntimeDebugSession() {
1357
1787
  const connection = currentConnection;
1358
1788
  currentConnection = null;
1359
1789
  currentController = null;
1790
+ resetRuntimeDebugVariableMonitor();
1360
1791
  clearBufferedRequestMessages();
1361
1792
  const RuntimeWebSocket = getRuntimeWebSocket();
1362
1793
  await stopDebugSession();
@@ -1403,6 +1834,13 @@ async function startRuntimeDebugSession() {
1403
1834
  }
1404
1835
  }).then((controller) => {
1405
1836
  currentController = controller;
1837
+ setRuntimeDebugVariableMessageSender((message) => {
1838
+ sendRuntimeDebugMessage(socket, {
1839
+ ...message,
1840
+ sessionId
1841
+ });
1842
+ });
1843
+ void syncRuntimeDebugVariablesSnapshotSafe("init");
1406
1844
  flushBufferedRequestMessages(socket, sessionId);
1407
1845
  }).catch((error) => {
1408
1846
  console.error("[debug] failed to start runtime debug session:", error);
@@ -1415,6 +1853,7 @@ async function startRuntimeDebugSession() {
1415
1853
  }
1416
1854
  currentConnection = null;
1417
1855
  currentController = null;
1856
+ resetRuntimeDebugVariableMonitor();
1418
1857
  clearBufferedRequestMessages();
1419
1858
  void stopDebugSession();
1420
1859
  };
@@ -1441,6 +1880,10 @@ async function startRuntimeDebugSession() {
1441
1880
  handleRuntimeDebugRequest(socket, sessionId, message);
1442
1881
  return;
1443
1882
  }
1883
+ if (isUiReplaceMessage(message)) {
1884
+ handleRuntimeDebugRequest(socket, sessionId, message);
1885
+ return;
1886
+ }
1444
1887
  if (message.type && message.type !== "session:init") {
1445
1888
  console.debug("[debug] runtime received message:", message);
1446
1889
  }
@@ -1674,49 +2117,31 @@ function useScenario(stories, startName, entryName, goNextOnLoad = false) {
1674
2117
 
1675
2118
  // src/hooks/useSoundEffect.ts
1676
2119
  var import_react4 = require("react");
1677
- var LOADED_SOUNDS = /* @__PURE__ */ new Set();
1678
2120
  function useSoundEffect(src) {
1679
2121
  (0, import_react4.useEffect)(() => {
1680
- const loadSound = async () => {
1681
- try {
1682
- await executePluginCommand("audio", {
1683
- subCommand: "load",
1684
- name: src,
1685
- src
1686
- });
1687
- } catch (error) {
1688
- console.error(`Failed to load sound effect ${src}:`, error);
1689
- }
1690
- LOADED_SOUNDS.add(src);
1691
- };
1692
- let needRelease = false;
1693
- if (!LOADED_SOUNDS.has(src)) {
1694
- void loadSound();
1695
- needRelease = true;
1696
- }
1697
2122
  return () => {
1698
- const releaseSound = () => {
1699
- try {
2123
+ try {
2124
+ if (src) {
1700
2125
  executePluginCommand("audio", {
1701
2126
  subCommand: "release",
1702
- name: src
2127
+ name: src,
2128
+ silentFail: true
1703
2129
  });
1704
- } catch (error) {
1705
- console.error(`Failed to release sound effect ${src}:`, error);
1706
2130
  }
1707
- LOADED_SOUNDS.delete(src);
1708
- };
1709
- if (needRelease) {
1710
- releaseSound();
2131
+ } catch (error) {
2132
+ console.error(`Failed to release sound effect ${src}:`, error);
1711
2133
  }
1712
2134
  };
1713
2135
  }, [src]);
1714
2136
  return (0, import_react4.useCallback)(() => {
1715
2137
  try {
1716
- executePluginCommand("audio", {
1717
- subCommand: "play",
1718
- name: src
1719
- });
2138
+ if (src) {
2139
+ executePluginCommand("audio", {
2140
+ subCommand: "loadAndPlay",
2141
+ name: src,
2142
+ src
2143
+ });
2144
+ }
1720
2145
  } catch (error) {
1721
2146
  console.error(`Failed to play sound effect ${src}:`, error);
1722
2147
  }
@@ -1725,25 +2150,25 @@ function useSoundEffect(src) {
1725
2150
 
1726
2151
  // src/hooks/useStage.ts
1727
2152
  var import_react5 = require("react");
1728
- var import_valtio3 = require("valtio");
2153
+ var import_valtio4 = require("valtio");
1729
2154
  var SKIP_INTERVAL_MS = 80;
1730
2155
  var DEFAULT_FAST_FORWARD_STEP_LIMIT = 1e4;
1731
2156
  var DEFAULT_FAST_FORWARD_TIME_LIMIT_MS = 3e3;
1732
2157
  var autoRuntime = {
1733
2158
  defaultTailMs: 0
1734
2159
  };
1735
- var skipState = (0, import_valtio3.proxy)({ active: false });
1736
- var autoState = (0, import_valtio3.proxy)({ active: false });
1737
- var fastForwardState = (0, import_valtio3.proxy)({ active: false });
1738
- var warpState = (0, import_valtio3.proxy)({ active: false });
2160
+ var skipState = (0, import_valtio4.proxy)({ active: false });
2161
+ var autoState = (0, import_valtio4.proxy)({ active: false });
2162
+ var fastForwardState = (0, import_valtio4.proxy)({ active: false });
2163
+ var warpState = (0, import_valtio4.proxy)({ active: false });
1739
2164
  function useIsSkipping() {
1740
- return (0, import_valtio3.useSnapshot)(skipState).active;
2165
+ return (0, import_valtio4.useSnapshot)(skipState).active;
1741
2166
  }
1742
2167
  function useIsAutoing() {
1743
- return (0, import_valtio3.useSnapshot)(autoState).active;
2168
+ return (0, import_valtio4.useSnapshot)(autoState).active;
1744
2169
  }
1745
2170
  function useIsSeeking() {
1746
- return (0, import_valtio3.useSnapshot)(fastForwardState).active;
2171
+ return (0, import_valtio4.useSnapshot)(fastForwardState).active;
1747
2172
  }
1748
2173
  function getSeekingType() {
1749
2174
  if (warpState.active) return "warp";