@agents24/node 0.5.17 → 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.
@@ -1,5 +1,5 @@
1
1
  {
2
- "contractHash": "8bc171524458786bf938b66ec15fe511e9359792c9eee5f2e852f8d00920c679",
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": "0394b689ce7604b444b11c8792050fae0c1da4233d6b318d22832c3b89516a4a",
18
+ "contract": "010b6a4647e7b995ac3a63399d94b2c9fcdfc58ba67026e89577e4a23560b90d",
19
19
  "runtime": "run-stream.v3"
20
20
  },
21
21
  "@agents24/node": {
22
- "contract": "8bc171524458786bf938b66ec15fe511e9359792c9eee5f2e852f8d00920c679",
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": "0394b689ce7604b444b11c8792050fae0c1da4233d6b318d22832c3b89516a4a",
30
+ "contract": "010b6a4647e7b995ac3a63399d94b2c9fcdfc58ba67026e89577e4a23560b90d",
31
31
  "protocolOwner": "@agents24/client"
32
32
  },
33
33
  "agents24": {
34
- "contract": "8bc171524458786bf938b66ec15fe511e9359792c9eee5f2e852f8d00920c679",
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 stream = new TransformStream();
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
- const write = (value) => writer.write(encoder.encode(`data: ${JSON.stringify(value)}
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)}
231
257
 
232
- `));
233
- void execute(write).catch(async (error) => {
258
+ `);
259
+ const control = (value) => {
260
+ const { type, ...metadata } = value;
261
+ return writeFrame(`: agents24.${type} ${JSON.stringify(metadata)}
262
+
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
- await write(failurePayload(error)).catch(() => void 0);
237
- return;
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
- const event = {
240
- version: "run-stream.v3",
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,
@@ -540,11 +577,11 @@ function createAgents24BffHandlerWithResolver(options, resolveCustomerIdentity)
540
577
  cursor: 0
541
578
  }, (event) => {
542
579
  if (event.event !== "assistant.completed" || new URL(request.url).searchParams.get("stream_features")?.split(",").includes("assistant-completion-v1")) return write(event);
543
- }, { signal: request.signal });
580
+ }, { signal, onControl: control, leaseSeconds: 60 });
544
581
  }, {
545
582
  "X-Run-ID": admittedRunId,
546
583
  ...admission.thread_id ? { "X-Thread-ID": admission.thread_id } : {}
547
- }, void 0, streamFailure(request, "chat.stream"));
584
+ }, void 0, streamFailure(request, "chat.stream"), request.signal);
548
585
  }
