@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.
- package/LICENSE +202 -0
- package/README.i18n.yaml +6 -0
- package/README.md +277 -2
- package/README.zh.md +276 -0
- package/cordis.patch.yml +17 -0
- package/icon.svg +3 -0
- package/lib/client.js +1830 -0
- package/lib/client.js.map +1 -0
- package/lib/index.js +5789 -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 +99 -0
- package/lib/types/core/connection.d.ts +170 -0
- package/lib/types/core/crypto.d.ts +48 -0
- package/lib/types/core/delta.d.ts +101 -0
- package/lib/types/core/devices.d.ts +72 -0
- package/lib/types/core/dsh-frames.d.ts +332 -0
- package/lib/types/core/envelope.d.ts +132 -0
- package/lib/types/core/extension.d.ts +144 -0
- package/lib/types/core/gateway-errors.d.ts +49 -0
- package/lib/types/core/hub.d.ts +127 -0
- package/lib/types/core/jobs.d.ts +204 -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 +319 -0
- package/lib/types/core/questions.d.ts +294 -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 +72 -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 +111 -0
- package/lib/types/index.d.ts +55 -0
- package/lib/types/mount-once.d.ts +55 -0
- package/locale/en.json +6 -0
- package/locale/ru.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +93 -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 +173 -0
- package/src/core/connection.ts +1906 -0
- package/src/core/crypto.ts +113 -0
- package/src/core/delta.ts +204 -0
- package/src/core/devices.ts +209 -0
- package/src/core/dsh-frames.ts +843 -0
- package/src/core/envelope.ts +219 -0
- package/src/core/extension.ts +197 -0
- package/src/core/gateway-errors.ts +92 -0
- package/src/core/hub.ts +310 -0
- package/src/core/jobs.ts +372 -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 +381 -0
- package/src/core/questions.ts +630 -0
- package/src/core/session-meta.ts +176 -0
- package/src/core/session-permissions.ts +266 -0
- package/src/core/wire.ts +159 -0
- package/src/core/ws-frames.ts +139 -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 +359 -0
- package/src/index.ts +257 -0
- package/src/mount-once.ts +142 -0
|
@@ -0,0 +1,55 @@
|
|
|
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
|
+
*
|
|
39
|
+
* Guarded by the family's single-instance rule: the aggregate row
|
|
40
|
+
* (`web-ui-remote-bridge`) and a standalone install of this same package can
|
|
41
|
+
* both be live in one profile, and the second mount would otherwise register
|
|
42
|
+
* `/ios/v1` and the token route a second time. Whichever source mounts first
|
|
43
|
+
* owns the package name; the other is queued and replayed when that owner
|
|
44
|
+
* disposes.
|
|
45
|
+
* @param ctx - the plugin's context, carrying the gateway and web server.
|
|
46
|
+
*/
|
|
47
|
+
declare function applyImpl(ctx: Context): void;
|
|
48
|
+
/**
|
|
49
|
+
* The plugin's mount point: {@link applyImpl} behind the family's
|
|
50
|
+
* single-instance guard. The name is the npm package identity, which every
|
|
51
|
+
* install source of this plugin shares — the aggregate's `web-ui-remote-bridge`
|
|
52
|
+
* row and the standalone `remote-bridge` row alike.
|
|
53
|
+
*/
|
|
54
|
+
export declare const apply: typeof applyImpl;
|
|
55
|
+
export {};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Host single-instance guard shared by the plugin family. The family bundle
|
|
3
|
+
* (dsh-web-all / dsh-skins) namespaces every child row id (web-ui-*), so
|
|
4
|
+
* the loader accepts a standalone install of the same package side by side;
|
|
5
|
+
* without this guard the second instance would still re-register the same
|
|
6
|
+
* webserver routes, tools, settings namespaces, and system-prompt sections
|
|
7
|
+
* and fail the boot. mountOnce makes a mount of an already-mounted package a
|
|
8
|
+
* no-op for as long as the first instance lives (the browser half is already
|
|
9
|
+
* deduped by package name in the client module host).
|
|
10
|
+
*
|
|
11
|
+
* A no-op is only safe while the holder is ALIVE. The holder can be disposed
|
|
12
|
+
* long after the refused mount ran its course: the Host reloads a profile by
|
|
13
|
+
* creating the new loader entries before the old ones are torn down (a
|
|
14
|
+
* plugin-manager enable/disable/install write, a settings-driven row reload,
|
|
15
|
+
* HMR), so the new aggregate shell entry mounts its family plugin while the
|
|
16
|
+
* previous entry still owns the name. Dropping that refused mount lost the
|
|
17
|
+
* plugin for good - the previous entry then disposed its own mount, releasing
|
|
18
|
+
* the name with nobody left to take it, and the family row stayed listed as
|
|
19
|
+
* active while its host routes 404ed (the task-board panel showed
|
|
20
|
+
* "board.hostError.notMounted", no degraded record appeared, and only a Host
|
|
21
|
+
* restart recovered it).
|
|
22
|
+
*
|
|
23
|
+
* The refused mount is therefore QUEUED, not dropped, and replayed the moment
|
|
24
|
+
* the holder releases the name - if the waiting fiber is still alive then. The
|
|
25
|
+
* single-instance guarantee is unchanged: exactly one mount is live per
|
|
26
|
+
* package name, and the replay re-enters the guard so a later mount still
|
|
27
|
+
* dedupes against it.
|
|
28
|
+
*
|
|
29
|
+
* The registry rides a global symbol so two module instances of the same
|
|
30
|
+
* package (npm copy vs repository link) still share one verdict. That symbol
|
|
31
|
+
* is a CROSS-REPOSITORY contract, not this file's private state: the four
|
|
32
|
+
* satellite packages (dsh-skins / dsh-pet / dsh-presets /
|
|
33
|
+
* dsh-community-plugins) are separate repositories carrying their own copy of
|
|
34
|
+
* this guard, rebuilt on their own schedule, so the value under `MOUNTED`
|
|
35
|
+
* must keep the shape every published copy reads (a `Set` of package names
|
|
36
|
+
* with `has`/`add`/`delete`). The wait queues this guard added therefore live
|
|
37
|
+
* under their own additive key, and the registry reads back a `Set` even when
|
|
38
|
+
* some other build left a different value there. Changing `MOUNTED`'s shape
|
|
39
|
+
* in place broke that contract once: a satellite's legacy copy created a
|
|
40
|
+
* `Set`, the family's new copy read it as a `Map`, and every family row
|
|
41
|
+
* mounted after it failed with "claims.get is not a function".
|
|
42
|
+
*
|
|
43
|
+
* cordis `ctx.effect` runs its callback immediately and treats the callback's
|
|
44
|
+
* return value as the fiber disposer, so the unmarker is returned, not run.
|
|
45
|
+
*/
|
|
46
|
+
/**
|
|
47
|
+
* Wrap a cordis plugin apply so the package runs at most once per process.
|
|
48
|
+
* The first mount registers normally and releases the name when its fiber
|
|
49
|
+
* disposes; a mount refused while that name is held waits for the release and
|
|
50
|
+
* then runs, unless its own fiber disposes first.
|
|
51
|
+
* @param packageName - npm package identity shared by every install source.
|
|
52
|
+
* @param fn - the original plugin apply.
|
|
53
|
+
* @returns an apply of the same shape.
|
|
54
|
+
*/
|
|
55
|
+
export declare function mountOnce<T extends (...args: any[]) => unknown>(packageName: string, fn: T): T;
|
package/locale/en.json
ADDED
package/locale/ru.json
ADDED
package/locale/zh.json
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,96 @@
|
|
|
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.8",
|
|
5
|
+
"icon": "./icon.svg",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": "^22.19.0 || >=24.0.0"
|
|
9
|
+
},
|
|
10
|
+
"main": "lib/index.js",
|
|
11
|
+
"types": "lib/types/index.d.ts",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./lib/types/index.d.ts",
|
|
15
|
+
"default": "./lib/index.js"
|
|
16
|
+
},
|
|
17
|
+
"./client": {
|
|
18
|
+
"types": "./lib/types/client/index.d.ts",
|
|
19
|
+
"default": "./lib/client.js"
|
|
20
|
+
},
|
|
21
|
+
"./src/*": "./src/*",
|
|
22
|
+
"./locale/en.json": "./locale/en.json",
|
|
23
|
+
"./locale/ru.json": "./locale/ru.json",
|
|
24
|
+
"./locale/zh.json": "./locale/zh.json",
|
|
25
|
+
"./package.json": "./package.json"
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"lib/**/*.js",
|
|
29
|
+
"lib/**/*.js.map",
|
|
30
|
+
"lib/**/*.d.ts",
|
|
31
|
+
"lib/**/*.d.ts.map",
|
|
32
|
+
"src",
|
|
33
|
+
"cordis.patch.yml",
|
|
34
|
+
"icon.svg",
|
|
35
|
+
"locale"
|
|
36
|
+
],
|
|
37
|
+
"license": "Apache-2.0",
|
|
38
|
+
"dsh": {
|
|
39
|
+
"engines": {
|
|
40
|
+
"dsh": ">=0.2.1-rc.2"
|
|
41
|
+
},
|
|
42
|
+
"bundle": {
|
|
43
|
+
"patch": "./cordis.patch.yml"
|
|
44
|
+
},
|
|
45
|
+
"client": {
|
|
46
|
+
"inject": [
|
|
47
|
+
"@deepseek-ai/dsh-client-locale",
|
|
48
|
+
"@deepseek-ai/dsh-client-ui-renderer",
|
|
49
|
+
"@deepseek-ai/dsh-client-ui-sidebar"
|
|
50
|
+
],
|
|
51
|
+
"platform": "web"
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
"dependencies": {
|
|
55
|
+
"qrcode.react": "^4.2.0"
|
|
56
|
+
},
|
|
57
|
+
"peerDependencies": {
|
|
58
|
+
"@deepseek-ai/dsh": ">=0.2.1-rc.2",
|
|
59
|
+
"react": "^18.2.0",
|
|
60
|
+
"react-dom": "^18.2.0"
|
|
61
|
+
},
|
|
62
|
+
"devDependencies": {
|
|
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",
|
|
74
|
+
"@testing-library/dom": "^10.4.1",
|
|
75
|
+
"@testing-library/react": "^16.3.2",
|
|
76
|
+
"@types/node": "^22.20.0",
|
|
77
|
+
"@types/react": "~18.3.1",
|
|
78
|
+
"@types/react-dom": "~18.3.0",
|
|
79
|
+
"jsdom": "29.1.1",
|
|
80
|
+
"react": "^18.2.0",
|
|
81
|
+
"react-dom": "^18.2.0",
|
|
82
|
+
"tsdown": "^0.22.2",
|
|
83
|
+
"typescript": "^6.0.3",
|
|
84
|
+
"vitest": "^4.1.8"
|
|
85
|
+
},
|
|
86
|
+
"repository": {
|
|
87
|
+
"type": "git",
|
|
88
|
+
"url": "https://github.com/zhu1090093659/dsh-web.git"
|
|
89
|
+
},
|
|
90
|
+
"scripts": {
|
|
91
|
+
"build": "tsc -b tsconfig.build.json && tsdown",
|
|
92
|
+
"watch": "tsdown --watch",
|
|
93
|
+
"test": "vitest run",
|
|
94
|
+
"typecheck": "tsgo --noEmit && tsgo -p tsconfig.test.json --pretty false"
|
|
95
|
+
}
|
|
6
96
|
}
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Inline SVG icons for the pairing panel (16px stroke style matching the
|
|
3
|
+
* shell's nav icons). Held here rather than imported from
|
|
4
|
+
* `@deepseek-ai/dsh-client-ui-primitives` so the panel's rendered tree carries
|
|
5
|
+
* no closure-factory bundle import: the component stays renderable under the
|
|
6
|
+
* package's own test runner.
|
|
7
|
+
*/
|
|
8
|
+
import type { SVGProps } from 'react'
|
|
9
|
+
|
|
10
|
+
/** Shared svg attrs for the 16px stroke icon family. */
|
|
11
|
+
function iconProps(props: SVGProps<SVGSVGElement>): SVGProps<SVGSVGElement> {
|
|
12
|
+
return {
|
|
13
|
+
width: 16,
|
|
14
|
+
height: 16,
|
|
15
|
+
viewBox: '0 0 16 16',
|
|
16
|
+
fill: 'none',
|
|
17
|
+
stroke: 'currentColor',
|
|
18
|
+
strokeWidth: 1.3,
|
|
19
|
+
strokeLinecap: 'round',
|
|
20
|
+
strokeLinejoin: 'round',
|
|
21
|
+
'aria-hidden': true,
|
|
22
|
+
...props,
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** A QR-symbol glyph: three finder squares plus a payload dot. */
|
|
27
|
+
export function QrIcon(props: SVGProps<SVGSVGElement>) {
|
|
28
|
+
return (
|
|
29
|
+
<svg {...iconProps(props)}>
|
|
30
|
+
<path d="M2.5 2.5h4v4h-4zM9.5 2.5h4v4h-4zM2.5 9.5h4v4h-4z" />
|
|
31
|
+
<path d="M9.5 9.5h1.6v1.6H9.5zM12.4 9.5h1.1M9.5 12.4v1.1M12 13.5h1.5v-1.5h-1.5z" />
|
|
32
|
+
</svg>
|
|
33
|
+
)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Copy glyph. */
|
|
37
|
+
export function CopyIcon(props: SVGProps<SVGSVGElement>) {
|
|
38
|
+
return (
|
|
39
|
+
<svg {...iconProps(props)}>
|
|
40
|
+
<rect x="5.5" y="5.5" width="9" height="9" rx="1.5" />
|
|
41
|
+
<path d="M2.5 10.5v-7A1.5 1.5 0 0 1 4 2h5.5" />
|
|
42
|
+
</svg>
|
|
43
|
+
)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Check glyph for the transient copied state. */
|
|
47
|
+
export function CheckIcon(props: SVGProps<SVGSVGElement>) {
|
|
48
|
+
return (
|
|
49
|
+
<svg {...iconProps(props)}>
|
|
50
|
+
<path d="M3 8.5 6.5 12l6.5-8" />
|
|
51
|
+
</svg>
|
|
52
|
+
)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Close glyph for the panel header. */
|
|
56
|
+
export function CloseIcon(props: SVGProps<SVGSVGElement>) {
|
|
57
|
+
return (
|
|
58
|
+
<svg {...iconProps(props)}>
|
|
59
|
+
<path d="M4 4l8 8M12 4l-8 8" />
|
|
60
|
+
</svg>
|
|
61
|
+
)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Refresh glyph for the mint-again action. */
|
|
65
|
+
export function RefreshIcon(props: SVGProps<SVGSVGElement>) {
|
|
66
|
+
return (
|
|
67
|
+
<svg {...iconProps(props)}>
|
|
68
|
+
<path d="M13 8a5 5 0 1 1-1.8-3.85" />
|
|
69
|
+
<path d="M13.2 2.4V5.6H10" />
|
|
70
|
+
</svg>
|
|
71
|
+
)
|
|
72
|
+
}
|