From 4a35799021fc30ef2f2967849f5ed9371203ced0 Mon Sep 17 00:00:00 2001 From: Mars-Sea <33591741+Mars-Sea@users.noreply.github.com> Date: Fri, 16 Oct 2026 17:36:20 +0800 Subject: [PATCH] factory 0.1.32: a model's token multiplier reaches magpie as its rate (#158) Every model in MODELS carries the tokenMultiplier droid 0.241.1's registry has for it (testdata/droid-0.241.1-costs.txt, byte for byte). A promotion running takes its discount off it, with the multiplier before it as rateWas; one that has ended or not begun is no discount. The rate reaches magpie through a provider hook (host.js reads rate/rateWas from its models; a config hook's models carry no rate there). Fast Mode entries stay last. Reviewed at 498b8723e5350657aa26f8f0e2bb1d859f59803b: - each multiplier and promotion checked against the testdata excerpt; gpt-5.6-sol's expired M promotion is skipped and Pt (20%) applies, as droid's first-running-promotion rule gives - host.js info(): the provider hook's list replaces the config list, and the request path strips temperature for OpenAI models in droidBody, so capabilities.temperature=true in providerModel changes nothing sent - factory unchanged on main between the PR's base 7687314 and fe5631c Run (HOME=$(mktemp -d), PR head merged with main fe5631c): - bun test: 1620 pass, 2 fail; the two are cursor latency.test reds under the full run at load ~260, already filed as #159 and #162; packages/cursor alone: 267 pass, 0 fail - bun test packages/factory: 149 pass, 0 fail - packages/factory/rate.test.mjs against main's index.mjs: 5 fail - bun scripts/check.mjs: passes Not tried with a real Factory account here (the author did, 46 models). --- packages/factory/README.md | 8 + packages/factory/index.mjs | 178 +++++++++++++----- packages/factory/package.json | 2 +- packages/factory/rate.test.mjs | 119 ++++++++++++ .../factory/testdata/droid-0.241.1-costs.txt | 55 ++++++ 5 files changed, 312 insertions(+), 50 deletions(-) create mode 100644 packages/factory/rate.test.mjs create mode 100644 packages/factory/testdata/droid-0.241.1-costs.txt diff --git a/packages/factory/README.md b/packages/factory/README.md index 8824ec5..01ca184 100644 --- a/packages/factory/README.md +++ b/packages/factory/README.md @@ -73,6 +73,14 @@ Flash, Kimi K3 and GLM-5.2 have `none`, droid's "off": they stop thinking. Reasoning is mandatory on GLM-5.3 and GLM-5.3-Flash (Fireworks turns `none` away), so `low` is their least. +A model's price is its token multiplier in droid's registry +(`cost.tokenMultiplier`), which magpie shows beside the model as its rate +(`0.8×`), so a cheap one is picked without opening droid. A promotion running +comes off it, with the multiplier before the discount struck through beside +the price (`rateWas`): Mistral Large 4's launch discount, Gemini 4 Argon's, +Gemini 3.8/3.7 Flash's and GPT-5.6 Sol's, each while droid's own dates for it +hold. A promotion that has ended is no discount at all. + Gemini's requests go to Factory's generate route with the model in the body, droid's line opening the system instruction. Gemini 4 Argon is behind a Factory feature flag in droid 0.241.1, so an account it isn't on for may diff --git a/packages/factory/index.mjs b/packages/factory/index.mjs index 0fc8618..1e13b88 100644 --- a/packages/factory/index.mjs +++ b/packages/factory/index.mjs @@ -56,62 +56,62 @@ const E4 = ["low", "medium", "high", "xhigh"] // base models aren't offered), GPT-5.6 Sol Fast (unreleased:!0) and // GLM-5.2 Fast (behind a feature flag, on a Baseten model id of its own). const MODELS = [ - ["claude-fable-5.1", "Fable 5.1", ANTHROPIC, "anthropic", 867000, 128000, E5, true], - ["claude-fable-5", "Fable 5", ANTHROPIC, "anthropic", 867000, 128000, E5, true], - ["claude-opus-5-5", "Opus 5.5", ANTHROPIC, "anthropic", 872000, 128000, E5, true], - ["claude-opus-5", "Opus 5", ANTHROPIC, "anthropic", 867000, 128000, E5, true], - ["claude-opus-4-8", "Opus 4.8", ANTHROPIC, "anthropic", 867000, 128000, E5, true], - ["claude-sonnet-5-5", "Sonnet 5.5", ANTHROPIC, "anthropic", 872000, 128000, E5, true], - ["claude-sonnet-5", "Sonnet 5", ANTHROPIC, "anthropic", 872000, 128000, E5, true], - ["claude-sonnet-4-6", "Sonnet 4.6", ANTHROPIC, "anthropic", 931000, 64000, ["low", "medium", "high", "max"], true], - ["claude-haiku-5-5", "Haiku 5.5", ANTHROPIC, "anthropic", 872000, 128000, E5, true], - ["claude-haiku-4-5-20251001", "Haiku 4.5", ANTHROPIC, "anthropic", 0, 0, ["low", "medium", "high"], true], - ["gpt-6.1-sol", "GPT-6.1 Sol", RESPONSES, "openai", 1050000, 128000, E5, true], - ["gpt-6-sol", "GPT-6 Sol", RESPONSES, "openai", 1050000, 128000, E6, true], - ["gpt-6-astra", "GPT-6 Astra", RESPONSES, "openai", 1050000, 128000, E5, true], - ["gpt-6-luna", "GPT-6 Luna", RESPONSES, "openai", 1050000, 128000, E6, true], - ["gpt-5.6-sol", "GPT-5.6 Sol", RESPONSES, "openai", 1050000, 128000, E6, true], - ["gpt-5.6-terra", "GPT-5.6 Terra", RESPONSES, "openai", 1050000, 128000, E6, true], - ["gpt-5.6-luna", "GPT-5.6 Luna", RESPONSES, "openai", 1050000, 128000, E6, true], - ["gpt-5.5", "GPT-5.5", RESPONSES, "openai", 1050000, 128000, E4, true], - ["gpt-5.4", "GPT-5.4", RESPONSES, "openai", 1050000, 128000, E4, true], - ["gpt-5.3-codex", "GPT-5.3-Codex", RESPONSES, "openai", 400000, 128000, E4, true], - ["grok-4.7", "Grok 4.7", RESPONSES, "xai", 500000, 63356, E4, true], - ["grok-4.6", "Grok 4.6", RESPONSES, "xai", 200000, 63356, E4, true], + ["claude-fable-5.1", "Fable 5.1", ANTHROPIC, "anthropic", 867000, 128000, E5, true, 4], + ["claude-fable-5", "Fable 5", ANTHROPIC, "anthropic", 867000, 128000, E5, true, 4], + ["claude-opus-5-5", "Opus 5.5", ANTHROPIC, "anthropic", 872000, 128000, E5, true, 1.6], + ["claude-opus-5", "Opus 5", ANTHROPIC, "anthropic", 867000, 128000, E5, true, 2], + ["claude-opus-4-8", "Opus 4.8", ANTHROPIC, "anthropic", 867000, 128000, E5, true, 2], + ["claude-sonnet-5-5", "Sonnet 5.5", ANTHROPIC, "anthropic", 872000, 128000, E5, true, 0.8], + ["claude-sonnet-5", "Sonnet 5", ANTHROPIC, "anthropic", 872000, 128000, E5, true, 0.8], + ["claude-sonnet-4-6", "Sonnet 4.6", ANTHROPIC, "anthropic", 931000, 64000, ["low", "medium", "high", "max"], true, 1.2], + ["claude-haiku-5-5", "Haiku 5.5", ANTHROPIC, "anthropic", 872000, 128000, E5, true, 0.04], + ["claude-haiku-4-5-20251001", "Haiku 4.5", ANTHROPIC, "anthropic", 0, 0, ["low", "medium", "high"], true, 0.4], + ["gpt-6.1-sol", "GPT-6.1 Sol", RESPONSES, "openai", 1050000, 128000, E5, true, 0.8], + ["gpt-6-sol", "GPT-6 Sol", RESPONSES, "openai", 1050000, 128000, E6, true, 0.8], + ["gpt-6-astra", "GPT-6 Astra", RESPONSES, "openai", 1050000, 128000, E5, true, 4], + ["gpt-6-luna", "GPT-6 Luna", RESPONSES, "openai", 1050000, 128000, E6, true, 0.04], + ["gpt-5.6-sol", "GPT-5.6 Sol", RESPONSES, "openai", 1050000, 128000, E6, true, 2], + ["gpt-5.6-terra", "GPT-5.6 Terra", RESPONSES, "openai", 1050000, 128000, E6, true, 0.8], + ["gpt-5.6-luna", "GPT-5.6 Luna", RESPONSES, "openai", 1050000, 128000, E6, true, 0.08], + ["gpt-5.5", "GPT-5.5", RESPONSES, "openai", 1050000, 128000, E4, true, 2], + ["gpt-5.4", "GPT-5.4", RESPONSES, "openai", 1050000, 128000, E4, true, 1], + ["gpt-5.3-codex", "GPT-5.3-Codex", RESPONSES, "openai", 400000, 128000, E4, true, 0.7], + ["grok-4.7", "Grok 4.7", RESPONSES, "xai", 500000, 63356, E4, true, 0.8], + ["grok-4.6", "Grok 4.6", RESPONSES, "xai", 200000, 63356, E4, true, 0.8], // reasoning is mandatory on GLM-5.3 and GLM-5.3-Flash (Fireworks turns // none away): low is their least. GLM-5.2, Kimi K3 and DeepSeek V4.1 // Flash stop thinking at none, droid's "off" (yetone/magpie#899). - ["glm-5.3", "GLM-5.3", CHAT, "fireworks", 1040000, 131072, ["low", "high", "max"], false], - ["glm-5.3-flash", "GLM-5.3-Flash", CHAT, "fireworks", 1048576, 131072, ["low", "high", "max"], true], - ["glm-5.2", "GLM-5.2", CHAT, "baseten", 1040000, 131072, ["none", "high", "max"], false], - ["kimi-k3", "Kimi K3", CHAT, "fireworks", 262144, 65536, ["none", "low", "high", "max"], true], - ["deepseek-v4.1-flash", "DeepSeek V4.1 Flash", CHAT, "fireworks", 1040000, 131072, ["none", "low", "high", "max"], true], - ["qwen3.8-max", "Qwen3.8 Max", CHAT, "fireworks", 262144, 131072, ["low", "medium", "xhigh"], false], - ["minimax-m3", "MiniMax M3", CHAT, "fireworks", 512000, 64000, ["high"], true], - ["minimax-m2.7", "MiniMax M2.7", ANTHROPIC, "fireworks", 196600, 64000, ["high"], false], - ["mistral-large-4", "Mistral Large 4", CHAT, "mistral", 524288, 64000, ["high"], true], - ["mistral-medium-3.5", "Mistral Medium 3.5", CHAT, "mistral", 256000, 64000, ["high"], true], - ["nemotron-3-ultra", "Nemotron 3 Ultra", CHAT, "baseten", 202000, 65536, ["high"], false], - ["gemini-4-argon", "Gemini 4 Argon", GEMINI, "google", 1000000, 65536, ["low", "medium", "high"], true], - ["gemini-3.1-pro-preview", "Gemini 3.1 Pro", GEMINI, "google", 1000000, 65536, ["low", "medium", "high"], true], - ["gemini-3.8-flash", "Gemini 3.8 Flash", GEMINI, "google", 1000000, 65536, ["low", "medium", "high"], true], - ["gemini-3.7-flash", "Gemini 3.7 Flash", GEMINI, "google", 1000000, 65536, ["low", "medium", "high"], true], - ["gemini-3.6-flash", "Gemini 3.6 Flash", GEMINI, "google", 1000000, 65536, ["low", "medium", "high"], true], - ["gemini-3.5-flash", "Gemini 3.5 Flash", GEMINI, "google", 1000000, 65536, ["minimal", "low", "medium", "high"], true], - ["gemini-3-flash-preview", "Gemini 3 Flash", GEMINI, "google", 1000000, 65536, ["minimal", "low", "medium", "high"], true], + ["glm-5.3", "GLM-5.3", CHAT, "fireworks", 1040000, 131072, ["low", "high", "max"], false, 0.56], + ["glm-5.3-flash", "GLM-5.3-Flash", CHAT, "fireworks", 1048576, 131072, ["low", "high", "max"], true, 0.06], + ["glm-5.2", "GLM-5.2", CHAT, "baseten", 1040000, 131072, ["none", "high", "max"], false, 0.56], + ["kimi-k3", "Kimi K3", CHAT, "fireworks", 262144, 65536, ["none", "low", "high", "max"], true, 1.2], + ["deepseek-v4.1-flash", "DeepSeek V4.1 Flash", CHAT, "fireworks", 1040000, 131072, ["none", "low", "high", "max"], true, 0.12], + ["qwen3.8-max", "Qwen3.8 Max", CHAT, "fireworks", 262144, 131072, ["low", "medium", "xhigh"], false, 0.8], + ["minimax-m3", "MiniMax M3", CHAT, "fireworks", 512000, 64000, ["high"], true, 0.12], + ["minimax-m2.7", "MiniMax M2.7", ANTHROPIC, "fireworks", 196600, 64000, ["high"], false, 0.12], + ["mistral-large-4", "Mistral Large 4", CHAT, "mistral", 524288, 64000, ["high"], true, 0.544], + ["mistral-medium-3.5", "Mistral Medium 3.5", CHAT, "mistral", 256000, 64000, ["high"], true, 0.6], + ["nemotron-3-ultra", "Nemotron 3 Ultra", CHAT, "baseten", 202000, 65536, ["high"], false, 0.24], + ["gemini-4-argon", "Gemini 4 Argon", GEMINI, "google", 1000000, 65536, ["low", "medium", "high"], true, 1.6], + ["gemini-3.1-pro-preview", "Gemini 3.1 Pro", GEMINI, "google", 1000000, 65536, ["low", "medium", "high"], true, 0.8], + ["gemini-3.8-flash", "Gemini 3.8 Flash", GEMINI, "google", 1000000, 65536, ["low", "medium", "high"], true, 0.6], + ["gemini-3.7-flash", "Gemini 3.7 Flash", GEMINI, "google", 1000000, 65536, ["low", "medium", "high"], true, 0.6], + ["gemini-3.6-flash", "Gemini 3.6 Flash", GEMINI, "google", 1000000, 65536, ["low", "medium", "high"], true, 0.6], + ["gemini-3.5-flash", "Gemini 3.5 Flash", GEMINI, "google", 1000000, 65536, ["minimal", "low", "medium", "high"], true, 0.6], + ["gemini-3-flash-preview", "Gemini 3 Flash", GEMINI, "google", 1000000, 65536, ["minimal", "low", "medium", "high"], true, 0.2], // The Fast Mode models come last: magpie shows a provider with no models // picked only the first 24 of its list, and placed beside their base // models in 0.1.27 they pushed GPT-5.3-Codex, Grok 4.7, Grok 4.6, GLM-5.3 // and GLM-5.3-Flash out of it (#119, psylch). A Fast Mode model costs more credits, so it is the one // to pick; asking its base model for fast reaches it all the same // (fastBody). - ["claude-opus-5-5-fast", "Opus 5.5 Fast Mode", ANTHROPIC, "anthropic", 872000, 128000, E5, true], - ["claude-opus-5-fast", "Opus 5 Fast Mode", ANTHROPIC, "anthropic", 867000, 128000, E5, true], - ["claude-opus-4-8-fast", "Opus 4.8 Fast Mode", ANTHROPIC, "anthropic", 867000, 128000, E5, true], - ["gpt-5.5-fast", "GPT-5.5 Fast Mode", RESPONSES, "openai", 1050000, 128000, E4, true], - ["gpt-5.4-fast", "GPT-5.4 Fast Mode", RESPONSES, "openai", 1050000, 128000, E4, true], - ["gpt-5.3-codex-fast", "GPT-5.3-Codex Fast Mode", RESPONSES, "openai", 400000, 128000, E4, true], -].map(([id, name, wire, upstream, context, output, efforts, images]) => ({ id, name, wire, upstream, context, output, efforts, images })) + ["claude-opus-5-5-fast", "Opus 5.5 Fast Mode", ANTHROPIC, "anthropic", 872000, 128000, E5, true, 3.2], + ["claude-opus-5-fast", "Opus 5 Fast Mode", ANTHROPIC, "anthropic", 867000, 128000, E5, true, 4], + ["claude-opus-4-8-fast", "Opus 4.8 Fast Mode", ANTHROPIC, "anthropic", 867000, 128000, E5, true, 4], + ["gpt-5.5-fast", "GPT-5.5 Fast Mode", RESPONSES, "openai", 1050000, 128000, E4, true, 5], + ["gpt-5.4-fast", "GPT-5.4 Fast Mode", RESPONSES, "openai", 1050000, 128000, E4, true, 2], + ["gpt-5.3-codex-fast", "GPT-5.3-Codex Fast Mode", RESPONSES, "openai", 400000, 128000, E4, true, 1.4], +].map(([id, name, wire, upstream, context, output, efforts, images, rate]) => ({ id, name, wire, upstream, context, output, efforts, images, rate })) const byId = new Map(MODELS.map((m) => [m.id, m])) @@ -162,9 +162,35 @@ function variant(npm, effort) { return { reasoningEffort: effort } } -function configModels() { +// Factory's running promotions, as droid's registry keeps them +// (cost.promotions): while one runs, a model's price is its token multiplier +// less the promotion's discount. magpie shows the price now with the price +// before it struck through (rateWas), as it does for WorkBuddy's credits and +// Qoder's price factors, so a cheap model is picked without opening droid. A +// promotion that has ended is no discount at all. +const PROMOTIONS = [ + { id: "gpt-5.6-sol", discount: 0.2, from: "2026-08-22T00:00:00Z", to: "2026-11-22T00:00:00Z" }, + { id: "mistral-large-4", discount: 0.5, from: "2026-10-06T00:00:00Z", to: "2026-10-20T00:00:00Z" }, + { id: "gemini-4-argon", discount: 0.5, from: "2026-10-13T16:00:00Z", to: "2026-12-01T08:00:00Z" }, + { id: "gemini-3.8-flash", discount: 0.5, from: "2026-08-17T21:10:19Z", to: "2027-01-01T08:00:00Z" }, + { id: "gemini-3.7-flash", discount: 0.5, from: "2026-08-17T21:10:19Z", to: "2027-01-01T08:00:00Z" }, +] + +// rateOf is a model's price as magpie keeps it: rate, the token multiplier a +// request costs now, and rateWas, the multiplier before a promotion running +// now (0: none). droid reads it the same way (ib(cost, now)): the first +// promotion that has started and not ended takes its discount off the +// multiplier. +function rateOf(m, now = Date.now()) { + const p = PROMOTIONS.find((p) => p.id === m.id && now >= Date.parse(p.from) && now < Date.parse(p.to)) + if (!p) return { rate: m.rate, rateWas: 0 } + return { rate: Number((m.rate * (1 - p.discount)).toFixed(4)), rateWas: m.rate } +} + +function configModels(now = Date.now()) { const out = {} for (const m of MODELS) { + const { rate, rateWas } = rateOf(m, now) out[m.id] = { id: m.id, name: m.name, @@ -177,12 +203,57 @@ function configModels() { modalities: { input: m.images ? ["text", "image"] : ["text"], output: ["text"] }, cost: { input: 0, output: 0 }, limit: { context: m.context, output: m.output }, + // magpie's own: the token multiplier a request costs now, and the + // multiplier before a promotion running now (0: none) + rate, + rateWas, variants: Object.fromEntries(m.efforts.map((e) => [e, variant(m.wire.npm, e)])), } } return out } +// providerModel is a model as magpie's provider hook keeps it: the same model +// configModels gives OpenCode, in magpie's shape. magpie reads a provider's +// models from this hook, and a config hook's models without the price beside +// them, so the token multiplier droid shows a model by (rateOf) is carried +// here. +function providerModel(provider, m, now = Date.now()) { + const { rate, rateWas } = rateOf(m, now) + return { + id: m.id, + providerID: provider.id, + name: m.name, + api: { id: m.id, url: m.wire.api, npm: m.wire.npm }, + status: "active", + headers: {}, + options: {}, + cost: { input: 0, output: 0, cache: { read: 0, write: 0 } }, + limit: { context: m.context, output: m.output }, + capabilities: { + temperature: true, + reasoning: m.efforts.length > 0, + attachment: m.images, + toolcall: true, + input: { text: true, image: m.images, audio: false, video: false, pdf: false }, + output: { text: true, image: false, audio: false, video: false, pdf: false }, + interleaved: false, + }, + release_date: "", + variants: Object.fromEntries(m.efforts.map((e) => [e, variant(m.wire.npm, e)])), + free: false, + // magpie's own: the token multiplier a request costs now, and the + // multiplier before a promotion running now (0: none) + rate, + rateWas, + } +} + +// providerModels is the list the provider hook answers with, at now. +function providerModels(provider, now = Date.now()) { + return Object.fromEntries(MODELS.map((m) => [m.id, providerModel(provider, m, now)])) +} + // ---- tokens ----------------------------------------------------------------- class FactoryStatus extends Error { @@ -1621,6 +1692,15 @@ export const FactoryAuthPlugin = async ({ client }) => { } return { + // magpie's list for this provider: the same models configModels gives + // OpenCode, each with the token multiplier droid shows it by, which + // magpie shows beside the model as its rate + provider: { + id: PROVIDER, + async models(provider) { + return providerModels(provider) + }, + }, config: async (config) => { config.provider ??= {} const was = config.provider[PROVIDER] ?? {} @@ -1786,4 +1866,4 @@ export const FactoryAuthPlugin = async ({ client }) => { } // for tests -export const _internal = { fastBody, FAST_BETA, limitWindows, windowEnd, dollars, vendorError, CORE, droidBody, DROID_LINE, geminiBody, leaveOutAttachments, LEFT_OUT, TOO_LARGE_HINT } +export const _internal = { fastBody, FAST_BETA, limitWindows, windowEnd, dollars, vendorError, CORE, droidBody, DROID_LINE, geminiBody, leaveOutAttachments, LEFT_OUT, TOO_LARGE_HINT, MODELS, PROMOTIONS, rateOf, configModels, providerModel, providerModels } diff --git a/packages/factory/package.json b/packages/factory/package.json index 84a6cd0..b2439b6 100644 --- a/packages/factory/package.json +++ b/packages/factory/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-factory-auth", - "version": "0.1.31", + "version": "0.1.32", "description": "Sign in to a Factory (Droid) subscription from OpenCode or magpie", "type": "module", "main": "./index.mjs", diff --git a/packages/factory/rate.test.mjs b/packages/factory/rate.test.mjs new file mode 100644 index 0000000..505e869 --- /dev/null +++ b/packages/factory/rate.test.mjs @@ -0,0 +1,119 @@ +// A model's price reaches magpie as its rate: droid's own picker shows a model +// by the token multiplier its entry keeps (cost.tokenMultiplier), and magpie +// shows the same beside the model, so a cheap one is picked without opening +// droid. A promotion running comes off it, with the multiplier before the +// discount as rateWas — magpie strikes that through beside the price, as it +// does for WorkBuddy's credits and Qoder's price factors. The cases are droid +// 0.241.1's own registry entries, saved byte for byte in testdata. +import { expect, test } from "bun:test" +import { readFileSync } from "node:fs" +import { FactoryAuthPlugin, _internal } from "./index.mjs" + +const DROID = readFileSync(new URL("./testdata/droid-0.241.1-costs.txt", import.meta.url), "utf8") + +// cost is a model's cost text, as its entry keeps it. +function cost(id) { + const line = DROID.split("\n").find((l) => l.startsWith(`"${id}":cost:`)) + if (!line) throw new Error(`${id} is not in the fixture`) + return line.slice(id.length + 3) +} + +// multiplier is the token multiplier an entry lists. +function multiplier(id) { + return Number(cost(id).match(/tokenMultiplier:([\d.]+)/)[1]) +} + +// promotions are the ones an entry names: droid keeps them apart from it, by a +// name, or inline in the entry itself. +function promotions(id) { + const text = cost(id) + const inline = text.match(/promotions:\[\{discount:([\d.]+),startsAt:new Date\("([^"]+)"\)(?:,expiresAt:new Date\("([^"]+)"\))?/) + if (inline) return [{ discount: +inline[1], from: inline[2], to: inline[3] ?? null }] + const named = text.match(/promotions:\[([A-Za-z_$][\w$]*(?:,[A-Za-z_$][\w$]*)*)\]/) + if (!named) return [] + const defs = Object.fromEntries( + [...DROID.matchAll(/^([A-Za-z_$][\w$]*)=\{discount:([\d.]+),startsAt:new Date\("([^"]+)"\)(?:,expiresAt:new Date\("([^"]+)"\))?/gm)].map((m) => [ + m[1], + { discount: +m[2], from: m[3], to: m[4] ?? null }, + ]), + ) + return named[1].split(",").map((n) => { + const d = defs[n.trim()] + if (!d) throw new Error(`promotion ${n} is not defined in the fixture`) + return d + }) +} + +// price is what magpie shows for a model at now: the multiplier less the first +// promotion running then, and the multiplier before it when one runs. +function price(id, now) { + const base = multiplier(id) + const p = promotions(id).find((p) => now >= Date.parse(p.from) && (!p.to || now < Date.parse(p.to))) + if (!p) return { rate: base, rateWas: 0 } + return { rate: Number((base * (1 - p.discount)).toFixed(4)), rateWas: base } +} + +test("every model's rate is the token multiplier droid 0.241.1 lists it with", () => { + for (const m of _internal.MODELS) { + expect(m.rate).toBe(multiplier(m.id)) + } +}) + +test("a promotion running comes off the rate, the multiplier before it as rateWas", () => { + const now = Date.parse("2026-10-14T21:30:00Z") + const models = _internal.configModels(now) + for (const m of _internal.MODELS) { + expect(models[m.id].rate).toBe(price(m.id, now).rate) + expect(models[m.id].rateWas).toBe(price(m.id, now).rateWas) + } + // the five droid 0.241.1 offers a price off on: Sol's 20%, Mistral Large 4's + // launch discount, and Gemini 4 Argon's, 3.8 Flash's and 3.7 Flash's 50% + const discounted = _internal.MODELS.filter((m) => models[m.id].rateWas > 0).map((m) => m.id) + expect(discounted).toEqual(["gpt-5.6-sol", "mistral-large-4", "gemini-4-argon", "gemini-3.8-flash", "gemini-3.7-flash"]) + expect(models["gemini-4-argon"].rate).toBe(0.8) + expect(models["gemini-4-argon"].rateWas).toBe(1.6) +}) + +test("a promotion that has ended, or hasn't started, is no discount at all", () => { + // Gemini 4 Argon's 50% off ends 2026-12-01T08:00Z; GLM-5.6 Sol's 20% starts + // 2026-08-22T00:00Z. Either way the multiplier is the price. + const after = _internal.configModels(Date.parse("2026-12-02T00:00:00Z")) + expect(after["gemini-4-argon"].rate).toBe(1.6) + expect(after["gemini-4-argon"].rateWas).toBe(0) + const before = _internal.configModels(Date.parse("2026-08-01T00:00:00Z")) + expect(before["gpt-5.6-sol"].rate).toBe(2) + expect(before["gpt-5.6-sol"].rateWas).toBe(0) +}) + +// magpie reads a model's rate from a provider hook's models, not from a config +// hook's (host.js, providers()): a price given only in config never reaches the +// model list. So the same list is offered both ways. +test("the provider hook carries every model with its rate, as magpie reads it", async () => { + const now = Date.parse("2026-10-14T21:30:00Z") + const models = _internal.providerModels({ id: "factory" }, now) + expect(Object.keys(models).sort()).toEqual(_internal.MODELS.map((m) => m.id).sort()) + for (const m of _internal.MODELS) { + const got = models[m.id] + expect(got.rate).toBe(price(m.id, now).rate) + expect(got.rateWas).toBe(price(m.id, now).rateWas) + // the fields magpie reads a provider's model list for + expect(got.id).toBe(m.id) + expect(got.name).toBe(m.name) + expect(got.api).toEqual({ id: m.id, url: m.wire.api, npm: m.wire.npm }) + expect(got.limit).toEqual({ context: m.context, output: m.output }) + expect(got.capabilities.reasoning).toBe(m.efforts.length > 0) + expect(got.capabilities.input.image).toBe(m.images) + expect(Object.keys(got.variants)).toEqual([...m.efforts]) + } + expect(models["gemini-4-argon"].rate).toBe(0.8) + expect(models["gemini-4-argon"].rateWas).toBe(1.6) +}) + +test("the plugin answers a provider hook, which a config hook alone doesn't", async () => { + const hooks = await FactoryAuthPlugin({ client: { auth: { set: async () => {} } } }) + expect(hooks.provider?.id).toBe("factory") + expect(typeof hooks.provider.models).toBe("function") + const models = await hooks.provider.models({ id: "factory" }, {}) + expect(models["gpt-6.1-sol"].rate).toBeGreaterThan(0) + expect(Object.keys(models)).toHaveLength(_internal.MODELS.length) +}) diff --git a/packages/factory/testdata/droid-0.241.1-costs.txt b/packages/factory/testdata/droid-0.241.1-costs.txt new file mode 100644 index 0000000..add20cb --- /dev/null +++ b/packages/factory/testdata/droid-0.241.1-costs.txt @@ -0,0 +1,55 @@ +// Excerpts, byte for byte, of droid 0.241.1 (@factory/cli-darwin-arm64@0.241.1, +// bin/droid, its bundled model registry): each offered model's cost, and the +// promotions the entries name (cost.promotions), which are defined apart from +// them. magpie shows a model's tokenMultiplier (less a running promotion's +// discount) beside the model as its rate, and the multiplier before the +// discount as rateWas, struck through. +"claude-fable-5.1":cost:{tokenMultiplier:4,cacheReadTokenMultiplier:0.025,outputTokenMultiplier:5} +"claude-fable-5":cost:{tokenMultiplier:4,outputTokenMultiplier:5} +"claude-opus-5-5":cost:{tokenMultiplier:1.6,cacheReadTokenMultiplier:0.05,outputTokenMultiplier:5} +"claude-opus-5-5-fast":cost:{tokenMultiplier:3.2,cacheReadTokenMultiplier:0.05,outputTokenMultiplier:5} +"claude-opus-5":cost:{tokenMultiplier:2} +"claude-opus-5-fast":cost:{tokenMultiplier:4} +"claude-opus-4-8":cost:{tokenMultiplier:2} +"claude-opus-4-8-fast":cost:{tokenMultiplier:4} +"claude-sonnet-5-5":cost:{tokenMultiplier:0.8,outputTokenMultiplier:5,cacheReadTokenMultiplier:0.05} +"claude-sonnet-5":cost:{tokenMultiplier:0.8,outputTokenMultiplier:5} +"claude-sonnet-4-6":cost:{tokenMultiplier:1.2} +"claude-haiku-5-5":cost:{tokenMultiplier:0.04,outputTokenMultiplier:5} +"claude-haiku-4-5-20251001":cost:{tokenMultiplier:0.4} +"gpt-6.1-sol":cost:{tokenMultiplier:0.8,cacheReadTokenMultiplier:0.05,outputTokenMultiplier:5} +"gpt-6-sol":cost:{tokenMultiplier:0.8,outputTokenMultiplier:5} +"gpt-6-astra":cost:{tokenMultiplier:4,outputTokenMultiplier:5,promotions:[M]} +"gpt-6-luna":cost:{tokenMultiplier:0.04,outputTokenMultiplier:5} +"gpt-5.6-sol":cost:{tokenMultiplier:2,outputTokenMultiplier:5,promotions:[M,Pt]} +"gpt-5.6-terra":cost:{tokenMultiplier:0.8,outputTokenMultiplier:6,promotions:[M]} +"gpt-5.6-luna":cost:{tokenMultiplier:0.08,outputTokenMultiplier:6,promotions:[M]} +"gpt-5.5":cost:{tokenMultiplier:2,outputTokenMultiplier:6,promotions:[M]} +"gpt-5.5-fast":cost:{tokenMultiplier:5,outputTokenMultiplier:6,promotions:[M]} +"gpt-5.4":cost:{tokenMultiplier:1,outputTokenMultiplier:6,promotions:[M]} +"gpt-5.4-fast":cost:{tokenMultiplier:2,outputTokenMultiplier:6,promotions:[M]} +"gpt-5.3-codex":cost:{tokenMultiplier:0.7,promotions:[M]} +"gpt-5.3-codex-fast":cost:{tokenMultiplier:1.4,outputTokenMultiplier:8,promotions:[M]} +"grok-4.7":cost:{tokenMultiplier:0.8,cacheReadTokenMultiplier:0.25,outputTokenMultiplier:3} +"grok-4.6":cost:{tokenMultiplier:0.8,cacheReadTokenMultiplier:0.25,outputTokenMultiplier:3,promotions:[M]} +"glm-5.3":cost:{tokenMultiplier:0.56,outputTokenMultiplier:3.15} +"glm-5.3-flash":cost:{tokenMultiplier:0.06,cacheReadTokenMultiplier:0.2,outputTokenMultiplier:3.34} +"glm-5.2":cost:{tokenMultiplier:0.56,outputTokenMultiplier:3.15} +"kimi-k3":cost:{tokenMultiplier:1.2,outputTokenMultiplier:5} +"deepseek-v4.1-flash":cost:{tokenMultiplier:0.12,cacheReadTokenMultiplier:0.024,outputTokenMultiplier:4} +"qwen3.8-max":cost:{tokenMultiplier:0.8,outputTokenMultiplier:3} +"minimax-m3":cost:{tokenMultiplier:0.12,outputTokenMultiplier:4} +"minimax-m2.7":cost:{tokenMultiplier:0.12,outputTokenMultiplier:4} +"mistral-large-4":cost:{tokenMultiplier:0.544,cacheReadTokenMultiplier:0.103,outputTokenMultiplier:3.08,promotions:[{discount:0.5,startsAt:new Date("2026-10-06T00:00:00Z"),expiresAt:new Date("2026-10-20T00:00:00Z"),label:"50% off, launch discount"}]} +"mistral-medium-3.5":cost:{tokenMultiplier:0.6,outputTokenMultiplier:5} +"nemotron-3-ultra":cost:{tokenMultiplier:0.24,outputTokenMultiplier:4} +"gemini-4-argon":cost:{tokenMultiplier:1.6,outputTokenMultiplier:5,cacheReadTokenMultiplier:0.05,promotions:[{discount:0.5,startsAt:new Date("2026-10-13T16:00:00Z"),expiresAt:new Date("2026-12-01T08:00:00Z"),label:"50% off"}]} +"gemini-3.1-pro-preview":cost:{tokenMultiplier:0.8} +"gemini-3.8-flash":cost:{tokenMultiplier:0.6,outputTokenMultiplier:5,promotions:[Mt]} +"gemini-3.7-flash":cost:{tokenMultiplier:0.6,outputTokenMultiplier:5,promotions:[Mt]} +"gemini-3.6-flash":cost:{tokenMultiplier:0.6,outputTokenMultiplier:5} +"gemini-3.5-flash":cost:{tokenMultiplier:0.6} +"gemini-3-flash-preview":cost:{tokenMultiplier:0.2} +M={discount:0.6,startsAt:new Date("2026-08-29T04:00:00Z"),expiresAt:new Date("2026-09-05T02:00:00Z"),label:"60% off"} +Pt={discount:0.2,startsAt:new Date("2026-08-22T00:00:00Z"),expiresAt:new Date("2026-11-22T00:00:00Z"),label:"20% off"} +Mt={discount:0.5,startsAt:new Date("2026-08-17T21:10:19Z"),expiresAt:new Date("2027-01-01T08:00:00Z"),label:"50% off"}