@ai-matrx/agents 0.23.0 → 0.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -4,7 +4,7 @@ export { projectAgentResultForDisplay } from './presentation/result.cjs';
4
4
  export { AgentProjectionEvent, AgentProjectionOperation, AgentProjectionRenderBlock, AgentProjectionStatus, AgentProjectionTool, AgentRequestProjection, createAgentRequestProjection, projectAgentEvent, projectAgentEvents } from './projection/request.cjs';
5
5
  export { DEFAULT_WORKFLOW_PROJECTION_LIMITS, WorkflowNodeProjection, WorkflowNodeStreamEvent, WorkflowProjectionLimits, WorkflowRenderBlockIssue, WorkflowRenderBlockIssueCode, WorkflowRenderFrameSet, createWorkflowNodeProjection, projectWorkflowNodeEvent } from './projection/workflow.cjs';
6
6
  export { F as FollowRuntimeOperationOptions, a as FollowRuntimeOperationToEndOptions, b as FollowRuntimeOperationToEndResult, M as MatrxAgentStartRequest, c as MatrxApiError, d as MatrxCancelResponse, e as MatrxChatMessage, f as MatrxCompletedRun, g as MatrxContextAnchor, h as MatrxConversationContinueRequest, i as MatrxConversationResumeRequest, j as MatrxConversationStart, k as MatrxEphemeralConversation, l as MatrxJsonObject, m as MatrxJsonValue, n as MatrxMandateStartRequest, o as MatrxOperationEventsPage, p as MatrxOperationFollowEvent, q as MatrxOperationStatusResponse, r as MatrxOperationsByLinkResponse, s as MatrxRequestScope, t as MatrxRunError, u as MatrxRunHandle, v as MatrxRuntimeExecutionStatus, w as MatrxRuntimeOperationEvent, x as MatrxRuntimeOperationView, y as MatrxStoredConversationContinue, z as MatrxStoredConversationCreate, A as MatrxStreamCallOptions, B as MatrxTransport, C as MatrxTransportRequest, D as MatrxTurnFields, R as RunAgentToCompletionOptions, T as TERMINAL_MATRX_RUNTIME_STATUSES, E as cancelAgentRun, G as continueAgentConversation, H as continueEphemeralConversationStart, I as continueStoredConversationStart, J as extractMatrxErrorCode, K as extractMatrxErrorMessage, L as followRuntimeOperationEvents, N as followRuntimeOperationToEnd, O as getRuntimeOperationStatus, P as getRuntimeOperationsByLink, Q as listRuntimeOperationEvents, S as mintMatrxConversationId, U as newEphemeralConversationStart, V as newStoredConversationStart, W as rejoinRuntimeOperation, X as resumeAgentConversation, Y as runAgentToCompletion, Z as startAgentRun, _ as startMandateRun } from './operations--f5ko9Su.cjs';
7
- export { CreateMatrxTransportOptions, MATRX_AI_API_VERSION_DEFAULT, MATRX_LIVE_STREAM_UNAVAILABLE, MATRX_RESUME_CONFLICT, MATRX_RUN_IN_PROGRESS, MatrxAiApiVersion, MatrxCallError, MatrxClientSessionProvider, MatrxClientToolResult, MatrxHttpErrorLike, MatrxPendingCallSummary, MatrxProtocolDowngrade, MatrxProtocolFallbackOptions, MatrxRequestInfo, MatrxToolResultsResponse, MatrxTransportDiagnostics, MatrxTransportTarget, OrganizationContextError, OrganizationContextErrorCode, OrganizationOperation, ProviderSessionFailure, ProviderSessionFailureVerdict, ResumeOrRejoinOptions, ResumeOrRejoinOutcome, V2_COVERED_AI_PATH_TEMPLATES, applyAiApiVersion, applyOrganizationContextHeader, assertOrganizationMatchesOperation, assertQueryOrganizationMatchesContext, createMatrxTransport, createOrganizationOperation, fetchWithMatrxProtocolFallback, isCoveredAiPath, isLiveStreamUnavailable, isResumeConflict, isV2Path, listConversationPendingToolCalls, listUserPendingToolCalls, normalizeMatrxError, openMatrxStream, providerSessionFailureBody, readLiveRunRequestId, readMatrxErrorCode, reportProviderSessionFailure, requireOrganizationContext, resumeOrRejoin, runtimeOperationRejoinPath, streamErrorText, submitAgentToolResults, toV1FallbackUrl, toV2Path, withRunOrganization } from './matrx/index.cjs';
7
+ export { CreateMatrxTransportOptions, MATRX_AI_API_VERSION_DEFAULT, MATRX_LIVE_STREAM_UNAVAILABLE, MATRX_RESUME_CONFLICT, MATRX_RUN_IN_PROGRESS, MatrxAiApiVersion, MatrxCallError, MatrxClientSessionProvider, MatrxClientToolResult, MatrxHttpErrorLike, MatrxLiveRunRejoin, MatrxPendingCallSummary, MatrxProtocolDowngrade, MatrxProtocolFallbackOptions, MatrxRequestInfo, MatrxToolResultsResponse, MatrxTransportDiagnostics, MatrxTransportTarget, OrganizationContextError, OrganizationContextErrorCode, OrganizationOperation, ProviderSessionFailure, ProviderSessionFailureVerdict, ResumeOrRejoinOptions, ResumeOrRejoinOutcome, V2_COVERED_AI_PATH_TEMPLATES, applyAiApiVersion, applyOrganizationContextHeader, assertOrganizationMatchesOperation, assertQueryOrganizationMatchesContext, createMatrxTransport, createOrganizationOperation, fetchWithMatrxProtocolFallback, followWorkflowRunEvents, isCoveredAiPath, isLiveStreamUnavailable, isResumeConflict, isV2Path, listConversationPendingToolCalls, listUserPendingToolCalls, normalizeMatrxError, openMatrxStream, providerSessionFailureBody, readLiveRunRejoin, readLiveRunRequestId, readMatrxErrorCode, reportProviderSessionFailure, requireOrganizationContext, resumeOrRejoin, runtimeOperationRejoinPath, streamErrorText, submitAgentToolResults, toV1FallbackUrl, toV2Path, withRunOrganization } from './matrx/index.cjs';
8
8
  import '@ai-matrx/data';
