feat(hub): rolling chat summary to cap AI context and prevent mid-session failures
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -3,11 +3,24 @@ from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from hub_ai.chat import _is_ai_error_reply
|
||||
from hub_ai.client import generate_text, model_label
|
||||
from hub_ai.config import CHAT_MAX_CONTINUATIONS, CHAT_MAX_OUTPUT_TOKENS, CHAT_TEMPERATURE
|
||||
from hub_ai.rolling_summary import refresh_session_rolling_summary
|
||||
from hub_ai.text_util import clip_text, is_ai_error_reply
|
||||
from hub_ai.config import (
|
||||
CHAT_MAX_CONTINUATIONS,
|
||||
CHAT_MAX_OUTPUT_TOKENS,
|
||||
CHAT_TEMPERATURE,
|
||||
CHAT_USER_MESSAGE_MAX_CHARS,
|
||||
)
|
||||
from hub_ai.prompts import CHAT_SYSTEM, build_archive_quote_review_prompt
|
||||
from hub_ai.store import CHAT_BOT_TRADING, append_chat_message, create_new_session, list_chat_sessions
|
||||
from hub_ai.store import (
|
||||
CHAT_BOT_TRADING,
|
||||
append_chat_message,
|
||||
create_new_session,
|
||||
delete_chat_session,
|
||||
get_active_session,
|
||||
list_chat_sessions,
|
||||
)
|
||||
from hub_symbol_archive_lib import list_daily_trades
|
||||
|
||||
|
||||
@@ -43,7 +56,10 @@ def format_archive_trades_for_ai(payload: dict[str, Any]) -> str:
|
||||
if not trades:
|
||||
lines.append("(该日无交易记录)")
|
||||
return "\n".join(lines)
|
||||
for i, t in enumerate(trades, 1):
|
||||
max_rows = 50
|
||||
if len(trades) > max_rows:
|
||||
lines.append(f"(共 {len(trades)} 笔,以下展示最近 {max_rows} 笔)")
|
||||
for i, t in enumerate(trades[:max_rows], 1):
|
||||
ex = str(t.get("exchange_key") or t.get("account_exchange_key") or "—")
|
||||
sym = str(t.get("symbol") or "—")
|
||||
direction = str(t.get("direction") or "—")
|
||||
@@ -54,7 +70,7 @@ def format_archive_trades_for_ai(payload: dict[str, Any]) -> str:
|
||||
pnl = _fmt_pnl(t.get("pnl_amount"))
|
||||
entry = str(t.get("entry_type") or t.get("entry_reason") or t.get("monitor_type") or "—")
|
||||
tag = _tag_label(str(t.get("behavior_tag") or ""))
|
||||
note = str(t.get("note") or "").strip()
|
||||
note = clip_text(str(t.get("note") or "").strip(), 80)
|
||||
line = (
|
||||
f"{i}. {ex} | {sym} | {direction} | 开仓类型 {entry} | "
|
||||
f"开 {opened} | 平 {closed} | 持仓 {hold} | 结果 {result} | "
|
||||
@@ -87,13 +103,12 @@ def send_archive_quote_review(
|
||||
|
||||
archive_payload = list_daily_trades(trading_day=day, period="today")
|
||||
archive_trades_text = format_archive_trades_for_ai(archive_payload)
|
||||
|
||||
append_chat_message(sid, "user", text)
|
||||
user_for_prompt = clip_text(text, CHAT_USER_MESSAGE_MAX_CHARS)
|
||||
|
||||
user_prompt = build_archive_quote_review_prompt(
|
||||
quote_date=day,
|
||||
archive_trades_text=archive_trades_text,
|
||||
user_message=text,
|
||||
user_message=user_for_prompt,
|
||||
)
|
||||
reply = generate_text(
|
||||
system=CHAT_SYSTEM,
|
||||
@@ -102,10 +117,20 @@ def send_archive_quote_review(
|
||||
max_tokens=CHAT_MAX_OUTPUT_TOKENS,
|
||||
max_continuations=CHAT_MAX_CONTINUATIONS,
|
||||
)
|
||||
if _is_ai_error_reply(reply):
|
||||
return {"ok": False, "msg": reply, "session_id": sid}
|
||||
if is_ai_error_reply(reply):
|
||||
delete_chat_session(sid)
|
||||
return {"ok": False, "msg": reply}
|
||||
|
||||
append_chat_message(sid, "user", text)
|
||||
session = append_chat_message(sid, "assistant", reply)
|
||||
refresh_session_rolling_summary(
|
||||
sid,
|
||||
prior_summary="",
|
||||
user_text=text,
|
||||
assistant_text=reply,
|
||||
bot_mode=CHAT_BOT_TRADING,
|
||||
)
|
||||
session = get_active_session() or session
|
||||
return {
|
||||
"ok": True,
|
||||
"trading_day": day,
|
||||
|
||||
Reference in New Issue
Block a user