magpie-modelscope-hub 0.1.3 → 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 +16 -9
- package/index.mjs +14 -2
- package/package.json +1 -1
- package/src/credit.mjs +110 -0
package/README.md
CHANGED
|
@@ -172,15 +172,22 @@ magpie 的模型选择器会把它画成 `2×` 这样的小标签,和 WorkBudd
|
|
|
172
172
|
|
|
173
173
|
### 一次请求花了多少魔粒?
|
|
174
174
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
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 不扣魔粒;没实测过单价的模型留空,不猜
|
|
183
|
+
|
|
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`。
|
|
187
|
+
|
|
188
|
+
这张卡上的「今天花了 5 魔粒 · 3 次(旗舰 2×2、标准 1×1)」仍来自厂商账单,
|
|
189
|
+
是实际扣款;逐条 credits 是按单价算的,两者对得上,但并发取消的请求可能只出现在
|
|
190
|
+
账单里、没有逐条记录。
|
|
184
191
|
|
|
185
192
|
### 用量卡
|
|
186
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
|
|
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
|
+
}
|