9
9
  import '@ai-matrx/data/net';
10
10
 
package/dist/index.d.ts CHANGED
@@ -4,7 +4,7 @@ export { projectAgentResultForDisplay } from './presentation/result.js';
4
4
  export { AgentProjectionEvent, AgentProjectionOperation, AgentProjectionRenderBlock, AgentProjectionStatus, AgentProjectionTool, AgentRequestProjection, createAgentRequestProjection, projectAgentEvent, projectAgentEvents } from './projection/request.js';
5
5
  export { DEFAULT_WORKFLOW_PROJECTION_LIMITS, WorkflowNodeProjection, WorkflowNodeStreamEvent, WorkflowProjectionLimits, WorkflowRenderBlockIssue, WorkflowRenderBlockIssueCode, WorkflowRenderFrameSet, createWorkflowNodeProjection, projectWorkflowNodeEvent } from './projection/workflow.js';
6
6
  export { F as FollowRuntimeOperationOptions, a as FollowRuntimeOperationToEndOptions, b as FollowRuntimeOperationToEndResult, M as MatrxAgentStartRequest, c as MatrxApiError, d as MatrxCancelResponse, e as MatrxChatMessage, f as MatrxCompletedRun, g as MatrxContextAnchor, h as MatrxConversationContinueRequest, i as MatrxConversationResumeRequest, j as MatrxConversationStart, k as MatrxEphemeralConversation, l as MatrxJsonObject, m as MatrxJsonValue, n as MatrxMandateStartRequest, o as MatrxOperationEventsPage, p as MatrxOperationFollowEvent, q as MatrxOperationStatusResponse, r as MatrxOperationsByLinkResponse, s as MatrxRequestScope, t as MatrxRunError, u as MatrxRunHandle, v as MatrxRuntimeExecutionStatus, w as MatrxRuntimeOperationEvent, x as MatrxRuntimeOperationView, y as MatrxStoredConversationContinue, z as MatrxStoredConversationCreate, A as MatrxStreamCallOptions, B as MatrxTransport, C as MatrxTransportRequest, D as MatrxTurnFields, R as RunAgentToCompletionOptions, T as TERMINAL_MATRX_RUNTIME_STATUSES, E as cancelAgentRun, G as continueAgentConversation, H as continueEphemeralConversationStart, I as continueStoredConversationStart, J as extractMatrxErrorCode, K as extractMatrxErrorMessage, L as followRuntimeOperationEvents, N as followRuntimeOperationToEnd, O as getRuntimeOperationStatus, P as getRuntimeOperationsByLink, Q as listRuntimeOperationEvents, S as mintMatrxConversationId, U as newEphemeralConversationStart, V as newStoredConversationStart, W as rejoinRuntimeOperation, X as resumeAgentConversation, Y as runAgentToCompletion, Z as startAgentRun, _ as startMandateRun } from './operations-Dp4ut-ac.js';
