feat: Issue #42 - Nexus Architect for autonomous Three.js world building
Implement Phase 31: Autonomous 'Nexus' Expansion & Architecture DELIVERABLES: - agent/nexus_architect.py: AI agent for natural language to Three.js conversion * Prompt engineering for LLM-driven immersive environment generation * Mental state integration for dynamic aesthetic tuning * Mood preset system (contemplative, energetic, mysterious, etc.) * Room and portal design generation - tools/nexus_build_tool.py: Build tool interface with functions: * create_room(name, description, style) - Generate room modules * create_portal(from_room, to_room, style) - Generate portal connections * add_lighting(room, type, color, intensity) - Add Three.js lighting * add_geometry(room, shape, position, material) - Add 3D objects * generate_scene_from_mood(mood_description) - Mood-based generation * deploy_nexus_module(module_code, test=True) - Deploy and test - agent/nexus_deployment.py: Real-time deployment system * Hot-reload Three.js modules without page refresh * Validation (syntax check, Three.js API compliance) * Rollback on error with version history * Module versioning and status tracking - config/nexus-templates/: Template library * base_room.js - Base room template (Three.js r128+) * portal_template.js - Portal template (circular, rectangular, stargate) * lighting_presets.json - Warm, cool, dramatic, serene, crystalline presets * material_presets.json - 15 material presets including Timmy's gold, Allegro blue - tests/test_nexus_architect.py: Comprehensive test coverage * Unit tests for all components * Integration tests for full workflow * Template file validation DESIGN PRINCIPLES: - Modular architecture (each room = separate JS module) - Valid Three.js code (r128+ compatible) - Hot-reloadable (no page refresh needed) - Mental state integration (SOUL.md values influence aesthetic) NEXUS AESTHETIC GUIDELINES: - Timmy's color: warm gold (#D4AF37) - Allegro's color: motion blue (#4A90E2) - Sovereignty theme: crystalline structures, clean lines - Service theme: open spaces, welcoming lighting - Default mood: contemplative, expansive, hopeful
This commit is contained in:
813
agent/nexus_architect.py
Normal file
813
agent/nexus_architect.py
Normal file
@@ -0,0 +1,813 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Nexus Architect AI Agent
|
||||
|
||||
Autonomous Three.js world generation system for Timmy's Nexus.
|
||||
Generates valid Three.js scene code from natural language descriptions
|
||||
and mental state integration.
|
||||
|
||||
This module provides:
|
||||
- LLM-driven immersive environment generation
|
||||
- Mental state integration for aesthetic tuning
|
||||
- Three.js code generation with validation
|
||||
- Scene composition from mood descriptions
|
||||
"""
|
||||
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
from typing import Dict, Any, List, Optional, Union
|
||||
from dataclasses import dataclass, field
|
||||
from enum import Enum
|
||||
import os
|
||||
import sys
|
||||
|
||||
# Add parent directory to path for imports
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Aesthetic Constants (from SOUL.md values)
|
||||
# =============================================================================
|
||||
|
||||
class NexusColors:
|
||||
"""Nexus color palette based on SOUL.md values."""
|
||||
TIMMY_GOLD = "#D4AF37" # Warm gold
|
||||
ALLEGRO_BLUE = "#4A90E2" # Motion blue
|
||||
SOVEREIGNTY_CRYSTAL = "#E0F7FA" # Crystalline structures
|
||||
SERVICE_WARMTH = "#FFE4B5" # Welcoming warmth
|
||||
DEFAULT_AMBIENT = "#1A1A2E" # Contemplative dark
|
||||
HOPE_ACCENT = "#64B5F6" # Hopeful blue
|
||||
|
||||
|
||||
class MoodPresets:
|
||||
"""Mood-based aesthetic presets."""
|
||||
|
||||
CONTEMPLATIVE = {
|
||||
"lighting": "soft_diffuse",
|
||||
"colors": ["#1A1A2E", "#16213E", "#0F3460"],
|
||||
"geometry": "minimalist",
|
||||
"atmosphere": "calm",
|
||||
"description": "A serene space for deep reflection and clarity"
|
||||
}
|
||||
|
||||
ENERGETIC = {
|
||||
"lighting": "dynamic_vivid",
|
||||
"colors": ["#D4AF37", "#FF6B6B", "#4ECDC4"],
|
||||
"geometry": "angular_dynamic",
|
||||
"atmosphere": "lively",
|
||||
"description": "An invigorating space full of motion and possibility"
|
||||
}
|
||||
|
||||
MYSTERIOUS = {
|
||||
"lighting": "dramatic_shadows",
|
||||
"colors": ["#2C003E", "#512B58", "#8B4F80"],
|
||||
"geometry": "organic_flowing",
|
||||
"atmosphere": "enigmatic",
|
||||
"description": "A mysterious realm of discovery and wonder"
|
||||
}
|
||||
|
||||
WELCOMING = {
|
||||
"lighting": "warm_inviting",
|
||||
"colors": ["#FFE4B5", "#FFA07A", "#98D8C8"],
|
||||
"geometry": "rounded_soft",
|
||||
"atmosphere": "friendly",
|
||||
"description": "An open, welcoming space that embraces visitors"
|
||||
}
|
||||
|
||||
SOVEREIGN = {
|
||||
"lighting": "crystalline_clear",
|
||||
"colors": ["#E0F7FA", "#B2EBF2", "#4DD0E1"],
|
||||
"geometry": "crystalline_structures",
|
||||
"atmosphere": "noble",
|
||||
"description": "A space of crystalline clarity and sovereign purpose"
|
||||
}
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Data Models
|
||||
# =============================================================================
|
||||
|
||||
@dataclass
|
||||
class MentalState:
|
||||
"""Timmy's mental state for aesthetic tuning."""
|
||||
mood: str = "contemplative" # contemplative, energetic, mysterious, welcoming, sovereign
|
||||
energy_level: float = 0.5 # 0.0 to 1.0
|
||||
clarity: float = 0.7 # 0.0 to 1.0
|
||||
focus_area: str = "general" # general, creative, analytical, social
|
||||
timestamp: Optional[str] = None
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
return {
|
||||
"mood": self.mood,
|
||||
"energy_level": self.energy_level,
|
||||
"clarity": self.clarity,
|
||||
"focus_area": self.focus_area,
|
||||
"timestamp": self.timestamp,
|
||||
}
|
||||
|
||||
|
||||
@dataclass
|
||||
class RoomDesign:
|
||||
"""Complete room design specification."""
|
||||
name: str
|
||||
description: str
|
||||
style: str
|
||||
dimensions: Dict[str, float] = field(default_factory=lambda: {"width": 20, "height": 10, "depth": 20})
|
||||
mood_preset: str = "contemplative"
|
||||
color_palette: List[str] = field(default_factory=list)
|
||||
lighting_scheme: str = "soft_diffuse"
|
||||
features: List[str] = field(default_factory=list)
|
||||
generated_code: Optional[str] = None
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
return {
|
||||
"name": self.name,
|
||||
"description": self.description,
|
||||
"style": self.style,
|
||||
"dimensions": self.dimensions,
|
||||
"mood_preset": self.mood_preset,
|
||||
"color_palette": self.color_palette,
|
||||
"lighting_scheme": self.lighting_scheme,
|
||||
"features": self.features,
|
||||
"has_code": self.generated_code is not None,
|
||||
}
|
||||
|
||||
|
||||
@dataclass
|
||||
class PortalDesign:
|
||||
"""Portal connection design."""
|
||||
name: str
|
||||
from_room: str
|
||||
to_room: str
|
||||
style: str
|
||||
position: Dict[str, float] = field(default_factory=lambda: {"x": 0, "y": 0, "z": 0})
|
||||
visual_effect: str = "energy_swirl"
|
||||
transition_duration: float = 1.5
|
||||
generated_code: Optional[str] = None
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
return {
|
||||
"name": self.name,
|
||||
"from_room": self.from_room,
|
||||
"to_room": self.to_room,
|
||||
"style": self.style,
|
||||
"position": self.position,
|
||||
"visual_effect": self.visual_effect,
|
||||
"transition_duration": self.transition_duration,
|
||||
"has_code": self.generated_code is not None,
|
||||
}
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Prompt Engineering
|
||||
# =============================================================================
|
||||
|
||||
class PromptEngineer:
|
||||
"""Engineers prompts for Three.js code generation."""
|
||||
|
||||
THREE_JS_BASE_TEMPLATE = """// Nexus Room Module: {room_name}
|
||||
// Style: {style}
|
||||
// Mood: {mood}
|
||||
// Generated for Three.js r128+
|
||||
|
||||
(function() {{
|
||||
'use strict';
|
||||
|
||||
// Room Configuration
|
||||
const config = {{
|
||||
name: "{room_name}",
|
||||
dimensions: {dimensions_json},
|
||||
colors: {colors_json},
|
||||
mood: "{mood}"
|
||||
}};
|
||||
|
||||
// Create Room Function
|
||||
function create{room_name_camel}() {{
|
||||
const roomGroup = new THREE.Group();
|
||||
roomGroup.name = config.name;
|
||||
|
||||
{room_content}
|
||||
|
||||
return roomGroup;
|
||||
}}
|
||||
|
||||
// Export for Nexus
|
||||
if (typeof module !== 'undefined' && module.exports) {{
|
||||
module.exports = {{ create{room_name_camel} }};
|
||||
}} else if (typeof window !== 'undefined') {{
|
||||
window.NexusRooms = window.NexusRooms || {{}};
|
||||
window.NexusRooms.{room_name} = create{room_name_camel};
|
||||
}}
|
||||
|
||||
return {{ create{room_name_camel} }};
|
||||
}})();"""
|
||||
|
||||
@staticmethod
|
||||
def engineer_room_prompt(
|
||||
name: str,
|
||||
description: str,
|
||||
style: str,
|
||||
mental_state: Optional[MentalState] = None,
|
||||
dimensions: Optional[Dict[str, float]] = None
|
||||
) -> str:
|
||||
"""
|
||||
Engineer an LLM prompt for room generation.
|
||||
|
||||
Args:
|
||||
name: Room identifier
|
||||
description: Natural language room description
|
||||
style: Visual style
|
||||
mental_state: Timmy's current mental state
|
||||
dimensions: Room dimensions
|
||||
"""
|
||||
# Determine mood from mental state or description
|
||||
mood = PromptEngineer._infer_mood(description, mental_state)
|
||||
mood_preset = getattr(MoodPresets, mood.upper(), MoodPresets.CONTEMPLATIVE)
|
||||
|
||||
# Build color palette
|
||||
color_palette = mood_preset["colors"]
|
||||
if mental_state:
|
||||
# Add Timmy's gold for high clarity states
|
||||
if mental_state.clarity > 0.7:
|
||||
color_palette = [NexusColors.TIMMY_GOLD] + color_palette[:2]
|
||||
# Add Allegro blue for creative focus
|
||||
if mental_state.focus_area == "creative":
|
||||
color_palette = [NexusColors.ALLEGRO_BLUE] + color_palette[:2]
|
||||
|
||||
# Create the engineering prompt
|
||||
prompt = f"""You are the Nexus Architect, an expert Three.js developer creating immersive 3D environments for Timmy.
|
||||
|
||||
DESIGN BRIEF:
|
||||
- Room Name: {name}
|
||||
- Description: {description}
|
||||
- Style: {style}
|
||||
- Mood: {mood}
|
||||
- Atmosphere: {mood_preset['atmosphere']}
|
||||
|
||||
AESTHETIC GUIDELINES:
|
||||
- Primary Colors: {', '.join(color_palette[:3])}
|
||||
- Lighting: {mood_preset['lighting']}
|
||||
- Geometry: {mood_preset['geometry']}
|
||||
- Theme: {mood_preset['description']}
|
||||
|
||||
TIMMY'S CONTEXT:
|
||||
- Timmy's Signature Color: Warm Gold ({NexusColors.TIMMY_GOLD})
|
||||
- Allegro's Color: Motion Blue ({NexusColors.ALLEGRO_BLUE})
|
||||
- Sovereignty Theme: Crystalline structures, clean lines
|
||||
- Service Theme: Open spaces, welcoming lighting
|
||||
|
||||
THREE.JS REQUIREMENTS:
|
||||
1. Use Three.js r128+ compatible syntax
|
||||
2. Create a self-contained module with a `create{name.title().replace('_', '')}()` function
|
||||
3. Return a THREE.Group containing all room elements
|
||||
4. Include proper memory management (dispose methods)
|
||||
5. Use MeshStandardMaterial for PBR lighting
|
||||
6. Include ambient light (intensity 0.3-0.5) + accent lights
|
||||
7. Add subtle animations for living feel
|
||||
8. Keep polygon count under 10,000 triangles
|
||||
|
||||
SAFETY RULES:
|
||||
- NO eval(), Function(), or dynamic code execution
|
||||
- NO network requests (fetch, XMLHttpRequest, WebSocket)
|
||||
- NO storage access (localStorage, sessionStorage, cookies)
|
||||
- NO navigation (window.location, window.open)
|
||||
- Only use allowed Three.js APIs
|
||||
|
||||
OUTPUT FORMAT:
|
||||
Return ONLY the JavaScript code wrapped in a markdown code block:
|
||||
|
||||
```javascript
|
||||
// Your Three.js room module here
|
||||
```
|
||||
|
||||
Generate the complete Three.js code for this room now."""
|
||||
|
||||
return prompt
|
||||
|
||||
@staticmethod
|
||||
def engineer_portal_prompt(
|
||||
name: str,
|
||||
from_room: str,
|
||||
to_room: str,
|
||||
style: str,
|
||||
mental_state: Optional[MentalState] = None
|
||||
) -> str:
|
||||
"""Engineer a prompt for portal generation."""
|
||||
mood = PromptEngineer._infer_mood(f"portal from {from_room} to {to_room}", mental_state)
|
||||
|
||||
prompt = f"""You are creating a portal connection in the Nexus 3D environment.
|
||||
|
||||
PORTAL SPECIFICATIONS:
|
||||
- Name: {name}
|
||||
- Connection: {from_room} → {to_room}
|
||||
- Style: {style}
|
||||
- Context Mood: {mood}
|
||||
|
||||
VISUAL REQUIREMENTS:
|
||||
1. Create an animated portal effect (shader or texture-based)
|
||||
2. Include particle system for energy flow
|
||||
3. Add trigger zone for teleportation detection
|
||||
4. Use signature colors: {NexusColors.TIMMY_GOLD} (Timmy) and {NexusColors.ALLEGRO_BLUE} (Allegro)
|
||||
5. Match the {mood} atmosphere
|
||||
|
||||
TECHNICAL REQUIREMENTS:
|
||||
- Three.js r128+ compatible
|
||||
- Export a `createPortal()` function returning THREE.Group
|
||||
- Include animation loop hook
|
||||
- Add collision detection placeholder
|
||||
|
||||
SAFETY: No eval, no network requests, no external dependencies.
|
||||
|
||||
Return ONLY JavaScript code in a markdown code block."""
|
||||
|
||||
return prompt
|
||||
|
||||
@staticmethod
|
||||
def engineer_mood_scene_prompt(mood_description: str) -> str:
|
||||
"""Engineer a prompt based on mood description."""
|
||||
# Analyze mood description
|
||||
mood_keywords = {
|
||||
"contemplative": ["thinking", "reflective", "calm", "peaceful", "quiet", "serene"],
|
||||
"energetic": ["excited", "dynamic", "lively", "active", "energetic", "vibrant"],
|
||||
"mysterious": ["mysterious", "dark", "unknown", "secret", "enigmatic"],
|
||||
"welcoming": ["friendly", "open", "warm", "welcoming", "inviting", "comfortable"],
|
||||
"sovereign": ["powerful", "clear", "crystalline", "noble", "dignified"],
|
||||
}
|
||||
|
||||
detected_mood = "contemplative"
|
||||
desc_lower = mood_description.lower()
|
||||
for mood, keywords in mood_keywords.items():
|
||||
if any(kw in desc_lower for kw in keywords):
|
||||
detected_mood = mood
|
||||
break
|
||||
|
||||
preset = getattr(MoodPresets, detected_mood.upper(), MoodPresets.CONTEMPLATIVE)
|
||||
|
||||
prompt = f"""Generate a Three.js room based on this mood description:
|
||||
|
||||
"{mood_description}"
|
||||
|
||||
INFERRED MOOD: {detected_mood}
|
||||
AESTHETIC: {preset['description']}
|
||||
|
||||
Create a complete room with:
|
||||
- Style: {preset['geometry']}
|
||||
- Lighting: {preset['lighting']}
|
||||
- Color Palette: {', '.join(preset['colors'][:3])}
|
||||
- Atmosphere: {preset['atmosphere']}
|
||||
|
||||
Return Three.js r128+ code as a module with `createMoodRoom()` function."""
|
||||
|
||||
return prompt
|
||||
|
||||
@staticmethod
|
||||
def _infer_mood(description: str, mental_state: Optional[MentalState] = None) -> str:
|
||||
"""Infer mood from description and mental state."""
|
||||
if mental_state and mental_state.mood:
|
||||
return mental_state.mood
|
||||
|
||||
desc_lower = description.lower()
|
||||
mood_map = {
|
||||
"contemplative": ["serene", "calm", "peaceful", "quiet", "meditation", "zen", "tranquil"],
|
||||
"energetic": ["dynamic", "active", "vibrant", "lively", "energetic", "motion"],
|
||||
"mysterious": ["mysterious", "shadow", "dark", "unknown", "secret", "ethereal"],
|
||||
"welcoming": ["warm", "welcoming", "friendly", "open", "inviting", "comfort"],
|
||||
"sovereign": ["crystal", "clear", "noble", "dignified", "powerful", "authoritative"],
|
||||
}
|
||||
|
||||
for mood, keywords in mood_map.items():
|
||||
if any(kw in desc_lower for kw in keywords):
|
||||
return mood
|
||||
|
||||
return "contemplative"
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Nexus Architect AI
|
||||
# =============================================================================
|
||||
|
||||
class NexusArchitectAI:
|
||||
"""
|
||||
AI-powered Nexus Architect for autonomous Three.js world generation.
|
||||
|
||||
This class provides high-level interfaces for:
|
||||
- Designing rooms from natural language
|
||||
- Creating mood-based scenes
|
||||
- Managing mental state integration
|
||||
- Validating generated code
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self.mental_state: Optional[MentalState] = None
|
||||
self.room_designs: Dict[str, RoomDesign] = {}
|
||||
self.portal_designs: Dict[str, PortalDesign] = {}
|
||||
self.prompt_engineer = PromptEngineer()
|
||||
|
||||
def set_mental_state(self, state: MentalState) -> None:
|
||||
"""Set Timmy's current mental state for aesthetic tuning."""
|
||||
self.mental_state = state
|
||||
logger.info(f"Mental state updated: {state.mood} (energy: {state.energy_level})")
|
||||
|
||||
def design_room(
|
||||
self,
|
||||
name: str,
|
||||
description: str,
|
||||
style: str,
|
||||
dimensions: Optional[Dict[str, float]] = None
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Design a room from natural language description.
|
||||
|
||||
Args:
|
||||
name: Room identifier (e.g., "contemplation_chamber")
|
||||
description: Natural language description of the room
|
||||
style: Visual style (e.g., "minimalist_ethereal", "crystalline_modern")
|
||||
dimensions: Optional room dimensions
|
||||
|
||||
Returns:
|
||||
Dict containing design specification and LLM prompt
|
||||
"""
|
||||
# Infer mood and select preset
|
||||
mood = self.prompt_engineer._infer_mood(description, self.mental_state)
|
||||
mood_preset = getattr(MoodPresets, mood.upper(), MoodPresets.CONTEMPLATIVE)
|
||||
|
||||
# Build color palette with mental state influence
|
||||
colors = mood_preset["colors"].copy()
|
||||
if self.mental_state:
|
||||
if self.mental_state.clarity > 0.7:
|
||||
colors.insert(0, NexusColors.TIMMY_GOLD)
|
||||
if self.mental_state.focus_area == "creative":
|
||||
colors.insert(0, NexusColors.ALLEGRO_BLUE)
|
||||
|
||||
# Create room design
|
||||
design = RoomDesign(
|
||||
name=name,
|
||||
description=description,
|
||||
style=style,
|
||||
dimensions=dimensions or {"width": 20, "height": 10, "depth": 20},
|
||||
mood_preset=mood,
|
||||
color_palette=colors[:4],
|
||||
lighting_scheme=mood_preset["lighting"],
|
||||
features=self._extract_features(description),
|
||||
)
|
||||
|
||||
# Generate LLM prompt
|
||||
prompt = self.prompt_engineer.engineer_room_prompt(
|
||||
name=name,
|
||||
description=description,
|
||||
style=style,
|
||||
mental_state=self.mental_state,
|
||||
dimensions=design.dimensions,
|
||||
)
|
||||
|
||||
# Store design
|
||||
self.room_designs[name] = design
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"room_name": name,
|
||||
"design": design.to_dict(),
|
||||
"llm_prompt": prompt,
|
||||
"message": f"Room '{name}' designed. Use the LLM prompt to generate Three.js code.",
|
||||
}
|
||||
|
||||
def create_portal(
|
||||
self,
|
||||
name: str,
|
||||
from_room: str,
|
||||
to_room: str,
|
||||
style: str = "energy_vortex"
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Design a portal connection between rooms.
|
||||
|
||||
Args:
|
||||
name: Portal identifier
|
||||
from_room: Source room name
|
||||
to_room: Target room name
|
||||
style: Portal visual style
|
||||
|
||||
Returns:
|
||||
Dict containing portal design and LLM prompt
|
||||
"""
|
||||
if from_room not in self.room_designs:
|
||||
return {"success": False, "error": f"Source room '{from_room}' not found"}
|
||||
if to_room not in self.room_designs:
|
||||
return {"success": False, "error": f"Target room '{to_room}' not found"}
|
||||
|
||||
design = PortalDesign(
|
||||
name=name,
|
||||
from_room=from_room,
|
||||
to_room=to_room,
|
||||
style=style,
|
||||
)
|
||||
|
||||
prompt = self.prompt_engineer.engineer_portal_prompt(
|
||||
name=name,
|
||||
from_room=from_room,
|
||||
to_room=to_room,
|
||||
style=style,
|
||||
mental_state=self.mental_state,
|
||||
)
|
||||
|
||||
self.portal_designs[name] = design
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"portal_name": name,
|
||||
"design": design.to_dict(),
|
||||
"llm_prompt": prompt,
|
||||
"message": f"Portal '{name}' designed connecting {from_room} to {to_room}",
|
||||
}
|
||||
|
||||
def generate_scene_from_mood(self, mood_description: str) -> Dict[str, Any]:
|
||||
"""
|
||||
Generate a complete scene based on mood description.
|
||||
|
||||
Args:
|
||||
mood_description: Description of desired mood/atmosphere
|
||||
|
||||
Returns:
|
||||
Dict containing scene design and LLM prompt
|
||||
"""
|
||||
# Infer mood
|
||||
mood = self.prompt_engineer._infer_mood(mood_description, self.mental_state)
|
||||
preset = getattr(MoodPresets, mood.upper(), MoodPresets.CONTEMPLATIVE)
|
||||
|
||||
# Create room name from mood
|
||||
room_name = f"{mood}_realm"
|
||||
|
||||
# Generate prompt
|
||||
prompt = self.prompt_engineer.engineer_mood_scene_prompt(mood_description)
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"room_name": room_name,
|
||||
"inferred_mood": mood,
|
||||
"aesthetic": preset,
|
||||
"llm_prompt": prompt,
|
||||
"message": f"Generated {mood} scene from mood description",
|
||||
}
|
||||
|
||||
def _extract_features(self, description: str) -> List[str]:
|
||||
"""Extract room features from description."""
|
||||
features = []
|
||||
feature_keywords = {
|
||||
"floating": ["floating", "levitating", "hovering"],
|
||||
"water": ["water", "fountain", "pool", "stream", "lake"],
|
||||
"vegetation": ["tree", "plant", "garden", "forest", "nature"],
|
||||
"crystals": ["crystal", "gem", "prism", "diamond"],
|
||||
"geometry": ["geometric", "shape", "sphere", "cube", "abstract"],
|
||||
"particles": ["particle", "dust", "sparkle", "glow", "mist"],
|
||||
}
|
||||
|
||||
desc_lower = description.lower()
|
||||
for feature, keywords in feature_keywords.items():
|
||||
if any(kw in desc_lower for kw in keywords):
|
||||
features.append(feature)
|
||||
|
||||
return features
|
||||
|
||||
def get_design_summary(self) -> Dict[str, Any]:
|
||||
"""Get summary of all designs."""
|
||||
return {
|
||||
"mental_state": self.mental_state.to_dict() if self.mental_state else None,
|
||||
"rooms": {name: design.to_dict() for name, design in self.room_designs.items()},
|
||||
"portals": {name: portal.to_dict() for name, portal in self.portal_designs.items()},
|
||||
"total_rooms": len(self.room_designs),
|
||||
"total_portals": len(self.portal_designs),
|
||||
}
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Module-level functions for easy import
|
||||
# =============================================================================
|
||||
|
||||
_architect_instance: Optional[NexusArchitectAI] = None
|
||||
|
||||
|
||||
def get_architect() -> NexusArchitectAI:
|
||||
"""Get or create the NexusArchitectAI singleton."""
|
||||
global _architect_instance
|
||||
if _architect_instance is None:
|
||||
_architect_instance = NexusArchitectAI()
|
||||
return _architect_instance
|
||||
|
||||
|
||||
def create_room(
|
||||
name: str,
|
||||
description: str,
|
||||
style: str,
|
||||
dimensions: Optional[Dict[str, float]] = None
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Create a room design from description.
|
||||
|
||||
Args:
|
||||
name: Room identifier
|
||||
description: Natural language room description
|
||||
style: Visual style (e.g., "minimalist_ethereal")
|
||||
dimensions: Optional dimensions dict with width, height, depth
|
||||
|
||||
Returns:
|
||||
Dict with design specification and LLM prompt for code generation
|
||||
"""
|
||||
architect = get_architect()
|
||||
return architect.design_room(name, description, style, dimensions)
|
||||
|
||||
|
||||
def create_portal(
|
||||
name: str,
|
||||
from_room: str,
|
||||
to_room: str,
|
||||
style: str = "energy_vortex"
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Create a portal between rooms.
|
||||
|
||||
Args:
|
||||
name: Portal identifier
|
||||
from_room: Source room name
|
||||
to_room: Target room name
|
||||
style: Visual style
|
||||
|
||||
Returns:
|
||||
Dict with portal design and LLM prompt
|
||||
"""
|
||||
architect = get_architect()
|
||||
return architect.create_portal(name, from_room, to_room, style)
|
||||
|
||||
|
||||
def generate_scene_from_mood(mood_description: str) -> Dict[str, Any]:
|
||||
"""
|
||||
Generate a scene based on mood description.
|
||||
|
||||
Args:
|
||||
mood_description: Description of desired mood
|
||||
|
||||
Example:
|
||||
"Timmy is feeling introspective and seeking clarity"
|
||||
→ Generates calm, minimalist space with clear sightlines
|
||||
|
||||
Returns:
|
||||
Dict with scene design and LLM prompt
|
||||
"""
|
||||
architect = get_architect()
|
||||
return architect.generate_scene_from_mood(mood_description)
|
||||
|
||||
|
||||
def set_mental_state(
|
||||
mood: str,
|
||||
energy_level: float = 0.5,
|
||||
clarity: float = 0.7,
|
||||
focus_area: str = "general"
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Set Timmy's mental state for aesthetic tuning.
|
||||
|
||||
Args:
|
||||
mood: Current mood (contemplative, energetic, mysterious, welcoming, sovereign)
|
||||
energy_level: 0.0 to 1.0
|
||||
clarity: 0.0 to 1.0
|
||||
focus_area: general, creative, analytical, social
|
||||
|
||||
Returns:
|
||||
Confirmation dict
|
||||
"""
|
||||
architect = get_architect()
|
||||
state = MentalState(
|
||||
mood=mood,
|
||||
energy_level=energy_level,
|
||||
clarity=clarity,
|
||||
focus_area=focus_area,
|
||||
)
|
||||
architect.set_mental_state(state)
|
||||
return {
|
||||
"success": True,
|
||||
"mental_state": state.to_dict(),
|
||||
"message": f"Mental state set to {mood}",
|
||||
}
|
||||
|
||||
|
||||
def get_nexus_summary() -> Dict[str, Any]:
|
||||
"""Get summary of all Nexus designs."""
|
||||
architect = get_architect()
|
||||
return architect.get_design_summary()
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Tool Schemas for integration
|
||||
# =============================================================================
|
||||
|
||||
NEXUS_ARCHITECT_AI_SCHEMAS = {
|
||||
"create_room": {
|
||||
"name": "create_room",
|
||||
"description": (
|
||||
"Design a new 3D room in the Nexus from a natural language description. "
|
||||
"Returns a design specification and LLM prompt for Three.js code generation. "
|
||||
"The room will be styled according to Timmy's current mental state."
|
||||
),
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": {
|
||||
"type": "string",
|
||||
"description": "Unique room identifier (e.g., 'contemplation_chamber')"
|
||||
},
|
||||
"description": {
|
||||
"type": "string",
|
||||
"description": "Natural language description of the room"
|
||||
},
|
||||
"style": {
|
||||
"type": "string",
|
||||
"description": "Visual style (minimalist_ethereal, crystalline_modern, organic_natural, etc.)"
|
||||
},
|
||||
"dimensions": {
|
||||
"type": "object",
|
||||
"description": "Optional room dimensions",
|
||||
"properties": {
|
||||
"width": {"type": "number"},
|
||||
"height": {"type": "number"},
|
||||
"depth": {"type": "number"},
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": ["name", "description", "style"]
|
||||
}
|
||||
},
|
||||
"create_portal": {
|
||||
"name": "create_portal",
|
||||
"description": "Create a portal connection between two rooms",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": {"type": "string"},
|
||||
"from_room": {"type": "string"},
|
||||
"to_room": {"type": "string"},
|
||||
"style": {"type": "string", "default": "energy_vortex"},
|
||||
},
|
||||
"required": ["name", "from_room", "to_room"]
|
||||
}
|
||||
},
|
||||
"generate_scene_from_mood": {
|
||||
"name": "generate_scene_from_mood",
|
||||
"description": (
|
||||
"Generate a complete 3D scene based on a mood description. "
|
||||
"Example: 'Timmy is feeling introspective' creates a calm, minimalist space."
|
||||
),
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"mood_description": {
|
||||
"type": "string",
|
||||
"description": "Description of desired mood or mental state"
|
||||
}
|
||||
},
|
||||
"required": ["mood_description"]
|
||||
}
|
||||
},
|
||||
"set_mental_state": {
|
||||
"name": "set_mental_state",
|
||||
"description": "Set Timmy's mental state to influence aesthetic generation",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"mood": {"type": "string"},
|
||||
"energy_level": {"type": "number"},
|
||||
"clarity": {"type": "number"},
|
||||
"focus_area": {"type": "string"},
|
||||
},
|
||||
"required": ["mood"]
|
||||
}
|
||||
},
|
||||
"get_nexus_summary": {
|
||||
"name": "get_nexus_summary",
|
||||
"description": "Get summary of all Nexus room and portal designs",
|
||||
"parameters": {"type": "object", "properties": {}}
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Demo usage
|
||||
print("Nexus Architect AI - Demo")
|
||||
print("=" * 50)
|
||||
|
||||
# Set mental state
|
||||
result = set_mental_state("contemplative", energy_level=0.3, clarity=0.8)
|
||||
print(f"\nMental State: {result['mental_state']}")
|
||||
|
||||
# Create a room
|
||||
result = create_room(
|
||||
name="contemplation_chamber",
|
||||
description="A serene circular room with floating geometric shapes and soft blue light",
|
||||
style="minimalist_ethereal",
|
||||
)
|
||||
print(f"\nRoom Design: {json.dumps(result['design'], indent=2)}")
|
||||
|
||||
# Generate from mood
|
||||
result = generate_scene_from_mood("Timmy is feeling introspective and seeking clarity")
|
||||
print(f"\nMood Scene: {result['inferred_mood']} - {result['aesthetic']['description']}")
|
||||
752
agent/nexus_deployment.py
Normal file
752
agent/nexus_deployment.py
Normal file
@@ -0,0 +1,752 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Nexus Deployment System
|
||||
|
||||
Real-time deployment system for Nexus Three.js modules.
|
||||
Provides hot-reload, validation, rollback, and versioning capabilities.
|
||||
|
||||
Features:
|
||||
- Hot-reload Three.js modules without page refresh
|
||||
- Syntax validation and Three.js API compliance checking
|
||||
- Rollback on error
|
||||
- Versioning for nexus modules
|
||||
- Module registry and dependency tracking
|
||||
|
||||
Usage:
|
||||
from agent.nexus_deployment import NexusDeployer
|
||||
|
||||
deployer = NexusDeployer()
|
||||
|
||||
# Deploy with hot-reload
|
||||
result = deployer.deploy_module(room_code, module_name="zen_garden")
|
||||
|
||||
# Rollback if needed
|
||||
deployer.rollback_module("zen_garden")
|
||||
|
||||
# Get module status
|
||||
status = deployer.get_module_status("zen_garden")
|
||||
"""
|
||||
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
import os
|
||||
import hashlib
|
||||
from typing import Dict, Any, List, Optional, Set
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime
|
||||
from enum import Enum
|
||||
|
||||
# Import validation from existing nexus_architect (avoid circular imports)
|
||||
import sys
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
def _import_validation():
|
||||
"""Lazy import to avoid circular dependencies."""
|
||||
try:
|
||||
from tools.nexus_architect import validate_three_js_code, sanitize_three_js_code
|
||||
return validate_three_js_code, sanitize_three_js_code
|
||||
except ImportError:
|
||||
# Fallback: define local validation functions
|
||||
def validate_three_js_code(code, strict_mode=False):
|
||||
"""Fallback validation."""
|
||||
errors = []
|
||||
if "eval(" in code:
|
||||
errors.append("Security violation: eval detected")
|
||||
if "Function(" in code:
|
||||
errors.append("Security violation: Function constructor detected")
|
||||
return type('ValidationResult', (), {
|
||||
'is_valid': len(errors) == 0,
|
||||
'errors': errors,
|
||||
'warnings': []
|
||||
})()
|
||||
|
||||
def sanitize_three_js_code(code):
|
||||
"""Fallback sanitization."""
|
||||
return code
|
||||
|
||||
return validate_three_js_code, sanitize_three_js_code
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Deployment States
|
||||
# =============================================================================
|
||||
|
||||
class DeploymentStatus(Enum):
|
||||
"""Status of a module deployment."""
|
||||
PENDING = "pending"
|
||||
VALIDATING = "validating"
|
||||
DEPLOYING = "deploying"
|
||||
ACTIVE = "active"
|
||||
FAILED = "failed"
|
||||
ROLLING_BACK = "rolling_back"
|
||||
ROLLED_BACK = "rolled_back"
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Data Models
|
||||
# =============================================================================
|
||||
|
||||
@dataclass
|
||||
class ModuleVersion:
|
||||
"""Version information for a Nexus module."""
|
||||
version_id: str
|
||||
module_name: str
|
||||
code_hash: str
|
||||
timestamp: str
|
||||
changes: str = ""
|
||||
author: str = "nexus_architect"
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
return {
|
||||
"version_id": self.version_id,
|
||||
"module_name": self.module_name,
|
||||
"code_hash": self.code_hash,
|
||||
"timestamp": self.timestamp,
|
||||
"changes": self.changes,
|
||||
"author": self.author,
|
||||
}
|
||||
|
||||
|
||||
@dataclass
|
||||
class DeployedModule:
|
||||
"""A deployed Nexus module."""
|
||||
name: str
|
||||
code: str
|
||||
status: DeploymentStatus
|
||||
version: str
|
||||
deployed_at: str
|
||||
last_updated: str
|
||||
validation_result: Dict[str, Any] = field(default_factory=dict)
|
||||
error_log: List[str] = field(default_factory=list)
|
||||
dependencies: Set[str] = field(default_factory=set)
|
||||
hot_reload_supported: bool = True
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
return {
|
||||
"name": self.name,
|
||||
"status": self.status.value,
|
||||
"version": self.version,
|
||||
"deployed_at": self.deployed_at,
|
||||
"last_updated": self.last_updated,
|
||||
"validation": self.validation_result,
|
||||
"dependencies": list(self.dependencies),
|
||||
"hot_reload_supported": self.hot_reload_supported,
|
||||
"code_preview": self.code[:200] + "..." if len(self.code) > 200 else self.code,
|
||||
}
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Nexus Deployer
|
||||
# =============================================================================
|
||||
|
||||
class NexusDeployer:
|
||||
"""
|
||||
Deployment system for Nexus Three.js modules.
|
||||
|
||||
Provides:
|
||||
- Hot-reload deployment
|
||||
- Validation before deployment
|
||||
- Automatic rollback on failure
|
||||
- Version tracking
|
||||
- Module registry
|
||||
"""
|
||||
|
||||
def __init__(self, modules_dir: Optional[str] = None):
|
||||
"""
|
||||
Initialize the Nexus Deployer.
|
||||
|
||||
Args:
|
||||
modules_dir: Directory to store deployed modules (optional)
|
||||
"""
|
||||
self.modules: Dict[str, DeployedModule] = {}
|
||||
self.version_history: Dict[str, List[ModuleVersion]] = {}
|
||||
self.modules_dir = modules_dir or os.path.expanduser("~/.nexus/modules")
|
||||
|
||||
# Ensure modules directory exists
|
||||
os.makedirs(self.modules_dir, exist_ok=True)
|
||||
|
||||
# Hot-reload configuration
|
||||
self.hot_reload_enabled = True
|
||||
self.auto_rollback = True
|
||||
self.strict_validation = True
|
||||
|
||||
logger.info(f"NexusDeployer initialized. Modules dir: {self.modules_dir}")
|
||||
|
||||
def deploy_module(
|
||||
self,
|
||||
module_code: str,
|
||||
module_name: str,
|
||||
version: Optional[str] = None,
|
||||
dependencies: Optional[List[str]] = None,
|
||||
hot_reload: bool = True,
|
||||
validate: bool = True
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Deploy a Nexus module with hot-reload support.
|
||||
|
||||
Args:
|
||||
module_code: The Three.js module code
|
||||
module_name: Unique module identifier
|
||||
version: Optional version string (auto-generated if not provided)
|
||||
dependencies: List of dependent module names
|
||||
hot_reload: Enable hot-reload for this module
|
||||
validate: Run validation before deployment
|
||||
|
||||
Returns:
|
||||
Dict with deployment results
|
||||
"""
|
||||
timestamp = datetime.now().isoformat()
|
||||
version = version or self._generate_version(module_name, module_code)
|
||||
|
||||
result = {
|
||||
"success": True,
|
||||
"module_name": module_name,
|
||||
"version": version,
|
||||
"timestamp": timestamp,
|
||||
"hot_reload": hot_reload,
|
||||
"validation": {},
|
||||
"deployment": {},
|
||||
}
|
||||
|
||||
# Check for existing module (hot-reload scenario)
|
||||
existing_module = self.modules.get(module_name)
|
||||
if existing_module and not hot_reload:
|
||||
return {
|
||||
"success": False,
|
||||
"error": f"Module '{module_name}' already exists. Use hot_reload=True to update."
|
||||
}
|
||||
|
||||
# Validation phase
|
||||
if validate:
|
||||
validation = self._validate_module(module_code)
|
||||
result["validation"] = validation
|
||||
|
||||
if not validation["is_valid"]:
|
||||
result["success"] = False
|
||||
result["error"] = "Validation failed"
|
||||
result["message"] = "Module deployment aborted due to validation errors"
|
||||
|
||||
if self.auto_rollback:
|
||||
result["rollback_triggered"] = False # Nothing to rollback yet
|
||||
|
||||
return result
|
||||
|
||||
# Create deployment backup for rollback
|
||||
if existing_module:
|
||||
self._create_backup(existing_module)
|
||||
|
||||
# Deployment phase
|
||||
try:
|
||||
deployed = DeployedModule(
|
||||
name=module_name,
|
||||
code=module_code,
|
||||
status=DeploymentStatus.DEPLOYING,
|
||||
version=version,
|
||||
deployed_at=timestamp if not existing_module else existing_module.deployed_at,
|
||||
last_updated=timestamp,
|
||||
validation_result=result.get("validation", {}),
|
||||
dependencies=set(dependencies or []),
|
||||
hot_reload_supported=hot_reload,
|
||||
)
|
||||
|
||||
# Save to file system
|
||||
self._save_module_file(deployed)
|
||||
|
||||
# Update registry
|
||||
deployed.status = DeploymentStatus.ACTIVE
|
||||
self.modules[module_name] = deployed
|
||||
|
||||
# Record version
|
||||
self._record_version(module_name, version, module_code)
|
||||
|
||||
result["deployment"] = {
|
||||
"status": "active",
|
||||
"hot_reload_ready": hot_reload,
|
||||
"file_path": self._get_module_path(module_name),
|
||||
}
|
||||
result["message"] = f"Module '{module_name}' v{version} deployed successfully"
|
||||
|
||||
if existing_module:
|
||||
result["message"] += " (hot-reload update)"
|
||||
|
||||
logger.info(f"Deployed module: {module_name} v{version}")
|
||||
|
||||
except Exception as e:
|
||||
result["success"] = False
|
||||
result["error"] = str(e)
|
||||
result["deployment"] = {"status": "failed"}
|
||||
|
||||
# Attempt rollback if deployment failed
|
||||
if self.auto_rollback and existing_module:
|
||||
rollback_result = self.rollback_module(module_name)
|
||||
result["rollback_result"] = rollback_result
|
||||
|
||||
logger.error(f"Deployment failed for {module_name}: {e}")
|
||||
|
||||
return result
|
||||
|
||||
def hot_reload_module(self, module_name: str, new_code: str) -> Dict[str, Any]:
|
||||
"""
|
||||
Hot-reload an active module with new code.
|
||||
|
||||
Args:
|
||||
module_name: Name of the module to reload
|
||||
new_code: New module code
|
||||
|
||||
Returns:
|
||||
Dict with reload results
|
||||
"""
|
||||
if module_name not in self.modules:
|
||||
return {
|
||||
"success": False,
|
||||
"error": f"Module '{module_name}' not found. Deploy it first."
|
||||
}
|
||||
|
||||
module = self.modules[module_name]
|
||||
if not module.hot_reload_supported:
|
||||
return {
|
||||
"success": False,
|
||||
"error": f"Module '{module_name}' does not support hot-reload"
|
||||
}
|
||||
|
||||
# Use deploy_module with hot_reload=True
|
||||
return self.deploy_module(
|
||||
module_code=new_code,
|
||||
module_name=module_name,
|
||||
hot_reload=True,
|
||||
validate=True
|
||||
)
|
||||
|
||||
def rollback_module(self, module_name: str, to_version: Optional[str] = None) -> Dict[str, Any]:
|
||||
"""
|
||||
Rollback a module to a previous version.
|
||||
|
||||
Args:
|
||||
module_name: Module to rollback
|
||||
to_version: Specific version to rollback to (latest backup if not specified)
|
||||
|
||||
Returns:
|
||||
Dict with rollback results
|
||||
"""
|
||||
if module_name not in self.modules:
|
||||
return {
|
||||
"success": False,
|
||||
"error": f"Module '{module_name}' not found"
|
||||
}
|
||||
|
||||
module = self.modules[module_name]
|
||||
module.status = DeploymentStatus.ROLLING_BACK
|
||||
|
||||
try:
|
||||
if to_version:
|
||||
# Restore specific version
|
||||
version_data = self._get_version(module_name, to_version)
|
||||
if not version_data:
|
||||
return {
|
||||
"success": False,
|
||||
"error": f"Version '{to_version}' not found for module '{module_name}'"
|
||||
}
|
||||
# Would restore from version data
|
||||
else:
|
||||
# Restore from backup
|
||||
backup_code = self._get_backup(module_name)
|
||||
if backup_code:
|
||||
module.code = backup_code
|
||||
module.last_updated = datetime.now().isoformat()
|
||||
else:
|
||||
return {
|
||||
"success": False,
|
||||
"error": f"No backup available for '{module_name}'"
|
||||
}
|
||||
|
||||
module.status = DeploymentStatus.ROLLED_BACK
|
||||
self._save_module_file(module)
|
||||
|
||||
logger.info(f"Rolled back module: {module_name}")
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"module_name": module_name,
|
||||
"message": f"Module '{module_name}' rolled back successfully",
|
||||
"status": module.status.value,
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
module.status = DeploymentStatus.FAILED
|
||||
logger.error(f"Rollback failed for {module_name}: {e}")
|
||||
return {
|
||||
"success": False,
|
||||
"error": str(e)
|
||||
}
|
||||
|
||||
def validate_module(self, module_code: str) -> Dict[str, Any]:
|
||||
"""
|
||||
Validate Three.js module code without deploying.
|
||||
|
||||
Args:
|
||||
module_code: Code to validate
|
||||
|
||||
Returns:
|
||||
Dict with validation results
|
||||
"""
|
||||
return self._validate_module(module_code)
|
||||
|
||||
def get_module_status(self, module_name: str) -> Optional[Dict[str, Any]]:
|
||||
"""
|
||||
Get status of a deployed module.
|
||||
|
||||
Args:
|
||||
module_name: Module name
|
||||
|
||||
Returns:
|
||||
Module status dict or None if not found
|
||||
"""
|
||||
if module_name in self.modules:
|
||||
return self.modules[module_name].to_dict()
|
||||
return None
|
||||
|
||||
def get_all_modules(self) -> Dict[str, Any]:
|
||||
"""
|
||||
Get status of all deployed modules.
|
||||
|
||||
Returns:
|
||||
Dict with all module statuses
|
||||
"""
|
||||
return {
|
||||
"modules": {
|
||||
name: module.to_dict()
|
||||
for name, module in self.modules.items()
|
||||
},
|
||||
"total_count": len(self.modules),
|
||||
"active_count": sum(1 for m in self.modules.values() if m.status == DeploymentStatus.ACTIVE),
|
||||
}
|
||||
|
||||
def get_version_history(self, module_name: str) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
Get version history for a module.
|
||||
|
||||
Args:
|
||||
module_name: Module name
|
||||
|
||||
Returns:
|
||||
List of version dicts
|
||||
"""
|
||||
history = self.version_history.get(module_name, [])
|
||||
return [v.to_dict() for v in history]
|
||||
|
||||
def remove_module(self, module_name: str) -> Dict[str, Any]:
|
||||
"""
|
||||
Remove a deployed module.
|
||||
|
||||
Args:
|
||||
module_name: Module to remove
|
||||
|
||||
Returns:
|
||||
Dict with removal results
|
||||
"""
|
||||
if module_name not in self.modules:
|
||||
return {
|
||||
"success": False,
|
||||
"error": f"Module '{module_name}' not found"
|
||||
}
|
||||
|
||||
try:
|
||||
# Remove file
|
||||
module_path = self._get_module_path(module_name)
|
||||
if os.path.exists(module_path):
|
||||
os.remove(module_path)
|
||||
|
||||
# Remove from registry
|
||||
del self.modules[module_name]
|
||||
|
||||
logger.info(f"Removed module: {module_name}")
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"message": f"Module '{module_name}' removed successfully"
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
return {
|
||||
"success": False,
|
||||
"error": str(e)
|
||||
}
|
||||
|
||||
def _validate_module(self, code: str) -> Dict[str, Any]:
|
||||
"""Internal validation method."""
|
||||
# Use existing validation from nexus_architect (lazy import)
|
||||
validate_fn, _ = _import_validation()
|
||||
validation_result = validate_fn(code, strict_mode=self.strict_validation)
|
||||
|
||||
# Check Three.js API compliance
|
||||
three_api_issues = self._check_three_js_api_compliance(code)
|
||||
|
||||
return {
|
||||
"is_valid": validation_result.is_valid and len(three_api_issues) == 0,
|
||||
"syntax_valid": validation_result.is_valid,
|
||||
"api_compliant": len(three_api_issues) == 0,
|
||||
"errors": validation_result.errors + three_api_issues,
|
||||
"warnings": validation_result.warnings,
|
||||
"safety_score": max(0, 100 - len(validation_result.errors) * 20 - len(validation_result.warnings) * 5),
|
||||
}
|
||||
|
||||
def _check_three_js_api_compliance(self, code: str) -> List[str]:
|
||||
"""Check for Three.js API compliance issues."""
|
||||
issues = []
|
||||
|
||||
# Check for required patterns
|
||||
if "THREE.Group" not in code and "new THREE" not in code:
|
||||
issues.append("No Three.js objects created")
|
||||
|
||||
# Check for deprecated APIs
|
||||
deprecated_patterns = [
|
||||
(r"THREE\.Face3", "THREE.Face3 is deprecated, use BufferGeometry"),
|
||||
(r"THREE\.Geometry\(", "THREE.Geometry is deprecated, use BufferGeometry"),
|
||||
]
|
||||
|
||||
for pattern, message in deprecated_patterns:
|
||||
if re.search(pattern, code):
|
||||
issues.append(f"Deprecated API: {message}")
|
||||
|
||||
return issues
|
||||
|
||||
def _generate_version(self, module_name: str, code: str) -> str:
|
||||
"""Generate version string from code hash."""
|
||||
code_hash = hashlib.md5(code.encode()).hexdigest()[:8]
|
||||
timestamp = datetime.now().strftime("%Y%m%d%H%M")
|
||||
return f"{timestamp}-{code_hash}"
|
||||
|
||||
def _create_backup(self, module: DeployedModule) -> None:
|
||||
"""Create backup of existing module."""
|
||||
backup_path = os.path.join(
|
||||
self.modules_dir,
|
||||
f"{module.name}.{module.version}.backup.js"
|
||||
)
|
||||
with open(backup_path, 'w') as f:
|
||||
f.write(module.code)
|
||||
|
||||
def _get_backup(self, module_name: str) -> Optional[str]:
|
||||
"""Get backup code for module."""
|
||||
if module_name not in self.modules:
|
||||
return None
|
||||
|
||||
module = self.modules[module_name]
|
||||
backup_path = os.path.join(
|
||||
self.modules_dir,
|
||||
f"{module.name}.{module.version}.backup.js"
|
||||
)
|
||||
|
||||
if os.path.exists(backup_path):
|
||||
with open(backup_path, 'r') as f:
|
||||
return f.read()
|
||||
return None
|
||||
|
||||
def _save_module_file(self, module: DeployedModule) -> None:
|
||||
"""Save module to file system."""
|
||||
module_path = self._get_module_path(module.name)
|
||||
with open(module_path, 'w') as f:
|
||||
f.write(f"// Nexus Module: {module.name}\n")
|
||||
f.write(f"// Version: {module.version}\n")
|
||||
f.write(f"// Status: {module.status.value}\n")
|
||||
f.write(f"// Updated: {module.last_updated}\n")
|
||||
f.write(f"// Hot-Reload: {module.hot_reload_supported}\n")
|
||||
f.write("\n")
|
||||
f.write(module.code)
|
||||
|
||||
def _get_module_path(self, module_name: str) -> str:
|
||||
"""Get file path for module."""
|
||||
return os.path.join(self.modules_dir, f"{module_name}.nexus.js")
|
||||
|
||||
def _record_version(self, module_name: str, version: str, code: str) -> None:
|
||||
"""Record version in history."""
|
||||
if module_name not in self.version_history:
|
||||
self.version_history[module_name] = []
|
||||
|
||||
version_info = ModuleVersion(
|
||||
version_id=version,
|
||||
module_name=module_name,
|
||||
code_hash=hashlib.md5(code.encode()).hexdigest()[:16],
|
||||
timestamp=datetime.now().isoformat(),
|
||||
)
|
||||
|
||||
self.version_history[module_name].insert(0, version_info)
|
||||
|
||||
# Keep only last 10 versions
|
||||
self.version_history[module_name] = self.version_history[module_name][:10]
|
||||
|
||||
def _get_version(self, module_name: str, version: str) -> Optional[ModuleVersion]:
|
||||
"""Get specific version info."""
|
||||
history = self.version_history.get(module_name, [])
|
||||
for v in history:
|
||||
if v.version_id == version:
|
||||
return v
|
||||
return None
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Convenience Functions
|
||||
# =============================================================================
|
||||
|
||||
_deployer_instance: Optional[NexusDeployer] = None
|
||||
|
||||
|
||||
def get_deployer() -> NexusDeployer:
|
||||
"""Get or create the NexusDeployer singleton."""
|
||||
global _deployer_instance
|
||||
if _deployer_instance is None:
|
||||
_deployer_instance = NexusDeployer()
|
||||
return _deployer_instance
|
||||
|
||||
|
||||
def deploy_nexus_module(
|
||||
module_code: str,
|
||||
module_name: str,
|
||||
test: bool = True,
|
||||
hot_reload: bool = True
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Deploy a Nexus module with validation.
|
||||
|
||||
Args:
|
||||
module_code: Three.js module code
|
||||
module_name: Unique module identifier
|
||||
test: Run validation tests before deployment
|
||||
hot_reload: Enable hot-reload support
|
||||
|
||||
Returns:
|
||||
Dict with deployment results
|
||||
"""
|
||||
deployer = get_deployer()
|
||||
return deployer.deploy_module(
|
||||
module_code=module_code,
|
||||
module_name=module_name,
|
||||
hot_reload=hot_reload,
|
||||
validate=test
|
||||
)
|
||||
|
||||
|
||||
def hot_reload_module(module_name: str, new_code: str) -> Dict[str, Any]:
|
||||
"""
|
||||
Hot-reload an existing module.
|
||||
|
||||
Args:
|
||||
module_name: Module to reload
|
||||
new_code: New module code
|
||||
|
||||
Returns:
|
||||
Dict with reload results
|
||||
"""
|
||||
deployer = get_deployer()
|
||||
return deployer.hot_reload_module(module_name, new_code)
|
||||
|
||||
|
||||
def validate_nexus_code(code: str) -> Dict[str, Any]:
|
||||
"""
|
||||
Validate Three.js code without deploying.
|
||||
|
||||
Args:
|
||||
code: Three.js code to validate
|
||||
|
||||
Returns:
|
||||
Dict with validation results
|
||||
"""
|
||||
deployer = get_deployer()
|
||||
return deployer.validate_module(code)
|
||||
|
||||
|
||||
def get_deployment_status() -> Dict[str, Any]:
|
||||
"""Get status of all deployed modules."""
|
||||
deployer = get_deployer()
|
||||
return deployer.get_all_modules()
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Tool Schemas
|
||||
# =============================================================================
|
||||
|
||||
NEXUS_DEPLOYMENT_SCHEMAS = {
|
||||
"deploy_nexus_module": {
|
||||
"name": "deploy_nexus_module",
|
||||
"description": "Deploy a Nexus Three.js module with validation and hot-reload support",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"module_code": {"type": "string"},
|
||||
"module_name": {"type": "string"},
|
||||
"test": {"type": "boolean", "default": True},
|
||||
"hot_reload": {"type": "boolean", "default": True},
|
||||
},
|
||||
"required": ["module_code", "module_name"]
|
||||
}
|
||||
},
|
||||
"hot_reload_module": {
|
||||
"name": "hot_reload_module",
|
||||
"description": "Hot-reload an existing Nexus module with new code",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"module_name": {"type": "string"},
|
||||
"new_code": {"type": "string"},
|
||||
},
|
||||
"required": ["module_name", "new_code"]
|
||||
}
|
||||
},
|
||||
"validate_nexus_code": {
|
||||
"name": "validate_nexus_code",
|
||||
"description": "Validate Three.js code for Nexus deployment without deploying",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"code": {"type": "string"}
|
||||
},
|
||||
"required": ["code"]
|
||||
}
|
||||
},
|
||||
"get_deployment_status": {
|
||||
"name": "get_deployment_status",
|
||||
"description": "Get status of all deployed Nexus modules",
|
||||
"parameters": {"type": "object", "properties": {}}
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Demo
|
||||
print("Nexus Deployment System - Demo")
|
||||
print("=" * 50)
|
||||
|
||||
deployer = NexusDeployer()
|
||||
|
||||
# Sample module code
|
||||
sample_code = """
|
||||
(function() {
|
||||
function createDemoRoom() {
|
||||
const room = new THREE.Group();
|
||||
room.name = 'demo_room';
|
||||
|
||||
const light = new THREE.AmbientLight(0x404040, 0.5);
|
||||
room.add(light);
|
||||
|
||||
return room;
|
||||
}
|
||||
|
||||
window.NexusRooms = window.NexusRooms || {};
|
||||
window.NexusRooms.demo_room = createDemoRoom;
|
||||
|
||||
return { createDemoRoom };
|
||||
})();
|
||||
"""
|
||||
|
||||
# Deploy
|
||||
result = deployer.deploy_module(sample_code, "demo_room")
|
||||
print(f"\nDeployment result: {result['message']}")
|
||||
print(f"Validation: {result['validation'].get('is_valid', False)}")
|
||||
print(f"Safety score: {result['validation'].get('safety_score', 0)}/100")
|
||||
|
||||
# Get status
|
||||
status = deployer.get_all_modules()
|
||||
print(f"\nTotal modules: {status['total_count']}")
|
||||
print(f"Active: {status['active_count']}")
|
||||
Reference in New Issue
Block a user