Skip to content

A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.

Add per-Bot browser views

Fei24118613d81587b6
diff --git a/app/package-lock.json b/app/package-lock.json
index a1b72415..339f846c 100644
--- a/app/package-lock.json
+++ b/app/package-lock.json
@@ -44,6 +44,7 @@
"ds-bot": "file:../ds-bot",
"electron-updater": "6.8.10",
"pnpm": "11.7.0",
+ "ws": "8.18.3",
"yaml": "2.9.1"
},
"devDependencies": {
@@ -885,6 +886,27 @@
"@deepseek-ai/cordis": "~4.0.5"
}
},
+ "node_modules/@deepseek-ai/dsh-api-gateway/node_modules/ws": {
+ "version": "8.22.0",
+ "resolved": "https://registry.npmjs.org/ws/-/ws-8.22.0.tgz",
+ "integrity": "sha512-Ydggc987+RO0AnWtZ/7Wq9FtNvcrL1b/RO0ud9mWjUPgDrsAAwQSF51sm2hm1XofbU/4jkpGEsLFsZZxU+1DOg==",
+ "license": "MIT",
+ "engines": {
+ "node": ">=10.0.0"
+ },
+ "peerDependencies": {
+ "bufferutil": "^4.0.1",
+ "utf-8-validate": ">=5.0.2"
+ },
+ "peerDependenciesMeta": {
+ "bufferutil": {
+ "optional": true
+ },
+ "utf-8-validate": {
+ "optional": true
+ }
+ }
+ },
"node_modules/@deepseek-ai/dsh-api-job-controller": {
"version": "0.2.1-rc.2",
"resolved": "https://registry.npmjs.org/@deepseek-ai/dsh-api-job-controller/-/dsh-api-job-controller-0.2.1-rc.2.tgz",
@@ -2523,6 +2545,27 @@
"@deepseek-ai/cordis": "~4.0.5"
}
},
+ "node_modules/@deepseek-ai/dsh-experimental-inspector/node_modules/ws": {
+ "version": "8.22.0",
+ "resolved": "https://registry.npmjs.org/ws/-/ws-8.22.0.tgz",
+ "integrity": "sha512-Ydggc987+RO0AnWtZ/7Wq9FtNvcrL1b/RO0ud9mWjUPgDrsAAwQSF51sm2hm1XofbU/4jkpGEsLFsZZxU+1DOg==",
+ "license": "MIT",
+ "engines": {
+ "node": ">=10.0.0"
+ },
+ "peerDependencies": {
+ "bufferutil": "^4.0.1",
+ "utf-8-validate": ">=5.0.2"
+ },
+ "peerDependenciesMeta": {
+ "bufferutil": {
+ "optional": true
+ },
+ "utf-8-validate": {
+ "optional": true
+ }
+ }
+ },
"node_modules/@deepseek-ai/dsh-experimental-plugin-market-bundle": {
"version": "0.2.1-rc.2",
"resolved": "https://registry.npmjs.org/@deepseek-ai/dsh-experimental-plugin-market-bundle/-/dsh-experimental-plugin-market-bundle-0.2.1-rc.2.tgz",
@@ -3327,6 +3370,43 @@
"node": ">=22.19.0"
}
},
+ "node_modules/@deepseek-ai/dsh-llm-pi-ai/node_modules/openai": {
+ "version": "7.19.0",
+ "resolved": "https://registry.npmjs.org/openai/-/openai-7.19.0.tgz",
+ "integrity": "sha512-MX2s3u2L5racTO0CC/SWpCOasJQBCJrqLKXK+l82cAhdeF8mPMBEe/gxMm0ZFa2xpKpOFLRjxv5afYEZbBXmbQ==",
+ "license": "Apache-2.0",
+ "engines": {
+ "node": ">=22.0.0"
+ },
+ "peerDependencies": {
+ "@aws-sdk/credential-provider-node": ">=3.972.0 <4",
+ "@smithy/hash-node": ">=4.3.0 <5",
+ "@smithy/signature-v4": ">=5.4.0 <6",
+ "undici": ">=5 <9",
+ "ws": "^8.21.0",
+ "zod": "^3.25 || ^4.0"
+ },
+ "peerDependenciesMeta": {
+ "@aws-sdk/credential-provider-node": {
+ "optional": true
+ },
+ "@smithy/hash-node": {
+ "optional": true
+ },
+ "@smithy/signature-v4": {
+ "optional": true
+ },
+ "undici": {
+ "optional": true
+ },
+ "ws": {
+ "optional": true
+ },
+ "zod": {
+ "optional": true
+ }
+ }
+ },
"node_modules/@deepseek-ai/dsh-llm-retry": {
"version": "0.2.1-rc.2",
"resolved": "https://registry.npmjs.org/@deepseek-ai/dsh-llm-retry/-/dsh-llm-retry-0.2.1-rc.2.tgz",
@@ -13862,43 +13942,6 @@
"url": "https://github.com/sponsors/sindresorhus"
}
},
- "node_modules/openai": {
- "version": "7.19.0",
- "resolved": "https://registry.npmjs.org/openai/-/openai-7.19.0.tgz",
- "integrity": "sha512-MX2s3u2L5racTO0CC/SWpCOasJQBCJrqLKXK+l82cAhdeF8mPMBEe/gxMm0ZFa2xpKpOFLRjxv5afYEZbBXmbQ==",
- "license": "Apache-2.0",
- "engines": {
- "node": ">=22.0.0"
- },
- "peerDependencies": {
- "@aws-sdk/credential-provider-node": ">=3.972.0 <4",
- "@smithy/hash-node": ">=4.3.0 <5",
- "@smithy/signature-v4": ">=5.4.0 <6",
- "undici": ">=5 <9",
- "ws": "^8.21.0",
- "zod": "^3.25 || ^4.0"
- },
- "peerDependenciesMeta": {
- "@aws-sdk/credential-provider-node": {
- "optional": true
- },
- "@smithy/hash-node": {
- "optional": true
- },
- "@smithy/signature-v4": {
- "optional": true
- },
- "undici": {
- "optional": true
- },
- "ws": {
- "optional": true
- },
- "zod": {
- "optional": true
- }
- }
- },
"node_modules/p-cancelable": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/p-cancelable/-/p-cancelable-4.0.1.tgz",
@@ -15919,9 +15962,9 @@
"license": "ISC"
},
"node_modules/ws": {
- "version": "8.22.0",
- "resolved": "https://registry.npmjs.org/ws/-/ws-8.22.0.tgz",
- "integrity": "sha512-Ydggc987+RO0AnWtZ/7Wq9FtNvcrL1b/RO0ud9mWjUPgDrsAAwQSF51sm2hm1XofbU/4jkpGEsLFsZZxU+1DOg==",
+ "version": "8.18.3",
+ "resolved": "https://registry.npmjs.org/ws/-/ws-8.18.3.tgz",
+ "integrity": "sha512-PEIGCY5tSlUt50cqyMXfCzX+oOPqN0vuGqWzbcJ2xvnkzkq46oOpz7dQaTDBdfICb4N14+GARUDw2XV2N4tvzg==",
"license": "MIT",
"engines": {
"node": ">=10.0.0"
diff --git a/app/package.json b/app/package.json
index a2701d41..6de31aa0 100644
--- a/app/package.json
+++ b/app/package.json
@@ -56,6 +56,7 @@
"ds-bot": "file:../ds-bot",
"electron-updater": "6.8.10",
"pnpm": "11.7.0",
+ "ws": "8.18.3",
"yaml": "2.9.1"
},
"devDependencies": {
diff --git a/app/src/bot-browser.mjs b/app/src/bot-browser.mjs
index a83531d8..63b5b6bd 100644
--- a/app/src/bot-browser.mjs
+++ b/app/src/bot-browser.mjs
@@ -47,6 +47,7 @@ export class BotBrowserGuests {
/** @type {Map<string, {owner: any, partition: string, attached: boolean, guest?: any}>} */
#leases = new Map()
#configured = new Set()
+ #downloadHandlers = new Map()
/**
* @param {{ session: any, origin: () => string | undefined, devTools: boolean }} options - Electron's
@@ -60,14 +61,26 @@ export class BotBrowserGuests {
/** One guest in the key's partition: an opaque lease and the partition approved for it. */
acquire(owner, key) {
+ const { partition } = this.storageFor(key)
+ const lease = randomUUID()
+ this.#leases.set(lease, { owner, partition, attached: false })
+ return { lease, partition }
+ }
+
+ /** Return the configured storage used by both the legacy guest and main-owned views. */
+ storageFor(key) {
const partition = partitionOf(keyOf(key))
if (!this.#configured.has(partition)) {
- this.#configure(this.session.fromPartition(partition))
+ this.#configure(this.session.fromPartition(partition), partition)
this.#configured.add(partition)
}
- const lease = randomUUID()
- this.#leases.set(lease, { owner, partition, attached: false })
- return { lease, partition }
+ return { partition, storage: this.session.fromPartition(partition) }
+ }
+
+ setDownloadHandler(key, handler) {
+ const { storage, partition } = this.storageFor(key)
+ this.#downloadHandlers.set(partition, handler)
+ return storage
}
/** Close a lease's guest; its partition's storage stays. */
@@ -93,6 +106,7 @@ export class BotBrowserGuests {
const storage = this.session.fromPartition(partition)
await storage.clearStorageData()
await storage.clearCache()
+ this.#downloadHandlers.delete(partition)
}
/** Install the attachment checks before the window's page can create a webview. */
@@ -163,12 +177,16 @@ export class BotBrowserGuests {
owner.once('destroyed', releaseAll)
}
- #configure(storage) {
+ #configure(storage, partition) {
storage.setPermissionRequestHandler((_contents, _permission, callback) => { callback(false) })
storage.setPermissionCheckHandler(() => false)
storage.setDevicePermissionHandler(() => false)
storage.setDisplayMediaRequestHandler((_request, callback) => { callback({}) })
- storage.on('will-download', (event) => { event.preventDefault() })
+ storage.on('will-download', (event, item, source) => {
+ const handler = this.#downloadHandlers.get(partition)
+ if (handler) handler(event, item, source)
+ else event.preventDefault()
+ })
storage.webRequest.onBeforeRequest((details, callback) => { callback({ cancel: !requestAllowed(details.url, this.origin()) }) })
}
}
diff --git a/app/src/bot-views.mjs b/app/src/bot-views.mjs
new file mode 100644
index 00000000..22a91225
--- /dev/null
+++ b/app/src/bot-views.mjs
@@ -0,0 +1,388 @@
+import { randomBytes, randomUUID } from 'node:crypto'
+import { mkdirSync } from 'node:fs'
+import { createServer } from 'node:http'
+import { join } from 'node:path'
+import { WebSocketServer } from 'ws'
+import { allowedNavigation, requestAllowed } from './bot-browser.mjs'
+
+const LOOPBACK = new Set(['127.0.0.1', '::1', '::ffff:127.0.0.1'])
+const rectOf = rect => ({
+ x: Math.max(0, Math.round(rect?.left ?? 0)),
+ y: Math.max(0, Math.round(rect?.top ?? 0)),
+ width: Math.max(1, Math.round(rect?.width ?? 800)),
+ height: Math.max(1, Math.round(rect?.height ?? 600)),
+})
+
+export class BotViews {
+ #window
+ #WebContentsView
+ #guests
+ #origin
+ #devTools
+ #views = new Map()
+ #tokens = new Map()
+ #clients = new Map()
+ #server
+ #wss
+ #address
+ #onChange
+ #downloadPaths = new Map()
+
+ constructor({ window, WebContentsView, guests, origin, devTools = false, onChange = () => {} }) {
+ this.#window = window
+ this.#WebContentsView = WebContentsView
+ this.#guests = guests
+ this.#origin = origin
+ this.#devTools = devTools
+ this.#onChange = onChange
+ this.#server = createServer((_request, response) => { response.writeHead(404).end() })
+ this.#wss = new WebSocketServer({ noServer: true })
+ this.#server.on('upgrade', (request, socket, head) => {
+ const peer = request.socket.remoteAddress
+ const token = /^\/devtools\/browser\/([a-f0-9]{64})$/u.exec(request.url ?? '')?.[1]
+ if (request.headers.origin !== undefined || !LOOPBACK.has(peer) || token === undefined || !this.#tokens.has(token)) {
+ socket.destroy()
+ return
+ }
+ this.#wss.handleUpgrade(request, socket, head, client => {
+ this.#wss.emit('connection', client, request, this.#tokens.get(token))
+ })
+ })
+ this.#wss.on('connection', (client, _request, botId) => this.#connect(client, botId))
+ this.#server.listen(0, '127.0.0.1')
+ this.#server.on('listening', () => { this.#address = this.#server.address() })
+ }
+
+ create(botId, { url = 'about:blank', createdByBot = false, openerId, tabId: requestedTabId } = {}) {
+ const tabId = requestedTabId ?? randomUUID()
+ const existing = this.#views.get(tabId)
+ if (existing) {
+ if (existing.botId !== botId) throw new Error('Browser tab belongs to another Bot')
+ return existing
+ }
+ const { partition } = this.#guests.storageFor(`bot:${botId}`)
+ const view = new this.#WebContentsView({
+ webPreferences: {
+ partition, nodeIntegration: false, nodeIntegrationInWorker: false, nodeIntegrationInSubFrames: false,
+ contextIsolation: true, sandbox: true, webSecurity: true, allowRunningInsecureContent: false,
+ webviewTag: false, plugins: false, navigateOnDragDrop: false, disableDialogs: true, devTools: this.#devTools,
+ },
+ })
+ const entry = { tabId, botId, view, createdByBot, openerId, sessions: new Map(), sessionMaps: new Map(), downloadPath: this.#downloadPaths.get(botId), mainFrameId: undefined }
+ this.#views.set(tabId, entry)
+ this.#window()?.contentView.addChildView(view)
+ view.setBounds(rectOf(null))
+ view.setVisible(false)
+ const contents = view.webContents
+ const clients = this.#clients.get(botId) ?? []
+ for (const client of clients) {
+ if (client.discover) this.#send(client.socket, { method: 'Target.targetCreated', params: { targetInfo: this.#targetInfo(entry) } })
+ if (client.autoAttach) client.attach(entry)
+ }
+ contents.on('did-navigate', () => this.#publish(entry))
+ contents.on('did-navigate-in-page', () => this.#publish(entry))
+ contents.on('page-title-updated', () => this.#publish(entry))
+ contents.on('page-favicon-updated', (_event, favicons) => { entry.favicon = favicons?.[0] ?? null; this.#publish(entry) })
+ contents.on('did-fail-load', (_event, code, description, _url, isMainFrame) => {
+ if (isMainFrame && code !== -3) { entry.error = description; this.#publish(entry) }
+ })
+ contents.on('did-start-loading', () => this.#publish(entry))
+ contents.on('did-stop-loading', () => this.#publish(entry))
+ contents.on('will-navigate', (event, target) => { if (!allowedNavigation(target, this.#origin())) event.preventDefault() })
+ contents.on('will-redirect', (event, target) => { if (!allowedNavigation(target, this.#origin())) event.preventDefault() })
+ contents.setWindowOpenHandler(({ url: target }) => {
+ if (entry.sessions.size > 0 && allowedNavigation(target, this.#origin())) {
+ const popup = this.create(botId, { url: target, createdByBot: true, openerId: tabId })
+ this.#publish(popup)
+ }
+ return { action: 'deny' }
+ })
+ contents.on('will-attach-webview', event => event.preventDefault())
+ contents.on('login', (event, _details, _auth, callback) => { event.preventDefault(); callback() })
+ this.#guests.setDownloadHandler(`bot:${botId}`, (event, item, source) => {
+ const target = [...this.#views.values()].find(value => value.botId === botId && value.view.webContents === source)
+ if (!target) { event.preventDefault(); return }
+ this.#download(target, event, item)
+ })
+ this.#installDebugger(entry)
+ this.#onChange({ type: 'created', ...this.#state(entry) })
+ if (url !== 'about:blank') void contents.loadURL(url).catch(() => {})
+ return entry
+ }
+
+ #download(entry, event, item) {
+ if (entry.downloadPath === undefined || entry.sessions.size === 0) {
+ event.preventDefault()
+ return
+ }
+ try {
+ mkdirSync(entry.downloadPath, { recursive: true })
+ const guid = randomUUID()
+ item.setSavePath(join(entry.downloadPath, guid))
+ this.#broadcast(entry.botId, { method: 'Browser.downloadWillBegin', params: {
+ frameId: entry.tabId, guid, url: item.getURL(), suggestedFilename: item.getFilename(),
+ } })
+ const progress = state => this.#broadcast(entry.botId, { method: 'Browser.downloadProgress', params: {
+ guid, receivedBytes: item.getReceivedBytes(), totalBytes: Math.max(0, item.getTotalBytes()),
+ state: state === 'completed' ? 'completed' : state === 'interrupted' ? 'canceled' : 'inProgress',
+ } })
+ item.on('updated', (_event, state) => progress(state))
+ item.once('done', (_event, state) => progress(state))
+ } catch {
+ event.preventDefault()
+ }
+ }
+
+ place(tabId, rect, visible = rect !== null) {
+ const entry = this.#views.get(tabId)
+ if (!entry) return false
+ if (rect !== null) entry.view.setBounds(rectOf(rect))
+ entry.view.setVisible(visible === true)
+ return true
+ }
+
+ async operate(botId, action, payload = {}) {
+ const entry = this.#owned(botId, payload.tabId)
+ const contents = entry.view.webContents
+ switch (action) {
+ case 'navigate': return contents.loadURL(String(payload.url))
+ case 'command':
+ if (payload.command === 'back' && contents.canGoBack()) contents.goBack()
+ else if (payload.command === 'forward' && contents.canGoForward()) contents.goForward()
+ else if (payload.command === 'reload') contents.reload()
+ else if (payload.command === 'stop') contents.stop()
+ return true
+ case 'focus': contents.focus(); return true
+ case 'read': return contents.executeJavaScript(String(payload.script))
+ case 'close': return this.close(botId, payload.tabId)
+ default: throw new Error('Unknown browser view operation')
+ }
+ }
+
+ async close(botId, tabId) {
+ const entry = this.#owned(botId, tabId)
+ for (const [socket, sessionId] of entry.sessions) {
+ this.#send(socket, { method: 'Target.detachedFromTarget', params: { sessionId, targetId: tabId } })
+ if ([...(this.#clients.get(botId) ?? [])].some(client => client.socket === socket && client.discover)) {
+ this.#send(socket, { method: 'Target.targetDestroyed', params: { targetId: tabId } })
+ }
+ }
+ this.#views.delete(tabId)
+ entry.view.webContents.close()
+ entry.sessions.clear()
+ entry.sessionMaps.clear()
+ this.#onChange({ type: 'closed', botId, tabId })
+ return true
+ }
+
+ async forget(botId) {
+ for (const entry of [...this.#views.values()]) if (entry.botId === botId) await this.close(botId, entry.tabId)
+ for (const [token, id] of this.#tokens) if (id === botId) this.#tokens.delete(token)
+ this.#downloadPaths.delete(botId)
+ }
+
+ async endpoint(botId) {
+ if (!this.#address) await new Promise(resolve => this.#server.once('listening', resolve))
+ for (const [token, id] of this.#tokens) if (id === botId) this.#tokens.delete(token)
+ const token = randomBytes(32).toString('hex')
+ this.#tokens.set(token, botId)
+ return `ws://127.0.0.1:${this.#address.port}/devtools/browser/${token}`
+ }
+
+ sync(botId) {
+ return [...this.#views.values()].filter(entry => entry.botId === botId).map(entry => this.#state(entry))
+ }
+
+ dispose() {
+ for (const entry of this.#views.values()) entry.view.webContents.close()
+ this.#views.clear()
+ this.#tokens.clear()
+ this.#wss.close()
+ this.#server.close()
+ }
+
+ #owned(botId, tabId) {
+ const entry = this.#views.get(tabId)
+ if (!entry || entry.botId !== botId) throw new Error('Browser tab does not belong to this Bot')
+ return entry
+ }
+
+ #state(entry) {
+ const contents = entry.view.webContents
+ return {
+ botId: entry.botId, tabId: entry.tabId, createdByBot: entry.createdByBot,
+ url: contents.getURL(), title: contents.getTitle(), loading: contents.isLoading(),
+ canGoBack: contents.canGoBack(), canGoForward: contents.canGoForward(),
+ favicon: entry.favicon ?? null, error: entry.error ?? null, live: true,
+ botAttached: entry.sessions.size > 0,
+ }
+ }
+
+ #publish(entry) {
+ if (!entry.view.webContents.isDestroyed()) {
+ if (!entry.view.webContents.isLoading()) entry.error = null
+ this.#onChange({ type: 'changed', ...this.#state(entry) })
+ }
+ }
+
+ #installDebugger(entry) {
+ const debuggerApi = entry.view.webContents.debugger
+ debuggerApi.attach('1.3')
+ debuggerApi.on('message', (_event, method, params, childSessionId) => {
+ this.#sendToSessions(entry, { method, params: this.#framePayload(entry, method, params) }, childSessionId)
+ })
+ }
+
+ #frameId(entry, frameId) {
+ return frameId === entry.mainFrameId ? entry.tabId : frameId
+ }
+
+ #framePayload(entry, method, payload) {
+ const value = structuredClone(payload)
+ if (method === 'Page.getFrameTree' && value.frameTree?.frame && !value.frameTree.frame.parentId) {
+ entry.mainFrameId = value.frameTree.frame.id
+ }
+ if (method === 'Page.frameNavigated' && value.frame && !value.frame.parentId) entry.mainFrameId = value.frame.id
+ if (method === 'Page.frameAttached' && !value.parentFrameId) entry.mainFrameId = value.frameId
+ const mapFrame = frame => {
+ if (!frame) return
+ if (frame.id !== undefined) frame.id = this.#frameId(entry, frame.id)
+ if (frame.parentId !== undefined) frame.parentId = this.#frameId(entry, frame.parentId)
+ for (const child of frame.childFrames ?? []) mapFrame(child)
+ }
+ if (method === 'Page.getFrameTree') mapFrame(value.frameTree?.frame)
+ if (value.frameId !== undefined) value.frameId = this.#frameId(entry, value.frameId)
+ if (value.parentFrameId !== undefined) value.parentFrameId = this.#frameId(entry, value.parentFrameId)
+ if (value.frame) mapFrame(value.frame)
+ if (value.context?.auxData?.frameId !== undefined) value.context.auxData.frameId = this.#frameId(entry, value.context.auxData.frameId)
+ return value
+ }
+
+ #sendToSessions(entry, message, childSessionId) {
+ for (const [socket, rootSessionId] of entry.sessions) {
+ if (childSessionId) entry.sessionMaps.get(socket)?.set(childSessionId, { entry, nativeId: childSessionId })
+ this.#send(socket, { ...message, sessionId: childSessionId || rootSessionId })
+ }
+ }
+
+ #broadcast(botId, message) {
+ for (const client of this.#clients.get(botId) ?? []) this.#send(client.socket, message)
+ }
+
+ #send(socket, message) {
+ if (socket.readyState === 1) socket.send(JSON.stringify(message))
+ }
+
+ #connect(socket, botId) {
+ const sessions = new Map()
+ const clientState = { socket, botId, autoAttach: false, discover: false, attach: undefined }
+ const clients = this.#clients.get(botId) ?? new Set()
+ clients.add(clientState)
+ this.#clients.set(botId, clients)
+ const targets = () => [...this.#views.values()].filter(entry => entry.botId === botId)
+ const info = entry => this.#targetInfo(entry)
+ const attach = entry => {
+ const existing = entry.sessions.get(socket)
+ if (existing) return existing
+ const sessionId = randomUUID()
+ sessions.set(sessionId, { entry, nativeId: undefined })
+ entry.sessions.set(socket, sessionId)
+ entry.sessionMaps.set(socket, sessions)
+ this.#send(socket, { method: 'Target.attachedToTarget', params: { sessionId, targetInfo: info(entry), waitingForDebugger: false } })
+ this.#onChange({ type: 'changed', ...this.#state(entry) })
+ return sessionId
+ }
+ clientState.attach = attach
+ let commandQueue = Promise.resolve()
+ socket.on('message', raw => {
+ commandQueue = commandQueue.then(async () => {
+ let request
+ try { request = JSON.parse(raw.toString()) } catch { return }
+ const { id, method, params = {}, sessionId } = request
+ const reply = result => {
+ this.#send(socket, { id, result, ...(sessionId ? { sessionId } : {}) })
+ }
+ const fail = error => {
+ this.#send(socket, { id, error: { code: -32000, message: String(error?.message ?? error) }, ...(sessionId ? { sessionId } : {}) })
+ }
+ try {
+ if (method === 'Browser.getVersion') { reply({ protocolVersion: '1.3', product: `Chrome/${process.versions.chrome}`, userAgent: `Electron/${process.versions.electron} Chrome/${process.versions.chrome}`, jsVersion: process.versions.v8 }); return }
+ if (method === 'Browser.setDownloadBehavior') {
+ this.#downloadPaths.set(botId, params.downloadPath)
+ for (const entry of targets()) entry.downloadPath = params.downloadPath
+ reply({}); return
+ }
+ if (method === 'Target.getBrowserContexts') { reply({ browserContextIds: [`bot:${botId}`] }); return }
+ if (method === 'Target.getTargets') { reply({ targetInfos: targets().map(info) }); return }
+ if (method === 'Target.setDiscoverTargets') {
+ clientState.discover = params.discover === true
+ if (clientState.discover) for (const entry of targets()) this.#send(socket, { method: 'Target.targetCreated', params: { targetInfo: info(entry) } })
+ reply({}); return
+ }
+ if (method === 'Target.setAutoAttach') {
+ clientState.autoAttach = params.autoAttach === true
+ if (clientState.autoAttach) for (const entry of targets()) if (!entry.sessions.has(socket)) attach(entry)
+ reply({}); return
+ }
+ if (method === 'Target.attachToTarget') {
+ const entry = this.#owned(botId, params.targetId)
+ const attached = attach(entry)
+ this.#send(socket, { id, result: { sessionId: attached } })
+ return
+ }
+ if (method === 'Target.createTarget') {
+ const entry = this.create(botId, { url: params.url ?? 'about:blank', createdByBot: true })
+ if (entry && params.background !== true) entry.view.webContents.focus()
+ reply({ targetId: entry.tabId }); return
+ }
+ if (method === 'Target.closeTarget') { await this.close(botId, params.targetId); reply({ success: true }); return }
+ if (method === 'Target.activateTarget') { this.#owned(botId, params.targetId).view.webContents.focus(); reply({}); return }
+ if (method === 'Target.detachFromTarget') {
+ const route = sessions.get(params.sessionId)
+ if (route?.nativeId) await route.entry.view.webContents.debugger.sendCommand('Target.detachFromTarget', { sessionId: route.nativeId })
+ route?.entry.sessions.delete(socket)
+ route?.entry.sessionMaps.delete(socket)
+ sessions.delete(params.sessionId); reply({}); return
+ }
+ if (method === 'Target.getTargetInfo') {
+ const entry = params.targetId
+ ? this.#owned(botId, params.targetId)
+ : sessions.get(sessionId)?.entry ?? targets()[0]
+ if (!entry || entry.botId !== botId) throw new Error('Unknown target')
+ reply({ targetInfo: info(entry) }); return
+ }
+ if (method === 'Browser.close') { socket.close(); return }
+ if (sessionId) {
+ const route = sessions.get(sessionId)
+ if (!route) throw new Error('Unknown target session')
+ const nativeParams = { ...params }
+ if (nativeParams.frameId === route.entry.tabId) nativeParams.frameId = route.entry.mainFrameId
+ const result = await route.entry.view.webContents.debugger.sendCommand(method, nativeParams, route.nativeId)
+ reply(this.#framePayload(route.entry, method, result))
+ return
+ }
+ if (['Browser.getBrowserCommandLine', 'Browser.getWindowForTarget', 'Target.setDiscoverTargets'].includes(method)) { reply({}); return }
+ throw new Error(`Unsupported CDP method: ${method}`)
+ } catch (error) { fail(error) }
+ })
+ })
+ socket.on('close', () => {
+ for (const entry of targets()) {
+ entry.sessions.delete(socket)
+ entry.sessionMaps.delete(socket)
+ this.#onChange({ type: 'changed', ...this.#state(entry) })
+ }
+ sessions.clear()
+ clients.delete(clientState)
+ if (clients.size === 0) this.#clients.delete(botId)
+ })
+ }
+
+ #targetInfo(entry) {
+ return {
+ targetId: entry.tabId, type: 'page', title: entry.view.webContents.getTitle(),
+ url: entry.view.webContents.getURL(), attached: entry.sessions.size > 0, canAccessOpener: true,
+ browserContextId: `bot:${entry.botId}`, ...(entry.openerId ? { openerId: entry.openerId } : {}),
+ }
+ }
+}
diff --git a/app/src/host-process.mjs b/app/src/host-process.mjs
index 18d5b3db..dba66e5c 100644
--- a/app/src/host-process.mjs
+++ b/app/src/host-process.mjs
@@ -20,6 +20,7 @@ export class HostProcess {
#stderr = ''
#stopping = false
#failed = false
+ #messageListeners = new Set()
/**
* @param {{ entry: string, cwd: string, env: NodeJS.ProcessEnv, pnpm: string, onFailure: (error: Error) => void }} options
@@ -46,6 +47,7 @@ export class HostProcess {
this.#stderr = (this.#stderr + chunk).slice(-STDERR_TAIL_CHARS)
})
child.on('message', (message) => {
+ for (const listener of this.#messageListeners) listener(message)
if (message?.type === 'ready') resolve({ url: message.url, injections: message.injections })
else if (message?.type === 'checked') resolve({ unreadable: message.unreadable })
else if (message?.type === 'fatal') this.#fail(new Error(message.message), reject)
@@ -62,6 +64,13 @@ export class HostProcess {
return this.#ready
}
+ onMessage(listener) { this.#messageListeners.add(listener); return () => this.#messageListeners.delete(listener) }
+
+ send(message) {
+ if (!this.#child?.connected) throw new Error('The DSH Host IPC channel is closed')
+ this.#child.send(message)
+ }
+
/** Ask the Host to shut down, then end it if it does not exit in time. */
async stop() {
const child = this.#child
diff --git a/app/src/main.mjs b/app/src/main.mjs
index f1d3a6ea..714723af 100644
--- a/app/src/main.mjs
+++ b/app/src/main.mjs
@@ -1,7 +1,7 @@
// Electron main process: one window for the Bot team, served by the app's own DSH Host.
// The window loads the Host's loopback address as DSH Web does: DSH's sign-in sends the page's
// origin as the address the browser comes back to, and accepts only a loopback one.
-import { app, BrowserWindow, dialog, ipcMain, Menu, nativeTheme, session, shell } from 'electron'
+import { app, BrowserWindow, WebContentsView, dialog, ipcMain, Menu, nativeTheme, session, shell } from 'electron'
import { mkdirSync, readdirSync, readFileSync, writeFileSync } from 'node:fs'
import { readFile, rm } from 'node:fs/promises'
import { release } from 'node:os'
@@ -11,6 +11,7 @@ import { fileURLToPath } from 'node:url'
import { getDshRuntimeVersion } from '@deepseek-ai/dsh-app-boot'
import electronUpdater from 'electron-updater'
import { BotBrowserGuests, partitionOf } from './bot-browser.mjs'
+import { BotViews } from './bot-views.mjs'
import { importFromBrowser, listSources } from './browser-sources.mjs'
import { applyCookies, CookieImports } from './cookie-import.mjs'
import { parseCookieFile } from './cookie-parse.mjs'
@@ -29,6 +30,11 @@ const IPC = {
caption: 'ds-bot:caption-colors', nativeTheme: 'ds-bot:native-theme', fullscreen: 'ds-bot:fullscreen',
dataLocation: 'ds-bot:data-location', moveData: 'ds-bot:move-data', teamWorkspace: 'ds-bot:team-workspace',
browserAcquire: 'ds-bot:browser-acquire', browserRelease: 'ds-bot:browser-release', browserForget: 'ds-bot:browser-forget',
+ browserViewChanged: 'ds-bot:browser-view-changed', browserViewCreate: 'ds-bot:browser-view-create',
+ browserViewSync: 'ds-bot:browser-view-sync', browserViewPlace: 'ds-bot:browser-view-place',
+ browserViewNavigate: 'ds-bot:browser-view-navigate', browserViewCommand: 'ds-bot:browser-view-command',
+ browserViewFocus: 'ds-bot:browser-view-focus', browserViewRead: 'ds-bot:browser-view-read',
+ browserViewClose: 'ds-bot:browser-view-close',
browserImportSources: 'ds-bot:browser-import-sources', browserImportPick: 'ds-bot:browser-import-pick',
browserImportLive: 'ds-bot:browser-import-live', browserImportApply: 'ds-bot:browser-import-apply',
browserImportCancel: 'ds-bot:browser-import-cancel',
@@ -66,7 +72,12 @@ let choosing
let copying
const copyAbort = new AbortController()
const browserGuests = new BotBrowserGuests({ session, origin: () => origin, devTools: !app.isPackaged })
+const botViews = new BotViews({
+ window: () => mainWindow, WebContentsView, guests: browserGuests, origin: () => origin, devTools: !app.isPackaged,
+ onChange: state => mainWindow?.webContents.send(IPC.browserViewChanged, state),
+})
const cookieImports = new CookieImports()
+app.once('will-quit', () => botViews.dispose())
// A packaged app started through an 8.3 short path or a junction restarts once on the
// canonical path: the lock below and every path the Host sees then agree with the long
@@ -145,6 +156,12 @@ async function start() {
updates = startUpdates()
updates?.subscribe((state) => { mainWindow?.webContents.send(IPC.appUpdateChanged, state) })
host = newHost()
+ host.onMessage(message => {
+ if (message?.type !== 'ds-bot:browser-endpoint' || typeof message.id !== 'string' || typeof message.botId !== 'string') return
+ void botViews.endpoint(message.botId).then(url => {
+ host?.send({ type: 'ds-bot:browser-endpoint-response', id: message.id, url })
+ }).catch(() => host?.send({ type: 'ds-bot:browser-endpoint-response', id: message.id, error: 'Could not create the browser endpoint.' }))
+ })
return host.start()
})()
ready.catch(fail)
@@ -169,8 +186,23 @@ async function start() {
})
ipcMain.handle(IPC.browserForget, (event, key) => {
assertWindow(event)
- return browserGuests.forget(event.sender, key)
+ const botId = typeof key === 'string' && key.startsWith('bot:') ? key.slice(4) : ''
+ return Promise.resolve(botId === '' ? undefined : botViews.forget(botId)).then(() => browserGuests.forget(event.sender, key))
+ })
+ ipcMain.handle(IPC.browserViewCreate, (event, botId, tabId, url) => {
+ assertWindow(event)
+ return botViews.create(String(botId), { tabId: String(tabId), ...(typeof url === 'string' && url !== '' ? { url } : {}) }).tabId
+ })
+ ipcMain.handle(IPC.browserViewSync, (event, botId) => {
+ assertWindow(event)
+ return botViews.sync(String(botId))
})
+ ipcMain.handle(IPC.browserViewPlace, (event, tabId, rect, visible) => { assertWindow(event); return botViews.place(String(tabId), rect, visible === true) })
+ ipcMain.handle(IPC.browserViewNavigate, (event, botId, tabId, url) => { assertWindow(event); return botViews.operate(String(botId), 'navigate', { tabId: String(tabId), url }) })
+ ipcMain.handle(IPC.browserViewCommand, (event, botId, tabId, command) => { assertWindow(event); return botViews.operate(String(botId), 'command', { tabId: String(tabId), command }) })
+ ipcMain.handle(IPC.browserViewFocus, (event, botId, tabId) => { assertWindow(event); return botViews.operate(String(botId), 'focus', { tabId: String(tabId) }) })
+ ipcMain.handle(IPC.browserViewRead, (event, botId, tabId, script) => { assertWindow(event); return botViews.operate(String(botId), 'read', { tabId: String(tabId), script }) })
+ ipcMain.handle(IPC.browserViewClose, (event, botId, tabId) => { assertWindow(event); return botViews.operate(String(botId), 'close', { tabId: String(tabId) }) })
// Cookie imports: a file or an installed browser's profile is parsed or harvested into a
// pending set first; only apply (the user's confirm) writes into the Bot's partition.
ipcMain.handle(IPC.browserImportSources, (event) => {
diff --git a/app/src/preload.cjs b/app/src/preload.cjs
index 5479f0d6..d3d77400 100644
--- a/app/src/preload.cjs
+++ b/app/src/preload.cjs
@@ -35,6 +35,12 @@ if (host !== undefined && location.origin === host) {
function browserBridge() {
const listeners = new Map()
+ const viewListeners = new Set()
+ ipcRenderer.on('ds-bot:browser-view-changed', (_event, change) => {
+ for (const listener of [...viewListeners]) {
+ try { listener(change) } catch (error) { console.error('DeepSeek Bot: a browser view listener failed', error) }
+ }
+ })
ipcRenderer.on('ds-bot:browser-open-requested', (_event, request) => {
if (typeof request?.lease !== 'string' || typeof request.url !== 'string') return
for (const listener of [...(listeners.get(request.lease) ?? [])]) {
@@ -45,6 +51,17 @@ function browserBridge() {
acquire: key => ipcRenderer.invoke('ds-bot:browser-acquire', String(key)),
release: lease => ipcRenderer.invoke('ds-bot:browser-release', String(lease)),
forget: key => ipcRenderer.invoke('ds-bot:browser-forget', String(key)),
+ views: {
+ create: (botId, tabId, url) => ipcRenderer.invoke('ds-bot:browser-view-create', String(botId), String(tabId), typeof url === 'string' ? url : undefined),
+ sync: botId => ipcRenderer.invoke('ds-bot:browser-view-sync', String(botId)),
+ place: (tabId, rect, visible) => ipcRenderer.invoke('ds-bot:browser-view-place', String(tabId), rect, visible === true),
+ navigate: (botId, tabId, url) => ipcRenderer.invoke('ds-bot:browser-view-navigate', String(botId), String(tabId), String(url)),
+ command: (botId, tabId, command) => ipcRenderer.invoke('ds-bot:browser-view-command', String(botId), String(tabId), String(command)),
+ focus: (botId, tabId) => ipcRenderer.invoke('ds-bot:browser-view-focus', String(botId), String(tabId)),
+ read: (botId, tabId, script) => ipcRenderer.invoke('ds-bot:browser-view-read', String(botId), String(tabId), String(script)),
+ close: (botId, tabId) => ipcRenderer.invoke('ds-bot:browser-view-close', String(botId), String(tabId)),
+ subscribe(listener) { viewListeners.add(listener); return () => viewListeners.delete(listener) },
+ },
// Cookie imports, only in the app: sources and a two-step pick/harvest → preview → apply.
importSources: () => ipcRenderer.invoke('ds-bot:browser-import-sources'),
importPick: () => ipcRenderer.invoke('ds-bot:browser-import-pick'),
diff --git a/app/test/bot-views.test.mjs b/app/test/bot-views.test.mjs
new file mode 100644
index 00000000..788fb700
--- /dev/null
+++ b/app/test/bot-views.test.mjs
@@ -0,0 +1,170 @@
+import assert from 'node:assert/strict'
+import { EventEmitter } from 'node:events'
+import { mkdtempSync, rmSync } from 'node:fs'
+import { tmpdir } from 'node:os'
+import { join } from 'node:path'
+import { test } from 'node:test'
+import { WebSocket } from 'ws'
+import { BotViews } from '../src/bot-views.mjs'
+
+function fixture() {
+ const changes = []
+ const handlers = new Map()
+ const views = []
+ class WebContents extends EventEmitter {
+ constructor() {
+ super()
+ this.url = 'about:blank'
+ this.debugger = Object.assign(new EventEmitter(), {
+ attach() {},
+ async sendCommand(method) {
+ if (method === 'Page.getFrameTree') return { frameTree: { frame: { id: 'frame-a', url: 'https://example.com/' } } }
+ return { method }
+ },
+ })
+ }
+ getURL() { return this.url }
+ getTitle() { return this.url }
+ isLoading() { return false }
+ canGoBack() { return false }
+ canGoForward() { return false }
+ isDestroyed() { return false }
+ async loadURL(url) { this.url = url }
+ async executeJavaScript(script) { return script }
+ setWindowOpenHandler(handler) { this.windowOpenHandler = handler }
+ close() {}
+ focus() {}
+ }
+ class WebContentsView {
+ constructor(options) {
+ this.options = options
+ this.webContents = new WebContents()
+ views.push(this)
+ }
+ setBounds(bounds) { this.bounds = bounds }
+ setVisible(visible) { this.visible = visible }
+ }
+ const guests = {
+ storageFor(key) { return { partition: `persist:${key}` } },
+ setDownloadHandler(key, handler) { handlers.set(key, handler) },
+ }
+ const window = { contentView: { addChildView() {} } }
+ const manager = new BotViews({ window: () => window, WebContentsView, guests, origin: () => 'http://127.0.0.1:43117' , onChange: change => changes.push(change) })
+ return { manager, handlers, views, changes }
+}
+
+function openSocket(url, options) {
+ return new Promise((resolve, reject) => {
+ const socket = new WebSocket(url, options)
+ socket.once('open', () => resolve(socket))
+ socket.once('error', reject)
+ })
+}
+
+function closed(socket) {
+ return new Promise(resolve => {
+ socket.on('error', () => {})
+ socket.once('close', resolve)
+ })
+}
+
+function request(socket, id, method, params = {}, sessionId) {
+ return new Promise((resolve, reject) => {
+ const timer = setTimeout(() => reject(new Error(`Timed out waiting for ${method}`)), 1000)
+ const onMessage = raw => {
+ const message = JSON.parse(raw.toString())
+ if (message.id !== id) return
+ clearTimeout(timer)
+ socket.off('message', onMessage)
+ resolve(message)
+ }
+ socket.on('message', onMessage)
+ socket.send(JSON.stringify({ id, method, params, ...(sessionId ? { sessionId } : {}) }))
+ })
+}
+
+test('CDP relay limits targets and created tabs to the token Bot', async t => {
+ const { manager, views } = fixture()
+ t.after(() => manager.dispose())
+ manager.create('a')
+ manager.create('b')
+ const [a, b] = await Promise.all([manager.endpoint('a'), manager.endpoint('b')])
+ const socketA = await openSocket(a)
+ const socketB = await openSocket(b)
+ t.after(() => { socketA.close(); socketB.close() })
+ const targetsA = await request(socketA, 1, 'Target.getTargets')
+ const targetsB = await request(socketB, 2, 'Target.getTargets')
+ assert.equal(targetsA.result.targetInfos.length, 1)
+ assert.equal(targetsA.result.targetInfos[0].browserContextId, 'bot:a')
+ assert.equal(targetsB.result.targetInfos.length, 1)
+ assert.equal(targetsB.result.targetInfos[0].browserContextId, 'bot:b')
+ const rootInfo = await request(socketA, 3, 'Target.getTargetInfo')
+ assert.equal(rootInfo.result.targetInfo.targetId, targetsA.result.targetInfos[0].targetId)
+ const foreignInfo = await request(socketA, 4, 'Target.getTargetInfo', { targetId: targetsB.result.targetInfos[0].targetId })
+ assert.equal(foreignInfo.error.message, 'Browser tab does not belong to this Bot')
+ const attached = await request(socketA, 5, 'Target.attachToTarget', { targetId: targetsA.result.targetInfos[0].targetId, flatten: true })
+ const sessionId = attached.result.sessionId
+ const pageEnabled = await request(socketA, 6, 'Page.enable', {}, sessionId)
+ assert.equal(pageEnabled.sessionId, sessionId)
+ assert.deepEqual(pageEnabled.result, { method: 'Page.enable' })
+ const frameTree = await request(socketA, 7, 'Page.getFrameTree', {}, sessionId)
+ assert.equal(frameTree.result.frameTree.frame.id, targetsA.result.targetInfos[0].targetId)
+ const created = await request(socketA, 8, 'Target.createTarget', { url: 'about:blank' })
+ assert.equal(created.result.targetId, manager.sync('a')[1].tabId)
+ assert.equal(manager.sync('a').length, 2)
+ assert.equal(manager.sync('b').length, 1)
+})
+
+test('CDP relay rejects invalid paths and Origin headers', async t => {
+ const { manager } = fixture()
+ t.after(() => manager.dispose())
+ const endpoint = await manager.endpoint('a')
+ const valid = await openSocket(endpoint)
+ t.after(() => valid.close())
+ const originSocket = new WebSocket(endpoint, { headers: { Origin: 'https://example.com' } })
+ await closed(originSocket)
+ const badPath = new WebSocket(endpoint.replace(/\/devtools\/browser\/[^/]+$/u, '/invalid'))
+ await closed(badPath)
+})
+
+test('CDP download behavior saves to the Bot path and emits download events', async t => {
+ const { manager, handlers, views } = fixture()
+ t.after(() => manager.dispose())
+ const downloadPath = mkdtempSync(join(tmpdir(), 'bot-views-download-'))
+ t.after(() => rmSync(downloadPath, { recursive: true, force: true }))
+ const entry = manager.create('a')
+ const socket = await openSocket(await manager.endpoint('a'))
+ t.after(() => socket.close())
+ const discovered = await request(socket, 1, 'Target.attachToTarget', { targetId: entry.tabId, flatten: true })
+ assert.ok(discovered.result.sessionId)
+ const sessionId = discovered.result.sessionId
+ await request(socket, 2, 'Browser.setDownloadBehavior', { behavior: 'allow', downloadPath })
+ const events = []
+ let resolveEvents
+ const eventsReady = new Promise(resolve => { resolveEvents = resolve })
+ socket.on('message', raw => {
+ const message = JSON.parse(raw.toString())
+ if (message.method?.startsWith('Browser.download')) {
+ events.push(message)
+ if (events.length === 3) resolveEvents()
+ }
+ })
+ const saved = []
+ const item = Object.assign(new EventEmitter(), {
+ setSavePath(path) { saved.push(path) },
+ getURL() { return 'https://example.com/file' },
+ getFilename() { return 'report.pdf' },
+ getReceivedBytes() { return 3 },
+ getTotalBytes() { return 3 },
+ })
+ const event = { preventDefault() { throw new Error('download was unexpectedly prevented') } }
+ handlers.get('bot:a')(event, item, views[0].webContents)
+ item.emit('updated', {}, 'progressing')
+ item.emit('done', {}, 'completed')
+ await Promise.race([eventsReady, new Promise((_, reject) => setTimeout(() => reject(new Error('download events were not relayed')), 1000))])
+ assert.equal(saved.length, 1)
+ assert.match(saved[0], new RegExp(`^${downloadPath.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}[/\\\\]`, 'u'))
+ assert.equal(events[0].params.suggestedFilename, 'report.pdf')
+ assert.equal(events.some(message => message.method === 'Browser.downloadProgress' && message.params.state === 'completed'), true)
+ assert.equal(sessionId.length > 0, true)
+})