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
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
"""Extractor agent for data extraction with selectors."""

import re
from typing import Any

from app.core.action import Action, ActionType
from app.core.observation import Observation, PageElement

from .base import BaseAgent


class ExtractorAgent(BaseAgent):
    """
    Agent responsible for extracting structured data from pages.
    
    The ExtractorAgent handles:
    - Identifying data elements using CSS/XPath selectors
    - Extracting text, attributes, and structured content
    - Handling tables and lists
    - Post-processing extracted values
    - Confidence scoring for extractions
    """

    def __init__(
        self,
        agent_id: str = "extractor",
        config: dict[str, Any] | None = None,
    ):
        """
        Initialize the ExtractorAgent.
        
        Args:
            agent_id: Unique identifier for this agent.
            config: Optional configuration with keys:
                - min_confidence: Minimum confidence to accept extraction
                - extraction_timeout: Timeout for extraction operations
                - enable_fuzzy_matching: Enable fuzzy text matching
        """
        super().__init__(agent_id, config)
        self.min_confidence = self.config.get("min_confidence", 0.5)
        self.extraction_timeout = self.config.get("extraction_timeout", 5000)
        self.enable_fuzzy_matching = self.config.get("enable_fuzzy_matching", True)
        self._extraction_cache: dict[str, Any] = {}
        self._selector_patterns: dict[str, list[str]] = self._init_selector_patterns()

    def _init_selector_patterns(self) -> dict[str, list[str]]:
        """Initialize common selector patterns for different field types."""
        return {
            "price": [
                "[class*='price']",
                "[id*='price']",
                "[itemprop='price']",
                ".product-price",
                ".item-price",
                "span[data-price]",
            ],
            "title": [
                "h1",
                "[class*='title']",
                "[itemprop='name']",
                ".product-title",
                ".item-title",
            ],
            "description": [
                "[class*='description']",
                "[itemprop='description']",
                ".product-description",
                "article p",
                ".content p",
            ],
            "image": [
                "[class*='product-image'] img",
                "[itemprop='image']",
                ".main-image img",
                "figure img",
            ],
            "date": [
                "time",
                "[datetime]",
                "[class*='date']",
                "[itemprop='datePublished']",
            ],
            "author": [
                "[class*='author']",
                "[itemprop='author']",
                "[rel='author']",
                ".byline",
            ],
        }

    async def act(self, observation: Observation) -> Action:
        """
        Select the best extraction action based on observation.
        
        Analyzes the page and decides what data to extract next.
        
        Args:
            observation: The current state observation.
            
        Returns:
            The extraction action to execute.
        """
        try:
            # Get remaining fields to extract
            remaining_fields = observation.fields_remaining

            if not remaining_fields:
                return Action(
                    action_type=ActionType.DONE,
                    parameters={"success": True, "message": "All fields extracted"},
                    reasoning="No more fields to extract",
                    confidence=1.0,
                    agent_id=self.agent_id,
                )

            # Pick the next field to extract
            field_name = remaining_fields[0]

            # Find best selector for the field
            selector, confidence = await self._find_selector_for_field(
                field_name,
                observation,
            )

            if selector and confidence >= self.min_confidence:
                return self._create_extraction_action(
                    field_name,
                    selector,
                    confidence,
                )

            # Try alternative extraction methods
            alt_action = await self._try_alternative_extraction(
                field_name,
                observation,
            )
            if alt_action:
                return alt_action

            # Cannot extract this field
            return Action(
                action_type=ActionType.EXTRACT_FIELD,
                parameters={
                    "field_name": field_name,
                    "selector": None,
                    "extraction_method": "llm",
                },
                reasoning=f"No selector found, using LLM extraction for {field_name}",
                confidence=0.4,
                agent_id=self.agent_id,
            )

        except Exception as e:
            return Action(
                action_type=ActionType.FAIL,
                parameters={"success": False, "message": str(e)},
                reasoning=f"Extraction error: {e}",
                confidence=1.0,
                agent_id=self.agent_id,
            )

    async def plan(self, observation: Observation) -> list[Action]:
        """
        Create an extraction plan for all remaining fields.
        
        Analyzes the page structure and plans the optimal
        extraction sequence.
        
        Args:
            observation: The current state observation.
            
        Returns:
            A list of planned extraction actions.
        """
        try:
            actions: list[Action] = []
            remaining_fields = observation.fields_remaining

            for field_name in remaining_fields:
                selector, confidence = await self._find_selector_for_field(
                    field_name,
                    observation,
                )

                if selector:
                    actions.append(
                        self._create_extraction_action(
                            field_name,
                            selector,
                            confidence,
                        )
                    )
                else:
                    # Plan LLM-based extraction as fallback
                    actions.append(
                        Action(
                            action_type=ActionType.EXTRACT_FIELD,
                            parameters={
                                "field_name": field_name,
                                "extraction_method": "llm",
                            },
                            reasoning=f"Planning LLM extraction for {field_name}",
                            confidence=0.5,
                            agent_id=self.agent_id,
                        )
                    )

            return actions

        except Exception as e:
            return [
                Action(
                    action_type=ActionType.FAIL,
                    parameters={"message": f"Extraction planning failed: {e}"},
                    reasoning=str(e),
                    confidence=1.0,
                    agent_id=self.agent_id,
                )
            ]

    async def _find_selector_for_field(
        self,
        field_name: str,
        observation: Observation,
    ) -> tuple[str | None, float]:
        """
        Find the best selector for a field.
        
        Args:
            field_name: Name of the field to extract.
            observation: Current observation.
            
        Returns:
            Tuple of (selector, confidence).
        """
        best_selector: str | None = None
        best_confidence = 0.0

        # Check predefined patterns first
        patterns = self._get_patterns_for_field(field_name)
        for pattern in patterns:
            element = self._find_element_by_selector(
                pattern,
                observation.page_elements,
            )
            if element:
                confidence = self._calculate_confidence(element, field_name)
                if confidence > best_confidence:
                    best_selector = element.selector
                    best_confidence = confidence

        # Search by text content if fuzzy matching enabled
        if self.enable_fuzzy_matching and best_confidence < 0.7:
            element, confidence = self._find_element_by_text(
                field_name,
                observation.page_elements,
            )
            if element and confidence > best_confidence:
                best_selector = element.selector
                best_confidence = confidence

        return best_selector, best_confidence

    def _get_patterns_for_field(self, field_name: str) -> list[str]:
        """Get selector patterns for a field type."""
        field_lower = field_name.lower()

        # Direct match
        if field_lower in self._selector_patterns:
            return self._selector_patterns[field_lower]

        # Partial match
        for key, patterns in self._selector_patterns.items():
            if key in field_lower or field_lower in key:
                return patterns

        # Generate generic patterns
        return [
            f"[class*='{field_lower}']",
            f"[id*='{field_lower}']",
            f"[data-{field_lower}]",
            f".{field_lower}",
            f"#{field_lower}",
        ]

    def _find_element_by_selector(
        self,
        selector: str,
        elements: list[PageElement],
    ) -> PageElement | None:
        """Find an element matching a selector pattern."""
        selector_lower = selector.lower()

        for element in elements:
            element_selector = element.selector.lower()
            if selector_lower in element_selector:
                return element

            # Check class and id attributes
            classes = element.attributes.get("class", "").lower()
            element_id = element.attributes.get("id", "").lower()

            if selector_lower.strip(".[#]") in classes:
                return element
            if selector_lower.strip(".[#]") in element_id:
                return element

        return None

    def _find_element_by_text(
        self,
        field_name: str,
        elements: list[PageElement],
    ) -> tuple[PageElement | None, float]:
        """Find an element by text content matching."""
        field_lower = field_name.lower().replace("_", " ")
        best_element: PageElement | None = None
        best_score = 0.0

        for element in elements:
            if not element.text:
                continue

            text_lower = element.text.lower()

            # Check for label-like patterns
            if f"{field_lower}:" in text_lower or f"{field_lower} :" in text_lower:
                score = 0.9
            elif field_lower in text_lower:
                # Calculate similarity score
                score = len(field_lower) / max(len(text_lower), 1) * 0.8
            else:
                continue

            if score > best_score:
                best_element = element
                best_score = score

        return best_element, best_score

    def _calculate_confidence(self, element: PageElement, field_name: str) -> float:
        """Calculate extraction confidence for an element."""
        confidence = 0.5

        # Boost for visible elements
        if element.is_visible:
            confidence += 0.1

        # Boost for semantic attributes
        if element.attributes.get("itemprop"):
            confidence += 0.2
        if element.attributes.get("data-field"):
            confidence += 0.15

        # Boost if text contains field name
        if element.text and field_name.lower() in element.text.lower():
            confidence += 0.1

        # Penalty for very long text (likely not a single field)
        if element.text and len(element.text) > 500:
            confidence -= 0.2

        return min(1.0, max(0.0, confidence))

    async def _try_alternative_extraction(
        self,
        field_name: str,
        observation: Observation,
    ) -> Action | None:
        """Try alternative extraction methods."""
        # Check for table data
        for element in observation.page_elements:
            if element.tag in ("table", "tbody"):
                return Action(
                    action_type=ActionType.EXTRACT_TABLE,
                    parameters={
                        "table_selector": element.selector,
                        "target_field": field_name,
                    },
                    reasoning=f"Extracting {field_name} from table",
                    confidence=0.6,
                    agent_id=self.agent_id,
                )

        # Check for list data
        for element in observation.page_elements:
            if element.tag in ("ul", "ol", "dl"):
                return Action(
                    action_type=ActionType.EXTRACT_LIST,
                    parameters={
                        "container_selector": element.selector,
                        "item_selector": "li",
                        "field_selectors": {field_name: "text"},
                    },
                    reasoning=f"Extracting {field_name} from list",
                    confidence=0.55,
                    agent_id=self.agent_id,
                )

        return None

    def _create_extraction_action(
        self,
        field_name: str,
        selector: str,
        confidence: float,
    ) -> Action:
        """Create an extraction action."""
        return Action(
            action_type=ActionType.EXTRACT_FIELD,
            parameters={
                "field_name": field_name,
                "selector": selector,
                "extraction_method": "text",
            },
            reasoning=f"Extracting {field_name} using selector: {selector}",
            confidence=confidence,
            agent_id=self.agent_id,
        )

    def extract_with_regex(
        self,
        text: str,
        pattern: str,
        group: int = 0,
    ) -> str | None:
        """
        Extract text using a regex pattern.
        
        Args:
            text: The text to search in.
            pattern: Regex pattern.
            group: Capture group to return.
            
        Returns:
            Extracted text or None.
        """
        try:
            match = re.search(pattern, text)
            if match:
                return match.group(group)
            return None
        except re.error:
            return None

    def post_process_value(
        self,
        value: Any,
        field_name: str,
    ) -> Any:
        """
        Post-process an extracted value based on field type.
        
        Args:
            value: The raw extracted value.
            field_name: Name of the field (used to infer type).
            
        Returns:
            Processed value.
        """
        if value is None:
            return None

        value_str = str(value).strip()
        field_lower = field_name.lower()

        # Price processing
        if "price" in field_lower:
            # Remove currency symbols but keep numbers and decimal
            price_match = re.search(r"[\d,]+\.?\d*", value_str.replace(",", ""))
            if price_match:
                return float(price_match.group().replace(",", ""))

        # Date processing
        if "date" in field_lower:
            return value_str  # Return as-is, let caller parse

        # Number processing
        if any(x in field_lower for x in ["count", "quantity", "number"]):
            num_match = re.search(r"\d+", value_str)
            if num_match:
                return int(num_match.group())

        return value_str

    def reset(self) -> None:
        """Reset the extractor state."""
        super().reset()
        self._extraction_cache.clear()