| from flask import Flask, request, jsonify |
| from core.lifestack_env import LifeStackEnv, LifeStackAction |
| import os |
|
|
| app = Flask(__name__) |
|
|
| |
| |
| |
| env = LifeStackEnv() |
|
|
| @app.route('/reset', methods=['POST']) |
| def reset_simulation(): |
| """Starts a new simulation episode.""" |
| |
| 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 |
| |
| |
| 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") |
| ) |
| |
| |
| 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) |
|
|