@linxin666/dsh-remote-bridge 0.4.9 → 0.4.11

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.
Files changed (40) hide show
  1. package/README.i18n.yaml +2 -2
  2. package/README.md +28 -4
  3. package/README.zh.md +27 -4
  4. package/lib/client.js +511 -9
  5. package/lib/client.js.map +1 -1
  6. package/lib/index.js +8671 -148
  7. package/lib/types/client/UiPresentRow.d.ts +13 -0
  8. package/lib/types/client/index.d.ts +4 -0
  9. package/lib/types/client/locales.d.ts +15 -2
  10. package/lib/types/client/toolview-slot.d.ts +68 -0
  11. package/lib/types/client/ui-card.d.ts +160 -0
  12. package/lib/types/core/connection.d.ts +38 -0
  13. package/lib/types/core/devices.d.ts +4 -0
  14. package/lib/types/core/extension.d.ts +13 -0
  15. package/lib/types/core/hub.d.ts +64 -0
  16. package/lib/types/core/protocol.d.ts +71 -7
  17. package/lib/types/core/push.d.ts +10 -2
  18. package/lib/types/core/turn-end-quiet.d.ts +135 -0
  19. package/lib/types/core/ui.d.ts +238 -0
  20. package/lib/types/host/ui-tools.d.ts +80 -0
  21. package/lib/types/host/watchers.d.ts +7 -0
  22. package/lib/types/index.d.ts +29 -1
  23. package/package.json +3 -2
  24. package/src/client/UiPresentRow.tsx +149 -0
  25. package/src/client/index.ts +11 -0
  26. package/src/client/locales.ts +22 -2
  27. package/src/client/toolview-slot.ts +84 -0
  28. package/src/client/ui-card.ts +368 -0
  29. package/src/client/ui-present.module.css +87 -0
  30. package/src/core/connection.ts +169 -6
  31. package/src/core/devices.ts +116 -2
  32. package/src/core/extension.ts +20 -0
  33. package/src/core/hub.ts +156 -1
  34. package/src/core/protocol.ts +78 -6
  35. package/src/core/push.ts +34 -3
  36. package/src/core/turn-end-quiet.ts +388 -0
  37. package/src/core/ui.ts +398 -0
  38. package/src/host/ui-tools.ts +233 -0
  39. package/src/host/watchers.ts +27 -3
  40. package/src/index.ts +70 -2
@@ -36,11 +36,12 @@
36
36
  * @module dsh-remote-bridge/host/watchers
37
37
  */
38
38
 
39
- import { SNAPSHOT_MAX_MESSAGES } from '../core/connection.ts'
39
+ import { isTurnStartDelta, SNAPSHOT_MAX_MESSAGES } from '../core/connection.ts'
40
40
  import { deltaOfFollowFrame, MessageIndex, readFollowBaseline, SpawnCallIndex, ToolCallIndex } from '../core/dsh-frames.ts'
41
41
  import { failureText, withServiceUnavailableRetry } from '../core/gateway-errors.ts'
42
42
  import type { LiveActivityNotifier } from '../core/liveactivity.ts'
43
43
  import { pushEventOfTurnEnd, type PushNotifier } from '../core/push.ts'
44
+ import type { TurnEndQuietGate } from '../core/turn-end-quiet.ts'
44
45
  import { sessionAddress, wireArgs, type BridgeGateway, type GatewayPeer } from '../core/wire.ts'
45
46
 
46
47
  /** Most standing streams this bridge keeps open at once. */
