An open-source, faster Devin alternative
TypeScript
Edge: the Work API routes, and a dashboard member's name and email in the assertion (work tracking 3.10)
Igor Zalutski via Computer3d790d104bdb
diff --git a/cloudflare-workers/api-edge/src/computer_api.test.ts b/cloudflare-workers/api-edge/src/computer_api.test.tsindex 4927fc86..12cd4380 100644--- a/cloudflare-workers/api-edge/src/computer_api.test.ts+++ b/cloudflare-workers/api-edge/src/computer_api.test.ts@@ -52,8 +52,8 @@ class FakeStatement {role: "admin",} as T;}- if (this.sql.includes("SELECT role FROM org_memberships")) {- return { role: "owner" } as T;+ if (this.sql.includes("FROM org_memberships m LEFT JOIN users u")) {+ return { role: "owner", user_name: "Igor", user_email: "[email protected]" } as T;}if (this.sql.includes("SELECT id FROM api_keys WHERE key_hash")) {const row = this.db.keys.get(String(this.args[0]));@@ -245,6 +245,11 @@ describe("computer API allowlist", () => {["PUT", "/computer/sessions/cs_01h/secrets/API_KEY"],["DELETE", "/computer/me/secrets/API_KEY"],["PUT", "/computer/organization/secrets/API_KEY"],+ ["GET", "/computer/work/repository"],+ ["PUT", "/computer/work/repository"],+ ["GET", "/computer/work/items"],+ ["GET", "/computer/work/items/22"],+ ["GET", "/computer/work/items/22/history"],["GET", "/computer/sessions/cs_01h/workspace/archive"],["PUT", "/computer/sessions/cs_01h/workspace/archive"],["GET", "/computer/sessions/cs_01h/workspace/file"],@@ -265,6 +270,10 @@ describe("computer API allowlist", () => {["GET", "/computer/sessions/a%2Fb"],["GET", "/computer/sessions/cs_01h/unknown"],["POST", "/computer/slack/install"],+ // Close and Reopen come with build step 5.+ ["POST", "/computer/work/items/22/close"],+ ["DELETE", "/computer/work/repository"],+ ["GET", "/computer/work/files"],])("refuses %s %s", (method, suffix) => {expect(isComputerApiRoute(method, suffix)).toBe(false);});@@ -509,7 +518,7 @@ describe("computer API proxy to blue", () => {expect(sent(fetchMock).headers.get("x-request-id")).toBeNull();});- it("asserts a dashboard session with no key claims", async () => {+ it("asserts a dashboard session with no key claims, and the member's name and email", async () => {const fetchMock = vi.fn().mockResolvedValue(Response.json({ sessions: [], nextCursor: null }));vi.stubGlobal("fetch", fetchMock);const res = await worker.fetch(@@ -526,8 +535,9 @@ describe("computer API proxy to blue", () => {for (const claim of Object.keys(payload)) {expect(claim.startsWith("key_")).toBe(false);}- expect(payload.user_name).toBeUndefined();- expect(payload.user_email).toBeUndefined();+ // The member's name and email, from the membership read (work tracking 3.10).+ expect(payload.user_name).toBe("Igor");+ expect(payload.user_email).toBe("[email protected]");});it("refuses a cookie's unsafe call from another origin", async () => {diff --git a/cloudflare-workers/api-edge/src/computer_api.ts b/cloudflare-workers/api-edge/src/computer_api.tsindex f6a6acb2..e9023df6 100644--- a/cloudflare-workers/api-edge/src/computer_api.ts+++ b/cloudflare-workers/api-edge/src/computer_api.ts@@ -72,6 +72,11 @@ const COMPUTER_API_ROUTES: ReadonlyArray<{methods: new Set(["PUT", "DELETE"]),pattern: route(`/(?:sessions/${SEG}|me|organization)/secrets/${SEG}`),},+ // The Work API (work tracking tech design 3.10).+ { methods: new Set(["GET", "PUT"]), pattern: route("/work/repository") },+ { methods: new Set(["GET"]), pattern: route("/work/items") },+ { methods: new Set(["GET"]), pattern: route(`/work/items/${SEG}`) },+ { methods: new Set(["GET"]), pattern: route(`/work/items/${SEG}/history`) },];const WORKSPACE_ROUTE = new RegExp(diff --git a/cloudflare-workers/api-edge/src/index.ts b/cloudflare-workers/api-edge/src/index.tsindex 23e88ba0..88ba2f22 100644--- a/cloudflare-workers/api-edge/src/index.ts+++ b/cloudflare-workers/api-edge/src/index.ts@@ -1570,11 +1570,15 @@ async function computerApiCall(reason: "origin_not_allowed",});}+ // The member's name and email ride along (work tracking 3.10: the Work+ // API's `mine` and the author of a person's write need them).const membership = await env.OPENCOMPUTER_DB.prepare(- `SELECT role FROM org_memberships WHERE org_id = ?1 AND user_id = ?2`,+ `SELECT m.role, u.name AS user_name, u.email AS user_email+ FROM org_memberships m LEFT JOIN users u ON u.id = m.user_id+ WHERE m.org_id = ?1 AND m.user_id = ?2`,).bind(session.orgID, session.userID)- .first<{ role: string }>();+ .first<{ role: string; user_name: string | null; user_email: string | null }>();if (!membership) {facts.auth = "not_member";return computerApiError(req, 401, "unauthenticated", "Sign in again.");@@ -1586,6 +1590,8 @@ async function computerApiCall(membership.role === "owner" || membership.role === "admin"? "admin": membership.role,+ userName: membership.user_name ?? null,+ userEmail: membership.user_email ?? null,};}}diff --git a/cloudflare-workers/api-edge/src/managed_agents.ts b/cloudflare-workers/api-edge/src/managed_agents.tsindex 1550f96f..35f7bc04 100644--- a/cloudflare-workers/api-edge/src/managed_agents.ts+++ b/cloudflare-workers/api-edge/src/managed_agents.ts@@ -50,7 +50,7 @@ export interface ManagedAgentsCaller {sessionKey?: string;/** Set only when the caller authenticated with an API key (work 084). */key?: AssertedAPIKey;- /** The key creator's name and email, when the auth read loaded them. */+ /** The key creator's (or the dashboard member's) name and email, when the auth read loaded them. */userName?: string | null;userEmail?: string | null;}@@ -143,9 +143,10 @@ export async function mintManagedAgentsAssertion(payload.key_scopes = caller.key.scopes;payload.key_expires_at = caller.key.expiresAt;payload.key_created_at = caller.key.createdAt;- if (caller.userName) payload.user_name = caller.userName;- if (caller.userEmail) payload.user_email = caller.userEmail;}+ // A key's creator, or a dashboard session's member (work tracking 3.10).+ if (caller.userName) payload.user_name = caller.userName;+ if (caller.userEmail) payload.user_email = caller.userEmail;const encoder = new TextEncoder();const signingInput =`${b64url(encoder.encode(JSON.stringify(header)))}.` +