Spaces:
Running
Running
File size: 61,753 Bytes
41449f8 | 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 | const CLAIM_HISTORY_URL = "greenwashing_claim_history.json";
const SUPERCLAIMS_URL = "greenwashing_superclaims.json";
const CODEBOOK_URL = "greenwashing_codebook.json";
const CLAIM_SUPERCLAIM_MAP_URL = "claim_superclaim_map.json";
const COLLAPSE_MAP_URL = "subclaim_bertopic_collapse.json";
let flattenedSnippets = null;
/** @type {string | null} */
let artifactBundleVersion = null;
/** @type {Map<string, { topicId: number, collapseFlag: boolean, collapseWith: string[], topicLabel?: string, hierarchyConfidence?: number }> | null} */
let collapseBySubclaim = null;
/** @type {Map<string, { superclaimId: string, superclaimText: string }> | null} subclaim → mapped superclaim (from loaded JSON) */
let superclaimMappingBySubclaim = null;
let dataLoadError = null;
function normalizeSubclaimId(raw) {
const s = String(raw).trim();
if (!s) return "";
if (s.startsWith("NC_")) return s;
return `NC_${s.replace(/^(NC_|SC_)/, "")}`;
}
function normalizeSuperclaimId(raw) {
const s = String(raw).trim();
if (!s) return "";
if (s.startsWith("SC_")) return s;
return `SC_${s.replace(/^(SC_|NC_)/, "")}`;
}
/**
* @param {unknown} json
* @param {"subclaim" | "superclaim"} kind
* @returns {Map<string, string>}
*/
function loadIdTextMap(json, kind) {
if (!json || typeof json !== "object" || Array.isArray(json)) {
const label =
kind === "subclaim" ? "greenwashing_codebook.json" : "greenwashing_superclaims.json";
throw new Error(`${label} must be a JSON object of {id: text}`);
}
const out = new Map();
for (const [k, v] of Object.entries(json)) {
const text = String(v == null ? "" : v).trim();
const id =
kind === "subclaim"
? normalizeSubclaimId(k)
: normalizeSuperclaimId(k);
if (!id) continue;
out.set(id, text);
}
return out;
}
/**
* @param {unknown} obj
* @returns {{ subclaimId: string, superclaimId: string, mapSuperText: string }[]}
*/
function parseClaimSuperclaimMap(obj) {
const rows = [];
if (obj == null) return rows;
if (typeof obj === "object" && !Array.isArray(obj)) {
const keys = Object.keys(obj);
const first = keys[0];
const sample = first != null ? obj[first] : undefined;
const isCombined =
sample != null &&
typeof sample === "object" &&
!Array.isArray(sample) &&
(Object.prototype.hasOwnProperty.call(sample, "superclaim_id") ||
Object.prototype.hasOwnProperty.call(sample, "superclaimId") ||
Object.prototype.hasOwnProperty.call(sample, "sc_id"));
if (isCombined) {
for (const [subId, record] of Object.entries(obj)) {
if (!record || typeof record !== "object" || Array.isArray(record)) continue;
const sc =
record.superclaim_id ??
record.superclaimId ??
record.sc_id ??
record.SC;
if (sc == null) continue;
const mapSuperText = String(
record.superclaim_text ??
record.superclaimText ??
record.superclaim ??
""
).trim();
rows.push({
subclaimId: normalizeSubclaimId(subId),
superclaimId: normalizeSuperclaimId(sc),
mapSuperText,
});
}
return rows;
}
for (const [nc, sc] of Object.entries(obj)) {
rows.push({
subclaimId: normalizeSubclaimId(nc),
superclaimId: normalizeSuperclaimId(sc),
mapSuperText: "",
});
}
return rows;
}
if (Array.isArray(obj)) {
for (const item of obj) {
if (Array.isArray(item) && item.length >= 2) {
rows.push({
subclaimId: normalizeSubclaimId(item[0]),
superclaimId: normalizeSuperclaimId(item[1]),
mapSuperText: "",
});
continue;
}
if (item && typeof item === "object") {
const nc =
item.subclaim_id ??
item.nc_id ??
item.subclaim ??
item.NC;
const sc =
item.superclaim_id ??
item.sc_id ??
item.superclaim ??
item.SC;
if (nc == null || sc == null) continue;
const mapSuperText = String(
item.superclaim_text ?? item.superclaimText ?? ""
).trim();
rows.push({
subclaimId: normalizeSubclaimId(nc),
superclaimId: normalizeSuperclaimId(sc),
mapSuperText,
});
}
}
}
return rows;
}
async function loadClaimsData() {
try {
const [historyRes, superRes, codebookRes, mapRes] = await Promise.all([
fetch(CLAIM_HISTORY_URL),
fetch(SUPERCLAIMS_URL),
fetch(CODEBOOK_URL),
fetch(CLAIM_SUPERCLAIM_MAP_URL),
]);
if (!historyRes.ok) throw new Error(`claim history HTTP ${historyRes.status}`);
if (!superRes.ok) throw new Error(`superclaims HTTP ${superRes.status}`);
if (!codebookRes.ok) throw new Error(`codebook HTTP ${codebookRes.status}`);
if (!mapRes.ok) throw new Error(`claim_superclaim_map HTTP ${mapRes.status}`);
const [historyJson, superJson, codebookJson, mapJson] = await Promise.all([
historyRes.json(),
superRes.json(),
codebookRes.json(),
mapRes.json(),
]);
// BERTopic collapse (offline artifact; optional).
collapseBySubclaim = new Map();
try {
const collapseRes = await fetch(COLLAPSE_MAP_URL);
if (collapseRes.ok) {
const collapseJson = await collapseRes.json();
if (typeof collapseJson.claims_bundle_version === "string") {
artifactBundleVersion = collapseJson.claims_bundle_version;
}
const sub = collapseJson.subclaims || {};
for (const [sid, row] of Object.entries(sub)) {
if (!row || typeof row !== "object") continue;
const collapseWith = Array.isArray(row.collapse_with)
? row.collapse_with.map(String)
: [];
const hc = row.hierarchy_confidence;
const entry = {
topicId: Number(row.topic_id),
collapseFlag: Boolean(row.collapse_flag),
collapseWith,
topicLabel:
typeof row.topic_label === "string" ? row.topic_label : undefined,
};
if (typeof hc === "number" && Number.isFinite(hc)) {
entry.hierarchyConfidence = hc;
}
collapseBySubclaim.set(sid, entry);
}
}
} catch {
// Missing or invalid collapse file: UI continues without collapse hints.
}
const superclaimsById = loadIdTextMap(superJson, "superclaim");
const codebookById = loadIdTextMap(codebookJson, "subclaim");
const mapRows = parseClaimSuperclaimMap(mapJson);
const superclaimBySubclaim = new Map();
for (const { subclaimId, superclaimId, mapSuperText } of mapRows) {
if (!subclaimId || !superclaimId) continue;
superclaimBySubclaim.set(subclaimId, {
superclaimId,
superclaimText:
superclaimsById.get(superclaimId) || mapSuperText || "",
});
}
superclaimMappingBySubclaim = superclaimBySubclaim;
const claims = historyJson.claims || {};
const snippets = [];
for (const [claimId, claimObj] of Object.entries(claims)) {
if (!claimObj || typeof claimObj !== "object") continue;
const subclaimId = claimId.startsWith("NC_")
? claimId
: `NC_${claimId.replace(/^(NC_|SC_)/, "")}`;
const subclaimText =
codebookById.get(subclaimId) ||
claimObj.current_text ||
"";
const mapping = superclaimBySubclaim.get(subclaimId);
if (!mapping) continue;
const { superclaimId, superclaimText } = mapping;
const history = Array.isArray(claimObj.history) ? claimObj.history : [];
history.forEach((entry) => {
if (!entry || !entry.source_snippet) return;
snippets.push({
sentenceSnippet: entry.source_snippet,
snippetLower: entry.source_snippet.toLowerCase(),
superclaimText,
superclaimId,
subclaimId,
subclaimText,
});
});
}
flattenedSnippets = snippets;
if (isRedesignedUi()) {
setTextIfPresent("stat-subclaims", codebookById.size);
setTextIfPresent("stat-superclaims", superclaimsById.size);
}
} catch (err) {
console.error("Failed to load claim data:", err);
dataLoadError = err;
}
}
/** Paragraphs = blocks separated by line breaks (after normalizing newlines). */
function splitIntoParagraphs(text) {
const normalized = text.replace(/\r\n/g, "\n").trim();
if (!normalized) return [];
return normalized
// Treat each line as its own paragraph; ignore empty lines.
.split(/\n+/)
.map((p) => p.replace(/\s+/g, " ").trim())
.filter(Boolean);
}
function computeMatchConfidence(paragraphLower, snippetLower) {
// NOTE: Keeps *mapping selection* behavior (local overlap / LCS).
if (!paragraphLower || !snippetLower) return 0;
if (paragraphLower === snippetLower) return 1;
if (snippetLower.includes(paragraphLower) || paragraphLower.includes(snippetLower)) return 1;
const minLen = Math.min(paragraphLower.length, snippetLower.length);
if (minLen < 12) return 0;
const intersection = longestCommonSubstr(paragraphLower, snippetLower);
return intersection.length / minLen;
}
/**
* Unique superclaims mapped from peer subclaims in the same BERTopic cluster.
* @param {string[]} peerSubclaimIds
* @param {string} [currentSuperclaimId] matched superclaim for this row (for labels)
*/
function superclaimTargetsFromPeers(peerSubclaimIds, currentSuperclaimId) {
const map = superclaimMappingBySubclaim;
if (!map || !peerSubclaimIds.length) return [];
const bySc = new Map();
for (const raw of peerSubclaimIds) {
const sid = normalizeSubclaimId(raw);
if (!sid) continue;
const m = map.get(sid);
if (!m || !m.superclaimId) continue;
if (!bySc.has(m.superclaimId)) {
bySc.set(m.superclaimId, {
superclaimId: m.superclaimId,
superclaimText: m.superclaimText || "",
});
}
}
const list = Array.from(bySc.values()).map((entry) => ({
...entry,
isCurrentMapping:
Boolean(currentSuperclaimId) && entry.superclaimId === currentSuperclaimId,
}));
list.sort((a, b) => {
if (a.isCurrentMapping !== b.isCurrentMapping) return a.isCurrentMapping ? 1 : -1;
return a.superclaimId.localeCompare(b.superclaimId);
});
return list;
}
function formatSuperclaimTargetsHtml(targets, { mappedPeerCount, artifactPeerCount }) {
if (!targets.length) {
return `<div class="collapse-peer-superclaims collapse-peer-superclaims--empty">No superclaim targets could be resolved from cluster peers that still appear in your map.</div>`;
}
const maxShow = 8;
const slice = targets.slice(0, maxShow);
const more =
targets.length > maxShow
? `<div class="collapse-more">+${targets.length - maxShow} more superclaim(s)</div>`
: "";
const staleNote =
artifactPeerCount > mappedPeerCount
? `<p class="collapse-peer-superclaims-stale">${artifactPeerCount - mappedPeerCount} offline cluster peer(s) are not in your current <code>claim_superclaim_map</code> and were ignored.</p>`
: "";
const items = slice
.map((t) => {
const idHtml = `<span class="claim-id">${escapeHtml(t.superclaimId)}</span>`;
const badge = t.isCurrentMapping
? ` <span class="collapse-mapping-badge">same as match</span>`
: "";
const textShort = t.superclaimText
? escapeHtml(
t.superclaimText.length > 160
? `${t.superclaimText.slice(0, 157)}…`
: t.superclaimText
)
: "";
return `<li class="collapse-sc-target-item">
<div class="collapse-sc-target-line">${idHtml}${badge}</div>
${textShort ? `<div class="collapse-sc-target-text">${textShort}</div>` : ""}
</li>`;
})
.join("");
return `
<div class="collapse-peer-superclaims">
<div class="collapse-peer-superclaims-title">Potential superclaims to collapse toward</div>
<p class="collapse-peer-superclaims-hint">Using <strong>${mappedPeerCount}</strong> cluster peer subclaim${mappedPeerCount === 1 ? "" : "s"} that still exist in your map${artifactPeerCount !== mappedPeerCount ? ` (of ${artifactPeerCount} in the offline artifact)` : ""}. Targets below are their mapped superclaims.</p>
${staleNote}
<ul class="collapse-sc-target-list">${items}</ul>
${more}
</div>
`;
}
function formatHierarchyLine(row, liveConfidence) {
if (typeof liveConfidence === "number" && Number.isFinite(liveConfidence)) {
const v = clamp01(liveConfidence);
const html = `<div class="hierarchy-confidence">Confidence (LLM): <strong>${v.toFixed(2)}</strong></div>`;
return {
html,
titlePart: `LLM confidence: ${v.toFixed(2)}`,
};
}
if (
!row ||
typeof row.hierarchyConfidence !== "number" ||
!Number.isFinite(row.hierarchyConfidence)
) {
return { html: "", titlePart: "" };
}
const v = row.hierarchyConfidence;
const html = `<div class="hierarchy-confidence">Confidence (offline cosine, subclaim↔superclaim): <strong>${v.toFixed(2)}</strong></div>`;
return {
html,
titlePart: `Offline cosine: ${v.toFixed(2)}`,
};
}
/**
* @param {string} subclaimId
* @param {string} [currentSuperclaimId] superclaim id for the matched row (for target labeling)
* @param {number|null|undefined} [liveConfidence] LLM confidence for this mapping (preferred over offline cosine)
*/
function formatCollapseMeta(subclaimId, currentSuperclaimId, liveConfidence) {
if (!collapseBySubclaim || !collapseBySubclaim.size) {
return {
html: `<span class="collapse-meta">No artifact loaded (run <code>python scripts/build_subclaim_collapse_bertopic.py</code>).</span>`,
title: "Generate subclaim_bertopic_collapse.json",
};
}
const row = collapseBySubclaim.get(subclaimId);
if (!row) {
return {
html: `<span class="collapse-meta">No BERTopic row for this subclaim.</span>`,
title: "",
};
}
// UI display rule: only show collapse *targets* when the offline hierarchy cosine
// similarity is above a minimum threshold. (We still show the score itself.)
const MIN_COLLAPSE_SIMILARITY = 0.6;
const hier = formatHierarchyLine(row, liveConfidence);
const peersArtifact = (row.collapseWith || []).map(String);
const peersMapped = superclaimMappingBySubclaim
? peersArtifact.filter((p) =>
superclaimMappingBySubclaim.has(normalizeSubclaimId(p))
)
: [...peersArtifact];
const label = row.topicLabel ? escapeHtml(String(row.topicLabel)) : "";
if (!row.collapseFlag || peersArtifact.length === 0) {
const tid = Number.isFinite(row.topicId) ? row.topicId : "";
const labelBit = label
? `<div class="collapse-topic-label">${label}</div>`
: "";
const titleBits = [
hier.titlePart,
label ? `Topic label: ${row.topicLabel}` : "",
"Singleton or outlier — not flagged for merge",
].filter(Boolean);
return {
html: `
<div class="collapse-meta">
${hier.html}
<span class="collapse-badge collapse-badge-quiet">Unique topic${tid !== "" ? ` (${tid})` : ""}</span>
${labelBit}
</div>
`,
title: titleBits.join(" · "),
};
}
if (peersMapped.length === 0) {
const labelBlock = label
? `<div class="collapse-topic-label">${label}</div>`
: "";
const titleBits = [
hier.titlePart,
label ? `Topic label: ${row.topicLabel}` : "",
"Cluster peers missing from current map",
].filter(Boolean);
return {
html: `
<div class="collapse-meta">
${hier.html}
<span class="collapse-badge">Topic cluster</span>
${labelBlock}
<div class="collapse-cluster-hint collapse-cluster-hint--error">
The offline artifact lists ${peersArtifact.length} cluster peer subclaim${peersArtifact.length === 1 ? "" : "s"}, but none appear in your current <code>claim_superclaim_map</code>. Regenerate <code>subclaim_bertopic_collapse.json</code> or refresh the map so collapse targets stay in sync.
</div>
</div>
`,
title: titleBits.join(" · "),
};
}
if (
(typeof liveConfidence !== "number" || !Number.isFinite(liveConfidence)) &&
typeof row.hierarchyConfidence === "number" &&
Number.isFinite(row.hierarchyConfidence) &&
row.hierarchyConfidence < MIN_COLLAPSE_SIMILARITY
) {
const labelBlock = label
? `<div class="collapse-topic-label">${label}</div>`
: "";
const titleBits = [
hier.titlePart,
label ? `Topic label: ${row.topicLabel}` : "",
`Collapse suggestions hidden (similarity < ${MIN_COLLAPSE_SIMILARITY.toFixed(2)})`,
].filter(Boolean);
return {
html: `
<div class="collapse-meta">
${hier.html}
<span class="collapse-badge">Topic cluster</span>
${labelBlock}
<div class="collapse-cluster-hint collapse-cluster-hint--muted">
Cluster peers exist, but collapse targets are hidden because the offline cosine similarity is below <strong>${MIN_COLLAPSE_SIMILARITY.toFixed(
2
)}</strong>.
</div>
</div>
`,
title: titleBits.join(" · "),
};
}
const scTargets = superclaimTargetsFromPeers(peersMapped, currentSuperclaimId);
const targetsHtml = formatSuperclaimTargetsHtml(scTargets, {
mappedPeerCount: peersMapped.length,
artifactPeerCount: peersArtifact.length,
});
const labelBlock = label
? `<div class="collapse-topic-label">${label}</div>`
: "";
const titleBits = [
hier.titlePart,
label ? `Topic label: ${row.topicLabel}` : "",
scTargets.length
? `Superclaims: ${scTargets.map((t) => t.superclaimId).join(", ")}`
: "",
].filter(Boolean);
return {
html: `
<div class="collapse-meta">
${hier.html}
<span class="collapse-badge">Topic cluster</span>
${labelBlock}
${targetsHtml}
</div>
`,
title: titleBits.join(" · "),
};
}
function findBestMatchesForParagraph(paragraph) {
if (!flattenedSnippets || flattenedSnippets.length === 0) return [];
const lowerParagraph = paragraph.toLowerCase();
const candidates = flattenedSnippets
.filter((s) => {
const confidence = computeMatchConfidence(lowerParagraph, s.snippetLower);
return confidence >= 0.6;
})
.map((s) => ({
...s,
mappingConfidence: computeMatchConfidence(
lowerParagraph,
s.snippetLower
),
}));
// For each paragraph, ensure at most one match per subclaim.
const bestBySubclaim = new Map();
for (const c of candidates) {
const key = c.subclaimId;
const existing = bestBySubclaim.get(key);
if (!existing || c.mappingConfidence > existing.mappingConfidence) {
bestBySubclaim.set(key, c);
}
}
const dedup = Array.from(bestBySubclaim.values()).sort(
(a, b) => b.mappingConfidence - a.mappingConfidence
);
return dedup.slice(0, 4);
}
function longestCommonSubstr(a, b) {
const dp = Array(a.length + 1)
.fill(null)
.map(() => Array(b.length + 1).fill(0));
let longest = 0;
let endIdx = 0;
for (let i = 1; i <= a.length; i++) {
for (let j = 1; j <= b.length; j++) {
if (a[i - 1] === b[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
if (dp[i][j] > longest) {
longest = dp[i][j];
endIdx = i;
}
}
}
}
return a.slice(endIdx - longest, endIdx);
}
function isRedesignedUi() {
try {
return (
Boolean(document.querySelector(".shell")) &&
Boolean(document.getElementById("results-container")) &&
Boolean(document.getElementById("proposals-list"))
);
} catch {
return false;
}
}
function setTextIfPresent(id, value) {
const el = document.getElementById(id);
if (!el) return;
el.textContent = String(value);
}
function clamp01(x) {
const n = typeof x === "number" ? x : Number(x);
if (!Number.isFinite(n)) return 0;
return Math.max(0, Math.min(1, n));
}
function pickConfidence(m) {
if (!m || typeof m !== "object") return { value: null, label: "" };
const llm = typeof m.confidence === "number" && Number.isFinite(m.confidence) ? clamp01(m.confidence) : null;
if (llm != null) {
const src = String(m.confidenceSource || "").trim();
if (src === "llm" || src === "llm_prompt") return { value: llm, label: "Confidence (LLM)" };
if (src === "tfidf_fallback") return { value: llm, label: "Confidence (TF‑IDF fallback)" };
if (src === "tfidf_no_openai_key") return { value: llm, label: "Confidence (TF‑IDF; no OpenAI key)" };
return { value: llm, label: "Confidence" };
}
const heuristic =
typeof m.mappingConfidence === "number" && Number.isFinite(m.mappingConfidence)
? clamp01(m.mappingConfidence)
: null;
if (heuristic != null) return { value: heuristic, label: "Confidence (heuristic)" };
return { value: null, label: "" };
}
function formatTopicChipHtml(subclaimId) {
if (!collapseBySubclaim || !collapseBySubclaim.size) {
return `<div class="topic-chip topic-chip--none">⚠ No artifact loaded</div>`;
}
const row = collapseBySubclaim.get(String(subclaimId || ""));
if (!row) return `<div class="topic-chip topic-chip--none">⚠ No BERTopic row</div>`;
const tid = Number.isFinite(row.topicId) ? row.topicId : "";
const peers = Array.isArray(row.collapseWith) ? row.collapseWith : [];
if (!row.collapseFlag || peers.length === 0) {
return `<div class="topic-chip topic-chip--none">⚠ Unique Topic${tid !== "" ? ` (${tid})` : ""}</div>`;
}
return `<div class="topic-chip topic-chip--match">Topic cluster${tid !== "" ? ` (${tid})` : ""}</div>`;
}
function renderResultsRedesigned(paragraphsWithMatches) {
const container = document.getElementById("results-container");
if (!container) return;
container.innerHTML = "";
if (!paragraphsWithMatches.length) {
container.innerHTML = `<div class="empty-note">No results yet. Paste an article and click “Analyze Paragraphs”.</div>`;
setTextIfPresent("stat-paragraphs", 0);
return;
}
setTextIfPresent("stat-paragraphs", paragraphsWithMatches.length);
paragraphsWithMatches.forEach(({ paragraph, matches }, idx) => {
const card = document.createElement("div");
card.className = "para-card";
const title = `Paragraph ${idx + 1} of ${paragraphsWithMatches.length}`;
const paraHtml = escapeHtml(paragraph || "");
const subBlocks = (Array.isArray(matches) ? matches : []).length
? matches
.map((m) => {
return `
<div class="claim-block claim-block--sub">
<div>
<span class="claim-type-tag claim-type-tag--sub">Subclaim <span class="claim-id-badge">${escapeHtml(
m.subclaimId || ""
)}</span></span>
</div>
<div class="claim-text">${escapeHtml(m.subclaimText || "")}</div>
</div>
`;
})
.join("")
: `<div class="no-match-note">No subclaim mapping found.</div>`;
const superBlocks = (Array.isArray(matches) ? matches : []).length
? matches
.map((m) => {
const picked = pickConfidence(m);
const conf = picked.value != null ? picked.value : 0;
return `
<div class="claim-block claim-block--super">
<div>
<span class="claim-type-tag claim-type-tag--super">Superclaim <span class="claim-id-badge">${escapeHtml(
m.superclaimId || ""
)}</span></span>
</div>
<div class="claim-text">${escapeHtml(m.superclaimText || "")}</div>
${formatTopicChipHtml(m.subclaimId)}
<div class="sim-row" style="margin-top:8px">
<span class="sim-label">${escapeHtml(picked.label || "Confidence")}</span>
<div class="sim-bar-wrap"><div class="sim-bar-fill sim-bar-fill--amber" style="width:${Math.round(
conf * 100
)}%"></div></div>
<span class="sim-val">${conf.toFixed(2)}</span>
</div>
</div>
`;
})
.join("")
: `<div class="no-match-note">No superclaim mapping found.</div>`;
card.innerHTML = `
<div class="para-card-header">
<div class="para-num">${idx + 1}</div>
<div>
<div class="para-card-title">${escapeHtml(title)}</div>
<div class="para-card-sub">Maps below apply only to this paragraph.</div>
</div>
</div>
<div class="para-grid">
<div class="para-col">
<div class="col-label">Paragraph Text</div>
<div class="para-text-block">${paraHtml}</div>
</div>
<div class="para-col">
<div class="col-label">Subclaim(s)</div>
${subBlocks}
</div>
<div class="para-col">
<div class="col-label">Superclaim(s)</div>
${superBlocks}
</div>
</div>
`;
container.appendChild(card);
});
}
function renderResults(paragraphsWithMatches) {
if (isRedesignedUi()) {
renderResultsRedesigned(paragraphsWithMatches);
return;
}
const container = document.getElementById("results-container");
container.innerHTML = "";
if (!paragraphsWithMatches.length) {
const p = document.createElement("p");
p.className = "placeholder";
p.textContent = "No paragraphs found. Paste article text (paragraphs separated by blank lines) and click “Analyze paragraphs”.";
container.appendChild(p);
return;
}
const ledger = document.createElement("div");
ledger.className = "results-ledger";
const total = paragraphsWithMatches.length;
paragraphsWithMatches.forEach(({ paragraph, matches, proposals: paragraphProposals = [] }, idx) => {
const card = document.createElement("article");
card.className = "paragraph-result-card";
card.setAttribute("aria-labelledby", `paragraph-result-title-${idx}`);
const header = document.createElement("header");
header.className = "paragraph-result-header";
const badge = document.createElement("span");
badge.className = "paragraph-result-badge";
badge.textContent = String(idx + 1);
const headerText = document.createElement("div");
headerText.className = "paragraph-result-header-text";
const titleEl = document.createElement("div");
titleEl.className = "paragraph-result-title";
titleEl.id = `paragraph-result-title-${idx}`;
titleEl.textContent = `Paragraph ${idx + 1} of ${total}`;
const subEl = document.createElement("div");
subEl.className = "paragraph-result-sub";
subEl.textContent = "Maps below apply only to this paragraph.";
headerText.appendChild(titleEl);
headerText.appendChild(subEl);
header.appendChild(badge);
header.appendChild(headerText);
card.appendChild(header);
const table = document.createElement("table");
table.className = "results-table results-table--in-card";
const thead = document.createElement("thead");
thead.innerHTML = `
<tr>
<th scope="col">Paragraph text</th>
<th scope="col">Subclaim(s)</th>
<th scope="col">Superclaim(s)</th>
</tr>
`;
const tbody = document.createElement("tbody");
if (!matches.length) {
const tr = document.createElement("tr");
const tdSentence = document.createElement("td");
tdSentence.className = "sentence-cell paragraph-cell";
tdSentence.textContent = paragraph;
const tdSub = document.createElement("td");
const tdSuper = document.createElement("td");
tdSub.innerHTML = `<div class="no-match"><strong>No subclaim mapping found</strong></div>`;
tdSuper.innerHTML = `<div class="no-match"><strong>No superclaim mapping found</strong></div>`;
tr.appendChild(tdSentence);
tr.appendChild(tdSub);
tr.appendChild(tdSuper);
tbody.appendChild(tr);
} else {
const rowSpan = matches.length;
matches.forEach((m, mIdx) => {
const tr = document.createElement("tr");
if (mIdx === 0) {
const tdSentence = document.createElement("td");
tdSentence.className = "sentence-cell paragraph-cell";
tdSentence.textContent = paragraph;
tdSentence.rowSpan = rowSpan;
tr.appendChild(tdSentence);
}
const tdSub = document.createElement("td");
const tdSuper = document.createElement("td");
tdSub.innerHTML = `
<div class="claim-label">Subclaim <span class="claim-id">(${m.subclaimId})</span></div>
<div class="claim-text">${m.subclaimText}</div>
`;
const picked = pickConfidence(m);
const collapse = formatCollapseMeta(m.subclaimId, m.superclaimId, picked.value);
tdSuper.innerHTML = `
<div class="claim-label">Superclaim <span class="claim-id">(${m.superclaimId})</span></div>
<div class="claim-text">${m.superclaimText}</div>
<div class="claim-meta collapse-block" title="${escapeHtmlAttr(collapse.title)}">
${collapse.html}
</div>
`;
tr.appendChild(tdSub);
tr.appendChild(tdSuper);
tbody.appendChild(tr);
});
}
table.appendChild(thead);
table.appendChild(tbody);
card.appendChild(table);
ledger.appendChild(card);
});
container.appendChild(ledger);
}
function escapeHtmlAttr(s) {
return String(s)
.replaceAll("&", "&")
.replaceAll('"', """)
.replaceAll("'", "'")
.replaceAll("<", "<")
.replaceAll(">", ">");
}
function escapeHtml(s) {
return String(s)
.replaceAll("&", "&")
.replaceAll("<", "<")
.replaceAll(">", ">");
}
const REVIEWER_STORAGE_KEY = "CLAIMS_REVIEWER_NAME";
function getReviewerName() {
const el = document.getElementById("reviewer-name");
const fromInput = el && el.value != null ? String(el.value).trim() : "";
if (fromInput) return fromInput;
try {
const fromStorage = localStorage.getItem(REVIEWER_STORAGE_KEY);
return fromStorage ? String(fromStorage).trim() : "";
} catch {
return "";
}
}
function persistReviewerName(name) {
const el = document.getElementById("reviewer-name");
if (el) el.value = name;
try {
localStorage.setItem(REVIEWER_STORAGE_KEY, name);
} catch {
// ignore
}
}
function requireReviewerName() {
const name = getReviewerName();
if (!name) {
alert("Please enter your reviewer name before approving, rejecting, or applying proposals.");
const el = document.getElementById("reviewer-name");
if (el) el.focus();
return null;
}
persistReviewerName(name);
return name;
}
function formatProposalMetaHtml(p) {
const bits = [];
if (p.reviewedBy) {
bits.push(
`<div class="proposal-line"><strong>Reviewed by:</strong> ${escapeHtml(
p.reviewedBy
)}</div>`
);
}
if (p.appliedBy) {
bits.push(
`<div class="proposal-line"><strong>Applied by:</strong> ${escapeHtml(
p.appliedBy
)}</div>`
);
}
return bits.length ? `<div class="proposal-meta">${bits.join("")}</div>` : "";
}
function getApiCandidates() {
const out = [];
/** @param {string|null|undefined} v */
const pushUnique = (v) => {
if (v === "") {
if (!out.includes("")) out.push("");
return;
}
if (v == null) return;
const s = String(v).trim().replace(/\/+$/, "");
if (!s) return;
if (!out.includes(s)) out.push(s);
};
const host =
typeof window !== "undefined" && window.location && window.location.hostname
? window.location.hostname
: "";
const isDevHost = host === "localhost" || host === "127.0.0.1";
const isLocalApiUrl = (base) =>
/^https?:\/\/(localhost|127\.0\.0\.1)(:\d+)?\/?$/i.test(String(base || "").trim());
const meta = document.querySelector('meta[name="claims-api-base"]');
const fromMeta = meta && meta.getAttribute("content");
// 1) Same-origin `/api/*` (Vercel rewrites, or a dev proxy) — always try first in the browser.
if (typeof window !== "undefined") {
pushUnique("");
}
// 2) Local uvicorn before any hardcoded remote meta/storage so localhost dev is not blocked by a dead URL.
if (isDevHost) {
pushUnique("http://localhost:8001");
}
try {
const fromStorage = localStorage.getItem("CLAIMS_API_BASE");
if (fromStorage != null && String(fromStorage).trim() !== "") {
if (!isLocalApiUrl(fromStorage) || isDevHost) {
pushUnique(fromStorage);
}
}
} catch {
// ignore
}
if (fromMeta != null && String(fromMeta).trim() !== "") {
if (!isLocalApiUrl(fromMeta) || isDevHost) {
pushUnique(fromMeta);
}
}
return out;
}
function resolveApiUrl(base, path) {
if (!path) return base || "";
if (/^https?:\/\//i.test(path)) return path;
const b = base || "";
if (!b) return path;
return `${b}${path.startsWith("/") ? "" : "/"}${path}`;
}
/** Same-origin calls must send cookies so Vercel Deployment Protection can authorize `/api/*`. Cross-origin keeps `omit` so `Access-Control-Allow-Origin: *` stays valid. */
function apiFetchCredentials(target) {
try {
if (typeof window === "undefined" || !window.location) return "omit";
const resolved = new URL(target, window.location.href);
if (resolved.origin === window.location.origin) return "same-origin";
} catch {
// ignore
}
return "omit";
}
/** Prefer a short hint when the server returned HTML (e.g. Vercel auth) instead of JSON. */
function shortenApiFailureMessage(res, text) {
const raw = text == null ? "" : String(text);
const ct = (res && res.headers && res.headers.get("content-type")) || "";
if (
raw.includes("Authentication Required") ||
(ct.includes("text/html") && raw.includes("vercel") && raw.length > 400)
) {
return (
"Vercel Deployment Protection blocked this API request (HTML login page instead of JSON). " +
"Options: turn off protection for this environment, use a production deployment without protection, " +
"or stay signed in—same-origin requests now send cookies so protected previews can work after you open the site once."
);
}
if (ct.includes("text/html") && raw.length > 400) {
return `Unexpected HTML from API (HTTP ${res.status}). Check the URL and deployment protection settings.`;
}
return raw.length > 900 ? `${raw.slice(0, 900)}…` : raw;
}
async function postJson(url, body) {
const bases = getApiCandidates();
let lastErr = null;
for (const base of bases) {
try {
const target = resolveApiUrl(base, url);
const creds = apiFetchCredentials(target);
const res = await fetch(target, {
method: "POST",
credentials: creds,
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
const text = await res.text();
let data = null;
try {
data = text ? JSON.parse(text) : null;
} catch {
// ignore
}
if (!res.ok) {
const msg =
(data && (data.detail || data.message)) ||
shortenApiFailureMessage(res, text) ||
`HTTP ${res.status}`;
// IMPORTANT: never fall back to a different backend on a valid HTTP error response.
// Doing so can mix proposal IDs between deployments (list from one backend, apply on another).
throw new Error(msg);
}
return data;
} catch (e) {
const target = resolveApiUrl(base, url);
const msg = e && e.message ? String(e.message) : String(e);
lastErr = new Error(
msg === "Failed to fetch"
? `Failed to fetch (${target}). Confirm the API is deployed (open /api/health in a tab), CORS allows this origin, and meta claims-api-base / localStorage CLAIMS_API_BASE are correct or cleared.`
: `${msg} (${target})`
);
// If we got a real application error (not a network failure), stop here.
// "Proposal not found" is a good example: falling back will only make it worse.
if (msg !== "Failed to fetch") break;
}
}
throw lastErr || new Error("Request failed.");
}
async function getJson(url) {
const bases = getApiCandidates();
let lastErr = null;
for (const base of bases) {
try {
const target = resolveApiUrl(base, url);
const creds = apiFetchCredentials(target);
const res = await fetch(target, { method: "GET", credentials: creds });
const text = await res.text();
let errData = null;
if (!res.ok) {
try {
errData = text ? JSON.parse(text) : null;
} catch {
// ignore
}
const msg =
(errData && (errData.detail || errData.message)) ||
shortenApiFailureMessage(res, text) ||
`HTTP ${res.status}`;
throw new Error(msg);
}
if (text != null && String(text).trim()) {
try {
return JSON.parse(text);
} catch (parseErr) {
throw new Error(
`Invalid JSON (HTTP ${res.status}): ${parseErr && parseErr.message ? parseErr.message : String(parseErr)}`
);
}
}
return null;
} catch (e) {
const target = resolveApiUrl(base, url);
const msg = e && e.message ? String(e.message) : String(e);
lastErr = new Error(
msg === "Failed to fetch"
? `Failed to fetch (${target}). Confirm the API is deployed (open /api/health in a tab), CORS allows this origin, and meta claims-api-base / localStorage CLAIMS_API_BASE are correct or cleared.`
: `${msg} (${target})`
);
if (msg !== "Failed to fetch") break;
}
}
throw lastErr || new Error("Request failed.");
}
function formatNewSuperclaimSectionsHtml(p) {
const article = escapeHtml(p.paragraph || "");
const payload = p.payload || {};
const scText = escapeHtml(String(payload.superclaimText || "").trim());
return `
<div class="proposal-section">
<div class="proposal-section-heading">Article</div>
<div class="proposal-section-body">${article}</div>
</div>
<div class="proposal-section">
<div class="proposal-section-heading">Suggested original superclaim</div>
<div class="proposal-section-body proposal-section-body--superclaim">${scText}</div>
</div>
`;
}
function formatProposalTitle(p) {
const type = p.type || "";
if (type === "new_subclaim") return "New subclaim";
if (type === "new_superclaim") return "Suggested original superclaim";
if (type === "link_subclaim_to_superclaim") return "Remap subclaim → superclaim";
if (type === "merge_subclaims") return "Merge subclaims";
if (type === "merge_superclaims") return "Merge superclaims";
return type || "Proposal";
}
function formatProposalBodyHtml(p) {
const payload = p.payload || {};
if (p.type === "merge_subclaims") {
const ids = Array.isArray(payload.mergeSubclaimIds)
? payload.mergeSubclaimIds.map((x) => String(x)).join(", ")
: "";
const canonText = String(payload.canonicalSubclaimText || "").trim();
const removeText = String(payload.removeSubclaimText || "").trim();
const mergeTexts =
canonText || removeText
? `<div class="merge-detail">
<div class="merge-item">
<div class="merge-item-label">Keep (canonical)</div>
<div class="merge-item-id">${escapeHtml(payload.canonicalSubclaimId || "")}</div>
<div class="merge-item-text">${escapeHtml(
canonText || "(no text in payload)"
)}</div>
</div>
<div class="merge-item">
<div class="merge-item-label">Merge away</div>
<div class="merge-item-id">${escapeHtml(payload.removeSubclaimId || "")}</div>
<div class="merge-item-text">${escapeHtml(
removeText || "(no text in payload)"
)}</div>
</div>
</div>`
: "";
const idLines =
mergeTexts === ""
? `<div class="proposal-line"><strong>Canonical:</strong> <code>${escapeHtml(
payload.canonicalSubclaimId || ""
)}</code></div>
<div class="proposal-line"><strong>Remove:</strong> <code>${escapeHtml(
payload.removeSubclaimId || ""
)}</code></div>`
: "";
const cos =
typeof payload.pairCosine === "number"
? `<div class="proposal-line"><strong>Pair cosine (TF‑IDF):</strong> ${payload.pairCosine.toFixed(
3
)}</div>`
: "";
return `
${mergeTexts}
${idLines}
<div class="proposal-line"><strong>Group:</strong> <code>${escapeHtml(ids)}</code></div>
<div class="proposal-line"><strong>Shared superclaim:</strong> <code>${escapeHtml(
payload.sharedSuperclaimId || ""
)}</code></div>
${cos}
${p.rationale ? `<div class="proposal-line proposal-reason">${escapeHtml(p.rationale)}</div>` : ""}
`;
}
if (p.type === "merge_superclaims") {
const ids = Array.isArray(payload.mergeSuperclaimIds)
? payload.mergeSuperclaimIds.map((x) => String(x)).join(", ")
: "";
const canonText = String(payload.canonicalSuperclaimText || "").trim();
const removeText = String(payload.removeSuperclaimText || "").trim();
const mergeTexts =
canonText || removeText
? `<div class="merge-detail">
<div class="merge-item">
<div class="merge-item-label">Keep (canonical)</div>
<div class="merge-item-id">${escapeHtml(payload.canonicalSuperclaimId || "")}</div>
<div class="merge-item-text">${escapeHtml(
canonText || "(no text in payload)"
)}</div>
</div>
<div class="merge-item">
<div class="merge-item-label">Merge away</div>
<div class="merge-item-id">${escapeHtml(payload.removeSuperclaimId || "")}</div>
<div class="merge-item-text">${escapeHtml(
removeText || "(no text in payload)"
)}</div>
</div>
</div>`
: "";
const idLines =
mergeTexts === ""
? `<div class="proposal-line"><strong>Canonical:</strong> <code>${escapeHtml(
payload.canonicalSuperclaimId || ""
)}</code></div>
<div class="proposal-line"><strong>Remove:</strong> <code>${escapeHtml(
payload.removeSuperclaimId || ""
)}</code></div>`
: "";
const cos =
typeof payload.pairCosine === "number"
? `<div class="proposal-line"><strong>Pair cosine (TF‑IDF):</strong> ${payload.pairCosine.toFixed(
3
)}</div>`
: "";
return `
${mergeTexts}
${idLines}
<div class="proposal-line"><strong>Group:</strong> <code>${escapeHtml(ids)}</code></div>
${cos}
${p.rationale ? `<div class="proposal-line proposal-reason">${escapeHtml(p.rationale)}</div>` : ""}
`;
}
if (p.type === "new_subclaim") {
const sc = payload.suggestedSuperclaimId
? `<div class="proposal-line"><strong>Suggested superclaim:</strong> <code>${escapeHtml(
payload.suggestedSuperclaimId
)}</code> ${payload.suggestedSuperclaimText ? `— ${escapeHtml(payload.suggestedSuperclaimText)}` : ""}</div>`
: "";
const conf =
typeof payload.confidence === "number"
? `<div class="proposal-line"><strong>LLM confidence:</strong> ${(payload.confidence * 100).toFixed(0)}%</div>`
: "";
return `
${sc}
${conf}
${p.rationale ? `<div class="proposal-line proposal-reason">${escapeHtml(p.rationale)}</div>` : ""}
`;
}
if (p.type === "new_superclaim") {
const conf =
typeof payload.confidence === "number"
? `<div class="proposal-line"><strong>LLM confidence (best candidate):</strong> ${(payload.confidence * 100).toFixed(0)}%</div>`
: "";
const near =
payload.fromLowConfidenceMapping && payload.nearbySuperclaimId
? `<div class="proposal-line"><strong>Closest taxonomy superclaim:</strong> <code>${escapeHtml(
String(payload.nearbySuperclaimId)
)}</code>${payload.nearbySuperclaimText ? ` — ${escapeHtml(String(payload.nearbySuperclaimText))}` : ""}</div>`
: "";
const reason = p.rationale
? `<div class="proposal-line proposal-reason"><strong>Reasoning:</strong> ${escapeHtml(p.rationale)}</div>`
: "";
return `
${conf}
${near}
${reason}
`;
}
return `
<div class="proposal-line"><strong>Payload:</strong> <code>${escapeHtml(
JSON.stringify(payload)
)}</code></div>
${p.rationale ? `<div class="proposal-line proposal-reason">${escapeHtml(p.rationale)}</div>` : ""}
`;
}
async function refreshPendingProposals() {
const list = document.getElementById("proposals-list");
if (list) {
list.innerHTML = `<div class="empty-note">Loading pending proposals…</div>`;
let proposals = [];
try {
const raw = await getJson("/api/proposals?status=pending");
if (!Array.isArray(raw)) {
throw new Error(
raw == null
? "Empty response from /api/proposals (expected a JSON array)."
: `Invalid response from /api/proposals: expected an array, got ${typeof raw}.`
);
}
proposals = raw;
} catch (e) {
list.innerHTML = `<div class="empty-note">Unable to load proposals: ${escapeHtml(
e.message || String(e)
)}</div>`;
setTextIfPresent("stat-proposals", 0);
return;
}
setTextIfPresent("stat-proposals", proposals.length);
if (proposals.length === 0) {
list.innerHTML = `<div class="empty-note">No pending proposals yet.</div>`;
return;
}
const byId = new Map();
proposals.forEach((p) => {
if (p && p.id) byId.set(String(p.id), p);
});
list.innerHTML = "";
proposals.forEach((p) => {
const card = document.createElement("div");
card.className = "proposal-card";
const type = formatProposalTitle(p);
const icon =
p.type === "merge_superclaims" || p.type === "merge_subclaims"
? "⇄"
: p.type === "link_subclaim_to_superclaim"
? "↪"
: p.type === "new_superclaim"
? "+"
: "•";
const payload = p.payload || {};
const cos =
typeof payload.pairCosine === "number" && Number.isFinite(payload.pairCosine)
? payload.pairCosine
: null;
const mergeDetail =
p.type === "merge_superclaims" || p.type === "merge_subclaims"
? (() => {
const keepId =
p.type === "merge_superclaims"
? payload.canonicalSuperclaimId
: payload.canonicalSubclaimId;
const removeId =
p.type === "merge_superclaims"
? payload.removeSuperclaimId
: payload.removeSubclaimId;
const keepText =
p.type === "merge_superclaims"
? payload.canonicalSuperclaimText
: payload.canonicalSubclaimText;
const removeText =
p.type === "merge_superclaims"
? payload.removeSuperclaimText
: payload.removeSubclaimText;
return `
<div class="merge-detail">
<div class="merge-item">
<div class="merge-item-label">Keep (canonical)</div>
<div class="merge-item-id">${escapeHtml(keepId || "")}</div>
<div class="merge-item-text">${escapeHtml(
String(keepText || "").trim() || "(no text in payload)"
)}</div>
</div>
<div class="merge-item">
<div class="merge-item-label">Merge away</div>
<div class="merge-item-id">${escapeHtml(removeId || "")}</div>
<div class="merge-item-text">${escapeHtml(
String(removeText || "").trim() || "(no text in payload)"
)}</div>
</div>
</div>
`;
})()
: "";
const scoreRow =
cos != null
? `
<div class="score-row">
<span class="sim-label">TF‑IDF Cosine Similarity</span>
<div class="sim-bar-wrap" style="flex:1">
<div class="sim-bar-fill sim-bar-fill--amber" style="width:${Math.round(
clamp01(cos) * 100
)}%"></div>
</div>
<span class="score-val">${cos.toFixed(3)}</span>
</div>
`
: "";
const rationale = String(p.rationale || "").trim();
const bodyLine = escapeHtml(String(p.paragraph || "").trim());
card.innerHTML = `
<div class="proposal-header">
<div class="proposal-icon">${escapeHtml(icon)}</div>
<div>
<div class="proposal-type">${escapeHtml(type)}</div>
<div class="proposal-id">${escapeHtml(p.id || "")}</div>
</div>
</div>
${bodyLine ? `<div class="proposal-body">${bodyLine}</div>` : ""}
${mergeDetail}
${scoreRow}
${rationale ? `<div class="score-note">${escapeHtml(rationale)}</div>` : ""}
<div class="proposal-actions">
<button class="btn-approve" type="button" data-action="approve" data-id="${escapeHtmlAttr(
p.id || ""
)}">✓ Approve</button>
<button class="btn-reject" type="button" data-action="reject" data-id="${escapeHtmlAttr(
p.id || ""
)}">✕ Reject</button>
<button class="btn-apply" type="button" data-action="apply" data-id="${escapeHtmlAttr(
p.id || ""
)}">Apply to CSV →</button>
</div>
`;
list.appendChild(card);
});
// Match the static mock's footer note.
const footer = document.createElement("div");
footer.className = "empty-note";
footer.textContent =
"No further proposals. Run the taxonomy diagnosis pipeline to generate new proposals.";
list.appendChild(footer);
list.querySelectorAll("button[data-action]").forEach((btn) => {
btn.addEventListener("click", async (ev) => {
const el = ev.currentTarget;
const action = el.getAttribute("data-action");
const id = el.getAttribute("data-id");
if (!id || !action) return;
const card = el.closest(".proposal-card");
const cardButtons = card ? Array.from(card.querySelectorAll("button[data-action]")) : [el];
cardButtons.forEach((b) => (b.disabled = true));
try {
const reviewer = requireReviewerName();
if (!reviewer) {
cardButtons.forEach((b) => (b.disabled = false));
return;
}
if (action === "apply") {
await postJson(`/api/proposals/${encodeURIComponent(id)}/approve`, {
reviewer_name: reviewer,
});
}
await postJson(`/api/proposals/${encodeURIComponent(id)}/${action}`, {
reviewer_name: reviewer,
});
await refreshPendingProposals();
} catch (e) {
cardButtons.forEach((b) => (b.disabled = false));
alert(`Proposal ${action} failed: ${e.message || String(e)}`);
}
});
});
return;
}
const container = document.getElementById("proposals-container");
if (!container) return;
container.innerHTML = `<p class="placeholder">Loading pending proposals…</p>`;
let proposals = [];
try {
const raw = await getJson("/api/proposals?status=pending");
if (!Array.isArray(raw)) {
throw new Error(
raw == null
? "Empty response from /api/proposals (expected a JSON array)."
: `Invalid response from /api/proposals: expected an array, got ${typeof raw}.`
);
}
proposals = raw;
} catch (e) {
container.innerHTML = `<p class="placeholder error-text">Unable to load proposals: ${escapeHtml(
e.message || String(e)
)}</p>`;
return;
}
if (proposals.length === 0) {
container.innerHTML = `<p class="placeholder">No pending proposals yet.</p>
<p class="placeholder proposal-empty-hint">This list only shows proposals with status <strong>pending</strong> (approved/rejected/applied disappear here).</p>
<p class="placeholder proposal-empty-hint">Proposals are written to the database only when <strong>Analyze paragraphs</strong> succeeds on the <strong>backend</strong> (<code>/api/analyze</code>). If the status line says the backend failed and switched to a local heuristic, nothing is saved—open DevTools → Console / Network for the <code>/api/analyze</code> error.</p>
<p class="placeholder proposal-empty-hint">Open <a href="/api/health" target="_blank" rel="noopener"><code>/api/health</code></a>: <code>proposalsPersistence</code> should be <code>postgres</code> for Railway, <code>taxonomyProposalsTotal</code> is the row count in the DB (if <code>0</code>, no proposals were stored yet—run backend <strong>Analyze</strong> or import rows). This panel only lists <strong>pending</strong>; open <a href="/api/proposals" target="_blank" rel="noopener"><code>/api/proposals</code></a> for every status.</p>`;
return;
}
const wrap = document.createElement("div");
wrap.className = "results-ledger";
const byId = new Map();
proposals.forEach((p) => {
if (p && p.id) byId.set(String(p.id), p);
const card = document.createElement("div");
card.className = "proposal-card";
const type = formatProposalTitle(p);
const icon =
p.type === "merge_superclaims" || p.type === "merge_subclaims"
? "⇄"
: p.type === "link_subclaim_to_superclaim"
? "↪"
: p.type === "new_superclaim"
? "+"
: "•";
const bodyLine = escapeHtml(String(p.paragraph || "").trim());
card.innerHTML = `
<div class="proposal-header">
<div class="proposal-icon">${escapeHtml(icon)}</div>
<div>
<div class="proposal-type">${escapeHtml(type)}</div>
<div class="proposal-id">${escapeHtml(p.id || "")}</div>
</div>
</div>
${bodyLine ? `<div class="proposal-body">${bodyLine}</div>` : ""}
<div style="padding:0 16px 4px">${formatProposalBodyHtml(p)}${formatProposalMetaHtml(p)}</div>
<div class="proposal-actions">
<button class="btn-approve" data-action="approve" data-id="${escapeHtmlAttr(p.id || "")}">✓ Approve</button>
<button class="btn-reject" data-action="reject" data-id="${escapeHtmlAttr(p.id || "")}">✕ Reject</button>
<button class="btn-apply" data-action="apply" data-id="${escapeHtmlAttr(p.id || "")}">Apply to CSV →</button>
</div>
`;
wrap.appendChild(card);
});
container.innerHTML = "";
container.appendChild(wrap);
container.querySelectorAll("button[data-action]").forEach((btn) => {
btn.addEventListener("click", async (ev) => {
const el = ev.currentTarget;
const action = el.getAttribute("data-action");
const id = el.getAttribute("data-id");
if (!id || !action) return;
const card = el.closest(".proposal-card");
const cardButtons = card ? Array.from(card.querySelectorAll("button[data-action]")) : [el];
cardButtons.forEach((b) => (b.disabled = true));
try {
const reviewer = requireReviewerName();
if (!reviewer) {
cardButtons.forEach((b) => (b.disabled = false));
return;
}
// Apply implies approval: approve first, then apply.
if (action === "apply") {
await postJson(`/api/proposals/${encodeURIComponent(id)}/approve`, {
reviewer_name: reviewer,
});
}
await postJson(`/api/proposals/${encodeURIComponent(id)}/${action}`, {
reviewer_name: reviewer,
});
// Applying should also refresh claims data next run; for now just refresh the list.
await refreshPendingProposals();
} catch (e) {
cardButtons.forEach((b) => (b.disabled = false));
alert(`Proposal ${action} failed: ${e.message || String(e)}`);
}
});
});
}
async function handleAnalyzeClick() {
const btn = document.getElementById("analyze-btn");
const statusEl = document.getElementById("status");
const text = document.getElementById("article-input").value;
statusEl.textContent = "";
if (!text.trim()) {
statusEl.textContent = "Please paste an article first.";
statusEl.classList.remove("error-text");
return;
}
btn.disabled = true;
statusEl.textContent = "Analyzing…";
statusEl.classList.remove("error-text");
// BERTopic collapse + snippet index live in static JSON; load them even when the backend
// handles /api/analyze, otherwise collapseBySubclaim stays null and the UI shows "No artifact loaded".
if (!flattenedSnippets && !dataLoadError) {
await loadClaimsData();
}
// Prefer the backend LLM API (supports proposals + human approval). Fallback to local heuristic if unavailable.
try {
const api = await postJson("/api/analyze", { text });
const rows = Array.isArray(api?.paragraphs) ? api.paragraphs : [];
const withMatches = rows.map((r) => ({
paragraph: r.paragraph,
matches: Array.isArray(r.matches) ? r.matches : [],
proposals: Array.isArray(r.proposals) ? r.proposals : [],
}));
renderResults(withMatches);
statusEl.textContent = `Analyzed ${withMatches.length} paragraph${withMatches.length === 1 ? "" : "s"} · bundle ${escapeHtml(
api?.bundleVersion || ""
)}.`;
await refreshPendingProposals();
btn.disabled = false;
return;
} catch (e) {
console.warn("Backend /api/analyze failed; falling back to local matching.", e);
}
// Local fallback (does not POST proposals to the server—only backend /api/analyze does)
statusEl.textContent = "Backend unavailable; running local heuristic…";
if (!flattenedSnippets && !dataLoadError) {
await loadClaimsData();
}
if (dataLoadError) {
statusEl.textContent =
"Unable to load claim JSON files. Serve the folder over HTTP and check that the four data files are present.";
statusEl.classList.add("error-text");
btn.disabled = false;
return;
}
const paragraphs = splitIntoParagraphs(text);
const withMatches = paragraphs.map((p) => ({
paragraph: p,
matches: findBestMatchesForParagraph(p),
}));
renderResults(withMatches);
const bundleBit =
artifactBundleVersion != null ? ` · artifact ${artifactBundleVersion}` : "";
statusEl.textContent = `Analyzed ${paragraphs.length} paragraph${paragraphs.length === 1 ? "" : "s"}${bundleBit}. Local matching only—backend /api/analyze failed, so proposals were not saved (see Console / Network for the error).`;
statusEl.classList.add("error-text");
btn.disabled = false;
void refreshPendingProposals();
}
window.addEventListener("DOMContentLoaded", () => {
const btn = document.getElementById("analyze-btn");
if (btn) btn.addEventListener("click", handleAnalyzeClick);
const reviewerEl = document.getElementById("reviewer-name");
if (reviewerEl) {
try {
const saved = localStorage.getItem(REVIEWER_STORAGE_KEY);
if (saved) reviewerEl.value = saved;
} catch {
// ignore
}
reviewerEl.addEventListener("change", () => {
const v = String(reviewerEl.value || "").trim();
if (v) persistReviewerName(v);
});
}
void loadClaimsData();
refreshPendingProposals();
});
|