@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,16 @@
|
|
|
1
|
+
import type { PropsLocale } from '@deepseek-ai/dsh-client-ui-slots';
|
|
2
|
+
import type { PairTokenOutcome } from './pair-token.ts';
|
|
3
|
+
export type PairEntryProps = PropsLocale<'remote-bridge'> & {
|
|
4
|
+
/** Whether the sidebar renders wide content (false = 56px rail). */
|
|
5
|
+
wide: boolean;
|
|
6
|
+
/** Reads a fresh one-shot token from this package's own Host route. */
|
|
7
|
+
read: () => Promise<PairTokenOutcome>;
|
|
8
|
+
/** Puts the token on the OS clipboard; false means the write was refused. */
|
|
9
|
+
copy: (text: string) => Promise<boolean>;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Render the pairing seat trigger and, when open, the panel.
|
|
13
|
+
* @param props - the footer seat's column state, the injected actions and the locale seat.
|
|
14
|
+
* @returns the trigger and the panel portal.
|
|
15
|
+
*/
|
|
16
|
+
export declare function PairEntry({ wide, read, copy, t }: PairEntryProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { PropsLocale } from '@deepseek-ai/dsh-client-ui-slots';
|
|
2
|
+
/** What the panel is currently showing. */
|
|
3
|
+
export type PairPanelState =
|
|
4
|
+
/** A read is in flight. */
|
|
5
|
+
{
|
|
6
|
+
kind: 'reading';
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* A live one-shot secret. `pairingUrl`, when the host could name its own
|
|
10
|
+
* public origin, is what the QR encodes: a `{token, url}` document the iOS
|
|
11
|
+
* client parses into both halves of its configuration, so one scan sets the
|
|
12
|
+
* address AND the token. With no origin the QR carries the bare token and the
|
|
13
|
+
* panel says the address has to be set on the phone by hand.
|
|
14
|
+
*/
|
|
15
|
+
| {
|
|
16
|
+
kind: 'ready';
|
|
17
|
+
token: string;
|
|
18
|
+
issuedAt: number;
|
|
19
|
+
expiresAt: number;
|
|
20
|
+
origin: string | null;
|
|
21
|
+
pairingUrl: string | null;
|
|
22
|
+
}
|
|
23
|
+
/** 403 from the loopback gate: this page is not open on the machine itself. */
|
|
24
|
+
| {
|
|
25
|
+
kind: 'forbidden';
|
|
26
|
+
}
|
|
27
|
+
/** The host route answered with an unexpected status or shape. */
|
|
28
|
+
| {
|
|
29
|
+
kind: 'failed';
|
|
30
|
+
status: number;
|
|
31
|
+
}
|
|
32
|
+
/** The request never reached the host. */
|
|
33
|
+
| {
|
|
34
|
+
kind: 'unreachable';
|
|
35
|
+
};
|
|
36
|
+
/** How the last copy attempt went. */
|
|
37
|
+
export type PairCopyState = 'idle' | 'copied' | 'failed';
|
|
38
|
+
export interface PairPanelProps {
|
|
39
|
+
t: PropsLocale<'remote-bridge'>['t'];
|
|
40
|
+
state: PairPanelState;
|
|
41
|
+
/** The current wall clock; the entry supplies it from a one-second ticker. */
|
|
42
|
+
now: number;
|
|
43
|
+
/** This page's own `host:port`, named in the origin warning. */
|
|
44
|
+
host: string;
|
|
45
|
+
copy: PairCopyState;
|
|
46
|
+
onClose(): void;
|
|
47
|
+
onRefresh(): void;
|
|
48
|
+
onCopy(token: string): void;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Render the pairing panel.
|
|
52
|
+
* @param props - locale seat, view state, clock, page host and actions.
|
|
53
|
+
* @returns the panel element tree.
|
|
54
|
+
*/
|
|
55
|
+
export declare function PairPanel({ t, state, now, host, copy, onClose, onRefresh, onCopy }: PairPanelProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,18 @@
|
|
|
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
|
+
/** A QR-symbol glyph: three finder squares plus a payload dot. */
|
|
10
|
+
export declare function QrIcon(props: SVGProps<SVGSVGElement>): import("react").JSX.Element;
|
|
11
|
+
/** Copy glyph. */
|
|
12
|
+
export declare function CopyIcon(props: SVGProps<SVGSVGElement>): import("react").JSX.Element;
|
|
13
|
+
/** Check glyph for the transient copied state. */
|
|
14
|
+
export declare function CheckIcon(props: SVGProps<SVGSVGElement>): import("react").JSX.Element;
|
|
15
|
+
/** Close glyph for the panel header. */
|
|
16
|
+
export declare function CloseIcon(props: SVGProps<SVGSVGElement>): import("react").JSX.Element;
|
|
17
|
+
/** Refresh glyph for the mint-again action. */
|
|
18
|
+
export declare function RefreshIcon(props: SVGProps<SVGSVGElement>): import("react").JSX.Element;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Protocol v1 pairing panel — browser half. Registers the `remote-bridge`
|
|
3
|
+
* dictionary and the sidebar-foot trigger (into the ui-sidebar-declared
|
|
4
|
+
* `sidebar.footer.action` list slot) that opens the panel showing this
|
|
5
|
+
* package's own one-shot bootstrap token as a QR symbol plus its plaintext.
|
|
6
|
+
*
|
|
7
|
+
* The panel is display-only for the native path: it reads the same loopback-only
|
|
8
|
+
* `GET /remote-bridge/pair/token` route an operator would curl, and never spends
|
|
9
|
+
* the token (`pair.begin` stays the phone's call). The browser half therefore
|
|
10
|
+
* adds no wire surface — the envelope, the method names, the topic names and
|
|
11
|
+
* the events are untouched — and it does not touch `dsh-remote-web-ui`'s pairing
|
|
12
|
+
* service, its roster, or its browser cookie path.
|
|
13
|
+
*
|
|
14
|
+
* Export discipline (packages/AGENTS.md): the /client surface carries only what
|
|
15
|
+
* cordis loading needs plus types.
|
|
16
|
+
*/
|
|
17
|
+
import type { Context as ClientContext } from '@deepseek-ai/cordis';
|
|
18
|
+
import { type RemoteBridgeKey } from './locales.ts';
|
|
19
|
+
declare module '@deepseek-ai/dsh-client-ui-slots' {
|
|
20
|
+
interface LocaleNamespaceMap {
|
|
21
|
+
/** Protocol v1 pairing panel copy. */
|
|
22
|
+
'remote-bridge': RemoteBridgeKey;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/** Services required by this plugin. */
|
|
26
|
+
export declare const inject: string[];
|
|
27
|
+
/**
|
|
28
|
+
* Register the protocol v1 pairing surface.
|
|
29
|
+
* @param ctx - client root context.
|
|
30
|
+
*/
|
|
31
|
+
export declare function apply(ctx: ClientContext): void;
|
|
32
|
+
export type { PairEntryProps } from './PairEntry.tsx';
|
|
33
|
+
export type { PairCopyState, PairPanelProps, PairPanelState } from './PairPanel.tsx';
|
|
34
|
+
export type { PairTokenFetch, PairTokenOutcome, PairTokenResponse, ReadPairTokenOptions } from './pair-token.ts';
|
|
35
|
+
export type { RemoteBridgeKey } from './locales.ts';
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `remote-bridge` namespace dictionaries: the protocol v1 pairing panel copy.
|
|
3
|
+
*
|
|
4
|
+
* The panel has one job — show the operator the one-shot bootstrap token as a
|
|
5
|
+
* QR symbol plus its plaintext — so the copy is dominated by the two facts a
|
|
6
|
+
* reader has to get right: where the phone's origin points (the public relay,
|
|
7
|
+
* never this machine's loopback address) and why the token vanished (30 s, one
|
|
8
|
+
* use, or a fresh mint).
|
|
9
|
+
*/
|
|
10
|
+
/** Simplified Chinese dictionary (the key-set source of truth). */
|
|
11
|
+
export declare const zh: {
|
|
12
|
+
readonly 'entry.label': "配对手机(协议 v1)";
|
|
13
|
+
readonly title: "协议 v1 配对";
|
|
14
|
+
readonly subtitle: "用手机 App 扫描下方二维码,即可以协议 v1 配对这台宿主。配对码由本机签发,仅在本机可读。";
|
|
15
|
+
readonly 'state.reading': "正在取配对码…";
|
|
16
|
+
readonly 'pair.qrLabel': "配对二维码";
|
|
17
|
+
readonly 'pair.payloadLabel': "二维码内容(可复制后粘贴到手机)";
|
|
18
|
+
readonly 'pair.countdown': "剩余 {s} 秒";
|
|
19
|
+
readonly 'pair.expired': "配对码已过期(30 秒)";
|
|
20
|
+
readonly 'pair.expiredHint': "过期的配对码不会被手机接受,请重新取码。";
|
|
21
|
+
readonly 'pair.usedHint': "配对码一次性:手机调用 pair.begin 的瞬间即作废,重新取码也会作废上一张。";
|
|
22
|
+
readonly 'pair.refresh': "重新取码";
|
|
23
|
+
readonly 'origin.title': "没能自动定位公网地址,二维码只装裸配对码";
|
|
24
|
+
readonly 'origin.body': "这台机器既没有 DSH_REMOTE_PUBLIC_BASE_URL,也没有中继注册表记录,因此无法把地址写进二维码。请先在手机 App 里把连接地址设成你的公网中继(形如 wss://<id>.dsh-market.com),再扫上面的码;本页所在的 {host} 只有本机能连,手机连不上。";
|
|
25
|
+
readonly 'origin.located.title': "手机将直接连到公网中继(地址已写进二维码)";
|
|
26
|
+
readonly 'origin.located.body': "二维码里已经带上 {origin}:手机扫一次就同时设好连接地址与配对码,不需要手填任何东西。";
|
|
27
|
+
readonly 'forbidden.title': "这个页面不是从本机打开的";
|
|
28
|
+
readonly 'forbidden.body': "配对码只能在本机 Web UI 上取。请在本机浏览器打开 http://127.0.0.1:<端口> 后重试。";
|
|
29
|
+
readonly 'failed.title': "取配对码失败";
|
|
30
|
+
readonly 'failed.body': "宿主返回了 HTTP {status},请刷新页面后重试。";
|
|
31
|
+
readonly 'unreachable.title': "无法连接本机宿主";
|
|
32
|
+
readonly 'unreachable.body': "请求没有到达宿主,请确认 dsh web 仍在运行后重试。";
|
|
33
|
+
readonly 'action.close': "关闭";
|
|
34
|
+
readonly 'action.copy': "复制配对码";
|
|
35
|
+
readonly 'action.copied': "已复制";
|
|
36
|
+
readonly 'action.copyFailed': "复制失败";
|
|
37
|
+
readonly 'action.retry': "重试";
|
|
38
|
+
};
|
|
39
|
+
/** English dictionary, key-for-key with the zh source. */
|
|
40
|
+
export declare const en: Record<RemoteBridgeKey, string>;
|
|
41
|
+
/** Every key the `remote-bridge` namespace carries. */
|
|
42
|
+
export type RemoteBridgeKey = keyof typeof zh;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The panel's one wire call: read the one-shot bootstrap token from this
|
|
3
|
+
* package's own Host route.
|
|
4
|
+
*
|
|
5
|
+
* The route is same-origin and document-relative (the GUI is served with
|
|
6
|
+
* `<base href="./">`, so a sub-path deployment resolves it against its entry
|
|
7
|
+
* directory instead of escaping to the origin root — the same rule
|
|
8
|
+
* `dsh-remote-web-ui`'s pair API follows). It is NOT consumed here: the panel
|
|
9
|
+
* only displays the secret, and the phone spends it in `pair.begin`, so a
|
|
10
|
+
* mis-click on "new code" voids a token without ever pairing a device.
|
|
11
|
+
*
|
|
12
|
+
* `403` is a fact, not an error: the loopback gate in `core/loopback.ts`
|
|
13
|
+
* refuses any page whose socket or `Host` is not this machine, which is exactly
|
|
14
|
+
* the case the panel must name out loud.
|
|
15
|
+
* @module dsh-remote-bridge/client/pair-token
|
|
16
|
+
*/
|
|
17
|
+
/** Document-relative form of the Host route, for a page served under any base. */
|
|
18
|
+
export declare const PAIR_TOKEN_URL: string;
|
|
19
|
+
/** The outcome of one token read. */
|
|
20
|
+
export type PairTokenOutcome =
|
|
21
|
+
/**
|
|
22
|
+
* A live one-shot token and the instant it stops being accepted, plus the
|
|
23
|
+
* address half when this machine can name its own public origin. `pairingUrl`
|
|
24
|
+
* is the code a QR should carry: a `{token, url}` document the phone parses
|
|
25
|
+
* into both halves of its configuration.
|
|
26
|
+
*/
|
|
27
|
+
{
|
|
28
|
+
kind: 'ok';
|
|
29
|
+
token: string;
|
|
30
|
+
expiresAt: number;
|
|
31
|
+
origin: string | null;
|
|
32
|
+
pairingUrl: string | null;
|
|
33
|
+
}
|
|
34
|
+
/** 403: the page is not open on the machine itself, so the loopback gate refused it. */
|
|
35
|
+
| {
|
|
36
|
+
kind: 'forbidden';
|
|
37
|
+
}
|
|
38
|
+
/** The route answered with a status the panel does not know, or with a body that is not a token. */
|
|
39
|
+
| {
|
|
40
|
+
kind: 'failed';
|
|
41
|
+
status: number;
|
|
42
|
+
}
|
|
43
|
+
/** The request never reached the host (offline, host stopped, connection reset). */
|
|
44
|
+
| {
|
|
45
|
+
kind: 'unreachable';
|
|
46
|
+
};
|
|
47
|
+
/** The minimal response surface the reader uses, so a test can inject its own. */
|
|
48
|
+
export interface PairTokenResponse {
|
|
49
|
+
readonly ok: boolean;
|
|
50
|
+
readonly status: number;
|
|
51
|
+
json(): Promise<unknown>;
|
|
52
|
+
}
|
|
53
|
+
/** The minimal fetch surface the reader uses, so a test can inject its own. */
|
|
54
|
+
export type PairTokenFetch = (url: string, init: {
|
|
55
|
+
signal?: AbortSignal;
|
|
56
|
+
cache: string;
|
|
57
|
+
}) => Promise<PairTokenResponse>;
|
|
58
|
+
export interface ReadPairTokenOptions {
|
|
59
|
+
/** Injected transport; defaults to the global fetch. */
|
|
60
|
+
readonly fetchImpl?: PairTokenFetch;
|
|
61
|
+
/** Aborts an in-flight read (the panel closed, the entry unmounted). */
|
|
62
|
+
readonly signal?: AbortSignal;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Read one bootstrap token.
|
|
66
|
+
*
|
|
67
|
+
* Never throws: every failure is one of the outcome arms, because the panel has
|
|
68
|
+
* to render each of them. A `200` whose body is not `{token, expiresAt}` is
|
|
69
|
+
* reported as a failure rather than shown as an empty QR.
|
|
70
|
+
* @param options - injected transport and abort signal.
|
|
71
|
+
* @returns the read outcome.
|
|
72
|
+
*/
|
|
73
|
+
export declare function readPairToken(options?: ReadPairTokenOptions): Promise<PairTokenOutcome>;
|
|
@@ -0,0 +1,28 @@
|
|
|
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
|
+
/** Plugin short name stamped on the footer trigger and the modal root. */
|
|
8
|
+
export declare const PLUGIN_ATTR = "remote-bridge";
|
|
9
|
+
/** The sidebar footer seat trigger. */
|
|
10
|
+
export declare const PART_ENTRY = "entry";
|
|
11
|
+
/** The modal panel root. */
|
|
12
|
+
export declare const PART_PANEL = "panel";
|
|
13
|
+
/** The QR symbol's wrapper — it also mirrors the encoded string. */
|
|
14
|
+
export declare const PART_QR = "qr";
|
|
15
|
+
/** The plaintext pairing code row. */
|
|
16
|
+
export declare const PART_TOKEN = "token";
|
|
17
|
+
/** A state notice: the origin warning, or a refusal the host raised. */
|
|
18
|
+
export declare const PART_NOTICE = "notice";
|
|
19
|
+
/** The mint-again button. */
|
|
20
|
+
export declare const PART_REFRESH = "refresh";
|
|
21
|
+
/** The copy-the-code button. */
|
|
22
|
+
export declare const PART_COPY = "copy";
|
|
23
|
+
/**
|
|
24
|
+
* Attribute carrying the exact string the QR symbol encodes, so the operator
|
|
25
|
+
* (and a UI check) can compare it against the host route's answer without
|
|
26
|
+
* decoding the symbol. The same string is already on screen in plaintext.
|
|
27
|
+
*/
|
|
28
|
+
export declare const QR_VALUE_ATTR = "data-dsh-qr-value";
|
|
@@ -0,0 +1,75 @@
|
|
|
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
|
+
/** How long a claimed question waits for a phone before delegating. */
|
|
12
|
+
export declare const APPROVAL_TIMEOUT_MS = 120000;
|
|
13
|
+
/** Entropy of one question id. */
|
|
14
|
+
export declare const APPROVAL_ID_BYTES = 16;
|
|
15
|
+
/** The two decisions a phone may send back. */
|
|
16
|
+
export type ClaimedDecision = 'allowed-once' | 'rejected';
|
|
17
|
+
/** A claim's answer: a decision, or `undefined` for "the bridge withdraws, ask the next answerer". */
|
|
18
|
+
export type ApprovalAnswer = ClaimedDecision | undefined;
|
|
19
|
+
export interface ApprovalPromptInput {
|
|
20
|
+
readonly sessionId: string;
|
|
21
|
+
readonly toolName: string;
|
|
22
|
+
readonly summary: string;
|
|
23
|
+
/** Cancellation lifetime of the pending request; aborting withdraws the question. */
|
|
24
|
+
readonly signal?: AbortSignal;
|
|
25
|
+
}
|
|
26
|
+
/** One claimed question, as handed back to the bridge. */
|
|
27
|
+
export interface ApprovalPrompt {
|
|
28
|
+
readonly id: string;
|
|
29
|
+
readonly sessionId: string;
|
|
30
|
+
readonly toolName: string;
|
|
31
|
+
readonly summary: string;
|
|
32
|
+
/** Settles exactly once, with a decision or `undefined` to delegate. */
|
|
33
|
+
readonly answer: Promise<ApprovalAnswer>;
|
|
34
|
+
}
|
|
35
|
+
export interface ApprovalRegistryOptions {
|
|
36
|
+
readonly timeoutMs?: number;
|
|
37
|
+
/** Id source, injectable so a test can answer the exact id it was given. */
|
|
38
|
+
readonly random?: (byteLength: number) => string;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Pending approval questions for one bridge process.
|
|
42
|
+
*
|
|
43
|
+
* One timer per question, one `settle` for every path (answer, timeout, agent
|
|
44
|
+
* cancellation, connection loss), so a question can never be answered twice and
|
|
45
|
+
* a withdrawn question leaves no timer or abort listener behind.
|
|
46
|
+
*/
|
|
47
|
+
export declare class ApprovalRegistry {
|
|
48
|
+
#private;
|
|
49
|
+
constructor(options?: ApprovalRegistryOptions);
|
|
50
|
+
/** @returns how many questions are currently waiting for a phone. */
|
|
51
|
+
get size(): number;
|
|
52
|
+
/**
|
|
53
|
+
* Claim one question.
|
|
54
|
+
* @param input - session identity, tool name, presentation summary, and the agent's own lifetime.
|
|
55
|
+
* @returns the prompt to publish, with its answer promise.
|
|
56
|
+
*/
|
|
57
|
+
open(input: ApprovalPromptInput): ApprovalPrompt;
|
|
58
|
+
/**
|
|
59
|
+
* Answer one question.
|
|
60
|
+
* @returns whether the id was still pending; a stale, expired or unknown id is refused.
|
|
61
|
+
*/
|
|
62
|
+
resolve(id: string, decision: ClaimedDecision): boolean;
|
|
63
|
+
/**
|
|
64
|
+
* Withdraw the named questions, delegating each to the next answerer.
|
|
65
|
+
* @returns how many were still pending.
|
|
66
|
+
*/
|
|
67
|
+
abandon(ids: readonly string[]): number;
|
|
68
|
+
/**
|
|
69
|
+
* Withdraw every pending question, delegating each to the next answerer.
|
|
70
|
+
* Called when the last paired phone goes away: a question nobody can answer
|
|
71
|
+
* must not hold the agent until its own timeout.
|
|
72
|
+
* @returns how many questions were withdrawn.
|
|
73
|
+
*/
|
|
74
|
+
abandonAll(): number;
|
|
75
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One client connection's protocol v1 state machine, transport-agnostic.
|
|
3
|
+
*
|
|
4
|
+
* Everything a connected device can do lives here, and nothing about the
|
|
5
|
+
* WebSocket carrier does: the state machine writes frames through a
|
|
6
|
+
* {@link BridgeTransport}, so the tests drive a whole session — pairing,
|
|
7
|
+
* listing, snapshotting, prompting, approvals — without a socket.
|
|
8
|
+
*
|
|
9
|
+
* Two rules from the frozen design are load-bearing here:
|
|
10
|
+
* - a follow stream is opened once per session with `assistantStream: true`,
|
|
11
|
+
* and its opening snapshot frame doubles as `session.snapshot`'s answer
|
|
12
|
+
* (decided after the original §2.1/§2.2 contradiction);
|
|
13
|
+
* - every follow stream is aborted when the connection ends. A stream that
|
|
14
|
+
* outlives its connection would keep the session's process-local assistant
|
|
15
|
+
* stream alive for a phone that is gone.
|
|
16
|
+
* @module dsh-remote-bridge/core/connection
|
|
17
|
+
*/
|
|
18
|
+
import type { ApprovalRegistry } from './approvals.ts';
|
|
19
|
+
import type { DeviceRoster } from './devices.ts';
|
|
20
|
+
import type { PairingService } from './pairing.ts';
|
|
21
|
+
import type { QuestionClaims } from './questions.ts';
|
|
22
|
+
import { type SessionMeta } from './session-meta.ts';
|
|
23
|
+
import type { SessionPermissions } from './session-permissions.ts';
|
|
24
|
+
import type { BridgeGateway, GatewayPeer } from './wire.ts';
|
|
25
|
+
/** Protocol-level heartbeat period: a frame is sent after this long without one. */
|
|
26
|
+
export declare const IDLE_PING_MS = 20000;
|
|
27
|
+
/** How long `session.snapshot` waits for its follow stream's opening frame. */
|
|
28
|
+
export declare const SNAPSHOT_OPEN_TIMEOUT_MS = 10000;
|
|
29
|
+
/** Largest turn window one snapshot asks DSH for. */
|
|
30
|
+
export declare const SNAPSHOT_MAX_MESSAGES = 200;
|
|
31
|
+
/** Default byte budget of one `session.history` page: 512 KiB. */
|
|
32
|
+
export declare const HISTORY_PAGE_BYTES: number;
|
|
33
|
+
/** Highest number of prompt receipts one connection remembers for idempotency. */
|
|
34
|
+
export declare const PROMPT_RECEIPT_LIMIT = 512;
|
|
35
|
+
/** Largest base64 text one prompt attachment may carry: 6 MiB (protocol §10.2). */
|
|
36
|
+
export declare const PROMPT_ATTACHMENT_MAX_BYTES: number;
|
|
37
|
+
/** The carrier a connection writes through. */
|
|
38
|
+
export interface BridgeTransport {
|
|
39
|
+
send(text: string): void;
|
|
40
|
+
close(code: number, reason: string): void;
|
|
41
|
+
}
|
|
42
|
+
export interface BridgeConnectionOptions {
|
|
43
|
+
readonly gateway: BridgeGateway;
|
|
44
|
+
readonly pairing: PairingService;
|
|
45
|
+
readonly roster: DeviceRoster;
|
|
46
|
+
readonly approvals: ApprovalRegistry;
|
|
47
|
+
/** Pending user questions: the claims this bridge holds and the answer path back to the host. */
|
|
48
|
+
readonly questions: QuestionClaims;
|
|
49
|
+
/** Process-local session metadata: live titles and the host's archive set. */
|
|
50
|
+
readonly sessionMeta: SessionMeta;
|
|
51
|
+
/** Host-process permission presets, read and switched through their real service objects. */
|
|
52
|
+
readonly permissions: SessionPermissions;
|
|
53
|
+
/**
|
|
54
|
+
* The in-process owner peer every gateway call speaks for, minted by the
|
|
55
|
+
* bridge itself (`core/owner-peer.ts`). Dispatch refuses unpaired
|
|
56
|
+
* connections before any gateway call exists, so this peer — a
|
|
57
|
+
* full-control credential — is exercised only behind the bridge's own
|
|
58
|
+
* pairing gate, and no client frame field is ever read into it.
|
|
59
|
+
*/
|
|
60
|
+
readonly ownerPeer: GatewayPeer;
|
|
61
|
+
readonly now: () => number;
|
|
62
|
+
/** This machine's IANA time zone, sent with every prompt. */
|
|
63
|
+
readonly clientTimeZone: () => string | undefined;
|
|
64
|
+
readonly log: (message: string) => void;
|
|
65
|
+
/**
|
|
66
|
+
* Called after a client's `hello` records its capabilities, so the hub can hand
|
|
67
|
+
* it whatever it can now receive but was not there to see.
|
|
68
|
+
*/
|
|
69
|
+
readonly onDeclare?: (connection: BridgeConnection) => void;
|
|
70
|
+
readonly heartbeatMs?: number;
|
|
71
|
+
}
|
|
72
|
+
/** A device the connection proved itself to be, resolved from the upgrade request. */
|
|
73
|
+
export interface ConnectionAuth {
|
|
74
|
+
readonly deviceId: string;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Everything one connected device may do.
|
|
78
|
+
*
|
|
79
|
+
* Not thread-safe by construction but safe by awaiting: handlers are invoked
|
|
80
|
+
* one frame at a time by the carrier's reader, and the only background work is
|
|
81
|
+
* a subscription's own drain loop.
|
|
82
|
+
*/
|
|
83
|
+
export declare class BridgeConnection {
|
|
84
|
+
#private;
|
|
85
|
+
constructor(options: BridgeConnectionOptions, transport: BridgeTransport, auth?: ConnectionAuth);
|
|
86
|
+
/** @returns whether this connection has proven a device token (or just enrolled). */
|
|
87
|
+
get paired(): boolean;
|
|
88
|
+
/** @returns the paired device id, or undefined while unpaired. */
|
|
89
|
+
get deviceId(): string | undefined;
|
|
90
|
+
/** @returns whether the connection has been torn down. */
|
|
91
|
+
get disposed(): boolean;
|
|
92
|
+
/**
|
|
93
|
+
* @param capability - one capability name this protocol defines.
|
|
94
|
+
* @returns whether this connection announced it in `hello`.
|
|
95
|
+
*/
|
|
96
|
+
declares(capability: string): boolean;
|
|
97
|
+
/** Start the idle heartbeat that keeps a quiet phone's socket alive. */
|
|
98
|
+
startHeartbeat(): void;
|
|
99
|
+
/**
|
|
100
|
+
* Handle one received text frame.
|
|
101
|
+
*
|
|
102
|
+
* This method NEVER rejects. Its caller in the mounted wiring is
|
|
103
|
+
* `void connection.handleText(text)`, so a rejection here would be dropped on
|
|
104
|
+
* the floor and the client would wait forever for an answer that is never
|
|
105
|
+
* coming — the same silent stall as a socket nobody reads. Any unexpected
|
|
106
|
+
* failure (a roster write that cannot complete, a carrier that throws) is
|
|
107
|
+
* answered as an `internal` refusal and logged.
|
|
108
|
+
* @param text - the frame body as it crossed the wire.
|
|
109
|
+
*/
|
|
110
|
+
handleText(text: string): Promise<void>;
|
|
111
|
+
/** Push one event to this connection, advancing its own sequence. */
|
|
112
|
+
emit(topic: string, payload: unknown): boolean;
|
|
113
|
+
/**
|
|
114
|
+
* Tear the connection down: stop the heartbeat, abort every follow stream it
|
|
115
|
+
* owns, and tell the carrier to go away. Idempotent, and safe to call from a
|
|
116
|
+
* socket error handler — a carrier whose socket already closed ignores it.
|
|
117
|
+
*/
|
|
118
|
+
dispose(): void;
|
|
119
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pairing cryptography: base64url codecs, the SHA-256 the roster stores
|
|
3
|
+
* instead of secrets, and the ES256 (ECDSA P-256 + SHA-256) verification that
|
|
4
|
+
* admits a phone into the device roster.
|
|
5
|
+
*
|
|
6
|
+
* Nothing here is mocked away in tests: a test generates a real P-256 key pair
|
|
7
|
+
* with node:crypto and signs with it, so a broken curve check, a DER-vs-raw
|
|
8
|
+
* signature mismatch or a swapped digest fails the suite.
|
|
9
|
+
* @module dsh-remote-bridge/core/crypto
|
|
10
|
+
*/
|
|
11
|
+
/** Length of a raw (IEEE P1363, r||s) P-256 signature. */
|
|
12
|
+
export declare const ES256_SIGNATURE_BYTES = 64;
|
|
13
|
+
/** Encode bytes as unpadded base64url. */
|
|
14
|
+
export declare function base64UrlEncode(bytes: Uint8Array): string;
|
|
15
|
+
/**
|
|
16
|
+
* Decode unpadded base64url text.
|
|
17
|
+
* @returns the bytes, or undefined when the text is not base64url.
|
|
18
|
+
*/
|
|
19
|
+
export declare function base64UrlDecode(text: string): Buffer | undefined;
|
|
20
|
+
/** Lowercase hex SHA-256 of a byte string or UTF-8 text. */
|
|
21
|
+
export declare function sha256Hex(input: Uint8Array | string): string;
|
|
22
|
+
/** Cryptographically random unpadded base64url string of `byteLength` bytes. */
|
|
23
|
+
export declare function randomBase64Url(byteLength: number): string;
|
|
24
|
+
/**
|
|
25
|
+
* Compare two strings without leaking where they differ. Both sides are
|
|
26
|
+
* hashed first so the comparison is over two equal-length digests: a plain
|
|
27
|
+
* `===` on a token would return early on the first differing byte.
|
|
28
|
+
*/
|
|
29
|
+
export declare function constantTimeEqualText(left: string, right: string): boolean;
|
|
30
|
+
/** @returns the token's stored hash: what the roster keeps in place of the secret. */
|
|
31
|
+
export declare function tokenHash(token: string): string;
|
|
32
|
+
/**
|
|
33
|
+
* Fingerprint of a public key: SHA-256 over the DER of the SPKI exactly as the
|
|
34
|
+
* client sent it. It is the human-comparable identity of the device, and the
|
|
35
|
+
* roster pins it next to the key so a swapped key is visible.
|
|
36
|
+
* @returns the hex fingerprint, or undefined when the SPKI does not decode.
|
|
37
|
+
*/
|
|
38
|
+
export declare function fingerprintOfSpki(spkiBase64Url: string): string | undefined;
|
|
39
|
+
/** Whether an SPKI blob is an EC P-256 public key this bridge will verify against. */
|
|
40
|
+
export declare function isP256Spki(spkiBase64Url: string): boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Verify one ES256 signature over a message.
|
|
43
|
+
* @param spkiBase64Url - the device's base64url SPKI, P-256.
|
|
44
|
+
* @param signatureBase64Url - base64url of the raw 64-byte r||s signature.
|
|
45
|
+
* @param message - the exact bytes that were signed.
|
|
46
|
+
* @returns whether the signature is valid for that key.
|
|
47
|
+
*/
|
|
48
|
+
export declare function verifyEs256(spkiBase64Url: string, signatureBase64Url: string, message: Uint8Array): boolean;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The paired-device roster: `$DSH_HOME/remote-bridge/devices.json`.
|
|
3
|
+
*
|
|
4
|
+
* What is stored is deliberately not a credential. Each row keeps the device's
|
|
5
|
+
* public key, its fingerprint, and the SHA-256 of the bearer token it was
|
|
6
|
+
* issued — never the token itself, so a leaked roster file grants nothing. The
|
|
7
|
+
* directory is 0700 and the file 0600, written through a temporary file plus
|
|
8
|
+
* rename, and a file that cannot be parsed is moved aside instead of being
|
|
9
|
+
* silently treated as an empty roster (which would quietly un-pair every phone
|
|
10
|
+
* and then overwrite the evidence on the next write).
|
|
11
|
+
* @module dsh-remote-bridge/core/devices
|
|
12
|
+
*/
|
|
13
|
+
/** Entropy of one device bearer token. */
|
|
14
|
+
export declare const DEVICE_TOKEN_BYTES = 32;
|
|
15
|
+
/** Entropy of one device id. */
|
|
16
|
+
export declare const DEVICE_ID_BYTES = 12;
|
|
17
|
+
/** On-disk schema version of the roster file. */
|
|
18
|
+
export declare const ROSTER_VERSION = 1;
|
|
19
|
+
/** One paired device as the bridge keeps it. */
|
|
20
|
+
export interface DeviceRecord {
|
|
21
|
+
readonly deviceId: string;
|
|
22
|
+
/** base64url SPKI of the device's P-256 key. */
|
|
23
|
+
readonly pubKey: string;
|
|
24
|
+
/** SHA-256 hex of the SPKI DER: the human-comparable identity of the device. */
|
|
25
|
+
readonly fingerprint: string;
|
|
26
|
+
readonly createdAt: number;
|
|
27
|
+
readonly lastSeenAt: number;
|
|
28
|
+
}
|
|
29
|
+
/** What one successful enrollment hands back to the caller exactly once. */
|
|
30
|
+
export interface EnrollmentResult {
|
|
31
|
+
readonly deviceId: string;
|
|
32
|
+
/** The bearer token: returned to the enrolling connection and never persisted again. */
|
|
33
|
+
readonly deviceToken: string;
|
|
34
|
+
}
|
|
35
|
+
export interface DeviceRosterOptions {
|
|
36
|
+
/** Absolute path of the roster file. */
|
|
37
|
+
readonly file: string;
|
|
38
|
+
readonly now?: () => number;
|
|
39
|
+
readonly log?: (message: string) => void;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* The persisted, revocable device roster.
|
|
43
|
+
*
|
|
44
|
+
* Revocation is a file edit: protocol v1 has no revoke method, so an operator
|
|
45
|
+
* removes the row (or deletes the file) and restarts. The in-memory copy is
|
|
46
|
+
* loaded once per process.
|
|
47
|
+
*/
|
|
48
|
+
export declare class DeviceRoster {
|
|
49
|
+
#private;
|
|
50
|
+
constructor(options: DeviceRosterOptions);
|
|
51
|
+
/**
|
|
52
|
+
* Read the roster once. A missing file is an empty roster; an unreadable or
|
|
53
|
+
* malformed one is moved aside and reported, never silently zeroed.
|
|
54
|
+
* @returns when the in-memory roster reflects the file.
|
|
55
|
+
*/
|
|
56
|
+
load(): Promise<void>;
|
|
57
|
+
/** @returns every paired device, without the token hash. */
|
|
58
|
+
devices(): readonly DeviceRecord[];
|
|
59
|
+
/**
|
|
60
|
+
* Resolve a bearer token to its device.
|
|
61
|
+
* @returns the device, or undefined when the token is unknown.
|
|
62
|
+
*/
|
|
63
|
+
authenticate(token: string): DeviceRecord | undefined;
|
|
64
|
+
/**
|
|
65
|
+
* Admit one device for a public key it has already proven possession of.
|
|
66
|
+
* @param pubKey - base64url SPKI of the device's P-256 key.
|
|
67
|
+
* @returns the enrollment, or undefined when the key is not an acceptable P-256 SPKI.
|
|
68
|
+
*/
|
|
69
|
+
enroll(pubKey: string): Promise<EnrollmentResult | undefined>;
|
|
70
|
+
/** Record that a device just reconnected; the file is refreshed at most once per call. */
|
|
71
|
+
touch(deviceId: string): Promise<void>;
|
|
72
|
+
}
|