@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.
- package/LICENSE +202 -0
- package/README.i18n.yaml +6 -0
- package/README.md +243 -2
- package/README.zh.md +244 -0
- package/cordis.patch.yml +17 -0
- package/lib/client.js +1830 -0
- package/lib/client.js.map +1 -0
- package/lib/index.js +3744 -0
- package/lib/types/client/PairEntry.d.ts +16 -0
- package/lib/types/client/PairPanel.d.ts +55 -0
- package/lib/types/client/icons.d.ts +18 -0
- package/lib/types/client/index.d.ts +35 -0
- package/lib/types/client/locales.d.ts +42 -0
- package/lib/types/client/pair-token.d.ts +73 -0
- package/lib/types/client/semantic.d.ts +28 -0
- package/lib/types/core/approvals.d.ts +75 -0
- package/lib/types/core/connection.d.ts +119 -0
- package/lib/types/core/crypto.d.ts +48 -0
- package/lib/types/core/devices.d.ts +72 -0
- package/lib/types/core/dsh-frames.d.ts +181 -0
- package/lib/types/core/envelope.d.ts +98 -0
- package/lib/types/core/gateway-errors.d.ts +49 -0
- package/lib/types/core/hub.d.ts +101 -0
- package/lib/types/core/loopback.d.ts +35 -0
- package/lib/types/core/owner-peer.d.ts +49 -0
- package/lib/types/core/pairing.d.ts +61 -0
- package/lib/types/core/protocol.d.ts +89 -0
- package/lib/types/core/questions.d.ts +246 -0
- package/lib/types/core/session-meta.d.ts +105 -0
- package/lib/types/core/session-permissions.d.ts +134 -0
- package/lib/types/core/wire.d.ts +115 -0
- package/lib/types/core/ws-frames.d.ts +61 -0
- package/lib/types/host/dsh-home.d.ts +16 -0
- package/lib/types/host/pair-origin.d.ts +44 -0
- package/lib/types/host/routes.d.ts +27 -0
- package/lib/types/host/websocket.d.ts +104 -0
- package/lib/types/index.d.ts +40 -0
- package/package.json +87 -3
- package/src/client/PairEntry.tsx +151 -0
- package/src/client/PairPanel.tsx +183 -0
- package/src/client/icons.tsx +72 -0
- package/src/client/index.ts +81 -0
- package/src/client/locales.ts +72 -0
- package/src/client/pair-token.ts +104 -0
- package/src/client/panel.module.css +344 -0
- package/src/client/semantic.ts +37 -0
- package/src/core/approvals.ts +142 -0
- package/src/core/connection.ts +1090 -0
- package/src/core/crypto.ts +113 -0
- package/src/core/devices.ts +209 -0
- package/src/core/dsh-frames.ts +486 -0
- package/src/core/envelope.ts +173 -0
- package/src/core/gateway-errors.ts +92 -0
- package/src/core/hub.ts +238 -0
- package/src/core/loopback.ts +60 -0
- package/src/core/owner-peer.ts +70 -0
- package/src/core/pairing.ts +113 -0
- package/src/core/protocol.ts +120 -0
- package/src/core/questions.ts +454 -0
- package/src/core/session-meta.ts +176 -0
- package/src/core/session-permissions.ts +266 -0
- package/src/core/wire.ts +155 -0
- package/src/core/ws-frames.ts +125 -0
- package/src/css-modules.d.ts +6 -0
- package/src/host/dsh-home.ts +42 -0
- package/src/host/pair-origin.ts +77 -0
- package/src/host/routes.ts +85 -0
- package/src/host/websocket.ts +325 -0
- package/src/index.ts +205 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The public origin a phone should dial, discovered rather than asked for.
|
|
3
|
+
*
|
|
4
|
+
* A pairing code is far better when it carries the address too: the phone then
|
|
5
|
+
* needs one scan and no typing. This module is where that address comes from,
|
|
6
|
+
* in the order the family already trusts:
|
|
7
|
+
*
|
|
8
|
+
* 1. `DSH_REMOTE_PUBLIC_BASE_URL` — the same environment contract
|
|
9
|
+
* `dsh-remote-web-ui` reads for its own public base.
|
|
10
|
+
* 2. The relay registry row `$DSH_HOME/remote-web-ui-registry/<profile>.json`,
|
|
11
|
+
* whose `id` is the stable `<id>.<relay>` subdomain that plugin registers
|
|
12
|
+
* for this profile (see its `relay-registry.ts`). Reading that row is a
|
|
13
|
+
* deliberate, documented data dependency: it is one small JSON file per
|
|
14
|
+
* profile, it is written by the machine itself, and a missing or unreadable
|
|
15
|
+
* row simply means "no origin known" rather than an error.
|
|
16
|
+
*
|
|
17
|
+
* Nothing here reaches the network, and nothing here mints or spends anything.
|
|
18
|
+
* @module dsh-remote-bridge/host/pair-origin
|
|
19
|
+
*/
|
|
20
|
+
/** The relay's worker domain; a profile's stable origin is `<id>.<this>`. */
|
|
21
|
+
export declare const RELAY_DOMAIN = "dsh-market.com";
|
|
22
|
+
/** The registry directory `dsh-remote-web-ui` keeps its per-profile row in. */
|
|
23
|
+
export declare const RELAY_REGISTRY_DIR = "remote-web-ui-registry";
|
|
24
|
+
/** One discovered origin and the fact that produced it. */
|
|
25
|
+
export interface PairOriginFacts {
|
|
26
|
+
/** Origin only, no path: `wss://<id>.dsh-market.com`. */
|
|
27
|
+
readonly origin: string;
|
|
28
|
+
/** Where the answer came from, for the panel and the log. */
|
|
29
|
+
readonly source: 'env' | 'registry';
|
|
30
|
+
}
|
|
31
|
+
export interface DiscoverPairOriginOptions {
|
|
32
|
+
/** Environment to read; defaults to `process.env`. */
|
|
33
|
+
readonly env?: NodeJS.ProcessEnv;
|
|
34
|
+
/** DSH home to look in; defaults to `resolveDshHome(env)`. */
|
|
35
|
+
readonly home?: string;
|
|
36
|
+
/** Profile whose registry row to read; defaults to `$DSH_PROFILE` or `desktop`. */
|
|
37
|
+
readonly profile?: string;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Discover the public origin of this instance.
|
|
41
|
+
* @param options - environment, home and profile seams for tests.
|
|
42
|
+
* @returns the origin and its source, or `undefined` when nothing is known.
|
|
43
|
+
*/
|
|
44
|
+
export declare function discoverPairOrigin(options?: DiscoverPairOriginOptions): PairOriginFacts | undefined;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The bootstrap token endpoint an operator reaches from the Host itself.
|
|
3
|
+
*
|
|
4
|
+
* It is a named route on the host web server at one exact path, and it is the
|
|
5
|
+
* only place a bootstrap token is ever minted. Admission is the two-condition
|
|
6
|
+
* loopback gate in `core/loopback.ts` (socket address AND Host header); the
|
|
7
|
+
* token itself is one-shot with a 30 s lifetime, and the same link is printed
|
|
8
|
+
* to the Host log so an operator can open it from the machine.
|
|
9
|
+
* @module dsh-remote-bridge/host/routes
|
|
10
|
+
*/
|
|
11
|
+
import type { WebRoute } from '@deepseek-ai/dsh-host-webserver';
|
|
12
|
+
import type { PairingService } from '../core/pairing.ts';
|
|
13
|
+
import { type PairOriginFacts } from './pair-origin.ts';
|
|
14
|
+
export interface PairTokenRouteDeps {
|
|
15
|
+
readonly pairing: PairingService;
|
|
16
|
+
readonly log: (message: string) => void;
|
|
17
|
+
/** Origin discovery seam; defaults to {@link discoverPairOrigin}. */
|
|
18
|
+
readonly pairOrigin?: () => PairOriginFacts | undefined;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Build the handler for `GET /remote-bridge/pair/token`.
|
|
22
|
+
* @param deps - the pairing service that mints the token, plus the Host log.
|
|
23
|
+
* @returns the handler; the caller owns route registration.
|
|
24
|
+
*/
|
|
25
|
+
export declare function makePairTokenHandler(deps: PairTokenRouteDeps): WebRoute['handler'];
|
|
26
|
+
/** The route as the host web server takes it: one exact path, no trailing slash. */
|
|
27
|
+
export declare function makePairTokenRoute(deps: PairTokenRouteDeps): WebRoute;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The WebSocket carrier of `/ios/v1`: RFC 6455 negotiation plus the frame loop.
|
|
3
|
+
*
|
|
4
|
+
* This is the only WebSocket in the family that terminates the protocol
|
|
5
|
+
* itself — the other remote plugin proxies its upgrades to the host mux — so
|
|
6
|
+
* the framing rules are implemented here against `core/ws-frames.ts` and every
|
|
7
|
+
* socket failure funnels into one teardown path that the hub observes exactly
|
|
8
|
+
* once.
|
|
9
|
+
* @module dsh-remote-bridge/host/websocket
|
|
10
|
+
*/
|
|
11
|
+
import type { IncomingMessage } from 'node:http';
|
|
12
|
+
import type { Duplex } from 'node:stream';
|
|
13
|
+
import type { BridgeTransport, ConnectionAuth } from '../core/connection.ts';
|
|
14
|
+
import type { BridgeHub } from '../core/hub.ts';
|
|
15
|
+
/**
|
|
16
|
+
* Messages one carrier buffers while no handler is registered. The mounted
|
|
17
|
+
* wiring registers one synchronously, so this only ever absorbs bytes that were
|
|
18
|
+
* handed over before the handler existed; past it the socket is closed instead of
|
|
19
|
+
* buffering a conversation nobody is reading.
|
|
20
|
+
*/
|
|
21
|
+
export declare const PENDING_MESSAGE_LIMIT = 32;
|
|
22
|
+
/** @returns whether this request asks for a WebSocket upgrade. */
|
|
23
|
+
export declare function isWebSocketUpgrade(req: IncomingMessage): boolean;
|
|
24
|
+
/** @returns the client's `Sec-WebSocket-Key`, or undefined when the handshake is incomplete. */
|
|
25
|
+
export declare function readSecWebSocketKey(req: IncomingMessage): string | undefined;
|
|
26
|
+
/** @returns the device token on `Authorization: Bearer …`, if the client sent one. */
|
|
27
|
+
export declare function bearerTokenOf(req: IncomingMessage): string | undefined;
|
|
28
|
+
/**
|
|
29
|
+
* @returns the token on `?token=…`, the fallback for clients whose handshake
|
|
30
|
+
* cannot set a header (share sheets, `wscat`, plain scripts).
|
|
31
|
+
*/
|
|
32
|
+
export declare function queryTokenOf(requestUrl: string | undefined): string | undefined;
|
|
33
|
+
/**
|
|
34
|
+
* @returns the token this upgrade presents: the header first, then the query.
|
|
35
|
+
* A bootstrap token presented here is simply not a device token, which leaves
|
|
36
|
+
* the connection unpaired — the state `pair.begin` needs.
|
|
37
|
+
*/
|
|
38
|
+
export declare function presentedTokenOf(req: IncomingMessage): string | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* Answer an upgrade this endpoint will not take, and close the write side.
|
|
41
|
+
*
|
|
42
|
+
* The refusal rides `end()`, which flushes the response before the FIN. Writing
|
|
43
|
+
* and then destroying in the same tick aborts the in-flight write on Linux, and
|
|
44
|
+
* that abort surfaces as the socket's own `error` event - which nothing on a
|
|
45
|
+
* refused socket listens for, because the carrier's subscriptions only exist
|
|
46
|
+
* once an upgrade is accepted. One malformed upgrade therefore reached the host
|
|
47
|
+
* as an uncaught exception. The listener absorbs the transport's own failures: a
|
|
48
|
+
* peer that vanished mid-refusal is the client's problem, never the host's.
|
|
49
|
+
*/
|
|
50
|
+
export declare function refuseUpgrade(socket: Duplex, status: number, reason: string): void;
|
|
51
|
+
/**
|
|
52
|
+
* One accepted upgrade handed to the bridge.
|
|
53
|
+
*/
|
|
54
|
+
export interface UpgradeHandoff {
|
|
55
|
+
/** The upgraded socket; the carrier both reads from and writes to it. */
|
|
56
|
+
readonly socket: Duplex;
|
|
57
|
+
/** The client's `Sec-WebSocket-Key`, already read from the handshake. */
|
|
58
|
+
readonly secWebSocketKey: string;
|
|
59
|
+
/** Bytes that arrived with the upgrade request itself. */
|
|
60
|
+
readonly head: Buffer;
|
|
61
|
+
readonly hub: BridgeHub;
|
|
62
|
+
/** The device the handshake token proved, when it proved one. */
|
|
63
|
+
readonly auth?: ConnectionAuth;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Hand one accepted upgrade to the hub, in the one order that is correct.
|
|
67
|
+
*
|
|
68
|
+
* Every step here is synchronous, which is what makes the carrier's own
|
|
69
|
+
* pre-handler buffer unnecessary in practice — and the buffer is there anyway,
|
|
70
|
+
* so the bytes of `head` (and anything that beats the handler) are delivered
|
|
71
|
+
* instead of dropped. The order is the point of this function: reading the
|
|
72
|
+
* socket without a handler, or attaching a connection whose carrier is never
|
|
73
|
+
* written to, both end in a client that waits forever.
|
|
74
|
+
* @param handoff - the socket, its handshake facts, and the hub to join.
|
|
75
|
+
* @returns the carrier, for a caller that needs to observe it.
|
|
76
|
+
*/
|
|
77
|
+
export declare function serveUpgrade(handoff: UpgradeHandoff): WebSocketCarrier;
|
|
78
|
+
/**
|
|
79
|
+
* One accepted socket, framed.
|
|
80
|
+
*
|
|
81
|
+
* Frames are buffered until a whole one has arrived; a fragmented message is
|
|
82
|
+
* reassembled up to {@link MAX_FRAME_BYTES} and anything larger closes the
|
|
83
|
+
* socket with the code that names the violation. The carrier is inert until
|
|
84
|
+
* {@link handshake} runs, so a caller can refuse a connection without having
|
|
85
|
+
* written a byte of the protocol.
|
|
86
|
+
*/
|
|
87
|
+
export declare class WebSocketCarrier implements BridgeTransport {
|
|
88
|
+
#private;
|
|
89
|
+
constructor(socket: Duplex);
|
|
90
|
+
/** Write the 101 handshake carrying the accept value for the client's key. */
|
|
91
|
+
handshake(secWebSocketKey: string): void;
|
|
92
|
+
/** Register the handler for every complete text message. */
|
|
93
|
+
onMessage(handler: (text: string) => void): void;
|
|
94
|
+
/** Register the one teardown hook; it runs exactly once. */
|
|
95
|
+
onClose(handler: () => void): void;
|
|
96
|
+
/** Feed raw bytes from the socket into the frame loop. */
|
|
97
|
+
feed(chunk: Buffer): void;
|
|
98
|
+
/** Send one text frame. */
|
|
99
|
+
send(text: string): void;
|
|
100
|
+
/** Send a close frame and end the socket. Idempotent. */
|
|
101
|
+
close(code: number, reason: string): void;
|
|
102
|
+
/** @returns whether the carrier has been torn down. */
|
|
103
|
+
get closed(): boolean;
|
|
104
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@linxin666/dsh-remote-bridge` — the Host half of the DSH remote bridge.
|
|
3
|
+
*
|
|
4
|
+
* One plugin row exposes the frozen protocol v1 to a paired remote client:
|
|
5
|
+
*
|
|
6
|
+
* - `/ios/v1` (WebSocket upgrade) carries the whole protocol. Admission is
|
|
7
|
+
* unconditional: a connection without a valid device token is UNPAIRED and
|
|
8
|
+
* may only call `hello`, `pair.begin` and `pair.finish` — even from loopback.
|
|
9
|
+
* - `/remote-bridge/pair/token` (exact HTTP route) mints the one-shot bootstrap
|
|
10
|
+
* token. It is admitted only when the request arrives from this machine AND
|
|
11
|
+
* its `Host` header names this machine: a tunneled request reaches the origin
|
|
12
|
+
* from loopback too, so the socket address alone is not a loopback proof.
|
|
13
|
+
* - `approval/request` is claimed in-process while a paired device is connected,
|
|
14
|
+
* and delegated (`next()`) in every other case, which the Host normalizes to
|
|
15
|
+
* the fail-closed `'unavailable'`.
|
|
16
|
+
* - `user-questions/request` is claimed the same way and published as
|
|
17
|
+
* `question.request` to the devices that declared `questions.v1`; their
|
|
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.
|
|
21
|
+
*
|
|
22
|
+
* The browser half lives in `src/client/` and is declared by `dsh.client`: one
|
|
23
|
+
* sidebar-foot panel that reads the same loopback-gated token route over a
|
|
24
|
+
* same-origin request and renders the secret as a QR symbol beside its
|
|
25
|
+
* plaintext. It is display-only — it cannot mint, spend, list or revoke
|
|
26
|
+
* anything — and this module does not import it.
|
|
27
|
+
* @module @linxin666/dsh-remote-bridge
|
|
28
|
+
*/
|
|
29
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
30
|
+
/** Loader row identity of this plugin. */
|
|
31
|
+
export declare const name = "remote-bridge";
|
|
32
|
+
/** The Host services this plugin cannot run without. */
|
|
33
|
+
export declare const inject: string[];
|
|
34
|
+
/** Where the paired-device roster lives inside the DSH home. */
|
|
35
|
+
export declare const ROSTER_PATH = "remote-bridge/devices.json";
|
|
36
|
+
/**
|
|
37
|
+
* Mount the bridge.
|
|
38
|
+
* @param ctx - the plugin's context, carrying the gateway and web server.
|
|
39
|
+
*/
|
|
40
|
+
export declare function apply(ctx: Context): void;
|
package/package.json
CHANGED
|
@@ -1,6 +1,90 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@linxin666/dsh-remote-bridge",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
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.7",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": "^22.19.0 || >=24.0.0"
|
|
8
|
+
},
|
|
9
|
+
"main": "lib/index.js",
|
|
10
|
+
"types": "lib/types/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./lib/types/index.d.ts",
|
|
14
|
+
"default": "./lib/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./client": {
|
|
17
|
+
"types": "./lib/types/client/index.d.ts",
|
|
18
|
+
"default": "./lib/client.js"
|
|
19
|
+
},
|
|
20
|
+
"./src/*": "./src/*",
|
|
21
|
+
"./package.json": "./package.json"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"lib/**/*.js",
|
|
25
|
+
"lib/**/*.js.map",
|
|
26
|
+
"lib/**/*.d.ts",
|
|
27
|
+
"lib/**/*.d.ts.map",
|
|
28
|
+
"src",
|
|
29
|
+
"cordis.patch.yml"
|
|
30
|
+
],
|
|
31
|
+
"license": "Apache-2.0",
|
|
32
|
+
"dsh": {
|
|
33
|
+
"engines": {
|
|
34
|
+
"dsh": ">=0.2.1-rc.2"
|
|
35
|
+
},
|
|
36
|
+
"bundle": {
|
|
37
|
+
"patch": "./cordis.patch.yml"
|
|
38
|
+
},
|
|
39
|
+
"client": {
|
|
40
|
+
"inject": [
|
|
41
|
+
"@deepseek-ai/dsh-client-locale",
|
|
42
|
+
"@deepseek-ai/dsh-client-ui-renderer",
|
|
43
|
+
"@deepseek-ai/dsh-client-ui-sidebar"
|
|
44
|
+
],
|
|
45
|
+
"platform": "web"
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"qrcode.react": "^4.2.0"
|
|
50
|
+
},
|
|
51
|
+
"peerDependencies": {
|
|
52
|
+
"@deepseek-ai/dsh": ">=0.2.1-rc.2",
|
|
53
|
+
"react": "^18.2.0",
|
|
54
|
+
"react-dom": "^18.2.0"
|
|
55
|
+
},
|
|
56
|
+
"devDependencies": {
|
|
57
|
+
"@deepseek-ai/cordis": "^4.0.5",
|
|
58
|
+
"@deepseek-ai/dsh-api-gateway": "^0.2.1-rc.2",
|
|
59
|
+
"@deepseek-ai/dsh-client-locale": "^0.2.1-rc.2",
|
|
60
|
+
"@deepseek-ai/dsh-client-ui-primitives": "^0.2.1-rc.2",
|
|
61
|
+
"@deepseek-ai/dsh-client-ui-renderer": "^0.2.1-rc.2",
|
|
62
|
+
"@deepseek-ai/dsh-client-ui-sidebar": "^0.2.1-rc.2",
|
|
63
|
+
"@deepseek-ai/dsh-client-ui-slots": "^0.2.1-rc.2",
|
|
64
|
+
"@deepseek-ai/dsh-host-webserver": "^0.2.1-rc.2",
|
|
65
|
+
"@deepseek-ai/dsh-session": "^0.2.1-rc.2",
|
|
66
|
+
"@deepseek-ai/dsh-typert-protocol": "^0.2.1-rc.2",
|
|
67
|
+
"@deepseek-ai/dsh-user-approval": "^0.2.1-rc.2",
|
|
68
|
+
"@testing-library/dom": "^10.4.1",
|
|
69
|
+
"@testing-library/react": "^16.3.2",
|
|
70
|
+
"@types/node": "^22.20.0",
|
|
71
|
+
"@types/react": "~18.3.1",
|
|
72
|
+
"@types/react-dom": "~18.3.0",
|
|
73
|
+
"jsdom": "29.1.1",
|
|
74
|
+
"react": "^18.2.0",
|
|
75
|
+
"react-dom": "^18.2.0",
|
|
76
|
+
"tsdown": "^0.22.2",
|
|
77
|
+
"typescript": "^6.0.3",
|
|
78
|
+
"vitest": "^4.1.8"
|
|
79
|
+
},
|
|
80
|
+
"repository": {
|
|
81
|
+
"type": "git",
|
|
82
|
+
"url": "https://github.com/zhu1090093659/dsh-web.git"
|
|
83
|
+
},
|
|
84
|
+
"scripts": {
|
|
85
|
+
"build": "tsc -b tsconfig.build.json && tsdown",
|
|
86
|
+
"watch": "tsdown --watch",
|
|
87
|
+
"test": "vitest run",
|
|
88
|
+
"typecheck": "tsgo --noEmit && tsgo -p tsconfig.test.json --pretty false"
|
|
89
|
+
}
|
|
6
90
|
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sidebar footer entry for the protocol v1 pairing panel: the icon-only trigger
|
|
3
|
+
* beside the sidebar settings seat (wide row / rail circle) that opens the
|
|
4
|
+
* panel. Registered into the official `sidebar.footer.action` list slot
|
|
5
|
+
* (declared by ui-sidebar — the same seat dsh-remote-web-ui and dsh-update
|
|
6
|
+
* share).
|
|
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.
|
|
13
|
+
*/
|
|
14
|
+
import { useCallback, useEffect, useRef, useState } from 'react'
|
|
15
|
+
import { createPortal } from 'react-dom'
|
|
16
|
+
import type { PropsLocale } from '@deepseek-ai/dsh-client-ui-slots'
|
|
17
|
+
import { PairPanel, type PairCopyState, type PairPanelState } from './PairPanel.tsx'
|
|
18
|
+
import type { PairTokenOutcome } from './pair-token.ts'
|
|
19
|
+
import { PART_ENTRY, PLUGIN_ATTR } from './semantic.ts'
|
|
20
|
+
import { QrIcon } from './icons.tsx'
|
|
21
|
+
import css from './panel.module.css'
|
|
22
|
+
|
|
23
|
+
/** How long the transient "copied" / "copy failed" label stays up. */
|
|
24
|
+
const COPY_FEEDBACK_MS = 1_500
|
|
25
|
+
/** The countdown's tick; one second is the resolution the panel shows. */
|
|
26
|
+
const TICK_MS = 1_000
|
|
27
|
+
|
|
28
|
+
export type PairEntryProps = PropsLocale<'remote-bridge'> & {
|
|
29
|
+
/** Whether the sidebar renders wide content (false = 56px rail). */
|
|
30
|
+
wide: boolean
|
|
31
|
+
/** Reads a fresh one-shot token from this package's own Host route. */
|
|
32
|
+
read: () => Promise<PairTokenOutcome>
|
|
33
|
+
/** Puts the token on the OS clipboard; false means the write was refused. */
|
|
34
|
+
copy: (text: string) => Promise<boolean>
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Map a read outcome onto the panel's view state. */
|
|
38
|
+
function panelStateOf(outcome: PairTokenOutcome, issuedAt: number): PairPanelState {
|
|
39
|
+
switch (outcome.kind) {
|
|
40
|
+
case 'ok':
|
|
41
|
+
return {
|
|
42
|
+
kind: 'ready',
|
|
43
|
+
token: outcome.token,
|
|
44
|
+
issuedAt,
|
|
45
|
+
expiresAt: outcome.expiresAt,
|
|
46
|
+
origin: outcome.origin,
|
|
47
|
+
pairingUrl: outcome.pairingUrl,
|
|
48
|
+
}
|
|
49
|
+
case 'forbidden': return { kind: 'forbidden' }
|
|
50
|
+
case 'unreachable': return { kind: 'unreachable' }
|
|
51
|
+
case 'failed': return { kind: 'failed', status: outcome.status }
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Render the pairing seat trigger and, when open, the panel.
|
|
57
|
+
* @param props - the footer seat's column state, the injected actions and the locale seat.
|
|
58
|
+
* @returns the trigger and the panel portal.
|
|
59
|
+
*/
|
|
60
|
+
export function PairEntry({ wide, read, copy, t }: PairEntryProps) {
|
|
61
|
+
const [open, setOpen] = useState(false)
|
|
62
|
+
const [state, setState] = useState<PairPanelState>({ kind: 'reading' })
|
|
63
|
+
const [copyState, setCopyState] = useState<PairCopyState>('idle')
|
|
64
|
+
const [now, setNow] = useState<number>(() => Date.now())
|
|
65
|
+
// Generation counter: closing (or minting again) invalidates a read that is
|
|
66
|
+
// still in flight, so an answer that lands after the panel closed cannot
|
|
67
|
+
// resurrect a code nobody asked for.
|
|
68
|
+
const generation = useRef(0)
|
|
69
|
+
|
|
70
|
+
const mint = useCallback((): void => {
|
|
71
|
+
const request = ++generation.current
|
|
72
|
+
setState({ kind: 'reading' })
|
|
73
|
+
void read().then((outcome) => {
|
|
74
|
+
if (request !== generation.current) return
|
|
75
|
+
const issuedAt = Date.now()
|
|
76
|
+
setNow(issuedAt)
|
|
77
|
+
setState(panelStateOf(outcome, issuedAt))
|
|
78
|
+
})
|
|
79
|
+
}, [read])
|
|
80
|
+
|
|
81
|
+
const openPanel = useCallback((): void => {
|
|
82
|
+
setOpen(true)
|
|
83
|
+
setCopyState('idle')
|
|
84
|
+
mint()
|
|
85
|
+
}, [mint])
|
|
86
|
+
|
|
87
|
+
const closePanel = useCallback((): void => {
|
|
88
|
+
generation.current += 1
|
|
89
|
+
setOpen(false)
|
|
90
|
+
}, [])
|
|
91
|
+
|
|
92
|
+
// One ticker per live code; it clears itself at the deadline, so a panel left
|
|
93
|
+
// open on an expired code stops re-rendering instead of ticking forever.
|
|
94
|
+
useEffect(() => {
|
|
95
|
+
if (!open || state.kind !== 'ready') return
|
|
96
|
+
const deadline = state.expiresAt
|
|
97
|
+
const timer = window.setInterval(() => {
|
|
98
|
+
const current = Date.now()
|
|
99
|
+
setNow(current)
|
|
100
|
+
if (current >= deadline) window.clearInterval(timer)
|
|
101
|
+
}, TICK_MS)
|
|
102
|
+
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])
|
|
113
|
+
|
|
114
|
+
const handleCopy = useCallback((token: string): void => {
|
|
115
|
+
void copy(token).then((ok) => {
|
|
116
|
+
setCopyState(ok ? 'copied' : 'failed')
|
|
117
|
+
window.setTimeout(() => { setCopyState('idle') }, COPY_FEEDBACK_MS)
|
|
118
|
+
})
|
|
119
|
+
}, [copy])
|
|
120
|
+
|
|
121
|
+
return (
|
|
122
|
+
<>
|
|
123
|
+
<button
|
|
124
|
+
type="button"
|
|
125
|
+
className={css.trigger}
|
|
126
|
+
data-dsh-plugin={PLUGIN_ATTR}
|
|
127
|
+
data-dsh-part={PART_ENTRY}
|
|
128
|
+
data-wide={wide ? 'wide' : 'rail'}
|
|
129
|
+
aria-label={t('entry.label')}
|
|
130
|
+
aria-expanded={open}
|
|
131
|
+
title={t('entry.label')}
|
|
132
|
+
onClick={openPanel}
|
|
133
|
+
>
|
|
134
|
+
<QrIcon />
|
|
135
|
+
</button>
|
|
136
|
+
{open && createPortal(
|
|
137
|
+
<PairPanel
|
|
138
|
+
t={t}
|
|
139
|
+
state={state}
|
|
140
|
+
now={now}
|
|
141
|
+
host={window.location.host}
|
|
142
|
+
copy={copyState}
|
|
143
|
+
onClose={closePanel}
|
|
144
|
+
onRefresh={mint}
|
|
145
|
+
onCopy={handleCopy}
|
|
146
|
+
/>,
|
|
147
|
+
document.body,
|
|
148
|
+
)}
|
|
149
|
+
</>
|
|
150
|
+
)
|
|
151
|
+
}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The protocol v1 pairing panel body: the QR symbol, the same secret in
|
|
3
|
+
* plaintext, the 30 s countdown with its expiry state, the refresh action, and
|
|
4
|
+
* the two facts the reader must not miss — the phone's origin is the public
|
|
5
|
+
* relay (never this machine's loopback address), and the code is one-shot.
|
|
6
|
+
*
|
|
7
|
+
* Pure presentation: every state and action arrives through props, and the
|
|
8
|
+
* clock is a prop, so the expiry flip is deterministic instead of read from
|
|
9
|
+
* `Date.now()` here.
|
|
10
|
+
*/
|
|
11
|
+
import { memo } from 'react'
|
|
12
|
+
import { QRCodeSVG } from 'qrcode.react'
|
|
13
|
+
import type { PropsLocale } from '@deepseek-ai/dsh-client-ui-slots'
|
|
14
|
+
import { CheckIcon, CloseIcon, CopyIcon, RefreshIcon } from './icons.tsx'
|
|
15
|
+
import { PART_COPY, PART_NOTICE, PART_PANEL, PART_QR, PART_REFRESH, PART_TOKEN, PLUGIN_ATTR, QR_VALUE_ATTR } from './semantic.ts'
|
|
16
|
+
import css from './panel.module.css'
|
|
17
|
+
|
|
18
|
+
/** What the panel is currently showing. */
|
|
19
|
+
export type PairPanelState =
|
|
20
|
+
/** A read is in flight. */
|
|
21
|
+
| { kind: 'reading' }
|
|
22
|
+
/**
|
|
23
|
+
* A live one-shot secret. `pairingUrl`, when the host could name its own
|
|
24
|
+
* public origin, is what the QR encodes: a `{token, url}` document the iOS
|
|
25
|
+
* client parses into both halves of its configuration, so one scan sets the
|
|
26
|
+
* address AND the token. With no origin the QR carries the bare token and the
|
|
27
|
+
* panel says the address has to be set on the phone by hand.
|
|
28
|
+
*/
|
|
29
|
+
| { kind: 'ready'; token: string; issuedAt: number; expiresAt: number; origin: string | null; pairingUrl: string | null }
|
|
30
|
+
/** 403 from the loopback gate: this page is not open on the machine itself. */
|
|
31
|
+
| { kind: 'forbidden' }
|
|
32
|
+
/** The host route answered with an unexpected status or shape. */
|
|
33
|
+
| { kind: 'failed'; status: number }
|
|
34
|
+
/** The request never reached the host. */
|
|
35
|
+
| { kind: 'unreachable' }
|
|
36
|
+
|
|
37
|
+
/** How the last copy attempt went. */
|
|
38
|
+
export type PairCopyState = 'idle' | 'copied' | 'failed'
|
|
39
|
+
|
|
40
|
+
export interface PairPanelProps {
|
|
41
|
+
t: PropsLocale<'remote-bridge'>['t']
|
|
42
|
+
state: PairPanelState
|
|
43
|
+
/** The current wall clock; the entry supplies it from a one-second ticker. */
|
|
44
|
+
now: number
|
|
45
|
+
/** This page's own `host:port`, named in the origin warning. */
|
|
46
|
+
host: string
|
|
47
|
+
copy: PairCopyState
|
|
48
|
+
onClose(): void
|
|
49
|
+
onRefresh(): void
|
|
50
|
+
onCopy(token: string): void
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The QR symbol, memoized on the secret: the countdown re-renders the panel
|
|
55
|
+
* every second, and qrcode.react rebuilds the whole module path on each render.
|
|
56
|
+
* The encoded value is the host's own `pairingUrl` verbatim when there is one —
|
|
57
|
+
* a `{token, url}` document whose `url` names THIS instance's public relay, so
|
|
58
|
+
* the phone adopts an origin that actually reaches this host. The bare-token
|
|
59
|
+
* fallback means the phone keeps whatever origin it already has.
|
|
60
|
+
*/
|
|
61
|
+
const PairQrCode = memo(function PairQrCode({ value, label }: { value: string; label: string }) {
|
|
62
|
+
return <QRCodeSVG value={value} size={184} level="M" marginSize={1} aria-label={label} />
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
/** One state notice: a title and, when there is one, the sentence explaining it. */
|
|
66
|
+
function Notice({ tone, title, body }: { tone: 'plain' | 'warning' | 'alert'; title: string; body?: string }) {
|
|
67
|
+
return (
|
|
68
|
+
<div className={css.notice} data-dsh-part={PART_NOTICE} data-tone={tone} role={tone === 'plain' ? 'status' : 'alert'}>
|
|
69
|
+
<p className={css.noticeTitle}>{title}</p>
|
|
70
|
+
{body !== undefined && <p className={css.noticeBody}>{body}</p>}
|
|
71
|
+
</div>
|
|
72
|
+
)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Render the pairing panel.
|
|
77
|
+
* @param props - locale seat, view state, clock, page host and actions.
|
|
78
|
+
* @returns the panel element tree.
|
|
79
|
+
*/
|
|
80
|
+
export function PairPanel({ t, state, now, host, copy, onClose, onRefresh, onCopy }: PairPanelProps) {
|
|
81
|
+
const remainingMs = state.kind === 'ready' ? Math.max(0, state.expiresAt - now) : 0
|
|
82
|
+
const expired = state.kind === 'ready' && remainingMs <= 0
|
|
83
|
+
const seconds = Math.ceil(remainingMs / 1000)
|
|
84
|
+
const windowMs = state.kind === 'ready' ? Math.max(1, state.expiresAt - state.issuedAt) : 1
|
|
85
|
+
const percent = Math.min(100, Math.max(0, Math.round((remainingMs / windowMs) * 100)))
|
|
86
|
+
// What the QR encodes and what the copy button hands over: the same string,
|
|
87
|
+
// so a human can paste exactly what a camera would have read.
|
|
88
|
+
const payload = state.kind === 'ready' ? (state.pairingUrl ?? state.token) : ''
|
|
89
|
+
|
|
90
|
+
return (
|
|
91
|
+
<div className={css.overlay} role="presentation">
|
|
92
|
+
<div className={css.mask} aria-hidden="true" onClick={onClose} />
|
|
93
|
+
<div
|
|
94
|
+
className={css.panel}
|
|
95
|
+
role="dialog"
|
|
96
|
+
aria-modal="true"
|
|
97
|
+
aria-label={t('title')}
|
|
98
|
+
data-dsh-plugin={PLUGIN_ATTR}
|
|
99
|
+
data-dsh-part={PART_PANEL}
|
|
100
|
+
>
|
|
101
|
+
<div className={css.header}>
|
|
102
|
+
<div className={css.heading}>
|
|
103
|
+
<h2 className={css.title}>{t('title')}</h2>
|
|
104
|
+
<p className={css.subtitle}>{t('subtitle')}</p>
|
|
105
|
+
</div>
|
|
106
|
+
<button type="button" className={css.closeButton} aria-label={t('action.close')} onClick={onClose}>
|
|
107
|
+
<CloseIcon />
|
|
108
|
+
</button>
|
|
109
|
+
</div>
|
|
110
|
+
|
|
111
|
+
{state.kind === 'reading' && <Notice tone="plain" title={t('state.reading')} />}
|
|
112
|
+
|
|
113
|
+
{state.kind === 'forbidden' && (
|
|
114
|
+
<Notice tone="alert" title={t('forbidden.title')} body={t('forbidden.body')} />
|
|
115
|
+
)}
|
|
116
|
+
|
|
117
|
+
{state.kind === 'failed' && (
|
|
118
|
+
<Notice tone="alert" title={t('failed.title')} body={t('failed.body', { status: String(state.status) })} />
|
|
119
|
+
)}
|
|
120
|
+
|
|
121
|
+
{state.kind === 'unreachable' && (
|
|
122
|
+
<Notice tone="alert" title={t('unreachable.title')} body={t('unreachable.body')} />
|
|
123
|
+
)}
|
|
124
|
+
|
|
125
|
+
{state.kind === 'ready' && (
|
|
126
|
+
<>
|
|
127
|
+
<div className={css.qrCard}>
|
|
128
|
+
<div className={css.qrWrap} data-dsh-part={PART_QR} {...{ [QR_VALUE_ATTR]: payload }}>
|
|
129
|
+
<PairQrCode value={payload} label={t('pair.qrLabel')} />
|
|
130
|
+
</div>
|
|
131
|
+
{expired ? (
|
|
132
|
+
<Notice tone="alert" title={t('pair.expired')} body={t('pair.expiredHint')} />
|
|
133
|
+
) : (
|
|
134
|
+
<div className={css.countdown}>
|
|
135
|
+
<span className={css.countdownText}>{t('pair.countdown', { s: String(seconds) })}</span>
|
|
136
|
+
<span className={css.bar}>
|
|
137
|
+
<span className={css.barFill} style={{ width: `${String(percent)}%` }} />
|
|
138
|
+
</span>
|
|
139
|
+
</div>
|
|
140
|
+
)}
|
|
141
|
+
</div>
|
|
142
|
+
|
|
143
|
+
<div className={css.tokenRow} data-dsh-part={PART_TOKEN}>
|
|
144
|
+
<span className={css.tokenLabel}>{t('pair.payloadLabel')}</span>
|
|
145
|
+
<code className={css.token}>{payload}</code>
|
|
146
|
+
</div>
|
|
147
|
+
|
|
148
|
+
<button
|
|
149
|
+
type="button"
|
|
150
|
+
className={css.copyButton}
|
|
151
|
+
data-dsh-part={PART_COPY}
|
|
152
|
+
data-copied={copy === 'copied'}
|
|
153
|
+
data-failed={copy === 'failed'}
|
|
154
|
+
onClick={() => { onCopy(payload) }}
|
|
155
|
+
>
|
|
156
|
+
{copy === 'copied' ? <CheckIcon /> : <CopyIcon />}
|
|
157
|
+
{copy === 'copied' ? t('action.copied') : copy === 'failed' ? t('action.copyFailed') : t('action.copy')}
|
|
158
|
+
</button>
|
|
159
|
+
|
|
160
|
+
{state.pairingUrl === null ? (
|
|
161
|
+
<Notice tone="warning" title={t('origin.title')} body={t('origin.body', { host })} />
|
|
162
|
+
) : (
|
|
163
|
+
<Notice
|
|
164
|
+
tone="plain"
|
|
165
|
+
title={t('origin.located.title')}
|
|
166
|
+
body={t('origin.located.body', { origin: state.origin ?? '' })}
|
|
167
|
+
/>
|
|
168
|
+
)}
|
|
169
|
+
</>
|
|
170
|
+
)}
|
|
171
|
+
|
|
172
|
+
<div className={css.actions}>
|
|
173
|
+
{state.kind !== 'reading' && state.kind !== 'forbidden' && (
|
|
174
|
+
<button type="button" className={css.refresh} data-dsh-part={PART_REFRESH} onClick={onRefresh}>
|
|
175
|
+
<RefreshIcon />
|
|
176
|
+
{t('pair.refresh')}
|
|
177
|
+
</button>
|
|
178
|
+
)}
|
|
179
|
+
</div>
|
|
180
|
+
</div>
|
|
181
|
+
</div>
|
|
182
|
+
)
|
|
183
|
+
}
|