基于WebView2实现的Win11桌面框架,利用插件自定义
Moli
2026-05-04 b3bde921167ee0a7dc21cf7e3d642840a0fcc11e
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
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>LLM Command Palette</title>
  <style>
    :root {
      color-scheme: dark;
      font-family: "Segoe UI", system-ui, sans-serif;
      background: transparent;
    }
 
    * {
      box-sizing: border-box;
    }
 
    html,
    body {
      width: 100%;
      min-height: 100%;
      margin: 0;
      background: transparent;
      overflow: hidden;
    }
 
    body {
      padding: 8px;
    }
 
    .shell {
      width: 100%;
      color: rgba(255, 255, 255, 0.92);
    }
 
    .bar,
    .output {
      width: 100%;
      border: 1px solid rgba(255, 255, 255, 0.16);
      background: rgba(30, 28, 27, 0.74);
      box-shadow: 0 18px 42px rgba(0, 0, 0, 0.24);
      backdrop-filter: blur(18px);
    }
 
    .bar {
      display: grid;
      grid-template-columns: 24px minmax(0, 1fr) 128px;
      align-items: center;
      gap: 14px;
      min-height: 56px;
      padding: 0 18px;
      border-radius: 20px;
    }
 
    .mark {
      width: 18px;
      height: 18px;
      color: #ff8258;
      display: grid;
      place-items: center;
      font-size: 20px;
      line-height: 1;
      transform-origin: center;
    }
 
    .mark.busy {
      animation: spin 1.1s linear infinite;
    }
 
    @keyframes spin {
      to { transform: rotate(360deg); }
    }
 
    textarea {
      width: 100%;
      min-height: 24px;
      max-height: 78px;
      resize: none;
      border: 0;
      outline: none;
      color: rgba(255, 255, 255, 0.94);
      background: transparent;
      font: 600 16px/1.35 "Segoe UI", system-ui, sans-serif;
      letter-spacing: 0;
      overflow-y: auto;
    }
 
    textarea::placeholder {
      color: rgba(255, 255, 255, 0.46);
    }
 
    select {
      width: 100%;
      height: 34px;
      border: 0;
      outline: none;
      color: rgba(255, 255, 255, 0.82);
      background: rgba(255, 255, 255, 0.08);
      border-radius: 8px;
      padding: 0 8px;
      font: 600 13px/1 "Segoe UI", system-ui, sans-serif;
    }
 
    .output {
      display: none;
      margin-top: 8px;
      max-height: 50vh;
      overflow: auto;
      border-radius: 16px;
      padding: 16px 18px 14px;
      font-size: 14px;
      line-height: 1.55;
      white-space: pre-wrap;
    }
 
    .output.open {
      display: block;
    }
 
    .actions {
      display: flex;
      justify-content: flex-end;
      gap: 8px;
      margin-top: 12px;
    }
 
    button {
      width: 30px;
      height: 30px;
      border: 0;
      border-radius: 8px;
      color: rgba(255, 255, 255, 0.84);
      background: rgba(255, 255, 255, 0.08);
      cursor: pointer;
    }
 
    .error {
      color: #ffad9b;
    }
  </style>
</head>
<body>
  <main class="shell">
    <section class="bar">
      <div id="mark" class="mark" aria-hidden="true">*</div>
      <textarea id="prompt" rows="1" placeholder="What can I help you with today?"></textarea>
      <select id="model" title="Model"></select>
    </section>
    <section id="output" class="output" aria-live="polite">
      <div id="text"></div>
      <div class="actions">
        <button id="copy" title="Copy">C</button>
        <button id="collapse" title="Collapse">-</button>
      </div>
    </section>
  </main>
 
  <script>
    const promptInput = document.getElementById("prompt");
    const modelSelect = document.getElementById("model");
    const output = document.getElementById("output");
    const text = document.getElementById("text");
    const mark = document.getElementById("mark");
    const copy = document.getElementById("copy");
    const collapse = document.getElementById("collapse");
    let activeRequestId = null;
    let currentText = "";
 
    function setBusy(busy) {
      mark.classList.toggle("busy", busy);
      mark.textContent = busy ? "*" : "*";
    }
 
    function openOutput() {
      output.classList.add("open");
      window.moliDesktop?.requestResize?.({ width: document.body.scrollWidth, height: Math.min(window.innerHeight, document.body.scrollHeight) }).catch(() => {});
    }
 
    function setError(message) {
      currentText = message;
      text.className = "error";
      text.textContent = message;
      openOutput();
    }
 
    async function loadModels() {
      try {
        const models = await window.moliDesktop.llm.getModels();
        modelSelect.innerHTML = "";
        for (const model of models) {
          const option = document.createElement("option");
          option.value = model.id;
          option.textContent = model.label || model.id;
          option.title = `${model.provider} / ${model.id}`;
          modelSelect.appendChild(option);
        }
 
        if (models.length === 0) {
          const option = document.createElement("option");
          option.textContent = "No models";
          option.value = "";
          modelSelect.appendChild(option);
        }
      } catch {
        modelSelect.innerHTML = "<option value=\"\">No models</option>";
      }
    }
 
    async function submitPrompt() {
      const content = promptInput.value.trim();
      const modelId = modelSelect.value;
      if (!content || !modelId || activeRequestId) return;
 
      currentText = "";
      text.className = "";
      text.textContent = "";
      openOutput();
      setBusy(true);
 
      try {
        const response = await window.moliDesktop.llm.streamChat({
          modelId,
          messages: [{ role: "user", content }],
          options: {}
        });
        activeRequestId = response.requestId;
      } catch (error) {
        activeRequestId = null;
        setBusy(false);
        setError(error?.message || "LLM request could not be started.");
      }
    }
 
    promptInput.addEventListener("keydown", (event) => {
      if (event.key === "Enter" && !event.shiftKey) {
        event.preventDefault();
        submitPrompt();
      }
 
      if (event.key === "Escape") {
        output.classList.remove("open");
      }
    });
 
    promptInput.addEventListener("input", () => {
      promptInput.style.height = "0";
      promptInput.style.height = `${Math.min(promptInput.scrollHeight, 78)}px`;
    });
 
    collapse.addEventListener("click", () => output.classList.remove("open"));
    copy.addEventListener("click", () => navigator.clipboard?.writeText(currentText).catch(() => {}));
 
    window.addEventListener("DOMContentLoaded", loadModels);
    window.moliDesktop?.llm?.onDelta?.((event) => {
      if (event.requestId !== activeRequestId) return;
      currentText += event.delta || "";
      text.textContent = currentText;
      output.scrollTop = output.scrollHeight;
    });
    window.moliDesktop?.llm?.onCompleted?.((event) => {
      if (event.requestId !== activeRequestId) return;
      activeRequestId = null;
      setBusy(false);
    });
    window.moliDesktop?.llm?.onError?.((event) => {
      if (event.requestId !== activeRequestId) return;
      activeRequestId = null;
      setBusy(false);
      setError(event.message || "LLM request failed.");
    });
  </script>
</body>
</html>