dsh-model-allowance 1.1.0 → 1.2.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 +23 -5
- package/README.zh.md +17 -5
- package/client.js +694 -97
- package/index.js +69 -0
- package/lib/balance.js +11 -2
- package/lib/providers/placeholder.js +0 -42
- package/lib/providers/promotions.js +169 -0
- package/lib/providers/qwen_promotions.js +223 -0
- package/lib/providers/qwen_token_plan.js +716 -0
- package/lib/providers/shared.js +24 -0
- package/lib/providers/zai.js +25 -0
- package/lib/providers.js +9 -6
- package/lib/remote.js +32 -0
- package/package.json +1 -1
package/lib/providers/shared.js
CHANGED
|
@@ -126,6 +126,8 @@ export function bindingWindow(windows) {
|
|
|
126
126
|
* @property {string} name Fixture identity, used in failure messages.
|
|
127
127
|
* @property {string} [route] Route to read; defaults to the adapter's first.
|
|
128
128
|
* @property {object} [response] Recorded JSON body, for an endpoint adapter.
|
|
129
|
+
* @property {readonly object[]} [responses] Recorded JSON bodies, one per
|
|
130
|
+
* request in order, for a read that performs several calls.
|
|
129
131
|
* @property {number} [httpStatus] Recorded HTTP status; defaults to 200.
|
|
130
132
|
* @property {Record<string, unknown>} [services] Host services the read uses,
|
|
131
133
|
* for a `readWithEnv` adapter.
|
|
@@ -153,6 +155,8 @@ export function bindingWindow(windows) {
|
|
|
153
155
|
* @param {'amount'|'quota'} spec.kind Quantity dimension of the reading.
|
|
154
156
|
* @param {boolean} [spec.modelScoped] Whether the reading is per model.
|
|
155
157
|
* @param {string} [spec.consoleUrl] Provider console for a manual check.
|
|
158
|
+
* @param {string} [spec.setupUrl] Guide that walks obtaining this provider's
|
|
159
|
+
* credentials, carried by the unconfigured reading so the card can link it.
|
|
156
160
|
* @param {{name: {zh: string, en: string}, mark: {icon?: string, color: string}}} spec.display
|
|
157
161
|
* Display metadata the browser needs to name and draw the route.
|
|
158
162
|
* @param {string} [spec.service] Host service this adapter reads through.
|
|
@@ -163,6 +167,10 @@ export function bindingWindow(windows) {
|
|
|
163
167
|
* @param {{endpoint: string, url: string, parse: (env: {text?: string, json?: unknown}) => {state: string, summary?: string}}} [spec.status]
|
|
164
168
|
* Live platform status source, when the vendor publishes one.
|
|
165
169
|
* @param {import('./schedule.js').Schedule} [spec.schedule] Time-of-day pricing schedule.
|
|
170
|
+
* @param {readonly import('./promotions.js').Promotion[]} [spec.promotions]
|
|
171
|
+
* Model-scoped, date-limited usage promotions, when the vendor declares any.
|
|
172
|
+
* @param {(env: {signal: AbortSignal}) => Promise<readonly import('./promotions.js').Promotion[]>} [spec.readPromotions]
|
|
173
|
+
* Live promotions read (a public catalog), when the vendor publishes one.
|
|
166
174
|
* @param {(env: {route: string, apiKey: string, body: Record<string, unknown>, model?: string}) => import('../providers.js').BalanceView} [spec.read]
|
|
167
175
|
* Projection for an endpoint adapter.
|
|
168
176
|
* @param {(env: import('../providers.js').AdapterEnv) => Promise<import('../providers.js').BalanceView>} [spec.readWithEnv]
|
|
@@ -190,6 +198,11 @@ export function defineAdapter(spec) {
|
|
|
190
198
|
throw new Error(`${where} needs an https console link`)
|
|
191
199
|
}
|
|
192
200
|
}
|
|
201
|
+
if (spec.setupUrl !== undefined) {
|
|
202
|
+
if (typeof spec.setupUrl !== 'string' || !spec.setupUrl.startsWith('https://')) {
|
|
203
|
+
throw new Error(`${where} needs an https setup guide link`)
|
|
204
|
+
}
|
|
205
|
+
}
|
|
193
206
|
if (spec.status !== undefined) {
|
|
194
207
|
if (typeof spec.status !== 'object' || spec.status === null) {
|
|
195
208
|
throw new Error(`${where} declares an unreadable status source`)
|
|
@@ -245,6 +258,12 @@ export function defineAdapter(spec) {
|
|
|
245
258
|
) {
|
|
246
259
|
throw new Error(`${where} declares an unreadable schedule`)
|
|
247
260
|
}
|
|
261
|
+
if (spec.promotions !== undefined && !Array.isArray(spec.promotions)) {
|
|
262
|
+
throw new Error(`${where} declares unreadable promotions`)
|
|
263
|
+
}
|
|
264
|
+
if (spec.readPromotions !== undefined && typeof spec.readPromotions !== 'function') {
|
|
265
|
+
throw new Error(`${where} declares an unreadable promotions source`)
|
|
266
|
+
}
|
|
248
267
|
const fixtures = spec.fixtures ?? []
|
|
249
268
|
if (!Array.isArray(fixtures) || fixtures.length === 0) {
|
|
250
269
|
throw new Error(`${where} ships no fixture, so nothing would prove it still parses`)
|
|
@@ -259,6 +278,7 @@ export function defineAdapter(spec) {
|
|
|
259
278
|
kind: spec.kind,
|
|
260
279
|
...(spec.modelScoped === true ? { modelScoped: true } : {}),
|
|
261
280
|
...(spec.consoleUrl === undefined ? {} : { consoleUrl: spec.consoleUrl }),
|
|
281
|
+
...(spec.setupUrl === undefined ? {} : { setupUrl: spec.setupUrl }),
|
|
262
282
|
service: spec.service,
|
|
263
283
|
http: hasEndpoint ? Object.freeze({ endpoint: spec.endpoint, auth: spec.auth }) : undefined,
|
|
264
284
|
credentialRefs: Object.freeze([...(spec.credentialRefs ?? [])]),
|
|
@@ -280,6 +300,10 @@ export function defineAdapter(spec) {
|
|
|
280
300
|
parse: spec.status.parse,
|
|
281
301
|
}),
|
|
282
302
|
schedule: spec.schedule,
|
|
303
|
+
...(spec.promotions === undefined || spec.promotions.length === 0
|
|
304
|
+
? {}
|
|
305
|
+
: { promotions: Object.freeze([...spec.promotions]) }),
|
|
306
|
+
...(spec.readPromotions === undefined ? {} : { readPromotions: spec.readPromotions }),
|
|
283
307
|
read: spec.read,
|
|
284
308
|
readWithEnv: spec.readWithEnv,
|
|
285
309
|
fixtures: Object.freeze(fixtures.map((fixture) => Object.freeze({ ...fixture }))),
|
package/lib/providers/zai.js
CHANGED
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
* them, which the balance service now discovers per route.
|
|
13
13
|
*/
|
|
14
14
|
|
|
15
|
+
import { definePromotions } from './promotions.js'
|
|
15
16
|
import { defineSchedule } from './schedule.js'
|
|
16
17
|
import { asObject, bindingWindow, defineAdapter, KIND_QUOTA, parseAmount } from './shared.js'
|
|
17
18
|
|
|
@@ -41,6 +42,29 @@ const SCHEDULE = defineSchedule({
|
|
|
41
42
|
note: { zh: '低谷时段积分按 50% 抵扣', en: 'Credits are halved off-peak' },
|
|
42
43
|
})
|
|
43
44
|
|
|
45
|
+
/**
|
|
46
|
+
* GLM's limited-time double-usage window for the Flash model. The note is the
|
|
47
|
+
* vendor's announcement verbatim: the switch menu shows only a small ×2 badge
|
|
48
|
+
* on the covered model's row, and hovering the badge reads this one line.
|
|
49
|
+
* The brand spelling is what the badge tip displays; matching against the
|
|
50
|
+
* catalog's lowercase id is case-insensitive.
|
|
51
|
+
*
|
|
52
|
+
* @type {readonly import('./promotions.js').Promotion[]}
|
|
53
|
+
*/
|
|
54
|
+
const PROMOTIONS = definePromotions([
|
|
55
|
+
{
|
|
56
|
+
models: ['GLM-5.3-Flash'],
|
|
57
|
+
chip: { zh: '×2', en: '×2' },
|
|
58
|
+
timezone: 'Asia/Shanghai',
|
|
59
|
+
from: '2026-11-24',
|
|
60
|
+
to: '2026-12-24',
|
|
61
|
+
note: {
|
|
62
|
+
zh: '11月24日至12月24日,GLM-5.3-Flash 限时双倍用量。',
|
|
63
|
+
en: 'Nov 24 – Dec 24: limited-time double usage for GLM-5.3-Flash.',
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
])
|
|
67
|
+
|
|
44
68
|
/**
|
|
45
69
|
* Project one Zhipu limit body.
|
|
46
70
|
* @param {{route: string, body: Record<string, unknown>}} env Projection context.
|
|
@@ -115,6 +139,7 @@ export const zaiAdapters = [
|
|
|
115
139
|
read: readQuota,
|
|
116
140
|
consoleUrl: CONSOLE,
|
|
117
141
|
schedule: SCHEDULE,
|
|
142
|
+
promotions: PROMOTIONS,
|
|
118
143
|
display: {
|
|
119
144
|
name: { zh: '智谱 ZAI', en: 'Zhipu ZAI' },
|
|
120
145
|
mark: { icon: 'zhipu', color: '#5668f5' },
|
package/lib/providers.js
CHANGED
|
@@ -43,15 +43,13 @@ import {
|
|
|
43
43
|
openaiCodexAdapters,
|
|
44
44
|
opencodeAdapters,
|
|
45
45
|
openrouterAdapters,
|
|
46
|
-
qwenTokenPlanAdapters,
|
|
47
|
-
qwenTokenPlanCnAdapters,
|
|
48
|
-
qwenTokenPlanIndividualAdapters,
|
|
49
46
|
radiusAdapters,
|
|
50
47
|
togetherAdapters,
|
|
51
48
|
typesafeAdapters,
|
|
52
49
|
vercelAiGatewayAdapters,
|
|
53
50
|
xaiAdapters,
|
|
54
51
|
} from './providers/placeholder.js'
|
|
52
|
+
import { qwenTokenPlanAdapters } from './providers/qwen_token_plan.js'
|
|
55
53
|
import { KIND_AMOUNT, KIND_QUOTA } from './providers/shared.js'
|
|
56
54
|
import { xiaomiAdapters } from './providers/xiaomi.js'
|
|
57
55
|
import { zaiAdapters } from './providers/zai.js'
|
|
@@ -71,6 +69,8 @@ import { zaiAdapters } from './providers/zai.js'
|
|
|
71
69
|
* @property {readonly import('./providers/shared.js').BalanceWindow[]} [windows] Per-window detail for a metered plan.
|
|
72
70
|
* @property {{kind: 'amount'|'percent', delta: number, at: number}} [consumption] Movement since the previous reading.
|
|
73
71
|
* @property {string} [consoleUrl] Provider console for a manual top-up or quota check.
|
|
72
|
+
* @property {string} [setupUrl] Guide that walks obtaining the credentials the
|
|
73
|
+
* reading needs, for `status === 'unconfigured'`.
|
|
74
74
|
* @property {number} [updatedAt] Epoch milliseconds of the reading.
|
|
75
75
|
* @property {string} [error] Failure detail for `status === 'failed'`.
|
|
76
76
|
*/
|
|
@@ -97,6 +97,7 @@ import { zaiAdapters } from './providers/zai.js'
|
|
|
97
97
|
* @property {'amount'|'quota'} kind Quantity dimension of the reading.
|
|
98
98
|
* @property {boolean} [modelScoped] Whether the reading is per provider-owned model.
|
|
99
99
|
* @property {string} [consoleUrl] Provider console for a manual check.
|
|
100
|
+
* @property {string} [setupUrl] Guide that walks obtaining this provider's credentials.
|
|
100
101
|
* @property {string} [service] Host service this adapter reads through, when it needs no key.
|
|
101
102
|
* @property {{endpoint: string, auth: 'bearer'|'raw'}} [http] Endpoint and signing
|
|
102
103
|
* style, when the service performs the request.
|
|
@@ -108,6 +109,10 @@ import { zaiAdapters } from './providers/zai.js'
|
|
|
108
109
|
* Live platform status source, when the vendor publishes one.
|
|
109
110
|
* @property {import('./providers/schedule.js').Schedule} [schedule]
|
|
110
111
|
* Time-of-day pricing schedule, when the vendor prices by wall clock.
|
|
112
|
+
* @property {readonly import('./providers/promotions.js').Promotion[]} [promotions]
|
|
113
|
+
* Model-scoped, date-limited usage promotions, when the vendor declares any.
|
|
114
|
+
* @property {(env: {signal: AbortSignal}) => Promise<readonly import('./providers/promotions.js').Promotion[]>} [readPromotions]
|
|
115
|
+
* Live promotions read (a public catalog), when the vendor publishes one.
|
|
111
116
|
* @property {(env: {route: string, apiKey: string, body: Record<string, unknown>, model?: string}) => BalanceView} [read]
|
|
112
117
|
* Projection for an endpoint adapter.
|
|
113
118
|
* @property {(env: AdapterEnv) => Promise<BalanceView>} [readWithEnv] Full read for an
|
|
@@ -127,6 +132,7 @@ export const PROVIDER_ADAPTERS = Object.freeze([
|
|
|
127
132
|
...kimiCodingAdapters,
|
|
128
133
|
...opencodeGoAdapters,
|
|
129
134
|
...xiaomiAdapters,
|
|
135
|
+
...qwenTokenPlanAdapters,
|
|
130
136
|
// Placeholder adapters (unsupported status)
|
|
131
137
|
...amazonBedrockAdapters,
|
|
132
138
|
...antLingAdapters,
|
|
@@ -152,9 +158,6 @@ export const PROVIDER_ADAPTERS = Object.freeze([
|
|
|
152
158
|
...openaiCodexAdapters,
|
|
153
159
|
...opencodeAdapters,
|
|
154
160
|
...openrouterAdapters,
|
|
155
|
-
...qwenTokenPlanAdapters,
|
|
156
|
-
...qwenTokenPlanCnAdapters,
|
|
157
|
-
...qwenTokenPlanIndividualAdapters,
|
|
158
161
|
...radiusAdapters,
|
|
159
162
|
...togetherAdapters,
|
|
160
163
|
...typesafeAdapters,
|
package/lib/remote.js
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import { Remote, TypertRemoteService } from '@deepseek-ai/dsh-typert-protocol'
|
|
10
|
+
import { testQwenPlanCredentials } from './providers/qwen_token_plan.js'
|
|
10
11
|
|
|
11
12
|
/**
|
|
12
13
|
* Apply the protocol's `@Remote` marker to a method without decorator syntax.
|
|
@@ -89,6 +90,22 @@ export class BalanceController extends TypertRemoteService {
|
|
|
89
90
|
return await this.ctx.modelAllowance.list()
|
|
90
91
|
}
|
|
91
92
|
|
|
93
|
+
/**
|
|
94
|
+
* Walk a provider's credential chain against the live endpoints.
|
|
95
|
+
*
|
|
96
|
+
* The settings page calls this with the AccessKey pair the user entered,
|
|
97
|
+
* before it saves: a stored credential is one the vendor accepted. The
|
|
98
|
+
* values travel to the vendor's own endpoints and nowhere else, and are
|
|
99
|
+
* never written to the store by this call.
|
|
100
|
+
*
|
|
101
|
+
* @param {string} accessKeyId AccessKey ID candidate.
|
|
102
|
+
* @param {string} secretAccessKey AccessKey secret candidate.
|
|
103
|
+
* @returns {Promise<{ok: true} | {ok: false, error: string}>} The outcome.
|
|
104
|
+
*/
|
|
105
|
+
async testCredentials(accessKeyId, secretAccessKey) {
|
|
106
|
+
return await testQwenPlanCredentials({ accessKeyId, secretAccessKey })
|
|
107
|
+
}
|
|
108
|
+
|
|
92
109
|
/**
|
|
93
110
|
* Read one provider's platform status.
|
|
94
111
|
*
|
|
@@ -115,6 +132,19 @@ export class BalanceController extends TypertRemoteService {
|
|
|
115
132
|
return this.ctx.modelAllowance.schedule(provider)
|
|
116
133
|
}
|
|
117
134
|
|
|
135
|
+
/**
|
|
136
|
+
* The model-scoped usage promotions a vendor declares, when it has any.
|
|
137
|
+
*
|
|
138
|
+
* Stable declared data, like the schedule: the browser decides which
|
|
139
|
+
* promotions its own clock falls inside and marks the covered models.
|
|
140
|
+
*
|
|
141
|
+
* @param {string} provider Provider route.
|
|
142
|
+
* @returns {Promise<object | null>} The declarations, or null.
|
|
143
|
+
*/
|
|
144
|
+
async promotions(provider) {
|
|
145
|
+
return this.ctx.modelAllowance.promotions(provider)
|
|
146
|
+
}
|
|
147
|
+
|
|
118
148
|
/**
|
|
119
149
|
* The provider a session with no model selection would use.
|
|
120
150
|
*
|
|
@@ -208,8 +238,10 @@ BalanceController.__applyRemoteMarkers = [
|
|
|
208
238
|
markRemote(BalanceController.prototype, 'capabilities'),
|
|
209
239
|
markRemote(BalanceController.prototype, 'balance'),
|
|
210
240
|
markRemote(BalanceController.prototype, 'list'),
|
|
241
|
+
markRemote(BalanceController.prototype, 'testCredentials'),
|
|
211
242
|
markRemote(BalanceController.prototype, 'status'),
|
|
212
243
|
markRemote(BalanceController.prototype, 'schedule'),
|
|
244
|
+
markRemote(BalanceController.prototype, 'promotions'),
|
|
213
245
|
markRemote(BalanceController.prototype, 'defaultProvider'),
|
|
214
246
|
markRemote(BalanceController.prototype, 'refresh'),
|
|
215
247
|
markRemote(BalanceController.prototype, 'watch', { mode: 'stream' }),
|