diff --git a/packages/loomy/fake.mjs b/packages/loomy/fake.mjs index e0769b1..a230386 100644 --- a/packages/loomy/fake.mjs +++ b/packages/loomy/fake.mjs @@ -33,6 +33,7 @@ export function fakeLoomy() { dailyBalance: 3000, // the day's first first-login refreshes it to 5000 recordsReads: 0, modelsReads: 0, + modelsCode: null, // set to a business code to answer the models read 200 with it // onboarding: done[key]=true once its complete call lands (idempotent) onboardingDone: {}, onboardingQueries: 0, @@ -48,6 +49,7 @@ export function fakeLoomy() { state.dailyBalance = 3000 state.recordsReads = 0 state.modelsReads = 0 + state.modelsCode = null state.onboardingDone = {} state.onboardingQueries = 0 state.onboardingCompletes = 0 @@ -108,6 +110,7 @@ export function fakeLoomy() { if (url.pathname === "/api/v1/models") { state.modelsReads++ + if (state.modelsCode) return json(200, { code: state.modelsCode, desc: "系统繁忙" }) return json(200, { data: [ { id: "spark-x", name: "Spark X2.5 (x0.1)", capabilities: { reasoning: true, function_calling: true, vision: false }, context_window: 128000, max_tokens: 8192 }, diff --git a/packages/loomy/index.mjs b/packages/loomy/index.mjs index 96bf411..b424030 100644 --- a/packages/loomy/index.mjs +++ b/packages/loomy/index.mjs @@ -669,7 +669,10 @@ export async function LoomyAuthPlugin({ client } = {}) { if (auth?.type !== "oauth" || !auth.access) return p.models try { const payload = await tokenRequest("GET", "/api/v1/models", auth.access) - if (codeOf(payload) && codeOf(payload) !== OK) return p.models + // a refusal in a 200 lists nothing: what was given (the static + // table at first) isn't the account's list, so magpie keeps the + // one it had and the models picked from it + if (codeOf(payload) && codeOf(payload) !== OK) return fellBack(p.models) const entries = Array.isArray(payload?.data) ? payload.data : [] for (const entry of entries) { const m = catalogModel(entry) @@ -678,13 +681,19 @@ export async function LoomyAuthPlugin({ client } = {}) { } return p.models } catch { - return p.models + return fellBack(p.models) } }, }, } } +// fellBack marks a list that isn't the account's own (magpie's plugin +// host reads Symbol.for("magpie.fellBack")) +function fellBack(models) { + return Object.defineProperty(models, Symbol.for("magpie.fellBack"), { value: true, configurable: true }) +} + // for tests export const _internal = { retryableRefusal, diff --git a/packages/loomy/index.test.mjs b/packages/loomy/index.test.mjs index 7701edb..d6fecc1 100644 --- a/packages/loomy/index.test.mjs +++ b/packages/loomy/index.test.mjs @@ -298,6 +298,17 @@ describe("models", () => { expect(list["glm-5.3-flash"].modalities).toEqual({ input: ["text", "image"], output: ["text"] }) expect(list["plain"].rate).toBeUndefined() expect(list["plain"].name).toBe("No Suffix At All") + expect(list[Symbol.for("magpie.fellBack")]).toBeUndefined() + }) + + // Archivemylife on X (agy-cli's sibling): a list that isn't the account's + // own, handed back as if it were, dropped the models the user picked + test("a refusal in a 200 gives the static table marked as not the account's", async () => { + fake.state.modelsCode = "100500" + const p = { models: { ..._internal.STATIC_MODELS } } + const list = await plugin.provider.models(p, { auth: saved }) + expect(Object.keys(list)).toEqual(Object.keys(_internal.STATIC_MODELS)) + expect(list[Symbol.for("magpie.fellBack")]).toBe(true) }) // plugins#1141: the host reads a listed model by OpenCode's shape diff --git a/packages/loomy/package.json b/packages/loomy/package.json index 2271846..0260253 100644 --- a/packages/loomy/package.json +++ b/packages/loomy/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-loomy-auth", - "version": "0.1.9", + "version": "0.1.10", "description": "OpenCode & Magpie provider plugin for Loomy (讯飞, loomyad.xunfei.cn): SMS sign-in, chat, models, the two point wallets and the daily first-login grant", "type": "module", "main": "./opencode.mjs",