diff --git a/packages/factory/README.md b/packages/factory/README.md index ac86918..23fcaad 100644 --- a/packages/factory/README.md +++ b/packages/factory/README.md @@ -59,6 +59,16 @@ see [Claude Code through magpie](#claude-code-through-magpie) below. ## Models +In magpie's Factory plugin options, enable **Group models by company** to +list Anthropic's Fable, Opus, Sonnet and Haiku families together, followed +by OpenAI, Google and the other model makers. Versions within each family +sort newest first; Fast Mode follows its base model. This groups by model +maker, not the API hosting company. Refresh the model list after changing +the option. It is off by default, preserving the existing order and default +first 24 models; enabling it also changes which models occupy those slots. +Names with the same version sort in descending natural name order, for +example GPT-5.6 Terra before Sol and GLM-5.3-Flash before GLM-5.3. + Each model is served on the one API droid uses for it: | API | Models | diff --git a/packages/factory/index.mjs b/packages/factory/index.mjs index d0d90c5..5edf328 100644 --- a/packages/factory/index.mjs +++ b/packages/factory/index.mjs @@ -137,6 +137,28 @@ const MODELS = [ ["glm-5.2-fast", "GLM-5.2 Fast", CHAT, "baseten", 524288, 131072, ["none", "high", "max"], false, 0.84], ].map(([id, name, wire, upstream, context, output, efforts, images, rate]) => ({ id, name, wire, upstream, context, output, efforts, images, rate })) +// Model makers, not API hosts: e.g. GLM and Kimi both use Fireworks. +const MODEL_FAMILIES = [ + "claude-fable-", "claude-opus-", "claude-sonnet-", "claude-haiku-", + "gpt-", "gemini-", "grok-", "glm-", "kimi-", "deepseek-", "qwen", + "minimax-", "mistral-", "nemotron-", "muse-", "inkling", "beam-", +] +const MODEL_ORDER = "group_models_by_company" +function groupedModels(models) { + const family = (id) => { + const index = MODEL_FAMILIES.findIndex((prefix) => id.startsWith(prefix)) + return index < 0 ? MODEL_FAMILIES.length : index + } + const base = (id) => id.replace(/-fast$/, "") + return Object.fromEntries(Object.entries(models).sort(([a], [b]) => { + const group = family(a) - family(b) + if (group) return group + const aa = base(a), bb = base(b) + if (aa === bb) return Number(a.endsWith("-fast")) - Number(b.endsWith("-fast")) + return bb.localeCompare(aa, "en", { numeric: true }) + })) +} + const byId = new Map(MODELS.map((m) => [m.id, m])) // A Fast Mode model is its base model served faster for more credits. @@ -1782,6 +1804,14 @@ export const FactoryAuthPlugin = async ({ client, magpie }) => { return false } } + const orderModels = (models) => { + try { + if (magpie?.option?.(PROVIDER, MODEL_ORDER) === true) return groupedModels(models) + } catch { + // Hosts without plugin options keep the existing picker order. + } + return models + } // the session id this process's requests carry const session = crypto.randomUUID() // serializes checking, rotating and saving tokens: WorkOS rotates the @@ -2004,7 +2034,7 @@ export const FactoryAuthPlugin = async ({ client, magpie }) => { provider: { id: PROVIDER, async models(provider) { - return providerModels(provider) + return orderModels(providerModels(provider)) }, }, config: async (config) => { @@ -2015,7 +2045,7 @@ export const FactoryAuthPlugin = async ({ client, magpie }) => { npm: CHAT.npm, api: CHAT.api, ...was, - models: { ...configModels(), ...(was.models ?? {}) }, + models: orderModels({ ...configModels(), ...(was.models ?? {}) }), } }, auth: { @@ -2032,6 +2062,11 @@ export const FactoryAuthPlugin = async ({ client, magpie }) => { // magpie's own switches (input.magpie.option), read at each usage // reading options: [ + { + key: MODEL_ORDER, + label: "Group models by company", + description: "Groups models by maker: Anthropic, OpenAI, Google, then others. Newer versions come first within each family, with Fast Mode beside its base model. Off preserves the default order and first 24 models. Refresh the model list after changing this option.", + }, { key: STANDARD_ONLY, label: "Standard usage only", diff --git a/packages/factory/order.test.mjs b/packages/factory/order.test.mjs new file mode 100644 index 0000000..bf1bf3b --- /dev/null +++ b/packages/factory/order.test.mjs @@ -0,0 +1,56 @@ +import { expect, test } from "bun:test" +import { FactoryAuthPlugin } from "./index.mjs" + +async function setup(magpie) { + return FactoryAuthPlugin({ client: { auth: { set: async () => {} } }, magpie }) +} +async function lists(hooks) { + const cfg = { provider: {} } + await hooks.config(cfg) + return [cfg.provider.factory.models, await hooks.provider.models({ id: "factory" }, {})] +} + +test("company ordering is opt-in, preserves metadata, and is read on each model listing", async () => { + let enabled = false + const hooks = await setup({ option: (_, key) => key === "group_models_by_company" && enabled }) + expect(hooks.auth.options.find((o) => o.key === "group_models_by_company")).toBeDefined() + const original = await lists(await setup()) + const originalOrder = original.map((models) => Object.keys(models)) + // Anchor the baseline independently of the switch: always-on grouping must fail. + expect(originalOrder[0][0]).toBe("claude-fable-5.1") + expect((await lists(hooks)).map((models) => Object.keys(models))).toEqual(originalOrder) + enabled = true + const grouped = await lists(hooks) + expect(Object.keys(grouped[0])).toEqual(Object.keys(grouped[1])) + for (const [index, models] of grouped.entries()) { + expect(models).toEqual(original[index]) + const ids = Object.keys(models) + const before = (a, b) => expect(ids.indexOf(a)).toBeLessThan(ids.indexOf(b)) + expect(ids[0]).toBe("claude-fable-5.2") + before("claude-fable-5.2", "claude-fable-5.1") + before("claude-fable-5", "claude-opus-5-5") + before("claude-opus-5-5", "claude-opus-5") + before("claude-haiku-4-5-20251001", "gpt-6.1-sol") + before("gpt-6.1-sol", "gpt-6-sol") + before("gpt-6-luna", "gpt-5.6-sol") + before("gpt-5.3-codex-fast", "gemini-4-carbon") + before("gemini-4-carbon", "gemini-4-argon") + before("gemini-3.8-flash", "gemini-3.1-pro-preview") + before("glm-5.2-fast", "kimi-k3.1-pro") + for (const id of ids.filter((id) => id.endsWith("-fast"))) { + expect(ids.indexOf(id)).toBe(ids.indexOf(id.slice(0, -5)) + 1) + } + } + enabled = false + expect((await lists(hooks)).map((models) => Object.keys(models))).toEqual(originalOrder) +}) + +test("absent, false, non-boolean and throwing options retain the default order", async () => { + const original = (await lists(await setup())).map((models) => Object.keys(models)) + expect(original[0][0]).toBe("claude-fable-5.1") + const hosts = [undefined, {}, { option: 5 }, { option() { throw Error("unsupported") } }, + ...[false, "true", 1, null].map((value) => ({ option: () => value }))] + for (const host of hosts) { + expect((await lists(await setup(host))).map((models) => Object.keys(models))).toEqual(original) + } +}) diff --git a/packages/factory/package.json b/packages/factory/package.json index 61ffbdf..bb717d9 100644 --- a/packages/factory/package.json +++ b/packages/factory/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-factory-auth", - "version": "0.1.44", + "version": "0.1.45", "description": "Sign in to a Factory (Droid) subscription from OpenCode or magpie", "type": "module", "main": "./opencode.mjs", diff --git a/packages/factory/usage.test.mjs b/packages/factory/usage.test.mjs index cd57ab7..870e01b 100644 --- a/packages/factory/usage.test.mjs +++ b/packages/factory/usage.test.mjs @@ -167,7 +167,7 @@ test("standard usage only is a switch magpie shows, off by default, read at each } const p = await plugin(account(), (path) => (path === "/api/billing/limits" ? json(LIMITS) : new Response("", { status: 404 })), magpie) // declared as magpie's host keeps a switch: a key of [a-z0-9_-], a label, off unless said - const o = p.hooks.auth.options + const o = p.hooks.auth.options.filter((option) => option.key === "standard_only") expect(o).toEqual([{ key: "standard_only", label: "Standard usage only", description: expect.any(String) }]) expect(o[0].key).toMatch(/^[a-z0-9_-]{1,40}$/) expect(o[0].label.length).toBeLessThanOrEqual(60)