mirror of
https://github.com/AstrBotDevs/AstrBot
synced 2026-07-16 01:40:15 +08:00
feat: add attachment saved event handling in chat and live chat routes (#7869)
Co-authored-by: Zhilan615 <2864095951@qq.com>
This commit is contained in:
@@ -819,6 +819,19 @@ class ChatRoute(Route):
|
||||
refs = {}
|
||||
return saved_record
|
||||
|
||||
def build_attachment_saved_event(part: dict | None) -> str | None:
|
||||
if not part or not part.get("attachment_id") or not part.get("type"):
|
||||
return None
|
||||
|
||||
payload = {
|
||||
"type": "attachment_saved",
|
||||
"data": {
|
||||
"id": part["attachment_id"],
|
||||
"type": part["type"],
|
||||
},
|
||||
}
|
||||
return f"data: {json.dumps(payload, ensure_ascii=False)}\n\n"
|
||||
|
||||
try:
|
||||
# Emit session_id first so clients can bind the stream immediately.
|
||||
session_info = {
|
||||
@@ -908,12 +921,20 @@ class ChatRoute(Route):
|
||||
filename, "image"
|
||||
)
|
||||
message_accumulator.add_attachment(part)
|
||||
if attachment_saved_event := build_attachment_saved_event(
|
||||
part
|
||||
):
|
||||
yield attachment_saved_event
|
||||
elif msg_type == "record":
|
||||
filename = result_text.replace("[RECORD]", "")
|
||||
part = await self._create_attachment_from_file(
|
||||
filename, "record"
|
||||
)
|
||||
message_accumulator.add_attachment(part)
|
||||
if attachment_saved_event := build_attachment_saved_event(
|
||||
part
|
||||
):
|
||||
yield attachment_saved_event
|
||||
elif msg_type == "file":
|
||||
# 格式: [FILE]filename
|
||||
filename = result_text.replace("[FILE]", "")
|
||||
@@ -921,12 +942,20 @@ class ChatRoute(Route):
|
||||
filename, "file"
|
||||
)
|
||||
message_accumulator.add_attachment(part)
|
||||
if attachment_saved_event := build_attachment_saved_event(
|
||||
part
|
||||
):
|
||||
yield attachment_saved_event
|
||||
elif msg_type == "video":
|
||||
filename = result_text.replace("[VIDEO]", "")
|
||||
part = await self._create_attachment_from_file(
|
||||
filename, "video"
|
||||
)
|
||||
message_accumulator.add_attachment(part)
|
||||
if attachment_saved_event := build_attachment_saved_event(
|
||||
part
|
||||
):
|
||||
yield attachment_saved_event
|
||||
|
||||
should_save = False
|
||||
if msg_type == "end":
|
||||
|
||||
@@ -537,6 +537,22 @@ class LiveChatRoute(Route):
|
||||
|
||||
pending_bot_message_flusher = flush_pending_bot_message
|
||||
|
||||
async def send_attachment_saved_event(part: dict | None) -> None:
|
||||
if not part or not part.get("attachment_id") or not part.get("type"):
|
||||
return
|
||||
|
||||
await self._send_chat_payload(
|
||||
session,
|
||||
{
|
||||
"ct": "chat",
|
||||
"type": "attachment_saved",
|
||||
"data": {
|
||||
"id": part["attachment_id"],
|
||||
"type": part["type"],
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
while True:
|
||||
if session.should_interrupt:
|
||||
session.should_interrupt = False
|
||||
@@ -586,18 +602,22 @@ class LiveChatRoute(Route):
|
||||
filename = str(result_text).replace("[IMAGE]", "")
|
||||
part = await self._create_attachment_from_file(filename, "image")
|
||||
message_accumulator.add_attachment(part)
|
||||
await send_attachment_saved_event(part)
|
||||
elif msg_type == "record":
|
||||
filename = str(result_text).replace("[RECORD]", "")
|
||||
part = await self._create_attachment_from_file(filename, "record")
|
||||
message_accumulator.add_attachment(part)
|
||||
await send_attachment_saved_event(part)
|
||||
elif msg_type == "file":
|
||||
filename = str(result_text).replace("[FILE]", "").split("|", 1)[0]
|
||||
part = await self._create_attachment_from_file(filename, "file")
|
||||
message_accumulator.add_attachment(part)
|
||||
await send_attachment_saved_event(part)
|
||||
elif msg_type == "video":
|
||||
filename = str(result_text).replace("[VIDEO]", "").split("|", 1)[0]
|
||||
part = await self._create_attachment_from_file(filename, "video")
|
||||
message_accumulator.add_attachment(part)
|
||||
await send_attachment_saved_event(part)
|
||||
|
||||
should_save = False
|
||||
if msg_type == "end":
|
||||
|
||||
Reference in New Issue
Block a user