Update app.py
Browse files
app.py
CHANGED
|
@@ -1254,7 +1254,7 @@ If you find any "Code-Blocks" then,
|
|
| 1254 |
logger.info("Plan refinement and block relation analysis completed for all plans.")
|
| 1255 |
return state
|
| 1256 |
|
| 1257 |
-
#
|
| 1258 |
def node_optimizer(state: GameState):
|
| 1259 |
logger.info("--- Running Node Optimizer Node ---")
|
| 1260 |
project_json = state["project_json"]
|
|
@@ -1302,820 +1302,820 @@ def node_optimizer(state: GameState):
|
|
| 1302 |
logger.error(f"Error in Node Optimizer Node: {e}")
|
| 1303 |
|
| 1304 |
# Node 2: planner node
|
| 1305 |
-
def overall_planner_node(state: GameState):
|
| 1306 |
-
|
| 1307 |
-
|
| 1308 |
-
|
| 1309 |
-
|
| 1310 |
-
|
| 1311 |
-
|
| 1312 |
-
|
| 1313 |
-
|
| 1314 |
-
|
| 1315 |
-
|
| 1316 |
-
|
| 1317 |
-
|
| 1318 |
-
|
| 1319 |
-
|
| 1320 |
-
|
| 1321 |
-
|
| 1322 |
-
|
| 1323 |
-
|
| 1324 |
-
|
| 1325 |
-
|
| 1326 |
-
|
| 1327 |
-
|
| 1328 |
-
|
| 1329 |
-
|
| 1330 |
-
|
| 1331 |
-
|
| 1332 |
-
|
| 1333 |
-
Generate a detailed action plan for the game's sprites and stage based on the given pseudo-code and sprite details for the given sprite name and .
|
| 1334 |
-
|
| 1335 |
-
Description:
|
| 1336 |
-
|
| 1337 |
-
|
| 1338 |
-
|
| 1339 |
-
|
| 1340 |
-
[Note: Make sure you just refine the pseudo code by correting mistake and adding the missing opcode if any and *Do not* generate any new logic]
|
| 1341 |
-
----
|
| 1342 |
-
**Targets in Game (Sprites and Stage) available in project_json:** {', '.join(target_names)}
|
| 1343 |
-
|
| 1344 |
-
--- Scratch 3.0 Block Reference ---
|
| 1345 |
-
This section provides a comprehensive reference of Scratch 3.0 blocks, categorized by shape, including their opcodes and functional descriptions. Use this to accurately identify block types and behavior.
|
| 1346 |
-
|
| 1347 |
-
### Hat Blocks
|
| 1348 |
-
Description: {hat_description}
|
| 1349 |
-
Blocks:
|
| 1350 |
-
{hat_opcodes_functionalities}
|
| 1351 |
-
|
| 1352 |
-
### Boolean Blocks
|
| 1353 |
-
Description: {boolean_description}
|
| 1354 |
-
Blocks:
|
| 1355 |
-
{boolean_opcodes_functionalities}
|
| 1356 |
-
|
| 1357 |
-
### C Blocks
|
| 1358 |
-
Description: {c_description}
|
| 1359 |
-
Blocks:
|
| 1360 |
-
{c_opcodes_functionalities}
|
| 1361 |
-
|
| 1362 |
-
### Cap Blocks
|
| 1363 |
-
Description: {cap_description}
|
| 1364 |
-
Blocks:
|
| 1365 |
-
{cap_opcodes_functionalities}
|
| 1366 |
-
|
| 1367 |
-
### Reporter Blocks
|
| 1368 |
-
Description: {reporter_description}
|
| 1369 |
-
Blocks:
|
| 1370 |
-
{reporter_opcodes_functionalities}
|
| 1371 |
-
|
| 1372 |
-
### Stack Blocks
|
| 1373 |
-
Description: {stack_description}
|
| 1374 |
-
Blocks:
|
| 1375 |
-
{stack_opcodes_functionalities}
|
| 1376 |
-
|
| 1377 |
-
-----------------------------------
|
| 1378 |
-
|
| 1379 |
-
Your task is to use the `Sprite_name` given and `Pseudo_code` and add it to the specific target name and define the primary actions and movements.
|
| 1380 |
-
The output should be a JSON object with a single key 'action_overall_flow'. Each key inside this object should be a sprite or 'Stage' name (e.g., 'Player', 'Enemy', 'Stage'), and its value must include a 'description' and a list of 'plans'.
|
| 1381 |
-
The first plan in each stage must start with one Scratch Hat Block (e.g., event_whenflagclicked).
|
| 1382 |
-
Other plans may use any hat blocks (including duplicates) to represent different logics or events.
|
| 1383 |
-
Each plan must include a **single Scratch Hat Block** (e.g., 'event_whenflagclicked') to start scratch project and should contain:
|
| 1384 |
-
|
| 1385 |
-
1. **'event'**: the exact `opcode` of the hat block that initiates the logic.
|
| 1386 |
-
[NOTE: INSTRUCTIONN TO FOLLOW IF PSEUDO_CODE HAVING PROBLEM ]
|
| 1387 |
-
2. **'logic'**: a natural language breakdown of each step taken after the event, formatted as a multi-line string representing pseudo-code. Ensure clarity and granularity—each described action should map closely to a Scratch block or tight sequence.
|
| 1388 |
-
|
| 1389 |
-
|
| 1390 |
-
|
| 1391 |
-
|
| 1392 |
-
|
| 1393 |
-
|
| 1394 |
-
|
| 1395 |
-
|
| 1396 |
-
|
| 1397 |
-
|
| 1398 |
-
|
| 1399 |
-
|
| 1400 |
-
|
| 1401 |
-
|
| 1402 |
-
|
| 1403 |
-
3. **Opcode Lists**: include relevant Scratch opcodes grouped under `motion`, `control`, `operator`, `sensing`, `looks`, `sounds`, `events`, and `data`. List only the non-empty categories. Use exact opcodes.
|
| 1404 |
-
4. Few Example of content of logics inside for a specific plan as scratch pseudo-code:
|
| 1405 |
-
|
| 1406 |
-
|
| 1407 |
-
|
| 1408 |
-
|
| 1409 |
-
|
| 1410 |
-
|
| 1411 |
-
|
| 1412 |
-
|
| 1413 |
-
|
| 1414 |
-
|
| 1415 |
-
|
| 1416 |
-
|
| 1417 |
-
|
| 1418 |
-
|
| 1419 |
-
|
| 1420 |
-
|
| 1421 |
-
|
| 1422 |
-
|
| 1423 |
-
|
| 1424 |
-
|
| 1425 |
-
|
| 1426 |
-
|
| 1427 |
-
|
| 1428 |
-
|
| 1429 |
-
|
| 1430 |
-
|
| 1431 |
-
|
| 1432 |
-
|
| 1433 |
-
|
| 1434 |
-
|
| 1435 |
-
|
| 1436 |
-
|
| 1437 |
-
|
| 1438 |
-
|
| 1439 |
-
5. Use target names exactly as listed in `Targets in Game`. Do NOT rename or invent new targets.
|
| 1440 |
-
6. Ensure the plan reflects accurate opcode usage derived strictly from the block reference above.
|
| 1441 |
-
7. Few shot Example structure for 'action_overall_flow':
|
| 1442 |
-
```json
|
| 1443 |
-
{{
|
| 1444 |
-
|
| 1445 |
-
|
| 1446 |
-
|
| 1447 |
-
|
| 1448 |
-
|
| 1449 |
-
|
| 1450 |
-
|
| 1451 |
-
|
| 1452 |
-
|
| 1453 |
-
|
| 1454 |
-
|
| 1455 |
-
|
| 1456 |
-
|
| 1457 |
-
|
| 1458 |
-
|
| 1459 |
-
|
| 1460 |
-
|
| 1461 |
-
|
| 1462 |
-
|
| 1463 |
-
|
| 1464 |
-
|
| 1465 |
-
|
| 1466 |
-
|
| 1467 |
-
|
| 1468 |
-
|
| 1469 |
-
|
| 1470 |
-
|
| 1471 |
-
|
| 1472 |
-
|
| 1473 |
-
|
| 1474 |
-
|
| 1475 |
-
|
| 1476 |
-
|
| 1477 |
-
|
| 1478 |
-
|
| 1479 |
-
|
| 1480 |
-
|
| 1481 |
-
|
| 1482 |
-
|
| 1483 |
-
|
| 1484 |
-
|
| 1485 |
-
|
| 1486 |
-
|
| 1487 |
-
|
| 1488 |
-
|
| 1489 |
-
|
| 1490 |
-
|
| 1491 |
-
|
| 1492 |
-
|
| 1493 |
-
|
| 1494 |
-
|
| 1495 |
-
|
| 1496 |
-
|
| 1497 |
-
|
| 1498 |
-
|
| 1499 |
-
|
| 1500 |
-
|
| 1501 |
-
|
| 1502 |
-
|
| 1503 |
-
|
| 1504 |
-
|
| 1505 |
-
|
| 1506 |
-
|
| 1507 |
-
|
| 1508 |
-
|
| 1509 |
-
|
| 1510 |
-
|
| 1511 |
-
|
| 1512 |
-
|
| 1513 |
-
|
| 1514 |
-
|
| 1515 |
-
|
| 1516 |
-
|
| 1517 |
-
|
| 1518 |
-
|
| 1519 |
-
|
| 1520 |
-
|
| 1521 |
-
|
| 1522 |
-
|
| 1523 |
-
|
| 1524 |
-
|
| 1525 |
-
|
| 1526 |
-
|
| 1527 |
-
|
| 1528 |
-
|
| 1529 |
-
|
| 1530 |
-
|
| 1531 |
-
|
| 1532 |
-
|
| 1533 |
-
|
| 1534 |
-
|
| 1535 |
-
|
| 1536 |
-
|
| 1537 |
-
|
| 1538 |
-
|
| 1539 |
-
|
| 1540 |
-
|
| 1541 |
-
|
| 1542 |
-
|
| 1543 |
-
|
| 1544 |
-
|
| 1545 |
-
|
| 1546 |
-
|
| 1547 |
-
|
| 1548 |
-
|
| 1549 |
-
|
| 1550 |
-
|
| 1551 |
-
|
| 1552 |
-
|
| 1553 |
-
|
| 1554 |
-
|
| 1555 |
-
|
| 1556 |
-
|
| 1557 |
-
|
| 1558 |
-
|
| 1559 |
-
|
| 1560 |
-
}}
|
| 1561 |
-
```
|
| 1562 |
-
8. Based on the provided context, generate the `action_overall_flow`.
|
| 1563 |
-
|
| 1564 |
-
|
| 1565 |
-
|
| 1566 |
-
|
| 1567 |
-
|
| 1568 |
-
|
| 1569 |
-
"""
|
| 1570 |
|
| 1571 |
-
|
| 1572 |
-
|
| 1573 |
-
|
| 1574 |
-
|
| 1575 |
-
|
| 1576 |
-
|
| 1577 |
-
|
| 1578 |
-
|
| 1579 |
-
|
| 1580 |
-
|
| 1581 |
-
|
| 1582 |
-
|
| 1583 |
-
|
| 1584 |
-
|
| 1585 |
-
|
| 1586 |
-
|
| 1587 |
-
|
| 1588 |
-
|
| 1589 |
-
|
| 1590 |
-
|
| 1591 |
-
|
| 1592 |
-
|
| 1593 |
-
|
| 1594 |
-
|
| 1595 |
-
|
| 1596 |
-
|
| 1597 |
-
|
| 1598 |
-
|
| 1599 |
-
|
| 1600 |
-
|
| 1601 |
-
|
| 1602 |
-
|
| 1603 |
|
| 1604 |
-
|
| 1605 |
-
|
| 1606 |
|
| 1607 |
-
|
| 1608 |
-
|
| 1609 |
|
| 1610 |
-
|
| 1611 |
|
| 1612 |
-
|
| 1613 |
-
|
| 1614 |
-
|
| 1615 |
-
# Node 3: refiner node
|
| 1616 |
-
def refined_planner_node(state: GameState):
|
| 1617 |
-
|
| 1618 |
-
|
| 1619 |
-
|
| 1620 |
-
|
| 1621 |
-
|
| 1622 |
-
|
| 1623 |
-
|
| 1624 |
-
|
| 1625 |
-
|
| 1626 |
-
|
| 1627 |
-
|
| 1628 |
-
|
| 1629 |
-
|
| 1630 |
-
|
| 1631 |
-
|
| 1632 |
-
|
| 1633 |
-
|
| 1634 |
-
|
| 1635 |
-
|
| 1636 |
-
|
| 1637 |
-
|
| 1638 |
-
|
| 1639 |
-
|
| 1640 |
-
|
| 1641 |
-
|
| 1642 |
-
|
| 1643 |
-
Refine and correct the JSON object `action_overall_flow` so that it fully aligns with the detailed game description, sprite positions, variable/broadcast declarations, and Scratch 3.0 block reference—while also validating for common formatting or opcode errors.
|
| 1644 |
-
|
| 1645 |
-
Here is the overall script available:
|
| 1646 |
-
**Sprite name**: {sprite_name}
|
| 1647 |
-
**and its corresponding Pseudo_code:**
|
| 1648 |
-
'{pseudo}'
|
| 1649 |
-
|
| 1650 |
-
[Note: Make sure you just refine the pseudo code by correting mistake and adding the missing opcode if any and *Do not* generate any new logic]
|
| 1651 |
-
---
|
| 1652 |
-
**Targets in Game (Sprites and Stage):** {', '.join(target_names)}
|
| 1653 |
-
**Current action plan:**
|
| 1654 |
-
{current_action_plan}
|
| 1655 |
-
|
| 1656 |
-
**Validation Feedback:**
|
| 1657 |
-
'{plan_validation_feedback}'
|
| 1658 |
-
|
| 1659 |
-
--- Scratch 3.0 Block Reference ---
|
| 1660 |
-
### Hat Blocks
|
| 1661 |
-
{hat_opcodes_functionalities}
|
| 1662 |
-
|
| 1663 |
-
### Boolean Blocks
|
| 1664 |
-
{boolean_opcodes_functionalities}
|
| 1665 |
-
|
| 1666 |
-
### C Blocks
|
| 1667 |
-
{c_opcodes_functionalities}
|
| 1668 |
-
|
| 1669 |
-
### Cap Blocks
|
| 1670 |
-
{cap_opcodes_functionalities}
|
| 1671 |
-
|
| 1672 |
-
### Reporter Blocks
|
| 1673 |
-
{reporter_opcodes_functionalities}
|
| 1674 |
-
|
| 1675 |
-
### Stack Blocks
|
| 1676 |
-
{stack_opcodes_functionalities}
|
| 1677 |
-
|
| 1678 |
-
-----------------------------------
|
| 1679 |
-
|
| 1680 |
-
* **Your task is to align to description, refine and correct the JSON object 'action_overall_flow'.**
|
| 1681 |
-
Use sprite names exactly as provided in `sprite_names` (e.g., 'Sprite1', 'soccer ball'); and also the stage, do **NOT** rename them.
|
| 1682 |
-
Other plans may use any hat blocks (including duplicates) to represent different logics or events.
|
| 1683 |
-
Each plan must include a **single Scratch Hat Block** (e.g., 'event_whenflagclicked') to start scratch project and should contain:
|
| 1684 |
-
1. **'event'**: the exact `opcode` of the hat block that initiates the logic.
|
| 1685 |
-
2. **'logic'**: a natural language breakdown of each step taken after the event, formatted as a multi-line string representing pseudo-code. Ensure clarity and granularity—each described action should map closely to a Scratch block or tight sequence.
|
| 1686 |
-
|
| 1687 |
-
|
| 1688 |
-
|
| 1689 |
-
|
| 1690 |
-
|
| 1691 |
-
|
| 1692 |
-
|
| 1693 |
-
|
| 1694 |
-
|
| 1695 |
-
|
| 1696 |
-
|
| 1697 |
-
|
| 1698 |
-
|
| 1699 |
-
|
| 1700 |
-
|
| 1701 |
-
3. **Validation & Formatting Checks**
|
| 1702 |
-
|
| 1703 |
-
|
| 1704 |
-
|
| 1705 |
-
|
| 1706 |
-
|
| 1707 |
-
|
| 1708 |
-
|
| 1709 |
-
4. **Opcode Lists**: include relevant Scratch opcodes grouped under `motion`, `control`, `operator`, `sensing`, `looks`, `sounds`, `events`, and `data`. List only the non-empty categories. Use exact opcodes from the given Scratch 3.0 Block Reference.
|
| 1710 |
-
5. Few Example of content of logics inside for a specific plan as scratch pseudo-code:
|
| 1711 |
-
|
| 1712 |
-
|
| 1713 |
-
|
| 1714 |
-
|
| 1715 |
-
|
| 1716 |
-
|
| 1717 |
-
|
| 1718 |
-
|
| 1719 |
-
|
| 1720 |
-
|
| 1721 |
-
|
| 1722 |
-
|
| 1723 |
-
|
| 1724 |
-
|
| 1725 |
-
|
| 1726 |
-
|
| 1727 |
-
|
| 1728 |
-
|
| 1729 |
-
|
| 1730 |
-
|
| 1731 |
-
|
| 1732 |
-
|
| 1733 |
-
|
| 1734 |
-
|
| 1735 |
-
|
| 1736 |
-
|
| 1737 |
-
|
| 1738 |
-
|
| 1739 |
-
|
| 1740 |
-
|
| 1741 |
-
|
| 1742 |
-
|
| 1743 |
-
|
| 1744 |
-
|
| 1745 |
-
6. Use target names exactly as listed in `Targets in Game`. Do NOT rename or invent new targets.
|
| 1746 |
-
7. Ensure the plan reflects accurate opcode usage derived strictly from the block reference above.
|
| 1747 |
-
8. Few shot Example structure for 'action_overall_flow':
|
| 1748 |
-
```json
|
| 1749 |
-
|
| 1750 |
-
|
| 1751 |
-
|
| 1752 |
-
|
| 1753 |
-
|
| 1754 |
-
|
| 1755 |
-
|
| 1756 |
-
|
| 1757 |
-
|
| 1758 |
-
|
| 1759 |
-
|
| 1760 |
-
|
| 1761 |
-
|
| 1762 |
-
|
| 1763 |
-
|
| 1764 |
-
|
| 1765 |
-
|
| 1766 |
-
|
| 1767 |
-
|
| 1768 |
-
|
| 1769 |
-
|
| 1770 |
-
|
| 1771 |
-
|
| 1772 |
-
|
| 1773 |
-
|
| 1774 |
-
|
| 1775 |
-
|
| 1776 |
-
|
| 1777 |
-
|
| 1778 |
-
|
| 1779 |
-
|
| 1780 |
-
|
| 1781 |
-
|
| 1782 |
-
|
| 1783 |
-
|
| 1784 |
-
|
| 1785 |
-
|
| 1786 |
-
|
| 1787 |
-
|
| 1788 |
-
|
| 1789 |
-
|
| 1790 |
-
|
| 1791 |
-
|
| 1792 |
-
|
| 1793 |
-
|
| 1794 |
-
|
| 1795 |
-
|
| 1796 |
-
|
| 1797 |
-
|
| 1798 |
-
|
| 1799 |
-
|
| 1800 |
-
|
| 1801 |
-
|
| 1802 |
-
|
| 1803 |
-
|
| 1804 |
-
|
| 1805 |
-
|
| 1806 |
-
|
| 1807 |
-
|
| 1808 |
-
|
| 1809 |
-
|
| 1810 |
-
|
| 1811 |
-
|
| 1812 |
-
|
| 1813 |
-
|
| 1814 |
-
|
| 1815 |
-
|
| 1816 |
-
|
| 1817 |
-
|
| 1818 |
-
|
| 1819 |
-
|
| 1820 |
-
|
| 1821 |
-
|
| 1822 |
-
|
| 1823 |
-
|
| 1824 |
-
|
| 1825 |
-
|
| 1826 |
-
|
| 1827 |
-
|
| 1828 |
-
|
| 1829 |
-
|
| 1830 |
-
|
| 1831 |
-
|
| 1832 |
-
|
| 1833 |
-
|
| 1834 |
-
|
| 1835 |
-
|
| 1836 |
-
|
| 1837 |
-
|
| 1838 |
-
|
| 1839 |
-
|
| 1840 |
-
|
| 1841 |
-
|
| 1842 |
-
|
| 1843 |
-
|
| 1844 |
-
|
| 1845 |
-
|
| 1846 |
-
|
| 1847 |
-
|
| 1848 |
-
|
| 1849 |
-
|
| 1850 |
-
|
| 1851 |
-
|
| 1852 |
-
|
| 1853 |
-
|
| 1854 |
-
|
| 1855 |
-
|
| 1856 |
-
|
| 1857 |
-
|
| 1858 |
-
|
| 1859 |
-
|
| 1860 |
-
|
| 1861 |
-
|
| 1862 |
-
|
| 1863 |
-
|
| 1864 |
-
|
| 1865 |
-
}}
|
| 1866 |
-
```
|
| 1867 |
-
9. Use the validation feedback to address errors, fill in missing logic, or enhance clarity.
|
| 1868 |
-
|
| 1869 |
-
|
| 1870 |
-
|
| 1871 |
-
|
| 1872 |
-
|
| 1873 |
-
|
| 1874 |
-
|
| 1875 |
-
"""
|
| 1876 |
-
|
| 1877 |
-
|
| 1878 |
-
|
| 1879 |
-
|
| 1880 |
-
|
| 1881 |
-
|
| 1882 |
-
|
| 1883 |
-
|
| 1884 |
-
|
| 1885 |
-
|
| 1886 |
-
|
| 1887 |
-
|
| 1888 |
-
|
| 1889 |
-
|
| 1890 |
-
|
| 1891 |
-
|
| 1892 |
-
|
| 1893 |
-
|
| 1894 |
-
|
| 1895 |
-
|
| 1896 |
-
|
| 1897 |
-
|
| 1898 |
-
|
| 1899 |
-
|
| 1900 |
-
|
| 1901 |
-
|
| 1902 |
-
|
| 1903 |
-
|
| 1904 |
-
|
| 1905 |
-
|
| 1906 |
-
|
| 1907 |
-
|
| 1908 |
-
|
| 1909 |
|
| 1910 |
-
|
| 1911 |
-
|
| 1912 |
-
|
| 1913 |
-
|
| 1914 |
-
|
| 1915 |
-
|
| 1916 |
-
|
| 1917 |
-
|
| 1918 |
|
| 1919 |
-
|
| 1920 |
-
|
| 1921 |
|
| 1922 |
-
|
| 1923 |
-
|
| 1924 |
-
|
| 1925 |
-
|
| 1926 |
-
# Node 4: opcode counter node
|
| 1927 |
-
def plan_opcode_counter_node(state: Dict[str, Any]) -> Dict[str, Any]:
|
| 1928 |
-
|
| 1929 |
-
|
| 1930 |
-
|
| 1931 |
-
|
| 1932 |
-
|
| 1933 |
-
|
| 1934 |
-
|
| 1935 |
-
|
| 1936 |
-
|
| 1937 |
-
|
| 1938 |
|
| 1939 |
-
|
| 1940 |
|
| 1941 |
-
|
| 1942 |
-
|
| 1943 |
-
|
| 1944 |
-
|
| 1945 |
|
| 1946 |
-
|
| 1947 |
-
|
| 1948 |
-
|
| 1949 |
-
|
| 1950 |
-
|
| 1951 |
-
|
| 1952 |
|
| 1953 |
-
|
| 1954 |
-
|
| 1955 |
-
|
| 1956 |
-
|
| 1957 |
-
|
| 1958 |
-
|
| 1959 |
-
|
| 1960 |
-
|
| 1961 |
-
|
| 1962 |
-
|
| 1963 |
-
|
| 1964 |
-
|
| 1965 |
-
|
| 1966 |
-
You are a Scratch 3.0 expert with deep knowledge of block types, nesting and stack relationships.
|
| 1967 |
-
Your job: read the plan logic below and decide exactly which blocks (and how many of each) are required to implement it.
|
| 1968 |
-
Review the following plan for '{sprite}' triggered by '{event}'.
|
| 1969 |
-
--- Scratch 3.0 Block Reference ---
|
| 1970 |
-
|
| 1971 |
-
|
| 1972 |
-
|
| 1973 |
-
|
| 1974 |
-
|
| 1975 |
-
|
| 1976 |
-
|
| 1977 |
-
|
| 1978 |
-
|
| 1979 |
-
|
| 1980 |
-
|
| 1981 |
-
|
| 1982 |
-
|
| 1983 |
-
|
| 1984 |
-
|
| 1985 |
-
|
| 1986 |
-
|
| 1987 |
-
|
| 1988 |
-
|
| 1989 |
-
|
| 1990 |
-
|
| 1991 |
-
|
| 1992 |
-
|
| 1993 |
-
|
| 1994 |
-
|
| 1995 |
-
|
| 1996 |
-
|
| 1997 |
-
|
| 1998 |
-
|
| 1999 |
-
-----------------------------------
|
| 2000 |
-
Current Plan Details:
|
| 2001 |
-
- Event (Hat Block Opcode): {event}
|
| 2002 |
-
- Associated Opcodes by Category: {json.dumps(opcodes_from_plan, indent=2)}
|
| 2003 |
-
|
| 2004 |
-
── Game Context ──
|
| 2005 |
-
Sprite: "{sprite}"
|
| 2006 |
-
|
| 2007 |
-
── Current Plan ──
|
| 2008 |
-
Event (hat block): {event}
|
| 2009 |
-
Logic (pseudo-Scratch): {logic}
|
| 2010 |
-
Plan : {plan}
|
| 2011 |
-
|
| 2012 |
-
── Opcode Candidates ──
|
| 2013 |
-
Motion: {opcodes_from_plan["motion"]}
|
| 2014 |
-
Control: {opcodes_from_plan["control"]}
|
| 2015 |
-
Operator: {opcodes_from_plan["operator"]}
|
| 2016 |
-
Sensing: {opcodes_from_plan["sensing"]}
|
| 2017 |
-
Looks: {opcodes_from_plan["looks"]}
|
| 2018 |
-
Sounds: {opcodes_from_plan["sounds"]}
|
| 2019 |
-
Events: {opcodes_from_plan["events"]}
|
| 2020 |
-
Data: {opcodes_from_plan["data"]}
|
| 2021 |
-
|
| 2022 |
-
── Your Task ──
|
| 2023 |
-
1. Analyze the “Logic” steps and choose exactly which opcodes are needed.
|
| 2024 |
-
2. Use exact opcodes from the given Scratch 3.0 Block Reference and verfiy the proper opcode are used available in the Scratch 3.0 Block Reference.
|
| 2025 |
-
3. Return a top-level JSON object with a single key: "opcode_counts".
|
| 2026 |
-
4. The value of "opcode_counts" should be a list of objects, where each object has "opcode": "<opcode_name>" and "count": <integer>.
|
| 2027 |
-
5. Ensure the list includes the hat block for this plan (e.g., event_whenflagclicked, event_whenkeypressed, event_whenbroadcastreceived) with a count of 1.
|
| 2028 |
-
6. The order of opcodes within the "opcode_counts" list does not matter.
|
| 2029 |
-
7. If any plan logic is None do not generate the opcode_counts for it.
|
| 2030 |
-
8. Use only double quotes and ensure valid JSON.
|
| 2031 |
-
|
| 2032 |
-
Example output:
|
| 2033 |
-
**example 1**
|
| 2034 |
-
```json
|
| 2035 |
-
{{
|
| 2036 |
-
"opcode_counts":[
|
| 2037 |
-
|
| 2038 |
-
|
| 2039 |
-
|
| 2040 |
-
|
| 2041 |
-
|
| 2042 |
-
|
| 2043 |
-
|
| 2044 |
-
|
| 2045 |
-
|
| 2046 |
-
}}
|
| 2047 |
-
```
|
| 2048 |
-
**example 2**
|
| 2049 |
-
```json
|
| 2050 |
-
{{
|
| 2051 |
-
"opcode_counts":[
|
| 2052 |
-
|
| 2053 |
-
|
| 2054 |
-
|
| 2055 |
-
|
| 2056 |
-
|
| 2057 |
-
|
| 2058 |
-
|
| 2059 |
-
|
| 2060 |
-
|
| 2061 |
-
|
| 2062 |
-
|
| 2063 |
-
}}
|
| 2064 |
-
```
|
| 2065 |
-
"""
|
| 2066 |
-
|
| 2067 |
-
|
| 2068 |
-
|
| 2069 |
-
|
| 2070 |
-
|
| 2071 |
-
|
| 2072 |
-
|
| 2073 |
-
|
| 2074 |
-
|
| 2075 |
-
|
| 2076 |
-
|
| 2077 |
-
|
| 2078 |
-
|
| 2079 |
-
|
| 2080 |
-
|
| 2081 |
-
|
| 2082 |
-
|
| 2083 |
-
|
| 2084 |
-
|
| 2085 |
-
|
| 2086 |
-
|
| 2087 |
-
|
| 2088 |
-
|
| 2089 |
-
|
| 2090 |
-
|
| 2091 |
-
|
| 2092 |
-
|
| 2093 |
-
|
| 2094 |
-
|
| 2095 |
-
|
| 2096 |
-
|
| 2097 |
-
|
| 2098 |
-
|
| 2099 |
-
|
| 2100 |
-
|
| 2101 |
-
|
| 2102 |
-
|
| 2103 |
-
|
| 2104 |
-
|
| 2105 |
-
|
| 2106 |
-
|
| 2107 |
-
|
| 2108 |
-
|
| 2109 |
|
| 2110 |
-
|
| 2111 |
-
|
| 2112 |
-
|
| 2113 |
|
| 2114 |
-
|
| 2115 |
-
|
| 2116 |
-
|
| 2117 |
-
|
| 2118 |
-
|
| 2119 |
|
| 2120 |
# Node 5: block_builder_node
|
| 2121 |
def overall_block_builder_node_2(state: GameState):
|
|
@@ -2228,6 +2228,41 @@ def variable_adder_node(state: GameState):
|
|
| 2228 |
raise
|
| 2229 |
|
| 2230 |
# Node 7: variable adder node
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2231 |
def processed_page_node(state: GameState):
|
| 2232 |
logger.info("--- Processing the Pages Node ---")
|
| 2233 |
image = state.get("project_image", "")
|
|
@@ -3626,6 +3661,7 @@ workflow = StateGraph(GameState)
|
|
| 3626 |
workflow.add_node("pseudo_generator", pseudo_generator_node)
|
| 3627 |
#workflow.add_node("plan_generator", overall_planner_node)
|
| 3628 |
workflow.add_node("Node_optimizer", node_optimizer)
|
|
|
|
| 3629 |
#workflow.add_node("logic_alignment", plan_logic_aligner_node)
|
| 3630 |
#workflow.add_node("plan_verifier", plan_verification_node)
|
| 3631 |
#workflow.add_node("refined_planner", refined_planner_node) # Refines the action plan
|
|
@@ -3640,14 +3676,15 @@ def decide_next_step(state: GameState):
|
|
| 3640 |
if state.get("processing", False):
|
| 3641 |
return "pseudo_generator"
|
| 3642 |
else:
|
| 3643 |
-
return END
|
| 3644 |
|
| 3645 |
workflow.add_conditional_edges(
|
| 3646 |
"page_processed",
|
| 3647 |
decide_next_step,
|
| 3648 |
{
|
| 3649 |
"pseudo_generator": "pseudo_generator",
|
| 3650 |
-
|
|
|
|
| 3651 |
}
|
| 3652 |
)
|
| 3653 |
# Main chain
|
|
@@ -3660,9 +3697,9 @@ workflow.add_edge("pseudo_generator", "Node_optimizer")
|
|
| 3660 |
# workflow.add_edge("time_delay_3", "opcode_counter")
|
| 3661 |
workflow.add_edge("Node_optimizer", "block_builder")
|
| 3662 |
workflow.add_edge("block_builder", "variable_initializer")
|
| 3663 |
-
|
| 3664 |
# After last node, check again
|
| 3665 |
workflow.add_edge("variable_initializer", "page_processed")
|
|
|
|
| 3666 |
|
| 3667 |
app_graph = workflow.compile()
|
| 3668 |
|
|
|
|
| 1254 |
logger.info("Plan refinement and block relation analysis completed for all plans.")
|
| 1255 |
return state
|
| 1256 |
|
| 1257 |
+
# Node2: Node Optimizer node
|
| 1258 |
def node_optimizer(state: GameState):
|
| 1259 |
logger.info("--- Running Node Optimizer Node ---")
|
| 1260 |
project_json = state["project_json"]
|
|
|
|
| 1302 |
logger.error(f"Error in Node Optimizer Node: {e}")
|
| 1303 |
|
| 1304 |
# Node 2: planner node
|
| 1305 |
+
# def overall_planner_node(state: GameState):
|
| 1306 |
+
# """
|
| 1307 |
+
# Generates a comprehensive action plan for sprites, including detailed Scratch block information.
|
| 1308 |
+
# This node acts as an overall planner, leveraging knowledge of all block shapes and categories.
|
| 1309 |
+
# """
|
| 1310 |
+
# logger.info("--- Running OverallPlannerNode ---")
|
| 1311 |
+
|
| 1312 |
+
# project_json = state["project_json"]
|
| 1313 |
+
# raw = state.get("pseudo_code", {})
|
| 1314 |
+
# refined_logic_data = raw.get("refined_logic", {})
|
| 1315 |
+
# sprite_name = refined_logic_data.get("name_variable", "<unknown>")
|
| 1316 |
+
# pseudo = refined_logic_data.get("pseudocode", "")
|
| 1317 |
+
|
| 1318 |
+
# # MODIFICATION 1: Include 'Stage' in the list of names to plan for.
|
| 1319 |
+
# # It's crucial to ensure 'Stage' is always present for its global role.
|
| 1320 |
+
# target_names = [t["name"] for t in project_json["targets"]]
|
| 1321 |
+
|
| 1322 |
+
# # MODIFICATION 2: Get sprite positions, providing default for Stage as it doesn't have x,y
|
| 1323 |
+
# sprite_positions = {}
|
| 1324 |
+
# for target in project_json["targets"]:
|
| 1325 |
+
# if not target["isStage"]:
|
| 1326 |
+
# sprite_positions[target["name"]] = {"x": target.get("x", 0), "y": target.get("y", 0)}
|
| 1327 |
+
# else:
|
| 1328 |
+
# sprite_positions[target["name"]] = {"x": "N/A", "y": "N/A"} # Stage doesn't have positional coordinates
|
| 1329 |
+
|
| 1330 |
+
# # declaration_plan = state["declaration_plan"]
|
| 1331 |
+
|
| 1332 |
+
# planning_prompt = f"""
|
| 1333 |
+
# Generate a detailed action plan for the game's sprites and stage based on the given pseudo-code and sprite details for the given sprite name and .
|
| 1334 |
+
|
| 1335 |
+
# Description:
|
| 1336 |
+
# **Sprite_name**: {sprite_name}
|
| 1337 |
+
# **and its corresponding Pseudo_code:**
|
| 1338 |
+
# '{pseudo}'
|
| 1339 |
+
|
| 1340 |
+
# [Note: Make sure you just refine the pseudo code by correting mistake and adding the missing opcode if any and *Do not* generate any new logic]
|
| 1341 |
+
# ----
|
| 1342 |
+
# **Targets in Game (Sprites and Stage) available in project_json:** {', '.join(target_names)}
|
| 1343 |
+
|
| 1344 |
+
# --- Scratch 3.0 Block Reference ---
|
| 1345 |
+
# This section provides a comprehensive reference of Scratch 3.0 blocks, categorized by shape, including their opcodes and functional descriptions. Use this to accurately identify block types and behavior.
|
| 1346 |
+
|
| 1347 |
+
# ### Hat Blocks
|
| 1348 |
+
# Description: {hat_description}
|
| 1349 |
+
# Blocks:
|
| 1350 |
+
# {hat_opcodes_functionalities}
|
| 1351 |
+
|
| 1352 |
+
# ### Boolean Blocks
|
| 1353 |
+
# Description: {boolean_description}
|
| 1354 |
+
# Blocks:
|
| 1355 |
+
# {boolean_opcodes_functionalities}
|
| 1356 |
+
|
| 1357 |
+
# ### C Blocks
|
| 1358 |
+
# Description: {c_description}
|
| 1359 |
+
# Blocks:
|
| 1360 |
+
# {c_opcodes_functionalities}
|
| 1361 |
+
|
| 1362 |
+
# ### Cap Blocks
|
| 1363 |
+
# Description: {cap_description}
|
| 1364 |
+
# Blocks:
|
| 1365 |
+
# {cap_opcodes_functionalities}
|
| 1366 |
+
|
| 1367 |
+
# ### Reporter Blocks
|
| 1368 |
+
# Description: {reporter_description}
|
| 1369 |
+
# Blocks:
|
| 1370 |
+
# {reporter_opcodes_functionalities}
|
| 1371 |
+
|
| 1372 |
+
# ### Stack Blocks
|
| 1373 |
+
# Description: {stack_description}
|
| 1374 |
+
# Blocks:
|
| 1375 |
+
# {stack_opcodes_functionalities}
|
| 1376 |
+
|
| 1377 |
+
# -----------------------------------
|
| 1378 |
+
|
| 1379 |
+
# Your task is to use the `Sprite_name` given and `Pseudo_code` and add it to the specific target name and define the primary actions and movements.
|
| 1380 |
+
# The output should be a JSON object with a single key 'action_overall_flow'. Each key inside this object should be a sprite or 'Stage' name (e.g., 'Player', 'Enemy', 'Stage'), and its value must include a 'description' and a list of 'plans'.
|
| 1381 |
+
# The first plan in each stage must start with one Scratch Hat Block (e.g., event_whenflagclicked).
|
| 1382 |
+
# Other plans may use any hat blocks (including duplicates) to represent different logics or events.
|
| 1383 |
+
# Each plan must include a **single Scratch Hat Block** (e.g., 'event_whenflagclicked') to start scratch project and should contain:
|
| 1384 |
+
|
| 1385 |
+
# 1. **'event'**: the exact `opcode` of the hat block that initiates the logic.
|
| 1386 |
+
# [NOTE: INSTRUCTIONN TO FOLLOW IF PSEUDO_CODE HAVING PROBLEM ]
|
| 1387 |
+
# 2. **'logic'**: a natural language breakdown of each step taken after the event, formatted as a multi-line string representing pseudo-code. Ensure clarity and granularity—each described action should map closely to a Scratch block or tight sequence.
|
| 1388 |
+
# - Use 'forever: ...' or 'repeat(10): ...' to prefix repeating logic suitable taking reference from the C blocks.
|
| 1389 |
+
# - Use Scratch-consistent verbs: 'move', 'change', 'wait', 'hide', 'show', 'say', 'glide', etc.
|
| 1390 |
+
# - **Numeric values** `(e.g., 0, 5, 0.2, -130)` **must** be in parentheses: `(0)`, `(5)`, `(0.2)`, `(-130)`.
|
| 1391 |
+
# - **AlphaNumeric values** `(e.g., hello, say 5, 4, hi!)` **must** be in parentheses: `(hello)`, `(say 5)`, `(4)`, `(hi!)`.
|
| 1392 |
+
# - **Variables** must be in the form `[variable v]` (e.g., `[score v]`), even when used inside expressions two example use `set [score v] to (1)` or `show variable ([speed v])`.
|
| 1393 |
+
# - **Dropdown options** must be in the form `[option v]` (e.g., `[Game Start v]`, `[blue sky v]`). example use `when [space v] key pressed`.
|
| 1394 |
+
# - **Reporter blocks** used as inputs must be double‑wrapped: `((x position))`, `((y position))`. example use `if <((y position)) = (-130)> then` or `(((x position)) * (1))`.
|
| 1395 |
+
# - **Boolean blocks** in conditions must be inside `< >`, including nested ones: `<not <condition>>`, `<<cond1> and <cond2>>`,`<<cond1> or <cond2>>`.
|
| 1396 |
+
# - **Other Boolean blocks** in conditions must be inside `< >`, including nested ones or values or variables: `<(block/value/variable) * (block/value/variable)>`,`<(block/value/variable) < (block/value/variable)>`, and example of another variable`<[apple v] contains [a v]?>`.
|
| 1397 |
+
# - **Operator expressions** must use explicit Scratch operator blocks, e.g.:
|
| 1398 |
+
# ```
|
| 1399 |
+
# (([ballSpeed v]) * (1.1))
|
| 1400 |
+
# ```
|
| 1401 |
+
# - **Every hat block script must end** with a final `end` on its own line.
|
| 1402 |
+
# - **Indent nested blocks by 4 spaces under their parent (`forever`, `if`, etc.).This is a critical requirement.**
|
| 1403 |
+
# 3. **Opcode Lists**: include relevant Scratch opcodes grouped under `motion`, `control`, `operator`, `sensing`, `looks`, `sounds`, `events`, and `data`. List only the non-empty categories. Use exact opcodes.
|
| 1404 |
+
# 4. Few Example of content of logics inside for a specific plan as scratch pseudo-code:
|
| 1405 |
+
# - example 1[continues moving objects]:
|
| 1406 |
+
# ```
|
| 1407 |
+
# when green flag clicked
|
| 1408 |
+
# go to x: (240) y: (-100)
|
| 1409 |
+
# set [speed v] to (-5)
|
| 1410 |
+
# show variable [speed v]
|
| 1411 |
+
# forever
|
| 1412 |
+
# change x by ([speed v])
|
| 1413 |
+
# if <((x position)) < (-240)> then
|
| 1414 |
+
# go to x: (240) y: (-100)
|
| 1415 |
+
# end
|
| 1416 |
+
# end
|
| 1417 |
+
# end
|
| 1418 |
+
# ```
|
| 1419 |
+
# - example 2[jumping script of an plan]:
|
| 1420 |
+
# ```
|
| 1421 |
+
# when [space v] key pressed
|
| 1422 |
+
# if <((y position)) = (-100)> then
|
| 1423 |
+
# repeat (5)
|
| 1424 |
+
# change y by (100)
|
| 1425 |
+
# wait (0.1) seconds
|
| 1426 |
+
# change y by (-100)
|
| 1427 |
+
# wait (0.1) seconds
|
| 1428 |
+
# end
|
| 1429 |
+
# end
|
| 1430 |
+
# end
|
| 1431 |
+
# ```
|
| 1432 |
+
# - example 3 [pattern for level up and increase difficulty]:
|
| 1433 |
+
# ```
|
| 1434 |
+
# when I receive [Level Up v]
|
| 1435 |
+
# change [level v] by (1)
|
| 1436 |
+
# set [ballSpeed v] to ((([ballSpeed v]) * (1.1)))
|
| 1437 |
+
# end
|
| 1438 |
+
# ```
|
| 1439 |
+
# 5. Use target names exactly as listed in `Targets in Game`. Do NOT rename or invent new targets.
|
| 1440 |
+
# 6. Ensure the plan reflects accurate opcode usage derived strictly from the block reference above.
|
| 1441 |
+
# 7. Few shot Example structure for 'action_overall_flow':
|
| 1442 |
+
# ```json
|
| 1443 |
+
# {{
|
| 1444 |
+
# "action_overall_flow": {{
|
| 1445 |
+
# "Stage": {{
|
| 1446 |
+
# "description": "Background and global game state management, including broadcasts, rewards, and score.",
|
| 1447 |
+
# "plans": [
|
| 1448 |
+
# {{
|
| 1449 |
+
# "event": "event_whenflagclicked",
|
| 1450 |
+
# "logic": "when green flag clicked\n switch backdrop to [backdrop1 v]\n set [score v] to 0\n show variable [score v]\n broadcast [Game Start v]\nend",
|
| 1451 |
+
# "motion": [],
|
| 1452 |
+
# "control": [],
|
| 1453 |
+
# "operator": [],
|
| 1454 |
+
# "sensing": [],
|
| 1455 |
+
# "looks": [
|
| 1456 |
+
# "looks_switchbackdropto"
|
| 1457 |
+
# ],
|
| 1458 |
+
# "sounds": [],
|
| 1459 |
+
# "events": [
|
| 1460 |
+
# "event_broadcast"
|
| 1461 |
+
# ],
|
| 1462 |
+
# "data": [
|
| 1463 |
+
# "data_setvariableto",
|
| 1464 |
+
# "data_showvariable"
|
| 1465 |
+
# ]
|
| 1466 |
+
# }},
|
| 1467 |
+
# {{
|
| 1468 |
+
# "event": "event_whenbroadcastreceived",
|
| 1469 |
+
# "logic": "when I receive [Game Over v]\n if <(score) > (High Score)> then\n set [High Score v] to (score)\n end\n switch backdrop to [HighScore v]\nend",
|
| 1470 |
+
# "motion": [],
|
| 1471 |
+
# "control": [
|
| 1472 |
+
# "control_if"
|
| 1473 |
+
# ],
|
| 1474 |
+
# "operator": [
|
| 1475 |
+
# "operator_gt"
|
| 1476 |
+
# ],
|
| 1477 |
+
# "sensing": [],
|
| 1478 |
+
# "looks": [
|
| 1479 |
+
# "looks_switchbackdropto"
|
| 1480 |
+
# ],
|
| 1481 |
+
# "sounds": [],
|
| 1482 |
+
# "events": [],
|
| 1483 |
+
# "data": [
|
| 1484 |
+
# "data_setvariableto"
|
| 1485 |
+
# ]
|
| 1486 |
+
# }}
|
| 1487 |
+
# ]
|
| 1488 |
+
# }},
|
| 1489 |
+
# "Sprite1": {{
|
| 1490 |
+
# "description": "Main character (cat) actions",
|
| 1491 |
+
# "plans": [
|
| 1492 |
+
# {{
|
| 1493 |
+
# "event": "event_whenflagclicked",
|
| 1494 |
+
# "logic": "when green flag clicked\n go to x: 240 y: -100\nend",
|
| 1495 |
+
# "motion": [
|
| 1496 |
+
# "motion_gotoxy"
|
| 1497 |
+
# ],
|
| 1498 |
+
# "control": [],
|
| 1499 |
+
# "operator": [],
|
| 1500 |
+
# "sensing": [],
|
| 1501 |
+
# "looks": [],
|
| 1502 |
+
# "sounds": [],
|
| 1503 |
+
# "events": [],
|
| 1504 |
+
# "data": []
|
| 1505 |
+
# }},
|
| 1506 |
+
# {{
|
| 1507 |
+
# "event": "event_whenkeypressed",
|
| 1508 |
+
# "logic": "when [space v] key pressed\n repeat (10)\n change y by (20)\n wait (0.1) seconds\n change y by (-20)\n end\nend",
|
| 1509 |
+
# "motion": [
|
| 1510 |
+
# "motion_changeyby"
|
| 1511 |
+
# ],
|
| 1512 |
+
# "control": [
|
| 1513 |
+
# "control_repeat",
|
| 1514 |
+
# "control_wait"
|
| 1515 |
+
# ],
|
| 1516 |
+
# "operator": [],
|
| 1517 |
+
# "sensing": [],
|
| 1518 |
+
# "looks": [],
|
| 1519 |
+
# "sounds": [],
|
| 1520 |
+
# "events": [],
|
| 1521 |
+
# "data": []
|
| 1522 |
+
# }}
|
| 1523 |
+
# ]
|
| 1524 |
+
# }},
|
| 1525 |
+
# "soccer ball": {{
|
| 1526 |
+
# "description": "Obstacle movement and interaction",
|
| 1527 |
+
# "plans": [
|
| 1528 |
+
# {{
|
| 1529 |
+
# "event": "event_whenflagclicked",
|
| 1530 |
+
# "logic": "when green flag clicked\n go to x: 240 y: -135\n forever\n glide 2 seconds to x: -240 y: -135\n if <(x position) < -235> then\n set x to 240\n end\n if <touching [Sprite1 v]?> then\n broadcast [Game Over v]\n stop [all v]\n end\n end\nend",
|
| 1531 |
+
# "motion": [
|
| 1532 |
+
# "motion_gotoxy",
|
| 1533 |
+
# "motion_glidesecstoxy",
|
| 1534 |
+
# "motion_xposition",
|
| 1535 |
+
# "motion_setx"
|
| 1536 |
+
# ],
|
| 1537 |
+
# "control": [
|
| 1538 |
+
# "control_forever",
|
| 1539 |
+
# "control_if",
|
| 1540 |
+
# "control_stop"
|
| 1541 |
+
# ],
|
| 1542 |
+
# "operator": [
|
| 1543 |
+
# "operator_lt"
|
| 1544 |
+
# ],
|
| 1545 |
+
# "sensing": [
|
| 1546 |
+
# "sensing_touchingobject",
|
| 1547 |
+
# "sensing_touchingobjectmenu"
|
| 1548 |
+
# ],
|
| 1549 |
+
# "looks": [],
|
| 1550 |
+
# "sounds": [],
|
| 1551 |
+
# "events": [
|
| 1552 |
+
# "event_broadcast"
|
| 1553 |
+
# ],
|
| 1554 |
+
# "data": []
|
| 1555 |
+
# }}
|
| 1556 |
+
# ]
|
| 1557 |
+
# }}
|
| 1558 |
+
|
| 1559 |
+
# }}
|
| 1560 |
+
# }}
|
| 1561 |
+
# ```
|
| 1562 |
+
# 8. Based on the provided context, generate the `action_overall_flow`.
|
| 1563 |
+
# - Maintain the **exact JSON structure** shown above.
|
| 1564 |
+
# - All `logic` fields must be **clear and granular**.
|
| 1565 |
+
# - Only include opcode categories that contain relevant opcodes.
|
| 1566 |
+
# - Ensure that each opcode matches its intended Scratch functionality.
|
| 1567 |
+
# - If feedback suggests major change, **rethink the entire plan** for the affected sprite(s).
|
| 1568 |
+
# - If feedback is minor, make precise, minimal improvements only.
|
| 1569 |
+
# """
|
| 1570 |
|
| 1571 |
+
# try:
|
| 1572 |
+
# response = agent.invoke({"messages": [{"role": "user", "content": planning_prompt}]})
|
| 1573 |
+
# print("Raw response from LLM [OverallPlannerNode 1]:",response)
|
| 1574 |
+
# raw_response = response["messages"][-1].content#strip_noise(response["messages"][-1].content)
|
| 1575 |
+
# print("Raw response from LLM [OverallPlannerNode 2]:", raw_response) # Uncomment for debugging
|
| 1576 |
+
# # json debugging and solving
|
| 1577 |
+
# try:
|
| 1578 |
+
# overall_plan = extract_json_from_llm_response(raw_response)
|
| 1579 |
+
# except json.JSONDecodeError as error_json:
|
| 1580 |
+
# logger.error("Failed to extract JSON from LLM response. Attempting to correct the response.")
|
| 1581 |
+
# # Use the JSON resolver agent to fix the response
|
| 1582 |
+
# correction_prompt = (
|
| 1583 |
+
# "Your task is to correct the provided JSON string to ensure it is **syntactically perfect and adheres strictly to JSON rules**.\n"
|
| 1584 |
+
# "Carefully review the JSON for any errors, especially focusing on the reported error at:\n"
|
| 1585 |
+
# f"- **Error Details**: {error_json}\n\n"
|
| 1586 |
+
# "**Strict Instructions for your response:**\n"
|
| 1587 |
+
# "1. **ONLY** output the corrected JSON. Do not include any other text, comments, or explanations outside the JSON.\n"
|
| 1588 |
+
# "2. Ensure all property names (keys) are enclosed in **double quotes**.\n"
|
| 1589 |
+
# "3. Ensure string values are correctly enclosed in **double quotes** and any internal special characters (like newlines `\\n`, tabs `\\t`, backslashes `\\\\`, or double quotes `\\`) are properly **escaped**.\n"
|
| 1590 |
+
# "4. Verify that there are **no extra commas**, especially between key-value pairs or after the last element in an object or array.\n"
|
| 1591 |
+
# "5. Ensure proper nesting and matching of curly braces `{}` and square brackets `[]`.\n"
|
| 1592 |
+
# "6. **Crucially, remove any extraneous characters or duplicate closing braces outside the main JSON object.**\n"
|
| 1593 |
+
# "7. The corrected JSON must be a **complete and valid** JSON object.\n\n"
|
| 1594 |
+
# "Here is the problematic JSON string to correct:\n"
|
| 1595 |
+
# "```json\n"
|
| 1596 |
+
# f"{raw_response}\n"
|
| 1597 |
+
# "```\n"
|
| 1598 |
+
# "Corrected JSON:\n"
|
| 1599 |
+
# )
|
| 1600 |
+
# correction_response = agent_json_resolver.invoke({"messages": [{"role": "user", "content": correction_prompt}]})
|
| 1601 |
+
# print(f"[JSON CORRECTOR RESPONSE AT OVERALLPLANNERNODE ]: {correction_response['messages'][-1].content}")
|
| 1602 |
+
# overall_plan= extract_json_from_llm_response(correction_response['messages'][-1].content)#strip_noise(correction_response["messages"][-1].content))
|
| 1603 |
|
| 1604 |
+
# state["action_plan"] = overall_plan
|
| 1605 |
+
# logger.info("Overall plan generated by OverallPlannerNode.")
|
| 1606 |
|
| 1607 |
+
# # with open("debug_state.json", "w", encoding="utf-8") as f:
|
| 1608 |
+
# # json.dump(state, f, indent=2, ensure_ascii=False)
|
| 1609 |
|
| 1610 |
+
# return state
|
| 1611 |
|
| 1612 |
+
# except Exception as e:
|
| 1613 |
+
# logger.error(f"Error in OverallPlannerNode: {e}")
|
| 1614 |
+
# raise
|
| 1615 |
+
# # Node 3: refiner node (scrape)
|
| 1616 |
+
# def refined_planner_node(state: GameState):
|
| 1617 |
+
# """
|
| 1618 |
+
# Refines the action plan based on validation feedback and game description.
|
| 1619 |
+
# """
|
| 1620 |
+
# logger.info("--- Running RefinedPlannerNode ---")
|
| 1621 |
+
# raw = state.get("pseudo_code", {})
|
| 1622 |
+
# refined_logic_data = raw.get("refined_logic", {})
|
| 1623 |
+
# sprite_name = refined_logic_data.get("name_variable", "<unknown>")
|
| 1624 |
+
# pseudo = refined_logic_data.get("pseudocode", "")
|
| 1625 |
+
# #detailed_game_description = state.get("detailed_game_description", state.get("description", "A game."))
|
| 1626 |
+
# current_action_plan = state.get("action_plan", {})
|
| 1627 |
+
# print(f"[current_action_plan before refinement] on ({state.get('iteration_count', 0)}): {json.dumps(current_action_plan, indent=2)}")
|
| 1628 |
+
# plan_validation_feedback = state.get("plan_validation_feedback", "No specific feedback provided. Assume general refinement is needed.")
|
| 1629 |
+
# project_json = state["project_json"]
|
| 1630 |
+
# target_names = [t["name"] for t in project_json["targets"]]
|
| 1631 |
+
|
| 1632 |
+
# # MODIFICATION 2: Get sprite positions, providing default for Stage as it doesn't have x,y
|
| 1633 |
+
# sprite_positions = {}
|
| 1634 |
+
# for target in project_json["targets"]:
|
| 1635 |
+
# if not target["isStage"]:
|
| 1636 |
+
# sprite_positions[target["name"]] = {"x": target.get("x", 0), "y": target.get("y", 0)}
|
| 1637 |
+
# else:
|
| 1638 |
+
# sprite_positions[target["name"]] = {"x": "N/A", "y": "N/A"} # Stage doesn't have positional coordinates
|
| 1639 |
+
|
| 1640 |
+
# #declaration_plan = state["declaration_plan"]
|
| 1641 |
+
|
| 1642 |
+
# refinement_prompt = f"""
|
| 1643 |
+
# Refine and correct the JSON object `action_overall_flow` so that it fully aligns with the detailed game description, sprite positions, variable/broadcast declarations, and Scratch 3.0 block reference—while also validating for common formatting or opcode errors.
|
| 1644 |
+
|
| 1645 |
+
# Here is the overall script available:
|
| 1646 |
+
# **Sprite name**: {sprite_name}
|
| 1647 |
+
# **and its corresponding Pseudo_code:**
|
| 1648 |
+
# '{pseudo}'
|
| 1649 |
+
|
| 1650 |
+
# [Note: Make sure you just refine the pseudo code by correting mistake and adding the missing opcode if any and *Do not* generate any new logic]
|
| 1651 |
+
# ---
|
| 1652 |
+
# **Targets in Game (Sprites and Stage):** {', '.join(target_names)}
|
| 1653 |
+
# **Current action plan:**
|
| 1654 |
+
# {current_action_plan}
|
| 1655 |
+
|
| 1656 |
+
# **Validation Feedback:**
|
| 1657 |
+
# '{plan_validation_feedback}'
|
| 1658 |
+
|
| 1659 |
+
# --- Scratch 3.0 Block Reference ---
|
| 1660 |
+
# ### Hat Blocks
|
| 1661 |
+
# {hat_opcodes_functionalities}
|
| 1662 |
+
|
| 1663 |
+
# ### Boolean Blocks
|
| 1664 |
+
# {boolean_opcodes_functionalities}
|
| 1665 |
+
|
| 1666 |
+
# ### C Blocks
|
| 1667 |
+
# {c_opcodes_functionalities}
|
| 1668 |
+
|
| 1669 |
+
# ### Cap Blocks
|
| 1670 |
+
# {cap_opcodes_functionalities}
|
| 1671 |
+
|
| 1672 |
+
# ### Reporter Blocks
|
| 1673 |
+
# {reporter_opcodes_functionalities}
|
| 1674 |
+
|
| 1675 |
+
# ### Stack Blocks
|
| 1676 |
+
# {stack_opcodes_functionalities}
|
| 1677 |
+
|
| 1678 |
+
# -----------------------------------
|
| 1679 |
+
|
| 1680 |
+
# * **Your task is to align to description, refine and correct the JSON object 'action_overall_flow'.**
|
| 1681 |
+
# Use sprite names exactly as provided in `sprite_names` (e.g., 'Sprite1', 'soccer ball'); and also the stage, do **NOT** rename them.
|
| 1682 |
+
# Other plans may use any hat blocks (including duplicates) to represent different logics or events.
|
| 1683 |
+
# Each plan must include a **single Scratch Hat Block** (e.g., 'event_whenflagclicked') to start scratch project and should contain:
|
| 1684 |
+
# 1. **'event'**: the exact `opcode` of the hat block that initiates the logic.
|
| 1685 |
+
# 2. **'logic'**: a natural language breakdown of each step taken after the event, formatted as a multi-line string representing pseudo-code. Ensure clarity and granularity—each described action should map closely to a Scratch block or tight sequence.
|
| 1686 |
+
# - Do **NOT** include any justification or comments—only the raw logic.
|
| 1687 |
+
# - Use 'forever: ...' or 'repeat(10): ...' to prefix repeating logic suitable taking reference from the C blocks.
|
| 1688 |
+
# - Use Scratch-consistent verbs: 'move', 'change', 'wait', 'hide', 'show', 'say', 'glide', etc.
|
| 1689 |
+
# - **Numeric values** `(e.g., 0, 5, 0.2, -130)` **must** be in parentheses: `(0)`, `(5)`, `(0.2)`, `(-130)`.
|
| 1690 |
+
# - **AlphaNumeric values** `(e.g., hello, say 5, 4, hi!)` **must** be in parentheses: `(hello)`, `(say 5)`, `(4)`, `(hi!)`.
|
| 1691 |
+
# - **Variables** must be in the form `[variable v]` (e.g., `[score v]`), even when used inside expressions two example use `set [score v] to (1)` or `show variable ([speed v])`.
|
| 1692 |
+
# - **Dropdown options** must be in the form `[option v]` (e.g., `[Game Start v]`, `[blue sky v]`). example use `when [space v] key pressed`.
|
| 1693 |
+
# - **Reporter blocks** used as inputs must be double‑wrapped: `((x position))`, `((y position))`. example use `if <((y position)) = (-130)> then` or `(((x position)) * (1))`.
|
| 1694 |
+
# - **Boolean blocks** in conditions must be inside `< >`, including nested ones: `<not <condition>>`, `<<cond1> and <cond2>>`,`<<cond1> or <cond2>>`.
|
| 1695 |
+
# - **Other Boolean blocks** in conditions must be inside `< >`, including nested ones or values or variables: `<(block/value/variable) * (block/value/variable)>`,`<(block/value/variable) < (block/value/variable)>`, and example of another variable`<[apple v] contains [a v]?>`.
|
| 1696 |
+
# - **Operator expressions** must use explicit Scratch operator blocks, e.g.:
|
| 1697 |
+
# ```
|
| 1698 |
+
# (([ballSpeed v]) * (1.1))
|
| 1699 |
+
# ```
|
| 1700 |
+
# - **Every hat block script must end** with a final `end` on its own line.
|
| 1701 |
+
# 3. **Validation & Formatting Checks**
|
| 1702 |
+
# - **Opcode Coverage**: Ensure every action in `logic` has a matching opcode in the lists.
|
| 1703 |
+
# - **Bracket Nesting**: Confirm every `(` has a matching `)`, e.g., `(pick random (100) to (-100))`.
|
| 1704 |
+
# - **Operator Formatting**: Validate that operators use Scratch operator blocks, not inline math.
|
| 1705 |
+
# - **Common Errors**:
|
| 1706 |
+
# - `pick random (100,-100)` → `(pick random (100) to (-100))`
|
| 1707 |
+
# - Missing `end` at script conclusion
|
| 1708 |
+
# - Unwrapped reporter inputs or Boolean tests
|
| 1709 |
+
# 4. **Opcode Lists**: include relevant Scratch opcodes grouped under `motion`, `control`, `operator`, `sensing`, `looks`, `sounds`, `events`, and `data`. List only the non-empty categories. Use exact opcodes from the given Scratch 3.0 Block Reference.
|
| 1710 |
+
# 5. Few Example of content of logics inside for a specific plan as scratch pseudo-code:
|
| 1711 |
+
# - example 1[continues moving objects]:
|
| 1712 |
+
# ```
|
| 1713 |
+
# when green flag clicked
|
| 1714 |
+
# go to x: (240) y: (-100)
|
| 1715 |
+
# set [speed v] to (-5)
|
| 1716 |
+
# show variable [speed v]
|
| 1717 |
+
# forever
|
| 1718 |
+
# change x by ([speed v])
|
| 1719 |
+
# if <((x position)) < (-240)> then
|
| 1720 |
+
# go to x: (240) y: (-100)
|
| 1721 |
+
# end
|
| 1722 |
+
# end
|
| 1723 |
+
# end
|
| 1724 |
+
# ```
|
| 1725 |
+
# - example 2[jumping script of an plan]:
|
| 1726 |
+
# ```
|
| 1727 |
+
# when [space v] key pressed
|
| 1728 |
+
# if <((y position)) = (-100)> then
|
| 1729 |
+
# repeat (5)
|
| 1730 |
+
# change y by (100)
|
| 1731 |
+
# wait (0.1) seconds
|
| 1732 |
+
# change y by (-100)
|
| 1733 |
+
# wait (0.1) seconds
|
| 1734 |
+
# end
|
| 1735 |
+
# end
|
| 1736 |
+
# end
|
| 1737 |
+
# ```
|
| 1738 |
+
# - example 3 [pattern for level up and increase difficulty]:
|
| 1739 |
+
# ```
|
| 1740 |
+
# when I receive [Level Up v]
|
| 1741 |
+
# change [level v] by (1)
|
| 1742 |
+
# set [ballSpeed v] to ((([ballSpeed v]) * (1.1)))
|
| 1743 |
+
# end
|
| 1744 |
+
# ```
|
| 1745 |
+
# 6. Use target names exactly as listed in `Targets in Game`. Do NOT rename or invent new targets.
|
| 1746 |
+
# 7. Ensure the plan reflects accurate opcode usage derived strictly from the block reference above.
|
| 1747 |
+
# 8. Few shot Example structure for 'action_overall_flow':
|
| 1748 |
+
# ```json
|
| 1749 |
+
# {{
|
| 1750 |
+
# "action_overall_flow": {{
|
| 1751 |
+
# "Stage": {{
|
| 1752 |
+
# "description": "Background and global game state management, including broadcasts, rewards, and score.",
|
| 1753 |
+
# "plans": [
|
| 1754 |
+
# {{
|
| 1755 |
+
# "event": "event_whenflagclicked",
|
| 1756 |
+
# "logic": "when green flag clicked\n switch backdrop to [backdrop1 v]\n set [score v] to 0\n show variable [score v]\n broadcast [Game Start v]\nend",
|
| 1757 |
+
# "motion": [],
|
| 1758 |
+
# "control": [],
|
| 1759 |
+
# "operator": [],
|
| 1760 |
+
# "sensing": [],
|
| 1761 |
+
# "looks": [
|
| 1762 |
+
# "looks_switchbackdropto"
|
| 1763 |
+
# ],
|
| 1764 |
+
# "sounds": [],
|
| 1765 |
+
# "events": [
|
| 1766 |
+
# "event_broadcast"
|
| 1767 |
+
# ],
|
| 1768 |
+
# "data": [
|
| 1769 |
+
# "data_setvariableto",
|
| 1770 |
+
# "data_showvariable"
|
| 1771 |
+
# ]
|
| 1772 |
+
# }},
|
| 1773 |
+
# {{
|
| 1774 |
+
# "event": "event_whenbroadcastreceived",
|
| 1775 |
+
# "logic": "when I receive [Game Over v]\n if <(score) > (High Score)> then\n set [High Score v] to (score)\n end\n switch backdrop to [HighScore v]\nend",
|
| 1776 |
+
# "motion": [],
|
| 1777 |
+
# "control": [
|
| 1778 |
+
# "control_if"
|
| 1779 |
+
# ],
|
| 1780 |
+
# "operator": [
|
| 1781 |
+
# "operator_gt"
|
| 1782 |
+
# ],
|
| 1783 |
+
# "sensing": [],
|
| 1784 |
+
# "looks": [
|
| 1785 |
+
# "looks_switchbackdropto"
|
| 1786 |
+
# ],
|
| 1787 |
+
# "sounds": [],
|
| 1788 |
+
# "events": [],
|
| 1789 |
+
# "data": [
|
| 1790 |
+
# "data_setvariableto"
|
| 1791 |
+
# ]
|
| 1792 |
+
# }}
|
| 1793 |
+
# ]
|
| 1794 |
+
# }},
|
| 1795 |
+
# "Sprite1": {{
|
| 1796 |
+
# "description": "Main character (cat) actions",
|
| 1797 |
+
# "plans": [
|
| 1798 |
+
# {{
|
| 1799 |
+
# "event": "event_whenflagclicked",
|
| 1800 |
+
# "logic": "when green flag clicked\n go to x: 240 y: -100\nend\n",
|
| 1801 |
+
# "motion": [
|
| 1802 |
+
# "motion_gotoxy"
|
| 1803 |
+
# ],
|
| 1804 |
+
# "control": [],
|
| 1805 |
+
# "operator": [],
|
| 1806 |
+
# "sensing": [],
|
| 1807 |
+
# "looks": [],
|
| 1808 |
+
# "sounds": [],
|
| 1809 |
+
# "events": [],
|
| 1810 |
+
# "data": []
|
| 1811 |
+
# }},
|
| 1812 |
+
# {{
|
| 1813 |
+
# "event": "event_whenkeypressed",
|
| 1814 |
+
# "logic": "when [space v] key pressed\n repeat (10)\n change y by (20)\n wait (0.1) seconds\n change y by (-20)\n end\nend",
|
| 1815 |
+
# "motion": [
|
| 1816 |
+
# "motion_changeyby"
|
| 1817 |
+
# ],
|
| 1818 |
+
# "control": [
|
| 1819 |
+
# "control_repeat",
|
| 1820 |
+
# "control_wait"
|
| 1821 |
+
# ],
|
| 1822 |
+
# "operator": [],
|
| 1823 |
+
# "sensing": [],
|
| 1824 |
+
# "looks": [],
|
| 1825 |
+
# "sounds": [],
|
| 1826 |
+
# "events": [],
|
| 1827 |
+
# "data": []
|
| 1828 |
+
# }}
|
| 1829 |
+
# ]
|
| 1830 |
+
# }},
|
| 1831 |
+
# "soccer ball": {{
|
| 1832 |
+
# "description": "Obstacle movement and interaction",
|
| 1833 |
+
# "plans": [
|
| 1834 |
+
# {{
|
| 1835 |
+
# "event": "event_whenflagclicked",
|
| 1836 |
+
# "logic": "when green flag clicked\n go to x: 240 y: -135\n forever\n glide 2 seconds to x: -240 y: -135\n if <(x position) < -235> then\n set x to 240\n end\n if <touching [Sprite1 v]?> then\n broadcast [Game Over v]\n stop [all v]\n end\n end\nend",
|
| 1837 |
+
# "motion": [
|
| 1838 |
+
# "motion_gotoxy",
|
| 1839 |
+
# "motion_glidesecstoxy",
|
| 1840 |
+
# "motion_xposition",
|
| 1841 |
+
# "motion_setx"
|
| 1842 |
+
# ],
|
| 1843 |
+
# "control": [
|
| 1844 |
+
# "control_forever",
|
| 1845 |
+
# "control_if",
|
| 1846 |
+
# "control_stop"
|
| 1847 |
+
# ],
|
| 1848 |
+
# "operator": [
|
| 1849 |
+
# "operator_lt"
|
| 1850 |
+
# ],
|
| 1851 |
+
# "sensing": [
|
| 1852 |
+
# "sensing_touchingobject",
|
| 1853 |
+
# "sensing_touchingobjectmenu"
|
| 1854 |
+
# ],
|
| 1855 |
+
# "looks": [],
|
| 1856 |
+
# "sounds": [],
|
| 1857 |
+
# "events": [
|
| 1858 |
+
# "event_broadcast"
|
| 1859 |
+
# ],
|
| 1860 |
+
# "data": []
|
| 1861 |
+
# }}
|
| 1862 |
+
# ]
|
| 1863 |
+
# }}
|
| 1864 |
+
# }}
|
| 1865 |
+
# }}
|
| 1866 |
+
# ```
|
| 1867 |
+
# 9. Use the validation feedback to address errors, fill in missing logic, or enhance clarity.
|
| 1868 |
+
# example of few possible improvements: 1.event_whenflagclicked is used to control sprite but its used for actual start scratch project and reset scratch. 2. looping like forever used where we should use iterative. 3. missing of for variable we used in the block
|
| 1869 |
+
# - Maintain the **exact JSON structure** shown above.
|
| 1870 |
+
# - All `logic` fields must be **clear and granular**.
|
| 1871 |
+
# - Only include opcode categories that contain relevant opcodes.
|
| 1872 |
+
# - Ensure that each opcode matches its intended Scratch functionality.
|
| 1873 |
+
# - If feedback suggests major change, **rethink the entire plan** for the affected sprite(s).
|
| 1874 |
+
# - If feedback is minor, make precise, minimal improvements only.
|
| 1875 |
+
# """
|
| 1876 |
+
# try:
|
| 1877 |
+
# response = agent.invoke({"messages": [{"role": "user", "content": refinement_prompt}]})
|
| 1878 |
+
# raw_response = response["messages"][-1].content#strip_noise(response["messages"][-1].content)
|
| 1879 |
+
# logger.info(f"Raw response from LLM [RefinedPlannerNode]: {raw_response[:500]}...")
|
| 1880 |
+
# # json debugging and solving
|
| 1881 |
+
# try:
|
| 1882 |
+
# refined_plan = extract_json_from_llm_response(raw_response)
|
| 1883 |
+
# except json.JSONDecodeError as error_json:
|
| 1884 |
+
# logger.error("Failed to extract JSON from LLM response. Attempting to correct the response.")
|
| 1885 |
+
# # Use the JSON resolver agent to fix the response
|
| 1886 |
+
# correction_prompt = (
|
| 1887 |
+
# "Your task is to correct the provided JSON string to ensure it is **syntactically perfect and adheres strictly to JSON rules**.\n"
|
| 1888 |
+
# "Carefully review the JSON for any errors, especially focusing on the reported error at:\n"
|
| 1889 |
+
# f"- **Error Details**: {error_json}\n\n"
|
| 1890 |
+
# "**Strict Instructions for your response:**\n"
|
| 1891 |
+
# "1. **ONLY** output the corrected JSON. Do not include any other text, comments, or explanations outside the JSON.\n"
|
| 1892 |
+
# "2. Ensure all property names (keys) are enclosed in **double quotes**.\n"
|
| 1893 |
+
# "3. Ensure string values are correctly enclosed in **double quotes** and any internal special characters (like newlines `\\n`, tabs `\\t`, backslashes `\\\\`, or double quotes `\\`) are properly **escaped**.\n"
|
| 1894 |
+
# "4. IN `logic` field make sure content enclosed in **double quotes** should not have invalid **double quotes**, **eliminate** all quotes inside the content if any. "
|
| 1895 |
+
# "4. Verify that there are **no extra commas**, especially between key-value pairs or after the last element in an object or array.\n"
|
| 1896 |
+
# "5. Ensure proper nesting and matching of curly braces `{}` and square brackets `[]`.\n"
|
| 1897 |
+
# "6. **Crucially, remove any extraneous characters or duplicate closing braces outside the main JSON object.**\n" # Added instruction
|
| 1898 |
+
# "7. The corrected JSON must be a **complete and valid** JSON object.\n\n"
|
| 1899 |
+
# "Here is the problematic JSON string to correct:\n"
|
| 1900 |
+
# "```json\n"
|
| 1901 |
+
# f"{raw_response}\n"
|
| 1902 |
+
# "```\n"
|
| 1903 |
+
# "Corrected JSON:\n"
|
| 1904 |
+
# )
|
| 1905 |
+
# correction_response = agent_json_resolver.invoke({"messages": [{"role": "user", "content": correction_prompt}]})
|
| 1906 |
+
# print(f"[JSON CORRECTOR RESPONSE AT REFINEPLANNER ]: {correction_response['messages'][-1].content}")
|
| 1907 |
+
# refined_plan = extract_json_from_llm_response(correction_response["messages"][-1].content)#strip_noise(correction_response["messages"][-1].content))
|
| 1908 |
+
# logger.info("Refined plan corrected by JSON resolver agent.")
|
| 1909 |
|
| 1910 |
+
# if refined_plan:
|
| 1911 |
+
# #state["action_plan"] = refined_plan.get("action_overall_flow", {}) # Update to the key 'action_overall_flow' [error]
|
| 1912 |
+
# state["action_plan"] = refined_plan #.get("action_overall_flow", {}) # Update the main the prompt includes updated only
|
| 1913 |
+
# logger.info("Action plan refined by RefinedPlannerNode.")
|
| 1914 |
+
# else:
|
| 1915 |
+
# logger.warning("RefinedPlannerNode did not return a valid 'action_overall_flow' structure. Keeping previous plan.")
|
| 1916 |
+
# print("[Refined Action Plan]:", json.dumps(state["action_plan"], indent=2))
|
| 1917 |
+
# #print("[current state after refinement]:", json.dumps(state, indent=2))
|
| 1918 |
|
| 1919 |
+
# # with open("debug_state.json", "w", encoding="utf-8") as f:
|
| 1920 |
+
# # json.dump(state, f, indent=2, ensure_ascii=False)
|
| 1921 |
|
| 1922 |
+
# return state
|
| 1923 |
+
# except Exception as e:
|
| 1924 |
+
# logger.error(f"Error in RefinedPlannerNode: {e}")
|
| 1925 |
+
# raise
|
| 1926 |
+
# # Node 4: opcode counter node (scrape)
|
| 1927 |
+
# def plan_opcode_counter_node(state: Dict[str, Any]) -> Dict[str, Any]:
|
| 1928 |
+
# """
|
| 1929 |
+
# For each plan in state["action_plan"], calls the LLM agent
|
| 1930 |
+
# to analyze the `logic` string and return a list of {opcode, count} for each category.
|
| 1931 |
+
# """
|
| 1932 |
+
# logger.info("=== Running OPCODE COUTER LOGIC with LLM counts ===")
|
| 1933 |
+
# #game_description = state.get("description", "No game description provided.")
|
| 1934 |
+
# sprite_name = {}
|
| 1935 |
+
# project_json_targets = state.get("project_json", {}).get("targets", [])
|
| 1936 |
+
# for target in project_json_targets:
|
| 1937 |
+
# sprite_name[target["name"]] = target["name"]
|
| 1938 |
|
| 1939 |
+
# action_flow = state.get("action_plan", {})
|
| 1940 |
|
| 1941 |
+
# if action_flow.get("action_overall_flow", {}) == {}:
|
| 1942 |
+
# plan_data = action_flow.items()
|
| 1943 |
+
# else:
|
| 1944 |
+
# plan_data = action_flow.get("action_overall_flow", {}).items()
|
| 1945 |
|
| 1946 |
+
# refined_flow: Dict[str, Any] = {}
|
| 1947 |
+
# for sprite, sprite_data in plan_data:
|
| 1948 |
+
# refined_plans = []
|
| 1949 |
+
# for plan in sprite_data.get("plans", []):
|
| 1950 |
+
# logic = plan.get("logic", "")
|
| 1951 |
+
# event = plan.get("event", "")
|
| 1952 |
|
| 1953 |
+
# # These are for guiding the LLM, not for the final output format directly
|
| 1954 |
+
# opcodes_from_plan = {
|
| 1955 |
+
# "motion": plan.get("motion", []),
|
| 1956 |
+
# "control": plan.get("control", []),
|
| 1957 |
+
# "operator": plan.get("operator", []),
|
| 1958 |
+
# "sensing": plan.get("sensing", []),
|
| 1959 |
+
# "looks": plan.get("looks", []),
|
| 1960 |
+
# "sounds": plan.get("sounds", []),
|
| 1961 |
+
# "events": plan.get("events", []) + ([event] if isinstance(event, str) else []),
|
| 1962 |
+
# "data": plan.get("data", []),
|
| 1963 |
+
# }
|
| 1964 |
+
|
| 1965 |
+
# refinement_prompt = f"""
|
| 1966 |
+
# You are a Scratch 3.0 expert with deep knowledge of block types, nesting and stack relationships.
|
| 1967 |
+
# Your job: read the plan logic below and decide exactly which blocks (and how many of each) are required to implement it.
|
| 1968 |
+
# Review the following plan for '{sprite}' triggered by '{event}'.
|
| 1969 |
+
# --- Scratch 3.0 Block Reference ---
|
| 1970 |
+
# ### Hat Blocks
|
| 1971 |
+
# Description: {hat_description}
|
| 1972 |
+
# Blocks:
|
| 1973 |
+
# {hat_opcodes_functionalities}
|
| 1974 |
+
|
| 1975 |
+
# ### Boolean Blocks
|
| 1976 |
+
# Description: {boolean_description}
|
| 1977 |
+
# Blocks:
|
| 1978 |
+
# {boolean_opcodes_functionalities}
|
| 1979 |
+
|
| 1980 |
+
# ### C Blocks
|
| 1981 |
+
# Description: {c_description}
|
| 1982 |
+
# Blocks:
|
| 1983 |
+
# {c_opcodes_functionalities}
|
| 1984 |
+
|
| 1985 |
+
# ### Cap Blocks
|
| 1986 |
+
# Description: {cap_description}
|
| 1987 |
+
# Blocks:
|
| 1988 |
+
# {cap_opcodes_functionalities}
|
| 1989 |
+
|
| 1990 |
+
# ### Reporter Blocks
|
| 1991 |
+
# Description: {reporter_description}
|
| 1992 |
+
# Blocks:
|
| 1993 |
+
# {reporter_opcodes_functionalities}
|
| 1994 |
+
|
| 1995 |
+
# ### Stack Blocks
|
| 1996 |
+
# Description: {stack_description}
|
| 1997 |
+
# Blocks:
|
| 1998 |
+
# {stack_opcodes_functionalities}
|
| 1999 |
+
# -----------------------------------
|
| 2000 |
+
# Current Plan Details:
|
| 2001 |
+
# - Event (Hat Block Opcode): {event}
|
| 2002 |
+
# - Associated Opcodes by Category: {json.dumps(opcodes_from_plan, indent=2)}
|
| 2003 |
+
|
| 2004 |
+
# ── Game Context ──
|
| 2005 |
+
# Sprite: "{sprite}"
|
| 2006 |
+
|
| 2007 |
+
# ── Current Plan ──
|
| 2008 |
+
# Event (hat block): {event}
|
| 2009 |
+
# Logic (pseudo-Scratch): {logic}
|
| 2010 |
+
# Plan : {plan}
|
| 2011 |
+
|
| 2012 |
+
# ── Opcode Candidates ──
|
| 2013 |
+
# Motion: {opcodes_from_plan["motion"]}
|
| 2014 |
+
# Control: {opcodes_from_plan["control"]}
|
| 2015 |
+
# Operator: {opcodes_from_plan["operator"]}
|
| 2016 |
+
# Sensing: {opcodes_from_plan["sensing"]}
|
| 2017 |
+
# Looks: {opcodes_from_plan["looks"]}
|
| 2018 |
+
# Sounds: {opcodes_from_plan["sounds"]}
|
| 2019 |
+
# Events: {opcodes_from_plan["events"]}
|
| 2020 |
+
# Data: {opcodes_from_plan["data"]}
|
| 2021 |
+
|
| 2022 |
+
# ── Your Task ──
|
| 2023 |
+
# 1. Analyze the “Logic” steps and choose exactly which opcodes are needed.
|
| 2024 |
+
# 2. Use exact opcodes from the given Scratch 3.0 Block Reference and verfiy the proper opcode are used available in the Scratch 3.0 Block Reference.
|
| 2025 |
+
# 3. Return a top-level JSON object with a single key: "opcode_counts".
|
| 2026 |
+
# 4. The value of "opcode_counts" should be a list of objects, where each object has "opcode": "<opcode_name>" and "count": <integer>.
|
| 2027 |
+
# 5. Ensure the list includes the hat block for this plan (e.g., event_whenflagclicked, event_whenkeypressed, event_whenbroadcastreceived) with a count of 1.
|
| 2028 |
+
# 6. The order of opcodes within the "opcode_counts" list does not matter.
|
| 2029 |
+
# 7. If any plan logic is None do not generate the opcode_counts for it.
|
| 2030 |
+
# 8. Use only double quotes and ensure valid JSON.
|
| 2031 |
+
|
| 2032 |
+
# Example output:
|
| 2033 |
+
# **example 1**
|
| 2034 |
+
# ```json
|
| 2035 |
+
# {{
|
| 2036 |
+
# "opcode_counts":[
|
| 2037 |
+
# {{"opcode":"motion_gotoxy","count":1}},
|
| 2038 |
+
# {{"opcode":"control_forever","count":1}},
|
| 2039 |
+
# {{"opcode":"control_if","count":1}},
|
| 2040 |
+
# {{"opcode":"looks_switchbackdropto","count":1}},
|
| 2041 |
+
# {{"opcode":"event_whenflagclicked","count":1}},
|
| 2042 |
+
# {{"opcode":"event_broadcast","count":1}},
|
| 2043 |
+
# {{"opcode":"data_setvariableto","count":2}},
|
| 2044 |
+
# {{"opcode":"data_showvariable","count":2}}
|
| 2045 |
+
# ]
|
| 2046 |
+
# }}
|
| 2047 |
+
# ```
|
| 2048 |
+
# **example 2**
|
| 2049 |
+
# ```json
|
| 2050 |
+
# {{
|
| 2051 |
+
# "opcode_counts":[
|
| 2052 |
+
# {{"opcode":"motion_gotoxy","count":1}},
|
| 2053 |
+
# {{"opcode":"motion_glidesecstoxy","count":1}},
|
| 2054 |
+
# {{"opcode":"motion_xposition","count":1}},
|
| 2055 |
+
# {{"opcode":"motion_setx","count":1}},
|
| 2056 |
+
# {{"opcode":"control_forever","count":1}},
|
| 2057 |
+
# {{"opcode":"control_if","count":2}},
|
| 2058 |
+
# {{"opcode":"operator_lt","count":1}},
|
| 2059 |
+
# {{"opcode":"sensing_touchingobject","count":1}},
|
| 2060 |
+
# {{"opcode":"event_whenflagclicked","count":1}},
|
| 2061 |
+
# {{"opcode":"event_broadcast","count":1}}
|
| 2062 |
+
# ]
|
| 2063 |
+
# }}
|
| 2064 |
+
# ```
|
| 2065 |
+
# """
|
| 2066 |
+
# try:
|
| 2067 |
+
# response = agent.invoke({"messages": [{"role": "user", "content": refinement_prompt}]})
|
| 2068 |
+
# llm_output = response["messages"][-1].content
|
| 2069 |
+
# llm_json = extract_json_from_llm_response(llm_output)
|
| 2070 |
+
# logger.info(f"Successfully analyze the opcode requirement for {sprite} - {event}.")
|
| 2071 |
+
|
| 2072 |
+
# except json.JSONDecodeError as error_json:
|
| 2073 |
+
# logger.error(f"JSON Decode Error for {sprite} - {event}: {error_json}. Attempting correction.")
|
| 2074 |
+
# correction_prompt = (
|
| 2075 |
+
# "Your task is to correct the provided JSON string to ensure it is **syntactically perfect and adheres strictly to JSON rules**.\n"
|
| 2076 |
+
# "It must be a JSON object with a single key `opcode_counts` containing a list of objects like {{'opcode': '<opcode_name>', 'count': <integer>}}.\n"
|
| 2077 |
+
# f"- **Error Details**: {error_json}\n\n"
|
| 2078 |
+
# "**Strict Instructions for your response:**\n"
|
| 2079 |
+
# "1. **ONLY** output the corrected JSON. Do not include any other text or explanations.\n"
|
| 2080 |
+
# "2. Ensure all keys and string values are enclosed in **double quotes**. Escape internal quotes (`\\`).\n"
|
| 2081 |
+
# "3. No trailing commas. Correct nesting.\n\n"
|
| 2082 |
+
# "Here is the problematic JSON string to correct:\n"
|
| 2083 |
+
# f"```json\n{llm_output}\n```\n"
|
| 2084 |
+
# "Corrected JSON:\n"
|
| 2085 |
+
# )
|
| 2086 |
+
# try:
|
| 2087 |
+
# correction_response = agent_json_resolver.invoke({"messages": [{"role": "user", "content": correction_prompt}]})
|
| 2088 |
+
# llm_json = extract_json_from_llm_response(correction_response["messages"][-1].content)
|
| 2089 |
+
# logger.info(f"Successfully corrected JSON output for {sprite} - {event}.")
|
| 2090 |
+
# except Exception as e_corr:
|
| 2091 |
+
# logger.error(f"Failed to correct JSON output for {sprite} - {event} even after retry: {e_corr}")
|
| 2092 |
+
# continue
|
| 2093 |
+
|
| 2094 |
+
# # Directly use the 'opcode_counts' list from the LLM's output
|
| 2095 |
+
# plan["opcode_counts"] = llm_json.get("opcode_counts", [])
|
| 2096 |
+
# plan["opcode_counts"] = validate_and_fix_opcodes(plan["opcode_counts"])
|
| 2097 |
+
# # Optionally, you can remove the individual category lists from the plan
|
| 2098 |
+
# # if they are no longer needed after the LLM provides the consolidated list.
|
| 2099 |
+
# # for key in ["motion", "control", "operator", "sensing", "looks", "sounds", "events", "data"]:
|
| 2100 |
+
# # if key in plan:
|
| 2101 |
+
# # del plan[key]
|
| 2102 |
+
|
| 2103 |
+
# refined_plans.append(plan)
|
| 2104 |
+
|
| 2105 |
+
# refined_flow[sprite] = {
|
| 2106 |
+
# "description": sprite_data.get("description", ""),
|
| 2107 |
+
# "plans": refined_plans
|
| 2108 |
+
# }
|
| 2109 |
|
| 2110 |
+
# if refined_flow:
|
| 2111 |
+
# state["action_plan"] = refined_flow
|
| 2112 |
+
# logger.info("logic aligned by logic_aligner_Node.")
|
| 2113 |
|
| 2114 |
+
# state["temporary_node"] = refined_flow
|
| 2115 |
+
# #state["temporary_node"] = refined_flow
|
| 2116 |
+
# print(f"[OPCODE COUTER LOGIC]: {refined_flow}")
|
| 2117 |
+
# logger.info("=== OPCODE COUTER LOGIC completed ===")
|
| 2118 |
+
# return state
|
| 2119 |
|
| 2120 |
# Node 5: block_builder_node
|
| 2121 |
def overall_block_builder_node_2(state: GameState):
|
|
|
|
| 2228 |
raise
|
| 2229 |
|
| 2230 |
# Node 7: variable adder node
|
| 2231 |
+
def layer_order_correction(state: GameState):
|
| 2232 |
+
"""
|
| 2233 |
+
Ensures that all sprites (isStage: false) have unique layerOrder values >= 1.
|
| 2234 |
+
If duplicates are found, they are reassigned sequentially.
|
| 2235 |
+
"""
|
| 2236 |
+
logger.info("--- Running Layer Order Correction Node ---")
|
| 2237 |
+
try:
|
| 2238 |
+
project_json = state.get("project_json", {})
|
| 2239 |
+
targets = project_json.get("targets", [])
|
| 2240 |
+
|
| 2241 |
+
# Collect all sprites (ignore Stage)
|
| 2242 |
+
sprites = [t for t in targets if not t.get("isStage", False)]
|
| 2243 |
+
|
| 2244 |
+
# Reassign layerOrder sequentially (starting from 1)
|
| 2245 |
+
for idx, sprite in enumerate(sprites, start=1):
|
| 2246 |
+
old_lo = sprite.get("layerOrder", None)
|
| 2247 |
+
sprite["layerOrder"] = idx
|
| 2248 |
+
logger.debug(f"Sprite '{sprite.get('name')}' layerOrder: {old_lo} -> {idx}")
|
| 2249 |
+
|
| 2250 |
+
# Stage always remains 0
|
| 2251 |
+
for target in targets:
|
| 2252 |
+
if target.get("isStage", False):
|
| 2253 |
+
target["layerOrder"] = 0
|
| 2254 |
+
|
| 2255 |
+
# Update state
|
| 2256 |
+
state["project_json"]["targets"] = targets
|
| 2257 |
+
logger.info("Layer Order Correction completed successfully.")
|
| 2258 |
+
|
| 2259 |
+
return state
|
| 2260 |
+
|
| 2261 |
+
except Exception as e:
|
| 2262 |
+
logger.error(f"Error in Layer Order Correction Node: {e}")
|
| 2263 |
+
return state
|
| 2264 |
+
|
| 2265 |
+
# Node 8: variable adder node
|
| 2266 |
def processed_page_node(state: GameState):
|
| 2267 |
logger.info("--- Processing the Pages Node ---")
|
| 2268 |
image = state.get("project_image", "")
|
|
|
|
| 3661 |
workflow.add_node("pseudo_generator", pseudo_generator_node)
|
| 3662 |
#workflow.add_node("plan_generator", overall_planner_node)
|
| 3663 |
workflow.add_node("Node_optimizer", node_optimizer)
|
| 3664 |
+
workflow.add_node("layer_optimizer", layer_order_correction)
|
| 3665 |
#workflow.add_node("logic_alignment", plan_logic_aligner_node)
|
| 3666 |
#workflow.add_node("plan_verifier", plan_verification_node)
|
| 3667 |
#workflow.add_node("refined_planner", refined_planner_node) # Refines the action plan
|
|
|
|
| 3676 |
if state.get("processing", False):
|
| 3677 |
return "pseudo_generator"
|
| 3678 |
else:
|
| 3679 |
+
return "layer_optimizer"#END
|
| 3680 |
|
| 3681 |
workflow.add_conditional_edges(
|
| 3682 |
"page_processed",
|
| 3683 |
decide_next_step,
|
| 3684 |
{
|
| 3685 |
"pseudo_generator": "pseudo_generator",
|
| 3686 |
+
"layer_optimizer": "layer_optimizer"
|
| 3687 |
+
#str(END): END # str(END) is '__end__'
|
| 3688 |
}
|
| 3689 |
)
|
| 3690 |
# Main chain
|
|
|
|
| 3697 |
# workflow.add_edge("time_delay_3", "opcode_counter")
|
| 3698 |
workflow.add_edge("Node_optimizer", "block_builder")
|
| 3699 |
workflow.add_edge("block_builder", "variable_initializer")
|
|
|
|
| 3700 |
# After last node, check again
|
| 3701 |
workflow.add_edge("variable_initializer", "page_processed")
|
| 3702 |
+
workflow.add_edge("layer_optimizer", END)
|
| 3703 |
|
| 3704 |
app_graph = workflow.compile()
|
| 3705 |
|