Commit 9c33e09

benny-dou <60535774+benny-dou@users.noreply.github.com>
2025-04-02 18:36:34
style(summary): do not send summary as a reply message
1 parent 8d4477a
Changed files (1)
src
src/llm/summary.py
@@ -123,10 +123,13 @@ async def ai_summary(client: Client, message: Message, summary_prefix: str | Non
     history = await get_parsed_chat_history(client, info["cid"], offset_id, num_history, begin_time, end_time, filter_users)
     # parse the history contexts
     parsed = await get_contexts(history)
-    if not history:
+    if not parsed["txt_format"]:
         await send2tg(client, message, texts=f"{num_history}条历史消息中未找到符合条件的消息", **kwargs)
         await modify_progress(del_status=True, **kwargs)
         return
+    # send contexts as txt file
+    with io.BytesIO(parsed["txt_format"].encode("utf-8")) as f:
+        await client.send_document(to_int(message.chat.id), f, file_name=CONTEXT_FILENAME)
     contexts = refine_prompts(parsed["system_context"] + [{"role": "user", "content": parsed["user_context"]}])
     sysmtem_tokens = count_tokens(contexts[0]["content"])
     user_tokens = count_tokens(contexts[-1]["content"])
@@ -157,11 +160,10 @@ async def ai_summary(client: Client, message: Message, summary_prefix: str | Non
     if texts := response.get("content"):
         texts = texts.strip("`")
         if summary_prefix is None:
-            summary_prefix = f"🤖**{response['model']}**:\n"
+            summary_prefix = f"🤖**{summary_model_name}**:\n"
+        kwargs["reply_msg_id"] = -1  # DO NOT send as a reply message
         await send2tg(client, message, texts=f"{summary_prefix}⏩开始时间: {begin_time:%m-%d %H:%M:%S}\n⏯️结束时间: {end_time:%m-%d %H:%M:%S}\n{texts}", **kwargs)
         await modify_progress(del_status=True, **kwargs)
-        with io.BytesIO(parsed["txt_format"].encode("utf-8")) as f:
-            await client.send_document(to_int(message.chat.id), f, file_name=CONTEXT_FILENAME)
 
 
 async def get_contexts(history: list[dict]) -> dict:
@@ -235,10 +237,12 @@ async def get_contexts(history: list[dict]) -> dict:
                 "username": info["full_name"],
                 "message": info["text"],
             }
+            txt_format += f"[{content['time']}]{content['username']}:\n"
             if reply_msg_content := get_message_by_id(history, info.get("reply_to_message_id")):
                 content["reply_to_message"] = reply_msg_content
+                txt_format += f"<quote>{reply_msg_content['username']}: {reply_msg_content['message']}</quote>\n"
             user_context.append({"type": "text", "text": str(content)})
-            txt_format += f"[{content['time']}]{content['username']}:\n{content['message']}\n\n"
+            txt_format += f"{content['message']}\n\n"
     if not user_context:
         return {}
     return {"system_context": system_context, "user_context": user_context, "txt_format": txt_format, "begin_time": begin_time, "end_time": end_time}