dsh-model-allowance 1.0.6 → 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/README.md +12 -4
- package/README.zh.md +7 -4
- package/client.js +1487 -1228
- package/lib/balance.js +156 -20
- package/lib/debug.js +72 -13
- package/lib/providers/deepseek.js +4 -82
- package/lib/providers/holidays.js +89 -0
- package/lib/providers/placeholder.js +6 -62
- package/lib/providers/schedule.js +8 -6
- package/lib/providers/shared.js +6 -4
- package/lib/providers/xiaomi.js +285 -0
- package/lib/providers.js +44 -15
- package/lib/remote.js +3 -1
- package/package.json +2 -1
|
@@ -12,8 +12,8 @@
|
|
|
12
12
|
* no extra round trips.
|
|
13
13
|
*/
|
|
14
14
|
|
|
15
|
-
/** HH:MM in 24-hour wall time. */
|
|
16
|
-
const TIME_PATTERN = /^([01]\d|2[0-3]):
|
|
15
|
+
/** HH:MM in 24-hour wall time; `24:00` is the day's end. */
|
|
16
|
+
const TIME_PATTERN = /^(?:(?:[01]\d|2[0-3]):[0-5]\d|24:00)$/
|
|
17
17
|
|
|
18
18
|
/** ISO calendar date, in the schedule's own zone. */
|
|
19
19
|
const DATE_PATTERN = /^\d{4}-\d{2}-\d{2}$/
|
|
@@ -21,17 +21,19 @@ const DATE_PATTERN = /^\d{4}-\d{2}-\d{2}$/
|
|
|
21
21
|
/**
|
|
22
22
|
* Parse one `HH:MM` string to minutes since midnight.
|
|
23
23
|
* @param {string} value Wall-clock time.
|
|
24
|
-
* @returns {number} Minutes since midnight.
|
|
24
|
+
* @returns {number} Minutes since midnight, or NaN when unparsable.
|
|
25
25
|
*/
|
|
26
26
|
export function timeToMinutes(value) {
|
|
27
|
-
const
|
|
28
|
-
|
|
27
|
+
const text = String(value)
|
|
28
|
+
if (!TIME_PATTERN.test(text)) return Number.NaN
|
|
29
|
+
const [hour, minute] = text.split(':').map(Number)
|
|
30
|
+
return hour * 60 + minute
|
|
29
31
|
}
|
|
30
32
|
|
|
31
33
|
/**
|
|
32
34
|
* @typedef {object} ScheduleWindow
|
|
33
35
|
* @property {string} from Wall-clock start, `HH:MM`.
|
|
34
|
-
* @property {string} to Wall-clock end, `HH:MM`; must be later than `from`.
|
|
36
|
+
* @property {string} to Wall-clock end, `HH:MM` or `24:00`; must be later than `from`.
|
|
35
37
|
*/
|
|
36
38
|
|
|
37
39
|
/**
|
package/lib/providers/shared.js
CHANGED
|
@@ -152,7 +152,7 @@ export function bindingWindow(windows) {
|
|
|
152
152
|
* balance route.
|
|
153
153
|
* @param {'amount'|'quota'} spec.kind Quantity dimension of the reading.
|
|
154
154
|
* @param {boolean} [spec.modelScoped] Whether the reading is per model.
|
|
155
|
-
* @param {string} spec.consoleUrl Provider console for a manual check.
|
|
155
|
+
* @param {string} [spec.consoleUrl] Provider console for a manual check.
|
|
156
156
|
* @param {{name: {zh: string, en: string}, mark: {icon?: string, color: string}}} spec.display
|
|
157
157
|
* Display metadata the browser needs to name and draw the route.
|
|
158
158
|
* @param {string} [spec.service] Host service this adapter reads through.
|
|
@@ -185,8 +185,10 @@ export function defineAdapter(spec) {
|
|
|
185
185
|
if (spec.kind !== KIND_AMOUNT && spec.kind !== KIND_QUOTA) {
|
|
186
186
|
throw new Error(`${where} must declare kind "${KIND_AMOUNT}" or "${KIND_QUOTA}"`)
|
|
187
187
|
}
|
|
188
|
-
if (
|
|
189
|
-
|
|
188
|
+
if (spec.consoleUrl !== undefined) {
|
|
189
|
+
if (typeof spec.consoleUrl !== 'string' || !spec.consoleUrl.startsWith('https://')) {
|
|
190
|
+
throw new Error(`${where} needs an https console link`)
|
|
191
|
+
}
|
|
190
192
|
}
|
|
191
193
|
if (spec.status !== undefined) {
|
|
192
194
|
if (typeof spec.status !== 'object' || spec.status === null) {
|
|
@@ -256,7 +258,7 @@ export function defineAdapter(spec) {
|
|
|
256
258
|
: { modelRoutes: Object.freeze([...spec.modelRoutes]) }),
|
|
257
259
|
kind: spec.kind,
|
|
258
260
|
...(spec.modelScoped === true ? { modelScoped: true } : {}),
|
|
259
|
-
consoleUrl: spec.consoleUrl,
|
|
261
|
+
...(spec.consoleUrl === undefined ? {} : { consoleUrl: spec.consoleUrl }),
|
|
260
262
|
service: spec.service,
|
|
261
263
|
http: hasEndpoint ? Object.freeze({ endpoint: spec.endpoint, auth: spec.auth }) : undefined,
|
|
262
264
|
credentialRefs: Object.freeze([...(spec.credentialRefs ?? [])]),
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Xiaomi MiMo adapters.
|
|
3
|
+
*
|
|
4
|
+
* Two authorizations exist and they do not mix: the pay-as-you-go route reads
|
|
5
|
+
* the account wallet with an `sk-` key, and each Token Plan cluster reads its
|
|
6
|
+
* subscription credits with a region-scoped `tp-` key. A plan key asked for the
|
|
7
|
+
* wallet answers 401, so every route keeps its own credential.
|
|
8
|
+
*
|
|
9
|
+
* The wallet reports one `balance_infos` entry whose `total_available` already
|
|
10
|
+
* sums cash, gift, and available credit with the frozen part added back — the
|
|
11
|
+
* reading takes that figure instead of adding the components itself.
|
|
12
|
+
*
|
|
13
|
+
* The plan reports one credit quota per billing period. The period timestamps
|
|
14
|
+
* are UTC wall clocks without a zone marker, so they are parsed as UTC here
|
|
15
|
+
* rather than through the shared local-time reader.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { defineSchedule } from './schedule.js'
|
|
19
|
+
import {
|
|
20
|
+
asObject,
|
|
21
|
+
defineAdapter,
|
|
22
|
+
KIND_AMOUNT,
|
|
23
|
+
KIND_QUOTA,
|
|
24
|
+
parseAmount,
|
|
25
|
+
windowLength,
|
|
26
|
+
} from './shared.js'
|
|
27
|
+
|
|
28
|
+
/** MiMo's wallet page. Verified 2026-11-16. */
|
|
29
|
+
const BALANCE_CONSOLE = 'https://platform.xiaomimimo.com/console/balance'
|
|
30
|
+
|
|
31
|
+
/** MiMo's plan management page. Verified 2026-11-16. */
|
|
32
|
+
const PLAN_CONSOLE = 'https://platform.xiaomimimo.com/console/plan-manage'
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The CN plan consumes credits at 0.8x between 00:00 and 08:00 Beijing time
|
|
36
|
+
* every day, so the peak window — full-rate consumption — runs 08:00 to
|
|
37
|
+
* midnight, every day of the week.
|
|
38
|
+
*/
|
|
39
|
+
const CN_SCHEDULE = defineSchedule({
|
|
40
|
+
timezone: 'Asia/Shanghai',
|
|
41
|
+
timezoneLabel: { zh: '北京时间', en: 'Beijing time' },
|
|
42
|
+
days: [0, 1, 2, 3, 4, 5, 6],
|
|
43
|
+
windows: [{ from: '08:00', to: '24:00' }],
|
|
44
|
+
holidays: [],
|
|
45
|
+
note: {
|
|
46
|
+
zh: '低谷时段(每日 00:00–08:00)积分按 0.8 倍消耗',
|
|
47
|
+
en: 'Credits are consumed at 0.8x off-peak, daily 00:00–08:00',
|
|
48
|
+
},
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Project one MiMo wallet body.
|
|
53
|
+
* @param {{route: string, body: Record<string, unknown>}} env Projection context.
|
|
54
|
+
* @returns {import('../providers.js').BalanceView} The reading.
|
|
55
|
+
*/
|
|
56
|
+
function readBalance({ route, body }) {
|
|
57
|
+
const infos = Array.isArray(body.balance_infos) ? body.balance_infos : []
|
|
58
|
+
if (infos.length === 0) {
|
|
59
|
+
return { provider: route, status: 'failed', kind: KIND_AMOUNT, error: 'no balance in response' }
|
|
60
|
+
}
|
|
61
|
+
const primary = asObject(infos[0])
|
|
62
|
+
const amount = parseAmount(primary.total_available)
|
|
63
|
+
if (amount === undefined) {
|
|
64
|
+
return { provider: route, status: 'failed', kind: KIND_AMOUNT, error: 'no balance in response' }
|
|
65
|
+
}
|
|
66
|
+
const currency = typeof primary.currency === 'string' ? primary.currency : 'CNY'
|
|
67
|
+
return {
|
|
68
|
+
provider: route,
|
|
69
|
+
status: 'ready',
|
|
70
|
+
kind: KIND_AMOUNT,
|
|
71
|
+
amount,
|
|
72
|
+
currency,
|
|
73
|
+
consoleUrl: BALANCE_CONSOLE,
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Parse a MiMo period timestamp: `YYYY-MM-DD HH:mm:ss` in UTC, with no zone
|
|
79
|
+
* marker. The shared instant reader would take it as host local time and shift
|
|
80
|
+
* the reset by the host's offset, so the zone is pinned here.
|
|
81
|
+
* @param {unknown} value Raw timestamp.
|
|
82
|
+
* @returns {number | undefined} Epoch milliseconds, or undefined when unparsable.
|
|
83
|
+
*/
|
|
84
|
+
function parseUtcInstant(value) {
|
|
85
|
+
if (typeof value !== 'string') return undefined
|
|
86
|
+
const text = value.trim()
|
|
87
|
+
if (!/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/.test(text)) return undefined
|
|
88
|
+
const parsed = Date.parse(`${text.replace(' ', 'T')}Z`)
|
|
89
|
+
return Number.isFinite(parsed) ? parsed : undefined
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Project one MiMo Token Plan body.
|
|
94
|
+
*
|
|
95
|
+
* The plan meters a single billing period, so the reading carries one window:
|
|
96
|
+
* the period's credits with its closing instant as the reset. A missing period
|
|
97
|
+
* still reports the quota, as an unsized window.
|
|
98
|
+
*
|
|
99
|
+
* @param {{route: string, body: Record<string, unknown>}} env Projection context.
|
|
100
|
+
* @returns {import('../providers.js').BalanceView} The reading.
|
|
101
|
+
*/
|
|
102
|
+
function readTokenPlan({ route, body }) {
|
|
103
|
+
const quota = asObject(body.quota)
|
|
104
|
+
const total = parseAmount(quota.total_credits)
|
|
105
|
+
if (total === undefined || total <= 0) {
|
|
106
|
+
return { provider: route, status: 'failed', kind: KIND_QUOTA, error: 'no quota in response' }
|
|
107
|
+
}
|
|
108
|
+
const used = parseAmount(quota.used_credits)
|
|
109
|
+
const remaining = parseAmount(quota.remaining_credits)
|
|
110
|
+
const remainingPercent = remaining === undefined ? undefined : (remaining / total) * 100
|
|
111
|
+
const period = asObject(body.period)
|
|
112
|
+
const start = parseUtcInstant(period.start_at)
|
|
113
|
+
const end = parseUtcInstant(period.end_at)
|
|
114
|
+
const window = {
|
|
115
|
+
id: 'monthly',
|
|
116
|
+
...windowLength(start !== undefined && end !== undefined ? end - start : undefined),
|
|
117
|
+
total,
|
|
118
|
+
...(used === undefined ? {} : { used }),
|
|
119
|
+
...(remainingPercent === undefined ? {} : { remainingPercent }),
|
|
120
|
+
...(end === undefined ? {} : { resetAt: end }),
|
|
121
|
+
}
|
|
122
|
+
return {
|
|
123
|
+
provider: route,
|
|
124
|
+
status: 'ready',
|
|
125
|
+
kind: KIND_QUOTA,
|
|
126
|
+
...(remainingPercent === undefined ? {} : { percent: remainingPercent }),
|
|
127
|
+
...(end === undefined ? {} : { resetsAt: end }),
|
|
128
|
+
windows: [window],
|
|
129
|
+
consoleUrl: PLAN_CONSOLE,
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** The recorded CN plan body, shared by the three cluster fixtures. */
|
|
134
|
+
const RECORDED_PLAN = {
|
|
135
|
+
object: 'token_plan',
|
|
136
|
+
plan_code: 'lite',
|
|
137
|
+
period: { start_at: '2026-11-16 06:42:34', end_at: '2026-12-16 23:59:59' },
|
|
138
|
+
quota: { total_credits: 4100000000, used_credits: 4042860, remaining_credits: 4095957140 },
|
|
139
|
+
auto_renew: false,
|
|
140
|
+
claw_enabled: false,
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** What the recorded plan body must project to, on any cluster. */
|
|
144
|
+
const RECORDED_PLAN_EXPECT = {
|
|
145
|
+
status: 'ready',
|
|
146
|
+
kind: KIND_QUOTA,
|
|
147
|
+
percent: (4095957140 / 4100000000) * 100,
|
|
148
|
+
// The period is a UTC wall clock: reading it as local time would shift the
|
|
149
|
+
// reset by the host's offset and fail here.
|
|
150
|
+
resetsAt: Date.parse('2026-12-16T23:59:59Z'),
|
|
151
|
+
windows: [
|
|
152
|
+
{
|
|
153
|
+
id: 'monthly',
|
|
154
|
+
unit: 'month',
|
|
155
|
+
count: 1,
|
|
156
|
+
total: 4100000000,
|
|
157
|
+
used: 4042860,
|
|
158
|
+
remainingPercent: (4095957140 / 4100000000) * 100,
|
|
159
|
+
resetAt: Date.parse('2026-12-16T23:59:59Z'),
|
|
160
|
+
},
|
|
161
|
+
],
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Declare one Token Plan cluster adapter. The clusters share the projection
|
|
166
|
+
* and differ only in endpoint, credential, and name.
|
|
167
|
+
* @param {object} spec Cluster specification.
|
|
168
|
+
* @param {string} spec.id Adapter identity.
|
|
169
|
+
* @param {string} spec.cluster Cluster host prefix, e.g. `token-plan-cn`.
|
|
170
|
+
* @param {string} spec.credentialRef Credential reference for the bare-host fallback.
|
|
171
|
+
* @param {{zh: string, en: string}} spec.name Display name.
|
|
172
|
+
* @param {import('./schedule.js').Schedule} [spec.schedule] Pricing schedule, when this cluster has one.
|
|
173
|
+
* @param {readonly import('./shared.js').AdapterFixture[]} [spec.extraFixtures] Additional fixtures for this cluster.
|
|
174
|
+
* @returns {import('../providers.js').ProviderAdapter} The adapter.
|
|
175
|
+
*/
|
|
176
|
+
function tokenPlanAdapter({ id, cluster, credentialRef, name, schedule, extraFixtures = [] }) {
|
|
177
|
+
return defineAdapter({
|
|
178
|
+
id,
|
|
179
|
+
routes: [id],
|
|
180
|
+
kind: KIND_QUOTA,
|
|
181
|
+
endpoint: `https://${cluster}.xiaomimimo.com/v1/platform/token_plan`,
|
|
182
|
+
auth: 'bearer',
|
|
183
|
+
credentialRefs: [credentialRef],
|
|
184
|
+
read: readTokenPlan,
|
|
185
|
+
consoleUrl: PLAN_CONSOLE,
|
|
186
|
+
schedule,
|
|
187
|
+
display: {
|
|
188
|
+
name,
|
|
189
|
+
mark: { icon: 'xiaomi', color: '#1F2329' },
|
|
190
|
+
},
|
|
191
|
+
fixtures: [
|
|
192
|
+
{
|
|
193
|
+
name: 'recorded billing period',
|
|
194
|
+
response: RECORDED_PLAN,
|
|
195
|
+
apiKey: 'tp-test',
|
|
196
|
+
expect: RECORDED_PLAN_EXPECT,
|
|
197
|
+
},
|
|
198
|
+
...extraFixtures,
|
|
199
|
+
],
|
|
200
|
+
})
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** Xiaomi MiMo's wallet route and three Token Plan clusters. */
|
|
204
|
+
export const xiaomiAdapters = [
|
|
205
|
+
defineAdapter({
|
|
206
|
+
id: 'xiaomi',
|
|
207
|
+
routes: ['xiaomi'],
|
|
208
|
+
kind: KIND_AMOUNT,
|
|
209
|
+
endpoint: 'https://api.xiaomimimo.com/v1/platform/balance',
|
|
210
|
+
auth: 'bearer',
|
|
211
|
+
credentialRefs: ['XIAOMI_API_KEY'],
|
|
212
|
+
read: readBalance,
|
|
213
|
+
consoleUrl: BALANCE_CONSOLE,
|
|
214
|
+
display: {
|
|
215
|
+
name: { zh: 'Xiaomi MiMo', en: 'Xiaomi MiMo' },
|
|
216
|
+
mark: { icon: 'xiaomi', color: '#1F2329' },
|
|
217
|
+
},
|
|
218
|
+
fixtures: [
|
|
219
|
+
{
|
|
220
|
+
name: 'recorded wallet',
|
|
221
|
+
response: {
|
|
222
|
+
object: 'balance',
|
|
223
|
+
is_available: true,
|
|
224
|
+
balance_infos: [
|
|
225
|
+
{
|
|
226
|
+
currency: 'CNY',
|
|
227
|
+
cash_balance: '0.00',
|
|
228
|
+
gift_balance: '5.00',
|
|
229
|
+
frozen_balance: '0.00',
|
|
230
|
+
credit_limit: '0.00',
|
|
231
|
+
credit_available: '0.00',
|
|
232
|
+
total_available: '5.00',
|
|
233
|
+
},
|
|
234
|
+
],
|
|
235
|
+
},
|
|
236
|
+
apiKey: 'sk-test',
|
|
237
|
+
// total_available already sums the components; the reading takes it whole.
|
|
238
|
+
expect: { status: 'ready', kind: KIND_AMOUNT, amount: 5, currency: 'CNY' },
|
|
239
|
+
},
|
|
240
|
+
{
|
|
241
|
+
name: 'empty balance list',
|
|
242
|
+
response: { object: 'balance', is_available: false, balance_infos: [] },
|
|
243
|
+
expect: { status: 'failed', error: 'no balance in response' },
|
|
244
|
+
},
|
|
245
|
+
{
|
|
246
|
+
name: 'key rejected',
|
|
247
|
+
httpStatus: 401,
|
|
248
|
+
expect: { status: 'failed', error: 'HTTP 401' },
|
|
249
|
+
},
|
|
250
|
+
],
|
|
251
|
+
}),
|
|
252
|
+
tokenPlanAdapter({
|
|
253
|
+
id: 'xiaomi-token-plan-cn',
|
|
254
|
+
cluster: 'token-plan-cn',
|
|
255
|
+
credentialRef: 'XIAOMI_TOKEN_PLAN_CN_API_KEY',
|
|
256
|
+
name: { zh: 'Xiaomi MiMo Token Plan (国内)', en: 'Xiaomi MiMo Token Plan (CN)' },
|
|
257
|
+
schedule: CN_SCHEDULE,
|
|
258
|
+
// The projection is shared, so one cluster proving the failure paths
|
|
259
|
+
// proves all three.
|
|
260
|
+
extraFixtures: [
|
|
261
|
+
{
|
|
262
|
+
name: 'no quota in response',
|
|
263
|
+
response: { object: 'token_plan', plan_code: 'lite' },
|
|
264
|
+
expect: { status: 'failed', error: 'no quota in response' },
|
|
265
|
+
},
|
|
266
|
+
{
|
|
267
|
+
name: 'key rejected',
|
|
268
|
+
httpStatus: 401,
|
|
269
|
+
expect: { status: 'failed', error: 'HTTP 401' },
|
|
270
|
+
},
|
|
271
|
+
],
|
|
272
|
+
}),
|
|
273
|
+
tokenPlanAdapter({
|
|
274
|
+
id: 'xiaomi-token-plan-ams',
|
|
275
|
+
cluster: 'token-plan-ams',
|
|
276
|
+
credentialRef: 'XIAOMI_TOKEN_PLAN_AMS_API_KEY',
|
|
277
|
+
name: { zh: 'Xiaomi MiMo Token Plan (阿姆斯特丹)', en: 'Xiaomi MiMo Token Plan (AMS)' },
|
|
278
|
+
}),
|
|
279
|
+
tokenPlanAdapter({
|
|
280
|
+
id: 'xiaomi-token-plan-sgp',
|
|
281
|
+
cluster: 'token-plan-sgp',
|
|
282
|
+
credentialRef: 'XIAOMI_TOKEN_PLAN_SGP_API_KEY',
|
|
283
|
+
name: { zh: 'Xiaomi MiMo Token Plan (新加坡)', en: 'Xiaomi MiMo Token Plan (SGP)' },
|
|
284
|
+
}),
|
|
285
|
+
]
|
package/lib/providers.js
CHANGED
|
@@ -7,10 +7,11 @@
|
|
|
7
7
|
* one file under `lib/providers/`, so adding a vendor is adding a file: this
|
|
8
8
|
* module only collects them and answers "which adapter serves this route".
|
|
9
9
|
*
|
|
10
|
-
* The quantity a provider reports is not always money. DeepSeek
|
|
11
|
-
* currency balance; MiniMax (CN), Zhipu
|
|
12
|
-
*
|
|
13
|
-
* the
|
|
10
|
+
* The quantity a provider reports is not always money. DeepSeek and Xiaomi
|
|
11
|
+
* MiMo's pay-as-you-go route report a currency balance; MiniMax (CN), Zhipu
|
|
12
|
+
* ZAI, Kimi Coding, OpenCode Go, and MiMo's Token Plan clusters report a
|
|
13
|
+
* subscription allowance. The `kind` field keeps the two apart so the UI can
|
|
14
|
+
* say the right thing.
|
|
14
15
|
*/
|
|
15
16
|
|
|
16
17
|
import { deepseekAdapters } from './providers/deepseek.js'
|
|
@@ -50,12 +51,9 @@ import {
|
|
|
50
51
|
typesafeAdapters,
|
|
51
52
|
vercelAiGatewayAdapters,
|
|
52
53
|
xaiAdapters,
|
|
53
|
-
xiaomiAdapters,
|
|
54
|
-
xiaomiTokenPlanAmsAdapters,
|
|
55
|
-
xiaomiTokenPlanCnAdapters,
|
|
56
|
-
xiaomiTokenPlanSgpAdapters,
|
|
57
54
|
} from './providers/placeholder.js'
|
|
58
55
|
import { KIND_AMOUNT, KIND_QUOTA } from './providers/shared.js'
|
|
56
|
+
import { xiaomiAdapters } from './providers/xiaomi.js'
|
|
59
57
|
import { zaiAdapters } from './providers/zai.js'
|
|
60
58
|
|
|
61
59
|
/**
|
|
@@ -94,10 +92,11 @@ import { zaiAdapters } from './providers/zai.js'
|
|
|
94
92
|
* @property {readonly string[]} routes Provider routes this adapter serves.
|
|
95
93
|
* @property {readonly string[]} [modelRoutes] Host provider routes whose catalog
|
|
96
94
|
* groups list this vendor's models, when they differ from the balance route.
|
|
97
|
-
* The first one that carries models answers.
|
|
95
|
+
* The first one that carries models answers. They are the same vendor under
|
|
96
|
+
* another id, so a reading for one of them answers through this adapter.
|
|
98
97
|
* @property {'amount'|'quota'} kind Quantity dimension of the reading.
|
|
99
98
|
* @property {boolean} [modelScoped] Whether the reading is per provider-owned model.
|
|
100
|
-
* @property {string} consoleUrl Provider console for a manual check.
|
|
99
|
+
* @property {string} [consoleUrl] Provider console for a manual check.
|
|
101
100
|
* @property {string} [service] Host service this adapter reads through, when it needs no key.
|
|
102
101
|
* @property {{endpoint: string, auth: 'bearer'|'raw'}} [http] Endpoint and signing
|
|
103
102
|
* style, when the service performs the request.
|
|
@@ -127,6 +126,7 @@ export const PROVIDER_ADAPTERS = Object.freeze([
|
|
|
127
126
|
...zaiAdapters,
|
|
128
127
|
...kimiCodingAdapters,
|
|
129
128
|
...opencodeGoAdapters,
|
|
129
|
+
...xiaomiAdapters,
|
|
130
130
|
// Placeholder adapters (unsupported status)
|
|
131
131
|
...amazonBedrockAdapters,
|
|
132
132
|
...antLingAdapters,
|
|
@@ -160,14 +160,20 @@ export const PROVIDER_ADAPTERS = Object.freeze([
|
|
|
160
160
|
...typesafeAdapters,
|
|
161
161
|
...vercelAiGatewayAdapters,
|
|
162
162
|
...xaiAdapters,
|
|
163
|
-
...xiaomiAdapters,
|
|
164
|
-
...xiaomiTokenPlanAmsAdapters,
|
|
165
|
-
...xiaomiTokenPlanCnAdapters,
|
|
166
|
-
...xiaomiTokenPlanSgpAdapters,
|
|
167
163
|
])
|
|
168
164
|
|
|
169
165
|
/** Route to the adapter serving it, resolved once at load. */
|
|
170
166
|
const BY_ROUTE = new Map()
|
|
167
|
+
/**
|
|
168
|
+
* Host route id to the adapter whose vendor it stands for.
|
|
169
|
+
*
|
|
170
|
+
* A balance route and the Host route that lists the same vendor's models are
|
|
171
|
+
* not always the same id: the DeepSeek API vendor answers a balance query as
|
|
172
|
+
* `deepseek` and a model request as `deepseek-official`. The second id is the
|
|
173
|
+
* same vendor, so it reads through the same adapter — otherwise the card
|
|
174
|
+
* would report "no balance API" for a vendor whose balance it can read.
|
|
175
|
+
*/
|
|
176
|
+
const BY_MODEL_ROUTE = new Map()
|
|
171
177
|
for (const adapter of PROVIDER_ADAPTERS) {
|
|
172
178
|
for (const route of adapter.routes) {
|
|
173
179
|
// Two adapters answering the same route would make a reading depend on
|
|
@@ -177,15 +183,38 @@ for (const adapter of PROVIDER_ADAPTERS) {
|
|
|
177
183
|
}
|
|
178
184
|
BY_ROUTE.set(route, adapter)
|
|
179
185
|
}
|
|
186
|
+
for (const route of adapter.modelRoutes ?? []) {
|
|
187
|
+
// The adapter's own balance route needs no second entry.
|
|
188
|
+
if (BY_ROUTE.get(route) === adapter) continue
|
|
189
|
+
if (BY_ROUTE.has(route) || BY_MODEL_ROUTE.has(route)) {
|
|
190
|
+
throw new Error(`two provider adapters declare the route "${route}"`)
|
|
191
|
+
}
|
|
192
|
+
BY_MODEL_ROUTE.set(route, adapter)
|
|
193
|
+
}
|
|
180
194
|
}
|
|
181
195
|
|
|
182
196
|
/**
|
|
183
197
|
* Find the adapter serving one provider route.
|
|
198
|
+
*
|
|
199
|
+
* A route the harness names for the same vendor under another id is served by
|
|
200
|
+
* the same adapter, so a reading never depends on which spelling the caller
|
|
201
|
+
* happens to hold.
|
|
202
|
+
*
|
|
184
203
|
* @param {string} route Provider route, e.g. `minimax-cn`.
|
|
185
204
|
* @returns {ProviderAdapter | undefined} The adapter, when one serves the route.
|
|
186
205
|
*/
|
|
187
206
|
export function findAdapter(route) {
|
|
188
|
-
return BY_ROUTE.get(route)
|
|
207
|
+
return BY_ROUTE.get(route) ?? BY_MODEL_ROUTE.get(route)
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* The Host route ids that stand for the same vendor as one balance route.
|
|
212
|
+
*
|
|
213
|
+
* @param {ProviderAdapter} adapter The adapter.
|
|
214
|
+
* @returns {readonly string[]} The other ids of the same vendor.
|
|
215
|
+
*/
|
|
216
|
+
export function aliasesOf(adapter) {
|
|
217
|
+
return (adapter.modelRoutes ?? []).filter((route) => !adapter.routes.includes(route))
|
|
189
218
|
}
|
|
190
219
|
|
|
191
220
|
export { KIND_AMOUNT, KIND_QUOTA }
|
package/lib/remote.js
CHANGED
|
@@ -135,7 +135,9 @@ export class BalanceController extends TypertRemoteService {
|
|
|
135
135
|
* @returns {Promise<import('./providers.js').BalanceView>} The reading.
|
|
136
136
|
*/
|
|
137
137
|
async refresh(provider, model) {
|
|
138
|
-
|
|
138
|
+
// A manual refresh is the user's click, not a background trigger: it must
|
|
139
|
+
// not be held back by the failure backoff the automatic reads use.
|
|
140
|
+
return await this.ctx.modelAllowance.read(provider, { force: true, model, bypassBackoff: true })
|
|
139
141
|
}
|
|
140
142
|
|
|
141
143
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-model-allowance",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Shows the current conversation model's provider balance or remaining allowance in the composer.",
|
|
6
6
|
"license": "MIT",
|
|
@@ -67,6 +67,7 @@
|
|
|
67
67
|
"typecheck": "tsc --noEmit",
|
|
68
68
|
"prepack": "npm run build"
|
|
69
69
|
},
|
|
70
|
+
"packageManager": "pnpm@12.10.1",
|
|
70
71
|
"devDependencies": {
|
|
71
72
|
"@biomejs/biome": "^2.6.5",
|
|
72
73
|
"@deepseek-ai/cordis": "^4.1.0",
|