@linxin666/dsh-remote-bridge 0.4.8 → 0.4.9
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 +50 -17
- package/README.zh.md +50 -17
- package/lib/client.js +274 -253
- package/lib/client.js.map +1 -1
- package/lib/index.js +6690 -2277
- package/lib/types/client/PairEntry.d.ts +2 -2
- package/lib/types/client/PairPanel.d.ts +10 -2
- package/lib/types/client/icons.d.ts +5 -13
- package/lib/types/client/locales.d.ts +10 -14
- package/lib/types/client/semantic.d.ts +9 -5
- package/lib/types/core/commands.d.ts +59 -0
- package/lib/types/core/connection.d.ts +68 -0
- package/lib/types/core/devices.d.ts +98 -2
- package/lib/types/core/hub.d.ts +65 -15
- package/lib/types/core/liveactivity.d.ts +324 -0
- package/lib/types/core/protocol.d.ts +260 -1
- package/lib/types/core/push.d.ts +178 -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/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/usage-stats.d.ts +75 -0
- package/lib/types/host/watchers.d.ts +88 -0
- package/lib/types/index.d.ts +18 -2
- package/package.json +13 -13
- package/src/client/PairEntry.tsx +58 -31
- package/src/client/PairPanel.tsx +181 -97
- package/src/client/icons.tsx +17 -60
- package/src/client/locales.ts +15 -23
- package/src/client/panel.module.css +239 -186
- package/src/client/semantic.ts +10 -6
- package/src/core/commands.ts +95 -0
- package/src/core/connection.ts +646 -10
- package/src/core/devices.ts +221 -3
- package/src/core/hub.ts +362 -42
- package/src/core/liveactivity.ts +833 -0
- package/src/core/protocol.ts +294 -1
- package/src/core/push.ts +354 -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/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/usage-stats.ts +155 -0
- package/src/host/watchers.ts +272 -0
- package/src/index.ts +257 -8
package/src/core/push.ts
ADDED
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What this bridge tells a phone that is not listening.
|
|
3
|
+
*
|
|
4
|
+
* Every fact a phone needs already has a read on the wire — a roster, a
|
|
5
|
+
* baseline, a question list — and a notification is not one of them. It is the
|
|
6
|
+
* `turn ended` or `something is waiting` event, delivered by APNs to a device
|
|
7
|
+
* whose socket is closed, and nothing else: this module deliberately holds no
|
|
8
|
+
* session state, publishes no topic, and answers no method. A client that acts
|
|
9
|
+
* on a notification still has to read the fact behind it (protocol §28).
|
|
10
|
+
*
|
|
11
|
+
* Three rules live here, so the sender beside it cannot disagree about them:
|
|
12
|
+
*
|
|
13
|
+
* - the payload carries a session's NAME, its workspace's short name, the state
|
|
14
|
+
* and one deep link. Never a message, a tool output, a file name or a
|
|
15
|
+
* question's body — a notification crosses Apple's servers and sits on a lock
|
|
16
|
+
* screen, which is the last place session content should be;
|
|
17
|
+
* - the same `(sessionId, event)` inside {@link PUSH_DEDUPE_MS} is one
|
|
18
|
+
* notification, because two phones following one session both see the same
|
|
19
|
+
* turn end and the user has one lock screen;
|
|
20
|
+
* - a failed delivery is logged and dropped. Nothing here is awaited by the
|
|
21
|
+
* delta path, so a broken APNs connection cannot delay, reorder or suppress a
|
|
22
|
+
* frame that a connected phone is waiting for.
|
|
23
|
+
* @module dsh-remote-bridge/core/push
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import { basename } from 'node:path'
|
|
27
|
+
|
|
28
|
+
/** One thing a notification can be about. */
|
|
29
|
+
export type PushEvent = 'turn-end' | 'turn-error' | 'question' | 'approval'
|
|
30
|
+
|
|
31
|
+
/** Window inside which the same `(sessionId, event)` is one notification. */
|
|
32
|
+
export const PUSH_DEDUPE_MS = 30_000
|
|
33
|
+
/** Shortest gap between two notifications about the same session. */
|
|
34
|
+
export const PUSH_SESSION_INTERVAL_MS = 10_000
|
|
35
|
+
/** Most notifications this bridge sends in any rolling minute. */
|
|
36
|
+
export const PUSH_GLOBAL_PER_MINUTE = 30
|
|
37
|
+
/** Sessions whose workspace short name is remembered for the payload. */
|
|
38
|
+
export const PUSH_WORKSPACE_LIMIT = 512
|
|
39
|
+
/** The only deep-link form this channel emits. */
|
|
40
|
+
export const PUSH_DEEP_LINK_PREFIX = 'whale://session/'
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The status line each event carries.
|
|
44
|
+
*
|
|
45
|
+
* Fixed strings on purpose: a body built out of host text would be session
|
|
46
|
+
* content wearing a notification's clothes, and the whole point of this channel
|
|
47
|
+
* is that it carries none.
|
|
48
|
+
*/
|
|
49
|
+
const PUSH_BODY: Readonly<Record<PushEvent, string>> = {
|
|
50
|
+
'turn-end': '回合结束',
|
|
51
|
+
'turn-error': '回合异常结束',
|
|
52
|
+
question: '有待回答的问题',
|
|
53
|
+
approval: '有待确认的审批',
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** One device a notification can be addressed to. */
|
|
57
|
+
export interface PushAddress {
|
|
58
|
+
readonly deviceToken: string
|
|
59
|
+
readonly environment: 'sandbox' | 'production'
|
|
60
|
+
readonly bundle: string
|
|
61
|
+
readonly locale?: string
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** The request body one APNs delivery carries (protocol §28.4). */
|
|
65
|
+
export interface PushNotification {
|
|
66
|
+
readonly aps: {
|
|
67
|
+
readonly alert: { readonly title: string; readonly body: string }
|
|
68
|
+
readonly sound: 'default'
|
|
69
|
+
}
|
|
70
|
+
/** Workspace short name; absent when this bridge cannot resolve one. */
|
|
71
|
+
readonly w?: string
|
|
72
|
+
readonly s: string
|
|
73
|
+
readonly st: PushEvent
|
|
74
|
+
readonly url: string
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** What one delivery attempt reported. */
|
|
78
|
+
export interface PushSendResult {
|
|
79
|
+
readonly ok: boolean
|
|
80
|
+
/** The status APNs answered, when it answered at all. */
|
|
81
|
+
readonly status?: number
|
|
82
|
+
/** The reason APNs named, or this bridge's own note. */
|
|
83
|
+
readonly reason?: string
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** One delivery a sender refused, in the words the bridge needs to read it. */
|
|
87
|
+
export interface PushDeliveryFailure {
|
|
88
|
+
/** The registration APNs refused: the roster row holding it is a device that no longer exists. */
|
|
89
|
+
readonly deviceToken: string
|
|
90
|
+
/** The status APNs answered, when it answered at all. */
|
|
91
|
+
readonly status?: number
|
|
92
|
+
/** The reason APNs named, or this bridge's own note. */
|
|
93
|
+
readonly reason?: string
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** What one {@link PushNotifier.notify} ended up doing, once every attempt settled. */
|
|
97
|
+
export interface PushDeliveryReport {
|
|
98
|
+
/** How many devices took the notification. */
|
|
99
|
+
readonly delivered: number
|
|
100
|
+
/** Every attempt that did not land, in target order. */
|
|
101
|
+
readonly failures: readonly PushDeliveryFailure[]
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* @param failure - one refused delivery.
|
|
106
|
+
* @returns whether the registration behind it is dead for good.
|
|
107
|
+
*
|
|
108
|
+
* APNs says a token will never work again in three ways: 410 with
|
|
109
|
+
* `Unregistered`, and 400 with `Unregistered` or `BadDeviceToken`. All three are
|
|
110
|
+
* final facts about the registration — the app was removed, or this token belongs
|
|
111
|
+
* to another environment or bundle — so it is cleaned up instead of retried, and
|
|
112
|
+
* a claim held for it is a claim held for a phone that cannot come back.
|
|
113
|
+
*/
|
|
114
|
+
export function isDeadPushToken(failure: PushDeliveryFailure): boolean {
|
|
115
|
+
const reason = failure.reason ?? ''
|
|
116
|
+
if (reason === 'Unregistered' || reason === 'BadDeviceToken') return true
|
|
117
|
+
return failure.status === 410
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Whatever actually reaches a device; the APNs client implements this. */
|
|
121
|
+
export interface PushSender {
|
|
122
|
+
send(target: PushAddress, notification: PushNotification): Promise<PushSendResult>
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** What the payload builder may say about one session. */
|
|
126
|
+
export interface PushLabels {
|
|
127
|
+
readonly title?: string
|
|
128
|
+
readonly workspace?: string
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export interface PushNotifierOptions {
|
|
132
|
+
/** The devices that asked to be notified, read per event. */
|
|
133
|
+
readonly targets: () => readonly PushAddress[]
|
|
134
|
+
/** The sender; absent is a bridge with no signing key, and then nothing happens. */
|
|
135
|
+
readonly sender?: PushSender
|
|
136
|
+
/** The session's own title (§26's source), read per event. */
|
|
137
|
+
readonly titles?: (sessionId: string) => string | undefined
|
|
138
|
+
readonly now?: () => number
|
|
139
|
+
readonly log?: (message: string) => void
|
|
140
|
+
readonly dedupeMs?: number
|
|
141
|
+
readonly sessionIntervalMs?: number
|
|
142
|
+
readonly globalPerMinute?: number
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* The notification the channel sends for one event (protocol §28.4).
|
|
147
|
+
*
|
|
148
|
+
* Pure, and exported so the shape can be asserted without a sender: the keys
|
|
149
|
+
* are the contract, and the absence of any body text is the property that
|
|
150
|
+
* matters most.
|
|
151
|
+
* @param event - what happened.
|
|
152
|
+
* @param sessionId - the session it happened to.
|
|
153
|
+
* @param labels - the name and workspace short name, when the bridge holds them.
|
|
154
|
+
* @returns the exact payload an APNs delivery carries.
|
|
155
|
+
*/
|
|
156
|
+
export function buildPushNotification(event: PushEvent, sessionId: string, labels: PushLabels = {}): PushNotification {
|
|
157
|
+
const named = labels.title !== undefined && labels.title !== ''
|
|
158
|
+
return {
|
|
159
|
+
aps: {
|
|
160
|
+
alert: { title: named ? labels.title as string : PUSH_BODY[event], body: PUSH_BODY[event] },
|
|
161
|
+
sound: 'default',
|
|
162
|
+
},
|
|
163
|
+
...(labels.workspace === undefined || labels.workspace === '' ? {} : { w: labels.workspace }),
|
|
164
|
+
s: sessionId,
|
|
165
|
+
st: event,
|
|
166
|
+
url: `${PUSH_DEEP_LINK_PREFIX}${sessionId}`,
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Which event one `turn-end` delta is.
|
|
172
|
+
* @param reason - the host's own reason for ending the turn.
|
|
173
|
+
* @returns the normal end for `completed`, the abnormal one for every other
|
|
174
|
+
* value — including `unknown`, which is what an unreadable reason reports.
|
|
175
|
+
*/
|
|
176
|
+
export function pushEventOfTurnEnd(reason: string): PushEvent {
|
|
177
|
+
return reason === 'completed' ? 'turn-end' : 'turn-error'
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* The bridge's outbound notification path.
|
|
182
|
+
*
|
|
183
|
+
* Deliberately inert when no sender is configured: {@link enabled} is false, no
|
|
184
|
+
* target is ever read and no clock is ever consulted, so an unconfigured bridge
|
|
185
|
+
* behaves — byte for byte, frame for frame — exactly as it did before this
|
|
186
|
+
* channel existed.
|
|
187
|
+
*/
|
|
188
|
+
export class PushNotifier {
|
|
189
|
+
readonly #options: PushNotifierOptions
|
|
190
|
+
readonly #now: () => number
|
|
191
|
+
readonly #log: (message: string) => void
|
|
192
|
+
/** Last send per `(sessionId, event)`. */
|
|
193
|
+
readonly #dedupe = new Map<string, number>()
|
|
194
|
+
/** Last send per session, whatever the event. */
|
|
195
|
+
readonly #sessions = new Map<string, number>()
|
|
196
|
+
/** Send timestamps inside the rolling minute. */
|
|
197
|
+
#recent: number[] = []
|
|
198
|
+
/** Session id to workspace short name, as the roster rows this bridge served reported it. */
|
|
199
|
+
readonly #workspaces = new Map<string, string>()
|
|
200
|
+
|
|
201
|
+
constructor(options: PushNotifierOptions) {
|
|
202
|
+
this.#options = options
|
|
203
|
+
this.#now = options.now ?? (() => Date.now())
|
|
204
|
+
this.#log = options.log ?? (() => undefined)
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** @returns whether this bridge will actually deliver anything. */
|
|
208
|
+
get enabled(): boolean {
|
|
209
|
+
return this.#options.sender !== undefined
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Remember the workspace each session belongs to, from a roster this bridge
|
|
214
|
+
* just served.
|
|
215
|
+
*
|
|
216
|
+
* A notification's `w` is the only label the bridge cannot read from the
|
|
217
|
+
* title service, and asking the host for a roster on every turn end would put
|
|
218
|
+
* a gateway call on the notification path. The rows are already in hand when a
|
|
219
|
+
* phone lists them, so the short name is picked up there and kept bounded; a
|
|
220
|
+
* session never listed simply gets a payload without `w`.
|
|
221
|
+
* @param rows - the roster rows that were just built for a client.
|
|
222
|
+
*/
|
|
223
|
+
noteSessions(rows: readonly { readonly id: string; readonly cwd?: string }[]): void {
|
|
224
|
+
if (!this.enabled) return
|
|
225
|
+
for (const row of rows) {
|
|
226
|
+
if (row.cwd === undefined || row.cwd === '') continue
|
|
227
|
+
const short = basename(row.cwd)
|
|
228
|
+
if (short === '') continue
|
|
229
|
+
this.#workspaces.delete(row.id)
|
|
230
|
+
this.#workspaces.set(row.id, short)
|
|
231
|
+
}
|
|
232
|
+
while (this.#workspaces.size > PUSH_WORKSPACE_LIMIT) {
|
|
233
|
+
const oldest = this.#workspaces.keys().next()
|
|
234
|
+
if (oldest.done === true) break
|
|
235
|
+
this.#workspaces.delete(oldest.value)
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Announce one event to every registered device.
|
|
241
|
+
*
|
|
242
|
+
* Returns before anything is sent: delivery starts on a fresh microtask, so a
|
|
243
|
+
* caller on the delta path never awaits an APNs round trip and a sender that
|
|
244
|
+
* throws cannot reach it.
|
|
245
|
+
* @param event - what happened.
|
|
246
|
+
* @param sessionId - the session it happened to.
|
|
247
|
+
* @param onReport - receives how the announcement ended, once every attempt has
|
|
248
|
+
* settled. A caller that held something for a phone — an offline claim (§32) —
|
|
249
|
+
* needs to know whether any phone was actually told, and may not block the
|
|
250
|
+
* caller's own path on an APNs round trip to find out. Not called when nothing
|
|
251
|
+
* was attempted: no sender, no target, or a rate limit that dropped this event.
|
|
252
|
+
*/
|
|
253
|
+
notify(event: PushEvent, sessionId: string, onReport?: (report: PushDeliveryReport) => void): void {
|
|
254
|
+
if (this.#options.sender === undefined) return
|
|
255
|
+
try {
|
|
256
|
+
this.#announce(event, sessionId, onReport)
|
|
257
|
+
} catch (error) {
|
|
258
|
+
// The caller is a frame path — a delta broadcast or a claim — and a
|
|
259
|
+
// notification that cannot even be built must not end a follow stream.
|
|
260
|
+
this.#log(`push ${event} for ${sessionId} could not be prepared: ${String(error)}`)
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/** Build and start one event's deliveries. */
|
|
265
|
+
#announce(event: PushEvent, sessionId: string, onReport?: (report: PushDeliveryReport) => void): void {
|
|
266
|
+
const sender = this.#options.sender
|
|
267
|
+
if (sender === undefined) return
|
|
268
|
+
const targets = this.#options.targets()
|
|
269
|
+
if (targets.length === 0) return
|
|
270
|
+
const now = this.#now()
|
|
271
|
+
if (!this.#admit(event, sessionId, now)) return
|
|
272
|
+
const notification = buildPushNotification(event, sessionId, this.#labelsOf(sessionId))
|
|
273
|
+
const failures: PushDeliveryFailure[] = []
|
|
274
|
+
let delivered = 0
|
|
275
|
+
let pending = targets.length
|
|
276
|
+
// One report per announcement, after the last attempt settled: a half-read
|
|
277
|
+
// count is worse than none, because the caller acts on it.
|
|
278
|
+
const settleOne = (failure: PushDeliveryFailure | undefined): void => {
|
|
279
|
+
if (failure === undefined) delivered += 1
|
|
280
|
+
else failures.push(failure)
|
|
281
|
+
pending -= 1
|
|
282
|
+
if (pending > 0 || onReport === undefined) return
|
|
283
|
+
try {
|
|
284
|
+
onReport({ delivered, failures })
|
|
285
|
+
} catch (error) {
|
|
286
|
+
this.#log(`push ${event} for ${sessionId} could not be reported: ${String(error)}`)
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
for (const target of targets) {
|
|
290
|
+
void Promise.resolve()
|
|
291
|
+
.then(async () => await sender.send(target, notification))
|
|
292
|
+
.then((result) => {
|
|
293
|
+
if (result.ok) {
|
|
294
|
+
settleOne(undefined)
|
|
295
|
+
return
|
|
296
|
+
}
|
|
297
|
+
this.#log(`push ${event} for ${sessionId} was not delivered (${result.status ?? '-'} ${result.reason ?? 'no reason'})`)
|
|
298
|
+
settleOne({
|
|
299
|
+
deviceToken: target.deviceToken,
|
|
300
|
+
...(result.status === undefined ? {} : { status: result.status }),
|
|
301
|
+
...(result.reason === undefined ? {} : { reason: result.reason }),
|
|
302
|
+
})
|
|
303
|
+
})
|
|
304
|
+
.catch((error: unknown) => {
|
|
305
|
+
this.#log(`push ${event} for ${sessionId} failed: ${String(error)}`)
|
|
306
|
+
settleOne({ deviceToken: target.deviceToken })
|
|
307
|
+
})
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** @returns whether this event may be sent now; records the send when it may. */
|
|
312
|
+
#admit(event: PushEvent, sessionId: string, now: number): boolean {
|
|
313
|
+
const dedupeMs = this.#options.dedupeMs ?? PUSH_DEDUPE_MS
|
|
314
|
+
const sessionMs = this.#options.sessionIntervalMs ?? PUSH_SESSION_INTERVAL_MS
|
|
315
|
+
const perMinute = this.#options.globalPerMinute ?? PUSH_GLOBAL_PER_MINUTE
|
|
316
|
+
this.#prune(now, dedupeMs)
|
|
317
|
+
const key = `${sessionId}|${event}`
|
|
318
|
+
const lastSame = this.#dedupe.get(key)
|
|
319
|
+
if (lastSame !== undefined && now - lastSame < dedupeMs) return false
|
|
320
|
+
const lastSession = this.#sessions.get(sessionId)
|
|
321
|
+
if (lastSession !== undefined && now - lastSession < sessionMs) return false
|
|
322
|
+
this.#recent = this.#recent.filter(at => now - at < 60_000)
|
|
323
|
+
if (this.#recent.length >= perMinute) return false
|
|
324
|
+
this.#dedupe.set(key, now)
|
|
325
|
+
this.#sessions.set(sessionId, now)
|
|
326
|
+
this.#recent.push(now)
|
|
327
|
+
return true
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/** Forget entries that can no longer suppress anything. */
|
|
331
|
+
#prune(now: number, dedupeMs: number): void {
|
|
332
|
+
if (this.#dedupe.size > 256) {
|
|
333
|
+
for (const [key, at] of this.#dedupe) if (now - at >= dedupeMs) this.#dedupe.delete(key)
|
|
334
|
+
}
|
|
335
|
+
if (this.#sessions.size > 256) {
|
|
336
|
+
const sessionMs = this.#options.sessionIntervalMs ?? PUSH_SESSION_INTERVAL_MS
|
|
337
|
+
for (const [id, at] of this.#sessions) if (now - at >= sessionMs) this.#sessions.delete(id)
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
#labelsOf(sessionId: string): PushLabels {
|
|
342
|
+
let title: string | undefined
|
|
343
|
+
try {
|
|
344
|
+
title = this.#options.titles?.(sessionId)
|
|
345
|
+
} catch {
|
|
346
|
+
title = undefined
|
|
347
|
+
}
|
|
348
|
+
const workspace = this.#workspaces.get(sessionId)
|
|
349
|
+
return {
|
|
350
|
+
...(title === undefined || title === '' ? {} : { title }),
|
|
351
|
+
...(workspace === undefined ? {} : { workspace }),
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
package/src/core/questions.ts
CHANGED
|
@@ -76,12 +76,73 @@ export interface QuestionAnswer {
|
|
|
76
76
|
readonly answers: readonly QuestionAnswerItem[]
|
|
77
77
|
}
|
|
78
78
|
|
|
79
|
+
/**
|
|
80
|
+
* The `question.request` payload for one claim, and the `payload` of every
|
|
81
|
+
* `question.list` entry.
|
|
82
|
+
*
|
|
83
|
+
* One function for both so the card a phone was handed and the card it reads back
|
|
84
|
+
* out of a list cannot drift apart: `expiresAt` appears only once the claim
|
|
85
|
+
* learned the host's own deadline, and a blocking ask publishes no such key at
|
|
86
|
+
* all rather than an empty or null one.
|
|
87
|
+
* @param state - whether the foreground ask still holds the question.
|
|
88
|
+
* @param question - the claim being published.
|
|
89
|
+
* @returns the frozen payload body.
|
|
90
|
+
*/
|
|
91
|
+
export function questionPayload(state: QuestionState, question: ClaimedQuestionView): QuestionRequestPayload {
|
|
92
|
+
return {
|
|
93
|
+
sessionId: question.sessionId,
|
|
94
|
+
callId: question.callId,
|
|
95
|
+
state,
|
|
96
|
+
questions: question.questions,
|
|
97
|
+
...(question.expiresAt === undefined ? {} : { expiresAt: question.expiresAt }),
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* The identity a client knows one question card by.
|
|
103
|
+
*
|
|
104
|
+
* The same `sessionId/callId` pair the phone already derives for its own card,
|
|
105
|
+
* so a list entry and the card it describes are compared as one name instead of
|
|
106
|
+
* two fields that could be recombined differently on each side.
|
|
107
|
+
* @param sessionId - the session that asked.
|
|
108
|
+
* @param callId - the wire call id the client answers with.
|
|
109
|
+
* @returns the card key.
|
|
110
|
+
*/
|
|
111
|
+
export function questionKey(sessionId: string, callId: string): string {
|
|
112
|
+
return sessionId + '/' + callId
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** One question this bridge still holds, as `question.list` publishes it. */
|
|
116
|
+
export interface ListedQuestion {
|
|
117
|
+
/** {@link questionKey}: the card identity the client holds it under. */
|
|
118
|
+
readonly key: string
|
|
119
|
+
readonly sessionId: string
|
|
120
|
+
/** `open` or `continued`; a settled question is not held and never listed. */
|
|
121
|
+
readonly state: QuestionState
|
|
122
|
+
/** The `question.request` payload, byte for byte the shape that channel froze. */
|
|
123
|
+
readonly payload: QuestionRequestPayload
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* The body of one `question.list` answer.
|
|
128
|
+
*
|
|
129
|
+
* `complete` is what a client is allowed to act on: only a complete list may
|
|
130
|
+
* retire a card it does not carry, so a bridge that could answer only part of the
|
|
131
|
+
* set must say `false` rather than let the difference read as "the question is
|
|
132
|
+
* over".
|
|
133
|
+
*/
|
|
134
|
+
export interface QuestionListPayload {
|
|
135
|
+
readonly questions: readonly ListedQuestion[]
|
|
136
|
+
readonly complete: boolean
|
|
137
|
+
}
|
|
138
|
+
|
|
79
139
|
/** Whether a question still holds the foreground ask, or only its late reply. */
|
|
80
140
|
/**
|
|
81
141
|
* `open` while the ask is still waiting, `continued` once the host's foreground
|
|
82
142
|
* wait ended but a late answer can still be steered in, and `settled` as the
|
|
83
|
-
* one-way notice that the phone's card is over — the host's own UI answered it or
|
|
84
|
-
* the
|
|
143
|
+
* one-way notice that the phone's card is over — the host's own UI answered it, or
|
|
144
|
+
* none was left to answer it once the claim's grace window ended. `settled` is
|
|
145
|
+
* published, never held.
|
|
85
146
|
*/
|
|
86
147
|
export type QuestionState = 'open' | 'continued' | 'settled'
|
|
87
148
|
|
|
@@ -522,14 +583,23 @@ export class QuestionClaims {
|
|
|
522
583
|
}
|
|
523
584
|
|
|
524
585
|
/**
|
|
525
|
-
* Every
|
|
586
|
+
* Every question this bridge still holds, as `question.list` publishes it.
|
|
526
587
|
*
|
|
527
|
-
*
|
|
528
|
-
*
|
|
529
|
-
*
|
|
588
|
+
* The claim registry IS the answer: a question leaves it the moment it is
|
|
589
|
+
* answered, settled by the host's own UI, withdrawn with the last paired phone
|
|
590
|
+
* or expired, so a card missing from this list is a card no one can answer any
|
|
591
|
+
* more. Both live states are listed — `continued` is still answerable through
|
|
592
|
+
* the host's late-answer path — and a settled question is never here, because
|
|
593
|
+
* it is never held.
|
|
594
|
+
* @returns one entry per held claim, in claim order.
|
|
530
595
|
*/
|
|
531
|
-
|
|
532
|
-
return [...this.#claims.values()].map(
|
|
596
|
+
listed(): readonly ListedQuestion[] {
|
|
597
|
+
return [...this.#claims.values()].map(record => ({
|
|
598
|
+
key: questionKey(record.sessionId, record.callId),
|
|
599
|
+
sessionId: record.sessionId,
|
|
600
|
+
state: record.state,
|
|
601
|
+
payload: questionPayload(record.state, viewOf(record)),
|
|
602
|
+
}))
|
|
533
603
|
}
|
|
534
604
|
|
|
535
605
|
/**
|
|
@@ -554,8 +624,9 @@ export class QuestionClaims {
|
|
|
554
624
|
|
|
555
625
|
/**
|
|
556
626
|
* Withdraw every claim, delegating each open question to the next answerer.
|
|
557
|
-
* Called when the last paired phone
|
|
558
|
-
*
|
|
627
|
+
* Called when the last paired phone has been gone long enough that no grace
|
|
628
|
+
* window is still running, and when the bridge unloads: a question nobody can
|
|
629
|
+
* answer must not hold the agent until its own deadline.
|
|
559
630
|
* @returns how many questions were still open.
|
|
560
631
|
*/
|
|
561
632
|
withdrawAll(): number {
|
package/src/core/session-meta.ts
CHANGED
|
@@ -37,18 +37,59 @@ export interface ArchiveSetFace {
|
|
|
37
37
|
readonly archivedSessionIds: readonly string[]
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
+
/**
|
|
41
|
+
* One workspace registration, as far as `workspace.list` publishes it.
|
|
42
|
+
*
|
|
43
|
+
* The host's entity carries more — its `sessionIds` account above all — and the
|
|
44
|
+
* face deliberately does not declare it: a row this bridge publishes must not be
|
|
45
|
+
* able to carry an array that grows with the session count.
|
|
46
|
+
*/
|
|
47
|
+
export interface WorkspaceFace {
|
|
48
|
+
readonly id: string
|
|
49
|
+
readonly path: string
|
|
50
|
+
readonly title: string
|
|
51
|
+
readonly createdAt?: string
|
|
52
|
+
readonly updatedAt?: string
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The slice of `ctx.workspaceRegistry` `workspace.list` reads: the registry
|
|
57
|
+
* itself.
|
|
58
|
+
*
|
|
59
|
+
* Wider than {@link ArchiveSetFace} by the ordered projection and the pin set,
|
|
60
|
+
* because one method publishes all three: a client pins a session and reads
|
|
61
|
+
* both sets back in one frame. Every member is synchronous — `list()` answers a
|
|
62
|
+
* fresh array and both sets are getters over the durable registry — which is
|
|
63
|
+
* what lets the answer be built without awaiting anything.
|
|
64
|
+
*/
|
|
65
|
+
export interface WorkspacesFace extends ArchiveSetFace {
|
|
66
|
+
list(): readonly WorkspaceFace[]
|
|
67
|
+
readonly pinnedSessionIds: readonly string[]
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** The body `workspace.list` answers. */
|
|
71
|
+
export interface WorkspaceListAnswer {
|
|
72
|
+
readonly workspaces: readonly WorkspaceFace[]
|
|
73
|
+
readonly archivedSessionIds: readonly string[]
|
|
74
|
+
readonly pinnedSessionIds: readonly string[]
|
|
75
|
+
}
|
|
76
|
+
|
|
40
77
|
/**
|
|
41
78
|
* The in-process services one metadata read may use.
|
|
42
79
|
*
|
|
43
80
|
* Every field is optional on purpose. The bridge does not require the title or
|
|
44
81
|
* workspace plugins to be installed — the protocol's roster and title methods
|
|
45
82
|
* stay answerable with them absent, and a field that is missing degrades one
|
|
46
|
-
* key of one row instead of failing a method.
|
|
83
|
+
* key of one row instead of failing a method. `workspaces` is the member that
|
|
84
|
+
* breaks that rule on purpose: `workspace.list` exists only to publish the
|
|
85
|
+
* registry, so its absence is reported as a host without one rather than
|
|
86
|
+
* answered with an empty list.
|
|
47
87
|
*/
|
|
48
88
|
export interface SessionMetaFaces {
|
|
49
89
|
readonly sessions?: SessionStoreFace | undefined
|
|
50
90
|
readonly titles?: SessionTitleFace | undefined
|
|
51
91
|
readonly archive?: ArchiveSetFace | undefined
|
|
92
|
+
readonly workspaces?: WorkspacesFace | undefined
|
|
52
93
|
}
|
|
53
94
|
|
|
54
95
|
/** Largest id batch one `session.titles` call accepts. */
|
|
@@ -150,6 +191,37 @@ export class SessionMeta {
|
|
|
150
191
|
}
|
|
151
192
|
}
|
|
152
193
|
|
|
194
|
+
/**
|
|
195
|
+
* The host's workspaces and its two registry-global session sets.
|
|
196
|
+
*
|
|
197
|
+
* One synchronous projection and nothing more: `list()` already answers a
|
|
198
|
+
* fresh array in registry order, and both sets are getters over the durable
|
|
199
|
+
* registry, so this read awaits nothing, opens no follow stream and touches no
|
|
200
|
+
* persistence. Workspaces are neither sorted nor filtered — the registry's own
|
|
201
|
+
* order is the display order — and a row carries exactly the fields protocol
|
|
202
|
+
* v1 names, `sessionIds` never among them.
|
|
203
|
+
*
|
|
204
|
+
* An unmounted registry answers undefined instead of an empty list: "this host
|
|
205
|
+
* has no workspaces" and "this host has no registry to ask" are two different
|
|
206
|
+
* facts, and only the caller can say which one it must report.
|
|
207
|
+
* @returns the answer, or undefined when this host composes no workspace registry.
|
|
208
|
+
*/
|
|
209
|
+
workspaceList(): WorkspaceListAnswer | undefined {
|
|
210
|
+
const registry = this.#faces().workspaces
|
|
211
|
+
if (registry === undefined) return undefined
|
|
212
|
+
return {
|
|
213
|
+
workspaces: registry.list().map(workspace => ({
|
|
214
|
+
id: workspace.id,
|
|
215
|
+
path: workspace.path,
|
|
216
|
+
title: workspace.title,
|
|
217
|
+
...(workspace.createdAt === undefined ? {} : { createdAt: workspace.createdAt }),
|
|
218
|
+
...(workspace.updatedAt === undefined ? {} : { updatedAt: workspace.updatedAt }),
|
|
219
|
+
})),
|
|
220
|
+
archivedSessionIds: [...registry.archivedSessionIds],
|
|
221
|
+
pinnedSessionIds: [...registry.pinnedSessionIds],
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
153
225
|
/** Resolve one title through the host services, degrading to `""` on any failure. */
|
|
154
226
|
#resolve(id: string): string {
|
|
155
227
|
try {
|