@beyondwork/docx-react-component 1.0.136 → 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 (56) hide show
  1. package/dist/api/public-types.d.cts +1 -1
  2. package/dist/api/public-types.d.ts +1 -1
  3. package/dist/api/v3.cjs +645 -21
  4. package/dist/api/v3.d.cts +2 -2
  5. package/dist/api/v3.d.ts +2 -2
  6. package/dist/api/v3.js +2 -2
  7. package/dist/{chunk-NX7W6T7L.js → chunk-M3AEVSKU.js} +626 -21
  8. package/dist/{chunk-HUWZ7AHE.js → chunk-NYIMPM3Z.js} +20 -1
  9. package/dist/{chunk-JZZKTL7K.js → chunk-SMBDQV73.js} +141 -80
  10. package/dist/core/commands/formatting-commands.d.cts +1 -1
  11. package/dist/core/commands/formatting-commands.d.ts +1 -1
  12. package/dist/core/commands/image-commands.d.cts +1 -1
  13. package/dist/core/commands/image-commands.d.ts +1 -1
  14. package/dist/core/commands/section-layout-commands.d.cts +1 -1
  15. package/dist/core/commands/section-layout-commands.d.ts +1 -1
  16. package/dist/core/commands/style-commands.d.cts +1 -1
  17. package/dist/core/commands/style-commands.d.ts +1 -1
  18. package/dist/core/commands/table-structure-commands.d.cts +1 -1
  19. package/dist/core/commands/table-structure-commands.d.ts +1 -1
  20. package/dist/core/commands/text-commands.d.cts +1 -1
  21. package/dist/core/commands/text-commands.d.ts +1 -1
  22. package/dist/core/selection/mapping.d.cts +1 -1
  23. package/dist/core/selection/mapping.d.ts +1 -1
  24. package/dist/core/state/editor-state.d.cts +1 -1
  25. package/dist/core/state/editor-state.d.ts +1 -1
  26. package/dist/index.cjs +808 -111
  27. package/dist/index.d.cts +4 -4
  28. package/dist/index.d.ts +4 -4
  29. package/dist/index.js +26 -14
  30. package/dist/io/docx-session.d.cts +3 -3
  31. package/dist/io/docx-session.d.ts +3 -3
  32. package/dist/{loader-Cr35kVUi.d.cts → loader-BB7tRkY2.d.cts} +2 -2
  33. package/dist/{loader-uGY6nDZP.d.ts → loader-DGPbaboy.d.ts} +2 -2
  34. package/dist/{public-types-CSSH2Whc.d.cts → public-types-CIvw1GQa.d.cts} +126 -1
  35. package/dist/{public-types-8kVIB5HW.d.ts → public-types-COCDrgVX.d.ts} +126 -1
  36. package/dist/public-types.d.cts +1 -1
  37. package/dist/public-types.d.ts +1 -1
  38. package/dist/runtime/collab.d.cts +2 -2
  39. package/dist/runtime/collab.d.ts +2 -2
  40. package/dist/runtime/document-runtime.cjs +160 -80
  41. package/dist/runtime/document-runtime.d.cts +1 -1
  42. package/dist/runtime/document-runtime.d.ts +1 -1
  43. package/dist/runtime/document-runtime.js +2 -2
  44. package/dist/{session-Beg8ihOi.d.cts → session-CVU-rtCd.d.cts} +2 -2
  45. package/dist/{session-Dgp-2uYw.d.ts → session-DOqy1-Lf.d.ts} +2 -2
  46. package/dist/session.d.cts +4 -4
  47. package/dist/session.d.ts +4 -4
  48. package/dist/tailwind.d.cts +1 -1
  49. package/dist/tailwind.d.ts +1 -1
  50. package/dist/{types-Cin_abw3.d.ts → types-CB_tOnVp.d.ts} +1 -1
  51. package/dist/{types-qsS39ZkQ.d.cts → types-CZAll7OS.d.cts} +1 -1
  52. package/dist/ui-tailwind/editor-surface/search-plugin.d.cts +2 -2
  53. package/dist/ui-tailwind/editor-surface/search-plugin.d.ts +2 -2
  54. package/dist/ui-tailwind.d.cts +2 -2
  55. package/dist/ui-tailwind.d.ts +2 -2
  56. package/package.json +1 -1
