dsh-ds-balance 2.1.3 → 2.3.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 +71 -18
- package/README_en.md +76 -18
- package/lib/adapters/domain-core-store.d.ts +46 -2
- package/lib/adapters/domain-core-store.js +144 -4
- package/lib/adapters/host-account.d.ts +66 -0
- package/lib/adapters/host-account.js +68 -0
- package/lib/client.js +437 -95
- package/lib/domain/account.d.ts +40 -0
- package/lib/domain/account.js +74 -0
- package/lib/domain/balance.d.ts +40 -1
- package/lib/domain/balance.js +20 -1
- package/lib/domain/normalize.d.ts +3 -2
- package/lib/domain/normalize.js +3 -2
- package/lib/domain/severity.d.ts +8 -2
- package/lib/domain/severity.js +11 -4
- package/lib/domain/situation.d.ts +104 -0
- package/lib/domain/situation.js +93 -0
- package/lib/http/handlers.d.ts +1 -1
- package/lib/http/handlers.js +29 -5
- package/lib/http/wire.d.ts +11 -1
- package/lib/http/wire.js +2 -0
- package/lib/index.js +122 -6
- package/lib/ports/account.d.ts +27 -0
- package/lib/ports/account.js +9 -0
- package/lib/ports/core-store.d.ts +15 -0
- package/lib/services/balance-service.d.ts +66 -94
- package/lib/services/balance-service.js +132 -196
- package/lib/services/balance-source.d.ts +187 -0
- package/lib/services/balance-source.js +320 -0
- package/lib/services/scheduler.d.ts +8 -2
- package/lib/services/scheduler.js +1 -1
- package/lib/services/source-readers.d.ts +28 -0
- package/lib/services/source-readers.js +83 -0
- package/lib/services/source-selector.d.ts +50 -0
- package/lib/services/source-selector.js +64 -0
- package/lib/types/client/api-types.d.ts +16 -0
- package/lib/types/client/data.d.ts +12 -3
- package/lib/types/client/index.d.ts +6 -0
- package/lib/types/client/locales.d.ts +10 -18
- package/lib/types/client/mock/index.d.ts +12 -11
- package/lib/types/client/mock/scenarios.d.ts +13 -5
- package/lib/types/client/model.d.ts +24 -25
- package/lib/types/client/route-hint.d.ts +61 -0
- package/lib/types/client/sidebar/BalancePopover.d.ts +7 -0
- package/lib/types/client/sidebar/PercentRing.d.ts +13 -5
- package/lib/types/client/sidebar/SidebarBalance.d.ts +9 -1
- package/lib/types/client/situation.d.ts +164 -0
- package/lib/version.d.ts +1 -1
- package/lib/version.js +1 -1
- package/package.json +25 -23
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 宿主账号服务 → {@link AccountSource}。
|
|
3
|
+
*
|
|
4
|
+
* 这是**可选**接缝:`apply` 里由 `ctx.inject(['deepseekAccount'], …)` 把门,服务缺席时整个对象
|
|
5
|
+
* 不存在,插件退回只看 API Key。
|
|
6
|
+
*
|
|
7
|
+
* 服务面用**鸭子类型**收窄而不是 import 宿主类型:`ctx.deepseekAccount` 的类型来自
|
|
8
|
+
* `@deepseek-ai/dsh-deepseek-account`,而运行时提供方是 `-account-platform` —— 引类型要多挂一个包,
|
|
9
|
+
* 而这里只用到三个方法。运行期关系由 `ctx.inject` 表达,声明面由 `peerDependencies` 表达。
|
|
10
|
+
* @module dsh-ds-balance/adapters/host-account
|
|
11
|
+
*/
|
|
12
|
+
import type { AccountWallet } from '../domain/account.js';
|
|
13
|
+
import type { AccountSource } from '../ports/account.js';
|
|
14
|
+
import type { Logger } from '../ports/logger.js';
|
|
15
|
+
/** 宿主单次余额查询的结果形状(对齐 `AccountDetails['balance']`)。 */
|
|
16
|
+
export type HostAccountBalance = {
|
|
17
|
+
readonly status: 'ready';
|
|
18
|
+
readonly value: readonly AccountWallet[];
|
|
19
|
+
readonly bonusWallets: readonly AccountWallet[];
|
|
20
|
+
} | {
|
|
21
|
+
readonly status: 'failed';
|
|
22
|
+
};
|
|
23
|
+
/** 宿主为这次调用要的客户端标识(对齐 `AccountClientMetadata`)。 */
|
|
24
|
+
export interface HostAccountClient {
|
|
25
|
+
readonly version: string;
|
|
26
|
+
readonly locale: string;
|
|
27
|
+
readonly timezoneOffsetSeconds: number;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* 宿主账号服务的最小面。
|
|
31
|
+
*
|
|
32
|
+
* 三个方法的语义逐字对齐宿主 `DeepSeekAccount` 的公开面;`null` 一律表示**此刻没有可用账号**
|
|
33
|
+
* (未登录,或凭据在查询途中被换掉),不是「余额为零」。
|
|
34
|
+
*/
|
|
35
|
+
export interface HostAccountFace {
|
|
36
|
+
getState(): Promise<{
|
|
37
|
+
readonly status?: string;
|
|
38
|
+
} | undefined>;
|
|
39
|
+
getBalance(client: HostAccountClient): Promise<HostAccountBalance | null>;
|
|
40
|
+
getPlatformSession(): Promise<{
|
|
41
|
+
readonly userId?: string | null;
|
|
42
|
+
} | null>;
|
|
43
|
+
}
|
|
44
|
+
/** 构造参数。 */
|
|
45
|
+
export interface HostAccountOptions {
|
|
46
|
+
service: HostAccountFace;
|
|
47
|
+
/** 上报给 Platform 的客户端版本;用插件版本,与响应里回传的那个同源。 */
|
|
48
|
+
version: string;
|
|
49
|
+
/** 上报给 Platform 的语言;只选服务端文案,界面文案由本插件自己的词典给。 */
|
|
50
|
+
locale?: string;
|
|
51
|
+
/** 上报给 Platform 的时区偏移(秒,东为正)。 */
|
|
52
|
+
timezoneOffsetSeconds?: () => number;
|
|
53
|
+
logger?: Logger | undefined;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* 把宿主账号服务收窄成 {@link AccountSource}。
|
|
57
|
+
*
|
|
58
|
+
* `readBalance` 的三种结果各有去处,**不合并**:
|
|
59
|
+
* - `null` → 回 `null`,上层回落另一条取数路;
|
|
60
|
+
* - `failed` → 抛 `UpstreamError`,**不回落**(回落只会把真因换成更难懂的 `NO_KEY`);
|
|
61
|
+
* - `ready` → 原样交出去。
|
|
62
|
+
* @param options - 宿主服务与客户端标识。
|
|
63
|
+
* @returns 端口实现。
|
|
64
|
+
*/
|
|
65
|
+
export declare function createAccountSource(options: HostAccountOptions): AccountSource;
|
|
66
|
+
//# sourceMappingURL=host-account.d.ts.map
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 宿主账号服务 → {@link AccountSource}。
|
|
3
|
+
*
|
|
4
|
+
* 这是**可选**接缝:`apply` 里由 `ctx.inject(['deepseekAccount'], …)` 把门,服务缺席时整个对象
|
|
5
|
+
* 不存在,插件退回只看 API Key。
|
|
6
|
+
*
|
|
7
|
+
* 服务面用**鸭子类型**收窄而不是 import 宿主类型:`ctx.deepseekAccount` 的类型来自
|
|
8
|
+
* `@deepseek-ai/dsh-deepseek-account`,而运行时提供方是 `-account-platform` —— 引类型要多挂一个包,
|
|
9
|
+
* 而这里只用到三个方法。运行期关系由 `ctx.inject` 表达,声明面由 `peerDependencies` 表达。
|
|
10
|
+
* @module dsh-ds-balance/adapters/host-account
|
|
11
|
+
*/
|
|
12
|
+
import { UpstreamError, describeError } from '../domain/errors.js';
|
|
13
|
+
/**
|
|
14
|
+
* 把宿主账号服务收窄成 {@link AccountSource}。
|
|
15
|
+
*
|
|
16
|
+
* `readBalance` 的三种结果各有去处,**不合并**:
|
|
17
|
+
* - `null` → 回 `null`,上层回落另一条取数路;
|
|
18
|
+
* - `failed` → 抛 `UpstreamError`,**不回落**(回落只会把真因换成更难懂的 `NO_KEY`);
|
|
19
|
+
* - `ready` → 原样交出去。
|
|
20
|
+
* @param options - 宿主服务与客户端标识。
|
|
21
|
+
* @returns 端口实现。
|
|
22
|
+
*/
|
|
23
|
+
export function createAccountSource(options) {
|
|
24
|
+
const { service } = options;
|
|
25
|
+
const client = () => ({
|
|
26
|
+
version: options.version,
|
|
27
|
+
locale: options.locale ?? 'zh-CN',
|
|
28
|
+
timezoneOffsetSeconds: options.timezoneOffsetSeconds?.() ?? 0,
|
|
29
|
+
});
|
|
30
|
+
return {
|
|
31
|
+
async signedIn() {
|
|
32
|
+
try {
|
|
33
|
+
const state = await service.getState();
|
|
34
|
+
return state?.status === 'credential-stored';
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
options.logger?.debug('ds-balance: account state unreadable', {
|
|
38
|
+
error: describeError(error),
|
|
39
|
+
});
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
async accountId() {
|
|
44
|
+
try {
|
|
45
|
+
const session = await service.getPlatformSession();
|
|
46
|
+
const id = session?.userId;
|
|
47
|
+
return typeof id === 'string' && id !== '' ? id : null;
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
options.logger?.debug('ds-balance: account identity unreadable', {
|
|
51
|
+
error: describeError(error),
|
|
52
|
+
});
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
async readBalance() {
|
|
57
|
+
const result = await service.getBalance(client());
|
|
58
|
+
if (result === null)
|
|
59
|
+
return null;
|
|
60
|
+
if (result.status === 'failed') {
|
|
61
|
+
// 502 只是给分类用的:账号服务的查询失败属于上游不可用,不是「没有额度」。
|
|
62
|
+
throw new UpstreamError(502, 'account balance query failed');
|
|
63
|
+
}
|
|
64
|
+
return { wallets: result.value, bonusWallets: result.bonusWallets };
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=host-account.js.map
|