An open-source, faster Devin alternative
TypeScript
Edge: automated Slack setup routes, public OAuth callback and channel consumers
Igor Zalutskiff12c7c11cdb
diff --git a/cloudflare-workers/api-edge/src/index.ts b/cloudflare-workers/api-edge/src/index.tsindex 984b79ff..ec3175d2 100644--- a/cloudflare-workers/api-edge/src/index.ts+++ b/cloudflare-workers/api-edge/src/index.ts@@ -85,6 +85,7 @@ import { createAPIKey, hashAPIKey } from "./api_keys";import {handleAgentWebhookInvocation,handleManagedGitHubCallback,+ handleManagedSlackCallback,handleManagedAgentChannelConnection,proxyManagedAgents,} from "./managed_agents";@@ -5479,6 +5480,15 @@ export default {) {return handleManagedGitHubCallback(req, env);}+ // Slack's OAuth redirect for apps created by the automated setup. This+ // exact URL is registered on every generated app; the backend answers+ // with a redirect into the project's Connections tab.+ if (+ path === "/api/managed-agents/slack/callback" &&+ req.method === "GET"+ ) {+ return handleManagedSlackCallback(req, env);+ }if (path.startsWith("/api/agent-webhooks/")) {return handleAgentWebhookInvocation(req, env);}diff --git a/cloudflare-workers/api-edge/src/managed_agents.test.ts b/cloudflare-workers/api-edge/src/managed_agents.test.tsindex b7f890bd..1327f95c 100644--- a/cloudflare-workers/api-edge/src/managed_agents.test.ts+++ b/cloudflare-workers/api-edge/src/managed_agents.test.ts@@ -4,6 +4,7 @@ import {handleAgentWebhookInvocation,handleManagedAgentChannelConnection,handleManagedGitHubCallback,+ handleManagedSlackCallback,hasBYOKPlanAccess,mintManagedAgentsAssertion,proxyManagedAgents,@@ -1492,6 +1493,370 @@ describe("managed agents proxy", () => {});});+ describe("automated Slack setup", () => {+ const env = {+ OC_MANAGED_AGENTS_SECRET: "test-secret",+ MANAGED_AGENTS_API_URL: "https://managedagents.test",+ };+ const caller = { orgID: "org_test", userID: "user_test" };+ // What the backend row carries beyond the public record: generated app+ // credentials, the manifest snapshot and the webhook identity.+ const backendSetup = {+ id: "setup_1",+ requestKey: "req_0123456789abcdef",+ projectId: "prj_test",+ ownerUserId: "user_private",+ agentId: "coder",+ alias: "development",+ channelId: "slack",+ connectionId: "channel_slack",+ webhookTokenHash: "private",+ name: "Patch",+ manifest: { display_information: { name: "Patch" } },+ phase: "app_created",+ attemptId: "attempt_private",+ app: { id: "A0APP", name: "Patch", clientId: "private" },+ clientSecret: "private",+ signingSecret: "private",+ configurationToken: "xoxe-private",+ error: {+ code: "slack_authorization_denied",+ message: "The installation was declined.",+ recoverable: true,+ at: "2026-09-17T10:05:00.000Z",+ provider: { body: "private" },+ },+ actions: ["authorize", "cancel", "delete_app"],+ createdAt: "2026-09-17T10:00:00.000Z",+ updatedAt: "2026-09-17T10:05:00.000Z",+ };+ const publicSetup = {+ id: "setup_1",+ requestKey: "req_0123456789abcdef",+ projectId: "prj_test",+ agentId: "coder",+ alias: "development",+ channelId: "slack",+ name: "Patch",+ connectionId: "channel_slack",+ phase: "app_created",+ app: { id: "A0APP", name: "Patch" },+ error: {+ code: "slack_authorization_denied",+ message: "The installation was declined.",+ recoverable: true,+ at: "2026-09-17T10:05:00.000Z",+ },+ actions: ["authorize", "cancel"],+ createdAt: "2026-09-17T10:00:00.000Z",+ updatedAt: "2026-09-17T10:05:00.000Z",+ };++ it("starts a setup and returns only the redacted record", async () => {+ const fetchSpy = vi+ .fn()+ .mockResolvedValue(+ Response.json({ setup: backendSetup }, { status: 201 }),+ );+ vi.stubGlobal("fetch", fetchSpy);++ const response = await proxyManagedAgents(+ new Request(+ "https://app.opencomputer.dev/api/managed-agents/channels/slack/setups",+ {+ method: "POST",+ headers: { "content-type": "application/json" },+ body: JSON.stringify({+ agentId: "coder",+ alias: "development",+ channelId: "slack",+ name: "Patch",+ requestKey: "req_0123456789abcdef",+ configurationToken: "xoxe-private",+ }),+ },+ ),+ env,+ caller,+ "/api/managed-agents",+ );++ expect(response.status).toBe(201);+ expect(response.headers.get("cache-control")).toBe("no-store");+ expect(await response.json()).toEqual({ setup: publicSetup });+ expect(fetchSpy.mock.calls[0]?.[0].toString()).toBe(+ "https://managedagents.test/v1/channels/slack/setups",+ );+ });++ it("rediscovers a setup by target and passes the query through", async () => {+ const fetchSpy = vi.fn().mockResolvedValue(Response.json({ setup: null }));+ vi.stubGlobal("fetch", fetchSpy);++ const response = await proxyManagedAgents(+ new Request(+ "https://app.opencomputer.dev/api/managed-agents/channels/slack/setups?agentId=coder&alias=development&channelId=slack",+ ),+ env,+ caller,+ "/api/managed-agents",+ );++ expect(await response.json()).toEqual({ setup: null });+ expect(fetchSpy.mock.calls[0]?.[0].toString()).toBe(+ "https://managedagents.test/v1/channels/slack/setups?agentId=coder&alias=development&channelId=slack",+ );+ });++ it("reads and cancels a setup through the same whitelist", async () => {+ vi.stubGlobal(+ "fetch",+ vi.fn(async () =>+ Response.json({ setup: { ...backendSetup, phase: "cancelled" } }),+ ),+ );++ const read = await proxyManagedAgents(+ new Request(+ "https://app.opencomputer.dev/api/managed-agents/channels/slack/setups/setup_1",+ ),+ env,+ caller,+ "/api/managed-agents",+ );+ const cancelled = await proxyManagedAgents(+ new Request(+ "https://app.opencomputer.dev/api/managed-agents/channels/slack/setups/setup_1/cancel",+ { method: "POST" },+ ),+ env,+ caller,+ "/api/managed-agents",+ );++ expect(await read.json()).toEqual({+ setup: { ...publicSetup, phase: "cancelled" },+ });+ expect(await cancelled.json()).toEqual({+ setup: { ...publicSetup, phase: "cancelled" },+ });+ });++ it("returns only the authorization URL when authorizing", async () => {+ vi.stubGlobal(+ "fetch",+ vi.fn().mockResolvedValue(+ Response.json({+ authorizationUrl:+ "https://slack.com/oauth/v2/authorize?client_id=1&scope=chat:write&state=opaque",+ expiresAt: "2026-09-17T10:15:00.000Z",+ state: "private",+ clientId: "private",+ }),+ ),+ );++ const response = await proxyManagedAgents(+ new Request(+ "https://app.opencomputer.dev/api/managed-agents/channels/slack/setups/setup_1/authorize",+ { method: "POST" },+ ),+ env,+ caller,+ "/api/managed-agents",+ );++ expect(await response.json()).toEqual({+ authorizationUrl:+ "https://slack.com/oauth/v2/authorize?client_id=1&scope=chat:write&state=opaque",+ expiresAt: "2026-09-17T10:15:00.000Z",+ });+ });++ it("maps setup errors to curated messages", async () => {+ const cases: Array<[number, string, string]> = [+ [+ 409,+ "slack_setup_active",+ "A Slack setup is already in progress for this agent and environment. Resume it instead of starting another.",+ ],+ [+ 409,+ "slack_already_connected",+ "Slack is already connected for this agent and environment. Disconnect it before creating another app.",+ ],+ [+ 409,+ "slack_setup_conflict",+ "This setup request was already used with a different bot name or target. Start a new setup.",+ ],+ [+ 503,+ "slack_setup_unavailable",+ "Automatic Slack setup is not available right now. Set up the app manually instead.",+ ],+ [+ 400,+ "slack_configuration_token_expired",+ "The configuration access token has expired. Generate a new one at api.slack.com/apps and try again.",+ ],+ ];+ for (const [status, code, message] of cases) {+ vi.stubGlobal(+ "fetch",+ vi.fn(async () =>+ Response.json(+ {+ error: { code, message: "backend text with xoxe-private" },+ setup: backendSetup,+ },+ { status },+ ),+ ),+ );+ const response = await proxyManagedAgents(+ new Request(+ "https://app.opencomputer.dev/api/managed-agents/channels/slack/setups",+ {+ method: "POST",+ headers: { "content-type": "application/json" },+ body: "{}",+ },+ ),+ env,+ caller,+ "/api/managed-agents",+ );+ expect(response.status).toBe(status);+ expect(await response.json()).toEqual({ error: { code, message } });+ }+ });++ it("lists the agents consuming each channel", async () => {+ vi.stubGlobal(+ "fetch",+ vi.fn().mockResolvedValue(+ Response.json({+ connections: [+ {+ id: "channel_slack",+ channelId: "team-slack",+ agentId: "coder",+ alias: "development",+ status: "connected",+ agents: ["coder", "reviewer", 7],+ botToken: "secret",+ createdAt: "2026-09-17T10:00:00.000Z",+ updatedAt: "2026-09-17T10:05:00.000Z",+ },+ ],+ }),+ ),+ );++ const response = await proxyManagedAgents(+ new Request("https://app.opencomputer.dev/api/managed-agents/channels"),+ env,+ caller,+ "/api/managed-agents",+ );++ expect(await response.json()).toEqual({+ channels: [+ {+ id: "channel_slack",+ channel: "slack",+ channelId: "team-slack",+ agentId: "coder",+ alias: "development",+ status: "connected",+ agents: ["coder", "reviewer"],+ createdAt: "2026-09-17T10:00:00.000Z",+ updatedAt: "2026-09-17T10:05:00.000Z",+ },+ ],+ });+ });++ it("passes the OAuth callback redirect through with its location", async () => {+ const fetchMock = vi.fn().mockResolvedValue(+ new Response(null, {+ status: 302,+ headers: {+ location:+ "https://app.opencomputer.dev/projects/prj_test/connections?environment=development&slack=connected&setup=setup_1",+ "set-cookie": "private=1",+ },+ }),+ );+ vi.stubGlobal("fetch", fetchMock);++ const response = await handleManagedSlackCallback(+ new Request(+ "https://app.opencomputer.dev/api/managed-agents/slack/callback?code=oauth-code&state=opaque",+ ),+ { MANAGED_AGENTS_API_URL: "https://managedagents.test" },+ );++ expect(response.status).toBe(302);+ expect(response.headers.get("location")).toBe(+ "https://app.opencomputer.dev/projects/prj_test/connections?environment=development&slack=connected&setup=setup_1",+ );+ expect(response.headers.get("cache-control")).toBe("no-store");+ expect(response.headers.get("set-cookie")).toBeNull();+ expect(fetchMock.mock.calls[0]?.[0].toString()).toBe(+ "https://managedagents.test/v1/channels/slack/oauth/callback?code=oauth-code&state=opaque",+ );+ expect(fetchMock.mock.calls[0]?.[1]).toEqual({ redirect: "manual" });+ });++ it("passes the OAuth callback's error page through without a location", async () => {+ vi.stubGlobal(+ "fetch",+ vi.fn().mockResolvedValue(+ new Response("<html>invalid state</html>", {+ status: 400,+ headers: {+ "content-type": "text/html; charset=utf-8",+ "x-frame-options": "DENY",+ },+ }),+ ),+ );++ const response = await handleManagedSlackCallback(+ new Request(+ "https://app.opencomputer.dev/api/managed-agents/slack/callback?state=broken",+ ),+ { MANAGED_AGENTS_API_URL: "https://managedagents.test" },+ );++ expect(response.status).toBe(400);+ expect(response.headers.get("location")).toBeNull();+ expect(response.headers.get("content-type")).toBe(+ "text/html; charset=utf-8",+ );+ expect(response.headers.get("x-frame-options")).toBe("DENY");+ expect(response.headers.get("cache-control")).toBe("no-store");+ await expect(response.text()).resolves.toContain("invalid state");+ });++ it("refuses to forward the OAuth callback over plain HTTP", async () => {+ const fetchMock = vi.fn();+ vi.stubGlobal("fetch", fetchMock);++ const response = await handleManagedSlackCallback(+ new Request(+ "https://app.opencomputer.dev/api/managed-agents/slack/callback?code=oauth-code&state=opaque",+ ),+ { MANAGED_AGENTS_API_URL: "http://managedagents.test" },+ );++ expect(response.status).toBe(503);+ expect(fetchMock).not.toHaveBeenCalled();+ });+ });+it("preserves the unavailable model id in deployment validation errors", async () => {vi.stubGlobal("fetch",diff --git a/cloudflare-workers/api-edge/src/managed_agents.ts b/cloudflare-workers/api-edge/src/managed_agents.tsindex 43a2bc53..6b08efdf 100644--- a/cloudflare-workers/api-edge/src/managed_agents.ts+++ b/cloudflare-workers/api-edge/src/managed_agents.ts@@ -122,6 +122,51 @@ function copyRequestHeaders(request: Request): Headers {return headers;}+// Automated Slack setup (docs/agents/channels.mdx "Connect Slack"). The+// backend's codes are stable; the messages are written here so a provider+// phrase never reaches the dashboard.+const SLACK_SETUP_ERROR_MESSAGES: Record<string, string> = {+ slack_setup_conflict:+ "This setup request was already used with a different bot name or target. Start a new setup.",+ slack_setup_active:+ "A Slack setup is already in progress for this agent and environment. Resume it instead of starting another.",+ slack_already_connected:+ "Slack is already connected for this agent and environment. Disconnect it before creating another app.",+ slack_setup_unavailable:+ "Automatic Slack setup is not available right now. Set up the app manually instead.",+ slack_setup_not_found: "That Slack setup does not exist.",+ slack_configuration_token_invalid:+ "Slack rejected the configuration access token. Generate a new token at api.slack.com/apps and try again.",+ slack_configuration_token_expired:+ "The configuration access token has expired. Generate a new one at api.slack.com/apps and try again.",+ slack_manifest_rejected:+ "Slack rejected the generated app manifest. Check the channel's declared scopes and events.",+ slack_app_limit_reached:+ "This Slack workspace has reached its app limit. Remove an unused app in Slack or choose another workspace.",+ slack_rate_limited:+ "Slack is rate limiting app creation. Wait a moment and try again.",+ slack_provider_unavailable:+ "Slack is temporarily unavailable. Try again shortly.",+ slack_creation_uncertain:+ "Slack may have created the app, but the result was lost. Check your Slack app list before creating another.",+ slack_exchange_uncertain:+ "The installation could not be confirmed. Authorize the app again.",+ slack_authorization_denied:+ "The Slack installation was declined. Authorize the app again when you are ready.",+ slack_authorization_expired:+ "The Slack authorization link expired. Authorize the app again.",+ slack_app_mismatch:+ "Slack installed a different app than the one created for this agent. Authorize the generated app again.",+ slack_workspace_mismatch:+ "The app was installed to a different Slack workspace than the one already connected.",+ slack_scope_missing:+ "The installed app is missing a permission the deployment declares. Authorize it again to reinstall with the current manifest.",+ slack_enterprise_install_unsupported:+ "Organization-wide Slack installations are not supported. Install the app into a single workspace.",+ slack_setup_superseded:+ "Another Slack connection change completed first. Check the current connection.",+};+async function publicErrorResponse(upstream: Response): Promise<Response> {const body: unknown = await upstream.json().catch(() => null);const backendError =@@ -151,7 +196,12 @@ async function publicErrorResponse(upstream: Response): Promise<Response> {? "template_manifest_missing": backendCode;let message = "The agent request could not be completed.";- if (missingTemplateManifest) {+ const slackSetupMessage = Object.hasOwn(SLACK_SETUP_ERROR_MESSAGES, backendCode)+ ? SLACK_SETUP_ERROR_MESSAGES[backendCode]+ : undefined;+ if (slackSetupMessage) {+ message = slackSetupMessage;+ } else if (missingTemplateManifest) {message ="This is not a valid template: oc-template.toml is missing from the repository root.";} else if (upstream.status === 400) {@@ -530,6 +580,68 @@ function publicChannel(value: unknown): Record<string, unknown> {...(Array.isArray(channel.destinations)? { destinations: channel.destinations.map(stripPrivateValues) }: {}),+ // The agents whose registrations consume this channel in the+ // connection's environment; what Project Connections lists as consumers.+ ...(Array.isArray(channel.agents)+ ? { agents: strings(channel.agents) }+ : {}),+ };+}++const SLACK_SETUP_ACTIONS = new Set(["create", "authorize", "manual", "cancel"]);+const SLACK_SETUPS_ROUTE = /^\/channels\/slack\/setups$/;+const SLACK_SETUP_ROUTE = /^\/channels\/slack\/setups\/[^/]+$/;+const SLACK_SETUP_AUTHORIZE_ROUTE = /^\/channels\/slack\/setups\/[^/]+\/authorize$/;+const SLACK_SETUP_CANCEL_ROUTE = /^\/channels\/slack\/setups\/[^/]+\/cancel$/;++/**+ * The automated Slack setup record. Redaction is by whitelist: the backend+ * row also carries the generated app credentials, the manifest snapshot and+ * the webhook identity, none of which the dashboard needs to resume.+ */+function publicSlackSetup(value: unknown): Record<string, unknown> {+ const setup = record(value) ?? {};+ const app = record(setup.app);+ const workspace = record(setup.workspace);+ const error = record(setup.error);+ return {+ id: setup.id,+ requestKey: setup.requestKey,+ projectId: setup.projectId,+ agentId: setup.agentId,+ alias: setup.alias,+ channelId: setup.channelId,+ name: setup.name,+ connectionId: setup.connectionId,+ phase: setup.phase,+ ...(app ? { app: { id: app.id, name: app.name } } : {}),+ ...(workspace+ ? { workspace: { id: workspace.id, name: workspace.name } }+ : {}),+ ...(typeof setup.botUserId === "string"+ ? { botUserId: setup.botUserId }+ : {}),+ ...(error+ ? {+ error: {+ code: error.code,+ message: error.message,+ recoverable: error.recoverable === true,+ ...(typeof error.retryAfterMs === "number"+ ? { retryAfterMs: error.retryAfterMs }+ : {}),+ ...(typeof error.pointer === "string"+ ? { pointer: error.pointer }+ : {}),+ at: error.at,+ },+ }+ : {}),+ actions: strings(setup.actions).filter((action) =>+ SLACK_SETUP_ACTIONS.has(action),+ ),+ createdAt: setup.createdAt,+ updatedAt: setup.updatedAt,};}@@ -1497,6 +1609,23 @@ function publicSuccessBody() {return publicChannel(body);}+ if (+ (method === "POST" && SLACK_SETUPS_ROUTE.test(suffix)) ||+ (method === "GET" && SLACK_SETUP_ROUTE.test(suffix)) ||+ (method === "POST" && SLACK_SETUP_CANCEL_ROUTE.test(suffix))+ ) {+ return { setup: publicSlackSetup(body.setup) };+ }+ if (method === "GET" && SLACK_SETUPS_ROUTE.test(suffix)) {+ // Lookup by target: the latest resumable setup, or null.+ return { setup: record(body.setup) ? publicSlackSetup(body.setup) : null };+ }+ if (method === "POST" && SLACK_SETUP_AUTHORIZE_ROUTE.test(suffix)) {+ return {+ authorizationUrl: body.authorizationUrl,+ expiresAt: body.expiresAt,+ };+ }if ((method === "GET" && suffix.startsWith("/connections")) ||(method === "POST" && suffix.startsWith("/connections")) ||@@ -1589,7 +1718,11 @@ async function publicSuccessResponse(const headers = new Headers({ "content-type": "application/json" });const cacheControl = upstream.headers.get("cache-control");if (cacheControl) headers.set("cache-control", cacheControl);- if (suffix.includes("/webhooks") || suffix.includes("/event-subscriptions")) {+ if (+ suffix.includes("/webhooks") ||+ suffix.includes("/event-subscriptions") ||+ suffix.startsWith("/channels/slack/setups")+ ) {headers.set("cache-control", "no-store");}return new Response(@@ -1966,6 +2099,62 @@ export async function handleManagedGitHubCallback(}}+/**+ * Slack's OAuth redirect for apps created by the automated setup. The exact+ * public URL is registered on every generated app, so it forwards the query+ * verbatim and never reinterprets it. The backend resolves the single-use+ * state and answers with a redirect into the project's Connections tab, or+ * a no-store HTML page for a malformed state; both pass through unchanged.+ */+export async function handleManagedSlackCallback(+ request: Request,+ env: ManagedAgentsEnv,+): Promise<Response> {+ if (request.method !== "GET") {+ return new Response("Method not allowed", { status: 405 });+ }+ const requestURL = new URL(request.url);+ const base = (+ env.MANAGED_AGENTS_API_URL ?? DEFAULT_MANAGED_AGENTS_API_URL+ ).replace(/\/+$/, "");+ const target = new URL(+ `${base}/v1/channels/slack/oauth/callback${requestURL.search}`,+ );+ if (target.protocol !== "https:" && target.hostname !== "localhost") {+ return new Response("Slack connection is unavailable", { status: 503 });+ }+ try {+ const upstream = await fetch(target, { redirect: "manual" });+ const headers = new Headers();+ for (const name of [+ "content-type",+ "cache-control",+ "content-security-policy",+ "referrer-policy",+ "x-content-type-options",+ "x-frame-options",+ ]) {+ const value = upstream.headers.get(name);+ if (value) headers.set(name, value);+ }+ if (upstream.status >= 300 && upstream.status < 400) {+ const location = upstream.headers.get("location");+ if (location) headers.set("location", location);+ }+ headers.set("cache-control", "no-store");+ return new Response(upstream.body, {+ status: upstream.status,+ statusText: upstream.statusText,+ headers,+ });+ } catch {+ return new Response("Slack connection is temporarily unavailable", {+ status: 502,+ headers: { "content-type": "text/plain; charset=utf-8" },+ });+ }+}+function channelConnectionPage(action: string,status: number,