15
15
Example:
16
16
```python
17
17
from .cdp_context import require_cdp_client
18
-
18
+
19
19
@require_cdp_client
20
20
async def my_tool_function(cdp_client):
21
21
# CDP client is guaranteed to be connected
@@ -39,21 +39,21 @@ async def my_tool_function(cdp_client):
39
39
def require_cdp_client (func : F ) -> F :
40
40
"""
41
41
Decorator that provides CDP client to tool functions with automatic validation.
42
-
42
+
43
43
This decorator eliminates the need for repetitive client access and connection
44
44
checking in every tool function. It automatically:
45
-
45
+
46
46
1. Imports the CDP client from the main module
47
47
2. Validates that the client exists and is connected
48
48
3. Passes the validated client as the first parameter to the decorated function
49
49
4. Returns appropriate error responses if client is unavailable
50
-
50
+
51
51
Args:
52
52
func: The async function to decorate. Must accept cdp_client as first parameter.
53
-
53
+
54
54
Returns:
55
55
The decorated function with automatic CDP client injection.
56
-
56
+
57
57
Example:
58
58
```python
59
59
@require_cdp_client
@@ -65,47 +65,47 @@ async def get_page_title(cdp_client, **kwargs):
65
65
return {"title": result["result"]["value"]}
66
66
```
67
67
"""
68
-
68
+
69
69
@wraps (func )
70
70
async def wrapper (* args : Any , ** kwargs : Any ) -> Any :
71
71
try :
72
72
# Import CDP client dynamically to avoid circular imports
73
73
from . import main
74
-
74
+
75
75
cdp_client = main .cdp_client
76
-
76
+
77
77
# Validate client availability and connection status
78
78
if not cdp_client :
79
79
return create_error_response (
80
80
"CDP client not initialised. Please start Chrome first."
81
81
)
82
-
82
+
83
83
if not cdp_client .connected :
84
84
return create_error_response (
85
85
"Not connected to browser. Please connect to Chrome first."
86
86
)
87
-
87
+
88
88
# Call the original function with CDP client as first argument
89
89
return await func (cdp_client , * args , ** kwargs )
90
-
90
+
91
91
except ImportError :
92
92
return create_error_response (
93
93
"CDP client module not available. Please check server configuration."
94
94
)
95
95
except Exception as e :
96
96
return create_error_response (f"CDP context error: { str (e )} " )
97
-
97
+
98
98
return wrapper
99
99
100
100
101
101
class CDPContext :
102
102
"""
103
103
Context manager for Chrome DevTools Protocol operations.
104
-
104
+
105
105
Provides a more explicit context-based approach for operations that require
106
106
multiple CDP interactions. This is useful for complex operations that need
107
107
to ensure the connection remains stable throughout the operation.
108
-
108
+
109
109
Example:
110
110
```python
111
111
async with CDPContext() as cdp:
@@ -114,41 +114,41 @@ class CDPContext:
114
114
result = await cdp.send_command("DOM.getDocument")
115
115
```
116
116
"""
117
-
117
+
118
118
def __init__ (self ) -> None :
119
119
"""Initialise the CDP context manager."""
120
120
self .cdp_client = None
121
-
121
+
122
122
async def __aenter__ (self ):
123
123
"""
124
124
Enter the async context and validate CDP client.
125
-
125
+
126
126
Returns:
127
127
The validated CDP client instance.
128
-
128
+
129
129
Raises:
130
130
RuntimeError: If CDP client is not available or not connected.
131
131
"""
132
132
try :
133
133
from . import main
134
-
134
+
135
135
self .cdp_client = main .cdp_client
136
-
136
+
137
137
if not self .cdp_client :
138
138
raise RuntimeError ("CDP client not initialised. Please start Chrome first." )
139
-
139
+
140
140
if not self .cdp_client .connected :
141
141
raise RuntimeError ("Not connected to browser. Please connect to Chrome first." )
142
-
142
+
143
143
return self .cdp_client
144
-
144
+
145
145
except ImportError as e :
146
146
raise RuntimeError ("CDP client module not available." ) from e
147
-
147
+
148
148
async def __aexit__ (self , exc_type , exc_val , exc_tb ):
149
149
"""
150
150
Exit the async context.
151
-
151
+
152
152
Currently performs no cleanup, but provides a hook for future
153
153
connection management improvements.
154
154
"""
@@ -158,14 +158,14 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
158
158
def get_cdp_client ():
159
159
"""
160
160
Get the current CDP client instance without validation.
161
-
161
+
162
162
This function provides direct access to the CDP client for cases where
163
163
you need to check its status or perform conditional operations based on
164
164
availability.
165
-
165
+
166
166
Returns:
167
167
ChromeDevToolsClient | None: The CDP client instance or None if not available.
168
-
168
+
169
169
Example:
170
170
```python
171
171
cdp = get_cdp_client()
@@ -178,4 +178,4 @@ def get_cdp_client():
178
178
from . import main
179
179
return main .cdp_client
180
180
except ImportError :
181
- return None
181
+ return None
0 commit comments