Skip to content

feat: add Anthropic Messages API proxy (claude-code compatible)#1

Merged
thaolaptrinh merged 16 commits into
mainfrom
feature/anthropic-api-proxy
Jul 1, 2026
Merged

feat: add Anthropic Messages API proxy (claude-code compatible)#1
thaolaptrinh merged 16 commits into
mainfrom
feature/anthropic-api-proxy

Conversation

@thaolaptrinh

Copy link
Copy Markdown
Owner

Summary

Add full Anthropic Messages API surface alongside the existing OpenAI-compatible endpoint. Claude Code can now use the proxy via claude --settings ~/.config/commandcode-api-proxy/claude-settings.json.

Changes

Phase 1: Refactor

  • OpenAIStreamEncoder class — fix concurrency bug (global toolCallIndex/_messageId)
  • Extract shared helpers (applyNoToolsSafeguard)

Phase 2: Anthropic API

  • POST /v1/messages — streaming + non-streaming (SSE event-typed)
  • POST /v1/messages/count_tokens — CJK-weighted heuristic estimate
  • GET /v1/models — content-negotiated (Anthropic shape via anthropic-version header)
  • Full tool_use/tool_result/thinking block support
  • Stop reason mapping, error shape đúng Anthropic format

Setup

  • --setup-claude-code — generates ~/.config/commandcode-api-proxy/claude-settings.json
  • Model mapping via Claude Code built-in env vars (ANTHROPIC_DEFAULT_SONNET_MODEL, etc.)
  • Auto-detect shell, không đụng ~/.claude/settings.json

Tests

  • 131 tests, all passing
  • E2E: non-streaming, streaming, error shapes, count_tokens, /v1/models content-negotiation

Usage

npx commandcode-api-proxy --setup-claude-code
claude --settings ~/.config/commandcode-api-proxy/claude-settings.json

… helpers

Fix concurrency bug: module-level toolCallIndex and _messageId were shared
across concurrent requests. Replace toOpenAIStreamChunk / toOpenAIErrorChunk
with a per-request OpenAIStreamEncoder class. Remove getMessageId/resetMessageId
globals from util.ts. Extract applyNoToolsSafeguard for reuse by the upcoming
Anthropic translator. Deduplicate OPENAI_FINISH_MAP (was declared twice).
Add full Anthropic API surface alongside the existing OpenAI-compatible endpoint:
- POST /v1/messages (streaming + non-streaming)
- POST /v1/messages/count_tokens (CJK-weighted heuristic estimate)
- GET /v1/models (content-negotiated via anthropic-version header)

Translator (src/translate/anthropic.ts):
- toCCRequest: Anthropic → CC request (messages, tools, tool_choice, thinking,
  system, images, tool_result with name lookup)
- AnthropicStreamEncoder: CC events → Anthropic SSE (message_start, content_block_*,
  signature_delta, ping, message_delta, message_stop, error)
- buildAnthropicResponse: non-streaming Anthropic response assembly

Supporting modules:
- anthropic-types.ts: discriminated-union types (no any)
- anthropic-models.ts: env-based claude-* → CC model mapping
- validation.ts: validateAnthropicRequest (rejects unsupported blocks/tools)
- stream.ts: formatAnthropicSSE for event-typed SSE
- server.ts: handleMessages, handleCountTokens, sendAnthropicError,
  format-aware handleUpstreamError(err, format)

122 tests pass, lint clean, build clean.
- /v1/messages (non-streaming + streaming)
- /v1/messages/count_tokens
- /v1/models content-negotiation (Anthropic vs OpenAI shape)
- Anthropic error shapes (400 invalid_request_error)
Instead of printing instructions, detect shell (zsh/bash/fish) and append
ANTHROPIC_BASE_URL and ANTHROPIC_API_KEY=proxy-managed to the user's RC
file. Detect already-set vars to avoid duplicates.
- Write model config to ~/.config/commandcode-api-proxy/anthropic-models.json
- Write Claude Code settings to ~/.config/commandcode-api-proxy/claude-settings.json
  (does NOT touch ~/.claude/settings.json)
- Print usage: claude --settings <path> or alias shortcut
- Remove anthropic-models.json glob config (no longer needed)
- --setup-claude-code only writes claude-settings.json with model env vars
  (ANTHROPIC_DEFAULT_SONNET/OPUS/HAIKU_MODEL)
- resolveAnthropicModel uses ANTHROPIC_DEFAULT_MODEL env or fallback
- Proxy pass-through model ID, Claude Code maps via its settings
@thaolaptrinh thaolaptrinh merged commit 88ff338 into main Jul 1, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant