基于WebView2实现的Win11桌面框架,利用插件自定义
Moli
2026-05-05 4df6165fa93c7caeea86385312415e312de4d11f
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
<!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,
    .memory {
      width: 100%;
      border: 1px solid rgba(255, 255, 255, 0.14);
      background: rgba(28, 26, 25, 0.72);
      box-shadow: 0 12px 30px rgba(0, 0, 0, 0.18);
      backdrop-filter: blur(18px);
    }
 
    .bar {
      display: grid;
      grid-template-columns: minmax(90px, 112px) 22px minmax(0, 1fr) 30px minmax(112px, 128px);
      align-items: center;
      gap: 14px;
      min-height: 56px;
      padding: 0 20px;
      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);
    }
 
    input {
      width: 100%;
      height: 34px;
      border: 1px solid rgba(255, 255, 255, 0.13);
      outline: none;
      color: rgba(255, 255, 255, 0.88);
      background: rgba(255, 255, 255, 0.07);
      border-radius: 8px;
      padding: 0 30px 0 10px;
      font: 600 13px/1 "Segoe UI", system-ui, sans-serif;
      letter-spacing: 0;
      transition: border-color 120ms ease, background 120ms ease, box-shadow 120ms ease;
    }
 
    .combo {
      position: relative;
      width: 100%;
      min-width: 0;
    }
 
    .combo-input {
      display: none;
    }
 
    .combo-trigger {
      width: 100%;
      height: 34px;
      border: 0;
      border-radius: 8px;
      color: rgba(255, 255, 255, 0.82);
      background: transparent;
      padding: 0 28px 0 10px;
      text-align: left;
      font: 600 13px/1 "Segoe UI", system-ui, sans-serif;
      letter-spacing: 0;
      cursor: pointer;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      position: relative;
      transition: border-color 120ms ease, background 120ms ease, box-shadow 120ms ease;
    }
 
    .combo-trigger::after {
      content: "";
      position: absolute;
      right: 11px;
      top: 13px;
      width: 7px;
      height: 7px;
      border-right: 2px solid rgba(255, 255, 255, 0.72);
      border-bottom: 2px solid rgba(255, 255, 255, 0.72);
      transform: rotate(45deg);
    }
 
    input:hover,
    input:focus,
    .combo.open .combo-trigger,
    .combo-trigger:hover,
    .combo-trigger:focus {
      background-color: rgba(255, 255, 255, 0.08);
      box-shadow: none;
    }
 
    .combo-menu {
      position: absolute;
      left: 0;
      right: 0;
      top: calc(100% + 6px);
      z-index: 10;
      display: none;
      max-height: 184px;
      overflow-y: auto;
      padding: 6px;
      border: 1px solid rgba(255, 255, 255, 0.13);
      border-radius: 10px;
      background: rgba(28, 26, 25, 0.92);
      box-shadow: 0 12px 28px rgba(0, 0, 0, 0.26);
      backdrop-filter: blur(16px);
    }
 
    .combo.open .combo-menu {
      display: block;
    }
 
    .combo-option {
      min-height: 36px;
      display: flex;
      align-items: center;
      border-radius: 7px;
      padding: 0 10px;
      color: rgba(255, 255, 255, 0.86);
      font: 600 13px/1.2 "Segoe UI", system-ui, sans-serif;
      cursor: pointer;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
 
    .combo-option:hover,
    .combo-option.selected {
      background: rgba(255, 130, 88, 0.16);
      color: rgba(255, 255, 255, 0.96);
    }
 
    .output,
    .memory {
      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,
    .memory.open {
      display: block;
      animation: panelIn 160ms ease-out;
    }
 
    @keyframes panelIn {
      from { opacity: 0; transform: translateY(-3px); }
      to { opacity: 1; transform: translateY(0); }
    }
 
    .actions,
    .memory-actions {
      display: flex;
      justify-content: flex-end;
      gap: 8px;
      margin-top: 12px;
    }
 
    .memory-row {
      display: grid;
      grid-template-columns: minmax(0, 1fr) 32px;
      gap: 8px;
      align-items: start;
      padding: 8px 0;
      border-top: 1px solid rgba(255, 255, 255, 0.08);
      white-space: normal;
    }
 
    .memory-row:first-child {
      border-top: 0;
    }
 
    .memory-content {
      color: rgba(255, 255, 255, 0.86);
      overflow-wrap: anywhere;
    }
 
    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;
      font: 700 13px/1 "Segoe UI", system-ui, sans-serif;
    }
 
    button:disabled,
    input:disabled {
      cursor: not-allowed;
      opacity: 0.52;
    }
 
    .error {
      color: #ffad9b;
    }
  </style>
</head>
<body>
  <main class="shell">
    <section class="bar">
      <div class="combo" id="projectCombo">
        <input id="project" class="combo-input" aria-hidden="true">
        <button id="projectButton" class="combo-trigger" type="button" title="AiChat project">Project</button>
        <div id="projectMenu" class="combo-menu" role="listbox"></div>
      </div>
      <div id="mark" class="mark" aria-hidden="true">*</div>
      <textarea id="prompt" rows="1" placeholder="What can I help you with today?"></textarea>
      <button id="memoryToggle" title="Memory">M</button>
      <div class="combo" id="modelCombo">
        <input id="model" class="combo-input" aria-hidden="true">
        <button id="modelButton" class="combo-trigger" type="button" title="Model">Model</button>
        <div id="modelMenu" class="combo-menu" role="listbox"></div>
      </div>
    </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>
    <section id="memory" class="memory" aria-live="polite">
      <div id="memoryList"></div>
      <div class="memory-actions">
        <input id="memoryInput" placeholder="Save memory">
        <button id="saveMemory" title="Save memory">+</button>
        <button id="refreshMemory" title="Refresh memory">R</button>
      </div>
    </section>
  </main>
 
  <script>
    const promptInput = document.getElementById("prompt");
    const projectSelect = document.getElementById("project");
    const projectCombo = document.getElementById("projectCombo");
    const projectButton = document.getElementById("projectButton");
    const projectMenu = document.getElementById("projectMenu");
    const modelSelect = document.getElementById("model");
    const modelCombo = document.getElementById("modelCombo");
    const modelButton = document.getElementById("modelButton");
    const modelMenu = document.getElementById("modelMenu");
    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");
    const memoryToggle = document.getElementById("memoryToggle");
    const memoryPanel = document.getElementById("memory");
    const memoryList = document.getElementById("memoryList");
    const memoryInput = document.getElementById("memoryInput");
    const saveMemory = document.getElementById("saveMemory");
    const refreshMemory = document.getElementById("refreshMemory");
 
    if (!window.moliDesktop) {
      window.moliDesktop = {
        getConfig: async () => ({
          mode: "provider-direct",
          aichat: { memory: { enabledInProviderDirect: false, defaultScope: "PROJECT", recallLimit: 8 } }
        }),
        requestResize: async () => {},
        llm: {
          getProjects: async () => [],
          getModels: async () => [],
          getMemoryCapabilities: async () => ({ canUseMemory: false, canViewMemory: false, canManageMemory: false }),
          getMemories: async () => ({ items: [] }),
          createMemory: async () => ({}),
          deleteMemory: async () => {},
          recallMemories: async () => ({ items: [] }),
          streamChat: async () => ({ requestId: "preview" }),
          onDelta: () => () => {},
          onCompleted: () => () => {},
          onError: () => () => {}
        }
      };
    }
 
    let activeRequestId = null;
    let currentText = "";
    let config = { mode: "provider-direct", aichat: { memory: { enabledInProviderDirect: true, defaultScope: "PROJECT", recallLimit: 8 } } };
    let memoryCapabilities = { canUseMemory: false, canViewMemory: false, canManageMemory: false };
 
    function mode() {
      return config.mode || "provider-direct";
    }
 
    function selectedProjectId() {
      return projectSelect.value || config.aichat?.defaultProjectId || "";
    }
 
    function setBusy(busy) {
      mark.classList.toggle("busy", busy);
      mark.textContent = busy ? "*" : "*";
    }
 
    function openOutput() {
      output.classList.add("open");
      requestResize();
    }
 
    function requestResize() {
      const openMenu = projectCombo.classList.contains("open")
        ? projectMenu
        : modelCombo.classList.contains("open") ? modelMenu : null;
      const menuBottom = openMenu ? openMenu.getBoundingClientRect().bottom + 8 : 72;
      window.moliDesktop?.requestResize?.({
        width: document.body.scrollWidth,
        height: Math.max(72, Math.ceil(menuBottom))
      }).catch(() => {});
    }
 
    function setError(message) {
      currentText = message;
      text.className = "error";
      text.textContent = message;
      openOutput();
    }
 
    const projectDropdown = { input: projectSelect, root: projectCombo, button: projectButton, menu: projectMenu };
    const modelDropdown = { input: modelSelect, root: modelCombo, button: modelButton, menu: modelMenu };
 
    function setupDropdown(dropdown) {
      dropdown.button.addEventListener("click", () => {
        const willOpen = !dropdown.root.classList.contains("open");
        closeDropdowns();
        dropdown.root.classList.toggle("open", willOpen);
        requestResize();
      });
    }
 
    function closeDropdowns() {
      projectCombo.classList.remove("open");
      modelCombo.classList.remove("open");
      requestResize();
    }
 
    function setDropdownFallback(dropdown, label, title) {
      dropdown.input.value = "";
      dropdown.button.textContent = label;
      dropdown.button.title = title || label;
      dropdown.menu.innerHTML = "";
      const option = document.createElement("div");
      option.className = "combo-option";
      option.textContent = label;
      option.title = title || label;
      dropdown.menu.appendChild(option);
    }
 
    function setDropdownOptions(dropdown, items, preferredValue, fallbackLabel) {
      dropdown.menu.innerHTML = "";
      if (!items.length) {
        setDropdownFallback(dropdown, fallbackLabel);
        return;
      }
 
      let selected = items.find((item) => item.value === preferredValue) || items[0];
      for (const item of items) {
        const option = document.createElement("div");
        option.className = `combo-option${item.value === selected.value ? " selected" : ""}`;
        option.textContent = item.label;
        option.title = item.title || item.label;
        option.dataset.value = item.value;
        option.addEventListener("click", () => {
          selected = item;
          setDropdownValue(dropdown, item);
          closeDropdowns();
        });
        dropdown.menu.appendChild(option);
      }
 
      setDropdownValue(dropdown, selected, false);
    }
 
    function setDropdownValue(dropdown, item, notify = true) {
      dropdown.input.value = item.value;
      dropdown.button.textContent = item.label;
      dropdown.button.title = item.title || item.label;
      for (const option of dropdown.menu.children) {
        option.classList.toggle("selected", option.dataset.value === item.value);
      }
      if (notify) {
        dropdown.input.dispatchEvent(new Event("change"));
      }
    }
 
    async function loadConfig() {
      try {
        config = { ...config, ...(await window.moliDesktop.getConfig() || {}) };
      } catch {
      }
    }
 
    async function loadProjects() {
      try {
        const projects = await window.moliDesktop.llm.getProjects();
        const items = projects.map((project) => ({
          value: project.id,
          label: project.name || project.id,
          title: `${project.projectType || "CHAT"} / ${project.memoryMode || "ISOLATED"}`
        }));
        setDropdownOptions(projectDropdown, items, config.aichat?.defaultProjectId || "", "No project");
      } catch (error) {
        setDropdownFallback(projectDropdown, "Project error", error?.message || "AiChat projects could not be loaded");
      }
    }
 
    async function loadModels() {
      try {
        const models = await window.moliDesktop.llm.getModels();
        const items = models.map((model) => ({
          value: model.id,
          label: model.label || model.id,
          title: `${model.provider} / ${model.id}`
        }));
        setDropdownOptions(modelDropdown, items, config.defaultModel || "", "No models");
      } catch {
        setDropdownFallback(modelDropdown, "Model error");
      }
    }
 
    async function loadMemoryCapabilities() {
      try {
        memoryCapabilities = await window.moliDesktop.llm.getMemoryCapabilities();
      } catch {
        memoryCapabilities = { canUseMemory: false, canViewMemory: false, canManageMemory: false };
      }
 
      const memoryEnabled = mode() === "provider-direct" && config.aichat?.memory?.enabledInProviderDirect;
      memoryToggle.disabled = !memoryEnabled || !memoryCapabilities.canViewMemory;
      memoryInput.disabled = !memoryCapabilities.canManageMemory;
      saveMemory.disabled = !memoryCapabilities.canManageMemory;
      refreshMemory.disabled = !memoryCapabilities.canViewMemory;
    }
 
    async function recallMemory(content) {
      if (mode() !== "provider-direct" || !config.aichat?.memory?.enabledInProviderDirect || !memoryCapabilities.canUseMemory) {
        return [];
      }
 
      const projectId = selectedProjectId();
      try {
        const result = await window.moliDesktop.llm.recallMemories({
          query: content,
          scope: config.aichat.memory.defaultScope || "PROJECT",
          projectId,
          limit: config.aichat.memory.recallLimit || 8
        });
        return Array.isArray(result?.items) ? result.items : [];
      } catch {
        return [];
      }
    }
 
    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 memories = await recallMemory(content);
        const messages = [];
        if (memories.length > 0) {
          const memoryText = memories.map((item) => `- ${item.content || ""}`).filter(Boolean).join("\n");
          if (memoryText) {
            messages.push({ role: "system", content: `Relevant memory:\n${memoryText}` });
          }
        }
        messages.push({ role: "user", content });
 
        const response = await window.moliDesktop.llm.streamChat({
          mode: mode(),
          projectId: selectedProjectId(),
          modelId,
          messages,
          options: {}
        });
        activeRequestId = response.requestId;
      } catch (error) {
        activeRequestId = null;
        setBusy(false);
        setError(error?.message || "LLM request could not be started.");
      }
    }
 
    async function refreshMemories() {
      if (!memoryCapabilities.canViewMemory) return;
      const projectId = selectedProjectId();
      memoryList.textContent = "Loading...";
      try {
        const scope = config.aichat?.memory?.defaultScope === "GLOBAL" ? "GLOBAL" : "PROJECT";
        const result = await window.moliDesktop.llm.getMemories({ scope, projectId });
        const items = Array.isArray(result) ? result : Array.isArray(result?.items) ? result.items : [];
        renderMemories(items);
      } catch (error) {
        memoryList.textContent = error?.message || "Memory unavailable.";
      }
    }
 
    function renderMemories(items) {
      memoryList.innerHTML = "";
      if (!items.length) {
        memoryList.textContent = "No memory";
        return;
      }
 
      for (const item of items) {
        const row = document.createElement("div");
        row.className = "memory-row";
        const content = document.createElement("div");
        content.className = "memory-content";
        content.textContent = item.content || item.id || "";
        row.appendChild(content);
        const remove = document.createElement("button");
        remove.textContent = "x";
        remove.title = "Delete memory";
        remove.disabled = !memoryCapabilities.canManageMemory;
        remove.addEventListener("click", async () => {
          if (!item.id) return;
          await window.moliDesktop.llm.deleteMemory(item.id);
          refreshMemories();
        });
        row.appendChild(remove);
        memoryList.appendChild(row);
      }
    }
 
    promptInput.addEventListener("keydown", (event) => {
      if (event.key === "Enter" && !event.shiftKey) {
        event.preventDefault();
        submitPrompt();
      }
 
      if (event.key === "Escape") {
        output.classList.remove("open");
        memoryPanel.classList.remove("open");
        requestResize();
      }
    });
 
    promptInput.addEventListener("input", () => {
      promptInput.style.height = "0";
      promptInput.style.height = `${Math.min(promptInput.scrollHeight, 78)}px`;
    });
 
    projectSelect.addEventListener("change", () => {
      if (memoryPanel.classList.contains("open")) {
        refreshMemories();
      }
    });
 
    memoryToggle.addEventListener("click", () => {
      memoryPanel.classList.toggle("open");
      if (memoryPanel.classList.contains("open")) {
        refreshMemories();
      }
      requestResize();
    });
 
    refreshMemory.addEventListener("click", refreshMemories);
    saveMemory.addEventListener("click", async () => {
      const content = memoryInput.value.trim();
      if (!content || !memoryCapabilities.canManageMemory) return;
      await window.moliDesktop.llm.createMemory({
        scope: config.aichat?.memory?.defaultScope === "GLOBAL" ? "GLOBAL" : "PROJECT",
        projectId: selectedProjectId(),
        content
      });
      memoryInput.value = "";
      refreshMemories();
    });
 
    collapse.addEventListener("click", () => {
      output.classList.remove("open");
      requestResize();
    });
    copy.addEventListener("click", () => navigator.clipboard?.writeText(currentText).catch(() => {}));
    document.addEventListener("click", (event) => {
      if (!projectCombo.contains(event.target) && !modelCombo.contains(event.target)) {
        closeDropdowns();
      }
    });
 
    window.addEventListener("DOMContentLoaded", async () => {
      setupDropdown(projectDropdown);
      setupDropdown(modelDropdown);
      await loadConfig();
      await Promise.all([loadProjects(), loadModels(), loadMemoryCapabilities()]);
    });
 
    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>