7
- export { CreateMatrxTransportOptions, MATRX_AI_API_VERSION_DEFAULT, MATRX_LIVE_STREAM_UNAVAILABLE, MATRX_RESUME_CONFLICT, MATRX_RUN_IN_PROGRESS, MatrxAiApiVersion, MatrxCallError, MatrxClientSessionProvider, MatrxClientToolResult, MatrxHttpErrorLike, MatrxPendingCallSummary, MatrxProtocolDowngrade, MatrxProtocolFallbackOptions, MatrxRequestInfo, MatrxToolResultsResponse, MatrxTransportDiagnostics, MatrxTransportTarget, OrganizationContextError, OrganizationContextErrorCode, OrganizationOperation, ProviderSessionFailure, ProviderSessionFailureVerdict, ResumeOrRejoinOptions, ResumeOrRejoinOutcome, V2_COVERED_AI_PATH_TEMPLATES, applyAiApiVersion, applyOrganizationContextHeader, assertOrganizationMatchesOperation, assertQueryOrganizationMatchesContext, createMatrxTransport, createOrganizationOperation, fetchWithMatrxProtocolFallback, isCoveredAiPath, isLiveStreamUnavailable, isResumeConflict, isV2Path, listConversationPendingToolCalls, listUserPendingToolCalls, normalizeMatrxError, openMatrxStream, providerSessionFailureBody, readLiveRunRequestId, readMatrxErrorCode, reportProviderSessionFailure, requireOrganizationContext, resumeOrRejoin, runtimeOperationRejoinPath, streamErrorText, submitAgentToolResults, toV1FallbackUrl, toV2Path, withRunOrganization } from './matrx/index.js';
7
+ export { CreateMatrxTransportOptions, MATRX_AI_API_VERSION_DEFAULT, MATRX_LIVE_STREAM_UNAVAILABLE, MATRX_RESUME_CONFLICT, MATRX_RUN_IN_PROGRESS, MatrxAiApiVersion, MatrxCallError, MatrxClientSessionProvider, MatrxClientToolResult, MatrxHttpErrorLike, MatrxLiveRunRejoin, MatrxPendingCallSummary, MatrxProtocolDowngrade, MatrxProtocolFallbackOptions, MatrxRequestInfo, MatrxToolResultsResponse, MatrxTransportDiagnostics, MatrxTransportTarget, OrganizationContextError, OrganizationContextErrorCode, OrganizationOperation, ProviderSessionFailure, ProviderSessionFailureVerdict, ResumeOrRejoinOptions, ResumeOrRejoinOutcome, V2_COVERED_AI_PATH_TEMPLATES, applyAiApiVersion, applyOrganizationContextHeader, assertOrganizationMatchesOperation, assertQueryOrganizationMatchesContext, createMatrxTransport, createOrganizationOperation, fetchWithMatrxProtocolFallback, followWorkflowRunEvents, isCoveredAiPath, isLiveStreamUnavailable, isResumeConflict, isV2Path, listConversationPendingToolCalls, listUserPendingToolCalls, normalizeMatrxError, openMatrxStream, providerSessionFailureBody, readLiveRunRejoin, readLiveRunRequestId, readMatrxErrorCode, reportProviderSessionFailure, requireOrganizationContext, resumeOrRejoin, runtimeOperationRejoinPath, streamErrorText, submitAgentToolResults, toV1FallbackUrl, toV2Path, withRunOrganization } from './matrx/index.js';
8
8
  import '@ai-matrx/data';
9
9
  import '@ai-matrx/data/net';
10
10
 
package/dist/index.js CHANGED
@@ -1472,17 +1472,37 @@ function readMatrxErrorCode(error) {
1472
1472
  }
1473
1473
  return null;
1474
1474
  }
