File size: 1,813 Bytes
aa2d45f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
TEAM_API_KEY_MAP = {
    "openai": ("OPENAI_API_KEY", "OpenAI API Key"),
    "google": ("GOOGLE_API_KEY", "Google API Key"),
    "anthropic": ("ANTHROPIC_API_KEY", "Anthropic API Key"),
    "opensource": ("HUGGINGFACEHUB_API_TOKEN", "HuggingFace API Token"),
}

def get_required_teams(red_team, blue_team):
    """Get unique teams that require API keys."""
    teams = set()
    
    # Handle random selections - could be any team
    if red_team == "random":
        teams.update(["openai", "google", "anthropic", "opensource"])
    else:
        teams.add(red_team)
    
    if blue_team == "random":
        teams.update(["openai", "google", "anthropic", "opensource"])
    else:
        teams.add(blue_team)
    
    return sorted(teams)


def validate_api_keys(red_team, blue_team, openai_key, google_key, anthropic_key, hf_key, current_api_keys):
    """Validate that all required API keys are provided."""
    required_teams = get_required_teams(red_team, blue_team)
    
    # Build the API keys dictionary
    api_keys = {}
    missing_keys = []
    
    key_mapping = {
        "openai": (openai_key, "OPENAI_API_KEY", "OpenAI"),
        "google": (google_key, "GOOGLE_API_KEY", "Google"),
        "anthropic": (anthropic_key, "ANTHROPIC_API_KEY", "Anthropic"),
        "opensource": (hf_key, "HUGGINGFACEHUB_API_TOKEN", "HuggingFace")
    }
    
    for team in required_teams:
        key_value, key_name, display_name = key_mapping[team]
        if key_value and key_value.strip():
            api_keys[key_name] = key_value.strip()
        else:
            missing_keys.append(display_name)
    
    if missing_keys:
        error_msg = f"⚠️ Missing API keys for: {', '.join(missing_keys)}"
        return api_keys, False, error_msg
    
    return api_keys, True, "✅ All API keys validated!"