magpie-plugin-retry 2.0.0 → 2.1.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 +11 -7
- package/package.json +1 -1
- package/src/index.mjs +46 -8
package/README.md
CHANGED
|
@@ -51,13 +51,15 @@ magpie plugin login retry api
|
|
|
51
51
|
magpie provider test retry
|
|
52
52
|
```
|
|
53
53
|
|
|
54
|
-
|
|
54
|
+
登录时会依次询问上游 Base URL 和 API Key,不需要先改插件选项。若已在插件选项里配置
|
|
55
|
+
`baseURL`,登录时留空即可复用该地址。Key 只保存在 Magpie 的账号存储里,不写进本包。
|
|
55
56
|
|
|
56
57
|
## 配置
|
|
57
58
|
|
|
58
59
|
通过 `magpie plugin options <安装时的 spec> '<json>'` 设置。本地目录安装时 spec 是目录
|
|
59
|
-
路径;从 npm 安装时通常是 `npm:magpie-plugin-retry`。`
|
|
60
|
-
|
|
60
|
+
路径;从 npm 安装时通常是 `npm:magpie-plugin-retry`。`models` 是必填项;`baseURL` 可以
|
|
61
|
+
直接在登录时填写,也可以写在这里作为默认值。插件不提供默认地址或默认模型映射;未配置
|
|
62
|
+
`models` 时 provider 不会暴露任何模型。
|
|
61
63
|
|
|
62
64
|
```sh
|
|
63
65
|
# 本地目录安装
|
|
@@ -67,11 +69,10 @@ magpie plugin options /Users/bill/WorkSpace/magpie-plugin-retry '<json>'
|
|
|
67
69
|
magpie plugin options npm:magpie-plugin-retry '<json>'
|
|
68
70
|
```
|
|
69
71
|
|
|
70
|
-
|
|
72
|
+
最小配置示例(Base URL 在登录时填写):
|
|
71
73
|
|
|
72
74
|
```json
|
|
73
75
|
{
|
|
74
|
-
"baseURL": "https://your-relay.example.com/v1",
|
|
75
76
|
"models": {
|
|
76
77
|
"vendor/asked-model": {
|
|
77
78
|
"target": "vendor/served-model",
|
|
@@ -82,6 +83,8 @@ magpie plugin options npm:magpie-plugin-retry '<json>'
|
|
|
82
83
|
}
|
|
83
84
|
```
|
|
84
85
|
|
|
86
|
+
也可以把 `baseURL` 固定写在插件选项里,登录时对应问题留空即可。
|
|
87
|
+
|
|
85
88
|
`models` 的 key 是客户端请求模型,value 可以是目标模型字符串,也可以是带
|
|
86
89
|
`target` / `name` 的对象:
|
|
87
90
|
|
|
@@ -112,7 +115,7 @@ magpie plugin options npm:magpie-plugin-retry '<json>'
|
|
|
112
115
|
|
|
113
116
|
| 选项 | 默认值 | 说明 |
|
|
114
117
|
|---|---|---|
|
|
115
|
-
| `baseURL` |
|
|
118
|
+
| `baseURL` | 登录时填写 | 上游 API 地址,必须以 `/v1` 结尾;在这里配置后登录时可留空 |
|
|
116
119
|
| `apiKey` | 空 | 可选固定 Key;正常使用优先 `magpie plugin login`,登录 Key 优先 |
|
|
117
120
|
| `models` | 必填 | 请求模型 → 目标模型映射,支持对象或数组 |
|
|
118
121
|
| `requestModel` | 空 | 只配置一组映射时的请求模型 |
|
|
@@ -122,7 +125,8 @@ magpie plugin options npm:magpie-plugin-retry '<json>'
|
|
|
122
125
|
| `maxAttempts` | `12` | 每次请求最多尝试次数,最大 50 |
|
|
123
126
|
| `maxConcurrency` | `4` | 每个账号并发请求数,最大 32 |
|
|
124
127
|
|
|
125
|
-
|
|
128
|
+
插件没有内置默认映射;每次安装后都需要显式设置至少一个 `models` 映射,并在登录时填写
|
|
129
|
+
`baseURL`(或通过插件选项设置)。
|
|
126
130
|
|
|
127
131
|
## 使用
|
|
128
132
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "magpie-plugin-retry",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "Generic Magpie provider plugin that pins any requested model to a configured real target model by checking the upstream's streaming response.",
|
|
5
5
|
"private": false,
|
|
6
6
|
"type": "module",
|
package/src/index.mjs
CHANGED
|
@@ -14,6 +14,21 @@ const RETRY_STATUS = new Set([408, 409, 425, 429, 500, 502, 503, 504]);
|
|
|
14
14
|
|
|
15
15
|
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
16
16
|
|
|
17
|
+
function parseBaseURL(value) {
|
|
18
|
+
const text = typeof value === "string" ? value.trim() : "";
|
|
19
|
+
if (!text) return { error: "Base URL required" };
|
|
20
|
+
let url;
|
|
21
|
+
try {
|
|
22
|
+
url = new URL(text);
|
|
23
|
+
} catch {
|
|
24
|
+
return { error: "Base URL must be an absolute http(s) URL" };
|
|
25
|
+
}
|
|
26
|
+
if (url.protocol !== "http:" && url.protocol !== "https:") {
|
|
27
|
+
return { error: "Base URL must be an absolute http(s) URL" };
|
|
28
|
+
}
|
|
29
|
+
return { value: text };
|
|
30
|
+
}
|
|
31
|
+
|
|
17
32
|
function parseBody(body) {
|
|
18
33
|
if (body == null) return null;
|
|
19
34
|
if (typeof body === "string") {
|
|
@@ -394,6 +409,18 @@ export async function RetryPlugin(_input, options = {}) {
|
|
|
394
409
|
type: "api",
|
|
395
410
|
label: `${providerName} API Key`,
|
|
396
411
|
prompts: [
|
|
412
|
+
{
|
|
413
|
+
type: "text",
|
|
414
|
+
key: "baseURL",
|
|
415
|
+
message: `${providerName} Base URL`,
|
|
416
|
+
placeholder: "https://your-relay.example.com/v1",
|
|
417
|
+
validate: (value) => {
|
|
418
|
+
if (!value || !String(value).trim()) {
|
|
419
|
+
return baseURL ? undefined : "Base URL required";
|
|
420
|
+
}
|
|
421
|
+
return parseBaseURL(value).error;
|
|
422
|
+
},
|
|
423
|
+
},
|
|
397
424
|
{
|
|
398
425
|
type: "password",
|
|
399
426
|
key: "key",
|
|
@@ -403,25 +430,36 @@ export async function RetryPlugin(_input, options = {}) {
|
|
|
403
430
|
},
|
|
404
431
|
],
|
|
405
432
|
async authorize(input) {
|
|
406
|
-
const
|
|
407
|
-
|
|
408
|
-
? input
|
|
409
|
-
: input?.key ?? input?.token ?? input?.apiKey ?? "";
|
|
433
|
+
const typed = typeof input === "string" ? input : input ?? {};
|
|
434
|
+
const value = typeof typed === "string" ? typed : typed.key ?? typed.token ?? typed.apiKey ?? "";
|
|
410
435
|
const key = value.trim();
|
|
411
436
|
if (!key) return { type: "failed", error: "API Key required" };
|
|
412
|
-
|
|
437
|
+
const typedBaseURL =
|
|
438
|
+
typeof typed === "object" && typeof typed.baseURL === "string" ? typed.baseURL.trim() : "";
|
|
439
|
+
const parsed = typedBaseURL ? parseBaseURL(typedBaseURL) : {};
|
|
440
|
+
if (parsed.error) return { type: "failed", error: parsed.error };
|
|
441
|
+
const effectiveBaseURL = parsed.value || baseURL;
|
|
442
|
+
if (!effectiveBaseURL) return { type: "failed", error: "Base URL required" };
|
|
443
|
+
return {
|
|
444
|
+
type: "success",
|
|
445
|
+
key,
|
|
446
|
+
...(parsed.value ? { metadata: { baseURL: parsed.value } } : {}),
|
|
447
|
+
};
|
|
413
448
|
},
|
|
414
449
|
},
|
|
415
450
|
],
|
|
416
451
|
async loader(getAuth) {
|
|
417
452
|
const auth = await getAuth();
|
|
418
453
|
const key = auth?.key ?? auth?.methods?.[0]?.key ?? pluginKey;
|
|
419
|
-
|
|
454
|
+
const authBaseURL =
|
|
455
|
+
typeof auth?.metadata?.baseURL === "string" ? auth.metadata.baseURL.trim() : "";
|
|
456
|
+
const effectiveBaseURL = authBaseURL || baseURL;
|
|
457
|
+
if (!key || !effectiveBaseURL) return {};
|
|
420
458
|
return {
|
|
421
|
-
baseURL,
|
|
459
|
+
baseURL: effectiveBaseURL,
|
|
422
460
|
apiKey: key,
|
|
423
461
|
headers: { "x-api-key": key, "anthropic-version": "2023-06-01" },
|
|
424
|
-
fetch: createFetch(
|
|
462
|
+
fetch: createFetch(effectiveBaseURL, key, models, maxAttempts),
|
|
425
463
|
};
|
|
426
464
|
},
|
|
427
465
|
},
|