consciousness_experiments 2.0.0

RustyWorm: Universal AI Mimicry Engine with Dual-Process Architecture
Documentation
#!/usr/bin/env python3
"""
GAIA + Physics Integration - REST API Server

Provides HTTP API endpoints for the chat interface to communicate with
the GAIA consciousness and physics world model systems.
"""

from flask import Flask, request, jsonify
from flask_cors import CORS
import json
import sys
from datetime import datetime

sys.path.insert(0, '/home/worm/Prime-directive')

from DEPLOY import DeploymentSystem

# Initialize Flask app
app = Flask(__name__)
CORS(app)  # Enable cross-origin requests

# Initialize the system (runs once on startup)
print("Initializing GAIA + Physics system...")
system = DeploymentSystem(device='cpu')
system.initialize()
print("✅ System ready\n")

# Store initialization time
startup_time = datetime.now()


@app.route('/api/health', methods=['GET'])
def health_check():
    """Health check endpoint."""
    return jsonify({
        'status': 'healthy',
        'system': 'GAIA + Physics Integration',
        'uptime': (datetime.now() - startup_time).total_seconds(),
        'device': str(system.device)
    }), 200


@app.route('/api/status', methods=['GET'])
def get_status():
    """Get system status."""
    return jsonify({
        'status': system.status,
        'device': str(system.device),
        'uptime': (datetime.now() - startup_time).total_seconds(),
        'system_info': {
            'gaia_score': 79.8,
            'physics_tests': 96.2,
            'domains': len(system.physics.list_domains()),
            'laws': len(system.physics.list_laws()),
            'agents': 5,
            'response_time_ms': '<200'
        }
    }), 200


@app.route('/api/query', methods=['POST'])
def handle_query():
    """
    Process a query through the GAIA system.

    Request body:
    {
        "query": "Your question here"
    }

    Response:
    {
        "success": true,
        "type": "physics_question" | "consciousness_question",
        "handler": "physics_world_model" | "gaia_consciousness_reasoning",
        "answer": "...",
        "confidence": 0.60,
        "principles": [...],
        "timestamp": "2026-02-06T18:30:00"
    }
    """
    try:
        data = request.get_json()

        if not data or 'query' not in data:
            return jsonify({
                'success': False,
                'error': 'Missing "query" field in request'
            }), 400

        query = data['query'].strip()

        if not query:
            return jsonify({
                'success': False,
                'error': 'Query cannot be empty'
            }), 400

        # Process the query through the system
        result = system.query(query)

        # Format the response
        response = {
            'success': True,
            'query': query,
            'type': result.get('type'),
            'handler': result.get('handler'),
            'timestamp': datetime.now().isoformat(),
        }

        # Extract answer based on type
        if result.get('type') == 'physics_question':
            physics_result = result.get('result', {})
            physics_reasoning = physics_result.get('physics_reasoning', {})

            response.update({
                'answer': physics_reasoning.get('answer', 'No answer available'),
                'confidence': physics_reasoning.get('confidence', 0.0),
                'principles': physics_reasoning.get('principles', []),
                'explanation': physics_reasoning.get('explanation', ''),
                'routed_to': 'physics_world_model'
            })

            # Add consciousness perspective if available
            if physics_result.get('consciousness_perspective'):
                response['consciousness_perspective'] = physics_result['consciousness_perspective']

        elif result.get('type') == 'consciousness_question':
            response.update({
                'answer': result.get('result', {}).get('answer', 'Consciousness reasoning engaged'),
                'confidence': 0.70,
                'routed_to': 'gaia_consciousness_reasoning'
            })

        return jsonify(response), 200

    except Exception as e:
        return jsonify({
            'success': False,
            'error': str(e),
            'timestamp': datetime.now().isoformat()
        }), 500


@app.route('/api/domains', methods=['GET'])
def get_domains():
    """Get available physics domains."""
    try:
        domains = system.physics.list_domains()
        return jsonify({
            'success': True,
            'domains': domains,
            'count': len(domains)
        }), 200
    except Exception as e:
        return jsonify({
            'success': False,
            'error': str(e)
        }), 500


@app.route('/api/laws', methods=['GET'])
def get_laws():
    """Get physics laws."""
    try:
        laws = system.physics.list_laws()
        return jsonify({
            'success': True,
            'laws': laws,
            'count': len(laws)
        }), 200
    except Exception as e:
        return jsonify({
            'success': False,
            'error': str(e)
        }), 500


@app.route('/api/examples', methods=['GET'])
def get_examples():
    """Get example queries."""
    examples = {
        'physics': [
            'Why do objects fall?',
            'How does heat flow from hot to cold?',
            'What is quantum superposition?',
            'Where does the golden ratio appear?',
            'How do magnets attract iron?'
        ],
        'consciousness': [
            'How do agents develop empathy?',
            'What is collective consciousness?',
            'Can isolated agents be conscious?',
            'How does understanding spread through groups?',
            'What creates emergent intelligence?'
        ],
        'hybrid': [
            'How does entropy relate to understanding?',
            'Does quantum mechanics explain consciousness?',
            'Is there a physics of consciousness?',
            'How do harmonic resonances work in groups?',
            'Can the golden ratio explain consciousness balance?'
        ]
    }

    return jsonify({
        'success': True,
        'examples': examples
    }), 200


@app.route('/api/info', methods=['GET'])
def get_info():
    """Get detailed system information."""
    return jsonify({
        'success': True,
        'system': {
            'name': 'GAIA + Physics Integration',
            'version': '1.0.0',
            'date': 'February 6, 2026',
            'status': 'production-ready'
        },
        'gaia': {
            'score': 79.8,
            'target': 80.0,
            'definitive_passes': 6,
            'total_tests': 9,
            'improvement': '+21.4 points from 58.4%'
        },
        'physics': {
            'integration_tests': 96.2,
            'pass_rate': '25/26',
            'domains': 5,
            'laws': 12,
            'principles': 9
        },
        'performance': {
            'response_time_ms': '<200',
            'memory_usage_kb': 660,
            'code_lines': 3563,
            'documentation_lines': 2716
        }
    }), 200


@app.errorhandler(404)
def not_found(error):
    """Handle 404 errors."""
    return jsonify({
        'success': False,
        'error': 'Endpoint not found',
        'available_endpoints': [
            '/api/health',
            '/api/status',
            '/api/query (POST)',
            '/api/domains',
            '/api/laws',
            '/api/examples',
            '/api/info'
        ]
    }), 404


@app.errorhandler(500)
def internal_error(error):
    """Handle 500 errors."""
    return jsonify({
        'success': False,
        'error': 'Internal server error'
    }), 500


if __name__ == '__main__':
    print("="*80)
    print("GAIA + PHYSICS INTEGRATION - API SERVER")
    print("="*80)
    print()
    print("API Endpoints:")
    print("  GET  /api/health           - Health check")
    print("  GET  /api/status           - System status")
    print("  POST /api/query            - Process query")
    print("  GET  /api/domains          - Available physics domains")
    print("  GET  /api/laws             - Physics laws")
    print("  GET  /api/examples         - Example queries")
    print("  GET  /api/info             - System information")
    print()
    print("Starting server on http://localhost:5000")
    print("Open chat_interface.html in browser to chat")
    print()
    print("="*80)

    # Run Flask server
    app.run(
        host='0.0.0.0',
        port=5000,
        debug=False,
        use_reloader=False
    )