Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1458,6 +1458,87 @@ Example output:
|
|
1458 |
logger.info("=== OPCODE COUTER LOGIC completed ===")
|
1459 |
return state
|
1460 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1461 |
# Node 10:Function based block builder node
|
1462 |
def overall_block_builder_node_2(state: dict):
|
1463 |
logger.info("--- Running OverallBlockBuilderNode ---")
|
@@ -1515,7 +1596,13 @@ def overall_block_builder_node_2(state: dict):
|
|
1515 |
opcode_counts = plan_entry.get("opcode_counts", {})
|
1516 |
|
1517 |
try:
|
1518 |
-
generated_blocks=block_builder(opcode_counts,logic_sequence)
|
|
|
|
|
|
|
|
|
|
|
|
|
1519 |
if "blocks" in generated_blocks and isinstance(generated_blocks["blocks"], dict):
|
1520 |
logger.warning(f"LLM returned nested 'blocks' key for {sprite_name}. Unwrapping.")
|
1521 |
generated_blocks = generated_blocks["blocks"]
|
@@ -1533,6 +1620,9 @@ def overall_block_builder_node_2(state: dict):
|
|
1533 |
logger.info(f"Action blocks added for sprite '{sprite_name}' by OverallBlockBuilderNode.")
|
1534 |
except Exception as e:
|
1535 |
logger.error(f"Error generating blocks for sprite '{sprite_name}': {e}")
|
|
|
|
|
|
|
1536 |
|
1537 |
state["project_json"] = project_json
|
1538 |
# with open("debug_state.json", "w", encoding="utf-8") as f:
|
|
|
1458 |
logger.info("=== OPCODE COUTER LOGIC completed ===")
|
1459 |
return state
|
1460 |
|
1461 |
+
# Node 10:Function based block builder node
|
1462 |
+
# def overall_block_builder_node_2(state: dict):
|
1463 |
+
# logger.info("--- Running OverallBlockBuilderNode ---")
|
1464 |
+
|
1465 |
+
# project_json = state["project_json"]
|
1466 |
+
# targets = project_json["targets"]
|
1467 |
+
# # --- Sprite and Stage Target Mapping ---
|
1468 |
+
# sprite_map = {target["name"]: target for target in targets if not target["isStage"]}
|
1469 |
+
# stage_target = next((target for target in targets if target["isStage"]), None)
|
1470 |
+
# if stage_target:
|
1471 |
+
# sprite_map[stage_target["name"]] = stage_target
|
1472 |
+
|
1473 |
+
# action_plan = state.get("action_plan", {})
|
1474 |
+
# print("[Overall Action Plan received at the block generator]:", json.dumps(action_plan, indent=2))
|
1475 |
+
# if not action_plan:
|
1476 |
+
# logger.warning("No action plan found in state. Skipping OverallBlockBuilderNode.")
|
1477 |
+
# return state
|
1478 |
+
|
1479 |
+
# # Initialize offsets for script placement on the Scratch canvas
|
1480 |
+
# script_y_offset = {}
|
1481 |
+
# script_x_offset_per_sprite = {name: 0 for name in sprite_map.keys()}
|
1482 |
+
|
1483 |
+
# # This handles potential variations in the action_plan structure.
|
1484 |
+
# if action_plan.get("action_overall_flow", {}) == {}:
|
1485 |
+
# plan_data = action_plan.items()
|
1486 |
+
# else:
|
1487 |
+
# plan_data = action_plan.get("action_overall_flow", {}).items()
|
1488 |
+
|
1489 |
+
# # --- Extract global project context for LLM ---
|
1490 |
+
# all_sprite_names = list(sprite_map.keys())
|
1491 |
+
# all_variable_names = {}
|
1492 |
+
# all_list_names = {}
|
1493 |
+
# all_broadcast_messages = {}
|
1494 |
+
|
1495 |
+
# for target in targets:
|
1496 |
+
# for var_id, var_info in target.get("variables", {}).items():
|
1497 |
+
# all_variable_names[var_info[0]] = var_id # Store name -> ID mapping (e.g., "myVariable": "myVarId123")
|
1498 |
+
# for list_id, list_info in target.get("lists", {}).items():
|
1499 |
+
# all_list_names[list_info[0]] = list_id # Store name -> ID mapping
|
1500 |
+
# for broadcast_id, broadcast_name in target.get("broadcasts", {}).items():
|
1501 |
+
# all_broadcast_messages[broadcast_name] = broadcast_id # Store name -> ID mapping
|
1502 |
+
|
1503 |
+
# # --- Process each sprite's action plan ---
|
1504 |
+
# for sprite_name, sprite_actions_data in plan_data:
|
1505 |
+
# if sprite_name in sprite_map:
|
1506 |
+
# current_sprite_target = sprite_map[sprite_name]
|
1507 |
+
# if "blocks" not in current_sprite_target:
|
1508 |
+
# current_sprite_target["blocks"] = {}
|
1509 |
+
|
1510 |
+
# if sprite_name not in script_y_offset:
|
1511 |
+
# script_y_offset[sprite_name] = 0
|
1512 |
+
|
1513 |
+
# for plan_entry in sprite_actions_data.get("plans", []):
|
1514 |
+
# logic_sequence = str(plan_entry["logic"])
|
1515 |
+
# opcode_counts = plan_entry.get("opcode_counts", {})
|
1516 |
+
|
1517 |
+
# try:
|
1518 |
+
# generated_blocks=block_builder(opcode_counts,logic_sequence)
|
1519 |
+
# if "blocks" in generated_blocks and isinstance(generated_blocks["blocks"], dict):
|
1520 |
+
# logger.warning(f"LLM returned nested 'blocks' key for {sprite_name}. Unwrapping.")
|
1521 |
+
# generated_blocks = generated_blocks["blocks"]
|
1522 |
+
|
1523 |
+
# # Update block positions for top-level script
|
1524 |
+
# for block_id, block_data in generated_blocks.items():
|
1525 |
+
# if block_data.get("topLevel"):
|
1526 |
+
# block_data["x"] = script_x_offset_per_sprite.get(sprite_name, 0)
|
1527 |
+
# block_data["y"] = script_y_offset[sprite_name]
|
1528 |
+
# script_y_offset[sprite_name] += 150 # Increment for next script
|
1529 |
+
|
1530 |
+
# current_sprite_target["blocks"].update(generated_blocks)
|
1531 |
+
# print(f"[current_sprite_target block updated]: {current_sprite_target['blocks']}")
|
1532 |
+
# state["iteration_count"] = 0
|
1533 |
+
# logger.info(f"Action blocks added for sprite '{sprite_name}' by OverallBlockBuilderNode.")
|
1534 |
+
# except Exception as e:
|
1535 |
+
# logger.error(f"Error generating blocks for sprite '{sprite_name}': {e}")
|
1536 |
+
|
1537 |
+
# state["project_json"] = project_json
|
1538 |
+
# # with open("debug_state.json", "w", encoding="utf-8") as f:
|
1539 |
+
# # json.dump(state, f, indent=2, ensure_ascii=False)
|
1540 |
+
|
1541 |
+
# return state
|
1542 |
# Node 10:Function based block builder node
|
1543 |
def overall_block_builder_node_2(state: dict):
|
1544 |
logger.info("--- Running OverallBlockBuilderNode ---")
|
|
|
1596 |
opcode_counts = plan_entry.get("opcode_counts", {})
|
1597 |
|
1598 |
try:
|
1599 |
+
generated_blocks = block_builder(opcode_counts, logic_sequence)
|
1600 |
+
|
1601 |
+
# Ensure generated_blocks is a dictionary
|
1602 |
+
if not isinstance(generated_blocks, dict):
|
1603 |
+
logger.error(f"block_builder for sprite '{sprite_name}' returned non-dict type: {type(generated_blocks)}. Skipping block update.")
|
1604 |
+
continue # Skip to next plan_entry if output is not a dictionary
|
1605 |
+
|
1606 |
if "blocks" in generated_blocks and isinstance(generated_blocks["blocks"], dict):
|
1607 |
logger.warning(f"LLM returned nested 'blocks' key for {sprite_name}. Unwrapping.")
|
1608 |
generated_blocks = generated_blocks["blocks"]
|
|
|
1620 |
logger.info(f"Action blocks added for sprite '{sprite_name}' by OverallBlockBuilderNode.")
|
1621 |
except Exception as e:
|
1622 |
logger.error(f"Error generating blocks for sprite '{sprite_name}': {e}")
|
1623 |
+
# Consider adding more specific error handling here if a malformed output
|
1624 |
+
# from block_builder should cause a specific state change, but generally
|
1625 |
+
# avoid nulling the entire project_json.
|
1626 |
|
1627 |
state["project_json"] = project_json
|
1628 |
# with open("debug_state.json", "w", encoding="utf-8") as f:
|