From be1de5ecfb2eb2a9bcecd5555d5412e9a828fc08 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 14 Aug 2026 18:18:09 +0000 Subject: [PATCH 1/2] Document colocated localhost Grok Bot bridge setup. Describe running the Node bridge on the Grok Bot computer (127.0.0.1:1340/8788) as a first-class option, and keep the split-machine Tailscale/tunnel path for when the bridge is elsewhere. Co-authored-by: Fly Over Coder --- .env.example | 13 ++++--- README.md | 65 +++++++++++++++++++++++++++++---- docs/PHASE2-GROK-BOT-BACKEND.md | 57 ++++++++++++++++++++++++----- docs/PLAN.md | 8 ++-- 4 files changed, 118 insertions(+), 25 deletions(-) diff --git a/.env.example b/.env.example index 9b30c3c..fafed63 100644 --- a/.env.example +++ b/.env.example @@ -21,20 +21,23 @@ API_KEY= MODEL=grok-2-latest # Grok Bot computer gateway (required when AGENT_BACKEND=grok-bot). -# Private LAN / Tailscale / SSH tunnel only — never expose :1340 to the internet. +# Colocated (same computer): http://127.0.0.1:1340 +# Split-machine: private LAN / Tailscale / SSH tunnel only — never expose :1340 to the internet. # GROK_BOT_TOKEN is the bearer from that computer's gateway.json (treat like a password). -# GROK_BOT_GATEWAY_URL=http://grok-bot-host:1340 +# GROK_BOT_GATEWAY_URL=http://127.0.0.1:1340 # GROK_BOT_TOKEN= # GROK_BOT_AGENT_ID= # GROK_BOT_TIMEOUT_MS=120000 # Bridge callback the Grok Bot agent POSTs to (required when AGENT_BACKEND=grok-bot). -# BRIDGE_PUBLIC_URL must be reachable from the Grok Bot computer (not localhost on that machine). +# BRIDGE_PUBLIC_URL must be reachable from the Grok Bot computer. +# Colocated: http://127.0.0.1:8788 with BRIDGE_HTTP_HOST=127.0.0.1 (localhost only). +# Split-machine: when the bridge runs elsewhere, localhost on the Grok Bot computer is not the bridge. # BRIDGE_REPLY_TOKEN is a shared secret you create — treat like a password; do not log it. -# BRIDGE_HTTP_HOST=0.0.0.0 +# BRIDGE_HTTP_HOST=127.0.0.1 # Compose publishes host:container as ${BRIDGE_HTTP_PORT:-8788}:${BRIDGE_HTTP_PORT:-8788} # BRIDGE_HTTP_PORT=8788 -# BRIDGE_PUBLIC_URL=http://bridge-host:8788 +# BRIDGE_PUBLIC_URL=http://127.0.0.1:8788 # BRIDGE_REPLY_TOKEN= # Neutral default; see examples/team-assistant and examples/gaming-helper diff --git a/README.md b/README.md index df88736..03af37c 100644 --- a/README.md +++ b/README.md @@ -86,13 +86,13 @@ docker compose up --build | `API_BASE_URL` | if openai | e.g. `https://api.x.ai/v1` or `https://api.openai.com/v1` | | `API_KEY` | if openai | Provider API key | | `MODEL` | if openai | Model id | -| `GROK_BOT_GATEWAY_URL` | if grok-bot | Private computer gateway base, e.g. `http://host:1340` | +| `GROK_BOT_GATEWAY_URL` | if grok-bot | Private computer gateway base. Colocated: `http://127.0.0.1:1340`. Split-machine: a private host URL. | | `GROK_BOT_TOKEN` | if grok-bot | Bearer token from that computer's `gateway.json` (password) | -| `GROK_BOT_AGENT_ID` | if grok-bot | Target agent id (from env, not hardcoded) | +| `GROK_BOT_AGENT_ID` | if grok-bot | Target agent id from `POST /api/listAgents` (from env, not hardcoded) | | `GROK_BOT_TIMEOUT_MS` | no | Default `120000` — how long `generate()` waits for `/internal/reply` | -| `BRIDGE_HTTP_HOST` | no | Default `0.0.0.0` — listen address for the callback server | +| `BRIDGE_HTTP_HOST` | no | Default `0.0.0.0`. Colocated: bind `127.0.0.1` so the callback is localhost-only. | | `BRIDGE_HTTP_PORT` | no | Default `8788`. Compose maps host and container to this port (`${BRIDGE_HTTP_PORT:-8788}:${BRIDGE_HTTP_PORT:-8788}`) | -| `BRIDGE_PUBLIC_URL` | if grok-bot | URL the **Grok Bot computer** uses to reach this bridge (not `localhost` on that machine) | +| `BRIDGE_PUBLIC_URL` | if grok-bot | URL the **Grok Bot computer** uses to reach this bridge. Colocated: `http://127.0.0.1:8788`. When the bridge runs on a different machine, that machine's `localhost` is not the bridge. | | `BRIDGE_REPLY_TOKEN` | if grok-bot | Shared secret for `Authorization: Bearer` on `POST /internal/reply` (password) | | `SYSTEM_PROMPT_FILE` | no | Default `SOUL.md` (neutral); swap for your agent persona | | `TEMPERATURE` | no | Default `0.7` | @@ -118,12 +118,62 @@ There is no official Discord plugin. The working community path is: 3. The Grok Bot agent `POST`s `{ correlationId, text }` back to the bridge. 4. The bridge posts `text` to the matching Discord channel/thread (same ~1900 split as the OpenAI path). -`BRIDGE_PUBLIC_URL` must be reachable **from the Grok Bot computer**. The bridge often runs in Docker on a different machine than the Grok Bot computer, so `localhost` on the Grok Bot side is not the bridge. Use Tailscale, an SSH tunnel, or another private path. +`BRIDGE_PUBLIC_URL` must be reachable **from the Grok Bot computer**. -**Do not** expose the Grok Bot gateway port (`:1340`) to the internet. **Do not** put `DISCORD_BOT_TOKEN` on the Grok Bot side. Treat `GROK_BOT_TOKEN` and `BRIDGE_REPLY_TOKEN` like passwords; never commit or log them. +- **Colocated (works today):** run this Node process on the Grok Bot computer and use `http://127.0.0.1:8788`. See [Run the bridge on the Grok Bot computer](#run-the-bridge-on-the-grok-bot-computer). +- **Split-machine:** when the bridge runs on a different machine (for example Docker on a laptop), `localhost` on the Grok Bot computer is not the bridge. Use Tailscale, an SSH tunnel, or another private path. + +**Do not** expose the Grok Bot gateway port (`:1340`) or the callback port (`:8788`) to the internet. Keep `DISCORD_BOT_TOKEN` in the bridge's local `.env` only — never in the Grok Bot agent, `gateway.json`, or git. Treat `GROK_BOT_TOKEN` and `BRIDGE_REPLY_TOKEN` like passwords; never commit or log them. This callback is unofficial and may break. See [docs/PHASE2-GROK-BOT-BACKEND.md](docs/PHASE2-GROK-BOT-BACKEND.md). +## Run the bridge on the Grok Bot computer + +The setup that works today without Tailscale or a tunnel: run **discord-agent-bridge on the same computer** as the Grok Bot agent so both the gateway (`:1340`) and the callback (`:8788`) stay on localhost. + +**Prerequisites** + +- Node.js **20+** on the Grok Bot computer +- Discord bot token in a local `.env` only (never commit it) +- Grok Bot computer gateway listening on `127.0.0.1:1340` + +**Configure** + +```bash +cp .env.example .env +``` + +Set `AGENT_BACKEND=grok-bot` and these localhost URLs (do not publish them): + +``` +GROK_BOT_GATEWAY_URL=http://127.0.0.1:1340 +BRIDGE_PUBLIC_URL=http://127.0.0.1:8788 +BRIDGE_HTTP_HOST=127.0.0.1 +``` + +Then fill in: + +- `GROK_BOT_TOKEN` — bearer from that computer's `gateway.json` (treat like a password). Grok Bot computers commonly store this at `/home/box/sand-data/gateway.json`; use whatever path that computer actually uses. +- `GROK_BOT_AGENT_ID` — from `POST /api/listAgents` on the gateway. Do not hardcode a real id in this repo. +- `BRIDGE_REPLY_TOKEN` — generate a random secret (`openssl rand -hex 32` or equivalent). Do not commit it. +- Discord allowlists (`DISCORD_ALLOWED_USERS` / roles / guilds, or DMs). Keep the Discord application **private**. + +**Run** + +```bash +npm ci +npm run build +npm start +``` + +A Grok Bot computer **Update** keeps files on disk but stops running processes. A simple restart wrapper (or any supervisor) is useful so the bridge comes back after an update, for example: + +```bash +while true; do npm start; echo "bridge exited; restarting in 5s"; sleep 5; done +``` + +In this mode, **never** expose `:1340` or `:8788` beyond localhost. Full topology notes (colocated vs split-machine + Tailscale/tunnel) are in [docs/PHASE2-GROK-BOT-BACKEND.md](docs/PHASE2-GROK-BOT-BACKEND.md). + ## Scripts | Script | Action | @@ -135,9 +185,10 @@ This callback is unofficial and may break. See [docs/PHASE2-GROK-BOT-BACKEND.md] ## Security -- **Never commit** `.env`, tokens, or API keys. `.gitignore` excludes `.env`. +- **Never commit** `.env`, tokens, or API keys. `.gitignore` excludes `.env` and `*.log`. - Treat `GROK_BOT_TOKEN` and `BRIDGE_REPLY_TOKEN` like passwords. Do not log them. - `POST /internal/reply` is authenticated and fail-closed (missing/wrong token → 401). It is not a public webhook. +- In colocated mode, bind the callback to `127.0.0.1` and do not expose `:1340` or `:8788` beyond localhost. - Keep the Discord application **private** until you trust your allowlists. - Prefer a private server (or DM-only personal use) for the first run. - The bot ignores other bots and itself; replies are split near 1900 characters. diff --git a/docs/PHASE2-GROK-BOT-BACKEND.md b/docs/PHASE2-GROK-BOT-BACKEND.md index ed519cb..031413c 100644 --- a/docs/PHASE2-GROK-BOT-BACKEND.md +++ b/docs/PHASE2-GROK-BOT-BACKEND.md @@ -63,26 +63,65 @@ flowchart LR This is **not** an unauthenticated public webhook. Do not log `BRIDGE_REPLY_TOKEN` or `GROK_BOT_TOKEN`. -`replyUrl` given to the agent is `BRIDGE_PUBLIC_URL + /internal/reply`. The Grok Bot computer is a **different machine** than the Docker host running the bridge, so `localhost` on the Grok Bot computer is not the bridge. Reachability options: Tailscale, SSH tunnel, or another private path. +`replyUrl` given to the agent is `BRIDGE_PUBLIC_URL + /internal/reply`. That URL must be reachable from the Grok Bot computer. Two topologies: -**Never** expose the Grok Bot gateway port (`:1340`) to the internet. **Never** put `DISCORD_BOT_TOKEN` on the Grok Bot side. +| Topology | Where the bridge runs | Typical URLs | Network | +|----------|----------------------|--------------|---------| +| **Colocated (works today)** | Same computer as the Grok Bot agent | `GROK_BOT_GATEWAY_URL=http://127.0.0.1:1340`, `BRIDGE_PUBLIC_URL=http://127.0.0.1:8788`, `BRIDGE_HTTP_HOST=127.0.0.1` | No Tailscale. Bind the callback to localhost only. | +| **Split-machine** | A different machine (for example Docker on a laptop) | Gateway URL is the Grok Bot host; `BRIDGE_PUBLIC_URL` is the bridge host | `localhost` on the Grok Bot computer is **not** the bridge. Use Tailscale, an SSH tunnel, or another private path. | + +**Never** expose the Grok Bot gateway port (`:1340`) or the callback port (`:8788`) to the internet. Keep `DISCORD_BOT_TOKEN` in the bridge's local `.env` only — never in the Grok Bot agent, `gateway.json`, or git. ## Env (grok-bot mode) | Variable | Required | Purpose | |----------|----------|---------| | `AGENT_BACKEND=grok-bot` | yes (to select) | Select this backend | -| `GROK_BOT_GATEWAY_URL` | yes | e.g. `http://host:1340` | -| `GROK_BOT_TOKEN` | yes | Bearer from `gateway.json` | -| `GROK_BOT_AGENT_ID` | yes | Target agent id (from env; do not hardcode a single id) | +| `GROK_BOT_GATEWAY_URL` | yes | Colocated: `http://127.0.0.1:1340`. Split-machine: private host URL. | +| `GROK_BOT_TOKEN` | yes | Bearer from that computer's `gateway.json` (password) | +| `GROK_BOT_AGENT_ID` | yes | Target agent id from `POST /api/listAgents` (from env; do not hardcode a real id) | | `GROK_BOT_TIMEOUT_MS` | no | Default `120000` | -| `BRIDGE_HTTP_HOST` | no | Default `0.0.0.0` | +| `BRIDGE_HTTP_HOST` | no | Default `0.0.0.0`. Colocated: `127.0.0.1` | | `BRIDGE_HTTP_PORT` | no | Default `8788` (Compose maps `${BRIDGE_HTTP_PORT:-8788}:${BRIDGE_HTTP_PORT:-8788}`) | -| `BRIDGE_PUBLIC_URL` | yes | URL the Grok Bot computer uses to reach the bridge | -| `BRIDGE_REPLY_TOKEN` | yes | Shared secret for the callback | +| `BRIDGE_PUBLIC_URL` | yes | URL the Grok Bot computer uses to reach the bridge. Colocated: `http://127.0.0.1:8788` | +| `BRIDGE_REPLY_TOKEN` | yes | Shared secret for the callback (generate locally; do not commit) | OpenAI-compatible remains the default. grok-bot fails closed if required vars are missing. +### Colocated setup (same computer) + +This is the path that works today without Tailscale or a tunnel. + +**Prerequisites:** Node.js 20+ on the Grok Bot computer; Discord bot token in a local `.env` only; gateway on `127.0.0.1:1340`. + +1. Copy `.env.example` → `.env` on the Grok Bot computer. Set `AGENT_BACKEND=grok-bot` and: + + ``` + GROK_BOT_GATEWAY_URL=http://127.0.0.1:1340 + BRIDGE_PUBLIC_URL=http://127.0.0.1:8788 + BRIDGE_HTTP_HOST=127.0.0.1 + ``` + +2. `GROK_BOT_TOKEN` — bearer from that computer's `gateway.json`. Grok Bot computers commonly store this at `/home/box/sand-data/gateway.json`; use the path that computer actually uses. Treat the token like a password. + +3. `GROK_BOT_AGENT_ID` — from `POST /api/listAgents` on the gateway (`Authorization: Bearer `). Pick the target agent id from the response. Do not hardcode a real id in this repo. + +4. Generate `BRIDGE_REPLY_TOKEN` (`openssl rand -hex 32` or equivalent). Do not commit it. + +5. Set Discord allowlists. Keep the Discord application **private**. + +6. `npm ci && npm run build && npm start` (or a simple supervisor). A Grok Bot computer **Update** keeps files on disk but stops running processes, so a restart wrapper is useful: + + ```bash + while true; do npm start; echo "bridge exited; restarting in 5s"; sleep 5; done + ``` + +In this mode, **never** expose `:1340` or `:8788` beyond localhost. + +### Split-machine setup + +When the bridge runs on a different machine, `localhost` on the Grok Bot computer is not the bridge. Point `GROK_BOT_GATEWAY_URL` at a private path to `:1340` (Tailscale, SSH tunnel, or LAN). Set `BRIDGE_PUBLIC_URL` to a URL the Grok Bot computer can reach (not `http://127.0.0.1:8788` on that computer unless you have tunneled it there). Default `BRIDGE_HTTP_HOST=0.0.0.0` is typical so the callback listens on the private interface. Still never publish `:1340` or `:8788` to the internet. + ## Prompt block (what the agent must parse) The bridge embeds a JSON block between `---BEGIN DISCORD_BRIDGE_REPLY---` / `---END DISCORD_BRIDGE_REPLY---` containing: @@ -106,7 +145,7 @@ A documented Discord/Channels inbound API would replace this callback. Until the ## Cutover / smoke -1. Set grok-bot env vars; keep `AGENT_BACKEND=openai` as fallback. +1. Set grok-bot env vars for the topology you are using (colocated localhost, or split-machine + Tailscale/tunnel); keep `AGENT_BACKEND=openai` as fallback. 2. Confirm `BRIDGE_PUBLIC_URL` is reachable from the Grok Bot computer (`POST /internal/reply` with a wrong token should 401; do not test with real tokens in logs). 3. Mention the bot in Discord → gateway `sendPrompt` → agent callback → Discord reply. 4. If the unofficial gateway changes, park grok-bot mode and stay on OpenAI. diff --git a/docs/PLAN.md b/docs/PLAN.md index 1ab9f3e..27a3fa9 100644 --- a/docs/PLAN.md +++ b/docs/PLAN.md @@ -80,13 +80,13 @@ flowchart LR | `DISCORD_REQUIRE_MENTION` | no | Default `true` outside free-response channels (DMs never require mention) | | `DISCORD_HOME_CHANNEL` | no | Optional home channel ID (logged / future routing) | | `AGENT_BACKEND` | no | `openai` (default) \| `grok-bot` (unofficial gateway + callback) | -| `GROK_BOT_GATEWAY_URL` | if grok-bot | Private computer gateway base (do not expose `:1340` publicly) | +| `GROK_BOT_GATEWAY_URL` | if grok-bot | Private computer gateway base (do not expose `:1340` publicly). Colocated: `http://127.0.0.1:1340` | | `GROK_BOT_TOKEN` | if grok-bot | Bearer from that computer's `gateway.json` | -| `GROK_BOT_AGENT_ID` | if grok-bot | Target agent id | +| `GROK_BOT_AGENT_ID` | if grok-bot | Target agent id from `POST /api/listAgents` | | `GROK_BOT_TIMEOUT_MS` | no | Default `120000` | -| `BRIDGE_HTTP_HOST` | no | Default `0.0.0.0` | +| `BRIDGE_HTTP_HOST` | no | Default `0.0.0.0`. Colocated: `127.0.0.1` | | `BRIDGE_HTTP_PORT` | no | Default `8788` | -| `BRIDGE_PUBLIC_URL` | if grok-bot | Reachable from the Grok Bot computer | +| `BRIDGE_PUBLIC_URL` | if grok-bot | Reachable from the Grok Bot computer. Colocated: `http://127.0.0.1:8788`. When the bridge runs on a different machine, that machine's `localhost` is not the bridge. | | `BRIDGE_REPLY_TOKEN` | if grok-bot | Bearer for `POST /internal/reply` | | `API_BASE_URL` | if openai | e.g. `https://api.x.ai/v1` | | `API_KEY` | if openai | Provider API key | From 92c264a518c52f85c0d011c1c4eeab8b7994913a Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 14 Aug 2026 18:22:32 +0000 Subject: [PATCH 2/2] Clarify colocated localhost is host-process-only. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Exclude Docker Compose from the 127.0.0.1 topology, and document that the while-loop only restarts a crashed child — it does not survive a Grok Bot computer Update. Co-authored-by: Fly Over Coder --- .env.example | 4 ++-- README.md | 26 +++++++++++++++++--------- docs/PHASE2-GROK-BOT-BACKEND.md | 26 ++++++++++++-------------- docs/PLAN.md | 6 +++--- 4 files changed, 34 insertions(+), 28 deletions(-) diff --git a/.env.example b/.env.example index fafed63..5e8e264 100644 --- a/.env.example +++ b/.env.example @@ -21,7 +21,7 @@ API_KEY= MODEL=grok-2-latest # Grok Bot computer gateway (required when AGENT_BACKEND=grok-bot). -# Colocated (same computer): http://127.0.0.1:1340 +# Colocated host process (npm start on the Grok Bot computer, not Docker): http://127.0.0.1:1340 # Split-machine: private LAN / Tailscale / SSH tunnel only — never expose :1340 to the internet. # GROK_BOT_TOKEN is the bearer from that computer's gateway.json (treat like a password). # GROK_BOT_GATEWAY_URL=http://127.0.0.1:1340 @@ -31,7 +31,7 @@ MODEL=grok-2-latest # Bridge callback the Grok Bot agent POSTs to (required when AGENT_BACKEND=grok-bot). # BRIDGE_PUBLIC_URL must be reachable from the Grok Bot computer. -# Colocated: http://127.0.0.1:8788 with BRIDGE_HTTP_HOST=127.0.0.1 (localhost only). +# Colocated host process: http://127.0.0.1:8788 with BRIDGE_HTTP_HOST=127.0.0.1 (host loopback only; not Docker). # Split-machine: when the bridge runs elsewhere, localhost on the Grok Bot computer is not the bridge. # BRIDGE_REPLY_TOKEN is a shared secret you create — treat like a password; do not log it. # BRIDGE_HTTP_HOST=127.0.0.1 diff --git a/README.md b/README.md index 03af37c..de64e96 100644 --- a/README.md +++ b/README.md @@ -61,13 +61,15 @@ Optional personas (copy or point `SYSTEM_PROMPT_FILE` at them): - [`examples/team-assistant/SOUL.md`](examples/team-assistant/SOUL.md) — small-team Discord assistant - [`examples/gaming-helper/SOUL.md`](examples/gaming-helper/SOUL.md) — domain example only -Docker: +Docker (OpenAI-compatible path, or a **split-machine** grok-bot bridge — not the colocated localhost topology): ```bash cp .env.example .env # fill in secrets docker compose up --build ``` +`docker compose` + `127.0.0.1` does **not** work for grok-bot on the Grok Bot computer. Inside the container, `127.0.0.1:1340` is the container, not the host gateway, and `BRIDGE_HTTP_HOST=127.0.0.1` makes Compose's published callback port unreachable. See [Run the bridge on the Grok Bot computer](#run-the-bridge-on-the-grok-bot-computer). + ## Environment variables | Variable | Required | Description | @@ -86,13 +88,13 @@ docker compose up --build | `API_BASE_URL` | if openai | e.g. `https://api.x.ai/v1` or `https://api.openai.com/v1` | | `API_KEY` | if openai | Provider API key | | `MODEL` | if openai | Model id | -| `GROK_BOT_GATEWAY_URL` | if grok-bot | Private computer gateway base. Colocated: `http://127.0.0.1:1340`. Split-machine: a private host URL. | +| `GROK_BOT_GATEWAY_URL` | if grok-bot | Private computer gateway base. Colocated host process: `http://127.0.0.1:1340`. Split-machine: a private host URL. | | `GROK_BOT_TOKEN` | if grok-bot | Bearer token from that computer's `gateway.json` (password) | | `GROK_BOT_AGENT_ID` | if grok-bot | Target agent id from `POST /api/listAgents` (from env, not hardcoded) | | `GROK_BOT_TIMEOUT_MS` | no | Default `120000` — how long `generate()` waits for `/internal/reply` | -| `BRIDGE_HTTP_HOST` | no | Default `0.0.0.0`. Colocated: bind `127.0.0.1` so the callback is localhost-only. | +| `BRIDGE_HTTP_HOST` | no | Default `0.0.0.0`. Colocated host process: bind `127.0.0.1` so the callback is localhost-only. | | `BRIDGE_HTTP_PORT` | no | Default `8788`. Compose maps host and container to this port (`${BRIDGE_HTTP_PORT:-8788}:${BRIDGE_HTTP_PORT:-8788}`) | -| `BRIDGE_PUBLIC_URL` | if grok-bot | URL the **Grok Bot computer** uses to reach this bridge. Colocated: `http://127.0.0.1:8788`. When the bridge runs on a different machine, that machine's `localhost` is not the bridge. | +| `BRIDGE_PUBLIC_URL` | if grok-bot | URL the **Grok Bot computer** uses to reach this bridge. Colocated host process: `http://127.0.0.1:8788`. When the bridge runs on a different machine, that machine's `localhost` is not the bridge. | | `BRIDGE_REPLY_TOKEN` | if grok-bot | Shared secret for `Authorization: Bearer` on `POST /internal/reply` (password) | | `SYSTEM_PROMPT_FILE` | no | Default `SOUL.md` (neutral); swap for your agent persona | | `TEMPERATURE` | no | Default `0.7` | @@ -120,7 +122,7 @@ There is no official Discord plugin. The working community path is: `BRIDGE_PUBLIC_URL` must be reachable **from the Grok Bot computer**. -- **Colocated (works today):** run this Node process on the Grok Bot computer and use `http://127.0.0.1:8788`. See [Run the bridge on the Grok Bot computer](#run-the-bridge-on-the-grok-bot-computer). +- **Colocated host process (works today):** `npm start` on the Grok Bot computer (not Docker) and use `http://127.0.0.1:8788`. See [Run the bridge on the Grok Bot computer](#run-the-bridge-on-the-grok-bot-computer). - **Split-machine:** when the bridge runs on a different machine (for example Docker on a laptop), `localhost` on the Grok Bot computer is not the bridge. Use Tailscale, an SSH tunnel, or another private path. **Do not** expose the Grok Bot gateway port (`:1340`) or the callback port (`:8788`) to the internet. Keep `DISCORD_BOT_TOKEN` in the bridge's local `.env` only — never in the Grok Bot agent, `gateway.json`, or git. Treat `GROK_BOT_TOKEN` and `BRIDGE_REPLY_TOKEN` like passwords; never commit or log them. @@ -129,11 +131,13 @@ This callback is unofficial and may break. See [docs/PHASE2-GROK-BOT-BACKEND.md] ## Run the bridge on the Grok Bot computer -The setup that works today without Tailscale or a tunnel: run **discord-agent-bridge on the same computer** as the Grok Bot agent so both the gateway (`:1340`) and the callback (`:8788`) stay on localhost. +The setup that works today without Tailscale or a tunnel is a **host-process-only** topology: run `npm start` **on the Grok Bot computer itself** (same network namespace as the gateway). Both the gateway (`:1340`) and the callback (`:8788`) stay on that host's localhost. + +This is **not** `docker compose` on that computer. Inside a container, `127.0.0.1:1340` is the container, not the host gateway, and `BRIDGE_HTTP_HOST=127.0.0.1` binds the callback to the container loopback so Compose's published port cannot reach it. **Prerequisites** -- Node.js **20+** on the Grok Bot computer +- Node.js **20+** on the Grok Bot computer (host process, not a container) - Discord bot token in a local `.env` only (never commit it) - Grok Bot computer gateway listening on `127.0.0.1:1340` @@ -166,13 +170,17 @@ npm run build npm start ``` -A Grok Bot computer **Update** keeps files on disk but stops running processes. A simple restart wrapper (or any supervisor) is useful so the bridge comes back after an update, for example: +Optional crash-only wrapper (restarts `npm start` if the Node process exits while this shell stays alive): ```bash while true; do npm start; echo "bridge exited; restarting in 5s"; sleep 5; done ``` -In this mode, **never** expose `:1340` or `:8788` beyond localhost. Full topology notes (colocated vs split-machine + Tailscale/tunnel) are in [docs/PHASE2-GROK-BOT-BACKEND.md](docs/PHASE2-GROK-BOT-BACKEND.md). +A Grok Bot computer **Update** keeps files on disk but stops running processes, including this shell. The loop does **not** survive an Update. After an Update, something **outside** that shell must start the bridge again: a manual `npm start`, a Grok Bot routine that checks and relaunches, or another supervisor that is re-armed after the update. + +Docker on the same Grok Bot host is a **different** setup, not this topology. The container must reach the host gateway (for example `host.docker.internal` / `extra_hosts`), and you must **not** bind the callback to the container's `127.0.0.1` if you need Compose's published port. Prefer the host-process path above. + +In this host-process mode, **never** expose `:1340` or `:8788` beyond localhost. Full topology notes (colocated host process vs split-machine + Tailscale/tunnel) are in [docs/PHASE2-GROK-BOT-BACKEND.md](docs/PHASE2-GROK-BOT-BACKEND.md). ## Scripts diff --git a/docs/PHASE2-GROK-BOT-BACKEND.md b/docs/PHASE2-GROK-BOT-BACKEND.md index 031413c..de3c1fb 100644 --- a/docs/PHASE2-GROK-BOT-BACKEND.md +++ b/docs/PHASE2-GROK-BOT-BACKEND.md @@ -67,9 +67,11 @@ This is **not** an unauthenticated public webhook. Do not log `BRIDGE_REPLY_TOKE | Topology | Where the bridge runs | Typical URLs | Network | |----------|----------------------|--------------|---------| -| **Colocated (works today)** | Same computer as the Grok Bot agent | `GROK_BOT_GATEWAY_URL=http://127.0.0.1:1340`, `BRIDGE_PUBLIC_URL=http://127.0.0.1:8788`, `BRIDGE_HTTP_HOST=127.0.0.1` | No Tailscale. Bind the callback to localhost only. | +| **Colocated host process (works today)** | `npm start` on the Grok Bot computer (same host network as the gateway — **not** Docker Compose) | `GROK_BOT_GATEWAY_URL=http://127.0.0.1:1340`, `BRIDGE_PUBLIC_URL=http://127.0.0.1:8788`, `BRIDGE_HTTP_HOST=127.0.0.1` | No Tailscale. Bind the callback to the **host** loopback only. | | **Split-machine** | A different machine (for example Docker on a laptop) | Gateway URL is the Grok Bot host; `BRIDGE_PUBLIC_URL` is the bridge host | `localhost` on the Grok Bot computer is **not** the bridge. Use Tailscale, an SSH tunnel, or another private path. | +`docker compose` + `127.0.0.1` on the Grok Bot computer is **not** the colocated topology. Inside the container, `127.0.0.1:1340` is the container, not the host gateway, and `BRIDGE_HTTP_HOST=127.0.0.1` binds the callback to the container loopback so Compose's published port cannot reach it. Docker on that same host is a different setup: reach the host gateway via `host.docker.internal` / `extra_hosts`, and do **not** bind the callback to the container's `127.0.0.1` if you need a published port. Prefer the host-process path. + **Never** expose the Grok Bot gateway port (`:1340`) or the callback port (`:8788`) to the internet. Keep `DISCORD_BOT_TOKEN` in the bridge's local `.env` only — never in the Grok Bot agent, `gateway.json`, or git. ## Env (grok-bot mode) @@ -77,22 +79,22 @@ This is **not** an unauthenticated public webhook. Do not log `BRIDGE_REPLY_TOKE | Variable | Required | Purpose | |----------|----------|---------| | `AGENT_BACKEND=grok-bot` | yes (to select) | Select this backend | -| `GROK_BOT_GATEWAY_URL` | yes | Colocated: `http://127.0.0.1:1340`. Split-machine: private host URL. | +| `GROK_BOT_GATEWAY_URL` | yes | Colocated host process: `http://127.0.0.1:1340`. Split-machine: private host URL. | | `GROK_BOT_TOKEN` | yes | Bearer from that computer's `gateway.json` (password) | | `GROK_BOT_AGENT_ID` | yes | Target agent id from `POST /api/listAgents` (from env; do not hardcode a real id) | | `GROK_BOT_TIMEOUT_MS` | no | Default `120000` | -| `BRIDGE_HTTP_HOST` | no | Default `0.0.0.0`. Colocated: `127.0.0.1` | +| `BRIDGE_HTTP_HOST` | no | Default `0.0.0.0`. Colocated host process: `127.0.0.1` | | `BRIDGE_HTTP_PORT` | no | Default `8788` (Compose maps `${BRIDGE_HTTP_PORT:-8788}:${BRIDGE_HTTP_PORT:-8788}`) | -| `BRIDGE_PUBLIC_URL` | yes | URL the Grok Bot computer uses to reach the bridge. Colocated: `http://127.0.0.1:8788` | +| `BRIDGE_PUBLIC_URL` | yes | URL the Grok Bot computer uses to reach the bridge. Colocated host process: `http://127.0.0.1:8788` | | `BRIDGE_REPLY_TOKEN` | yes | Shared secret for the callback (generate locally; do not commit) | OpenAI-compatible remains the default. grok-bot fails closed if required vars are missing. -### Colocated setup (same computer) +### Colocated setup (host process on the Grok Bot computer) -This is the path that works today without Tailscale or a tunnel. +This is the path that works today without Tailscale or a tunnel: a **host-process-only** topology (`npm start` on the Grok Bot computer). It is **not** `docker compose` on that host. -**Prerequisites:** Node.js 20+ on the Grok Bot computer; Discord bot token in a local `.env` only; gateway on `127.0.0.1:1340`. +**Prerequisites:** Node.js 20+ on the Grok Bot computer (host process, not a container); Discord bot token in a local `.env` only; gateway on `127.0.0.1:1340`. 1. Copy `.env.example` → `.env` on the Grok Bot computer. Set `AGENT_BACKEND=grok-bot` and: @@ -110,13 +112,9 @@ This is the path that works today without Tailscale or a tunnel. 5. Set Discord allowlists. Keep the Discord application **private**. -6. `npm ci && npm run build && npm start` (or a simple supervisor). A Grok Bot computer **Update** keeps files on disk but stops running processes, so a restart wrapper is useful: - - ```bash - while true; do npm start; echo "bridge exited; restarting in 5s"; sleep 5; done - ``` +6. `npm ci && npm run build && npm start`. An optional `while true; do npm start; ...; done` loop only restarts the Node child if it exits **while that shell stays alive**. A Grok Bot computer **Update** keeps files on disk but stops running processes, including the wrapper shell. The loop does **not** survive an Update. After an Update, something outside that shell must start the bridge again (manual `npm start`, a Grok Bot routine that checks and relaunches, or another supervisor that is re-armed after the update). -In this mode, **never** expose `:1340` or `:8788` beyond localhost. +In this host-process mode, **never** expose `:1340` or `:8788` beyond localhost. ### Split-machine setup @@ -145,7 +143,7 @@ A documented Discord/Channels inbound API would replace this callback. Until the ## Cutover / smoke -1. Set grok-bot env vars for the topology you are using (colocated localhost, or split-machine + Tailscale/tunnel); keep `AGENT_BACKEND=openai` as fallback. +1. Set grok-bot env vars for the topology you are using (colocated host process, or split-machine + Tailscale/tunnel); keep `AGENT_BACKEND=openai` as fallback. 2. Confirm `BRIDGE_PUBLIC_URL` is reachable from the Grok Bot computer (`POST /internal/reply` with a wrong token should 401; do not test with real tokens in logs). 3. Mention the bot in Discord → gateway `sendPrompt` → agent callback → Discord reply. 4. If the unofficial gateway changes, park grok-bot mode and stay on OpenAI. diff --git a/docs/PLAN.md b/docs/PLAN.md index 27a3fa9..92a64f1 100644 --- a/docs/PLAN.md +++ b/docs/PLAN.md @@ -80,13 +80,13 @@ flowchart LR | `DISCORD_REQUIRE_MENTION` | no | Default `true` outside free-response channels (DMs never require mention) | | `DISCORD_HOME_CHANNEL` | no | Optional home channel ID (logged / future routing) | | `AGENT_BACKEND` | no | `openai` (default) \| `grok-bot` (unofficial gateway + callback) | -| `GROK_BOT_GATEWAY_URL` | if grok-bot | Private computer gateway base (do not expose `:1340` publicly). Colocated: `http://127.0.0.1:1340` | +| `GROK_BOT_GATEWAY_URL` | if grok-bot | Private computer gateway base (do not expose `:1340` publicly). Colocated host process: `http://127.0.0.1:1340` | | `GROK_BOT_TOKEN` | if grok-bot | Bearer from that computer's `gateway.json` | | `GROK_BOT_AGENT_ID` | if grok-bot | Target agent id from `POST /api/listAgents` | | `GROK_BOT_TIMEOUT_MS` | no | Default `120000` | -| `BRIDGE_HTTP_HOST` | no | Default `0.0.0.0`. Colocated: `127.0.0.1` | +| `BRIDGE_HTTP_HOST` | no | Default `0.0.0.0`. Colocated host process: `127.0.0.1` | | `BRIDGE_HTTP_PORT` | no | Default `8788` | -| `BRIDGE_PUBLIC_URL` | if grok-bot | Reachable from the Grok Bot computer. Colocated: `http://127.0.0.1:8788`. When the bridge runs on a different machine, that machine's `localhost` is not the bridge. | +| `BRIDGE_PUBLIC_URL` | if grok-bot | Reachable from the Grok Bot computer. Colocated host process: `http://127.0.0.1:8788`. When the bridge runs on a different machine, that machine's `localhost` is not the bridge. | | `BRIDGE_REPLY_TOKEN` | if grok-bot | Bearer for `POST /internal/reply` | | `API_BASE_URL` | if openai | e.g. `https://api.x.ai/v1` | | `API_KEY` | if openai | Provider API key |