feat: AI hint supports multi-turn conversation with context - hint_history list stored in conversation, accumulates across rounds - Each new hint appended, full history passed to AI as combined prompt - Format: 'hint1 → hint2 → hint3' so AI understands the conversation flow

This commit is contained in:
2026-07-22 13:32:09 +08:00
parent e0c2110c09
commit a517768083

View File

@@ -310,16 +310,18 @@ def _do_regen(bot_token: str, cfg: Config, chat_id: int, msg_id: int, ctx: dict,
_show_ai_suggestions(bot_token, chat_id, msg_id, new_suggestions, comparison) _show_ai_suggestions(bot_token, chat_id, msg_id, new_suggestions, comparison)
def _do_ai_hint(bot_token: str, cfg: Config, chat_id: int, conv: dict, ctx: dict, hint: str): def _do_ai_hint(bot_token: str, cfg: Config, chat_id: int, conv: dict, ctx: dict, hint_history: list[str]):
"""在 AI 线程池中根据用户提示生成回复""" """在 AI 线程池中根据用户提示生成回复(支持多轮追问)"""
user = find_user_by_chat_id(cfg, chat_id) user = find_user_by_chat_id(cfg, chat_id)
if not user: if not user:
return return
style = conv.get("reply_style", "short") style = conv.get("reply_style", "short")
# 拼接历史提示作为上下文
combined_hint = "".join(hint_history)
try: try:
result = generate_more_replies( result = generate_more_replies(
user.ai, ctx["sender"], ctx["subject"], ctx["original_body"], user.ai, ctx["sender"], ctx["subject"], ctx["original_body"],
conv.get("all_suggestions", []), user_hint=hint, style=style, conv.get("all_suggestions", []), user_hint=combined_hint, style=style,
) )
new_suggestions = result["suggestions"] new_suggestions = result["suggestions"]
comparison = result.get("comparison", "") comparison = result.get("comparison", "")
@@ -332,9 +334,10 @@ def _do_ai_hint(bot_token: str, cfg: Config, chat_id: int, conv: dict, ctx: dict
conv["all_suggestions"].extend(new_suggestions) conv["all_suggestions"].extend(new_suggestions)
conv["comparison"] = comparison conv["comparison"] = comparison
conv["state"] = "ai_reply_selection" conv["state"] = "ai_reply_selection"
conv["hint_history"] = hint_history
conv.pop("prompt_msg_id", None) conv.pop("prompt_msg_id", None)
save_conversation(chat_id, conv) save_conversation(chat_id, conv)
logger.info("[ai_hint] 根据提示生成 %d 条新回复", len(new_suggestions)) logger.info("[ai_hint] 根据提示生成 %d 条新回复 (历史 %d 条)", len(new_suggestions), len(hint_history))
_show_ai_suggestions(bot_token, chat_id, conv["summary_msg_id"], new_suggestions, comparison) _show_ai_suggestions(bot_token, chat_id, conv["summary_msg_id"], new_suggestions, comparison)
@@ -499,8 +502,10 @@ def _handle_callback(bot_token: str, cfg: Config, cb: dict):
if conv and conv.get("summary_msg_id") == msg_id: if conv and conv.get("summary_msg_id") == msg_id:
conv["state"] = "awaiting_ai_hint" conv["state"] = "awaiting_ai_hint"
conv["prompt_msg_id"] = prompt_msg_id conv["prompt_msg_id"] = prompt_msg_id
if "hint_history" not in conv:
conv["hint_history"] = []
save_conversation(chat_id, conv) save_conversation(chat_id, conv)
logger.info(" 等待用户输入 AI 提示") logger.info(" 等待用户输入 AI 提示 (历史 %d 条)", len(conv["hint_history"]))
elif action == CALLBACK_CANCEL: elif action == CALLBACK_CANCEL:
conv = load_conversation(chat_id) conv = load_conversation(chat_id)
@@ -616,13 +621,17 @@ def _handle_message(bot_token: str, cfg: Config, msg: dict):
if prompt_msg_id: if prompt_msg_id:
delete_message(bot_token, chat_id_str, prompt_msg_id) delete_message(bot_token, chat_id_str, prompt_msg_id)
# 追加到 hint 历史
hint_history = conv.get("hint_history", [])
hint_history.append(text)
# 更新消息为 AI 思考中 # 更新消息为 AI 思考中
_tg_req(bot_token, "editMessageText", { _tg_req(bot_token, "editMessageText", {
"chat_id": chat_id, "message_id": conv["summary_msg_id"], "chat_id": chat_id, "message_id": conv["summary_msg_id"],
"text": "🤖 *AI思考中…*", "parse_mode": "MarkdownV2", "text": "🤖 *AI思考中…*", "parse_mode": "MarkdownV2",
}) })
# 丢到 AI 线程池处理 # 丢到 AI 线程池处理,传入完整 hint 历史
_ai_pool.submit(_do_ai_hint, bot_token, cfg, chat_id, conv, ctx, text) _ai_pool.submit(_do_ai_hint, bot_token, cfg, chat_id, conv, ctx, hint_history)
def _show_ai_suggestions(bot_token: str, chat_id: int, msg_id: int, def _show_ai_suggestions(bot_token: str, chat_id: int, msg_id: int,