fix: normalize attachments to core Attachment type in message handler
Upstream AgentApi responses can return attachment objects that don't implement the Attachment dataclass. _to_core_attachments coerces them via duck-typing so OutgoingMessage always carries typed Attachment instances regardless of the upstream response shape.
This commit is contained in:
parent
be4607b422
commit
76230392fa
1 changed files with 30 additions and 2 deletions
|
|
@ -1,7 +1,35 @@
|
||||||
# core/handlers/message.py
|
# core/handlers/message.py
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from core.protocol import IncomingMessage, OutgoingMessage, OutgoingTyping
|
from core.protocol import Attachment, IncomingMessage, OutgoingMessage, OutgoingTyping
|
||||||
|
|
||||||
|
|
||||||
|
def _infer_attachment_type(mime_type: str | None) -> str:
|
||||||
|
if not mime_type:
|
||||||
|
return "document"
|
||||||
|
if mime_type.startswith("image/"):
|
||||||
|
return "image"
|
||||||
|
if mime_type.startswith("audio/"):
|
||||||
|
return "audio"
|
||||||
|
if mime_type.startswith("video/"):
|
||||||
|
return "video"
|
||||||
|
return "document"
|
||||||
|
|
||||||
|
|
||||||
|
def _to_core_attachments(raw: list) -> list[Attachment]:
|
||||||
|
result = []
|
||||||
|
for a in raw:
|
||||||
|
if isinstance(a, Attachment):
|
||||||
|
result.append(a)
|
||||||
|
else:
|
||||||
|
result.append(Attachment(
|
||||||
|
type=getattr(a, "type", None) or _infer_attachment_type(getattr(a, "mime_type", None)),
|
||||||
|
url=getattr(a, "url", None),
|
||||||
|
filename=getattr(a, "filename", None),
|
||||||
|
mime_type=getattr(a, "mime_type", None),
|
||||||
|
workspace_path=getattr(a, "workspace_path", None),
|
||||||
|
))
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
def _start_command(platform: str) -> str:
|
def _start_command(platform: str) -> str:
|
||||||
|
|
@ -38,6 +66,6 @@ async def handle_message(event: IncomingMessage, auth_mgr, platform, chat_mgr, s
|
||||||
chat_id=event.chat_id,
|
chat_id=event.chat_id,
|
||||||
text=response.response,
|
text=response.response,
|
||||||
parse_mode="markdown",
|
parse_mode="markdown",
|
||||||
attachments=list(getattr(response, "attachments", [])),
|
attachments=_to_core_attachments(getattr(response, "attachments", [])),
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue