A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Merge remote-tracking branch 'origin/cursor/bot-browser-plugins-c44c' into merge-528
Fei241120c2d8dfecb0
diff --cc ds-bot/src/host/browser-plugins.jsindex 5cda4663,746abbcb..2f6abd4c--- a/ds-bot/src/host/browser-plugins.js+++ b/ds-bot/src/host/browser-plugins.js@@@ -116,14 -94,7 +116,16 @@@ export function install(rt)? rt.config.browserHeadless: defaultBrowserHeadless(process.platform)const downloadRoot = join(rt.home, 'browser-downloads')- const mounted = mountBrowserPlugin(rt.ctx, {+ async function downloadRoots(botId) {+ await rt.load()+ const team = rt.teamFolder()+ const requested = rt.workDirOf({ botId })+ const working = requested !== undefined && await rt.isFolder(requested) ? requested : team+ return [...new Set([working, team].filter(root => typeof root === 'string' && isAbsolute(root)))]+ }++ const mounted = {}+ mountBrowserPlugin(rt.ctx, {++ api: mounted,toolDefs: plugin.tools ?? plugin.toolDefs,defineTool,profileRoot: join(rt.home, 'browser-profiles'),diff --cc ds-bot/src/host/browser/mount.jsindex 6de9ca65,8777badb..f74bb648--- a/ds-bot/src/host/browser/mount.js+++ b/ds-bot/src/host/browser/mount.js@@@ -30,24 -19,24 +31,32 @@@ export const browserOutput =/*** @param {any} ctx- * @param {{ toolDefs: any[], defineTool: Function, keyOf: (exec: any) => Promise<string|undefined>, profileRoot: string, downloadRoot?: string, downloadRoots?: (botId: string) => Promise<string[]>, uploadRoots?: (botId: string) => Promise<string[]>, headless: boolean, playwrightUrl?: string, executablePath?: string, actionTimeout?: number, authorize?: (exec: any, botId: string, action: any) => Promise<void>, completeFor?: (exec: any) => Function, connect?: (botId: string, downloadRoots?: string[]) => Promise<string>, getHold?: (botId: string) => any, onHold?: (botId: string, hold: any) => void, subscribeHolds?: (listener: (botId: string, hold: any) => void) => (() => void) | undefined, openSession?: typeof openBotBrowser }} options- * @param {{ toolDefs: any[], defineTool: Function, keyOf: (exec: any) => Promise<string|undefined>, profileRoot: string, downloadRoot?: string, uploadRoots?: (botId: string) => Promise<string[]>, headless: boolean, playwrightUrl?: string, executablePath?: string, actionTimeout?: number, authorize?: (exec: any, botId: string, action: any) => Promise<void>, completeFor?: (exec: any) => Function }} options++ * @param {{ toolDefs: any[], defineTool: Function, keyOf: (exec: any) => Promise<string|undefined>, profileRoot: string, downloadRoot?: string, downloadRoots?: (botId: string) => Promise<string[]>, uploadRoots?: (botId: string) => Promise<string[]>, headless: boolean, playwrightUrl?: string, executablePath?: string, actionTimeout?: number, authorize?: (exec: any, botId: string, action: any) => Promise<void>, completeFor?: (exec: any) => Function, connect?: (botId: string, downloadRoots?: string[]) => Promise<string>, getHold?: (botId: string) => any, onHold?: (botId: string, hold: any) => void, subscribeHolds?: (listener: (botId: string, hold: any) => void) => (() => void) | undefined, openSession?: typeof openBotBrowser, api?: object }} options*/- export function mountBrowserPlugin(ctx, { toolDefs, defineTool, keyOf, profileRoot, downloadRoot, downloadRoots, uploadRoots, headless, playwrightUrl, executablePath, actionTimeout, authorize, completeFor, connect, getHold, onHold, subscribeHolds, openSession = openBotBrowser }) {-export function mountBrowserPlugin(ctx, { toolDefs, defineTool, keyOf, profileRoot, downloadRoot, uploadRoots, headless, playwrightUrl, executablePath, actionTimeout, authorize, completeFor }) {++export function mountBrowserPlugin(ctx, { toolDefs, defineTool, keyOf, profileRoot, downloadRoot, downloadRoots, uploadRoots, headless, playwrightUrl, executablePath, actionTimeout, authorize, completeFor, connect, getHold, onHold, subscribeHolds, openSession = openBotBrowser, api }) {const sessions = new Map()+ const unsubscribeHolds = subscribeHolds?.((botId, hold) => {+ if (hold?.by !== 'user') return+ const session = sessions.get(botId)+ if (!session) return+ sessions.delete(botId)- void session.close()++ void Promise.resolve(session).then(opened => opened.close(), () => {})+ })ctx.effect(() => async () => {+ unsubscribeHolds?.()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,@@@ -57,27 -46,26 +66,35 @@@async execute(args, exec) {const botId = await keyOf(exec)if (!botId) return 'Only Bots on the team have a browser.'+ if (getHold?.(botId)?.by === 'user') throw new Error('The user has taken over this browser. Wait for them to give it back.')let session = sessions.get(botId)if (spec.needsBrowser && session === undefined) {- const roots = downloadRoots ? await downloadRoots(botId) : undefined- session = await openSession({- botId,- profileRoot,- downloadDir: roots === undefined- ? join(downloadRoot ?? join(profileRoot, 'browser-downloads'), botId)- : async () => (await downloadRoots(botId))[0],- headless,- playwrightUrl,- executablePath,- actionTimeout,- connect: connect ? id => connect(id, roots ?? []) : undefined,- getHold: () => getHold?.(botId),- onHold: hold => onHold?.(botId, hold),- uploadRoots: uploadRoots ? () => uploadRoots(botId) : undefined,+ // 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),- headless,- playwrightUrl,- executablePath,- actionTimeout,- uploadRoots: uploadRoots ? () => uploadRoots(botId) : undefined,- })++ const opening = (async () => {++ const roots = downloadRoots ? await downloadRoots(botId) : undefined++ return openSession({++ botId,++ profileRoot,++ downloadDir: roots === undefined++ ? join(downloadRoot ?? join(profileRoot, 'browser-downloads'), botId)++ : async () => (await downloadRoots(botId))[0],++ headless,++ playwrightUrl,++ executablePath,++ actionTimeout,++ connect: connect ? id => connect(id, roots ?? []) : undefined,++ getHold: () => getHold?.(botId),++ onHold: hold => onHold?.(botId, hold),++ uploadRoots: uploadRoots ? () => uploadRoots(botId) : undefined,++ })++ })()+ sessions.set(botId, opening)+ opening.catch(() => {+ if (sessions.get(botId) === opening) sessions.delete(botId)})- sessions.set(botId, session)+ session = opening}+ session = await sessionconst gate = authorize ? action => authorize(exec, botId, action) : undefinedreturn gateScope.run(gate, () => spec.execute(args ?? {}, {botId,@@@ -88,4 -76,6 +105,7 @@@},}))}+- return { drop }++ // An object return would be collected as an effect by cordis, so the api is an out-param.++ if (api) api.drop = drop}