@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.
Files changed (55) hide show
  1. package/README.i18n.yaml +2 -2
  2. package/README.md +50 -17
  3. package/README.zh.md +50 -17
  4. package/lib/client.js +274 -253
  5. package/lib/client.js.map +1 -1
  6. package/lib/index.js +6690 -2277
  7. package/lib/types/client/PairEntry.d.ts +2 -2
  8. package/lib/types/client/PairPanel.d.ts +10 -2
  9. package/lib/types/client/icons.d.ts +5 -13
  10. package/lib/types/client/locales.d.ts +10 -14
  11. package/lib/types/client/semantic.d.ts +9 -5
  12. package/lib/types/core/commands.d.ts +59 -0
  13. package/lib/types/core/connection.d.ts +68 -0
  14. package/lib/types/core/devices.d.ts +98 -2
  15. package/lib/types/core/hub.d.ts +65 -15
  16. package/lib/types/core/liveactivity.d.ts +324 -0
  17. package/lib/types/core/protocol.d.ts +260 -1
  18. package/lib/types/core/push.d.ts +178 -0
  19. package/lib/types/core/questions.d.ts +61 -9
  20. package/lib/types/core/session-meta.d.ts +55 -1
  21. package/lib/types/core/squads.d.ts +130 -0
  22. package/lib/types/core/usage-stats.d.ts +160 -0
  23. package/lib/types/core/usage.d.ts +294 -0
  24. package/lib/types/host/apns.d.ts +151 -0
  25. package/lib/types/host/squads-ledger.d.ts +39 -0
  26. package/lib/types/host/squads.d.ts +77 -0
  27. package/lib/types/host/usage-stats.d.ts +75 -0
  28. package/lib/types/host/watchers.d.ts +88 -0
  29. package/lib/types/index.d.ts +18 -2
  30. package/package.json +13 -13
  31. package/src/client/PairEntry.tsx +58 -31
  32. package/src/client/PairPanel.tsx +181 -97
  33. package/src/client/icons.tsx +17 -60
  34. package/src/client/locales.ts +15 -23
  35. package/src/client/panel.module.css +239 -186
  36. package/src/client/semantic.ts +10 -6
  37. package/src/core/commands.ts +95 -0
  38. package/src/core/connection.ts +646 -10
  39. package/src/core/devices.ts +221 -3
  40. package/src/core/hub.ts +362 -42
  41. package/src/core/liveactivity.ts +833 -0
  42. package/src/core/protocol.ts +294 -1
  43. package/src/core/push.ts +354 -0
  44. package/src/core/questions.ts +81 -10
  45. package/src/core/session-meta.ts +73 -1
  46. package/src/core/squads.ts +345 -0
  47. package/src/core/usage-stats.ts +424 -0
  48. package/src/core/usage.ts +937 -0
  49. package/src/core/wire.ts +8 -0
  50. package/src/host/apns.ts +320 -0
  51. package/src/host/squads-ledger.ts +105 -0
  52. package/src/host/squads.ts +206 -0
  53. package/src/host/usage-stats.ts +155 -0
  54. package/src/host/watchers.ts +272 -0
  55. package/src/index.ts +257 -8
