dsh-browser-firefox 0.5.0 → 0.5.2
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/README.md +14 -4
- package/icon.svg +10 -0
- package/lib/index.js +621 -68
- package/lib/protocol.js +60 -4
- package/lib/types/approvals.d.ts +7 -0
- package/lib/types/discovery-beacon.d.ts +31 -0
- package/lib/types/extension-sessions.d.ts +88 -6
- package/lib/types/index.d.ts +6 -0
- package/lib/types/protocol.d.ts +53 -0
- package/lib/types/questions.d.ts +37 -2
- package/lib/types/server.d.ts +47 -9
- package/lib/types/untrusted.d.ts +31 -0
- package/lib/types/waterfall.d.ts +4 -0
- package/package.json +33 -30
- package/src/approvals.ts +65 -4
- package/src/discovery-beacon.ts +77 -0
- package/src/extension-sessions.ts +147 -7
- package/src/index.ts +158 -2
- package/src/protocol.ts +68 -1
- package/src/questions.ts +128 -8
- package/src/server.ts +123 -31
- package/src/session-workspace.ts +18 -4
- package/src/tools.ts +20 -4
- package/src/untrusted.ts +50 -0
- package/src/waterfall.ts +24 -1
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-browser-firefox",
|
|
3
3
|
"description": "Firefox browser control for DeepSeek Harness: token-authenticated WebSocket bridge + browser_* tools (native-image screenshots) driving the user's own Firefox via the companion AMO extension, with sidebar ask_user_question answering",
|
|
4
|
-
"version": "0.5.
|
|
4
|
+
"version": "0.5.2",
|
|
5
5
|
"author": "tuojc",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": {
|
|
@@ -37,30 +37,32 @@
|
|
|
37
37
|
},
|
|
38
38
|
"./cordis.patch.yml": "./cordis.patch.yml",
|
|
39
39
|
"./src/*": "./src/*",
|
|
40
|
-
"./package.json": "./package.json"
|
|
40
|
+
"./package.json": "./package.json",
|
|
41
|
+
"./icon": "./icon.svg"
|
|
41
42
|
},
|
|
42
43
|
"files": [
|
|
43
44
|
"lib/index.js",
|
|
44
45
|
"lib/protocol.js",
|
|
45
46
|
"lib/types/**/*.d.ts",
|
|
46
47
|
"cordis.patch.yml",
|
|
47
|
-
"src"
|
|
48
|
+
"src",
|
|
49
|
+
"icon.svg"
|
|
48
50
|
],
|
|
49
51
|
"peerDependencies": {
|
|
50
|
-
"@deepseek-ai/cordis": "^4.0.
|
|
51
|
-
"@deepseek-ai/dsh-api-gateway": "^0.2.
|
|
52
|
-
"@deepseek-ai/dsh-api-session-controller": "^0.2.
|
|
53
|
-
"@deepseek-ai/dsh-api-workspace-controller": "^0.2.
|
|
54
|
-
"@deepseek-ai/dsh-attachment": "^0.2.
|
|
55
|
-
"@deepseek-ai/dsh-home-paths": "^0.2.
|
|
56
|
-
"@deepseek-ai/dsh-host-webserver": "^0.2.
|
|
57
|
-
"@deepseek-ai/dsh-session": "^0.2.
|
|
58
|
-
"@deepseek-ai/dsh-tools": "^0.2.
|
|
59
|
-
"@deepseek-ai/dsh-user-approval": "^0.2.
|
|
60
|
-
"@deepseek-ai/dsh-user-questions": "^0.2.
|
|
52
|
+
"@deepseek-ai/cordis": "^4.2.0-alpha.1",
|
|
53
|
+
"@deepseek-ai/dsh-api-gateway": "^0.2.3-alpha.2",
|
|
54
|
+
"@deepseek-ai/dsh-api-session-controller": "^0.2.3-alpha.2",
|
|
55
|
+
"@deepseek-ai/dsh-api-workspace-controller": "^0.2.3-alpha.2",
|
|
56
|
+
"@deepseek-ai/dsh-attachment": "^0.2.3-alpha.2",
|
|
57
|
+
"@deepseek-ai/dsh-home-paths": "^0.2.3-alpha.2",
|
|
58
|
+
"@deepseek-ai/dsh-host-webserver": "^0.2.3-alpha.2",
|
|
59
|
+
"@deepseek-ai/dsh-session": "^0.2.3-alpha.2",
|
|
60
|
+
"@deepseek-ai/dsh-tools": "^0.2.3-alpha.2",
|
|
61
|
+
"@deepseek-ai/dsh-user-approval": "^0.2.3-alpha.2",
|
|
62
|
+
"@deepseek-ai/dsh-user-questions": "^0.2.3-alpha.2"
|
|
61
63
|
},
|
|
62
64
|
"dependencies": {
|
|
63
|
-
"@deepseek-ai/schemastery": "^3.
|
|
65
|
+
"@deepseek-ai/schemastery": "^3.20.0-alpha.1",
|
|
64
66
|
"ws": "^8.21.0"
|
|
65
67
|
},
|
|
66
68
|
"scripts": {
|
|
@@ -70,25 +72,26 @@
|
|
|
70
72
|
"prepublishOnly": "pnpm run build"
|
|
71
73
|
},
|
|
72
74
|
"devDependencies": {
|
|
73
|
-
"@deepseek-ai/cordis": "4.0.
|
|
74
|
-
"@deepseek-ai/cordis-plugin-include": "1.0.
|
|
75
|
-
"@deepseek-ai/cordis-plugin-loader": "1.0.
|
|
76
|
-
"@deepseek-ai/dsh-api-gateway": "0.2.
|
|
77
|
-
"@deepseek-ai/dsh-api-session-controller": "0.2.
|
|
78
|
-
"@deepseek-ai/dsh-api-workspace-controller": "0.2.
|
|
79
|
-
"@deepseek-ai/dsh-attachment": "0.2.
|
|
80
|
-
"@deepseek-ai/dsh-home-paths": "0.2.
|
|
81
|
-
"@deepseek-ai/dsh-host-webserver": "0.2.
|
|
82
|
-
"@deepseek-ai/dsh-session": "0.2.
|
|
83
|
-
"@deepseek-ai/dsh-system-prompt": "0.2.
|
|
84
|
-
"@deepseek-ai/dsh-tools": "0.2.
|
|
85
|
-
"@deepseek-ai/dsh-user-approval": "0.2.
|
|
86
|
-
"@deepseek-ai/dsh-user-questions": "0.2.
|
|
75
|
+
"@deepseek-ai/cordis": "4.2.0-alpha.1",
|
|
76
|
+
"@deepseek-ai/cordis-plugin-include": "1.2.0-alpha.1",
|
|
77
|
+
"@deepseek-ai/cordis-plugin-loader": "1.2.0-alpha.1",
|
|
78
|
+
"@deepseek-ai/dsh-api-gateway": "0.2.3-alpha.2",
|
|
79
|
+
"@deepseek-ai/dsh-api-session-controller": "0.2.3-alpha.2",
|
|
80
|
+
"@deepseek-ai/dsh-api-workspace-controller": "0.2.3-alpha.2",
|
|
81
|
+
"@deepseek-ai/dsh-attachment": "0.2.3-alpha.2",
|
|
82
|
+
"@deepseek-ai/dsh-home-paths": "0.2.3-alpha.2",
|
|
83
|
+
"@deepseek-ai/dsh-host-webserver": "0.2.3-alpha.2",
|
|
84
|
+
"@deepseek-ai/dsh-session": "0.2.3-alpha.2",
|
|
85
|
+
"@deepseek-ai/dsh-system-prompt": "0.2.3-alpha.2",
|
|
86
|
+
"@deepseek-ai/dsh-tools": "0.2.3-alpha.2",
|
|
87
|
+
"@deepseek-ai/dsh-user-approval": "0.2.3-alpha.2",
|
|
88
|
+
"@deepseek-ai/dsh-user-questions": "0.2.3-alpha.2",
|
|
87
89
|
"@types/node": "^22.20.0",
|
|
88
90
|
"@types/ws": "^8.18.1",
|
|
89
91
|
"playwright-core": "^1.61.1",
|
|
90
92
|
"tsdown": "0.22.2",
|
|
91
93
|
"typescript": "^6.0.3",
|
|
92
94
|
"vitest": "^4.1.8"
|
|
93
|
-
}
|
|
95
|
+
},
|
|
96
|
+
"icon": "./icon.svg"
|
|
94
97
|
}
|
package/src/approvals.ts
CHANGED
|
@@ -8,6 +8,9 @@
|
|
|
8
8
|
* - the host aborts `request.signal` (turn cancelled, session disposed) →
|
|
9
9
|
* the extension is sent `approval.resolved` and the waterfall resolves
|
|
10
10
|
* with `'cancelled'` (the host vocabulary has no rejection channel here);
|
|
11
|
+
* - nobody decides inside {@link answerTimeoutMs} → the extension is sent
|
|
12
|
+
* `approval.resolved` and the waterfall resolves `'cancelled'`, the
|
|
13
|
+
* fail-closed outcome for an abandoned card;
|
|
11
14
|
* - the bridge connection drops ({@link delegateAll}) → the waterfall is
|
|
12
15
|
* delegated to `next()` so another answerer (dsh web) can still decide;
|
|
13
16
|
* - plugin unload ({@link dispose}) → `'unavailable'`, the fail-closed
|
|
@@ -49,6 +52,12 @@ type PendingOutcome =
|
|
|
49
52
|
| { kind: 'decide'; outcome: ApprovalOutcome }
|
|
50
53
|
| { kind: 'delegate' }
|
|
51
54
|
|
|
55
|
+
/** Domain state kept for one pending card so a replacement socket can re-push it. */
|
|
56
|
+
interface ApprovalPayload {
|
|
57
|
+
sessionId: string
|
|
58
|
+
prompt: ApprovalPrompt
|
|
59
|
+
}
|
|
60
|
+
|
|
52
61
|
/** Parse the extension's `bridge/approvalDecision` rpc args (ids + closed decision). */
|
|
53
62
|
export function asApprovalDecisionArgs(value: unknown): ApprovalDecisionArgs | undefined {
|
|
54
63
|
if (typeof value !== 'object' || value === null || Array.isArray(value)) return undefined
|
|
@@ -61,11 +70,13 @@ export function asApprovalDecisionArgs(value: unknown): ApprovalDecisionArgs | u
|
|
|
61
70
|
|
|
62
71
|
/** Owns the host approval waterfall for extension-driven sessions. */
|
|
63
72
|
export class ApprovalBridge {
|
|
64
|
-
private readonly waterfalls = new PendingWaterfalls<PendingOutcome>()
|
|
73
|
+
private readonly waterfalls = new PendingWaterfalls<PendingOutcome, ApprovalPayload>()
|
|
65
74
|
|
|
66
75
|
constructor(private readonly deps: {
|
|
67
76
|
/** Push one server frame to the connected extension (must tolerate a dead socket). */
|
|
68
77
|
push: (frame: ServerFrame) => void
|
|
78
|
+
/** How long a card may sit undecided before it settles as cancelled; 0/undefined disables the budget. */
|
|
79
|
+
answerTimeoutMs?: number
|
|
69
80
|
}) {}
|
|
70
81
|
|
|
71
82
|
/**
|
|
@@ -87,14 +98,28 @@ export class ApprovalBridge {
|
|
|
87
98
|
if (signal?.aborted) return Promise.resolve<ApprovalOutcome>('cancelled')
|
|
88
99
|
const approvalId = randomUUID()
|
|
89
100
|
return new Promise<ApprovalOutcome>((resolve, reject) => {
|
|
101
|
+
let timer: NodeJS.Timeout | undefined
|
|
90
102
|
this.waterfalls.register({
|
|
91
103
|
id: approvalId,
|
|
92
104
|
sessionId: routingSessionId,
|
|
93
105
|
signal,
|
|
94
|
-
payload:
|
|
106
|
+
payload: { sessionId: routingSessionId, prompt },
|
|
95
107
|
complete: (outcome) => {
|
|
108
|
+
if (timer !== undefined) {
|
|
109
|
+
clearTimeout(timer)
|
|
110
|
+
timer = undefined
|
|
111
|
+
}
|
|
96
112
|
if (outcome.kind === 'decide') resolve(outcome.outcome)
|
|
97
|
-
|
|
113
|
+
// `next()` may throw synchronously; keep that inside the rejection
|
|
114
|
+
// instead of letting it escape a socket close listener.
|
|
115
|
+
else Promise.resolve().then(next).then(resolve, reject)
|
|
116
|
+
},
|
|
117
|
+
fail: (error) => {
|
|
118
|
+
if (timer !== undefined) {
|
|
119
|
+
clearTimeout(timer)
|
|
120
|
+
timer = undefined
|
|
121
|
+
}
|
|
122
|
+
reject(error instanceof Error ? error : new Error(String(error)))
|
|
98
123
|
},
|
|
99
124
|
onAbort: () => {
|
|
100
125
|
// Withdraw the card before settling so a late click can never be
|
|
@@ -103,6 +128,17 @@ export class ApprovalBridge {
|
|
|
103
128
|
this.waterfalls.settle(approvalId, { kind: 'decide', outcome: 'cancelled' })
|
|
104
129
|
},
|
|
105
130
|
})
|
|
131
|
+
const budget = this.deps.answerTimeoutMs
|
|
132
|
+
if (budget !== undefined && budget > 0) {
|
|
133
|
+
timer = setTimeout(() => {
|
|
134
|
+
// An abandoned approval fails closed: 'cancelled' is the host's own
|
|
135
|
+
// vocabulary for "the user never decided", so the tool call reports a
|
|
136
|
+
// refusal rather than hanging the turn.
|
|
137
|
+
this.deps.push({ t: 'approval.resolved', id: approvalId, sessionId: routingSessionId })
|
|
138
|
+
this.waterfalls.settle(approvalId, { kind: 'decide', outcome: 'cancelled' })
|
|
139
|
+
}, budget)
|
|
140
|
+
timer.unref?.()
|
|
141
|
+
}
|
|
106
142
|
this.deps.push({
|
|
107
143
|
t: 'approval.requested',
|
|
108
144
|
id: approvalId,
|
|
@@ -122,6 +158,9 @@ export class ApprovalBridge {
|
|
|
122
158
|
if (!this.waterfalls.matches(args.approvalId, args.sessionId)) {
|
|
123
159
|
return { accepted: false, reason: 'not-pending' }
|
|
124
160
|
}
|
|
161
|
+
// Keep the closed-set check at this boundary too: a direct caller (or a
|
|
162
|
+
// future parser change) must never settle an approval with an unknown
|
|
163
|
+
// outcome, since `outcome` is typed as a closed union.
|
|
125
164
|
if (args.decision !== 'allowed-once' && args.decision !== 'rejected') {
|
|
126
165
|
return { accepted: false, reason: 'bad-decision' }
|
|
127
166
|
}
|
|
@@ -131,7 +170,29 @@ export class ApprovalBridge {
|
|
|
131
170
|
|
|
132
171
|
/** Bridge connection replaced/closed: delegate every unsettled approval. */
|
|
133
172
|
delegateAll(): void {
|
|
134
|
-
for (const id of this.waterfalls.ids())
|
|
173
|
+
for (const id of this.waterfalls.ids()) {
|
|
174
|
+
// Withdraw the card first so the extension's replay cache cannot keep a
|
|
175
|
+
// card that can only answer `not-pending`.
|
|
176
|
+
const sessionId = this.waterfalls.sessionOf(id)
|
|
177
|
+
if (sessionId !== undefined) this.deps.push({ t: 'approval.resolved', id, sessionId })
|
|
178
|
+
this.waterfalls.settle(id, { kind: 'delegate' })
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** A new connection took over: re-push pending approvals to the fresh panel. */
|
|
183
|
+
replay(): void {
|
|
184
|
+
for (const id of this.waterfalls.ids()) {
|
|
185
|
+
const entry = this.waterfalls.payloadOf(id)
|
|
186
|
+
if (entry === undefined) continue
|
|
187
|
+
this.deps.push({
|
|
188
|
+
t: 'approval.requested',
|
|
189
|
+
id,
|
|
190
|
+
sessionId: entry.sessionId,
|
|
191
|
+
toolName: entry.prompt.toolName,
|
|
192
|
+
...(entry.prompt.callId === undefined ? {} : { callId: entry.prompt.callId }),
|
|
193
|
+
...(entry.prompt.reason === undefined ? {} : { reason: entry.prompt.reason }),
|
|
194
|
+
})
|
|
195
|
+
}
|
|
135
196
|
}
|
|
136
197
|
|
|
137
198
|
/** Plugin unload: answer everything fail-closed so no tool call outlives the plugin. */
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fixed-port discovery beacon for dsh Desktop hosts that bind an ephemeral
|
|
3
|
+
* webserver port (`--port 0`). The beacon binds the first free port of
|
|
4
|
+
* {@link BEACON_PORTS}, answers `/ext/bridge-config` with the real bridge URL,
|
|
5
|
+
* and lets the extension find this instance with a bounded probe list.
|
|
6
|
+
*
|
|
7
|
+
* Hardening (mirrors `natgurlain/dsh-browser-agent`, MIT):
|
|
8
|
+
* - binds loopback only;
|
|
9
|
+
* - sends no CORS headers, so a web page cannot read the answer;
|
|
10
|
+
* - rejects a foreign `Host` header (DNS rebinding);
|
|
11
|
+
* - carries no secret — the WebSocket itself authenticates every client.
|
|
12
|
+
*
|
|
13
|
+
* @module
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { createServer, type IncomingMessage, type Server, type ServerResponse } from 'node:http'
|
|
17
|
+
import { BEACON_PORTS, BRIDGE_CONFIG_PATH, type BridgeConfigPayload } from './protocol.ts'
|
|
18
|
+
|
|
19
|
+
/** Host authorities a browser on this machine may send. */
|
|
20
|
+
const LOOPBACK_HOSTS = new Set(['127.0.0.1', 'localhost'])
|
|
21
|
+
|
|
22
|
+
/** One bound discovery beacon. */
|
|
23
|
+
export interface DiscoveryBeacon {
|
|
24
|
+
/** The port actually bound. */
|
|
25
|
+
port: number
|
|
26
|
+
/** Stop listening and release the port. */
|
|
27
|
+
close(): Promise<void>
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Bind the first free beacon port.
|
|
32
|
+
*
|
|
33
|
+
* @param hostPort - the real webserver port (or a getter, for late binding).
|
|
34
|
+
* @param ports - candidate ports, tried in order.
|
|
35
|
+
* @param warn - diagnostics sink, silent by default.
|
|
36
|
+
* @returns the bound beacon, or undefined when every candidate is busy.
|
|
37
|
+
*/
|
|
38
|
+
export async function startDiscoveryBeacon(
|
|
39
|
+
hostPort: number | (() => number | undefined),
|
|
40
|
+
ports: readonly number[] = BEACON_PORTS,
|
|
41
|
+
warn: (message: string) => void = () => {},
|
|
42
|
+
): Promise<DiscoveryBeacon | undefined> {
|
|
43
|
+
for (const port of ports) {
|
|
44
|
+
const server: Server = createServer((req: IncomingMessage, res: ServerResponse) => {
|
|
45
|
+
const host = (req.headers.host ?? '').split(':')[0] ?? ''
|
|
46
|
+
if (!LOOPBACK_HOSTS.has(host.toLowerCase())) {
|
|
47
|
+
res.writeHead(403).end()
|
|
48
|
+
return
|
|
49
|
+
}
|
|
50
|
+
const path = new URL(req.url ?? '/', 'http://localhost').pathname
|
|
51
|
+
if (path !== BRIDGE_CONFIG_PATH) {
|
|
52
|
+
res.writeHead(404).end()
|
|
53
|
+
return
|
|
54
|
+
}
|
|
55
|
+
const realPort = typeof hostPort === 'function' ? hostPort() : hostPort
|
|
56
|
+
if (realPort === undefined) {
|
|
57
|
+
res.writeHead(503).end()
|
|
58
|
+
return
|
|
59
|
+
}
|
|
60
|
+
const payload: BridgeConfigPayload = { wsUrl: `ws://127.0.0.1:${realPort}/ext/bridge` }
|
|
61
|
+
res.writeHead(200, { 'content-type': 'application/json' }).end(JSON.stringify(payload))
|
|
62
|
+
})
|
|
63
|
+
const bound = await new Promise<boolean>((resolve) => {
|
|
64
|
+
server.once('error', () => resolve(false))
|
|
65
|
+
server.listen(port, '127.0.0.1', () => resolve(true))
|
|
66
|
+
})
|
|
67
|
+
if (bound) {
|
|
68
|
+
const actualPort = (server.address() as { port: number }).port
|
|
69
|
+
return {
|
|
70
|
+
port: actualPort,
|
|
71
|
+
close: () => new Promise<void>((resolve) => { server.close(() => resolve()) }),
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
warn('browser bridge: no free discovery beacon port')
|
|
76
|
+
return undefined
|
|
77
|
+
}
|
|
@@ -6,13 +6,17 @@
|
|
|
6
6
|
*
|
|
7
7
|
* Ownership has three sources, all revocable:
|
|
8
8
|
* - **driven**: the extension created or prompted the session (kept until the
|
|
9
|
-
* plugin unloads);
|
|
9
|
+
* session disappears or the plugin unloads);
|
|
10
10
|
* - **followed**: an open `session/follow` stream means the sidebar is
|
|
11
11
|
* displaying that session right now (released when the stream closes);
|
|
12
12
|
* - **inherited**: an ask from a subagent child session belongs to the
|
|
13
13
|
* nearest extension-owned ancestor, so child questions/approvals surface in
|
|
14
14
|
* the parent conversation the user is actually watching.
|
|
15
15
|
*
|
|
16
|
+
* Inheritance only ever matters for **approvals**: the host's `ask()` rejects a
|
|
17
|
+
* delegated caller outright (`DELEGATED_CALLER`) before the questions waterfall
|
|
18
|
+
* runs, so a subagent never reaches the user-questions event.
|
|
19
|
+
*
|
|
16
20
|
* @module
|
|
17
21
|
*/
|
|
18
22
|
|
|
@@ -20,6 +24,12 @@
|
|
|
20
24
|
export class ExtensionSessionRegistry {
|
|
21
25
|
private readonly ids = new Set<string>()
|
|
22
26
|
|
|
27
|
+
/** Sessions that disappeared from the store are dropped on the next read. */
|
|
28
|
+
private readonly missing = new Set<string>()
|
|
29
|
+
|
|
30
|
+
/** Whether a session the registry tracks has since been disposed. */
|
|
31
|
+
private exists: (sessionId: string) => boolean = () => true
|
|
32
|
+
|
|
23
33
|
/** Remember a session the extension successfully created or prompted. */
|
|
24
34
|
note(sessionId: string | undefined): void {
|
|
25
35
|
if (typeof sessionId === 'string' && sessionId.length > 0) this.ids.add(sessionId)
|
|
@@ -30,14 +40,40 @@ export class ExtensionSessionRegistry {
|
|
|
30
40
|
if (typeof sessionId === 'string') this.ids.delete(sessionId)
|
|
31
41
|
}
|
|
32
42
|
|
|
43
|
+
/**
|
|
44
|
+
* Install the store lookup that lets a disposed session release its
|
|
45
|
+
* ownership. Called lazily by the plugin so a Session store registered after
|
|
46
|
+
* this plugin still takes effect.
|
|
47
|
+
*
|
|
48
|
+
* @param exists - session-id → whether the store still holds it.
|
|
49
|
+
*/
|
|
50
|
+
watchStore(exists: (sessionId: string) => boolean): void {
|
|
51
|
+
this.exists = exists
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Record that a session was disposed; the next {@link has} drops it. */
|
|
55
|
+
dispose(sessionId: string | undefined): void {
|
|
56
|
+
if (typeof sessionId === 'string' && sessionId.length > 0) this.missing.add(sessionId)
|
|
57
|
+
}
|
|
58
|
+
|
|
33
59
|
/** Whether the extension has touched this session over the bridge. */
|
|
34
60
|
has(sessionId: string | undefined): boolean {
|
|
35
|
-
|
|
61
|
+
if (typeof sessionId !== 'string') return false
|
|
62
|
+
if (this.missing.delete(sessionId)) this.ids.delete(sessionId)
|
|
63
|
+
if (!this.ids.has(sessionId)) return false
|
|
64
|
+
// A session the store no longer holds cannot ask again; releasing it keeps
|
|
65
|
+
// the id from shadowing a future session that reuses the identity.
|
|
66
|
+
if (!this.exists(sessionId)) {
|
|
67
|
+
this.ids.delete(sessionId)
|
|
68
|
+
return false
|
|
69
|
+
}
|
|
70
|
+
return true
|
|
36
71
|
}
|
|
37
72
|
|
|
38
73
|
/** Test helper: drop all tracked ids. */
|
|
39
74
|
clear(): void {
|
|
40
75
|
this.ids.clear()
|
|
76
|
+
this.missing.clear()
|
|
41
77
|
}
|
|
42
78
|
}
|
|
43
79
|
|
|
@@ -76,6 +112,7 @@ export class FollowedSessions {
|
|
|
76
112
|
}
|
|
77
113
|
}
|
|
78
114
|
|
|
115
|
+
|
|
79
116
|
/** Ownership sources consulted by {@link owningSessionId}. */
|
|
80
117
|
export interface OwnershipSources {
|
|
81
118
|
/** Sessions the extension created or prompted. */
|
|
@@ -123,8 +160,86 @@ export function owningSessionId(
|
|
|
123
160
|
return undefined
|
|
124
161
|
}
|
|
125
162
|
|
|
163
|
+
/** Which UI most recently spoke in a session. */
|
|
164
|
+
export type InteractionSource = 'sidebar' | 'web' | 'unknown'
|
|
165
|
+
|
|
166
|
+
/** Most `rpcId`s retained for attribution; older ones fall out (FIFO). */
|
|
167
|
+
const MAX_TRACKED_REQUESTS = 200
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Last-active-UI tracking, per session.
|
|
171
|
+
*
|
|
172
|
+
* The host exposes no focus or visibility signal (its `TypertGateway` surface
|
|
173
|
+
* has none), so "which window is the user looking at" cannot be asked
|
|
174
|
+
* directly. What CAN be observed exactly is **which path the session's last
|
|
175
|
+
* prompt came through**:
|
|
176
|
+
*
|
|
177
|
+
* - a prompt the extension sends crosses the bridge, so the plugin sees it in
|
|
178
|
+
* its RPC dispatch and knows the sidebar spoke;
|
|
179
|
+
* - a prompt typed in dsh web never crosses the bridge, so it reaches this
|
|
180
|
+
* plugin only as a durable `user/message` whose source carries the prompt's
|
|
181
|
+
* `rpcId`. Anything not minted by the bridge came from another UI.
|
|
182
|
+
*
|
|
183
|
+
* That makes "who spoke last" the routing rule: the card follows the UI the
|
|
184
|
+
* user is actually driving, which is the closest reliable proxy for attention.
|
|
185
|
+
*/
|
|
186
|
+
export class SessionActivity {
|
|
187
|
+
private readonly last = new Map<string, InteractionSource>()
|
|
188
|
+
/** Prompt identities this bridge minted, in arrival order (bounded). */
|
|
189
|
+
private readonly sidebarRequests = new Set<string>()
|
|
190
|
+
|
|
191
|
+
/** Record that the sidebar sent a prompt (called from the bridge's RPC dispatch). */
|
|
192
|
+
noteSidebarPrompt(sessionId: string | undefined, requestId: string | undefined): void {
|
|
193
|
+
if (typeof requestId === 'string' && requestId.length > 0) {
|
|
194
|
+
this.sidebarRequests.add(requestId)
|
|
195
|
+
// Bound the set: the oldest entries are unreachable once their prompt has
|
|
196
|
+
// long been admitted, and an unbounded Set would grow for the process life.
|
|
197
|
+
while (this.sidebarRequests.size > MAX_TRACKED_REQUESTS) {
|
|
198
|
+
const oldest = this.sidebarRequests.values().next().value
|
|
199
|
+
if (oldest === undefined) break
|
|
200
|
+
this.sidebarRequests.delete(oldest)
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
if (typeof sessionId === 'string' && sessionId.length > 0) this.last.set(sessionId, 'sidebar')
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** Record that another UI (dsh web) sent a prompt. */
|
|
207
|
+
noteWebPrompt(sessionId: string | undefined): void {
|
|
208
|
+
if (typeof sessionId === 'string' && sessionId.length > 0) this.last.set(sessionId, 'web')
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** Whether this prompt identity was minted by the bridge (i.e. the sidebar). */
|
|
212
|
+
isSidebarRequest(requestId: string | undefined): boolean {
|
|
213
|
+
return typeof requestId === 'string' && this.sidebarRequests.has(requestId)
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** Which UI last spoke in this session; `unknown` before any prompt. */
|
|
217
|
+
sourceOf(sessionId: string | undefined): InteractionSource {
|
|
218
|
+
if (typeof sessionId !== 'string') return 'unknown'
|
|
219
|
+
return this.last.get(sessionId) ?? 'unknown'
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** Test helper / bridge generation reset. */
|
|
223
|
+
clear(): void {
|
|
224
|
+
this.last.clear()
|
|
225
|
+
this.sidebarRequests.clear()
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
126
229
|
/** Why a host interaction was delegated to the next answerer. */
|
|
127
|
-
export type DelegateReason =
|
|
230
|
+
export type DelegateReason =
|
|
231
|
+
/** No extension is connected, so only the native answerer remains. */
|
|
232
|
+
| 'no-connection'
|
|
233
|
+
/** The connected extension predates this card kind. */
|
|
234
|
+
| 'unsupported-client'
|
|
235
|
+
/** Connected, capable, but no side panel is open: a card would be invisible. */
|
|
236
|
+
| 'no-panel'
|
|
237
|
+
/**
|
|
238
|
+
* A dsh web client is live and the session's most recent prompt did NOT come
|
|
239
|
+
* from the sidebar — so the user is driving that UI and the card belongs there.
|
|
240
|
+
*/
|
|
241
|
+
| 'recently-web'
|
|
242
|
+
| 'not-owned'
|
|
128
243
|
|
|
129
244
|
/** One interaction-ownership decision, kept inspectable for logging and tests. */
|
|
130
245
|
export type InteractionOwnership =
|
|
@@ -133,20 +248,45 @@ export type InteractionOwnership =
|
|
|
133
248
|
|
|
134
249
|
/**
|
|
135
250
|
* Decide whether the bridge should own one host interaction (question or
|
|
136
|
-
* approval). All gates must pass: an extension is connected, it advertised
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
*
|
|
251
|
+
* approval). All gates must pass: an extension is connected, it advertised the
|
|
252
|
+
* matching card capability in its hello caps, a panel is attached to show the
|
|
253
|
+
* card, the sidebar (not dsh web) drove the session most recently, and the
|
|
254
|
+
* asking session is extension-owned (directly or through an owned ancestor).
|
|
255
|
+
* Everything else falls through to `next()` so the native answerer waterfall
|
|
256
|
+
* keeps working.
|
|
257
|
+
*
|
|
258
|
+
* Gate order matters for diagnostics: the connection/capability/presence gates
|
|
259
|
+
* are about the extension alone and are answered first, so a log line names the
|
|
260
|
+
* real obstacle instead of blaming the web client for a missing socket.
|
|
140
261
|
*/
|
|
141
262
|
export function resolveInteractionOwnership(input: {
|
|
142
263
|
hasExtensionConnection: boolean
|
|
143
264
|
clientSupports: boolean
|
|
265
|
+
/**
|
|
266
|
+
* Whether a side panel is attached. A socket without a panel cannot show a
|
|
267
|
+
* card, and claiming the waterfall anyway would strand the turn.
|
|
268
|
+
*/
|
|
269
|
+
uiAttached: boolean
|
|
270
|
+
/**
|
|
271
|
+
* Whether another answerer (the dsh web client) is live right now. On its own
|
|
272
|
+
* this says nothing about whether the user is looking at that UI, so it only
|
|
273
|
+
* decides the card when the sidebar did not speak last.
|
|
274
|
+
*/
|
|
275
|
+
webAnswersInteractions: boolean
|
|
276
|
+
/** Which UI most recently sent a prompt for this session. */
|
|
277
|
+
activeSource: InteractionSource
|
|
144
278
|
sessionId: string | undefined
|
|
145
279
|
sources: OwnershipSources
|
|
146
280
|
parentOf?: (sessionId: string) => string | undefined
|
|
147
281
|
}): InteractionOwnership {
|
|
148
282
|
if (!input.hasExtensionConnection) return { owned: false, reason: 'no-connection' }
|
|
149
283
|
if (!input.clientSupports) return { owned: false, reason: 'unsupported-client' }
|
|
284
|
+
if (!input.uiAttached) return { owned: false, reason: 'no-panel' }
|
|
285
|
+
// A live dsh web client wins ONLY when the sidebar did not drive this session
|
|
286
|
+
// last: a card belongs to the UI the user is actually typing in.
|
|
287
|
+
if (input.webAnswersInteractions && input.activeSource !== 'sidebar') {
|
|
288
|
+
return { owned: false, reason: 'recently-web' }
|
|
289
|
+
}
|
|
150
290
|
const sessionId = owningSessionId(input.sessionId, input.sources, input.parentOf)
|
|
151
291
|
if (sessionId === undefined) return { owned: false, reason: 'not-owned' }
|
|
152
292
|
return { owned: true, sessionId }
|