Skip to content

Latest commit

 

History

History
216 lines (166 loc) · 8.13 KB

CONTRIBUTING.md

File metadata and controls

216 lines (166 loc) · 8.13 KB

Contributing to OpenCode API

Thanks for taking the time to contribute. This document covers the development workflow, the coding standards and — most importantly — the commit message convention this repository enforces.

Getting set up

pnpm install
pnpm setup          # writes .env; the default API key is sk-dummy-123
pnpm dev            # tsx watch on http://127.0.0.1:20123

Requirements: Node.js >= 20.11, pnpm 10+, and an installed OpenCode CLI 2.x (opencode --version). The gateway spawns its own opencode serve process, so you do not need to start anything else.

Run the same checks CI would run before opening a pull request:

pnpm typecheck
pnpm build
pnpm test
pnpm smoke:dist     # boots the emitted dist/ and probes /health
pnpm audit --prod   # the merge gate for shipped dependencies
pnpm lint:commits   # lints your latest local commit message

Every push to main and every pull request runs the same steps in GitHub Actions (.github/workflows/ci.yml). The commit-messages job lints every commit the pull request adds, plus the pull request title, against commitlint.config.mjs — a branch that does not satisfy the convention below 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. 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

  • English only. Code, comments, JSDoc, log and error messages, tests, commit messages, issues and pull requests are written in English.
  • The internal contract is frozen. docs/ARCHITECTURE.md defines the module boundaries and public signatures. Changing one requires updating every call site and the document in the same commit.
  • Streaming is a first-class path. Every surface must work both streamed and buffered; a change that only handles one of them is incomplete.
  • No direct upstream access. Talk to OpenCode through ctx.client (OpencodeClient) and, for completions, through ctx.completion. Never call fetch against the OpenCode server from an adapter, and never call a model provider directly.
  • Never log secrets. API keys, upstream passwords and raw Authorization headers must not reach the logs. Use the redaction already configured in src/logger.ts.

Coding standards

  • TypeScript strict mode plus noUncheckedIndexedAccess; no any (use unknown and narrow).
  • ESM only: every relative import ends in .js.
  • Validate inbound JSON with zod (schema.safeParse(body)), and throw ApiError.badRequest(message, { code, param }) on failure so the surface adapter renders the right error shape.
  • Keep adapter-specific wire formatting inside src/adapter/<surface>/; shared logic belongs in src/adapter/common/.
  • Formatting is manual and consistent with the existing files: 2 spaces, double quotes, semicolons, trailing commas, ~120 column soft limit.

Tests

  • New behaviour needs a test. Unit tests must never require a live OpenCode server: use test/helpers/context.ts and script the CompletionEvent stream.
  • Anything that touches the upstream protocol should also get a live integration test in test/integration/ when it can be expressed without consuming a lot of quota.
  • Bug fixes need a regression test that fails without the fix.

Commit messages

This project follows Conventional Commits 1.0.0. The format is not optional: release notes and change grouping are generated from it, reviewers rely on the type to triage, and CI rejects a commit that does not parse. The exact rules are in commitlint.config.mjs; run pnpm lint:commits to check your latest local commit before pushing.

Format

<type>(<scope>)<!>: <subject>

<body>

<footer>
  • Header — required, at most 72 characters for the subject line.
  • Body — optional, wrapped at 72 columns; explain why the change is needed and any trade-off made. Do not restate the diff.
  • Footer — optional; BREAKING CHANGE:, issue references, co-authors.

Types

Type Use for
feat A new user-visible capability: an endpoint, a request field, an option, a surface.
fix A bug fix that changes behaviour.
perf A change that only improves performance.
refactor Internal change with no behaviour difference.
docs Documentation only (README, docs/, JSDoc).
test Test-only changes.
build Build system, dependencies, package.json, tsconfig, Dockerfile.
ci CI configuration.
chore Maintenance that fits nothing above (gitignore, formatting, scratch cleanup).
revert Reverts a previous commit; reference its hash in the body.

Scopes

Use the module or concern the commit touches. Pick one; omit it only when the change is genuinely repository-wide.

openai · anthropic · responses · models · streaming · media · reasoning · tools · upstream · sessions · events · auth · config · server · metrics · docs · deps · release

Subject rules

  • Imperative mood ("add", not "added" / "adds"): the commit completes the sentence "If applied, this commit will …".
  • Lowercase first letter, no trailing period.
  • Describe the effect, not the mechanics: fix(media): reject IPv6-mapped loopback hosts beats fix(media): change isPrivateHost regex.

Breaking changes

Mark a breaking change with ! after the type or scope and a BREAKING CHANGE: footer that states what breaks and how to migrate.

feat(config)!: require an explicit API key

Remove the built-in default key so an unconfigured deployment cannot start.

BREAKING CHANGE: OPENCODE_API_KEYS is now mandatory. Set it in .env
(`pnpm setup -- --random`) or start with OPENCODE_ALLOW_NO_AUTH=true.

Examples

feat(anthropic): stream thinking blocks with deterministic signatures
fix(upstream): track the spawned server so shutdown terminates it

The child process handle was never stored, so stop() had nothing to kill and
every restart leaked a listener on the internal port.

Refs: #12
docs(config): document OPENCODE_TOOL_BRIDGE_DENY_NATIVE_TOOLS
build(deps): bump fastify to 5.6.2

Avoid:

  • update stuff, fix bug, wip, changes — no information.
  • Mixing unrelated changes: a dependency bump plus an endpoint change belongs in two commits.
  • Commit-per-file. Group by intent.

Branches and pull requests

  • Branch names mirror the commit type: feat/tool-bridge, fix/session-busy, docs/contributing.
  • Keep the history linear; rebase on main rather than merging it back in.
  • A pull request should contain one logical change, a description of the behaviour change, and the commands you ran to verify it.
  • Do not commit .env, dist/, .opencode-api/ or anything else the .gitignore covers.

Pull request checklist

  • pnpm typecheck passes.
  • pnpm build passes.
  • pnpm test passes.
  • pnpm smoke:dist passes (it needs pnpm build first).
  • pnpm audit --prod reports no advisory in the shipped dependencies.
  • New behaviour is covered by a test.
  • docs/ARCHITECTURE.md and the relevant reference doc are updated if a contract, an environment variable or a wire format changed.
  • No secrets, no debug logging, no commented-out code.
  • Commits follow the convention above.

Reporting bugs

Include the gateway version (GET /health), the OpenCode CLI version (opencode --version), the surface you called, the exact request (redact your key), the response or SSE frames, and the relevant gateway log lines. Errors from the upstream provider are surfaced with their original type — for example a 429 rate_limit_error with retry-after means the OpenCode free tier quota, not a gateway defect.

License

By contributing you agree that your contributions are licensed under the MIT License in LICENSE.