FROM node:22-alpine AS opencode-build
RUN apk add --no-cache curl && \
    curl -fsSL https://opencode.ai/install | sh

FROM python:3.12-slim
RUN apt-get update && apt-get install -y --no-install-recommends curl && \
    rm -rf /var/lib/apt/lists/*
RUN pip install --no-cache-dir uv
WORKDIR /app
COPY requirements.txt .
RUN uv pip install --system --no-cache-dir -r requirements.txt
COPY --from=opencode-build /usr/local/bin/opencode /usr/local/bin/opencode
COPY . .
EXPOSE 8000
ENV OPENCODE_SERVE_URL=http://127.0.0.1:4096
ENV AGENT_API_KEY=dev-agent-key-change-in-production
RUN mkdir -p /root/.config && \
    ln -s /app/opencode /root/.config/opencode && \
    printf '#!/bin/sh\nopencode serve --host 127.0.0.1 --port 4096 &\nsleep 1\nexec uvicorn app.main:app --host 0.0.0.0 --port 8000\n' > /entrypoint.sh && chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
