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