Spaces:
Running
Running
File size: 123,549 Bytes
c509967 | 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 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 | """Main chat interface page for ChemGraph."""
import asyncio
import html
import json
import logging
import os
import pprint
import queue
import re
import threading
import uuid
from datetime import datetime
from pathlib import Path
from typing import Any, Dict, Optional
import pandas as pd
import streamlit as st
from ase.io import read as ase_read
from chemgraph.agent.llm_agent import HumanInputRequired
from chemgraph.memory.schemas import SessionMessage
from chemgraph.memory.store import SessionStore
from chemgraph.models.supported_models import supported_argo_models
from chemgraph.schemas.ase_input import (
get_available_calculator_names,
get_default_calculator_name,
)
from chemgraph.utils.config_utils import (
get_argo_user_from_nested_config,
get_base_url_for_model_from_nested_config,
)
from chemgraph.utils.tool_mapping import (
format_validation_failure_message,
validate_completion,
)
from ui.agent_manager import initialize_agent
from ui.branding import LOGO_IMAGES, first_existing_asset
from ui.config import load_config
from ui.endpoint import check_local_model_endpoint
from ui.file_utils import (
extract_log_dir_from_messages,
find_latest_xyz_file_in_dir,
)
from ui.message_utils import (
extract_messages_from_result,
extract_molecular_structure,
extract_xyz_from_report_html,
find_html_filename,
find_structure_in_messages,
has_structure_signal,
is_infrared_requested,
normalize_latex_delimiters,
normalize_message_content,
split_markdown_latex_blocks,
strip_viewer_from_report_html,
)
from ui.scientific_reminders import (
QUERY_SPECIFICATION_HINT,
render_chat_scientific_reminder,
)
from ui.session_utils import (
conversation_entry_to_messages,
generate_session_id,
messages_from_result,
session_to_conversation_history,
)
from ui.state import init_session_state
from ui.visualization import (
STMOL_AVAILABLE,
display_molecular_structure,
visualize_trajectory,
)
# Re-use the constants from the configuration page
from ui._pages.configuration import normalize_workflow_name
logger = logging.getLogger(__name__)
_AGENT_RUN_LOCK = threading.Lock()
# ---------------------------------------------------------------------------
# Thin wrappers around config utilities
# ---------------------------------------------------------------------------
def _get_base_url_for_model(model_name: str, config: Dict[str, Any]) -> Optional[str]:
"""Resolve the configured base URL for a model.
Parameters
----------
model_name : str
Selected model identifier.
config : dict[str, Any]
Nested UI configuration.
Returns
-------
str or None
Provider base URL, or ``None`` when not configured.
"""
return get_base_url_for_model_from_nested_config(model_name, config)
def _initial_ui_log_root() -> str:
"""Return the root directory for per-chat UI artifacts."""
env_log_dir = os.environ.get("CHEMGRAPH_LOG_DIR")
if env_log_dir:
return str(_ui_log_root_from_path(Path(env_log_dir).expanduser()))
return str((Path.cwd() / "cg_logs").resolve())
def _ui_log_root_from_path(path: Path) -> Path:
"""Return the chat-log root even when given a stale session/query path."""
current = path.resolve()
while current.name.startswith(("query_", "session_", "ui_session_")):
current = current.parent
return current
def _ensure_chat_log_dir() -> str:
"""Create and activate a log directory owned by the current chat."""
if st.session_state.get("ui_log_root"):
st.session_state.ui_log_root = str(
_ui_log_root_from_path(Path(st.session_state.ui_log_root).expanduser())
)
else:
st.session_state.ui_log_root = _initial_ui_log_root()
chat_log_dir = st.session_state.get("current_chat_log_dir")
if chat_log_dir:
chat_path = Path(chat_log_dir).expanduser().resolve()
root_path = Path(st.session_state.ui_log_root).expanduser().resolve()
if chat_path.parent != root_path:
st.session_state.current_chat_log_dir = None
chat_log_dir = None
if not chat_log_dir:
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
suffix = str(uuid.uuid4())[:8]
chat_log_dir = str(
Path(st.session_state.ui_log_root) / f"ui_session_{timestamp}_{suffix}"
)
st.session_state.current_chat_log_dir = chat_log_dir
os.makedirs(chat_log_dir, exist_ok=True)
os.environ["CHEMGRAPH_LOG_DIR"] = chat_log_dir
return chat_log_dir
def _create_query_log_dir() -> str:
"""Create a per-query artifact directory under the active chat directory."""
chat_log_dir = _ensure_chat_log_dir()
query_idx = len(st.session_state.get("conversation_history", [])) + 1
suffix = str(uuid.uuid4())[:8]
query_log_dir = str(Path(chat_log_dir) / f"query_{query_idx:03d}_{suffix}")
os.makedirs(query_log_dir, exist_ok=True)
st.session_state.active_query_log_dir = query_log_dir
return query_log_dir
def _begin_agent_run(*, from_pending: bool = False) -> bool:
"""Mark an agent run as active if no other run is in progress."""
if st.session_state.get("agent_running") and not from_pending:
return False
if st.session_state.get("agent_run_lock_acquired"):
return True
if _AGENT_RUN_LOCK.locked():
return False
acquired = _AGENT_RUN_LOCK.acquire(blocking=False)
if not acquired:
return False
st.session_state.agent_running = True
st.session_state.agent_run_lock_acquired = True
return True
def _agent_run_active() -> bool:
"""Return whether any Streamlit session has an active workflow run."""
lock_active = _AGENT_RUN_LOCK.locked()
has_pending_submission = st.session_state.get("pending_agent_submission") is not None
if (
st.session_state.get("agent_running")
and not lock_active
and not has_pending_submission
):
# A browser refresh creates a fresh Streamlit session_state while the
# process lock is the authoritative cross-session signal.
st.session_state.agent_running = False
st.session_state.agent_run_lock_acquired = False
return bool(lock_active or st.session_state.get("agent_running"))
def _end_agent_run() -> None:
"""Clear the active-run marker and release the process-local run lock."""
st.session_state.agent_running = False
st.session_state.active_query_log_dir = None
if st.session_state.get("agent_run_lock_acquired"):
st.session_state.agent_run_lock_acquired = False
try:
_AGENT_RUN_LOCK.release()
except RuntimeError:
pass
def _session_id_from_url() -> Optional[str]:
"""Read the session id from the URL query string when present."""
try:
value = st.query_params.get("session")
except Exception:
return None
if isinstance(value, list):
return str(value[0]) if value else None
return str(value) if value else None
def _set_session_id_in_url(session_id: Optional[str]) -> None:
"""Keep the active session id in the browser URL for refresh recovery."""
try:
if session_id:
st.query_params["session"] = session_id
else:
st.query_params.pop("session", None)
except Exception:
pass
def _restore_agent_log_context(agent, old_agent_log_dir, old_env_log_dir) -> None:
"""Restore agent/env log state after a per-query run."""
if agent is not None:
agent.log_dir = old_agent_log_dir
if st.session_state.get("current_chat_log_dir"):
os.environ["CHEMGRAPH_LOG_DIR"] = st.session_state.current_chat_log_dir
elif old_env_log_dir is not None:
old_path = Path(old_env_log_dir).expanduser()
if old_path.name.startswith("query_"):
os.environ["CHEMGRAPH_LOG_DIR"] = str(_ui_log_root_from_path(old_path))
else:
os.environ["CHEMGRAPH_LOG_DIR"] = old_env_log_dir
else:
os.environ.pop("CHEMGRAPH_LOG_DIR", None)
def _resolve_structured_output_for_model(
model_name: str, structured_output: bool
) -> tuple[bool, Optional[str]]:
"""Disable structured output for Argo models, including quick overrides.
Parameters
----------
model_name : str
Selected model identifier.
structured_output : bool
Requested structured-output setting.
Returns
-------
tuple[bool, str | None]
Effective structured-output setting and optional warning message.
"""
if model_name in supported_argo_models and structured_output:
return (
False,
"Structured output is disabled for Argo models to avoid JSON parsing errors.",
)
return structured_output, None
# ---------------------------------------------------------------------------
# Page entry point
# ---------------------------------------------------------------------------
def render() -> None:
"""Render the Main Interface page."""
init_session_state()
_restore_session_from_url()
config = st.session_state.config
selected_model = config["general"]["model"]
selected_workflow = normalize_workflow_name(config["general"]["workflow"])
selected_output = config["general"]["output"]
structured_output = config["general"]["structured"]
generate_report = config["general"]["report"]
human_supervised = config["general"].get("human_supervised", False)
thread_id = config["general"]["thread"]
# ----- Header -----
logo_image = first_existing_asset(LOGO_IMAGES)
if logo_image:
st.image(logo_image, width=320)
else:
st.title("\U0001f9ea ChemGraph")
st.markdown("""
ChemGraph enables you to perform various **computational chemistry** tasks with
natural-language queries using AI agents.
""")
# ----- Calculator availability sidebar -----
_render_available_calculators_sidebar()
_render_chat_controls()
structured_output, ui_notice = _resolve_structured_output_for_model(
selected_model, structured_output
)
st.session_state.ui_notice = ui_notice
st.session_state.active_model = selected_model
st.session_state.active_workflow = selected_workflow
if ui_notice:
st.info(ui_notice)
render_chat_scientific_reminder()
selected_base_url = _get_base_url_for_model(selected_model, config)
endpoint_status = check_local_model_endpoint(selected_base_url)
# ----- Session management sidebar -----
_render_session_sidebar()
# Reload config button
if st.sidebar.button("\U0001f504 Reload Config", disabled=_agent_run_active()):
st.session_state.config = load_config()
st.success("\u2705 Configuration reloaded!")
st.rerun()
# ----- Auto-initialize agent -----
_auto_initialize_agent(
config,
selected_model,
selected_workflow,
structured_output,
selected_output,
generate_report,
human_supervised,
selected_base_url,
)
# ----- Agent status sidebar -----
_render_agent_status(selected_model, selected_workflow, thread_id, endpoint_status)
# ----- Restore pending clarification from the visible history -----
_restore_pending_interrupt_from_history()
# ----- Conversation history -----
_render_conversation_history(thread_id)
# ----- Pending interrupt display -----
_render_pending_interrupt()
# ----- Example queries -----
_render_example_queries(config, selected_model)
# ----- Chat input (handles both normal queries and interrupt responses) -----
is_interrupt = st.session_state.pending_human_question is not None
agent_busy = _agent_run_active()
if agent_busy:
st.info("Agent is running. Please wait for the current task to finish.")
prompt = st.chat_input(
(
"Type your response..."
if is_interrupt
else "Ask: molecule/reaction + property + calculator + conditions..."
),
disabled=agent_busy,
)
# Check for example query submitted via button click
if agent_busy:
st.session_state.pop("_pending_example_query", None)
prompt = None
else:
example_query = st.session_state.pop("_pending_example_query", None)
if example_query:
prompt = example_query
if prompt:
if is_interrupt:
_handle_human_response(prompt, thread_id)
else:
_queue_query_submission(
prompt, thread_id, endpoint_status, selected_base_url
)
_execute_pending_agent_submission(thread_id, endpoint_status, selected_base_url)
# ---------------------------------------------------------------------------
# Internal renderers
# ---------------------------------------------------------------------------
def _render_markdown_with_math(text: str) -> None:
"""Render Markdown text, sending display math blocks through ``st.latex``.
Parameters
----------
text : str
Markdown text that may contain display math blocks.
"""
for block_type, content in split_markdown_latex_blocks(text):
if block_type == "latex":
st.latex(_prepare_latex_block(content))
else:
st.markdown(content)
def _prepare_latex_block(content: str) -> str:
"""Clean display math for Streamlit's KaTeX renderer.
Parameters
----------
content : str
Raw LaTeX block content.
Returns
-------
str
KaTeX-compatible display math.
"""
lines = [line.strip() for line in content.splitlines() if line.strip()]
if not lines:
return ""
if len(lines) == 1 or r"\begin{" in content:
return " ".join(lines)
return "\\begin{aligned}\n" + (r" \\" + "\n").join(lines) + "\n\\end{aligned}"
def _format_calculator_label(calculator_name: str) -> str:
"""Format calculator class names for display.
Parameters
----------
calculator_name : str
Calculator class name or label.
Returns
-------
str
Human-readable calculator label.
"""
label = calculator_name.removesuffix("Calc")
if label == "TBLite":
return "TBLite (xTB, GFN1-xTB, GFN2-xTB)"
return label
def _render_available_calculators_sidebar() -> None:
"""Render the calculators detected during ChemGraph initialization."""
available = get_available_calculator_names()
default = get_default_calculator_name()
with st.sidebar.expander("\U0001f9ee Available Calculators", expanded=True):
st.caption("Detected during ChemGraph initialization.")
for calculator_name in available:
label = _format_calculator_label(calculator_name)
if calculator_name == default:
st.success(f"{label} (default)")
else:
st.markdown(f"- {label}")
st.caption(
"The agent uses this list when choosing calculators for ASE simulations. "
"Availability does not imply suitability; verify calculator domain, "
"charge/spin, and convergence settings for your system."
)
def _start_new_chat() -> None:
"""Reset conversation state for a fresh chat session."""
if st.session_state.get("agent_run_lock_acquired"):
try:
_AGENT_RUN_LOCK.release()
except RuntimeError:
pass
st.session_state.conversation_history.clear()
st.session_state.current_session_id = None
st.session_state.session_created = False
st.session_state.query_input = ""
st.session_state.last_run_error = None
st.session_state.last_run_result = None
st.session_state.last_run_query = None
st.session_state.pop("_pending_example_query", None)
st.session_state.agent = None
st.session_state.last_config = None
st.session_state.current_chat_log_dir = None
st.session_state.active_query_log_dir = None
st.session_state.active_graph_thread_id = None
st.session_state.agent_running = False
st.session_state.agent_run_lock_acquired = False
st.session_state.pending_agent_submission = None
_set_session_id_in_url(None)
os.environ.pop("CHEMGRAPH_LOG_DIR", None)
_clear_interrupt_state()
def _ensure_graph_thread_id(thread_id: int) -> str:
"""Return a stable LangGraph thread ID for the current chat.
The thread must persist across follow-up questions inside one chat so the
agent can resolve references like "its dipole moment". It must also change
when the user starts a new chat so old molecule/tool context does not leak
into an unrelated task.
"""
active_id = st.session_state.get("active_graph_thread_id")
if not active_id:
active_id = f"{thread_id}-{uuid.uuid4().hex[:8]}"
st.session_state.active_graph_thread_id = active_id
return active_id
def _render_chat_controls() -> None:
"""Render chat-level actions that must be available even without memory."""
if st.sidebar.button(
"New Chat",
key="new_chat_btn",
use_container_width=True,
disabled=_agent_run_active(),
):
_start_new_chat()
st.rerun()
def _render_session_sidebar() -> None:
"""Render the session management panel in the sidebar."""
store: Optional[SessionStore] = st.session_state.get("session_store")
if store is None:
return
with st.sidebar.expander("\U0001f4c2 Sessions", expanded=False):
# Show current session info
current_sid = st.session_state.get("current_session_id")
if current_sid:
st.caption(f"Active session: `{current_sid}`")
# List recent sessions
try:
sessions = store.list_sessions(limit=10)
except Exception:
sessions = []
if not sessions:
st.caption("No saved sessions yet.")
return
st.markdown("**Recent sessions:**")
for s in sessions:
# Highlight the active session
is_active = current_sid and s.session_id == current_sid
prefix = "\u25b6 " if is_active else ""
label = s.title or "Untitled"
if len(label) > 35:
label = label[:32] + "..."
col_load, col_del = st.columns([4, 1])
with col_load:
if st.button(
f"{prefix}{label}",
key=f"load_session_{s.session_id}",
use_container_width=True,
help=(
f"Model: {s.model_name} | "
f"Queries: {s.query_count} | "
f"{s.updated_at.strftime('%Y-%m-%d %H:%M')}"
),
):
_load_session(s.session_id)
st.rerun()
with col_del:
if st.button(
"\U0001f5d1",
key=f"del_session_{s.session_id}",
help="Delete this session",
):
try:
store.delete_session(s.session_id)
# If we just deleted the active session, reset
if current_sid == s.session_id:
_start_new_chat()
except Exception as exc:
logger.warning(
"Failed to delete session %s: %s", s.session_id, exc
)
st.rerun()
def _load_session(session_id: str) -> None:
"""Load a stored session into the active conversation.
Parameters
----------
session_id : str
Session ID or prefix selected in the sidebar.
"""
store: Optional[SessionStore] = st.session_state.get("session_store")
if store is None:
return
session = store.get_session(session_id)
if session is None:
st.sidebar.error(f"Session '{session_id}' not found.")
return
# Rebuild conversation_history from stored messages
st.session_state.conversation_history = session_to_conversation_history(session)
st.session_state.current_session_id = session.session_id
st.session_state.session_created = True
st.session_state.query_input = ""
st.session_state.last_run_error = None
st.session_state.last_run_result = None
st.session_state.last_run_query = None
st.session_state.current_chat_log_dir = session.log_dir
st.session_state.active_graph_thread_id = f"session-{session.session_id}"
_set_session_id_in_url(session.session_id)
if session.log_dir:
os.environ["CHEMGRAPH_LOG_DIR"] = session.log_dir
def _restore_session_from_url() -> None:
"""Restore the visible chat after a browser refresh using URL state."""
if st.session_state.get("current_session_id"):
return
session_id = _session_id_from_url()
if not session_id:
return
try:
_load_session(session_id)
except Exception as exc:
logger.warning("Failed to restore session %s from URL: %s", session_id, exc)
def _active_session_metadata() -> tuple[str, str]:
"""Return model/workflow metadata matching the active UI run."""
config = st.session_state.config
model = (
st.session_state.get("pending_interrupt_model")
or st.session_state.get("active_model")
or config["general"]["model"]
)
workflow = (
st.session_state.get("pending_interrupt_workflow")
or st.session_state.get("active_workflow")
or config["general"]["workflow"]
)
return model, normalize_workflow_name(workflow)
def _ensure_ui_session(query: str) -> Optional[str]:
"""Create the UI session-store row if needed and return its id."""
store: Optional[SessionStore] = st.session_state.get("session_store")
if store is None:
return None
model, workflow = _active_session_metadata()
if not st.session_state.session_created:
sid = st.session_state.get("current_session_id") or generate_session_id()
st.session_state.current_session_id = sid
title = SessionStore.generate_title(query)
store.create_session(
session_id=sid,
model_name=model,
workflow_type=workflow,
title=title,
log_dir=st.session_state.get("current_chat_log_dir"),
)
st.session_state.session_created = True
_set_session_id_in_url(st.session_state.current_session_id)
return st.session_state.current_session_id
def _save_query_to_store(query: str) -> bool:
"""Persist the human query immediately so refresh does not hide it."""
store: Optional[SessionStore] = st.session_state.get("session_store")
if store is None:
return False
try:
session_id = _ensure_ui_session(query)
if session_id:
store.save_messages(
session_id,
[SessionMessage(role="human", content=query)],
)
return True
except Exception as exc:
logger.warning("Failed to save pending query to session store: %s", exc)
return False
def _save_exchange_to_store(
query: str,
result: Any,
*,
include_query: bool = True,
) -> None:
"""Persist a single query/result exchange to the SessionStore.
Creates the session DB row on the first call, then appends messages.
Parameters
----------
query : str
User query text.
result : Any
Agent result to persist as session messages.
"""
store: Optional[SessionStore] = st.session_state.get("session_store")
if store is None:
return
try:
_ensure_ui_session(query)
# Build SessionMessage objects for this exchange
if include_query:
entry = {"query": query, "result": result}
messages = conversation_entry_to_messages(entry)
else:
messages = [
msg for msg in messages_from_result(result) if msg.role != "human"
]
if messages:
store.save_messages(st.session_state.current_session_id, messages)
except Exception as exc:
# Best-effort persistence -- don't break the UI.
logger.warning("Failed to save exchange to session store: %s", exc)
def _render_agent_status(
selected_model: str,
selected_workflow: str,
thread_id: int,
endpoint_status: dict,
) -> None:
"""Render sidebar status for the active agent.
Parameters
----------
selected_model : str
Selected model name.
selected_workflow : str
Selected workflow name.
thread_id : int
Current LangGraph thread ID.
endpoint_status : dict
Local endpoint status dictionary.
"""
st.sidebar.header("Agent Status")
if st.session_state.agent:
st.sidebar.success("\u2705 Agents Ready")
st.sidebar.info(f"\U0001f9e0 Model: {selected_model}")
st.sidebar.info(f"\u2699\ufe0f Workflow: {selected_workflow}")
st.sidebar.info(f"\U0001f517 Thread ID: {thread_id}")
st.sidebar.info(
f"\U0001f4ac Messages: {len(st.session_state.conversation_history)}"
)
if endpoint_status["ok"]:
st.sidebar.caption(f"LLM endpoint: {endpoint_status['message']}")
else:
st.sidebar.error(f"LLM endpoint issue: {endpoint_status['message']}")
if st.session_state.pending_human_question is not None:
st.sidebar.warning("Waiting for your input...")
if st.session_state.last_run_error:
st.sidebar.error("Last run error (see verbose info).")
if st.sidebar.button("\U0001f504 Refresh Agents"):
st.session_state.agent = None
# Checkpoint is lost on re-init, so clear interrupt state
_clear_interrupt_state()
st.rerun()
else:
st.sidebar.error("\u274c Agents Not Ready")
st.sidebar.info("Agents will initialize automatically...")
if not endpoint_status["ok"]:
st.sidebar.error(f"LLM endpoint issue: {endpoint_status['message']}")
st.sidebar.markdown("---")
st.sidebar.markdown("**\u2699\ufe0f Configuration**")
st.sidebar.markdown(
"Use the Configuration page to modify settings, API endpoints, and chemistry parameters."
)
st.sidebar.markdown("Current config loaded from: `config.toml`")
def _auto_initialize_agent(
config: dict,
selected_model: str,
selected_workflow: str,
structured_output: bool,
selected_output: str,
generate_report: bool,
human_supervised: bool,
selected_base_url: Optional[str],
) -> None:
"""Initialize or refresh the cached Streamlit agent when config changes.
Parameters
----------
config : dict
Nested UI configuration.
selected_model : str
Selected model name.
selected_workflow : str
Selected workflow name.
structured_output : bool
Effective structured-output setting.
selected_output : str
Agent return mode.
generate_report : bool
Whether report generation is enabled.
human_supervised : bool
Whether human-supervision tools are enabled.
selected_base_url : str, optional
Model endpoint URL.
"""
current_config = (
selected_model,
selected_workflow,
structured_output,
selected_output,
generate_report,
human_supervised,
config["general"]["recursion_limit"],
selected_base_url,
get_argo_user_from_nested_config(config),
st.session_state.get("current_chat_log_dir"),
)
if st.session_state.agent is None or st.session_state.last_config != current_config:
with st.spinner("\U0001f680 Initializing ChemGraph agents..."):
chat_log_dir = _ensure_chat_log_dir()
agent = initialize_agent(
selected_model,
selected_workflow,
structured_output,
selected_output,
generate_report,
human_supervised,
config["general"]["recursion_limit"],
selected_base_url,
get_argo_user_from_nested_config(config),
log_dir=chat_log_dir,
)
st.session_state.agent = agent
if agent is not None:
st.session_state.last_config = (
selected_model,
selected_workflow,
structured_output,
selected_output,
generate_report,
human_supervised,
config["general"]["recursion_limit"],
selected_base_url,
get_argo_user_from_nested_config(config),
chat_log_dir,
)
else:
st.session_state.last_config = None
def _render_conversation_history(thread_id: int) -> None:
"""Render all saved conversation exchanges.
Parameters
----------
thread_id : int
Current LangGraph thread ID.
"""
if not st.session_state.conversation_history:
return
pending = st.session_state.get("pending_agent_submission")
pending_log_dir = pending.get("query_log_dir") if isinstance(pending, dict) else None
pending_interrupt_log_dir = (
st.session_state.get("pending_interrupt_log_dir")
if st.session_state.get("pending_human_question") is not None
else None
)
visible_idx = 1
for entry in st.session_state.conversation_history:
if (
pending_log_dir
and entry.get("status") == "running"
and entry.get("log_dir") == pending_log_dir
):
# The live streaming panel below renders this same submitted query.
# Avoid showing a duplicate history bubble while the run is active.
continue
if (
pending_interrupt_log_dir
and entry.get("status") == "waiting_for_input"
and entry.get("log_dir") == pending_interrupt_log_dir
):
# The pending interrupt panel renders the original query, prior
# clarification exchanges, and the current question.
continue
_render_single_exchange(visible_idx, entry, thread_id)
visible_idx += 1
def _render_single_exchange(idx: int, entry: dict, thread_id: int) -> None:
"""Render one user-query / agent-response exchange.
Parameters
----------
idx : int
One-based exchange index.
entry : dict
Conversation-history entry.
thread_id : int
Current LangGraph thread ID.
"""
# User message
with st.chat_message("user"):
st.markdown(entry["query"])
# Interrupt exchanges (if any occurred during this query)
for exch in entry.get("interrupt_exchanges", []):
with st.chat_message("assistant"):
_render_markdown_with_math(exch["question"])
with st.chat_message("user"):
st.markdown(exch["answer"])
messages = extract_messages_from_result(entry["result"])
if not messages:
with st.chat_message("assistant"):
status = entry.get("status")
if status == "error":
st.error(f"Processing error: {entry.get('error', 'unknown error')}")
elif status == "waiting_for_input":
st.info("The agent is waiting for your input.")
elif status == "running" or _agent_run_active():
st.info("Agent is running. Please wait for the current task to finish.")
else:
st.info(
"No assistant response was saved for this run. "
"It may have been interrupted; please rerun the query."
)
_render_workflow_process_trace(messages, entry)
_render_verbose_info(idx, messages, entry)
return
# Prefer validated structured output over intermediate LLM prose.
final_answer = _final_answer_for_entry(entry, messages)
# Display the AI response with visualizations
with st.chat_message("assistant"):
if final_answer:
_render_markdown_with_math(final_answer)
_render_workflow_process_trace(messages, entry)
# Structure visualisation
html_filename = find_html_filename(messages)
_render_structure_section(idx, messages, final_answer, entry, html_filename)
# HTML report
if html_filename:
_render_html_report(idx, html_filename, messages, entry)
# IR spectrum
if is_infrared_requested(messages):
_render_ir_spectrum(idx, messages, entry)
# Debug expander
_render_verbose_info(idx, messages, entry)
def _extract_final_answer(messages: list) -> str:
"""Walk messages in reverse to find the last non-JSON AI message.
Parameters
----------
messages : list
Message-like objects or dictionaries.
Returns
-------
str
Final displayable answer text, or an empty string.
"""
final_answer = ""
for message in reversed(messages):
if hasattr(message, "content") and hasattr(message, "type"):
content = normalize_message_content(message.content).strip()
if message.type == "ai" and content:
if not (
content.startswith("{")
and content.endswith("}")
and "numbers" in content
):
final_answer = content
break
elif isinstance(message, dict):
content = normalize_message_content(message.get("content", "")).strip()
if message.get("type") == "ai" and content:
if not (
content.startswith("{")
and content.endswith("}")
and "numbers" in content
):
final_answer = content
break
elif hasattr(message, "content"):
content = normalize_message_content(getattr(message, "content", "")).strip()
if content and not (
content.startswith("{")
and content.endswith("}")
and "numbers" in content
):
final_answer = content
break
return final_answer
def _is_response_formatter_payload(payload: Any) -> bool:
"""Return whether *payload* looks like ChemGraph structured final output."""
if not isinstance(payload, dict):
return False
formatter_fields = {
"smiles",
"scalar_answer",
"scalar_answers",
"dipole",
"vibrational_answer",
"ir_spectrum",
"atoms_data",
"_failure",
}
return any(field in payload for field in formatter_fields)
def _parse_structured_output_text(text: str) -> Optional[dict]:
"""Parse a structured-output JSON string if it matches ResponseFormatter."""
stripped = normalize_message_content(text).strip()
if not (stripped.startswith("{") and stripped.endswith("}")):
return None
try:
payload = json.loads(stripped)
except json.JSONDecodeError:
return None
if not _is_response_formatter_payload(payload):
return None
return payload
def _structured_output_from_entry(entry: dict, messages: list) -> Optional[dict]:
"""Return the validated final output saved by the graph, if available."""
result = entry.get("result") if isinstance(entry, dict) else None
if isinstance(result, dict):
final_output = result.get("final_output")
if _is_response_formatter_payload(final_output):
return _augment_structured_output_from_messages(entry, final_output, messages)
for message in reversed(messages):
if hasattr(message, "content"):
parsed = _parse_structured_output_text(getattr(message, "content", ""))
elif isinstance(message, dict):
parsed = _parse_structured_output_text(message.get("content", ""))
else:
parsed = None
if parsed:
return _augment_structured_output_from_messages(entry, parsed, messages)
return None
def _augment_structured_output_from_messages(
entry: dict,
structured: dict,
messages: list,
) -> dict:
"""Fill derived fields from saved tool messages when older output lacks them."""
if structured.get("scalar_answers"):
return structured
query = str(entry.get("query") or "").strip()
if not query or not messages:
return structured
try:
validation = validate_completion(query, messages)
except Exception:
return structured
candidate = (
validation.get("structured_output") if isinstance(validation, dict) else None
)
if not isinstance(candidate, dict) or not candidate.get("scalar_answers"):
return structured
augmented = dict(structured)
augmented["scalar_answers"] = candidate["scalar_answers"]
return augmented
def _workflow_status_from_result(result: dict) -> str:
"""Return scientific workflow status, not just graph stream status."""
if not isinstance(result, dict):
return "complete"
final_output = result.get("final_output")
if isinstance(final_output, dict):
failure = final_output.get("_failure")
if isinstance(failure, dict):
status = str(failure.get("status") or "").lower()
if status in {"blocked", "failed", "error"}:
return "failed" if status == "error" else status
return "failed"
ledger = result.get("scientific_ledger")
if isinstance(ledger, dict):
status = str(ledger.get("status") or "").lower()
if status in {"complete", "blocked", "failed", "partial"}:
return status
validation = (
result.get("completion_validation")
if isinstance(result.get("completion_validation"), dict)
else result.get("validation")
)
if isinstance(validation, dict) and "complete" in validation:
return "complete" if validation.get("complete") else "blocked"
return "complete"
def _workflow_status_label(workflow_status: str) -> str:
return {
"complete": "Complete",
"blocked": "Blocked",
"failed": "Failed",
"partial": "Partial",
}.get(str(workflow_status or "").lower(), "Complete")
def _streamlit_state_for_workflow_status(workflow_status: str) -> str:
return "complete" if str(workflow_status or "").lower() == "complete" else "error"
def _format_number_for_display(value: Any) -> str:
"""Format numeric values without excessive trailing precision."""
if isinstance(value, int):
return str(value)
if isinstance(value, float):
return f"{value:.8g}"
return str(value)
def _plain_scientific_unit(unit: Any) -> str:
"""Return a readable unit string for Markdown text."""
text = normalize_latex_delimiters(str(unit or "")).strip()
text = text.strip("$")
text = re.sub(r"\\text\{([^{}]+)\}", r"\1", text)
text = re.sub(r"\\mathrm\{([^{}]+)\}", r"\1", text)
text = text.replace(r"\cdot", "·")
text = text.replace(r"\AA", "Å")
text = text.replace("Angstrom", "Å").replace("angstrom", "Å")
text = re.sub(r"\s*\*\s*", "·", text)
text = re.sub(r"\s*·\s*", "·", text)
return text
def _format_structured_output_answer(structured: Optional[dict]) -> str:
"""Create user-facing Markdown from validated structured output.
The UI should prefer this over intermediate LLM prose because these fields
come from the graph's completion validator or ResponseFormatter output.
"""
if not structured:
return ""
if isinstance(structured.get("_failure"), dict):
return format_validation_failure_message(structured)
lines: list[str] = []
smiles = structured.get("smiles")
if smiles:
values = smiles if isinstance(smiles, list) else [smiles]
lines.append("**SMILES:** " + ", ".join(f"`{value}`" for value in values))
scalar_values = structured.get("scalar_answers")
if isinstance(scalar_values, list):
scalars = [
item
for item in scalar_values
if isinstance(item, dict) and item.get("value") is not None
]
else:
scalar = structured.get("scalar_answer")
scalars = (
[scalar]
if isinstance(scalar, dict) and scalar.get("value") is not None
else []
)
for scalar in scalars:
property_name = str(scalar.get("property") or "Scalar result").strip()
value = _format_number_for_display(scalar.get("value"))
unit = _plain_scientific_unit(scalar.get("unit"))
suffix = f" {unit}" if unit else ""
lines.append(f"**{property_name}:** {value}{suffix}")
dipole = structured.get("dipole")
if isinstance(dipole, dict) and dipole.get("value") is not None:
values = dipole.get("value")
if isinstance(values, list):
vector = ", ".join(_format_number_for_display(value) for value in values)
vector = f"[{vector}]"
else:
vector = _format_number_for_display(values)
unit = _plain_scientific_unit(dipole.get("unit"))
suffix = f" {unit}" if unit else ""
lines.append(f"**Dipole moment:** {vector}{suffix}")
vibrational = structured.get("vibrational_answer")
if isinstance(vibrational, dict):
frequencies = vibrational.get("frequency_cm1") or []
if frequencies:
sample = ", ".join(str(freq) for freq in frequencies[:10])
extra = "" if len(frequencies) <= 10 else f", ... ({len(frequencies)} total)"
lines.append(f"**Vibrational frequencies:** {sample}{extra} cm^-1")
ir_spectrum = structured.get("ir_spectrum")
if isinstance(ir_spectrum, dict):
frequencies = ir_spectrum.get("frequency_cm1") or []
intensities = ir_spectrum.get("intensity") or []
if frequencies or intensities:
lines.append(
"**IR spectrum:** "
f"{len(frequencies)} frequencies and {len(intensities)} intensities."
)
plot = ir_spectrum.get("plot")
if plot:
lines.append(f"**IR plot:** `{plot}`")
if structured.get("atoms_data"):
lines.append("**Structure:** atomic geometry data is available below.")
return "\n\n".join(lines)
def _final_answer_for_entry(entry: dict, messages: list) -> str:
"""Return the final answer text for a UI exchange."""
structured = _structured_output_from_entry(entry, messages)
structured_answer = _format_structured_output_answer(structured)
if structured_answer:
return structured_answer
return _extract_final_answer(messages)
def _render_structure_section(
idx: int,
messages: list,
final_answer: str,
entry: dict,
html_filename: Optional[str],
) -> None:
"""Render molecular structure artifacts for an exchange.
Parameters
----------
idx : int
One-based exchange index.
messages : list
Message-like objects from the exchange.
final_answer : str
Final assistant answer text.
entry : dict
Conversation-history entry.
html_filename : str, optional
HTML report path/filename, if detected.
"""
structure = find_structure_in_messages(messages)
if structure:
display_molecular_structure(
structure["atomic_numbers"],
structure["positions"],
title=f"Molecular Structure (Query {idx})",
)
else:
structure_from_text = extract_molecular_structure(final_answer)
if structure_from_text:
display_molecular_structure(
structure_from_text["atomic_numbers"],
structure_from_text["positions"],
title=f"Structure from Response {idx}",
)
elif not html_filename:
if has_structure_signal(messages, entry.get("query", ""), final_answer):
log_dir = _artifact_log_dir(messages, entry)
if log_dir and os.path.isdir(log_dir):
latest_xyz = find_latest_xyz_file_in_dir(log_dir)
if latest_xyz:
try:
atoms = ase_read(latest_xyz)
display_molecular_structure(
atoms.get_atomic_numbers().tolist(),
atoms.get_positions().tolist(),
title=(
f"Structure from {Path(latest_xyz).name} "
f"(Query {idx})"
),
)
except Exception as exc:
st.warning(f"Failed to load XYZ structure: {exc}")
def _render_html_report(
idx: int, html_filename: str, messages: list, entry: dict
) -> None:
"""Render an HTML report expander and download button.
Parameters
----------
idx : int
One-based exchange index.
html_filename : str
HTML report path or filename.
messages : list
Message-like objects from the exchange.
entry : dict
Conversation-history entry.
"""
with st.expander("\U0001f4ca Report", expanded=False):
try:
resolved_html = _resolve_artifact_path(
html_filename,
_artifact_log_dir(messages, entry),
)
with open(resolved_html, "r", encoding="utf-8") as f:
html_content = f.read()
report_structure = extract_xyz_from_report_html(html_content)
if report_structure:
display_molecular_structure(
report_structure["atomic_numbers"],
report_structure["positions"],
title=f"Molecular Structure (Report {idx})",
)
cleaned_html = strip_viewer_from_report_html(html_content)
st.download_button(
"Download HTML Report",
data=html_content,
file_name=Path(resolved_html).name,
mime="text/html",
key=f"download_report_{idx}",
)
st.components.v1.html(cleaned_html, height=600, scrolling=True)
except FileNotFoundError:
st.warning(f"HTML file '{html_filename}' not found")
except Exception as e:
st.error(f"Error displaying HTML: {e}")
def _artifact_log_dir(messages: list, entry: dict) -> Optional[str]:
"""Return the log directory tied to a specific conversation entry.
Parameters
----------
messages : list
Message-like objects from the exchange.
entry : dict
Conversation-history entry.
Returns
-------
str or None
Artifact/log directory, if found.
"""
entry_log_dir = entry.get("log_dir")
if entry_log_dir:
return entry_log_dir
return extract_log_dir_from_messages(messages)
def _latest_artifact_path(directory: Optional[str], pattern: str) -> Optional[str]:
"""Return the newest shallow match for an output artifact pattern.
Parameters
----------
directory : str, optional
Directory to search.
pattern : str
Glob pattern to match.
Returns
-------
str or None
Newest matching file path, or ``None``.
"""
if not directory or not os.path.isdir(directory):
return None
candidates: list[Path] = []
try:
candidates.extend(path for path in Path(directory).glob(pattern) if path.is_file())
except OSError:
return None
if not candidates:
return None
return str(max(candidates, key=lambda path: path.stat().st_mtime))
def _resolve_artifact_path(filename: str, directory: Optional[str]) -> str:
"""Resolve an artifact path relative to its run directory when known.
Parameters
----------
filename : str
Absolute or relative artifact path.
directory : str, optional
Run artifact directory.
Returns
-------
str
Resolved artifact path.
"""
if os.path.isabs(filename):
return filename
if directory:
return str(Path(directory) / filename)
return filename
def _render_ir_spectrum(idx: int, messages: list, entry: dict) -> None:
"""Render IR spectrum plot, frequency table, and trajectory viewer.
Parameters
----------
idx : int
One-based exchange index.
messages : list
Message-like objects from the exchange.
entry : dict
Conversation-history entry.
"""
log_dir = _artifact_log_dir(messages, entry)
ir_path = _latest_artifact_path(log_dir, "ir_spectrum*.png")
freq_path = _latest_artifact_path(log_dir, "frequencies*.csv")
if not ir_path and not freq_path:
st.warning("IR spectrum not found.")
return
with st.expander("\U0001f50d IR Spectrum", expanded=True):
col1, col2 = st.columns(2, border=True)
with col1:
if ir_path and os.path.exists(ir_path):
st.image(ir_path)
else:
st.warning("IR spectrum plot not found.")
with col2:
if not freq_path or not os.path.exists(freq_path):
st.warning("Frequencies file not found.")
return
df = pd.read_csv(
freq_path,
index_col=False,
names=["filename", "frequency"],
)
modes = df.iloc[6:] if len(df) > 6 else df
if modes.empty:
st.warning("No vibrational frequencies found.")
return
st.write("**Select a frequency to visualize:**")
freq_options = {}
for mode_idx, row in modes.iterrows():
freq_text = str(row["frequency"]).strip()
suffix = "i" if freq_text.endswith("i") else ""
try:
freq_value = float(freq_text.rstrip("i"))
label = f"Mode {mode_idx}: {freq_value:.2f}{suffix} cm\u207b\u00b9"
except ValueError:
label = f"Mode {mode_idx}: {freq_text} cm\u207b\u00b9"
freq_options[label] = mode_idx
selected_freq = st.selectbox(
"Frequency",
list(freq_options.keys()),
index=0,
key=f"ir_frequency_select_{idx}",
)
traj_file = str(modes.loc[freq_options[selected_freq]]["filename"])
traj_path = _resolve_artifact_path(traj_file, log_dir)
if not os.path.exists(traj_path):
st.warning(f"Trajectory file '{traj_file}' not found.")
elif not STMOL_AVAILABLE:
st.info("3D viewer not available; install stmol to animate trajectories.")
else:
import stmol
from ase.io.trajectory import Trajectory
traj = Trajectory(traj_path)
view = visualize_trajectory(traj)
view.zoomTo()
stmol.showmol(view, height=400, width=500)
def _render_workflow_process_trace(messages: list, entry: dict) -> None:
"""Render the validated tool-path visualization for a saved exchange."""
ledger = _scientific_ledger_from_entry(entry)
if isinstance(ledger, dict):
trace = _workflow_trace_from_ledger(ledger)
else:
trace = entry.get("workflow_trace")
rebuilt_trace = _workflow_trace_from_messages(messages) if messages else None
if not isinstance(trace, dict):
trace = rebuilt_trace
elif _trace_has_incomplete_run_ase(trace) and isinstance(rebuilt_trace, dict):
trace = rebuilt_trace
completed = trace.get("completed", []) if isinstance(trace, dict) else []
active = trace.get("active", []) if isinstance(trace, dict) else []
if not completed and not active:
return
st.html(_workflow_trace_diagram_html(completed, active))
st.html(_workflow_tool_status_html(completed, active))
identity_html = _identity_provenance_html(_identity_records_from_messages(messages))
if identity_html:
st.html(identity_html)
def _scientific_ledger_from_entry(entry: dict) -> Optional[dict]:
"""Return saved scientific ledger for a UI exchange, when available."""
result = entry.get("result") if isinstance(entry, dict) else None
if not isinstance(result, dict):
return None
ledger = result.get("scientific_ledger")
return ledger if isinstance(ledger, dict) else None
def _workflow_trace_from_ledger(ledger: dict) -> dict[str, list]:
"""Build UI trace labels from canonical requirement ledger rows."""
completed: list[str] = []
active: list[dict] = []
can_answer = bool(ledger.get("can_answer"))
artifacts_by_id = {
artifact.get("id"): artifact
for artifact in ledger.get("artifacts", []) or []
if isinstance(artifact, dict) and artifact.get("id")
}
for req in ledger.get("requirements", []) or []:
if not isinstance(req, dict):
continue
label = _ledger_requirement_label(req, artifacts_by_id)
status = str(req.get("status") or "").lower()
if status == "done":
completed.append(label)
elif status in {"failed", "blocked"}:
completed.append(f"{label} ({status})")
elif can_answer:
continue
else:
completed.append(f"{label} (incomplete)")
return {"completed": completed, "active": active}
def _ledger_requirement_label(req: dict, artifacts_by_id: Optional[dict] = None) -> str:
"""Return a trace-compatible label for one ledger requirement."""
kind = str(req.get("kind") or "requirement")
species = req.get("species")
target = f" on {species}" if species else ""
if kind == "identity":
return f"molecule_name_to_smiles:{target}".replace(": on", ":")
if kind == "xyz":
return f"smiles_to_coordinate_file:{target}".replace(": on", ":")
if kind == "aggregation":
return "calculator: aggregate reaction property"
if kind in {"energy", "dipole", "thermo", "vib", "ir"}:
calculator = _ledger_requirement_calculator(req, artifacts_by_id or {})
calc_suffix = f"/{calculator}" if calculator else ""
return f"run_ase: {kind}{calc_suffix}{target}"
return f"{kind}:{target}".replace(": on", ":")
def _ledger_requirement_calculator(req: dict, artifacts_by_id: dict) -> str:
"""Return calculator family from a requirement's satisfied artifacts."""
for artifact_id in req.get("satisfied_by", []) or []:
artifact = artifacts_by_id.get(artifact_id)
if not isinstance(artifact, dict):
continue
calculator = artifact.get("calculator")
if calculator:
return str(calculator)
return ""
def _trace_has_incomplete_run_ase(trace: dict) -> bool:
"""Return whether a saved workflow trace contains stale incomplete run_ase rows."""
labels = []
if isinstance(trace, dict):
labels.extend(str(label) for label in trace.get("completed", []) or [])
labels.extend(_trace_entry_label(item) for item in trace.get("active", []) or [])
return any("run_ase" in label and "(incomplete)" in label for label in labels)
def _render_verbose_info(idx: int, messages: list, entry: dict) -> None:
"""Render raw result/debug information for an exchange.
Parameters
----------
idx : int
One-based exchange index.
messages : list
Message-like objects from the exchange.
entry : dict
Conversation-history entry.
"""
structure = find_structure_in_messages(messages)
with st.expander(f"\U0001f50d Verbose Info (Query {idx})", expanded=False):
st.write(f"**Number of messages:** {len(messages)}")
st.write(f"**Structure found:** {'Yes' if structure else 'No'}")
raw_result = entry.get("result")
if st.session_state.last_run_query == entry.get("query"):
if st.session_state.last_run_error:
st.write("**Last run error:**")
st.code(str(st.session_state.last_run_error))
if st.session_state.last_run_result is not None:
raw_result = st.session_state.last_run_result
st.write("**Raw result:**")
st.code(pprint.pformat(raw_result, width=1, compact=False), language="text")
def _render_example_queries(config: dict, selected_model: str) -> None:
"""Show example queries that the user can click to submit directly.
Parameters
----------
config : dict
Nested UI configuration.
selected_model : str
Selected model name.
"""
# Hide after the first message or during an interrupt
if (
st.session_state.conversation_history
or st.session_state.pending_human_question is not None
or _agent_run_active()
):
return
with st.expander("Example Queries", expanded=False):
st.markdown("**Based on your current configuration:**")
st.markdown(f"- Model: {selected_model}")
st.markdown(
f"- Default Calculator: {config['chemistry']['calculators']['default']}"
)
st.caption(QUERY_SPECIFICATION_HINT)
examples = [
"What is the SMILES string for caffeine?",
f"Optimize the geometry of water molecule using {config['chemistry']['calculators']['default']}",
"Calculate the infrared spectrum of methanol with xtb calculator",
"What is the reaction enthalpy of methane combustion using mace_mp",
]
for ex in examples:
if st.button(ex, key=f"ex_{ex}"):
st.session_state._pending_example_query = ex
st.rerun()
def _render_pending_interrupt() -> None:
"""Show the agent's pending question and any prior interrupt exchanges."""
question = st.session_state.pending_human_question
if question is None:
return
# Show the original user query that triggered the interrupt
original_query = st.session_state.pending_interrupt_query
if original_query:
with st.chat_message("user"):
_render_markdown_with_math(original_query)
# Show any prior interrupt exchanges in this chain
for exch in st.session_state.interrupt_exchanges:
with st.chat_message("assistant"):
_render_markdown_with_math(exch["question"])
with st.chat_message("user"):
_render_markdown_with_math(exch["answer"])
# Show the current pending question
with st.chat_message("assistant"):
st.info("The agent needs your input to continue.", icon="\u2753")
_render_markdown_with_math(question)
# Cancel button
if st.button("Cancel", key="cancel_interrupt"):
_clear_interrupt_state()
st.rerun()
def _restore_pending_interrupt_from_history() -> None:
"""Restore in-memory clarification state from a waiting history entry."""
if st.session_state.pending_human_question is not None:
return
for entry in reversed(st.session_state.get("conversation_history", [])):
if entry.get("status") != "waiting_for_input":
continue
question = entry.get("pending_human_question")
resume_config = entry.get("pending_interrupt_config")
if not question or not isinstance(resume_config, dict):
return
st.session_state.pending_human_question = question
st.session_state.pending_interrupt_config = dict(resume_config)
st.session_state.pending_interrupt_query = entry.get(
"pending_interrupt_query", entry.get("query")
)
st.session_state.pending_interrupt_thread_id = entry.get(
"pending_interrupt_thread_id", entry.get("thread_id")
)
st.session_state.pending_interrupt_prev_msg_count = int(
entry.get("pending_interrupt_prev_msg_count", 0) or 0
)
st.session_state.pending_interrupt_model = entry.get("pending_interrupt_model")
st.session_state.pending_interrupt_workflow = entry.get(
"pending_interrupt_workflow"
)
st.session_state.pending_interrupt_log_dir = entry.get(
"pending_interrupt_log_dir", entry.get("log_dir")
)
st.session_state.interrupt_count = int(entry.get("interrupt_count", 1) or 1)
st.session_state.interrupt_exchanges = list(
entry.get("interrupt_exchanges") or []
)
return
def _clear_interrupt_state() -> None:
"""Clear all interrupt-related session state."""
st.session_state.pending_human_question = None
st.session_state.pending_interrupt_config = None
st.session_state.pending_interrupt_query = None
st.session_state.pending_interrupt_thread_id = None
st.session_state.pending_interrupt_prev_msg_count = 0
st.session_state.pending_interrupt_model = None
st.session_state.pending_interrupt_workflow = None
st.session_state.pending_interrupt_log_dir = None
st.session_state.interrupt_count = 0
st.session_state.interrupt_exchanges = []
def _classify_message(msg):
"""Classify a LangGraph message for UI display.
Parameters
----------
msg : Any
LangGraph/LangChain message to classify.
Returns
-------
tuple or None
``("tool_call", [tool_items])``, ``("tool_result", tool_item)``, or
``None`` when not relevant for display.
"""
if isinstance(msg, dict):
tool_calls = msg.get("tool_calls")
msg_type = msg.get("type") or msg.get("role")
tool_name = msg.get("name")
tool_call_id = msg.get("tool_call_id")
content = msg.get("content")
else:
tool_calls = getattr(msg, "tool_calls", None)
msg_type = getattr(msg, "type", None)
tool_name = getattr(msg, "name", None)
tool_call_id = getattr(msg, "tool_call_id", None)
content = getattr(msg, "content", None)
if tool_calls:
items = [
_tool_call_display_item(tc)
for tc in tool_calls
if isinstance(tc, dict)
]
if items:
return ("tool_call", items)
if msg_type == "tool":
if tool_name:
return (
"tool_result",
{
"id": tool_call_id,
"name": tool_name,
"label": _tool_result_label(
tool_name,
content,
),
},
)
return None
def _tool_call_display_item(tool_call: dict) -> dict:
"""Return a compact UI label for an LLM tool call."""
name = tool_call.get("name", "unknown")
args = tool_call.get("args") or {}
return {
"id": tool_call.get("id"),
"name": name,
"label": _tool_call_label(name, args if isinstance(args, dict) else {}),
}
def _tool_call_label(name: str, args: dict) -> str:
"""Format a short in-progress label for a tool call."""
if name in {"molecule_name_to_smiles", "resolve_molecule_identity"}:
molecule = args.get("name")
return f"{name}: {molecule}" if molecule else name
if name == "smiles_to_coordinate_file":
smiles = args.get("smiles")
output_file = args.get("output_file")
if smiles and output_file:
return f"{name}: {smiles} -> {Path(str(output_file)).name}"
if smiles:
return f"{name}: {smiles}"
return name
if name == "run_ase":
params = args.get("params") if isinstance(args.get("params"), dict) else args
driver = params.get("driver")
structure = params.get("input_structure_file")
calculator = _calculator_label(params.get("calculator"))
parts = [part for part in [driver, calculator] if part]
prefix = "/".join(parts) if parts else "ASE"
if structure:
return f"{name}: {prefix} on {Path(str(structure)).name}"
return f"{name}: {prefix}"
return name
def _tool_result_label(name: str, content: Any) -> str:
"""Format a completed-tool label using the actual tool output when possible."""
data = _tool_result_content_to_data(content)
if name in {"molecule_name_to_smiles", "resolve_molecule_identity"} and isinstance(data, dict):
molecule = data.get("name") or data.get("input_name")
smiles = data.get("smiles")
details = []
if data.get("source"):
details.append(str(data["source"]))
if data.get("cid"):
details.append(f"CID {data['cid']}")
if data.get("credibility_score") is not None:
details.append(f"score {data['credibility_score']}")
if data.get("requires_clarification") or data.get("needs_clarification"):
details.append("needs clarification")
suffix = f" ({', '.join(details)})" if details else ""
if molecule and smiles:
return f"{name}: {molecule} -> {smiles}{suffix}"
if molecule:
return f"{name}: {molecule}{suffix}"
if name == "smiles_to_coordinate_file" and isinstance(data, dict):
smiles = data.get("smiles")
path = data.get("path") or data.get("output_file")
if smiles and path:
return f"{name}: {smiles} -> {Path(str(path)).name}"
if path:
return f"{name}: {Path(str(path)).name}"
if name == "run_ase" and isinstance(data, dict):
status = data.get("status")
result = data.get("result") if isinstance(data.get("result"), dict) else {}
details = []
if "single_point_energy" in data:
details.append("energy")
if isinstance(result, dict) and result.get("thermochemistry"):
details.append("thermo")
if data.get("dipole_moment"):
details.append("dipole")
if isinstance(result, dict) and result.get("ir_spectrum"):
details.append("ir")
elif isinstance(result, dict) and result.get("vibrational_frequencies"):
details.append("vib")
detail = "/".join(details) if details else "ASE"
structure_hint = _run_ase_result_structure_hint(data)
if structure_hint:
detail = f"{detail} on {structure_hint}"
return f"{name}: {detail}{f' ({status})' if status else ''}"
return name
def _run_ase_result_structure_hint(data: dict) -> str:
"""Return a compact structure filename from a run_ase result payload."""
candidates: list[Any] = []
for key in ("input_structure_file", "structure_file"):
if data.get(key):
candidates.append(data.get(key))
simulation_input = data.get("simulation_input")
if isinstance(simulation_input, dict) and simulation_input.get("input_structure_file"):
candidates.append(simulation_input.get("input_structure_file"))
message = data.get("message")
if isinstance(message, str):
match = re.search(r"saved to\s+(.+?\.json)\b", message)
if match:
candidates.append(match.group(1))
for candidate in candidates:
hint = _structure_hint_from_path(candidate)
if hint:
return hint
return ""
def _structure_hint_from_path(path_value: Any) -> str:
"""Convert a result or structure path into a displayable XYZ-like basename."""
if not path_value:
return ""
name = Path(str(path_value)).name
if name.endswith(".xyz"):
return name
if name.startswith("output_") and name.endswith(".json"):
species = name.removeprefix("output_").removesuffix(".json")
if species:
return f"{species}.xyz"
return ""
def _tool_result_content_to_data(content: Any) -> Any:
"""Best-effort parse of a tool message payload for UI labels."""
if isinstance(content, (dict, list)):
return content
text = str(content or "").strip()
if not text:
return text
try:
return json.loads(text)
except json.JSONDecodeError:
return text
def _workflow_trace_from_messages(messages: list) -> dict[str, list]:
"""Rebuild completed tool labels from saved LangGraph messages."""
completed: list[str] = []
active: list[dict] = []
for message in messages or []:
classified = _classify_message(message)
if not classified:
continue
event_type, event_data = classified
if event_type == "tool_call":
for item in event_data:
active.append(item)
elif event_type == "tool_result":
item = event_data
_pop_active_trace_match(active, item)
label = str(item.get("label") or item.get("name") or "")
if label and label not in completed:
completed.append(label)
for item in active:
label = _trace_entry_label(item)
incomplete = f"{label} (incomplete)"
if label and incomplete not in completed:
completed.append(incomplete)
return {"completed": completed, "active": []}
def _pop_active_trace_match(active: list[dict], result: Any) -> Optional[dict]:
"""Pop the active tool call matching a completed tool result."""
result_id = result.get("id") if isinstance(result, dict) else None
result_name = result.get("name") if isinstance(result, dict) else str(result)
result_label = _trace_entry_label(result)
result_hint = _trace_structure_hint(result_label)
if result_id:
for index, item in enumerate(active):
if isinstance(item, dict) and item.get("id") == result_id:
return active.pop(index)
specific_matches: list[int] = []
generic_matches: list[int] = []
for index, item in enumerate(active):
if not isinstance(item, dict):
continue
item_name = item.get("name")
item_label = _trace_entry_label(item)
if item_name != result_name and item_label != result_name:
continue
generic_matches.append(index)
item_hint = _trace_structure_hint(item_label)
if result_hint and item_hint and result_hint == item_hint:
specific_matches.append(index)
if specific_matches:
return active.pop(specific_matches[0])
if len(generic_matches) == 1:
return active.pop(generic_matches[0])
return None
def _identity_records_from_messages(messages: list) -> list[dict]:
"""Extract resolver provenance records from saved tool messages."""
records: list[dict] = []
for message in messages or []:
classified = _classify_message(message)
if not classified:
continue
event_type, event_data = classified
if event_type != "tool_result":
continue
if event_data.get("name") not in {
"molecule_name_to_smiles",
"resolve_molecule_identity",
}:
continue
content = message.get("content") if isinstance(message, dict) else getattr(message, "content", None)
data = _tool_result_content_to_data(content)
if isinstance(data, dict) and (data.get("smiles") or data.get("candidates")):
records.append(data)
return records
def _identity_provenance_html(records: list[dict]) -> str:
"""Render resolver provenance as auditable UI, not model reasoning."""
if not records:
return ""
cards = []
for record in records[-6:]:
name = record.get("input_name") or record.get("name") or "chemical"
smiles = record.get("smiles") or "n/a"
source = record.get("source") or "unknown"
cid = record.get("cid") or "n/a"
score = record.get("credibility_score")
score_text = "n/a" if score is None else str(score)
flags = (
record.get("identity_flags")
if isinstance(record.get("identity_flags"), dict)
else {}
)
active_flags = [
key.replace("_", " ")
for key, value in flags.items()
if value is True
]
if record.get("requires_clarification") or record.get("needs_clarification"):
active_flags.append("needs clarification")
warnings = record.get("warnings") or []
if isinstance(warnings, str):
warnings = [warnings]
warning = record.get("warning")
if warning and warning not in warnings:
warnings.append(warning)
flags_text = ", ".join(active_flags) if active_flags else "none"
warning_text = " ".join(str(item) for item in warnings if item) or "none"
candidate_count = len(record.get("candidates") or [])
cards.append(
f"""
<div class="cg-identity-card">
<div class="cg-identity-title">{html.escape(str(name))}</div>
<div class="cg-identity-grid">
<span>SMILES</span><code>{html.escape(str(smiles))}</code>
<span>Source</span><strong>{html.escape(str(source))}</strong>
<span>CID</span><strong>{html.escape(str(cid))}</strong>
<span>Score</span><strong>{html.escape(score_text)}</strong>
<span>Flags</span><em>{html.escape(flags_text)}</em>
<span>Candidates</span><strong>{candidate_count}</strong>
</div>
<div class="cg-identity-warning">{html.escape(warning_text)}</div>
</div>
"""
)
return f"""
<style>
.cg-identity-panel {{
margin-top: 0.85rem;
border-left: 3px solid #3b82f6;
padding-left: 0.75rem;
}}
.cg-identity-heading {{
font-weight: 700;
margin-bottom: 0.55rem;
}}
.cg-identity-cards {{
display: grid;
grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
gap: 0.65rem;
}}
.cg-identity-card {{
border: 1px solid #334155;
border-radius: 8px;
padding: 0.75rem;
background: rgba(15, 23, 42, 0.55);
}}
.cg-identity-title {{
font-weight: 700;
margin-bottom: 0.45rem;
}}
.cg-identity-grid {{
display: grid;
grid-template-columns: 90px minmax(0, 1fr);
gap: 0.25rem 0.5rem;
font-size: 0.78rem;
}}
.cg-identity-grid span {{
color: #94a3b8;
}}
.cg-identity-grid code {{
white-space: normal;
overflow-wrap: anywhere;
}}
.cg-identity-warning {{
margin-top: 0.55rem;
color: #fbbf24;
font-size: 0.78rem;
line-height: 1.35;
}}
</style>
<div class="cg-identity-panel">
<div class="cg-identity-heading">Identity resolution</div>
<div class="cg-identity-cards">{''.join(cards)}</div>
</div>
"""
def _calculator_label(calculator: Any) -> Optional[str]:
"""Return a compact calculator name from string/dict calculator specs."""
if isinstance(calculator, dict):
return str(
calculator.get("name")
or calculator.get("type")
or calculator.get("calculator")
or ""
) or None
if calculator:
return str(calculator)
return None
def _trace_entry_label(entry: Any) -> str:
"""Return the display label for a live trace entry."""
if isinstance(entry, dict):
return str(entry.get("label") or entry.get("name") or "unknown")
return str(entry)
def _trace_structure_hint(label: str) -> str:
"""Extract a structure basename from a compact trace label."""
text = str(label)
match = re.search(r"\bon\s+([A-Za-z0-9_.-]+\.xyz)\b", text)
if match:
return match.group(1)
match = re.search(r"\b(output_[A-Za-z0-9_.-]+\.json)\b", text)
if match:
return _structure_hint_from_path(match.group(1))
return ""
def _is_failure_label(label: str) -> bool:
"""Return whether a tool/status label represents a failed result."""
lowered = str(label).lower()
return any(
marker in lowered
for marker in (
"(failure)",
"(failed)",
"(error)",
"(incomplete)",
"(aborted)",
"(blocked)",
" failed",
" failure",
" error",
" incomplete",
" aborted",
" blocked",
)
)
def _stage_status(
completed_labels: list[str], active_labels: list[str], keys: list[str]
) -> tuple[str, str]:
"""Return CSS class and display text for a workflow stage."""
if any(any(key in label for key in keys) for label in active_labels):
return ("running", "running")
matching_completed = [
label
for label in completed_labels
if any(key in label for key in keys)
]
if not matching_completed:
return ("waiting", "waiting")
last_matching = matching_completed[-1]
if _is_failure_label(last_matching):
return ("failed", "failed")
if any(_is_failure_label(label) for label in matching_completed[:-1]):
return ("recovered", "recovered")
return ("done", "done")
def _simulation_stage(labels: list[str]) -> tuple[str, str, str]:
"""Infer the simulation stage from live tool labels."""
joined = " ".join(labels).lower()
if "run_ase: ir" in joined or "ir/" in joined or "/ir" in joined:
return (
"IR spectrum",
"ASE",
"Compute vibrational intensities and generate an infrared spectrum.",
)
if "thermo" in joined:
return (
"Thermochemistry",
"ASE",
"Compute enthalpy, entropy, and Gibbs corrections from the structure.",
)
if "dipole" in joined:
return ("Dipole", "ASE", "Compute the molecular dipole moment from the geometry.")
if "vib" in joined:
return ("Vibrations", "ASE", "Compute vibrational frequencies from the structure.")
if "opt" in joined:
return ("Optimize", "ASE", "Relax the geometry before reporting properties.")
return ("Energy / property", "ASE", "Run the requested calculator on the XYZ structure.")
def _calculator_route_info(labels: list[str], sim_title: str) -> dict[str, str]:
"""Infer the selected calculator and concise routing rationale from trace labels."""
joined = " ".join(labels).lower()
selected = "Waiting"
reason = "Select an ASE-compatible calculator after the molecule and XYZ structure are available."
if "tblite" in joined or "xtb" in joined or "gfn" in joined:
selected = "TBLite / xTB"
reason = (
"Fast molecular backend; preferred here because IR, vibrations, "
"thermochemistry, and dipole tasks need electronic/force information."
)
elif "mace" in joined:
selected = "MACE / ML potential"
reason = (
"Machine-learned potential route; useful for fast geometry/energy-style "
"work when the chemistry is inside the model domain."
)
elif "emt" in joined:
selected = "EMT"
reason = (
"Lightweight ASE demo calculator; good for plumbing tests, not for "
"serious organic electronic properties."
)
elif "orca" in joined:
selected = "ORCA"
reason = "External quantum-chemistry engine selected; requires a working ORCA executable/profile."
elif "nwchem" in joined:
selected = "NWChem"
reason = "External quantum-chemistry engine selected; requires a working NWChem executable/profile."
elif "run_ase:" in joined:
selected = "ASE default"
reason = (
"The run reached ASE without an explicit calculator label; verify the "
"configured default is suitable for this property."
)
if selected == "Waiting" and sim_title in {"IR spectrum", "Vibrations", "Thermochemistry", "Dipole"}:
reason = (
"For this molecular property, TBLite/xTB is the default low-cost option "
"unless the user explicitly requests MACE, EMT, ORCA, or NWChem."
)
return {
"selected": selected,
"reason": reason,
"options": "TBLite/xTB, MACE/ML, EMT, ORCA, NWChem",
}
def _calculator_stage_status(
completed_labels: list[str],
active_labels: list[str],
) -> tuple[str, str]:
"""Return workflow status for the calculator selection stage."""
labels = completed_labels + active_labels
if not any("run_ase" in label for label in labels):
return ("waiting", "waiting")
if any("run_ase" in label for label in active_labels):
return ("done", "selected")
matching_completed = [label for label in completed_labels if "run_ase" in label]
if not matching_completed:
return ("waiting", "waiting")
last_matching = matching_completed[-1]
if _is_failure_label(last_matching):
return ("failed", "check")
if any(_is_failure_label(label) for label in matching_completed[:-1]):
return ("recovered", "recovered")
return ("done", "selected")
def _workflow_trace_diagram_html(completed: list[str], active: list[dict]) -> str:
"""Build a diagram-style workflow explanation without raw model thoughts."""
completed_labels = [str(label) for label in completed]
active_labels = [_trace_entry_label(item) for item in active]
all_labels = completed_labels + active_labels
sim_title, sim_token, sim_why = _simulation_stage(all_labels)
calculator_info = _calculator_route_info(all_labels, sim_title)
species_state, species_text = _stage_status(
completed_labels,
active_labels,
["molecule_name_to_smiles", "resolve_molecule_identity"],
)
coords_state, coords_text = _stage_status(
completed_labels, active_labels, ["smiles_to_coordinate_file"]
)
calculator_state, calculator_text = _calculator_stage_status(
completed_labels, active_labels
)
sim_state, sim_text = _stage_status(completed_labels, active_labels, ["run_ase"])
composer_ready = sim_state in {"done", "recovered"} and not active_labels
composer_state = "done" if composer_ready else "waiting"
composer_text = "ready" if composer_state == "done" else "waiting"
stages = [
(
"done",
"planned",
"Q",
"Intent",
"Identify the requested property and required chemistry workflow.",
),
(
species_state,
species_text,
"ID",
"Species",
"Resolve molecule names to explicit SMILES identities.",
),
(
coords_state,
coords_text,
"XYZ",
"3D structure",
"Generate coordinates because ASE calculators need atoms in space.",
),
(
calculator_state,
calculator_text,
"CALC",
"Calculator",
"Select the simulation backend and check property/backend fit.",
),
(sim_state, sim_text, sim_token, sim_title, sim_why),
(
composer_state,
composer_text,
"OUT",
"Composer",
"Validate tool outputs and turn them into the final answer.",
),
]
stage_cards = []
for state, status, token, title, why in stages:
stage_cards.append(
f"""
<div class="cg-stage cg-stage-{state}">
<div class="cg-stage-token">{html.escape(token)}</div>
<div class="cg-stage-title">{html.escape(title)}</div>
<div class="cg-stage-status">{html.escape(status)}</div>
<div class="cg-stage-why">{html.escape(why)}</div>
</div>
"""
)
return f"""
<style>
.cg-workflow-panel {{
display: grid;
gap: 0.85rem;
padding: 0.2rem 0 0.1rem;
}}
.cg-workflow-heading {{
display: flex;
align-items: center;
justify-content: space-between;
gap: 1rem;
font-weight: 700;
color: #f5f7fb;
}}
.cg-workflow-heading small {{
color: #9aa4b2;
font-weight: 500;
}}
.cg-workflow-diagram {{
display: grid;
grid-template-columns: repeat(6, minmax(120px, 1fr));
gap: 0.75rem;
align-items: stretch;
}}
.cg-stage {{
position: relative;
min-height: 130px;
border: 1px solid #303743;
border-radius: 8px;
background: #111820;
padding: 0.85rem 0.75rem;
display: grid;
grid-template-rows: auto auto auto 1fr;
gap: 0.35rem;
}}
.cg-stage:not(:last-child)::after {{
content: "";
position: absolute;
top: 50%;
right: -0.78rem;
width: 0.78rem;
height: 2px;
background: #3a4350;
transform: translateY(-50%);
}}
.cg-stage-token {{
width: 2.25rem;
height: 2.25rem;
border-radius: 999px;
display: inline-flex;
align-items: center;
justify-content: center;
background: #1d2632;
color: #dce6f2;
font-size: 0.78rem;
font-weight: 800;
letter-spacing: 0;
}}
.cg-stage-title {{
color: #f8fafc;
font-weight: 800;
line-height: 1.2;
}}
.cg-stage-status {{
justify-self: start;
border-radius: 999px;
padding: 0.12rem 0.5rem;
font-size: 0.72rem;
font-weight: 700;
text-transform: uppercase;
letter-spacing: 0;
}}
.cg-stage-why {{
color: #aab4c1;
font-size: 0.78rem;
line-height: 1.35;
}}
.cg-stage-done {{
border-color: #2f8f5b;
background: #0d1d18;
}}
.cg-stage-done .cg-stage-token,
.cg-stage-done .cg-stage-status {{
background: #1d6f46;
color: #eafff3;
}}
.cg-stage-recovered {{
border-color: #2f8f5b;
background: #0d1d18;
}}
.cg-stage-recovered .cg-stage-token,
.cg-stage-recovered .cg-stage-status {{
background: #1d6f46;
color: #eafff3;
}}
.cg-stage-running {{
border-color: #c8932f;
background: #21190b;
}}
.cg-stage-running .cg-stage-token,
.cg-stage-running .cg-stage-status {{
background: #8b641d;
color: #fff5d8;
}}
.cg-stage-failed {{
border-color: #9f3a3a;
background: #211010;
}}
.cg-stage-failed .cg-stage-token,
.cg-stage-failed .cg-stage-status {{
background: #8f2f2f;
color: #fff0f0;
}}
.cg-stage-failed .cg-stage-why {{
color: #f0b8b8;
}}
.cg-stage-waiting {{
border-color: #394150;
background: #0f141b;
}}
.cg-stage-waiting .cg-stage-status {{
background: #2a3140;
color: #b8c1cf;
}}
.cg-workflow-caveat {{
color: #9aa4b2;
border-left: 3px solid #3a4350;
padding-left: 0.65rem;
font-size: 0.78rem;
line-height: 1.35;
}}
.cg-calculator-route {{
display: grid;
grid-template-columns: minmax(140px, 0.7fr) minmax(240px, 1.5fr) minmax(220px, 1fr);
gap: 0.7rem;
border: 1px solid #303743;
border-radius: 8px;
background: #0f141b;
padding: 0.7rem;
}}
.cg-route-block {{
min-width: 0;
}}
.cg-route-label {{
color: #94a3b8;
font-size: 0.72rem;
font-weight: 800;
text-transform: uppercase;
letter-spacing: 0;
margin-bottom: 0.25rem;
}}
.cg-route-value {{
color: #f8fafc;
font-size: 0.84rem;
line-height: 1.35;
overflow-wrap: anywhere;
}}
.cg-route-options {{
display: flex;
flex-wrap: wrap;
gap: 0.35rem;
}}
.cg-option-chip {{
border: 1px solid #334155;
border-radius: 999px;
color: #dbeafe;
background: #172033;
padding: 0.16rem 0.45rem;
font-size: 0.75rem;
line-height: 1.2;
}}
@media (max-width: 1100px) {{
.cg-workflow-diagram {{
grid-template-columns: repeat(2, minmax(150px, 1fr));
}}
.cg-stage:not(:last-child)::after {{
display: none;
}}
.cg-calculator-route {{
grid-template-columns: 1fr;
}}
}}
</style>
<div class="cg-workflow-panel">
<div class="cg-workflow-heading">
<span>Workflow diagram</span>
<small>validated tool path, not raw model thoughts</small>
</div>
<div class="cg-workflow-diagram">
{''.join(stage_cards)}
</div>
<div class="cg-calculator-route">
<div class="cg-route-block">
<div class="cg-route-label">Selected calculator</div>
<div class="cg-route-value">{html.escape(calculator_info["selected"])}</div>
</div>
<div class="cg-route-block">
<div class="cg-route-label">Reason</div>
<div class="cg-route-value">{html.escape(calculator_info["reason"])}</div>
</div>
<div class="cg-route-block">
<div class="cg-route-label">Options</div>
<div class="cg-route-options">
{''.join(f'<span class="cg-option-chip">{html.escape(option.strip())}</span>' for option in calculator_info["options"].split(","))}
</div>
</div>
</div>
<div class="cg-workflow-caveat">
Check identity, charge/spin, calculator domain, convergence, and units before using numeric outputs.
</div>
</div>
"""
def _tool_status_parts(label: str) -> tuple[str, str]:
"""Split a compact tool label into tool name and operation detail."""
if ": " not in label:
return (label, "")
tool_name, detail = label.split(": ", 1)
return (tool_name, detail)
def _workflow_tool_status_html(completed: list[str], active: list[dict]) -> str:
"""Build an illustrative tool status board with escaped dynamic labels."""
rows = []
entries = [
("failed" if _is_failure_label(str(label)) else "done", str(label), "failed" if _is_failure_label(str(label)) else "done")
for label in completed
] + [
("running", _trace_entry_label(item), "running")
for item in active
]
for state, label, status in entries:
tool_name, detail = _tool_status_parts(label)
rows.append(
f"""
<div class="cg-tool-row cg-tool-row-{state}">
<div class="cg-tool-dot"></div>
<div class="cg-tool-body">
<div class="cg-tool-name">{html.escape(tool_name)}</div>
<div class="cg-tool-detail">{html.escape(detail or label)}</div>
</div>
<div class="cg-tool-badge">{html.escape(status)}</div>
</div>
"""
)
if not rows:
rows.append(
"""
<div class="cg-tool-row cg-tool-row-waiting">
<div class="cg-tool-dot"></div>
<div class="cg-tool-body">
<div class="cg-tool-name">Waiting</div>
<div class="cg-tool-detail">No tool call has started yet.</div>
</div>
<div class="cg-tool-badge">waiting</div>
</div>
"""
)
return f"""
<style>
.cg-tool-board {{
display: grid;
gap: 0.45rem;
margin-top: 0.7rem;
}}
.cg-tool-board-title {{
color: #f5f7fb;
font-weight: 800;
margin-bottom: 0.15rem;
}}
.cg-tool-row {{
display: grid;
grid-template-columns: 0.8rem minmax(0, 1fr) auto;
align-items: center;
gap: 0.7rem;
border: 1px solid #303743;
border-radius: 8px;
padding: 0.55rem 0.65rem;
background: #10161d;
}}
.cg-tool-dot {{
width: 0.62rem;
height: 0.62rem;
border-radius: 999px;
background: #6b7280;
}}
.cg-tool-name {{
color: #dbe5f0;
font-size: 0.74rem;
font-weight: 800;
text-transform: uppercase;
letter-spacing: 0;
line-height: 1.2;
}}
.cg-tool-detail {{
color: #f8fafc;
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
font-size: 0.82rem;
line-height: 1.25;
overflow-wrap: anywhere;
}}
.cg-tool-badge {{
border-radius: 999px;
padding: 0.16rem 0.55rem;
font-size: 0.72rem;
font-weight: 800;
text-transform: uppercase;
color: #cbd5e1;
background: #273142;
}}
.cg-tool-row-done {{
border-color: #246d49;
background: #0c1b16;
}}
.cg-tool-row-done .cg-tool-dot,
.cg-tool-row-done .cg-tool-badge {{
background: #1d6f46;
color: #eafff3;
}}
.cg-tool-row-done .cg-tool-detail {{
color: #25d66f;
font-weight: 800;
}}
.cg-tool-row-running {{
border-color: #9a7124;
background: #20180c;
}}
.cg-tool-row-running .cg-tool-dot,
.cg-tool-row-running .cg-tool-badge {{
background: #8b641d;
color: #fff5d8;
}}
.cg-tool-row-failed {{
border-color: #9f3a3a;
background: #211010;
}}
.cg-tool-row-failed .cg-tool-dot,
.cg-tool-row-failed .cg-tool-badge {{
background: #8f2f2f;
color: #fff0f0;
}}
.cg-tool-row-failed .cg-tool-detail {{
color: #ffb4b4;
font-weight: 800;
}}
.cg-tool-row-waiting {{
border-color: #394150;
background: #0f141b;
}}
</style>
<div class="cg-tool-board">
<div class="cg-tool-board-title">Tool status</div>
{''.join(rows)}
</div>
"""
def _stream_workflow(stream_input, config, agent, msg_queue):
"""Run the agent workflow in a background thread, pushing events to a queue.
Events pushed:
("tool_call", [tool_names]) — agent is calling tool(s)
("tool_result", tool_name) — a tool finished
("interrupt", question_str)
("done", last_state)
("error", exception)
Parameters
----------
stream_input : dict or Command
Initial workflow input or resume command.
config : dict
LangGraph run configuration.
agent : ChemGraph
Active ChemGraph agent.
msg_queue : queue.Queue
Queue receiving stream events for the UI thread.
"""
from langgraph.errors import GraphInterrupt
async def _run():
"""Stream the workflow and enqueue UI events."""
prev_msgs: list = []
last_st = None
interrupt_val = None
try:
async for s in agent.workflow.astream(
stream_input, stream_mode="values", config=config
):
if "__interrupt__" in s:
int_data = s["__interrupt__"]
if isinstance(int_data, (list, tuple)) and int_data:
interrupt_val = int_data[0].value
elif hasattr(int_data, "value"):
interrupt_val = int_data.value
else:
interrupt_val = {"question": "The workflow needs your input."}
if "messages" in s and s["messages"] != prev_msgs:
new_message = s["messages"][-1]
classified = _classify_message(new_message)
if classified:
msg_queue.put(classified)
prev_msgs = s["messages"]
last_st = s
except GraphInterrupt as gi:
interrupts = gi.args[0] if gi.args else []
if interrupts:
interrupt_val = interrupts[0].value
else:
interrupt_val = {"question": "The workflow needs your input."}
# Check checkpoint for pending interrupts
if interrupt_val is None:
try:
snapshot = agent.workflow.get_state(config)
if snapshot and snapshot.tasks:
for t in snapshot.tasks:
t_interrupts = getattr(t, "interrupts", None)
if t_interrupts:
interrupt_val = t_interrupts[0].value
break
except Exception:
pass
if interrupt_val is not None:
if isinstance(interrupt_val, dict):
q = interrupt_val.get(
"question",
interrupt_val.get("message", str(interrupt_val)),
)
else:
q = str(interrupt_val)
msg_queue.put(("interrupt", q))
else:
msg_queue.put(("done", last_st))
try:
asyncio.run(_run())
except HumanInputRequired as hir:
msg_queue.put(("interrupt", hir.question))
except Exception as exc:
msg_queue.put(("error", exc))
def _poll_and_display(msg_queue, status_container, placeholder, thread):
"""Poll the message queue and render a compact tool-call log.
Uses a single ``st.empty()`` placeholder to re-render the full list
each time, so completed tools get a checkmark and only the active
tool shows a spinner indicator.
Returns:
("done", last_state) | ("interrupt", question) | ("error", exception)
Parameters
----------
msg_queue : queue.Queue
Queue receiving stream events.
status_container : DeltaGenerator
Streamlit status container.
placeholder : DeltaGenerator
Placeholder used for the tool-call log.
thread : threading.Thread
Background stream thread.
Returns
-------
tuple
``("done", state)``, ``("interrupt", question)``, or
``("error", exception)``.
"""
completed: list[str] = [] # tools that finished
active: list[dict] = [] # tools currently running
def _snapshot() -> dict[str, list]:
return {
"completed": list(completed),
"active": [dict(item) for item in active if isinstance(item, dict)],
}
def _render():
"""Render the current tool-call status list."""
with placeholder.container():
st.html(_workflow_trace_diagram_html(completed, active))
st.html(_workflow_tool_status_html(completed, active))
def _active_labels() -> list[str]:
return [_trace_entry_label(item) for item in active]
def _pop_active_for_result(result: Any) -> Optional[dict]:
return _pop_active_trace_match(active, result)
while True:
try:
event_type, event_data = msg_queue.get(timeout=0.1)
except queue.Empty:
if not thread.is_alive():
try:
event_type, event_data = msg_queue.get_nowait()
except queue.Empty:
return ("error", RuntimeError("Stream ended without result."))
else:
continue
if event_type == "tool_call":
active.clear()
active.extend(event_data)
label = ", ".join(_active_labels())
status_container.update(label=f"Running {label}", state="running")
_render()
elif event_type == "tool_result":
# Move this specific tool from active to completed
active_item = _pop_active_for_result(event_data)
result_label = (
str(event_data.get("label"))
if isinstance(event_data, dict) and event_data.get("label")
else _trace_entry_label(active_item or event_data)
)
if result_label not in completed:
completed.append(result_label)
if active:
status_container.update(
label=f"Running {', '.join(_active_labels())}", state="running"
)
else:
status_container.update(label="Thinking...", state="running")
_render()
elif event_type in ("done", "interrupt", "error"):
# Only tool_result events are completed. Unmatched tool calls mean the
# graph stopped before the tool returned and should not render green.
for label in _active_labels():
incomplete = f"{label} (incomplete)"
if incomplete not in completed:
completed.append(incomplete)
active.clear()
_render()
return (event_type, event_data, _snapshot())
def _find_conversation_entry_by_log_dir(log_dir: Optional[str]) -> Optional[dict]:
"""Return the visible exchange tied to a query artifact directory."""
if not log_dir:
return None
for entry in reversed(st.session_state.get("conversation_history", [])):
if entry.get("log_dir") == log_dir:
return entry
return None
def _error_result(error: Any) -> dict:
"""Return a persistable assistant message for workflow-level errors."""
return {
"messages": [
{
"type": "ai",
"content": f"Processing error: {error}",
}
],
"final_output": None,
"scientific_ledger": None,
"validation": None,
"completion_validation": None,
"run_context": None,
}
def _queue_query_submission(
query: str,
thread_id: int,
endpoint_status: dict,
selected_base_url: Optional[str],
) -> None:
"""Reserve a workflow run and rerun so the input renders disabled first."""
if not endpoint_status["ok"]:
msg = (
f"Cannot reach local model endpoint `{selected_base_url}`. "
f"{endpoint_status['message']}"
)
st.session_state.last_run_error = RuntimeError(msg)
st.error(msg)
return
if not st.session_state.agent:
st.error("Agent not ready. Please check configuration and try again.")
return
trimmed_query = query.strip()
if not trimmed_query:
return
if not _begin_agent_run():
st.info("A ChemGraph task is already running. Please wait for it to finish.")
return
agent = st.session_state.agent
_ensure_chat_log_dir()
# Create the persistent sessions while the agent still points to the
# chat-level directory; calculation artifacts go into query dirs below.
try:
agent._ensure_session(trimmed_query)
except Exception:
pass
query_saved = _save_query_to_store(trimmed_query)
query_log_dir = _create_query_log_dir()
run_thread_id = _ensure_graph_thread_id(thread_id)
st.session_state.last_run_query = trimmed_query
st.session_state.last_run_error = None
st.session_state.last_run_result = None
st.session_state.conversation_history.append(
{
"query": trimmed_query,
"result": {"messages": []},
"thread_id": run_thread_id,
"log_dir": query_log_dir,
"status": "running",
}
)
st.session_state.pending_agent_submission = {
"kind": "query",
"query": trimmed_query,
"thread_id": thread_id,
"run_thread_id": run_thread_id,
"query_log_dir": query_log_dir,
"query_saved": query_saved,
}
st.rerun()
def _execute_pending_agent_submission(
thread_id: int,
endpoint_status: dict,
selected_base_url: Optional[str],
) -> None:
"""Run a reserved pending submission after the disabled input is rendered."""
pending = st.session_state.get("pending_agent_submission")
if not pending or pending.get("kind") != "query":
return
_handle_query_submission(
pending["query"],
pending.get("thread_id", thread_id),
endpoint_status,
selected_base_url,
run_reserved=True,
query_log_dir=pending.get("query_log_dir"),
run_thread_id=pending.get("run_thread_id"),
query_saved=bool(pending.get("query_saved")),
)
def _handle_query_submission(
query: str,
thread_id: int,
endpoint_status: dict,
selected_base_url: Optional[str],
*,
run_reserved: bool = False,
query_log_dir: Optional[str] = None,
run_thread_id: Optional[str] = None,
query_saved: bool = False,
) -> None:
"""Handle a submitted user query and stream the workflow response.
Parameters
----------
query : str
User query text.
thread_id : int
Current LangGraph thread ID.
endpoint_status : dict
Local endpoint status dictionary.
selected_base_url : str, optional
Model endpoint URL used in error messages.
"""
if not endpoint_status["ok"]:
msg = (
f"Cannot reach local model endpoint `{selected_base_url}`. "
f"{endpoint_status['message']}"
)
st.session_state.last_run_error = RuntimeError(msg)
st.error(msg)
return
if not st.session_state.agent:
st.error("Agent not ready. Please check configuration and try again.")
return
if not query.strip():
return
trimmed_query = query.strip()
if not _begin_agent_run(from_pending=run_reserved):
st.info("A ChemGraph task is already running. Please wait for it to finish.")
return
agent = st.session_state.agent
old_agent_log_dir = getattr(agent, "log_dir", None)
old_env_log_dir = None
try:
_ensure_chat_log_dir()
old_env_log_dir = os.environ.get("CHEMGRAPH_LOG_DIR")
# Create the persistent session while the agent still points to the
# chat-level directory; only calculation artifacts go into query dirs.
try:
agent._ensure_session(trimmed_query)
except Exception:
pass
if not query_saved:
query_saved = _save_query_to_store(trimmed_query)
query_log_dir = query_log_dir or _create_query_log_dir()
agent.log_dir = query_log_dir
os.environ["CHEMGRAPH_LOG_DIR"] = query_log_dir
run_thread_id = run_thread_id or _ensure_graph_thread_id(thread_id)
cfg = {"configurable": {"thread_id": run_thread_id}}
cfg["recursion_limit"] = agent.recursion_limit
st.session_state.last_run_query = trimmed_query
st.session_state.last_run_error = None
st.session_state.last_run_result = None
pending_entry = _find_conversation_entry_by_log_dir(query_log_dir)
if pending_entry is None:
pending_entry = {
"query": trimmed_query,
"result": {"messages": []},
"thread_id": run_thread_id,
"log_dir": query_log_dir,
"status": "running",
}
st.session_state.conversation_history.append(pending_entry)
else:
pending_entry["status"] = "running"
# Snapshot message count before streaming so we can isolate new messages
prev_msg_count = 0
try:
snapshot = agent.workflow.get_state(cfg)
if snapshot and snapshot.values:
prev_msg_count = len(snapshot.values.get("messages", []))
except Exception:
pass
# Show the user's message immediately
with st.chat_message("user"):
st.markdown(trimmed_query)
# Stream agent response with live tool-call display
with st.chat_message("assistant"):
msg_q: queue.Queue = queue.Queue()
inputs = {"messages": trimmed_query}
stream_thread = threading.Thread(
target=_stream_workflow,
args=(inputs, cfg, agent, msg_q),
daemon=True,
)
status = st.status("Thinking...", expanded=True)
with status:
tool_log = st.empty()
stream_thread.start()
event_type, event_data, workflow_trace = _poll_and_display(
msg_q, status, tool_log, stream_thread
)
stream_thread.join(timeout=5)
if event_type == "done":
last_state = event_data
if last_state is None:
msg = "Workflow produced no output."
result = _error_result(msg)
pending_entry.update(
{
"result": result,
"thread_id": run_thread_id,
"log_dir": query_log_dir,
"workflow_trace": workflow_trace,
"status": "error",
"error": msg,
}
)
_save_exchange_to_store(
trimmed_query,
result,
include_query=not query_saved,
)
st.session_state.pending_agent_submission = None
st.error(msg)
return
# Only keep messages from this query (not prior thread history)
all_msgs = last_state.get("messages", [])
new_msgs = all_msgs[prev_msg_count:]
result = {
"messages": new_msgs,
"final_output": last_state.get("final_output"),
"scientific_ledger": last_state.get("scientific_ledger"),
"validation": last_state.get("validation"),
"completion_validation": last_state.get("completion_validation"),
"run_context": last_state.get("run_context"),
}
workflow_status = _workflow_status_from_result(result)
status.update(
label=_workflow_status_label(workflow_status),
state=_streamlit_state_for_workflow_status(workflow_status),
expanded=False,
)
# Save messages to persistent session store (best-effort)
try:
agent._save_messages_to_store(last_state, trimmed_query)
except Exception:
pass
st.session_state.last_run_result = result
pending_entry.update(
{
"query": trimmed_query,
"result": result,
"thread_id": run_thread_id,
"log_dir": query_log_dir,
"workflow_trace": workflow_trace,
"status": workflow_status,
}
)
_save_exchange_to_store(
trimmed_query,
result,
include_query=not query_saved,
)
st.session_state.query_input = ""
st.session_state.pending_agent_submission = None
st.rerun()
elif event_type == "interrupt":
status.update(
label="Waiting for input", state="complete", expanded=False
)
cfg_for_resume = dict(cfg)
st.session_state.pending_human_question = event_data
st.session_state.pending_interrupt_config = cfg_for_resume
st.session_state.pending_interrupt_query = trimmed_query
st.session_state.pending_interrupt_thread_id = thread_id
st.session_state.pending_interrupt_prev_msg_count = prev_msg_count
st.session_state.pending_interrupt_model = st.session_state.get(
"active_model"
)
st.session_state.pending_interrupt_workflow = st.session_state.get(
"active_workflow"
)
st.session_state.pending_interrupt_log_dir = query_log_dir
st.session_state.interrupt_count = 1
st.session_state.interrupt_exchanges = []
pending_entry.update(
{
"status": "waiting_for_input",
"workflow_trace": workflow_trace,
"pending_human_question": event_data,
"pending_interrupt_config": cfg_for_resume,
"pending_interrupt_query": trimmed_query,
"pending_interrupt_thread_id": thread_id,
"pending_interrupt_prev_msg_count": prev_msg_count,
"pending_interrupt_model": st.session_state.get(
"active_model"
),
"pending_interrupt_workflow": st.session_state.get(
"active_workflow"
),
"pending_interrupt_log_dir": query_log_dir,
"interrupt_count": 1,
"interrupt_exchanges": [],
}
)
st.session_state.pending_agent_submission = None
st.rerun()
else: # error
status.update(label="Error", state="error", expanded=False)
st.session_state.last_run_error = event_data
pending_entry["status"] = "error"
pending_entry["error"] = str(event_data)
pending_entry["workflow_trace"] = workflow_trace
result = _error_result(event_data)
pending_entry["result"] = result
_save_exchange_to_store(
trimmed_query,
result,
include_query=not query_saved,
)
st.session_state.pending_agent_submission = None
st.error(f"Processing error: {event_data}")
st.rerun()
finally:
pending_submission = st.session_state.get("pending_agent_submission")
if (
isinstance(pending_submission, dict)
and pending_submission.get("query_log_dir") == query_log_dir
):
st.session_state.pending_agent_submission = None
_restore_agent_log_context(agent, old_agent_log_dir, old_env_log_dir)
_end_agent_run()
def _handle_human_response(answer: str, thread_id: int) -> None:
"""Resume the agent workflow with the human's answer.
Parameters
----------
answer : str
Human response to the pending interrupt question.
thread_id : int
Current LangGraph thread ID.
"""
from langgraph.types import Command
agent = st.session_state.agent
resume_config = st.session_state.pending_interrupt_config
original_query = st.session_state.pending_interrupt_query
current_question = st.session_state.pending_human_question
interrupt_count = st.session_state.interrupt_count
if agent is None or resume_config is None:
st.error("Agent was re-initialized. Please submit your query again.")
_clear_interrupt_state()
return
if not _begin_agent_run():
st.info("A ChemGraph task is already running. Please wait for it to finish.")
return
MAX_INTERRUPTS = 10
old_agent_log_dir = getattr(agent, "log_dir", None)
old_env_log_dir = os.environ.get("CHEMGRAPH_LOG_DIR")
resume_log_dir = (
st.session_state.get("pending_interrupt_log_dir")
or st.session_state.get("active_query_log_dir")
or _create_query_log_dir()
)
try:
agent.log_dir = resume_log_dir
os.environ["CHEMGRAPH_LOG_DIR"] = resume_log_dir
pending_entry = _find_conversation_entry_by_log_dir(resume_log_dir)
if pending_entry is not None:
pending_entry["status"] = "running"
# Record this exchange
st.session_state.interrupt_exchanges.append(
{"question": current_question, "answer": answer}
)
# Show the user's reply immediately
with st.chat_message("user"):
st.markdown(answer)
# Stream resumed agent response
with st.chat_message("assistant"):
msg_q: queue.Queue = queue.Queue()
resume_cmd = Command(resume=answer)
stream_thread = threading.Thread(
target=_stream_workflow,
args=(resume_cmd, resume_config, agent, msg_q),
daemon=True,
)
status = st.status("Processing your response...", expanded=True)
with status:
tool_log = st.empty()
stream_thread.start()
event_type, event_data, workflow_trace = _poll_and_display(
msg_q, status, tool_log, stream_thread
)
stream_thread.join(timeout=5)
if event_type == "done":
result_state = event_data
if result_state is None:
msg = "Resume produced no output."
final_result = _error_result(msg)
if pending_entry is not None:
pending_entry.update(
{
"query": original_query,
"result": final_result,
"thread_id": thread_id,
"log_dir": resume_log_dir,
"workflow_trace": workflow_trace,
"status": "error",
"error": msg,
}
)
_save_exchange_to_store(
original_query,
final_result,
include_query=pending_entry is None,
)
st.error(msg)
_clear_interrupt_state()
return
# Only keep messages from this query (not prior thread history)
prev_msg_count = st.session_state.get(
"pending_interrupt_prev_msg_count", 0
)
all_msgs = result_state.get("messages", [])
new_msgs = all_msgs[prev_msg_count:]
final_result = {
"messages": new_msgs,
"final_output": result_state.get("final_output"),
"scientific_ledger": result_state.get("scientific_ledger"),
"validation": result_state.get("validation"),
"completion_validation": result_state.get("completion_validation"),
"run_context": result_state.get("run_context"),
}
workflow_status = _workflow_status_from_result(final_result)
status.update(
label=_workflow_status_label(workflow_status),
state=_streamlit_state_for_workflow_status(workflow_status),
expanded=False,
)
exchanges = list(st.session_state.interrupt_exchanges)
st.session_state.last_run_result = final_result
entry_payload = {
"query": original_query,
"result": final_result,
"thread_id": thread_id,
"log_dir": resume_log_dir,
"interrupt_exchanges": exchanges,
"workflow_trace": workflow_trace,
"status": workflow_status,
}
if pending_entry is not None:
pending_entry.update(entry_payload)
else:
st.session_state.conversation_history.append(entry_payload)
_save_exchange_to_store(
original_query,
final_result,
include_query=pending_entry is None,
)
st.session_state.query_input = ""
_clear_interrupt_state()
st.rerun()
elif event_type == "interrupt":
status.update(
label="Waiting for input", state="complete", expanded=False
)
new_count = interrupt_count + 1
if new_count > MAX_INTERRUPTS:
st.error(
"Agent exceeded maximum number of follow-up questions. Aborting."
)
if pending_entry is not None:
pending_entry["status"] = "error"
pending_entry["error"] = "Exceeded maximum interrupts."
_clear_interrupt_state()
return
st.session_state.pending_human_question = event_data
st.session_state.interrupt_count = new_count
if pending_entry is not None:
pending_entry.update(
{
"status": "waiting_for_input",
"workflow_trace": workflow_trace,
"pending_human_question": event_data,
"pending_interrupt_config": resume_config,
"pending_interrupt_query": original_query,
"pending_interrupt_thread_id": thread_id,
"pending_interrupt_prev_msg_count": st.session_state.get(
"pending_interrupt_prev_msg_count", 0
),
"pending_interrupt_model": st.session_state.get(
"pending_interrupt_model"
),
"pending_interrupt_workflow": st.session_state.get(
"pending_interrupt_workflow"
),
"pending_interrupt_log_dir": resume_log_dir,
"interrupt_count": new_count,
"interrupt_exchanges": list(
st.session_state.interrupt_exchanges
),
}
)
st.rerun()
else: # error
status.update(label="Error", state="error", expanded=False)
st.session_state.last_run_error = event_data
final_result = _error_result(event_data)
if pending_entry is not None:
pending_entry["status"] = "error"
pending_entry["error"] = str(event_data)
pending_entry["workflow_trace"] = workflow_trace
pending_entry["result"] = final_result
_save_exchange_to_store(
original_query,
final_result,
include_query=pending_entry is None,
)
st.error(f"Error during resume: {event_data}")
_clear_interrupt_state()
st.rerun()
finally:
_restore_agent_log_context(agent, old_agent_log_dir, old_env_log_dir)
_end_agent_run()
|