from typing import List, Optional, Union from pydantic import Field from app.workflow.actions import BaseAction, ActionChain from app.application.configuration import get_chain_runtime_config_snapshot from app.schemas.workflow import ActionParams from app.schemas.workflow import ActionContext from app.schemas.message import Message class SendMessageParams(ActionParams): """ 发送消息参数 """ client: Optional[List[str]] = Field(default=[], description="消息渠道") userid: Optional[Union[str, int]] = Field(default=None, description="用户ID") class SendMessageAction(BaseAction): """ 发送消息 """ contract = {} def __init__(self, action_id: str): super().__init__(action_id) name = "发送消息" description = "发送任务执行消息" data = SendMessageParams().model_dump() @property def success(self) -> bool: return self.done def execute(self, workflow_id: int, params: dict, context: ActionContext) -> ActionContext: """ 发送messages中的消息 """ params = SendMessageParams(**params) msg_text = f"当前进度:{context.progress}%" index = 1 if context.execute_history: for history in context.execute_history: if not history.message: continue msg_text += f"\n{index}. {history.action}:{history.message}" index += 1 # 发送消息 if not params.client: params.client = [""] for client in params.client: ActionChain().post_message( Message( source=client, userid=params.userid, title="【工作流执行结果】", text=msg_text, link=get_chain_runtime_config_snapshot().workflow_url ) ) self.job_done() return context