dsh-browser-firefox 0.4.5 → 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/lib/index.js +347 -24
- package/lib/protocol.js +36 -11
- package/lib/types/approvals.d.ts +73 -0
- package/lib/types/extension-sessions.d.ts +79 -7
- package/lib/types/protocol.d.ts +29 -2
- package/lib/types/server.d.ts +18 -0
- package/package.json +36 -33
- package/src/approvals.ts +160 -0
- package/src/extension-sessions.ts +136 -11
- package/src/index.ts +127 -20
- package/src/protocol.ts +54 -11
- package/src/server.ts +56 -4
package/src/index.ts
CHANGED
|
@@ -31,7 +31,9 @@ import z from '@deepseek-ai/schemastery'
|
|
|
31
31
|
import type {} from '@deepseek-ai/dsh-tools'
|
|
32
32
|
import type {} from '@deepseek-ai/dsh-api-gateway/types'
|
|
33
33
|
import type {} from '@deepseek-ai/dsh-user-questions/types'
|
|
34
|
+
import type { ApprovalRequestEvent } from '@deepseek-ai/dsh-user-approval/types'
|
|
34
35
|
import type { WebRoute, WebUpgradeRoute } from '@deepseek-ai/dsh-host-webserver'
|
|
36
|
+
import { readFileSync } from 'node:fs'
|
|
35
37
|
import { dshHomePath } from '@deepseek-ai/dsh-home-paths'
|
|
36
38
|
import { BridgeServer } from './server.ts'
|
|
37
39
|
import { registerBrowserTools } from './tools.ts'
|
|
@@ -39,8 +41,9 @@ import { BRIDGE_CONFIG_PATH, BRIDGE_PATH, type RemoteWireResult } from './protoc
|
|
|
39
41
|
import { withSessionDeferral } from './session-deferral.ts'
|
|
40
42
|
import { withSessionWorkspace, type RpcDispatch } from './session-workspace.ts'
|
|
41
43
|
import { resolveToken } from './token.ts'
|
|
42
|
-
import { ExtensionSessionRegistry,
|
|
44
|
+
import { ExtensionSessionRegistry, FollowedSessions, resolveInteractionOwnership, type DelegateReason } from './extension-sessions.ts'
|
|
43
45
|
import { asQuestionAnswerArgs, QuestionBridge } from './questions.ts'
|
|
46
|
+
import { ApprovalBridge, asApprovalDecisionArgs } from './approvals.ts'
|
|
44
47
|
|
|
45
48
|
/** Cordis plugin name used by loader diagnostics. */
|
|
46
49
|
export const name = 'bridge-browser'
|
|
@@ -105,6 +108,22 @@ function sessionIdOf(value: unknown): string | undefined {
|
|
|
105
108
|
return typeof sessionId === 'string' && sessionId.length > 0 ? sessionId : undefined
|
|
106
109
|
}
|
|
107
110
|
|
|
111
|
+
/**
|
|
112
|
+
* Human-readable approval reason: the asker's localized text when present
|
|
113
|
+
* (zh first, then en), otherwise the raw reason string.
|
|
114
|
+
*
|
|
115
|
+
* @param request - pending host approval request.
|
|
116
|
+
* @returns display text, or undefined when the asker supplied none.
|
|
117
|
+
*/
|
|
118
|
+
function approvalReasonText(request: ApprovalRequestEvent): string | undefined {
|
|
119
|
+
const display = request.displayReason
|
|
120
|
+
if (display !== undefined) {
|
|
121
|
+
const localized = display.zh ?? display['zh-Hans'] ?? display.en
|
|
122
|
+
if (typeof localized === 'string' && localized.length > 0) return localized
|
|
123
|
+
}
|
|
124
|
+
return typeof request.reason === 'string' && request.reason.length > 0 ? request.reason : undefined
|
|
125
|
+
}
|
|
126
|
+
|
|
108
127
|
/**
|
|
109
128
|
* Apply defaults and direct-call validation at the plugin boundary.
|
|
110
129
|
* @param config - Loader-resolved or directly supplied plugin configuration.
|
|
@@ -162,23 +181,55 @@ export async function apply(ctx: Context, config: Config): Promise<void> {
|
|
|
162
181
|
),
|
|
163
182
|
resolved.deferSessionCreate,
|
|
164
183
|
)
|
|
165
|
-
// Ownership tracking for the
|
|
166
|
-
// extension-owned
|
|
167
|
-
//
|
|
168
|
-
//
|
|
184
|
+
// Ownership tracking for the interaction waterfalls (ask_user_question and
|
|
185
|
+
// host approvals). A session becomes extension-owned when a create/prompt is
|
|
186
|
+
// DISPATCHED over the bridge (noted optimistically so a question asked
|
|
187
|
+
// during the very first turn still finds an owner) and is released again
|
|
188
|
+
// when the dispatch fails hard — a failed prompt against a desktop session
|
|
189
|
+
// must not steal its cards away from the native answerer.
|
|
169
190
|
const extensionSessions = new ExtensionSessionRegistry()
|
|
191
|
+
const followedSessions = new FollowedSessions()
|
|
192
|
+
const ownershipSources = { driven: extensionSessions, followed: followedSessions }
|
|
193
|
+
// Subagent child sessions inherit their parent's ownership: a question asked
|
|
194
|
+
// by a delegated agent belongs in the conversation the user is watching.
|
|
195
|
+
const sessions = ctx.get('sessions')
|
|
196
|
+
const parentOf = sessions === undefined
|
|
197
|
+
? undefined
|
|
198
|
+
: (sessionId: string): string | undefined => sessions.get(sessionId as never)?.header.parentSession
|
|
170
199
|
const notingDispatchRpc: RpcDispatch = async (method, args, signal) => {
|
|
200
|
+
const tracksOwnership = method === 'session/create' || method === 'session/prompt'
|
|
201
|
+
const dispatched = tracksOwnership ? sessionIdOf(args.request) : undefined
|
|
202
|
+
if (dispatched !== undefined) extensionSessions.note(dispatched)
|
|
171
203
|
const result = await dispatchRpc(method, args, signal)
|
|
172
|
-
if (result.ok
|
|
173
|
-
|
|
174
|
-
|
|
204
|
+
if (!result.ok) {
|
|
205
|
+
// Only a lookup failure disproves the optimistic note (the session does
|
|
206
|
+
// not exist); transient failures keep the session extension-owned.
|
|
207
|
+
const code = result.error.code
|
|
208
|
+
if (dispatched !== undefined && (code.includes('not-found') || code.includes('session-invalid'))) {
|
|
209
|
+
extensionSessions.forget(dispatched)
|
|
210
|
+
}
|
|
211
|
+
return result
|
|
175
212
|
}
|
|
213
|
+
if (tracksOwnership) extensionSessions.note(sessionIdOf(result.value))
|
|
176
214
|
return result
|
|
177
215
|
}
|
|
216
|
+
// Plugin version for the hello.ok handshake (extension flags a mismatch in
|
|
217
|
+
// the sidebar). src/ and lib/ layouts both resolve to the same package.json.
|
|
218
|
+
let pluginVersion: string | undefined
|
|
219
|
+
try {
|
|
220
|
+
const pkg = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8')) as { version?: unknown }
|
|
221
|
+
pluginVersion = typeof pkg.version === 'string' ? pkg.version : undefined
|
|
222
|
+
} catch {
|
|
223
|
+
pluginVersion = undefined
|
|
224
|
+
}
|
|
225
|
+
|
|
178
226
|
const questions = new QuestionBridge({
|
|
179
227
|
// Late-bound through the closure: server is constructed right below.
|
|
180
228
|
push: (frame) => { server.push(frame) },
|
|
181
229
|
})
|
|
230
|
+
const approvals = new ApprovalBridge({
|
|
231
|
+
push: (frame) => { server.push(frame) },
|
|
232
|
+
})
|
|
182
233
|
const server = new BridgeServer({
|
|
183
234
|
token: tokenRes.token,
|
|
184
235
|
dispatchRpc: notingDispatchRpc,
|
|
@@ -194,7 +245,24 @@ export async function apply(ctx: Context, config: Config): Promise<void> {
|
|
|
194
245
|
? { accepted: false, reason: 'bad-answer' }
|
|
195
246
|
: questions.answer(parsed)
|
|
196
247
|
},
|
|
197
|
-
|
|
248
|
+
answerApproval: (args) => {
|
|
249
|
+
const parsed = asApprovalDecisionArgs(args)
|
|
250
|
+
return parsed === undefined
|
|
251
|
+
? { accepted: false, reason: 'bad-decision' }
|
|
252
|
+
: approvals.decide(parsed)
|
|
253
|
+
},
|
|
254
|
+
onConnectionLost: () => {
|
|
255
|
+
questions.delegateAll()
|
|
256
|
+
approvals.delegateAll()
|
|
257
|
+
},
|
|
258
|
+
onFollowChanged: (sessionId, open) => {
|
|
259
|
+
if (open) followedSessions.open(sessionId)
|
|
260
|
+
else followedSessions.close(sessionId)
|
|
261
|
+
},
|
|
262
|
+
version: pluginVersion,
|
|
263
|
+
onVersionMismatch: (clientVersion) => {
|
|
264
|
+
ctx.logger.warn(`browser extension version ${clientVersion} != plugin version ${pluginVersion ?? '?'}; both components must be updated together`)
|
|
265
|
+
},
|
|
198
266
|
toolTimeoutMs: resolved.toolTimeoutMs,
|
|
199
267
|
caps: {
|
|
200
268
|
snapshotMaxChars: resolved.snapshotMaxChars,
|
|
@@ -203,22 +271,29 @@ export async function apply(ctx: Context, config: Config): Promise<void> {
|
|
|
203
271
|
})
|
|
204
272
|
|
|
205
273
|
// Sidebar ask_user_question: claim the waterfall only for sessions the
|
|
206
|
-
// extension owns (
|
|
207
|
-
// question-capable extension is
|
|
208
|
-
// native answerer chain (dsh web).
|
|
209
|
-
// Remote event forwarding listener
|
|
274
|
+
// extension owns (driven over the bridge, currently followed, or a subagent
|
|
275
|
+
// child of an owned session) while a question-capable extension is
|
|
276
|
+
// connected; everything else keeps the native answerer chain (dsh web).
|
|
277
|
+
// Prepended so the claim runs before the Remote event forwarding listener
|
|
278
|
+
// in dsh-api-remotes. Delegations are logged with the failing gate so an
|
|
279
|
+
// "invisible card" report is diagnosable from the host log.
|
|
280
|
+
const logDelegation = (kind: 'user-questions' | 'approval', sessionId: string | undefined, reason: DelegateReason): void => {
|
|
281
|
+
ctx.logger.info(`browser bridge: delegated ${kind} for session ${sessionId ?? '<unknown>'} (${reason})`)
|
|
282
|
+
}
|
|
210
283
|
ctx.effect(() => {
|
|
211
284
|
const disposeListener = ctx.on('user-questions/request', (request, next) => {
|
|
212
|
-
const
|
|
213
|
-
if (sessionId === undefined || !shouldBridgeOwnQuestion({
|
|
285
|
+
const ownership = resolveInteractionOwnership({
|
|
214
286
|
hasExtensionConnection: server.hasConnection(),
|
|
215
|
-
|
|
216
|
-
sessionId,
|
|
217
|
-
|
|
218
|
-
|
|
287
|
+
clientSupports: server.clientSupportsQuestions(),
|
|
288
|
+
sessionId: request.agent?.id,
|
|
289
|
+
sources: ownershipSources,
|
|
290
|
+
...(parentOf === undefined ? {} : { parentOf }),
|
|
291
|
+
})
|
|
292
|
+
if (!ownership.owned) {
|
|
293
|
+
logDelegation('user-questions', request.agent?.id, ownership.reason)
|
|
219
294
|
return next()
|
|
220
295
|
}
|
|
221
|
-
return questions.ask(sessionId, request.questions, next, request.signal)
|
|
296
|
+
return questions.ask(ownership.sessionId, request.questions, next, request.signal)
|
|
222
297
|
}, { prepend: true })
|
|
223
298
|
return () => {
|
|
224
299
|
disposeListener()
|
|
@@ -226,6 +301,38 @@ export async function apply(ctx: Context, config: Config): Promise<void> {
|
|
|
226
301
|
}
|
|
227
302
|
}, 'bridge-browser: user-questions answerer')
|
|
228
303
|
|
|
304
|
+
// Host approvals (a hooked tool call / permission ask): same ownership rule,
|
|
305
|
+
// same sidebar surface, so the user is not sent to dsh web for a decision.
|
|
306
|
+
ctx.effect(() => {
|
|
307
|
+
const disposeListener = ctx.on('approval/request', (request, next) => {
|
|
308
|
+
const ownership = resolveInteractionOwnership({
|
|
309
|
+
hasExtensionConnection: server.hasConnection(),
|
|
310
|
+
clientSupports: server.clientSupportsApprovals(),
|
|
311
|
+
sessionId: request.agent?.id,
|
|
312
|
+
sources: ownershipSources,
|
|
313
|
+
...(parentOf === undefined ? {} : { parentOf }),
|
|
314
|
+
})
|
|
315
|
+
if (!ownership.owned) {
|
|
316
|
+
logDelegation('approval', request.agent?.id, ownership.reason)
|
|
317
|
+
return next()
|
|
318
|
+
}
|
|
319
|
+
return approvals.ask(
|
|
320
|
+
ownership.sessionId,
|
|
321
|
+
{
|
|
322
|
+
toolName: request.toolName,
|
|
323
|
+
...(typeof request.callId === 'string' ? { callId: request.callId } : {}),
|
|
324
|
+
...(approvalReasonText(request) === undefined ? {} : { reason: approvalReasonText(request) as string }),
|
|
325
|
+
},
|
|
326
|
+
next,
|
|
327
|
+
request.signal,
|
|
328
|
+
)
|
|
329
|
+
}, { prepend: true })
|
|
330
|
+
return () => {
|
|
331
|
+
disposeListener()
|
|
332
|
+
approvals.dispose()
|
|
333
|
+
}
|
|
334
|
+
}, 'bridge-browser: approval answerer')
|
|
335
|
+
|
|
229
336
|
const route: WebUpgradeRoute = {
|
|
230
337
|
path: BRIDGE_PATH,
|
|
231
338
|
handler: (req, socket, head) => { server.handleUpgrade(req, socket, head) },
|
package/src/protocol.ts
CHANGED
|
@@ -68,6 +68,8 @@ export interface BridgeCaps {
|
|
|
68
68
|
maxInteractiveItems: number
|
|
69
69
|
/** Client advertises it can render ask_user_question cards; the plugin only claims the user-questions waterfall for extension-owned sessions when set. */
|
|
70
70
|
questions?: boolean
|
|
71
|
+
/** Client advertises it can render host approval (permission) cards; the plugin only claims the approval waterfall for extension-owned sessions when set. */
|
|
72
|
+
approvals?: boolean
|
|
71
73
|
}
|
|
72
74
|
|
|
73
75
|
/** One Remote failure as plain wire data (RemoteError's message is non-enumerable, so it is restated explicitly). */
|
|
@@ -103,8 +105,8 @@ export interface QuestionItem {
|
|
|
103
105
|
|
|
104
106
|
/** Frames sent by the extension to the bridge plugin. */
|
|
105
107
|
export type ClientFrame =
|
|
106
|
-
/** First frame, within HELLO_TIMEOUT_MS of socket open. */
|
|
107
|
-
| { t: 'hello'; token: string; caps: BridgeCaps }
|
|
108
|
+
/** First frame, within HELLO_TIMEOUT_MS of socket open. `version` (extension version) lets the plugin detect a version mismatch; absent on pre-0.4.6 clients. */
|
|
109
|
+
| { t: 'hello'; token: string; caps: BridgeCaps; version?: string }
|
|
108
110
|
/** Unary Remote call: slash-joined endpoint plus the descriptor's native args object. */
|
|
109
111
|
| { t: 'rpc'; id: string; method: string; args: Record<string, unknown> }
|
|
110
112
|
/** Open one Remote stream (`session/follow`, `workspace/follow`); frames arrive as `stream.frame`. */
|
|
@@ -119,8 +121,8 @@ export type ClientFrame =
|
|
|
119
121
|
|
|
120
122
|
/** Frames sent by the bridge plugin to the extension. */
|
|
121
123
|
export type ServerFrame =
|
|
122
|
-
/** Accepted after a valid `hello`. */
|
|
123
|
-
| { t: 'hello.ok'; caps: BridgeCaps }
|
|
124
|
+
/** Accepted after a valid `hello`. `version` (plugin version) lets the extension detect a version mismatch; absent on pre-0.4.6 servers. */
|
|
125
|
+
| { t: 'hello.ok'; caps: BridgeCaps; version?: string }
|
|
124
126
|
/** Reply to an `rpc` frame: the RemoteResult wire form, flattened onto the frame. */
|
|
125
127
|
| { t: 'rpc.result'; id: string; ok: true; value: unknown }
|
|
126
128
|
| { t: 'rpc.result'; id: string; ok: false; error: RemoteWireFailure }
|
|
@@ -143,6 +145,18 @@ export type ServerFrame =
|
|
|
143
145
|
| { t: 'question.requested'; id: string; sessionId: string; questions: QuestionItem[] }
|
|
144
146
|
/** A previously pushed question is gone (host aborted it or the bridge delegated the waterfall elsewhere); dismiss the card without answering. */
|
|
145
147
|
| { t: 'question.resolved'; id: string; sessionId: string }
|
|
148
|
+
/**
|
|
149
|
+
* The host needs one approval decision (a hooked tool call, a permission
|
|
150
|
+
* ask) in an extension-owned session and the bridge claimed the waterfall:
|
|
151
|
+
* the extension should render an approval card. The decision rides a normal
|
|
152
|
+
* `rpc` frame with the plugin-local method `bridge/approvalDecision` (args
|
|
153
|
+
* `{approvalId, sessionId, decision: 'allowed-once' | 'rejected'}`),
|
|
154
|
+
* answered `{accepted: true} | {accepted: false, reason: 'not-pending' |
|
|
155
|
+
* 'bad-decision'}`.
|
|
156
|
+
*/
|
|
157
|
+
| { t: 'approval.requested'; id: string; sessionId: string; toolName: string; callId?: string; reason?: string }
|
|
158
|
+
/** A previously pushed approval is gone (host cancelled it or the bridge delegated the waterfall elsewhere); dismiss the card without deciding. */
|
|
159
|
+
| { t: 'approval.resolved'; id: string; sessionId: string }
|
|
146
160
|
/** Liveness probe. */
|
|
147
161
|
| { t: 'ping' }
|
|
148
162
|
/** Fatal connection error; the client should re-authenticate. */
|
|
@@ -167,6 +181,8 @@ export function isServerFrame(frame: BridgeFrame): frame is ServerFrame {
|
|
|
167
181
|
|| frame.t === 'tool.cancel'
|
|
168
182
|
|| frame.t === 'question.requested'
|
|
169
183
|
|| frame.t === 'question.resolved'
|
|
184
|
+
|| frame.t === 'approval.requested'
|
|
185
|
+
|| frame.t === 'approval.resolved'
|
|
170
186
|
|| frame.t === 'ping'
|
|
171
187
|
|| frame.t === 'error'
|
|
172
188
|
}
|
|
@@ -208,10 +224,14 @@ export function parseBridgeFrame(text: string): BridgeFrame | undefined {
|
|
|
208
224
|
if (typeof frame.t !== 'string') return undefined
|
|
209
225
|
switch (frame.t) {
|
|
210
226
|
case 'hello':
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
:
|
|
227
|
+
if (typeof frame.token !== 'string' || !isCaps(frame.caps)) return undefined
|
|
228
|
+
if (frame.version !== undefined && typeof frame.version !== 'string') return undefined
|
|
229
|
+
return {
|
|
230
|
+
t: 'hello',
|
|
231
|
+
token: frame.token,
|
|
232
|
+
caps: frame.caps,
|
|
233
|
+
...(frame.version === undefined ? {} : { version: frame.version as string }),
|
|
234
|
+
}
|
|
215
235
|
case 'rpc':
|
|
216
236
|
return typeof frame.id === 'string' && typeof frame.method === 'string' && isArgs(frame.args)
|
|
217
237
|
? { t: 'rpc', id: frame.id, method: frame.method, args: frame.args }
|
|
@@ -233,9 +253,13 @@ export function parseBridgeFrame(text: string): BridgeFrame | undefined {
|
|
|
233
253
|
case 'pong':
|
|
234
254
|
return { t: 'pong' }
|
|
235
255
|
case 'hello.ok':
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
256
|
+
if (!isCaps(frame.caps)) return undefined
|
|
257
|
+
if (frame.version !== undefined && typeof frame.version !== 'string') return undefined
|
|
258
|
+
return {
|
|
259
|
+
t: 'hello.ok',
|
|
260
|
+
caps: frame.caps,
|
|
261
|
+
...(frame.version === undefined ? {} : { version: frame.version as string }),
|
|
262
|
+
}
|
|
239
263
|
case 'rpc.result':
|
|
240
264
|
if (typeof frame.id !== 'string') return undefined
|
|
241
265
|
if (frame.ok === true && 'value' in frame) {
|
|
@@ -282,6 +306,24 @@ export function parseBridgeFrame(text: string): BridgeFrame | undefined {
|
|
|
282
306
|
return typeof frame.id === 'string' && typeof frame.sessionId === 'string'
|
|
283
307
|
? { t: 'question.resolved', id: frame.id, sessionId: frame.sessionId }
|
|
284
308
|
: undefined
|
|
309
|
+
case 'approval.requested':
|
|
310
|
+
if (typeof frame.id !== 'string' || typeof frame.sessionId !== 'string' || typeof frame.toolName !== 'string') {
|
|
311
|
+
return undefined
|
|
312
|
+
}
|
|
313
|
+
if (frame.callId !== undefined && typeof frame.callId !== 'string') return undefined
|
|
314
|
+
if (frame.reason !== undefined && typeof frame.reason !== 'string') return undefined
|
|
315
|
+
return {
|
|
316
|
+
t: 'approval.requested',
|
|
317
|
+
id: frame.id,
|
|
318
|
+
sessionId: frame.sessionId,
|
|
319
|
+
toolName: frame.toolName,
|
|
320
|
+
...(typeof frame.callId === 'string' ? { callId: frame.callId } : {}),
|
|
321
|
+
...(typeof frame.reason === 'string' ? { reason: frame.reason } : {}),
|
|
322
|
+
}
|
|
323
|
+
case 'approval.resolved':
|
|
324
|
+
return typeof frame.id === 'string' && typeof frame.sessionId === 'string'
|
|
325
|
+
? { t: 'approval.resolved', id: frame.id, sessionId: frame.sessionId }
|
|
326
|
+
: undefined
|
|
285
327
|
case 'ping':
|
|
286
328
|
return { t: 'ping' }
|
|
287
329
|
case 'error':
|
|
@@ -301,6 +343,7 @@ function isCaps(value: unknown): value is BridgeCaps {
|
|
|
301
343
|
return typeof caps.snapshotMaxChars === 'number' && caps.snapshotMaxChars > 0
|
|
302
344
|
&& typeof caps.maxInteractiveItems === 'number' && caps.maxInteractiveItems > 0
|
|
303
345
|
&& (caps.questions === undefined || typeof caps.questions === 'boolean')
|
|
346
|
+
&& (caps.approvals === undefined || typeof caps.approvals === 'boolean')
|
|
304
347
|
}
|
|
305
348
|
|
|
306
349
|
/** Narrow one question item; unknown extra keys (e.g. host `intent`) pass through. */
|
package/src/server.ts
CHANGED
|
@@ -119,6 +119,14 @@ function orderedSessionId(frame: Extract<ClientFrame, { t: 'rpc' }>): string | u
|
|
|
119
119
|
return typeof sessionId === 'string' ? sessionId : undefined
|
|
120
120
|
}
|
|
121
121
|
|
|
122
|
+
/** Session id carried by a `session/follow` stream's native args. */
|
|
123
|
+
function followedSessionOf(args: Record<string, unknown>): string | undefined {
|
|
124
|
+
const request = args.request
|
|
125
|
+
if (typeof request !== 'object' || request === null || Array.isArray(request)) return undefined
|
|
126
|
+
const sessionId = (request as Record<string, unknown>).sessionId
|
|
127
|
+
return typeof sessionId === 'string' && sessionId.length > 0 ? sessionId : undefined
|
|
128
|
+
}
|
|
129
|
+
|
|
122
130
|
/** Loopback IPv4/IPv6 literals (IPv4-mapped included). Exported for tests and reuse. */
|
|
123
131
|
export function isLoopbackAddress(address: string | undefined): boolean {
|
|
124
132
|
return address === '127.0.0.1' || address === '::1' || address === '::ffff:127.0.0.1'
|
|
@@ -151,12 +159,28 @@ export interface BridgeServerDeps {
|
|
|
151
159
|
toolTimeoutMs: number
|
|
152
160
|
/** Capabilities to echo in `hello.ok` (negotiated snapshot budgets). */
|
|
153
161
|
caps: BridgeCaps
|
|
162
|
+
/** Plugin version echoed in `hello.ok` so the extension can flag a mismatch (0.4.6+). */
|
|
163
|
+
version?: string | undefined
|
|
164
|
+
/** An authenticated client arrived with a version different from ours. */
|
|
165
|
+
onVersionMismatch?: (clientVersion: string) => void
|
|
154
166
|
/**
|
|
155
167
|
* Plugin-local endpoint `bridge/questionAnswer`: settle one pending
|
|
156
168
|
* ask_user_question the bridge pushed to this extension. Answered as the
|
|
157
169
|
* rpc result value `{accepted: true} | {accepted: false, reason}`.
|
|
158
170
|
*/
|
|
159
171
|
answerQuestion: (args: Record<string, unknown>) => unknown
|
|
172
|
+
/**
|
|
173
|
+
* Plugin-local endpoint `bridge/approvalDecision`: settle one pending host
|
|
174
|
+
* approval the bridge pushed to this extension. Answered as the rpc result
|
|
175
|
+
* value `{accepted: true} | {accepted: false, reason}`.
|
|
176
|
+
*/
|
|
177
|
+
answerApproval: (args: Record<string, unknown>) => unknown
|
|
178
|
+
/**
|
|
179
|
+
* A `session/follow` stream opened (`open: true`) or ended (`open: false`)
|
|
180
|
+
* for a session: the sidebar is displaying that session while the stream
|
|
181
|
+
* lives, so interaction ownership can follow the visible conversation.
|
|
182
|
+
*/
|
|
183
|
+
onFollowChanged?: (sessionId: string, open: boolean) => void
|
|
160
184
|
/** The active connection was replaced or closed (settle pushed questions). */
|
|
161
185
|
onConnectionLost?: () => void
|
|
162
186
|
/**
|
|
@@ -190,6 +214,8 @@ interface PendingTool {
|
|
|
190
214
|
interface OpenStream {
|
|
191
215
|
abort: AbortController
|
|
192
216
|
pump: Promise<void>
|
|
217
|
+
/** Session a `session/follow` stream is displaying, for interaction ownership. */
|
|
218
|
+
followedSessionId?: string
|
|
193
219
|
}
|
|
194
220
|
|
|
195
221
|
/** A socket that passed authentication and owns the single active slot. */
|
|
@@ -367,6 +393,11 @@ export class BridgeServer {
|
|
|
367
393
|
return this.current?.clientCaps.questions === true
|
|
368
394
|
}
|
|
369
395
|
|
|
396
|
+
/** @returns whether the connected extension advertised approval-card support. */
|
|
397
|
+
clientSupportsApprovals(): boolean {
|
|
398
|
+
return this.current?.clientCaps.approvals === true
|
|
399
|
+
}
|
|
400
|
+
|
|
370
401
|
/**
|
|
371
402
|
* Push one unsolicited server frame (question.requested/question.resolved)
|
|
372
403
|
* to the connected extension. No-op when no extension is connected or the
|
|
@@ -415,7 +446,7 @@ export class BridgeServer {
|
|
|
415
446
|
}
|
|
416
447
|
clearTimeout(helloTimer)
|
|
417
448
|
helloTimer = undefined
|
|
418
|
-
this.promote(ws, remoteAddress, frame.caps)
|
|
449
|
+
this.promote(ws, remoteAddress, frame.caps, frame.version)
|
|
419
450
|
return
|
|
420
451
|
}
|
|
421
452
|
this.handleReadyFrame(frame)
|
|
@@ -430,8 +461,11 @@ export class BridgeServer {
|
|
|
430
461
|
}
|
|
431
462
|
|
|
432
463
|
/** Promote an authenticated socket to the single active slot. */
|
|
433
|
-
private promote(ws: WebSocket, remoteAddress: string | undefined, clientCaps: BridgeCaps): void {
|
|
464
|
+
private promote(ws: WebSocket, remoteAddress: string | undefined, clientCaps: BridgeCaps, clientVersion?: string): void {
|
|
434
465
|
this.replaceConnection()
|
|
466
|
+
if (clientVersion !== undefined && this.deps.version !== undefined && clientVersion !== this.deps.version) {
|
|
467
|
+
this.deps.onVersionMismatch?.(clientVersion)
|
|
468
|
+
}
|
|
435
469
|
const abort = new AbortController()
|
|
436
470
|
const intervalMs = this.deps.pingIntervalMs ?? PING_INTERVAL_MS
|
|
437
471
|
const allowed = this.deps.missedPongsAllowed ?? DEFAULT_MISSED_PONGS_ALLOWED
|
|
@@ -448,7 +482,11 @@ export class BridgeServer {
|
|
|
448
482
|
}, intervalMs)
|
|
449
483
|
conn.ping = ping
|
|
450
484
|
this.current = conn
|
|
451
|
-
sendFrame(ws, {
|
|
485
|
+
sendFrame(ws, {
|
|
486
|
+
t: 'hello.ok',
|
|
487
|
+
caps: this.deps.caps,
|
|
488
|
+
...(this.deps.version === undefined ? {} : { version: this.deps.version }),
|
|
489
|
+
})
|
|
452
490
|
ws.once('close', () => {
|
|
453
491
|
clearInterval(ping)
|
|
454
492
|
abort.abort()
|
|
@@ -535,6 +573,10 @@ export class BridgeServer {
|
|
|
535
573
|
sendFrame(conn.ws, { t: 'rpc.result', id: frame.id, ok: true, value: this.deps.answerQuestion(frame.args) })
|
|
536
574
|
return
|
|
537
575
|
}
|
|
576
|
+
if (frame.method === 'bridge/approvalDecision') {
|
|
577
|
+
sendFrame(conn.ws, { t: 'rpc.result', id: frame.id, ok: true, value: this.deps.answerApproval(frame.args) })
|
|
578
|
+
return
|
|
579
|
+
}
|
|
538
580
|
try {
|
|
539
581
|
const result = await this.deps.dispatchRpc(frame.method, frame.args, conn.abort.signal)
|
|
540
582
|
if (result.ok) {
|
|
@@ -561,6 +603,10 @@ export class BridgeServer {
|
|
|
561
603
|
const ws = conn.ws
|
|
562
604
|
const abort = new AbortController()
|
|
563
605
|
conn.abort.signal.addEventListener('abort', () => { abort.abort() }, { once: true })
|
|
606
|
+
// A session/follow stream means the sidebar is displaying this session:
|
|
607
|
+
// report the lifetime so interaction ownership can follow the visible
|
|
608
|
+
// conversation (released in the pump's finally below).
|
|
609
|
+
const followedSessionId = frame.method === 'session/follow' ? followedSessionOf(frame.args) : undefined
|
|
564
610
|
let pump!: Promise<void>
|
|
565
611
|
pump = (async () => {
|
|
566
612
|
let iterator: AsyncIterator<unknown> | undefined
|
|
@@ -592,9 +638,15 @@ export class BridgeServer {
|
|
|
592
638
|
if (current !== null && current.streams.get(frame.id)?.pump === pump) {
|
|
593
639
|
current.streams.delete(frame.id)
|
|
594
640
|
}
|
|
641
|
+
if (followedSessionId !== undefined) this.deps.onFollowChanged?.(followedSessionId, false)
|
|
595
642
|
}
|
|
596
643
|
})()
|
|
597
|
-
conn.streams.set(frame.id, {
|
|
644
|
+
conn.streams.set(frame.id, {
|
|
645
|
+
abort,
|
|
646
|
+
pump,
|
|
647
|
+
...(followedSessionId === undefined ? {} : { followedSessionId }),
|
|
648
|
+
})
|
|
649
|
+
if (followedSessionId !== undefined) this.deps.onFollowChanged?.(followedSessionId, true)
|
|
598
650
|
}
|
|
599
651
|
|
|
600
652
|
/**
|