From 7b70b40c7b4ae9454f28c8425f7aeabed1a2ba00 Mon Sep 17 00:00:00 2001 From: yetone Date: Thu, 1 Oct 2026 17:06:07 +0800 Subject: [PATCH] zcode 0.1.6: ZCode's config out of reach, the plugin's table stands in marked Symbol.for("magpie.fellBack"), and magpie keeps the list it was told last, as the built-in keeps the one it fetched last. models.test.mjs (passes on Linux with no ZCode installed; with ZCode.app on macOS its own config is read, unmarked). --- packages/zcode/index.mjs | 9 +++++++-- packages/zcode/models.test.mjs | 32 ++++++++++++++++++++++++++++++++ packages/zcode/package.json | 2 +- 3 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 packages/zcode/models.test.mjs diff --git a/packages/zcode/index.mjs b/packages/zcode/index.mjs index f024bfa..e1e9a29 100644 --- a/packages/zcode/index.mjs +++ b/packages/zcode/index.mjs @@ -1048,9 +1048,14 @@ export async function ZCodeAuthPlugin({ client }) { const start = await onStart(s).catch(() => false) const base = start ? START_BASE : s.base let ms = modelsOf(await zcodeConfig().catch(() => null), planID(base)) - if (!ms.length) ms = start ? START_MODELS : MODELS + // ZCode's config can't be had: its table stands in, and magpie keeps + // the list it was told last, as the built-in keeps the one it fetched last + const fell = !ms.length + if (fell) ms = start ? START_MODELS : MODELS const url = s.base + "/v1" - return Object.fromEntries(ms.map((m) => [m.id, model(m, url)])) + const out = Object.fromEntries(ms.map((m) => [m.id, model(m, url)])) + if (fell) out[Symbol.for("magpie.fellBack")] = true + return out }, }, auth: { diff --git a/packages/zcode/models.test.mjs b/packages/zcode/models.test.mjs new file mode 100644 index 0000000..7a0462a --- /dev/null +++ b/packages/zcode/models.test.mjs @@ -0,0 +1,32 @@ +// Run with HOME=$(mktemp -d) bun test: a plan's models when ZCode's config +// can't be had stand in from the plugin's table, said to be a fallback, +// so magpie keeps the list it was told last, as the built-in keeps the one +// it fetched last. +import { test, expect, beforeAll, afterAll } from "bun:test" +import { homedir, tmpdir } from "node:os" +import { existsSync, realpathSync } from "node:fs" + +let ZCodeAuthPlugin +const fetched = globalThis.fetch +beforeAll(async () => { + if (![tmpdir(), realpathSync(tmpdir())].some((t) => homedir().startsWith(t))) throw new Error("run with HOME=$(mktemp -d) bun test") + ;({ ZCodeAuthPlugin } = await import("./index.mjs")) +}) +afterAll(() => { + globalThis.fetch = fetched +}) + +// an installed ZCode (macOS) has a config of its own, which is no fallback +const app = process.platform === "darwin" && existsSync("/Applications/ZCode.app/Contents/Resources/config/provider/zcode-builtin.json") + +test("ZCode's config out of reach, the table stands in, said to be a fallback", async () => { + globalThis.fetch = async () => { + throw new Error("connection refused") + } + const hooks = await ZCodeAuthPlugin({ client: {} }) + const ms = await hooks.provider.models({ models: {} }, { auth: { type: "api", key: "k" } }) + expect(Object.keys(ms).length).toBeGreaterThan(0) + expect(ms[Symbol.for("magpie.fellBack")]).toBe(app ? undefined : true) + // OpenCode, reading the list, sees only the models + expect(Object.keys(ms).every((k) => ms[k].id === k)).toBe(true) +}) diff --git a/packages/zcode/package.json b/packages/zcode/package.json index 21fbb0a..e47e586 100644 --- a/packages/zcode/package.json +++ b/packages/zcode/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-zcode-auth", - "version": "0.1.5", + "version": "0.1.6", "description": "OpenCode provider plugin: signs in to Z.ai's or BigModel's (智谱) GLM Coding Plan as ZCode does, including team seats and ZCode's Start Plan", "type": "module", "main": "./index.mjs",