A cross-platform desktop All-in-One assistant for Claude Code, Codex, OpenCode,DeepSeek Harness and more. Only official website: ccswitch.io
Rust ai-toolsclaude-codecodexdeepseek-harnessdesktop-appgrokgrokbuildhermeskimi-codemcpopen-sourceopencodepiprovider-managementrustskillsskills-managementtauriwsl-support
fix(openclaw): register models added while editing a provider (#10191)
SaladDay84dd063891b1
diff --git a/src/hooks/useProviderActions.ts b/src/hooks/useProviderActions.tsindex 02c93e2f1..51deab99b 100644--- a/src/hooks/useProviderActions.ts+++ b/src/hooks/useProviderActions.ts@@ -16,7 +16,10 @@ import type {OpenClawDefaultModel,} from "@/types";import { getOpencodeDeclaredModels } from "@/components/providers/forms/helpers/opencodeFormUtils";-import type { OpenClawSuggestedDefaults } from "@/config/openclawProviderPresets";+import {+ savedOpenClawModelIds,+ type OpenClawSuggestedDefaults,+} from "@/config/openclawProviderPresets";import type { ProviderEditorSave } from "@/lib/api/providers";import { injectCodingPlanUsageScript } from "@/config/codingPlanProviders";import {@@ -161,6 +164,32 @@ export function useProviderActions(editorSave,});+ // OpenClaw: 编辑时新加的模型也登记到 /model 列表。只写进配置的供应商才登记,+ // 目录未设置时不新建,已有条目(别名、手加的字段)原样保留+ if (activeApp === "openclaw") {+ try {+ const live = await openclawApi.getLiveProvider(provider.id);+ const catalog = live && (await openclawApi.getModelCatalog());+ const added = catalog+ ? savedOpenClawModelIds(JSON.stringify(live))+ .map((id) => `${provider.id}/${id}`)+ .filter((ref) => !(ref in catalog))+ : [];+ if (catalog && added.length > 0) {+ await openclawApi.setModelCatalog({+ ...catalog,+ ...Object.fromEntries(added.map((ref) => [ref, {}])),+ });+ await queryClient.invalidateQueries({ queryKey: openclawKeys.all });+ }+ } catch (error) {+ console.warn(+ "[OpenClaw] Failed to register edited models to allowlist:",+ error,+ );+ }+ }+// 更新托盘菜单(失败不影响主操作)try {await providersApi.updateTrayMenu();@@ -171,7 +200,7 @@ export function useProviderActions();}},- [updateProviderMutation],+ [updateProviderMutation, activeApp, queryClient],);// 切换供应商diff --git a/tests/hooks/useProviderActions.test.tsx b/tests/hooks/useProviderActions.test.tsxindex f34f1812c..e3d1a561a 100644--- a/tests/hooks/useProviderActions.test.tsx+++ b/tests/hooks/useProviderActions.test.tsx@@ -58,6 +58,7 @@ const openclawApiGetModelCatalogMock = vi.fn();const openclawApiSetModelCatalogMock = vi.fn();const openclawApiGetDefaultModelMock = vi.fn();const openclawApiSetDefaultModelMock = vi.fn();+const openclawApiGetLiveProviderMock = vi.fn();vi.mock("@/lib/api", () => ({piApi: {@@ -83,6 +84,8 @@ vi.mock("@/lib/api", () => ({openclawApiGetDefaultModelMock(...args),setDefaultModel: (...args: unknown[]) =>openclawApiSetDefaultModelMock(...args),+ getLiveProvider: (...args: unknown[]) =>+ openclawApiGetLiveProviderMock(...args),},}));@@ -124,6 +127,7 @@ beforeEach(() => {openclawApiSetModelCatalogMock.mockReset();openclawApiGetDefaultModelMock.mockReset();openclawApiSetDefaultModelMock.mockReset();+ openclawApiGetLiveProviderMock.mockReset();toastSuccessMock.mockReset();toastErrorMock.mockReset();toastInfoMock.mockReset();@@ -960,6 +964,61 @@ describe("useProviderActions", () => {});});+ it("registers models added while editing an OpenClaw provider, keeping existing entries", async () => {+ openclawApiGetLiveProviderMock.mockResolvedValueOnce({+ models: [{ id: "kept" }, { id: "new" }],+ });+ openclawApiGetModelCatalogMock.mockResolvedValueOnce({+ "my-or/kept": { alias: "Opus", custom: true },+ "other/x": {},+ });+ const { wrapper } = createWrapper();+ const { result } = renderHook(() => useProviderActions("openclaw"), {+ wrapper,+ });++ await act(async () => {+ await result.current.updateProvider(+ createProvider({+ id: "my-or",+ settingsConfig: { models: [{ id: "kept" }, { id: "new" }] },+ }),+ );+ });++ expect(openclawApiSetModelCatalogMock).toHaveBeenCalledWith({+ "my-or/kept": { alias: "Opus", custom: true },+ "other/x": {},+ "my-or/new": {},+ });+ });++ it.each([+ ["the provider is not in the live config", null, {}],+ ["no model catalog is set", { models: [{ id: "new" }] }, null],+ ])(+ "leaves the OpenClaw catalog alone after an edit when %s",+ async (_case, live, catalog) => {+ openclawApiGetLiveProviderMock.mockResolvedValueOnce(live);+ openclawApiGetModelCatalogMock.mockResolvedValueOnce(catalog);+ const { wrapper } = createWrapper();+ const { result } = renderHook(() => useProviderActions("openclaw"), {+ wrapper,+ });++ await act(async () => {+ await result.current.updateProvider(+ createProvider({+ id: "my-or",+ settingsConfig: { models: [{ id: "kept" }, { id: "new" }] },+ }),+ );+ });++ expect(openclawApiSetModelCatalogMock).not.toHaveBeenCalled();+ },+ );+it("sets the first OpenClaw model without inventing a fallback chain", async () => {openclawApiSetDefaultModelMock.mockResolvedValueOnce({backupPath: "/tmp/openclaw-backup.json5",