@agents24/node 0.5.32 → 0.5.33

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.cjs CHANGED
@@ -235,6 +235,48 @@ function computerFileQuery(url, requirePath) {
235
235
  };
236
236
  }
237
237
 
238
+ // src/bff-history.ts
239
+ function historyProjection(value, historical) {
240
+ const row = objectValue(value);
241
+ if (Array.isArray(row.items)) return { ...row, items: row.items.map((item) => historyProjection(item, historical)) };
242
+ if (row.thread) return { ...row, thread: historyProjection(row.thread, historical) };
243
+ if (row.runtime_selection) return { ...row, read_only: historical.has(String(objectValue(row.runtime_selection).agent_id)) };
244
+ return value;
245
+ }
246
+ function assertWritableAgent(agent, historical) {
247
+ if (historical.has(agent)) throw new BffInputError(409, "THREAD_READ_ONLY", "This conversation is read-only. Start a new chat.");
248
+ }
249
+ async function guardHistoricalRequest(input) {
250
+ const { request, path, historical } = input;
251
+ if (!historical.size || request.method === "GET" || request.method === "HEAD" || request.method === "DELETE" && /^\/threads\/[^/]+$/.test(path) || request.method === "POST" && (/^\/attachments\/[^/]+\/content-access$/.test(path) || /^\/runs\/[^/]+\/attach$/.test(path) || /^\/threads\/[^/]+\/computer\/content-access$/.test(path))) return;
252
+ let threadId = path.match(/^\/threads\/([^/]+)/)?.[1];
253
+ if (path === "/chat/stream") threadId = stringValue((await requestJson(request.clone())).threadId);
254
+ if (path === "/attachments/upload") threadId = stringValue((await requestMultipart(request.clone())).get("thread_id"));
255
+ const scope = {
256
+ externalUserId: input.externalUserId,
257
+ integrationId: input.integrationId,
258
+ externalSessionId: input.resolution.principal.externalSessionId,
259
+ chatScope: input.resolution.chatScope,
260
+ runtimeTarget: input.runtimeTarget
261
+ };
262
+ if (threadId) {
263
+ const thread = await input.embed.getConversationThread(decodeURIComponent(threadId), { ...scope, agentIds: input.agentIds });
264
+ assertWritableAgent(String(thread.runtime_selection.agent_id), historical);
265
+ }
266
+ const runId = path.match(/^\/runs\/([^/]+)/)?.[1];
267
+ if (runId) {
268
+ const run = await input.embed.resolveConversationRun(decodeURIComponent(runId), {
269
+ agent_ids: input.agentIds,
270
+ external_user_id: scope.externalUserId,
271
+ integration_id: scope.integrationId,
272
+ external_session_id: scope.externalSessionId,
273
+ chat_scope: scope.chatScope,
274
+ runtime_target: scope.runtimeTarget
275
+ });
276
+ assertWritableAgent(run.agent_id, historical);
277
+ }
278
+ }
279
+
238
280
  // src/bff-conversation-submission.ts
