agent-chat.js 102 KB
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
/**
 * Agent Chat Route — 通用 Agent 对话 SSE 端点
 *
 * 存储架构:conversations/agents/{agentId}/{conversationId}/
 *    ├── meta.json                    ← 对话元数据
 *    ├── {messageId}/messages.log     ← 每轮对话日志
 *    └── history.json                 ← 有序的消息列表(供前端展示 + 上下文恢复)
 *
 * 多轮对话:
 *    - ClaudeCliProtocol 持久进程 → 天然多轮
 *    - 服务器重启后自动恢复:从 history.json 加载历史,注入 system prompt + 历史摘要
 *
 * 使用 @mas/acp-core 的 ClaudeCliProtocol。
 */
const express = require('express');
const path = require('path');
const fs = require('fs');
const { randomUUID } = require('crypto');
const multer = require('multer');
const { ClaudeCliProtocol } = require('@mas/acp-core');
const { Parser } = require('../src/core/parser');
const AgentManager = require('../src/core/agent-manager');
const { GoalsService } = require('../services/goals');
const { saveSessionSummary, loadRecentMemory, retrieveRelevantPlans, autoReflect, consolidateMemory, generateDailyReport } = require('../services/memory-service');
const systemConfig = require('../config');
const router = express.Router();

// Agent 目录由 getAgentDir() 统一路由 (system → system/agents/, company → data/companies/{id}/agents/)

function getAgentDir(id) {
  if (id && id.includes(':')) {
    const [compId, subId] = id.split(':');
    return path.resolve(__dirname, `../../data/companies/${compId}/agents/${subId}`);
  }
  return path.resolve(__dirname, `../../system/agents/${id}`);
}
const goalsService = new GoalsService();
// CONV_ROOT is now dynamically evaluated per-agent based on DATA_DIR.

function loadSystemConfig() {
    const configDir = path.resolve(__dirname, '../config');
    const backends = JSON.parse(fs.readFileSync(path.join(configDir, 'backends.json'), 'utf-8'));
    const { loadConfigFromSkills } = require('../src/core/skill-config-loader');
    const skillsDir = systemConfig.skillsCodeDir;
    return loadConfigFromSkills(skillsDir, backends);
}

const fullSystemConfig = loadSystemConfig();
// 初始化 AgentManager 用于 Sub-Agent 调用
const agentManager = new AgentManager(fullSystemConfig, path.resolve(__dirname, '../../'));

// ═══════════════════════════════════════
//  meta_creator 硬编码执行器
//  保证 AgentScaffold 父类一定被调用
// ═══════════════════════════════════════

/**
 * 从 Innovator 的任务文本中解析 Agent 创建参数,直接调用 AgentScaffold。
 * 不依赖 Claude 子进程拼 curl 命令,100% 可靠。
 *
 * @param {string} task - Innovator 传来的任务描述
 * @param {string} parentAgentId - 父 Agent ID (如 'innovator-001')
 * @param {string} convId - 当前对话 ID
 * @param {function} sendSSE - SSE 回调
 * @param {string} callId - 调用 ID
 * @returns {Promise<{agent: string, output: string, status: string}>}
 */
async function executeMetaCreator(task, parentAgentId, convId, sendSSE, callId) {
  sendSSE('sub_agent', { event: 'chunk', callId, agent: 'meta_creator', chunk: '🏗️ [meta_creator] 开始通过 AgentScaffold 父类创建 Agent...\n' });

  // Step 1: 从任务文本中解析 Agent 名称
  // 支持多种格式: "data/agents/poetry_writer", "Agent: poetry_writer", "名称: xxx", "agent_name" 等
  let agentName = null;
  let displayName = null;

  // 尝试匹配常见模式
  const patterns = [
    /data\/agents\/(\w+)/,
    /[Aa]gent[:\s]+(\w+)/,
    /名称[:\s]*(\S+)/,
    /创建[^"]*?[「"'"]([^"'」]+)['"」]/,
    /(?:id|ID)[:\s]*['"]*(\w+)['"]*/, 
  ];
  for (const pat of patterns) {
    const m = task.match(pat);
    if (m) { agentName = m[1].replace(/[^a-zA-Z0-9_-]/g, '_').toLowerCase(); break; }
  }

  // 如果没解析到,从领域关键词生成
  if (!agentName) {
    const domainMatch = task.match(/领域[:\s]*([^\n,,]+)/);
    if (domainMatch) {
      agentName = domainMatch[1].trim()
        .replace(/[\u4e00-\u9fa5]+/g, (m) => {
          // 简单中文→拼音映射(仅常用词)
          const map = { '诗词': 'poetry', '小说': 'novel', '古典': 'classical', '创作': 'writer', '写诗': 'poetry', '写作': 'writing' };
          return map[m] || m;
        })
        .replace(/[^a-zA-Z0-9]/g, '_')
        .replace(/_+/g, '_')
        .replace(/^_|_$/g, '')
        .toLowerCase();
    }
  }
  if (!agentName) agentName = `agent_${Date.now().toString(36)}`;

  // 解析显示名称
  const displayMatch = task.match(/显示名[称:\s]*([^\n,,]+)/) || task.match(/名称[:\s]*([^\n,,]+)/);
  displayName = displayMatch ? displayMatch[1].trim() : agentName;

  sendSSE('sub_agent', { event: 'chunk', callId, agent: 'meta_creator', chunk: `📝 Agent ID: ${agentName}\n📝 显示名称: ${displayName}\n` });

  // Step 2: 读取 knowhow 和 benchmark(如果有)
  const parentDir = getAgentDir(parentAgentId);
  const workspaceDir = path.join(parentDir, 'workspace', agentName);
  let knowhow = '';
  let benchmark = '';
  
  // 搜索可能的 knowhow/benchmark 位置
  const searchDirs = [workspaceDir, path.join(parentDir, 'workspace'), path.join(parentDir, 'knowledge')];
  for (const dir of searchDirs) {
    if (!knowhow) {
      const kp = path.join(dir, 'knowhow.md');
      if (fs.existsSync(kp)) knowhow = fs.readFileSync(kp, 'utf-8');
    }
    if (!benchmark) {
      const bp = path.join(dir, 'benchmark.md');
      if (fs.existsSync(bp)) benchmark = fs.readFileSync(bp, 'utf-8');
    }
  }

  sendSSE('sub_agent', { event: 'chunk', callId, agent: 'meta_creator', chunk: `📚 Knowhow: ${knowhow ? `${knowhow.length}字` : '未找到'}\n📊 Benchmark: ${benchmark ? `${benchmark.length}字` : '未找到'}\n` });

  // Step 3: 生成基础 SKILL.md (用 knowhow + benchmark 合成)
  const skillMd = generateSkillMd(agentName, displayName, knowhow, benchmark, task);
  
  // Step 4: 调用 AgentScaffold 父类创建完整目录
  const { AgentScaffold } = await import('../../packages/agent-core/AgentScaffold.js');
  const scaffold = new AgentScaffold(path.resolve(__dirname, '../../system/agents'));
  
  const agentMd = `# ${displayName}\n\n你是 **${displayName}**,专业的 AI Agent。\n\n## 执行准则\n你的完整操作手册硬编码在 main_skill/SKILL.md 中,请严格遵照执行。\n`;

  const agentDir = await scaffold.create({
    id: agentName,
    type: 'skill', // 执行型 Agent
    name: displayName,
    parentId: parentAgentId,
    bootstrap: { agentMd }
  });

  // 写入生成的 SKILL.md
  const skillPath = path.join(agentDir, '.claude', 'skills', 'main_skill', 'SKILL.md');
  fs.writeFileSync(skillPath, skillMd, 'utf-8');

  // 如果有 knowhow/benchmark,复制到新 Agent 的 knowledge/ 下
  const knowledgeDir = path.join(agentDir, 'knowledge');
  if (knowhow) fs.writeFileSync(path.join(knowledgeDir, 'knowhow.md'), knowhow, 'utf-8');
  if (benchmark) fs.writeFileSync(path.join(knowledgeDir, 'benchmark.md'), benchmark, 'utf-8');

  const output = `✅ Agent "${displayName}" (${agentName}) 已通过 AgentScaffold 父类创建完毕。

目录: data/agents/${agentName}/
标准结构:
  ├── agent.md (身份定义)
  ├── soul.md (灵魂)
  ├── identity.md (标识)
  ├── config.json (配置,含 cli.toolPolicy)
  ├── .claude/skills/main_skill/SKILL.md (核心技能, ${skillMd.length}字)
  ├── memory/ (记忆)
  ├── knowledge/ (知识${knowhow ? ', 含 knowhow.md' : ''}${benchmark ? ', 含 benchmark.md' : ''})
  ├── workspace/ (工作区)
  └── logs/ (日志)

[RESULT: DONE]`;

  sendSSE('sub_agent', { event: 'chunk', callId, agent: 'meta_creator', chunk: output });
  console.log(`[meta_creator] ✅ Agent "${displayName}" (${agentName}) 创建完毕: ${agentDir}`);

  return { agent: 'meta_creator', output, status: 'success' };
}

/**
 * 根据 knowhow/benchmark/task 生成 SKILL.md 内容
 */
function generateSkillMd(agentName, displayName, knowhow, benchmark, task) {
  // 从 task 中提取领域信息
  const domainMatch = task.match(/领域[:\s]*([^\n,,]+)/);
  const domain = domainMatch ? domainMatch[1].trim() : displayName;

  let content = `---
name: ${agentName}
description: >
  ${displayName} — 专业的 ${domain} Agent。
  基于深度调研的方法论和标杆案例创建。
version: 1.0
domain: ${domain}
---

# ${displayName}

你是 **${displayName}**,专业的 ${domain} 执行 Agent。

## 核心方法论

`;

  if (knowhow) {
    // 提取 knowhow 中的关键部分(不超过 3000 字)
    content += knowhow.slice(0, 3000);
  } else {
    content += `(待补充 — 请通过 meta_evolving 迭代训练补充方法论)\n`;
  }

  content += `\n\n## 质量标准\n\n`;

  if (benchmark) {
    content += benchmark.slice(0, 1500);
  } else {
    content += `(待补充 — 请通过 meta_evolving 迭代训练补充评分标准)\n`;
  }

  content += `\n\n## 输出协议\n\n完成后**必须**以 \`[RESULT: DONE]\` 标记收尾。\n`;

  return content;
}

// ═══════════════════════════════════════
//  Bootstrap 工具
// ═══════════════════════════════════════

function loadFile(agentDir, filename) {
  try { return fs.readFileSync(path.join(agentDir, filename), 'utf-8'); }
  catch { return ''; }
}

// ═══════════════════════════════════════
//  基类模板目录 — 运行时继承
// ═══════════════════════════════════════
const BASE_DIR = path.join(__dirname, '../../packages/agent-core/base');

/**
 * 构建 system prompt — 三层继承
 *
 * Layer 1: base/common/*          (所有 Agent 通用的工具规范、能力说明)
 * Layer 2: base/{type}/*-base.md  (类型专属的角色定义)
 * Layer 3: data/agents/{id}/*     (个体专属的覆盖/扩展)
 *
 * 继承规则:
 * - agent.md: base/type/agent-base.md + agent/agent.md (追加)
 * - tools.md: base/common/tools-base.md + agent/tools.md (追加)
 * - 其他文件 (soul.md, identity.md 等): 只从 agent/ 读取(纯个性化)
 */
function buildSystemPrompt(agentDir, userMessage) {
  const config = loadAgentConfig(agentDir);
  const agentType = config?.type || 'skill';

  const parts = [];

  // ── Layer 0: 公司 workspace 路径注入 ──
  // 检测是否为公司 Agent (路径格式: data/<companyId>/agents/<role>)
  const agentDirNorm = agentDir.replace(/\\/g, '/');
  const companyMatch = agentDirNorm.match(/data\/companies\/([^/]+)\/agents\/([^/]+)$/);
  if (companyMatch && companyMatch[1] !== 'agents') {
    const companyId = companyMatch[1];
    const roleName = companyMatch[2];
    const companyWorkspace = `data/companies/${companyId}/workspace`;
    parts.push(`# 🏢 公司环境上下文

你隶属于公司 **${companyId}**,你的角色是 **${roleName}**。

## ⚠️ 关键路径规范(必须遵守!)
- **公司共享工作区**: \`${companyWorkspace}/\`
- **你写入的所有文件(战略蓝图、Pipeline JSON、分析报告等)都必须放在 \`${companyWorkspace}/\` 目录下**
- 🚫 **绝对禁止**写入全局的 \`workspace/\` 目录!那是旧版遗留路径!
- 🚫 **绝对禁止**写入 \`data/workspace/\` 目录!
- ✅ 正确路径示例: \`${companyWorkspace}/strategy.md\`、\`${companyWorkspace}/pipeline_design.json\`
- 你的同事(CEO/CTO/COO/HR 等)共享同一个 \`${companyWorkspace}/\` 目录,用它来交换文件和数据。
- 你自己的 Agent 目录: \`data/${companyId}/agents/${roleName}/\`(存放记忆、配置等私人文件)`);
  }

  // ── Layer 1: 通用基类 ──
  const commonFiles = ['methodology.md', 'tools-base.md', 'capabilities.md'];
  for (const f of commonFiles) {
    const fp = path.join(BASE_DIR, 'common', f);
    if (fs.existsSync(fp)) {
      const content = fs.readFileSync(fp, 'utf-8').trim();
      if (content) parts.push(content);
    }
  }

  // ── Layer 2: 类型基类 ──
  const typeBaseAgent = path.join(BASE_DIR, agentType, 'agent-base.md');
  if (fs.existsSync(typeBaseAgent)) {
    const content = fs.readFileSync(typeBaseAgent, 'utf-8').trim();
    if (content) parts.push(content);
  }

  // ── Layer 3: Agent 专属文件 ──
  const agentListText = Object.entries(fullSystemConfig.agents || {})
    .filter(([name]) => name !== 'master' && name !== 'innovator')
    .map(([name, cfg]) => {
      const typeLabel = cfg.type === 'orchestrator' ? '子编排' : cfg.type;
      return `- ${name} (${typeLabel}): ${cfg.description || ''}`;
    })
    .join('\n');

  const agentFiles = ['agent.md', 'soul.md', 'identity.md', 'user.md', 'tools.md'];
  for (const f of agentFiles) {
    let c = loadFile(agentDir, f);
    if (c.trim()) {
      if (c.includes('{{AVAILABLE_AGENTS}}')) {
        c = c.replace('{{AVAILABLE_AGENTS}}', agentListText);
      }
      parts.push(c.trim());
    }
  }

  // heartbeat.md
  const heartbeat = loadFile(agentDir, 'heartbeat.md');
  if (heartbeat.trim()) parts.push(heartbeat.trim());

  // ── Phase 3: 注入 OKR 目标上下文 ──
  const agentIdForMemory = config?.id || path.basename(agentDir);
  try {
    const goalContext = goalsService.getActiveContextForAgent(agentIdForMemory);
    if (goalContext) {
      parts.push(goalContext.trim());
    }
  } catch (err) {
    console.error(`[GoalContext] ⚠️ 读取目标上下文失败: ${err.message}`);
  }

  // memory — 核心记忆索引
  const memory = loadFile(agentDir, 'memory/memory.md');
  if (memory.trim()) parts.push('---\n\n# 我的记忆\n\n' + memory.trim());

  // short-term — 近期对话摘要 (Phase 1 MVP)
  const recentMemory = loadRecentMemory(agentIdForMemory);
  if (recentMemory) parts.push('---\n\n' + recentMemory);

  // hippocampus — 海马体检索相关经验 (Phase 2)
  // 将 userMessage 放在前,避免 todo.md 历史堆积导致当前任务的关键词被截断
  const todoContent = loadFile(agentDir, 'todo.md');
  const searchText = [userMessage || '', todoContent].join('\n\n');
  const hippocampusPlans = retrieveRelevantPlans(agentIdForMemory, searchText);
  if (hippocampusPlans) parts.push('---\n\n' + hippocampusPlans);

  // skills
  let mainSkill = loadFile(agentDir, '.claude/skills/main_skill/SKILL.md');
  if (mainSkill.trim()) {
    if (mainSkill.includes('{{AVAILABLE_AGENTS}}')) {
      mainSkill = mainSkill.replace('{{AVAILABLE_AGENTS}}', agentListText);
    }
    parts.push('---\n\n# 我的核心主技能 (执行准则)\n\n' + mainSkill.trim());
  }

  // ── Phase 1 / Micro-Verify: 当前局部任务的试错经验池 `_lessons.md` ──
  // 当 Agent 遇到棘手的坑或执行报错时,要求它总结并写入 _lessons.md。
  // 通过在此处注入,它下次 retry 时便能带着前车之鉴,避免死循环。
  const localLessons = loadFile(agentDir, 'workspace/_lessons.md');
  if (localLessons.trim()) {
    parts.push('---\n\n## ⚠️ 本地试错防坑经验 (_lessons.md)\n不要重复踩下面的坑,仔细阅读并绕开:\n\n' + localLessons.trim());
  }
  const skills = loadFile(agentDir, '.claude/skills/skills.md');
  if (skills.trim()) parts.push('---\n\n' + skills.trim());

  // ── 扫描所有 skill 子目录的 SKILL.md ──
  const skillsDir = path.join(agentDir, '.claude', 'skills');
  if (fs.existsSync(skillsDir)) {
    const entries = fs.readdirSync(skillsDir, { withFileTypes: true });
    for (const entry of entries) {
      if (!entry.isDirectory() || entry.name === 'main_skill') continue;
      const skillMdPath = path.join(skillsDir, entry.name, 'SKILL.md');
      if (fs.existsSync(skillMdPath)) {
        const skillContent = fs.readFileSync(skillMdPath, 'utf-8').trim();
        if (skillContent) {
          parts.push(`---\n\n## 依赖技能: ${entry.name}\n\n${skillContent}`);
        }
      }
    }
  }

  return parts.join('\n\n---\n\n');
}

/**
 * 构建 cron 恢复指令 — 合并基类 + Agent 专属 cron
 *
 * Layer 1: base/{type}/cron-base.json  (类型通用的定时任务)
 * Layer 2: data/agents/{id}/cron.json  (Agent 专属追加)
 *
 * 合并策略: 合并两个数组,去重 (相同 schedule+prompt 只保留一个)
 */
function buildCronInstructions(agentDir) {
  const config = loadAgentConfig(agentDir);
  const agentType = config?.type || 'skill';

  let allCrons = [];

  // Layer 1: 基类 cron
  const baseCronPath = path.join(BASE_DIR, agentType, 'cron-base.json');
  if (fs.existsSync(baseCronPath)) {
    try {
      const base = JSON.parse(fs.readFileSync(baseCronPath, 'utf-8'));
      allCrons = allCrons.concat(base);
    } catch (err) {
      console.error(`[Cron] ⚠️ 读取 ${baseCronPath} 失败:`, err.message);
    }
  }

  // Layer 2: Agent 专属 cron (追加)
  const agentCronPath = path.join(agentDir, 'cron.json');
  if (fs.existsSync(agentCronPath)) {
    try {
      const agent = JSON.parse(fs.readFileSync(agentCronPath, 'utf-8'));
      allCrons = allCrons.concat(agent);
    } catch (err) {
      console.error(`[Cron] ⚠️ 读取 ${agentCronPath} 失败:`, err.message);
    }
  }

  // 过滤已启用 + 按 schedule 去重(后加入的 Agent 专属 cron 覆盖基类)
  const enabled = allCrons.filter(c => c.enabled !== false);
  const scheduleMap = new Map();
  for (const c of enabled) {
    // 同一 schedule 下,后者(Agent专属)覆盖前者(基类)
    scheduleMap.set(c.schedule, c);
  }
  const unique = Array.from(scheduleMap.values());

  if (unique.length === 0) return '';

  const lines = unique.map((c, i) => {
    const recur = c.recurring !== false ? '循环' : '单次';
    return `${i + 1}. 调度: \`${c.schedule}\` (${recur})\n   Prompt: "${c.prompt}"`;
  }).join('\n');

  return `\n\n---\n\n## ⏰ 定时任务恢复\n\n**重要**: 请在处理用户消息之前,先用 CronCreate 工具创建以下定时任务(这些是你的持久化调度配置,每次新会话需重建):\n\n${lines}\n\n请依次调用 CronCreate 创建上述任务,然后再回答用户。`;
}

function loadAgentConfig(agentDir) {
  try { return JSON.parse(fs.readFileSync(path.join(agentDir, 'config.json'), 'utf-8')); }
  catch { return null; }
}

function loadIdentity(agentDir) {
  const content = loadFile(agentDir, 'identity.md');
  const r = { name: 'Agent', emoji: '🤖', greeting: '' };
  const nm = content.match(/名称\**[::]\s*(.+)/);   if (nm) r.name = nm[1].trim();
  // fallback: 如果 identity.md 没有名称,从 config.json 取 name
  if (r.name === 'Agent') {
    const cfg = loadAgentConfig(agentDir);
    if (cfg && cfg.name && cfg.name !== 'Agent') r.name = cfg.name;
  }
  const em = content.match(/Emoji\**[::]\s*(.+)/);   if (em) r.emoji = em[1].trim();
  const gm = content.match(/## 问候语\n"?([^"]*)"?/s);
  if (gm) r.greeting = gm[1].trim().replace(/^"|"$/g, '');
  return r;
}

function setupSSE(res) {
  res.writeHead(200, {
    'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache',
    'Connection': 'keep-alive', 'X-Accel-Buffering': 'no',
  });
  return (event, data) => res.write(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`);
}

// ═══════════════════════════════════════
//  对话存储 — conversationId/messageId 两层
// ═══════════════════════════════════════

function getConvDir(agentId, convId) {
  const dir = path.join(getAgentDir(agentId), 'conversations', convId);
  if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
  return dir;
}

/** 读写 meta.json */
function loadMeta(agentId, convId) {
  try {
    return JSON.parse(fs.readFileSync(path.join(getAgentDir(agentId), 'conversations', convId, 'meta.json'), 'utf-8'));
  } catch { return null; }
}

function saveMeta(agentId, convId, meta) {
  const dir = getConvDir(agentId, convId);
  meta.updatedAt = new Date().toISOString();
  fs.writeFileSync(path.join(dir, 'meta.json'), JSON.stringify(meta, null, 2));
}

/** 读写 history.json — 有序消息列表 */
function loadHistory(agentId, convId) {
  try {
    const historyPath = path.join(getAgentDir(agentId), 'conversations', convId, 'history.json');
    if (fs.existsSync(historyPath)) {
      return JSON.parse(fs.readFileSync(historyPath, 'utf-8'));
    }
    return [];
  } catch { return []; }
}

/**
 * 解析 messages.log 为消息数组
 * 格式: [ISO_TIMESTAMP] [USER|AGENT]\n content...\n\n
 */
function parseMessagesLog(logContent) {
  const messages = [];
  // 按消息头分割: [timestamp] [USER] 或 [timestamp] [AGENT]
  const pattern = /\[(\d{4}-\d{2}-\d{2}T[\d:.]+Z)\]\s+\[(USER|AGENT)\]\n/g;
  const matches = [];
  let match;
  while ((match = pattern.exec(logContent)) !== null) {
    matches.push({ index: match.index, timestamp: match[1], role: match[2], headerLen: match[0].length });
  }
  
  for (let i = 0; i < matches.length; i++) {
    const start = matches[i].index + matches[i].headerLen;
    const end = i + 1 < matches.length ? matches[i + 1].index : logContent.length;
    const content = logContent.substring(start, end).trim();
    messages.push({
      role: matches[i].role === 'USER' ? 'user' : 'assistant',
      content,
      timestamp: matches[i].timestamp,
    });
  }
  return messages;
}

function appendHistory(agentId, convId, msg) {
  const history = loadHistory(agentId, convId);
  history.push(msg);
  const dir = getConvDir(agentId, convId);
  fs.writeFileSync(path.join(dir, 'history.json'), JSON.stringify(history, null, 2));
}

/** 写日志(流式,带缓冲) */
const _logBuffers = new Map();
const LOG_BUFFER_SIZE = 4096; // 4KB
const LOG_FLUSH_INTERVAL = 1000; // 1s

function logMessageChunk(agentId, convId, content) {
  const key = `${agentId}::${convId}`;
  if (!_logBuffers.has(key)) {
    _logBuffers.set(key, { buf: '', timer: null, agentId, convId });
  }
  const entry = _logBuffers.get(key);
  entry.buf += content;
  if (entry.buf.length >= LOG_BUFFER_SIZE) {
    _flushLogBuffer(key);
  } else if (!entry.timer) {
    entry.timer = setTimeout(() => _flushLogBuffer(key), LOG_FLUSH_INTERVAL);
  }
}

function _flushLogBuffer(key) {
  const entry = _logBuffers.get(key);
  if (!entry || !entry.buf) return;
  try {
    const dir = getConvDir(entry.agentId, entry.convId);
    fs.appendFileSync(path.join(dir, 'messages.log'), entry.buf, 'utf-8');
  } catch (err) {
    console.error(`[LogBuffer] ⚠️ flush 失败: ${key}`, err.message);
  }
  entry.buf = '';
  if (entry.timer) { clearTimeout(entry.timer); entry.timer = null; }
}

/** 强制刷出指定对话的日志缓冲 */
function flushLogBufferForConv(agentId, convId) {
  const key = `${agentId}::${convId}`;
  _flushLogBuffer(key);
  _logBuffers.delete(key);
}

/** 传统单次写日志 (为了兼容) */
function logMessage(agentId, convId, role, content) {
  const ts = new Date().toISOString();
  const tag = role === 'user' ? 'USER' : 'AGENT';
  const line = `[${ts}] [${tag}]\n${content}\n\n`;
  logMessageChunk(agentId, convId, line);
}

function listConversations(agentId) {
  const agentConvDir = path.join(getAgentDir(agentId), 'conversations');
  if (!fs.existsSync(agentConvDir)) return [];
  try {
    return fs.readdirSync(agentConvDir, { withFileTypes: true })
      .filter(d => d.isDirectory())
      .map(d => {
        const m = loadMeta(agentId, d.name);
        if (m) m.id = m.id || m.conversationId || d.name;
        return m;
      })
      .filter(Boolean)
      .sort((a, b) => new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime());
  } catch { return []; }
}

// ═══════════════════════════════════════
//  ACP Session 池 — 支持多轮 + 上下文恢复
// ═══════════════════════════════════════

// key: `${agentId}:${conversationId}` → { protocol, messageCount }
const sessions = new Map();

async function getOrCreateProtocol(agentId, convId) {
  const key = `${agentId}:${convId}`;
  let entry = sessions.get(key);

  if (entry && entry.protocol.isConnected) {
    return entry;
  }

  // 关闭旧的
  if (entry) {
    try { entry.protocol.disconnect(); } catch {}
  }

  const agentDir = getAgentDir(agentId);
  const identity = loadIdentity(agentDir);
  const agentConfig = loadAgentConfig(agentDir);

  // 工具隔离策略:从 config.json 的 cli.toolPolicy 读取(由 AgentScaffold 父类生成)
  // 编排型 Agent 禁用所有原生工具,强制纯文本 [CALL:] 输出
  const cliOptions = {};
  const toolPolicy = agentConfig?.cli?.toolPolicy;
  if (toolPolicy?.mode === 'full') {
    // 执行型 Agent:完整工具权限(不设 allowedTools 限制)
    console.log(`[AgentChat] 🔓 ${identity.name} 工具策略: full (所有工具可用)`);
  } else if (toolPolicy && toolPolicy.allowedTools !== undefined) {
    // 使用 config.json 中声明的策略 (新架构)
    cliOptions.allowedTools = toolPolicy.allowedTools;
    if (toolPolicy.disallowedTools) cliOptions.disallowedTools = toolPolicy.disallowedTools;
    console.log(`[AgentChat] 🔒 ${identity.name} 工具策略 (from config): allowedTools=${JSON.stringify(cliOptions.allowedTools)}`);
  } else if (agentConfig && (agentConfig.type === 'orchestrator')) {
    // 兼容旧 config.json (没有 cli.toolPolicy 字段) — 仅 orchestrator 默认禁用
    cliOptions.allowedTools = '';
    console.log(`[AgentChat] 🔒 ${identity.name} 启用编排模式 (legacy fallback): 原生工具已全部禁用`);
  }

  // effort 级别控制 (low=禁用thinking | medium | high | max)
  // 优先从 config.json 的 cli.effort 读取,其次环境变量 CLAUDE_EFFORT
  if (agentConfig?.cli?.effort) {
    cliOptions.effort = agentConfig.cli.effort;
    console.log(`[AgentChat] 💡 ${identity.name} effort=${cliOptions.effort}`);
  }

  const protocol = new ClaudeCliProtocol({ name: identity.name }, cliOptions);
  await protocol.connect(null, agentDir);
  await protocol.initialize();
  await protocol.newSession({ cwd: agentDir });

  const newEntry = { protocol, messageCount: 0, agentId, convId };
  sessions.set(key, newEntry);
  console.log(`[AgentChat] 🧬 ${identity.name} ACP 会话已创建 (conv: ${convId})`);

  return newEntry;
}

/**
 * 构建 prompt — 根据是否首次消息 & 是否有历史上下文
 *
 * 场景 A: 全新对话的第一条消息 → system prompt + 用户消息
 * 场景 B: 同一进程中的后续消息 → 直接发消息(ACP 自带上下文)
 * 场景 C: 服务重启后恢复对话 → system prompt + 历史摘要 + 用户消息
 */
// ── 文件上传相关工具 ──
const TEXT_EXTENSIONS = new Set([
  '.md', '.txt', '.log', '.js', '.ts', '.jsx', '.tsx', '.py', '.java', '.go', '.rs',
  '.sql', '.sh', '.css', '.html', '.vue', '.svelte', '.json', '.yaml', '.yml',
  '.toml', '.xml', '.csv', '.conf', '.ini', '.env', '.dockerfile', '.makefile',
  '.rb', '.php', '.c', '.cpp', '.h', '.hpp', '.swift', '.kt', '.scala', '.r',
]);
const IMAGE_EXTENSIONS = new Set(['.png', '.jpg', '.jpeg', '.gif', '.webp', '.svg', '.bmp', '.ico']);
const MAX_INJECT_SIZE = 50 * 1024; // 50KB per file
const MAX_TOTAL_INJECT = 500 * 1024; // 500KB total

function isTextFile(ext) { return TEXT_EXTENSIONS.has(ext.toLowerCase()); }
function isImageFile(ext) { return IMAGE_EXTENSIONS.has(ext.toLowerCase()); }
function formatFileSize(bytes) {
  if (bytes < 1024) return bytes + 'B';
  if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + 'KB';
  return (bytes / (1024 * 1024)).toFixed(1) + 'MB';
}
function getLanguageTag(ext) {
  const map = { '.js': 'javascript', '.ts': 'typescript', '.py': 'python', '.java': 'java',
    '.go': 'go', '.rs': 'rust', '.sql': 'sql', '.sh': 'bash', '.css': 'css', '.html': 'html',
    '.vue': 'vue', '.json': 'json', '.yaml': 'yaml', '.yml': 'yaml', '.xml': 'xml',
    '.csv': 'csv', '.md': 'markdown', '.rb': 'ruby', '.php': 'php', '.c': 'c', '.cpp': 'cpp',
  };
  return map[ext.toLowerCase()] || '';
}
function sanitizeFilename(name) {
  return name.replace(/[\/\\:*?"<>|]/g, '_').replace(/\.\./g, '_').slice(0, 200);
}

function getUploadDir(agentDir, convId) {
  return path.join(agentDir, 'uploads', convId);
}

/**
 * 扫描 uploads/{convId}/ 目录,构建附件上下文块注入到用户消息前。
 */
function buildUploadContext(agentDir, convId) {
  const uploadDir = getUploadDir(agentDir, convId);
  if (!fs.existsSync(uploadDir)) return '';
  
  const files = fs.readdirSync(uploadDir).filter(f => !f.startsWith('.'));
  if (files.length === 0) return '';
  
  let contextBlock = '\n\n---\n📎 **附件上下文** (以下文件已上传到当前会话):\n\n';
  let totalInjected = 0;
  
  for (const file of files) {
    const ext = path.extname(file).toLowerCase();
    const filePath = path.join(uploadDir, file);
    let stat;
    try { stat = fs.statSync(filePath); } catch { continue; }
    
    if (isTextFile(ext) && stat.size <= MAX_INJECT_SIZE && totalInjected + stat.size <= MAX_TOTAL_INJECT) {
      try {
        const content = fs.readFileSync(filePath, 'utf-8');
        const lang = getLanguageTag(ext);
        contextBlock += `### 📄 ${file} (${formatFileSize(stat.size)})\n\`\`\`${lang}\n${content}\n\`\`\`\n\n`;
        totalInjected += stat.size;
      } catch { contextBlock += `### 📄 ${file} (${formatFileSize(stat.size)}) - 读取失败\n\n`; }
    } else if (isTextFile(ext) && stat.size > MAX_INJECT_SIZE) {
      // 大文本文件:只注入前 50KB
      try {
        const fd = fs.openSync(filePath, 'r');
        const buf = Buffer.alloc(MAX_INJECT_SIZE);
        fs.readSync(fd, buf, 0, MAX_INJECT_SIZE, 0);
        fs.closeSync(fd);
        const lang = getLanguageTag(ext);
        contextBlock += `### 📄 ${file} (${formatFileSize(stat.size)}, 仅显示前50KB)\n\`\`\`${lang}\n${buf.toString('utf-8')}\n...(文件过大已截断,完整路径: ${filePath})\n\`\`\`\n\n`;
        totalInjected += MAX_INJECT_SIZE;
      } catch { contextBlock += `### 📄 ${file} (${formatFileSize(stat.size)}) - 路径: ${filePath}\n\n`; }
    } else if (isImageFile(ext)) {
      contextBlock += `### 🖼️ ${file} (${formatFileSize(stat.size)})\n(图片文件,路径: ${filePath})\n\n`;
    } else {
      contextBlock += `### 📦 ${file} (${formatFileSize(stat.size)})\n(二进制文件,路径: ${filePath})\n\n`;
    }
    
    if (totalInjected >= MAX_TOTAL_INJECT) {
      contextBlock += '> ⚠️ 已达到注入上限(500KB),剩余文件仅列出路径。\n\n';
      break;
    }
  }
  
  contextBlock += '---\n';
  return contextBlock;
}

function buildPrompt(agentDir, agentId, convId, userMessage, isNewSession) {
  // 注入上传文件上下文
  const uploadContext = buildUploadContext(agentDir, convId);
  const enrichedMessage = uploadContext ? uploadContext + '\n' + userMessage : userMessage;

  if (!isNewSession) {
    // 场景 B: ACP 进程还在,多轮上下文自动保持
    return enrichedMessage;
  }

  const systemPrompt = buildSystemPrompt(agentDir, userMessage);
  const cronInstructions = buildCronInstructions(agentDir);
  const history = loadHistory(agentId, convId);

  if (history.length === 0) {
    // 场景 A: 全新对话
    return `${systemPrompt}${cronInstructions}\n\n---\n\n## 用户消息\n\n${enrichedMessage}`;
  }

  // 场景 C: 恢复上下文 — 注入历史摘要
  const recentHistory = history.slice(-20);
  const historyText = recentHistory.map(m => {
    const role = m.role === 'user' ? 'Human' : 'Assistant';
    const content = m.content.length > 500 ? m.content.slice(0, 500) + '...(截断)' : m.content;
    return `${role}: ${content}`;
  }).join('\n\n');

  return `${systemPrompt}${cronInstructions}\n\n---\n\n## 之前的对话历史(供你参考以保持上下文连贯)\n\n${historyText}\n\n---\n\n## 用户新消息\n\n${enrichedMessage}`;
}


// ═══════════════════════════════════════
//  路由
// ═══════════════════════════════════════

router.get('/quota', (req, res) => {
  try {
    const DATA_ROOT = path.resolve(__dirname, '../../data');
    const records = [];
    const daysBack = parseInt(String(req.query.days || '30')) || 30; // 默认读最近 30 天
    const cutoff = new Date(Date.now() - daysBack * 86400000).toISOString();

    // 读取 billing 日志(兼容旧 billing.log + 新 billing-YYYY-MM-DD.log)
    const readBilling = (dir) => {
      if (!fs.existsSync(dir)) return;
      const files = fs.readdirSync(dir).filter(f => f.startsWith('billing'));
      for (const file of files) {
        // 按日分片优化:跳过超出时间范围的文件
        const dateMatch = file.match(/billing-(\d{4}-\d{2}-\d{2})\.log/);
        if (dateMatch && dateMatch[1] < cutoff.substring(0, 10)) continue;

        const p = path.join(dir, file);
        try {
          fs.readFileSync(p, 'utf-8').split('\n').forEach(line => {
            line = line.trim();
            if (line.startsWith('{')) {
              try {
                const rec = JSON.parse(line);
                if (rec.timestamp >= cutoff) records.push(rec);
              } catch(e) {}
            } else if (line) {
              // 旧的纯文本兼容
              try {
                const parts = line.split('|').map(p => p.trim());
                const ts = parts[0];
                if (ts < cutoff) return;
                const ai = parts[1].replace('Agent:', '').trim();
                const ci = parts[2].replace('Conv:', '').trim();
                const cost = parseFloat(parts[3].replace('Cost: $', '').trim());
                const dur = parseInt(parts[4].replace('Duration:', '').replace('ms', '').trim());
                records.push({ timestamp: ts, agentId: ai, convId: ci, costUsd: cost || 0, durationMs: dur || 0, usage: { input: 0, output: 0 } });
              } catch(e) {}
            }
          });
        } catch(e) {}
      }
    };

    readBilling(path.resolve(__dirname, '../../system/agents/master/logs'));
    const compDir = path.join(DATA_ROOT, 'companies');
    if (fs.existsSync(compDir)) {
      const comps = fs.readdirSync(compDir, { withFileTypes: true });
      for (const c of comps) {
        if (c.isDirectory() && !c.name.startsWith('.')) {
          readBilling(path.join(compDir, c.name, 'logs'));
        }
      }
    }

    res.json(records);
  } catch (err) {
    res.status(500).json({ error: err.message });
  }
});

// ── Agent List 缓存 (5秒 TTL,避免每次请求全量扫描磁盘) ──
let _agentListCache = null;
let _agentListCacheTime = 0;
const AGENT_LIST_CACHE_TTL = 5000; // 5 秒

