opencode-magpie-plugin 0.1.5 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +11 -5
  2. package/dist/index.mjs +58 -14
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -27,7 +27,7 @@ OpenCode V2
27
27
  - 启动时发现目录,每 30 秒刷新;服务新增、模型下线都能反映到 OpenCode。
28
28
  - Provider 显示 Magpie 中的分组名称,而不是统一挤在一个 “Magpie” 下;路由分组统一归入 Magpie。
29
29
  - 内置独立价格表和模型名归一化逻辑,支持输入、输出、缓存读取与写入价格。
30
- - Magpie 已安装但未运行时自动执行 `magpie serve`;未安装时给出安装链接。
30
+ - Magpie 已安装但未运行时自动启动;macOS 上找到 app 内的可执行文件时优先启动 app,保留路由页;未安装时给出安装链接。
31
31
  - 登录和服务凭据由 Magpie 管理,插件不需要复制上游 token。
32
32
 
33
33
  这不是一个绕过 Magpie 的社区插件宿主:**推理请求仍经过本机 Magpie 网关**。
@@ -55,7 +55,7 @@ OpenCode V2
55
55
 
56
56
  ```jsonc
57
57
  {
58
- "plugins": ["opencode-magpie-plugin@0.1.5"]
58
+ "plugins": ["opencode-magpie-plugin@0.1.6"]
59
59
  }
60
60
  ```
61
61
 
@@ -122,9 +122,9 @@ MAGPIE_ADDR=127.0.0.1:3426 opencode --standalone
122
122
 
123
123
  `0.0.0.0`、`::` 监听地址会转换为本机访问地址。本版本面向本机网关,不支持远程服务器地址。共享服务模式下,在终端临时设置变量不一定会改变已经启动的 OpenCode 服务环境。
124
124
 
125
- 网关不在线时,插件在 PATH、`~/.local/bin`、常见 Homebrew 路径以及 macOS 的 `Magpie.app` 中寻找可执行文件,并后台启动 `magpie serve`。Windows 通过 PATH / `~/.local/bin` 寻找 `magpie.exe`。如果无法找到,请安装 Magpie 或把它加入 OpenCode 服务的 PATH。
125
+ 网关不在线时,插件在 PATH、`~/.local/bin`、常见 Homebrew 路径以及 macOS 的 `Magpie.app` 中寻找可执行文件并把它拉起来。找到的是 `Magpie.app` 里的可执行文件时(安装器的软链会解析到真实路径),macOS 上优先启动 `magpie tray`——它同样提供网关,而且带窗口,路由页可以查看。app 非零退出或被信号终止后,才退回无窗口的 `magpie serve`。其他情况直接用 `serve`。Windows 通过 PATH / `~/.local/bin` 寻找 `magpie.exe`。如果无法找到,请安装 Magpie 或把它加入 OpenCode 服务的 PATH。
126
126
 
127
- 启动后会等待网关就绪;请求前和目录刷新时也会检查网关。插件卸载不会关闭已启动的共享网关。若端口被其他服务占用,会报错而不是向其发送模型请求。
127
+ 启动后会等待网关就绪;app 明确异常退出时才降级到 `serve`。app 仍在启动或正常退出(可能已交给现有 app)时,超时会提示检查 app,不会另起 `serve` 抢占端口。请求前和目录刷新时也会检查网关。插件卸载不会关闭已启动的共享网关。若端口被其他服务占用,会报错而不是向其发送模型请求。
128
128
 
129
129
  便携版使用自定义端口时,请显式设置 `MAGPIE_ADDR`;插件不自动推断便携版的 data 目录。
130
130
 
@@ -139,10 +139,16 @@ MAGPIE_ADDR=127.0.0.1:3426 opencode --standalone
139
139
  1. 内置价格表,优先应用已识别 provider 的专属覆盖。
140
140
  2. 内置表未覆盖时,使用 Magpie 返回的 `magpie_price`。
141
141
  3. 普通模型仍缺价时,查询 models.dev 本地缓存:优先同 provider 的精确 ID,再查已识别厂商(OpenAI、Anthropic、DeepSeek、Google、xAI)的精确 ID 或 `canonical_model_id`。不跨版本猜价、不采用其他转售商报价、不使用订阅 plan 目录的零价。
