All files / src/views chat-view.ts

100% Statements 813/813
100% Branches 323/323
100% Functions 164/164
100% Lines 727/727

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553                                                                                                                                          2x     2x     2x 2x     2x   2x                     1520x                                                                                 2x                 3x                     1x           88x 37x   51x       90x 84x 84x             78x                       8x 6x 5x         255x   255x   255x   255x   255x   255x 255x 255x 255x 255x 255x 255x 255x 255x 255x 255x 255x 255x 255x 255x 255x   255x         255x 255x 255x 2x 255x 255x 255x     255x 255x       1x       1x       1x       251x 251x 251x   251x 251x 251x       27x 27x 27x 11x 11x   27x       251x           251x   251x 251x 251x 251x 251x 251x 17x     251x 251x 251x 251x 251x 251x 13x       251x 251x       251x       251x 251x 1x       251x   251x   251x     251x   251x 2x   251x 247x 247x         247x 741x 741x 741x 1x 1x 1x     247x     251x   251x 251x 251x 251x     251x 249x 249x 249x 249x     251x 251x 251x 251x   251x 251x 251x 251x 251x       251x   251x 251x 251x 251x   251x       251x 251x         251x         106x 106x 105x 105x       102x   1x 1x       251x 104x 1x   103x     251x 5x 3x 3x           275x   19x 22x 20x 22x 22x     254x 5x 5x   254x 254x 254x 254x 254x   254x 254x         254x   254x 6x 6x   248x       21x 21x 21x   21x 21x 21x 21x 21x 21x 21x 3x   21x             254x 254x 254x 9x 9x 254x   254x       254x 18x       18x 18x 18x 6x 6x 6x   18x 5x 3x           3x 3x 2x 2x   1x 1x             2x 2x 2x 4x 2x               255x 255x 221x 255x         269x 269x         269x 9x           269x         270x         498x 270x 270x 270x 22x 22x                   270x 8x 6x   270x         498x 270x 270x 498x 239x   476x 476x 476x                 17x 1x         23x 16x 12x           12x 12x 12x 11x     12x   4x         5x 5x   3x 3x 3x 3x   2x           13x 3x         6x 6x 6x 6x 6x 1x 1x   5x 5x 3x 3x 3x   2x 2x 2x 2x 2x                   1518x 1518x 1518x 1518x 1518x                   57x 40x 38x 38x 39x 39x 74x 74x       12x   11x 11x         38x                 254x 254x 254x 254x 254x             508x 508x 1016x                   1026x 1026x 1026x 1026x 1026x 2x   1026x         1026x 1026x     1026x 1026x 448x   1026x 1026x       1016x 1016x 1016x 1016x 1016x 1016x 1016x 1016x   1016x 10x 7x     1016x       7x 3x 3x     4x 4x 1x 1x   3x 3x 3x 3x         6x     5x 5x 3x 3x   2x       14x 14x 1x 1x   13x 13x 13x 13x 13x 9x 5x 5x 5x 4x         4x 3x 3x 3x 3x 3x 3x       3x 1x 1x   2x 2x 2x   3x 3x   10x   10x   7x   7x 7x 1x       6x 6x 6x   7x 7x           1x       28x 28x 28x 28x 28x       4x   1x 1x           1x 1x         102x 91x 91x 91x   89x   89x 89x       89x           165x   163x 163x 163x         5x 4x                   158x 156x         23x 23x   1x 1x 1x       22x 1x 1x   22x 22x 22x 22x   22x 22x 30x 30x   22x 22x 22x 22x         22x 4x 1x 1x   1x         22x 22x 3x 2x 2x 2x         750x         15x 8x 8x 8x 8x 8x 8x 8x 8x 8x             15x 15x 6x 5x         7x 7x 5x 5x 5x 5x 5x 5x       7x       31x 30x 16x 16x 16x   14x 14x 14x 14x         13x 13x 13x 13x 13x         3x 2x 2x 2x       106x 104x   103x 103x 103x 103x 103x 103x   103x 103x 103x   103x 102x 100x     103x 103x 103x 103x 103x 103x 103x 103x   103x                               103x 103x 173x 173x 173x   68x 68x 68x         103x 70x 69x 69x 69x     69x         66x       103x 103x                 192x             13x       7x 7x 7x 7x 4x 4x   3x   6x 2x 4x 2x   2x               103x 103x 103x 103x 103x 102x   103x                       205x     4x 2x 2x 2x     4x     15x   15x 15x 15x   15x     7x 7x 7x 7x 7x     70x   70x 67x 67x   70x 70x 70x       13x 13x 13x 13x 13x     5x 5x   83x 83x 83x     83x 83x 6x 6x 6x   83x 83x 83x   83x   83x 62x 62x   83x     5x 5x 5x       5x 5x 5x 5x       5x 5x 1x 1x   5x     3x 3x 2x 2x   3x             19x 8x   8x 8x 8x   8x 8x 8x 8x         13x 11x 11x 10x 10x     9x           251x 251x 247x         250x 250x 250x 245x         107x 107x 107x       6x 6x 6x 6x 6x 6x 6x       7x 7x 7x     1x     7x 7x   3x   7x 7x   1x   7x 7x     1x     7x         45x 45x 45x         45x 45x 45x   42x 2x 1x 1x     42x 42x     45x 45x 1x 1x 1x   44x       4x 4x     3x 2x     1x         2x       6x 5x 5x 5x 5x 5x 5x 1x         270x 1x 1x         4x 1x 1x   3x 15x 3x 3x 3x 3x   3x 3x 6x 6x   3x   3x 3x 3x 3x 2x   3x 2x   1x           18x 18x 18x 18x       5x 5x       83x 83x 1x 1x 1x               4x 4x 4x       1x       1x       2x      
import {
    FuzzySuggestModal,
    ItemView,
    MarkdownRenderer,
    Menu,
    Notice,
    Platform,
    setIcon,
    type TFile,
    WorkspaceLeaf,
} from "obsidian";
import type LilbeePlugin from "../main";
import {
    CATALOG_SOURCE,
    CHAT_MODE,
    CONFIG_KEY,
    HOSTED_SOURCES,
    MODEL_TASK,
    SEARCH_CHUNK_TYPE,
    SESSION_ROLE,
    SSE_EVENT,
    ERROR_NAME,
} from "../types";
import type {
    CatalogEntry,
    ChatMode,
    CompactingEventData,
    CompactionEventData,
    InstalledModel,
    MemoryExtractedData,
    Message,
    SearchChunkType,
    SessionDetail,
    SessionMessageItem,
    SessionRole,
    Source,
    SSEEvent,
} from "../types";
import { RateLimitedError, isHttpStatus } from "../api";
 
import { renderAggregatedSourceChips } from "./results";
import { displayLabelForRef, extractHfRepo } from "../utils/model-ref";
import { ConfirmPullModal } from "./confirm-pull-modal";
import { ConfirmModal } from "./confirm-modal";
import { CatalogModal } from "./catalog-modal";
import { CrawlModal } from "./crawl-modal";
import { addCloseButton } from "../components/close-button";
import { MESSAGES } from "../locales/en";
import {
    RETRY_INTERVAL_MS,
    SPINNER_MIN_DISPLAY_MS,
    percentFromSse,
    errorMessage,
    extractSseErrorMessage,
    extractSseErrorCode,
    isModelUnavailableError,
    noticeForResultError,
    getRelevantSystemMemoryGB,
    configString,
    isStreamInterruptedError,
    streamInterruptedMessage,
} from "../utils";
import { SessionsModal } from "./sessions-modal";
import { chunkTypeFromScope, deriveSessionTitle, scopeFromChunkType } from "../utils/session";
import { SetupWizard } from "./setup-wizard";
import { revealPlacementBeside } from "./placement-view";
import { hostedOptions, isUsableHostedRow } from "./catalog-helpers";
import { electronDialog } from "../utils/file-dialog";
 
export const VIEW_TYPE_CHAT = "lilbee-chat";
 
/** Within this distance of the bottom the view counts as pinned and follows the stream. */
const SCROLL_FOLLOW_THRESHOLD_PX = 80;
 
/** Fold glyph on the compaction boundary; the warning glyph when turns were dropped outright. */
const COMPACTION_ICON = "chevrons-down-up";
const COMPACTION_LOSSY_ICON = "alert-triangle";
 
/** Sentinel option value: selecting it opens the catalog instead of switching models. */
const RAIL_BROWSE_KEY = "__lilbee_browse__";
/** Option value that turns an optional role off (matches the server's empty model ref). */
const RAIL_DISABLED_KEY = "";
 
/** One pickable entry in a rail role's menu. */
interface RailOption {
    value: string;
    label: string;
    checked: boolean;
}
 
/** Label shown on a chip trigger: the checked option, else the first (matching a native select). */
function railTriggerLabel(options: RailOption[]): string {
    return (options.find((o) => o.checked) ?? options[0])?.label ?? "";
}
 
type OptionalRoleTask = typeof MODEL_TASK.VISION | typeof MODEL_TASK.RERANK;
 
