dsh-plugin-balance-usage 1.0.0 → 1.0.1
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/CHANGELOG.md +5 -0
- package/client.js +70 -0
- package/index.js +117 -3
- package/package.json +5 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
# 更新日志
|
|
2
2
|
|
|
3
|
+
## 1.0.1
|
|
4
|
+
|
|
5
|
+
- 「主线」与折叠分组从浏览器 localStorage 迁到 `~/.dsh/plugin-settings/balance-usage.json`(Host 侧读写),配合 `dsh-plugin-profile-sync` 可在换机后沿用同一套设置;localStorage 保留为离线兜底。
|
|
6
|
+
- 新增 Host 路由 `GET/POST /plugins/balance-usage/prefs`,与用量路由同样只接受同源请求。
|
|
7
|
+
|
|
3
8
|
## 1.0.0
|
|
4
9
|
|
|
5
10
|
首个公开版本。
|
package/client.js
CHANGED
|
@@ -26,6 +26,7 @@ window.__ModuleLoader__.load({
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
const ROUTE = '/plugins/balance-usage/summary'
|
|
29
|
+
const PREFS_ROUTE = '/plugins/balance-usage/prefs'
|
|
29
30
|
const FALLBACK_USAGE_URL = 'https://platform.deepseek.com/usage'
|
|
30
31
|
const KEY_PRIMARY = 'dsh.balance-usage.primary'
|
|
31
32
|
const KEY_COLLAPSED = 'dsh.balance-usage.collapsed'
|
|
@@ -265,6 +266,7 @@ window.__ModuleLoader__.load({
|
|
|
265
266
|
} catch {
|
|
266
267
|
/* storage may be unavailable */
|
|
267
268
|
}
|
|
269
|
+
pushPrefs({ primary: value })
|
|
268
270
|
}
|
|
269
271
|
|
|
270
272
|
function readCollapsed() {
|
|
@@ -284,6 +286,42 @@ window.__ModuleLoader__.load({
|
|
|
284
286
|
} catch {
|
|
285
287
|
/* ignore */
|
|
286
288
|
}
|
|
289
|
+
pushPrefs({ collapsed: value })
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/* The Host keeps the same two settings in `~/.dsh/plugin-settings/balance-usage.json`
|
|
293
|
+
so the profile-sync plugin can carry them to another machine. localStorage stays
|
|
294
|
+
as the offline fallback. */
|
|
295
|
+
async function pushPrefs(patch) {
|
|
296
|
+
try {
|
|
297
|
+
const values = {
|
|
298
|
+
primary: patch.primary !== undefined ? patch.primary : state.primary,
|
|
299
|
+
collapsed: patch.collapsed !== undefined ? patch.collapsed : state.collapsed,
|
|
300
|
+
}
|
|
301
|
+
await fetch(PREFS_ROUTE, {
|
|
302
|
+
method: 'POST',
|
|
303
|
+
headers: { 'content-type': 'application/json', accept: 'application/json' },
|
|
304
|
+
body: JSON.stringify({ values }),
|
|
305
|
+
cache: 'no-store',
|
|
306
|
+
})
|
|
307
|
+
} catch {
|
|
308
|
+
/* the Host route may be absent in an older install */
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
async function readPrefs() {
|
|
313
|
+
try {
|
|
314
|
+
const options = { headers: { accept: 'application/json' }, cache: 'no-store' }
|
|
315
|
+
if (typeof AbortSignal !== 'undefined' && typeof AbortSignal.timeout === 'function') {
|
|
316
|
+
options.signal = AbortSignal.timeout(4000)
|
|
317
|
+
}
|
|
318
|
+
const response = await fetch(PREFS_ROUTE, options)
|
|
319
|
+
if (!response.ok) return null
|
|
320
|
+
const body = await response.json()
|
|
321
|
+
return body && body.ok === true && body.values ? body.values : null
|
|
322
|
+
} catch {
|
|
323
|
+
return null
|
|
324
|
+
}
|
|
287
325
|
}
|
|
288
326
|
|
|
289
327
|
/* ---------------------------------------------------------------- *
|
|
@@ -1316,6 +1354,38 @@ window.__ModuleLoader__.load({
|
|
|
1316
1354
|
ctx.slots.register({ name: 'shell.overlay', id: 'balance-usage-panel', order: 40 }, Panel),
|
|
1317
1355
|
)
|
|
1318
1356
|
|
|
1357
|
+
/* Settings are authoritative on the Host; localStorage is only the fallback
|
|
1358
|
+
for a build whose Host half predates the prefs route. */
|
|
1359
|
+
ctx.effect(() => {
|
|
1360
|
+
let alive = true
|
|
1361
|
+
readPrefs()
|
|
1362
|
+
.then((values) => {
|
|
1363
|
+
if (!alive || !values) return
|
|
1364
|
+
const patch = {}
|
|
1365
|
+
if (values.primary && typeof values.primary.provider === 'string') {
|
|
1366
|
+
patch.primary = values.primary
|
|
1367
|
+
try {
|
|
1368
|
+
globalThis.localStorage?.setItem(KEY_PRIMARY, JSON.stringify(values.primary))
|
|
1369
|
+
} catch {
|
|
1370
|
+
/* ignore */
|
|
1371
|
+
}
|
|
1372
|
+
}
|
|
1373
|
+
if (Array.isArray(values.collapsed)) {
|
|
1374
|
+
patch.collapsed = values.collapsed
|
|
1375
|
+
try {
|
|
1376
|
+
globalThis.localStorage?.setItem(KEY_COLLAPSED, JSON.stringify(values.collapsed))
|
|
1377
|
+
} catch {
|
|
1378
|
+
/* ignore */
|
|
1379
|
+
}
|
|
1380
|
+
}
|
|
1381
|
+
if (Object.keys(patch).length > 0) setState(patch)
|
|
1382
|
+
})
|
|
1383
|
+
.catch(() => {})
|
|
1384
|
+
return () => {
|
|
1385
|
+
alive = false
|
|
1386
|
+
}
|
|
1387
|
+
})
|
|
1388
|
+
|
|
1319
1389
|
ctx.effect(() => {
|
|
1320
1390
|
const warm = setTimeout(() => load(ctx).catch(() => {}), 1_500)
|
|
1321
1391
|
|
package/index.js
CHANGED
|
@@ -14,12 +14,14 @@
|
|
|
14
14
|
* `webServer` route is the supported way to hand Host-side numbers to the
|
|
15
15
|
* Client too.
|
|
16
16
|
*/
|
|
17
|
-
import { readdir, readFile, stat } from 'node:fs/promises'
|
|
17
|
+
import { mkdir, readdir, readFile, stat, writeFile } from 'node:fs/promises'
|
|
18
18
|
import os from 'node:os'
|
|
19
19
|
import path from 'node:path'
|
|
20
20
|
import zlib from 'node:zlib'
|
|
21
21
|
|
|
22
22
|
const ROUTE = '/plugins/balance-usage/summary'
|
|
23
|
+
const PREFS_ROUTE = '/plugins/balance-usage/prefs'
|
|
24
|
+
const PREFS_NS = 'balance-usage'
|
|
23
25
|
const LOG_FILE = 'session.v4.jsonl.zstd'
|
|
24
26
|
const SCAN_TTL_MS = 20_000
|
|
25
27
|
const MAX_FILE_BYTES = 64 * 1024 * 1024
|
|
@@ -203,11 +205,54 @@ function aggregateLog(text) {
|
|
|
203
205
|
* Scanning
|
|
204
206
|
* ------------------------------------------------------------------ */
|
|
205
207
|
|
|
208
|
+
function dshHome() {
|
|
209
|
+
return process.env.DSH_HOME || path.join(os.homedir(), '.dsh')
|
|
210
|
+
}
|
|
211
|
+
|
|
206
212
|
function sessionsRoot() {
|
|
207
|
-
|
|
208
|
-
|
|
213
|
+
return path.join(dshHome(), 'sessions')
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/* Settings live in the shared, profile-sync friendly location so another
|
|
217
|
+
machine can align them: `~/.dsh/plugin-settings/<ns>.json` = {updatedAt, values}. */
|
|
218
|
+
function prefsFile() {
|
|
219
|
+
return path.join(dshHome(), 'plugin-settings', `${PREFS_NS}.json`)
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
async function readPrefs() {
|
|
223
|
+
try {
|
|
224
|
+
const raw = await readFile(prefsFile(), 'utf8')
|
|
225
|
+
const parsed = JSON.parse(raw.replace(/^\uFEFF/, ''))
|
|
226
|
+
const values = parsed && typeof parsed === 'object' ? parsed.values : null
|
|
227
|
+
if (!values || typeof values !== 'object') return {}
|
|
228
|
+
const clean = {}
|
|
229
|
+
if (values.primary && typeof values.primary.provider === 'string' && typeof values.primary.model === 'string') {
|
|
230
|
+
clean.primary = { provider: values.primary.provider, model: values.primary.model }
|
|
231
|
+
}
|
|
232
|
+
if (Array.isArray(values.collapsed)) {
|
|
233
|
+
clean.collapsed = values.collapsed.filter((item) => typeof item === 'string')
|
|
234
|
+
}
|
|
235
|
+
return clean
|
|
236
|
+
} catch {
|
|
237
|
+
return {}
|
|
238
|
+
}
|
|
209
239
|
}
|
|
210
240
|
|
|
241
|
+
async function writePrefs(values) {
|
|
242
|
+
const clean = {}
|
|
243
|
+
if (values.primary && typeof values.primary.provider === 'string' && typeof values.primary.model === 'string') {
|
|
244
|
+
clean.primary = { provider: values.primary.provider, model: values.primary.model }
|
|
245
|
+
}
|
|
246
|
+
if (Array.isArray(values.collapsed)) {
|
|
247
|
+
clean.collapsed = values.collapsed.filter((item) => typeof item === 'string')
|
|
248
|
+
}
|
|
249
|
+
const target = prefsFile()
|
|
250
|
+
await mkdir(path.dirname(target), { recursive: true })
|
|
251
|
+
await writeFile(target, `${JSON.stringify({ updatedAt: Date.now(), values: clean }, null, 2)}\n`, 'utf8')
|
|
252
|
+
return clean
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
|
|
211
256
|
async function listLogFiles(root) {
|
|
212
257
|
const files = []
|
|
213
258
|
let workspaces
|
|
@@ -358,6 +403,46 @@ function json(body, status = 200) {
|
|
|
358
403
|
})
|
|
359
404
|
}
|
|
360
405
|
|
|
406
|
+
/* Web resource routes only accept GET/HEAD, and saving preferences needs POST, so
|
|
407
|
+
the prefs route is a plain Node handler with the same origin rules. */
|
|
408
|
+
function sameOriginNode(request) {
|
|
409
|
+
const site = request.headers['sec-fetch-site']
|
|
410
|
+
if (site && site !== 'same-origin' && site !== 'none') return false
|
|
411
|
+
|
|
412
|
+
const origin = request.headers.origin
|
|
413
|
+
if (origin) {
|
|
414
|
+
try {
|
|
415
|
+
if (new URL(origin).host !== request.headers.host) return false
|
|
416
|
+
} catch {
|
|
417
|
+
return false
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
return true
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
function sendNodeJson(response, body, status = 200) {
|
|
425
|
+
const payload = JSON.stringify(body)
|
|
426
|
+
response.writeHead(status, {
|
|
427
|
+
'content-type': 'application/json; charset=utf-8',
|
|
428
|
+
'cache-control': 'no-store',
|
|
429
|
+
'content-length': Buffer.byteLength(payload),
|
|
430
|
+
})
|
|
431
|
+
response.end(payload)
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
async function readNodeJsonBody(request, limit = 128 * 1024) {
|
|
435
|
+
const chunks = []
|
|
436
|
+
let total = 0
|
|
437
|
+
for await (const chunk of request) {
|
|
438
|
+
total += chunk.length
|
|
439
|
+
if (total > limit) throw new Error('payload too large')
|
|
440
|
+
chunks.push(chunk)
|
|
441
|
+
}
|
|
442
|
+
if (total === 0) return {}
|
|
443
|
+
return JSON.parse(Buffer.concat(chunks).toString('utf8').replace(/^\uFEFF/, ''))
|
|
444
|
+
}
|
|
445
|
+
|
|
361
446
|
export const name = 'balance-usage'
|
|
362
447
|
export const inject = ['webServer']
|
|
363
448
|
|
|
@@ -431,6 +516,35 @@ export function apply(ctx) {
|
|
|
431
516
|
'balance-usage: summary route',
|
|
432
517
|
)
|
|
433
518
|
|
|
519
|
+
/* Preferences (primary provider/model + collapsed groups). Kept in the Host
|
|
520
|
+
rather than localStorage so they survive a new machine once the profile-sync
|
|
521
|
+
plugin pushes/pulls `~/.dsh/plugin-settings/balance-usage.json`. */
|
|
522
|
+
ctx.effect(
|
|
523
|
+
() =>
|
|
524
|
+
ctx.webServer.register({
|
|
525
|
+
kind: 'exact',
|
|
526
|
+
path: PREFS_ROUTE,
|
|
527
|
+
handler: async (request, response) => {
|
|
528
|
+
if (!sameOriginNode(request)) return sendNodeJson(response, { ok: false, error: 'forbidden' }, 403)
|
|
529
|
+
try {
|
|
530
|
+
if (request.method === 'POST') {
|
|
531
|
+
const body = await readNodeJsonBody(request)
|
|
532
|
+
const values = await writePrefs(body?.values ?? body ?? {})
|
|
533
|
+
return sendNodeJson(response, { ok: true, values })
|
|
534
|
+
}
|
|
535
|
+
if (request.method !== 'GET' && request.method !== 'HEAD') {
|
|
536
|
+
return sendNodeJson(response, { ok: false, error: 'method not allowed' }, 405)
|
|
537
|
+
}
|
|
538
|
+
return sendNodeJson(response, { ok: true, values: await readPrefs() })
|
|
539
|
+
} catch (error) {
|
|
540
|
+
ctx.logger?.warn?.(error)
|
|
541
|
+
return sendNodeJson(response, { ok: false, error: String(error?.message ?? error) }, 500)
|
|
542
|
+
}
|
|
543
|
+
},
|
|
544
|
+
}),
|
|
545
|
+
'balance-usage: prefs route',
|
|
546
|
+
)
|
|
547
|
+
|
|
434
548
|
/* Warm the cache once so the first open of the panel is instant. */
|
|
435
549
|
ctx.effect(() => {
|
|
436
550
|
const timer = setTimeout(() => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-plugin-balance-usage",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "DSH 侧栏余额与用量:在左侧栏底部显示 DeepSeek 账户余额与 Token 用量,面板内可查看近 14 天走势、按运营商/模型折叠、设置主线,并直接在应用内打开官方用量页。",
|
|
6
6
|
"keywords": [
|
|
@@ -61,7 +61,10 @@
|
|
|
61
61
|
"client": {
|
|
62
62
|
"platform": "web",
|
|
63
63
|
"immediately": true,
|
|
64
|
-
"inject": [
|
|
64
|
+
"inject": [
|
|
65
|
+
"@deepseek-ai/dsh-api-remotes",
|
|
66
|
+
"@deepseek-ai/dsh-client-locale"
|
|
67
|
+
]
|
|
65
68
|
},
|
|
66
69
|
"compatibility": {
|
|
67
70
|
"dsh": ">=0.2.2-rc.1 <0.3.0-0 || >=0.2.3-rc.1 <0.3.0-0 || >=0.2.4-rc.1 <0.3.0-0",
|