Skip to content

feat: Multi-API format support (OpenAI Chat, Response API, Anthropic)#43

Merged
pescn merged 14 commits into
mainfrom
feat/multi-api-format-support
Jan 13, 2026
Merged

feat: Multi-API format support (OpenAI Chat, Response API, Anthropic)#43
pescn merged 14 commits into
mainfrom
feat/multi-api-format-support

Conversation

@pescn
Copy link
Copy Markdown
Contributor

@pescn pescn commented Jan 13, 2026

Summary

This PR implements full multi-API format support for NexusGate, enabling:

  • Downstream API Compatibility: Applications can call NexusGate using OpenAI Chat, OpenAI Response API, or Anthropic Messages API formats
  • Upstream Provider Support: NexusGate can forward requests to providers using any of the three API formats
  • Cross-format Conversion: Automatic format conversion between different API formats (e.g., Anthropic SDK → OpenAI upstream)

Key Changes

  • Adapter Pattern Architecture: Introduced a clean adapter pattern with:

    • Request adapters: Convert incoming requests to internal unified format
    • Response adapters: Convert internal format to client-expected format
    • Upstream adapters: Build requests for different provider types
  • New API Endpoints:

    • POST /v1/messages - Anthropic Messages API compatible endpoint
    • POST /v1/responses - OpenAI Response API compatible endpoint
  • Authentication: Support for both Authorization: Bearer (OpenAI) and x-api-key (Anthropic) headers

  • Database Schema: Extended to support new provider types and track request formats

  • Frontend: Added provider type selection and API version configuration

API Format Support Matrix

Downstream Format Upstream Provider
OpenAI Chat OpenAI, Anthropic, OpenAI Responses
OpenAI Response OpenAI, Anthropic, OpenAI Responses
Anthropic Messages OpenAI, Anthropic, OpenAI Responses

Commits (11 atomic commits)

  1. feat(adapters): add internal unified types and adapter factory
  2. feat(adapters): add request adapters for all API formats
  3. feat(adapters): add response adapters for all API formats
  4. feat(adapters): add upstream adapters and tool conversion utilities
  5. feat(db): extend schema for multi-API format support
  6. feat(api): add Anthropic Messages and OpenAI Response API endpoints
  7. refactor(api): update completions endpoint to use adapter pattern
  8. feat(auth): add x-api-key header support for Anthropic SDK
  9. feat(admin): update provider management for new provider types
  10. feat(ui): add provider type selection and API version config
  11. chore: add python_test_code to gitignore

Test plan

  • Run comprehensive E2E tests covering 12 scenarios (24 tests: streaming + non-streaming)
    • Baseline tests: Direct to OpenAI/Anthropic/Response API upstreams
    • Same-format proxy tests: Through NexusGate with matching formats
    • Cross-format conversion tests: Different downstream/upstream formats
  • All 24 tests passing
  • Manual testing of provider creation in UI
  • Verify database migration runs successfully

🤖 Generated with Claude Code

pescn and others added 11 commits January 13, 2026 22:18
- Define InternalMessage, InternalRequest, InternalResponse types
- Define InternalStreamChunk for streaming response handling
- Add UpstreamAdapter interface for provider communication
- Add RequestAdapter/ResponseAdapter interfaces for format conversion
- Implement adapter factory with getRequestAdapter, getResponseAdapter,
  getUpstreamAdapter functions

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Add request adapters to convert incoming requests to internal format:
- openai-chat.ts: OpenAI Chat Completion format
- openai-response.ts: OpenAI Response API format
- anthropic.ts: Anthropic Messages API format

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Add response adapters to convert internal responses to client formats:
- openai-chat.ts: Internal → OpenAI Chat Completion format
- openai-response.ts: Internal → OpenAI Response API format
- anthropic.ts: Internal → Anthropic Messages API format

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Add upstream adapters for building requests to different providers:
- openai.ts: Build requests for OpenAI-compatible APIs
- openai-responses.ts: Build requests for OpenAI Response API
- anthropic.ts: Build requests for Anthropic Claude API

Also adds tools.ts for cross-format tool/function calling conversion.

Includes SSE parsing fixes to handle both "data: " and "data:" formats
for compatibility with various providers (e.g., Alibaba DashScope).

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Add 'anthropic' and 'openai-responses' to provider_type enum
- Add api_version column to providers table (for Anthropic version header)
- Add request_format column to completions table
- Add raw_request JSONB column to store original request data

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Add POST /v1/messages endpoint for Anthropic Messages API compatibility
- Add POST /v1/responses endpoint for OpenAI Response API compatibility
- Register new routes in api/index.ts

Both endpoints support streaming and non-streaming modes with full
format conversion to/from internal representation.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Refactor /v1/chat/completions to use the new adapter pattern for
handling different upstream provider types (OpenAI, Anthropic,
OpenAI Responses).

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Support both authentication methods:
- Authorization: Bearer <token> (OpenAI style)
- x-api-key: <token> (Anthropic style)

This allows Anthropic SDK clients to authenticate with NexusGate
using their native authentication header format.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Support apiVersion field for Anthropic providers
- Update provider testing to handle different provider types
- Adjust model discovery for Anthropic and Response API providers

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Add 'Anthropic' and 'OpenAI Responses' options to provider type selector
- Add API Version input field for Anthropic providers
- Update provider columns to display new types
- Add i18n translations for new provider types (en-US, zh-CN)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Local Python test scripts for manual API testing.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Comment thread backend/src/adapters/upstream/openai-responses.ts
pescn and others added 3 commits January 13, 2026 22:58
Backend changes:
- Extract common API helpers to backend/src/utils/api-helpers.ts
  - extractUpstreamHeaders, selectModel, extractContentText
  - parseModelProvider, PROVIDER_HEADER, EXCLUDED_HEADERS
- Replace t.Unknown() with specific type schemas in API endpoints
  - completions.ts: add tStreamOptions schema
  - messages.ts: add Anthropic content block, tool, and metadata types
  - responses.ts: add Response API input, tool, and metadata types

Frontend changes:
- Extract PROVIDER_TYPES constant to frontend/src/constants/providers.ts
  - Add PROVIDER_TYPE_LABELS for display names
  - Add requiresApiVersion() and getApiVersionPlaceholder() helpers
- Update provider components to use shared constants

This reduces code duplication and improves type safety.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Add "reasoning" to ResponseApiOutput.type union (fixes TS2367)
- Add @ts-expect-error for TextDecoderStream.pipeThrough compatibility
  issue in 4 SSE parser locations (fixes TS2345)
- Remove unused @ts-expect-error directive in utils.ts (fixes TS2578)

Backend now passes tsc --noEmit with no errors.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Fix JSDoc tag in api-helpers.ts (changed @Provider to description)
- Remove unnecessary type argument in sse.ts
- Add @ts-expect-error for Eden type imports in frontend api.ts

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@pescn pescn merged commit 86bc85a into main Jan 13, 2026
@pescn pescn deleted the feat/multi-api-format-support branch January 24, 2026 22:54
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.

same model dynamic selection and QoS scoring [Feature] Add anthropic-like API Support

2 participants