@beyondwork/docx-react-component 1.0.135 → 1.0.137

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.
Files changed (62) hide show
  1. package/dist/api/public-types.d.cts +2 -2
  2. package/dist/api/public-types.d.ts +2 -2
  3. package/dist/api/v3.cjs +827 -59
  4. package/dist/api/v3.d.cts +3 -3
  5. package/dist/api/v3.d.ts +3 -3
  6. package/dist/api/v3.js +2 -2
  7. package/dist/{canonical-document-CfZIc-fC.d.cts → canonical-document-COmM7v11.d.cts} +1 -1
  8. package/dist/{canonical-document-CfZIc-fC.d.ts → canonical-document-COmM7v11.d.ts} +1 -1
  9. package/dist/{chunk-EPFVMUKF.js → chunk-M3AEVSKU.js} +793 -56
  10. package/dist/{chunk-5CCYF333.js → chunk-NYIMPM3Z.js} +143 -1
  11. package/dist/{chunk-GIFXKIM5.js → chunk-SMBDQV73.js} +688 -57
  12. package/dist/compare.d.cts +1 -1
  13. package/dist/compare.d.ts +1 -1
  14. package/dist/core/commands/formatting-commands.d.cts +2 -2
  15. package/dist/core/commands/formatting-commands.d.ts +2 -2
  16. package/dist/core/commands/image-commands.d.cts +2 -2
  17. package/dist/core/commands/image-commands.d.ts +2 -2
  18. package/dist/core/commands/section-layout-commands.d.cts +2 -2
  19. package/dist/core/commands/section-layout-commands.d.ts +2 -2
  20. package/dist/core/commands/style-commands.d.cts +2 -2
  21. package/dist/core/commands/style-commands.d.ts +2 -2
  22. package/dist/core/commands/table-structure-commands.d.cts +2 -2
  23. package/dist/core/commands/table-structure-commands.d.ts +2 -2
  24. package/dist/core/commands/text-commands.d.cts +2 -2
  25. package/dist/core/commands/text-commands.d.ts +2 -2
  26. package/dist/core/selection/mapping.d.cts +2 -2
  27. package/dist/core/selection/mapping.d.ts +2 -2
  28. package/dist/core/state/editor-state.d.cts +2 -2
  29. package/dist/core/state/editor-state.d.ts +2 -2
  30. package/dist/index.cjs +1808 -190
  31. package/dist/index.d.cts +5 -5
  32. package/dist/index.d.ts +5 -5
  33. package/dist/index.js +144 -32
  34. package/dist/io/docx-session.d.cts +4 -4
  35. package/dist/io/docx-session.d.ts +4 -4
  36. package/dist/legal.d.cts +1 -1
  37. package/dist/legal.d.ts +1 -1
  38. package/dist/{loader-BQ7AB-0v.d.cts → loader-BB7tRkY2.d.cts} +3 -3
  39. package/dist/{loader-Cy6OYBfn.d.ts → loader-DGPbaboy.d.ts} +3 -3
  40. package/dist/{public-types-D31xKNGc.d.cts → public-types-CIvw1GQa.d.cts} +142 -9
  41. package/dist/{public-types-DqYt8GdP.d.ts → public-types-COCDrgVX.d.ts} +142 -9
  42. package/dist/public-types.d.cts +2 -2
  43. package/dist/public-types.d.ts +2 -2
  44. package/dist/runtime/collab.d.cts +3 -3
  45. package/dist/runtime/collab.d.ts +3 -3
  46. package/dist/runtime/document-runtime.cjs +825 -54
  47. package/dist/runtime/document-runtime.d.cts +2 -2
  48. package/dist/runtime/document-runtime.d.ts +2 -2
  49. package/dist/runtime/document-runtime.js +2 -2
  50. package/dist/{session-DA-F2fCw.d.cts → session-CVU-rtCd.d.cts} +3 -3
  51. package/dist/{session-DqL8H0oZ.d.ts → session-DOqy1-Lf.d.ts} +3 -3
  52. package/dist/session.d.cts +5 -5
  53. package/dist/session.d.ts +5 -5
  54. package/dist/tailwind.d.cts +2 -2
  55. package/dist/tailwind.d.ts +2 -2
  56. package/dist/{types-SllbCtGs.d.ts → types-CB_tOnVp.d.ts} +2 -2
  57. package/dist/{types-B2y94n5t.d.cts → types-CZAll7OS.d.cts} +2 -2
  58. package/dist/ui-tailwind/editor-surface/search-plugin.d.cts +3 -3
  59. package/dist/ui-tailwind/editor-surface/search-plugin.d.ts +3 -3
  60. package/dist/ui-tailwind.d.cts +3 -3
  61. package/dist/ui-tailwind.d.ts +3 -3
  62. package/package.json +1 -1
