@linxin666/dsh-remote-bridge 0.4.8 → 0.4.10
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.i18n.yaml +2 -2
- package/README.md +75 -18
- package/README.zh.md +74 -18
- package/lib/client.js +783 -260
- package/lib/client.js.map +1 -1
- package/lib/index.js +15849 -2913
- package/lib/types/client/PairEntry.d.ts +2 -2
- package/lib/types/client/PairPanel.d.ts +10 -2
- package/lib/types/client/UiPresentRow.d.ts +13 -0
- package/lib/types/client/icons.d.ts +5 -13
- package/lib/types/client/index.d.ts +4 -0
- package/lib/types/client/locales.d.ts +24 -15
- package/lib/types/client/semantic.d.ts +9 -5
- package/lib/types/client/toolview-slot.d.ts +68 -0
- package/lib/types/client/ui-card.d.ts +160 -0
- package/lib/types/core/commands.d.ts +59 -0
- package/lib/types/core/connection.d.ts +106 -0
- package/lib/types/core/devices.d.ts +102 -2
- package/lib/types/core/extension.d.ts +13 -0
- package/lib/types/core/hub.d.ts +129 -15
- package/lib/types/core/liveactivity.d.ts +324 -0
- package/lib/types/core/protocol.d.ts +328 -5
- package/lib/types/core/push.d.ts +186 -0
- package/lib/types/core/questions.d.ts +61 -9
- package/lib/types/core/session-meta.d.ts +55 -1
- package/lib/types/core/squads.d.ts +130 -0
- package/lib/types/core/turn-end-quiet.d.ts +135 -0
- package/lib/types/core/ui.d.ts +238 -0
- package/lib/types/core/usage-stats.d.ts +160 -0
- package/lib/types/core/usage.d.ts +294 -0
- package/lib/types/host/apns.d.ts +151 -0
- package/lib/types/host/squads-ledger.d.ts +39 -0
- package/lib/types/host/squads.d.ts +77 -0
- package/lib/types/host/ui-tools.d.ts +80 -0
- package/lib/types/host/usage-stats.d.ts +75 -0
- package/lib/types/host/watchers.d.ts +95 -0
- package/lib/types/index.d.ts +47 -3
- package/package.json +15 -14
- package/src/client/PairEntry.tsx +58 -31
- package/src/client/PairPanel.tsx +181 -97
- package/src/client/UiPresentRow.tsx +149 -0
- package/src/client/icons.tsx +17 -60
- package/src/client/index.ts +11 -0
- package/src/client/locales.ts +36 -24
- package/src/client/panel.module.css +239 -186
- package/src/client/semantic.ts +10 -6
- package/src/client/toolview-slot.ts +84 -0
- package/src/client/ui-card.ts +368 -0
- package/src/client/ui-present.module.css +87 -0
- package/src/core/commands.ts +95 -0
- package/src/core/connection.ts +811 -12
- package/src/core/devices.ts +337 -5
- package/src/core/extension.ts +20 -0
- package/src/core/hub.ts +518 -43
- package/src/core/liveactivity.ts +833 -0
- package/src/core/protocol.ts +370 -5
- package/src/core/push.ts +385 -0
- package/src/core/questions.ts +81 -10
- package/src/core/session-meta.ts +73 -1
- package/src/core/squads.ts +345 -0
- package/src/core/turn-end-quiet.ts +388 -0
- package/src/core/ui.ts +398 -0
- package/src/core/usage-stats.ts +424 -0
- package/src/core/usage.ts +937 -0
- package/src/core/wire.ts +8 -0
- package/src/host/apns.ts +320 -0
- package/src/host/squads-ledger.ts +105 -0
- package/src/host/squads.ts +206 -0
- package/src/host/ui-tools.ts +233 -0
- package/src/host/usage-stats.ts +155 -0
- package/src/host/watchers.ts +296 -0
- package/src/index.ts +327 -10
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The bridge's claims on `ui.request` (§35): which self-contained pages a phone
|
|
3
|
+
* or the desktop may show, and the one path a user's action travels back into the
|
|
4
|
+
* tool call that presented it.
|
|
5
|
+
*
|
|
6
|
+
* A card belongs to exactly one tool call and ends at the user's FIRST action:
|
|
7
|
+
* the first `ui.event` settles it and becomes that call's result, so a card that
|
|
8
|
+
* should keep taking actions is a second presentation rather than a second event
|
|
9
|
+
* (§35.3, §35.8). Every claim is one record keyed by the call id published on the
|
|
10
|
+
* wire, so an event that arrives late still lands, and one naming a call id this
|
|
11
|
+
* bridge never claimed is refused instead of being handed to a tool that is not
|
|
12
|
+
* waiting for it.
|
|
13
|
+
*
|
|
14
|
+
* The bridge invents no deadline: the tool schema declares no `timeout`, so a
|
|
15
|
+
* card carries no `expiresAt` and stays open until the user acts, the agent's own
|
|
16
|
+
* turn is cancelled, or the last paired device has been gone long enough for the
|
|
17
|
+
* hub's grace window (§35.5). It also fabricates no event: a card nobody answered
|
|
18
|
+
* is released as a refusal, never as a decision the operator did not make.
|
|
19
|
+
*
|
|
20
|
+
* Both caps are enforced on this side rather than the client's (§35.2, §35.3): a
|
|
21
|
+
* client that refused an oversized page would leave the agent waiting for a card
|
|
22
|
+
* that never comes, so the call is refused as `invalid-args` before any frame is
|
|
23
|
+
* written.
|
|
24
|
+
* @module dsh-remote-bridge/core/ui
|
|
25
|
+
*/
|
|
26
|
+
/** Upper bound of one card's document, in UTF-8 bytes (§35.2). */
|
|
27
|
+
export declare const UI_HTML_MAX_BYTES = 262144;
|
|
28
|
+
/** Upper bound of one `ui.event` payload, in UTF-8 bytes (§35.3). */
|
|
29
|
+
export declare const UI_PAYLOAD_MAX_BYTES = 8192;
|
|
30
|
+
/**
|
|
31
|
+
* How many settled call ids this bridge remembers, so a repeated tap is answered
|
|
32
|
+
* `ui-settled` instead of `ui-unknown` (§35.3).
|
|
33
|
+
*
|
|
34
|
+
* The promise is already settled and the card is already gone by then; the only
|
|
35
|
+
* reason to remember the id is that a client which tapped twice must be able to
|
|
36
|
+
* tell "already done" from "never mine". A phone holds no more live cards than it
|
|
37
|
+
* rendered, so the bound costs one short string per presentation and never grows
|
|
38
|
+
* with the process's uptime.
|
|
39
|
+
*/
|
|
40
|
+
export declare const UI_SETTLED_MEMORY = 256;
|
|
41
|
+
/** Whether a card still holds the tool call, or is only a notice now. */
|
|
42
|
+
export type UiState = 'open' | 'settled';
|
|
43
|
+
/** One lossless JSON value, as the wire carries it inside an event payload. */
|
|
44
|
+
export type JsonValue = null | boolean | number | string | JsonValue[] | {
|
|
45
|
+
[key: string]: JsonValue;
|
|
46
|
+
};
|
|
47
|
+
/** The action a user took inside one card, in the presenter's own closed set. */
|
|
48
|
+
export interface UiEvent {
|
|
49
|
+
readonly type: string;
|
|
50
|
+
readonly payload?: JsonValue;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The `ui.request` payload for one claim, and the `payload` of every `ui.list`
|
|
54
|
+
* entry.
|
|
55
|
+
*
|
|
56
|
+
* One function for both so the card a client was handed and the card it reads back
|
|
57
|
+
* out of a list cannot drift apart: an absent `title` or `height` is an absent key
|
|
58
|
+
* rather than an empty or null one, exactly as §35.2 froze it. `expiresAt` is never
|
|
59
|
+
* written: a presentation whose tool schema declares no `timeout` has no deadline
|
|
60
|
+
* to publish, and a published one would be a promise this bridge does not keep.
|
|
61
|
+
* @param state - whether the card still holds the tool call.
|
|
62
|
+
* @param card - the claim being published.
|
|
63
|
+
* @returns the frozen payload body.
|
|
64
|
+
*/
|
|
65
|
+
export declare function uiPayload(state: UiState, card: UiCardView): UiRequestPayload;
|
|
66
|
+
/**
|
|
67
|
+
* The identity a client knows one card by (`"<sessionId>/<callId>"`, §35.4).
|
|
68
|
+
*
|
|
69
|
+
* The same pair a `ui.list` row carries, so the card a tool presented and the row
|
|
70
|
+
* a client reconciles it against are compared as one name instead of two fields
|
|
71
|
+
* that could be recombined differently on each side.
|
|
72
|
+
* @param sessionId - the session whose agent presented the card.
|
|
73
|
+
* @param callId - the tool call id it is held under.
|
|
74
|
+
* @returns the card key.
|
|
75
|
+
*/
|
|
76
|
+
export declare function uiKey(sessionId: string, callId: string): string;
|
|
77
|
+
/** @returns whether one card document is within {@link UI_HTML_MAX_BYTES}. */
|
|
78
|
+
export declare function uiHtmlFits(html: string): boolean;
|
|
79
|
+
/**
|
|
80
|
+
* Read one `ui.event` body out of a decoded frame.
|
|
81
|
+
*
|
|
82
|
+
* The event's `type` is the presenter's own closed set: this bridge carries it and
|
|
83
|
+
* never interprets the value, so the only shape it enforces is the one the frame
|
|
84
|
+
* declares (§35.3). An absent and an explicit `null` payload are different
|
|
85
|
+
* things — the payload key is kept exactly when the client sent one.
|
|
86
|
+
* @param raw - the `event` field as it crossed the wire.
|
|
87
|
+
* @returns the event, or undefined when the frame is not one.
|
|
88
|
+
*/
|
|
89
|
+
export declare function uiEventOf(raw: unknown): UiEvent | undefined;
|
|
90
|
+
/** @returns whether one event's payload is within {@link UI_PAYLOAD_MAX_BYTES}. */
|
|
91
|
+
export declare function uiEventFits(event: UiEvent): boolean;
|
|
92
|
+
/** One card this bridge still holds, as `ui.list` publishes it (§35.4). */
|
|
93
|
+
export interface ListedCard {
|
|
94
|
+
/** {@link uiKey}: the identity the client holds it under. */
|
|
95
|
+
readonly key: string;
|
|
96
|
+
readonly sessionId: string;
|
|
97
|
+
/** `open` or `settled`; a settled card is not held and never listed. */
|
|
98
|
+
readonly state: UiState;
|
|
99
|
+
/** The `ui.request` payload, byte for byte the shape §35.2 froze. */
|
|
100
|
+
readonly payload: UiRequestPayload;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* The body of one `ui.list` answer.
|
|
104
|
+
*
|
|
105
|
+
* `complete` is what a client is allowed to act on: only a COMPLETE list may
|
|
106
|
+
* retire a card it does not carry (§35.5, §24.3), so a bridge that could answer
|
|
107
|
+
* only part of the set must say `false` rather than let the difference read as
|
|
108
|
+
* "the card is over".
|
|
109
|
+
*/
|
|
110
|
+
export interface UiListPayload {
|
|
111
|
+
readonly cards: readonly ListedCard[];
|
|
112
|
+
readonly complete: boolean;
|
|
113
|
+
}
|
|
114
|
+
/** `ui.request`'s payload, as §35.2 froze it. */
|
|
115
|
+
export interface UiRequestPayload {
|
|
116
|
+
readonly sessionId: string;
|
|
117
|
+
readonly callId: string;
|
|
118
|
+
readonly state: UiState;
|
|
119
|
+
/** One header line; absent when the presenter named none. */
|
|
120
|
+
readonly title?: string;
|
|
121
|
+
/** A whole self-contained document, `<!doctype html>` first, within {@link UI_HTML_MAX_BYTES}. */
|
|
122
|
+
readonly html: string;
|
|
123
|
+
/** The presenter's suggested initial height in points; the client still measures. */
|
|
124
|
+
readonly height?: number;
|
|
125
|
+
/**
|
|
126
|
+
* Epoch milliseconds of this bridge's own deadline for the card; absent, always,
|
|
127
|
+
* for a presentation whose tool schema declares no `timeout` (§35.2).
|
|
128
|
+
*/
|
|
129
|
+
readonly expiresAt?: number;
|
|
130
|
+
}
|
|
131
|
+
/** The identity and content of one claim, as the hub publishes it. */
|
|
132
|
+
export interface UiCardView {
|
|
133
|
+
readonly sessionId: string;
|
|
134
|
+
readonly callId: string;
|
|
135
|
+
readonly html: string;
|
|
136
|
+
readonly title?: string;
|
|
137
|
+
readonly height?: number;
|
|
138
|
+
}
|
|
139
|
+
/** Why one presentation ended without a user action, in the wire's own vocabulary. */
|
|
140
|
+
export type UiRefusalReason = 'unavailable' | 'invalid-args' | 'timeout' | 'cancelled';
|
|
141
|
+
/** What one presentation ended as: the user's action, or the refusal to wait for one. */
|
|
142
|
+
export type UiPresentOutcome = {
|
|
143
|
+
readonly ok: true;
|
|
144
|
+
readonly type: string;
|
|
145
|
+
readonly payload?: JsonValue;
|
|
146
|
+
} | {
|
|
147
|
+
readonly ok: false;
|
|
148
|
+
readonly reason: UiRefusalReason;
|
|
149
|
+
};
|
|
150
|
+
/** What one `ui.event` produced: acknowledgement, or the refusal to accept it. */
|
|
151
|
+
export type UiEventReply = {
|
|
152
|
+
readonly ok: true;
|
|
153
|
+
} | {
|
|
154
|
+
readonly ok: false;
|
|
155
|
+
readonly code: string;
|
|
156
|
+
readonly msg: string;
|
|
157
|
+
};
|
|
158
|
+
export interface OpenCardInput {
|
|
159
|
+
readonly sessionId: string;
|
|
160
|
+
/** The id published on the wire, which is the host's own `ToolCallId`. */
|
|
161
|
+
readonly callId: string;
|
|
162
|
+
readonly html: string;
|
|
163
|
+
readonly title?: string;
|
|
164
|
+
readonly height?: number;
|
|
165
|
+
/** The agent's own cancellation lifetime for this call. */
|
|
166
|
+
readonly signal?: AbortSignal;
|
|
167
|
+
/**
|
|
168
|
+
* Called when the card leaves this bridge — the user's event, the grace window,
|
|
169
|
+
* a cancelled turn, the bridge unloading. The client still holds a card for it
|
|
170
|
+
* and has to be told, or it keeps showing a page whose actions go nowhere.
|
|
171
|
+
*/
|
|
172
|
+
readonly onSettled?: (card: UiCardView) => void;
|
|
173
|
+
}
|
|
174
|
+
/** The presentation one `ui.present` call is waiting on, as the hub publishes it. */
|
|
175
|
+
export interface ClaimedCard extends UiCardView {
|
|
176
|
+
/** The user's action, or the refusal that ended the card. Never rejects. */
|
|
177
|
+
readonly outcome: Promise<UiPresentOutcome>;
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Pending cards of one bridge process.
|
|
181
|
+
*
|
|
182
|
+
* One record per call id published on the wire and one settle for every path
|
|
183
|
+
* (the user's event, the agent's cancellation, the grace window, bridge unload),
|
|
184
|
+
* so a card can never be settled twice and a released claim leaves no abort
|
|
185
|
+
* listener behind.
|
|
186
|
+
*/
|
|
187
|
+
export declare class UiCards {
|
|
188
|
+
#private;
|
|
189
|
+
/** @returns how many cards this bridge currently holds. */
|
|
190
|
+
get size(): number;
|
|
191
|
+
/**
|
|
192
|
+
* @param callId - one wire call id.
|
|
193
|
+
* @returns `open` while it holds a tool call, `settled` once a user's event
|
|
194
|
+
* settled it, and undefined when this bridge never claimed it — which is how a
|
|
195
|
+
* listener tells `ui-settled` from `ui-unknown` (§35.3).
|
|
196
|
+
*/
|
|
197
|
+
stateOf(callId: string): UiState | undefined;
|
|
198
|
+
/**
|
|
199
|
+
* Claim one card.
|
|
200
|
+
* @param input - session identity, wire call id, the page, and the host's lifetime.
|
|
201
|
+
* @returns the claim the hub publishes, or undefined when there is nothing to publish.
|
|
202
|
+
*/
|
|
203
|
+
open(input: OpenCardInput): ClaimedCard | undefined;
|
|
204
|
+
/**
|
|
205
|
+
* Settle one card from the client's first event (§35.3).
|
|
206
|
+
*
|
|
207
|
+
* The event is the tool call's result and its only result: the card leaves the
|
|
208
|
+
* held table in the same step, so a second event finds it settled rather than
|
|
209
|
+
* settling the call twice.
|
|
210
|
+
* @param sessionId - the session the client believes owns the card.
|
|
211
|
+
* @param callId - the wire call id the client was shown.
|
|
212
|
+
* @param event - the user's action, carried verbatim.
|
|
213
|
+
*/
|
|
214
|
+
settle(sessionId: string, callId: string, event: UiEvent): UiEventReply;
|
|
215
|
+
/**
|
|
216
|
+
* Every card this bridge still holds, as `ui.list` publishes it (§35.4).
|
|
217
|
+
*
|
|
218
|
+
* The claim registry IS the answer: a card leaves it the moment the user settles
|
|
219
|
+
* it, the grace window releases it, or the bridge unloads, so a card missing from
|
|
220
|
+
* this list is one no action can reach any more. Only `open` is ever listed —
|
|
221
|
+
* `settled` is the one-way notice, never a held row.
|
|
222
|
+
* @returns one entry per held card, in claim order.
|
|
223
|
+
*/
|
|
224
|
+
listed(): readonly ListedCard[];
|
|
225
|
+
/**
|
|
226
|
+
* Withdraw the named cards, ending each tool call with a refusal.
|
|
227
|
+
* @param callIds - the cards to release.
|
|
228
|
+
* @param reason - why; carried into the tool result as it stands.
|
|
229
|
+
* @returns how many were still open and therefore released.
|
|
230
|
+
*/
|
|
231
|
+
withdraw(callIds: readonly string[], reason?: UiRefusalReason): number;
|
|
232
|
+
/**
|
|
233
|
+
* Withdraw every claim.
|
|
234
|
+
* @param reason - why; carried into every released tool call.
|
|
235
|
+
* @returns how many cards were still open.
|
|
236
|
+
*/
|
|
237
|
+
withdrawAll(reason?: UiRefusalReason): number;
|
|
238
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `usage.stats` 的应答:宿主自己那份按天记的 token 台账的聚合。
|
|
3
|
+
*
|
|
4
|
+
* 这一半与 `usage.ts` 的两半有一个根本区别:那两半读的是本进程自己的事实,这一半
|
|
5
|
+
* 读的是另一个插件(`dsh-usage`)挂在这个宿主上的 HTTP 路由。所以它的缺席方式也
|
|
6
|
+
* 不同——宿主根本没挂那个插件是一件关于这台 Mac 的事实(`no-usage-plugin`),
|
|
7
|
+
* 不是错误;而路由取不到、形状不符、超时才是 `probe-failed`。
|
|
8
|
+
*
|
|
9
|
+
* 两条铁律贯穿本模块:
|
|
10
|
+
*
|
|
11
|
+
* - 永不编 0。五个桶在一行里恒在,但那说的是「这一行读到过」:一次没读到的聚合
|
|
12
|
+
* 回 `probe-failed` 并带上短原因,绝不拿上一轮的值冒充这一轮。日历上某一天宿主
|
|
13
|
+
* 台账里没有条目,是那一天确实没有用量——宿主自己的 `/day` 路由对这种日子答的
|
|
14
|
+
* 也是全 0,所以按 0 收是转述,不是编造。
|
|
15
|
+
* - 宿主侧 30 秒一次。同一份聚合数据在一个窗口内最多读一次,并发调用合并成一次:
|
|
16
|
+
* 台账是这台 Mac 的事实,一部手机重复问不该让宿主多读一遍。
|
|
17
|
+
*
|
|
18
|
+
* 读的是两条路由(都在那个插件的 `/api/dsh-usage` 前缀下):`GET /overview` 一次
|
|
19
|
+
* 给出今天、最近 30 个有记录的日子的台账、以及观测花费;`GET /day?date=YYYY-MM-DD`
|
|
20
|
+
* 补一天。所以一次聚合是 1 + K 次本机回环读,K 只在请求的窗口比 `/overview` 自带的
|
|
21
|
+
* 那一截更深、或本月台账被那一截截断时才不为 0。
|
|
22
|
+
* @module dsh-remote-bridge/core/usage-stats
|
|
23
|
+
*/
|
|
24
|
+
/** 省略 `days` 时回多少天。 */
|
|
25
|
+
export declare const STATS_DAYS_DEFAULT = 14;
|
|
26
|
+
/** 一次最多回多少天:再往上按这个数收。 */
|
|
27
|
+
export declare const STATS_DAYS_MAX = 60;
|
|
28
|
+
/** 同一份聚合数据在这个窗口内最多读一次(本桥这一侧)。 */
|
|
29
|
+
export declare const STATS_CACHE_TTL_MS = 30000;
|
|
30
|
+
/** 最多记住几个不同窗口的聚合;一次开屏通常只问一个。 */
|
|
31
|
+
export declare const STATS_CACHE_LIMIT = 8;
|
|
32
|
+
/** 一次读数里的一行:五个互不重叠的 token 桶,未用即真实的 0。 */
|
|
33
|
+
export interface UsageBuckets {
|
|
34
|
+
/** 未命中缓存的输入 token。 */
|
|
35
|
+
readonly input: number;
|
|
36
|
+
/** 输出 token(含推理)。 */
|
|
37
|
+
readonly output: number;
|
|
38
|
+
/** 命中缓存的输入 token。 */
|
|
39
|
+
readonly cacheRead: number;
|
|
40
|
+
/** 写入缓存的 token。 */
|
|
41
|
+
readonly cacheWrite: number;
|
|
42
|
+
/** 推理 token,provider 报过才有。 */
|
|
43
|
+
readonly reasoning: number;
|
|
44
|
+
}
|
|
45
|
+
/** 按天的一行:日期键 `YYYY-MM-DD` 加上五个桶。 */
|
|
46
|
+
export interface UsageDayBucket extends UsageBuckets {
|
|
47
|
+
/** 本地日期键 `YYYY-MM-DD`。 */
|
|
48
|
+
readonly day: string;
|
|
49
|
+
}
|
|
50
|
+
/** 观测到的真实花费:官方账户余额的下降累计,CNY 口径。 */
|
|
51
|
+
export interface UsageObservedSpend {
|
|
52
|
+
/** 累计花费,元。 */
|
|
53
|
+
readonly cny: number;
|
|
54
|
+
/** 这次累计起点的那次余额观测时刻(epoch ms)。 */
|
|
55
|
+
readonly since: number;
|
|
56
|
+
}
|
|
57
|
+
/** `usage.stats` 读得到时回的那一份。 */
|
|
58
|
+
export interface UsageStatsView {
|
|
59
|
+
/** 宿主拼这份聚合的时刻(epoch ms),不是本桥答话的时刻。 */
|
|
60
|
+
readonly updatedAt: number;
|
|
61
|
+
/** 宿主本地的今天。 */
|
|
62
|
+
readonly today: UsageBuckets;
|
|
63
|
+
/** 宿主本地的本月,从 1 号到今天的累计。 */
|
|
64
|
+
readonly month: UsageBuckets;
|
|
65
|
+
/** 请求的窗口内按日期升序的一天一行,最多 `days` 条。 */
|
|
66
|
+
readonly days: UsageDayBucket[];
|
|
67
|
+
/** 宿主有观测花费时才有这个键。 */
|
|
68
|
+
readonly observedSpend?: UsageObservedSpend;
|
|
69
|
+
}
|
|
70
|
+
/** 读不到时回的那一份:为什么读不到,不是错误码。 */
|
|
71
|
+
export interface UsageStatsUnsupported {
|
|
72
|
+
/**
|
|
73
|
+
* `no-usage-plugin` —— 这台宿主没挂 `dsh-usage`(关于这台 Mac 的事实);
|
|
74
|
+
* `probe-failed` —— 路由取不到、形状不符或超时。
|
|
75
|
+
*/
|
|
76
|
+
readonly unsupported: 'no-usage-plugin' | 'probe-failed';
|
|
77
|
+
/** 短原因,只给 `probe-failed`。 */
|
|
78
|
+
readonly reason?: string;
|
|
79
|
+
}
|
|
80
|
+
/** `usage.stats` 的应答体。 */
|
|
81
|
+
export type UsageStatsAnswer = UsageStatsView | UsageStatsUnsupported;
|
|
82
|
+
/**
|
|
83
|
+
* 一次宿主路由读数的结果。
|
|
84
|
+
*
|
|
85
|
+
* `missing` 单独一格,是因为「路由不存在」与「路由答了错」在协议里是两个答案:
|
|
86
|
+
* 前者说明那台宿主没挂插件,后者说明这次没读到。
|
|
87
|
+
*/
|
|
88
|
+
export type UsageStatsOutcome = {
|
|
89
|
+
readonly kind: 'answer';
|
|
90
|
+
readonly body: unknown;
|
|
91
|
+
} | {
|
|
92
|
+
readonly kind: 'missing';
|
|
93
|
+
} | {
|
|
94
|
+
readonly kind: 'failed';
|
|
95
|
+
readonly reason: string;
|
|
96
|
+
};
|
|
97
|
+
/** 宿主那两条路由,按本模块需要的那一格给出来。 */
|
|
98
|
+
export interface UsageStatsTransport {
|
|
99
|
+
/** `GET /api/dsh-usage/overview`。 */
|
|
100
|
+
overview(): Promise<UsageStatsOutcome>;
|
|
101
|
+
/**
|
|
102
|
+
* `GET /api/dsh-usage/day?date=<day>`。
|
|
103
|
+
* @param day - 本地日期键 `YYYY-MM-DD`。
|
|
104
|
+
*/
|
|
105
|
+
day(day: string): Promise<UsageStatsOutcome>;
|
|
106
|
+
}
|
|
107
|
+
/** 构造 {@link UsageStatsReader} 的全部输入。 */
|
|
108
|
+
export interface UsageStatsReaderOptions {
|
|
109
|
+
/** 每次读数都重新解析一次;晚挂载的宿主路由因此也能被读到。 */
|
|
110
|
+
readonly transport: () => UsageStatsTransport | undefined;
|
|
111
|
+
/** 缓存窗口读的时钟。 */
|
|
112
|
+
readonly now: () => number;
|
|
113
|
+
/** 读不到时的落点。 */
|
|
114
|
+
readonly log: (message: string) => void;
|
|
115
|
+
/** 缓存窗口;默认 {@link STATS_CACHE_TTL_MS}。 */
|
|
116
|
+
readonly ttlMs?: number;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* `usage.stats` 背后的读服务,一个宿主一份、所有连接共享。
|
|
120
|
+
*
|
|
121
|
+
* 缓存与合并都按窗口值分开放:两个客户端问 14 天和问 30 天是两份不同的聚合,但
|
|
122
|
+
* 它们各自的窗口内都不会让宿主多读一遍。
|
|
123
|
+
*/
|
|
124
|
+
export declare class UsageStatsReader {
|
|
125
|
+
#private;
|
|
126
|
+
/**
|
|
127
|
+
* @param options - 宿主路由的解析函数、时钟与日志。
|
|
128
|
+
*/
|
|
129
|
+
constructor(options: UsageStatsReaderOptions);
|
|
130
|
+
/**
|
|
131
|
+
* 一个窗口的用量聚合。
|
|
132
|
+
*
|
|
133
|
+
* 窗口值先收进 `[1, 60]`:省下的天数与超出的天数都不是客户端能定的,而「问 1000
|
|
134
|
+
* 天」不该让本桥去翻 1000 个日历。
|
|
135
|
+
* @param days - 要多少天。
|
|
136
|
+
* @returns 读到的那一份,或者为什么读不到。
|
|
137
|
+
*/
|
|
138
|
+
stats(days: number): Promise<UsageStatsAnswer>;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* 把客户端给的天数收成合法的窗口。
|
|
142
|
+
* @param days - 客户端给的天数。
|
|
143
|
+
* @returns 一个 `[1, 60]` 间的整数;不是有限数时回默认的 14 天。
|
|
144
|
+
*/
|
|
145
|
+
export declare function clampStatsDays(days: number): number;
|
|
146
|
+
/**
|
|
147
|
+
* 一个日期键加减天数。
|
|
148
|
+
*
|
|
149
|
+
* 走 UTC 是为了不让夏令时把「加一天」算成 23 或 25 小时:这里加的是日历天。
|
|
150
|
+
* @param day - 日期键 `YYYY-MM-DD`。
|
|
151
|
+
* @param delta - 加减多少天,负数向前。
|
|
152
|
+
* @returns 新的日期键。
|
|
153
|
+
*/
|
|
154
|
+
export declare function shiftDay(day: string, delta: number): string;
|
|
155
|
+
/**
|
|
156
|
+
* `YYYY-MM-DD` 是不是一个真实存在的日子。
|
|
157
|
+
* @param value - 待判定的文本。
|
|
158
|
+
* @returns 格式对且日子存在时为 true。
|
|
159
|
+
*/
|
|
160
|
+
export declare function isDayKey(value: string): boolean;
|