File size: 1,837 Bytes
77da5ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from flask import Flask, request, jsonify
from core.lifestack_env import LifeStackEnv, LifeStackAction
import os

app = Flask(__name__)

# Initialize the LifeStack Engine
# Note: In a production Flask app, you'd handle session-based env storage
# For this demo, we'll use a globally shared instance
env = LifeStackEnv()

@app.route('/reset', methods=['POST'])
def reset_simulation():
    """Starts a new simulation episode."""
    # Reset to a fresh state (optionally with a custom task)
    obs = env.reset()
    return jsonify({
        "status": "success",
        "current_metrics": obs.metrics,
        "message": "LifeStack simulation reset successfully."
    })

@app.route('/step', methods=['POST'])
def take_action():
    """Executes a single step in the life simulation."""
    data = request.json
    
    # Construct the LifeStack Action from the request
    action = LifeStackAction(
        action_type=data.get('action_type', 'inaction'),
        target=data.get('target', 'leisure'),
        metric_changes=data.get('metric_changes', {}),
        resource_cost=data.get('resource_cost', {}),
        reasoning=data.get('reasoning', "Flask API Request")
    )
    
    # Execute the step in the engine
    obs = env.step(action)
    
    return jsonify({
        "metrics_after": obs.metrics,
        "reward": obs.reward,
        "done": obs.done,
        "metadata": obs.metadata
    })

@app.route('/status', methods=['GET'])
def get_status():
    """Returns the current state of the engine."""
    return jsonify({
        "metrics": env.state.current_metrics.flatten(),
        "step_count": env.state.step_count
    })

if __name__ == '__main__':
    print("\n🚀 LifeStack Engine is now running via Flask!")
    print("Endpoints: /reset [POST], /step [POST], /status [GET]")
    app.run(host='0.0.0.0', port=5000)