@fish-under-sea/magpie-dragonapi 1.0.8 → 1.0.9

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 CHANGED
@@ -320,19 +320,59 @@ export default async function dragonapi(input, options) {
320
320
  const key = found?.secret || ''
321
321
  return {
322
322
  apiKey: key,
323
- baseURL: ctx.site + '/v1',
323
+ // 不写 baseURL:每个模型在自己 api.url 上按自己的协议(openai/
324
+ // anthropic/gemini)发请求。全局 base 会把所有模型压回同一个协议。
324
325
  fetch: async (input_, init) => {
326
+ // 每个模型按自己的协议发出:OpenAI 兼容把模型放在 body 里,
327
+ // Gemini 放在 URL 的 /models/<模型>:generateContent 里,
328
+ // Anthropic 又在 body 里。所以两处都要还原裸模型名。
329
+ const href = (() => {
330
+ try { return String(typeof input_ === 'string' ? input_ : input_?.url || '') } catch { return '' }
331
+ })()
332
+
333
+ /** bareModel 去掉 `分组/` 前缀,只留站点认的模型名。 */
334
+ const bareModel = (v) => {
335
+ if (typeof v !== 'string' || !v.includes('/')) return ''
336
+ const cut = v.indexOf('/')
337
+ return v.slice(cut + 1)
338
+ }
339
+
340
+ // Gemini:/v1beta/models/<id>:generateContent,id 可能被 URL 编码成 %2F
341
+ let nextHref = href
342
+ if (href.includes('/models/')) {
343
+ const q = href.indexOf('?')
344
+ const head = q < 0 ? href : href.slice(0, q)
345
+ const tail = q < 0 ? '' : href.slice(q)
346
+ const marker = '/models/'
347
+ const at = head.indexOf(marker)
348
+ if (at >= 0) {
349
+ const after = head.slice(at + marker.length)
350
+ const decoded = decodeURIComponent(after)
351
+ // `模型:generateContent` —— 冒号后面是动作,只能编码模型名那一段。
352
+ const colon = decoded.lastIndexOf(':')
353
+ const namePart = colon < 0 ? decoded : decoded.slice(0, colon)
354
+ const action = colon < 0 ? '' : decoded.slice(colon)
355
+ const bare = bareModel(namePart)
356
+ if (bare && bare !== namePart) {
357
+ nextHref = head.slice(0, at + marker.length) + encodeURIComponent(bare) + action + tail
358
+ }
359
+ }
360
+ }
361
+
325
362
  const body = init?.body
326
- if (typeof body !== 'string') return fetch(input_, init)
363
+ if (typeof body !== 'string') {
364
+ return nextHref !== href ? fetch(nextHref, init) : fetch(input_, init)
365
+ }
327
366
  let parsed
328
- try { parsed = JSON.parse(body) } catch { return fetch(input_, init) }
329
- const raw = parsed?.model
330
- if (typeof raw !== 'string' || !raw.includes('/')) return fetch(input_, init)
367
+ try { parsed = JSON.parse(body) } catch {
368
+ return nextHref !== href ? fetch(nextHref, init) : fetch(input_, init)
369
+ }
370
+ const bare = bareModel(parsed?.model)
371
+ if (!bare) return nextHref !== href ? fetch(nextHref, init) : fetch(input_, init)
331
372
  // `分组/模型` → `模型`。分组已经由账号选定了,请求里不需要它。
332
- const bare = raw.slice(raw.indexOf('/') + 1)
333
- if (!bare) return fetch(input_, init)
334
373
  parsed.model = bare
335
- return fetch(input_, { ...init, body: JSON.stringify(parsed) })
374
+ const url = nextHref !== href ? nextHref : input_
375
+ return fetch(url, { ...init, body: JSON.stringify(parsed) })
336
376
  },
337
377
  }
338
378
  },
package/lib/catalog.mjs CHANGED
@@ -18,15 +18,23 @@ import { priceOf } from './pricing.mjs'
18
18
  /** 端点类型 → 该用哪个 SDK 方言发出去。 */
19
19
  export function npmOf(row) {
20
20
  const ends = Array.isArray(row?.supported_endpoint_types) ? row.supported_endpoint_types.map(String) : []
21
- // 只要模型支持 openai 兼容,就走 openai-compatible:本站绝大多数模型都挂着它,
22
- // 而且 anthropic / gemini 原生通道在 magpie 里要额外的账号形态,第一版不做。
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 / anthropic / gemini 多个协议。按模型挑
22
+ // 原生协议,才能真正做到「每个模型走自己的端点」;只挂 openai 的走兼容层。
25
23
  if (ends.includes('anthropic')) return '@ai-sdk/anthropic'
26
24
  if (ends.includes('gemini')) return '@ai-sdk/google'
27
25
  return '@ai-sdk/openai-compatible'
28
26
  }
29
27
 
28
+ /** apiFor 按模型协议给 base URL:Anthropic 要根地址,Gemini 要 /v1beta。 */
29
+ function apiFor(row, site) {
30
+ const base = String(site || '').replace(/\/+$/, '')
31
+ const npm = npmOf(row)
32
+ const url = npm === '@ai-sdk/anthropic' ? base
33
+ : npm === '@ai-sdk/google' ? base + '/v1beta'
34
+ : base + '/v1'
35
+ return { url, npm }
36
+ }
37
+
30
38
  /** 模型的类别,决定 magpie 把它放进哪个分区。 */
31
39
  export function kindOf(row) {
32
40
  const ends = Array.isArray(row?.supported_endpoint_types) ? row.supported_endpoint_types.map(String) : []
@@ -141,10 +149,11 @@ export function buildCatalog(env, { cur, enabledGroups = [], site } = {}) {
141
149
  _rank: [r0, r1, name, group],
142
150
  })
143
151
 
152
+ const { url: apiUrl, npm } = apiFor(row, site)
144
153
  models[id] = {
145
154
  id,
146
155
  name: `${name} · ${group}${ratio > 0 ? ` ×${round(ratio)}` : ''}`,
147
- api: { id: name, url: String(site || '').replace(/\/+$/, '') + '/v1', npm: npmOf(row) },
156
+ api: { id: name, url: apiUrl, npm },
148
157
  limit: {
149
158
  context: Number(row?.context_window) > 0 ? Number(row.context_window) : 0,
150
159
  output: 0,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fish-under-sea/magpie-dragonapi",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
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",