diff --git a/app/src/lib/models/effort.ts b/app/src/lib/models/effort.ts index 0a0f253d4e..557d05aa84 100644 --- a/app/src/lib/models/effort.ts +++ b/app/src/lib/models/effort.ts @@ -137,13 +137,23 @@ export function shownPick( ? connectionOf(catalogue, saved.connection) : undefined; // A stopped connection's pick is not moved anywhere: its run is refused and the person told so. - if (named && !named.ready && saved?.model) + const stoppedModel = saved?.model || named?.default; + if (named && !named.ready && stoppedModel) return { - model: saved.model, - effort: saved.effort ?? null, + model: stoppedModel, + effort: saved?.effort ?? null, connection: named.id, }; - if (named && optionFor(catalogueFor(catalogue, named), saved?.model)) { + /* + * A pick on a connection with no model named is that connection's own default: the server keeps + * a picked model that is its connection's default as null (`ModelChoice`), and runs it there. Shown + * as the default connection's default instead, a pick of Claude Opus on a Claude plan read back as + * a ChatGPT model once the conversation opened, while the run went on Opus. + */ + if ( + named && + (!saved?.model || optionFor(catalogueFor(catalogue, named), saved.model)) + ) { const pick = shownPickIn(saved, catalogueFor(catalogue, named)); return pick && { ...pick, connection: named.id }; } diff --git a/app/src/lib/models/run-model.ts b/app/src/lib/models/run-model.ts index c919fb6bfe..8660a3e292 100644 --- a/app/src/lib/models/run-model.ts +++ b/app/src/lib/models/run-model.ts @@ -287,8 +287,9 @@ function sayModelNotUsed(bot: AgentProfile) { * It starts on the catalogue's default. `pickFor` is what `startPicked` (and a start with an `@`) * asks once the Bot is known: the pick to save with that Bot's conversation, before the first * message runs, and the line to say once it has started when that Bot takes no pick - * (`takesNoModelChoice`). Nothing to save, and nothing to say, while the pick is the default. A Bot - * that could not be read takes the pick, as `useBotTakesModelChoice` reads it. + * (`takesNoModelChoice`). Nothing to save, and nothing to say, while the pick is the default: the + * Bot's own default runs. Any other pick runs, whatever the Bot's default is. A Bot that could not + * be read takes the pick, as `useBotTakesModelChoice` reads it. */ export function usePickedBotModel( catalogue: ModelCatalogue | undefined, @@ -298,15 +299,31 @@ export function usePickedBotModel( const pickFor = async ( agentId: string, ): Promise<{ pick: ConversationModelInput | null; notUsed?: () => void }> => { - const pick = draft.requested; - if (!pick) return { pick: null }; + // On the default (untouched, Auto, or picked by hand), the pick follows the Bot's own default. + const shown = draft.value; + if (!draft.requested || !shown) return { pick: null }; const bot = roster?.find((agent) => agent.id === agentId) ?? (await queryClient .fetchQuery(agentQueryOptions(agentId)) .catch(() => undefined)); - if (!bot || !(await takesNoModelChoice(queryClient, bot))) return { pick }; - return { pick: null, notUsed: () => sayModelNotUsed(bot) }; + if (bot && (await takesNoModelChoice(queryClient, bot))) + return { pick: null, notUsed: () => sayModelNotUsed(bot) }; + /* + * THE PERSON'S PICK OVER THE BOT'S DEFAULT. What the picker showed is saved as it reads against + * the Bot the message went to. Saved against the catalogue's default instead, a pick of the + * default model at another effort was kept as "the default", and the Bot's own default replaced + * it in that conversation, in the run and in the picker. Against the Bot's default, the default + * model is named outright (`pinModel`), and nothing is saved only for the Bot's default itself. + */ + const request = requestFor( + shown, + catalogue, + offeredBotDefault(bot?.defaultModel, catalogue), + ); + return { + pick: request.model === null && request.effort === null ? null : request, + }; }; return { ...draft, pickFor }; } diff --git a/app/tests/model-picker-connections.test.tsx b/app/tests/model-picker-connections.test.tsx index 13a888343e..ba4ce2db1c 100644 --- a/app/tests/model-picker-connections.test.tsx +++ b/app/tests/model-picker-connections.test.tsx @@ -360,6 +360,25 @@ test("a model on a connection that isn't the default is saved with its connectio ).toEqual({ model: OPUS.id, effort: "high", connection: "anthropic" }); }); +/* + * Reported from dogfooding: the read OpenBot Cloud answered for a pick of Claude Opus 5.5 at Extra + * high on a Claude plan beside a ChatGPT default. The server keeps a model that is its connection's + * own default as null, on that connection, and runs that default there; the picker showed the + * ChatGPT default instead. + */ +test("a pick on a connection that names no model shows that connection's default, which it runs on", () => { + const read = { model: null, effort: "high", connection: "anthropic" }; + expect(shownPick(read, TWO)).toEqual({ + model: OPUS.id, + effort: "high", + connection: "anthropic", + }); + // Saved again as shown, it stays on that connection. + expect( + requestFor(shownPick(read, TWO) as NonNullable, TWO), + ).toEqual({ model: OPUS.id, effort: "high", connection: "anthropic" }); +}); + test("no default connection: the trigger asks to choose, and a pick still saves", async () => { const noDefault = catalogueOf([{ ...CHATGPT, isDefault: false }, CLAUDE]); const picks: ModelPick[] = []; diff --git a/app/tests/new-conversation-picked-model.test.tsx b/app/tests/new-conversation-picked-model.test.tsx index 2ad6245e72..0ba3bdf2af 100644 --- a/app/tests/new-conversation-picked-model.test.tsx +++ b/app/tests/new-conversation-picked-model.test.tsx @@ -106,10 +106,126 @@ const CATALOGUE = { ], }; -type Pick = { model: string | null; effort: string | null }; +/* + * A PERSON WITH SEVERAL AI CONNECTIONS, as reported from dogfooding on OpenBot Cloud: a ChatGPT + * plan, their default, whose GPT-6.2 Sol thinks at Low unless asked, and a Claude plan, whose + * default is Claude Opus 5.5. They picked Claude Opus 5.5 at Extra high on a new message, OpenBot + * handed it to General Assistant, and the conversation's picker then said GPT-6.2 Sol · Low while + * the run went on Opus: the server keeps a picked model that is its connection's own default as + * null, on that connection, and the picker read null as the default connection's default. + */ +const SOL = { + id: "gpt-6.2-sol", + label: "GPT-6.2 Sol", + vendor: "openai", + efforts: ["low", "medium", "high", "xhigh"], + defaultEffort: "low", +}; +const ASTRA = { + id: "gpt-6.2-astra", + label: "GPT-6.2 Astra", + vendor: "openai", + efforts: ["low", "medium", "high", "xhigh"], + defaultEffort: "medium", +}; +const OPUS = { + id: "claude-opus-5-5", + label: "Claude Opus 5.5", + vendor: "anthropic", + efforts: ["low", "medium", "high", "xhigh", "max"], + defaultEffort: "high", +}; +const SONNET = { + id: "claude-sonnet-5-5", + label: "Claude Sonnet 5.5", + vendor: "anthropic", + efforts: ["low", "medium", "high"], + defaultEffort: "medium", +}; +const CHATGPT_PLAN = { + id: "openai", + owner: "personal", + provider: "openai", + login: "plan", + source: "chatgpt", + isDefault: true, + ready: true, + problem: null, + default: SOL.id, + models: [SOL, ASTRA], + listed: true, +}; +const CLAUDE_PLAN = { + id: "anthropic", + owner: "personal", + provider: "anthropic", + login: "plan", + source: "claude", + isDefault: false, + ready: true, + problem: null, + default: OPUS.id, + models: [OPUS, SONNET], + listed: true, +}; +const PLANS = { + provider: "openai", + source: "chatgpt", + exposure: "all", + default: SOL.id, + choosable: true, + models: CHATGPT_PLAN.models, + connections: [CHATGPT_PLAN, CLAUDE_PLAN], +}; +type Connection = typeof CHATGPT_PLAN; +/** Built into OpenBot, with no default model of its own: the run takes the person's pick. */ +const GENERAL = bot("general-assistant", "General Assistant", true); +/** A Bot whose owner set its default model, GPT-6.2 Astra, on a ChatGPT plan. */ +const ASTRA_BOT = { + ...bot("astra-bot", "Astra Bot", true), + defaultModel: { model: ASTRA.id, effort: null, source: "chatgpt" }, +}; + +type Pick = { + model: string | null; + effort: string | null; + connection?: string | null; +}; + +/** What the server knows: the catalogue the person picks from, and their Bots. */ +let catalogue: Record = CATALOGUE; +let roster: (typeof TERRA | typeof ASTRA_BOT)[] = ROSTER; +/** The connections a pick can name, the default first. None: one connection, as before. */ +const connectionsOf = () => + (catalogue.connections as Connection[] | undefined) ?? []; +/** A connection by id, or the default for none. */ +const connectionNamed = (id: string | null | undefined) => + connectionsOf().find((one) => (id ? one.id === id : one.isDefault)); +/** The default of the Bot a conversation is with, as saved when it was made. */ +const botDefaultOf = (botId: string) => + (roster.find((one) => one.id === botId)?.defaultModel ?? null) as { + model: string; + effort: string | null; + source: string; + } | null; + +/** + * What a conversation's run answers on, as the server decides it (channels/model-choice.ts on the + * server, `resolveFor`): a pick on a connection runs there, its model or that connection's default; + * no model picked, the Bot's default; otherwise the pick, or the default. + */ +function runModelOf(conversation: { botId: string; pick: Pick }): string { + const { pick } = conversation; + const named = pick.connection ? connectionNamed(pick.connection) : undefined; + if (named) return `${pick.model ?? named.default} ${pick.effort}`; + const botDefault = botDefaultOf(conversation.botId); + if (!pick.model && botDefault) + return `${botDefault.model} ${botDefault.effort}`; + return `${pick.model ?? catalogue.default} ${pick.effort}`; +} /** The Bot OpenBot picks for the next message. */ -let routeTo = TERRA; +let routeTo: typeof TERRA | typeof ASTRA_BOT = TERRA; /** * The header each page sends with every request, naming the page. The server is shared by every * test in the file, and a page closed at the end of one test can still have a request on its way @@ -131,7 +247,13 @@ let made = 0; /** The conversations made, by id: their Bot, their thread and the model pick saved for them. */ const conversations = new Map< string, - { botId: string; threadId: string; pick: Pick } + { + botId: string; + threadId: string; + pick: Pick; + /** The model and effort its last run answered on, as `runModelOf` decides. */ + ranOn?: string; + } >(); let port = 0; @@ -156,7 +278,7 @@ function run(threadId: string, runId: string): Response { function conversationBody(id: string) { const conversation = conversations.get(id); - const owner = ROSTER.find((one) => one.id === conversation?.botId) ?? TERRA; + const owner = roster.find((one) => one.id === conversation?.botId) ?? TERRA; return { id, name: owner.name, @@ -174,7 +296,7 @@ async function answer(request: Request, path: string): Promise { return json({ version: "fixture", agents: Object.fromEntries( - ROSTER.map((one) => [ + roster.map((one) => [ one.id, { description: "Fixture", capabilities: {} }, ]), @@ -185,13 +307,16 @@ async function answer(request: Request, path: string): Promise { if (/\/agent\/[^/]+\/run$/.test(path)) { const body = (await request.json()) as { threadId: string; runId: string }; note(request, "run"); + for (const conversation of conversations.values()) + if (conversation.threadId === body.threadId) + conversation.ranOn = runModelOf(conversation); return run(body.threadId, body.runId); } if (path === ROUTE_PATH && request.method === "POST") { const body = (await request.json()) as { botId?: string }; // Recorded for a Bot the person named; picked by OpenBot for a message nobody was named for. if (!body.botId) note(request, "route"); - const to = ROSTER.find((one) => one.id === body.botId) ?? routeTo; + const to = roster.find((one) => one.id === body.botId) ?? routeTo; return json({ botId: to.id, name: to.name, @@ -224,13 +349,32 @@ async function answer(request: Request, path: string): Promise { }, ); if (request.method === "PUT") { - const pick = (await request.json()) as Pick; + const pick = (await request.json()) as Pick & { pinModel?: boolean }; note(request, `save ${pick.model} ${pick.effort}`); - conversation.pick = { model: pick.model, effort: pick.effort }; + /* + * As the server keeps it (`saveOn`): a model that is its connection's own default is kept as + * null, unless it is pinned, and a pick on a connection keeps that connection. + */ + const on = connectionNamed(pick.connection); + const own = on?.default ?? catalogue.default; + conversation.pick = { + model: pick.model === own && !pick.pinModel ? null : pick.model, + effort: pick.effort, + ...(pick.connection && on ? { connection: on.id } : {}), + }; } - return json(conversation.pick); + // As a read answers it: the connection a pick runs on, and the Bot's default it was made with. + const botDefault = botDefaultOf(conversation.botId); + return json({ + model: conversation.pick.model, + effort: conversation.pick.effort, + ...(connectionsOf().length > 0 + ? { connection: conversation.pick.connection ?? null, stopped: null } + : {}), + ...(botDefault ? { defaultModel: botDefault } : {}), + }); } - if (path === MODEL_CATALOGUE_PATH) return json(catalogueBody(CATALOGUE)); + if (path === MODEL_CATALOGUE_PATH) return json(catalogueBody(catalogue)); const conversation = path.match(/\/conversations\/([^/]+)$/); if (conversation && conversations.has(conversation[1])) return json({ conversation: conversationBody(conversation[1]) }); @@ -242,8 +386,8 @@ async function answer(request: Request, path: string): Promise { return json({ sessions: [], nextCursor: null }); if (/\/threads\/[^/]+\/messages$/.test(path)) return json({ messages: [] }); if (path === `${ORGANIZATION_PATH}/bots`) - return json({ bots: ROSTER, nextCursor: null }); - for (const one of ROSTER) { + return json({ bots: roster, nextCursor: null }); + for (const one of roster) { if (path === `${ORGANIZATION_PATH}/bots/${one.id}`) return json({ bot: one }); // How the Bot works: on its own, so a Bot on OpenBot's model takes a pick. @@ -462,6 +606,7 @@ async function open( width = 1440, phone = false, mode = "all", + plans = false, }: { theme?: Theme; width?: number; @@ -469,6 +614,8 @@ async function open( phone?: boolean; /** How the picker offers models (lib/models/picker-mode.ts). */ mode?: "all" | "simplified"; + /** A ChatGPT plan and a Claude plan, and the Bots General Assistant and Astra Bot (`PLANS`). */ + plans?: boolean; } = {}, ): Promise { const browser = browsers.get(engine); @@ -477,7 +624,9 @@ async function open( const id = `page-${pages}`; trail = []; trails.set(id, trail); - routeTo = TERRA; + catalogue = plans ? PLANS : CATALOGUE; + roster = plans ? [GENERAL, ASTRA_BOT] : ROSTER; + routeTo = plans ? GENERAL : TERRA; const page = await browser.newPage({ ...(phone ? phones.get(engine) @@ -655,6 +804,117 @@ describe.skipIf(!asked)( await page.close(); } }, 60_000); + + /* + * Reported from dogfooding: Claude Opus 5.5 at Extra high, picked on a new message, ran on + * Opus, but the conversation it opened on General Assistant said GPT-6.2 Sol · Low. + */ + for (const layout of [ + { name: "desktop-light", phone: false, theme: "light" }, + { name: "phone-dark", phone: true, theme: "dark" }, + ] as const) + test(`a model picked on another AI connection is the one the Bot's conversation shows and runs (${engine}, ${layout.name})`, async () => { + const page = await open(engine, "/channel/new", { + plans: true, + phone: layout.phone, + theme: layout.theme, + }); + const shot = (step: string) => + `model-pick-handoff-${step}-${engine}-${layout.name}`; + try { + await still(page); + await shoot(page, shot("1-new")); + await picker(page, "GPT-6.2 Sol, thinking: Low").click({ + timeout: 5_000, + }); + await page.getByRole("button", { name: /^Claude plan/ }).click(); + await page.getByRole("option", { name: "Claude Opus 5.5" }).click(); + await page.getByRole("option", { name: "Extra high" }).click(); + await picker( + page, + "Claude Opus 5.5, thinking: Extra high", + ).waitFor(); + await shoot(page, shot("2-picked")); + if (layout.phone) { + // On a phone, Return is a new line: the Send button sends. + await page.getByRole("textbox", { name: "Message" }).tap(); + await page.keyboard.type(QUESTION); + await page.getByRole("button", { name: /^Send/ }).tap(); + } else await send(page); + await page.waitForURL(/\/channel\/conversation-\d+$/); + const id = new URL(page.url()).pathname.split("/").pop() ?? ""; + await page + .getByRole("log") + .filter({ hasText: ANSWER }) + .waitFor({ timeout: 15_000 }); + // Kept as the server keeps it: that connection's default, on that connection. + expect(conversations.get(id)).toMatchObject({ + botId: GENERAL.id, + pick: { model: null, effort: "xhigh", connection: "anthropic" }, + ranOn: `${OPUS.id} xhigh`, + }); + // The conversation's picker says what the run answers on. + await picker(page, "Claude Opus 5.5, thinking: Extra high").waitFor( + { + timeout: 5_000, + }, + ); + await shoot(page, shot("3-conversation")); + } finally { + await page.close(); + } + }, 60_000); + + test(`a pick that isn't the Bot's default runs over it, and the conversation shows it (${engine})`, async () => { + const page = await open(engine, "/channel/new", { plans: true }); + try { + await still(page); + routeTo = ASTRA_BOT; + // The default model, at a harder effort than its own. + await picker(page, "GPT-6.2 Sol, thinking: Low").click({ + timeout: 5_000, + }); + await page.getByRole("option", { name: "High", exact: true }).click(); + await picker(page, "GPT-6.2 Sol, thinking: High").waitFor(); + await send(page); + await page.waitForURL(/\/channel\/conversation-\d+$/); + const id = new URL(page.url()).pathname.split("/").pop() ?? ""; + await page + .getByRole("log") + .filter({ hasText: ANSWER }) + .waitFor({ timeout: 15_000 }); + await shoot(page, `model-pick-handoff-4-bot-default-${engine}`); + // Not GPT-6.2 Astra, the Bot's own default: the person picked. + expect(conversations.get(id)?.ranOn).toBe(`${SOL.id} high`); + await picker(page, "GPT-6.2 Sol, thinking: High").waitFor({ + timeout: 5_000, + }); + } finally { + await page.close(); + } + }, 60_000); + + test(`untouched, a new message runs on the Bot's own default, and says so (${engine})`, async () => { + const page = await open(engine, "/channel/new", { plans: true }); + try { + await still(page); + routeTo = ASTRA_BOT; + await send(page); + await page.waitForURL(/\/channel\/conversation-\d+$/); + const id = new URL(page.url()).pathname.split("/").pop() ?? ""; + await page + .getByRole("log") + .filter({ hasText: ANSWER }) + .waitFor({ timeout: 15_000 }); + expect(trail.some((step) => step.startsWith("save"))).toBe(false); + expect(conversations.get(id)?.ranOn).toBe(`${ASTRA.id} null`); + await picker(page, "GPT-6.2 Astra, thinking: Medium").waitFor({ + timeout: 5_000, + }); + } finally { + await page.close(); + } + }, 60_000); } }, );