跳到主要内容

Grok Bot 把编码交给本地 OpenCode,完成后再通知

XXAsher Suter2026-11-28

编程与仓库全文核对建议先读

概要

作者通过 Tailscale 把 Grok Bot 连到本地 OpenCode 模型,并用 webhook 插件在编码任务结束时回传通知。

核心流程

起因:让 Grok Bot 把编码任务交给本地模型,并在工作完成后收到一份 PR 和摘要。作者说,这个本地模型曾是一个从未做成真实工作的酷演示。已经在用 Grok Bot 的读者,可以跳到文末的提示词,让 bot 来完成设置。

自己做:先弄清这套组合要解决的问题。作者在 Mac Studio(M5 Max,64 GB)上运行 Inkling 2 Mini。它免费且私密,但和 Codex、Cursor 不在同一套驾具里,作者得盯着它。与此同时,把每项任务都交给 Cursor Cloud Agent,会消耗 Grok Bot 的周用量。现在由 Grok Bot 写出详细提示词,再由本地模型去做工作。

自己做:弄清各部分如何配合。一台电脑运行 OpenCode、Tailscale 和 Docker。模型就在那里编辑文件、跑测试,并推送到 GitHub。它可以是一台闲置电脑、一块树莓派,或一台云 VPS。OpenCode 跑在作者的 Ubuntu 电脑上,装在一个只能接触 ~/code 的 Docker 容器里。systemd 在开机时启动它。Tailscale 保证它是私有的。Grok Bot 通过 Tailscale 连接器到达它。Grok Bot 启动 OpenCode 会话并发送任务。opencode-webhook 是作者做的一个小插件,在任务结束时 ping 一条 Grok Bot routine。不必轮询。Docker 把模型锁进一个严格的容器,它只能看见被给予的文件夹,因此永远不会完全控制运行 OpenCode 的那台电脑。可选的 T3 Code 会加上一个网页界面,并让人更容易直接使用本地模型。每个 bot 都会给自己的会话打标签(例如 Grok Bot [Chief of Staff]: add tests),这样回 ping 会回到启动它的那一个。

  1. 自己做:配置模型。任何 OpenAI 兼容的服务器都可以(MLX、llama.cpp、vLLM、LM Studio)。必须已安装 Docker,并且用户在 docker 组里。创建 OpenCode 配置目录,打开配置文件,并粘贴这份配置:

    中文意思:创建 OpenCode 配置目录。

    mkdir -p ~/.config/opencode

    中文意思:用编辑器打开 OpenCode 配置文件。

    nano ~/.config/opencode/opencode.json

    中文意思:OpenCode 配置,把本地 OpenAI 兼容模型设为默认,并写上无人值守时需要的权限。

    { "$schema": "https://opencode.ai/config.json", "provider": { "local": { "npm": "@ai-sdk/openai-compatible", "name": "My Local Model", "options": { "baseURL": "https://your-model-server/v1", "apiKey": "{env:LOCAL_API_KEY}" }, "models": { "your-model-id": { "name": "Your Model" } } } }, "model": "local/your-model-id", "permission": { "external_directory": "allow", "doom_loop": "deny" } }

作者说 your-model-id 必须与服务器报告的 ID 一致。权限那一行很重要:当 bot 驱动 OpenCode 时,没有人在旁边点允许,没有它,会话可能永远停在忙碌状态。真正的边界是容器。

  1. 自己做:安装 OpenCode 插件。创建插件目录并下载 webhook-notify.js:

    中文意思:创建 OpenCode 插件目录。

    mkdir -p ~/.config/opencode/plugins

    中文意思:下载作者的 webhook-notify.js 插件。

    curl -fsSL https://raw.githubusercontent.com/araysuter/opencode-webhook/main/webhook-notify.js -o ~/.config/opencode/plugins/webhook-notify.js

