Spaces:
Paused

File size: 6,028 Bytes
bc9f3e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<script>
  import { onMount } from "svelte";
  import { t } from "../lib/stores.svelte.js";
  import { llamaServerInfo } from "../lib/api.js";
  import "./LlamaServerInstallDialog.css";

  /** llama-server download popup β€” the Settings tab's "llama-server"
   * download button opens this instead of silently auto-picking a build.
   * Fetches the detection report (/api/settings/llama-server-info), shows
   * every build that CAN run on this PC (CPU / CUDA 12.4 / CUDA 13.3) with
   * its reason, and lets the user pick. `oninstall(choice)` fires with the
   * chosen id (auto / cpu / cuda12 / cuda13) when the user confirms. */
  let { onclose, oninstall } = $props();

  let plan = $state(null);
  let loadErr = $state("");
  let selected = $state("auto");
  let rootEl;
  let boxEl = $state(null);

  // ── Draggable ──────────────────────────────────────────────────
  let dragOffsetX = 0;
  let dragOffsetY = 0;
  let posX = $state(null);
  let posY = $state(null);
  let dragging = $state(false);

  function onDragStart(e) {
    if (!boxEl) return;
    if (e.button !== 0) return;
    e.preventDefault();
    const rect = boxEl.getBoundingClientRect();
    dragOffsetX = e.clientX - rect.left;
    dragOffsetY = e.clientY - rect.top;
    if (posX === null) posX = rect.left;
    if (posY === null) posY = rect.top;
    dragging = true;
  }

  function onDragMove(e) {
    if (!dragging) return;
    const w = window.innerWidth;
    const h = window.innerHeight;
    let x = e.clientX - dragOffsetX;
    let y = e.clientY - dragOffsetY;
    const bw = boxEl ? boxEl.offsetWidth : 480;
    const bh = boxEl ? boxEl.offsetHeight : 350;
    x = Math.max(0, Math.min(x, w - bw));
    y = Math.max(0, Math.min(y, h - bh));
    posX = x;
    posY = y;
  }

  function onDragEnd() {
    dragging = false;
  }

  function fmt(key, params) {
    const s = t(key);
    if (!params) return s;
    return s.replace(/\{(\w+)\}/g, (m, k) => (params[k] !== undefined ? String(params[k]) : m));
  }

  function close() {
    onclose?.();
  }

  function confirm() {
    oninstall?.(selected);
    onclose?.();
  }

  function pick(id, available) {
    if (available) selected = id;
  }

  onMount(async () => {
    try {
      const r = await llamaServerInfo();
      plan = r.plan || r;
      const rec = (plan.options || []).find((o) => o.recommended);
      if (rec) selected = rec.id;
    } catch (e) {
      loadErr = e.message || String(e);
    }
    rootEl?.querySelector(".llsrv-ok")?.focus();
    const onKey = (e) => {
      if (e.key === "Escape") {
        e.preventDefault();
        close();
      }
    };
    document.addEventListener("keydown", onKey, true);
    return () => document.removeEventListener("keydown", onKey, true);
  });

  const detected = $derived(plan?.detected || {});
  const options = $derived(plan?.options || []);
</script>

<!-- svelte-ignore a11y_no_static_element_interactions -->
<svelte:window
  onmousemove={onDragMove}
  onmouseup={onDragEnd}
/>

<div
  class="llsrv-overlay"
  role="presentation"
  bind:this={rootEl}
  onmousedown={(e) => {
    if (e.target === rootEl) close();
  }}
>
  <div
    class="llsrv-box"
    class:dragging
    role="dialog"
    aria-modal="true"
    aria-labelledby="llsrv-title"
    bind:this={boxEl}
    style="{posX !== null ? `left:${posX}px;top:${posY}px;transform:none` : ''}"
  >
    <!-- svelte-ignore a11y_no_static_element_interactions -->
    <div
      class="llsrv-title llsrv-drag-handle"
      id="llsrv-title"
      role="none"
      onmousedown={onDragStart}
    >{t("llsrv_dialog_title")}</div>

    {#if loadErr}
      <div class="llsrv-err">{loadErr}</div>
      <div class="llsrv-actions">
        <button type="button" class="llsrv-ok" data-tooltip={t("llsrv_dialog_ok")} onclick={close}>
          {t("llsrv_dialog_ok")}
        </button>
      </div>
    {:else if !plan}
      <div class="llsrv-loading">{t("web_loading", "…")}</div>
    {:else}
      <div class="llsrv-detect">
        {t("llsrv_detected_gpu")}: <b>{detected.gpu_name || detected.gpu_brand || "β€”"}</b>
        <br />
        {t("llsrv_detected_cpu")}: <b>{detected.cpu_name || "β€”"}</b>
        {#if detected.driver_cuda_version}
          <br />{t("llsrv_detected_driver")}: <b>{detected.driver_cuda_version}</b>
        {/if}
        <br />{t("llsrv_detected_release")}: <b>{detected.release_tag || t("llsrv_release_unknown")}</b>
      </div>

      <div class="llsrv-pick">{t("llsrv_dialog_ask")}</div>
      <div class="llsrv-opts">
        {#each options as o (o.id)}
          {@const avail = !!o.available}
          <label
            class="llsrv-opt"
            class:unavail={!avail}
            class:sel={avail && selected === o.id}
            data-tooltip={t("llsrv_opt_tip")}
          >
            <input
              type="radio"
              name="llsrv-choice"
              value={o.id}
              checked={avail && selected === o.id}
              disabled={!avail}
              onchange={() => pick(o.id, avail)}
            />
            <span class="llsrv-opt-main">
              <span class="llsrv-opt-label">
                {t(o.label_key)}
                {#if o.recommended && avail}<span class="llsrv-rec">β˜… {t("llsrv_recommended")}</span>{/if}
                {#if !avail}<span class="llsrv-na">β€” {t("llsrv_not_available")}</span>{/if}
              </span>
              <span class="llsrv-opt-reason">{fmt(o.reason_key, o.params)}</span>
            </span>
          </label>
        {/each}
      </div>

      <div class="llsrv-actions">
        <button type="button" class="llsrv-cancel" data-tooltip={t("llsrv_dialog_cancel")} onclick={close}>
          {t("llsrv_dialog_cancel")}
        </button>
        <button type="button" class="llsrv-ok" data-tooltip={t("llsrv_dialog_install")} onclick={confirm}>
          {t("llsrv_dialog_install")}
        </button>
      </div>
    {/if}
  </div>
</div>