@agents24/node 0.5.16 → 0.5.18
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/compatibility.json +5 -5
- package/dist/bff.cjs +91 -39
- package/dist/bff.cjs.map +1 -1
- package/dist/bff.d.cts +1 -1
- package/dist/bff.d.ts +1 -1
- package/dist/bff.js +91 -39
- package/dist/bff.js.map +1 -1
- package/dist/{client-ChHref1j.d.cts → client-YEw6vRtg.d.cts} +6 -0
- package/dist/{client-ChHref1j.d.ts → client-YEw6vRtg.d.ts} +6 -0
- package/dist/index.cjs +24 -12
- 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 +25 -13
- package/dist/index.js.map +1 -1
- package/dist/types/bff-sse.d.ts +3 -1
- package/dist/types/bff-sse.d.ts.map +1 -1
- package/dist/types/bff.d.ts.map +1 -1
- package/dist/types/embed.d.ts.map +1 -1
- package/dist/types/streams.d.ts +3 -3
- package/dist/types/streams.d.ts.map +1 -1
- package/dist/types/types.d.ts +5 -0
- package/dist/types/types.d.ts.map +1 -1
- package/package.json +2 -2
package/compatibility.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"contractHash": "
|
|
2
|
+
"contractHash": "39792de00fbe302638c6304df42bd350e3a953ed030cd0cbd8a844411a386e83",
|
|
3
3
|
"contractVersion": "0.3.0",
|
|
4
4
|
"corpus": "conformance/v1/corpus.json",
|
|
5
5
|
"generatedBy": "@agents24/sdk-contract",
|
|
@@ -15,11 +15,11 @@
|
|
|
15
15
|
"reactDom": ">=19 <20"
|
|
16
16
|
},
|
|
17
17
|
"@agents24/client": {
|
|
18
|
-
"contract": "
|
|
18
|
+
"contract": "010b6a4647e7b995ac3a63399d94b2c9fcdfc58ba67026e89577e4a23560b90d",
|
|
19
19
|
"runtime": "run-stream.v3"
|
|
20
20
|
},
|
|
21
21
|
"@agents24/node": {
|
|
22
|
-
"contract": "
|
|
22
|
+
"contract": "39792de00fbe302638c6304df42bd350e3a953ed030cd0cbd8a844411a386e83",
|
|
23
23
|
"node": "^22.0.0 || ^24.0.0"
|
|
24
24
|
},
|
|
25
25
|
"@agents24/react": {
|
|
@@ -27,11 +27,11 @@
|
|
|
27
27
|
"react": ">=19 <20"
|
|
28
28
|
},
|
|
29
29
|
"@talmudpedia/runtime-sdk": {
|
|
30
|
-
"contract": "
|
|
30
|
+
"contract": "010b6a4647e7b995ac3a63399d94b2c9fcdfc58ba67026e89577e4a23560b90d",
|
|
31
31
|
"protocolOwner": "@agents24/client"
|
|
32
32
|
},
|
|
33
33
|
"agents24": {
|
|
34
|
-
"contract": "
|
|
34
|
+
"contract": "39792de00fbe302638c6304df42bd350e3a953ed030cd0cbd8a844411a386e83",
|
|
35
35
|
"python": ">=3.10"
|
|
36
36
|
}
|
|
37
37
|
},
|
package/dist/bff.cjs
CHANGED
|
@@ -223,31 +223,68 @@ function sseHeaders(headers) {
|
|
|
223
223
|
result.set("X-Accel-Buffering", "no");
|
|
224
224
|
return result;
|
|
225
225
|
}
|
|
226
|
-
function sseResponse(execute, headers, failurePayload, onFailure) {
|
|
227
|
-
const
|
|
228
|
-
const writer = stream.writable.getWriter();
|
|
226
|
+
function sseResponse(execute, headers, failurePayload, onFailure, requestSignal, leaseSeconds = 60) {
|
|
227
|
+
const lifetime = new AbortController();
|
|
229
228
|
const encoder = new TextEncoder();
|
|
230
|
-
|
|
229
|
+
let sink;
|
|
230
|
+
let wake;
|
|
231
|
+
const abort = () => {
|
|
232
|
+
lifetime.abort(requestSignal?.reason);
|
|
233
|
+
wake?.();
|
|
234
|
+
};
|
|
235
|
+
requestSignal?.addEventListener("abort", abort, { once: true });
|
|
236
|
+
const stream = new ReadableStream({
|
|
237
|
+
start(controller) {
|
|
238
|
+
sink = controller;
|
|
239
|
+
},
|
|
240
|
+
pull() {
|
|
241
|
+
wake?.();
|
|
242
|
+
wake = void 0;
|
|
243
|
+
},
|
|
244
|
+
cancel(reason) {
|
|
245
|
+
lifetime.abort(reason);
|
|
246
|
+
wake?.();
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
const writeFrame = async (frame) => {
|
|
250
|
+
if (lifetime.signal.aborted) return;
|
|
251
|
+
if ((sink.desiredSize ?? 0) <= 0) await new Promise((resolve) => {
|
|
252
|
+
wake = resolve;
|
|
253
|
+
});
|
|
254
|
+
if (!lifetime.signal.aborted) sink.enqueue(encoder.encode(frame));
|
|
255
|
+
};
|
|
256
|
+
const write = (value) => writeFrame(`data: ${JSON.stringify(value)}
|
|
257
|
+
|
|
258
|
+
`);
|
|
259
|
+
const control = (value) => {
|
|
260
|
+
const { type, ...metadata } = value;
|
|
261
|
+
return writeFrame(`: agents24.${type} ${JSON.stringify(metadata)}
|
|
231
262
|
|
|
232
|
-
`)
|
|
233
|
-
|
|
263
|
+
`);
|
|
264
|
+
};
|
|
265
|
+
const timer = setTimeout(() => {
|
|
266
|
+
if (!lifetime.signal.aborted) {
|
|
267
|
+
sink.enqueue(encoder.encode(": agents24.lease_expired {}\n\n"));
|
|
268
|
+
lifetime.abort("Observation lease expired.");
|
|
269
|
+
wake?.();
|
|
270
|
+
}
|
|
271
|
+
}, leaseSeconds * 1e3);
|
|
272
|
+
if (requestSignal?.aborted) abort();
|
|
273
|
+
void execute(write, lifetime.signal, control).catch(async (error) => {
|
|
274
|
+
if (lifetime.signal.aborted) return;
|
|
234
275
|
onFailure?.(error);
|
|
235
|
-
if (failurePayload)
|
|
236
|
-
|
|
237
|
-
|
|
276
|
+
if (failurePayload) await write(failurePayload(error)).catch(() => void 0);
|
|
277
|
+
}).finally(() => {
|
|
278
|
+
clearTimeout(timer);
|
|
279
|
+
requestSignal?.removeEventListener("abort", abort);
|
|
280
|
+
lifetime.abort();
|
|
281
|
+
wake?.();
|
|
282
|
+
try {
|
|
283
|
+
sink.close();
|
|
284
|
+
} catch {
|
|
238
285
|
}
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
seq: Number.MAX_SAFE_INTEGER,
|
|
242
|
-
ts: (/* @__PURE__ */ new Date()).toISOString(),
|
|
243
|
-
event: "run.failed",
|
|
244
|
-
run_id: "bff-stream-error",
|
|
245
|
-
stage: "run",
|
|
246
|
-
payload: { error: "The Agent stream failed." }
|
|
247
|
-
};
|
|
248
|
-
await write(event).catch(() => void 0);
|
|
249
|
-
}).finally(() => writer.close().catch(() => void 0));
|
|
250
|
-
return new Response(stream.readable, { headers: sseHeaders(headers) });
|
|
286
|
+
});
|
|
287
|
+
return new Response(stream, { headers: sseHeaders(headers) });
|
|
251
288
|
}
|
|
252
289
|
|
|
253
290
|
// src/bff-values.ts
|
|
@@ -530,7 +567,7 @@ function createAgents24BffHandlerWithResolver(options, resolveCustomerIdentity)
|
|
|
530
567
|
if (!admittedRunId) {
|
|
531
568
|
response = json(admission, void 0, 202);
|
|
532
569
|
} else {
|
|
533
|
-
response = sseResponse(async (write) => {
|
|
570
|
+
response = sseResponse(async (write, signal, control) => {
|
|
534
571
|
await embed.attachAgentRun(agentId, admittedRunId, {
|
|
535
572
|
...identity,
|
|
536
573
|
integration_id: integrationId,
|
|
@@ -538,11 +575,13 @@ function createAgents24BffHandlerWithResolver(options, resolveCustomerIdentity)
|
|
|
538
575
|
metadata: objectValue(body.metadata),
|
|
539
576
|
client: objectValue(body.client),
|
|
540
577
|
cursor: 0
|
|
541
|
-
}, (event) =>
|
|
578
|
+
}, (event) => {
|
|
579
|
+
if (event.event !== "assistant.completed" || new URL(request.url).searchParams.get("stream_features")?.split(",").includes("assistant-completion-v1")) return write(event);
|
|
580
|
+
}, { signal, onControl: control, leaseSeconds: 60 });
|
|
542
581
|
}, {
|
|
543
582
|
"X-Run-ID": admittedRunId,
|
|
544
583
|
...admission.thread_id ? { "X-Thread-ID": admission.thread_id } : {}
|
|
545
|
-
}, void 0, streamFailure(request, "chat.stream"));
|
|
584
|
+
}, void 0, streamFailure(request, "chat.stream"), request.signal);
|
|
546
585
|
}
|
|
547
586
|
} else if (request.method === "POST" && /^\/runs\/[^/]+\/attach$/.test(path)) {
|
|
548
587
|
const runId = routeId(path, /^\/runs\/([^/]+)\/attach$/, "runId");
|
|
@@ -551,17 +590,22 @@ function createAgents24BffHandlerWithResolver(options, resolveCustomerIdentity)
|
|
|
551
590
|
if (cursor !== void 0 && (!Number.isInteger(cursor) || Number(cursor) < 0)) {
|
|
552
591
|
throw new BffInputError(400, "INVALID_CURSOR", "cursor must be a non-negative integer.");
|
|
553
592
|
}
|
|
554
|
-
response = sseResponse(async (write) => {
|
|
593
|
+
response = sseResponse(async (write, signal, control) => {
|
|
555
594
|
await embed.attachAgentRun(agentId, runId, {
|
|
556
595
|
...identity,
|
|
557
596
|
runtime_target: runtimeTarget,
|
|
558
597
|
integration_id: integrationId,
|
|
559
598
|
...cursor === void 0 ? {} : { cursor: Number(cursor) }
|
|
560
|
-
}, (event) =>
|
|
599
|
+
}, (event) => {
|
|
600
|
+
if (event.event !== "assistant.completed" || new URL(request.url).searchParams.get("stream_features")?.split(",").includes("assistant-completion-v1")) return write(event);
|
|
601
|
+
}, {
|
|
561
602
|
idempotencyKey,
|
|
562
|
-
signal
|
|
603
|
+
signal,
|
|
604
|
+
onControl: control,
|
|
605
|
+
leaseSeconds: 60,
|
|
606
|
+
...url.searchParams.get("thread_id") ? { threadId: url.searchParams.get("thread_id") } : {}
|
|
563
607
|
});
|
|
564
|
-
}, void 0, void 0, streamFailure(request, "run.attach"));
|
|
608
|
+
}, void 0, void 0, streamFailure(request, "run.attach"), request.signal);
|
|
565
609
|
} else if (request.method === "POST" && /^\/runs\/[^/]+\/cancel$/.test(path)) {
|
|
566
610
|
const runId = routeId(path, /^\/runs\/([^/]+)\/cancel$/, "runId");
|
|
567
611
|
response = json(await embed.cancelAgentRun(agentId, runId, {
|
|
@@ -611,19 +655,22 @@ function createAgents24BffHandlerWithResolver(options, resolveCustomerIdentity)
|
|
|
611
655
|
if (cursor !== void 0 && (!Number.isInteger(cursor) || cursor < 0)) {
|
|
612
656
|
throw new BffInputError(400, "INVALID_CURSOR", "cursor must be a non-negative integer.");
|
|
613
657
|
}
|
|
614
|
-
response = sseResponse(async (write) => {
|
|
658
|
+
response = sseResponse(async (write, signal, control) => {
|
|
615
659
|
await embed.subscribeAgentThreadEvents(agentId, {
|
|
616
660
|
...threadIdentity,
|
|
617
661
|
integrationId,
|
|
618
662
|
...cursor !== void 0 ? { cursor } : {},
|
|
619
|
-
signal
|
|
663
|
+
signal,
|
|
664
|
+
onControl: control,
|
|
665
|
+
leaseSeconds: 60,
|
|
666
|
+
...url.searchParams.get("thread_id") ? { threadId: url.searchParams.get("thread_id") } : {}
|
|
620
667
|
}, (event) => write(event));
|
|
621
668
|
}, void 0, () => ({
|
|
622
669
|
schema_version: "agents24.thread_summary_event.v1",
|
|
623
670
|
event: "snapshot_required",
|
|
624
|
-
cursor,
|
|
671
|
+
cursor: cursor ?? 0,
|
|
625
672
|
reason: "stream_failed"
|
|
626
|
-
}), streamFailure(request, "threads.events"));
|
|
673
|
+
}), streamFailure(request, "threads.events"), request.signal);
|
|
627
674
|
} else if (/^\/threads\/[^/]+\/messages$/.test(path) && request.method === "GET") {
|
|
628
675
|
const threadId = routeId(path, /^\/threads\/([^/]+)\/messages$/, "threadId");
|
|
629
676
|
const before = url.searchParams.has("before") ? messageRevision(url.searchParams.get("before")) : void 0;
|
|
@@ -978,7 +1025,7 @@ function createResolvedAgents24BffHandler(options) {
|
|
|
978
1025
|
}, { idempotencyKey: stringValue(request.headers.get("idempotency-key")) });
|
|
979
1026
|
const admittedRunId = admission.run_id;
|
|
980
1027
|
if (!admittedRunId) return mergeHeaders(json(admission, void 0, 202), resolution.responseHeaders);
|
|
981
|
-
return mergeHeaders(sseResponse(async (write) => {
|
|
1028
|
+
return mergeHeaders(sseResponse(async (write, signal, control) => {
|
|
982
1029
|
await options.client.embed.attachAgentRun(selected.agentId, admittedRunId, {
|
|
983
1030
|
...identity,
|
|
984
1031
|
runtime_target: runtimeTarget,
|
|
@@ -986,11 +1033,13 @@ function createResolvedAgents24BffHandler(options) {
|
|
|
986
1033
|
metadata: objectValue(body.metadata),
|
|
987
1034
|
client: objectValue(body.client),
|
|
988
1035
|
cursor: 0
|
|
989
|
-
}, (event) =>
|
|
1036
|
+
}, (event) => {
|
|
1037
|
+
if (event.event !== "assistant.completed" || new URL(request.url).searchParams.get("stream_features")?.split(",").includes("assistant-completion-v1")) return write(event);
|
|
1038
|
+
}, { signal, onControl: control, leaseSeconds: 60 });
|
|
990
1039
|
}, {
|
|
991
1040
|
"X-Run-ID": admittedRunId,
|
|
992
1041
|
...admission.thread_id ? { "X-Thread-ID": admission.thread_id } : {}
|
|
993
|
-
}, void 0, (error) => reportFailureResponse(options.onDiagnostic, request, "chat.stream", errorResponse(error))), resolution.responseHeaders);
|
|
1042
|
+
}, void 0, (error) => reportFailureResponse(options.onDiagnostic, request, "chat.stream", errorResponse(error)), request.signal), resolution.responseHeaders);
|
|
994
1043
|
}
|
|
995
1044
|
if (request.method === "GET" && path === "/threads") {
|
|
996
1045
|
const skip = Number(url.searchParams.get("skip") || 0);
|
|
@@ -1014,7 +1063,7 @@ function createResolvedAgents24BffHandler(options) {
|
|
|
1014
1063
|
if (cursor !== void 0 && (!Number.isInteger(cursor) || cursor < 0)) {
|
|
1015
1064
|
throw new BffInputError(400, "INVALID_CURSOR", "cursor must be a non-negative integer.");
|
|
1016
1065
|
}
|
|
1017
|
-
return mergeHeaders(sseResponse(async (write) => {
|
|
1066
|
+
return mergeHeaders(sseResponse(async (write, signal, control) => {
|
|
1018
1067
|
await options.client.embed.subscribeAgentThreadEventsMulti({
|
|
1019
1068
|
agentIds: agents.map((agent) => agent.agentId),
|
|
1020
1069
|
externalUserId,
|
|
@@ -1022,14 +1071,17 @@ function createResolvedAgents24BffHandler(options) {
|
|
|
1022
1071
|
...cursor === void 0 ? {} : { cursor },
|
|
1023
1072
|
runtimeTarget,
|
|
1024
1073
|
integrationId,
|
|
1025
|
-
signal
|
|
1074
|
+
signal,
|
|
1075
|
+
onControl: control,
|
|
1076
|
+
leaseSeconds: 60,
|
|
1077
|
+
...url.searchParams.get("thread_id") ? { threadId: url.searchParams.get("thread_id") } : {}
|
|
1026
1078
|
}, (event) => write(event));
|
|
1027
1079
|
}, void 0, () => ({
|
|
1028
1080
|
schema_version: "agents24.thread_summary_event.v1",
|
|
1029
1081
|
event: "snapshot_required",
|
|
1030
|
-
cursor,
|
|
1082
|
+
cursor: cursor ?? 0,
|
|
1031
1083
|
reason: "stream_failed"
|
|
1032
|
-
}), (error) => reportFailureResponse(options.onDiagnostic, request, "threads.events", errorResponse(error))), resolution.responseHeaders);
|
|
1084
|
+
}), (error) => reportFailureResponse(options.onDiagnostic, request, "threads.events", errorResponse(error)), request.signal), resolution.responseHeaders);
|
|
1033
1085
|
}
|
|
1034
1086
|
if (/^\/threads\/[^/]+\/(?:messages(?:\/[^/]+)?|queue\/resume)$/.test(path) && ["GET", "POST", "PATCH", "DELETE"].includes(request.method)) {
|
|
1035
1087
|
const threadId = routeId(path, /^\/threads\/([^/]+)\//, "threadId");
|