Skip to content

Commit d7a5023

Browse files
committed
refactor: address code review comments on pre_caption_images
1 parent 82a5e7f commit d7a5023

2 files changed

Lines changed: 15 additions & 13 deletions

File tree

astrbot/core/astr_main_agent.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -483,15 +483,13 @@ async def _request_img_caption(
483483
async def pre_caption_images(
484484
event: AstrMessageEvent,
485485
plugin_context: Context,
486+
cfg: dict,
486487
) -> None:
487488
"""在 session lock 外提前完成图片描述,结果写入 event extra。
488489
489490
由 pipeline 在获取 session lock 之前调用,避免图片描述慢速 LLM
490491
调用占用 session lock,阻塞后续消息处理。
491492
"""
492-
cfg = plugin_context.get_config(umo=event.unified_msg_origin).get(
493-
"provider_settings", {}
494-
)
495493
img_cap_prov_id: str = cfg.get("default_image_caption_provider_id") or ""
496494
if not img_cap_prov_id:
497495
return
@@ -519,7 +517,7 @@ async def pre_caption_images(
519517
)
520518
event.set_extra(_PRE_CAPTION_RESULT_KEY, caption or "")
521519
except Exception as exc: # noqa: BLE001
522-
logger.error("预处理图片描述失败: %s", exc)
520+
logger.error("预处理图片描述失败: %s", exc, exc_info=True)
523521
event.set_extra(_PRE_CAPTION_RESULT_KEY, None)
524522

525523

@@ -530,12 +528,14 @@ async def _ensure_img_caption(
530528
plugin_context: Context,
531529
image_caption_provider: str,
532530
) -> None:
531+
if event.get_extra("_skip_img_caption"):
532+
return
533+
533534
pre_caption = event.get_extra(_PRE_CAPTION_RESULT_KEY)
534-
if pre_caption is not None:
535-
if pre_caption:
536-
req.extra_user_content_parts.append(
537-
TextPart(text=f"<image_caption>{pre_caption}</image_caption>")
538-
)
535+
if pre_caption:
536+
req.extra_user_content_parts.append(
537+
TextPart(text=f"<image_caption>{pre_caption}</image_caption>")
538+
)
539539
req.image_urls = []
540540
return
541541

astrbot/core/pipeline/process_stage/method/agent_sub_stages/internal.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,13 @@ async def process(
189189

190190
if not event.get_extra("provider_request"):
191191
plugin_context = self.ctx.plugin_manager.context
192-
cfg = plugin_context.get_config(
193-
umo=event.unified_msg_origin
194-
).get("provider_settings", {})
192+
cfg = plugin_context.get_config(umo=event.unified_msg_origin).get(
193+
"provider_settings", {}
194+
)
195195
if cfg.get("image_caption_wait_for_context_order", True):
196-
await pre_caption_images(event, plugin_context)
196+
await pre_caption_images(event, plugin_context, cfg)
197+
else:
198+
event.set_extra("_skip_img_caption", True)
197199

198200
async with session_lock_manager.acquire_lock(event.unified_msg_origin):
199201
logger.debug("acquired session lock for llm request")

0 commit comments

Comments
 (0)