Skip to content

feat(provider): add Custom API provider with console form - #672

Merged
caigee-cmd merged 6 commits into
caigee-cmd:mainfrom
guyu233:feat/custom-api-provider
Dec 6, 2026
Merged

caigee-cmd merged 6 commits into
caigee-cmd:mainfrom
guyu233:feat/custom-api-provider

Conversation

@guyu233

@guyu233 guyu233 commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Add a Custom API provider: one account is one endpoint (base URL + key), proxied directly upstream with no reverse proxy. Upstream wire protocol can be OpenAI Chat Completions, Anthropic Messages, or Responses; the catalog comes from /models or a manual model list, and models are routed as custom/<model>.
  • Console: the "Add account" modal offers a Custom API form (base URL, key, upstream protocol, optional model list) with bilingual labels and a dedicated provider mark.
  • Import/export: custom credentials use the custom-api-v1 format; the import detector maps that format to the custom provider.

Implementation

  • Backend adapter in internal/providers/custom/ (catalog, chat/stream relay, credential validation, quota/outcome/error mapping, import/export), wired into internal/providers/registry.go and internal/app/app.go alongside the existing PAT-style providers.
  • Shared relay helpers in internal/translate/: BuildUpstreamBody, RelayUpstreamStream, and the UpstreamProtocol type.
  • Frontend: provider family/label helpers, CustomMark, the modal custom form and guards, and English/Chinese i18n entries.
  • Bilingual unreleased changelog fragment added at changelog/unreleased/custom-api-provider.md.

Test plan

  • go test ./...
  • go vet ./...
  • cd worker && npm test (covered by CI)
  • cd frontend && npm run build && npm run lint
  • Added changelog/unreleased/<slug>.md, or this PR has no user-facing change
  • No tokens, auth blobs, raw captures, or host details in the diff

guyu233 and others added 3 commits December 5, 2026 19:35
- Backend: custom adapter that proxies OpenAI Chat Completions,
  Anthropic Messages, or Responses upstreams directly (no reverse proxy);
  catalog from /models or a manual model list; models routed as
  custom/<model>.
- Console: Add-account Custom API form (base URL, key, upstream protocol,
  optional model list) with bilingual labels and a dedicated provider mark.
- Import detection maps custom-api-v1 credentials to the custom provider.
- Add bilingual unreleased changelog fragment.

@caigee-cmd caigee-cmd left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

感谢贡献这个 Custom API 供应商!整体结构很清晰:一个账号对应一个端点,进程内直连,不起子进程,也没有碰 store / executor / runtime 的边界,和现有 in-process provider 的写法一致。

我在本地把最新 main 合进来后跑了 go build ./...,以及 internal/providers/...、internal/translate、internal/control、internal/executor、internal/gateway 的测试,全部通过。我也确认了这个 PR 没有破坏性改动:没有新的 migration,没有修改任何现有 provider,translate 里只新增导出符号,custom-api-v1 的导入格式也不会被其他 provider 认领。

有几处我觉得合并前最好处理一下,具体位置和改法都写在行内评论里了。前三条会影响 Anthropic 和 OpenAI 两种协议的实际可用性,所以先标成 Request changes,改完我这边很快再看一遍:

  1. Anthropic 协议拉 /models 时没带 Anthropic 的鉴权头(catalog.go),没手填模型时 Probe 会一直失败。
  2. 手填了模型,/models 返回 401/403 时 Probe 仍判定不可用(client.go)。
  3. OpenAI 协议流式请求缺少 stream_options.include_usage(upstream_body.go),token 用量大概率记成 0。
  4. Classify 先匹配正文关键词再看状态码(errors.go),容易把普通请求错误误判成账号鉴权或额度问题,导致账号被冷却。
  5. changelog 写了"或自动",但 UI 里没有 auto 选项(AddAccountModal.tsx / changelog)。
  6. 提示文案没有标"实验性"(messages.ts)。按 AGENTS.md,除 Qoder、WorkBuddy、Trae CN Work 外的 provider 都是实验性,其他 provider 的 hint 都以 Experimental. / 实验性。 开头。