1475
- function readLiveRunRequestId(error) {
1476
- if (statusOf(error) !== 409) return null;
1477
- for (const candidate of detailCandidates(bodyOf(error))) {
1478
- if (candidate.code !== MATRX_RUN_IN_PROGRESS) continue;
1479
- const live = nonBlank(candidate.live_request_id);
1480
- if (live) return live.trim();
1475
+ function firstString(candidates, key) {
1476
+ for (const candidate of candidates) {
1477
+ const value = nonBlank(candidate[key]);
1478
+ if (value) return value.trim();
1481
1479
  }
1482
1480
  return null;
1483
1481
  }
1482
+ function readLiveRunRejoin(error) {
1483
+ if (statusOf(error) !== 409) return null;
1484
+ const candidates = detailCandidates(bodyOf(error));
1485
+ if (candidates.length === 0) return null;
1486
+ const code = readMatrxErrorCode(error);
1487
+ if (code !== MATRX_RUN_IN_PROGRESS && code !== MATRX_RESUME_CONFLICT) return null;
1488
+ const liveRequestId = firstString(candidates, "live_request_id");
1489
+ const pathFromBody = firstString(candidates, "rejoin_path");
1490
+ const rejoinPath = pathFromBody && pathFromBody.startsWith("/") ? pathFromBody : liveRequestId ? runtimeOperationRejoinPath(liveRequestId) : null;
1491
+ if (!rejoinPath) return null;
1492
+ const root = bodyOf(error);
1493
+ return {
1494
+ liveRequestId,
1495
+ rejoinPath,
1496
+ runId: firstString(candidates, "run_id"),
1497
+ code,
1498
+ body: isRecord4(root) ? root : {}
1499
+ };
1500
+ }
1501
+ function readLiveRunRequestId(error) {
1502
+ return readLiveRunRejoin(error)?.liveRequestId ?? null;
1503
+ }
1484
1504
  function isResumeConflict(error) {
1485
- return statusOf(error) === 409 && readMatrxErrorCode(error) === MATRX_RESUME_CONFLICT;
1505
+ return statusOf(error) === 409 && readMatrxErrorCode(error) === MATRX_RESUME_CONFLICT && readLiveRunRejoin(error) === null;
1486
1506
  }
1487
1507
  function isLiveStreamUnavailable(error) {
1488
1508
  return statusOf(error) === 409 && readMatrxErrorCode(error) === MATRX_LIVE_STREAM_UNAVAILABLE;
@@ -1512,54 +1532,110 @@ function withRunOrganization(transport, organizationId) {
1512
1532
  })
1513
1533
  };
1514
1534
  }
1535
+ async function followWorkflowRunEvents(transport, runId, options) {
1536
+ const headers = { Accept: "text/event-stream" };
1537
+ if (options.lastEventId) headers["Last-Event-ID"] = String(options.lastEventId);
1538
+ let response;
1539
+ try {
1540
+ response = await requestStream(
1541
+ transport,
1542
+ `/runs/${encodePathSegment(runId)}/events/stream`,
1543
+ {
1544
+ method: "GET",
1545
+ headers,
1546
+ ...options.signal ? { signal: options.signal } : {}
1547
+ }
1548
+ );
1549
+ } catch (error) {
1550
+ if (options.signal?.aborted) return { ended: false };
1551
+ throw error;
1552
+ }
1553
+ try {
1554
+ for await (const frame of readMatrxSseStream(
1555
+ response.body
1556
+ )) {
1557
+ if (frame.event === "end") return { ended: true };
1558
+ if (frame.data === null) continue;
1559
+ let parsed;
1560
+ try {
1561
+ parsed = JSON.parse(frame.data);
1562
+ } catch {
1563
+ continue;
1564
+ }
1565
+ if (!isRecord4(parsed)) continue;
1566
+ options.onEvent(parsed, parsed.event === "node_stream" ? null : frame.seq);
1567
+ }
1568
+ } catch (error) {
1569
+ if (!options.signal?.aborted) throw error;
1570
+ }
1571
+ return { ended: false };
1572
+ }
1515
1573
  async function drain(handle, onEnvelope) {
1516
1574
  for await (const envelope of handle.events) onEnvelope(envelope);
1517
1575
  }
