opencode-magpie-plugin 0.1.8 → 0.2.0
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 +49 -6
- package/dist/index.d.mts +2 -0
- package/dist/index.mjs +67 -9
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -25,7 +25,7 @@ OpenCode V2
|
|
|
25
25
|
|
|
26
26
|
- `opencode.jsonc` 只加载插件,不维护 `providers.models`。
|
|
27
27
|
- 启动时发现目录,每 30 秒刷新;服务新增、模型下线都能反映到 OpenCode。
|
|
28
|
-
- Provider 显示 Magpie 中的分组名称,而不是统一挤在一个 “Magpie”
|
|
28
|
+
- Provider 显示 Magpie 中的分组名称,而不是统一挤在一个 “Magpie” 下。路由分组默认仍归入 Magpie;要换呈现方式,在插件 `options` 里声明,不要改构建产物。
|
|
29
29
|
- 内置独立价格表和模型名归一化逻辑,支持输入、输出、缓存读取与写入价格。
|
|
30
30
|
- Magpie 已安装但未运行时自动启动;macOS 上找到 app 内的可执行文件时优先启动 app,保留路由页;未安装时给出安装链接。
|
|
31
31
|
- 登录和服务凭据由 Magpie 管理,插件不需要复制上游 token。
|
|
@@ -81,7 +81,39 @@ opencode service restart
|
|
|
81
81
|
|
|
82
82
|
示例模型仅说明格式,实际列表取决于 Magpie 当前账号与配置。Magpie 自定义 provider ID 会原样映射为带 `magpie-` 前缀的 ID。
|
|
83
83
|
|
|
84
|
-
Routing group(例如 `group/auto`)会跨多个服务路由,因此不按 `owned_by` 或首个候选服务拆成 provider
|
|
84
|
+
Routing group(例如 `group/auto`)会跨多个服务路由,因此不按 `owned_by` 或首个候选服务拆成 provider。没有插件 options 时,它们统一归到 **Magpie** 分组下。发送时仍保留网关原始 id(`group/auto`),价格只用 Magpie 给出的价格,不按成员猜测。
|
|
85
|
+
|
|
86
|
+
### 目录呈现
|
|
87
|
+
|
|
88
|
+
路由组的成员由 Magpie 维护;这个插件只决定 OpenCode 选择器里怎么摆。分组、显示名和排序写在 `opencode.jsonc` 的插件 `options` 里,升级插件包之后仍在,不用再改 `dist`。空 options(或仍用字符串引用插件)和不声明时完全一样:所有 `group/` 行都在 `magpie-magpie`。
|
|
89
|
+
|
|
90
|
+
```jsonc
|
|
91
|
+
{
|
|
92
|
+
"plugins": [
|
|
93
|
+
{
|
|
94
|
+
"package": "opencode-magpie-plugin",
|
|
95
|
+
"options": {
|
|
96
|
+
"groups": [
|
|
97
|
+
{ "prefix": "adams-", "id": "adams", "name": "Adams" },
|
|
98
|
+
{ "prefix": "combo-", "id": "combo", "name": "Combo" }
|
|
99
|
+
],
|
|
100
|
+
"order": {
|
|
101
|
+
"magpie-adams": ["deepseek-v4.1-flash"],
|
|
102
|
+
"magpie-cursor": ["grok-4.7@500k"]
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
]
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
上面只是写法示例,不是插件默认值。语义:
|
|
111
|
+
|
|
112
|
+
- 只对 id 以 `group/` 开头的行分桶。`groups` 按 `prefix` 最长优先匹配。命中后 provider 是 `magpie-<id>`,显示名是 `name`,选择器里的模型 id 去掉这段 prefix(`group/adams-deepseek-v4.1-flash` → `magpie-adams/deepseek-v4.1-flash`)。发给 Magpie 的仍是原始 id。没匹配到的组保持今天的行为:`magpie-magpie`,显示名 Magpie,id 不去前缀。厂商模型(`cursor/`、`workbuddy/`)不受 `groups` 影响。
|
|
113
|
+
- OpenCode 同 provider 内按 `time.released` 倒序排。`order` 的 key 是最终 provider id,值是最终模型 id(已去前缀),越靠前越靠前。名单里没有的保持 0,退化为字母序。`-fast` 克隆继承父模型的权重并紧跟在后面;不要给它单独写一条,除非那个 `-fast` id 自己出现在 `order` 里。provider 之间的顺序控制不了。
|
|
114
|
+
- 插件不隐藏模型。网关列出的都会出现,没标价的也一样。不想在 OpenCode 里看到某个组,在 Magpie 里处理:不用了就 `magpie group rm <id>`,只对 OpenCode 收窄就用 `magpie visible opencode …`。组的成员是厂商模型时,删掉一个组不会影响把同一批模型当成员的其他组。
|
|
115
|
+
|
|
116
|
+
`options` 非法会在启动时失败,并写明出错字段的路径,不会静默忽略。改 options 后要重启 OpenCode 服务(`opencode service restart`)。热重载不一定会重新执行 `setup`,目录的 30 秒刷新也仍用启动时解析的那一份。
|
|
85
117
|
|
|
86
118
|
## 自动发现与请求行为
|
|
87
119
|
|
|
@@ -142,11 +174,20 @@ MAGPIE_ADDR=127.0.0.1:3426 opencode --standalone
|
|
|
142
174
|
|
|
143
175
|
1. 内置价格表,优先应用已识别 provider 的专属覆盖。
|
|
144
176
|
2. 内置表未覆盖时,使用 Magpie 返回的 `magpie_price`。
|
|
145
|
-
3. 普通模型仍缺价时,查询 models.dev 本地缓存:优先同 provider 的精确 ID,再查已识别厂商(OpenAI、Anthropic、DeepSeek、Google、xAI)的精确 ID 或 `canonical_model_id`。不跨版本猜价、不采用其他转售商报价、不使用订阅 plan 目录的零价。
|
|
146
|
-
4.
|
|
177
|
+
3. 普通模型仍缺价时,查询 models.dev 本地缓存:优先同 provider 的精确 ID,再查已识别厂商(OpenAI、Anthropic、DeepSeek、Google、xAI、Meta)的精确 ID 或 `canonical_model_id`。不跨版本猜价、不采用其他转售商报价、不使用订阅 plan 目录的零价。
|
|
178
|
+
4. 所有来源均没有价格时,模型仍然列出、仍然可选,只是不填 `cost`。空费用不是 0 元,OpenCode 不会把它当成免费;实际费用以 Magpie 或服务账单为准。**路由组**同样如此:Magpie 只在组内全部成员同价时才给组报价,而路由组每一轮的成员由路由决定,成本本来就是一个区间而不是单一数字。路由分组仍只用 Magpie 报价,不根据 `auto-*` 名称推断成员价格。
|
|
147
179
|
|
|
148
180
|
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`,仍然零运行时依赖,基础价和 context 阶梯价一并传给 OpenCode。
|
|
149
181
|
|
|
182
|
+
### 0.2.0
|
|
183
|
+
|
|
184
|
+
- 目录呈现改为插件 options:路由组的分桶、显示名和排序写在 `opencode.jsonc`,升级不再冲掉使用者自己的摆法。默认为空,与之前的目录一致。网关、价格表和 Fast 计价不变。
|
|
185
|
+
|
|
186
|
+
### 0.1.9
|
|
187
|
+
|
|
188
|
+
- 缺价模型不再从选择器里拿掉:仍然列出、仍然可选,只是不填 `cost`。空费用不是 0 元,也不代表实际免费。
|
|
189
|
+
- `muse-*` 在 Magpie 未报价时,回退到 models.dev 的 Meta 官方价,不采用转售商报价。Muse Wonder 1.0 为输入/输出/缓存读 `$4/$20/$0.20`,Muse Spark 1.3 为 `$1.25/$4.25/$0.15`。单位为美元/百万 token。这是 Meta 标价,不是 Cursor 账单价。
|
|
190
|
+
|
|
150
191
|
### 0.1.8
|
|
151
192
|
|
|
152
193
|
- 修复上下文阶梯价丢失:保留内置表、models.dev 的 `tier.type=context` 与 Magpie 的 `above` 分档,交给 OpenCode 根据单次输入(含缓存)选择整单费率。
|
|
@@ -233,8 +274,10 @@ pnpm check
|
|
|
233
274
|
| `/plugins` 没有 `magpie.providers` | 全局配置是否被当前服务读取;file 模式是否填写 `dist` 目录并已构建;查看 OpenCode 日志 |
|
|
234
275
|
| 提示未安装 Magpie | 安装 [Magpie](https://usemagpie.ai/zh/),确认服务进程 PATH 可以找到它 |
|
|
235
276
|
| 启动超时 | 手动执行 `magpie serve` 查看错误,检查监听端口 |
|
|
236
|
-
| 插件已加载但没有模型 | 在 Magpie 登录并启用服务,检查 `/v1/models`
|
|
277
|
+
| 插件已加载但没有模型 | 在 Magpie 登录并启用服务,检查 `/v1/models` 是否有模型;缺价不再隐藏模型 |
|
|
237
278
|
| 添加模型后没出现 | 等待下一个 30 秒刷新周期;初次初始化失败则重载 OpenCode |
|
|
279
|
+
| 改了 options 但分组没变 | 重启 OpenCode 服务;reload 不一定重新执行 setup |
|
|
280
|
+
| profile 报 Model unavailable | id 是否和 `groups` 一致(去前缀后的 id);该组是否已在 Magpie 里删掉 |
|
|
238
281
|
| 原有 Cursor 与新 Cursor 同时出现 | 两个插件可同时存在;Magpie 的内部 ID 是 `magpie-cursor` |
|
|
239
282
|
| 费用和实际账单不同 | 查看上面的价格优先级、迟到用量和阶梯价格边界 |
|
|
240
283
|
|
|
@@ -257,7 +300,7 @@ pnpm pack
|
|
|
257
300
|
|
|
258
301
|
维护者发布采用 tag 驱动:推送与 `package.json.version` 一致的 `v<version>` tag 后,GitHub Actions 自动检查、发布公网 npm 并创建 GitHub Release。正式版使用 `latest`,预发布版使用 `next`。完整步骤、认证要求与失败恢复见 [发布指南](docs/releasing.md)。
|
|
259
302
|
|
|
260
|
-
测试覆盖目录分组与替换、wire model ID、协议元数据、价格归一化与覆盖、未知价格、会话头、网关身份检查、自动启动与并发去重、未安装提示、刷新失败和清理。测试不消耗真实服务额度。
|
|
303
|
+
测试覆盖目录分组与替换、options 呈现(分桶、排序、非法配置)、wire model ID、协议元数据、价格归一化与覆盖、未知价格、会话头、网关身份检查、自动启动与并发去重、未安装提示、刷新失败和清理。测试不消耗真实服务额度。
|
|
261
304
|
|
|
262
305
|
只发布 `dist`、README 和许可证,**没有 runtime dependencies**。价格表变更时应更新相应测试,并在 release 说明价格来源与变化。
|
|
263
306
|
|
package/dist/index.d.mts
CHANGED
|
@@ -57,6 +57,8 @@ type ProviderEditor = {
|
|
|
57
57
|
}) => void;
|
|
58
58
|
};
|
|
59
59
|
type PluginContext = {
|
|
60
|
+
/** Object-form plugin options from opencode.jsonc. Absent when the package is listed as a string. */
|
|
61
|
+
options?: unknown;
|
|
60
62
|
provider?: {
|
|
61
63
|
transform: (callback: (editor: ProviderEditor) => void) => Promise<Registration>;
|
|
62
64
|
reload: () => Promise<void>;
|
package/dist/index.mjs
CHANGED
|
@@ -243,7 +243,7 @@ const lookupCatalogPrice = (catalog, maker, modelID) => {
|
|
|
243
243
|
const id = modelID.replace(/@\d+(?:k|m)$/i, "").toLowerCase();
|
|
244
244
|
const own = priceInProvider(catalog, maker, id);
|
|
245
245
|
if (own) return own;
|
|
246
|
-
const vendor = /^(?:gpt-|o[134](?:-|$))/.test(id) ? "openai" : id.startsWith("claude-") ? "anthropic" : id.startsWith("deepseek-") ? "deepseek" : id.startsWith("gemini-") ? "google" : id.startsWith("grok-") ? "xai" : void 0;
|
|
246
|
+
const vendor = /^(?:gpt-|o[134](?:-|$))/.test(id) ? "openai" : id.startsWith("claude-") ? "anthropic" : id.startsWith("deepseek-") ? "deepseek" : id.startsWith("gemini-") ? "google" : id.startsWith("grok-") ? "xai" : id.startsWith("muse-") ? "meta" : void 0;
|
|
247
247
|
return vendor && vendor !== maker ? priceInProvider(catalog, vendor, id) : void 0;
|
|
248
248
|
};
|
|
249
249
|
const priceInProvider = (catalog, maker, id) => {
|
|
@@ -298,6 +298,55 @@ const providerName = (model) => {
|
|
|
298
298
|
workbuddy: "WorkBuddy"
|
|
299
299
|
}[model.owned_by] ?? model.owned_by;
|
|
300
300
|
};
|
|
301
|
+
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
302
|
+
const invalidOptions = (path, reason) => /* @__PURE__ */ new Error(`Magpie 插件的 options 无效(opencode.jsonc → plugins → ${path}):${reason}。`);
|
|
303
|
+
const requiredString = (value, path) => {
|
|
304
|
+
if (typeof value !== "string" || value.length === 0) throw invalidOptions(path, "必须是非空字符串");
|
|
305
|
+
return value;
|
|
306
|
+
};
|
|
307
|
+
const parseGroups = (value) => {
|
|
308
|
+
if (value === void 0) return [];
|
|
309
|
+
if (!Array.isArray(value)) throw invalidOptions("options.groups", "必须是数组");
|
|
310
|
+
return value.map((entry, index) => {
|
|
311
|
+
const path = `options.groups[${index}]`;
|
|
312
|
+
if (!isRecord(entry)) throw invalidOptions(path, "必须是对象");
|
|
313
|
+
for (const key of Object.keys(entry)) if (key !== "prefix" && key !== "id" && key !== "name") throw invalidOptions(`${path}.${key}`, "不是已知字段");
|
|
314
|
+
return {
|
|
315
|
+
prefix: requiredString(entry.prefix, `${path}.prefix`),
|
|
316
|
+
id: requiredString(entry.id, `${path}.id`),
|
|
317
|
+
name: requiredString(entry.name, `${path}.name`)
|
|
318
|
+
};
|
|
319
|
+
});
|
|
320
|
+
};
|
|
321
|
+
const parseOrder = (value) => {
|
|
322
|
+
if (value === void 0) return /* @__PURE__ */ new Map();
|
|
323
|
+
if (!isRecord(value)) throw invalidOptions("options.order", "必须是对象");
|
|
324
|
+
const order = /* @__PURE__ */ new Map();
|
|
325
|
+
for (const [providerID, ids] of Object.entries(value)) {
|
|
326
|
+
const path = `options.order.${providerID}`;
|
|
327
|
+
if (!Array.isArray(ids)) throw invalidOptions(path, "必须是模型 id 数组");
|
|
328
|
+
const weights = /* @__PURE__ */ new Map();
|
|
329
|
+
for (const [index, id] of ids.entries()) {
|
|
330
|
+
const modelID = requiredString(id, `${path}[${index}]`);
|
|
331
|
+
if (!weights.has(modelID)) weights.set(modelID, 1e3 - index * 10);
|
|
332
|
+
}
|
|
333
|
+
order.set(providerID, weights);
|
|
334
|
+
}
|
|
335
|
+
return order;
|
|
336
|
+
};
|
|
337
|
+
/** Read presentation once. Invalid options fail startup instead of being skipped. */
|
|
338
|
+
const parsePresentation = (options) => {
|
|
339
|
+
if (options === void 0) return {
|
|
340
|
+
groups: [],
|
|
341
|
+
order: /* @__PURE__ */ new Map()
|
|
342
|
+
};
|
|
343
|
+
if (!isRecord(options)) throw invalidOptions("options", "必须是对象");
|
|
344
|
+
for (const key of Object.keys(options)) if (key !== "groups" && key !== "order") throw invalidOptions(`options.${key}`, "不是已知字段");
|
|
345
|
+
return {
|
|
346
|
+
groups: parseGroups(options.groups),
|
|
347
|
+
order: parseOrder(options.order)
|
|
348
|
+
};
|
|
349
|
+
};
|
|
301
350
|
const modelCost = (row, group, catalog) => {
|
|
302
351
|
const pricingID = row.id.slice(row.id.indexOf("/") + 1).replace(/@\d+(?:k|m)$/i, "");
|
|
303
352
|
const builtin = group ? void 0 : getModelPricing(pricingID, pricingProvider(row.owned_by));
|
|
@@ -322,12 +371,19 @@ const modelCost = (row, group, catalog) => {
|
|
|
322
371
|
const buildInventory = (rows, baseURL, options = {}) => {
|
|
323
372
|
const providers = /* @__PURE__ */ new Map();
|
|
324
373
|
const fastModelIDs = new Set(options.fastModelIDs);
|
|
374
|
+
const presentation = options.presentation ?? {
|
|
375
|
+
groups: [],
|
|
376
|
+
order: /* @__PURE__ */ new Map()
|
|
377
|
+
};
|
|
378
|
+
const buckets = [...presentation.groups].sort((left, right) => right.prefix.length - left.prefix.length);
|
|
325
379
|
for (const row of rows) if (row.service_tiers?.some((tier) => (typeof tier === "string" ? tier : tier.id) === "priority")) fastModelIDs.add(row.id);
|
|
326
380
|
rows.filter((row) => !row.kind && row.id.includes("/")).forEach((row) => {
|
|
327
381
|
const group = row.id.startsWith("group/");
|
|
328
|
-
const
|
|
382
|
+
const groupKey = group ? row.id.slice(6) : void 0;
|
|
383
|
+
const bucket = groupKey ? buckets.find((entry$1) => groupKey.length > entry$1.prefix.length && groupKey.startsWith(entry$1.prefix)) : void 0;
|
|
384
|
+
const owner = group ? bucket?.id ?? ROUTING_GROUP : row.owned_by;
|
|
329
385
|
const providerID = `magpie-${encodeURIComponent(owner)}`;
|
|
330
|
-
const modelID = row.id.slice(row.id.indexOf("/") + 1);
|
|
386
|
+
const modelID = bucket && groupKey ? groupKey.slice(bucket.prefix.length) : row.id.slice(row.id.indexOf("/") + 1);
|
|
331
387
|
const { cost } = modelCost(row, group, options.catalog);
|
|
332
388
|
const model = toClassicModelInfo(providerID, modelID, {
|
|
333
389
|
id: row.id,
|
|
@@ -340,8 +396,6 @@ const buildInventory = (rows, baseURL, options = {}) => {
|
|
|
340
396
|
modalities: row.modalities,
|
|
341
397
|
tool_call: true
|
|
342
398
|
});
|
|
343
|
-
if (!cost && !group) model.name += " (价格未知)";
|
|
344
|
-
model.enabled = !!cost || group;
|
|
345
399
|
const anthropic = row.native_endpoints?.includes("/v1/messages");
|
|
346
400
|
const responses = !anthropic && row.native_endpoints?.includes("/v1/responses");
|
|
347
401
|
model.package = anthropic ? "@opencode/ai/providers/anthropic-compatible" : responses ? "@opencode/ai/providers/openai-compatible/responses" : "@opencode/ai/providers/openai-compatible";
|
|
@@ -351,10 +405,11 @@ const buildInventory = (rows, baseURL, options = {}) => {
|
|
|
351
405
|
transport: "http"
|
|
352
406
|
};
|
|
353
407
|
model.variants = reasoningVariants(row, !!anthropic);
|
|
408
|
+
model.time = { released: presentation.order.get(providerID)?.get(model.id) ?? 0 };
|
|
354
409
|
const entry = providers.get(providerID) ?? {
|
|
355
410
|
info: {
|
|
356
411
|
id: providerID,
|
|
357
|
-
name: group ? "Magpie" : providerName(row),
|
|
412
|
+
name: bucket ? bucket.name : group ? "Magpie" : providerName(row),
|
|
358
413
|
activation: "enabled",
|
|
359
414
|
package: "@opencode/ai/providers/openai-compatible",
|
|
360
415
|
settings: {
|
|
@@ -385,7 +440,7 @@ const buildInventory = (rows, baseURL, options = {}) => {
|
|
|
385
440
|
...model,
|
|
386
441
|
id: `${model.id}-fast`,
|
|
387
442
|
cost: fastPrice ? normalizeModelCosts(fastPrice) : model.cost,
|
|
388
|
-
name: `${model.name
|
|
443
|
+
name: `${model.name} Fast`,
|
|
389
444
|
body: {
|
|
390
445
|
...model.body,
|
|
391
446
|
service_tier: "priority"
|
|
@@ -565,12 +620,14 @@ const plugin = {
|
|
|
565
620
|
};
|
|
566
621
|
const setupPlugin = async (ctx) => {
|
|
567
622
|
if (!ctx.provider) throw new Error("Magpie 插件需要 OpenCode V2 provider.transform。");
|
|
623
|
+
const presentation = parsePresentation(ctx.options);
|
|
568
624
|
const baseURL = await resolveBaseURL();
|
|
569
625
|
await ensureGateway(baseURL);
|
|
570
626
|
let catalog = await loadCatalogCache();
|
|
571
627
|
let inventory = buildInventory(await fetchModels(baseURL), baseURL, {
|
|
572
628
|
catalog: catalog.data,
|
|
573
|
-
fastModelIDs: await loadFastModelIDs()
|
|
629
|
+
fastModelIDs: await loadFastModelIDs(),
|
|
630
|
+
presentation
|
|
574
631
|
});
|
|
575
632
|
const { track, cleanup } = createTrackedSetup();
|
|
576
633
|
let disposed = false;
|
|
@@ -613,7 +670,8 @@ const setupPlugin = async (ctx) => {
|
|
|
613
670
|
catalog = await loadCatalogCache(hasUnpricedModel(inventory));
|
|
614
671
|
await apply(buildInventory(rows, baseURL, {
|
|
615
672
|
catalog: catalog.data,
|
|
616
|
-
fastModelIDs: await loadFastModelIDs()
|
|
673
|
+
fastModelIDs: await loadFastModelIDs(),
|
|
674
|
+
presentation
|
|
617
675
|
}));
|
|
618
676
|
} catch (error) {
|
|
619
677
|
console.warn("[magpie] 模型刷新失败,保留上次目录:", error instanceof Error ? error.message : String(error));
|
package/package.json
CHANGED