142
- 4. 所有来源均没有价格时,不启用该模型,避免把未知价格显示为免费。路由分组仍只用 Magpie 报价,不根据 `auto-*` 名称推断成员价格。
142
+ 4. 所有来源均没有价格时,不启用该模型,避免把未知价格显示为免费。**路由组例外**:Magpie 只在组内全部成员同价时才给组报价,而路由组每一轮的成员由路由决定,成本本来就是一个区间而不是单一数字。这类组保持可选用、保留自己的名字,只是费用读数不精确。路由分组仍只用 Magpie 报价,不根据 `auto-*` 名称推断成员价格。
143
143
 
144
144
  models.dev 缓存写入 `$XDG_CACHE_HOME/opencode-magpie-plugin/models.json`(默认 `~/.cache/opencode-magpie-plugin/models.json`)。首次可只读复用 Magpie、OpenCode 的 `models.json`;下载只写插件自己的目录。每次模型刷新检查缓存,超过 24 小时后台更新,缺价时可提前到 6 小时;同进程下载去重且失败后至少间隔 6 小时重试。首次下载和更新不阻塞模型发现,成功后下一次 30 秒刷新采用新目录;离线保留旧缓存。下载源为 `https://models.dev/api.json`,仍然零运行时依赖,目前只采用基础价,未接入阶梯价。
145
145
 
146
+ ### 0.1.6
147
+
148
+ - 无报价路由组保持可选并保留原名;OpenCode 对这些请求的费用估算仍为 0,不代表实际免费,实际费用请查看 Magpie 或服务账单。
149
+ - macOS 找到 Magpie app 时优先启动带窗口的网关,解析安装器软链;仅在 app 异常退出时降级到 `serve`,避免慢启动或交接现有 app 时被无窗口进程抢占端口。
150
+ - 内置费率不变,尚未接入按请求实际路由成员计费。
151
+
146
152
  ### 0.1.5
147
153
 
148
154
  新增 models.dev 缓存作为缺价兜底,不改动内置费率和 provider 覆盖。补充的价格来自 models.dev,对应目录更新后可能变化;以实际服务账单为准。
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { n as getModelPricing } from "./model-pricing--x7wwPba.mjs";
2
2
  import { randomUUID } from "node:crypto";
3
- import { access, mkdir, readFile, rename, rm, stat, writeFile } from "node:fs/promises";
3
+ import { access, mkdir, readFile, realpath, rename, rm, stat, writeFile } from "node:fs/promises";
4
4
  import { homedir } from "node:os";
5
5
  import { delimiter, dirname, join } from "node:path";
6
6
  import { spawn } from "node:child_process";
@@ -305,8 +305,8 @@ const buildInventory = (rows, baseURL, options = {}) => {
305
305
  modalities: row.modalities,
306
306
  tool_call: true
307
307
  });
308
- model.enabled = !!cost;
309
- if (!cost) model.name += " (价格未知)";
308
+ if (!cost && !group) model.name += " (价格未知)";
309
+ model.enabled = !!cost || group;
310
310
  const anthropic = row.native_endpoints?.includes("/v1/messages");
311
311
  const responses = !anthropic && row.native_endpoints?.includes("/v1/responses");
312
312
  model.package = anthropic ? "@opencode/ai/providers/anthropic-compatible" : responses ? "@opencode/ai/providers/openai-compatible/responses" : "@opencode/ai/providers/openai-compatible";
@@ -372,7 +372,9 @@ const findExecutable = async () => {
372
372
  "/Applications/Magpie.app/Contents/MacOS/magpie",
373
373
  join(homedir(), "Applications/Magpie.app/Contents/MacOS/magpie")
374
374
  ];
375
- return (await Promise.all(candidates.map(async (path) => await access(path, constants.X_OK).then(() => path, () => void 0)))).find(Boolean);
375
+ const executable = (await Promise.all(candidates.map(async (path) => await access(path, constants.X_OK).then(() => path, () => void 0)))).find(Boolean);
376
+ if (!executable) return void 0;
377
+ return await realpath(executable).catch(() => executable);
376
378
  };
