An open-source, faster Devin alternative
TypeScript
api-edge: pass the project memory resource inventory through
Igor Zalutskicf846b15e381
diff --git a/cloudflare-workers/api-edge/src/managed_agents.test.ts b/cloudflare-workers/api-edge/src/managed_agents.test.tsindex 3772fe95..c16d60b9 100644--- a/cloudflare-workers/api-edge/src/managed_agents.test.ts+++ b/cloudflare-workers/api-edge/src/managed_agents.test.ts@@ -2177,4 +2177,87 @@ describe("managed agents proxy", () => {]);expect(body).not.toHaveProperty("runtimeToken");});+ // The resource inventory (C9) is the durable list of an environment's+ // resources: a resource no active deployment declares any more still+ // appears while it holds documents.+ it("lists the memory resource inventory with declared flags and document counts", async () => {+ const fetchSpy = vi.fn(async (target: RequestInfo | URL) => {+ expect(String(target)).toBe(+ "https://managedagents.test/v1/projects/prj_1/memory?environment=production",+ );+ return Response.json({+ resources: [+ {+ id: "requirements",+ provider: { kind: "document", maxBytes: 8192, bucket: "private" },+ declared: true,+ documents: 3,+ accountId: "acc_private",+ },+ {+ id: "scratch",+ provider: { kind: "document", maxBytes: 4096 },+ declared: false,+ documents: 2,+ },+ ],+ });+ });+ vi.stubGlobal("fetch", fetchSpy);++ const response = await proxyManagedAgents(+ new Request(+ "https://app.opencomputer.dev/api/managed-agents/projects/prj_1/memory?environment=production",+ ),+ memoryEnv,+ memoryCaller,+ "/api/managed-agents",+ );++ expect(response.status).toBe(200);+ expect(response.headers.get("cache-control")).toBe("no-store");+ expect(await response.json()).toEqual({+ resources: [+ {+ id: "requirements",+ provider: { kind: "document", maxBytes: 8192 },+ declared: true,+ documents: 3,+ },+ {+ id: "scratch",+ provider: { kind: "document", maxBytes: 4096 },+ declared: false,+ documents: 2,+ },+ ],+ });+ });++ it("keeps a backend without the inventory route answering 404 so clients can fall back", async () => {+ vi.stubGlobal(+ "fetch",+ vi.fn(async () =>+ Response.json(+ { error: { code: "not_found", message: "Route not found" } },+ { status: 404 },+ ),+ ),+ );++ const response = await proxyManagedAgents(+ new Request(+ "https://app.opencomputer.dev/api/managed-agents/projects/prj_1/memory?environment=development",+ ),+ memoryEnv,+ memoryCaller,+ "/api/managed-agents",+ );++ expect(response.status).toBe(404);+ expect(await response.json()).toEqual({+ error: { code: "not_found", message: "Route not found" },+ });+ });+});diff --git a/cloudflare-workers/api-edge/src/managed_agents.ts b/cloudflare-workers/api-edge/src/managed_agents.tsindex bf7ae06a..84f9c57d 100644--- a/cloudflare-workers/api-edge/src/managed_agents.ts+++ b/cloudflare-workers/api-edge/src/managed_agents.ts@@ -219,13 +219,23 @@ function strings(value: unknown): string[] {// backend mirrors the public routes one-to-one, so the edge passes the// documented bodies, status codes, error envelope and the conditional// headers (`ETag`, `If-Match`, `If-None-Match`) through untouched.+// The resource inventory is the durable list of an environment's resources,+// independent of what the current deployments declare: `declared` says+// whether an active deployment still names the resource, `documents` counts+// its live documents.+const MEMORY_RESOURCES_ROUTE = /^\/projects\/[^/]+\/memory$/;const MEMORY_DOCUMENTS_ROUTE = /^\/projects\/[^/]+\/memory\/[^/]+\/documents$/;const MEMORY_DOCUMENT_ROUTE =/^\/projects\/[^/]+\/memory\/[^/]+\/documents\/[^/]+$/;const MEMORY_CONDITIONAL_REQUEST_HEADERS = ["if-match", "if-none-match"];function isMemoryRoute(method: string, suffix: string): boolean {- if (method === "GET" && MEMORY_DOCUMENTS_ROUTE.test(suffix)) return true;+ if (+ method === "GET" &&+ (MEMORY_RESOURCES_ROUTE.test(suffix) || MEMORY_DOCUMENTS_ROUTE.test(suffix))+ ) {+ return true;+ }return ((method === "GET" ||method === "PUT" ||@@ -262,6 +272,17 @@ function publicMemoryDocument(value: unknown): Record<string, unknown> {return { ...publicMemoryDocumentMeta(document), text: document.text };}+function publicMemoryResource(value: unknown): Record<string, unknown> {+ const resource = record(value) ?? {};+ const provider = record(resource.provider) ?? {};+ return {+ id: resource.id,+ provider: { kind: provider.kind, maxBytes: provider.maxBytes },+ declared: resource.declared === true,+ documents: typeof resource.documents === "number" ? resource.documents : 0,+ };+}+// Memory errors are the documented `{ error: { code, message } }` envelope// with user-facing messages (stale revision, oversized text, missing// condition header), so client errors pass through as the backend wrote them.@@ -297,15 +318,21 @@ async function memoryResponse(const body = record(await upstream.json().catch(() => null)) ?? {};headers.set("content-type", "application/json");const value =- method === "GET" && MEMORY_DOCUMENTS_ROUTE.test(suffix)+ method === "GET" && MEMORY_RESOURCES_ROUTE.test(suffix)? {- documents: Array.isArray(body.documents)- ? body.documents.map(publicMemoryDocumentMeta)+ resources: Array.isArray(body.resources)+ ? body.resources.map(publicMemoryResource): [],- nextCursor:- typeof body.nextCursor === "string" ? body.nextCursor : null,}- : publicMemoryDocument(body);+ : method === "GET" && MEMORY_DOCUMENTS_ROUTE.test(suffix)+ ? {+ documents: Array.isArray(body.documents)+ ? body.documents.map(publicMemoryDocumentMeta)+ : [],+ nextCursor:+ typeof body.nextCursor === "string" ? body.nextCursor : null,+ }+ : publicMemoryDocument(body);return new Response(JSON.stringify(value), {status: upstream.status,headers,