239
281
  async function submitConversationStream(input) {
240
282
  const { request, body, resolution } = input;
@@ -677,12 +719,14 @@ function createAgents24BffHandlerWithResolver(options, resolveCustomerIdentity)
677
719
  input,
678
720
  attachmentIds,
679
721
  ...resolution.customerTenant === void 0 ? {} : { customerTenant: resolution.customerTenant },
722
+ ...resolution.trustedContext === void 0 ? {} : { trustedContext: resolution.trustedContext },
680
723
  ...stringValue(body.requestedModelId) ? { requestedModelId: stringValue(body.requestedModelId) } : {},
681
724
  ...resolution.knowledgeNamespaces === void 0 ? {} : { knowledgeNamespaces: resolution.knowledgeNamespaces },
682
725
  toolInputs: objectValue(body.toolInputs),
683
726
  ...body.metadata === void 0 ? {} : { metadata: objectValue(body.metadata) }
684
727
  }, { idempotencyKey }) : await embed.startAgentRun(agentId, {
685
728
  ...resolution.customerTenant === void 0 ? {} : { customer_tenant: resolution.customerTenant },
729
+ ...resolution.trustedContext === void 0 ? {} : { trusted_context: resolution.trustedContext },
686
730
  ...identity,
687
731
  integration_id: integrationId,
688
732
  ...resolution.chatScope === void 0 ? {} : { chat_scope: resolution.chatScope },
@@ -827,7 +871,7 @@ function createAgents24BffHandlerWithResolver(options, resolveCustomerIdentity)
827
871
  response = json(await embed.submitAgentThreadMessage(
828
872
  agentId,
829
873
  threadId,
830
- { ...threadIdentity, integrationId, ...messageBody(body), ...resolution.knowledgeNamespaces === void 0 ? {} : { knowledgeNamespaces: resolution.knowledgeNamespaces }, agentVersionId: agentVersionId ?? null },
874
+ { ...threadIdentity, integrationId, ...messageBody(body), ...resolution.trustedContext === void 0 ? {} : { trustedContext: resolution.trustedContext }, ...resolution.customerTenant === void 0 ? {} : { customerTenant: resolution.customerTenant }, ...resolution.knowledgeNamespaces === void 0 ? {} : { knowledgeNamespaces: resolution.knowledgeNamespaces }, agentVersionId: agentVersionId ?? null },
831
875
  { idempotencyKey }
832
876
  ));
833
877
  } else if (/^\/threads\/[^/]+\/messages\/[^/]+$/.test(path) && request.method === "PATCH") {
@@ -1008,6 +1052,7 @@ function createAgents24BffHandlerWithResolver(options, resolveCustomerIdentity)
1008
1052
 
1009
1053
  // src/bff-multi-agent.ts
1010
1054
  function createResolvedAgents24BffHandler(options, sharedIdentity) {
1055
+ const historical = new Set(options.readOnlyAgentIds || []);
1011
1056
  const runtimeTarget = options.runtimeTarget || "published";
1012
1057
  const basePath = normalizeBasePath(options.basePath);
1013
1058
  const integrationId = normalized(options.integrationId, "integrationId");
@@ -1065,6 +1110,7 @@ function createResolvedAgents24BffHandler(options, sharedIdentity) {
1065
1110
  }
1066
1111
  try {
1067
1112
  const externalUserId = await resolveCustomerIdentity(resolution.principal);
1113
+ await guardHistoricalRequest({ request, path, historical, agentIds: agents.map((row) => row.agentId), embed: options.client.embed, resolution, externalUserId, integrationId, runtimeTarget });
1068
1114
  await options.onPrincipalResolved?.(request, resolution, externalUserId);
1069
1115
  const identity = {
1070
1116
  external_user_id: externalUserId,
@@ -1121,18 +1167,20 @@ function createResolvedAgents24BffHandler(options, sharedIdentity) {
1121
1167
  if (request.method === "POST" && path === "/chat/stream") {
1122
1168
  const body = await requestJson(request);
1123
1169
  const threadId = stringValue(body.threadId);
1170
+ if (threadId && body.requestedAgentId) assertWritableAgent((await requestedAgent(body.requestedAgentId)).agentId, historical);
1124
1171
  if (threadId) return submitConversationStream({
1125
1172
  options,
1126
1173
  request,
1127
1174
  body,
1128
1175
  resolution,
1129
- agents,
1176
+ agents: agents.filter((agent) => !historical.has(agent.agentId)),
1130
1177
  identity,
1131
1178
  integrationId,
1132
1179
  runtimeTarget,
1133
1180
  requestedAgentId: body.requestedAgentId ? (await requestedAgent(body.requestedAgentId)).agentId : void 0
1134
1181
  });
1135
1182
  const selected = await requestedAgent(body.requestedAgentId);
1183
+ assertWritableAgent(selected.agentId, historical);
1136
1184
  const input = stringValue(body.input);
1137
1185
  const attachmentIds = stringList(body.attachmentIds);
1138
1186
  if (!input && attachmentIds.length === 0) throw new BffInputError(400, "INPUT_REQUIRED", "A message or attachment is required.");
@@ -1181,7 +1229,7 @@ function createResolvedAgents24BffHandler(options, sharedIdentity) {
1181
1229
  if (!Number.isInteger(skip) || skip < 0 || !Number.isInteger(limit) || limit < 1 || limit > 100) {
1182
1230
  throw new BffInputError(400, "INVALID_PAGINATION", "skip must be non-negative and limit must be between 1 and 100.");
1183
1231
  }
1184
- return mergeHeaders(json(await options.client.embed.listAgentThreadsMulti({
1232
+ return mergeHeaders(json(historyProjection(await options.client.embed.listAgentThreadsMulti({
1185
1233
  agent_ids: agents.map((agent) => agent.agentId),
1186
1234
  external_user_id: externalUserId,
1187
1235
  ...resolution.principal.externalSessionId ? { external_session_id: resolution.principal.externalSessionId } : {},
@@ -1190,7 +1238,7 @@ function createResolvedAgents24BffHandler(options, sharedIdentity) {
1190
1238
  ...resolution.chatScope === void 0 ? {} : { chat_scope: resolution.chatScope },
1191
1239
  skip,
1192
1240
  limit
1193
- })), resolution.responseHeaders);
1241
+ }), historical)), resolution.responseHeaders);
1194
1242
  }
1195
1243
  if (request.method === "GET" && path === "/threads/events") {
1196
1244
  const rawCursor = url.searchParams.get("cursor");
@@ -1211,7 +1259,7 @@ function createResolvedAgents24BffHandler(options, sharedIdentity) {
1211
1259
  onControl: control,
1212
1260
  leaseSeconds: 60,
1213
1261
  ...url.searchParams.get("thread_id") ? { threadId: url.searchParams.get("thread_id") } : {}
1214
- }, (event) => write(event));
1262
+ }, (event) => write(historyProjection(event, historical)));
1215
1263
  }, void 0, () => ({
1216
1264
  schema_version: "agents24.thread_summary_event.v1",
1217
1265
  event: "snapshot_required",
@@ -1229,8 +1277,11 @@ function createResolvedAgents24BffHandler(options, sharedIdentity) {
1229
1277
  if (/^\/threads\/[^/]+\/(?:messages(?:\/[^/]+)?|queue\/resume)$/.test(path) && ["GET", "POST", "PATCH", "DELETE"].includes(request.method)) {
1230
1278
  const threadId = routeId(path, /^\/threads\/([^/]+)\//, "threadId");
1231
1279
  const storedAgent = await resolveThreadAgent(threadId);
1280
+ if (request.method !== "GET") assertWritableAgent(storedAgent, historical);
1232
1281
  const message = request.method === "PATCH" || request.method === "POST" && path.endsWith("/messages") ? await request.clone().json() : void 0;
1233
- return dispatchToAgent((await requestedAgent(message?.requestedAgentId || storedAgent)).agentId, message);
1282
+ const selected = (await requestedAgent(message?.requestedAgentId || storedAgent)).agentId;
1283
+ if (request.method !== "GET") assertWritableAgent(selected, historical);
1284
+ return dispatchToAgent(selected, message);
1234
1285
  }
1235
1286
  if (/^\/threads\/[^/]+\/computer(?:\/files|\/file|\/content-access)?$/.test(path) && (request.method === "GET" || request.method === "POST" && path.endsWith("/content-access"))) {
1236
1287
  const threadId = routeId(path, /^\/threads\/([^/]+)\/computer/, "threadId");
@@ -1275,7 +1326,7 @@ function createResolvedAgents24BffHandler(options, sharedIdentity) {
1275
1326
  if (detailLimit !== void 0 && (!Number.isInteger(detailLimit) || detailLimit < 1 || detailLimit > 100)) {
1276
1327
  throw new BffInputError(400, "INVALID_PAGINATION", "limit must be between 1 and 100.");
1277
1328
  }
1278
- return mergeHeaders(json(await options.client.embed.getConversationThread(threadId, {
1329
+ return mergeHeaders(json(historyProjection(await options.client.embed.getConversationThread(threadId, {
1279
1330
  agentIds: agents.map((agent) => agent.agentId),
1280
1331
  externalUserId,
1281
1332
  ...resolution.principal.externalSessionId ? { externalSessionId: resolution.principal.externalSessionId } : {},
@@ -1284,12 +1335,13 @@ function createResolvedAgents24BffHandler(options, sharedIdentity) {
1284
1335
  runtimeTarget,
1285
1336
  beforeTurnIndex,
1286
1337
  limit: detailLimit
1287
- })), resolution.responseHeaders);
1338
+ }), historical)), resolution.responseHeaders);
1288
1339
  }
1289
1340
  if (/^\/threads\/[^/]+\/runtime-selection$/.test(path) && request.method === "PATCH") {
1290
1341
  const threadId = routeId(path, /^\/threads\/([^/]+)\//, "threadId");
1291
1342
  const body = await requestJson(request);
1292
1343
  const selected = await requestedAgent(body.preferredAgentId);
1344
+ assertWritableAgent(selected.agentId, historical);
1293
1345
  return mergeHeaders(json(await options.client.embed.setConversationRuntimeSelection(threadId, {
1294
1346
  agent_ids: agents.map((agent) => agent.agentId),
1295
1347
  preferred_agent_id: selected.agentId,
@@ -1314,6 +1366,7 @@ function createResolvedAgents24BffHandler(options, sharedIdentity) {
1314
1366
  if (request.method === "POST" && path === "/attachments/upload") {
1315
1367
  const form = await requestMultipart(request.clone());
1316
1368
  const selected = await requestedAgent(form.get("requested_agent_id"));
1369
+ assertWritableAgent(selected.agentId, historical);
1317
1370
  const policy = await options.client.embed.getResourcePolicy(selected.agentId, {
1318
1371
  external_user_id: externalUserId,
1319
1372
  integration_id: integrationId,
@@ -1324,7 +1377,22 @@ function createResolvedAgents24BffHandler(options, sharedIdentity) {
1324
1377
  return dispatchToAgent(selected.agentId);
1325
1378
  }
1326
1379
  if (request.method === "POST" && /^\/attachments\/[^/]+\/content-access$/.test(path)) {
1327
- return dispatchToAgent(agents[0].agentId);
1380
+ const body = await requestJson(request);
1381
+ if (body.disposition !== "inline" && body.disposition !== "attachment") throw new BffInputError(400, "INVALID_DISPOSITION", "Attachment disposition is invalid.");
1382
+ for (const [index, agent] of agents.entries()) {
1383
+ try {
1384
+ return mergeHeaders(json(await options.client.embed.createAttachmentContentAccess(agent.agentId, routeId(path, /^\/attachments\/([^/]+)\/content-access$/, "attachmentId"), {
1385
+ ...identity,
1386
+ integration_id: integrationId,
1387
+ runtime_target: runtimeTarget,
1388
+ ...resolution.chatScope === void 0 ? {} : { chat_scope: resolution.chatScope },
1389
+ disposition: body.disposition
1390
+ })), resolution.responseHeaders);
1391
+ } catch (error) {
1392
+ if (error.status !== 404 || index === agents.length - 1) throw error;
1393
+ }
1394
+ }
1395
+ throw new BffInputError(404, "NOT_FOUND", "Attachment not found.");
1328
1396
  }
1329
1397
  if (/^\/runs\/[^/]+\/(?:attach|cancel|feedback)$/.test(path) || /^\/runs\/[^/]+\/hitl\/[^/]+\/resume$/.test(path) || /^\/runs\/[^/]+\/mcp\/servers\/[^/]+\/auth\/start$/.test(path)) {
1330
1398
  const runId = routeId(path, /^\/runs\/([^/]+)\//, "runId");
@@ -1416,6 +1484,11 @@ async function runtimeBootstrap(options, request, identityResolver) {
1416
1484
  // src/bff.ts
1417
1485
  function createAgents24BffHandler(options) {
1418
1486
  const resolveAgents = prepareAgents24BffAgentResolution(options);
1487
+ const resolveHistory = options.readOnlyAgents?.length ? prepareAgents24BffAgentResolution({ ...options, agents: options.readOnlyAgents }) : async () => [];
1488
+ const withHistory = async (agents) => {
1489
+ const history = await resolveHistory();
1490
+ return createResolvedAgents24BffHandler({ ...options, agents: [...agents, ...history], readOnlyAgentIds: history.map((row) => row.agentId) }, identity);
1491
+ };
1419
1492
  const identity = createCustomerIdentityResolver(options.client.embed, options.integrationId, options.integrationName);
1420
1493
  const basePath = normalizeBasePath(options.basePath);
1421
1494
  let resolvedHandler;
@@ -1425,10 +1498,10 @@ function createAgents24BffHandler(options) {
1425
1498
  if (request.method === "GET" && new URL(request.url).pathname === `${basePath}/bootstrap`) {
1426
1499
  const { response, result } = await runtimeBootstrap(options, request, identity);
1427
1500
  const agents = result.resolved_agents.map((row) => ({ agentId: row.agent_id, agentAlias: row.agent_alias, agentVersionId: row.agent_version_id || void 0 }));
1428
- resolvedHandler = Promise.resolve(createResolvedAgents24BffHandler({ ...options, agents }, identity));
1501
+ resolvedHandler = Promise.resolve(await withHistory(agents));
1429
1502
  return response;
1430
1503
  }
1431
- resolvedHandler ||= resolveAgents().then((agents) => createResolvedAgents24BffHandler({ ...options, agents }, identity));
1504
+ resolvedHandler ||= resolveAgents().then(withHistory);
1432
1505
  return await (await resolvedHandler)(request);
1433
1506
  } catch (error) {
1434
1507
  resolvedHandler = void 0;