1515Example:
1616 ```python
1717 from .cdp_context import require_cdp_client
18-
18+
1919 @require_cdp_client
2020 async def my_tool_function(cdp_client):
2121 # CDP client is guaranteed to be connected
@@ -39,21 +39,21 @@ async def my_tool_function(cdp_client):
3939def require_cdp_client (func : F ) -> F :
4040 """
4141 Decorator that provides CDP client to tool functions with automatic validation.
42-
42+
4343 This decorator eliminates the need for repetitive client access and connection
4444 checking in every tool function. It automatically:
45-
45+
4646 1. Imports the CDP client from the main module
4747 2. Validates that the client exists and is connected
4848 3. Passes the validated client as the first parameter to the decorated function
4949 4. Returns appropriate error responses if client is unavailable
50-
50+
5151 Args:
5252 func: The async function to decorate. Must accept cdp_client as first parameter.
53-
53+
5454 Returns:
5555 The decorated function with automatic CDP client injection.
56-
56+
5757 Example:
5858 ```python
5959 @require_cdp_client
@@ -65,47 +65,47 @@ async def get_page_title(cdp_client, **kwargs):
6565 return {"title": result["result"]["value"]}
6666 ```
6767 """
68-
68+
6969 @wraps (func )
7070 async def wrapper (* args : Any , ** kwargs : Any ) -> Any :
7171 try :
7272 # Import CDP client dynamically to avoid circular imports
7373 from . import main
74-
74+
7575 cdp_client = main .cdp_client
76-
76+
7777 # Validate client availability and connection status
7878 if not cdp_client :
7979 return create_error_response (
8080 "CDP client not initialised. Please start Chrome first."
8181 )
82-
82+
8383 if not cdp_client .connected :
8484 return create_error_response (
8585 "Not connected to browser. Please connect to Chrome first."
8686 )
87-
87+
8888 # Call the original function with CDP client as first argument
8989 return await func (cdp_client , * args , ** kwargs )
90-
90+
9191 except ImportError :
9292 return create_error_response (
9393 "CDP client module not available. Please check server configuration."
9494 )
9595 except Exception as e :
9696 return create_error_response (f"CDP context error: { str (e )} " )
97-
97+
9898 return wrapper
9999
100100
101101class CDPContext :
102102 """
103103 Context manager for Chrome DevTools Protocol operations.
104-
104+
105105 Provides a more explicit context-based approach for operations that require
106106 multiple CDP interactions. This is useful for complex operations that need
107107 to ensure the connection remains stable throughout the operation.
108-
108+
109109 Example:
110110 ```python
111111 async with CDPContext() as cdp:
@@ -114,41 +114,41 @@ class CDPContext:
114114 result = await cdp.send_command("DOM.getDocument")
115115 ```
116116 """
117-
117+
118118 def __init__ (self ) -> None :
119119 """Initialise the CDP context manager."""
120120 self .cdp_client = None
121-
121+
122122 async def __aenter__ (self ):
123123 """
124124 Enter the async context and validate CDP client.
125-
125+
126126 Returns:
127127 The validated CDP client instance.
128-
128+
129129 Raises:
130130 RuntimeError: If CDP client is not available or not connected.
131131 """
132132 try :
133133 from . import main
134-
134+
135135 self .cdp_client = main .cdp_client
136-
136+
137137 if not self .cdp_client :
138138 raise RuntimeError ("CDP client not initialised. Please start Chrome first." )
139-
139+
140140 if not self .cdp_client .connected :
141141 raise RuntimeError ("Not connected to browser. Please connect to Chrome first." )
142-
142+
143143 return self .cdp_client
144-
144+
145145 except ImportError as e :
146146 raise RuntimeError ("CDP client module not available." ) from e
147-
147+
148148 async def __aexit__ (self , exc_type , exc_val , exc_tb ):
149149 """
150150 Exit the async context.
151-
151+
152152 Currently performs no cleanup, but provides a hook for future
153153 connection management improvements.
154154 """
@@ -158,14 +158,14 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
158158def get_cdp_client ():
159159 """
160160 Get the current CDP client instance without validation.
161-
161+
162162 This function provides direct access to the CDP client for cases where
163163 you need to check its status or perform conditional operations based on
164164 availability.
165-
165+
166166 Returns:
167167 ChromeDevToolsClient | None: The CDP client instance or None if not available.
168-
168+
169169 Example:
170170 ```python
171171 cdp = get_cdp_client()
@@ -178,4 +178,4 @@ def get_cdp_client():
178178 from . import main
179179 return main .cdp_client
180180 except ImportError :
181- return None
181+ return None
0 commit comments