Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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 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://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 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=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
Expand Down
75 changes: 67 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand All @@ -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, e.g. `http://host:1340` |
| `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 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 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 (not `localhost` on that machine) |
| `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` |
Expand All @@ -118,12 +120,68 @@ 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**.

- **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`) 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.
**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 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 (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`

**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
```

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
Comment thread
FlyOverCoderKY marked this conversation as resolved.
Resolved
```

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

| Script | Action |
Expand All @@ -135,9 +193,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.
Expand Down
55 changes: 46 additions & 9 deletions docs/PHASE2-GROK-BOT-BACKEND.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,26 +63,63 @@ 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 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)

| 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 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` |
| `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 |
| `BRIDGE_REPLY_TOKEN` | yes | Shared secret for the callback |
| `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 (host process on the Grok Bot computer)

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 (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:

```
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 <GROK_BOT_TOKEN>`). 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`. 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 host-process 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:
Expand All @@ -106,7 +143,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 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.
Expand Down
8 changes: 4 additions & 4 deletions docs/PLAN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 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 |
| `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 host process: `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 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 |
Expand Down
Loading