-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Feat/thymia anam flux v2 #1853
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/thymia anam flux v2 #1853
Conversation
… Deepgram Flux STT This commit adds the following features: ## New Example App - voice-assistant-advanced: Advanced voice assistant with wellness analysis and avatar support ## New Extensions - thymia_analyzer_python: Real-time wellness analysis via Apollo API - anam_avatar_python: Anam avatar integration with WebRTC audio/video - heygen_avatar_python: HeyGen Streaming Avatar integration - deepgram_ws_asr_python: Deepgram WebSocket ASR with Nova 3 Flux model - generic_video_python: Generic video handling for avatar extensions ## Server Changes - Channel auto-injection for avatar extensions (heygen, anam, etc.) - Graceful shutdown with SIGTERM before SIGKILL (2s timeout) ## Playground Changes - Video track support for avatar display in AgentView - Remote audio/video track type fixes - UI improvements (graph select truncation, mobile layout) ## OpenAI LLM2 Changes - GPT-5.1 minimal_parameters mode support - Cross-provider tool compatibility (Groq, OpenAI) - Enhanced error logging for debugging
The previous changes to openai.py and extension.py were too invasive for upstream code and outside the scope of the feature branch: - Removed on_retrieve_prompt method (breaks prompt retrieval) - Changed system_prompt logic (removes per-request override) - Added minimal_parameters mode (changes API behavior) - Removed strict/additionalProperties (changes tool schema) - Modified logging verbosity These changes belong in a separate PR if needed.
Add optional use_max_completion_tokens config to openai_llm2_python: - When true: uses max_completion_tokens and excludes unsupported params (temperature, top_p, frequency_penalty, presence_penalty, seed) - When false (default): unchanged behavior, uses max_tokens with all params This enables GPT-5.1 and o1 models which require different API parameters. Default behavior is completely unchanged for backward compatibility.
Code Review: PR #1853 - Thymia Wellness Analysis & Avatar Integration📊 OverviewLines Changed: +23,664 / -90 🚨 CRITICAL ISSUES - Must Fix Before Merge1. Backup Files Committed ❌Files to remove:
Action: Remove these files and add 🔒 SECURITY REVIEW✅ API Key Handling - GoodAll extensions properly handle API keys:
|
| Category | Count | Severity |
|---|---|---|
| Critical (Must Fix) | 1 | Backup files |
| High (Should Fix) | 5 | Complexity, missing tests, resource leaks |
| Medium (Consider) | 8 | Error handling, performance, design |
| Low (Nice to have) | 2 | Documentation, requirements |
| Good Practices | 4 | Security, server changes, docs |
🎯 RECOMMENDATIONS PRIORITIZED
Before Merge (Critical) 🔴
- ✅ Remove all backup files (.backup, .bak)
- ✅ Add backup patterns to .gitignore
⚠️ Add basic integration tests for new extensions⚠️ Populate requirements.txt files
High Priority (Next Sprint) 🟠
- Refactor thymia_analyzer_python (split into modules)
- Fix resource leak risks (session management)
- Add input validation for user-controlled parameters
- Remove commented code from heygen extension
Medium Priority (Technical Debt) 🟡
- Simplify deepgram accumulation logic
- Add stricter error handling patterns
- Document state machines for complex extensions
- Optimize thymia buffer memory usage
Low Priority (Quality of Life) 🟢
- Improve documentation with troubleshooting sections
- Add directory traversal protection in worker.go
- Increase WebSocket reconnection backoff limits
🎉 Overall Assessment
This PR adds significant functionality with solid security practices for API key handling. The core implementations appear sound, but would benefit from:
- Cleanup (backup files, commented code)
- Better testing (4 extensions have no tests)
- Code organization (thymia extension needs refactoring)
- Resource management (session lifecycle improvements)
The server changes and documentation are excellent. Once the critical issues are addressed, this will be a valuable addition to the framework.
Reviewed by: Claude Sonnet 4.5 via Claude Code
Review Date: 2025-12-09
Code Review SummaryI've completed a comprehensive review of PR #1853. This PR adds significant new functionality including 5 new extensions, a voice-assistant-advanced example, and AI documentation. However, there are several critical issues that should be addressed before merging. 🔴 Critical Issues (Must Fix)1. Security: Hardcoded Security Bypass TokenFile: "x-vercel-protection-bypass": "WvrmD1CsSyCzkAdxjyU6Iz1DuJOgKjMZ"Issue: Production security token hardcoded in source code and now exposed in public repository. Risk: HIGH - This bypass token should be moved to environment variables immediately. Recommendation: bypass_token = os.getenv('VERCEL_PROTECTION_BYPASS', '')
if bypass_token:
headers["x-vercel-protection-bypass"] = bypass_token2. Performance: Blocking HTTP Calls in Async Event LoopFiles: All avatar extensions ( Issue: Using synchronous # anam.py:759, 888, 961
response = requests.post(endpoint, json=payload, headers=headers, timeout=30)Impact: Each HTTP call blocks the entire event loop for up to 30 seconds. This affects:
Recommendation: Replace all async with aiohttp.ClientSession() as session:
async with session.post(endpoint, json=payload, headers=headers, timeout=30) as response:
data = await response.json()3. Security: Command Injection RiskFile: Issue: Using subprocess to execute curl commands: curl_cmd = ["curl", "-X", "POST", f"{self.base_url}/v1/models/mental-wellness", ...]
process = await asyncio.create_subprocess_exec(*curl_cmd, ...)Problems:
Recommendation: Remove subprocess approach entirely and use aiohttp consistently. 4. Maintainability: Extremely Large FileFile: Issue: Single file with 2,821 lines of code is extremely difficult to maintain, test, and review. Recommendation: Split into multiple modules:
5. Security: API Key Exposure in LogsFiles: Multiple locations across extensions Examples:
Risk: MEDIUM - API keys and session tokens in log files Recommendation: Implement log sanitization for all sensitive data: def sanitize_for_logging(data):
sanitized = copy.deepcopy(data)
for key in ['api_key', 'token', 'authorization', 'sessionToken']:
if key in sanitized:
sanitized[key] = '***REDACTED***'
return sanitized🟡 High Priority Issues (Should Fix)6. Resource Leak: Uncancelled Background TasksFiles: asyncio.create_task(self._listen_for_messages()) # No reference stored!Issue: Tasks created but never cancelled during cleanup. Impact: Tasks continue running after extension stops, consuming resources. Fix: self._listen_task = asyncio.create_task(self._listen_for_messages())
# In cleanup:
if self._listen_task:
self._listen_task.cancel()
try:
await self._listen_task
except asyncio.CancelledError:
pass7. Race Conditions: Unprotected Shared StateFiles: All extensions with WebSocket connections Examples:
Impact: Connection state could become inconsistent across async tasks. Fix: Use self._state_lock = asyncio.Lock()
async with self._state_lock:
self._connection_broken = True8. Resource Leak: Session Close TimeoutFile: try:
await asyncio.wait_for(self.session.close(), timeout=5.0)
except asyncio.TimeoutError:
self.ten_env.log_warn("[DEEPGRAM-WS] Session close timed out")
self.session = None # ← Reference lost without proper cleanup!Issue: Session set to None without being properly closed on timeout. Fix: except asyncio.TimeoutError:
self.ten_env.log_warn("[DEEPGRAM-WS] Session close timed out, forcing cleanup")
if self.session and not self.session.closed:
await self.session.close() # Force close9. Code Hygiene: Backup File in RepositoryFile: Issue: Backup file should not be committed. Fix:
10. Performance: Busy-Wait LoopFile: for _ in range(150): # Wait up to 15 seconds
await asyncio.sleep(0.1)
if self._ready_for_audio:
breakIssue: Inefficient polling wastes CPU cycles. Fix: Use self._ready_for_audio_event = asyncio.Event()
# Wait with timeout
try:
await asyncio.wait_for(self._ready_for_audio_event.wait(), timeout=15.0)
except asyncio.TimeoutError:
raise TimeoutError("Timeout waiting for audio readiness")🟢 Medium Priority Issues (Nice to Have)11. Performance: Inefficient Audio ResamplingFile: Custom numpy-based linear interpolation lacks anti-aliasing filters. Impact: Poor audio quality for upsampling/downsampling. Recommendation: Use proper audio libraries: import librosa
resampled = librosa.resample(pcm_data, orig_sr=source_rate, target_sr=target_rate)12. Memory: Unbounded Queue GrowthFile: self.input_audio_queue.put_nowait(frame_buf) # No max size!Impact: Unbounded memory growth if processing is slower than input rate. Fix: self.input_audio_queue = asyncio.Queue(maxsize=100)13. Security: Insecure Session Cache FilesFiles: with open("/tmp/anam_session_id.txt", "w") as f: # World-readable!
f.write(session_id)Risk: Session IDs exposed in world-readable /tmp files. Fix: import tempfile
with tempfile.NamedTemporaryFile(mode='w', delete=False, prefix='anam_session_') as f:
f.write(session_id)
os.chmod(f.name, 0o600) # Owner read/write only14. Configuration: Hardcoded ConstantsFile: ANNOUNCEMENT_MIN_SPACING_SECONDS = 15.0
MOOD_PHASE_DURATION_SECONDS = 30.0
READING_PHASE_DURATION_SECONDS = 30.0Issue: Should be configurable via 15. Memory: Memory-Intensive WAV ConversionFile: Loads up to 5 minutes of audio into memory (~25MB). Recommendation: Consider streaming or chunked processing for large audio buffers. 📊 Server Changes Reviewhttp_server.go (Lines removed: 24342-24387)Good: Property-based channel auto-injection is elegant and future-proof. Concern: Removed Recommendation: Verify that no existing extensions break due to this change. If there are, document the breaking change in release notes. worker.go (Lines 24437-24477)Good: Implements graceful shutdown with SIGTERM before SIGKILL (2-second grace period). Minor Issue: Loop variable for i := 0; i < 20; i++ { // i unused✅ Positive Aspects
🎯 Recommendations SummaryBefore Merge (Critical):
High Priority (Should Fix):
Nice to Have:
📝 Test Coverage NoteI notice this PR doesn't include tests for the new extensions. The Recommendation: Add basic integration tests for:
Following the pattern in Overall AssessmentThis PR adds valuable functionality but requires refinement before merge. The core architecture is sound, but the implementation has critical security and performance issues that must be addressed. Primary Concerns:
Once these issues are addressed, this will be a strong addition to the TEN Framework. Happy to review again after changes! 🚀 |
openai_llm2_python/openai.py: - replace use_max_completion_tokens config flag with model name detection - automatically detect gpt-5.x models and use max_completion_tokens - no config changes needed - works based on model name server/internal/http_server.go: - restore convertToString function (accidentally removed) - restore ConvertType logic for user_id string conversion - channel auto-injection feature unchanged
7ac953d to
f80905b
Compare
Pull Request Review: Feat/thymia anam flux v2OverviewThis is a large PR with 23,658 additions across 89 files, introducing multiple new extensions, comprehensive AI documentation, server-side property injection improvements, and playground enhancements. ✅ Strengths1. Excellent Documentation
2. Server Architecture Improvements
3. Good Test Coverage
4. Type Safety Improvements
|
| Category | Rating | Notes |
|---|---|---|
| Code Quality | Good architecture, files too large | |
| Security | API logging risk, validation gaps | |
| Performance | ✅ Good | Some memory concerns |
| Test Coverage | Incomplete coverage | |
| Documentation | ✅ Excellent | Comprehensive AI docs |
| Architecture | ✅ Great | Property injection is elegant |
Overall: Solid work with significant value, but needs cleanup before merge. Remove backup files and refactor large extensions.
Great work on the property injection system and documentation!
Pull Request Review: Feat/thymia anam flux v2OverviewThis PR adds significant new functionality including multiple new extensions (Thymia analyzer, Anam avatar, Deepgram WebSocket ASR, HeyGen avatar, Generic video), comprehensive AI documentation, and server/playground enhancements. The PR is large (23,658 additions across 89 files) but adds valuable features for the framework. ✅ Strengths1. Excellent Documentation
2. Server Architecture Improvement
3. Extension Architecture
🔴 Critical Issues1. Backup Files Should Not Be CommittedThese files should be removed:
Impact: Adds 6,935 lines of unnecessary code to the repository 2. Thymia Extension - Potential Security IssuesFile: Issue 1: API credentials should support environment variables like other extensions in the framework Issue 2: Large file size concerns
3. Deepgram WebSocket Extension - Race ConditionFile: The extension uses 4. TypeScript Type Error in PlaygroundFile: Line 11 changed Action Required: Verify TypeScript build passes:
|
Pull Request Review: #1853📊 Review SummaryOverall Assessment: Stats: 23,658 additions, 51 deletions across 89 files 🔴 Critical Issues (Must Fix Before Merge)1. Security: API Key Exposure in LogsFile: thymia_analyzer_python/extension.py:373-416 The full API key exists in curl_cmd_str before masking. If logging is captured by external systems, the key could be exposed. Fix: Build the masked command directly without ever storing the full key, or replace subprocess curl with native aiohttp requests. 2. Resource Leak: Missing HTTP Client TimeoutFile: thymia_analyzer_python/extension.py:331-341 No timeout configured - requests could hang indefinitely. Fix: Add timeout=aiohttp.ClientTimeout(total=60) to ClientSession 3. Logic Error: Premature Buffer ClearFile: thymia_analyzer_python/extension.py:1410-1415 Buffer is cleared when Apollo is running, not when it has successfully uploaded. Apollo might still need the audio data. Fix: Only clear after both hellos_session_id is set AND apollo_complete is True 4. Security: Session File ConflictsFile: anam_avatar_python/anam.py:34 SESSION_CACHE_PATH = /tmp/anam_session_id.txt causes conflicts between multiple instances. Fix: Include channel name in filename 5. Bug: Zombie Process LeakFile: ai_agents/server/internal/worker.go After SIGTERM/SIGKILL, no wait() call to reap the process. Fix: Add syscall.Wait4(w.Pid, nil, syscall.WNOHANG, nil) after kill 🟡 High Priority Issues (Should Fix)6. Race Condition: Audio Queue and Recorder StateFile: anam_avatar_python/extension.py:143-150 Recorder could disconnect between ready check and usage. Fix: Wrap recorder operations in try/except 7. Missing Error Handling: API PollingFile: thymia_analyzer_python/extension.py:1959-1962 Exceptions during polling don't update completion flags, causing infinite polling. Fix: Mark completion on persistent errors after N consecutive failures 8. Missing Error Handling: JSON ParsingFile: heygen_avatar_python/extension.py:228-233 json.loads() could throw JSONDecodeError without handling. Fix: Wrap in try/except 9. Memory Leak: Unbounded Segment AccumulationFile: deepgram_ws_asr_python/extension.py:230-253 If UtteranceEnd never arrives, accumulated_segments grows forever. Fix: Add max limit (e.g., 50 segments = ~5 minutes)
|
Code Review - PR #1853: Thymia wellness analysis, avatar integration, and Deepgram Flux STT📊 OverviewThis PR adds significant new functionality with 86 files changed (+16,723/-51 lines):
Overall Assessment: Good code quality with strong security practices. However, 2 critical issues require immediate attention before merge. Critical Issues (Must Fix)1. Potential AttributeError in openai_llm2_python (openai.py:235)Location: ai_agents/agents/ten_packages/extension/openai_llm2_python/openai.py:235 Problem: No null check before calling .lower(). If self.config.model is None, this will raise AttributeError. Fix: Add defensive check: 2. Memory Leak in Video Track Management (View.tsx:20-28)Location: ai_agents/playground/src/components/Agent/View.tsx:20-28 Problem: When videoTrack changes from one track to another, the cleanup function stops the NEW track instead of the OLD track, leaving the old video element running. Fix: Track the user ID to ensure proper cleanup by depending on videoTrack?.getUserId() instead of just videoTrack. Medium Priority Issues (Should Address)3. Incomplete Reasoning Model Detection (openai.py:250-258)The current GPT-5 detection only checks for gpt-5 prefix, missing OpenAI existing reasoning models like o1-preview, o1-mini, and future o3 models. Should expand detection logic to include these models. 4. Race Condition in Worker Removal (worker.go:249,264)The workers.Remove(channelName) is called in multiple code paths without synchronization. If another goroutine tries to stop the same worker concurrently, it could lead to double-removal attempts or nil pointer dereferences. Add defensive check before removal. 5. Logging Level for Security-Relevant Operation (http_server.go:720)Uses Debug level for channel auto-injection, which is security-relevant and should be auditable. Recommend using Info level for audit trail. 6. Complex State Management in Deepgram ExtensionThe extension tracks 8+ state variables (extension.py:45-59), increasing cognitive complexity and bug risk. Recommend encapsulating into a state object using dataclasses. Low Priority / Nice to Have7. Magic Number in Graceful Shutdown (worker.go:244)Use named constants for timeout calculation instead of hardcoded 20. 8. Missing Test Coverage for Deepgram ExtensionThe new Deepgram WebSocket ASR extension lacks a tests/ directory, which is recommended by CLAUDE.md guidelines. 9. Incomplete Error Cleanup (worker.go:260)If SIGKILL fails, the worker is NOT removed from the map, potentially leaving a zombie entry. Positive FindingsSecurity
Code Quality
Architecture
SummaryTotal Issues Found: 10 (2 Critical, 5 Medium, 3 Low) Before Merging:
Final RecommendationConditional Approval: This is a well-architected PR with strong security practices. The 2 critical issues are straightforward to fix and must be addressed before merge. The medium-priority issues should be addressed to improve robustness and maintainability. The new features (avatar integration, wellness analysis, advanced ASR) are substantial additions that follow the framework patterns. Once the critical fixes are applied, this PR will be ready to merge. Reviewed by: Claude Code |
Pull Request Review: Feat/thymia anam flux v2This PR adds substantial new functionality including multiple new extensions, comprehensive documentation, and enhanced server capabilities. Below is a detailed analysis across code quality, security, performance, and testing dimensions. 📋 SummaryChanges Overview:
✅ Strengths1. Excellent DocumentationThe addition of
2. Security Improvements in Server (
|
Code Review: PR #1853OverviewThis PR adds significant functionality: comprehensive documentation, 5 new Python extensions, frontend video support, and server enhancements. Critical Issues (Must Fix)1. Security: API Key ExposureFile: thymia_analyzer_python/extension.py:376 2. Security: Hardcoded Bypass HeaderFile: anam_avatar_python/anam.py:156 3. Performance: Blocking HTTP CallsMultiple extensions (anam_avatar, generic_video, heygen_avatar) use blocking requests library in async code. Replace with aiohttp. 4. Race Condition: Worker MapFile: server/internal/worker.go:217-220 5. Type Safety IssueFile: playground/src/manager/rtc/types.ts:17 High Priority Issues
Positive Observations
RecommendationRequest changes to fix 5 critical issues (security, performance, correctness) before approval. Great work overall! |
Code Review - PR #1853📋 Executive SummaryThis PR adds significant new functionality with 5 new extensions, GPT-5 support, server improvements, and UI enhancements. The changes are substantial (16,790 additions across 85 files) and generally well-structured. However, I've identified 57 issues requiring attention before merge, including 9 security concerns and 15 error handling gaps. Overall Assessment: 🔴 Critical Issues (Must Fix Before Merge)1. Security: API Key Exposure in LogsSeverity: CRITICAL API keys are logged in plaintext which can lead to unauthorized access if logs are exposed. Fix: Mask sensitive data before logging. 2. Memory Leak: Unbounded Audio Buffer GrowthSeverity: CRITICAL AudioBuffer can grow to 9.6MB+ per session with no memory-based cap. Fix: Add memory-based limit to prevent resource exhaustion. 3. Command Injection VulnerabilitySeverity: CRITICAL Shell command constructed with unsanitized property file path. Fix: Use separate arguments to avoid shell expansion. 4. Race Condition: Concurrent Session AccessSeverity: HIGH Session creation and WebSocket connection lack synchronization. Fix: Add connection lock to prevent race conditions. 5. Error Handling: Silent API FailuresSeverity: HIGH API errors logged but not surfaced to users who wait indefinitely. Fix: Send error notification to users. 🟠 High Priority Issues6. Missing Input Validation for API ResponsesAPI responses accessed without checking field existence leading to potential KeyError crashes. 7. Resource Cleanup: WebSocket/Session LeaksWebSocket connections and sessions not cleaned up on errors. 8. No Retry Logic for Transient FailuresNetwork failures cause immediate abandonment without retries. 🟡 Medium Priority Issues
🟢 Positive Aspects
🧪 Test Coverage
📊 Summary StatisticsTotal: 57 issues (2 Critical, 14 High, 27 Medium, 14 Low) 🎯 Action Items - Before Merge
💡 Recommendations
Overall, this is a solid feature addition with good structure, but needs security and error handling improvements before production deployment. Reviewed with: TEN Framework CLAUDE.md guidelines, security best practices, Python async patterns |
No description provided.