Class Chrome
- All Implemented Interfaces:
ContextProvider, PropertyChangeSource, Rebindable
This toolkit manages a fleet of BrowserDrone instances, supporting
both headless execution and live connection to the user's active Chrome profiles.
- Author:
- anahata
-
Field Summary
Fields inherited from class AbstractBrowser
dronesFields inherited from class AnahataToolkit
childrenProviders, propertyChangeSupportFields inherited from class ToolContext
toolkit -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate StringclearSingletonLockInternal(String userDataDir) Internal implementation to clear the Chrome Singleton Lock files.Scans the OS process table to find running Chrome instances bound to the requesteddataDir.private StringconnectToExistingInternal(BrowserDrone d, int port) Internal implementation to connect to an existing Chrome instance on a port.private StringdetectActiveProfile(String userDataDir) Detects which profile is currently active in the user data directory.private StringdetectPortFromFiles(String userDataDir, String profileDir) Detects the remote debugging port from active profile files.private StringextractArg(String cmdLine, String argName) Extracts an argument value from the command line string.private StringAttempts to resolve the full command line of a process.org.openqa.selenium.WebDriverResolves the active WebDriver for the requested drone.private StringGenerates a human-readable markdown report of all active Chrome processes on the host system.Gets a list of system instruction strings provided by this context provider.private booleanChecks if a directory contains any active lock files.private voidinitDriver(BrowserDrone d, org.openqa.selenium.chrome.ChromeOptions options, Map<String, String> environment) Initializes the ChromeDriver instance with specified options and environment.voidPerforms initial setup for a newly created toolkit instance.killAll()Closes all active browser processes.private StringInternal implementation to kill all running Chrome and ChromeDriver processes.private StringlaunchProfileChromeInternal(BrowserDrone d, String initialUrl) Internal implementation to launch Chrome with a specific profile.Lists all open tabs/windows in the specified drone.voidpopulateMessage(RagMessage ragMessage) Populates the givenRagMessagewith dynamic, just-in-time context parts.private StringresetExitStateInternal(String userDataDir, String profileDir) Internal implementation to reset Chrome's exit state.Methods inherited from class AbstractBrowser
clickElement, close, executeScript, fillForm, getPageSource, getPageText, getScreenshot, getStatus, goBack, goForward, inspectForm, locateElementGracefully, navigate, openNewTab, refresh, scrollToElement, switchToTab, waitForElementMethods inherited from class AnahataToolkit
getChildrenProviders, getDescription, getId, getName, getParentProvider, getPropertyChangeSupport, isEffectivelyProviding, postActivate, rebindMethods inherited from class ToolContext
addAttachment, addAttachment, addAttachment, error, error, getAgi, getApplicationMap, getAsiContainer, getAsiContainerMap, getCall, getExecutorService, getModelId, getModelMessage, getResourceManager, getResponse, getSessionMap, getThreadSafeLogger, getTool, getToolContext, getToolkit, getToolManager, getTurnMap, isToolExecution, log, peekResponse, runAsyncMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface ContextProvider
getAgi, getContextPosition, getFlattenedHierarchy, getFullyQualifiedId, getHeader, getIconId, getInstructionsTokenCount, getRagTokenCount, isProviding, setParentProvider, setProvidingMethods inherited from interface Rebindable
rebind
-
Constructor Details
-
Chrome
public Chrome()Public constructor to instantiate the Chrome automation toolkit.
-
-
Method Details
-
initialize
public void initialize()Performs initial setup for a newly created toolkit instance.This method is called exactly once by the
ToolManagerafter the toolkit has been instantiated and registered. It provides a clean hook for toolkits to perform one-time initialization logic that requires the tool environment to be fully configured, such as scanning for project instructions or registering default resources.Implementation details: Disables the toolkit by default to prevent accidental drone spawns in environments without local browsers.
- Overrides:
initializein classAnahataToolkit
-
getSystemInstructions
Gets a list of system instruction strings provided by this context provider. These are typically prepended to the conversation as high-level guidance.Provides context-specific instructions to the ASI regarding Chrome automation.
- Returns:
- A list of system instruction strings.
-
populateMessage
Populates the givenRagMessagewith dynamic, just-in-time context parts. These parts are appended to the end of the user's prompt (RAG).Populates the RAG message with current Chrome environment state.
- Parameters:
ragMessage- The message to be augmented with context.
-
connect
public String connect(String droneId, String profile, Boolean headless, String dataDir, String binaryPath) throws AgiToolException Scans the OS process table to find running Chrome instances bound to the requesteddataDir. If a matching process is found in debug mode, it attaches to the existing port; otherwise, it launches a fresh instance with a dynamic debug port.- Parameters:
droneId- A unique ID for this drone.profile- An optional profile name (e.g., 'Default', 'Profile 1').headless- Whether to launch in an invisible headless mode.dataDir- The path to the user data directory.binaryPath- Optional absolute path to the chrome executable.- Returns:
- A status message confirming the connection.
- Throws:
AgiToolException- If the droneId is already in use.
-
killAll
Closes all active browser processes.- Specified by:
killAllin classAbstractBrowser- Returns:
- A status message describing the cleanup operations.
-
listTabs
Lists all open tabs/windows in the specified drone.Implementations should ideally use native DevTools protocols to avoid forcing window focus and causing screen flickering.
Implementation details: Iterates over all window handles and uses the Chrome DevTools Protocol (CDP)
Target.getTargetInfocommand to retrieve titles and URLs without forcing window focus. Falls back to standardswitchTo().window()if CDP fails.- Specified by:
listTabsin classAbstractBrowser- Parameters:
droneId- The ID of the drone to inspect.- Returns:
- A list of strings describing each open tab.
-
getDriver
Resolves the active WebDriver for the requested drone.Implementation details: This is an internal lifecycle method. If the connection is lost, concrete implementations should attempt to re-establish it using the cached port and profile data.
- Specified by:
getDriverin classAbstractBrowser- Parameters:
droneId- The unique ID of the drone.- Returns:
- The active WebDriver, or
nullif it cannot be resolved.
-
initDriver
private void initDriver(BrowserDrone d, org.openqa.selenium.chrome.ChromeOptions options, Map<String, String> environment) Initializes the ChromeDriver instance with specified options and environment.- Parameters:
d- the browser drone to initializeoptions- the ChromeOptions to applyenvironment- custom environment variables for the driver service
-
getProcessReport
Generates a human-readable markdown report of all active Chrome processes on the host system.Uses
ProcessHandleand OS-specific command line extraction (via/procon Linux orwmicon Windows) to identify ports and data directories.- Returns:
- A markdown string containing process PIDs, modes, and command lines.
-
getCommandLine
Attempts to resolve the full command line of a process.On Linux, this reads directly from
/proc/[pid]/cmdlinefor maximum fidelity. On other platforms or as a fallback, it uses Shell-based queries.- Parameters:
p- The process handle to inspect.- Returns:
- The full command line string.
-
extractArg
-
detectPortFromFiles
-
detectActiveProfile
-
hasLock
Checks if a directory contains any active lock files.- Parameters:
dir- the directory to check- Returns:
trueif a lock is detected,falseotherwise
-
killAllInternal
Internal implementation to kill all running Chrome and ChromeDriver processes.- Returns:
- status message describing the kill count
-
resetExitStateInternal
-
clearSingletonLockInternal
-
launchProfileChromeInternal
Internal implementation to launch Chrome with a specific profile.- Parameters:
d- the browser droneinitialUrl- optional initial URL to navigate to- Returns:
- status message describing the launch
-
connectToExistingInternal
Internal implementation to connect to an existing Chrome instance on a port.- Parameters:
d- the browser droneport- the remote debugging port- Returns:
- status message of the connection
-

