File size: 117,645 Bytes
49aa045 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 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 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 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 1440 1441 1442 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 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 1616 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 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 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 1746 1747 1748 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 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 1927 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 1967 1968 1969 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 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 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 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 | import hashlib
import hmac
import base64
import binascii
import zlib
import json
import uuid
import math
import re
from typing import Dict, Any, List
class ToolSimulator:
def __init__(self):
"""工具模拟器
文件处理工具(zip_unzip, zip_unzip_file, zip_unzip_item, json_get, csv_query,
regex_search, graph_shortest_path)不需要真实文件系统。
这些工具只验证输入参数,然后返回预先初始化好的 golden_outputs。
"""
self.tool_map = {
# 数论
"calc_bigmul": self.calc_bigmul,
"calc_modexp": self.calc_modexp,
"calc_modexp_int": self.calc_modexp_int,
"calc_gcd": self.calc_gcd,
"calc_gcd_int": self.calc_gcd_int,
"calc_inv_mod": self.calc_inv_mod,
"calc_inv_mod_int": self.calc_inv_mod_int,
# 哈希
"hash_sha256": self.hash_sha256,
"hash_md5": self.hash_md5,
"hmac_sha256": self.hmac_sha256,
# 编码
"base64_decode": self.base64_decode,
"base64_encode": self.base64_encode,
"hex_decode": self.hex_decode,
"hex_encode": self.hex_encode,
"zlib_inflate": self.zlib_inflate,
"zlib_deflate": self.zlib_deflate,
# 文件
"zip_unzip": self.zip_unzip,
"zip_unzip_file": self.zip_unzip_file,
"zip_unzip_item": self.zip_unzip_item,
"regex_search": self.regex_search,
"json_get": self.json_get,
"csv_query": self.csv_query,
"code_py_exec": self.code_py_exec,
# 图
"graph_shortest_path": self.graph_shortest_path,
# 加解密 (简化模拟)
"crypto_aes_cbc_decrypt": self.crypto_aes_cbc_decrypt,
"crypto_rsa_decrypt_hex": self.crypto_rsa_decrypt_hex,
# 其他
"num_base_convert": self.num_base_convert,
"time_convert": self.time_convert,
"checksum_crc32": self.checksum_crc32,
"geo_haversine": self.geo_haversine,
"otp_totp_generate": self.otp_totp_generate,
"unicode_norm": self.unicode_norm,
"text_bidi_sanitize": self.text_bidi_sanitize,
"text_xor_hex": self.text_xor_hex,
"text_rotN": self.text_rotN,
"luhn_check": self.luhn_check,
"iban_validate": self.iban_validate,
# 物理
"matter_synthesizer": self.matter_synthesizer,
# 终端机
"terminal_1": self.terminal_1,
"terminal_2": self.terminal_2,
"Logic Terminal": self.logic_terminal,
"Math Terminal": self.math_terminal,
"Encryption Terminal": self.crypt_terminal,
"Sequence Terminal": self.sequence_terminal
}
def execute(self, tool_name: str, inputs: Dict[str, Any]) -> Dict[str, Any]:
if tool_name not in self.tool_map:
# Fallback for unknown tools: return dummy outputs based on expected output types
# This should ideally not happen if all tools are implemented
return {}
try:
return self.tool_map[tool_name](inputs)
except Exception as e:
print(f"Error executing {tool_name}: {e}")
# Return empty or error indication? For generation, we might want to fail hard or return dummy.
# For now, return dummy to keep flow going, but log error.
return {}
# --- Implementations ---
def calc_bigmul(self, inputs):
# Check if required parameters are present
if "a" not in inputs or "b" not in inputs:
raise ValueError("Missing required parameters 'a' and/or 'b'")
try:
a = inputs["a"]
b = inputs["b"]
# 转换为整数:如果本身就是数字就不用操作,如果是字符串就转成数字类型
if isinstance(a, str):
a = int(a)
elif not isinstance(a, int):
raise ValueError("Parameter 'a' must be a string or integer")
if isinstance(b, str):
b = int(b)
elif not isinstance(b, int):
raise ValueError("Parameter 'b' must be a string or integer")
return {"product": str(a * b)}
except (ValueError, TypeError) as e:
raise ValueError(f"Failed to convert parameters to integers: {str(e)}")
def calc_modexp(self, inputs):
# 参数检查
if "base" not in inputs:
raise ValueError("Missing required parameter: base")
if "exp" not in inputs:
raise ValueError("Missing required parameter: exp")
if "mod" not in inputs:
raise ValueError("Missing required parameter: mod")
# 转换参数:如果本身就是数字就不用操作,如果是字符串就转成数字类型
try:
base = inputs["base"]
exp = inputs["exp"]
mod = inputs["mod"]
if isinstance(base, str):
base = int(base)
elif not isinstance(base, int):
raise ValueError("Parameter 'base' must be a string or integer")
if isinstance(exp, str):
exp = int(exp)
elif not isinstance(exp, int):
raise ValueError("Parameter 'exp' must be a string or integer")
if isinstance(mod, str):
mod = int(mod)
elif not isinstance(mod, int):
raise ValueError("Parameter 'mod' must be a string or integer")
except ValueError as e:
raise ValueError(f"Parameter conversion error: {str(e)}")
if mod == 0:
raise ValueError("Modulus cannot be zero")
return {"value": str(pow(base, exp, mod))}
def calc_modexp_int(self, inputs):
# 参数检查
if "base" not in inputs:
raise ValueError("Missing required parameter: base")
if "exp" not in inputs:
raise ValueError("Missing required parameter: exp")
if "mod" not in inputs:
raise ValueError("Missing required parameter: mod")
# 类型检查
if not isinstance(inputs["base"], int):
raise ValueError("Parameter 'base' must be an integer")
if not isinstance(inputs["exp"], int):
raise ValueError("Parameter 'exp' must be an integer")
if not isinstance(inputs["mod"], int):
raise ValueError("Parameter 'mod' must be an integer")
# 获取参数
base = inputs["base"]
exp = inputs["exp"]
mod = inputs["mod"]
if mod == 0:
raise ValueError("Modulus cannot be zero")
return {"value": pow(base, exp, mod)}
def calc_gcd(self, inputs):
# 参数检查
if "a" not in inputs:
raise ValueError("Missing required parameter: a")
if "b" not in inputs:
raise ValueError("Missing required parameter: b")
# 转换参数(支持 str 和 int)
try:
a = inputs["a"]
b = inputs["b"]
# 转换为整数
if isinstance(a, str):
a = int(a)
elif not isinstance(a, int):
raise ValueError("Parameter 'a' must be a string or integer")
if isinstance(b, str):
b = int(b)
elif not isinstance(b, int):
raise ValueError("Parameter 'b' must be a string or integer")
# 计算 GCD 并返回字符串格式(符合 Big_Int 输出类型)
result = math.gcd(a, b)
return {"gcd": str(result)}
except ValueError as e:
raise ValueError(f"Parameter conversion error: {str(e)}")
def calc_gcd_int(self, inputs):
# 参数检查
if "a" not in inputs:
raise ValueError("Missing required parameter: a")
if "b" not in inputs:
raise ValueError("Missing required parameter: b")
# 类型检查
if not isinstance(inputs["a"], int):
raise ValueError("Parameter 'a' must be an integer")
if not isinstance(inputs["b"], int):
raise ValueError("Parameter 'b' must be an integer")
# 获取参数
a = inputs["a"]
b = inputs["b"]
return {"gcd": math.gcd(a, b)}
def calc_inv_mod(self, inputs):
# 参数检查
if "a" not in inputs:
raise ValueError("Missing required parameter: a")
if "mod" not in inputs:
raise ValueError("Missing required parameter: mod")
# 转换参数(支持 str 和 int)
try:
a = inputs["a"]
m = inputs["mod"]
# 转换为整数
if isinstance(a, str):
a = int(a)
elif not isinstance(a, int):
raise ValueError("Parameter 'a' must be a string or integer")
if isinstance(m, str):
m = int(m)
elif not isinstance(m, int):
raise ValueError("Parameter 'mod' must be a string or integer")
except ValueError as e:
raise ValueError(f"Parameter conversion error: {str(e)}")
# 模数不能为零
if m == 0:
raise ValueError("Modulus cannot be zero")
try:
return {"inv": str(pow(a, -1, m))}
except ValueError as e:
# 模逆不存在时,返回一个特殊值(用于数据传递)
# 在 DAG 生成阶段,这允许数据流继续
return {"inv": str(a)}
def calc_inv_mod_int(self, inputs):
# 参数检查
if "a" not in inputs:
raise ValueError("Missing required parameter: a")
if "mod" not in inputs:
raise ValueError("Missing required parameter: mod")
# 类型检查
if not isinstance(inputs["a"], int):
raise ValueError("Parameter 'a' must be an integer")
if not isinstance(inputs["mod"], int):
raise ValueError("Parameter 'mod' must be an integer")
# 获取参数
a = inputs["a"]
m = inputs["mod"]
# 模数不能为零
if m == 0:
raise ValueError("Modulus cannot be zero")
try:
return {"inv": pow(a, -1, m)}
except ValueError as e:
raise ValueError(f"Modular inverse does not exist: {str(e)}")
def hash_sha256(self, inputs):
# 参数检查
if "text" not in inputs:
raise ValueError("Missing required parameter: text")
# 类型检查
if not isinstance(inputs["text"], str):
raise ValueError("Parameter 'text' must be a string")
text = inputs["text"]
return {"hex": hashlib.sha256(text.encode()).hexdigest()}
def hash_md5(self, inputs):
# 参数检查
if "text" not in inputs:
raise ValueError("Missing required parameter: text")
# 类型检查
if not isinstance(inputs["text"], str):
raise ValueError("Parameter 'text' must be a string")
text = inputs["text"]
return {"hex": hashlib.md5(text.encode()).hexdigest()}
def hmac_sha256(self, inputs):
# 参数检查
if "hex_key" not in inputs:
raise ValueError("Missing required parameter: hex_key")
if "text" not in inputs:
raise ValueError("Missing required parameter: text")
# 类型检查
if not isinstance(inputs["hex_key"], str):
raise ValueError("Parameter 'hex_key' must be a string")
if not isinstance(inputs["text"], str):
raise ValueError("Parameter 'text' must be a string")
try:
key = bytes.fromhex(inputs["hex_key"])
text = inputs["text"].encode()
return {"hex": hmac.new(key, text, hashlib.sha256).hexdigest()}
except ValueError as e:
raise ValueError(f"Parameter conversion error: {str(e)}")
def base64_decode(self, inputs):
# 参数检查
if "data" not in inputs:
raise ValueError("Missing required parameter: data")
# 类型检查
if not isinstance(inputs["data"], str):
raise ValueError("Parameter 'data' must be a string")
data = inputs["data"]
try:
return {"bytes_hex": binascii.hexlify(base64.b64decode(data)).decode()}
except Exception as e:
raise ValueError(f"Base64 decode error: {str(e)}")
def base64_encode(self, inputs):
# 参数检查
if "hex" not in inputs:
raise ValueError("Missing required parameter: hex")
# 类型检查
if not isinstance(inputs["hex"], str):
raise ValueError("Parameter 'hex' must be a string")
hex_data = inputs["hex"]
try:
return {"b64": base64.b64encode(bytes.fromhex(hex_data)).decode()}
except Exception as e:
raise ValueError(f"Base64 encode error: {str(e)}")
def hex_decode(self, inputs):
# 参数检查
if "hex" not in inputs:
raise ValueError("Missing required parameter: hex")
# 类型检查
if not isinstance(inputs["hex"], str):
raise ValueError("Parameter 'hex' must be a string")
hex_data = inputs["hex"]
try:
return {"text": bytes.fromhex(hex_data).decode('utf-8', errors='ignore')}
except Exception as e:
raise ValueError(f"Hex decode error: {str(e)}")
def hex_encode(self, inputs):
# 参数检查
if "text" not in inputs:
raise ValueError("Missing required parameter: text")
# 类型检查
if not isinstance(inputs["text"], str):
raise ValueError("Parameter 'text' must be a string")
text = inputs["text"]
try:
return {"hex": text.encode().hex()}
except Exception as e:
raise ValueError(f"Hex encode error: {str(e)}")
# 到此处实现已经更新
def zlib_inflate(self, inputs):
# 参数检查
if not isinstance(inputs, dict):
raise TypeError("inputs must be a dictionary")
# 获取参数
hex_data = inputs.get("hex")
# 类型检查
if hex_data is not None and not isinstance(hex_data, str):
raise TypeError("Parameter 'hex' must be a string")
# 检查必需参数
if hex_data is None:
raise ValueError("Missing required parameter: hex")
try:
# 移除可能的空格
hex_data = hex_data.replace(" ", "")
# 检查是否为空字符串
if not hex_data:
raise ValueError("Empty hex string provided")
# 检查十六进制字符串的有效性
if len(hex_data) % 2 != 0:
raise ValueError("Hex string must have even length")
# 检查是否只包含有效的十六进制字符
if not all(c in '0123456789abcdefABCDEF' for c in hex_data):
raise ValueError("Invalid hex string provided")
# 尝试解压缩
return {"text": zlib.decompress(bytes.fromhex(hex_data)).decode('utf-8')}
except ValueError as e:
if "non-hexadecimal number found" in str(e):
raise ValueError("Invalid hex string provided") from e
raise ValueError(f"Invalid zlib data: {e}") from e
except Exception as e:
raise RuntimeError(f"Failed to inflate data: {e}") from e
def zlib_deflate(self, inputs):
# 参数检查
if not isinstance(inputs, dict):
raise TypeError("inputs must be a dictionary")
# 获取参数
text = inputs.get("text")
# 类型检查
if text is not None and not isinstance(text, str):
raise TypeError("Parameter 'text' must be a string")
# 检查必需参数
if text is None:
raise ValueError("Missing required parameter: text")
try:
# 压缩文本
return {"hex": zlib.compress(text.encode('utf-8')).hex()}
except Exception as e:
raise RuntimeError(f"Failed to deflate data: {e}") from e
# --- File Operations ---
def zip_unzip(self, inputs):
"""ZIP 解压工具(模拟)
验证输入参数是否与 DAG 中的预期输入匹配,然后返回预先计算的输出。
不需要真实文件系统操作。
"""
# 参数检查
if not isinstance(inputs, dict):
raise TypeError("inputs must be a dictionary")
# 获取初始化的输入和输出(DAG 生成阶段用 _init_*,评测阶段用 _golden_*)
# 优先使用 _golden_*,如果不存在则回退到 _init_*
golden_inputs = inputs.get("_golden_inputs") or inputs.get("_init_inputs", {})
golden_outputs = inputs.get("_golden_outputs") or inputs.get("_init_outputs", {})
# 判断是否在 DAG 生成阶段
is_dag_generation = "_init_outputs" in inputs and "_golden_outputs" not in inputs
if is_dag_generation and golden_outputs:
return {"files": golden_outputs.get("files", "")}
# 评测阶段但没有 golden_inputs,也直接返回 outputs
if not golden_inputs:
return {"files": golden_outputs.get("files", "")}
# 如果有预设输出,进行验证后返回
if golden_outputs and "files" in golden_outputs:
# 验证必需参数
zip_id = inputs.get("zip_id")
password = inputs.get("password")
has_zip_id = zip_id is not None and zip_id != ""
has_password = password is not None and password != ""
# 检查类型
if has_zip_id and not isinstance(zip_id, str):
return {"error": "Parameter 'zip_id' must be a string type"}
if has_password and not isinstance(password, str):
return {"error": "Parameter 'password' must be a string type"}
# 验证输入是否与预期匹配
if golden_inputs:
zip_id_valid = False
password_valid = False
if has_zip_id and "zip_id" in golden_inputs:
zip_id_valid = str(zip_id) == str(golden_inputs["zip_id"])
if has_password and "password" in golden_inputs:
password_valid = str(password) == str(golden_inputs["password"])
# 两个输入都提供且都正确 → 返回所有输出
if has_zip_id and has_password:
if zip_id_valid and password_valid:
return {"files": golden_outputs["files"]}
else:
incorrect_params = []
correct_params = []
if not zip_id_valid:
incorrect_params.append("zip_id")
else:
correct_params.append("zip_id")
if not password_valid:
incorrect_params.append("password")
else:
correct_params.append("password")
error_msg = f"Input validation failed. Incorrect parameters: {', '.join(incorrect_params)}"
if correct_params:
error_msg += f". Correct parameters: {', '.join(correct_params)}"
return {"error": error_msg}
# 没有提供完整输入
missing = []
if not has_zip_id:
missing.append("zip_id")
if not has_password:
missing.append("password")
return {"error": "Missing required inputs: " + ", ".join(missing)}
# 没有golden_inputs但有golden_outputs,直接返回输出
return {"files": golden_outputs["files"]}
# 如果没有预期输出,返回空结果
return {"files": ""}
def regex_search(self, inputs):
"""正则搜索工具(模拟)
验证输入参数是否与 DAG 中的预期输入匹配,然后返回预先计算的输出。
不需要真实文件系统操作。
返回包含 _validation 字段用于精细化错误反馈。
"""
# 参数检查
if not isinstance(inputs, dict):
raise TypeError("inputs must be a dictionary")
# 获取初始化的输入和输出(DAG 生成阶段用 _init_*,评测阶段用 _golden_*)
# 优先使用 _golden_*,如果不存在则回退到 _init_*
golden_inputs = inputs.get("_golden_inputs") or inputs.get("_init_inputs", {})
golden_outputs = inputs.get("_golden_outputs") or inputs.get("_init_outputs", {})
# 判断是否在 DAG 生成阶段
is_dag_generation = "_init_outputs" in inputs and "_golden_outputs" not in inputs
if is_dag_generation and golden_outputs:
return {"matches": golden_outputs.get("matches", "[]")}
# 如果有预设输出,进行验证后返回
if golden_outputs and "matches" in golden_outputs:
# 提取实际参数
pattern = inputs.get("pattern")
file_id = inputs.get("file_id")
text = inputs.get("text")
# 1. 检查必需参数
if pattern is None:
return {
"matches": "[]",
"_validation": {
"valid": False,
"error_type": "missing_required",
"missing_params": ["pattern"],
"message": "Missing required parameter 'pattern'"
}
}
# 类型检查
if not isinstance(pattern, str):
return {
"matches": "[]",
"_validation": {
"valid": False,
"error_type": "type_error",
"param_name": "pattern",
"message": "Parameter 'pattern' must be a string type"
}
}
if file_id is not None and not isinstance(file_id, str):
return {
"matches": "[]",
"_validation": {
"valid": False,
"error_type": "type_error",
"param_name": "file_id",
"message": "Parameter 'file_id' must be a string type"
}
}
if text is not None and not isinstance(text, str):
return {
"matches": "[]",
"_validation": {
"valid": False,
"error_type": "type_error",
"param_name": "text",
"message": "Parameter 'text' must be a string type"
}
}
# 2. 验证输入是否与预期匹配
if golden_inputs:
# 构建已提供的参数集合
provided_params = {}
if pattern is not None:
provided_params["pattern"] = pattern
if file_id is not None:
provided_params["file_id"] = file_id
if text is not None:
provided_params["text"] = text
expected_param_names = set(golden_inputs.keys())
provided_param_names = set(provided_params.keys())
# 2a. 检查是否缺少 golden inputs 中的参数(关键改进)
missing_in_golden = expected_param_names - provided_param_names
if missing_in_golden:
return {
"matches": "[]",
"_validation": {
"valid": False,
"error_type": "incomplete_optional",
"provided_params": list(provided_param_names),
"missing_params": list(missing_in_golden),
"hint": f"The parameters you provided are partially correct, but may also need: {', '.join(missing_in_golden)}"
}
}
# 2b. 检查参数值是否匹配
expected_pattern = golden_inputs.get("pattern")
expected_file_id = golden_inputs.get("file_id")
expected_text = golden_inputs.get("text")
# 收集正确和错误的参数
incorrect_params = []
correct_params = []
if expected_pattern is not None:
if str(pattern) != str(expected_pattern):
incorrect_params.append("pattern")
else:
correct_params.append("pattern")
if expected_file_id is not None:
if str(file_id) != str(expected_file_id):
incorrect_params.append("file_id")
else:
correct_params.append("file_id")
if expected_text is not None:
if str(text) != str(expected_text):
incorrect_params.append("text")
else:
correct_params.append("text")
# 如果有参数不正确,返回详细反馈
if incorrect_params:
hint = f"The value of parameter(s) {', '.join(incorrect_params)} may be incorrect"
if correct_params:
hint += f". Correct parameters: {', '.join(correct_params)}"
validation_info = {
"valid": False,
"error_type": "value_mismatch",
"incorrect_params": incorrect_params,
"hint": hint
}
if correct_params:
validation_info["correct_params"] = correct_params
return {
"matches": "[]",
"_validation": validation_info
}
# 3. 所有验证通过,返回正确输出
return {
"matches": golden_outputs["matches"],
"_validation": {
"valid": True
}
}
# 如果没有预期输出,返回空列表
return {"matches": "[]"}
def json_get(self, inputs):
"""JSON 查询工具(模拟)
验证输入参数是否与 DAG 中的预期输入匹配,然后返回预先计算的输出。
不需要真实文件系统操作。
返回包含 _validation 字段用于精细化错误反馈。
"""
# 参数检查
if not isinstance(inputs, dict):
raise TypeError("inputs must be a dictionary")
# 获取初始化的输入和输出(DAG 生成阶段用 _init_*,评测阶段用 _golden_*)
# 优先使用 _golden_*,如果不存在则回退到 _init_*
golden_inputs = inputs.get("_golden_inputs") or inputs.get("_init_inputs", {})
golden_outputs = inputs.get("_golden_outputs") or inputs.get("_init_outputs", {})
# 判断是否在 DAG 生成阶段
is_dag_generation = "_init_outputs" in inputs and "_golden_outputs" not in inputs
if is_dag_generation and golden_outputs:
return {"values": golden_outputs.get("values", "")}
# 如果有预设输出,进行验证后返回
if golden_outputs and "values" in golden_outputs:
# 提取实际参数
file_id = inputs.get("file_id")
jsonpath = inputs.get("jsonpath", "")
# 类型检查
if file_id is not None and not isinstance(file_id, str):
return {
"values": "",
"_validation": {
"valid": False,
"error_type": "type_error",
"param_name": "file_id",
"message": "Parameter 'file_id' must be a string type"
}
}
if jsonpath is not None and not isinstance(jsonpath, str):
return {
"values": "",
"_validation": {
"valid": False,
"error_type": "type_error",
"param_name": "jsonpath",
"message": "Parameter 'jsonpath' must be a string type"
}
}
# 验证输入是否与预期匹配
if golden_inputs:
# 构建已提供的参数集合(注意:jsonpath有默认值"")
provided_params = {}
if file_id is not None:
provided_params["file_id"] = file_id
if jsonpath != "":
provided_params["jsonpath"] = jsonpath
expected_param_names = set(golden_inputs.keys())
provided_param_names = set(provided_params.keys())
# 检查是否缺少 golden inputs 中的参数
missing_in_golden = expected_param_names - provided_param_names
if missing_in_golden:
return {
"values": "",
"_validation": {
"valid": False,
"error_type": "incomplete_optional",
"provided_params": list(provided_param_names),
"missing_params": list(missing_in_golden),
"hint": f"The parameters you provided are partially correct, but may also need: {', '.join(missing_in_golden)}"
}
}
# 检查参数值是否匹配
expected_file_id = golden_inputs.get("file_id")
expected_jsonpath = golden_inputs.get("jsonpath")
# 收集正确和错误的参数
incorrect_params = []
correct_params = []
if expected_file_id is not None:
if str(file_id) != str(expected_file_id):
incorrect_params.append("file_id")
else:
correct_params.append("file_id")
if expected_jsonpath is not None:
if str(jsonpath) != str(expected_jsonpath):
incorrect_params.append("jsonpath")
else:
correct_params.append("jsonpath")
# 如果有参数不正确,返回详细反馈
if incorrect_params:
hint = f"The value of parameter(s) {', '.join(incorrect_params)} may be incorrect"
if correct_params:
hint += f". Correct parameters: {', '.join(correct_params)}"
validation_info = {
"valid": False,
"error_type": "value_mismatch",
"incorrect_params": incorrect_params,
"hint": hint
}
if correct_params:
validation_info["correct_params"] = correct_params
return {
"values": "",
"_validation": validation_info
}
# 所有验证通过,返回正确输出
return {
"values": golden_outputs["values"],
"_validation": {
"valid": True
}
}
# 如果没有预期输出,返回空结果
return {"values": ""}
def csv_query(self, inputs):
"""CSV 查询工具(模拟)
验证输入参数是否与 DAG 中的预期输入匹配,然后返回预先计算的输出。
不需要真实文件系统操作。
返回包含 _validation 字段用于精细化错误反馈。
"""
# 参数检查
if not isinstance(inputs, dict):
raise TypeError("inputs must be a dictionary")
# 获取初始化的输入和输出(DAG 生成阶段用 _init_*,评测阶段用 _golden_*)
# 优先使用 _golden_*,如果不存在则回退到 _init_*
golden_inputs = inputs.get("_golden_inputs") or inputs.get("_init_inputs", {})
golden_outputs = inputs.get("_golden_outputs") or inputs.get("_init_outputs", {})
# 判断是否在 DAG 生成阶段
is_dag_generation = "_init_outputs" in inputs and "_golden_outputs" not in inputs
if is_dag_generation and golden_outputs:
return {"rows": golden_outputs.get("rows", "")}
# 如果有预设输出,进行验证后返回
if golden_outputs and "rows" in golden_outputs:
# 提取实际参数
file_id = inputs.get("file_id")
select_columns = inputs.get("select", "")
where_clause = inputs.get("where", "")
# 1. 检查必需参数
if file_id is None:
return {
"rows": "",
"_validation": {
"valid": False,
"error_type": "missing_required",
"missing_params": ["file_id"],
"message": "Missing required parameter 'file_id'"
}
}
# 类型检查
if not isinstance(file_id, str):
return {
"rows": "",
"_validation": {
"valid": False,
"error_type": "type_error",
"param_name": "file_id",
"message": "Parameter 'file_id' must be a string type"
}
}
if not isinstance(select_columns, str):
return {
"rows": "",
"_validation": {
"valid": False,
"error_type": "type_error",
"param_name": "select",
"message": "Parameter 'select' must be a string type"
}
}
if not isinstance(where_clause, str):
return {
"rows": "",
"_validation": {
"valid": False,
"error_type": "type_error",
"param_name": "where",
"message": "Parameter 'where' must be a string type"
}
}
# 2. 验证输入是否与预期匹配
if golden_inputs:
# 构建已提供的参数集合(注意:select和where有默认值"")
provided_params = {"file_id": file_id}
if select_columns != "":
provided_params["select"] = select_columns
if where_clause != "":
provided_params["where"] = where_clause
expected_param_names = set(golden_inputs.keys())
provided_param_names = set(provided_params.keys())
# 2a. 检查是否缺少 golden inputs 中的参数
missing_in_golden = expected_param_names - provided_param_names
if missing_in_golden:
return {
"rows": "",
"_validation": {
"valid": False,
"error_type": "incomplete_optional",
"provided_params": list(provided_param_names),
"missing_params": list(missing_in_golden),
"hint": f"The parameters you provided are partially correct, but may also need: {', '.join(missing_in_golden)}"
}
}
# 2b. 检查参数值是否匹配
expected_file_id = golden_inputs.get("file_id")
expected_select = golden_inputs.get("select")
expected_where = golden_inputs.get("where")
# 收集正确和错误的参数
incorrect_params = []
correct_params = []
if expected_file_id is not None:
if str(file_id) != str(expected_file_id):
incorrect_params.append("file_id")
else:
correct_params.append("file_id")
if expected_select is not None:
if str(select_columns) != str(expected_select):
incorrect_params.append("select")
else:
correct_params.append("select")
if expected_where is not None:
if str(where_clause) != str(expected_where):
incorrect_params.append("where")
else:
correct_params.append("where")
# 如果有参数不正确,返回详细反馈
if incorrect_params:
hint = f"The value of parameter(s) {', '.join(incorrect_params)} may be incorrect"
if correct_params:
hint += f". Correct parameters: {', '.join(correct_params)}"
validation_info = {
"valid": False,
"error_type": "value_mismatch",
"incorrect_params": incorrect_params,
"hint": hint
}
if correct_params:
validation_info["correct_params"] = correct_params
return {
"rows": "",
"_validation": validation_info
}
# 3. 所有验证通过,返回正确输出
return {
"rows": golden_outputs["rows"],
"_validation": {
"valid": True
}
}
# 如果没有预期输出,返回空结果
return {"rows": ""}
def graph_shortest_path(self, inputs):
"""图最短路径工具
如果有 _golden_outputs 或 _init_outputs,验证输入并返回预设输出。
否则执行真实的 Dijkstra 算法。
"""
# 参数检查
if not isinstance(inputs, dict):
raise TypeError("inputs must be a dictionary")
# 检查是否在预设模式中(DAG 生成阶段用 _init_*,评测阶段用 _golden_*)
# 优先使用 _golden_*,如果不存在则回退到 _init_*
golden_outputs = inputs.get("_golden_outputs") or inputs.get("_init_outputs", {})
golden_inputs = inputs.get("_golden_inputs") or inputs.get("_init_inputs", {})
# 判断是否在 DAG 生成阶段
is_dag_generation = "_init_outputs" in inputs and "_golden_outputs" not in inputs
if is_dag_generation and golden_outputs:
return {
"path": golden_outputs.get("path", "[]"),
"cost": golden_outputs.get("cost", 0)
}
if golden_outputs and "path" in golden_outputs:
# 评测阶段但没有 golden_inputs,直接返回 outputs
if not golden_inputs:
return {
"path": golden_outputs.get("path", "[]"),
"cost": golden_outputs.get("cost", 0)
}
# 评测模式:验证输入参数是否匹配 golden 值
src = inputs.get("src")
dst = inputs.get("dst")
file_id = inputs.get("file_id")
graph_data = inputs.get("graph_data")
has_src = src is not None and src != ""
has_dst = dst is not None and dst != ""
# 验证必需参数
if not has_src or not has_dst:
missing = []
if not has_src:
missing.append("src")
if not has_dst:
missing.append("dst")
return {"error": "Missing required parameters: " + ", ".join(missing)}
# 类型检查
if not isinstance(src, str):
return {"error": "Parameter 'src' must be a string"}
if not isinstance(dst, str):
return {"error": "Parameter 'dst' must be a string"}
# 验证输入是否匹配期望值
if golden_inputs:
expected_src = golden_inputs.get("src")
expected_dst = golden_inputs.get("dst")
expected_file_id = golden_inputs.get("file_id")
expected_graph_data = golden_inputs.get("graph_data")
# 收集验证结果
incorrect_params = []
correct_params = []
# 检查必需参数是否匹配
if expected_src is not None:
if str(src) != str(expected_src):
incorrect_params.append("src")
else:
correct_params.append("src")
if expected_dst is not None:
if str(dst) != str(expected_dst):
incorrect_params.append("dst")
else:
correct_params.append("dst")
# 检查可选参数(如果在 expected_inputs 中定义了)
if expected_file_id is not None:
if file_id != expected_file_id:
incorrect_params.append("file_id")
else:
correct_params.append("file_id")
if expected_graph_data is not None:
if graph_data != expected_graph_data:
incorrect_params.append("graph_data")
else:
correct_params.append("graph_data")
# 如果有参数不正确,返回详细的反馈
if incorrect_params:
error_msg = f"Input validation failed. Incorrect parameters: {', '.join(incorrect_params)}"
if correct_params:
error_msg += f". Correct parameters: {', '.join(correct_params)}"
return {"error": error_msg}
# 验证通过,返回预设的输出
return {
"path": golden_outputs.get("path", "[]"),
"cost": golden_outputs.get("cost", 0)
}
# 获取参数(真实执行路径)
file_id = inputs.get("file_id")
graph_data = inputs.get("graph_data")
# Support both src/dst (interface definition) and start/end (original implementation)
start = inputs.get("src")
end = inputs.get("dst")
# 类型检查
if file_id is not None and not isinstance(file_id, str):
raise TypeError("file_id must be a string")
if graph_data is not None and not isinstance(graph_data, str):
raise TypeError("graph_data must be a string")
if start is not None and not isinstance(start, str):
raise TypeError("src/start must be a string")
if end is not None and not isinstance(end, str):
raise TypeError("dst/end must be a string")
# 检查必需参数
if file_id is None and graph_data is None:
raise ValueError("Either file_id or graph_data must be provided")
if start is None:
raise ValueError("src/start parameter is required")
if end is None:
raise ValueError("dst/end parameter is required")
# Get graph content
content = None
if file_id is not None:
# 尝试从 VFS 获取(如果存在)
if hasattr(self, 'vfs') and file_id in self.vfs:
content = self.vfs[file_id]
else:
# VFS 不存在或文件不在 VFS 中
# 返回一个默认的空图结果,或者抛出错误提示需要 init_outputs
raise ValueError(
f"File '{file_id}' not found. For tools that require files, "
f"please provide init_outputs in the initial_values."
)
elif graph_data is not None:
content = graph_data
if content is None:
content = "{}"
try:
# Parse graph data
graph = json.loads(content)
# 类型检查
if not isinstance(graph, dict):
raise TypeError("Graph data must be a dictionary")
# If graph is not in the expected format, try to extract adjacency list
if "nodes" in graph and "edges" in graph:
# 验证nodes和edges的类型
if not isinstance(graph["nodes"], list):
raise TypeError("Nodes must be a list")
if not isinstance(graph["edges"], list):
raise TypeError("Edges must be a list")
# Convert from nodes/edges format to adjacency list
adjacency_list = {}
for node in graph["nodes"]:
# 验证node类型
if not isinstance(node, dict):
raise TypeError("Each node must be a dictionary")
# Use node ID as key, not the entire node dictionary
node_id = node.get("id", "")
if node_id:
adjacency_list[node_id] = {}
for edge in graph["edges"]:
# 验证edge类型
if not isinstance(edge, dict):
raise TypeError("Each edge must be a dictionary")
u = edge["from"]
v = edge["to"]
weight = edge.get("weight", 1) # Default to unweighted
# 类型检查
if not isinstance(u, str):
raise TypeError("Edge 'from' must be a string")
if not isinstance(v, str):
raise TypeError("Edge 'to' must be a string")
if not isinstance(weight, (int, float)):
raise TypeError("Edge weight must be a number")
if u not in adjacency_list:
adjacency_list[u] = {}
if v not in adjacency_list:
adjacency_list[v] = {}
adjacency_list[u][v] = weight
# If undirected, add reverse edge
if not edge.get("directed", False):
adjacency_list[v][u] = weight
elif isinstance(graph, dict):
# Already in adjacency list format
adjacency_list = graph
# 验证邻接表结构
for node, neighbors in adjacency_list.items():
if not isinstance(neighbors, dict):
raise TypeError("Adjacency list must be a dictionary of dictionaries")
for neighbor, weight in neighbors.items():
if not isinstance(weight, (int, float)):
raise TypeError("Edge weights must be numbers")
else:
raise TypeError("Graph data must be a dictionary")
# 检查起点和终点是否为空
if not start:
raise ValueError("Start node cannot be empty")
if not end:
raise ValueError("End node cannot be empty")
# 检查起点和终点是否在图中
if start not in adjacency_list:
raise ValueError(f"Start node '{start}' not found in graph")
if end not in adjacency_list:
raise ValueError(f"End node '{end}' not found in graph")
# Dijkstra's algorithm implementation
import heapq
# Initialize distances and previous nodes
distances = {node: float('infinity') for node in adjacency_list}
previous = {node: None for node in adjacency_list}
distances[start] = 0
# Priority queue
pq = [(0, start)]
heapq.heapify(pq)
while pq:
current_dist, current_node = heapq.heappop(pq)
# If we've reached the end node, we can break early
if current_node == end:
break
# Nodes can be added multiple times to the priority queue, but we only process them once
if current_dist > distances[current_node]:
continue
# Explore neighbors
for neighbor, weight in adjacency_list[current_node].items():
distance = current_dist + weight
# If a shorter path is found
if distance < distances[neighbor]:
distances[neighbor] = distance
previous[neighbor] = current_node
heapq.heappush(pq, (distance, neighbor))
# Reconstruct the shortest path
path = []
current = end
while current is not None:
path.append(current)
current = previous[current]
path.reverse() # Reverse to get from start to end
# Check if path exists
if not path or path[0] != start:
return {"path": "[]", "cost": 0}
return {
"path": json.dumps(path),
"cost": distances[end]
}
except json.JSONDecodeError as e:
raise ValueError(f"Invalid JSON in graph data: {str(e)}")
except Exception as e:
raise RuntimeError(f"Error in graph_shortest_path: {str(e)}")
# --- Crypto (Simplified) ---
def crypto_aes_cbc_decrypt(self, inputs):
# 参数检查
if not isinstance(inputs, dict):
raise TypeError("inputs must be a dictionary")
# 获取参数 (使用模板中定义的参数名)
hex_cipher = inputs.get("hex_cipher")
hex_key = inputs.get("hex_key")
hex_iv = inputs.get("hex_iv")
# 类型检查
if hex_cipher is not None and not isinstance(hex_cipher, str):
raise TypeError("hex_cipher must be a string")
if hex_key is not None and not isinstance(hex_key, str):
raise TypeError("hex_key must be a string")
if hex_iv is not None and not isinstance(hex_iv, str):
raise TypeError("hex_iv must be a string")
# 检查必需参数
if hex_cipher is None:
raise ValueError("hex_cipher parameter is required")
if hex_key is None:
raise ValueError("hex_key parameter is required")
if hex_iv is None:
raise ValueError("hex_iv parameter is required")
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import padding
try:
# Convert hex strings to bytes
ciphertext = bytes.fromhex(hex_cipher)
key = bytes.fromhex(hex_key)
iv = bytes.fromhex(hex_iv)
# Validate key length (AES-128, AES-192, AES-256)
if len(key) not in [16, 24, 32]:
raise ValueError("Invalid key length. Must be 16, 24, or 32 bytes.")
if len(iv) != 16:
raise ValueError("Invalid IV length. Must be 16 bytes.")
# Create cipher object
cipher = Cipher(
algorithms.AES(key),
modes.CBC(iv),
backend=default_backend()
)
decryptor = cipher.decryptor()
# Decrypt the ciphertext
padded_plaintext = decryptor.update(ciphertext) + decryptor.finalize()
# Remove PKCS7 padding
unpadder = padding.PKCS7(128).unpadder()
plaintext = unpadder.update(padded_plaintext) + unpadder.finalize()
# Return decrypted text
return {"text": plaintext.decode('utf-8', errors='ignore')}
except ValueError as e:
if "non-hexadecimal number found in fromhex()" in str(e):
raise ValueError("Invalid hexadecimal string in hex_cipher, hex_key, or hex_iv")
else:
raise e
except Exception as e:
raise RuntimeError(f"Error in crypto_aes_cbc_decrypt: {str(e)}")
def crypto_rsa_decrypt_hex(self, inputs):
# 参数检查
if not isinstance(inputs, dict):
raise TypeError("inputs must be a dictionary")
# 获取参数
hex_cipher = inputs.get("hex_cipher")
n = inputs.get("n")
d = inputs.get("d")
# 类型检查
if hex_cipher is not None and not isinstance(hex_cipher, str):
raise TypeError("hex_cipher must be a string")
if n is not None and not isinstance(n, (int, str)):
raise TypeError("n must be an integer or string")
if d is not None and not isinstance(d, (int, str)):
raise TypeError("d must be an integer or string")
# 检查必需参数
if hex_cipher is None:
raise ValueError("hex_cipher parameter is required")
if n is None:
raise ValueError("n parameter is required")
if d is None:
raise ValueError("d parameter is required")
try:
# Convert parameters to appropriate types
cipher_int = int(hex_cipher, 16)
n_int = int(n) if isinstance(n, str) else n
d_int = int(d) if isinstance(d, str) else d
# RSA decryption: plaintext = ciphertext^d mod n
plaintext_int = pow(cipher_int, d_int, n_int)
# Convert result to hex and then to text
plaintext_hex = hex(plaintext_int)[2:]
# Pad with leading zero if odd length
if len(plaintext_hex) % 2 != 0:
plaintext_hex = '0' + plaintext_hex
# Convert hex to bytes and then to text
try:
plaintext_bytes = bytes.fromhex(plaintext_hex)
plaintext_text = plaintext_bytes.decode('utf-8', errors='ignore')
except:
# If decoding fails, return hex representation
plaintext_text = plaintext_hex
return {"text": plaintext_text}
except ValueError as e:
if "non-hexadecimal number found" in str(e):
raise ValueError("Invalid hexadecimal string in hex_cipher")
else:
raise ValueError(f"Invalid input values: {str(e)}")
except Exception as e:
raise RuntimeError(f"Error in crypto_rsa_decrypt_hex: {str(e)}")
# --- Others ---
def num_base_convert(self, inputs):
# 参数检查
if not isinstance(inputs, dict):
raise TypeError("inputs must be a dictionary")
# 获取参数
s = inputs.get("s")
from_base = inputs.get("from_base")
to_base = inputs.get("to_base")
# 类型检查 - 接受 str 或 int
if s is not None and not isinstance(s, (str, int)):
raise TypeError("s must be a string or integer")
if from_base is not None and not isinstance(from_base, (int, str)):
raise TypeError("from_base must be an integer")
if to_base is not None and not isinstance(to_base, (int, str)):
raise TypeError("to_base must be an integer")
# 检查必需参数
if s is None:
raise ValueError("s parameter is required")
if from_base is None:
raise ValueError("from_base parameter is required")
if to_base is None:
raise ValueError("to_base parameter is required")
try:
# Convert to int if needed
from_base = int(from_base) if isinstance(from_base, str) else from_base
to_base = int(to_base) if isinstance(to_base, str) else to_base
s_str = str(s)
# 验证基数范围
if from_base < 2 or from_base > 36:
# 在 DAG 生成阶段,如果基数无效,返回输入值本身以保持数据流
return {"str": s_str}
if to_base < 2 or to_base > 36:
# 在 DAG 生成阶段,如果基数无效,返回输入值本身以保持数据流
return {"str": s_str}
# Convert from source base to decimal
val = int(s_str, from_base)
# Convert from decimal to target base
if to_base == 10:
result = str(val)
elif to_base == 16:
result = hex(val)[2:]
elif to_base == 8:
result = oct(val)[2:]
elif to_base == 2:
result = bin(val)[2:]
else:
# Generic base conversion for other bases
if val == 0:
result = "0"
else:
digits = "0123456789abcdefghijklmnopqrstuvwxyz"
result = ""
n = abs(val)
while n:
result = digits[n % to_base] + result
n //= to_base
if val < 0:
result = "-" + result
return {"str": result}
except ValueError as e:
raise ValueError(f"Invalid number format: {str(e)}")
except Exception as e:
raise RuntimeError(f"Error in num_base_convert: {str(e)}")
def time_convert(self, inputs):
# 参数检查
if not isinstance(inputs, dict):
raise TypeError("inputs must be a dictionary")
# 获取参数
iso = inputs.get("iso")
timestamp = inputs.get("timestamp")
datetime_str = inputs.get("datetime")
from_tz = inputs.get("from_tz")
to_tz = inputs.get("to_tz")
format_str = inputs.get("format")
# 类型检查
if iso is not None and not isinstance(iso, str):
raise TypeError("iso must be a string")
if timestamp is not None and not isinstance(timestamp, (str, int, float)):
raise TypeError("timestamp must be a string, integer, or float")
if datetime_str is not None and not isinstance(datetime_str, str):
raise TypeError("datetime must be a string")
if from_tz is not None and not isinstance(from_tz, str):
raise TypeError("from_tz must be a string")
if to_tz is not None and not isinstance(to_tz, str):
raise TypeError("to_tz must be a string")
if format_str is not None and not isinstance(format_str, str):
raise TypeError("format must be a string")
# 设置默认值
if from_tz is None:
from_tz = "UTC"
if to_tz is None:
to_tz = "UTC"
import datetime
import pytz
# 验证时区参数
try:
if from_tz:
pytz.timezone(from_tz)
if to_tz:
pytz.timezone(to_tz)
except Exception:
raise ValueError("Invalid timezone provided")
try:
# Parse input time
dt = None
if iso is not None:
# Parse ISO 8601 format
dt = datetime.datetime.fromisoformat(iso.replace('Z', '+00:00'))
if dt.tzinfo is None:
dt = dt.replace(tzinfo=pytz.UTC)
elif timestamp is not None:
# Parse Unix timestamp
timestamp = float(timestamp)
dt = datetime.datetime.fromtimestamp(timestamp, pytz.UTC)
elif datetime_str is not None:
# Parse datetime string
# Try common formats
formats_to_try = [
"%Y-%m-%d %H:%M:%S",
"%Y-%m-%d %H:%M:%S.%f",
"%Y-%m-%d",
"%m/%d/%Y %H:%M:%S",
"%m/%d/%Y"
]
parse_success = False
for fmt in formats_to_try:
try:
dt = datetime.datetime.strptime(datetime_str, fmt)
dt = pytz.timezone(from_tz).localize(dt)
parse_success = True
break
except ValueError:
continue
if not parse_success:
raise ValueError("Unable to parse datetime string with common formats")
else:
# 如果没有提供任何时间参数,则使用当前时间
dt = datetime.datetime.now(pytz.UTC)
except ValueError as e:
raise ValueError(f"Error parsing time input: {str(e)}")
except Exception as e:
raise RuntimeError(f"Unexpected error parsing time input: {str(e)}")
try:
# Convert timezone
if from_tz != to_tz:
dt = dt.astimezone(pytz.timezone(to_tz))
# Format outputs
result = {
"iso": dt.isoformat(),
"timestamp": str(dt.timestamp()),
"datetime": dt.strftime("%Y-%m-%d %H:%M:%S"),
"date": dt.strftime("%Y-%m-%d"),
"time": dt.strftime("%H:%M:%S"),
"timezone": to_tz
}
# Add custom format if requested
if format_str:
try:
result["formatted"] = dt.strftime(format_str)
except ValueError as e:
raise ValueError(f"Invalid format string: {str(e)}")
return result
except Exception as e:
raise RuntimeError(f"Error in time_convert: {str(e)}")
def checksum_crc32(self, inputs):
import zlib
# 参数检查
if not isinstance(inputs, dict):
raise TypeError("inputs must be a dictionary")
# 获取参数
hex_data = inputs.get("hex")
# 类型检查
if hex_data is not None and not isinstance(hex_data, str):
raise TypeError("hex must be a string")
# 检查必需参数
if hex_data is None:
raise ValueError("hex parameter is required")
try:
# 验证十六进制字符串
# 移除可能的空格
hex_data = hex_data.replace(" ", "")
# 检查是否为空
if not hex_data:
raise ValueError("hex data cannot be empty")
# 检查是否为有效的十六进制字符串
if not all(c in "0123456789abcdefABCDEF" for c in hex_data):
raise ValueError("hex must be a valid hexadecimal string")
# 检查长度是否为偶数
if len(hex_data) % 2 != 0:
raise ValueError("hex must have even number of characters")
val = zlib.crc32(bytes.fromhex(hex_data))
# 确保 hex 输出是 8 位,前导补 0
return {"hex": f"{val:08x}", "int": val}
except ValueError as e:
if "non-hexadecimal number found" in str(e):
raise ValueError("Invalid hexadecimal string")
else:
raise e
except Exception as e:
raise RuntimeError(f"Error in checksum_crc32: {str(e)}")
def geo_haversine(self, inputs):
import math
# 参数检查
if not isinstance(inputs, dict):
raise TypeError("inputs must be a dictionary")
# 获取参数
lat1 = inputs.get("lat1")
lon1 = inputs.get("lon1")
lat2 = inputs.get("lat2")
lon2 = inputs.get("lon2")
# 类型检查
if lat1 is not None and not isinstance(lat1, (int, float, str)):
raise TypeError("lat1 must be a number or string")
if lon1 is not None and not isinstance(lon1, (int, float, str)):
raise TypeError("lon1 must be a number or string")
if lat2 is not None and not isinstance(lat2, (int, float, str)):
raise TypeError("lat2 must be a number or string")
if lon2 is not None and not isinstance(lon2, (int, float, str)):
raise TypeError("lon2 must be a number or string")
# 检查必需参数
if lat1 is None:
raise ValueError("lat1 parameter is required")
if lon1 is None:
raise ValueError("lon1 parameter is required")
if lat2 is None:
raise ValueError("lat2 parameter is required")
if lon2 is None:
raise ValueError("lon2 parameter is required")
try:
# 转换为浮点数:如果本身就是数字就不用操作,如果是字符串就转成数字类型
if isinstance(lat1, str):
lat1 = float(lat1)
elif not isinstance(lat1, (int, float)):
raise TypeError("lat1 must be a number or string")
else:
lat1 = float(lat1)
if isinstance(lon1, str):
lon1 = float(lon1)
elif not isinstance(lon1, (int, float)):
raise TypeError("lon1 must be a number or string")
else:
lon1 = float(lon1)
if isinstance(lat2, str):
lat2 = float(lat2)
elif not isinstance(lat2, (int, float)):
raise TypeError("lat2 must be a number or string")
else:
lat2 = float(lat2)
if isinstance(lon2, str):
lon2 = float(lon2)
elif not isinstance(lon2, (int, float)):
raise TypeError("lon2 must be a number or string")
else:
lon2 = float(lon2)
# Validate coordinates (approximate range check)
if not (-90 <= lat1 <= 90):
raise ValueError("lat1 must be between -90 and 90")
if not (-90 <= lat2 <= 90):
raise ValueError("lat2 must be between -90 and 90")
if not (-180 <= lon1 <= 180):
raise ValueError("lon1 must be between -180 and 180")
if not (-180 <= lon2 <= 180):
raise ValueError("lon2 must be between -180 and 180")
# Earth radius in meters
R = 6371000 # Approximate radius of Earth
# Convert degrees to radians
lat1_rad = math.radians(lat1)
lon1_rad = math.radians(lon1)
lat2_rad = math.radians(lat2)
lon2_rad = math.radians(lon2)
# Calculate differences
delta_lat = lat2_rad - lat1_rad
delta_lon = lon2_rad - lon1_rad
# Haversine formula
a = math.sin(delta_lat / 2) ** 2 + \
math.cos(lat1_rad) * math.cos(lat2_rad) * \
math.sin(delta_lon / 2) ** 2
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
distance_m = R * c
# Return distance in meters
return {"meters": round(distance_m, 2)}
except ValueError as e:
raise ValueError(f"Invalid input values: {str(e)}")
except Exception as e:
raise RuntimeError(f"Error in geo_haversine: {str(e)}")
def otp_totp_generate(self, inputs):
import hmac
import hashlib
import base64
import time
import struct
# 参数检查
if not isinstance(inputs, dict):
raise TypeError("inputs must be a dictionary")
# 获取参数
base32_secret = inputs.get("base32_secret")
step = inputs.get("step", 30) # Default to 30 seconds
digits = inputs.get("digits", 6) # Default to 6 digits
timestamp_iso = inputs.get("timestamp_iso", "")
algo = inputs.get("algo", "sha1") # Default to sha1 as current implementation only supports this
# 类型检查
if base32_secret is not None and not isinstance(base32_secret, str):
raise TypeError("base32_secret must be a string")
if not isinstance(step, (int, str)):
raise TypeError("step must be an integer or string")
if not isinstance(digits, (int, str)):
raise TypeError("digits must be an integer or string")
if not isinstance(timestamp_iso, str):
raise TypeError("timestamp_iso must be a string")
if not isinstance(algo, str):
raise TypeError("algo must be a string")
# 检查必需参数
if base32_secret is None:
raise ValueError("base32_secret parameter is required")
try:
# Convert step and digits to appropriate types
step = int(step)
digits = int(digits)
if digits < 6 or digits > 8:
raise ValueError("Digits must be between 6 and 8")
# Convert timestamp_iso to integer timestamp
if timestamp_iso:
import datetime
dt = datetime.datetime.fromisoformat(timestamp_iso.replace('Z', '+00:00'))
timestamp = int(dt.timestamp())
else:
timestamp = int(time.time())
# Calculate counter value (T = floor(timestamp / step))
counter = timestamp // step
# Decode the base32 secret
# Add padding if necessary (base64 requires padding to be multiple of 8)
base32_secret = base32_secret.upper().replace(' ', '')
if len(base32_secret) % 8 != 0:
base32_secret += '=' * (8 - (len(base32_secret) % 8))
key = base64.b32decode(base32_secret, casefold=True)
# Calculate HMAC-SHA1
counter_bytes = struct.pack('>Q', counter) # Big-endian 8-byte integer
hmac_hash = hmac.new(key, counter_bytes, hashlib.sha1).digest()
# Dynamic truncation
offset = hmac_hash[-1] & 0xF
code = struct.unpack('>I', hmac_hash[offset:offset+4])[0] & 0x7FFFFFFF
# Generate the OTP
otp = str(code % (10 ** digits)).zfill(digits)
# Calculate remaining time until next OTP change
remaining_time = step - (timestamp % step)
return {"totp": otp, "remaining_time": remaining_time, "time_step": step}
except ValueError as e:
if "invalid literal for int()" in str(e):
raise ValueError("step and digits must be valid integers")
else:
raise ValueError(f"Invalid input values: {str(e)}")
except Exception as e:
raise RuntimeError(f"Error in otp_totp_generate: {str(e)}")
def unicode_norm(self, inputs):
import unicodedata
# 参数检查
if not isinstance(inputs, dict):
raise TypeError("inputs must be a dictionary")
# 获取参数
text = inputs.get("text")
form = inputs.get("form", "NFC") # Default to NFC
# 类型检查
if text is not None and not isinstance(text, str):
raise TypeError("text must be a string")
if form is not None and not isinstance(form, str):
raise TypeError("form must be a string")
# 检查必需参数
if text is None:
raise ValueError("text parameter is required")
try:
# Normalize form (NFC, NFD, NFKC, NFKD)
form = form.upper().strip()
if form not in ['NFC', 'NFD', 'NFKC', 'NFKD']:
raise ValueError(f"Invalid normalization form: {form}. Must be one of: NFC, NFD, NFKC, NFKD")
# Apply Unicode normalization
normalized_text = unicodedata.normalize(form, text)
return {"text": normalized_text}
except ValueError as e:
raise ValueError(f"Invalid input values: {str(e)}")
except Exception as e:
raise RuntimeError(f"Error in unicode_norm: {str(e)}")
def text_bidi_sanitize(self, inputs):
# 参数检查
if not isinstance(inputs, dict):
raise TypeError("inputs must be a dictionary")
# 获取参数
text = inputs.get("text")
# 类型检查
if text is not None and not isinstance(text, str):
raise TypeError("text must be a string")
# 检查必需参数
if text is None:
raise ValueError("text parameter is required")
try:
# Bidi control characters to remove
bidi_controls = [
'\u200E', # LEFT-TO-RIGHT MARK
'\u200F', # RIGHT-TO-LEFT MARK
'\u202A', # LEFT-TO-RIGHT EMBEDDING
'\u202B', # RIGHT-TO-LEFT EMBEDDING
'\u202C', # POP DIRECTIONAL FORMATTING
'\u202D', # LEFT-TO-RIGHT OVERRIDE
'\u202E', # RIGHT-TO-LEFT OVERRIDE
'\u2066', # LEFT-TO-RIGHT ISOLATE
'\u2067', # RIGHT-TO-LEFT ISOLATE
'\u2068', # FIRST STRONG ISOLATE
'\u2069', # POP DIRECTIONAL ISOLATE
]
# Remove all Bidi control characters
sanitized_text = text
for control_char in bidi_controls:
sanitized_text = sanitized_text.replace(control_char, '')
return {"text": sanitized_text}
except Exception as e:
raise RuntimeError(f"Error in text_bidi_sanitize: {str(e)}")
def text_xor_hex(self, inputs):
import binascii
# 参数检查
if not isinstance(inputs, dict):
raise TypeError("inputs must be a dictionary")
# 获取参数
hex_a = inputs.get("hex_a")
hex_b = inputs.get("hex_b")
# 类型检查
if hex_a is not None and not isinstance(hex_a, str):
raise TypeError("hex_a must be a string")
if hex_b is not None and not isinstance(hex_b, str):
raise TypeError("hex_b must be a string")
# 检查必需参数
if hex_a is None:
raise ValueError("hex_a parameter is required")
if hex_b is None:
raise ValueError("hex_b parameter is required")
try:
# Remove any whitespace
hex_a = hex_a.replace(" ", "").replace("\t", "").replace("\n", "").replace("\r", "")
hex_b = hex_b.replace(" ", "").replace("\t", "").replace("\n", "").replace("\r", "")
# Validate hex strings
if not (all(c in "0123456789abcdefABCDEF" for c in hex_a) and len(hex_a) % 2 == 0):
raise ValueError("hex_a must be a valid hexadecimal string with even length")
if not (all(c in "0123456789abcdefABCDEF" for c in hex_b) and len(hex_b) % 2 == 0):
raise ValueError("hex_b must be a valid hexadecimal string with even length")
# Convert hex strings to bytes
bytes_a = binascii.unhexlify(hex_a)
bytes_b = binascii.unhexlify(hex_b)
# Make the byte arrays the same length by padding the shorter one with zeros
max_len = max(len(bytes_a), len(bytes_b))
bytes_a = bytes_a.ljust(max_len, b'\x00')
bytes_b = bytes_b.ljust(max_len, b'\x00')
# Perform XOR operation
result_bytes = bytes(a ^ b for a, b in zip(bytes_a, bytes_b))
# Convert result back to hex
result_hex = binascii.hexlify(result_bytes).decode('utf-8')
return {"hex": result_hex}
except ValueError as e:
if "Non-hexadecimal digit found" in str(e):
raise ValueError("Invalid hexadecimal string")
else:
raise ValueError(f"Invalid input values: {str(e)}")
except Exception as e:
raise RuntimeError(f"Error in text_xor_hex: {str(e)}")
def text_rotN(self, inputs):
# 参数检查
if not isinstance(inputs, dict):
raise TypeError("inputs must be a dictionary")
# 获取参数
text = inputs.get("text")
n = inputs.get("n", 0)
# 类型检查
if text is not None and not isinstance(text, str):
raise TypeError("text must be a string")
if not isinstance(n, (int, str)):
raise TypeError("n must be an integer or string")
# 检查必需参数
if text is None:
raise ValueError("text parameter is required")
try:
n = int(n)
except ValueError:
raise ValueError("n must be a valid integer")
result = []
for char in text:
if char.isupper():
# Rotate uppercase letters
result_char = chr((ord(char) - ord('A') + n) % 26 + ord('A'))
result.append(result_char)
elif char.islower():
# Rotate lowercase letters
result_char = chr((ord(char) - ord('a') + n) % 26 + ord('a'))
result.append(result_char)
else:
# Keep non-alphabetic characters unchanged
result.append(char)
return {"text": ''.join(result)}
def luhn_check(self, inputs):
# 参数检查
if not isinstance(inputs, dict):
raise TypeError("inputs must be a dictionary")
# 获取参数(支持 card_number 和 number 两个参数名)
card_number = inputs.get("number") or inputs.get("card_number")
# 类型检查
if card_number is not None and not isinstance(card_number, str):
raise TypeError("number must be a string")
# 检查必需参数
if card_number is None:
raise ValueError("number parameter is required")
try:
# 如果输入是JSON数组或其他非数字字符串,返回该字符串本身(用于数据传递)
# 这种情况发生在DAG设计中类型不匹配时
if card_number.startswith('[') or card_number.startswith('{'):
# 输入是JSON,直接返回原值(用于传递)
return {"valid": card_number}
# Remove whitespace from card number
card_number = card_number.replace(" ", "").replace("-", "")
# Check if card number is all digits
if not card_number.isdigit() or len(card_number) < 13:
return {"valid": "false"}
# Implement Luhn algorithm
total = 0
reverse_digits = card_number[::-1] # Reverse the number for easier processing
for i, digit in enumerate(reverse_digits):
num = int(digit)
if i % 2 == 1: # Every second digit starting from the right (which is first in reversed string)
num *= 2
if num > 9:
num -= 9 # Equivalent to sum of digits for 2-digit numbers
total += num
# Check if total is divisible by 10
is_valid = total % 10 == 0
return {"valid": "true" if is_valid else "false"}
except Exception as e:
raise RuntimeError(f"Error in luhn_check: {str(e)}")
def iban_validate(self, inputs):
# 参数检查
if not isinstance(inputs, dict):
raise TypeError("inputs must be a dictionary")
# 获取参数
iban = inputs.get("iban")
# 类型检查
if iban is not None and not isinstance(iban, str):
raise TypeError("iban must be a string")
# 检查必需参数
if iban is None:
raise ValueError("iban parameter is required")
try:
# Remove whitespace from IBAN
iban = iban.replace(" ", "")
# Basic length and format check
if not (len(iban) >= 15 and len(iban) <= 34):
return {"valid": "False"}
if not (iban[:2].isalpha() and iban[2:].isdigit()):
return {"valid": "False"}
# Move first four characters to the end
rearranged = iban[4:] + iban[:4]
# Replace letters with numbers (A=10, B=11, ..., Z=35)
numeric_iban = ''
for char in rearranged:
if char.isdigit():
numeric_iban += char
else:
numeric_iban += str(ord(char.upper()) - ord('A') + 10)
# Perform modulo 97 check
# Use step-by-step approach for very large numbers
remainder = 0
for char in numeric_iban:
remainder = (remainder * 10 + int(char)) % 97
is_valid = remainder == 1
return {"valid": "True" if is_valid else "False"}
except Exception as e:
raise RuntimeError(f"Error in iban_validate: {str(e)}")
def matter_synthesizer(self, inputs):
import hashlib
# 参数检查
if not isinstance(inputs, dict):
raise TypeError("inputs must be a dictionary")
# 获取参数
blueprint = inputs.get("blueprint")
# 类型检查
if blueprint is not None and not isinstance(blueprint, str):
raise TypeError("blueprint must be a string")
try:
# Get blueprint parameter as defined in interface
if blueprint is None:
blueprint = ""
# Generate a unique item ID based on the blueprint
# This ensures the same blueprint always produces the same item
blueprint_hash = hashlib.sha256(blueprint.encode()).hexdigest()[:16]
# Create a synthesized item with a deterministic ID based on blueprint
synthesized_item = f"synthesized_item_{blueprint_hash}"
return {"output_slot": synthesized_item}
except Exception as e:
raise RuntimeError(f"Error in matter_synthesizer: {str(e)}")
# --- Terminal Implementations ---
def terminal_1(self, inputs):
"""
terminal_1: Terminal 节点,根据输入参数的正确性返回对应的输出
逻辑:
- 只输入正确的 slot_a → 只返回 text_output
- 只输入正确的 slot_b → 只返回 hex_output
- 同时输入正确的 slot_a 和 slot_b → 返回 item_dispenser(物品掉落)
- 输入错误 → 返回错误信息
在离线 DAG 生成阶段,golden_outputs 存在但 golden_inputs 为空,直接返回所有 outputs。
在运行时评测阶段,golden_inputs 和 golden_outputs 都存在,根据输入验证返回对应输出。
"""
# 优先使用 _golden_*,如果不存在则回退到 _init_*
golden_outputs = inputs.get("_golden_outputs") or inputs.get("_init_outputs", {})
golden_inputs = inputs.get("_golden_inputs") or inputs.get("_init_inputs", {})
# 判断是否在 DAG 生成阶段(使用 _init_* 参数)
# DAG 生成阶段:直接返回所有 init_outputs,不进行参数验证
# 因为 init_inputs 可能只包含部分参数,其他参数从上游节点传递
is_dag_generation = "_init_outputs" in inputs and "_golden_outputs" not in inputs
if is_dag_generation:
return dict(golden_outputs)
# 评测阶段但没有 golden_inputs,也直接返回 outputs
if not golden_inputs:
return dict(golden_outputs)
# 运行时评测阶段:根据输入验证返回对应输出
slot_a = inputs.get("slot_a")
slot_b = inputs.get("slot_b")
# 检查哪些输入被提供了
has_a = slot_a is not None and slot_a != ""
has_b = slot_b is not None and slot_b != ""
# 验证输入值是否正确(与 golden values 比较)
a_valid = False
b_valid = False
if has_a and "slot_a" in golden_inputs:
expected_a = golden_inputs["slot_a"]
a_valid = str(slot_a) == str(expected_a)
if has_b and "slot_b" in golden_inputs:
expected_b = golden_inputs["slot_b"]
b_valid = str(slot_b) == str(expected_b)
# 根据验证结果返回对应的输出
result = {}
# 如果同时提供了两个输入且都正确 → 返回 item_dispenser
if has_a and has_b:
if a_valid and b_valid:
if "item_dispenser" in golden_outputs:
result["item_dispenser"] = golden_outputs["item_dispenser"]
return result
else:
# 至少有一个输入错误
incorrect_params = []
correct_params = []
if not a_valid:
incorrect_params.append("slot_a")
else:
correct_params.append("slot_a")
if not b_valid:
incorrect_params.append("slot_b")
else:
correct_params.append("slot_b")
error_msg = f"Input validation failed. Incorrect parameters: {', '.join(incorrect_params)}"
if correct_params:
error_msg += f". Correct parameters: {', '.join(correct_params)}"
return {"error": error_msg}
# 只提供 slot_a 且正确 → 返回 text_output
if has_a and not has_b:
if a_valid:
if "text_output" in golden_outputs:
result["text_output"] = golden_outputs["text_output"]
return result
else:
return {"error": "Input validation failed. Incorrect parameters: slot_a"}
# 只提供 slot_b 且正确 → 返回 hex_output
if has_b and not has_a:
if b_valid:
if "hex_output" in golden_outputs:
result["hex_output"] = golden_outputs["hex_output"]
return result
else:
return {"error": "Input validation failed. Incorrect parameters: slot_b"}
# 没有提供任何输入
return {"error": "No valid inputs provided"}
def terminal_2(self, inputs):
"""
terminal_2: Terminal 节点,根据输入参数的正确性返回对应的输出
逻辑:
- 只输入正确的 num1 或 num2(任一)→ 返回 text_result
- 同时输入正确的 num1 和 num2 → 返回 hex_result 和 math_token
- 输入错误 → 返回错误信息
在离线 DAG 生成阶段,golden_outputs 存在但 golden_inputs 为空,直接返回所有 outputs。
在运行时评测阶段,golden_inputs 和 golden_outputs 都存在,根据输入验证返回对应输出。
"""
# 优先使用 _golden_*,如果不存在则回退到 _init_*
golden_outputs = inputs.get("_golden_outputs") or inputs.get("_init_outputs", {})
golden_inputs = inputs.get("_golden_inputs") or inputs.get("_init_inputs", {})
# 判断是否在 DAG 生成阶段(使用 _init_* 参数)
# DAG 生成阶段:直接返回所有 init_outputs,不进行参数验证
# 因为 init_inputs 可能只包含部分参数,其他参数从上游节点传递
is_dag_generation = "_init_outputs" in inputs and "_golden_outputs" not in inputs
if is_dag_generation:
return dict(golden_outputs)
# 评测阶段但没有 golden_inputs,也直接返回 outputs
if not golden_inputs:
return dict(golden_outputs)
# 运行时评测阶段:根据输入验证返回对应输出
num1 = inputs.get("num1")
num2 = inputs.get("num2")
# 检查哪些输入被提供了
has_num1 = num1 is not None and num1 != ""
has_num2 = num2 is not None and num2 != ""
# 验证输入值是否正确(与 golden values 比较)
num1_valid = False
num2_valid = False
if has_num1 and "num1" in golden_inputs:
expected_num1 = golden_inputs["num1"]
num1_valid = str(num1) == str(expected_num1)
if has_num2 and "num2" in golden_inputs:
expected_num2 = golden_inputs["num2"]
num2_valid = str(num2) == str(expected_num2)
# 根据验证结果返回对应的输出
result = {}
# 如果同时提供了两个输入且都正确 → 返回 hex_result 和 math_token
if has_num1 and has_num2:
if num1_valid and num2_valid:
if "hex_result" in golden_outputs:
result["hex_result"] = golden_outputs["hex_result"]
if "math_token" in golden_outputs:
result["math_token"] = golden_outputs["math_token"]
return result
else:
# 至少有一个输入错误
incorrect_params = []
correct_params = []
if not num1_valid:
incorrect_params.append("num1")
else:
correct_params.append("num1")
if not num2_valid:
incorrect_params.append("num2")
else:
correct_params.append("num2")
error_msg = f"Input validation failed. Incorrect parameters: {', '.join(incorrect_params)}"
if correct_params:
error_msg += f". Correct parameters: {', '.join(correct_params)}"
return {"error": error_msg}
# 只提供一个输入且正确 → 返回 text_result
if (has_num1 or has_num2):
if (has_num1 and num1_valid) or (has_num2 and num2_valid):
if "text_result" in golden_outputs:
result["text_result"] = golden_outputs["text_result"]
return result
else:
if has_num1 and not num1_valid:
return {"error": "Input validation failed. Incorrect parameters: num1"}
if has_num2 and not num2_valid:
return {"error": "Input validation failed. Incorrect parameters: num2"}
# 没有提供任何输入
return {"error": "No valid inputs provided"}
def logic_terminal(self, inputs):
"""
Logic Terminal: 根据输入参数的正确性返回对应的输出
逻辑:
- 只输入正确的 slot_a → 返回 screen(部分输出)
- 只输入正确的 slot_b → 返回 screen 和 secondary_output
- 同时输入正确的 slot_a 和 slot_b → 返回 screen, secondary_output, item_dispenser
- 输入错误 → 返回错误信息
在离线 DAG 生成阶段,golden_outputs 存在但 golden_inputs 为空,直接返回所有 outputs。
在运行时评测阶段,golden_inputs 和 golden_outputs 都存在,根据输入验证返回对应输出。
"""
# 优先使用 _golden_*,如果不存在则回退到 _init_*
golden_outputs = inputs.get("_golden_outputs") or inputs.get("_init_outputs", {})
golden_inputs = inputs.get("_golden_inputs") or inputs.get("_init_inputs", {})
# 判断是否在 DAG 生成阶段(使用 _init_* 参数)
# DAG 生成阶段:直接返回所有 init_outputs,不进行参数验证
# 因为 init_inputs 可能只包含部分参数,其他参数从上游节点传递
is_dag_generation = "_init_outputs" in inputs and "_golden_outputs" not in inputs
if is_dag_generation:
return dict(golden_outputs)
# 评测阶段但没有 golden_inputs,也直接返回 outputs
if not golden_inputs:
return dict(golden_outputs)
# 运行时评测阶段:根据输入验证返回对应输出
slot_a = inputs.get("slot_a")
slot_b = inputs.get("slot_b")
has_a = slot_a is not None and slot_a != ""
has_b = slot_b is not None and slot_b != ""
# 验证输入值(与 golden values 比较)
a_valid = False
b_valid = False
if has_a and "slot_a" in golden_inputs:
a_valid = str(slot_a) == str(golden_inputs["slot_a"])
if has_b and "slot_b" in golden_inputs:
b_valid = str(slot_b) == str(golden_inputs["slot_b"])
result = {}
# 同时提供两个输入且都正确 → 返回所有输出(包括 item_dispenser)
if has_a and has_b:
if a_valid and b_valid:
if "screen" in golden_outputs:
result["screen"] = golden_outputs["screen"]
if "secondary_output" in golden_outputs:
result["secondary_output"] = golden_outputs["secondary_output"]
if "item_dispenser" in golden_outputs:
result["item_dispenser"] = golden_outputs["item_dispenser"]
return result
else:
incorrect_params = []
correct_params = []
if not a_valid:
incorrect_params.append("slot_a")
else:
correct_params.append("slot_a")
if not b_valid:
incorrect_params.append("slot_b")
else:
correct_params.append("slot_b")
error_msg = f"Input validation failed. Incorrect parameters: {', '.join(incorrect_params)}"
if correct_params:
error_msg += f". Correct parameters: {', '.join(correct_params)}"
return {"error": error_msg}
# 只提供 slot_a 且正确 → 返回 screen(部分输出)
if has_a and not has_b:
if a_valid:
if "screen" in golden_outputs:
result["screen"] = golden_outputs["screen"]
return result
else:
return {"error": "Input validation failed. Incorrect parameters: slot_a"}
# 只提供 slot_b 且正确 → 返回 screen 和 secondary_output
if has_b and not has_a:
if b_valid:
if "screen" in golden_outputs:
result["screen"] = golden_outputs["screen"]
if "secondary_output" in golden_outputs:
result["secondary_output"] = golden_outputs["secondary_output"]
return result
else:
return {"error": "Input validation failed. Incorrect parameters: slot_b"}
# 没有提供任何输入
return {"error": "No valid inputs provided"}
def math_terminal(self, inputs):
"""
Math Terminal: 根据输入参数的正确性返回对应的输出
逻辑:
- 只输入正确的 num1 或 num2(任一)→ 返回 result_display
- 同时输入正确的 num1 和 num2 → 返回 result_display, hex_result, math_token
- 输入错误 → 返回错误信息
在离线 DAG 生成阶段,golden_outputs 存在但 golden_inputs 为空,直接返回所有 outputs。
在运行时评测阶段,golden_inputs 和 golden_outputs 都存在,根据输入验证返回对应输出。
"""
# 优先使用 _golden_*,如果不存在则回退到 _init_*
golden_outputs = inputs.get("_golden_outputs") or inputs.get("_init_outputs", {})
golden_inputs = inputs.get("_golden_inputs") or inputs.get("_init_inputs", {})
# 判断是否在 DAG 生成阶段(使用 _init_* 参数)
# DAG 生成阶段:直接返回所有 init_outputs,不进行参数验证
# 因为 init_inputs 可能只包含部分参数,其他参数从上游节点传递
is_dag_generation = "_init_outputs" in inputs and "_golden_outputs" not in inputs
if is_dag_generation:
return dict(golden_outputs)
# 评测阶段但没有 golden_inputs,也直接返回 outputs
if not golden_inputs:
return dict(golden_outputs)
# 运行时评测阶段:根据输入验证返回对应输出
num1 = inputs.get("num1")
num2 = inputs.get("num2")
has_num1 = num1 is not None and num1 != ""
has_num2 = num2 is not None and num2 != ""
# 验证输入值(与 golden values 比较)
num1_valid = False
num2_valid = False
if has_num1 and "num1" in golden_inputs:
num1_valid = str(num1) == str(golden_inputs["num1"])
if has_num2 and "num2" in golden_inputs:
num2_valid = str(num2) == str(golden_inputs["num2"])
result = {}
# 同时提供两个输入且都正确 → 返回所有输出
if has_num1 and has_num2:
if num1_valid and num2_valid:
if "result_display" in golden_outputs:
result["result_display"] = golden_outputs["result_display"]
if "hex_result" in golden_outputs:
result["hex_result"] = golden_outputs["hex_result"]
if "math_token" in golden_outputs:
result["math_token"] = golden_outputs["math_token"]
return result
else:
incorrect_params = []
correct_params = []
if not num1_valid:
incorrect_params.append("num1")
else:
correct_params.append("num1")
if not num2_valid:
incorrect_params.append("num2")
else:
correct_params.append("num2")
error_msg = f"Input validation failed. Incorrect parameters: {', '.join(incorrect_params)}"
if correct_params:
error_msg += f". Correct parameters: {', '.join(correct_params)}"
return {"error": error_msg}
# 只提供一个输入且正确 → 返回 result_display
if (has_num1 or has_num2):
if (has_num1 and num1_valid) or (has_num2 and num2_valid):
if "result_display" in golden_outputs:
result["result_display"] = golden_outputs["result_display"]
return result
else:
if has_num1 and not num1_valid:
return {"error": "Input validation failed. Incorrect parameters: num1"}
if has_num2 and not num2_valid:
return {"error": "Input validation failed. Incorrect parameters: num2"}
# 没有提供任何输入
return {"error": "No valid inputs provided"}
def crypt_terminal(self, inputs):
"""
Encryption Terminal: 根据输入参数的正确性返回对应的输出
逻辑:
- 只输入正确的 plaintext → 返回 status(部分输出)
- 同时输入正确的 plaintext 和 key → 返回 status, encrypted_hex, encryption_chip
- 输入错误 → 返回错误信息
在离线 DAG 生成阶段,golden_outputs 存在但 golden_inputs 为空,直接返回所有 outputs。
在运行时评测阶段,golden_inputs 和 golden_outputs 都存在,根据输入验证返回对应输出。
"""
# 优先使用 _golden_*,如果不存在则回退到 _init_*
golden_outputs = inputs.get("_golden_outputs") or inputs.get("_init_outputs", {})
golden_inputs = inputs.get("_golden_inputs") or inputs.get("_init_inputs", {})
# 判断是否在 DAG 生成阶段(使用 _init_* 参数)
# DAG 生成阶段:直接返回所有 init_outputs,不进行参数验证
# 因为 init_inputs 可能只包含部分参数,其他参数从上游节点传递
is_dag_generation = "_init_outputs" in inputs and "_golden_outputs" not in inputs
if is_dag_generation:
return dict(golden_outputs)
# 评测阶段但没有 golden_inputs,也直接返回 outputs
if not golden_inputs:
return dict(golden_outputs)
# 运行时评测阶段:根据输入验证返回对应输出
plaintext = inputs.get("plaintext")
key = inputs.get("key")
has_plaintext = plaintext is not None and plaintext != ""
has_key = key is not None and key != ""
# 验证输入值(与 golden values 比较)
plaintext_valid = False
key_valid = False
if has_plaintext and "plaintext" in golden_inputs:
plaintext_valid = str(plaintext) == str(golden_inputs["plaintext"])
if has_key and "key" in golden_inputs:
key_valid = str(key) == str(golden_inputs["key"])
result = {}
# 同时提供两个输入且都正确 → 返回所有输出
if has_plaintext and has_key:
if plaintext_valid and key_valid:
if "status" in golden_outputs:
result["status"] = golden_outputs["status"]
if "encrypted_hex" in golden_outputs:
result["encrypted_hex"] = golden_outputs["encrypted_hex"]
if "encryption_chip" in golden_outputs:
result["encryption_chip"] = golden_outputs["encryption_chip"]
return result
else:
incorrect_params = []
correct_params = []
if not plaintext_valid:
incorrect_params.append("plaintext")
else:
correct_params.append("plaintext")
if not key_valid:
incorrect_params.append("key")
else:
correct_params.append("key")
error_msg = f"Input validation failed. Incorrect parameters: {', '.join(incorrect_params)}"
if correct_params:
error_msg += f". Correct parameters: {', '.join(correct_params)}"
return {"error": error_msg}
# 只提供 plaintext 且正确 → 返回 status(部分输出)
if has_plaintext and not has_key:
if plaintext_valid:
if "status" in golden_outputs:
result["status"] = golden_outputs["status"]
return result
else:
return {"error": "Input validation failed. Incorrect parameters: plaintext"}
# 只提供 key 但没有 plaintext
if has_key and not has_plaintext:
return {"error": "plaintext required"}
# 没有提供任何输入
return {"error": "No valid inputs provided"}
def sequence_terminal(self, inputs):
"""
Sequence Terminal: 根据输入参数的正确性返回对应的输出
逻辑:
- 只输入正确的 sequence → 返回 verification(部分输出)
- 同时输入正确的 sequence 和 checksum → 返回 verification, auth_code, access_card
- 输入错误 → 返回错误信息
在离线 DAG 生成阶段,golden_outputs 存在但 golden_inputs 为空,直接返回所有 outputs。
在运行时评测阶段,golden_inputs 和 golden_outputs 都存在,根据输入验证返回对应输出。
"""
# 优先使用 _golden_*,如果不存在则回退到 _init_*
golden_outputs = inputs.get("_golden_outputs") or inputs.get("_init_outputs", {})
golden_inputs = inputs.get("_golden_inputs") or inputs.get("_init_inputs", {})
# 判断是否在 DAG 生成阶段(使用 _init_* 参数)
# DAG 生成阶段:直接返回所有 init_outputs,不进行参数验证
# 因为 init_inputs 可能只包含部分参数,其他参数从上游节点传递
is_dag_generation = "_init_outputs" in inputs and "_golden_outputs" not in inputs
if is_dag_generation:
return dict(golden_outputs)
# 评测阶段但没有 golden_inputs,也直接返回 outputs
if not golden_inputs:
return dict(golden_outputs)
# 运行时评测阶段:根据输入验证返回对应输出
sequence = inputs.get("sequence")
checksum = inputs.get("checksum")
has_sequence = sequence is not None and sequence != ""
has_checksum = checksum is not None and checksum != ""
# 验证输入值(与 golden values 比较)
sequence_valid = False
checksum_valid = False
if has_sequence and "sequence" in golden_inputs:
sequence_valid = str(sequence) == str(golden_inputs["sequence"])
if has_checksum and "checksum" in golden_inputs:
checksum_valid = str(checksum) == str(golden_inputs["checksum"])
result = {}
# 同时提供两个输入且都正确 → 返回所有输出
if has_sequence and has_checksum:
if sequence_valid and checksum_valid:
if "verification" in golden_outputs:
result["verification"] = golden_outputs["verification"]
if "auth_code" in golden_outputs:
result["auth_code"] = golden_outputs["auth_code"]
if "access_card" in golden_outputs:
result["access_card"] = golden_outputs["access_card"]
return result
else:
incorrect_params = []
correct_params = []
if not sequence_valid:
incorrect_params.append("sequence")
else:
correct_params.append("sequence")
if not checksum_valid:
incorrect_params.append("checksum")
else:
correct_params.append("checksum")
error_msg = f"Input validation failed. Incorrect parameters: {', '.join(incorrect_params)}"
if correct_params:
error_msg += f". Correct parameters: {', '.join(correct_params)}"
return {"error": error_msg}
# 只提供 sequence 且正确 → 返回 verification(部分输出)
if has_sequence and not has_checksum:
if sequence_valid:
if "verification" in golden_outputs:
result["verification"] = golden_outputs["verification"]
return result
else:
return {"error": "Input validation failed. Incorrect parameters: sequence"}
# 只提供 checksum 但没有 sequence
if has_checksum and not has_sequence:
return {"error": "sequence required"}
# 没有提供任何输入
return {"error": "No valid inputs provided"}
# --- Additional File Tools ---
def zip_unzip_file(self, inputs):
"""ZIP 解压文件工具(模拟)
在 DAG 生成阶段,直接返回 init_outputs,不验证输入。
在运行时评测阶段,验证输入并返回对应输出。
"""
# 优先使用 _golden_*,如果不存在则回退到 _init_*
golden_outputs = inputs.get("_golden_outputs") or inputs.get("_init_outputs", {})
golden_inputs = inputs.get("_golden_inputs") or inputs.get("_init_inputs", {})
# 判断是否在 DAG 生成阶段
is_dag_generation = "_init_outputs" in inputs and "_golden_outputs" not in inputs
if is_dag_generation:
return dict(golden_outputs)
# 评测阶段但没有 golden_inputs,也直接返回 outputs
if not golden_inputs:
return dict(golden_outputs)
# 运行时评测阶段:验证输入
zip_id = inputs.get("zip_id")
password = inputs.get("password")
has_zip_id = zip_id is not None and zip_id != ""
has_password = password is not None and password != ""
# 验证输入值(与 golden values 比较)
zip_id_valid = False
password_valid = False
if has_zip_id and "zip_id" in golden_inputs:
zip_id_valid = str(zip_id) == str(golden_inputs["zip_id"])
if has_password and "password" in golden_inputs:
password_valid = str(password) == str(golden_inputs["password"])
# 两个输入都提供且都正确 → 返回所有输出
if has_zip_id and has_password:
if zip_id_valid and password_valid:
return dict(golden_outputs)
else:
incorrect_params = []
correct_params = []
if not zip_id_valid:
incorrect_params.append("zip_id")
else:
correct_params.append("zip_id")
if not password_valid:
incorrect_params.append("password")
else:
correct_params.append("password")
error_msg = f"Input validation failed. Incorrect parameters: {', '.join(incorrect_params)}"
if correct_params:
error_msg += f". Correct parameters: {', '.join(correct_params)}"
return {"error": error_msg}
# 没有提供完整输入
missing = []
if not has_zip_id:
missing.append("zip_id")
if not has_password:
missing.append("password")
return {"error": "Missing required inputs: " + ", ".join(missing)}
def zip_unzip_item(self, inputs):
"""ZIP 解压道具工具(模拟)
在 DAG 生成阶段,直接返回 init_outputs,不验证输入。
在运行时评测阶段,验证输入并返回对应输出。
"""
# 优先使用 _golden_*,如果不存在则回退到 _init_*
golden_outputs = inputs.get("_golden_outputs") or inputs.get("_init_outputs", {})
golden_inputs = inputs.get("_golden_inputs") or inputs.get("_init_inputs", {})
# 判断是否在 DAG 生成阶段
is_dag_generation = "_init_outputs" in inputs and "_golden_outputs" not in inputs
if is_dag_generation:
return dict(golden_outputs)
# 评测阶段但没有 golden_inputs,也直接返回 outputs
if not golden_inputs:
return dict(golden_outputs)
# 运行时评测阶段:验证输入
zip_id = inputs.get("zip_id")
password = inputs.get("password")
has_zip_id = zip_id is not None and zip_id != ""
has_password = password is not None and password != ""
# 验证输入值(与 golden values 比较)
zip_id_valid = False
password_valid = False
if has_zip_id and "zip_id" in golden_inputs:
zip_id_valid = str(zip_id) == str(golden_inputs["zip_id"])
if has_password and "password" in golden_inputs:
password_valid = str(password) == str(golden_inputs["password"])
# 两个输入都提供且都正确 → 返回所有输出
if has_zip_id and has_password:
if zip_id_valid and password_valid:
return dict(golden_outputs)
else:
incorrect_params = []
correct_params = []
if not zip_id_valid:
incorrect_params.append("zip_id")
else:
correct_params.append("zip_id")
if not password_valid:
incorrect_params.append("password")
else:
correct_params.append("password")
error_msg = f"Input validation failed. Incorrect parameters: {', '.join(incorrect_params)}"
if correct_params:
error_msg += f". Correct parameters: {', '.join(correct_params)}"
return {"error": error_msg}
# 没有提供完整输入
missing = []
if not has_zip_id:
missing.append("zip_id")
if not has_password:
missing.append("password")
return {"error": "Missing required inputs: " + ", ".join(missing)}
def code_py_exec(self, inputs):
"""Python 代码执行工具
这是一个真实执行的工具,在 DAG 生成阶段和运行时评测阶段都会执行代码。
输入: code (Python 代码字符串)
输出: stdout (标准输出,包含执行结果和错误信息)
特殊行为:
- 如果代码定义了 main() 函数,会自动调用它
- 如果执行出错,错误信息会包含在 stdout 中
"""
code = inputs.get("code", "")
if not code:
return {"stdout": "Error: No code provided"}
try:
# 创建一个受限的执行环境
import io
import sys
# 捕获标准输出和标准错误
old_stdout = sys.stdout
old_stderr = sys.stderr
sys.stdout = io.StringIO()
sys.stderr = io.StringIO()
# 执行代码
exec_globals = {"__builtins__": __builtins__}
exec_locals = {}
exec(code, exec_globals, exec_locals)
# 如果定义了 main() 函数,自动调用它
if 'main' in exec_locals and callable(exec_locals['main']):
exec_locals['main']()
# 获取输出
stdout_value = sys.stdout.getvalue()
stderr_value = sys.stderr.getvalue()
sys.stdout = old_stdout
sys.stderr = old_stderr
# 合并 stdout 和 stderr
output = stdout_value
if stderr_value:
output += ("\n" if output else "") + f"[stderr] {stderr_value}"
# 如果没有任何输出,尝试返回 result 变量的值
if not output:
result = exec_locals.get("_result") or exec_locals.get("result")
if result is not None:
output = str(result)
return {"stdout": output if output else "(no output)"}
except Exception as e:
# 恢复标准输出和错误输出
sys.stdout = old_stdout
sys.stderr = old_stderr
# 返回错误信息
return {"stdout": f"Error: {type(e).__name__}: {str(e)}"} |