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 | 4x 4x 4x 1x 1x 3x 3x 3x 4x 4x 4x 4x 4x 4x 412x 4x 193x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 1x 1x 1x 59x 59x 59x 59x 59x 59x 66x 66x 10x 10x 6x 6x 6x 6x 4x 4x 1x 1x 3x 3x 56x 56x 56x 56x 10x 5x 6x 5x 5x 5x 5x 6x 6x 3x 3x 61x 61x 179x 813x 813x 1x 1x 813x 4x 3x 3x 103x 103x 101x 101x 101x 101x 101x 101x 103x 7x 94x 101x 101x 101x 85x 48x 37x 101x 101x 101x 101x 101x 101x 101x 5x 5x 5x 202x 7x 7x 7x 5x 2x 7x 2x 2x 2x 1x 1x 1x 1x 193x 193x 193x 193x 193x 193x 579x 391x 193x 193x 193x 193x 4x 4x 4x 4x 4x 189x 189x 189x 382x 382x 382x 382x 382x 382x 199x 199x 199x 101x 101x 101x 299x 299x 299x 299x 299x 299x 299x 299x 299x 10x 10x 299x 282x 282x 564x 299x 572x 299x 198x 94x 94x 94x 94x 188x 94x 564x 564x 564x 564x 564x 564x 288x 288x 276x 104x 104x 104x 104x 104x 104x 48x 48x 48x 48x 56x 56x 788x 388x 388x 388x 390x 208x 61x 61x 61x 7x 6x 6x 6x 9x 8x 8x 2x 1x 1x 1x 1x 1x 1x 6x 4x 4x 9x 16x 12x 12x 11x 11x 12x 12x 12x 12x 10x 668x 101x 101x 101x 101x 101x 2x 2x 99x 37x 37x 62x 101x 101x 37x 30x 30x 37x 64x 64x 64x 64x 45x 64x 64x 64x 16x 16x 16x 1x 1x 8x 8x 1x 7x 3x 4x 8x 8x 4x 4x 4x 4x 48x 132x 132x 128x 171x 35x 15x 15x 45x 45x 45x 15x 12x 12x 5x 5x 9x 9x 3x 3x 3x 6x 6x 3x 6x 2x 6x 6x 6x 6x 2x 2x 2x 2x 4x 4x 4x 4x 4x 4x 4x 4x 5x 5x 1x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 61x 61x 61x 61x 61x 61x 3x 1x 1x 1x 2x 59x 1x 1x 59x 2x 2x 59x 57x 57x | import { ItemView, Notice, Platform, WorkspaceLeaf, type App } from "obsidian";
import type LilbeePlugin from "../main";
import { isHttpStatus } from "../api";
import { displayLabelForRef } from "../utils/model-ref";
import {
ERROR_NAME,
PLACEMENT_MODE,
REPLICA_ROLES,
SERVER_MODE,
SSE_EVENT,
WORKER_ROLE,
type GpuInfo,
type GpuStat,
type GpuStatsPayload,
type PlacementMode,
type PlacementResponse,
type PlacementRoleSpec,
type PlacementSpec,
type RolePlacement,
type WorkerRole,
} from "../types";
import { addCloseButton } from "../components/close-button";
import { MESSAGES } from "../locales/en";
import { errorMessage } from "../utils";
export const VIEW_TYPE_PLACEMENT = "lilbee-placement";
/** Open the GPU placement view in a split beside `sourceLeaf` so live GPU
* activity sits next to the chat. Reuses an existing placement leaf if open. */
export async function revealPlacementBeside(app: App, sourceLeaf: WorkspaceLeaf): Promise<void> {
const existing = app.workspace.getLeavesOfType(VIEW_TYPE_PLACEMENT);
if (existing.length > 0) {
await app.workspace.revealLeaf(existing[0]);
return;
}
const leaf = app.workspace.createLeafBySplit(sourceLeaf, "vertical");
await leaf.setViewState({ type: VIEW_TYPE_PLACEMENT, active: false });
await app.workspace.revealLeaf(leaf);
}
const PREVIEW_DEBOUNCE_MS = 350;
const STARTUP_RETRY_MS = 2000;
const HTTP_NOT_FOUND = 404;
const HTTP_CONFLICT = 409;
const HTTP_UNPROCESSABLE = 422;
const GB = 1_000_000_000;
/** A role row's editable state while a manual placement is being assembled. */
interface RoleDraft {
devices: Set<number>;
replicas: number;
}
function formatGb(bytes: number): string {
return `${(bytes / GB).toFixed(1)} GB`;
}
/** Apple-silicon GPUs have no live memory sampling; their memory figure is a capacity, not a gauge.
* The server reports the device prefix "MTL" (older builds "Metal"). Metal alone isn't enough:
* an Intel Mac's discrete AMD card is also Metal but has real dedicated VRAM, so require an
* Apple-named device too. */
const UNIFIED_BACKENDS = new Set(["mtl", "metal"]);
function isUnifiedMemory(gpu: GpuInfo): boolean {
return UNIFIED_BACKENDS.has(gpu.backend.toLowerCase()) && gpu.name.startsWith("Apple");
}
export class PlacementView extends ItemView {
private plugin: LilbeePlugin;
private current: PlacementResponse | null = null;
private mode: PlacementMode = PLACEMENT_MODE.AUTO;
private draft: Map<WorkerRole, RoleDraft> = new Map();
private applying = false;
private unplaceable: string[] = [];
private applyDisabled = false;
private multiDevice = false;
private previewTimer: number | null = null;
private startupRetryTimer: number | null = null;
private waitingForServer = false;
private statsController: AbortController | null = null;
private gpuNotice: string | null = null;
private gpuNoticeEl: HTMLElement | null = null;
/** Live util + vram bars and their text per device index, updated in place by the stats stream. */
// vramFill/memText are absent on unified-memory rows, which render a static capacity label.
private gpuBars: Map<
number,
{ utilFill: HTMLElement; utilText: HTMLElement; vramFill?: HTMLElement; memText?: HTMLElement }
> = new Map();
private bodyEl: HTMLElement | null = null;
constructor(leaf: WorkspaceLeaf, plugin: LilbeePlugin) {
super(leaf);
this.plugin = plugin;
}
getViewType(): string {
return VIEW_TYPE_PLACEMENT;
}
getDisplayText(): string {
return MESSAGES.LABEL_PLACEMENT_VIEW;
}
getIcon(): string {
return "cpu";
}
async onOpen(): Promise<void> {
const { contentEl } = this;
contentEl.empty();
contentEl.addClass("lilbee-placement-container");
this.bodyEl = contentEl.createDiv({ cls: "lilbee-placement-body" });
await this.reload();
// Stream live per-GPU utilization + memory so the cards animate as the
// fleet loads and ingest runs, instead of freezing at the open snapshot.
void this.subscribeStats();
}
async reload(): Promise<void> {
const result = await this.plugin.api.placement();
if (result.isErr()) {
this.current = null;
if (this.isTransientStartupError(result.error)) {
this.waitingForServer = true;
this.renderWaitingForServer();
this.scheduleStartupRetry();
return;
}
this.waitingForServer = false;
// No /api/placement route: a pre-0.6.90 server. Point at the server
// update instead of a raw 404.
if (isHttpStatus(result.error, HTTP_NOT_FOUND)) {
this.renderMessage(MESSAGES.PLACEMENT_NEEDS_NEWER_SERVER);
return;
}
this.renderMessage(
MESSAGES.PLACEMENT_LOAD_FAILED(
errorMessage(result.error, MESSAGES.ERROR_UNKNOWN, this.plugin.settings.serverMode),
),
);
return;
}
const resumedAfterWait = this.waitingForServer;
this.waitingForServer = false;
this.adoptResponse(result.value);
// The stats stream opened at onOpen died while the server was booting;
// reopen it so the freshly rendered cards animate.
if (resumedAfterWait) void this.subscribeStats();
}
/** Whether a failed load will heal on its own if we keep polling.
* A 401 in managed mode is the startup race between a restored leaf and
* session-token discovery: the plugin holds the previous run's token until
* it reads the fresh server.json. External mode's token is user-entered,
* so a rejection there is actionable, not transient. */
private isTransientStartupError(error: Error): boolean {
if (error.name === ERROR_NAME.SERVER_STARTING) return true;
return error.name === ERROR_NAME.SESSION_TOKEN && this.plugin.settings.serverMode === SERVER_MODE.MANAGED;
}
/** Live holding state while the server boots; reload() polls until it answers. */
private renderWaitingForServer(): void {
if (!this.bodyEl) return;
this.bodyEl.empty();
const wait = this.bodyEl.createDiv({ cls: "lilbee-placement-empty lilbee-placement-waiting" });
wait.createDiv({ cls: "lilbee-placement-spinner" });
wait.createDiv({ text: MESSAGES.PLACEMENT_WAITING_SERVER });
}
private scheduleStartupRetry(): void {
if (this.startupRetryTimer !== null) window.clearTimeout(this.startupRetryTimer);
this.startupRetryTimer = window.setTimeout(() => {
this.startupRetryTimer = null;
void this.reload();
}, STARTUP_RETRY_MS);
}
/** Rebuild the editable draft from the current resolved plan. */
private seedDraft(roles: RolePlacement[]): void {
this.draft = new Map();
for (const role of roles) {
this.draft.set(role.role, { devices: new Set(role.devices), replicas: role.replicas });
}
}
/** The draft for a role, created (pinned to no device, single replica) if absent. */
private draftFor(role: WorkerRole): RoleDraft {
let d = this.draft.get(role);
if (!d) {
d = { devices: new Set(), replicas: 1 };
this.draft.set(role, d);
}
return d;
}
private renderMessage(message: string): void {
if (!this.bodyEl) return;
this.bodyEl.empty();
this.bodyEl.createDiv({ cls: "lilbee-placement-empty", text: message });
}
private render(): void {
const data = this.current;
if (!this.bodyEl || !data) return;
this.multiDevice = data.gpus.length >= 2;
this.gpuBars.clear();
this.gpuNoticeEl = null;
this.bodyEl.empty();
this.renderHeader(this.bodyEl, data);
this.renderNotice(this.bodyEl, data.notice ?? null);
if (data.gpus.length < 2) {
this.renderSingleDevice(this.bodyEl, data);
} else {
this.renderMatrix(this.bodyEl, data);
}
this.renderFooter(this.bodyEl, data);
if (this.applying) this.renderApplyingOverlay(this.bodyEl);
}
private stateLabel(data: PlacementResponse): string {
if (this.applying) return MESSAGES.PLACEMENT_STATE_APPLYING;
if (this.mode === PLACEMENT_MODE.MANUAL) {
return this.isEdited(data) ? MESSAGES.PLACEMENT_STATE_EDITED : MESSAGES.PLACEMENT_STATE_MANUAL;
}
return MESSAGES.PLACEMENT_STATE_AUTO;
}
private renderHeader(container: HTMLElement, data: PlacementResponse): void {
const header = container.createDiv({ cls: "lilbee-placement-header" });
header.createEl("h2", { text: MESSAGES.PLACEMENT_TITLE });
const state = this.applying ? "applying" : this.mode;
header.createSpan({ cls: `lilbee-placement-state is-${state}`, text: this.stateLabel(data) });
addCloseButton(header, this.leaf, MESSAGES.LABEL_CLOSE_VIEW(MESSAGES.LABEL_PLACEMENT_VIEW));
}
/** Server-issued host fix hint, rendered verbatim under a localized label. */
private renderNotice(container: HTMLElement, notice: string | null): void {
this.gpuNotice = notice;
this.gpuNoticeEl = notice ? this.createNoticeEl(container, notice) : null;
}
private createNoticeEl(container: HTMLElement, notice: string): HTMLElement {
const el = container.createDiv({ cls: "lilbee-placement-notice", text: MESSAGES.PLACEMENT_GPU_NOTICE(notice) });
this.tip(el, MESSAGES.PLACEMENT_GPU_NOTICE_TOOLTIP);
return el;
}
private renderSectionTitle(container: HTMLElement, text: string): void {
container.createDiv({ cls: "lilbee-placement-section-title", text });
}
// ---- hardware cards -------------------------------------------------------
/** Single device: one unified/CPU host card (no discrete GPUs) or one GPU card. */
private renderSingleDevice(container: HTMLElement, data: PlacementResponse): void {
this.renderSectionTitle(container, MESSAGES.PLACEMENT_SECTION_HARDWARE);
const hw = container.createDiv({ cls: "lilbee-placement-hw" });
if (data.gpus.length === 1) {
this.renderGpuRow(hw, data.gpus[0], data);
} else {
this.renderHostCard(hw);
}
this.renderRoleSection(container, data, null);
}
/** The host's compute when no discrete GPU is enumerated: Apple unified memory
* (the chat fleet runs on Metal) or a plain CPU host elsewhere. */
private renderHostCard(container: HTMLElement): void {
const card = container.createDiv({ cls: "lilbee-placement-card" });
const head = card.createDiv({ cls: "lilbee-placement-card-head" });
if (Platform.isMacOS) {
head.createSpan({ cls: "lilbee-placement-card-name", text: MESSAGES.PLACEMENT_HOST_APPLE });
head.createSpan({ cls: "lilbee-placement-card-sub", text: MESSAGES.PLACEMENT_HOST_APPLE_SUB });
} else {
head.createSpan({ cls: "lilbee-placement-card-name", text: MESSAGES.PLACEMENT_HOST_CPU });
head.createSpan({ cls: "lilbee-placement-card-sub", text: MESSAGES.PLACEMENT_HOST_CPU_SUB });
}
}
/** One GPU as a compact row: index chip, name, role badges, and util + vram
* mini-bars. Scales to many GPUs where a card-per-GPU layout would not. */
private renderGpuRow(container: HTMLElement, gpu: GpuInfo, data: PlacementResponse): void {
const row = container.createDiv({ cls: "lilbee-gpu-row" });
row.createSpan({ cls: "lilbee-gpu-idx", text: String(gpu.index) });
const top = row.createDiv({ cls: "lilbee-gpu-top" });
top.createSpan({ cls: "lilbee-gpu-name", text: gpu.name });
const badges = top.createDiv({ cls: "lilbee-gpu-roles" });
for (const role of data.roles) {
if (role.devices.includes(gpu.index)) {
badges.createSpan({ cls: `lilbee-role-badge is-${role.role}`, text: role.role });
}
}
const meters = row.createDiv({ cls: "lilbee-gpu-meters" });
const util = this.renderMeter(meters, "util", MESSAGES.PLACEMENT_METER_UTIL);
util.val.setText(MESSAGES.PLACEMENT_UTIL_NA);
if (isUnifiedMemory(gpu)) {
// Apple reports no live GPU-memory usage, so a free/total gauge would
// sit at "all free" forever. Show the unified capacity as a plain label.
const meter = meters.createDiv({ cls: "lilbee-meter lilbee-meter-vram" });
meter.createSpan({ cls: "lilbee-meter-label", text: MESSAGES.PLACEMENT_METER_VRAM });
meter.createSpan({
cls: "lilbee-meter-val",
text: MESSAGES.PLACEMENT_MEM_UNIFIED(formatGb(gpu.total_bytes)),
});
this.gpuBars.set(gpu.index, { utilFill: util.fill, utilText: util.val });
return;
}
const vram = this.renderMeter(meters, "vram", MESSAGES.PLACEMENT_METER_VRAM);
this.setVram(vram, gpu.free_bytes, gpu.total_bytes);
// Keep refs so the live stats stream can move the bars without a full re-render.
this.gpuBars.set(gpu.index, {
utilFill: util.fill,
utilText: util.val,
vramFill: vram.fill,
memText: vram.val,
});
}
/** A labelled mini-bar ("util" / "vram"): label, track+fill, value text. */
private renderMeter(
container: HTMLElement,
variant: "util" | "vram",
label: string,
): { fill: HTMLElement; val: HTMLElement } {
const meter = container.createDiv({ cls: `lilbee-meter lilbee-meter-${variant}` });
meter.createSpan({ cls: "lilbee-meter-label", text: label });
const bar = meter.createDiv({ cls: "lilbee-bar" });
const fill = bar.createDiv({ cls: "lilbee-bar-fill" });
const val = meter.createSpan({ cls: "lilbee-meter-val" });
return { fill, val };
}
/** Fill the vram bar to the used fraction and set its free/total text. */
private setVram(vram: { fill: HTMLElement; val: HTMLElement }, freeBytes: number, totalBytes: number): void {
const usedPct = totalBytes > 0 ? this.clampPct(((totalBytes - freeBytes) / totalBytes) * 100) : 0;
vram.fill.setCssProps({ width: `${usedPct}%` });
vram.val.setText(MESSAGES.PLACEMENT_MEM_FREE(formatGb(freeBytes), formatGb(totalBytes)));
}
// ---- roles ----------------------------------------------------------------
/** The Roles section. `gpus` non-null renders the per-role GPU toggle matrix. */
private renderRoleSection(container: HTMLElement, data: PlacementResponse, gpus: GpuInfo[] | null): void {
this.renderSectionTitle(container, MESSAGES.PLACEMENT_SECTION_ROLES);
const table = container.createDiv({ cls: "lilbee-placement-roles" });
for (const role of data.roles) {
this.renderRoleRow(table, role, gpus);
}
}
private renderRoleRow(container: HTMLElement, role: RolePlacement, gpus: GpuInfo[] | null): void {
const row = container.createDiv({ cls: "lilbee-placement-role-row" });
row.dataset.role = role.role;
if (this.unplaceable.includes(role.role)) row.addClass("lilbee-placement-role-unfit");
const head = row.createDiv({ cls: "lilbee-placement-role-head" });
head.createSpan({ cls: `lilbee-placement-badge is-${role.role}`, text: role.role });
head.createSpan({ cls: "lilbee-placement-role-hint", text: this.roleHint(role.role) });
row.createSpan({
cls: "lilbee-placement-role-model",
text: role.model ? displayLabelForRef(role.model) : MESSAGES.PLACEMENT_NOT_SET,
});
// Truthy: absent on older servers, 0 for a role with no model loaded.
if (role.vram_bytes) {
const vram = row.createSpan({
cls: "lilbee-placement-role-vram",
text: MESSAGES.PLACEMENT_ROLE_VRAM(formatGb(role.vram_bytes)),
});
this.tip(vram, MESSAGES.PLACEMENT_TIP_ROLE_VRAM(role.role));
}
if (gpus) {
const toggles = row.createDiv({ cls: "lilbee-placement-toggles" });
for (const gpu of gpus) {
this.renderToggle(toggles, role.role, gpu);
}
}
if (REPLICA_ROLES.has(role.role)) this.renderStepper(row, role.role);
}
/** A single-instance role (rerank) pins to exactly one card: it is neither
* tensor-split like chat nor data-parallel replicated like embed/vision. */
private isSingleSelect(role: WorkerRole): boolean {
return role !== WORKER_ROLE.CHAT && !REPLICA_ROLES.has(role);
}
/** The one-word placement rule shown under each role: split / mirror / one card. */
private roleHint(role: WorkerRole): string {
if (role === WORKER_ROLE.CHAT) return MESSAGES.PLACEMENT_HINT_SPLIT;
if (REPLICA_ROLES.has(role)) return MESSAGES.PLACEMENT_HINT_MIRROR;
return MESSAGES.PLACEMENT_HINT_SINGLE;
}
// ---- multi-GPU matrix -----------------------------------------------------
private renderMatrix(container: HTMLElement, data: PlacementResponse): void {
this.renderSectionTitle(container, MESSAGES.PLACEMENT_SECTION_HARDWARE);
const hw = container.createDiv({ cls: "lilbee-placement-hw" });
for (const gpu of data.gpus) {
this.renderGpuRow(hw, gpu, data);
}
this.renderRoleSection(container, data, data.gpus);
}
private renderToggle(container: HTMLElement, role: WorkerRole, gpu: GpuInfo): void {
const draft = this.draftFor(role);
let cls = "lilbee-placement-toggle";
if (this.isSingleSelect(role)) cls += " is-radio";
if (draft.devices.has(gpu.index)) cls += " is-on";
const toggle = container.createEl("button", { cls, text: String(gpu.index) });
if (this.isEditable()) {
this.tip(toggle, MESSAGES.PLACEMENT_TIP_CHIP(role, gpu.label));
toggle.addEventListener("click", () => this.toggleDevice(role, gpu.index));
} else {
this.makeReadOnly(toggle);
}
}
private renderStepper(container: HTMLElement, role: WorkerRole): void {
const draft = this.draftFor(role);
const stepper = container.createDiv({ cls: "lilbee-placement-stepper" });
const dec = stepper.createEl("button", { cls: "lilbee-placement-step", text: "−" });
stepper.createSpan({ cls: "lilbee-placement-step-count", text: `×${draft.replicas}` });
const inc = stepper.createEl("button", { cls: "lilbee-placement-step", text: "+" });
if (this.isEditable()) {
this.tip(dec, MESSAGES.PLACEMENT_TIP_REPLICA_REMOVE(role));
this.tip(inc, MESSAGES.PLACEMENT_TIP_REPLICA_ADD(role));
dec.addEventListener("click", () => this.changeReplicas(role, -1));
inc.addEventListener("click", () => this.changeReplicas(role, 1));
} else {
this.makeReadOnly(dec);
this.makeReadOnly(inc);
}
}
/** Tooltip via aria-label only: Obsidian renders its own styled tooltip from
* it. Setting `title` too would stack a second native tooltip on hover. */
private tip(el: HTMLElement, text: string): void {
el.setAttribute("aria-label", text);
}
/** A disabled control: greyed, tooltipped with why, and a click explains where
* to make the change instead (rather than silently doing nothing). */
private makeReadOnly(el: HTMLElement): void {
el.addClass("is-readonly");
this.tip(el, this.readOnlyHint());
el.addEventListener("click", () => new Notice(this.readOnlyHint()));
}
/** Why editing is unavailable: enter manual mode on multi-GPU, or use Settings
* on a single/unified device where there is nothing to assign. */
private readOnlyHint(): string {
return this.multiDevice ? MESSAGES.PLACEMENT_HINT_EDIT_MANUALLY : MESSAGES.PLACEMENT_HINT_REPLICAS_SETTINGS;
}
private clampPct(pct: number): number {
return Math.min(100, Math.max(0, Math.round(pct)));
}
/** Subscribe to the live GPU stats SSE stream; each event moves the bars in
* place. The stream stays open until the view closes (onClose aborts it) or
* the server drops it, in which case the bars hold their last values. */
private async subscribeStats(): Promise<void> {
this.statsController = new AbortController();
try {
for await (const event of this.plugin.api.gpuStatsStream(this.statsController.signal)) {
if (event.event === SSE_EVENT.GPU_STATS) {
const payload = event.data as GpuStatsPayload;
this.applyStats(payload.gpus);
this.applyNotice(payload.notice ?? null);
}
}
} catch {
// Stream aborted (view closed) or failed (server gone): leave the bars as they are.
}
}
/** Move the notice banner in place without rebuilding the rest of the view. */
private applyNotice(notice: string | null): void {
if (notice === this.gpuNotice) return;
this.gpuNotice = notice;
if (this.gpuNoticeEl) {
if (notice) {
this.gpuNoticeEl.setText(MESSAGES.PLACEMENT_GPU_NOTICE(notice));
this.tip(this.gpuNoticeEl, MESSAGES.PLACEMENT_GPU_NOTICE_TOOLTIP);
return;
}
this.gpuNoticeEl.detach();
this.gpuNoticeEl = null;
return;
}
if (!notice || !this.bodyEl) return;
const el = this.createNoticeEl(this.bodyEl, notice);
// Insert right after the header; append when nothing follows it yet.
this.bodyEl.insertBefore(el, (this.bodyEl.children[1] as HTMLElement | undefined) ?? null);
this.gpuNoticeEl = el;
}
/** Move each card's utilization bar and util/memory text from a live snapshot.
* Skips devices not currently rendered (a reload rebuilds the cards). */
private applyStats(gpus: GpuStat[]): void {
for (const gpu of gpus) {
const refs = this.gpuBars.get(gpu.index);
if (!refs) continue;
const pct = gpu.utilization_pct;
const clamped = pct === null ? 0 : this.clampPct(pct);
refs.utilFill.setCssProps({ width: `${clamped}%` });
// Glow only while the card is actually working; idle bars stay flat.
refs.utilFill.toggleClass("is-active", clamped > 0);
refs.utilText.setText(pct === null ? MESSAGES.PLACEMENT_UTIL_NA : MESSAGES.PLACEMENT_UTIL(clamped));
if (refs.vramFill && refs.memText) {
this.setVram({ fill: refs.vramFill, val: refs.memText }, gpu.free_bytes, gpu.total_bytes);
}
}
}
/** Chips and replica steppers are editable only in manual mode, which is
* reachable only on multi-GPU hosts. A single or unified device has nothing to
* assign — the server rejects a device-less spec — so replica counts there are
* configured in Settings (Hardware / fleet), not here. */
private isEditable(): boolean {
return this.mode === PLACEMENT_MODE.MANUAL && !this.applying;
}
// ---- footer + actions -----------------------------------------------------
private renderFooter(container: HTMLElement, data: PlacementResponse): void {
const footer = container.createDiv({ cls: "lilbee-placement-footer" });
const fit = footer.createSpan({ cls: "lilbee-placement-fit" });
fit.createSpan({ cls: "lilbee-placement-fit-dot" });
const label = fit.createSpan({ cls: "lilbee-placement-fit-label" });
if (this.unplaceable.length > 0) {
fit.addClass("is-unfit");
label.setText(MESSAGES.PLACEMENT_WONT_FIT(this.unplaceable.join(", ")));
} else if (this.mode === PLACEMENT_MODE.AUTO) {
fit.addClass("is-muted");
label.setText(MESSAGES.PLACEMENT_AUTO_MANAGED);
} else {
label.setText(MESSAGES.PLACEMENT_FITS);
}
this.renderFooterButtons(footer, data);
}
private renderFooterButtons(footer: HTMLElement, data: PlacementResponse): void {
if (this.mode === PLACEMENT_MODE.AUTO) {
// Single device has nothing to assign; multi-GPU offers manual editing.
if (data.gpus.length >= 2) {
const edit = footer.createEl("button", { cls: "lilbee-placement-btn", text: MESSAGES.PLACEMENT_EDIT });
edit.addEventListener("click", () => this.enterManual());
}
return;
}
const preview = footer.createEl("button", { cls: "lilbee-placement-btn", text: MESSAGES.PLACEMENT_PREVIEW });
preview.addEventListener("click", () => void this.runPreview());
const apply = footer.createEl("button", {
cls: "lilbee-placement-btn lilbee-placement-btn-primary",
text: MESSAGES.PLACEMENT_APPLY,
});
if (this.applyBlocked()) apply.addClass("is-disabled");
else apply.addEventListener("click", () => void this.runApply());
const reset = footer.createEl("button", { cls: "lilbee-placement-btn", text: MESSAGES.PLACEMENT_RESET });
reset.addEventListener("click", () => void this.runReset());
}
private applyBlocked(): boolean {
return this.unplaceable.length > 0 || this.applyDisabled || this.applying;
}
private renderApplyingOverlay(container: HTMLElement): void {
const overlay = container.createDiv({ cls: "lilbee-placement-overlay" });
overlay.createDiv({ cls: "lilbee-placement-spinner" });
overlay.createDiv({ cls: "lilbee-placement-overlay-text", text: MESSAGES.PLACEMENT_REBUILDING });
}
// ---- edit transitions -----------------------------------------------------
private enterManual(): void {
this.mode = PLACEMENT_MODE.MANUAL;
this.render();
}
private toggleDevice(role: WorkerRole, deviceIndex: number): void {
const draft = this.draftFor(role);
if (this.isSingleSelect(role)) {
// rerank is a single pinned instance: picking a card replaces the pin.
draft.devices = new Set([deviceIndex]);
} else if (draft.devices.has(deviceIndex)) {
// Keep at least one device per role — the server rejects an empty set.
if (draft.devices.size > 1) draft.devices.delete(deviceIndex);
} else {
draft.devices.add(deviceIndex);
}
this.render();
this.schedulePreview();
}
private changeReplicas(role: WorkerRole, delta: number): void {
const draft = this.draftFor(role);
draft.replicas = Math.max(1, draft.replicas + delta);
this.render();
this.schedulePreview();
}
/** True when the draft diverges from the resolved plan we loaded. */
private isEdited(data: PlacementResponse): boolean {
for (const role of data.roles) {
const draft = this.draftFor(role.role);
if (draft.replicas !== role.replicas) return true;
if (draft.devices.size !== role.devices.length) return true;
if (!role.devices.every((d) => draft.devices.has(d))) return true;
}
return false;
}
/** Build the wire spec covering every drafted role. */
private buildSpec(): PlacementSpec {
const spec: PlacementSpec = {};
for (const [role, draft] of this.draft) {
const entry: PlacementRoleSpec = { devices: [...draft.devices].sort((a, b) => a - b) };
if (REPLICA_ROLES.has(role)) entry.replicas = draft.replicas;
spec[role] = entry;
}
return spec;
}
private schedulePreview(): void {
if (this.previewTimer !== null) window.clearTimeout(this.previewTimer);
this.previewTimer = window.setTimeout(() => {
this.previewTimer = null;
void this.runPreview();
}, PREVIEW_DEBOUNCE_MS);
}
private async runPreview(): Promise<void> {
// Preview is automatic (debounced on every edit), so a failure stays quiet
// (no toast) — but a fit rejection surfaces as the "Won't fit" badge so a
// model pinned to too few GPUs reads as won't-fit, not as nothing happening.
const result = await this.plugin.api.placementPreview(this.buildSpec());
if (result.isErr()) {
this.unplaceable = this.unplaceableFrom422(result.error);
this.render();
return;
}
this.unplaceable = result.value.unplaceable;
this.render();
}
/** The server rejects an impossible pin with a 422 naming the role(s) that
* won't fit ("chat pinned to device 0 needs N GiB but device 0 has M GiB").
* Map that to the unplaceable list; on any other error keep the prior state. */
private unplaceableFrom422(error: Error): string[] {
if (!isHttpStatus(error, HTTP_UNPROCESSABLE)) return this.unplaceable;
const roles = [...this.draft.keys()].filter((role) => error.message.includes(`${role} pinned to device`));
return roles.length > 0 ? roles : this.unplaceable;
}
private async runApply(): Promise<void> {
this.applying = true;
this.render();
const result = await this.plugin.api.applyPlacement(this.buildSpec());
if (result.isErr()) {
this.applying = false;
this.handleMutationError(result.error);
this.render();
return;
}
this.adoptResponse(result.value);
// The model reloads on the new device layout — keep the "Rebuilding fleet"
// overlay up until it reports ready, so the view isn't a silent idle screen.
this.applying = true;
this.render();
await this.waitForFleetReady();
this.applying = false;
this.render();
new Notice(MESSAGES.PLACEMENT_APPLIED);
}
/** Poll health until the fleet reports ready after a placement change; stops
* early on an errored/absent health signal so it never blocks indefinitely. */
private async waitForFleetReady(): Promise<void> {
for (let i = 0; i < 200; i++) {
const health = await this.plugin.api.health();
if (health.isErr() || health.value.chat_ready !== false) return;
await new Promise((r) => window.setTimeout(r, 1500));
}
}
private async runReset(): Promise<void> {
this.applying = true;
this.render();
const result = await this.plugin.api.clearPlacement();
this.applying = false;
if (result.isErr()) {
this.handleMutationError(result.error);
this.render();
return;
}
new Notice(MESSAGES.PLACEMENT_RESET_DONE);
this.adoptResponse(result.value);
}
private adoptResponse(response: PlacementResponse): void {
this.current = response;
this.mode = response.manual ? PLACEMENT_MODE.MANUAL : PLACEMENT_MODE.AUTO;
this.unplaceable = response.unplaceable;
this.applyDisabled = false;
this.seedDraft(response.roles);
this.render();
}
private handleMutationError(error: Error): void {
if (isHttpStatus(error, HTTP_CONFLICT)) {
this.applyDisabled = true;
new Notice(MESSAGES.PLACEMENT_APPLY_NOT_ENABLED);
return;
}
new Notice(
MESSAGES.PLACEMENT_APPLY_FAILED(
errorMessage(error, MESSAGES.ERROR_UNKNOWN, this.plugin.settings.serverMode),
),
);
}
async onClose(): Promise<void> {
if (this.previewTimer !== null) {
window.clearTimeout(this.previewTimer);
this.previewTimer = null;
}
if (this.startupRetryTimer !== null) {
window.clearTimeout(this.startupRetryTimer);
this.startupRetryTimer = null;
}
if (this.statsController !== null) {
this.statsController.abort();
this.statsController = null;
}
}
}
|