# Cubic Echo: AI, piecemeal, for QNAP Container Station 3
# Applications → Create, paste this in, then Validate YAML and Create.
# Guide: https://cubicecho.com/guides/ai-infrastructure/  ·  QNAP: https://cubicecho.com/guides/qnap/
#
# Container Station reads no .env file, so replace each of these everywhere
# it appears (generate secrets with `openssl rand -hex 32`):
#   CHANGE_ME_POSTGRES_PASSWORD
#   CHANGE_ME_MCP_ROUTER_AUTH_SECRET
#   CHANGE_ME_LLM_ROUTER_TOKEN
#   CHANGE_ME_AGENT_ROUTER_ADMIN_TOKEN
# AGENT_ROUTER_SRC is set to /share/Container/agent-router; create that folder in File Station first.
name: cubicecho-ai

services:
  mcp-router:
    image: vantreeseba/mcp-router:latest
    restart: unless-stopped
    depends_on:
      postgres: { condition: service_healthy }
    environment:
      DATABASE_URL: postgres://cubicecho:CHANGE_ME_POSTGRES_PASSWORD@postgres:5432/mcp_router
      BETTER_AUTH_SECRET: CHANGE_ME_MCP_ROUTER_AUTH_SECRET
      SECURE_LOCAL_NET: ""
      OIDC_ISSUER: ""   # or pair with heautou from the UI
      OIDC_CLIENT_ID: ""
      OIDC_CLIENT_SECRET: ""
      TRUST_PROXY: false
      MCP_ROUTER_TOKEN: ""   # upgrades only; imported as an API key
    ports:
      - "3200:3000"
    volumes:
      - mcp-router-data:/data   # installed servers and caches; runs as uid 1000

  llm-router:
    image: ghcr.io/cubicecho/llm-router:latest
    restart: unless-stopped
    environment:
      LLM_ROUTER_TOKEN: CHANGE_ME_LLM_ROUTER_TOKEN
      SECURE_LOCAL_NET: ""
    ports:
      - "3201:3000"
    volumes:
      - llm-router-data:/data
    extra_hosts:
      - "host.docker.internal:host-gateway"   # reach a model server on the host

  agent-router:
    # No published image yet: runs a checkout of the repo on Node 26.
    image: node:26-slim
    restart: unless-stopped
    depends_on:
      postgres: { condition: service_healthy }
    working_dir: /app
    command: sh -c "npm ci && npm start"
    environment:
      DATABASE_URL: postgres://cubicecho:CHANGE_ME_POSTGRES_PASSWORD@postgres:5432/agent_router
      # Agents name models as <provider>/<model>; "llm/ollama/qwen3:8b" goes to
      # llm-router, which passes "ollama/qwen3:8b" on to its own provider.
      AGENT_ROUTER_PROVIDERS: '[{"name":"llm","baseUrl":"http://llm-router:3000/v1","apiKey":"CHANGE_ME_LLM_ROUTER_TOKEN"}]'
      AGENT_ROUTER_ADMIN_TOKEN: CHANGE_ME_AGENT_ROUTER_ADMIN_TOKEN
      SECURE_LOCAL_NET: ""
      NODE_ENV: production
      PORT: 3100
    ports:
      - "3205:3100"
    volumes:
      - /share/Container/agent-router:/app

  min-agent:
    image: ghcr.io/cubicecho/min-agent:latest
    restart: unless-stopped
    depends_on:
      postgres: { condition: service_healthy }
    environment:
      DATABASE_URL: postgres://cubicecho:CHANGE_ME_POSTGRES_PASSWORD@postgres:5432/min_agent
      OPENAI_API_KEY: ""
      PORT: 8787
    ports:
      - "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: postgres://cubicecho:CHANGE_ME_POSTGRES_PASSWORD@postgres:5432/task_server
      OPENAI_API_KEY: ""
      TZ: UTC
    ports:
      - "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: CHANGE_ME_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 mcp_router;
      CREATE DATABASE agent_router;
      CREATE DATABASE min_agent;
      CREATE DATABASE task_server;

volumes:
  mcp-router-data:
  llm-router-data:
  task-server-data:
  postgres-data:
