File size: 4,996 Bytes
94a7d52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
import requests
from crewai.tools import BaseTool
import re
from typing import Any
from merchs.merch import Merchant, NaturaMerchant, MercadoLivreMerchant

class CalculateDiscountedPriceTool(BaseTool):
    """
    A tool to calculate the final price of an item after a discount is applied.
    """
    name: str = "Calculate Discounted Price Tool"
    description: str = "Calculates the price after applying a given discount percentage."
    
    def _run(self, original_price: float, discount_percentage: float) -> float:
        
        """Calculates the discounted price and the total discount amount.

        This method takes an original price and a discount percentage, validates
        the inputs, and then computes the final price after the discount is
        applied, as well as the amount saved.

        Args:
            original_price: The initial price of the item as a float or integer.

        Returns:
            float:
                - The final discounted price, rounded to 2 decimal places.
        """
        
        if not isinstance(original_price, (int, float)) or not isinstance(discount_percentage, (int, float)):
            raise ValueError("Both original_price and discount_percentage must be numbers.")
        if discount_percentage < 0 or discount_percentage > 100:
            raise ValueError("Discount percentage must be between 0 and 100.")
        
        discount_amount = original_price * (discount_percentage / 100)
        discounted_price = original_price - discount_amount
        return round(discounted_price, 2)
    
class CalculateDiscountValueTool(BaseTool):
    """
    A tool to calculate the final discount value of an item after comparing the original value and the final value.
    """
    name: str = "Calculate Discount Value Tool"
    description: str = "Calculates the discount value after comparing two values."
    
    def _run(self, original_price: float, final_price: float) -> float:
        
        """Calculates the total discounted amount give the original and final price.

        This method takes an original price and a final price, validates
        the inputs, and then computes the final discounted value.

        Args:
            original_price: The initial price of the item as a float or integer.
            final_price: The final price after discount as a float or integer.

        Returns:
            float:
                - The final discount value, rounded to 0 decimal places.
        """        
        if not isinstance(original_price, (int, float)) or not isinstance(final_price, (int, float)):
            raise ValueError("Both original_price and final_price must be numbers.")
        
        discount_value = original_price - final_price
        discount_percentage = (discount_value / original_price) * 100
        return round(discount_percentage, 0)

class GetImageUrlTool(BaseTool):
    """
    A tool to retrieve the image URL for a given product URL.
    """
    name: str = "Get Image URL Tool"
    description: str = "Retrieves the image URL for a given product URL."

    def _run(self, product_url: str) -> str:
        """
        Retrieves the image URL for a given product URL.

        Example:
            product_url = (
                "https://minhaloja.natura.com/p/refil-shampoo-mamae-e-bebe/"
                "NATBRA-92791?product=refil-shampoo-mamae-e-bebe&productId=NATBRA-92791"
                "&consultoria=lidimelocosmeticos&marca=natura"
            )
            image_url = GetImageUrlTool()._run(product_url)
            # Returns:
            # "https://production.na01.natura.com/on/demandware.static/-/Sites-natura-br-storefront-catalog/default/dw68595724/NATBRA-92791_1.jpg"
        """
        if not isinstance(product_url, str):
            raise ValueError("product_url must be a string.")

        # Extract the numeric part after "NATBRA-" using a regular expression.
        match = re.search(r"NATBRA-(\d+)", product_url)
        if not match:
            raise ValueError(
                "Could not extract product_id from the provided URL. "
                "Expected a pattern like 'NATBRA-<digits>'."
            )
        product_id = match.group(1)

        # Build the final image URL.
        image_url = (
            f"https://production.na01.natura.com/on/demandware.static/-/Sites-natura-br-storefront-catalog/"
            f"default/dw68595724/NATBRA-{product_id}_1.jpg"
        )
        return image_url

class MerchantSelectorTool(BaseTool):
    name: str = "Merchant Selector Tool"
    description: str = "Selects the merchant based on url."
    natura_api_token: str

    def _run(self, original_url: str) -> Merchant:
        if "mercadolivre" in original_url or "ml.com.br" in original_url:
            return MercadoLivreMerchant()
        elif "natura.com" in original_url:
            return NaturaMerchant(natura_api_token=self.natura_api_token)
        else:
            raise ValueError("Unsupported merchant in URL.")