dsh-browser-firefox 0.4.6 → 0.4.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.js +450 -51
- package/lib/protocol.js +19 -2
- package/lib/types/approvals.d.ts +73 -0
- package/lib/types/extension-sessions.d.ts +72 -12
- package/lib/types/protocol.d.ts +25 -0
- package/lib/types/questions.d.ts +1 -1
- package/lib/types/server.d.ts +23 -0
- package/lib/types/session-follow.d.ts +46 -0
- package/lib/types/waterfall.d.ts +49 -0
- package/package.json +29 -33
- package/src/approvals.ts +143 -0
- package/src/extension-sessions.ts +123 -16
- package/src/index.ts +121 -20
- package/src/protocol.ts +35 -0
- package/src/questions.ts +20 -38
- package/src/server.ts +59 -4
- package/src/session-follow.ts +76 -0
- package/src/waterfall.ts +91 -0
package/lib/index.js
CHANGED
|
@@ -6,6 +6,29 @@ import { randomBytes, randomUUID, timingSafeEqual } from "node:crypto";
|
|
|
6
6
|
import { WebSocket, WebSocketServer } from "ws";
|
|
7
7
|
import { chmod, mkdir, readFile, rename, writeFile } from "node:fs/promises";
|
|
8
8
|
import { dirname } from "node:path";
|
|
9
|
+
//#region lib/types/session-follow.js
|
|
10
|
+
/**
|
|
11
|
+
* Session id displayed by a `session/follow` stream, read from the host's
|
|
12
|
+
* `SessionFollowRequest.address`. A subagent address carries the child session
|
|
13
|
+
* on `childSessionId`; ownership then walks up to the parent conversation, so
|
|
14
|
+
* either form routes the card correctly.
|
|
15
|
+
*
|
|
16
|
+
* @param args - native args of the `stream.open` frame.
|
|
17
|
+
* @returns the followed session id, or undefined when absent/malformed.
|
|
18
|
+
*/
|
|
19
|
+
function followedSessionOf(args) {
|
|
20
|
+
const request = args.request;
|
|
21
|
+
if (typeof request !== "object" || request === null || Array.isArray(request)) return void 0;
|
|
22
|
+
const address = request.address;
|
|
23
|
+
if (typeof address !== "object" || address === null || Array.isArray(address)) return void 0;
|
|
24
|
+
const fields = address;
|
|
25
|
+
if (fields.kind === "session") return nonEmptyString(fields.sessionId);
|
|
26
|
+
if (fields.kind === "subagent") return nonEmptyString(fields.childSessionId);
|
|
27
|
+
}
|
|
28
|
+
function nonEmptyString(value) {
|
|
29
|
+
return typeof value === "string" && value.length > 0 ? value : void 0;
|
|
30
|
+
}
|
|
31
|
+
//#endregion
|
|
9
32
|
//#region lib/types/token.js
|
|
10
33
|
/**
|
|
11
34
|
* Bridge bearer-token lifecycle: generation, constant-time verification, and
|
|
@@ -166,10 +189,16 @@ function delay(ms) {
|
|
|
166
189
|
/**
|
|
167
190
|
* Race one promise against an abort signal. Resolves `undefined` when the
|
|
168
191
|
* signal wins; the loser's handlers are always attached, so a late rejection
|
|
169
|
-
* cannot surface as an unhandled rejection
|
|
192
|
+
* cannot surface as an unhandled rejection — the host loader escalates those
|
|
193
|
+
* to a fatal load failure.
|
|
194
|
+
*
|
|
195
|
+
* Exported for tests.
|
|
170
196
|
*/
|
|
171
197
|
function raceAbort(promise, signal) {
|
|
172
|
-
if (signal.aborted)
|
|
198
|
+
if (signal.aborted) {
|
|
199
|
+
promise.catch(() => void 0);
|
|
200
|
+
return Promise.resolve(void 0);
|
|
201
|
+
}
|
|
173
202
|
return new Promise((resolve, reject) => {
|
|
174
203
|
const onAbort = () => {
|
|
175
204
|
resolve(void 0);
|
|
@@ -347,6 +376,10 @@ var BridgeServer = class {
|
|
|
347
376
|
clientSupportsQuestions() {
|
|
348
377
|
return this.current?.clientCaps.questions === true;
|
|
349
378
|
}
|
|
379
|
+
/** @returns whether the connected extension advertised approval-card support. */
|
|
380
|
+
clientSupportsApprovals() {
|
|
381
|
+
return this.current?.clientCaps.approvals === true;
|
|
382
|
+
}
|
|
350
383
|
/**
|
|
351
384
|
* Push one unsolicited server frame (question.requested/question.resolved)
|
|
352
385
|
* to the connected extension. No-op when no extension is connected or the
|
|
@@ -506,6 +539,15 @@ var BridgeServer = class {
|
|
|
506
539
|
});
|
|
507
540
|
return;
|
|
508
541
|
}
|
|
542
|
+
if (frame.method === "bridge/approvalDecision") {
|
|
543
|
+
sendFrame(conn.ws, {
|
|
544
|
+
t: "rpc.result",
|
|
545
|
+
id: frame.id,
|
|
546
|
+
ok: true,
|
|
547
|
+
value: this.deps.answerApproval(frame.args)
|
|
548
|
+
});
|
|
549
|
+
return;
|
|
550
|
+
}
|
|
509
551
|
try {
|
|
510
552
|
const result = await this.deps.dispatchRpc(frame.method, frame.args, conn.abort.signal);
|
|
511
553
|
if (result.ok) sendFrame(conn.ws, {
|
|
@@ -550,12 +592,14 @@ var BridgeServer = class {
|
|
|
550
592
|
conn.abort.signal.addEventListener("abort", () => {
|
|
551
593
|
abort.abort();
|
|
552
594
|
}, { once: true });
|
|
595
|
+
const followedSessionId = frame.method === "session/follow" ? followedSessionOf(frame.args) : void 0;
|
|
553
596
|
let pump;
|
|
554
597
|
pump = (async () => {
|
|
555
598
|
let iterator;
|
|
556
599
|
try {
|
|
557
600
|
iterator = (await this.deps.openStream(frame.method, frame.args, abort.signal))[Symbol.asyncIterator]();
|
|
558
601
|
for (;;) {
|
|
602
|
+
if (abort.signal.aborted) break;
|
|
559
603
|
const next = await raceAbort(iterator.next(), abort.signal);
|
|
560
604
|
if (next === void 0 || next.done === true) break;
|
|
561
605
|
if (ws.readyState !== WebSocket.OPEN) break;
|
|
@@ -567,23 +611,32 @@ var BridgeServer = class {
|
|
|
567
611
|
});
|
|
568
612
|
}
|
|
569
613
|
} catch (error) {
|
|
570
|
-
if (!abort.signal.aborted && ws.readyState === WebSocket.OPEN)
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
614
|
+
if (!abort.signal.aborted && ws.readyState === WebSocket.OPEN) try {
|
|
615
|
+
sendFrame(ws, {
|
|
616
|
+
t: "stream.error",
|
|
617
|
+
id: frame.id,
|
|
618
|
+
message: String(error)
|
|
619
|
+
});
|
|
620
|
+
} catch {}
|
|
575
621
|
} finally {
|
|
576
622
|
if (iterator !== void 0) try {
|
|
577
623
|
await iterator.return?.();
|
|
578
624
|
} catch {}
|
|
579
625
|
const current = this.current;
|
|
580
626
|
if (current !== null && current.streams.get(frame.id)?.pump === pump) current.streams.delete(frame.id);
|
|
627
|
+
if (followedSessionId !== void 0) try {
|
|
628
|
+
this.deps.onFollowChanged?.(followedSessionId, false);
|
|
629
|
+
} catch {}
|
|
581
630
|
}
|
|
582
631
|
})();
|
|
583
632
|
conn.streams.set(frame.id, {
|
|
584
633
|
abort,
|
|
585
634
|
pump
|
|
586
635
|
});
|
|
636
|
+
pump.catch(() => {});
|
|
637
|
+
if (followedSessionId !== void 0) try {
|
|
638
|
+
this.deps.onFollowChanged?.(followedSessionId, true);
|
|
639
|
+
} catch {}
|
|
587
640
|
}
|
|
588
641
|
/**
|
|
589
642
|
* Backpressure gate for stream pumps. Returns false when the wait ended
|
|
@@ -1243,9 +1296,19 @@ function withSessionWorkspace(dispatch, workspacePath, warn) {
|
|
|
1243
1296
|
//#region lib/types/extension-sessions.js
|
|
1244
1297
|
/**
|
|
1245
1298
|
* Track session ids that the browser extension has driven through the bridge.
|
|
1246
|
-
* Sessions the extension never touched must keep the host
|
|
1247
|
-
*
|
|
1248
|
-
*
|
|
1299
|
+
* Sessions the extension never touched must keep the host waterfalls
|
|
1300
|
+
* (user-questions, approvals) so the dsh web UI (or any other answerer)
|
|
1301
|
+
* renders the cards.
|
|
1302
|
+
*
|
|
1303
|
+
* Ownership has three sources, all revocable:
|
|
1304
|
+
* - **driven**: the extension created or prompted the session (kept until the
|
|
1305
|
+
* plugin unloads);
|
|
1306
|
+
* - **followed**: an open `session/follow` stream means the sidebar is
|
|
1307
|
+
* displaying that session right now (released when the stream closes);
|
|
1308
|
+
* - **inherited**: an ask from a subagent child session belongs to the
|
|
1309
|
+
* nearest extension-owned ancestor, so child questions/approvals surface in
|
|
1310
|
+
* the parent conversation the user is actually watching.
|
|
1311
|
+
*
|
|
1249
1312
|
* @module
|
|
1250
1313
|
*/
|
|
1251
1314
|
/** Mutable registry of extension-owned session ids. */
|
|
@@ -1255,6 +1318,10 @@ var ExtensionSessionRegistry = class {
|
|
|
1255
1318
|
note(sessionId) {
|
|
1256
1319
|
if (typeof sessionId === "string" && sessionId.length > 0) this.ids.add(sessionId);
|
|
1257
1320
|
}
|
|
1321
|
+
/** Forget a session (a failed dispatch revoked an optimistic note). */
|
|
1322
|
+
forget(sessionId) {
|
|
1323
|
+
if (typeof sessionId === "string") this.ids.delete(sessionId);
|
|
1324
|
+
}
|
|
1258
1325
|
/** Whether the extension has touched this session over the bridge. */
|
|
1259
1326
|
has(sessionId) {
|
|
1260
1327
|
return typeof sessionId === "string" && this.ids.has(sessionId);
|
|
@@ -1265,15 +1332,161 @@ var ExtensionSessionRegistry = class {
|
|
|
1265
1332
|
}
|
|
1266
1333
|
};
|
|
1267
1334
|
/**
|
|
1268
|
-
*
|
|
1269
|
-
*
|
|
1270
|
-
*
|
|
1271
|
-
*
|
|
1272
|
-
* the native answerer waterfall (dsh web) keeps working.
|
|
1335
|
+
* Live `session/follow` subscriptions: the sidebar is displaying these
|
|
1336
|
+
* sessions, so they count as extension-owned while the stream is open.
|
|
1337
|
+
* Reference-counted per session because one session can be followed by more
|
|
1338
|
+
* than one panel connection.
|
|
1273
1339
|
*/
|
|
1274
|
-
|
|
1275
|
-
|
|
1340
|
+
var FollowedSessions = class {
|
|
1341
|
+
counts = /* @__PURE__ */ new Map();
|
|
1342
|
+
/** Register one open follow stream for a session. */
|
|
1343
|
+
open(sessionId) {
|
|
1344
|
+
if (typeof sessionId !== "string" || sessionId.length === 0) return;
|
|
1345
|
+
this.counts.set(sessionId, (this.counts.get(sessionId) ?? 0) + 1);
|
|
1346
|
+
}
|
|
1347
|
+
/** Release one follow stream; the last release drops the session. */
|
|
1348
|
+
close(sessionId) {
|
|
1349
|
+
if (typeof sessionId !== "string") return;
|
|
1350
|
+
const current = this.counts.get(sessionId);
|
|
1351
|
+
if (current === void 0) return;
|
|
1352
|
+
if (current <= 1) this.counts.delete(sessionId);
|
|
1353
|
+
else this.counts.set(sessionId, current - 1);
|
|
1354
|
+
}
|
|
1355
|
+
/** Whether any open follow stream is displaying this session. */
|
|
1356
|
+
has(sessionId) {
|
|
1357
|
+
return typeof sessionId === "string" && this.counts.has(sessionId);
|
|
1358
|
+
}
|
|
1359
|
+
/** Drop every subscription (bridge generation replacement, plugin unload). */
|
|
1360
|
+
clear() {
|
|
1361
|
+
this.counts.clear();
|
|
1362
|
+
}
|
|
1363
|
+
};
|
|
1364
|
+
/** Maximum ancestor hops when looking for an owned session. */
|
|
1365
|
+
const MAX_OWNER_DEPTH = 8;
|
|
1366
|
+
/** Whether one exact session id is extension-owned by either source. */
|
|
1367
|
+
function isOwnedSession(sessionId, sources) {
|
|
1368
|
+
if (sessionId === void 0) return false;
|
|
1369
|
+
return sources.driven.has(sessionId) || sources.followed?.has(sessionId) === true;
|
|
1276
1370
|
}
|
|
1371
|
+
/**
|
|
1372
|
+
* Resolve which session id the sidebar card should be filed under: the asking
|
|
1373
|
+
* session itself when it is owned, otherwise the nearest owned ancestor (a
|
|
1374
|
+
* subagent child session inherits its parent's ownership).
|
|
1375
|
+
*
|
|
1376
|
+
* @param sessionId - the asking session (the host event's agent id).
|
|
1377
|
+
* @param sources - ownership sources.
|
|
1378
|
+
* @param parentOf - session-id → parent-session-id lookup; absent disables inheritance.
|
|
1379
|
+
* @returns the owned session id to route to, or undefined when nothing owns it.
|
|
1380
|
+
*/
|
|
1381
|
+
function owningSessionId(sessionId, sources, parentOf) {
|
|
1382
|
+
if (sessionId === void 0) return void 0;
|
|
1383
|
+
if (isOwnedSession(sessionId, sources)) return sessionId;
|
|
1384
|
+
if (parentOf === void 0) return void 0;
|
|
1385
|
+
const seen = /* @__PURE__ */ new Set([sessionId]);
|
|
1386
|
+
let current = sessionId;
|
|
1387
|
+
for (let depth = 0; depth < MAX_OWNER_DEPTH; depth += 1) {
|
|
1388
|
+
const parent = parentOf(current);
|
|
1389
|
+
if (parent === void 0 || parent.length === 0 || seen.has(parent)) return void 0;
|
|
1390
|
+
if (isOwnedSession(parent, sources)) return parent;
|
|
1391
|
+
seen.add(parent);
|
|
1392
|
+
current = parent;
|
|
1393
|
+
}
|
|
1394
|
+
}
|
|
1395
|
+
/**
|
|
1396
|
+
* Decide whether the bridge should own one host interaction (question or
|
|
1397
|
+
* approval). All gates must pass: an extension is connected, it advertised
|
|
1398
|
+
* the matching card capability in its hello caps, and the asking session is
|
|
1399
|
+
* extension-owned (directly or through an owned ancestor). Everything else
|
|
1400
|
+
* falls through to `next()` so the native answerer waterfall keeps working.
|
|
1401
|
+
*/
|
|
1402
|
+
function resolveInteractionOwnership(input) {
|
|
1403
|
+
if (!input.hasExtensionConnection) return {
|
|
1404
|
+
owned: false,
|
|
1405
|
+
reason: "no-connection"
|
|
1406
|
+
};
|
|
1407
|
+
if (!input.clientSupports) return {
|
|
1408
|
+
owned: false,
|
|
1409
|
+
reason: "unsupported-client"
|
|
1410
|
+
};
|
|
1411
|
+
const sessionId = owningSessionId(input.sessionId, input.sources, input.parentOf);
|
|
1412
|
+
if (sessionId === void 0) return {
|
|
1413
|
+
owned: false,
|
|
1414
|
+
reason: "not-owned"
|
|
1415
|
+
};
|
|
1416
|
+
return {
|
|
1417
|
+
owned: true,
|
|
1418
|
+
sessionId
|
|
1419
|
+
};
|
|
1420
|
+
}
|
|
1421
|
+
//#endregion
|
|
1422
|
+
//#region lib/types/waterfall.js
|
|
1423
|
+
/**
|
|
1424
|
+
* Bookkeeping shared by the host waterfalls the bridge claims
|
|
1425
|
+
* (`user-questions/request`, `approval/request`).
|
|
1426
|
+
*
|
|
1427
|
+
* Both bridges need exactly the same lifecycle: one pending entry per
|
|
1428
|
+
* generated id, a **single settlement** guarantee against concurrent paths
|
|
1429
|
+
* (extension answer vs host abort vs connection loss vs plugin unload), an
|
|
1430
|
+
* abort listener that is detached once settled, and the id+session match the
|
|
1431
|
+
* uplink must pass before it may touch an entry. Only the outcome vocabulary
|
|
1432
|
+
* and the push/settle policy differ, and those stay in the domain modules.
|
|
1433
|
+
*
|
|
1434
|
+
* @module
|
|
1435
|
+
*/
|
|
1436
|
+
/** Registry of pending waterfall requests, keyed by the id sent to the extension. */
|
|
1437
|
+
var PendingWaterfalls = class {
|
|
1438
|
+
entries = /* @__PURE__ */ new Map();
|
|
1439
|
+
/**
|
|
1440
|
+
* Track one request pushed to the extension.
|
|
1441
|
+
*
|
|
1442
|
+
* @param input - id, asking session, host cancellation signal, the domain's
|
|
1443
|
+
* settlement policy, and the abort handler (typically: tell the extension
|
|
1444
|
+
* the card is gone, then settle).
|
|
1445
|
+
*/
|
|
1446
|
+
register(input) {
|
|
1447
|
+
const entry = {
|
|
1448
|
+
sessionId: input.sessionId,
|
|
1449
|
+
payload: input.payload,
|
|
1450
|
+
complete: input.complete,
|
|
1451
|
+
signal: input.signal,
|
|
1452
|
+
onAbort: input.onAbort,
|
|
1453
|
+
settled: false
|
|
1454
|
+
};
|
|
1455
|
+
if (input.signal !== void 0) input.signal.addEventListener("abort", input.onAbort, { once: true });
|
|
1456
|
+
this.entries.set(input.id, entry);
|
|
1457
|
+
}
|
|
1458
|
+
/**
|
|
1459
|
+
* Whether an unsettled entry exists for exactly this id and session. The
|
|
1460
|
+
* uplink must not be able to settle another session's card.
|
|
1461
|
+
*/
|
|
1462
|
+
matches(id, sessionId) {
|
|
1463
|
+
const entry = this.entries.get(id);
|
|
1464
|
+
return entry !== void 0 && !entry.settled && entry.sessionId === sessionId;
|
|
1465
|
+
}
|
|
1466
|
+
/** Domain payload registered with one unsettled entry (undefined once settled). */
|
|
1467
|
+
payloadOf(id) {
|
|
1468
|
+
return this.entries.get(id)?.payload;
|
|
1469
|
+
}
|
|
1470
|
+
/**
|
|
1471
|
+
* Settle one request. Later settlements for the same id are no-ops, and the
|
|
1472
|
+
* entry is dropped together with its abort listener.
|
|
1473
|
+
*
|
|
1474
|
+
* @returns true when this call performed the settlement.
|
|
1475
|
+
*/
|
|
1476
|
+
settle(id, outcome) {
|
|
1477
|
+
const entry = this.entries.get(id);
|
|
1478
|
+
if (entry === void 0 || entry.settled) return false;
|
|
1479
|
+
entry.settled = true;
|
|
1480
|
+
this.entries.delete(id);
|
|
1481
|
+
if (entry.onAbort !== void 0) entry.signal?.removeEventListener("abort", entry.onAbort);
|
|
1482
|
+
entry.complete(outcome);
|
|
1483
|
+
return true;
|
|
1484
|
+
}
|
|
1485
|
+
/** Every unsettled id, for the connection-loss and unload sweeps. */
|
|
1486
|
+
ids() {
|
|
1487
|
+
return [...this.entries.keys()];
|
|
1488
|
+
}
|
|
1489
|
+
};
|
|
1277
1490
|
//#endregion
|
|
1278
1491
|
//#region lib/types/questions.js
|
|
1279
1492
|
/**
|
|
@@ -1329,7 +1542,7 @@ function asQuestionAnswerArgs(value) {
|
|
|
1329
1542
|
/** Owns the user-questions waterfall for extension-driven sessions. */
|
|
1330
1543
|
var QuestionBridge = class {
|
|
1331
1544
|
deps;
|
|
1332
|
-
|
|
1545
|
+
waterfalls = new PendingWaterfalls();
|
|
1333
1546
|
constructor(deps) {
|
|
1334
1547
|
this.deps = deps;
|
|
1335
1548
|
}
|
|
@@ -1346,38 +1559,33 @@ var QuestionBridge = class {
|
|
|
1346
1559
|
ask(sessionId, questions, next, signal) {
|
|
1347
1560
|
if (signal?.aborted) return Promise.reject(abortedError());
|
|
1348
1561
|
const questionId = randomUUID();
|
|
1562
|
+
const questionIds = new Set(questions.map((question) => question.id));
|
|
1349
1563
|
return new Promise((resolve, reject) => {
|
|
1350
|
-
const
|
|
1564
|
+
const settle = (outcome) => {
|
|
1565
|
+
this.waterfalls.settle(questionId, outcome);
|
|
1566
|
+
};
|
|
1567
|
+
this.waterfalls.register({
|
|
1568
|
+
id: questionId,
|
|
1351
1569
|
sessionId,
|
|
1352
|
-
questionIds: new Set(questions.map((question) => question.id)),
|
|
1353
|
-
settled: false,
|
|
1354
1570
|
signal,
|
|
1571
|
+
payload: questionIds,
|
|
1355
1572
|
complete: (outcome) => {
|
|
1356
|
-
if (entry.settled) return;
|
|
1357
|
-
entry.settled = true;
|
|
1358
|
-
this.pending.delete(questionId);
|
|
1359
|
-
if (entry.onAbort !== void 0) entry.signal?.removeEventListener("abort", entry.onAbort);
|
|
1360
1573
|
if (outcome.kind === "answer") resolve(outcome.answer);
|
|
1361
1574
|
else if (outcome.kind === "reject") reject(outcome.error);
|
|
1362
1575
|
else next().then(resolve, reject);
|
|
1363
1576
|
},
|
|
1364
|
-
onAbort:
|
|
1365
|
-
};
|
|
1366
|
-
if (signal !== void 0) {
|
|
1367
|
-
entry.onAbort = () => {
|
|
1577
|
+
onAbort: () => {
|
|
1368
1578
|
this.deps.push({
|
|
1369
1579
|
t: "question.resolved",
|
|
1370
1580
|
id: questionId,
|
|
1371
1581
|
sessionId
|
|
1372
1582
|
});
|
|
1373
|
-
|
|
1583
|
+
settle({
|
|
1374
1584
|
kind: "reject",
|
|
1375
1585
|
error: abortedError()
|
|
1376
1586
|
});
|
|
1377
|
-
}
|
|
1378
|
-
|
|
1379
|
-
}
|
|
1380
|
-
this.pending.set(questionId, entry);
|
|
1587
|
+
}
|
|
1588
|
+
});
|
|
1381
1589
|
this.deps.push({
|
|
1382
1590
|
t: "question.requested",
|
|
1383
1591
|
id: questionId,
|
|
@@ -1391,24 +1599,23 @@ var QuestionBridge = class {
|
|
|
1391
1599
|
* rpc. The server replies with this receipt.
|
|
1392
1600
|
*/
|
|
1393
1601
|
answer(args) {
|
|
1394
|
-
|
|
1395
|
-
if (entry === void 0 || entry.settled || entry.sessionId !== args.sessionId) return {
|
|
1602
|
+
if (!this.waterfalls.matches(args.questionId, args.sessionId)) return {
|
|
1396
1603
|
accepted: false,
|
|
1397
1604
|
reason: "not-pending"
|
|
1398
1605
|
};
|
|
1399
1606
|
if (args.decline === true) {
|
|
1400
|
-
|
|
1607
|
+
this.waterfalls.settle(args.questionId, {
|
|
1401
1608
|
kind: "reject",
|
|
1402
1609
|
error: declinedError()
|
|
1403
1610
|
});
|
|
1404
1611
|
return { accepted: true };
|
|
1405
1612
|
}
|
|
1406
|
-
const answer = asAnswerPayload(args.answer,
|
|
1613
|
+
const answer = asAnswerPayload(args.answer, this.waterfalls.payloadOf(args.questionId) ?? /* @__PURE__ */ new Set());
|
|
1407
1614
|
if (answer === void 0) return {
|
|
1408
1615
|
accepted: false,
|
|
1409
1616
|
reason: "bad-answer"
|
|
1410
1617
|
};
|
|
1411
|
-
|
|
1618
|
+
this.waterfalls.settle(args.questionId, {
|
|
1412
1619
|
kind: "answer",
|
|
1413
1620
|
answer
|
|
1414
1621
|
});
|
|
@@ -1416,17 +1623,132 @@ var QuestionBridge = class {
|
|
|
1416
1623
|
}
|
|
1417
1624
|
/** Bridge connection replaced/closed: delegate every unsettled question. */
|
|
1418
1625
|
delegateAll() {
|
|
1419
|
-
for (const
|
|
1626
|
+
for (const id of this.waterfalls.ids()) this.waterfalls.settle(id, { kind: "delegate" });
|
|
1420
1627
|
}
|
|
1421
1628
|
/** Plugin unload: reject everything so no tool call outlives the plugin. */
|
|
1422
1629
|
dispose() {
|
|
1423
|
-
for (const
|
|
1630
|
+
for (const id of this.waterfalls.ids()) this.waterfalls.settle(id, {
|
|
1424
1631
|
kind: "reject",
|
|
1425
1632
|
error: /* @__PURE__ */ new Error("browser bridge plugin was unloaded")
|
|
1426
1633
|
});
|
|
1427
1634
|
}
|
|
1428
1635
|
};
|
|
1429
1636
|
//#endregion
|
|
1637
|
+
//#region lib/types/approvals.js
|
|
1638
|
+
/**
|
|
1639
|
+
* Host approval ownership: claim the host `approval/request` waterfall for
|
|
1640
|
+
* extension-owned sessions, push the decision request to the connected
|
|
1641
|
+
* extension, and settle the waterfall with the sidebar's choice.
|
|
1642
|
+
*
|
|
1643
|
+
* Settlement paths (first wins, later ones no-op):
|
|
1644
|
+
* - the sidebar decides → the waterfall resolves with that outcome;
|
|
1645
|
+
* - the host aborts `request.signal` (turn cancelled, session disposed) →
|
|
1646
|
+
* the extension is sent `approval.resolved` and the waterfall resolves
|
|
1647
|
+
* with `'cancelled'` (the host vocabulary has no rejection channel here);
|
|
1648
|
+
* - the bridge connection drops ({@link delegateAll}) → the waterfall is
|
|
1649
|
+
* delegated to `next()` so another answerer (dsh web) can still decide;
|
|
1650
|
+
* - plugin unload ({@link dispose}) → `'unavailable'`, the fail-closed
|
|
1651
|
+
* outcome of the host's closed outcome set.
|
|
1652
|
+
*
|
|
1653
|
+
* @module
|
|
1654
|
+
*/
|
|
1655
|
+
/** Parse the extension's `bridge/approvalDecision` rpc args (ids + closed decision). */
|
|
1656
|
+
function asApprovalDecisionArgs(value) {
|
|
1657
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return void 0;
|
|
1658
|
+
const args = value;
|
|
1659
|
+
if (typeof args.approvalId !== "string" || typeof args.sessionId !== "string") return void 0;
|
|
1660
|
+
const decision = args.decision;
|
|
1661
|
+
if (decision !== "allowed-once" && decision !== "rejected") return void 0;
|
|
1662
|
+
return {
|
|
1663
|
+
approvalId: args.approvalId,
|
|
1664
|
+
sessionId: args.sessionId,
|
|
1665
|
+
decision
|
|
1666
|
+
};
|
|
1667
|
+
}
|
|
1668
|
+
/** Owns the host approval waterfall for extension-driven sessions. */
|
|
1669
|
+
var ApprovalBridge = class {
|
|
1670
|
+
deps;
|
|
1671
|
+
waterfalls = new PendingWaterfalls();
|
|
1672
|
+
constructor(deps) {
|
|
1673
|
+
this.deps = deps;
|
|
1674
|
+
}
|
|
1675
|
+
/**
|
|
1676
|
+
* Own one host waterfall request: push `approval.requested` to the
|
|
1677
|
+
* extension and suspend until one of the settlement paths wins.
|
|
1678
|
+
*
|
|
1679
|
+
* @param routingSessionId - extension-visible session id (the owned session the card is filed under).
|
|
1680
|
+
* @param prompt - tool name, optional call id and human-readable reason.
|
|
1681
|
+
* @param next - waterfall delegation, used when the bridge connection drops.
|
|
1682
|
+
* @param signal - host cancellation lifetime for this request.
|
|
1683
|
+
* @returns the host approval outcome.
|
|
1684
|
+
*/
|
|
1685
|
+
ask(routingSessionId, prompt, next, signal) {
|
|
1686
|
+
if (signal?.aborted) return Promise.resolve("cancelled");
|
|
1687
|
+
const approvalId = randomUUID();
|
|
1688
|
+
return new Promise((resolve, reject) => {
|
|
1689
|
+
this.waterfalls.register({
|
|
1690
|
+
id: approvalId,
|
|
1691
|
+
sessionId: routingSessionId,
|
|
1692
|
+
signal,
|
|
1693
|
+
payload: void 0,
|
|
1694
|
+
complete: (outcome) => {
|
|
1695
|
+
if (outcome.kind === "decide") resolve(outcome.outcome);
|
|
1696
|
+
else next().then(resolve, reject);
|
|
1697
|
+
},
|
|
1698
|
+
onAbort: () => {
|
|
1699
|
+
this.deps.push({
|
|
1700
|
+
t: "approval.resolved",
|
|
1701
|
+
id: approvalId,
|
|
1702
|
+
sessionId: routingSessionId
|
|
1703
|
+
});
|
|
1704
|
+
this.waterfalls.settle(approvalId, {
|
|
1705
|
+
kind: "decide",
|
|
1706
|
+
outcome: "cancelled"
|
|
1707
|
+
});
|
|
1708
|
+
}
|
|
1709
|
+
});
|
|
1710
|
+
this.deps.push({
|
|
1711
|
+
t: "approval.requested",
|
|
1712
|
+
id: approvalId,
|
|
1713
|
+
sessionId: routingSessionId,
|
|
1714
|
+
toolName: prompt.toolName,
|
|
1715
|
+
...prompt.callId === void 0 ? {} : { callId: prompt.callId },
|
|
1716
|
+
...prompt.reason === void 0 ? {} : { reason: prompt.reason }
|
|
1717
|
+
});
|
|
1718
|
+
});
|
|
1719
|
+
}
|
|
1720
|
+
/**
|
|
1721
|
+
* Settle one pending approval from the extension's `bridge/approvalDecision`
|
|
1722
|
+
* rpc. The server replies with this receipt.
|
|
1723
|
+
*/
|
|
1724
|
+
decide(args) {
|
|
1725
|
+
if (!this.waterfalls.matches(args.approvalId, args.sessionId)) return {
|
|
1726
|
+
accepted: false,
|
|
1727
|
+
reason: "not-pending"
|
|
1728
|
+
};
|
|
1729
|
+
if (args.decision !== "allowed-once" && args.decision !== "rejected") return {
|
|
1730
|
+
accepted: false,
|
|
1731
|
+
reason: "bad-decision"
|
|
1732
|
+
};
|
|
1733
|
+
this.waterfalls.settle(args.approvalId, {
|
|
1734
|
+
kind: "decide",
|
|
1735
|
+
outcome: args.decision
|
|
1736
|
+
});
|
|
1737
|
+
return { accepted: true };
|
|
1738
|
+
}
|
|
1739
|
+
/** Bridge connection replaced/closed: delegate every unsettled approval. */
|
|
1740
|
+
delegateAll() {
|
|
1741
|
+
for (const id of this.waterfalls.ids()) this.waterfalls.settle(id, { kind: "delegate" });
|
|
1742
|
+
}
|
|
1743
|
+
/** Plugin unload: answer everything fail-closed so no tool call outlives the plugin. */
|
|
1744
|
+
dispose() {
|
|
1745
|
+
for (const id of this.waterfalls.ids()) this.waterfalls.settle(id, {
|
|
1746
|
+
kind: "decide",
|
|
1747
|
+
outcome: "unavailable"
|
|
1748
|
+
});
|
|
1749
|
+
}
|
|
1750
|
+
};
|
|
1751
|
+
//#endregion
|
|
1430
1752
|
//#region lib/types/index.js
|
|
1431
1753
|
/**
|
|
1432
1754
|
* `@deepseek-ai/dsh-bridge-browser`: token-authenticated WebSocket bridge for
|
|
@@ -1492,6 +1814,21 @@ function sessionIdOf(value) {
|
|
|
1492
1814
|
return typeof sessionId === "string" && sessionId.length > 0 ? sessionId : void 0;
|
|
1493
1815
|
}
|
|
1494
1816
|
/**
|
|
1817
|
+
* Human-readable approval reason: the asker's localized text when present
|
|
1818
|
+
* (zh first, then en), otherwise the raw reason string.
|
|
1819
|
+
*
|
|
1820
|
+
* @param request - pending host approval request.
|
|
1821
|
+
* @returns display text, or undefined when the asker supplied none.
|
|
1822
|
+
*/
|
|
1823
|
+
function approvalReasonText(request) {
|
|
1824
|
+
const display = request.displayReason;
|
|
1825
|
+
if (display !== void 0) {
|
|
1826
|
+
const localized = display.zh ?? display["zh-Hans"] ?? display.en;
|
|
1827
|
+
if (typeof localized === "string" && localized.length > 0) return localized;
|
|
1828
|
+
}
|
|
1829
|
+
return typeof request.reason === "string" && request.reason.length > 0 ? request.reason : void 0;
|
|
1830
|
+
}
|
|
1831
|
+
/**
|
|
1495
1832
|
* Apply defaults and direct-call validation at the plugin boundary.
|
|
1496
1833
|
* @param config - Loader-resolved or directly supplied plugin configuration.
|
|
1497
1834
|
* @returns a complete configuration ready for runtime use.
|
|
@@ -1545,12 +1882,25 @@ async function apply(ctx, config) {
|
|
|
1545
1882
|
ctx.logger.warn(message);
|
|
1546
1883
|
}), resolved.deferSessionCreate);
|
|
1547
1884
|
const extensionSessions = new ExtensionSessionRegistry();
|
|
1885
|
+
const followedSessions = new FollowedSessions();
|
|
1886
|
+
const ownershipSources = {
|
|
1887
|
+
driven: extensionSessions,
|
|
1888
|
+
followed: followedSessions
|
|
1889
|
+
};
|
|
1890
|
+
const parentOf = (sessionId) => {
|
|
1891
|
+
return ctx.get("sessions")?.get(sessionId)?.header.parentSession;
|
|
1892
|
+
};
|
|
1548
1893
|
const notingDispatchRpc = async (method, args, signal) => {
|
|
1894
|
+
const tracksOwnership = method === "session/create" || method === "session/prompt";
|
|
1895
|
+
const dispatched = tracksOwnership ? sessionIdOf(args.request) : void 0;
|
|
1896
|
+
if (dispatched !== void 0) extensionSessions.note(dispatched);
|
|
1549
1897
|
const result = await dispatchRpc(method, args, signal);
|
|
1550
|
-
if (result.ok
|
|
1551
|
-
|
|
1552
|
-
|
|
1898
|
+
if (!result.ok) {
|
|
1899
|
+
const code = result.error.code;
|
|
1900
|
+
if (dispatched !== void 0 && (code.includes("not-found") || code.includes("session-invalid"))) extensionSessions.forget(dispatched);
|
|
1901
|
+
return result;
|
|
1553
1902
|
}
|
|
1903
|
+
if (tracksOwnership) extensionSessions.note(sessionIdOf(result.value));
|
|
1554
1904
|
return result;
|
|
1555
1905
|
};
|
|
1556
1906
|
let pluginVersion;
|
|
@@ -1563,6 +1913,9 @@ async function apply(ctx, config) {
|
|
|
1563
1913
|
const questions = new QuestionBridge({ push: (frame) => {
|
|
1564
1914
|
server.push(frame);
|
|
1565
1915
|
} });
|
|
1916
|
+
const approvals = new ApprovalBridge({ push: (frame) => {
|
|
1917
|
+
server.push(frame);
|
|
1918
|
+
} });
|
|
1566
1919
|
const server = new BridgeServer({
|
|
1567
1920
|
token: tokenRes.token,
|
|
1568
1921
|
dispatchRpc: notingDispatchRpc,
|
|
@@ -1584,8 +1937,20 @@ async function apply(ctx, config) {
|
|
|
1584
1937
|
reason: "bad-answer"
|
|
1585
1938
|
} : questions.answer(parsed);
|
|
1586
1939
|
},
|
|
1940
|
+
answerApproval: (args) => {
|
|
1941
|
+
const parsed = asApprovalDecisionArgs(args);
|
|
1942
|
+
return parsed === void 0 ? {
|
|
1943
|
+
accepted: false,
|
|
1944
|
+
reason: "bad-decision"
|
|
1945
|
+
} : approvals.decide(parsed);
|
|
1946
|
+
},
|
|
1587
1947
|
onConnectionLost: () => {
|
|
1588
1948
|
questions.delegateAll();
|
|
1949
|
+
approvals.delegateAll();
|
|
1950
|
+
},
|
|
1951
|
+
onFollowChanged: (sessionId, open) => {
|
|
1952
|
+
if (open) followedSessions.open(sessionId);
|
|
1953
|
+
else followedSessions.close(sessionId);
|
|
1589
1954
|
},
|
|
1590
1955
|
version: pluginVersion,
|
|
1591
1956
|
onVersionMismatch: (clientVersion) => {
|
|
@@ -1597,22 +1962,56 @@ async function apply(ctx, config) {
|
|
|
1597
1962
|
maxInteractiveItems: resolved.maxInteractiveItems
|
|
1598
1963
|
}
|
|
1599
1964
|
});
|
|
1965
|
+
const logDelegation = (kind, sessionId, reason) => {
|
|
1966
|
+
const line = `browser bridge: delegated ${kind} for session ${sessionId ?? "<unknown>"} (${reason})`;
|
|
1967
|
+
if (reason === "unsupported-client") ctx.logger.info(line);
|
|
1968
|
+
else ctx.logger.debug(line);
|
|
1969
|
+
};
|
|
1600
1970
|
ctx.effect(() => {
|
|
1601
1971
|
const disposeListener = ctx.on("user-questions/request", (request, next) => {
|
|
1602
|
-
const
|
|
1603
|
-
if (sessionId === void 0 || !shouldBridgeOwnQuestion({
|
|
1972
|
+
const ownership = resolveInteractionOwnership({
|
|
1604
1973
|
hasExtensionConnection: server.hasConnection(),
|
|
1605
|
-
|
|
1606
|
-
sessionId,
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1974
|
+
clientSupports: server.clientSupportsQuestions(),
|
|
1975
|
+
sessionId: request.agent?.id,
|
|
1976
|
+
sources: ownershipSources,
|
|
1977
|
+
parentOf
|
|
1978
|
+
});
|
|
1979
|
+
if (!ownership.owned) {
|
|
1980
|
+
logDelegation("user-questions", request.agent?.id, ownership.reason);
|
|
1981
|
+
return next();
|
|
1982
|
+
}
|
|
1983
|
+
return questions.ask(ownership.sessionId, request.questions, next, request.signal);
|
|
1610
1984
|
}, { prepend: true });
|
|
1611
1985
|
return () => {
|
|
1612
1986
|
disposeListener();
|
|
1613
1987
|
questions.dispose();
|
|
1614
1988
|
};
|
|
1615
1989
|
}, "bridge-browser: user-questions answerer");
|
|
1990
|
+
ctx.effect(() => {
|
|
1991
|
+
const disposeListener = ctx.on("approval/request", (request, next) => {
|
|
1992
|
+
const ownership = resolveInteractionOwnership({
|
|
1993
|
+
hasExtensionConnection: server.hasConnection(),
|
|
1994
|
+
clientSupports: server.clientSupportsApprovals(),
|
|
1995
|
+
sessionId: request.agent?.id,
|
|
1996
|
+
sources: ownershipSources,
|
|
1997
|
+
parentOf
|
|
1998
|
+
});
|
|
1999
|
+
if (!ownership.owned) {
|
|
2000
|
+
logDelegation("approval", request.agent?.id, ownership.reason);
|
|
2001
|
+
return next();
|
|
2002
|
+
}
|
|
2003
|
+
const reason = approvalReasonText(request);
|
|
2004
|
+
return approvals.ask(ownership.sessionId, {
|
|
2005
|
+
toolName: request.toolName,
|
|
2006
|
+
...typeof request.callId === "string" ? { callId: request.callId } : {},
|
|
2007
|
+
...reason === void 0 ? {} : { reason }
|
|
2008
|
+
}, next, request.signal);
|
|
2009
|
+
}, { prepend: true });
|
|
2010
|
+
return () => {
|
|
2011
|
+
disposeListener();
|
|
2012
|
+
approvals.dispose();
|
|
2013
|
+
};
|
|
2014
|
+
}, "bridge-browser: approval answerer");
|
|
1616
2015
|
const route = {
|
|
1617
2016
|
path: BRIDGE_PATH,
|
|
1618
2017
|
handler: (req, socket, head) => {
|