diff --git a/packages/agy-cli/README.en.md b/packages/agy-cli/README.en.md index 7c9793c..457bda1 100644 --- a/packages/agy-cli/README.en.md +++ b/packages/agy-cli/README.en.md @@ -75,6 +75,10 @@ MCP server. The control port listens on loopback only, with a token per process. state file, so after a restart that list is served while agy is asked again. Models and thinking levels can be switched mid-conversation without losing history. + `agy models` runs with `--settings '{"modelProvider":""}'`, so a + `"modelProvider": "gemini"` (AI Studio key) in settings.json doesn't change + this list: it lists the Google account's models. An AI Studio key is added on + magpie's Providers page as Google Gemini. - **Streaming with thought summaries**: summaries come back as `thinking` blocks. Set `AGY_BRIDGE_THINKING=0` to turn them off. - **Image input**: inline base64 PNG, JPEG, GIF and WebP. This needs agy 1.5.0 or diff --git a/packages/agy-cli/README.md b/packages/agy-cli/README.md index dbbdc59..cb877f8 100644 --- a/packages/agy-cli/README.md +++ b/packages/agy-cli/README.md @@ -53,7 +53,9 @@ agy 连接 `--ide-mcp-port` 时会运行 `nc 127.0.0.1 <端口>`。插件在 agy 就不会出现在列表里。列模型时插件会先用 `agy acp` 开一个会话(不发 prompt)问 agy 开放了哪些, 问到之前只列 Gemini。上次 agy 列出并开放的模型存在会话状态文件旁边的 `models.json`, 重启后先列它,同时再问一次 agy。模型和思考等级可以在会话中切换, - 历史保留。 + 历史保留。`agy models` 带 `--settings '{"modelProvider":""}'` 运行,所以 + settings.json 里的 `"modelProvider": "gemini"`(AI Studio 密钥)不影响这里的列表, + 列出的是 Google 账号的模型;AI Studio 密钥请在 Magpie 的 Providers 页加成 Google Gemini。 - **流式输出与思考摘要**:思考摘要以 `thinking` 块返回,设置 `AGY_BRIDGE_THINKING=0` 可关闭。 - **图片输入**:支持内联 base64 的 PNG、JPEG、GIF、WebP。需要 agy 1.5.0 或更新版本, 因为图片能力要由 ACP 服务端声明。工具返回的图片(如 `read` 读到的截图)也作为提示里的 diff --git a/packages/agy-cli/index.mjs b/packages/agy-cli/index.mjs index 8b33a0a..b81ce54 100644 --- a/packages/agy-cli/index.mjs +++ b/packages/agy-cli/index.mjs @@ -258,6 +258,38 @@ function acpHelp(agyPath) { return probe } +// `agy models` reads the user's own settings.json, while a turn runs agy acp +// on the bridge's folder and the Google sign-in. With "modelProvider": +// "gemini" there (an AI Studio key), `agy models` lists API-key mode's +// built-in models, or fails when magpie's environment has no +// GEMINI_API_KEY, and the catalog kept a list acp doesn't serve +// (plugins#1218). agy 1.5.9 takes --settings, a session-only settings.json +// object, before the subcommand; an agy whose help doesn't list it gets the +// bare command, as before. Asked once per binary, like acpHelp. +const topHelpText = new Map() +function topHelp(agyPath) { + let key = agyPath + try { + const st = fs.statSync(agyPath) + key += `\0${st.mtimeMs}\0${st.size}` + } catch {} + let probe = topHelpText.get(key) + if (!probe) { + probe = execOnce(agyPath, ["--help"], { timeoutMs: 10_000, unrefTimer: true }).then(({ err, stdout, stderr, timedOut }) => { + if (err || timedOut) topHelpText.delete(key) + return stdout + stderr + }) + topHelpText.set(key, probe) + } + return probe +} + +export const MODELS_SETTINGS = JSON.stringify({ modelProvider: "" }) + +export async function modelsArgs(agyPath) { + return /^\s*--settings\b/m.test(await topHelp(agyPath)) ? ["--settings", MODELS_SETTINGS, "models"] : ["models"] +} + export function noDefaultCustomizationsArgs(agyPath) { return acpHelp(agyPath).then((help) => (/--include-default-customizations\b/.test(help) ? ["--include-default-customizations=false"] : [])) } @@ -352,7 +384,7 @@ async function listAgyModels(agyPath) { // a listing nobody asked for doesn't run an agy that would start its own // Google sign-in (signin.mjs); the catalog it has stays if (await backgroundBlocked()) return null - const { err, stdout, stderr, code, timedOut } = await execOnce(agyPath, ["models"], { timeoutMs: 15_000, unrefTimer: true }) + const { err, stdout, stderr, code, timedOut } = await execOnce(agyPath, await modelsArgs(agyPath), { timeoutMs: 15_000, unrefTimer: true }) // null, not FALLBACK_MODELS: a failed listing must not overwrite a dynamic catalog. if (err || timedOut || code !== 0) { if (!err && !timedOut) noteRefusal(agyRefusal(`${stderr}\n${stdout}`)) @@ -383,7 +415,7 @@ function refusalOf() { async function refusalNow(agyPath) { if (refusals.has(currentHome() ?? "")) return refusalOf() if (!agyPath || await backgroundBlocked()) return "" - const { err, stdout, stderr, code, timedOut } = await execOnce(agyPath, ["models"], { timeoutMs: 15_000 }) + const { err, stdout, stderr, code, timedOut } = await execOnce(agyPath, await modelsArgs(agyPath), { timeoutMs: 15_000 }) if (err || timedOut || code === 0) return "" const why = agyRefusal(`${stderr}\n${stdout}`) noteRefusal(why) @@ -419,7 +451,7 @@ async function probeSignedIn(agy) { // caller goes on to magpie's own sign-in link instead const blocked = await backgroundBlocked({ fresh: true }) if (blocked) return { ok: false, why: blocked } - const probe = await execOnce(agy, ["models"], { timeoutMs: 15_000 }) + const probe = await execOnce(agy, await modelsArgs(agy), { timeoutMs: 15_000 }) const listed = !probe.err && !probe.timedOut && probe.code === 0 && probe.stdout.split("\n").some(line => /^\S+\s+\S/.test(line.trim())) if (listed) { diff --git a/packages/agy-cli/package.json b/packages/agy-cli/package.json index b3ae16c..cb4351b 100644 --- a/packages/agy-cli/package.json +++ b/packages/agy-cli/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-agy-cli-auth", - "version": "0.1.78", + "version": "0.1.79", "description": "Magpie/OpenCode model provider that connects your local agy CLI through the Agent Client Protocol (agy acp), so Antigravity subscription models run through the real CLI", "type": "module", "main": "./opencode.mjs", diff --git a/packages/agy-cli/test/catalog.mjs b/packages/agy-cli/test/catalog.mjs index 45b042e..90ac2c8 100644 --- a/packages/agy-cli/test/catalog.mjs +++ b/packages/agy-cli/test/catalog.mjs @@ -686,3 +686,48 @@ test("#3501 请求:会话一个型号都不开放时答 403 和 Google 的原 await settle(kit) }) }) + +// plugins#1218 (shenggxhz on X): settings.json's "modelProvider": "gemini" +// (an AI Studio key) made a bare `agy models` fail without GEMINI_API_KEY in +// magpie's environment, and the old catalog stayed. agy 1.5.9's own help +// lines; the fake answers like that agy: bare, the API-key error; with +// --settings '{"modelProvider":""}', the Google sign-in's list. +const AGY_159_TOP_HELP = [ + "Usage of agy:", + " --add-dir Add a directory to the workspace (repeatable) (default [])", + " --sandbox Run in a sandbox with terminal restrictions enabled", + " --settings Settings for this session only, never saved: a JSON object of settings.json keys, inline or as a file path", + "", + "Available subcommands:", + " models List available models", + "", +].join("\n") +const GEMINI_PROVIDER_ERROR = 'modelProvider is set to "gemini" in settings.json, but the GEMINI_API_KEY environment variable is not set' +function geminiSettingsAgy() { + return ({ args }) => { + const i = args.indexOf("--settings") + const provider = i >= 0 ? JSON.parse(args[i + 1]).modelProvider : "gemini" + return provider === "gemini" ? { stderr: GEMINI_PROVIDER_ERROR + "\n", code: 1 } : DYNAMIC_OK + } +} + +test("#1218 agy models runs with modelProvider cleared when agy takes --settings, so a gemini settings.json doesn't keep a stale list", async () => { + await withKit({ topHelp: AGY_159_TOP_HELP, modelsBehavior: geminiSettingsAgy() }, async (kit) => { + await kit.models() + await warmed(kit) + const listed = await kit.models() + assert.ok(listed["gemini-9-test"], `agy's own list should be read, got ${ids(listed).join(", ")}`) + assert.ok(kit.modelsSpawns.length >= 1) + for (const spawn of kit.modelsSpawns) assert.deepEqual(spawn.args, ["--settings", '{"modelProvider":""}', "models"]) + assert.equal(kit.topHelpSpawns.length, 1, "agy --help is asked once per binary") + }) +}) + +test("#1218 an agy whose help doesn't list --settings still runs a bare agy models", async () => { + await withKit({ modelsBehavior: DYNAMIC_OK }, async (kit) => { + await kit.models() + await warmed(kit) + assert.ok((await kit.models())["gemini-9-test"]) + for (const spawn of kit.modelsSpawns) assert.deepEqual(spawn.args, ["models"]) + }) +}) diff --git a/packages/agy-cli/test/testkit.mjs b/packages/agy-cli/test/testkit.mjs index 62a0524..25442c0 100644 --- a/packages/agy-cli/test/testkit.mjs +++ b/packages/agy-cli/test/testkit.mjs @@ -179,9 +179,9 @@ class FakeChild extends EventEmitter { } function spawnModels(kit, file, args) { - kit.modelsSpawns.push({ file, at: kit.clock.now() }) + kit.modelsSpawns.push({ file, args, at: kit.clock.now() }) const raw = typeof kit.modelsBehavior === "function" - ? kit.modelsBehavior({ file, count: kit.modelsSpawns.length }) + ? kit.modelsBehavior({ file, args, count: kit.modelsSpawns.length }) : kit.modelsBehavior const behavior = raw || { hang: true } if (behavior.throwSync) throw Object.assign(new Error("spawn EACCES (test)"), { code: "EACCES" }) @@ -436,6 +436,7 @@ export async function createBridgeKit(options = {}) { acpSpawns: [], otherSpawns: [], helpSpawns: [], + topHelpSpawns: [], spawnSyncs: [], execFiles: [], modelsBehavior: options.modelsBehavior ?? { stdout: modelsOutput(BASE_MODEL_ROWS) }, @@ -490,7 +491,18 @@ export async function createBridgeKit(options = {}) { }, }) const spawn = (file, args = [], opts = {}) => { - if (args[0] === "models") return spawnModels(kit, file, args) + // agy's own flags before the subcommand (--settings for `agy models`) + const sub = args[0] === "--settings" ? args[2] : args[0] + if (args[0] === "--help") { + kit.topHelpSpawns.push({ file, args }) + const child = new FakeChild(kit, file, args) + setImmediate(() => { + if (options.topHelp) child.stderr.emit("data", options.topHelp) + child.finish(0) + }) + return child + } + if (sub === "models") return spawnModels(kit, file, args) if (args[0] === "acp" && args.includes("--help")) { // agy's flag usage goes to stderr; an older agy doesn't list the flag kit.helpSpawns.push({ file, args })