Skip to content

支持 OpenAI Chat 兼容协议#99

Merged
claude-code-best merged 3 commits intomainfrom
feature/openai
Apr 3, 2026
Merged

支持 OpenAI Chat 兼容协议#99
claude-code-best merged 3 commits intomainfrom
feature/openai

Conversation

@claude-code-best
Copy link
Copy Markdown
Owner

@claude-code-best claude-code-best commented Apr 3, 2026

Summary by CodeRabbit

  • New Features

    • OpenAI Chat Completions compatibility and an "OpenAI Compatible" login option; provider selection persisted in settings.
  • Documentation

    • Added detailed OpenAI compatibility plan and updated README checklist.
  • Tests

    • Added extensive tests covering message/tool conversion, streaming adaptation, and model mapping.
  • Chores

    • Added OpenAI dev dependency and updated model configurations with OpenAI identifiers.

@mintlify
Copy link
Copy Markdown

mintlify bot commented Apr 3, 2026

Preview deployment for your docs. Learn more about Mintlify Previews.

Project Status Preview Updated (UTC)
ccb-863780bf 🟢 Ready View Preview Apr 3, 2026, 3:30 PM

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 3, 2026

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 99905766-4621-4e35-8d13-cef373a758a2

📥 Commits

Reviewing files that changed from the base of the PR and between 80af4fa and d250b26.

📒 Files selected for processing (1)
  • tests/integration/cli-arguments.test.ts

📝 Walkthrough

Walkthrough

The PR adds OpenAI Chat Completions compatibility: new routing to an OpenAI adapter, message/tool conversion utilities, an OpenAI client and stream adapter that emit Anthropic-style events, OAuth/settings UI to select OpenAI, model mapping, tests, docs, and a dev dependency for openai.

Changes

Cohort / File(s) Summary
CI & Docs
\.github/workflows/ci.yml, package.json, README.md, DEV-LOG.md, docs/plans/openai-compatibility.md
Switched CI runner to macos-latest, added openai devDependency, updated README checklist, added DEV-LOG entry and a detailed OpenAI compatibility design doc.
OAuth & Settings
src/components/ConsoleOAuthFlow.tsx, src/utils/settings/types.ts
Added openai_chat_api OAuth state and UI, persisted modelType (`'anthropic'
Provider & Model Configs
src/utils/model/providers.ts, src/utils/model/configs.ts
Added 'openai' to APIProvider and prioritize settings/env for provider selection; added openai ids to CLAUDE_* model config constants.
Core Routing
src/services/api/claude.ts
Added early branch in queryModel to dynamically delegate to OpenAI adapter when provider is openai, bypassing Claude-specific pipelines.
OpenAI Client & Model Resolution
src/services/api/openai/client.ts, src/services/api/openai/modelMapping.ts
New cached OpenAI client factory (getOpenAIClient, clearOpenAIClientCache) and resolveOpenAIModel implementing env-var precedence and Anthropic→OpenAI mapping rules.
Message & Tool Conversion
src/services/api/openai/convertMessages.ts, src/services/api/openai/convertTools.ts
Added converters: anthropicMessagesToOpenAI and anthropicToolsToOpenAI plus anthropicToolChoiceToOpenAI to transform messages, tool schemas, and tool-choice semantics for OpenAI.
OpenAI Adapter & Stream Adaptation
src/services/api/openai/index.ts, src/services/api/openai/streamAdapter.ts
Implemented queryModelOpenAI to call OpenAI streaming completions and adaptOpenAIStreamToAnthropic to translate OpenAI SSE chunks into Anthropic BetaRawMessageStreamEvent sequences, including thinking/text/tool blocks, stop_reason mapping, and prompt-cache usage.
Tests
src/services/api/openai/__tests__/convertMessages.test.ts, .../convertTools.test.ts, .../modelMapping.test.ts, .../streamAdapter.test.ts
Added test suites covering message/tool conversion, model resolution with env precedence, and stream adaptation behavior (text, thinking, tool_calls, finish reasons, prompt-caching).
Minor Test Changes
tests/integration/cli-arguments.test.ts
Replaced local require of Command with ES module import from @commander-js/extra-typings.

Sequence Diagram

sequenceDiagram
    actor User
    participant OAuth as OAuth Flow
    participant Settings as Settings
    participant QueryAPI as queryModel (dispatcher)
    participant OpenAIAdapter as queryModelOpenAI
    participant OpenAIAPI as OpenAI API
    participant StreamAdapter as Stream Adapter
    participant Client as Client/UI

    User->>OAuth: select "OpenAI Compatible"
    OAuth->>Settings: updateSettingsForSource(modelType: 'openai')
    Settings-->>OAuth: saved

    Client->>QueryAPI: queryModel(messages, tools, ...)
    QueryAPI->>QueryAPI: getAPIProvider() -> 'openai'
    QueryAPI->>OpenAIAdapter: queryModelOpenAI(...)
    OpenAIAdapter->>OpenAIAdapter: convert messages & tools
    OpenAIAdapter->>OpenAIAPI: chat.completions.create(stream: true)
    OpenAIAPI-->>StreamAdapter: stream chunks (SSE)
    StreamAdapter->>OpenAIAdapter: BetaRawMessageStreamEvent (text/thinking/tool deltas)
    OpenAIAdapter->>OpenAIAdapter: accumulate deltas -> AssistantMessage
    OpenAIAdapter-->>Client: yield AssistantMessage and StreamEvent
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~75 minutes

Poem

🐰 I hopped through routes both old and new,
I stitched messages, tools, and streams in two;
From Anthropic shapes to OpenAI's call,
I bundled thinking, text, and tool-use all—
A rabbit's bridge, so both worlds can chat anew.

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 47.62% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title '支持 OpenAI Chat 兼容协议' (Support OpenAI Chat Compatible Protocol) accurately describes the main objective of the pull request—adding OpenAI Chat Completions API compatibility throughout the codebase.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/openai

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

@claude-code-best claude-code-best merged commit 00b044e into main Apr 3, 2026
7 checks passed
@claude-code-best claude-code-best deleted the feature/openai branch April 3, 2026 15:39
@hushicai
Copy link
Copy Markdown

hushicai commented Apr 3, 2026

deepseek-chat │ Context 0% (0/200k)

这个上下文大小不对,有办法可以配置么

@hushicai
Copy link
Copy Markdown

hushicai commented Apr 3, 2026

deepseek-chat │ Context % (0/200k) Not logged in · Run /login

另外已经配置好了,右边还显示not logged in,应该是没有配置anthropic_xx,改为openai_xxx字段导致的。

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.

2 participants