diff --git a/packages/catpaw/fake.mjs b/packages/catpaw/fake.mjs index 951cb73..b6f4870 100644 --- a/packages/catpaw/fake.mjs +++ b/packages/catpaw/fake.mjs @@ -5,8 +5,8 @@ const json = (status, body) => new Response(JSON.stringify(body), { status, head const defaultModelTypes = () => [ { modelTypeId: 77, modelTypeName: "LongCat-2.0", provider: "CATPAW", supportImage: true }, - { modelTypeId: 83, modelTypeName: "Kimi-K3", provider: "CATPAW", supportImage: true }, - { modelTypeId: 120, modelTypeName: "LongCat-2.5", provider: "CATPAW", supportImage: true }, + { modelTypeId: 83, modelTypeName: "Kimi-K3", provider: "CATPAW", supportImage: true, extendedInfo: { rateMultiplier: "1.5" } }, + { modelTypeId: 120, modelTypeName: "LongCat-2.5", provider: "CATPAW", supportImage: true, extendedInfo: { rateMultiplier: "0", marketingCopy: "限时免费" } }, { modelTypeId: -3, modelTypeName: "my-own-model", provider: "USER_CUSTOM", supportImage: false }, ] diff --git a/packages/catpaw/index.mjs b/packages/catpaw/index.mjs index 82025b7..3159e82 100644 --- a/packages/catpaw/index.mjs +++ b/packages/catpaw/index.mjs @@ -70,11 +70,41 @@ function liveModels(rows) { const name = typeof r.modelTypeName === "string" ? r.modelTypeName.trim() : "" const id = byType.get(type) ?? name.toLowerCase().replace(/\s+/g, "-") if (!id || id in out) continue - out[id] = { name: MODELS[id]?.name ?? name, type, image: r.supportImage === true } + const x = extendedOf(r) + const label = (typeof r.description === "string" && r.description.trim()) || (typeof x.shortName === "string" && x.shortName.trim()) || name + // the badges the client draws beside a model's name (extendedInfo. + // marketingCopy, comma-separated: "限时免费"), written the way the table + // writes LongCat-2.0's + const copy = typeof x.marketingCopy === "string" ? x.marketingCopy.split(/[,,]/).map((c) => c.trim()).filter(Boolean) : [] + const rate = rateOf(x.rateMultiplier) + const free = copy.some((c) => c.includes("免费")) || rate === 0 + out[id] = { + name: MODELS[id]?.name ?? (copy.length ? `${label} (${copy.join("、")})` : label), + type, + image: r.supportImage === true, + ...(rate !== undefined ? { rate } : {}), + ...(free ? { free: true } : {}), + } } return out } +// extendedInfo as the client reads it: an object (a JSON string is parsed) +function extendedOf(r) { + let x = r?.extendedInfo + if (typeof x === "string") { + try { x = JSON.parse(x) } catch { x = null } + } + return x && typeof x === "object" ? x : {} +} + +// the multiplier the client prints as "x" (extendedInfo.rateMultiplier); +// undefined when the vendor gives none, so nothing is shown rather than 0 +function rateOf(v) { + const n = typeof v === "number" ? v : typeof v === "string" && /^\s*\d+(?:\.\d+)?\s*[x×]?\s*$/i.test(v) ? Number.parseFloat(v) : NaN + return Number.isFinite(n) && n >= 0 ? n : undefined +} + async function fetchLiveModels(token, signal) { const res = await fetch(ORIGIN + "/api/agent/maas/model-types", { method: "POST", @@ -115,6 +145,9 @@ async function specOf(token, id, signal) { // window; an invented number would lie to the agent sizing its prompts) const configModel = (id, m) => ({ name: m.name, + // magpie's own: the credit multiplier and the free mark, as the client shows them + ...(m.rate !== undefined ? { rate: m.rate } : {}), + ...(m.free ? { free: true } : {}), }) // ── small helpers ────────────────────────────────────────────────────────── @@ -706,8 +739,16 @@ export async function CatPawAuthPlugin({ client } = {}) { } const live = await liveListFor(token) if (!live) return provider?.models ?? {} - const extra = Object.fromEntries(Object.entries(live).filter(([id]) => !(id in (provider?.models ?? {}))).map(([id, m]) => [id, configModel(id, m)])) - return { ...(provider?.models ?? {}), ...extra } + const have = provider?.models ?? {} + const extra = Object.fromEntries(Object.entries(live).filter(([id]) => !(id in have)).map(([id, m]) => [id, configModel(id, m)])) + // a table model gets the list's multiplier and free mark unless the + // user's own config already set one + const known = Object.fromEntries(Object.entries(have).map(([id, m]) => { + const l = live[id] + if (!l) return [id, m] + return [id, { ...m, ...(l.rate !== undefined && m.rate === undefined ? { rate: l.rate } : {}), ...(l.free && m.free === undefined ? { free: true } : {}) }] + })) + return { ...known, ...extra } }, }, async config(config) { diff --git a/packages/catpaw/index.test.mjs b/packages/catpaw/index.test.mjs index f27f63f..1e9276b 100644 --- a/packages/catpaw/index.test.mjs +++ b/packages/catpaw/index.test.mjs @@ -485,13 +485,29 @@ describe("models the vendor added (the desktop client asks the gateway for its l const cfg = {} await plugin.config(cfg) const got = await plugin.provider.models({ models: cfg.provider.catpaw.models }, { auth: saved }) - expect(got["longcat-2.5"]).toEqual({ name: "LongCat-2.5" }) + expect(got["longcat-2.5"]).toEqual({ name: "LongCat-2.5 (限时免费)", rate: 0, free: true }) expect(Object.keys(got)).toContain("longcat-2.0") expect(got["longcat-2.0"].name).toBe("LongCat-2.0 (限时免费)") // the user's own custom model (negative id) is not the gateway's to serve expect(Object.keys(got)).not.toContain("my-own-model") }) + test("a table model takes the list's multiplier; the user's own value stays", async () => { + const got = await plugin.provider.models({ models: { "kimi-k3": { name: "Kimi-K3" } } }, { auth: saved }) + expect(got["kimi-k3"]).toEqual({ name: "Kimi-K3", rate: 1.5 }) + const own = await plugin.provider.models({ models: { "kimi-k3": { name: "Kimi-K3", rate: 9 } } }, { auth: saved }) + expect(own["kimi-k3"].rate).toBe(9) + }) + + test("the multiplier is read as the client reads it, and a missing one is not 0", () => { + const row = (extendedInfo) => ({ modelTypeId: 5, modelTypeName: "X-1", extendedInfo }) + expect(_internal.liveModels([row({ rateMultiplier: "2x" })])["x-1"].rate).toBe(2) + expect(_internal.liveModels([row(JSON.stringify({ rateMultiplier: 0.5 }))])["x-1"].rate).toBe(0.5) + expect("rate" in _internal.liveModels([row({})])["x-1"]).toBe(false) + expect("rate" in _internal.liveModels([row({ rateMultiplier: "abc" })])["x-1"]).toBe(false) + expect(_internal.liveModels([row({ shortName: "X One", marketingCopy: "新,限时免费" })])["x-1"]).toEqual({ name: "X One (新、限时免费)", type: 5, image: false, free: true }) + }) + test("a chat on the new model sends the listed modelType", async () => { const res = await chatWith("longcat-2.5") expect(res.status).toBe(200) diff --git a/packages/catpaw/package.json b/packages/catpaw/package.json index 8f9c1c5..c1f5d39 100644 --- a/packages/catpaw/package.json +++ b/packages/catpaw/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-catpaw-auth", - "version": "0.1.3", + "version": "0.1.4", "description": "OpenCode & Magpie provider plugin for Meituan's 妙手 CatPaw (ai.catpaw.meituan.com): web sign-in or the desktop client's saved credentials, chat through the round/event/turn protocol, models and credit balance", "type": "module", "main": "./opencode.mjs",