@linxin666/dsh-remote-bridge 0.4.7 → 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 +93 -26
- package/README.zh.md +91 -26
- package/icon.svg +3 -0
- package/lib/client.js +274 -253
- package/lib/client.js.map +1 -1
- package/lib/index.js +8891 -2433
- 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/approvals.d.ts +24 -0
- package/lib/types/core/commands.d.ts +59 -0
- package/lib/types/core/connection.d.ts +124 -5
- package/lib/types/core/delta.d.ts +101 -0
- package/lib/types/core/devices.d.ts +98 -2
- package/lib/types/core/dsh-frames.d.ts +155 -4
- package/lib/types/core/envelope.d.ts +34 -0
- package/lib/types/core/extension.d.ts +144 -0
- package/lib/types/core/hub.d.ts +96 -20
- package/lib/types/core/jobs.d.ts +204 -0
- package/lib/types/core/liveactivity.d.ts +324 -0
- package/lib/types/core/protocol.d.ts +491 -2
- package/lib/types/core/push.d.ts +178 -0
- package/lib/types/core/questions.d.ts +109 -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/core/wire.d.ts +1 -1
- package/lib/types/core/ws-frames.d.ts +11 -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/host/websocket.d.ts +8 -1
- package/lib/types/index.d.ts +34 -3
- package/lib/types/mount-once.d.ts +55 -0
- package/locale/en.json +6 -0
- package/locale/ru.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +20 -14
- 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/approvals.ts +32 -1
- package/src/core/commands.ts +95 -0
- package/src/core/connection.ts +1483 -31
- package/src/core/delta.ts +204 -0
- package/src/core/devices.ts +221 -3
- package/src/core/dsh-frames.ts +365 -8
- package/src/core/envelope.ts +46 -0
- package/src/core/extension.ts +197 -0
- package/src/core/hub.ts +441 -49
- package/src/core/jobs.ts +372 -0
- package/src/core/liveactivity.ts +833 -0
- package/src/core/protocol.ts +556 -2
- package/src/core/push.ts +354 -0
- package/src/core/questions.ts +264 -17
- 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 +13 -1
- package/src/core/ws-frames.ts +14 -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/host/websocket.ts +37 -3
- package/src/index.ts +310 -9
- package/src/mount-once.ts +142 -0
package/src/core/questions.ts
CHANGED
|
@@ -30,6 +30,13 @@
|
|
|
30
30
|
* delegated to `next()`, never answered with a decision the operator did not
|
|
31
31
|
* make.
|
|
32
32
|
*
|
|
33
|
+
* A timed ask's deadline is read back from the host itself
|
|
34
|
+
* (`userQuestions/attachWait`) and published as `expiresAt`, so the phone can
|
|
35
|
+
* count down the wait the host is actually serving. Holding that subscription
|
|
36
|
+
* parks the host's own deadline timer, so this module also has to let go at the
|
|
37
|
+
* instant the deadline it learned has passed: only then does the host raise
|
|
38
|
+
* `ASK_TIMED_OUT` and hand the claim its `continued` state.
|
|
39
|
+
*
|
|
33
40
|
* The types below mirror `@deepseek-ai/dsh-user-questions` field for field. The
|
|
34
41
|
* package is not a dependency of this one (it is the host's, not the bridge's),
|
|
35
42
|
* so the contract is declared locally instead of imported.
|
|
@@ -69,12 +76,73 @@ export interface QuestionAnswer {
|
|
|
69
76
|
readonly answers: readonly QuestionAnswerItem[]
|
|
70
77
|
}
|
|
71
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
|
+
|
|
72
139
|
/** Whether a question still holds the foreground ask, or only its late reply. */
|
|
73
140
|
/**
|
|
74
141
|
* `open` while the ask is still waiting, `continued` once the host's foreground
|
|
75
142
|
* wait ended but a late answer can still be steered in, and `settled` as the
|
|
76
|
-
* one-way notice that the phone's card is over — the host's own UI answered it or
|
|
77
|
-
* 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.
|
|
78
146
|
*/
|
|
79
147
|
export type QuestionState = 'open' | 'continued' | 'settled'
|
|
80
148
|
|
|
@@ -110,12 +178,30 @@ export interface ClaimedQuestionView {
|
|
|
110
178
|
readonly sessionId: string
|
|
111
179
|
readonly callId: string
|
|
112
180
|
readonly questions: readonly QuestionItem[]
|
|
181
|
+
/**
|
|
182
|
+
* Epoch milliseconds at which the host's own deadline falls — the absolute
|
|
183
|
+
* form of the remaining wait the host reported through `attachWait`.
|
|
184
|
+
*
|
|
185
|
+
* Absent when the host said nothing: a blocking ask has no deadline at all,
|
|
186
|
+
* and a timed one is only known once a wait frame arrived.
|
|
187
|
+
*/
|
|
188
|
+
readonly expiresAt?: number
|
|
113
189
|
}
|
|
114
190
|
|
|
115
191
|
/** One claim the hub holds while the host's ask is still open. */
|
|
116
192
|
export interface ClaimedQuestion extends ClaimedQuestionView {
|
|
117
193
|
/** The host's ask: the phone's answer, or whatever the next answerer says. */
|
|
118
194
|
readonly answer: Promise<QuestionAnswer>
|
|
195
|
+
/**
|
|
196
|
+
* Epoch milliseconds at which the host's own deadline falls, from the first
|
|
197
|
+
* wait frame of this claim.
|
|
198
|
+
*
|
|
199
|
+
* Resolves undefined when there is no deadline to publish — a blocking ask, a
|
|
200
|
+
* failed subscription, or a stream that ended before its first frame — and
|
|
201
|
+
* never rejects: a phone must be shown the question whether or not the host
|
|
202
|
+
* would say how long it has.
|
|
203
|
+
*/
|
|
204
|
+
readonly deadline: Promise<number | undefined>
|
|
119
205
|
}
|
|
120
206
|
|
|
121
207
|
/** What one `question.answer` produced: acknowledgement, or the refusal to send. */
|
|
@@ -134,6 +220,15 @@ export const QUESTION_TIMED_OUT_CODE = 'ASK_TIMED_OUT'
|
|
|
134
220
|
export const MINTED_CALL_ID_PREFIX = 'bridge:'
|
|
135
221
|
/** Entropy of one minted call id. */
|
|
136
222
|
export const MINTED_CALL_ID_BYTES = 16
|
|
223
|
+
/**
|
|
224
|
+
* How often at most a phone is told that the host's deadline moved, in
|
|
225
|
+
* milliseconds.
|
|
226
|
+
*
|
|
227
|
+
* The host re-reports its remaining wait on every touch of the wait, which can
|
|
228
|
+
* be far more often than a countdown needs; one update per second keeps the
|
|
229
|
+
* wire quiet without the phone ever showing a stale second.
|
|
230
|
+
*/
|
|
231
|
+
export const QUESTION_WAIT_BROADCAST_MIN_INTERVAL_MS = 1000
|
|
137
232
|
|
|
138
233
|
export interface OpenQuestionInput {
|
|
139
234
|
readonly sessionId: string
|
|
@@ -153,6 +248,15 @@ export interface OpenQuestionInput {
|
|
|
153
248
|
readonly signal?: AbortSignal
|
|
154
249
|
/** Called once when the host's deadline released the model but the question stayed answerable. */
|
|
155
250
|
readonly onContinued?: (question: ClaimedQuestionView) => void
|
|
251
|
+
/**
|
|
252
|
+
* Called when a later wait frame moved the host's deadline, so a phone that
|
|
253
|
+
* already holds the card can update it in place.
|
|
254
|
+
*
|
|
255
|
+
* The first frame is not reported here: it is what {@link ClaimedQuestion.deadline}
|
|
256
|
+
* hands the hub for the question's own first broadcast, so reporting it would
|
|
257
|
+
* publish the same deadline twice.
|
|
258
|
+
*/
|
|
259
|
+
readonly onDeadline?: (question: ClaimedQuestionView) => void
|
|
156
260
|
/**
|
|
157
261
|
* Called when the claim leaves this bridge without the phone answering it — the
|
|
158
262
|
* host's own UI answered first, or the last paired client went away. The phone
|
|
@@ -176,6 +280,10 @@ interface Claim {
|
|
|
176
280
|
readonly onContinued: ((question: ClaimedQuestionView) => void) | undefined
|
|
177
281
|
/** Tells the phone its card is over; set from the input's `onSettled`. */
|
|
178
282
|
readonly notifySettled: () => void
|
|
283
|
+
/** Epoch milliseconds of the host's deadline, once a wait frame reported one. */
|
|
284
|
+
expiresAt: number | undefined
|
|
285
|
+
/** End the wait subscription and its timer. Idempotent. */
|
|
286
|
+
readonly stopWait: () => void
|
|
179
287
|
}
|
|
180
288
|
|
|
181
289
|
export interface QuestionClaimsOptions {
|
|
@@ -185,6 +293,11 @@ export interface QuestionClaimsOptions {
|
|
|
185
293
|
readonly log: (message: string) => void
|
|
186
294
|
/** Id source, injectable so a test can answer the exact id it was given. */
|
|
187
295
|
readonly random?: (byteLength: number) => string
|
|
296
|
+
/**
|
|
297
|
+
* Clock the deadline bookkeeping reads, injectable so a suite can move the
|
|
298
|
+
* remaining wait instead of sleeping through it.
|
|
299
|
+
*/
|
|
300
|
+
readonly now?: () => number
|
|
188
301
|
}
|
|
189
302
|
|
|
190
303
|
/** @returns whether a failure is the host's own foreground deadline. */
|
|
@@ -192,6 +305,34 @@ function isTimedOut(reason: unknown): boolean {
|
|
|
192
305
|
return (reason as { code?: unknown } | null | undefined)?.code === QUESTION_TIMED_OUT_CODE
|
|
193
306
|
}
|
|
194
307
|
|
|
308
|
+
/**
|
|
309
|
+
* The publishable view of one claim.
|
|
310
|
+
*
|
|
311
|
+
* The deadline key is spread in rather than set to undefined: a blocking ask has
|
|
312
|
+
* no expiry at all, and the phone must see no such field instead of a null one.
|
|
313
|
+
* @param record - the live claim.
|
|
314
|
+
* @returns the claim's fields as the hub publishes them.
|
|
315
|
+
*/
|
|
316
|
+
function viewOf(record: Claim): ClaimedQuestionView {
|
|
317
|
+
return {
|
|
318
|
+
sessionId: record.sessionId,
|
|
319
|
+
callId: record.callId,
|
|
320
|
+
questions: record.questions,
|
|
321
|
+
...(record.expiresAt === undefined ? {} : { expiresAt: record.expiresAt }),
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Read the remaining wait out of one `attachWait` frame.
|
|
327
|
+
* @param frame - the frame as the gateway delivered it.
|
|
328
|
+
* @returns the remaining milliseconds, or undefined when the frame is not usable.
|
|
329
|
+
*/
|
|
330
|
+
function remainingOf(frame: unknown): number | undefined {
|
|
331
|
+
const remaining = (frame as { remainingMs?: unknown } | null | undefined)?.remainingMs
|
|
332
|
+
if (typeof remaining !== 'number' || !Number.isFinite(remaining) || remaining < 0) return undefined
|
|
333
|
+
return remaining
|
|
334
|
+
}
|
|
335
|
+
|
|
195
336
|
/**
|
|
196
337
|
* Project a host question into the contract's own fields.
|
|
197
338
|
*
|
|
@@ -232,6 +373,7 @@ export class QuestionClaims {
|
|
|
232
373
|
readonly #peer: GatewayPeer
|
|
233
374
|
readonly #log: (message: string) => void
|
|
234
375
|
readonly #random: (byteLength: number) => string
|
|
376
|
+
readonly #now: () => number
|
|
235
377
|
readonly #claims = new Map<string, Claim>()
|
|
236
378
|
|
|
237
379
|
constructor(options: QuestionClaimsOptions) {
|
|
@@ -239,6 +381,7 @@ export class QuestionClaims {
|
|
|
239
381
|
this.#peer = options.peer
|
|
240
382
|
this.#log = options.log
|
|
241
383
|
this.#random = options.random ?? randomBase64Url
|
|
384
|
+
this.#now = options.now ?? Date.now
|
|
242
385
|
}
|
|
243
386
|
|
|
244
387
|
/** @returns how many questions this bridge currently holds. */
|
|
@@ -290,15 +433,38 @@ export class QuestionClaims {
|
|
|
290
433
|
}
|
|
291
434
|
return nextStarted
|
|
292
435
|
}
|
|
436
|
+
// The host parks its own deadline timer while a waiter is attached, so this
|
|
437
|
+
// subscription must let go at the instant the deadline it reported has
|
|
438
|
+
// passed: only then does the host raise ASK_TIMED_OUT, which is what turns
|
|
439
|
+
// the claim into today's `continued` one. Every settling path stops it.
|
|
440
|
+
const wait = new AbortController()
|
|
441
|
+
let waitTimer: NodeJS.Timeout | undefined
|
|
442
|
+
let stopped = false
|
|
443
|
+
// The resolver is installed by the executor below; a claim that closes before
|
|
444
|
+
// the first frame needs calling it early, and that early call must be a no-op.
|
|
445
|
+
const deferred: { resolve?: (deadline: number | undefined) => void } = {}
|
|
446
|
+
const deadline = new Promise<number | undefined>((resolve) => { deferred.resolve = resolve })
|
|
447
|
+
const resolveDeadline = (value: number | undefined): void => { deferred.resolve?.(value) }
|
|
448
|
+
const stopWait = (): void => {
|
|
449
|
+
if (stopped) return
|
|
450
|
+
stopped = true
|
|
451
|
+
if (waitTimer !== undefined) {
|
|
452
|
+
clearTimeout(waitTimer)
|
|
453
|
+
waitTimer = undefined
|
|
454
|
+
}
|
|
455
|
+
wait.abort()
|
|
456
|
+
// A claim that closes before the host said anything must not leave the hub
|
|
457
|
+
// awaiting a first frame that will never come.
|
|
458
|
+
resolveDeadline(undefined)
|
|
459
|
+
}
|
|
293
460
|
const record: Claim = {
|
|
294
461
|
sessionId: input.sessionId,
|
|
295
462
|
callId: input.callId,
|
|
296
463
|
hosted: input.hosted,
|
|
297
464
|
questions: projectQuestions(input.questions),
|
|
298
465
|
state: 'open',
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
},
|
|
466
|
+
expiresAt: undefined,
|
|
467
|
+
notifySettled: (): void => { input.onSettled?.(viewOf(record)) },
|
|
302
468
|
settle,
|
|
303
469
|
fail,
|
|
304
470
|
delegate: (): void => {
|
|
@@ -306,6 +472,72 @@ export class QuestionClaims {
|
|
|
306
472
|
},
|
|
307
473
|
detach: (): void => { signal?.removeEventListener('abort', onAbort) },
|
|
308
474
|
onContinued: input.onContinued,
|
|
475
|
+
stopWait,
|
|
476
|
+
}
|
|
477
|
+
/** Arm the timer that lets go of the wait when the host's own deadline is due. */
|
|
478
|
+
const armDeadline = (expiresAt: number): void => {
|
|
479
|
+
if (waitTimer !== undefined) clearTimeout(waitTimer)
|
|
480
|
+
waitTimer = setTimeout(() => { stopWait() }, Math.max(0, expiresAt - this.#now()))
|
|
481
|
+
// A question waiting on a phone must never keep the host process alive.
|
|
482
|
+
waitTimer.unref?.()
|
|
483
|
+
}
|
|
484
|
+
/**
|
|
485
|
+
* Follow the host's own remaining wait and report it when it moves.
|
|
486
|
+
*
|
|
487
|
+
* A failure here is not a verdict on the question: the phone can answer it
|
|
488
|
+
* without ever knowing the deadline, so every error only ends the
|
|
489
|
+
* subscription instead of failing the claim.
|
|
490
|
+
*/
|
|
491
|
+
const trackWait = async (): Promise<void> => {
|
|
492
|
+
if (stopped) return
|
|
493
|
+
let frames = 0
|
|
494
|
+
let lastBroadcast = 0
|
|
495
|
+
try {
|
|
496
|
+
const stream = await this.#gateway.stream({
|
|
497
|
+
namespace: 'userQuestions',
|
|
498
|
+
method: 'attachWait',
|
|
499
|
+
args: wireArgs('userQuestions', 'attachWait', { agentId: input.sessionId, callId: input.callId }),
|
|
500
|
+
peer: this.#peer,
|
|
501
|
+
signal: wait.signal,
|
|
502
|
+
})
|
|
503
|
+
const iterator = stream[Symbol.asyncIterator]()
|
|
504
|
+
try {
|
|
505
|
+
for (;;) {
|
|
506
|
+
const next = await iterator.next()
|
|
507
|
+
if (next.done === true) return
|
|
508
|
+
const remaining = remainingOf(next.value)
|
|
509
|
+
if (remaining === undefined) continue
|
|
510
|
+
const at = this.#now()
|
|
511
|
+
const expiresAt = at + remaining
|
|
512
|
+
record.expiresAt = expiresAt
|
|
513
|
+
armDeadline(expiresAt)
|
|
514
|
+
// The first frame IS the deadline: the hub is already awaiting it for
|
|
515
|
+
// the question's own broadcast, so only later frames move a card.
|
|
516
|
+
if (frames === 0) {
|
|
517
|
+
frames = 1
|
|
518
|
+
lastBroadcast = at
|
|
519
|
+
resolveDeadline(expiresAt)
|
|
520
|
+
continue
|
|
521
|
+
}
|
|
522
|
+
if (at - lastBroadcast < QUESTION_WAIT_BROADCAST_MIN_INTERVAL_MS) continue
|
|
523
|
+
lastBroadcast = at
|
|
524
|
+
input.onDeadline?.(viewOf(record))
|
|
525
|
+
}
|
|
526
|
+
} finally {
|
|
527
|
+
await iterator.return?.()
|
|
528
|
+
}
|
|
529
|
+
} catch (error) {
|
|
530
|
+
// Releasing the wait on purpose — its deadline arrived, the claim settled —
|
|
531
|
+
// aborts the stream, and the gateway reports an aborted Remote stream as a
|
|
532
|
+
// failed read of its own (`gateway/cancelled`). That is the normal path,
|
|
533
|
+
// not a fault, so only an end nobody asked for is logged.
|
|
534
|
+
if (!stopped) this.#log(`userQuestions/attachWait failed: ${failureText(error)}`)
|
|
535
|
+
} finally {
|
|
536
|
+
// No frame, no deadline: the hub publishes the question without one
|
|
537
|
+
// instead of waiting on a wait the host never reported. A no-op once a
|
|
538
|
+
// frame already settled it.
|
|
539
|
+
resolveDeadline(undefined)
|
|
540
|
+
}
|
|
309
541
|
}
|
|
310
542
|
this.#claims.set(input.callId, record)
|
|
311
543
|
// The rest of the chain is NOT started here. A connected phone takes the
|
|
@@ -314,7 +546,10 @@ export class QuestionClaims {
|
|
|
314
546
|
// A phone that is absent, or a claim that is withdrawn, still delegates.
|
|
315
547
|
if (signal?.aborted === true) onAbort()
|
|
316
548
|
else signal?.addEventListener('abort', onAbort, { once: true })
|
|
317
|
-
|
|
549
|
+
if (input.hosted) void trackWait()
|
|
550
|
+
// A blocking ask carries no host deadline at all: nothing to follow, nothing to publish.
|
|
551
|
+
else resolveDeadline(undefined)
|
|
552
|
+
return { ...viewOf(record), answer, deadline }
|
|
318
553
|
}
|
|
319
554
|
|
|
320
555
|
/**
|
|
@@ -335,6 +570,7 @@ export class QuestionClaims {
|
|
|
335
570
|
if (record.state === 'open') {
|
|
336
571
|
this.#claims.delete(callId)
|
|
337
572
|
record.detach()
|
|
573
|
+
record.stopWait()
|
|
338
574
|
record.settle({ answers: answer.answers })
|
|
339
575
|
return { ok: true }
|
|
340
576
|
}
|
|
@@ -347,17 +583,22 @@ export class QuestionClaims {
|
|
|
347
583
|
}
|
|
348
584
|
|
|
349
585
|
/**
|
|
350
|
-
* Every
|
|
586
|
+
* Every question this bridge still holds, as `question.list` publishes it.
|
|
351
587
|
*
|
|
352
|
-
*
|
|
353
|
-
*
|
|
354
|
-
*
|
|
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.
|
|
355
595
|
*/
|
|
356
|
-
|
|
357
|
-
return [...this.#claims.values()].map(
|
|
596
|
+
listed(): readonly ListedQuestion[] {
|
|
597
|
+
return [...this.#claims.values()].map(record => ({
|
|
598
|
+
key: questionKey(record.sessionId, record.callId),
|
|
358
599
|
sessionId: record.sessionId,
|
|
359
|
-
|
|
360
|
-
|
|
600
|
+
state: record.state,
|
|
601
|
+
payload: questionPayload(record.state, viewOf(record)),
|
|
361
602
|
}))
|
|
362
603
|
}
|
|
363
604
|
|
|
@@ -372,6 +613,7 @@ export class QuestionClaims {
|
|
|
372
613
|
if (record === undefined) continue
|
|
373
614
|
this.#claims.delete(callId)
|
|
374
615
|
record.detach()
|
|
616
|
+
record.stopWait()
|
|
375
617
|
record.notifySettled()
|
|
376
618
|
if (record.state !== 'open') continue
|
|
377
619
|
record.delegate()
|
|
@@ -382,8 +624,9 @@ export class QuestionClaims {
|
|
|
382
624
|
|
|
383
625
|
/**
|
|
384
626
|
* Withdraw every claim, delegating each open question to the next answerer.
|
|
385
|
-
* Called when the last paired phone
|
|
386
|
-
*
|
|
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.
|
|
387
630
|
* @returns how many questions were still open.
|
|
388
631
|
*/
|
|
389
632
|
withdrawAll(): number {
|
|
@@ -408,7 +651,10 @@ export class QuestionClaims {
|
|
|
408
651
|
if (accepted !== true) {
|
|
409
652
|
return { ok: false, code: CODE_QUESTION_UNKNOWN, msg: 'the host does not hold this question as answerable yet' }
|
|
410
653
|
}
|
|
654
|
+
const record = this.#claims.get(callId)
|
|
411
655
|
this.#claims.delete(callId)
|
|
656
|
+
// The late answer is the end of this ask: nothing is left to count down.
|
|
657
|
+
record?.stopWait()
|
|
412
658
|
return { ok: true }
|
|
413
659
|
} catch (error) {
|
|
414
660
|
this.#log(`userQuestions/answer failed: ${failureText(error)}`)
|
|
@@ -427,6 +673,7 @@ export class QuestionClaims {
|
|
|
427
673
|
const record = this.#claims.get(callId)
|
|
428
674
|
if (record === undefined || record.state !== 'open') return
|
|
429
675
|
record.detach()
|
|
676
|
+
record.stopWait()
|
|
430
677
|
record.fail(reason)
|
|
431
678
|
// Only a host deadline keeps a claim answerable, and only for an id the host
|
|
432
679
|
// itself assigned: a minted one has no host question behind it, so nothing
|
|
@@ -436,7 +683,7 @@ export class QuestionClaims {
|
|
|
436
683
|
return
|
|
437
684
|
}
|
|
438
685
|
record.state = 'continued'
|
|
439
|
-
record.onContinued?.(
|
|
686
|
+
record.onContinued?.(viewOf(record))
|
|
440
687
|
}
|
|
441
688
|
}
|
|
442
689
|
|
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 {
|