@@ -0,0 +1,88 @@
1
+ /**
2
+ * The bridge's own follow streams: one per session that holds a live card.
3
+ *
4
+ * §28 and §29 both describe things this bridge must report while it is the only
5
+ * party still awake, and everything that reports them used to hang off a
6
+ * client's own stream: a turn end was observed only while this bridge was
7
+ * draining a follow stream for a connected phone. A suspended app has no such
8
+ * stream — its socket is closed and the subscription is aborted with the
9
+ * connection — so the card frozen on its lock screen kept saying "running" long
10
+ * after the turn was over, and no APNs delivery ever failed, because nothing was
11
+ * ever sent.
12
+ *
13
+ * A standing watcher is that missing party: for one session with at least one
14
+ * registered live-activity token this bridge opens its OWN follow stream, under
15
+ * the same in-process owner peer every other gateway call speaks for, and turns
16
+ * the one frame it cares about — `turn/end` — into the two things a card needs:
17
+ * §28's notification and §29's card end, each addressed to that session alone.
18
+ *
19
+ * Five rules keep that from becoming a second, unaccountable scheduler:
20
+ *
21
+ * - a watcher exists exactly while its session holds a card token, reconciled by
22
+ * {@link StandingWatchers.sync} from the roster rather than from any client
23
+ * frame;
24
+ * - at most {@link WATCHER_LIMIT} streams are open at once; past that a session
25
+ * is logged once and left unwatched, so a card on a session that never ends
26
+ * cannot grow the set without bound;
27
+ * - dedupe is inherited, not added: a client that is also following the same
28
+ * session sees the same turn end, and the two reports collapse in the layers
29
+ * that already own that rule — §28.5's `(sessionId, event)` window and §29's
30
+ * identical-content key. No third window is introduced here;
31
+ * - a stream that ends on its own is re-armed after {@link WATCHER_REOPEN_MS},
32
+ * because the next turn is the next thing a card has to show;
33
+ * - nothing a watcher does can reach a client frame: every failure is one log
34
+ * line, and a stream that cannot be opened is dropped rather than retried in a
35
+ * tight loop.
36
+ * @module dsh-remote-bridge/host/watchers
37
+ */
38
+ import type { LiveActivityNotifier } from '../core/liveactivity.ts';
39
+ import { type PushNotifier } from '../core/push.ts';
40
+ import { type BridgeGateway, type GatewayPeer } from '../core/wire.ts';
41
+ /** Most standing streams this bridge keeps open at once. */
42
+ export declare const WATCHER_LIMIT = 64;
43
+ /** How long a watcher waits before re-opening a stream that ended on its own. */
44
+ export declare const WATCHER_REOPEN_MS = 1000;
45
+ export interface StandingWatcherOptions {
46
+ /** The host's gateway, the same one the connections speak through. */
47
+ readonly gateway: BridgeGateway;
48
+ /** The in-process owner peer every call of this bridge speaks for. */
49
+ readonly peer: GatewayPeer;
50
+ /**
51
+ * Sessions that currently hold at least one registered live-activity token,
52
+ * read from the roster on every reconcile.
53
+ */
54
+ readonly sessions: () => readonly string[];
55
+ /** The card path of §29; absent is a bridge with no signing key. */
56
+ readonly liveActivity?: LiveActivityNotifier;
57
+ /** The notification path of §28; absent for the same reason. */
58
+ readonly push?: PushNotifier;
59
+ readonly log: (message: string) => void;
60
+ readonly limit?: number;
61
+ readonly reopenMs?: number;
62
+ readonly maxMessages?: number;
63
+ }
64
+ /**
65
+ * The bridge's stand-in for a client that is no longer there.
66
+ *
67
+ * Deliberately inert when neither push path is configured: {@link sync} returns
68
+ * without opening anything, so a bridge without an Apple signing key keeps the
69
+ * exact stream count it had before this module existed.
70
+ */
71
+ export declare class StandingWatchers {
72
+ #private;
73
+ constructor(options: StandingWatcherOptions);
74
+ /** @returns how many standing streams are open right now. */
75
+ get size(): number;
76
+ /** @returns whether this bridge has any path worth opening a stream for. */
77
+ get enabled(): boolean;
78
+ /**
79
+ * Reconcile the open streams with the sessions that hold a card right now.
80
+ *
81
+ * Called when the roster changes (a registration arrived, a dead token was
82
+ * retired) and when a stream ends. Idempotent, and safe to call before the
83
+ * roster has been read: no session with a token means nothing to watch.
84
+ */
85
+ sync(): void;
86
+ /** Close every stream and drop every pending re-arm. Idempotent. */
87
+ dispose(): void;
88
+ }
@@ -16,8 +16,11 @@
16
16
  * - `user-questions/request` is claimed the same way and published as
17
17
  * `question.request` to the devices that declared `questions.v1`; their
18
18
  * `question.answer` settles the host's own ask while it is open, or reaches a
19
- * continued question through the host's Remote `answer`. Every other case
20
- * delegates, and the question keeps the Host's own deadline.
19
+ * continued question through the host's Remote `answer`. With no paired
20
+ * connection on the wire the claim is still made when a roster device registered
21
+ * for notifications, which is reached by the alert and holds the question for one
22
+ * window before it goes back to the host's chain (§11.3, §32). Every other case
23
+ * delegates, and a timed question keeps the Host's own deadline.
21
24
  *
