feat: add Anthropic Messages API proxy (claude-code compatible)#1
Merged
Conversation
… 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
OpenAIStreamEncoderclass — fix concurrency bug (globaltoolCallIndex/_messageId)applyNoToolsSafeguard)Phase 2: Anthropic API
POST /v1/messages— streaming + non-streaming (SSE event-typed)POST /v1/messages/count_tokens— CJK-weighted heuristic estimateGET /v1/models— content-negotiated (Anthropic shape viaanthropic-versionheader)Setup
--setup-claude-code— generates~/.config/commandcode-api-proxy/claude-settings.jsonANTHROPIC_DEFAULT_SONNET_MODEL, etc.)~/.claude/settings.jsonTests
Usage
npx commandcode-api-proxy --setup-claude-code claude --settings ~/.config/commandcode-api-proxy/claude-settings.json