Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(engines): check for an already-added local server inside the add route's busy section

AdityaUmalebf936b1b7a6c
diff --git a/server/app-settings/registry.ts b/server/app-settings/registry.ts
index 0c1bd2584d..76dbca8ef9 100644
--- a/server/app-settings/registry.ts
+++ b/server/app-settings/registry.ts
@@ -10,7 +10,7 @@ import { MAX_MCP_CALL_TIMEOUT_MINUTES, MAX_ROOM_TURN_TIMEOUT_MINUTES, mcpCallTim
import type { InstanceConfig } from "../contracts.ts";
import { BUILT_IN_DRIVERS } from "../drivers/builtIn.ts";
import type { ProviderRegistry } from "../harness/registry.ts";
-import { endpointBaseUrl, endpointDisplayUrl, isAddedEndpoint } from "../openai-compat-endpoints.ts";
+import { endpointDisplayUrl, isAddedEndpoint, localServerAlreadyAdded } from "../openai-compat-endpoints.ts";
import type { RequestAuth } from "../request-auth.ts";
import { callRoute, type RouteReply } from "../routes/call-route.ts";
import type { RouteHandler } from "../routes/table.ts";
@@ -279,10 +279,8 @@ export function createSettingsRegistry(deps: RegistryDeps): SettingEntry[] {
describe: (change: EngineChange) => {
if (change.kind === "remove") return { refusal: personOnly("Removing an AI engine", "Settings → AI") };
if (change.kind === "ollama" || change.kind === "lmstudio") {
- // Settings adds a local server once per address; a second one only shows the same engine twice.
- const url = endpointBaseUrl(change.url);
- const existing = url ? Object.entries(instances()).find(([instanceId, entry]) => isAddedEndpoint(instanceId, entry) && endpointDisplayUrl(entry.config) === url) : undefined;
- if (existing) return { refusal: `"${existing[1].displayName ?? existing[0]}" at ${url} is already an engine (${existing[0]}). Use that one; do not add it again.` };
+ const duplicate = localServerAlreadyAdded(instances(), change.url);
+ if (duplicate) return { refusal: duplicate };
}
if (change.kind === "openai-compat") {
if (!savedKey(change.keyFrom)) return { refusal: keyMissing(change.keyFrom) };
diff --git a/server/openai-compat-endpoints.ts b/server/openai-compat-endpoints.ts
index 3440dd0b53..19a363b2e4 100644
--- a/server/openai-compat-endpoints.ts
+++ b/server/openai-compat-endpoints.ts
@@ -39,6 +39,13 @@ export function isAddedEndpoint(instanceId: string, entry: InstanceConfig | unde
return entry?.driver === "openai-compat" && instanceId.startsWith(ADDED_ENDPOINT_PREFIX);
}
+/** Ollama and LM Studio are added once per address: a second entry only shows the same engine twice. Returns why, or null. */
+export function localServerAlreadyAdded(instances: InstanceConfigMap, rawUrl: string): string | null {
+ const url = endpointBaseUrl(rawUrl);
+ const existing = url ? Object.entries(instances).find(([instanceId, entry]) => isAddedEndpoint(instanceId, entry) && endpointDisplayUrl(entry.config) === url) : undefined;
+ return existing ? `"${existing[1].displayName ?? existing[0]}" at ${url} is already an engine (${existing[0]}). Use that one; do not add it again.` : null;
+}
+
export function addEndpoint(instances: InstanceConfigMap, body: AddEndpoint): { instanceId: string } | { error: string } {
const url = endpointBaseUrl(body.url);
if (!url) return { error: "Enter an http or https base URL without a user name, password or query string." };
diff --git a/server/routes/instances.ts b/server/routes/instances.ts
index 03a6f75948..f0a20fd363 100644
--- a/server/routes/instances.ts
+++ b/server/routes/instances.ts
@@ -28,7 +28,7 @@ import {
} from "../claude-bedrock.ts";
import { updateClaudeCli } from "../claude-update.ts";
import { persistableInstanceConfigs, saveConfig, withInstanceCli, type AppConfig } from "../config.ts";
-import { addEndpoint, addEndpointSchema, endpointBaseUrl, isAddedEndpoint, withEndpointUrls } from "../openai-compat-endpoints.ts";
+import { addEndpoint, addEndpointSchema, endpointBaseUrl, isAddedEndpoint, localServerAlreadyAdded, withEndpointUrls } from "../openai-compat-endpoints.ts";
import { findOllama, probeOllama } from "../drivers/ollama.ts";
import type { InstanceConfigMap, ModelSelection, ProviderInstance } from "../contracts.ts";
import { removeAntigravityProfile } from "../drivers/antigravity-acp.ts";
@@ -191,6 +191,9 @@ export function createInstanceRoutes(deps: InstanceRouteDeps): RouteHandler {
providerConfigBusy.set(true);
try {
const instances = persistableInstanceConfigs(cfg);
+ // Checked again here, inside the busy section, so two quick adds cannot both pass the settings check.
+ const duplicate = parsed.data.catalog ? localServerAlreadyAdded(instances, parsed.data.url) : null;
+ if (duplicate) return json(res, 409, { error: duplicate });
const added = addEndpoint(instances, parsed.data);
if ("error" in added) return json(res, 400, { error: added.error });
await persistProviderInstance(added.instanceId, instances);
diff --git a/server/routes/openai-compat-endpoints.test.ts b/server/routes/openai-compat-endpoints.test.ts
index 6975c28c9b..2f89bebe71 100644
--- a/server/routes/openai-compat-endpoints.test.ts
+++ b/server/routes/openai-compat-endpoints.test.ts
@@ -115,6 +115,17 @@ describe("added OpenAI-compatible servers", () => {
expect(used.cfg.instances).toHaveProperty("openai-compat-used");
});
+ it("refuses a second local server at an address already added, even when the first add just finished", async () => {
+ const { cfg, call } = await serve();
+ const local = { displayName: "LM Studio", url: "http://127.0.0.1:1234/v1", key: "lm-studio", catalog: "lmstudio" };
+ const [first, second] = await Promise.all([call("POST", "/api/instances/openai-compat", local), call("POST", "/api/instances/openai-compat", local)]);
+ expect([first.status, second.status].sort()).toEqual([201, 409]);
+ const again = await call("POST", "/api/instances/openai-compat", { ...local, url: "http://127.0.0.1:1234/v1/" });
+ expect(again.status, again.text).toBe(409);
+ expect(again.body.error).toMatch(/already an engine/);
+ expect(Object.keys(cfg.instances ?? {}).filter((id) => id.startsWith("openai-compat-"))).toHaveLength(1);
+ });
+
it("is read-only on a hosted server", async () => {
const { cfg, call } = await serve({ hosted: true });
expect((await call("POST", "/api/instances/openai-compat", add)).status).toBe(403);