另外还有几处小问题,不阻塞合并,顺手改掉就更好了:

  • chat.go 的 stripProviderPrefix 里 "custom/auto/" 永远匹配不到,因为 "custom/" 排在前面会先命中。可以删掉这一项,或者把它挪到列表最前面。
  • chat.go 的 onlyModel 注释说"手填列表只有一个模型时生效",但 Models() 遇到手填列表会直接返回,不写 c.catalogs,所以这个分支对手填列表其实不生效。详见行内评论。
  • catalog.go 的 lookupModel 目前没有调用方,可以先删掉。
  • RelayUpstreamStream 的 default 分支(io.Copy)在当前调用路径里走不到,因为 OpenAI 协议在 chat.go 里已经单独处理了。保留也可以,但建议注释说明一下。
  • Anthropic 请求体会同时转发 temperature 和 top_p,部分较新的 Claude 模型不允许两者同时出现。可以考虑两者都有时只保留 temperature。

再次感谢,这个功能很实用,期待合并 🙏

Comment thread internal/providers/custom/catalog.go Resolved
Comment thread internal/providers/custom/client.go Resolved
Comment thread internal/translate/upstream_body.go Resolved
Comment thread internal/providers/custom/errors.go Resolved
Comment thread frontend/src/i18n/messages.ts Resolved
Comment thread internal/providers/custom/chat.go Resolved
- catalog: use Anthropic auth headers for GET /models when the protocol is anthropic
- probe: treat 401/403 on /models as usable when a manual catalog is set
- openai wire: request stream_options.include_usage so token counts are recorded
- classify: status code first, body keywords only refine ambiguous statuses
- anthropic wire: keep temperature and drop top_p when both are sent
- chat: single manual model fills a missing request model; lookupModel removed
- console: mark the provider experimental; changelog no longer mentions auto
@guyu233

guyu233 commented Dec 5, 2026

Copy link
Copy Markdown
Contributor Author

已按评论全部改完并推了新提交(428fdebe),并把最新 main 合了进来。逐条对应:

  1. catalog.go:按账号协议选鉴权头,Anthropic 账号拉 /models 带 x-api-key + anthropic-version;新增 TestCatalogUsesAnthropicAuthHeaders。
  2. client.go:catalogMissing 改名 catalogUnusable,401/403/404/405 都放行(手填列表时);TestProbeManualCatalogWithoutModelsRoute 覆盖这四种状态。
  3. upstream_body.go:OpenAI 流式请求加 stream_options.include_usage,并在 TestChatNonStreamOpenAI 断言。
  4. errors.go:改为先看状态码,正文关键词只在 403/429/5xx/0 时补充判断;新增 Classify(400,"insufficient max_tokens")、Classify(400,"authentication header format is invalid")、403 带 quota 正文、5xx 带余额正文的用例。
  5. AddAccountModal.tsx / changelog:采用第二种改法,UI 不暴露 auto,changelog 中英文都删掉了"或自动 / or auto"。
  6. messages.ts:中英文 hint 分别加 Experimental. / 实验性。。

小问题也一并处理了:

  • stripProviderPrefix:custom/auto/ 移到列表最前,并补了 custom/auto/anthropic/... 的用例。
  • chat.go:手填单个模型时直接用 credential.Models 兜底,onlyModel 只负责抓取到的单模型 catalog,注释同步更新,并加了单模型兜底测试。
  • lookupModel 已删除。
  • RelayUpstreamStream 的 default 分支加了说明注释。
  • Anthropic 请求体同时有 temperature 和 top_p 时只保留 temperature,另有测试覆盖只传 top_p 的情况。

本地 go build ./...、go test ./...、npm run sync、npm run lint 均通过。

@caigee-cmd 麻烦再看一遍 🙏

@caigee-cmd
caigee-cmd merged commit a4bc2dd into caigee-cmd:main Dec 6, 2026
6 checks passed
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