22
25
  * The browser half lives in `src/client/` and is declared by `dsh.client`: one
23
26
  * sidebar-foot panel that reads the same loopback-gated token route over a
@@ -33,6 +36,19 @@ export declare const name = "remote-bridge";
33
36
  export declare const inject: string[];
34
37
  /** Where the paired-device roster lives inside the DSH home. */
35
38
  export declare const ROSTER_PATH = "remote-bridge/devices.json";
39
+ /** The environment variable that moves the offline claim window (§32.2). */
40
+ export declare const OFFLINE_CLAIM_TTL_ENV = "DSH_REMOTE_BRIDGE_OFFLINE_CLAIM_TTL_MS";
41
+ /**
42
+ * The offline claim window this process runs with.
43
+ *
44
+ * The window is the bridge's own constant and never a host fact, so a deployment
45
+ * moves it here. A value that is missing, unreadable or not positive falls back to
46
+ * the constant instead of closing the window: a zero window would silently switch
47
+ * the channel off, and an operator who wanted that would be turning off push.
48
+ * @param env - the process environment, injectable for a suite.
49
+ * @returns the window in milliseconds.
50
+ */
51
+ export declare function offlineClaimTtl(env?: NodeJS.ProcessEnv): number;
36
52
  /**
37
53
  * Mount the bridge.
38
54
  *
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@linxin666/dsh-remote-bridge",
3
3
  "description": "DSH remote bridge: the host half serves protocol v1 over one exact /ios/v1 WebSocket route — session roster, history baseline, prompting, live turn increments with tool activity, interrupt, and in-process agent approval — to a device paired by a one-shot bootstrap token and a P-256 challenge-response, with the token endpoint admitted only from this machine; the browser half adds one sidebar-foot panel that shows that same one-shot token as a QR symbol, and no wire surface",
4
- "version": "0.4.8",
4
+ "version": "0.4.9",
5
5
  "icon": "./icon.svg",
6
6
  "type": "module",
7
7
  "engines": {
@@ -37,7 +37,7 @@
37
37
  "license": "Apache-2.0",
38
38
  "dsh": {
39
39
  "engines": {
40
- "dsh": ">=0.2.1-rc.2"
40
+ "dsh": ">=0.2.1-rc.3"
41
41
  },
42
42
  "bundle": {
43
43
  "patch": "./cordis.patch.yml"
@@ -55,22 +55,22 @@
55
55
  "qrcode.react": "^4.2.0"
56
56
  },
57
57
  "peerDependencies": {
58
- "@deepseek-ai/dsh": ">=0.2.1-rc.2",
58
+ "@deepseek-ai/dsh": ">=0.2.1-rc.3",
59
59
  "react": "^18.2.0",
60
60
  "react-dom": "^18.2.0"
61
61
  },
62
62
  "devDependencies": {
63
63
  "@deepseek-ai/cordis": "^4.0.5",
64
- "@deepseek-ai/dsh-api-gateway": "^0.2.1-rc.2",
65
- "@deepseek-ai/dsh-client-locale": "^0.2.1-rc.2",
66
- "@deepseek-ai/dsh-client-ui-primitives": "^0.2.1-rc.2",
67
- "@deepseek-ai/dsh-client-ui-renderer": "^0.2.1-rc.2",
68
- "@deepseek-ai/dsh-client-ui-sidebar": "^0.2.1-rc.2",
69
- "@deepseek-ai/dsh-client-ui-slots": "^0.2.1-rc.2",
70
- "@deepseek-ai/dsh-host-webserver": "^0.2.1-rc.2",
71
- "@deepseek-ai/dsh-session": "^0.2.1-rc.2",
72
- "@deepseek-ai/dsh-typert-protocol": "^0.2.1-rc.2",
73
- "@deepseek-ai/dsh-user-approval": "^0.2.1-rc.2",
64
+ "@deepseek-ai/dsh-api-gateway": "^0.2.1-rc.3",
65
+ "@deepseek-ai/dsh-client-locale": "^0.2.1-rc.3",
66
+ "@deepseek-ai/dsh-client-ui-primitives": "^0.2.1-rc.3",
67
+ "@deepseek-ai/dsh-client-ui-renderer": "^0.2.1-rc.3",
68
+ "@deepseek-ai/dsh-client-ui-sidebar": "^0.2.1-rc.3",
69
+ "@deepseek-ai/dsh-client-ui-slots": "^0.2.1-rc.3",
70
+ "@deepseek-ai/dsh-host-webserver": "^0.2.1-rc.3",
71
+ "@deepseek-ai/dsh-session": "^0.2.1-rc.3",
72
+ "@deepseek-ai/dsh-typert-protocol": "^0.2.1-rc.3",
73
+ "@deepseek-ai/dsh-user-approval": "^0.2.1-rc.3",
74
74
  "@testing-library/dom": "^10.4.1",
75
75
  "@testing-library/react": "^16.3.2",
76
76
  "@types/node": "^22.20.0",
@@ -5,14 +5,14 @@
5
5
  * (declared by ui-sidebar — the same seat dsh-remote-web-ui and dsh-update
6
6
  * share).
7
7
  *
8
- * This component owns the panel's behavior: one read per open, one clock tick
9
- * while a live code is on screen, the mint-again action, and the copy action.
10
- * The wire call and the clipboard are injected (see the plugin entry), so the
11
- * rendered tree carries no closure-factory bundle import and both can be driven
12
- * by a test without patching a module.
8
+ * This component owns the panel's presence and behavior: one read per open, one
9
+ * clock tick while a live code is on screen, the mint-again action, the copy
10
+ * action, and the leave leg that keeps the modal mounted until its exit reports
11
+ * completion. The wire call and the clipboard are injected (see the plugin
12
+ * entry), so the rendered tree carries no closure-factory bundle import and both
13
+ * can be driven by a test without patching a module.
13
14
  */
