PicoBot/src/command/context.rs

83 lines
2.1 KiB
Rust

use std::collections::HashMap;
use uuid::Uuid;
/// 命令执行上下文 - 携带执行所需信息
#[derive(Debug, Clone)]
pub struct CommandContext {
/// 唯一请求ID
pub request_id: Uuid,
/// 当前会话ID
pub session_id: Option<String>,
/// 当前聊天ID
pub chat_id: Option<String>,
/// 发送者ID
pub sender_id: String,
/// 额外元数据
pub metadata: HashMap<String, String>,
}
impl CommandContext {
/// 创建新的命令上下文
pub fn new(sender_id: impl Into<String>) -> Self {
Self {
request_id: Uuid::new_v4(),
session_id: None,
chat_id: None,
sender_id: sender_id.into(),
metadata: HashMap::new(),
}
}
/// 设置会话ID
pub fn with_session_id(mut self, session_id: impl Into<String>) -> Self {
self.session_id = Some(session_id.into());
self
}
/// 设置聊天ID
pub fn with_chat_id(mut self, chat_id: impl Into<String>) -> Self {
self.chat_id = Some(chat_id.into());
self
}
/// 添加元数据
pub fn with_metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.metadata.insert(key.into(), value.into());
self
}
}
/// 适配器上下文 - 用于输入解析
#[derive(Debug, Clone)]
pub struct AdapterContext {
/// 当前会话ID
pub session_id: Option<String>,
/// 当前聊天ID
pub chat_id: Option<String>,
/// 发送者ID
pub sender_id: String,
}
impl AdapterContext {
/// 创建新的适配器上下文
pub fn new(sender_id: impl Into<String>) -> Self {
Self {
session_id: None,
chat_id: None,
sender_id: sender_id.into(),
}
}
/// 设置会话ID
pub fn with_session_id(mut self, session_id: impl Into<String>) -> Self {
self.session_id = Some(session_id.into());
self
}
/// 设置聊天ID
pub fn with_chat_id(mut self, chat_id: impl Into<String>) -> Self {
self.chat_id = Some(chat_id.into());
self
}
}