@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.mjs CHANGED
@@ -19,6 +19,7 @@ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "defau
19
19
  // src/lib.ts
20
20
  var lib_exports = {};
21
21
  __export(lib_exports, {
22
+ DEBUG_EMPTY_PAGE: () => DEBUG_EMPTY_PAGE,
22
23
  OverlayParamsContext: () => OverlayParamsContext,
23
24
  PageParamsContext: () => PageParamsContext,
24
25
  StageContextProvider: () => StageContextProvider,
@@ -31,6 +32,7 @@ __export(lib_exports, {
31
32
  createStackNavigator: () => createStackNavigator,
32
33
  createStage: () => createStage,
33
34
  createStaticNavigation: () => createStaticNavigation,
35
+ ensureArchiveVariableDefaults: () => ensureArchiveVariableDefaults,
34
36
  executeNodeCommand: () => executeNodeCommand,
35
37
  executePluginCommand: () => executePluginCommand,
36
38
  fastForwardState: () => fastForwardState,
@@ -38,11 +40,16 @@ __export(lib_exports, {
38
40
  getNavigator: () => getNavigator,
39
41
  getSeekingType: () => getSeekingType,
40
42
  getStageSize: () => getStageSize,
43
+ getUiData: () => getUiData,
41
44
  globalEventListeners: () => globalEventListeners,
42
45
  globalRequestAnimationFrameListeners: () => globalRequestAnimationFrameListeners,
46
+ initGlobalVariableDefaults: () => initGlobalVariableDefaults,
47
+ initUiData: () => initUiData,
48
+ loadVariablesDocument: () => loadVariablesDocument,
43
49
  moyu: () => moyu_exports,
44
50
  nextLine: () => nextLine,
45
51
  registerAppStateAdapter: () => registerAppStateAdapter,
52
+ replaceUiData: () => replaceUiData,
46
53
  setDefaultAutoTailMs: () => setDefaultAutoTailMs,
47
54
  setWaiting: () => setWaiting,
48
55
  skipState: () => skipState,
@@ -69,6 +76,7 @@ __export(lib_exports, {
69
76
  useSkipCallback: () => useSkipCallback,
70
77
  useSoundEffect: () => useSoundEffect,
71
78
  useStageContext: () => useStageContext,
79
+ useUiData: () => useUiData,
72
80
  warpState: () => warpState
73
81
  });
74
82
 
@@ -393,6 +401,259 @@ function createRoot(options) {
393
401
  };
394
402
  }
395
403
 
404
+ // src/variables.ts
405
+ var VARIABLES_DATA_ASSET_PATH = "assets:///data/variables.json";
406
+ function normalizeVariableDefinitions(value) {
407
+ if (!Array.isArray(value)) {
408
+ return [];
409
+ }
410
+ return value.flatMap((entry) => {
411
+ if (!entry || typeof entry !== "object") {
412
+ return [];
413
+ }
414
+ const definition = entry;
415
+ if (typeof definition.id !== "string" || definition.id.length === 0) {
416
+ return [];
417
+ }
418
+ return [{ id: definition.id, defaultValue: definition.defaultValue }];
419
+ });
420
+ }
421
+ async function loadVariablesDocument() {
422
+ let rawText;
423
+ try {
424
+ rawText = await executePluginCommand("system", {
425
+ subCommand: "readFile",
426
+ path: VARIABLES_DATA_ASSET_PATH
427
+ });
428
+ } catch {
429
+ return null;
430
+ }
431
+ let rawData;
432
+ try {
433
+ rawData = JSON.parse(rawText);
434
+ } catch (error) {
435
+ console.error("Failed to parse variable defaults data:", error);
436
+ return null;
437
+ }
438
+ if (!rawData || typeof rawData !== "object") {
439
+ return {
440
+ archive: [],
441
+ global: []
442
+ };
443
+ }
444
+ const document = rawData;
445
+ return {
446
+ archive: normalizeVariableDefinitions(document.archive),
447
+ global: normalizeVariableDefinitions(document.global)
448
+ };
449
+ }
450
+ function toVariableRecord(value) {
451
+ if (value instanceof Map) {
452
+ return Object.fromEntries(value);
453
+ }
454
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
455
+ return {};
456
+ }
457
+ return value;
458
+ }
459
+ async function ensureVariableDefaults(scope) {
460
+ const document = await loadVariablesDocument();
461
+ if (!document) {
462
+ return;
463
+ }
464
+ const definitions = document[scope];
465
+ if (definitions.length === 0) {
466
+ return;
467
+ }
468
+ const currentVariables = toVariableRecord(
469
+ await executePluginCommand(
470
+ "scenario",
471
+ scope === "global" ? { subCommand: "getPermanentVariables" } : { subCommand: "getVariables" }
472
+ )
473
+ );
474
+ const missingVariables = {};
475
+ for (const definition of definitions) {
476
+ if (!Object.hasOwn(currentVariables, definition.id)) {
477
+ missingVariables[definition.id] = definition.defaultValue;
478
+ }
479
+ }
480
+ if (Object.keys(missingVariables).length === 0) {
481
+ return;
482
+ }
483
+ await executePluginCommand(
484
+ "scenario",
485
+ scope === "global" ? {
486
+ subCommand: "setPermanentVariables",
487
+ variables: missingVariables
488
+ } : {
489
+ subCommand: "setVariables",
490
+ variables: missingVariables
491
+ }
492
+ );
493
+ reportRuntimeDebugVariablesSet(scope, missingVariables);
494
+ }
495
+ async function initGlobalVariableDefaults() {
496
+ await ensureVariableDefaults("global");
497
+ }
498
+ async function ensureArchiveVariableDefaults() {
499
+ await ensureVariableDefaults("archive");
500
+ }
501
+
502
+ // src/debug/variableMonitor.ts
503
+ var sendRuntimeDebugVariableMessage = null;
504
+ var allowlistCache = null;
505
+ var allowlistPromise = null;
506
+ var variableEmissionSuspended = false;
507
+ function createEmptyAllowlist() {
508
+ return {
509
+ archive: /* @__PURE__ */ new Set(),
510
+ global: /* @__PURE__ */ new Set()
511
+ };
512
+ }
513
+ function isJsonValue(value) {
514
+ if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
515
+ return true;
516
+ }
517
+ if (Array.isArray(value)) {
518
+ return value.every((item) => isJsonValue(item));
519
+ }
520
+ if (!value || typeof value !== "object") {
521
+ return false;
522
+ }
523
+ return Object.values(value).every((item) => isJsonValue(item));
524
+ }
525
+ async function loadRuntimeDebugVariableAllowlist() {
526
+ const document = await loadVariablesDocument();
527
+ if (!document) {
528
+ return createEmptyAllowlist();
529
+ }
530
+ return {
531
+ archive: new Set(document.archive.map((def) => def.id)),
532
+ global: new Set(document.global.map((def) => def.id))
533
+ };
534
+ }
535
+ function toJsonValueRecord(value) {
536
+ if (value instanceof Map) {
537
+ return Object.fromEntries(
538
+ Array.from(value.entries()).flatMap(([key, entryValue]) => {
539
+ if (typeof key !== "string" || !isJsonValue(entryValue)) {
540
+ return [];
541
+ }
542
+ return [[key, entryValue]];
543
+ })
544
+ );
545
+ }
546
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
547
+ return {};
548
+ }
549
+ return Object.fromEntries(
550
+ Object.entries(value).flatMap(([key, entryValue]) => {
551
+ if (!isJsonValue(entryValue)) {
552
+ return [];
553
+ }
554
+ return [[key, entryValue]];
555
+ })
556
+ );
557
+ }
558
+ function filterVariablesByAllowlist(scope, values, allowlist) {
559
+ const allowedIds = allowlist[scope];
560
+ if (allowedIds.size === 0) {
561
+ return {};
562
+ }
563
+ return Object.fromEntries(
564
+ Object.entries(values).flatMap(([key, value]) => allowedIds.has(key) ? [[key, value]] : [])
565
+ );
566
+ }
567
+ function hasRecordEntries(value) {
568
+ return !!value && Object.keys(value).length > 0;
569
+ }
570
+ async function ensureRuntimeDebugVariableAllowlist() {
571
+ if (allowlistCache) {
572
+ return allowlistCache;
573
+ }
574
+ if (!allowlistPromise) {
575
+ allowlistPromise = loadRuntimeDebugVariableAllowlist().then((allowlist) => {
576
+ allowlistCache = allowlist;
577
+ return allowlist;
578
+ }).finally(() => {
579
+ allowlistPromise = null;
580
+ });
581
+ }
582
+ return allowlistPromise;
583
+ }
584
+ async function emitRuntimeDebugVariablesChanged(scope, variables) {
585
+ const sender = sendRuntimeDebugVariableMessage;
586
+ if (!sender || variableEmissionSuspended) {
587
+ return;
588
+ }
589
+ const allowlist = await ensureRuntimeDebugVariableAllowlist();
590
+ if (!sendRuntimeDebugVariableMessage || sendRuntimeDebugVariableMessage !== sender || variableEmissionSuspended) {
591
+ return;
592
+ }
593
+ const filtered = filterVariablesByAllowlist(scope, variables, allowlist);
594
+ if (!hasRecordEntries(filtered)) {
595
+ return;
596
+ }
597
+ sender({
598
+ type: "variables:changed",
599
+ archiveSet: scope === "archive" ? filtered : void 0,
600
+ globalSet: scope === "global" ? filtered : void 0
601
+ });
602
+ }
603
+ function setRuntimeDebugVariableMessageSender(sender) {
604
+ sendRuntimeDebugVariableMessage = sender;
605
+ if (sender) {
606
+ void ensureRuntimeDebugVariableAllowlist();
607
+ }
608
+ }
609
+ function setRuntimeDebugVariableEmissionSuspended(suspended) {
610
+ variableEmissionSuspended = suspended;
611
+ }
612
+ function resetRuntimeDebugVariableMonitor() {
613
+ sendRuntimeDebugVariableMessage = null;
614
+ allowlistCache = null;
615
+ allowlistPromise = null;
616
+ variableEmissionSuspended = false;
617
+ }
618
+ function reportRuntimeDebugVariableSet(scope, key, value) {
619
+ if (key.length === 0) {
620
+ return;
621
+ }
622
+ void emitRuntimeDebugVariablesChanged(scope, {
623
+ [key]: value
624
+ });
625
+ }
626
+ function reportRuntimeDebugVariablesSet(scope, variables) {
627
+ if (Object.keys(variables).length === 0) {
628
+ return;
629
+ }
630
+ void emitRuntimeDebugVariablesChanged(scope, variables);
631
+ }
632
+ async function syncRuntimeDebugVariablesSnapshot(reason) {
633
+ const sender = sendRuntimeDebugVariableMessage;
634
+ if (!sender) {
635
+ return;
636
+ }
637
+ const [allowlist, archiveVariables2, globalVariables2] = await Promise.all([
638
+ ensureRuntimeDebugVariableAllowlist(),
639
+ executePluginCommand("scenario", {
640
+ subCommand: "getVariables"
641
+ }),
642
+ executePluginCommand("scenario", {
643
+ subCommand: "getPermanentVariables"
644
+ })
645
+ ]);
646
+ if (!sendRuntimeDebugVariableMessage || sendRuntimeDebugVariableMessage !== sender) {
647
+ return;
648
+ }
649
+ sender({
650
+ type: "variables:snapshot",
651
+ reason,
652
+ archive: filterVariablesByAllowlist("archive", toJsonValueRecord(archiveVariables2), allowlist),
653
+ global: filterVariablesByAllowlist("global", toJsonValueRecord(globalVariables2), allowlist)
654
+ });
655
+ }
656
+
396
657
  // src/sandbox.ts
397
658
  var JS_GLOBAL_KEYS = new Set(Object.getOwnPropertyNames(globalThis));
398
659
  var JS_GLOBAL_KEYS_EXCLUDED = /* @__PURE__ */ new Set(["window", "globalThis", "LOCAL", "ARCHIVE", "GLOBAL"]);
@@ -443,6 +704,7 @@ var archiveVariables = new Proxy(
443
704
  name: key,
444
705
  value
445
706
  });
707
+ reportRuntimeDebugVariableSet("archive", key, value);
446
708
  return true;
447
709
  }
