Skip to content

A desktop coding agent with free access to frontier models.

TypeScript
4.1 KB
Raw Blame
import type { Hono } from "hono"
import type { RequestIdVariables } from "hono/request-id"
import { delegatedRoute } from "../../middleware/index.js"
import { registerOrgApiKeyRoutes } from "./api-keys.js"
import { registerOrgBillingRoutes } from "./billing.js"
import { registerOrgBrandAssetRoutes } from "./brand-assets.js"
import { registerOrgWorkflowRunRoutes } from "./codemode-runs.js"
import { registerOrgWorkflowRoutes } from "./codemode-scripts.js"
import { LEGACY_ORG_PROXY_HEADER } from "../../middleware/user-organizations.js"
import type { OrgRouteVariables } from "./shared.js"
import { registerOrgCoreRoutes } from "./core.js"
import { registerOrgDashboardRoutes } from "./dashboards.js"
import { registerDeleteOrganizationRoutes } from "./delete-organization.js"
import { registerOrgDesktopPolicyRoutes } from "./desktop-policies.js"
import { registerOrgEgressDiagnosticRoutes } from "./egress-diagnostics.js"
import { registerOrgInvitationRoutes } from "./invitations.js"
import { registerGoogleWorkspaceRoutes } from "./google-workspace.js"
import { registerOrgInstallLinkRoutes } from "./install-links.js"
import { registerOrgInferenceRoutes } from "./inference.js"
import { registerModelsAnalyticsRoutes } from "./models-analytics.js"
import { registerModelsAnalyticsExportRoutes } from "../../models-analytics-export.js"
import { registerOrgLlmProviderRoutes } from "./llm-providers.js"
import { registerOrgMemberRoutes } from "./members.js"
import { registerMcpConnectionRoutes } from "./mcp-connections.js"
import { registerMicrosoft365Routes } from "./microsoft-365.js"
import { registerOAuthProviderRoutes } from "./oauth-providers.js"
import { registerPluginArchRoutes } from "./plugin-system/routes.js"
import { registerOrgRoleRoutes } from "./roles.js"
import { registerOrgScimRoutes } from "./scim.js"
import { registerOrgSsoRoutes } from "./sso.js"
import { registerOrgResourceRoutes } from "./resources.js"
import { registerOrgTeamRoutes } from "./teams.js"

const LEGACY_ORG_PATH_PREFIX = "/v1/orgs/"

function extractLegacyOrgProxyTarget(pathname: string) {
  if (!pathname.startsWith(LEGACY_ORG_PATH_PREFIX)) {
    return null
  }

  const remainder = pathname.slice(LEGACY_ORG_PATH_PREFIX.length)
  const slashIndex = remainder.indexOf("/")
  if (slashIndex <= 0) {
    return null
  }

  const organizationId = remainder.slice(0, slashIndex)
  if (!organizationId.startsWith("org_")) {
    return null
  }

  const targetPath = `/v1${remainder.slice(slashIndex)}`
  if (targetPath === pathname) {
    return null
  }

  return { organizationId, targetPath }
}

export function registerOrgRoutes<T extends { Variables: OrgRouteVariables & RequestIdVariables }>(app: Hono<T>) {
  registerOrgCoreRoutes(app)
  registerDeleteOrganizationRoutes(app)
  registerOrgApiKeyRoutes(app)
  registerOrgBillingRoutes(app)
  registerOrgBrandAssetRoutes(app)
  registerOrgWorkflowRunRoutes(app)
  registerOrgWorkflowRoutes(app)
  registerOrgDashboardRoutes(app)
  registerOrgDesktopPolicyRoutes(app)
  registerOrgEgressDiagnosticRoutes(app)
  registerOrgInferenceRoutes(app)
  registerModelsAnalyticsRoutes(app)
  registerModelsAnalyticsExportRoutes(app)
  registerOrgScimRoutes(app)
  registerOrgSsoRoutes(app)
  registerOrgInvitationRoutes(app)
  registerOrgInstallLinkRoutes(app)
  registerOrgLlmProviderRoutes(app)
  registerOrgMemberRoutes(app)
  registerOAuthProviderRoutes(app)
  registerGoogleWorkspaceRoutes(app)
  registerMicrosoft365Routes(app)
  registerMcpConnectionRoutes(app)
  registerPluginArchRoutes(app)
  registerOrgRoleRoutes(app)
  registerOrgResourceRoutes(app)
  registerOrgTeamRoutes(app)

  app.all("/v1/orgs/:orgId/*", delegatedRoute, async (c) => {
    const url = new URL(c.req.raw.url)
    const target = extractLegacyOrgProxyTarget(url.pathname)
    if (!target) {
      return c.json({ error: "not_found" }, 404)
    }

    const proxiedUrl = new URL(url)
    proxiedUrl.pathname = target.targetPath

    const headers = new Headers(c.req.raw.headers)
    headers.set(LEGACY_ORG_PROXY_HEADER, target.organizationId)

    const proxiedRequest = new Request(new Request(proxiedUrl, c.req.raw), { headers })

    return app.fetch(proxiedRequest)
  })
}