@standardagents/builder 0.10.1-dev.d2d335e → 0.10.1-next.82e11d5

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.
@@ -777,6 +777,11 @@ var providers_get_default = defineController(async ({ env }) => {
777
777
 
778
778
  // src/api/threads/index.post.ts
779
779
  var index_post_default3 = defineController(async ({ req, env }) => {
780
+ const authResult = await requireAuth(req, env);
781
+ if (authResult instanceof Response) {
782
+ return authResult;
783
+ }
784
+ const auth = authResult;
780
785
  let body;
781
786
  try {
782
787
  body = await req.json();
@@ -786,7 +791,8 @@ var index_post_default3 = defineController(async ({ req, env }) => {
786
791
  { status: 400 }
787
792
  );
788
793
  }
789
- const { agent_id, user_id, initial_messages, data, tags } = body;
794
+ const { agent_id, initial_messages, data, tags } = body;
795
+ const user_id = auth.authType === "super_admin" ? null : auth.user.id;
790
796
  if (!agent_id) {
791
797
  return Response.json(
792
798
  { error: "Missing required field: agent_id" },
@@ -828,7 +834,7 @@ var index_post_default3 = defineController(async ({ req, env }) => {
828
834
  try {
829
835
  const thread = await agentBuilder.createThread({
830
836
  agent_name: agent_id,
831
- user_id,
837
+ user_id: user_id ?? void 0,
832
838
  tags
833
839
  });
834
840
  return Response.json({
@@ -862,13 +868,18 @@ var threads_default = defineController(async ({ req, env }) => {
862
868
  if (req.method !== "GET") {
863
869
  return new Response("Method Not Allowed", { status: 405 });
864
870
  }
871
+ const authResult = await requireAuth(req, env);
872
+ if (authResult instanceof Response) {
873
+ return authResult;
874
+ }
875
+ const auth = authResult;
865
876
  try {
866
877
  const url = new URL(req.url);
867
878
  const origin = url.origin;
868
879
  const limit = parseInt(url.searchParams.get("limit") || "50", 10);
869
880
  const offset = parseInt(url.searchParams.get("offset") || "0", 10);
870
881
  const agentName = url.searchParams.get("agent_id");
871
- const userId = url.searchParams.get("user_id");
882
+ const userId = auth.user.role === "admin" ? void 0 : auth.user.id;
872
883
  const agentBuilderId = env.AGENT_BUILDER.idFromName("singleton");
873
884
  const agentBuilder = env.AGENT_BUILDER.get(agentBuilderId);
874
885
  const result = await agentBuilder.listThreads({
@@ -1079,6 +1090,7 @@ var index_get_default2 = defineController(async ({ req, env }) => {
1079
1090
  });
1080
1091
 
1081
1092
  // src/api/users/index.post.ts
1093
+ var VALID_ROLES = ["admin", "user"];
1082
1094
  var index_post_default4 = defineController(async ({ req, env }) => {
1083
1095
  try {
1084
1096
  const authResult = await requireAdmin(req, env);
@@ -1086,7 +1098,7 @@ var index_post_default4 = defineController(async ({ req, env }) => {
1086
1098
  return authResult;
1087
1099
  }
1088
1100
  const body = await req.json();
1089
- const { username, password, role = "admin" } = body;
1101
+ const { username, password, role = "user" } = body;
1090
1102
  if (!username || !password) {
1091
1103
  return Response.json(
1092
1104
  { error: "Username and password are required" },
@@ -1111,6 +1123,12 @@ var index_post_default4 = defineController(async ({ req, env }) => {
1111
1123
  { status: 400 }
1112
1124
  );
1113
1125
  }
1126
+ if (!VALID_ROLES.includes(role)) {
1127
+ return Response.json(
1128
+ { error: `Invalid role. Must be one of: ${VALID_ROLES.join(", ")}` },
1129
+ { status: 400 }
1130
+ );
1131
+ }
1114
1132
  const agentBuilderId = env.AGENT_BUILDER.idFromName("singleton");
1115
1133
  const agentBuilder = env.AGENT_BUILDER.get(agentBuilderId);
1116
1134
  const existingUser = await agentBuilder.getUserByUsername(username);
@@ -1331,8 +1349,24 @@ var name_get_default2 = defineController(async ({ params, prompts, models, model
1331
1349
  }
1332
1350
  });
1333
1351
 
1352
+ // src/utils/permissions.ts
1353
+ function canAccessThread(auth, thread) {
1354
+ if (auth.user.role === "admin") {
1355
+ return true;
1356
+ }
1357
+ if (thread.user_id === null) {
1358
+ return false;
1359
+ }
1360
+ return thread.user_id === auth.user.id;
1361
+ }
1362
+
1334
1363
  // src/api/threads/[id].delete.ts
1335
1364
  var id_delete_default2 = defineController(async ({ req, env, params }) => {
1365
+ const authResult = await requireAuth(req, env);
1366
+ if (authResult instanceof Response) {
1367
+ return authResult;
1368
+ }
1369
+ const auth = authResult;
1336
1370
  const threadId = params.id;
1337
1371
  if (!threadId) {
1338
1372
  return Response.json({ error: "Missing thread ID" }, { status: 400 });
@@ -1346,6 +1380,12 @@ var id_delete_default2 = defineController(async ({ req, env, params }) => {
1346
1380
  { status: 404 }
1347
1381
  );
1348
1382
  }
1383
+ if (!canAccessThread(auth, thread)) {
1384
+ return Response.json(
1385
+ { error: "Forbidden: You don't have access to this thread" },
1386
+ { status: 403 }
1387
+ );
1388
+ }
1349
1389
  try {
1350
1390
  await agentBuilder.deleteThread(threadId);
1351
1391
  const durableId = env.AGENT_BUILDER_THREAD.idFromName(threadId);
@@ -1373,6 +1413,11 @@ var id_patch_default = defineController(async ({ req, env, params }) => {
1373
1413
  if (req.method !== "PATCH") {
1374
1414
  return new Response("Method Not Allowed", { status: 405 });
1375
1415
  }
1416
+ const authResult = await requireAuth(req, env);
1417
+ if (authResult instanceof Response) {
1418
+ return authResult;
1419
+ }
1420
+ const auth = authResult;
1376
1421
  const threadId = params?.id;
1377
1422
  if (!threadId) {
1378
1423
  return Response.json({ error: "Thread ID required" }, { status: 400 });
@@ -1389,6 +1434,12 @@ var id_patch_default = defineController(async ({ req, env, params }) => {
1389
1434
  { status: 404 }
1390
1435
  );
1391
1436
  }
1437
+ if (!canAccessThread(auth, existingThread)) {
1438
+ return Response.json(
1439
+ { error: "Forbidden: You don't have access to this thread" },
1440
+ { status: 403 }
1441
+ );
1442
+ }
1392
1443
  const updates = {};
1393
1444
  if (tags !== void 0) {
1394
1445
  if (!Array.isArray(tags)) {
@@ -1472,11 +1523,31 @@ function resolveIconUrl2(icon, origin) {
1472
1523
  return icon;
1473
1524
  }
1474
1525
  var id_default = defineController(async ({ req, params, env }) => {
1526
+ const authResult = await requireAuth(req, env);
1527
+ if (authResult instanceof Response) {
1528
+ return authResult;
1529
+ }
1530
+ const auth = authResult;
1475
1531
  const threadId = params.id;
1476
1532
  if (!threadId) {
1477
1533
  return Response.json({ error: "Thread ID required" }, { status: 400 });
1478
1534
  }
1479
1535
  try {
1536
+ const agentBuilderId = env.AGENT_BUILDER.idFromName("singleton");
1537
+ const agentBuilder = env.AGENT_BUILDER.get(agentBuilderId);
1538
+ const thread = await agentBuilder.getThread(threadId);
1539
+ if (!thread) {
1540
+ return Response.json(
1541
+ { error: `Thread not found: ${threadId}` },
1542
+ { status: 404 }
1543
+ );
1544
+ }
1545
+ if (!canAccessThread(auth, thread)) {
1546
+ return Response.json(
1547
+ { error: "Forbidden: You don't have access to this thread" },
1548
+ { status: 403 }
1549
+ );
1550
+ }
1480
1551
  const durableId = env.AGENT_BUILDER_THREAD.idFromName(threadId);
1481
1552
  const stub = env.AGENT_BUILDER_THREAD.get(durableId);
1482
1553
  const doData = await stub.getThreadMeta(threadId);
@@ -1652,7 +1723,7 @@ var login_post_default = defineController(async ({ req, env }) => {
1652
1723
  { status: 400 }
1653
1724
  );
1654
1725
  }
1655
- if (env.SUPER_ADMIN_PASSWORD && password === env.SUPER_ADMIN_PASSWORD) {
1726
+ if (env.SUPER_ADMIN_PASSWORD && username === "admin" && password === env.SUPER_ADMIN_PASSWORD) {
1656
1727
  if (!env.ENCRYPTION_KEY) {
1657
1728
  return Response.json(
1658
1729
  { error: "Server misconfigured: ENCRYPTION_KEY required for super admin" },