@@ -18,7 +18,7 @@ import {
18
18
  resolveScopeRange,
19
19
  scopeSpecificity,
20
20
  searchDocument
21
- } from "./chunk-HUWZ7AHE.js";
21
+ } from "./chunk-NYIMPM3Z.js";
22
22
  import {
23
23
  BROADCAST_COMMAND_TYPES,
24
24
  COMMAND_EVENT_SCHEMA_VERSION,
@@ -1508,6 +1508,29 @@ var createScopeFromAnchorMetadata = {
1508
1508
  broadcastsVia: "crdt",
1509
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)."
1510
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
+ };
1511
1534
  var setScopeGuardPolicyMetadata = {
1512
1535
  name: "runtime.workflow.setScopeGuardPolicy",
1513
1536
  status: "live",
@@ -1603,6 +1626,228 @@ var setMarkupModePolicyMetadata = {
1603
1626
  broadcastsVia: "crdt",
1604
1627
  rwdReference: "\xA7Runtime API \xA7 runtime.workflow.setMarkupModePolicy"
1605
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
+ }
1606
1851
  function createWorkflowFamily(runtime) {
1607
1852
  return {
1608
1853
  queryScopes(filter) {
@@ -1617,7 +1862,10 @@ function createWorkflowFamily(runtime) {
1617
1862
  createScope(input) {
1618
1863
  const adapterResult = createScopeFromBlockId(runtime, {
1619
1864
  blockId: input.blockId,
1865
+ scopeId: input.scopeId,
1620
1866
  mode: input.mode,
1867
+ persistence: input.persistence,
1868
+ metadata: input.metadata,
1621
1869
  label: input.label,
1622
1870
  ...input.visibility ? { visibility: input.visibility } : {},
1623
1871
  ...input.guardPolicy ? { guardPolicy: input.guardPolicy } : {},
@@ -1651,6 +1899,25 @@ function createWorkflowFamily(runtime) {
1651
1899
  }
1652
1900
  return { scopeId: adapterResult.scopeId, status: "created" };
1653
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
+ },
1654
1921
  getScopeGuardPolicy(scopeId) {
1655
1922
  return runtime.getScopeGuardPolicy(scopeId);
1656
1923
  },
@@ -1686,7 +1953,10 @@ function createWorkflowFamily(runtime) {
1686
1953
  createScopeFromAnchor(input) {
1687
1954
  const adapterResult = createScopeFromAnchor(runtime, {
1688
1955
  anchor: input.anchor,
1956
+ scopeId: input.scopeId,
1689
1957
  mode: input.mode,
1958
+ persistence: input.persistence,
1959
+ metadata: input.metadata,
1690
1960
  label: input.label,
1691
1961
  ...input.visibility ? { visibility: input.visibility } : {},
1692
1962
  ...input.guardPolicy ? { guardPolicy: input.guardPolicy } : {},
@@ -4065,6 +4335,12 @@ function createListsFamily(runtime) {
4065
4335
  const resolved = resolveCurrentListTarget(runtime.getCanonicalDocument(), input);
4066
4336
  return resolved.kind === "resolved" ? toReadback(runtime.getCanonicalDocument(), resolved.target, resolved.paragraph) : null;
4067
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
+ },
4068
4344
  previewCommand(input) {
4069
4345
  return previewListCommand(runtime.getCanonicalDocument(), input, activeStoryKey(runtime));
4070
4346
  },
@@ -4129,6 +4405,37 @@ function createListsFamily(runtime) {
4129
4405
  }
4130
4406
  };
4131
4407
  }
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
+ }
4132
4439
  function previewListCommand(document, input, activeStoryKey2) {
4133
4440
  const resolved = resolveCurrentListTarget(document, input);
4134
4441
  if (resolved.kind !== "resolved") {
@@ -4320,7 +4627,7 @@ function collectParagraphEntriesInto(blocks, storyKey, basePath, out) {
4320
4627
  const blockPath = `${basePath}/block[${blockIndex}]`;
4321
4628
  switch (block.type) {
4322
4629
  case "paragraph":
4323
- out.push({ paragraph: block, storyKey, blockPath });
4630
+ out.push({ paragraph: block, storyKey, blockPath, containerPath: basePath });
4324
4631
  break;
4325
4632
  case "table":
4326
4633
  for (let rowIndex = 0; rowIndex < block.rows.length; rowIndex += 1) {
@@ -4392,6 +4699,7 @@ function canContinuePreviousSequence(document, paragraphIndex) {
4392
4699
  for (let index = paragraphIndex - 1; index >= 0; index -= 1) {
4393
4700
  const previousEntry = paragraphs[index];
4394
4701
  if (previousEntry?.storyKey !== currentEntry.storyKey) break;
4702
+ if (previousEntry.containerPath !== currentEntry.containerPath) break;
4395
4703
  const previous = previousEntry.paragraph;
4396
4704
  if (!previous?.numbering) continue;
4397
4705
  const previousKind = getListKind(document.numbering, previous.numbering.numberingInstanceId);
@@ -4404,6 +4712,7 @@ function canJoinPreviousSequence(document, paragraphIndex) {
4404
4712
  const currentEntry = paragraphs[paragraphIndex];
4405
4713
  const previousEntry = paragraphs[paragraphIndex - 1];
4406
4714
  if (previousEntry?.storyKey !== currentEntry?.storyKey) return false;
4715
+ if (previousEntry?.containerPath !== currentEntry?.containerPath) return false;
4407
4716
  const current = currentEntry?.paragraph;
4408
4717
  const previous = previousEntry?.paragraph;
4409
4718
  if (!current?.numbering || !previous?.numbering) return false;
@@ -9264,6 +9573,9 @@ function applyRewrite(runtime, target, input) {
9264
9573
  if (target.kind === "editable-text") {
9265
9574
  return applyEditableTextRewrite(runtime, target, input);
9266
9575
  }
9576
+ if (target.handle.provenance === "marker-backed" && (target.scope.kind === "paragraph" || target.scope.kind === "list-item")) {
9577
+ return applyMarkerBackedRangeRewrite(runtime, target, input);
9578
+ }
9267
9579
  if (target.scope.kind === "list-item") {
9268
9580
  const listTextTarget = uniqueListTextActionForScope(runtime, target.handle);
9269
9581
  if (!listTextTarget.ok) return blockedApplyFromResolution(listTextTarget);
@@ -9410,13 +9722,97 @@ function applyEditableTextRewrite(runtime, target, input) {
9410
9722
  afterReadback
9411
9723
  };
9412
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
+ }
9413
9806
  function applyTableScopedMarkerRewrite(runtime, target, action, input) {
9414
9807
  const workflowScope = runtime.getScope(target.handle.scopeId);
9808
+ const markerRange = buildScopePositionMap(runtime.getCanonicalDocument()).markerScopes.get(
9809
+ target.handle.scopeId
9810
+ );
9415
9811
  const editableTarget = tableEditableTargetForActionHandle(
9416
9812
  runtime,
9417
9813
  action.actionHandle
9418
9814
  );
9419
- if (!workflowScope || workflowScope.anchor.kind !== "range" || !editableTarget) {
9815
+ if (!workflowScope || !markerRange || !editableTarget) {
9420
9816
  return blockedApply(
9421
9817
  `actions:rewrite:table-marker-target-unresolved:${target.handle.scopeId}`,
9422
9818
  "unresolved-target",
@@ -9424,19 +9820,23 @@ function applyTableScopedMarkerRewrite(runtime, target, action, input) {
9424
9820
  "Refresh placeholder scopes and retry; route persistent failures to L08 table scope target mapping."
9425
9821
  );
9426
9822
  }
9823
+ const beforeReadback = markerScopeReadback(runtime, target.handle.scopeId) ?? {
9824
+ text: target.scope.content.text,
9825
+ isEmpty: target.scope.content.text.length === 0
9826
+ };
9427
9827
  const before = runtime.getCanonicalDocument();
9428
9828
  try {
9429
9829
  runtime.dispatch({
9430
9830
  type: "selection.set",
9431
9831
  selection: {
9432
- anchor: workflowScope.anchor.from,
9433
- head: workflowScope.anchor.to,
9434
- isCollapsed: workflowScope.anchor.from === workflowScope.anchor.to,
9832
+ anchor: markerRange.from,
9833
+ head: markerRange.to,
9834
+ isCollapsed: markerRange.from === markerRange.to,
9435
9835
  activeRange: {
9436
9836
  kind: "range",
9437
- from: workflowScope.anchor.from,
9438
- to: workflowScope.anchor.to,
9439
- assoc: workflowScope.anchor.assoc ?? { start: -1, end: 1 }
9837
+ from: markerRange.from,
9838
+ to: markerRange.to,
9839
+ assoc: { start: -1, end: 1 }
9440
9840
  }
9441
9841
  },
9442
9842
  origin: actionOrigin(runtime, input)
@@ -9471,12 +9871,19 @@ function applyTableScopedMarkerRewrite(runtime, target, action, input) {
9471
9871
  );
9472
9872
  const afterText = paragraph ? collectInlineText3(paragraph.children) : void 0;
9473
9873
  const afterReadback = afterText === void 0 ? void 0 : { text: afterText, isEmpty: afterText.length === 0 };
9474
- 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) {
9475
9880
  return {
9476
9881
  status: "blocked",
9477
9882
  applied: false,
9478
9883
  changed,
9479
- target: summarizeTarget({ kind: "table-text", action }),
9884
+ target: summarizeTarget(
9885
+ compiledAfter ? { ...target, scope: compiledAfter.scope, handle: compiledAfter.scope.handle } : target
9886
+ ),
9480
9887
  posture: "suspect-readback",
9481
9888
  blockers: Object.freeze([
9482
9889
  `actions:rewrite:table-marker-readback-mismatch:${target.handle.scopeId}`
@@ -9489,25 +9896,24 @@ function applyTableScopedMarkerRewrite(runtime, target, action, input) {
9489
9896
  "Treat the mutation as suspect. Re-read the target and export before claiming success."
9490
9897
  )
9491
9898
  ]),
9492
- ...afterReadback ? { afterReadback } : {}
9899
+ ...scopeReadback ? { afterReadback: scopeReadback } : afterReadback ? { afterReadback } : {}
9493
9900
  };
9494
9901
  }
9495
9902
  return {
9496
9903
  status: "applied",
9497
9904
  applied: true,
9498
9905
  changed: true,
9499
- target: summarizeTarget({
9500
- kind: "table-text",
9501
- action: { ...action, readback: afterReadback }
9502
- }),
9906
+ target: summarizeTarget(
9907
+ compiledAfter ? { ...target, scope: compiledAfter.scope, handle: compiledAfter.scope.handle } : { kind: "table-text", action: { ...action, readback: scopeReadback } }
9908
+ ),
9503
9909
  commandReference: {
9504
9910
  command: "text.insert",
9505
9911
  actorId: input.actorId ?? "v3-ai-api",
9506
9912
  origin: input.origin ?? "agent",
9507
9913
  emittedAtUtc: currentAuditTimestamp(runtime)
9508
9914
  },
9509
- ...action.readback ? { beforeReadback: action.readback } : {},
9510
- afterReadback
9915
+ beforeReadback,
9916
+ afterReadback: scopeReadback
9511
9917
  };
9512
9918
  }
9513
9919
  function projectRewriteScopeResult(runtime, result, target, beforeText, proposedText, documentMutated) {
@@ -9601,7 +10007,7 @@ function summarizeTarget(target) {
9601
10007
  if (target.kind === "editable-text") {
9602
10008
  return {
9603
10009
  kind: target.targetKind,
9604
- handle: target.ownerHandle,
10010
+ handle: publicActionScopeHandle(target.ownerHandle),
9605
10011
  actionHandle: target.actionHandle,
9606
10012
  readback: target.readback,
9607
10013
  canRewriteText: true,
@@ -9612,13 +10018,21 @@ function summarizeTarget(target) {
9612
10018
  }
9613
10019
  return {
9614
10020
  kind: target.scope.kind,
9615
- handle: target.handle,
10021
+ handle: publicActionScopeHandle(target.handle),
9616
10022
  canRewriteText: canRewriteScopeText(target.scope),
9617
10023
  canInsertAdjacentText: canInsertAdjacentScopeText(target.scope),
9618
10024
  canFlag: true,
9619
10025
  canMark: canMarkScope(target.scope)
9620
10026
  };
9621
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
+ }
9622
10036
  function tableTextActionsForScope(runtime, handle) {
9623
10037
  const result = createTableActionFamily(runtime).listTableActions({
9624
10038
  handle,
@@ -9671,6 +10085,13 @@ function tableEditableTargetForActionHandle(runtime, actionHandle) {
9671
10085
  }
9672
10086
  return null;
9673
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
+ }
9674
10095
  function editableTextActionsForScope(runtime, handle) {
9675
10096
  const bundle = createScopeCompilerService(runtime).compileBundleById(
9676
10097
  handle.scopeId,
@@ -11023,6 +11444,86 @@ function blockTextLength(block) {
11023
11444
  return 0;
11024
11445
  }
11025
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
+ }
11026
11527
  function inlineTextLength(inline) {
11027
11528
  if (!inline) return 0;
11028
11529
  switch (inline.type) {
@@ -11178,6 +11679,109 @@ function summarizePlanResult(mode, steps) {
11178
11679
  };
11179
11680
  }
11180
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
+
11181
11785
  // src/api/v3/_create.ts
11182
11786
  function createApiV3(handle, opts) {
11183
11787
  const ai = {
@@ -11195,7 +11799,8 @@ function createApiV3(handle, opts) {
11195
11799
  ...createOutlineFamily(handle),
11196
11800
  ...createTableActionFamily(handle),
11197
11801
  ...createObjectActionFamily(handle),
11198
- ...createActionsFamily(handle)
11802
+ ...createActionsFamily(handle),
11803
+ ...createAgentFamily(handle, opts?.pe2Evidence)
11199
11804
  };
11200
11805
  const runtime = {
11201
11806
  document: createDocumentFamily(handle),