Fix supervisor AI empty replies with fallback templates
Skip appending AI error strings to the session and use event-specific fallback commentary when the model returns empty content. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -388,6 +388,56 @@ def event_tag(event_type: str) -> str:
|
||||
}.get(event_type, "监管")
|
||||
|
||||
|
||||
def _fmt_pnl_u(pnl: Any) -> str:
|
||||
try:
|
||||
v = float(pnl)
|
||||
sign = "+" if v > 0 else ""
|
||||
return f"{sign}{v:.4f}".rstrip("0").rstrip(".") + "U"
|
||||
except (TypeError, ValueError):
|
||||
return ""
|
||||
|
||||
|
||||
def build_supervisor_fallback_reply(event: dict, warnings: list[dict] | None = None) -> str:
|
||||
"""AI 不可用或返回空时的短评语(不展示错误文案)。"""
|
||||
et = str(event.get("event_type") or "")
|
||||
sym = str(event.get("symbol") or "—")
|
||||
ex = str(event.get("exchange_name") or event.get("account_name") or "").strip()
|
||||
pnl_txt = _fmt_pnl_u(event.get("pnl_amount"))
|
||||
warn = (warnings or [])[:1]
|
||||
warn_txt = str(warn[0].get("message") or "").strip() if warn else ""
|
||||
|
||||
if et == EVENT_PROGRAM_SL:
|
||||
base = f"{sym} 程序止损"
|
||||
if pnl_txt:
|
||||
base += f"({pnl_txt})"
|
||||
base += ",按计划出场是纪律。先歇一会儿,别急着马上再开。"
|
||||
elif et == EVENT_PROGRAM_TP:
|
||||
base = f"{sym} 程序止盈"
|
||||
if pnl_txt:
|
||||
base += f"({pnl_txt})"
|
||||
base += ",执行不错。保持节奏,别立刻反手再开一单。"
|
||||
elif et == EVENT_OPEN:
|
||||
who = f"{ex} " if ex else ""
|
||||
base = f"看到 {who}新开 {sym}。动手前确认是不是计划内,别因为上一笔情绪再开。"
|
||||
elif et == EVENT_HUB_CLOSE:
|
||||
base = f"中控平了 {sym}"
|
||||
if pnl_txt:
|
||||
base += f"({pnl_txt})"
|
||||
base += "。"
|
||||
base += f" {warn_txt}" if warn_txt else " 停一停,别连着手痒。"
|
||||
elif et == EVENT_MANUAL_CLOSE:
|
||||
base = f"手动平了 {sym}"
|
||||
if pnl_txt:
|
||||
base += f"({pnl_txt})"
|
||||
base += "。"
|
||||
base += f" {warn_txt}" if warn_txt else " 想好再开下一单。"
|
||||
elif et == EVENT_FREQ_WARN:
|
||||
base = warn_txt or "今日操作偏频繁,先休息一会儿。"
|
||||
else:
|
||||
base = "收到。确认是否按计划执行,别连续加码。"
|
||||
return base.strip()[:320]
|
||||
|
||||
|
||||
def build_system_message(event: dict, *, trading_day: str, warnings: list[dict] | None = None) -> str:
|
||||
tag = event_tag(str(event.get("event_type") or ""))
|
||||
ex = event.get("exchange_name") or event.get("account_name") or "—"
|
||||
@@ -611,11 +661,22 @@ def process_supervisor_tick(
|
||||
trading_day=trading_day,
|
||||
session_id=session_id,
|
||||
)
|
||||
if reply and reply.strip():
|
||||
append_supervisor_ai_message(session_id, reply.strip())
|
||||
from hub_ai.text_util import is_ai_error_reply
|
||||
|
||||
text = str(reply or "").strip()
|
||||
if not text or is_ai_error_reply(text):
|
||||
text = build_supervisor_fallback_reply(evt_snapshot, evt_warnings)
|
||||
if text:
|
||||
append_supervisor_ai_message(session_id, text)
|
||||
_fire_notify()
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
fb = build_supervisor_fallback_reply(evt_snapshot, evt_warnings)
|
||||
if fb:
|
||||
append_supervisor_ai_message(session_id, fb)
|
||||
_fire_notify()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
threading.Thread(target=_ai_bg, daemon=True).start()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user