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
76 changes: 2 additions & 74 deletions desktop/renderer/src/utils/model-provider.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export { removeProviderConnectionConfig } from "../../../src/provider-connection-config";

export type ModelApiFormat = "openai-chat" | "openai-responses" | "anthropic";
export type ModelInputCapability = "text" | "image";
export type ModelReasoningEffort =
Expand Down Expand Up @@ -426,77 +428,3 @@ export function removeModelProviderConfig(
},
};
}

export function removeProviderConnectionConfig(
existing: JsonObject,
providerKey: string,
): JsonObject {
const models = asJsonObject(existing.models);
const providers = asJsonObject(models.providers);
if (!Object.hasOwn(providers, providerKey) && providerKey !== "llama-cpp") {
throw new Error(`Provider "${providerKey}" is not configured`);
}
const nextProviders = { ...providers };
delete nextProviders[providerKey];

const belongsToProvider = (value: unknown) =>
typeof value === "string" && value.startsWith(`${providerKey}/`);
const clearModelReferences = (value: unknown): unknown => {
if (!isJsonObject(value)) return value;
const scope = { ...value };
if (belongsToProvider(scope.model)) {
delete scope.model;
} else if (isJsonObject(scope.model)) {
const selector = { ...scope.model };
if (belongsToProvider(selector.primary)) delete selector.primary;
if (Array.isArray(selector.fallbacks)) {
const remaining = selector.fallbacks.filter((model) => !belongsToProvider(model));
if (remaining.length !== selector.fallbacks.length) {
if (remaining.length) selector.fallbacks = remaining;
else delete selector.fallbacks;
}
}
if (Object.keys(selector).length) scope.model = selector;
else delete scope.model;
}
if (isJsonObject(scope.models)) {
const remaining = Object.fromEntries(
Object.entries(scope.models).filter(([modelRef]) => !belongsToProvider(modelRef)),
);
if (Object.keys(remaining).length !== Object.keys(scope.models).length) {
if (Object.keys(remaining).length) scope.models = remaining;
else delete scope.models;
}
}
return scope;
};
const agents = asJsonObject(existing.agents);
const nextAgents = { ...agents };
if (Object.hasOwn(agents, "defaults")) {
nextAgents.defaults = clearModelReferences(agents.defaults);
}
if (Array.isArray(agents.list)) nextAgents.list = agents.list.map(clearModelReferences);
if (isJsonObject(agents.entries)) {
nextAgents.entries = Object.fromEntries(
Object.entries(agents.entries).map(([id, agent]) => [id, clearModelReferences(agent)]),
);
}
const nextConfig = {
...existing,
models: { ...models, providers: nextProviders },
agents: nextAgents,
};
if (providerKey !== "llama-cpp") return nextConfig;
const plugins = asJsonObject(existing.plugins);
const entries = asJsonObject(plugins.entries);
return {
...nextConfig,
plugins: {
...plugins,
entries: {
...entries,
"llama-cpp": { ...asJsonObject(entries["llama-cpp"]), enabled: false },
},
},
};
}
73 changes: 72 additions & 1 deletion desktop/src/model-config-application.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
type ModelConfigUpdate,
} from "./model-config-application";
import { ensureGitHubCopilotProviderPlugin } from "./model-provider-plugins";
import { removeProviderConnectionConfig } from "./provider-connection-config";
import { runWindowsNodeMxcModelUpdate } from "./windows-node-mxc-model-config";

