opencode-magpie-plugin 0.5.1 → 0.6.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 +25 -0
- package/dist/index.mjs +16 -10
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -192,6 +192,31 @@ Responses 和 Anthropic 请求同样在该前缀下使用 `responses`、`message
|
|
|
192
192
|
|
|
193
193
|
模型发现、30 秒刷新和推理都使用同一个远端与 token。远程模式不读取本机 Magpie 地址或 Fast 配置,也不会自动启动本机网关、失败后回退到本机。首次发现失败会报错;后续刷新失败保留最近成功的目录。目录请求不跟随重定向,反代应直接处理所配置的地址。
|
|
194
194
|
|
|
195
|
+
### Agent 标签与原生归属
|
|
196
|
+
|
|
197
|
+
`options.agentID` 是可选的请求标签,默认 `opencode`;支持 1–64 个小写字母、数字、`.`、`_`、`-`,且首位为字母或数字。它会用于模型发现、目录刷新、推理和生图请求的 `X-Magpie-Agent`,但**不会覆盖 OpenCode 的真实 User-Agent,也不是 UI 来源检测**。
|
|
198
|
+
|
|
199
|
+
例如 [Yuri](https://useyuri.top) 通过 Magpie 的原生 Agent 连接获得网关 URL 和 key 后,可以把它们原样传入:
|
|
200
|
+
|
|
201
|
+
```jsonc
|
|
202
|
+
{
|
|
203
|
+
"plugins": [{
|
|
204
|
+
"package": "opencode-magpie-plugin",
|
|
205
|
+
"options": {
|
|
206
|
+
"agentID": "yuri",
|
|
207
|
+
"remote": {
|
|
208
|
+
"baseURL": "http://127.0.0.1:3425/v1",
|
|
209
|
+
"token": "{env:MAGPIE_AGENT_GATEWAY_KEY}"
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}]
|
|
213
|
+
}
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
环境变量里的值必须是 Magpie 实际分配给该 Agent 连接的 key,而不是上游服务凭据。插件保留 `remote.token` 原值,不会按 `agentID` 生成、改写或替换它。本机网关优先通过分配的 Agent key 确定归属,单改 advisory header 不能把直接 OpenCode 请求变成 Yuri 请求。未配置 `remote` 时,本机连接仍保持原有默认行为,不会因为 `agentID` 变化而换 key。
|
|
217
|
+
|
|
218
|
+
本插件不自动扫描 Yuri 的状态文件,不改写 OpenCode 配置,也不猜测哪个 UI 使用了共享服务。CLI 若使用同一个 Provider/key,会共享这条连接的归属。早于 `opencode-magpie-plugin@0.6.0` 的固定安装不包含这项选项支持,需要包含本次改动的新构建;仅修改本仓库源码不会更新正在运行的安装。
|
|
219
|
+
|
|
195
220
|
### 本机网关
|
|
196
221
|
|
|
197
222
|
未配置 `remote` 时,地址优先级:
|
package/dist/index.mjs
CHANGED
|
@@ -382,7 +382,7 @@ const parsePresentation = (options) => {
|
|
|
382
382
|
order: /* @__PURE__ */ new Map()
|
|
383
383
|
};
|
|
384
384
|
if (!isRecord(options)) throw invalidOptions("options", "必须是对象");
|
|
385
|
-
for (const key of Object.keys(options)) if (key !== "groups" && key !== "order" && key !== "remote") throw invalidOptions(`options.${key}`, "不是已知字段");
|
|
385
|
+
for (const key of Object.keys(options)) if (key !== "groups" && key !== "order" && key !== "remote" && key !== "agentID") throw invalidOptions(`options.${key}`, "不是已知字段");
|
|
386
386
|
return {
|
|
387
387
|
groups: parseGroups(options.groups),
|
|
388
388
|
order: parseOrder(options.order)
|
|
@@ -463,7 +463,7 @@ const buildInventory = (rows, baseURL, options = {}) => {
|
|
|
463
463
|
},
|
|
464
464
|
headers: {
|
|
465
465
|
Authorization: `Bearer ${apiKey}`,
|
|
466
|
-
"X-Magpie-Agent": "opencode"
|
|
466
|
+
"X-Magpie-Agent": options.agentID ?? "opencode"
|
|
467
467
|
}
|
|
468
468
|
},
|
|
469
469
|
models: []
|
|
@@ -520,10 +520,13 @@ const loadFastModelIDs = async () => {
|
|
|
520
520
|
const INSTALL_MESSAGE = "未安装 Magpie,请先从 https://usemagpie.ai/zh/ 安装并登录所需服务,再使用此插件。";
|
|
521
521
|
/** Explicit remote URLs are API bases, including any reverse-proxy prefix and version path. */
|
|
522
522
|
const resolveConnection = async (options) => {
|
|
523
|
+
const agentID = options?.agentID ?? "opencode";
|
|
524
|
+
if (typeof agentID !== "string" || !/^[a-z0-9][a-z0-9._-]{0,63}$/.test(agentID)) throw new Error("Magpie options.agentID must be a lowercase identifier (1-64 characters).");
|
|
523
525
|
const remote = options?.remote;
|
|
524
526
|
if (remote === void 0) {
|
|
525
527
|
const localURL = await resolveBaseURL();
|
|
526
528
|
return {
|
|
529
|
+
agentID,
|
|
527
530
|
baseURL: `${localURL}/v1`,
|
|
528
531
|
token: "magpie",
|
|
529
532
|
remote: false,
|
|
@@ -544,6 +547,7 @@ const resolveConnection = async (options) => {
|
|
|
544
547
|
}
|
|
545
548
|
if (typeof value.token !== "string" || !/^[\x21-\x7e]+$/.test(value.token)) throw invalid(".token", "必须是非空 token,不能包含空格或控制字符");
|
|
546
549
|
return {
|
|
550
|
+
agentID,
|
|
547
551
|
baseURL: url.href.replace(/\/+$/, ""),
|
|
548
552
|
token: value.token,
|
|
549
553
|
remote: true,
|
|
@@ -674,12 +678,12 @@ const ensureGateway = async (baseURL) => {
|
|
|
674
678
|
starting.delete(baseURL);
|
|
675
679
|
}
|
|
676
680
|
};
|
|
677
|
-
const fetchModels = async (baseURL, token = "magpie") => {
|
|
681
|
+
const fetchModels = async (baseURL, token = "magpie", agentID = "opencode") => {
|
|
678
682
|
const response = await fetch(`${baseURL}/models`, {
|
|
679
683
|
headers: {
|
|
680
684
|
Authorization: `Bearer ${token}`,
|
|
681
685
|
"X-Magpie-Prices": "1",
|
|
682
|
-
"X-Magpie-Agent":
|
|
686
|
+
"X-Magpie-Agent": agentID
|
|
683
687
|
},
|
|
684
688
|
redirect: "error",
|
|
685
689
|
signal: AbortSignal.timeout(15e3)
|
|
@@ -699,7 +703,7 @@ const listImageModels = async (connection, signal) => {
|
|
|
699
703
|
headers: {
|
|
700
704
|
Authorization: `Bearer ${connection.token}`,
|
|
701
705
|
"X-Magpie-Drawers": "1",
|
|
702
|
-
"X-Magpie-Agent": "opencode"
|
|
706
|
+
"X-Magpie-Agent": connection.agentID ?? "opencode"
|
|
703
707
|
},
|
|
704
708
|
redirect: "error",
|
|
705
709
|
signal: AbortSignal.any([AbortSignal.timeout(15e3), ...signal ? [signal] : []])
|
|
@@ -730,7 +734,7 @@ const drawImage = async (connection, input, signal) => {
|
|
|
730
734
|
headers: {
|
|
731
735
|
Authorization: `Bearer ${connection.token}`,
|
|
732
736
|
"Content-Type": "application/json",
|
|
733
|
-
"X-Magpie-Agent": "opencode"
|
|
737
|
+
"X-Magpie-Agent": connection.agentID ?? "opencode"
|
|
734
738
|
},
|
|
735
739
|
body: JSON.stringify({
|
|
736
740
|
...input,
|
|
@@ -860,12 +864,13 @@ const setupPlugin = async (ctx) => {
|
|
|
860
864
|
if (!ctx.provider) throw new Error("Magpie 插件需要 OpenCode V2 provider.transform。");
|
|
861
865
|
const presentation = parsePresentation(ctx.options);
|
|
862
866
|
const connection = await resolveConnection(ctx.options);
|
|
863
|
-
const { baseURL, token } = connection;
|
|
867
|
+
const { baseURL, token, agentID } = connection;
|
|
864
868
|
const apiURL = new URL(baseURL);
|
|
865
869
|
await connection.ensure();
|
|
866
870
|
let catalog = await loadCatalogCache();
|
|
867
|
-
let inventory = buildInventory(await fetchModels(baseURL, token), baseURL, {
|
|
871
|
+
let inventory = buildInventory(await fetchModels(baseURL, token, agentID), baseURL, {
|
|
868
872
|
apiKey: token,
|
|
873
|
+
agentID,
|
|
869
874
|
catalog: catalog.data,
|
|
870
875
|
fastModelIDs: connection.remote ? void 0 : await loadFastModelIDs(),
|
|
871
876
|
presentation
|
|
@@ -894,7 +899,7 @@ const setupPlugin = async (ctx) => {
|
|
|
894
899
|
if (url.origin !== apiURL.origin || !url.pathname.startsWith(`${apiURL.pathname.replace(/\/$/, "")}/`)) return;
|
|
895
900
|
await connection.ensure();
|
|
896
901
|
event.request.headers.set("X-Magpie-Session", event.sessionID);
|
|
897
|
-
event.request.headers.set("X-Magpie-Agent", "opencode");
|
|
902
|
+
event.request.headers.set("X-Magpie-Agent", agentID ?? "opencode");
|
|
898
903
|
}));
|
|
899
904
|
await reloadProviderCatalog(ctx);
|
|
900
905
|
} catch (error) {
|
|
@@ -917,10 +922,11 @@ const setupPlugin = async (ctx) => {
|
|
|
917
922
|
refreshing = true;
|
|
918
923
|
try {
|
|
919
924
|
await connection.ensure();
|
|
920
|
-
const rows = await fetchModels(baseURL, token);
|
|
925
|
+
const rows = await fetchModels(baseURL, token, agentID);
|
|
921
926
|
catalog = await loadCatalogCache(hasUnpricedModel(inventory));
|
|
922
927
|
await apply(buildInventory(rows, baseURL, {
|
|
923
928
|
apiKey: token,
|
|
929
|
+
agentID,
|
|
924
930
|
catalog: catalog.data,
|
|
925
931
|
fastModelIDs: connection.remote ? void 0 : await loadFastModelIDs(),
|
|
926
932
|
presentation
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opencode-magpie-plugin",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Discover Magpie providers and models in OpenCode V2 with built-in pricing and automatic local gateway startup",
|
|
5
5
|
"icon": "data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2044%2044'%20fill='currentColor'%3E%3Cpath%20fill-rule='evenodd'%20d='M1.5,5.5C1.3,5.7,1.2,6.5,1.2,6.9C1.2,6.9,1.2,7,1.2,7.1C1.1,7.4,1.3,8,1.4,8.3C1.4,8.4,1.5,8.5,1.5,8.7C1.5,8.9,1.7,9,1.8,9.3C2.2,9.9,2.2,10,2.3,10.1C2.4,10.2,2.5,10.3,2.6,10.5C2.8,10.8,3,11.1,3.2,11.3C3.3,11.3,3.4,11.6,3.6,11.8C3.8,12,4.1,12.4,4.2,12.5C4.7,13,5.2,13.7,5.4,13.9C6,14.5,6.5,15.1,6.7,15.3C6.7,15.4,6.9,15.5,6.9,15.7C7.3,15.9,7.7,16.5,7.9,16.8C7.9,16.8,8,16.9,8,16.9C8.1,16.9,8.2,17.2,8.2,17.3C8.2,17.3,8.1,17.2,8.1,17.1C7.9,16.9,7.9,16.9,7.6,16.6C7.5,16.5,7.4,16.5,7.4,16.3C7.2,16.2,6.7,15.7,6.3,15.2C6.1,15.1,6,14.9,6,14.9C5.9,14.8,5.7,14.7,5.7,14.6C5.6,14.5,5.4,14.3,5.3,14.2C5.1,14.1,4.9,13.8,4.7,13.5C4.5,13.3,4.2,13.1,4.1,12.9C4.1,12.9,3.8,12.7,3.8,12.5C3.5,12.3,3,11.8,2.8,11.4C1.8,10.5,0.9,9.6,0.7,9.6C0.6,9.7,0.6,9.8,0.5,10C0.5,10.1,0.6,10.2,0.5,10.3C0.5,10.4,0.6,10.5,0.6,10.6C0.5,10.7,0.5,10.9,0.6,11C0.5,11.1,0.6,11.4,0.6,11.5C0.9,12.9,1.3,13.8,2.8,14.9C2.9,15.1,3.3,15.3,3.4,15.4C3.4,15.5,3.6,15.6,3.7,15.7C4.2,16,6.4,17.8,7,18.4C7.4,18.8,7.4,18.8,7.5,18.9C7.6,19,7.7,19,7.7,19.1C7.7,19.2,7.8,19.2,7.9,19.2C7.9,19.3,8.1,19.4,8.1,19.5C8.2,19.6,8.2,19.7,8.3,19.7C8.3,19.7,8.3,19.7,8.2,19.7C8.2,19.6,8.1,19.6,8,19.6C7.9,19.5,7.9,19.4,7.8,19.4C7.7,19.3,7.6,19.2,7.6,19.2C7.6,19.1,7.5,19,7.3,18.9C7.2,18.9,6.9,18.6,6.7,18.4C6.1,17.8,4.5,16.6,3.5,16C3.4,15.8,3,15.6,2.9,15.5C2.6,15.3,1.5,14.6,0.9,14.3C0.7,14.1,0.6,14.1,0.6,14.2C0.5,14.5,0.8,15.9,0.9,16.1C0.9,16.2,1,16.2,0.9,16.3C1.2,16.8,1.9,17.6,2.7,18.3C2.8,18.5,4,19.2,4.7,19.5C6.2,20.5,6.6,20.7,7.4,21.5C8.2,22.2,8.7,22.7,9.3,23.6C9.5,24.1,9.9,24.4,10.3,25.3C10.4,25.5,10.5,25.8,10.5,25.8C10.7,25.8,10.8,25.7,11,25.4C11.6,25,11.9,24.6,12.3,24.3C12.8,24,13,23.8,13,23.7C13.1,23.7,13.1,23.8,13,24C12,24.8,11,26,10.2,26.9C10.1,27.1,9.9,27.5,9.7,27.7C9.5,27.8,9.4,28,9.4,28C9.4,28,9.3,28.3,9.1,28.5C8.6,29.2,7.7,30.8,7.7,30.9C7.7,31,7.8,31,8.3,30.8C9,30.4,9.5,30.2,10.4,30C11.7,29.6,12.5,29.6,11.6,30C10.5,30.2,9.7,30.6,9.2,31.2C8.8,31.7,8.8,31.7,9.3,31.7C10.1,31.8,10.9,31.9,11.6,32.2C12.8,32.7,13.5,33.1,15.5,34.8C16.4,35.5,17.3,36.2,17.8,36.4C17.9,36.5,18.1,36.7,18.2,36.8C19.1,37.3,21.2,38.1,22.3,38.3C22.4,38.3,22.7,38.3,22.8,38.3C22.8,38.5,23,38.5,23,38.5C23.1,38.5,23.2,38.5,23.3,38.5C23.6,38.6,25.9,38.6,26.1,38.5C26.2,38.5,26.3,38.5,26.4,38.5C26.5,38.5,26.6,38.5,26.7,38.3C26.7,38.3,27,38.3,27.1,38.3C28.3,38.1,30.2,37.3,31.4,36.7C31.5,36.7,31.7,36.4,31.8,36.4C32.2,36.2,33.2,35.4,33.4,35.2C34.2,34.5,35.5,33,35.8,32.6C35.8,32.5,36,32.2,36.1,32C36.4,31.8,36.6,31.4,36.6,31.2C36.7,31.1,36.8,30.9,36.9,30.8C37.2,30.4,37.5,29.6,37.7,29.2C37.7,29.1,37.8,28.7,37.9,28.7C38.1,28.5,38.4,27.1,38.5,26.8C39,25.2,39.1,23.7,39,22.4C39,22.2,38.9,21.9,38.9,21.8C38.9,21.7,38.9,21.6,38.9,21.4C38.6,20.5,38.5,19.9,38.2,19C37.9,18.5,37.5,17.5,37.3,17.2C37,16.8,36.8,15.8,36.8,15.2C36.8,13.8,37.7,12.7,39.6,12.2C40.1,12.1,41.3,11.8,42.1,11.7C43.5,11.5,43.5,11.5,43.4,11.3C42.9,10.7,40.9,9.9,39.2,9.7C37.9,9.5,38.1,9.5,37.6,9C37,8.6,36.4,8,36,7.9C36,7.8,35.8,7.8,35.7,7.7C35.1,7.3,34.2,7,33.1,6.9C32.6,6.7,31.6,6.9,31,7C29.6,7.3,28.3,8,27.3,9.2C26.7,9.8,26.2,10.8,25.7,11.7C25.5,12.1,24.8,13.4,24.7,13.7C24.6,13.9,24.5,14.1,24.1,14.7C23.5,15.7,22.1,16.9,20.6,17.8C20.5,18,20.3,18.2,20.1,18.3C19.7,18.5,19.4,18.8,19.1,19C18.9,19.1,18.7,19.3,18.5,19.4C18,19.8,17.2,20.3,17,20.5C16.9,20.5,16.8,20.6,16.7,20.7C16.3,20.8,16.1,20.9,16,20.9C15.5,20.8,14.9,20.5,14.2,20.2C13.7,19.8,12.7,18.9,12.4,18.6C12.3,18.5,12.2,18.4,12.1,18.2C11.9,18,11.2,17.1,11,16.6C10.9,16.5,10.8,16.4,10.7,16.3C10.7,16.2,10.6,16.1,10.6,15.9C10.5,15.9,10.4,15.7,10.4,15.6C10.3,15.5,10.2,15.2,10.1,15C10,14.8,9.8,14.4,9.7,14.2C9.6,14,9.5,13.7,9.4,13.6C9.4,13.4,9.3,13.2,9.2,13C9.2,12.8,9,12.6,9,12.5C8.9,12.3,8.7,12.1,8.7,12C8.7,12,8.5,11.7,8.5,11.7C8.3,11.2,8.2,10.9,7.5,10C6.4,8.3,4.2,6.4,2.7,5.8C2,5.5,1.6,5.4,1.5,5.5ZM28.4,16.7C29.2,16.9,29.7,17.2,29.9,17.7C30.1,18.2,30,18.9,29.6,19.4C29.2,19.9,28.2,20.9,27.6,21.2C26.8,21.7,25.3,22.4,24.2,22.7C23.9,22.8,23.6,22.9,23.5,22.9C22.8,23.2,20.7,23.7,19.6,24C19,24.2,18.4,24.3,18.1,24.4C17.9,24.4,16.1,24.9,15.3,25.1C14.6,25.2,14.7,25.1,15.7,23.6C16.4,22.7,18.5,20.6,19.4,19.9C20.8,18.8,21.4,18.4,23.2,17.6C23.8,17.4,25.6,16.8,25.9,16.8C25.9,16.8,26.3,16.8,26.6,16.7C26.8,16.7,28.2,16.7,28.4,16.7ZM30.4,20.2C30.5,20.3,30.6,20.6,30.6,20.7C31.7,23.1,33.4,24.6,35.6,25.3C36.1,25.5,36.7,25.7,37.2,25.8C37.7,25.8,37.8,25.9,37.8,26.1C37.7,26.2,37.7,26.3,37.7,26.5C37.7,26.6,37.7,26.8,37.6,27C37.4,28.5,36.8,29.9,36.1,31C36.1,31.2,35.9,31.4,35.9,31.6C35.7,31.9,35.2,32.6,34.7,33.3C32.6,35.6,29.7,37.3,26.6,37.8C26.5,37.8,26.3,37.9,26.2,37.9C25.8,37.9,23.7,37.9,23.3,37.9C23.2,37.9,23,37.8,22.9,37.8C21.1,37.6,19.3,36.8,18,35.7C17.7,35.4,17,34.7,16.8,34.5C16.3,33.8,16.1,33,16.3,32.5C16.5,31.8,16.8,31.6,17.9,31.1C19.1,30.5,19.4,30.4,20.2,30C21.4,29.4,23.2,28.4,23.9,27.9C23.9,27.8,24.2,27.7,24.5,27.6C26.3,26.3,28,24.6,28.9,22.9C29.5,22.2,29.7,21.6,29.9,20.7C30.1,20,30.2,19.9,30.4,20.2Z'/%3E%3C/svg%3E",
|
|
6
6
|
"type": "module",
|