File size: 78,124 Bytes
fcf8749 | 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 | /**
* Live Agent Supervision Dashboard
* Main Application Logic
*/
class AgentSupervisionDashboard {
constructor() {
// DOM Elements
this.canvasContainer = document.getElementById('canvasContainer');
this.nodesContainer = document.getElementById('nodesContainer');
this.connectionsLayer = document.getElementById('connectionsLayer');
this.minimap = document.getElementById('minimap');
this.minimapViewport = document.getElementById('minimapViewport');
// State
this.nodes = new Map();
this.connections = [];
this.zoom = 1;
this.pan = { x: 0, y: 0 };
this.isDragging = false;
this.dragNode = null;
this.dragOffset = { x: 0, y: 0 };
// SSE state
this.eventSource = null;
this.currentRunId = this.getRunIdFromUrl();
// Agent name to node ID mapping for SSE events
this.agentNameToNodeId = {
'ML_EFFORT': 'route_planner', // Maps to route planner visualization
'ROUTE_PLANNER': 'route_planner',
'FAIRNESS_MANAGER': 'fairness_manager',
'DRIVER_LIAISON': 'driver_liaison',
'EXPLAINABILITY': 'explainability',
'FINAL_RESOLUTION': 'route_planner',
};
// Layout config
this.layout = this.getDefaultLayout();
// Initialize
this.init();
}
/**
* Get run_id from URL query parameter
*/
getRunIdFromUrl() {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get('run_id');
}
/**
* Get default node positions
*/
getDefaultLayout() {
const containerWidth = window.innerWidth;
const containerHeight = window.innerHeight - 100; // Account for header/footer
const centerX = containerWidth / 2;
const topY = 80;
const bottomY = topY + 220;
// Calculate spacing for 6 bottom nodes
const nodeWidth = 180;
const spacing = 20;
const totalWidth = 6 * nodeWidth + 5 * spacing;
const startX = (containerWidth - totalWidth) / 2;
return {
central_orchestrator: { x: centerX - 130, y: topY },
route_database: { x: startX, y: bottomY },
route_planner: { x: startX + nodeWidth + spacing, y: bottomY },
fairness_manager: { x: startX + 2 * (nodeWidth + spacing), y: bottomY },
driver_liaison: { x: startX + 3 * (nodeWidth + spacing), y: bottomY },
explainability: { x: startX + 4 * (nodeWidth + spacing), y: bottomY },
learning: { x: startX + 5 * (nodeWidth + spacing), y: bottomY }
};
}
/**
* Initialize the dashboard
*/
async init() {
// Load saved layout or use default
this.loadLayout();
// Get initial workflow state (try real data first, fallback to mock)
// IMPORTANT: If we have a run_id from URL, fetch data for THAT specific run
let state;
try {
if (this.currentRunId) {
// Fetch workflow state for the specific run from URL
state = await API.getRealWorkflowState(null, this.currentRunId);
} else {
// No specific run - get latest for today
state = await API.getRealWorkflowState();
}
} catch (error) {
console.log('Using mock data for initial load:', error.message);
state = API.getMockWorkflowState();
}
// Store current state
this.currentWorkflowState = state;
// Render agents
this.renderAgents(state.agents);
// Draw connections
this.updateConnections(state.connections);
// Update stats
this.updateStats(state.stats);
// Setup event listeners
this.setupEventListeners();
// Setup minimap
this.updateMinimap();
// Start API polling with workflow updates
this.setupAPIPolling();
// Connect to SSE for real-time agent events
this.connectSSE();
// Show run_id banner or no-run-selected message
if (this.currentRunId) {
this.showRunBanner(this.currentRunId);
this.fetchRoutesForRun(this.currentRunId);
this.fetchRunSummary(this.currentRunId);
// Fetch and replay historical events (for when allocation already completed)
this.fetchHistoricalEvents(this.currentRunId);
} else {
this.showNoRunSelectedBanner();
}
// Show data source indicator
if (state.isRealData) {
this.showToast('Connected to live backend data!');
}
}
/**
* Show banner with current run ID
*/
showRunBanner(runId) {
const shortId = runId.substring(0, 8);
const banner = document.createElement('div');
banner.id = 'run-banner';
banner.className = 'run-banner';
banner.innerHTML = `
<div class="run-banner-content">
<span class="run-icon">🎯</span>
<span>Tracking Run: <strong>${shortId}...</strong></span>
<button class="run-banner-close" onclick="this.parentElement.parentElement.remove()">×</button>
</div>
`;
banner.style.cssText = `
position: fixed;
top: 70px;
left: 50%;
transform: translateX(-50%);
background: linear-gradient(135deg, #00d4aa 0%, #00b894 100%);
color: #0a0f1a;
padding: 8px 20px;
border-radius: 20px;
z-index: 1000;
font-size: 14px;
font-weight: 500;
box-shadow: 0 4px 20px rgba(0, 212, 170, 0.3);
`;
document.body.appendChild(banner);
}
/**
* Show banner indicating no run is selected
*/
showNoRunSelectedBanner() {
const banner = document.createElement('div');
banner.id = 'no-run-banner';
banner.innerHTML = `
<div class="no-run-content">
<span class="no-run-icon">⚠️</span>
<span>No allocation run selected. Open from <a href="http://localhost:8000/demo/allocate" target="_blank">API Demo</a> or enter a Run ID.</span>
</div>
`;
banner.style.cssText = `
position: fixed;
top: 70px;
left: 50%;
transform: translateX(-50%);
background: rgba(251, 191, 36, 0.2);
color: #fbbf24;
padding: 12px 24px;
border-radius: 8px;
border: 1px solid #fbbf24;
z-index: 1000;
font-size: 14px;
`;
const link = banner.querySelector('a');
if (link) {
link.style.color = '#00d4aa';
link.style.textDecoration = 'underline';
}
document.body.appendChild(banner);
}
/**
* Fetch routes for map from run-scoped endpoint
*/
async fetchRoutesForRun(runId) {
try {
const response = await fetch(`http://localhost:8000/api/v1/runs/${runId}/routes-on-map`);
if (!response.ok) {
console.error('Failed to fetch routes for run:', response.status);
return;
}
const data = await response.json();
console.log('Fetched routes for run:', data.routes.length);
// Store routes for map display
this.currentRoutes = data.routes;
// Update map if available
if (typeof this.updateMapWithRoutes === 'function') {
this.updateMapWithRoutes(data.routes);
}
} catch (error) {
console.error('Error fetching routes for run:', error);
}
}
/**
* Fetch run summary to update stats
*/
async fetchRunSummary(runId) {
try {
const response = await fetch(`http://localhost:8000/api/v1/runs/${runId}/summary`);
if (!response.ok) {
console.error('Failed to fetch run summary:', response.status);
return;
}
const data = await response.json();
console.log('Fetched run summary:', data);
// Update stats display with run metrics
this.updateStats({
total_drivers: data.num_drivers,
active_routes: data.num_routes,
delivered: 0, // Not tracked in summary
pending: data.num_packages,
gini_index: data.global_gini_index,
std_dev: data.global_std_dev,
});
} catch (error) {
console.error('Error fetching run summary:', error);
}
}
/**
* Fetch and replay historical events for a run
* This updates agent statuses when the dashboard is opened after allocation completes
*/
async fetchHistoricalEvents(runId) {
try {
const response = await fetch(`http://localhost:8000/api/v1/runs/${runId}/recent-events`);
if (!response.ok) {
console.log('No recent events endpoint or no events:', response.status);
return;
}
const data = await response.json();
const events = data.events || [];
console.log(`Replaying ${events.length} historical events for run`);
// Process each event to update agent statuses
// Sort by timestamp to replay in order
events.sort((a, b) => new Date(a.timestamp) - new Date(b.timestamp));
// Track the last state for each agent to show final status
const agentStates = new Map();
events.forEach(event => {
const key = event.agent_name;
agentStates.set(key, event);
});
// Update each agent to its final state
agentStates.forEach((event, agentName) => {
this.handleAgentEvent(event);
});
if (events.length > 0) {
this.showToast(`Loaded ${events.length} agent events for this run`);
}
} catch (error) {
console.log('Could not fetch historical events:', error.message);
}
}
/**
* Update map with routes from run-scoped API
* Uses driver_name directly from the API response
*/
updateMapWithRoutes(routes) {
if (!this.map || !this.mapLayer) {
console.log('Map not initialized, skipping route update');
return;
}
// Clear existing layers
this.mapLayer.clearLayers();
// Route colors palette
const routeColors = [
{ main: '#00d4aa', glow: '#00b894' },
{ main: '#6c5ce7', glow: '#a29bfe' },
{ main: '#fd79a8', glow: '#e84393' },
{ main: '#fdcb6e', glow: '#f39c12' },
{ main: '#74b9ff', glow: '#0984e3' },
{ main: '#ff7675', glow: '#d63031' },
{ main: '#55efc4', glow: '#00cec9' },
{ main: '#fab1a0', glow: '#e17055' },
];
// Add warehouse marker
const warehouseIcon = L.divIcon({
className: 'warehouse-marker',
html: `
<div style="
width:20px; height:20px;
background: linear-gradient(135deg, #00d4aa 0%, #00b894 100%);
border-radius:50%;
border:2px solid #fff;
box-shadow: 0 0 20px rgba(0,212,170,0.6), 0 4px 8px rgba(0,0,0,0.3);
display:flex; align-items:center; justify-content:center;
">
<span style="color:#fff; font-size:10px; font-weight:bold;">W</span>
</div>
`,
iconSize: [20, 20],
iconAnchor: [10, 10]
});
L.marker([12.9716, 77.5946], { icon: warehouseIcon })
.addTo(this.mapLayer)
.bindPopup(`
<div style="font-family: 'Inter', sans-serif; padding: 8px;">
<div style="font-size:14px; font-weight:600; color:#00d4aa; margin-bottom:4px;">
🏭 Central Warehouse
</div>
<div style="font-size:12px; color:#888;">Bangalore Hub</div>
</div>
`);
const renderedRoutes = [];
// Render each route
routes.forEach((route, i) => {
const colorScheme = routeColors[i % routeColors.length];
// Use driver_name from API response directly
const driverName = route.driver_name || `Driver ${i + 1}`;
if (route.stops && route.stops.length > 0) {
const latlngs = [[12.9716, 77.5946]]; // Start at warehouse
// Add stop markers
route.stops.forEach((stop, idx) => {
const latlng = [stop.lat, stop.lng];
latlngs.push(latlng);
const stopIcon = L.divIcon({
className: 'stop-marker-premium',
html: `
<div style="
width:24px; height:24px;
background: linear-gradient(135deg, ${colorScheme.main} 0%, ${colorScheme.glow} 100%);
border-radius:50%;
border:2px solid #fff;
box-shadow: 0 2px 8px rgba(0,0,0,0.3), 0 0 12px ${colorScheme.main}40;
display:flex; align-items:center; justify-content:center;
font-size:10px; font-weight:bold; color:#fff;
">${idx + 1}</div>
`,
iconSize: [24, 24],
iconAnchor: [12, 12]
});
L.marker(latlng, { icon: stopIcon })
.addTo(this.mapLayer)
.bindPopup(`
<div style="font-family: 'Inter', sans-serif; padding: 8px; min-width: 180px;">
<div style="display:flex; align-items:center; gap:8px; padding-bottom:8px; border-bottom:1px solid #eee; margin-bottom:8px;">
<div style="
width:28px; height:28px;
background: ${colorScheme.main};
border-radius:50%;
display:flex; align-items:center; justify-content:center;
color:#fff; font-weight:bold; font-size:12px;
">${idx + 1}</div>
<div>
<div style="font-weight:600; color:#333;">Stop ${idx + 1}</div>
<div style="font-size:11px; color:#666;">${driverName}</div>
</div>
</div>
<div style="font-size:12px; color:#555; margin-bottom:4px;">
📍 ${stop.address || 'Delivery Location'}
</div>
</div>
`);
});
// Draw route line with glow
L.polyline(latlngs, {
color: colorScheme.glow,
weight: 8,
opacity: 0.3,
lineCap: 'round',
lineJoin: 'round'
}).addTo(this.mapLayer);
// Main line
L.polyline(latlngs, {
color: colorScheme.main,
weight: 4,
opacity: 0.9,
lineCap: 'round',
lineJoin: 'round'
}).addTo(this.mapLayer);
renderedRoutes.push({
color: colorScheme.main,
name: driverName,
stops: route.stops.length
});
}
});
// Add legend with correct driver names
this.addMapLegend(renderedRoutes);
// Fit bounds
if (this.mapLayer.getLayers().length > 0) {
try {
const group = L.featureGroup(this.mapLayer.getLayers());
this.map.fitBounds(group.getBounds().pad(0.1));
} catch (e) {
// Ignore bounds error
}
}
console.log('Map updated with', renderedRoutes.length, 'routes');
}
/**
* Connect to Server-Sent Events for real-time agent status updates
*/
connectSSE() {
// Close existing connection if any
if (this.eventSource) {
this.eventSource.close();
}
// Only connect if we have a run_id - otherwise no events to subscribe to
if (!this.currentRunId) {
console.log('No run_id present, skipping SSE connection');
return;
}
// Use run-scoped SSE endpoint
const sseUrl = `http://localhost:8000/api/v1/runs/${encodeURIComponent(this.currentRunId)}/agent-events`;
try {
this.eventSource = new EventSource(sseUrl);
this.eventSource.onopen = () => {
console.log('SSE connection established for run:', this.currentRunId);
};
this.eventSource.onmessage = (event) => {
try {
const eventData = JSON.parse(event.data);
this.handleAgentEvent(eventData);
} catch (err) {
console.warn('Failed to parse SSE event:', err);
}
};
this.eventSource.onerror = (error) => {
console.warn('SSE connection error, will retry...', error);
};
} catch (error) {
console.error('Failed to create SSE connection:', error);
}
}
/**
* Handle incoming agent event from SSE stream
*/
handleAgentEvent(event) {
// Skip connection events
if (event.type === 'connected') {
console.log('SSE connected:', event.message);
return;
}
const { agent_name, state: eventState, step_type, payload } = event;
// Map agent name to node ID
const nodeId = this.agentNameToNodeId[agent_name];
if (!nodeId) {
console.log('Unknown agent:', agent_name);
return;
}
// Determine status based on event state
let status = 'idle';
if (eventState === 'STARTED') {
status = 'processing';
} else if (eventState === 'COMPLETED') {
status = 'active'; // Use 'active' for completed to show success
} else if (eventState === 'ERROR') {
status = 'error';
}
// Update the agent status
this.updateAgentStatusFromEvent(nodeId, status, step_type, payload);
// Log for debugging
console.log(`Agent event: ${agent_name} - ${step_type} - ${eventState}`);
}
/**
* Update a single agent's status from SSE event
*/
updateAgentStatusFromEvent(nodeId, status, stepType, payload) {
const node = this.nodes.get(nodeId);
if (!node) return;
// Update status badge
const statusBadge = node.element.querySelector('.agent-status');
if (statusBadge) {
// Remove all status classes
statusBadge.classList.remove('active', 'processing', 'idle', 'error');
statusBadge.classList.add(status);
// Update status text
const statusIcons = {
active: '✓',
processing: '⟳',
idle: '○',
error: '✗'
};
const statusText = status.charAt(0).toUpperCase() + status.slice(1);
statusBadge.innerHTML = `<span class="dot"></span> ${statusIcons[status] || '○'} ${statusText}`;
}
// Add visual pulse effect
node.element.classList.remove('node-active-pulse', 'node-processing-pulse');
if (status === 'active') {
node.element.classList.add('node-active-pulse');
} else if (status === 'processing') {
node.element.classList.add('node-processing-pulse');
}
// Auto-clear processing status after a timeout (for visual feedback)
if (status === 'active') {
setTimeout(() => {
this.updateAgentStatusFromEvent(nodeId, 'idle', '', {});
}, 5000); // Reset to idle after 5 seconds
}
}
/**
* Render agent nodes
*/
renderAgents(agents) {
this.nodesContainer.innerHTML = '';
agents.forEach(agent => {
const node = this.createAgentNode(agent);
this.nodesContainer.appendChild(node);
this.nodes.set(agent.id, {
element: node,
data: agent,
position: this.layout[agent.id] || { x: 100, y: 100 }
});
// Position the node
this.updateNodePosition(agent.id);
});
}
/**
* Create an agent node element
*/
createAgentNode(agent) {
const node = document.createElement('div');
node.className = `agent-node ${agent.type}`;
node.dataset.id = agent.id;
// Status class
if (agent.status) {
node.classList.add(agent.status);
}
// Icon based on type
const iconSVG = this.getAgentIcon(agent.type);
// Processing badge for processing status
const processingBadge = agent.status === 'processing'
? '<span class="processing-badge">Processing</span>'
: '';
node.innerHTML = `
<div class="agent-icon">
${iconSVG}
</div>
<div class="agent-name">${agent.name}</div>
<div class="agent-description">${agent.description}</div>
<div class="agent-footer">
<div class="agent-status ${agent.status}">
<span class="dot"></span>
${this.formatStatus(agent.status)}
</div>
${agent.meta ? `<div class="agent-meta">${agent.meta}</div>` : ''}
${processingBadge}
</div>
`;
return node;
}
/**
* Get icon SVG for agent type
*/
getAgentIcon(type) {
const icons = {
orchestrator: `<svg viewBox="0 0 24 24" fill="currentColor">
<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-2 15l-5-5 1.41-1.41L10 14.17l7.59-7.59L19 8l-9 9z"/>
</svg>`,
database: `<svg viewBox="0 0 24 24" fill="currentColor">
<ellipse cx="12" cy="5" rx="8" ry="3"/>
<path d="M4 5v6c0 1.66 3.58 3 8 3s8-1.34 8-3V5"/>
<path d="M4 11v6c0 1.66 3.58 3 8 3s8-1.34 8-3v-6"/>
</svg>`,
agent: `<svg viewBox="0 0 24 24" fill="currentColor">
<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 3c1.66 0 3 1.34 3 3s-1.34 3-3 3-3-1.34-3-3 1.34-3 3-3zm0 14.2c-2.5 0-4.71-1.28-6-3.22.03-1.99 4-3.08 6-3.08 1.99 0 5.97 1.09 6 3.08-1.29 1.94-3.5 3.22-6 3.22z"/>
</svg>`
};
return icons[type] || icons.agent;
}
/**
* Format status text
*/
formatStatus(status) {
const statusMap = {
active: 'Active',
processing: 'Processing',
idle: 'Idle',
error: 'Error'
};
return statusMap[status] || status;
}
/**
* Update node position
*/
updateNodePosition(nodeId) {
const node = this.nodes.get(nodeId);
if (!node) return;
const pos = node.position;
node.element.style.left = `${pos.x}px`;
node.element.style.top = `${pos.y}px`;
}
/**
* Update agent nodes with new status data (called during polling)
*/
updateAgentStatuses(agents) {
if (!agents || !Array.isArray(agents)) return;
agents.forEach(agent => {
const node = this.nodes.get(agent.id);
if (!node) return;
// Update status badge
const statusBadge = node.element.querySelector('.agent-status');
if (statusBadge) {
// Remove all status classes
statusBadge.classList.remove('active', 'processing', 'idle', 'error');
// Add current status class
const status = agent.status || 'idle';
statusBadge.classList.add(status);
// Update status text with icon
const statusText = status.charAt(0).toUpperCase() + status.slice(1);
const statusIcons = {
active: '✓',
processing: '⟳',
idle: '○',
error: '✗'
};
statusBadge.innerHTML = `<span class="dot"></span> ${statusIcons[status] || '○'} ${statusText}`;
}
// Update description if provided
const descEl = node.element.querySelector('.node-description');
if (descEl && agent.description) {
descEl.textContent = agent.description;
}
// Update meta badge if provided
const metaBadge = node.element.querySelector('.meta-badge');
if (metaBadge && agent.meta) {
metaBadge.textContent = agent.meta;
metaBadge.style.display = 'inline-block';
}
// Add visual pulse effect for active agents
node.element.classList.remove('node-active-pulse', 'node-processing-pulse');
if (agent.status === 'active') {
node.element.classList.add('node-active-pulse');
} else if (agent.status === 'processing') {
node.element.classList.add('node-processing-pulse');
}
});
}
/**
* Draw connections between nodes
*/
updateConnections(connections) {
this.connections = connections;
this.drawConnections();
}
/**
* Draw all connection lines
*/
drawConnections() {
// Clear existing connections (keep defs)
const defs = this.connectionsLayer.querySelector('defs');
this.connectionsLayer.innerHTML = '';
this.connectionsLayer.appendChild(defs);
this.connections.forEach(conn => {
const fromNode = this.nodes.get(conn.from);
const toNode = this.nodes.get(conn.to);
if (!fromNode || !toNode) return;
// Get node centers
const fromRect = fromNode.element.getBoundingClientRect();
const toRect = toNode.element.getBoundingClientRect();
const containerRect = this.canvasContainer.getBoundingClientRect();
// Calculate positions relative to container
const fromX = fromRect.left - containerRect.left + fromRect.width / 2;
const fromY = fromRect.top - containerRect.top + fromRect.height;
const toX = toRect.left - containerRect.left + toRect.width / 2;
const toY = toRect.top - containerRect.top;
// Create curved path
const path = this.createConnectionPath(fromX, fromY, toX, toY, conn.active);
this.connectionsLayer.appendChild(path);
// Add click handler for data payload
path.addEventListener('click', (e) => {
e.stopPropagation();
this.showDataPayload(conn);
});
});
}
/**
* Create a curved SVG path for connection
*/
createConnectionPath(x1, y1, x2, y2, isActive) {
const path = document.createElementNS('http://www.w3.org/2000/svg', 'path');
// Calculate control points for bezier curve
const midY = (y1 + y2) / 2;
const controlOffset = Math.abs(y2 - y1) * 0.5;
const d = `M ${x1} ${y1}
C ${x1} ${y1 + controlOffset},
${x2} ${y2 - controlOffset},
${x2} ${y2}`;
path.setAttribute('d', d);
path.setAttribute('class', isActive ? 'connection-line-animated' : 'connection-line');
path.style.pointerEvents = 'stroke';
path.style.cursor = 'pointer';
return path;
}
/**
* Setup event listeners
*/
setupEventListeners() {
// Node click handler
this.nodesContainer.addEventListener('click', (e) => {
const node = e.target.closest('.agent-node');
if (node) {
this.openTerminal(node.dataset.id);
}
});
// Node drag handlers
this.nodesContainer.addEventListener('mousedown', (e) => {
const node = e.target.closest('.agent-node');
if (node) {
this.startDrag(node, e);
}
});
document.addEventListener('mousemove', (e) => {
if (this.isDragging) {
this.handleDrag(e);
}
});
document.addEventListener('mouseup', () => {
if (this.isDragging) {
this.endDrag();
}
});
// Button handlers
document.getElementById('saveLayoutBtn').addEventListener('click', () => {
this.saveLayout();
});
document.getElementById('resetBtn').addEventListener('click', () => {
this.resetLayout();
});
// Start Allocation handler
document.getElementById('startAllocationBtn').addEventListener('click', () => {
this.handleStartAllocation();
});
// Map View Toggles
document.getElementById('graphViewBtn').addEventListener('click', () => {
this.toggleView('graph');
});
document.getElementById('mapViewBtn').addEventListener('click', () => {
this.toggleView('map');
});
// Date Picker
const dateInput = document.getElementById('historyDate');
// Set today as default - use LOCAL timezone, not UTC
const now = new Date();
const today = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')}`;
console.log('Initializing date picker with today (local):', today);
dateInput.value = today;
API.setSelectedDate(today); // Sync with API
// Store current date for reference
this._currentDisplayDate = today;
dateInput.addEventListener('change', (e) => {
const newDate = e.target.value;
console.log('Date picker changed to:', newDate);
this._currentDisplayDate = newDate;
API.setSelectedDate(newDate); // Sync with API
this.loadHistory(newDate);
});
// Zoom handlers
document.getElementById('zoomIn').addEventListener('click', () => {
this.setZoom(this.zoom + 0.1);
});
document.getElementById('zoomOut').addEventListener('click', () => {
this.setZoom(this.zoom - 0.1);
});
// Modal close handlers
document.getElementById('closeTerminal').addEventListener('click', () => {
this.closeModal('terminalModal');
});
document.getElementById('closeData').addEventListener('click', () => {
this.closeModal('dataModal');
});
// Close modal on backdrop click
document.querySelectorAll('.modal').forEach(modal => {
modal.addEventListener('click', (e) => {
if (e.target === modal) {
modal.classList.remove('active');
}
});
});
// Window resize
window.addEventListener('resize', () => {
this.drawConnections();
this.updateMinimap();
});
}
/**
* Start dragging a node
*/
startDrag(nodeElement, e) {
const nodeId = nodeElement.dataset.id;
const node = this.nodes.get(nodeId);
if (!node) return;
this.isDragging = true;
this.dragNode = node;
const rect = nodeElement.getBoundingClientRect();
this.dragOffset = {
x: e.clientX - rect.left,
y: e.clientY - rect.top
};
nodeElement.classList.add('dragging');
}
/**
* Handle drag movement
*/
handleDrag(e) {
if (!this.dragNode) return;
const containerRect = this.canvasContainer.getBoundingClientRect();
const newX = e.clientX - containerRect.left - this.dragOffset.x;
const newY = e.clientY - containerRect.top - this.dragOffset.y;
this.dragNode.position = {
x: Math.max(0, newX),
y: Math.max(0, newY)
};
// Update node visual position
this.dragNode.element.style.left = `${this.dragNode.position.x}px`;
this.dragNode.element.style.top = `${this.dragNode.position.y}px`;
// Redraw connections
this.drawConnections();
// Update minimap
this.updateMinimap();
}
/**
* End dragging
*/
endDrag() {
if (this.dragNode) {
this.dragNode.element.classList.remove('dragging');
// Update layout
this.layout[this.dragNode.data.id] = this.dragNode.position;
}
this.isDragging = false;
this.dragNode = null;
}
/**
* Open terminal modal for an agent
*/
openTerminal(nodeId) {
if (this.isDragging) return;
const node = this.nodes.get(nodeId);
if (!node) return;
const modal = document.getElementById('terminalModal');
const title = document.getElementById('terminalTitle');
const output = document.getElementById('terminalOutput');
title.textContent = `${node.data.name} - Terminal`;
// Try to get real agent logs first
const realLogs = API.getAgentLogs(nodeId);
if (realLogs && realLogs.length > 0) {
output.textContent = this.formatRealLogs(realLogs, node.data);
} else {
// Fallback to mock terminal output
output.textContent = this.generateTerminalOutput(node.data);
}
modal.classList.add('active');
}
/**
* Format real agent logs for terminal display
*/
formatRealLogs(logs, agent) {
let output = `=== ${agent.name} Decision Log ===\n\n`;
logs.forEach((log, index) => {
const timestamp = log.timestamp || new Date().toISOString();
const stepType = log.step_type || log.action || 'Decision';
output += `[${timestamp}] Step ${index + 1}: ${stepType}\n`;
// Show short message if available
if (log.short_message) {
output += ` Message: ${log.short_message}\n`;
}
// Show details if available
if (log.details && Object.keys(log.details).length > 0) {
output += ` Details:\n`;
Object.entries(log.details).forEach(([key, value]) => {
let displayValue;
if (typeof value === 'number') {
displayValue = Number.isInteger(value) ? value : value.toFixed(2);
} else if (typeof value === 'object') {
displayValue = JSON.stringify(value);
} else {
displayValue = value;
}
// Format key to be more readable
const formattedKey = key.replace(/_/g, ' ').replace(/\b\w/g, c => c.toUpperCase());
output += ` - ${formattedKey}: ${displayValue}\n`;
});
}
// Legacy fields support
if (log.decision) {
output += ` Decision: ${log.decision}\n`;
}
if (log.reasoning) {
output += ` Reasoning: ${log.reasoning}\n`;
}
if (log.input_snapshot) {
const summary = JSON.stringify(log.input_snapshot).substring(0, 150);
output += ` Input: ${summary}${summary.length >= 150 ? '...' : ''}\n`;
}
if (log.output_snapshot) {
const summary = JSON.stringify(log.output_snapshot).substring(0, 150);
output += ` Output: ${summary}${summary.length >= 150 ? '...' : ''}\n`;
}
output += '\n';
});
output += `=== Total: ${logs.length} decision(s) ===`;
return output;
}
/**
* Generate mock terminal output
*/
generateTerminalOutput(agent) {
const timestamp = new Date().toISOString();
const allocationRun = API.getCurrentAllocationRun();
// If we have a real allocation run, show real info
if (allocationRun) {
const outputs = {
central_orchestrator: `[${timestamp}] Central Orchestrator Agent - LIVE\n[${timestamp}] Current Allocation Run: ${allocationRun.id.substring(0, 8)}...\n[${timestamp}] Status: ${allocationRun.status || 'COMPLETED'}\n[${timestamp}] Gini Index: ${allocationRun.gini_index?.toFixed(4) || 'N/A'}\n[${timestamp}] Std Dev: ${allocationRun.std_dev?.toFixed(2) || 'N/A'}\n[${timestamp}] Avg Workload: ${allocationRun.avg_workload?.toFixed(2) || 'N/A'}`,
route_database: `[${timestamp}] Route Database Connection - LIVE\n[${timestamp}] Host: localhost (SQLite)\n[${timestamp}] Database: fair_dispatch.db\n[${timestamp}] Allocation Run ID: ${allocationRun.id}\n[${timestamp}] Last sync: ${allocationRun.created_at || timestamp}\n[${timestamp}] Connection: Active`
};
if (outputs[agent.id]) {
return outputs[agent.id];
}
}
// Default mock outputs
const mockOutputs = {
central_orchestrator: `[${timestamp}] Central Orchestrator Agent initialized\n[${timestamp}] Monitoring 6 child agents\n[${timestamp}] Data flow channels: ACTIVE\n[${timestamp}] Current task: Coordinating route allocation\n[${timestamp}] Status: All systems nominal\n[${timestamp}] Last heartbeat: 2ms ago`,
route_database: `[${timestamp}] Route Database Connection\n[${timestamp}] Host: localhost\n[${timestamp}] Database: fair_dispatch\n[${timestamp}] Last sync: ${timestamp}\n[${timestamp}] Connection pool: healthy`,
route_planner: `[${timestamp}] Route Planner Agent - Standby\n[${timestamp}] Awaiting allocation request...\n[${timestamp}] K-Means clustering: Ready\n[${timestamp}] OR-Tools optimization: Ready`,
fairness_manager: `[${timestamp}] Fairness Manager Agent\n[${timestamp}] Gini threshold: 0.30\n[${timestamp}] Awaiting route proposals...`,
driver_liaison: `[${timestamp}] Driver Liaison Agent\n[${timestamp}] Processing driver feedback...\n[${timestamp}] Driver contexts loaded\n[${timestamp}] Awaiting route proposals...`,
explainability: `[${timestamp}] Explainability Agent\n[${timestamp}] Template engine: ACTIVE\n[${timestamp}] Language support: en, ta, hi\n[${timestamp}] Gemini integration: READY`,
learning: `[${timestamp}] Learning Agent\n[${timestamp}] Model: Ready\n[${timestamp}] Mode: Online learning`
};
return mockOutputs[agent.id] || `[${timestamp}] Agent ${agent.name} initialized`;
}
/**
* Show data payload modal
*/
showDataPayload(connection) {
const modal = document.getElementById('dataModal');
const title = document.getElementById('dataTitle');
const payload = document.getElementById('dataPayload');
const fromNode = this.nodes.get(connection.from);
const toNode = this.nodes.get(connection.to);
title.textContent = `Data Flow: ${fromNode?.data.name || connection.from} → ${toNode?.data.name || connection.to}`;
// Generate mock payload
payload.textContent = JSON.stringify(this.generateDataPayload(connection), null, 2);
modal.classList.add('active');
}
/**
* Generate mock data payload
*/
generateDataPayload(connection) {
const payloads = {
'central_orchestrator-route_planner': {
type: 'allocation_request',
timestamp: new Date().toISOString(),
packages: 47,
drivers: 5,
warehouse: { lat: 12.9716, lng: 77.5946 },
config: {
target_packages_per_route: 20,
fairness_threshold: 0.30
}
},
'route_planner-fairness_manager': {
type: 'route_proposal',
proposal_id: 1,
assignments: [
{ driver_id: 'driver_001', route_id: 'route_001', effort: 65.3 },
{ driver_id: 'driver_002', route_id: 'route_002', effort: 72.1 }
],
total_effort: 324.5
}
};
const key = `${connection.from}-${connection.to}`;
return payloads[key] || {
type: 'data_flow',
from: connection.from,
to: connection.to,
active: connection.active,
timestamp: new Date().toISOString()
};
}
/**
* Close modal
*/
closeModal(modalId) {
document.getElementById(modalId).classList.remove('active');
}
/**
* Update zoom level
*/
setZoom(level) {
this.zoom = Math.max(0.5, Math.min(2, level));
this.nodesContainer.style.transform = `scale(${this.zoom})`;
this.nodesContainer.style.transformOrigin = 'center center';
this.updateMinimap();
}
/**
* Save layout to localStorage
*/
saveLayout() {
const layoutData = {};
this.nodes.forEach((node, id) => {
layoutData[id] = node.position;
});
localStorage.setItem('agentDashboardLayout', JSON.stringify(layoutData));
// Show brief confirmation
this.showToast('Layout saved!');
}
/**
* Load layout from localStorage
*/
loadLayout() {
try {
const saved = localStorage.getItem('agentDashboardLayout');
if (saved) {
const layoutData = JSON.parse(saved);
this.layout = { ...this.getDefaultLayout(), ...layoutData };
}
} catch (e) {
console.warn('Could not load saved layout:', e);
}
}
/**
* Reset layout to default
*/
resetLayout() {
this.layout = this.getDefaultLayout();
localStorage.removeItem('agentDashboardLayout');
// Update all node positions
this.nodes.forEach((node, id) => {
node.position = this.layout[id] || { x: 100, y: 100 };
this.updateNodePosition(id);
});
// Redraw connections
this.drawConnections();
this.updateMinimap();
this.showToast('Layout reset!');
}
/**
* Show toast notification
*/
showToast(message) {
// Simple toast implementation
const toast = document.createElement('div');
toast.style.cssText = `
position: fixed;
bottom: 60px;
left: 50%;
transform: translateX(-50%);
background: var(--accent-primary);
color: var(--bg-primary);
padding: 10px 20px;
border-radius: 6px;
font-size: 13px;
font-weight: 500;
z-index: 2000;
animation: fadeInOut 2s ease;
`;
toast.textContent = message;
document.body.appendChild(toast);
setTimeout(() => toast.remove(), 2000);
}
/**
* Update minimap
*/
updateMinimap() {
// Clear minimap
this.minimap.innerHTML = '<div class="minimap-viewport" id="minimapViewport"></div>';
const minimapWidth = 180;
const minimapHeight = 120;
const containerWidth = this.canvasContainer.clientWidth;
const containerHeight = this.canvasContainer.clientHeight;
const scaleX = minimapWidth / containerWidth;
const scaleY = minimapHeight / containerHeight;
const scale = Math.min(scaleX, scaleY);
// Draw nodes on minimap
this.nodes.forEach((node) => {
const miniNode = document.createElement('div');
miniNode.className = 'minimap-node';
miniNode.style.left = `${node.position.x * scale}px`;
miniNode.style.top = `${node.position.y * scale}px`;
miniNode.style.width = '8px';
miniNode.style.height = '5px';
this.minimap.appendChild(miniNode);
});
// Update viewport indicator
const viewport = document.getElementById('minimapViewport');
if (viewport) {
viewport.style.width = `${minimapWidth}px`;
viewport.style.height = `${minimapHeight}px`;
viewport.style.left = '0';
viewport.style.top = '0';
}
}
/**
* Update stats display
*/
updateStats(stats) {
document.getElementById('processingCount').textContent = stats.processing;
document.getElementById('dataFlowCount').textContent = stats.dataFlows;
document.getElementById('agentCount').textContent = stats.totalAgents;
}
/**
* Setup API polling for real-time updates
*/
setupAPIPolling() {
// Handle connection status updates
API.onStatusUpdate((status) => {
// Update connection indicator
const liveIndicator = document.querySelector('.status-indicator');
const liveStatus = document.getElementById('liveStatus');
if (status.connected) {
if (liveStatus) liveStatus.textContent = 'Live';
if (liveIndicator) {
liveIndicator.classList.remove('disconnected');
liveIndicator.querySelector('.status-dot').style.background = '';
}
} else {
if (liveStatus) liveStatus.textContent = 'Offline';
if (liveIndicator) {
liveIndicator.classList.add('disconnected');
liveIndicator.querySelector('.status-dot').style.background = 'var(--status-error)';
}
}
});
// Handle workflow updates (real-time data)
API.onWorkflowUpdate((state) => {
// Debug log
console.log(`[${new Date().toLocaleTimeString()}] Poll update received. Real data: ${state.isRealData}, Run ID: ${state.allocationRun?.id?.substring(0, 8) || 'none'}`);
if (state && state.isRealData !== undefined) {
// Check if this is new data
const isNewData = !this.currentWorkflowState ||
(state.allocationRun?.id !== this.currentWorkflowState?.allocationRun?.id);
// Update current state
this.currentWorkflowState = state;
// Re-render agents with new data - ALWAYS update to show real statuses
this.updateAgentStatuses(state.agents);
// Update connections - ALWAYS update to show active flows
this.updateConnections(state.connections);
// Update stats
this.updateStats(state.stats);
// Update minimap
this.updateMinimap();
// Show notification if new allocation (but don't change date picker)
if (isNewData && state.isRealData) {
this.showToast(`🚀 New allocation! Run: ${state.allocationRun.id.substring(0, 8)}...`);
// Refresh map if active
if (document.getElementById('mapWrapper')?.classList.contains('active')) {
this.renderMap(state.allocationRun);
}
// Trigger Replay Animation for the workflow
if (state.timeline) {
this.playWorkflowAnimation(state.timeline);
}
}
// Also update map in background if we have allocation data
if (state.allocationRun && this.map) {
this.renderMap(state.allocationRun);
}
}
});
API.onError((error) => {
console.error('API Error:', error);
});
// Start polling
API.startPolling();
}
/**
* Simulate agent activity for demo
*/
simulateActivity() {
const agentIds = ['route_planner', 'fairness_manager', 'driver_liaison', 'explainability', 'learning'];
let currentIndex = 0;
setInterval(() => {
// Reset all to idle
agentIds.forEach(id => {
const node = this.nodes.get(id);
if (node) {
node.element.classList.remove('processing');
node.data.status = 'idle';
}
});
// Set current to processing
const currentId = agentIds[currentIndex];
const currentNode = this.nodes.get(currentId);
if (currentNode) {
currentNode.element.classList.add('processing');
currentNode.data.status = 'processing';
}
// Update connections
const state = API.getMockWorkflowState();
state.connections.forEach((conn, i) => {
conn.active = i <= currentIndex + 1;
});
this.updateConnections(state.connections);
// Move to next
currentIndex = (currentIndex + 1) % agentIds.length;
// Update stats
this.updateStats({
processing: 1,
dataFlows: currentIndex + 2,
totalAgents: 6
});
}, 3000);
}
/**
* Handle start allocation click
*/
async handleStartAllocation() {
const btn = document.getElementById('startAllocationBtn');
if (btn.disabled) return;
// FIRST: Force update date picker to today immediately (LOCAL timezone)
const now = new Date();
const today = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')}`;
const dateInput = document.getElementById('historyDate');
if (dateInput) {
dateInput.value = today;
this._currentDisplayDate = today;
console.log('[Start Allocation] Date picker forced to:', today);
}
API.setSelectedDate(today);
// Clear any cached old data
API._lastKnownRunId = null;
API._lastRunCount = 0;
API._currentAllocationRun = null;
// Disable button and show loading state
btn.disabled = true;
const originalText = btn.innerHTML;
btn.innerHTML = `<span class="status-badge processing" style="background:transparent; border:none; padding:0;"><svg viewBox="0 0 14 14" fill="none" stroke="currentColor" stroke-width="2" style="animation: spin 1s linear infinite;"><path d="M7 1v2M7 11v2M1 7h2M11 7h2"/></svg></span> Starting...`;
// Set all agents to "processing" state for visual feedback
this.setAgentsProcessing();
this.showToast('🚀 Initiating Agent Workflow with 30 packages & 5 drivers...');
try {
// Generate comprehensive test data for Bangalore area
const payload = this.generateTestAllocationData();
// Call API
const result = await API.triggerAllocation(payload);
// Success
this.showToast(`🎉 Workflow Complete! Run ID: ${result.allocation_run_id.substring(0, 8)}... | Gini: ${result.global_fairness.gini_index.toFixed(3)}`);
console.log('Allocation result:', result);
// Force refresh the dashboard data
API._lastKnownRunId = null;
API._lastRunCount = 0;
API._currentAllocationRun = null;
const workflowState = await API.getRealWorkflowState(today);
if (workflowState) {
// Update UI with new data
this.currentWorkflowState = workflowState;
this.updateAgentStatuses(workflowState.agents);
this.updateConnections(workflowState.connections);
this.updateStats(workflowState.stats);
// Render map if allocation run exists
if (workflowState.allocationRun) {
this.renderMap(workflowState.allocationRun);
}
// Play workflow animation
if (workflowState.timeline) {
this.playWorkflowAnimation(workflowState.timeline);
}
}
} catch (error) {
console.error('Allocation start failed:', error);
this.showToast('❌ Failed to start: ' + error.message);
// Reset agents to idle on error
this.resetAgentsToIdle();
} finally {
// Restore button after delay
setTimeout(() => {
btn.disabled = false;
btn.innerHTML = originalText;
}, 2000);
}
}
/**
* Set all agents to processing state for visual feedback
*/
setAgentsProcessing() {
const agentIds = ['route_planner', 'fairness_manager', 'driver_liaison', 'explainability', 'learning'];
agentIds.forEach(id => {
const node = this.nodes.get(id);
if (node) {
node.element.classList.add('processing');
const statusBadge = node.element.querySelector('.agent-status');
if (statusBadge) {
statusBadge.className = 'agent-status processing';
statusBadge.innerHTML = '<span class="dot"></span> Processing';
}
}
});
}
/**
* Reset all agents to idle state
*/
resetAgentsToIdle() {
const agentIds = ['route_planner', 'fairness_manager', 'driver_liaison', 'explainability', 'learning'];
agentIds.forEach(id => {
const node = this.nodes.get(id);
if (node) {
node.element.classList.remove('processing');
const statusBadge = node.element.querySelector('.agent-status');
if (statusBadge) {
statusBadge.className = 'agent-status idle';
statusBadge.innerHTML = '<span class="dot"></span> Idle';
}
}
});
}
/**
* Generate comprehensive test allocation data
* Creates realistic packages and drivers for Bangalore area
*/
generateTestAllocationData() {
const timestamp = Date.now();
// Bangalore area locations with realistic addresses
const locations = [
{ name: "Electronic City Phase 1", lat: 12.8456, lng: 77.6603, area: "South" },
{ name: "Electronic City Phase 2", lat: 12.8520, lng: 77.6680, area: "South" },
{ name: "Whitefield Main Road", lat: 12.9698, lng: 77.7500, area: "East" },
{ name: "ITPL Main Road", lat: 12.9854, lng: 77.7081, area: "East" },
{ name: "Marathahalli Bridge", lat: 12.9591, lng: 77.6974, area: "East" },
{ name: "Indiranagar 100ft Road", lat: 12.9784, lng: 77.6408, area: "Central" },
{ name: "Indiranagar CMH Road", lat: 12.9716, lng: 77.6412, area: "Central" },
{ name: "Koramangala 4th Block", lat: 12.9352, lng: 77.6245, area: "Central" },
{ name: "Koramangala 5th Block", lat: 12.9344, lng: 77.6150, area: "Central" },
{ name: "HSR Layout Sector 1", lat: 12.9116, lng: 77.6389, area: "South" },
{ name: "HSR Layout Sector 7", lat: 12.9081, lng: 77.6476, area: "South" },
{ name: "BTM Layout 1st Stage", lat: 12.9166, lng: 77.6101, area: "South" },
{ name: "JP Nagar 5th Phase", lat: 12.9063, lng: 77.5857, area: "South" },
{ name: "Jayanagar 4th Block", lat: 12.9308, lng: 77.5838, area: "South" },
{ name: "Bannerghatta Road", lat: 12.8876, lng: 77.5973, area: "South" },
{ name: "MG Road", lat: 12.9756, lng: 77.6068, area: "Central" },
{ name: "Brigade Road", lat: 12.9716, lng: 77.6070, area: "Central" },
{ name: "Commercial Street", lat: 12.9824, lng: 77.6074, area: "Central" },
{ name: "Rajajinagar 1st Block", lat: 12.9914, lng: 77.5521, area: "West" },
{ name: "Malleswaram 18th Cross", lat: 13.0067, lng: 77.5713, area: "North" },
{ name: "Yeshwanthpur", lat: 13.0271, lng: 77.5450, area: "North" },
{ name: "Hebbal Flyover", lat: 13.0358, lng: 77.5970, area: "North" },
{ name: "Yelahanka New Town", lat: 13.1007, lng: 77.5963, area: "North" },
{ name: "RT Nagar", lat: 13.0206, lng: 77.5970, area: "North" },
{ name: "Basavanagudi Bull Temple", lat: 12.9429, lng: 77.5688, area: "South" },
{ name: "Vijayanagar BDA Complex", lat: 12.9710, lng: 77.5360, area: "West" },
{ name: "Kengeri Satellite Town", lat: 12.9048, lng: 77.4823, area: "West" },
{ name: "Banashankari 2nd Stage", lat: 12.9255, lng: 77.5468, area: "South" },
{ name: "Sarjapur Road", lat: 12.9107, lng: 77.6868, area: "East" },
{ name: "Bellandur Lake View", lat: 12.9260, lng: 77.6762, area: "East" },
];
// Generate 30 packages with varied properties
const priorities = ["NORMAL", "NORMAL", "NORMAL", "HIGH", "HIGH", "EXPRESS"];
const packages = locations.map((loc, index) => ({
id: `pkg_${timestamp}_${String(index + 1).padStart(3, '0')}`,
weight_kg: parseFloat((Math.random() * 8 + 0.5).toFixed(2)), // 0.5 to 8.5 kg
fragility_level: Math.floor(Math.random() * 4) + 1, // 1-4
address: `${loc.name}, Bangalore - ${560000 + Math.floor(Math.random() * 100)}`,
latitude: loc.lat + (Math.random() - 0.5) * 0.01, // Small random offset
longitude: loc.lng + (Math.random() - 0.5) * 0.01,
priority: priorities[Math.floor(Math.random() * priorities.length)]
}));
// Generate 5 drivers with different capacities and languages
const driverNames = [
{ name: "Rajesh Kumar", lang: "hi", capacity: 200 },
{ name: "Venkatesh S", lang: "kn", capacity: 180 },
{ name: "Mohammed Ashraf", lang: "en", capacity: 220 },
{ name: "Suresh Babu", lang: "ta", capacity: 150 },
{ name: "Prashanth Gowda", lang: "kn", capacity: 250 }
];
const drivers = driverNames.map((d, index) => ({
id: `driver_${timestamp}_${String(index + 1).padStart(2, '0')}`,
name: d.name,
vehicle_capacity_kg: d.capacity,
preferred_language: d.lang
}));
return {
allocation_date: API.getTodayDate(),
warehouse: { lat: 12.9716, lng: 77.5946 }, // MG Road area - central Bangalore
packages,
drivers
};
}
/**
* Map Integration Methods
*/
initMap() {
if (this.map) return;
// Initialize Leaflet map
this.map = L.map('map').setView([12.9716, 77.5946], 12);
L.tileLayer('https://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}{r}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors © <a href="https://carto.com/attributions">CARTO</a>',
subdomains: 'abcd',
maxZoom: 19
}).addTo(this.map);
this.mapLayer = L.layerGroup().addTo(this.map);
this.renderMap(this.currentWorkflowState?.allocationRun);
}
toggleView(view) {
const graphBtn = document.getElementById('graphViewBtn');
const mapBtn = document.getElementById('mapViewBtn');
const mapWrapper = document.getElementById('mapWrapper');
if (view === 'map') {
mapBtn.classList.add('active');
graphBtn.classList.remove('active');
mapWrapper.classList.add('active');
if (!this.map) this.initMap();
} else {
graphBtn.classList.add('active');
mapBtn.classList.remove('active');
mapWrapper.classList.remove('active');
}
}
async renderMap(allocationRun) {
if (!this.map || !allocationRun) return;
this.mapLayer.clearLayers();
// Premium color palette with gradients
const routeColors = [
{ main: '#3b82f6', glow: '#60a5fa', name: 'Route A' },
{ main: '#f59e0b', glow: '#fbbf24', name: 'Route B' },
{ main: '#ec4899', glow: '#f472b6', name: 'Route C' },
{ main: '#10b981', glow: '#34d399', name: 'Route D' },
{ main: '#8b5cf6', glow: '#a78bfa', name: 'Route E' },
{ main: '#f97316', glow: '#fb923c', name: 'Route F' }
];
// Premium Warehouse marker with pulsing animation
const warehouseIcon = L.divIcon({
className: 'warehouse-marker-premium',
html: `
<div style="position:relative;">
<div style="
position:absolute;
width:40px; height:40px;
background: radial-gradient(circle, rgba(0,212,170,0.3) 0%, transparent 70%);
border-radius:50%;
animation: pulse 2s infinite;
left:-12px; top:-12px;
"></div>
<div style="
width:20px; height:20px;
background: linear-gradient(135deg, #00d4aa 0%, #00a388 100%);
border-radius:50%;
border:3px solid #fff;
box-shadow: 0 0 20px rgba(0,212,170,0.6), 0 4px 8px rgba(0,0,0,0.3);
display:flex; align-items:center; justify-content:center;
">
<span style="color:#fff; font-size:10px; font-weight:bold;">W</span>
</div>
</div>
`,
iconSize: [20, 20],
iconAnchor: [10, 10]
});
L.marker([12.9716, 77.5946], { icon: warehouseIcon })
.addTo(this.mapLayer)
.bindPopup(`
<div style="font-family: 'Inter', sans-serif; padding: 8px;">
<div style="font-size:14px; font-weight:600; color:#00d4aa; margin-bottom:4px;">
🏭 Central Warehouse
</div>
<div style="font-size:12px; color:#888;">Bangalore Hub</div>
<div style="font-size:11px; color:#666; margin-top:4px;">
📍 12.9716°N, 77.5946°E
</div>
</div>
`);
// Get assignments
let assignments = allocationRun.assignments;
if (!assignments || assignments.length === 0) {
try {
const dateStr = allocationRun.run_date || allocationRun.allocation_date || API.getTodayDate();
const response = await API.getAssignments(dateStr);
assignments = response?.items || [];
} catch (e) {
console.error('Failed to fetch assignments:', e);
assignments = [];
}
}
if (!assignments || assignments.length === 0) {
// Add "No routes" indicator
this.addMapLegend([]);
return;
}
// Render each route with premium styling
const renderedRoutes = [];
for (let i = 0; i < assignments.length; i++) {
const assign = assignments[i];
try {
const routeId = assign.route_id || assign.route?.id;
if (!routeId) continue;
const route = await API.getRoute(routeId);
const colorScheme = routeColors[i % routeColors.length];
const driverName = assign.driver?.name || assign.driver_name || `Driver ${i + 1}`;
if (route.stops && route.stops.length > 0) {
const latlngs = [[12.9716, 77.5946]];
// Render stops with premium markers
route.stops.forEach((stop, idx) => {
const latlng = [stop.latitude, stop.longitude];
latlngs.push(latlng);
// Premium stop marker with number
const stopIcon = L.divIcon({
className: 'stop-marker-premium',
html: `
<div style="
width:24px; height:24px;
background: linear-gradient(135deg, ${colorScheme.main} 0%, ${colorScheme.glow} 100%);
border-radius:50%;
border:2px solid #fff;
box-shadow: 0 2px 8px rgba(0,0,0,0.3), 0 0 12px ${colorScheme.main}40;
display:flex; align-items:center; justify-content:center;
font-size:10px; font-weight:bold; color:#fff;
">${idx + 1}</div>
`,
iconSize: [24, 24],
iconAnchor: [12, 12]
});
L.marker(latlng, { icon: stopIcon })
.addTo(this.mapLayer)
.bindPopup(`
<div style="font-family: 'Inter', sans-serif; padding: 8px; min-width: 180px;">
<div style="
display:flex; align-items:center; gap:8px;
padding-bottom:8px; border-bottom:1px solid #eee; margin-bottom:8px;
">
<div style="
width:28px; height:28px;
background: ${colorScheme.main};
border-radius:50%;
display:flex; align-items:center; justify-content:center;
color:#fff; font-weight:bold; font-size:12px;
">${idx + 1}</div>
<div>
<div style="font-weight:600; color:#333;">Stop ${idx + 1}</div>
<div style="font-size:11px; color:#666;">${driverName}</div>
</div>
</div>
<div style="font-size:12px; color:#555; margin-bottom:4px;">
📍 ${stop.address || 'Delivery Location'}
</div>
<div style="display:flex; gap:12px; font-size:11px; color:#888;">
<span>📦 ${stop.weight_kg?.toFixed(1) || '?'} kg</span>
<span>⚡ ${stop.priority || 'Normal'}</span>
</div>
</div>
`);
});
// Animated route line with glow effect
// Background glow
L.polyline(latlngs, {
color: colorScheme.glow,
weight: 8,
opacity: 0.3,
lineCap: 'round',
lineJoin: 'round'
}).addTo(this.mapLayer);
// Main line
L.polyline(latlngs, {
color: colorScheme.main,
weight: 4,
opacity: 0.9,
lineCap: 'round',
lineJoin: 'round'
}).addTo(this.mapLayer);
// Direction arrows
const decorator = L.polylineDecorator ? L.polylineDecorator(L.polyline(latlngs), {
patterns: [{
offset: 25,
repeat: 50,
symbol: L.Symbol.arrowHead({
pixelSize: 8,
polygon: false,
pathOptions: { stroke: true, color: '#fff', weight: 2, opacity: 0.8 }
})
}]
}).addTo(this.mapLayer) : null;
renderedRoutes.push({
color: colorScheme.main,
name: driverName,
stops: route.stops.length,
distance: route.total_distance_km || 'N/A'
});
}
} catch (e) {
console.error('Error rendering route:', e);
}
}
// Add legend
this.addMapLegend(renderedRoutes);
// Fit bounds to show all markers
if (this.mapLayer.getLayers().length > 0) {
try {
const group = L.featureGroup(this.mapLayer.getLayers());
this.map.fitBounds(group.getBounds().pad(0.1));
} catch (e) {
// Ignore bounds error
}
}
}
addMapLegend(routes) {
// Remove existing legend
if (this.mapLegend) {
this.map.removeControl(this.mapLegend);
}
// Create custom legend control
const LegendControl = L.Control.extend({
options: { position: 'bottomright' },
onAdd: function () {
const div = L.DomUtil.create('div', 'map-legend');
div.style.cssText = `
background: rgba(15, 23, 42, 0.95);
backdrop-filter: blur(10px);
padding: 12px 16px;
border-radius: 12px;
border: 1px solid rgba(255,255,255,0.1);
box-shadow: 0 4px 20px rgba(0,0,0,0.3);
font-family: 'Inter', sans-serif;
min-width: 160px;
`;
let html = `<div style="font-size:12px; font-weight:600; color:#fff; margin-bottom:8px; display:flex; align-items:center; gap:6px;">
<span style="font-size:14px;">🗺️</span> Route Legend
</div>`;
if (routes.length === 0) {
html += `<div style="font-size:11px; color:#888;">No routes to display</div>`;
} else {
routes.forEach((route, i) => {
html += `
<div style="display:flex; align-items:center; gap:8px; padding:4px 0; border-bottom:1px solid rgba(255,255,255,0.05);">
<div style="width:12px; height:12px; background:${route.color}; border-radius:50%;"></div>
<div style="flex:1;">
<div style="font-size:11px; color:#fff; font-weight:500;">${route.name}</div>
<div style="font-size:10px; color:#888;">${route.stops} stops</div>
</div>
</div>
`;
});
}
div.innerHTML = html;
return div;
}
});
this.mapLegend = new LegendControl();
this.map.addControl(this.mapLegend);
}
async loadHistory(date) {
this.showToast(`Loading history for ${date}...`);
try {
const response = await API.getAllocationRuns(date);
const runs = response?.runs || [];
if (runs && runs.length > 0) {
// Load the first run of the day for now
const latestRun = runs[runs.length - 1]; // Get the latest
// Construct a state object similar to verify/real data
// Note: We need to manually construct the state structure as getRealWorkflowState does
// Or refactor to reuse that logic. For now, simple update.
this.currentWorkflowState = {
allocationRun: latestRun,
isRealData: true,
agents: {}, // We might miss agent logs for history unless we fetch timeline too
stats: {},
connections: []
};
// Fetch timeline for logs
const timeline = await API.getAgentTimeline(latestRun.id);
const state = API.transformTimelineToWorkflowState(timeline, latestRun);
this.currentWorkflowState = state;
// Update UI
this.updateAgentStatuses(state.agents);
this.updateStats(state.stats);
this.updateConnections(state.connections);
this.renderMap(latestRun);
this.showToast(`Loaded ${runs.length} runs for ${date}`);
} else {
this.showToast(`No data found for ${date}`);
}
} catch (e) {
console.error(e);
this.showToast('Failed to load history');
}
}
/**
* Animate the workflow steps
*/
async playWorkflowAnimation(timeline) {
// Handle both new format (timeline.timeline) and legacy format (timeline.steps)
const steps = timeline?.timeline || timeline?.steps || [];
if (!steps || steps.length === 0) return;
this.showToast('Replaying agent workflow...');
console.log('Starting workflow replay with steps:', steps.length);
// Helper to reset all nodes visual state
this.nodes.forEach(node => {
// Remove status classes
const statusBadge = node.element.querySelector('.agent-status');
if (statusBadge) {
statusBadge.className = 'agent-status idle';
statusBadge.innerHTML = '<span class="dot"></span> Idle';
}
node.element.classList.remove('active', 'processing');
});
// Map backend agent names to frontend node IDs
const agentMap = {
'MLEffortAgent': 'route_planner',
'RoutePlannerAgent': 'route_planner',
'FairnessManagerAgent': 'fairness_manager',
'DriverLiaisonAgent': 'driver_liaison',
'ExplainabilityAgent': 'explainability',
'LearningAgent': 'learning',
};
for (const step of steps) {
const nodeId = agentMap[step.agent_name];
if (!nodeId) continue;
const node = this.nodes.get(nodeId);
if (node) {
// Update to processing
const statusBadge = node.element.querySelector('.agent-status');
if (statusBadge) {
statusBadge.className = 'agent-status processing';
statusBadge.innerHTML = '<span class="dot"></span> Processing';
}
node.element.classList.add('processing');
// Log to terminal
const logMsg = `[${step.agent_name}] ${step.step_type}`;
console.log(logMsg);
// Use the existing terminal if open, or update the mock one
// We'll assuming updateConnections highlights the flow too
await new Promise(r => setTimeout(r, 1200)); // Visible delay
// Update to active/done
if (statusBadge) {
statusBadge.className = 'agent-status active';
statusBadge.innerHTML = '<span class="dot"></span> Active';
}
node.element.classList.remove('processing');
node.element.classList.add('active');
}
}
this.showToast('Workflow Replay Complete');
}
}
// Add fadeInOut animation
const style = document.createElement('style');
style.textContent = `
@keyframes fadeInOut {
0% { opacity: 0; transform: translateX(-50%) translateY(20px); }
20% { opacity: 1; transform: translateX(-50%) translateY(0); }
80% { opacity: 1; transform: translateX(-50%) translateY(0); }
100% { opacity: 0; transform: translateX(-50%) translateY(-20px); }
}
`;
document.head.appendChild(style);
// Initialize dashboard when DOM is ready
document.addEventListener('DOMContentLoaded', () => {
window.dashboard = new AgentSupervisionDashboard();
// Start activity simulation for demo
// window.dashboard.simulateActivity();
});
|