# Cubic Echo — AI infrastructure, all in one
# ai-router (models, MCP servers and agents in one app), min-agent, task-server,
# and one shared Postgres. https://cubicecho.com/guides/ai-infrastructure/
#
# Set in the stack's environment (generate secrets with `openssl rand -hex 32`):
#   PUBLIC_HOST            this machine's LAN address or hostname
#   POSTGRES_PASSWORD
#   AI_ROUTER_AUTH_SECRET
# Optional: OPENAI_API_KEY for min-agent / task-server.
#
# Set an app's *_DATABASE_URL to use your own Postgres instead of the bundled one.
name: cubicecho-ai

services:
  ai-router:
    image: ghcr.io/cubicecho/ai-router:latest
    restart: unless-stopped
    depends_on:
      postgres: { condition: service_healthy }
    environment:
      DATABASE_URL: ${AI_ROUTER_DATABASE_URL:-postgres://cubicecho:${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD}@postgres:5432/ai_router}
      BETTER_AUTH_SECRET: ${AI_ROUTER_AUTH_SECRET:?set AI_ROUTER_AUTH_SECRET}
      APP_URL: ${AI_ROUTER_URL:-http://${PUBLIC_HOST:-localhost}:${AI_ROUTER_PORT:-3202}}
      OIDC_ISSUER: ${AI_ROUTER_OIDC_ISSUER:-}   # or pair with heautou from the sign-in page
      OIDC_CLIENT_ID: ${AI_ROUTER_OIDC_CLIENT_ID:-}
      OIDC_CLIENT_SECRET: ${AI_ROUTER_OIDC_CLIENT_SECRET:-}
      SECURE_LOCAL_NET: ${SECURE_LOCAL_NET:-false}
      TRUST_PROXY: ${TRUST_PROXY:-false}
    ports:
      - "${AI_ROUTER_PORT:-3202}:3000"
    volumes:
      - ai-router-data:/data

  min-agent:
    image: ghcr.io/cubicecho/min-agent:latest
    restart: unless-stopped
    depends_on:
      postgres: { condition: service_healthy }
    environment:
      DATABASE_URL: ${MIN_AGENT_DATABASE_URL:-postgres://cubicecho:${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD}@postgres:5432/min_agent}
      OPENAI_API_KEY: ${OPENAI_API_KEY:-}
      PORT: 8787
    ports:
      - "${MIN_AGENT_PORT:-3203}:8787"
    extra_hosts:
      - "host.docker.internal:host-gateway"

  task-server:
    image: ghcr.io/cubicecho/task_server:latest
    restart: unless-stopped
    depends_on:
      postgres: { condition: service_healthy }
    environment:
      DATABASE_URL: ${TASK_SERVER_DATABASE_URL:-postgres://cubicecho:${POSTGRES_PASSWORD}@postgres:5432/task_server}
      OPENAI_API_KEY: ${OPENAI_API_KEY:-}
      TZ: ${TZ:-UTC}
    ports:
      - "${TASK_SERVER_PORT:-3204}:8787"
    volumes:
      - task-server-data:/data

  # One Postgres for the apps that need one, one database each.
  postgres:
    image: postgres:17-alpine
    restart: unless-stopped
    environment:
      POSTGRES_USER: cubicecho
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: cubicecho
    configs:
      - source: create-databases
        target: /docker-entrypoint-initdb.d/create-databases.sql
    volumes:
      - postgres-data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U cubicecho -d cubicecho"]
      interval: 5s
      timeout: 5s
      retries: 10

# Runs once, when the Postgres volume is first created.
configs:
  create-databases:
    content: |
      CREATE DATABASE ai_router;
      CREATE DATABASE min_agent;
      CREATE DATABASE task_server;

volumes:
  ai-router-data:
  task-server-data:
  postgres-data:
