@kenz1117/dsh-ui-usage-billing 1.8.11 → 1.8.13

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.
Files changed (34) hide show
  1. package/README.en.md +24 -7
  2. package/README.md +23 -8
  3. package/lib/client.js +5 -5
  4. package/lib/index.js +1449 -381
  5. package/lib/types/aggregate.d.ts +22 -13
  6. package/lib/types/balance.d.ts +23 -2
  7. package/lib/types/client/PeakScheduleEditor.d.ts +9 -0
  8. package/lib/types/client/api/balance.d.ts +1 -1
  9. package/lib/types/client/domain/floating-model.d.ts +31 -0
  10. package/lib/types/client/domain/types.d.ts +10 -1
  11. package/lib/types/client/live-cost.d.ts +10 -3
  12. package/lib/types/client/locales.d.ts +2 -1
  13. package/lib/types/client/peak-alert.d.ts +2 -1
  14. package/lib/types/client/peak-schedule-locales.d.ts +48 -0
  15. package/lib/types/client/price-drafts.d.ts +19 -0
  16. package/lib/types/client/price-picker.d.ts +17 -0
  17. package/lib/types/client/pricing.d.ts +8 -9
  18. package/lib/types/client/queue-eligibility.d.ts +30 -0
  19. package/lib/types/client/round-chart.d.ts +1 -0
  20. package/lib/types/client/scheduled-band.d.ts +23 -0
  21. package/lib/types/client/task-queue-quick.d.ts +1 -1
  22. package/lib/types/config.d.ts +2 -0
  23. package/lib/types/peak-ledger.d.ts +21 -0
  24. package/lib/types/peak-schedule-history.d.ts +7 -0
  25. package/lib/types/peak-schedule-models.d.ts +21 -0
  26. package/lib/types/peak-schedule-store.d.ts +10 -0
  27. package/lib/types/peak-schedule.d.ts +67 -0
  28. package/lib/types/pricing-shared.d.ts +75 -1
  29. package/lib/types/reconcile.d.ts +11 -2
  30. package/lib/types/routes/balance.d.ts +6 -3
  31. package/lib/types/routes/peak-schedule.d.ts +12 -0
  32. package/lib/types/routes/pricing-models.d.ts +11 -0
  33. package/lib/types/task-queue-shared.d.ts +12 -0
  34. package/package.json +1 -1
@@ -0,0 +1,21 @@
1
+ import { type ProviderRouteView } from './aggregate.ts';
2
+ import { type ScheduleModel, type ScheduleNotice } from './peak-schedule.ts';
3
+ export interface ScheduleProfile extends ProviderRouteView {
4
+ displayName?: string;
5
+ models?: unknown[];
6
+ }
7
+ interface Llm {
8
+ listProviders?(): readonly {
9
+ id: string;
10
+ name?: string;
11
+ }[];
12
+ listModels?(route: string): Promise<unknown[]>;
13
+ }
14
+ export declare function eligibleScheduleModel(key: string): boolean;
15
+ /** Cache only remote discovery. Route identity and locally declared models are refreshed every request. */
16
+ export declare function createScheduleModelDiscovery(now?: () => number, ttlMs?: number): (llm: Llm | undefined, profiles: Record<string, ScheduleProfile>, aliases: Readonly<Record<string, string>>) => Promise<{
17
+ models: ScheduleModel[];
18
+ notices: ScheduleNotice[];
19
+ }>;
20
+ export {};
21
+ //# sourceMappingURL=peak-schedule-models.d.ts.map
@@ -0,0 +1,10 @@
1
+ import { type ScheduleEdit, type ScheduleHistory, type ScheduleNotice } from './peak-schedule.ts';
2
+ export declare class ScheduleClockConflict extends Error {
3
+ }
4
+ export declare function openScheduleStore(path: string, eligible: (key: string) => boolean, now?: () => number): {
5
+ current: () => import("./peak-schedule.ts").PeakSchedule;
6
+ history: () => ScheduleHistory;
7
+ notices: () => ScheduleNotice[];
8
+ save(edit: ScheduleEdit, latestEventTime?: number): Promise<void>;
9
+ };
10
+ //# sourceMappingURL=peak-schedule-store.d.ts.map
@@ -0,0 +1,67 @@
1
+ /** A source/model override. Omitted fields inherit that source's defaults. */
2
+ export interface PeakRule {
3
+ windows?: number[];
4
+ holidays?: string[];
5
+ weekendOffPeak?: boolean;
6
+ utcOffsetMinutes?: number;
7
+ }
8
+ export interface PeakSchedule {
9
+ version: 2;
10
+ models: Record<string, PeakRule>;
11
+ }
12
+ export interface ScheduleModel {
13
+ source: string;
14
+ model: string;
15
+ label: string;
16
+ }
17
+ export interface ScheduleEdit {
18
+ version: 3;
19
+ changes: Record<string, PeakRule | null>;
20
+ }
21
+ export type ScheduleResults = Record<string, 'accepted' | 'invalidRule' | 'unavailableModel'>;
22
+ export interface RevisionAudit {
23
+ source: 'local-api';
24
+ actor: 'unidentified';
25
+ }
26
+ export interface ScheduleRevision {
27
+ audit?: RevisionAudit;
28
+ effectiveFromMs: number;
29
+ changes: ScheduleEdit['changes'];
30
+ }
31
+ export interface ScheduleHistory {
32
+ version: 3;
33
+ revisions: ScheduleRevision[];
34
+ }
35
+ export type ScheduleNoticeCode = 'legacySchedule' | 'unreadableSchedule' | 'malformedSchedule' | 'malformedRevision' | 'malformedRule' | 'ineligibleModel' | 'discoveryFailed';
36
+ export interface ScheduleNotice {
37
+ code: ScheduleNoticeCode;
38
+ detail?: string;
39
+ }
40
+ export interface ScheduleSnapshot {
41
+ effectiveFromMs?: number;
42
+ results?: ScheduleResults;
43
+ config: PeakSchedule;
44
+ models: ScheduleModel[];
45
+ notices?: ScheduleNotice[];
46
+ }
47
+ export declare const emptySchedule: () => PeakSchedule;
48
+ export declare const scheduleKey: (source: string, model: string) => string;
49
+ export declare const accountRoute: (id: string) => boolean;
50
+ export declare function validSource(source: string): boolean;
51
+ export declare function parseRule(value: unknown): PeakRule;
52
+ export declare function parseSchedule(value: unknown, eligible: (model: string) => boolean): PeakSchedule;
53
+ export declare function modelFromScheduleKey(key: string): string;
54
+ export declare function partitionScheduleEdit(value: unknown, allowed: (key: string, model: string) => boolean): {
55
+ edit: ScheduleEdit;
56
+ results: ScheduleResults;
57
+ };
58
+ export declare function parseScheduleEdit(value: unknown): ScheduleEdit;
59
+ export declare function readScheduleHistory(value: unknown): {
60
+ history: ScheduleHistory;
61
+ notices: ScheduleNotice[];
62
+ };
63
+ export declare const TIER_BOUNDARY_MINUTES: readonly [540, 720, 840, 1080];
64
+ export declare const officialHolidays = "20260925 20260926 20260927 20261001 20261002 20261003 20261004 20261005 20261006 20261007";
65
+ export declare function effectiveRule(source: string, rule: PeakRule): Required<PeakRule>;
66
+ export declare function customTier(time: number, rule: Required<PeakRule>): 'peak' | 'offPeak';
67
+ //# sourceMappingURL=peak-schedule.d.ts.map
@@ -4,6 +4,13 @@
4
4
  * the built-in catalog). An absent field means the built-in value still
5
5
  * applies — the dashboard degrades to the catalog, never to fabricated data.
6
6
  */
7
+ export interface PricingModel {
8
+ key: string;
9
+ name: string;
10
+ route: string;
11
+ channel: string;
12
+ origin: string;
13
+ }
7
14
  /** One model's unit prices as reported by the router, in USD per 1M tokens. */
