Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
import { agentEndpointAddress, discoverAgentEndpointModels, hasExplicitModel, isGatewayEndpoint } from "../agent-endpoint-models.ts";
import { codexEndpointSchema, codexEndpointUpdateSchema, newCodexEndpoint, updateCodexEndpoint, withCodexEndpointInfo, isCodexEndpoint } from "../codex-endpoints.ts";
import { userError, userErrorFields } from "../user-errors.ts";
import { signInReasonOf } from "../../shared/sign-in-outcome.ts";
import { modelManifest } from "../model-manifest/manifest.ts";
import { randomUUID } from "node:crypto";
import { providerIconError } from "../../shared/provider-icon.ts";
import { isGlmOrigin } from "../../shared/glm-provider.ts";
import {
assertSeparateClaudeAccount,
configuredAccountDirectory,
createClaudeAccountSchema,
instanceSettingsSchema,
newClaudeAccount,
} from "../claude-accounts.ts";
import {
claudeEndpointSchema,
claudeEndpointUpdateSchema,
newClaudeEndpoint,
isClaudeEndpoint,
updateClaudeEndpoint,
withClaudeEndpointInfo,
} from "../claude-endpoints.ts";
import {
claudeBedrockSchema,
claudeBedrockUpdateSchema,
newClaudeBedrock,
updateClaudeBedrock,
withClaudeBedrockInfo,
} from "../claude-bedrock.ts";
import { updateClaudeCli } from "../claude-update.ts";
import { instanceConfigs, persistableInstanceConfigs, saveConfig, withInstanceCli, type AppConfig } from "../config.ts";
import { addEndpoint, addEndpointSchema, changeEndpointAddress, endpointBaseUrl, endpointModelsReadable, isAddedEndpoint, localServerAlreadyAdded, withEndpointUrls } from "../openai-compat-endpoints.ts";
import { CustomHeaderValidationError, mergeCustomHeadersPatch } from "../drivers/openai-compat-headers.ts";
import { findOllama, probeOllama } from "../drivers/ollama.ts";
import type { InstanceConfigMap, ModelSelection, ProviderInstance } from "../contracts.ts";
import { removeAntigravityProfile } from "../drivers/antigravity-acp.ts";
import { BUILT_IN_DRIVERS } from "../drivers/builtIn.ts";
import { findCliCandidates, resetPathCache } from "../env-path.ts";
import type { ProviderRegistry } from "../harness/registry.ts";
import { productName } from "../brand.ts";
import { checkEngine, type EngineCheck } from "../engine-check.ts";
import { HOSTED_PROVIDER_SETTINGS_ERROR } from "../hosted-models.ts";
import type { ProviderAuthSessions } from "../provider-auth-sessions.ts";
import { providerIconPatchSchema, withInstanceIcon } from "../provider-icon.ts";
import { fromPairedDevice, isLoopbackHost, isProxied, ownerOrAdmin } from "../request-auth.ts";
import type { SessionRegistry } from "../sessions.ts";
import type { Store } from "../store.ts";
import type { BusyFlag, ConfigStatus } from "./config.ts";
import { PASS, type RouteHandler } from "./table.ts";
export interface InstanceRouteDeps {
store: Pick<Store, "bots" | "tasks">;
cfg: AppConfig;
registry: Pick<ProviderRegistry, "get" | "refreshModels" | "installRuntime" | "cliTarget" | "forgetSnapshots" | "onWrongCli">;
sessions: Pick<SessionRegistry, "isLive">;
providerAuthSessions: Pick<ProviderAuthSessions, "status" | "start" | "revokeOwner" | "signOut" | "complete" | "cancel">;
hostedModels: boolean;
providerConfigBusy: BusyFlag;
providerInstancesChanging: Pick<Set<string>, "add" | "delete">;
describeInstances(): Promise<unknown>;
persistProviderInstance(instanceId: string, instances: InstanceConfigMap): Promise<void>;
configStatus(): ConfigStatus;
broadcast(payload: Record<string, unknown>): void;
testCliBinary(cli: string, driver: (typeof BUILT_IN_DRIVERS)[number] | undefined): Promise<unknown>;
cliProbeEnvironment(): NodeJS.ProcessEnv;
threadBusy(botId: string, threadId: string): boolean;
botForThread(botId: string, threadId: string): Pick<{ modelSelection: ModelSelection }, "modelSelection"> | null;
activeGroupTurnForBot(botId: string): { threadId: string } | null;
/** The server's tested-AI record (server/tested-ai.ts): a check there also settles the new-bot default. */
checkInstance?(instance: ProviderInstance): Promise<EngineCheck>;
/** Whether an added server lists its models (server/openai-compat-endpoints.ts); replaced in tests. */
endpointModelsReadable?(config: { url: string; key: string; catalog?: unknown }): Promise<boolean>;
}
/** The engines as a member (a client-scope session) sees them: names, models and capabilities, never the signed-in
* account, CLI paths, sign-in commands or a server's address. */
function memberInstances(described: unknown): unknown[] {
return (Array.isArray(described) ? described as Array<Record<string, any>> : []).map((instance) => ({
instanceId: instance.instanceId, driverKind: instance.driverKind, displayName: instance.displayName, readOnly: true,
...(instance.icon ? { icon: instance.icon } : {}), ...(instance.access ? { access: instance.access } : {}),
models: instance.models, ...(instance.capabilities ? { capabilities: instance.capabilities } : {}),
snapshot: { state: instance.snapshot?.state, ...(typeof instance.snapshot?.authenticated === "boolean" ? { authenticated: instance.snapshot.authenticated } : {}) },
}));
}
export function createInstanceRoutes(deps: InstanceRouteDeps): RouteHandler {
const {
store, cfg, registry, sessions, providerAuthSessions, hostedModels, providerConfigBusy, providerInstancesChanging,
persistProviderInstance, configStatus, broadcast, testCliBinary, cliProbeEnvironment, threadBusy,
botForThread, activeGroupTurnForBot,
} = deps;
const detectModels = deps.endpointModelsReadable ?? endpointModelsReadable;
const describeInstances = async () => hostedModels ? deps.describeInstances()
: withEndpointUrls(withCodexEndpointInfo(withClaudeBedrockInfo(withClaudeEndpointInfo(await deps.describeInstances(), cfg.instances ?? {}), cfg.instances ?? {}), cfg.instances ?? {}), cfg.instances ?? {});
/** Bots, their backups and threads, the new-bot default, or a turn running on it: a provider in use can't go away. */
const providerInUse = (instanceId: string): boolean => cfg.defaultModelSelection?.instanceId === instanceId || store.bots.some((bot) =>
bot.modelSelection.instanceId === instanceId || bot.fallback?.some(candidate => candidate.instanceId === instanceId) || store.tasks(bot.id).some((task) => task.modelSelection?.instanceId === instanceId)) ||
busyProviderSelections().some((selection) => selection.instanceId === instanceId);
const busyProviderSelections = () => store.bots.flatMap((bot) => {
const busyTasks = store.tasks(bot.id).filter((task) => threadBusy(bot.id, task.threadId));
const selections = busyTasks.map((task) => botForThread(bot.id, task.threadId)!.modelSelection);
if (activeGroupTurnForBot(bot.id) || (bot.busy && busyTasks.length === 0)) selections.push(bot.modelSelection);
return selections;
});
/** After the app installed its own copy of an engine whose CLI found a copy that does not start: point the engine at the
* app's copy, as Set CLI would. Not while provider settings are changing or a bot is using the engine. */
const useAppCopy = async (instanceId: string, cli: string): Promise<boolean> => {
if (providerConfigBusy.get() || busyProviderSelections().some((selection) => selection.instanceId === instanceId)) return false;
providerConfigBusy.set(true);
providerInstancesChanging.add(instanceId);
try {
const result = withInstanceCli(cfg, instanceId, cli);
const instances = persistableInstanceConfigs(result.config);
if (!result.ok || !Object.hasOwn(instances, instanceId)) return false;
await persistProviderInstance(instanceId, instances);
return true;
} finally {
providerInstancesChanging.delete(instanceId);
providerConfigBusy.set(false);
}
};
// A saved CLI path that runs another program (an install command pasted as the path) gives way to the engine's own CLI
// once one is installed, by itself: nobody has to find Reset.
registry?.onWrongCli?.((instanceId, cli) => useAppCopy(instanceId, cli));
// One updater per executable: Claude instances can share an install, and two self-updates would race its files.
const claudeUpdatesInFlight = new Set<string>();
return async ({ req, res, url, path, method, auth, json, readBody }) => {
if (method === "GET" && path === "/api/instances/hidden") {
const hidden = Object.entries(persistableInstanceConfigs(cfg)).filter(([, entry]) => entry.hidden === true);
return json(res, 200, { instances: hidden.map(([instanceId, entry]) => ({ instanceId, driverKind: entry.driver, displayName: entry.displayName ?? BUILT_IN_DRIVERS.find((driver) => driver.driverKind === entry.driver)?.metadata.displayName ?? instanceId })) });
}
if (method === "GET" && path === "/api/instances") {
// A member reads what picking a model and steering a turn need; a rescan and a fresh read stay the admin's.
if (auth.kind === "session" && !auth.scopes.includes("admin")) return json(res, 200, { instances: memberInstances(await describeInstances()) });
// Rescan PATH: Windows never pushes PATH changes into a live process, so a CLI installed since launch stays invisible.
resetPathCache();
if (url.searchParams.get("fresh") === "1") registry.forgetSnapshots();
// Whatever this download brings shows on the next open; the answer does not wait for it.
void modelManifest().refreshOnDemand().catch(() => {});
return json(res, 200, { instances: await describeInstances() });
}
if (method === "GET" && path === "/api/instances/ollama") {
if (hostedModels || !ownerOrAdmin(auth)) return json(res, 403, { error: "Only the owner of this server can look for Ollama." });
const asked = url.searchParams.get("url")?.trim();
const address = asked ? endpointBaseUrl(asked) : null;
if (asked && !address) return json(res, 400, { error: "Enter an http or https address, such as http://127.0.0.1:11434." });
return json(res, 200, { status: address ? await probeOllama(address) : await findOllama() });
}
// How many engines could start a bot now, never which: the check an update makes that the new version still can
// (deploy/update.sh), which a shared server lets a local caller without a session make (SERVICE_ALLOW).
if (method === "GET" && path === "/api/health/engines") {
const described = await describeInstances();
const available = Array.isArray(described) ? described.filter((instance) => Object(Object(instance).snapshot).state === "available").length : 0;
return json(res, 200, { app: "openmausbot", engines: available });
}
const companyMutation = /^\/api\/instances\/(company\.[\w.-]+)(?:\/|$)/.exec(path);
if (hostedModels && path.startsWith("/api/instances/") && method !== "GET") return json(res, 403, { error: HOSTED_PROVIDER_SETTINGS_ERROR });
if (companyMutation && method !== "GET") return json(res, 403, { error: "Company accounts are read-only here. Manage this connection in desktop Settings." });
const instanceIconPatch = /^\/api\/instances\/([\w.-]+)\/icon$/.exec(path);
if (method === "PATCH" && instanceIconPatch) {
if (!String(req.headers["content-type"] ?? "").toLowerCase().startsWith("application/json")) {
return json(res, 415, { error: "content-type must be application/json" });
}
const parsed = providerIconPatchSchema.safeParse(await readBody(req, 192 * 1024));
if (!parsed.success) {
const detail = parsed.error.issues[0]?.message;
return json(res, 400, { error: detail && detail !== "Invalid input" ? detail : "Choose a built-in icon or upload a PNG, JPEG, or WebP image up to 128 KB." });
}
if (parsed.data.icon) {
const invalid = providerIconError(parsed.data.icon);
if (invalid) return json(res, 400, { error: invalid });
}
if (providerConfigBusy.get()) return json(res, 409, { error: "provider settings are already being updated" });
providerConfigBusy.set(true);
try {
const changed = withInstanceIcon(cfg, instanceIconPatch[1], parsed.data.icon);
if (!changed.ok) return json(res, 404, { error: `unknown instance "${instanceIconPatch[1]}"` });
saveConfig({ instances: changed.instances }, { replaceInstances: true });
cfg.instances = changed.instances;
broadcast({ kind: "config", ...configStatus() });
return json(res, 200, { instances: await describeInstances() });
} finally { providerConfigBusy.set(false); }
}
if (method === "POST" && path === "/api/instances/chatgpt-accounts") {
if (!String(req.headers["content-type"] ?? "").toLowerCase().startsWith("application/json")) return json(res, 415, { error: "content-type must be application/json" });
const parsed = createClaudeAccountSchema.pick({ displayName: true }).safeParse(await readBody(req, 8192));
if (!parsed.success) return json(res, 400, { error: "Enter an account name (up to 80 characters)." });
if (providerConfigBusy.get()) return json(res, 409, { error: "provider settings are already being updated" });
providerConfigBusy.set(true);
try {
const instances = persistableInstanceConfigs(cfg);
const instanceId = `chatgpt-${randomUUID()}`;
// Reuse the selected executable, never another account's environment.
const cli = (instances.chatgpt?.config as { cli?: unknown } | undefined)?.cli
?? (instances.codex?.config as { cli?: unknown } | undefined)?.cli;
instances[instanceId] = { driver: "codex", displayName: parsed.data.displayName,
config: { authMode: "chatgpt-plan", ...(typeof cli === "string" && cli ? { cli } : {}) } };
await persistProviderInstance(instanceId, instances);
return json(res, 201, { instanceId, instances: await describeInstances() });
} finally { providerConfigBusy.set(false); }
}
if (method === "POST" && path === "/api/instances/openai-compat") {
if (!String(req.headers["content-type"] ?? "").toLowerCase().startsWith("application/json")) return json(res, 415, { error: "content-type must be application/json" });
const parsed = addEndpointSchema.safeParse(await readBody(req, 8192));
if (!parsed.success) return json(res, 400, { error: "Enter a display name (up to 80 characters), a base URL and an API key." });
if (providerConfigBusy.get()) return json(res, 409, { error: "provider settings are already being updated" });
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 });
try {
const added = addEndpoint(instances, parsed.data);
if ("error" in added) return json(res, 400, { error: added.error });
await persistProviderInstance(added.instanceId, instances);
if (isGlmOrigin(parsed.data.url)) await registry.refreshModels(added.instanceId).catch(() => false);
return json(res, 201, { instanceId: added.instanceId, instances: await describeInstances() });
} catch (error) {
if (error instanceof CustomHeaderValidationError) return json(res, 400, { error: error.message });
throw error;
}
} finally { providerConfigBusy.set(false); }
}
if (method === "POST" && (path === "/api/instances/claude-endpoint" || path === "/api/instances/codex-endpoint")) {
if (!String(req.headers["content-type"] ?? "").toLowerCase().startsWith("application/json")) {
return json(res, 415, { error: "content-type must be application/json" });
}
const codex = path.endsWith("/codex-endpoint");
const parsed = (codex ? codexEndpointSchema : claudeEndpointSchema).safeParse(await readBody(req, 16384));
if (!parsed.success) return json(res, 400, { error: codex ? parsed.error.issues[0]?.message ?? "Check the endpoint settings." : "Enter a name, an http(s) base URL, an API key." });
if (providerConfigBusy.get()) return json(res, 409, { error: "provider settings are already being updated" });
providerConfigBusy.set(true);
try {
const { instanceId, instances } = (codex ? newCodexEndpoint : newClaudeEndpoint)(cfg, parsed.data);
if (!parsed.data.model) await discoverAgentEndpointModels(instances[instanceId]);
await persistProviderInstance(instanceId, instances);
return json(res, 201, { instanceId, instances: await describeInstances() });
} catch (error) {
const status = (error as { status?: unknown }).status;
return json(res, typeof status === "number" ? status : 400, { error: error instanceof Error ? error.message : String(error) });
} finally { providerConfigBusy.set(false); }
}
const endpointPatch = /^\/api\/instances\/([\w.-]+)\/(claude|codex)-endpoint$/.exec(path);
if (method === "PATCH" && endpointPatch) {
if (!String(req.headers["content-type"] ?? "").toLowerCase().startsWith("application/json")) {
return json(res, 415, { error: "content-type must be application/json" });
}
const instanceId = endpointPatch[1];
const codex = endpointPatch[2] === "codex";
const parsed = (codex ? codexEndpointUpdateSchema : claudeEndpointUpdateSchema).safeParse(await readBody(req, 16384));
if (!parsed.success) return json(res, 400, { error: "Check the address and API key." });
if (providerConfigBusy.get()) return json(res, 409, { error: "provider settings are already being updated" });
if (busyProviderSelections().some((selection) => selection.instanceId === instanceId)) {
return json(res, 409, { error: "Wait for bots using this endpoint to finish before changing its settings." });
}
providerConfigBusy.set(true);
providerInstancesChanging.add(instanceId);
try {
const { instances } = (codex ? updateCodexEndpoint : updateClaudeEndpoint)(cfg, instanceId, parsed.data);
const previous = persistableInstanceConfigs(cfg)[instanceId];
const addressChanged = agentEndpointAddress(previous) !== agentEndpointAddress(instances[instanceId]);
if (!parsed.data.model && !hasExplicitModel(previous) && (parsed.data.token !== undefined || addressChanged)) await discoverAgentEndpointModels(instances[instanceId]);
await persistProviderInstance(instanceId, instances);
return json(res, 200, { instances: await describeInstances() });
} catch (error) {
const status = (error as { status?: unknown }).status;
return json(res, typeof status === "number" ? status : 400, { error: error instanceof Error ? error.message : String(error) });
} finally {
providerInstancesChanging.delete(instanceId);
providerConfigBusy.set(false);
}
}
if (method === "POST" && path === "/api/instances/claude-bedrock") {
if (!String(req.headers["content-type"] ?? "").toLowerCase().startsWith("application/json")) {
return json(res, 415, { error: "content-type must be application/json" });
}
const parsed = claudeBedrockSchema.safeParse(await readBody(req, 16384));
if (!parsed.success) return json(res, 400, { error: parsed.error.issues[0]?.message ?? "Enter a name, an AWS profile and a region." });
if (providerConfigBusy.get()) return json(res, 409, { error: "provider settings are already being updated" });
providerConfigBusy.set(true);
try {
const { instanceId, instances } = newClaudeBedrock(cfg, parsed.data);
await persistProviderInstance(instanceId, instances);
return json(res, 201, { instanceId, instances: await describeInstances() });
} catch (error) {
const status = (error as { status?: unknown }).status;
return json(res, typeof status === "number" ? status : 400, { error: error instanceof Error ? error.message : String(error) });
} finally { providerConfigBusy.set(false); }
}
const bedrockPatch = /^\/api\/instances\/([\w.-]+)\/claude-bedrock$/.exec(path);
if (method === "PATCH" && bedrockPatch) {
if (!String(req.headers["content-type"] ?? "").toLowerCase().startsWith("application/json")) {
return json(res, 415, { error: "content-type must be application/json" });
}
const instanceId = bedrockPatch[1];
const parsed = claudeBedrockUpdateSchema.safeParse(await readBody(req, 16384));
if (!parsed.success) return json(res, 400, { error: parsed.error.issues[0]?.message ?? "Check the AWS profile, region and models." });
if (providerConfigBusy.get()) return json(res, 409, { error: "provider settings are already being updated" });
if (busyProviderSelections().some((selection) => selection.instanceId === instanceId)) {
return json(res, 409, { error: "Wait for bots using this engine to finish before changing its settings." });
}
providerConfigBusy.set(true);
providerInstancesChanging.add(instanceId);
try {
const { instances } = updateClaudeBedrock(cfg, instanceId, parsed.data);
await persistProviderInstance(instanceId, instances);
return json(res, 200, { instances: await describeInstances() });
} catch (error) {
const status = (error as { status?: unknown }).status;
return json(res, typeof status === "number" ? status : 400, { error: error instanceof Error ? error.message : String(error) });
} finally {
providerInstancesChanging.delete(instanceId);
providerConfigBusy.set(false);
}
}
if (method === "POST" && path === "/api/instances/claude-accounts") {
if (!String(req.headers["content-type"] ?? "").toLowerCase().startsWith("application/json")) {
return json(res, 415, { error: "content-type must be application/json" });
}
const parsed = createClaudeAccountSchema.safeParse(await readBody(req, 8192));
if (!parsed.success) return json(res, 400, { error: "Enter an account name (up to 80 characters) and an optional configuration directory." });
if (providerConfigBusy.get()) return json(res, 409, { error: "provider settings are already being updated" });
providerConfigBusy.set(true);
try {
const { instanceId, instances } = newClaudeAccount(cfg, parsed.data);
await persistProviderInstance(instanceId, instances);
return json(res, 201, { instanceId, instances: await describeInstances() });
} finally { providerConfigBusy.set(false); }
}
// Each phone has its own sign-in, so it never resumes or reads the computer's, or another phone's.
const signInOwner = () => auth.kind === "session" ? auth.session.id
: fromPairedDevice(req, auth) ? `companion:${String(req.headers["x-openmausbot-companion-device"] ?? "")}` : "loopback";
const authStatus = /^\/api\/instances\/([\w.-]+)\/auth\/status$/.exec(path);
if (method === "GET" && authStatus) {
res.setHeader("cache-control", "no-store");
const state = await providerAuthSessions.status(authStatus[1], signInOwner(), url.searchParams.get("flowId") ?? "");
// A finished device sign-in changed what the engine reports; the page reads the engines next.
if (state.phase !== "waiting") registry.forgetSnapshots();
return json(res, 200, { auth: state });
}
const instanceAction = /^\/api\/instances\/([\w.-]+)\/(refresh-models|install|check|auth\/start|auth\/complete|auth\/cancel|auth\/sign-out)$/.exec(path);
if (method === "POST" && instanceAction) {
if (!String(req.headers["content-type"] ?? "").toLowerCase().startsWith("application/json")) {
return json(res, 415, { error: "content-type must be application/json" });
}
const instanceId = instanceAction[1];
const action = instanceAction[2];
const owner = signInOwner();
if (action.startsWith("auth/")) res.setHeader("cache-control", "no-store");
try {
if (action === "check") {
const instance = registry.get(instanceId);
if (!instance) return json(res, 404, { error: "unknown instance" });
res.setHeader("cache-control", "no-store");
// A check is the person asking again: what was last read of the sign-in does not stand in for it.
instance.forgetAuthentication?.();
registry.forgetSnapshots();
return json(res, 200, await (deps.checkInstance ?? checkEngine)(instance));
}
if (action === "refresh-models") {
const endpoint = persistableInstanceConfigs(cfg)[instanceId];
if (endpoint && (isClaudeEndpoint(endpoint) || isCodexEndpoint(endpoint)) && isGatewayEndpoint(endpoint) && !hasExplicitModel(endpoint)) {
if (providerConfigBusy.get()) return json(res, 409, { error: "provider settings are already being updated" });
providerConfigBusy.set(true);
try {
const instances = persistableInstanceConfigs(cfg);
await discoverAgentEndpointModels(instances[instanceId]);
await persistProviderInstance(instanceId, instances);
} finally { providerConfigBusy.set(false); }
}
if (!(await registry.refreshModels(instanceId))) return json(res, 404, { error: "unknown instance" });
return json(res, 200, { instances: await describeInstances() });
}
if (action === "install") {
const installed = await registry.installRuntime(instanceId);
if (!installed) return json(res, 404, { error: "Installing this engine from Settings is not available on this server. Use the install command on the machine running OpenMausBot." });
const usingAppCopy = installed !== true && await useAppCopy(instanceId, installed.useCli);
return json(res, 200, { instances: await describeInstances(), ...(usingAppCopy ? { usingAppCopy: true } : {}) });
}
if (action === "auth/start") {
const instance = registry.get(instanceId);
if (!instance) return json(res, 404, { error: "unknown instance" });
// The official local OAuth callback terminates on this machine, not a phone or a remote browser.
if ((instance.authenticationMethod === "browser-pkce" || instance.authenticationMethod === "browser")
&& (isProxied(req) || !isLoopbackHost(req.socket.remoteAddress) || fromPairedDevice(req, auth))) {
if (instance.authenticationMethod === "browser") {
return json(res, 403, { error: `This sign-in happens in a browser on the computer running ${productName()}. Open ${productName()} there to sign in.` });
}
return json(res, 403, { error: `Continue with ChatGPT on the computer running ${productName()}: this sign-in finishes in a browser there. Open ${productName()} on that computer to sign in.` });
}
const started = await providerAuthSessions.start(instance, owner);
if (started.phase === "succeeded") registry.forgetSnapshots();
// Revocation can arrive while the CLI is obtaining a device code.
if (auth.kind === "session" && !sessions.isLive(auth.session.id)) {
providerAuthSessions.revokeOwner(owner);
return json(res, 401, { error: "Your session ended. Start a new sign-in." });
}
return json(res, 200, { auth: started });
}
if (action === "auth/sign-out") {
const instance = registry.get(instanceId);
if (!instance) return json(res, 404, { error: "unknown instance" });
await providerAuthSessions.signOut(instance, owner);
registry.forgetSnapshots();
return json(res, 200, { instances: await describeInstances() });
}
if (action === "auth/complete") {
const body = await readBody(req, 8192);
const flowId = typeof body?.flowId === "string" ? body.flowId : "";
// `code` for a pasted sign-in code (Claude), `callbackUrl` for a browser callback
const callbackUrl = typeof body?.callbackUrl === "string" ? body.callbackUrl : typeof body?.code === "string" ? body.code : "";
if (!flowId || !callbackUrl) return json(res, 400, { error: "flowId and a code or callbackUrl are required" });
await providerAuthSessions.complete(instanceId, owner, flowId, callbackUrl);
registry.forgetSnapshots();
return json(res, 200, { ok: true });
}
const body = await readBody(req, 4096);
await providerAuthSessions.cancel(instanceId, owner, typeof body?.flowId === "string" ? body.flowId : "");
return json(res, 200, { ok: true });
} catch (error) {
const requestedStatus = error && typeof error === "object" ? (error as { status?: unknown }).status : undefined;
const status = typeof requestedStatus === "number" && [400, 401, 404, 409, 413, 415].includes(requestedStatus) ? requestedStatus : 500;
// `reason`: why a sign-in did not go on, as a fixed code (shared/sign-in-outcome.ts), such as `wrong_code` or `interrupted`.
const reason = action.startsWith("auth/") ? signInReasonOf(error) : null;
return json(res, status, { error: error instanceof Error ? error.message : String(error), ...userErrorFields(error), ...(reason ? { reason } : {}) });
}
}
// ?name=claude → every `claude` on the augmented PATH, in PATH order (first = what a bare name runs).
if (method === "GET" && path === "/api/cli-candidates") {
const name = url.searchParams.get("name") ?? "";
resetPathCache();
return json(res, 200, { candidates: findCliCandidates(name) });
}
// POST {cli, driver} → spawn `<cli> --version` with the PATH a turn would use.
if (method === "POST" && path === "/api/cli-test") {
// JSON-only: this executes a local binary, so a hostile page must not send it as a simple cross-origin request.
if (!String(req.headers["content-type"] ?? "").toLowerCase().startsWith("application/json")) {
return json(res, 415, { error: "content-type must be application/json" });
}
const body = await readBody(req, 4096);
const cli = typeof body?.cli === "string" ? body.cli.trim() : "";
if (!cli || /[\n\r]/.test(cli)) return json(res, 400, { error: "cli must be a non-empty path" });
const driver = typeof body?.driver === "string" ? BUILT_IN_DRIVERS.find((d) => d.driverKind === body.driver) : undefined;
// testCliBinary uses a credential-redacted environment, so wrapper arguments cannot read inherited secrets.
const probe = await testCliBinary(cli, driver);
return json(res, 200, probe);
}
// No command or path comes from the request: the registry supplies this instance's executable. JSON-only.
const claudeUpdate = /^\/api\/instances\/([\w.-]+)\/claude-update$/.exec(path);
if (method === "POST" && claudeUpdate) {
if (!String(req.headers["content-type"] ?? "").toLowerCase().startsWith("application/json")) {
return json(res, 415, { error: "content-type must be application/json" });
}
await readBody(req, 4096);
const target = registry.cliTarget(claudeUpdate[1]);
if (!target) return json(res, 404, { error: "no such provider instance" });
if (target.driverKind !== "claudeAgent") {
return json(res, 400, { error: "only Claude Code instances can be updated here" });
}
if (!target.cli) return json(res, 409, { error: "this Claude instance has no configured executable" });
if (claudeUpdatesInFlight.has(target.cli)) {
return json(res, 409, { error: "this Claude installation is already updating" });
}
const active = busyProviderSelections().some((selection) => registry.cliTarget(selection.instanceId)?.cli === target.cli);
if (active) {
return json(res, 409, { error: "wait for running Claude tasks to finish before updating" });
}
claudeUpdatesInFlight.add(target.cli);
try {
const result = await updateClaudeCli(target.cli, cliProbeEnvironment());
resetPathCache();
return json(res, 200, { ok: true, version: result.version });
} catch (error) {
return json(res, 500, { error: error instanceof Error ? error.message : String(error), ...userErrorFields(error) });
} finally {
claudeUpdatesInFlight.delete(target.cli);
// Even a failed update may have replaced the executable.
registry.forgetSnapshots();
}
}
// PATCH /api/instances/:id {cli: "/path/to/cli" | ""}: "" reverts to the driver default. An added OpenAI-compatible
// server also takes {url, apiKey}: its address changes in place, and its key stays unless the host changes.
const instancePatch = /^\/api\/instances\/([\w.-]+)$/.exec(path);
if (method === "PATCH" && instancePatch) {
// JSON-only, so a hostile page cannot send it as a simple request.
if (!String(req.headers["content-type"] ?? "").toLowerCase().startsWith("application/json")) {
return json(res, 415, { error: "content-type must be application/json" });
}
const parsed = instanceSettingsSchema.safeParse(await readBody(req, 16384));
if (!parsed.success) return json(res, 400, { ...userError("serverError.instance.settingsInvalid") });
const body = parsed.data;
const instanceId = instancePatch[1];
if (providerConfigBusy.get()) return json(res, 409, { error: "provider settings are already being updated" });
if (busyProviderSelections().some((selection) => selection.instanceId === instanceId)) {
return json(res, 409, { error: "Wait for bots using this account to finish before changing its settings." });
}
/** Set when the model list is read again from a new address or with a new key: after the busy guard, so bots on
* this server are not refused while a slow server answers. */
let detect: { url: string; key: string; catalog?: unknown } | null = null;
providerConfigBusy.set(true);
providerInstancesChanging.add(instanceId);
try {
const result = body.cli === undefined ? { ok: true, config: cfg } : withInstanceCli(cfg, instanceId, body.cli);
const instances = persistableInstanceConfigs(result.config);
if (!result.ok || !Object.hasOwn(instances, instanceId)) return json(res, 404, { error: `unknown instance "${instanceId}"` });
const entry = instances[instanceId];
if ((body.configDir !== undefined && entry.driver !== "claudeAgent") || (body.displayName !== undefined && !["claudeAgent", "codex", "antigravityAgent"].includes(entry.driver))) {
return json(res, 400, { error: "Account settings are available for Claude, ChatGPT and Google Gemini (Antigravity) accounts only." });
}
if (body.hidden === true && providerInUse(instanceId)) {
return json(res, 409, { error: "Choose another model for the bots and default model using this provider before hiding it." });
}
if (body.hidden !== undefined) {
if (body.hidden) entry.hidden = true;
else delete entry.hidden;
}
if (body.tools !== undefined) {
if (!["openai-compat", "grok", "minimax", "mistral", "cerebras", "groq", "greenference", "modelgo", "meta"].includes(entry.driver)) {
return json(res, 400, { error: "The tools setting is available for OpenAI-compatible, Grok API, MiniMax API, Mistral API, Cerebras API, Groq API, Greenference API, ModelGo API and Meta API instances only." });
}
entry.config = { ...entry.config as Record<string, unknown>, tools: body.tools };
}
if (body.replyWaitMinutes !== undefined) {
if (entry.driver !== "openai-compat") return json(res, 400, { ...userError("serverError.instance.replyWaitUnsupported") });
const { replyWaitMinutes: _previous, ...rest } = (entry.config ?? {}) as Record<string, unknown>;
entry.config = body.replyWaitMinutes === null ? rest : { ...rest, replyWaitMinutes: body.replyWaitMinutes };
}
if (body.url !== undefined || body.apiKey !== undefined) {
if (!ownerOrAdmin(auth)) return json(res, 403, { error: "Only the owner or an admin can change a server's address or key." });
if (!isAddedEndpoint(instanceId, entry)) return json(res, 400, { error: "The base URL and key can be changed here for added OpenAI-compatible servers only." });
const changed = changeEndpointAddress(instances, instanceId, { url: body.url, apiKey: body.apiKey });
if ("error" in changed) return json(res, changed.status, { error: changed.error });
// A model list read from the server's /models is read again from the new address or with the new key.
const config = entry.config as { url: string; key: string; catalog?: unknown; managedModels?: unknown };
if ((changed.addressChanged || body.apiKey !== undefined) && !Array.isArray(config.managedModels)) detect = { url: config.url, key: config.key, catalog: config.catalog };
}
if (body.customHeaders !== undefined) {
if (!ownerOrAdmin(auth)) return json(res, 403, { error: "Only the owner or an admin can configure custom headers." });
if (entry.driver !== "openai-compat") return json(res, 400, { error: "Custom request headers are available for OpenAI-compatible instances only." });
const { customHeaders: previous, ...rest } = (entry.config ?? {}) as Record<string, unknown>;
const effectivePrevious = previous ?? (isAddedEndpoint(instanceId, entry)
? undefined
: (instanceConfigs(cfg)[instanceId]?.config as { customHeaders?: unknown } | undefined)?.customHeaders);
try {
entry.config = body.customHeaders === null
? rest
: { ...rest, customHeaders: mergeCustomHeadersPatch(body.customHeaders, effectivePrevious) };
} catch (error) {
if (error instanceof CustomHeaderValidationError) return json(res, 400, { error: error.message });
throw error;
}
}
if (body.models !== undefined) {
if (!isAddedEndpoint(instanceId, entry)) return json(res, 400, { ...userError("serverError.instance.modelsUnsupported") });
const { managedModels: _previous, ...rest } = (entry.config ?? {}) as Record<string, unknown>;
const models = [...new Set(body.models)];
const defaultModel = cfg.defaultModelSelection?.instanceId === instanceId ? cfg.defaultModelSelection.model : undefined;
if (models.length && defaultModel && !models.includes(defaultModel)) {
return json(res, 409, { ...userError("serverError.instance.defaultModelRemoved", { model: defaultModel }) });
}
entry.config = models.length ? { ...rest, managedModels: models } : rest;
}
if (body.displayName !== undefined) entry.displayName = body.displayName;
if (body.configDir !== undefined) {
let previousDir: string | undefined;
try { previousDir = configuredAccountDirectory(entry); } catch { }
entry.config = { ...entry.config as Record<string, unknown>, configDir: body.configDir };
if (previousDir !== configuredAccountDirectory(entry)) {
const used = store.bots.some((bot) => bot.modelSelection.instanceId === instanceId || bot.fallback?.some(candidate => candidate.instanceId === instanceId) || store.tasks(bot.id).some((task) =>
task.modelSelection?.instanceId === instanceId || task.resumeCursors[instanceId] || task.lastInstanceId === instanceId));
if (used) return json(res, 409, { error: "This account is used by bots or conversation history. Add another account and select it for the bot instead." });
assertSeparateClaudeAccount(instances, instanceId, entry);
}
}
await persistProviderInstance(instanceId, instances);
if (!detect) return json(res, 200, { instances: await describeInstances() });
} finally {
providerInstancesChanging.delete(instanceId);
providerConfigBusy.set(false);
}
// Saved either way; a list that can't be read leaves sample models in the picker, so the answer says so.
const [, readable] = await Promise.all([registry?.refreshModels(instanceId).catch(() => {}), detectModels(detect)]);
return json(res, 200, {
instances: await describeInstances(),
...(readable ? {} : { warning: "Saved. The model list could not be read at this address with this key, so the model picker may show sample models. Check the address and key, or enter the model IDs." }),
});
}
if (method === "DELETE" && instancePatch) {
const instanceId = instancePatch[1];
if (providerConfigBusy.get()) return json(res, 409, { error: "provider settings are already being updated" });
// Held from before the config is read until it is saved: a sign-out waits on a CLI, and another account added or
// removed meanwhile would be overwritten by this one's older copy.
providerConfigBusy.set(true);
providerInstancesChanging.add(instanceId);
try {
const instances = persistableInstanceConfigs(cfg);
if (!Object.hasOwn(instances, instanceId)) return json(res, 404, { error: "unknown instance" });
const addedChatgpt = instances[instanceId].driver === "codex" && instanceId.startsWith("chatgpt-");
const addedAntigravity = instances[instanceId].driver === "antigravityAgent" && instanceId.startsWith("antigravity-");
if (!addedChatgpt && !addedAntigravity && !isCodexEndpoint(instances[instanceId]) && !isAddedEndpoint(instanceId, instances[instanceId]) && (instances[instanceId].driver !== "claudeAgent" || instanceId === "claude")) {
return json(res, 400, { error: "Only added accounts and custom endpoints can be removed here." });
}
if (providerInUse(instanceId)) {
return json(res, 409, { error: "Choose another account for the bots and default model using this account before removing it." });
}
// OpenMausBot keeps a ChatGPT account's sign-in in its own data folder: removing the account clears it. An added
// Antigravity account's sign-in, on either runtime, is in its profile, which is removed below.
const live = addedChatgpt ? registry.get(instanceId) : undefined;
if (live?.signOut) {
try { await providerAuthSessions.signOut(live, auth.kind === "session" ? auth.session.id : "loopback"); }
catch (error) {
if ((error as { code?: unknown }).code !== "chatgpt_revocation_unconfirmed") {
return json(res, 409, { error: error instanceof Error ? error.message : String(error), ...userErrorFields(error) });
}
}
}
delete instances[instanceId];
await persistProviderInstance(instanceId, instances);
// An added Antigravity account's Google sign-in lives in OpenMausBot's data folder too: removing the account deletes it.
if (addedAntigravity) await removeAntigravityProfile(instanceId);
return json(res, 200, { instances: await describeInstances() });
} finally {
providerInstancesChanging.delete(instanceId);
providerConfigBusy.set(false);
}
}
return PASS;
};
}