cua-agent 0.4.4__tar.gz → 0.4.6__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of cua-agent might be problematic. Click here for more details.
- {cua_agent-0.4.4 → cua_agent-0.4.6}/PKG-INFO +1 -1
- cua_agent-0.4.6/agent/callbacks/pii_anonymization.py +96 -0
- {cua_agent-0.4.4 → cua_agent-0.4.6}/agent/loops/anthropic.py +2 -2
- {cua_agent-0.4.4 → cua_agent-0.4.6}/pyproject.toml +1 -1
- cua_agent-0.4.4/agent/callbacks/pii_anonymization.py +0 -259
- {cua_agent-0.4.4 → cua_agent-0.4.6}/README.md +0 -0
- {cua_agent-0.4.4 → cua_agent-0.4.6}/agent/__init__.py +0 -0
- {cua_agent-0.4.4 → cua_agent-0.4.6}/agent/__main__.py +0 -0
- {cua_agent-0.4.4 → cua_agent-0.4.6}/agent/adapters/__init__.py +0 -0
- {cua_agent-0.4.4 → cua_agent-0.4.6}/agent/adapters/huggingfacelocal_adapter.py +0 -0
- {cua_agent-0.4.4 → cua_agent-0.4.6}/agent/agent.py +0 -0
- {cua_agent-0.4.4 → cua_agent-0.4.6}/agent/callbacks/__init__.py +0 -0
- {cua_agent-0.4.4 → cua_agent-0.4.6}/agent/callbacks/base.py +0 -0
- {cua_agent-0.4.4 → cua_agent-0.4.6}/agent/callbacks/budget_manager.py +0 -0
- {cua_agent-0.4.4 → cua_agent-0.4.6}/agent/callbacks/image_retention.py +0 -0
- {cua_agent-0.4.4 → cua_agent-0.4.6}/agent/callbacks/logging.py +0 -0
- {cua_agent-0.4.4 → cua_agent-0.4.6}/agent/callbacks/telemetry.py +0 -0
- {cua_agent-0.4.4 → cua_agent-0.4.6}/agent/callbacks/trajectory_saver.py +0 -0
- {cua_agent-0.4.4 → cua_agent-0.4.6}/agent/cli.py +0 -0
- {cua_agent-0.4.4 → cua_agent-0.4.6}/agent/computer_handler.py +0 -0
- {cua_agent-0.4.4 → cua_agent-0.4.6}/agent/decorators.py +0 -0
- {cua_agent-0.4.4 → cua_agent-0.4.6}/agent/loops/__init__.py +0 -0
- {cua_agent-0.4.4 → cua_agent-0.4.6}/agent/loops/omniparser.py +0 -0
- {cua_agent-0.4.4 → cua_agent-0.4.6}/agent/loops/openai.py +0 -0
- {cua_agent-0.4.4 → cua_agent-0.4.6}/agent/loops/uitars.py +0 -0
- {cua_agent-0.4.4 → cua_agent-0.4.6}/agent/responses.py +0 -0
- {cua_agent-0.4.4 → cua_agent-0.4.6}/agent/telemetry.py +0 -0
- {cua_agent-0.4.4 → cua_agent-0.4.6}/agent/types.py +0 -0
- {cua_agent-0.4.4 → cua_agent-0.4.6}/agent/ui/__init__.py +0 -0
- {cua_agent-0.4.4 → cua_agent-0.4.6}/agent/ui/__main__.py +0 -0
- {cua_agent-0.4.4 → cua_agent-0.4.6}/agent/ui/gradio/__init__.py +0 -0
- {cua_agent-0.4.4 → cua_agent-0.4.6}/agent/ui/gradio/app.py +0 -0
- {cua_agent-0.4.4 → cua_agent-0.4.6}/agent/ui/gradio/ui_components.py +0 -0
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"""
|
|
2
|
+
PII anonymization callback handler using Microsoft Presidio for text and image redaction.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from typing import List, Dict, Any, Optional, Tuple
|
|
6
|
+
from .base import AsyncCallbackHandler
|
|
7
|
+
import base64
|
|
8
|
+
import io
|
|
9
|
+
import logging
|
|
10
|
+
|
|
11
|
+
try:
|
|
12
|
+
# TODO: Add Presidio dependencies
|
|
13
|
+
from PIL import Image
|
|
14
|
+
PRESIDIO_AVAILABLE = True
|
|
15
|
+
except ImportError:
|
|
16
|
+
PRESIDIO_AVAILABLE = False
|
|
17
|
+
|
|
18
|
+
logger = logging.getLogger(__name__)
|
|
19
|
+
|
|
20
|
+
class PIIAnonymizationCallback(AsyncCallbackHandler):
|
|
21
|
+
"""
|
|
22
|
+
Callback handler that anonymizes PII in text and images using Microsoft Presidio.
|
|
23
|
+
|
|
24
|
+
This handler:
|
|
25
|
+
1. Anonymizes PII in messages before sending to the agent loop
|
|
26
|
+
2. Deanonymizes PII in tool calls and message outputs after the agent loop
|
|
27
|
+
3. Redacts PII from images in computer_call_output messages
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
def __init__(
|
|
31
|
+
self,
|
|
32
|
+
# TODO: Any extra kwargs if needed
|
|
33
|
+
):
|
|
34
|
+
"""
|
|
35
|
+
Initialize the PII anonymization callback.
|
|
36
|
+
|
|
37
|
+
Args:
|
|
38
|
+
anonymize_text: Whether to anonymize text content
|
|
39
|
+
anonymize_images: Whether to redact images
|
|
40
|
+
entities_to_anonymize: List of entity types to anonymize (None for all)
|
|
41
|
+
anonymization_operator: Presidio operator to use ("replace", "mask", "redact", etc.)
|
|
42
|
+
image_redaction_color: RGB color for image redaction
|
|
43
|
+
"""
|
|
44
|
+
if not PRESIDIO_AVAILABLE:
|
|
45
|
+
raise ImportError(
|
|
46
|
+
"Presidio is not available. Install with: "
|
|
47
|
+
"pip install cua-agent[pii-anonymization]"
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
# TODO: Implement __init__
|
|
51
|
+
|
|
52
|
+
async def on_llm_start(self, messages: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
|
53
|
+
"""
|
|
54
|
+
Anonymize PII in messages before sending to agent loop.
|
|
55
|
+
|
|
56
|
+
Args:
|
|
57
|
+
messages: List of message dictionaries
|
|
58
|
+
|
|
59
|
+
Returns:
|
|
60
|
+
List of messages with PII anonymized
|
|
61
|
+
"""
|
|
62
|
+
anonymized_messages = []
|
|
63
|
+
for msg in messages:
|
|
64
|
+
anonymized_msg = await self._anonymize_message(msg)
|
|
65
|
+
anonymized_messages.append(anonymized_msg)
|
|
66
|
+
|
|
67
|
+
return anonymized_messages
|
|
68
|
+
|
|
69
|
+
async def on_llm_end(self, output: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
|
70
|
+
"""
|
|
71
|
+
Deanonymize PII in tool calls and message outputs after agent loop.
|
|
72
|
+
|
|
73
|
+
Args:
|
|
74
|
+
output: List of output dictionaries
|
|
75
|
+
|
|
76
|
+
Returns:
|
|
77
|
+
List of output with PII deanonymized for tool calls
|
|
78
|
+
"""
|
|
79
|
+
deanonymized_output = []
|
|
80
|
+
for item in output:
|
|
81
|
+
# Only deanonymize tool calls and computer_call messages
|
|
82
|
+
if item.get("type") in ["computer_call", "computer_call_output"]:
|
|
83
|
+
deanonymized_item = await self._deanonymize_item(item)
|
|
84
|
+
deanonymized_output.append(deanonymized_item)
|
|
85
|
+
else:
|
|
86
|
+
deanonymized_output.append(item)
|
|
87
|
+
|
|
88
|
+
return deanonymized_output
|
|
89
|
+
|
|
90
|
+
async def _anonymize_message(self, message: Dict[str, Any]) -> Dict[str, Any]:
|
|
91
|
+
# TODO: Implement _anonymize_message
|
|
92
|
+
return message
|
|
93
|
+
|
|
94
|
+
async def _deanonymize_item(self, item: Dict[str, Any]) -> Dict[str, Any]:
|
|
95
|
+
# TODO: Implement _deanonymize_item
|
|
96
|
+
return item
|
|
@@ -326,13 +326,13 @@ def _convert_responses_items_to_completion_messages(messages: Messages) -> List[
|
|
|
326
326
|
"text": "+".join(action.get("keys", []))
|
|
327
327
|
}
|
|
328
328
|
})
|
|
329
|
-
elif action_type
|
|
329
|
+
elif action_type in ["mouse_move", "move"]:
|
|
330
330
|
# Input:
|
|
331
331
|
# {
|
|
332
332
|
# "type": "computer_call",
|
|
333
333
|
# "call_id": "call_1",
|
|
334
334
|
# "action": {
|
|
335
|
-
# "type": "
|
|
335
|
+
# "type": "move",
|
|
336
336
|
# "x": 150,
|
|
337
337
|
# "y": 250
|
|
338
338
|
# }
|
|
@@ -1,259 +0,0 @@
|
|
|
1
|
-
"""
|
|
2
|
-
PII anonymization callback handler using Microsoft Presidio for text and image redaction.
|
|
3
|
-
"""
|
|
4
|
-
|
|
5
|
-
from typing import List, Dict, Any, Optional, Tuple
|
|
6
|
-
from .base import AsyncCallbackHandler
|
|
7
|
-
import base64
|
|
8
|
-
import io
|
|
9
|
-
import logging
|
|
10
|
-
|
|
11
|
-
try:
|
|
12
|
-
from presidio_analyzer import AnalyzerEngine
|
|
13
|
-
from presidio_anonymizer import AnonymizerEngine, DeanonymizeEngine
|
|
14
|
-
from presidio_anonymizer.entities import RecognizerResult, OperatorConfig
|
|
15
|
-
from presidio_image_redactor import ImageRedactorEngine
|
|
16
|
-
from PIL import Image
|
|
17
|
-
PRESIDIO_AVAILABLE = True
|
|
18
|
-
except ImportError:
|
|
19
|
-
PRESIDIO_AVAILABLE = False
|
|
20
|
-
|
|
21
|
-
logger = logging.getLogger(__name__)
|
|
22
|
-
|
|
23
|
-
class PIIAnonymizationCallback(AsyncCallbackHandler):
|
|
24
|
-
"""
|
|
25
|
-
Callback handler that anonymizes PII in text and images using Microsoft Presidio.
|
|
26
|
-
|
|
27
|
-
This handler:
|
|
28
|
-
1. Anonymizes PII in messages before sending to the agent loop
|
|
29
|
-
2. Deanonymizes PII in tool calls and message outputs after the agent loop
|
|
30
|
-
3. Redacts PII from images in computer_call_output messages
|
|
31
|
-
"""
|
|
32
|
-
|
|
33
|
-
def __init__(
|
|
34
|
-
self,
|
|
35
|
-
anonymize_text: bool = True,
|
|
36
|
-
anonymize_images: bool = True,
|
|
37
|
-
entities_to_anonymize: Optional[List[str]] = None,
|
|
38
|
-
anonymization_operator: str = "replace",
|
|
39
|
-
image_redaction_color: Tuple[int, int, int] = (255, 192, 203) # Pink
|
|
40
|
-
):
|
|
41
|
-
"""
|
|
42
|
-
Initialize the PII anonymization callback.
|
|
43
|
-
|
|
44
|
-
Args:
|
|
45
|
-
anonymize_text: Whether to anonymize text content
|
|
46
|
-
anonymize_images: Whether to redact images
|
|
47
|
-
entities_to_anonymize: List of entity types to anonymize (None for all)
|
|
48
|
-
anonymization_operator: Presidio operator to use ("replace", "mask", "redact", etc.)
|
|
49
|
-
image_redaction_color: RGB color for image redaction
|
|
50
|
-
"""
|
|
51
|
-
if not PRESIDIO_AVAILABLE:
|
|
52
|
-
raise ImportError(
|
|
53
|
-
"Presidio is not available. Install with: "
|
|
54
|
-
"pip install presidio-analyzer presidio-anonymizer presidio-image-redactor"
|
|
55
|
-
)
|
|
56
|
-
|
|
57
|
-
self.anonymize_text = anonymize_text
|
|
58
|
-
self.anonymize_images = anonymize_images
|
|
59
|
-
self.entities_to_anonymize = entities_to_anonymize
|
|
60
|
-
self.anonymization_operator = anonymization_operator
|
|
61
|
-
self.image_redaction_color = image_redaction_color
|
|
62
|
-
|
|
63
|
-
# Initialize Presidio engines
|
|
64
|
-
self.analyzer = AnalyzerEngine()
|
|
65
|
-
self.anonymizer = AnonymizerEngine()
|
|
66
|
-
self.deanonymizer = DeanonymizeEngine()
|
|
67
|
-
self.image_redactor = ImageRedactorEngine()
|
|
68
|
-
|
|
69
|
-
# Store anonymization mappings for deanonymization
|
|
70
|
-
self.anonymization_mappings: Dict[str, Any] = {}
|
|
71
|
-
|
|
72
|
-
async def on_llm_start(self, messages: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
|
73
|
-
"""
|
|
74
|
-
Anonymize PII in messages before sending to agent loop.
|
|
75
|
-
|
|
76
|
-
Args:
|
|
77
|
-
messages: List of message dictionaries
|
|
78
|
-
|
|
79
|
-
Returns:
|
|
80
|
-
List of messages with PII anonymized
|
|
81
|
-
"""
|
|
82
|
-
if not self.anonymize_text and not self.anonymize_images:
|
|
83
|
-
return messages
|
|
84
|
-
|
|
85
|
-
anonymized_messages = []
|
|
86
|
-
for msg in messages:
|
|
87
|
-
anonymized_msg = await self._anonymize_message(msg)
|
|
88
|
-
anonymized_messages.append(anonymized_msg)
|
|
89
|
-
|
|
90
|
-
return anonymized_messages
|
|
91
|
-
|
|
92
|
-
async def on_llm_end(self, output: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
|
93
|
-
"""
|
|
94
|
-
Deanonymize PII in tool calls and message outputs after agent loop.
|
|
95
|
-
|
|
96
|
-
Args:
|
|
97
|
-
output: List of output dictionaries
|
|
98
|
-
|
|
99
|
-
Returns:
|
|
100
|
-
List of output with PII deanonymized for tool calls
|
|
101
|
-
"""
|
|
102
|
-
if not self.anonymize_text:
|
|
103
|
-
return output
|
|
104
|
-
|
|
105
|
-
deanonymized_output = []
|
|
106
|
-
for item in output:
|
|
107
|
-
# Only deanonymize tool calls and computer_call messages
|
|
108
|
-
if item.get("type") in ["computer_call", "computer_call_output"]:
|
|
109
|
-
deanonymized_item = await self._deanonymize_item(item)
|
|
110
|
-
deanonymized_output.append(deanonymized_item)
|
|
111
|
-
else:
|
|
112
|
-
deanonymized_output.append(item)
|
|
113
|
-
|
|
114
|
-
return deanonymized_output
|
|
115
|
-
|
|
116
|
-
async def _anonymize_message(self, message: Dict[str, Any]) -> Dict[str, Any]:
|
|
117
|
-
"""Anonymize PII in a single message."""
|
|
118
|
-
msg_copy = message.copy()
|
|
119
|
-
|
|
120
|
-
# Anonymize text content
|
|
121
|
-
if self.anonymize_text:
|
|
122
|
-
msg_copy = await self._anonymize_text_content(msg_copy)
|
|
123
|
-
|
|
124
|
-
# Redact images in computer_call_output
|
|
125
|
-
if self.anonymize_images and msg_copy.get("type") == "computer_call_output":
|
|
126
|
-
msg_copy = await self._redact_image_content(msg_copy)
|
|
127
|
-
|
|
128
|
-
return msg_copy
|
|
129
|
-
|
|
130
|
-
async def _anonymize_text_content(self, message: Dict[str, Any]) -> Dict[str, Any]:
|
|
131
|
-
"""Anonymize text content in a message."""
|
|
132
|
-
msg_copy = message.copy()
|
|
133
|
-
|
|
134
|
-
# Handle content array
|
|
135
|
-
content = msg_copy.get("content", [])
|
|
136
|
-
if isinstance(content, str):
|
|
137
|
-
anonymized_text, _ = await self._anonymize_text(content)
|
|
138
|
-
msg_copy["content"] = anonymized_text
|
|
139
|
-
elif isinstance(content, list):
|
|
140
|
-
anonymized_content = []
|
|
141
|
-
for item in content:
|
|
142
|
-
if isinstance(item, dict) and item.get("type") == "text":
|
|
143
|
-
text = item.get("text", "")
|
|
144
|
-
anonymized_text, _ = await self._anonymize_text(text)
|
|
145
|
-
item_copy = item.copy()
|
|
146
|
-
item_copy["text"] = anonymized_text
|
|
147
|
-
anonymized_content.append(item_copy)
|
|
148
|
-
else:
|
|
149
|
-
anonymized_content.append(item)
|
|
150
|
-
msg_copy["content"] = anonymized_content
|
|
151
|
-
|
|
152
|
-
return msg_copy
|
|
153
|
-
|
|
154
|
-
async def _redact_image_content(self, message: Dict[str, Any]) -> Dict[str, Any]:
|
|
155
|
-
"""Redact PII from images in computer_call_output messages."""
|
|
156
|
-
msg_copy = message.copy()
|
|
157
|
-
output = msg_copy.get("output", {})
|
|
158
|
-
|
|
159
|
-
if isinstance(output, dict) and "image_url" in output:
|
|
160
|
-
try:
|
|
161
|
-
# Extract base64 image data
|
|
162
|
-
image_url = output["image_url"]
|
|
163
|
-
if image_url.startswith("data:image/"):
|
|
164
|
-
# Parse data URL
|
|
165
|
-
header, data = image_url.split(",", 1)
|
|
166
|
-
image_data = base64.b64decode(data)
|
|
167
|
-
|
|
168
|
-
# Load image with PIL
|
|
169
|
-
image = Image.open(io.BytesIO(image_data))
|
|
170
|
-
|
|
171
|
-
# Redact PII from image
|
|
172
|
-
redacted_image = self.image_redactor.redact(image, self.image_redaction_color)
|
|
173
|
-
|
|
174
|
-
# Convert back to base64
|
|
175
|
-
buffer = io.BytesIO()
|
|
176
|
-
redacted_image.save(buffer, format="PNG")
|
|
177
|
-
redacted_data = base64.b64encode(buffer.getvalue()).decode()
|
|
178
|
-
|
|
179
|
-
# Update image URL
|
|
180
|
-
output_copy = output.copy()
|
|
181
|
-
output_copy["image_url"] = f"data:image/png;base64,{redacted_data}"
|
|
182
|
-
msg_copy["output"] = output_copy
|
|
183
|
-
|
|
184
|
-
except Exception as e:
|
|
185
|
-
logger.warning(f"Failed to redact image: {e}")
|
|
186
|
-
|
|
187
|
-
return msg_copy
|
|
188
|
-
|
|
189
|
-
async def _deanonymize_item(self, item: Dict[str, Any]) -> Dict[str, Any]:
|
|
190
|
-
"""Deanonymize PII in tool calls and computer outputs."""
|
|
191
|
-
item_copy = item.copy()
|
|
192
|
-
|
|
193
|
-
# Handle computer_call arguments
|
|
194
|
-
if item.get("type") == "computer_call":
|
|
195
|
-
args = item_copy.get("args", {})
|
|
196
|
-
if isinstance(args, dict):
|
|
197
|
-
deanonymized_args = {}
|
|
198
|
-
for key, value in args.items():
|
|
199
|
-
if isinstance(value, str):
|
|
200
|
-
deanonymized_value, _ = await self._deanonymize_text(value)
|
|
201
|
-
deanonymized_args[key] = deanonymized_value
|
|
202
|
-
else:
|
|
203
|
-
deanonymized_args[key] = value
|
|
204
|
-
item_copy["args"] = deanonymized_args
|
|
205
|
-
|
|
206
|
-
return item_copy
|
|
207
|
-
|
|
208
|
-
async def _anonymize_text(self, text: str) -> Tuple[str, List[RecognizerResult]]:
|
|
209
|
-
"""Anonymize PII in text and return the anonymized text and results."""
|
|
210
|
-
if not text.strip():
|
|
211
|
-
return text, []
|
|
212
|
-
|
|
213
|
-
try:
|
|
214
|
-
# Analyze text for PII
|
|
215
|
-
analyzer_results = self.analyzer.analyze(
|
|
216
|
-
text=text,
|
|
217
|
-
entities=self.entities_to_anonymize,
|
|
218
|
-
language="en"
|
|
219
|
-
)
|
|
220
|
-
|
|
221
|
-
if not analyzer_results:
|
|
222
|
-
return text, []
|
|
223
|
-
|
|
224
|
-
# Anonymize the text
|
|
225
|
-
anonymized_result = self.anonymizer.anonymize(
|
|
226
|
-
text=text,
|
|
227
|
-
analyzer_results=analyzer_results,
|
|
228
|
-
operators={entity_type: OperatorConfig(self.anonymization_operator)
|
|
229
|
-
for entity_type in set(result.entity_type for result in analyzer_results)}
|
|
230
|
-
)
|
|
231
|
-
|
|
232
|
-
# Store mapping for deanonymization
|
|
233
|
-
mapping_key = str(hash(text))
|
|
234
|
-
self.anonymization_mappings[mapping_key] = {
|
|
235
|
-
"original": text,
|
|
236
|
-
"anonymized": anonymized_result.text,
|
|
237
|
-
"results": analyzer_results
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
return anonymized_result.text, analyzer_results
|
|
241
|
-
|
|
242
|
-
except Exception as e:
|
|
243
|
-
logger.warning(f"Failed to anonymize text: {e}")
|
|
244
|
-
return text, []
|
|
245
|
-
|
|
246
|
-
async def _deanonymize_text(self, text: str) -> Tuple[str, bool]:
|
|
247
|
-
"""Attempt to deanonymize text using stored mappings."""
|
|
248
|
-
try:
|
|
249
|
-
# Look for matching anonymized text in mappings
|
|
250
|
-
for mapping_key, mapping in self.anonymization_mappings.items():
|
|
251
|
-
if mapping["anonymized"] == text:
|
|
252
|
-
return mapping["original"], True
|
|
253
|
-
|
|
254
|
-
# If no mapping found, return original text
|
|
255
|
-
return text, False
|
|
256
|
-
|
|
257
|
-
except Exception as e:
|
|
258
|
-
logger.warning(f"Failed to deanonymize text: {e}")
|
|
259
|
-
return text, False
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|