feat: add persistent model-aware reasoning effort settings

This commit is contained in:
2026-11-15 18:45:22 -08:00
parent 91c7c0310e
commit 92a16a39c0
22 changed files with 678 additions and 36 deletions
+144
View File
@@ -0,0 +1,144 @@
import assert from "node:assert/strict";
// Run against a local production preview; all API/socket traffic is mocked.
const { chromium } = await import(process.env.PLAYWRIGHT_MODULE || "playwright-core");
const target = new URL(process.env.WEB_TEST_URL || "http://127.0.0.1:5197/");
assert.ok(target.protocol === "http:" && ["127.0.0.1", "localhost"].includes(target.hostname));
const browser = await chromium.launch({ headless: true, executablePath: process.env.CHROME_EXECUTABLE || undefined,
...(process.env.BROWSER_NO_SANDBOX === "1" ? { args: ["--no-sandbox"] } : {}) });
const date = "2026-11-15T12:00:00Z";
const capabilities = {
"gpt-5.2": { efforts: ["none", "low", "medium", "high", "xhigh"], defaultEffort: "none" },
"gpt-6.1-sol": { efforts: ["low", "medium", "high", "xhigh", "max"], defaultEffort: "medium" },
};
async function fixture(viewport, withCapabilities = true) {
const context = await browser.newContext({ viewport, isMobile: viewport.width < 768, hasTouch: viewport.width < 768 });
await context.addInitScript(() => {
localStorage.setItem("sybil_admin_token", "fixture-token");
localStorage.setItem("sybil:ui-mode", "full");
});
const chat = { id: "reasoning-chat", title: "Reasoning fixture", createdAt: date, updatedAt: date,
parentChatId: null, titleGenerationPending: false, starred: false, starredAt: null,
initiatedProvider: "openai", initiatedModel: "gpt-5.2", lastUsedProvider: "openai", lastUsedModel: "gpt-5.2",
additionalSystemPrompt: null, enabledTools: [], reasoningEffort: null, messages: [] };
const chats = new Map([[chat.id, chat]]);
const writes = [], errors = [];
await context.route("**/*", async route => {
const request = route.request(), url = new URL(request.url()), path = url.pathname;
if (url.origin !== target.origin) return route.abort();
if (!path.startsWith("/api/")) return route.continue();
const body = request.method() === "GET" ? null : request.postDataJSON();
if (body) writes.push({ path, body });
let value = {};
const id = path.match(/\/chats\/([^/]+)$/)?.[1];
if (path.endsWith("/auth/session")) value = { mode: "token", authenticated: true };
else if (path.endsWith("/workspace-items")) value = { items: [...chats.values()].map(chat => ({ ...chat, type: "chat" })) };
else if (path.endsWith("/models")) value = { providers: { openai: { models: Object.keys(capabilities), loadedAt: date, error: null,
...(withCapabilities ? { reasoningCapabilities: capabilities } : {}) }, anthropic: { models: ["claude-fixture"], reasoningCapabilities: {} } } };
else if (path.endsWith("/chat-tools") || path.endsWith("/agent/tools")) value = { tools: [] };
else if (path.endsWith("/active-runs")) value = { chats: [], searches: [] };
else if (path.endsWith("/agent/messages")) value = { conversationId: "1", messages: [], cursor: "0", beforeCursor: null, hasMore: false };
else if (path.endsWith("/agent")) value = { id: "1", title: "Sybil Agent", activity: { state: "idle", isThinking: false, tools: [], queuedMessages: [] }, protocol: "pi-room.v1", websocket: "/v1/agent/ws" };
else if (path.endsWith("/read-state")) value = { sourceId: "fixture", cursor: "0", updatedAt: null };
else if (path.endsWith("/push/config")) value = { enabled: false };
else if (path.endsWith("/chats") && body) {
const created = { ...chat, id: `new-${chats.size}`, title: body.title || "New chat", initiatedProvider: body.provider,
initiatedModel: body.model, lastUsedProvider: body.provider, lastUsedModel: body.model, reasoningEffort: body.reasoningEffort ?? null };
chats.set(created.id, created);
value = { chat: created };
} else if (id && chats.has(id)) {
const current = chats.get(id);
if (request.method() === "PATCH") Object.assign(current, body, { lastUsedProvider: body.provider ?? current.lastUsedProvider, lastUsedModel: body.model ?? current.lastUsedModel });
value = { chat: current };
} else if (path.endsWith("/chat-completions/stream")) {
const current = chats.get(body.chatId);
current.messages = [{ id: "answer", role: "assistant", content: "Fixture answer.", createdAt: date, metadata: null }];
return route.fulfill({ contentType: "text/event-stream", body: [
["meta", { chatId: current.id, callId: "call", provider: body.provider, model: body.model }],
["delta", { text: "Fixture answer." }], ["done", { text: "Fixture answer." }],
].map(([event, data]) => `event: ${event}\ndata: ${JSON.stringify(data)}\n\n`).join("") });
}
await route.fulfill({ json: value });
});
await context.routeWebSocket("**/api/v1/agent/ws", socket => socket.onMessage(raw => {
if (JSON.parse(String(raw)).type === "auth") socket.send(JSON.stringify({ type: "ready", conversationId: "1" }));
}));
const page = await context.newPage();
page.on("pageerror", error => errors.push(error.message));
await page.goto(new URL(`/?chat=${chat.id}`, target).href, { waitUntil: "networkidle" });
await page.locator("main h1").filter({ hasText: chat.title }).waitFor();
const dialog = page.getByRole("dialog", { name: "Chat settings", exact: true });
const slider = dialog.getByRole("slider", { name: "Reasoning effort" });
const open = async () => { await page.getByRole("button", { name: "Open chat settings", exact: true }).click(); await dialog.waitFor(); };
const changeModel = async model => { const input = dialog.getByRole("combobox", { name: "Model", exact: true }); await input.fill(model); await input.press("Enter"); };
const save = async () => { await dialog.getByRole("button", { name: "Save", exact: true }).click(); await dialog.waitFor({ state: "hidden" }); };
return { context, page, dialog, slider, open, changeModel, save, chat, writes, errors };
}
try {
for (const viewport of [{ width: 1280, height: 900 }, { width: 390, height: 844 }]) {
const f = await fixture(viewport);
const { page, dialog, slider, chat, writes } = f;
await f.open();
assert.equal(await slider.getAttribute("aria-valuetext"), "Model default (None)");
await slider.press("End");
await slider.press("ArrowLeft");
assert.equal(await slider.getAttribute("aria-valuetext"), "High");
await dialog.getByRole("button", { name: "Cancel", exact: true }).click();
assert.equal(writes.filter(write => write.path.endsWith(`/chats/${chat.id}`)).length, 0);
await f.open();
assert.equal(await slider.getAttribute("aria-valuetext"), "Model default (None)");
await slider.press("End");
await slider.press("ArrowLeft");
await f.save();
assert.equal(chat.reasoningEffort, "high");
assert.equal(writes.at(-1).body.model, "gpt-5.2");
await page.reload({ waitUntil: "networkidle" });
await f.open();
assert.equal(await slider.getAttribute("aria-valuetext"), "High");
await f.changeModel("gpt-6.1-sol");
assert.equal(await slider.getAttribute("aria-valuetext"), "Model default (Medium)");
await slider.press("Home");
await slider.press("ArrowRight");
assert.equal(await slider.getAttribute("aria-valuetext"), "Low"); // no unsupported None/Minimal
await f.changeModel("gpt-5.2-pro");
assert.equal(await slider.count(), 0);
await dialog.getByText(/Reasoning settings aren’t available/).waitFor();
await f.changeModel("gpt-5.2");
assert.equal(await slider.getAttribute("aria-valuetext"), "High");
await dialog.getByRole("combobox", { name: "Provider", exact: true }).selectOption("anthropic");
assert.equal(await slider.count(), 0);
await dialog.getByRole("button", { name: "Cancel", exact: true }).click();
await page.keyboard.press("Control+j");
await f.open();
assert.equal(await slider.getAttribute("aria-valuetext"), "High");
const bounds = await dialog.boundingBox();
assert.ok(bounds.x >= 0 && bounds.x + bounds.width <= viewport.width + 1);
if (process.env.REASONING_SCREENSHOT_DIR) await page.screenshot({ path: `${process.env.REASONING_SCREENSHOT_DIR}/reasoning-${viewport.width}.png` });
await f.save();
const composer = page.locator("#composer-input");
await composer.fill("Test remembered effort");
const submitted = page.waitForResponse(response => response.url().endsWith("/chat-completions/stream"));
await composer.press("Enter");
await submitted;
assert.equal(writes.findLast(write => write.path.endsWith("/chats")).body.reasoningEffort, "high");
assert.equal(writes.findLast(write => write.path.endsWith("/chat-completions/stream")).body.reasoningEffort, "high");
await page.getByText("Fixture answer.", { exact: true }).waitFor();
await f.open();
await slider.press("Home");
await f.save();
assert.equal(writes.findLast(write => /\/chats\/[^/]+$/.test(write.path)).body.reasoningEffort, null);
assert.deepEqual(f.errors, []);
await f.context.close();
}
const oldServer = await fixture({ width: 1280, height: 900 }, false);
await oldServer.open();
assert.equal(await oldServer.slider.count(), 0);
await oldServer.dialog.getByText(/Reasoning settings aren’t available/).waitFor();
await oldServer.save();
assert.equal(oldServer.chat.reasoningEffort, null);
assert.deepEqual(oldServer.errors, []);
await oldServer.context.close();
console.log("Reasoning settings browser checks passed (desktop, mobile, missing capabilities).");
} finally { await browser.close(); }
+39
View File
@@ -0,0 +1,39 @@
import assert from "node:assert/strict";
import test from "node:test";
import { getReasoningCapabilities, loadReasoningPreferences, normalizeReasoningEffort, reasoningPreferenceKey, saveReasoningPreferences } from "../src/lib/reasoning-effort.ts";
const capabilities = { efforts: ["none", "low", "medium", "high", "xhigh"], defaultEffort: "none" };
const catalog = { openai: { reasoningCapabilities: { "gpt-5.2": capabilities } } };
test("capabilities come only from the selected provider/model and a valid catalog entry", () => {
assert.deepEqual(getReasoningCapabilities(catalog, "openai", " gpt-5.2 "), capabilities);
for (const [data, provider, model] of [[{}, "openai", "gpt-5.2"], [catalog, "xai", "gpt-5.2"], [catalog, "openai", "gpt-5.2-new"]]) {
assert.equal(getReasoningCapabilities(data, provider, model), null);
}
for (const entry of [{}, { efforts: [] }, { efforts: ["future"], defaultEffort: "future" }, { efforts: ["high"], defaultEffort: "none" }]) {
assert.equal(getReasoningCapabilities({ openai: { reasoningCapabilities: { model: entry } } }, "openai", "model"), null);
}
assert.equal(normalizeReasoningEffort(capabilities, "xhigh"), "xhigh");
assert.equal(normalizeReasoningEffort(capabilities, "max"), null);
assert.equal(normalizeReasoningEffort(null, "high"), null);
assert.equal(normalizeReasoningEffort(capabilities, null), null);
});
test("browser memory is per provider/model and survives corrupt or unavailable storage", (t) => {
const original = globalThis.window;
t.after(() => { globalThis.window = original; });
let stored = "{}";
globalThis.window = { localStorage: { getItem: () => stored, setItem: (_, value) => { stored = value; } } };
const key = reasoningPreferenceKey("openai", "gpt-5.2");
assert.notEqual(key, reasoningPreferenceKey("xai", "gpt-5.2"));
assert.notEqual(key, reasoningPreferenceKey("openai", "gpt-5.1"));
assert.equal(saveReasoningPreferences({ [key]: "high" }), true);
assert.deepEqual(loadReasoningPreferences(), { [key]: "high" });
stored = JSON.stringify({ [key]: "future", other: null });
assert.deepEqual(loadReasoningPreferences(), { other: null });
stored = "broken JSON";
assert.deepEqual(loadReasoningPreferences(), {});
globalThis.window = { get localStorage() { throw new Error("blocked"); } };
assert.deepEqual(loadReasoningPreferences(), {});
assert.equal(saveReasoningPreferences({ [key]: "high" }), false);
});