Class Chrome

All Implemented Interfaces:
ContextProvider, PropertyChangeSource, Rebindable

public class Chrome extends AbstractBrowser
A toolkit for web automation and form filling using Chrome and Selenium.

This toolkit manages a fleet of BrowserDrone instances, supporting both headless execution and live connection to the user's active Chrome profiles.

Author:
anahata
  • 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 ToolManager after 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:
      initialize in class AnahataToolkit
    • getSystemInstructions

      public List<String> 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

      public void populateMessage(RagMessage ragMessage)
      Populates the given RagMessage with 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 requested dataDir. 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

      public String killAll()
      Closes all active browser processes.
      Specified by:
      killAll in class AbstractBrowser
      Returns:
      A status message describing the cleanup operations.
    • listTabs

      public List<String> listTabs(String droneId)
      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.getTargetInfo command to retrieve titles and URLs without forcing window focus. Falls back to standard switchTo().window() if CDP fails.

      Specified by:
      listTabs in class AbstractBrowser
      Parameters:
      droneId - The ID of the drone to inspect.
      Returns:
      A list of strings describing each open tab.
    • getDriver

      public org.openqa.selenium.WebDriver getDriver(String droneId)
      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:
      getDriver in class AbstractBrowser
      Parameters:
      droneId - The unique ID of the drone.
      Returns:
      The active WebDriver, or null if 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 initialize
      options - the ChromeOptions to apply
      environment - custom environment variables for the driver service
    • getProcessReport

      private String getProcessReport()
      Generates a human-readable markdown report of all active Chrome processes on the host system.

      Uses ProcessHandle and OS-specific command line extraction (via /proc on Linux or wmic on Windows) to identify ports and data directories.

      Returns:
      A markdown string containing process PIDs, modes, and command lines.
    • getCommandLine

      private String getCommandLine(ProcessHandle p)
      Attempts to resolve the full command line of a process.

      On Linux, this reads directly from /proc/[pid]/cmdline for 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

      private String extractArg(String cmdLine, String argName)
      Extracts an argument value from the command line string.
      Parameters:
      cmdLine - the full command line of the process
      argName - the argument flag to search for
      Returns:
      the argument value, or null if not found
    • detectPortFromFiles

      private String detectPortFromFiles(String userDataDir, String profileDir)
      Detects the remote debugging port from active profile files.
      Parameters:
      userDataDir - the Chrome user data directory
      profileDir - the specific profile directory
      Returns:
      the port string, or null if undetected
    • detectActiveProfile

      private String detectActiveProfile(String userDataDir)
      Detects which profile is currently active in the user data directory.
      Parameters:
      userDataDir - the Chrome user data directory
      Returns:
      the profile directory name
    • hasLock

      private boolean hasLock(File dir)
      Checks if a directory contains any active lock files.
      Parameters:
      dir - the directory to check
      Returns:
      true if a lock is detected, false otherwise
    • killAllInternal

      private String killAllInternal()
      Internal implementation to kill all running Chrome and ChromeDriver processes.
      Returns:
      status message describing the kill count
    • resetExitStateInternal

      private String resetExitStateInternal(String userDataDir, String profileDir)
      Internal implementation to reset Chrome's exit state.
      Parameters:
      userDataDir - the Chrome user data directory
      profileDir - the specific profile directory
      Returns:
      status message describing the reset
    • clearSingletonLockInternal

      private String clearSingletonLockInternal(String userDataDir)
      Internal implementation to clear the Chrome Singleton Lock files.
      Parameters:
      userDataDir - the user data directory
      Returns:
      status message describing the operation
    • launchProfileChromeInternal

      private String launchProfileChromeInternal(BrowserDrone d, String initialUrl)
      Internal implementation to launch Chrome with a specific profile.
      Parameters:
      d - the browser drone
      initialUrl - optional initial URL to navigate to
      Returns:
      status message describing the launch
    • connectToExistingInternal

      private String connectToExistingInternal(BrowserDrone d, int port)
      Internal implementation to connect to an existing Chrome instance on a port.
      Parameters:
      d - the browser drone
      port - the remote debugging port
      Returns:
      status message of the connection