14
- import { useCallback, useEffect, useRef, useState } from 'react'
15
- import { createPortal } from 'react-dom'
15
+ import { useCallback, useEffect, useId, useRef, useState } from 'react'
16
16
  import type { PropsLocale } from '@deepseek-ai/dsh-client-ui-slots'
17
17
  import { PairPanel, type PairCopyState, type PairPanelState } from './PairPanel.tsx'
18
18
  import type { PairTokenOutcome } from './pair-token.ts'
@@ -24,6 +24,19 @@ import css from './panel.module.css'
24
24
  const COPY_FEEDBACK_MS = 1_500
25
25
  /** The countdown's tick; one second is the resolution the panel shows. */
26
26
  const TICK_MS = 1_000
27
+ /**
28
+ * Upper bound on the leave leg. The card reports its own animation end, and the
29
+ * reduced-motion fade reports a transition end; this only ends the presence of a
30
+ * browser that reports neither, just past var(--ds-transition-duration).
31
+ */
32
+ const EXIT_TIMEOUT_MS = 260
33
+
34
+ /**
35
+ * The modal's presence. `closing` is a state of its own: the card stays mounted
36
+ * for its reverse exit and unmounts only when that exit reports completion,
37
+ * instead of disappearing on the same tick as the click.
38
+ */
39
+ type Presence = 'closed' | 'open' | 'closing'
27
40
 
28
41
  export type PairEntryProps = PropsLocale<'remote-bridge'> & {
29
42
  /** Whether the sidebar renders wide content (false = 56px rail). */
@@ -53,15 +66,16 @@ function panelStateOf(outcome: PairTokenOutcome, issuedAt: number): PairPanelSta
53
66
  }
54
67
 
55
68
  /**
56
- * Render the pairing seat trigger and, when open, the panel.
69
+ * Render the pairing seat trigger and, while it is open or leaving, the panel.
57
70
  * @param props - the footer seat's column state, the injected actions and the locale seat.
58
- * @returns the trigger and the panel portal.
71
+ * @returns the trigger and, when present, the official Modal holding the panel.
59
72
  */
60
73
  export function PairEntry({ wide, read, copy, t }: PairEntryProps) {
61
- const [open, setOpen] = useState(false)
74
+ const [presence, setPresence] = useState<Presence>('closed')
62
75
  const [state, setState] = useState<PairPanelState>({ kind: 'reading' })
63
76
  const [copyState, setCopyState] = useState<PairCopyState>('idle')
64
77
  const [now, setNow] = useState<number>(() => Date.now())
78
+ const panelId = useId()
65
79
  // Generation counter: closing (or minting again) invalidates a read that is
66
80
  // still in flight, so an answer that lands after the panel closed cannot
67
81
  // resurrect a code nobody asked for.
@@ -79,20 +93,38 @@ export function PairEntry({ wide, read, copy, t }: PairEntryProps) {
79
93
  }, [read])