@@ -62,6 +63,12 @@ export interface StandingWatcherOptions {
62
63
  readonly liveActivity?: LiveActivityNotifier
63
64
  /** The notification path of §28; absent for the same reason. */
64
65
  readonly push?: PushNotifier
66
+ /**
67
+ * The gate both channels announce a finished turn through, so a card and a
68
+ * notification wait for the same quiet session. Absent is a watcher set built
69
+ * without one — a suite — and then both report at the frame.
70
+ */
71
+ readonly turnEndQuiet?: TurnEndQuietGate
65
72
  readonly log: (message: string) => void
66
73
  readonly limit?: number
67
74
  readonly reopenMs?: number
@@ -208,7 +215,14 @@ export class StandingWatchers {
208
215
  if (next.done === true) break
209
216
  if (signal.aborted) break
210
217
  const delta = deltaOfFollowFrame(next.value, toolCalls, messages, memory)
211
- if (delta === undefined || delta.kind !== 'turn-end') continue
218
+ if (delta === undefined) continue
219
+ // Only an increment that proves this session's own work proves a new turn; a
220
+ // settled child, a usage count and a catalog change all arrive here without
221
+ // meaning the user prompted again.
222
+ if (delta.kind !== 'turn-end') {
223
+ if (isTurnStartDelta(delta)) this.#options.turnEndQuiet?.noteTurnStarted(sessionId)
224
+ continue
225
+ }
212
226
  this.#turnEnded(sessionId, delta.reason)
213
227
  }
214
228
  } catch (error) {
@@ -224,10 +238,20 @@ export class StandingWatchers {
224
238
  * Both, on purpose: §28's notification is what wakes a phone that is not
225
239
  * holding a socket, and §29's card end is what clears a lock screen. Reporting
226
240
  * only one of them leaves the other surface stale, which is the exact state
227
- * this module exists to end.
241
+ * this module exists to end. Both go through the shared gate, so a watcher and
242
+ * a client that followed the same session announce it at the same instant — when
243
+ * the session is quiet — and never at the frame.
228
244
  */
229
245
  #turnEnded(sessionId: string, reason: string): void {
230
246
  try {
247
+ const gate = this.#options.turnEndQuiet
248
+ if (gate !== undefined) {
249
+ gate.noteTurnEnd(sessionId, reason)
250
+ // Nothing has been sent at this point, so the line says what really
251
+ // happened: the end is held, and the gate's own line reports the send.
252
+ this.#options.log(`watcher turn-end session=${sessionId} reason=${reason} held=quiet-gate`)
253
+ return
254
+ }
231
255
  this.#options.push?.notify(pushEventOfTurnEnd(reason), sessionId)
232
256
  this.#options.liveActivity?.noteTurnEnd(sessionId, reason)
233
257
  const pushed = this.#options.liveActivity?.enabled === true ? 'card' : 'notification-only'
package/src/index.ts CHANGED
@@ -13,6 +13,12 @@
13
13
  * - `approval/request` is claimed in-process while a paired device is connected,
14
14
  * and delegated (`next()`) in every other case, which the Host normalizes to
15
15
  * the fail-closed `'unavailable'`.
16
+ * - `ui_present` is this bridge's own model tool (protocol §35): one whole
17
+ * self-contained page is announced as `ui.request` to the paired clients that
18
+ * declared `ui.v1`, and the user's first action inside it comes back as that
19
+ * tool call's result. `ui.event` settles it, `ui.list` reads the cards still
20
+ * held. No paired client that declared the channel means the call answers
21
+ * `unavailable` at once instead of holding the agent.
16
22
  * - `user-questions/request` is claimed the same way and published as
17
23
  * `question.request` to the devices that declared `questions.v1`; their
18
24
  * `question.answer` settles the host's own ask while it is open, or reaches a
@@ -37,6 +43,7 @@ import type { Context } from '@deepseek-ai/cordis'
37
43
  import type {} from '@deepseek-ai/dsh-api-gateway'
38
44
  import type {} from '@deepseek-ai/dsh-host-webserver'
39
45
  import type {} from '@deepseek-ai/dsh-session'
46
+ import type {} from '@deepseek-ai/dsh-tools'
40
47
  import type {} from '@deepseek-ai/dsh-user-approval'
41
48
  import { ApprovalRegistry } from './core/approvals.ts'
42
49
  import type { ConnectionAuth } from './core/connection.ts'
@@ -49,8 +56,10 @@ import { PairingService } from './core/pairing.ts'
49
56
  import { LiveActivityNotifier } from './core/liveactivity.ts'
50
57
  import { PushNotifier } from './core/push.ts'
51
58
  import { QuestionClaims } from './core/questions.ts'
59
+ import { UiCards } from './core/ui.ts'
52
60
  import { WS_PATH } from './core/protocol.ts'
53
61
  import { SessionMeta, type SessionStoreFace, type SessionTitleFace, type WorkspacesFace } from './core/session-meta.ts'
62
+ import { sessionQuietProbe, TURN_END_QUIET_MAX_MS, TurnEndQuietGate } from './core/turn-end-quiet.ts'
54
63
  import { SessionPermissions, type ApprovalPolicyFace, type PermissionPresetsFace, type SandboxPolicyFace } from './core/session-permissions.ts'
55
64
  import { selectionOf, modelSelectionOf } from './core/connection.ts'
56
65
  import { UsageService, type CredentialStoreFace, type SessionRosterFace, type SessionRouteFace, type SettingsFace } from './core/usage.ts'
@@ -62,14 +71,21 @@ import { StandingWatchers } from './host/watchers.ts'
62
71
  import { LoopbackSquads } from './host/squads.ts'
63
72
  import { mountOnce } from './mount-once.ts'
64
73
  import { makePairTokenRoute } from './host/routes.ts'
74
+ import { createUiPresentTool } from './host/ui-tools.ts'
65
75
  import { hostWebOrigin, LoopbackUsage } from './host/usage-stats.ts'
66
76
  import { isWebSocketUpgrade, presentedTokenOf, readSecWebSocketKey, refuseUpgrade, serveUpgrade } from './host/websocket.ts'
67
77
 
68
78
  /** Loader row identity of this plugin. */
69
79
  export const name = 'remote-bridge'
70
80
 
71
- /** The Host services this plugin cannot run without. */
72
- export const inject = ['typertGateway', 'webServer']
81
+ /**
82
+ * The Host services this plugin cannot run without.
83
+ *
84
+ * `tools` is what protocol §35 added: this bridge contributes one model tool of
85
+ * its own (`ui_present`), so a host that composes no tool registry is a host where
86
+ * the card channel has no caller — the row simply does not mount here.
87
+ */
88
+ export const inject = ['typertGateway', 'webServer', 'tools']
73
89
 
74
90
  /** Where the paired-device roster lives inside the DSH home. */
75
91
  export const ROSTER_PATH = 'remote-bridge/devices.json'
@@ -92,6 +108,27 @@ export function offlineClaimTtl(env: NodeJS.ProcessEnv = process.env): number {
92
108
  return Number.isFinite(configured) && configured > 0 ? configured : OFFLINE_QUESTION_CLAIM_TTL_MS
93
109
  }
94
110
 
111
+ /** The environment variable that moves how long a finished turn waits for its session to go quiet (§28). */
112
+ export const TURN_END_QUIET_MAX_ENV = 'DSH_REMOTE_BRIDGE_TURN_END_QUIET_MAX_MS'
113
+
114
+ /**
115
+ * The quiet-wait budget this process runs with.
116
+ *
117
+ * The budget is the bridge's own constant and never a host fact, so a deployment
118
+ * moves it here. A value that is missing, unreadable or not positive falls back to
119
+ * the constant instead of closing the window: a zero budget would announce every
120
+ * turn end at its frame, and an operator who wanted that would be turning push
121
+ * off. The budget only bounds the wait — a session that does fall quiet inside it
122
+ * is announced then, and one that does not is announced at the end with the
123
+ * reason it was still busy.
124
+ * @param env - the process environment, injectable for a suite.
125
+ * @returns the budget in milliseconds.
126
+ */
127
+ export function turnEndQuietMaxWait(env: NodeJS.ProcessEnv = process.env): number {
128
+ const configured = Number(env[TURN_END_QUIET_MAX_ENV])
129
+ return Number.isFinite(configured) && configured > 0 ? configured : TURN_END_QUIET_MAX_MS
130
+ }
131
+
95
132
  /** This machine's IANA time zone, or undefined when the platform has none. */
96
133
  function localTimeZone(): string | undefined {
97
134
  try {
@@ -332,6 +369,9 @@ function applyImpl(ctx: Context): void {
332
369
  // it is gated by the bridge's own pairing check (see core/owner-peer.ts).
333
370
  const ownerPeer = createOwnerPeer(ctx)
334
371
  const questions = new QuestionClaims({ gateway: ctx.typertGateway, peer: ownerPeer, log })
372
+ // 整页卡(§35):一张宿主级的卡表,所有连接共享——一条动作只属于发起它的那个
373
+ // 工具调用,与它从哪台设备回来无关。
374
+ const ui = new UiCards()
335
375
  // One roster per mount, shared by every connection: a background job is a fact
336
376
  // about the host, not about the device that asked about it.
337
377
  const jobs = new JobRoster({ faces: () => jobFaces(ctx), log })
@@ -390,6 +430,18 @@ function applyImpl(ctx: Context): void {
390
430
  return dropped
391
431
  },
392
432
  })
433
+ // The quiet gate both channels announce a finished turn through: §28's
434
+ // notification and §29's card end wait for the host's own answer that nothing is
435
+ // left running under that session — its own agent, any descendant, and any job
436
+ // the host attributes to either. Inert while no key is configured, because both
437
+ // channels are then off and no timer is worth arming.
438
+ const turnEndQuiet = new TurnEndQuietGate({
439
+ push,
440
+ liveActivity,
441
+ probe: sessionQuietProbe({ gateway: ctx.typertGateway, peer: ownerPeer, jobs }),
442
+ maxWaitMs: turnEndQuietMaxWait(),
443
+ log,
444
+ })
393
445
  // The bridge's own follow streams (§28/§29): a suspended phone has no socket,
394
446
  // so the turn end its card has to show would otherwise be seen by nobody. One
395
447
  // watcher per session that still holds a card token, armed from the roster and
@@ -408,6 +460,7 @@ function applyImpl(ctx: Context): void {
408
460
  peer: ownerPeer,
409
461
  sessions: watchedSessions,
410
462
  ...(apnsSender === undefined ? {} : { liveActivity, push }),
463
+ turnEndQuiet,
411
464
  log,
412
465
  })
413
466
  const permissions = new SessionPermissions(() => permissionFaces(ctx), log)
@@ -440,6 +493,7 @@ function applyImpl(ctx: Context): void {
440
493
  roster,
441
494
  approvals,
442
495
  questions,
496
+ ui,
443
497
  jobs,
444
498
  sessionMeta,
445
499
  usage,
@@ -448,6 +502,7 @@ function applyImpl(ctx: Context): void {
448
502
  squads,
449
503
  push,
450
504
  liveActivity,
505
+ turnEndQuiet,
451
506
  offlineClaimTtlMs,
452
507
  onPushRegistered: () => { watchers.sync() },
453
508
  now: () => Date.now(),
@@ -480,11 +535,24 @@ function applyImpl(ctx: Context): void {
480
535
  disposeUpgrade()
481
536
  disposeRoute()
482
537
  watchers.dispose()
538
+ turnEndQuiet.dispose()
483
539
  hub.dispose()
484
540
  jobs.dispose()
485
541
  }
486
542
  }, 'remote-bridge: protocol v1 routes')
487
543
 
544
+ // §35 的模型工具:`ui_present` 把 agent 写的整页交给已配对的客户端,并在用户
545
+ // 动作时把结构化事件作为它自己的返回值交回。注册在 `ctx.effect` 里、返回
546
+ // disposer,所以插件卸载时工具名随行消失,宿主工具表不会留下一根指向空实现的
547
+ // 指针;`execute` 把 `exec.signal` 一路传到认领表,回合被取消时那张卡立刻结算。
548
+ ctx.effect(() => {
549
+ const disposeTool = ctx.tools.register(createUiPresentTool({
550
+ present: async (card, signal) => await hub.present(card, signal),
551
+ }))
552
+ log('model tool ui_present registered')
553
+ return () => { disposeTool() }
554
+ }, 'remote-bridge: ui_present tool')
555
+
488
556
  // 会话事件是 `usage.session` 唯一的来源:桥不读会话日志、也不回填历史,所以
489
557
  // 这个监听器挂上之前广播的事件不会有读数,宿主没广播过的窗口也不会被编出来。
490
558
  ctx.on('session/event', (session, event) => { usage.record(session.id, event) })