ForcePilot/backend/package/yuxi/channels/contract/dtos/agent_run.py

207 lines
8.9 KiB
Python
Raw Normal View History

"""AgentRun DTO。
定义 Agent 运行端口的命令与上下文值对象包括 Agent 运行 ID渠道格式
规格Agent 运行上下文渠道上下文注记与 Agent 运行命令所有 DTO 均为
``dataclass(frozen=True)``仅依赖标准库与契约层内部类型用于 Agent
运行的命令传递与渠道上下文注入FR-10 / FR-11集合字段使用 tuple
保证 frozen dataclass 的不可变语义
"""
from __future__ import annotations
from dataclasses import dataclass
from typing import Literal
from yuxi.channels.contract.dtos.channel import ChannelType, UserIdentity
from yuxi.channels.contract.dtos.command import CommandResult
from yuxi.channels.contract.dtos.common import MessageContent, MessageFormat
from yuxi.channels.contract.dtos.mention import MentionTarget
from yuxi.channels.contract.dtos.pairing import DmDecision
from yuxi.channels.contract.dtos.tools import ChannelTool
from yuxi.channels.contract.errors import ValidationError
@dataclass(frozen=True)
class AgentRunId:
"""Agent 运行 ID。
标识一次 Agent 运行的唯一 ID用于关联 Agent 执行链路与出站消息
字段
value: Agent 运行 ID 字符串
"""
value: str
@dataclass(frozen=True)
class ChannelFormatSpec:
"""渠道格式规格。
描述渠道支持的消息格式规格包括格式说明与支持的格式列表用于
Agent 上下文注入以指导消息渲染集合字段使用 tuple 以保证不可变
字段
format_description: 格式说明
supported_formats: 支持的格式列表默认空 tuple
"""
format_description: str
supported_formats: tuple[MessageFormat, ...] = ()
@dataclass(frozen=True)
class ChannelContextNote:
"""渠道上下文注记。
描述渠道上下文的注记信息包括系统提示附录与上下文备注用于 Agent
上下文注入以补充渠道特定信息
字段
system_prompt_appendix: 系统提示附录
context_remark: 上下文备注可选
"""
system_prompt_appendix: str
context_remark: str | None = None
@dataclass(frozen=True)
class AgentRunContext:
"""Agent 运行上下文。
描述 Agent 运行的渠道上下文包括渠道类型账户 ID对端 ID群组 ID
话题 ID发送者 ID渠道格式规格渠道上下文注记渠道工具列表统一
用户身份DM 安全决策与配对 ID用于 Agent 上下文注入FR-10与渠道
工具注册FR-11
渠道字段均为可选默认空以支持非渠道来源如直接 API 调用构造
上下文FR-10``agent_id`` ``AgentRunEnqueueStage``
``route_binding.agent_binding`` 提取携带路由解析出的 Agent slug
``AgentRunAdapter`` 传递给 ``create_agent_run_view`` 创建运行
``channel_context_note`` ``AgentRunEnqueueStage`` 调用插件方法
``buildAgentContextNote``生成携带渠道特定的系统提示附录与上下文
备注``channel_tools`` ``AgentRunEnqueueStage`` 读取
``agent_tools_enabled`` 开关后调用插件方法``getChannelTools``填充
携带渠道原生工具声明 Agent 运行时合并到可用工具列表FR-11
``unified_identity`` ``identity_resolve_stage`` 解析得到携带
统一用户身份信息用于 Agent 上下文注入身份维度``dm_decision``
``pairing_id`` ``security_stage`` 产出分别携带 DM 安全决策结果
与在 ``pending_pairing`` 决策下生成的配对标识 Agent 运行感知 DM
安全处置状态
字段
agent_id: Agent slug默认空串由路由绑定解析用于创建 Agent 运行
channel_type: 渠道类型可选默认 None
channel_account_id: 渠道账户 ID默认空串
channel_peer_id: 对端 ID默认空串
channel_group_id: 群组 ID可选
channel_topic_id: 话题 ID可选
channel_sender_id: 发送者 ID可选
channel_format_spec: 渠道格式规格可选
channel_context_note: 渠道上下文注记可选由插件方法生成
channel_tools: 渠道工具列表默认空 tuple由插件方法 ``getChannelTools``
生成FR-11非渠道来源或开关关闭时为空 tuple不合并任何
渠道工具
unified_identity: 统一用户身份可选默认 None来源
``identity_resolve_stage`` 解析结果携带用户在渠道侧或外部
身份系统的身份信息用于 Agent 上下文注入身份维度
service_user_uid: 服务用户 UIDdeprecated默认空串后续迭代移除
channel_user_identity: 渠道用户身份可选默认 None
``unified_identity`` 的替代字段
dm_decision: DM 安全决策可选默认 None来源
``security_stage`` 决策结果携带 DM 消息处置决策allow /
deny / pending_pairing / whitelist Agent 运行感知 DM
安全处置状态
pairing_id: 配对 ID可选默认 None来源 ``security_stage``
``pending_pairing`` 决策下生成的配对标识用于关联后续配对
审批流程与审计
Agent 协作链路字段默认 None 表示非协作场景``parent_run_id``
标识父 AgentRun委派发起方``delegated_by_agent_id`` 标识委派发起的
Agent slug``collaboration_id`` 关联同一协作链路的多个 AgentRun
"""
agent_id: str = ""
channel_type: ChannelType | None = None
channel_account_id: str = ""
channel_peer_id: str = ""
channel_group_id: str | None = None
channel_topic_id: str | None = None
channel_sender_id: str | None = None
channel_format_spec: ChannelFormatSpec | None = None
channel_context_note: ChannelContextNote | None = None
channel_tools: tuple[ChannelTool, ...] = ()
unified_identity: UserIdentity | None = None # deprecated: 后续迭代移除,使用 channel_user_identity 替代
service_user_uid: str = ""
channel_user_identity: UserIdentity | None = None
dm_decision: DmDecision | None = None
pairing_id: str | None = None
parent_run_id: str | None = None
delegated_by_agent_id: str | None = None
collaboration_id: str | None = None
@dataclass(frozen=True)
class AgentRunCmd:
"""Agent 运行命令FR-10 / FR-15
Agent 运行端口方法引用描述一次 Agent 运行请求携带会话 ID
消息内容上下文与追踪 ID用于驱动 Agent 执行
字段
conversation_id: 会话 ID
message_content: 消息内容
context: Agent 运行上下文可选
trace_id: 追踪 ID可选
source: 运行来源 "channel" / "api" / "admin" / "delegation"用于 Agent
上下文注入区分来源FR-10可选默认空串
command_result: 非静默命令执行结果可选FR-15作为上下文
注入 Agent 运行 Agent 感知命令执行结果
"""
conversation_id: str
message_content: MessageContent
context: AgentRunContext | None = None
trace_id: str | None = None
source: Literal["channel", "api", "admin", "delegation"] = ""
command_result: CommandResult | None = None
def __post_init__(self) -> None:
"""校验必填字段非空。
``conversation_id`` ``message_content.text`` 必须非空
在构造时即抛出 ``ValidationError``adapter 不再做该校验INV-8 /
§7.3 端口契约前置条件
"""
if not self.conversation_id:
raise ValidationError("conversation_id", "must not be empty")
if not self.message_content.text:
raise ValidationError("message_content", "must not be empty")
@dataclass(frozen=True)
class AgentRoutingDecision:
"""Agent 路由决策。
描述入站消息中识别到的 Agent 路由决策 ClassifyStage
mention_context.target_agent_ids 非空时构造供下游 RouteStage
决定路由目标 Agent 协作
字段
target_agent_id: 目标 Agent ID必填
source: 触发路由的 MentionTarget可选默认 None
collaboration_id: 协作链路 ID可选默认 None
is_handoff: 是否为 Agent 移交默认 False
"""
target_agent_id: str
source: MentionTarget | None = None
collaboration_id: str | None = None
is_handoff: bool = False
def __post_init__(self) -> None:
"""校验必填字段非空。"""
if not self.target_agent_id:
raise ValidationError("target_agent_id", "must not be empty")