@standardagents/builder 0.18.2 → 0.19.0

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.
@@ -63379,6 +63379,14 @@ function platformLoginUrl(req, env2, returnTo) {
63379
63379
  url.searchParams.set("return_to", next || "/");
63380
63380
  return url.toString();
63381
63381
  }
63382
+ function platformLogoutUrl(req, env2, returnTo) {
63383
+ const url = new URL("/auth/logout", platformEndpoint(env2));
63384
+ url.searchParams.set("redirect", hostedInstanceRedirectId(req, env2));
63385
+ const reqUrl = new URL(req.url);
63386
+ const next = returnTo && returnTo.startsWith("/") && !returnTo.startsWith("//") ? returnTo : `${reqUrl.pathname}${reqUrl.search}`;
63387
+ url.searchParams.set("return_to", next || "/");
63388
+ return url.toString();
63389
+ }
63382
63390
  function sanitizeUsername(input) {
63383
63391
  const normalized = input.trim().toLowerCase().replace(/[^a-z0-9_-]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 50);
63384
63392
  return normalized || "standard-agents";
@@ -63851,6 +63859,10 @@ var diagnostics_get_default = defineController2(async ({ tools, prompts, promptN
63851
63859
 
63852
63860
  // src/api/events.get.ts
63853
63861
  var events_get_default = defineController2(async ({ req, env: env2 }) => {
63862
+ const authResult = await requireAuth(req, env2);
63863
+ if (authResult instanceof Response) {
63864
+ return authResult;
63865
+ }
63854
63866
  if (req.headers.get("upgrade")?.toLowerCase() !== "websocket") {
63855
63867
  return Response.json(
63856
63868
  { error: "This endpoint requires a WebSocket connection" },
@@ -65785,7 +65797,7 @@ var config_get_default2 = defineController2(async ({ req, env: env2 }) => {
65785
65797
  const platformConnected = hasPlatformApiKey(env2) || !!findLocalBootstrapSession(void 0, env2);
65786
65798
  const localPassword = typeof env2.SUPER_ADMIN_PASSWORD === "string" && env2.SUPER_ADMIN_PASSWORD.length > 0;
65787
65799
  const hosted = isPlatformHosted(env2);
65788
- const standardAgentsLogin = hosted;
65800
+ const standardAgentsLogin = false;
65789
65801
  return {
65790
65802
  github: githubConfigured,
65791
65803
  google: googleConfigured,
@@ -65885,26 +65897,57 @@ var login_post_default = defineController2(async ({ req, env: env2 }) => {
65885
65897
  }
65886
65898
  });
65887
65899
 
65900
+ // src/api/auth/_logout.ts
65901
+ async function deleteLocalSession(req, env2) {
65902
+ const authHeader = req.headers.get("Authorization");
65903
+ const token = authHeader && authHeader.startsWith("Bearer ") ? authHeader.substring(7) : readSessionCookie(req);
65904
+ if (!token) {
65905
+ return;
65906
+ }
65907
+ if (!token.includes(".") && isValidUserToken(token)) {
65908
+ const tokenHash = await hashToken(token);
65909
+ const agentBuilder = env2.AGENT_BUILDER.get(env2.AGENT_BUILDER.idFromName("singleton"));
65910
+ await agentBuilder.deleteSession(tokenHash);
65911
+ }
65912
+ }
65913
+ function clearedSessionCookie(req) {
65914
+ return buildSessionCookie(req, "", 0);
65915
+ }
65916
+
65917
+ // src/api/auth/logout.get.ts
65918
+ function safeReturnTo(value) {
65919
+ if (!value || !value.startsWith("/") || value.startsWith("//")) return "/";
65920
+ return value;
65921
+ }
65922
+ var logout_get_default = defineController2(async ({ req, env: env2 }) => {
65923
+ try {
65924
+ await deleteLocalSession(req, env2);
65925
+ } catch (error) {
65926
+ console.error("Logout error:", error);
65927
+ }
65928
+ const url = new URL(req.url);
65929
+ const returnTo = safeReturnTo(url.searchParams.get("return_to"));
65930
+ const location = isPlatformHosted(env2) ? platformLogoutUrl(req, env2, returnTo) : `${url.origin}/login`;
65931
+ return new Response(null, {
65932
+ status: 302,
65933
+ headers: {
65934
+ Location: location,
65935
+ "Set-Cookie": clearedSessionCookie(req)
65936
+ }
65937
+ });
65938
+ });
65939
+
65888
65940
  // src/api/auth/logout.post.ts
65889
65941
  var logout_post_default = defineController2(async ({ req, env: env2 }) => {
65890
65942
  const clearCookie = (body, status = 200) => new Response(JSON.stringify(body), {
65891
65943
  status,
65892
65944
  headers: {
65893
65945
  "Content-Type": "application/json",
65894
- "Set-Cookie": buildSessionCookie(req, "", 0)
65946
+ "Set-Cookie": clearedSessionCookie(req)
65895
65947
  }
65896
65948
  });
65897
65949
  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
- }
65950
+ await deleteLocalSession(req, env2);
65908
65951
  return clearCookie({ success: true });
65909
65952
  } catch (error) {
65910
65953
  console.error("Logout error:", error);
@@ -68444,6 +68487,10 @@ var kv_default = defineController2(async ({ req, params, env: env2, url }) => {
68444
68487
 
68445
68488
  // src/api/threads/[id]/logs.get.ts
68446
68489
  var logs_get_default = defineController2(async ({ req, params, env: env2 }) => {
68490
+ const authResult = await requireAuth(req, env2);
68491
+ if (authResult instanceof Response) {
68492
+ return authResult;
68493
+ }
68447
68494
  const url = new URL(req.url);
68448
68495
  const threadId = params.id;
68449
68496
  if (!threadId) {
@@ -68708,6 +68755,10 @@ var stop_post_default = defineController2(async ({ params, env: env2 }) => {
68708
68755
 
68709
68756
  // src/api/threads/[id]/stream.ts
68710
68757
  var stream_default = defineController2(async ({ req, params, env: env2 }) => {
68758
+ const authResult = await requireAuth(req, env2);
68759
+ if (authResult instanceof Response) {
68760
+ return authResult;
68761
+ }
68711
68762
  const threadId = params.id;
68712
68763
  if (!threadId) {
68713
68764
  return Response.json({ error: "Thread ID required" }, { status: 400 });
@@ -68945,12 +68996,20 @@ var name_get_default7 = defineController2(async ({ params, url, prompts, agents,
68945
68996
 
68946
68997
  // src/api/auth/sa/callback.get.ts
68947
68998
  var SESSION_TTL_SECONDS2 = 30 * 24 * 60 * 60;
68948
- function safeReturnTo(value) {
68999
+ function safeReturnTo2(value) {
68949
69000
  if (!value || !value.startsWith("/") || value.startsWith("//")) return "/";
68950
69001
  return value;
68951
69002
  }
68952
- function failRedirect(req, reason) {
69003
+ function failRedirect(req, env2, reason) {
68953
69004
  const origin = new URL(req.url).origin;
69005
+ if (isPlatformHosted(env2)) {
69006
+ const target = new URL(platformLoginUrl(req, env2, "/"));
69007
+ target.searchParams.set("error", reason === "forbidden" ? "no_instance_access" : "instance_login_failed");
69008
+ return new Response(null, {
69009
+ status: 302,
69010
+ headers: { Location: target.toString() }
69011
+ });
69012
+ }
68954
69013
  return new Response(null, {
68955
69014
  status: 302,
68956
69015
  headers: { Location: `${origin}/login?sa_error=${encodeURIComponent(reason)}` }
@@ -68958,12 +69017,12 @@ function failRedirect(req, reason) {
68958
69017
  }
68959
69018
  var callback_get_default = defineController2(async ({ req, env: env2 }) => {
68960
69019
  if (!isPlatformHosted(env2)) {
68961
- return failRedirect(req, "not_hosted");
69020
+ return failRedirect(req, env2, "not_hosted");
68962
69021
  }
68963
69022
  const url = new URL(req.url);
68964
69023
  const handoff = url.searchParams.get("handoff") || url.searchParams.get("token");
68965
69024
  if (!handoff) {
68966
- return failRedirect(req, "invalid_response");
69025
+ return failRedirect(req, env2, "invalid_response");
68967
69026
  }
68968
69027
  const configuredProjectId = typeof env2.STANDARD_AGENTS_PROJECT_ID === "string" ? env2.STANDARD_AGENTS_PROJECT_ID : void 0;
68969
69028
  const payload = await verifyPlatformSignedPayload(
@@ -68975,10 +69034,10 @@ var callback_get_default = defineController2(async ({ req, env: env2 }) => {
68975
69034
  }
68976
69035
  );
68977
69036
  if (!payload || !payload.platform_user_id || !payload.role) {
68978
- return failRedirect(req, "forbidden");
69037
+ return failRedirect(req, env2, "forbidden");
68979
69038
  }
68980
69039
  if (!configuredProjectId && payload.project_id !== hostedInstanceRedirectId(req, env2)) {
68981
- return failRedirect(req, "forbidden");
69040
+ return failRedirect(req, env2, "forbidden");
68982
69041
  }
68983
69042
  const session = await mintLocalSessionForPlatformUser(env2, {
68984
69043
  platform_user_id: payload.platform_user_id,
@@ -68988,7 +69047,7 @@ var callback_get_default = defineController2(async ({ req, env: env2 }) => {
68988
69047
  avatar_url: payload.avatar_url ?? null,
68989
69048
  role: payload.role
68990
69049
  });
68991
- const returnTo = safeReturnTo(url.searchParams.get("return_to"));
69050
+ const returnTo = safeReturnTo2(url.searchParams.get("return_to"));
68992
69051
  return new Response(null, {
68993
69052
  status: 302,
68994
69053
  headers: {
@@ -70108,6 +70167,7 @@ var routeHandlers = {
70108
70167
  "POST:/auth/bootstrap": bootstrap_post_default,
70109
70168
  "GET:/auth/config": config_get_default2,
70110
70169
  "POST:/auth/login": login_post_default,
70170
+ "GET:/auth/logout": logout_get_default,
70111
70171
  "POST:/auth/logout": logout_post_default,
70112
70172
  "GET:/auth/me": me_get_default,
70113
70173
  "POST:/auth/platform-replica": platform_replica_post_default,