Commit f9f6c62

benny-dou <60535774+benny-dou@users.noreply.github.com>
2025-06-13 14:32:27
style(gpt): use forwarded name in contexts
1 parent bff3b60
Changed files (1)
src
src/llm/contexts.py
@@ -80,6 +80,7 @@ async def single_gpt_context(client: Client, message: Message) -> dict:
     contexts = []
     for msg in messages:
         info = parse_msg(msg, silent=True)
+        sender = info["fwd_full_name"] or info["full_name"]
         try:
             if info["mtype"] == "photo":
                 res = await base64_media(client, msg)
@@ -90,7 +91,7 @@ async def single_gpt_context(client: Client, message: Message) -> dict:
                     contexts.append(
                         {
                             "type": "text",
-                            "text": f"[fileowner]: {info['full_name']}\n[filename]: {info['file_name']}\n[file content]:\n{read_text(fpath).strip()}",
+                            "text": f"[filename]: {info['file_name']}\n[file content]:\n{read_text(fpath).strip()}",
                         }
                     )
                 if Path(info["file_name"]).suffix in extra_markdown_extensions:
@@ -100,14 +101,14 @@ async def single_gpt_context(client: Client, message: Message) -> dict:
                     contexts.append(
                         {
                             "type": "text",
-                            "text": f"[fileowner]: {info['full_name']}\n[filename]: {info['file_name']}\n[file content]:\n{text.strip()}",
+                            "text": f"[filename]: {info['file_name']}\n[file content]:\n{text.strip()}",
                         }
                     )
             # user message has entity urls, use full html
             clean_texts = clean_context(info["html"]) if role == "user" and info["entity_urls"] else clean_context(info["text"])
             if not clean_texts:
                 continue
-            texts = f"[username]: {info['full_name']}\n[message]:\n{clean_texts}" if role == "user" else clean_texts
+            texts = f"[username]: {sender}\n[message]:\n{clean_texts}" if role == "user" else clean_texts
             contexts.append({"type": "text", "text": texts})
         except Exception as e:
             logger.warning(f"Download media from message failed: {e}")
@@ -142,13 +143,14 @@ async def single_gemini_context(client: Client, message: Message, app: genai.Cli
     parts = []
     for msg in messages:
         info = parse_msg(msg, silent=True)
+        sender = info["fwd_full_name"] or info["full_name"]
         try:
             if info["mtype"] in ["video", "photo", "audio", "voice"] or info["mime_type"] in gemini_mime_types or any(info["file_name"].endswith(ext) for ext in gemini_extensions):
                 fpath: str = await client.download_media(msg, in_memory=False)  # type: ignore  # type: ignore
                 if info["mtype"] in ["audio", "voice"] and Path(fpath).suffix not in GEMINI_AUDIO_EXT:
                     audio_path = await downsampe_audio(fpath)
                     fpath = audio_path.as_posix()
-                upload = await app.aio.files.upload(file=fpath, config=UploadFileConfig(display_name=info["file_name"] or f"send from {info['full_name']}"))
+                upload = await app.aio.files.upload(file=fpath, config=UploadFileConfig(display_name=info["file_name"] or f"send from {sender}"))
                 while upload.state == FileState.PROCESSING:
                     logger.trace("Waiting for upload to complete...")
                     await asyncio.sleep(1)
@@ -169,7 +171,7 @@ async def single_gemini_context(client: Client, message: Message, app: genai.Cli
             clean_texts = clean_context(info["html"]) if role == "user" and info["entity_urls"] else clean_context(info["text"])
             if not clean_texts:
                 continue
-            texts = f"[username]: {info['full_name']}\n[message]:\n{clean_texts}" if role == "user" else clean_texts
+            texts = f"[username]: {sender}\n[message]:\n{clean_texts}" if role == "user" else clean_texts
             parts.append(Part.from_text(text=texts))
         except Exception as e:
             logger.warning(f"Download media from message failed: {e}")