Skip to content

An open-source, faster Devin alternative

TypeScript

api-edge: forward the question dismiss route; words for held_inputs_full and the Linear routes' shared codes

Igor Zalutski103bbfe93d42
diff --git a/cloudflare-workers/api-edge/src/managed_agents.test.ts b/cloudflare-workers/api-edge/src/managed_agents.test.ts
index e1cccb67..3fffab78 100644
--- a/cloudflare-workers/api-edge/src/managed_agents.test.ts
+++ b/cloudflare-workers/api-edge/src/managed_agents.test.ts
@@ -4175,6 +4175,89 @@ describe("managed agents proxy", () => {
});
});
+ it("forwards a dismiss of the open question and returns the sanitized session", async () => {
+ const fetchSpy = vi.fn(async () =>
+ Response.json({
+ id: "session-1",
+ status: "idle",
+ accountId: "acct_private",
+ turns: [],
+ question: null,
+ }),
+ );
+ vi.stubGlobal("fetch", fetchSpy);
+ const dismiss = (method: string) =>
+ proxyManagedAgents(
+ new Request(
+ `https://app.opencomputer.dev/api/managed-agents/sessions/session-1/questions/${question.id}/dismiss`,
+ { method },
+ ),
+ env,
+ caller,
+ "/api/managed-agents",
+ );
+ const response = await dismiss("POST");
+ expect(response.status).toBe(200);
+ const [target, init] = fetchSpy.mock.calls[0] as unknown as [URL, RequestInit];
+ expect(String(target)).toBe(
+ `https://managedagents.test/v1/sessions/session-1/questions/${question.id}/dismiss`,
+ );
+ expect(init.method).toBe("POST");
+ expect(await response.json()).toEqual({
+ id: "session-1",
+ status: "idle",
+ turns: [],
+ question: null,
+ });
+
+ // Only POST, and only on a named question.
+ expect((await dismiss("GET")).status).toBe(404);
+ const bare = await proxyManagedAgents(
+ new Request(
+ "https://app.opencomputer.dev/api/managed-agents/sessions/session-1/questions/dismiss",
+ { method: "POST" },
+ ),
+ env,
+ caller,
+ "/api/managed-agents",
+ );
+ expect(bare.status).toBe(404);
+ expect(fetchSpy).toHaveBeenCalledTimes(1);
+ });
+
+ it("words held_inputs_full for the caller", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn(async () =>
+ Response.json(
+ { error: { code: "held_inputs_full", message: "held inputs exceed 65536 bytes for q_1" } },
+ { status: 409 },
+ ),
+ ),
+ );
+ const response = await proxyManagedAgents(
+ new Request(
+ "https://app.opencomputer.dev/api/managed-agents/sessions/session-1/turns",
+ {
+ method: "POST",
+ headers: { "content-type": "application/json" },
+ body: JSON.stringify({ input: "One more constraint" }),
+ },
+ ),
+ env,
+ caller,
+ "/api/managed-agents",
+ );
+ expect(response.status).toBe(409);
+ expect(await response.json()).toEqual({
+ error: {
+ code: "held_inputs_full",
+ message:
+ "Too many messages are waiting on the agent's question. Answer or dismiss the question, then send again.",
+ },
+ });
+ });
+
it("passes a held receipt through with its question and no turn", async () => {
vi.stubGlobal(
"fetch",
@@ -6172,6 +6255,17 @@ describe("Linear agent connections", () => {
"fetch",
vi
.fn()
+ .mockResolvedValueOnce(
+ Response.json(
+ {
+ error: {
+ code: "project_archived",
+ message: "Restore the project before starting new work",
+ },
+ },
+ { status: 409 },
+ ),
+ )
.mockResolvedValueOnce(
Response.json(
{ error: { code: "project_not_found", message: "Project not found" } },
@@ -6191,12 +6285,25 @@ describe("Linear agent connections", () => {
),
);
+ const archived = await send("/projects/prj_old/linear/connections", {
+ method: "POST",
+ headers: { "content-type": "application/json" },
+ body: JSON.stringify({ name: "Triage", environment: "production", agentId: "triage" }),
+ });
+ expect(archived.status).toBe(409);
+ await expect(archived.json()).resolves.toEqual({
+ error: {
+ code: "project_archived",
+ message: "Restore this project before connecting Linear.",
+ },
+ });
+
const listed = await send("/projects/prj_other/linear/connections");
expect(listed.status).toBe(404);
await expect(listed.json()).resolves.toEqual({
error: {
code: "project_not_found",
- message: "The requested agent resource was not found.",
+ message: "That project does not exist.",
},
});
@@ -6208,7 +6315,8 @@ describe("Linear agent connections", () => {
await expect(authorized.json()).resolves.toEqual({
error: {
code: "forbidden",
- message: "The agent request was not authorized.",
+ message:
+ "Project-scoped API keys cannot create or change Linear connections. Use an organization API key.",
},
});
});
diff --git a/cloudflare-workers/api-edge/src/managed_agents.ts b/cloudflare-workers/api-edge/src/managed_agents.ts
index f359395f..fa39bf32 100644
--- a/cloudflare-workers/api-edge/src/managed_agents.ts
+++ b/cloudflare-workers/api-edge/src/managed_agents.ts
@@ -206,7 +206,10 @@ const WORKSPACE_EXPORT_ERROR_MESSAGES: Record<string, string> = {
workspace_export_failed: "The workspace export failed.",
};
-async function publicErrorResponse(upstream: Response): Promise<Response> {
+async function publicErrorResponse(
+ upstream: Response,
+ suffix?: string,
+): Promise<Response> {
const body: unknown = await upstream.json().catch(() => null);
const backendError =
body &&
@@ -252,9 +255,16 @@ async function publicErrorResponse(upstream: Response): Promise<Response> {
: undefined;
const linearMessage = Object.hasOwn(LINEAR_ERROR_MESSAGES, backendCode)
? LINEAR_ERROR_MESSAGES[backendCode]
- : undefined;
+ : suffix !== undefined &&
+ isLinearRouteSuffix(suffix) &&
+ Object.hasOwn(LINEAR_ROUTE_ERROR_MESSAGES, backendCode)
+ ? LINEAR_ROUTE_ERROR_MESSAGES[backendCode]
+ : undefined;
if (slackSetupMessage) {
message = slackSetupMessage;
+ } else if (backendCode === "held_inputs_full") {
+ message =
+ "Too many messages are waiting on the agent's question. Answer or dismiss the question, then send again.";
} else if (linearMessage) {
message = linearMessage;
} else if (missingTemplateManifest) {
@@ -855,6 +865,14 @@ const LINEAR_ERROR_MESSAGES: Record<string, string> = {
'Linear does not allow app names that contain "Linear". Choose another name.',
};
+// Shared codes worded for the Linear connection routes that return them.
+const LINEAR_ROUTE_ERROR_MESSAGES: Record<string, string> = {
+ forbidden:
+ "Project-scoped API keys cannot create or change Linear connections. Use an organization API key.",
+ project_not_found: "That project does not exist.",
+ project_archived: "Restore this project before connecting Linear.",
+};
+
/** A Linear connection by whitelist: never its credentials or account. */
function publicLinearConnection(value: unknown): Record<string, unknown> {
const connection = record(value) ?? {};
@@ -2140,7 +2158,9 @@ function publicSuccessBody(
(method === "GET" && /^\/sessions\/[^/]+$/.test(suffix)) ||
(method === "PATCH" && /^\/sessions\/[^/]+\/labels$/.test(suffix)) ||
(method === "POST" &&
- /^\/sessions\/[^/]+\/(resume|end|terminate|interrupt)$/.test(suffix))
+ /^\/sessions\/[^/]+\/(resume|end|terminate|interrupt)$/.test(suffix)) ||
+ (method === "POST" &&
+ /^\/sessions\/[^/]+\/questions\/[^/]+\/dismiss$/.test(suffix))
) {
return publicSessionSnapshot(body);
}
@@ -2723,6 +2743,13 @@ function isAllowedManagedAgentsRoute(method: string, suffix: string): boolean {
) {
return true;
}
+ // Closes the open question without an answer.
+ if (
+ method === "POST" &&
+ /^\/sessions\/[^/]+\/questions\/[^/]+\/dismiss$/.test(suffix)
+ ) {
+ return true;
+ }
return (
method === "POST" &&
/^\/sessions\/[^/]+\/(turns|suspend|resume|end|terminate|interrupt)$/.test(
@@ -3397,7 +3424,7 @@ export async function proxyManagedAgents(
},
});
}
- if (!upstream.ok) return publicErrorResponse(upstream);
+ if (!upstream.ok) return publicErrorResponse(upstream, suffix);
if (upstream.status === 204) return new Response(null, { status: 204 });
if (
/^\/projects\/[^/]+\/source-archive$/.test(suffix) ||