/** Static description of an optional rail role (Vision, Rerank). Dynamic state lives on the view. */
interface OptionalRoleSpec {
    task: OptionalRoleTask;
    label: string;
    dotClass: string;
    triggerClass: string;
    disabledLabel: string;
    tooltip: string;
    configKey: string;
    setActive: (api: LilbeePlugin["api"], model: string) => Promise<{ isErr(): boolean }>;
    failNotice: string;
}
 
/** Per-message streaming state: accumulated text and the live reasoning DOM. */
interface StreamState {
    fullContent: string;
    reasoningContent: string;
    sources: Source[];
    renderPending: boolean;
    reasoningRenderPending: boolean;
    reasoningContentEl: HTMLElement | null;
    reasoningDetailsEl: HTMLElement | null;
    answerStarted: boolean;
    /** Set at DONE/stop/error so a queued animation-frame plain-text repaint
     *  can't overwrite the final markdown render. */
    streamEnded: boolean;
    /** The turn's user bubble; compaction markers are inserted above it. */
    anchorEl: HTMLElement;
    /** The condensing card, held so progress can advance it and the outcome can settle it. */
    compaction: CompactionMarker | null;
    /** The thinking-dots container; a warming label lands here and leaves with it. */
    spinnerEl: HTMLElement;
    /** Set once the server's `warming` event has been surfaced, so it renders once. */
    warmingShown: boolean;
}
 
const OPTIONAL_ROLE_SPECS: OptionalRoleSpec[] = [
    {
        task: MODEL_TASK.VISION,
        label: MESSAGES.RAIL_LABEL_VISION,
        dotClass: "is-vision",
        triggerClass: "lilbee-vision-model-select",
        disabledLabel: MESSAGES.LABEL_VISION_DISABLED,
        tooltip: MESSAGES.TOOLTIP_ROLE_VISION,
        configKey: "vision_model",
        setActive: (api, model) => api.setVisionModel(model),
        failNotice: MESSAGES.RAIL_LABEL_VISION,
    },
    {
        task: MODEL_TASK.RERANK,
        label: MESSAGES.RAIL_LABEL_RERANK,
        dotClass: "is-rerank",
        triggerClass: "lilbee-rerank-model-select",
        disabledLabel: MESSAGES.LABEL_RERANKER_DISABLED,
        tooltip: MESSAGES.TOOLTIP_ROLE_RERANK,
        configKey: "reranker_model",
        setActive: (api, model) => api.setRerankerModel(model),
        failNotice: MESSAGES.RAIL_LABEL_RERANK,
    },
];
 
function extractString(data: unknown, field: string): string {
    if (typeof data === "object" && data !== null && field in data) {
        return String((data as Record<string, unknown>)[field]);
    }
    return String(data);
}
 
export function extractBanner(data: unknown): string | null {
    if (data === null || typeof data !== "object") return null;
    const banner = (data as Record<string, unknown>).banner;
    return typeof banner === "string" && banner.length > 0 ? banner : null;
}
 
/** Strip the markdown markers that would otherwise show as raw syntax while text
 *  streams as plain text (bold `**`, code backticks). The full markdown — bold,
 *  code blocks, the lot — is rendered once when the message completes. */
