opencode-magpie-plugin 0.1.5 → 0.1.7
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.
- package/README.md +21 -5
- package/dist/index.d.mts +1 -0
- package/dist/index.mjs +106 -17
- 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
|
|
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.
|
|
58
|
+
"plugins": ["opencode-magpie-plugin@0.1.7"]
|
|
59
59
|
}
|
|
60
60
|
```
|
|
61
61
|
|
|
@@ -91,6 +91,10 @@ Routing group(例如 `group/auto`)会跨多个服务路由,因此不按 `o
|
|
|
91
91
|
|
|
92
92
|
思考强度读取新版 `reasoning_levels`,并兼容已安装版本常见的 `supported_reasoning_levels`(`{ effort }` 对象或字符串列表)。只有网关实际声明的档位才会出现,可在 OpenCode 的模型变体控件中选择,或使用 `provider/model#high` 这样的引用。`default` 保留服务默认行为,不额外生成档位;网关未提供档位时不会凭模型名称猜测。
|
|
93
93
|
|
|
94
|
+
支持 Fast 的模型会额外显示一个 `-fast` 模型,例如 `composer-2.5` / `composer-2.5-fast`,显示名为 `Composer 2.5` / `Composer 2.5 Fast`。选择 Fast 后仍向 Magpie 发送原模型 ID,同时附带 `service_tier: "priority"`,并保留原有思考档位。检测依据是 `/v1/models` 的 `service_tiers` 包含 `priority`;旧网关漏报时,补读本机 Magpie 配置目录的 `plugin-providers.json` 中 Cursor 的 `fast: true` 或插件的 `serviceTiers`。仅扩展网关当前列出的模型,已有同名 `-fast` 模型优先,刷新时同步增删。
|
|
95
|
+
|
|
96
|
+
Fast 模型暂沿用原模型的参考单价,不代表 Fast 实际费率;实际加价以 Magpie 或服务账单为准。缺少原模型报价时仍按未知价格处理。
|
|
97
|
+
|
|
94
98
|
根据 `native_endpoints` 选择 OpenCode 内置 runtime:
|
|
95
99
|
|
|
96
100
|
| Magpie 元数据 | OpenCode runtime |
|
|
@@ -122,9 +126,9 @@ MAGPIE_ADDR=127.0.0.1:3426 opencode --standalone
|
|
|
122
126
|
|
|
123
127
|
`0.0.0.0`、`::` 监听地址会转换为本机访问地址。本版本面向本机网关,不支持远程服务器地址。共享服务模式下,在终端临时设置变量不一定会改变已经启动的 OpenCode 服务环境。
|
|
124
128
|
|
|
125
|
-
网关不在线时,插件在 PATH、`~/.local/bin`、常见 Homebrew 路径以及 macOS 的 `Magpie.app`
|
|
129
|
+
网关不在线时,插件在 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
130
|
|
|
127
|
-
|
|
131
|
+
启动后会等待网关就绪;app 明确异常退出时才降级到 `serve`。app 仍在启动或正常退出(可能已交给现有 app)时,超时会提示检查 app,不会另起 `serve` 抢占端口。请求前和目录刷新时也会检查网关。插件卸载不会关闭已启动的共享网关。若端口被其他服务占用,会报错而不是向其发送模型请求。
|
|
128
132
|
|
|
129
133
|
便携版使用自定义端口时,请显式设置 `MAGPIE_ADDR`;插件不自动推断便携版的 data 目录。
|
|
130
134
|
|
|
@@ -139,10 +143,22 @@ MAGPIE_ADDR=127.0.0.1:3426 opencode --standalone
|
|
|
139
143
|
1. 内置价格表,优先应用已识别 provider 的专属覆盖。
|
|
140
144
|
2. 内置表未覆盖时,使用 Magpie 返回的 `magpie_price`。
|
|
141
145
|
3. 普通模型仍缺价时,查询 models.dev 本地缓存:优先同 provider 的精确 ID,再查已识别厂商(OpenAI、Anthropic、DeepSeek、Google、xAI)的精确 ID 或 `canonical_model_id`。不跨版本猜价、不采用其他转售商报价、不使用订阅 plan 目录的零价。
|
|
142
|
-
4.
|
|
146
|
+
4. 所有来源均没有价格时,不启用该模型,避免把未知价格显示为免费。**路由组例外**:Magpie 只在组内全部成员同价时才给组报价,而路由组每一轮的成员由路由决定,成本本来就是一个区间而不是单一数字。这类组保持可选用、保留自己的名字,只是费用读数不精确。路由分组仍只用 Magpie 报价,不根据 `auto-*` 名称推断成员价格。
|
|
143
147
|
|
|
144
148
|
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
149
|
|
|
150
|
+
### 0.1.7
|
|
151
|
+
|
|
152
|
+
- 根据模型声明的 Fast 能力自动派生 `-fast` 模型,使用原模型 ID 加 `service_tier: "priority"` 路由,并保留思考档位。
|
|
153
|
+
- 兼容旧网关未透出的本地插件 Fast 元数据;已有同名模型优先,目录刷新同步增删。
|
|
154
|
+
- 内置费率不变;派生 Fast 模型沿用原模型参考价,尚未接入独立 Fast 费率。
|
|
155
|
+
|
|
156
|
+
### 0.1.6
|
|
157
|
+
|
|
158
|
+
- 无报价路由组保持可选并保留原名;OpenCode 对这些请求的费用估算仍为 0,不代表实际免费,实际费用请查看 Magpie 或服务账单。
|
|
159
|
+
- macOS 找到 Magpie app 时优先启动带窗口的网关,解析安装器软链;仅在 app 异常退出时降级到 `serve`,避免慢启动或交接现有 app 时被无窗口进程抢占端口。
|
|
160
|
+
- 内置费率不变,尚未接入按请求实际路由成员计费。
|
|
161
|
+
|
|
146
162
|
### 0.1.5
|
|
147
163
|
|
|
148
164
|
新增 models.dev 缓存作为缺价兜底,不改动内置费率和 provider 覆盖。补充的价格来自 models.dev,对应目录更新后可能变化;以实际服务账单为准。
|
package/dist/index.d.mts
CHANGED
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";
|
|
@@ -288,6 +288,8 @@ const modelCost = (row, group, catalog) => {
|
|
|
288
288
|
};
|
|
289
289
|
const buildInventory = (rows, baseURL, options = {}) => {
|
|
290
290
|
const providers = /* @__PURE__ */ new Map();
|
|
291
|
+
const fastModelIDs = new Set(options.fastModelIDs);
|
|
292
|
+
for (const row of rows) if (row.service_tiers?.some((tier) => (typeof tier === "string" ? tier : tier.id) === "priority")) fastModelIDs.add(row.id);
|
|
291
293
|
rows.filter((row) => !row.kind && row.id.includes("/")).forEach((row) => {
|
|
292
294
|
const group = row.id.startsWith("group/");
|
|
293
295
|
const owner = group ? ROUTING_GROUP : row.owned_by;
|
|
@@ -305,8 +307,8 @@ const buildInventory = (rows, baseURL, options = {}) => {
|
|
|
305
307
|
modalities: row.modalities,
|
|
306
308
|
tool_call: true
|
|
307
309
|
});
|
|
308
|
-
model.
|
|
309
|
-
|
|
310
|
+
if (!cost && !group) model.name += " (价格未知)";
|
|
311
|
+
model.enabled = !!cost || group;
|
|
310
312
|
const anthropic = row.native_endpoints?.includes("/v1/messages");
|
|
311
313
|
const responses = !anthropic && row.native_endpoints?.includes("/v1/responses");
|
|
312
314
|
model.package = anthropic ? "@opencode/ai/providers/anthropic-compatible" : responses ? "@opencode/ai/providers/openai-compatible/responses" : "@opencode/ai/providers/openai-compatible";
|
|
@@ -337,7 +339,44 @@ const buildInventory = (rows, baseURL, options = {}) => {
|
|
|
337
339
|
if (!entry.models.some((existing) => existing.id === model.id)) entry.models.push(model);
|
|
338
340
|
providers.set(providerID, entry);
|
|
339
341
|
});
|
|
340
|
-
return [...providers.values()]
|
|
342
|
+
return [...providers.values()].map((entry) => {
|
|
343
|
+
const ids = new Set(entry.models.map((model) => model.id));
|
|
344
|
+
return {
|
|
345
|
+
...entry,
|
|
346
|
+
models: entry.models.flatMap((model) => {
|
|
347
|
+
if (!fastModelIDs.has(model.modelID) || /-fast(?:@\d+[km])?$/i.test(model.id) || ids.has(`${model.id}-fast`)) return [model];
|
|
348
|
+
return [model, {
|
|
349
|
+
...model,
|
|
350
|
+
id: `${model.id}-fast`,
|
|
351
|
+
name: `${model.name.replace(/ \(价格未知\)$/, "")} Fast${model.enabled ? "" : " (价格未知)"}`,
|
|
352
|
+
body: {
|
|
353
|
+
...model.body,
|
|
354
|
+
service_tier: "priority"
|
|
355
|
+
}
|
|
356
|
+
}];
|
|
357
|
+
})
|
|
358
|
+
};
|
|
359
|
+
});
|
|
360
|
+
};
|
|
361
|
+
|
|
362
|
+
//#endregion
|
|
363
|
+
//#region src/fast-models.ts
|
|
364
|
+
/** Older gateways omit Cursor's fast flag from /v1/models; read only capability metadata. */
|
|
365
|
+
const loadFastModelIDs = async () => {
|
|
366
|
+
const path = join(process.env.XDG_CONFIG_HOME || join(homedir(), ".config"), "magpie", "plugin-providers.json");
|
|
367
|
+
const ids = /* @__PURE__ */ new Set();
|
|
368
|
+
try {
|
|
369
|
+
const providers = JSON.parse(await readFile(path, "utf8"));
|
|
370
|
+
if (!Array.isArray(providers)) return ids;
|
|
371
|
+
for (const provider of providers) {
|
|
372
|
+
if (typeof provider?.id !== "string" || !Array.isArray(provider.models)) continue;
|
|
373
|
+
for (const model of provider.models) {
|
|
374
|
+
if (typeof model?.id !== "string") continue;
|
|
375
|
+
if (Array.isArray(model.serviceTiers) && model.serviceTiers.includes("priority") || provider.id === "cursor" && model.fast === true) ids.add(`${provider.id}/${model.id}`);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
} catch {}
|
|
379
|
+
return ids;
|
|
341
380
|
};
|
|
342
381
|
|
|
343
382
|
//#endregion
|
|
@@ -372,7 +411,9 @@ const findExecutable = async () => {
|
|
|
372
411
|
"/Applications/Magpie.app/Contents/MacOS/magpie",
|
|
373
412
|
join(homedir(), "Applications/Magpie.app/Contents/MacOS/magpie")
|
|
374
413
|
];
|
|
375
|
-
|
|
414
|
+
const executable = (await Promise.all(candidates.map(async (path) => await access(path, constants.X_OK).then(() => path, () => void 0)))).find(Boolean);
|
|
415
|
+
if (!executable) return void 0;
|
|
416
|
+
return await realpath(executable).catch(() => executable);
|
|
376
417
|
};
|
|
377
418
|
const probeGateway = async (baseURL) => {
|
|
378
419
|
let response;
|
|
@@ -385,19 +426,63 @@ const probeGateway = async (baseURL) => {
|
|
|
385
426
|
if (!response.ok || info?.name !== "magpie") throw new Error(`${baseURL} 已被非 Magpie 服务占用。`);
|
|
386
427
|
return true;
|
|
387
428
|
};
|
|
388
|
-
|
|
429
|
+
/**
|
|
430
|
+
* How to bring the gateway up when nothing is listening.
|
|
431
|
+
*
|
|
432
|
+
* `magpie serve` runs the gateway with no window, and only a window can show Magpie's Routing
|
|
433
|
+
* view. A windowless gateway that wins the port keeps Magpie's own app from starting one — the
|
|
434
|
+
* app does not take the port back, it just reports that routing cannot be watched there. On
|
|
435
|
+
* macOS the bundled executable is the app, which serves the gateway as well and keeps the
|
|
436
|
+
* Routing view available.
|
|
437
|
+
*
|
|
438
|
+
* `--web` is not an option here: it opens a second listener on a fixed port, and when that port
|
|
439
|
+
* is taken the whole gateway exits instead of just the page.
|
|
440
|
+
*/
|
|
441
|
+
const gatewayArgs = (executable) => {
|
|
442
|
+
const app = /[\\/]magpie\.app[\\/]/i.test(executable);
|
|
443
|
+
return process.platform === "darwin" && app ? ["tray"] : ["serve"];
|
|
444
|
+
};
|
|
445
|
+
/**
|
|
446
|
+
* The commands to try, in order, when nothing is listening.
|
|
447
|
+
*
|
|
448
|
+
* The app is preferred on macOS because only it has a window, and a window is what shows the
|
|
449
|
+
* Routing view. It needs a GUI session, though: a Mac reached over SSH, or one where OpenCode
|
|
450
|
+
* runs as a daemon, has none, and `magpie tray` then exits without serving. The windowless
|
|
451
|
+
* gateway has no such requirement, which is why it stays as the fallback rather than being
|
|
452
|
+
* replaced.
|
|
453
|
+
*/
|
|
454
|
+
const startAttempts = (executable) => {
|
|
455
|
+
const preferred = gatewayArgs(executable);
|
|
456
|
+
return preferred[0] === "tray" ? [preferred, ["serve"]] : [preferred];
|
|
457
|
+
};
|
|
458
|
+
const startGateway = async (executable, args) => {
|
|
459
|
+
const state = { failed: false };
|
|
389
460
|
await new Promise((resolve, reject) => {
|
|
390
|
-
const child = spawn(executable,
|
|
461
|
+
const child = spawn(executable, args, {
|
|
391
462
|
detached: true,
|
|
392
463
|
stdio: "ignore",
|
|
393
464
|
windowsHide: true
|
|
394
465
|
});
|
|
395
466
|
child.once("error", reject);
|
|
467
|
+
child.once("exit", (code, signal) => {
|
|
468
|
+
state.failed = code !== null && code !== 0 || signal != null;
|
|
469
|
+
});
|
|
396
470
|
child.once("spawn", () => {
|
|
397
471
|
child.unref();
|
|
398
472
|
resolve();
|
|
399
473
|
});
|
|
400
474
|
});
|
|
475
|
+
return state;
|
|
476
|
+
};
|
|
477
|
+
/** Polls until the gateway answers, or gives up after `tries` half-second waits. */
|
|
478
|
+
const waitForGateway = async (baseURL, tries, state) => {
|
|
479
|
+
for (let remaining = tries; remaining >= 0; remaining -= 1) {
|
|
480
|
+
if (await probeGateway(baseURL)) return true;
|
|
481
|
+
if (state.failed) return false;
|
|
482
|
+
if (remaining === 0) return false;
|
|
483
|
+
await setTimeout(500);
|
|
484
|
+
}
|
|
485
|
+
return false;
|
|
401
486
|
};
|
|
402
487
|
const starting = /* @__PURE__ */ new Map();
|
|
403
488
|
const ensureGateway = async (baseURL) => {
|
|
@@ -407,14 +492,12 @@ const ensureGateway = async (baseURL) => {
|
|
|
407
492
|
const task = (async () => {
|
|
408
493
|
const executable = await findExecutable();
|
|
409
494
|
if (!executable) throw new Error(INSTALL_MESSAGE);
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
if (await
|
|
413
|
-
if (
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
};
|
|
417
|
-
await wait(30);
|
|
495
|
+
for (const args of startAttempts(executable)) {
|
|
496
|
+
const state = await startGateway(executable, args);
|
|
497
|
+
if (await waitForGateway(baseURL, args[0] === "tray" ? 60 : 30, state)) return;
|
|
498
|
+
if (args[0] === "tray" && !state.failed) throw new Error("Magpie app 网关尚未就绪,请在 app 中检查网关状态;未启动无窗口服务,以免抢占端口。");
|
|
499
|
+
}
|
|
500
|
+
throw new Error("Magpie 网关启动超时,请运行 magpie serve 查看错误。");
|
|
418
501
|
})();
|
|
419
502
|
starting.set(baseURL, task);
|
|
420
503
|
try {
|
|
@@ -448,7 +531,10 @@ const setupPlugin = async (ctx) => {
|
|
|
448
531
|
const baseURL = await resolveBaseURL();
|
|
449
532
|
await ensureGateway(baseURL);
|
|
450
533
|
let catalog = await loadCatalogCache();
|
|
451
|
-
let inventory = buildInventory(await fetchModels(baseURL), baseURL, {
|
|
534
|
+
let inventory = buildInventory(await fetchModels(baseURL), baseURL, {
|
|
535
|
+
catalog: catalog.data,
|
|
536
|
+
fastModelIDs: await loadFastModelIDs()
|
|
537
|
+
});
|
|
452
538
|
const { track, cleanup } = createTrackedSetup();
|
|
453
539
|
let disposed = false;
|
|
454
540
|
let refreshing = false;
|
|
@@ -488,7 +574,10 @@ const setupPlugin = async (ctx) => {
|
|
|
488
574
|
await ensureGateway(baseURL);
|
|
489
575
|
const rows = await fetchModels(baseURL);
|
|
490
576
|
catalog = await loadCatalogCache(hasUnpricedModel(inventory));
|
|
491
|
-
await apply(buildInventory(rows, baseURL, {
|
|
577
|
+
await apply(buildInventory(rows, baseURL, {
|
|
578
|
+
catalog: catalog.data,
|
|
579
|
+
fastModelIDs: await loadFastModelIDs()
|
|
580
|
+
}));
|
|
492
581
|
} catch (error) {
|
|
493
582
|
console.warn("[magpie] 模型刷新失败,保留上次目录:", error instanceof Error ? error.message : String(error));
|
|
494
583
|
} finally {
|
package/package.json
CHANGED