router.get('/list', (req, res) => {
  try {
    // 缓存命中:5秒内直接返回
    if (_agentListCache && Date.now() - _agentListCacheTime < AGENT_LIST_CACHE_TTL) {
      return res.json(_agentListCache);
    }

    const allAgents = []
    const DATA_ROOT = path.resolve(__dirname, '../../data')

    // 辅助函数:解析 Agent 目录
    const scanAgentDir = (baseDir, companyId) => {
      if (!fs.existsSync(baseDir)) return
      const entries = fs.readdirSync(baseDir, { withFileTypes: true })
      for (const e of entries) {
        if (!e.isDirectory() || e.name.startsWith('.')) continue
        const agentDir = path.join(baseDir, e.name)
        const config = loadAgentConfig(agentDir)
        if (!config) continue
        const identity = loadIdentity(agentDir)

        let lastActiveAt = null
        let lastMessage = ''
        
        const convDir = path.join(agentDir, 'conversations')
        if (fs.existsSync(convDir)) {
          try {
            const convs = fs.readdirSync(convDir, { withFileTypes: true }).filter(d => d.isDirectory())
            let latest = 0
            let latestConvName = null
            for (const c of convs) {
              const metaPath = path.join(convDir, c.name, 'meta.json')
              if (fs.existsSync(metaPath)) {
                try {
                  const meta = JSON.parse(fs.readFileSync(metaPath, 'utf-8'))
                  const t = new Date(meta.updatedAt || meta.createdAt).getTime()
                  if (t > latest) {
                    latest = t
                    latestConvName = c.name
                  }
                } catch {}
              }
            }
            if (latest > 0) {
              lastActiveAt = new Date(latest).toISOString()
              if (latestConvName) {
                const historyPath = path.join(convDir, latestConvName, 'history.json')
                if (fs.existsSync(historyPath)) {
                  try {
                    const history = JSON.parse(fs.readFileSync(historyPath, 'utf-8'))
                    if (history.length > 0) {
                      const msg = history[history.length - 1]
                      if (Array.isArray(msg.content)) {
                        lastMessage = msg.content.map(c => c.text || '').join(' ')
                      } else {
                        lastMessage = msg.content || ''
                      }
                      if (msg.timestamp) lastActiveAt = msg.timestamp
                    }
                  } catch {}
                }
              }
            }
          } catch {}
        }

        // 截断一行
        if (lastMessage) {
          lastMessage = lastMessage.replace(/[\r\n]+/g, ' ').trim()
          if (lastMessage.length > 50) lastMessage = lastMessage.substring(0, 50) + '...'
        }

        // System 级 Agent ID 保持原样,Company 级需要拼装 "companyId:agentName"
        let agentId = config.id || e.name;
        if (companyId !== 'system') {
           agentId = `${companyId}:${agentId.includes(':') ? agentId.split(':')[1] : agentId}`;
        }

        allAgents.push({
          id: agentId,
          name: identity.name,
          emoji: identity.emoji,
          type: config.type,
          status: config.status,
          greeting: identity.greeting,
          lastActiveAt,
          lastMessage,
          companyId // 新增用于前端分组过滤
        })
      }
    }

    // 1. 扫描 System Agents (system/agents/)
    scanAgentDir(path.resolve(__dirname, '../../system/agents'), 'system')

    // 2. 扫描 Company Agents (data/companies/{companyId}/agents/)
    const companiesDir = path.join(DATA_ROOT, 'companies')
    if (fs.existsSync(companiesDir)) {
      const companies = fs.readdirSync(companiesDir, { withFileTypes: true })
      for (const c of companies) {
        if (!c.isDirectory() || c.name.startsWith('.')) continue
        scanAgentDir(path.join(companiesDir, c.name, 'agents'), c.name)
      }
    }

    // 排序: master 永远第一,其他按活跃倒序
    allAgents.sort((a, b) => {
      if (a.type === 'master') return -1
      if (b.type === 'master') return 1
      const ta = a.lastActiveAt ? new Date(a.lastActiveAt).getTime() : 0
      const tb = b.lastActiveAt ? new Date(b.lastActiveAt).getTime() : 0
      return tb - ta
    })

    // 更新缓存
    _agentListCache = allAgents;
    _agentListCacheTime = Date.now();

    res.json(allAgents)
  } catch (err) { res.status(500).json({ error: err.message }) }
});

// ── Phase 2: 小脑方案固化 API ──
router.post('/:agentId/memory/cerebellum', (req, res) => {
  const { saveCerebellumPlan } = require('../services/memory-service');
  const { scenario, keywords, steps, conditions, notes, rawContent, confidence } = req.body;
  if (!scenario) return res.status(400).json({ error: 'scenario is required' });

  const result = saveCerebellumPlan(req.params.agentId, {
    scenario, keywords, steps, conditions, notes, rawContent,
    confidence: confidence || 0.7,
  });
  if (result) {
    res.json({ success: true, path: result });
  } else {
    res.status(500).json({ error: 'Failed to save cerebellum plan' });
  }
});

// ── Phase 3: 记忆管理 API ──
router.post('/:agentId/memory/consolidate', (req, res) => {
  const result = consolidateMemory(req.params.agentId);
  res.json(result);
});

router.post('/:agentId/memory/daily-report', (req, res) => {
  const reportPath = generateDailyReport(req.params.agentId);
  res.json({ success: !!reportPath, path: reportPath });
});

router.get('/:agentId/info', (req, res) => {
  const agentDir = getAgentDir(req.params.agentId);
  if (!fs.existsSync(agentDir)) return res.status(404).json({ error: 'Agent not found' });
  const config = loadAgentConfig(agentDir); 
  const identity = loadIdentity(agentDir);
  
  // Read cron settings
  let cron = [];
  try {
    const cronPath = path.join(agentDir, 'cron.json');
    if (fs.existsSync(cronPath)) {
      cron = JSON.parse(fs.readFileSync(cronPath, 'utf8'));
    }
  } catch (e) {
    console.error('Failed to parse cron.json:', e);
  }

  res.json({ 
    id: config?.id || req.params.agentId, 
    ...identity, 
    type: config?.type, 
    status: config?.status,
    heartbeat: config?.heartbeat || { enabled: false, intervalMinutes: 15 },
    cron: cron,
    bootstrapFiles: ['agent.md','soul.md','identity.md','user.md','tools.md','heartbeat.md', 'cron.json'].filter(f => loadFile(agentDir, f).trim().length > 0),
  });
});

router.put('/:agentId/heartbeat', (req, res) => {
  try {
    const agentDir = getAgentDir(req.params.agentId);
    if (!fs.existsSync(agentDir)) return res.status(404).json({ error: 'Agent not found' });
    
    const configPath = path.join(agentDir, 'config.json');
    let config = {};
    if (fs.existsSync(configPath)) {
      config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
    }
    
    config.heartbeat = {
      enabled: req.body.enabled || false,
      intervalMinutes: req.body.intervalMinutes || 15
    };
    
    fs.writeFileSync(configPath, JSON.stringify(config, null, 2), 'utf8');
    
    // Update heartbeat service if running
    const heartbeatService = require('../services/heartbeat-service');
    // Using namespacedId logic from UI (`system:master` is skipped, only `company:role` or pure system agent like `master`)
    const agentId = req.params.agentId;
    if (config.heartbeat.enabled) {
      heartbeatService._setupAgentHeartbeat(agentId);
    } else if (heartbeatService.timers.has(agentId)) {
      clearInterval(heartbeatService.timers.get(agentId));
      heartbeatService.timers.delete(agentId);
    }
    
    res.json({ success: true, heartbeat: config.heartbeat });
  } catch (err) {
    res.status(500).json({ error: err.message });
  }
});

router.put('/:agentId/cron', (req, res) => {
  try {
    const agentDir = getAgentDir(req.params.agentId);
    if (!fs.existsSync(agentDir)) return res.status(404).json({ error: 'Agent not found' });
    
    const cronPath = path.join(agentDir, 'cron.json');
    const cronList = Array.isArray(req.body.cron) ? req.body.cron : [];
    
    fs.writeFileSync(cronPath, JSON.stringify(cronList, null, 2), 'utf8');
    
    // Check if the heartbeat service picks up the cron changes automatically.
    // Generally cron jobs execute within the heartbeat mechanism.
    res.json({ success: true, cron: cronList });
  } catch (err) {
    res.status(500).json({ error: err.message });
  }
});

/** 列出对话 */
router.get('/:agentId/conversations', (req, res) => {
  res.json(listConversations(req.params.agentId));
});

/** 获取对话详情 (meta + history) */
router.get('/:agentId/conversations/:convId', (req, res) => {
  const { agentId, convId } = req.params;
  const meta = loadMeta(agentId, convId);
  if (!meta) return res.status(404).json({ error: 'Not found' });
  const messages = loadHistory(agentId, convId);
  // 同时返回 sub-agent logs 列表
  const subLogs = listSubAgentLogs(agentId, convId);
  res.json({ ...meta, messages, subLogs });
});

/** 获取子 Agent 日志列表 */
router.get('/:agentId/conversations/:convId/sub-logs', (req, res) => {
  res.json(listSubAgentLogs(req.params.agentId, req.params.convId));
});

/** 获取单个子 Agent 日志内容 */
router.get('/:agentId/conversations/:convId/sub-logs/:logFile', (req, res) => {
  const { agentId, convId, logFile } = req.params;
  const logPath = path.join(getConvDir(agentId, convId), logFile);
  if (!logFile.endsWith('.log') || !fs.existsSync(logPath)) {
    return res.status(404).json({ error: 'Log not found' });
  }
  const content = fs.readFileSync(logPath, 'utf-8');
  res.json({ file: logFile, content });
});

function listSubAgentLogs(agentId, convId) {
  const convDir = getConvDir(agentId, convId);
  if (!fs.existsSync(convDir)) return [];
  return fs.readdirSync(convDir)
    .filter(f => f.startsWith('sub-') && f.endsWith('.log'))
    .map(f => {
      const match = f.match(/^(sub-[a-f0-9]+)-(.+)\.log$/);
      const content = fs.readFileSync(path.join(convDir, f), 'utf-8');
      const status = content.includes('STATUS: SUCCESS') ? 'success' :
                     content.includes('STATUS: ERROR') ? 'error' : 'running';
      return {
        file: f,
        callId: match ? match[1] : f,
        agent: match ? match[2] : 'unknown',
        status,
        size: content.length,
        content, // 直接内嵌内容,省一次 API 调用
      };
    })
    .sort((a, b) => a.file.localeCompare(b.file));
}

/** 删除对话 */
router.delete('/:agentId/conversations/:convId', (req, res) => {
  const { agentId, convId } = req.params;
  try {
    const agentDir = getAgentDir(agentId);
    // 1. 删除对话目录
    const convDir = path.join(agentDir, 'conversations', convId);
    if (fs.existsSync(convDir)) fs.rmSync(convDir, { recursive: true, force: true });
    // 2. 删除关联的日志目录/文件
    const logsDir = path.join(agentDir, 'logs');
    if (fs.existsSync(logsDir)) {
      for (const f of fs.readdirSync(logsDir)) {
        if (f.includes(convId)) {
          const fp = path.join(logsDir, f);
          fs.rmSync(fp, { recursive: true, force: true });
        }
      }
    }
    // 3. 删除上传文件
    const uploadDir = getUploadDir(agentDir, convId);
    if (fs.existsSync(uploadDir)) fs.rmSync(uploadDir, { recursive: true, force: true });
    // 4. 断开会话
    const key = `${agentId}:${convId}`;
    const entry = sessions.get(key);
    if (entry && entry.protocol) entry.protocol.disconnect();
    sessions.delete(key);
    console.log(`[AgentChat] 🗑️ 已删除对话 ${agentId}/${convId} (含uploads)`);
    res.json({ success: true });
  } catch (err) { res.status(500).json({ error: err.message }); }
});

/** 停止当前正在生成的任务 */
router.post('/:agentId/chat/:convId/stop', (req, res) => {
  const { agentId, convId } = req.params;
  const key = `${agentId}:${convId}`;
  const entry = sessions.get(key);
  if (entry && entry.protocol) {
    entry.protocol.disconnect(); // 关闭底层 claude cli 进程
    sessions.delete(key); // 下次从冷启动重新加载
    return res.json({ success: true, message: 'Agent 已中断' });
  }
  res.json({ success: false, message: '未找到运行中的 Agent 进程' });
});

/** 读取 bootstrap file */
router.get('/:agentId/bootstrap/:filename', (req, res) => {
  const allowed = ['agent.md','soul.md','identity.md','user.md','tools.md','heartbeat.md'];
  if (!allowed.includes(req.params.filename)) return res.status(400).json({ error: 'Invalid' });
  res.json({ filename: req.params.filename, content: loadFile(getAgentDir(req.params.agentId), req.params.filename) });
});

/**
 * v5: Agent 文件树 + 文件预览 + todo/plan
 */

// GET /api/agent/:agentId/files — 递归列出 Agent 目录文件树
router.get('/:agentId/files', (req, res) => {
  const agentDir = getAgentDir(req.params.agentId);
  if (!fs.existsSync(agentDir)) return res.status(404).json({ error: 'Agent not found' });

  const scanDir = (dir, relativePath = '') => {
    const entries = fs.readdirSync(dir, { withFileTypes: true });
    const result = [];
    for (const entry of entries) {
      const relPath = relativePath ? `${relativePath}/${entry.name}` : entry.name;
      if (entry.name.startsWith('.') && entry.name !== '.claude') continue;
      if (entry.isDirectory()) {
        const children = scanDir(path.join(dir, entry.name), relPath);
        result.push({ name: entry.name, path: relPath, type: 'dir', children, count: children.length });
      } else {
        const stat = fs.statSync(path.join(dir, entry.name));
        result.push({ name: entry.name, path: relPath, type: 'file', size: stat.size });
      }
    }
    return result.sort((a, b) => {
      if (a.type !== b.type) return a.type === 'dir' ? -1 : 1;
      return a.name.localeCompare(b.name);
    });
  };

  try {
    const tree = scanDir(agentDir);
    const countFiles = (nodes) => {
      return nodes.reduce((sum, n) => sum + (n.type === 'file' ? 1 : countFiles(n.children || [])), 0);
    };
    res.json({ tree, totalFiles: countFiles(tree) });
  } catch (err) {
    res.status(500).json({ error: err.message });
  }
});

// GET /api/agent/:agentId/file/* — 读取 Agent 目录内的文件内容
router.get('/:agentId/file/*', (req, res) => {
  const filePath = req.params[0];
  if (!filePath) return res.status(400).json({ error: 'file path required' });

  const fullPath = path.join(getAgentDir(req.params.agentId), filePath);
  if (!fullPath.startsWith(getAgentDir(req.params.agentId))) {
    return res.status(403).json({ error: 'Access denied' });
  }
  if (!fs.existsSync(fullPath)) return res.status(404).json({ error: 'File not found' });

  try {
    const stat = fs.statSync(fullPath);
    if (stat.isDirectory()) return res.status(400).json({ error: 'Path is a directory' });
    const ext = path.extname(fullPath).toLowerCase();
    const isBinary = ['.png', '.jpg', '.jpeg', '.gif', '.ico', '.woff', '.woff2', '.ttf'].includes(ext);
    if (isBinary) return res.json({ path: filePath, size: stat.size, binary: true, content: '(binary file)' });
    const content = fs.readFileSync(fullPath, 'utf-8');
    res.json({ path: filePath, size: stat.size, content, ext });
  } catch (err) {
    res.status(500).json({ error: err.message });
  }
});

