@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,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DSH session frames to protocol v1 `session.delta` payloads, plus the reverse
|
|
3
|
+
* projection of a follow baseline into `session.snapshot` turns.
|
|
4
|
+
*
|
|
5
|
+
* Only frame SHAPES are read here, never DSH classes: the frame argument is
|
|
6
|
+
* `unknown` and every field is narrowed structurally, so a cohort that adds a
|
|
7
|
+
* field to a chunk cannot break the bridge and a cohort that renames one fails
|
|
8
|
+
* loudly at the mapping instead of emitting a half-filled delta.
|
|
9
|
+
* @module dsh-remote-bridge/core/dsh-frames
|
|
10
|
+
*/
|
|
11
|
+
/** One image a durable `user/message` carried, as far as a client renders it. */
|
|
12
|
+
export interface UserMessageAttachment {
|
|
13
|
+
readonly attachmentId: string;
|
|
14
|
+
readonly mediaType: string;
|
|
15
|
+
readonly name?: string;
|
|
16
|
+
}
|
|
17
|
+
/** Token usage one finished `assistant/message` reported, passed through unconverted. */
|
|
18
|
+
export interface MessageUsage {
|
|
19
|
+
readonly inputTokens: number;
|
|
20
|
+
readonly outputTokens: number;
|
|
21
|
+
readonly totalTokens?: number;
|
|
22
|
+
readonly cacheReadTokens?: number;
|
|
23
|
+
readonly cacheWriteTokens?: number;
|
|
24
|
+
readonly reasoningTokens?: number;
|
|
25
|
+
}
|
|
26
|
+
/** One protocol v1 delta, before the session id and event sequence are attached. */
|
|
27
|
+
export type SessionDelta = {
|
|
28
|
+
readonly kind: 'text';
|
|
29
|
+
readonly text: string;
|
|
30
|
+
readonly blockIndex: number;
|
|
31
|
+
} | {
|
|
32
|
+
readonly kind: 'reasoning';
|
|
33
|
+
readonly text: string;
|
|
34
|
+
readonly blockIndex: number;
|
|
35
|
+
} | {
|
|
36
|
+
readonly kind: 'tool-call';
|
|
37
|
+
readonly callId: string;
|
|
38
|
+
readonly name: string;
|
|
39
|
+
readonly argumentsDelta: string;
|
|
40
|
+
} | {
|
|
41
|
+
readonly kind: 'tool-result';
|
|
42
|
+
readonly callId: string;
|
|
43
|
+
readonly output: string;
|
|
44
|
+
readonly isError: boolean;
|
|
45
|
+
} | {
|
|
46
|
+
readonly kind: 'turn-end';
|
|
47
|
+
readonly reason: string;
|
|
48
|
+
} | {
|
|
49
|
+
readonly kind: 'user-message';
|
|
50
|
+
readonly text: string;
|
|
51
|
+
readonly messageId?: string;
|
|
52
|
+
readonly attachments?: readonly UserMessageAttachment[];
|
|
53
|
+
} | {
|
|
54
|
+
readonly kind: 'usage';
|
|
55
|
+
readonly usage: MessageUsage;
|
|
56
|
+
readonly turn?: number;
|
|
57
|
+
readonly step?: number;
|
|
58
|
+
};
|
|
59
|
+
/** Highest number of tool calls one connection remembers by id. */
|
|
60
|
+
export declare const TOOL_CALL_MEMORY = 512;
|
|
61
|
+
/** Highest number of published user messages one connection remembers by id. */
|
|
62
|
+
export declare const USER_MESSAGE_MEMORY = 512;
|
|
63
|
+
/**
|
|
64
|
+
* The durable entry types a protocol v1 client renders — `user/message`,
|
|
65
|
+
* `assistant/message`, `tool/call` and `tool/result`. Everything else in a
|
|
66
|
+
* turn's entries is the harness's own bookkeeping, which the client can only
|
|
67
|
+
* count as unknown, so a client that asks for a trimmed baseline is not sent it.
|
|
68
|
+
*/
|
|
69
|
+
export declare const RENDERED_ENTRY_TYPES: ReadonlySet<string>;
|
|
70
|
+
/** One raw durable event inside a snapshot turn. */
|
|
71
|
+
export interface SnapshotEntry {
|
|
72
|
+
readonly seq: number;
|
|
73
|
+
readonly time: number;
|
|
74
|
+
readonly type: string;
|
|
75
|
+
readonly data: unknown;
|
|
76
|
+
}
|
|
77
|
+
/** One turn of a session snapshot: the durable events from `turn/start` to `turn/end`. */
|
|
78
|
+
export interface SnapshotTurn {
|
|
79
|
+
/** Seq of the `turn/start` event, or 0 for the lead-in before the first turn. */
|
|
80
|
+
readonly seq: number;
|
|
81
|
+
readonly time: number;
|
|
82
|
+
/** Seq of the `turn/end` event, or 0 while the turn is still open. */
|
|
83
|
+
readonly endSeq: number;
|
|
84
|
+
readonly endTime: number;
|
|
85
|
+
/** `turn/end` reason kind, or the empty string while the turn is open. */
|
|
86
|
+
readonly reason: string;
|
|
87
|
+
readonly entries: readonly SnapshotEntry[];
|
|
88
|
+
}
|
|
89
|
+
/** The opening frame of a `session/follow` stream. */
|
|
90
|
+
export interface FollowBaseline {
|
|
91
|
+
readonly cursor: number;
|
|
92
|
+
readonly records: readonly unknown[];
|
|
93
|
+
readonly hasMore: boolean;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Remembers which tool calls have already streamed their arguments, so the
|
|
97
|
+
* durable `tool/call` event that follows the stream is not replayed as a second
|
|
98
|
+
* copy of the same arguments. Bounded: a long-lived connection must not grow
|
|
99
|
+
* with every call it ever saw.
|
|
100
|
+
*/
|
|
101
|
+
export declare class ToolCallIndex {
|
|
102
|
+
#private;
|
|
103
|
+
/** @returns whether this call already contributed a delta. */
|
|
104
|
+
has(callId: string): boolean;
|
|
105
|
+
/** @returns the last name seen for this call, if any. */
|
|
106
|
+
nameOf(callId: string): string | undefined;
|
|
107
|
+
/** Remember one call, evicting the oldest entry past {@link TOOL_CALL_MEMORY}. */
|
|
108
|
+
remember(callId: string, name: string): void;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Remembers which durable user messages this connection already published.
|
|
112
|
+
*
|
|
113
|
+
* A client only ever appends a transcript, so one durable `user/message` must
|
|
114
|
+
* reach the wire once: a replayed event would draw the same prompt a second
|
|
115
|
+
* time. Bounded for the same reason {@link ToolCallIndex} is — a long-lived
|
|
116
|
+
* connection must not grow with every message it ever saw.
|
|
117
|
+
*/
|
|
118
|
+
export declare class MessageIndex {
|
|
119
|
+
#private;
|
|
120
|
+
/** @returns whether this message already contributed a delta. */
|
|
121
|
+
has(id: string): boolean;
|
|
122
|
+
/** Remember one message, evicting the oldest entry past {@link USER_MESSAGE_MEMORY}. */
|
|
123
|
+
remember(id: string): void;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Read the opening frame of a follow stream.
|
|
127
|
+
* @returns the cursor and records, or undefined when this frame is not a snapshot.
|
|
128
|
+
*/
|
|
129
|
+
export declare function readFollowBaseline(frame: unknown): FollowBaseline | undefined;
|
|
130
|
+
/**
|
|
131
|
+
* Map one DSH follow frame to a delta, or undefined when the frame carries no
|
|
132
|
+
* increment this protocol publishes.
|
|
133
|
+
* @param frame - one `SessionFollowFrame`.
|
|
134
|
+
* @param toolCalls - this connection's tool-call memory.
|
|
135
|
+
* @param messages - this connection's published-user-message memory.
|
|
136
|
+
*/
|
|
137
|
+
export declare function deltaOfFollowFrame(frame: unknown, toolCalls: ToolCallIndex, messages: MessageIndex): SessionDelta | undefined;
|
|
138
|
+
/**
|
|
139
|
+
* Project a follow baseline's records into turns.
|
|
140
|
+
* @param records - the baseline's durable records, oldest first.
|
|
141
|
+
* @returns one turn per `turn/start`, plus a lead-in turn when records precede the first one.
|
|
142
|
+
*/
|
|
143
|
+
export declare function turnsOfRecords(records: readonly unknown[]): SnapshotTurn[];
|
|
144
|
+
/**
|
|
145
|
+
* Drop the entries of each turn that a protocol v1 client would not render.
|
|
146
|
+
*
|
|
147
|
+
* Only the inside of a turn changes: which turns survive, their order, their
|
|
148
|
+
* `seq`/`endSeq` metadata and every kept entry's `type` and `data` are the
|
|
149
|
+
* projection's own output. A turn whose entries are all unrenderable stays as a
|
|
150
|
+
* turn with an empty `entries` array, so the client never sees a shape it does
|
|
151
|
+
* not know how to read.
|
|
152
|
+
* @param turns - the projection of a follow baseline, in order.
|
|
153
|
+
* @returns the same turns, each carrying only its rendered entries.
|
|
154
|
+
*/
|
|
155
|
+
export declare function trimUnrenderedEntries(turns: readonly SnapshotTurn[]): SnapshotTurn[];
|
|
156
|
+
/** One contiguous backwards page of a snapshot projection. */
|
|
157
|
+
export interface SnapshotPage {
|
|
158
|
+
/** The turns this page carries, oldest first; each keeps its own turn-level fields. */
|
|
159
|
+
readonly turns: SnapshotTurn[];
|
|
160
|
+
/** Durable seq of this page's oldest entry: the cursor for the next, older page. */
|
|
161
|
+
readonly nextBefore?: number;
|
|
162
|
+
readonly hasMore: boolean;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Slice one backwards page out of a projection, BY ENTRY rather than by turn.
|
|
166
|
+
*
|
|
167
|
+
* `before` is a durable watermark, not an index: the page carries the newest
|
|
168
|
+
* entries whose `seq` is strictly below it, so a cursor stays meaningful across
|
|
169
|
+
* connections and a projection that changed shape under it (a trimmed entry
|
|
170
|
+
* that never reaches the client) cannot make a page skip or repeat data. The
|
|
171
|
+
* turn list is rebuilt around exactly the entries in the page, each turn
|
|
172
|
+
* keeping its own `seq`/`endSeq`/`reason`, so a turn may be split across pages.
|
|
173
|
+
*
|
|
174
|
+
* A single entry that exceeds the budget on its own is a page of its own: the
|
|
175
|
+
* size rule never removes an entry, it only ends the page.
|
|
176
|
+
* @param turns - the projection to page, in order.
|
|
177
|
+
* @param before - exclusive durable watermark, or undefined for the newest page.
|
|
178
|
+
* @param maxBytes - byte budget of the encoded page.
|
|
179
|
+
* @param measure - exact encoded size of one page shape, owned by the caller.
|
|
180
|
+
*/
|
|
181
|
+
export declare function pageTurns(turns: readonly SnapshotTurn[], before: number | undefined, maxBytes: number, measure: (turns: readonly SnapshotTurn[], nextBefore: number | undefined, hasMore: boolean) => number): SnapshotPage;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Protocol v1 frame envelope: the one JSON-object-per-text-frame codec both
|
|
3
|
+
* ends of the bridge speak. Shapes are frozen by the parent card and are not
|
|
4
|
+
* ours to extend:
|
|
5
|
+
*
|
|
6
|
+
* request {"t":"req","id":<uint>,"m":"<method>","p":{...}}
|
|
7
|
+
* response {"t":"res","id":<uint>,"ok":true,"r":{...}}
|
|
8
|
+
* {"t":"res","id":<uint>,"ok":false,"e":{"code":"<str>","msg":"<str>"}}
|
|
9
|
+
* event {"t":"ev","seq":<uint>,"m":"<topic>","p":{...}}
|
|
10
|
+
* heartbeat {"t":"ping","ts":<ms>} / {"t":"pong","ts":<ms>}
|
|
11
|
+
*
|
|
12
|
+
* Decisions this module owns (not restated by the frozen design):
|
|
13
|
+
* - the 10 MiB cap is measured in UTF-8 bytes of the received text, before
|
|
14
|
+
* parsing, so an oversized frame never allocates a parsed object graph;
|
|
15
|
+
* - direction violations (a `res` or `ev` arriving FROM the client) are their
|
|
16
|
+
* own failure code instead of `malformed`, because "the peer is speaking the
|
|
17
|
+
* wrong half of the protocol" is an actionable diagnostic;
|
|
18
|
+
* - a frame whose identity cannot be recovered is answered with the reserved
|
|
19
|
+
* id 0, since the envelope has no way to decline answering.
|
|
20
|
+
* @module dsh-remote-bridge/core/envelope
|
|
21
|
+
*/
|
|
22
|
+
/** Largest single text frame either direction may carry, in UTF-8 bytes: 10 MiB. */
|
|
23
|
+
export declare const MAX_FRAME_BYTES: number;
|
|
24
|
+
/** Reserved response id for a frame whose own id could not be recovered. */
|
|
25
|
+
export declare const UNIDENTIFIED_FRAME_ID = 0;
|
|
26
|
+
export interface ReqFrame {
|
|
27
|
+
readonly t: 'req';
|
|
28
|
+
readonly id: number;
|
|
29
|
+
readonly m: string;
|
|
30
|
+
readonly p: Readonly<Record<string, unknown>>;
|
|
31
|
+
}
|
|
32
|
+
export interface PingFrame {
|
|
33
|
+
readonly t: 'ping';
|
|
34
|
+
readonly ts: number;
|
|
35
|
+
}
|
|
36
|
+
export interface PongFrame {
|
|
37
|
+
readonly t: 'pong';
|
|
38
|
+
readonly ts: number;
|
|
39
|
+
}
|
|
40
|
+
/** Every frame a client may legitimately send. */
|
|
41
|
+
export type ClientFrame = ReqFrame | PingFrame | PongFrame;
|
|
42
|
+
export interface ResFrame {
|
|
43
|
+
readonly t: 'res';
|
|
44
|
+
readonly id: number;
|
|
45
|
+
readonly ok: boolean;
|
|
46
|
+
readonly r?: unknown;
|
|
47
|
+
readonly e?: {
|
|
48
|
+
readonly code: string;
|
|
49
|
+
readonly msg: string;
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
export interface EvFrame {
|
|
53
|
+
readonly t: 'ev';
|
|
54
|
+
readonly seq: number;
|
|
55
|
+
readonly m: string;
|
|
56
|
+
readonly p: unknown;
|
|
57
|
+
}
|
|
58
|
+
export type ServerFrame = ResFrame | EvFrame | PingFrame | PongFrame;
|
|
59
|
+
/** One refusal the decoder can state about the bytes it was handed. */
|
|
60
|
+
export interface FrameFailure {
|
|
61
|
+
readonly ok: false;
|
|
62
|
+
/** Response id to answer on: the recovered request id, else {@link UNIDENTIFIED_FRAME_ID}. */
|
|
63
|
+
readonly id: number;
|
|
64
|
+
readonly code: string;
|
|
65
|
+
readonly msg: string;
|
|
66
|
+
}
|
|
67
|
+
export type DecodeOutcome = {
|
|
68
|
+
readonly ok: true;
|
|
69
|
+
readonly frame: ClientFrame;
|
|
70
|
+
} | FrameFailure;
|
|
71
|
+
/** Failure codes the codec itself raises. */
|
|
72
|
+
export declare const FRAME_TOO_LARGE = "frame-too-large";
|
|
73
|
+
export declare const MALFORMED_FRAME = "malformed";
|
|
74
|
+
export declare const DIRECTION_VIOLATION = "direction-violation";
|
|
75
|
+
/**
|
|
76
|
+
* Decode one received text frame.
|
|
77
|
+
* @param text - the frame body exactly as it crossed the wire.
|
|
78
|
+
* @returns the frame, or the refusal to answer with.
|
|
79
|
+
*/
|
|
80
|
+
export declare function decodeClientFrame(text: string): DecodeOutcome;
|
|
81
|
+
/** Encode one outgoing frame. */
|
|
82
|
+
export declare function encodeFrame(frame: ServerFrame): string;
|
|
83
|
+
/** One successful response frame. */
|
|
84
|
+
export declare function responseFrame(id: number, result: unknown): ResFrame;
|
|
85
|
+
/** One failed response frame. */
|
|
86
|
+
export declare function errorFrame(id: number, code: string, msg: string): ResFrame;
|
|
87
|
+
/**
|
|
88
|
+
* Per-connection event sequence. The frozen design gives each connection its
|
|
89
|
+
* own counter starting at 1; it is deliberately NOT the DSH session cursor
|
|
90
|
+
* (§3.4), so the two numbers are never compared.
|
|
91
|
+
*/
|
|
92
|
+
export declare class EventSequence {
|
|
93
|
+
#private;
|
|
94
|
+
/** @returns the next sequence number, advancing the counter. */
|
|
95
|
+
take(): number;
|
|
96
|
+
/** @returns the value {@link take} will return next, without advancing. */
|
|
97
|
+
peek(): number;
|
|
98
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bounded retry for the DSH service-activation window.
|
|
3
|
+
*
|
|
4
|
+
* The gateway raises its activation failures as `error.code`, never as an
|
|
5
|
+
* `instanceof` identity, and two generations of code strings coexist: the
|
|
6
|
+
* namespace-qualified `gateway/service-unavailable` and the bare
|
|
7
|
+
* `service-unavailable`. Both are recognized, verbatim from the family's
|
|
8
|
+
* task-board runner, so a provider that is merely slow to activate is retried
|
|
9
|
+
* rather than reported to the phone as a broken deployment.
|
|
10
|
+
* @module dsh-remote-bridge/core/gateway-errors
|
|
11
|
+
*/
|
|
12
|
+
/** Tries one unary call may consume while the target service is activating. */
|
|
13
|
+
export declare const SERVICE_UNAVAILABLE_ATTEMPTS = 5;
|
|
14
|
+
/** Wait between those tries. */
|
|
15
|
+
export declare const SERVICE_UNAVAILABLE_BACKOFF_MS = 2000;
|
|
16
|
+
/**
|
|
17
|
+
* Whether a failure means the target service has not finished activating.
|
|
18
|
+
* This is the one failure worth retrying: the same call succeeds moments later.
|
|
19
|
+
*/
|
|
20
|
+
export declare function isServiceUnavailable(error: unknown): boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Whether the running gateway does not serve this endpoint at all (the cohort
|
|
23
|
+
* changed under this plugin). Retrying cannot help; the caller reports it.
|
|
24
|
+
*/
|
|
25
|
+
export declare function isInvocationUnavailable(error: unknown): boolean;
|
|
26
|
+
/** Whether the endpoint exists but its strict definition was withdrawn by the running gateway. */
|
|
27
|
+
export declare function isDefinitionWithdrawn(error: unknown): boolean;
|
|
28
|
+
/** Whether the call's named arguments did not match the descriptor. */
|
|
29
|
+
export declare function isArgumentsInvalid(error: unknown): boolean;
|
|
30
|
+
/** One-line text of a failure, for a decideable reason the phone can show. */
|
|
31
|
+
export declare function failureText(error: unknown): string;
|
|
32
|
+
/**
|
|
33
|
+
* The failure's own code, for the response envelope. The gateway's codes cross
|
|
34
|
+
* to the phone unchanged (`gateway/arguments-invalid` and friends) so the client
|
|
35
|
+
* can tell "this deployment cannot" from "this build is wrong"; anything
|
|
36
|
+
* without a string code is `internal`.
|
|
37
|
+
*/
|
|
38
|
+
export declare function errorCodeOf(error: unknown, fallback: string): string;
|
|
39
|
+
/**
|
|
40
|
+
* Run one unary call, retrying only the service-activation window.
|
|
41
|
+
* @param run - the call to attempt.
|
|
42
|
+
* @param options.attempts - total tries (default {@link SERVICE_UNAVAILABLE_ATTEMPTS}).
|
|
43
|
+
* @param options.backoffMs - wait between tries (default {@link SERVICE_UNAVAILABLE_BACKOFF_MS}).
|
|
44
|
+
* @returns the call's own result or failure; the failure is never swallowed.
|
|
45
|
+
*/
|
|
46
|
+
export declare function withServiceUnavailableRetry<T>(run: () => Promise<T>, options?: {
|
|
47
|
+
attempts?: number;
|
|
48
|
+
backoffMs?: number;
|
|
49
|
+
}): Promise<T>;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The bridge's process-wide connection registry and its claims on
|
|
3
|
+
* `approval/request` and `user-questions/request`.
|
|
4
|
+
*
|
|
5
|
+
* The hub is what makes "phone not answering equals refusal" true: a question
|
|
6
|
+
* is claimed only while at least one paired device is connected, the publish
|
|
7
|
+
* has to actually reach someone, and every other path (timeout, agent
|
|
8
|
+
* cancellation, the last phone leaving) delegates to the next answerer, which
|
|
9
|
+
* the Host normalizes to `'unavailable'`.
|
|
10
|
+
*
|
|
11
|
+
* The claim on `user-questions/request` follows the same two rules, with two
|
|
12
|
+
* differences that belong to the host's own semantics: delegating is not a
|
|
13
|
+
* refusal, because only a phone that declares `questions.v1` is ever shown the
|
|
14
|
+
* question; and a question is named on the wire even when the host named none,
|
|
15
|
+
* because a blocking ask carries no call id and the phone still needs a handle
|
|
16
|
+
* to answer with.
|
|
17
|
+
* @module dsh-remote-bridge/core/hub
|
|
18
|
+
*/
|
|
19
|
+
import type { ApprovalOutcome } from '@deepseek-ai/dsh-user-approval';
|
|
20
|
+
import type { BridgeConnectionOptions, BridgeTransport, ConnectionAuth } from './connection.ts';
|
|
21
|
+
import { BridgeConnection } from './connection.ts';
|
|
22
|
+
import type { QuestionAnswer, UserQuestionRequestLike } from './questions.ts';
|
|
23
|
+
/**
|
|
24
|
+
* The fields of an approval question this bridge reads. Structural on purpose:
|
|
25
|
+
* the event's own type is not exported from the package root, and only these
|
|
26
|
+
* fields are ever touched.
|
|
27
|
+
*/
|
|
28
|
+
export interface ApprovalRequestLike {
|
|
29
|
+
/** The agent asking; its id IS the session id (the event carries no session field). */
|
|
30
|
+
readonly agent: {
|
|
31
|
+
readonly id: string;
|
|
32
|
+
};
|
|
33
|
+
readonly toolName: string;
|
|
34
|
+
readonly displayReason?: {
|
|
35
|
+
readonly en: string;
|
|
36
|
+
readonly [locale: string]: string;
|
|
37
|
+
};
|
|
38
|
+
readonly signal?: AbortSignal;
|
|
39
|
+
}
|
|
40
|
+
/** The summary line a phone shows for one question. */
|
|
41
|
+
export declare function approvalSummary(request: ApprovalRequestLike): string;
|
|
42
|
+
/** The bridge's live connections plus the answerer it composes onto the approval chain. */
|
|
43
|
+
export declare class BridgeHub {
|
|
44
|
+
#private;
|
|
45
|
+
constructor(options: BridgeConnectionOptions);
|
|
46
|
+
/** @returns how many paired devices could answer a question right now. */
|
|
47
|
+
get pairedClients(): number;
|
|
48
|
+
/**
|
|
49
|
+
* Register one accepted socket.
|
|
50
|
+
* @param transport - the carrier the connection writes through.
|
|
51
|
+
* @param auth - the device the upgrade proved, when it carried a valid token.
|
|
52
|
+
*/
|
|
53
|
+
attach(transport: BridgeTransport, auth?: ConnectionAuth): BridgeConnection;
|
|
54
|
+
/**
|
|
55
|
+
* Forget one connection and abort everything it owns. When it was the last
|
|
56
|
+
* paired device, every pending question is withdrawn: nobody left can answer.
|
|
57
|
+
*/
|
|
58
|
+
detach(connection: BridgeConnection): void;
|
|
59
|
+
/**
|
|
60
|
+
* Publish one event to every paired device.
|
|
61
|
+
* @returns how many connections actually took the frame.
|
|
62
|
+
*/
|
|
63
|
+
broadcast(topic: string, payload: unknown): number;
|
|
64
|
+
/**
|
|
65
|
+
* Publish one event to every paired device that announced a capability.
|
|
66
|
+
* @returns how many connections actually took the frame.
|
|
67
|
+
*/
|
|
68
|
+
/**
|
|
69
|
+
* Hand one freshly declared connection the questions it can now answer.
|
|
70
|
+
*
|
|
71
|
+
* Without this, a question raised while the phone was reconnecting stays on the
|
|
72
|
+
* host with no card anywhere on the phone: the original broadcast was addressed
|
|
73
|
+
* to clients that had already announced the channel.
|
|
74
|
+
*/
|
|
75
|
+
replayQuestions(connection: BridgeConnection): void;
|
|
76
|
+
broadcastTo(capability: string, topic: string, payload: unknown): number;
|
|
77
|
+
/**
|
|
78
|
+
* The body of this plugin's `approval/request` listener.
|
|
79
|
+
* @param request - the pending question.
|
|
80
|
+
* @param next - the rest of the answerer chain.
|
|
81
|
+
* @returns a claimed decision, or whatever the next answerer says.
|
|
82
|
+
*/
|
|
83
|
+
approve(request: ApprovalRequestLike, next: () => Promise<ApprovalOutcome>): Promise<ApprovalOutcome>;
|
|
84
|
+
/**
|
|
85
|
+
* The body of this plugin's `user-questions/request` listener.
|
|
86
|
+
*
|
|
87
|
+
* Claimed only while a paired device is connected and the ask names an agent:
|
|
88
|
+
* the payload has no session field of its own, and a question nobody can be
|
|
89
|
+
* told about is not one this bridge may hold. The wire id is the host's own
|
|
90
|
+
* when the ask is timed, and minted here when it is blocking and the host
|
|
91
|
+
* assigned none — either way the phone answers the id it was shown. A claim
|
|
92
|
+
* nobody received is withdrawn at once instead of holding the agent until the
|
|
93
|
+
* host's deadline.
|
|
94
|
+
* @param request - the pending question batch.
|
|
95
|
+
* @param next - the rest of the answerer chain.
|
|
96
|
+
* @returns the phone's answer, the host's own deadline outcome, or whatever the next answerer says.
|
|
97
|
+
*/
|
|
98
|
+
question(request: UserQuestionRequestLike, next: () => Promise<QuestionAnswer>): Promise<QuestionAnswer>;
|
|
99
|
+
/** Close every connection and withdraw every question. Idempotent. */
|
|
100
|
+
dispose(): void;
|
|
101
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The gate in front of the bootstrap token endpoint.
|
|
3
|
+
*
|
|
4
|
+
* The socket address alone is NOT a loopback proof on this deployment: a
|
|
5
|
+
* request that arrives over a public tunnel is terminated by the tunnel client
|
|
6
|
+
* on the same machine, so cloudflared connects to the origin from loopback and
|
|
7
|
+
* `req.socket.remoteAddress` reads `127.0.0.1` for a request the whole internet
|
|
8
|
+
* can reach. The reliable discriminator is the `Host` header, which both the
|
|
9
|
+
* market relay and a plain tunnel rewrite to the public hostname before the
|
|
10
|
+
* origin sees the request (`market/worker/src/relay.js:263` sets it to the
|
|
11
|
+
* registered tunnel host). Both conditions must hold, so a forged
|
|
12
|
+
* `remoteAddress` in a test — or a proxy that forgets to rewrite `Host` — still
|
|
13
|
+
* cannot mint a pairing token.
|
|
14
|
+
* @module dsh-remote-bridge/core/loopback
|
|
15
|
+
*/
|
|
16
|
+
import type { IncomingMessage } from 'node:http';
|
|
17
|
+
/** Socket addresses that count as "this machine". */
|
|
18
|
+
export declare const LOOPBACK_ADDRESSES: ReadonlySet<string>;
|
|
19
|
+
/** Host header values that count as "this machine". */
|
|
20
|
+
export declare const LOOPBACK_HOSTS: ReadonlySet<string>;
|
|
21
|
+
/** @returns whether a socket's remote address is this machine. */
|
|
22
|
+
export declare function isLoopbackAddress(remoteAddress: string | undefined): boolean;
|
|
23
|
+
/**
|
|
24
|
+
* @returns whether a `Host` header names this machine. The port is ignored;
|
|
25
|
+
* anything that is not a loopback literal — including a public tunnel
|
|
26
|
+
* hostname — is refused.
|
|
27
|
+
*/
|
|
28
|
+
export declare function isLoopbackHost(host: string | undefined): boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Why this request may not reach the bootstrap token endpoint, or undefined
|
|
31
|
+
* when it may.
|
|
32
|
+
* @param req - the incoming request.
|
|
33
|
+
* @returns a one-line reason, or undefined.
|
|
34
|
+
*/
|
|
35
|
+
export declare function loopbackDenialOf(req: IncomingMessage): string | undefined;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The in-process owner peer every gateway call of this bridge speaks for.
|
|
3
|
+
*
|
|
4
|
+
* The 0.2.1 gateway made `peer` mandatory on every request, and the
|
|
5
|
+
* authorization layer reads only `peer.principal`: `{ kind: 'owner' }` is how
|
|
6
|
+
* host-local code states that the call IS the local user driving their own
|
|
7
|
+
* Host, so the gateway answers it directly instead of running a remote-access
|
|
8
|
+
* decision (only non-owner principals are judged).
|
|
9
|
+
* @module dsh-remote-bridge/core/owner-peer
|
|
10
|
+
*/
|
|
11
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
12
|
+
import type { GatewayPeer } from './wire.ts';
|
|
13
|
+
/**
|
|
14
|
+
* Mint the in-process owner peer this bridge's Host calls speak for.
|
|
15
|
+
*
|
|
16
|
+
* The bridge's security model states that pairing is a full-control
|
|
17
|
+
* credential: a paired device reaches the harness in-process — sessions,
|
|
18
|
+
* prompts, approvals. Protocol v1 is therefore deliberately paired with the
|
|
19
|
+
* one principal that preserves the pre-0.2.1 capability of the bridge's own
|
|
20
|
+
* in-process calls: the owner. That claim is safe only because of WHERE this
|
|
21
|
+
* peer is attached:
|
|
22
|
+
*
|
|
23
|
+
* - Every gateway call a connection can trigger sits behind the bridge's own
|
|
24
|
+
* pairing gate: dispatch answers `unpaired` before any session, approval or
|
|
25
|
+
* roster work, and the three unpaired methods (`hello`, `pair.begin`,
|
|
26
|
+
* `pair.finish`) never invoke the gateway at all. The owner peer crosses to
|
|
27
|
+
* the harness only after a connection proved a device token (or just
|
|
28
|
+
* enrolled one), never for an unpaired one.
|
|
29
|
+
* - No wire field can set a principal: the connection never reads a `peer`
|
|
30
|
+
* or `principal` (or any identity) out of a client frame. The peer is minted
|
|
31
|
+
* here, in-process, fixed at `{ kind: 'owner' }`, and a client that names
|
|
32
|
+
* one in its payload is ignored — the call still speaks for this peer.
|
|
33
|
+
*
|
|
34
|
+
* `interactions` stays `'none'`: the bridge only invokes unary methods and
|
|
35
|
+
* opens the `session/follow` business stream. The gateway's interactions
|
|
36
|
+
* check gates exactly the forwarded-event endpoints (opening an event stream,
|
|
37
|
+
* answering a waterfall), which this caller never touches; an `'answer'`
|
|
38
|
+
* peer would additionally receive forwarded Host events, a capability the
|
|
39
|
+
* bridge neither needs nor wants — approvals reach devices through this
|
|
40
|
+
* plugin's own `approval/request` listener and the protocol's own events, not
|
|
41
|
+
* through a Typert forwarded-event subscription.
|
|
42
|
+
*
|
|
43
|
+
* The peer's signal aborts once `ctx` disposes, so calls and streams opened
|
|
44
|
+
* for it end with the plugin fiber, and `dispose()` is idempotent with racing
|
|
45
|
+
* calls sharing one completion. The id carries no credential — the protocol
|
|
46
|
+
* fixes `principal` as the only identity persisted in a session log — so a
|
|
47
|
+
* fresh unique id per mount is sufficient.
|
|
48
|
+
*/
|
|
49
|
+
export declare function createOwnerPeer(ctx: Context): GatewayPeer;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* In-memory bootstrap tokens and pairing nonces.
|
|
3
|
+
*
|
|
4
|
+
* Both are one-shot and short-lived, and neither is ever persisted: a token an
|
|
5
|
+
* operator reads off the Host log is only useful while its 30 s window is open,
|
|
6
|
+
* and a restart invalidating every pending pairing is the desired behavior.
|
|
7
|
+
* @module dsh-remote-bridge/core/pairing
|
|
8
|
+
*/
|
|
9
|
+
/** Lifetime of one bootstrap token handed to the operator's phone. */
|
|
10
|
+
export declare const BOOTSTRAP_TOKEN_TTL_MS = 30000;
|
|
11
|
+
/** Lifetime of one pairing nonce, from `pair.begin` to `pair.finish`. */
|
|
12
|
+
export declare const PAIR_NONCE_TTL_MS = 60000;
|
|
13
|
+
/** Entropy of one bootstrap token. */
|
|
14
|
+
export declare const BOOTSTRAP_TOKEN_BYTES = 32;
|
|
15
|
+
/** Entropy of one pairing nonce. */
|
|
16
|
+
export declare const PAIR_NONCE_BYTES = 16;
|
|
17
|
+
/** A minted one-shot secret and the instant it stops being accepted. */
|
|
18
|
+
export interface PairingTicket {
|
|
19
|
+
readonly value: string;
|
|
20
|
+
readonly expiresAt: number;
|
|
21
|
+
}
|
|
22
|
+
export interface PairingServiceOptions {
|
|
23
|
+
/** Clock, injectable so expiry is tested without sleeping. */
|
|
24
|
+
readonly now?: () => number;
|
|
25
|
+
readonly tokenTtlMs?: number;
|
|
26
|
+
readonly nonceTtlMs?: number;
|
|
27
|
+
/** Secret source, injectable so a test can pin the value it will present. */
|
|
28
|
+
readonly random?: (byteLength: number) => string;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The two one-shot secrets of the pairing handshake.
|
|
32
|
+
*
|
|
33
|
+
* `issueToken` is called by the loopback-only route an operator reaches from
|
|
34
|
+
* the Host itself; `beginPairing` consumes that token and mints the nonce the
|
|
35
|
+
* phone signs; `consumeNonce` closes the handshake. Consumption happens BEFORE
|
|
36
|
+
* any signature check in the caller, so a wrong signature still burns the
|
|
37
|
+
* nonce: a brute-force loop cannot reuse one challenge.
|
|
38
|
+
*/
|
|
39
|
+
export declare class PairingService {
|
|
40
|
+
#private;
|
|
41
|
+
constructor(options?: PairingServiceOptions);
|
|
42
|
+
/** Mint one bootstrap token, valid until its own expiry. */
|
|
43
|
+
issueToken(): PairingTicket;
|
|
44
|
+
/**
|
|
45
|
+
* Consume one bootstrap token.
|
|
46
|
+
* @returns whether the token was live; a consumed or expired token is gone.
|
|
47
|
+
*/
|
|
48
|
+
consumeToken(token: string): boolean;
|
|
49
|
+
/** Mint one pairing nonce for a connection that just consumed a valid token. */
|
|
50
|
+
issueNonce(): PairingTicket;
|
|
51
|
+
/**
|
|
52
|
+
* Consume one pairing nonce.
|
|
53
|
+
* @returns whether the nonce was live; a consumed or expired nonce is gone.
|
|
54
|
+
*/
|
|
55
|
+
consumeNonce(nonce: string): boolean;
|
|
56
|
+
/** @returns how many tokens and nonces are currently acceptable, for tests and diagnostics. */
|
|
57
|
+
get pending(): {
|
|
58
|
+
tokens: number;
|
|
59
|
+
nonces: number;
|
|
60
|
+
};
|
|
61
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The frozen protocol surface: method names, topic names, refusal codes, and
|
|
3
|
+
* the one authorization rule the dispatcher enforces before any method runs.
|
|
4
|
+
* @module dsh-remote-bridge/core/protocol
|
|
5
|
+
*/
|
|
6
|
+
/** Client-callable methods of protocol v1. */
|
|
7
|
+
export declare const METHOD_HELLO = "hello";
|
|
8
|
+
export declare const METHOD_SESSION_LIST = "session.list";
|
|
9
|
+
export declare const METHOD_SESSION_TITLES = "session.titles";
|
|
10
|
+
export declare const METHOD_SESSION_SNAPSHOT = "session.snapshot";
|
|
11
|
+
export declare const METHOD_SESSION_HISTORY = "session.history";
|
|
12
|
+
export declare const METHOD_SESSION_PROMPT = "session.prompt";
|
|
13
|
+
export declare const METHOD_SESSION_INTERRUPT = "session.interrupt";
|
|
14
|
+
export declare const METHOD_SESSION_ATTACHMENT = "session.attachment";
|
|
15
|
+
export declare const METHOD_SESSION_PERMISSIONS = "session.permissions";
|
|
16
|
+
export declare const METHOD_SESSION_SET_PERMISSION = "session.setPermission";
|
|
17
|
+
export declare const METHOD_SESSION_MODELS = "session.models";
|
|
18
|
+
export declare const METHOD_SESSION_SET_MODEL = "session.setModel";
|
|
19
|
+
export declare const METHOD_PAIR_BEGIN = "pair.begin";
|
|
20
|
+
export declare const METHOD_PAIR_FINISH = "pair.finish";
|
|
21
|
+
export declare const METHOD_APPROVAL_RESOLVE = "approval.resolve";
|
|
22
|
+
export declare const METHOD_QUESTION_ANSWER = "question.answer";
|
|
23
|
+
/** Server-pushed topics of protocol v1. */
|
|
24
|
+
export declare const TOPIC_SESSION_DELTA = "session.delta";
|
|
25
|
+
export declare const TOPIC_APPROVAL_REQUEST = "approval.request";
|
|
26
|
+
export declare const TOPIC_QUESTION_REQUEST = "question.request";
|
|
27
|
+
/**
|
|
28
|
+
* Capability a client declares in `hello`'s `cap` array to ask for a trimmed
|
|
29
|
+
* `session.snapshot` baseline. A client that does not declare it — including
|
|
30
|
+
* every client written before this capability existed — is served exactly the
|
|
31
|
+
* baseline protocol v1 always served.
|
|
32
|
+
*/
|
|
33
|
+
export declare const CAP_SESSION_SNAPSHOT_TRIM = "session.snapshot.trim.v1";
|
|
34
|
+
/**
|
|
35
|
+
* Capability a client declares in `hello`'s `cap` array to have
|
|
36
|
+
* `session.snapshot` answer with one bounded page instead of the whole opening
|
|
37
|
+
* window. Declaring it is what makes an opening screen cost one page rather
|
|
38
|
+
* than a whole session; a client that does not declare it — including every
|
|
39
|
+
* client written before this capability existed — receives byte for byte the
|
|
40
|
+
* snapshot protocol v1 always served.
|
|
41
|
+
*/
|
|
42
|
+
export declare const CAP_SESSION_SNAPSHOT_PAGE = "session.snapshot.page.v1";
|
|
43
|
+
/**
|
|
44
|
+
* Capability a client declares in `hello`'s `cap` array to be shown the
|
|
45
|
+
* host's pending user questions and to answer them with `question.answer`.
|
|
46
|
+
*
|
|
47
|
+
* Declaring it is what makes `question.request` reachable and what makes the
|
|
48
|
+
* method part of that client's protocol at all: a client that does not declare
|
|
49
|
+
* it — including every client written before this channel existed — receives no
|
|
50
|
+
* such event, and its `question.answer` is refused as an unknown method,
|
|
51
|
+
* exactly as that frame was refused before.
|
|
52
|
+
*/
|
|
53
|
+
export declare const CAP_QUESTIONS = "questions.v1";
|
|
54
|
+
/**
|
|
55
|
+
* Methods an unpaired connection may still call: the handshake itself and the
|
|
56
|
+
* two pairing steps. Everything else answers `unpaired` — a caller can pair
|
|
57
|
+
* without a device token, but cannot touch a session without one.
|
|
58
|
+
*/
|
|
59
|
+
export declare const UNPAIRED_METHODS: ReadonlySet<string>;
|
|
60
|
+
/** Every method this protocol version serves. */
|
|
61
|
+
export declare const KNOWN_METHODS: ReadonlySet<string>;
|
|
62
|
+
/** Refusal codes the bridge itself raises (DSH failures cross as their own code). */
|
|
63
|
+
export declare const CODE_UNPAIRED = "unpaired";
|
|
64
|
+
export declare const CODE_UNKNOWN_METHOD = "unknown-method";
|
|
65
|
+
export declare const CODE_INVALID_ARGS = "invalid-args";
|
|
66
|
+
export declare const CODE_PROTO_UNSUPPORTED = "proto-unsupported";
|
|
67
|
+
export declare const CODE_TOKEN_INVALID = "token-invalid";
|
|
68
|
+
export declare const CODE_NONCE_INVALID = "nonce-invalid";
|
|
69
|
+
export declare const CODE_SIGNATURE_INVALID = "signature-invalid";
|
|
70
|
+
export declare const CODE_APPROVAL_UNKNOWN = "approval-unknown";
|
|
71
|
+
/**
|
|
72
|
+
* The bridge does not hold the named question: it never claimed that call id, it
|
|
73
|
+
* already answered it, or the host no longer holds it as answerable. Host
|
|
74
|
+
* failures crossing this method keep their own codes instead.
|
|
75
|
+
*/
|
|
76
|
+
export declare const CODE_QUESTION_UNKNOWN = "question-unknown";
|
|
77
|
+
export declare const CODE_SNAPSHOT_UNAVAILABLE = "snapshot-unavailable";
|
|
78
|
+
export declare const CODE_SNAPSHOT_TOO_LARGE = "snapshot-too-large";
|
|
79
|
+
export declare const CODE_INTERNAL = "internal";
|
|
80
|
+
/** Protocol version this package speaks. */
|
|
81
|
+
export declare const PROTO = 1;
|
|
82
|
+
/** Minor version this package speaks. */
|
|
83
|
+
export declare const MINOR = 0;
|
|
84
|
+
/** `server` field of the hello response. */
|
|
85
|
+
export declare const SERVER_ID = "dsh-remote-bridge";
|
|
86
|
+
/** Exact route path of the bridge's WebSocket endpoint. */
|
|
87
|
+
export declare const WS_PATH = "/ios/v1";
|
|
88
|
+
/** Exact route path of the loopback-only bootstrap token endpoint. */
|
|
89
|
+
export declare const PAIR_TOKEN_PATH = "/remote-bridge/pair/token";
|