factory 0.1.45: optional model ordering by company (#959)

An opt-in switch, "Group models by company", orders Factory's models by maker: Claude (Fable, Opus, Sonnet, Haiku), GPT, Gemini, Grok, GLM, Kimi, then the rest. Within a family, newer versions come first, and each Fast Mode model sits right after its base. Off, which is the default, keeps main's order byte for byte. Only a strict `true` from magpie's option turns it on. factory 0.1.45.

Run at the reviewed head fe96f7eb67b308a31ce33492fd3c5d5f19a4f5b1:
- On mote at 9b19aa76: bun test packages/factory 196 pass (main 194). Main's and this index.mjs were compared as JSON of the config hook and provider.models: off is identical to main for absent, {}, false, "true", 1, null, a throwing option and `option: 5`; on, there are 59 models, the same set, with no duplicates.
- On the Windows box at fe96f7eb (mote's sshd was refusing connections): bun test packages/factory 196 pass, bun scripts/check.mjs exit 0.
- Broken by hand at fe96f7eb: grouping always on turns both order.test.mjs tests red. Before the review's fix, it stayed green.
- npm has 0.1.44.

Not tried: in a real magpie window.
This commit is contained in:
Mars-Sea
2026-11-11 10:48:37 +08:00
committed by GitHub
parent 2ee2ee84f9
commit 86d3febf9b
5 changed files with 105 additions and 4 deletions
+10
View File
@@ -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 |
+37 -2
View File
@@ -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",
+56
View File
@@ -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)
}
})
+1 -1
View File
@@ -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",
+1 -1
View File
@@ -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)