123 lines
2.8 KiB
Rust
123 lines
2.8 KiB
Rust
use std::collections::HashMap;
|
|
use uuid::Uuid;
|
|
|
|
/// 命令响应 - 与渠道无关的统一响应格式
|
|
#[derive(Debug, Clone)]
|
|
pub struct CommandResponse {
|
|
/// 对应的请求ID
|
|
pub request_id: Uuid,
|
|
/// 是否成功
|
|
pub success: bool,
|
|
/// 响应数据(可选)
|
|
pub data: Option<serde_json::Value>,
|
|
/// 响应消息列表
|
|
pub messages: Vec<ResponseMessage>,
|
|
/// 错误信息(如果有)
|
|
pub error: Option<CommandError>,
|
|
/// 元数据
|
|
pub metadata: HashMap<String, String>,
|
|
}
|
|
|
|
impl CommandResponse {
|
|
/// 创建成功的响应
|
|
pub fn success(request_id: Uuid) -> Self {
|
|
Self {
|
|
request_id,
|
|
success: true,
|
|
data: None,
|
|
messages: Vec::new(),
|
|
error: None,
|
|
metadata: HashMap::new(),
|
|
}
|
|
}
|
|
|
|
/// 创建失败的响应
|
|
pub fn error(request_id: Uuid, error: CommandError) -> Self {
|
|
Self {
|
|
request_id,
|
|
success: false,
|
|
data: None,
|
|
messages: Vec::new(),
|
|
error: Some(error),
|
|
metadata: HashMap::new(),
|
|
}
|
|
}
|
|
|
|
/// 添加响应消息
|
|
pub fn with_message(mut self, kind: MessageKind, content: impl Into<String>) -> Self {
|
|
self.messages.push(ResponseMessage {
|
|
kind,
|
|
content: content.into(),
|
|
metadata: HashMap::new(),
|
|
});
|
|
self
|
|
}
|
|
|
|
/// 添加元数据
|
|
pub fn with_metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
|
|
self.metadata.insert(key.into(), value.into());
|
|
self
|
|
}
|
|
|
|
/// 设置响应数据
|
|
pub fn with_data(mut self, data: serde_json::Value) -> Self {
|
|
self.data = Some(data);
|
|
self
|
|
}
|
|
}
|
|
|
|
/// 响应消息
|
|
#[derive(Debug, Clone)]
|
|
pub struct ResponseMessage {
|
|
/// 消息类型
|
|
pub kind: MessageKind,
|
|
/// 消息内容
|
|
pub content: String,
|
|
/// 消息元数据
|
|
pub metadata: HashMap<String, String>,
|
|
}
|
|
|
|
/// 消息类型
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
pub enum MessageKind {
|
|
/// 普通文本
|
|
Text,
|
|
/// 通知
|
|
Notification,
|
|
/// 错误
|
|
Error,
|
|
/// 工具调用
|
|
ToolCall,
|
|
/// 工具结果
|
|
ToolResult,
|
|
/// 工具等待
|
|
ToolPending,
|
|
}
|
|
|
|
/// 命令错误
|
|
#[derive(Debug, Clone)]
|
|
pub struct CommandError {
|
|
/// 错误代码
|
|
pub code: String,
|
|
/// 错误消息
|
|
pub message: String,
|
|
}
|
|
|
|
impl CommandError {
|
|
/// 创建新的命令错误
|
|
pub fn new(code: impl Into<String>, message: impl Into<String>) -> Self {
|
|
Self {
|
|
code: code.into(),
|
|
message: message.into(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl std::fmt::Display for CommandError {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
write!(f, "[{}] {}", self.code, self.message)
|
|
}
|
|
}
|
|
|
|
impl std::error::Error for CommandError {}
|