@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,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,99 @@
|
|
|
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
|
+
/**
|
|
36
|
+
* One live question as a second delivery publishes it.
|
|
37
|
+
*
|
|
38
|
+
* The same four fields the first broadcast carries, so a card a phone was shown
|
|
39
|
+
* and the same card handed to it again after a reconnect cannot be spelled
|
|
40
|
+
* differently.
|
|
41
|
+
*/
|
|
42
|
+
export interface PendingPromptView {
|
|
43
|
+
readonly id: string;
|
|
44
|
+
readonly sessionId: string;
|
|
45
|
+
readonly toolName: string;
|
|
46
|
+
readonly summary: string;
|
|
47
|
+
}
|
|
48
|
+
export interface ApprovalRegistryOptions {
|
|
49
|
+
readonly timeoutMs?: number;
|
|
50
|
+
/** Id source, injectable so a test can answer the exact id it was given. */
|
|
51
|
+
readonly random?: (byteLength: number) => string;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Pending approval questions for one bridge process.
|
|
55
|
+
*
|
|
56
|
+
* One timer per question, one `settle` for every path (answer, timeout, agent
|
|
57
|
+
* cancellation, connection loss), so a question can never be answered twice and
|
|
58
|
+
* a withdrawn question leaves no timer or abort listener behind.
|
|
59
|
+
*/
|
|
60
|
+
export declare class ApprovalRegistry {
|
|
61
|
+
#private;
|
|
62
|
+
constructor(options?: ApprovalRegistryOptions);
|
|
63
|
+
/** @returns how many questions are currently waiting for a phone. */
|
|
64
|
+
get size(): number;
|
|
65
|
+
/**
|
|
66
|
+
* Every question still waiting for a phone, in claim order.
|
|
67
|
+
*
|
|
68
|
+
* Only a live claim can be named here: {@link #settle} is the single path out
|
|
69
|
+
* of the pending map, and an answer, the bridge's own timeout, the agent's
|
|
70
|
+
* cancellation and every withdrawal all pass through it before this reads the
|
|
71
|
+
* map again. A question this bridge released therefore cannot come back to a
|
|
72
|
+
* client that reconnects.
|
|
73
|
+
* @returns the fields a second delivery republishes, oldest claim first.
|
|
74
|
+
*/
|
|
75
|
+
openPrompts(): readonly PendingPromptView[];
|
|
76
|
+
/**
|
|
77
|
+
* Claim one question.
|
|
78
|
+
* @param input - session identity, tool name, presentation summary, and the agent's own lifetime.
|
|
79
|
+
* @returns the prompt to publish, with its answer promise.
|
|
80
|
+
*/
|
|
81
|
+
open(input: ApprovalPromptInput): ApprovalPrompt;
|
|
82
|
+
/**
|
|
83
|
+
* Answer one question.
|
|
84
|
+
* @returns whether the id was still pending; a stale, expired or unknown id is refused.
|
|
85
|
+
*/
|
|
86
|
+
resolve(id: string, decision: ClaimedDecision): boolean;
|
|
87
|
+
/**
|
|
88
|
+
* Withdraw the named questions, delegating each to the next answerer.
|
|
89
|
+
* @returns how many were still pending.
|
|
90
|
+
*/
|
|
91
|
+
abandon(ids: readonly string[]): number;
|
|
92
|
+
/**
|
|
93
|
+
* Withdraw every pending question, delegating each to the next answerer.
|
|
94
|
+
* Called when the last paired phone goes away: a question nobody can answer
|
|
95
|
+
* must not hold the agent until its own timeout.
|
|
96
|
+
* @returns how many questions were withdrawn.
|
|
97
|
+
*/
|
|
98
|
+
abandonAll(): number;
|
|
99
|
+
}
|
|
@@ -0,0 +1,170 @@
|
|
|
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 { RemoteBridgeRegistry } from './extension.ts';
|
|
21
|
+
import { type JobRoster } from './jobs.ts';
|
|
22
|
+
import type { PairingService } from './pairing.ts';
|
|
23
|
+
import type { QuestionClaims } from './questions.ts';
|
|
24
|
+
import { type SessionMeta } from './session-meta.ts';
|
|
25
|
+
import type { SessionPermissions } from './session-permissions.ts';
|
|
26
|
+
import type { BridgeGateway, GatewayPeer } from './wire.ts';
|
|
27
|
+
/** Protocol-level heartbeat period: a frame is sent after this long without one. */
|
|
28
|
+
export declare const IDLE_PING_MS = 20000;
|
|
29
|
+
/**
|
|
30
|
+
* Deadline behind one liveness ping: a peer that sends no frame at all within
|
|
31
|
+
* this long of the ping is torn down.
|
|
32
|
+
*
|
|
33
|
+
* It only ever exists because a ping was sent — a connection the host has not
|
|
34
|
+
* pinged has no deadline — so a quiet client is never closed for being quiet,
|
|
35
|
+
* only for failing to answer. The measured one-way path over the relay is about
|
|
36
|
+
* 410 ms (2026-10-19), so this is more than an order of magnitude of slack.
|
|
37
|
+
*/
|
|
38
|
+
export declare const LIVENESS_TIMEOUT_MS = 10000;
|
|
39
|
+
/** How long `session.snapshot` waits for its follow stream's opening frame. */
|
|
40
|
+
export declare const SNAPSHOT_OPEN_TIMEOUT_MS = 10000;
|
|
41
|
+
/** Largest turn window one snapshot asks DSH for. */
|
|
42
|
+
export declare const SNAPSHOT_MAX_MESSAGES = 200;
|
|
43
|
+
/** Default byte budget of one `session.history` page: 512 KiB. */
|
|
44
|
+
export declare const HISTORY_PAGE_BYTES: number;
|
|
45
|
+
/**
|
|
46
|
+
* Window one `session.history.deep` read asks DSH for: at least 50 user or
|
|
47
|
+
* assistant messages across at least 2 turns, and never more than 500 messages.
|
|
48
|
+
*
|
|
49
|
+
* It is the window the harness's own Web client pages history with, and it is
|
|
50
|
+
* deliberately wider than one answer: a byte budget is the caller's, so the
|
|
51
|
+
* store is asked for a whole span once and the bridge then cuts that span to
|
|
52
|
+
* `maxBytes` by entry. Nothing in the answer depends on where the store cut:
|
|
53
|
+
* the host's `hasMore` is folded into the answer's own `hasMore`, and the
|
|
54
|
+
* cursor is always the oldest entry the answer carries.
|
|
55
|
+
*/
|
|
56
|
+
export declare const DEEP_HISTORY_WINDOW: {
|
|
57
|
+
readonly maxMessages: 500;
|
|
58
|
+
readonly turnWindow: {
|
|
59
|
+
readonly minMessages: 50;
|
|
60
|
+
readonly minTurns: 2;
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
/** Highest number of prompt receipts one connection remembers for idempotency. */
|
|
64
|
+
export declare const PROMPT_RECEIPT_LIMIT = 512;
|
|
65
|
+
/** Largest base64 text one prompt attachment may carry: 6 MiB (protocol §10.2). */
|
|
66
|
+
export declare const PROMPT_ATTACHMENT_MAX_BYTES: number;
|
|
67
|
+
/** The carrier a connection writes through. */
|
|
68
|
+
export interface BridgeTransport {
|
|
69
|
+
send(text: string): void;
|
|
70
|
+
close(code: number, reason: string): void;
|
|
71
|
+
/**
|
|
72
|
+
* Optional: whether the peer declared `frame.deflate.v1`, reported once the
|
|
73
|
+
* `hello` that declares it has been recorded. The capability is the client's
|
|
74
|
+
* declaration and the wire form is the carrier's, so this is the one fact that
|
|
75
|
+
* has to cross between them. A carrier with no socket to compress — a
|
|
76
|
+
* recording double, for one — omits the member and keeps writing text.
|
|
77
|
+
*/
|
|
78
|
+
frameDeflate?(declared: boolean): void;
|
|
79
|
+
}
|
|
80
|
+
export interface BridgeConnectionOptions {
|
|
81
|
+
readonly gateway: BridgeGateway;
|
|
82
|
+
readonly pairing: PairingService;
|
|
83
|
+
readonly roster: DeviceRoster;
|
|
84
|
+
readonly approvals: ApprovalRegistry;
|
|
85
|
+
/** Pending user questions: the claims this bridge holds and the answer path back to the host. */
|
|
86
|
+
readonly questions: QuestionClaims;
|
|
87
|
+
/** The host's background jobs: the read-only projection and the scopes this connection watches. */
|
|
88
|
+
readonly jobs: JobRoster;
|
|
89
|
+
/** Process-local session metadata: live titles and the host's archive set. */
|
|
90
|
+
readonly sessionMeta: SessionMeta;
|
|
91
|
+
/** Host-process permission presets, read and switched through their real service objects. */
|
|
92
|
+
readonly permissions: SessionPermissions;
|
|
93
|
+
/**
|
|
94
|
+
* The in-process owner peer every gateway call speaks for, minted by the
|
|
95
|
+
* bridge itself (`core/owner-peer.ts`). Dispatch refuses unpaired
|
|
96
|
+
* connections before any gateway call exists, so this peer — a
|
|
97
|
+
* full-control credential — is exercised only behind the bridge's own
|
|
98
|
+
* pairing gate, and no client frame field is ever read into it.
|
|
99
|
+
*/
|
|
100
|
+
readonly ownerPeer: GatewayPeer;
|
|
101
|
+
readonly now: () => number;
|
|
102
|
+
/** This machine's IANA time zone, sent with every prompt. */
|
|
103
|
+
readonly clientTimeZone: () => string | undefined;
|
|
104
|
+
readonly log: (message: string) => void;
|
|
105
|
+
/**
|
|
106
|
+
* Called after a client's `hello` records its capabilities, so the hub can hand
|
|
107
|
+
* it whatever it can now receive but was not there to see.
|
|
108
|
+
*/
|
|
109
|
+
readonly onDeclare?: (connection: BridgeConnection) => void;
|
|
110
|
+
readonly heartbeatMs?: number;
|
|
111
|
+
/**
|
|
112
|
+
* Methods plugins registered on the bridge's extension seam. Absent is the
|
|
113
|
+
* protocol exactly as this package defines it: every name outside
|
|
114
|
+
* `KNOWN_METHODS` answers `unknown-method`.
|
|
115
|
+
*/
|
|
116
|
+
readonly extensions?: RemoteBridgeRegistry;
|
|
117
|
+
}
|
|
118
|
+
/** A device the connection proved itself to be, resolved from the upgrade request. */
|
|
119
|
+
export interface ConnectionAuth {
|
|
120
|
+
readonly deviceId: string;
|
|
121
|
+
}
|
|
122
|
+
export declare class BridgeConnection {
|
|
123
|
+
#private;
|
|
124
|
+
constructor(options: BridgeConnectionOptions, transport: BridgeTransport, auth?: ConnectionAuth);
|
|
125
|
+
/** @returns whether this connection has proven a device token (or just enrolled). */
|
|
126
|
+
get paired(): boolean;
|
|
127
|
+
/** @returns the paired device id, or undefined while unpaired. */
|
|
128
|
+
get deviceId(): string | undefined;
|
|
129
|
+
/** @returns whether the connection has been torn down. */
|
|
130
|
+
get disposed(): boolean;
|
|
131
|
+
/**
|
|
132
|
+
* @returns this connection's identity for the extension seam. Stable for the
|
|
133
|
+
* life of the socket and never reused by a later one.
|
|
134
|
+
*/
|
|
135
|
+
get id(): string;
|
|
136
|
+
/**
|
|
137
|
+
* @param capability - one capability name this protocol defines.
|
|
138
|
+
* @returns whether this connection announced it in `hello`.
|
|
139
|
+
*/
|
|
140
|
+
declares(capability: string): boolean;
|
|
141
|
+
/** Start the idle heartbeat that keeps a quiet phone's socket alive. */
|
|
142
|
+
startHeartbeat(): void;
|
|
143
|
+
/**
|
|
144
|
+
* Handle one received text frame.
|
|
145
|
+
*
|
|
146
|
+
* This method NEVER rejects. Its caller in the mounted wiring is
|
|
147
|
+
* `void connection.handleText(text)`, so a rejection here would be dropped on
|
|
148
|
+
* the floor and the client would wait forever for an answer that is never
|
|
149
|
+
* coming — the same silent stall as a socket nobody reads. Any unexpected
|
|
150
|
+
* failure (a roster write that cannot complete, a carrier that throws) is
|
|
151
|
+
* answered as an `internal` refusal and logged.
|
|
152
|
+
* @param text - the frame body as it crossed the wire.
|
|
153
|
+
*/
|
|
154
|
+
handleText(text: string): Promise<void>;
|
|
155
|
+
/** Push one event to this connection, advancing its own sequence. */
|
|
156
|
+
emit(topic: string, payload: unknown): boolean;
|
|
157
|
+
/**
|
|
158
|
+
* Tear the connection down: stop the heartbeat, abort every follow stream it
|
|
159
|
+
* owns, and tell the carrier to go away. Idempotent, and safe to call from a
|
|
160
|
+
* socket error handler — a carrier whose socket already closed ignores it.
|
|
161
|
+
* @param close - the close frame to send instead of the bridge-unloading one.
|
|
162
|
+
* Only the liveness deadline passes it, so a client can tell a peer that
|
|
163
|
+
* stopped answering from an ordinary teardown; every other caller leaves it
|
|
164
|
+
* out and keeps the same code and reason as before.
|
|
165
|
+
*/
|
|
166
|
+
dispose(close?: {
|
|
167
|
+
readonly code: number;
|
|
168
|
+
readonly reason: string;
|
|
169
|
+
}): void;
|
|
170
|
+
}
|
|
@@ -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;
|