Skip to content

feat: 添加 openrouter-enterprise 支持 - #1897

Merged
Calcium-Ion merged 4 commits into
mainfrom
openrouter-enterprise
Sep 28, 2025
Merged

Calcium-Ion merged 4 commits into
mainfrom
openrouter-enterprise

Conversation

@creamlike1024

@creamlike1024 creamlike1024 commented Sep 28, 2025 •

Copy link
Copy Markdown
Collaborator
image

response:

{
  "data": {
    "choices": [
      {
        "finish_reason": "stop",
        "index": 0,
        "logprobs": null,
        "message": {
          "content": "The meaning of life is a profound philosophical question that people have contemplated throughout human history. There's no single universally agreed-upon answer.\n\nSome find meaning in relationships and connection with others. Some discover purpose through religious or spiritual beliefs. Others create meaning through their work, creative pursuits, or contributing to causes larger than themselves.\n\nPhilosophers have offered various perspectives: finding joy in the journey itself (Camus), creating your own meaning (existentialists), maximizing happiness (utilitarians), or fulfilling your unique potential.\n\nPerhaps what makes this question so enduring is that each person ultimately determines what gives their own life significance and purpose.",
          "reasoning": null,
          "refusal": null,
          "role": "assistant"
        },
        "native_finish_reason": "stop"
      }
    ],
    "created": 1759022987,
    "id": "gen-1759022987-z5EYu8hxawTaZKOrsqok",
    "model": "anthropic/claude-3.7-sonnet",
    "object": "chat.completion",
    "provider": "Anthropic",
    "usage": {
      "completion_tokens": 138,
      "completion_tokens_details": {
        "image_tokens": 0,
        "reasoning_tokens": 0
      },
      "cost": 0.0001056,
      "cost_details": {
        "upstream_inference_completions_cost": 0.00207,
        "upstream_inference_cost": 0.002112,
        "upstream_inference_prompt_cost": 0.000042
      },
      "is_byok": true,
      "prompt_tokens": 14,
      "prompt_tokens_details": {
        "audio_tokens": 0,
        "cached_tokens": 0
      },
      "total_tokens": 152
    }
  },
  "success": true
}

Summary by CodeRabbit

  • New Features

    • Add option to mark OpenRouter channels as "enterprise account" in the channel create/edit modal.
  • Bug Fixes

    • Fix handling of enterprise-wrapped OpenRouter responses so channels using enterprise accounts return correct results.
  • Chores

    • Added backend support and logging for OpenRouter enterprise responses and persisted enterprise flag in channel settings.

@coderabbitai

coderabbitai Bot commented Sep 28, 2025 •

Copy link
Copy Markdown
Contributor

Walkthrough

Adds detection and parsing for OpenRouter enterprise-wrapped responses in the OpenAI relay handler, a new OpenRouter enterprise DTO, a channel settings flag openrouter_enterprise with helper, a request-error log call, and frontend UI/state to toggle and persist the enterprise flag.

Changes

Cohort / File(s) Summary
OpenAI Relay Handler
relay/channel/openai/relay-openai.go
When ChannelTypeOpenRouter and enterprise mode is enabled, unmarshal upstream into OpenRouterEnterpriseResponse; if Success is true use Data as the response body (then unmarshal into the normal simple response), otherwise log and return an OpenAI error. Falls back to previous path when not enterprise.
OpenRouter DTO addition
relay/channel/openrouter/dto.go
Adds OpenRouterEnterpriseResponse type with Data json.RawMessage and Success bool; imports encoding/json.
Channel settings DTO
dto/channel_settings.go
Adds OpenRouterEnterprise *bool to ChannelOtherSettings with JSON tag openrouter_enterprise,omitempty and method IsOpenRouterEnterprise() bool.
Request logging
relay/channel/api_request.go
Logs do request failed: <err> via logger.LogError before returning DoRequestFailed on request errors.
Frontend: Edit Channel Modal
web/src/components/table/channels/modals/EditChannelModal.jsx
Adds is_enterprise_account input/state, reads/writes settings.openrouter_enterprise, shows a switch for OpenRouter channels (type 20), syncs/resets logic, removes the public field before submitting to backend.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Client
  participant Handler as OpenaiHandler
  participant Upstream as OpenRouter API

  Client->>Handler: Request (channel=OpenRouter)
  Handler->>Upstream: Forward upstream request
  Upstream-->>Handler: HTTP response (body)

  alt Enterprise wrapper path
    note over Handler #D6EAF8: Attempt enterprise unwrap
    Handler->>Handler: Unmarshal into OpenRouterEnterpriseResponse
    alt enterpriseResponse.Success == true
      Handler->>Handler: Replace responseBody with enterpriseResponse.Data
      Handler->>Handler: Unmarshal Data into simpleResponse
      Handler-->>Client: Return parsed simpleResponse
    else enterpriseResponse.Success == false
      Handler->>Handler: Log error and return OpenAI error
      Handler-->>Client: Error
    end
  else Non-enterprise / fallback
    Handler->>Handler: Unmarshal directly into simpleResponse (existing flow)
    Handler-->>Client: Return parsed simpleResponse
  end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Poem

