From cf90bdefc5c10c4e987dfec506f3d4412d2975cb Mon Sep 17 00:00:00 2001 From: tjc66666666 <3428979959@qq.com> Date: Wed, 10 Jun 2026 14:29:04 +0800 Subject: [PATCH 01/11] Implement time tracking for image caption requests Added time tracking for image caption provider usage and logging. --- .../astrbot/group_chat_context.py | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/astrbot/builtin_stars/astrbot/group_chat_context.py b/astrbot/builtin_stars/astrbot/group_chat_context.py index 7fee3c0df9..83be6aaba9 100644 --- a/astrbot/builtin_stars/astrbot/group_chat_context.py +++ b/astrbot/builtin_stars/astrbot/group_chat_context.py @@ -1,6 +1,7 @@ import asyncio import datetime import random +import time import uuid from collections import defaultdict, deque @@ -10,6 +11,7 @@ from astrbot.api.message_components import At, Image, Plain from astrbot.api.platform import MessageType from astrbot.api.provider import Provider, ProviderRequest +from astrbot.core import db_helper from astrbot.core.agent.message import TextPart from astrbot.core.astrbot_config_mgr import AstrBotConfigManager @@ -79,21 +81,64 @@ async def get_image_caption( image_url: str, image_caption_provider_id: str, image_caption_prompt: str, + event: AstrMessageEvent | None = None, ) -> str: + provider_id = image_caption_provider_id if not image_caption_provider_id: provider = self.context.get_using_provider() + provider_id = provider.meta().id if hasattr(provider, 'meta') else "" else: provider = self.context.get_provider_by_id(image_caption_provider_id) if not provider: raise Exception(f"没有找到 ID 为 {image_caption_provider_id} 的提供商") if not isinstance(provider, Provider): raise Exception(f"提供商类型错误({type(provider)}),无法获取图片描述") + + start_time = time.time() response = await provider.text_chat( prompt=image_caption_prompt, session_id=uuid.uuid4().hex, image_urls=[image_url], persist=False, ) + end_time = time.time() + + # 记录图片转述模型的调用统计 + if event is not None: + try: + provider_model = ( + provider.get_model() if provider.get_model() else None + ) + usage_dict: dict = {} + if response.usage: + usage_dict = { + "input_other": response.usage.input_other, + "input_cached": response.usage.input_cached, + "output": response.usage.output, + } + + await db_helper.insert_provider_stat( + umo=event.unified_msg_origin, + provider_id=provider_id, + provider_model=provider_model, + conversation_id=None, + status="completed" + if response.role != "err" + else "error", + stats={ + "token_usage": usage_dict, + "start_time": start_time, + "end_time": end_time, + "time_to_first_token": 0.0, + }, + agent_type="internal", + ) + except Exception: + logger.debug( + "Failed to record group chat image caption provider stat", + exc_info=True, + ) + return response.completion_text async def need_active_reply(self, event: AstrMessageEvent) -> bool: @@ -200,6 +245,7 @@ async def _format_message(self, event: AstrMessageEvent, cfg: dict) -> str: url, cfg["image_caption_provider_id"], cfg["image_caption_prompt"], + event=event, ) parts.append(f" [Image: {caption}]") except Exception as e: From f0cbb7c0d0a4b24b46d5718a94d110effe8f014b Mon Sep 17 00:00:00 2001 From: tjc66666666 <3428979959@qq.com> Date: Wed, 10 Jun 2026 14:34:09 +0800 Subject: [PATCH 02/11] Enhance image caption processing with logging and params Added event and conversation_id parameters to image caption processing and included timing and usage statistics logging. --- astrbot/core/astr_main_agent.py | 43 ++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/astrbot/core/astr_main_agent.py b/astrbot/core/astr_main_agent.py index cee6e9e27d..18dc0fb603 100644 --- a/astrbot/core/astr_main_agent.py +++ b/astrbot/core/astr_main_agent.py @@ -11,7 +11,9 @@ from dataclasses import dataclass, field from pathlib import Path -from astrbot.core import logger +import time + +from astrbot.core import db_helper, logger from astrbot.core.agent.handoff import HandoffTool from astrbot.core.agent.mcp_client import MCPTool from astrbot.core.agent.message import TextPart @@ -606,6 +608,8 @@ async def _request_img_caption( cfg: dict, image_urls: list[str], plugin_context: Context, + event: AstrMessageEvent | None = None, + conversation_id: str | None = None, ) -> str: prov = plugin_context.get_provider_by_id(provider_id) if prov is None: @@ -622,10 +626,45 @@ async def _request_img_caption( "Please describe the image.", ) logger.debug("Processing image caption with provider: %s", provider_id) + + start_time = time.time() llm_resp = await prov.text_chat( prompt=img_cap_prompt, image_urls=image_urls, ) + end_time = time.time() + + # 记录图片转述模型的调用统计 + if event is not None: + try: + provider_model = prov.get_model() if prov.get_model() else None + usage_dict: dict = {} + if llm_resp.usage: + usage_dict = { + "input_other": llm_resp.usage.input_other, + "input_cached": llm_resp.usage.input_cached, + "output": llm_resp.usage.output, + } + + await db_helper.insert_provider_stat( + umo=event.unified_msg_origin, + provider_id=provider_id, + provider_model=provider_model, + conversation_id=conversation_id, + status="completed" if llm_resp.role != "err" else "error", + stats={ + "token_usage": usage_dict, + "start_time": start_time, + "end_time": end_time, + "time_to_first_token": 0.0, + }, + agent_type="internal", + ) + except Exception: + logger.debug( + "Failed to record image caption provider stat", exc_info=True + ) + return llm_resp.completion_text @@ -648,6 +687,8 @@ async def _ensure_img_caption( cfg, compressed_urls, plugin_context, + event=event, + conversation_id=req.conversation.cid if req.conversation else None, ) if caption: req.extra_user_content_parts.append( From 978a1bd87ca29af83004d262eede32e48d2686d2 Mon Sep 17 00:00:00 2001 From: tjc66666666 <3428979959@qq.com> Date: Wed, 10 Jun 2026 14:39:28 +0800 Subject: [PATCH 03/11] Fix formatting issues in group_chat_context.py --- astrbot/builtin_stars/astrbot/group_chat_context.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/astrbot/builtin_stars/astrbot/group_chat_context.py b/astrbot/builtin_stars/astrbot/group_chat_context.py index 83be6aaba9..ebc57560f8 100644 --- a/astrbot/builtin_stars/astrbot/group_chat_context.py +++ b/astrbot/builtin_stars/astrbot/group_chat_context.py @@ -86,7 +86,7 @@ async def get_image_caption( provider_id = image_caption_provider_id if not image_caption_provider_id: provider = self.context.get_using_provider() - provider_id = provider.meta().id if hasattr(provider, 'meta') else "" + provider_id = provider.meta().id if hasattr(provider, "meta") else "" else: provider = self.context.get_provider_by_id(image_caption_provider_id) if not provider: @@ -106,9 +106,7 @@ async def get_image_caption( # 记录图片转述模型的调用统计 if event is not None: try: - provider_model = ( - provider.get_model() if provider.get_model() else None - ) + provider_model = provider.get_model() if provider.get_model() else None usage_dict: dict = {} if response.usage: usage_dict = { @@ -122,9 +120,7 @@ async def get_image_caption( provider_id=provider_id, provider_model=provider_model, conversation_id=None, - status="completed" - if response.role != "err" - else "error", + status="completed" if response.role != "err" else "error", stats={ "token_usage": usage_dict, "start_time": start_time, From fe8b58dc9e1b332bef26e58576217680e9552d3d Mon Sep 17 00:00:00 2001 From: tjc66666666 <3428979959@qq.com> Date: Wed, 10 Jun 2026 14:40:30 +0800 Subject: [PATCH 04/11] Fix indentation in exception logging --- astrbot/core/astr_main_agent.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/astrbot/core/astr_main_agent.py b/astrbot/core/astr_main_agent.py index 18dc0fb603..b0dedccd1b 100644 --- a/astrbot/core/astr_main_agent.py +++ b/astrbot/core/astr_main_agent.py @@ -661,9 +661,7 @@ async def _request_img_caption( agent_type="internal", ) except Exception: - logger.debug( - "Failed to record image caption provider stat", exc_info=True - ) + logger.debug("Failed to record image caption provider stat", exc_info=True) return llm_resp.completion_text From be30eed1396de6077635e24d9c7d072949eda83b Mon Sep 17 00:00:00 2001 From: tjc66666666 <3428979959@qq.com> Date: Wed, 10 Jun 2026 14:43:54 +0800 Subject: [PATCH 05/11] Clean up time module imports Removed redundant import of time module. --- astrbot/core/astr_main_agent.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/astrbot/core/astr_main_agent.py b/astrbot/core/astr_main_agent.py index b0dedccd1b..efc3e7c275 100644 --- a/astrbot/core/astr_main_agent.py +++ b/astrbot/core/astr_main_agent.py @@ -6,13 +6,12 @@ import json import os import platform +import time import zoneinfo from collections.abc import Coroutine from dataclasses import dataclass, field from pathlib import Path -import time - from astrbot.core import db_helper, logger from astrbot.core.agent.handoff import HandoffTool from astrbot.core.agent.mcp_client import MCPTool From aaac393440cd9a289d0176c7dfc3d84fa1a8acb2 Mon Sep 17 00:00:00 2001 From: tjc66666666 <3428979959@qq.com> Date: Wed, 10 Jun 2026 14:54:12 +0800 Subject: [PATCH 06/11] Refactor model retrieval logic in group_chat_context.py --- astrbot/builtin_stars/astrbot/group_chat_context.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/astrbot/builtin_stars/astrbot/group_chat_context.py b/astrbot/builtin_stars/astrbot/group_chat_context.py index ebc57560f8..74dde6e868 100644 --- a/astrbot/builtin_stars/astrbot/group_chat_context.py +++ b/astrbot/builtin_stars/astrbot/group_chat_context.py @@ -106,7 +106,8 @@ async def get_image_caption( # 记录图片转述模型的调用统计 if event is not None: try: - provider_model = provider.get_model() if provider.get_model() else None + model = provider.get_model() + provider_model = model or None usage_dict: dict = {} if response.usage: usage_dict = { From 26aec65398bc4c10af05e3ca34d248eee9cf57ee Mon Sep 17 00:00:00 2001 From: tjc66666666 <3428979959@qq.com> Date: Wed, 10 Jun 2026 14:54:53 +0800 Subject: [PATCH 07/11] Refactor model retrieval logic in astr_main_agent.py --- astrbot/core/astr_main_agent.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/astrbot/core/astr_main_agent.py b/astrbot/core/astr_main_agent.py index efc3e7c275..98cf3afe78 100644 --- a/astrbot/core/astr_main_agent.py +++ b/astrbot/core/astr_main_agent.py @@ -636,7 +636,8 @@ async def _request_img_caption( # 记录图片转述模型的调用统计 if event is not None: try: - provider_model = prov.get_model() if prov.get_model() else None + model = prov.get_model() + provider_model = model or None usage_dict: dict = {} if llm_resp.usage: usage_dict = { From 136808cbabad1e9d16056c6f85025925fd709fa6 Mon Sep 17 00:00:00 2001 From: tjc66666666 <3428979959@qq.com> Date: Wed, 10 Jun 2026 15:23:37 +0800 Subject: [PATCH 08/11] Refactor image caption stat recording logic Removed unused database helper calls and exception handling for image caption statistics. --- .../astrbot/group_chat_context.py | 39 ++++--------------- 1 file changed, 8 insertions(+), 31 deletions(-) diff --git a/astrbot/builtin_stars/astrbot/group_chat_context.py b/astrbot/builtin_stars/astrbot/group_chat_context.py index 74dde6e868..a9835de0c9 100644 --- a/astrbot/builtin_stars/astrbot/group_chat_context.py +++ b/astrbot/builtin_stars/astrbot/group_chat_context.py @@ -11,7 +11,6 @@ from astrbot.api.message_components import At, Image, Plain from astrbot.api.platform import MessageType from astrbot.api.provider import Provider, ProviderRequest -from astrbot.core import db_helper from astrbot.core.agent.message import TextPart from astrbot.core.astrbot_config_mgr import AstrBotConfigManager @@ -105,36 +104,14 @@ async def get_image_caption( # 记录图片转述模型的调用统计 if event is not None: - try: - model = provider.get_model() - provider_model = model or None - usage_dict: dict = {} - if response.usage: - usage_dict = { - "input_other": response.usage.input_other, - "input_cached": response.usage.input_cached, - "output": response.usage.output, - } - - await db_helper.insert_provider_stat( - umo=event.unified_msg_origin, - provider_id=provider_id, - provider_model=provider_model, - conversation_id=None, - status="completed" if response.role != "err" else "error", - stats={ - "token_usage": usage_dict, - "start_time": start_time, - "end_time": end_time, - "time_to_first_token": 0.0, - }, - agent_type="internal", - ) - except Exception: - logger.debug( - "Failed to record group chat image caption provider stat", - exc_info=True, - ) + await provider.record_image_caption_stat( + umo=event.unified_msg_origin, + provider_id=provider_id, + conversation_id=None, + start_time=start_time, + end_time=end_time, + response=response, + ) return response.completion_text From 4b3233bb9a0af6d74e5bab42b2e75f24c6641846 Mon Sep 17 00:00:00 2001 From: tjc66666666 <3428979959@qq.com> Date: Wed, 10 Jun 2026 15:24:42 +0800 Subject: [PATCH 09/11] Implement record_image_caption_stat method Adds a method to record statistics for image caption model calls. --- astrbot/core/provider/provider.py | 44 +++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/astrbot/core/provider/provider.py b/astrbot/core/provider/provider.py index efe9e2e47e..de0b1d9388 100644 --- a/astrbot/core/provider/provider.py +++ b/astrbot/core/provider/provider.py @@ -62,6 +62,50 @@ async def test(self) -> None: """ ... + async def record_image_caption_stat( + self, + *, + umo: str, + provider_id: str, + conversation_id: str | None, + start_time: float, + end_time: float, + response: LLMResponse, + ) -> None: + """记录图片转述模型调用统计,由子类调用。""" + try: + from astrbot.core import db_helper, logger + except ImportError: + return + + try: + model = self.get_model() + provider_model = model or None + usage_dict: dict = {} + if response.usage: + usage_dict = { + "input_other": response.usage.input_other, + "input_cached": response.usage.input_cached, + "output": response.usage.output, + } + + await db_helper.insert_provider_stat( + umo=umo, + provider_id=provider_id, + provider_model=provider_model, + conversation_id=conversation_id, + status="completed" if response.role != "err" else "error", + stats={ + "token_usage": usage_dict, + "start_time": start_time, + "end_time": end_time, + "time_to_first_token": 0.0, + }, + agent_type="internal", + ) + except Exception: + logger.debug("Failed to record image caption provider stat", exc_info=True) + class Provider(AbstractProvider): """Chat Provider""" From e3b7f5bd41ce7300b5d716feee6030487f985652 Mon Sep 17 00:00:00 2001 From: tjc66666666 <3428979959@qq.com> Date: Wed, 10 Jun 2026 15:25:46 +0800 Subject: [PATCH 10/11] Refactor image caption logging and statistics Removed unused database helper calls for image caption statistics and streamlined the logging process. --- astrbot/core/astr_main_agent.py | 38 ++++++++------------------------- 1 file changed, 9 insertions(+), 29 deletions(-) diff --git a/astrbot/core/astr_main_agent.py b/astrbot/core/astr_main_agent.py index 98cf3afe78..1ad5de0910 100644 --- a/astrbot/core/astr_main_agent.py +++ b/astrbot/core/astr_main_agent.py @@ -12,7 +12,7 @@ from dataclasses import dataclass, field from pathlib import Path -from astrbot.core import db_helper, logger +from astrbot.core import logger from astrbot.core.agent.handoff import HandoffTool from astrbot.core.agent.mcp_client import MCPTool from astrbot.core.agent.message import TextPart @@ -633,35 +633,15 @@ async def _request_img_caption( ) end_time = time.time() - # 记录图片转述模型的调用统计 if event is not None: - try: - model = prov.get_model() - provider_model = model or None - usage_dict: dict = {} - if llm_resp.usage: - usage_dict = { - "input_other": llm_resp.usage.input_other, - "input_cached": llm_resp.usage.input_cached, - "output": llm_resp.usage.output, - } - - await db_helper.insert_provider_stat( - umo=event.unified_msg_origin, - provider_id=provider_id, - provider_model=provider_model, - conversation_id=conversation_id, - status="completed" if llm_resp.role != "err" else "error", - stats={ - "token_usage": usage_dict, - "start_time": start_time, - "end_time": end_time, - "time_to_first_token": 0.0, - }, - agent_type="internal", - ) - except Exception: - logger.debug("Failed to record image caption provider stat", exc_info=True) + await prov.record_image_caption_stat( + umo=event.unified_msg_origin, + provider_id=provider_id, + conversation_id=conversation_id, + start_time=start_time, + end_time=end_time, + response=llm_resp, + ) return llm_resp.completion_text From 926c3cadac00a6422b1144713494f0370128d241 Mon Sep 17 00:00:00 2001 From: tjc66666666 <3428979959@qq.com> Date: Wed, 10 Jun 2026 15:33:35 +0800 Subject: [PATCH 11/11] Update group_chat_context.py