// PUT /api/agent/:agentId/file/* — 保存文件内容
router.put('/:agentId/file/*', (req, res) => {
  const filePath = req.params[0];
  if (!filePath) return res.status(400).json({ error: 'file path required' });

  const agentDir = getAgentDir(req.params.agentId);
  const fullPath = path.join(agentDir, filePath);
  if (!fullPath.startsWith(agentDir)) {
    return res.status(403).json({ error: 'Access denied' });
  }

  try {
    // 确保目录存在
    const dir = path.dirname(fullPath);
    if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
    fs.writeFileSync(fullPath, req.body.content || '', 'utf-8');
    console.log(`[AgentChat] 💾 已保存文件 ${req.params.agentId}/${filePath}`);
    res.json({ success: true, path: filePath });
  } catch (err) {
    res.status(500).json({ error: err.message });
  }
});

// DELETE /api/agent/:agentId/file/* — 删除文件或文件夹
router.delete('/:agentId/file/*', (req, res) => {
  const filePath = req.params[0];
  if (!filePath) return res.status(400).json({ error: 'file path required' });

  const agentDir = getAgentDir(req.params.agentId);
  const fullPath = path.join(agentDir, filePath);
  if (!fullPath.startsWith(agentDir)) {
    return res.status(403).json({ error: 'Access denied' });
  }
  if (!fs.existsSync(fullPath)) return res.status(404).json({ error: 'Not found' });

  try {
    fs.rmSync(fullPath, { recursive: true, force: true });
    console.log(`[AgentChat] 🗑️ 已删除 ${req.params.agentId}/${filePath}`);
    res.json({ success: true, path: filePath });
  } catch (err) {
    res.status(500).json({ error: err.message });
  }
});

// GET/PUT /api/agent/:agentId/todo
router.get('/:agentId/todo', (req, res) => {
  const fp = path.join(getAgentDir(req.params.agentId), 'todo.md');
  if (!fs.existsSync(fp)) return res.json({ content: '' });
  res.json({ content: fs.readFileSync(fp, 'utf-8') });
});
router.put('/:agentId/todo', (req, res) => {
  const agentDir = getAgentDir(req.params.agentId);
  if (!fs.existsSync(agentDir)) return res.status(404).json({ error: 'Agent not found' });
  fs.writeFileSync(path.join(agentDir, 'todo.md'), req.body.content || '', 'utf-8');
  res.json({ success: true });
});

// GET/PUT /api/agent/:agentId/plan
router.get('/:agentId/plan', (req, res) => {
  const fp = path.join(getAgentDir(req.params.agentId), 'plan.md');
  if (!fs.existsSync(fp)) return res.json({ content: '' });
  res.json({ content: fs.readFileSync(fp, 'utf-8') });
});
router.put('/:agentId/plan', (req, res) => {
  const agentDir = getAgentDir(req.params.agentId);
  if (!fs.existsSync(agentDir)) return res.status(404).json({ error: 'Agent not found' });
  fs.writeFileSync(path.join(agentDir, 'plan.md'), req.body.content || '', 'utf-8');
  res.json({ success: true });
});

// GET/PUT /api/agent/:agentId/cron — 定时任务声明 (cron.json)
router.get('/:agentId/cron', (req, res) => {
  const fp = path.join(getAgentDir(req.params.agentId), 'cron.json');
  if (!fs.existsSync(fp)) return res.json([]);
  try {
    res.json(JSON.parse(fs.readFileSync(fp, 'utf-8')));
  } catch { res.json([]); }
});
router.put('/:agentId/cron', (req, res) => {
  const agentDir = getAgentDir(req.params.agentId);
  if (!fs.existsSync(agentDir)) return res.status(404).json({ error: 'Agent not found' });
  const crons = req.body;
  if (!Array.isArray(crons)) return res.status(400).json({ error: 'Body must be an array of cron objects' });
  fs.writeFileSync(path.join(agentDir, 'cron.json'), JSON.stringify(crons, null, 2), 'utf-8');
  console.log(`[Cron] 📝 ${req.params.agentId} cron.json 已更新 (${crons.length} tasks)`);
  res.json({ success: true, count: crons.length });
});

/**
 * POST /api/agent/scaffold — 通过 AgentScaffold 父类创建新 Agent 实例
 * 
 * body: { id, type, name, parentId?, bootstrap?: { agentMd?, soulMd?, skillMd? } }
 * 
 * 这是 meta_creator 子 Agent 调用的核心接口,确保每个新 Agent 都
 * 继承了标准目录结构(.claude/skills/main_skill, memory, workspace 等)
 */
router.post('/scaffold', async (req, res) => {
  try {
    const { id, type = 'skill', name, parentId, bootstrap = {} } = req.body;
    if (!id || !name) return res.status(400).json({ error: 'id and name are required' });

    const { AgentScaffold } = await import('../../packages/agent-core/AgentScaffold.js');
    const scaffold = new AgentScaffold(path.resolve(__dirname, '../../system/agents'));
    const agentDir = await scaffold.create({ id, type, name, parentId, bootstrap });

    if (bootstrap.skillMd) {
      const skillPath = path.join(agentDir, '.claude', 'skills', 'main_skill', 'SKILL.md');
      fs.writeFileSync(skillPath, bootstrap.skillMd, 'utf-8');
    }

    console.log(`[Scaffold] ✅ 新 Agent "${name}" (${id}) 已通过父类脚手架创建: ${agentDir}`);
    res.json({ 
      success: true, 
      agentId: id, 
      agentDir, 
      message: `Agent ${name} 已创建 (v5: +todo/plan/hippocampus)`,
      structure: [
        '.claude/skills/main_skill/',
        'memory/', 'memory/cerebellum/', 'knowledge/', 'workspace/', 'conversations/', 'logs/',
        'agent.md', 'soul.md', 'identity.md', 'tools.md', 'todo.md', 'plan.md', 'cron.json', 'config.json'
      ]
    });
  } catch (err) {
    console.error('[Scaffold] Error:', err.message);
    res.status(500).json({ error: err.message });
  }
});

// ═══════════════════════════════════════
//  v5: Task Dispatch — 跨 Agent 任务分发
// ═══════════════════════════════════════

/**
 * POST /api/agent/task/dispatch — 向目标 Agent 分发任务
 *
 * body: {
 *   from: 'master-001',        // 发送者 Agent ID
 *   to: 'agent-creator',       // 目标 Agent ID
 *   task: '创建一个诗人 Agent', // 任务描述
 *   priority: 'P0',            // 优先级
 *   planId: 'P-001',           // 在发送者 plan.md 中的编号 (可选)
 *   autoExecute: true,         // 是否自动异步触发执行
 * }
 */
// ── Task 元数据持久化目录 ──
const TASK_META_DIR = path.resolve(__dirname, '../../system/agents/.tasks');
if (!fs.existsSync(TASK_META_DIR)) fs.mkdirSync(TASK_META_DIR, { recursive: true });

function saveTaskMeta(taskId, meta) {
  fs.writeFileSync(path.join(TASK_META_DIR, `${taskId}.json`), JSON.stringify(meta, null, 2), 'utf-8');
}
function loadTaskMeta(taskId) {
  const fp = path.join(TASK_META_DIR, `${taskId}.json`);
  if (!fs.existsSync(fp)) return null;
  try { return JSON.parse(fs.readFileSync(fp, 'utf-8')); } catch { return null; }
}

router.post('/task/dispatch', async (req, res) => {
  try {
    const { from, to, task, priority = 'P1', planId, goalId, nodeId, autoExecute = true, parentTaskId: explicitParent } = req.body;
    if (!from || !to || !task) return res.status(400).json({ error: 'from, to, task are required' });

    const targetDir = getAgentDir(to);
    const senderDir = getAgentDir(from);
    if (!fs.existsSync(targetDir)) return res.status(404).json({ error: `Target agent "${to}" not found` });
    if (!fs.existsSync(senderDir)) return res.status(404).json({ error: `Sender agent "${from}" not found` });

    const taskId = `T-${Date.now().toString(36)}`;
    const now = new Date().toISOString().slice(0, 16).replace('T', ' ');

    // 净化多行文本和竖线,防止破坏 Markdown 表格格式
    const safeTask = task.replace(/\n/g, '<br>').replace(/\|/g, '&#124;');

    // ── 自动推断 parentTaskId ──
    // 如果调用者没有显式传 parentTaskId,则尝试从 .tasks/ 目录下
    // 查找 from agent 正在执行的(status=dispatched 且 to=from)的 task
    let parentTaskId = explicitParent || null;
    let sourceConvId = null;
    if (!parentTaskId) {
      try {
        const taskFiles = fs.readdirSync(TASK_META_DIR).filter(f => f.endsWith('.json'));
        // 找到 "to === from" 且尚未完成的任务 → 这就是 from agent 正在执行的父任务
        let candidates = [];
        for (const tf of taskFiles) {
          try {
            const tm = JSON.parse(fs.readFileSync(path.join(TASK_META_DIR, tf), 'utf-8'));
            if (tm.to === from && tm.status === 'dispatched') {
              candidates.push(tm);
            }
          } catch(e) {}
        }
        // 如果有多个,取最近创建的
        if (candidates.length > 0) {
          candidates.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime());
          parentTaskId = candidates[0].taskId;
          sourceConvId = candidates[0].executionConvId || null;
          console.log(`[TaskDispatch] 🔗 Auto-detected parentTaskId: ${parentTaskId} (${from} is executing it)`);
        }
      } catch(e) { /* ignore */ }
    }

    // 1. 写入目标 Agent 的 todo.md
    const todoPath = path.join(targetDir, 'todo.md');
    let todoContent = fs.existsSync(todoPath) ? fs.readFileSync(todoPath, 'utf-8') : '';
    // 移除空占位行
    todoContent = todoContent.replace(/\| *\| *(暂无待办).*\n?/g, '');
    const newTodoRow = `| ${taskId} | ${safeTask} | ⬚ 未开始 | ${from} | ${priority} | ${now} | - |\n`;
    todoContent = todoContent.trimEnd() + '\n' + newTodoRow;
    fs.writeFileSync(todoPath, todoContent, 'utf-8');

    // 2. 更新发送者的 plan.md (如果有 planId)
    if (planId) {
      const planPath = path.join(senderDir, 'plan.md');
      let planContent = fs.existsSync(planPath) ? fs.readFileSync(planPath, 'utf-8') : '';
      planContent = planContent.replace(/\| *\| *(暂无计划).*\n?/g, '');
      const newPlanRow = `| ${planId} | ${safeTask} | ${to} | 🔄 已分发 | ${now} | taskId: ${taskId} |\n`;
      planContent = planContent.trimEnd() + '\n' + newPlanRow;
      fs.writeFileSync(planPath, planContent, 'utf-8');
    }

    // 3. 更新 Goal 节点状态 (如果有 goalId)
    if (goalId && nodeId) {
      try {
        goalsService.updateNode(goalId, nodeId, {
          status: 'in_progress', taskId, owner: to,
        });
        console.log(`[TaskDispatch] 🎯 Goal ${goalId} 节点 ${nodeId} → in_progress`);
      } catch (err) {
        console.warn(`[TaskDispatch] ⚠️ Goal 更新失败: ${err.message}`);
      }
    }

    console.log(`[TaskDispatch] 📨 ${from} → ${to}: "${task}" (${taskId}, ${priority}${goalId ? `, goal: ${goalId}` : ''}${parentTaskId ? `, parent: ${parentTaskId}` : ''})`);

    // 4. 持久化 Task 元数据 (用于 task/report 回调)
    const executionConvId = autoExecute ? `task-${taskId}` : null;
    saveTaskMeta(taskId, {
      taskId,
      from,
      to,
      task: safeTask,
      priority,
      goalId: goalId || null,
      nodeId: nodeId || null,
      parentTaskId: parentTaskId || null,
      sourceConvId: sourceConvId || null,
      executionConvId,
      createdAt: new Date().toISOString(),
      status: 'dispatched',
    });

    // 5. 异步自动触发执行 (发送 chat 消息给目标 Agent)
    if (autoExecute) {
      // 异步触发,不阻塞当前响应
      setImmediate(async () => {
        try {
          const goalInfo = goalId ? `\n目标: ${goalId} → ${nodeId}` : '';
          const taskMessage = `[TASK from ${from}]\n任务编号: ${taskId}\n优先级: ${priority}${goalInfo}\n\n${task}\n\n请立即执行此任务。执行完成后,如果你的 agent.md 规定了后续步骤,请在同一轮对话中继续执行,不要停下来等待上级催促!\n全部完成后使用以下命令汇报:\nnode bin/lemon.js task report --taskId ${taskId} --from ${to} --to ${from} --status done --summary "(你的完成摘要)"`;

          // 内部触发聊天 (通过 HTTP 自调用)
          const http = require('http');
          const postData = JSON.stringify({ message: taskMessage, conversationId: executionConvId });
          const options = {
            hostname: 'localhost', port: systemConfig.port,
            path: `/api/agent/${to}/chat`,
            method: 'POST',
            headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(postData) },
          };
          const chatReq = http.request(options, (chatRes) => {
            let body = '';
            chatRes.on('data', (chunk) => body += chunk);
            chatRes.on('end', () => {
              console.log(`[TaskDispatch] ✅ ${to} 异步执行已启动 (conv: ${executionConvId})`);
            });
          });
          chatReq.on('error', (err) => {
            console.error(`[TaskDispatch] ❌ 异步执行失败: ${err.message}`);
          });
          chatReq.write(postData);
          chatReq.end();
        } catch (err) {
          console.error(`[TaskDispatch] ❌ 异步触发错误: ${err.message}`);
        }
      });
    }

    res.json({
      success: true,
      taskId,
      from, to,
      task,
      priority,
      autoExecute,
      executionConvId,
      message: `任务已分发给 ${to}` + (autoExecute ? ',正在异步执行' : ''),
    });
  } catch (err) {
    console.error('[TaskDispatch] Error:', err.message);
    res.status(500).json({ error: err.message });
  }
});

