Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

3.3 KB
Raw Blame
import { z } from 'zod'
import { parseDiscordConfig, type DiscordConfig } from './discord-config.js'
import { parseSlackConfig, type SlackConfig } from './slack-config.js'

const OptionalTextSchema = z.string().min(1).optional()

const EnvSchema = z.object({
  ORCA_CONNECTOR_MODE: z.enum(['active', 'validation']).default('active'),
  // The same image runs the API, or the one Discord gateway connection (its own service).
  ORCA_CONNECTOR_ROLE: z.enum(['api', 'discord-gateway']).default('api'),
  PORT: z.coerce.number().int().positive().default(8080),
  ORCA_CONNECTOR_PUBLIC_URL: z.string().url(),
  ORCA_CONNECTOR_DATABASE_URL: OptionalTextSchema,
  ORCA_CONNECTOR_DATA_DIR: z.string().min(1).default('./data/connector'),
  ORCA_CONNECTOR_DATABASE_POOL_MAX: z.coerce.number().int().positive().max(100).default(10),
  ORCA_CONNECTOR_AUTH_ISSUER: z.string().url(),
  ORCA_CONNECTOR_JWKS_URL: z.string().url(),
  // As push: proxies appending to x-forwarded-for after the client (0 is Cloud Run alone).
  ORCA_CONNECTOR_TRUSTED_PROXY_HOPS: z.coerce.number().int().nonnegative().max(8).default(0)
})

export type ConnectorConfig = {
  mode: 'active' | 'validation'
  role: 'api' | 'discord-gateway'
  port: number
  publicUrl: string
  databaseUrl?: string
  dataDir: string
  databasePoolMax: number
  authIssuer: string
  jwksUrl: string
  trustedProxyHops: number
  slack?: SlackConfig
  discord?: DiscordConfig
}

function secureUrl(value: string, name: string): URL {
  const url = new URL(value)
  const loopback = ['127.0.0.1', 'localhost', '[::1]'].includes(url.hostname)
  if (url.protocol !== 'https:' && !(loopback && url.protocol === 'http:')) {
    throw new Error(`${name} must use HTTPS outside loopback development`)
  }
  return url
}

function canonicalOrigin(value: string, name: string): string {
  if (secureUrl(value, name).origin !== value) {
    throw new Error(`${name} must be an origin`)
  }
  return value
}

export function loadConnectorConfig(env: NodeJS.ProcessEnv = process.env): ConnectorConfig {
  const present = Object.fromEntries(
    Object.entries(env).map(([key, value]) => [key, value?.trim() === '' ? undefined : value])
  )
  const parsed = EnvSchema.parse(present)
  const slack = parseSlackConfig(present)
  const discord = parseDiscordConfig(present)
  if (parsed.ORCA_CONNECTOR_ROLE === 'discord-gateway' && discord === undefined) {
    throw new Error('The discord-gateway role needs the Discord settings')
  }
  return {
    mode: parsed.ORCA_CONNECTOR_MODE,
    role: parsed.ORCA_CONNECTOR_ROLE,
    port: parsed.PORT,
    publicUrl: canonicalOrigin(parsed.ORCA_CONNECTOR_PUBLIC_URL, 'ORCA_CONNECTOR_PUBLIC_URL'),
    ...(parsed.ORCA_CONNECTOR_DATABASE_URL === undefined
      ? {}
      : { databaseUrl: parsed.ORCA_CONNECTOR_DATABASE_URL }),
    dataDir: parsed.ORCA_CONNECTOR_DATA_DIR,
    databasePoolMax: parsed.ORCA_CONNECTOR_DATABASE_POOL_MAX,
    authIssuer: canonicalOrigin(parsed.ORCA_CONNECTOR_AUTH_ISSUER, 'ORCA_CONNECTOR_AUTH_ISSUER'),
    // The signing keys come from here, so plain HTTP would let anyone on the path mint tokens.
    jwksUrl: secureUrl(parsed.ORCA_CONNECTOR_JWKS_URL, 'ORCA_CONNECTOR_JWKS_URL').href,
    trustedProxyHops: parsed.ORCA_CONNECTOR_TRUSTED_PROXY_HOPS,
    ...(slack === undefined ? {} : { slack }),
    ...(discord === undefined ? {} : { discord })
  }
}