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
18 changes: 16 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ PORT=20123
# Serve GET /metrics without authentication.
# OPENCODE_METRICS_PUBLIC=false

# Dedicated scrape credential for GET /metrics. When set, the endpoint requires
# it (timing-safe compare) even when OPENCODE_METRICS_PUBLIC=true, so a scraper
# never needs a general-purpose API key.
# OPENCODE_METRICS_TOKEN=

# In-process rate limit per authenticated API key. RPM is requests per minute;
# BURST is the bucket capacity (0 derives max(1, ceil(rpm / 6))). RPM=0 disables
# the limiter. /admin/* uses a separate bucket. Rejections answer 429 with a
# retry-after header.
# OPENCODE_RATE_LIMIT_RPM=0
# OPENCODE_RATE_LIMIT_BURST=0

# ---------------------------------------------------------------------------
# Upstream OpenCode server
# ---------------------------------------------------------------------------
Expand Down Expand Up @@ -140,8 +152,10 @@ PORT=20123
# OPENCODE_ALLOW_REMOTE_MEDIA_URLS=true

# Allow remote media on private/loopback addresses. Disabling it prevents SSRF
# against the host network; keep it false unless you control the URL sources.
# OPENCODE_ALLOW_PRIVATE_MEDIA_URLS=true
# against the host network. Default: false. Set it to true only when every media
# URL source is trusted; when it is enabled while HOST is not loopback the
# gateway logs a startup error naming the SSRF exposure.
# OPENCODE_ALLOW_PRIVATE_MEDIA_URLS=false

# Size and count limits. The same byte budget applies to one attachment and to
# the combined total per request: an attachment that would push the request over
Expand Down
33 changes: 33 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Dependabot keeps the dependency pins current.
#
# `ci.yml` pins every action to the commit SHA of a release tag, so without this
# file those pins would silently age. The commit prefixes mirror
# `CONTRIBUTING.md`: Dependabot pull requests are linted by the "Commit messages"
# job like any other, so `build(deps)` / `ci(deps)` are the valid spellings.
version: 2
updates:
# Runtime and dev dependencies from package.json / pnpm-lock.yaml.
- package-ecosystem: npm
directory: "/"
schedule:
interval: weekly
open-pull-requests-limit: 10
commit-message:
prefix: "build(deps)"
groups:
# Routine dev-tool bumps land as one pull request instead of one per
# package. Runtime dependencies keep individual pull requests because they
# change the shipped surface.
dev-dependencies:
dependency-type: development
update-types: ["minor", "patch"]

# The actions used by .github/workflows/ci.yml; Dependabot rewrites the SHA
# and refreshes the trailing `# vN` comment.
- package-ecosystem: github-actions
directory: "/"
schedule:
interval: weekly
open-pull-requests-limit: 10
commit-message:
prefix: "ci(deps)"
90 changes: 84 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,34 @@ concurrency:
permissions:
contents: read

# Every action is pinned to the commit SHA of the release tag named in the
# trailing comment; `.github/dependabot.yml` keeps the pins current. Never
# replace a pin with a floating `@vN` tag: this workflow holds a repository
# token, and a moved tag would silently change what runs.
#
# The required status checks on `main` are "Commit messages",
# "Verify (Node 20.x)" and "Verify (Node 22.x)". The `windows` and `docker`
# jobs below are additive and must never take over those names.
jobs:
commit-messages:
name: Commit messages
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- name: Check out the full history
uses: actions/checkout@v7
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
with:
fetch-depth: 0

- name: Set up pnpm
uses: pnpm/action-setup@v6
uses: pnpm/action-setup@73e46992e05b19abd31f2f3c20e0e7370d762115 # v6
with:
# Pinned to the version in `packageManager`; `allowBuilds` in
# pnpm-workspace.yaml is a pnpm 10+ feature. Bump both together.
version: 12.25.1

- name: Set up Node.js
uses: actions/setup-node@v7
uses: actions/setup-node@8fccf151e3ba8038c300f83b40c8fd1e94f9c5ce # v7
with:
node-version: 22.x
cache: pnpm
Expand Down Expand Up @@ -91,15 +99,15 @@ jobs:
node: ["20.x", "22.x"]
steps:
- name: Check out the repository
uses: actions/checkout@v7
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7

- name: Set up pnpm
uses: pnpm/action-setup@v6
uses: pnpm/action-setup@73e46992e05b19abd31f2f3c20e0e7370d762115 # v6
with:
version: 12.25.1

