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/lib/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { BRIDGE_CONFIG_PATH, BRIDGE_PATH, parseBridgeFrame } from "./protocol.js";
|
|
2
2
|
import z from "@deepseek-ai/schemastery";
|
|
3
|
+
import { readFileSync } from "node:fs";
|
|
3
4
|
import { dshHomePath } from "@deepseek-ai/dsh-home-paths";
|
|
4
5
|
import { randomBytes, randomUUID, timingSafeEqual } from "node:crypto";
|
|
5
6
|
import { WebSocket, WebSocketServer } from "ws";
|
|
@@ -194,6 +195,13 @@ function orderedSessionId(frame) {
|
|
|
194
195
|
const sessionId = request.sessionId;
|
|
195
196
|
return typeof sessionId === "string" ? sessionId : void 0;
|
|
196
197
|
}
|
|
198
|
+
/** Session id carried by a `session/follow` stream's native args. */
|
|
199
|
+
function followedSessionOf(args) {
|
|
200
|
+
const request = args.request;
|
|
201
|
+
if (typeof request !== "object" || request === null || Array.isArray(request)) return void 0;
|
|
202
|
+
const sessionId = request.sessionId;
|
|
203
|
+
return typeof sessionId === "string" && sessionId.length > 0 ? sessionId : void 0;
|
|
204
|
+
}
|
|
197
205
|
/** Loopback IPv4/IPv6 literals (IPv4-mapped included). Exported for tests and reuse. */
|
|
198
206
|
function isLoopbackAddress(address) {
|
|
199
207
|
return address === "127.0.0.1" || address === "::1" || address === "::ffff:127.0.0.1";
|
|
@@ -346,6 +354,10 @@ var BridgeServer = class {
|
|
|
346
354
|
clientSupportsQuestions() {
|
|
347
355
|
return this.current?.clientCaps.questions === true;
|
|
348
356
|
}
|
|
357
|
+
/** @returns whether the connected extension advertised approval-card support. */
|
|
358
|
+
clientSupportsApprovals() {
|
|
359
|
+
return this.current?.clientCaps.approvals === true;
|
|
360
|
+
}
|
|
349
361
|
/**
|
|
350
362
|
* Push one unsolicited server frame (question.requested/question.resolved)
|
|
351
363
|
* to the connected extension. No-op when no extension is connected or the
|
|
@@ -377,7 +389,7 @@ var BridgeServer = class {
|
|
|
377
389
|
}
|
|
378
390
|
clearTimeout(helloTimer);
|
|
379
391
|
helloTimer = void 0;
|
|
380
|
-
this.promote(ws, remoteAddress, frame.caps);
|
|
392
|
+
this.promote(ws, remoteAddress, frame.caps, frame.version);
|
|
381
393
|
return;
|
|
382
394
|
}
|
|
383
395
|
this.handleReadyFrame(frame);
|
|
@@ -391,8 +403,9 @@ var BridgeServer = class {
|
|
|
391
403
|
ws.once("error", onClose);
|
|
392
404
|
}
|
|
393
405
|
/** Promote an authenticated socket to the single active slot. */
|
|
394
|
-
promote(ws, remoteAddress, clientCaps) {
|
|
406
|
+
promote(ws, remoteAddress, clientCaps, clientVersion) {
|
|
395
407
|
this.replaceConnection();
|
|
408
|
+
if (clientVersion !== void 0 && this.deps.version !== void 0 && clientVersion !== this.deps.version) this.deps.onVersionMismatch?.(clientVersion);
|
|
396
409
|
const abort = new AbortController();
|
|
397
410
|
const intervalMs = this.deps.pingIntervalMs ?? 3e4;
|
|
398
411
|
const allowed = this.deps.missedPongsAllowed ?? DEFAULT_MISSED_PONGS_ALLOWED;
|
|
@@ -416,7 +429,8 @@ var BridgeServer = class {
|
|
|
416
429
|
this.current = conn;
|
|
417
430
|
sendFrame(ws, {
|
|
418
431
|
t: "hello.ok",
|
|
419
|
-
caps: this.deps.caps
|
|
432
|
+
caps: this.deps.caps,
|
|
433
|
+
...this.deps.version === void 0 ? {} : { version: this.deps.version }
|
|
420
434
|
});
|
|
421
435
|
ws.once("close", () => {
|
|
422
436
|
clearInterval(ping);
|
|
@@ -503,6 +517,15 @@ var BridgeServer = class {
|
|
|
503
517
|
});
|
|
504
518
|
return;
|
|
505
519
|
}
|
|
520
|
+
if (frame.method === "bridge/approvalDecision") {
|
|
521
|
+
sendFrame(conn.ws, {
|
|
522
|
+
t: "rpc.result",
|
|
523
|
+
id: frame.id,
|
|
524
|
+
ok: true,
|
|
525
|
+
value: this.deps.answerApproval(frame.args)
|
|
526
|
+
});
|
|
527
|
+
return;
|
|
528
|
+
}
|
|
506
529
|
try {
|
|
507
530
|
const result = await this.deps.dispatchRpc(frame.method, frame.args, conn.abort.signal);
|
|
508
531
|
if (result.ok) sendFrame(conn.ws, {
|
|
@@ -547,6 +570,7 @@ var BridgeServer = class {
|
|
|
547
570
|
conn.abort.signal.addEventListener("abort", () => {
|
|
548
571
|
abort.abort();
|
|
549
572
|
}, { once: true });
|
|
573
|
+
const followedSessionId = frame.method === "session/follow" ? followedSessionOf(frame.args) : void 0;
|
|
550
574
|
let pump;
|
|
551
575
|
pump = (async () => {
|
|
552
576
|
let iterator;
|
|
@@ -575,12 +599,15 @@ var BridgeServer = class {
|
|
|
575
599
|
} catch {}
|
|
576
600
|
const current = this.current;
|
|
577
601
|
if (current !== null && current.streams.get(frame.id)?.pump === pump) current.streams.delete(frame.id);
|
|
602
|
+
if (followedSessionId !== void 0) this.deps.onFollowChanged?.(followedSessionId, false);
|
|
578
603
|
}
|
|
579
604
|
})();
|
|
580
605
|
conn.streams.set(frame.id, {
|
|
581
606
|
abort,
|
|
582
|
-
pump
|
|
607
|
+
pump,
|
|
608
|
+
...followedSessionId === void 0 ? {} : { followedSessionId }
|
|
583
609
|
});
|
|
610
|
+
if (followedSessionId !== void 0) this.deps.onFollowChanged?.(followedSessionId, true);
|
|
584
611
|
}
|
|
585
612
|
/**
|
|
586
613
|
* Backpressure gate for stream pumps. Returns false when the wait ended
|
|
@@ -1240,9 +1267,19 @@ function withSessionWorkspace(dispatch, workspacePath, warn) {
|
|
|
1240
1267
|
//#region lib/types/extension-sessions.js
|
|
1241
1268
|
/**
|
|
1242
1269
|
* Track session ids that the browser extension has driven through the bridge.
|
|
1243
|
-
* Sessions the extension never touched must keep the host
|
|
1244
|
-
*
|
|
1245
|
-
*
|
|
1270
|
+
* Sessions the extension never touched must keep the host waterfalls
|
|
1271
|
+
* (user-questions, approvals) so the dsh web UI (or any other answerer)
|
|
1272
|
+
* renders the cards.
|
|
1273
|
+
*
|
|
1274
|
+
* Ownership has three sources, all revocable:
|
|
1275
|
+
* - **driven**: the extension created or prompted the session (kept until the
|
|
1276
|
+
* plugin unloads);
|
|
1277
|
+
* - **followed**: an open `session/follow` stream means the sidebar is
|
|
1278
|
+
* displaying that session right now (released when the stream closes);
|
|
1279
|
+
* - **inherited**: an ask from a subagent child session belongs to the
|
|
1280
|
+
* nearest extension-owned ancestor, so child questions/approvals surface in
|
|
1281
|
+
* the parent conversation the user is actually watching.
|
|
1282
|
+
*
|
|
1246
1283
|
* @module
|
|
1247
1284
|
*/
|
|
1248
1285
|
/** Mutable registry of extension-owned session ids. */
|
|
@@ -1252,6 +1289,10 @@ var ExtensionSessionRegistry = class {
|
|
|
1252
1289
|
note(sessionId) {
|
|
1253
1290
|
if (typeof sessionId === "string" && sessionId.length > 0) this.ids.add(sessionId);
|
|
1254
1291
|
}
|
|
1292
|
+
/** Forget a session (a failed dispatch revoked an optimistic note). */
|
|
1293
|
+
forget(sessionId) {
|
|
1294
|
+
if (typeof sessionId === "string") this.ids.delete(sessionId);
|
|
1295
|
+
}
|
|
1255
1296
|
/** Whether the extension has touched this session over the bridge. */
|
|
1256
1297
|
has(sessionId) {
|
|
1257
1298
|
return typeof sessionId === "string" && this.ids.has(sessionId);
|
|
@@ -1262,14 +1303,89 @@ var ExtensionSessionRegistry = class {
|
|
|
1262
1303
|
}
|
|
1263
1304
|
};
|
|
1264
1305
|
/**
|
|
1265
|
-
*
|
|
1266
|
-
*
|
|
1267
|
-
*
|
|
1268
|
-
*
|
|
1269
|
-
|
|
1306
|
+
* Live `session/follow` subscriptions: the sidebar is displaying these
|
|
1307
|
+
* sessions, so they count as extension-owned while the stream is open.
|
|
1308
|
+
* Reference-counted per session because one session can be followed by more
|
|
1309
|
+
* than one panel connection.
|
|
1310
|
+
*/
|
|
1311
|
+
var FollowedSessions = class {
|
|
1312
|
+
counts = /* @__PURE__ */ new Map();
|
|
1313
|
+
/** Register one open follow stream for a session. */
|
|
1314
|
+
open(sessionId) {
|
|
1315
|
+
if (typeof sessionId !== "string" || sessionId.length === 0) return;
|
|
1316
|
+
this.counts.set(sessionId, (this.counts.get(sessionId) ?? 0) + 1);
|
|
1317
|
+
}
|
|
1318
|
+
/** Release one follow stream; the last release drops the session. */
|
|
1319
|
+
close(sessionId) {
|
|
1320
|
+
if (typeof sessionId !== "string") return;
|
|
1321
|
+
const current = this.counts.get(sessionId);
|
|
1322
|
+
if (current === void 0) return;
|
|
1323
|
+
if (current <= 1) this.counts.delete(sessionId);
|
|
1324
|
+
else this.counts.set(sessionId, current - 1);
|
|
1325
|
+
}
|
|
1326
|
+
/** Whether any open follow stream is displaying this session. */
|
|
1327
|
+
has(sessionId) {
|
|
1328
|
+
return typeof sessionId === "string" && this.counts.has(sessionId);
|
|
1329
|
+
}
|
|
1330
|
+
/** Drop every subscription (bridge generation replacement, plugin unload). */
|
|
1331
|
+
clear() {
|
|
1332
|
+
this.counts.clear();
|
|
1333
|
+
}
|
|
1334
|
+
};
|
|
1335
|
+
/** Whether one exact session id is extension-owned by either source. */
|
|
1336
|
+
function isOwnedSession(sessionId, sources) {
|
|
1337
|
+
if (sessionId === void 0) return false;
|
|
1338
|
+
return sources.driven.has(sessionId) || sources.followed?.has(sessionId) === true;
|
|
1339
|
+
}
|
|
1340
|
+
/**
|
|
1341
|
+
* Resolve which session id the sidebar card should be filed under: the asking
|
|
1342
|
+
* session itself when it is owned, otherwise the nearest owned ancestor (a
|
|
1343
|
+
* subagent child session inherits its parent's ownership).
|
|
1344
|
+
*
|
|
1345
|
+
* @param sessionId - the asking session (the host event's agent id).
|
|
1346
|
+
* @param sources - ownership sources.
|
|
1347
|
+
* @param parentOf - session-id → parent-session-id lookup; absent disables inheritance.
|
|
1348
|
+
* @returns the owned session id to route to, or undefined when nothing owns it.
|
|
1270
1349
|
*/
|
|
1271
|
-
function
|
|
1272
|
-
|
|
1350
|
+
function owningSessionId(sessionId, sources, parentOf) {
|
|
1351
|
+
if (sessionId === void 0) return void 0;
|
|
1352
|
+
if (isOwnedSession(sessionId, sources)) return sessionId;
|
|
1353
|
+
if (parentOf === void 0) return void 0;
|
|
1354
|
+
const seen = /* @__PURE__ */ new Set([sessionId]);
|
|
1355
|
+
let current = sessionId;
|
|
1356
|
+
for (let depth = 0; depth < 8; depth += 1) {
|
|
1357
|
+
const parent = parentOf(current);
|
|
1358
|
+
if (parent === void 0 || parent.length === 0 || seen.has(parent)) return void 0;
|
|
1359
|
+
if (isOwnedSession(parent, sources)) return parent;
|
|
1360
|
+
seen.add(parent);
|
|
1361
|
+
current = parent;
|
|
1362
|
+
}
|
|
1363
|
+
}
|
|
1364
|
+
/**
|
|
1365
|
+
* Decide whether the bridge should own one host interaction (question or
|
|
1366
|
+
* approval). All gates must pass: an extension is connected, it advertised
|
|
1367
|
+
* the matching card capability in its hello caps, and the asking session is
|
|
1368
|
+
* extension-owned (directly or through an owned ancestor). Everything else
|
|
1369
|
+
* falls through to `next()` so the native answerer waterfall keeps working.
|
|
1370
|
+
*/
|
|
1371
|
+
function resolveInteractionOwnership(input) {
|
|
1372
|
+
if (!input.hasExtensionConnection) return {
|
|
1373
|
+
owned: false,
|
|
1374
|
+
reason: "no-connection"
|
|
1375
|
+
};
|
|
1376
|
+
if (!input.clientSupports) return {
|
|
1377
|
+
owned: false,
|
|
1378
|
+
reason: "unsupported-client"
|
|
1379
|
+
};
|
|
1380
|
+
const sessionId = owningSessionId(input.sessionId, input.sources, input.parentOf);
|
|
1381
|
+
if (sessionId === void 0) return {
|
|
1382
|
+
owned: false,
|
|
1383
|
+
reason: "not-owned"
|
|
1384
|
+
};
|
|
1385
|
+
return {
|
|
1386
|
+
owned: true,
|
|
1387
|
+
sessionId
|
|
1388
|
+
};
|
|
1273
1389
|
}
|
|
1274
1390
|
//#endregion
|
|
1275
1391
|
//#region lib/types/questions.js
|
|
@@ -1424,6 +1540,129 @@ var QuestionBridge = class {
|
|
|
1424
1540
|
}
|
|
1425
1541
|
};
|
|
1426
1542
|
//#endregion
|
|
1543
|
+
//#region lib/types/approvals.js
|
|
1544
|
+
/**
|
|
1545
|
+
* Host approval ownership: claim the host `approval/request` waterfall for
|
|
1546
|
+
* extension-owned sessions, push the decision request to the connected
|
|
1547
|
+
* extension, and settle the waterfall with the sidebar's choice.
|
|
1548
|
+
*
|
|
1549
|
+
* Settlement paths (first wins, later ones no-op):
|
|
1550
|
+
* - the sidebar decides → the waterfall resolves with that outcome;
|
|
1551
|
+
* - the host aborts `request.signal` (turn cancelled, session disposed) →
|
|
1552
|
+
* the extension is sent `approval.resolved` and the waterfall resolves
|
|
1553
|
+
* with `'cancelled'` (the host vocabulary has no rejection channel here);
|
|
1554
|
+
* - the bridge connection drops ({@link delegateAll}) → the waterfall is
|
|
1555
|
+
* delegated to `next()` so another answerer (dsh web) can still decide;
|
|
1556
|
+
* - plugin unload ({@link dispose}) → `'unavailable'`, the fail-closed
|
|
1557
|
+
* outcome of the host's closed outcome set.
|
|
1558
|
+
*
|
|
1559
|
+
* @module
|
|
1560
|
+
*/
|
|
1561
|
+
/** Parse the extension's `bridge/approvalDecision` rpc args (ids + closed decision). */
|
|
1562
|
+
function asApprovalDecisionArgs(value) {
|
|
1563
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return void 0;
|
|
1564
|
+
const args = value;
|
|
1565
|
+
if (typeof args.approvalId !== "string" || typeof args.sessionId !== "string") return void 0;
|
|
1566
|
+
if (args.decision !== "allowed-once" && args.decision !== "rejected") return void 0;
|
|
1567
|
+
return {
|
|
1568
|
+
approvalId: args.approvalId,
|
|
1569
|
+
sessionId: args.sessionId,
|
|
1570
|
+
decision: args.decision
|
|
1571
|
+
};
|
|
1572
|
+
}
|
|
1573
|
+
/** Owns the host approval waterfall for extension-driven sessions. */
|
|
1574
|
+
var ApprovalBridge = class {
|
|
1575
|
+
deps;
|
|
1576
|
+
pending = /* @__PURE__ */ new Map();
|
|
1577
|
+
constructor(deps) {
|
|
1578
|
+
this.deps = deps;
|
|
1579
|
+
}
|
|
1580
|
+
/**
|
|
1581
|
+
* Own one host waterfall request: push `approval.requested` to the
|
|
1582
|
+
* extension and suspend until one of the settlement paths wins.
|
|
1583
|
+
*
|
|
1584
|
+
* @param routingSessionId - extension-visible session id (the owned session the card is filed under).
|
|
1585
|
+
* @param prompt - tool name, optional call id and human-readable reason.
|
|
1586
|
+
* @param next - waterfall delegation, used when the bridge connection drops.
|
|
1587
|
+
* @param signal - host cancellation lifetime for this request.
|
|
1588
|
+
* @returns the host approval outcome.
|
|
1589
|
+
*/
|
|
1590
|
+
ask(routingSessionId, prompt, next, signal) {
|
|
1591
|
+
if (signal?.aborted) return Promise.resolve("cancelled");
|
|
1592
|
+
const approvalId = randomUUID();
|
|
1593
|
+
return new Promise((resolve, reject) => {
|
|
1594
|
+
const entry = {
|
|
1595
|
+
sessionId: routingSessionId,
|
|
1596
|
+
settled: false,
|
|
1597
|
+
signal,
|
|
1598
|
+
complete: (outcome) => {
|
|
1599
|
+
if (entry.settled) return;
|
|
1600
|
+
entry.settled = true;
|
|
1601
|
+
this.pending.delete(approvalId);
|
|
1602
|
+
if (entry.onAbort !== void 0) entry.signal?.removeEventListener("abort", entry.onAbort);
|
|
1603
|
+
if (outcome.kind === "decide") resolve(outcome.outcome);
|
|
1604
|
+
else next().then(resolve, reject);
|
|
1605
|
+
},
|
|
1606
|
+
onAbort: void 0
|
|
1607
|
+
};
|
|
1608
|
+
if (signal !== void 0) {
|
|
1609
|
+
entry.onAbort = () => {
|
|
1610
|
+
this.deps.push({
|
|
1611
|
+
t: "approval.resolved",
|
|
1612
|
+
id: approvalId,
|
|
1613
|
+
sessionId: routingSessionId
|
|
1614
|
+
});
|
|
1615
|
+
entry.complete({
|
|
1616
|
+
kind: "decide",
|
|
1617
|
+
outcome: "cancelled"
|
|
1618
|
+
});
|
|
1619
|
+
};
|
|
1620
|
+
signal.addEventListener("abort", entry.onAbort, { once: true });
|
|
1621
|
+
}
|
|
1622
|
+
this.pending.set(approvalId, entry);
|
|
1623
|
+
this.deps.push({
|
|
1624
|
+
t: "approval.requested",
|
|
1625
|
+
id: approvalId,
|
|
1626
|
+
sessionId: routingSessionId,
|
|
1627
|
+
toolName: prompt.toolName,
|
|
1628
|
+
...prompt.callId === void 0 ? {} : { callId: prompt.callId },
|
|
1629
|
+
...prompt.reason === void 0 ? {} : { reason: prompt.reason }
|
|
1630
|
+
});
|
|
1631
|
+
});
|
|
1632
|
+
}
|
|
1633
|
+
/**
|
|
1634
|
+
* Settle one pending approval from the extension's `bridge/approvalDecision`
|
|
1635
|
+
* rpc. The server replies with this receipt.
|
|
1636
|
+
*/
|
|
1637
|
+
decide(args) {
|
|
1638
|
+
const entry = this.pending.get(args.approvalId);
|
|
1639
|
+
if (entry === void 0 || entry.settled || entry.sessionId !== args.sessionId) return {
|
|
1640
|
+
accepted: false,
|
|
1641
|
+
reason: "not-pending"
|
|
1642
|
+
};
|
|
1643
|
+
if (args.decision !== "allowed-once" && args.decision !== "rejected") return {
|
|
1644
|
+
accepted: false,
|
|
1645
|
+
reason: "bad-decision"
|
|
1646
|
+
};
|
|
1647
|
+
entry.complete({
|
|
1648
|
+
kind: "decide",
|
|
1649
|
+
outcome: args.decision
|
|
1650
|
+
});
|
|
1651
|
+
return { accepted: true };
|
|
1652
|
+
}
|
|
1653
|
+
/** Bridge connection replaced/closed: delegate every unsettled approval. */
|
|
1654
|
+
delegateAll() {
|
|
1655
|
+
for (const entry of [...this.pending.values()]) if (!entry.settled) entry.complete({ kind: "delegate" });
|
|
1656
|
+
}
|
|
1657
|
+
/** Plugin unload: answer everything fail-closed so no tool call outlives the plugin. */
|
|
1658
|
+
dispose() {
|
|
1659
|
+
for (const entry of [...this.pending.values()]) if (!entry.settled) entry.complete({
|
|
1660
|
+
kind: "decide",
|
|
1661
|
+
outcome: "unavailable"
|
|
1662
|
+
});
|
|
1663
|
+
}
|
|
1664
|
+
};
|
|
1665
|
+
//#endregion
|
|
1427
1666
|
//#region lib/types/index.js
|
|
1428
1667
|
/**
|
|
1429
1668
|
* `@deepseek-ai/dsh-bridge-browser`: token-authenticated WebSocket bridge for
|
|
@@ -1489,6 +1728,21 @@ function sessionIdOf(value) {
|
|
|
1489
1728
|
return typeof sessionId === "string" && sessionId.length > 0 ? sessionId : void 0;
|
|
1490
1729
|
}
|
|
1491
1730
|
/**
|
|
1731
|
+
* Human-readable approval reason: the asker's localized text when present
|
|
1732
|
+
* (zh first, then en), otherwise the raw reason string.
|
|
1733
|
+
*
|
|
1734
|
+
* @param request - pending host approval request.
|
|
1735
|
+
* @returns display text, or undefined when the asker supplied none.
|
|
1736
|
+
*/
|
|
1737
|
+
function approvalReasonText(request) {
|
|
1738
|
+
const display = request.displayReason;
|
|
1739
|
+
if (display !== void 0) {
|
|
1740
|
+
const localized = display.zh ?? display["zh-Hans"] ?? display.en;
|
|
1741
|
+
if (typeof localized === "string" && localized.length > 0) return localized;
|
|
1742
|
+
}
|
|
1743
|
+
return typeof request.reason === "string" && request.reason.length > 0 ? request.reason : void 0;
|
|
1744
|
+
}
|
|
1745
|
+
/**
|
|
1492
1746
|
* Apply defaults and direct-call validation at the plugin boundary.
|
|
1493
1747
|
* @param config - Loader-resolved or directly supplied plugin configuration.
|
|
1494
1748
|
* @returns a complete configuration ready for runtime use.
|
|
@@ -1542,17 +1796,39 @@ async function apply(ctx, config) {
|
|
|
1542
1796
|
ctx.logger.warn(message);
|
|
1543
1797
|
}), resolved.deferSessionCreate);
|
|
1544
1798
|
const extensionSessions = new ExtensionSessionRegistry();
|
|
1799
|
+
const followedSessions = new FollowedSessions();
|
|
1800
|
+
const ownershipSources = {
|
|
1801
|
+
driven: extensionSessions,
|
|
1802
|
+
followed: followedSessions
|
|
1803
|
+
};
|
|
1804
|
+
const sessions = ctx.get("sessions");
|
|
1805
|
+
const parentOf = sessions === void 0 ? void 0 : (sessionId) => sessions.get(sessionId)?.header.parentSession;
|
|
1545
1806
|
const notingDispatchRpc = async (method, args, signal) => {
|
|
1807
|
+
const tracksOwnership = method === "session/create" || method === "session/prompt";
|
|
1808
|
+
const dispatched = tracksOwnership ? sessionIdOf(args.request) : void 0;
|
|
1809
|
+
if (dispatched !== void 0) extensionSessions.note(dispatched);
|
|
1546
1810
|
const result = await dispatchRpc(method, args, signal);
|
|
1547
|
-
if (result.ok
|
|
1548
|
-
|
|
1549
|
-
|
|
1811
|
+
if (!result.ok) {
|
|
1812
|
+
const code = result.error.code;
|
|
1813
|
+
if (dispatched !== void 0 && (code.includes("not-found") || code.includes("session-invalid"))) extensionSessions.forget(dispatched);
|
|
1814
|
+
return result;
|
|
1550
1815
|
}
|
|
1816
|
+
if (tracksOwnership) extensionSessions.note(sessionIdOf(result.value));
|
|
1551
1817
|
return result;
|
|
1552
1818
|
};
|
|
1819
|
+
let pluginVersion;
|
|
1820
|
+
try {
|
|
1821
|
+
const pkg = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
|
|
1822
|
+
pluginVersion = typeof pkg.version === "string" ? pkg.version : void 0;
|
|
1823
|
+
} catch {
|
|
1824
|
+
pluginVersion = void 0;
|
|
1825
|
+
}
|
|
1553
1826
|
const questions = new QuestionBridge({ push: (frame) => {
|
|
1554
1827
|
server.push(frame);
|
|
1555
1828
|
} });
|
|
1829
|
+
const approvals = new ApprovalBridge({ push: (frame) => {
|
|
1830
|
+
server.push(frame);
|
|
1831
|
+
} });
|
|
1556
1832
|
const server = new BridgeServer({
|
|
1557
1833
|
token: tokenRes.token,
|
|
1558
1834
|
dispatchRpc: notingDispatchRpc,
|
|
@@ -1574,8 +1850,24 @@ async function apply(ctx, config) {
|
|
|
1574
1850
|
reason: "bad-answer"
|
|
1575
1851
|
} : questions.answer(parsed);
|
|
1576
1852
|
},
|
|
1853
|
+
answerApproval: (args) => {
|
|
1854
|
+
const parsed = asApprovalDecisionArgs(args);
|
|
1855
|
+
return parsed === void 0 ? {
|
|
1856
|
+
accepted: false,
|
|
1857
|
+
reason: "bad-decision"
|
|
1858
|
+
} : approvals.decide(parsed);
|
|
1859
|
+
},
|
|
1577
1860
|
onConnectionLost: () => {
|
|
1578
1861
|
questions.delegateAll();
|
|
1862
|
+
approvals.delegateAll();
|
|
1863
|
+
},
|
|
1864
|
+
onFollowChanged: (sessionId, open) => {
|
|
1865
|
+
if (open) followedSessions.open(sessionId);
|
|
1866
|
+
else followedSessions.close(sessionId);
|
|
1867
|
+
},
|
|
1868
|
+
version: pluginVersion,
|
|
1869
|
+
onVersionMismatch: (clientVersion) => {
|
|
1870
|
+
ctx.logger.warn(`browser extension version ${clientVersion} != plugin version ${pluginVersion ?? "?"}; both components must be updated together`);
|
|
1579
1871
|
},
|
|
1580
1872
|
toolTimeoutMs: resolved.toolTimeoutMs,
|
|
1581
1873
|
caps: {
|
|
@@ -1583,22 +1875,53 @@ async function apply(ctx, config) {
|
|
|
1583
1875
|
maxInteractiveItems: resolved.maxInteractiveItems
|
|
1584
1876
|
}
|
|
1585
1877
|
});
|
|
1878
|
+
const logDelegation = (kind, sessionId, reason) => {
|
|
1879
|
+
ctx.logger.info(`browser bridge: delegated ${kind} for session ${sessionId ?? "<unknown>"} (${reason})`);
|
|
1880
|
+
};
|
|
1586
1881
|
ctx.effect(() => {
|
|
1587
1882
|
const disposeListener = ctx.on("user-questions/request", (request, next) => {
|
|
1588
|
-
const
|
|
1589
|
-
if (sessionId === void 0 || !shouldBridgeOwnQuestion({
|
|
1883
|
+
const ownership = resolveInteractionOwnership({
|
|
1590
1884
|
hasExtensionConnection: server.hasConnection(),
|
|
1591
|
-
|
|
1592
|
-
sessionId,
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1885
|
+
clientSupports: server.clientSupportsQuestions(),
|
|
1886
|
+
sessionId: request.agent?.id,
|
|
1887
|
+
sources: ownershipSources,
|
|
1888
|
+
...parentOf === void 0 ? {} : { parentOf }
|
|
1889
|
+
});
|
|
1890
|
+
if (!ownership.owned) {
|
|
1891
|
+
logDelegation("user-questions", request.agent?.id, ownership.reason);
|
|
1892
|
+
return next();
|
|
1893
|
+
}
|
|
1894
|
+
return questions.ask(ownership.sessionId, request.questions, next, request.signal);
|
|
1596
1895
|
}, { prepend: true });
|
|
1597
1896
|
return () => {
|
|
1598
1897
|
disposeListener();
|
|
1599
1898
|
questions.dispose();
|
|
1600
1899
|
};
|
|
1601
1900
|
}, "bridge-browser: user-questions answerer");
|
|
1901
|
+
ctx.effect(() => {
|
|
1902
|
+
const disposeListener = ctx.on("approval/request", (request, next) => {
|
|
1903
|
+
const ownership = resolveInteractionOwnership({
|
|
1904
|
+
hasExtensionConnection: server.hasConnection(),
|
|
1905
|
+
clientSupports: server.clientSupportsApprovals(),
|
|
1906
|
+
sessionId: request.agent?.id,
|
|
1907
|
+
sources: ownershipSources,
|
|
1908
|
+
...parentOf === void 0 ? {} : { parentOf }
|
|
1909
|
+
});
|
|
1910
|
+
if (!ownership.owned) {
|
|
1911
|
+
logDelegation("approval", request.agent?.id, ownership.reason);
|
|
1912
|
+
return next();
|
|
1913
|
+
}
|
|
1914
|
+
return approvals.ask(ownership.sessionId, {
|
|
1915
|
+
toolName: request.toolName,
|
|
1916
|
+
...typeof request.callId === "string" ? { callId: request.callId } : {},
|
|
1917
|
+
...approvalReasonText(request) === void 0 ? {} : { reason: approvalReasonText(request) }
|
|
1918
|
+
}, next, request.signal);
|
|
1919
|
+
}, { prepend: true });
|
|
1920
|
+
return () => {
|
|
1921
|
+
disposeListener();
|
|
1922
|
+
approvals.dispose();
|
|
1923
|
+
};
|
|
1924
|
+
}, "bridge-browser: approval answerer");
|
|
1602
1925
|
const route = {
|
|
1603
1926
|
path: BRIDGE_PATH,
|
|
1604
1927
|
handler: (req, socket, head) => {
|
package/lib/protocol.js
CHANGED
|
@@ -36,7 +36,7 @@ const DEFAULT_TOKEN_BYTES = 32;
|
|
|
36
36
|
* @returns true for server-sendable frames.
|
|
37
37
|
*/
|
|
38
38
|
function isServerFrame(frame) {
|
|
39
|
-
return frame.t === "hello.ok" || frame.t === "rpc.result" || frame.t === "stream.frame" || frame.t === "stream.error" || frame.t === "tool.call" || frame.t === "tool.cancel" || frame.t === "question.requested" || frame.t === "question.resolved" || frame.t === "ping" || frame.t === "error";
|
|
39
|
+
return frame.t === "hello.ok" || frame.t === "rpc.result" || frame.t === "stream.frame" || frame.t === "stream.error" || frame.t === "tool.call" || frame.t === "tool.cancel" || frame.t === "question.requested" || frame.t === "question.resolved" || frame.t === "approval.requested" || frame.t === "approval.resolved" || frame.t === "ping" || frame.t === "error";
|
|
40
40
|
}
|
|
41
41
|
/**
|
|
42
42
|
* Type guard: is this frame one the CLIENT may send? Server-only shapes
|
|
@@ -67,11 +67,15 @@ function parseBridgeFrame(text) {
|
|
|
67
67
|
const frame = value;
|
|
68
68
|
if (typeof frame.t !== "string") return void 0;
|
|
69
69
|
switch (frame.t) {
|
|
70
|
-
case "hello":
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
70
|
+
case "hello":
|
|
71
|
+
if (typeof frame.token !== "string" || !isCaps(frame.caps)) return void 0;
|
|
72
|
+
if (frame.version !== void 0 && typeof frame.version !== "string") return void 0;
|
|
73
|
+
return {
|
|
74
|
+
t: "hello",
|
|
75
|
+
token: frame.token,
|
|
76
|
+
caps: frame.caps,
|
|
77
|
+
...frame.version === void 0 ? {} : { version: frame.version }
|
|
78
|
+
};
|
|
75
79
|
case "rpc": return typeof frame.id === "string" && typeof frame.method === "string" && isArgs(frame.args) ? {
|
|
76
80
|
t: "rpc",
|
|
77
81
|
id: frame.id,
|
|
@@ -103,10 +107,14 @@ function parseBridgeFrame(text) {
|
|
|
103
107
|
error: frame.error
|
|
104
108
|
} : void 0;
|
|
105
109
|
case "pong": return { t: "pong" };
|
|
106
|
-
case "hello.ok":
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
+
case "hello.ok":
|
|
111
|
+
if (!isCaps(frame.caps)) return void 0;
|
|
112
|
+
if (frame.version !== void 0 && typeof frame.version !== "string") return void 0;
|
|
113
|
+
return {
|
|
114
|
+
t: "hello.ok",
|
|
115
|
+
caps: frame.caps,
|
|
116
|
+
...frame.version === void 0 ? {} : { version: frame.version }
|
|
117
|
+
};
|
|
110
118
|
case "rpc.result":
|
|
111
119
|
if (typeof frame.id !== "string") return void 0;
|
|
112
120
|
if (frame.ok === true && "value" in frame) return {
|
|
@@ -159,6 +167,23 @@ function parseBridgeFrame(text) {
|
|
|
159
167
|
id: frame.id,
|
|
160
168
|
sessionId: frame.sessionId
|
|
161
169
|
} : void 0;
|
|
170
|
+
case "approval.requested":
|
|
171
|
+
if (typeof frame.id !== "string" || typeof frame.sessionId !== "string" || typeof frame.toolName !== "string") return;
|
|
172
|
+
if (frame.callId !== void 0 && typeof frame.callId !== "string") return void 0;
|
|
173
|
+
if (frame.reason !== void 0 && typeof frame.reason !== "string") return void 0;
|
|
174
|
+
return {
|
|
175
|
+
t: "approval.requested",
|
|
176
|
+
id: frame.id,
|
|
177
|
+
sessionId: frame.sessionId,
|
|
178
|
+
toolName: frame.toolName,
|
|
179
|
+
...typeof frame.callId === "string" ? { callId: frame.callId } : {},
|
|
180
|
+
...typeof frame.reason === "string" ? { reason: frame.reason } : {}
|
|
181
|
+
};
|
|
182
|
+
case "approval.resolved": return typeof frame.id === "string" && typeof frame.sessionId === "string" ? {
|
|
183
|
+
t: "approval.resolved",
|
|
184
|
+
id: frame.id,
|
|
185
|
+
sessionId: frame.sessionId
|
|
186
|
+
} : void 0;
|
|
162
187
|
case "ping": return { t: "ping" };
|
|
163
188
|
case "error": return typeof frame.code === "string" && typeof frame.message === "string" ? {
|
|
164
189
|
t: "error",
|
|
@@ -171,7 +196,7 @@ function parseBridgeFrame(text) {
|
|
|
171
196
|
function isCaps(value) {
|
|
172
197
|
if (typeof value !== "object" || value === null) return false;
|
|
173
198
|
const caps = value;
|
|
174
|
-
return typeof caps.snapshotMaxChars === "number" && caps.snapshotMaxChars > 0 && typeof caps.maxInteractiveItems === "number" && caps.maxInteractiveItems > 0 && (caps.questions === void 0 || typeof caps.questions === "boolean");
|
|
199
|
+
return typeof caps.snapshotMaxChars === "number" && caps.snapshotMaxChars > 0 && typeof caps.maxInteractiveItems === "number" && caps.maxInteractiveItems > 0 && (caps.questions === void 0 || typeof caps.questions === "boolean") && (caps.approvals === void 0 || typeof caps.approvals === "boolean");
|
|
175
200
|
}
|
|
176
201
|
/** Narrow one question item; unknown extra keys (e.g. host `intent`) pass through. */
|
|
177
202
|
function isQuestionItem(value) {
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Host approval ownership: claim the host `approval/request` waterfall for
|
|
3
|
+
* extension-owned sessions, push the decision request to the connected
|
|
4
|
+
* extension, and settle the waterfall with the sidebar's choice.
|
|
5
|
+
*
|
|
6
|
+
* Settlement paths (first wins, later ones no-op):
|
|
7
|
+
* - the sidebar decides → the waterfall resolves with that outcome;
|
|
8
|
+
* - the host aborts `request.signal` (turn cancelled, session disposed) →
|
|
9
|
+
* the extension is sent `approval.resolved` and the waterfall resolves
|
|
10
|
+
* with `'cancelled'` (the host vocabulary has no rejection channel here);
|
|
11
|
+
* - the bridge connection drops ({@link delegateAll}) → the waterfall is
|
|
12
|
+
* delegated to `next()` so another answerer (dsh web) can still decide;
|
|
13
|
+
* - plugin unload ({@link dispose}) → `'unavailable'`, the fail-closed
|
|
14
|
+
* outcome of the host's closed outcome set.
|
|
15
|
+
*
|
|
16
|
+
* @module
|
|
17
|
+
*/
|
|
18
|
+
import type { ServerFrame } from './protocol.ts';
|
|
19
|
+
/** The host's closed approval outcome vocabulary. */
|
|
20
|
+
export type ApprovalOutcome = 'allowed-once' | 'rejected' | 'cancelled' | 'unavailable';
|
|
21
|
+
/** One approval prompt as it crosses the bridge. */
|
|
22
|
+
export interface ApprovalPrompt {
|
|
23
|
+
toolName: string;
|
|
24
|
+
callId?: string;
|
|
25
|
+
reason?: string;
|
|
26
|
+
}
|
|
27
|
+
/** Decision the sidebar sends back over `bridge/approvalDecision`. */
|
|
28
|
+
export type ApprovalDecision = 'allowed-once' | 'rejected';
|
|
29
|
+
/** Receipt answered to the extension's `bridge/approvalDecision` rpc. */
|
|
30
|
+
export type ApprovalReceipt = {
|
|
31
|
+
accepted: true;
|
|
32
|
+
} | {
|
|
33
|
+
accepted: false;
|
|
34
|
+
reason: 'not-pending' | 'bad-decision';
|
|
35
|
+
};
|
|
36
|
+
/** Args of the plugin-local `bridge/approvalDecision` rpc method. */
|
|
37
|
+
export interface ApprovalDecisionArgs {
|
|
38
|
+
approvalId: string;
|
|
39
|
+
sessionId: string;
|
|
40
|
+
decision?: unknown;
|
|
41
|
+
}
|
|
42
|
+
/** Parse the extension's `bridge/approvalDecision` rpc args (ids + closed decision). */
|
|
43
|
+
export declare function asApprovalDecisionArgs(value: unknown): ApprovalDecisionArgs | undefined;
|
|
44
|
+
/** Owns the host approval waterfall for extension-driven sessions. */
|
|
45
|
+
export declare class ApprovalBridge {
|
|
46
|
+
private readonly deps;
|
|
47
|
+
private readonly pending;
|
|
48
|
+
constructor(deps: {
|
|
49
|
+
/** Push one server frame to the connected extension (must tolerate a dead socket). */
|
|
50
|
+
push: (frame: ServerFrame) => void;
|
|
51
|
+
});
|
|
52
|
+
/**
|
|
53
|
+
* Own one host waterfall request: push `approval.requested` to the
|
|
54
|
+
* extension and suspend until one of the settlement paths wins.
|
|
55
|
+
*
|
|
56
|
+
* @param routingSessionId - extension-visible session id (the owned session the card is filed under).
|
|
57
|
+
* @param prompt - tool name, optional call id and human-readable reason.
|
|
58
|
+
* @param next - waterfall delegation, used when the bridge connection drops.
|
|
59
|
+
* @param signal - host cancellation lifetime for this request.
|
|
60
|
+
* @returns the host approval outcome.
|
|
61
|
+
*/
|
|
62
|
+
ask(routingSessionId: string, prompt: ApprovalPrompt, next: () => Promise<ApprovalOutcome>, signal?: AbortSignal): Promise<ApprovalOutcome>;
|
|
63
|
+
/**
|
|
64
|
+
* Settle one pending approval from the extension's `bridge/approvalDecision`
|
|
65
|
+
* rpc. The server replies with this receipt.
|
|
66
|
+
*/
|
|
67
|
+
decide(args: ApprovalDecisionArgs): ApprovalReceipt;
|
|
68
|
+
/** Bridge connection replaced/closed: delegate every unsettled approval. */
|
|
69
|
+
delegateAll(): void;
|
|
70
|
+
/** Plugin unload: answer everything fail-closed so no tool call outlives the plugin. */
|
|
71
|
+
dispose(): void;
|
|
72
|
+
}
|
|
73
|
+
//# sourceMappingURL=approvals.d.ts.map
|