@standardagents/builder 0.41.6 → 0.41.8

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.
@@ -79652,7 +79652,7 @@ var config_get_default = defineController2(async ({ config, env: env2, req }) =>
79652
79652
  });
79653
79653
 
79654
79654
  // src/version.ts
79655
- var BUILDER_VERSION = "0.41.6".length > 0 ? "0.41.6" : "dev";
79655
+ var BUILDER_VERSION = "0.41.8".length > 0 ? "0.41.8" : "dev";
79656
79656
 
79657
79657
  // src/api/diagnostics.get.ts
79658
79658
  init_LLMRequest();
@@ -85716,6 +85716,16 @@ var analyze_get_default2 = defineController2(async ({ req, env: env2 }) => {
85716
85716
  });
85717
85717
 
85718
85718
  // src/users/dispatch.ts
85719
+ function userValueStore(env2, userId) {
85720
+ const hub = env2.AGENT_BUILDER_USER_HUB;
85721
+ if (hub) return hub.get(hub.idFromName(userId));
85722
+ const singleton = env2.AGENT_BUILDER;
85723
+ if (!singleton) return null;
85724
+ return singleton.get(singleton.idFromName("singleton"));
85725
+ }
85726
+ function validUserValueKey(key) {
85727
+ return typeof key === "string" && key.length > 0 && key.length <= 512;
85728
+ }
85719
85729
  function userHubStub(env2, userId) {
85720
85730
  const namespace = env2.AGENT_BUILDER_USER_HUB;
85721
85731
  if (!namespace) return null;
@@ -85740,8 +85750,9 @@ async function forwardToUserScope(req, env2, userId, scopePath, channel = "user"
85740
85750
  }
85741
85751
 
85742
85752
  // src/api/users/[id]/[...rest].ts
85743
- var rest_default = defineController2(async ({ req, env: env2, params }) => {
85744
- const authResult = await requireAuth(req, env2);
85753
+ var rest_default = defineController2(async (context) => {
85754
+ const { req, env: env2, params } = context;
85755
+ const authResult = await resolveRequestAuth(context);
85745
85756
  if (authResult instanceof Response) return authResult;
85746
85757
  const isAdmin = authResult.authType === "super_admin" || authResult.user?.role === "admin";
85747
85758
  if (!isAdmin) {
@@ -85828,6 +85839,25 @@ var env_patch_default = defineController2(async ({ req, env: env2, params }) =>
85828
85839
  }
85829
85840
  });
85830
85841
 
85842
+ // src/api/users/[id]/grants/index.get.ts
85843
+ var index_get_default4 = defineController2(async ({ req, env: env2, params }) => {
85844
+ const auth = await requireAdmin(req, env2);
85845
+ if (auth instanceof Response) return auth;
85846
+ const userId = params.id;
85847
+ if (!userId) return Response.json({ error: "User ID is required" }, { status: 400 });
85848
+ const builder = env2.AGENT_BUILDER.get(env2.AGENT_BUILDER.idFromName("singleton"));
85849
+ if (!await builder.getUserById(userId)) {
85850
+ return Response.json({ error: "User not found" }, { status: 404 });
85851
+ }
85852
+ if (!env2.AGENT_BUILDER_USER_HUB) {
85853
+ return Response.json({ error: "The user scope is not available on this runtime" }, { status: 500 });
85854
+ }
85855
+ const hub = env2.AGENT_BUILDER_USER_HUB.get(
85856
+ env2.AGENT_BUILDER_USER_HUB.idFromName(userId)
85857
+ );
85858
+ return Response.json({ grants: await hub.listGrants(userId) });
85859
+ });
85860
+
85831
85861
  // src/api/users/[id]/stream.ts
85832
85862
  var stream_default2 = defineController2(async (context) => {
85833
85863
  const { req, env: env2, params } = context;
@@ -86173,8 +86203,9 @@ var start_get_default = defineController2(async ({ req, env: env2 }) => {
86173
86203
  });
86174
86204
 
86175
86205
  // src/api/users/me/[...rest].ts
86176
- var rest_default2 = defineController2(async ({ req, env: env2, params }) => {
86177
- const authResult = await requireAuth(req, env2);
86206
+ var rest_default2 = defineController2(async (context) => {
86207
+ const { req, env: env2, params } = context;
86208
+ const authResult = await resolveRequestAuth(context);
86178
86209
  if (authResult instanceof Response) return authResult;
86179
86210
  if (authResult.authType === "super_admin") {
86180
86211
  return Response.json(
@@ -86274,7 +86305,7 @@ var hub_default = defineController2(async (context) => {
86274
86305
  if (req.headers.get("upgrade")?.toLowerCase() !== "websocket") {
86275
86306
  return Response.json(
86276
86307
  { error: "This endpoint only supports WebSocket connections" },
86277
- { status: 400 }
86308
+ { status: 426 }
86278
86309
  );
86279
86310
  }
86280
86311
  const namespace = env2.AGENT_BUILDER_USER_HUB;
@@ -86299,9 +86330,10 @@ var hub_default = defineController2(async (context) => {
86299
86330
  });
86300
86331
 
86301
86332
  // src/api/users/me/kv/index.delete.ts
86302
- var index_delete_default = defineController2(async ({ req, env: env2 }) => {
86333
+ var index_delete_default = defineController2(async (context) => {
86334
+ const { req, env: env2 } = context;
86303
86335
  try {
86304
- const authResult = await requireAuth(req, env2);
86336
+ const authResult = await resolveRequestAuth(context);
86305
86337
  if (authResult instanceof Response) {
86306
86338
  return authResult;
86307
86339
  }
@@ -86313,19 +86345,18 @@ var index_delete_default = defineController2(async ({ req, env: env2 }) => {
86313
86345
  }
86314
86346
  const url = new URL(req.url);
86315
86347
  const key = url.searchParams.get("key");
86316
- if (!key) {
86317
- return Response.json({ error: "key query parameter required" }, { status: 400 });
86348
+ if (!validUserValueKey(key)) {
86349
+ return Response.json({ error: "key query parameter required (1 to 512 characters)" }, { status: 400 });
86318
86350
  }
86319
86351
  if (!isUserKvKeyClientWritable(key)) return reservedKvKeyResponse();
86320
- const agentBuilderId = env2.AGENT_BUILDER.idFromName("singleton");
86321
- const agentBuilder = env2.AGENT_BUILDER.get(agentBuilderId);
86322
- if (typeof agentBuilder.setUserValue !== "function") {
86352
+ const store = userValueStore(env2, authResult.user.id);
86353
+ if (!store) {
86323
86354
  return Response.json(
86324
86355
  { error: "User value storage is not supported by this runtime" },
86325
86356
  { status: 500 }
86326
86357
  );
86327
86358
  }
86328
- await agentBuilder.setUserValue(authResult.user.id, key, null);
86359
+ await store.setUserValue(authResult.user.id, key, null);
86329
86360
  return Response.json({ status: "ok", key });
86330
86361
  } catch (error) {
86331
86362
  console.error("Delete user kv error:", error);
@@ -86337,9 +86368,10 @@ var index_delete_default = defineController2(async ({ req, env: env2 }) => {
86337
86368
  });
86338
86369
 
86339
86370
  // src/api/users/me/kv/index.get.ts
86340
- var index_get_default4 = defineController2(async ({ req, env: env2 }) => {
86371
+ var index_get_default5 = defineController2(async (context) => {
86372
+ const { req, env: env2 } = context;
86341
86373
  try {
86342
- const authResult = await requireAuth(req, env2);
86374
+ const authResult = await resolveRequestAuth(context);
86343
86375
  if (authResult instanceof Response) {
86344
86376
  return authResult;
86345
86377
  }
@@ -86350,28 +86382,24 @@ var index_get_default4 = defineController2(async ({ req, env: env2 }) => {
86350
86382
  );
86351
86383
  }
86352
86384
  const url = new URL(req.url);
86353
- const agentBuilderId = env2.AGENT_BUILDER.idFromName("singleton");
86354
- const agentBuilder = env2.AGENT_BUILDER.get(agentBuilderId);
86355
- const key = url.searchParams.get("key");
86356
- if (key) {
86357
- if (typeof agentBuilder.getUserValue !== "function") {
86358
- return Response.json(
86359
- { error: "User value storage is not supported by this runtime" },
86360
- { status: 500 }
86361
- );
86362
- }
86363
- const value = await agentBuilder.getUserValue(authResult.user.id, key);
86364
- return Response.json({ key, value });
86365
- }
86366
- if (typeof agentBuilder.listUserValues !== "function") {
86385
+ const store = userValueStore(env2, authResult.user.id);
86386
+ if (!store) {
86367
86387
  return Response.json(
86368
86388
  { error: "User value storage is not supported by this runtime" },
86369
86389
  { status: 500 }
86370
86390
  );
86371
86391
  }
86392
+ const key = url.searchParams.get("key");
86393
+ if (key !== null) {
86394
+ if (!validUserValueKey(key)) {
86395
+ return Response.json({ error: "key must be 1 to 512 characters" }, { status: 400 });
86396
+ }
86397
+ const value = await store.getUserValue(authResult.user.id, key);
86398
+ return Response.json({ key, value });
86399
+ }
86372
86400
  const limitParam = Number.parseInt(url.searchParams.get("limit") ?? "", 10);
86373
86401
  const offsetParam = Number.parseInt(url.searchParams.get("offset") ?? "", 10);
86374
- const result = await agentBuilder.listUserValues(authResult.user.id, {
86402
+ const result = await store.listUserValues(authResult.user.id, {
86375
86403
  prefix: url.searchParams.get("prefix"),
86376
86404
  limit: Number.isFinite(limitParam) ? limitParam : void 0,
86377
86405
  offset: Number.isFinite(offsetParam) ? offsetParam : void 0
@@ -86387,9 +86415,10 @@ var index_get_default4 = defineController2(async ({ req, env: env2 }) => {
86387
86415
  });
86388
86416
 
86389
86417
  // src/api/users/me/kv/index.post.ts
86390
- var index_post_default7 = defineController2(async ({ req, env: env2 }) => {
86418
+ var index_post_default7 = defineController2(async (context) => {
86419
+ const { req, env: env2 } = context;
86391
86420
  try {
86392
- const authResult = await requireAuth(req, env2);
86421
+ const authResult = await resolveRequestAuth(context);
86393
86422
  if (authResult instanceof Response) {
86394
86423
  return authResult;
86395
86424
  }
@@ -86410,19 +86439,18 @@ var index_post_default7 = defineController2(async ({ req, env: env2 }) => {
86410
86439
  return Response.json({ error: "Invalid JSON body" }, { status: 400 });
86411
86440
  }
86412
86441
  const key = body.key;
86413
- if (typeof key !== "string" || key.length === 0) {
86414
- return Response.json({ error: "key required" }, { status: 400 });
86442
+ if (!validUserValueKey(key)) {
86443
+ return Response.json({ error: "key required (1 to 512 characters)" }, { status: 400 });
86415
86444
  }
86416
86445
  if (!isUserKvKeyClientWritable(key)) return reservedKvKeyResponse();
86417
- const agentBuilderId = env2.AGENT_BUILDER.idFromName("singleton");
86418
- const agentBuilder = env2.AGENT_BUILDER.get(agentBuilderId);
86419
- if (typeof agentBuilder.setUserValue !== "function") {
86446
+ const store = userValueStore(env2, authResult.user.id);
86447
+ if (!store) {
86420
86448
  return Response.json(
86421
86449
  { error: "User value storage is not supported by this runtime" },
86422
86450
  { status: 500 }
86423
86451
  );
86424
86452
  }
86425
- await agentBuilder.setUserValue(authResult.user.id, key, body.value ?? null);
86453
+ await store.setUserValue(authResult.user.id, key, body.value ?? null);
86426
86454
  return Response.json({ status: "ok", key });
86427
86455
  } catch (error) {
86428
86456
  console.error("Set user kv error:", error);
@@ -87484,6 +87512,124 @@ var requestId_post_default = defineController2(async ({ req, params, env: env2 }
87484
87512
  }
87485
87513
  });
87486
87514
 
87515
+ // src/api/users/[id]/grants/[name].delete.ts
87516
+ var name_delete_default2 = defineController2(async ({ req, env: env2, params }) => {
87517
+ const auth = await requireAdmin(req, env2);
87518
+ if (auth instanceof Response) return auth;
87519
+ const userId = params.id;
87520
+ const name15 = params.name;
87521
+ if (!userId || !name15) return Response.json({ error: "User ID and grant name are required" }, { status: 400 });
87522
+ const builder = env2.AGENT_BUILDER.get(env2.AGENT_BUILDER.idFromName("singleton"));
87523
+ if (!await builder.getUserById(userId)) {
87524
+ return Response.json({ error: "User not found" }, { status: 404 });
87525
+ }
87526
+ if (!env2.AGENT_BUILDER_USER_HUB) {
87527
+ return Response.json({ error: "The user scope is not available on this runtime" }, { status: 500 });
87528
+ }
87529
+ const hub = env2.AGENT_BUILDER_USER_HUB.get(
87530
+ env2.AGENT_BUILDER_USER_HUB.idFromName(userId)
87531
+ );
87532
+ await hub.deleteGrant(userId, name15);
87533
+ return new Response(null, { status: 204 });
87534
+ });
87535
+ var GRANT_NAME = /^[a-z0-9][a-z0-9._-]{0,127}$/;
87536
+ var HEADER_NAME = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/;
87537
+ var QUERY_NAME = /^[A-Za-z0-9._~-]{1,128}$/;
87538
+ var SUPPORTED_KINDS = /* @__PURE__ */ new Set(["bearer", "header", "query", "basic"]);
87539
+ var RECORD_KEYS = /* @__PURE__ */ new Set(["name", "kind", "origins", "inject", "allow", "metadata", "secret"]);
87540
+ var ALLOW_KEYS = /* @__PURE__ */ new Set(["tools", "endpoints", "userEndpoints", "agents"]);
87541
+ var INJECT_KEYS = /* @__PURE__ */ new Set(["header", "format", "query"]);
87542
+ function validateCredentialGrantRecord(value, routeName) {
87543
+ if (!value || typeof value !== "object" || Array.isArray(value)) return null;
87544
+ const input = value;
87545
+ if (Object.keys(input).some((key) => !RECORD_KEYS.has(key))) return null;
87546
+ const name15 = routeName ?? input.name;
87547
+ if (typeof name15 !== "string" || !GRANT_NAME.test(name15)) return null;
87548
+ if (input.name !== void 0 && input.name !== name15) return null;
87549
+ if (typeof input.secret !== "string" || input.secret.length === 0 || input.secret.length > 16 * 1024) return null;
87550
+ if (typeof input.kind !== "string" || !SUPPORTED_KINDS.has(input.kind)) return null;
87551
+ if ((input.kind === "bearer" || input.kind === "header") && /[\r\n]/.test(input.secret)) return null;
87552
+ if (!Array.isArray(input.origins) || input.origins.length === 0 || input.origins.length > 64) return null;
87553
+ const origins = [];
87554
+ for (const origin of input.origins) {
87555
+ if (typeof origin !== "string" || origin.length > 512) return null;
87556
+ if (origin.startsWith("*.")) {
87557
+ if (!/^\*\.[a-z0-9.-]+$/i.test(origin)) return null;
87558
+ } else {
87559
+ try {
87560
+ const url = new URL(origin);
87561
+ if (url.origin !== origin || url.protocol !== "https:" || url.username || url.password) return null;
87562
+ } catch {
87563
+ return null;
87564
+ }
87565
+ }
87566
+ origins.push(origin);
87567
+ }
87568
+ if (!input.allow || typeof input.allow !== "object" || Array.isArray(input.allow)) return null;
87569
+ const allowInput = input.allow;
87570
+ if (Object.keys(allowInput).some((key) => !ALLOW_KEYS.has(key))) return null;
87571
+ const allow = {};
87572
+ for (const key of ["tools", "endpoints", "userEndpoints", "agents"]) {
87573
+ const list = allowInput[key];
87574
+ if (list === void 0) continue;
87575
+ if (!Array.isArray(list) || list.length > 128 || !list.every((item) => typeof item === "string" && item.length > 0 && item.length <= 256)) {
87576
+ return null;
87577
+ }
87578
+ allow[key] = [...list];
87579
+ }
87580
+ if (Object.keys(allow).length === 0) return null;
87581
+ const inject = input.inject;
87582
+ if (inject !== void 0 && (!inject || typeof inject !== "object" || Array.isArray(inject))) return null;
87583
+ if (inject !== void 0) {
87584
+ const injection = inject;
87585
+ if (Object.keys(injection).some((key) => !INJECT_KEYS.has(key))) return null;
87586
+ if (injection.header !== void 0 && (typeof injection.header !== "string" || !HEADER_NAME.test(injection.header))) return null;
87587
+ if (injection.format !== void 0 && (typeof injection.format !== "string" || injection.format.length > 1024 || /[\r\n]/.test(injection.format))) return null;
87588
+ if (injection.query !== void 0 && (typeof injection.query !== "string" || !QUERY_NAME.test(injection.query))) return null;
87589
+ }
87590
+ const metadata = input.metadata;
87591
+ if (metadata !== void 0 && (!metadata || typeof metadata !== "object" || Array.isArray(metadata))) return null;
87592
+ if (metadata !== void 0) {
87593
+ try {
87594
+ if (JSON.stringify(metadata).length > 16 * 1024) return null;
87595
+ } catch {
87596
+ return null;
87597
+ }
87598
+ }
87599
+ const grant = {
87600
+ name: name15,
87601
+ kind: input.kind,
87602
+ origins,
87603
+ allow,
87604
+ ...inject === void 0 ? {} : { inject },
87605
+ ...metadata === void 0 ? {} : { metadata }
87606
+ };
87607
+ return { grant, secret: input.secret };
87608
+ }
87609
+
87610
+ // src/api/users/[id]/grants/[name].put.ts
87611
+ var name_put_default = defineController2(async ({ req, env: env2, params }) => {
87612
+ const auth = await requireAdmin(req, env2);
87613
+ if (auth instanceof Response) return auth;
87614
+ const userId = params.id;
87615
+ const name15 = params.name;
87616
+ if (!userId || !name15) return Response.json({ error: "User ID and grant name are required" }, { status: 400 });
87617
+ const record24 = validateCredentialGrantRecord(await req.json().catch(() => null), name15);
87618
+ if (!record24) return Response.json({ error: "Invalid credential grant" }, { status: 400 });
87619
+ const builder = env2.AGENT_BUILDER.get(env2.AGENT_BUILDER.idFromName("singleton"));
87620
+ if (!await builder.getUserById(userId)) {
87621
+ return Response.json({ error: "User not found" }, { status: 404 });
87622
+ }
87623
+ if (!env2.AGENT_BUILDER_USER_HUB) {
87624
+ return Response.json({ error: "The user scope is not available on this runtime" }, { status: 500 });
87625
+ }
87626
+ const hub = env2.AGENT_BUILDER_USER_HUB.get(
87627
+ env2.AGENT_BUILDER_USER_HUB.idFromName(userId)
87628
+ );
87629
+ await hub.setGrant(userId, record24.grant, record24.secret);
87630
+ return new Response(null, { status: 204 });
87631
+ });
87632
+
87487
87633
  // src/api/threads/[id]/effects/[effectId]/cancel.post.ts
87488
87634
  async function getAuthorizedThreadStub3(req, env2, threadId) {
87489
87635
  const authResult = await requireAuth(req, env2);
@@ -87949,6 +88095,7 @@ var routeHandlers = {
87949
88095
  "GET:/users/:id/**": rest_default,
87950
88096
  "GET:/users/:id/env": env_get_default,
87951
88097
  "PATCH:/users/:id/env": env_patch_default,
88098
+ "GET:/users/:id/grants": index_get_default4,
87952
88099
  "GET:/users/:id/stream": stream_default2,
87953
88100
  "GET:/variables/agents/:name": name_get_default6,
87954
88101
  "GET:/variables/prompts/:name": name_get_default7,
@@ -87965,7 +88112,7 @@ var routeHandlers = {
87965
88112
  "PATCH:/users/me/env": env_patch_default2,
87966
88113
  "GET:/users/me/hub": hub_default,
87967
88114
  "DELETE:/users/me/kv": index_delete_default,
87968
- "GET:/users/me/kv": index_get_default4,
88115
+ "GET:/users/me/kv": index_get_default5,
87969
88116
  "POST:/users/me/kv": index_post_default7,
87970
88117
  "GET:/users/me/stream": stream_default3,
87971
88118
  "GET:/auth/device/:code/approve": approve_get_default,
@@ -87979,6 +88126,8 @@ var routeHandlers = {
87979
88126
  "PATCH:/threads/:id/messages/:messageId": messageId_patch_default,
87980
88127
  "GET:/threads/:id/variables/:requestId": requestId_get_default,
87981
88128
  "POST:/threads/:id/variables/:requestId": requestId_post_default,
88129
+ "DELETE:/users/:id/grants/:name": name_delete_default2,
88130
+ "PUT:/users/:id/grants/:name": name_put_default,
87982
88131
  "POST:/threads/:id/effects/:effectId/cancel": cancel_post_default,
87983
88132
  "POST:/threads/:id/effects/:effectId/run": run_post_default,
87984
88133
  "GET:/threads/:id/logs/:logId/inspect/:provider": provider_get_default