@ai-matrx/agents 0.22.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/CHANGELOG.md +12 -0
- package/dist/content-transfer/index.cjs +33 -33
- package/dist/content-transfer/index.cjs.map +1 -1
- package/dist/content-transfer/index.d.cts +1 -1
- package/dist/content-transfer/index.d.ts +1 -1
- package/dist/content-transfer/index.js +33 -33
- package/dist/content-transfer/index.js.map +1 -1
- package/dist/content-transfer/react/index.cjs +33 -33
- package/dist/content-transfer/react/index.cjs.map +1 -1
- package/dist/content-transfer/react/index.js +33 -33
- package/dist/content-transfer/react/index.js.map +1 -1
- package/dist/index.cjs +404 -131
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +404 -131
- package/dist/index.js.map +1 -1
- package/dist/mandates/index.cjs +2 -2
- package/dist/mandates/index.cjs.map +1 -1
- package/dist/mandates/index.d.cts +4 -4
- package/dist/mandates/index.d.ts +4 -4
- package/dist/mandates/index.js +2 -2
- package/dist/mandates/index.js.map +1 -1
- package/dist/matrx/index.cjs +404 -131
- package/dist/matrx/index.cjs.map +1 -1
- package/dist/matrx/index.d.cts +242 -4
- package/dist/matrx/index.d.ts +242 -4
- package/dist/matrx/index.js +404 -131
- package/dist/matrx/index.js.map +1 -1
- package/dist/react/index.cjs +41 -41
- package/dist/react/index.cjs.map +1 -1
- package/dist/react/index.js +41 -41
- package/dist/react/index.js.map +1 -1
- package/mandates/snapshots/keys.0.23.0.json +652 -0
- package/mandates/snapshots/keys.0.24.0.json +652 -0
- package/package.json +4 -4
package/dist/matrx/index.js
CHANGED
|
@@ -701,136 +701,6 @@ async function requestStream(transport, path, options) {
|
|
|
701
701
|
return response;
|
|
702
702
|
}
|
|
703
703
|
|
|
704
|
-
// matrx/run.ts
|
|
705
|
-
async function streamCall(transport, path, body, options) {
|
|
706
|
-
const response = await requestStream(transport, path, {
|
|
707
|
-
method: "POST",
|
|
708
|
-
// This client IS the streaming path — `stream: true` always, last so a
|
|
709
|
-
// caller-supplied value can never flip the response off NDJSON.
|
|
710
|
-
body: { ...body, stream: true },
|
|
711
|
-
...options.signal ? { signal: options.signal } : {}
|
|
712
|
-
});
|
|
713
|
-
return toRunHandle(response, options);
|
|
714
|
-
}
|
|
715
|
-
function startAgentRun(transport, agentId, request, options = {}) {
|
|
716
|
-
return streamCall(
|
|
717
|
-
transport,
|
|
718
|
-
`/ai/agents/${encodePathSegment(agentId)}`,
|
|
719
|
-
request,
|
|
720
|
-
options
|
|
721
|
-
);
|
|
722
|
-
}
|
|
723
|
-
function startMandateRun(transport, mandateKey, request, options = {}) {
|
|
724
|
-
return streamCall(
|
|
725
|
-
transport,
|
|
726
|
-
`/ai/mandates/${encodePathSegment(mandateKey)}`,
|
|
727
|
-
request,
|
|
728
|
-
options
|
|
729
|
-
);
|
|
730
|
-
}
|
|
731
|
-
function continueAgentConversation(transport, conversationId, request, options = {}) {
|
|
732
|
-
return streamCall(
|
|
733
|
-
transport,
|
|
734
|
-
`/ai/conversations/${encodePathSegment(conversationId)}`,
|
|
735
|
-
request,
|
|
736
|
-
options
|
|
737
|
-
);
|
|
738
|
-
}
|
|
739
|
-
function resumeAgentConversation(transport, conversationId, request = {}, options = {}) {
|
|
740
|
-
return streamCall(
|
|
741
|
-
transport,
|
|
742
|
-
`/ai/conversations/${encodePathSegment(conversationId)}/resume`,
|
|
743
|
-
request,
|
|
744
|
-
options
|
|
745
|
-
);
|
|
746
|
-
}
|
|
747
|
-
function cancelAgentRun(transport, requestId, options = {}) {
|
|
748
|
-
const query = buildQuery(
|
|
749
|
-
options.mode === "interrupt" ? { mode: "interrupt" } : {}
|
|
750
|
-
);
|
|
751
|
-
return requestJson(
|
|
752
|
-
transport,
|
|
753
|
-
`/ai/cancel/${encodePathSegment(requestId)}${query}`,
|
|
754
|
-
{
|
|
755
|
-
method: "POST",
|
|
756
|
-
...options.signal ? { signal: options.signal } : {}
|
|
757
|
-
}
|
|
758
|
-
);
|
|
759
|
-
}
|
|
760
|
-
var MatrxRunError = class extends Error {
|
|
761
|
-
name = "MatrxRunError";
|
|
762
|
-
/** The verbatim `error` event payload, when one fired. */
|
|
763
|
-
errorPayload;
|
|
764
|
-
/** The `user_request` completion status (`"failed"` | `"cancelled"`), when that was the trigger. */
|
|
765
|
-
completionStatus;
|
|
766
|
-
/** Text streamed before the failure — partial content never vanishes. */
|
|
767
|
-
partialText;
|
|
768
|
-
constructor(args) {
|
|
769
|
-
super(args.message);
|
|
770
|
-
this.errorPayload = args.errorPayload ?? null;
|
|
771
|
-
this.completionStatus = args.completionStatus ?? null;
|
|
772
|
-
this.partialText = args.partialText ?? "";
|
|
773
|
-
}
|
|
774
|
-
};
|
|
775
|
-
function isRecord3(value) {
|
|
776
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
777
|
-
}
|
|
778
|
-
function stringField(value, key) {
|
|
779
|
-
if (!isRecord3(value)) return null;
|
|
780
|
-
const field = value[key];
|
|
781
|
-
return typeof field === "string" && field ? field : null;
|
|
782
|
-
}
|
|
783
|
-
async function runAgentToCompletion(transport, agentId, request, options = {}) {
|
|
784
|
-
const handle = await startAgentRun(transport, agentId, request, options);
|
|
785
|
-
let text = "";
|
|
786
|
-
let completion = null;
|
|
787
|
-
let failure = null;
|
|
788
|
-
for await (const envelope of handle.events) {
|
|
789
|
-
options.onEvent?.(envelope);
|
|
790
|
-
if (envelope.event === "chunk") {
|
|
791
|
-
const chunk = stringField(envelope.data, "text");
|
|
792
|
-
if (chunk !== null) {
|
|
793
|
-
text += chunk;
|
|
794
|
-
options.onChunk?.(text);
|
|
795
|
-
}
|
|
796
|
-
continue;
|
|
797
|
-
}
|
|
798
|
-
if (envelope.event === "error" && failure === null) {
|
|
799
|
-
const payload = isRecord3(envelope.data) ? envelope.data : null;
|
|
800
|
-
failure = new MatrxRunError({
|
|
801
|
-
message: stringField(payload, "user_message") ?? stringField(payload, "message") ?? "The agent run failed",
|
|
802
|
-
errorPayload: payload,
|
|
803
|
-
partialText: text
|
|
804
|
-
});
|
|
805
|
-
continue;
|
|
806
|
-
}
|
|
807
|
-
if (envelope.event !== "completion" || !isRecord3(envelope.data)) continue;
|
|
808
|
-
if (envelope.data.operation !== "user_request") continue;
|
|
809
|
-
completion = envelope.data;
|
|
810
|
-
const status = envelope.data.status;
|
|
811
|
-
if ((status === "failed" || status === "cancelled") && failure === null) {
|
|
812
|
-
const result = isRecord3(envelope.data.result) ? envelope.data.result : null;
|
|
813
|
-
failure = new MatrxRunError({
|
|
814
|
-
message: stringField(result, "error") ?? stringField(result, "user_message") ?? `The agent run ${status}`,
|
|
815
|
-
completionStatus: status,
|
|
816
|
-
partialText: text
|
|
817
|
-
});
|
|
818
|
-
}
|
|
819
|
-
}
|
|
820
|
-
if (failure) throw failure;
|
|
821
|
-
if (!text && completion) {
|
|
822
|
-
const result = completion.result;
|
|
823
|
-
const output = stringField(result, "output");
|
|
824
|
-
if (output !== null) text = output;
|
|
825
|
-
}
|
|
826
|
-
return {
|
|
827
|
-
text,
|
|
828
|
-
requestId: handle.requestId,
|
|
829
|
-
conversationId: handle.conversationId,
|
|
830
|
-
completion
|
|
831
|
-
};
|
|
832
|
-
}
|
|
833
|
-
|
|
834
704
|
// stream/sse.ts
|
|
835
705
|
var FRAME_SEPARATOR = /\r\n\r\n|\n\n|\r\r/;
|
|
836
706
|
var LINE_SEPARATOR = /\r\n|\n|\r/;
|
|
@@ -1057,6 +927,393 @@ async function rejoinRuntimeOperation(transport, requestId, options = {}) {
|
|
|
1057
927
|
return toRunHandle(response, options);
|
|
1058
928
|
}
|
|
1059
929
|
|
|
930
|
+
// matrx/rejoin.ts
|
|
931
|
+
var MATRX_RUN_IN_PROGRESS = "run_in_progress";
|
|
932
|
+
var MATRX_RESUME_CONFLICT = "resume_conflict";
|
|
933
|
+
var MATRX_LIVE_STREAM_UNAVAILABLE = "live_stream_unavailable";
|
|
934
|
+
function isRecord3(value) {
|
|
935
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
936
|
+
}
|
|
937
|
+
function nonBlank(value) {
|
|
938
|
+
return typeof value === "string" && value.trim() ? value : null;
|
|
939
|
+
}
|
|
940
|
+
function detailCandidates(body) {
|
|
941
|
+
if (!isRecord3(body)) return [];
|
|
942
|
+
const out = [];
|
|
943
|
+
if (isRecord3(body.detail)) out.push(body.detail);
|
|
944
|
+
if (isRecord3(body.details)) out.push(body.details);
|
|
945
|
+
out.push(body);
|
|
946
|
+
return out;
|
|
947
|
+
}
|
|
948
|
+
function statusOf(error) {
|
|
949
|
+
if (!isRecord3(error) && !(error instanceof Error)) return null;
|
|
950
|
+
const status = error.status;
|
|
951
|
+
return typeof status === "number" ? status : null;
|
|
952
|
+
}
|
|
953
|
+
function bodyOf(error) {
|
|
954
|
+
if (!isRecord3(error) && !(error instanceof Error)) return void 0;
|
|
955
|
+
return error.serverDetail;
|
|
956
|
+
}
|
|
957
|
+
function readMatrxErrorCode(error) {
|
|
958
|
+
for (const candidate of detailCandidates(bodyOf(error))) {
|
|
959
|
+
const code = nonBlank(candidate.code);
|
|
960
|
+
if (code) return code;
|
|
961
|
+
}
|
|
962
|
+
for (const candidate of detailCandidates(bodyOf(error))) {
|
|
963
|
+
const code = nonBlank(candidate.error);
|
|
964
|
+
if (code) return code;
|
|
965
|
+
}
|
|
966
|
+
return null;
|
|
967
|
+
}
|
|
968
|
+
function firstString(candidates, key) {
|
|
969
|
+
for (const candidate of candidates) {
|
|
970
|
+
const value = nonBlank(candidate[key]);
|
|
971
|
+
if (value) return value.trim();
|
|
972
|
+
}
|
|
973
|
+
return null;
|
|
974
|
+
}
|
|
975
|
+
function readLiveRunRejoin(error) {
|
|
976
|
+
if (statusOf(error) !== 409) return null;
|
|
977
|
+
const candidates = detailCandidates(bodyOf(error));
|
|
978
|
+
if (candidates.length === 0) return null;
|
|
979
|
+
const code = readMatrxErrorCode(error);
|
|
980
|
+
if (code !== MATRX_RUN_IN_PROGRESS && code !== MATRX_RESUME_CONFLICT) return null;
|
|
981
|
+
const liveRequestId = firstString(candidates, "live_request_id");
|
|
982
|
+
const pathFromBody = firstString(candidates, "rejoin_path");
|
|
983
|
+
const rejoinPath = pathFromBody && pathFromBody.startsWith("/") ? pathFromBody : liveRequestId ? runtimeOperationRejoinPath(liveRequestId) : null;
|
|
984
|
+
if (!rejoinPath) return null;
|
|
985
|
+
const root = bodyOf(error);
|
|
986
|
+
return {
|
|
987
|
+
liveRequestId,
|
|
988
|
+
rejoinPath,
|
|
989
|
+
runId: firstString(candidates, "run_id"),
|
|
990
|
+
code,
|
|
991
|
+
body: isRecord3(root) ? root : {}
|
|
992
|
+
};
|
|
993
|
+
}
|
|
994
|
+
function readLiveRunRequestId(error) {
|
|
995
|
+
return readLiveRunRejoin(error)?.liveRequestId ?? null;
|
|
996
|
+
}
|
|
997
|
+
function isResumeConflict(error) {
|
|
998
|
+
return statusOf(error) === 409 && readMatrxErrorCode(error) === MATRX_RESUME_CONFLICT && readLiveRunRejoin(error) === null;
|
|
999
|
+
}
|
|
1000
|
+
function isLiveStreamUnavailable(error) {
|
|
1001
|
+
return statusOf(error) === 409 && readMatrxErrorCode(error) === MATRX_LIVE_STREAM_UNAVAILABLE;
|
|
1002
|
+
}
|
|
1003
|
+
function runtimeOperationRejoinPath(requestId) {
|
|
1004
|
+
return `/runtime/operations/${encodePathSegment(requestId)}/rejoin`;
|
|
1005
|
+
}
|
|
1006
|
+
function streamErrorText(eventOrPayload) {
|
|
1007
|
+
if (!isRecord3(eventOrPayload)) return null;
|
|
1008
|
+
const payload = typeof eventOrPayload.event === "string" && isRecord3(eventOrPayload.data) ? eventOrPayload.data : eventOrPayload;
|
|
1009
|
+
return nonBlank(payload.user_message) ?? nonBlank(payload.message);
|
|
1010
|
+
}
|
|
1011
|
+
async function openMatrxStream(transport, path, options = {}) {
|
|
1012
|
+
const response = await requestStream(transport, path, {
|
|
1013
|
+
method: "POST",
|
|
1014
|
+
...options.body !== void 0 ? { body: options.body } : {},
|
|
1015
|
+
...options.signal ? { signal: options.signal } : {}
|
|
1016
|
+
});
|
|
1017
|
+
return toRunHandle(response, options);
|
|
1018
|
+
}
|
|
1019
|
+
function withRunOrganization(transport, organizationId) {
|
|
1020
|
+
const orgId = requireOrganizationContext(organizationId);
|
|
1021
|
+
return {
|
|
1022
|
+
fetch: (path, init) => transport.fetch(path, {
|
|
1023
|
+
...init,
|
|
1024
|
+
headers: { ...init.headers, "X-Organization-Id": orgId }
|
|
1025
|
+
})
|
|
1026
|
+
};
|
|
1027
|
+
}
|
|
1028
|
+
async function followWorkflowRunEvents(transport, runId, options) {
|
|
1029
|
+
const headers = { Accept: "text/event-stream" };
|
|
1030
|
+
if (options.lastEventId) headers["Last-Event-ID"] = String(options.lastEventId);
|
|
1031
|
+
let response;
|
|
1032
|
+
try {
|
|
1033
|
+
response = await requestStream(
|
|
1034
|
+
transport,
|
|
1035
|
+
`/runs/${encodePathSegment(runId)}/events/stream`,
|
|
1036
|
+
{
|
|
1037
|
+
method: "GET",
|
|
1038
|
+
headers,
|
|
1039
|
+
...options.signal ? { signal: options.signal } : {}
|
|
1040
|
+
}
|
|
1041
|
+
);
|
|
1042
|
+
} catch (error) {
|
|
1043
|
+
if (options.signal?.aborted) return { ended: false };
|
|
1044
|
+
throw error;
|
|
1045
|
+
}
|
|
1046
|
+
try {
|
|
1047
|
+
for await (const frame of readMatrxSseStream(
|
|
1048
|
+
response.body
|
|
1049
|
+
)) {
|
|
1050
|
+
if (frame.event === "end") return { ended: true };
|
|
1051
|
+
if (frame.data === null) continue;
|
|
1052
|
+
let parsed;
|
|
1053
|
+
try {
|
|
1054
|
+
parsed = JSON.parse(frame.data);
|
|
1055
|
+
} catch {
|
|
1056
|
+
continue;
|
|
1057
|
+
}
|
|
1058
|
+
if (!isRecord3(parsed)) continue;
|
|
1059
|
+
options.onEvent(parsed, parsed.event === "node_stream" ? null : frame.seq);
|
|
1060
|
+
}
|
|
1061
|
+
} catch (error) {
|
|
1062
|
+
if (!options.signal?.aborted) throw error;
|
|
1063
|
+
}
|
|
1064
|
+
return { ended: false };
|
|
1065
|
+
}
|
|
1066
|
+
async function drain(handle, onEnvelope) {
|
|
1067
|
+
for await (const envelope of handle.events) onEnvelope(envelope);
|
|
1068
|
+
}
|
|
1069
|
+
async function resumeOrRejoin(transport, resumeCall, options) {
|
|
1070
|
+
const {
|
|
1071
|
+
onEnvelope,
|
|
1072
|
+
organizationId,
|
|
1073
|
+
onRejoin,
|
|
1074
|
+
onOperationEvent,
|
|
1075
|
+
onWorkflowEvent,
|
|
1076
|
+
follow,
|
|
1077
|
+
...rest
|
|
1078
|
+
} = options;
|
|
1079
|
+
const bound = organizationId ? withRunOrganization(transport, organizationId) : transport;
|
|
1080
|
+
const streamOptions = {
|
|
1081
|
+
...rest,
|
|
1082
|
+
...options.signal ? { signal: options.signal } : {}
|
|
1083
|
+
};
|
|
1084
|
+
const signalOnly = options.signal ? { signal: options.signal } : {};
|
|
1085
|
+
let rejoin;
|
|
1086
|
+
try {
|
|
1087
|
+
const handle = await resumeCall(bound, streamOptions);
|
|
1088
|
+
await drain(handle, onEnvelope);
|
|
1089
|
+
return { kind: "resumed", requestId: handle.requestId };
|
|
1090
|
+
} catch (error) {
|
|
1091
|
+
const target = readLiveRunRejoin(error);
|
|
1092
|
+
if (!target) {
|
|
1093
|
+
if (error instanceof MatrxApiError && isResumeConflict(error)) {
|
|
1094
|
+
return { kind: "resume_conflict", error };
|
|
1095
|
+
}
|
|
1096
|
+
throw error;
|
|
1097
|
+
}
|
|
1098
|
+
rejoin = target;
|
|
1099
|
+
}
|
|
1100
|
+
onRejoin?.(rejoin);
|
|
1101
|
+
let unavailableBody;
|
|
1102
|
+
try {
|
|
1103
|
+
const handle = await openMatrxStream(bound, rejoin.rejoinPath, {
|
|
1104
|
+
...streamOptions,
|
|
1105
|
+
// The rejoin routes take no body model; the reference client posts {}.
|
|
1106
|
+
body: {}
|
|
1107
|
+
});
|
|
1108
|
+
await drain(handle, onEnvelope);
|
|
1109
|
+
return { kind: "rejoined", rejoin };
|
|
1110
|
+
} catch (error) {
|
|
1111
|
+
if (!isLiveStreamUnavailable(error)) throw error;
|
|
1112
|
+
unavailableBody = bodyOf(error);
|
|
1113
|
+
}
|
|
1114
|
+
const runId = firstString(detailCandidates(unavailableBody), "run_id") ?? rejoin.runId;
|
|
1115
|
+
if (runId && rejoin.rejoinPath.startsWith("/runtime/operations/")) {
|
|
1116
|
+
const result2 = await followWorkflowRunEvents(bound, runId, {
|
|
1117
|
+
...signalOnly,
|
|
1118
|
+
onEvent: (event, seq) => onWorkflowEvent?.(event, seq)
|
|
1119
|
+
});
|
|
1120
|
+
return { kind: "followed_workflow", rejoin, runId, ended: result2.ended };
|
|
1121
|
+
}
|
|
1122
|
+
const liveRequestId = rejoin.liveRequestId;
|
|
1123
|
+
const statusView = liveRequestId ? await getRuntimeOperationStatus(bound, liveRequestId, signalOnly) : null;
|
|
1124
|
+
const operation = statusView?.operations.find((op) => !op.is_terminal) ?? statusView?.operations[0] ?? null;
|
|
1125
|
+
if (!operation) {
|
|
1126
|
+
return { kind: "followed", rejoin, executionId: null, ended: false, status: null };
|
|
1127
|
+
}
|
|
1128
|
+
if (operation.is_terminal) {
|
|
1129
|
+
return {
|
|
1130
|
+
kind: "followed",
|
|
1131
|
+
rejoin,
|
|
1132
|
+
executionId: operation.execution_id,
|
|
1133
|
+
ended: true,
|
|
1134
|
+
status: operation.status
|
|
1135
|
+
};
|
|
1136
|
+
}
|
|
1137
|
+
const result = await followRuntimeOperationToEnd(bound, operation.execution_id, {
|
|
1138
|
+
...follow,
|
|
1139
|
+
lastEventSeq: operation.last_event_seq,
|
|
1140
|
+
...signalOnly,
|
|
1141
|
+
onEvent: (event) => onOperationEvent?.(event)
|
|
1142
|
+
});
|
|
1143
|
+
return {
|
|
1144
|
+
kind: "followed",
|
|
1145
|
+
rejoin,
|
|
1146
|
+
executionId: operation.execution_id,
|
|
1147
|
+
ended: result.ended,
|
|
1148
|
+
status: result.status
|
|
1149
|
+
};
|
|
1150
|
+
}
|
|
1151
|
+
|
|
1152
|
+
// matrx/run.ts
|
|
1153
|
+
async function streamCall(transport, path, body, options) {
|
|
1154
|
+
const response = await requestStream(transport, path, {
|
|
1155
|
+
method: "POST",
|
|
1156
|
+
// This client IS the streaming path — `stream: true` always, last so a
|
|
1157
|
+
// caller-supplied value can never flip the response off NDJSON.
|
|
1158
|
+
body: { ...body, stream: true },
|
|
1159
|
+
...options.signal ? { signal: options.signal } : {}
|
|
1160
|
+
});
|
|
1161
|
+
return toRunHandle(response, options);
|
|
1162
|
+
}
|
|
1163
|
+
function startAgentRun(transport, agentId, request, options = {}) {
|
|
1164
|
+
return streamCall(
|
|
1165
|
+
transport,
|
|
1166
|
+
`/ai/agents/${encodePathSegment(agentId)}`,
|
|
1167
|
+
request,
|
|
1168
|
+
options
|
|
1169
|
+
);
|
|
1170
|
+
}
|
|
1171
|
+
function startMandateRun(transport, mandateKey, request, options = {}) {
|
|
1172
|
+
return streamCall(
|
|
1173
|
+
transport,
|
|
1174
|
+
`/ai/mandates/${encodePathSegment(mandateKey)}`,
|
|
1175
|
+
request,
|
|
1176
|
+
options
|
|
1177
|
+
);
|
|
1178
|
+
}
|
|
1179
|
+
function continueAgentConversation(transport, conversationId, request, options = {}) {
|
|
1180
|
+
return streamCall(
|
|
1181
|
+
transport,
|
|
1182
|
+
`/ai/conversations/${encodePathSegment(conversationId)}`,
|
|
1183
|
+
request,
|
|
1184
|
+
options
|
|
1185
|
+
);
|
|
1186
|
+
}
|
|
1187
|
+
function resumeAgentConversation(transport, conversationId, request = {}, options = {}) {
|
|
1188
|
+
return streamCall(
|
|
1189
|
+
transport,
|
|
1190
|
+
`/ai/conversations/${encodePathSegment(conversationId)}/resume`,
|
|
1191
|
+
request,
|
|
1192
|
+
options
|
|
1193
|
+
);
|
|
1194
|
+
}
|
|
1195
|
+
function cancelAgentRun(transport, requestId, options = {}) {
|
|
1196
|
+
const query = buildQuery(
|
|
1197
|
+
options.mode === "interrupt" ? { mode: "interrupt" } : {}
|
|
1198
|
+
);
|
|
1199
|
+
return requestJson(
|
|
1200
|
+
transport,
|
|
1201
|
+
`/ai/cancel/${encodePathSegment(requestId)}${query}`,
|
|
1202
|
+
{
|
|
1203
|
+
method: "POST",
|
|
1204
|
+
...options.signal ? { signal: options.signal } : {}
|
|
1205
|
+
}
|
|
1206
|
+
);
|
|
1207
|
+
}
|
|
1208
|
+
var MatrxRunError = class extends Error {
|
|
1209
|
+
name = "MatrxRunError";
|
|
1210
|
+
/** The verbatim `error` event payload, when one fired. */
|
|
1211
|
+
errorPayload;
|
|
1212
|
+
/** The `user_request` completion status (`"failed"` | `"cancelled"`), when that was the trigger. */
|
|
1213
|
+
completionStatus;
|
|
1214
|
+
/** Text streamed before the failure — partial content never vanishes. */
|
|
1215
|
+
partialText;
|
|
1216
|
+
constructor(args) {
|
|
1217
|
+
super(args.message);
|
|
1218
|
+
this.errorPayload = args.errorPayload ?? null;
|
|
1219
|
+
this.completionStatus = args.completionStatus ?? null;
|
|
1220
|
+
this.partialText = args.partialText ?? "";
|
|
1221
|
+
}
|
|
1222
|
+
};
|
|
1223
|
+
function isRecord4(value) {
|
|
1224
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1225
|
+
}
|
|
1226
|
+
function stringField(value, key) {
|
|
1227
|
+
if (!isRecord4(value)) return null;
|
|
1228
|
+
const field = value[key];
|
|
1229
|
+
return typeof field === "string" && field ? field : null;
|
|
1230
|
+
}
|
|
1231
|
+
async function runAgentToCompletion(transport, agentId, request, options = {}) {
|
|
1232
|
+
const handle = await startAgentRun(transport, agentId, request, options);
|
|
1233
|
+
let text = "";
|
|
1234
|
+
let completion = null;
|
|
1235
|
+
let failure = null;
|
|
1236
|
+
for await (const envelope of handle.events) {
|
|
1237
|
+
options.onEvent?.(envelope);
|
|
1238
|
+
if (envelope.event === "chunk") {
|
|
1239
|
+
const chunk = stringField(envelope.data, "text");
|
|
1240
|
+
if (chunk !== null) {
|
|
1241
|
+
text += chunk;
|
|
1242
|
+
options.onChunk?.(text);
|
|
1243
|
+
}
|
|
1244
|
+
continue;
|
|
1245
|
+
}
|
|
1246
|
+
if (envelope.event === "error" && failure === null) {
|
|
1247
|
+
const payload = isRecord4(envelope.data) ? envelope.data : null;
|
|
1248
|
+
failure = new MatrxRunError({
|
|
1249
|
+
message: streamErrorText(payload) ?? "The agent run failed",
|
|
1250
|
+
errorPayload: payload,
|
|
1251
|
+
partialText: text
|
|
1252
|
+
});
|
|
1253
|
+
continue;
|
|
1254
|
+
}
|
|
1255
|
+
if (envelope.event !== "completion" || !isRecord4(envelope.data)) continue;
|
|
1256
|
+
if (envelope.data.operation !== "user_request") continue;
|
|
1257
|
+
completion = envelope.data;
|
|
1258
|
+
const status = envelope.data.status;
|
|
1259
|
+
if ((status === "failed" || status === "cancelled") && failure === null) {
|
|
1260
|
+
const result = isRecord4(envelope.data.result) ? envelope.data.result : null;
|
|
1261
|
+
failure = new MatrxRunError({
|
|
1262
|
+
message: stringField(result, "error") ?? stringField(result, "user_message") ?? `The agent run ${status}`,
|
|
1263
|
+
completionStatus: status,
|
|
1264
|
+
partialText: text
|
|
1265
|
+
});
|
|
1266
|
+
}
|
|
1267
|
+
}
|
|
1268
|
+
if (failure) throw failure;
|
|
1269
|
+
if (!text && completion) {
|
|
1270
|
+
const result = completion.result;
|
|
1271
|
+
const output = stringField(result, "output");
|
|
1272
|
+
if (output !== null) text = output;
|
|
1273
|
+
}
|
|
1274
|
+
return {
|
|
1275
|
+
text,
|
|
1276
|
+
requestId: handle.requestId,
|
|
1277
|
+
conversationId: handle.conversationId,
|
|
1278
|
+
completion
|
|
1279
|
+
};
|
|
1280
|
+
}
|
|
1281
|
+
|
|
1282
|
+
// matrx/provider-failures.ts
|
|
1283
|
+
var clip = (value, max) => value.length > max ? value.slice(0, max) : value;
|
|
1284
|
+
function providerSessionFailureBody(failure) {
|
|
1285
|
+
const message = failure.message?.trim() || failure.error_type?.trim() || (failure.status_code ? `HTTP ${failure.status_code}` : "provider session failed");
|
|
1286
|
+
const body = {
|
|
1287
|
+
provider: failure.provider,
|
|
1288
|
+
message: clip(message, 4e3)
|
|
1289
|
+
};
|
|
1290
|
+
if (failure.model?.trim()) body.model = clip(failure.model.trim(), 200);
|
|
1291
|
+
if (failure.error_type?.trim()) body.error_type = clip(failure.error_type.trim(), 200);
|
|
1292
|
+
if (typeof failure.status_code === "number" && Number.isInteger(failure.status_code) && failure.status_code >= 100 && failure.status_code <= 599) {
|
|
1293
|
+
body.status_code = failure.status_code;
|
|
1294
|
+
}
|
|
1295
|
+
return body;
|
|
1296
|
+
}
|
|
1297
|
+
async function reportProviderSessionFailure(transport, failure, options = {}) {
|
|
1298
|
+
try {
|
|
1299
|
+
return await requestJson(
|
|
1300
|
+
transport,
|
|
1301
|
+
"/broker/provider-failures",
|
|
1302
|
+
{
|
|
1303
|
+
method: "POST",
|
|
1304
|
+
body: providerSessionFailureBody(failure),
|
|
1305
|
+
...options.signal ? { signal: options.signal } : {}
|
|
1306
|
+
}
|
|
1307
|
+
);
|
|
1308
|
+
} catch (error) {
|
|
1309
|
+
console.warn(
|
|
1310
|
+
`[provider-failures] could not report a ${failure.provider} session failure`,
|
|
1311
|
+
error
|
|
1312
|
+
);
|
|
1313
|
+
return null;
|
|
1314
|
+
}
|
|
1315
|
+
}
|
|
1316
|
+
|
|
1060
1317
|
// matrx/tools.ts
|
|
1061
1318
|
function submitAgentToolResults(transport, conversationId, results, options = {}) {
|
|
1062
1319
|
return requestJson(
|
|
@@ -1097,6 +1354,9 @@ function listUserPendingToolCalls(transport, options = {}) {
|
|
|
1097
1354
|
}
|
|
1098
1355
|
export {
|
|
1099
1356
|
MATRX_AI_API_VERSION_DEFAULT,
|
|
1357
|
+
MATRX_LIVE_STREAM_UNAVAILABLE,
|
|
1358
|
+
MATRX_RESUME_CONFLICT,
|
|
1359
|
+
MATRX_RUN_IN_PROGRESS,
|
|
1100
1360
|
MatrxApiError,
|
|
1101
1361
|
MatrxRunError,
|
|
1102
1362
|
OrganizationContextError,
|
|
@@ -1117,9 +1377,12 @@ export {
|
|
|
1117
1377
|
fetchWithMatrxProtocolFallback,
|
|
1118
1378
|
followRuntimeOperationEvents,
|
|
1119
1379
|
followRuntimeOperationToEnd,
|
|
1380
|
+
followWorkflowRunEvents,
|
|
1120
1381
|
getRuntimeOperationStatus,
|
|
1121
1382
|
getRuntimeOperationsByLink,
|
|
1122
1383
|
isCoveredAiPath,
|
|
1384
|
+
isLiveStreamUnavailable,
|
|
1385
|
+
isResumeConflict,
|
|
1123
1386
|
isV2Path,
|
|
1124
1387
|
listConversationPendingToolCalls,
|
|
1125
1388
|
listRuntimeOperationEvents,
|
|
@@ -1128,14 +1391,24 @@ export {
|
|
|
1128
1391
|
newEphemeralConversationStart,
|
|
1129
1392
|
newStoredConversationStart,
|
|
1130
1393
|
normalizeMatrxError,
|
|
1394
|
+
openMatrxStream,
|
|
1395
|
+
providerSessionFailureBody,
|
|
1396
|
+
readLiveRunRejoin,
|
|
1397
|
+
readLiveRunRequestId,
|
|
1398
|
+
readMatrxErrorCode,
|
|
1131
1399
|
rejoinRuntimeOperation,
|
|
1400
|
+
reportProviderSessionFailure,
|
|
1132
1401
|
requireOrganizationContext,
|
|
1133
1402
|
resumeAgentConversation,
|
|
1403
|
+
resumeOrRejoin,
|
|
1134
1404
|
runAgentToCompletion,
|
|
1405
|
+
runtimeOperationRejoinPath,
|
|
1135
1406
|
startAgentRun,
|
|
1136
1407
|
startMandateRun,
|
|
1408
|
+
streamErrorText,
|
|
1137
1409
|
submitAgentToolResults,
|
|
1138
1410
|
toV1FallbackUrl,
|
|
1139
|
-
toV2Path
|
|
1411
|
+
toV2Path,
|
|
1412
|
+
withRunOrganization
|
|
1140
1413
|
};
|
|
1141
1414
|
//# sourceMappingURL=index.js.map
|