mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-09-22 18:08:04 +00:00
feat(webui): 首启人格向导(默认 / 自定义 / 稍后)+ 一次性标记
接续人格配置项化(597f07c):现在人格是 core.agent.personal_prompt,
本次加上「首启问一次」的界面,之后不再打扰。
后端(GET/POST /api/v1/persona):
- GET → {initialized, current_prompt, file_override}
未设置时 current_prompt 回落到内置默认模板;存在 personal/personal.md
时报告 file_override(它会覆盖配置项,向导据此提示用户)
- POST → {"mode":"default"|"custom"|"later","content":"…"}
写配置 + 打一次性标记 core.internal.persona_initialized;
custom 返回 restart_required=true(人格在启动时载入);
「稍后」= 保留当前默认 + 打标记,**绝不阻塞任何流程**
- 空内容的 custom 与未知 mode 一律 400,且**不打标记**(否则向导会被跳过)
前端(dashboard.html):
- 首启拉一次 /api/v1/persona,未初始化则弹向导(复用一直没人用的 .confirm-* 样式)
- 「自定义…」第一次点击展开文本域并预填当前人格,再次点击才提交(避免误提交)
- 中英双语走既有 __() 机制;保存失败/空内容用 toast 提示
测试:TestPersonaWizardFlow(首启状态、later 打标记不改人格、custom 写入 + 需重启、
空内容与未知 mode 被拒且不打标记)、TestPersonaWizardReportsFileOverride。
E2E(真实实例):首启 initialized=false → POST later → initialized=true,
config 中标记=1、人格键为默认模板;前端页面含向导函数。
This commit is contained in:
@ -1124,6 +1124,33 @@
|
||||
justify-content: flex-end;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
/* 首启人格向导(复用 confirm-* 弹窗)*/
|
||||
.persona-ta {
|
||||
width: 100%;
|
||||
display: none;
|
||||
margin-bottom: 12px;
|
||||
font: 12px/1.5 var(--font-mono, monospace);
|
||||
padding: 8px;
|
||||
border-radius: var(--radius-md, 8px);
|
||||
border: 1px solid var(--glass-border);
|
||||
background: var(--glass-bg);
|
||||
color: var(--text-primary);
|
||||
box-sizing: border-box;
|
||||
}
|
||||
.persona-warn {
|
||||
display: none;
|
||||
font-size: 12px;
|
||||
line-height: 1.5;
|
||||
color: var(--text-secondary);
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
.persona-actions {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
gap: 8px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
.empty-state {
|
||||
text-align: center;
|
||||
padding: 48px 24px;
|
||||
@ -2521,8 +2548,87 @@
|
||||
if (card) card.style.transform = "";
|
||||
});
|
||||
|
||||
// ===== API =====
|
||||
async function api(p, o) {
|
||||
// ===== 首启人格向导 =====
|
||||
// 人格是配置项(core.agent.personal_prompt,默认模板不含任何版本号)。
|
||||
// 首次启动问一次「默认 / 自定义 / 稍后」,之后不再打扰;
|
||||
// 不回答 = 稍后 = 保留默认人格,绝不阻塞启动。
|
||||
async function maybeShowPersonaWizard() {
|
||||
var st;
|
||||
try {
|
||||
st = await api("/persona");
|
||||
} catch (e) {
|
||||
return; // 拿不到状态就不打扰用户
|
||||
}
|
||||
if (!st || st.initialized) return;
|
||||
var ov = document.createElement("div");
|
||||
ov.className = "confirm-overlay";
|
||||
ov.style.display = "flex";
|
||||
ov.innerHTML =
|
||||
'<div class="confirm-box">' +
|
||||
"<h3>" + escHtml(__("人格设定", "Persona")) + "</h3>" +
|
||||
"<p>" + escHtml(__(
|
||||
"首次启动:选一下助手的人格。选「使用默认」即可(之后可在设置里修改);自定义内容在下次重启后生效。",
|
||||
"First run: pick your assistant's persona. \"Use default\" is fine (change it later in Settings); custom content takes effect after the next restart."
|
||||
)) + "</p>" +
|
||||
'<div class="persona-warn"></div>' +
|
||||
'<textarea class="persona-ta" rows="6"></textarea>' +
|
||||
'<div class="persona-actions">' +
|
||||
'<button class="btn btn-ghost btn-sm" data-mode="later">' + escHtml(__("稍后再说", "Later")) + "</button>" +
|
||||
'<button class="btn btn-ghost btn-sm" data-mode="custom">' + escHtml(__("自定义…", "Custom…")) + "</button>" +
|
||||
'<button class="btn btn-sm" data-mode="default">' + escHtml(__("使用默认", "Use default")) + "</button>" +
|
||||
"</div></div>";
|
||||
document.body.appendChild(ov);
|
||||
var ta = ov.querySelector(".persona-ta");
|
||||
var warn = ov.querySelector(".persona-warn");
|
||||
var customOpen = false;
|
||||
if (st.file_override) {
|
||||
warn.style.display = "block";
|
||||
warn.textContent = __(
|
||||
"注意:检测到 personal/personal.md,它优先于这里的设置。",
|
||||
"Note: personal/personal.md exists and takes precedence over this choice."
|
||||
);
|
||||
}
|
||||
function close() {
|
||||
ov.remove();
|
||||
}
|
||||
async function submit(mode, content) {
|
||||
try {
|
||||
var r = await api("/persona", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ mode: mode, content: content || "" }),
|
||||
});
|
||||
if (r && r.restart_required) toast(__("已保存,重启后生效", "Saved; takes effect after restart"));
|
||||
else toast(__("已保存", "Saved"));
|
||||
} catch (e) {
|
||||
toast(__("保存失败:", "Save failed: ") + e, true);
|
||||
}
|
||||
close();
|
||||
}
|
||||
ov.querySelectorAll("button[data-mode]").forEach(function (b) {
|
||||
b.onclick = function () {
|
||||
var mode = b.getAttribute("data-mode");
|
||||
if (mode !== "custom") {
|
||||
submit(mode);
|
||||
return;
|
||||
}
|
||||
if (!customOpen) { // 第一次点:展开文本域并预填当前人格
|
||||
customOpen = true;
|
||||
ta.style.display = "block";
|
||||
ta.value = st.current_prompt || "";
|
||||
ta.focus();
|
||||
return;
|
||||
}
|
||||
if (!ta.value.trim()) {
|
||||
toast(__("内容不能为空", "Content cannot be empty"), true);
|
||||
return;
|
||||
}
|
||||
submit("custom", ta.value);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
// ===== API =====
|
||||
async function api(p, o) {
|
||||
var opts = {
|
||||
credentials: "include",
|
||||
headers: { "Content-Type": "application/json", ...o?.headers },
|
||||
@ -6516,6 +6622,7 @@
|
||||
renderAll();
|
||||
connectSSE();
|
||||
startUptimeTicker();
|
||||
maybeShowPersonaWizard();
|
||||
})();
|
||||
setInterval(renderAll, 15000);
|
||||
// 消息同步轮询兜底:每30秒增量同步 chatHistory,补偿 SSE 断连窗口期
|
||||
|
||||
Reference in New Issue
Block a user