> AI tools where used when creating this PR Updates `AGENTS.md` to reflect that `/aibridge` is now a directory in coder/coder repo not a separate repo.
4.4 KiB
AI Agent Guidelines for aibridge
This is a package-level guide for the
aibridge/subdirectory inside the coder/coder repository.Read the repo-root
AGENTS.mdandCLAUDE.mdfirst. They are the source of truth for all shared conventions: tone, foundational rules, essential commands, git hooks, code style, Go patterns, testing patterns, LSP navigation, and PR style. This file documents only what is specific to theaibridge/package; it never relaxes a root rule.For local overrides, create
AGENTS.local.md(gitignored).
Architecture Overview
AI Bridge is a smart gateway that sits between AI clients (Claude Code,
Cursor, etc.) and upstream providers (Anthropic, OpenAI). It intercepts
all AI traffic to provide centralized authn/z, auditing, token
attribution, and MCP tool administration. It runs as part of coderd
(the Coder control plane). Users authenticate with their Coder session
tokens.
┌─────────────┐ ┌──────────────────────────────────────────┐
│ AI Client │ │ aibridge │
│ (Claude Code,│────▶│ RequestBridge (http.Handler) │
│ Cursor) │ │ ├── Provider (Anthropic/OpenAI) │
└─────────────┘ │ ├── Interceptor (streaming/blocking) │
│ ├── Recorder (tokens, prompts, tools) │
│ └── MCP Proxy (tool injection) │
└──────────────┬───────────────────────────┘
│
▼
┌──────────────┐
│ Upstream API │
│ (Anthropic, │
│ OpenAI) │
└──────────────┘
The wire-up between aibridge and coderd lives in
enterprise/aibridged/. That package is outside the scope of this
guide.
Key packages within aibridge/:
intercept/: request/response interception, per-provider subdirs (messages/,responses/,chatcompletions/)provider/: upstream provider definitions (Anthropic, OpenAI, Copilot)mcp/: MCP protocol integrationcircuitbreaker/: circuit breaker for upstream callscontext/: request-scoped context helpersinternal/integrationtest/: integration tests with mock upstreams
Commands
Use the repo-root commands documented in the root AGENTS.md. The
notes below are aibridge-specific:
- Run only aibridge tests with
go test ./aibridge/.... The rootmake testruns the full coder/coder suite. - Regenerate the MCP mock with
go generate ./aibridge/mcpmock/after changingaibridge/mcp/api.go. The repo-rootmake gendoes not include this target.
Streaming Code
This package heavily uses SSE streaming. When modifying interceptors:
- Always handle both blocking and streaming paths.
- Test with
*_test.gofiles in the same package. They cover edge cases for chunked responses. - Be careful with goroutine lifecycle. Ensure proper cleanup on context cancellation.
Commit and PR Scope
Follow the commit and PR style in the root AGENTS.md and
.claude/docs/PR_STYLE_GUIDE.md. Format: type(scope): message. The
scope must be a real filesystem path containing every changed file.
For changes inside aibridge/, the scope is the path from the repo
root, for example:
feat(aibridge/intercept/messages): add cache token trackingfix(aibridge/provider): handle nil response bodyrefactor(aibridge/mcp): extract tool filtering
Use a broader scope, or omit the scope, when changes span beyond
aibridge/.
Common Pitfalls
| Problem | Fix |
|---|---|
| Race in streaming tests | Use t.Cleanup() and proper synchronization, never time.Sleep. |
mcpmock out of date |
Run go generate ./aibridge/mcpmock/ after changing aibridge/mcp/api.go. |
| Formatting failures | Run make fmt from the repo root before committing. |