feat(hub): add AI coach page with daily summary and chat
Aggregate four-account trades via hub_ai module and /api/hub/trades/today; store sessions in JSON; default OpenAI config matches instances. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -3342,3 +3342,132 @@ html[data-theme="light"] button.danger {
|
||||
background: rgba(201, 53, 82, 0.1);
|
||||
border-color: rgba(201, 53, 82, 0.45);
|
||||
}
|
||||
|
||||
/* --- Hub AI 教练 --- */
|
||||
.ai-layout {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr 1fr;
|
||||
gap: 16px;
|
||||
align-items: start;
|
||||
}
|
||||
@media (max-width: 960px) {
|
||||
.ai-layout {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
}
|
||||
.ai-panel {
|
||||
background: var(--panel);
|
||||
border: 1px solid var(--border-soft);
|
||||
border-radius: var(--radius);
|
||||
padding: 14px 16px;
|
||||
min-height: 420px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 12px;
|
||||
}
|
||||
.ai-panel-head {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 8px;
|
||||
}
|
||||
.ai-panel-head h2 {
|
||||
margin: 0;
|
||||
font-size: 1rem;
|
||||
font-family: var(--display);
|
||||
letter-spacing: 0.04em;
|
||||
}
|
||||
.ai-panel-actions {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
}
|
||||
.ai-stats-row {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 8px 14px;
|
||||
font-size: 0.82rem;
|
||||
color: var(--muted);
|
||||
}
|
||||
.ai-stat-chip {
|
||||
padding: 4px 8px;
|
||||
border-radius: 6px;
|
||||
background: var(--inset-surface);
|
||||
border: 1px solid var(--border-soft);
|
||||
}
|
||||
.ai-stat-chip strong {
|
||||
color: var(--text);
|
||||
margin-right: 4px;
|
||||
}
|
||||
.ai-md-body {
|
||||
flex: 1;
|
||||
overflow: auto;
|
||||
max-height: min(62vh, 640px);
|
||||
padding: 12px;
|
||||
border-radius: 8px;
|
||||
background: var(--inset-surface);
|
||||
border: 1px solid var(--border-soft);
|
||||
font-size: 0.86rem;
|
||||
line-height: 1.55;
|
||||
color: var(--text);
|
||||
}
|
||||
.ai-placeholder {
|
||||
color: var(--muted);
|
||||
margin: 0;
|
||||
}
|
||||
.ai-chat-panel {
|
||||
min-height: 520px;
|
||||
}
|
||||
.ai-chat-messages {
|
||||
flex: 1;
|
||||
overflow: auto;
|
||||
max-height: min(52vh, 520px);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 10px;
|
||||
padding: 8px 4px;
|
||||
}
|
||||
.ai-bubble {
|
||||
max-width: 92%;
|
||||
padding: 10px 12px;
|
||||
border-radius: 10px;
|
||||
font-size: 0.88rem;
|
||||
line-height: 1.5;
|
||||
white-space: pre-wrap;
|
||||
word-break: break-word;
|
||||
}
|
||||
.ai-bubble-user {
|
||||
align-self: flex-end;
|
||||
background: var(--accent-dim);
|
||||
border: 1px solid var(--border);
|
||||
}
|
||||
.ai-bubble-assistant {
|
||||
align-self: flex-start;
|
||||
background: var(--inset-surface);
|
||||
border: 1px solid var(--border-soft);
|
||||
}
|
||||
.ai-chat-form {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr auto;
|
||||
gap: 8px;
|
||||
align-items: end;
|
||||
}
|
||||
.ai-chat-form textarea {
|
||||
width: 100%;
|
||||
resize: vertical;
|
||||
min-height: 72px;
|
||||
padding: 10px 12px;
|
||||
border-radius: 8px;
|
||||
border: 1px solid var(--border-soft);
|
||||
background: var(--inset-surface);
|
||||
color: var(--text);
|
||||
font-family: var(--font);
|
||||
font-size: 0.88rem;
|
||||
}
|
||||
.ai-chat-form textarea:focus {
|
||||
outline: none;
|
||||
border-color: var(--accent);
|
||||
}
|
||||
|
||||
|
||||
@@ -623,12 +623,14 @@
|
||||
const p = window.location.pathname.replace(/\/$/, "") || "/monitor";
|
||||
if (p.includes("settings")) return "settings";
|
||||
if (p.includes("market")) return "market";
|
||||
if (p.includes("/ai")) return "ai";
|
||||
return "monitor";
|
||||
}
|
||||
|
||||
function pageElementId(page) {
|
||||
if (page === "settings") return "page-settings";
|
||||
if (page === "market") return "page-market";
|
||||
if (page === "ai") return "page-ai";
|
||||
return "page-monitor";
|
||||
}
|
||||
|
||||
@@ -648,6 +650,7 @@
|
||||
if (page === "monitor") startMonitorPoll();
|
||||
else stopMonitorPoll();
|
||||
if (page === "settings") loadSettingsUI();
|
||||
if (page === "ai") loadAiPage();
|
||||
if (page === "market" && window.hubMarketChart) {
|
||||
window.hubMarketChart.init();
|
||||
} else if (window.hubMarketChart) {
|
||||
@@ -2935,6 +2938,184 @@
|
||||
showToast("已添加一行,请填写 URL 后点「保存设置」");
|
||||
};
|
||||
|
||||
let aiMeta = null;
|
||||
let aiSummaryLoading = false;
|
||||
let aiChatLoading = false;
|
||||
|
||||
function renderAiMarkdown(text) {
|
||||
const esc = (s) =>
|
||||
String(s || "")
|
||||
.replace(/&/g, "&")
|
||||
.replace(/</g, "<")
|
||||
.replace(/>/g, ">");
|
||||
return esc(text)
|
||||
.replace(/\*\*(.+?)\*\*/g, "<strong>$1</strong>")
|
||||
.replace(/\n/g, "<br>");
|
||||
}
|
||||
|
||||
function renderAiSummaryStats(snapshot) {
|
||||
const el = document.getElementById("ai-summary-stats");
|
||||
if (!el) return;
|
||||
if (!snapshot || !snapshot.totals) {
|
||||
el.innerHTML = "";
|
||||
return;
|
||||
}
|
||||
const t = snapshot.totals;
|
||||
const pnl = Number(t.total_pnl_u);
|
||||
const pnlCls = pnl > 0 ? "pos" : pnl < 0 ? "neg" : "";
|
||||
el.innerHTML = [
|
||||
`<span class="ai-stat-chip"><strong>交易日</strong>${esc(t.trading_day || "—")}</span>`,
|
||||
`<span class="ai-stat-chip ${pnlCls}"><strong>平仓盈亏</strong>${fmt(pnl, 2)}U</span>`,
|
||||
`<span class="ai-stat-chip"><strong>笔数</strong>${t.closed_count || 0}(胜${t.win_count || 0}/负${t.loss_count || 0})</span>`,
|
||||
`<span class="ai-stat-chip"><strong>浮盈亏</strong>${fmt(Number(t.float_pnl_u), 2)}U</span>`,
|
||||
].join("");
|
||||
}
|
||||
|
||||
function renderAiChatMessages(session) {
|
||||
const box = document.getElementById("ai-chat-messages");
|
||||
const title = document.getElementById("ai-chat-title");
|
||||
if (!box) return;
|
||||
const msgs = (session && session.messages) || [];
|
||||
if (title) {
|
||||
title.textContent = session && session.title ? `聊天 · ${session.title}` : "聊天";
|
||||
}
|
||||
if (!msgs.length) {
|
||||
box.innerHTML =
|
||||
'<p class="ai-placeholder">随便聊:行情、心态、纪律、执行都行。我会先看四户监控数据,用搭档口吻回你。</p>';
|
||||
return;
|
||||
}
|
||||
box.innerHTML = msgs
|
||||
.map((m) => {
|
||||
const role = m.role === "user" ? "user" : "assistant";
|
||||
return `<div class="ai-bubble ai-bubble-${role}">${esc(m.content || "")}</div>`;
|
||||
})
|
||||
.join("");
|
||||
box.scrollTop = box.scrollHeight;
|
||||
}
|
||||
|
||||
async function loadAiMeta() {
|
||||
const r = await apiFetch("/api/ai/meta");
|
||||
aiMeta = await r.json();
|
||||
const sm = document.getElementById("ai-summary-meta");
|
||||
const cm = document.getElementById("ai-chat-meta");
|
||||
const label = aiMeta && aiMeta.model ? aiMeta.model : "";
|
||||
if (sm) sm.textContent = label;
|
||||
if (cm) cm.textContent = label;
|
||||
return aiMeta;
|
||||
}
|
||||
|
||||
async function loadAiSummary() {
|
||||
const body = document.getElementById("ai-summary-body");
|
||||
try {
|
||||
const r = await apiFetch("/api/ai/summary");
|
||||
const j = await r.json();
|
||||
const latest = j.latest;
|
||||
if (latest && latest.content_md) {
|
||||
if (body) body.innerHTML = renderAiMarkdown(latest.content_md);
|
||||
renderAiSummaryStats(latest.stats_snapshot);
|
||||
const sm = document.getElementById("ai-summary-meta");
|
||||
if (sm && latest.generated_at) {
|
||||
sm.textContent = `${j.model || ""} · ${latest.generated_at}`;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
if (body) body.innerHTML = `<p class="ai-placeholder">${esc(String(e))}</p>`;
|
||||
}
|
||||
}
|
||||
|
||||
async function loadAiChatSession() {
|
||||
const r = await apiFetch("/api/ai/chat/session");
|
||||
const j = await r.json();
|
||||
renderAiChatMessages(j.session);
|
||||
}
|
||||
|
||||
async function loadAiPage() {
|
||||
await loadAiMeta();
|
||||
await Promise.all([loadAiSummary(), loadAiChatSession()]);
|
||||
}
|
||||
|
||||
async function generateAiSummary() {
|
||||
if (aiSummaryLoading) return;
|
||||
aiSummaryLoading = true;
|
||||
const btn = document.getElementById("btn-ai-summary");
|
||||
const body = document.getElementById("ai-summary-body");
|
||||
if (btn) btn.disabled = true;
|
||||
if (body) body.innerHTML = '<p class="ai-placeholder">正在聚合四户数据并生成总结…</p>';
|
||||
try {
|
||||
const r = await apiFetch("/api/ai/summary/generate", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ force: true }),
|
||||
});
|
||||
const j = await r.json();
|
||||
if (!r.ok) throw new Error(j.detail || j.msg || "生成失败");
|
||||
if (!j.ok && j.detail) throw new Error(j.detail);
|
||||
const sum = j.summary;
|
||||
if (sum && sum.content_md && body) {
|
||||
body.innerHTML = renderAiMarkdown(sum.content_md);
|
||||
renderAiSummaryStats(sum.stats_snapshot);
|
||||
}
|
||||
showToast(j.cached ? "已是最新上下文,返回缓存总结" : "今日总结已生成");
|
||||
await loadAiSummary();
|
||||
} catch (e) {
|
||||
showToast(String(e), true);
|
||||
if (body) body.innerHTML = `<p class="ai-placeholder">${esc(String(e))}</p>`;
|
||||
} finally {
|
||||
aiSummaryLoading = false;
|
||||
if (btn) btn.disabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function newAiChat() {
|
||||
try {
|
||||
const r = await apiFetch("/api/ai/chat/new", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({}),
|
||||
});
|
||||
const j = await r.json();
|
||||
renderAiChatMessages(j.session);
|
||||
showToast("已开始新对话");
|
||||
} catch (e) {
|
||||
showToast(String(e), true);
|
||||
}
|
||||
}
|
||||
|
||||
async function sendAiChat(ev) {
|
||||
if (ev) ev.preventDefault();
|
||||
if (aiChatLoading) return;
|
||||
const input = document.getElementById("ai-chat-input");
|
||||
const text = (input && input.value || "").trim();
|
||||
if (!text) return;
|
||||
aiChatLoading = true;
|
||||
const btn = document.getElementById("btn-ai-chat-send");
|
||||
if (btn) btn.disabled = true;
|
||||
try {
|
||||
const r = await apiFetch("/api/ai/chat/send", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ message: text }),
|
||||
});
|
||||
const j = await r.json();
|
||||
if (!r.ok) throw new Error(j.detail || j.msg || "发送失败");
|
||||
if (j.detail) throw new Error(j.detail);
|
||||
if (input) input.value = "";
|
||||
renderAiChatMessages(j.session);
|
||||
} catch (e) {
|
||||
showToast(String(e), true);
|
||||
} finally {
|
||||
aiChatLoading = false;
|
||||
if (btn) btn.disabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
const aiSummaryBtn = document.getElementById("btn-ai-summary");
|
||||
if (aiSummaryBtn) aiSummaryBtn.onclick = () => generateAiSummary();
|
||||
const aiChatNewBtn = document.getElementById("btn-ai-chat-new");
|
||||
if (aiChatNewBtn) aiChatNewBtn.onclick = () => newAiChat();
|
||||
const aiChatForm = document.getElementById("ai-chat-form");
|
||||
if (aiChatForm) aiChatForm.addEventListener("submit", sendAiChat);
|
||||
|
||||
initTpslModal();
|
||||
initInstanceFrame();
|
||||
initFullscreen();
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
|
||||
<link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@400;500;600&family=Orbitron:wght@500;600;700&display=swap" rel="stylesheet" media="print" onload="this.media='all'" />
|
||||
<noscript><link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@400;500;600&family=Orbitron:wght@500;600;700&display=swap" rel="stylesheet" /></noscript>
|
||||
<link rel="stylesheet" href="/assets/app.css?v=20260604-hub-theme4" />
|
||||
<link rel="stylesheet" href="/assets/app.css?v=20260606-hub-ai" />
|
||||
</head>
|
||||
<body>
|
||||
<div class="app-bg" aria-hidden="true"></div>
|
||||
@@ -46,6 +46,7 @@
|
||||
<nav class="top-nav">
|
||||
<a href="/monitor" id="nav-monitor">监控区</a>
|
||||
<a href="/market" id="nav-market">行情区</a>
|
||||
<a href="/ai" id="nav-ai">AI 教练</a>
|
||||
<a href="/settings" id="nav-settings">系统设置</a>
|
||||
</nav>
|
||||
<button type="button" id="btn-logout" class="ghost" title="退出登录">退出</button>
|
||||
@@ -203,6 +204,42 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="page-ai" class="page hidden">
|
||||
<div class="page-head">
|
||||
<h1><span class="head-tag">AI</span> 教练</h1>
|
||||
<p class="page-desc">四户今日总结 · 口语化陪聊(单会话,点「新开对话」清空上下文)</p>
|
||||
</div>
|
||||
<div class="ai-layout">
|
||||
<section class="ai-panel ai-summary-panel">
|
||||
<div class="ai-panel-head">
|
||||
<h2>今日总结</h2>
|
||||
<div class="ai-panel-actions">
|
||||
<span id="ai-summary-meta" class="toolbar-meta"></span>
|
||||
<button type="button" id="btn-ai-summary" class="primary">生成今日总结</button>
|
||||
</div>
|
||||
</div>
|
||||
<div id="ai-summary-stats" class="ai-stats-row" aria-live="polite"></div>
|
||||
<div id="ai-summary-body" class="ai-md-body">
|
||||
<p class="ai-placeholder">点击「生成今日总结」聚合四户平仓与持仓数据(未启用账户显示「未监控」)。</p>
|
||||
</div>
|
||||
</section>
|
||||
<section class="ai-panel ai-chat-panel">
|
||||
<div class="ai-panel-head">
|
||||
<h2 id="ai-chat-title">聊天</h2>
|
||||
<div class="ai-panel-actions">
|
||||
<span id="ai-chat-meta" class="toolbar-meta"></span>
|
||||
<button type="button" id="btn-ai-chat-new" class="ghost">新开对话</button>
|
||||
</div>
|
||||
</div>
|
||||
<div id="ai-chat-messages" class="ai-chat-messages" aria-live="polite"></div>
|
||||
<form id="ai-chat-form" class="ai-chat-form">
|
||||
<textarea id="ai-chat-input" rows="3" placeholder="聊聊行情、心态、纪律、执行…" autocomplete="off"></textarea>
|
||||
<button type="submit" id="btn-ai-chat-send" class="primary">发送</button>
|
||||
</form>
|
||||
</section>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="page-settings" class="page hidden">
|
||||
<div class="page-head">
|
||||
<h1><span class="head-tag">CFG</span> 系统设置</h1>
|
||||
@@ -250,6 +287,6 @@
|
||||
<div id="toast"></div>
|
||||
<script src="https://unpkg.com/lightweight-charts@4.2.0/dist/lightweight-charts.standalone.production.js"></script>
|
||||
<script src="/assets/chart.js?v=20260604-upnl-contracts"></script>
|
||||
<script src="/assets/app.js?v=20260604-iframe-theme-flash"></script>
|
||||
<script src="/assets/app.js?v=20260606-hub-ai"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user