@fish-under-sea/magpie-dragonapi 1.0.8 → 1.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/index.mjs +76 -10
- package/lib/catalog.mjs +21 -7
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -30,6 +30,8 @@ import { startService } from './lib/service.mjs'
|
|
|
30
30
|
const PROVIDER_ID = 'dragonapi'
|
|
31
31
|
/** 站点给这把钥匙起的名字前缀,便于用户在控制台认出来。 */
|
|
32
32
|
const KEY_NAME = 'Magpie'
|
|
33
|
+
/** 「优先走 Responses API」开关的 key(magpie 的 auth.options)。 */
|
|
34
|
+
const OPT_RESPONSES = 'responsesFirst'
|
|
33
35
|
|
|
34
36
|
/**
|
|
35
37
|
* 插件状态。全部放在闭包里,一个进程一份。
|
|
@@ -115,6 +117,18 @@ function makeContext(input, options) {
|
|
|
115
117
|
remember,
|
|
116
118
|
catalog,
|
|
117
119
|
status,
|
|
120
|
+
/**
|
|
121
|
+
* preferResponses 读「优先走 Responses API」开关。
|
|
122
|
+
*
|
|
123
|
+
* 站点有些模型同时支持 chat 与 Responses;默认走 chat 兼容性最好,
|
|
124
|
+
* 用户打开这个开关后,声明了 openai-response 的模型改用 Responses。
|
|
125
|
+
*/
|
|
126
|
+
preferResponses() {
|
|
127
|
+
try {
|
|
128
|
+
const v = input?.magpie?.option?.(PROVIDER_ID, OPT_RESPONSES)
|
|
129
|
+
return v === true
|
|
130
|
+
} catch { return false }
|
|
131
|
+
},
|
|
118
132
|
noteModelsError: (why) => { state.lastModelsError = why },
|
|
119
133
|
}
|
|
120
134
|
}
|
|
@@ -197,6 +211,18 @@ export default async function dragonapi(input, options) {
|
|
|
197
211
|
|
|
198
212
|
auth: {
|
|
199
213
|
provider: PROVIDER_ID,
|
|
214
|
+
/**
|
|
215
|
+
* options 是 magpie 自己的开关声明(OpenCode 不读)。用户在插件编辑器里
|
|
216
|
+
* 打开后,声明了 openai-response 的模型改用 Responses API。
|
|
217
|
+
*/
|
|
218
|
+
options: [
|
|
219
|
+
{
|
|
220
|
+
key: OPT_RESPONSES,
|
|
221
|
+
label: '优先 Responses API',
|
|
222
|
+
description: '同时支持 chat 与 Responses 的模型改用 Responses API;关掉则走 chat completions。',
|
|
223
|
+
default: false,
|
|
224
|
+
},
|
|
225
|
+
],
|
|
200
226
|
get icon() {
|
|
201
227
|
// 站点图标(Ciallo 立绘):包内 png 编成 data URI。宿主只收 https 与
|
|
202
228
|
// data:image,联网抓会在断网时变成灰书页。
|
|
@@ -320,19 +346,59 @@ export default async function dragonapi(input, options) {
|
|
|
320
346
|
const key = found?.secret || ''
|
|
321
347
|
return {
|
|
322
348
|
apiKey: key,
|
|
323
|
-
baseURL
|
|
349
|
+
// 不写 baseURL:每个模型在自己 api.url 上按自己的协议(openai/
|
|
350
|
+
// anthropic/gemini)发请求。全局 base 会把所有模型压回同一个协议。
|
|
324
351
|
fetch: async (input_, init) => {
|
|
352
|
+
// 每个模型按自己的协议发出:OpenAI 兼容把模型放在 body 里,
|
|
353
|
+
// Gemini 放在 URL 的 /models/<模型>:generateContent 里,
|
|
354
|
+
// Anthropic 又在 body 里。所以两处都要还原裸模型名。
|
|
355
|
+
const href = (() => {
|
|
356
|
+
try { return String(typeof input_ === 'string' ? input_ : input_?.url || '') } catch { return '' }
|
|
357
|
+
})()
|
|
358
|
+
|
|
359
|
+
/** bareModel 去掉 `分组/` 前缀,只留站点认的模型名。 */
|
|
360
|
+
const bareModel = (v) => {
|
|
361
|
+
if (typeof v !== 'string' || !v.includes('/')) return ''
|
|
362
|
+
const cut = v.indexOf('/')
|
|
363
|
+
return v.slice(cut + 1)
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// Gemini:/v1beta/models/<id>:generateContent,id 可能被 URL 编码成 %2F
|
|
367
|
+
let nextHref = href
|
|
368
|
+
if (href.includes('/models/')) {
|
|
369
|
+
const q = href.indexOf('?')
|
|
370
|
+
const head = q < 0 ? href : href.slice(0, q)
|
|
371
|
+
const tail = q < 0 ? '' : href.slice(q)
|
|
372
|
+
const marker = '/models/'
|
|
373
|
+
const at = head.indexOf(marker)
|
|
374
|
+
if (at >= 0) {
|
|
375
|
+
const after = head.slice(at + marker.length)
|
|
376
|
+
const decoded = decodeURIComponent(after)
|
|
377
|
+
// `模型:generateContent` —— 冒号后面是动作,只能编码模型名那一段。
|
|
378
|
+
const colon = decoded.lastIndexOf(':')
|
|
379
|
+
const namePart = colon < 0 ? decoded : decoded.slice(0, colon)
|
|
380
|
+
const action = colon < 0 ? '' : decoded.slice(colon)
|
|
381
|
+
const bare = bareModel(namePart)
|
|
382
|
+
if (bare && bare !== namePart) {
|
|
383
|
+
nextHref = head.slice(0, at + marker.length) + encodeURIComponent(bare) + action + tail
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
325
388
|
const body = init?.body
|
|
326
|
-
if (typeof body !== 'string')
|
|
389
|
+
if (typeof body !== 'string') {
|
|
390
|
+
return nextHref !== href ? fetch(nextHref, init) : fetch(input_, init)
|
|
391
|
+
}
|
|
327
392
|
let parsed
|
|
328
|
-
try { parsed = JSON.parse(body) } catch {
|
|
329
|
-
|
|
330
|
-
|
|
393
|
+
try { parsed = JSON.parse(body) } catch {
|
|
394
|
+
return nextHref !== href ? fetch(nextHref, init) : fetch(input_, init)
|
|
395
|
+
}
|
|
396
|
+
const bare = bareModel(parsed?.model)
|
|
397
|
+
if (!bare) return nextHref !== href ? fetch(nextHref, init) : fetch(input_, init)
|
|
331
398
|
// `分组/模型` → `模型`。分组已经由账号选定了,请求里不需要它。
|
|
332
|
-
const bare = raw.slice(raw.indexOf('/') + 1)
|
|
333
|
-
if (!bare) return fetch(input_, init)
|
|
334
399
|
parsed.model = bare
|
|
335
|
-
|
|
400
|
+
const url = nextHref !== href ? nextHref : input_
|
|
401
|
+
return fetch(url, { ...init, body: JSON.stringify(parsed) })
|
|
336
402
|
},
|
|
337
403
|
}
|
|
338
404
|
},
|
|
@@ -364,7 +430,7 @@ export default async function dragonapi(input, options) {
|
|
|
364
430
|
const st = await ctx.status.get({})
|
|
365
431
|
const cur = currencyOf(st)
|
|
366
432
|
const built = cat.env
|
|
367
|
-
? buildCatalog(cat.env, { cur, enabledGroups: [], site: ctx.site })
|
|
433
|
+
? buildCatalog(cat.env, { cur, enabledGroups: [], site: ctx.site, preferResponses: ctx.preferResponses() })
|
|
368
434
|
: { models: {}, rows: [], groups: [] }
|
|
369
435
|
|
|
370
436
|
// 保护 1:列表单调不减。站点偶发少给几个模型时用上一次实时表补齐,
|
|
@@ -512,7 +578,7 @@ async function authorizeKey(ctx, key) {
|
|
|
512
578
|
const cat = await ctx.catalog.get({})
|
|
513
579
|
const st = await ctx.status.get({})
|
|
514
580
|
const built = cat.env
|
|
515
|
-
? buildCatalog(cat.env, { cur: currencyOf(st), enabledGroups: [], site: ctx.site })
|
|
581
|
+
? buildCatalog(cat.env, { cur: currencyOf(st), enabledGroups: [], site: ctx.site, preferResponses: ctx.preferResponses() })
|
|
516
582
|
: { models: {} }
|
|
517
583
|
const known = new Set(Object.keys(built.models).map((id) => {
|
|
518
584
|
const cut = id.indexOf('/')
|
package/lib/catalog.mjs
CHANGED
|
@@ -16,17 +16,30 @@
|
|
|
16
16
|
import { priceOf } from './pricing.mjs'
|
|
17
17
|
|
|
18
18
|
/** 端点类型 → 该用哪个 SDK 方言发出去。 */
|
|
19
|
-
export function npmOf(row) {
|
|
19
|
+
export function npmOf(row, { preferResponses = false } = {}) {
|
|
20
20
|
const ends = Array.isArray(row?.supported_endpoint_types) ? row.supported_endpoint_types.map(String) : []
|
|
21
|
-
//
|
|
22
|
-
//
|
|
23
|
-
if (ends.includes('openai') || ends.includes('openai-response')) return '@ai-sdk/openai-compatible'
|
|
24
|
-
if (ends.includes('image-generation')) return '@ai-sdk/openai-compatible'
|
|
21
|
+
// 站点一个模型常常同时挂 openai / openai-response / anthropic / gemini。
|
|
22
|
+
// 按模型挑原生协议,才能真正做到「每个模型走自己的端点」。
|
|
25
23
|
if (ends.includes('anthropic')) return '@ai-sdk/anthropic'
|
|
26
24
|
if (ends.includes('gemini')) return '@ai-sdk/google'
|
|
25
|
+
// Responses API:只有声明了 openai-response 才可能走它;同时支持 chat 时
|
|
26
|
+
// 默认走 chat(兼容性最好),用户打开「优先 Responses」才切过去。
|
|
27
|
+
const responses = ends.includes('openai-response')
|
|
28
|
+
const chat = ends.includes('openai')
|
|
29
|
+
if (responses && (!chat || preferResponses)) return '@ai-sdk/openai'
|
|
27
30
|
return '@ai-sdk/openai-compatible'
|
|
28
31
|
}
|
|
29
32
|
|
|
33
|
+
/** apiFor 按模型协议给 base URL:Anthropic 要根地址,Gemini 要 /v1beta。 */
|
|
34
|
+
function apiFor(row, site, opts = {}) {
|
|
35
|
+
const base = String(site || '').replace(/\/+$/, '')
|
|
36
|
+
const npm = npmOf(row, opts)
|
|
37
|
+
const url = npm === '@ai-sdk/anthropic' ? base
|
|
38
|
+
: npm === '@ai-sdk/google' ? base + '/v1beta'
|
|
39
|
+
: base + '/v1'
|
|
40
|
+
return { url, npm }
|
|
41
|
+
}
|
|
42
|
+
|
|
30
43
|
/** 模型的类别,决定 magpie 把它放进哪个分区。 */
|
|
31
44
|
export function kindOf(row) {
|
|
32
45
|
const ends = Array.isArray(row?.supported_endpoint_types) ? row.supported_endpoint_types.map(String) : []
|
|
@@ -71,7 +84,7 @@ function rankOf(kind, group, groupOrder) {
|
|
|
71
84
|
* `rows` 是「分组 × 模型」的扁平表,给页面用;`models` 是给宿主的模型表。
|
|
72
85
|
* `enabledGroups` 是用户勾选的分组(空数组表示全部分组)。
|
|
73
86
|
*/
|
|
74
|
-
export function buildCatalog(env, { cur, enabledGroups = [], site } = {}) {
|
|
87
|
+
export function buildCatalog(env, { cur, enabledGroups = [], site, preferResponses = false } = {}) {
|
|
75
88
|
const data = Array.isArray(env?.data) ? env.data : []
|
|
76
89
|
const ratios = env?.group_ratio && typeof env.group_ratio === 'object' ? env.group_ratio : {}
|
|
77
90
|
const order = Array.isArray(env?.group_order) ? env.group_order : []
|
|
@@ -141,10 +154,11 @@ export function buildCatalog(env, { cur, enabledGroups = [], site } = {}) {
|
|
|
141
154
|
_rank: [r0, r1, name, group],
|
|
142
155
|
})
|
|
143
156
|
|
|
157
|
+
const { url: apiUrl, npm } = apiFor(row, site, { preferResponses })
|
|
144
158
|
models[id] = {
|
|
145
159
|
id,
|
|
146
160
|
name: `${name} · ${group}${ratio > 0 ? ` ×${round(ratio)}` : ''}`,
|
|
147
|
-
api: { id: name, url:
|
|
161
|
+
api: { id: name, url: apiUrl, npm },
|
|
148
162
|
limit: {
|
|
149
163
|
context: Number(row?.context_window) > 0 ? Number(row.context_window) : 0,
|
|
150
164
|
output: 0,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fish-under-sea/magpie-dragonapi",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "DragonAPI for magpie: a provider plugin whose models, groups and prices come from the site's public /api/pricing, with balance and per-call usage from an sk- key, plus a local page that lists the market (read-only), shows usage and can mint new keys for a group.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.mjs",
|