Skip to content

Feat/chatgpt web reverse channel - #5257

Closed
kill136 wants to merge 6 commits into
QuantumNous:mainfrom
kill136:feat/chatgpt-web-reverse-channel
Closed

Feat/chatgpt web reverse channel#5257
kill136 wants to merge 6 commits into
QuantumNous:mainfrom
kill136:feat/chatgpt-web-reverse-channel

Conversation

@kill136

@kill136 kill136 commented Jun 2, 2026

Copy link
Copy Markdown

⚠️ 提交说明 / PR Notice

Important

  • 请提供人工撰写的简洁摘要,避免直接粘贴未经整理的 AI 输出。

📝 变更描述 / Description

(简述:做了什么?为什么这样改能生效?请基于你对代码逻辑的理解来写,避免粘贴未经整理的内容)

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix) - 请关联对应 Issue,避免将设计取舍、理解偏差或预期不一致直接归类为 bug
  • ✨ 新功能 (New feature) - 重大特性建议先通过 Issue 沟通
  • ⚡ 性能优化 / 重构 (Refactor)
  • 📝 文档更新 (Documentation)

🔗 关联任务 / Related Issue

  • Closes # (如有)

✅ 提交前检查项 / Checklist

  • 人工确认: 我已亲自整理并撰写此描述,没有直接粘贴未经处理的 AI 输出。
  • 非重复提交: 我已搜索现有的 IssuesPRs,确认不是重复提交。
  • Bug fix 说明: 若此 PR 标记为 Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。
  • 变更理解: 我已理解这些更改的工作原理及可能影响。
  • 范围聚焦: 本 PR 未包含任何与当前任务无关的代码改动。
  • 本地验证: 已在本地运行并通过测试或手动验证,维护者可以据此复核结果。
  • 安全合规: 代码中无敏感凭据,且符合项目代码规范。

📸 运行证明 / Proof of Work

(请在此粘贴截图、关键日志或测试报告,以证明变更生效)

Summary by CodeRabbit

New Features

  • ChatGPT Web channel integration – Added support for ChatGPT Web as a new channel option, enabling chat completions and responses endpoints
  • Request/response body logging – Introduced configurable logging of full API request and response payloads with optional storage control
  • Request detail viewer – Added modal interface to inspect captured request and response bodies with JSON formatting and preview capabilities
  • Enhanced usage logs – New "Request Detail" column in logs with admin and user-specific API endpoints for retrieving logged request information

kill136 and others added 6 commits April 7, 2026 02:33
Add a new RequestLog table to store complete request and response bodies
for API relay calls. Includes a settings toggle, backend API endpoints,
response capture middleware, and a frontend modal for previewing logged
content with JSON formatting.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
When LOG_SQL_DSN is not set, LOG_DB = DB but migrateLOGDB() was never
called, so the request_logs table was never created, causing "table
doesn't exist" errors.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Two issues fixed:
1. MySQL TEXT type is 64KB max, too small for large request bodies.
   Changed to MEDIUMTEXT (16MB) with explicit ALTER TABLE migration.
2. Response body was never captured because middleware set the context
   key after c.Next() returned, but handler's defer runs before that.
   Now store the writer reference in context so handler can read it
   directly during defer.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Increase request/response body capture and truncation limit from
  64KB to 4MB to avoid truncation of large payloads
- Replace plain text JSON display with interactive collapsible tree
  view with syntax highlighting and expand/collapse toggles

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
新增渠道类型 ChannelTypeChatGPTWeb(58)/APITypeChatGPTWeb,用 ChatGPT 订阅账号的
OAuth access_token 模拟网页前端调用 chatgpt.com/backend-api/conversation,下游同时
兼容 OpenAI /v1/chat/completions 与 /v1/responses(均支持流式与非流式)。

实现要点(relay/channel/chatgpt_web/):
- web_key.go:Key 支持直接粘原始 access_token,account_id 自动从 JWT 提取、
  device_id 由 account_id 确定性派生;也支持 JSON 形式覆盖
- pow.go:sentinel chat-requirements + Proof-of-Work(SHA3-512)
- convert.go:OpenAI messages -> conversation 体;解析 ChatGPT v1 delta SSE
  (新消息为裸 {"v":{message}} 快照、正文走 patch/append 到 parts/0)转 chat 格式
- responses.go:/v1/responses 输入解析与 responses 事件合成
- adaptor.go:SetupRequestHeader 内完成 sentinel+PoW,DoRequest 复用
  channel.DoApiRequest,DoResponse 按 RelayMode 分流

集成:constant/channel.go、constant/api_type.go、common/api_type.go、
relay/relay_adaptor.go、web/src/constants/channel.constants.js。

已对真实 chatgpt.com 端到端实测通过:chat="21"、responses="4"。

已知约束:token 不可刷新(账号风控),约 10 天过期需手动从浏览器重抠更新;
Turnstile 当前为建议未强制,若上游转为强制则 conversation 直接报错(不加降级)。
仅支持文本输入;usage 为本地估算(上游不返回 token 数)。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 4c279418-3904-4874-8081-41c72302ce56

📥 Commits

Reviewing files that changed from the base of the PR and between 7aaa533 and e27b7c6.

