@standardagents/builder 0.18.3 → 0.19.1

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.
@@ -63365,11 +63365,19 @@ function platformEndpoint(env2) {
63365
63365
  return FALLBACK_PLATFORM_ORIGIN;
63366
63366
  }
63367
63367
  function hostedInstanceRedirectId(req, env2) {
63368
- const configured = env2.STANDARD_AGENTS_PROJECT_ID || env2.STANDARD_AGENTS_INSTANCE_ID || env2.STANDARD_AGENTS_INSTANCE_SUBDOMAIN;
63368
+ const configured = env2.STANDARD_AGENTS_INSTANCE_HOST || env2.STANDARD_AGENTS_INSTANCE_SUBDOMAIN || env2.STANDARD_AGENTS_INSTANCE_ID;
63369
63369
  if (typeof configured === "string" && configured.trim()) {
63370
63370
  return configured.trim();
63371
63371
  }
63372
- return new URL(req.url).hostname;
63372
+ const hostname = new URL(req.url).hostname;
63373
+ if (hostname && hostname !== "localhost" && hostname !== "127.0.0.1" && hostname !== "standardagents.internal") {
63374
+ return hostname;
63375
+ }
63376
+ const projectId = env2.STANDARD_AGENTS_PROJECT_ID;
63377
+ if (typeof projectId === "string" && projectId.trim()) {
63378
+ return projectId.trim();
63379
+ }
63380
+ return hostname;
63373
63381
  }
