@agents24/node 0.5.17 → 0.5.19

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/bff.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { A as Agents24 } from './client-ChHref1j.cjs';
1
+ import { A as Agents24 } from './client-YEw6vRtg.cjs';
2
2
  import '@agents24/client/protocol';
3
3
 
4
4
  interface AgentBffPrincipal {
package/dist/bff.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { A as Agents24 } from './client-ChHref1j.js';
1
+ import { A as Agents24 } from './client-YEw6vRtg.js';
2
2
  import '@agents24/client/protocol';
3
3
 
4
4
  interface AgentBffPrincipal {
package/dist/bff.js CHANGED
@@ -221,31 +221,68 @@ function sseHeaders(headers) {
221
221
  result.set("X-Accel-Buffering", "no");
222
222
  return result;
223
223
  }
224
- function sseResponse(execute, headers, failurePayload, onFailure) {
225
- const stream = new TransformStream();
226
- const writer = stream.writable.getWriter();
224
+ function sseResponse(execute, headers, failurePayload, onFailure, requestSignal, leaseSeconds = 60) {
225
+ const lifetime = new AbortController();
227
226
  const encoder = new TextEncoder();
228
- const write = (value) => writer.write(encoder.encode(`data: ${JSON.stringify(value)}
227
+ let sink;
228
+ let wake;
229
+ const abort = () => {
230
+ lifetime.abort(requestSignal?.reason);
231
+ wake?.();
232
+ };
233
+ requestSignal?.addEventListener("abort", abort, { once: true });
234
+ const stream = new ReadableStream({
235
+ start(controller) {
236
+ sink = controller;
237
+ },
238
+ pull() {
239
+ wake?.();
240
+ wake = void 0;
241
+ },
242
+ cancel(reason) {
243
+ lifetime.abort(reason);
244
+ wake?.();
245
+ }
246
+ });
247
+ const writeFrame = async (frame) => {
248
+ if (lifetime.signal.aborted) return;
249
+ if ((sink.desiredSize ?? 0) <= 0) await new Promise((resolve) => {
250
+ wake = resolve;
251
+ });
252
+ if (!lifetime.signal.aborted) sink.enqueue(encoder.encode(frame));
253
+ };
254
+ const write = (value) => writeFrame(`data: ${JSON.stringify(value)}
229
255
 
230
- `));
231
- void execute(write).catch(async (error) => {
256
+ `);
257
+ const control = (value) => {
258
+ const { type, ...metadata } = value;
259
+ return writeFrame(`: agents24.${type} ${JSON.stringify(metadata)}
260
+
261
+ `);
262
+ };
263
+ const timer = setTimeout(() => {
264
+ if (!lifetime.signal.aborted) {
265
+ sink.enqueue(encoder.encode(": agents24.lease_expired {}\n\n"));
266
+ lifetime.abort("Observation lease expired.");
267
+ wake?.();
268
+ }
269
+ }, leaseSeconds * 1e3);
270
+ if (requestSignal?.aborted) abort();
271
+ void execute(write, lifetime.signal, control).catch(async (error) => {
272
+ if (lifetime.signal.aborted) return;
232
273
  onFailure?.(error);
233
- if (failurePayload) {
234
- await write(failurePayload(error)).catch(() => void 0);
235
- return;
274
+ if (failurePayload) await write(failurePayload(error)).catch(() => void 0);
275
+ }).finally(() => {
276
+ clearTimeout(timer);
277
+ requestSignal?.removeEventListener("abort", abort);
278
+ lifetime.abort();
279
+ wake?.();
280
+ try {
281
+ sink.close();
282
+ } catch {
236
283
  }
237
- const event = {
238
- version: "run-stream.v3",
239
- seq: Number.MAX_SAFE_INTEGER,
240
- ts: (/* @__PURE__ */ new Date()).toISOString(),
241
- event: "run.failed",
242
- run_id: "bff-stream-error",
243
- stage: "run",
244
- payload: { error: "The Agent stream failed." }
245
- };
246
- await write(event).catch(() => void 0);
247
- }).finally(() => writer.close().catch(() => void 0));
248
- return new Response(stream.readable, { headers: sseHeaders(headers) });
284
+ });
285
+ return new Response(stream, { headers: sseHeaders(headers) });
249
286
  }
250
287
 
251
288
  // src/bff-values.ts
@@ -528,7 +565,7 @@ function createAgents24BffHandlerWithResolver(options, resolveCustomerIdentity)
528
565
  if (!admittedRunId) {
529
566
  response = json(admission, void 0, 202);
530
567
  } else {
531
- response = sseResponse(async (write) => {
568
+ response = sseResponse(async (write, signal, control) => {
532
569
  await embed.attachAgentRun(agentId, admittedRunId, {
533
570
  ...identity,
534
571
  integration_id: integrationId,
@@ -538,11 +575,11 @@ function createAgents24BffHandlerWithResolver(options, resolveCustomerIdentity)
538
575
  cursor: 0
539
576
  }, (event) => {
540
577
  if (event.event !== "assistant.completed" || new URL(request.url).searchParams.get("stream_features")?.split(",").includes("assistant-completion-v1")) return write(event);
541
- }, { signal: request.signal });
578
+ }, { signal, onControl: control, leaseSeconds: 60 });
542
579
  }, {
543
580
  "X-Run-ID": admittedRunId,
544
581
  ...admission.thread_id ? { "X-Thread-ID": admission.thread_id } : {}
545
- }, void 0, streamFailure(request, "chat.stream"));
582
+ }, void 0, streamFailure(request, "chat.stream"), request.signal);
546
583
  }
547
584
  } else if (request.method === "POST" && /^\/runs\/[^/]+\/attach$/.test(path)) {
548
585
  const runId = routeId(path, /^\/runs\/([^/]+)\/attach$/, "runId");
@@ -551,7 +588,7 @@ function createAgents24BffHandlerWithResolver(options, resolveCustomerIdentity)
551
588
  if (cursor !== void 0 && (!Number.isInteger(cursor) || Number(cursor) < 0)) {
552
589
  throw new BffInputError(400, "INVALID_CURSOR", "cursor must be a non-negative integer.");
553
590
  }
554
- response = sseResponse(async (write) => {
591
+ response = sseResponse(async (write, signal, control) => {
555
592
  await embed.attachAgentRun(agentId, runId, {
556
593
  ...identity,
557
594
  runtime_target: runtimeTarget,
@@ -561,9 +598,12 @@ function createAgents24BffHandlerWithResolver(options, resolveCustomerIdentity)
561
598
  if (event.event !== "assistant.completed" || new URL(request.url).searchParams.get("stream_features")?.split(",").includes("assistant-completion-v1")) return write(event);
562
599
  }, {
563
600
  idempotencyKey,
564
- signal: request.signal
601
+ signal,
602
+ onControl: control,
603
+ leaseSeconds: 60,
604
+ ...url.searchParams.get("thread_id") ? { threadId: url.searchParams.get("thread_id") } : {}
565
605
  });
566
- }, void 0, void 0, streamFailure(request, "run.attach"));
606
+ }, void 0, void 0, streamFailure(request, "run.attach"), request.signal);
567
607
  } else if (request.method === "POST" && /^\/runs\/[^/]+\/cancel$/.test(path)) {
568
608
  const runId = routeId(path, /^\/runs\/([^/]+)\/cancel$/, "runId");
569
609
  response = json(await embed.cancelAgentRun(agentId, runId, {
@@ -613,19 +653,22 @@ function createAgents24BffHandlerWithResolver(options, resolveCustomerIdentity)
613
653
  if (cursor !== void 0 && (!Number.isInteger(cursor) || cursor < 0)) {
614
654
  throw new BffInputError(400, "INVALID_CURSOR", "cursor must be a non-negative integer.");
615
655
  }
616
- response = sseResponse(async (write) => {
656
+ response = sseResponse(async (write, signal, control) => {
617
657
  await embed.subscribeAgentThreadEvents(agentId, {
618
658
  ...threadIdentity,
619
659
  integrationId,
620
660
  ...cursor !== void 0 ? { cursor } : {},
621
- signal: request.signal
661
+ signal,
662
+ onControl: control,
663
+ leaseSeconds: 60,
664
+ ...url.searchParams.get("thread_id") ? { threadId: url.searchParams.get("thread_id") } : {}
622
665
  }, (event) => write(event));
623
666
  }, void 0, () => ({
624
667
  schema_version: "agents24.thread_summary_event.v1",
625
668
  event: "snapshot_required",
626
- cursor,
669
+ cursor: cursor ?? 0,
627
670
  reason: "stream_failed"
628
- }), streamFailure(request, "threads.events"));
671
+ }), streamFailure(request, "threads.events"), request.signal);
629
672
  } else if (/^\/threads\/[^/]+\/messages$/.test(path) && request.method === "GET") {
630
673
  const threadId = routeId(path, /^\/threads\/([^/]+)\/messages$/, "threadId");
631
674
  const before = url.searchParams.has("before") ? messageRevision(url.searchParams.get("before")) : void 0;
@@ -980,7 +1023,7 @@ function createResolvedAgents24BffHandler(options) {
980
1023
  }, { idempotencyKey: stringValue(request.headers.get("idempotency-key")) });
981
1024
  const admittedRunId = admission.run_id;
982
1025
  if (!admittedRunId) return mergeHeaders(json(admission, void 0, 202), resolution.responseHeaders);
983
- return mergeHeaders(sseResponse(async (write) => {
1026
+ return mergeHeaders(sseResponse(async (write, signal, control) => {
984
1027
  await options.client.embed.attachAgentRun(selected.agentId, admittedRunId, {
985
1028
  ...identity,
986
1029
  runtime_target: runtimeTarget,
@@ -990,11 +1033,11 @@ function createResolvedAgents24BffHandler(options) {
990
1033
  cursor: 0
991
1034
  }, (event) => {
992
1035
  if (event.event !== "assistant.completed" || new URL(request.url).searchParams.get("stream_features")?.split(",").includes("assistant-completion-v1")) return write(event);
993
- }, { signal: request.signal });
1036
+ }, { signal, onControl: control, leaseSeconds: 60 });
994
1037
  }, {
995
1038
  "X-Run-ID": admittedRunId,
996
1039
  ...admission.thread_id ? { "X-Thread-ID": admission.thread_id } : {}
997
- }, void 0, (error) => reportFailureResponse(options.onDiagnostic, request, "chat.stream", errorResponse(error))), resolution.responseHeaders);
1040
+ }, void 0, (error) => reportFailureResponse(options.onDiagnostic, request, "chat.stream", errorResponse(error)), request.signal), resolution.responseHeaders);
998
1041
  }