549
586
  } else if (request.method === "POST" && /^\/runs\/[^/]+\/attach$/.test(path)) {
550
587
  const runId = routeId(path, /^\/runs\/([^/]+)\/attach$/, "runId");
@@ -553,7 +590,7 @@ function createAgents24BffHandlerWithResolver(options, resolveCustomerIdentity)
553
590
  if (cursor !== void 0 && (!Number.isInteger(cursor) || Number(cursor) < 0)) {
554
591
  throw new BffInputError(400, "INVALID_CURSOR", "cursor must be a non-negative integer.");
555
592
  }
556
- response = sseResponse(async (write) => {
593
+ response = sseResponse(async (write, signal, control) => {
557
594
  await embed.attachAgentRun(agentId, runId, {
558
595
  ...identity,
559
596
  runtime_target: runtimeTarget,
@@ -563,9 +600,12 @@ function createAgents24BffHandlerWithResolver(options, resolveCustomerIdentity)
563
600
  if (event.event !== "assistant.completed" || new URL(request.url).searchParams.get("stream_features")?.split(",").includes("assistant-completion-v1")) return write(event);
564
601
  }, {
565
602
  idempotencyKey,
566
- signal: request.signal
603
+ signal,
604
+ onControl: control,
605
+ leaseSeconds: 60,
606
+ ...url.searchParams.get("thread_id") ? { threadId: url.searchParams.get("thread_id") } : {}
567
607
  });
568
- }, void 0, void 0, streamFailure(request, "run.attach"));
608
+ }, void 0, void 0, streamFailure(request, "run.attach"), request.signal);
569
609
  } else if (request.method === "POST" && /^\/runs\/[^/]+\/cancel$/.test(path)) {
570
610
  const runId = routeId(path, /^\/runs\/([^/]+)\/cancel$/, "runId");
571
611
  response = json(await embed.cancelAgentRun(agentId, runId, {
@@ -615,19 +655,22 @@ function createAgents24BffHandlerWithResolver(options, resolveCustomerIdentity)
615
655
  if (cursor !== void 0 && (!Number.isInteger(cursor) || cursor < 0)) {
616
656
  throw new BffInputError(400, "INVALID_CURSOR", "cursor must be a non-negative integer.");
617
657
  }
618
- response = sseResponse(async (write) => {
658
+ response = sseResponse(async (write, signal, control) => {
619
659
  await embed.subscribeAgentThreadEvents(agentId, {
620
660
  ...threadIdentity,
621
661
  integrationId,
622
662
  ...cursor !== void 0 ? { cursor } : {},
623
- signal: request.signal
663
+ signal,
664
+ onControl: control,
665
+ leaseSeconds: 60,
666
+ ...url.searchParams.get("thread_id") ? { threadId: url.searchParams.get("thread_id") } : {}
624
667
  }, (event) => write(event));
625
668
  }, void 0, () => ({
626
669
  schema_version: "agents24.thread_summary_event.v1",
627
670
  event: "snapshot_required",
628
- cursor,
671
+ cursor: cursor ?? 0,
629
672
  reason: "stream_failed"
630
- }), streamFailure(request, "threads.events"));
673
+ }), streamFailure(request, "threads.events"), request.signal);
631
674
  } else if (/^\/threads\/[^/]+\/messages$/.test(path) && request.method === "GET") {
632
675
  const threadId = routeId(path, /^\/threads\/([^/]+)\/messages$/, "threadId");
633
676
  const before = url.searchParams.has("before") ? messageRevision(url.searchParams.get("before")) : void 0;
@@ -982,7 +1025,7 @@ function createResolvedAgents24BffHandler(options) {
982
1025
  }, { idempotencyKey: stringValue(request.headers.get("idempotency-key")) });
983
1026
  const admittedRunId = admission.run_id;
984
1027
  if (!admittedRunId) return mergeHeaders(json(admission, void 0, 202), resolution.responseHeaders);
985
- return mergeHeaders(sseResponse(async (write) => {
1028
+ return mergeHeaders(sseResponse(async (write, signal, control) => {
986
1029
  await options.client.embed.attachAgentRun(selected.agentId, admittedRunId, {
987
1030
  ...identity,
988
1031
  runtime_target: runtimeTarget,
@@ -992,11 +1035,11 @@ function createResolvedAgents24BffHandler(options) {
992
1035
  cursor: 0
993
1036
  }, (event) => {
994
1037
  if (event.event !== "assistant.completed" || new URL(request.url).searchParams.get("stream_features")?.split(",").includes("assistant-completion-v1")) return write(event);
995
- }, { signal: request.signal });
1038
+ }, { signal, onControl: control, leaseSeconds: 60 });
996
1039
  }, {
997
1040
  "X-Run-ID": admittedRunId,
998
1041
  ...admission.thread_id ? { "X-Thread-ID": admission.thread_id } : {}
999
- }, 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);
1000
1043
  }
1001
1044
  if (request.method === "GET" && path === "/threads") {
1002
1045
  const skip = Number(url.searchParams.get("skip") || 0);
@@ -1020,7 +1063,7 @@ function createResolvedAgents24BffHandler(options) {
1020
1063
  if (cursor !== void 0 && (!Number.isInteger(cursor) || cursor < 0)) {
1021
1064
  throw new BffInputError(400, "INVALID_CURSOR", "cursor must be a non-negative integer.");
1022
1065
  }
1023
- return mergeHeaders(sseResponse(async (write) => {
1066
+ return mergeHeaders(sseResponse(async (write, signal, control) => {
1024
1067
  await options.client.embed.subscribeAgentThreadEventsMulti({
1025
1068
  agentIds: agents.map((agent) => agent.agentId),
1026
1069
  externalUserId,
@@ -1028,14 +1071,17 @@ function createResolvedAgents24BffHandler(options) {
1028
1071
  ...cursor === void 0 ? {} : { cursor },
1029
1072
  runtimeTarget,
1030
1073
  integrationId,
1031
- signal: request.signal
1074
+ signal,
1075
+ onControl: control,
1076
+ leaseSeconds: 60,
1077
+ ...url.searchParams.get("thread_id") ? { threadId: url.searchParams.get("thread_id") } : {}
1032
1078
  }, (event) => write(event));
1033
1079
  }, void 0, () => ({
1034
1080
  schema_version: "agents24.thread_summary_event.v1",
1035
1081
  event: "snapshot_required",
1036
- cursor,
1082
+ cursor: cursor ?? 0,
1037
1083
  reason: "stream_failed"
1038
- }), (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);
1039
1085
  }
1040
1086
  if (/^\/threads\/[^/]+\/(?:messages(?:\/[^/]+)?|queue\/resume)$/.test(path) && ["GET", "POST", "PATCH", "DELETE"].includes(request.method)) {
1041
1087
  const threadId = routeId(path, /^\/threads\/([^/]+)\//, "threadId");