@linxin666/dsh-remote-bridge 0.0.0-stage → 0.4.8

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 (81) hide show
  1. package/LICENSE +202 -0
  2. package/README.i18n.yaml +6 -0
  3. package/README.md +277 -2
  4. package/README.zh.md +276 -0
  5. package/cordis.patch.yml +17 -0
  6. package/icon.svg +3 -0
  7. package/lib/client.js +1830 -0
  8. package/lib/client.js.map +1 -0
  9. package/lib/index.js +5789 -0
  10. package/lib/types/client/PairEntry.d.ts +16 -0
  11. package/lib/types/client/PairPanel.d.ts +55 -0
  12. package/lib/types/client/icons.d.ts +18 -0
  13. package/lib/types/client/index.d.ts +35 -0
  14. package/lib/types/client/locales.d.ts +42 -0
  15. package/lib/types/client/pair-token.d.ts +73 -0
  16. package/lib/types/client/semantic.d.ts +28 -0
  17. package/lib/types/core/approvals.d.ts +99 -0
  18. package/lib/types/core/connection.d.ts +170 -0
  19. package/lib/types/core/crypto.d.ts +48 -0
  20. package/lib/types/core/delta.d.ts +101 -0
  21. package/lib/types/core/devices.d.ts +72 -0
  22. package/lib/types/core/dsh-frames.d.ts +332 -0
  23. package/lib/types/core/envelope.d.ts +132 -0
  24. package/lib/types/core/extension.d.ts +144 -0
  25. package/lib/types/core/gateway-errors.d.ts +49 -0
  26. package/lib/types/core/hub.d.ts +127 -0
  27. package/lib/types/core/jobs.d.ts +204 -0
  28. package/lib/types/core/loopback.d.ts +35 -0
  29. package/lib/types/core/owner-peer.d.ts +49 -0
  30. package/lib/types/core/pairing.d.ts +61 -0
  31. package/lib/types/core/protocol.d.ts +319 -0
  32. package/lib/types/core/questions.d.ts +294 -0
  33. package/lib/types/core/session-meta.d.ts +105 -0
  34. package/lib/types/core/session-permissions.d.ts +134 -0
  35. package/lib/types/core/wire.d.ts +115 -0
  36. package/lib/types/core/ws-frames.d.ts +72 -0
  37. package/lib/types/host/dsh-home.d.ts +16 -0
  38. package/lib/types/host/pair-origin.d.ts +44 -0
  39. package/lib/types/host/routes.d.ts +27 -0
  40. package/lib/types/host/websocket.d.ts +111 -0
  41. package/lib/types/index.d.ts +55 -0
  42. package/lib/types/mount-once.d.ts +55 -0
  43. package/locale/en.json +6 -0
  44. package/locale/ru.json +6 -0
  45. package/locale/zh.json +6 -0
  46. package/package.json +93 -3
  47. package/src/client/PairEntry.tsx +151 -0
  48. package/src/client/PairPanel.tsx +183 -0
  49. package/src/client/icons.tsx +72 -0
  50. package/src/client/index.ts +81 -0
  51. package/src/client/locales.ts +72 -0
  52. package/src/client/pair-token.ts +104 -0
  53. package/src/client/panel.module.css +344 -0
  54. package/src/client/semantic.ts +37 -0
  55. package/src/core/approvals.ts +173 -0
  56. package/src/core/connection.ts +1906 -0
  57. package/src/core/crypto.ts +113 -0
  58. package/src/core/delta.ts +204 -0
  59. package/src/core/devices.ts +209 -0
  60. package/src/core/dsh-frames.ts +843 -0
  61. package/src/core/envelope.ts +219 -0
  62. package/src/core/extension.ts +197 -0
  63. package/src/core/gateway-errors.ts +92 -0
  64. package/src/core/hub.ts +310 -0
  65. package/src/core/jobs.ts +372 -0
  66. package/src/core/loopback.ts +60 -0
  67. package/src/core/owner-peer.ts +70 -0
  68. package/src/core/pairing.ts +113 -0
  69. package/src/core/protocol.ts +381 -0
  70. package/src/core/questions.ts +630 -0
  71. package/src/core/session-meta.ts +176 -0
  72. package/src/core/session-permissions.ts +266 -0
  73. package/src/core/wire.ts +159 -0
  74. package/src/core/ws-frames.ts +139 -0
  75. package/src/css-modules.d.ts +6 -0
  76. package/src/host/dsh-home.ts +42 -0
  77. package/src/host/pair-origin.ts +77 -0
  78. package/src/host/routes.ts +85 -0
  79. package/src/host/websocket.ts +359 -0
  80. package/src/index.ts +257 -0
  81. package/src/mount-once.ts +142 -0
