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:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user