999
1042
  if (request.method === "GET" && path === "/threads") {
1000
1043
  const skip = Number(url.searchParams.get("skip") || 0);
@@ -1018,7 +1061,7 @@ function createResolvedAgents24BffHandler(options) {
1018
1061
  if (cursor !== void 0 && (!Number.isInteger(cursor) || cursor < 0)) {
1019
1062
  throw new BffInputError(400, "INVALID_CURSOR", "cursor must be a non-negative integer.");
1020
1063
  }
1021
- return mergeHeaders(sseResponse(async (write) => {
1064
+ return mergeHeaders(sseResponse(async (write, signal, control) => {
1022
1065
  await options.client.embed.subscribeAgentThreadEventsMulti({
1023
1066
  agentIds: agents.map((agent) => agent.agentId),
1024
1067
  externalUserId,
@@ -1026,14 +1069,17 @@ function createResolvedAgents24BffHandler(options) {
1026
1069
  ...cursor === void 0 ? {} : { cursor },
1027
1070
  runtimeTarget,
1028
1071
  integrationId,
1029
- signal: request.signal
1072
+ signal,
1073
+ onControl: control,
1074
+ leaseSeconds: 60,
1075
+ ...url.searchParams.get("thread_id") ? { threadId: url.searchParams.get("thread_id") } : {}
1030
1076
  }, (event) => write(event));
1031
1077
  }, void 0, () => ({
1032
1078
  schema_version: "agents24.thread_summary_event.v1",
1033
1079
  event: "snapshot_required",
1034
- cursor,
1080
+ cursor: cursor ?? 0,
1035
1081
  reason: "stream_failed"
1036
- }), (error) => reportFailureResponse(options.onDiagnostic, request, "threads.events", errorResponse(error))), resolution.responseHeaders);
1082
+ }), (error) => reportFailureResponse(options.onDiagnostic, request, "threads.events", errorResponse(error)), request.signal), resolution.responseHeaders);
1037
1083
  }
1038
1084
  if (/^\/threads\/[^/]+\/(?:messages(?:\/[^/]+)?|queue\/resume)$/.test(path) && ["GET", "POST", "PATCH", "DELETE"].includes(request.method)) {
1039
1085
  const threadId = routeId(path, /^\/threads\/([^/]+)\//, "threadId");