@linxin666/dsh-remote-bridge 0.4.15 → 0.4.17

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.
@@ -64,16 +64,24 @@ export interface QuestionItem {
64
64
  readonly multiSelect?: boolean
65
65
  }
66
66
 
67
- /** One item of the answer batch. */
67
+ /**
68
+ * One item of the answer batch. `selected` is a mutable array because the host's
69
+ * own answer item declares it mutable, and a readonly array is not assignable to
70
+ * a mutable one - the host's `user-questions/request` handler is typed with its
71
+ * own answer type, so this mirror has to stay assignable to it.
72
+ */
68
73
  export interface QuestionAnswerItem {
69
74
  readonly id: string
70
- readonly selected: readonly string[]
75
+ readonly selected: string[]
71
76
  readonly custom?: string
72
77
  }
73
78
 
74
- /** The whole answer batch the host's ask is settled with. */
79
+ /**
80
+ * The whole answer batch the host's ask is settled with. `answers` is mutable for
81
+ * the same reason {@link QuestionAnswerItem} keeps `selected` mutable.
82
+ */
75
83
  export interface QuestionAnswer {
76
- readonly answers: readonly QuestionAnswerItem[]
84
+ readonly answers: QuestionAnswerItem[]
77
85
  }
78
86
 
79
87
  /**
@@ -192,6 +200,14 @@ export interface ClaimedQuestionView {
192
200
  export interface ClaimedQuestion extends ClaimedQuestionView {
193
201
  /** The host's ask: the phone's answer, or whatever the next answerer says. */
194
202
  readonly answer: Promise<QuestionAnswer>
203
+ /**
204
+ * Hand the same question to the host's own answerer chain, once.
205
+ *
206
+ * Called by the hub the instant a claim exists, in every mode: the phone is
207
+ * not the only surface any more, so the host's own panel shows the card too
208
+ * and the first answer settles it.
209
+ */
210
+ readonly startHostChain: () => void
195
211
  /**
196
212
  * Epoch milliseconds at which the host's own deadline falls, from the first
197
213
  * wait frame of this claim.
@@ -417,11 +433,11 @@ export class QuestionClaims {
417
433
  })
418
434
  const signal = input.signal
419
435
  const onAbort = (): void => { this.#expire(input.callId, signal?.reason) }
420
- // The rest of the answerer chain is the host's own UI, and it must keep
421
- // showing the question: claiming here used to swallow it. Started once, at
422
- // claim time, so both surfaces display it and the first answer settles it.
423
- // A rejection is not a verdict — the phone may still answer — so it is
424
- // remembered for a later withdraw instead of failing the claim now.
436
+ // The rest of the answerer chain is the host's own UI. Started once and
437
+ // memoized, because both paths that yield to it — the claim itself, and the
438
+ // withdraw that hands a question back — have to reach the same panel rather
439
+ // than open a second one. A rejection is not a verdict (the phone may still
440
+ // answer), so only the withdraw path turns one into this claim's failure.
425
441
  let nextStarted: Promise<QuestionAnswer> | undefined
426
442
  const startNext = (): Promise<QuestionAnswer> => {
427
443
  if (nextStarted === undefined) {
@@ -539,17 +555,27 @@ export class QuestionClaims {
539
555
  resolveDeadline(undefined)
540
556
  }
541
557
  }
558
+ /**
559
+ * Hand this claim to the host's own answerers, once.
560
+ *
561
+ * Both surfaces are shown the question and the first answer settles it: an
562
+ * answer from the chain ends the claim here, and an answer from the phone
563
+ * ends it in {@link answer} — which leaves the chain's later answer ignored
564
+ * instead of settling or broadcasting a second time.
565
+ */
566
+ const startHostChain = (): void => {
567
+ void startNext().then(answer => { this.#settleFromHost(input.callId, answer) }, () => undefined)
568
+ }
542
569
  this.#claims.set(input.callId, record)
543
- // The rest of the chain is NOT started here. A connected phone takes the
544
- // question on its own, and the host's own panel would otherwise stay open
545
- // after the phone answered it — the bridge has no way to close that panel.
546
- // A phone that is absent, or a claim that is withdrawn, still delegates.
570
+ // The chain itself is started by the hub through {@link startHostChain} the
571
+ // instant this claim exists, so the panel and the phone are shown the same
572
+ // question and whichever answers first settles both.
547
573
  if (signal?.aborted === true) onAbort()
548
574
  else signal?.addEventListener('abort', onAbort, { once: true })
549
575
  if (input.hosted) void trackWait()
550
576
  // A blocking ask carries no host deadline at all: nothing to follow, nothing to publish.
551
577
  else resolveDeadline(undefined)
552
- return { ...viewOf(record), answer, deadline }
578
+ return { ...viewOf(record), answer, deadline, startHostChain }
553
579
  }
554
580
 
555
581
  /**
@@ -662,6 +688,27 @@ export class QuestionClaims {
662
688
  }
663
689
  }
664
690
 
691
+ /**
692
+ * Settle one claim from the host's own answerer chain.
693
+ *
694
+ * Only a claim this bridge still holds can be ended this way: a phone that
695
+ * answered first deletes it, and the chain's late answer must not settle it a
696
+ * second time or announce a card that is already gone. A claim the host's own
697
+ * deadline turned into `continued` is left alone as well — its answer promise
698
+ * is that deadline, and the phone's late answer still has its own road.
699
+ * @param callId - the claim the host answered.
700
+ * @param answer - the host's own answer.
701
+ */
702
+ #settleFromHost(callId: string, answer: QuestionAnswer): void {
703
+ const record = this.#claims.get(callId)
704
+ if (record === undefined || record.state !== 'open') return
705
+ this.#claims.delete(callId)
706
+ record.detach()
707
+ record.stopWait()
708
+ record.notifySettled()
709
+ record.settle(answer)
710
+ }
711
+
665
712
  /**
666
713
  * Settle one open claim from the host's own cancellation lifetime.
667
714
  *
package/src/core/wire.ts CHANGED
@@ -88,6 +88,15 @@ export function subagentAddress(parentSessionId: string, childSessionId: string)
88
88
  /** The failure code the harness raises for a child addressed as an ordinary session. */
