Skip to content

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

Offer reasoning levels on OpenAI and OpenRouter API models

milind-soni4f89310340dc
diff --git a/apps/docs/content/docs/providers/model-providers.mdx b/apps/docs/content/docs/providers/model-providers.mdx
index 296bffb049..30d4fb772f 100644
--- a/apps/docs/content/docs/providers/model-providers.mdx
+++ b/apps/docs/content/docs/providers/model-providers.mdx
@@ -119,6 +119,9 @@ To use more than one provider at once, add extra `openai-compat` engines to
Give each one its own key. An engine without a key uses the key from
Connections and sends it to that engine's address.
+With an OpenAI key or OpenRouter, a model that reasons gets a thinking control
+in the picker: Low, Medium or High. Leave it unset for the model's default.
+
### Free OpenRouter models
A new OpenRouter account with no credit can only use free models. Paid models
diff --git a/server/drivers/openai-compat-reasoning.test.ts b/server/drivers/openai-compat-reasoning.test.ts
new file mode 100644
index 0000000000..38c4aae527
--- /dev/null
+++ b/server/drivers/openai-compat-reasoning.test.ts
@@ -0,0 +1,84 @@
+import { randomUUID } from "node:crypto";
+
+import { afterEach, describe, expect, it, vi } from "vitest";
+
+import type { ProviderInstance } from "../contracts.ts";
+import { recordEvents } from "../testing/events.ts";
+import { OpenAICompatDriver } from "./openai-compat.ts";
+
+const json = (body: unknown, status = 200) => new Response(JSON.stringify(body), { status, headers: { "content-type": "application/json" } });
+
+/** Serves `rows` as the model list, records what each chat asked for. */
+function server(rows: unknown[]) {
+ const chats: Array<Record<string, unknown>> = [];
+ vi.stubGlobal("fetch", vi.fn<typeof fetch>(async (url, init) => {
+ const path = new URL(String(url)).pathname;
+ if (path.endsWith("/models")) return json({ data: rows });
+ if (path.endsWith("/chat/completions")) {
+ chats.push(JSON.parse(String(init!.body)) as Record<string, unknown>);
+ return new Response(`data: ${JSON.stringify({ choices: [{ index: 0, delta: { content: "Hi." }, finish_reason: "stop" }] })}\n\ndata: [DONE]\n\n`, { headers: { "content-type": "text/event-stream" } });
+ }
+ return json({ error: "not found" }, 404);
+ }));
+ return chats;
+}
+
+const create = async (config: Record<string, unknown>) => {
+ const instance = await OpenAICompatDriver.create({
+ instanceId: randomUUID(), displayName: undefined, enabled: true,
+ config: OpenAICompatDriver.decodeConfig({ apiKeyEnv: "OMB_FIXTURE_KEY", ...config }), environment: { OMB_FIXTURE_KEY: "fixture-key" },
+ });
+ await instance.refreshModels?.();
+ return instance;
+};
+
+async function turn(instance: ProviderInstance, model: string, variant?: string) {
+ const recorder = recordEvents(instance.adapter);
+ const threadId = randomUUID();
+ await instance.adapter.sendTurn({ threadId, text: "hi", model, ...(variant ? { variant } : {}) });
+ await recorder.until((event) => event.type === "turn.completed" && event.threadId === threadId);
+ recorder.stop();
+}
+
+const variantsOf = (instance: ProviderInstance) => Object.fromEntries(instance.models.options.map((option) => [option.id, option.variants?.map((choice) => choice.id)]));
+
+afterEach(() => vi.unstubAllGlobals());
+
+describe("reasoning effort on an API key", () => {
+ it("offers OpenAI's reasoning models the levels, and sends the one picked as reasoning_effort", async () => {
+ const chats = server([{ id: "gpt-5.2", created: 3 }, { id: "gpt-5-chat-latest", created: 2 }, { id: "gpt-4.1", created: 1 }, { id: "o1-mini", created: 0 }]);
+ const instance = await create({ url: "https://llm.example.test/v1", catalog: "openai" });
+ try {
+ expect(instance.adapter.capabilities.modelVariants).toBe(true);
+ expect(variantsOf(instance)).toEqual({ "gpt-5.2": ["low", "medium", "high"], "gpt-5-chat-latest": undefined, "gpt-4.1": undefined, "o1-mini": undefined });
+ await turn(instance, "gpt-5.2", "low");
+ await turn(instance, "gpt-5.2");
+ await turn(instance, "gpt-4.1", "low");
+ expect(chats.map((chat) => chat.reasoning_effort)).toEqual(["low", undefined, undefined]);
+ } finally { await instance.dispose(); }
+ });
+
+ it("offers an OpenRouter model the levels only when it says it reasons, and sends OpenRouter's own field", async () => {
+ const chats = server([{ id: "acme/thinker", supported_parameters: ["tools", "reasoning"] }, { id: "acme/plain", supported_parameters: ["tools"] }]);
+ const instance = await create({ url: "https://openrouter.ai/api/v1" });
+ try {
+ expect(instance.adapter.capabilities.modelVariants).toBe(true);
+ expect(variantsOf(instance)).toMatchObject({ "acme/thinker": ["low", "medium", "high"], "acme/plain": undefined });
+ await turn(instance, "acme/thinker", "high");
+ await turn(instance, "acme/plain", "high");
+ expect(chats.map((chat) => chat.reasoning)).toEqual([{ effort: "high" }, undefined]);
+ expect(chats.every((chat) => chat.reasoning_effort === undefined)).toBe(true);
+ } finally { await instance.dispose(); }
+ });
+
+ it("sends nothing to any other server, and offers it no control", async () => {
+ const chats = server([{ id: "gpt-5.2", supported_parameters: ["reasoning"] }]);
+ const instance = await create({ url: "https://llm.example.test/v1" });
+ try {
+ expect(instance.adapter.capabilities.modelVariants).toBeUndefined();
+ await turn(instance, "gpt-5.2", "high");
+ expect(chats[0]).not.toHaveProperty("reasoning_effort");
+ expect(chats[0]).not.toHaveProperty("reasoning");
+ } finally { await instance.dispose(); }
+ });
+});
diff --git a/server/drivers/openai-compat.ts b/server/drivers/openai-compat.ts
index e8e5168f76..dc229b1b56 100644
--- a/server/drivers/openai-compat.ts
+++ b/server/drivers/openai-compat.ts
@@ -43,6 +43,9 @@ const OLLAMA_STATUS_MS = 5_000;
const OLLAMA_FIRST_LIST_MS = 2_000;
const withEffort = (effort: string | undefined) => effort ? { reasoning_effort: effort } : {};
+const EFFORT_CHOICES = ["low", "medium", "high"].map((id) => ({ id, label: titleCaseModelId(id, /[-_ ]+/) }));
+/** OpenAI's reasoning models. o1-mini, o1-preview and the gpt-5 chat alias refuse `reasoning_effort`. */
+const OPENAI_REASONING = /^(?:o1(?!-mini|-preview)|o[3-9]|gpt-5(?!-chat))/i;
const localFacts = ({ bytes, parameters, quantization, tools, vision }: OllamaModel) => ({
...(bytes ? { bytes } : {}), ...(parameters ? { parameters } : {}), ...(quantization ? { quantization } : {}),
@@ -241,6 +244,13 @@ export const OpenAICompatDriver: ProviderDriver<OpenAICompatConfig> = {
return true;
};
+ const takesEffort = (id: string, row: OpenRouterModelRow) => config.catalog === "openai" ? OPENAI_REASONING.test(id)
+ : openRouter && Array.isArray(row.supported_parameters) && row.supported_parameters.includes("reasoning");
+ const apiEffort = (model: string, variant: string | undefined) => {
+ if (!variant || !catalog.options.find((option) => option.id === model)?.variants?.some((choice) => choice.id === variant)) return {};
+ return config.catalog === "openai" ? { reasoning_effort: variant } : openRouter ? { reasoning: { effort: variant } } : {};
+ };
+
const fetchModels = async () => {
if (!apiKey) return;
if (ollamaCandidate && await listOllama()) return;
@@ -304,6 +314,7 @@ export const OpenAICompatDriver: ProviderDriver<OpenAICompatConfig> = {
id,
label: openRouter ? freeLabel(label, row) : label,
...(contextWindow ? { contextWindow } : {}),
+ ...(takesEffort(id, row) ? { variants: EFFORT_CHOICES } : {}),
...(config.catalog === "openai" ? {} : { custom: true }),
});
}
@@ -371,13 +382,14 @@ export const OpenAICompatDriver: ProviderDriver<OpenAICompatConfig> = {
return reason ? { reason, ollama: { state: status.state, url: status.url, ...(status.problem ? { problem: status.problem } : {}) } } : null;
},
} : {}),
- modelVariants: ollamaCandidate,
+ modelVariants: ollamaCandidate || config.catalog === "openai" || openRouter,
requestBody: (model, messages, stream, variant) => ({
model,
messages,
stream,
stream_options: stream ? { include_usage: true } : undefined,
...withEffort(thinkingEffort(fact(pickedName(model))?.thinking, variant)),
+ ...apiEffort(model, variant),
...(config.provider && isOpenRouterUrl(config.url)
? { provider: { order: [config.provider], allow_fallbacks: false } }
: {}),