Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -423,101 +423,77 @@ def deep_dive():
|
|
| 423 |
|
| 424 |
|
| 425 |
# -----------------------------------------------------------------------------
|
| 426 |
-
# ODYSSEUS ENGINE
|
| 427 |
# -----------------------------------------------------------------------------
|
| 428 |
|
| 429 |
-
def
|
| 430 |
-
"""Ensures the AI output
|
| 431 |
try:
|
| 432 |
-
if t.get("
|
| 433 |
-
|
| 434 |
-
for k in req_keys:
|
| 435 |
-
if k not in t: return False, f"Missing key: {k}"
|
| 436 |
-
if not isinstance(t.get("entities"), list) or len(t.get("entities")) < 2:
|
| 437 |
-
return False, "Insufficient entities for interaction"
|
| 438 |
return True, "ok"
|
| 439 |
-
except: return False, "
|
| 440 |
|
| 441 |
@app.route('/api/trial/generate', methods=['POST'])
|
| 442 |
def generate_trial():
|
| 443 |
"""
|
| 444 |
-
Odysseus
|
| 445 |
-
|
| 446 |
-
- 'assembly' (Mechanical/Structural)
|
| 447 |
-
- 'reaction' (Chemical/Biological)
|
| 448 |
-
- 'navigation' (Flow/Aero)
|
| 449 |
-
- 'splicer' (Genetic/Hybrid)
|
| 450 |
"""
|
| 451 |
-
logger.info(">>> ODYSSEUS
|
| 452 |
uid = verify_token(request.headers.get('Authorization'))
|
| 453 |
if not uid: return jsonify({"error": "Unauthorized"}), 401
|
| 454 |
|
| 455 |
payload = request.get_json()
|
| 456 |
-
ep_id = payload.get("epiphanyId")
|
| 457 |
-
layer_key = payload.get("layerKey")
|
| 458 |
-
subject = payload.get("subject")
|
| 459 |
|
| 460 |
-
# 1. Cache Check
|
| 461 |
trial_path = f"epiphanies/{ep_id}/trials/{layer_key}"
|
| 462 |
existing = db_ref.child(trial_path).get()
|
| 463 |
if existing: return jsonify(existing), 200
|
| 464 |
|
| 465 |
-
# 2. Transactional Lock
|
| 466 |
lock_ref = db_ref.child(f"epiphanies/{ep_id}/trialLocks/{layer_key}")
|
| 467 |
-
if lock_ref.get(): return jsonify({"error": "
|
| 468 |
lock_ref.set({"uid": uid, "at": datetime.utcnow().isoformat()})
|
| 469 |
|
| 470 |
try:
|
| 471 |
-
# Fetch Context
|
| 472 |
layer_data = db_ref.child(f"epiphanies/{ep_id}/layers/{layer_key}").get() or {}
|
| 473 |
-
|
| 474 |
|
|
|
|
| 475 |
trial_prompt = f"""
|
| 476 |
-
Act as a
|
| 477 |
-
Context: {
|
| 478 |
|
| 479 |
-
Task:
|
|
|
|
| 480 |
|
| 481 |
-
Play Styles to choose from:
|
| 482 |
-
- 'assembly': Dragging parts into a blueprint.
|
| 483 |
-
- 'reaction': Tapping to add catalysts/energy to a system.
|
| 484 |
-
- 'navigation': Drawing paths for flow/vectors.
|
| 485 |
-
- 'splicer': Combining two traits to survive.
|
| 486 |
-
|
| 487 |
MANDATORY JSON SCHEMA:
|
| 488 |
{{
|
| 489 |
-
"engine": {{"name": "odysseus", "version": "
|
| 490 |
-
"
|
| 491 |
-
"
|
| 492 |
-
|
| 493 |
-
"goal": "One sentence Socratic goal",
|
| 494 |
-
"tool_name": "The name of the user's interaction tool (e.g. 'Dropper', 'Wrench', 'Ionizer')"
|
| 495 |
-
}},
|
| 496 |
"entities": [
|
| 497 |
{{
|
| 498 |
"id": "e1",
|
| 499 |
-
"
|
| 500 |
-
"
|
| 501 |
-
"
|
|
|
|
|
|
|
| 502 |
}}
|
| 503 |
],
|
| 504 |
-
"
|
| 505 |
-
"
|
| 506 |
-
"physics_modifier": "Gravity/Friction/Volatility level (0-1)",
|
| 507 |
-
"entropy_per_second": "How fast the system decays if left alone"
|
| 508 |
-
}},
|
| 509 |
-
"goal_state": {{
|
| 510 |
-
"metric": "stability | growth | flow_rate",
|
| 511 |
-
"target_value": 100,
|
| 512 |
-
"win_threshold_ms": 3000
|
| 513 |
-
}},
|
| 514 |
-
"realtime_insights": [
|
| 515 |
-
{{"trigger": "start", "text": "Profound Feynman opening statement."}},
|
| 516 |
-
{{"trigger": "midway", "text": "Observation about the system's strain."}}
|
| 517 |
],
|
| 518 |
-
"
|
| 519 |
-
"
|
| 520 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 521 |
}}
|
| 522 |
}}
|
| 523 |
"""
|
|
@@ -530,31 +506,28 @@ def generate_trial():
|
|
| 530 |
|
| 531 |
trial_data = json.loads(_strip_json_fences(res.text))
|
| 532 |
|
| 533 |
-
|
| 534 |
-
ok, why = validate_odysseus_v3(trial_data)
|
| 535 |
-
if not ok:
|
| 536 |
lock_ref.delete()
|
| 537 |
-
return jsonify({"error":
|
| 538 |
|
| 539 |
user_ref = db_ref.child(f'users/{uid}')
|
| 540 |
credits = user_ref.get().get('credits', 0)
|
| 541 |
if credits < 4:
|
| 542 |
lock_ref.delete()
|
| 543 |
-
return jsonify({"error": "Insufficient Sparks
|
| 544 |
|
| 545 |
-
# 4. Finalize
|
| 546 |
trial_data["createdAt"] = datetime.utcnow().isoformat()
|
| 547 |
db_ref.child(trial_path).set(trial_data)
|
| 548 |
user_ref.update({'credits': credits - 4})
|
| 549 |
|
| 550 |
lock_ref.delete()
|
| 551 |
-
logger.info(f"Odysseus
|
| 552 |
return jsonify(trial_data), 201
|
| 553 |
|
| 554 |
except Exception as e:
|
| 555 |
lock_ref.delete()
|
| 556 |
-
logger.error(f"
|
| 557 |
-
return jsonify({"error":
|
| 558 |
|
| 559 |
# -----------------------------------------------------------------------------
|
| 560 |
# 5. THE CHIRON MENTOR & SYSTEM UTILS
|
|
|
|
| 423 |
|
| 424 |
|
| 425 |
# -----------------------------------------------------------------------------
|
| 426 |
+
# ODYSSEUS ENGINE V6: THE AUTONOMOUS ARCHITECT (NO TEMPLATES)
|
| 427 |
# -----------------------------------------------------------------------------
|
| 428 |
|
| 429 |
+
def validate_odysseus_v6(t: dict) -> (bool, str):
|
| 430 |
+
"""Ensures the AI output has the basic components to build a world."""
|
| 431 |
try:
|
| 432 |
+
if not t.get("entities") or not t.get("victory_logic"):
|
| 433 |
+
return False, "Incomplete World Definition"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 434 |
return True, "ok"
|
| 435 |
+
except: return False, "Blueprint Error"
|
| 436 |
|
| 437 |
@app.route('/api/trial/generate', methods=['POST'])
|
| 438 |
def generate_trial():
|
| 439 |
"""
|
| 440 |
+
Odysseus v6: The AI as a Game Architect.
|
| 441 |
+
Directly synthesizes physics, entities, and logic from the image context.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 442 |
"""
|
| 443 |
+
logger.info(">>> ODYSSEUS V6: AUTONOMOUS GENERATION")
|
| 444 |
uid = verify_token(request.headers.get('Authorization'))
|
| 445 |
if not uid: return jsonify({"error": "Unauthorized"}), 401
|
| 446 |
|
| 447 |
payload = request.get_json()
|
| 448 |
+
ep_id, layer_key, subject = payload.get("epiphanyId"), payload.get("layerKey"), payload.get("subject")
|
|
|
|
|
|
|
| 449 |
|
|
|
|
| 450 |
trial_path = f"epiphanies/{ep_id}/trials/{layer_key}"
|
| 451 |
existing = db_ref.child(trial_path).get()
|
| 452 |
if existing: return jsonify(existing), 200
|
| 453 |
|
|
|
|
| 454 |
lock_ref = db_ref.child(f"epiphanies/{ep_id}/trialLocks/{layer_key}")
|
| 455 |
+
if lock_ref.get(): return jsonify({"error": "Architecting Simulation..."}), 409
|
| 456 |
lock_ref.set({"uid": uid, "at": datetime.utcnow().isoformat()})
|
| 457 |
|
| 458 |
try:
|
|
|
|
| 459 |
layer_data = db_ref.child(f"epiphanies/{ep_id}/layers/{layer_key}").get() or {}
|
| 460 |
+
context = layer_data.get('text', '')
|
| 461 |
|
| 462 |
+
# THE OPEN-ENDED PROMPT
|
| 463 |
trial_prompt = f"""
|
| 464 |
+
Act as a Master Game Designer and Scientific Architect.
|
| 465 |
+
Subject: {subject}. Context: {context}.
|
| 466 |
|
| 467 |
+
Task: Invent a unique, fun, 30-second 2D physics interaction that demonstrates the First Principles of this subject.
|
| 468 |
+
DO NOT use a template. Use your reasoning to decide what the user should do (e.g., Assemble a structure, trigger a reaction, balance forces, or mutate a system).
|
| 469 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 470 |
MANDATORY JSON SCHEMA:
|
| 471 |
{{
|
| 472 |
+
"engine": {{"name": "odysseus", "version": "v6"}},
|
| 473 |
+
"scenario_name": "Unique Name of the Experiment",
|
| 474 |
+
"mission_brief": "Feynman-style goal for the user.",
|
| 475 |
+
"world_settings": {{ "gravity": [x, y], "background_vibe": "string" }},
|
|
|
|
|
|
|
|
|
|
| 476 |
"entities": [
|
| 477 |
{{
|
| 478 |
"id": "e1",
|
| 479 |
+
"label": "Part Name",
|
| 480 |
+
"shape": "circle | rectangle | polygon",
|
| 481 |
+
"physics": {{ "mass": float, "restitution": 0-1, "isStatic": bool, "isDraggable": bool }},
|
| 482 |
+
"spawn_logic": {{ "count": int, "position": "random | center | grid" }},
|
| 483 |
+
"visuals": {{ "color": "hex", "glow": bool, "icon": "string" }}
|
| 484 |
}}
|
| 485 |
],
|
| 486 |
+
"logic_rules": [
|
| 487 |
+
{{ "trigger": "onCollision | onDrag | onTimed", "action": "scale | destroy | spawn | win_progress", "details": "string" }}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 488 |
],
|
| 489 |
+
"victory_logic": {{
|
| 490 |
+
"condition": "Explain the math (e.g. 'Stability > 80' or 'All pathogens destroyed')",
|
| 491 |
+
"target_metric": "string",
|
| 492 |
+
"threshold": int
|
| 493 |
+
}},
|
| 494 |
+
"revelation": {{
|
| 495 |
+
"on_win": "Profound Feynman 'Aha!' moment.",
|
| 496 |
+
"on_fail": "Socratic hint about the system's nature."
|
| 497 |
}}
|
| 498 |
}}
|
| 499 |
"""
|
|
|
|
| 506 |
|
| 507 |
trial_data = json.loads(_strip_json_fences(res.text))
|
| 508 |
|
| 509 |
+
if not validate_odysseus_v6(trial_data):
|
|
|
|
|
|
|
| 510 |
lock_ref.delete()
|
| 511 |
+
return jsonify({"error": "Architect failed to compile logic"}), 422
|
| 512 |
|
| 513 |
user_ref = db_ref.child(f'users/{uid}')
|
| 514 |
credits = user_ref.get().get('credits', 0)
|
| 515 |
if credits < 4:
|
| 516 |
lock_ref.delete()
|
| 517 |
+
return jsonify({"error": "Insufficient Sparks"}), 402
|
| 518 |
|
|
|
|
| 519 |
trial_data["createdAt"] = datetime.utcnow().isoformat()
|
| 520 |
db_ref.child(trial_path).set(trial_data)
|
| 521 |
user_ref.update({'credits': credits - 4})
|
| 522 |
|
| 523 |
lock_ref.delete()
|
| 524 |
+
logger.info(f"Odysseus v6 Architected unique game for {subject}")
|
| 525 |
return jsonify(trial_data), 201
|
| 526 |
|
| 527 |
except Exception as e:
|
| 528 |
lock_ref.delete()
|
| 529 |
+
logger.error(f"v6 Architectural Error: {e}")
|
| 530 |
+
return jsonify({"error": str(e)}), 500
|
| 531 |
|
| 532 |
# -----------------------------------------------------------------------------
|
| 533 |
# 5. THE CHIRON MENTOR & SYSTEM UTILS
|