@fish-under-sea/magpie-dragonapi 1.0.9 → 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 +28 -2
- package/lib/catalog.mjs +12 -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,联网抓会在断网时变成灰书页。
|
|
@@ -404,7 +430,7 @@ export default async function dragonapi(input, options) {
|
|
|
404
430
|
const st = await ctx.status.get({})
|
|
405
431
|
const cur = currencyOf(st)
|
|
406
432
|
const built = cat.env
|
|
407
|
-
? buildCatalog(cat.env, { cur, enabledGroups: [], site: ctx.site })
|
|
433
|
+
? buildCatalog(cat.env, { cur, enabledGroups: [], site: ctx.site, preferResponses: ctx.preferResponses() })
|
|
408
434
|
: { models: {}, rows: [], groups: [] }
|
|
409
435
|
|
|
410
436
|
// 保护 1:列表单调不减。站点偶发少给几个模型时用上一次实时表补齐,
|
|
@@ -552,7 +578,7 @@ async function authorizeKey(ctx, key) {
|
|
|
552
578
|
const cat = await ctx.catalog.get({})
|
|
553
579
|
const st = await ctx.status.get({})
|
|
554
580
|
const built = cat.env
|
|
555
|
-
? buildCatalog(cat.env, { cur: currencyOf(st), enabledGroups: [], site: ctx.site })
|
|
581
|
+
? buildCatalog(cat.env, { cur: currencyOf(st), enabledGroups: [], site: ctx.site, preferResponses: ctx.preferResponses() })
|
|
556
582
|
: { models: {} }
|
|
557
583
|
const known = new Set(Object.keys(built.models).map((id) => {
|
|
558
584
|
const cut = id.indexOf('/')
|
package/lib/catalog.mjs
CHANGED
|
@@ -16,19 +16,24 @@
|
|
|
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
|
-
// 站点一个模型常常同时挂 openai / anthropic / gemini
|
|
22
|
-
//
|
|
21
|
+
// 站点一个模型常常同时挂 openai / openai-response / anthropic / gemini。
|
|
22
|
+
// 按模型挑原生协议,才能真正做到「每个模型走自己的端点」。
|
|
23
23
|
if (ends.includes('anthropic')) return '@ai-sdk/anthropic'
|
|
24
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'
|
|
25
30
|
return '@ai-sdk/openai-compatible'
|
|
26
31
|
}
|
|
27
32
|
|
|
28
33
|
/** apiFor 按模型协议给 base URL:Anthropic 要根地址,Gemini 要 /v1beta。 */
|
|
29
|
-
function apiFor(row, site) {
|
|
34
|
+
function apiFor(row, site, opts = {}) {
|
|
30
35
|
const base = String(site || '').replace(/\/+$/, '')
|
|
31
|
-
const npm = npmOf(row)
|
|
36
|
+
const npm = npmOf(row, opts)
|
|
32
37
|
const url = npm === '@ai-sdk/anthropic' ? base
|
|
33
38
|
: npm === '@ai-sdk/google' ? base + '/v1beta'
|
|
34
39
|
: base + '/v1'
|
|
@@ -79,7 +84,7 @@ function rankOf(kind, group, groupOrder) {
|
|
|
79
84
|
* `rows` 是「分组 × 模型」的扁平表,给页面用;`models` 是给宿主的模型表。
|
|
80
85
|
* `enabledGroups` 是用户勾选的分组(空数组表示全部分组)。
|
|
81
86
|
*/
|
|
82
|
-
export function buildCatalog(env, { cur, enabledGroups = [], site } = {}) {
|
|
87
|
+
export function buildCatalog(env, { cur, enabledGroups = [], site, preferResponses = false } = {}) {
|
|
83
88
|
const data = Array.isArray(env?.data) ? env.data : []
|
|
84
89
|
const ratios = env?.group_ratio && typeof env.group_ratio === 'object' ? env.group_ratio : {}
|
|
85
90
|
const order = Array.isArray(env?.group_order) ? env.group_order : []
|
|
@@ -149,7 +154,7 @@ export function buildCatalog(env, { cur, enabledGroups = [], site } = {}) {
|
|
|
149
154
|
_rank: [r0, r1, name, group],
|
|
150
155
|
})
|
|
151
156
|
|
|
152
|
-
const { url: apiUrl, npm } = apiFor(row, site)
|
|
157
|
+
const { url: apiUrl, npm } = apiFor(row, site, { preferResponses })
|
|
153
158
|
models[id] = {
|
|
154
159
|
id,
|
|
155
160
|
name: `${name} · ${group}${ratio > 0 ? ` ×${round(ratio)}` : ''}`,
|
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",
|