1518
1576
  async function resumeOrRejoin(transport, resumeCall, options) {
1519
- const { onEnvelope, organizationId, onRejoin, onOperationEvent, follow, ...rest } = options;
1577
+ const {
1578
+ onEnvelope,
1579
+ organizationId,
1580
+ onRejoin,
1581
+ onOperationEvent,
1582
+ onWorkflowEvent,
1583
+ follow,
1584
+ ...rest
1585
+ } = options;
1520
1586
  const bound = organizationId ? withRunOrganization(transport, organizationId) : transport;
1521
1587
  const streamOptions = {
1522
1588
  ...rest,
1523
1589
  ...options.signal ? { signal: options.signal } : {}
1524
1590
  };
1525
- let liveRequestId;
1591
+ const signalOnly = options.signal ? { signal: options.signal } : {};
1592
+ let rejoin;
1526
1593
  try {
1527
1594
  const handle = await resumeCall(bound, streamOptions);
1528
1595
  await drain(handle, onEnvelope);
1529
1596
  return { kind: "resumed", requestId: handle.requestId };
1530
1597
  } catch (error) {
1531
- if (error instanceof MatrxApiError && isResumeConflict(error)) {
1532
- return { kind: "resume_conflict", error };
1598
+ const target = readLiveRunRejoin(error);
1599
+ if (!target) {
1600
+ if (error instanceof MatrxApiError && isResumeConflict(error)) {
1601
+ return { kind: "resume_conflict", error };
1602
+ }
1603
+ throw error;
1533
1604
  }
1534
- const live = readLiveRunRequestId(error);
1535
- if (!live) throw error;
1536
- liveRequestId = live;
1605
+ rejoin = target;
1537
1606
  }
1538
- onRejoin?.(liveRequestId);
1607
+ onRejoin?.(rejoin);
1608
+ let unavailableBody;
1539
1609
  try {
1540
- const handle = await rejoinRuntimeOperation(bound, liveRequestId, streamOptions);
1610
+ const handle = await openMatrxStream(bound, rejoin.rejoinPath, {
1611
+ ...streamOptions,
1612
+ // The rejoin routes take no body model; the reference client posts {}.
1613
+ body: {}
1614
+ });
1541
1615
  await drain(handle, onEnvelope);
1542
- return { kind: "rejoined", liveRequestId };
1616
+ return { kind: "rejoined", rejoin };
1543
1617
  } catch (error) {
1544
1618
  if (!isLiveStreamUnavailable(error)) throw error;
1619
+ unavailableBody = bodyOf(error);
1545
1620
  }
1546
- const statusView = await getRuntimeOperationStatus(bound, liveRequestId, {
1547
- ...options.signal ? { signal: options.signal } : {}
1548
- });
1621
+ const runId = firstString(detailCandidates(unavailableBody), "run_id") ?? rejoin.runId;
1622
+ if (runId && rejoin.rejoinPath.startsWith("/runtime/operations/")) {
1623
+ const result2 = await followWorkflowRunEvents(bound, runId, {
1624
+ ...signalOnly,
1625
+ onEvent: (event, seq) => onWorkflowEvent?.(event, seq)
1626
+ });
1627
+ return { kind: "followed_workflow", rejoin, runId, ended: result2.ended };
1628
+ }
1629
+ const liveRequestId = rejoin.liveRequestId;
1630
+ const statusView = liveRequestId ? await getRuntimeOperationStatus(bound, liveRequestId, signalOnly) : null;
1549
1631
  const operation = statusView?.operations.find((op) => !op.is_terminal) ?? statusView?.operations[0] ?? null;
1550
1632
  if (!operation) {
1551
- return {
1552
- kind: "followed",
1553
- liveRequestId,
1554
- executionId: null,
1555
- ended: false,
1556
- status: null
1557
- };
1633
+ return { kind: "followed", rejoin, executionId: null, ended: false, status: null };
1558
1634
  }
1559
1635
  if (operation.is_terminal) {
1560
1636
  return {
1561
1637
  kind: "followed",
1562
- liveRequestId,
1638
+ rejoin,
1563
1639
  executionId: operation.execution_id,
1564
1640
  ended: true,
1565
1641
  status: operation.status
@@ -1568,12 +1644,12 @@ async function resumeOrRejoin(transport, resumeCall, options) {
1568
1644
  const result = await followRuntimeOperationToEnd(bound, operation.execution_id, {
1569
1645
  ...follow,
1570
1646
  lastEventSeq: operation.last_event_seq,
1571
- ...options.signal ? { signal: options.signal } : {},
1647
+ ...signalOnly,
1572
1648
  onEvent: (event) => onOperationEvent?.(event)
1573
1649
  });
1574
1650
  return {
1575
1651
  kind: "followed",
1576
- liveRequestId,
1652
+ rejoin,
1577
1653
  executionId: operation.execution_id,
1578
1654
  ended: result.ended,
1579
1655
  status: result.status
@@ -2038,6 +2114,7 @@ export {
2038
2114
  fetchWithMatrxProtocolFallback,
2039
2115
  followRuntimeOperationEvents,
2040
2116
  followRuntimeOperationToEnd,
2117
+ followWorkflowRunEvents,
2041
2118
  getRuntimeOperationStatus,
2042
2119
  getRuntimeOperationsByLink,
2043
2120
  imageRoleVerdict,
@@ -2068,6 +2145,7 @@ export {
2068
2145
  projectWorkflowNodeEvent,
2069
2146
  providerSessionFailureBody,
2070
2147
  readImageRoleLimits,
2148
+ readLiveRunRejoin,
2071
2149
  readLiveRunRequestId,
2072
2150
  readMatrxErrorCode,
2073
2151
  readMatrxNdjsonStream,