Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(local-inject): verify Rapid-MLX before sending its key; capability-only filter for its records
Raullen165c591abaf0
diff --git a/server/drivers/local-inject.test.ts b/server/drivers/local-inject.test.tsindex f0dea81961..c1fc9cc7aa 100644--- a/server/drivers/local-inject.test.ts+++ b/server/drivers/local-inject.test.ts@@ -56,16 +56,20 @@ describe("inject ids", () => {describe("Rapid-MLX discovery", () => {it.each([undefined, "rapid-secret"])("lists only Rapid-MLX records with API key %s", async (apiKey) => {+ const rapidRequests: string[] = [];const catalog = await mergeLocalInject({ default: "keep", options: [{ id: "keep", label: "Keep" }] },{ VITEST: "true", OPENMAUSBOT_PROBE_LOCAL_INJECT: "1", RAPID_MLX_API_KEY: apiKey },async (url, init) => {+ if (String(url).startsWith("http://127.0.0.1:8000/")) rapidRequests.push(String(url));if (String(url) !== "http://127.0.0.1:8000/v1/models") return new Response("nope", { status: 500 });- expect(new Headers(init?.headers).get("Authorization")).toBe(`Bearer ${apiKey ?? "rapid-mlx"}`);+ expect(new Headers(init?.headers).get("Authorization")).toBeNull();return new Response(JSON.stringify({default_model: "other-chat",data: [{ id: "mlx-community/Qwen3.5-4B-MLX-4bit", owned_by: "rapid-mlx", capabilities: ["text", "vision", "tools"] },+ { id: "nomic-ai/chat", owned_by: "rapid-mlx", capabilities: ["text"] },+ { id: "chat-omb-32k", owned_by: "rapid-mlx", capabilities: ["text"] },{ id: "other-chat", owned_by: "vllm", loaded: true },{ id: "unknown-chat", loaded: true },{ id: "missing-capabilities-chat", owned_by: "rapid-mlx" },@@ -78,6 +82,7 @@ describe("Rapid-MLX discovery", () => {}));},);+ expect(rapidRequests).toEqual(["http://127.0.0.1:8000/v1/models"]);expect(catalog.options).toEqual([{ id: "keep", label: "Keep" },{@@ -85,9 +90,70 @@ describe("Rapid-MLX discovery", () => {label: "mlx-community/Qwen3.5-4B-MLX-4bit (Rapid-MLX)",custom: true,},+ { id: "rapid_mlx::nomic-ai/chat", label: "nomic-ai/chat (Rapid-MLX)", custom: true },]);});+ it.each([+ {},+ { ready: true, model_loaded: false, engine_type: "mlx" },+ { ready: "true", model_loaded: false, engine_type: "mlx", model_lifecycle: null },+ { ready: true, model_loaded: "false", engine_type: "mlx", model_lifecycle: null },+ { ready: true, model_loaded: false, engine_type: null, model_lifecycle: null },+ null,+ [],+ ])("never sends the key to a listener with non-Rapid health %j", async (health) => {+ const rapidRequests: Array<[string, string | null]> = [];+ const models = await probeLocalInjects({ RAPID_MLX_API_KEY: "rapid-secret" }, async (url, init) => {+ if (!String(url).startsWith("http://127.0.0.1:8000/")) return new Response("nope", { status: 500 });+ rapidRequests.push([String(url), new Headers(init?.headers).get("Authorization")]);+ if (String(url).endsWith("/health")) return Response.json(health);+ return new Response("unauthorized", { status: 401 });+ });+ expect(models).toEqual([]);+ expect(rapidRequests).toEqual([+ ["http://127.0.0.1:8000/v1/models", null],+ ["http://127.0.0.1:8000/health", null],+ ]);+ });++ it("does not follow redirects while verifying a listener before sending the key", async () => {+ const rapidRequests: Array<[string, string | null, RequestInit["redirect"]]> = [];+ const models = await probeLocalInjects({ RAPID_MLX_API_KEY: "rapid-secret" }, async (url, init) => {+ if (!String(url).startsWith("http://127.0.0.1:8000/")) return new Response("nope", { status: 500 });+ rapidRequests.push([String(url), new Headers(init?.headers).get("Authorization"), init?.redirect]);+ if (String(url).endsWith("/health")) {+ return new Response(null, { status: 302, headers: { Location: "http://127.0.0.1:9999/health" } });+ }+ return new Response("unauthorized", { status: 401 });+ });+ expect(models).toEqual([]);+ expect(rapidRequests).toEqual([+ ["http://127.0.0.1:8000/v1/models", null, "manual"],+ ["http://127.0.0.1:8000/health", null, "manual"],+ ]);+ });++ it("retries with the key only after a public Rapid-MLX health response", async () => {+ const rapidRequests: Array<[string, string | null]> = [];+ const models = await probeLocalInjects({ RAPID_MLX_API_KEY: "rapid-secret" }, async (url, init) => {+ if (!String(url).startsWith("http://127.0.0.1:8000/")) return new Response("nope", { status: 500 });+ const authorization = new Headers(init?.headers).get("Authorization");+ rapidRequests.push([String(url), authorization]);+ if (String(url).endsWith("/health")) {+ return Response.json({ ready: false, model_loaded: false, engine_type: "mlx", model_lifecycle: null });+ }+ if (!authorization) return new Response("unauthorized", { status: 401 });+ return Response.json({ data: [{ id: "nomic-ai/chat", owned_by: "rapid-mlx", capabilities: ["text"] }] });+ });+ expect(rapidRequests).toEqual([+ ["http://127.0.0.1:8000/v1/models", null],+ ["http://127.0.0.1:8000/health", null],+ ["http://127.0.0.1:8000/v1/models", "Bearer rapid-secret"],+ ]);+ expect(models.map((model) => model.id)).toEqual(["rapid_mlx::nomic-ai/chat"]);+ });+it.each(["vllm", undefined])("ignores a server on port 8000 with owned_by %s", async (ownedBy) => {const models = await probeLocalInjects({}, async (url) => {if (String(url) !== "http://127.0.0.1:8000/v1/models") return new Response("nope", { status: 500 });diff --git a/server/drivers/local-inject.ts b/server/drivers/local-inject.tsindex ff6c526a06..ab45eccb3f 100644--- a/server/drivers/local-inject.ts+++ b/server/drivers/local-inject.ts@@ -226,7 +226,7 @@ function idsFromModelsPayload(payload: unknown, ownedBy?: string): string[] {if (typeof id !== "string" || !MODEL_ID.test(id)) return [];const low = id.toLowerCase();// Our own names for a model's window are that model, not another one to pick.- if (low.includes("embed") || low.includes("bge-") || low.includes("nomic") || isContextName(id)) return [];+ if (isContextName(id) || (!ownedBy && (low.includes("embed") || low.includes("bge-") || low.includes("nomic")))) return [];return [id];});}@@ -241,10 +241,25 @@ async function timedJson(const timer = setTimeout(() => controller.abort(), 1200);timer.unref?.();try {- const response = await fetchImpl(url, {+ const rapidMlx = host.id === "rapid_mlx";+ let response = await fetchImpl(url, {signal: controller.signal,- headers: { Authorization: `Bearer ${hostApiKey(host, env)}` },+ ...(rapidMlx ? { redirect: "manual" as const } : { headers: { Authorization: `Bearer ${hostApiKey(host, env)}` } }),});+ if (rapidMlx && response.status === 401 && env.RAPID_MLX_API_KEY) {+ const healthResponse = await fetchImpl(`${anthropicBaseUrl(host)}/health`, { signal: controller.signal, redirect: "manual" });+ if (!healthResponse.ok) return null;+ const health: unknown = await healthResponse.json();+ if (!health || typeof health !== "object" || Array.isArray(health)) return null;+ const row = health as Record<string, unknown>;+ if (typeof row.ready !== "boolean" || typeof row.model_loaded !== "boolean" ||+ typeof row.engine_type !== "string" || !("model_lifecycle" in row)) return null;+ response = await fetchImpl(url, {+ signal: controller.signal,+ redirect: "manual",+ headers: { Authorization: `Bearer ${env.RAPID_MLX_API_KEY}` },+ });+ }if (!response.ok) return null;return await response.json();} catch {