Skip to content

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

fix(server): background text on an OpenAI-style engine keeps the bot's own model

AdityaUmalea4d0bd238cd6
diff --git a/server/drivers/openai-chat.test.ts b/server/drivers/openai-chat.test.ts
index d05b6f2aaf..0be4ef8fcf 100644
--- a/server/drivers/openai-chat.test.ts
+++ b/server/drivers/openai-chat.test.ts
@@ -12,6 +12,7 @@ import { GrokDriver } from "./grok.ts";
import { MinimaxDriver } from "./minimax.ts";
import { OpenAICompatDriver } from "./openai-compat.ts";
import { MistralDriver } from "./mistral.ts";
+import { ModelgoDriver } from "./modelgo.ts";
import { startFakeHttpMcp } from "../testing/fake-http-mcp-server.ts";
import { whopLikeCatalog } from "../testing/whop-like-catalog.ts";
@@ -110,6 +111,27 @@ describe("createOpenAIChatRuntime one-shot usage", () => {
await instance.dispose();
});
+ it("writes a bot's background text with the bot's own model, not the catalog's first one", async () => {
+ vi.stubGlobal("fetch", vi.fn(async (url: string | URL) => String(url).endsWith("/models")
+ ? new Response(JSON.stringify({ data: [
+ { id: "claude-fable-5", output_modalities: ["text"] },
+ { id: "deepseek-v4.1-flash", output_modalities: ["text"] },
+ ] }), { headers: { "content-type": "application/json" } })
+ : new Response(JSON.stringify({ choices }), { headers: { "content-type": "application/json" } })));
+ const instance = await ModelgoDriver.create({
+ instanceId: "modelgo", displayName: "ModelGo", enabled: true,
+ config: ModelgoDriver.defaultConfig(), environment: { MODELGO_API_KEY: "fixture-key" },
+ });
+ await vi.waitFor(() => expect(instance.models.options.length).toBe(2));
+ const fallback = { model: "deepseek-v4.1-flash", refused: new Set<string>(), onRefused: () => {} };
+ await expect(instance.generateText!("summarize", { fallback })).resolves.toBe("helper result");
+ await instance.generateText!("summarize", { model: "claude-fable-5", fallback });
+ const sent = vi.mocked(fetch).mock.calls.filter((call) => String(call[0]).endsWith("/chat/completions"))
+ .map((call) => JSON.parse(call[1]!.body as string).model);
+ expect(sent).toEqual(["deepseek-v4.1-flash", "claude-fable-5"]);
+ await instance.dispose();
+ });
+
it("does not invent usage for malformed JSON or an aborted request", async () => {
const instance = await fixture("not JSON");
const onUsage = vi.fn();
diff --git a/server/drivers/openai-chat.ts b/server/drivers/openai-chat.ts
index ab9d089bf9..a6af050acb 100644
--- a/server/drivers/openai-chat.ts
+++ b/server/drivers/openai-chat.ts
@@ -978,8 +978,8 @@ export function createOpenAIChatRuntime<Config>(options: RuntimeOptions<Config>)
return () => listeners.delete(listener);
},
},
- generateText: async (prompt, { signal, onUsage, model: named, checkModel } = {}) => {
- const model = named ?? options.generateModel?.() ?? options.models().default;
+ generateText: async (prompt, { signal, onUsage, model: named, checkModel, fallback } = {}) => {
+ const model = named ?? options.generateModel?.() ?? fallback?.model ?? options.models().default;
signal?.throwIfAborted();
checkModel?.(model);
const { text, reasoning, toolCalls } = await complete([{ role: "user", content: prompt }], model, false, signal, undefined, [], onUsage);