@agents24/node 0.5.22 → 0.5.24
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 +8 -8
- package/dist/bff.cjs +314 -199
- package/dist/bff.cjs.map +1 -1
- package/dist/bff.d.cts +3 -1
- package/dist/bff.d.ts +3 -1
- package/dist/bff.js +314 -199
- package/dist/bff.js.map +1 -1
- package/dist/{client-CR3usQ8l.d.cts → client-DMkQFCYV.d.cts} +476 -191
- package/dist/{client-CR3usQ8l.d.ts → client-DMkQFCYV.d.ts} +476 -191
- package/dist/index.cjs +211 -19
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +211 -19
- package/dist/index.js.map +1 -1
- package/dist/types/bff-agent-resolution.d.ts +2 -0
- package/dist/types/bff-agent-resolution.d.ts.map +1 -1
- package/dist/types/bff-delegations.d.ts +5 -0
- package/dist/types/bff-delegations.d.ts.map +1 -0
- package/dist/types/bff-request.d.ts +36 -0
- package/dist/types/bff-request.d.ts.map +1 -0
- package/dist/types/bff.d.ts.map +1 -1
- package/dist/types/delegations.d.ts +1 -78
- package/dist/types/delegations.d.ts.map +1 -1
- package/dist/types/embed-types.d.ts +302 -0
- package/dist/types/embed-types.d.ts.map +1 -0
- package/dist/types/embed.d.ts +3 -2
- package/dist/types/embed.d.ts.map +1 -1
- package/dist/types/generated/delegation-types.d.ts +130 -0
- package/dist/types/generated/delegation-types.d.ts.map +1 -0
- package/dist/types/generated/embedDelegations.d.ts +12 -0
- package/dist/types/generated/embedDelegations.d.ts.map +1 -0
- package/dist/types/generated/ingestion-types.d.ts +155 -3
- package/dist/types/generated/ingestion-types.d.ts.map +1 -1
- package/dist/types/generated/ingestionJobs.d.ts +6 -1
- package/dist/types/generated/ingestionJobs.d.ts.map +1 -1
- package/dist/types/generated/knowledgeStores.d.ts +8 -1
- package/dist/types/generated/knowledgeStores.d.ts.map +1 -1
- package/dist/types/generated/manifest.d.ts +1 -1
- package/dist/types/generated/manifest.d.ts.map +1 -1
- package/dist/types/generated/model-capabilities.d.ts +31 -0
- package/dist/types/generated/model-capabilities.d.ts.map +1 -0
- package/dist/types/generated/resourcePackages.d.ts.map +1 -1
- package/dist/types/types.d.ts +13 -279
- package/dist/types/types.d.ts.map +1 -1
- package/package.json +2 -2
package/dist/bff.js
CHANGED
|
@@ -1,5 +1,231 @@
|
|
|
1
1
|
import { randomUUID } from 'crypto';
|
|
2
2
|
|
|
3
|
+
// src/bff-values.ts
|
|
4
|
+
function normalized(value, field) {
|
|
5
|
+
const result = String(value || "").trim();
|
|
6
|
+
if (!result || /[\r\n\0]/.test(result)) throw new Error(`${field} is required.`);
|
|
7
|
+
return result;
|
|
8
|
+
}
|
|
9
|
+
function stringValue(value) {
|
|
10
|
+
const result = typeof value === "string" ? value.trim() : "";
|
|
11
|
+
return result || void 0;
|
|
12
|
+
}
|
|
13
|
+
function stringList(value) {
|
|
14
|
+
return Array.isArray(value) ? value.map(stringValue).filter((item) => Boolean(item)) : [];
|
|
15
|
+
}
|
|
16
|
+
function objectValue(value) {
|
|
17
|
+
return value && typeof value === "object" && !Array.isArray(value) ? value : {};
|
|
18
|
+
}
|
|
19
|
+
function bootstrapDocument(raw, agentVersionId) {
|
|
20
|
+
const features = objectValue(raw.features);
|
|
21
|
+
const selectable = objectValue(features.models).selectable === true;
|
|
22
|
+
const resolvedVersionId = stringValue(raw.agent_version_id) || agentVersionId || "";
|
|
23
|
+
const versionNumber = Number(raw.agent_version_number || 0);
|
|
24
|
+
return {
|
|
25
|
+
version: String(raw.version || "agents24.embed.bootstrap.v1"),
|
|
26
|
+
deployment: {
|
|
27
|
+
id: "server-embed",
|
|
28
|
+
name: normalized(String(raw.agent_name || ""), "agent_name"),
|
|
29
|
+
version_policy: agentVersionId ? { mode: "pinned", version_id: agentVersionId } : { mode: "latest_published" },
|
|
30
|
+
resolved_agent_version_id: resolvedVersionId,
|
|
31
|
+
resolved_agent_version_number: Number.isFinite(versionNumber) ? versionNumber : 0,
|
|
32
|
+
policy_epoch: 0
|
|
33
|
+
},
|
|
34
|
+
authentication: { modes: ["server_embed"], access_token_lifetime_seconds: 0, jwks_path: "" },
|
|
35
|
+
capabilities: ["attachments.read", "attachments.write", "feedback.write", ...selectable ? ["models.select"] : []],
|
|
36
|
+
features,
|
|
37
|
+
native_profiles: []
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// src/bff-request.ts
|
|
42
|
+
var JSON_LIMIT_BYTES = 1048576;
|
|
43
|
+
var MAX_INPUT_LENGTH = 2e5;
|
|
44
|
+
var MAX_ATTACHMENTS = 10;
|
|
45
|
+
var MAX_ATTACHMENT_BYTES = 25 * 1024 * 1024;
|
|
46
|
+
var HITL_ACTIONS = /* @__PURE__ */ new Set(["approve", "reject", "connect", "skip", "respond"]);
|
|
47
|
+
var CUSTOMER_IDENTITY_CACHE_TTL_MS = 6e4;
|
|
48
|
+
var CUSTOMER_IDENTITY_CACHE_MAX_ENTRIES = 1024;
|
|
49
|
+
function createCustomerIdentityResolver(embed, integrationId, integrationName) {
|
|
50
|
+
const cache = /* @__PURE__ */ new Map();
|
|
51
|
+
return async (principal) => {
|
|
52
|
+
const profile = {
|
|
53
|
+
display_name: principal.displayName,
|
|
54
|
+
email: principal.email,
|
|
55
|
+
avatar_url: principal.avatarUrl
|
|
56
|
+
};
|
|
57
|
+
const profileFields = Object.fromEntries(Object.entries(profile).filter(([, value2]) => value2 !== void 0));
|
|
58
|
+
const key = JSON.stringify([integrationId, principal.issuer, principal.subject, profileFields]);
|
|
59
|
+
const now = Date.now();
|
|
60
|
+
const cached = cache.get(key);
|
|
61
|
+
if (cached && cached.expiresAt > now) {
|
|
62
|
+
cache.delete(key);
|
|
63
|
+
cache.set(key, cached);
|
|
64
|
+
return cached.value;
|
|
65
|
+
}
|
|
66
|
+
if (cached) cache.delete(key);
|
|
67
|
+
const value = embed.resolveCustomerPrincipal({
|
|
68
|
+
integration_id: integrationId,
|
|
69
|
+
...integrationName ? { integration_name: integrationName } : {},
|
|
70
|
+
...principal.issuer ? { issuer: principal.issuer } : {},
|
|
71
|
+
subject: principal.subject,
|
|
72
|
+
...Object.keys(profileFields).length ? { profile: profileFields } : {}
|
|
73
|
+
}).then((result) => normalized(result.customer_principal_ref, "customer_principal_ref"));
|
|
74
|
+
const entry = { expiresAt: now + CUSTOMER_IDENTITY_CACHE_TTL_MS, value };
|
|
75
|
+
cache.set(key, entry);
|
|
76
|
+
while (cache.size > CUSTOMER_IDENTITY_CACHE_MAX_ENTRIES) {
|
|
77
|
+
const oldest = cache.keys().next().value;
|
|
78
|
+
if (oldest === void 0) break;
|
|
79
|
+
cache.delete(oldest);
|
|
80
|
+
}
|
|
81
|
+
void value.catch(() => {
|
|
82
|
+
if (cache.get(key) === entry) cache.delete(key);
|
|
83
|
+
});
|
|
84
|
+
return value;
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
function normalizeBasePath(value) {
|
|
88
|
+
const path = String(value || "/api/agents24").trim().replace(/\/+$/, "");
|
|
89
|
+
if (!path.startsWith("/") || /[\r\n\0?#]/.test(path)) throw new Error("basePath must be an absolute path.");
|
|
90
|
+
return path;
|
|
91
|
+
}
|
|
92
|
+
function browserRequestIsSameOrigin(request) {
|
|
93
|
+
const fetchSite = String(request.headers.get("sec-fetch-site") || "").trim().toLowerCase();
|
|
94
|
+
if (fetchSite) return fetchSite === "same-origin";
|
|
95
|
+
const rawOrigin = String(request.headers.get("origin") || "").trim();
|
|
96
|
+
if (!rawOrigin) return true;
|
|
97
|
+
try {
|
|
98
|
+
return rawOrigin !== "null" && new URL(rawOrigin).origin === new URL(request.url).origin;
|
|
99
|
+
} catch {
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
function mergeHeaders(response, additional) {
|
|
104
|
+
if (!additional) return response;
|
|
105
|
+
const headers = new Headers(response.headers);
|
|
106
|
+
const extras = new Headers(additional);
|
|
107
|
+
for (const [name, value] of extras) headers.append(name, value);
|
|
108
|
+
return new Response(response.body, { status: response.status, statusText: response.statusText, headers });
|
|
109
|
+
}
|
|
110
|
+
function json(value, headers, status = 200) {
|
|
111
|
+
const responseHeaders = new Headers(headers);
|
|
112
|
+
responseHeaders.set("Cache-Control", "no-store");
|
|
113
|
+
responseHeaders.set("Content-Type", "application/json; charset=utf-8");
|
|
114
|
+
return new Response(JSON.stringify(value), { status, headers: responseHeaders });
|
|
115
|
+
}
|
|
116
|
+
async function requestJson(request) {
|
|
117
|
+
const contentLength = Number(request.headers.get("content-length") || 0);
|
|
118
|
+
if (contentLength > JSON_LIMIT_BYTES) throw new BffInputError(413, "REQUEST_TOO_LARGE", "The request body is too large.");
|
|
119
|
+
const value = await request.json().catch(() => null);
|
|
120
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
121
|
+
throw new BffInputError(400, "INVALID_JSON", "A JSON object is required.");
|
|
122
|
+
}
|
|
123
|
+
return value;
|
|
124
|
+
}
|
|
125
|
+
var BffInputError = class extends Error {
|
|
126
|
+
constructor(status, code, message) {
|
|
127
|
+
super(message);
|
|
128
|
+
this.status = status;
|
|
129
|
+
this.code = code;
|
|
130
|
+
}
|
|
131
|
+
status;
|
|
132
|
+
code;
|
|
133
|
+
};
|
|
134
|
+
function routeId(path, pattern, field) {
|
|
135
|
+
const match = pattern.exec(path);
|
|
136
|
+
if (!match?.[1]) throw new BffInputError(404, "NOT_FOUND", "The requested BFF route does not exist.");
|
|
137
|
+
try {
|
|
138
|
+
return normalized(decodeURIComponent(match[1]), field);
|
|
139
|
+
} catch {
|
|
140
|
+
throw new BffInputError(400, "INVALID_PATH", `${field} is invalid.`);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
function messageBody(body) {
|
|
144
|
+
const mode = body.mode === "steer" ? "steer" : body.mode === "queue" ? "queue" : null;
|
|
145
|
+
if (!mode) throw new BffInputError(400, "INVALID_MODE", "Message mode must be queue or steer.");
|
|
146
|
+
const input = stringValue(body.input) ?? "";
|
|
147
|
+
const attachmentIds = stringList(body.attachmentIds);
|
|
148
|
+
if (!input && attachmentIds.length === 0) throw new BffInputError(400, "INPUT_REQUIRED", "A message or attachment is required.");
|
|
149
|
+
if (input.length > MAX_INPUT_LENGTH) throw new BffInputError(413, "INPUT_TOO_LARGE", "The message is too large.");
|
|
150
|
+
if (attachmentIds.length > MAX_ATTACHMENTS) throw new BffInputError(400, "TOO_MANY_ATTACHMENTS", "Too many attachments.");
|
|
151
|
+
if (mode === "steer" && attachmentIds.length) throw new BffInputError(400, "STEER_TEXT_ONLY", "Steering supports text only.");
|
|
152
|
+
const requestedModelId = stringValue(body.requestedModelId) || void 0;
|
|
153
|
+
return {
|
|
154
|
+
clientSubmissionId: stringValue(body.clientSubmissionId),
|
|
155
|
+
mode,
|
|
156
|
+
input,
|
|
157
|
+
attachmentIds,
|
|
158
|
+
...requestedModelId ? { requestedModelId } : {},
|
|
159
|
+
toolInputs: objectValue(body.toolInputs),
|
|
160
|
+
metadata: objectValue(body.metadata)
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
function messageRevision(value) {
|
|
164
|
+
const revision = Number(value);
|
|
165
|
+
if (!Number.isInteger(revision) || revision < 1) throw new BffInputError(400, "INVALID_REVISION", "Message revision is invalid.");
|
|
166
|
+
return revision;
|
|
167
|
+
}
|
|
168
|
+
function computerFileQuery(url, requirePath) {
|
|
169
|
+
const path = url.searchParams.get("path") || void 0;
|
|
170
|
+
if (requirePath && !path) throw new BffInputError(400, "INVALID_PATH", "path is required.");
|
|
171
|
+
if (path && path.length > 1024) throw new BffInputError(400, "INVALID_PATH", "path is too long.");
|
|
172
|
+
const rawCursor = url.searchParams.get("cursor");
|
|
173
|
+
const rawLimit = url.searchParams.get("limit");
|
|
174
|
+
const cursor = rawCursor === null ? void 0 : Number(rawCursor);
|
|
175
|
+
const limit = rawLimit === null ? void 0 : Number(rawLimit);
|
|
176
|
+
if (cursor !== void 0 && (!Number.isInteger(cursor) || cursor < 0)) {
|
|
177
|
+
throw new BffInputError(400, "INVALID_PAGINATION", "cursor is invalid.");
|
|
178
|
+
}
|
|
179
|
+
if (limit !== void 0 && (!Number.isInteger(limit) || limit < 1 || limit > 200)) {
|
|
180
|
+
throw new BffInputError(400, "INVALID_PAGINATION", "limit is invalid.");
|
|
181
|
+
}
|
|
182
|
+
return {
|
|
183
|
+
...path ? { path } : {},
|
|
184
|
+
...cursor !== void 0 ? { cursor } : {},
|
|
185
|
+
...limit !== void 0 ? { limit } : {}
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// src/bff-delegations.ts
|
|
190
|
+
function delegationRoute(path) {
|
|
191
|
+
return /^\/threads\/([^/]+)\/delegations(?:\/(events|[^/]+)(?:\/(history|actions))?)?$/.exec(path);
|
|
192
|
+
}
|
|
193
|
+
async function dispatchDelegations(request, path, embed, agentId, scope) {
|
|
194
|
+
const match = delegationRoute(path);
|
|
195
|
+
const threadId = decodeURIComponent(match[1]);
|
|
196
|
+
const handle = match[2] ? decodeURIComponent(match[2]) : void 0;
|
|
197
|
+
const query = new URL(request.url).searchParams;
|
|
198
|
+
const paging = (key, max) => {
|
|
199
|
+
const raw = query.get(key), rawLimit = query.get("limit");
|
|
200
|
+
const cursor = raw === null ? void 0 : Number(raw);
|
|
201
|
+
const limit = rawLimit === null ? void 0 : Number(rawLimit);
|
|
202
|
+
if (cursor !== void 0 && (!Number.isInteger(cursor) || cursor < 0) || limit !== void 0 && (!Number.isInteger(limit) || limit < 1 || limit > max)) {
|
|
203
|
+
throw new BffInputError(400, "INVALID_PAGINATION", "Task pagination values are invalid.");
|
|
204
|
+
}
|
|
205
|
+
return { ...scope, [key]: cursor, limit };
|
|
206
|
+
};
|
|
207
|
+
if (request.method === "GET") {
|
|
208
|
+
if (!handle) return json(await embed.listAgentDelegations(agentId, threadId, scope));
|
|
209
|
+
if (handle === "events" && !match[3]) return json(await embed.eventsAgentDelegations(agentId, threadId, paging("after", 200)));
|
|
210
|
+
if (match[3] === "history") return json(await embed.historyAgentDelegation(agentId, threadId, handle, paging("before", 100)));
|
|
211
|
+
if (!match[3]) return json(await embed.inspectAgentDelegation(agentId, threadId, handle, scope));
|
|
212
|
+
}
|
|
213
|
+
if (request.method === "POST" && handle && match[3] === "actions") {
|
|
214
|
+
const body = await requestJson(request);
|
|
215
|
+
if (body.action !== "interrupt" && body.action !== "cancel" || typeof body.operation_id !== "string" || !/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(body.operation_id)) {
|
|
216
|
+
throw new BffInputError(400, "INVALID_TASK_ACTION", "A task action and operation UUID are required.");
|
|
217
|
+
}
|
|
218
|
+
return json(await embed.actAgentDelegation(
|
|
219
|
+
agentId,
|
|
220
|
+
threadId,
|
|
221
|
+
handle,
|
|
222
|
+
{ action: body.action, operation_id: body.operation_id },
|
|
223
|
+
scope
|
|
224
|
+
));
|
|
225
|
+
}
|
|
226
|
+
throw new BffInputError(404, "NOT_FOUND", "The requested task route does not exist.");
|
|
227
|
+
}
|
|
228
|
+
|
|
3
229
|
// src/bff-agent-resolution.ts
|
|
4
230
|
function clean(value) {
|
|
5
231
|
const result = typeof value === "string" ? value.trim() : "";
|
|
@@ -285,191 +511,7 @@ function sseResponse(execute, headers, failurePayload, onFailure, requestSignal,
|
|
|
285
511
|
return new Response(stream, { headers: sseHeaders(headers) });
|
|
286
512
|
}
|
|
287
513
|
|
|
288
|
-
// src/bff-values.ts
|
|
289
|
-
function normalized(value, field) {
|
|
290
|
-
const result = String(value || "").trim();
|
|
291
|
-
if (!result || /[\r\n\0]/.test(result)) throw new Error(`${field} is required.`);
|
|
292
|
-
return result;
|
|
293
|
-
}
|
|
294
|
-
function stringValue(value) {
|
|
295
|
-
const result = typeof value === "string" ? value.trim() : "";
|
|
296
|
-
return result || void 0;
|
|
297
|
-
}
|
|
298
|
-
function stringList(value) {
|
|
299
|
-
return Array.isArray(value) ? value.map(stringValue).filter((item) => Boolean(item)) : [];
|
|
300
|
-
}
|
|
301
|
-
function objectValue(value) {
|
|
302
|
-
return value && typeof value === "object" && !Array.isArray(value) ? value : {};
|
|
303
|
-
}
|
|
304
|
-
function bootstrapDocument(raw, agentVersionId) {
|
|
305
|
-
const features = objectValue(raw.features);
|
|
306
|
-
const selectable = objectValue(features.models).selectable === true;
|
|
307
|
-
const resolvedVersionId = stringValue(raw.agent_version_id) || agentVersionId || "";
|
|
308
|
-
const versionNumber = Number(raw.agent_version_number || 0);
|
|
309
|
-
return {
|
|
310
|
-
version: String(raw.version || "agents24.embed.bootstrap.v1"),
|
|
311
|
-
deployment: {
|
|
312
|
-
id: "server-embed",
|
|
313
|
-
name: normalized(String(raw.agent_name || ""), "agent_name"),
|
|
314
|
-
version_policy: agentVersionId ? { mode: "pinned", version_id: agentVersionId } : { mode: "latest_published" },
|
|
315
|
-
resolved_agent_version_id: resolvedVersionId,
|
|
316
|
-
resolved_agent_version_number: Number.isFinite(versionNumber) ? versionNumber : 0,
|
|
317
|
-
policy_epoch: 0
|
|
318
|
-
},
|
|
319
|
-
authentication: { modes: ["server_embed"], access_token_lifetime_seconds: 0, jwks_path: "" },
|
|
320
|
-
capabilities: ["attachments.read", "attachments.write", "feedback.write", ...selectable ? ["models.select"] : []],
|
|
321
|
-
features,
|
|
322
|
-
native_profiles: []
|
|
323
|
-
};
|
|
324
|
-
}
|
|
325
|
-
|
|
326
514
|
// src/bff.ts
|
|
327
|
-
var JSON_LIMIT_BYTES = 1048576;
|
|
328
|
-
var MAX_INPUT_LENGTH = 2e5;
|
|
329
|
-
var MAX_ATTACHMENTS = 10;
|
|
330
|
-
var MAX_ATTACHMENT_BYTES = 25 * 1024 * 1024;
|
|
331
|
-
var HITL_ACTIONS = /* @__PURE__ */ new Set(["approve", "reject", "connect", "skip", "respond"]);
|
|
332
|
-
var CUSTOMER_IDENTITY_CACHE_TTL_MS = 6e4;
|
|
333
|
-
var CUSTOMER_IDENTITY_CACHE_MAX_ENTRIES = 1024;
|
|
334
|
-
function createCustomerIdentityResolver(embed, integrationId, integrationName) {
|
|
335
|
-
const cache = /* @__PURE__ */ new Map();
|
|
336
|
-
return async (principal) => {
|
|
337
|
-
const profile = {
|
|
338
|
-
display_name: principal.displayName,
|
|
339
|
-
email: principal.email,
|
|
340
|
-
avatar_url: principal.avatarUrl
|
|
341
|
-
};
|
|
342
|
-
const profileFields = Object.fromEntries(Object.entries(profile).filter(([, value2]) => value2 !== void 0));
|
|
343
|
-
const key = JSON.stringify([integrationId, principal.issuer, principal.subject, profileFields]);
|
|
344
|
-
const now = Date.now();
|
|
345
|
-
const cached = cache.get(key);
|
|
346
|
-
if (cached && cached.expiresAt > now) {
|
|
347
|
-
cache.delete(key);
|
|
348
|
-
cache.set(key, cached);
|
|
349
|
-
return cached.value;
|
|
350
|
-
}
|
|
351
|
-
if (cached) cache.delete(key);
|
|
352
|
-
const value = embed.resolveCustomerPrincipal({
|
|
353
|
-
integration_id: integrationId,
|
|
354
|
-
...integrationName ? { integration_name: integrationName } : {},
|
|
355
|
-
...principal.issuer ? { issuer: principal.issuer } : {},
|
|
356
|
-
subject: principal.subject,
|
|
357
|
-
...Object.keys(profileFields).length ? { profile: profileFields } : {}
|
|
358
|
-
}).then((result) => normalized(result.customer_principal_ref, "customer_principal_ref"));
|
|
359
|
-
const entry = { expiresAt: now + CUSTOMER_IDENTITY_CACHE_TTL_MS, value };
|
|
360
|
-
cache.set(key, entry);
|
|
361
|
-
while (cache.size > CUSTOMER_IDENTITY_CACHE_MAX_ENTRIES) {
|
|
362
|
-
const oldest = cache.keys().next().value;
|
|
363
|
-
if (oldest === void 0) break;
|
|
364
|
-
cache.delete(oldest);
|
|
365
|
-
}
|
|
366
|
-
void value.catch(() => {
|
|
367
|
-
if (cache.get(key) === entry) cache.delete(key);
|
|
368
|
-
});
|
|
369
|
-
return value;
|
|
370
|
-
};
|
|
371
|
-
}
|
|
372
|
-
function normalizeBasePath(value) {
|
|
373
|
-
const path = String(value || "/api/agents24").trim().replace(/\/+$/, "");
|
|
374
|
-
if (!path.startsWith("/") || /[\r\n\0?#]/.test(path)) throw new Error("basePath must be an absolute path.");
|
|
375
|
-
return path;
|
|
376
|
-
}
|
|
377
|
-
function browserRequestIsSameOrigin(request) {
|
|
378
|
-
const fetchSite = String(request.headers.get("sec-fetch-site") || "").trim().toLowerCase();
|
|
379
|
-
if (fetchSite) return fetchSite === "same-origin";
|
|
380
|
-
const rawOrigin = String(request.headers.get("origin") || "").trim();
|
|
381
|
-
if (!rawOrigin) return true;
|
|
382
|
-
try {
|
|
383
|
-
return rawOrigin !== "null" && new URL(rawOrigin).origin === new URL(request.url).origin;
|
|
384
|
-
} catch {
|
|
385
|
-
return false;
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
function mergeHeaders(response, additional) {
|
|
389
|
-
if (!additional) return response;
|
|
390
|
-
const headers = new Headers(response.headers);
|
|
391
|
-
const extras = new Headers(additional);
|
|
392
|
-
for (const [name, value] of extras) headers.append(name, value);
|
|
393
|
-
return new Response(response.body, { status: response.status, statusText: response.statusText, headers });
|
|
394
|
-
}
|
|
395
|
-
function json(value, headers, status = 200) {
|
|
396
|
-
const responseHeaders = new Headers(headers);
|
|
397
|
-
responseHeaders.set("Cache-Control", "no-store");
|
|
398
|
-
responseHeaders.set("Content-Type", "application/json; charset=utf-8");
|
|
399
|
-
return new Response(JSON.stringify(value), { status, headers: responseHeaders });
|
|
400
|
-
}
|
|
401
|
-
async function requestJson(request) {
|
|
402
|
-
const contentLength = Number(request.headers.get("content-length") || 0);
|
|
403
|
-
if (contentLength > JSON_LIMIT_BYTES) throw new BffInputError(413, "REQUEST_TOO_LARGE", "The request body is too large.");
|
|
404
|
-
const value = await request.json().catch(() => null);
|
|
405
|
-
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
406
|
-
throw new BffInputError(400, "INVALID_JSON", "A JSON object is required.");
|
|
407
|
-
}
|
|
408
|
-
return value;
|
|
409
|
-
}
|
|
410
|
-
var BffInputError = class extends Error {
|
|
411
|
-
constructor(status, code, message) {
|
|
412
|
-
super(message);
|
|
413
|
-
this.status = status;
|
|
414
|
-
this.code = code;
|
|
415
|
-
}
|
|
416
|
-
status;
|
|
417
|
-
code;
|
|
418
|
-
};
|
|
419
|
-
function routeId(path, pattern, field) {
|
|
420
|
-
const match = pattern.exec(path);
|
|
421
|
-
if (!match?.[1]) throw new BffInputError(404, "NOT_FOUND", "The requested BFF route does not exist.");
|
|
422
|
-
try {
|
|
423
|
-
return normalized(decodeURIComponent(match[1]), field);
|
|
424
|
-
} catch {
|
|
425
|
-
throw new BffInputError(400, "INVALID_PATH", `${field} is invalid.`);
|
|
426
|
-
}
|
|
427
|
-
}
|
|
428
|
-
function messageBody(body) {
|
|
429
|
-
const mode = body.mode === "steer" ? "steer" : body.mode === "queue" ? "queue" : null;
|
|
430
|
-
if (!mode) throw new BffInputError(400, "INVALID_MODE", "Message mode must be queue or steer.");
|
|
431
|
-
const input = stringValue(body.input) ?? "";
|
|
432
|
-
const attachmentIds = stringList(body.attachmentIds);
|
|
433
|
-
if (!input && attachmentIds.length === 0) throw new BffInputError(400, "INPUT_REQUIRED", "A message or attachment is required.");
|
|
434
|
-
if (input.length > MAX_INPUT_LENGTH) throw new BffInputError(413, "INPUT_TOO_LARGE", "The message is too large.");
|
|
435
|
-
if (attachmentIds.length > MAX_ATTACHMENTS) throw new BffInputError(400, "TOO_MANY_ATTACHMENTS", "Too many attachments.");
|
|
436
|
-
if (mode === "steer" && attachmentIds.length) throw new BffInputError(400, "STEER_TEXT_ONLY", "Steering supports text only.");
|
|
437
|
-
const requestedModelId = stringValue(body.requestedModelId) || void 0;
|
|
438
|
-
return {
|
|
439
|
-
clientSubmissionId: stringValue(body.clientSubmissionId),
|
|
440
|
-
mode,
|
|
441
|
-
input,
|
|
442
|
-
attachmentIds,
|
|
443
|
-
...requestedModelId ? { requestedModelId } : {},
|
|
444
|
-
toolInputs: objectValue(body.toolInputs),
|
|
445
|
-
metadata: objectValue(body.metadata)
|
|
446
|
-
};
|
|
447
|
-
}
|
|
448
|
-
function messageRevision(value) {
|
|
449
|
-
const revision = Number(value);
|
|
450
|
-
if (!Number.isInteger(revision) || revision < 1) throw new BffInputError(400, "INVALID_REVISION", "Message revision is invalid.");
|
|
451
|
-
return revision;
|
|
452
|
-
}
|
|
453
|
-
function computerFileQuery(url, requirePath) {
|
|
454
|
-
const path = url.searchParams.get("path") || void 0;
|
|
455
|
-
if (requirePath && !path) throw new BffInputError(400, "INVALID_PATH", "path is required.");
|
|
456
|
-
if (path && path.length > 1024) throw new BffInputError(400, "INVALID_PATH", "path is too long.");
|
|
457
|
-
const rawCursor = url.searchParams.get("cursor");
|
|
458
|
-
const rawLimit = url.searchParams.get("limit");
|
|
459
|
-
const cursor = rawCursor === null ? void 0 : Number(rawCursor);
|
|
460
|
-
const limit = rawLimit === null ? void 0 : Number(rawLimit);
|
|
461
|
-
if (cursor !== void 0 && (!Number.isInteger(cursor) || cursor < 0)) {
|
|
462
|
-
throw new BffInputError(400, "INVALID_PAGINATION", "cursor is invalid.");
|
|
463
|
-
}
|
|
464
|
-
if (limit !== void 0 && (!Number.isInteger(limit) || limit < 1 || limit > 200)) {
|
|
465
|
-
throw new BffInputError(400, "INVALID_PAGINATION", "limit is invalid.");
|
|
466
|
-
}
|
|
467
|
-
return {
|
|
468
|
-
...path ? { path } : {},
|
|
469
|
-
...cursor !== void 0 ? { cursor } : {},
|
|
470
|
-
...limit !== void 0 ? { limit } : {}
|
|
471
|
-
};
|
|
472
|
-
}
|
|
473
515
|
function createAgents24BffHandlerWithResolver(options, resolveCustomerIdentity) {
|
|
474
516
|
const agentId = normalized(options.agentId, "agentId");
|
|
475
517
|
const agentVersionId = stringValue(options.agentVersionId);
|
|
@@ -478,6 +520,14 @@ function createAgents24BffHandlerWithResolver(options, resolveCustomerIdentity)
|
|
|
478
520
|
if (runtimeTarget === "draft" && agentVersionId) throw new Error("agentVersionId cannot be used with draft runtimeTarget.");
|
|
479
521
|
const basePath = normalizeBasePath(options.basePath);
|
|
480
522
|
const embed = options.client.embed;
|
|
523
|
+
const validateSelection = async (body) => {
|
|
524
|
+
const selector = stringValue(body.requestedAgentId);
|
|
525
|
+
if (!selector || selector === agentId) return;
|
|
526
|
+
const bootstrap = await embed.getRuntimeBootstrap(agentId, agentVersionId, runtimeTarget);
|
|
527
|
+
if (selector !== bootstrap.agent_alias && !stringList(bootstrap.agent_aliases).includes(selector)) {
|
|
528
|
+
throw new BffInputError(404, "AGENT_NOT_CONFIGURED", "The requested Agent is not available.");
|
|
529
|
+
}
|
|
530
|
+
};
|
|
481
531
|
const streamFailure = (request, operation) => (error) => {
|
|
482
532
|
reportFailureResponse(options.onDiagnostic, request, operation, errorResponse(error));
|
|
483
533
|
};
|
|
@@ -510,9 +560,17 @@ function createAgents24BffHandlerWithResolver(options, resolveCustomerIdentity)
|
|
|
510
560
|
};
|
|
511
561
|
const threadIdentity = {
|
|
512
562
|
externalUserId: customerPrincipalRef,
|
|
563
|
+
...resolution.chatScope === void 0 ? {} : { chatScope: resolution.chatScope },
|
|
513
564
|
runtimeTarget,
|
|
514
565
|
...principal.externalSessionId ? { externalSessionId: principal.externalSessionId } : {}
|
|
515
566
|
};
|
|
567
|
+
if (delegationRoute(path)) return mergeHeaders(await dispatchDelegations(
|
|
568
|
+
request,
|
|
569
|
+
path,
|
|
570
|
+
embed,
|
|
571
|
+
agentId,
|
|
572
|
+
{ ...threadIdentity, integrationId }
|
|
573
|
+
), resolution.responseHeaders);
|
|
516
574
|
let response;
|
|
517
575
|
if (request.method === "GET" && path === "/bootstrap") {
|
|
518
576
|
const raw = await embed.getRuntimeBootstrap(agentId, agentVersionId, runtimeTarget);
|
|
@@ -526,6 +584,7 @@ function createAgents24BffHandlerWithResolver(options, resolveCustomerIdentity)
|
|
|
526
584
|
}));
|
|
527
585
|
} else if (request.method === "POST" && path === "/chat/stream") {
|
|
528
586
|
const body = await requestJson(request);
|
|
587
|
+
await validateSelection(body);
|
|
529
588
|
const input = stringValue(body.input) ?? "";
|
|
530
589
|
const threadId = stringValue(body.threadId);
|
|
531
590
|
const attachmentIds = stringList(body.attachmentIds);
|
|
@@ -540,23 +599,28 @@ function createAgents24BffHandlerWithResolver(options, resolveCustomerIdentity)
|
|
|
540
599
|
externalUserId: customerPrincipalRef,
|
|
541
600
|
...principal.externalSessionId ? { externalSessionId: principal.externalSessionId } : {},
|
|
542
601
|
integrationId,
|
|
602
|
+
...resolution.chatScope === void 0 ? {} : { chatScope: resolution.chatScope },
|
|
543
603
|
runtimeTarget,
|
|
544
604
|
clientSubmissionId: stringValue(body.clientSubmissionId),
|
|
605
|
+
agentVersionId: agentVersionId ?? null,
|
|
545
606
|
mode: "queue",
|
|
546
607
|
input,
|
|
547
608
|
attachmentIds,
|
|
548
609
|
...stringValue(body.requestedModelId) ? { requestedModelId: stringValue(body.requestedModelId) } : {},
|
|
610
|
+
...resolution.knowledgeNamespaces === void 0 ? {} : { knowledgeNamespaces: resolution.knowledgeNamespaces },
|
|
549
611
|
toolInputs: objectValue(body.toolInputs),
|
|
550
612
|
metadata: objectValue(body.metadata)
|
|
551
613
|
}, { idempotencyKey }) : await embed.startAgentRun(agentId, {
|
|
552
614
|
...identity,
|
|
553
615
|
integration_id: integrationId,
|
|
616
|
+
...resolution.chatScope === void 0 ? {} : { chat_scope: resolution.chatScope },
|
|
554
617
|
...agentVersionId ? { agent_version_id: agentVersionId } : {},
|
|
555
618
|
runtime_target: runtimeTarget,
|
|
556
619
|
...input ? { input } : {},
|
|
557
620
|
client_submission_id: stringValue(body.clientSubmissionId),
|
|
558
621
|
attachment_ids: attachmentIds,
|
|
559
622
|
...stringValue(body.requestedModelId) ? { requested_model_id: stringValue(body.requestedModelId) } : {},
|
|
623
|
+
...resolution.knowledgeNamespaces === void 0 ? {} : { knowledge_namespaces: resolution.knowledgeNamespaces },
|
|
560
624
|
tool_inputs: objectValue(body.toolInputs),
|
|
561
625
|
metadata: objectValue(body.metadata),
|
|
562
626
|
client: objectValue(body.client)
|
|
@@ -569,6 +633,7 @@ function createAgents24BffHandlerWithResolver(options, resolveCustomerIdentity)
|
|
|
569
633
|
await embed.attachAgentRun(agentId, admittedRunId, {
|
|
570
634
|
...identity,
|
|
571
635
|
integration_id: integrationId,
|
|
636
|
+
...resolution.chatScope === void 0 ? {} : { chat_scope: resolution.chatScope },
|
|
572
637
|
runtime_target: runtimeTarget,
|
|
573
638
|
metadata: objectValue(body.metadata),
|
|
574
639
|
client: objectValue(body.client),
|
|
@@ -584,6 +649,7 @@ function createAgents24BffHandlerWithResolver(options, resolveCustomerIdentity)
|
|
|
584
649
|
} else if (request.method === "POST" && /^\/runs\/[^/]+\/attach$/.test(path)) {
|
|
585
650
|
const runId = routeId(path, /^\/runs\/([^/]+)\/attach$/, "runId");
|
|
586
651
|
const body = await requestJson(request);
|
|
652
|
+
await validateSelection(body);
|
|
587
653
|
const cursor = body.cursor;
|
|
588
654
|
if (cursor !== void 0 && (!Number.isInteger(cursor) || Number(cursor) < 0)) {
|
|
589
655
|
throw new BffInputError(400, "INVALID_CURSOR", "cursor must be a non-negative integer.");
|
|
@@ -593,6 +659,7 @@ function createAgents24BffHandlerWithResolver(options, resolveCustomerIdentity)
|
|
|
593
659
|
...identity,
|
|
594
660
|
runtime_target: runtimeTarget,
|
|
595
661
|
integration_id: integrationId,
|
|
662
|
+
...resolution.chatScope === void 0 ? {} : { chat_scope: resolution.chatScope },
|
|
596
663
|
...cursor === void 0 ? {} : { cursor: Number(cursor) }
|
|
597
664
|
}, (event) => {
|
|
598
665
|
if (event.event !== "assistant.completed" || new URL(request.url).searchParams.get("stream_features")?.split(",").includes("assistant-completion-v1")) return write(event);
|
|
@@ -608,11 +675,13 @@ function createAgents24BffHandlerWithResolver(options, resolveCustomerIdentity)
|
|
|
608
675
|
const runId = routeId(path, /^\/runs\/([^/]+)\/cancel$/, "runId");
|
|
609
676
|
response = json(await embed.cancelAgentRun(agentId, runId, {
|
|
610
677
|
...threadIdentity,
|
|
611
|
-
integrationId
|
|
678
|
+
integrationId,
|
|
679
|
+
...resolution.chatScope === void 0 ? {} : { chatScope: resolution.chatScope }
|
|
612
680
|
}, { idempotencyKey }));
|
|
613
681
|
} else if (request.method === "PUT" && /^\/runs\/[^/]+\/feedback$/.test(path)) {
|
|
614
682
|
const runId = routeId(path, /^\/runs\/([^/]+)\/feedback$/, "runId");
|
|
615
683
|
const body = await requestJson(request);
|
|
684
|
+
await validateSelection(body);
|
|
616
685
|
const rating = body.rating === null ? null : stringValue(body.rating);
|
|
617
686
|
const reasons = /* @__PURE__ */ new Set(["incorrect", "irrelevant", "incomplete", "unsafe", "citation_issue", "other"]);
|
|
618
687
|
const reason = stringValue(body.reason);
|
|
@@ -630,6 +699,7 @@ function createAgents24BffHandlerWithResolver(options, resolveCustomerIdentity)
|
|
|
630
699
|
...identity,
|
|
631
700
|
runtime_target: runtimeTarget,
|
|
632
701
|
integration_id: integrationId,
|
|
702
|
+
...resolution.chatScope === void 0 ? {} : { chat_scope: resolution.chatScope },
|
|
633
703
|
rating,
|
|
634
704
|
...rating === "dislike" && reason ? { reason } : {},
|
|
635
705
|
...rating === "dislike" && comment ? { comment } : {}
|
|
@@ -643,6 +713,7 @@ function createAgents24BffHandlerWithResolver(options, resolveCustomerIdentity)
|
|
|
643
713
|
response = json(await embed.listAgentThreads(agentId, {
|
|
644
714
|
...threadIdentity,
|
|
645
715
|
integrationId,
|
|
716
|
+
...resolution.chatScope === void 0 ? {} : { chatScope: resolution.chatScope },
|
|
646
717
|
runtimeTarget,
|
|
647
718
|
skip,
|
|
648
719
|
limit
|
|
@@ -657,6 +728,7 @@ function createAgents24BffHandlerWithResolver(options, resolveCustomerIdentity)
|
|
|
657
728
|
await embed.subscribeAgentThreadEvents(agentId, {
|
|
658
729
|
...threadIdentity,
|
|
659
730
|
integrationId,
|
|
731
|
+
...resolution.chatScope === void 0 ? {} : { chatScope: resolution.chatScope },
|
|
660
732
|
...cursor !== void 0 ? { cursor } : {},
|
|
661
733
|
signal,
|
|
662
734
|
onControl: control,
|
|
@@ -678,16 +750,18 @@ function createAgents24BffHandlerWithResolver(options, resolveCustomerIdentity)
|
|
|
678
750
|
} else if (/^\/threads\/[^/]+\/messages$/.test(path) && request.method === "POST") {
|
|
679
751
|
const threadId = routeId(path, /^\/threads\/([^/]+)\/messages$/, "threadId");
|
|
680
752
|
const body = await requestJson(request);
|
|
753
|
+
await validateSelection(body);
|
|
681
754
|
response = json(await embed.submitAgentThreadMessage(
|
|
682
755
|
agentId,
|
|
683
756
|
threadId,
|
|
684
|
-
{ ...threadIdentity, integrationId, ...messageBody(body) },
|
|
757
|
+
{ ...threadIdentity, integrationId, ...messageBody(body), ...resolution.knowledgeNamespaces === void 0 ? {} : { knowledgeNamespaces: resolution.knowledgeNamespaces }, agentVersionId: agentVersionId ?? null },
|
|
685
758
|
{ idempotencyKey }
|
|
686
759
|
));
|
|
687
760
|
} else if (/^\/threads\/[^/]+\/messages\/[^/]+$/.test(path) && request.method === "PATCH") {
|
|
688
761
|
const threadId = routeId(path, /^\/threads\/([^/]+)\/messages\//, "threadId");
|
|
689
762
|
const messageId = routeId(path, /\/messages\/([^/]+)$/, "messageId");
|
|
690
763
|
const body = await requestJson(request);
|
|
764
|
+
await validateSelection(body);
|
|
691
765
|
const input = stringValue(body.input) ?? "";
|
|
692
766
|
const attachmentIds = stringList(body.attachmentIds);
|
|
693
767
|
if (!input && !attachmentIds.length) throw new BffInputError(400, "INPUT_REQUIRED", "A message or attachment is required.");
|
|
@@ -702,6 +776,7 @@ function createAgents24BffHandlerWithResolver(options, resolveCustomerIdentity)
|
|
|
702
776
|
{
|
|
703
777
|
...threadIdentity,
|
|
704
778
|
integrationId,
|
|
779
|
+
agentVersionId: agentVersionId ?? null,
|
|
705
780
|
revision: messageRevision(body.revision),
|
|
706
781
|
input,
|
|
707
782
|
attachmentIds,
|
|
@@ -723,7 +798,7 @@ function createAgents24BffHandlerWithResolver(options, resolveCustomerIdentity)
|
|
|
723
798
|
response = json(await embed.resumeAgentThreadQueue(agentId, threadId, { ...threadIdentity, integrationId }));
|
|
724
799
|
} else if (/^\/threads\/[^/]+\/computer(?:\/files|\/file|\/content-access)?$/.test(path) && (request.method === "GET" || request.method === "POST" && path.endsWith("/content-access"))) {
|
|
725
800
|
const threadId = routeId(path, /^\/threads\/([^/]+)\/computer/, "threadId");
|
|
726
|
-
const scope = { ...threadIdentity, integrationId, runtimeTarget };
|
|
801
|
+
const scope = { ...threadIdentity, integrationId, ...resolution.chatScope === void 0 ? {} : { chatScope: resolution.chatScope }, runtimeTarget };
|
|
727
802
|
if (path.endsWith("/content-access")) {
|
|
728
803
|
response = json(await embed.createAgentComputerContentAccess(agentId, threadId, {
|
|
729
804
|
...scope,
|
|
@@ -758,6 +833,7 @@ function createAgents24BffHandlerWithResolver(options, resolveCustomerIdentity)
|
|
|
758
833
|
response = json(await embed.getAgentThread(agentId, threadId, {
|
|
759
834
|
...threadIdentity,
|
|
760
835
|
integrationId,
|
|
836
|
+
...resolution.chatScope === void 0 ? {} : { chatScope: resolution.chatScope },
|
|
761
837
|
...before !== void 0 ? { beforeTurnIndex: before } : {},
|
|
762
838
|
...limit !== void 0 ? { limit } : {}
|
|
763
839
|
}));
|
|
@@ -779,11 +855,13 @@ function createAgents24BffHandlerWithResolver(options, resolveCustomerIdentity)
|
|
|
779
855
|
files,
|
|
780
856
|
...stringValue(form.get("thread_id")) ? { threadId: stringValue(form.get("thread_id")) } : {},
|
|
781
857
|
integrationId,
|
|
858
|
+
...resolution.chatScope === void 0 ? {} : { chatScope: resolution.chatScope },
|
|
782
859
|
idempotencyKey
|
|
783
860
|
}));
|
|
784
861
|
} else if (request.method === "POST" && /^\/attachments\/[^/]+\/content-access$/.test(path)) {
|
|
785
862
|
const attachmentId = routeId(path, /^\/attachments\/([^/]+)\/content-access$/, "attachmentId");
|
|
786
863
|
const body = await requestJson(request);
|
|
864
|
+
await validateSelection(body);
|
|
787
865
|
const disposition = stringValue(body.disposition);
|
|
788
866
|
if (disposition !== "inline" && disposition !== "attachment") {
|
|
789
867
|
throw new BffInputError(400, "INVALID_DISPOSITION", "Attachment disposition is invalid.");
|
|
@@ -793,6 +871,7 @@ function createAgents24BffHandlerWithResolver(options, resolveCustomerIdentity)
|
|
|
793
871
|
...threadIdentity.externalSessionId ? { external_session_id: threadIdentity.externalSessionId } : {},
|
|
794
872
|
runtime_target: runtimeTarget,
|
|
795
873
|
integration_id: integrationId,
|
|
874
|
+
...resolution.chatScope === void 0 ? {} : { chat_scope: resolution.chatScope },
|
|
796
875
|
disposition
|
|
797
876
|
}));
|
|
798
877
|
} else if (request.method === "POST" && /^\/runs\/[^/]+\/hitl\/[^/]+\/resume$/.test(path)) {
|
|
@@ -800,6 +879,7 @@ function createAgents24BffHandlerWithResolver(options, resolveCustomerIdentity)
|
|
|
800
879
|
const runId = routeId(path, /^\/runs\/([^/]+)\/hitl\//, "runId");
|
|
801
880
|
const interruptId = match?.[2] ? decodeURIComponent(match[2]) : "";
|
|
802
881
|
const body = await requestJson(request);
|
|
882
|
+
await validateSelection(body);
|
|
803
883
|
const action = stringValue(body.action);
|
|
804
884
|
if (!action || !HITL_ACTIONS.has(action)) {
|
|
805
885
|
throw new BffInputError(400, "INVALID_HITL_ACTION", "The HITL action is invalid.");
|
|
@@ -808,6 +888,7 @@ function createAgents24BffHandlerWithResolver(options, resolveCustomerIdentity)
|
|
|
808
888
|
...identity,
|
|
809
889
|
runtime_target: runtimeTarget,
|
|
810
890
|
integration_id: integrationId,
|
|
891
|
+
...resolution.chatScope === void 0 ? {} : { chat_scope: resolution.chatScope },
|
|
811
892
|
schema_version: "agents24.hitl.resume.v2",
|
|
812
893
|
interrupt_id: normalized(interruptId, "interruptId"),
|
|
813
894
|
action,
|
|
@@ -819,11 +900,13 @@ function createAgents24BffHandlerWithResolver(options, resolveCustomerIdentity)
|
|
|
819
900
|
const runId = match?.[1] ? decodeURIComponent(match[1]) : "";
|
|
820
901
|
const serverId = match?.[2] ? decodeURIComponent(match[2]) : "";
|
|
821
902
|
const body = await requestJson(request);
|
|
903
|
+
await validateSelection(body);
|
|
822
904
|
const callbackOrigin = new URL(request.url).origin;
|
|
823
905
|
response = json(await embed.startAgentRunMcpAuth(agentId, normalized(runId, "runId"), normalized(serverId, "serverId"), {
|
|
824
906
|
...identity,
|
|
825
907
|
runtime_target: runtimeTarget,
|
|
826
908
|
integration_id: integrationId,
|
|
909
|
+
...resolution.chatScope === void 0 ? {} : { chat_scope: resolution.chatScope },
|
|
827
910
|
interrupt_id: normalized(String(body.interruptId || ""), "interruptId"),
|
|
828
911
|
callback_origin: callbackOrigin,
|
|
829
912
|
popup_nonce: normalized(String(body.popupNonce || ""), "popupNonce")
|
|
@@ -907,18 +990,26 @@ function createResolvedAgents24BffHandler(options) {
|
|
|
907
990
|
external_user_id: externalUserId,
|
|
908
991
|
...resolution.principal.externalSessionId ? { external_session_id: resolution.principal.externalSessionId } : {}
|
|
909
992
|
};
|
|
910
|
-
const requestedAgent = (value) => {
|
|
993
|
+
const requestedAgent = async (value) => {
|
|
911
994
|
const agentId = stringValue(value) || agents[0].agentId;
|
|
912
|
-
|
|
995
|
+
let configured = agentBySelector.get(agentId);
|
|
996
|
+
if (!configured && stringValue(value)) {
|
|
997
|
+
try {
|
|
998
|
+
const resolved = await options.client.embed.resolveAgentAlias(agentId);
|
|
999
|
+
configured = agentBySelector.get(resolved.agent_id);
|
|
1000
|
+
} catch (error) {
|
|
1001
|
+
if (error.status !== 404) throw error;
|
|
1002
|
+
}
|
|
1003
|
+
}
|
|
913
1004
|
if (!configured) throw new BffInputError(404, "AGENT_NOT_CONFIGURED", "The requested Agent is not available.");
|
|
914
1005
|
return configured;
|
|
915
1006
|
};
|
|
916
|
-
const dispatchToAgent = async (agentId) => {
|
|
1007
|
+
const dispatchToAgent = async (agentId, message) => {
|
|
917
1008
|
const handler = scopedHandlers.get(agentId);
|
|
918
1009
|
if (!handler) throw new BffInputError(404, "NOT_FOUND", "The requested resource does not exist.");
|
|
919
1010
|
const target = new URL(request.url);
|
|
920
1011
|
target.pathname = `${basePath}/agents/${encodeURIComponent(agentId)}${path}`;
|
|
921
|
-
const body = request.method === "GET" || request.method === "HEAD" ? void 0 : await request.arrayBuffer();
|
|
1012
|
+
const body = request.method === "GET" || request.method === "HEAD" ? void 0 : message ? JSON.stringify({ ...message, requestedAgentId: agentId }) : await request.arrayBuffer();
|
|
922
1013
|
return handler(new Request(target, {
|
|
923
1014
|
method: request.method,
|
|
924
1015
|
headers: request.headers,
|
|
@@ -931,9 +1022,10 @@ function createResolvedAgents24BffHandler(options) {
|
|
|
931
1022
|
agent_ids: agents.map((agent) => agent.agentId),
|
|
932
1023
|
...identity,
|
|
933
1024
|
integration_id: integrationId,
|
|
1025
|
+
...resolution.chatScope === void 0 ? {} : { chat_scope: resolution.chatScope },
|
|
934
1026
|
runtime_target: runtimeTarget
|
|
935
1027
|
});
|
|
936
|
-
return requestedAgent(result.agent_id).agentId;
|
|
1028
|
+
return (await requestedAgent(result.agent_id)).agentId;
|
|
937
1029
|
};
|
|
938
1030
|
const resolveThreadAgent = async (threadId) => {
|
|
939
1031
|
const detail = await options.client.embed.getConversationThread(threadId, {
|
|
@@ -941,9 +1033,10 @@ function createResolvedAgents24BffHandler(options) {
|
|
|
941
1033
|
externalUserId,
|
|
942
1034
|
...resolution.principal.externalSessionId ? { externalSessionId: resolution.principal.externalSessionId } : {},
|
|
943
1035
|
integrationId,
|
|
1036
|
+
...resolution.chatScope === void 0 ? {} : { chatScope: resolution.chatScope },
|
|
944
1037
|
runtimeTarget
|
|
945
1038
|
});
|
|
946
|
-
return requestedAgent(detail.runtime_selection?.agent_id).agentId;
|
|
1039
|
+
return (await requestedAgent(detail.runtime_selection?.agent_id)).agentId;
|
|
947
1040
|
};
|
|
948
1041
|
if (request.method === "GET" && path === "/bootstrap") {
|
|
949
1042
|
const candidates = await Promise.all(agents.map(async (agent) => {
|
|
@@ -962,7 +1055,7 @@ function createResolvedAgents24BffHandler(options) {
|
|
|
962
1055
|
for (const item of stringList(raw.agent_aliases)) agentBySelector.set(item, agent);
|
|
963
1056
|
return { agent, raw, policy };
|
|
964
1057
|
}));
|
|
965
|
-
const selected = requestedAgent(url.searchParams.get("agent_id"));
|
|
1058
|
+
const selected = await requestedAgent(url.searchParams.get("agent_id"));
|
|
966
1059
|
const allowed = candidates.filter((candidate) => candidate.policy.agent_access_allowed !== false);
|
|
967
1060
|
const selectedCandidate = allowed.find((candidate) => candidate.agent.agentId === selected.agentId) || allowed[0];
|
|
968
1061
|
if (!selectedCandidate) throw new BffInputError(403, "RESOURCE_POLICY_ACCESS_DENIED", "No configured Agent is available.");
|
|
@@ -989,9 +1082,10 @@ function createResolvedAgents24BffHandler(options) {
|
|
|
989
1082
|
}
|
|
990
1083
|
if (request.method === "POST" && path === "/chat/stream") {
|
|
991
1084
|
const body = await requestJson(request);
|
|
992
|
-
const selected = requestedAgent(body.requestedAgentId);
|
|
993
|
-
const input = stringValue(body.input);
|
|
994
1085
|
const threadId = stringValue(body.threadId);
|
|
1086
|
+
const storedAgent = threadId ? await resolveThreadAgent(threadId) : void 0;
|
|
1087
|
+
const selected = await requestedAgent(body.requestedAgentId || storedAgent);
|
|
1088
|
+
const input = stringValue(body.input);
|
|
995
1089
|
const attachmentIds = stringList(body.attachmentIds);
|
|
996
1090
|
if (!input && attachmentIds.length === 0) throw new BffInputError(400, "INPUT_REQUIRED", "A message or attachment is required.");
|
|
997
1091
|
if (input && input.length > MAX_INPUT_LENGTH) throw new BffInputError(413, "INPUT_TOO_LARGE", "The message is too large.");
|
|
@@ -1000,12 +1094,15 @@ function createResolvedAgents24BffHandler(options) {
|
|
|
1000
1094
|
externalUserId,
|
|
1001
1095
|
...resolution.principal.externalSessionId ? { externalSessionId: resolution.principal.externalSessionId } : {},
|
|
1002
1096
|
integrationId,
|
|
1097
|
+
...resolution.chatScope === void 0 ? {} : { chatScope: resolution.chatScope },
|
|
1003
1098
|
runtimeTarget,
|
|
1004
1099
|
clientSubmissionId: stringValue(body.clientSubmissionId),
|
|
1100
|
+
agentVersionId: selected.agentVersionId ?? null,
|
|
1005
1101
|
mode: "queue",
|
|
1006
1102
|
input: input ?? "",
|
|
1007
1103
|
attachmentIds,
|
|
1008
1104
|
...stringValue(body.requestedModelId) ? { requestedModelId: stringValue(body.requestedModelId) } : {},
|
|
1105
|
+
...resolution.knowledgeNamespaces === void 0 ? {} : { knowledgeNamespaces: resolution.knowledgeNamespaces },
|
|
1009
1106
|
toolInputs: objectValue(body.toolInputs),
|
|
1010
1107
|
metadata: objectValue(body.metadata)
|
|
1011
1108
|
}, { idempotencyKey: stringValue(request.headers.get("idempotency-key")) }) : await options.client.embed.startAgentRun(selected.agentId, {
|
|
@@ -1013,10 +1110,12 @@ function createResolvedAgents24BffHandler(options) {
|
|
|
1013
1110
|
...selected.agentVersionId ? { agent_version_id: selected.agentVersionId } : {},
|
|
1014
1111
|
runtime_target: runtimeTarget,
|
|
1015
1112
|
integration_id: integrationId,
|
|
1113
|
+
...resolution.chatScope === void 0 ? {} : { chat_scope: resolution.chatScope },
|
|
1016
1114
|
...input ? { input } : {},
|
|
1017
1115
|
client_submission_id: stringValue(body.clientSubmissionId),
|
|
1018
1116
|
attachment_ids: attachmentIds,
|
|
1019
1117
|
...stringValue(body.requestedModelId) ? { requested_model_id: stringValue(body.requestedModelId) } : {},
|
|
1118
|
+
...resolution.knowledgeNamespaces === void 0 ? {} : { knowledge_namespaces: resolution.knowledgeNamespaces },
|
|
1020
1119
|
tool_inputs: objectValue(body.toolInputs),
|
|
1021
1120
|
metadata: objectValue(body.metadata),
|
|
1022
1121
|
client: objectValue(body.client)
|
|
@@ -1028,6 +1127,7 @@ function createResolvedAgents24BffHandler(options) {
|
|
|
1028
1127
|
...identity,
|
|
1029
1128
|
runtime_target: runtimeTarget,
|
|
1030
1129
|
integration_id: integrationId,
|
|
1130
|
+
...resolution.chatScope === void 0 ? {} : { chat_scope: resolution.chatScope },
|
|
1031
1131
|
metadata: objectValue(body.metadata),
|
|
1032
1132
|
client: objectValue(body.client),
|
|
1033
1133
|
cursor: 0
|
|
@@ -1051,6 +1151,7 @@ function createResolvedAgents24BffHandler(options) {
|
|
|
1051
1151
|
...resolution.principal.externalSessionId ? { external_session_id: resolution.principal.externalSessionId } : {},
|
|
1052
1152
|
runtime_target: runtimeTarget,
|
|
1053
1153
|
integration_id: integrationId,
|
|
1154
|
+
...resolution.chatScope === void 0 ? {} : { chat_scope: resolution.chatScope },
|
|
1054
1155
|
skip,
|
|
1055
1156
|
limit
|
|
1056
1157
|
})), resolution.responseHeaders);
|
|
@@ -1069,6 +1170,7 @@ function createResolvedAgents24BffHandler(options) {
|
|
|
1069
1170
|
...cursor === void 0 ? {} : { cursor },
|
|
1070
1171
|
runtimeTarget,
|
|
1071
1172
|
integrationId,
|
|
1173
|
+
...resolution.chatScope === void 0 ? {} : { chatScope: resolution.chatScope },
|
|
1072
1174
|
signal,
|
|
1073
1175
|
onControl: control,
|
|
1074
1176
|
leaseSeconds: 60,
|
|
@@ -1081,9 +1183,18 @@ function createResolvedAgents24BffHandler(options) {
|
|
|
1081
1183
|
reason: "stream_failed"
|
|
1082
1184
|
}), (error) => reportFailureResponse(options.onDiagnostic, request, "threads.events", errorResponse(error)), request.signal), resolution.responseHeaders);
|
|
1083
1185
|
}
|
|
1186
|
+
if (delegationRoute(path)) return mergeHeaders(await dispatchDelegations(
|
|
1187
|
+
request,
|
|
1188
|
+
path,
|
|
1189
|
+
options.client.embed,
|
|
1190
|
+
await resolveThreadAgent(decodeURIComponent(delegationRoute(path)[1])),
|
|
1191
|
+
{ externalUserId, externalSessionId: resolution.principal.externalSessionId, integrationId, ...resolution.chatScope === void 0 ? {} : { chatScope: resolution.chatScope }, runtimeTarget }
|
|
1192
|
+
), resolution.responseHeaders);
|
|
1084
1193
|
if (/^\/threads\/[^/]+\/(?:messages(?:\/[^/]+)?|queue\/resume)$/.test(path) && ["GET", "POST", "PATCH", "DELETE"].includes(request.method)) {
|
|
1085
1194
|
const threadId = routeId(path, /^\/threads\/([^/]+)\//, "threadId");
|
|
1086
|
-
|
|
1195
|
+
const storedAgent = await resolveThreadAgent(threadId);
|
|
1196
|
+
const message = request.method === "PATCH" || request.method === "POST" && path.endsWith("/messages") ? await request.clone().json() : void 0;
|
|
1197
|
+
return dispatchToAgent((await requestedAgent(message?.requestedAgentId || storedAgent)).agentId, message);
|
|
1087
1198
|
}
|
|
1088
1199
|
if (/^\/threads\/[^/]+\/computer(?:\/files|\/file|\/content-access)?$/.test(path) && (request.method === "GET" || request.method === "POST" && path.endsWith("/content-access"))) {
|
|
1089
1200
|
const threadId = routeId(path, /^\/threads\/([^/]+)\/computer/, "threadId");
|
|
@@ -1092,6 +1203,7 @@ function createResolvedAgents24BffHandler(options) {
|
|
|
1092
1203
|
externalUserId,
|
|
1093
1204
|
...resolution.principal.externalSessionId ? { externalSessionId: resolution.principal.externalSessionId } : {},
|
|
1094
1205
|
integrationId,
|
|
1206
|
+
...resolution.chatScope === void 0 ? {} : { chatScope: resolution.chatScope },
|
|
1095
1207
|
runtimeTarget
|
|
1096
1208
|
};
|
|
1097
1209
|
if (path.endsWith("/content-access")) {
|
|
@@ -1132,6 +1244,7 @@ function createResolvedAgents24BffHandler(options) {
|
|
|
1132
1244
|
externalUserId,
|
|
1133
1245
|
...resolution.principal.externalSessionId ? { externalSessionId: resolution.principal.externalSessionId } : {},
|
|
1134
1246
|
integrationId,
|
|
1247
|
+
...resolution.chatScope === void 0 ? {} : { chatScope: resolution.chatScope },
|
|
1135
1248
|
runtimeTarget,
|
|
1136
1249
|
beforeTurnIndex,
|
|
1137
1250
|
limit: detailLimit
|
|
@@ -1140,13 +1253,14 @@ function createResolvedAgents24BffHandler(options) {
|
|
|
1140
1253
|
if (/^\/threads\/[^/]+\/runtime-selection$/.test(path) && request.method === "PATCH") {
|
|
1141
1254
|
const threadId = routeId(path, /^\/threads\/([^/]+)\//, "threadId");
|
|
1142
1255
|
const body = await requestJson(request);
|
|
1143
|
-
const selected = requestedAgent(body.preferredAgentId);
|
|
1256
|
+
const selected = await requestedAgent(body.preferredAgentId);
|
|
1144
1257
|
return mergeHeaders(json(await options.client.embed.setConversationRuntimeSelection(threadId, {
|
|
1145
1258
|
agent_ids: agents.map((agent) => agent.agentId),
|
|
1146
1259
|
preferred_agent_id: selected.agentId,
|
|
1147
1260
|
...stringValue(body.preferredModelId) ? { preferred_model_id: stringValue(body.preferredModelId) } : {},
|
|
1148
1261
|
...identity,
|
|
1149
1262
|
integration_id: integrationId,
|
|
1263
|
+
...resolution.chatScope === void 0 ? {} : { chat_scope: resolution.chatScope },
|
|
1150
1264
|
runtime_target: runtimeTarget
|
|
1151
1265
|
})), resolution.responseHeaders);
|
|
1152
1266
|
}
|
|
@@ -1157,12 +1271,13 @@ function createResolvedAgents24BffHandler(options) {
|
|
|
1157
1271
|
externalUserId,
|
|
1158
1272
|
...resolution.principal.externalSessionId ? { externalSessionId: resolution.principal.externalSessionId } : {},
|
|
1159
1273
|
integrationId,
|
|
1274
|
+
...resolution.chatScope === void 0 ? {} : { chatScope: resolution.chatScope },
|
|
1160
1275
|
runtimeTarget
|
|
1161
1276
|
}, { idempotencyKey: stringValue(request.headers.get("idempotency-key")) })), resolution.responseHeaders);
|
|
1162
1277
|
}
|
|
1163
1278
|
if (request.method === "POST" && path === "/attachments/upload") {
|
|
1164
1279
|
const form = await request.clone().formData();
|
|
1165
|
-
const selected = requestedAgent(form.get("requested_agent_id"));
|
|
1280
|
+
const selected = await requestedAgent(form.get("requested_agent_id"));
|
|
1166
1281
|
const policy = await options.client.embed.getResourcePolicy(selected.agentId, {
|
|
1167
1282
|
external_user_id: externalUserId,
|
|
1168
1283
|
integration_id: integrationId,
|