mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-07-08 19:31:33 +08:00
Simplify agent message handling and streaming cleanup
This commit is contained in:
@@ -317,6 +317,8 @@ class MoviePilotAgent:
|
||||
"""
|
||||
判断当前 Agent 是否需要写入会话历史表。
|
||||
"""
|
||||
if self._tool_context.get("user_reply_sent"):
|
||||
return False
|
||||
return bool(self.channel and self.source)
|
||||
|
||||
def _save_display_history_messages(self, messages: List[dict]) -> None:
|
||||
@@ -1102,9 +1104,9 @@ class MoviePilotAgent:
|
||||
self._streamed_output = ""
|
||||
|
||||
# 获取历史消息
|
||||
messages = memory_manager.get_agent_messages(
|
||||
messages = list(memory_manager.get_agent_messages(
|
||||
session_id=self.session_id, user_id=self.user_id
|
||||
)
|
||||
))
|
||||
|
||||
# 构建结构化用户消息内容
|
||||
request_payload = {
|
||||
@@ -1269,6 +1271,7 @@ class MoviePilotAgent:
|
||||
self._agent_started_at = datetime.now()
|
||||
self._llm_runtime_config = None
|
||||
self._llm_provider_selection = {}
|
||||
streaming_stopped = False
|
||||
try:
|
||||
# Agent运行配置
|
||||
agent_config = {
|
||||
@@ -1316,6 +1319,7 @@ class MoviePilotAgent:
|
||||
all_sent_via_stream,
|
||||
streamed_text,
|
||||
) = await self.stream_handler.stop_streaming()
|
||||
streaming_stopped = True
|
||||
|
||||
if not all_sent_via_stream:
|
||||
# 流式输出未能发送全部内容(发送失败等)
|
||||
@@ -1418,7 +1422,8 @@ class MoviePilotAgent:
|
||||
error=execution_error,
|
||||
)
|
||||
# 确保停止流式输出
|
||||
await self.stream_handler.stop_streaming()
|
||||
if not streaming_stopped:
|
||||
await self.stream_handler.stop_streaming()
|
||||
|
||||
async def send_agent_message(self, message: str, title: str = ""):
|
||||
"""
|
||||
|
||||
@@ -87,7 +87,6 @@ You act as a proactive agent. Your goal is to fully resolve the user's media-rel
|
||||
{button_choice_spec}
|
||||
- Voice replies: {voice_reply_spec}
|
||||
- If the current channel supports image sending and an image would materially help, you may use the `send_message` tool with `image_url` to send it.
|
||||
{send_message_format_spec}
|
||||
- If the current channel supports file sending and you need to return a local image or file for the user to download, use `send_local_file`.
|
||||
</communication_runtime>
|
||||
|
||||
|
||||
@@ -141,9 +141,6 @@ class PromptManager:
|
||||
if caps:
|
||||
markdown_spec = self._generate_formatting_instructions(caps)
|
||||
button_choice_spec = self._generate_button_choice_instructions(msg_channel)
|
||||
send_message_format_spec = self._generate_send_message_format_instructions(
|
||||
msg_channel
|
||||
)
|
||||
|
||||
# 啰嗦模式
|
||||
verbose_spec = ""
|
||||
@@ -169,7 +166,6 @@ class PromptManager:
|
||||
moviepilot_info=moviepilot_info,
|
||||
voice_reply_spec=voice_reply_spec,
|
||||
button_choice_spec=button_choice_spec,
|
||||
send_message_format_spec=send_message_format_spec,
|
||||
)
|
||||
|
||||
return base_prompt
|
||||
@@ -404,27 +400,6 @@ class PromptManager:
|
||||
"content as a text fallback and still completes the reply."
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _generate_send_message_format_instructions(
|
||||
channel: MessageChannel = None,
|
||||
) -> str:
|
||||
"""
|
||||
根据渠道生成 send_message 工具的格式参数提示。
|
||||
"""
|
||||
if channel != MessageChannel.Telegram:
|
||||
return ""
|
||||
return (
|
||||
"- Telegram message formatting: `send_message` supports an optional "
|
||||
"`parse_mode` argument. Leave it empty for default MarkdownV2. When a "
|
||||
"structured Telegram notice would be clearer in HTML, set "
|
||||
"`parse_mode=\"HTML\"` and write the `message` using only Telegram-supported "
|
||||
"HTML tags such as `<b>`, `<i>`, `<u>`, `<s>`, `<code>`, `<pre>`, "
|
||||
"`<blockquote>`, and `<a href=\"...\">`. Keep `title` as plain text; "
|
||||
"the Telegram module renders it as a bold heading automatically. Escape "
|
||||
"user-provided or dynamic values before embedding them in HTML. Do "
|
||||
"not mix Markdown syntax into an HTML-formatted message."
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _generate_button_choice_instructions(
|
||||
channel: MessageChannel = None,
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
"""发送消息工具"""
|
||||
|
||||
import html as html_utils
|
||||
import re
|
||||
from typing import Optional, Type
|
||||
|
||||
from pydantic import BaseModel, Field, model_validator
|
||||
@@ -12,49 +10,6 @@ from app.log import logger
|
||||
from app.schemas import Notification
|
||||
from app.schemas.types import NotificationType
|
||||
|
||||
SEND_MESSAGE_PARSE_MODE_MARKDOWN = "MarkdownV2"
|
||||
SEND_MESSAGE_PARSE_MODE_HTML = "HTML"
|
||||
SEND_MESSAGE_PARSE_MODE_ALIASES = {
|
||||
"markdownv2": SEND_MESSAGE_PARSE_MODE_MARKDOWN,
|
||||
"mdv2": SEND_MESSAGE_PARSE_MODE_MARKDOWN,
|
||||
"html": SEND_MESSAGE_PARSE_MODE_HTML,
|
||||
}
|
||||
SEND_MESSAGE_HTML_ALLOWED_TAGS = {
|
||||
"a",
|
||||
"b",
|
||||
"blockquote",
|
||||
"code",
|
||||
"del",
|
||||
"em",
|
||||
"i",
|
||||
"ins",
|
||||
"pre",
|
||||
"s",
|
||||
"span",
|
||||
"strike",
|
||||
"strong",
|
||||
"tg-spoiler",
|
||||
"u",
|
||||
}
|
||||
SEND_MESSAGE_HTML_NORMALIZATION_RULES = (
|
||||
(re.compile(r"<\s*br\s*/?\s*>", re.IGNORECASE), "\n"),
|
||||
(re.compile(r"<\s*/\s*p\s*>", re.IGNORECASE), "\n"),
|
||||
(re.compile(r"<\s*p(?:\s+[^>]*)?>", re.IGNORECASE), ""),
|
||||
(re.compile(r"<\s*/\s*div\s*>", re.IGNORECASE), "\n"),
|
||||
(re.compile(r"<\s*div(?:\s+[^>]*)?>", re.IGNORECASE), ""),
|
||||
(re.compile(r"<\s*/\s*li\s*>", re.IGNORECASE), "\n"),
|
||||
(re.compile(r"<\s*li(?:\s+[^>]*)?>", re.IGNORECASE), "• "),
|
||||
(re.compile(r"<\s*/?\s*(?:ul|ol)(?:\s+[^>]*)?>", re.IGNORECASE), ""),
|
||||
(re.compile(r"<\s*h[1-6](?:\s+[^>]*)?>", re.IGNORECASE), "<b>"),
|
||||
(re.compile(r"<\s*/\s*h[1-6]\s*>", re.IGNORECASE), "</b>\n"),
|
||||
)
|
||||
SEND_MESSAGE_HTML_TAG_PATTERN = re.compile(
|
||||
r"<\s*(/?)\s*([a-zA-Z][\w:-]*)\b([^>]*)>"
|
||||
)
|
||||
SEND_MESSAGE_HTML_ATTR_PATTERN_TEMPLATE = (
|
||||
r"""\b{attr_name}\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'>]+))"""
|
||||
)
|
||||
|
||||
|
||||
class SendMessageInput(BaseModel):
|
||||
"""发送消息工具的输入参数模型"""
|
||||
@@ -75,22 +30,12 @@ class SendMessageInput(BaseModel):
|
||||
None,
|
||||
description="Optional image URL to send together with the message on channels that support images (such as Telegram and Slack)",
|
||||
)
|
||||
parse_mode: Optional[str] = Field(
|
||||
None,
|
||||
description=(
|
||||
"Optional Telegram message body format. Supported values: HTML or MarkdownV2. "
|
||||
"Leave empty for default."
|
||||
),
|
||||
)
|
||||
|
||||
@model_validator(mode="after")
|
||||
def validate_payload(self) -> "SendMessageInput":
|
||||
"""校验消息内容和可选格式参数。"""
|
||||
if not self.message and not self.title and not self.image_url:
|
||||
raise ValueError("message、title、image_url 至少需要提供一个")
|
||||
self.parse_mode = SendMessageTool.normalize_parse_mode(self.parse_mode)
|
||||
if self.parse_mode == SEND_MESSAGE_PARSE_MODE_HTML:
|
||||
self.message = SendMessageTool.normalize_html_message(self.message)
|
||||
return self
|
||||
|
||||
|
||||
@@ -109,92 +54,12 @@ class SendMessageTool(MoviePilotTool):
|
||||
description: str = (
|
||||
"Send notification message to the user through configured notification channels "
|
||||
"(Telegram, Slack, WeChat, etc.). Supports optional image_url on channels that can "
|
||||
"send images. For Telegram, the optional parse_mode parameter controls message body "
|
||||
"rendering. Supported values are HTML and MarkdownV2; leave it empty for default. "
|
||||
"This is a terminal response tool: after it sends the user-facing message, do not "
|
||||
"send another final text reply with the same content."
|
||||
"send images. This is a terminal response tool: after it sends the user-facing "
|
||||
"message, do not send another final text reply with the same content."
|
||||
)
|
||||
args_schema: Type[BaseModel] = SendMessageInput
|
||||
require_admin: bool = True
|
||||
|
||||
@staticmethod
|
||||
def normalize_parse_mode(parse_mode: Optional[str]) -> Optional[str]:
|
||||
"""
|
||||
规范化 send_message 支持的 Telegram 格式参数。
|
||||
"""
|
||||
if not parse_mode:
|
||||
return None
|
||||
normalized = SEND_MESSAGE_PARSE_MODE_ALIASES.get(str(parse_mode).strip().lower())
|
||||
if not normalized:
|
||||
raise ValueError("parse_mode 仅支持 MarkdownV2 或 HTML")
|
||||
return normalized
|
||||
|
||||
@staticmethod
|
||||
def _extract_html_attr(attrs: str, attr_name: str) -> Optional[str]:
|
||||
"""
|
||||
从 HTML 标签属性中提取指定属性值。
|
||||
"""
|
||||
pattern = SEND_MESSAGE_HTML_ATTR_PATTERN_TEMPLATE.format(
|
||||
attr_name=re.escape(attr_name)
|
||||
)
|
||||
match = re.search(pattern, attrs or "", re.IGNORECASE)
|
||||
if not match:
|
||||
return None
|
||||
return next((value for value in match.groups() if value is not None), None)
|
||||
|
||||
@staticmethod
|
||||
def _normalize_html_tag(match: re.Match) -> str:
|
||||
"""
|
||||
规范化 Telegram 支持的 HTML 标签,并剥离不支持的属性。
|
||||
"""
|
||||
closing, tag_name, attrs = match.groups()
|
||||
tag_name = tag_name.lower()
|
||||
if tag_name not in SEND_MESSAGE_HTML_ALLOWED_TAGS:
|
||||
raise ValueError(f"HTML 标签 <{tag_name}> 不受 Telegram 支持")
|
||||
|
||||
if closing:
|
||||
return f"</{tag_name}>"
|
||||
|
||||
if tag_name == "a":
|
||||
href = SendMessageTool._extract_html_attr(attrs, "href")
|
||||
if not href:
|
||||
raise ValueError("HTML 标签 <a> 必须包含 href 属性")
|
||||
return f'<a href="{html_utils.escape(href, quote=True)}">'
|
||||
|
||||
if tag_name == "span":
|
||||
class_name = SendMessageTool._extract_html_attr(attrs, "class")
|
||||
if class_name != "tg-spoiler":
|
||||
raise ValueError('HTML 标签 <span> 仅支持 class="tg-spoiler"')
|
||||
return '<span class="tg-spoiler">'
|
||||
|
||||
if tag_name == "blockquote":
|
||||
if re.search(r"(^|\s)expandable(\s|/|$)", attrs or "", re.IGNORECASE):
|
||||
return "<blockquote expandable>"
|
||||
return "<blockquote>"
|
||||
|
||||
if tag_name == "code":
|
||||
class_name = SendMessageTool._extract_html_attr(attrs, "class")
|
||||
if class_name and class_name.startswith("language-"):
|
||||
escaped_class = html_utils.escape(class_name, quote=True)
|
||||
return f'<code class="{escaped_class}">'
|
||||
return "<code>"
|
||||
|
||||
return f"<{tag_name}>"
|
||||
|
||||
@staticmethod
|
||||
def normalize_html_message(message: Optional[str]) -> Optional[str]:
|
||||
"""
|
||||
规范化 Agent 生成的 Telegram HTML 正文。
|
||||
"""
|
||||
if not message:
|
||||
return message
|
||||
normalized = message
|
||||
for pattern, replacement in SEND_MESSAGE_HTML_NORMALIZATION_RULES:
|
||||
normalized = pattern.sub(replacement, normalized)
|
||||
return SEND_MESSAGE_HTML_TAG_PATTERN.sub(
|
||||
SendMessageTool._normalize_html_tag, normalized
|
||||
)
|
||||
|
||||
def get_tool_message(self, **kwargs) -> Optional[str]:
|
||||
"""根据消息参数生成友好的提示消息"""
|
||||
message = kwargs.get("message", "") or ""
|
||||
@@ -218,22 +83,15 @@ class SendMessageTool(MoviePilotTool):
|
||||
message: Optional[str] = None,
|
||||
title: Optional[str] = None,
|
||||
image_url: Optional[str] = None,
|
||||
parse_mode: Optional[str] = None,
|
||||
**kwargs,
|
||||
) -> str:
|
||||
"""发送消息到当前会话渠道。"""
|
||||
title = title or ("图片" if image_url and not message else "")
|
||||
text = message or ""
|
||||
try:
|
||||
parse_mode = self.normalize_parse_mode(parse_mode)
|
||||
if parse_mode == SEND_MESSAGE_PARSE_MODE_HTML:
|
||||
text = self.normalize_html_message(text) or ""
|
||||
except ValueError as e:
|
||||
return str(e)
|
||||
|
||||
logger.info(
|
||||
f"执行工具: {self.name}, 参数: title={title}, message={text}, "
|
||||
f"image_url={image_url}, parse_mode={parse_mode}"
|
||||
f"image_url={image_url}"
|
||||
)
|
||||
try:
|
||||
await self.send_notification_message(
|
||||
@@ -246,7 +104,6 @@ class SendMessageTool(MoviePilotTool):
|
||||
title=title,
|
||||
text=text,
|
||||
image=image_url,
|
||||
parse_mode=parse_mode,
|
||||
)
|
||||
)
|
||||
self._agent_context["user_reply_sent"] = True
|
||||
|
||||
Reference in New Issue
Block a user