# 例如 Atlas A3 镜像;Atlas A2 镜像(无 -a3)
FROM quay.io/ascend/vllm-ascend:v0.30.0-a3

USER root

RUN chmod -R a+rX "/vllm-workspace/vllm-ascend"

# 替换APT源为华为云
RUN echo "配置华为云APT源..." && \
cp /etc/apt/sources.list /etc/apt/sources.list.bak && \
sed -i 's@http://.*archive.ubuntu.com@http://repo.huaweicloud.com@g' /etc/apt/sources.list && \
sed -i 's@http://.*security.ubuntu.com@http://repo.huaweicloud.com@g' /etc/apt/sources.list && \
echo "华为云APT源配置完成"

# 安装基础系统工具
RUN apt-get update && \
    apt-get install -y \
    sudo \
    zip \
    unzip \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

ARG TARGETARCH
RUN echo "Building for architecture: $TARGETARCH" && \
    case "$TARGETARCH" in \
      amd64|x86_64) TTYD_URL="https://github.com/tsl0922/ttyd/releases/latest/download/ttyd.x86_64" ;; \
      arm64|aarch64) TTYD_URL="https://github.com/tsl0922/ttyd/releases/latest/download/ttyd.aarch64" ;; \
      *) echo "Unsupported arch: $TARGETARCH"; exit 1 ;; \
    esac \
    && curl -fsSL "$TTYD_URL" -o /usr/local/bin/ttyd \
    && chmod +x /usr/local/bin/ttyd

WORKDIR /repositories
ARG CACHEBUST=1
# 凭据用 BuildKit secret 挂载,不写进镜像层(勿再用 COPY git-credentials)
# 子模块多为 git@gitcode.com:...,无 SSH;改为 HTTPS 后由 credential.helper 读挂载的凭据
RUN --mount=type=secret,id=git_credentials,target=/tmp/.git-credentials,required \
    # 在 RUN 中引用该参数,让 Docker 认为该层依赖 CACHEBUST
    test -n "${CACHEBUST}" \
    && set -eux \
    && cp /tmp/.git-credentials /root/.git-credentials \
    && git config --global credential.helper store \
    && git config --global url."https://gitcode.com/".insteadOf "git@gitcode.com:" \
    && export GIT_TERMINAL_PROMPT=0 \
    && git clone --depth 1 --recurse-submodules https://gitcode.com/gmq123/mofix-new.git \
    && git clone --depth 1 --recurse-submodules https://gitcode.com/MoFixGo/adapt-agent.git \
    && git clone --depth 1 --recurse-submodules https://gitcode.com/MoFixGo/deploy-agent.git \
    && git clone --depth 1 --recurse-submodules https://gitcode.com/MoFixGo/doc-agent.git \
    && git clone --depth 1 --recurse-submodules https://gitcode.com/MoFixGo/optimization-agent.git \
    && git clone --depth 1 --recurse-submodules https://gitcode.com/MoFixGo/quantify-agent.git \
    && git clone --depth 1 --recurse-submodules https://gitcode.com/MoFixGo/search-agent.git \
    && git clone --depth 1 --recurse-submodules https://gitcode.com/gmq123/model-agent-eval-core.git \
    && git clone --depth 1 --recurse-submodules https://gitcode.com/MoFixGo/verify-agent.git \
    && git clone --depth 1 --recurse-submodules -b v2.0.0 --single-branch https://gitcode.com/LucciC/pta-helper.git \
    && git clone https://gitee.com/lordstar-habile/huaweicloud-lts-python-sdk.git \
    && git clone --depth 1 --recurse-submodules https://gitcode.com/cann/cannbot-skills.git \
    && git clone --depth 1 --recurse-submodules https://gitcode.com/Ascend/msagent.git \
    && for repo in mofix-new model-agent-eval-core adapt-agent deploy-agent doc-agent optimization-agent quantify-agent search-agent verify-agent pta-helper huaweicloud-lts-python-sdk cannbot-skills msagent; do \
       printf '%s %s\n' "$repo" "$(git -C "$repo" rev-parse HEAD)"; \
       done | sort > /repositories/dependency-revisions.txt \
    && git config --global --unset credential.helper \
    && git config --global --unset url."https://gitcode.com/".insteadOf \
    && rm -rf mofix-new/.git adapt-agent/.git deploy-agent/.git doc-agent/.git optimization-agent/.git quantify-agent/.git search-agent/.git model-agent-eval-core/.git verify-agent/.git \
    && rm -rf /root/.git-credentials

# 更换清华源
RUN mkdir -p /root/.config/pip \
    && echo "[global]" > /root/.config/pip/pip.conf && \
    echo "index-url = https://pypi.tuna.tsinghua.edu.cn/simple" >> /root/.config/pip/pip.conf && \
    echo "extra-index-url = https://repo.huaweicloud.com/repository/pypi/simple" >> /root/.config/pip/pip.conf && \
    echo "trusted-host = pypi.tuna.tsinghua.edu.cn repo.huaweicloud.com" >> /root/.config/pip/pip.conf && \
    echo "timeout = 120" >> /root/.config/pip/pip.conf && \
    echo "retries = 5" >> /root/.config/pip/pip.conf

