From bd5e598d255e17244b4b21643c6aaff1837edf15 Mon Sep 17 00:00:00 2001 From: SivanCola Date: Fri, 13 Nov 2026 00:47:50 -0800 Subject: [PATCH] deepzora 0.2.0: every model Magpie offers the agent is imported (#1081) Deepzora's model list now imports every model Magpie offers the agent (through agentplug's connect catalog), with per-model display name, effort and context settings, and keeps the user's default when an upstream is removed. Run at df024f9d on WSL (Ubuntu 24.04), HOME=$(mktemp -d): - node --test deepzora.test.mjs: 13 pass - bun scripts/check.mjs deepzora: ok - acceptance.mjs and resident-acceptance.mjs against a magpie CLI built from main 51b98b1a8 (-tags nogui): every PASS line - Broken by hand: the tests went red. - The per-model toml keys were checked against Deepzora's ProviderModelOverride source. Not tried: a real Deepzora desktop install. Co-authored-by: SivanCola --- packages/deepzora/README.md | 7 +++-- packages/deepzora/acceptance.mjs | 21 ++++++++----- packages/deepzora/bridge.mjs | 38 ++++++++++++++++++++--- packages/deepzora/deepzora.agent.js | 7 ++++- packages/deepzora/deepzora.test.mjs | 28 ++++++++++++++++- packages/deepzora/package.json | 2 +- packages/deepzora/resident-acceptance.mjs | 12 +++++-- 7 files changed, 97 insertions(+), 18 deletions(-) diff --git a/packages/deepzora/README.md b/packages/deepzora/README.md index 3c21778..663e780 100644 --- a/packages/deepzora/README.md +++ b/packages/deepzora/README.md @@ -26,8 +26,11 @@ The descriptor writes `~/.deepzora/magpie-agent.json`. The host bridges it into provider and default model. The assigned gateway key goes into `~/.deepzora/.env` as `MAGPIE_DEEPZORA_API_KEY`, never into TOML or logs. Deepzora sends requests to Magpie; Magpie handles upstream credentials and -protocol conversion. Known context/output sizes and image support follow the -chosen model. Existing providers and unrelated settings are kept. +protocol conversion. The selected model is the default. All models Magpie +offers this agent are imported, with their own names, context/output sizes +and image support. Magpie catalog changes update this connection; manual +providers and existing session selections are kept. After upgrading from a +single-model version, reselect the current model once to refresh the catalog. Choose the Agent's default to disconnect, wait for the bridge, then restart Deepzora. Normal plugin-host exit flushes a pending disconnect, including diff --git a/packages/deepzora/acceptance.mjs b/packages/deepzora/acceptance.mjs index 6bf6d6d..18d25a0 100644 --- a/packages/deepzora/acceptance.mjs +++ b/packages/deepzora/acceptance.mjs @@ -20,7 +20,7 @@ const dz = env.DEEPZORA_HOME const config = path.join(dz, "config.toml") const control = path.join(dz, "magpie-agent.json") const run = async (bin, args) => exec(bin, args, { cwd: root, env, timeout: 120000, maxBuffer: 2 << 20 }) -let requests = 0 +const requestedModels = new Set() let expectedKey = "" const server = http.createServer(async (req, res) => { let raw = "" @@ -32,9 +32,9 @@ const server = http.createServer(async (req, res) => { } const body = JSON.parse(raw) assert.equal(req.url, "/v1/chat/completions") - assert.equal(body.model, "fixture/test-model") + assert.ok(["fixture/test-model", "ollama-cloud/second"].includes(body.model)) assert.equal(req.headers.authorization, `Bearer ${expectedKey}`) - requests++ + requestedModels.add(body.model) if (body.stream) { res.setHeader("content-type", "text/event-stream") res.end(`data: ${JSON.stringify({ id: "fixture", object: "chat.completion.chunk", choices: [{ index: 0, delta: { role: "assistant", content: "bridge-ok" }, finish_reason: null }] })}\n\ndata: ${JSON.stringify({ id: "fixture", object: "chat.completion.chunk", choices: [{ index: 0, delta: {}, finish_reason: "stop" }] })}\n\ndata: [DONE]\n\n`) @@ -52,7 +52,10 @@ try { const original = '# native acceptance fixture\ndefault_model="mine/model"\n[[providers]]\nname="mine"\nkind="openai"\nbase_url="http://127.0.0.1:49998/v1"\nmodels=["model"]\n' fs.writeFileSync(config, original) fs.writeFileSync(path.join(dz, ".env"), "OTHER_KEY=fixture-original\n") - fs.writeFileSync(path.join(env.XDG_CONFIG_HOME, "magpie/providers.json"), JSON.stringify({ providers: [{ id: "fixture", name: "Fixture", key: "fixture-only", chat: base, models: ["test-model"] }] })) + fs.writeFileSync(path.join(env.XDG_CONFIG_HOME, "magpie/providers.json"), JSON.stringify({ providers: [ + { id: "fixture", name: "Fixture", key: "fixture-only", chat: base, models: ["test-model"] }, + { id: "ollama-cloud", name: "Ollama Cloud", key: "fixture-only", chat: base, models: ["second"] }, + ] })) await run(magpie, ["plugin", "add", repo]) const agents = await run(magpie, ["agents"]) assert.match(agents.stdout, /Deepzora/) @@ -61,7 +64,8 @@ try { const loaded = await run(magpie, ["plugin", "list", "--json"]) assert.ok(!JSON.parse(loaded.stdout).loaded.some(plugin => plugin.error)) assert.equal(parse(fs.readFileSync(config, "utf8")).default_model, "magpie-agent/fixture/test-model") - console.log("PASS: Magpie installs and discovers the agent; its plugin host bridges the selected model") + assert.ok(parse(fs.readFileSync(config, "utf8")).providers.find(p => p.name === "magpie-agent").models.includes("ollama-cloud/second")) + console.log("PASS: Magpie's plugin host imports the unselected Ollama route and keeps the selected default") if (deepzora) { expectedKey = parseEnv(fs.readFileSync(path.join(dz, ".env"))).MAGPIE_DEEPZORA_API_KEY assert.ok(expectedKey) @@ -72,9 +76,12 @@ try { fs.writeFileSync(control, JSON.stringify(cfg)) await run(process.execPath, [path.join(repo, "cli.mjs"), "sync"]) const result = await run(deepzora, ["-p", "Reply with bridge-ok", "--output-format", "json"]) - assert.ok(requests > 0) + assert.ok(requestedModels.has("fixture/test-model")) assert.match(result.stdout, /bridge-ok/) - console.log("PASS: unchanged Deepzora sends its selected model and assigned credential to the local API") + const other = await run(deepzora, ["--model", "magpie-agent/ollama-cloud/second", "-p", "Reply with bridge-ok", "--output-format", "json"]) + assert.match(other.stdout, /bridge-ok/) + assert.ok(requestedModels.has("ollama-cloud/second")) + console.log("PASS: unchanged Deepzora routes both the default and an imported Ollama model to the local API") } await run(magpie, ["deepzora", "default"]) await run(process.execPath, [path.join(repo, "cli.mjs"), "sync"]) diff --git a/packages/deepzora/bridge.mjs b/packages/deepzora/bridge.mjs index 732ac70..c31e60e 100644 --- a/packages/deepzora/bridge.mjs +++ b/packages/deepzora/bridge.mjs @@ -108,9 +108,24 @@ function desired(control) { models: [control.model], default: control.model, api_key_env: KEY, } const info = control.modelInfo || {} - if (Number.isSafeInteger(info.context) && info.context > 0) entry.context_window = info.context - if (Number.isSafeInteger(info.output) && info.output > 0) entry.max_output_tokens = info.output - if (typeof info.images === "boolean") entry.model_overrides = { [control.model]: { vision: info.images } } + // Old control files have only modelInfo. Keep the explicit default even + // when it is a manually entered route absent from the discovery catalog. + const catalog = new Map() + for (const m of control.catalog || []) { + if (typeof m?.id === "string" && m.id.trim()) catalog.set(m.id, m) + } + catalog.set(control.model, { ...info, ...catalog.get(control.model) }) + entry.models = [...catalog.keys()] + const overrides = [] + for (const [id, m] of catalog) { + const item = {} + if (typeof m.name === "string" && m.name.trim() && m.name !== id) item.display_name = m.name.trim() + if (Number.isSafeInteger(m.context) && m.context > 0) item.context_window = m.context + if (Number.isSafeInteger(m.output) && m.output > 0) item.max_output_tokens = m.output + if (typeof m.images === "boolean") item.vision = m.images + if (Object.keys(item).length) overrides.push([id, item]) + } + if (overrides.length) entry.model_overrides = Object.fromEntries(overrides) return { entry, key: control.gatewayKey, ref: `${PROVIDER}/${control.model}` } } @@ -201,8 +216,23 @@ export function syncBridge(p = pathsFor()) { export function startBridge(p = pathsFor(), { interval = 750, onError = () => console.error("Deepzora bridge paused; run magpie-deepzora sync for details (configuration retained).") } = {}) { if (active.has(p.state)) return active.get(p.state) let previousError = "" + let previousFiles + const watched = [p.control, p.config, p.env, p.state, p.state + ".pending"] const tick = () => { - try { syncBridge(p); previousError = "" } catch (error) { + try { + // A catalog can contain hundreds of overrides. Avoid parsing the whole + // TOML and backup every 750 ms when neither side has changed. Use the + // pre-sync stamp so a concurrent edit is always seen on the next tick. + const files = watched.map(file => { + const st = fs.lstatSync(file, { bigint: true, throwIfNoEntry: false }) + return st ? `${st.ino}:${st.size}:${st.mtimeNs}:${st.ctimeNs}` : "-" + }).join("|") + if (files === previousFiles) return + syncBridge(p) + previousFiles = files + previousError = "" + } catch (error) { + previousFiles = undefined // Parser errors can contain a source line; never write those to logs. if (error.message !== previousError) onError() previousError = error.message diff --git a/packages/deepzora/deepzora.agent.js b/packages/deepzora/deepzora.agent.js index 60c0857..f8154a0 100644 --- a/packages/deepzora/deepzora.agent.js +++ b/packages/deepzora/deepzora.agent.js @@ -14,11 +14,16 @@ export const agent = { notice: "Wait a moment for the bridge, restart Deepzora, then start a new conversation. 等待桥接完成,重启 Deepzora 并新建会话。", } -export function connect({ gateway, model }) { +export function connect({ gateway, model, models }) { if (!model?.id || !gateway?.v1 || !gateway?.key) throw new Error("Choose a model with a configured Magpie gateway first.") return { baseURL: gateway.v1, gatewayKey: gateway.key, + // Magpie calls connect again when the agent-visible catalog changes. + // Keep IDs as values: dotted or nested model IDs are not edit paths. + catalog: (models || []).map(({ id, name, context, output, images }) => ({ + id, name, context, output, images, + })), modelInfo: { context: model.context > 0 ? model.context : 0, output: model.output > 0 ? model.output : 0, diff --git a/packages/deepzora/deepzora.test.mjs b/packages/deepzora/deepzora.test.mjs index c988e9d..4b49dba 100644 --- a/packages/deepzora/deepzora.test.mjs +++ b/packages/deepzora/deepzora.test.mjs @@ -37,7 +37,33 @@ function fixture(t) { return { p, select, disconnect: () => fs.writeFileSync(p.control, "{}") } } -test("agent descriptor and sandbox hook expose one selected model", () => { +test("catalog refresh replaces routes and per-model limits without changing the default or manual connections", t => { + const { p, disconnect } = fixture(t) + const chosen = { id: "codex/gpt", name: "GPT", context: 512000, images: true } + const ollama = { id: "ollama-cloud/vendor/glm:5.3", name: "GLM · Ollama Cloud", context: 64000, output: 4096, images: false } + const apply = models => { + fs.writeFileSync(p.control, JSON.stringify({ model: chosen.id, ...connect({ gateway: { v1: "http://localhost/v1", key: "fixture" }, model: chosen, models }) })) + syncBridge(p) + const cfg = parse(fs.readFileSync(p.config, "utf8")) + assert.equal(cfg.default_model, PROVIDER + "/" + chosen.id) + assert.deepEqual(cfg.providers[0], parse(originalConfig).providers[0]) + return cfg.providers[1] + } + const full = apply([chosen, ollama, ollama]) + assert.deepEqual(full.models, [chosen.id, ollama.id]) + assert.deepEqual({ ...full.model_overrides[ollama.id] }, { display_name: ollama.name, context_window: 64000, max_output_tokens: 4096, vision: false }) + assert.equal(full.context_window, undefined) + assert.equal(full.max_output_tokens, undefined) + // A manually selected default can be absent from the returned catalog. + const updated = apply([{ id: "local/new" }]) + assert.deepEqual(updated.models, ["local/new", chosen.id]) + assert.equal(updated.model_overrides[ollama.id], undefined) + disconnect() + syncBridge(p) + assert.equal(fs.readFileSync(p.config, "utf8"), originalConfig) +}) + +test("agent descriptor and sandbox hook expose the selected default", () => { assert.equal(agent.id, "deepzora") assert.equal(agent.format, "json") assert.equal(agent.model, "model") diff --git a/packages/deepzora/package.json b/packages/deepzora/package.json index 1644599..eb7d7da 100644 --- a/packages/deepzora/package.json +++ b/packages/deepzora/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/agent-deepzora", - "version": "0.1.0", + "version": "0.2.0", "description": "Deepzora Desktop, CLI and ACP as a Magpie agent, with a reversible TOML and dotenv configuration bridge", "type": "module", "main": "./index.mjs", diff --git a/packages/deepzora/resident-acceptance.mjs b/packages/deepzora/resident-acceptance.mjs index 3119663..a1d63cc 100644 --- a/packages/deepzora/resident-acceptance.mjs +++ b/packages/deepzora/resident-acceptance.mjs @@ -40,7 +40,10 @@ try { fs.mkdirSync(path.join(env.XDG_CONFIG_HOME, "magpie"), { recursive: true }) fs.writeFileSync(config, original) fs.writeFileSync(credentials, "OTHER_KEY=fixture-original\n") - fs.writeFileSync(path.join(env.XDG_CONFIG_HOME, "magpie/providers.json"), JSON.stringify({ providers: [{ id: "fixture", name: "Fixture", key: "fixture-only", chat: "http://127.0.0.1:9/v1", models: ["model"] }] })) + fs.writeFileSync(path.join(env.XDG_CONFIG_HOME, "magpie/providers.json"), JSON.stringify({ providers: [ + { id: "fixture", name: "Fixture", key: "fixture-only", chat: "http://127.0.0.1:9/v1", models: ["model"] }, + { id: "ollama-cloud", name: "Ollama Cloud", key: "fixture-only", chat: "http://127.0.0.1:9/v1", models: ["second"] }, + ] })) await run(["plugin", "add", repo]) await run(["deepzora", "fixture/model"]) child = spawn(magpie, ["serve"], { cwd: root, env, stdio: "ignore" }) @@ -49,7 +52,12 @@ try { try { return (await fetch(`http://${addr}/api/hello`, { signal: AbortSignal.timeout(500) })).ok } catch { return false } }) await fetch(`http://${addr}/v1/models`, { signal: AbortSignal.timeout(10000) }) - await until(() => parseTOML(fs.readFileSync(config, "utf8")).default_model === "magpie-agent/fixture/model") + const managed = () => parseTOML(fs.readFileSync(config, "utf8")).providers?.find(p => p.name === "magpie-agent") + await until(() => managed()?.models.includes("ollama-cloud/second")) + await run(["provider", "rm", "ollama-cloud"]) + await until(() => managed() && !managed().models.includes("ollama-cloud/second")) + assert.equal(parseTOML(fs.readFileSync(config, "utf8")).default_model, "magpie-agent/fixture/model") + console.log("PASS: removing an upstream updates Deepzora without reselecting the default") await run(["plugin", "off", repo]) // Refreshing the catalog observes plugins.json and closes the old host. await fetch(`http://${addr}/v1/models`, { signal: AbortSignal.timeout(10000) })