@chenacc/dsh-agentbridge 0.4.7 → 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/approval-broker.js +88 -0
- package/lib/index.js +105 -21
- package/package.json +2 -1
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Holds approval decisions that are waiting on a phone.
|
|
3
|
+
*
|
|
4
|
+
* DSH asks for permission through a hook that expects an answer in return — the tool
|
|
5
|
+
* call is suspended until someone says yes or no. The phone is not in that call stack,
|
|
6
|
+
* so the plugin parks a promise here, shows a card, and resolves the promise when the
|
|
7
|
+
* decision comes back over the bridge.
|
|
8
|
+
*
|
|
9
|
+
* Two things must never leak: a promise that nobody ever settles (the turn would hang
|
|
10
|
+
* forever) and a decision delivered after the fact (it would answer the wrong card).
|
|
11
|
+
* So every waiter is either resolved, rejected by timeout, or cancelled with its turn.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** Outcomes DSH understands for an approval. */
|
|
15
|
+
export const APPROVAL_OUTCOMES = Object.freeze(["allowed-once", "rejected", "cancelled", "unavailable"]);
|
|
16
|
+
|
|
17
|
+
export function normalizeApprovalOutcome(value) {
|
|
18
|
+
return APPROVAL_OUTCOMES.includes(value) ? value : "rejected";
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export class ApprovalBroker {
|
|
22
|
+
#waiters = new Map();
|
|
23
|
+
|
|
24
|
+
constructor({ now = () => Date.now(), timeoutMs = 5 * 60_000 } = {}) {
|
|
25
|
+
this.now = now;
|
|
26
|
+
this.timeoutMs = timeoutMs;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
get size() {
|
|
30
|
+
return this.#waiters.size;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
has(id) {
|
|
34
|
+
return this.#waiters.has(id);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Wait for the decision on `id`.
|
|
39
|
+
*
|
|
40
|
+
* Resolves with one of {@link APPROVAL_OUTCOMES}. A timeout resolves as `cancelled`
|
|
41
|
+
* rather than `rejected`: nobody declined, the chance simply expired, and the turn
|
|
42
|
+
* should say that instead of claiming the user refused.
|
|
43
|
+
*/
|
|
44
|
+
wait(id, { signal } = {}) {
|
|
45
|
+
if (this.#waiters.has(id)) throw new Error(`approval ${id} is already pending`);
|
|
46
|
+
return new Promise((resolve) => {
|
|
47
|
+
const finish = (outcome) => {
|
|
48
|
+
const waiter = this.#waiters.get(id);
|
|
49
|
+
if (!waiter) return;
|
|
50
|
+
this.#waiters.delete(id);
|
|
51
|
+
clearTimeout(waiter.timer);
|
|
52
|
+
if (signal && waiter.onAbort) signal.removeEventListener("abort", waiter.onAbort);
|
|
53
|
+
resolve(normalizeApprovalOutcome(outcome));
|
|
54
|
+
};
|
|
55
|
+
const waiter = {
|
|
56
|
+
timer: setTimeout(() => finish("cancelled"), this.timeoutMs),
|
|
57
|
+
resolve: finish,
|
|
58
|
+
};
|
|
59
|
+
if (typeof waiter.timer.unref === "function") waiter.timer.unref();
|
|
60
|
+
if (signal) {
|
|
61
|
+
if (signal.aborted) {
|
|
62
|
+
clearTimeout(waiter.timer);
|
|
63
|
+
resolve("cancelled");
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
waiter.onAbort = () => finish("cancelled");
|
|
67
|
+
signal.addEventListener("abort", waiter.onAbort, { once: true });
|
|
68
|
+
}
|
|
69
|
+
this.#waiters.set(id, waiter);
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Deliver a decision. False when the card is gone (already answered or expired). */
|
|
74
|
+
resolve(id, outcome) {
|
|
75
|
+
const waiter = this.#waiters.get(id);
|
|
76
|
+
if (!waiter) return false;
|
|
77
|
+
waiter.resolve(outcome);
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
cancel(id) {
|
|
82
|
+
return this.resolve(id, "cancelled");
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
cancelAll() {
|
|
86
|
+
for (const id of [...this.#waiters.keys()]) this.cancel(id);
|
|
87
|
+
}
|
|
88
|
+
}
|
package/lib/index.js
CHANGED
|
@@ -4,6 +4,7 @@ import { createRequire } from "node:module";
|
|
|
4
4
|
import { homedir } from "node:os";
|
|
5
5
|
import path from "node:path";
|
|
6
6
|
import { fileURLToPath } from "node:url";
|
|
7
|
+
import { ApprovalBroker } from "./approval-broker.js";
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* The tunnel and path-selection machinery lives in the bridge package, because
|
|
@@ -645,7 +646,16 @@ export class PhoneConnections {
|
|
|
645
646
|
// conversation, not to the channel that tried to carry them.
|
|
646
647
|
this.interactionScopes = new Map();
|
|
647
648
|
this.liveModelSelections = new WeakMap();
|
|
649
|
+
// Approvals DSH is waiting on. The phone answers out of band, so the question is
|
|
650
|
+
// parked here until the card comes back — see approval-broker.js.
|
|
651
|
+
this.approvals = new ApprovalBroker();
|
|
652
|
+
this.approvalIds = new Map();
|
|
648
653
|
this.ctx.on("session/event", (session, event) => this.captureEvent(session, event));
|
|
654
|
+
// DSH 0.2.x asks for permission through a waterfall event and waits for a return
|
|
655
|
+
// value; the desktop window answers through the same event. With no live phone
|
|
656
|
+
// channel this returns "unavailable" immediately, so the desktop behaves exactly as
|
|
657
|
+
// it did before this listener existed.
|
|
658
|
+
this.ctx.on("approval/request", (request) => this.approvalRequest(request));
|
|
649
659
|
this.ctx.on("agent/disposed", ({ agent }) => this.dispose(agent.session.id));
|
|
650
660
|
// A conversation reopened inside the desktop app gets a fresh agent; if the
|
|
651
661
|
// bridge already holds a paired session for it, re-attach without waiting for
|
|
@@ -1155,31 +1165,94 @@ export class PhoneConnections {
|
|
|
1155
1165
|
}
|
|
1156
1166
|
}
|
|
1157
1167
|
|
|
1168
|
+
/**
|
|
1169
|
+
* One approval, from DSH's question to the phone's answer.
|
|
1170
|
+
*
|
|
1171
|
+
* Returns what DSH expects back from the hook: `allowed-once` is the only grant,
|
|
1172
|
+
* `rejected` the explicit refusal, `cancelled` when the chance expired or the turn
|
|
1173
|
+
* went away, and `unavailable` when there is no live phone channel to ask through —
|
|
1174
|
+
* in that case the desktop window owns the decision, exactly as before.
|
|
1175
|
+
*/
|
|
1176
|
+
async approvalRequest(request) {
|
|
1177
|
+
const sessionId = request?.agent?.session?.id;
|
|
1178
|
+
const state = sessionId ? this.bySession.get(sessionId) : null;
|
|
1179
|
+
if (!state || state.controller.signal.aborted) return "unavailable";
|
|
1180
|
+
|
|
1181
|
+
const approvalId = randomUUID();
|
|
1182
|
+
const scope = this.#interactionScope(sessionId);
|
|
1183
|
+
scope.queue.push({
|
|
1184
|
+
kind: "approval-requested",
|
|
1185
|
+
requestId: approvalId,
|
|
1186
|
+
dshApprovalId: approvalId,
|
|
1187
|
+
toolName: clipped(request?.toolName, 200) || "tool",
|
|
1188
|
+
...(request?.reason ? { reason: clipped(request.reason, 2_000) } : {}),
|
|
1189
|
+
...(request?.callId ? { callId: clipped(request.callId, 200) } : {}),
|
|
1190
|
+
});
|
|
1191
|
+
void this.#drainInteractions(state, scope, sessionId);
|
|
1192
|
+
|
|
1193
|
+
const pending = this.approvalIds.get(sessionId) ?? new Set();
|
|
1194
|
+
pending.add(approvalId);
|
|
1195
|
+
this.approvalIds.set(sessionId, pending);
|
|
1196
|
+
|
|
1197
|
+
const outcome = await this.approvals.wait(approvalId, {
|
|
1198
|
+
signal: request?.signal ?? state.controller.signal,
|
|
1199
|
+
});
|
|
1200
|
+
|
|
1201
|
+
pending.delete(approvalId);
|
|
1202
|
+
if (pending.size === 0) this.approvalIds.delete(sessionId);
|
|
1203
|
+
|
|
1204
|
+
// Settle the card so it stops looking answerable, whichever way it ended.
|
|
1205
|
+
const live = this.bySession.get(sessionId);
|
|
1206
|
+
if (live && !live.controller.signal.aborted) {
|
|
1207
|
+
const settle = this.#interactionScope(sessionId);
|
|
1208
|
+
settle.queue.push({ kind: "approval-resolved", requestId: "", dshApprovalId: approvalId, outcome });
|
|
1209
|
+
void this.#drainInteractions(live, settle, sessionId);
|
|
1210
|
+
}
|
|
1211
|
+
return outcome;
|
|
1212
|
+
}
|
|
1213
|
+
|
|
1158
1214
|
async answerApproval(state, command) {
|
|
1159
|
-
const
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1215
|
+
const approvalId = String(command.dshApprovalId ?? "");
|
|
1216
|
+
// The normal path on DSH 0.2.x: the request is parked in the broker, waiting for
|
|
1217
|
+
// this decision to come back over the bridge.
|
|
1218
|
+
if (this.approvals.resolve(approvalId, command.outcome)) {
|
|
1219
|
+
await this.reportCommandResult(state, command.requestId, { ok: true });
|
|
1220
|
+
return;
|
|
1221
|
+
}
|
|
1222
|
+
// A card carried by the older rpc channel (a DSH build that still exposes the
|
|
1223
|
+
// client stream) is answered where it came from, so an upgraded phone still works
|
|
1224
|
+
// against an older desktop.
|
|
1225
|
+
if (command.rpcId) {
|
|
1226
|
+
const key = `${APPROVAL_PREFIX}${approvalId}`;
|
|
1227
|
+
state.answered.add(key);
|
|
1228
|
+
try {
|
|
1229
|
+
const receipt = await this.respond(command.rpcId, {
|
|
1230
|
+
ok: true,
|
|
1231
|
+
value: {
|
|
1232
|
+
sessionId: state.agent.session.id,
|
|
1233
|
+
approvalId,
|
|
1234
|
+
outcome: command.outcome === "allowed-once" ? "allowed-once" : "rejected",
|
|
1235
|
+
},
|
|
1175
1236
|
});
|
|
1176
|
-
|
|
1237
|
+
if (!receipt?.accepted) {
|
|
1238
|
+
state.answered.delete(key);
|
|
1239
|
+
await this.reportCommandResult(state, command.requestId, {
|
|
1240
|
+
ok: false,
|
|
1241
|
+
error: `DSH Desktop did not accept this decision (${receipt?.reason ?? "unknown"})`,
|
|
1242
|
+
});
|
|
1243
|
+
return;
|
|
1244
|
+
}
|
|
1245
|
+
await this.reportCommandResult(state, command.requestId, { ok: true });
|
|
1246
|
+
} catch (error) {
|
|
1247
|
+
state.answered.delete(key);
|
|
1248
|
+
await this.reportCommandResult(state, command.requestId, { ok: false, error: textFromError(error) });
|
|
1177
1249
|
}
|
|
1178
|
-
|
|
1179
|
-
} catch (error) {
|
|
1180
|
-
state.answered.delete(key);
|
|
1181
|
-
await this.reportCommandResult(state, command.requestId, { ok: false, error: textFromError(error) });
|
|
1250
|
+
return;
|
|
1182
1251
|
}
|
|
1252
|
+
await this.reportCommandResult(state, command.requestId, {
|
|
1253
|
+
ok: false,
|
|
1254
|
+
error: "这张批准卡已经失效:可能已在桌面端处理,或已经超时",
|
|
1255
|
+
});
|
|
1183
1256
|
}
|
|
1184
1257
|
|
|
1185
1258
|
/** Tear the channel down; the conversation itself lives on (a reconnect reuses it). */
|
|
@@ -1189,6 +1262,9 @@ export class PhoneConnections {
|
|
|
1189
1262
|
state.controller.abort();
|
|
1190
1263
|
if (state.eventTimer) clearTimeout(state.eventTimer);
|
|
1191
1264
|
this.bySession.delete(sessionId);
|
|
1265
|
+
// An approval card outlives the channel that carried it, but the question behind it
|
|
1266
|
+
// does not: leaving it pending would hold the tool call until the broker's timeout.
|
|
1267
|
+
this.#cancelApprovals(sessionId);
|
|
1192
1268
|
}
|
|
1193
1269
|
|
|
1194
1270
|
/** The desktop let go of the conversation: stop every loop, including reconnect. */
|
|
@@ -1201,6 +1277,14 @@ export class PhoneConnections {
|
|
|
1201
1277
|
this.close(sessionId);
|
|
1202
1278
|
}
|
|
1203
1279
|
|
|
1280
|
+
/** Settle every approval this conversation was waiting on as cancelled. */
|
|
1281
|
+
#cancelApprovals(sessionId) {
|
|
1282
|
+
const ids = this.approvalIds.get(sessionId);
|
|
1283
|
+
if (!ids?.size) return;
|
|
1284
|
+
this.approvalIds.delete(sessionId);
|
|
1285
|
+
for (const approvalId of ids) this.approvals.cancel(approvalId);
|
|
1286
|
+
}
|
|
1287
|
+
|
|
1204
1288
|
captureEvent(session, event) {
|
|
1205
1289
|
const state = this.bySession.get(session.id);
|
|
1206
1290
|
if (!state || state.controller.signal.aborted) return;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chenacc/dsh-agentbridge",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.8",
|
|
4
4
|
"description": "AgentBridge for DSH Desktop: run /phone in a conversation to pair a phone console with that exact live Agent.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
},
|
|
11
11
|
"files": [
|
|
12
12
|
"lib/index.js",
|
|
13
|
+
"lib/approval-broker.js",
|
|
13
14
|
"cordis.patch.yml",
|
|
14
15
|
"README.md",
|
|
15
16
|
"LICENSE",
|