Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions changelog/unreleased/custom-api-provider.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
### English

- Add a Custom API provider: one account is one endpoint (base URL + key), with OpenAI Chat Completions, Anthropic Messages, or Responses as the upstream wire and no reverse proxy.
- Custom endpoints get their catalog from `/models` or a manual model list, and their models are routed as `custom/<model>`.
- Console: "Add account" offers a Custom API form — base URL, key, the upstream protocol (OpenAI, Anthropic, or Responses), and an optional model list.

### 中文

- 新增「自定义 API」供应商:一个账号就是一个端点(Base URL + 密钥),上游协议可选 OpenAI Chat Completions、Anthropic Messages 或 Responses,直连不走反代。
- 自定义端点按 `/models` 或手填列表生成 catalog,模型按 `custom/<model>` 路由。
- 控制台:「添加账号」新增「自定义 API」表单 —— Base URL、密钥、上游协议(OpenAI、Anthropic、Responses)以及可选的模型列表。
92 changes: 88 additions & 4 deletions frontend/src/components/AddAccountModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const labelKeys: Record<string, string> = {
'minimaxcode-global': 'accountTypeMiniMaxCodeGlobal',
'opencode-go': 'accountTypeOpenCode',
'opencode-zen': 'accountTypeOpenCode',
'custom-api': 'accountTypeCustomAPI',
'freemilk-global': 'accountTypeFreeMilk',
}

Expand All @@ -101,6 +102,7 @@ const hintKeys: Record<string, string> = {
'minimaxcode-global': 'accountTypeMiniMaxCodeGlobalHint',
'opencode-go': 'accountTypeOpenCodeGoHint',
'opencode-zen': 'accountTypeOpenCodeZenHint',
'custom-api': 'accountTypeCustomAPIHint',
'freemilk-global': 'accountTypeFreeMilkHint',
}