@@ -0,0 +1,173 @@
1
+ /**
2
+ * The bridge's claims on `approval/request`.
3
+ *
4
+ * The agent asks a waterfall question and the bridge claims it only while a
5
+ * paired phone can actually answer; every other outcome delegates to `next()`,
6
+ * which the Host normalizes to the fail-closed `'unavailable'`. A phone that
7
+ * never answers therefore equals a refusal, which is the intended default: an
8
+ * unreachable operator must not silently authorize a tool.
9
+ * @module dsh-remote-bridge/core/approvals
10
+ */
11
+
12
+ import { randomBase64Url } from './crypto.ts'
13
+
14
+ /** How long a claimed question waits for a phone before delegating. */
15
+ export const APPROVAL_TIMEOUT_MS = 120_000
16
+ /** Entropy of one question id. */
17
+ export const APPROVAL_ID_BYTES = 16
18
+
19
+ /** The two decisions a phone may send back. */
20
+ export type ClaimedDecision = 'allowed-once' | 'rejected'
21
+ /** A claim's answer: a decision, or `undefined` for "the bridge withdraws, ask the next answerer". */
22
+ export type ApprovalAnswer = ClaimedDecision | undefined
23
+
24
+ export interface ApprovalPromptInput {
25
+ readonly sessionId: string
26
+ readonly toolName: string
27
+ readonly summary: string
28
+ /** Cancellation lifetime of the pending request; aborting withdraws the question. */
29
+ readonly signal?: AbortSignal
30
+ }
31
+
32
+ /** One claimed question, as handed back to the bridge. */
33
+ export interface ApprovalPrompt {
34
+ readonly id: string
35
+ readonly sessionId: string
36
+ readonly toolName: string
37
+ readonly summary: string
38
+ /** Settles exactly once, with a decision or `undefined` to delegate. */
39
+ readonly answer: Promise<ApprovalAnswer>
40
+ }
41
+
42
+ /**
43
+ * One live question as a second delivery publishes it.
44
+ *
45
+ * The same four fields the first broadcast carries, so a card a phone was shown
46
+ * and the same card handed to it again after a reconnect cannot be spelled
47
+ * differently.
48
+ */
49
+ export interface PendingPromptView {
50
+ readonly id: string
51
+ readonly sessionId: string
52
+ readonly toolName: string
53
+ readonly summary: string
54
+ }
55
+
56
+ /** One live question. */
57
+ interface PendingPrompt {
58
+ readonly sessionId: string
59
+ readonly toolName: string
60
+ readonly summary: string
61
+ readonly settle: (answer: ApprovalAnswer) => void
62
+ readonly timer: NodeJS.Timeout
63
+ readonly detached: () => void
64
+ }
65
+
66
+ export interface ApprovalRegistryOptions {
67
+ readonly timeoutMs?: number
68
+ /** Id source, injectable so a test can answer the exact id it was given. */
69
+ readonly random?: (byteLength: number) => string
70
+ }
71
+
72
+ /**
73
+ * Pending approval questions for one bridge process.
74
+ *
75
+ * One timer per question, one `settle` for every path (answer, timeout, agent
76
+ * cancellation, connection loss), so a question can never be answered twice and
77
+ * a withdrawn question leaves no timer or abort listener behind.
78
+ */
79
+ export class ApprovalRegistry {
80
+ readonly #timeoutMs: number
81
+ readonly #random: (byteLength: number) => string
82
+ readonly #pending = new Map<string, PendingPrompt>()
83
+
84
+ constructor(options: ApprovalRegistryOptions = {}) {
85
+ this.#timeoutMs = options.timeoutMs ?? APPROVAL_TIMEOUT_MS
86
+ this.#random = options.random ?? randomBase64Url
87
+ }
88
+
89
+ /** @returns how many questions are currently waiting for a phone. */
90
+ get size(): number {
91
+ return this.#pending.size
92
+ }
93
+
94
+ /**
95
+ * Every question still waiting for a phone, in claim order.
96
+ *
97
+ * Only a live claim can be named here: {@link #settle} is the single path out
98
+ * of the pending map, and an answer, the bridge's own timeout, the agent's
99
+ * cancellation and every withdrawal all pass through it before this reads the
100
+ * map again. A question this bridge released therefore cannot come back to a
101
+ * client that reconnects.
102
+ * @returns the fields a second delivery republishes, oldest claim first.
103
+ */
104
+ openPrompts(): readonly PendingPromptView[] {
105
+ return [...this.#pending].map(([id, entry]) => ({ id, sessionId: entry.sessionId, toolName: entry.toolName, summary: entry.summary }))
106
+ }
107
+
108
+ /**
109
+ * Claim one question.
110
+ * @param input - session identity, tool name, presentation summary, and the agent's own lifetime.
111
+ * @returns the prompt to publish, with its answer promise.
112
+ */
113
+ open(input: ApprovalPromptInput): ApprovalPrompt {
114
+ const id = this.#random(APPROVAL_ID_BYTES)
115
+ let settle: (answer: ApprovalAnswer) => void = () => undefined
116
+ const answer = new Promise<ApprovalAnswer>(resolve => { settle = resolve })
117
+ const withdraw = (): void => { this.#withdraw(id) }
118
+ const timer = setTimeout(withdraw, this.#timeoutMs)
119
+ timer.unref?.()
120
+ const signal = input.signal
121
+ const detached = signal === undefined ? (): void => undefined : (): void => { signal.removeEventListener('abort', withdraw) }
122
+ signal?.addEventListener('abort', withdraw, { once: true })
123
+ this.#pending.set(id, { sessionId: input.sessionId, toolName: input.toolName, summary: input.summary, settle, timer, detached })
124
+ return { id, sessionId: input.sessionId, toolName: input.toolName, summary: input.summary, answer }
125
+ }
126
+
127
+ /**
128
+ * Answer one question.
129
+ * @returns whether the id was still pending; a stale, expired or unknown id is refused.
130
+ */
131
+ resolve(id: string, decision: ClaimedDecision): boolean {
132
+ return this.#settle(id, decision) !== undefined
133
+ }
134
+
135
+ /**
136
+ * Withdraw the named questions, delegating each to the next answerer.
137
+ * @returns how many were still pending.
138
+ */
139
+ abandon(ids: readonly string[]): number {
140
+ let withdrawn = 0
141
+ for (const id of ids) if (this.#settle(id, undefined) !== undefined) withdrawn += 1
142
+ return withdrawn
143
+ }
144
+
145
+ /**
146
+ * Withdraw every pending question, delegating each to the next answerer.
147
+ * Called when the last paired phone goes away: a question nobody can answer
148
+ * must not hold the agent until its own timeout.
149
+ * @returns how many questions were withdrawn.
150
+ */
151
+ abandonAll(): number {
152
+ return this.abandon([...this.#pending.keys()])
153
+ }
154
+
155
+ /**
156
+ * Settle one question at most once.
157
+ * @returns whether this call is the one that settled it.
158
+ */
159
+ #settle(id: string, answer: ApprovalAnswer): boolean | undefined {
160
+ const entry = this.#pending.get(id)
161
+ if (entry === undefined) return undefined
162
+ this.#pending.delete(id)
163
+ clearTimeout(entry.timer)
164
+ entry.detached()
165
+ entry.settle(answer)
166
+ return true
167
+ }
168
+
169
+ /** Withdraw without caring whether it was still pending (timeout and abort paths). */
170
+ #withdraw(id: string): void {
171
+ this.#settle(id, undefined)
172
+ }
173
+ }