89
89
  export const CODE_SUBAGENT_ADDRESS_REQUIRED = 'session/agent-busy'
90
90
 
91
+ /**
92
+ * The failure the harness raises when a session address names a session this host
93
+ * does not have. `session/page` answers it, `session/projections` answers
94
+ * `null` for the same fact, and a caller that needs one name for "no such
95
+ * session" folds both into its own code rather than letting one fact cross as two
96
+ * different failures.
97
+ */
98
+ export const CODE_HOST_SESSION_NOT_FOUND = 'session/not-found'
99
+
91
100
  /**
92
101
  * One endpoint's declared argument names: the name of its single parameter, the
93
102
  * empty declaration of a parameterless endpoint, or the exact names of an
package/src/index.ts CHANGED
@@ -24,9 +24,8 @@
24
24
  * `question.answer` settles the host's own ask while it is open, or reaches a
25
25
  * continued question through the host's Remote `answer`. With no paired
26
26
  * connection on the wire the claim is still made when a roster device registered
27
- * for notifications, which is reached by the alert and holds the question for one
28
- * window before it goes back to the host's chain (§11.3, §32). Every other case
29
- * delegates, and a timed question keeps the Host's own deadline.
27
+ * for notifications, and the alert is then its delivery (§11.3, §32). Every other
28
+ * case delegates, and a timed question keeps the Host's own deadline.
30
29
  *
31
30
  * The browser half lives in `src/client/` and is declared by `dsh.client`: one
32
31
  * sidebar-foot panel that reads the same loopback-gated token route over a
@@ -51,7 +50,7 @@ import { DeviceRoster } from './core/devices.ts'
51
50
  import { createRemoteBridgeFace, RemoteBridgeRegistry, REMOTE_BRIDGE_SERVICE } from './core/extension.ts'
52
51
  import { JobRoster, type JobRegistryFace } from './core/jobs.ts'
53
52
  import { ModelEffortPreferences } from './core/model-efforts.ts'
54
- import { BridgeHub, OFFLINE_QUESTION_CLAIM_TTL_MS } from './core/hub.ts'
53
+ import { BridgeHub } from './core/hub.ts'
55
54
  import { createOwnerPeer } from './core/owner-peer.ts'
56
55
  import { PairingService } from './core/pairing.ts'
57
56
  import { LiveActivityNotifier } from './core/liveactivity.ts'
@@ -151,24 +150,6 @@ export const ROSTER_PATH = 'remote-bridge/devices.json'
151
150
  */
152
151
  export const EFFORT_PATH = 'remote-bridge/model-efforts.json'
153
152
 
154
- /** The environment variable that moves the offline claim window (§32.2). */
155
- export const OFFLINE_CLAIM_TTL_ENV = 'DSH_REMOTE_BRIDGE_OFFLINE_CLAIM_TTL_MS'
156
-
157
- /**
158
- * The offline claim window this process runs with.
159
- *
160
- * The window is the bridge's own constant and never a host fact, so a deployment
161
- * moves it here. A value that is missing, unreadable or not positive falls back to
162
- * the constant instead of closing the window: a zero window would silently switch
163
- * the channel off, and an operator who wanted that would be turning off push.
164
- * @param env - the process environment, injectable for a suite.
165
- * @returns the window in milliseconds.
166
- */
167
- export function offlineClaimTtl(env: NodeJS.ProcessEnv = process.env): number {
168
- const configured = Number(env[OFFLINE_CLAIM_TTL_ENV])
169
- return Number.isFinite(configured) && configured > 0 ? configured : OFFLINE_QUESTION_CLAIM_TTL_MS
170
- }
171
-
172
153
  /**
173
154
  * The environment variable that moves when a still-waiting turn end is reported (§28).
174
155
  *
@@ -1586,8 +1567,6 @@ function applyImpl(ctx: Context, config?: unknown): void {
1586
1567
  log,
1587
1568
  })
1588
1569
  const extensions = new RemoteBridgeRegistry()
1589
- // 离线认领的窗口归桥所有,宿主从不认识它(§32.2):常量是默认值,部署可用环境变量覆盖。
1590
- const offlineClaimTtlMs = offlineClaimTtl()
1591
1570
  /**
1592
1571
  * 第二会合点在协议面上的两处接线:`hello` 的公告与 `relay.grant` 的收帧。
1593
1572
  *
@@ -1652,7 +1631,6 @@ function applyImpl(ctx: Context, config?: unknown): void {
1652
1631
  push,
1653
1632
  liveActivity,
1654
1633
  turnEndQuiet,
1655
- offlineClaimTtlMs,
1656
1634
  onPushRegistered: () => { watchers.sync() },
1657
1635
  ...(relayFace === undefined ? {} : { relay: relayFace }),
1658
1636
  onDeviceEnrolled: () => { wakeRelay() },