@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
|
@@ -14,8 +14,57 @@
|
|
|
14
14
|
export declare const DEVICE_TOKEN_BYTES = 32;
|
|
15
15
|
/** Entropy of one device id. */
|
|
16
16
|
export declare const DEVICE_ID_BYTES = 12;
|
|
17
|
-
/**
|
|
18
|
-
|
|
17
|
+
/**
|
|
18
|
+
* On-disk schema version of the roster file.
|
|
19
|
+
*
|
|
20
|
+
* 1 carried the six identity fields only; 2 adds the optional `push` block per
|
|
21
|
+
* row (§28). The difference is additive, so a version-1 file reads as-is: no row
|
|
22
|
+
* loses a field, every paired phone stays paired, and the file is rewritten as
|
|
23
|
+
* version 2 the next time anything is written. The reader therefore accepts any
|
|
24
|
+
* version whose row table validates rather than quarantining a file it merely
|
|
25
|
+
* does not recognise — a downgrade must never un-pair every phone.
|
|
26
|
+
*/
|
|
27
|
+
export declare const ROSTER_VERSION = 2;
|
|
28
|
+
/** The APNs environments a device may register for. */
|
|
29
|
+
export type PushEnvironment = 'sandbox' | 'production';
|
|
30
|
+
/** One Live Activity push token, with the session the card belongs to. */
|
|
31
|
+
export interface PushLiveActivityToken {
|
|
32
|
+
readonly activityId: string;
|
|
33
|
+
readonly token: string;
|
|
34
|
+
/**
|
|
35
|
+
* The session this card belongs to (§29.9).
|
|
36
|
+
*
|
|
37
|
+
* A card is about one session, and a token without this field cannot be
|
|
38
|
+
* addressed at all: this bridge has nothing to put on it. Absent is therefore
|
|
39
|
+
* "never pushed to", not "pushed to with whatever is at hand" — the other
|
|
40
|
+
* reading would put one session's state on another session's lock screen.
|
|
41
|
+
*/
|
|
42
|
+
readonly sessionId?: string;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* What one device registered so this bridge can notify it.
|
|
46
|
+
*
|
|
47
|
+
* Stored beside the device's identity in the same row: it is a fact about that
|
|
48
|
+
* device, so it disappears with the row when the device is revoked and it never
|
|
49
|
+
* becomes a second identity. Nothing here is a secret of the bridge's — an APNs
|
|
50
|
+
* token is the phone's, and holding it grants nothing without the signing key,
|
|
51
|
+
* which never touches this file.
|
|
52
|
+
*/
|
|
53
|
+
export interface PushRegistration {
|
|
54
|
+
/** APNs device token, as the hex string the system handed the app. */
|
|
55
|
+
readonly deviceToken: string;
|
|
56
|
+
readonly environment: PushEnvironment;
|
|
57
|
+
/** The app's bundle id, used verbatim as `apns-topic`. */
|
|
58
|
+
readonly bundle: string;
|
|
59
|
+
readonly locale?: string;
|
|
60
|
+
readonly liveActivityTokens?: readonly PushLiveActivityToken[];
|
|
61
|
+
readonly registeredAt: number;
|
|
62
|
+
}
|
|
63
|
+
/** One device a notification can be addressed to. */
|
|
64
|
+
export interface PushTargetView {
|
|
65
|
+
readonly deviceId: string;
|
|
66
|
+
readonly push: PushRegistration;
|
|
67
|
+
}
|
|
19
68
|
/** One paired device as the bridge keeps it. */
|
|
20
69
|
export interface DeviceRecord {
|
|
21
70
|
readonly deviceId: string;
|
|
@@ -25,6 +74,12 @@ export interface DeviceRecord {
|
|
|
25
74
|
readonly fingerprint: string;
|
|
26
75
|
readonly createdAt: number;
|
|
27
76
|
readonly lastSeenAt: number;
|
|
77
|
+
/**
|
|
78
|
+
* The device's APNs registration, absent until it calls `push.register`.
|
|
79
|
+
* A row without it is a paired phone that asks for no notification, which is
|
|
80
|
+
* every phone that predates the channel.
|
|
81
|
+
*/
|
|
82
|
+
readonly push?: PushRegistration;
|
|
28
83
|
}
|
|
29
84
|
/** What one successful enrollment hands back to the caller exactly once. */
|
|
30
85
|
export interface EnrollmentResult {
|
|
@@ -44,6 +99,10 @@ export interface DeviceRosterOptions {
|
|
|
44
99
|
* Revocation is a file edit: protocol v1 has no revoke method, so an operator
|
|
45
100
|
* removes the row (or deletes the file) and restarts. The in-memory copy is
|
|
46
101
|
* loaded once per process.
|
|
102
|
+
*
|
|
103
|
+
* One phone is one row. Pairing again replaces the credential on the row the
|
|
104
|
+
* phone already has instead of adding a second one, and a table that still
|
|
105
|
+
* carries duplicates is compacted when it is read.
|
|
47
106
|
*/
|
|
48
107
|
export declare class DeviceRoster {
|
|
49
108
|
#private;
|
|
@@ -69,4 +128,45 @@ export declare class DeviceRoster {
|
|
|
69
128
|
enroll(pubKey: string): Promise<EnrollmentResult | undefined>;
|
|
70
129
|
/** Record that a device just reconnected; the file is refreshed at most once per call. */
|
|
71
130
|
touch(deviceId: string): Promise<void>;
|
|
131
|
+
/**
|
|
132
|
+
* Record this device's APNs registration, replacing any it had.
|
|
133
|
+
*
|
|
134
|
+
* Registering twice is what a token rotation looks like from here, so this is
|
|
135
|
+
* a plain overwrite rather than a merge: the newest complete registration is
|
|
136
|
+
* the only one worth sending to, and a half-merged pair of rows would send to
|
|
137
|
+
* a token the phone already retired.
|
|
138
|
+
* @param deviceId - the device that just registered.
|
|
139
|
+
* @param registration - what it registered.
|
|
140
|
+
* @returns false when no such device is paired.
|
|
141
|
+
*/
|
|
142
|
+
registerPush(deviceId: string, registration: PushRegistration): Promise<boolean>;
|
|
143
|
+
/**
|
|
144
|
+
* Forget one Live Activity token the phone no longer holds.
|
|
145
|
+
*
|
|
146
|
+
* APNs answers 410 (or 400 `BadDeviceToken`) for the token of a card the app
|
|
147
|
+
* already ended, and such a token never works again: the next card arrives as a
|
|
148
|
+
* new activity with a new token through `push.register`, which is a complete
|
|
149
|
+
* replacement of this set. Dropping it here is therefore the whole recovery —
|
|
150
|
+
* retrying would only repeat the same refusal (§29 token lifecycle).
|
|
151
|
+
* @param activityId - the card the token belonged to.
|
|
152
|
+
* @param token - the token APNs refused.
|
|
153
|
+
* @returns whether any row changed.
|
|
154
|
+
*/
|
|
155
|
+
removeLiveActivityToken(activityId: string, token: string): Promise<boolean>;
|
|
156
|
+
/**
|
|
157
|
+
* Forget one device's notification registration for a token APNs will never
|
|
158
|
+
* accept again.
|
|
159
|
+
*
|
|
160
|
+
* A dead alert token is the whole app install's refusal: the phone was restored,
|
|
161
|
+
* reinstalled, or the token belongs to another environment or bundle, so the
|
|
162
|
+
* Live Activity tokens that same install registered are dead by the same fact
|
|
163
|
+
* and go with it. The next `push.register` — which a live app performs on launch
|
|
164
|
+
* — writes a complete new block. The device itself stays paired: revoking a
|
|
165
|
+
* phone is a roster edit, never something a push refusal may do.
|
|
166
|
+
* @param deviceToken - the token APNs refused.
|
|
167
|
+
* @returns whether any row changed.
|
|
168
|
+
*/
|
|
169
|
+
removePush(deviceToken: string): Promise<boolean>;
|
|
170
|
+
/** @returns every paired device that registered for notifications. */
|
|
171
|
+
pushTargets(): readonly PushTargetView[];
|
|
72
172
|
}
|
|
@@ -118,6 +118,19 @@ export declare class RemoteBridgeRegistry {
|
|
|
118
118
|
* @returns whether any plugin registered it, whatever capability it requires.
|
|
119
119
|
*/
|
|
120
120
|
has(method: string): boolean;
|
|
121
|
+
/**
|
|
122
|
+
* The capabilities the registered tables require a client to declare.
|
|
123
|
+
*
|
|
124
|
+
* A table registered without a capability contributes none: nothing gates its
|
|
125
|
+
* methods, so there is nothing for a client to announce. A name stays listed
|
|
126
|
+
* while any registered method still requires it, so withdrawing one table
|
|
127
|
+
* withdraws its capability only when it was the last one holding it. Sorted,
|
|
128
|
+
* because this answer travels on the wire: one set must read the same way
|
|
129
|
+
* whatever order the plugins happened to mount in.
|
|
130
|
+
* @returns the distinct non-blank capabilities, ascending; empty when no
|
|
131
|
+
* extension is registered.
|
|
132
|
+
*/
|
|
133
|
+
capabilities(): readonly string[];
|
|
121
134
|
/**
|
|
122
135
|
* @param method - the method name a client asked for.
|
|
123
136
|
* @param declares - whether the calling connection declared a capability.
|
package/lib/types/core/hub.d.ts
CHANGED
|
@@ -2,24 +2,42 @@
|
|
|
2
2
|
* The bridge's process-wide connection registry and its claims on
|
|
3
3
|
* `approval/request` and `user-questions/request`.
|
|
4
4
|
*
|
|
5
|
-
* The hub is what makes "phone not answering equals refusal" true:
|
|
6
|
-
* is claimed only while at least one paired device is connected, the publish
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
5
|
+
* The hub is what makes "phone not answering equals refusal" true: an approval
|
|
6
|
+
* is claimed only while at least one paired device is connected, the publish has
|
|
7
|
+
* to actually reach someone, and every other path (timeout, agent cancellation,
|
|
8
|
+
* the last phone leaving) delegates to the next answerer, which the Host
|
|
9
|
+
* normalizes to `'unavailable'`.
|
|
10
10
|
*
|
|
11
|
-
*
|
|
11
|
+
* A user question adds the one case where "reach someone" is the notification
|
|
12
|
+
* rather than the socket: with no paired connection on the wire but a device on
|
|
13
|
+
* the roster that registered for pushes, the question is claimed for that phone
|
|
14
|
+
* and the alert is its delivery (§11.3, §32). One predicate decides it — "can a
|
|
15
|
+
* device that is reachable right now receive a question card" — and it is asked
|
|
16
|
+
* again every time a connection finishes its hello, because a client that never
|
|
17
|
+
* declares `questions.v1` going online turns that answer into no.
|
|
18
|
+
*
|
|
19
|
+
* The claim on `user-questions/request` follows the same rules, with three
|
|
12
20
|
* differences that belong to the host's own semantics: delegating is not a
|
|
13
21
|
* refusal, because only a phone that declares `questions.v1` is ever shown the
|
|
14
|
-
* question;
|
|
22
|
+
* question; a question is named on the wire even when the host named none,
|
|
15
23
|
* because a blocking ask carries no call id and the phone still needs a handle
|
|
16
|
-
* to answer with
|
|
24
|
+
* to answer with; and the claim outlives the last paired device by a grace
|
|
25
|
+
* window, because a phone whose app the system suspended has refused nothing.
|
|
26
|
+
*
|
|
27
|
+
* A presented page (`ui.present`, protocol §35) is the second claim that takes a
|
|
28
|
+
* grace window, and it takes the same one: the tool call it blocks is the agent's
|
|
29
|
+
* own wait, the card is the only place its answer can come from, and a phone that
|
|
30
|
+
* was suspended has refused nothing either. It differs in one direction only —
|
|
31
|
+
* there is no "next answerer" to hand a card back to, so a window that runs out
|
|
32
|
+
* ends the tool call as a refusal (§35.5: the host side reads it as a timeout)
|
|
33
|
+
* rather than starting another answerer.
|
|
17
34
|
* @module dsh-remote-bridge/core/hub
|
|
18
35
|
*/
|
|
19
36
|
import type { ApprovalOutcome } from '@deepseek-ai/dsh-user-approval';
|
|
20
37
|
import type { BridgeConnectionOptions, BridgeTransport, ConnectionAuth } from './connection.ts';
|
|
21
38
|
import { BridgeConnection } from './connection.ts';
|
|
22
39
|
import type { QuestionAnswer, UserQuestionRequestLike } from './questions.ts';
|
|
40
|
+
import type { JsonValue, UiCardView, UiRefusalReason } from './ui.ts';
|
|
23
41
|
/**
|
|
24
42
|
* The fields of an approval question this bridge reads. Structural on purpose:
|
|
25
43
|
* the event's own type is not exported from the package root, and only these
|
|
@@ -46,10 +64,68 @@ export interface ApprovalRequestPayload {
|
|
|
46
64
|
readonly summary: string;
|
|
47
65
|
readonly toolName: string;
|
|
48
66
|
}
|
|
67
|
+
/**
|
|
68
|
+
* How long a user question outlives the last paired device, in milliseconds.
|
|
69
|
+
*
|
|
70
|
+
* A push notification carries a session link and nothing else: the phone can
|
|
71
|
+
* draw a question card only while this bridge still holds the claim, and it has
|
|
72
|
+
* to reconnect before it can read one. A phone whose app was suspended is gone
|
|
73
|
+
* for tens of seconds, so releasing the claim the instant its socket drops
|
|
74
|
+
* decides the question for it — the ask went back to the host's own answerer
|
|
75
|
+
* chain, the Mac showed the card, and the tap that arrived afterwards opened a
|
|
76
|
+
* session with nothing in it (measured 2026-10-23). The window is that reconnect
|
|
77
|
+
* budget, not a second deadline: a claim no phone came back for is released on
|
|
78
|
+
* expiry exactly as it was before.
|
|
79
|
+
*/
|
|
80
|
+
export declare const QUESTION_CLAIM_GRACE_MS = 60000;
|
|
81
|
+
/**
|
|
82
|
+
* How long a user question is held for a phone this bridge reached by
|
|
83
|
+
* notification only, in milliseconds (§32.2).
|
|
84
|
+
*
|
|
85
|
+
* Inside this window the question exists only on the phone's side: the alert is
|
|
86
|
+
* out, no socket has it, and the host's own answerer chain has not been started.
|
|
87
|
+
* When it runs out the claim is withdrawn and delegated, so the agent behind a
|
|
88
|
+
* timed ask waits at most this long past its own deadline and the agent behind a
|
|
89
|
+
* blocking ask (no call id, no deadline) waits at most this long in total — an
|
|
90
|
+
* accepted cost, because the alternative is a question that vanished while the
|
|
91
|
+
* phone was asleep. The window is the bridge's own constant and not the host's:
|
|
92
|
+
* a deployment overrides it, the host never sees it.
|
|
93
|
+
*/
|
|
94
|
+
export declare const OFFLINE_QUESTION_CLAIM_TTL_MS = 600000;
|
|
95
|
+
/** What the hub is built with: the bridge's shared options plus the windows it owns. */
|
|
96
|
+
export interface BridgeHubOptions extends BridgeConnectionOptions {
|
|
97
|
+
/**
|
|
98
|
+
* The offline claim window, in milliseconds.
|
|
99
|
+
*
|
|
100
|
+
* Defaults to {@link OFFLINE_QUESTION_CLAIM_TTL_MS}; a deployment knob, and the
|
|
101
|
+
* seam a suite uses to move the window instead of waiting it out.
|
|
102
|
+
*/
|
|
103
|
+
readonly offlineClaimTtlMs?: number;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* What one `ui.present` call ended as, with the count of connections that took
|
|
107
|
+
* the first frame.
|
|
108
|
+
*
|
|
109
|
+
* The count is here rather than in the card registry because it is a fact about
|
|
110
|
+
* delivery: the registry owns the claim, the hub owns the fan-out, and a client
|
|
111
|
+
* count read back out of the claim would be a second answer to the same question.
|
|
112
|
+
* It rides the tool result so the desktop can say how many screens the page
|
|
113
|
+
* reached (§35.10).
|
|
114
|
+
*/
|
|
115
|
+
export type UiPresentReply = {
|
|
116
|
+
readonly ok: true;
|
|
117
|
+
readonly delivered: number;
|
|
118
|
+
readonly type: string;
|
|
119
|
+
readonly payload?: JsonValue;
|
|
120
|
+
} | {
|
|
121
|
+
readonly ok: false;
|
|
122
|
+
readonly delivered: number;
|
|
123
|
+
readonly reason: UiRefusalReason;
|
|
124
|
+
};
|
|
49
125
|
/** The bridge's live connections plus the answerer it composes onto the approval chain. */
|
|
50
126
|
export declare class BridgeHub {
|
|
51
127
|
#private;
|
|
52
|
-
constructor(options:
|
|
128
|
+
constructor(options: BridgeHubOptions);
|
|
53
129
|
/** @returns how many paired devices could answer a question right now. */
|
|
54
130
|
get pairedClients(): number;
|
|
55
131
|
/**
|
|
@@ -60,7 +136,8 @@ export declare class BridgeHub {
|
|
|
60
136
|
attach(transport: BridgeTransport, auth?: ConnectionAuth): BridgeConnection;
|
|
61
137
|
/**
|
|
62
138
|
* Forget one connection and abort everything it owns. When it was the last
|
|
63
|
-
* paired device, every pending
|
|
139
|
+
* paired device, every pending approval is withdrawn at once and every pending
|
|
140
|
+
* user question is released when its grace window ends.
|
|
64
141
|
*/
|
|
65
142
|
detach(connection: BridgeConnection): void;
|
|
66
143
|
/**
|
|
@@ -93,6 +170,22 @@ export declare class BridgeHub {
|
|
|
93
170
|
* @param connection - the connection that just declared its capabilities.
|
|
94
171
|
*/
|
|
95
172
|
replayApprovals(connection: BridgeConnection): void;
|
|
173
|
+
/**
|
|
174
|
+
* Hand one freshly declared connection the cards this bridge still holds (§35.5).
|
|
175
|
+
*
|
|
176
|
+
* Without this, a presentation raised while the phone was reconnecting stays on
|
|
177
|
+
* the host with no page anywhere on the phone: the first `ui.request` was
|
|
178
|
+
* addressed to the clients that had already announced the channel. The replay
|
|
179
|
+
* emits a frame and nothing else — it claims no card and moves no deadline, so
|
|
180
|
+
* re-handing a page never lengthens how long the agent waits for it.
|
|
181
|
+
*
|
|
182
|
+
* A phone that can take a card is on the wire, so the window that held those
|
|
183
|
+
* cards for nobody is over: it is cancelled here rather than at attach, because
|
|
184
|
+
* only a connection that DECLARED the channel can actually receive one (§11.5's
|
|
185
|
+
* own judgement, which §35.5 reuses).
|
|
186
|
+
* @param connection - the connection that just declared its capabilities.
|
|
187
|
+
*/
|
|
188
|
+
replayUi(connection: BridgeConnection): void;
|
|
96
189
|
/**
|
|
97
190
|
* Publish one event to every paired device that announced a capability.
|
|
98
191
|
* @returns how many connections actually took the frame.
|
|
@@ -108,11 +201,13 @@ export declare class BridgeHub {
|
|
|
108
201
|
/**
|
|
109
202
|
* The body of this plugin's `user-questions/request` listener.
|
|
110
203
|
*
|
|
111
|
-
* Claimed only while a
|
|
112
|
-
*
|
|
113
|
-
*
|
|
114
|
-
*
|
|
115
|
-
*
|
|
204
|
+
* Claimed only while a device that is reachable right now can be shown the
|
|
205
|
+
* card: a paired connection that declared `questions.v1`, or — with no phone on
|
|
206
|
+
* the wire — a roster device that registered for notifications, which the alert
|
|
207
|
+
* reaches instead of a frame (§11.3, §32). The payload has no session field of
|
|
208
|
+
* its own, so an ask that names no agent is delegated either. The wire id is the
|
|
209
|
+
* host's own when the ask is timed, and minted here when it is blocking and the
|
|
210
|
+
* host assigned none — either way the phone answers the id it was shown. A claim
|
|
116
211
|
* nobody received is withdrawn at once instead of holding the agent until the
|
|
117
212
|
* host's deadline. For a timed ask the host's own remaining wait travels with
|
|
118
213
|
* the first frame as `expiresAt`, and every later move of that deadline is
|
|
@@ -122,6 +217,25 @@ export declare class BridgeHub {
|
|
|
122
217
|
* @returns the phone's answer, the host's own deadline outcome, or whatever the next answerer says.
|
|
123
218
|
*/
|
|
124
219
|
question(request: UserQuestionRequestLike, next: () => Promise<QuestionAnswer>): Promise<QuestionAnswer>;
|
|
220
|
+
/**
|
|
221
|
+
* Present one page to the clients that declared `ui.v1` and wait for the user's
|
|
222
|
+
* first action (§35).
|
|
223
|
+
*
|
|
224
|
+
* The body of the `ui.present` tool call. Claimed only while a paired
|
|
225
|
+
* connection that declared the channel is on the wire: with none, a page would
|
|
226
|
+
* reach nobody, so the call answers `unavailable` at once instead of holding the
|
|
227
|
+
* agent on a wait no screen can end (§35.6 — the same judgement as §11.3's claim
|
|
228
|
+
* exclusivity). A frame no connection took is not held either, for the same
|
|
229
|
+
* reason.
|
|
230
|
+
*
|
|
231
|
+
* The card is announced `open` here and announced `settled` from the claim's own
|
|
232
|
+
* notice, so both frames come out of one payload builder and the page a phone was
|
|
233
|
+
* originally handed cannot drift from the one it reads back out of `ui.list`.
|
|
234
|
+
* @param card - the page, under the host's own `ToolCallId`.
|
|
235
|
+
* @param signal - the tool call's cancellation lifetime.
|
|
236
|
+
* @returns the user's action, or the refusal that ended the wait.
|
|
237
|
+
*/
|
|
238
|
+
present(card: UiCardView, signal?: AbortSignal): Promise<UiPresentReply>;
|
|
125
239
|
/** Close every connection and withdraw every question. Idempotent. */
|
|
126
240
|
dispose(): void;
|
|
127
241
|
}
|
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Live Activity half of the push channel (protocol §29).
|
|
3
|
+
*
|
|
4
|
+
* The alert channel in `core/push.ts` tells a phone something happened; a Live
|
|
5
|
+
* Activity is the lock-screen card that keeps being right while the app is
|
|
6
|
+
* suspended, which is why it is a second payload with its own rules rather than
|
|
7
|
+
* a field on the first one:
|
|
8
|
+
*
|
|
9
|
+
* - the request is a `liveactivity` push to a per-card APNs token, under the app
|
|
10
|
+
* bundle's `.push-type.liveactivity` topic, with `apns-expiration: 0` — a card
|
|
11
|
+
* update that arrives late is a lie about the present, so it is never resent;
|
|
12
|
+
* - `content-state` is the App's own `SessionActivityState`, decoded with
|
|
13
|
+
* ActivityKit's default Codable strategy. Timestamps in it are seconds since
|
|
14
|
+
* Apple's reference date (2001-01-01T00:00:00Z), never Unix seconds: a Unix
|
|
15
|
+
* number decodes as a date decades away and the whole card fails to decode;
|
|
16
|
+
* - one card shows one session. A registered card names its session, a card
|
|
17
|
+
* without one is never pushed to, and a state is delivered to the cards whose
|
|
18
|
+
* session it is about — never to "all cards", because that is how one session's
|
|
19
|
+
* question ends up on another session's lock screen (§29.9). `runningCount` is
|
|
20
|
+
* the one bridge-wide number on a card, and it stays the total on purpose;
|
|
21
|
+
* - one card lays out at most three rows (§30). The address is still one session
|
|
22
|
+
* per card — rows say what that card has to tell, not who it is sent to — and
|
|
23
|
+
* every card is given them the same way: the sessions waiting on the user
|
|
24
|
+
* first, in the order their questions arrived, then the sessions running now,
|
|
25
|
+
* most recently updated first. A row's name is the host's own title with its
|
|
26
|
+
* trailing path group taken off, because §29.5 keeps paths off a lock screen;
|
|
27
|
+
* - a card that would say what the card before it already said is not sent, one
|
|
28
|
+
* activity takes at most one update per `LIVE_ACTIVITY_INTERVAL_MS`, and
|
|
29
|
+
* the whole bridge at most `LIVE_ACTIVITY_PER_MINUTE` a minute. What does
|
|
30
|
+
* not fit is dropped, never queued: a card is a snapshot of now, and a backlog
|
|
31
|
+
* of stale snapshots is exactly what this channel must not deliver.
|
|
32
|
+
*
|
|
33
|
+
* Nothing here is awaited by a frame path. Every entry point returns before a
|
|
34
|
+
* delivery starts, and a failure — connection, 401, 410, 5xx, timeout — is one
|
|
35
|
+
* log line and a dropped card, never a delayed or broken follow stream (§28.5).
|
|
36
|
+
* @module dsh-remote-bridge/core/liveactivity
|
|
37
|
+
*/
|
|
38
|
+
import type { PushSendResult } from './push.ts';
|
|
39
|
+
/** The phases a lock-screen card can report. */
|
|
40
|
+
export type SessionActivityPhase = 'running' | 'waitingForAnswer' | 'failed' | 'ended';
|
|
41
|
+
/** The two events one Live Activity push carries. */
|
|
42
|
+
export type LiveActivityEvent = 'update' | 'end';
|
|
43
|
+
/**
|
|
44
|
+
* Seconds from 1970-01-01T00:00:00Z to Apple's reference date, 2001-01-01T00:00:00Z.
|
|
45
|
+
*
|
|
46
|
+
* ActivityKit encodes a `Date` as a double relative to that reference date, so a
|
|
47
|
+
* value meant to decode as "now" is Unix seconds minus this offset.
|
|
48
|
+
*/
|
|
49
|
+
export declare const APPLE_REFERENCE_OFFSET_SECONDS = 978307200;
|
|
50
|
+
/** How long after delivery iOS may still treat a card as current. */
|
|
51
|
+
export declare const LIVE_ACTIVITY_STALE_SECONDS = 90;
|
|
52
|
+
/** Shortest gap between two updates of the same activity. */
|
|
53
|
+
export declare const LIVE_ACTIVITY_INTERVAL_MS = 5000;
|
|
54
|
+
/** Most Live Activity pushes this bridge sends in any rolling minute. */
|
|
55
|
+
export declare const LIVE_ACTIVITY_PER_MINUTE = 30;
|
|
56
|
+
/** Longest option list a card carries; the App shows them as the answer choices. */
|
|
57
|
+
export declare const LIVE_ACTIVITY_OPTION_LIMIT = 2;
|
|
58
|
+
/** Most rows one card lays out; a fourth is dropped, never wrapped or paged (§30.2). */
|
|
59
|
+
export declare const LIVE_ACTIVITY_ROW_LIMIT = 3;
|
|
60
|
+
/**
|
|
61
|
+
* Longest row name a card carries, in characters.
|
|
62
|
+
*
|
|
63
|
+
* A row is one line under a status phrase, so a name longer than this is cut
|
|
64
|
+
* rather than allowed to push the rest of the card off the glass.
|
|
65
|
+
*/
|
|
66
|
+
export declare const LIVE_ACTIVITY_ROW_NAME_MAX = 48;
|
|
67
|
+
/** Activity ids whose last content is remembered before the oldest is forgotten. */
|
|
68
|
+
export declare const LIVE_ACTIVITY_TRACKED = 64;
|
|
69
|
+
/**
|
|
70
|
+
* The one line a card shows per phase, in the App's own words.
|
|
71
|
+
*
|
|
72
|
+
* Fixed English strings, one per phase, and the same table the App renders
|
|
73
|
+
* (§29.7, §30.2): the words on the glass are the client's, so a bridge that sent
|
|
74
|
+
* its own would put a second language on the same card. Fixed for the second
|
|
75
|
+
* reason the alert channel's bodies are fixed too — a line built out of host text
|
|
76
|
+
* is session content on a lock screen. `core/push.ts` keeps its own localized
|
|
77
|
+
* bodies: a notification is read in the user's language, a card is the App's, and
|
|
78
|
+
* both are the same event said for two different surfaces.
|
|
79
|
+
*/
|
|
80
|
+
export declare const ACTIVITY_STATUS_LINE: {
|
|
81
|
+
readonly running: "Running";
|
|
82
|
+
readonly waitingForAnswer: "Waiting for your answer";
|
|
83
|
+
readonly failed: "Stopped";
|
|
84
|
+
readonly ended: "Turn finished";
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* The fixed line one phase shows.
|
|
88
|
+
*
|
|
89
|
+
* The one accessor the single-session field and a row both go through, so a
|
|
90
|
+
* phase cannot be named two ways on one card.
|
|
91
|
+
* @param phase - the phase whose line is wanted.
|
|
92
|
+
* @returns the App's own English phrase for it.
|
|
93
|
+
*/
|
|
94
|
+
export declare function statusLineOf(phase: SessionActivityPhase): string;
|
|
95
|
+
/**
|
|
96
|
+
* One row of the multi-session card (§30.2).
|
|
97
|
+
*
|
|
98
|
+
* Exactly three keys, all non-empty: the host's own name for the session, the
|
|
99
|
+
* same status phrase that session's own card would carry, and its phase. There
|
|
100
|
+
* is no id, no url and no count — a row is a line on a card, not a record.
|
|
101
|
+
*
|
|
102
|
+
* A row only ever carries one of the two live phases: a failed or ended turn is
|
|
103
|
+
* the card's own `phase`, and repeating it as a row would say one thing twice
|
|
104
|
+
* on a card that is already on its way out.
|
|
105
|
+
*/
|
|
106
|
+
export interface ActivityRow {
|
|
107
|
+
readonly name: string;
|
|
108
|
+
readonly status: string;
|
|
109
|
+
readonly phase: SessionActivityPhase;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* One card's state, as the App's own `SessionActivityState` decodes it.
|
|
113
|
+
*
|
|
114
|
+
* The key set is frozen (§29): no field may be added or renamed, because the
|
|
115
|
+
* App's Codable type is the other half of this contract and a key it does not
|
|
116
|
+
* declare fails the whole decode.
|
|
117
|
+
*/
|
|
118
|
+
export interface SessionActivityState {
|
|
119
|
+
readonly phase: SessionActivityPhase;
|
|
120
|
+
readonly statusLine: string;
|
|
121
|
+
readonly questionHeader?: string;
|
|
122
|
+
readonly optionLabels?: readonly string[];
|
|
123
|
+
/** Apple-reference-date seconds of the moment this bridge delivered the card. */
|
|
124
|
+
readonly asOf: number;
|
|
125
|
+
/** Sessions running right now across the whole bridge, never below 1. */
|
|
126
|
+
readonly runningCount: number;
|
|
127
|
+
/**
|
|
128
|
+
* The sessions this card lays out, most urgent first, at most
|
|
129
|
+
* {@link LIVE_ACTIVITY_ROW_LIMIT} of them (§30). Absent and empty mean the
|
|
130
|
+
* same thing to the App: it lays out one row out of the single-session keys.
|
|
131
|
+
*/
|
|
132
|
+
readonly rows?: readonly ActivityRow[];
|
|
133
|
+
}
|
|
134
|
+
/** The request body one Live Activity delivery carries (protocol §29). */
|
|
135
|
+
export interface LiveActivityNotification {
|
|
136
|
+
readonly aps: {
|
|
137
|
+
/** Unix epoch seconds of the delivery. */
|
|
138
|
+
readonly timestamp: number;
|
|
139
|
+
readonly event: LiveActivityEvent;
|
|
140
|
+
readonly 'content-state': SessionActivityState;
|
|
141
|
+
/** Unix epoch seconds after which the card is stale: the delivery plus 90. */
|
|
142
|
+
readonly 'stale-date': number;
|
|
143
|
+
readonly 'relevance-score': number;
|
|
144
|
+
/** Only an `end` for a card whose session left the roster dismisses at once. */
|
|
145
|
+
readonly 'dismissal-date'?: number;
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
/** What the projection of one delivery says, before it is dated. */
|
|
149
|
+
export interface LiveActivityStateInput {
|
|
150
|
+
readonly event: LiveActivityEvent;
|
|
151
|
+
readonly phase: SessionActivityPhase;
|
|
152
|
+
readonly statusLine: string;
|
|
153
|
+
readonly questionHeader?: string;
|
|
154
|
+
readonly optionLabels?: readonly string[];
|
|
155
|
+
readonly runningCount: number;
|
|
156
|
+
/** The rows this card lays out, already chosen, ordered and capped by the notifier. */
|
|
157
|
+
readonly rows?: readonly ActivityRow[];
|
|
158
|
+
/** Delivery moment, in milliseconds since the Unix epoch. */
|
|
159
|
+
readonly now: number;
|
|
160
|
+
/** Dismiss the card the moment it is delivered, instead of letting it expire. */
|
|
161
|
+
readonly dismiss?: boolean;
|
|
162
|
+
}
|
|
163
|
+
/** @returns the score iOS ranks one card of this phase by. */
|
|
164
|
+
export declare function relevanceScoreOf(phase: SessionActivityPhase): number;
|
|
165
|
+
/**
|
|
166
|
+
* The push one card gets.
|
|
167
|
+
*
|
|
168
|
+
* Pure, and exported so the shape can be asserted without a sender: the headers
|
|
169
|
+
* and this key set are the contract, and every value derived from the delivery
|
|
170
|
+
* moment is derived here, once.
|
|
171
|
+
* @param input - the projection and the delivery moment.
|
|
172
|
+
* @returns the exact body an APNs Live Activity delivery carries.
|
|
173
|
+
*/
|
|
174
|
+
export declare function buildLiveActivityNotification(input: LiveActivityStateInput): LiveActivityNotification;
|
|
175
|
+
/** One card this bridge may push to. */
|
|
176
|
+
export interface LiveActivityAddress {
|
|
177
|
+
/** The activity the App created; stable for the life of one card. */
|
|
178
|
+
readonly activityId: string;
|
|
179
|
+
/** The per-card APNs token, which is not the device's token. */
|
|
180
|
+
readonly token: string;
|
|
181
|
+
/**
|
|
182
|
+
* The session this card shows. Absent is a card this bridge never pushes to:
|
|
183
|
+
* it has no state to put on it, and borrowing another session's state is
|
|
184
|
+
* exactly what §29.9 forbids.
|
|
185
|
+
*/
|
|
186
|
+
readonly sessionId?: string;
|
|
187
|
+
readonly environment: 'sandbox' | 'production';
|
|
188
|
+
/** The app bundle id; the topic is this bundle plus the Live Activity suffix. */
|
|
189
|
+
readonly bundle: string;
|
|
190
|
+
}
|
|
191
|
+
/** Whatever actually reaches a card; the APNs client implements this. */
|
|
192
|
+
export interface LiveActivitySender {
|
|
193
|
+
sendLiveActivity(target: LiveActivityAddress, notification: LiveActivityNotification): Promise<PushSendResult>;
|
|
194
|
+
}
|
|
195
|
+
/** One row of a roster this bridge just served, as the projection reads it. */
|
|
196
|
+
export interface ActivityRosterRow {
|
|
197
|
+
readonly id: string;
|
|
198
|
+
readonly agentRunning?: boolean;
|
|
199
|
+
/** The host's own title for that session, the name a row carries (§26, §30.2). */
|
|
200
|
+
readonly title?: string;
|
|
201
|
+
/** When the host last updated the session, in milliseconds; 0 when it names none. */
|
|
202
|
+
readonly updatedAt?: number;
|
|
203
|
+
}
|
|
204
|
+
export interface LiveActivityNotifierOptions {
|
|
205
|
+
/** The cards that are alive right now, read per delivery. */
|
|
206
|
+
readonly targets: () => readonly LiveActivityAddress[];
|
|
207
|
+
/** The sender; absent is a bridge with no signing key, and then nothing happens. */
|
|
208
|
+
readonly sender?: LiveActivitySender;
|
|
209
|
+
readonly now?: () => number;
|
|
210
|
+
readonly log?: (message: string) => void;
|
|
211
|
+
/**
|
|
212
|
+
* Forget a token APNs reported dead. The roster owner removes it from the
|
|
213
|
+
* device row and persists, so a token that can never work again is not retried
|
|
214
|
+
* on every later card (§29 token lifecycle).
|
|
215
|
+
*/
|
|
216
|
+
readonly retire?: (activityId: string, token: string) => Promise<boolean> | boolean | void;
|
|
217
|
+
readonly intervalMs?: number;
|
|
218
|
+
readonly perMinute?: number;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* The bridge's Live Activity path.
|
|
222
|
+
*
|
|
223
|
+
* Deliberately inert when no sender is configured: `enabled` is false, no
|
|
224
|
+
* card is ever read and no clock is consulted, so a bridge without an Apple key
|
|
225
|
+
* behaves — byte for byte, frame for frame — exactly as it did before this
|
|
226
|
+
* channel existed, and `push.register` still answers the same acknowledgement
|
|
227
|
+
* (§28.3: registering is one thing, sending is another).
|
|
228
|
+
*/
|
|
229
|
+
export declare class LiveActivityNotifier {
|
|
230
|
+
#private;
|
|
231
|
+
constructor(options: LiveActivityNotifierOptions);
|
|
232
|
+
/** @returns whether this bridge will actually deliver anything. */
|
|
233
|
+
get enabled(): boolean;
|
|
234
|
+
/**
|
|
235
|
+
* Take the roster this bridge just served as the current picture of what runs.
|
|
236
|
+
*
|
|
237
|
+
* A listing is the only place this bridge learns which sessions are live, so it
|
|
238
|
+
* is where each running session's own card is refreshed: that session's card is
|
|
239
|
+
* told it is running, and the number beside it is how many sessions are running
|
|
240
|
+
* in total. A card waiting on the user keeps that state — listing sessions is
|
|
241
|
+
* not an answer, and downgrading the card would hide the question still owed.
|
|
242
|
+
* @param rows - the roster rows, as this connection built them.
|
|
243
|
+
*/
|
|
244
|
+
noteRoster(rows: readonly ActivityRosterRow[]): void;
|
|
245
|
+
/**
|
|
246
|
+
* Report one turn that ended.
|
|
247
|
+
*
|
|
248
|
+
* Only that session's own card is affected: the session stops running, its
|
|
249
|
+
* pending question is gone with the turn, and its card is ended — not updated —
|
|
250
|
+
* because the turn it was about is over. A session that was listed before and
|
|
251
|
+
* is not in the roster any more has left it, so its card is dismissed on
|
|
252
|
+
* delivery instead of being left on a lock screen to describe a session that no
|
|
253
|
+
* longer exists.
|
|
254
|
+
* @param sessionId - the session whose turn ended.
|
|
255
|
+
* @param reason - the host's own reason, read exactly as the alert channel reads it.
|
|
256
|
+
*/
|
|
257
|
+
noteTurnEnd(sessionId: string, reason: string): void;
|
|
258
|
+
/**
|
|
259
|
+
* Report a question that reached a phone and is now waiting for an answer.
|
|
260
|
+
*
|
|
261
|
+
* Only the header and at most two option labels cross: they are the choices the
|
|
262
|
+
* App can offer. The question itself and its detail stay behind the wire, the
|
|
263
|
+
* same rule the alert channel follows (§28.4), and only this session's card is
|
|
264
|
+
* told — another session's card has no business showing this question (§29.9).
|
|
265
|
+
* @param sessionId - the session that asked.
|
|
266
|
+
* @param question - the first question of the claimed batch.
|
|
267
|
+
*/
|
|
268
|
+
noteQuestion(sessionId: string, question: {
|
|
269
|
+
readonly header?: string;
|
|
270
|
+
readonly optionLabels?: readonly string[];
|
|
271
|
+
}): void;
|
|
272
|
+
/**
|
|
273
|
+
* Report an approval that reached a phone and is blocking the agent.
|
|
274
|
+
*
|
|
275
|
+
* The card says what state that session is in and nothing more: an approval's
|
|
276
|
+
* own summary is a tool's reason text, which is exactly the session content a
|
|
277
|
+
* lock screen must not carry, so it gets the fixed line and no details.
|
|
278
|
+
* @param sessionId - the session whose tool is blocked.
|
|
279
|
+
*/
|
|
280
|
+
noteApproval(sessionId: string): void;
|
|
281
|
+
/**
|
|
282
|
+
* Report that the question one card was waiting on is gone — answered here, on
|
|
283
|
+
* the desktop, or expired.
|
|
284
|
+
*
|
|
285
|
+
* A card left saying "waiting for an answer" after the answer landed is the one
|
|
286
|
+
* lie a lock screen cannot afford, so that session's card goes back to whatever
|
|
287
|
+
* is true now. Nothing is sent when that session is not known to run: there is
|
|
288
|
+
* no fact to show, and the ended card already covers that case.
|
|
289
|
+
* @param sessionId - the session whose claim settled.
|
|
290
|
+
*/
|
|
291
|
+
noteSettled(sessionId: string): void;
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* What identifies a content state for the dedupe.
|
|
295
|
+
*
|
|
296
|
+
* `asOf` is left out on purpose: it is the delivery moment, so it differs on
|
|
297
|
+
* every build, and including it would make "the same content was sent" impossible
|
|
298
|
+
* to observe — a card is a duplicate exactly when everything a user can read on
|
|
299
|
+
* it is unchanged. The rows are part of what a user reads (§30), so a card whose
|
|
300
|
+
* list of sessions reordered, was renamed or lost a row says something new.
|
|
301
|
+
* @param state - the content state of one delivery.
|
|
302
|
+
* @returns a stable key for the readable part of it.
|
|
303
|
+
*/
|
|
304
|
+
export declare function contentKeyOf(state: SessionActivityState): string;
|
|
305
|
+
/**
|
|
306
|
+
* The host's own title, as one row may carry it (§30.2).
|
|
307
|
+
*
|
|
308
|
+
* The same rule the App applies to a card's own name
|
|
309
|
+
* (`SessionActivityDisplayName`), so one card cannot show two versions of one
|
|
310
|
+
* name. A roster title is usually the session's first message, and those end on
|
|
311
|
+
* the absolute path the session ran in — `你是本仓 (/Users/zcl/code/x)` — while
|
|
312
|
+
* §29.5 keeps paths off a lock screen. A trailing group whose body is a path or
|
|
313
|
+
* nothing therefore comes off, brackets included, and whatever it exposed is
|
|
314
|
+
* cleaned up after it (the separator in front of it, the bracket of the group
|
|
315
|
+
* before it). A group that is part of the name, `Ship (main)`, stays. What is
|
|
316
|
+
* left is whitespace-collapsed, trimmed of the separators and dangling brackets
|
|
317
|
+
* its tail may hang on, and cut to {@link LIVE_ACTIVITY_ROW_NAME_MAX}.
|
|
318
|
+
*
|
|
319
|
+
* The rule lives here and nowhere else in this bridge, so there is one answer to
|
|
320
|
+
* "which words of a host title may reach a lock screen".
|
|
321
|
+
* @param title - the title the roster carried for that session, possibly empty.
|
|
322
|
+
* @returns a name a row can carry, and an empty string when the title held nothing else.
|
|
323
|
+
*/
|
|
324
|
+
export declare function normalizeActivityRowName(title: string): string;
|