- name: Set up Node.js ${{ matrix.node }}
uses: actions/setup-node@v7
uses: actions/setup-node@8fccf151e3ba8038c300f83b40c8fd1e94f9c5ce # v7
with:
node-version: ${{ matrix.node }}
cache: pnpm
Expand All @@ -123,3 +131,73 @@ jobs:

- name: Audit the production dependencies
run: pnpm audit --prod

# The step above is the merge gate (`--prod` covers what ships). This one
# reports advisories in the dev toolchain too, without blocking: a
# vulnerable vitest or commitlint is worth seeing but not worth a red PR.
- name: Audit all dependencies (informational)
continue-on-error: true
run: pnpm audit

# Windows coverage. The suite has platform-specific paths the Ubuntu matrix
# never runs: `OPEN_NOFOLLOW` collapses to 0 on Windows, the local-file read
# has a Windows fallback, and CLI discovery walks `%APPDATA%`/`%LOCALAPPDATA%`.
# The three media tests that pin POSIX `O_NOFOLLOW` semantics stay skipped here
# (`it.skipIf(platform === "win32")`) and keep running on Ubuntu; this job
# covers the rest of the suite. It is additive and not one of the required
# checks, but it is a real gate: a Windows-only regression must not merge.
windows:
name: Verify on Windows (Node 22.x)
runs-on: windows-latest
timeout-minutes: 20
permissions:
contents: read
steps:
- name: Check out the repository
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7

- name: Set up pnpm
uses: pnpm/action-setup@73e46992e05b19abd31f2f3c20e0e7370d762115 # v6
with:
version: 12.25.1

- name: Set up Node.js 22.x
uses: actions/setup-node@8fccf151e3ba8038c300f83b40c8fd1e94f9c5ce # v7
with:
node-version: 22.x
cache: pnpm

- name: Install the dependencies
run: pnpm install --frozen-lockfile

- name: Typecheck
run: pnpm typecheck

- name: Build
run: pnpm build

- name: Test
run: pnpm test

# The README documents a Docker deployment; nothing verified the Dockerfile
# until this job existed, so a broken image could merge unnoticed.
docker:
name: Docker image
runs-on: ubuntu-latest
timeout-minutes: 20
permissions:
contents: read
steps:
- name: Check out the repository
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7

- name: Build the image
run: docker build --tag opencode-api:ci .

# `env_file: .env` has to exist for the file to parse, and the repository
# does not ship a `.env`; the example file is a valid stand-in. This
# validates the compose contract the README documents.
- name: Validate the compose file
run: |
cp .env.example .env
docker compose config --quiet
5 changes: 4 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ cannot be merged.

`pnpm test:integration` additionally needs a running gateway and consumes real
upstream quota; it is skipped unless `OPENCODE_API_URL` and `OPENCODE_API_KEY` are
set.
set. `pnpm test:all` runs the unit suite and the integration suite in one
`vitest run`; because the integration tests skip themselves when no gateway is
configured, it refuses to start (exit 1, naming `OPENCODE_API_URL` and
`OPENCODE_API_KEY`) instead of silently degrading to a unit-only run.

## Ground rules

Expand Down
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ RUN pnpm install --frozen-lockfile

COPY tsconfig.json tsconfig.build.json ./
COPY src ./src
# `pnpm build` runs `scripts/clean-dist.mjs` before `tsc`, so the build stage
# needs the scripts directory too.
COPY scripts ./scripts
RUN pnpm build

# Produce a pruned, production-only dependency tree (dist is already built).
Expand Down
59 changes: 48 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ existing OpenAI or Anthropic SDK at it by changing only the base URL.

## Requirements

