mirror of
https://github.com/mblanke/StrikePackageGPT.git
synced 2026-03-01 14:20:21 -05:00
Final code quality improvements: fix error handling and memory management
Co-authored-by: mblanke <9078342+mblanke@users.noreply.github.com>
This commit is contained in:
@@ -22,7 +22,9 @@ const VoiceControls = ({ onCommand, hotkey = ' ' }) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Request microphone permission
|
// Note: We request permission on mount for better UX.
|
||||||
|
// Alternative: Request only on first use by removing this and letting
|
||||||
|
// startListening() handle the permission request
|
||||||
requestMicrophonePermission();
|
requestMicrophonePermission();
|
||||||
|
|
||||||
// Setup hotkey listener
|
// Setup hotkey listener
|
||||||
|
|||||||
@@ -467,7 +467,10 @@ def _is_valid_target(target: str) -> bool:
|
|||||||
ip_pattern = r'^(\d{1,3}\.){3}\d{1,3}$'
|
ip_pattern = r'^(\d{1,3}\.){3}\d{1,3}$'
|
||||||
if re.match(ip_pattern, target):
|
if re.match(ip_pattern, target):
|
||||||
parts = target.split('.')
|
parts = target.split('.')
|
||||||
return all(0 <= int(part) <= 255 for part in parts)
|
try:
|
||||||
|
return all(0 <= int(part) <= 255 for part in parts)
|
||||||
|
except ValueError:
|
||||||
|
return False
|
||||||
|
|
||||||
# CIDR notation
|
# CIDR notation
|
||||||
if '/' in target:
|
if '/' in target:
|
||||||
@@ -522,7 +525,10 @@ def _is_valid_ip_or_hostname(host: str) -> bool:
|
|||||||
ip_pattern = r'^(\d{1,3}\.){3}\d{1,3}$'
|
ip_pattern = r'^(\d{1,3}\.){3}\d{1,3}$'
|
||||||
if re.match(ip_pattern, host):
|
if re.match(ip_pattern, host):
|
||||||
parts = host.split('.')
|
parts = host.split('.')
|
||||||
return all(0 <= int(part) <= 255 for part in parts)
|
try:
|
||||||
|
return all(0 <= int(part) <= 255 for part in parts)
|
||||||
|
except ValueError:
|
||||||
|
return False
|
||||||
|
|
||||||
# Hostname
|
# Hostname
|
||||||
hostname_pattern = r'^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)*[a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?$'
|
hostname_pattern = r'^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)*[a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?$'
|
||||||
|
|||||||
@@ -11,7 +11,10 @@ import json
|
|||||||
|
|
||||||
|
|
||||||
# Store conversation history per session
|
# Store conversation history per session
|
||||||
|
# Note: In production, use Redis or similar with TTL for scalability
|
||||||
|
# This simple in-memory dict will grow unbounded - implement cleanup as needed
|
||||||
conversation_contexts: Dict[str, List[Dict[str, str]]] = {}
|
conversation_contexts: Dict[str, List[Dict[str, str]]] = {}
|
||||||
|
MAX_SESSIONS = 1000 # Limit number of concurrent sessions
|
||||||
|
|
||||||
|
|
||||||
async def chat_completion(
|
async def chat_completion(
|
||||||
@@ -87,6 +90,11 @@ Always emphasize ethical hacking practices and legal considerations."""
|
|||||||
# Store in conversation history
|
# Store in conversation history
|
||||||
if session_id:
|
if session_id:
|
||||||
if session_id not in conversation_contexts:
|
if session_id not in conversation_contexts:
|
||||||
|
# Cleanup old sessions if limit reached
|
||||||
|
if len(conversation_contexts) >= MAX_SESSIONS:
|
||||||
|
# Remove oldest session (simple FIFO)
|
||||||
|
oldest_session = next(iter(conversation_contexts))
|
||||||
|
del conversation_contexts[oldest_session]
|
||||||
conversation_contexts[session_id] = []
|
conversation_contexts[session_id] = []
|
||||||
conversation_contexts[session_id].append({"role": "user", "content": message})
|
conversation_contexts[session_id].append({"role": "user", "content": message})
|
||||||
conversation_contexts[session_id].append({"role": "assistant", "content": assistant_message})
|
conversation_contexts[session_id].append({"role": "assistant", "content": assistant_message})
|
||||||
|
|||||||
@@ -91,8 +91,8 @@ def _transcribe_with_local_whisper(audio_data: bytes, format: str) -> Dict[str,
|
|||||||
# Clean up temp file
|
# Clean up temp file
|
||||||
try:
|
try:
|
||||||
os.unlink(temp_audio_path)
|
os.unlink(temp_audio_path)
|
||||||
except:
|
except (OSError, FileNotFoundError) as e:
|
||||||
pass
|
print(f"Warning: Could not delete temp file: {e}")
|
||||||
|
|
||||||
except ImportError:
|
except ImportError:
|
||||||
raise Exception("Whisper not installed. Install with: pip install openai-whisper")
|
raise Exception("Whisper not installed. Install with: pip install openai-whisper")
|
||||||
|
|||||||
Reference in New Issue
Block a user