63374
63382
  function platformLoginUrl(req, env2, returnTo) {
63375
63383
  const url = new URL("/login", platformEndpoint(env2));
@@ -63379,6 +63387,14 @@ function platformLoginUrl(req, env2, returnTo) {
63379
63387
  url.searchParams.set("return_to", next || "/");
63380
63388
  return url.toString();
63381
63389
  }
63390
+ function platformLogoutUrl(req, env2, returnTo) {
63391
+ const url = new URL("/auth/logout", platformEndpoint(env2));
63392
+ url.searchParams.set("redirect", hostedInstanceRedirectId(req, env2));
63393
+ const reqUrl = new URL(req.url);
63394
+ const next = returnTo && returnTo.startsWith("/") && !returnTo.startsWith("//") ? returnTo : `${reqUrl.pathname}${reqUrl.search}`;
63395
+ url.searchParams.set("return_to", next || "/");
63396
+ return url.toString();
63397
+ }
63382
63398
  function sanitizeUsername(input) {
63383
63399
  const normalized = input.trim().toLowerCase().replace(/[^a-z0-9_-]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 50);
63384
63400
  return normalized || "standard-agents";
@@ -63851,6 +63867,10 @@ var diagnostics_get_default = defineController2(async ({ tools, prompts, promptN
63851
63867
 
63852
63868
  // src/api/events.get.ts
63853
63869
  var events_get_default = defineController2(async ({ req, env: env2 }) => {
63870
+ const authResult = await requireAuth(req, env2);
63871
+ if (authResult instanceof Response) {
63872
+ return authResult;
63873
+ }
63854
63874
  if (req.headers.get("upgrade")?.toLowerCase() !== "websocket") {
63855
63875
  return Response.json(
63856
63876
  { error: "This endpoint requires a WebSocket connection" },
@@ -65785,7 +65805,7 @@ var config_get_default2 = defineController2(async ({ req, env: env2 }) => {
65785
65805
  const platformConnected = hasPlatformApiKey(env2) || !!findLocalBootstrapSession(void 0, env2);
65786
65806
  const localPassword = typeof env2.SUPER_ADMIN_PASSWORD === "string" && env2.SUPER_ADMIN_PASSWORD.length > 0;
65787
65807
  const hosted = isPlatformHosted(env2);
65788
- const standardAgentsLogin = hosted;
65808
+ const standardAgentsLogin = false;
65789
65809
  return {
65790
65810
  github: githubConfigured,
65791
65811
  google: googleConfigured,
@@ -65885,26 +65905,57 @@ var login_post_default = defineController2(async ({ req, env: env2 }) => {
65885
65905
  }
65886
65906
  });
65887
65907
 
65908
+ // src/api/auth/_logout.ts
65909
+ async function deleteLocalSession(req, env2) {
65910
+ const authHeader = req.headers.get("Authorization");
65911
+ const token = authHeader && authHeader.startsWith("Bearer ") ? authHeader.substring(7) : readSessionCookie(req);
65912
+ if (!token) {
65913
+ return;
65914
+ }
65915
+ if (!token.includes(".") && isValidUserToken(token)) {
65916
+ const tokenHash = await hashToken(token);
65917
+ const agentBuilder = env2.AGENT_BUILDER.get(env2.AGENT_BUILDER.idFromName("singleton"));
65918
+ await agentBuilder.deleteSession(tokenHash);
65919
+ }
65920
+ }
65921
+ function clearedSessionCookie(req) {
65922
+ return buildSessionCookie(req, "", 0);
65923
+ }
65924
+
65925
+ // src/api/auth/logout.get.ts
65926
+ function safeReturnTo(value) {
65927
+ if (!value || !value.startsWith("/") || value.startsWith("//")) return "/";
65928
+ return value;
65929
+ }
65930
+ var logout_get_default = defineController2(async ({ req, env: env2 }) => {
65931
+ try {
65932
+ await deleteLocalSession(req, env2);
65933
+ } catch (error) {
65934
+ console.error("Logout error:", error);
65935
+ }
65936
+ const url = new URL(req.url);
65937
+ const returnTo = safeReturnTo(url.searchParams.get("return_to"));
65938
+ const location = isPlatformHosted(env2) ? platformLogoutUrl(req, env2, returnTo) : `${url.origin}/login`;
65939
+ return new Response(null, {
65940
+ status: 302,
65941
+ headers: {
65942
+ Location: location,
65943
+ "Set-Cookie": clearedSessionCookie(req)
65944
+ }
65945
+ });
65946
+ });
65947
+
65888
65948
  // src/api/auth/logout.post.ts
65889
65949
  var logout_post_default = defineController2(async ({ req, env: env2 }) => {
65890
65950
  const clearCookie = (body, status = 200) => new Response(JSON.stringify(body), {
65891
65951
  status,
65892
65952
  headers: {
65893
65953
  "Content-Type": "application/json",
65894
- "Set-Cookie": buildSessionCookie(req, "", 0)
65954
+ "Set-Cookie": clearedSessionCookie(req)
65895
65955
  }
65896
65956
  });
65897
65957
  try {
65898
- const authHeader = req.headers.get("Authorization");
65899
- const token = authHeader && authHeader.startsWith("Bearer ") ? authHeader.substring(7) : readSessionCookie(req);
65900
- if (!token) {
65901
- return clearCookie({ success: true });
65902
- }
65903
- if (!token.includes(".") && isValidUserToken(token)) {
65904
- const tokenHash = await hashToken(token);
65905
- const agentBuilder = env2.AGENT_BUILDER.get(env2.AGENT_BUILDER.idFromName("singleton"));
65906
- await agentBuilder.deleteSession(tokenHash);
65907
- }
65958
+ await deleteLocalSession(req, env2);
65908
65959
  return clearCookie({ success: true });
65909
65960
  } catch (error) {
65910
65961
  console.error("Logout error:", error);
@@ -68444,6 +68495,10 @@ var kv_default = defineController2(async ({ req, params, env: env2, url }) => {
68444
68495
 
68445
68496
  // src/api/threads/[id]/logs.get.ts
68446
68497
  var logs_get_default = defineController2(async ({ req, params, env: env2 }) => {
68498
+ const authResult = await requireAuth(req, env2);
68499
+ if (authResult instanceof Response) {
68500
+ return authResult;
68501
+ }
68447
68502
  const url = new URL(req.url);
68448
68503
  const threadId = params.id;
68449
68504
  if (!threadId) {
@@ -68708,6 +68763,10 @@ var stop_post_default = defineController2(async ({ params, env: env2 }) => {
68708
68763
 
68709
68764
  // src/api/threads/[id]/stream.ts
68710
68765
  var stream_default = defineController2(async ({ req, params, env: env2 }) => {
68766
+ const authResult = await requireAuth(req, env2);
68767
+ if (authResult instanceof Response) {
68768
+ return authResult;
68769
+ }
68711
68770
  const threadId = params.id;
68712
68771
  if (!threadId) {
68713
68772
  return Response.json({ error: "Thread ID required" }, { status: 400 });
@@ -68945,12 +69004,20 @@ var name_get_default7 = defineController2(async ({ params, url, prompts, agents,
68945
69004
 
68946
69005
  // src/api/auth/sa/callback.get.ts
68947
69006
  var SESSION_TTL_SECONDS2 = 30 * 24 * 60 * 60;
68948
- function safeReturnTo(value) {
69007
+ function safeReturnTo2(value) {
68949
69008
  if (!value || !value.startsWith("/") || value.startsWith("//")) return "/";
68950
69009
  return value;
68951
69010
  }
68952
- function failRedirect(req, reason) {
69011
+ function failRedirect(req, env2, reason) {
68953
69012
  const origin = new URL(req.url).origin;
69013
+ if (isPlatformHosted(env2)) {
69014
+ const target = new URL(platformLoginUrl(req, env2, "/"));
69015
+ target.searchParams.set("error", reason === "forbidden" ? "no_instance_access" : "instance_login_failed");
69016
+ return new Response(null, {
69017
+ status: 302,
69018
+ headers: { Location: target.toString() }
69019
+ });
69020
+ }
68954
69021
  return new Response(null, {
68955
69022
  status: 302,
68956
69023
  headers: { Location: `${origin}/login?sa_error=${encodeURIComponent(reason)}` }
@@ -68958,12 +69025,12 @@ function failRedirect(req, reason) {
68958
69025
  }
68959
69026
  var callback_get_default = defineController2(async ({ req, env: env2 }) => {
68960
69027
  if (!isPlatformHosted(env2)) {
68961
- return failRedirect(req, "not_hosted");
69028
+ return failRedirect(req, env2, "not_hosted");
68962
69029
  }
68963
69030
  const url = new URL(req.url);
68964
69031
  const handoff = url.searchParams.get("handoff") || url.searchParams.get("token");
68965
69032
  if (!handoff) {
68966
- return failRedirect(req, "invalid_response");
69033
+ return failRedirect(req, env2, "invalid_response");
68967
69034
  }
68968
69035
  const configuredProjectId = typeof env2.STANDARD_AGENTS_PROJECT_ID === "string" ? env2.STANDARD_AGENTS_PROJECT_ID : void 0;
68969
69036
  const payload = await verifyPlatformSignedPayload(
@@ -68975,10 +69042,10 @@ var callback_get_default = defineController2(async ({ req, env: env2 }) => {
68975
69042
  }
68976
69043
  );
68977
69044
  if (!payload || !payload.platform_user_id || !payload.role) {
68978
- return failRedirect(req, "forbidden");
69045
+ return failRedirect(req, env2, "forbidden");
68979
69046
  }
68980
69047
  if (!configuredProjectId && payload.project_id !== hostedInstanceRedirectId(req, env2)) {
68981
- return failRedirect(req, "forbidden");
69048
+ return failRedirect(req, env2, "forbidden");
68982
69049
  }
68983
69050
  const session = await mintLocalSessionForPlatformUser(env2, {
68984
69051
  platform_user_id: payload.platform_user_id,
@@ -68988,7 +69055,7 @@ var callback_get_default = defineController2(async ({ req, env: env2 }) => {
68988
69055
  avatar_url: payload.avatar_url ?? null,
68989
69056
  role: payload.role
68990
69057
  });
68991
- const returnTo = safeReturnTo(url.searchParams.get("return_to"));
69058
+ const returnTo = safeReturnTo2(url.searchParams.get("return_to"));
68992
69059
  return new Response(null, {
68993
69060
  status: 302,
68994
69061
  headers: {
@@ -70108,6 +70175,7 @@ var routeHandlers = {
70108
70175
  "POST:/auth/bootstrap": bootstrap_post_default,
70109
70176
  "GET:/auth/config": config_get_default2,
70110
70177
  "POST:/auth/login": login_post_default,
70178
+ "GET:/auth/logout": logout_get_default,
70111
70179
  "POST:/auth/logout": logout_post_default,
70112
70180
  "GET:/auth/me": me_get_default,
70113
70181
  "POST:/auth/platform-replica": platform_replica_post_default,