RUN pip install ml-dtypes tornado \
    && pip install --no-cache-dir -r mofix-new/requirements.txt

# 安装pta-helper依赖知识库
RUN python pta-helper/pta_helper.py --setup

# 安装 claude-agent-sdk(指定版本确保兼容性)
RUN pip install --no-cache-dir claude-agent-sdk==0.2.110

# 安装 Node.js 20.x(Claude Code CLI 需要)
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \
    && apt-get install -y nodejs \
    && rm -rf /var/lib/apt/lists/*

# 安装 Claude Code CLI + Agent SDK
RUN npm install -g @anthropic-ai/claude-code @anthropic-ai/claude-agent-sdk@0.3.177 \
    --registry=https://mirrors.huaweicloud.com/repository/npm/ \
    --timeout=300000 \
    --fetch-retries=3 \
    --fetch-retry-mintimeout=20000 \
    --fetch-retry-maxtimeout=120000 \
    || npm install -g @anthropic-ai/claude-code @anthropic-ai/claude-agent-sdk@0.3.177 \
        --registry=https://registry.npmmirror.com \
        --timeout=300000 --fetch-retries=3

# 安装 atomcode 命令行工具
RUN curl -fsSL https://raw.atomgit.com/atomgit_atomcode/atomcode/raw/main/scripts/install.sh | sh

# 安装插件
RUN atomcode plugin marketplace add https://gitcode.com/gmq123/ascend-model-agent-plugin
RUN atomcode plugin install ascend-model-agent-plugin@ascend-model-agent-plugin
RUN mv /root/.atomcode /workspace

RUN echo "创建 atomgit 用户与用户组..." && \
    groupadd -g 1000 -r atomgitgroup && \
    useradd -u 1000 -m -g atomgitgroup -s /bin/bash atomgit && \
    usermod -aG sudo atomgit && \
    echo "atomgit ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/atomgit && \
    chmod 0440 /etc/sudoers.d/atomgit && \
    mkdir -p /opt/atomgit && \
    chown -R atomgit:atomgitgroup /opt/atomgit && \
    echo "✅ atomgit 用户已创建并授予 sudo 权限"

RUN mkdir -p /workspace \
    && cd /repositories && zip -qr /workspace/mofix-new.zip . \
        && mv mofix-new/skills /workspace/ \
    && chown -R atomgit:atomgitgroup /workspace \
    && rm -rf /repositories

# ⭐ 切换到 atomgit 用户
USER atomgit

# 直接使用 `docker exec -it <container> /bin/bash` 时也初始化交互终端。
# Ctrl-S 在 Vim 中保存,Ctrl-Z 不再误暂停编辑器;仅影响 atomgit 的交互 Shell。
RUN printf '%s\n' \
        '' \
        '# MoFix managed interactive shell defaults.' \
        'case "$-" in' \
        '  *i*) stty sane 2>/dev/null || true; stty -ixon -tostop 2>/dev/null || true ;;' \
        'esac' \
        >> /home/atomgit/.bashrc \
    && printf '%s\n' \
        '" MoFix managed Vim defaults for container editing.' \
        'nnoremap <silent> <C-S> :write<CR>' \
        'inoremap <silent> <C-S> <C-O>:write<CR>' \
        'vnoremap <silent> <C-S> <Esc>:write<CR>gv' \
        'nnoremap <C-Z> <Nop>' \
        'inoremap <C-Z> <Nop>' \
        'vnoremap <C-Z> <Nop>' \
        >> /home/atomgit/.vimrc

# 在atomgit 用户下创建
# 创建必要的目录
RUN mkdir -p ~/.claude

# Claude 配置文件由部署时动态写入,配置来源
# 以下为参考示例(可按需要在此处修改 token)
#RUN echo '{                                                    \
#    "env": {                                                   \
#        "ANTHROPIC_BASE_URL": "https://api.kimi.com/coding/",  \
#        "ANTHROPIC_AUTH_TOKEN":"sk-kimi-xxxxxxxxxxxx",         \
#        "ANTHROPIC_MODEL": "kimi-xx",                          \
#        "ANTHROPIC_SMALL_FAST_MODEL": "kimi-xx",               \
#        "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1",       \
#        "API_TIMEOUT_MS": "600000"                             \
#    }                                                          \
#}' > ~/.claude/settings.json

# 将skills移到claude对应目录下
RUN mv /workspace/skills ~/.claude/

# 创建atomcode配置目录
RUN mkdir -p ~/.atomcode/

# 创建配置
ADD docker/config.toml /opt/atomgit/.atomcode/
ADD docker/config.toml /home/atomgit/.atomcode/
ADD docker/config.toml /opt/atomgit/conf/


# 创建工作目录
WORKDIR /opt/atomgit

# 主镜像未在本文件配置 MoFix 自动启动;平台可单独指定启动命令。
# AtomGit 接入端口;EXPOSE 只声明端口。
EXPOSE 8501