opencode-magpie-plugin 0.1.9 → 0.2.1

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 CHANGED
@@ -25,7 +25,7 @@ OpenCode V2
25
25
 
26
26
  - `opencode.jsonc` 只加载插件,不维护 `providers.models`。
27
27
  - 启动时发现目录,每 30 秒刷新;服务新增、模型下线都能反映到 OpenCode。
28
- - Provider 显示 Magpie 中的分组名称,而不是统一挤在一个 “Magpie” 下;路由分组统一归入 Magpie。
28
+ - Provider 显示 Magpie 中的分组名称,而不是统一挤在一个 “Magpie” 下。路由分组默认仍归入 Magpie;要换呈现方式,在插件 `options` 里声明,不要改构建产物。
29
29
  - 内置独立价格表和模型名归一化逻辑,支持输入、输出、缓存读取与写入价格。
30
30
  - Magpie 已安装但未运行时自动启动;macOS 上找到 app 内的可执行文件时优先启动 app,保留路由页;未安装时给出安装链接。
31
31
  - 登录和服务凭据由 Magpie 管理,插件不需要复制上游 token。
@@ -81,7 +81,41 @@ opencode service restart
81
81
 
82
82
  示例模型仅说明格式,实际列表取决于 Magpie 当前账号与配置。Magpie 自定义 provider ID 会原样映射为带 `magpie-` 前缀的 ID。
83
83
 
84
- Routing group(例如 `group/auto`)会跨多个服务路由,因此不按 `owned_by` 或首个候选服务拆成 provider,而是统一归到 **Magpie** 分组下。发送时仍保留 `group/auto`,价格只用 Magpie 给出的价格,不按成员猜测。
84
+ Routing group(例如 `group/auto`)会跨多个服务路由,因此不按 `owned_by` 或首个候选服务拆成 provider。没有插件 options 时,它们统一归到 **Magpie** 分组下。发送时仍保留网关原始 id(`group/auto`),价格只用 Magpie 给出的价格,不按成员猜测。
85
+
86
+ ### 目录呈现
87
+
88
+ 新版 Auto Group 的 `auto/<model>` 也按路由组处理,默认放在 `magpie-magpie`,选择器 ID 为 `auto-<model>`,请求仍发送原始 `auto/<model>`。它按 `auto-<model>` 匹配下述 `groups.prefix`,因此可用 `auto-` 单独分桶;模型名中的点号保持原样。旧的 `group/` 格式继续支持,普通厂商模型不会被自动组覆盖。自动组及其 Fast 变体仅采用 Magpie 报价。
89
+
90
+ 路由组的成员由 Magpie 维护;这个插件只决定 OpenCode 选择器里怎么摆。分组、显示名和排序写在 `opencode.jsonc` 的插件 `options` 里,升级插件包之后仍在,不用再改 `dist`。空 options(或仍用字符串引用插件)和不声明时完全一样:所有 `group/` 行都在 `magpie-magpie`。
91
+
92
+ ```jsonc
93
+ {
94
+ "plugins": [
95
+ {
96
+ "package": "opencode-magpie-plugin",
97
+ "options": {
98
+ "groups": [
99
+ { "prefix": "adams-", "id": "adams", "name": "Adams" },
100
+ { "prefix": "combo-", "id": "combo", "name": "Combo" }
101
+ ],
102
+ "order": {
103
+ "magpie-adams": ["deepseek-v4.1-flash"],
104
+ "magpie-cursor": ["grok-4.7@500k"]
105
+ }
106
+ }
107
+ }
108
+ ]
109
+ }
110
+ ```
111
+
112
+ 上面只是写法示例,不是插件默认值。语义:
113
+
114
+ - 只对 id 以 `group/` 或 `auto/` 开头的行分桶。`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` 影响。
115
+ - OpenCode 同 provider 内按 `time.released` 倒序排。`order` 的 key 是最终 provider id,值是最终模型 id(已去前缀),越靠前越靠前。名单里没有的保持 0,退化为字母序。`-fast` 克隆继承父模型的权重并紧跟在后面;不要给它单独写一条,除非那个 `-fast` id 自己出现在 `order` 里。provider 之间的顺序控制不了。
116
+ - 插件不隐藏模型。网关列出的都会出现,没标价的也一样。不想在 OpenCode 里看到某个组,在 Magpie 里处理:不用了就 `magpie group rm <id>`,只对 OpenCode 收窄就用 `magpie visible opencode …`。组的成员是厂商模型时,删掉一个组不会影响把同一批模型当成员的其他组。
117
+
118
+ `options` 非法会在启动时失败,并写明出错字段的路径,不会静默忽略。改 options 后要重启 OpenCode 服务(`opencode service restart`)。热重载不一定会重新执行 `setup`,目录的 30 秒刷新也仍用启动时解析的那一份。
85
119
 
