feat(hub): add data dashboard and AI chat with session history
Add /dashboard with daily PnL overview and loss alerts. Extend AI coach chat with history sidebar, delete/switch sessions, message copy, and trading vs general bot modes. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -13,15 +13,27 @@ from hub_ai.config import (
|
||||
CHAT_MAX_OUTPUT_TOKENS,
|
||||
CHAT_SUMMARY_EXCERPT_MAX_CHARS,
|
||||
CHAT_TEMPERATURE,
|
||||
trading_day_reset_hour,
|
||||
)
|
||||
from hub_trades_lib import current_trading_day
|
||||
from hub_ai.context import build_daily_context, format_chat_context_for_chat
|
||||
from hub_ai.prompts import CHAT_SYSTEM, build_chat_user_prompt
|
||||
from hub_ai.prompts import (
|
||||
CHAT_GENERAL_SYSTEM,
|
||||
CHAT_SYSTEM,
|
||||
build_chat_user_prompt,
|
||||
build_general_chat_user_prompt,
|
||||
)
|
||||
from hub_ai.store import (
|
||||
CHAT_BOT_GENERAL,
|
||||
CHAT_BOT_TRADING,
|
||||
append_chat_message,
|
||||
create_new_session,
|
||||
delete_chat_session,
|
||||
ensure_active_session,
|
||||
get_active_session,
|
||||
list_chat_sessions,
|
||||
load_chat_store,
|
||||
set_active_session,
|
||||
summary_excerpt_for_chat,
|
||||
)
|
||||
|
||||
@@ -58,16 +70,48 @@ def _history_lines(
|
||||
def get_chat_state() -> dict[str, Any]:
|
||||
store = load_chat_store()
|
||||
session = get_active_session()
|
||||
if session:
|
||||
session.setdefault("bot_mode", CHAT_BOT_TRADING)
|
||||
return {
|
||||
"active_session_id": store.get("active_session_id"),
|
||||
"session": session,
|
||||
"sessions": list_chat_sessions(),
|
||||
"model": model_label(),
|
||||
}
|
||||
|
||||
|
||||
def start_new_chat(*, trading_day: str) -> dict:
|
||||
session = create_new_session(trading_day=trading_day)
|
||||
return {"ok": True, "session": session, "model": model_label()}
|
||||
def start_new_chat(*, trading_day: str, bot_mode: str = CHAT_BOT_TRADING) -> dict:
|
||||
session = create_new_session(trading_day=trading_day, bot_mode=bot_mode)
|
||||
return {
|
||||
"ok": True,
|
||||
"session": session,
|
||||
"sessions": list_chat_sessions(),
|
||||
"model": model_label(),
|
||||
}
|
||||
|
||||
|
||||
def switch_chat_session(session_id: str) -> dict[str, Any]:
|
||||
session = set_active_session(session_id)
|
||||
return {
|
||||
"ok": True,
|
||||
"session": session,
|
||||
"sessions": list_chat_sessions(),
|
||||
"model": model_label(),
|
||||
}
|
||||
|
||||
|
||||
def remove_chat_session(session_id: str) -> dict[str, Any]:
|
||||
deleted, new_active = delete_chat_session(session_id)
|
||||
if not deleted:
|
||||
return {"ok": False, "msg": "session_not_found"}
|
||||
session = get_active_session()
|
||||
return {
|
||||
"ok": True,
|
||||
"active_session_id": new_active,
|
||||
"session": session,
|
||||
"sessions": list_chat_sessions(),
|
||||
"model": model_label(),
|
||||
}
|
||||
|
||||
|
||||
def send_chat_message(
|
||||
@@ -90,8 +134,9 @@ def send_chat_message(
|
||||
if not user_visible and parsed.get("attachment_note"):
|
||||
user_visible = f"(上传了 {parsed['attachment_note']})"
|
||||
|
||||
ctx = build_daily_context(exchanges, trading_day=trading_day)
|
||||
day = ctx["trading_day"]
|
||||
day = (trading_day or "").strip()[:10] or current_trading_day(
|
||||
reset_hour=trading_day_reset_hour()
|
||||
)
|
||||
session = ensure_active_session(trading_day=day)
|
||||
sid = session["id"]
|
||||
history = _history_lines(
|
||||
@@ -106,22 +151,35 @@ def send_chat_message(
|
||||
attachments=parsed.get("attachment_meta") or [],
|
||||
)
|
||||
|
||||
brief_ctx = format_chat_context_for_chat(ctx, max_chars=CHAT_CONTEXT_MAX_CHARS)
|
||||
excerpt = summary_excerpt_for_chat(day, max_chars=CHAT_SUMMARY_EXCERPT_MAX_CHARS)
|
||||
|
||||
user_prompt = build_chat_user_prompt(
|
||||
context_text=brief_ctx,
|
||||
trading_day=day,
|
||||
summary_excerpt=excerpt,
|
||||
history_lines=history,
|
||||
user_message=text or user_visible,
|
||||
attachment_note=str(parsed.get("attachment_note") or ""),
|
||||
)
|
||||
if parsed.get("text_append"):
|
||||
user_prompt += "\n\n【附件正文】\n" + parsed["text_append"]
|
||||
bot_mode = (session.get("bot_mode") or CHAT_BOT_TRADING).strip().lower()
|
||||
if bot_mode == CHAT_BOT_GENERAL:
|
||||
user_prompt = build_general_chat_user_prompt(
|
||||
history_lines=history,
|
||||
user_message=text or user_visible,
|
||||
attachment_note=str(parsed.get("attachment_note") or ""),
|
||||
)
|
||||
if parsed.get("text_append"):
|
||||
user_prompt += "\n\n【附件正文】\n" + parsed["text_append"]
|
||||
system_prompt = CHAT_GENERAL_SYSTEM
|
||||
else:
|
||||
ctx = build_daily_context(exchanges, trading_day=day)
|
||||
day = ctx["trading_day"]
|
||||
brief_ctx = format_chat_context_for_chat(ctx, max_chars=CHAT_CONTEXT_MAX_CHARS)
|
||||
excerpt = summary_excerpt_for_chat(day, max_chars=CHAT_SUMMARY_EXCERPT_MAX_CHARS)
|
||||
user_prompt = build_chat_user_prompt(
|
||||
context_text=brief_ctx,
|
||||
trading_day=day,
|
||||
summary_excerpt=excerpt,
|
||||
history_lines=history,
|
||||
user_message=text or user_visible,
|
||||
attachment_note=str(parsed.get("attachment_note") or ""),
|
||||
)
|
||||
if parsed.get("text_append"):
|
||||
user_prompt += "\n\n【附件正文】\n" + parsed["text_append"]
|
||||
system_prompt = CHAT_SYSTEM
|
||||
|
||||
reply = generate_text(
|
||||
system=CHAT_SYSTEM,
|
||||
system=system_prompt,
|
||||
user=user_prompt,
|
||||
temperature=CHAT_TEMPERATURE,
|
||||
images_b64=parsed.get("images_b64") or None,
|
||||
@@ -136,6 +194,7 @@ def send_chat_message(
|
||||
"ok": True,
|
||||
"trading_day": day,
|
||||
"session": session,
|
||||
"sessions": list_chat_sessions(),
|
||||
"reply": reply,
|
||||
"model": model_label(),
|
||||
"attachment_warnings": parsed.get("errors") or [],
|
||||
|
||||
Reference in New Issue
Block a user