@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.
Files changed (81) hide show
  1. package/LICENSE +202 -0
  2. package/README.i18n.yaml +6 -0
  3. package/README.md +277 -2
  4. package/README.zh.md +276 -0
  5. package/cordis.patch.yml +17 -0
  6. package/icon.svg +3 -0
  7. package/lib/client.js +1830 -0
  8. package/lib/client.js.map +1 -0
  9. package/lib/index.js +5789 -0
  10. package/lib/types/client/PairEntry.d.ts +16 -0
  11. package/lib/types/client/PairPanel.d.ts +55 -0
  12. package/lib/types/client/icons.d.ts +18 -0
  13. package/lib/types/client/index.d.ts +35 -0
  14. package/lib/types/client/locales.d.ts +42 -0
  15. package/lib/types/client/pair-token.d.ts +73 -0
  16. package/lib/types/client/semantic.d.ts +28 -0
  17. package/lib/types/core/approvals.d.ts +99 -0
  18. package/lib/types/core/connection.d.ts +170 -0
  19. package/lib/types/core/crypto.d.ts +48 -0
  20. package/lib/types/core/delta.d.ts +101 -0
  21. package/lib/types/core/devices.d.ts +72 -0
  22. package/lib/types/core/dsh-frames.d.ts +332 -0
  23. package/lib/types/core/envelope.d.ts +132 -0
  24. package/lib/types/core/extension.d.ts +144 -0
  25. package/lib/types/core/gateway-errors.d.ts +49 -0
  26. package/lib/types/core/hub.d.ts +127 -0
  27. package/lib/types/core/jobs.d.ts +204 -0
  28. package/lib/types/core/loopback.d.ts +35 -0
  29. package/lib/types/core/owner-peer.d.ts +49 -0
  30. package/lib/types/core/pairing.d.ts +61 -0
  31. package/lib/types/core/protocol.d.ts +319 -0
  32. package/lib/types/core/questions.d.ts +294 -0
  33. package/lib/types/core/session-meta.d.ts +105 -0
  34. package/lib/types/core/session-permissions.d.ts +134 -0
  35. package/lib/types/core/wire.d.ts +115 -0
  36. package/lib/types/core/ws-frames.d.ts +72 -0
  37. package/lib/types/host/dsh-home.d.ts +16 -0
  38. package/lib/types/host/pair-origin.d.ts +44 -0
  39. package/lib/types/host/routes.d.ts +27 -0
  40. package/lib/types/host/websocket.d.ts +111 -0
  41. package/lib/types/index.d.ts +55 -0
  42. package/lib/types/mount-once.d.ts +55 -0
  43. package/locale/en.json +6 -0
  44. package/locale/ru.json +6 -0
  45. package/locale/zh.json +6 -0
  46. package/package.json +93 -3
  47. package/src/client/PairEntry.tsx +151 -0
  48. package/src/client/PairPanel.tsx +183 -0
  49. package/src/client/icons.tsx +72 -0
  50. package/src/client/index.ts +81 -0
  51. package/src/client/locales.ts +72 -0
  52. package/src/client/pair-token.ts +104 -0
  53. package/src/client/panel.module.css +344 -0
  54. package/src/client/semantic.ts +37 -0
  55. package/src/core/approvals.ts +173 -0
  56. package/src/core/connection.ts +1906 -0
  57. package/src/core/crypto.ts +113 -0
  58. package/src/core/delta.ts +204 -0
  59. package/src/core/devices.ts +209 -0
  60. package/src/core/dsh-frames.ts +843 -0
  61. package/src/core/envelope.ts +219 -0
  62. package/src/core/extension.ts +197 -0
  63. package/src/core/gateway-errors.ts +92 -0
  64. package/src/core/hub.ts +310 -0
  65. package/src/core/jobs.ts +372 -0
  66. package/src/core/loopback.ts +60 -0
  67. package/src/core/owner-peer.ts +70 -0
  68. package/src/core/pairing.ts +113 -0
  69. package/src/core/protocol.ts +381 -0
  70. package/src/core/questions.ts +630 -0
  71. package/src/core/session-meta.ts +176 -0
  72. package/src/core/session-permissions.ts +266 -0
  73. package/src/core/wire.ts +159 -0
  74. package/src/core/ws-frames.ts +139 -0
  75. package/src/css-modules.d.ts +6 -0
  76. package/src/host/dsh-home.ts +42 -0
  77. package/src/host/pair-origin.ts +77 -0
  78. package/src/host/routes.ts +85 -0
  79. package/src/host/websocket.ts +359 -0
  80. package/src/index.ts +257 -0
  81. package/src/mount-once.ts +142 -0
