Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 | /** * DingTalk Channel Plugin for Moltbot * * 通过钉钉 Stream 模式连接,支持 AI Card 流式响应。 * 完整接入 Moltbot 消息处理管道。 */ import { DWClient, TOPIC_ROBOT } from 'dingtalk-stream'; import axios from 'axios'; import * as fs from 'fs'; import * as path from 'path'; import * as os from 'os'; import type { ClawdbotPluginApi, PluginRuntime, ClawdbotConfig } from 'clawdbot/plugin-sdk'; // ============ 常量 ============ export const id = 'dingtalk-connector'; /** 默认账号 ID,用于标记单账号模式(无 accounts 配置)时的内部标识,映射到 'main' agent */ const DEFAULT_ACCOUNT_ID = '__default__'; let runtime: PluginRuntime | null = null; function getRuntime(): PluginRuntime { if (!runtime) throw new Error('DingTalk runtime not initialized'); return runtime; } // ============ Session 管理 ============ /** 用户会话状态:记录最后活跃时间和当前 session 标识 */ interface UserSession { lastActivity: number; sessionId: string; // 格式: dingtalk-connector:<senderId> 或 dingtalk-connector:<senderId>:<timestamp> } /** 用户会话缓存 Map<senderId, UserSession> */ const userSessions = new Map<string, UserSession>(); /** 消息去重缓存 Map<messageId, timestamp> - 防止同一消息被重复处理 */ const processedMessages = new Map<string, number>(); /** 消息去重缓存过期时间(5分钟) */ const MESSAGE_DEDUP_TTL = 5 * 60 * 1000; /** 清理过期的消息去重缓存 */ function cleanupProcessedMessages(): void { const now = Date.now(); for (const [msgId, timestamp] of processedMessages.entries()) { if (now - timestamp > MESSAGE_DEDUP_TTL) { processedMessages.delete(msgId); } } } /** 检查消息是否已处理过(去重) */ function isMessageProcessed(messageId: string): boolean { if (!messageId) return false; return processedMessages.has(messageId); } /** 标记消息为已处理 */ function markMessageProcessed(messageId: string): void { if (!messageId) return; processedMessages.set(messageId, Date.now()); // 定期清理(每处理100条消息清理一次) if (processedMessages.size >= 100) { cleanupProcessedMessages(); } } /** 新会话触发命令 */ const NEW_SESSION_COMMANDS = ['/new', '/reset', '/clear', '新会话', '重新开始', '清空对话']; /** 检查消息是否是新会话命令 */ function normalizeSlashCommand(text: string): string { const trimmed = text.trim(); const lower = trimmed.toLowerCase(); if (NEW_SESSION_COMMANDS.some(cmd => lower === cmd.toLowerCase())) { return '/new'; } return text; } /** * OpenClaw 标准会话上下文 * 遵循 OpenClaw session.dmScope 机制,让 Gateway 根据配置自动处理会话隔离 */ interface SessionContext { channel: 'dingtalk-connector'; accountId: string; chatType: 'direct' | 'group'; peerId: string; conversationId?: string; senderName?: string; groupSubject?: string; } /** * 构建 OpenClaw 标准会话上下文 * 遵循 OpenClaw session.dmScope 机制,让 Gateway 根据配置自动处理会话隔离 * * @param separateSessionByConversation - 是否按单聊/群聊/群区分 session(默认 true) * - true: 单聊、群聊、不同群各自拥有独立的 session * - false: 按用户维度维护 session,不区分单聊/群聊(兼容旧行为) * @param groupSessionScope - 群聊会话隔离策略(仅当 separateSessionByConversation=true 时生效) * - 'group': 整个群共享一个会话(默认) * - 'group_sender': 群内每个用户独立会话 */ function buildSessionContext(params: { accountId: string; senderId: string; senderName?: string; conversationType: string; conversationId?: string; groupSubject?: string; separateSessionByConversation?: boolean; groupSessionScope?: 'group' | 'group_sender'; }): SessionContext { const { accountId, senderId, senderName, conversationType, conversationId, groupSubject, separateSessionByConversation, groupSessionScope } = params; const isDirect = conversationType === '1'; // separateSessionByConversation=false 时,不区分单聊/群聊,按用户维度维护 session if (separateSessionByConversation === false) { return { channel: 'dingtalk-connector', accountId, chatType: isDirect ? 'direct' : 'group', peerId: senderId, // 只用 senderId,不区分会话 senderName, }; } // 以下是 separateSessionByConversation=true(默认)的逻辑 if (isDirect) { // 单聊:peerId 为发送者 ID,由 OpenClaw Gateway 根据 dmScope 配置处理 return { channel: 'dingtalk-connector', accountId, chatType: 'direct', peerId: senderId, senderName, }; } // 群聊:根据 groupSessionScope 配置决定会话隔离策略 if (groupSessionScope === 'group_sender') { // 群内每个用户独立会话 return { channel: 'dingtalk-connector', accountId, chatType: 'group', peerId: `${conversationId}:${senderId}`, conversationId, senderName, groupSubject, }; } // 默认:整个群共享一个会话 return { channel: 'dingtalk-connector', accountId, chatType: 'group', peerId: conversationId || senderId, conversationId, senderName, groupSubject, }; } // ============ Access Token 缓存 ============ let accessToken: string | null = null; let accessTokenExpiry = 0; async function getAccessToken(config: any): Promise<string> { const now = Date.now(); if (accessToken && accessTokenExpiry > now + 60_000) { return accessToken; } const response = await axios.post('https://api.dingtalk.com/v1.0/oauth2/accessToken', { appKey: config.clientId, appSecret: config.clientSecret, }); accessToken = response.data.accessToken; accessTokenExpiry = now + (response.data.expireIn * 1000); return accessToken!; } // ============ 配置工具 ============ function getConfig(cfg: ClawdbotConfig) { return (cfg?.channels as any)?.['dingtalk-connector'] || {}; } function isConfigured(cfg: ClawdbotConfig): boolean { const config = getConfig(cfg); return Boolean(config.clientId && config.clientSecret); } // ============ 钉钉图片上传 ============ async function getOapiAccessToken(config: any): Promise<string | null> { try { const resp = await axios.get('https://oapi.dingtalk.com/gettoken', { params: { appkey: config.clientId, appsecret: config.clientSecret }, }); if (resp.data?.errcode === 0) return resp.data.access_token; return null; } catch { return null; } } /** staffId → unionId 缓存 */ const unionIdCache = new Map<string, string>(); /** * 通过 oapi 旧版接口将 staffId 转换为 unionId */ async function getUnionId(staffId: string, config: any, log?: any): Promise<string | null> { const cached = unionIdCache.get(staffId); if (cached) return cached; try { const token = await getOapiAccessToken(config); if (!token) { log?.error?.('[DingTalk] getUnionId: 无法获取 oapi access_token'); return null; } const resp = await axios.get(`${DINGTALK_OAPI}/user/get`, { params: { access_token: token, userid: staffId }, timeout: 10_000, }); const unionId = resp.data?.unionid; if (unionId) { unionIdCache.set(staffId, unionId); log?.info?.(`[DingTalk] getUnionId: ${staffId} → ${unionId}`); return unionId; } log?.error?.(`[DingTalk] getUnionId: 响应中无 unionid 字段: ${JSON.stringify(resp.data)}`); return null; } catch (err: any) { log?.error?.(`[DingTalk] getUnionId 失败: ${err.message}`); return null; } } function buildMediaSystemPrompt(): string { return `## 钉钉图片和文件显示规则 你正在钉钉中与用户对话。 ### 一、图片显示 显示图片时,直接使用本地文件路径,系统会自动上传处理。 **正确方式**: \`\`\`markdown    \`\`\` **禁止**: - 不要自己执行 curl 上传 - 不要猜测或构造 URL - **不要对路径进行转义(如使用反斜杠 \\ )** 直接输出本地路径即可,系统会自动上传到钉钉。 ### 二、视频分享 **何时分享视频**: - ✅ 用户明确要求**分享、发送、上传**视频时 - ❌ 仅生成视频保存到本地时,**不需要**分享 **视频标记格式**: 当需要分享视频时,在回复**末尾**添加: \`\`\` [DINGTALK_VIDEO]{"path":"<本地视频路径>"}[/DINGTALK_VIDEO] \`\`\` **支持格式**:mp4(最大 20MB) **重要**: - 视频大小不得超过 20MB,超过限制时告知用户 - 仅支持 mp4 格式 - 系统会自动提取视频时长、分辨率并生成封面 ### 三、音频分享 **何时分享音频**: - ✅ 用户明确要求**分享、发送、上传**音频/语音文件时 - ❌ 仅生成音频保存到本地时,**不需要**分享 **音频标记格式**: 当需要分享音频时,在回复**末尾**添加: \`\`\` [DINGTALK_AUDIO]{"path":"<本地音频路径>"}[/DINGTALK_AUDIO] \`\`\` **支持格式**:ogg、amr(最大 20MB) **重要**: - 音频大小不得超过 20MB,超过限制时告知用户 - 系统会自动提取音频时长 ### 四、文件分享 **何时分享文件**: - ✅ 用户明确要求**分享、发送、上传**文件时 - ❌ 仅生成文件保存到本地时,**不需要**分享 **文件标记格式**: 当需要分享文件时,在回复**末尾**添加: \`\`\` [DINGTALK_FILE]{"path":"<本地文件路径>","fileName":"<文件名>","fileType":"<扩展名>"}[/DINGTALK_FILE] \`\`\` **支持的文件类型**:几乎所有常见格式 **重要**:文件大小不得超过 20MB,超过限制时告知用户文件过大。`; } // ============ 图片后处理:自动上传本地图片到钉钉 ============ /** * 匹配 markdown 图片中的本地文件路径(跨平台): * -  * -  * -  * macOS: * -  * -  * -  * Linux: * -  * -  * Windows: * -  * -  */ const LOCAL_IMAGE_RE = /!\[([^\]]*)\]\(((?:file:\/\/\/|MEDIA:|attachment:\/\/\/)[^)]+|\/(?:tmp|var|private|Users|home|root)[^)]+|[A-Za-z]:[\\/ ][^)]+)\)/g; /** 图片文件扩展名 */ const IMAGE_EXTENSIONS = /\.(png|jpg|jpeg|gif|bmp|webp|tiff|svg)$/i; /** * 匹配纯文本中的本地图片路径(不在 markdown 图片语法中,跨平台): * macOS: * - `/var/folders/.../screenshot.png` * - `/tmp/image.jpg` * - `/Users/xxx/photo.png` * Linux: * - `/home/user/photo.png` * - `/root/photo.png` * Windows: * - `C:\Users\xxx\photo.png` * - `C:/temp/image.jpg` * 支持 backtick 包裹: `path` */ const BARE_IMAGE_PATH_RE = /`?((?:\/(?:tmp|var|private|Users|home|root)\/[^\s`'",)]+|[A-Za-z]:[\\/][^\s`'",)]+)\.(?:png|jpg|jpeg|gif|bmp|webp))`?/gi; /** 去掉 file:// / MEDIA: / attachment:// 前缀,得到实际的绝对路径 */ function toLocalPath(raw: string): string { let path = raw; if (path.startsWith('file://')) path = path.replace('file://', ''); else if (path.startsWith('MEDIA:')) path = path.replace('MEDIA:', ''); else if (path.startsWith('attachment://')) path = path.replace('attachment://', ''); // 解码 URL 编码的路径(如中文字符 %E5%9B%BE → 图) try { path = decodeURIComponent(path); } catch { // 解码失败则保持原样 } return path; } /** * 通用媒体文件上传函数 * @param filePath 文件路径 * @param mediaType 媒体类型:image, file, video, voice * @param oapiToken 钉钉 access_token * @param maxSize 最大文件大小(字节),默认 20MB * @param log 日志对象 * @returns media_id 或 null */ async function uploadMediaToDingTalk( filePath: string, mediaType: 'image' | 'file' | 'video' | 'voice', oapiToken: string, maxSize: number = 20 * 1024 * 1024, log?: any, ): Promise<string | null> { try { const fs = await import('fs'); const path = await import('path'); const FormData = (await import('form-data')).default; const absPath = toLocalPath(filePath); if (!fs.existsSync(absPath)) { log?.warn?.(`[DingTalk][${mediaType}] 文件不存在: ${absPath}`); return null; } // 检查文件大小 const stats = fs.statSync(absPath); const fileSizeMB = (stats.size / (1024 * 1024)).toFixed(2); if (stats.size > maxSize) { const maxSizeMB = (maxSize / (1024 * 1024)).toFixed(0); log?.warn?.(`[DingTalk][${mediaType}] 文件过大: ${absPath}, 大小: ${fileSizeMB}MB, 超过限制 ${maxSizeMB}MB`); return null; } const form = new FormData(); form.append('media', fs.createReadStream(absPath), { filename: path.basename(absPath), contentType: mediaType === 'image' ? 'image/jpeg' : 'application/octet-stream', }); log?.info?.(`[DingTalk][${mediaType}] 上传文件: ${absPath} (${fileSizeMB}MB)`); const resp = await axios.post( `https://oapi.dingtalk.com/media/upload?access_token=${oapiToken}&type=${mediaType}`, form, { headers: form.getHeaders(), timeout: 60_000 }, ); const mediaId = resp.data?.media_id; if (mediaId) { log?.info?.(`[DingTalk][${mediaType}] 上传成功: media_id=${mediaId}`); return mediaId; } log?.warn?.(`[DingTalk][${mediaType}] 上传返回无 media_id: ${JSON.stringify(resp.data)}`); return null; } catch (err: any) { log?.error?.(`[DingTalk][${mediaType}] 上传失败: ${err.message}`); return null; } } /** 扫描内容中的本地图片路径,上传到钉钉并替换为 media_id */ async function processLocalImages( content: string, oapiToken: string | null, log?: any, ): Promise<string> { if (!oapiToken) { log?.warn?.(`[DingTalk][Media] 无 oapiToken,跳过图片后处理`); return content; } let result = content; // 第一步:匹配 markdown 图片语法  const mdMatches = [...content.matchAll(LOCAL_IMAGE_RE)]; if (mdMatches.length > 0) { log?.info?.(`[DingTalk][Media] 检测到 ${mdMatches.length} 个 markdown 图片,开始上传...`); for (const match of mdMatches) { const [fullMatch, alt, rawPath] = match; // 清理转义字符(AI 可能会对含空格的路径添加 \ ) const cleanPath = rawPath.replace(/\\ /g, ' '); const mediaId = await uploadMediaToDingTalk(cleanPath, 'image', oapiToken, 20 * 1024 * 1024, log); if (mediaId) { result = result.replace(fullMatch, ``); } } } // 第二步:匹配纯文本中的本地图片路径(如 `/var/folders/.../xxx.png`) // 排除已被 markdown 图片语法包裹的路径 const bareMatches = [...result.matchAll(BARE_IMAGE_PATH_RE)]; const newBareMatches = bareMatches.filter(m => { // 检查这个路径是否已经在  中 const idx = m.index!; const before = result.slice(Math.max(0, idx - 10), idx); return !before.includes(']('); }); if (newBareMatches.length > 0) { log?.info?.(`[DingTalk][Media] 检测到 ${newBareMatches.length} 个纯文本图片路径,开始上传...`); // 从后往前替换,避免 index 偏移 for (const match of newBareMatches.reverse()) { const [fullMatch, rawPath] = match; log?.info?.(`[DingTalk][Media] 纯文本图片: "${fullMatch}" -> path="${rawPath}"`); const mediaId = await uploadMediaToDingTalk(rawPath, 'image', oapiToken, 20 * 1024 * 1024, log); if (mediaId) { const replacement = ``; result = result.slice(0, match.index!) + result.slice(match.index!).replace(fullMatch, replacement); log?.info?.(`[DingTalk][Media] 替换纯文本路径为图片: ${replacement}`); } } } if (mdMatches.length === 0 && newBareMatches.length === 0) { log?.info?.(`[DingTalk][Media] 未检测到本地图片路径`); } return result; } // ============ 文件后处理:提取文件标记并发送独立消息 ============ /** * 文件标记正则:[DINGTALK_FILE]{"path":"...","fileName":"...","fileType":"..."}[/DINGTALK_FILE] */ const FILE_MARKER_PATTERN = /\[DINGTALK_FILE\]({.*?})\[\/DINGTALK_FILE\]/g; /** 视频大小限制:20MB */ const MAX_VIDEO_SIZE = 20 * 1024 * 1024; // ============ 视频后处理:提取视频标记并发送视频消息 ============ /** * 视频标记正则:[DINGTALK_VIDEO]{"path":"..."}[/DINGTALK_VIDEO] */ const VIDEO_MARKER_PATTERN = /\[DINGTALK_VIDEO\]({.*?})\[\/DINGTALK_VIDEO\]/g; /** * 音频标记正则:[DINGTALK_AUDIO]{"path":"..."}[/DINGTALK_AUDIO] */ const AUDIO_MARKER_PATTERN = /\[DINGTALK_AUDIO\]({.*?})\[\/DINGTALK_AUDIO\]/g; /** 视频信息接口 */ interface VideoInfo { path: string; } /** 视频元数据接口 */ interface VideoMetadata { duration: number; width: number; height: number; } /** * 提取视频元数据(时长、分辨率) */ async function extractVideoMetadata( filePath: string, log?: any, ): Promise<VideoMetadata | null> { try { const ffmpeg = require('fluent-ffmpeg'); const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path; ffmpeg.setFfmpegPath(ffmpegPath); return new Promise((resolve) => { ffmpeg.ffprobe(filePath, (err: any, metadata: any) => { if (err) { log?.error?.(`[DingTalk][Video] 提取元数据失败: ${err.message}`); return resolve({ duration: 0, width: 0, height: 0 }); } const videoStream = metadata.streams.find((s: any) => s.codec_type === 'video'); if (!videoStream) { log?.warn?.(`[DingTalk][Video] 未找到视频流`); return resolve({ duration: 0, width: 0, height: 0 }); } const result = { duration: Math.floor(metadata.format.duration || 0), width: videoStream.width || 0, height: videoStream.height || 0, }; log?.info?.(`[DingTalk][Video] 元数据: duration=${result.duration}s, ${result.width}x${result.height}`); resolve(result); }); }); } catch (err: any) { log?.error?.(`[DingTalk][Video] ffprobe 失败: ${err.message}`); return { duration: 0, width: 0, height: 0 }; } } /** * 生成视频封面图(第1秒截图) */ async function extractVideoThumbnail( videoPath: string, outputPath: string, log?: any, ): Promise<string | null> { try { const ffmpeg = require('fluent-ffmpeg'); const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path; const path = await import('path'); ffmpeg.setFfmpegPath(ffmpegPath); return new Promise((resolve) => { ffmpeg(videoPath) .screenshots({ count: 1, folder: path.dirname(outputPath), filename: path.basename(outputPath), timemarks: ['1'], size: '?x360', }) .on('end', () => { log?.info?.(`[DingTalk][Video] 封面生成成功: ${outputPath}`); resolve(outputPath); }) .on('error', (err: any) => { log?.error?.(`[DingTalk][Video] 封面生成失败: ${err.message}`); resolve(null); }); }); } catch (err: any) { log?.error?.(`[DingTalk][Video] ffmpeg 失败: ${err.message}`); return null; } } /** * 发送视频消息到钉钉 */ async function sendVideoMessage( config: any, sessionWebhook: string, videoInfo: VideoInfo, videoMediaId: string, picMediaId: string, metadata: VideoMetadata, oapiToken: string, log?: any, ): Promise<void> { try { const path = await import('path'); const fileName = path.basename(videoInfo.path); const payload = { msgtype: 'video', video: { duration: metadata.duration.toString(), videoMediaId: videoMediaId, videoType: 'mp4', picMediaId: picMediaId, }, }; log?.info?.(`[DingTalk][Video] 发送视频消息: ${fileName}, payload: ${JSON.stringify(payload)}`); const resp = await axios.post(sessionWebhook, payload, { headers: { 'x-acs-dingtalk-access-token': oapiToken, 'Content-Type': 'application/json', }, timeout: 10_000, }); if (resp.data?.success !== false) { log?.info?.(`[DingTalk][Video] 视频消息发送成功: ${fileName}`); } else { log?.error?.(`[DingTalk][Video] 视频消息发送失败: ${JSON.stringify(resp.data)}`); } } catch (err: any) { log?.error?.(`[DingTalk][Video] 发送失败: ${err.message}`); } } /** * 视频后处理主函数 * 返回移除标记后的内容,并附带视频处理的状态提示 * * @param useProactiveApi 是否使用主动消息 API(用于 AI Card 场景) * @param target 主动 API 需要的目标信息(useProactiveApi=true 时必须提供) */ async function processVideoMarkers( content: string, sessionWebhook: string, config: any, oapiToken: string | null, log?: any, useProactiveApi: boolean = false, target?: AICardTarget, ): Promise<string> { const logPrefix = useProactiveApi ? '[DingTalk][Video][Proactive]' : '[DingTalk][Video]'; if (!oapiToken) { log?.warn?.(`${logPrefix} 无 oapiToken,跳过视频处理`); return content; } const fs = await import('fs'); const path = await import('path'); const os = await import('os'); // 提取视频标记 const matches = [...content.matchAll(VIDEO_MARKER_PATTERN)]; const videoInfos: VideoInfo[] = []; const invalidVideos: string[] = []; for (const match of matches) { try { const videoInfo = JSON.parse(match[1]) as VideoInfo; if (videoInfo.path && fs.existsSync(videoInfo.path)) { videoInfos.push(videoInfo); log?.info?.(`${logPrefix} 提取到视频: ${videoInfo.path}`); } else { invalidVideos.push(videoInfo.path || '未知路径'); log?.warn?.(`${logPrefix} 视频文件不存在: ${videoInfo.path}`); } } catch (err: any) { log?.warn?.(`${logPrefix} 解析标记失败: ${err.message}`); } } if (videoInfos.length === 0 && invalidVideos.length === 0) { log?.info?.(`${logPrefix} 未检测到视频标记`); return content.replace(VIDEO_MARKER_PATTERN, '').trim(); } // 先移除所有视频标记,保留其他文本内容 let cleanedContent = content.replace(VIDEO_MARKER_PATTERN, '').trim(); // 收集处理结果状态 const statusMessages: string[] = []; // 处理无效视频 for (const invalidPath of invalidVideos) { statusMessages.push(`⚠️ 视频文件不存在: ${path.basename(invalidPath)}`); } if (videoInfos.length > 0) { log?.info?.(`${logPrefix} 检测到 ${videoInfos.length} 个视频,开始处理...`); } // 逐个处理视频 for (const videoInfo of videoInfos) { const fileName = path.basename(videoInfo.path); let thumbnailPath = ''; try { // 1. 提取元数据 const metadata = await extractVideoMetadata(videoInfo.path, log); if (!metadata) { log?.warn?.(`${logPrefix} 无法提取元数据: ${videoInfo.path}`); statusMessages.push(`⚠️ 视频处理失败: ${fileName}(无法读取视频信息,请检查 ffmpeg 是否已安装)`); continue; } // 2. 生成封面 thumbnailPath = path.join(os.tmpdir(), `thumbnail_${Date.now()}.jpg`); const thumbnail = await extractVideoThumbnail(videoInfo.path, thumbnailPath, log); if (!thumbnail) { log?.warn?.(`${logPrefix} 无法生成封面: ${videoInfo.path}`); statusMessages.push(`⚠️ 视频处理失败: ${fileName}(无法生成封面)`); continue; } // 3. 上传视频 const videoMediaId = await uploadMediaToDingTalk(videoInfo.path, 'video', oapiToken, MAX_VIDEO_SIZE, log); if (!videoMediaId) { log?.warn?.(`${logPrefix} 视频上传失败: ${videoInfo.path}`); statusMessages.push(`⚠️ 视频上传失败: ${fileName}(文件可能超过 20MB 限制)`); continue; } // 4. 上传封面 const picMediaId = await uploadMediaToDingTalk(thumbnailPath, 'image', oapiToken, 20 * 1024 * 1024, log); if (!picMediaId) { log?.warn?.(`${logPrefix} 封面上传失败: ${thumbnailPath}`); statusMessages.push(`⚠️ 视频封面上传失败: ${fileName}`); continue; } // 5. 发送视频消息 if (useProactiveApi && target) { await sendVideoProactive(config, target, videoMediaId, picMediaId, metadata, log); } else { await sendVideoMessage(config, sessionWebhook, videoInfo, videoMediaId, picMediaId, metadata, oapiToken, log); } log?.info?.(`${logPrefix} 视频处理完成: ${fileName}`); statusMessages.push(`✅ 视频已发送: ${fileName}`); } catch (err: any) { log?.error?.(`${logPrefix} 处理视频失败: ${err.message}`); statusMessages.push(`⚠️ 视频处理异常: ${fileName}(${err.message})`); } finally { // 统一清理临时文件 if (thumbnailPath) { try { fs.unlinkSync(thumbnailPath); } catch { // 文件可能不存在,忽略删除错误 } } } } // 将状态信息附加到清理后的内容 if (statusMessages.length > 0) { const statusText = statusMessages.join('\n'); cleanedContent = cleanedContent ? `${cleanedContent}\n\n${statusText}` : statusText; } return cleanedContent; } /** 音频文件扩展名 */ const AUDIO_EXTENSIONS = ['mp3', 'wav', 'amr', 'ogg', 'aac', 'flac', 'm4a']; /** 判断是否为音频文件 */ function isAudioFile(fileType: string): boolean { return AUDIO_EXTENSIONS.includes(fileType.toLowerCase()); } /** 文件大小限制:20MB(字节) */ const MAX_FILE_SIZE = 20 * 1024 * 1024; /** 文件信息接口 */ interface FileInfo { path: string; // 本地文件路径 fileName: string; // 文件名 fileType: string; // 文件类型(扩展名) } /** * 从内容中提取文件标记 * @returns { cleanedContent, fileInfos } */ function extractFileMarkers(content: string, log?: any): { cleanedContent: string; fileInfos: FileInfo[] } { const fileInfos: FileInfo[] = []; const matches = [...content.matchAll(FILE_MARKER_PATTERN)]; for (const match of matches) { try { const fileInfo = JSON.parse(match[1]) as FileInfo; // 验证必需字段 if (fileInfo.path && fileInfo.fileName) { fileInfos.push(fileInfo); log?.info?.(`[DingTalk][File] 提取到文件标记: ${fileInfo.fileName}`); } } catch (err: any) { log?.warn?.(`[DingTalk][File] 解析文件标记失败: ${match[1]}, 错误: ${err.message}`); } } // 移除文件标记,返回清理后的内容 const cleanedContent = content.replace(FILE_MARKER_PATTERN, '').trim(); return { cleanedContent, fileInfos }; } /** * 发送文件消息到钉钉 */ async function sendFileMessage( config: any, sessionWebhook: string, fileInfo: FileInfo, mediaId: string, oapiToken: string, log?: any, ): Promise<void> { try { const fileMessage = { msgtype: 'file', file: { mediaId: mediaId, fileName: fileInfo.fileName, fileType: fileInfo.fileType, }, }; log?.info?.(`[DingTalk][File] 发送文件消息: ${fileInfo.fileName}`); const resp = await axios.post(sessionWebhook, fileMessage, { headers: { 'x-acs-dingtalk-access-token': oapiToken, 'Content-Type': 'application/json', }, timeout: 10_000, }); if (resp.data?.success !== false) { log?.info?.(`[DingTalk][File] 文件消息发送成功: ${fileInfo.fileName}`); } else { log?.error?.(`[DingTalk][File] 文件消息发送失败: ${JSON.stringify(resp.data)}`); } } catch (err: any) { log?.error?.(`[DingTalk][File] 发送文件消息异常: ${fileInfo.fileName}, 错误: ${err.message}`); } } /** * 获取 ffprobe 可执行文件路径 * 优先级: @ffprobe-installer/ffprobe > FFPROBE_PATH 环境变量 > 系统 PATH */ function getFfprobePath(): string { // 1. 尝试 @ffprobe-installer/ffprobe 包 try { const ffprobePath = require('@ffprobe-installer/ffprobe').path; if (ffprobePath) return ffprobePath; } catch { /* 未安装,跳过 */ } // 2. 尝试环境变量 if (process.env.FFPROBE_PATH) return process.env.FFPROBE_PATH; // 3. fallback 到系统 PATH return 'ffprobe'; } /** * 提取音频文件时长(毫秒) * 使用 ffprobe CLI 直接获取,避免 fluent-ffmpeg 在部分运行环境中回调不触发的问题 */ async function extractAudioDuration( filePath: string, log?: any, ): Promise<number | null> { try { const { execFile } = await import('child_process'); const ffprobeBin = getFfprobePath(); return new Promise((resolve) => { execFile(ffprobeBin, [ '-v', 'quiet', '-print_format', 'json', '-show_format', filePath, ], { timeout: 10_000 }, (err: any, stdout: string, stderr: string) => { if (err) { log?.error?.(`[DingTalk][Audio] ffprobe 执行失败 (${ffprobeBin}): ${err.message}`); return resolve(null); } try { const parsed = JSON.parse(stdout); const durationSec = parseFloat(parsed?.format?.duration); if (isNaN(durationSec)) { log?.warn?.(`[DingTalk][Audio] 无法解析音频时长,ffprobe 输出: ${stdout.slice(0, 200)}`); return resolve(null); } const durationMs = Math.floor(durationSec * 1000); log?.info?.(`[DingTalk][Audio] 音频时长: ${durationMs}ms (${durationSec}s)`); resolve(durationMs); } catch (parseErr: any) { log?.error?.(`[DingTalk][Audio] ffprobe 输出解析失败: ${parseErr.message}`); resolve(null); } }); }); } catch (err: any) { log?.error?.(`[DingTalk][Audio] extractAudioDuration 异常: ${err.message}`); return null; } } /** * 发送音频消息到钉钉(被动回复场景) */ async function sendAudioMessage( config: any, sessionWebhook: string, fileInfo: FileInfo, mediaId: string, oapiToken: string, log?: any, durationMs?: number, ): Promise<void> { try { // 钉钉语音消息格式 const actualDuration = (durationMs && durationMs > 0) ? durationMs.toString() : '60000'; const audioMessage = { msgtype: 'voice', voice: { mediaId: mediaId, duration: actualDuration, }, }; log?.info?.(`[DingTalk][Audio] 发送语音消息: ${fileInfo.fileName}`); const resp = await axios.post(sessionWebhook, audioMessage, { headers: { 'x-acs-dingtalk-access-token': oapiToken, 'Content-Type': 'application/json', }, timeout: 10_000, }); if (resp.data?.success !== false) { log?.info?.(`[DingTalk][Audio] 语音消息发送成功: ${fileInfo.fileName}`); } else { log?.error?.(`[DingTalk][Audio] 语音消息发送失败: ${JSON.stringify(resp.data)}`); } } catch (err: any) { log?.error?.(`[DingTalk][Audio] 发送语音消息异常: ${fileInfo.fileName}, 错误: ${err.message}`); } } /** * 处理文件标记:提取、上传、发送独立消息 * 返回移除标记后的内容,并附带文件处理的状态提示 * * @param useProactiveApi 是否使用主动消息 API(用于 AI Card 场景,避免 sessionWebhook 失效问题) * @param target 主动 API 需要的目标信息(useProactiveApi=true 时必须提供) */ async function processFileMarkers( content: string, sessionWebhook: string, config: any, oapiToken: string | null, log?: any, useProactiveApi: boolean = false, target?: AICardTarget, ): Promise<string> { if (!oapiToken) { log?.warn?.(`[DingTalk][File] 无 oapiToken,跳过文件处理`); return content; } const { cleanedContent, fileInfos } = extractFileMarkers(content, log); if (fileInfos.length === 0) { log?.info?.(`[DingTalk][File] 未检测到文件标记`); return cleanedContent; } log?.info?.(`[DingTalk][File] 检测到 ${fileInfos.length} 个文件标记,开始处理... (useProactiveApi=${useProactiveApi})`); const statusMessages: string[] = []; const fs = await import('fs'); // 逐个上传并发送文件消息 for (const fileInfo of fileInfos) { // 预检查:文件是否存在、是否超限 const absPath = toLocalPath(fileInfo.path); if (!fs.existsSync(absPath)) { statusMessages.push(`⚠️ 文件不存在: ${fileInfo.fileName}`); continue; } const stats = fs.statSync(absPath); if (stats.size > MAX_FILE_SIZE) { const sizeMB = (stats.size / (1024 * 1024)).toFixed(1); const maxMB = (MAX_FILE_SIZE / (1024 * 1024)).toFixed(0); statusMessages.push(`⚠️ 文件过大无法发送: ${fileInfo.fileName}(${sizeMB}MB,限制 ${maxMB}MB)`); continue; } // 区分音频文件和普通文件 if (isAudioFile(fileInfo.fileType)) { // 音频文件使用 voice 类型上传 const mediaId = await uploadMediaToDingTalk(fileInfo.path, 'voice', oapiToken, MAX_FILE_SIZE, log); if (mediaId) { // 提取音频实际时长 const audioDurationMs = await extractAudioDuration(fileInfo.path, log); if (useProactiveApi && target) { // 使用主动消息 API(适用于 AI Card 场景) await sendAudioProactive(config, target, fileInfo, mediaId, log, audioDurationMs ?? undefined); } else { // 使用 sessionWebhook(传统被动回复场景) await sendAudioMessage(config, sessionWebhook, fileInfo, mediaId, oapiToken, log, audioDurationMs ?? undefined); } statusMessages.push(`✅ 音频已发送: ${fileInfo.fileName}`); } else { log?.error?.(`[DingTalk][Audio] 音频上传失败,跳过发送: ${fileInfo.fileName}`); statusMessages.push(`⚠️ 音频上传失败: ${fileInfo.fileName}`); } } else { // 普通文件 const mediaId = await uploadMediaToDingTalk(fileInfo.path, 'file', oapiToken, MAX_FILE_SIZE, log); if (mediaId) { if (useProactiveApi && target) { // 使用主动消息 API(适用于 AI Card 场景) await sendFileProactive(config, target, fileInfo, mediaId, log); } else { // 使用 sessionWebhook(传统被动回复场景) await sendFileMessage(config, sessionWebhook, fileInfo, mediaId, oapiToken, log); } statusMessages.push(`✅ 文件已发送: ${fileInfo.fileName}`); } else { log?.error?.(`[DingTalk][File] 文件上传失败,跳过发送: ${fileInfo.fileName}`); statusMessages.push(`⚠️ 文件上传失败: ${fileInfo.fileName}`); } } } // 将状态信息附加到清理后的内容 if (statusMessages.length > 0) { const statusText = statusMessages.join('\n'); return cleanedContent ? `${cleanedContent}\n\n${statusText}` : statusText; } return cleanedContent; } // ============ AI Card Streaming ============ const DINGTALK_API = 'https://api.dingtalk.com'; const DINGTALK_OAPI = 'https://oapi.dingtalk.com'; const AI_CARD_TEMPLATE_ID = '02fcf2f4-5e02-4a85-b672-46d1f715543e.schema'; // flowStatus 值与 Python SDK AICardStatus 一致(cardParamMap 的值必须是字符串) const AICardStatus = { PROCESSING: '1', INPUTING: '2', FINISHED: '3', EXECUTING: '4', FAILED: '5', } as const; interface AICardInstance { cardInstanceId: string; accessToken: string; inputingStarted: boolean; } /** * 创建 AI Card 实例(被动回复场景) * 从钉钉回调 data 中提取目标信息,委托给通用函数 */ async function createAICard( config: any, data: any, log?: any, ): Promise<AICardInstance | null> { const isGroup = data.conversationType === '2'; log?.info?.(`[DingTalk][AICard] conversationType=${data.conversationType}, conversationId=${data.conversationId}, senderStaffId=${data.senderStaffId}, senderId=${data.senderId}`); // 构建通用目标 const target: AICardTarget = isGroup ? { type: 'group', openConversationId: data.conversationId } : { type: 'user', userId: data.senderStaffId || data.senderId }; return createAICardForTarget(config, target, log); } /** * 确保 Markdown 表格前有空行,否则钉钉无法正确渲染表格。 * * 逐行向前看:当前行像表头(含 `|`)且下一行是分隔行时, * 若前一行非空且非表格行,则在表头前插入空行。 * 支持缩进表格(行首有空白字符)。 */ function ensureTableBlankLines(text: string): string { const lines = text.split('\n'); const result: string[] = []; // 匹配表格分隔行 (例如 | --- | --- | 或 --- | ---) const tableDividerRegex = /^\s*\|?\s*:?-+:?\s*(\|?\s*:?-+:?\s*)+\|?\s*$/; // 匹配包含竖线的表格行 const tableRowRegex = /^\s*\|?.*\|.*\|?\s*$/; const isDivider = (line: string) => line.includes('|') && tableDividerRegex.test(line); for (let i = 0; i < lines.length; i++) { const currentLine = lines[i]; const nextLine = lines[i + 1] ?? ''; // 逻辑: // 1. 当前行看起来像表头(包含 |) // 2. 下一行是分隔行(---) // 3. 前一行不是空行且不是表格行 if ( tableRowRegex.test(currentLine) && isDivider(nextLine) && i > 0 && lines[i - 1].trim() !== '' && !tableRowRegex.test(lines[i - 1]) ) { result.push(''); } result.push(currentLine); } return result.join('\n'); } // 流式更新 AI Card 内容 async function streamAICard( card: AICardInstance, content: string, finished: boolean = false, log?: any, ): Promise<void> { // 首次 streaming 前,先切换到 INPUTING 状态(与 Python SDK get_card_data(INPUTING) 一致) if (!card.inputingStarted) { const statusBody = { outTrackId: card.cardInstanceId, cardData: { cardParamMap: { flowStatus: AICardStatus.INPUTING, msgContent: '', staticMsgContent: '', sys_full_json_obj: JSON.stringify({ order: ['msgContent'], // 只声明实际使用的字段,避免部分客户端显示空占位 }), }, }, }; log?.info?.(`[DingTalk][AICard] PUT /v1.0/card/instances (INPUTING) outTrackId=${card.cardInstanceId}`); try { const statusResp = await axios.put(`${DINGTALK_API}/v1.0/card/instances`, statusBody, { headers: { 'x-acs-dingtalk-access-token': card.accessToken, 'Content-Type': 'application/json' }, }); log?.info?.(`[DingTalk][AICard] INPUTING 响应: status=${statusResp.status} data=${JSON.stringify(statusResp.data)}`); } catch (err: any) { log?.error?.(`[DingTalk][AICard] INPUTING 切换失败: ${err.message}, resp=${JSON.stringify(err.response?.data)}`); throw err; } card.inputingStarted = true; } // 调用 streaming API 更新内容 const fixedContent = ensureTableBlankLines(content); const body = { outTrackId: card.cardInstanceId, guid: `${Date.now()}_${Math.random().toString(36).slice(2, 8)}`, key: 'msgContent', content: fixedContent, isFull: true, // 全量替换 isFinalize: finished, isError: false, }; log?.info?.(`[DingTalk][AICard] PUT /v1.0/card/streaming contentLen=${content.length} isFinalize=${finished} guid=${body.guid}`); try { const streamResp = await axios.put(`${DINGTALK_API}/v1.0/card/streaming`, body, { headers: { 'x-acs-dingtalk-access-token': card.accessToken, 'Content-Type': 'application/json' }, }); log?.info?.(`[DingTalk][AICard] streaming 响应: status=${streamResp.status}`); } catch (err: any) { log?.error?.(`[DingTalk][AICard] streaming 更新失败: ${err.message}, resp=${JSON.stringify(err.response?.data)}`); throw err; } } // 完成 AI Card:先 streaming isFinalize 关闭流式通道,再 put_card_data 更新 FINISHED 状态 async function finishAICard( card: AICardInstance, content: string, log?: any, ): Promise<void> { const fixedContent = ensureTableBlankLines(content); log?.info?.(`[DingTalk][AICard] 开始 finish,最终内容长度=${fixedContent.length}`); // 1. 先用最终内容关闭流式通道(isFinalize=true),确保卡片显示替换后的内容 await streamAICard(card, fixedContent, true, log); // 2. 更新卡片状态为 FINISHED const body = { outTrackId: card.cardInstanceId, cardData: { cardParamMap: { flowStatus: AICardStatus.FINISHED, msgContent: fixedContent, staticMsgContent: '', sys_full_json_obj: JSON.stringify({ order: ['msgContent'], // 只声明实际使用的字段,避免部分客户端显示空占位 }), }, }, }; log?.info?.(`[DingTalk][AICard] PUT /v1.0/card/instances (FINISHED) outTrackId=${card.cardInstanceId}`); try { const finishResp = await axios.put(`${DINGTALK_API}/v1.0/card/instances`, body, { headers: { 'x-acs-dingtalk-access-token': card.accessToken, 'Content-Type': 'application/json' }, }); log?.info?.(`[DingTalk][AICard] FINISHED 响应: status=${finishResp.status} data=${JSON.stringify(finishResp.data)}`); } catch (err: any) { log?.error?.(`[DingTalk][AICard] FINISHED 更新失败: ${err.message}, resp=${JSON.stringify(err.response?.data)}`); } } // ============ Gateway SSE Streaming ============ // ============ Bindings 匹配逻辑 ============ interface BindingMatch { channel?: string; accountId?: string; peer?: { kind?: 'direct' | 'group'; id?: string; }; } interface Binding { agentId: string; match?: BindingMatch; } /** * 根据 OpenClaw bindings 配置解析 agentId * * 匹配优先级(从高到低): * 1. peer.kind + peer.id 精确匹配(非 '*') * 2. peer.kind + peer.id='*' 通配匹配 * 3. peer.kind 匹配(无 peer.id) * 4. accountId 匹配 * 5. channel 匹配 * 6. 默认 fallback * * @param accountId 账号 ID * @param peerKind 会话类型:'direct'(单聊)或 'group'(群聊) * @param peerId 发送者 ID(单聊)或会话 ID(群聊) * @param log 日志对象 * @returns 匹配到的 agentId */ function resolveAgentIdByBindings( accountId: string, peerKind: 'direct' | 'group', peerId: string, log?: any, ): string { const rt = getRuntime(); const defaultAgentId = accountId === DEFAULT_ACCOUNT_ID ? 'main' : accountId; // 读取 OpenClaw 配置 let bindings: Binding[] = []; try { const configPath = path.join(os.homedir(), '.openclaw', 'openclaw.json'); if (fs.existsSync(configPath)) { const configContent = fs.readFileSync(configPath, 'utf-8'); const config = JSON.parse(configContent); bindings = config.bindings || []; } } catch (err: any) { log?.warn?.(`[DingTalk][Bindings] 读取 OpenClaw 配置失败: ${err.message}`); return defaultAgentId; } if (bindings.length === 0) { log?.info?.(`[DingTalk][Bindings] 无 bindings 配置,使用默认 agentId=${defaultAgentId}`); return defaultAgentId; } // 筛选 channel='dingtalk-connector' 的 bindings const channelBindings = bindings.filter(b => !b.match?.channel || b.match.channel === 'dingtalk-connector' ); if (channelBindings.length === 0) { log?.info?.(`[DingTalk][Bindings] 无匹配 channel 的 bindings,使用默认 agentId=${defaultAgentId}`); return defaultAgentId; } log?.info?.(`[DingTalk][Bindings] 开始匹配: accountId=${accountId}, peerKind=${peerKind}, peerId=${peerId}, bindings数量=${channelBindings.length}`); // 按优先级匹配 // 优先级1: peer.kind + peer.id 精确匹配 for (const binding of channelBindings) { const match = binding.match || {}; if (match.peer?.kind === peerKind && match.peer?.id && match.peer.id !== '*' && match.peer.id === peerId) { // 还需检查 accountId 是否匹配(如果指定了) if (match.accountId && match.accountId !== accountId) continue; log?.info?.(`[DingTalk][Bindings] 精确匹配 peer.id: agentId=${binding.agentId}`); return binding.agentId || defaultAgentId; } } // 优先级2: peer.kind + peer.id='*' 通配匹配 for (const binding of channelBindings) { const match = binding.match || {}; if (match.peer?.kind === peerKind && match.peer?.id === '*') { if (match.accountId && match.accountId !== accountId) continue; log?.info?.(`[DingTalk][Bindings] 通配匹配 peer.kind=${peerKind}, peer.id=*: agentId=${binding.agentId}`); return binding.agentId || defaultAgentId; } } // 优先级3: 仅 peer.kind 匹配(无 peer.id) for (const binding of channelBindings) { const match = binding.match || {}; if (match.peer?.kind === peerKind && !match.peer?.id) { if (match.accountId && match.accountId !== accountId) continue; log?.info?.(`[DingTalk][Bindings] 匹配 peer.kind=${peerKind}: agentId=${binding.agentId}`); return binding.agentId || defaultAgentId; } } // 优先级4: accountId 匹配(无 peer 配置) for (const binding of channelBindings) { const match = binding.match || {}; if (!match.peer && match.accountId === accountId) { log?.info?.(`[DingTalk][Bindings] 匹配 accountId=${accountId}: agentId=${binding.agentId}`); return binding.agentId || defaultAgentId; } } // 优先级5: 仅 channel 匹配(无 peer 和 accountId) for (const binding of channelBindings) { const match = binding.match || {}; if (!match.peer && !match.accountId) { log?.info?.(`[DingTalk][Bindings] 匹配 channel=dingtalk-connector: agentId=${binding.agentId}`); return binding.agentId || defaultAgentId; } } log?.info?.(`[DingTalk][Bindings] 无匹配,使用默认 agentId=${defaultAgentId}`); return defaultAgentId; } interface GatewayOptions { userContent: string; systemPrompts: string[]; sessionContext: SessionContext; gatewayAuth?: string; // token 或 password,都用 Bearer 格式 /** 记忆归属用户标识,用于 Gateway 区分记忆;sharedMemoryAcrossConversations=true 时传 accountId,false 时传 sessionContext JSON */ memoryUser?: string; /** 本地图片文件路径列表,用于 OpenClaw AgentMediaPayload */ imageLocalPaths?: string[]; /** 自定义 Gateway URL(如通过 Nginx 代理),用于 TLS 等场景 */ gatewayBaseUrl?: string; /** 会话类型:'direct'(单聊)或 'group'(群聊),用于 bindings 匹配 */ peerKind?: 'direct' | 'group'; /** 发送者 ID,用于 bindings 匹配 */ peerId?: string; gatewayPort?: number; log?: any; } async function* streamFromGateway(options: GatewayOptions, accountId: string): AsyncGenerator<string, void, unknown> { // 支持自定义 Gateway URL(如通过 Nginx 代理),用于 TLS 等场景 const { userContent, systemPrompts, sessionKey, gatewayAuth, gatewayBaseUrl, memoryUser, imageLocalPaths, peerKind, peerId, gatewayPort, log } = options; const rt = getRuntime(); const port = gatewayPort || rt.gateway?.port || 18789; const gatewayUrl = gatewayBaseUrl ? `${gatewayBaseUrl}/v1/chat/completions` : `http://127.0.0.1:${port}/v1/chat/completions`; const messages: any[] = []; for (const prompt of systemPrompts) { messages.push({ role: 'system', content: prompt }); } // 如果有图片,在文本中嵌入本地文件路径(OpenClaw AgentMediaPayload 格式) let finalContent = userContent; if (imageLocalPaths && imageLocalPaths.length > 0) { const imageMarkdown = imageLocalPaths.map(p => ``).join('\n'); finalContent = finalContent ? `${finalContent}\n\n${imageMarkdown}` : imageMarkdown; log?.info?.(`[DingTalk][Gateway] 附加 ${imageLocalPaths.length} 张本地图片路径`); } messages.push({ role: 'user', content: finalContent }); const headers: Record<string, string> = { 'Content-Type': 'application/json' }; if (gatewayAuth) { headers['Authorization'] = `Bearer ${gatewayAuth}`; } // 使用 bindings 配置解析 agentId,支持基于 peer.kind(单聊/群聊)的路由 // 如果没有提供 peerKind/peerId,则回退到原有逻辑 const agentId = (peerKind && peerId) ? resolveAgentIdByBindings(accountId, peerKind, peerId, log) : (accountId === DEFAULT_ACCOUNT_ID ? 'main' : accountId); headers['X-OpenClaw-Agent-Id'] = agentId; if (memoryUser) { // 使用 Base64 编码处理可能包含中文字符的 memoryUser // HTTP Header 只能包含 ASCII 字符,中文字符会导致 ByteString 编码错误 headers['X-OpenClaw-Memory-User'] = Buffer.from(memoryUser, 'utf-8').toString('base64'); } log?.info?.(`[DingTalk][Gateway] POST ${gatewayUrl}, session=${sessionKey}, accountId=${accountId}, agentId=${agentId}, peerKind=${peerKind}, messages=${messages.length}`); const response = await fetch(gatewayUrl, { method: 'POST', headers, body: JSON.stringify({ model: 'main', messages, stream: true, user: sessionKey, // 用于 session 持久化 }), }); log?.info?.(`[DingTalk][Gateway] 响应 status=${response.status}, ok=${response.ok}, hasBody=${!!response.body}`); if (!response.ok || !response.body) { const errText = response.body ? await response.text() : '(no body)'; log?.error?.(`[DingTalk][Gateway] 错误响应: ${errText}`); throw new Error(`Gateway error: ${response.status} - ${errText}`); } const reader = response.body.getReader(); const decoder = new TextDecoder(); let buffer = ''; while (true) { const { done, value } = await reader.read(); if (done) break; buffer += decoder.decode(value, { stream: true }); const lines = buffer.split('\n'); buffer = lines.pop() || ''; for (const line of lines) { if (!line.startsWith('data: ')) continue; const data = line.slice(6).trim(); if (data === '[DONE]') return; try { const chunk = JSON.parse(data); const content = chunk.choices?.[0]?.delta?.content; if (content) yield content; } catch {} } } } // ============ 图片下载到本地文件 ============ /** * 下载钉钉图片到本地临时文件 * 返回本地文件路径,用于 OpenClaw AgentMediaPayload */ async function downloadImageToFile( downloadUrl: string, log?: any, ): Promise<string | null> { try { log?.info?.(`[DingTalk][Image] 开始下载图片: ${downloadUrl.slice(0, 100)}...`); const resp = await axios.get(downloadUrl, { responseType: 'arraybuffer', timeout: 30_000, }); const buffer = Buffer.from(resp.data); const contentType = resp.headers['content-type'] || 'image/jpeg'; const ext = contentType.includes('png') ? '.png' : contentType.includes('gif') ? '.gif' : contentType.includes('webp') ? '.webp' : '.jpg'; const mediaDir = path.join(os.homedir(), '.openclaw', 'workspace', 'media', 'inbound'); fs.mkdirSync(mediaDir, { recursive: true }); const tmpFile = path.join(mediaDir, `openclaw-media-${Date.now()}-${Math.random().toString(36).slice(2, 8)}${ext}`); fs.writeFileSync(tmpFile, buffer); log?.info?.(`[DingTalk][Image] 图片下载成功: size=${buffer.length} bytes, type=${contentType}, path=${tmpFile}`); return tmpFile; } catch (err: any) { log?.error?.(`[DingTalk][Image] 图片下载失败: ${err.message}`); return null; } } /** * 通过钉钉 API 下载媒体文件(需要 access_token) * 适用于 picture/file 类型的 downloadCode */ async function downloadMediaByCode( downloadCode: string, config: any, log?: any, ): Promise<string | null> { try { const token = await getAccessToken(config); log?.info?.(`[DingTalk][Image] 通过 downloadCode 下载媒体: ${downloadCode.slice(0, 30)}...`); const resp = await axios.post( `${DINGTALK_API}/v1.0/robot/messageFiles/download`, { downloadCode, robotCode: config.clientId }, { headers: { 'x-acs-dingtalk-access-token': token, 'Content-Type': 'application/json' }, timeout: 30_000, }, ); const downloadUrl = resp.data?.downloadUrl; if (!downloadUrl) { log?.warn?.(`[DingTalk][Image] downloadCode 换取 downloadUrl 失败: ${JSON.stringify(resp.data)}`); return null; } return downloadImageToFile(downloadUrl, log); } catch (err: any) { log?.error?.(`[DingTalk][Image] downloadCode 下载失败: ${err.message}`); return null; } } /** * 通过钉钉 API 下载文件附件(需要 access_token) * 与 downloadMediaByCode 不同,此函数保留原始文件名 */ async function downloadFileByCode( downloadCode: string, fileName: string, config: any, log?: any, ): Promise<string | null> { try { const token = await getAccessToken(config); log?.info?.(`[DingTalk][File] 通过 downloadCode 下载文件: ${fileName}`); const resp = await axios.post( `${DINGTALK_API}/v1.0/robot/messageFiles/download`, { downloadCode, robotCode: config.clientId }, { headers: { 'x-acs-dingtalk-access-token': token, 'Content-Type': 'application/json' }, timeout: 30_000, }, ); const downloadUrl = resp.data?.downloadUrl; if (!downloadUrl) { log?.warn?.(`[DingTalk][File] downloadCode 换取 downloadUrl 失败: ${JSON.stringify(resp.data)}`); return null; } // 下载文件内容 const fileResp = await axios.get(downloadUrl, { responseType: 'arraybuffer', timeout: 60_000, }); const buffer = Buffer.from(fileResp.data); const mediaDir = path.join(os.homedir(), '.openclaw', 'workspace', 'media', 'inbound'); fs.mkdirSync(mediaDir, { recursive: true }); // 用时间戳前缀避免文件名冲突,保留原始文件名 const safeFileName = fileName.replace(/[/\\:*?"<>|]/g, '_'); const localPath = path.join(mediaDir, `${Date.now()}-${safeFileName}`); fs.writeFileSync(localPath, buffer); log?.info?.(`[DingTalk][File] 文件下载成功: size=${buffer.length} bytes, path=${localPath}`); return localPath; } catch (err: any) { log?.error?.(`[DingTalk][File] 文件下载失败: ${err.message}`); return null; } } /** 可直接读取内容的文本类文件扩展名 */ const TEXT_FILE_EXTENSIONS = new Set(['.txt', '.md', '.csv', '.json', '.xml', '.yaml', '.yml', '.html', '.htm', '.log', '.conf', '.ini', '.sh', '.py', '.js', '.ts', '.css', '.sql']); /** 需要保存但无法直接读取的 Office/二进制文件扩展名 */ const OFFICE_FILE_EXTENSIONS = new Set(['.docx', '.xlsx', '.pptx', '.pdf', '.doc', '.xls', '.ppt', '.zip', '.rar', '.7z']); // ============ 消息处理 ============ /** 消息内容提取结果 */ interface ExtractedMessage { text: string; messageType: string; /** 图片 URL 列表(来自 richText 或 picture 消息) */ imageUrls: string[]; /** 图片 downloadCode 列表(用于通过 API 下载) */ downloadCodes: string[]; /** 文件名列表(与 downloadCodes 对应,用于文件类型消息) */ fileNames: string[]; /** at的钉钉用户ID列表 */ atDingtalkIds: string[]; /** at的手机号列表 */ atMobiles: string[]; } function extractMessageContent(data: any): ExtractedMessage { const msgtype = data.msgtype || 'text'; switch (msgtype) { case 'text': { const atDingtalkIds = data.text?.at?.atDingtalkIds || []; const atMobiles = data.text?.at?.atMobiles || []; return { text: data.text?.content?.trim() || '', messageType: 'text', imageUrls: [], downloadCodes: [], fileNames: [], atDingtalkIds, atMobiles }; } case 'richText': { const parts = data.content?.richText || []; const textParts: string[] = []; const imageUrls: string[] = []; for (const part of parts) { if (part.text) { textParts.push(part.text); } if (part.pictureUrl) { imageUrls.push(part.pictureUrl); } if (part.type === 'picture' && part.downloadCode) { // 有些 richText 图片通过 downloadCode 获取 imageUrls.push(`downloadCode:${part.downloadCode}`); } } const text = textParts.join('') || (imageUrls.length > 0 ? '[图片]' : '[富文本消息]'); return { text, messageType: 'richText', imageUrls, downloadCodes: [], fileNames: [], atDingtalkIds: [], atMobiles: [] }; } case 'picture': { const downloadCode = data.content?.downloadCode || ''; const pictureUrl = data.content?.pictureUrl || ''; const imageUrls: string[] = []; const downloadCodes: string[] = []; if (pictureUrl) { imageUrls.push(pictureUrl); } if (downloadCode) { downloadCodes.push(downloadCode); } return { text: '[图片]', messageType: 'picture', imageUrls, downloadCodes, fileNames: [], atDingtalkIds: [], atMobiles: [] }; } case 'audio': return { text: data.content?.recognition || '[语音消息]', messageType: 'audio', imageUrls: [], downloadCodes: [], fileNames: [], atDingtalkIds: [], atMobiles: [] }; case 'video': return { text: '[视频]', messageType: 'video', imageUrls: [], downloadCodes: [], fileNames: [], atDingtalkIds: [], atMobiles: [] }; case 'file': { const fileName = data.content?.fileName || '文件'; const downloadCode = data.content?.downloadCode || ''; const downloadCodes: string[] = []; const fileNames: string[] = []; if (downloadCode) { downloadCodes.push(downloadCode); fileNames.push(fileName); } return { text: `[文件: ${fileName}]`, messageType: 'file', imageUrls: [], downloadCodes, fileNames, atDingtalkIds: [], atMobiles: [] }; } default: return { text: data.text?.content?.trim() || `[${msgtype}消息]`, messageType: msgtype, imageUrls: [], downloadCodes: [], fileNames: [], atDingtalkIds: [], atMobiles: [] }; } } // 发送 Markdown 消息 async function sendMarkdownMessage( config: any, sessionWebhook: string, title: string, markdown: string, options: any = {}, ): Promise<any> { const token = await getAccessToken(config); let text = markdown; if (options.atUserId) text = `${text} @${options.atUserId}`; const body: any = { msgtype: 'markdown', markdown: { title: title || 'Moltbot', text }, }; if (options.atUserId) body.at = { atUserIds: [options.atUserId], isAtAll: false }; return (await axios.post(sessionWebhook, body, { headers: { 'x-acs-dingtalk-access-token': token, 'Content-Type': 'application/json' }, })).data; } // 发送文本消息 async function sendTextMessage( config: any, sessionWebhook: string, text: string, options: any = {}, ): Promise<any> { const token = await getAccessToken(config); const body: any = { msgtype: 'text', text: { content: text } }; if (options.atUserId) body.at = { atUserIds: [options.atUserId], isAtAll: false }; return (await axios.post(sessionWebhook, body, { headers: { 'x-acs-dingtalk-access-token': token, 'Content-Type': 'application/json' }, })).data; } // 智能选择 text / markdown async function sendMessage( config: any, sessionWebhook: string, text: string, options: any = {}, ): Promise<any> { const hasMarkdown = /^[#*>-]|[*_`#\[\]]/.test(text) || text.includes('\n'); const useMarkdown = options.useMarkdown !== false && (options.useMarkdown || hasMarkdown); if (useMarkdown) { const title = options.title || text.split('\n')[0].replace(/^[#*\s\->]+/, '').slice(0, 20) || 'Moltbot'; return sendMarkdownMessage(config, sessionWebhook, title, text, options); } return sendTextMessage(config, sessionWebhook, text, options); } // ============ 主动发送消息 API ============ /** 消息类型枚举 */ type DingTalkMsgType = 'text' | 'markdown' | 'link' | 'actionCard' | 'image'; /** 主动发送消息的结果 */ interface SendResult { ok: boolean; processQueryKey?: string; cardInstanceId?: string; // AI Card 成功时返回 error?: string; usedAICard?: boolean; // 是否使用了 AI Card } /** 主动发送选项 */ interface ProactiveSendOptions { msgType?: DingTalkMsgType; title?: string; log?: any; useAICard?: boolean; // 是否使用 AI Card,默认 true fallbackToNormal?: boolean; // AI Card 失败时是否降级到普通消息,默认 true } /** AI Card 投放目标类型 */ type AICardTarget = | { type: 'user'; userId: string } | { type: 'group'; openConversationId: string }; /** * 构建卡片投放请求体(提取公共逻辑) */ function buildDeliverBody( cardInstanceId: string, target: AICardTarget, robotCode: string, ): any { const base = { outTrackId: cardInstanceId, userIdType: 1 }; if (target.type === 'group') { return { ...base, openSpaceId: `dtv1.card//IM_GROUP.${target.openConversationId}`, imGroupOpenDeliverModel: { robotCode }, }; } return { ...base, openSpaceId: `dtv1.card//IM_ROBOT.${target.userId}`, imRobotOpenDeliverModel: { spaceType: 'IM_ROBOT', robotCode }, }; } /** * 通用 AI Card 创建函数 * 支持被动回复和主动发送两种场景 */ async function createAICardForTarget( config: any, target: AICardTarget, log?: any, ): Promise<AICardInstance | null> { const targetDesc = target.type === 'group' ? `群聊 ${target.openConversationId}` : `用户 ${target.userId}`; try { const token = await getAccessToken(config); const cardInstanceId = `card_${Date.now()}_${Math.random().toString(36).slice(2, 10)}`; log?.info?.(`[DingTalk][AICard] 开始创建卡片: ${targetDesc}, outTrackId=${cardInstanceId}`); // 1. 创建卡片实例 const createBody = { cardTemplateId: AI_CARD_TEMPLATE_ID, outTrackId: cardInstanceId, cardData: { cardParamMap: {} }, callbackType: 'STREAM', imGroupOpenSpaceModel: { supportForward: true }, imRobotOpenSpaceModel: { supportForward: true }, }; log?.info?.(`[DingTalk][AICard] POST /v1.0/card/instances`); const createResp = await axios.post(`${DINGTALK_API}/v1.0/card/instances`, createBody, { headers: { 'x-acs-dingtalk-access-token': token, 'Content-Type': 'application/json' }, }); log?.info?.(`[DingTalk][AICard] 创建卡片响应: status=${createResp.status}`); // 2. 投放卡片 const deliverBody = buildDeliverBody(cardInstanceId, target, config.clientId); log?.info?.(`[DingTalk][AICard] POST /v1.0/card/instances/deliver body=${JSON.stringify(deliverBody)}`); const deliverResp = await axios.post(`${DINGTALK_API}/v1.0/card/instances/deliver`, deliverBody, { headers: { 'x-acs-dingtalk-access-token': token, 'Content-Type': 'application/json' }, }); log?.info?.(`[DingTalk][AICard] 投放卡片响应: status=${deliverResp.status}`); return { cardInstanceId, accessToken: token, inputingStarted: false }; } catch (err: any) { log?.error?.(`[DingTalk][AICard] 创建卡片失败 (${targetDesc}): ${err.message}`); if (err.response) { log?.error?.(`[DingTalk][AICard] 错误响应: status=${err.response.status} data=${JSON.stringify(err.response.data)}`); } return null; } } /** * 主动发送文件消息(使用普通消息 API) */ async function sendFileProactive( config: any, target: AICardTarget, fileInfo: FileInfo, mediaId: string, log?: any, ): Promise<void> { try { const token = await getAccessToken(config); // 钉钉普通消息 API 的文件消息格式 const msgParam = { mediaId: mediaId, fileName: fileInfo.fileName, fileType: fileInfo.fileType, }; const body: any = { robotCode: config.clientId, msgKey: 'sampleFile', msgParam: JSON.stringify(msgParam), }; let endpoint: string; if (target.type === 'group') { body.openConversationId = target.openConversationId; endpoint = `${DINGTALK_API}/v1.0/robot/groupMessages/send`; } else { body.userIds = [target.userId]; endpoint = `${DINGTALK_API}/v1.0/robot/oToMessages/batchSend`; } log?.info?.(`[DingTalk][File][Proactive] 发送文件消息: ${fileInfo.fileName}`); const resp = await axios.post(endpoint, body, { headers: { 'x-acs-dingtalk-access-token': token, 'Content-Type': 'application/json' }, timeout: 10_000, }); if (resp.data?.processQueryKey) { log?.info?.(`[DingTalk][File][Proactive] 文件消息发送成功: ${fileInfo.fileName}`); } else { log?.warn?.(`[DingTalk][File][Proactive] 文件消息发送响应异常: ${JSON.stringify(resp.data)}`); } } catch (err: any) { log?.error?.(`[DingTalk][File][Proactive] 发送文件消息失败: ${fileInfo.fileName}, 错误: ${err.message}`); } } /** * 主动发送音频消息(使用普通消息 API) */ async function sendAudioProactive( config: any, target: AICardTarget, fileInfo: FileInfo, mediaId: string, log?: any, durationMs?: number, ): Promise<void> { try { const token = await getAccessToken(config); // 钉钉普通消息 API 的音频消息格式 const actualDuration = (durationMs && durationMs > 0) ? durationMs.toString() : '60000'; const msgParam = { mediaId: mediaId, duration: actualDuration, }; const body: any = { robotCode: config.clientId, msgKey: 'sampleAudio', msgParam: JSON.stringify(msgParam), }; let endpoint: string; if (target.type === 'group') { body.openConversationId = target.openConversationId; endpoint = `${DINGTALK_API}/v1.0/robot/groupMessages/send`; } else { body.userIds = [target.userId]; endpoint = `${DINGTALK_API}/v1.0/robot/oToMessages/batchSend`; } log?.info?.(`[DingTalk][Audio][Proactive] 发送音频消息: ${fileInfo.fileName}`); const resp = await axios.post(endpoint, body, { headers: { 'x-acs-dingtalk-access-token': token, 'Content-Type': 'application/json' }, timeout: 10_000, }); if (resp.data?.processQueryKey) { log?.info?.(`[DingTalk][Audio][Proactive] 音频消息发送成功: ${fileInfo.fileName}`); } else { log?.warn?.(`[DingTalk][Audio][Proactive] 音频消息发送响应异常: ${JSON.stringify(resp.data)}`); } } catch (err: any) { log?.error?.(`[DingTalk][Audio][Proactive] 发送音频消息失败: ${fileInfo.fileName}, 错误: ${err.message}`); } } /** * 主动发送视频消息(使用普通消息 API) */ async function sendVideoProactive( config: any, target: AICardTarget, videoMediaId: string, picMediaId: string, metadata: VideoMetadata, log?: any, ): Promise<void> { try { const token = await getAccessToken(config); // 钉钉普通消息 API 的视频消息格式 const msgParam = { duration: metadata.duration.toString(), videoMediaId: videoMediaId, videoType: 'mp4', picMediaId: picMediaId, }; const body: any = { robotCode: config.clientId, msgKey: 'sampleVideo', msgParam: JSON.stringify(msgParam), }; let endpoint: string; if (target.type === 'group') { body.openConversationId = target.openConversationId; endpoint = `${DINGTALK_API}/v1.0/robot/groupMessages/send`; } else { body.userIds = [target.userId]; endpoint = `${DINGTALK_API}/v1.0/robot/oToMessages/batchSend`; } log?.info?.(`[DingTalk][Video][Proactive] 发送视频消息`); const resp = await axios.post(endpoint, body, { headers: { 'x-acs-dingtalk-access-token': token, 'Content-Type': 'application/json' }, timeout: 10_000, }); if (resp.data?.processQueryKey) { log?.info?.(`[DingTalk][Video][Proactive] 视频消息发送成功`); } else { log?.warn?.(`[DingTalk][Video][Proactive] 视频消息发送响应异常: ${JSON.stringify(resp.data)}`); } } catch (err: any) { log?.error?.(`[DingTalk][Video][Proactive] 发送视频消息失败: ${err.message}`); } } /** 音频信息接口 */ interface AudioInfo { path: string; } /** * 提取音频标记并发送音频消息 * 解析 [DINGTALK_AUDIO]{"path":"..."}[/DINGTALK_AUDIO] 标记 * * @param useProactiveApi 是否使用主动消息 API(用于 AI Card 场景) * @param target 主动 API 需要的目标信息(useProactiveApi=true 时必须提供) */ async function processAudioMarkers( content: string, sessionWebhook: string, config: any, oapiToken: string | null, log?: any, useProactiveApi: boolean = false, target?: AICardTarget, ): Promise<string> { const logPrefix = useProactiveApi ? '[DingTalk][Audio][Proactive]' : '[DingTalk][Audio]'; if (!oapiToken) { log?.warn?.(`${logPrefix} 无 oapiToken,跳过音频处理`); return content; } const fs = await import('fs'); const path = await import('path'); const matches = [...content.matchAll(AUDIO_MARKER_PATTERN)]; const audioInfos: AudioInfo[] = []; const invalidAudios: string[] = []; for (const match of matches) { try { const audioInfo = JSON.parse(match[1]) as AudioInfo; if (audioInfo.path && fs.existsSync(audioInfo.path)) { audioInfos.push(audioInfo); log?.info?.(`${logPrefix} 提取到音频: ${audioInfo.path}`); } else { invalidAudios.push(audioInfo.path || '未知路径'); log?.warn?.(`${logPrefix} 音频文件不存在: ${audioInfo.path}`); } } catch (err: any) { log?.warn?.(`${logPrefix} 解析标记失败: ${err.message}`); } } if (audioInfos.length === 0 && invalidAudios.length === 0) { log?.info?.(`${logPrefix} 未检测到音频标记`); return content.replace(AUDIO_MARKER_PATTERN, '').trim(); } // 先移除所有音频标记 let cleanedContent = content.replace(AUDIO_MARKER_PATTERN, '').trim(); const statusMessages: string[] = []; for (const invalidPath of invalidAudios) { statusMessages.push(`⚠️ 音频文件不存在: ${path.basename(invalidPath)}`); } if (audioInfos.length > 0) { log?.info?.(`${logPrefix} 检测到 ${audioInfos.length} 个音频,开始处理...`); } for (const audioInfo of audioInfos) { const fileName = path.basename(audioInfo.path); try { const ext = path.extname(audioInfo.path).slice(1).toLowerCase(); const fileInfo: FileInfo = { path: audioInfo.path, fileName: fileName, fileType: ext, }; // 上传音频到钉钉 const mediaId = await uploadMediaToDingTalk(audioInfo.path, 'voice', oapiToken, 20 * 1024 * 1024, log); if (!mediaId) { statusMessages.push(`⚠️ 音频上传失败: ${fileName}(文件可能超过 20MB 限制)`); continue; } // 提取音频实际时长 const audioDurationMs = await extractAudioDuration(audioInfo.path, log); // 发送音频消息 if (useProactiveApi && target) { await sendAudioProactive(config, target, fileInfo, mediaId, log, audioDurationMs ?? undefined); } else { await sendAudioMessage(config, sessionWebhook, fileInfo, mediaId, oapiToken, log, audioDurationMs ?? undefined); } statusMessages.push(`✅ 音频已发送: ${fileName}`); log?.info?.(`${logPrefix} 音频处理完成: ${fileName}`); } catch (err: any) { log?.error?.(`${logPrefix} 处理音频失败: ${err.message}`); statusMessages.push(`⚠️ 音频处理异常: ${fileName}(${err.message})`); } } if (statusMessages.length > 0) { const statusText = statusMessages.join('\n'); cleanedContent = cleanedContent ? `${cleanedContent}\n\n${statusText}` : statusText; } return cleanedContent; } /** * 主动创建并发送 AI Card(通用内部实现) * 复用 createAICardForTarget 并完整支持后处理 * @param config 钉钉配置 * @param target 投放目标(单聊或群聊) * @param content 消息内容 * @param log 日志对象 * @returns SendResult */ async function sendAICardInternal( config: any, target: AICardTarget, content: string, log?: any, ): Promise<SendResult> { const targetDesc = target.type === 'group' ? `群聊 ${target.openConversationId}` : `用户 ${target.userId}`; try { // 0. 获取 oapiToken 用于后处理 const oapiToken = await getOapiAccessToken(config); // 1. 后处理01:上传本地图片到钉钉,替换路径为 media_id let processedContent = content; if (oapiToken) { log?.info?.(`[DingTalk][AICard][Proactive] 开始图片后处理`); processedContent = await processLocalImages(content, oapiToken, log); } else { log?.warn?.(`[DingTalk][AICard][Proactive] 无法获取 oapiToken,跳过媒体后处理`); } // 2. 后处理02:提取视频标记并发送视频消息 log?.info?.(`[DingTalk][Video][Proactive] 开始视频后处理`); processedContent = await processVideoMarkers(processedContent, '', config, oapiToken, log, true, target); // 3. 后处理03:提取音频标记并发送音频消息(使用主动消息 API) log?.info?.(`[DingTalk][Audio][Proactive] 开始音频后处理`); processedContent = await processAudioMarkers(processedContent, '', config, oapiToken, log, true, target); // 4. 后处理04:提取文件标记并发送独立文件消息(使用主动消息 API) log?.info?.(`[DingTalk][File][Proactive] 开始文件后处理`); processedContent = await processFileMarkers(processedContent, '', config, oapiToken, log, true, target); // 5. 检查处理后的内容是否为空(纯文件/视频/音频消息场景) // 如果内容只包含文件/视频/音频标记,处理后会变成空字符串,此时跳过创建空白 AI Card const trimmedContent = processedContent.trim(); if (!trimmedContent) { log?.info?.(`[DingTalk][AICard][Proactive] 处理后内容为空(纯文件/视频消息),跳过创建 AI Card`); return { ok: true, usedAICard: false }; } // 5. 创建卡片(复用通用函数) const card = await createAICardForTarget(config, target, log); if (!card) { return { ok: false, error: 'Failed to create AI Card', usedAICard: false }; } // 6. 使用 finishAICard 设置内容 await finishAICard(card, processedContent, log); log?.info?.(`[DingTalk][AICard][Proactive] AI Card 发送成功: ${targetDesc}, cardInstanceId=${card.cardInstanceId}`); return { ok: true, cardInstanceId: card.cardInstanceId, usedAICard: true }; } catch (err: any) { log?.error?.(`[DingTalk][AICard][Proactive] AI Card 发送失败 (${targetDesc}): ${err.message}`); if (err.response) { log?.error?.(`[DingTalk][AICard][Proactive] 错误响应: status=${err.response.status} data=${JSON.stringify(err.response.data)}`); } return { ok: false, error: err.response?.data?.message || err.message, usedAICard: false }; } } /** * 主动发送 AI Card 到单聊用户 */ async function sendAICardToUser( config: any, userId: string, content: string, log?: any, ): Promise<SendResult> { return sendAICardInternal(config, { type: 'user', userId }, content, log); } /** * 主动发送 AI Card 到群聊 */ async function sendAICardToGroup( config: any, openConversationId: string, content: string, log?: any, ): Promise<SendResult> { return sendAICardInternal(config, { type: 'group', openConversationId }, content, log); } /** * 构建普通消息的 msgKey 和 msgParam * 提取公共逻辑,供 sendNormalToUser 和 sendNormalToGroup 复用 */ function buildMsgPayload( msgType: DingTalkMsgType, content: string, title?: string, ): { msgKey: string; msgParam: Record<string, any> } | { error: string } { switch (msgType) { case 'markdown': return { msgKey: 'sampleMarkdown', msgParam: { title: title || content.split('\n')[0].replace(/^[#*\s\->]+/, '').slice(0, 20) || 'Message', text: content, }, }; case 'link': try { return { msgKey: 'sampleLink', msgParam: typeof content === 'string' ? JSON.parse(content) : content, }; } catch { return { error: 'Invalid link message format, expected JSON' }; } case 'actionCard': try { return { msgKey: 'sampleActionCard', msgParam: typeof content === 'string' ? JSON.parse(content) : content, }; } catch { return { error: 'Invalid actionCard message format, expected JSON' }; } case 'image': return { msgKey: 'sampleImageMsg', msgParam: { photoURL: content }, }; case 'text': default: return { msgKey: 'sampleText', msgParam: { content }, }; } } /** * 使用普通消息 API 发送单聊消息(降级方案) */ async function sendNormalToUser( config: any, userIds: string | string[], content: string, options: { msgType?: DingTalkMsgType; title?: string; log?: any } = {}, ): Promise<SendResult> { const { msgType = 'text', title, log } = options; const userIdArray = Array.isArray(userIds) ? userIds : [userIds]; // 构建消息参数 const payload = buildMsgPayload(msgType, content, title); if ('error' in payload) { return { ok: false, error: payload.error, usedAICard: false }; } try { const token = await getAccessToken(config); const body = { robotCode: config.clientId, userIds: userIdArray, msgKey: payload.msgKey, msgParam: JSON.stringify(payload.msgParam), }; log?.info?.(`[DingTalk][Normal] 发送单聊消息: userIds=${userIdArray.join(',')}, msgType=${msgType}`); const resp = await axios.post(`${DINGTALK_API}/v1.0/robot/oToMessages/batchSend`, body, { headers: { 'x-acs-dingtalk-access-token': token, 'Content-Type': 'application/json' }, timeout: 10_000, }); if (resp.data?.processQueryKey) { log?.info?.(`[DingTalk][Normal] 发送成功: processQueryKey=${resp.data.processQueryKey}`); return { ok: true, processQueryKey: resp.data.processQueryKey, usedAICard: false }; } log?.warn?.(`[DingTalk][Normal] 发送响应异常: ${JSON.stringify(resp.data)}`); return { ok: false, error: resp.data?.message || 'Unknown error', usedAICard: false }; } catch (err: any) { const errMsg = err.response?.data?.message || err.message; log?.error?.(`[DingTalk][Normal] 发送失败: ${errMsg}`); return { ok: false, error: errMsg, usedAICard: false }; } } /** * 使用普通消息 API 发送群聊消息(降级方案) */ async function sendNormalToGroup( config: any, openConversationId: string, content: string, options: { msgType?: DingTalkMsgType; title?: string; log?: any } = {}, ): Promise<SendResult> { const { msgType = 'text', title, log } = options; // 构建消息参数 const payload = buildMsgPayload(msgType, content, title); if ('error' in payload) { return { ok: false, error: payload.error, usedAICard: false }; } try { const token = await getAccessToken(config); const body = { robotCode: config.clientId, openConversationId, msgKey: payload.msgKey, msgParam: JSON.stringify(payload.msgParam), }; log?.info?.(`[DingTalk][Normal] 发送群聊消息: openConversationId=${openConversationId}, msgType=${msgType}`); const resp = await axios.post(`${DINGTALK_API}/v1.0/robot/groupMessages/send`, body, { headers: { 'x-acs-dingtalk-access-token': token, 'Content-Type': 'application/json' }, timeout: 10_000, }); if (resp.data?.processQueryKey) { log?.info?.(`[DingTalk][Normal] 发送成功: processQueryKey=${resp.data.processQueryKey}`); return { ok: true, processQueryKey: resp.data.processQueryKey, usedAICard: false }; } log?.warn?.(`[DingTalk][Normal] 发送响应异常: ${JSON.stringify(resp.data)}`); return { ok: false, error: resp.data?.message || 'Unknown error', usedAICard: false }; } catch (err: any) { const errMsg = err.response?.data?.message || err.message; log?.error?.(`[DingTalk][Normal] 发送失败: ${errMsg}`); return { ok: false, error: errMsg, usedAICard: false }; } } /** * 主动发送单聊消息给指定用户 * 默认使用 AI Card,失败时降级到普通消息 * @param config 钉钉配置(需包含 clientId 和 clientSecret) * @param userIds 用户 ID 数组(staffId 或 unionId) * @param content 消息内容 * @param options 可选配置 */ async function sendToUser( config: any, userIds: string | string[], content: string, options: ProactiveSendOptions = {}, ): Promise<SendResult> { const { log, useAICard = true, fallbackToNormal = true } = options; if (!config.clientId || !config.clientSecret) { return { ok: false, error: 'Missing clientId or clientSecret', usedAICard: false }; } const userIdArray = (Array.isArray(userIds) ? userIds : [userIds]).filter((id) => Boolean(id)); if (userIdArray.length === 0) { return { ok: false, error: 'userIds cannot be empty', usedAICard: false }; } // AI Card 只支持单个用户 if (useAICard && userIdArray.length === 1) { log?.info?.(`[DingTalk][SendToUser] 尝试使用 AI Card 发送: userId=${userIdArray[0]}`); const cardResult = await sendAICardToUser(config, userIdArray[0], content, log); if (cardResult.ok) { return cardResult; } // AI Card 失败 log?.warn?.(`[DingTalk][SendToUser] AI Card 发送失败: ${cardResult.error}`); if (!fallbackToNormal) { log?.error?.(`[DingTalk][SendToUser] 不降级到普通消息,返回错误`); return cardResult; } log?.info?.(`[DingTalk][SendToUser] 降级到普通消息发送`); } else if (useAICard && userIdArray.length > 1) { log?.info?.(`[DingTalk][SendToUser] 多用户发送不支持 AI Card,使用普通消息`); } // 使用普通消息 return sendNormalToUser(config, userIdArray, content, options); } /** * 主动发送群聊消息到指定群 * 默认使用 AI Card,失败时降级到普通消息 * @param config 钉钉配置(需包含 clientId 和 clientSecret) * @param openConversationId 群会话 ID * @param content 消息内容 * @param options 可选配置 */ async function sendToGroup( config: any, openConversationId: string, content: string, options: ProactiveSendOptions = {}, ): Promise<SendResult> { const { log, useAICard = true, fallbackToNormal = true } = options; if (!config.clientId || !config.clientSecret) { return { ok: false, error: 'Missing clientId or clientSecret', usedAICard: false }; } if (!openConversationId) { return { ok: false, error: 'openConversationId cannot be empty', usedAICard: false }; } // 尝试使用 AI Card if (useAICard) { log?.info?.(`[DingTalk][SendToGroup] 尝试使用 AI Card 发送: openConversationId=${openConversationId}`); const cardResult = await sendAICardToGroup(config, openConversationId, content, log); if (cardResult.ok) { return cardResult; } // AI Card 失败 log?.warn?.(`[DingTalk][SendToGroup] AI Card 发送失败: ${cardResult.error}`); if (!fallbackToNormal) { log?.error?.(`[DingTalk][SendToGroup] 不降级到普通消息,返回错误`); return cardResult; } log?.info?.(`[DingTalk][SendToGroup] 降级到普通消息发送`); } // 使用普通消息 return sendNormalToGroup(config, openConversationId, content, options); } /** * 智能发送消息 * 默认使用 AI Card,失败时降级到普通消息 * @param config 钉钉配置 * @param target 目标:{ userId } 或 { openConversationId } * @param content 消息内容 * @param options 可选配置 */ async function sendProactive( config: any, target: { userId?: string; userIds?: string[]; openConversationId?: string }, content: string, options: ProactiveSendOptions = {}, ): Promise<SendResult> { // 自动检测是否使用 markdown(用于降级时) if (!options.msgType) { const hasMarkdown = /^[#*>-]|[*_`#\[\]]/.test(content) || content.includes('\n'); if (hasMarkdown) { options.msgType = 'markdown'; } } // 发送到用户 if (target.userId || target.userIds) { const userIds = target.userIds || [target.userId!]; return sendToUser(config, userIds, content, options); } // 发送到群 if (target.openConversationId) { return sendToGroup(config, target.openConversationId, content, options); } return { ok: false, error: 'Must specify userId, userIds, or openConversationId', usedAICard: false }; } // ============ 消息处理中表情 ============ /** 在用户消息上贴 🤔思考中 表情,表示正在处理 */ async function addEmotionReply(config: any, data: any, log?: any): Promise<void> { if (!data.msgId || !data.conversationId) return; try { const token = await getAccessToken(config); await axios.post(`${DINGTALK_API}/v1.0/robot/emotion/reply`, { robotCode: data.robotCode ?? config.clientId, openMsgId: data.msgId, openConversationId: data.conversationId, emotionType: 2, emotionName: '🤔思考中', textEmotion: { emotionId: '2659900', emotionName: '🤔思考中', text: '🤔思考中', backgroundId: 'im_bg_1', }, }, { headers: { 'x-acs-dingtalk-access-token': token, 'Content-Type': 'application/json' }, timeout: 5_000, }); log?.info?.(`[DingTalk][Emotion] 贴表情成功: msgId=${data.msgId}`); } catch (err: any) { log?.warn?.(`[DingTalk][Emotion] 贴表情失败(不影响主流程): ${err.message}`); } } /** 撤回用户消息上的 🤔思考中 表情 */ async function recallEmotionReply(config: any, data: any, log?: any): Promise<void> { if (!data.msgId || !data.conversationId) return; try { const token = await getAccessToken(config); await axios.post(`${DINGTALK_API}/v1.0/robot/emotion/recall`, { robotCode: data.robotCode ?? config.clientId, openMsgId: data.msgId, openConversationId: data.conversationId, emotionType: 2, emotionName: '🤔思考中', textEmotion: { emotionId: '2659900', emotionName: '🤔思考中', text: '🤔思考中', backgroundId: 'im_bg_1', }, }, { headers: { 'x-acs-dingtalk-access-token': token, 'Content-Type': 'application/json' }, timeout: 5_000, }); log?.info?.(`[DingTalk][Emotion] 撤回表情成功: msgId=${data.msgId}`); } catch (err: any) { log?.warn?.(`[DingTalk][Emotion] 撤回表情失败(不影响主流程): ${err.message}`); } } // ============ 核心消息处理 (AI Card Streaming) ============ async function handleDingTalkMessage(params: { cfg: ClawdbotConfig; accountId: string; data: any; sessionWebhook: string; log?: any; dingtalkConfig: any; }): Promise<void> { const { cfg, accountId, data, sessionWebhook, log, dingtalkConfig } = params; const content = extractMessageContent(data); if (!content.text && content.imageUrls.length === 0 && content.downloadCodes.length === 0) return; const isDirect = data.conversationType === '1'; const senderId = data.senderStaffId || data.senderId; const senderName = data.senderNick || 'Unknown'; log?.info?.(`[DingTalk] 收到消息: from=${senderName} type=${content.messageType} text="${content.text.slice(0, 50)}..." images=${content.imageUrls.length} downloadCodes=${content.downloadCodes.length}`); // ===== DM Policy 检查 ===== if (isDirect) { const dmPolicy = dingtalkConfig.dmPolicy || 'open'; const allowFrom: string[] = dingtalkConfig.allowFrom || []; if (dmPolicy === 'allowlist' && allowFrom.length > 0 && !allowFrom.includes(senderId)) { log?.warn?.(`[DingTalk] DM 被拦截: senderId=${senderId} 不在 allowFrom 白名单中`); return; } } // 构建 OpenClaw 标准会话上下文 // 兼容旧配置:sessionTimeout 已废弃,打印警告 if (dingtalkConfig.sessionTimeout !== undefined) { log?.warn?.(`[DingTalk][Deprecation] 'sessionTimeout' 配置已废弃,会话超时由 OpenClaw Gateway 的 session.reset 配置控制`); } const separateSessionByConversation = dingtalkConfig.separateSessionByConversation as boolean | undefined; const groupSessionScope = dingtalkConfig.groupSessionScope as 'group' | 'group_sender' | undefined; const sessionContext = buildSessionContext({ accountId, senderId, senderName, conversationType: data.conversationType, conversationId: data.conversationId, groupSubject: data.conversationTitle, separateSessionByConversation, groupSessionScope, }); const sessionContextJson = JSON.stringify(sessionContext); log?.info?.(`[DingTalk][Session] context=${sessionContextJson}`); // memoryUser 用于 Gateway 区分记忆归属 // 使用 peerId(不包含中文)作为标识符,避免 HTTP Header 编码问题 const memoryUser = dingtalkConfig.sharedMemoryAcrossConversations === true ? accountId : `${sessionContext.channel}:${sessionContext.accountId}:${sessionContext.peerId}`; // Gateway 认证:优先使用 token,其次 password const gatewayAuth = dingtalkConfig.gatewayToken || dingtalkConfig.gatewayPassword || ''; // 构建 system prompts & 获取 oapi token(用于图片和文件后处理) const systemPrompts: string[] = []; let oapiToken: string | null = null; if (dingtalkConfig.enableMediaUpload !== false) { // 添加图片和文件使用提示(告诉 LLM 直接输出本地路径或文件标记) systemPrompts.push(buildMediaSystemPrompt()); // 获取 token 用于后处理上传 oapiToken = await getOapiAccessToken(dingtalkConfig); log?.info?.(`[DingTalk][Media] oapiToken 获取${oapiToken ? '成功' : '失败'}`); } else { log?.info?.(`[DingTalk][Media] enableMediaUpload=false,跳过`); } // 自定义 system prompt if (dingtalkConfig.systemPrompt) { systemPrompts.push(dingtalkConfig.systemPrompt); } // ===== 图片下载到本地文件(用于 OpenClaw AgentMediaPayload) ===== const imageLocalPaths: string[] = []; // 处理直接图片 URL(来自 richText 的 pictureUrl) for (const url of content.imageUrls) { if (url.startsWith('downloadCode:')) { // 通过 downloadCode 下载 const code = url.slice('downloadCode:'.length); const localPath = await downloadMediaByCode(code, dingtalkConfig, log); if (localPath) imageLocalPaths.push(localPath); } else { // 直接 URL 下载 const localPath = await downloadImageToFile(url, log); if (localPath) imageLocalPaths.push(localPath); } } // 处理 downloadCode(来自 picture 消息,fileNames 为空的是图片) for (let i = 0; i < content.downloadCodes.length; i++) { const code = content.downloadCodes[i]; const fileName = content.fileNames[i]; // 有 fileName 说明是文件,否则是图片 if (!fileName) { const localPath = await downloadMediaByCode(code, dingtalkConfig, log); if (localPath) imageLocalPaths.push(localPath); } } if (imageLocalPaths.length > 0) { log?.info?.(`[DingTalk][Image] 成功下载 ${imageLocalPaths.length} 张图片到本地`); } // ===== 文件附件下载与内容提取 ===== const fileContentParts: string[] = []; for (let i = 0; i < content.downloadCodes.length; i++) { const code = content.downloadCodes[i]; const fileName = content.fileNames[i]; if (!fileName) continue; // 图片已在上面处理 const ext = path.extname(fileName).toLowerCase(); const localPath = await downloadFileByCode(code, fileName, dingtalkConfig, log); if (!localPath) { fileContentParts.push(`[文件下载失败: ${fileName}]`); continue; } if (TEXT_FILE_EXTENSIONS.has(ext)) { // 文本类文件:读取内容追加到消息 try { const fileContent = fs.readFileSync(localPath, 'utf-8'); const maxLen = 50_000; // 限制最大读取长度 const truncated = fileContent.length > maxLen ? fileContent.slice(0, maxLen) + '\n...(内容过长,已截断)' : fileContent; fileContentParts.push(`[文件: ${fileName}]\n\`\`\`\n${truncated}\n\`\`\``); log?.info?.(`[DingTalk][File] 文本文件已读取: ${fileName}, size=${fileContent.length}`); } catch (err: any) { log?.error?.(`[DingTalk][File] 读取文本文件失败: ${err.message}`); fileContentParts.push(`[文件已保存: ${localPath},但读取内容失败]`); } } else if (ext === '.docx') { // Word 文档:用 mammoth 提取纯文本 try { const mammoth = await import('mammoth'); const result = await mammoth.default.extractRawText({ path: localPath }); const fileContent = result.value; const maxLen = 50_000; const truncated = fileContent.length > maxLen ? fileContent.slice(0, maxLen) + '\n...(内容过长,已截断)' : fileContent; fileContentParts.push(`[文件: ${fileName}]\n\`\`\`\n${truncated}\n\`\`\``); log?.info?.(`[DingTalk][File] Word 文档已提取文本: ${fileName}, size=${fileContent.length}`); } catch (err: any) { log?.error?.(`[DingTalk][File] Word 文档文本提取失败: ${err.message}`); fileContentParts.push(`[文件已保存: ${localPath},但提取文本失败]`); } } else if (ext === '.pdf') { // PDF 文档:用 pdf-parse 提取纯文本 try { const pdfParse = (await import('pdf-parse')).default; const dataBuffer = fs.readFileSync(localPath); const pdfData = await pdfParse(dataBuffer); const fileContent = pdfData.text; const maxLen = 50_000; const truncated = fileContent.length > maxLen ? fileContent.slice(0, maxLen) + '\n...(内容过长,已截断)' : fileContent; fileContentParts.push(`[文件: ${fileName}]\n\`\`\`\n${truncated}\n\`\`\``); log?.info?.(`[DingTalk][File] PDF 文档已提取文本: ${fileName}, size=${fileContent.length}`); } catch (err: any) { log?.error?.(`[DingTalk][File] PDF 文档文本提取失败: ${err.message}`); fileContentParts.push(`[文件已保存: ${localPath},但提取文本失败]`); } } else { // Office/二进制文件:保存到本地,提示路径 fileContentParts.push(`[文件已保存: ${localPath},请基于文件名和上下文回答]`); log?.info?.(`[DingTalk][File] 文件已保存: ${fileName} -> ${localPath}`); } } // 对于纯图片消息(无文本),添加默认提示 // 文本部分先经过 normalizeSlashCommand,统一将 /reset /clear 等别名指令转为 /new,再交由 Gateway 解析 const rawText = content.text || ''; let userContent = normalizeSlashCommand(rawText) || (imageLocalPaths.length > 0 ? '请描述这张图片' : ''); // 追加文件内容 if (fileContentParts.length > 0) { const fileText = fileContentParts.join('\n\n'); userContent = userContent ? `${userContent}\n\n${fileText}` : fileText; } if (!userContent && imageLocalPaths.length === 0) return; // ===== 贴处理中表情 ===== await addEmotionReply(dingtalkConfig, data, log); try { // ===== 异步模式:立即回执 + 后台执行 + 主动推送结果 ===== const asyncMode = dingtalkConfig.asyncMode === true; const proactiveTarget = isDirect ? { userId: data.senderStaffId || data.senderId } : { openConversationId: data.conversationId }; if (asyncMode) { const ackText = dingtalkConfig.ackText || '🫡 任务已接收,处理中...'; try { await sendProactive(dingtalkConfig, proactiveTarget, ackText, { msgType: 'text', useAICard: false, fallbackToNormal: true, log, }); } catch (ackErr: any) { log?.warn?.(`[DingTalk][Async] 回执发送失败: ${ackErr?.message || ackErr}`); } // 计算 peerKind 和 peerId 用于 bindings 匹配 const peerKind: 'direct' | 'group' = isDirect ? 'direct' : 'group'; const peerId = senderId; let fullResponse = ''; try { for await (const chunk of streamFromGateway({ userContent, systemPrompts, sessionKey: sessionContextJson, gatewayAuth, gatewayBaseUrl: dingtalkConfig.gatewayBaseUrl, memoryUser, imageLocalPaths: imageLocalPaths.length > 0 ? imageLocalPaths : undefined, peerKind, peerId, gatewayPort: cfg.gateway?.port, log, }, accountId)) { fullResponse += chunk; } log?.info?.(`[DingTalk][Async] Gateway 完成,原始长度=${fullResponse.length}`); // 后处理01:上传本地图片到钉钉,替换 file:// 路径为 media_id fullResponse = await processLocalImages(fullResponse, oapiToken, log); // 后处理02:提取视频标记并发送视频消息(主动 API) const proactiveMediaTarget: AICardTarget = isDirect ? { type: 'user', userId: data.senderStaffId || data.senderId } : { type: 'group', openConversationId: data.conversationId }; fullResponse = await processVideoMarkers(fullResponse, '', dingtalkConfig, oapiToken, log, true, proactiveMediaTarget); // 后处理03:提取音频标记并发送音频消息(主动 API) fullResponse = await processAudioMarkers(fullResponse, '', dingtalkConfig, oapiToken, log, true, proactiveMediaTarget); // 后处理04:提取文件标记并发送独立文件消息(主动 API) fullResponse = await processFileMarkers(fullResponse, '', dingtalkConfig, oapiToken, log, true, proactiveMediaTarget); const finalText = fullResponse.trim() || '✅ 任务执行完成(无文本输出)'; await sendProactive(dingtalkConfig, proactiveTarget, finalText, { msgType: 'markdown', useAICard: false, fallbackToNormal: true, log, }); log?.info?.(`[DingTalk][Async] 结果已主动推送,长度=${finalText.length}`); } catch (err: any) { const errMsg = `⚠️ 任务执行失败: ${err?.message || err}`; log?.error?.(`[DingTalk][Async] ${errMsg}`); try { await sendProactive(dingtalkConfig, proactiveTarget, errMsg, { msgType: 'text', useAICard: false, fallbackToNormal: true, log, }); } catch (sendErr: any) { log?.error?.(`[DingTalk][Async] 错误通知发送失败: ${sendErr?.message || sendErr}`); } } return; } // 计算 peerKind 和 peerId 用于 bindings 匹配(在 asyncMode 外部定义,供所有分支使用) const peerKind: 'direct' | 'group' = isDirect ? 'direct' : 'group'; const peerId = senderId; // 尝试创建 AI Card const card = await createAICard(dingtalkConfig, data, log); if (card) { // ===== AI Card 流式模式 ===== log?.info?.(`[DingTalk] AI Card 创建成功: ${card.cardInstanceId}`); let accumulated = ''; let lastUpdateTime = 0; const updateInterval = 300; // 最小更新间隔 ms let chunkCount = 0; try { log?.info?.(`[DingTalk] 开始请求 Gateway 流式接口...`); for await (const chunk of streamFromGateway({ userContent, systemPrompts, sessionKey: sessionContextJson, gatewayAuth, gatewayBaseUrl: dingtalkConfig.gatewayBaseUrl, memoryUser, imageLocalPaths: imageLocalPaths.length > 0 ? imageLocalPaths : undefined, peerKind, peerId, gatewayPort: cfg.gateway?.port, log, }, accountId)) { accumulated += chunk; chunkCount++; if (chunkCount <= 3) { log?.info?.(`[DingTalk] Gateway chunk #${chunkCount}: "${chunk.slice(0, 50)}..." (accumulated=${accumulated.length})`); } // 节流更新,避免过于频繁 const now = Date.now(); if (now - lastUpdateTime >= updateInterval) { // 实时清理文件、视频、音频标记(避免用户在流式过程中看到标记) const displayContent = accumulated .replace(FILE_MARKER_PATTERN, '') .replace(VIDEO_MARKER_PATTERN, '') .replace(AUDIO_MARKER_PATTERN, '') .trim(); await streamAICard(card, displayContent, false, log); lastUpdateTime = now; } } log?.info?.(`[DingTalk] Gateway 流完成,共 ${chunkCount} chunks, ${accumulated.length} 字符`); // 后处理01:上传本地图片到钉钉,替换 file:// 路径为 media_id log?.info?.(`[DingTalk][Media] 开始图片后处理,内容片段="${accumulated.slice(0, 200)}..."`); accumulated = await processLocalImages(accumulated, oapiToken, log); // 【关键修复】AI Card 场景使用主动消息 API 发送文件/视频,避免 sessionWebhook 失效问题 // 构建目标信息用于主动 API(isDirect 已在上面定义) const proactiveTarget: AICardTarget = isDirect ? { type: 'user', userId: data.senderStaffId || data.senderId } : { type: 'group', openConversationId: data.conversationId }; // 后处理02:提取视频标记并发送视频消息(使用主动消息 API) log?.info?.(`[DingTalk][Video] 开始视频后处理 (使用主动API)`); accumulated = await processVideoMarkers(accumulated, '', dingtalkConfig, oapiToken, log, true, proactiveTarget); // 后处理03:提取音频标记并发送音频消息(使用主动消息 API) log?.info?.(`[DingTalk][Audio] 开始音频后处理 (使用主动API)`); accumulated = await processAudioMarkers(accumulated, '', dingtalkConfig, oapiToken, log, true, proactiveTarget); // 后处理04:提取文件标记并发送独立文件消息(使用主动消息 API) log?.info?.(`[DingTalk][File] 开始文件后处理 (使用主动API,目标=${JSON.stringify(proactiveTarget)})`); accumulated = await processFileMarkers(accumulated, sessionWebhook, dingtalkConfig, oapiToken, log, true, proactiveTarget); // 完成 AI Card(如果内容为空,说明是纯媒体消息,使用默认提示) const finalContent = accumulated.trim(); if (finalContent.length === 0) { log?.info?.(`[DingTalk][AICard] 内容为空(纯媒体消息),使用默认提示`); await finishAICard(card, '✅ 媒体已发送', log); } else { await finishAICard(card, finalContent, log); } log?.info?.(`[DingTalk] 流式响应完成,共 ${finalContent.length} 字符`); } catch (err: any) { log?.error?.(`[DingTalk] Gateway 调用失败: ${err.message}`); log?.error?.(`[DingTalk] 错误详情: ${err.stack}`); accumulated += `\n\n⚠️ 响应中断: ${err.message}`; try { await finishAICard(card, accumulated, log); } catch (finishErr: any) { log?.error?.(`[DingTalk] 错误恢复 finish 也失败: ${finishErr.message}`); } } } else { // ===== 降级:普通消息模式 ===== log?.warn?.(`[DingTalk] AI Card 创建失败,降级为普通消息`); let fullResponse = ''; try { for await (const chunk of streamFromGateway({ userContent, systemPrompts, sessionKey: sessionContextJson, gatewayAuth, gatewayBaseUrl: dingtalkConfig.gatewayBaseUrl, memoryUser, imageLocalPaths: imageLocalPaths.length > 0 ? imageLocalPaths : undefined, peerKind, peerId, gatewayPort: cfg.gateway?.port, log, }, accountId)) { fullResponse += chunk; } // 后处理01:上传本地图片到钉钉,替换 file:// 路径为 media_id log?.info?.(`[DingTalk][Media] (降级模式) 开始图片后处理,内容片段="${fullResponse.slice(0, 200)}..."`); fullResponse = await processLocalImages(fullResponse, oapiToken, log); // 后处理02:提取视频标记并发送视频消息 log?.info?.(`[DingTalk][Video] (降级模式) 开始视频后处理`); fullResponse = await processVideoMarkers(fullResponse, sessionWebhook, dingtalkConfig, oapiToken, log); // 后处理03:提取音频标记并发送音频消息 log?.info?.(`[DingTalk][Audio] (降级模式) 开始音频后处理`); fullResponse = await processAudioMarkers(fullResponse, sessionWebhook, dingtalkConfig, oapiToken, log); // 后处理04:提取文件标记并发送独立文件消息 log?.info?.(`[DingTalk][File] (降级模式) 开始文件后处理`); fullResponse = await processFileMarkers(fullResponse, sessionWebhook, dingtalkConfig, oapiToken, log); await sendMessage(dingtalkConfig, sessionWebhook, fullResponse || '(无响应)', { atUserId: !isDirect ? senderId : null, useMarkdown: true, }); log?.info?.(`[DingTalk] 普通消息回复完成,共 ${fullResponse.length} 字符`); } catch (err: any) { log?.error?.(`[DingTalk] Gateway 调用失败: ${err.message}`); await sendMessage(dingtalkConfig, sessionWebhook, `抱歉,处理请求时出错: ${err.message}`, { atUserId: !isDirect ? senderId : null, }); } } } finally { // ===== 撤回处理中表情 ===== await recallEmotionReply(dingtalkConfig, data, log); } } // ============ 钉钉文档 API ============ /** 文档信息接口 */ interface DocInfo { docId: string; title: string; docType: string; creatorId?: string; updatedAt?: string; } /** 文档内容块 */ interface DocBlock { blockId: string; blockType: string; text?: string; children?: DocBlock[]; } /** * 钉钉文档客户端 * 支持读写钉钉在线文档(文档、表格等) */ class DingtalkDocsClient { private config: any; private log?: any; constructor(config: any, log?: any) { this.config = config; this.log = log; } /** 获取带鉴权的请求头 */ private async getHeaders(): Promise<Record<string, string>> { const token = await getAccessToken(this.config); return { 'x-acs-dingtalk-access-token': token, 'Content-Type': 'application/json', }; } /** * 获取文档元信息 * @param spaceId 空间 ID * @param docId 文档 ID */ async getDocInfo(spaceId: string, docId: string): Promise<DocInfo | null> { try { const headers = await this.getHeaders(); this.log?.info?.(`[DingTalk][Docs] 获取文档信息: spaceId=${spaceId}, docId=${docId}`); const resp = await axios.get( `${DINGTALK_API}/v1.0/doc/spaces/${spaceId}/docs/${docId}`, { headers, timeout: 10_000 }, ); const data = resp.data; this.log?.info?.(`[DingTalk][Docs] 文档信息获取成功: title=${data?.title}`); return { docId: data.docId || docId, title: data.title || '', docType: data.docType || 'unknown', creatorId: data.creatorId, updatedAt: data.updatedAt, }; } catch (err: any) { this.log?.error?.(`[DingTalk][Docs] 获取文档信息失败: ${err.message}`); return null; } } /** * 读取文档内容(通过 v2.0/wiki 节点 API) * @param nodeId 知识库节点 ID * @param operatorId 操作者 unionId(必须) */ async readDoc(nodeId: string, operatorId?: string): Promise<string | null> { try { const headers = await this.getHeaders(); this.log?.info?.(`[DingTalk][Docs] 读取知识库节点: nodeId=${nodeId}, operatorId=${operatorId}`); if (!operatorId) { this.log?.error?.('[DingTalk][Docs] readDoc 需要 operatorId(unionId)'); return null; } const resp = await axios.get( `${DINGTALK_API}/v2.0/wiki/nodes/${nodeId}`, { headers, params: { operatorId }, timeout: 15_000 }, ); const node = resp.data?.node || resp.data; const name = node.name || '未知文档'; const category = node.category || 'unknown'; const url = node.url || ''; const workspaceId = node.workspaceId || ''; const content = [ `文档名: ${name}`, `类型: ${category}`, `URL: ${url}`, `工作区: ${workspaceId}`, ].join('\n'); this.log?.info?.(`[DingTalk][Docs] 节点信息获取成功: name=${name}, category=${category}`); return content; } catch (err: any) { this.log?.error?.(`[DingTalk][Docs] 读取节点失败: ${err.message}`); if (err.response) { this.log?.error?.(`[DingTalk][Docs] 错误详情: status=${err.response.status} data=${JSON.stringify(err.response.data)}`); } return null; } } /** * 从 block 树中递归提取纯文本内容 */ private extractTextFromBlocks(blocks: DocBlock[]): string[] { const result: string[] = []; for (const block of blocks) { if (block.text) { result.push(block.text); } if (block.children && block.children.length > 0) { result.push(...this.extractTextFromBlocks(block.children)); } } return result; } /** * 向文档追加内容 * @param docId 文档 ID * @param content 要追加的文本内容 * @param index 插入位置(-1 表示末尾) */ async appendToDoc( docId: string, content: string, index: number = -1, ): Promise<boolean> { try { const headers = await this.getHeaders(); this.log?.info?.(`[DingTalk][Docs] 向文档追加内容: docId=${docId}, contentLen=${content.length}`); const body = { blockType: 'PARAGRAPH', body: { text: content, }, index, }; await axios.post( `${DINGTALK_API}/v1.0/doc/documents/${docId}/blocks/root/children`, body, { headers, timeout: 10_000 }, ); this.log?.info?.(`[DingTalk][Docs] 内容追加成功`); return true; } catch (err: any) { this.log?.error?.(`[DingTalk][Docs] 追加内容失败: ${err.message}`); if (err.response) { this.log?.error?.(`[DingTalk][Docs] 错误详情: status=${err.response.status} data=${JSON.stringify(err.response.data)}`); } return false; } } /** * 创建新文档 * @param spaceId 空间 ID * @param title 文档标题 * @param content 初始内容(可选) */ async createDoc( spaceId: string, title: string, content?: string, ): Promise<DocInfo | null> { try { const headers = await this.getHeaders(); this.log?.info?.(`[DingTalk][Docs] 创建文档: spaceId=${spaceId}, title=${title}`); const body: any = { spaceId, parentDentryId: '', name: title, docType: 'alidoc', }; const resp = await axios.post( `${DINGTALK_API}/v1.0/doc/spaces/${spaceId}/docs`, body, { headers, timeout: 10_000 }, ); const data = resp.data; this.log?.info?.(`[DingTalk][Docs] 文档创建成功: docId=${data?.docId}`); const docInfo: DocInfo = { docId: data.docId || data.dentryUuid || '', title: title, docType: data.docType || 'alidoc', }; // 如果有初始内容,追加到文档 if (content && docInfo.docId) { await this.appendToDoc(docInfo.docId, content); } return docInfo; } catch (err: any) { this.log?.error?.(`[DingTalk][Docs] 创建文档失败: ${err.message}`); if (err.response) { this.log?.error?.(`[DingTalk][Docs] 错误详情: status=${err.response.status} data=${JSON.stringify(err.response.data)}`); } return null; } } /** * 搜索文档 * @param keyword 搜索关键词 * @param spaceId 空间 ID(可选,不填则搜索所有空间) */ async searchDocs( keyword: string, spaceId?: string, ): Promise<DocInfo[]> { try { const headers = await this.getHeaders(); this.log?.info?.(`[DingTalk][Docs] 搜索文档: keyword=${keyword}, spaceId=${spaceId || '全部'}`); const body: any = { keyword, maxResults: 20 }; if (spaceId) body.spaceId = spaceId; const resp = await axios.post( `${DINGTALK_API}/v1.0/doc/docs/search`, body, { headers, timeout: 10_000 }, ); const items = resp.data?.items || []; const docs: DocInfo[] = items.map((item: any) => ({ docId: item.docId || item.dentryUuid || '', title: item.name || item.title || '', docType: item.docType || 'unknown', creatorId: item.creatorId, updatedAt: item.updatedAt, })); this.log?.info?.(`[DingTalk][Docs] 搜索到 ${docs.length} 个文档`); return docs; } catch (err: any) { this.log?.error?.(`[DingTalk][Docs] 搜索文档失败: ${err.message}`); return []; } } /** * 列出空间下的文档 * @param spaceId 空间 ID * @param parentId 父目录 ID(可选,不填则列出根目录) */ async listDocs( spaceId: string, parentId?: string, ): Promise<DocInfo[]> { try { const headers = await this.getHeaders(); this.log?.info?.(`[DingTalk][Docs] 列出文档: spaceId=${spaceId}, parentId=${parentId || '根目录'}`); const params: any = { maxResults: 50 }; if (parentId) params.parentDentryId = parentId; const resp = await axios.get( `${DINGTALK_API}/v1.0/doc/spaces/${spaceId}/dentries`, { headers, params, timeout: 10_000 }, ); const items = resp.data?.items || []; const docs: DocInfo[] = items.map((item: any) => ({ docId: item.dentryUuid || item.docId || '', title: item.name || '', docType: item.docType || item.dentryType || 'unknown', creatorId: item.creatorId, updatedAt: item.updatedAt, })); this.log?.info?.(`[DingTalk][Docs] 列出 ${docs.length} 个文档/目录`); return docs; } catch (err: any) { this.log?.error?.(`[DingTalk][Docs] 列出文档失败: ${err.message}`); return []; } } } // ============ 插件定义 ============ const meta = { id: 'dingtalk-connector', label: 'DingTalk', selectionLabel: 'DingTalk (钉钉)', docsPath: '/channels/dingtalk-connector', docsLabel: 'dingtalk-connector', blurb: '钉钉企业内部机器人,使用 Stream 模式,无需公网 IP,支持 AI Card 流式响应。', order: 70, aliases: ['dd', 'ding'], }; const dingtalkPlugin = { id: 'dingtalk-connector', meta, capabilities: { chatTypes: ['direct', 'group'], reactions: false, threads: false, media: true, nativeCommands: false, blockStreaming: false, }, reload: { configPrefixes: ['channels.dingtalk-connector'] }, configSchema: { schema: { type: 'object', additionalProperties: false, properties: { enabled: { type: 'boolean', default: true }, clientId: { type: 'string', description: 'DingTalk App Key (Client ID)' }, clientSecret: { type: 'string', description: 'DingTalk App Secret (Client Secret)' }, enableMediaUpload: { type: 'boolean', default: true, description: 'Enable media upload prompt injection' }, systemPrompt: { type: 'string', default: '', description: 'Custom system prompt' }, dmPolicy: { type: 'string', enum: ['open', 'pairing', 'allowlist'], default: 'open' }, allowFrom: { type: 'array', items: { type: 'string' }, description: 'Allowed sender IDs' }, groupPolicy: { type: 'string', enum: ['open', 'allowlist'], default: 'open' }, gatewayToken: { type: 'string', default: '', description: 'Gateway auth token (Bearer)' }, gatewayPassword: { type: 'string', default: '', description: 'Gateway auth password (alternative to token)' }, gatewayBaseUrl: { type: 'string', default: '', description: 'Custom Gateway URL (e.g., http://127.0.0.1:18788 for Nginx proxy to TLS Gateway)' }, sessionTimeout: { type: 'number', default: 1800000, description: 'Session timeout in ms (default 30min)' }, separateSessionByConversation: { type: 'boolean', default: true, description: '是否按单聊/群聊/群区分 session' }, sharedMemoryAcrossConversations: { type: 'boolean', default: false, description: '单 agent 场景下是否共享记忆;false 时不同群聊、群聊与私聊记忆隔离' }, asyncMode: { type: 'boolean', default: false, description: 'Send immediate ack and push final result as a second message' }, ackText: { type: 'string', default: '🫡 任务已接收,处理中...', description: 'Ack text when asyncMode is enabled' }, debug: { type: 'boolean', default: false }, }, required: ['clientId', 'clientSecret'], }, uiHints: { enabled: { label: 'Enable DingTalk' }, clientId: { label: 'App Key', sensitive: false }, clientSecret: { label: 'App Secret', sensitive: true }, dmPolicy: { label: 'DM Policy' }, groupPolicy: { label: 'Group Policy' }, }, }, config: { listAccountIds: (cfg: ClawdbotConfig) => { const config = getConfig(cfg); // __default__ 是内部标记,表示使用顶层配置(单账号模式) return config.accounts ? Object.keys(config.accounts) : (isConfigured(cfg) ? ['__default__'] : []); }, resolveAccount: (cfg: ClawdbotConfig, accountId?: string) => { const config = getConfig(cfg); const id = accountId || DEFAULT_ACCOUNT_ID; if (config.accounts?.[id]) { // 合并 channel 级别配置(如 gatewayBaseUrl)到 account 配置 const { accounts, ...channelConfig } = config; const mergedConfig = { ...channelConfig, ...config.accounts[id] }; return { accountId: id, config: mergedConfig, enabled: config.accounts[id].enabled !== false }; } // 没有 accounts 配置或找不到指定账号时,使用顶层配置 return { accountId: DEFAULT_ACCOUNT_ID, config, enabled: config.enabled !== false }; }, defaultAccountId: () => '__default__', isConfigured: (account: any) => Boolean(account.config?.clientId && account.config?.clientSecret), describeAccount: (account: any) => ({ accountId: account.accountId, name: account.config?.name || 'DingTalk', enabled: account.enabled, configured: Boolean(account.config?.clientId), }), }, security: { resolveDmPolicy: ({ account }: any) => ({ policy: account.config?.dmPolicy || 'open', allowFrom: account.config?.allowFrom || [], policyPath: 'channels.dingtalk-connector.dmPolicy', allowFromPath: 'channels.dingtalk-connector.allowFrom', approveHint: '使用 /allow dingtalk-connector:<userId> 批准用户', normalizeEntry: (raw: string) => raw.replace(/^(dingtalk-connector|dingtalk|dd|ding):/i, ''), }), }, groups: { resolveRequireMention: ({ cfg }: any) => getConfig(cfg).groupPolicy !== 'open', }, messaging: { // 注意:normalizeTarget 接收字符串,返回字符串(保持大小写,因为 openConversationId 是 base64 编码) normalizeTarget: (raw: string) => { if (!raw) return undefined; // 去掉渠道前缀,但保持原始大小写 return raw.trim().replace(/^(dingtalk-connector|dingtalk|dd|ding):/i, ''); }, targetResolver: { // 支持普通 ID、Base64 编码的 conversationId,以及 user:/group: 前缀格式 looksLikeId: (id: string) => /^(user:|group:)?[\w+/=-]+$/.test(id), hint: 'user:<userId> 或 group:<conversationId>', }, }, outbound: { deliveryMode: 'direct' as const, textChunkLimit: 4000, /** * 主动发送文本消息 * @param ctx.to 目标格式:user:<userId> 或 group:<openConversationId> * @param ctx.text 消息内容 * @param ctx.accountId 账号 ID */ sendText: async (ctx: any) => { const { cfg, to, text, accountId, log } = ctx; const account = dingtalkPlugin.config.resolveAccount(cfg, accountId); const config = account?.config; if (!config?.clientId || !config?.clientSecret) { throw new Error('DingTalk not configured'); } if (!to) { throw new Error('Target is required. Format: user:<userId> or group:<openConversationId>'); } // 解析目标:user:<userId> 或 group:<openConversationId> const targetStr = String(to); let result: SendResult; log?.info?.(`[DingTalk][outbound.sendText] 解析目标: targetStr="${targetStr}"`); if (targetStr.startsWith('user:')) { const userId = targetStr.slice(5); log?.info?.(`[DingTalk][outbound.sendText] 发送给用户: userId="${userId}"`); result = await sendToUser(config, userId, text, { log }); } else if (targetStr.startsWith('group:')) { const openConversationId = targetStr.slice(6); log?.info?.(`[DingTalk][outbound.sendText] 发送到群: openConversationId="${openConversationId}"`); result = await sendToGroup(config, openConversationId, text, { log }); } else { // 默认当作 userId 处理 log?.info?.(`[DingTalk][outbound.sendText] 默认发送给用户: userId="${targetStr}"`); result = await sendToUser(config, targetStr, text, { log }); } if (result.ok) { return { channel: 'dingtalk-connector', messageId: result.processQueryKey || 'unknown' }; } throw new Error(result.error || 'Failed to send message'); }, /** * 主动发送媒体消息(图片) * @param ctx.to 目标格式:user:<userId> 或 group:<openConversationId> * @param ctx.text 消息文本/标题 * @param ctx.mediaUrl 媒体 URL(钉钉仅支持图片 URL) * @param ctx.accountId 账号 ID */ sendMedia: async (ctx: any) => { const { cfg, to, text, mediaUrl, accountId, log } = ctx; const account = dingtalkPlugin.config.resolveAccount(cfg, accountId); const config = account?.config; if (!config?.clientId || !config?.clientSecret) { throw new Error('DingTalk not configured'); } if (!to) { throw new Error('Target is required. Format: user:<userId> or group:<openConversationId>'); } // 解析目标 const targetStr = String(to); let result: SendResult; // 如果有媒体 URL,发送图片消息 if (mediaUrl) { if (targetStr.startsWith('user:')) { const userId = targetStr.slice(5); result = await sendToUser(config, userId, mediaUrl, { msgType: 'image', log }); } else if (targetStr.startsWith('group:')) { const openConversationId = targetStr.slice(6); result = await sendToGroup(config, openConversationId, mediaUrl, { msgType: 'image', log }); } else { result = await sendToUser(config, targetStr, mediaUrl, { msgType: 'image', log }); } } else { // 无媒体,发送文本 if (targetStr.startsWith('user:')) { const userId = targetStr.slice(5); result = await sendToUser(config, userId, text || '', { log }); } else if (targetStr.startsWith('group:')) { const openConversationId = targetStr.slice(6); result = await sendToGroup(config, openConversationId, text || '', { log }); } else { result = await sendToUser(config, targetStr, text || '', { log }); } } if (result.ok) { return { channel: 'dingtalk-connector', messageId: result.processQueryKey || 'unknown' }; } throw new Error(result.error || 'Failed to send media'); }, }, gateway: { startAccount: async (ctx: any) => { const { account, cfg, abortSignal } = ctx; const config = account.config; if (!config.clientId || !config.clientSecret) { throw new Error('DingTalk clientId and clientSecret are required'); } ctx.log?.info(`[${account.accountId}] 启动钉钉 Stream 客户端...`); // 启用 DWClient 内置的 autoReconnect 和 keepAlive // - autoReconnect: 连接断开时自动重连 // - keepAlive: 启用心跳机制,防止服务端因长时间无活动而断开连接 const client = new DWClient({ clientId: config.clientId, clientSecret: config.clientSecret, debug: config.debug || false, autoReconnect: true, keepAlive: true, } as any); client.registerCallbackListener(TOPIC_ROBOT, async (res: any) => { const messageId = res.headers?.messageId; ctx.log?.info?.(`[DingTalk] 收到 Stream 回调, messageId=${messageId}, headers=${JSON.stringify(res.headers)}`); // 【关键修复】立即确认回调,避免钉钉服务器因超时而重发 // 钉钉 Stream 模式要求及时响应,否则约60秒后会重发消息 if (messageId) { client.socketCallBackResponse(messageId, { success: true }); ctx.log?.info?.(`[DingTalk] 已立即确认回调: messageId=${messageId}`); } // 【消息去重】检查是否已处理过该消息(按账号维度隔离) if (messageId && isMessageProcessed(account.accountId, messageId)) { ctx.log?.warn?.(`[DingTalk][${account.accountId}] 检测到重复消息,跳过处理: messageId=${messageId}`); return; } // 标记消息为已处理(按账号维度隔离) if (messageId) { markMessageProcessed(account.accountId, messageId); } // 异步处理消息(不阻塞回调确认) try { ctx.log?.info?.(`[DingTalk] 原始 data: ${typeof res.data === 'string' ? res.data.slice(0, 500) : JSON.stringify(res.data).slice(0, 500)}`); const data = JSON.parse(res.data); await handleDingTalkMessage({ cfg, accountId: account.accountId, data, sessionWebhook: data.sessionWebhook, log: ctx.log, dingtalkConfig: config, }); } catch (error: any) { ctx.log?.error?.(`[DingTalk] 处理消息异常: ${error.message}`); // 注意:即使处理失败,也不需要再次响应(已经提前确认了) } }); await client.connect(); ctx.log?.info(`[${account.accountId}] 钉钉 Stream 客户端已连接`); const rt = getRuntime(); rt.channel.activity.record('dingtalk-connector', account.accountId, 'start'); let stopped = false; // 统一的停止逻辑 const doStop = (reason: string) => { if (stopped) return; stopped = true; ctx.log?.info(`[${account.accountId}] 停止钉钉 Stream 客户端 (${reason})...`); try { // 【关键】调用 disconnect() 正确关闭 WebSocket 连接 client.disconnect(); } catch (err: any) { ctx.log?.warn?.(`[${account.accountId}] 断开连接时出错: ${err.message}`); } rt.channel.activity.record('dingtalk-connector', account.accountId, 'stop'); }; // 【关键修复】返回一个 Promise 并保持 pending 状态直到 abortSignal 触发 // 这样框架不会认为账号已退出,避免触发 auto-restart // 参考:OpenClaw changelog - "keep startAccount pending until abort to prevent restart-loop storms" return new Promise((resolve) => { if (abortSignal) { abortSignal.addEventListener('abort', () => { doStop('abortSignal'); resolve({ stop: () => doStop('manual'), isHealthy: () => !stopped, }); }); } }); }, }, status: { defaultRuntime: { accountId: DEFAULT_ACCOUNT_ID, running: false, lastStartAt: null, lastStopAt: null, lastError: null }, probe: async ({ cfg }: any) => { if (!isConfigured(cfg)) return { ok: false, error: 'Not configured' }; try { const config = getConfig(cfg); await getAccessToken(config); return { ok: true, details: { clientId: config.clientId } }; } catch (error: any) { return { ok: false, error: error.message }; } }, buildChannelSummary: ({ snapshot }: any) => ({ configured: snapshot?.configured ?? false, running: snapshot?.running ?? false, lastStartAt: snapshot?.lastStartAt ?? null, lastStopAt: snapshot?.lastStopAt ?? null, lastError: snapshot?.lastError ?? null, }), }, }; // ============ 插件注册 ============ const plugin = { id: 'dingtalk-connector', name: 'DingTalk Channel', description: 'DingTalk (钉钉) messaging channel via Stream mode with AI Card streaming', configSchema: { type: 'object', additionalProperties: true, properties: { enabled: { type: 'boolean', default: true } }, }, register(api: ClawdbotPluginApi) { runtime = api.runtime; api.registerChannel({ plugin: dingtalkPlugin }); // ===== Gateway Methods ===== api.registerGatewayMethod('dingtalk-connector.status', async ({ respond, cfg }: any) => { const result = await dingtalkPlugin.status.probe({ cfg }); respond(true, result); }); api.registerGatewayMethod('dingtalk-connector.probe', async ({ respond, cfg }: any) => { const result = await dingtalkPlugin.status.probe({ cfg }); respond(result.ok, result); }); /** * 主动发送单聊消息 * 参数: * - userId / userIds: 目标用户 ID(支持单个或数组) * - content: 消息内容 * - msgType?: 'text' | 'markdown' | 'link' | 'actionCard' | 'image'(降级时使用,默认 text) * - title?: markdown 消息标题 * - useAICard?: 是否使用 AI Card(默认 true) * - fallbackToNormal?: AI Card 失败时是否降级到普通消息(默认 true) * - accountId?: 使用的账号 ID(可选,不传则使用默认配置) */ api.registerGatewayMethod('dingtalk-connector.sendToUser', async ({ respond, cfg, params, log }: any) => { const { userId, userIds, content, msgType, title, useAICard, fallbackToNormal, accountId } = params || {}; const account = dingtalkPlugin.config.resolveAccount(cfg, accountId); if (!account.config?.clientId) { return respond(false, { error: 'DingTalk not configured' }); } const targetUserIds = userIds || (userId ? [userId] : []); if (targetUserIds.length === 0) { return respond(false, { error: 'userId or userIds is required' }); } if (!content) { return respond(false, { error: 'content is required' }); } const result = await sendToUser(account.config, targetUserIds, content, { msgType, title, log, useAICard: useAICard !== false, // 默认 true fallbackToNormal: fallbackToNormal !== false, // 默认 true }); respond(result.ok, result); }); /** * 主动发送群聊消息 * 参数: * - openConversationId: 群会话 ID * - content: 消息内容 * - msgType?: 'text' | 'markdown' | 'link' | 'actionCard' | 'image'(降级时使用,默认 text) * - title?: markdown 消息标题 * - useAICard?: 是否使用 AI Card(默认 true) * - fallbackToNormal?: AI Card 失败时是否降级到普通消息(默认 true) * - accountId?: 使用的账号 ID(可选,不传则使用默认配置) */ api.registerGatewayMethod('dingtalk-connector.sendToGroup', async ({ respond, cfg, params, log }: any) => { const { openConversationId, content, msgType, title, useAICard, fallbackToNormal, accountId } = params || {}; const account = dingtalkPlugin.config.resolveAccount(cfg, accountId); if (!account.config?.clientId) { return respond(false, { error: 'DingTalk not configured' }); } if (!openConversationId) { return respond(false, { error: 'openConversationId is required' }); } if (!content) { return respond(false, { error: 'content is required' }); } const result = await sendToGroup(account.config, openConversationId, content, { msgType, title, log, useAICard: useAICard !== false, // 默认 true fallbackToNormal: fallbackToNormal !== false, }); respond(result.ok, result); }); /** * 智能发送消息(自动检测目标类型和消息格式) * 参数: * - target: 目标(user:<userId> 或 group:<openConversationId>) * - content: 消息内容 * - msgType?: 消息类型(降级时使用,可选,不指定则自动检测) * - title?: 标题(用于 markdown) * - useAICard?: 是否使用 AI Card(默认 true) * - fallbackToNormal?: AI Card 失败时是否降级到普通消息(默认 true) * - accountId?: 账号 ID */ api.registerGatewayMethod('dingtalk-connector.send', async ({ respond, cfg, params, log }: any) => { const { target, content, message, msgType, title, useAICard, fallbackToNormal, accountId } = params || {}; const actualContent = content || message; // 兼容 message 字段 const account = dingtalkPlugin.config.resolveAccount(cfg, accountId); log?.info?.(`[DingTalk][Send] 收到请求: params=${JSON.stringify(params)}`); if (!account.config?.clientId) { return respond(false, { error: 'DingTalk not configured' }); } if (!target) { return respond(false, { error: 'target is required (format: user:<userId> or group:<openConversationId>)' }); } if (!actualContent) { return respond(false, { error: 'content is required' }); } const targetStr = String(target); let sendTarget: { userId?: string; openConversationId?: string }; if (targetStr.startsWith('user:')) { sendTarget = { userId: targetStr.slice(5) }; } else if (targetStr.startsWith('group:')) { sendTarget = { openConversationId: targetStr.slice(6) }; } else { // 默认当作 userId sendTarget = { userId: targetStr }; } log?.info?.(`[DingTalk][Send] 解析后目标: sendTarget=${JSON.stringify(sendTarget)}`); const result = await sendProactive(account.config, sendTarget, actualContent, { msgType, title, log, useAICard: useAICard !== false, // 默认 true fallbackToNormal: fallbackToNormal !== false, }); respond(result.ok, result); }); // ===== 文档 API Methods ===== /** * 读取钉钉知识库文档节点信息 * 参数: * - docId: 知识库节点 ID * - operatorId: 操作者 unionId 或 staffId(会自动转换为 unionId) * - accountId?: 账号 ID */ api.registerGatewayMethod('dingtalk-connector.docs.read', async ({ respond, cfg, params, log }: any) => { const { docId, operatorId: rawOperatorId, accountId } = params || {}; const account = dingtalkPlugin.config.resolveAccount(cfg, accountId); if (!account.config?.clientId) { return respond(false, { error: 'DingTalk not configured' }); } if (!docId) { return respond(false, { error: 'docId is required' }); } if (!rawOperatorId) { return respond(false, { error: 'operatorId (unionId or staffId) is required' }); } // 如果 operatorId 不像 unionId(通常以字母数字开头且较长),尝试将 staffId 转为 unionId let operatorId = rawOperatorId; if (!rawOperatorId.includes('$')) { // 可能已经是 unionId,直接使用;否则尝试转换 const resolved = await getUnionId(rawOperatorId, account.config, log); if (resolved) operatorId = resolved; } const client = new DingtalkDocsClient(account.config, log); const content = await client.readDoc(docId, operatorId); if (content !== null) { respond(true, { content }); } else { respond(false, { error: 'Failed to read document node' }); } }); /** * 创建钉钉文档 * 参数: * - spaceId: 空间 ID * - title: 文档标题 * - content?: 初始内容 * - accountId?: 账号 ID */ api.registerGatewayMethod('dingtalk-connector.docs.create', async ({ respond, cfg, params, log }: any) => { const { spaceId, title, content, accountId } = params || {}; const account = dingtalkPlugin.config.resolveAccount(cfg, accountId); if (!account.config?.clientId) { return respond(false, { error: 'DingTalk not configured' }); } if (!spaceId || !title) { return respond(false, { error: 'spaceId and title are required' }); } const client = new DingtalkDocsClient(account.config, log); const doc = await client.createDoc(spaceId, title, content); if (doc) { respond(true, doc); } else { respond(false, { error: 'Failed to create document' }); } }); /** * 向钉钉文档追加内容 * 参数: * - docId: 文档 ID * - content: 要追加的内容 * - accountId?: 账号 ID */ api.registerGatewayMethod('dingtalk-connector.docs.append', async ({ respond, cfg, params, log }: any) => { const { docId, content, accountId } = params || {}; const account = dingtalkPlugin.config.resolveAccount(cfg, accountId); if (!account.config?.clientId) { return respond(false, { error: 'DingTalk not configured' }); } if (!docId || !content) { return respond(false, { error: 'docId and content are required' }); } const client = new DingtalkDocsClient(account.config, log); const ok = await client.appendToDoc(docId, content); respond(ok, ok ? { success: true } : { error: 'Failed to append to document' }); }); /** * 搜索钉钉文档 * 参数: * - keyword: 搜索关键词 * - spaceId?: 空间 ID(可选) * - accountId?: 账号 ID */ api.registerGatewayMethod('dingtalk-connector.docs.search', async ({ respond, cfg, params, log }: any) => { const { keyword, spaceId, accountId } = params || {}; const account = dingtalkPlugin.config.resolveAccount(cfg, accountId); if (!account.config?.clientId) { return respond(false, { error: 'DingTalk not configured' }); } if (!keyword) { return respond(false, { error: 'keyword is required' }); } const client = new DingtalkDocsClient(account.config, log); const docs = await client.searchDocs(keyword, spaceId); respond(true, { docs }); }); /** * 列出空间下的文档 * 参数: * - spaceId: 空间 ID * - parentId?: 父目录 ID(可选) * - accountId?: 账号 ID */ api.registerGatewayMethod('dingtalk-connector.docs.list', async ({ respond, cfg, params, log }: any) => { const { spaceId, parentId, accountId } = params || {}; const account = dingtalkPlugin.config.resolveAccount(cfg, accountId); if (!account.config?.clientId) { return respond(false, { error: 'DingTalk not configured' }); } if (!spaceId) { return respond(false, { error: 'spaceId is required' }); } const client = new DingtalkDocsClient(account.config, log); const docs = await client.listDocs(spaceId, parentId); respond(true, { docs }); }); api.logger?.info('[DingTalk] 插件已注册(支持主动发送 AI Card 消息、文档读写)'); }, }; export default plugin; export { dingtalkPlugin, // 回复消息(需要 sessionWebhook) sendMessage, sendTextMessage, sendMarkdownMessage, // 主动发送消息(无需 sessionWebhook) sendToUser, sendToGroup, sendProactive, // 钉钉文档客户端 DingtalkDocsClient, }; // ============ 测试辅助导出 ============ // 仅用于单元测试,避免在业务代码中直接依赖内部实现细节 export const __testables = { // 会话 & 去重 normalizeSlashCommand, buildSessionContext, isMessageProcessed, markMessageProcessed, cleanupProcessedMessages, // 配置 & Token getConfig, isConfigured, getAccessToken, getOapiAccessToken, getUnionId, // 媒体处理 toLocalPath, processLocalImages, uploadMediaToDingTalk, downloadImageToFile, downloadMediaByCode, downloadFileByCode, // 视频处理 extractVideoMetadata, extractVideoThumbnail, processVideoMarkers, sendVideoMessage, // 音频处理 getFfprobePath, extractAudioDuration, sendAudioMessage, processAudioMarkers, isAudioFile, // 文件处理 extractFileMarkers, sendFileMessage, processFileMarkers, // 消息内容提取 extractMessageContent, // 消息发送 sendMarkdownMessage, sendTextMessage, sendMessage, // 提示词与消息体 buildMediaSystemPrompt, buildDeliverBody, buildMsgPayload, // AI Card createAICard, streamAICard, finishAICard, createAICardForTarget, sendFileProactive, sendAudioProactive, sendVideoProactive, sendAICardInternal, sendAICardToUser, sendAICardToGroup, // 主动消息 sendNormalToUser, sendNormalToGroup, sendToUser, sendToGroup, sendProactive, // Bindings 解析(测试时需 mock getRuntime/fs/path/os) resolveAgentIdByBindings, /** 仅测试用:注入 runtime 使 resolveAgentIdByBindings 不抛错 */ setRuntimeForTest(r: PluginRuntime | null) { runtime = r; }, }; |