8
15
  export interface LivePrice {
9
16
  /** Uncached input price per 1M tokens. */
@@ -52,8 +59,65 @@ export interface ExtraModelPrice {
52
59
  * `invalid` 表示上游返回 2xx 但响应结构偏离预期(接口改版/漂移),
53
60
  * 与"网络不可达"(unreachable)区分,便于定位是上游变更还是网络问题。 */
54
61
  export type BalanceError = 'unconfigured' | 'unauthorized' | 'unreachable' | 'invalid';
62
+ /** Metadata from the UI making the balance request, for the host login wallet service. */
63
+ export interface BalanceClientMetadata {
64
+ version: string;
65
+ locale: string;
66
+ timezoneOffsetSeconds: number;
67
+ }
68
+ export declare const BALANCE_CLIENT_HEADERS: {
69
+ readonly version: "x-dsh-billing-client-version";
70
+ readonly locale: "x-dsh-billing-client-locale";
71
+ readonly timezone: "x-dsh-billing-client-timezone-offset";
72
+ };
73
+ /**
74
+ * 宿主内置的两条 DeepSeek 通道的路由 id(宿主包 `llm-deepseek-api-key` /
75
+ * `llm-deepseek-account` 注册,`0.1.2-alpha.1` ~ `0.2.3-alpha.3` 全程稳定):
76
+ *
77
+ * - `deepseek-official`:平台 API key 通道,花的是用户自己的 DeepSeek 平台
78
+ * 账户余额(凭据 ref 默认 `DEEPSEEK_API_KEY`);
79
+ * - `deepseek-account`:登录账号通道,花的是宿主登录钱包的余额。
80
+ *
81
+ * 两条通道都**不在 llm-pi-ai 路由表里**(插件自造的 `deepseek` 路由 id 与它们
82
+ * 不同)。钱包归属必须按宿主通道 id 认,否则内置通道的用量桶认不到任何钱包
83
+ * (issue #141:悬浮卡显示「余额不可确认」)。
84
+ */
85
+ export declare const DEEPSEEK_PLATFORM_ROUTE = "deepseek-official";
86
+ export declare const DEEPSEEK_LOGIN_ROUTE = "deepseek-account";
87
+ /** Wire format shared by the aggregation buckets and their consumers. */
88
+ export type SiteKind = 'site' | 'direct' | 'unknown';
89
+ export interface SiteRef {
90
+ kind: SiteKind;
91
+ origin?: string;
92
+ provider: string;
93
+ }
94
+ export declare function siteBucketKey(ref: SiteRef): string;
95
+ type SiteKeyParts = {
96
+ kind: 'site';
97
+ origin: string;
98
+ } | {
99
+ kind: 'direct';
100
+ route: string;
101
+ } | {
102
+ kind: 'unknown';
103
+ };
104
+ export declare function siteKeyParts(siteKey: string): SiteKeyParts;
105
+ /** New servers declare coverage explicitly, including for errors. Legacy rows need an exact route or alias. */
106
+ export declare function balanceCoversSite(row: ProviderBalance, siteKey: string): boolean;
55
107
  /** 一个提供方的账户余额(`/api/billing/balance` 的一行)。 */
56
108
  export interface ProviderBalance {
109
+ /** Actual direct route ID (without `direct:`), including on failures. */
110
+ sourceRoute?: string;
111
+ /** Wallet type, independent of the queried route. */
112
+ walletKind?: 'api-key' | 'account' | 'relay';
113
+ /** Routes whose usage this wallet can cover. An explicit empty list means none. */
114
+ attributedRoutes?: readonly string[];
115
+ /** Explicitly verified same-wallet route IDs, separate from the actual queried route. */
116
+ sourceRouteAliases?: readonly string[];
117
+ /** Exact site origin where this is the uniquely identified wallet. */
118
+ sourceOrigin?: string;
119
+ /** 登录钱包缺席的具体原因(宿主没提供登录服务 / 缺元数据 / 未登录)。 */
120
+ accountState?: 'no-service' | 'no-metadata' | 'signed-out';
57
121
  /** 提供方 id(小写,如 `deepseek`),与模型表 provider 匹配用。 */
58
122
  provider: string;
59
123
  /** 显示名(如 `DeepSeek`)。 */
@@ -62,6 +126,11 @@ export interface ProviderBalance {
62
126
  currency?: string;
63
127
  /** 总可用余额(含赠金与充值)。 */
64
128
  totalBalance?: number;
129
+ /** 独立币种的钱包余额,不与主余额相加。 */
130
+ otherBalances?: readonly {
131
+ currency: string;
132
+ totalBalance: number;
133
+ }[];
65
134
  /** 未过期赠金余额。 */
66
135
  grantedBalance?: number;
67
136
  /** 充值余额。 */
@@ -119,8 +188,10 @@ export interface SubscriptionWindow {
119
188
  }
120
189
  /** One provider's subscription plan quota row. */
121
190
  export interface SubscriptionQuota {
122
- /** Adapter id: `kimi` / `zai` / `opencode-go`. */
191
+ /** Exact provider route ID. */
123
192
  provider: string;
193
+ /** Exact normalized origin when this is the only configured route at the site. */
194
+ origin?: string;
124
195
  /** Human display name (e.g. Kimi For Coding). */
125
196
  displayName: string;
126
197
  /** Plan label; absent when the provider did not name one. */
@@ -151,6 +222,8 @@ export interface RelayQuota {
151
222
  route: string;
152
223
  /** 站点 origin(baseURL 归一化)。 */
153
224
  origin: string;
225
+ /** More than one configured route shares this origin, even if only one was queried. */
226
+ sourceAmbiguous?: true;
154
227
  /** 站点显示名。 */
155
228
  displayName: string;
156
229
  kind: RelayKind;
@@ -278,4 +351,5 @@ export interface DeclaredEndpointConfig {
278
351
  /** 滚动额度窗口列表。 */
279
352
  windows?: readonly DeclaredWindowConfig[];
280
353
  }
354
+ export {};
281
355
  //# sourceMappingURL=pricing-shared.d.ts.map
@@ -9,6 +9,7 @@
9
9
  *
10
10
  * 纯函数:输入上一基准与本次余额查询结果,输出新基准与对账事件,可独立单测。
11
11
  */
12
+ import type { UsageStatsDocument } from './aggregate.ts';
12
13
  /** 对账基准快照:上次余额查询时的官方账户状态,用于反推当日消费。 */
13
14
  export interface BalanceRef {
14
15
  /** 本地日期键(YYYY-MM-DD)。 */
@@ -23,7 +24,15 @@ export interface BalanceRef {
23
24
  currency: string;
24
25
  /** 打基准时刻(epoch ms)。 */
25
26
  at: number;
27
+ source?: string;
28
+ costAtBaseline?: number;
26
29
  }
30
+ /** The daily official aggregate also includes other deepseek-* routes, the official
31
+ * origin (possibly shared), and web search (estimated cost, separate credentials).
32
+ * Require complete site coverage and exclude unrelated routes before comparing. */
33
+ export declare function platformSpend(doc: UsageStatsDocument, day: string, providers: Readonly<Record<string, {
34
+ baseURL?: string;
35
+ }>>, allowDirect?: boolean): number | undefined;
27
36
  /** 一次对账的结果类别。 */
28
37
  export type ReconcileKind = 'baseline' | 'structure-reset' | 'flat' | 'ok' | 'drift';
29
38
  /** 对账事件:`ok`/`drift`/`flat` 携带有意义的结果,`baseline`/`structure-reset` 仅为无消费的基准刷新。 */
@@ -52,9 +61,9 @@ interface BalanceSnapshot {
52
61
  * @param todayOfficialCost - 本地账本当日的官方渠道费用(CNY)。
53
62
  * @param dayKey - 本地日期键(YYYY-MM-DD)。
54
63
  * @param nowMs - 当前时刻(epoch ms)。
55
- * @returns 新基准与对账事件;余额不可用(无 totalBalance)时返回 `{ ref: prevRef, event: null }`。
64
+ * @returns 新基准与对账事件;余额不可用(无 totalBalance)时返回 `{ ref: null, event: null }`。
56
65
  */
57
- export declare function reconcileBalanceDelta(prevRef: BalanceRef | null, balance: BalanceSnapshot, todayOfficialCost: number, dayKey: string, nowMs: number): {
66
+ export declare function reconcileBalanceDelta(prevRef: BalanceRef | null, balance: BalanceSnapshot, todayOfficialCost: number, dayKey: string, nowMs: number, source?: string): {
58
67
  ref: BalanceRef | null;
59
68
  event: ReconcileEvent | null;
60
69
  };
@@ -12,7 +12,8 @@
12
12
  * `cached()` 与 `refreshOnce()` 拿到结果。
13
13
  */
14
14
  import type { Context } from '@deepseek-ai/cordis';
15
- import type { ProviderBalance } from '../pricing-shared.ts';
15
+ import type { IncomingMessage } from 'node:http';
16
+ import { type BalanceClientMetadata, type ProviderBalance } from '../pricing-shared.ts';
16
17
  import type { ReconcileEvent } from '../reconcile.ts';
17
18
  /** 余额响应体形状(来自装配器 `balanceCache.doc`)。 */
18
19
  export interface BalanceDoc {
@@ -32,9 +33,9 @@ interface CachedBalance {
32
33
  export interface BalanceDeps {
33
34
  trustedHosts: ReadonlySet<string>;
34
35
  /** 装配器内缓存 + 时间戳(`Date.now()`)。 */
35
- cached: () => CachedBalance;
36
+ cached: (client?: BalanceClientMetadata) => CachedBalance;
36
37
  /** in-flight 去重的刷新入口。 */
37
- refreshOnce: () => Promise<void>;
38
+ refreshOnce: (client?: BalanceClientMetadata) => Promise<void>;
38
39
  /** 5 分钟缓存间隔(毫秒)。 */
39
40
  cacheMs: number;
40
41
  /**
@@ -48,6 +49,8 @@ export interface BalanceDeps {
48
49
  */
49
50
  responseBudgetMs: number;
50
51
  }
52
+ /** Loopback callers can supply these advisory UI headers. */
53
+ export declare function readClientMetadata(headers: IncomingMessage['headers']): BalanceClientMetadata | undefined;
51
54
  export declare function registerBalanceRoute(ctx: Context, deps: BalanceDeps): void;
52
55
  export {};
53
56
  //# sourceMappingURL=balance.d.ts.map
@@ -0,0 +1,12 @@
1
+ import type { Context } from '@deepseek-ai/cordis';
2
+ import type { ScheduleSnapshot } from '../peak-schedule.ts';
3
+ export interface PeakScheduleDeps {
4
+ trustedHosts: ReadonlySet<string>;
5
+ read: () => Promise<ScheduleSnapshot>;
6
+ save: (raw: unknown) => Promise<ScheduleSnapshot>;
7
+ }
8
+ /** Domain validation failures are distinct from storage failures. */
9
+ export declare class PeakScheduleInputError extends Error {
10
+ }
11
+ export declare function registerPeakScheduleRoute(ctx: Context, deps: PeakScheduleDeps): void;
12
+ //# sourceMappingURL=peak-schedule.d.ts.map
@@ -0,0 +1,11 @@
1
+ import type { Context } from '@deepseek-ai/cordis';
2
+ import type { PricingModel } from '../pricing-shared.ts';
3
+ type PricingModelsResult = {
4
+ models: PricingModel[];
5
+ partial: boolean;
6
+ };
7
+ /** Static settings remain authoritative even when the registry is unavailable. */
8
+ export declare function configuredPricingModels(ctx: Context): Promise<PricingModelsResult>;
9
+ export declare function registerPricingModelsRoute(ctx: Context, trustedHosts: ReadonlySet<string>): void;
10
+ export {};
11
+ //# sourceMappingURL=pricing-models.d.ts.map
@@ -45,6 +45,16 @@ export interface TaskQueueModelSelection {
45
45
  readonly provider: string;
46
46
  readonly model: string;
47
47
  }
48
+ /**
49
+ * 候选为空时的根因分层(issue #143):只说「模型不支持」用户无从下手,必须
50
+ * 分清「宿主没有官方通道」与「官方通道里没有可用模型」——两者的下一步动作
51
+ * 完全不同(配 API Key 通道 vs 看模型清单)。
52
+ */
53
+ export type TaskQueueBlockedReason =
54
+ /** 宿主没有内置的 DeepSeek 官方按量通道(路由 id `deepseek-official`)。 */
55
+ 'no-official-route'
56
+ /** 官方通道在,但其中没有任何带官方峰谷价的模型。 */
57
+ | 'no-official-model';
48
58
  export interface TaskQueueSnapshot {
49
59
  /** All required host Session-control methods are available. */
50
60
  readonly supported: boolean;
@@ -55,6 +65,8 @@ export interface TaskQueueSnapshot {
55
65
  readonly models: readonly TaskQueueModel[];
56
66
  /** Host default route for an unconfigured Session; eligibility is checked separately. */
57
67
  readonly defaultModel?: TaskQueueModelSelection | undefined;
68
+ /** 候选为空的根因(models 为空时才有值)。 */
69
+ readonly blockedReason?: TaskQueueBlockedReason | undefined;
58
70
  readonly optionsError?: string;
59
71
  }
60
72
  /** Lightweight alert polling shape: never transports Prompt or Session content. */
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@kenz1117/dsh-ui-usage-billing",
3
3
  "description": "Usage billing dashboard for DeepSeek Harness: sidebar cost metrics plus a full dashboard modal, priced from a current multi-provider catalog with real usage aggregated from session logs.",
4
- "version": "1.8.11",
4
+ "version": "1.8.13",
5
5
  "keywords": [
6
6
  "deepseek-harness",
7
7
  "deepseek",