Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

1.4 KB
Raw Blame
export type ModelLifecycleLease = number;
export type ModelLifecyclePhase = "preparing" | "loading" | "unloading";

/** The lease stops one async caller clearing another's loading state. */
export class ModelLifecycleGate {
  private activeLease: ModelLifecycleLease | null = null;
  private phase: ModelLifecyclePhase | null = null;
  private nextLease = 1;

  tryAcquire(
    phase: ModelLifecyclePhase = "preparing",
  ): ModelLifecycleLease | null {
    if (this.activeLease !== null) {
      return null;
    }
    const lease = this.nextLease++;
    this.activeLease = lease;
    this.phase = phase;
    return lease;
  }

  currentPhase(): ModelLifecyclePhase | null {
    return this.phase;
  }

  canQueue(): boolean {
    return this.activeLease === null || this.phase === "loading";
  }

  markLoading(lease: ModelLifecycleLease): boolean {
    if (this.activeLease !== lease || this.phase !== "preparing") {
      return false;
    }
    this.phase = "loading";
    return true;
  }

  markFailed(lease: ModelLifecycleLease): boolean {
    if (this.activeLease !== lease || this.phase !== "loading") return false;
    this.phase = "preparing";
    return true;
  }

  release(lease: ModelLifecycleLease): boolean {
    if (this.activeLease !== lease) {
      return false;
    }
    this.activeLease = null;
    this.phase = null;
    return true;
  }
}

export const chatModelLifecycleGate = new ModelLifecycleGate();