448
710
  return false;
@@ -468,6 +730,7 @@ var globalVariables = new Proxy(
468
730
  key,
469
731
  value
470
732
  });
733
+ reportRuntimeDebugVariableSet("global", key, value);
471
734
  return true;
472
735
  }
473
736
  return false;
@@ -475,7 +738,7 @@ var globalVariables = new Proxy(
475
738
  }
476
739
  );
477
740
  function safeEval(code, sandbox) {
478
- const proxy4 = new Proxy(sandbox, {
741
+ const proxy5 = new Proxy(sandbox, {
479
742
  has(target, key) {
480
743
  if (typeof key === "string" && JS_GLOBAL_KEYS.has(key)) {
481
744
  return false;
@@ -483,7 +746,7 @@ function safeEval(code, sandbox) {
483
746
  return key in target || key === "LOCAL" || key === "ARCHIVE" || key === "GLOBAL" || key === "window" || key === "globalThis";
484
747
  },
485
748
  get(target, key) {
486
- if (key === "window" || key === "globalThis") return proxy4;
749
+ if (key === "window" || key === "globalThis") return proxy5;
487
750
  if (key === "LOCAL") return localVariables;
488
751
  if (key === "ARCHIVE") return archiveVariables;
489
752
  if (key === "GLOBAL") return globalVariables;
@@ -495,7 +758,7 @@ function safeEval(code, sandbox) {
495
758
  return true;
496
759
  }
497
760
  });
498
- return new Function("sandbox", `with(sandbox) { return (${code}) }`).bind(proxy4)(proxy4);
761
+ return new Function("sandbox", `with(sandbox) { return (${code}) }`).bind(proxy5)(proxy5);
499
762
  }
500
763
  globalThis.__moyu_eval_sandbox = (code) => {
501
764
  try {
@@ -683,6 +946,7 @@ import { proxy, useSnapshot } from "valtio";
683
946
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
684
947
  var PageParamsContext = createContext(void 0);
685
948
  var OverlayParamsContext = createContext(void 0);
949
+ var DEBUG_EMPTY_PAGE = "debug-empty";
686
950
  var overlayIdCounter = 0;
687
951
  var generateOverlayId = () => {
688
952
  return `overlay-${++overlayIdCounter}-${Date.now()}`;
@@ -710,6 +974,12 @@ function createStackNavigator(options) {
710
974
  for (const [key, config] of Object.entries(options.pages)) {
711
975
  normalizedPages[key] = normalizeDescriptor(config);
712
976
  }
977
+ if (normalizedPages[DEBUG_EMPTY_PAGE]) {
978
+ throw new Error(`Navigation Error: Page name "${DEBUG_EMPTY_PAGE}" is reserved.`);
979
+ }
980
+ normalizedPages[DEBUG_EMPTY_PAGE] = {
981
+ component: () => null
982
+ };
713
983
  const normalizedOverlays = {};
714
984
  if (options.overlays) {
715
985
  for (const [key, config] of Object.entries(options.overlays)) {
@@ -739,6 +1009,9 @@ function createStackNavigator(options) {
739
1009
  hasPage: (page) => {
740
1010
  return normalizedPages[page] !== void 0;
741
1011
  },
1012
+ hasOverlay: (overlay) => {
1013
+ return normalizedOverlays[overlay] !== void 0;
1014
+ },
742
1015
  goBack: () => {
743
1016
  console.warn("goBack() not implemented yet");
744
1017
  },
@@ -826,6 +1099,7 @@ var debugState = proxy2({
826
1099
  active: false,
827
1100
  checkpoints: {},
828
1101
  currentCursor: null,
1102
+ currentStory: null,
829
1103
  restoring: false,
830
1104
  lastError: null
831
1105
  });
@@ -869,9 +1143,37 @@ function reportDebugSessionError(error) {
869
1143
  function clearLocalRuntimeState() {
870
1144
  debugState.checkpoints = {};
871
1145
  debugState.currentCursor = null;
1146
+ debugState.currentStory = null;
872
1147
  debugState.restoring = false;
873
1148
  debugState.lastError = null;
874
1149
  }
1150
+ async function restartJumpStoryFromHead(targetStory, currentStory) {
1151
+ debugState.checkpoints = {};
1152
+ debugState.currentCursor = null;
1153
+ debugState.currentStory = null;
1154
+ if (targetStory) {
1155
+ if (appStateAdapter?.restartStoryFromHead) {
1156
+ await appStateAdapter.restartStoryFromHead({
1157
+ story: targetStory,
1158
+ entry: "entry"
1159
+ });
1160
+ } else if (currentStory === targetStory && appStateAdapter?.restartCurrentStoryFromHead) {
1161
+ await appStateAdapter.restartCurrentStoryFromHead();
1162
+ } else {
1163
+ throw new Error("Restarting the target story from head is not supported by the app state adapter");
1164
+ }
1165
+ debugState.currentCursor = await executeScenarioCommand({
1166
+ subCommand: "getExecutionCursor"
1167
+ });
1168
+ debugState.currentStory = debugState.currentCursor?.story ?? targetStory;
1169
+ return;
1170
+ }
1171
+ if (!appStateAdapter?.restartCurrentStoryFromHead) {
1172
+ throw new Error("Restarting the current story from head is not supported by the app state adapter");
1173
+ }
1174
+ await appStateAdapter.restartCurrentStoryFromHead();
1175
+ debugState.currentCursor = null;
1176
+ }
875
1177
  async function doSettleActiveSeek(op, clearSlot, error) {
876
1178
  if (!op) {
877
1179
  return;
@@ -924,6 +1226,7 @@ async function syncLocalWarpCheckpoint(targetMarkerId) {
924
1226
  appState: appStateAdapter ? await appStateAdapter.capture() : void 0
925
1227
  };
926
1228
  debugState.currentCursor = checkpointCursor;
1229
+ debugState.currentStory = checkpointCursor.story;
927
1230
  debugState.checkpoints[targetMarkerId] = checkpoint;
928
1231
  await currentConfig.onMarkerEnter(checkpoint);
929
1232
  }
@@ -947,9 +1250,16 @@ function notifyMarkerEnter(checkpoint) {
947
1250
  reportDebugSessionError(error);
948
1251
  }
949
1252
  }
950
- async function prepareJumpStart(markerId) {
1253
+ async function prepareJumpStart(markerId, options) {
1254
+ const currentStory = debugState.currentStory ?? debugState.currentCursor?.story ?? null;
1255
+ const targetStory = options?.story;
1256
+ let restartedFromHead = false;
1257
+ if (targetStory && targetStory !== currentStory) {
1258
+ await restartJumpStoryFromHead(targetStory, currentStory);
1259
+ restartedFromHead = true;
1260
+ }
951
1261
  const checkpoint = debugState.checkpoints[markerId];
952
- if (checkpoint) {
1262
+ if (!restartedFromHead && checkpoint && (!targetStory || checkpoint.cursor.story === targetStory)) {
953
1263
  const restored2 = await executeScenarioCommand({
954
1264
  subCommand: "restoreCheckpoint",
955
1265
  key: markerId
@@ -961,6 +1271,7 @@ async function prepareJumpStart(markerId) {
961
1271
  await appStateAdapter.restore(checkpoint.appState);
962
1272
  }
963
1273
  debugState.currentCursor = checkpoint.cursor;
1274
+ debugState.currentStory = checkpoint.cursor.story;
964
1275
  return false;
965
1276
  }
966
1277
  const lookup = await executeScenarioCommand({
@@ -968,16 +1279,15 @@ async function prepareJumpStart(markerId) {
968
1279
  key: markerId
969
1280
  });
970
1281
  if (!lookup.markerExists) {
971
- throw new Error(`Marker ${markerId} is not available in the current story`);
1282
+ throw new Error(
1283
+ targetStory ? `Marker ${markerId} is not available in story ${targetStory}` : `Marker ${markerId} is not available in the current story`
1284
+ );
972
1285
  }
973
1286
  const startCheckpointKey = lookup.checkpointKey ?? null;
974
1287
  if (!startCheckpointKey) {
975
- if (!appStateAdapter?.restartCurrentStoryFromHead) {
976
- throw new Error("Restarting the current story from head is not supported by the app state adapter");
1288
+ if (!restartedFromHead) {
1289
+ await restartJumpStoryFromHead(targetStory, currentStory);
977
1290
  }
978
- await appStateAdapter.restartCurrentStoryFromHead();
979
- debugState.checkpoints = {};
980
- debugState.currentCursor = null;
981
1291
  return true;
982
1292
  }
983
1293
  const startCheckpoint = debugState.checkpoints[startCheckpointKey];
@@ -995,6 +1305,7 @@ async function prepareJumpStart(markerId) {
995
1305
  await appStateAdapter.restore(startCheckpoint.appState);
996
1306
  }
997
1307
  debugState.currentCursor = startCheckpoint.cursor;
1308
+ debugState.currentStory = startCheckpoint.cursor.story;
998
1309
  return true;
999
1310
  }
1000
1311
  function handleMarkerEnter(marker) {
@@ -1007,6 +1318,7 @@ function handleMarkerEnter(marker) {
1007
1318
  appState: captureCheckpointAppStateSync()
1008
1319
  };
1009
1320
  debugState.currentCursor = checkpoint.cursor;
1321
+ debugState.currentStory = checkpoint.cursor.story;
1010
1322
  debugState.checkpoints[marker.markerId] = checkpoint;
1011
1323
  notifyMarkerEnter(checkpoint);
1012
1324
  if (activeFastForward?.targetMarkerId === marker.markerId) {
@@ -1067,11 +1379,11 @@ async function runWarp(options) {
1067
1379
  await completion;
1068
1380
  }
1069
1381
  var debugSessionController = {
1070
- async restoreCheckpoint(markerId) {
1382
+ async restoreCheckpoint(markerId, options) {
1071
1383
  debugState.restoring = true;
1072
1384
  debugState.lastError = null;
1073
1385
  try {
1074
- const needsFastForward = await enqueueDebugOperation(async () => prepareJumpStart(markerId));
1386
+ const needsFastForward = await enqueueDebugOperation(async () => prepareJumpStart(markerId, options));
1075
1387
  if (needsFastForward) {
1076
1388
  await runFastForwardToMarker(markerId);
1077
1389
  }
@@ -1087,7 +1399,7 @@ var debugSessionController = {
1087
1399
  debugState.restoring = true;
1088
1400
  debugState.lastError = null;
1089
1401
  try {
1090
- const needsWarp = await enqueueDebugOperation(async () => prepareJumpStart(options.markerId));
1402
+ const needsWarp = await enqueueDebugOperation(async () => prepareJumpStart(options.markerId, options));
1091
1403
  if (needsWarp) {
1092
1404
  await runWarp(options);
1093
1405
  }
@@ -1098,14 +1410,26 @@ var debugSessionController = {
1098
1410
  debugState.restoring = false;
1099
1411
  }
1100
1412
  },
1101
- async switchPage(page, params) {
1413
+ async switchRoute(routeName, params) {
1102
1414
  return enqueueDebugOperation(async () => {
1103
1415
  debugState.lastError = null;
1104
1416
  try {
1105
- if (!appStateAdapter?.switchPage) {
1106
- throw new Error("Page switching is not supported by the app state adapter");
1417
+ const navigator = getNavigator();
1418
+ if (navigator.hasPage(routeName)) {
1419
+ if (!appStateAdapter?.switchPage) {
1420
+ throw new Error("Page switching is not supported by the app state adapter");
1421
+ }
1422
+ await appStateAdapter.switchPage(routeName, params);
1423
+ return;
1107
1424
  }
1108
- await appStateAdapter.switchPage(page, params);
1425
+ if (navigator.hasOverlay(routeName)) {
1426
+ if (!appStateAdapter?.switchOverlay) {
1427
+ throw new Error("Overlay switching is not supported by the app state adapter");
1428
+ }
1429
+ await appStateAdapter.switchOverlay(routeName, params);
1430
+ return;
1431
+ }
1432
+ throw new Error(`Route "${routeName}" not found`);
1109
1433
  } catch (error) {
1110
1434
  debugState.lastError = toErrorMessage(error);
1111
1435
  throw error;
@@ -1170,6 +1494,7 @@ async function startDebugSession(config = {}) {
1170
1494
  debugState.currentCursor = await executeScenarioCommand({
1171
1495
  subCommand: "getExecutionCursor"
1172
1496
  });
1497
+ debugState.currentStory = debugState.currentCursor?.story ?? null;
1173
1498
  } catch (error) {
1174
1499
  debugState.active = false;
1175
1500
  clearLocalRuntimeState();
@@ -1230,10 +1555,83 @@ function useDebugSession() {
1230
1555
  stopDebugSession,
1231
1556
  restoreCheckpoint: debugSessionController.restoreCheckpoint,
1232
1557
  warp: debugSessionController.warp,
1233
- switchPage: debugSessionController.switchPage
1558
+ switchRoute: debugSessionController.switchRoute
1234
1559
  };
1235
1560
  }
1236
1561
 
1562
+ // src/ui.ts
1563
+ import { proxy as proxy3, useSnapshot as useSnapshot3 } from "valtio";
1564
+ var UI_DATA_ASSET_PATH = "assets:///data/ui.json";
1565
+ var activeUiSchema = null;
1566
+ var uiDataState = proxy3({});
1567
+ var uiRuntimeState = proxy3({
1568
+ initialized: false,
1569
+ error: null
1570
+ });
1571
+ function applyUiData(nextData) {
1572
+ for (const key of Object.keys(uiDataState)) {
1573
+ delete uiDataState[key];
1574
+ }
1575
+ Object.assign(uiDataState, nextData);
1576
+ uiRuntimeState.initialized = true;
1577
+ uiRuntimeState.error = null;
1578
+ }
1579
+ async function initUiData(schema) {
1580
+ uiRuntimeState.initialized = false;
1581
+ uiRuntimeState.error = null;
1582
+ activeUiSchema = schema;
1583
+ let rawData;
1584
+ try {
1585
+ const rawText = await Promise.resolve(
1586
+ executePluginCommand("system", {
1587
+ subCommand: "readFile",
1588
+ path: UI_DATA_ASSET_PATH
1589
+ })
1590
+ );
1591
+ rawData = JSON.parse(rawText);
1592
+ } catch (error) {
1593
+ const message = error instanceof Error ? error.message : String(error);
1594
+ uiRuntimeState.error = message;
1595
+ throw new Error(`Failed to initialize UI data: ${message}`);
1596
+ }
1597
+ const parsed = schema.safeParse(rawData);
1598
+ if (!parsed.success) {
1599
+ uiRuntimeState.error = parsed.error.message;
1600
+ throw new Error(`Invalid UI data: ${parsed.error.message}`);
1601
+ }
1602
+ applyUiData(parsed.data);
1603
+ }
1604
+ function replaceUiData(rawData) {
1605
+ if (!activeUiSchema) {
1606
+ throw new Error("UI data schema has not been initialized. Call initUiData() first.");
1607
+ }
1608
+ const parsed = activeUiSchema.safeParse(rawData);
1609
+ if (!parsed.success) {
1610
+ uiRuntimeState.error = parsed.error.message;
1611
+ throw new Error(`Invalid replacement UI data: ${parsed.error.message}`);
1612
+ }
1613
+ applyUiData(parsed.data);
1614
+ }
1615
+ function getUiData(name) {
1616
+ if (!uiRuntimeState.initialized) {
1617
+ throw new Error("UI data has not been initialized. Call initUiData() before reading it.");
1618
+ }
1619
+ if (!(name in uiDataState)) {
1620
+ throw new Error(`UI data "${name}" is not available.`);
1621
+ }
1622
+ return uiDataState[name];
1623
+ }
1624
+ function useUiData(name) {
1625
+ if (!uiRuntimeState.initialized) {
1626
+ throw new Error("UI data has not been initialized. Call initUiData() before rendering UI pages.");
1627
+ }
1628
+ const snapshot = useSnapshot3(uiDataState);
1629
+ if (!(name in snapshot)) {
1630
+ throw new Error(`UI data "${name}" is not available.`);
1631
+ }
1632
+ return snapshot[name];
1633
+ }
1634
+
1237
1635
  // src/debug/runtime.ts
1238
1636
  var currentConnection = null;
1239
1637
  var currentController = null;
@@ -1242,11 +1640,14 @@ function getRuntimeWebSocket() {
1242
1640
  return globalThis.WebSocket ?? null;
1243
1641
  }
1244
1642
  function isJumpRequestMessage(message) {
1245
- 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");
1643
+ 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");
1246
1644
  }
1247
1645
  function isRouteRequestMessage(message) {
1248
1646
  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));
1249
1647
  }
1648
+ function isUiReplaceMessage(message) {
1649
+ return message.type === "ui:replace" && typeof message.sessionId === "string";
1650
+ }
1250
1651
  function sendRuntimeDebugMessage(socket, message) {
1251
1652
  const RuntimeWebSocket = getRuntimeWebSocket();
1252
1653
  if (!RuntimeWebSocket || socket.readyState !== RuntimeWebSocket.OPEN) {
@@ -1267,18 +1668,37 @@ function flushBufferedRequestMessages(socket, sessionId) {
1267
1668
  handleRuntimeDebugRequest(socket, sessionId, message);
1268
1669
  }
1269
1670
  }
1671
+ async function syncRuntimeDebugVariablesSnapshotSafe(reason) {
1672
+ try {
1673
+ await syncRuntimeDebugVariablesSnapshot(reason);
1674
+ } catch (error) {
1675
+ console.error("[debug] failed to sync runtime variables snapshot:", error);
1676
+ }
1677
+ }
1270
1678
  function handleRuntimeDebugRequest(socket, sessionId, message) {
1679
+ if (isUiReplaceMessage(message)) {
1680
+ try {
1681
+ replaceUiData(message.data);
1682
+ } catch (error) {
1683
+ console.error("[debug] failed to replace runtime UI data:", error);
1684
+ }
1685
+ return;
1686
+ }
1271
1687
  const controller = currentController;
1272
1688
  if (!controller) {
1273
1689
  bufferRequestMessage(message);
1274
1690
  return;
1275
1691
  }
1276
1692
  if (isJumpRequestMessage(message)) {
1693
+ setRuntimeDebugVariableEmissionSuspended(true);
1277
1694
  const runJump = message.strategy === "warp" ? controller.warp({
1278
1695
  markerId: message.markerId,
1696
+ story: message.story,
1279
1697
  boundary: message.boundary
1280
- }).then(() => true) : controller.restoreCheckpoint(message.markerId);
1281
- void runJump.then((restored) => {
1698
+ }).then(() => true) : controller.restoreCheckpoint(message.markerId, {
1699
+ story: message.story
1700
+ });
1701
+ void runJump.then(async (restored) => {
1282
1702
  if (!restored) {
1283
1703
  sendRuntimeDebugMessage(socket, {
1284
1704
  type: "jump:error",
@@ -1287,6 +1707,7 @@ function handleRuntimeDebugRequest(socket, sessionId, message) {
1287
1707
  markerId: message.markerId,
1288
1708
  error: "Checkpoint not found"
1289
1709
  });
1710
+ await syncRuntimeDebugVariablesSnapshotSafe("jump-error");
1290
1711
  return;
1291
1712
  }
1292
1713
  sendRuntimeDebugMessage(socket, {
@@ -1295,7 +1716,8 @@ function handleRuntimeDebugRequest(socket, sessionId, message) {
1295
1716
  requestId: message.requestId,
1296
1717
  markerId: message.markerId
1297
1718
  });
1298
- }).catch((error) => {
1719
+ await syncRuntimeDebugVariablesSnapshotSafe("jump-done");
1720
+ }).catch(async (error) => {
1299
1721
  sendRuntimeDebugMessage(socket, {
1300
1722
  type: "jump:error",
1301
1723
  sessionId,
@@ -1303,17 +1725,22 @@ function handleRuntimeDebugRequest(socket, sessionId, message) {
1303
1725
  markerId: message.markerId,
1304
1726
  error: error instanceof Error ? error.message : String(error)
1305
1727
  });
1728
+ await syncRuntimeDebugVariablesSnapshotSafe("jump-error");
1729
+ }).finally(() => {
1730
+ setRuntimeDebugVariableEmissionSuspended(false);
1306
1731
  });
1307
1732
  return;
1308
1733
  }
1309
- void controller.switchPage(message.page, message.params).then(() => {
1734
+ setRuntimeDebugVariableEmissionSuspended(true);
1735
+ void controller.switchRoute(message.page, message.params).then(async () => {
1310
1736
  sendRuntimeDebugMessage(socket, {
1311
1737
  type: "route:done",
1312
1738
  sessionId,
1313
1739
  requestId: message.requestId,
1314
1740
  page: message.page
1315
1741
  });
1316
- }).catch((error) => {
1742
+ await syncRuntimeDebugVariablesSnapshotSafe("route-done");
1743
+ }).catch(async (error) => {
1317
1744
  sendRuntimeDebugMessage(socket, {
1318
1745
  type: "route:error",
1319
1746
  sessionId,
@@ -1321,6 +1748,9 @@ function handleRuntimeDebugRequest(socket, sessionId, message) {
1321
1748
  page: message.page,
1322
1749
  error: error instanceof Error ? error.message : String(error)
1323
1750
  });
1751
+ await syncRuntimeDebugVariablesSnapshotSafe("route-error");
1752
+ }).finally(() => {
1753
+ setRuntimeDebugVariableEmissionSuspended(false);
1324
1754
  });
1325
1755
  }
1326
1756
  async function readDebugParams() {
@@ -1345,6 +1775,7 @@ async function stopRuntimeDebugSession() {
1345
1775
  const connection = currentConnection;
1346
1776
  currentConnection = null;
1347
1777
  currentController = null;
1778
+ resetRuntimeDebugVariableMonitor();
1348
1779
  clearBufferedRequestMessages();
1349
1780
  const RuntimeWebSocket = getRuntimeWebSocket();
1350
1781
  await stopDebugSession();
@@ -1391,6 +1822,13 @@ async function startRuntimeDebugSession() {
1391
1822
  }
1392
1823
  }).then((controller) => {
1393
1824
  currentController = controller;
1825
+ setRuntimeDebugVariableMessageSender((message) => {
1826
+ sendRuntimeDebugMessage(socket, {
1827
+ ...message,
1828
+ sessionId
1829
+ });
1830
+ });
1831
+ void syncRuntimeDebugVariablesSnapshotSafe("init");
1394
1832
  flushBufferedRequestMessages(socket, sessionId);
1395
1833
  }).catch((error) => {
1396
1834
  console.error("[debug] failed to start runtime debug session:", error);
@@ -1403,6 +1841,7 @@ async function startRuntimeDebugSession() {
1403
1841
  }
1404
1842
  currentConnection = null;
1405
1843
  currentController = null;
1844
+ resetRuntimeDebugVariableMonitor();
1406
1845
  clearBufferedRequestMessages();
1407
1846
  void stopDebugSession();
1408
1847
  };
@@ -1429,6 +1868,10 @@ async function startRuntimeDebugSession() {
1429
1868
  handleRuntimeDebugRequest(socket, sessionId, message);
1430
1869
  return;
1431
1870
  }
1871
+ if (isUiReplaceMessage(message)) {
1872
+ handleRuntimeDebugRequest(socket, sessionId, message);
1873
+ return;
1874
+ }
1432
1875
  if (message.type && message.type !== "session:init") {
1433
1876
  console.debug("[debug] runtime received message:", message);
1434
1877
  }
@@ -1662,49 +2105,31 @@ function useScenario(stories, startName, entryName, goNextOnLoad = false) {
1662
2105
 
1663
2106
  // src/hooks/useSoundEffect.ts
1664
2107
  import { useCallback, useEffect as useEffect2 } from "react";
1665
- var LOADED_SOUNDS = /* @__PURE__ */ new Set();
1666
2108
  function useSoundEffect(src) {
1667
2109
  useEffect2(() => {
1668
- const loadSound = async () => {
1669
- try {
1670
- await executePluginCommand("audio", {
1671
- subCommand: "load",
1672
- name: src,
1673
- src
1674
- });
1675
- } catch (error) {
1676
- console.error(`Failed to load sound effect ${src}:`, error);
1677
- }
1678
- LOADED_SOUNDS.add(src);
1679
- };
1680
- let needRelease = false;
1681
- if (!LOADED_SOUNDS.has(src)) {
1682
- void loadSound();
1683
- needRelease = true;
1684
- }
1685
2110
  return () => {
1686
- const releaseSound = () => {
1687
- try {
2111
+ try {
2112
+ if (src) {
1688
2113
  executePluginCommand("audio", {
1689
2114
  subCommand: "release",
1690
- name: src
2115
+ name: src,
2116
+ silentFail: true
1691
2117
  });
1692
- } catch (error) {
1693
- console.error(`Failed to release sound effect ${src}:`, error);
1694
2118
  }
1695
- LOADED_SOUNDS.delete(src);
1696
- };
1697
- if (needRelease) {
1698
- releaseSound();
2119
+ } catch (error) {
2120
+ console.error(`Failed to release sound effect ${src}:`, error);
1699
2121
  }
1700
2122
  };
1701
2123
  }, [src]);
1702
2124
  return useCallback(() => {
1703
2125
  try {
1704
- executePluginCommand("audio", {
1705
- subCommand: "play",
1706
- name: src
1707
- });
2126
+ if (src) {
2127
+ executePluginCommand("audio", {
2128
+ subCommand: "loadAndPlay",
2129
+ name: src,
2130
+ src
2131
+ });
2132
+ }
1708
2133
  } catch (error) {
1709
2134
  console.error(`Failed to play sound effect ${src}:`, error);
1710
2135
  }
@@ -1713,25 +2138,25 @@ function useSoundEffect(src) {
1713
2138
 
1714
2139
  // src/hooks/useStage.ts
1715
2140
  import { createContext as createContext2, useContext as useContext2, useEffect as useEffect3, createElement, useCallback as useCallback2 } from "react";
1716
- import { proxy as proxy3, useSnapshot as useSnapshot3 } from "valtio";
2141
+ import { proxy as proxy4, useSnapshot as useSnapshot4 } from "valtio";
1717
2142
  var SKIP_INTERVAL_MS = 80;
1718
2143
  var DEFAULT_FAST_FORWARD_STEP_LIMIT = 1e4;
1719
2144
  var DEFAULT_FAST_FORWARD_TIME_LIMIT_MS = 3e3;
1720
2145
  var autoRuntime = {
1721
2146
  defaultTailMs: 0
1722
2147
  };
1723
- var skipState = proxy3({ active: false });
1724
- var autoState = proxy3({ active: false });
1725
- var fastForwardState = proxy3({ active: false });
1726
- var warpState = proxy3({ active: false });
2148
+ var skipState = proxy4({ active: false });
2149
+ var autoState = proxy4({ active: false });
2150
+ var fastForwardState = proxy4({ active: false });
2151
+ var warpState = proxy4({ active: false });
1727
2152
  function useIsSkipping() {
1728
- return useSnapshot3(skipState).active;
2153
+ return useSnapshot4(skipState).active;
1729
2154
  }
1730
2155
  function useIsAutoing() {
1731
- return useSnapshot3(autoState).active;
2156
+ return useSnapshot4(autoState).active;
1732
2157
  }
1733
2158
  function useIsSeeking() {
1734
- return useSnapshot3(fastForwardState).active;
2159
+ return useSnapshot4(fastForwardState).active;
1735
2160
  }
1736
2161
  function getSeekingType() {
1737
2162
  if (warpState.active) return "warp";
@@ -2437,6 +2862,7 @@ function useBeforeHandleCommandCallback(callback) {
2437
2862
  // src/lib.ts
2438
2863
  export * from "@react-spring/core";
2439
2864
  export {
2865
+ DEBUG_EMPTY_PAGE,
2440
2866
  OverlayParamsContext,
2441
2867
  PageParamsContext,
2442
2868
  StageContextProvider,
@@ -2449,6 +2875,7 @@ export {
2449
2875
  createStackNavigator,
2450
2876
  createStage,
2451
2877
  createStaticNavigation,
2878
+ ensureArchiveVariableDefaults,
2452
2879
  executeNodeCommand,
2453
2880
  executePluginCommand,
2454
2881
  fastForwardState,
@@ -2456,11 +2883,16 @@ export {
2456
2883
  getNavigator,
2457
2884
  getSeekingType,
2458
2885
  getStageSize,
2886
+ getUiData,
2459
2887
  globalEventListeners,
2460
2888
  globalRequestAnimationFrameListeners,
2889
+ initGlobalVariableDefaults,
2890
+ initUiData,
2891
+ loadVariablesDocument,
2461
2892
  moyu_exports as moyu,
2462
2893
  nextLine,
2463
2894
  registerAppStateAdapter,
2895
+ replaceUiData,
2464
2896
  setDefaultAutoTailMs,
2465
2897
  setWaiting,
2466
2898
  skipState,
@@ -2487,5 +2919,6 @@ export {
2487
2919
  useSkipCallback,
2488
2920
  useSoundEffect,
2489
2921
  useStageContext,
2922
+ useUiData,
2490
2923
  warpState
2491
2924
  };