File size: 3,116 Bytes
7dadc22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ab4638
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Configuration utilities for user role-based settings.
"""
from typing import Any, Dict, Optional
from .models import UserRole
from .api_config import (
    CONFIG_FOR_ADMIN_HF_USERS,
    CONFIG_FOR_PRO_HF_USERS,
    CONFIG_FOR_STANDARD_HF_USERS,
    CONFIG_FOR_ANONYMOUS_USERS
)


def get_config_value(role: UserRole, field: str, options: Optional[Dict[str, Any]] = None) -> Any:
    """
    Get the appropriate config value for a user role.
    
    Args:
        role: The user role ('anon', 'normal', 'pro', 'admin')
        field: The config field name to retrieve
        options: Optional user-provided options that may override defaults
        
    Returns:
        The config value appropriate for the user's role with respect to
        min/max boundaries and user overrides.
    """
    # Select the appropriate config based on user role
    if role == 'admin':
        config = CONFIG_FOR_ADMIN_HF_USERS
    elif role == 'pro':
        config = CONFIG_FOR_PRO_HF_USERS
    elif role == 'normal':
        config = CONFIG_FOR_STANDARD_HF_USERS
    else:  # Anonymous users
        config = CONFIG_FOR_ANONYMOUS_USERS
    
    # Get the default value for this field from the config
    default_value = config.get(f"default_{field}", None)
    
    # For fields that have min/max bounds
    min_field = f"min_{field}"
    max_field = f"max_{field}"
    
    # Check if min/max constraints exist for this field
    has_constraints = min_field in config or max_field in config
    
    if not has_constraints:
        # For fields without constraints, just return the value from config
        return default_value
    
    # Get min and max values from config (if they exist)
    min_value = config.get(min_field, None)
    max_value = config.get(max_field, None)
    
    # If user provided options with this field
    if options and field in options:
        user_value = options[field]
        
        # Apply constraints if they exist
        if min_value is not None and user_value < min_value:
            return min_value
        if max_value is not None and user_value > max_value:
            return max_value
            
        # If within bounds, use the user's value
        return user_value
    
    # If no user value, return the default
    return default_value


def get_game_master_prompt(role: UserRole, options: Optional[Dict[str, Any]] = None) -> str:
    """
    Get the game master prompt for the given user role and options.
    
    Args:
        role: The user role ('anon', 'normal', 'pro', 'admin')
        options: Optional user-provided options that may contain game_master_prompt
        
    Returns:
        The game master prompt string, or empty string if not set
    """
    # Check if user provided a custom game master prompt
    if options and 'game_master_prompt' in options:
        user_prompt = options['game_master_prompt']
        if user_prompt and user_prompt.strip():
            return user_prompt.strip()
    
    # Fall back to role-based default
    default_value = get_config_value(role, 'game_master_prompt', options)
    return default_value or ""