Skip to content

Add Anthropic /v1/messages and proxy rotation - #6

Open
Suydev wants to merge 2 commits into
jcleng:mainfrom
Suydev:feat/anthropic-proxy-pool
Open

Suydev wants to merge 2 commits into
jcleng:mainfrom
Suydev:feat/anthropic-proxy-pool

Conversation

@Suydev

@Suydev Suydev commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

What

Adds Anthropic Messages API compatibility to the relay without touching the OpenAI surface, so a single baseURL works for both client families at the same endpoint:

  • POST /v1/chat/completions — OpenAI shape (unchanged)
  • POST /v1/messages — Anthropic shape (new): message blocks (text, tool_use, tool_result), system (string or block array), tools[].input_schema, max_tokens/temperature/top_p, stream

Both route to the same free-tier upstream pipeline (fingerprint injection, tool bridging, quota tracking).

Why

A growing set of Anthropic-protocol clients (Claude Code and similar) assume the POST /v1/messages contract. Prior art for serving both dialects from one proxy: maxnowack/anthropic-proxy (★414) and AntonioAEMartins/claude-code-proxy; mizaawa/ZakoProxyCLI likewise advertises Responses + Chat Completions + Messages. This change follows the same dual-shape pattern while keeping the relay pure-forwarding (no tool execution, no CLI/dialect side effects).

Converter details

  • Anthropic → OpenAI: system blocks flattened; tools input_schema → function.parameters; assistant tool_use → message.tool_calls; user tool_result → tool role messages emitted before any trailing user text (they answer the previous assistant tool_calls; strict OpenAI ordering rejects text interleaved before tool results — this ordering bug is caught by the new tests).
  • OpenAI → Anthropic: finish_reason → stop_reason (stop→end_turn, tool_calls→tool_use, length→max_tokens), usage mapped to input_tokens/output_tokens, msg_… ids.
  • Streaming: Anthropic SSE lifecycle (message_start, content_block_start, content_block_delta, content_block_stop, message_delta, message_stop).
  • Auth: x-api-key (Anthropic convention) or Bearer both accepted; documented anthropic-version header accepted.
  • Model fallback via OPENCODE_ANTHROPIC_MODEL when an unknown model id is requested.

Also in this branch

  • src/proxy-pool.js + upstream.js: optional egress proxy pool (PROXY_POOL_FILE) with automatic rotation + retry on FreeUsageLimitError (free-tier quota is partly IP-tied; rotation gives repeatable escapes).
  • MAX_REQUEST_ATTEMPTS now env-overridable.

Tests

  • 9 new tests in test/anthropic.test.js: converter unit tests + HTTP integration against a mock upstream (JSON mode, tool_use round-trip, Anthropic SSE lifecycle, model fallback).
  • Full suite: 90/90 pass (npm test).
  • Live verification against the real free tier (bun runtime): OpenAI sentinel round-tripped via /v1/chat/completions, Anthropic sentinel via /v1/messages (JSON + SSE) on the same base URL.

root and others added 2 commits November 9, 2026 09:32
…ic endpoint tests

- anthropicMessagesToOpenAI: tool_result blocks must convert to OpenAI 'tool'
  messages BEFORE the trailing user text (they answer the previous assistant
  tool_calls; strict OpenAI order otherwise).
- export the pure converters for testability.
- test/anthropic.test.js: 9 tests (converter unit tests + HTTP integration
  against a mock upstream: JSON mode, tool_use round-trip, SSE lifecycle,
  model fallback).
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