Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1334,51 +1334,91 @@ If you find any "Code-Blocks" then,
|
|
| 1334 |
return state
|
| 1335 |
|
| 1336 |
# Node2: Node Optimizer node
|
| 1337 |
-
|
|
|
|
|
|
|
|
|
|
| 1338 |
logger.info("--- Running Node Optimizer Node ---")
|
| 1339 |
-
|
| 1340 |
-
|
| 1341 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1342 |
sprite_name = refined_logic_data.get("name_variable", "<unknown>")
|
| 1343 |
-
|
| 1344 |
-
|
| 1345 |
-
project_json_targets =
|
| 1346 |
for target in project_json_targets:
|
| 1347 |
-
|
| 1348 |
-
|
| 1349 |
-
|
|
|
|
|
|
|
| 1350 |
try:
|
| 1351 |
-
|
| 1352 |
-
|
|
|
|
|
|
|
|
|
|
| 1353 |
state["pseudo_code"]["refined_logic"] = refined_logic_data
|
| 1354 |
-
|
|
|
|
|
|
|
| 1355 |
state["action_plan"] = transform_logic_to_action_flow(state["pseudo_code"])
|
| 1356 |
-
|
| 1357 |
-
|
| 1358 |
-
action_flow = state.get("action_plan", {})
|
| 1359 |
-
|
| 1360 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1361 |
else:
|
| 1362 |
-
plan_data =
|
| 1363 |
-
|
| 1364 |
refined_flow: Dict[str, Any] = {}
|
| 1365 |
for sprite, sprite_data in plan_data:
|
|
|
|
|
|
|
|
|
|
| 1366 |
refined_plans = []
|
| 1367 |
for plan in sprite_data.get("plans", []):
|
| 1368 |
logic = plan.get("logic", "")
|
| 1369 |
-
|
|
|
|
| 1370 |
refined_plans.append(plan)
|
| 1371 |
-
|
|
|
|
| 1372 |
"description": sprite_data.get("description", ""),
|
| 1373 |
"plans": refined_plans
|
| 1374 |
}
|
|
|
|
| 1375 |
if refined_flow:
|
| 1376 |
-
state["action_plan"] = refined_flow
|
| 1377 |
logger.info("Node Optimization completed.")
|
| 1378 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1379 |
return state
|
| 1380 |
-
except Exception as e:
|
| 1381 |
-
logger.error(f"Error in Node Optimizer Node: {e}")
|
| 1382 |
|
| 1383 |
# Node 2: planner node
|
| 1384 |
# def overall_planner_node(state: GameState):
|
|
|
|
| 1334 |
return state
|
| 1335 |
|
| 1336 |
# Node2: Node Optimizer node
|
| 1337 |
+
|
| 1338 |
+
from typing import Dict, Any
|
| 1339 |
+
|
| 1340 |
+
def node_optimizer(state: Dict[str, Any]) -> Dict[str, Any]:
|
| 1341 |
logger.info("--- Running Node Optimizer Node ---")
|
| 1342 |
+
# safe fetches
|
| 1343 |
+
project_json = state.get("project_json", {}) if isinstance(state.get("project_json", {}), dict) else {}
|
| 1344 |
+
# ensure pseudo_code exists so we can write into it safely
|
| 1345 |
+
raw = state.setdefault("pseudo_code", {})
|
| 1346 |
+
refined_logic_data = raw.get("refined_logic", {}) or {}
|
| 1347 |
+
|
| 1348 |
+
# keep scalar sprite_name (if present) and build a separate map
|
| 1349 |
sprite_name = refined_logic_data.get("name_variable", "<unknown>")
|
| 1350 |
+
sprite_name_map: Dict[str, str] = {}
|
| 1351 |
+
|
| 1352 |
+
project_json_targets = project_json.get("targets", []) if isinstance(project_json, dict) else []
|
| 1353 |
for target in project_json_targets:
|
| 1354 |
+
if isinstance(target, dict):
|
| 1355 |
+
name = target.get("name")
|
| 1356 |
+
if name:
|
| 1357 |
+
sprite_name_map[name] = name
|
| 1358 |
+
|
| 1359 |
try:
|
| 1360 |
+
# ensure we read the pseudocode from the same key consistently
|
| 1361 |
+
pseudo_text = refined_logic_data.get("pseudocode", "")
|
| 1362 |
+
refined_logic_data["pseudocode"] = separate_scripts(str(pseudo_text))
|
| 1363 |
+
|
| 1364 |
+
# write refined logic back into state safely
|
| 1365 |
state["pseudo_code"]["refined_logic"] = refined_logic_data
|
| 1366 |
+
logger.debug("[The pseudo_code generated here]: %s", state["pseudo_code"])
|
| 1367 |
+
|
| 1368 |
+
# transform to action flow (expecting transform_logic_to_action_flow to accept the pseudo_code dict)
|
| 1369 |
state["action_plan"] = transform_logic_to_action_flow(state["pseudo_code"])
|
| 1370 |
+
logger.debug("[The action plan generated here]: %s", state["action_plan"])
|
| 1371 |
+
|
| 1372 |
+
action_flow = state.get("action_plan", {})
|
| 1373 |
+
|
| 1374 |
+
# Normalize plan_data into a list of (sprite, sprite_data) pairs
|
| 1375 |
+
plan_data = []
|
| 1376 |
+
if isinstance(action_flow, dict):
|
| 1377 |
+
ao = action_flow.get("action_overall_flow")
|
| 1378 |
+
if isinstance(ao, dict) and ao:
|
| 1379 |
+
plan_data = list(ao.items())
|
| 1380 |
+
else:
|
| 1381 |
+
# fall back to the top-level dict items
|
| 1382 |
+
plan_data = list(action_flow.items())
|
| 1383 |
+
elif isinstance(action_flow, list):
|
| 1384 |
+
# possible structure: list of dicts each holding sprite info
|
| 1385 |
+
for item in action_flow:
|
| 1386 |
+
if isinstance(item, tuple) and len(item) == 2:
|
| 1387 |
+
plan_data.append(item)
|
| 1388 |
+
elif isinstance(item, dict):
|
| 1389 |
+
# try to detect common shapes
|
| 1390 |
+
if "name" in item and "plans" in item:
|
| 1391 |
+
plan_data.append((item["name"], item))
|
| 1392 |
else:
|
| 1393 |
+
plan_data = []
|
| 1394 |
+
|
| 1395 |
refined_flow: Dict[str, Any] = {}
|
| 1396 |
for sprite, sprite_data in plan_data:
|
| 1397 |
+
if sprite is None:
|
| 1398 |
+
continue
|
| 1399 |
+
sprite_data = sprite_data or {}
|
| 1400 |
refined_plans = []
|
| 1401 |
for plan in sprite_data.get("plans", []):
|
| 1402 |
logic = plan.get("logic", "")
|
| 1403 |
+
# analyze_opcode_counts should accept a string
|
| 1404 |
+
plan["opcode_counts"] = analyze_opcode_counts(str(logic))
|
| 1405 |
refined_plans.append(plan)
|
| 1406 |
+
|
| 1407 |
+
refined_flow[str(sprite)] = {
|
| 1408 |
"description": sprite_data.get("description", ""),
|
| 1409 |
"plans": refined_plans
|
| 1410 |
}
|
| 1411 |
+
|
| 1412 |
if refined_flow:
|
| 1413 |
+
state["action_plan"] = refined_flow
|
| 1414 |
logger.info("Node Optimization completed.")
|
| 1415 |
+
|
| 1416 |
+
return state
|
| 1417 |
+
|
| 1418 |
+
except Exception:
|
| 1419 |
+
# log full stacktrace for debugging but return state so caller won't break
|
| 1420 |
+
logger.exception("Error in Node Optimizer Node — returning current state for inspection")
|
| 1421 |
return state
|
|
|
|
|
|
|
| 1422 |
|
| 1423 |
# Node 2: planner node
|
| 1424 |
# def overall_planner_node(state: GameState):
|