I nibbled bytes by lantern's glow,
Found an enterprise wrapped below.
I hopped, I unwrapped, then set Data free—
Responses hopped back swift to me.
Toggle flipped, logs sang cheer, integrations near. 🐇✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ 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 succinctly states the primary change of this pull request, namely adding support for the openrouter enterprise feature, and is clear and specific without unnecessary detail or noise.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch openrouter-enterprise

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b91eb8a and 7fe969c.

📒 Files selected for processing (1)
  • relay/channel/openai/relay-openai.go (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • relay/channel/openai/relay-openai.go

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6659a8a and ee7ce5a.

📒 Files selected for processing (2)
  • relay/channel/openai/relay-openai.go (2 hunks)
  • relay/channel/openrouter/dto.go (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
relay/channel/openai/relay-openai.go (3)
constant/channel.go (1)
  • ChannelTypeOpenRouter (24-24)
relay/channel/openrouter/dto.go (1)
  • OpenRouterEnterpriseResponse (13-16)
common/json.go (1)
  • Unmarshal (8-10)

Comment thread relay/channel/openai/relay-openai.go Resolved

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ee7ce5a and b91eb8a.

📒 Files selected for processing (4)
  • dto/channel_settings.go (1 hunks)
  • relay/channel/api_request.go (1 hunks)
  • relay/channel/openai/relay-openai.go (2 hunks)
  • web/src/components/table/channels/modals/EditChannelModal.jsx (7 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-08-27T02:15:25.448Z
Learnt from: AAEE86
PR: QuantumNous/new-api#1658
File: web/src/components/table/channels/modals/EditChannelModal.jsx:555-569
Timestamp: 2025-08-27T02:15:25.448Z
Learning: In EditChannelModal.jsx, the database stores mapped keys (display names) in the models field after applying model mapping transformations. When loading a channel, data.models contains the mapped keys, not the original model names. The filtering logic if (data.models.includes(key)) in the initialization is correct.

Applied to files:

  • web/src/components/table/channels/modals/EditChannelModal.jsx
🧬 Code graph analysis (3)
relay/channel/api_request.go (1)
logger/logger.go (1)
  • LogError (63-65)
relay/channel/openai/relay-openai.go (6)
constant/channel.go (1)
  • ChannelTypeOpenRouter (24-24)
dto/channel_settings.go (1)
  • ChannelOtherSettings (19-23)
relay/channel/openrouter/dto.go (1)
  • OpenRouterEnterpriseResponse (13-16)
common/json.go (1)
  • Unmarshal (8-10)
types/error.go (2)
  • NewOpenAIError (215-238)
  • ErrorCodeBadResponseBody (68-68)
logger/logger.go (1)
  • LogError (63-65)
web/src/components/table/channels/modals/EditChannelModal.jsx (1)
web/src/components/table/channels/modals/EditTagModal.jsx (2)
  • inputs (73-73)
  • handleInputChange (77-129)

Comment thread relay/channel/openai/relay-openai.go Resolved
@Calcium-Ion
Calcium-Ion merged commit 045ba23 into main Sep 28, 2025
@Calcium-Ion
Calcium-Ion deleted the openrouter-enterprise branch October 3, 2025 13:52
x22x22 pushed a commit to x22x22/new-api that referenced this pull request Apr 24, 2026
…prise

feat: 添加 openrouter-enterprise 支持
jiutubaba pushed a commit to jiutubaba/fx-api that referenced this pull request May 17, 2026
…iate-rebate

feat: 新增邀请返利功能,并支持后台配置返利比例
salem-2007 added a commit to salem-2007/new-api that referenced this pull request Sep 10, 2026
…prise

feat: 添加 openrouter-enterprise 支持
junjundesk pushed a commit to junjundesk/new-api that referenced this pull request Oct 3, 2026
…prise

feat: 添加 openrouter-enterprise 支持
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