@ai-matrx/agents 0.24.2 → 0.25.1

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/index.cjs CHANGED
@@ -56,6 +56,7 @@ __export(index_exports, {
56
56
  fetchWithMatrxProtocolFallback: () => fetchWithMatrxProtocolFallback,
57
57
  followRuntimeOperationEvents: () => followRuntimeOperationEvents,
58
58
  followRuntimeOperationToEnd: () => followRuntimeOperationToEnd,
59
+ followUnavailableRejoin: () => followUnavailableRejoin,
59
60
  followWorkflowRunEvents: () => followWorkflowRunEvents,
60
61
  getRuntimeOperationStatus: () => getRuntimeOperationStatus,
61
62
  getRuntimeOperationsByLink: () => getRuntimeOperationsByLink,
@@ -89,6 +90,7 @@ __export(index_exports, {
89
90
  readImageRoleLimits: () => readImageRoleLimits,
90
91
  readLiveRunRejoin: () => readLiveRunRejoin,
91
92
  readLiveRunRequestId: () => readLiveRunRequestId,
93
+ readLiveStreamUnavailable: () => readLiveStreamUnavailable,
92
94
  readMatrxErrorCode: () => readMatrxErrorCode,
93
95
  readMatrxNdjsonStream: () => readMatrxNdjsonStream,
94
96
  readMatrxSseStream: () => readMatrxSseStream,
@@ -102,6 +104,7 @@ __export(index_exports, {
102
104
  rolesFor: () => rolesFor,
103
105
  runAgentToCompletion: () => runAgentToCompletion,
104
106
  runtimeOperationRejoinPath: () => runtimeOperationRejoinPath,
107
+ settleRunPickup: () => settleRunPickup,
105
108
  startAgentRun: () => startAgentRun,
106
109
  startMandateRun: () => startMandateRun,
107
110
  streamErrorText: () => streamErrorText,
@@ -1493,18 +1496,18 @@ async function followRuntimeOperationToEnd(transport, executionId, options) {
1493
1496
  let cursor = options.lastEventSeq ?? 0;
1494
1497
  let failures = 0;
1495
1498
  while (!outer?.aborted && failures < reconnectLimit) {
1496
- const attempt = new AbortController();
1497
- const onOuterAbort = () => attempt.abort();
1499
+ const attempt2 = new AbortController();
1500
+ const onOuterAbort = () => attempt2.abort();
1498
1501
  outer?.addEventListener("abort", onOuterAbort, { once: true });
1499
1502
  let stallTimer = null;
1500
1503
  const armStall = () => {
1501
1504
  if (stallTimer !== null) clearTimeout(stallTimer);
1502
- stallTimer = setTimeout(() => attempt.abort(), stallTimeoutMs);
1505
+ stallTimer = setTimeout(() => attempt2.abort(), stallTimeoutMs);
1503
1506
  };
1504
1507
  try {
1505
1508
  const items = followRuntimeOperationEvents(transport, executionId, {
1506
1509
  lastEventSeq: cursor,
1507
- signal: attempt.signal,
1510
+ signal: attempt2.signal,
1508
1511
  ...options.onMalformedFrame ? { onMalformedFrame: options.onMalformedFrame } : {}
1509
1512
  });
1510
1513
  armStall();
@@ -1599,7 +1602,7 @@ function readLiveRunRejoin(error) {
1599
1602
  const candidates = detailCandidates(bodyOf(error));
1600
1603
  if (candidates.length === 0) return null;
1601
1604
  const code = readMatrxErrorCode(error);
1602
- if (code !== MATRX_RUN_IN_PROGRESS && code !== MATRX_RESUME_CONFLICT) return null;
1605
+ if (code !== MATRX_RUN_IN_PROGRESS) return null;
1603
1606
  const liveRequestId = firstString(candidates, "live_request_id");
1604
1607
  const pathFromBody = firstString(candidates, "rejoin_path");
1605
1608
  const rejoinPath = pathFromBody && pathFromBody.startsWith("/") ? pathFromBody : liveRequestId ? runtimeOperationRejoinPath(liveRequestId) : null;
@@ -1617,7 +1620,7 @@ function readLiveRunRequestId(error) {
1617
1620
  return readLiveRunRejoin(error)?.liveRequestId ?? null;
1618
1621
  }
1619
1622
  function isResumeConflict(error) {
1620
- return statusOf(error) === 409 && readMatrxErrorCode(error) === MATRX_RESUME_CONFLICT && readLiveRunRejoin(error) === null;
1623
+ return statusOf(error) === 409 && readMatrxErrorCode(error) === MATRX_RESUME_CONFLICT;
1621
1624
  }
1622
1625
  function isLiveStreamUnavailable(error) {
1623
1626
  return statusOf(error) === 409 && readMatrxErrorCode(error) === MATRX_LIVE_STREAM_UNAVAILABLE;
@@ -1720,7 +1723,7 @@ async function resumeOrRejoin(transport, resumeCall, options) {
1720
1723
  rejoin = target;
1721
1724
  }
1722
1725
  onRejoin?.(rejoin);
1723
- let unavailableBody;
1726
+ let unavailable;
1724
1727
  try {
1725
1728
  const handle = await openMatrxStream(bound, rejoin.rejoinPath, {
1726
1729
  ...streamOptions,
@@ -1730,14 +1733,29 @@ async function resumeOrRejoin(transport, resumeCall, options) {
1730
1733
  await drain(handle, onEnvelope);
1731
1734
  return { kind: "rejoined", rejoin };
1732
1735
  } catch (error) {
1733
- if (!isLiveStreamUnavailable(error)) throw error;
1734
- unavailableBody = bodyOf(error);
1736
+ const read = readLiveStreamUnavailable(error);
1737
+ if (!read) throw error;
1738
+ unavailable = read;
1735
1739
  }
1736
- const runId = firstString(detailCandidates(unavailableBody), "run_id") ?? rejoin.runId;
1740
+ return followUnavailableRejoin(bound, rejoin, unavailable, {
1741
+ ...signalOnly,
1742
+ ...onOperationEvent ? { onOperationEvent } : {},
1743
+ ...onWorkflowEvent ? { onWorkflowEvent } : {},
1744
+ ...follow ? { follow } : {}
1745
+ });
1746
+ }
1747
+ function readLiveStreamUnavailable(error) {
1748
+ if (!isLiveStreamUnavailable(error)) return null;
1749
+ return { runId: firstString(detailCandidates(bodyOf(error)), "run_id") };
1750
+ }
1751
+ async function followUnavailableRejoin(transport, rejoin, unavailable, options = {}) {
1752
+ const bound = options.organizationId ? withRunOrganization(transport, options.organizationId) : transport;
1753
+ const signalOnly = options.signal ? { signal: options.signal } : {};
1754
+ const runId = unavailable.runId ?? rejoin.runId;
1737
1755
  if (runId && rejoin.rejoinPath.startsWith("/runtime/operations/")) {
1738
1756
  const result2 = await followWorkflowRunEvents(bound, runId, {
1739
1757
  ...signalOnly,
1740
- onEvent: (event, seq) => onWorkflowEvent?.(event, seq)
1758
+ onEvent: (event, seq) => options.onWorkflowEvent?.(event, seq)
1741
1759
  });
1742
1760
  return { kind: "followed_workflow", rejoin, runId, ended: result2.ended };
1743
1761
  }
@@ -1757,10 +1775,10 @@ async function resumeOrRejoin(transport, resumeCall, options) {
1757
1775
  };
1758
1776
  }
1759
1777
  const result = await followRuntimeOperationToEnd(bound, operation.execution_id, {
1760
- ...follow,
1778
+ ...options.follow,
1761
1779
  lastEventSeq: operation.last_event_seq,
1762
1780
  ...signalOnly,
1763
- onEvent: (event) => onOperationEvent?.(event)
1781
+ onEvent: (event) => options.onOperationEvent?.(event)
1764
1782
  });
1765
1783
  return {
1766
1784
  kind: "followed",
@@ -1770,6 +1788,35 @@ async function resumeOrRejoin(transport, resumeCall, options) {
1770
1788
  status: result.status
1771
1789
  };
1772
1790
  }
1791
+ async function attempt(run) {
1792
+ try {
1793
+ await run();
1794
+ return { reloaded: true };
1795
+ } catch (error) {
1796
+ return { reloaded: false, error };
1797
+ }
1798
+ }
1799
+ async function settleRunPickup(pickup, options) {
1800
+ switch (pickup.kind) {
1801
+ case "resumed":
1802
+ case "rejoined":
1803
+ return { state: "streamed" };
1804
+ case "resume_conflict":
1805
+ return { state: "retry" };
1806
+ case "followed_workflow":
1807
+ case "followed": {
1808
+ const status = pickup.kind === "followed" ? pickup.status : null;
1809
+ const nothingLive = pickup.kind === "followed" && pickup.executionId === null;
1810
+ if (pickup.ended || nothingLive) {
1811
+ return { state: "settled", status, ...await attempt(options.reloadSavedTurn) };
1812
+ }
1813
+ return {
1814
+ state: "still_running",
1815
+ ...await attempt(options.onStillRunning ?? options.reloadSavedTurn)
1816
+ };
1817
+ }
1818
+ }
1819
+ }
1773
1820
 
1774
1821
  // matrx/run.ts
1775
1822
  async function streamCall(transport, path, body, options) {