diff --git a/src/tg_bot.py b/src/tg_bot.py index c8a124a..a9847d1 100644 --- a/src/tg_bot.py +++ b/src/tg_bot.py @@ -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) -def _do_ai_hint(bot_token: str, cfg: Config, chat_id: int, conv: dict, ctx: dict, hint: str): - """在 AI 线程池中根据用户提示生成回复""" +def _do_ai_hint(bot_token: str, cfg: Config, chat_id: int, conv: dict, ctx: dict, hint_history: list[str]): + """在 AI 线程池中根据用户提示生成回复(支持多轮追问)""" user = find_user_by_chat_id(cfg, chat_id) if not user: return style = conv.get("reply_style", "short") + # 拼接历史提示作为上下文 + combined_hint = " → ".join(hint_history) try: result = generate_more_replies( 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"] 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["comparison"] = comparison conv["state"] = "ai_reply_selection" + conv["hint_history"] = hint_history conv.pop("prompt_msg_id", None) 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) @@ -499,8 +502,10 @@ def _handle_callback(bot_token: str, cfg: Config, cb: dict): if conv and conv.get("summary_msg_id") == msg_id: conv["state"] = "awaiting_ai_hint" conv["prompt_msg_id"] = prompt_msg_id + if "hint_history" not in conv: + conv["hint_history"] = [] save_conversation(chat_id, conv) - logger.info(" 等待用户输入 AI 提示") + logger.info(" 等待用户输入 AI 提示 (历史 %d 条)", len(conv["hint_history"])) elif action == CALLBACK_CANCEL: conv = load_conversation(chat_id) @@ -616,13 +621,17 @@ def _handle_message(bot_token: str, cfg: Config, msg: dict): if 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 思考中 _tg_req(bot_token, "editMessageText", { "chat_id": chat_id, "message_id": conv["summary_msg_id"], "text": "🤖 *AI思考中…*", "parse_mode": "MarkdownV2", }) - # 丢到 AI 线程池处理 - _ai_pool.submit(_do_ai_hint, bot_token, cfg, chat_id, conv, ctx, text) + # 丢到 AI 线程池处理,传入完整 hint 历史 + _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,