86
120
  ## 自动发现与请求行为
87
121
 
@@ -147,6 +181,10 @@ MAGPIE_ADDR=127.0.0.1:3426 opencode --standalone
147
181
 
148
182
  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
183
 
184
+ ### 0.2.0
185
+
186
+ - 目录呈现改为插件 options:路由组的分桶、显示名和排序写在 `opencode.jsonc`,升级不再冲掉使用者自己的摆法。默认为空,与之前的目录一致。网关、价格表和 Fast 计价不变。
187
+
150
188
  ### 0.1.9
151
189
 
152
190
  - 缺价模型不再从选择器里拿掉:仍然列出、仍然可选,只是不填 `cost`。空费用不是 0 元,也不代表实际免费。
@@ -238,8 +276,10 @@ pnpm check
238
276
  | `/plugins` 没有 `magpie.providers` | 全局配置是否被当前服务读取;file 模式是否填写 `dist` 目录并已构建;查看 OpenCode 日志 |
239
277
  | 提示未安装 Magpie | 安装 [Magpie](https://usemagpie.ai/zh/),确认服务进程 PATH 可以找到它 |
240
278
  | 启动超时 | 手动执行 `magpie serve` 查看错误,检查监听端口 |
241
- | 插件已加载但没有模型 | 在 Magpie 登录并启用服务,检查 `/v1/models` 是否有模型及有效价格 |
279
+ | 插件已加载但没有模型 | 在 Magpie 登录并启用服务,检查 `/v1/models` 是否有模型;缺价不再隐藏模型 |
242
280
  | 添加模型后没出现 | 等待下一个 30 秒刷新周期;初次初始化失败则重载 OpenCode |
281
+ | 改了 options 但分组没变 | 重启 OpenCode 服务;reload 不一定重新执行 setup |
282
+ | profile 报 Model unavailable | id 是否和 `groups` 一致(去前缀后的 id);该组是否已在 Magpie 里删掉 |
243
283
  | 原有 Cursor 与新 Cursor 同时出现 | 两个插件可同时存在;Magpie 的内部 ID 是 `magpie-cursor` |
244
284
  | 费用和实际账单不同 | 查看上面的价格优先级、迟到用量和阶梯价格边界 |
245
285
 
@@ -262,7 +302,7 @@ pnpm pack
262
302
 
263
303
  维护者发布采用 tag 驱动:推送与 `package.json.version` 一致的 `v<version>` tag 后,GitHub Actions 自动检查、发布公网 npm 并创建 GitHub Release。正式版使用 `latest`,预发布版使用 `next`。完整步骤、认证要求与失败恢复见 [发布指南](docs/releasing.md)。
264
304
 
265
- 测试覆盖目录分组与替换、wire model ID、协议元数据、价格归一化与覆盖、未知价格、会话头、网关身份检查、自动启动与并发去重、未安装提示、刷新失败和清理。测试不消耗真实服务额度。
305
+ 测试覆盖目录分组与替换、options 呈现(分桶、排序、非法配置)、wire model ID、协议元数据、价格归一化与覆盖、未知价格、会话头、网关身份检查、自动启动与并发去重、未安装提示、刷新失败和清理。测试不消耗真实服务额度。
266
306
 
267
307
  只发布 `dist`、README 和许可证,**没有 runtime dependencies**。价格表变更时应更新相应测试,并在 release 说明价格来源与变化。
268
308
 
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
@@ -267,6 +267,10 @@ const priceInProvider = (catalog, maker, id) => {
267
267
  const hasUnpricedModel = (inventory) => inventory.some((entry) => entry.models.some((model) => model.cost.length === 0));
268
268
  /** Routing groups route across providers, so they belong to Magpie itself rather than to a vendor. */
269
269
  const ROUTING_GROUP = "magpie";
270
+ const routingGroupKey = (id) => {
271
+ if (id.startsWith("group/")) return id.slice(6);
272
+ if (id.startsWith("auto/")) return `auto-${id.slice(5)}`;
273
+ };
270
274
  const reasoningVariants = (row, anthropic) => {
271
275
  if (row.reasoning === false) return [];
272
276
  const levels = row.reasoning_levels ?? row.supported_reasoning_levels?.map((level) => typeof level === "string" ? level : level.effort) ?? [];
@@ -298,6 +302,55 @@ const providerName = (model) => {
298
302
  workbuddy: "WorkBuddy"
299
303
  }[model.owned_by] ?? model.owned_by;
300
304
  };
305
+ const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
306
+ const invalidOptions = (path, reason) => /* @__PURE__ */ new Error(`Magpie 插件的 options 无效(opencode.jsonc → plugins → ${path}):${reason}。`);
307
+ const requiredString = (value, path) => {
308
+ if (typeof value !== "string" || value.length === 0) throw invalidOptions(path, "必须是非空字符串");
309
+ return value;
310
+ };
311
+ const parseGroups = (value) => {
312
+ if (value === void 0) return [];
313
+ if (!Array.isArray(value)) throw invalidOptions("options.groups", "必须是数组");
314
+ return value.map((entry, index) => {
315
+ const path = `options.groups[${index}]`;
316
+ if (!isRecord(entry)) throw invalidOptions(path, "必须是对象");
317
+ for (const key of Object.keys(entry)) if (key !== "prefix" && key !== "id" && key !== "name") throw invalidOptions(`${path}.${key}`, "不是已知字段");
318
+ return {
319
+ prefix: requiredString(entry.prefix, `${path}.prefix`),
320
+ id: requiredString(entry.id, `${path}.id`),
321
+ name: requiredString(entry.name, `${path}.name`)
322
+ };
323
+ });
324
+ };
325
+ const parseOrder = (value) => {
326
+ if (value === void 0) return /* @__PURE__ */ new Map();
327
+ if (!isRecord(value)) throw invalidOptions("options.order", "必须是对象");
328
+ const order = /* @__PURE__ */ new Map();
329
+ for (const [providerID, ids] of Object.entries(value)) {
330
+ const path = `options.order.${providerID}`;
331
+ if (!Array.isArray(ids)) throw invalidOptions(path, "必须是模型 id 数组");
332
+ const weights = /* @__PURE__ */ new Map();
333
+ for (const [index, id] of ids.entries()) {
334
+ const modelID = requiredString(id, `${path}[${index}]`);
335
+ if (!weights.has(modelID)) weights.set(modelID, 1e3 - index * 10);
336
+ }
337
+ order.set(providerID, weights);
338
+ }
339
+ return order;
340
+ };
341
+ /** Read presentation once. Invalid options fail startup instead of being skipped. */
342
+ const parsePresentation = (options) => {
343
+ if (options === void 0) return {
344
+ groups: [],
345
+ order: /* @__PURE__ */ new Map()
346
+ };
347
+ if (!isRecord(options)) throw invalidOptions("options", "必须是对象");
348
+ for (const key of Object.keys(options)) if (key !== "groups" && key !== "order") throw invalidOptions(`options.${key}`, "不是已知字段");
349
+ return {
350
+ groups: parseGroups(options.groups),
351
+ order: parseOrder(options.order)
352
+ };
353
+ };
301
354
  const modelCost = (row, group, catalog) => {
302
355
  const pricingID = row.id.slice(row.id.indexOf("/") + 1).replace(/@\d+(?:k|m)$/i, "");
303
356
  const builtin = group ? void 0 : getModelPricing(pricingID, pricingProvider(row.owned_by));
@@ -322,12 +375,19 @@ const modelCost = (row, group, catalog) => {
322
375
  const buildInventory = (rows, baseURL, options = {}) => {
323
376
  const providers = /* @__PURE__ */ new Map();
324
377
  const fastModelIDs = new Set(options.fastModelIDs);
378
+ const presentation = options.presentation ?? {
379
+ groups: [],
380
+ order: /* @__PURE__ */ new Map()
381
+ };
382
+ const buckets = [...presentation.groups].sort((left, right) => right.prefix.length - left.prefix.length);
325
383
  for (const row of rows) if (row.service_tiers?.some((tier) => (typeof tier === "string" ? tier : tier.id) === "priority")) fastModelIDs.add(row.id);
326
384
  rows.filter((row) => !row.kind && row.id.includes("/")).forEach((row) => {
327
- const group = row.id.startsWith("group/");
328
- const owner = group ? ROUTING_GROUP : row.owned_by;
385
+ const groupKey = routingGroupKey(row.id);
386
+ const group = groupKey !== void 0;
387
+ const bucket = groupKey ? buckets.find((entry$1) => groupKey.length > entry$1.prefix.length && groupKey.startsWith(entry$1.prefix)) : void 0;
388
+ const owner = group ? bucket?.id ?? ROUTING_GROUP : row.owned_by;
329
389
  const providerID = `magpie-${encodeURIComponent(owner)}`;
330
- const modelID = row.id.slice(row.id.indexOf("/") + 1);
390
+ const modelID = bucket && groupKey ? groupKey.slice(bucket.prefix.length) : groupKey ?? row.id.slice(row.id.indexOf("/") + 1);
331
391
  const { cost } = modelCost(row, group, options.catalog);
332
392
  const model = toClassicModelInfo(providerID, modelID, {
333
393
  id: row.id,
@@ -349,10 +409,11 @@ const buildInventory = (rows, baseURL, options = {}) => {
349
409
  transport: "http"
350
410
  };
351
411
  model.variants = reasoningVariants(row, !!anthropic);
412
+ model.time = { released: presentation.order.get(providerID)?.get(model.id) ?? 0 };
352
413
  const entry = providers.get(providerID) ?? {
353
414
  info: {
354
415
  id: providerID,
355
- name: group ? "Magpie" : providerName(row),
416
+ name: bucket ? bucket.name : group ? "Magpie" : providerName(row),
356
417
  activation: "enabled",
357
418
  package: "@opencode/ai/providers/openai-compatible",
358
419
  settings: {
@@ -378,7 +439,7 @@ const buildInventory = (rows, baseURL, options = {}) => {
378
439
  if (!fastModelIDs.has(model.modelID) || /-fast(?:@\d+[km])?$/i.test(model.id) || ids.has(`${model.id}-fast`)) return [model];
379
440
  const row = rows.find((row$1) => row$1.id === model.modelID);
380
441
  const fastKey = `${model.id.replace(/@\d+(?:k|m)$/i, "")}-fast`;
381
- const fastPrice = row && !row.id.startsWith("group/") && resolveModelPricingKey(fastKey)?.endsWith("-fast") ? getModelPricing(fastKey, pricingProvider(row.owned_by)) : void 0;
442
+ const fastPrice = row && routingGroupKey(row.id) === void 0 && resolveModelPricingKey(fastKey)?.endsWith("-fast") ? getModelPricing(fastKey, pricingProvider(row.owned_by)) : void 0;
382
443
  return [model, {
383
444
  ...model,
384
445
  id: `${model.id}-fast`,
@@ -563,12 +624,14 @@ const plugin = {
563
624
  };
564
625
  const setupPlugin = async (ctx) => {
565
626
  if (!ctx.provider) throw new Error("Magpie 插件需要 OpenCode V2 provider.transform。");
627
+ const presentation = parsePresentation(ctx.options);
566
628
  const baseURL = await resolveBaseURL();
567
629
  await ensureGateway(baseURL);
568
630
  let catalog = await loadCatalogCache();
569
631
  let inventory = buildInventory(await fetchModels(baseURL), baseURL, {
570
632
  catalog: catalog.data,
571
- fastModelIDs: await loadFastModelIDs()
633
+ fastModelIDs: await loadFastModelIDs(),
634
+ presentation
572
635
  });
573
636
  const { track, cleanup } = createTrackedSetup();
574
637
  let disposed = false;
@@ -611,7 +674,8 @@ const setupPlugin = async (ctx) => {
611
674
  catalog = await loadCatalogCache(hasUnpricedModel(inventory));
612
675
  await apply(buildInventory(rows, baseURL, {
613
676
  catalog: catalog.data,
614
- fastModelIDs: await loadFastModelIDs()
677
+ fastModelIDs: await loadFastModelIDs(),
678
+ presentation
615
679
  }));
616
680
  } catch (error) {
617
681
  console.warn("[magpie] 模型刷新失败,保留上次目录:", error instanceof Error ? error.message : String(error));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-magpie-plugin",
3
- "version": "0.1.9",
3
+ "version": "0.2.1",
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",