Skip to content

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

Share the first browser launch, consume screenshots, drop profiles with the Bot

Fei2411dfef33c799b5
diff --git a/ds-bot/src/host/browser-plugins.js b/ds-bot/src/host/browser-plugins.js
index 2927504d..746abbcb 100644
--- a/ds-bot/src/host/browser-plugins.js
+++ b/ds-bot/src/host/browser-plugins.js
@@ -94,7 +94,7 @@ export function install(rt) {
? rt.config.browserHeadless
: defaultBrowserHeadless(process.platform)
const downloadRoot = join(rt.home, 'browser-downloads')
- mountBrowserPlugin(rt.ctx, {
+ const mounted = mountBrowserPlugin(rt.ctx, {
toolDefs: plugin.tools ?? plugin.toolDefs,
defineTool,
profileRoot: join(rt.home, 'browser-profiles'),
@@ -119,5 +119,14 @@ export function install(rt) {
return id
},
})
+ // A deleted Bot leaves no browser state behind: close its session and drop the profile dir.
+ if (typeof rt.deleteBot === 'function') {
+ const originalDelete = rt.deleteBot.bind(rt)
+ rt.deleteBot = async (id) => {
+ const result = await originalDelete(id)
+ await mounted.drop(id).catch(() => {})
+ return result
+ }
+ }
rt.browserPlugin = plugin.id ?? name
}
diff --git a/ds-bot/src/host/browser/mount.js b/ds-bot/src/host/browser/mount.js
index dbe5b4cd..8777badb 100644
--- a/ds-bot/src/host/browser/mount.js
+++ b/ds-bot/src/host/browser/mount.js
@@ -1,5 +1,6 @@
// Registers one browser plugin's tools. ds-bot passes a key function that resolves
// the Bot; a standalone package can pass the Session id instead.
+import { rm } from 'node:fs/promises'
import { join } from 'node:path'
import { openBotBrowser } from './session.js'
import { gateScope } from './gate.js'
@@ -25,9 +26,17 @@ export function mountBrowserPlugin(ctx, { toolDefs, defineTool, keyOf, profileRo
ctx.effect(() => async () => {
const open = [...sessions.values()]
sessions.clear()
- await Promise.all(open.map(session => session.close()))
+ await Promise.all(open.map(session => Promise.resolve(session).then(opened => opened.close(), () => {})))
}, 'ds-bot: browser plugin')
+ // Closes a Bot's browser and removes its profile directory; used when the Bot is deleted.
+ async function drop(botId) {
+ const session = sessions.get(botId)
+ sessions.delete(botId)
+ if (session !== undefined) await Promise.resolve(session).then(opened => opened.close(), () => {})
+ await rm(join(profileRoot, botId), { recursive: true, force: true }).catch(() => {})
+ }
+
for (const spec of toolDefs) {
ctx.tools.register(defineTool({
name: spec.name,
@@ -39,7 +48,8 @@ export function mountBrowserPlugin(ctx, { toolDefs, defineTool, keyOf, profileRo
if (!botId) return 'Only Bots on the team have a browser.'
let session = sessions.get(botId)
if (spec.needsBrowser && session === undefined) {
- session = await openBotBrowser({
+ // Stored before the first await so concurrent calls share one browser launch.
+ const opening = openBotBrowser({
botId,
profileRoot,
downloadDir: join(downloadRoot ?? join(profileRoot, 'browser-downloads'), botId),
@@ -49,8 +59,13 @@ export function mountBrowserPlugin(ctx, { toolDefs, defineTool, keyOf, profileRo
actionTimeout,
uploadRoots: uploadRoots ? () => uploadRoots(botId) : undefined,
})
- sessions.set(botId, session)
+ sessions.set(botId, opening)
+ opening.catch(() => {
+ if (sessions.get(botId) === opening) sessions.delete(botId)
+ })
+ session = opening
}
+ session = await session
const gate = authorize ? action => authorize(exec, botId, action) : undefined
return gateScope.run(gate, () => spec.execute(args ?? {}, {
botId,
@@ -61,4 +76,6 @@ export function mountBrowserPlugin(ctx, { toolDefs, defineTool, keyOf, profileRo
},
}))
}
+
+ return { drop }
}
diff --git a/ds-bot/src/host/browser/script.js b/ds-bot/src/host/browser/script.js
index a3c9a789..d987214a 100644
--- a/ds-bot/src/host/browser/script.js
+++ b/ds-bot/src/host/browser/script.js
@@ -27,7 +27,7 @@ export const toolDefs = [
},
async execute(args, ctx) {
const text = await ctx.session.runScript(args.script)
- const image = ctx.session.lastImage
+ const image = typeof ctx.session.takeLastImage === 'function' ? ctx.session.takeLastImage() : ctx.session.lastImage
return image ? { text, image } : { text }
},
},
diff --git a/ds-bot/src/host/browser/session.js b/ds-bot/src/host/browser/session.js
index 21f4db9b..6e4c23ff 100644
--- a/ds-bot/src/host/browser/session.js
+++ b/ds-bot/src/host/browser/session.js
@@ -492,6 +492,11 @@ function createSession({ context, page, botId, userDataDir, cookieFile, download
userDataDir,
get hold() { return hold },
get lastImage() { return lastImage },
+ takeLastImage() {
+ const image = lastImage
+ lastImage = ''
+ return image
+ },
url: () => activePage()?.url() ?? '',
async open(url) {
ready()