@rayadesu/dsh-billing 0.1.0-rc.8
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/AGENTS.md +54 -0
- package/LICENSE +21 -0
- package/README.i18n.yaml +6 -0
- package/README.md +118 -0
- package/README.zh.md +114 -0
- package/cordis.patch.yml +20 -0
- package/package.json +33 -0
- package/packages/llm-billing/README.i18n.yaml +6 -0
- package/packages/llm-billing/README.md +44 -0
- package/packages/llm-billing/README.zh.md +44 -0
- package/packages/llm-billing/lib/index.js +562 -0
- package/packages/llm-billing/lib/invariant.js +23 -0
- package/packages/llm-billing/lib/tsconfig.tsbuildinfo +1 -0
- package/packages/llm-billing/lib/typert.host.d.ts +3 -0
- package/packages/llm-billing/lib/typert.host.js +114 -0
- package/packages/llm-billing/lib/typert.remote-client.d.ts +27 -0
- package/packages/llm-billing/lib/typert.remote-client.d.ts.map +1 -0
- package/packages/llm-billing/lib/typert.remote-client.js +108 -0
- package/packages/llm-billing/lib/types/balance.d.ts +71 -0
- package/packages/llm-billing/lib/types/balance.d.ts.map +1 -0
- package/packages/llm-billing/lib/types/balance.js +187 -0
- package/packages/llm-billing/lib/types/balance.js.map +1 -0
- package/packages/llm-billing/lib/types/billing.d.ts +106 -0
- package/packages/llm-billing/lib/types/billing.d.ts.map +1 -0
- package/packages/llm-billing/lib/types/billing.js +163 -0
- package/packages/llm-billing/lib/types/billing.js.map +1 -0
- package/packages/llm-billing/lib/types/index.d.ts +49 -0
- package/packages/llm-billing/lib/types/index.d.ts.map +1 -0
- package/packages/llm-billing/lib/types/index.js +155 -0
- package/packages/llm-billing/lib/types/index.js.map +1 -0
- package/packages/llm-billing/lib/types/invariant.d.ts +16 -0
- package/packages/llm-billing/lib/types/invariant.d.ts.map +1 -0
- package/packages/llm-billing/lib/types/invariant.js +22 -0
- package/packages/llm-billing/lib/types/invariant.js.map +1 -0
- package/packages/llm-billing/lib/types/types/balance.d.ts +71 -0
- package/packages/llm-billing/lib/types/types/balance.d.ts.map +1 -0
- package/packages/llm-billing/lib/types/types/balance.js +187 -0
- package/packages/llm-billing/lib/types/types/balance.js.map +1 -0
- package/packages/llm-billing/lib/types/types/billing.d.ts +106 -0
- package/packages/llm-billing/lib/types/types/billing.d.ts.map +1 -0
- package/packages/llm-billing/lib/types/types/billing.js +170 -0
- package/packages/llm-billing/lib/types/types/billing.js.map +1 -0
- package/packages/llm-billing/lib/types/types/index.d.ts +49 -0
- package/packages/llm-billing/lib/types/types/index.d.ts.map +1 -0
- package/packages/llm-billing/lib/types/types/index.js +156 -0
- package/packages/llm-billing/lib/types/types/index.js.map +1 -0
- package/packages/llm-billing/lib/types/types/invariant.d.ts +16 -0
- package/packages/llm-billing/lib/types/types/invariant.d.ts.map +1 -0
- package/packages/llm-billing/lib/types/types/invariant.js +22 -0
- package/packages/llm-billing/lib/types/types/invariant.js.map +1 -0
- package/packages/llm-billing/lib/types/types/types.d.ts +63 -0
- package/packages/llm-billing/lib/types/types/types.d.ts.map +1 -0
- package/packages/llm-billing/lib/types/types/types.js +7 -0
- package/packages/llm-billing/lib/types/types/types.js.map +1 -0
- package/packages/llm-billing/lib/types/types.d.ts +63 -0
- package/packages/llm-billing/lib/types/types.d.ts.map +1 -0
- package/packages/llm-billing/lib/types/types.js +7 -0
- package/packages/llm-billing/lib/types/types.js.map +1 -0
- package/packages/llm-billing/node_modules/.bin/cordis +43 -0
- package/packages/llm-billing/node_modules/.bin/cordis.CMD +12 -0
- package/packages/llm-billing/node_modules/.bin/cordis.ps1 +41 -0
- package/packages/llm-billing/package.json +75 -0
- package/packages/llm-billing/src/balance.ts +164 -0
- package/packages/llm-billing/src/billing.ts +251 -0
- package/packages/llm-billing/src/index.ts +219 -0
- package/packages/llm-billing/src/invariant.ts +30 -0
- package/packages/llm-billing/src/types.ts +67 -0
- package/packages/llm-billing/tests/balance.spec.ts +254 -0
- package/packages/llm-billing/tests/billing.spec.ts +216 -0
- package/packages/llm-billing/tsconfig.json +42 -0
- package/packages/ui-billing/README.i18n.yaml +6 -0
- package/packages/ui-billing/README.md +30 -0
- package/packages/ui-billing/README.zh.md +30 -0
- package/packages/ui-billing/lib/client.js +4466 -0
- package/packages/ui-billing/lib/client.js.map +1 -0
- package/packages/ui-billing/lib/index.js +11 -0
- package/packages/ui-billing/lib/invariant.js +25 -0
- package/packages/ui-billing/lib/tsconfig.tsbuildinfo +1 -0
- package/packages/ui-billing/lib/types/client/BalanceBadge.d.ts +33 -0
- package/packages/ui-billing/lib/types/client/BalanceBadge.d.ts.map +1 -0
- package/packages/ui-billing/lib/types/client/BalanceBadge.js +166 -0
- package/packages/ui-billing/lib/types/client/BalanceBadge.js.map +1 -0
- package/packages/ui-billing/lib/types/client/index.d.ts +20 -0
- package/packages/ui-billing/lib/types/client/index.d.ts.map +1 -0
- package/packages/ui-billing/lib/types/client/index.js +55 -0
- package/packages/ui-billing/lib/types/client/index.js.map +1 -0
- package/packages/ui-billing/lib/types/client/locales.d.ts +26 -0
- package/packages/ui-billing/lib/types/client/locales.d.ts.map +1 -0
- package/packages/ui-billing/lib/types/client/locales.js +40 -0
- package/packages/ui-billing/lib/types/client/locales.js.map +1 -0
- package/packages/ui-billing/lib/types/index.d.ts +9 -0
- package/packages/ui-billing/lib/types/index.d.ts.map +1 -0
- package/packages/ui-billing/lib/types/index.js +9 -0
- package/packages/ui-billing/lib/types/index.js.map +1 -0
- package/packages/ui-billing/lib/types/invariant.d.ts +16 -0
- package/packages/ui-billing/lib/types/invariant.d.ts.map +1 -0
- package/packages/ui-billing/lib/types/invariant.js +24 -0
- package/packages/ui-billing/lib/types/invariant.js.map +1 -0
- package/packages/ui-billing/node_modules/.bin/cordis +43 -0
- package/packages/ui-billing/node_modules/.bin/cordis.CMD +12 -0
- package/packages/ui-billing/node_modules/.bin/cordis.ps1 +41 -0
- package/packages/ui-billing/node_modules/.bin/vite +43 -0
- package/packages/ui-billing/node_modules/.bin/vite.CMD +12 -0
- package/packages/ui-billing/node_modules/.bin/vite.ps1 +41 -0
- package/packages/ui-billing/package.json +76 -0
- package/packages/ui-billing/src/client/BalanceBadge.module.css +184 -0
- package/packages/ui-billing/src/client/BalanceBadge.tsx +266 -0
- package/packages/ui-billing/src/client/index.ts +80 -0
- package/packages/ui-billing/src/client/locales.ts +45 -0
- package/packages/ui-billing/src/css-modules.d.ts +6 -0
- package/packages/ui-billing/src/index.ts +9 -0
- package/packages/ui-billing/src/invariant.ts +32 -0
- package/packages/ui-billing/tests/balance-badge.client.spec.tsx +267 -0
- package/packages/ui-billing/tests/browser-plugin.client.spec.ts +205 -0
- package/packages/ui-billing/tsconfig.json +36 -0
- package/packages/ui-billing/tsdown.config.ts +3 -0
|
@@ -0,0 +1,562 @@
|
|
|
1
|
+
import z from "@deepseek-ai/schemastery";
|
|
2
|
+
import { LlmError, assertUsableApiKey } from "@deepseek-ai/dsh-llm";
|
|
3
|
+
import { credentialRef } from "@deepseek-ai/dsh-credentials";
|
|
4
|
+
import { launchEnvironmentOf } from "@deepseek-ai/dsh-launch-environment";
|
|
5
|
+
import { Remote, TypertRemoteService } from "@deepseek-ai/dsh-typert-protocol";
|
|
6
|
+
//#region lib/types/balance.js
|
|
7
|
+
/**
|
|
8
|
+
* DeepSeek account-balance capability: the `GET /user/balance` transport and
|
|
9
|
+
* the Remote gateway that exposes one snapshot to trusted clients. The fetch
|
|
10
|
+
* takes an already-resolved endpoint and bearer token so the registering
|
|
11
|
+
* plugin stays the one owner of credential policy; the gateway carries only a
|
|
12
|
+
* `fetchBalance` thunk for the same reason.
|
|
13
|
+
* @module @rayadesu/dsh-llm-billing/balance
|
|
14
|
+
*/
|
|
15
|
+
var __runInitializers = function(thisArg, initializers, value) {
|
|
16
|
+
var useValue = arguments.length > 2;
|
|
17
|
+
for (var i = 0; i < initializers.length; i++) value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
|
18
|
+
return useValue ? value : void 0;
|
|
19
|
+
};
|
|
20
|
+
var __esDecorate = function(ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
|
21
|
+
function accept(f) {
|
|
22
|
+
if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected");
|
|
23
|
+
return f;
|
|
24
|
+
}
|
|
25
|
+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
|
26
|
+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
|
27
|
+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
|
28
|
+
var _, done = false;
|
|
29
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
30
|
+
var context = {};
|
|
31
|
+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
|
32
|
+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
|
33
|
+
context.addInitializer = function(f) {
|
|
34
|
+
if (done) throw new TypeError("Cannot add initializers after decoration has completed");
|
|
35
|
+
extraInitializers.push(accept(f || null));
|
|
36
|
+
};
|
|
37
|
+
var result = (0, decorators[i])(kind === "accessor" ? {
|
|
38
|
+
get: descriptor.get,
|
|
39
|
+
set: descriptor.set
|
|
40
|
+
} : descriptor[key], context);
|
|
41
|
+
if (kind === "accessor") {
|
|
42
|
+
if (result === void 0) continue;
|
|
43
|
+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
|
44
|
+
if (_ = accept(result.get)) descriptor.get = _;
|
|
45
|
+
if (_ = accept(result.set)) descriptor.set = _;
|
|
46
|
+
if (_ = accept(result.init)) initializers.unshift(_);
|
|
47
|
+
} else if (_ = accept(result)) if (kind === "field") initializers.unshift(_);
|
|
48
|
+
else descriptor[key] = _;
|
|
49
|
+
}
|
|
50
|
+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
|
51
|
+
done = true;
|
|
52
|
+
};
|
|
53
|
+
/** Map a balance HTTP status to a stable LlmError code. */
|
|
54
|
+
function httpErrorCode(status) {
|
|
55
|
+
if (status === 401 || status === 403) return "AUTH";
|
|
56
|
+
if (status === 429) return "RATE_LIMIT";
|
|
57
|
+
if (status >= 500) return "SERVER";
|
|
58
|
+
return `HTTP_${status}`;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Parse and validate the DeepSeek balance response at the wire boundary.
|
|
62
|
+
* @param body - decoded JSON response body.
|
|
63
|
+
* @returns the validated, detached balance snapshot.
|
|
64
|
+
* @throws {@link LlmError} with code `TRANSPORT` when the body is malformed.
|
|
65
|
+
*/
|
|
66
|
+
function parseDeepSeekBalance(body) {
|
|
67
|
+
if (typeof body !== "object" || body === null || Array.isArray(body)) throw new LlmError("DeepSeek balance response was not a JSON object", "TRANSPORT");
|
|
68
|
+
const response = body;
|
|
69
|
+
const isAvailable = response["is_available"];
|
|
70
|
+
const infos = response["balance_infos"];
|
|
71
|
+
if (typeof isAvailable !== "boolean" || !Array.isArray(infos)) throw new LlmError("DeepSeek balance response is missing is_available or balance_infos", "TRANSPORT");
|
|
72
|
+
return {
|
|
73
|
+
isAvailable,
|
|
74
|
+
lines: infos.map((info, index) => {
|
|
75
|
+
if (typeof info !== "object" || info === null || Array.isArray(info)) throw new LlmError(`DeepSeek balance line ${index} is malformed`, "TRANSPORT");
|
|
76
|
+
const line = info;
|
|
77
|
+
const currency = line["currency"];
|
|
78
|
+
const total = line["total_balance"];
|
|
79
|
+
const granted = line["granted_balance"];
|
|
80
|
+
const toppedUp = line["topped_up_balance"];
|
|
81
|
+
if (typeof currency !== "string" || currency.length === 0 || typeof total !== "string" || typeof granted !== "string" || typeof toppedUp !== "string") throw new LlmError(`DeepSeek balance line ${index} has missing or invalid fields`, "TRANSPORT");
|
|
82
|
+
return {
|
|
83
|
+
currency,
|
|
84
|
+
total,
|
|
85
|
+
granted,
|
|
86
|
+
toppedUp
|
|
87
|
+
};
|
|
88
|
+
})
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Fetch one account-balance snapshot from `{baseURL}/user/balance`.
|
|
93
|
+
* @param baseURL - resolved endpoint base; `/user/balance` is appended.
|
|
94
|
+
* @param apiKey - resolved bearer token for this endpoint.
|
|
95
|
+
* @param signal - optional cancellation.
|
|
96
|
+
* @returns the validated balance snapshot.
|
|
97
|
+
* @throws {@link LlmError} for transport, HTTP, or malformed-response failures.
|
|
98
|
+
*/
|
|
99
|
+
async function fetchDeepSeekBalance(baseURL, apiKey, signal) {
|
|
100
|
+
let response;
|
|
101
|
+
try {
|
|
102
|
+
response = await fetch(`${baseURL}/user/balance`, {
|
|
103
|
+
method: "GET",
|
|
104
|
+
headers: {
|
|
105
|
+
"authorization": `Bearer ${apiKey}`,
|
|
106
|
+
"accept": "application/json"
|
|
107
|
+
},
|
|
108
|
+
...signal === void 0 ? {} : { signal }
|
|
109
|
+
});
|
|
110
|
+
} catch (error) {
|
|
111
|
+
if (signal?.aborted) throw new LlmError("DeepSeek balance request aborted by caller", "ABORTED", { cause: error });
|
|
112
|
+
throw new LlmError(`DeepSeek balance request to ${baseURL} failed`, "TRANSPORT", { cause: error });
|
|
113
|
+
}
|
|
114
|
+
if (!response.ok) throw new LlmError(`DeepSeek balance request failed (HTTP ${response.status})`, httpErrorCode(response.status), { status: response.status });
|
|
115
|
+
let body;
|
|
116
|
+
try {
|
|
117
|
+
body = await response.json();
|
|
118
|
+
} catch (error) {
|
|
119
|
+
throw new LlmError("DeepSeek balance response was not valid JSON", "TRANSPORT", { cause: error });
|
|
120
|
+
}
|
|
121
|
+
return parseDeepSeekBalance(body);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Remote-only service exposing the DeepSeek account balance and session spend.
|
|
125
|
+
* The plugin that owns connection, credential, and session-history resolution
|
|
126
|
+
* constructs it with the matching thunks, so the Remote boundary never sees an
|
|
127
|
+
* endpoint, key, or the session store.
|
|
128
|
+
*/
|
|
129
|
+
let DeepSeekBalanceGateway = (() => {
|
|
130
|
+
let _classSuper = TypertRemoteService;
|
|
131
|
+
let _instanceExtraInitializers = [];
|
|
132
|
+
let _getBalance_decorators;
|
|
133
|
+
let _getSessionSpend_decorators;
|
|
134
|
+
let _getTodaySpend_decorators;
|
|
135
|
+
return class DeepSeekBalanceGateway extends _classSuper {
|
|
136
|
+
static {
|
|
137
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
|
|
138
|
+
_getBalance_decorators = [Remote("getBalance")];
|
|
139
|
+
_getSessionSpend_decorators = [Remote("getSessionSpend")];
|
|
140
|
+
_getTodaySpend_decorators = [Remote("getTodaySpend")];
|
|
141
|
+
__esDecorate(this, null, _getBalance_decorators, {
|
|
142
|
+
kind: "method",
|
|
143
|
+
name: "getBalance",
|
|
144
|
+
static: false,
|
|
145
|
+
private: false,
|
|
146
|
+
access: {
|
|
147
|
+
has: (obj) => "getBalance" in obj,
|
|
148
|
+
get: (obj) => obj.getBalance
|
|
149
|
+
},
|
|
150
|
+
metadata: _metadata
|
|
151
|
+
}, null, _instanceExtraInitializers);
|
|
152
|
+
__esDecorate(this, null, _getSessionSpend_decorators, {
|
|
153
|
+
kind: "method",
|
|
154
|
+
name: "getSessionSpend",
|
|
155
|
+
static: false,
|
|
156
|
+
private: false,
|
|
157
|
+
access: {
|
|
158
|
+
has: (obj) => "getSessionSpend" in obj,
|
|
159
|
+
get: (obj) => obj.getSessionSpend
|
|
160
|
+
},
|
|
161
|
+
metadata: _metadata
|
|
162
|
+
}, null, _instanceExtraInitializers);
|
|
163
|
+
__esDecorate(this, null, _getTodaySpend_decorators, {
|
|
164
|
+
kind: "method",
|
|
165
|
+
name: "getTodaySpend",
|
|
166
|
+
static: false,
|
|
167
|
+
private: false,
|
|
168
|
+
access: {
|
|
169
|
+
has: (obj) => "getTodaySpend" in obj,
|
|
170
|
+
get: (obj) => obj.getTodaySpend
|
|
171
|
+
},
|
|
172
|
+
metadata: _metadata
|
|
173
|
+
}, null, _instanceExtraInitializers);
|
|
174
|
+
if (_metadata) Object.defineProperty(this, Symbol.metadata, {
|
|
175
|
+
enumerable: true,
|
|
176
|
+
configurable: true,
|
|
177
|
+
writable: true,
|
|
178
|
+
value: _metadata
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
options = __runInitializers(this, _instanceExtraInitializers);
|
|
182
|
+
/**
|
|
183
|
+
* Register the balance Remote under the `billing` namespace.
|
|
184
|
+
* @param ctx - owning plugin context.
|
|
185
|
+
* @param options - balance and spend thunks bound to the plugin's facts.
|
|
186
|
+
*/
|
|
187
|
+
constructor(ctx, options) {
|
|
188
|
+
super(ctx, "billing");
|
|
189
|
+
this.options = options;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Read the current DeepSeek account balance.
|
|
193
|
+
* @returns the validated balance snapshot.
|
|
194
|
+
*/
|
|
195
|
+
getBalance() {
|
|
196
|
+
return this.options.fetchBalance();
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Read one session's billed spend, priced per event by its peak/off-peak hour.
|
|
200
|
+
* @param sessionId - the session whose spend to compute.
|
|
201
|
+
* @returns the session's total cost plus one row per priced model.
|
|
202
|
+
*/
|
|
203
|
+
getSessionSpend(sessionId) {
|
|
204
|
+
return this.options.fetchSessionSpend(sessionId);
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Read today's billed spend across every session, priced per event by its
|
|
208
|
+
* Beijing-time calendar day and peak/off-peak hour.
|
|
209
|
+
* @returns today's total cost plus one row per priced model.
|
|
210
|
+
*/
|
|
211
|
+
getTodaySpend() {
|
|
212
|
+
return this.options.fetchTodaySpend();
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
})();
|
|
216
|
+
//#endregion
|
|
217
|
+
//#region lib/types/billing.js
|
|
218
|
+
/**
|
|
219
|
+
* DeepSeek billing: the peak/off-peak pricing table and the per-session spend
|
|
220
|
+
* pricing. Pure functions over session events and the pricing table, so the
|
|
221
|
+
* Remote gateway stays transport-free and the whole spend is testable without
|
|
222
|
+
* a key.
|
|
223
|
+
* @module @rayadesu/dsh-llm-billing/billing
|
|
224
|
+
*/
|
|
225
|
+
/** Published peak-hour windows (Beijing time): 09:00–12:00 and 14:00–18:00. */
|
|
226
|
+
const DEFAULT_PEAK_HOURS = [{
|
|
227
|
+
start: 9,
|
|
228
|
+
end: 12
|
|
229
|
+
}, {
|
|
230
|
+
start: 14,
|
|
231
|
+
end: 18
|
|
232
|
+
}];
|
|
233
|
+
/** Official peak/off-peak rates (CNY per 1M tokens), effective 2026-08-17. */
|
|
234
|
+
const DEFAULT_MODEL_PRICING = [
|
|
235
|
+
{
|
|
236
|
+
model: "deepseek-v4-flash",
|
|
237
|
+
peak: {
|
|
238
|
+
cacheHitInput: .1,
|
|
239
|
+
cacheMissInput: 3,
|
|
240
|
+
output: 9
|
|
241
|
+
},
|
|
242
|
+
offPeak: {
|
|
243
|
+
cacheHitInput: .05,
|
|
244
|
+
cacheMissInput: 1.5,
|
|
245
|
+
output: 4.5
|
|
246
|
+
}
|
|
247
|
+
},
|
|
248
|
+
{
|
|
249
|
+
model: "deepseek-v4-pro",
|
|
250
|
+
peak: {
|
|
251
|
+
cacheHitInput: .3,
|
|
252
|
+
cacheMissInput: 9,
|
|
253
|
+
output: 27
|
|
254
|
+
},
|
|
255
|
+
offPeak: {
|
|
256
|
+
cacheHitInput: .15,
|
|
257
|
+
cacheMissInput: 4.5,
|
|
258
|
+
output: 13.5
|
|
259
|
+
}
|
|
260
|
+
},
|
|
261
|
+
{
|
|
262
|
+
model: "deepseek-v4-flash-vision-exp",
|
|
263
|
+
peak: {
|
|
264
|
+
cacheHitInput: .1,
|
|
265
|
+
cacheMissInput: 3,
|
|
266
|
+
output: 9
|
|
267
|
+
},
|
|
268
|
+
offPeak: {
|
|
269
|
+
cacheHitInput: .05,
|
|
270
|
+
cacheMissInput: 1.5,
|
|
271
|
+
output: 4.5
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
];
|
|
275
|
+
/**
|
|
276
|
+
* Resolve optional configuration to a pricing table, defaulting omitted or
|
|
277
|
+
* empty rows to the published rates. Schemastery materializes an absent
|
|
278
|
+
* `z.array` as `[]` rather than `undefined`, so emptiness — not just absence —
|
|
279
|
+
* selects the defaults. Explicit non-empty rows override the same model; a
|
|
280
|
+
* supplied non-empty `models` list is authoritative.
|
|
281
|
+
* @param config - optional raw billing configuration.
|
|
282
|
+
* @returns the resolved table and peak-hour windows.
|
|
283
|
+
*/
|
|
284
|
+
function resolveBilling(config) {
|
|
285
|
+
const peakHours = config?.peakHours !== void 0 && config.peakHours.length > 0 ? config.peakHours : DEFAULT_PEAK_HOURS;
|
|
286
|
+
const rows = config?.models !== void 0 && config.models.length > 0 ? config.models : DEFAULT_MODEL_PRICING;
|
|
287
|
+
const models = /* @__PURE__ */ new Map();
|
|
288
|
+
for (const row of rows) models.set(row.model, {
|
|
289
|
+
peak: row.peak,
|
|
290
|
+
offPeak: row.offPeak
|
|
291
|
+
});
|
|
292
|
+
return {
|
|
293
|
+
peakHours,
|
|
294
|
+
models
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
/** The Beijing (Asia/Shanghai, UTC+8, no DST) hour of a timestamp. */
|
|
298
|
+
function beijingHour(now) {
|
|
299
|
+
return new Date(now.getTime() + 8 * 36e5).getUTCHours();
|
|
300
|
+
}
|
|
301
|
+
/** The Beijing (Asia/Shanghai, UTC+8, no DST) calendar-day key of a timestamp. */
|
|
302
|
+
function beijingDayKey(now) {
|
|
303
|
+
return new Date(now.getTime() + 8 * 36e5).toISOString().slice(0, 10);
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Whether a timestamp falls inside any peak-hour window (Beijing time).
|
|
307
|
+
* @param billing - resolved pricing with peak-hour windows.
|
|
308
|
+
* @param now - the moment to classify.
|
|
309
|
+
* @returns true during peak hours.
|
|
310
|
+
*/
|
|
311
|
+
function isPeak(billing, now) {
|
|
312
|
+
const hour = beijingHour(now);
|
|
313
|
+
return billing.peakHours.some(({ start, end }) => hour >= start && hour < end);
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Price a set of billed events at the official per-model rates, applying the
|
|
317
|
+
* peak/off-peak table per event by its Beijing-time hour. Each
|
|
318
|
+
* `assistant/message` event with usage contributes cache-hit input, cache-miss
|
|
319
|
+
* input (uncached input plus cache writes), and output (reasoning included)
|
|
320
|
+
* tokens at the rate of its own timestamp, with the three component costs
|
|
321
|
+
* carried separately; a model with usage but no pricing row is omitted (the
|
|
322
|
+
* published table prices only the two V4 rows).
|
|
323
|
+
* @param events - the events to price.
|
|
324
|
+
* @param billing - resolved pricing with peak-hour windows.
|
|
325
|
+
* @param catalog - model display rows, in presentation order.
|
|
326
|
+
* @returns the total cost plus one row per priced model.
|
|
327
|
+
*/
|
|
328
|
+
function priceEvents(events, billing, catalog) {
|
|
329
|
+
const names = new Map(catalog.map((model) => [model.id, model.name]));
|
|
330
|
+
const rows = /* @__PURE__ */ new Map();
|
|
331
|
+
for (const event of events) {
|
|
332
|
+
if (event.type !== "assistant/message") continue;
|
|
333
|
+
const reported = event.data.usage;
|
|
334
|
+
if (reported === void 0) continue;
|
|
335
|
+
const model = event.data.message.source.model;
|
|
336
|
+
const pricing = billing.models.get(model);
|
|
337
|
+
if (pricing === void 0) continue;
|
|
338
|
+
const peak = isPeak(billing, new Date(event.time));
|
|
339
|
+
const price = peak ? pricing.peak : pricing.offPeak;
|
|
340
|
+
const hit = reported.cacheReadTokens ?? 0;
|
|
341
|
+
const miss = reported.inputTokens + (reported.cacheWriteTokens ?? 0);
|
|
342
|
+
const output = reported.outputTokens;
|
|
343
|
+
const hitCost = hit * price.cacheHitInput / 1e6;
|
|
344
|
+
const missCost = miss * price.cacheMissInput / 1e6;
|
|
345
|
+
const outputCost = output * price.output / 1e6;
|
|
346
|
+
const cost = hitCost + missCost + outputCost;
|
|
347
|
+
let row = rows.get(model);
|
|
348
|
+
if (row === void 0) {
|
|
349
|
+
row = {
|
|
350
|
+
cacheHitInputTokens: 0,
|
|
351
|
+
cacheMissInputTokens: 0,
|
|
352
|
+
outputTokens: 0,
|
|
353
|
+
cost: 0,
|
|
354
|
+
peakCost: 0,
|
|
355
|
+
offPeakCost: 0,
|
|
356
|
+
cacheHitInputCost: 0,
|
|
357
|
+
cacheMissInputCost: 0,
|
|
358
|
+
outputCost: 0
|
|
359
|
+
};
|
|
360
|
+
rows.set(model, row);
|
|
361
|
+
}
|
|
362
|
+
row.cacheHitInputTokens += hit;
|
|
363
|
+
row.cacheMissInputTokens += miss;
|
|
364
|
+
row.outputTokens += output;
|
|
365
|
+
row.cost += cost;
|
|
366
|
+
row.cacheHitInputCost += hitCost;
|
|
367
|
+
row.cacheMissInputCost += missCost;
|
|
368
|
+
row.outputCost += outputCost;
|
|
369
|
+
if (peak) row.peakCost += cost;
|
|
370
|
+
else row.offPeakCost += cost;
|
|
371
|
+
}
|
|
372
|
+
const models = [...rows.entries()].map(([model, row]) => ({
|
|
373
|
+
model,
|
|
374
|
+
displayName: names.get(model) ?? model,
|
|
375
|
+
cost: row.cost,
|
|
376
|
+
peakCost: row.peakCost,
|
|
377
|
+
offPeakCost: row.offPeakCost,
|
|
378
|
+
cacheHitInputTokens: row.cacheHitInputTokens,
|
|
379
|
+
cacheMissInputTokens: row.cacheMissInputTokens,
|
|
380
|
+
outputTokens: row.outputTokens,
|
|
381
|
+
cacheHitInputCost: row.cacheHitInputCost,
|
|
382
|
+
cacheMissInputCost: row.cacheMissInputCost,
|
|
383
|
+
outputCost: row.outputCost
|
|
384
|
+
}));
|
|
385
|
+
return {
|
|
386
|
+
total: models.reduce((sum, model) => sum + model.cost, 0),
|
|
387
|
+
models
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
/**
|
|
391
|
+
* Price one session's complete event log at the official per-model rates.
|
|
392
|
+
* @param events - one session's complete event log.
|
|
393
|
+
* @param billing - resolved pricing with peak-hour windows.
|
|
394
|
+
* @param catalog - model display rows, in presentation order.
|
|
395
|
+
* @returns the session's total cost plus one row per priced model.
|
|
396
|
+
*/
|
|
397
|
+
function computeSessionSpend(events, billing, catalog) {
|
|
398
|
+
return priceEvents(events, billing, catalog);
|
|
399
|
+
}
|
|
400
|
+
/**
|
|
401
|
+
* Price every event whose Beijing-time calendar day is the day of `now`,
|
|
402
|
+
* aggregating across every session's event log. Events from other Beijing
|
|
403
|
+
* days are ignored, so a caller passes the concatenated logs of all sessions.
|
|
404
|
+
* @param events - every session's complete event log, concatenated.
|
|
405
|
+
* @param billing - resolved pricing with peak-hour windows.
|
|
406
|
+
* @param catalog - model display rows, in presentation order.
|
|
407
|
+
* @param now - the reference moment whose Beijing-time calendar day is "today".
|
|
408
|
+
* @returns today's total cost plus one row per priced model.
|
|
409
|
+
*/
|
|
410
|
+
function computeTodaySpend(events, billing, catalog, now = /* @__PURE__ */ new Date()) {
|
|
411
|
+
const day = beijingDayKey(now);
|
|
412
|
+
return priceEvents(events.filter((event) => beijingDayKey(new Date(event.time)) === day), billing, catalog);
|
|
413
|
+
}
|
|
414
|
+
//#endregion
|
|
415
|
+
//#region lib/types/index.js
|
|
416
|
+
/**
|
|
417
|
+
* DeepSeek account balance and session-spend provider, as a standalone host
|
|
418
|
+
* plugin. It resolves the DeepSeek endpoint and API key from its own config and
|
|
419
|
+
* the credential/environment seams, prices each session's billed usage with the
|
|
420
|
+
* peak/off-peak table, and exposes the `billing` Remote (`getBalance`, the
|
|
421
|
+
* per-session `getSessionSpend`, and the all-sessions `getTodaySpend`).
|
|
422
|
+
* @module @rayadesu/dsh-llm-billing
|
|
423
|
+
*/
|
|
424
|
+
const name = "llm-billing";
|
|
425
|
+
const DEFAULT_API_KEY_ENV = "DEEPSEEK_API_KEY";
|
|
426
|
+
const BASE_URL_ENV = "DEEPSEEK_BASE_URL";
|
|
427
|
+
/** Public API default; deployments may point elsewhere via $DEEPSEEK_BASE_URL. */
|
|
428
|
+
const PUBLIC_BASE_URL = "https://api.deepseek.com";
|
|
429
|
+
const DEFAULT_MODELS = [
|
|
430
|
+
{
|
|
431
|
+
id: "deepseek-v4-flash",
|
|
432
|
+
name: "DeepSeek-V4-Flash"
|
|
433
|
+
},
|
|
434
|
+
{
|
|
435
|
+
id: "deepseek-v4-pro",
|
|
436
|
+
name: "DeepSeek-V4-Pro"
|
|
437
|
+
},
|
|
438
|
+
{
|
|
439
|
+
id: "deepseek-v4-flash-vision-exp",
|
|
440
|
+
name: "DeepSeek-V4-Flash-Vision-Exp"
|
|
441
|
+
}
|
|
442
|
+
];
|
|
443
|
+
const billingModel = z.object({
|
|
444
|
+
id: z.string().required(),
|
|
445
|
+
name: z.string()
|
|
446
|
+
});
|
|
447
|
+
const tokenPrice = z.object({
|
|
448
|
+
cacheHitInput: z.number().min(0),
|
|
449
|
+
cacheMissInput: z.number().min(0),
|
|
450
|
+
output: z.number().min(0)
|
|
451
|
+
});
|
|
452
|
+
const billingConfig = z.object({
|
|
453
|
+
peakHours: z.array(z.object({
|
|
454
|
+
start: z.number().step(1).min(0).max(23),
|
|
455
|
+
end: z.number().step(1).min(0).max(24)
|
|
456
|
+
})).default(DEFAULT_PEAK_HOURS),
|
|
457
|
+
models: z.array(z.object({
|
|
458
|
+
model: z.string().required(),
|
|
459
|
+
peak: tokenPrice,
|
|
460
|
+
offPeak: tokenPrice
|
|
461
|
+
})).default(DEFAULT_MODEL_PRICING)
|
|
462
|
+
});
|
|
463
|
+
const Config = z.object({
|
|
464
|
+
apiKeyEnv: z.string().role("credential-ref").default(DEFAULT_API_KEY_ENV),
|
|
465
|
+
baseURL: z.string(),
|
|
466
|
+
models: z.array(billingModel).default(DEFAULT_MODELS),
|
|
467
|
+
billing: billingConfig
|
|
468
|
+
});
|
|
469
|
+
/**
|
|
470
|
+
* Read one session's event log: the live SessionStore first, then the
|
|
471
|
+
* persistence backend for a flushed session.
|
|
472
|
+
* @param ctx - plugin context carrying the SessionStore and optional persistence.
|
|
473
|
+
* @param sessionId - the session to read.
|
|
474
|
+
* @returns the session's complete event log.
|
|
475
|
+
* @throws {@link LlmError} with code `NOT_FOUND` when the session is unknown.
|
|
476
|
+
*/
|
|
477
|
+
async function sessionEvents(ctx, sessionId) {
|
|
478
|
+
const live = ctx.get("sessions")?.get(sessionId);
|
|
479
|
+
if (live !== void 0) return live.events;
|
|
480
|
+
const persistence = ctx.get("sessionPersistence");
|
|
481
|
+
if (persistence !== void 0) for (const header of await persistence.list()) {
|
|
482
|
+
if (header.id !== sessionId) continue;
|
|
483
|
+
return (await persistence.inspect(sessionId)).events;
|
|
484
|
+
}
|
|
485
|
+
throw new LlmError(`llm-billing: session ${sessionId} not found`, "NOT_FOUND");
|
|
486
|
+
}
|
|
487
|
+
/**
|
|
488
|
+
* Read every session's event log, concatenated: each live SessionStore
|
|
489
|
+
* session first (its log may hold events not yet flushed), then each persisted
|
|
490
|
+
* session that is not live, so no event is counted twice. Events are appended
|
|
491
|
+
* one at a time: spreading a very large log into `push(...)` exceeds the
|
|
492
|
+
* engine's argument limit and throws a stack RangeError.
|
|
493
|
+
* @param ctx - plugin context carrying the SessionStore and optional persistence.
|
|
494
|
+
* @returns every session's complete event log, concatenated.
|
|
495
|
+
*/
|
|
496
|
+
async function allSessionEvents(ctx) {
|
|
497
|
+
const events = [];
|
|
498
|
+
const sessions = ctx.get("sessions");
|
|
499
|
+
const liveIds = /* @__PURE__ */ new Set();
|
|
500
|
+
if (sessions !== void 0) for (const session of sessions.list()) {
|
|
501
|
+
liveIds.add(session.id);
|
|
502
|
+
for (const event of session.events) events.push(event);
|
|
503
|
+
}
|
|
504
|
+
const persistence = ctx.get("sessionPersistence");
|
|
505
|
+
if (persistence !== void 0) for (const header of await persistence.list()) {
|
|
506
|
+
if (liveIds.has(header.id)) continue;
|
|
507
|
+
try {
|
|
508
|
+
const inspection = await persistence.inspect(header.id);
|
|
509
|
+
for (const event of inspection.events) events.push(event);
|
|
510
|
+
} catch (error) {
|
|
511
|
+
ctx.logger.warn(`llm-billing: skipping unreadable session ${header.id}: ${String(error)}`);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
return events;
|
|
515
|
+
}
|
|
516
|
+
/**
|
|
517
|
+
* Register the `billing` Remote under the `billing` namespace.
|
|
518
|
+
* @param ctx - owning plugin context.
|
|
519
|
+
* @param config - validated plugin config.
|
|
520
|
+
*/
|
|
521
|
+
function apply(ctx, config) {
|
|
522
|
+
const baseURL = () => config.baseURL ?? launchEnvironmentOf(ctx).get(BASE_URL_ENV)?.value ?? "https://api.deepseek.com";
|
|
523
|
+
const apiKeyRef = credentialRef(config.apiKeyEnv ?? DEFAULT_API_KEY_ENV);
|
|
524
|
+
const resolveApiKey = async () => {
|
|
525
|
+
const credentials = ctx.get("credentials");
|
|
526
|
+
if (credentials !== void 0) {
|
|
527
|
+
const hit = await credentials.resolve(apiKeyRef);
|
|
528
|
+
if (hit !== void 0) return assertUsableApiKey(hit.value, "llm-billing", apiKeyRef);
|
|
529
|
+
} else {
|
|
530
|
+
const ambient = launchEnvironmentOf(ctx).get(apiKeyRef);
|
|
531
|
+
if (ambient !== void 0 && ambient.value.length > 0) return assertUsableApiKey(ambient.value, "llm-billing", apiKeyRef);
|
|
532
|
+
}
|
|
533
|
+
throw new LlmError(`llm-billing: no API key; store ${apiKeyRef} through the credentials service or export it`, "MISSING_CREDENTIAL");
|
|
534
|
+
};
|
|
535
|
+
const fetchBalance = async () => {
|
|
536
|
+
const apiKey = await resolveApiKey();
|
|
537
|
+
return fetchDeepSeekBalance(baseURL(), apiKey);
|
|
538
|
+
};
|
|
539
|
+
const fetchSessionSpend = async (sessionId) => {
|
|
540
|
+
const billing = resolveBilling(config.billing);
|
|
541
|
+
const catalog = (config.models ?? DEFAULT_MODELS).map((model) => ({
|
|
542
|
+
id: model.id,
|
|
543
|
+
name: model.name ?? model.id
|
|
544
|
+
}));
|
|
545
|
+
return computeSessionSpend(await sessionEvents(ctx, sessionId), billing, catalog);
|
|
546
|
+
};
|
|
547
|
+
const fetchTodaySpend = async () => {
|
|
548
|
+
const billing = resolveBilling(config.billing);
|
|
549
|
+
const catalog = (config.models ?? DEFAULT_MODELS).map((model) => ({
|
|
550
|
+
id: model.id,
|
|
551
|
+
name: model.name ?? model.id
|
|
552
|
+
}));
|
|
553
|
+
return computeTodaySpend(await allSessionEvents(ctx), billing, catalog);
|
|
554
|
+
};
|
|
555
|
+
new DeepSeekBalanceGateway(ctx, {
|
|
556
|
+
fetchBalance,
|
|
557
|
+
fetchSessionSpend,
|
|
558
|
+
fetchTodaySpend
|
|
559
|
+
});
|
|
560
|
+
}
|
|
561
|
+
//#endregion
|
|
562
|
+
export { Config, DEFAULT_MODEL_PRICING, DEFAULT_PEAK_HOURS, DeepSeekBalanceGateway, PUBLIC_BASE_URL, apply, computeSessionSpend, computeTodaySpend, fetchDeepSeekBalance, isPeak, name, parseDeepSeekBalance, resolveBilling };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
//#region lib/types/invariant.js
|
|
2
|
+
/**
|
|
3
|
+
* Package-owned invariant companion for `@rayadesu/dsh-llm-billing`.
|
|
4
|
+
* @module @rayadesu/dsh-llm-billing/invariant
|
|
5
|
+
*/
|
|
6
|
+
const PACKAGE_NAME = "@rayadesu/dsh-llm-billing";
|
|
7
|
+
/** Cordis companion plugin name. */
|
|
8
|
+
const name = "llm-billing-invariant";
|
|
9
|
+
/** Service required before the companion can reserve package ownership. */
|
|
10
|
+
const inject = ["invariants"];
|
|
11
|
+
/**
|
|
12
|
+
* No runtime invariant: this package is a read-only Remote projection over the
|
|
13
|
+
* provider balance and session logs, and owns no cross-plugin mutable state.
|
|
14
|
+
*/
|
|
15
|
+
const install = () => {};
|
|
16
|
+
/**
|
|
17
|
+
* Register this package's invariant companion.
|
|
18
|
+
* @param ctx - Cordis context carrying the invariant service.
|
|
19
|
+
* @returns the installed registration's disposer after setup succeeds.
|
|
20
|
+
*/
|
|
21
|
+
const apply = (ctx) => Promise.resolve(ctx.invariants.register(PACKAGE_NAME, install));
|
|
22
|
+
//#endregion
|
|
23
|
+
export { apply, inject, name };
|