export function plainStream(md: string): string {
    return md.replace(/\*\*/g, "").replace(/`/g, "");
}
 
/** The condensing card's live parts: a title that counts batches and a progress fill. */
interface CompactionMarker {
    root: HTMLElement;
    title: HTMLElement;
    fill: HTMLElement;
}
 
/** Boundary wording for a compaction: what was condensed, and what was dropped outright. */
export function compactionMarkerText(data: CompactionEventData): string {
    if (data.condensed > 0 && data.stranded > 0) return MESSAGES.CHAT_COMPACTED_PARTIAL(data.condensed, data.stranded);
    if (data.stranded > 0) return MESSAGES.CHAT_STRANDED(data.stranded);
    return MESSAGES.CHAT_COMPACTED(data.condensed);
}
 
export class ChatView extends ItemView {
    private plugin: LilbeePlugin;
    private history: Message[] = [];
    /** Server-side conversation this view appends to. Null until the first turn opens one. */
    private sessionId: string | null = null;
    /** Carry-forward compaction notes; sent with each turn and replaced by `compaction` events. */
    private summary = "";
    /** Bumped when the transcript is replaced or cleared; stale queued writes check it and no-op. */
    private conversationEpoch = 0;
    /** Serializes session writes: the log is append-only, so turns must land in order. */
    private persistQueue: Promise<void> = Promise.resolve();
    /** The send in progress, so a resume can let it finish unwinding before replacing the transcript. */
    private inFlightSend: Promise<void> | null = null;
    private messagesEl: HTMLElement | null = null;
    private sendBtn: HTMLButtonElement | null = null;
    private textareaEl: HTMLTextAreaElement | null = null;
    private sending = false;
    private streamController: AbortController | null = null;
    private pullController: AbortController | null = null;
    private chatCatalogEntries: CatalogEntry[] = [];
    private chatInstalled: InstalledModel[] = [];
    private chatActive = "";
    private chatTriggerTextEl: HTMLElement | null = null;
    private embeddingTriggerTextEl: HTMLElement | null = null;
    private embeddingModels: CatalogEntry[] = [];
    private activeEmbeddingModel = "";
    private chatModeContainer: HTMLElement | null = null;
    private chatModeCurrent: ChatMode | null = null;
    /** Search-scope toggle buttons by scope. Empty while the wiki feature is off — they aren't rendered then. */
    private searchModeButtons = new Map<SearchChunkType, HTMLElement>();
    // Optional model roles (Vision, Rerank) surfaced in the rail. Keyed by the
    // spec's task; data refreshed alongside the chat/embed selectors. Options
    // come from the per-task catalog (so only role-capable models show), exactly
    // like the Settings model manager.
    private optionalRailEl: HTMLElement | null = null;
    private optionalCatalog: Record<OptionalRoleTask, CatalogEntry[]> = { vision: [], rerank: [] };
    private optionalActive: Record<OptionalRoleTask, string> = { vision: "", rerank: "" };
    private static readonly OFFLINE_THRESHOLD = 3;
    private retryTimer: number | null = null;
    private retryCount = 0;
    private emptyStateEl: HTMLElement | null = null;
 
    constructor(leaf: WorkspaceLeaf, plugin: LilbeePlugin) {
        super(leaf);
        this.plugin = plugin;
    }
 
    getViewType(): string {
        return VIEW_TYPE_CHAT;
    }
 
    getDisplayText(): string {
        return MESSAGES.LABEL_CHAT_VIEW;
    }
 
    getIcon(): string {
        return "message-circle";
    }
 
    async onOpen(): Promise<void> {
        const container = this.containerEl.children[1] as HTMLElement;
        container.empty();
        container.addClass("lilbee-chat-container");
 
        this.createToolbar(container);
        this.messagesEl = container.createDiv({ cls: "lilbee-chat-messages" });
        this.createInputArea(container);
    }
 
    async onClose(): Promise<void> {
        this.streamController?.abort();
        this.pullController?.abort();
        if (this.retryTimer) {
            window.clearTimeout(this.retryTimer);
            this.retryTimer = null;
        }
        this.retryCount = 0;
    }
 
    private createToolbar(container: HTMLElement): void {
        const toolbar = container.createDiv({ cls: "lilbee-chat-toolbar" });
 
        // Model rail (line 1): the four roles as content-sized chips that wrap
        // when the panel is narrow. Each chip is a colored dot (filled = on,
        // hollow = off) + a clear label + the model picker. All four read the
        // same way — bb-72pq parity, no role is icon-only.
        const rail = toolbar.createDiv({ cls: "lilbee-model-rail" });
 
        const chatChip = rail.createDiv({ cls: "lilbee-model-chip lilbee-toolbar-group" });
        chatChip.setAttribute("aria-label", MESSAGES.TOOLTIP_ROLE_CHAT);
        chatChip.setAttribute("aria-label-position", "top");
        chatChip.createSpan({ cls: "lilbee-model-chip-dot is-chat is-active" });
        chatChip.createSpan({ cls: "lilbee-model-chip-label", text: MESSAGES.RAIL_LABEL_CHAT });
        this.chatTriggerTextEl = this.createRailTrigger(chatChip, "lilbee-chat-model-select", (event) =>
            this.openChatMenu(event),
        );
 
        const embedChip = rail.createDiv({ cls: "lilbee-model-chip lilbee-toolbar-group lilbee-toolbar-group-embed" });
        embedChip.setAttribute("aria-label", MESSAGES.TOOLTIP_ROLE_EMBED);
        embedChip.setAttribute("aria-label-position", "top");
        embedChip.createSpan({ cls: "lilbee-model-chip-dot is-embed is-active" });
        embedChip.createSpan({ cls: "lilbee-model-chip-label", text: MESSAGES.RAIL_LABEL_EMBED });
        this.embeddingTriggerTextEl = this.createRailTrigger(embedChip, "lilbee-embed-model-select", (event) =>
            this.openEmbeddingMenu(event),
        );
 
        // Optional roles (Vision, Rerank) are chips in the same wrapping rail.
        this.optionalRailEl = rail.createDiv({ cls: "lilbee-model-rail-optional" });
        this.fillOptionalRoles();
 
        // "Browse more" sits at the far right of the rail and opens the full
        // model catalog (all roles) — not tied to any one chip.
        const railBrowseBtn = rail.createEl("button", {
            text: MESSAGES.BUTTON_BROWSE_MORE,
            cls: "lilbee-embed-browse lilbee-rail-browse",
        });
        railBrowseBtn.setAttribute("aria-label", MESSAGES.BUTTON_BROWSE_MORE);
        railBrowseBtn.addEventListener("click", () => {
            new CatalogModal(this.app, this.plugin).open();
        });
 
        // Controls (line 2): mode toggles on the left, save/clear on the right.
        const actions = toolbar.createDiv({ cls: "lilbee-chat-toolbar-actions" });
 
        this.chatModeContainer = actions.createDiv({ cls: "lilbee-chat-mode-container" });
 
        this.fetchAndFillSelectors();
 
        // Search mode toggle (only shown when wiki feature is enabled)
        const wikiEnabled = this.plugin.settings.wikiEnabled;
        // With the toggle hidden, any narrowed scope is a scope the user cannot see or undo.
        if (!wikiEnabled && this.plugin.settings.searchChunkType !== SEARCH_CHUNK_TYPE.ALL) {
            this.plugin.settings.searchChunkType = SEARCH_CHUNK_TYPE.ALL;
        }
        if (wikiEnabled) {
            const modeGroup = actions.createDiv({ cls: "lilbee-search-mode" });
            const modes: { value: SearchChunkType; label: string }[] = [
                { value: SEARCH_CHUNK_TYPE.ALL, label: MESSAGES.LABEL_SEARCH_ALL },
                { value: SEARCH_CHUNK_TYPE.WIKI, label: MESSAGES.LABEL_SEARCH_WIKI },
                { value: SEARCH_CHUNK_TYPE.RAW, label: MESSAGES.LABEL_SEARCH_RAW },
            ];
            for (const mode of modes) {
                const btn = modeGroup.createEl("button", { text: mode.label, cls: "lilbee-search-mode-btn" });
                this.searchModeButtons.set(mode.value, btn);
                btn.addEventListener("click", () => {
                    this.plugin.settings.searchChunkType = mode.value;
                    void this.plugin.saveSettings();
                    this.syncSearchModeButtons(mode.value);
                });
            }
            this.syncSearchModeButtons(this.plugin.settings.searchChunkType);
        }
 
        actions.createDiv({ cls: "lilbee-toolbar-spacer" });
 
        const gpuBtn = actions.createEl("button", { cls: "lilbee-chat-gpu" });
        setIcon(gpuBtn, "cpu");
        gpuBtn.setAttribute("aria-label", MESSAGES.LABEL_OPEN_GPU_ACTIVITY);
        gpuBtn.addEventListener("click", () => void revealPlacementBeside(this.app, this.leaf));
 
        // Saved conversations are hidden outright on servers without the routes.
        if (this.plugin.serverSupportsSessions()) {
            const sessionsBtn = actions.createEl("button", { cls: "lilbee-chat-sessions" });
            setIcon(sessionsBtn, "history");
            sessionsBtn.setAttribute("aria-label", MESSAGES.LABEL_OPEN_SESSIONS);
            sessionsBtn.addEventListener("click", () => this.openSessions());
        }
 
        const saveBtn = actions.createEl("button", { cls: "lilbee-chat-save" });
        setIcon(saveBtn, "save");
        saveBtn.setAttribute("aria-label", MESSAGES.LABEL_SAVE_VAULT);
        saveBtn.addEventListener("click", () => void this.saveToVault());
 
        const clearBtn = actions.createEl("button", { cls: "lilbee-chat-clear" });
        setIcon(clearBtn, "eraser");
        clearBtn.setAttribute("aria-label", MESSAGES.BUTTON_CLEAR_CHAT);
        clearBtn.addEventListener("click", () => this.clearChat());
        addCloseButton(actions, this.leaf, MESSAGES.LABEL_CLOSE_VIEW(MESSAGES.LABEL_CHAT_VIEW));
    }
 
    private createInputArea(container: HTMLElement): void {
        const inputArea = container.createDiv({ cls: "lilbee-chat-input" });
 
        const addBtn = inputArea.createEl("button", { cls: "lilbee-chat-add-file" });
        addBtn.setAttribute("aria-label", MESSAGES.LABEL_ADD_FILE);
        setIcon(addBtn, "paperclip");
        addBtn.addEventListener("click", (e) => this.openFilePicker(e));
 
        const textarea = inputArea.createEl("textarea", {
            placeholder: MESSAGES.PLACEHOLDER_ASK_SOMETHING,
            cls: "lilbee-chat-textarea",
        });
        this.textareaEl = textarea;
        this.sendBtn = inputArea.createEl("button", {
            text: MESSAGES.BUTTON_SEND,
            cls: "lilbee-chat-send",
        });
 
        const handleSend = (): void => {
            // Defensively wrap in try/catch — exceptions in the click/keydown
            // path used to bubble out silently because Obsidian swallows them
            // at the event-listener boundary, leaving the user staring at a
            // chat with their question echoed back but no fetch fired.
            try {
                if (this.sending) return;
                const text = textarea.value.trim();
                if (!text) return;
                // sendMessage clears the box once its guards pass: a refused send
                // (engine still warming, another turn in flight) must not eat the
                // question the user typed.
                this.inFlightSend = this.sendMessage(text);
            } catch (err) {
                const reason = errorMessage(err, MESSAGES.ERROR_UNKNOWN, this.plugin.settings.serverMode);
                new Notice(MESSAGES.ERROR_CHAT_FAILED(reason));
            }
        };
 
        this.sendBtn.addEventListener("click", () => {
            if (this.sending) {
                this.streamController?.abort();
            } else {
                handleSend();
            }
        });
        textarea.addEventListener("keydown", (e) => {
            if (e.key === "Enter" && !e.shiftKey) {
                e.preventDefault();
                handleSend();
            }
        });
    }
 
    private fetchAndFillSelectors(): void {
        Promise.all([
            this.plugin.api.catalog({ task: MODEL_TASK.CHAT }),
            this.plugin.api.installedModels({ task: MODEL_TASK.CHAT }).catch(() => ({ models: [] })),
            this.plugin.api.catalog({ task: MODEL_TASK.EMBEDDING }).catch(() => null),
            this.plugin.api.config().catch(() => null),
            this.plugin.api.catalog({ task: MODEL_TASK.VISION }).catch(() => null),
            this.plugin.api.catalog({ task: MODEL_TASK.RERANK }).catch(() => null),
        ])
            .then(([chatCatalogResult, chatInstalled, embeddingResult, serverConfig, visionCatalog, rerankCatalog]) => {
                if (this.retryTimer) {
                    window.clearTimeout(this.retryTimer);
                    this.retryTimer = null;
                }
                this.retryCount = 0;
                this.chatCatalogEntries = chatCatalogResult.isOk() ? chatCatalogResult.value.models : [];
                this.chatInstalled = chatInstalled.models;
                this.chatActive = serverConfig ? configString(serverConfig, "chat_model") : "";
                this.chatTriggerTextEl?.setText(railTriggerLabel(this.chatOptionGroups().flat()));
 
                this.fillEmbeddingSelector(embeddingResult, serverConfig);
                this.fillOptionalRoleData(
                    visionCatalog && visionCatalog.isOk() ? visionCatalog.value.models : [],
                    rerankCatalog && rerankCatalog.isOk() ? rerankCatalog.value.models : [],
                    serverConfig,
                );
                this.renderChatModeToggle(serverConfig);
 
                if (this.chatInstalled.length === 0) {
                    this.showEmptyState();
                    this.retryTimer = window.setTimeout(() => this.fetchAndFillSelectors(), RETRY_INTERVAL_MS);
                } else {
                    this.hideEmptyState();
                }
            })
            .catch(() => {
                this.retryCount++;
                const connecting = this.retryCount < ChatView.OFFLINE_THRESHOLD;
                const label = connecting ? MESSAGES.LABEL_CONNECTING : MESSAGES.LABEL_OFFLINE;
                // Clear option state so the menus offer nothing while unreachable.
                this.chatCatalogEntries = [];
                this.chatInstalled = [];
                this.embeddingModels = [];
                this.activeEmbeddingModel = "";
                this.chatTriggerTextEl?.setText(label);
                this.embeddingTriggerTextEl?.setText(label);
                if (this.retryCount === ChatView.OFFLINE_THRESHOLD) {
                    new Notice(MESSAGES.ERROR_SERVER_UNREACHABLE);
                }
                this.retryTimer = window.setTimeout(() => this.fetchAndFillSelectors(), RETRY_INTERVAL_MS);
            });
    }
 
    private renderChatModeToggle(serverConfig: Record<string, unknown> | null): void {
        /* v8 ignore next 2 */
        if (!this.chatModeContainer) return;
        this.chatModeContainer.empty();
        const rawMode = serverConfig?.chat_mode;
        if (rawMode !== CHAT_MODE.SEARCH && rawMode !== CHAT_MODE.CHAT) return;
        this.chatModeCurrent = rawMode;
        const embeddingModel = typeof serverConfig?.embedding_model === "string" ? serverConfig.embedding_model : "";
        const noEmbedding = embeddingModel === "";
 
        const segments: { mode: ChatMode; label: string; tooltip: string }[] = [
            { mode: CHAT_MODE.SEARCH, label: MESSAGES.LABEL_CHAT_MODE_SEARCH, tooltip: MESSAGES.TOOLTIP_MODE_SEARCH },
            { mode: CHAT_MODE.CHAT, label: MESSAGES.LABEL_CHAT_MODE_CHAT, tooltip: MESSAGES.TOOLTIP_MODE_CHAT },
        ];
        for (const seg of segments) {
            const btn = this.chatModeContainer.createEl("button", {
                text: seg.label,
                cls: `lilbee-chat-mode-btn${seg.mode === rawMode ? " active" : ""}`,
            });
            btn.setAttribute("aria-label", seg.tooltip);
            btn.setAttribute("aria-label-position", "top");
            if (noEmbedding) {
                btn.disabled = true;
                btn.setAttribute("title", MESSAGES.TOOLTIP_CHAT_MODE_NEEDS_EMBEDDING);
                btn.addClass("lilbee-disabled");
            }
            btn.addEventListener("click", () => {
                if (noEmbedding || seg.mode === this.chatModeCurrent) return;
                void this.persistChatMode(seg.mode);
            });
        }
    }
 
    private async persistChatMode(mode: ChatMode): Promise<void> {
        try {
            await this.plugin.api.updateConfig({ [CONFIG_KEY.CHAT_MODE]: mode });
            this.chatModeCurrent = mode;
            this.applyActiveClassToChatModeButtons(mode);
        } catch (err) {
            const reason = errorMessage(err, MESSAGES.ERROR_UNKNOWN, this.plugin.settings.serverMode);
            new Notice(MESSAGES.NOTICE_FAILED_UPDATE(`${MESSAGES.LABEL_CHAT_MODE}: ${reason}`));
        }
    }
 
    private applyActiveClassToChatModeButtons(mode: ChatMode): void {
        /* v8 ignore next 2 */
        if (!this.chatModeContainer) return;
        const buttons = this.chatModeContainer.querySelectorAll(".lilbee-chat-mode-btn");
        const activeIdx = mode === CHAT_MODE.SEARCH ? 0 : 1;
        for (let i = 0; i < buttons.length; i++) {
            if (i === activeIdx) buttons[i].addClass("active");
            else buttons[i].removeClass("active");
        }
    }
 
    private fillEmbeddingSelector(
        embeddingResult: import("../result").Result<import("../types").CatalogResponse, Error> | null,
        serverConfig: Record<string, unknown> | null,
    ): void {
        this.activeEmbeddingModel = serverConfig ? configString(serverConfig, "embedding_model") : "";
        this.embeddingModels =
            embeddingResult && embeddingResult.isOk() ? embeddingResult.value.models.filter((m) => m.installed) : [];
        this.embeddingTriggerTextEl?.setText(railTriggerLabel(this.embeddingOptions()));
    }
 
    /** Embedding menu entries: installed builds, or the bare active ref when none are installed. */
    private embeddingOptions(): RailOption[] {
        const activeRepo = extractHfRepo(this.activeEmbeddingModel);
        const options = this.embeddingModels.map((m) => ({
            value: m.hf_repo,
            label: m.display_name,
            checked: m.hf_repo === activeRepo,
        }));
        if (options.length === 0 && this.activeEmbeddingModel) {
            options.push({
                value: this.activeEmbeddingModel,
                label: displayLabelForRef(this.activeEmbeddingModel),
                checked: true,
            });
        }
        return options;
    }
 
    /** Chat menu entries: [featured installed + hosted rows, other installed builds]. */
    private chatOptionGroups(): RailOption[][] {
        return [this.chatPrimaryOptions(), this.chatOtherOptions()];
    }
 
    /** Featured rows that have an installed quant, then hosted rows not already listed. */
    private chatPrimaryOptions(): RailOption[] {
        const installedRepos = new Set(this.chatInstalled.map((m) => extractHfRepo(m.name)));
        const activeRepo = extractHfRepo(this.chatActive);
        const primary: RailOption[] = [];
        for (const entry of this.chatCatalogEntries.filter((e) => installedRepos.has(e.hf_repo))) {
            const sourceTag = HOSTED_SOURCES.has(entry.source) ? ` [${entry.provider ?? entry.source}]` : "";
            primary.push({
                value: entry.hf_repo,
                label: `${entry.display_name}${sourceTag}`,
                checked: entry.hf_repo === activeRepo,
            });
        }
        // Hosted rows (frontier/ollama) are selectable even when absent from the
        // installed registry — ollama always, frontier with a ready key. Skip any
        // already emitted above as an installed featured row (an ollama model can
        // be both hosted and registered as installed).
        for (const [ref, label] of hostedOptions(this.chatCatalogEntries)) {
            if (installedRepos.has(ref)) continue;
            primary.push({ value: ref, label, checked: ref === activeRepo });
        }
        return primary;
    }
 
    /** Installed builds that aren't in the featured catalog (manually pulled, ollama/, openai/, …). */
    private chatOtherOptions(): RailOption[] {
        const sourceMap = new Map(this.chatInstalled.map((m) => [m.name, m.source]));
        const featuredRepos = new Set(this.chatCatalogEntries.map((e) => e.hf_repo));
        return this.chatInstalled
            .filter((m) => !featuredRepos.has(extractHfRepo(m.name)))
            .sort((a, b) => a.name.localeCompare(b.name))
            .map((m) => {
                const source = sourceMap.get(m.name);
                const suffix = source && source !== CATALOG_SOURCE.NATIVE ? ` [${source}]` : "";
                return {
                    value: m.name,
                    label: `${displayLabelForRef(m.name)}${suffix}`,
                    checked: m.name === this.chatActive,
                };
            });
    }
 
    private openChatMenu(event: MouseEvent): void {
        this.openRailMenu(event, this.chatTriggerTextEl, this.chatOptionGroups(), (value) =>
            this.handleChatSelection(value),
        );
    }
 
    private handleChatSelection(value: string): void {
        const uninstalled = this.chatCatalogEntries.find((e) => e.hf_repo === value && !e.installed);
        if (uninstalled) {
            const modal = new ConfirmPullModal(this.plugin.app, {
                displayName: uninstalled.display_name,
                sizeGb: uninstalled.size_gb,
                minRamGb: uninstalled.min_ram_gb,
                systemMemGb: getRelevantSystemMemoryGB(this.plugin.settings.serverMode),
            });
            modal.open();
            void modal.result.then((confirmed) => {
                if (confirmed) {
                    void this.autoPullAndSet(uninstalled);
                }
            });
            return;
        }
        this.applyChatModel(value);
    }
 
    /** Point the server at an already-installed chat model. */
    private applyChatModel(value: string): void {
        void this.plugin.api.setChatModel(value).then((result) => {
            if (result.isOk()) {
                // Keep the menu's checkmark in sync without waiting for a refetch.
                this.chatActive = value;
                this.plugin.activeModel = value;
                void this.plugin.fetchActiveModel();
                this.plugin.refreshSettingsTab();
            } else {
                new Notice(MESSAGES.ERROR_SWITCH_MODEL);
            }
        });
    }
 
    private openEmbeddingMenu(event: MouseEvent): void {
        this.openRailMenu(event, this.embeddingTriggerTextEl, [this.embeddingOptions()], (value) =>
            this.handleEmbeddingSelection(value),
        );
    }
 
    private handleEmbeddingSelection(value: string): void {
        const previous = this.activeEmbeddingModel;
        const modal = new ConfirmModal(this.plugin.app, MESSAGES.DESC_EMBEDDING_REINDEX_WARNING);
        modal.open();
        void modal.result.then(async (confirmed) => {
            if (!confirmed) {
                this.revertEmbeddingTrigger(previous);
                return;
            }
            const result = await this.plugin.api.setEmbeddingModel(value);
            if (result.isErr()) {
                new Notice(noticeForResultError(result.error, MESSAGES.NOTICE_FAILED_EMBEDDING));
                this.revertEmbeddingTrigger(previous);
                return;
            }
            this.activeEmbeddingModel = value;
            new Notice(MESSAGES.NOTICE_EMBEDDING_UPDATED);
            new Notice(MESSAGES.NOTICE_REINDEX_REQUIRED);
            this.plugin.refreshSettingsTab();
            void this.plugin.triggerSync();
        });
    }
 
    /** Chip trigger button: current model text + caret; click opens the role's menu. */
    private createRailTrigger(
        chip: HTMLElement,
        cls: string,
        onOpen: (event: MouseEvent, textEl: HTMLElement) => void,
    ): HTMLElement {
        const trigger = chip.createEl("button", { cls: `lilbee-model-chip-select ${cls}` });
        const text = trigger.createSpan({ cls: "lilbee-model-chip-select-text" });
        setIcon(trigger.createSpan({ cls: "lilbee-model-chip-select-caret" }), "chevron-down");
        trigger.addEventListener("click", (event) => onOpen(event, text));
        return text;
    }
 
    /** In-window menu of rail options; the checked entry is the active one. */
    private openRailMenu(
        event: MouseEvent,
        textEl: HTMLElement | null,
        groups: RailOption[][],
        onPick: (value: string) => void,
    ): void {
        const nonEmpty = groups.filter((group) => group.length > 0);
        if (nonEmpty.length === 0) return;
        const menu = new Menu();
        nonEmpty.forEach((group, index) => {
            if (index > 0) menu.addSeparator();
            for (const opt of group) {
                menu.addItem((item) => {
                    item.setTitle(opt.label)
                        .setChecked(opt.checked)
                        .onClick(() => {
                            // Re-picking the active item is a no-op (matches a native select's change event).
                            if (opt.checked) return;
                            // Browse opens the catalog; the chip keeps showing the current model.
                            if (opt.value !== RAIL_BROWSE_KEY) textEl?.setText(opt.label);
                            onPick(opt.value);
                        });
                });
            }
        });
        this.showMenu(menu, event);
    }
 
    /** Store the latest per-task catalog + active model for the optional roles, then re-render. */
    private fillOptionalRoleData(
        visionCatalog: CatalogEntry[],
        rerankCatalog: CatalogEntry[],
        serverConfig: Record<string, unknown> | null,
    ): void {
        this.optionalCatalog.vision = visionCatalog;
        this.optionalCatalog.rerank = rerankCatalog;
        this.optionalActive.vision = serverConfig ? configString(serverConfig, "vision_model") : "";
        this.optionalActive.rerank = serverConfig ? configString(serverConfig, "reranker_model") : "";
        this.fillOptionalRoles();
    }
 
    /** Rebuild the optional (Vision, Rerank) rail rows from current catalog/active state. */
    private fillOptionalRoles(): void {
        /* v8 ignore next 2 */
        if (!this.optionalRailEl) return;
        this.optionalRailEl.empty();
        for (const spec of OPTIONAL_ROLE_SPECS) {
            this.renderOptionalRoleRow(this.optionalRailEl, spec);
        }
    }
 
    /**
     * Selectable models for a role: local installed builds + hosted (LiteLLM)
     * entries — mirroring the Settings model manager so only role-capable
     * models appear. Un-installed catalog builds are reached via "Browse catalog".
     */
    private optionalRoleOptions(spec: OptionalRoleSpec): { value: string; label: string }[] {
        const entries = this.optionalCatalog[spec.task];
        const localInstalled = entries.filter((e) => !HOSTED_SOURCES.has(e.source) && e.installed);
        const hosted = entries.filter(isUsableHostedRow);
        const options = localInstalled.map((e) => ({ value: e.hf_repo, label: e.display_name }));
        for (const e of hosted) {
            options.push({ value: e.hf_repo, label: `${e.display_name} — ${MESSAGES.LABEL_VISION_HOSTED_GROUP}` });
        }
        return options;
    }
 
    /** Full menu entry list for an optional role: Disabled, role-capable models, Browse catalog. */
    private optionalRoleMenuOptions(spec: OptionalRoleSpec): RailOption[] {
        const active = this.optionalActive[spec.task];
        const activeRepo = extractHfRepo(active);
        // "Disabled" turns the role off (model ref ""); the role stays visible
        // even with nothing installed, and "Browse catalog" downloads one.
        const options: RailOption[] = [{ value: RAIL_DISABLED_KEY, label: spec.disabledLabel, checked: !active }];
        for (const opt of this.optionalRoleOptions(spec)) {
            options.push({ ...opt, checked: opt.value === active || opt.value === activeRepo });
        }
        options.push({ value: RAIL_BROWSE_KEY, label: MESSAGES.RAIL_BROWSE_CATALOG, checked: false });
        return options;
    }
 
    private renderOptionalRoleRow(rail: HTMLElement, spec: OptionalRoleSpec): void {
        const active = this.optionalActive[spec.task];
        const chip = rail.createDiv({ cls: "lilbee-model-chip lilbee-model-chip-optional" });
        chip.setAttribute("aria-label", spec.tooltip);
        chip.setAttribute("aria-label-position", "top");
        if (!active) chip.addClass("is-off");
        const dot = chip.createSpan({ cls: `lilbee-model-chip-dot ${spec.dotClass}` });
        if (active) dot.addClass("is-active");
        chip.createSpan({ cls: "lilbee-model-chip-label", text: spec.label });
 
        const textEl = this.createRailTrigger(chip, spec.triggerClass, (event, text) =>
            this.openRailMenu(event, text, [this.optionalRoleMenuOptions(spec)], (value) =>
                this.handleOptionalRoleSelection(spec, value),
            ),
        );
        textEl.setText(railTriggerLabel(this.optionalRoleMenuOptions(spec)));
    }
 
    private handleOptionalRoleSelection(spec: OptionalRoleSpec, value: string): void {
        if (value === RAIL_BROWSE_KEY) {
            new CatalogModal(this.app, this.plugin, spec.task).open();
            return;
        }
        // RAIL_DISABLED_KEY disables the role; any other value activates it.
        void spec.setActive(this.plugin.api, value).then((result) => {
            if (result.isErr()) {
                new Notice(MESSAGES.NOTICE_FAILED_UPDATE(spec.failNotice));
                return;
            }
            this.optionalActive[spec.task] = value;
            this.fillOptionalRoles();
            void this.plugin.fetchActiveModel();
            this.plugin.refreshSettingsTab();
        });
    }
 
    private revertEmbeddingTrigger(previousValue: string): void {
        if (!this.embeddingTriggerTextEl) return;
        // Restore the previous label only when that value still exists in the
        // option set; otherwise fall back to a server refresh.
        const option = this.embeddingOptions().find((opt) => opt.value === previousValue);
        if (option) {
            this.embeddingTriggerTextEl.setText(option.label);
            return;
        }
        void this.fetchAndFillSelectors();
    }
 
    private async autoPullAndSet(entry: CatalogEntry): Promise<void> {
        const taskId = this.plugin.enqueuePull(`Pull ${entry.display_name}`);
        if (taskId === null) {
            new Notice(MESSAGES.NOTICE_QUEUE_FULL);
            return;
        }
        this.pullController = new AbortController();
        this.plugin.taskQueue.registerAbort(taskId, this.pullController);
        let pullFailed = false;
        try {
            for await (const event of this.plugin.api.pullModel(entry.hf_repo, "native", this.pullController.signal)) {
                if (event.event === SSE_EVENT.PROGRESS) {
                    const d = event.data as { percent?: number; current?: number; total?: number };
                    const pct = percentFromSse(d);
                    if (pct !== undefined) {
                        this.plugin.taskQueue.update(taskId, pct, entry.display_name, {
                            current: d.current,
                            total: d.total,
                        });
                    }
                } else if (event.event === SSE_EVENT.ERROR) {
                    const d = event.data as { message?: string } | string;
                    const msg = extractSseErrorMessage(d, MESSAGES.ERROR_UNKNOWN);
                    new Notice(MESSAGES.ERROR_PULL_MODEL.replace("{model}", entry.display_name));
                    this.plugin.taskQueue.fail(taskId, msg);
                    pullFailed = true;
                    break;
                }
            }
        } catch (err) {
            if (err instanceof Error && err.name === ERROR_NAME.ABORT_ERROR) {
                new Notice(MESSAGES.NOTICE_PULL_CANCELLED);
                this.plugin.taskQueue.cancel(taskId);
            } else {
                const reason = errorMessage(err, MESSAGES.ERROR_UNKNOWN);
                new Notice(`${MESSAGES.ERROR_PULL_MODEL.replace("{model}", entry.display_name)}: ${reason}`);
                this.plugin.taskQueue.fail(taskId, reason);
            }
            this.pullController = null;
            return;
        }
        this.pullController = null;
 
        if (pullFailed) return;
 
        this.plugin.taskQueue.complete(taskId);
 
        const result = await this.plugin.api.setChatModel(entry.hf_repo);
        if (result.isErr()) {
            new Notice(
                noticeForResultError(result.error, MESSAGES.ERROR_SET_MODEL.replace("{model}", entry.display_name)),
            );
        } else {
            this.plugin.activeModel = entry.hf_repo;
            new Notice(MESSAGES.NOTICE_MODEL_ACTIVATED_FULL(entry.display_name));
            this.plugin.refreshSettingsTab();
        }
        void this.plugin.fetchActiveModel();
        this.fetchAndFillSelectors();
    }
 
    // Re-sync the rail pills with the server's active models, for callers
    // outside this view (e.g. the catalog) that switch a model.
    refreshRail(): void {
        this.fetchAndFillSelectors();
    }
 
    private clearChat(): void {
        this.history = [];
        this.sessionId = null;
        this.summary = "";
        this.conversationEpoch++;
        if (this.messagesEl) this.messagesEl.empty();
    }
 
    private openSessions(): void {
        new SessionsModal(this.app, this.plugin, {
            activeId: this.sessionId,
            resume: (id) => void this.resumeSession(id),
            startNew: () => this.startNewConversation(),
        }).open();
    }
 
    /** Drop the transcript and unbind the session. The old one is already persisted. */
    private startNewConversation(): void {
        this.clearChat();
        new Notice(MESSAGES.NOTICE_SESSION_NEW);
    }
 
    /** Open the session lazily, on the first turn, so an idle view creates nothing. Returns its id. */
    private async ensureSession(firstText: string): Promise<string> {
        if (this.sessionId) return this.sessionId;
        const epoch = this.conversationEpoch;
        const scope = scopeFromChunkType(this.plugin.settings.searchChunkType);
        const created = await this.plugin.api.createSession(this.chatActive, scope);
        // A resume or clear that raced the create wins; this conversation stays unbound from the view.
        if (epoch === this.conversationEpoch) this.sessionId = created.meta.id;
        // The server auto-titles only TUI sessions; HTTP surfaces title their own via rename.
        try {
            await this.plugin.api.renameSession(created.meta.id, deriveSessionTitle(firstText));
        } catch {
            // A failed title write leaves the server's default; the transcript still persists.
        }
        return created.meta.id;
    }
 
    /** Queue a session write. Never awaited by the chat path: persistence must not stall the answer. */
    private queuePersist(write: () => Promise<void>): void {
        // Servers without the session routes keep the conversation in memory only.
        if (!this.plugin.serverSupportsSessions()) return;
        // Writes queued for one conversation must not touch the one open when they run.
        const epoch = this.conversationEpoch;
        this.persistQueue = this.persistQueue
            .then(() => (epoch === this.conversationEpoch ? write() : undefined))
            .catch((err) => {
                // Sessions switched off server-side (404) is permanent: unbind so the chat
                // goes on in memory. A transient failure (busy server, timeout) drops only
                // this write; a gap in the transcript beats splitting the conversation.
                if (epoch !== this.conversationEpoch) return;
                if (err instanceof Error && isHttpStatus(err, 404)) this.sessionId = null;
            });
    }
 
    private async persistTurn(
        sessionId: string | null,
        role: SessionRole,
        content: string,
        sources: string[] = [],
    ): Promise<void> {
        if (!sessionId) return;
        await this.plugin.api.appendSessionMessage(sessionId, role, content, sources);
    }
 
    private async resumeSession(id: string): Promise<void> {
        let detail: SessionDetail;
        try {
            detail = await this.plugin.api.getSession(id);
        } catch (err) {
            const reason = errorMessage(err, MESSAGES.ERROR_UNKNOWN, this.plugin.settings.serverMode);
            new Notice(MESSAGES.ERROR_SESSION_RESUME_FAILED(reason));
            return;
        }
        // Let an in-flight answer finish unwinding first: its abort handler appends to
        // `history`, which would otherwise land on top of the transcript we restore below.
        if (this.sending) {
            this.streamController?.abort();
            await this.inFlightSend;
        }
        this.clearChat();
        this.sessionId = detail.meta.id;
        this.summary = detail.summary;
        this.hideEmptyState();
 
        if (detail.summary) this.renderSummaryBoundary(detail.summary);
        for (const message of detail.messages) {
            this.renderRestoredMessage(message);
            this.history.push({ role: message.role, content: message.content });
        }
        this.restoreScope(detail.meta.scope);
        this.restoreModel(detail.meta.model_ref);
        if (this.messagesEl) this.messagesEl.scrollTop = this.messagesEl.scrollHeight;
        new Notice(MESSAGES.NOTICE_SESSION_RESUMED(detail.meta.title));
    }
 
    /** Never point chat at a model that isn't installed; keep the current one and say so. */
    private restoreModel(modelRef: string): void {
        if (!modelRef || modelRef === this.chatActive) return;
        if (this.chatInstalled.some((m) => m.name === modelRef)) {
            this.applyChatModel(modelRef);
            return;
        }
        new Notice(MESSAGES.NOTICE_SESSION_MODEL_UNAVAILABLE(modelRef, this.chatActive));
    }
 
    /** Wiki scope is unreachable with the wiki feature off, so a wiki-scoped session keeps the current one. */
    private restoreScope(scope: string): void {
        const chunkType = chunkTypeFromScope(scope);
        if (!chunkType || chunkType === this.plugin.settings.searchChunkType) return;
        if (chunkType === SEARCH_CHUNK_TYPE.WIKI && !this.plugin.settings.wikiEnabled) return;
        this.plugin.settings.searchChunkType = chunkType;
        void this.plugin.saveSettings();
        this.syncSearchModeButtons(chunkType);
    }
 
    /** Move the toggle's highlight to `active`, whether a click or a session resume changed it. */
    private syncSearchModeButtons(active: SearchChunkType): void {
        for (const [value, btn] of this.searchModeButtons) btn.toggleClass("active", value === active);
    }
 
    /** The card shown while the server condenses: what is happening, and how far along. */
    private openCompactionMarker(state: StreamState): void {
        if (!this.messagesEl || state.compaction) return;
        const root = this.messagesEl.createDiv({ cls: "lilbee-chat-compaction is-condensing" });
        root.setAttribute("title", MESSAGES.TOOLTIP_COMPACTION);
        const head = root.createDiv({ cls: "lilbee-chat-compaction-head" });
        setIcon(head.createSpan({ cls: "lilbee-chat-compaction-icon" }), COMPACTION_ICON);
        const title = head.createSpan({ text: MESSAGES.CHAT_COMPACTING });
        const bar = root.createDiv({ cls: "lilbee-progress-bar-container" });
        const fill = bar.createDiv({ cls: "lilbee-progress-bar lilbee-progress-indeterminate" });
        this.messagesEl.insertBefore(root, state.anchorEl);
        state.compaction = { root, title, fill };
    }
 
    /** Count batches in the label. The bar stays indeterminate: each batch is a
     *  model call with no knowable duration, and a fill that reads full while the
     *  work continues is both wrong and dead-looking. */
    private advanceCompactionMarker(state: StreamState, progress: CompactingEventData): void {
        const marker = state.compaction;
        if (!marker || progress.batch === undefined || progress.batches === undefined) return;
        if (progress.batches <= 1) return;
        marker.title.setText(MESSAGES.CHAT_COMPACTING_PROGRESS(progress.batch, progress.batches));
    }
 
    /** Collapse the working card into the quiet boundary the transcript keeps. */
    private settleCompactionMarker(state: StreamState, data: CompactionEventData): void {
        const marker = state.compaction;
        if (!marker) return;
        marker.root.empty();
        marker.root.removeClass("is-condensing");
        marker.root.addClass("is-done");
        if (data.stranded > 0) marker.root.addClass("is-lossy");
        const pill = marker.root.createSpan({ cls: "lilbee-chat-compaction-pill" });
        setIcon(
            pill.createSpan({ cls: "lilbee-chat-compaction-icon" }),
            data.stranded > 0 ? COMPACTION_LOSSY_ICON : COMPACTION_ICON,
        );
        pill.createSpan({ text: compactionMarkerText(data) });
    }
 
    private renderRestoredMessage(message: SessionMessageItem): void {
        if (!this.messagesEl) return;
        if (message.role === SESSION_ROLE.USER) {
            const bubble = this.messagesEl.createDiv({ cls: "lilbee-chat-message user" });
            bubble.createEl("p", { text: message.content });
            return;
        }
        const bubble = this.messagesEl.createDiv({ cls: "lilbee-chat-message assistant" });
        const textEl = bubble.createDiv({ cls: "lilbee-chat-content" });
        void this.renderMarkdown(textEl, message.content);
        if (message.sources.length > 0) this.renderRestoredSources(bubble, message.sources);
    }
 
    /** Persisted sources are bare paths, so restored chips link out without the live chunk detail. */
    private renderRestoredSources(container: HTMLElement, sources: string[]): void {
        const chipsEl = this.createSourcesBlock(container);
        for (const path of sources) {
            const chip = chipsEl.createSpan({ cls: "lilbee-source-chip" });
            chip.createSpan({ text: path, cls: "lilbee-source-chip-file" });
            chip.addEventListener("click", () => void this.app.workspace.openLinkText(path, ""));
        }
    }
 
    private renderSummaryBoundary(summary: string): void {
        if (!this.messagesEl) return;
        const el = this.messagesEl.createDiv({ cls: "lilbee-chat-summary" });
        el.createDiv({ cls: "lilbee-chat-summary-label", text: MESSAGES.LABEL_SESSION_SUMMARY });
        el.createDiv({ cls: "lilbee-chat-summary-body", text: summary });
    }
 
    private async sendMessage(text: string): Promise<void> {
        if (!this.messagesEl || this.sending) return;
        if (!this.plugin.assertFleetReady()) return;
        // Past the guards: the turn is happening, so the box can be emptied.
        if (this.textareaEl) this.textareaEl.value = "";
        this.sending = true;
        this.streamController = new AbortController();
        this.plugin.notifyChatStart();
        if (this.sendBtn) this.sendBtn.setText(MESSAGES.BUTTON_STOP);
        if (this.textareaEl) this.textareaEl.disabled = true;
 
        const userBubble = this.messagesEl.createDiv({ cls: "lilbee-chat-message user" });
        userBubble.createEl("p", { text });
        this.history.push({ role: "user", content: text });
        // Queued before the stream so the question is saved even if the answer never lands.
        this.queuePersist(async () => {
            const sessionId = await this.ensureSession(text);
            await this.persistTurn(sessionId, SESSION_ROLE.USER, text);
        });
 
        const assistantBubble = this.messagesEl.createDiv({ cls: "lilbee-chat-message assistant" });
        const spinner = assistantBubble.createDiv({ cls: "lilbee-thinking-dots" });
        spinner.createDiv({ cls: "lilbee-thinking-dot" });
        spinner.createDiv({ cls: "lilbee-thinking-dot" });
        spinner.createDiv({ cls: "lilbee-thinking-dot" });
        const textEl = assistantBubble.createDiv({ cls: "lilbee-chat-content" });
        textEl.hide();
        this.messagesEl.scrollTop = this.messagesEl.scrollHeight;
 
        const state: StreamState = {
            fullContent: "",
            reasoningContent: "",
            sources: [],
            renderPending: false,
            reasoningRenderPending: false,
            reasoningContentEl: null,
            reasoningDetailsEl: null,
            answerStarted: false,
            streamEnded: false,
            anchorEl: userBubble,
            compaction: null,
            spinnerEl: spinner,
            warmingShown: false,
        };
 
        const spinnerCreatedAt = Date.now();
        const revealContent = (): void => {
            const elapsed = Date.now() - spinnerCreatedAt;
            const delay = Math.max(0, SPINNER_MIN_DISPLAY_MS - elapsed);
            window.setTimeout(() => {
                // Revealing the hidden content grows the bubble; keep the view pinned.
                void this.renderFollowing(() => {
                    if (spinner.parentElement) spinner.remove();
                    textEl.show();
                });
            }, delay);
        };
 
        const scheduleRender = (): void => {
            if (state.renderPending) return;
            state.renderPending = true;
            window.requestAnimationFrame(() => {
                state.renderPending = false;
                // A frame queued before DONE can fire after the final markdown
                // render; painting it would wipe the formatting with plain text.
                if (state.streamEnded) return;
                // Stream as lightweight plain text (markdown markers stripped, so
                // no raw `**` shows): a synchronous setText that can't stall under
                // load, so the answer never freezes mid-stream. The DONE event
                // renders the full formatted markdown once.
                void this.renderFollowing(() => textEl.setText(plainStream(state.fullContent)));
            });
        };
 
        try {
            for await (const event of this.plugin.api.chatStream(
                text,
                this.history.slice(0, -1),
                this.plugin.settings.topK,
                this.streamController.signal,
                undefined,
                this.plugin.settings.searchChunkType,
                { summary: this.summary, sessionId: this.sessionId },
            )) {
                this.handleStreamEvent(event, textEl, assistantBubble, state, revealContent, scheduleRender);
            }
        } catch (err) {
            // Trust the signal over the error shape: an aborted fetch reaches
            // here as AbortError from some paths and TypeError("Failed to
            // fetch") from others, and the latter would otherwise render a
            // server-failure message for a deliberate Stop.
            if (
                this.streamController?.signal.aborted ||
                (err instanceof Error && err.name === ERROR_NAME.ABORT_ERROR)
            ) {
                revealContent();
                state.streamEnded = true;
                state.reasoningDetailsEl?.removeAttribute("open");
                if (state.fullContent) {
                    void this.renderMarkdown(textEl, `${state.fullContent}\n\n${MESSAGES.LABEL_STOPPED_MD}`);
                    this.history.push({ role: "assistant", content: state.fullContent });
                } else {
                    textEl.setText(MESSAGES.LABEL_STOPPED);
                }
            } else if (err instanceof RateLimitedError) {
                this.renderInlineError(assistantBubble, MESSAGES.ERROR_RATE_LIMITED(err.retryAfterSeconds));
            } else if (isStreamInterruptedError(err)) {
                this.renderInlineError(assistantBubble, streamInterruptedMessage(this.plugin.settings.serverMode));
            } else {
                this.renderInlineError(
                    assistantBubble,
                    MESSAGES.ERROR_CHAT_FAILED(
                        errorMessage(err, MESSAGES.ERROR_UNKNOWN, this.plugin.settings.serverMode),
                    ),
                );
            }
        } finally {
            state.streamEnded = true;
            this.sending = false;
            this.streamController = null;
            this.plugin.notifyChatEnd();
            if (this.sendBtn) {
                this.sendBtn.setText(MESSAGES.BUTTON_SEND);
            }
            if (this.textareaEl) this.textareaEl.disabled = false;
        }
    }
 
    private handleStreamEvent(
        event: SSEEvent,
        textEl: HTMLElement,
        assistantBubble: HTMLElement,
        state: StreamState,
        revealContent: () => void,
        scheduleRender: () => void,
    ): void {
        switch (event.event) {
            case SSE_EVENT.WARMING: {
                // The chat engine is cold-loading; say so instead of showing silent dots.
                if (!state.warmingShown) {
                    state.warmingShown = true;
                    void this.renderFollowing(() => {
                        state.spinnerEl.createDiv({ cls: "lilbee-chat-warming-label", text: MESSAGES.CHAT_WARMING });
                    });
                }
                break;
            }
            case SSE_EVENT.COMPACTING: {
                const progress = event.data as CompactingEventData;
                // Inserting above the anchor grows the transcript; renderFollowing keeps the pin.
                void this.renderFollowing(() => {
                    this.openCompactionMarker(state);
                    this.advanceCompactionMarker(state, progress);
                });
                break;
            }
            case SSE_EVENT.COMPACTION: {
                const data = event.data as CompactionEventData;
                this.summary = data.summary;
                this.history.splice(0, data.condensed + data.stranded);
                void this.renderFollowing(() => this.settleCompactionMarker(state, data));
                break;
            }
            case SSE_EVENT.TOKEN: {
                revealContent();
                // First answer token: collapse the reasoning block.
                if (!state.answerStarted) {
                    state.answerStarted = true;
                    state.reasoningDetailsEl?.removeAttribute("open");
                }
                state.fullContent += extractString(event.data, "token");
                scheduleRender();
                break;
            }
            case SSE_EVENT.REASONING: {
                // Stream reasoning live into an expanded block above the answer.
                revealContent();
                const el = this.ensureReasoningBlock(assistantBubble, textEl, state);
                state.reasoningContent += extractString(event.data, "token");
                this.scheduleReasoningRender(state, el);
                break;
            }
            case SSE_EVENT.SOURCES:
                state.sources.push(...(event.data as Source[]));
                break;
            case SSE_EVENT.DONE: {
                revealContent();
                state.streamEnded = true;
                const rendered = state.fullContent;
                // Banner and sources grow the bubble after the last token; render
                // them inside one follow so the view ends pinned to the bottom.
                void this.renderFollowing(async () => {
                    if (state.reasoningContent) {
                        const el = this.ensureReasoningBlock(assistantBubble, textEl, state);
                        await this.renderMarkdown(el, state.reasoningContent);
                        state.reasoningDetailsEl?.removeAttribute("open");
                    }
                    this.renderBannerIfPresent(event.data, assistantBubble);
                    await this.renderMarkdown(textEl, rendered);
                    if (state.sources.length > 0) this.renderSources(assistantBubble, state.sources);
                });
                this.history.push({ role: "assistant", content: rendered });
                // Only a completed answer is persisted; a cancelled one leaves the question alone.
                if (rendered) {
                    const paths = [...new Set(state.sources.map((s) => s.source))];
                    this.queuePersist(() => this.persistTurn(this.sessionId, SESSION_ROLE.ASSISTANT, rendered, paths));
                }
                break;
            }
            case SSE_EVENT.ERROR: {
                state.streamEnded = true;
                const errMsg = extractString(event.data, "message");
                assistantBubble.empty();
                // Match the thrown-error path: drop the assistant skin so the
                // error bubble doesn't inherit the assistant background colour
                // from .lilbee-chat-message.assistant.
                assistantBubble.removeClass("assistant");
                assistantBubble.addClass("lilbee-chat-message-error");
                assistantBubble.setAttribute("role", "alert");
                assistantBubble.createDiv({
                    cls: "lilbee-chat-error-text",
                    text: MESSAGES.ERROR_STREAM(errMsg),
                });
                new Notice(MESSAGES.ERROR_STREAM(errMsg));
                if (isModelUnavailableError(extractSseErrorCode(event.data), errMsg)) {
                    new Notice(MESSAGES.NOTICE_MODEL_UNAVAILABLE_SETUP);
                    new SetupWizard(this.app, this.plugin).open();
                }
                break;
            }
            case SSE_EVENT.MEMORY_EXTRACTED: {
                const extracted = event.data as MemoryExtractedData;
                if (extracted.count > 0) {
                    new Notice(MESSAGES.MEMORY_EXTRACTED_NOTICE(extracted.count));
                    this.plugin.refreshMemoryViews();
                }
                break;
            }
        }
    }
 
    /** Create the reasoning block above the answer on first use; return its content div. */
    private ensureReasoningBlock(assistantBubble: HTMLElement, textEl: HTMLElement, state: StreamState): HTMLElement {
        if (state.reasoningContentEl) return state.reasoningContentEl;
        const details = assistantBubble.createEl("details", { cls: "lilbee-reasoning" });
        // Expanded while thinking; collapsed if the answer already started.
        if (!state.answerStarted) details.setAttribute("open", "");
        details.createEl("summary", { text: MESSAGES.LABEL_REASONING });
        const content = details.createDiv({ cls: "lilbee-reasoning-content" });
        // Move the reasoning block above the answer div.
        assistantBubble.insertBefore(details, textEl);
        state.reasoningDetailsEl = details;
        state.reasoningContentEl = content;
        return content;
    }
 
    /** Coalesce live reasoning re-renders to one per frame. */
    private scheduleReasoningRender(state: StreamState, el: HTMLElement): void {
        if (state.reasoningRenderPending) return;
        state.reasoningRenderPending = true;
        window.requestAnimationFrame(() => {
            state.reasoningRenderPending = false;
            if (state.streamEnded) return;
            // Same lightweight plain-text streaming as the answer (reasoning can
            // be long — the heavy per-token markdown render stuttered most here).
            void this.renderFollowing(() => el.setText(plainStream(state.reasoningContent)));
        });
    }
 
    /** True when the message list is scrolled to (or near) the bottom. */
    private isNearBottom(): boolean {
        const el = this.messagesEl;
        if (!el) return false;
        return el.scrollHeight - el.scrollTop - el.clientHeight < SCROLL_FOLLOW_THRESHOLD_PX;
    }
 
    /** Run a DOM-growing render; re-pin the view to the bottom only if it was pinned before. */
    private async renderFollowing(run: () => Promise<void> | void): Promise<void> {
        const follow = this.isNearBottom();
        await run();
        if (follow && this.messagesEl) {
            this.messagesEl.scrollTop = this.messagesEl.scrollHeight;
        }
    }
 
    private async renderMarkdown(el: HTMLElement, markdown: string): Promise<void> {
        el.empty();
        await MarkdownRenderer.render(this.app, markdown, el, "", this);
        el.addClass("markdown-rendered");
    }
 
    private renderInlineError(assistantBubble: HTMLElement, text: string): void {
        this.history.pop();
        assistantBubble.empty();
        assistantBubble.removeClass("assistant");
        assistantBubble.addClass("lilbee-chat-message-error");
        assistantBubble.setAttribute("role", "alert");
        assistantBubble.createDiv({ cls: "lilbee-chat-error-text", text });
        new Notice(text);
    }
 
    private openFilePicker(event: MouseEvent): void {
        const menu = new Menu();
        menu.addItem((item) => {
            item.setTitle(MESSAGES.WIZARD_FILE_PICKER_VAULT)
                .setIcon("vault")
                .onClick(() => {
                    new VaultFilePickerModal(this.app, (file) => this.enqueueAddFile(file)).open();
                });
        });
        menu.addItem((item) => {
            item.setTitle(MESSAGES.WIZARD_FILE_PICKER_DISK)
                .setIcon("file-plus")
                .onClick(() => this.openNativeFilePicker(false));
        });
        menu.addItem((item) => {
            item.setTitle(MESSAGES.WIZARD_FOLDER_PICKER_DISK)
                .setIcon("folder-plus")
                .onClick(() => this.openNativeFilePicker(true));
        });
        menu.addItem((item) => {
            item.setTitle(MESSAGES.WIZARD_CRAWL_WEB)
                .setIcon("globe")
                .onClick(() => {
                    new CrawlModal(this.app, this.plugin).open();
                });
        });
        this.showMenu(menu, event);
    }
 
    /** The core "Native menus" appearance setting; unset falls back to the platform default (native on macOS). */
    private prefersNativeMenu(): boolean {
        const vault = this.app.vault as unknown as { getConfig?: (key: string) => unknown };
        const value = vault.getConfig?.("nativeMenus");
        return typeof value === "boolean" ? value : Platform.isMacOS;
    }
 
    /** Show a menu, native when the vault prefers it; the in-window menu gets capture-phase ESC dismissal. */
    private showMenu(menu: Menu, event: MouseEvent): void {
        const useNative = this.prefersNativeMenu();
        menu.setUseNativeMenu(useNative);
        if (!useNative) {
            // Capture-phase ESC: a focused input can otherwise swallow the keypress.
            const onKey = (e: KeyboardEvent) => {
                if (e.key === "Escape") {
                    e.preventDefault();
                    menu.hide();
                }
            };
            activeDocument.addEventListener("keydown", onKey, true);
            menu.onHide(() => activeDocument.removeEventListener("keydown", onKey, true));
        }
        // Keyboard-synthesized clicks (detail 0) carry no coordinates; anchor to the trigger instead.
        const trigger = event.detail === 0 ? (event.currentTarget as HTMLElement | null) : null;
        if (trigger) {
            const rect = trigger.getBoundingClientRect();
            menu.showAtPosition({ x: rect.left, y: rect.bottom });
            return;
        }
        menu.showAtMouseEvent(event);
    }
 
    private openNativeFilePicker(directory: boolean): void {
        const properties = directory ? ["openDirectory"] : ["openFile", "multiSelections"];
        electronDialog
            .showOpenDialog({ properties })
            .then((result) => {
                if (result.canceled || result.filePaths.length === 0) return;
                void this.plugin.addExternalFiles(result.filePaths);
            })
            .catch(() => {
                new Notice(MESSAGES.ERROR_FILE_PICKER);
            });
    }
 
    private enqueueAddFile(file: TFile): void {
        void this.plugin.addToLilbee(file);
    }
 
    private showEmptyState(): void {
        if (this.emptyStateEl || !this.messagesEl) return;
        this.emptyStateEl = this.messagesEl.createDiv({ cls: "lilbee-chat-empty-state" });
        this.emptyStateEl.createDiv({ cls: "lilbee-chat-empty-icon", text: "🔬" });
        this.emptyStateEl.createDiv({ cls: "lilbee-chat-empty-heading", text: MESSAGES.NOTICE_NO_MODELS_INSTALLED });
        this.emptyStateEl.createEl("p", { text: MESSAGES.NOTICE_NO_MODELS_DESCRIPTION });
        const btn = this.emptyStateEl.createEl("button", { text: MESSAGES.BUTTON_BROWSE_CATALOG, cls: "mod-cta" });
        btn.addEventListener("click", () => {
            new CatalogModal(this.app, this.plugin).open();
        });
    }
 
    private hideEmptyState(): void {
        if (this.emptyStateEl) {
            this.emptyStateEl.remove();
            this.emptyStateEl = null;
        }
    }
 
    private async saveToVault(): Promise<void> {
        if (this.history.length === 0) {
            new Notice(MESSAGES.NOTICE_NOTHING_SAVE);
            return;
        }
        const now = new Date();
        const pad = (n: number) => String(n).padStart(2, "0");
        const stamp = `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())}-${pad(now.getHours())}${pad(now.getMinutes())}${pad(now.getSeconds())}`;
        const filename = `chat-${stamp}.md`;
        const folder = "lilbee";
        const path = `${folder}/${filename}`;
 
        const lines = [`# ${MESSAGES.LABEL_CHAT_VIEW} — ${now.toLocaleDateString()}`, ""];
        for (const msg of this.history) {
            const label = msg.role === "user" ? "User" : "Assistant";
            lines.push(`**${label}**: ${msg.content}`, "");
        }
        const content = lines.join("\n");
 
        try {
            const vault = this.app.vault;
            const existing = vault.getAbstractFileByPath(folder);
            if (!existing) {
                await vault.createFolder(folder);
            }
            await vault.create(path, content);
            new Notice(MESSAGES.NOTICE_SAVED(path));
        } catch {
            new Notice(MESSAGES.ERROR_SAVE_CHAT);
        }
    }
 
    /** The collapsed "Sources" block shared by live and restored answers. Returns the chip container. */
    private createSourcesBlock(container: HTMLElement): HTMLElement {
        const sourcesEl = container.createDiv({ cls: "lilbee-chat-sources" });
        const details = sourcesEl.createEl("details");
        details.createEl("summary", { text: MESSAGES.LABEL_SOURCES });
        return details.createDiv({ cls: "lilbee-chat-source-chips" });
    }
 
    private renderSources(container: HTMLElement, sources: Source[]): void {
        const chipsEl = this.createSourcesBlock(container);
        renderAggregatedSourceChips(chipsEl, sources, this.app, this.plugin.api);
    }
 
    private renderBannerIfPresent(data: unknown, assistantBubble: HTMLElement): void {
        const banner = extractBanner(data);
        if (banner === null || !this.messagesEl) return;
        const bannerEl = this.messagesEl.createDiv({ cls: "lilbee-chat-banner" });
        bannerEl.setText(banner);
        this.messagesEl.insertBefore(bannerEl, assistantBubble);
    }
}
 
export class VaultFilePickerModal extends FuzzySuggestModal<TFile> {
    private onChoose: (file: TFile) => void;
 
    constructor(app: import("obsidian").App, onChoose: (file: TFile) => void) {
        super(app);
        this.onChoose = onChoose;
        this.setPlaceholder(MESSAGES.PLACEHOLDER_PICK_VAULT_FILE);
    }
 
    getItems(): TFile[] {
        return this.app.vault.getFiles();
    }
 
    getItemText(item: TFile): string {
        return item.path;
    }
 
    onChooseItem(item: TFile): void {
        this.onChoose(item);
    }
}