@@ -0,0 +1,319 @@
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_HISTORY_DEEP = "session.history.deep";
13
+ export declare const METHOD_SESSION_PROMPT = "session.prompt";
14
+ export declare const METHOD_SESSION_INTERRUPT = "session.interrupt";
15
+ export declare const METHOD_SESSION_ATTACHMENT = "session.attachment";
16
+ export declare const METHOD_SESSION_PERMISSIONS = "session.permissions";
17
+ export declare const METHOD_SESSION_SET_PERMISSION = "session.setPermission";
18
+ export declare const METHOD_SESSION_MODELS = "session.models";
19
+ export declare const METHOD_SESSION_SET_MODEL = "session.setModel";
20
+ export declare const METHOD_SESSION_CREATE = "session.create";
21
+ export declare const METHOD_SESSION_FORK = "session.fork";
22
+ export declare const METHOD_PRESETS_LIST = "presets.list";
23
+ export declare const METHOD_PAIR_BEGIN = "pair.begin";
24
+ export declare const METHOD_PAIR_FINISH = "pair.finish";
25
+ export declare const METHOD_APPROVAL_RESOLVE = "approval.resolve";
26
+ export declare const METHOD_QUESTION_ANSWER = "question.answer";
27
+ export declare const METHOD_JOBS_LIST = "jobs.list";
28
+ /** Server-pushed topics of protocol v1. */
29
+ export declare const TOPIC_SESSION_DELTA = "session.delta";
30
+ export declare const TOPIC_APPROVAL_REQUEST = "approval.request";
31
+ export declare const TOPIC_QUESTION_REQUEST = "question.request";
32
+ export declare const TOPIC_JOBS_CHANGED = "jobs.changed";
33
+ /**
34
+ * Capability a client declares in `hello`'s `cap` array to ask for a trimmed
35
+ * `session.snapshot` baseline. A client that does not declare it — including
36
+ * every client written before this capability existed — is served exactly the
37
+ * baseline protocol v1 always served.
38
+ */
39
+ export declare const CAP_SESSION_SNAPSHOT_TRIM = "session.snapshot.trim.v1";
40
+ /**
41
+ * Capability a client declares in `hello`'s `cap` array to have
42
+ * `session.snapshot` answer with one bounded page instead of the whole opening
43
+ * window. Declaring it is what makes an opening screen cost one page rather
44
+ * than a whole session; a client that does not declare it — including every
45
+ * client written before this capability existed — receives byte for byte the
46
+ * snapshot protocol v1 always served.
47
+ */
48
+ export declare const CAP_SESSION_SNAPSHOT_PAGE = "session.snapshot.page.v1";
49
+ /**
50
+ * Capability a client declares in `hello`'s `cap` array to have
51
+ * `session.snapshot` answer a reconnect with only what it missed.
52
+ *
53
+ * The declaration and the request's `sinceSeq` are what it takes together: a
54
+ * snapshot that carries no `sinceSeq` is answered, byte for byte, the body
55
+ * protocol v1 always served, whether or not the client declared this. Behind
56
+ * both, the client names the durable `lastSeq` of a snapshot it already holds
57
+ * and the answer is either that same reply in an incremental form —
58
+ * `{turns, lastSeq, mode:"since", reset:false}` carrying only the entries above
59
+ * that watermark — or a full baseline with `mode:"full"` and `reset:true`,
60
+ * which is the one answer that tells a client to throw its copy away. The
61
+ * estimate is only served when the bridge can PROVE nothing was lost: the
62
+ * current follow window's oldest entry must sit at or below `sinceSeq + 1`, so a
63
+ * window that has already scrolled past the watermark answers the baseline
64
+ * instead of a delta with a hole in it. It exists because a reconnect on the
65
+ * phone re-paid 0.86-1.06 s of baseline five times in one measured run.
66
+ */
67
+ export declare const CAP_SESSION_SNAPSHOT_SINCE = "session.snapshot.since.v1";
68
+ /**
69
+ * Capability a client declares in `hello`'s `cap` array to be shown the
70
+ * host's pending user questions and to answer them with `question.answer`.
71
+ *
72
+ * Declaring it is what makes `question.request` reachable and what makes the
73
+ * method part of that client's protocol at all: a client that does not declare
74
+ * it — including every client written before this channel existed — receives no
75
+ * such event, and its `question.answer` is refused as an unknown method,
76
+ * exactly as that frame was refused before.
77
+ */
78
+ export declare const CAP_QUESTIONS = "questions.v1";
79
+ /**
80
+ * Capability a client declares in `hello`'s `cap` array to be handed the
81
+ * approval questions this bridge is already holding for it.
82
+ *
83
+ * Unlike every channel capability beside it, this one gates a second delivery
84
+ * rather than the channel: `approval.request` still reaches every paired
85
+ * connection the moment a tool asks, exactly as it did before this name
86
+ * existed, because gating the first broadcast would take approvals away from
87
+ * every client written before the capability. What the declaration buys is the
88
+ * replay — after a reconnect, or on a session screen opened late, the client is
89
+ * handed the questions that are still waiting instead of a card it never saw.
90
+ * Only a live claim is replayed: a question that was answered, that the bridge
91
+ * timed out, or that was withdrawn with the last phone is gone from the claim
92
+ * registry before the replay reads it.
93
+ *
94
+ * It is deliberately absent from {@link SERVED_CAPABILITIES}: that list is what
95
+ * `hello` advertises as served, advertising a capability is a minor-version
96
+ * addition (see {@link MINOR}), and this channel changes nothing a client can
97
+ * observe in the handshake — no answer, no field and no method behind it. The
98
+ * declaration is the whole switch, and the name reaches a client through this
99
+ * bridge's own protocol documentation.
100
+ */
101
+ export declare const CAP_APPROVALS = "approvals.v1";
102
+ /**
103
+ * Capability a client declares in `hello`'s `cap` array to be told where a
104
+ * message came from and that an agent spawned a child session.
105
+ *
106
+ * Declaring it is what makes the `subagent` delta part of that client's
107
+ * protocol: a client that does not declare it — including every client written
108
+ * before this channel existed — receives no such event, exactly as that frame
109
+ * was dropped before. The `actor` of a `user-message` is not gated: it is an
110
+ * additive field a client that does not read it ignores.
111
+ */
112
+ export declare const CAP_SUBAGENT_THREAD = "subagent.thread.v1";
113
+ /**
114
+ * Capability a client declares in `hello`'s `cap` array to see the host's
115
+ * background jobs and to be told when the set changes.
116
+ *
117
+ * Declaring it is what makes `jobs.list` reachable, and `jobs.list` is the only
118
+ * thing that opens a roster: a connection that declares this capability but
119
+ * never asks for a scope receives no `jobs.changed` at all. A client that does
120
+ * not declare it — including every client written before this channel existed —
121
+ * receives no such event, and its `jobs.list` is refused as an unknown method,
122
+ * exactly as that frame was refused before.
123
+ */
124
+ export declare const CAP_JOBS = "jobs.v1";
125
+ /**
126
+ * Capability a client declares in `hello`'s `cap` array to read the host's
127
+ * Agent presets and to instantiate one as a session.
128
+ *
129
+ * Declaring it is what makes `presets.list` and `session.create` part of that
130
+ * client's protocol at all: a client that does not declare it — including every
131
+ * client written before this channel existed — is refused both as unknown
132
+ * methods, exactly as those frames were refused before. Neither method is
133
+ * called on a connection's behalf either, so a client that never asks sends the
134
+ * host no request.
135
+ */
136
+ export declare const CAP_AGENTS = "agents.v1";
137
+ /**
138
+ * Capability a client declares in `hello`'s `cap` array to be answered with
139
+ * byte-level increments instead of the whole roster or the whole board.
140
+ *
141
+ * Behind it, `session.list` and `board.snapshot` (the latter through the board
142
+ * plugin, §15) accept an opaque `since` token and answer either the whole set
143
+ * (`mode:"reset"`) or only what changed since the set that token names. A
144
+ * client that does not declare it — including every client written before this
145
+ * channel existed — is answered today's body plus the ignorable `mode` and
146
+ * `token` keys, with `mode` always `"reset"`.
147
+ */
148
+ export declare const CAP_DELTA = "delta.v1";
149
+ /**
150
+ * Capability a client declares in `hello`'s `cap` array to be told that an
151
+ * archived session is not in the roster at all.
152
+ *
153
+ * Unlike every capability beside it this one negotiates nothing: the bridge
154
+ * never transfers an archived session, whether or not a client declares it, so
155
+ * an older client simply receives the shorter roster it would have hidden
156
+ * anyway. The name is an announcement of that fact, so a client can stop
157
+ * filtering for itself: behind it, no `session.list` row is a session the
158
+ * host's archive set names, a row that does survive still carries
159
+ * `archived: false` under the same schema, and a live child of an archived
160
+ * parent still crosses with its `parentSessionId` — the parent id, not the
161
+ * parent row.
162
+ */
163
+ export declare const CAP_ROSTER_OMITS_ARCHIVED = "session.list.omit-archived.v1";
164
+ /**
165
+ * Capability a client declares in `hello`'s `cap` array to be answered a
166
+ * compact roster it can page.
167
+ *
168
+ * The declaration alone changes nothing: a listing that carries none of the
169
+ * compact parameters (`cwd`, `maxRows`, `after`) is answered exactly the body
170
+ * protocol v1 always served, so a client can opt into the name before it
171
+ * relies on any of it. Behind the parameters, a row drops its absolute `cwd`
172
+ * unless the request asks for it, drops its `updatedAt` when this connection
173
+ * already sent that id the same value, and a `maxRows`/`after` pair cuts the
174
+ * roster into pages ordered most recent first. The roster is the most repeated
175
+ * large reply on the link, so this is what lets a reconnect pay for the newest
176
+ * screenful plus a cursor instead of the whole list again.
177
+ */
178
+ export declare const CAP_ROSTER_COMPACT = "session.list.compact.v1";
179
+ /**
180
+ * Capability a client declares in `hello`'s `cap` array to be told that this
181
+ * bridge can page a session's durable history down to its oldest entry.
182
+ *
183
+ * The capability is an announcement and nothing more. `session.history.deep` is
184
+ * open to every paired connection, exactly as `session.history` is, because it
185
+ * is a pure read of the session store that costs no other client anything;
186
+ * declaring the capability is how a client learns the method exists, and a
187
+ * client that does not declare it loses only that knowledge.
188
+ */
189
+ export declare const CAP_SESSION_DEEP_HISTORY = "session.deep-history.v1";
190
+ /**
191
+ * Capability a client declares in `hello`'s `cap` array to fork a session.
192
+ *
193
+ * Declaring it is what makes `session.fork` part of that client's protocol at
194
+ * all: a client that does not declare it — including every client written
195
+ * before this channel existed — is refused `unknown-method`, and no fork is
196
+ * asked of the host on its behalf. Behind the name the bridge forwards one
197
+ * `SessionForkRequest` to the host's `session/fork` and hands back the host's
198
+ * own `SessionForkValue`, so what a fork point means stays the host's: an
199
+ * `atSeq` is the source event seq the fork includes, an omitted one is the
200
+ * newest finished turn, and a cut the host opens itself is closed by the host's
201
+ * own synthetic finishing event. Nothing about a fork is modelled or
202
+ * re-derived here.
203
+ */
204
+ export declare const CAP_SESSION_FORK = "session.fork.v1";
205
+ /**
206
+ * Capability a client declares in `hello`'s `cap` array to be sent frames
207
+ * compressed instead of as plain text.
208
+ *
209
+ * Declaring it is what allows the host to compress at all: a frame whose JSON
210
+ * text is at least 1024 UTF-8 bytes crosses as one WebSocket binary frame
211
+ * carrying raw DEFLATE (RFC 1951) of exactly that text, and a frame that does
212
+ * not get smaller still crosses as the text frame it always was. A client that
213
+ * does not declare it — including every client written before this capability
214
+ * existed — receives byte for byte the text frames protocol v1 always wrote.
215
+ *
216
+ * The direction is one way: a client still sends text frames only, and the
217
+ * 10 MiB frame cap stays measured on the text, before inflation.
218
+ */
219
+ export declare const CAP_FRAME_DEFLATE = "frame.deflate.v1";
220
+ /**
221
+ * Methods an unpaired connection may still call: the handshake itself and the
222
+ * two pairing steps. Everything else answers `unpaired` — a caller can pair
223
+ * without a device token, but cannot touch a session without one.
224
+ */
225
+ export declare const UNPAIRED_METHODS: ReadonlySet<string>;
226
+ /** Every method this protocol version serves. */
227
+ export declare const KNOWN_METHODS: ReadonlySet<string>;
228
+ /** Refusal codes the bridge itself raises (DSH failures cross as their own code). */
229
+ export declare const CODE_UNPAIRED = "unpaired";
230
+ export declare const CODE_UNKNOWN_METHOD = "unknown-method";
231
+ export declare const CODE_INVALID_ARGS = "invalid-args";
232
+ export declare const CODE_PROTO_UNSUPPORTED = "proto-unsupported";
233
+ export declare const CODE_TOKEN_INVALID = "token-invalid";
234
+ export declare const CODE_NONCE_INVALID = "nonce-invalid";
235
+ export declare const CODE_SIGNATURE_INVALID = "signature-invalid";
236
+ export declare const CODE_APPROVAL_UNKNOWN = "approval-unknown";
237
+ /**
238
+ * The bridge does not hold the named question: it never claimed that call id, it
239
+ * already answered it, or the host no longer holds it as answerable. Host
240
+ * failures crossing this method keep their own codes instead.
241
+ */
242
+ export declare const CODE_QUESTION_UNKNOWN = "question-unknown";
243
+ /**
244
+ * The named session is a delegated child whose durable parent is not a session
245
+ * this connection may read. The bridge follows an addressable child and refuses
246
+ * an unaddressable one, rather than reading it blind.
247
+ */
248
+ export declare const CODE_SESSION_NOT_FOLLOWABLE = "session-not-followable";
249
+ export declare const CODE_SNAPSHOT_UNAVAILABLE = "snapshot-unavailable";
250
+ /**
251
+ * This host composes no job registry, so it has no background jobs to publish.
252
+ * The capability stays advertised — it is a fact about this bridge, not about
253
+ * what a particular host mounts — and only a call that needs the registry fails.
254
+ */
255
+ export declare const CODE_JOBS_UNAVAILABLE = "jobs-unavailable";
256
+ export declare const CODE_SNAPSHOT_TOO_LARGE = "snapshot-too-large";
257
+ export declare const CODE_INTERNAL = "internal";
258
+ /** Protocol version this package speaks. */
259
+ export declare const PROTO = 1;
260
+ /**
261
+ * Minor version this package speaks.
262
+ *
263
+ * 11 when the fork channel shipped: behind {@link CAP_SESSION_FORK} a client may
264
+ * call {@link METHOD_SESSION_FORK} and receive the host's own fork answer, so a
265
+ * phone branches a session that inherits the context of the one it forked. This
266
+ * is the second of two appends cut from 9 — the snapshot-`since` channel took
267
+ * 10 on the integration branch first — so the number steps over the one already
268
+ * on `dev`: the minor counts additions to this protocol, and a later landing
269
+ * steps over the earlier one instead of reusing its number.
270
+ * 10 when the snapshot became incremental on request: behind
271
+ * {@link CAP_SESSION_SNAPSHOT_SINCE} a `session.snapshot` that names `sinceSeq`
272
+ * is answered either the entries above that watermark or a full baseline
273
+ * carrying `mode` and `reset`, so a client reads a new reply shape off the
274
+ * wire and the number rises with it.
275
+ * 9 when the compact roster shipped: behind {@link CAP_ROSTER_COMPACT} a
276
+ * `session.list` that names one of the compact parameters is answered a
277
+ * shorter row and may be cut into pages, so a client reads a new row schema
278
+ * off the wire and the number rises with it.
279
+ * 8 when the outbound frame form became a capability: behind
280
+ * {@link CAP_FRAME_DEFLATE} a declaring client may be answered in binary
281
+ * frames carrying raw DEFLATE of the text, so the minor is raised for a change
282
+ * a client reads off the wire rather than for a name alone.
283
+ * 7 when the two channels met: the agent channel and the durable history
284
+ * channel each raised 5 to 6 on its own branch, so the protocol that carries
285
+ * both is 7. 6 when the agent channel shipped: behind {@link CAP_AGENTS}
286
+ * `presets.list` forwards the host's Agent-preset roster and `session.create`
287
+ * hands the host one creation request, both verbatim. 6 was also the durable
288
+ * history channel on its own: behind {@link CAP_SESSION_DEEP_HISTORY} a client
289
+ * may call {@link METHOD_SESSION_HISTORY_DEEP} and walk a session's store back
290
+ * to its oldest entry, below the follow window `session.history` slices. 5 when
291
+ * the roster stopped carrying archived sessions: behind
292
+ * {@link CAP_ROSTER_OMITS_ARCHIVED} `session.list` names no session the host's
293
+ * archive set holds, so a session archived between two listings arrives as a
294
+ * removal and one restored as a change. 4 when byte-level increments shipped:
295
+ * behind {@link CAP_DELTA} `session.list` and `board.snapshot` answer a reset
296
+ * or a delta against a `since` token. 3 was
297
+ * the background-job channel — behind {@link CAP_JOBS} a client may call
298
+ * `jobs.list` and receive `jobs.changed`. 2 was the subagent thread —
299
+ * a `user-message` delta may carry an `actor` and a `subagent` delta exists
300
+ * behind {@link CAP_SUBAGENT_THREAD}. 1 was the extension seam — a mounted
301
+ * plugin may answer methods outside {@link KNOWN_METHODS} once the client
302
+ * declares the capability that plugin requires. No addition changed the major
303
+ * version, a frozen field, or an existing method or topic name.
304
+ */
305
+ export declare const MINOR = 11;
306
+ /**
307
+ * The capabilities this bridge itself serves, as `hello` advertises them.
308
+ *
309
+ * A capability a mounted plugin adds is not listed here: the bridge cannot know
310
+ * what it is not the author of. A client learns such a capability from the
311
+ * plugin's own documentation, exactly as it did before this list existed.
312
+ */
313
+ export declare const SERVED_CAPABILITIES: readonly string[];
314
+ /** `server` field of the hello response. */
315
+ export declare const SERVER_ID = "dsh-remote-bridge";
316
+ /** Exact route path of the bridge's WebSocket endpoint. */
317
+ export declare const WS_PATH = "/ios/v1";
318
+ /** Exact route path of the loopback-only bootstrap token endpoint. */
319
+ export declare const PAIR_TOKEN_PATH = "/remote-bridge/pair/token";
@@ -0,0 +1,294 @@
1
+ /**
2
+ * The bridge's claims on `user-questions/request`: which questions a phone may
3
+ * answer, and the one path an answer travels back into the host.
4
+ *
5
+ * The host asks in two shapes, and they are two shapes of the SAME channel:
6
+ *
7
+ * - a timed ask (`askTimed`, the tool schema that declares a `timeout`) rides
8
+ * the waterfall with a `wait.callId`. While it is open the bridge HOLDS the
9
+ * host's promise, so the answer is that promise's resolution — exactly what an
10
+ * in-process answerer does, and the only way an open question can be answered
11
+ * at all (`ctx.userQuestions.answer` refuses an open question by returning
12
+ * false). Once the host's own deadline released the model, the ask is gone and
13
+ * the question can only be answered through the host's Remote `answer`, which
14
+ * steers the reply into the agent as a queued user message.
15
+ * - a blocking ask (the schema without `timeout`, which is the `ask_user_question`
16
+ * a phone actually shows) carries NO `wait` at all, and the host assigns no
17
+ * call id. It is still claimed, and the bridge mints its own opaque id for the
18
+ * wire — resolving the promise it holds needs no host identity at all. Such a
19
+ * question is only ever `open`: the host has no deadline for it, so it never
20
+ * becomes `continued` and never takes the Remote `answer` path.
21
+ *
22
+ * Every claim is one record keyed by the id published on the wire, so an answer
23
+ * that arrives late still lands, and an answer for an id this bridge never
24
+ * claimed is refused instead of being forwarded to the host blindly.
25
+ *
26
+ * The bridge invents no deadline, for either shape: a timed ask's deadline is
27
+ * the host's own and is only propagated here, and a blocking ask simply has
28
+ * none — a phone that never answers leaves it open for as long as the asker
29
+ * waits. It also never fabricates a refusal: a question nobody can answer is
30
+ * delegated to `next()`, never answered with a decision the operator did not
31
+ * make.
32
+ *
33
+ * A timed ask's deadline is read back from the host itself
34
+ * (`userQuestions/attachWait`) and published as `expiresAt`, so the phone can
35
+ * count down the wait the host is actually serving. Holding that subscription
36
+ * parks the host's own deadline timer, so this module also has to let go at the
37
+ * instant the deadline it learned has passed: only then does the host raise
38
+ * `ASK_TIMED_OUT` and hand the claim its `continued` state.
39
+ *
40
+ * The types below mirror `@deepseek-ai/dsh-user-questions` field for field. The
41
+ * package is not a dependency of this one (it is the host's, not the bridge's),
42
+ * so the contract is declared locally instead of imported.
43
+ * @module dsh-remote-bridge/core/questions
44
+ */
45
+ import { type BridgeGateway, type GatewayPeer } from './wire.ts';
46
+ /** One selectable answer offered by the asker. */
47
+ export interface QuestionOption {
48
+ readonly label: string;
49
+ readonly description?: string;
50
+ }
51
+ /** One question, in the field names protocol v1 publishes. */
52
+ export interface QuestionItem {
53
+ readonly id: string;
54
+ readonly question: string;
55
+ readonly header?: string;
56
+ readonly detail?: string;
57
+ readonly options?: readonly QuestionOption[];
58
+ readonly multiSelect?: boolean;
59
+ }
60
+ /** One item of the answer batch. */
61
+ export interface QuestionAnswerItem {
62
+ readonly id: string;
63
+ readonly selected: readonly string[];
64
+ readonly custom?: string;
65
+ }
66
+ /** The whole answer batch the host's ask is settled with. */
67
+ export interface QuestionAnswer {
68
+ readonly answers: readonly QuestionAnswerItem[];
69
+ }
70
+ /** Whether a question still holds the foreground ask, or only its late reply. */
71
+ /**
72
+ * `open` while the ask is still waiting, `continued` once the host's foreground
73
+ * wait ended but a late answer can still be steered in, and `settled` as the
74
+ * one-way notice that the phone's card is over — the host's own UI answered it or
75
+ * the last paired client left. `settled` is published, never held.
76
+ */
77
+ export type QuestionState = 'open' | 'continued' | 'settled';
78
+ /** `question.request`'s payload, as frozen by the contract. */
79
+ export interface QuestionRequestPayload {
80
+ readonly sessionId: string;
81
+ readonly callId: string;
82
+ readonly state: QuestionState;
83
+ readonly questions: readonly QuestionItem[];
84
+ /** Epoch milliseconds; absent when the host published no readable deadline. */
85
+ readonly expiresAt?: number;
86
+ }
87
+ /**
88
+ * The fields of a host question request this bridge reads.
89
+ *
90
+ * Structural on purpose, for the reason the approval listener is: the event's
91
+ * own type is not reachable from here, and only these fields are ever touched.
92
+ * `wait` is the host's own field for the timed schema — it is what carries the
93
+ * call id, and it is absent for a legacy blocking ask, which this bridge
94
+ * therefore delegates rather than claims.
95
+ */
96
+ export interface UserQuestionRequestLike {
97
+ readonly questions: readonly QuestionItem[];
98
+ /** The agent asking; its id IS the session id (the event carries no session field). */
99
+ readonly agent?: {
100
+ readonly id: string;
101
+ };
102
+ readonly signal?: AbortSignal;
103
+ readonly wait?: {
104
+ readonly callId?: string;
105
+ readonly timed?: boolean;
106
+ };
107
+ }
108
+ /** The identity and content of one claim, as the hub publishes it. */
109
+ export interface ClaimedQuestionView {
110
+ readonly sessionId: string;
111
+ readonly callId: string;
112
+ readonly questions: readonly QuestionItem[];
113
+ /**
114
+ * Epoch milliseconds at which the host's own deadline falls — the absolute
115
+ * form of the remaining wait the host reported through `attachWait`.
116
+ *
117
+ * Absent when the host said nothing: a blocking ask has no deadline at all,
118
+ * and a timed one is only known once a wait frame arrived.
119
+ */
120
+ readonly expiresAt?: number;
121
+ }
122
+ /** One claim the hub holds while the host's ask is still open. */
123
+ export interface ClaimedQuestion extends ClaimedQuestionView {
124
+ /** The host's ask: the phone's answer, or whatever the next answerer says. */
125
+ readonly answer: Promise<QuestionAnswer>;
126
+ /**
127
+ * Epoch milliseconds at which the host's own deadline falls, from the first
128
+ * wait frame of this claim.
129
+ *
130
+ * Resolves undefined when there is no deadline to publish — a blocking ask, a
131
+ * failed subscription, or a stream that ended before its first frame — and
132
+ * never rejects: a phone must be shown the question whether or not the host
133
+ * would say how long it has.
134
+ */
135
+ readonly deadline: Promise<number | undefined>;
136
+ }
137
+ /** What one `question.answer` produced: acknowledgement, or the refusal to send. */
138
+ export type QuestionReply = {
139
+ readonly ok: true;
140
+ } | {
141
+ readonly ok: false;
142
+ readonly code: string;
143
+ readonly msg: string;
144
+ };
145
+ /** The code the host raises when its own foreground deadline ends the ask. */
146
+ export declare const QUESTION_TIMED_OUT_CODE = "ASK_TIMED_OUT";
147
+ /**
148
+ * Prefix of a call id this bridge minted for a question the host never named.
149
+ *
150
+ * A blocking ask has no host call id, but the phone still needs one opaque
151
+ * handle to answer with. The prefix keeps that handle in a namespace of its own,
152
+ * so a minted id can never be mistaken for a host `ToolCallId` — by this bridge
153
+ * or by anything reading the wire.
154
+ */
155
+ export declare const MINTED_CALL_ID_PREFIX = "bridge:";
156
+ /** Entropy of one minted call id. */
157
+ export declare const MINTED_CALL_ID_BYTES = 16;
158
+ /**
159
+ * How often at most a phone is told that the host's deadline moved, in
160
+ * milliseconds.
161
+ *
162
+ * The host re-reports its remaining wait on every touch of the wait, which can
163
+ * be far more often than a countdown needs; one update per second keeps the
164
+ * wire quiet without the phone ever showing a stale second.
165
+ */
166
+ export declare const QUESTION_WAIT_BROADCAST_MIN_INTERVAL_MS = 1000;
167
+ export interface OpenQuestionInput {
168
+ readonly sessionId: string;
169
+ /** The id published on the wire: the host's own, or one this bridge minted. */
170
+ readonly callId: string;
171
+ /**
172
+ * Whether `callId` is the host's own tool call identity.
173
+ *
174
+ * Only a hosted claim can ever be `continued`: the state is what a host
175
+ * deadline produces, and the late-answer path needs a real call id to address.
176
+ */
177
+ readonly hosted: boolean;
178
+ readonly questions: readonly QuestionItem[];
179
+ /** The rest of the answerer chain, used when the bridge withdraws. */
180
+ readonly next: () => Promise<QuestionAnswer>;
181
+ /** The host's own cancellation lifetime for this ask. */
182
+ readonly signal?: AbortSignal;
183
+ /** Called once when the host's deadline released the model but the question stayed answerable. */
184
+ readonly onContinued?: (question: ClaimedQuestionView) => void;
185
+ /**
186
+ * Called when a later wait frame moved the host's deadline, so a phone that
187
+ * already holds the card can update it in place.
188
+ *
189
+ * The first frame is not reported here: it is what {@link ClaimedQuestion.deadline}
190
+ * hands the hub for the question's own first broadcast, so reporting it would
191
+ * publish the same deadline twice.
192
+ */
193
+ readonly onDeadline?: (question: ClaimedQuestionView) => void;
194
+ /**
195
+ * Called when the claim leaves this bridge without the phone answering it — the
196
+ * host's own UI answered first, or the last paired client went away. The phone
197
+ * still holds a card for it and has to be told, or it keeps offering a question
198
+ * that no longer exists.
199
+ */
200
+ readonly onSettled?: (question: ClaimedQuestionView) => void;
201
+ }
202
+ export interface QuestionClaimsOptions {
203
+ readonly gateway: BridgeGateway;
204
+ /** The in-process owner peer every gateway call speaks for; the bridge never derives it from a client frame. */
205
+ readonly peer: GatewayPeer;
206
+ readonly log: (message: string) => void;
207
+ /** Id source, injectable so a test can answer the exact id it was given. */
208
+ readonly random?: (byteLength: number) => string;
209
+ /**
210
+ * Clock the deadline bookkeeping reads, injectable so a suite can move the
211
+ * remaining wait instead of sleeping through it.
212
+ */
213
+ readonly now?: () => number;
214
+ }
215
+ /**
216
+ * Project a host question into the contract's own fields.
217
+ *
218
+ * Explicitly field by field: the host's items carry presentation intents this
219
+ * protocol does not publish, and a payload that grew a key the contract does
220
+ * not declare would be a wire change nobody agreed to.
221
+ * @param items - the host's question batch.
222
+ * @returns the same questions in protocol vocabulary.
223
+ */
224
+ export declare function projectQuestions(items: readonly QuestionItem[]): readonly QuestionItem[];
225
+ /**
226
+ * Pending questions of one bridge process.
227
+ *
228
+ * One record per id published on the wire — the host's own, or one minted here
229
+ * for a blocking ask — and one settle for every path (answer, the host's
230
+ * deadline, the last phone leaving, bridge unload), so a question can never be
231
+ * settled twice and a withdrawn claim leaves no abort listener behind.
232
+ */
233
+ export declare class QuestionClaims {
234
+ #private;
235
+ constructor(options: QuestionClaimsOptions);
236
+ /** @returns how many questions this bridge currently holds. */
237
+ get size(): number;
238
+ /**
239
+ * Mint the opaque wire id of a question the host never named.
240
+ * @returns an id in this bridge's own namespace.
241
+ */
242
+ mintCallId(): string;
243
+ /** @returns the state of one claimed call, or undefined when it is not claimed. */
244
+ stateOf(callId: string): QuestionState | undefined;
245
+ /**
246
+ * Claim one question.
247
+ * @param input - session identity, wire call id and its provenance, questions, the rest of the chain, and the host's lifetime.
248
+ * @returns the claim the hub publishes, or undefined when this call id is already claimed.
249
+ */
250
+ open(input: OpenQuestionInput): ClaimedQuestion | undefined;
251
+ /**
252
+ * Answer one claimed question.
253
+ *
254
+ * An open claim is settled by resolving the host's own ask; a continued one
255
+ * only through the host's Remote `answer`, whose result and failures cross
256
+ * unchanged.
257
+ * @param sessionId - the session the client believes owns the question.
258
+ * @param callId - the wire call id the client was shown.
259
+ * @param answer - the complete answer batch.
260
+ */
261
+ answer(sessionId: string, callId: string, answer: QuestionAnswer): Promise<QuestionReply>;
262
+ /**
263
+ * Every claim still waiting on an answer.
264
+ *
265
+ * A phone that connects after the question was raised never saw the original
266
+ * broadcast, so it has to be handed the live claims again — otherwise the
267
+ * question sits on the host with an answerable card nowhere.
268
+ */
269
+ openClaims(): readonly ClaimedQuestionView[];
270
+ /**
271
+ * Withdraw the named questions, delegating each to the next answerer.
272
+ * @returns how many were still open and therefore delegated.
273
+ */
274
+ withdraw(callIds: readonly string[]): number;
275
+ /**
276
+ * Withdraw every claim, delegating each open question to the next answerer.
277
+ * Called when the last paired phone goes away and when the bridge unloads: a
278
+ * question nobody can answer must not hold the agent until its own deadline.
279
+ * @returns how many questions were still open.
280
+ */
281
+ withdrawAll(): number;
282
+ }
283
+ declare module '@deepseek-ai/cordis' {
284
+ interface Events {
285
+ /**
286
+ * Ask composed answerers for structured user input; the bridge claims it
287
+ * while a paired phone is connected, and delegates every other case. The
288
+ * parameters are the ones this package reads — the host's own declaration
289
+ * is not reachable from here.
290
+ * @mode waterfall
291
+ */
292
+ 'user-questions/request'(this: unknown, request: UserQuestionRequestLike, next: () => Promise<QuestionAnswer>): Promise<QuestionAnswer>;
293
+ }
294
+ }