File size: 1,300 Bytes
49cd06a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from smolagents import tool
import random
import string

@tool
def generate_password(length: int = 12, include_special: bool = True) -> str:
    """Generate a secure random password
    Args:
        length: Length of password (default: 12)
        include_special: Include special characters (default: True)
    """
    try:
        if length < 8:
            return "Password length must be at least 8 characters"
        
        chars = string.ascii_letters + string.digits
        if include_special:
            chars += string.punctuation
            
        password = ''.join(random.choice(chars) for _ in range(length))
        
        # Ensure password contains at least one of each required type
        if not any(c.isupper() for c in password):
            password = random.choice(string.ascii_uppercase) + password[1:]
        if not any(c.islower() for c in password):
            password = password[:-1] + random.choice(string.ascii_lowercase)
        if not any(c.isdigit() for c in password):
            pos = random.randint(1, len(password)-2)
            password = password[:pos] + random.choice(string.digits) + password[pos+1:]
            
        return f"Generated password: {password}"
    except Exception as e:
        return f"Error generating password: {str(e)}"