📒 Files selected for processing (30)
  • common/api_type.go
  • common/constants.go
  • constant/api_type.go
  • constant/channel.go
  • controller/relay.go
  • controller/request_log.go
  • middleware/request_log.go
  • model/main.go
  • model/option.go
  • model/request_log.go
  • relay/channel/chatgpt_web/adaptor.go
  • relay/channel/chatgpt_web/constants.go
  • relay/channel/chatgpt_web/convert.go
  • relay/channel/chatgpt_web/live_test.go
  • relay/channel/chatgpt_web/pow.go
  • relay/channel/chatgpt_web/responses.go
  • relay/channel/chatgpt_web/web_key.go
  • relay/relay_adaptor.go
  • router/api-router.go
  • router/relay-router.go
  • service/request_log.go
  • web/src/components/table/usage-logs/UsageLogsColumnDefs.jsx
  • web/src/components/table/usage-logs/UsageLogsTable.jsx
  • web/src/components/table/usage-logs/index.jsx
  • web/src/components/table/usage-logs/modals/RequestLogDetailModal.jsx
  • web/src/constants/channel.constants.js
  • web/src/hooks/usage-logs/useUsageLogsData.jsx
  • web/src/i18n/locales/en.json
  • web/src/i18n/locales/zh-CN.json
  • web/src/pages/Setting/Operation/SettingsLog.jsx

Walkthrough

This PR adds ChatGPT Web as a new relay channel (type 58) with full SSE-based request/response conversion, and introduces a comprehensive request/response logging system that captures full API payloads into a database with REST APIs and a frontend JSON viewer for inspection.

Changes

ChatGPT Web Channel + Request Logging System

Layer / File(s) Summary
Channel and API Type Constants
constant/api_type.go, constant/channel.go, common/api_type.go, common/constants.go
New constant ChannelTypeChatGPTWeb (value 58) with base URL https://chatgpt.com and channel name mapping, plus APITypeChatGPTWeb iota entry and configuration flag LogRequestBodyEnabled defaulting to false.
ChatGPT Web Channel Adaptor and Models
relay/channel/chatgpt_web/adaptor.go, relay/channel/chatgpt_web/constants.go, relay/channel/chatgpt_web/convert.go, relay/channel/chatgpt_web/web_key.go, relay/channel/chatgpt_web/pow.go, relay/channel/chatgpt_web/responses.go
Complete ChatGPT Web adaptor implementing OpenAI↔SSE delta format conversion, web key parsing (access token, account ID, device ID extraction from JWT), proof-of-work challenge solving, and dual-mode support for /v1/chat/completions and /v1/responses endpoints with both streaming and non-streaming handlers.
ChatGPT Web Integration and Tests
relay/relay_adaptor.go, relay/channel/chatgpt_web/live_test.go
Registers chatgpt_web.Adaptor in the relay adaptor factory for APITypeChatGPTWeb, and provides live integration tests for conversation/responses endpoints plus unit tests for request conversion.
Request/Response Logging: Capture, Storage, Service
middleware/request_log.go, controller/relay.go, model/request_log.go, model/main.go, service/request_log.go
Request body capture in relay handler, response body capture in middleware (4MB capped with truncation marker), database model for persisting logs with user/model/token/channel/endpoint/status metadata, and async service layer for persistence with body truncation.
Request Log API Endpoints and Option Wiring
controller/request_log.go, router/api-router.go, router/relay-router.go, model/option.go
Admin and user-scoped REST endpoints (GET /api/log/detail, GET /api/log/self/detail) for retrieving request logs, integration with configuration option system to sync LogRequestBodyEnabled, and middleware registration on relay routes.
Frontend Request Log Viewer and Settings
web/src/components/table/usage-logs/UsageLogsColumnDefs.jsx, web/src/components/table/usage-logs/UsageLogsTable.jsx, web/src/components/table/usage-logs/index.jsx, web/src/components/table/usage-logs/modals/RequestLogDetailModal.jsx, web/src/hooks/usage-logs/useUsageLogsData.jsx, web/src/constants/channel.constants.js, web/src/pages/Setting/Operation/SettingsLog.jsx, web/src/i18n/locales/*
Modal with JSON tree viewer for request/response bodies with syntax coloring and depth-based expansion, column additions to logs table with click-to-inspect, hook state management, channel 58 option with teal color label, settings toggle with storage warning, and localization keys in English and Simplified Chinese.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

  • QuantumNous/new-api#2780: The main PR's request logging flow in controller/relay.go depends on common.GetBodyStorage/BodyStorage to capture and reread the request body, which is introduced by PR #2780's disk-backed request body storage implementation.
  • QuantumNous/new-api#1920: Both PRs extend the same channel↔API type plumbing (updating the ChannelType2APIType switch in common/api_type.go and adding new constant/api_type.go iota entries for the new channel types), so the changes are code-level related.
  • QuantumNous/new-api#2644: Both PRs change controller/relay.go's Relay request-handling flow—main PR adds request/response logging when enabled, while retrieved PR extends the relay mode switch to support /v1/responses/compact routing—so they're related at the Relay handler level.

Suggested labels

ready to merge

Suggested reviewers

  • Calcium-Ion
  • creamlike1024

Poem

🐰 A web chat channel joins the fold,
With SSE deltas smooth and bold,
Requests and responses captured whole,
JSON trees expand on viewer's scroll,
Hopping through logs to find your goal!

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
⚔️ Resolve merge conflicts
  • Resolve merge conflict in branch feat/chatgpt-web-reverse-channel

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.

@kill136 kill136 closed this Jun 2, 2026
@kill136
kill136 deleted the feat/chatgpt-web-reverse-channel branch June 2, 2026 10:23
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