Expand Down Expand Up @@ -165,6 +167,10 @@ export function AddAccountModal({ isOpen, onClose, onAdded }: Props) {
const [zhipuOrganization, setZhipuOrganization] = useState('')
const [zhipuProject, setZhipuProject] = useState('')
const [doubaoCookies, setDoubaoCookies] = useState('')
const [customBaseURL, setCustomBaseURL] = useState('')
const [customKey, setCustomKey] = useState('')
const [customProtocol, setCustomProtocol] = useState<'openai' | 'anthropic' | 'responses'>('openai')
Comment thread
guyu233 marked this conversation as resolved.
Resolved
const [customModels, setCustomModels] = useState('')
const [phase, setPhase] = useState<Phase>('idle')
const [message, setMessage] = useState('')
const [authUrl, setAuthUrl] = useState('')
Expand Down Expand Up @@ -274,6 +280,10 @@ export function AddAccountModal({ isOpen, onClose, onAdded }: Props) {
setZhipuOrganization('')
setZhipuProject('')
setDoubaoCookies('')
setCustomBaseURL('')
setCustomKey('')
setCustomProtocol('openai')
setCustomModels('')
setAdvancedOpen(false)
setPhase('idle')
setMessage('')
Expand Down Expand Up @@ -456,6 +466,41 @@ export function AddAccountModal({ isOpen, onClose, onAdded }: Props) {
}
}

async function runCustom() {
const baseURL = customBaseURL.trim().replace(/\/+$/, '')
if (!baseURL) { setMessage(t('customBaseURLRequired')); return }
if (!activeOption) { setMessage(t('accountTypeHint')); return }
setMessage('')
try {
setPhase('busy')
const options = accountOptions()
const models = customModels
.split(/[\n,]/)
.map((model) => model.trim())
.filter(Boolean)
await importAccount({
format: 'custom-api-v1',
api_key: customKey.trim(),
base_url: baseURL,
protocol: customProtocol,
models,
name: name.trim() || t('accountTypeCustomAPI'),
enabled: true,
provider: activeOption.provider,
region: activeOption.region,
max_inflight: options.max_inflight,
priority: options.priority,
})
setPhase('done')
setMessage(t('wizardAccountReady'))
onAdded()
window.setTimeout(finishAndClose, 700)
} catch (error) {
setPhase('idle')
setMessage(error instanceof Error ? error.message : String(error))
}
}

async function runDoubao() {
const cookie = doubaoCookies.trim()
if (!cookie) { setMessage(t('doubaoCookieRequired')); return }
Expand Down Expand Up @@ -596,6 +641,7 @@ export function AddAccountModal({ isOpen, onClose, onAdded }: Props) {
const tabPending = (key: TabKey) => busy && tab === key
const isZhipu = activeOption?.provider === 'zhipu'
const isDoubao = activeOption?.provider === 'doubaowork'
const isCustom = activeOption?.provider === 'custom'
const methodOptions = [
activeOption?.descriptor.capabilities?.browser_login !== false
? { value: 'browser' as const, label: t('tabBrowser') }
Expand Down Expand Up @@ -800,7 +846,40 @@ export function AddAccountModal({ isOpen, onClose, onAdded }: Props) {
</div>
) : null}

{isZhipu || isDoubao ? null : (
{isCustom ? (
<div className="mt-4 flex flex-col gap-4">
<p className="rounded-lg bg-surface-secondary/45 px-3.5 py-3 text-caption leading-5 text-muted">{t('customEndpointLead')}</p>
<FormRow label={t('customBaseURL')}>
<Input value={customBaseURL} onChange={(event) => setCustomBaseURL(event.target.value)} placeholder={t('customBaseURLPh')} aria-label={t('customBaseURL')} disabled={busy} autoFocus />
</FormRow>
<FormRow label={t('customAPIKey')}>
<Input type="password" value={customKey} onChange={(event) => setCustomKey(event.target.value)} placeholder={t('customAPIKeyPh')} aria-label={t('customAPIKey')} disabled={busy} />
</FormRow>
<div className="space-y-2">
<span className="text-body font-medium text-muted">{t('customProtocol')}</span>
<OptionTiles
ariaLabel={t('customProtocol')}
columns={3}
value={customProtocol}
onChange={(next) => { if (!busy) setCustomProtocol(next) }}
options={[
{ value: 'openai', label: t('customProtocolOpenAI'), hint: t('customProtocolOpenAIHint'), disabled: busy },
{ value: 'anthropic', label: t('customProtocolAnthropic'), hint: t('customProtocolAnthropicHint'), disabled: busy },
{ value: 'responses', label: t('customProtocolResponses'), hint: t('customProtocolResponsesHint'), disabled: busy },
]}
/>
</div>
<FormRow label={t('customModels')}>
<TextArea className="min-h-20 font-mono text-caption leading-5" value={customModels} onChange={(event) => setCustomModels(event.target.value)} placeholder={t('customModelsPh')} aria-label={t('customModels')} disabled={busy} />
</FormRow>
<p className="-mt-2 text-caption leading-5 text-muted">{t('customModelsHint')}</p>
{message ? (
<p className="flex items-center gap-2 rounded-lg bg-surface-secondary px-3 py-2 text-caption">{isDone ? <CheckCircle size={14} className="shrink-0 text-success" /> : null}<span className={isDone ? 'font-medium text-foreground' : 'text-muted'}>{message}</span></p>
) : null}
</div>
) : null}

{isZhipu || isDoubao || isCustom ? null : (
<>
<FilterToggle
className="mt-3"
Expand Down Expand Up @@ -947,17 +1026,22 @@ export function AddAccountModal({ isOpen, onClose, onAdded }: Props) {
{isDone ? <><CheckCircle size={15} />{t('wizardAccountReady')}</> : <><Key size={15} />{t('doubaoImport')}</>}
</Button>
) : null}
{step === 'login' && !isZhipu && !isDoubao && tab === 'browser' ? (
{step === 'login' && isCustom ? (
<Button isPending={phase === 'busy'} onPress={() => void runCustom()} isDisabled={isDone}>
{isDone ? <><CheckCircle size={15} />{t('wizardAccountReady')}</> : <><Key size={15} />{t('customCreate')}</>}
</Button>
) : null}
{step === 'login' && !isZhipu && !isDoubao && !isCustom && tab === 'browser' ? (
<Button isPending={tabPending('browser')} onPress={() => void runBrowser()} isDisabled={isDone}>
{isDone ? <><CheckCircle size={15} />{t('wizardAccountReady')}</> : <><ShieldCheck size={15} />{t('wizardStartBrowser')}</>}
</Button>
) : null}
{step === 'login' && !isZhipu && !isDoubao && tab === 'pat' ? (
{step === 'login' && !isZhipu && !isDoubao && !isCustom && tab === 'pat' ? (
<Button isPending={tabPending('pat')} onPress={() => void runPat()} isDisabled={isDone}>
{isDone ? <><CheckCircle size={15} />{t('patDone')}</> : <><Key size={15} />{t('wizardCreateAndLogin')}</>}
</Button>
) : null}
{step === 'login' && !isZhipu && !isDoubao && tab === 'import' ? (
{step === 'login' && !isZhipu && !isDoubao && !isCustom && tab === 'import' ? (
<Button isPending={tabPending('import')} onPress={() => void runImport()} isDisabled={isDone}>
{isDone ? <><CheckCircle size={15} />{t('accountImported')}</> : <><FileCode size={15} />{t('importCredential')}</>}
</Button>
Expand Down
29 changes: 29 additions & 0 deletions frontend/src/components/CustomMark.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
type Props = {
size?: number
className?: string
}

// Socket-and-plug mark for the custom endpoint tiles: one endpoint, any
// upstream. Decorative; the provider name sits next to it.
export function CustomMark({ size = 16, className = '' }: Props) {
return (
<svg
width={size}
height={size}
viewBox="0 0 24 24"
className={`block shrink-0 text-foreground ${className}`.trim()}
aria-hidden="true"
>
<g
fill="none"
stroke="currentColor"
strokeWidth="1.8"
strokeLinecap="round"
strokeLinejoin="round"
>
<path d="M9 3.2v3.4M15 3.2v3.4M7.4 6.6h9.2a2.2 2.2 0 0 1 2.2 2.2v2.6a2.2 2.2 0 0 1-2.2 2.2H7.4a2.2 2.2 0 0 1-2.2-2.2V8.8a2.2 2.2 0 0 1 2.2-2.2Z" />
<path d="M12 13.6v7.2" />
</g>
</svg>
)
}
4 changes: 4 additions & 0 deletions frontend/src/components/ProviderMark.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CommandMark } from '@/components/CommandMark'
import { CodeArtsMark } from '@/components/CodeArtsMark'
import { CodexMark } from '@/components/CodexMark'
import { CustomMark } from '@/components/CustomMark'
import { DevinMark } from '@/components/DevinMark'
import { FreeMilkMark } from '@/components/FreeMilkMark'
import { DoubaoworkMark } from '@/components/DoubaoworkMark'
Expand Down Expand Up @@ -66,6 +67,9 @@ export function ProviderMark({ provider, size = 16, className = '' }: Props) {
if (id === 'opencode') {
return <OpenCodeMark size={size} className={className} />
}
if (id === 'custom') {
return <CustomMark size={size} className={className} />
}
if (id === 'freemilk') {
return <FreeMilkMark size={size} className={className} />
}
Expand Down
38 changes: 38 additions & 0 deletions frontend/src/i18n/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,25 @@ export const messages: Record<Lang, Dict> = {
accountTypeOpenCodeGoHint: 'Experimental. Paste an OpenCode API key. This account calls the Go subscription gateway.',
accountTypeOpenCodeZen: 'OpenCode Zen',
accountTypeOpenCodeZenHint: 'Experimental. Paste an OpenCode API key. This account calls the Zen pay-as-you-go gateway.',
accountTypeCustomAPI: 'Custom API',
accountTypeCustomAPIHint: 'Experimental. Any endpoint that speaks OpenAI, Anthropic, or Responses. Paste a base URL and key; calls go straight to it.',
customEndpointLead: 'One account is one endpoint. Enter the API root, not a full /chat/completions path, plus the key it expects. Local servers such as Ollama can leave the key empty.',
customBaseURL: 'Base URL',
customBaseURLPh: 'https://api.example.com/v1',
customBaseURLRequired: 'Base URL is required',
customAPIKey: 'API Key',
customAPIKeyPh: 'sk-…',
customProtocol: 'Upstream protocol',
customProtocolOpenAI: 'OpenAI',
customProtocolOpenAIHint: 'Chat Completions',
customProtocolAnthropic: 'Anthropic',
customProtocolAnthropicHint: 'Messages',
customProtocolResponses: 'Responses',
customProtocolResponsesHint: 'Responses API',
customModels: 'Models (optional)',
customModelsPh: 'gpt-4o, claude-sonnet-4',
customModelsHint: 'When set, this list replaces the endpoint catalog. Separate ids with commas or newlines.',
customCreate: 'Add endpoint',
accountTypeFreeMilk: 'FreeMilk',
accountTypeFreeMilkHint: 'Experimental. Sign in with Google. Chat uses the hosted free models.',
accountTypeCodexGlobal: 'Codex',
Expand Down Expand Up @@ -2313,6 +2332,25 @@ export const messages: Record<Lang, Dict> = {
accountTypeOpenCodeGoHint: '实验性。粘贴 OpenCode API Key。此账号走 Go 订阅网关。',
accountTypeOpenCodeZen: 'OpenCode Zen',
accountTypeOpenCodeZenHint: '实验性。粘贴 OpenCode API Key。此账号走 Zen 按量网关。',
accountTypeCustomAPI: '自定义 API',
accountTypeCustomAPIHint: '实验性。任何 OpenAI / Anthropic / Responses 兼容端点。填写 Base URL 和密钥,请求直连该地址。',
customEndpointLead: '一个账号就是一个端点。填 API 根地址(不要带 /chat/completions 这类完整路径)和它要求的密钥;Ollama 等本地服务可留空。',
customBaseURL: 'Base URL',
customBaseURLPh: 'https://api.example.com/v1',
customBaseURLRequired: '请填写 Base URL',
customAPIKey: 'API Key',
customAPIKeyPh: 'sk-…',
customProtocol: '上游协议',
customProtocolOpenAI: 'OpenAI',
customProtocolOpenAIHint: 'Chat Completions',
customProtocolAnthropic: 'Anthropic',
customProtocolAnthropicHint: 'Messages',
customProtocolResponses: 'Responses',
customProtocolResponsesHint: 'Responses API',
customModels: '模型(可选)',
customModelsPh: 'gpt-4o, claude-sonnet-4',
customModelsHint: '填写后以此列表为准,不再请求 /models;用逗号或换行分隔。',
customCreate: '添加端点',
accountTypeFreeMilk: 'FreeMilk',
accountTypeFreeMilkHint: '实验性。使用 Google 登录。聊天走托管的免费模型。',
accountTypeCodexGlobal: 'Codex',
Expand Down
1 change: 1 addition & 0 deletions frontend/src/lib/accountImportDetect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const FORMAT_TO_PROVIDER: Record<string, string> = {
'devin-session-v1': 'devin',
'command-key-v1': 'command',
'opencode-key-v1': 'opencode',
'custom-api-v1': 'custom',
'codex-oauth-v1': 'codex',
'zhipu-key-v1': 'zhipu',
'qwenwork-oauth-v1': 'qwenwork',
Expand Down
8 changes: 8 additions & 0 deletions frontend/src/lib/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ export function isOpenCodeProvider(provider?: string) {
return String(provider || '').toLowerCase() === 'opencode'
}

export function isCustomProvider(provider?: string) {
return String(provider || '').toLowerCase() === 'custom'
}

export function isFreeMilkProvider(provider?: string) {
return String(provider || '').toLowerCase() === 'freemilk'
}
Expand All @@ -72,6 +76,7 @@ export function accountProviderFamilyLabel(
if (isDoubaoworkProvider(providerID)) return 'Doubao Work'
if (isMiniMaxCodeProvider(providerID)) return 'MiniMax Code'
if (isOpenCodeProvider(providerID)) return 'OpenCode'
if (isCustomProvider(providerID)) return 'Custom API'
if (isFreeMilkProvider(providerID)) return 'FreeMilk'
if (isMetaProvider(providerID)) return 'Muse (Meta)'
if (isQoderProvider(providerID)) return 'Qoder'
Expand Down Expand Up @@ -118,6 +123,9 @@ export function accountProviderLabel(
if (isOpenCodeProvider(providerID)) {
return regionID === 'zen' ? t('accountTypeOpenCodeZen') : t('accountTypeOpenCodeGo')
}
if (isCustomProvider(providerID)) {
return t('accountTypeCustomAPI')
}
if (isFreeMilkProvider(providerID)) {
return t('accountTypeFreeMilk')
}
Expand Down
2 changes: 2 additions & 0 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/caigee-cmd/cli2api/internal/providers/codearts"
"github.com/caigee-cmd/cli2api/internal/providers/codex"
"github.com/caigee-cmd/cli2api/internal/providers/command"
"github.com/caigee-cmd/cli2api/internal/providers/custom"
"github.com/caigee-cmd/cli2api/internal/providers/devin"
"github.com/caigee-cmd/cli2api/internal/providers/doubaowork"
"github.com/caigee-cmd/cli2api/internal/providers/freemilk"
Expand Down Expand Up @@ -146,6 +147,7 @@ func New(cfg config.Config) *App {
providerReg.Register(meta.NewClient(store).Adapter())
providerReg.Register(doubaowork.NewClient(store).Adapter())
providerReg.Register(opencode.NewClient(store).Adapter())
providerReg.Register(custom.NewClient(store).Adapter())
providerReg.Register(freemilk.NewClient(store).Adapter())
qoderClient := qoder.NewClient()
qoderClient.Bind(manager.AccountURL, manager.ProxyAPIKey)
Expand Down
Loading
Loading