File size: 6,986 Bytes
23804b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
#!/usr/bin/env python3
"""
Simple Genkit Integration Test
Tests basic Genkit functionality with the cybersecurity AI system
"""

import os
import asyncio
import sys
from typing import Dict, List, Any
from datetime import datetime

# Add project root to path
sys.path.append('/home/o1/Desktop/cyber_llm')

try:
    # Test Genkit imports
    from genkit.ai import Genkit
    from genkit.plugins.google_genai import GoogleAI  
    from genkit import z
    GENKIT_AVAILABLE = True
    print("βœ… Genkit imports successful")
except ImportError as e:
    GENKIT_AVAILABLE = False
    print(f"❌ Genkit import failed: {e}")

class SimpleGenkitTest:
    """Simple test of Genkit integration"""
    
    def __init__(self):
        if not GENKIT_AVAILABLE:
            print("❌ Genkit not available")
            return
            
        try:
            # Initialize Genkit with Google AI plugin
            self.ai = Genkit(
                plugins=[GoogleAI()],
                model='googleai/gemini-2.5-flash'
            )
            print("βœ… Genkit AI initialized")
        except Exception as e:
            print(f"❌ Failed to initialize Genkit: {e}")
            self.ai = None
    
    def create_simple_agent(self):
        """Create a simple cybersecurity agent"""
        if not self.ai:
            return None
            
        try:
            # Define a simple cybersecurity analysis tool
            analyze_tool = self.ai.defineTool(
                {
                    'name': 'analyzeSecurityEvent',
                    'description': 'Analyze a security event for threats',
                    'inputSchema': z.object({
                        'event': z.string().describe('Security event description'),
                        'priority': z.string().describe('Event priority: low, medium, high')
                    }),
                    'outputSchema': z.string().describe('Security analysis result')
                },
                async_tool_impl=self._analyze_security_event
            )
            
            # Create a cybersecurity agent prompt
            security_agent = self.ai.definePrompt({
                'name': 'securityAnalysisAgent',
                'description': 'Cybersecurity threat analysis agent',
                'tools': [analyze_tool],
                'system': '''You are a cybersecurity threat analysis expert.
                Analyze security events and provide risk assessments.
                Use available tools to perform detailed analysis.
                Always provide clear, actionable security recommendations.'''
            })
            
            print("βœ… Simple security agent created")
            return security_agent
            
        except Exception as e:
            print(f"❌ Failed to create agent: {e}")
            return None
    
    async def _analyze_security_event(self, input_data):
        """Simple security event analysis implementation"""
        try:
            event = input_data['event']
            priority = input_data['priority']
            
            # Simple analysis logic
            analysis = {
                "event": event,
                "priority": priority,
                "timestamp": datetime.now().isoformat(),
                "analysis": f"Analyzed security event with {priority} priority",
                "recommendations": [
                    "Monitor for related events",
                    "Check system logs",
                    "Verify user activities"
                ]
            }
            
            return str(analysis)
            
        except Exception as e:
            return f"Analysis error: {str(e)}"
    
    async def test_agent_interaction(self):
        """Test basic agent interaction"""
        if not self.ai:
            print("❌ Genkit not initialized")
            return False
            
        try:
            # Create simple agent
            agent = self.create_simple_agent()
            if not agent:
                return False
            
            # Test agent interaction
            chat = self.ai.chat(agent)
            
            # Send a test query
            test_query = "Analyze this security event: Multiple failed login attempts from IP 192.168.1.100"
            
            print(f"πŸ” Sending query: {test_query}")
            response = await chat.send(test_query)
            
            print(f"βœ… Agent response: {response.content[:200]}...")
            return True
            
        except Exception as e:
            print(f"❌ Agent interaction failed: {e}")
            return False
    
    async def test_simple_generation(self):
        """Test simple text generation"""
        if not self.ai:
            print("❌ Genkit not initialized")  
            return False
            
        try:
            # Simple generation test
            result = await self.ai.generate({
                'model': 'googleai/gemini-2.5-flash',
                'prompt': 'Explain what makes a good cybersecurity threat detection system in 2 sentences.'
            })
            
            print(f"βœ… Generation test successful: {result.output[:100]}...")
            return True
            
        except Exception as e:
            print(f"❌ Generation test failed: {e}")
            return False

async def main():
    """Main test function"""
    
    print("πŸš€ Starting Simple Genkit Integration Test")
    print("=" * 50)
    
    # Check if API key is set
    api_key = os.getenv('GEMINI_API_KEY')
    if not api_key:
        print("⚠️  GEMINI_API_KEY not set in environment")
        print("   Please set your API key: export GEMINI_API_KEY=your_key_here")
        print("   Get your key from: https://aistudio.google.com/app/apikey")
        return False
    
    # Initialize test
    test = SimpleGenkitTest()
    if not test.ai:
        return False
    
    print("\nπŸ“‹ Running Tests...")
    
    # Test 1: Simple generation
    print("\n1. Testing simple text generation...")
    gen_success = await test.test_simple_generation()
    
    # Test 2: Agent interaction (if generation works)
    if gen_success:
        print("\n2. Testing agent interaction...")
        agent_success = await test.test_agent_interaction()
    else:
        agent_success = False
    
    # Results
    print("\n" + "=" * 50)
    print("🎯 Test Results:")
    print(f"   β€’ Simple Generation: {'βœ… PASS' if gen_success else '❌ FAIL'}")
    print(f"   β€’ Agent Interaction: {'βœ… PASS' if agent_success else '❌ FAIL'}")
    
    if gen_success and agent_success:
        print("\nπŸŽ‰ All tests passed! Genkit integration is working.")
        print("βœ… Ready to proceed with full orchestrator integration")
        return True
    else:
        print("\n⚠️  Some tests failed. Check API key and configuration.")
        return False

if __name__ == "__main__":
    success = asyncio.run(main())
    sys.exit(0 if success else 1)