From df6a437518d89eebfa1a583e09ec99c462e64e47 Mon Sep 17 00:00:00 2001
From: Moli <13026959681@163.com>
Date: 星期三, 12 八月 2026 20:42:12 +0800
Subject: [PATCH] 移除发布文件的跟踪
---
samples/plugins/sample.llm-command-palette/index.html | 1177 ++++++++++++++++++++++++++++++++++++++++++++++++++--------
1 files changed, 1,005 insertions(+), 172 deletions(-)
diff --git a/samples/plugins/sample.llm-command-palette/index.html b/samples/plugins/sample.llm-command-palette/index.html
index 9617dd9..6057f32 100644
--- a/samples/plugins/sample.llm-command-palette/index.html
+++ b/samples/plugins/sample.llm-command-palette/index.html
@@ -1,5 +1,6 @@
<!doctype html>
<html lang="en">
+
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
@@ -7,8 +8,21 @@
<style>
:root {
color-scheme: dark;
- font-family: "Segoe UI", system-ui, sans-serif;
- background: transparent;
+ --bar-bg: rgba(29, 28, 27, 0.78);
+ --bar-bg-solid: rgb(35, 34, 33);
+ --panel-bg: rgba(25, 24, 23, 0.82);
+ --line: rgba(255, 255, 255, 0.12);
+ --line-strong: rgba(255, 255, 255, 0.18);
+ --text: rgba(255, 255, 255, 0.9);
+ --muted: rgba(255, 255, 255, 0.56);
+ --dim: rgba(255, 255, 255, 0.38);
+ --accent: #e76f45;
+ --error: #ff8b7f;
+ --ok: #8fe0bb;
+ --shadow: 0 16px 38px rgba(0, 0, 0, 0.22);
+ --panel-max-height: 420px;
+ --response-max-height: 376px;
+ font-family: "Segoe UI", "Microsoft YaHei UI", system-ui, sans-serif;
}
* {
@@ -18,256 +32,1075 @@
html,
body {
width: 100%;
- min-height: 100%;
+ min-width: 0;
margin: 0;
- background: transparent;
overflow: hidden;
+ background: transparent;
}
body {
+ color: var(--text);
+ user-select: none;
+ }
+
+ .palette-shell {
+ width: 100%;
+ min-width: 360px;
+ min-height: 250px;
padding: 8px;
}
- .shell {
+ .palette {
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: 1px solid var(--line);
border-radius: 20px;
+ background: var(--bar-bg-solid);
+ background: var(--bar-bg);
+ box-shadow: var(--shadow), inset 0 1px 0 rgba(255, 255, 255, 0.06);
+ overflow: visible;
}
- .mark {
+ @supports ((backdrop-filter: blur(18px)) or (-webkit-backdrop-filter: blur(18px))) {
+ .palette {
+ -webkit-backdrop-filter: blur(18px) saturate(128%);
+ backdrop-filter: blur(18px) saturate(128%);
+ }
+ }
+
+ .input-row {
+ display: grid;
+ grid-template-columns: 24px minmax(96px, 154px) 1fr auto minmax(92px, 132px);
+ align-items: center;
+ gap: 8px;
+ min-height: 54px;
+ padding: 0 14px 0 18px;
+ }
+
+ .status-mark {
width: 18px;
height: 18px;
- color: #ff8258;
display: grid;
place-items: center;
- font-size: 20px;
- line-height: 1;
- transform-origin: center;
+ border-radius: 999px;
+ color: var(--accent);
+ flex: 0 0 auto;
}
- .mark.busy {
- animation: spin 1.1s linear infinite;
+ .status-mark::before {
+ content: "";
+ width: 15px;
+ height: 15px;
+ display: block;
+ background:
+ conic-gradient(from 0deg, transparent 0 9deg, currentColor 10deg 18deg, transparent 19deg 39deg, currentColor 40deg 48deg, transparent 49deg 69deg, currentColor 70deg 78deg, transparent 79deg 99deg, currentColor 100deg 108deg, transparent 109deg 129deg, currentColor 130deg 138deg, transparent 139deg 159deg, currentColor 160deg 168deg, transparent 169deg 189deg, currentColor 190deg 198deg, transparent 199deg 219deg, currentColor 220deg 228deg, transparent 229deg 249deg, currentColor 250deg 258deg, transparent 259deg 279deg, currentColor 280deg 288deg, transparent 289deg 309deg, currentColor 310deg 318deg, transparent 319deg 339deg, currentColor 340deg 348deg, transparent 349deg);
+ -webkit-mask: radial-gradient(circle, transparent 0 3px, #000 3.2px 7.5px, transparent 7.8px);
+ mask: radial-gradient(circle, transparent 0 3px, #000 3.2px 7.5px, transparent 7.8px);
}
- @keyframes spin {
- to { transform: rotate(360deg); }
+ .status-mark.loading::before {
+ width: 16px;
+ height: 16px;
+ border: 2px solid rgba(255, 255, 255, 0.18);
+ border-top-color: var(--accent);
+ border-radius: 999px;
+ background: none;
+ -webkit-mask: none;
+ mask: none;
+ animation: spin 0.9s linear infinite;
}
- 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;
+ .status-mark.error {
+ color: var(--error);
}
- textarea::placeholder {
- color: rgba(255, 255, 255, 0.46);
+ .status-mark.error::before {
+ width: 10px;
+ height: 10px;
+ border-radius: 999px;
+ background: currentColor;
+ -webkit-mask: none;
+ mask: none;
+ box-shadow: 0 0 0 4px rgba(255, 139, 127, 0.14);
}
- select {
+ .select-wrap {
+ min-width: 0;
+ position: relative;
+ }
+
+ .select-wrap::after {
+ content: "";
+ position: absolute;
+ right: 8px;
+ top: 50%;
+ width: 7px;
+ height: 7px;
+ border-right: 1.5px solid var(--dim);
+ border-bottom: 1.5px solid var(--dim);
+ transform: translateY(-65%) rotate(45deg);
+ pointer-events: none;
+ }
+
+ .select-control {
width: 100%;
height: 34px;
- border: 0;
+ line-height: 32px !important;
+ padding: 0 22px 0 10px;
+ border: 1px solid transparent;
+ border-radius: 10px;
+ background: rgba(255, 255, 255, 0.04);
+ color: var(--muted);
+ font: 500 12px/1 "Segoe UI", system-ui, sans-serif;
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;
+ appearance: none;
+ white-space: nowrap;
+ text-overflow: ellipsis;
+ overflow: hidden;
+ cursor: pointer;
}
- .output {
+ .select-control:hover,
+ .select-control:focus-visible,
+ .select-control.open {
+ color: var(--text);
+ border-color: var(--line-strong);
+ background: rgba(255, 255, 255, 0.07);
+ }
+
+ .select-menu {
+ position: absolute;
+ left: 0;
+ right: 0;
+ top: calc(100% + 6px);
+ z-index: 30;
display: none;
- margin-top: 8px;
- max-height: 50vh;
+ max-height: 190px;
overflow: auto;
- border-radius: 16px;
- padding: 16px 18px 14px;
- font-size: 14px;
- line-height: 1.55;
- white-space: pre-wrap;
+ padding: 6px;
+ border: 1px solid var(--line-strong);
+ border-radius: 10px;
+ background: rgba(31, 30, 29, 0.96);
+ box-shadow: 0 12px 28px rgba(0, 0, 0, 0.26);
}
- .output.open {
+ .select-control.open + .select-menu {
display: block;
+ }
+
+ .select-option {
+ min-height: 30px;
+ display: flex;
+ align-items: center;
+ padding: 0 8px;
+ border-radius: 7px;
+ color: var(--muted);
+ font: 500 12px/1.2 "Segoe UI", system-ui, sans-serif;
+ overflow: hidden;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+ cursor: pointer;
+ }
+
+ .select-option:hover,
+ .select-option.selected {
+ color: var(--text);
+ background: rgba(231, 111, 69, 0.14);
+ }
+
+ .prompt-editor {
+ width: 100%;
+ min-width: 0;
+ max-height: 66px;
+ min-height: 24px;
+ padding: 2px 0;
+ border: 0;
+ resize: none;
+ overflow: hidden;
+ outline: none;
+ background: transparent;
+ color: var(--text);
+ font: 500 16px/1.45 "Segoe UI", "Microsoft YaHei UI", system-ui, sans-serif;
+ letter-spacing: 0;
+ }
+
+ .prompt-editor:empty::before {
+ content: attr(data-placeholder);
+ color: var(--muted);
+ pointer-events: none;
+ }
+
+ .tool-button {
+ width: 32px;
+ height: 32px;
+ display: grid;
+ place-items: center;
+ border: 1px solid transparent;
+ border-radius: 10px;
+ background: rgba(255, 255, 255, 0.04);
+ color: var(--muted);
+ font: 700 12px/1 "Segoe UI", system-ui, sans-serif;
+ cursor: pointer;
+ }
+
+ .tool-button:hover,
+ .tool-button:focus-visible,
+ .tool-button.active {
+ color: var(--text);
+ border-color: var(--line-strong);
+ background: rgba(255, 255, 255, 0.08);
+ outline: none;
+ }
+
+ .tool-button.disabled,
+ .icon-action.disabled,
+ .memory-input.disabled {
+ cursor: not-allowed;
+ opacity: 0.52;
+ pointer-events: none;
+ }
+
+ .output-panel,
+ .memory-panel {
+ max-height: 0;
+ opacity: 0;
+ overflow: hidden;
+ border-top: 1px solid transparent;
+ transition: max-height 160ms ease, opacity 140ms ease, border-color 140ms ease;
+ }
+
+ .output-panel.open,
+ .memory-panel.open {
+ max-height: var(--panel-max-height);
+ opacity: 1;
+ border-top-color: rgba(255, 255, 255, 0.08);
+ }
+
+ .output-inner {
+ max-height: var(--response-max-height);
+ overflow: auto;
+ padding: 14px 18px 8px 18px;
+ color: rgba(255, 255, 255, 0.86);
+ font: 14px/1.58 "Segoe UI", "Microsoft YaHei UI", system-ui, sans-serif;
+ white-space: pre-wrap;
+ user-select: text;
+ }
+
+ .output-inner.error {
+ color: var(--error);
+ }
+
+ .output-inner:empty::before {
+ content: attr(data-placeholder);
+ color: var(--dim);
+ }
+
+ .stream-caret {
+ display: inline-block;
+ width: 6px;
+ height: 1em;
+ margin-left: 2px;
+ vertical-align: -2px;
+ border-radius: 999px;
+ background: var(--accent);
+ animation: pulse 1s ease-in-out infinite;
}
.actions {
display: flex;
justify-content: flex-end;
- gap: 8px;
- margin-top: 12px;
+ gap: 6px;
+ padding: 0 12px 10px;
}
- button {
+ .icon-action {
width: 30px;
- height: 30px;
- border: 0;
+ height: 28px;
+ border: 1px solid transparent;
border-radius: 8px;
- color: rgba(255, 255, 255, 0.84);
- background: rgba(255, 255, 255, 0.08);
+ background: transparent;
+ color: var(--muted);
+ font: 600 13px/1 "Segoe UI", system-ui, sans-serif;
cursor: pointer;
}
- .error {
- color: #ffad9b;
+ .icon-action:hover,
+ .icon-action:focus-visible {
+ color: var(--text);
+ border-color: var(--line);
+ background: rgba(255, 255, 255, 0.06);
+ outline: none;
+ }
+
+ .memory-panel.open {
+ background: rgba(0, 0, 0, 0.1);
+ }
+
+ .memory-inner {
+ display: grid;
+ grid-template-columns: 1fr auto;
+ gap: 8px;
+ padding: 12px 14px;
+ }
+
+ .memory-list {
+ min-height: 34px;
+ max-height: 134px;
+ overflow: auto;
+ color: var(--muted);
+ font-size: 12px;
+ user-select: text;
+ }
+
+ .memory-item {
+ display: flex;
+ align-items: flex-start;
+ gap: 8px;
+ padding: 7px 0;
+ border-bottom: 1px solid rgba(255, 255, 255, 0.06);
+ }
+
+ .memory-item:last-child {
+ border-bottom: 0;
+ }
+
+ .memory-text {
+ min-width: 0;
+ flex: 1;
+ color: rgba(255, 255, 255, 0.78);
+ line-height: 1.45;
+ }
+
+ .memory-form {
+ display: flex;
+ align-items: center;
+ gap: 6px;
+ }
+
+ .memory-input {
+ width: 180px;
+ height: 32px;
+ border: 1px solid var(--line);
+ border-radius: 9px;
+ padding: 0 10px;
+ outline: none;
+ background: rgba(255, 255, 255, 0.05);
+ color: var(--text);
+ font-size: 12px;
+ display: flex;
+ align-items: center;
+ overflow: hidden;
+ white-space: nowrap;
+ }
+
+ .memory-input:focus-visible {
+ border-color: var(--line-strong);
+ }
+
+ .memory-input:empty::before {
+ content: attr(data-placeholder);
+ color: var(--dim);
+ pointer-events: none;
+ }
+
+ .memory-status {
+ grid-column: 1 / -1;
+ color: var(--dim);
+ font-size: 12px;
+ min-height: 16px;
+ }
+
+ @keyframes spin {
+ to {
+ transform: rotate(360deg);
+ }
+ }
+
+ @keyframes pulse {
+ 0%,
+ 100% {
+ opacity: 0.25;
+ }
+
+ 50% {
+ opacity: 0.9;
+ }
+ }
+
+ @media (max-width: 560px) {
+ .palette-shell {
+ min-width: 300px;
+ padding: 6px;
+ }
+
+ .input-row {
+ grid-template-columns: 22px minmax(74px, 104px) 1fr 30px minmax(76px, 96px);
+ gap: 6px;
+ padding: 0 10px 0 13px;
+ }
+
+ select {
+ padding-left: 7px;
+ font-size: 11px;
+ }
+
+ textarea {
+ font-size: 14px;
+ }
+
+ .memory-inner {
+ grid-template-columns: 1fr;
+ }
+
+ .memory-input {
+ width: 100%;
+ }
}
</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 class="palette-shell" role="main">
+ <div class="palette" role="region" aria-label="LLM command palette">
+ <div class="input-row">
+ <div id="statusMark" class="status-mark" title="Idle" aria-hidden="true"></div>
+ <div class="select-wrap">
+ <div id="projectSelect" class="select-control" role="combobox" tabindex="0" title="AiChat project"
+ aria-label="AiChat project" aria-expanded="false" data-value="">No project</div>
+ <div id="projectMenu" class="select-menu" role="listbox" aria-label="AiChat projects"></div>
+ </div>
+ <div id="promptInput" class="prompt-editor" contenteditable="true" spellcheck="true" role="textbox"
+ aria-multiline="true" data-placeholder="What can I help you with today?" aria-label="Prompt"></div>
+ <div id="memoryButton" class="tool-button" role="button" tabindex="0" title="Memories" aria-label="Memories">M</div>
+ <div class="select-wrap">
+ <div id="modelSelect" class="select-control" role="combobox" tabindex="0" title="Model" aria-label="Model"
+ aria-expanded="false" data-value="">Model</div>
+ <div id="modelMenu" class="select-menu" role="listbox" aria-label="Models"></div>
+ </div>
</div>
- </section>
- </main>
+
+ <div id="memoryPanel" class="memory-panel" role="region" aria-label="Memories">
+ <div class="memory-inner">
+ <div id="memoryList" class="memory-list"></div>
+ <div id="memoryForm" class="memory-form">
+ <div id="memoryInput" class="memory-input" contenteditable="true" role="textbox"
+ data-placeholder="Add memory" aria-label="Add memory text"></div>
+ <div id="addMemoryButton" class="icon-action" role="button" tabindex="0" title="Add memory"
+ aria-label="Add memory">+</div>
+ <div id="recallButton" class="icon-action" role="button" tabindex="0" title="Recall memories"
+ aria-label="Recall memories">R</div>
+ </div>
+ <div id="memoryStatus" class="memory-status"></div>
+ </div>
+ </div>
+
+ <div id="outputPanel" class="output-panel" aria-live="polite" role="region" aria-label="LLM response">
+ <div id="outputText" class="output-inner" data-placeholder="Waiting for response"></div>
+ <div class="actions">
+ <div id="copyButton" class="icon-action" role="button" tabindex="0" title="Copy" aria-label="Copy">C</div>
+ <div id="retryButton" class="icon-action" role="button" tabindex="0" title="Regenerate"
+ aria-label="Regenerate">R</div>
+ <div id="collapseButton" class="icon-action" role="button" tabindex="0" title="Collapse"
+ aria-label="Collapse">X</div>
+ </div>
+ </div>
+ </div>
+ </div>
<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 = "";
+ const bridge = window.moliDesktop || null;
+ const llm = bridge && bridge.llm ? bridge.llm : null;
- function setBusy(busy) {
- mark.classList.toggle("busy", busy);
- mark.textContent = busy ? "*" : "*";
+ const state = {
+ mode: "provider-direct",
+ models: [],
+ projects: [],
+ selectedModelId: "",
+ selectedProjectId: "",
+ requestId: "",
+ busy: false,
+ output: "",
+ lastPrompt: "",
+ memories: [],
+ memoryCapabilities: {
+ canUseMemory: false,
+ canViewMemory: false,
+ canManageMemory: false
+ },
+ config: {}
+ };
+
+ let _resizeTimer;
+
+ const els = {
+ statusMark: document.getElementById("statusMark"),
+ projectSelect: document.getElementById("projectSelect"),
+ projectMenu: document.getElementById("projectMenu"),
+ modelSelect: document.getElementById("modelSelect"),
+ modelMenu: document.getElementById("modelMenu"),
+ promptInput: document.getElementById("promptInput"),
+ outputPanel: document.getElementById("outputPanel"),
+ outputText: document.getElementById("outputText"),
+ memoryButton: document.getElementById("memoryButton"),
+ memoryPanel: document.getElementById("memoryPanel"),
+ memoryList: document.getElementById("memoryList"),
+ memoryForm: document.getElementById("memoryForm"),
+ memoryInput: document.getElementById("memoryInput"),
+ addMemoryButton: document.getElementById("addMemoryButton"),
+ memoryStatus: document.getElementById("memoryStatus"),
+ recallButton: document.getElementById("recallButton"),
+ copyButton: document.getElementById("copyButton"),
+ retryButton: document.getElementById("retryButton"),
+ collapseButton: document.getElementById("collapseButton")
+ };
+
+ const fallbackModels = [
+ { id: "deepseek-v4-flash", label: "DeepSeek", provider: "deepseek", supportsStreaming: true, enabled: true },
+ { id: "kimi-k2.6", label: "Kimi", provider: "kimi", supportsStreaming: true, enabled: true },
+ { id: "glm-4.7-flash", label: "GLM", provider: "bigmodel", supportsStreaming: true, enabled: true }
+ ];
+
+ const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
+
+ function getText(element) {
+ return (element.textContent || "").replace(/\u00a0/g, " ");
}
- function openOutput() {
- output.classList.add("open");
- window.moliDesktop?.requestResize?.({ width: document.body.scrollWidth, height: Math.min(window.innerHeight, document.body.scrollHeight) }).catch(() => {});
+ function setText(element, value) {
+ element.textContent = value || "";
}
- function setError(message) {
- currentText = message;
- text.className = "error";
- text.textContent = message;
- openOutput();
+ function getSelectValue(control) {
+ return control.dataset.value || "";
+ }
+
+ function setDisabled(element, disabled) {
+ element.classList.toggle("disabled", Boolean(disabled));
+ element.setAttribute("aria-disabled", disabled ? "true" : "false");
+ if (element.hasAttribute("contenteditable")) {
+ element.setAttribute("contenteditable", disabled ? "false" : "true");
+ }
+ }
+
+ function invokeOnAction(element, handler) {
+ element.addEventListener("click", (event) => {
+ if (element.classList.contains("disabled")) return;
+ handler(event);
+ });
+ element.addEventListener("keydown", (event) => {
+ if (element.classList.contains("disabled")) return;
+ if (event.key === "Enter" || event.key === " ") {
+ event.preventDefault();
+ handler(event);
+ }
+ });
+ }
+
+ function asArray(value) {
+ return Array.isArray(value) ? value : [];
+ }
+
+ function getErrorMessage(error, fallback) {
+ if (!error) return fallback;
+ if (typeof error === "string") return error;
+ return error.message || error.Message || error.code || error.Code || fallback;
+ }
+
+ function normalizeEvent(detail) {
+ const event = detail && (detail.event || detail.Event || detail);
+ return {
+ requestId: event.requestId || event.RequestId || detail.requestId || detail.RequestId || "",
+ delta: event.delta || event.Delta || "",
+ code: event.code || event.Code || "",
+ message: event.message || event.Message || ""
+ };
+ }
+
+ function setStatus(kind, title) {
+ els.statusMark.className = "status-mark" + (kind ? " " + kind : "");
+ els.statusMark.title = title || "Idle";
+ }
+
+ function setOutput(text, options = {}) {
+ state.output = text || "";
+ els.outputText.classList.toggle("error", options.error === true);
+ els.outputText.textContent = state.output;
+ if (options.streaming) {
+ const caret = document.createElement("div");
+ caret.className = "stream-caret";
+ els.outputText.appendChild(caret);
+ }
+ els.outputPanel.classList.add("open");
+ requestHostResize();
+ els.outputText.scrollTop = els.outputText.scrollHeight;
+ }
+
+ function appendOutput(delta) {
+ state.output += delta || "";
+ setOutput(state.output, { streaming: state.busy });
+ }
+
+ function collapseOutput() {
+ els.outputPanel.classList.remove("open");
+ requestHostResize(72);
+ }
+
+ function resizeTextarea() {
+ els.promptInput.style.height = "0px";
+ const height = Math.min(66, Math.max(24, els.promptInput.scrollHeight));
+ els.promptInput.style.height = height + "px";
+ }
+
+ function requestHostResize(explicitHeight) {
+ if (!bridge || typeof bridge.requestResize !== "function") return;
+ const screenHeight = window.screen && window.screen.availHeight ? window.screen.availHeight : 900;
+ const heightLimit = Math.floor(screenHeight * 0.5) + 88;
+ const height = explicitHeight || Math.min(Math.ceil(document.body.scrollHeight + 2), heightLimit);
+ if (explicitHeight != null) {
+ clearTimeout(_resizeTimer);
+ bridge.requestResize({ width: Math.ceil(document.body.scrollWidth), height }).catch(() => {});
+ return;
+ }
+ clearTimeout(_resizeTimer);
+ _resizeTimer = setTimeout(function() {
+ bridge.requestResize({ width: Math.ceil(document.body.scrollWidth), height }).catch(() => {});
+ }, 50);
+ }
+
+ function updatePanelLimits() {
+ const screenHeight = window.screen && window.screen.availHeight ? window.screen.availHeight : window.innerHeight || 900;
+ const panelMax = Math.max(180, Math.floor(screenHeight * 0.5));
+ document.documentElement.style.setProperty("--panel-max-height", panelMax + "px");
+ document.documentElement.style.setProperty("--response-max-height", Math.max(132, panelMax - 44) + "px");
+ }
+
+ function closeSelects() {
+ for (const control of [els.projectSelect, els.modelSelect]) {
+ control.classList.remove("open");
+ control.setAttribute("aria-expanded", "false");
+ }
+ }
+
+ function setSelectValue(control, menu, option) {
+ const value = option ? option.value : "";
+ const label = option ? option.label : control.dataset.emptyLabel || "";
+ control.dataset.value = value;
+ control.textContent = label;
+ control.title = option && option.title ? option.title : label;
+ for (const child of menu.children) {
+ child.classList.toggle("selected", child.dataset.value === value);
+ }
+ control.dispatchEvent(new CustomEvent("change", { detail: { value } }));
+ }
+
+ function setSelectOptions(control, menu, items, emptyLabel, selectedId) {
+ control.dataset.emptyLabel = emptyLabel;
+ menu.textContent = "";
+
+ const options = items
+ .map((item) => ({
+ value: item.id || item.Id || "",
+ label: item.label || item.name || item.Name || item.id || item.Id || emptyLabel,
+ title: [item.label || item.name || item.Name || item.id || item.Id, item.provider || item.Provider].filter(Boolean).join(" 路 ")
+ }))
+ .filter((item) => item.value);
+
+ for (const option of options) {
+ const item = document.createElement("div");
+ item.className = "select-option";
+ item.setAttribute("role", "option");
+ item.tabIndex = -1;
+ item.dataset.value = option.value;
+ item.textContent = option.label;
+ item.title = option.title || option.label;
+ item.addEventListener("click", () => {
+ setSelectValue(control, menu, option);
+ closeSelects();
+ });
+ menu.appendChild(item);
+ }
+
+ const selected = options.find((option) => option.value === selectedId) || options[0] || null;
+ setSelectValue(control, menu, selected || { value: "", label: emptyLabel, title: emptyLabel });
+ }
+
+ function bindSelect(control, menu) {
+ invokeOnAction(control, () => {
+ const willOpen = !control.classList.contains("open");
+ closeSelects();
+ control.classList.toggle("open", willOpen);
+ control.setAttribute("aria-expanded", willOpen ? "true" : "false");
+ });
+ control.addEventListener("keydown", (event) => {
+ const options = [...menu.children];
+ if (!options.length) return;
+ const selectedIndex = Math.max(0, options.findIndex((item) => item.classList.contains("selected")));
+ if (event.key === "ArrowDown" || event.key === "ArrowUp") {
+ event.preventDefault();
+ const nextIndex = event.key === "ArrowDown"
+ ? Math.min(options.length - 1, selectedIndex + 1)
+ : Math.max(0, selectedIndex - 1);
+ options[nextIndex].click();
+ } else if (event.key === "Escape") {
+ closeSelects();
+ }
+ });
+ }
+
+ function readFallbackConfig(config) {
+ const enabledModels = asArray(config.models)
+ .filter((model) => model && model.enabled !== false)
+ .map((model) => ({
+ id: model.id,
+ label: model.label || model.id,
+ provider: model.provider || "provider",
+ supportsStreaming: model.supportsStreaming !== false,
+ enabled: true
+ }))
+ .filter((model) => model.id);
+ return enabledModels.length ? enabledModels : fallbackModels;
+ }
+
+ async function loadConfig() {
+ try {
+ const config = bridge && typeof bridge.getConfig === "function" ? await bridge.getConfig() : null;
+ state.config = config || {};
+ } catch {
+ state.config = {};
+ }
+ state.mode = state.config.mode || "provider-direct";
+ state.selectedProjectId = (state.config.aichat && state.config.aichat.defaultProjectId) || "";
+ }
+
+ async function loadProjects() {
+ try {
+ if (!llm || typeof llm.getProjects !== "function") throw new Error("AiChat bridge unavailable.");
+ state.projects = asArray(await llm.getProjects());
+ setSelectOptions(els.projectSelect, els.projectMenu, state.projects, state.projects.length ? "Project" : "No project", state.selectedProjectId);
+ state.selectedProjectId = getSelectValue(els.projectSelect);
+ } catch (error) {
+ state.projects = [];
+ setSelectOptions(els.projectSelect, els.projectMenu, [], "No project", "");
+ els.projectSelect.title = getErrorMessage(error, "Project list unavailable.");
+ }
}
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);
- }
+ if (!llm || typeof llm.getModels !== "function") throw new Error("LLM bridge unavailable.");
+ state.models = asArray(await llm.getModels()).filter((model) => model.enabled !== false && (model.id || model.Id));
} catch {
- modelSelect.innerHTML = "<option value=\"\">No models</option>";
+ state.models = readFallbackConfig(state.config);
}
+
+ if (!state.models.length) state.models = fallbackModels;
+ state.selectedModelId = state.config.defaultModel || state.selectedModelId || "";
+ setSelectOptions(els.modelSelect, els.modelMenu, state.models, "Model", state.selectedModelId);
+ state.selectedModelId = getSelectValue(els.modelSelect);
}
- 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);
+ async function loadMemoryCapabilities() {
+ if (state.mode === "aichat-api") {
+ state.memoryCapabilities = { canUseMemory: false, canViewMemory: false, canManageMemory: false };
+ renderMemories("AiChat mode uses service-side memory.");
+ return;
+ }
try {
- const response = await window.moliDesktop.llm.streamChat({
- modelId,
- messages: [{ role: "user", content }],
- options: {}
- });
- activeRequestId = response.requestId;
+ if (!llm || typeof llm.getMemoryCapabilities !== "function") throw new Error("Memory bridge unavailable.");
+ const capabilities = await llm.getMemoryCapabilities();
+ state.memoryCapabilities = {
+ canUseMemory: Boolean(capabilities.canUseMemory ?? capabilities.CanUseMemory),
+ canViewMemory: Boolean(capabilities.canViewMemory ?? capabilities.CanViewMemory),
+ canManageMemory: Boolean(capabilities.canManageMemory ?? capabilities.CanManageMemory)
+ };
+ } catch {
+ state.memoryCapabilities = { canUseMemory: false, canViewMemory: false, canManageMemory: false };
+ }
+ setDisabled(els.memoryButton, false);
+ }
+
+ async function loadMemories() {
+ if (!state.memoryCapabilities.canViewMemory) {
+ renderMemories("Memory unavailable");
+ return;
+ }
+
+ try {
+ const scope = state.selectedProjectId ? "PROJECT" : "GLOBAL";
+ const result = await llm.getMemories({ scope, projectId: state.selectedProjectId || undefined });
+ const items = asArray(result.items || result.memories || result.data || result);
+ state.memories = items;
+ renderMemories(items.length ? "" : "No memories");
} catch (error) {
- activeRequestId = null;
- setBusy(false);
- setError(error?.message || "LLM request could not be started.");
+ renderMemories(getErrorMessage(error, "Memory unavailable"));
}
}
- promptInput.addEventListener("keydown", (event) => {
- if (event.key === "Enter" && !event.shiftKey) {
- event.preventDefault();
- submitPrompt();
+ function getMemoryContent(item) {
+ return item.content || item.Content || item.text || item.Text || item.summary || item.Summary || "";
+ }
+
+ function getMemoryId(item) {
+ return item.id || item.Id || item.memoryId || item.MemoryId || "";
+ }
+
+ function renderMemories(status) {
+ els.memoryList.textContent = "";
+ for (const item of state.memories.slice(0, 12)) {
+ const row = document.createElement("div");
+ row.className = "memory-item";
+ const text = document.createElement("div");
+ text.className = "memory-text";
+ text.textContent = getMemoryContent(item) || "Memory";
+ row.appendChild(text);
+
+ if (state.memoryCapabilities.canManageMemory) {
+ const button = document.createElement("div");
+ button.className = "icon-action";
+ button.role = "button";
+ button.tabIndex = 0;
+ button.title = "Delete memory";
+ button.setAttribute("aria-label", "Delete memory");
+ button.textContent = "-";
+ invokeOnAction(button, () => deleteMemory(getMemoryId(item)));
+ row.appendChild(button);
+ }
+
+ els.memoryList.appendChild(row);
}
- if (event.key === "Escape") {
- output.classList.remove("open");
+ if (!state.memories.length) {
+ els.memoryList.textContent = status || "";
}
- });
+ els.memoryStatus.textContent = status || "";
+ setDisabled(els.memoryInput, !state.memoryCapabilities.canManageMemory);
+ setDisabled(els.addMemoryButton, !state.memoryCapabilities.canManageMemory);
+ setDisabled(els.recallButton, !state.memoryCapabilities.canUseMemory);
+ }
- promptInput.addEventListener("input", () => {
- promptInput.style.height = "0";
- promptInput.style.height = `${Math.min(promptInput.scrollHeight, 78)}px`;
- });
+ async function createMemory(content) {
+ if (!content.trim() || !state.memoryCapabilities.canManageMemory) return;
+ try {
+ const scope = state.selectedProjectId ? "PROJECT" : "GLOBAL";
+ await llm.createMemory({ scope, projectId: state.selectedProjectId || undefined, content: content.trim() });
+ setText(els.memoryInput, "");
+ await loadMemories();
+ } catch (error) {
+ renderMemories(getErrorMessage(error, "Could not create memory"));
+ }
+ }
- collapse.addEventListener("click", () => output.classList.remove("open"));
- copy.addEventListener("click", () => navigator.clipboard?.writeText(currentText).catch(() => {}));
+ async function deleteMemory(memoryId) {
+ if (!memoryId) return;
+ try {
+ await llm.deleteMemory(memoryId);
+ await loadMemories();
+ } catch (error) {
+ renderMemories(getErrorMessage(error, "Could not delete memory"));
+ }
+ }
- 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.");
- });
+ async function recallMemoryContext(prompt) {
+ if (state.mode === "aichat-api" || !state.memoryCapabilities.canUseMemory || !prompt.trim()) return "";
+ try {
+ const memoryConfig = state.config.aichat && state.config.aichat.memory ? state.config.aichat.memory : {};
+ const result = await llm.recallMemories({
+ query: prompt,
+ scope: memoryConfig.defaultScope || "ALL",
+ projectId: state.selectedProjectId || undefined,
+ limit: memoryConfig.recallLimit || 8
+ });
+ const items = asArray(result.items || result.memories || result.data || result);
+ const contents = items.map(getMemoryContent).filter(Boolean).slice(0, 8);
+ if (!contents.length) return "";
+ return "Relevant memory:\n" + contents.map((item) => "- " + item).join("\n");
+ } catch {
+ return "";
+ }
+ }
+
+ async function mockStream(prompt) {
+ state.requestId = "mock-" + Date.now();
+ const text = "This is a local render preview. In Moli.Desktop, this prompt is sent through the host LLM bridge with the selected project and model.\n\nPrompt: " + prompt;
+ for (const chunk of text.match(/.{1,12}/g) || []) {
+ if (!state.busy) return;
+ appendOutput(chunk);
+ await delay(35);
+ }
+ finishRequest();
+ }
+
+ async function submitPrompt(promptOverride) {
+ const prompt = (promptOverride || getText(els.promptInput) || "").trim();
+ if (!prompt || state.busy) return;
+ if (!state.selectedModelId) {
+ showError("No model is available.");
+ return;
+ }
+
+ state.busy = true;
+ state.output = "";
+ state.lastPrompt = prompt;
+ state.requestId = "";
+ setStatus("loading", "Generating");
+ setOutput("", { streaming: true });
+
+ const memoryContext = await recallMemoryContext(prompt);
+ const messages = [];
+ if (memoryContext) messages.push({ role: "system", content: memoryContext });
+ messages.push({ role: "user", content: prompt });
+
+ if (!llm || typeof llm.streamChat !== "function") {
+ await mockStream(prompt);
+ return;
+ }
+
+ try {
+ const result = await llm.streamChat({
+ mode: state.mode,
+ projectId: state.selectedProjectId || undefined,
+ modelId: state.selectedModelId,
+ messages,
+ options: state.config.request || undefined
+ });
+ state.requestId = result.requestId || result.RequestId || "";
+ } catch (error) {
+ showError(getErrorMessage(error, "Request failed."));
+ finishRequest(true);
+ }
+ }
+
+ function finishRequest(keepError) {
+ state.busy = false;
+ setStatus(keepError ? "error" : "", keepError ? "Error" : "Idle");
+ setOutput(state.output, { error: Boolean(keepError) });
+ }
+
+ function showError(message) {
+ state.output = message || "Request failed.";
+ setStatus("error", "Error");
+ setOutput(state.output, { error: true });
+ requestHostResize();
+ }
+
+ function handleDelta(detail) {
+ const event = normalizeEvent(detail);
+ if (event.requestId && state.requestId && event.requestId !== state.requestId) return;
+ appendOutput(event.delta);
+ }
+
+ function handleCompleted(detail) {
+ const event = normalizeEvent(detail);
+ if (event.requestId && state.requestId && event.requestId !== state.requestId) return;
+ finishRequest(false);
+ }
+
+ function handleError(detail) {
+ const event = normalizeEvent(detail);
+ if (event.requestId && state.requestId && event.requestId !== state.requestId) return;
+ showError(event.message || event.code || "Request failed.");
+ finishRequest(true);
+ }
+
+ function bindBridgeEvents() {
+ if (!llm) return;
+ if (typeof llm.onDelta === "function") llm.onDelta(handleDelta);
+ if (typeof llm.onCompleted === "function") llm.onCompleted(handleCompleted);
+ if (typeof llm.onError === "function") llm.onError(handleError);
+ }
+
+ function bindUi() {
+ bindSelect(els.projectSelect, els.projectMenu);
+ bindSelect(els.modelSelect, els.modelMenu);
+
+ els.promptInput.addEventListener("input", resizeTextarea);
+ els.promptInput.addEventListener("keydown", (event) => {
+ if (event.key === "Enter" && !event.shiftKey) {
+ event.preventDefault();
+ submitPrompt();
+ } else if (event.key === "Escape") {
+ if (els.outputPanel.classList.contains("open")) collapseOutput();
+ else els.promptInput.blur();
+ }
+ });
+
+ els.memoryInput.addEventListener("keydown", (event) => {
+ if (event.key === "Enter") {
+ event.preventDefault();
+ createMemory(getText(els.memoryInput));
+ }
+ });
+
+ window.addEventListener("keydown", (event) => {
+ if (event.key === "Escape" && els.outputPanel.classList.contains("open")) {
+ collapseOutput();
+ }
+ });
+
+ els.projectSelect.addEventListener("change", async () => {
+ state.selectedProjectId = getSelectValue(els.projectSelect);
+ if (els.memoryPanel.classList.contains("open")) await loadMemories();
+ });
+
+ els.modelSelect.addEventListener("change", () => {
+ state.selectedModelId = getSelectValue(els.modelSelect);
+ });
+
+ invokeOnAction(els.memoryButton, async () => {
+ const isOpen = !els.memoryPanel.classList.contains("open");
+ els.memoryPanel.classList.toggle("open", isOpen);
+ els.memoryButton.classList.toggle("active", isOpen);
+ if (isOpen) await loadMemories();
+ requestHostResize();
+ });
+
+ invokeOnAction(els.addMemoryButton, () => createMemory(getText(els.memoryInput)));
+
+ invokeOnAction(els.recallButton, async () => {
+ const context = await recallMemoryContext(getText(els.promptInput) || state.lastPrompt);
+ renderMemories(context ? "Memory recalled for next request" : "No relevant memory");
+ });
+
+ invokeOnAction(els.copyButton, async () => {
+ if (!state.output) return;
+ try {
+ await navigator.clipboard.writeText(state.output);
+ } catch {
+ const range = document.createRange();
+ range.selectNodeContents(els.outputText);
+ const selection = window.getSelection();
+ selection.removeAllRanges();
+ selection.addRange(range);
+ }
+ });
+
+ invokeOnAction(els.retryButton, () => submitPrompt(state.lastPrompt));
+ invokeOnAction(els.collapseButton, collapseOutput);
+
+ document.addEventListener("click", (event) => {
+ if (!els.projectSelect.parentElement.contains(event.target) && !els.modelSelect.parentElement.contains(event.target)) {
+ closeSelects();
+ }
+ });
+ }
+
+ async function init() {
+ bindUi();
+ bindBridgeEvents();
+ updatePanelLimits();
+ setStatus("", "Idle");
+ resizeTextarea();
+ await loadConfig();
+ await Promise.all([loadProjects(), loadModels(), loadMemoryCapabilities()]);
+ renderMemories("");
+ requestHostResize(72);
+ els.promptInput.focus({ preventScroll: true });
+ }
+
+ window.addEventListener("resize", updatePanelLimits);
+
+ init();
</script>
</body>
+
</html>
--
Gitblit v1.9.1