Skip to content

feat: add OpenAI API integration support#132

Closed
xuzhongpeng wants to merge 1 commit intoclaude-code-best:mainfrom
xuzhongpeng:feat/support-openai-api
Closed

feat: add OpenAI API integration support#132
xuzhongpeng wants to merge 1 commit intoclaude-code-best:mainfrom
xuzhongpeng:feat/support-openai-api

Conversation

@xuzhongpeng
Copy link
Copy Markdown

@xuzhongpeng xuzhongpeng commented Apr 5, 2026

Add comprehensive OpenAI API adapter to support OpenAI-compatible API endpoints, enabling users to use OpenAI models alongside Claude models.

Changes:

  • Add OpenAI API adapter with streaming support and tool calling
  • Add OpenAI integration documentation with usage examples
  • Update model configuration system to support OpenAI models
  • Enhance authentication flow to handle OpenAI API keys
  • Add model provider detection and routing logic

Closes #48

Summary by CodeRabbit

  • New Features

    • Added OpenAI API compatibility mode, allowing use of OpenAI-compatible providers through environment variable configuration without code changes.
    • Support for multiple providers including DeepSeek, Azure OpenAI, and advanced models (o1/o3).
  • Documentation

    • Added comprehensive configuration guide for OpenAI API mode, covering both JSON and command-line setup across all platforms.
  • Improvements

    • Enhanced message normalization and null-safety handling.

Add comprehensive OpenAI API adapter to support OpenAI-compatible API endpoints, enabling users to use OpenAI models alongside Claude models.

Changes:
- Add OpenAI API adapter with streaming support and tool calling
- Add OpenAI integration documentation with usage examples
- Update model configuration system to support OpenAI models
- Enhance authentication flow to handle OpenAI API keys
- Add model provider detection and routing logic

Closes claude-code-best#48

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 5, 2026

Caution

Review failed

Pull request was closed or merged during review

📝 Walkthrough

Walkthrough

This PR implements OpenAI-compatible API mode support, allowing users to configure and use OpenAI API endpoints instead of Anthropic's. It introduces environment variable configuration, an adapter translating Anthropic message format to OpenAI, model identifier mappings, and comprehensive documentation for multi-platform setup.

Changes

Cohort / File(s) Summary
Documentation & Setup
README.md, docs/features/openai.mdx, package.json
Added README checklist entry and comprehensive documentation for OpenAI mode configuration via ~/.claude/settings.json or shell environment variables (macOS/Linux/Windows), with examples for DeepSeek, Azure OpenAI, and large output models. Added openai ^6.0.0 SDK dependency.
Core API Integration
src/services/api/client.ts, src/services/api/openaiAdapter.ts
Updated client factory to conditionally import and return OpenAI adapter when CLAUDE_CODE_USE_OPENAI is set. New adapter file implements Anthropic-compatible API surface by translating message formats, tool schemas, streaming deltas, and stop-reason mappings between OpenAI Chat Completions and Anthropic message protocols.
Model Configuration
src/utils/model/configs.ts, src/utils/model/deprecation.ts, src/utils/model/providers.ts
Added OpenAI model identifier field (e.g., openai: 'claude-3-5-sonnet') to 11 model config constants; added deprecation tracking for OpenAI provider; extended APIProvider type union to include 'openai' and added detection logic in getAPIProvider().
Utility Functions
src/services/api/claude.ts, src/utils/auth.ts, src/utils/messages.ts
Updated max output token resolution to read from OPENAI_MAX_TOKENS when OpenAI mode is active; short-circuit authentication when OpenAI API key is present; added defensive null/undefined guard in message processing and fallback for unrecognized message types.

Sequence Diagram

sequenceDiagram
    actor User
    participant CodeClient as Code Client<br/>(CLAUDE_CODE_USE_OPENAI=true)
    participant Adapter as OpenAI Adapter
    participant OpenAI as OpenAI API

    User->>CodeClient: Call message creation
    CodeClient->>CodeClient: Check CLAUDE_CODE_USE_OPENAI env
    CodeClient->>Adapter: Instantiate adapter with<br/>apiKey & baseURL
    
    CodeClient->>Adapter: createMessage(messages, tools, etc)
    Adapter->>Adapter: Convert Anthropic message<br/>format to OpenAI format
    Adapter->>Adapter: Map tool schemas &<br/>system prompts
    
    Adapter->>OpenAI: POST /chat/completions<br/>(with stream or non-stream)
    OpenAI-->>Adapter: Response deltas/completion
    
    Adapter->>Adapter: Convert OpenAI response<br/>to Anthropic message
    Adapter->>Adapter: Map stop reasons &<br/>assemble content blocks
    Adapter-->>CodeClient: Anthropic-compatible<br/>message object
    CodeClient-->>User: Processed result
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

Suggested reviewers

  • KonghaYao

Poem

🐰 A rabbit bounces through code so neat,
OpenAI doors now open sweet!
From Anthropic's path to broader ways,
New adapters bridge through API maze.
Configuration dances—settings dance too,
Multi-platform magic, fresh and new! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 38.89% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely summarizes the main change: adding OpenAI API integration support to the project.
Linked Issues check ✅ Passed The PR successfully implements the core requirement from issue #48: enabling support for non-Anthropic model providers via API key integration, specifically with OpenAI-compatible APIs.
Out of Scope Changes check ✅ Passed All changes are directly related to the OpenAI API integration feature. Minor defensive improvements (null guards, default cases) are reasonable quality-of-life enhancements supporting the feature's robustness.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
⚔️ Resolve merge conflicts
  • Resolve merge conflict in branch feat/support-openai-api

Comment @coderabbitai help to get the list of available commands and usage tips.

@xuzhongpeng
Copy link
Copy Markdown
Author

前天提交了一半忘提pr了,看到最新代码以及已经更新该功能了,快得离谱,现关闭pr

@xuzhongpeng xuzhongpeng closed this Apr 5, 2026
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.

添加对除了 anthropic 之外的模型兼容

1 participant