fix: enhance file naming logic in File component and update prompt handling in InternalAgentSubStage

This commit is contained in:
Soulter
2025-12-01 18:12:03 +08:00
parent 6aa6963ab5
commit 1de377e749
3 changed files with 21 additions and 12 deletions

View File

@@ -722,7 +722,12 @@ class File(BaseMessageComponent):
"""下载文件""" """下载文件"""
download_dir = os.path.join(get_astrbot_data_path(), "temp") download_dir = os.path.join(get_astrbot_data_path(), "temp")
os.makedirs(download_dir, exist_ok=True) os.makedirs(download_dir, exist_ok=True)
file_path = os.path.join(download_dir, f"{uuid.uuid4().hex}") if self.name:
name, ext = os.path.splitext(self.name)
filename = f"{name}_{uuid.uuid4().hex[:8]}{ext}"
else:
filename = f"{uuid.uuid4().hex}"
file_path = os.path.join(download_dir, filename)
await download_file(self.url, file_path) await download_file(self.url, file_path)
self.file_ = os.path.abspath(file_path) self.file_ = os.path.abspath(file_path)

View File

@@ -141,6 +141,8 @@ class InternalAgentSubStage(Stage):
file_names.append(reply_comp.name) file_names.append(reply_comp.name)
if not file_paths: if not file_paths:
return return
if not req.prompt:
req.prompt = "总结一下文件里面讲了什么?"
if self.file_extract_prov == "moonshotai": if self.file_extract_prov == "moonshotai":
if not self.file_extract_msh_api_key: if not self.file_extract_msh_api_key:
logger.error("Moonshot AI API key for file extract is not set") logger.error("Moonshot AI API key for file extract is not set")
@@ -396,16 +398,6 @@ class InternalAgentSubStage(Stage):
event.set_extra("provider_request", req) event.set_extra("provider_request", req)
if not req.prompt and not req.image_urls:
return
# call event hook
if await call_event_hook(event, EventType.OnLLMRequestEvent, req):
return
# apply knowledge base feature
await self._apply_kb(event, req)
# fix contexts json str # fix contexts json str
if isinstance(req.contexts, str): if isinstance(req.contexts, str):
req.contexts = json.loads(req.contexts) req.contexts = json.loads(req.contexts)
@@ -417,6 +409,16 @@ class InternalAgentSubStage(Stage):
except Exception as e: except Exception as e:
logger.error(f"Error occurred while applying file extract: {e}") logger.error(f"Error occurred while applying file extract: {e}")
if not req.prompt and not req.image_urls:
return
# call event hook
if await call_event_hook(event, EventType.OnLLMRequestEvent, req):
return
# apply knowledge base feature
await self._apply_kb(event, req)
# truncate contexts to fit max length # truncate contexts to fit max length
if req.contexts: if req.contexts:
req.contexts = self._truncate_contexts(req.contexts) req.contexts = self._truncate_contexts(req.contexts)

View File

@@ -381,7 +381,9 @@ class TelegramPlatformAdapter(Platform):
f"Telegram document file_path is None, cannot save the file {file_name}.", f"Telegram document file_path is None, cannot save the file {file_name}.",
) )
else: else:
message.message.append(Comp.File(file=file_path, name=file_name)) message.message.append(
Comp.File(file=file_path, name=file_name, url=file_path)
)
elif update.message.video: elif update.message.video:
file = await update.message.video.get_file() file = await update.message.video.get_file()