mirror of
https://github.com/browseros-ai/BrowserOS.git
synced 2026-05-19 11:31:03 +00:00
* clean-up bunch of files for re-write * more clean-up and adding basic agent * Minor fix moved types into respective files. * Deleted bunch of old files backup Update gitignore Deleted a bunch of files Remove message manager Deleted old docs Update rules rename Profiler to profiler * Temporarily adding old code * Adding two small things back * backup * Implemented LangChainProvider and updated cursor rules backup LangChainProvider curosr rules * Implement tests for LangChainProvider -- unit test and integration test integration test passes integration test backup * Tool Design Tools Desing tools design * NavigationTool ready NavigationTool ready NavigationTool ready NaivgationTool ready backup * MessageManager MessageManager backup * Fixed integration test * Agent design new Updated agent design and added bunch of /NTN commands agent new design * Delete old agent design * MessageManagerReadOnly class * PlannerTool ready PlannerTool almost ready * ToolManager and DoneTool * Integration of BrowserAgent * BrowserAgent implementation v0.1 * BrowserAgent small fix v0.2 * Tool calling design too call design tool design claude * Update agent tool design with // NTN * add zod-to-json npm install * BrowserAGent v0.3 * BrowserAgent v0.4 * BrowserAgent v0.5 * fixes * Build error fixes in my NEWLY added code build errors fix * Build error fixes in old code (integration work) backup * Comment StreamEventProcessor for now, it is not used * Small build error fix * Small rename * Added integration test to check structuredLLM and changed to 4o-mini change default to nxtscape integration test * Small docstring * Simplified BrowserAgent code and added integration test Simplified BrowserAgent code BrowserAGent integrationt est * Update CLAUDE.md with project memory and instructions on how to write code Update CLAUDE.md with project memory and instructions on how to write code Project Memory * Just a mova.. Moved ToolManager outside. Build works. * TabOperations tool TabOperations Tool and fixing some test tab operations * Update CLAUDE.md * Added ClassificationTool classifiction tool classification prommpt * Refactored and simplified PlannerTool unit test and integration test * Updated Plnnaer tool * Update CLAUDE.md * BrowserAgent modified to do classification BrowserAgent with classification * minor fix to ToolManager * Instead of ToolCall and ToolResult -- just updating message manager once * minor fix to BrowserAgent integration test * Changed done to "done_tool" * Updated CLAUDE.md to reflect understanding of claude * Uncommented stream event processor * Renamed EventBus to StreamEventBus * Commented StreamEventProcessor * Event Processor * Integrated EventProcessor with BrowserAgent Added EventProcessor to BrowserAgetn * Renamed StreamEventBus to EventBus * Made EventBus required parameter in ExecutionContext * PlanGenerator rewrite PlanGenerator rewrite backup * For simple task, explicitly tell it to call done tool * Max attempts for simple task * backup * Revert "backup" This reverts commit 7d79a3d4d5774bfef79ec9827878b74edad3593f. * Consolidating where EventBus and EventProcessor are created and initialized backup * Update CLAUDE.md Update CLAUDE.md * Improving agent loop code Cleaned up processTooCall classification task * Create test-writer subAgent test-agent-prompt test agent prompt test-agent-prompt Update test-writer.md * BrowserAgent test Browseragent test BrowserAgent test * BrowserAgent refactor backup backup * Minor fixes * Minor fix * minor change -- NEW AGENT LOOP IS WORKING WELL * Update cursor rules * Small change * Improved BrowserAgent integration test Improved BrowserAgent integration test * Small change * Update CLAUDE.md * Different tools * FindElementTool is ready Find element update backup find element backup * Updated to test strings to say "tests..." * ScrollTool is ready * RefreshStateTool is updated as well * MessageManager updated * SearchTool is ready backup * Interaction Element is also ready * Add debugMessage emitter * ValidatorTool ready and tests are passing Validation Tool validator tool backup backup * GroupTabs tool ready * Registered all the tools * Planning changed to 5 steps * BrowserAgent integration test fix * Minor string changes * backup * Removed too many confusing events in EventProcessor -- there is only event.info right now * Abort control implemented backup Abort * Formatter for toolResult Formatter for toolResult backup * Always render using Markdown * Minor fix --------- Co-authored-by: Nikhil Sonti <nikhilsv92@gmail.com>
130 lines
3.9 KiB
TypeScript
130 lines
3.9 KiB
TypeScript
import { z } from 'zod'
|
|
import BrowserContext from '../browser/BrowserContext'
|
|
import MessageManager from '@/lib/runtime/MessageManager'
|
|
import { StreamEventBus } from '@/lib/events'
|
|
|
|
/**
|
|
* Configuration options for ExecutionContext
|
|
*/
|
|
export const ExecutionContextOptionsSchema = z.object({
|
|
browserContext: z.instanceof(BrowserContext), // Browser context for page operations
|
|
messageManager: z.instanceof(MessageManager), // Message manager for communication
|
|
abortController: z.instanceof(AbortController), // Abort controller for task cancellation
|
|
debugMode: z.boolean().default(false), // Whether to enable debug logging
|
|
eventBus: z.instanceof(StreamEventBus).optional() // Event bus for streaming updates
|
|
})
|
|
|
|
export type ExecutionContextOptions = z.infer<typeof ExecutionContextOptionsSchema>
|
|
|
|
/**
|
|
* Agent execution context containing browser context, message manager, and control state
|
|
*/
|
|
export class ExecutionContext {
|
|
abortController: AbortController // Abort controller for task cancellation
|
|
browserContext: BrowserContext // Browser context for page operations
|
|
messageManager: MessageManager // Message manager for communication
|
|
debugMode: boolean // Whether debug logging is enabled
|
|
eventBus: StreamEventBus | null // Event bus for streaming updates
|
|
selectedTabIds: number[] | null = null // Selected tab IDs
|
|
private userInitiatedCancel: boolean = false // Track if cancellation was user-initiated
|
|
private _isExecuting: boolean = false // Track actual execution state
|
|
private _lockedTabId: number | null = null // Tab that execution is locked to
|
|
|
|
constructor(options: ExecutionContextOptions) {
|
|
this.abortController = options.abortController
|
|
this.browserContext = options.browserContext
|
|
this.messageManager = options.messageManager
|
|
this.debugMode = options.debugMode
|
|
this.eventBus = options.eventBus || null
|
|
this.userInitiatedCancel = false
|
|
}
|
|
|
|
public setSelectedTabIds(tabIds: number[]): void {
|
|
this.selectedTabIds = tabIds;
|
|
}
|
|
|
|
public getSelectedTabIds(): number[] | null {
|
|
return this.selectedTabIds;
|
|
}
|
|
|
|
/**
|
|
* Set the event bus for streaming updates
|
|
* @param eventBus - The event bus to use
|
|
*/
|
|
public setEventBus(eventBus: StreamEventBus): void {
|
|
this.eventBus = eventBus;
|
|
}
|
|
|
|
/**
|
|
* Get the current event bus
|
|
* @returns The event bus or null if not set
|
|
*/
|
|
public getEventBus(): StreamEventBus | null {
|
|
return this.eventBus;
|
|
}
|
|
|
|
/**
|
|
* Cancel execution with user-initiated flag
|
|
* @param isUserInitiated - Whether the cancellation was initiated by the user
|
|
*/
|
|
public cancelExecution(isUserInitiated: boolean = false): void {
|
|
this.userInitiatedCancel = isUserInitiated;
|
|
this.abortController.abort();
|
|
}
|
|
|
|
/**
|
|
* Check if the current cancellation was user-initiated
|
|
*/
|
|
public isUserCancellation(): boolean {
|
|
return this.userInitiatedCancel && this.abortController.signal.aborted;
|
|
}
|
|
|
|
/**
|
|
* Reset abort controller for new task execution
|
|
*/
|
|
public resetAbortController(): void {
|
|
this.userInitiatedCancel = false;
|
|
this.abortController = new AbortController();
|
|
}
|
|
|
|
/**
|
|
* Mark execution as started and lock to a specific tab
|
|
* @param tabId - The tab ID to lock execution to
|
|
*/
|
|
public startExecution(tabId: number): void {
|
|
this._isExecuting = true;
|
|
this._lockedTabId = tabId;
|
|
}
|
|
|
|
/**
|
|
* Mark execution as ended
|
|
*/
|
|
public endExecution(): void {
|
|
this._isExecuting = false;
|
|
// Keep lockedTabId until reset() for debugging purposes
|
|
}
|
|
|
|
/**
|
|
* Check if currently executing
|
|
*/
|
|
public isExecuting(): boolean {
|
|
return this._isExecuting;
|
|
}
|
|
|
|
/**
|
|
* Get the tab ID that execution is locked to
|
|
*/
|
|
public getLockedTabId(): number | null {
|
|
return this._lockedTabId;
|
|
}
|
|
|
|
/**
|
|
* Reset execution state
|
|
*/
|
|
public reset(): void {
|
|
this._isExecuting = false;
|
|
this._lockedTabId = null;
|
|
this.userInitiatedCancel = false;
|
|
}
|
|
}
|
|
|