80
94
 
81
95
  const openPanel = useCallback((): void => {
82
- setOpen(true)
96
+ setPresence('open')
83
97
  setCopyState('idle')
84
98
  mint()
85
99
  }, [mint])
86
100
 
87
- const closePanel = useCallback((): void => {
101
+ // Escape, the mask click, the focus trap and focus restoration belong to the
102
+ // official Modal; this only decides what closing means for the presence, and
103
+ // it invalidates a read that is still in flight.
104
+ const requestClose = useCallback((): void => {
88
105
  generation.current += 1
89
- setOpen(false)
106
+ setPresence((current) => (current === 'closed' ? 'closed' : 'closing'))
107
+ }, [])
108
+
109
+ const finishClose = useCallback((): void => {
110
+ setPresence((current) => (current === 'closing' ? 'closed' : current))
90
111
  }, [])
91
112
 
113
+ const closing = presence === 'closing'
114
+ const showing = presence !== 'closed'
115
+
116
+ // Safety net for the leave leg: the card normally reports its own animation or
117
+ // transition end, and this bound covers the case where neither arrives.
118
+ useEffect(() => {
119
+ if (!closing) return
120
+ const timer = window.setTimeout(() => { finishClose() }, EXIT_TIMEOUT_MS)
121
+ return () => { window.clearTimeout(timer) }
122
+ }, [closing, finishClose])
123
+
92
124
  // One ticker per live code; it clears itself at the deadline, so a panel left
93
125
  // open on an expired code stops re-rendering instead of ticking forever.
94
126
  useEffect(() => {
95
- if (!open || state.kind !== 'ready') return
127
+ if (presence !== 'open' || state.kind !== 'ready') return
96
128
  const deadline = state.expiresAt
97
129
  const timer = window.setInterval(() => {
98
130
  const current = Date.now()
@@ -100,19 +132,10 @@ export function PairEntry({ wide, read, copy, t }: PairEntryProps) {
100
132
  if (current >= deadline) window.clearInterval(timer)
101
133
  }, TICK_MS)
102
134
  return () => { window.clearInterval(timer) }
103
- }, [open, state])
104
-
105
- useEffect(() => {
106
- if (!open) return
107
- const onKey = (event: KeyboardEvent): void => {
108
- if (event.key === 'Escape') closePanel()
109
- }
110
- window.addEventListener('keydown', onKey)
111
- return () => { window.removeEventListener('keydown', onKey) }
112
- }, [open, closePanel])
135
+ }, [presence, state])
113
136
 
114
- const handleCopy = useCallback((token: string): void => {
115
- void copy(token).then((ok) => {
137
+ const handleCopy = useCallback((payload: string): void => {
138
+ void copy(payload).then((ok) => {
116
139
  setCopyState(ok ? 'copied' : 'failed')
117
140
  window.setTimeout(() => { setCopyState('idle') }, COPY_FEEDBACK_MS)
118
141
  })
@@ -127,24 +150,28 @@ export function PairEntry({ wide, read, copy, t }: PairEntryProps) {
127
150
  data-dsh-part={PART_ENTRY}
128
151
  data-wide={wide ? 'wide' : 'rail'}
129
152
  aria-label={t('entry.label')}
130
- aria-expanded={open}
153
+ aria-expanded={showing}
154
+ aria-controls={showing ? panelId : undefined}
131
155
  title={t('entry.label')}
132
156
  onClick={openPanel}
133
157
  >
134
158
  <QrIcon />
135
159
  </button>
136
- {open && createPortal(
160
+ {showing && (
137
161
  <PairPanel
162
+ id={panelId}
138
163
  t={t}
139
164
  state={state}
140
165
  now={now}
141
166
  host={window.location.host}
142
167
  copy={copyState}
143
- onClose={closePanel}
168
+ showing={showing}
169
+ closing={closing}
170
+ onClose={requestClose}
171
+ onExited={finishClose}
144
172
  onRefresh={mint}
145
173
  onCopy={handleCopy}
146
- />,
147
- document.body,
174
+ />
148
175
  )}
149
176
  </>
150
177
  )