function fixture() {
Expand Down Expand Up @@ -227,9 +228,53 @@ describe("applyModelConfig", () => {
expect(state.saved()).toEqual(state.input.config);
});

it.each(["string", "selector"])(
"removes a referenced provider through config.set and clears its %s default",
async (format) => {
const state = await removalFixture();
const agents = {
defaults: {
workspace: "keep",
model:
format === "string"
? "minimax/one"
: { primary: "minimax/one", fallbacks: ["minimax/two", "qwen/old"] },
models: { "minimax/one": {}, "qwen/old": { alias: "keep" } },
},
};
for (const config of [state.input.expectedConfig, state.saved()])
(config as Record<string, unknown>).agents = structuredClone(agents);
state.input.config = removeProviderConnectionConfig(
state.input.expectedConfig as Record<string, unknown>,
"minimax",
);
const hooks = {
readConfig: state.deps.readConfig,
pause: vi.fn(async () => {}),
apply: () => applyModelConfig(state.deps, state.input),
restartSavedConfig: vi.fn(async () => {}),
verify: vi.fn(async () => {}),
resume: vi.fn(),
failClosed: vi.fn(async () => {}),
};
await runWindowsNodeMxcModelUpdate(hooks);
expect(state.gateway.request.mock.calls.map(([method]) => method)).toEqual([
"config.get",
"config.set",
"config.get",
]);
expect(state.saved()).toEqual(state.input.config);
expect(hooks.resume).toHaveBeenCalledOnce();
expect(hooks.restartSavedConfig).not.toHaveBeenCalled();
expect(hooks.failClosed).not.toHaveBeenCalled();
expect((state.input.expectedConfig as Record<string, unknown>).agents).toEqual(agents);
},
);

it.each([
"default-change",
"agent-change",
"workspace-change",
"other-provider",
"plugin-change",
"retained-primary",
Expand All @@ -240,14 +285,15 @@ describe("applyModelConfig", () => {
const next = state.input.config as {
models: { providers: Record<string, unknown> };
agents: {
defaults: { model: { primary: string } };
defaults: { model: { primary: string }; workspace?: string };
entries?: Record<string, unknown>;
list?: unknown[];
};
plugins?: unknown;
};
if (reason === "default-change") next.agents.defaults.model.primary = "qwen/replacement";
if (reason === "agent-change") next.agents.entries = { writer: { model: "qwen/old" } };
if (reason === "workspace-change") next.agents.defaults.workspace = "different";
if (reason === "other-provider") delete next.models.providers.qwen;
if (reason === "plugin-change") next.plugins = { entries: { minimax: { enabled: false } } };
if (reason.startsWith("retained-")) {
Expand Down Expand Up @@ -300,6 +346,31 @@ describe("applyModelConfig", () => {
expect(state.saved()).toEqual(state.input.config);
});

it("rejects a removal receipt that retained the removed provider's model references", async () => {
const state = await removalFixture();
for (const config of [state.input.expectedConfig, state.saved()])
(config as Record<string, unknown>).agents = {
defaults: { model: { primary: "minimax/one" } },
};
state.input.config = removeProviderConnectionConfig(
state.input.expectedConfig as Record<string, unknown>,
"minimax",
);
const request = state.gateway.request.getMockImplementation()!;
state.gateway.request.mockImplementation(async (method, params) => {
if (method === "config.set") {
const config = JSON.parse(String(params?.raw));
config.agents = (state.input.expectedConfig as Record<string, unknown>).agents;
return request(method, { ...params, raw: JSON.stringify(config) });
}
return request(method, params);
});
await expect(applyModelConfig(state.deps, state.input)).rejects.toMatchObject({
cause: { message: "The saved model references do not match the requested removal" },
});
expect(state.deps.log).not.toHaveBeenCalledWith(expect.stringContaining("hot-applied"));
});

it("applies Copilot activation through the MXC model transaction", async () => {
const state = fixture();
const next = structuredClone(state.input.expectedConfig) as Record<string, unknown>;
Expand Down
38 changes: 15 additions & 23 deletions desktop/src/model-config-application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { assertConfigWriteAllowed } from "./config-write-policy";
import { GatewayRequestError } from "./gateway-client";
import { supportsLocalModelGatewayVersion } from "./local-model-service";
import { assertModelConfigUnchanged, ModelConfigRestartRequiredError } from "./model-setup";
import { removeProviderConnectionConfig } from "./provider-connection-config";

type JsonObject = Record<string, unknown>;

Expand Down Expand Up @@ -40,7 +41,7 @@ function object(value: unknown): JsonObject {
return value && typeof value === "object" && !Array.isArray(value) ? (value as JsonObject) : {};
}

/** Connection edits/removal leave all other providers, model references and plugins alone. */
/** Connection edits preserve models; removal may also clear only the removed provider's refs. */
function isManagedProviderConnectionUpdate(params: ModelConfigUpdate): boolean {
if (
!["provider-save", "provider-remove"].includes(params.action) ||
Expand All @@ -56,27 +57,12 @@ function isManagedProviderConnectionUpdate(params: ModelConfigUpdate): boolean {
Object.hasOwn(nextProviders, params.providerKey)
)
return false;
const agents = object(object(params.config).agents);
const scopes = [
agents.defaults,
...Object.values(object(agents.entries)),
...(Array.isArray(agents.list) ? agents.list : []),
];
const refersToProvider = (value: unknown) =>
typeof value === "string" && value.startsWith(`${params.providerKey}/`);
if (
scopes.some((scope) => {
const current = object(scope);
const selector = object(current.model);
return (
refersToProvider(current.model) ||
refersToProvider(selector.primary) ||
(Array.isArray(selector.fallbacks) && selector.fallbacks.some(refersToProvider)) ||
Object.keys(object(current.models)).some(refersToProvider)
);
})
)
return false;
// Use the same removal as the UI so clearing the active default/fallbacks
// does not force config.apply's slower restart-receipt processing.
return isDeepStrictEqual(
removeProviderConnectionConfig(object(params.expectedConfig), params.providerKey),
params.config,
);
} else {
const next = object(nextProviders[params.providerKey]);
if (
Expand Down Expand Up @@ -241,7 +227,8 @@ export async function applyGatewayConfiguration(
assertRequestedPluginsSaved();
if (useConnectionSet) {
const providers = (config: unknown) => object(object(object(config).models).providers);
const savedProviders = providers(deps.readConfig());
const savedConfig = deps.readConfig();
const savedProviders = providers(savedConfig);
const expectedProviders = providers(params.config);
const providerKey = params.connectionProvider!;
const saved = object(savedProviders[providerKey]);
Expand All @@ -254,6 +241,11 @@ export async function applyGatewayConfiguration(
)
)
throw new Error("The saved provider connection does not match the requested settings");
if (
!Object.hasOwn(expectedProviders, providerKey) &&
!isDeepStrictEqual(object(savedConfig).agents, object(params.config).agents)
)
throw new Error("The saved model references do not match the requested removal");
}
deps.log(`${context} status=hot-applied`);
return;
Expand Down
84 changes: 84 additions & 0 deletions desktop/src/provider-connection-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
type JsonObject = Record<string, unknown>;

function isJsonObject(value: unknown): value is JsonObject {
return typeof value === "object" && value !== null && !Array.isArray(value);
}

function asJsonObject(value: unknown): JsonObject {
return isJsonObject(value) ? value : {};
}

/** Shared by the settings UI and the Gateway write guard. */
export function removeProviderConnectionConfig(
existing: JsonObject,
providerKey: string,
): JsonObject {
const models = asJsonObject(existing.models);
const providers = asJsonObject(models.providers);
if (!Object.hasOwn(providers, providerKey) && providerKey !== "llama-cpp") {
throw new Error(`Provider "${providerKey}" is not configured`);
}
const nextProviders = { ...providers };
delete nextProviders[providerKey];

const belongsToProvider = (value: unknown) =>
typeof value === "string" && value.startsWith(`${providerKey}/`);
const clearModelReferences = (value: unknown): unknown => {
if (!isJsonObject(value)) return value;
const scope = { ...value };
if (belongsToProvider(scope.model)) {
delete scope.model;
} else if (isJsonObject(scope.model)) {
const selector = { ...scope.model };
if (belongsToProvider(selector.primary)) delete selector.primary;
if (Array.isArray(selector.fallbacks)) {
const remaining = selector.fallbacks.filter((model) => !belongsToProvider(model));
if (remaining.length !== selector.fallbacks.length) {
if (remaining.length) selector.fallbacks = remaining;
else delete selector.fallbacks;
}
}
if (Object.keys(selector).length) scope.model = selector;
else delete scope.model;
}
if (isJsonObject(scope.models)) {
const remaining = Object.fromEntries(
Object.entries(scope.models).filter(([modelRef]) => !belongsToProvider(modelRef)),
);
if (Object.keys(remaining).length !== Object.keys(scope.models).length) {
if (Object.keys(remaining).length) scope.models = remaining;
else delete scope.models;
}
}
return scope;
};
const agents = asJsonObject(existing.agents);
const nextAgents = { ...agents };
if (Object.hasOwn(agents, "defaults")) {
nextAgents.defaults = clearModelReferences(agents.defaults);
}
if (Array.isArray(agents.list)) nextAgents.list = agents.list.map(clearModelReferences);
if (isJsonObject(agents.entries)) {
nextAgents.entries = Object.fromEntries(
Object.entries(agents.entries).map(([id, agent]) => [id, clearModelReferences(agent)]),
);
}
const nextConfig = {
...existing,
models: { ...models, providers: nextProviders },
agents: nextAgents,
};
if (providerKey !== "llama-cpp") return nextConfig;
const plugins = asJsonObject(existing.plugins);
const entries = asJsonObject(plugins.entries);
return {
...nextConfig,
plugins: {
...plugins,
entries: {
...entries,
"llama-cpp": { ...asJsonObject(entries["llama-cpp"]), enabled: false },
},
},
};
}
Loading
Loading