* **Node.js >= 20.11** (verified on Node 26)
* **Node.js >= 20.11** — the supported range is the CI matrix: Linux with Node
20.x and 22.x, plus Windows with Node 22.x, which exercises the
Windows-specific paths (`O_NOFOLLOW` is unavailable there, CLI discovery walks
`%APPDATA%`/`%LOCALAPPDATA%`). The container image ships Node 22
(`node:22-bookworm-slim`).
* **pnpm** (the repository ships a `pnpm-lock.yaml`)
* An installed **`opencode` CLI 2.x** on `PATH`, or an explicit path through
`OPENCODE_BIN` (verified against **2.3.2** and **2.4.3**)
Expand Down Expand Up @@ -468,19 +472,32 @@ model_disabled`. Query `GET /v1/models` instead of guessing.
| `HOST` / `PORT` | `127.0.0.1` / `20123` | Listen address. |
| `OPENCODE_API_KEYS` | `sk-dummy-123` | Accepted caller keys (comma separated). Replace the built-in default before exposing the port. |
| `OPENCODE_ALLOW_NO_AUTH` | `false` | Start without keys (dangerous). |
| `OPENCODE_METRICS_TOKEN` | *(unset)* | Dedicated scrape token for `GET /metrics`; configuring it keeps that endpoint authenticated even with `OPENCODE_METRICS_PUBLIC=true`. |
| `OPENCODE_RATE_LIMIT_RPM` | `0` (disabled) | Per-minute request budget per API key; `0` turns rate limiting off. |
| `OPENCODE_RATE_LIMIT_BURST` | `0` (disabled) | Burst capacity allowed on top of the per-minute budget. |
| `OPENCODE_CORS_ORIGINS` | *(empty)* | Browser origins allowed to call the gateway; `*` allows every origin. |
| `OPENCODE_SERVER_URL` / `OPENCODE_SERVER_PASSWORD` | *(unset)* | Explicit upstream server. |
| `OPENCODE_AUTOSTART` | `true` | Spawn a private `opencode serve`. |
| `OPENCODE_BIN` | *(unset)* | Path to the OpenCode CLI executable. |
| `OPENCODE_START_TIMEOUT_MS` | `30000` | Time allowed for the private upstream server to report readiness. |
| `OPENCODE_REQUEST_TIMEOUT_MS` | `600000` | Wall-clock budget for one upstream turn. |
| `OPENCODE_WORKDIR` | `<data dir>/workspace` | Directory the agent may work in. |
| `OPENCODE_AGENT` | `build` | Default agent for API turns. |
| `OPENCODE_AGENT_MAX_STEPS` | *(unset)* | Maximum agent steps per turn. |
| `OPENCODE_ALLOW_LOCAL_FILES` | `false` | Accept local-path attachments. |
| `OPENCODE_ALLOW_PRIVATE_MEDIA_URLS` | `true` | Allow remote media on private hosts (set `false` to close SSRF). |
| `OPENCODE_ALLOW_PRIVATE_MEDIA_URLS` | `false` | Allow remote media on private hosts (enabling it re-opens SSRF). |
| `OPENCODE_MAX_ATTACHMENT_BYTES` | `20971520` | Per-request media budget. |
| `OPENCODE_REASONING_DEFAULT` | `on-request` | Global reasoning policy. |
| `OPENCODE_SESSION_CACHE` | `true` | Reuse upstream sessions. |
| `OPENCODE_SESSION_PERSIST` | `false` | Persist the session cache to `<data dir>/sessions.json`. |
| `OPENCODE_MODEL_CACHE_TTL_MS` | `300000` | Model catalog cache lifetime. |
| `OPENCODE_TOOL_BRIDGE_DENY_NATIVE_TOOLS` | `true` | Deny built-in agent tools when client tools are bridged. |
| `OPENCODE_EXPOSE_NATIVE_TOOLS` | `false` | Stream `opencode.tool_call` events. |
| `OPENCODE_NATIVE_TOOL_EVENT` | `opencode.tool_call` | SSE event name for native tool activity. |
| `OPENCODE_LOG_LEVEL` | `info` | Pino log level. |
| `OPENCODE_LOG_PRETTY` | `true` on a TTY | Pretty-print logs instead of JSON lines. |
| `OPENCODE_METRICS_ENABLED` | `true` | Prometheus collection; `false` makes `GET /metrics` answer `404`. |
| `OPENCODE_INSTANCE_ID` | `opencode-api-<pid>` | Instance identifier, and the fingerprint of the persisted session cache. |

Every variable, with types, defaults and security notes:
[docs/CONFIGURATION.md](docs/CONFIGURATION.md).
Expand Down Expand Up @@ -547,13 +564,22 @@ Every variable, with types, defaults and security notes:
`OPENCODE_TOOL_BRIDGE_DENY_NATIVE_TOOLS=true` (the default). Requests without
`tools` are not restricted by this mechanism at all — bound them with
`OPENCODE_AGENT_PERMISSIONS` and a read-only agent instead.
* **SSRF through remote media.** `OPENCODE_ALLOW_PRIVATE_MEDIA_URLS=true`
(the default) lets a caller make the gateway fetch `http://127.0.0.1:...`,
RFC1918 addresses, `169.254.169.254` metadata endpoints and similar. Set it to
`false` on any deployment reachable by untrusted clients.
* **SSRF through remote media.** `OPENCODE_ALLOW_PRIVATE_MEDIA_URLS` is `false`
by default. Setting it to `true` lets a caller make the gateway fetch
`http://127.0.0.1:...`, RFC1918 addresses, `169.254.169.254` metadata endpoints
and similar; the gateway then logs a startup error while `HOST` is not a
loopback address. Leave it `false` on any deployment reachable by untrusted
clients.
* **Local file disclosure.** `OPENCODE_ALLOW_LOCAL_FILES=false` by default. If
you enable it, keep `OPENCODE_ALLOWED_FILE_ROOTS` narrow and remember that
symlinks are resolved before the check.
symlinks are resolved before the check (on Windows a final symlink or junction
is refused and the opened handle is re-checked against the pre-open `lstat`).
* **Unbounded per-key traffic.** `OPENCODE_RATE_LIMIT_RPM=0` disables the
built-in limiter. Set it (with `OPENCODE_RATE_LIMIT_BURST`) on a shared
deployment so one key cannot drive unlimited paid upstream turns.
* **Anonymous metrics.** `OPENCODE_METRICS_PUBLIC=true` exposes model names and
traffic without authentication; prefer `OPENCODE_METRICS_TOKEN` and keep the
endpoint closed.
* Put the gateway behind TLS, keep CORS origins explicit, and do not enable
`OPENCODE_TRUST_PROXY` unless a proxy you control sets the forwarded headers.

