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

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 (69) hide show
  1. package/LICENSE +202 -0
  2. package/README.i18n.yaml +6 -0
  3. package/README.md +243 -2
  4. package/README.zh.md +244 -0
  5. package/cordis.patch.yml +17 -0
  6. package/lib/client.js +1830 -0
  7. package/lib/client.js.map +1 -0
  8. package/lib/index.js +3744 -0
  9. package/lib/types/client/PairEntry.d.ts +16 -0
  10. package/lib/types/client/PairPanel.d.ts +55 -0
  11. package/lib/types/client/icons.d.ts +18 -0
  12. package/lib/types/client/index.d.ts +35 -0
  13. package/lib/types/client/locales.d.ts +42 -0
  14. package/lib/types/client/pair-token.d.ts +73 -0
  15. package/lib/types/client/semantic.d.ts +28 -0
  16. package/lib/types/core/approvals.d.ts +75 -0
  17. package/lib/types/core/connection.d.ts +119 -0
  18. package/lib/types/core/crypto.d.ts +48 -0
  19. package/lib/types/core/devices.d.ts +72 -0
  20. package/lib/types/core/dsh-frames.d.ts +181 -0
  21. package/lib/types/core/envelope.d.ts +98 -0
  22. package/lib/types/core/gateway-errors.d.ts +49 -0
  23. package/lib/types/core/hub.d.ts +101 -0
  24. package/lib/types/core/loopback.d.ts +35 -0
  25. package/lib/types/core/owner-peer.d.ts +49 -0
  26. package/lib/types/core/pairing.d.ts +61 -0
  27. package/lib/types/core/protocol.d.ts +89 -0
  28. package/lib/types/core/questions.d.ts +246 -0
  29. package/lib/types/core/session-meta.d.ts +105 -0
  30. package/lib/types/core/session-permissions.d.ts +134 -0
  31. package/lib/types/core/wire.d.ts +115 -0
  32. package/lib/types/core/ws-frames.d.ts +61 -0
  33. package/lib/types/host/dsh-home.d.ts +16 -0
  34. package/lib/types/host/pair-origin.d.ts +44 -0
  35. package/lib/types/host/routes.d.ts +27 -0
  36. package/lib/types/host/websocket.d.ts +104 -0
  37. package/lib/types/index.d.ts +40 -0
  38. package/package.json +87 -3
  39. package/src/client/PairEntry.tsx +151 -0
  40. package/src/client/PairPanel.tsx +183 -0
  41. package/src/client/icons.tsx +72 -0
  42. package/src/client/index.ts +81 -0
  43. package/src/client/locales.ts +72 -0
  44. package/src/client/pair-token.ts +104 -0
  45. package/src/client/panel.module.css +344 -0
  46. package/src/client/semantic.ts +37 -0
  47. package/src/core/approvals.ts +142 -0
  48. package/src/core/connection.ts +1090 -0
  49. package/src/core/crypto.ts +113 -0
  50. package/src/core/devices.ts +209 -0
  51. package/src/core/dsh-frames.ts +486 -0
  52. package/src/core/envelope.ts +173 -0
  53. package/src/core/gateway-errors.ts +92 -0
  54. package/src/core/hub.ts +238 -0
  55. package/src/core/loopback.ts +60 -0
  56. package/src/core/owner-peer.ts +70 -0
  57. package/src/core/pairing.ts +113 -0
  58. package/src/core/protocol.ts +120 -0
  59. package/src/core/questions.ts +454 -0
  60. package/src/core/session-meta.ts +176 -0
  61. package/src/core/session-permissions.ts +266 -0
  62. package/src/core/wire.ts +155 -0
  63. package/src/core/ws-frames.ts +125 -0
  64. package/src/css-modules.d.ts +6 -0
  65. package/src/host/dsh-home.ts +42 -0
  66. package/src/host/pair-origin.ts +77 -0
  67. package/src/host/routes.ts +85 -0
  68. package/src/host/websocket.ts +325 -0
  69. package/src/index.ts +205 -0
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Semantic attribute values for the protocol v1 pairing panel (contract:
3
+ * skins/skin-center/contracts/semantic-attrs-v1.md). Root containers carry
4
+ * `data-dsh-plugin`; parts carry bare `data-dsh-part` values owned by that
5
+ * plugin attribute.
6
+ */
7
+
8
+ /** Plugin short name stamped on the footer trigger and the modal root. */
9
+ export const PLUGIN_ATTR = 'remote-bridge'
10
+
11
+ /** The sidebar footer seat trigger. */
12
+ export const PART_ENTRY = 'entry'
13
+
14
+ /** The modal panel root. */
15
+ export const PART_PANEL = 'panel'
16
+
17
+ /** The QR symbol's wrapper — it also mirrors the encoded string. */
18
+ export const PART_QR = 'qr'
19
+
20
+ /** The plaintext pairing code row. */
21
+ export const PART_TOKEN = 'token'
22
+
23
+ /** A state notice: the origin warning, or a refusal the host raised. */
24
+ export const PART_NOTICE = 'notice'
25
+
26
+ /** The mint-again button. */
27
+ export const PART_REFRESH = 'refresh'
28
+
29
+ /** The copy-the-code button. */
30
+ export const PART_COPY = 'copy'
31
+
32
+ /**
33
+ * Attribute carrying the exact string the QR symbol encodes, so the operator
34
+ * (and a UI check) can compare it against the host route's answer without
35
+ * decoding the symbol. The same string is already on screen in plaintext.
36
+ */
37
+ export const QR_VALUE_ATTR = 'data-dsh-qr-value'
@@ -0,0 +1,142 @@
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
+ /** One live question. */
43
+ interface PendingPrompt {
44
+ readonly settle: (answer: ApprovalAnswer) => void
45
+ readonly timer: NodeJS.Timeout
46
+ readonly detached: () => void
47
+ }
48
+
49
+ export interface ApprovalRegistryOptions {
50
+ readonly timeoutMs?: number
51
+ /** Id source, injectable so a test can answer the exact id it was given. */
52
+ readonly random?: (byteLength: number) => string
53
+ }
54
+
55
+ /**
56
+ * Pending approval questions for one bridge process.
57
+ *
58
+ * One timer per question, one `settle` for every path (answer, timeout, agent
59
+ * cancellation, connection loss), so a question can never be answered twice and
60
+ * a withdrawn question leaves no timer or abort listener behind.
61
+ */
62
+ export class ApprovalRegistry {
63
+ readonly #timeoutMs: number
64
+ readonly #random: (byteLength: number) => string
65
+ readonly #pending = new Map<string, PendingPrompt>()
66
+
67
+ constructor(options: ApprovalRegistryOptions = {}) {
68
+ this.#timeoutMs = options.timeoutMs ?? APPROVAL_TIMEOUT_MS
69
+ this.#random = options.random ?? randomBase64Url
70
+ }
71
+
72
+ /** @returns how many questions are currently waiting for a phone. */
73
+ get size(): number {
74
+ return this.#pending.size
75
+ }
76
+
77
+ /**
78
+ * Claim one question.
79
+ * @param input - session identity, tool name, presentation summary, and the agent's own lifetime.
80
+ * @returns the prompt to publish, with its answer promise.
81
+ */
82
+ open(input: ApprovalPromptInput): ApprovalPrompt {
83
+ const id = this.#random(APPROVAL_ID_BYTES)
84
+ let settle: (answer: ApprovalAnswer) => void = () => undefined
85
+ const answer = new Promise<ApprovalAnswer>(resolve => { settle = resolve })
86
+ const withdraw = (): void => { this.#withdraw(id) }
87
+ const timer = setTimeout(withdraw, this.#timeoutMs)
88
+ timer.unref?.()
89
+ const signal = input.signal
90
+ const detached = signal === undefined ? (): void => undefined : (): void => { signal.removeEventListener('abort', withdraw) }
91
+ signal?.addEventListener('abort', withdraw, { once: true })
92
+ this.#pending.set(id, { settle, timer, detached })
93
+ return { id, sessionId: input.sessionId, toolName: input.toolName, summary: input.summary, answer }
94
+ }
95
+
96
+ /**
97
+ * Answer one question.
98
+ * @returns whether the id was still pending; a stale, expired or unknown id is refused.
99
+ */
100
+ resolve(id: string, decision: ClaimedDecision): boolean {
101
+ return this.#settle(id, decision) !== undefined
102
+ }
103
+
104
+ /**
105
+ * Withdraw the named questions, delegating each to the next answerer.
106
+ * @returns how many were still pending.
107
+ */
108
+ abandon(ids: readonly string[]): number {
109
+ let withdrawn = 0
110
+ for (const id of ids) if (this.#settle(id, undefined) !== undefined) withdrawn += 1
111
+ return withdrawn
112
+ }
113
+
114
+ /**
115
+ * Withdraw every pending question, delegating each to the next answerer.
116
+ * Called when the last paired phone goes away: a question nobody can answer
117
+ * must not hold the agent until its own timeout.
118
+ * @returns how many questions were withdrawn.
119
+ */
120
+ abandonAll(): number {
121
+ return this.abandon([...this.#pending.keys()])
122
+ }
123
+
124
+ /**
125
+ * Settle one question at most once.
126
+ * @returns whether this call is the one that settled it.
127
+ */
128
+ #settle(id: string, answer: ApprovalAnswer): boolean | undefined {
129
+ const entry = this.#pending.get(id)
130
+ if (entry === undefined) return undefined
131
+ this.#pending.delete(id)
132
+ clearTimeout(entry.timer)
133
+ entry.detached()
134
+ entry.settle(answer)
135
+ return true
136
+ }
137
+
138
+ /** Withdraw without caring whether it was still pending (timeout and abort paths). */
139
+ #withdraw(id: string): void {
140
+ this.#settle(id, undefined)
141
+ }
142
+ }