@@ -18,7 +18,7 @@ import {
18
18
  resolveScopeRange,
19
19
  scopeSpecificity,
20
20
  searchDocument
21
- } from "./chunk-5CCYF333.js";
21
+ } from "./chunk-NYIMPM3Z.js";
22
22
  import {
23
23
  BROADCAST_COMMAND_TYPES,
24
24
  COMMAND_EVENT_SCHEMA_VERSION,
@@ -41,7 +41,8 @@ import {
41
41
  import {
42
42
  chartModelStore,
43
43
  createEditorSurfaceSnapshot,
44
- createFormattingContext
44
+ createFormattingContext,
45
+ storyTargetKey
45
46
  } from "./chunk-EZFF6GKF.js";
46
47
  import {
47
48
  createSelectionSnapshot,
@@ -61,6 +62,7 @@ import {
61
62
  GRADIENT_STOP_UNITS,
62
63
  MAIN_STORY_KEY,
63
64
  collectEditableTargetRefs,
65
+ collectStoryBlockContexts,
64
66
  createHeaderFooterStoryKey,
65
67
  createNoteStoryKey,
66
68
  validateEditableTargetRef
@@ -1329,9 +1331,23 @@ function findSurfaceBlockById(blocks, blockId) {
1329
1331
  }
1330
1332
  function createScopeFromAnchor(runtime, input) {
1331
1333
  const doc = runtime.getCanonicalDocument();
1332
- const storyTarget = input.anchor.storyTarget ?? { kind: "main" };
1334
+ const normalizedInputAnchor = normalizeCreateScopeAnchor(input.anchor);
1335
+ const storyTarget = normalizedInputAnchor.storyTarget ?? { kind: "main" };
1333
1336
  const storyLength = storyTarget.kind === "main" ? computeMainStoryLength(doc) : Number.MAX_SAFE_INTEGER;
1334
- const { from, to } = input.anchor;
1337
+ const { from, to } = normalizedInputAnchor;
1338
+ if (!Number.isFinite(from) || !Number.isFinite(to)) {
1339
+ const diagnosticFrom = Number.isFinite(from) ? from : 0;
1340
+ const diagnosticTo = Number.isFinite(to) ? to : diagnosticFrom;
1341
+ return {
1342
+ status: "range-invalid",
1343
+ reason: "non-finite-range",
1344
+ from: diagnosticFrom,
1345
+ to: diagnosticTo,
1346
+ storyLength,
1347
+ message: `createScopeFromAnchor requires finite numeric from/to offsets. Pass selection.activeRange or an object shaped as {from, to}; do not pass a malformed range.`,
1348
+ nextStep: "pass-selection-activeRange-or-finite-from-to"
1349
+ };
1350
+ }
1335
1351
  if (from < 0) {
1336
1352
  return {
1337
1353
  status: "range-invalid",
@@ -1429,6 +1445,29 @@ function createScopeFromAnchor(runtime, input) {
1429
1445
  }
1430
1446
  return { status: "created", scopeId: result.scopeId, anchor: result.anchor };
1431
1447
  }
1448
+ function normalizeCreateScopeAnchor(anchor) {
1449
+ const candidate = anchor;
1450
+ const activeRange = candidate.activeRange;
1451
+ if (activeRange?.kind === "range") {
1452
+ return {
1453
+ from: activeRange.from,
1454
+ to: activeRange.to,
1455
+ storyTarget: candidate.storyTarget
1456
+ };
1457
+ }
1458
+ if (candidate.kind === "range") {
1459
+ return {
1460
+ from: candidate.from ?? Number.NaN,
1461
+ to: candidate.to ?? Number.NaN,
1462
+ storyTarget: candidate.storyTarget
1463
+ };
1464
+ }
1465
+ return {
1466
+ from: candidate.from ?? Number.NaN,
1467
+ to: candidate.to ?? Number.NaN,
1468
+ storyTarget: candidate.storyTarget
1469
+ };
1470
+ }
1432
1471
 
1433
1472
  // src/api/v3/runtime/workflow.ts
1434
1473
  var createScopeMetadata = {
@@ -1469,6 +1508,29 @@ var createScopeFromAnchorMetadata = {
1469
1508
  broadcastsVia: "crdt",
1470
1509
  rwdReference: "\xA7Runtime API \xA7 runtime.workflow.createScopeFromAnchor. Companion to createScope({blockId}) for sub-block ranges. Consumes from/to once to plant inline scope_marker_start/end; the returned scopeId is the durable reference (KI-P9)."
1471
1510
  };
1511
+ var createScopesMetadata = {
1512
+ name: "runtime.workflow.createScopes",
1513
+ status: "live-with-adapter",
1514
+ sourceLayer: "workflow-review",
1515
+ liveEvidence: {
1516
+ runnerTest: "test/api/v3/workflow-create-scope-live.test.ts",
1517
+ commit: "pending"
1518
+ },
1519
+ uxIntent: {
1520
+ uiVisible: true,
1521
+ expectsUxResponse: "scope-created",
1522
+ expectedDelta: "rail shows new scope chips; editor surface refreshes once after the bulk scope batch"
1523
+ },
1524
+ agentMetadata: {
1525
+ readOrMutate: "mutate",
1526
+ boundedScope: "selection",
1527
+ auditCategory: "scope-creation"
1528
+ },
1529
+ stateClass: "A-canonical",
1530
+ persistsTo: "customXml",
1531
+ broadcastsVia: "crdt",
1532
+ rwdReference: "\xA7Runtime API \xA7 runtime.workflow.createScopes. Bulk companion to createScope/createScopeFromAnchor; resolves blockId/range inputs, plants marker-backed scopes via one runtime addScopes batch, and returns per-item results."
1533
+ };
1472
1534
  var setScopeGuardPolicyMetadata = {
1473
1535
  name: "runtime.workflow.setScopeGuardPolicy",
1474
1536
  status: "live",
@@ -1564,6 +1626,228 @@ var setMarkupModePolicyMetadata = {
1564
1626
  broadcastsVia: "crdt",
1565
1627
  rwdReference: "\xA7Runtime API \xA7 runtime.workflow.setMarkupModePolicy"
1566
1628
  };
1629
+ function isAnchorScopeInput(input) {
1630
+ return "anchor" in input;
1631
+ }
1632
+ function normalizeCreateScopesAnchor(anchor) {
1633
+ const candidate = anchor;
1634
+ const activeRange = candidate.activeRange;
1635
+ if (activeRange?.kind === "range") {
1636
+ return {
1637
+ from: activeRange.from,
1638
+ to: activeRange.to,
1639
+ storyTarget: candidate.storyTarget
1640
+ };
1641
+ }
1642
+ if (candidate.kind === "range") {
1643
+ return {
1644
+ from: candidate.from ?? Number.NaN,
1645
+ to: candidate.to ?? Number.NaN,
1646
+ storyTarget: candidate.storyTarget
1647
+ };
1648
+ }
1649
+ return {
1650
+ from: candidate.from ?? Number.NaN,
1651
+ to: candidate.to ?? Number.NaN,
1652
+ storyTarget: candidate.storyTarget
1653
+ };
1654
+ }
1655
+ function stableRefMetadataFields(stableRefHint) {
1656
+ if (stableRefHint === void 0) return void 0;
1657
+ return [{
1658
+ key: "stableRefHint",
1659
+ valueType: "string",
1660
+ value: stableRefHint
1661
+ }];
1662
+ }
1663
+ function createRangeInvalidResult(inputIndex, reason, from, to, storyLength, detail) {
1664
+ const base = {
1665
+ inputIndex,
1666
+ scopeId: "",
1667
+ status: "range-invalid",
1668
+ reason,
1669
+ from,
1670
+ to,
1671
+ storyLength,
1672
+ ...detail?.blockIndex !== void 0 ? { blockIndex: detail.blockIndex } : {},
1673
+ ...detail?.blockKind !== void 0 ? { blockKind: detail.blockKind } : {}
1674
+ };
1675
+ switch (reason) {
1676
+ case "non-finite-range":
1677
+ return {
1678
+ ...base,
1679
+ message: `createScopes requires finite numeric from/to values (received from=${from}, to=${to}). Pass selection.activeRange or an object shaped as {from, to}.`,
1680
+ nextStep: "pass-selection-activeRange-or-finite-from-to"
1681
+ };
1682
+ case "from-negative":
1683
+ return {
1684
+ ...base,
1685
+ message: `createScopes requires from >= 0 (received from=${from}). Clamp negative values to 0 before calling.`,
1686
+ nextStep: "clamp-from-to-zero"
1687
+ };
1688
+ case "to-less-than-from":
1689
+ return {
1690
+ ...base,
1691
+ message: `createScopes requires to >= from (received from=${from}, to=${to}). Swap the two values before calling.`,
1692
+ nextStep: "swap-from-and-to"
1693
+ };
1694
+ case "range-exceeds-story-length":
1695
+ return {
1696
+ ...base,
1697
+ message: `createScopes requires to <= storyLength (received to=${to}, storyLength=${storyLength}). Re-derive positions from the current document and retry.`,
1698
+ nextStep: "clamp-to-to-storyLength-or-pick-a-different-range"
1699
+ };
1700
+ case "non-paragraph-target":
1701
+ return {
1702
+ ...base,
1703
+ message: `createScopes refused range [${from}, ${to}]: it targets a ${detail?.blockKind ?? "non-paragraph"} block. Pick a paragraph target or use a structural workflow scope.`,
1704
+ nextStep: "pick-a-paragraph-target"
1705
+ };
1706
+ case "empty-document":
1707
+ return {
1708
+ ...base,
1709
+ message: "createScopes refused: the target document has no blocks; initialize a document before creating scopes.",
1710
+ nextStep: "initialize-document-before-creating-scopes"
1711
+ };
1712
+ }
1713
+ }
1714
+ function plantFailureToCreateScopesResult(pending, result) {
1715
+ const status = result.plantStatus;
1716
+ if (!status || status.planted !== false) {
1717
+ return {
1718
+ inputIndex: pending.inputIndex,
1719
+ status: "created",
1720
+ scopeId: result.scopeId
1721
+ };
1722
+ }
1723
+ if (status.reason === "non-paragraph-target") {
1724
+ return createRangeInvalidResult(
1725
+ pending.inputIndex,
1726
+ "non-paragraph-target",
1727
+ pending.from,
1728
+ pending.to,
1729
+ status.storyLength ?? pending.storyLength,
1730
+ {
1731
+ blockIndex: status.blockIndex ?? -1,
1732
+ blockKind: status.blockKind ?? "unknown"
1733
+ }
1734
+ );
1735
+ }
1736
+ if (status.reason === "range-out-of-bounds") {
1737
+ return createRangeInvalidResult(
1738
+ pending.inputIndex,
1739
+ "range-exceeds-story-length",
1740
+ pending.from,
1741
+ pending.to,
1742
+ status.storyLength ?? pending.storyLength
1743
+ );
1744
+ }
1745
+ return createRangeInvalidResult(
1746
+ pending.inputIndex,
1747
+ "empty-document",
1748
+ pending.from,
1749
+ pending.to,
1750
+ status.storyLength ?? pending.storyLength
1751
+ );
1752
+ }
1753
+ function createScopesBulk(runtime, input) {
1754
+ const doc = runtime.getCanonicalDocument();
1755
+ const storyLength = runtime.getRenderSnapshot().surface?.storySize ?? 0;
1756
+ const results = [];
1757
+ const pending = [];
1758
+ input.scopes.forEach((item, inputIndex) => {
1759
+ const assoc = item.assoc ?? { start: 1, end: -1 };
1760
+ const scopeMetadataFields = stableRefMetadataFields(item.stableRefHint);
1761
+ if (isAnchorScopeInput(item)) {
1762
+ const normalizedAnchor = normalizeCreateScopesAnchor(item.anchor);
1763
+ const { from, to } = normalizedAnchor;
1764
+ const itemStoryLength = normalizedAnchor.storyTarget?.kind === void 0 || normalizedAnchor.storyTarget.kind === "main" ? storyLength : Number.MAX_SAFE_INTEGER;
1765
+ if (!Number.isFinite(from) || !Number.isFinite(to)) {
1766
+ results.push(createRangeInvalidResult(inputIndex, "non-finite-range", from, to, itemStoryLength));
1767
+ return;
1768
+ }
1769
+ if (from < 0) {
1770
+ results.push(createRangeInvalidResult(inputIndex, "from-negative", from, to, itemStoryLength));
1771
+ return;
1772
+ }
1773
+ if (to < from) {
1774
+ results.push(createRangeInvalidResult(inputIndex, "to-less-than-from", from, to, itemStoryLength));
1775
+ return;
1776
+ }
1777
+ if (to > itemStoryLength) {
1778
+ results.push(createRangeInvalidResult(inputIndex, "range-exceeds-story-length", from, to, itemStoryLength));
1779
+ return;
1780
+ }
1781
+ const anchor2 = {
1782
+ kind: "range",
1783
+ from,
1784
+ to,
1785
+ assoc
1786
+ };
1787
+ pending.push({
1788
+ inputIndex,
1789
+ request: {
1790
+ anchor: anchor2,
1791
+ scopeId: item.scopeId,
1792
+ mode: item.mode,
1793
+ persistence: item.persistence,
1794
+ metadata: item.metadata,
1795
+ label: item.label,
1796
+ storyTarget: normalizedAnchor.storyTarget ?? { kind: "main" },
1797
+ ...item.visibility ? { visibility: item.visibility } : {},
1798
+ ...item.guardPolicy ? { guardPolicy: item.guardPolicy } : {},
1799
+ ...scopeMetadataFields ? { scopeMetadataFields } : {}
1800
+ },
1801
+ from,
1802
+ to,
1803
+ storyLength: itemStoryLength
1804
+ });
1805
+ return;
1806
+ }
1807
+ const anchor = resolveBlockAnchorFromCanonical(doc, item.blockId, assoc);
1808
+ if (!anchor || anchor.kind !== "range") {
1809
+ results.push({
1810
+ inputIndex,
1811
+ scopeId: "",
1812
+ status: "block-not-found"
1813
+ });
1814
+ return;
1815
+ }
1816
+ pending.push({
1817
+ inputIndex,
1818
+ request: {
1819
+ anchor,
1820
+ scopeId: item.scopeId,
1821
+ mode: item.mode,
1822
+ persistence: item.persistence,
1823
+ metadata: item.metadata,
1824
+ label: item.label,
1825
+ ...item.visibility ? { visibility: item.visibility } : {},
1826
+ ...item.guardPolicy ? { guardPolicy: item.guardPolicy } : {},
1827
+ ...scopeMetadataFields ? { scopeMetadataFields } : {}
1828
+ },
1829
+ from: anchor.from,
1830
+ to: anchor.to,
1831
+ storyLength
1832
+ });
1833
+ });
1834
+ const created = runtime.addScopes(pending.map((item) => item.request));
1835
+ created.forEach((result, index) => {
1836
+ const item = pending[index];
1837
+ if (!item) return;
1838
+ results.push(plantFailureToCreateScopesResult(item, result));
1839
+ });
1840
+ const sorted = results.sort((a, b) => a.inputIndex - b.inputIndex);
1841
+ const createdCount = sorted.filter((item) => item.status === "created").length;
1842
+ const blocked = sorted.length - createdCount;
1843
+ return {
1844
+ status: blocked === 0 ? "created" : createdCount > 0 ? "partial" : "blocked",
1845
+ total: input.scopes.length,
1846
+ created: createdCount,
1847
+ blocked,
1848
+ scopes: Object.freeze(sorted)
1849
+ };
1850
+ }
1567
1851
  function createWorkflowFamily(runtime) {
1568
1852
  return {
1569
1853
  queryScopes(filter) {
@@ -1578,7 +1862,10 @@ function createWorkflowFamily(runtime) {
1578
1862
  createScope(input) {
1579
1863
  const adapterResult = createScopeFromBlockId(runtime, {
1580
1864
  blockId: input.blockId,
1865
+ scopeId: input.scopeId,
1581
1866
  mode: input.mode,
1867
+ persistence: input.persistence,
1868
+ metadata: input.metadata,
1582
1869
  label: input.label,
1583
1870
  ...input.visibility ? { visibility: input.visibility } : {},
1584
1871
  ...input.guardPolicy ? { guardPolicy: input.guardPolicy } : {},
@@ -1612,6 +1899,25 @@ function createWorkflowFamily(runtime) {
1612
1899
  }
1613
1900
  return { scopeId: adapterResult.scopeId, status: "created" };
1614
1901
  },
1902
+ createScopes(input) {
1903
+ const result = createScopesBulk(runtime, input);
1904
+ emitUxResponse(runtime, {
1905
+ apiFn: createScopesMetadata.name,
1906
+ intent: createScopesMetadata.uxIntent.expectedDelta ?? "",
1907
+ mockOrLive: "live",
1908
+ uiVisible: true,
1909
+ expectedDelta: createScopesMetadata.uxIntent.expectedDelta,
1910
+ actualDelta: result.created > 0 ? {
1911
+ kind: "inline-change",
1912
+ payload: {
1913
+ created: result.created,
1914
+ blocked: result.blocked,
1915
+ scopeIds: result.scopes.filter((scope) => scope.status === "created").map((scope) => scope.scopeId)
1916
+ }
1917
+ } : void 0
1918
+ });
1919
+ return result;
1920
+ },
1615
1921
  getScopeGuardPolicy(scopeId) {
1616
1922
  return runtime.getScopeGuardPolicy(scopeId);
1617
1923
  },
@@ -1647,7 +1953,10 @@ function createWorkflowFamily(runtime) {
1647
1953
  createScopeFromAnchor(input) {
1648
1954
  const adapterResult = createScopeFromAnchor(runtime, {
1649
1955
  anchor: input.anchor,
1956
+ scopeId: input.scopeId,
1650
1957
  mode: input.mode,
1958
+ persistence: input.persistence,
1959
+ metadata: input.metadata,
1651
1960
  label: input.label,
1652
1961
  ...input.visibility ? { visibility: input.visibility } : {},
1653
1962
  ...input.guardPolicy ? { guardPolicy: input.guardPolicy } : {},
@@ -3969,7 +4278,8 @@ var SUPPORTED_COMMANDS = [
3969
4278
  "indent",
3970
4279
  "outdent",
3971
4280
  "restart-numbering",
3972
- "continue-numbering"
4281
+ "continue-numbering",
4282
+ "set-value"
3973
4283
  ];
3974
4284
  var UNSUPPORTED_COMMANDS = [
3975
4285
  "create",
@@ -3978,7 +4288,6 @@ var UNSUPPORTED_COMMANDS = [
3978
4288
  "join",
3979
4289
  "separate",
3980
4290
  "split",
3981
- "set-value",
3982
4291
  "apply-template",
3983
4292
  "capture-template",
3984
4293
  "apply-preset",
@@ -4026,11 +4335,17 @@ function createListsFamily(runtime) {
4026
4335
  const resolved = resolveCurrentListTarget(runtime.getCanonicalDocument(), input);
4027
4336
  return resolved.kind === "resolved" ? toReadback(runtime.getCanonicalDocument(), resolved.target, resolved.paragraph) : null;
4028
4337
  },
4338
+ canJoin(input) {
4339
+ return sequencePreflight(runtime.getCanonicalDocument(), input, activeStoryKey(runtime));
4340
+ },
4341
+ canContinuePrevious(input) {
4342
+ return sequencePreflight(runtime.getCanonicalDocument(), input, activeStoryKey(runtime));
4343
+ },
4029
4344
  previewCommand(input) {
4030
- return previewListCommand(runtime.getCanonicalDocument(), input);
4345
+ return previewListCommand(runtime.getCanonicalDocument(), input, activeStoryKey(runtime));
4031
4346
  },
4032
4347
  applyCommand(input) {
4033
- const preview = previewListCommand(runtime.getCanonicalDocument(), input);
4348
+ const preview = previewListCommand(runtime.getCanonicalDocument(), input, activeStoryKey(runtime));
4034
4349
  if (!preview.supported || !preview.target) {
4035
4350
  return {
4036
4351
  applied: false,
@@ -4090,7 +4405,38 @@ function createListsFamily(runtime) {
4090
4405
  }
4091
4406
  };
4092
4407
  }
4093
- function previewListCommand(document, input) {
4408
+ function sequencePreflight(document, input, activeStoryKey2) {
4409
+ const resolved = resolveCurrentListTarget(document, input);
4410
+ if (resolved.kind !== "resolved") {
4411
+ return {
4412
+ canJoin: false,
4413
+ canContinuePrevious: false,
4414
+ blockers: [resolved.blocker]
4415
+ };
4416
+ }
4417
+ const target = toReadback(document, resolved.target, resolved.paragraph);
4418
+ const targetRef = { targetKey: target.targetKey, addressKey: target.addressKey };
4419
+ if (activeStoryKey2 !== void 0 && target.storyKey !== activeStoryKey2) {
4420
+ return {
4421
+ target,
4422
+ canJoin: false,
4423
+ canContinuePrevious: false,
4424
+ blockers: [{
4425
+ code: "list-target-wrong-story",
4426
+ ownerLayer: "L07",
4427
+ message: "List target does not belong to the runtime active story.",
4428
+ ...targetRef
4429
+ }]
4430
+ };
4431
+ }
4432
+ return {
4433
+ target,
4434
+ canJoin: canJoinPreviousSequence(document, resolved.paragraphIndex),
4435
+ canContinuePrevious: canContinuePreviousSequence(document, resolved.paragraphIndex),
4436
+ blockers: []
4437
+ };
4438
+ }
4439
+ function previewListCommand(document, input, activeStoryKey2) {
4094
4440
  const resolved = resolveCurrentListTarget(document, input);
4095
4441
  if (resolved.kind !== "resolved") {
4096
4442
  return {
@@ -4102,6 +4448,20 @@ function previewListCommand(document, input) {
4102
4448
  }
4103
4449
  const target = toReadback(document, resolved.target, resolved.paragraph);
4104
4450
  const targetRef = { targetKey: target.targetKey, addressKey: target.addressKey };
4451
+ if (activeStoryKey2 !== void 0 && target.storyKey !== activeStoryKey2) {
4452
+ return {
4453
+ command: input.command,
4454
+ supported: false,
4455
+ target,
4456
+ affectedTargets: [target],
4457
+ blockers: [{
4458
+ code: "list-target-wrong-story",
4459
+ ownerLayer: "L07",
4460
+ message: "List target does not belong to the runtime active story.",
4461
+ ...targetRef
4462
+ }]
4463
+ };
4464
+ }
4105
4465
  if (!SUPPORTED_COMMANDS.includes(input.command)) {
4106
4466
  return {
4107
4467
  command: input.command,
@@ -4114,6 +4474,17 @@ function previewListCommand(document, input) {
4114
4474
  const canContinuePrevious = canContinuePreviousSequence(document, resolved.paragraphIndex);
4115
4475
  const canJoin = canJoinPreviousSequence(document, resolved.paragraphIndex);
4116
4476
  const blockers = [];
4477
+ if (input.command === "set-value") {
4478
+ const value = input.startAt;
4479
+ if (!Number.isInteger(value) || value === void 0 || value < 1) {
4480
+ blockers.push({
4481
+ code: "list-command-preflight-blocked",
4482
+ ownerLayer: "L07",
4483
+ message: "runtime.lists.set-value requires a positive integer startAt value.",
4484
+ ...targetRef
4485
+ });
4486
+ }
4487
+ }
4117
4488
  if (input.command === "continue-numbering" && !canContinuePrevious) {
4118
4489
  blockers.push({
4119
4490
  code: "list-continue-target-missing",
@@ -4144,6 +4515,7 @@ function editorCommandForListCommand(command, editableTarget, timestamp, startAt
4144
4515
  case "outdent":
4145
4516
  return { type: "list.outdent", editableTargets: [editableTarget], origin };
4146
4517
  case "restart-numbering":
4518
+ case "set-value":
4147
4519
  return {
4148
4520
  type: "list.restart-numbering",
4149
4521
  editableTarget,
@@ -4227,33 +4599,35 @@ function resolveCurrentListTarget(document, input) {
4227
4599
  return { kind: "resolved", ...resolved };
4228
4600
  }
4229
4601
  function currentListTargets(document) {
4230
- const paragraphs = collectParagraphEntries(document.content.children, "main");
4602
+ const paragraphs = collectParagraphEntries(document);
4231
4603
  const targets = collectEditableTargetRefs(document).filter(isListTextTarget);
4232
- const byBlockPath = /* @__PURE__ */ new Map();
4233
- for (const target of targets) byBlockPath.set(target.blockPath, target);
4604
+ const byStoryBlockPath = /* @__PURE__ */ new Map();
4605
+ for (const target of targets) byStoryBlockPath.set(storyBlockPathKey(target.storyKey, target.blockPath), target);
4234
4606
  const out = [];
4235
4607
  for (let paragraphIndex = 0; paragraphIndex < paragraphs.length; paragraphIndex += 1) {
4236
4608
  const entry = paragraphs[paragraphIndex];
4237
4609
  if (!entry?.paragraph.numbering) continue;
4238
- const target = byBlockPath.get(entry.blockPath);
4610
+ const target = byStoryBlockPath.get(storyBlockPathKey(entry.storyKey, entry.blockPath));
4239
4611
  if (!target) continue;
4240
- out.push({ target, paragraph: entry.paragraph, paragraphIndex });
4612
+ out.push({ target, paragraph: entry.paragraph, paragraphIndex, storyKey: entry.storyKey });
4241
4613
  }
4242
4614
  return out;
4243
4615
  }
4244
- function collectParagraphEntries(blocks, basePath) {
4616
+ function collectParagraphEntries(document) {
4245
4617
  const out = [];
4246
- collectParagraphEntriesInto(blocks, basePath, out);
4618
+ for (const context of collectStoryBlockContexts(document)) {
4619
+ collectParagraphEntriesInto(context.blocks, context.storyKey, context.basePath, out);
4620
+ }
4247
4621
  return out;
4248
4622
  }
4249
- function collectParagraphEntriesInto(blocks, basePath, out) {
4623
+ function collectParagraphEntriesInto(blocks, storyKey, basePath, out) {
4250
4624
  for (let blockIndex = 0; blockIndex < blocks.length; blockIndex += 1) {
4251
4625
  const block = blocks[blockIndex];
4252
4626
  if (!block) continue;
4253
4627
  const blockPath = `${basePath}/block[${blockIndex}]`;
4254
4628
  switch (block.type) {
4255
4629
  case "paragraph":
4256
- out.push({ paragraph: block, blockPath });
4630
+ out.push({ paragraph: block, storyKey, blockPath, containerPath: basePath });
4257
4631
  break;
4258
4632
  case "table":
4259
4633
  for (let rowIndex = 0; rowIndex < block.rows.length; rowIndex += 1) {
@@ -4264,6 +4638,7 @@ function collectParagraphEntriesInto(blocks, basePath, out) {
4264
4638
  if (!cell) continue;
4265
4639
  collectParagraphEntriesInto(
4266
4640
  cell.children,
4641
+ storyKey,
4267
4642
  `${blockPath}/row[${rowIndex}]/cell[${cellIndex}]`,
4268
4643
  out
4269
4644
  );
@@ -4271,7 +4646,7 @@ function collectParagraphEntriesInto(blocks, basePath, out) {
4271
4646
  }
4272
4647
  break;
4273
4648
  case "sdt":
4274
- collectParagraphEntriesInto(block.children, blockPath, out);
4649
+ collectParagraphEntriesInto(block.children, storyKey, blockPath, out);
4275
4650
  break;
4276
4651
  case "custom_xml":
4277
4652
  break;
@@ -4315,13 +4690,17 @@ function toReadback(document, target, paragraph) {
4315
4690
  };
4316
4691
  }
4317
4692
  function canContinuePreviousSequence(document, paragraphIndex) {
4318
- const paragraphs = collectParagraphEntries(document.content.children, "main");
4319
- const current = paragraphs[paragraphIndex]?.paragraph;
4693
+ const paragraphs = collectParagraphEntries(document);
4694
+ const currentEntry = paragraphs[paragraphIndex];
4695
+ const current = currentEntry?.paragraph;
4320
4696
  if (!current?.numbering) return false;
4321
4697
  const currentKind = getListKind(document.numbering, current.numbering.numberingInstanceId);
4322
4698
  if (!currentKind) return false;
4323
4699
  for (let index = paragraphIndex - 1; index >= 0; index -= 1) {
4324
- const previous = paragraphs[index]?.paragraph;
4700
+ const previousEntry = paragraphs[index];
4701
+ if (previousEntry?.storyKey !== currentEntry.storyKey) break;
4702
+ if (previousEntry.containerPath !== currentEntry.containerPath) break;
4703
+ const previous = previousEntry.paragraph;
4325
4704
  if (!previous?.numbering) continue;
4326
4705
  const previousKind = getListKind(document.numbering, previous.numbering.numberingInstanceId);
4327
4706
  return previousKind === currentKind && previous.numbering.numberingInstanceId !== current.numbering.numberingInstanceId;
@@ -4329,14 +4708,26 @@ function canContinuePreviousSequence(document, paragraphIndex) {
4329
4708
  return false;
4330
4709
  }
4331
4710
  function canJoinPreviousSequence(document, paragraphIndex) {
4332
- const paragraphs = collectParagraphEntries(document.content.children, "main");
4333
- const current = paragraphs[paragraphIndex]?.paragraph;
4334
- const previous = paragraphs[paragraphIndex - 1]?.paragraph;
4711
+ const paragraphs = collectParagraphEntries(document);
4712
+ const currentEntry = paragraphs[paragraphIndex];
4713
+ const previousEntry = paragraphs[paragraphIndex - 1];
4714
+ if (previousEntry?.storyKey !== currentEntry?.storyKey) return false;
4715
+ if (previousEntry?.containerPath !== currentEntry?.containerPath) return false;
4716
+ const current = currentEntry?.paragraph;
4717
+ const previous = previousEntry?.paragraph;
4335
4718
  if (!current?.numbering || !previous?.numbering) return false;
4336
4719
  const currentKind = getListKind(document.numbering, current.numbering.numberingInstanceId);
4337
4720
  const previousKind = getListKind(document.numbering, previous.numbering.numberingInstanceId);
4338
4721
  return Boolean(currentKind) && currentKind === previousKind && current.numbering.numberingInstanceId !== previous.numbering.numberingInstanceId;
4339
4722
  }
4723
+ function storyBlockPathKey(storyKey, blockPath) {
4724
+ return `${storyKey}
4725
+ ${blockPath}`;
4726
+ }
4727
+ function activeStoryKey(runtime) {
4728
+ const activeStory = runtime.getRenderSnapshot()?.activeStory;
4729
+ return activeStory ? storyTargetKey(activeStory) : void 0;
4730
+ }
4340
4731
  function unsupportedCommandBlocker(command, target) {
4341
4732
  return {
4342
4733
  code: "list-command-unsupported",
@@ -8631,33 +9022,51 @@ function createPlaceholderScopes(runtime, input) {
8631
9022
  blockerDetails: Object.freeze([detail])
8632
9023
  };
8633
9024
  }
8634
- const compiler = createScopeCompilerService(runtime);
8635
9025
  const byOriginalIndex = /* @__PURE__ */ new Map();
8636
- for (const item of orderedHits) {
9026
+ const createdScopeIds = [];
9027
+ const scopeRequests = orderedHits.map((item) => {
8637
9028
  const text = item.text || query;
8638
9029
  const label = input.labelPrefix === void 0 ? text : `${input.labelPrefix} ${item.originalIndex + 1}`;
8639
- const result = createScopeFromAnchor(runtime, {
8640
- anchor: { from: item.hit.from, to: item.hit.to },
9030
+ return {
9031
+ anchor: {
9032
+ kind: "range",
9033
+ from: item.hit.from,
9034
+ to: item.hit.to,
9035
+ assoc: input.assoc ?? { start: 1, end: -1 }
9036
+ },
8641
9037
  mode: input.mode ?? "edit",
8642
9038
  label,
8643
9039
  ...input.visibility ? { visibility: input.visibility } : {},
8644
9040
  ...input.guardPolicy ? { guardPolicy: input.guardPolicy } : {},
8645
9041
  ...input.assoc ? { assoc: input.assoc } : {},
8646
- ...input.stableRefHint ? { stableRefHint: input.stableRefHint } : {}
8647
- });
8648
- if (result.status === "created") {
8649
- const compiled = compiler.compileScopeById(result.scopeId)?.scope;
9042
+ ...input.stableRefHint ? {
9043
+ scopeMetadataFields: [
9044
+ {
9045
+ key: "stableRefHint",
9046
+ valueType: "string",
9047
+ value: input.stableRefHint
9048
+ }
9049
+ ]
9050
+ } : {}
9051
+ };
9052
+ });
9053
+ const scopeResults = runtime.addScopes(scopeRequests);
9054
+ orderedHits.forEach((item, requestIndex) => {
9055
+ const text = item.text || query;
9056
+ const result = scopeResults[requestIndex];
9057
+ if (result && result.scopeId && result.plantStatus?.planted !== false) {
9058
+ createdScopeIds.push(result.scopeId);
8650
9059
  byOriginalIndex.set(item.originalIndex, {
8651
9060
  status: "created",
8652
9061
  text,
8653
9062
  excerpt: excerpt(text),
8654
- scopeId: result.scopeId,
8655
- ...compiled?.handle ? { handle: compiled.handle } : {}
9063
+ scopeId: result.scopeId
8656
9064
  });
8657
- continue;
9065
+ return;
8658
9066
  }
9067
+ const reason = result?.plantStatus?.reason ?? "unknown";
8659
9068
  const detail = blockerWithOwner(
8660
- `actions:placeholder-scopes:create-refused:${result.reason}`,
9069
+ `actions:placeholder-scopes:create-refused:${reason}`,
8661
9070
  "blocked",
8662
9071
  "The placeholder match could not be converted into a workflow scope.",
8663
9072
  "Refresh the document and retry; if it still refuses, route the target to the workflow/scope writer owner.",
@@ -8670,6 +9079,38 @@ function createPlaceholderScopes(runtime, input) {
8670
9079
  blockers: Object.freeze([detail.code]),
8671
9080
  blockerDetails: Object.freeze([detail])
8672
9081
  });
9082
+ });
9083
+ if (createdScopeIds.length > 0) {
9084
+ const createdScopeIdSet = new Set(createdScopeIds);
9085
+ const handlesByScopeId = new Map(
9086
+ createScopeCompilerService(runtime).compileAllScopes().filter((scope) => createdScopeIdSet.has(scope.handle.scopeId)).map((scope) => [scope.handle.scopeId, scope.handle])
9087
+ );
9088
+ for (const [originalIndex, item] of byOriginalIndex) {
9089
+ if (item.status !== "created" || !item.scopeId) continue;
9090
+ const handle = handlesByScopeId.get(item.scopeId);
9091
+ if (!handle) {
9092
+ const detail = blockerWithOwner(
9093
+ `actions:placeholder-scopes:create-unresolved:${item.scopeId}`,
9094
+ "blocked",
9095
+ "The placeholder scope marker was planted but did not enumerate as a current semantic scope handle.",
9096
+ "Treat this target as unresolved, refresh scope enumeration, and route persistent misses to the semantic-scope compiler owner.",
9097
+ "L08"
9098
+ );
9099
+ byOriginalIndex.set(originalIndex, {
9100
+ status: "blocked",
9101
+ text: item.text,
9102
+ excerpt: item.excerpt,
9103
+ scopeId: item.scopeId,
9104
+ blockers: Object.freeze([detail.code]),
9105
+ blockerDetails: Object.freeze([detail])
9106
+ });
9107
+ continue;
9108
+ }
9109
+ byOriginalIndex.set(originalIndex, {
9110
+ ...item,
9111
+ handle
9112
+ });
9113
+ }
8673
9114
  }
8674
9115
  const scopes = orderedHits.map((item) => byOriginalIndex.get(item.originalIndex)).filter((value) => value !== void 0);
8675
9116
  const created = scopes.filter((scope) => scope.status === "created").length;
@@ -9132,6 +9573,9 @@ function applyRewrite(runtime, target, input) {
9132
9573
  if (target.kind === "editable-text") {
9133
9574
  return applyEditableTextRewrite(runtime, target, input);
9134
9575
  }
9576
+ if (target.handle.provenance === "marker-backed" && (target.scope.kind === "paragraph" || target.scope.kind === "list-item")) {
9577
+ return applyMarkerBackedRangeRewrite(runtime, target, input);
9578
+ }
9135
9579
  if (target.scope.kind === "list-item") {
9136
9580
  const listTextTarget = uniqueListTextActionForScope(runtime, target.handle);
9137
9581
  if (!listTextTarget.ok) return blockedApplyFromResolution(listTextTarget);
@@ -9278,13 +9722,97 @@ function applyEditableTextRewrite(runtime, target, input) {
9278
9722
  afterReadback
9279
9723
  };
9280
9724
  }
9725
+ function applyMarkerBackedRangeRewrite(runtime, target, input) {
9726
+ const range = buildScopePositionMap(runtime.getCanonicalDocument()).markerScopes.get(
9727
+ target.handle.scopeId
9728
+ );
9729
+ if (!range) {
9730
+ return blockedApply(
9731
+ `actions:rewrite:marker-range-unresolved:${target.handle.scopeId}`,
9732
+ "unresolved-target",
9733
+ "The marker-backed scope could not be joined to its current marker range.",
9734
+ "Refresh placeholder scopes and retry; route persistent failures to L06 marker scope tracking."
9735
+ );
9736
+ }
9737
+ const beforeReadback = markerScopeReadback(runtime, target.handle.scopeId) ?? {
9738
+ text: target.scope.content.text,
9739
+ isEmpty: target.scope.content.text.length === 0
9740
+ };
9741
+ const before = runtime.getCanonicalDocument();
9742
+ const origin = actionOrigin(runtime, input);
9743
+ runtime.dispatch({
9744
+ type: "selection.set",
9745
+ selection: {
9746
+ anchor: range.from,
9747
+ head: range.to,
9748
+ isCollapsed: range.from === range.to,
9749
+ activeRange: {
9750
+ kind: "range",
9751
+ from: range.from,
9752
+ to: range.to,
9753
+ assoc: { start: -1, end: 1 }
9754
+ }
9755
+ },
9756
+ origin
9757
+ });
9758
+ const ack = runtime.applyActiveStoryTextCommand({
9759
+ type: "text.insert",
9760
+ text: input.text,
9761
+ origin
9762
+ });
9763
+ const changed = runtime.getCanonicalDocument() !== before;
9764
+ const afterReadback = markerScopeReadback(runtime, target.handle.scopeId);
9765
+ const compiledAfter = createScopeCompilerService(runtime).compileScopeById(target.handle.scopeId);
9766
+ if (!changed || afterReadback?.text !== input.text) {
9767
+ return {
9768
+ status: "blocked",
9769
+ applied: false,
9770
+ changed,
9771
+ target: summarizeTarget(
9772
+ compiledAfter ? { ...target, scope: compiledAfter.scope, handle: compiledAfter.scope.handle } : target
9773
+ ),
9774
+ posture: "suspect-readback",
9775
+ blockers: Object.freeze([
9776
+ `actions:rewrite:marker-range-readback-mismatch:${target.handle.scopeId}`
9777
+ ]),
9778
+ blockerDetails: Object.freeze([
9779
+ blocker(
9780
+ `actions:rewrite:marker-range-readback-mismatch:${target.handle.scopeId}`,
9781
+ "blocked",
9782
+ ack.kind === "rejected" ? "The marker-backed rewrite command was rejected or did not produce the requested marker readback." : "The marker-backed rewrite command did not produce the requested marker readback.",
9783
+ "Treat the mutation as suspect. Re-read the target and export before claiming success."
9784
+ )
9785
+ ]),
9786
+ ...afterReadback ? { afterReadback } : {}
9787
+ };
9788
+ }
9789
+ return {
9790
+ status: "applied",
9791
+ applied: true,
9792
+ changed: true,
9793
+ target: summarizeTarget(
9794
+ compiledAfter ? { ...target, scope: compiledAfter.scope, handle: compiledAfter.scope.handle } : target
9795
+ ),
9796
+ commandReference: {
9797
+ command: "text.insert",
9798
+ actorId: input.actorId ?? "v3-ai-api",
9799
+ origin: input.origin ?? "agent",
9800
+ emittedAtUtc: currentAuditTimestamp(runtime)
9801
+ },
9802
+ beforeReadback,
9803
+ afterReadback
9804
+ };
9805
+ }
9281
9806
  function applyTableScopedMarkerRewrite(runtime, target, action, input) {
9282
9807
  const workflowScope = runtime.getScope(target.handle.scopeId);
9808
+ const markerRange = buildScopePositionMap(runtime.getCanonicalDocument()).markerScopes.get(
9809
+ target.handle.scopeId
9810
+ );
9283
9811
  const editableTarget = tableEditableTargetForActionHandle(
9284
9812
  runtime,
9285
9813
  action.actionHandle
9286
9814
  );
9287
- if (!workflowScope || workflowScope.anchor.kind !== "range" || !editableTarget) {
9815
+ if (!workflowScope || !markerRange || !editableTarget) {
9288
9816
  return blockedApply(
9289
9817
  `actions:rewrite:table-marker-target-unresolved:${target.handle.scopeId}`,
9290
9818
  "unresolved-target",
@@ -9292,19 +9820,23 @@ function applyTableScopedMarkerRewrite(runtime, target, action, input) {
9292
9820
  "Refresh placeholder scopes and retry; route persistent failures to L08 table scope target mapping."
9293
9821
  );
9294
9822
  }
9823
+ const beforeReadback = markerScopeReadback(runtime, target.handle.scopeId) ?? {
9824
+ text: target.scope.content.text,
9825
+ isEmpty: target.scope.content.text.length === 0
9826
+ };
9295
9827
  const before = runtime.getCanonicalDocument();
9296
9828
  try {
9297
9829
  runtime.dispatch({
9298
9830
  type: "selection.set",
9299
9831
  selection: {
9300
- anchor: workflowScope.anchor.from,
9301
- head: workflowScope.anchor.to,
9302
- isCollapsed: workflowScope.anchor.from === workflowScope.anchor.to,
9832
+ anchor: markerRange.from,
9833
+ head: markerRange.to,
9834
+ isCollapsed: markerRange.from === markerRange.to,
9303
9835
  activeRange: {
9304
9836
  kind: "range",
9305
- from: workflowScope.anchor.from,
9306
- to: workflowScope.anchor.to,
9307
- assoc: workflowScope.anchor.assoc ?? { start: -1, end: 1 }
9837
+ from: markerRange.from,
9838
+ to: markerRange.to,
9839
+ assoc: { start: -1, end: 1 }
9308
9840
  }
9309
9841
  },
9310
9842
  origin: actionOrigin(runtime, input)
@@ -9339,12 +9871,19 @@ function applyTableScopedMarkerRewrite(runtime, target, action, input) {
9339
9871
  );
9340
9872
  const afterText = paragraph ? collectInlineText3(paragraph.children) : void 0;
9341
9873
  const afterReadback = afterText === void 0 ? void 0 : { text: afterText, isEmpty: afterText.length === 0 };
9342
- if (!changed || afterReadback?.text !== input.text) {
9874
+ const compiledAfter = createScopeCompilerService(runtime).compileScopeById(target.handle.scopeId);
9875
+ const scopeReadback = markerScopeReadback(runtime, target.handle.scopeId) ?? (compiledAfter ? {
9876
+ text: compiledAfter.scope.content.text,
9877
+ isEmpty: compiledAfter.scope.content.text.length === 0
9878
+ } : void 0);
9879
+ if (!changed || scopeReadback?.text !== input.text) {
9343
9880
  return {
9344
9881
  status: "blocked",
9345
9882
  applied: false,
9346
9883
  changed,
9347
- target: summarizeTarget({ kind: "table-text", action }),
9884
+ target: summarizeTarget(
9885
+ compiledAfter ? { ...target, scope: compiledAfter.scope, handle: compiledAfter.scope.handle } : target
9886
+ ),
9348
9887
  posture: "suspect-readback",
9349
9888
  blockers: Object.freeze([
9350
9889
  `actions:rewrite:table-marker-readback-mismatch:${target.handle.scopeId}`
@@ -9357,25 +9896,24 @@ function applyTableScopedMarkerRewrite(runtime, target, action, input) {
9357
9896
  "Treat the mutation as suspect. Re-read the target and export before claiming success."
9358
9897
  )
9359
9898
  ]),
9360
- ...afterReadback ? { afterReadback } : {}
9899
+ ...scopeReadback ? { afterReadback: scopeReadback } : afterReadback ? { afterReadback } : {}
9361
9900
  };
9362
9901
  }
9363
9902
  return {
9364
9903
  status: "applied",
9365
9904
  applied: true,
9366
9905
  changed: true,
9367
- target: summarizeTarget({
9368
- kind: "table-text",
9369
- action: { ...action, readback: afterReadback }
9370
- }),
9906
+ target: summarizeTarget(
9907
+ compiledAfter ? { ...target, scope: compiledAfter.scope, handle: compiledAfter.scope.handle } : { kind: "table-text", action: { ...action, readback: scopeReadback } }
9908
+ ),
9371
9909
  commandReference: {
9372
9910
  command: "text.insert",
9373
9911
  actorId: input.actorId ?? "v3-ai-api",
9374
9912
  origin: input.origin ?? "agent",
9375
9913
  emittedAtUtc: currentAuditTimestamp(runtime)
9376
9914
  },
9377
- ...action.readback ? { beforeReadback: action.readback } : {},
9378
- afterReadback
9915
+ beforeReadback,
9916
+ afterReadback: scopeReadback
9379
9917
  };
9380
9918
  }
9381
9919
  function projectRewriteScopeResult(runtime, result, target, beforeText, proposedText, documentMutated) {
@@ -9469,7 +10007,7 @@ function summarizeTarget(target) {
9469
10007
  if (target.kind === "editable-text") {
9470
10008
  return {
9471
10009
  kind: target.targetKind,
9472
- handle: target.ownerHandle,
10010
+ handle: publicActionScopeHandle(target.ownerHandle),
9473
10011
  actionHandle: target.actionHandle,
9474
10012
  readback: target.readback,
9475
10013
  canRewriteText: true,
@@ -9480,13 +10018,21 @@ function summarizeTarget(target) {
9480
10018
  }
9481
10019
  return {
9482
10020
  kind: target.scope.kind,
9483
- handle: target.handle,
10021
+ handle: publicActionScopeHandle(target.handle),
9484
10022
  canRewriteText: canRewriteScopeText(target.scope),
9485
10023
  canInsertAdjacentText: canInsertAdjacentScopeText(target.scope),
9486
10024
  canFlag: true,
9487
10025
  canMark: canMarkScope(target.scope)
9488
10026
  };
9489
10027
  }
10028
+ function publicActionScopeHandle(handle) {
10029
+ const parentScopeId = handle.parentScopeId;
10030
+ if (!parentScopeId || !/^(cell|table|row|column|span):/u.test(parentScopeId)) {
10031
+ return handle;
10032
+ }
10033
+ const { parentScopeId: _parentScopeId, ...rest } = handle;
10034
+ return rest;
10035
+ }
9490
10036
  function tableTextActionsForScope(runtime, handle) {
9491
10037
  const result = createTableActionFamily(runtime).listTableActions({
9492
10038
  handle,
@@ -9539,6 +10085,13 @@ function tableEditableTargetForActionHandle(runtime, actionHandle) {
9539
10085
  }
9540
10086
  return null;
9541
10087
  }
10088
+ function markerScopeReadback(runtime, scopeId) {
10089
+ const document = runtime.getCanonicalDocument();
10090
+ const range = buildScopePositionMap(document).markerScopes.get(scopeId);
10091
+ if (!range) return null;
10092
+ const text = textForCanonicalRange(document, range.from, range.to);
10093
+ return { text, isEmpty: text.length === 0 };
10094
+ }
9542
10095
  function editableTextActionsForScope(runtime, handle) {
9543
10096
  const bundle = createScopeCompilerService(runtime).compileBundleById(
9544
10097
  handle.scopeId,
@@ -10891,6 +11444,86 @@ function blockTextLength(block) {
10891
11444
  return 0;
10892
11445
  }
10893
11446
  }
11447
+ function textForCanonicalRange(document, from, to) {
11448
+ let cursor = 0;
11449
+ let text = "";
11450
+ const append = (value, start, end) => {
11451
+ const clippedFrom = Math.max(from, start);
11452
+ const clippedTo = Math.min(to, end);
11453
+ if (clippedTo <= clippedFrom) return;
11454
+ const localFrom = clippedFrom - start;
11455
+ const localTo = clippedTo - start;
11456
+ text += Array.from(value).slice(localFrom, localTo).join("");
11457
+ };
11458
+ const walkInline = (inline) => {
11459
+ const start = cursor;
11460
+ switch (inline.type) {
11461
+ case "text": {
11462
+ const length = Array.from(inline.text).length;
11463
+ cursor += length;
11464
+ append(inline.text, start, cursor);
11465
+ return length;
11466
+ }
11467
+ case "tab":
11468
+ cursor += 1;
11469
+ append(" ", start, cursor);
11470
+ return 1;
11471
+ case "hard_break":
11472
+ cursor += 1;
11473
+ append("\n", start, cursor);
11474
+ return 1;
11475
+ case "hyperlink":
11476
+ case "field": {
11477
+ const before = cursor;
11478
+ for (const child of inline.children) {
11479
+ walkInline(child);
11480
+ }
11481
+ return cursor - before;
11482
+ }
11483
+ case "bookmark_start":
11484
+ case "bookmark_end":
11485
+ case "scope_marker_start":
11486
+ case "scope_marker_end":
11487
+ return 0;
11488
+ default:
11489
+ cursor += 1;
11490
+ return 1;
11491
+ }
11492
+ };
11493
+ const walkBlock = (block) => {
11494
+ if (!block) return 0;
11495
+ const before = cursor;
11496
+ switch (block.type) {
11497
+ case "paragraph":
11498
+ for (const child of block.children) walkInline(child);
11499
+ break;
11500
+ case "table":
11501
+ for (const row of block.rows) {
11502
+ for (const cell of row.cells) {
11503
+ for (const child of cell.children) walkBlock(child);
11504
+ }
11505
+ }
11506
+ break;
11507
+ case "sdt":
11508
+ case "custom_xml":
11509
+ for (const child of block.children) walkBlock(child);
11510
+ break;
11511
+ default:
11512
+ cursor += 1;
11513
+ break;
11514
+ }
11515
+ return cursor - before;
11516
+ };
11517
+ const blocks = document.content.children;
11518
+ for (let index = 0; index < blocks.length; index += 1) {
11519
+ walkBlock(blocks[index]);
11520
+ if (index < blocks.length - 1 && blocks[index + 1]?.type === "paragraph") {
11521
+ cursor += 1;
11522
+ append("\n", cursor - 1, cursor);
11523
+ }
11524
+ }
11525
+ return text;
11526
+ }
10894
11527
  function inlineTextLength(inline) {
10895
11528
  if (!inline) return 0;
10896
11529
  switch (inline.type) {
@@ -11046,6 +11679,109 @@ function summarizePlanResult(mode, steps) {
11046
11679
  };
11047
11680
  }
11048
11681
 
11682
+ // src/api/v3/ai/agent.ts
11683
+ function createAgentFamily(runtime, pe2Evidence) {
11684
+ const inspect = createInspectFamily(runtime, pe2Evidence);
11685
+ const resolve = createResolveFamily(runtime);
11686
+ const bundle = createBundleFamily(runtime, pe2Evidence);
11687
+ const actions = createActionsFamily(runtime).actions;
11688
+ const table = createTableActionFamily(runtime);
11689
+ const object = createObjectActionFamily(runtime);
11690
+ const policy = createPolicyFamily(runtime);
11691
+ const evaluate = createEvaluateFamily(runtime);
11692
+ const outline = createOutlineFamily(runtime);
11693
+ const stats = createStatsFamily(runtime);
11694
+ const explain = createExplainFamily(runtime);
11695
+ const review = createReviewFamily2(runtime);
11696
+ const exportFamily = createExportFamily(runtime);
11697
+ const read = Object.freeze({
11698
+ inspectDocument: inspect.inspectDocument,
11699
+ listScopes: inspect.listScopes,
11700
+ getScope: bundle.getScope,
11701
+ getScopeBundle: bundle.getScopeBundle,
11702
+ resolveReference: resolve.resolveReference,
11703
+ queryScopeAtPosition: resolve.queryScopeAtPosition,
11704
+ queryScopeInRange: resolve.queryScopeInRange,
11705
+ brief: actions.brief,
11706
+ getDocumentOutline: outline.getDocumentOutline,
11707
+ getDocumentStatistics: stats.getDocumentStatistics,
11708
+ explainFormatting: explain.explainFormatting
11709
+ });
11710
+ const target = Object.freeze({
11711
+ discover: actions.discover,
11712
+ locate: actions.locate,
11713
+ locateAll: actions.locateAll,
11714
+ createPlaceholderScopes: actions.createPlaceholderScopes
11715
+ });
11716
+ const edit = Object.freeze({
11717
+ rewrite: actions.rewrite,
11718
+ runPlan: actions.runPlan,
11719
+ insertText: actions.insertText,
11720
+ mark: actions.mark,
11721
+ flag: actions.flag,
11722
+ flagMany: actions.flagMany,
11723
+ fieldRefresh: actions.fieldRefresh,
11724
+ tocRefresh: actions.tocRefresh,
11725
+ bookmarkEdit: actions.bookmarkEdit,
11726
+ hyperlinkDestinationEdit: actions.hyperlinkDestinationEdit,
11727
+ hyperlinkTextEdit: actions.hyperlinkTextEdit
11728
+ });
11729
+ const template = Object.freeze({
11730
+ validateTargets: actions.validateTemplateTargets,
11731
+ readTarget: actions.templateTargetRead,
11732
+ fillField: actions.templateFieldFill
11733
+ });
11734
+ const tableGroup = Object.freeze({
11735
+ replaceContent: actions.tableContent,
11736
+ applyFragment: actions.tableFragment,
11737
+ applySelection: actions.tableSelection,
11738
+ listActions: table.listTableActions,
11739
+ applyAction: table.applyTableAction
11740
+ });
11741
+ const list = Object.freeze({
11742
+ applyOperation: actions.listOperation
11743
+ });
11744
+ const objectGroup = Object.freeze({
11745
+ listActions: object.listObjectActions,
11746
+ applyAction: object.applyObjectAction
11747
+ });
11748
+ const reviewGroup = Object.freeze({
11749
+ acceptRevision: review.acceptRevision,
11750
+ rejectRevision: review.rejectRevision,
11751
+ resolveCommentThread: review.resolveCommentThread,
11752
+ resolveIssue: review.resolveIssue,
11753
+ reopenIssue: review.reopenIssue
11754
+ });
11755
+ const policyGroup = Object.freeze({
11756
+ getPolicy: policy.getPolicy,
11757
+ listActions: policy.listAIActions,
11758
+ evaluateAction: evaluate.evaluateAction
11759
+ });
11760
+ const exportGroup = Object.freeze({
11761
+ reviewedDocument: exportFamily.exportReviewedDocument
11762
+ });
11763
+ const category = Object.freeze({
11764
+ read,
11765
+ target,
11766
+ edit,
11767
+ template,
11768
+ table: tableGroup,
11769
+ list,
11770
+ object: objectGroup,
11771
+ review: reviewGroup,
11772
+ policy: policyGroup,
11773
+ export: exportGroup
11774
+ });
11775
+ const family = {
11776
+ agent() {
11777
+ return category;
11778
+ }
11779
+ };
11780
+ return {
11781
+ agent: family.agent()
11782
+ };
11783
+ }
11784
+
11049
11785
  // src/api/v3/_create.ts
11050
11786
  function createApiV3(handle, opts) {
11051
11787
  const ai = {
@@ -11063,7 +11799,8 @@ function createApiV3(handle, opts) {
11063
11799
  ...createOutlineFamily(handle),
11064
11800
  ...createTableActionFamily(handle),
11065
11801
  ...createObjectActionFamily(handle),
11066
- ...createActionsFamily(handle)
11802
+ ...createActionsFamily(handle),
11803
+ ...createAgentFamily(handle, opts?.pe2Evidence)
11067
11804
  };
11068
11805
  const runtime = {
11069
11806
  document: createDocumentFamily(handle),