/**
 * POST /api/agent/task/report — Agent 汇报任务完成
 *
 * body: {
 *   taskId: 'T-xxx',
 *   from: 'agent-creator',   // 执行者
 *   to: 'master-001',        // 上级(任务发送者)
 *   status: 'done' | 'failed' | 'blocked',
 *   summary: '已成功创建 poet Agent',
 *   outputs: ['poet/agent.md', 'poet/soul.md'],  // 产出文件 (可选)
 * }
 */
router.post('/task/report', (req, res) => {
  try {
    const { taskId, from, to, status = 'done', summary = '', outputs = [], planId, goalId, nodeId } = req.body;
    if (!taskId || !from || !to) return res.status(400).json({ error: 'taskId, from, to are required' });

    const now = new Date().toISOString().slice(0, 16).replace('T', ' ');
    const statusEmoji = status === 'done' ? '✅ 完成' : status === 'failed' ? '❌ 失败' : '⏸ 阻塞';
    const safeSummary = (summary || '').replace(/\n/g, '<br>').replace(/\|/g, '&#124;');

    // 1. 更新执行者的 todo.md (标记完成)
    const executorDir = getAgentDir(from);
    if (fs.existsSync(executorDir)) {
      const todoPath = path.join(executorDir, 'todo.md');
      if (fs.existsSync(todoPath)) {
        let content = fs.readFileSync(todoPath, 'utf-8');
        // 找到 taskId 所在行,更新状态和检查时间
        content = content.replace(
          new RegExp(`(\\| *${taskId} *\\|[^|]*\\|) *[^|]* *(\\|.*)$`, 'm'),
          `$1 ${statusEmoji} $2`.replace(/\| *- *\|$/, `| ${now} |`)
        );
        fs.writeFileSync(todoPath, content, 'utf-8');
      }
    }

    // 2. 更新上级的 plan.md (标记完成) — 使用行级匹配,兼容各种格式
    const senderDir = getAgentDir(to);
    if (fs.existsSync(senderDir)) {
      const planPath = path.join(senderDir, 'plan.md');
      if (fs.existsSync(planPath)) {
        let content = fs.readFileSync(planPath, 'utf-8');
        const lines = content.split('\n');
        let updated = false;

        for (let i = 0; i < lines.length; i++) {
          const line = lines[i];
          // 匹配包含 taskId 的行(无论格式如何)
          if (line.includes(taskId) || (planId && line.includes(planId))) {
            // 替换状态列:找到第4个 | 分隔的列(状态列),替换为新状态
            const cells = line.split('|').map(c => c.trim());
            if (cells.length >= 5) {
              // cells: ['', '#', '子任务', '责任人', '状态', '上次检查', '备注', '']
              cells[4] = ` ${statusEmoji} `;
              cells[5] = ` ${now} `;
              if (safeSummary && cells.length >= 7) {
                cells[6] = ` ${safeSummary} `;
              }
              lines[i] = cells.join(' | ').replace(/\*\*/g, ''); // 去掉粗体
              updated = true;
              console.log(`[TaskReport] 📝 更新 plan.md 第 ${i + 1} 行: ${statusEmoji}`);
            }
          }
        }

        if (updated) {
          fs.writeFileSync(planPath, lines.join('\n'), 'utf-8');
        } else {
          console.warn(`[TaskReport] ⚠️ 未在 ${to}/plan.md 中找到 taskId=${taskId} 的行`);
        }
      }
    }

    // 3. 更新 Goal 节点状态 (如果有 goalId)
    if (goalId && nodeId) {
      try {
        const goalStatus = status === 'done' ? 'done' : status === 'failed' ? 'failed' : 'blocked';
        goalsService.updateNode(goalId, nodeId, {
          status: goalStatus, summary, output: outputs.join(', '),
        });
        console.log(`[TaskReport] 🎯 Goal ${goalId} 节点 ${nodeId} → ${goalStatus}`);
      } catch (err) {
        console.warn(`[TaskReport] ⚠️ Goal 更新失败: ${err.message}`);
      }
    }

    console.log(`[TaskReport] 📋 ${from} → ${to}: ${taskId} = ${status} (${summary})`);

    // 4. Pipeline↔Task 桥接:自动推进 Pipeline 步骤
    let pipelineAdvanced = false;
    try {
      const DATA_ROOT = path.resolve(__dirname, '../../data');
      const { PipelineService } = require('../services/pipeline-service');

      // 扫描所有公司的 Pipeline Runs
      const companyDirs = fs.readdirSync(DATA_ROOT, { withFileTypes: true })
        .filter(d => d.isDirectory() && !['agents', 'skills', 'templates'].includes(d.name)
          && fs.existsSync(path.join(DATA_ROOT, d.name, 'company.json')));

      for (const compDir of companyDirs) {
        const pSvc = new PipelineService(compDir.name);
        // from 可能是 "companyId:agentId" 格式,Pipeline step.agent 可能只是 "agentId"
        const agentShortId = from.includes(':') ? from.split(':')[1] : from;
        const activeStep = pSvc.findActiveRunStep(agentShortId) || pSvc.findActiveRunStep(from);

        if (activeStep) {
          const advanceStatus = status === 'done' ? 'done' : 'failed';
          pSvc.advanceStep(activeStep.runId, activeStep.stepIndex, {
            output: { summary, outputs: outputs || [], taskId },
            status: advanceStatus,
          });
          pipelineAdvanced = true;
          console.log(`[TaskReport→Pipeline] 🔗 自动推进 ${compDir.name} Pipeline Run ${activeStep.runId} 步骤 ${activeStep.stepIndex} → ${advanceStatus}`);
          break;
        }
      }
    } catch (pipeErr) {
      console.warn(`[TaskReport→Pipeline] ⚠️ Pipeline 桥接失败: ${pipeErr.message}`);
    }

    // 5. 自动回调:通知上级 Agent 任务已完成,唤醒其继续执行后续步骤
    if (status === 'done' || status === 'failed') {
      setImmediate(async () => {
        try {
          // 读取 task 元数据获取上下文
          const taskMeta = loadTaskMeta(taskId);
          const callbackConvId = `callback-${taskId}`;

          const statusText = status === 'done' ? '✅ 已完成' : '❌ 失败';
          const callbackMessage = `[TASK_CALLBACK]\n任务编号: ${taskId}\n执行者: ${from}\n状态: ${statusText}\n摘要: ${summary || '(无摘要)'}\n产出文件: ${outputs.length > 0 ? outputs.join(', ') : '(无)'}\n${taskMeta ? `原始任务: ${taskMeta.task}` : ''}\n\n请检查上述任务的执行结果,然后根据你的 plan.md 继续执行下一步计划。如有后续任务需要分发,请立即执行 dispatch。`;

          const http = require('http');
          const postData = JSON.stringify({ message: callbackMessage, conversationId: callbackConvId });
          const options = {
            hostname: 'localhost', port: systemConfig.port,
            path: `/api/agent/${to}/chat`,
            method: 'POST',
            headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(postData) },
          };
          const chatReq = http.request(options, (chatRes) => {
            let body = '';
            chatRes.on('data', (chunk) => body += chunk);
            chatRes.on('end', () => {
              console.log(`[TaskCallback] ✅ 已回调 ${to}: ${taskId} ${statusText} (conv: ${callbackConvId})`);
            });
          });
          chatReq.on('error', (err) => {
            console.error(`[TaskCallback] ❌ 回调失败: ${err.message}`);
          });
          chatReq.write(postData);
          chatReq.end();

          // 更新 task 元数据
          if (taskMeta) {
            taskMeta.status = status;
            taskMeta.completedAt = new Date().toISOString();
            taskMeta.summary = summary;
            saveTaskMeta(taskId, taskMeta);
          }
        } catch (err) {
          console.error(`[TaskCallback] ❌ 回调触发错误: ${err.message}`);
        }
      });
    }

    res.json({ success: true, taskId, status, goalId, nodeId, message: `任务 ${taskId} 已汇报为 ${status}` });
  } catch (err) {
    console.error('[TaskReport] Error:', err.message);
    res.status(500).json({ error: err.message });
  }
});

