32 lines
1.2 KiB
Python
32 lines
1.2 KiB
Python
from __future__ import annotations
|
|
|
|
from collections.abc import Iterable
|
|
|
|
from review_agent.models import FileAttachment
|
|
|
|
from ..services.attachment_reader import read_attachment_details
|
|
from .base import BaseSkill, SkillResult, WorkflowContext
|
|
|
|
|
|
class AttachmentReaderSkill(BaseSkill):
|
|
name = "attachment_reader"
|
|
|
|
def run(self, context: WorkflowContext) -> SkillResult:
|
|
attachments = FileAttachment.objects.filter(
|
|
conversation=context.batch.conversation,
|
|
is_active=True,
|
|
).exclude(upload_status=FileAttachment.UploadStatus.DELETED)
|
|
return self.run_for_attachments(attachments)
|
|
|
|
def run_for_attachments(self, attachments: Iterable[FileAttachment]) -> SkillResult:
|
|
results = [read_attachment_details(attachment).to_dict() for attachment in attachments]
|
|
if not results:
|
|
return SkillResult(success=False, message="当前对话没有可读取的附件。")
|
|
|
|
has_success = any(item["status"] == "success" for item in results)
|
|
return SkillResult(
|
|
success=has_success,
|
|
data={"attachments": results},
|
|
message="附件解析完成。" if has_success else "附件解析失败。",
|
|
)
|