Expand Down Expand Up @@ -587,23 +613,34 @@ The compose file maps `20123:20123`, reads `.env`, mounts
`${OPENCODE_DATA_DIR:-./.opencode-api}:/data` and
`${OPENCODE_WORKDIR:-./workspace}:/workspace`, and sets
`OPENCODE_DATA_DIR=/data`, `OPENCODE_WORKDIR=/workspace` and
`OPENCODE_BIN=/usr/local/bin/opencode`. The container runs as the non-root
`node` user, so the mounted directories must be writable by UID 1000.
`OPENCODE_BIN=/usr/local/bin/opencode`.

The image itself is built from `node:22-bookworm-slim`, declares `/data` and
`/workspace` as volumes, defaults `HOST=0.0.0.0` and `PORT=20123`, ships a
`HEALTHCHECK` that probes `/health` with the bundled Node runtime, and runs as
the non-root `node` user (uid/gid 1000) — so the mounted directories must be
writable by UID 1000. CI builds the image on every pull request
(`.github/workflows/ci.yml`, job "Docker image") and validates this compose file.

## Development

| Script | What it does |
| --- | --- |
| `pnpm setup` | Write `.env` (keeps `sk-dummy-123`; `-- --random` generates a strong key instead). |
| `pnpm dev` | Start with `tsx watch` and reload on change. |
| `pnpm build` | Type-check and emit `src/` into `dist/` with `tsc -p tsconfig.build.json`. |
| `pnpm clean` | Remove `dist/`. |
| `pnpm build` | Remove `dist/` first, then type-check and emit `src/` into it with `tsc -p tsconfig.build.json`, so a removed or renamed source cannot leave stale output behind. |
| `pnpm typecheck` | `tsc -p tsconfig.json --noEmit`. |
| `pnpm test` | Unit tests (excludes `test/integration/**`). |
| `pnpm test:integration` | Integration tests against a live gateway/upstream. |
| `pnpm test:all` | Everything. |
| `pnpm test:all` | Unit **and** integration tests. Refuses to start (exit 1) unless `OPENCODE_API_URL` and `OPENCODE_API_KEY` are set, because the integration suite skips itself without them. |
| `pnpm lint:commits` | Lint the latest commit message against `commitlint.config.mjs`. |
| `pnpm smoke` | Run `scripts/smoke.mjs` against a running gateway. |

`pnpm test:all` means what it says: instead of silently running the unit suite
only, it prints the missing variables and exits 1. Use `pnpm test` for the unit
suite alone and `pnpm test:integration` for the live suite alone.

`pnpm smoke` reads `OPENCODE_API_URL` (default `http://127.0.0.1:20123`) and
`OPENCODE_API_KEY` (defaults to `sk-dummy-123`) and checks health, models, non-streaming chat,
streaming chat and Anthropic messages:
Expand Down
Loading
Loading