这个插件让 OpenCode 在任务完成、失败、卡在提示、遇到模型错误,或停滞 10 分钟时 ping Grok Bot(OPENCODE_NOTIFY_STALL_MINUTES 可改这个时间)。

  1. 交给 bot:创建 Grok Bot routine。向一个 Grok Bot 要一条名为 OpenCode task finished 的 webhook routine。没有轮询:任务结束时,OpenCode 会打到这条 routine 的 URL。其他 bot 用自己的名字给会话命名,所以那个 bot 知道该把每次 ping 转给谁。保存它给出的 webhook URL 和密钥。
  2. 自己做:加入密钥。为 OpenCode 创建环境变量文件并在终端里打开,填入主人自己的密钥,然后把它锁起来,只让主人能读。LOCAL_API_KEY 这个名字必须与第 1 步 opencode.json 里的 {env:...} 名称一致。

    中文意思:用编辑器打开 OpenCode 环境变量文件。

    nano ~/.config/opencode/env

    中文意思:写入模型密钥、OpenCode 密码和 webhook 通知相关的环境变量示例。

    LOCAL_API_KEY=your-model-api-key OPENCODE_SERVER_PASSWORD=your-opencode-password OPENCODE_NOTIFY_WEBHOOK_URL=https://your-webhook-url OPENCODE_NOTIFY_SECRET=your-webhook-secret OPENCODE_NOTIFY_TITLE_PREFIX=Grok Bot

    中文意思:把环境变量文件权限设为仅主人可读。

    chmod 600 ~/.config/opencode/env

  3. 自己做:构建容器。创建 Docker 目录,打开 Dockerfile,粘贴镜像定义,并构建它:

    中文意思:创建 OpenCode 的 Docker 目录。

    mkdir -p ~/.config/opencode/docker

    中文意思:用编辑器打开 Dockerfile。

    nano ~/.config/opencode/docker/Dockerfile

    中文意思:用于运行 OpenCode 的 Debian 容器定义,安装必要工具并暴露 4096 端口。

    FROM debian:bookworm-slim ARG USER=opencode ARG UID=1000 ARG GID=1000 ARG HOME=/home/opencode ENV DEBIAN_FRONTEND=noninteractive RUN apt-get update && apt-get install -y --no-install-recommends \ ca-certificates curl git jq python3 python3-pip python3-venv \ build-essential pkg-config libssl-dev ripgrep unzip gnupg \ && curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg \ -o /usr/share/keyrings/githubcli-archive-keyring.gpg \ && chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg \ && echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" \

    /etc/apt/sources.list.d/github-cli.list \

    && apt-get update && apt-get install -y --no-install-recommends gh \ && rm -rf /var/lib/apt/lists/* RUN groupadd --gid "$GID" "$USER" \ && useradd --uid "$UID" --gid "$GID" --create-home --home-dir "$HOME" "$USER" USER $UID:$GID ENV HOME=$HOME \ PATH="$HOME/.opencode/bin:$HOME/.cargo/bin:$HOME/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" RUN curl -fsSL https://opencode.ai/install | bash WORKDIR $HOME EXPOSE 4096 CMD ["opencode", "web", "--hostname", "0.0.0.0", "--port", "4096"]

    中文意思:按当前用户身份构建名为 opencode-sandbox:local 的镜像。

    docker build --network=host --build-arg USER="$USER" --build-arg UID="$(id -u)" --build-arg GID="$(id -g)" --build-arg HOME="$HOME" -t opencode-sandbox:local ~/.config/opencode/docker

接下来是启动脚本。它只挂载项目文件夹和 OpenCode 自己的数据,绝不挂载家目录、SSH 密钥或 Docker socket。gh 和 .gitconfig 的挂载是可选的,能让它以主人的身份推送 PR。打开启动脚本,粘贴进去,设为可执行,然后重新加载用户 systemd:

中文意思:用编辑器打开容器启动脚本。

nano ~/.config/opencode/docker/run.sh

中文意思:启动脚本:只用给定的项目目录和 OpenCode 数据挂载容器,不挂载家目录、SSH 密钥或 Docker socket。

#!/bin/bash set -euo pipefail IMAGE="${OPENCODE_DOCKER_IMAGE:-opencode-sandbox:local}" exec /usr/bin/docker run --rm --name opencode \ --env-file "${HOME}/.config/opencode/env" \ --user "$(id -u):$(id -g)" \ --init \ -p 127.0.0.1:4096:4096 \ -v "${HOME}/code:${HOME}/code" \ -v "${HOME}/.t3/worktrees:${HOME}/.t3/worktrees" \ -v "${HOME}/.config/opencode:${HOME}/.config/opencode:ro" \ -v "${HOME}/.local/share/opencode:${HOME}/.local/share/opencode" \ -v "${HOME}/.local/state:${HOME}/.local/state" \ -v "${HOME}/.cache/opencode:${HOME}/.cache/opencode" \ -v "${HOME}/.config/gh:${HOME}/.config/gh:ro" \ -v "${HOME}/.gitconfig:${HOME}/.gitconfig:ro" \ -e "HOME=${HOME}" \ "${IMAGE}" \ opencode web --hostname 0.0.0.0 --port 4096

中文意思:把启动脚本设为可执行。

chmod 755 ~/.config/opencode/docker/run.sh

中文意思:重新加载用户级 systemd。

systemctl --user daemon-reload

  1. 自己做:用 systemd 让它在开机时启动。创建用户 systemd 目录,写入 OpenCode 服务,打开 lingering 以便注销后仍能运行,重新加载 systemd,启动服务,并用 Tailscale 发布它。用密码以 opencode 身份登录。随时可用 systemctl --user restart opencode 重启。

    中文意思:创建用户级 systemd 目录。

    mkdir -p ~/.config/systemd/user

    中文意思:用编辑器打开 OpenCode 的 systemd 服务文件。

    nano ~/.config/systemd/user/opencode.service

    中文意思:让 OpenCode 容器在开机时启动的 systemd 用户服务。

    [Unit] Description=OpenCode web (Docker sandbox) After=docker.socket network-online.target Wants=network-online.target

    [Service] Type=simple WorkingDirectory=%h ExecStartPre=-/usr/bin/docker rm -f opencode ExecStart=%h/.config/opencode/docker/run.sh ExecStop=/usr/bin/docker stop opencode Restart=on-failure RestartSec=5 TimeoutStopSec=30

    [Install] WantedBy=default.target

    中文意思:为当前用户打开 lingering,以便注销后服务仍能运行。

    sudo loginctl enable-linger $USER

    中文意思:再次重新加载用户级 systemd。

    systemctl --user daemon-reload

    中文意思:启用并立即启动 OpenCode 用户服务。

    systemctl --user enable --now opencode

    中文意思:用 Tailscale 把本机 4096 端口以 HTTPS 发布到 8443。

    sudo tailscale serve --bg --https=8443 http://127.0.0.1:4096

  2. 自己做:连接 Grok Bot。在 Grok Bot 里连接 Tailscale 连接器,并把 OpenCode 密码保存为密钥 OPENCODE_SERVER_PASSWORD。然后就可以提出请求,例如让本地模型为 my-repo 加上测试并打开一份 PR。作者说,做完后主人会收到回音。

自己做:也可以选择加上 T3 Code,用于浏览器或手机界面。T3 Code 为浏览器或手机加上一个界面。把它跑在主机上,不要放进容器。它需要 gh 2.81+。创建目录,写入 T3 服务,启动它,并用 Tailscale 发布。如果推送失败并提示 terminal prompts disabled,就运行 gh auth setup-git。

中文意思:创建 T3 Code 和 OpenCode 需要的本地目录。

mkdir -p ~/code ~/.t3/worktrees ~/.local/share/opencode ~/.local/state ~/.cache/opencode

中文意思:用编辑器打开 T3 Code 的 systemd 服务文件。

nano ~/.config/systemd/user/t3code.service

中文意思:在主机上运行 T3 Code 的 systemd 用户服务。

[Unit] Description=T3 Code After=network-online.target Wants=network-online.target

[Service] Type=simple WorkingDirectory=%h/code EnvironmentFile=-%h/.config/opencode/env Environment=PATH=%h/.local/bin:%h/.opencode/bin:%h/.cargo/bin:/usr/local/bin:/usr/bin:/bin ExecStart=%h/.local/bin/t3 serve Restart=on-failure RestartSec=5

[Install] WantedBy=default.target

中文意思:启用并立即启动 T3 Code 用户服务。

systemctl --user enable --now t3code

中文意思:用 Tailscale 把本机 3773 端口以 HTTPS 发布到 8444。

sudo tailscale serve --bg --https=8444 http://127.0.0.1:3773

中文意思:配置 gh,避免推送时出现终端无法提示的问题。

gh auth setup-git

交给 bot:或者跳过手动步骤,粘贴这段设置提示词。作者说,已经在用 Grok Bot 的读者可以跳到这段提示词,让 bot 来完成设置:

中文意思:请 Grok Bot 按这篇指南逐步完成设置,一次只给一条命令,并在完成后做一次 ping 测试。

I want my local model to do coding work that you can steer. Follow this guide: [https://x.com/araysuter/status/2126417376769929259]. My model runs on an OpenAI-compatible server, and I'll give you its URL, model ID, and API key when you ask. Walk me through each step, give me one command at a time, and wait for me to confirm before moving on.

  1. Configure OpenCode in ~/.config/opencode/opencode.json to use my model as the default.
  2. Install the plugin from https://github.com/araysuter/opencode-webhook so OpenCode pings you when a task finishes.
  3. Create a webhook routine called "OpenCode task finished." When another bot hands off a task, it names the session "Grok Bot [Bot Name]: …", and you forward the result to that bot. If no bot is named, tell me directly.
  4. Run OpenCode in a Docker container that can only see my ~/code folder. Start it at boot with a systemd user service, keep secrets in ~/.config/opencode/env (mode 600), and publish it with tailscale serve on port 8443.
  5. Connect the Tailscale connector so you can reach my computer, and save my OpenCode password as the secret OPENCODE_SERVER_PASSWORD.

Then test it. Create a session titled "Grok Bot: ping test", send "reply with pong" via prompt_async, and confirm both the reply and the done-ping arrive. Once it works, save how to hand off coding tasks so you remember next time. Never show my password, API key, or webhook secret in chat.

怎么检查:作者报告了一手结果:本地模型终于能做真实工作,Grok Bot 现在会把编码任务交给它,并在完成后发回一份 PR 和摘要(作者自述,未复现)。这个插件是开源的,可用于任何 webhook:https://github.com/araysuter/opencode-webhook

核心观点和结果

  • Grok Bot 写出详细提示词,本地模型在主人控制的机器上做工作。作者此前要盯着与 Codex、Cursor 不在同一套驾具里的 Inkling 2 Mini,而把每项任务都交给 Cursor Cloud Agent,又会消耗 Grok Bot 的周用量。新的路径是:Grok Bot 启动一次 OpenCode 会话,自定义插件在任务结束时 ping 一条 routine,主人会收到一份 PR 和摘要(作者自述,未复现)。
  • 真正的安全边界是容器。启动脚本只挂载项目文件夹和 OpenCode 自己的数据,绝不挂载家目录、SSH 密钥或 Docker socket。opencode.json 里的权限行很重要,因为当 bot 驱动 OpenCode 时,没有人在旁边点允许。
  • 用 webhook routine 代替轮询。向一个 Grok Bot 要一条名为 OpenCode task finished 的 routine,再把 webhook URL 和密钥放进环境变量文件。每个 bot 都会给自己的会话打标签,这样回 ping 会回到启动它的那一个。
  • 已经在用 Grok Bot 的读者,可以粘贴文末的设置提示词,让 bot 带着走完这些步骤。插件在 https://github.com/araysuter/opencode-webhook 开源,可用于任何 webhook。

来源

平台
X
原作者
Asher Suter
原文日期
分类
编程与仓库

这条证据标签是什么意思

全文核对

来源已经读过(帖子、文章或字幕)。这只说明原文被读过,不代表本站复现了作者的结果。

阅读编辑与证据规范

同场景的其他案例

非官方社区项目,与 xAI 没有隶属关系,也未获 xAI 认可或背书。没有注册。做法和数字属于原作者。