// ═══════════════════════════════════════
//  POST /api/agent/:agentId/task/transition — 强制流转与记忆断舍离 (Context Flush)
// ═══════════════════════════════════════
router.post('/:agentId/task/transition', (req, res) => {
  try {
    const { phase, artifact } = req.body;
    const agentId = req.params.agentId;
    const agentDir = getAgentDir(agentId);
    if (!fs.existsSync(agentDir)) return res.status(404).json({ error: 'Agent not found' });

    console.log(`[TaskTransition] 🔄 ${agentId} 正在跨越阶段至: ${phase}`);

    // 1. 查找当前的活动会话 ID
    let activeConvId = null;
    let sessionKey = null;
    for (const [k, v] of sessions.entries()) {
      if (v.agentId === agentId) {
        activeConvId = v.convId;
        sessionKey = k;
        break;
      }
    }

    // 2. Phase 3 核心: 事前阻断阀 (Human-in-the-loop)
    // 拦截 P0 任务由 plan -> execute 的跳转,强制挂起并申请 Master 审批
    if (phase === 'execute' && activeConvId && activeConvId.startsWith('task-')) {
       const taskId = activeConvId.substring(5);
       const taskMeta = loadTaskMeta(taskId);
       if (taskMeta && taskMeta.priority === 'P0') {
           console.log(`[TaskTransition] 🛑 拦截 P0 任务进入 Execute,挂起等待人类审批 (taskId: ${taskId})`);

           // 更新 OKR 节点状态为 pending_approval
           if (taskMeta.goalId && taskMeta.nodeId) {
               try {
                   goalsService.updateNode(taskMeta.goalId, taskMeta.nodeId, { status: 'pending_approval' });
               } catch (err) {}
           }

           // 通知 Master 审批
           setImmediate(() => {
               const masterMsg = `[⚠️ 事前审批请求]\n你的下属 ${agentId} 已经完成了 P0 级任务 ${taskId} 的规划阶段。\n现在它正准备加载大威力执行权限 (Execute) 落地方案。\n\n请你阅读其产生的 ${artifact || '_plan.md'},并在确认无误后通过指令手动放行它。`;
               const http = require('http');
               const postData = JSON.stringify({ message: masterMsg, conversationId: `approval-${taskId}` });
               const options = {
                   hostname: 'localhost', port: systemConfig.port,
                   path: `/api/agent/master/chat`,
                   method: 'POST',
                   headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(postData) },
               };
               const chatReq = http.request(options, () => {});
               chatReq.on('error', () => {});
               chatReq.write(postData);
               chatReq.end();
           });

           // 掐断当前进程使其休眠,等待重新唤醒
           const v = sessions.get(sessionKey);
           if (v && v.protocol) v.protocol.disconnect();
           sessions.delete(sessionKey);

           return res.json({ success: true, message: '已拦截。该任务为 P0 级别,必须等待 Master 审批放行后才能进入执行环节!' });
       }
    }

    // 若未被阻断,更新 OKR 为进行中子状态 (如 researching, executing等)
    if (activeConvId && activeConvId.startsWith('task-')) {
       const taskId = activeConvId.substring(5);
       const taskMeta = loadTaskMeta(taskId);
       if (taskMeta && taskMeta.goalId && taskMeta.nodeId) {
           try {
               const subStatus = phase === 'research' ? 'researching' : phase === 'plan' ? 'planning' : phase === 'execute' ? 'executing' : phase === 'verify' ? 'verifying' : 'in_progress';
               goalsService.updateNode(taskMeta.goalId, taskMeta.nodeId, { status: subStatus });
           } catch (err) {}
       }
    }

    // 3. Hot Swap 动态换脑: 把 main_skill 替换为 meta-* 技能
    const sourceSkill = path.resolve(__dirname, `../../system/skills/meta-${phase}/SKILL.md`);
    const targetSkill = path.join(agentDir, '.claude', 'skills', 'main_skill', 'SKILL.md');
    if (fs.existsSync(sourceSkill)) {
      if (!fs.existsSync(path.dirname(targetSkill))) fs.mkdirSync(path.dirname(targetSkill), { recursive: true });
      fs.copyFileSync(sourceSkill, targetSkill);
      console.log(`[TaskTransition] 🧠 ${agentId} 动态技能树已切换为 meta-${phase}`);
    }

    // 4. Context Flush 记忆断舍离: 掐断当前运行中的 ACP 进程,清空历史消息
    if (sessionKey) {
      const v = sessions.get(sessionKey);
      if (v && v.protocol) v.protocol.disconnect();
      sessions.delete(sessionKey);
    }

    if (activeConvId) {
      const dir = getConvDir(agentId, activeConvId);
      // 清空长周期的幻觉来源
      fs.writeFileSync(path.join(dir, 'history.json'), '[]', 'utf-8');
      logMessageChunk(agentId, activeConvId, `\n\n[SYSTEM] 🛑 Context Flush 触发!记忆已断开,阶段切换至 ${phase}\n\n`);

      // 3. 构建起步上下文: 告诉 Agent 它的前序成就
      let artifactContent = '';
      if (artifact) {
         // artifact 可能带有 'workspace/' 前缀,尝试解析
         const possiblePaths = [
           path.join(agentDir, artifact),
           path.join(agentDir, path.basename(artifact)),
           path.join(agentDir, 'workspace', path.basename(artifact))
         ];
         for (const p of possiblePaths) {
           if (fs.existsSync(p)) {
             artifactContent = fs.readFileSync(p, 'utf-8');
             break;
           }
         }
      }

      const flushMsg = `【系统指令:强制状态切换】
你的生命周期已成功切入新阶段:**${phase.toUpperCase()}**。
你之前的对话记忆已被完全抹除防范幻觉,你的系统技能已获得 ${phase} 的授权限制。

${artifactContent ? `这是上一阶段你或你的前置节点留下的交付物(Artifact),你必须严格参照此文件行动,不再另行发散:\n\n\`\`\`\n${artifactContent}\n\`\`\`` : ''}

请开始你的新阶段工作!`;

      // 写入一条用户指令开启新的清爽对话
      appendHistory(agentId, activeConvId, {
        role: 'user', content: flushMsg, messageId: `msg-${Date.now()}-flush`, timestamp: new Date().toISOString()
      });

      // 4. 发送唤醒指令让他立刻基于新脑子行动
      setImmediate(() => {
        const http = require('http');
        const postData = JSON.stringify({ message: "请确认你已收到系统的新阶段挂载指令,并立即执行你当前阶段本分的任务!", conversationId: activeConvId });
        const options = {
          hostname: 'localhost', port: systemConfig.port,
          path: `/api/agent/${agentId}/chat`,
          method: 'POST',
          headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(postData) },
        };
        const chatReq = http.request(options, () => {});
        chatReq.on('error', () => {});
        chatReq.write(postData);
        chatReq.end();
      });
    }

    res.json({ success: true, message: `已成功进行 Context Flush 并切换至 ${phase} 阶段` });
  } catch (err) {
    console.error('[TaskTransition] Error:', err.message);
    res.status(500).json({ error: err.message });
  }
});

// ═══════════════════════════════════════
//  POST /api/agent/task/reject — COO 质量门控:驳回任务
// ═══════════════════════════════════════

/**
 * POST /api/agent/task/reject — 驳回 Pipeline 步骤,要求 Agent 重做
 *
 * body: {
 *   companyId: 'lemonv',
 *   runId: 'R-xxx',
 *   stepIndex: 2,
 *   reason: '文章质量不达标,缺少数据支撑',
 *   reviewedBy: 'coo',
 * }
 */
router.post('/task/reject', (req, res) => {
  try {
    const { companyId, runId, stepIndex, reason = '', reviewedBy = 'coo' } = req.body;
    if (!companyId || !runId || stepIndex === undefined) {
      return res.status(400).json({ error: 'companyId, runId, stepIndex are required' });
    }

    const { PipelineService } = require('../services/pipeline-service');
    const pSvc = new PipelineService(companyId);
    const run = pSvc.rejectStep(runId, Number(stepIndex), { reason, reviewedBy });

    const step = run.steps[Number(stepIndex)];
    const agentId = step.agent;

    // 自动触发 Agent 重新执行(如果有 agent)
    if (agentId) {
      setImmediate(() => {
        const rejectMessage = [
          `[PIPELINE TASK - REDO]`,
          `⚠️ 你的上次产出被 ${reviewedBy} 驳回。`,
          `驳回原因: ${reason}`,
          `这是第 ${step.rejections?.length || 1} 次重做。`,
          ``,
          `请认真阅读以上反馈,改进你的输出质量后重新提交。`,
          `完成后调用:`,
          `node bin/lemon.js task report --taskId redo-${runId}-${stepIndex} --from ${agentId} --to ${reviewedBy} --status done --summary "(改进后的完成摘要)"`,
        ].join('\n');

        const http = require('http');
        const postData = JSON.stringify({ message: rejectMessage, conversationId: `redo-${runId}-step-${stepIndex}` });
        const options = {
          hostname: 'localhost', port: systemConfig.port,
          path: `/api/agent/${agentId}/chat`,
          method: 'POST',
          headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(postData) },
        };
        const chatReq = http.request(options, (chatRes) => {
          let body = '';
          chatRes.on('data', (chunk) => body += chunk);
          chatRes.on('end', () => {
            console.log(`[TaskReject] 🔄 ${agentId} 重做任务已触发`);
          });
        });
        chatReq.on('error', (err) => {
          console.error(`[TaskReject] ❌ 重做触发失败: ${err.message}`);
        });
        chatReq.write(postData);
        chatReq.end();
      });
    }

    console.log(`[TaskReject] 🔙 ${companyId} Run ${runId} 步骤 ${stepIndex} 被 ${reviewedBy} 驳回: ${reason}`);
    res.json({
      success: true,
      runId,
      stepIndex,
      reason,
      reviewedBy,
      rejectionCount: step.rejections?.length || 1,
      message: `步骤 ${stepIndex} 已驳回,Agent ${agentId} 将重新执行`,
    });
  } catch (err) {
    console.error('[TaskReject] Error:', err.message);
    res.status(500).json({ error: err.message });
  }
});

// ═══════════════════════════════════════
//  POST /api/agent/human-request — Human-in-the-loop: API Key 申请等
// ═══════════════════════════════════════

router.post('/human-request', (req, res) => {
  try {
    const { from, type, key, reason } = req.body;
    if (!from || !type || !key) {
      return res.status(400).json({ error: 'from, type, key are required' });
    }

    console.log(`[HumanRequest] 🙋 ${from} 遇到需要人类介入的请求: ${type} - ${key} (${reason})`);

    // 1. 同步写入根目录下的 .env 以备人类填入
    const envPath = path.resolve(__dirname, '../../.env');
    let envContent = '';
    try { envContent = fs.readFileSync(envPath, 'utf-8'); } catch {}
    
    // 如果还没包含这个 key,追加进去
    if (!envContent.includes(key + '=')) {
      const appendStr = `\n# [${new Date().toISOString().substring(0, 10)}] Requested by ${from}: ${reason}\n${key}=\n`;
      fs.appendFileSync(envPath, appendStr, 'utf-8');
      console.log(`[HumanRequest] 🔑 已将 ${key} 追加到 .env 等待配置`);
    }

    // 2. 写入 master 的 To-Do List
    const masterWorkspace = path.resolve(__dirname, '../../system/agents/master/workspace');
    if (!fs.existsSync(masterWorkspace)) fs.mkdirSync(masterWorkspace, { recursive: true });
    
    const todoPath = path.join(masterWorkspace, 'human_todo.md');
    const todoStr = `- [ ] **${type.toUpperCase()}**: Agent \`${from}\` 需要 \`${key}\` (${reason}) — ` +
                    `_请在根目录 \`.env\` 中配置后打钩_\n`;
    fs.appendFileSync(todoPath, todoStr, 'utf-8');

    // 3. 通知 Master (非阻塞)
    setImmediate(() => {
      const masterMsg = [
        `[🙋 HUMAN INTERVENTION REQUIRED]`,
        `你的下属 ${from} 遇到了系统级的阻碍。`,
        `请求类型: ${type}`,
        `资源名: ${key}`,
        `原因: ${reason}`,
        ``,
        `我已经将其记录到了 workspace/human_todo.md 和根目录的 .env 文件中。`,
        `请敦促人类 Master 提供该 Key,或者你评估是否可以使用备用方案。`
      ].join('\n');

      const http = require('http');
      const postData = JSON.stringify({ message: masterMsg, conversationId: `human-req-${Date.now().toString(36)}` });
      const options = {
        hostname: 'localhost', port: systemConfig.port,
        path: `/api/agent/master/chat`,
        method: 'POST',
        headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(postData) },
      };
      const chatReq = http.request(options, (chatRes) => {
        chatRes.on('data', () => {});
      });
      chatReq.on('error', () => {});
      chatReq.write(postData);
      chatReq.end();
    });

    res.json({ success: true, message: `已将请求上报给 Master 并记录至 Human To-Do List` });
  } catch (err) {
    console.error('[HumanRequest] Error:', err.message);
    res.status(500).json({ error: err.message });
  }
});

// ═══════════════════════════════════════
//  文件上传 API
// ═══════════════════════════════════════

const uploadStorage = multer.diskStorage({
  destination: (req, file, cb) => {
    const agentDir = getAgentDir(req.params.agentId);
    const convId = req.body.conversationId || `conv-${Date.now().toString(36)}`;
    req._uploadConvId = convId;  // 传递给后续逻辑
    const uploadDir = getUploadDir(agentDir, convId);
    fs.mkdirSync(uploadDir, { recursive: true });
    cb(null, uploadDir);
  },
  filename: (req, file, cb) => {
    const safe = sanitizeFilename(file.originalname);
    cb(null, safe);
  },
});
const upload = multer({
  storage: uploadStorage,
  limits: { fileSize: 10 * 1024 * 1024, files: 20 }, // 10MB per file, 20 files max
});

// POST /api/agent/:agentId/upload — 上传文件
router.post('/:agentId/upload', upload.array('files', 20), (req, res) => {
  try {
    const agentId = req.params.agentId;
    const agentDir = getAgentDir(agentId);
    if (!fs.existsSync(agentDir)) return res.status(404).json({ error: 'Agent not found' });

    const convId = req._uploadConvId || req.body.conversationId;
    const uploaded = (req.files || []).map(f => {
      const ext = path.extname(f.originalname).toLowerCase();
      return {
        name: f.filename,
        originalName: f.originalname,
        size: f.size,
        type: isTextFile(ext) ? 'text' : isImageFile(ext) ? 'image' : 'binary',
        path: `uploads/${convId}/${f.filename}`,
      };
    });

    console.log(`[Upload] 📎 ${agentId}/${convId}: ${uploaded.length} file(s) uploaded`);
    res.json({ conversationId: convId, uploaded });
  } catch (err) {
    res.status(500).json({ error: err.message });
  }
});

// GET /api/agent/:agentId/uploads/:convId — 列出已上传文件
router.get('/:agentId/uploads/:convId', (req, res) => {
  try {
    const agentDir = getAgentDir(req.params.agentId);
    const uploadDir = getUploadDir(agentDir, req.params.convId);
    if (!fs.existsSync(uploadDir)) return res.json([]);

    const files = fs.readdirSync(uploadDir).filter(f => !f.startsWith('.')).map(f => {
      const fp = path.join(uploadDir, f);
      const stat = fs.statSync(fp);
      const ext = path.extname(f).toLowerCase();
      return {
        name: f,
        size: stat.size,
        type: isTextFile(ext) ? 'text' : isImageFile(ext) ? 'image' : 'binary',
        uploadedAt: stat.mtime.toISOString(),
      };
    });
    res.json(files);
  } catch (err) { res.status(500).json({ error: err.message }); }
});

// DELETE /api/agent/:agentId/uploads/:convId/:filename — 删除单个文件
router.delete('/:agentId/uploads/:convId/:filename', (req, res) => {
  try {
    const agentDir = getAgentDir(req.params.agentId);
    const filePath = path.join(getUploadDir(agentDir, req.params.convId), sanitizeFilename(req.params.filename));
    if (fs.existsSync(filePath)) fs.unlinkSync(filePath);
    res.json({ success: true });
  } catch (err) { res.status(500).json({ error: err.message }); }
});

// GET /api/agent/:agentId/uploads/:convId/:filename — 下载/预览文件
router.get('/:agentId/uploads/:convId/:filename', (req, res) => {
  try {
    const agentDir = getAgentDir(req.params.agentId);
    const filePath = path.join(getUploadDir(agentDir, req.params.convId), sanitizeFilename(req.params.filename));
    if (!fs.existsSync(filePath)) return res.status(404).json({ error: 'File not found' });
    res.sendFile(filePath);
  } catch (err) { res.status(500).json({ error: err.message }); }
});

// ═══════════════════════════════════════
//  POST /api/agent/:agentId/chat — SSE 流式对话
// ═══════════════════════════════════════

