magpie-modelscope-hub 0.1.3 → 0.1.5
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 +25 -7
- package/index.mjs +14 -2
- package/package.json +1 -1
- package/src/credit.mjs +110 -0
- package/src/limits.mjs +59 -0
- package/src/models.mjs +2 -1
package/README.md
CHANGED
|
@@ -122,6 +122,17 @@ supported" 的条目)标成 `deprecated`,magpie 会把它们从选择器和
|
|
|
122
122
|
2026-11-18 对国内站 38 个模型逐个核过 pipeline 标签。表里没有的新 id 只报文本、
|
|
123
123
|
不声明图片能力,将来上的视觉模型仍能收到图,不会被本地网关先挡掉。
|
|
124
124
|
|
|
125
|
+
### 上下文窗口
|
|
126
|
+
|
|
127
|
+
`/v1/models` 不报窗口大小。之前所有线上模型吃默认 128K,对 1M 窗口的
|
|
128
|
+
DeepSeek-V4.1 / GLM-5.2 是错的:dsh 按「窗口 − 72K」当压缩阈值,128K 只剩
|
|
129
|
+
54272 令牌,一条 66K 的对话就会「压缩失败、切不过去」。现在 `src/limits.mjs`
|
|
130
|
+
按模型仓库 `config.json` 的 `max_position_embeddings`(含 `text_config` /
|
|
131
|
+
`llm_config` 里的)逐个补:DeepSeek 四兄弟 1M、GLM-5.2 1M、Qwen3.5/3.8 全系
|
|
132
|
+
256K、GLM-4.7-Flash 202752、Intern-S1 64K。查不到的仍按 128K,不乱报。
|
|
133
|
+
|
|
134
|
+
输出上限没有按模型的可靠来源,统一 8192。
|
|
135
|
+
|
|
125
136
|
### 推理强度
|
|
126
137
|
|
|
127
138
|
`/v1/models` 只给 id,给不了「是不是思考模型」「支持哪几档推理强度」。不补这张
|
|
@@ -172,15 +183,22 @@ magpie 的模型选择器会把它画成 `2×` 这样的小标签,和 WorkBudd
|
|
|
172
183
|
|
|
173
184
|
### 一次请求花了多少魔粒?
|
|
174
185
|
|
|
175
|
-
|
|
186
|
+
逐笔归因厂商仍然不给:响应里只有 token 用量,账单只有按 (天, 档位) 聚合的
|
|
187
|
+
`SPEND_CONFIRM` 行。但按次计费、单价已知,所以插件自己把这一笔的魔粒补进响应的
|
|
188
|
+
`usage.credit`——magpie 的用量账本本来就收这个字段(WorkBuddy 的 `credit` 同一个
|
|
189
|
+
位置,`internal/usage/ledger.go`):
|
|
190
|
+
|
|
191
|
+
- 非流式:直接改写响应 JSON 的 `usage`
|
|
192
|
+
- 流式:给请求补 `stream_options.include_usage`,再改最后那块 usage
|
|
193
|
+
- 只写 2xx:4xx/5xx 不扣魔粒;没实测过单价的模型留空,不猜
|
|
176
194
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
-
|
|
180
|
-
(`docs/subsystems/providers-accounts.md` 明说不做按次归因)
|
|
195
|
+
这样 `magpie usage --csv` 的 `credits` 列、GUI 的用量页都能逐条看到
|
|
196
|
+
「这一笔 2 魔粒」,并按模型汇总。2026-11-19 实测:网关走 `Qwen/Qwen3.8-27B`
|
|
197
|
+
一条记 `credits=1`,走 `DeepSeek-V4.1-Flash-1017` 记 `credits=2`。
|
|
181
198
|
|
|
182
|
-
|
|
183
|
-
|
|
199
|
+
这张卡上的「今天花了 5 魔粒 · 3 次(旗舰 2×2、标准 1×1)」仍来自厂商账单,
|
|
200
|
+
是实际扣款;逐条 credits 是按单价算的,两者对得上,但并发取消的请求可能只出现在
|
|
201
|
+
账单里、没有逐条记录。
|
|
184
202
|
|
|
185
203
|
### 用量卡
|
|
186
204
|
|
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
|
|
60
|
-
|
|
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
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/limits.mjs
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// 每个模型能吃多长的上下文。
|
|
2
|
+
//
|
|
3
|
+
// /v1/models 只回 id,模型详情接口也不说窗口大小。之前所有线上模型的
|
|
4
|
+
// limit.context 都吃默认 128000 —— 对 DeepSeek-V4.1 / GLM-5.2 这种 1M
|
|
5
|
+
// 窗口的模型,等于骗了客户端:magpie 的 dsh 按「窗口 - 72K」当压缩阈值,
|
|
6
|
+
// 128K 窗口只剩 54272 令牌,一条 66K 的对话就压不动、切不过去(2026-11-19
|
|
7
|
+
// 用户报的错)。窗口按模型仓库里的 config.json 读:
|
|
8
|
+
// max_position_embeddings,或 text_config / llm_config 里的同名键。
|
|
9
|
+
//
|
|
10
|
+
// 只有 config 里读得到的才写;读不到的不猜。MiniMax-M1-80k 的 config 写
|
|
11
|
+
// 10M(外推位置),实际服务窗口按名字是 80K,单独按 81920 算。
|
|
12
|
+
// 输出上限没有可靠的按模型来源,统一留 8192。
|
|
13
|
+
|
|
14
|
+
export const CONTEXTS = {
|
|
15
|
+
"deepseek-ai/DeepSeek-V4-Flash-0731": 1048576,
|
|
16
|
+
"deepseek-ai/DeepSeek-V4-Pro": 1048576,
|
|
17
|
+
"deepseek-ai/DeepSeek-V4-Pro-0813": 1048576,
|
|
18
|
+
"deepseek-ai/DeepSeek-V4.1-Flash": 1048576,
|
|
19
|
+
"deepseek-ai/DeepSeek-V4.1-Flash-1017": 1048576,
|
|
20
|
+
|
|
21
|
+
"MiniMax/MiniMax-M1-80k": 81920,
|
|
22
|
+
"MiniMax/MiniMax-M3": 1048576,
|
|
23
|
+
"mistralai/Mistral-Large-Instruct-2407": 131072,
|
|
24
|
+
"moonshotai/Kimi-K3.1-Pro": 1048576,
|
|
25
|
+
|
|
26
|
+
"nex-agi/Nex-N2.5-mini": 262144,
|
|
27
|
+
"nex-agi/Nex-N2.5-Pro": 262144,
|
|
28
|
+
"opencompass/CompassJudger-1-32B-Instruct": 32768,
|
|
29
|
+
"OpenGVLab/InternVL3_5-241B-A28B": 40960,
|
|
30
|
+
|
|
31
|
+
"PaddlePaddle/ERNIE-4.5-0.3B-PT": 131072,
|
|
32
|
+
"PaddlePaddle/ERNIE-4.5-21B-A3B-PT": 131072,
|
|
33
|
+
"PaddlePaddle/ERNIE-4.5-300B-A47B-PT": 131072,
|
|
34
|
+
"PaddlePaddle/ERNIE-4.5-VL-28B-A3B-PT": 131072,
|
|
35
|
+
|
|
36
|
+
"Qwen/Qwen3.5-122B-A10B": 262144,
|
|
37
|
+
"Qwen/Qwen3.5-27B": 262144,
|
|
38
|
+
"Qwen/Qwen3.5-35B-A3B": 262144,
|
|
39
|
+
"Qwen/Qwen3.5-397B-A17B": 262144,
|
|
40
|
+
"Qwen/Qwen3.8-27B": 262144,
|
|
41
|
+
"Qwen/Qwen3.8-Flash-Next": 262144,
|
|
42
|
+
|
|
43
|
+
"Shanghai_AI_Laboratory/Intern-S1": 65536,
|
|
44
|
+
"Shanghai_AI_Laboratory/Intern-S1-mini": 65536,
|
|
45
|
+
"Shanghai_AI_Laboratory/Intern-S2-Preview": 262144,
|
|
46
|
+
|
|
47
|
+
"stepfun-ai/Step-3.5-Flash": 262144,
|
|
48
|
+
"stepfun-ai/Step-3.7-Flash": 262144,
|
|
49
|
+
"stepfun-ai/Step-5-Preview": 1048576,
|
|
50
|
+
|
|
51
|
+
"XGenerationLab/XiYanSQL-QwenCoder-32B-2412": 32768,
|
|
52
|
+
"XGenerationLab/XiYanSQL-QwenCoder-32B-2504": 32768,
|
|
53
|
+
"ZhipuAI/GLM-4.7-Flash": 202752,
|
|
54
|
+
"ZhipuAI/GLM-5.2": 1048576,
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function contextOf(id) {
|
|
58
|
+
return CONTEXTS[id]
|
|
59
|
+
}
|
package/src/models.mjs
CHANGED
|
@@ -12,6 +12,7 @@ import { expiredError } from "./net.mjs"
|
|
|
12
12
|
import { familyOf, variantsOf } from "./capabilities.mjs"
|
|
13
13
|
import { modalitiesOf } from "./modalities.mjs"
|
|
14
14
|
import { rateOf } from "./rates.mjs"
|
|
15
|
+
import { contextOf } from "./limits.mjs"
|
|
15
16
|
|
|
16
17
|
const MODELS_TTL_MS = 30 * 60 * 1000
|
|
17
18
|
const STATE_MAX = 256
|
|
@@ -58,7 +59,7 @@ export function modelEntry(profile, id, created) {
|
|
|
58
59
|
name: known?.name ?? displayName(id),
|
|
59
60
|
api: { id, url: apiBase(profile), npm: "@ai-sdk/openai-compatible" },
|
|
60
61
|
status: isDeprecated(profile, id) ? "deprecated" : "active",
|
|
61
|
-
limit: known?.limit ?? { context: 128000, output: 8192 },
|
|
62
|
+
limit: known?.limit ?? { context: contextOf(id) ?? 128000, output: 8192 },
|
|
62
63
|
// 计费是魔粒按次扣,不按 token,所以成本字段报 0(和上游两个插件一致)。
|
|
63
64
|
cost: { input: 0, output: 0, cache: { read: 0, write: 0 } },
|
|
64
65
|
options: {},
|