From 338321e55359b4610ec0651376d91b2ef9c25bfc Mon Sep 17 00:00:00 2001 From: Kirill Turanskiy Date: Sun, 8 Mar 2026 15:59:13 +0300 Subject: [PATCH 1/4] fix: use camelCase systemInstruction in OpenAI-to-Gemini translators The Gemini v1internal (cloudcode-pa) and Antigravity Manager endpoints require camelCase "systemInstruction" in request JSON. The current snake_case "system_instruction" causes system prompts to be silently ignored when routing through these endpoints. Replace all "system_instruction" JSON keys with "systemInstruction" in chat-completions and responses request translators. --- .../chat-completions/gemini_openai_request.go | 14 +++++++------- .../responses/gemini_openai-responses_request.go | 6 +++--- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/internal/translator/gemini/openai/chat-completions/gemini_openai_request.go b/internal/translator/gemini/openai/chat-completions/gemini_openai_request.go index f18f45bec3..c8948ac593 100644 --- a/internal/translator/gemini/openai/chat-completions/gemini_openai_request.go +++ b/internal/translator/gemini/openai/chat-completions/gemini_openai_request.go @@ -147,21 +147,21 @@ func ConvertOpenAIRequestToGemini(modelName string, inputRawJSON []byte, _ bool) content := m.Get("content") if (role == "system" || role == "developer") && len(arr) > 1 { - // system -> system_instruction as a user message style + // system -> systemInstruction as a user message style if content.Type == gjson.String { - out, _ = sjson.SetBytes(out, "system_instruction.role", "user") - out, _ = sjson.SetBytes(out, fmt.Sprintf("system_instruction.parts.%d.text", systemPartIndex), content.String()) + out, _ = sjson.SetBytes(out, "systemInstruction.role", "user") + out, _ = sjson.SetBytes(out, fmt.Sprintf("systemInstruction.parts.%d.text", systemPartIndex), content.String()) systemPartIndex++ } else if content.IsObject() && content.Get("type").String() == "text" { - out, _ = sjson.SetBytes(out, "system_instruction.role", "user") - out, _ = sjson.SetBytes(out, fmt.Sprintf("system_instruction.parts.%d.text", systemPartIndex), content.Get("text").String()) + out, _ = sjson.SetBytes(out, "systemInstruction.role", "user") + out, _ = sjson.SetBytes(out, fmt.Sprintf("systemInstruction.parts.%d.text", systemPartIndex), content.Get("text").String()) systemPartIndex++ } else if content.IsArray() { contents := content.Array() if len(contents) > 0 { - out, _ = sjson.SetBytes(out, "system_instruction.role", "user") + out, _ = sjson.SetBytes(out, "systemInstruction.role", "user") for j := 0; j < len(contents); j++ { - out, _ = sjson.SetBytes(out, fmt.Sprintf("system_instruction.parts.%d.text", systemPartIndex), contents[j].Get("text").String()) + out, _ = sjson.SetBytes(out, fmt.Sprintf("systemInstruction.parts.%d.text", systemPartIndex), contents[j].Get("text").String()) systemPartIndex++ } } diff --git a/internal/translator/gemini/openai/responses/gemini_openai-responses_request.go b/internal/translator/gemini/openai/responses/gemini_openai-responses_request.go index 143359d60e..463203a759 100644 --- a/internal/translator/gemini/openai/responses/gemini_openai-responses_request.go +++ b/internal/translator/gemini/openai/responses/gemini_openai-responses_request.go @@ -26,7 +26,7 @@ func ConvertOpenAIResponsesRequestToGemini(modelName string, inputRawJSON []byte if instructions := root.Get("instructions"); instructions.Exists() { systemInstr := `{"parts":[{"text":""}]}` systemInstr, _ = sjson.Set(systemInstr, "parts.0.text", instructions.String()) - out, _ = sjson.SetRaw(out, "system_instruction", systemInstr) + out, _ = sjson.SetRaw(out, "systemInstruction", systemInstr) } // Convert input messages to Gemini contents format @@ -119,7 +119,7 @@ func ConvertOpenAIResponsesRequestToGemini(modelName string, inputRawJSON []byte if strings.EqualFold(itemRole, "system") { if contentArray := item.Get("content"); contentArray.Exists() { systemInstr := "" - if systemInstructionResult := gjson.Get(out, "system_instruction"); systemInstructionResult.Exists() { + if systemInstructionResult := gjson.Get(out, "systemInstruction"); systemInstructionResult.Exists() { systemInstr = systemInstructionResult.Raw } else { systemInstr = `{"parts":[]}` @@ -140,7 +140,7 @@ func ConvertOpenAIResponsesRequestToGemini(modelName string, inputRawJSON []byte } if systemInstr != `{"parts":[]}` { - out, _ = sjson.SetRaw(out, "system_instruction", systemInstr) + out, _ = sjson.SetRaw(out, "systemInstruction", systemInstr) } } continue From d0cc0cd9a54dbbd16295df2a49a284aa2e51cb1a Mon Sep 17 00:00:00 2001 From: anime Date: Mon, 9 Mar 2026 02:00:16 +0800 Subject: [PATCH 2/4] docs: add All API Hub to related projects list - Update README.md with All API Hub entry in English - Update README_CN.md with All API Hub entry in Chinese --- README.md | 4 ++++ README_CN.md | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/README.md b/README.md index 8491b97c8f..722fa86b30 100644 --- a/README.md +++ b/README.md @@ -150,6 +150,10 @@ A Windows tray application implemented using PowerShell scripts, without relying A modern web-based management dashboard for CLIProxyAPI built with Next.js, React, and PostgreSQL. Features real-time log streaming, structured configuration editing, API key management, OAuth provider integration for Claude/Gemini/Codex, usage analytics, container management, and config sync with OpenCode via companion plugin - no manual YAML editing needed. +### [All API Hub](https://github.com/qixing-jk/all-api-hub) + +Browser extension for one-stop management of New API-compatible relay site accounts, featuring balance and usage dashboards, auto check-in, one-click key export to common apps, in-page API availability testing, and channel/model sync and redirection. It integrates with CLIProxyAPI through the Management API for one-click provider import and config sync. + > [!NOTE] > If you developed a project based on CLIProxyAPI, please open a PR to add it to this list. diff --git a/README_CN.md b/README_CN.md index 6e987fdf60..5dff9c5530 100644 --- a/README_CN.md +++ b/README_CN.md @@ -149,6 +149,10 @@ Windows 托盘应用,基于 PowerShell 脚本实现,不依赖任何第三方 一个面向 CLIProxyAPI 的现代化 Web 管理仪表盘,基于 Next.js、React 和 PostgreSQL 构建。支持实时日志流、结构化配置编辑、API Key 管理、Claude/Gemini/Codex 的 OAuth 提供方集成、使用量分析、容器管理,并可通过配套插件与 OpenCode 同步配置,无需手动编辑 YAML。 +### [All API Hub](https://github.com/qixing-jk/all-api-hub) + +用于一站式管理 New API 兼容中转站账号的浏览器扩展,提供余额与用量看板、自动签到、密钥一键导出到常用应用、网页内 API 可用性测试,以及渠道与模型同步和重定向。支持通过 CLIProxyAPI Management API 一键导入 Provider 与同步配置。 + > [!NOTE] > 如果你开发了基于 CLIProxyAPI 的项目,请提交一个 PR(拉取请求)将其添加到此列表中。 From 90afb9cb73e2e881780f213c99d75459a4a6eef3 Mon Sep 17 00:00:00 2001 From: DragonFSKY Date: Mon, 9 Mar 2026 03:11:47 +0800 Subject: [PATCH 3/4] fix(auth): new OAuth accounts invisible to scheduler after dynamic registration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When new OAuth auth files are added while the service is running, `applyCoreAuthAddOrUpdate` calls `coreManager.Register()` (which upserts into the scheduler) BEFORE `registerModelsForAuth()`. At upsert time, `buildScheduledAuthMeta` snapshots `supportedModelSetForAuth` from the global model registry — but models haven't been registered yet, so the set is empty. With an empty `supportedModelSet`, `supportsModel()` always returns false and the new auth is never added to any model shard. Additionally, when all existing accounts are in cooldown, the scheduler returns `modelCooldownError`, but `shouldRetrySchedulerPick` only handles `*Error` types — so the `syncScheduler` safety-net rebuild never triggers and the new accounts remain invisible. Fix: 1. Add `RefreshSchedulerEntry()` to re-upsert a single auth after its models are registered, rebuilding `supportedModelSet` from the now-populated registry. 2. Call it from `applyCoreAuthAddOrUpdate` after `registerModelsForAuth`. 3. Make `shouldRetrySchedulerPick` also match `*modelCooldownError` so the full scheduler rebuild triggers when all credentials are cooling down — catching any similar stale-snapshot edge cases. --- sdk/cliproxy/auth/conductor.go | 24 ++++++++++++++++++++++++ sdk/cliproxy/service.go | 6 ++++++ 2 files changed, 30 insertions(+) diff --git a/sdk/cliproxy/auth/conductor.go b/sdk/cliproxy/auth/conductor.go index aacf932255..b29e04db8c 100644 --- a/sdk/cliproxy/auth/conductor.go +++ b/sdk/cliproxy/auth/conductor.go @@ -213,6 +213,26 @@ func (m *Manager) syncScheduler() { m.syncSchedulerFromSnapshot(m.snapshotAuths()) } +// RefreshSchedulerEntry re-upserts a single auth into the scheduler so that its +// supportedModelSet is rebuilt from the current global model registry state. +// This must be called after models have been registered for a newly added auth, +// because the initial scheduler.upsertAuth during Register/Update runs before +// registerModelsForAuth and therefore snapshots an empty model set. +func (m *Manager) RefreshSchedulerEntry(authID string) { + if m == nil || m.scheduler == nil || authID == "" { + return + } + m.mu.RLock() + auth, ok := m.auths[authID] + if !ok || auth == nil { + m.mu.RUnlock() + return + } + snapshot := auth.Clone() + m.mu.RUnlock() + m.scheduler.upsertAuth(snapshot) +} + func (m *Manager) SetSelector(selector Selector) { if m == nil { return @@ -2038,6 +2058,10 @@ func shouldRetrySchedulerPick(err error) bool { if err == nil { return false } + var cooldownErr *modelCooldownError + if errors.As(err, &cooldownErr) { + return true + } var authErr *Error if !errors.As(err, &authErr) || authErr == nil { return false diff --git a/sdk/cliproxy/service.go b/sdk/cliproxy/service.go index 6124f8b18c..10cc35f3d8 100644 --- a/sdk/cliproxy/service.go +++ b/sdk/cliproxy/service.go @@ -312,6 +312,12 @@ func (s *Service) applyCoreAuthAddOrUpdate(ctx context.Context, auth *coreauth.A // This operation may block on network calls, but the auth configuration // is already effective at this point. s.registerModelsForAuth(auth) + + // Refresh the scheduler entry so that the auth's supportedModelSet is rebuilt + // from the now-populated global model registry. Without this, newly added auths + // have an empty supportedModelSet (because Register/Update upserts into the + // scheduler before registerModelsForAuth runs) and are invisible to the scheduler. + s.coreManager.RefreshSchedulerEntry(auth.ID) } func (s *Service) applyCoreAuthRemoval(ctx context.Context, id string) { From f5941a411c7193fa3cca9ccf4cce72bbaabad315 Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Mon, 9 Mar 2026 09:27:56 +0800 Subject: [PATCH 4/4] test(auth): cover scheduler refresh regression paths --- .../auth/conductor_scheduler_refresh_test.go | 163 ++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 sdk/cliproxy/auth/conductor_scheduler_refresh_test.go diff --git a/sdk/cliproxy/auth/conductor_scheduler_refresh_test.go b/sdk/cliproxy/auth/conductor_scheduler_refresh_test.go new file mode 100644 index 0000000000..5c6eff7805 --- /dev/null +++ b/sdk/cliproxy/auth/conductor_scheduler_refresh_test.go @@ -0,0 +1,163 @@ +package auth + +import ( + "context" + "errors" + "net/http" + "testing" + + "github.com/router-for-me/CLIProxyAPI/v6/internal/registry" + cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/executor" +) + +type schedulerProviderTestExecutor struct { + provider string +} + +func (e schedulerProviderTestExecutor) Identifier() string { return e.provider } + +func (e schedulerProviderTestExecutor) Execute(ctx context.Context, auth *Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (cliproxyexecutor.Response, error) { + return cliproxyexecutor.Response{}, nil +} + +func (e schedulerProviderTestExecutor) ExecuteStream(ctx context.Context, auth *Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (*cliproxyexecutor.StreamResult, error) { + return nil, nil +} + +func (e schedulerProviderTestExecutor) Refresh(ctx context.Context, auth *Auth) (*Auth, error) { + return auth, nil +} + +func (e schedulerProviderTestExecutor) CountTokens(ctx context.Context, auth *Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (cliproxyexecutor.Response, error) { + return cliproxyexecutor.Response{}, nil +} + +func (e schedulerProviderTestExecutor) HttpRequest(ctx context.Context, auth *Auth, req *http.Request) (*http.Response, error) { + return nil, nil +} + +func TestManager_RefreshSchedulerEntry_RebuildsSupportedModelSetAfterModelRegistration(t *testing.T) { + ctx := context.Background() + + testCases := []struct { + name string + prime func(*Manager, *Auth) error + }{ + { + name: "register", + prime: func(manager *Manager, auth *Auth) error { + _, errRegister := manager.Register(ctx, auth) + return errRegister + }, + }, + { + name: "update", + prime: func(manager *Manager, auth *Auth) error { + _, errRegister := manager.Register(ctx, auth) + if errRegister != nil { + return errRegister + } + updated := auth.Clone() + updated.Metadata = map[string]any{"updated": true} + _, errUpdate := manager.Update(ctx, updated) + return errUpdate + }, + }, + } + + for _, testCase := range testCases { + testCase := testCase + t.Run(testCase.name, func(t *testing.T) { + manager := NewManager(nil, &RoundRobinSelector{}, nil) + auth := &Auth{ + ID: "refresh-entry-" + testCase.name, + Provider: "gemini", + } + if errPrime := testCase.prime(manager, auth); errPrime != nil { + t.Fatalf("prime auth %s: %v", testCase.name, errPrime) + } + + registerSchedulerModels(t, "gemini", "scheduler-refresh-model", auth.ID) + + got, errPick := manager.scheduler.pickSingle(ctx, "gemini", "scheduler-refresh-model", cliproxyexecutor.Options{}, nil) + var authErr *Error + if !errors.As(errPick, &authErr) || authErr == nil { + t.Fatalf("pickSingle() before refresh error = %v, want auth_not_found", errPick) + } + if authErr.Code != "auth_not_found" { + t.Fatalf("pickSingle() before refresh code = %q, want %q", authErr.Code, "auth_not_found") + } + if got != nil { + t.Fatalf("pickSingle() before refresh auth = %v, want nil", got) + } + + manager.RefreshSchedulerEntry(auth.ID) + + got, errPick = manager.scheduler.pickSingle(ctx, "gemini", "scheduler-refresh-model", cliproxyexecutor.Options{}, nil) + if errPick != nil { + t.Fatalf("pickSingle() after refresh error = %v", errPick) + } + if got == nil || got.ID != auth.ID { + t.Fatalf("pickSingle() after refresh auth = %v, want %q", got, auth.ID) + } + }) + } +} + +func TestManager_PickNext_RebuildsSchedulerAfterModelCooldownError(t *testing.T) { + ctx := context.Background() + manager := NewManager(nil, &RoundRobinSelector{}, nil) + manager.RegisterExecutor(schedulerProviderTestExecutor{provider: "gemini"}) + + registerSchedulerModels(t, "gemini", "scheduler-cooldown-rebuild-model", "cooldown-stale-old") + + oldAuth := &Auth{ + ID: "cooldown-stale-old", + Provider: "gemini", + } + if _, errRegister := manager.Register(ctx, oldAuth); errRegister != nil { + t.Fatalf("register old auth: %v", errRegister) + } + + manager.MarkResult(ctx, Result{ + AuthID: oldAuth.ID, + Provider: "gemini", + Model: "scheduler-cooldown-rebuild-model", + Success: false, + Error: &Error{HTTPStatus: http.StatusTooManyRequests, Message: "quota"}, + }) + + newAuth := &Auth{ + ID: "cooldown-stale-new", + Provider: "gemini", + } + if _, errRegister := manager.Register(ctx, newAuth); errRegister != nil { + t.Fatalf("register new auth: %v", errRegister) + } + + reg := registry.GetGlobalRegistry() + reg.RegisterClient(newAuth.ID, "gemini", []*registry.ModelInfo{{ID: "scheduler-cooldown-rebuild-model"}}) + t.Cleanup(func() { + reg.UnregisterClient(newAuth.ID) + }) + + got, errPick := manager.scheduler.pickSingle(ctx, "gemini", "scheduler-cooldown-rebuild-model", cliproxyexecutor.Options{}, nil) + var cooldownErr *modelCooldownError + if !errors.As(errPick, &cooldownErr) { + t.Fatalf("pickSingle() before sync error = %v, want modelCooldownError", errPick) + } + if got != nil { + t.Fatalf("pickSingle() before sync auth = %v, want nil", got) + } + + got, executor, errPick := manager.pickNext(ctx, "gemini", "scheduler-cooldown-rebuild-model", cliproxyexecutor.Options{}, nil) + if errPick != nil { + t.Fatalf("pickNext() error = %v", errPick) + } + if executor == nil { + t.Fatal("pickNext() executor = nil") + } + if got == nil || got.ID != newAuth.ID { + t.Fatalf("pickNext() auth = %v, want %q", got, newAuth.ID) + } +}