377
379
  const probeGateway = async (baseURL) => {
378
380
  let response;
@@ -385,19 +387,63 @@ const probeGateway = async (baseURL) => {
385
387
  if (!response.ok || info?.name !== "magpie") throw new Error(`${baseURL} 已被非 Magpie 服务占用。`);
386
388
  return true;
387
389
  };
388
- const startGateway = async (executable) => {
390
+ /**
391
+ * How to bring the gateway up when nothing is listening.
392
+ *
393
+ * `magpie serve` runs the gateway with no window, and only a window can show Magpie's Routing
394
+ * view. A windowless gateway that wins the port keeps Magpie's own app from starting one — the
395
+ * app does not take the port back, it just reports that routing cannot be watched there. On
396
+ * macOS the bundled executable is the app, which serves the gateway as well and keeps the
397
+ * Routing view available.
398
+ *
399
+ * `--web` is not an option here: it opens a second listener on a fixed port, and when that port
400
+ * is taken the whole gateway exits instead of just the page.
401
+ */
402
+ const gatewayArgs = (executable) => {
403
+ const app = /[\\/]magpie\.app[\\/]/i.test(executable);
404
+ return process.platform === "darwin" && app ? ["tray"] : ["serve"];
405
+ };
406
+ /**
407
+ * The commands to try, in order, when nothing is listening.
408
+ *
409
+ * The app is preferred on macOS because only it has a window, and a window is what shows the
410
+ * Routing view. It needs a GUI session, though: a Mac reached over SSH, or one where OpenCode
411
+ * runs as a daemon, has none, and `magpie tray` then exits without serving. The windowless
412
+ * gateway has no such requirement, which is why it stays as the fallback rather than being
413
+ * replaced.
414
+ */
415
+ const startAttempts = (executable) => {
416
+ const preferred = gatewayArgs(executable);
417
+ return preferred[0] === "tray" ? [preferred, ["serve"]] : [preferred];
418
+ };
419
+ const startGateway = async (executable, args) => {
420
+ const state = { failed: false };
389
421
  await new Promise((resolve, reject) => {
390
- const child = spawn(executable, ["serve"], {
422
+ const child = spawn(executable, args, {
391
423
  detached: true,
392
424
  stdio: "ignore",
393
425
  windowsHide: true
394
426
  });
395
427
  child.once("error", reject);
428
+ child.once("exit", (code, signal) => {
429
+ state.failed = code !== null && code !== 0 || signal != null;
430
+ });
396
431
  child.once("spawn", () => {
397
432
  child.unref();
398
433
  resolve();
399
434
  });
400
435
  });
436
+ return state;
437
+ };
438
+ /** Polls until the gateway answers, or gives up after `tries` half-second waits. */
439
+ const waitForGateway = async (baseURL, tries, state) => {
440
+ for (let remaining = tries; remaining >= 0; remaining -= 1) {
441
+ if (await probeGateway(baseURL)) return true;
442
+ if (state.failed) return false;
443
+ if (remaining === 0) return false;
444
+ await setTimeout(500);
445
+ }
446
+ return false;
401
447
  };
402
448
  const starting = /* @__PURE__ */ new Map();
403
449
  const ensureGateway = async (baseURL) => {
@@ -407,14 +453,12 @@ const ensureGateway = async (baseURL) => {
407
453
  const task = (async () => {
408
454
  const executable = await findExecutable();
409
455
  if (!executable) throw new Error(INSTALL_MESSAGE);
410
- await startGateway(executable);
411
- const wait = async (remaining) => {
412
- if (await probeGateway(baseURL)) return;
413
- if (remaining === 0) throw new Error("Magpie 网关启动超时,请运行 magpie serve 查看错误。");
414
- await setTimeout(500);
415
- return wait(remaining - 1);
416
- };
417
- await wait(30);
456
+ for (const args of startAttempts(executable)) {
457
+ const state = await startGateway(executable, args);
458
+ if (await waitForGateway(baseURL, args[0] === "tray" ? 60 : 30, state)) return;
459
+ if (args[0] === "tray" && !state.failed) throw new Error("Magpie app 网关尚未就绪,请在 app 中检查网关状态;未启动无窗口服务,以免抢占端口。");
460
+ }
461
+ throw new Error("Magpie 网关启动超时,请运行 magpie serve 查看错误。");
418
462
  })();
419
463
  starting.set(baseURL, task);
420
464
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-magpie-plugin",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "description": "Discover Magpie providers and models in OpenCode V2 with built-in pricing and automatic local gateway startup",
5
5
  "type": "module",
6
6
  "main": "./dist/index.mjs",