opencode-magpie-plugin 0.2.3 → 0.3.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 +54 -10
- package/dist/index.mjs +60 -21
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
**在 OpenCode V2 中动态使用 Magpie 的 Cursor、WorkBuddy 等服务,无需手写模型配置。**
|
|
8
8
|
|
|
9
|
-
An OpenCode V2 plugin for
|
|
9
|
+
An OpenCode V2 plugin for local or remote [Magpie](https://usemagpie.ai/) gateways. It discovers providers and models, supplies built-in token pricing, and starts an installed local gateway when needed. Zero runtime npm dependencies; no private registry or company-scoped packages required.
|
|
10
10
|
|
|
11
11
|
## 它解决什么问题
|
|
12
12
|
|
|
@@ -18,7 +18,7 @@ OpenCode V2
|
|
|
18
18
|
├─ 模型能力、上下文限制、reasoning 档位和价格
|
|
19
19
|
└─ 原生 Chat / Responses / Anthropic 请求
|
|
20
20
|
↓
|
|
21
|
-
|
|
21
|
+
本机或远程 Magpie 网关
|
|
22
22
|
↓
|
|
23
23
|
Cursor / WorkBuddy / 其他已配置服务
|
|
24
24
|
```
|
|
@@ -30,13 +30,13 @@ OpenCode V2
|
|
|
30
30
|
- Magpie 已安装但未运行时自动启动;macOS 上找到 app 内的可执行文件时优先启动 app,保留路由页;未安装时给出安装链接。
|
|
31
31
|
- 登录和服务凭据由 Magpie 管理,插件不需要复制上游 token。
|
|
32
32
|
|
|
33
|
-
这不是一个绕过 Magpie
|
|
33
|
+
这不是一个绕过 Magpie 的社区插件宿主:**推理请求仍经过所配置的 Magpie 网关**。
|
|
34
34
|
|
|
35
35
|
## 前置条件
|
|
36
36
|
|
|
37
37
|
1. 使用支持 `setup()`、`provider.transform` 和原生 provider runtime 的 **OpenCode V2**。不支持 V1。
|
|
38
|
-
2. 从 [Magpie 官网](https://usemagpie.ai/zh/) 安装 Magpie
|
|
39
|
-
3.
|
|
38
|
+
2. 从 [Magpie 官网](https://usemagpie.ai/zh/) 安装 Magpie,并在其中登录需要的服务、启用模型;使用远程网关时,本机无需安装。
|
|
39
|
+
3. 网关对 OpenCode 服务进程可访问。本机默认地址 `http://127.0.0.1:3425`;远程连接见下方配置。
|
|
40
40
|
|
|
41
41
|
包的 Node.js 要求为 **22 或更高**。macOS 上已验证插件加载与 Cursor / WorkBuddy 目录注册;Windows 和 Linux 的实际网关联调尚未验证。
|
|
42
42
|
|
|
@@ -55,7 +55,7 @@ OpenCode V2
|
|
|
55
55
|
|
|
56
56
|
```jsonc
|
|
57
57
|
{
|
|
58
|
-
"plugins": ["opencode-magpie-plugin@0.
|
|
58
|
+
"plugins": ["opencode-magpie-plugin@0.3.0"]
|
|
59
59
|
}
|
|
60
60
|
```
|
|
61
61
|
|
|
@@ -119,13 +119,13 @@ Routing group(例如 `group/auto`)会跨多个服务路由,因此不按 `o
|
|
|
119
119
|
|
|
120
120
|
## 自动发现与请求行为
|
|
121
121
|
|
|
122
|
-
|
|
122
|
+
本机模式先检查网关根路径的 `name: "magpie"`,再请求 `/v1/models`。远程模式直接请求配置的 API baseURL 下的 `models`,不探测站点根路径,以兼容只暴露 API 的反向代理。请求携带 `X-Magpie-Prices: 1` 读取可用的网关价格扩展。
|
|
123
123
|
|
|
124
124
|
目录映射保留模型的上下文窗口、最大输出、输入输出模态和 reasoning 档位。仅发现聊天目录;重复的无 provider 前缀别名、图片生成或其他非聊天专用模型不会注册为聊天模型。
|
|
125
125
|
|
|
126
126
|
思考强度读取新版 `reasoning_levels`,并兼容已安装版本常见的 `supported_reasoning_levels`(`{ effort }` 对象或字符串列表)。只有网关实际声明的档位才会出现,可在 OpenCode 的模型变体控件中选择,或使用 `provider/model#high` 这样的引用。`default` 保留服务默认行为,不额外生成档位;网关未提供档位时不会凭模型名称猜测。
|
|
127
127
|
|
|
128
|
-
支持 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
|
|
128
|
+
支持 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` 模型优先,刷新时同步增删。
|
|
129
129
|
|
|
130
130
|
Fast 模型优先采用内置表中明确登记的 Fast 费率及上下文档位;没有独立 Fast 费率时仍沿用原模型参考价,不代表 Fast 实际费率。缺少原模型报价时仍按未知价格处理。
|
|
131
131
|
|
|
@@ -147,7 +147,45 @@ Fast 模型优先采用内置表中明确登记的 Fast 费率及上下文档位
|
|
|
147
147
|
|
|
148
148
|
## 网关地址与启动
|
|
149
149
|
|
|
150
|
-
|
|
150
|
+
### 远程网关与反向代理
|
|
151
|
+
|
|
152
|
+
在插件 `options.remote` 中配置 **API baseURL** 和 **Magpie Gateway Key**:
|
|
153
|
+
|
|
154
|
+
```jsonc
|
|
155
|
+
{
|
|
156
|
+
"plugins": [
|
|
157
|
+
{
|
|
158
|
+
"package": "opencode-magpie-plugin",
|
|
159
|
+
"options": {
|
|
160
|
+
"remote": {
|
|
161
|
+
"baseURL": "https://magpie.example.com/magpie/v1",
|
|
162
|
+
"token": "{env:MAGPIE_REMOTE_TOKEN}"
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
]
|
|
167
|
+
}
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
`token` 是远端 Magpie 的 **Gateway Key**(网关调用密钥),不是管理页面链接里的 `?k=` 登录密钥,也不是上游服务的 token。可直接填写密钥,或像示例一样由 OpenCode 从其服务进程环境变量展开。`remote` 可与 `groups`、`order` 同时配置。
|
|
171
|
+
|
|
172
|
+
OpenCode V2 使用 `plugins` 数组中的 `{ "package": "…", "options": { … } }` 对象传入配置;`[packageName, options]` 是旧版 tuple 写法,V2 配置请使用上述对象形式。
|
|
173
|
+
|
|
174
|
+
`baseURL` 必须包含实际使用的完整 API 前缀;插件只去掉末尾的 `/`,**不会补 `/v1`、截掉 path 或推测 rewrite 规则**:
|
|
175
|
+
|
|
176
|
+
| 配置 | 请求模型目录 | Chat 请求 |
|
|
177
|
+
| --- | --- | --- |
|
|
178
|
+
| `https://host/v1` | `https://host/v1/models` | `https://host/v1/chat/completions` |
|
|
179
|
+
| `https://host/magpie/v1/` | `https://host/magpie/v1/models` | `https://host/magpie/v1/chat/completions` |
|
|
180
|
+
| `https://host/custom-api` | `https://host/custom-api/models` | `https://host/custom-api/chat/completions` |
|
|
181
|
+
|
|
182
|
+
Responses 和 Anthropic 请求同样在该前缀下使用 `responses`、`messages`。例如 NGINX 把 `/magpie/` 转发到远端网关根路径时,填写 `https://magpie.example.com/magpie/v1`;外部的 `/magpie/v1/models` 对应上游 `/v1/models`。如果把 `/custom-api/` rewrite 到远端网关 `/v1/`,则填写 `https://host/custom-api`。地址指向 **Magpie API 网关**(默认端口 3425),而不是 Web 管理界面(默认端口 3430)。支持 HTTP/HTTPS;不接受 URL 内的用户名、密码、查询参数或片段。
|
|
183
|
+
|
|
184
|
+
模型发现、30 秒刷新和推理都使用同一个远端与 token。远程模式不读取本机 Magpie 地址或 Fast 配置,也不会自动启动本机网关、失败后回退到本机。首次发现失败会报错;后续刷新失败保留最近成功的目录。目录请求不跟随重定向,反代应直接处理所配置的地址。
|
|
185
|
+
|
|
186
|
+
### 本机网关
|
|
187
|
+
|
|
188
|
+
未配置 `remote` 时,地址优先级:
|
|
151
189
|
|
|
152
190
|
1. OpenCode 服务进程中的 `MAGPIE_ADDR`。
|
|
153
191
|
2. `$XDG_CONFIG_HOME/magpie/settings.json` 的 `port`。
|
|
@@ -160,7 +198,7 @@ Fast 模型优先采用内置表中明确登记的 Fast 费率及上下文档位
|
|
|
160
198
|
MAGPIE_ADDR=127.0.0.1:3426 opencode --standalone
|
|
161
199
|
```
|
|
162
200
|
|
|
163
|
-
`0.0.0.0`、`::`
|
|
201
|
+
`0.0.0.0`、`::` 监听地址会转换为本机访问地址。`MAGPIE_ADDR` 只用于本机网关,远程服务器使用上面的 `options.remote`。共享服务模式下,在终端临时设置变量不一定会改变已经启动的 OpenCode 服务环境。
|
|
164
202
|
|
|
165
203
|
网关不在线时,插件在 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。
|
|
166
204
|
|
|
@@ -183,6 +221,12 @@ MAGPIE_ADDR=127.0.0.1:3426 opencode --standalone
|
|
|
183
221
|
|
|
184
222
|
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。
|
|
185
223
|
|
|
224
|
+
### 0.3.0
|
|
225
|
+
|
|
226
|
+
- 新增 `options.remote.baseURL` 与 `options.remote.token`,支持远端 Magpie 网关及带路径前缀的反向代理。
|
|
227
|
+
- API baseURL 保留完整路径,不自动追加 `/v1`;模型发现、刷新和三种原生推理协议共用远端配置与 Gateway Key。
|
|
228
|
+
- 远程模式不启动本机 Magpie,也不读取本机插件 Fast 能力;刷新失败仍保留最近成功的目录。
|
|
229
|
+
|
|
186
230
|
### 0.2.0
|
|
187
231
|
|
|
188
232
|
- 目录呈现改为插件 options:路由组的分桶、显示名和排序写在 `opencode.jsonc`,升级不再冲掉使用者自己的摆法。默认为空,与之前的目录一致。网关、价格表和 Fast 计价不变。
|
package/dist/index.mjs
CHANGED
|
@@ -358,7 +358,7 @@ const parsePresentation = (options) => {
|
|
|
358
358
|
order: /* @__PURE__ */ new Map()
|
|
359
359
|
};
|
|
360
360
|
if (!isRecord(options)) throw invalidOptions("options", "必须是对象");
|
|
361
|
-
for (const key of Object.keys(options)) if (key !== "groups" && key !== "order") throw invalidOptions(`options.${key}`, "不是已知字段");
|
|
361
|
+
for (const key of Object.keys(options)) if (key !== "groups" && key !== "order" && key !== "remote") throw invalidOptions(`options.${key}`, "不是已知字段");
|
|
362
362
|
return {
|
|
363
363
|
groups: parseGroups(options.groups),
|
|
364
364
|
order: parseOrder(options.order)
|
|
@@ -387,6 +387,7 @@ const modelCost = (row, group, catalog) => {
|
|
|
387
387
|
};
|
|
388
388
|
const buildInventory = (rows, baseURL, options = {}) => {
|
|
389
389
|
const providers = /* @__PURE__ */ new Map();
|
|
390
|
+
const apiKey = options.apiKey ?? "magpie";
|
|
390
391
|
const fastModelIDs = new Set(options.fastModelIDs);
|
|
391
392
|
const presentation = options.presentation ?? {
|
|
392
393
|
groups: [],
|
|
@@ -417,8 +418,8 @@ const buildInventory = (rows, baseURL, options = {}) => {
|
|
|
417
418
|
const responses = !anthropic && row.native_endpoints?.includes("/v1/responses");
|
|
418
419
|
model.package = anthropic ? "@opencode/ai/providers/anthropic-compatible" : responses ? "@opencode/ai/providers/openai-compatible/responses" : "@opencode/ai/providers/openai-compatible";
|
|
419
420
|
model.settings = {
|
|
420
|
-
baseURL
|
|
421
|
-
apiKey
|
|
421
|
+
baseURL,
|
|
422
|
+
apiKey,
|
|
422
423
|
transport: "http"
|
|
423
424
|
};
|
|
424
425
|
model.variants = reasoningVariants(row, !!anthropic);
|
|
@@ -430,12 +431,12 @@ const buildInventory = (rows, baseURL, options = {}) => {
|
|
|
430
431
|
activation: "enabled",
|
|
431
432
|
package: "@opencode/ai/providers/openai-compatible",
|
|
432
433
|
settings: {
|
|
433
|
-
baseURL
|
|
434
|
-
apiKey
|
|
434
|
+
baseURL,
|
|
435
|
+
apiKey,
|
|
435
436
|
transport: "http"
|
|
436
437
|
},
|
|
437
438
|
headers: {
|
|
438
|
-
Authorization:
|
|
439
|
+
Authorization: `Bearer ${apiKey}`,
|
|
439
440
|
"X-Magpie-Agent": "opencode"
|
|
440
441
|
}
|
|
441
442
|
},
|
|
@@ -491,6 +492,38 @@ const loadFastModelIDs = async () => {
|
|
|
491
492
|
//#endregion
|
|
492
493
|
//#region src/gateway.ts
|
|
493
494
|
const INSTALL_MESSAGE = "未安装 Magpie,请先从 https://usemagpie.ai/zh/ 安装并登录所需服务,再使用此插件。";
|
|
495
|
+
/** Explicit remote URLs are API bases, including any reverse-proxy prefix and version path. */
|
|
496
|
+
const resolveConnection = async (options) => {
|
|
497
|
+
const remote = options?.remote;
|
|
498
|
+
if (remote === void 0) {
|
|
499
|
+
const localURL = await resolveBaseURL();
|
|
500
|
+
return {
|
|
501
|
+
baseURL: `${localURL}/v1`,
|
|
502
|
+
token: "magpie",
|
|
503
|
+
remote: false,
|
|
504
|
+
ensure: () => ensureGateway(localURL)
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
const invalid = (field, reason) => /* @__PURE__ */ new Error(`Magpie 插件的 options.remote${field} 无效:${reason}。`);
|
|
508
|
+
if (typeof remote !== "object" || remote === null || Array.isArray(remote)) throw invalid("", "必须是包含 baseURL 和 token 的对象");
|
|
509
|
+
const value = remote;
|
|
510
|
+
if (Object.keys(value).some((key) => key !== "baseURL" && key !== "token")) throw invalid("", "仅支持 baseURL 和 token 字段");
|
|
511
|
+
let url;
|
|
512
|
+
try {
|
|
513
|
+
if (typeof value.baseURL !== "string" || value.baseURL.trim() !== value.baseURL) throw new Error();
|
|
514
|
+
url = new URL(value.baseURL);
|
|
515
|
+
if (!["http:", "https:"].includes(url.protocol) || url.username || url.password || url.search || url.hash) throw new Error();
|
|
516
|
+
} catch {
|
|
517
|
+
throw invalid(".baseURL", "必须是完整 HTTP(S) API 基地址,不能包含用户名、密码、查询参数或片段");
|
|
518
|
+
}
|
|
519
|
+
if (typeof value.token !== "string" || !/^[\x21-\x7e]+$/.test(value.token)) throw invalid(".token", "必须是非空 token,不能包含空格或控制字符");
|
|
520
|
+
return {
|
|
521
|
+
baseURL: url.href.replace(/\/+$/, ""),
|
|
522
|
+
token: value.token,
|
|
523
|
+
remote: true,
|
|
524
|
+
ensure: async () => {}
|
|
525
|
+
};
|
|
526
|
+
};
|
|
494
527
|
const resolveBaseURL = async () => {
|
|
495
528
|
if (process.env.MAGPIE_ADDR) {
|
|
496
529
|
const addr = process.env.MAGPIE_ADDR.replace(/^:/, "127.0.0.1:");
|
|
@@ -615,17 +648,19 @@ const ensureGateway = async (baseURL) => {
|
|
|
615
648
|
starting.delete(baseURL);
|
|
616
649
|
}
|
|
617
650
|
};
|
|
618
|
-
const fetchModels = async (baseURL) => {
|
|
619
|
-
const response = await fetch(`${baseURL}/
|
|
651
|
+
const fetchModels = async (baseURL, token = "magpie") => {
|
|
652
|
+
const response = await fetch(`${baseURL}/models`, {
|
|
620
653
|
headers: {
|
|
621
|
-
Authorization:
|
|
622
|
-
"X-Magpie-Prices": "1"
|
|
654
|
+
Authorization: `Bearer ${token}`,
|
|
655
|
+
"X-Magpie-Prices": "1",
|
|
656
|
+
"X-Magpie-Agent": "opencode"
|
|
623
657
|
},
|
|
658
|
+
redirect: "error",
|
|
624
659
|
signal: AbortSignal.timeout(15e3)
|
|
625
660
|
});
|
|
626
661
|
if (!response.ok) throw new Error(`Magpie 模型发现失败:HTTP ${response.status}`);
|
|
627
|
-
const body = await response.json();
|
|
628
|
-
if (!Array.isArray(body
|
|
662
|
+
const body = await response.json().catch(() => void 0);
|
|
663
|
+
if (!Array.isArray(body?.data) || body.data.some((model) => !model || typeof model.id !== "string" || typeof model.owned_by !== "string")) throw new Error("Magpie models 接口返回了无效的模型目录。");
|
|
629
664
|
return body.data;
|
|
630
665
|
};
|
|
631
666
|
|
|
@@ -638,12 +673,15 @@ const plugin = {
|
|
|
638
673
|
const setupPlugin = async (ctx) => {
|
|
639
674
|
if (!ctx.provider) throw new Error("Magpie 插件需要 OpenCode V2 provider.transform。");
|
|
640
675
|
const presentation = parsePresentation(ctx.options);
|
|
641
|
-
const
|
|
642
|
-
|
|
676
|
+
const connection = await resolveConnection(ctx.options);
|
|
677
|
+
const { baseURL, token } = connection;
|
|
678
|
+
const apiURL = new URL(baseURL);
|
|
679
|
+
await connection.ensure();
|
|
643
680
|
let catalog = await loadCatalogCache();
|
|
644
|
-
let inventory = buildInventory(await fetchModels(baseURL), baseURL, {
|
|
681
|
+
let inventory = buildInventory(await fetchModels(baseURL, token), baseURL, {
|
|
682
|
+
apiKey: token,
|
|
645
683
|
catalog: catalog.data,
|
|
646
|
-
fastModelIDs: await loadFastModelIDs(),
|
|
684
|
+
fastModelIDs: connection.remote ? void 0 : await loadFastModelIDs(),
|
|
647
685
|
presentation
|
|
648
686
|
});
|
|
649
687
|
const { track, cleanup } = createTrackedSetup();
|
|
@@ -663,8 +701,8 @@ const setupPlugin = async (ctx) => {
|
|
|
663
701
|
]) await track(ctx.session.hook(name, normalizeOpenAICallIDs, { providerID: "openai" }));
|
|
664
702
|
await track(ctx.session.hook("http.request", async (event) => {
|
|
665
703
|
const url = new URL(event.request.url);
|
|
666
|
-
if (url.origin !==
|
|
667
|
-
await
|
|
704
|
+
if (url.origin !== apiURL.origin || !url.pathname.startsWith(`${apiURL.pathname.replace(/\/$/, "")}/`)) return;
|
|
705
|
+
await connection.ensure();
|
|
668
706
|
event.request.headers.set("X-Magpie-Session", event.sessionID);
|
|
669
707
|
event.request.headers.set("X-Magpie-Agent", "opencode");
|
|
670
708
|
}));
|
|
@@ -688,12 +726,13 @@ const setupPlugin = async (ctx) => {
|
|
|
688
726
|
if (disposed || refreshing) return;
|
|
689
727
|
refreshing = true;
|
|
690
728
|
try {
|
|
691
|
-
await
|
|
692
|
-
const rows = await fetchModels(baseURL);
|
|
729
|
+
await connection.ensure();
|
|
730
|
+
const rows = await fetchModels(baseURL, token);
|
|
693
731
|
catalog = await loadCatalogCache(hasUnpricedModel(inventory));
|
|
694
732
|
await apply(buildInventory(rows, baseURL, {
|
|
733
|
+
apiKey: token,
|
|
695
734
|
catalog: catalog.data,
|
|
696
|
-
fastModelIDs: await loadFastModelIDs(),
|
|
735
|
+
fastModelIDs: connection.remote ? void 0 : await loadFastModelIDs(),
|
|
697
736
|
presentation
|
|
698
737
|
}));
|
|
699
738
|
} catch (error) {
|
package/package.json
CHANGED