router.post('/:agentId/chat', async (req, res) => {
  const { message, conversationId: reqConvId } = req.body;
  if (!message) return res.status(400).json({ error: 'message is required' });

  const agentId = req.params.agentId;
  const agentDir = getAgentDir(agentId);
  if (!fs.existsSync(agentDir)) return res.status(404).json({ error: 'Agent not found' });

  const convId = reqConvId || `conv-${Date.now().toString(36)}`;
  const msgId = `msg-${Date.now()}-${randomUUID().substring(0, 8)}`;
  const sendSSE = setupSSE(res);
  const identity = loadIdentity(agentDir);

  // 确保 meta.json 存在
  let meta = loadMeta(agentId, convId);
  if (!meta) {
    meta = {
      conversationId: convId,
      agentId,
      title: req.body.isHeartbeat ? '⏱️ 自主心跳巡检' : message.slice(0, 50),
      type: req.body.isHeartbeat ? 'heartbeat' : 'chat',
      createdAt: new Date().toISOString(),
      updatedAt: new Date().toISOString(),
      status: 'running',
      stats: { messageCount: 0, toolCallCount: 0 },
    };
  }
  if (!meta.stats) meta.stats = { messageCount: 0, toolCallCount: 0 };
  meta.stats.messageCount = (meta.stats.messageCount || 0) + 1;
  meta.status = 'running';
  saveMeta(agentId, convId, meta);

  // 写用户消息到两层存储
  logMessage(agentId, convId, 'user', message);
  appendHistory(agentId, convId, { role: 'user', content: message, messageId: msgId, timestamp: new Date().toISOString() });

  sendSSE('session', { conversationId: convId, agentId, messageId: msgId });
  sendSSE('thinking', { message: `${identity.emoji} ${identity.name} 思考中...` });

  let fullOutput = '';
  // 记录 Agent 开始回复的标识头
  logMessageChunk(agentId, convId, `[${new Date().toISOString()}] [AGENT]\n`);

  try {
    // 获取或创建 ACP 会话(per conversation)
    const entry = await getOrCreateProtocol(agentId, convId);
    const isNewSession = entry.messageCount === 0;

    // 构建 prompt(自动处理首次/后续/恢复三种场景)
    let prompt = buildPrompt(agentDir, agentId, convId, message, isNewSession);
    entry.messageCount++;

    let loopCount = 0;
    while (loopCount < 10) {
      loopCount++;
      fullOutput = ''; // Reset fullOutput for this turn

      // 监听流式事件
      let currentToolInput = '';
      const onUpdate = (payload) => {
        const update = payload.update;
        if (update?.sessionUpdate === 'agent_message_chunk' && update.content?.text) {
          console.log(`[SSE-DEBUG] 📤 agent_output (${update.content.text.length} chars)`);
          fullOutput += update.content.text;
          logMessageChunk(agentId, convId, update.content.text);
          sendSSE('agent_output', { agent: agentId, message: update.content.text });
        } else if (update?.sessionUpdate === 'agent_thought_chunk' && update.content?.text) {
          logMessageChunk(agentId, convId, update.content.text);
          sendSSE('thinking', { message: update.content.text });
        }
      };

      const onAcpEvent = (event) => {
        // 透传所有 ACP 事件给前端(完整可观测性)
        sendSSE('acp_raw', { method: event.method, params: event.params, ts: event.timestamp });

        if (event.method === 'claude/tool_use_start') {
          currentToolInput = '';
          const toolName = event.params.name;
          const toolMsg = `\n🔧 [TOOL_CALL] ${toolName} (ID: ${event.params.tool_use_id || 'unknown'})\n`;
          fullOutput += toolMsg;
          logMessageChunk(agentId, convId, toolMsg);
          sendSSE('tool_call', { tool: toolName, toolId: event.params.tool_use_id, message: `🔧 调用工具: ${toolName}` });
          meta.stats.toolCallCount = (meta.stats.toolCallCount || 0) + 1;
        } else if (event.method === 'claude/stream/content_block_stop') {
          // 工具调用结束 — 输出积攒的输入参数
          if (currentToolInput) {
            const inputLog = `   输入: ${currentToolInput.slice(0, 500)}${currentToolInput.length > 500 ? '...' : ''}\n`;
            fullOutput += inputLog;
            logMessageChunk(agentId, convId, inputLog);
            sendSSE('tool_input', { input: currentToolInput });
            currentToolInput = '';
          }
        } else if (event.method === 'session/result') {
          if (event.params?.duration_ms) {
            sendSSE('session_cost', { cost: event.params.cost_usd, duration: event.params.duration_ms, usage: event.params.usage });
            // Phase 5: Economic Audit - 记录账单 (Token Profiling) JSON lines
            try {
              let billingDir;
              if (agentId && agentId.includes(':')) {
                const compId = agentId.split(':')[0];
                billingDir = path.resolve(__dirname, `../../data/companies/${compId}/logs`);
              } else {
                billingDir = path.resolve(__dirname, '../../system/agents/master/logs');
              }
              if (!fs.existsSync(billingDir)) fs.mkdirSync(billingDir, { recursive: true });
              const logEntry = {
                timestamp: new Date().toISOString(),
                agentId,
                convId,
                model: event.params.model || 'unknown',
                costUsd: event.params.cost_usd || 0,
                durationMs: event.params.duration_ms,
                usage: event.params.usage || { input: 0, output: 0 }
              };
              const billingDate = new Date().toISOString().substring(0, 10);
              fs.appendFileSync(path.join(billingDir, `billing-${billingDate}.log`), JSON.stringify(logEntry) + '\n', 'utf-8');
            } catch (err) {}
          }
          if (!fullOutput && event.params?.result) {
            fullOutput = event.params.result;
            sendSSE('agent_output', { agent: agentId, message: event.params.result });
          }
        } else if (event.method?.startsWith('claude/')) {
          // 捕获其他 claude 事件(tool_result 等)
          const innerEvent = event.params;
          // input_json_delta — 工具的 JSON 输入增量
          if (innerEvent?.delta?.type === 'input_json_delta' && innerEvent.delta.partial_json) {
            currentToolInput += innerEvent.delta.partial_json;
          }
          // tool_result — 工具执行结果
          if (innerEvent?.type === 'tool_result' || innerEvent?.content_block?.type === 'tool_result') {
            const result = innerEvent.content || innerEvent.content_block?.content || '';
            const resultStr = typeof result === 'string' ? result : JSON.stringify(result);
            const truncated = resultStr.slice(0, 1000) + (resultStr.length > 1000 ? `...(${resultStr.length} chars)` : '');
            const resultLog = `   结果: ${truncated}\n`;
            fullOutput += `\n` + resultLog;
            logMessageChunk(agentId, convId, resultLog);
            sendSSE('tool_result', { result: truncated });
          }
        }
      };

      entry.protocol.on('session_update', onUpdate);
      entry.protocol.on('acp_event', onAcpEvent);

      // 监听 stderr / error / disconnect — 将 Claude CLI 错误实时推送到前端
      const onStderr = (msg) => {
        console.warn(`[AgentChat] stderr from ${agentId}:`, msg.substring(0, 300));
        sendSSE('acp_raw', { method: 'claude/stderr', params: { message: msg }, ts: Date.now() });
        // 如果包含严重错误关键词,额外发一条 error 事件
        if (/error|rate.?limit|429|403|timeout|ECONNREFUSED|failed/i.test(msg)) {
          sendSSE('error', { message: `⚠️ Claude CLI: ${msg.substring(0, 500)}` });
        }
      };
      const onDisconnected = ({ code, signal }) => {
        if (code !== 0) {
          sendSSE('error', { message: `Claude CLI 异常退出 (code=${code}, signal=${signal})` });
        }
      };
      entry.protocol.on('stderr', onStderr);
      entry.protocol.on('disconnected', onDisconnected);

      // 发出状态以便前端展示不卡壳
      sendSSE('agent_output', { agent: agentId, message: `_[请求已发送,等待底层大模型响应...]_\\\n\\\n` });
      
      // 发送 prompt 并等待完成
      await entry.protocol.sendPrompt(prompt);

      // 清理监听
      entry.protocol.off('session_update', onUpdate);
      entry.protocol.off('acp_event', onAcpEvent);
      entry.protocol.off('stderr', onStderr);
      entry.protocol.off('disconnected', onDisconnected);

      // 持久化当前轮次的回复
      if (fullOutput) {
        logMessageChunk(agentId, convId, '\n\n'); 
        appendHistory(agentId, convId, { role: 'assistant', content: fullOutput, messageId: `msg-${Date.now()}-${loopCount}`, timestamp: new Date().toISOString() });
      }

      // 解析是否带有 [CALL: sub_agent] 标记
      const calls = Parser.parseCalls(fullOutput, { allowParallel: true, agents: fullSystemConfig.agents });
      if (calls.length === 0) {
        break; // 没有后续调用,当前回合结束
      }

      // 存在 Sub-Agent 嵌套调用
      logMessageChunk(agentId, convId, `\n\n[System] 执行子 Agent 编排调度(并行)...\n`);
      sendSSE('thinking', { message: `🔄 分析到系统编排指令,正在并行启动 ${calls.length} 个子代理...` });

      const resultsText = [];
      const MAX_CONCURRENCY = 2;
      
      // Checkpoint: 保存当前调度计划
      const checkpoint = {
        loopCount,
        calls: calls.map(c => ({ agent: c.agent, task: c.task })),
        completedCalls: [],
        timestamp: new Date().toISOString()
      };
      const checkpointPath = path.join(getConvDir(agentId, convId), 'checkpoint.json');
      fs.writeFileSync(checkpointPath, JSON.stringify(checkpoint, null, 2));

      // 分批并发执行 (最多同时跑 MAX_CONCURRENCY 个子进程)
      for (let i = 0; i < calls.length; i += MAX_CONCURRENCY) {
        const batch = calls.slice(i, i + MAX_CONCURRENCY);
        
        await Promise.all(batch.map(async (call) => {
          const callId = `sub-${randomUUID().substring(0, 6)}`;
          const subLogPath = path.join(getConvDir(agentId, convId), `${callId}-${call.agent}.log`);

          // 初始化子 Agent 独立日志文件
          const subLogHeader = `[${new Date().toISOString()}] [SUB_AGENT: ${call.agent}] [CALL_ID: ${callId}]\n` +
            `任务:\n${call.task}\n\n--- 执行输出 ---\n`;
          fs.writeFileSync(subLogPath, subLogHeader);

          sendSSE('sub_agent', { event: 'start', callId, agent: call.agent, message: `🚀 开始执行: ${call.agent}...` });
          logMessageChunk(agentId, convId, `\n--- 调度 [${callId}]: ${call.agent} ---\n任务: ${call.task}\n\n`);
          try {
            let res;

            // ═══ 硬编码拦截: meta_creator → 直接调用 AgentScaffold 父类 ═══
            if (call.agent === 'meta_creator') {
              res = await executeMetaCreator(call.task, agentId, convId, sendSSE, callId);
              // 写入子 Agent 日志
              fs.appendFileSync(subLogPath, res.output + '\n');
            } else {
              res = await agentManager.callAgent(call.agent, call.task, {
                 metadata: { parentConvId: convId, layer: 'sub' },
                 onData: (chunk) => {
                     sendSSE('sub_agent', { event: 'chunk', callId, agent: call.agent, chunk });
                     // 实时追加到子 Agent 独立日志
                     fs.appendFileSync(subLogPath, chunk);
                 }
              });
            }

            // 写入最终结果到子 Agent 日志
            fs.appendFileSync(subLogPath, `\n\n--- 执行结果 ---\n[${new Date().toISOString()}] STATUS: SUCCESS\n输出长度: ${(res.output || '').length} 字符\n`);

            resultsText.push(`[Sub Agent ${call.agent} (${callId}) 返回结果]\n${res.output}\n[/Sub Agent ${call.agent} (${callId}) 返回结果]`);
            logMessageChunk(agentId, convId, `✅ 子 Agent ${call.agent} [${callId}] 执行成功。\n`);
            sendSSE('sub_agent', { event: 'done', callId, agent: call.agent, status: 'success', message: `✅ [${call.agent}] 执行完毕.` });
            // Checkpoint: 标记为完毕
            checkpoint.completedCalls.push({ callId, agent: call.agent, status: 'success' });
          } catch (subErr) {
            // 写入错误到子 Agent 日志
            fs.appendFileSync(subLogPath, `\n\n--- 执行结果 ---\n[${new Date().toISOString()}] STATUS: ERROR\n错误: ${subErr.message}\n${subErr.stack || ''}\n`);

            resultsText.push(`[Sub Agent ${call.agent} (${callId}) 执行失败]\n${subErr.message}\n[/Sub Agent ${call.agent} (${callId}) 执行失败]`);
            logMessageChunk(agentId, convId, `❌ 子 Agent ${call.agent} [${callId}] 失败: ${subErr.message}\n`);
            sendSSE('sub_agent', { event: 'done', callId, agent: call.agent, status: 'error', message: `❌ [${call.agent}] 抛出异常: ${subErr.message}` });
            checkpoint.completedCalls.push({ callId, agent: call.agent, status: 'error', error: subErr.message });
          }
          // 每批次后更新 checkpoint
          fs.writeFileSync(checkpointPath, JSON.stringify(checkpoint, null, 2));
        }));
      }

      // 构造下一次反思与后续动作的 Prompt
      prompt = `
## Sub Agent 执行结果反馈

${resultsText.join('\n\n')}

---
请对本次执行结果进行 Reflection 反思,并在确定所有步骤完成时,记得在对话结尾加上 [RESULT: DONE]。
`;
      
      // 更新消息计数和记录 User 视角的 system 反馈消息
      entry.messageCount++;
      logMessageChunk(agentId, convId, `\n[${new Date().toISOString()}] [SYSTEM FEEDBACK]\n${prompt}\n\n`);
      appendHistory(agentId, convId, { role: 'user', content: prompt, messageId: `sys-${Date.now()}`, timestamp: new Date().toISOString() });
      
      // 循环继续,下一次将会把这个反馈 prompt 发给 Innovator
      logMessageChunk(agentId, convId, `[${new Date().toISOString()}] [AGENT]\n`);
    }

    meta.status = 'completed';
    flushLogBufferForConv(agentId, convId); // 刷出日志缓冲
    saveMeta(agentId, convId, meta);

    // ── Phase 1 MVP: 自动保存 Session 摘要到记忆 ──
    try {
      saveSessionSummary(agentId, convId, meta, fullOutput);
    } catch (memErr) {
      console.error('[Memory] Session 摘要保存失败:', memErr.message);
    }

    // ── Phase 3: 自动反思 (toolCallCount>=3 或 duration>=60s 时触发) ──
    try {
      autoReflect(agentId, convId, meta, fullOutput);
    } catch (refErr) {
      console.error('[Memory] 反思失败:', refErr.message);
    }

    sendSSE('result', { conversationId: convId, agentId, messageId: msgId, output: fullOutput });

  } catch (err) {
    console.error('[AgentChat] Error:', err.message);
    logMessageChunk(agentId, convId, `\n\n[ERROR]\n${err.message}\n\n`);
    flushLogBufferForConv(agentId, convId); // 刷出日志缓冲
    meta.status = 'failed';
    meta.error = err.message;
    saveMeta(agentId, convId, meta);

    // 清理断开的 session
    const key = `${agentId}:${convId}`;
    sessions.delete(key);

    sendSSE('error', { message: err.message });
    console.log(`[SSE-DEBUG] 🚨 sendSSE('error') called:`, err.message.substring(0, 200));
  } finally {
    res.end();
  }
});

module.exports = router;