magpie-modelscope-hub 0.1.2 → 0.1.4

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 CHANGED
@@ -33,7 +33,13 @@ provider 的模型时,只用 `accountsOf(readAuth(), id)[0]` —— **第一
33
33
 
34
34
  ## 安装
35
35
 
36
- 从 GitHub 装(推荐):
36
+ 从 npm 装(推荐,更新检查走 npm,不占 GitHub 限额):
37
+
38
+ ```powershell
39
+ magpie plugin add magpie-modelscope-hub@latest
40
+ ```
41
+
42
+ 从 GitHub 装(想看源码提交时用):
37
43
 
38
44
  ```powershell
39
45
  magpie plugin add github:BangShou1st/magpie-modelscope-hub
@@ -138,17 +144,50 @@ supported" 的条目)标成 `deprecated`,magpie 会把它们从选择器和
138
144
  档位但上游不认的变体,会让用户选「high」而实际仍是默认档——比没有更糟。国内站
139
145
  38 个模型里,15 个标了思考,其中 4 个有档位。
140
146
 
147
+ ### 每个模型消耗多少魔粒(倍率)
148
+
149
+ 官方只给三档价:轻量 0.5、主流 1、旗舰 2 魔粒/次,但**没有「哪个模型属于哪档」
150
+ 的接口**。`/v1/models` 只回 id;模型详情 API 里没有价格、`magicube` 或 `tier`
151
+ 字段;模型页上那句「预计消耗魔粒」是前端算的。所以这张表是实测出来的:
152
+
153
+ 1. 对每个模型打一次 `max_tokens: 1` 的最小请求
154
+ 2. 读当天账单,看 `SPEND_CONFIRM` 里哪个 `model_tier` 的 `count` 加了 1
155
+ 3. 加的 `total_amount / count` 就是单价
156
+
157
+ 2026-11-19 对国内站逐个测过(`_probe/rate-sweep.mjs`),实测结果写进
158
+ `src/rates.mjs`,作为模型的 `rate` 字段交给 magpie:
159
+
160
+ | 单价 | 模型 |
161
+ | --- | --- |
162
+ | 0.5 魔粒/次 | `nex-agi/Nex-N2.5-mini` |
163
+ | 1 魔粒/次 | `MiniMax/MiniMax-M1-80k`、`mistralai/Mistral-Large-Instruct-2407`、`nex-agi/Nex-N2.5-Pro`、`opencompass/CompassJudger-1-32B-Instruct`、`Qwen/Qwen3.5-122B-A10B`、`Qwen/Qwen3.5-27B`、`Qwen/Qwen3.5-35B-A3B`、`Qwen/Qwen3.8-27B`、`Qwen/Qwen3.8-Flash-Next`、`Shanghai_AI_Laboratory/Intern-S2-Preview`、`XGenerationLab/XiYanSQL-QwenCoder-32B-2412 / 2504` |
164
+ | 2 魔粒/次 | `deepseek-ai/DeepSeek-V4-Pro-0813`、`deepseek-ai/DeepSeek-V4.1-Flash-1017`、`Qwen/Qwen3.5-397B-A17B`、`ZhipuAI/GLM-5.2` |
165
+
166
+ 返回「has no provider supported」或 401 的模型测不到,就不给 `rate`——没数不猜。
167
+ 国际站还没登录、也没实测,同样不显示倍率。
168
+
169
+ magpie 的模型选择器会把它画成 `2×` 这样的小标签,和 WorkBuddy 的 `credits`
170
+ 倍率是同一个字段(`internal/plugin/providers.go` 的 `Rate`)。上面这 17 个模型
171
+ 之外,选择器里不显示倍率,不是「免费」。
172
+
141
173
  ### 一次请求花了多少魔粒?
142
174
 
143
- 做不到按请求归因,三个来源都不给这个粒度:
175
+ 逐笔归因厂商仍然不给:响应里只有 token 用量,账单只有按 (天, 档位) 聚合的
176
+ `SPEND_CONFIRM` 行。但按次计费、单价已知,所以插件自己把这一笔的魔粒补进响应的
177
+ `usage.credit`——magpie 的用量账本本来就收这个字段(WorkBuddy 的 `credit` 同一个
178
+ 位置,`internal/usage/ledger.go`):
179
+
180
+ - 非流式:直接改写响应 JSON 的 `usage`
181
+ - 流式:给请求补 `stream_options.include_usage`,再改最后那块 usage
182
+ - 只写 2xx:4xx/5xx 不扣魔粒;没实测过单价的模型留空,不猜
144
183
 
145
- - 推理响应里只有 token 用量,没有魔粒字段,响应头也没有(2026-11-18 实测)
146
- - 账单只有按 (天, 档位) 聚合的 `SPEND_CONFIRM` 行(`model_tier` + `total_amount` + `count`)
147
- - magpie 的账本本身没有按请求的积分记账,并发时余额差也分不清是谁花的
148
- (`docs/subsystems/providers-accounts.md` 明说不做按次归因)
184
+ 这样 `magpie usage --csv` 的 `credits` 列、GUI 的用量页都能逐条看到
185
+ 「这一笔 2 魔粒」,并按模型汇总。2026-11-19 实测:网关走 `Qwen/Qwen3.8-27B`
186
+ 一条记 `credits=1`,走 `DeepSeek-V4.1-Flash-1017` 记 `credits=2`。
149
187
 
150
- 所以卡片说的是「今天花了 5 魔粒 · 3 次(旗舰 2×2、标准 1×1)」,不是「这一笔 2 魔粒」。
151
- 每笔按 2 的估算只在本地用来判断什么时候该提前去读余额。
188
+ 这张卡上的「今天花了 5 魔粒 · 3 次(旗舰 2×2、标准 1×1)」仍来自厂商账单,
189
+ 是实际扣款;逐条 credits 是按单价算的,两者对得上,但并发取消的请求可能只出现在
190
+ 账单里、没有逐条记录。
152
191
 
153
192
  ### 用量卡
154
193
 
package/index.mjs CHANGED
@@ -17,6 +17,8 @@ import { modelsFor } from "./src/models.mjs"
17
17
  import { checkinFor } from "./src/checkin.mjs"
18
18
  import { dropLive, noteCall, resetLive, usageFor } from "./src/usage.mjs"
19
19
  import { resetModelsCache } from "./src/models.mjs"
20
+ import { rateOf } from "./src/rates.mjs"
21
+ import { requestModel, withCredit, withUsageOption } from "./src/credit.mjs"
20
22
 
21
23
  // 魔搭的方块标(models.dev 那份),内联成 data URI:不联网也能画出来。
22
24
  const ICON =
@@ -56,8 +58,15 @@ function makePlugin(profile) {
56
58
  async fetch(input, init) {
57
59
  const headers = new Headers(init?.headers)
58
60
  headers.set("authorization", `Bearer ${auth.key}`)
59
- const res = await fetch(input, { ...init, headers })
60
- if (res.status === 200) noteCall(profile, auth.key)
61
+ const prepared = withUsageOption(input, init)
62
+ const rate = rateOf(profile, requestModel(prepared))
63
+ const res = await fetch(input, { ...prepared, headers })
64
+ if (res.status === 200) {
65
+ noteCall(profile, auth.key)
66
+ // 按次计费、单价已知:把这一笔的魔粒写进 usage.credit,
67
+ // magpie 的用量页就能逐条显示(WorkBuddy 的 credit 同一个字段)。
68
+ if (rate !== undefined) return withCredit(res, rate)
69
+ }
61
70
  if (res.status === 401) return withSignIn(res, "expired")
62
71
  if (res.status === 403) return withSignIn(res, "kept")
63
72
  if (res.status === 402) {
@@ -100,4 +109,7 @@ export const _internal = {
100
109
  resetLive,
101
110
  resetModelsCache,
102
111
  withSignIn,
112
+ withCredit,
113
+ requestModel,
114
+ withUsageOption,
103
115
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "magpie-modelscope-hub",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "魔搭 ModelScope 合一插件:国内站与国际站各自提供模型调用、魔粒用量卡和每日签到",
5
5
  "type": "module",
6
6
  "main": "./index.mjs",
package/src/credit.mjs ADDED
@@ -0,0 +1,110 @@
1
+ // 把「这次调用值多少魔粒」写进响应的 usage.credit。
2
+ //
3
+ // magpie 的用量账本有一条 credits(internal/usage/ledger.go):记录响应里
4
+ // usage.credit 说的「这一笔花了多少积分」,WorkBuddy 就是靠它按请求显示的。
5
+ // 魔搭的响应只有 token 用量,没有这个字段;但按次计费、单价已知(src/rates.mjs),
6
+ // 所以由插件补上 —— 对实测过的模型,这个数和厂商实际扣的一样。
7
+ //
8
+ // 只动成功响应;4xx/5xx 不扣魔粒,不写。没实测过单价的模型不写,不猜。
9
+
10
+ const TEXT = new TextEncoder()
11
+ const DECODE = new TextDecoder()
12
+
13
+ function rewriteLine(line, rate) {
14
+ const cr = line.endsWith("\r")
15
+ const raw = cr ? line.slice(0, -1) : line
16
+ if (!raw.startsWith("data:")) return line
17
+ const payload = raw.slice(5).trim()
18
+ if (!payload || payload === "[DONE]") return line
19
+ try {
20
+ const chunk = JSON.parse(payload)
21
+ if (chunk?.usage && typeof chunk.usage === "object") {
22
+ chunk.usage.credit = rate
23
+ return `data: ${JSON.stringify(chunk)}${cr ? "\r" : ""}`
24
+ }
25
+ } catch {
26
+ // 不是 JSON 的事件原样放行
27
+ }
28
+ return line
29
+ }
30
+
31
+ function creditStream(body, rate) {
32
+ let buffer = ""
33
+ return body.pipeThrough(
34
+ new TransformStream({
35
+ transform(chunk, controller) {
36
+ buffer += DECODE.decode(chunk, { stream: true })
37
+ let at
38
+ while ((at = buffer.indexOf("\n")) >= 0) {
39
+ const line = buffer.slice(0, at)
40
+ buffer = buffer.slice(at + 1)
41
+ controller.enqueue(TEXT.encode(`${rewriteLine(line, rate)}\n`))
42
+ }
43
+ },
44
+ flush(controller) {
45
+ buffer += DECODE.decode()
46
+ if (buffer) controller.enqueue(TEXT.encode(rewriteLine(buffer, rate)))
47
+ },
48
+ }),
49
+ )
50
+ }
51
+
52
+ function bare(headers) {
53
+ const out = new Headers(headers)
54
+ // 正文重新编码过,长度和压缩标记都不再作数
55
+ out.delete("content-length")
56
+ out.delete("content-encoding")
57
+ return out
58
+ }
59
+
60
+ export async function withCredit(res, rate) {
61
+ if (!(rate > 0)) return res
62
+ const type = res.headers.get("content-type") ?? ""
63
+ if (type.includes("text/event-stream")) {
64
+ return new Response(creditStream(res.body, rate), {
65
+ status: res.status,
66
+ statusText: res.statusText,
67
+ headers: bare(res.headers),
68
+ })
69
+ }
70
+ const text = await res.text()
71
+ let body = text
72
+ try {
73
+ const json = JSON.parse(text)
74
+ if (json?.usage && typeof json.usage === "object") {
75
+ json.usage.credit = rate
76
+ body = JSON.stringify(json)
77
+ }
78
+ } catch {
79
+ // 不是 JSON:原样交回
80
+ }
81
+ return new Response(body, { status: res.status, statusText: res.statusText, headers: bare(res.headers) })
82
+ }
83
+
84
+ // 请求体里的模型 id;流式或表单体读不出来时返回 undefined。
85
+ export function requestModel(init) {
86
+ if (typeof init?.body !== "string") return undefined
87
+ try {
88
+ const json = JSON.parse(init.body)
89
+ return typeof json?.model === "string" ? json.model : undefined
90
+ } catch {
91
+ return undefined
92
+ }
93
+ }
94
+
95
+ // 魔搭流式响应默认不带 usage(结尾那些块的 usage 是 null),要显式要
96
+ // stream_options.include_usage 才会在最后给一块带 token 用量的 usage。
97
+ // 只动 chat/completions,别的端点不碰。
98
+ export function withUsageOption(input, init) {
99
+ if (typeof init?.body !== "string") return init
100
+ if (!String(input).includes("/chat/completions")) return init
101
+ try {
102
+ const json = JSON.parse(init.body)
103
+ if (json?.stream !== true) return init
104
+ if (json.stream_options?.include_usage === true) return init
105
+ json.stream_options = { ...json.stream_options, include_usage: true }
106
+ return { ...init, body: JSON.stringify(json) }
107
+ } catch {
108
+ return init
109
+ }
110
+ }
package/src/models.mjs CHANGED
@@ -11,6 +11,7 @@ import { PATHS, TIMEOUT_MS, apiBase } from "./sites.mjs"
11
11
  import { expiredError } from "./net.mjs"
12
12
  import { familyOf, variantsOf } from "./capabilities.mjs"
13
13
  import { modalitiesOf } from "./modalities.mjs"
14
+ import { rateOf } from "./rates.mjs"
14
15
 
15
16
  const MODELS_TTL_MS = 30 * 60 * 1000
16
17
  const STATE_MAX = 256
@@ -75,6 +76,9 @@ export function modelEntry(profile, id, created) {
75
76
  // 推理强度:只有文档写明档位的模型才有。空对象表示「这个模型没有分档」,
76
77
  // 不是「还没做」。
77
78
  variants: variantsOf(id),
79
+ // 魔粒倍率:调一次扣多少魔粒,只写实测过的。magpie 的模型选择器会把它
80
+ // 画成「2×」这样的标签(和 WorkBuddy 的 credits 倍率同一个字段)。
81
+ ...(rateOf(profile, id) !== undefined ? { rate: rateOf(profile, id) } : {}),
78
82
  }
79
83
  }
80
84
 
package/src/rates.mjs ADDED
@@ -0,0 +1,43 @@
1
+ // 每个模型「调一次扣多少魔粒」。
2
+ //
3
+ // 官方只公布三档价格(轻量 0.5、主流 1、旗舰 2),却没有「哪个模型属于
4
+ // 哪档」的接口:/v1/models 只回 id,模型详情 API 里没有价格或档位字段,
5
+ // 模型页上那句「预计消耗魔粒」是前端算的。所以这张表是实测出来的:
6
+ // 对每个模型各打一次最小请求,再读当天账单,看 SPEND_CONFIRM 里哪个
7
+ // model_tier 的 count 加了 1,加的钱数就是这个模型的单价。
8
+ //
9
+ // 只写实测过的。没测到的(返回 "has no provider supported" / 401 的)
10
+ // 不给值 —— 没数就不显示倍率,不拿别的模型的价来猜。
11
+ //
12
+ // 重测办法:node _probe/rate-sweep.mjs,结果写 _probe/rate-map.json。
13
+
14
+ export const RATES = {
15
+ // 轻量 0.5 魔粒/次
16
+ "nex-agi/Nex-N2.5-mini": 0.5,
17
+
18
+ // 主流 1 魔粒/次
19
+ "MiniMax/MiniMax-M1-80k": 1,
20
+ "mistralai/Mistral-Large-Instruct-2407": 1,
21
+ "nex-agi/Nex-N2.5-Pro": 1,
22
+ "opencompass/CompassJudger-1-32B-Instruct": 1,
23
+ "Qwen/Qwen3.5-122B-A10B": 1,
24
+ "Qwen/Qwen3.5-27B": 1,
25
+ "Qwen/Qwen3.5-35B-A3B": 1,
26
+ "Qwen/Qwen3.8-27B": 1,
27
+ "Qwen/Qwen3.8-Flash-Next": 1,
28
+ "Shanghai_AI_Laboratory/Intern-S2-Preview": 1,
29
+ "XGenerationLab/XiYanSQL-QwenCoder-32B-2412": 1,
30
+ "XGenerationLab/XiYanSQL-QwenCoder-32B-2504": 1,
31
+
32
+ // 旗舰 2 魔粒/次
33
+ "deepseek-ai/DeepSeek-V4-Pro-0813": 2,
34
+ "deepseek-ai/DeepSeek-V4.1-Flash-1017": 2,
35
+ "Qwen/Qwen3.5-397B-A17B": 2,
36
+ "ZhipuAI/GLM-5.2": 2,
37
+ }
38
+
39
+ // 国际站还没登录、也没测过,不给价;以后在那边实测了再按站点分开写。
40
+ export function rateOf(profile, id) {
41
+ if (profile?.id !== "modelscope-cn") return undefined
42
+ return RATES[id]
43
+ }