dsh-browser-firefox 0.4.6 → 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 +330 -21
- package/lib/protocol.js +19 -2
- package/lib/types/approvals.d.ts +73 -0
- package/lib/types/extension-sessions.d.ts +79 -7
- package/lib/types/protocol.d.ts +25 -0
- package/lib/types/server.d.ts +14 -0
- package/package.json +36 -33
- package/src/approvals.ts +160 -0
- package/src/extension-sessions.ts +136 -11
- package/src/index.ts +112 -20
- package/src/protocol.ts +35 -0
- package/src/server.ts +42 -1
package/lib/index.js
CHANGED
|
@@ -195,6 +195,13 @@ function orderedSessionId(frame) {
|
|
|
195
195
|
const sessionId = request.sessionId;
|
|
196
196
|
return typeof sessionId === "string" ? sessionId : void 0;
|
|
197
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
|
+
}
|
|
198
205
|
/** Loopback IPv4/IPv6 literals (IPv4-mapped included). Exported for tests and reuse. */
|
|
199
206
|
function isLoopbackAddress(address) {
|
|
200
207
|
return address === "127.0.0.1" || address === "::1" || address === "::ffff:127.0.0.1";
|
|
@@ -347,6 +354,10 @@ var BridgeServer = class {
|
|
|
347
354
|
clientSupportsQuestions() {
|
|
348
355
|
return this.current?.clientCaps.questions === true;
|
|
349
356
|
}
|
|
357
|
+
/** @returns whether the connected extension advertised approval-card support. */
|
|
358
|
+
clientSupportsApprovals() {
|
|
359
|
+
return this.current?.clientCaps.approvals === true;
|
|
360
|
+
}
|
|
350
361
|
/**
|
|
351
362
|
* Push one unsolicited server frame (question.requested/question.resolved)
|
|
352
363
|
* to the connected extension. No-op when no extension is connected or the
|
|
@@ -506,6 +517,15 @@ var BridgeServer = class {
|
|
|
506
517
|
});
|
|
507
518
|
return;
|
|
508
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
|
+
}
|
|
509
529
|
try {
|
|
510
530
|
const result = await this.deps.dispatchRpc(frame.method, frame.args, conn.abort.signal);
|
|
511
531
|
if (result.ok) sendFrame(conn.ws, {
|
|
@@ -550,6 +570,7 @@ var BridgeServer = class {
|
|
|
550
570
|
conn.abort.signal.addEventListener("abort", () => {
|
|
551
571
|
abort.abort();
|
|
552
572
|
}, { once: true });
|
|
573
|
+
const followedSessionId = frame.method === "session/follow" ? followedSessionOf(frame.args) : void 0;
|
|
553
574
|
let pump;
|
|
554
575
|
pump = (async () => {
|
|
555
576
|
let iterator;
|
|
@@ -578,12 +599,15 @@ var BridgeServer = class {
|
|
|
578
599
|
} catch {}
|
|
579
600
|
const current = this.current;
|
|
580
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);
|
|
581
603
|
}
|
|
582
604
|
})();
|
|
583
605
|
conn.streams.set(frame.id, {
|
|
584
606
|
abort,
|
|
585
|
-
pump
|
|
607
|
+
pump,
|
|
608
|
+
...followedSessionId === void 0 ? {} : { followedSessionId }
|
|
586
609
|
});
|
|
610
|
+
if (followedSessionId !== void 0) this.deps.onFollowChanged?.(followedSessionId, true);
|
|
587
611
|
}
|
|
588
612
|
/**
|
|
589
613
|
* Backpressure gate for stream pumps. Returns false when the wait ended
|
|
@@ -1243,9 +1267,19 @@ function withSessionWorkspace(dispatch, workspacePath, warn) {
|
|
|
1243
1267
|
//#region lib/types/extension-sessions.js
|
|
1244
1268
|
/**
|
|
1245
1269
|
* Track session ids that the browser extension has driven through the bridge.
|
|
1246
|
-
* Sessions the extension never touched must keep the host
|
|
1247
|
-
*
|
|
1248
|
-
*
|
|
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
|
+
*
|
|
1249
1283
|
* @module
|
|
1250
1284
|
*/
|
|
1251
1285
|
/** Mutable registry of extension-owned session ids. */
|
|
@@ -1255,6 +1289,10 @@ var ExtensionSessionRegistry = class {
|
|
|
1255
1289
|
note(sessionId) {
|
|
1256
1290
|
if (typeof sessionId === "string" && sessionId.length > 0) this.ids.add(sessionId);
|
|
1257
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
|
+
}
|
|
1258
1296
|
/** Whether the extension has touched this session over the bridge. */
|
|
1259
1297
|
has(sessionId) {
|
|
1260
1298
|
return typeof sessionId === "string" && this.ids.has(sessionId);
|
|
@@ -1265,14 +1303,89 @@ var ExtensionSessionRegistry = class {
|
|
|
1265
1303
|
}
|
|
1266
1304
|
};
|
|
1267
1305
|
/**
|
|
1268
|
-
*
|
|
1269
|
-
*
|
|
1270
|
-
*
|
|
1271
|
-
*
|
|
1272
|
-
* the native answerer waterfall (dsh web) keeps working.
|
|
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.
|
|
1273
1310
|
*/
|
|
1274
|
-
|
|
1275
|
-
|
|
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.
|
|
1349
|
+
*/
|
|
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
|
+
};
|
|
1276
1389
|
}
|
|
1277
1390
|
//#endregion
|
|
1278
1391
|
//#region lib/types/questions.js
|
|
@@ -1427,6 +1540,129 @@ var QuestionBridge = class {
|
|
|
1427
1540
|
}
|
|
1428
1541
|
};
|
|
1429
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
|
|
1430
1666
|
//#region lib/types/index.js
|
|
1431
1667
|
/**
|
|
1432
1668
|
* `@deepseek-ai/dsh-bridge-browser`: token-authenticated WebSocket bridge for
|
|
@@ -1492,6 +1728,21 @@ function sessionIdOf(value) {
|
|
|
1492
1728
|
return typeof sessionId === "string" && sessionId.length > 0 ? sessionId : void 0;
|
|
1493
1729
|
}
|
|
1494
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
|
+
/**
|
|
1495
1746
|
* Apply defaults and direct-call validation at the plugin boundary.
|
|
1496
1747
|
* @param config - Loader-resolved or directly supplied plugin configuration.
|
|
1497
1748
|
* @returns a complete configuration ready for runtime use.
|
|
@@ -1545,12 +1796,24 @@ async function apply(ctx, config) {
|
|
|
1545
1796
|
ctx.logger.warn(message);
|
|
1546
1797
|
}), resolved.deferSessionCreate);
|
|
1547
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;
|
|
1548
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);
|
|
1549
1810
|
const result = await dispatchRpc(method, args, signal);
|
|
1550
|
-
if (result.ok
|
|
1551
|
-
|
|
1552
|
-
|
|
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;
|
|
1553
1815
|
}
|
|
1816
|
+
if (tracksOwnership) extensionSessions.note(sessionIdOf(result.value));
|
|
1554
1817
|
return result;
|
|
1555
1818
|
};
|
|
1556
1819
|
let pluginVersion;
|
|
@@ -1563,6 +1826,9 @@ async function apply(ctx, config) {
|
|
|
1563
1826
|
const questions = new QuestionBridge({ push: (frame) => {
|
|
1564
1827
|
server.push(frame);
|
|
1565
1828
|
} });
|
|
1829
|
+
const approvals = new ApprovalBridge({ push: (frame) => {
|
|
1830
|
+
server.push(frame);
|
|
1831
|
+
} });
|
|
1566
1832
|
const server = new BridgeServer({
|
|
1567
1833
|
token: tokenRes.token,
|
|
1568
1834
|
dispatchRpc: notingDispatchRpc,
|
|
@@ -1584,8 +1850,20 @@ async function apply(ctx, config) {
|
|
|
1584
1850
|
reason: "bad-answer"
|
|
1585
1851
|
} : questions.answer(parsed);
|
|
1586
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
|
+
},
|
|
1587
1860
|
onConnectionLost: () => {
|
|
1588
1861
|
questions.delegateAll();
|
|
1862
|
+
approvals.delegateAll();
|
|
1863
|
+
},
|
|
1864
|
+
onFollowChanged: (sessionId, open) => {
|
|
1865
|
+
if (open) followedSessions.open(sessionId);
|
|
1866
|
+
else followedSessions.close(sessionId);
|
|
1589
1867
|
},
|
|
1590
1868
|
version: pluginVersion,
|
|
1591
1869
|
onVersionMismatch: (clientVersion) => {
|
|
@@ -1597,22 +1875,53 @@ async function apply(ctx, config) {
|
|
|
1597
1875
|
maxInteractiveItems: resolved.maxInteractiveItems
|
|
1598
1876
|
}
|
|
1599
1877
|
});
|
|
1878
|
+
const logDelegation = (kind, sessionId, reason) => {
|
|
1879
|
+
ctx.logger.info(`browser bridge: delegated ${kind} for session ${sessionId ?? "<unknown>"} (${reason})`);
|
|
1880
|
+
};
|
|
1600
1881
|
ctx.effect(() => {
|
|
1601
1882
|
const disposeListener = ctx.on("user-questions/request", (request, next) => {
|
|
1602
|
-
const
|
|
1603
|
-
if (sessionId === void 0 || !shouldBridgeOwnQuestion({
|
|
1883
|
+
const ownership = resolveInteractionOwnership({
|
|
1604
1884
|
hasExtensionConnection: server.hasConnection(),
|
|
1605
|
-
|
|
1606
|
-
sessionId,
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
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);
|
|
1610
1895
|
}, { prepend: true });
|
|
1611
1896
|
return () => {
|
|
1612
1897
|
disposeListener();
|
|
1613
1898
|
questions.dispose();
|
|
1614
1899
|
};
|
|
1615
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");
|
|
1616
1925
|
const route = {
|
|
1617
1926
|
path: BRIDGE_PATH,
|
|
1618
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
|
|
@@ -167,6 +167,23 @@ function parseBridgeFrame(text) {
|
|
|
167
167
|
id: frame.id,
|
|
168
168
|
sessionId: frame.sessionId
|
|
169
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;
|
|
170
187
|
case "ping": return { t: "ping" };
|
|
171
188
|
case "error": return typeof frame.code === "string" && typeof frame.message === "string" ? {
|
|
172
189
|
t: "error",
|
|
@@ -179,7 +196,7 @@ function parseBridgeFrame(text) {
|
|
|
179
196
|
function isCaps(value) {
|
|
180
197
|
if (typeof value !== "object" || value === null) return false;
|
|
181
198
|
const caps = value;
|
|
182
|
-
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");
|
|
183
200
|
}
|
|
184
201
|
/** Narrow one question item; unknown extra keys (e.g. host `intent`) pass through. */
|
|
185
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
|