Skip to main content

SecretaryBehavior

Trait SecretaryBehavior 

Source
pub trait SecretaryBehavior: Send + Sync {
    type Input: SecretaryInput;
    type Output: SecretaryOutput;
    type State: Send + Sync + 'static;

    // Required methods
    fn handle_input<'life0, 'life1, 'async_trait>(
        &'life0 self,
        input: Self::Input,
        ctx: &'life1 mut SecretaryContext<Self::State>,
    ) -> Pin<Box<dyn Future<Output = Result<Vec<Self::Output>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn initial_state(&self) -> Self::State;

    // Provided methods
    fn welcome_message(&self) -> Option<Self::Output> { ... }
    fn periodic_check<'life0, 'life1, 'async_trait>(
        &'life0 self,
        _ctx: &'life1 mut SecretaryContext<Self::State>,
    ) -> Pin<Box<dyn Future<Output = Result<Vec<Self::Output>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn on_disconnect<'life0, 'life1, 'async_trait>(
        &'life0 self,
        _ctx: &'life1 mut SecretaryContext<Self::State>,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn handle_error(&self, _error: &Error) -> Option<Self::Output> { ... }
}
Expand description

秘书行为Trait - 核心抽象

这是框架最核心的trait,定义了秘书如何响应用户输入。 开发者需要实现这个trait来创建自定义的秘书。

§类型参数

  • Input: 秘书接受的输入类型
  • Output: 秘书产生的输出类型
  • State: 秘书状态类型

§示例

struct MySecretary {
    name: String,
    llm: Arc<dyn LLMProvider>,
}

#[async_trait]
impl SecretaryBehavior for MySecretary {
    type Input = MyInput;
    type Output = MyOutput;
    type State = MyState;

    async fn handle_input(
        &self,
        input: Self::Input,
        ctx: &mut SecretaryContext<Self::State>,
    ) -> anyhow::Result<Vec<Self::Output>> {
        match input {
            MyInput::TextMessage(text) => {
                // 处理文本消息
                Ok(vec![MyOutput::TextReply(format!("收到: {}", text))])
            }
            _ => Ok(vec![]),
        }
    }

    fn welcome_message(&self) -> Option<Self::Output> {
        Some(MyOutput::TextReply(format!("你好,我是{}!", self.name)))
    }

    fn initial_state(&self) -> Self::State {
        MyState::new()
    }
}

Required Associated Types§

Source

type Input: SecretaryInput

秘书接受的输入类型

Source

type Output: SecretaryOutput

秘书产生的输出类型

Source

type State: Send + Sync + 'static

秘书的状态类型

Required Methods§

Source

fn handle_input<'life0, 'life1, 'async_trait>( &'life0 self, input: Self::Input, ctx: &'life1 mut SecretaryContext<Self::State>, ) -> Pin<Box<dyn Future<Output = Result<Vec<Self::Output>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

处理用户输入

这是秘书的核心方法,当收到用户输入时会调用此方法。 返回要发送给用户的输出消息列表。

§参数
  • input: 用户输入
  • ctx: 秘书上下文,包含状态和共享资源
§返回

返回要发送给用户的输出消息列表

Source

fn initial_state(&self) -> Self::State

初始化状态

创建秘书的初始状态

Provided Methods§

Source

fn welcome_message(&self) -> Option<Self::Output>

欢迎消息

当秘书启动时,可以选择发送一条欢迎消息给用户。 返回 None 表示不发送欢迎消息。

Source

fn periodic_check<'life0, 'life1, 'async_trait>( &'life0 self, _ctx: &'life1 mut SecretaryContext<Self::State>, ) -> Pin<Box<dyn Future<Output = Result<Vec<Self::Output>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

定时检查

在事件循环的每次迭代中调用(当没有用户输入时)。 可以用来检查后台任务、发送提醒等。

默认实现不做任何事情。

Source

fn on_disconnect<'life0, 'life1, 'async_trait>( &'life0 self, _ctx: &'life1 mut SecretaryContext<Self::State>, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

连接断开时的清理

当用户连接断开时调用,可以用来保存状态、清理资源等。

Source

fn handle_error(&self, _error: &Error) -> Option<Self::Output>

错误处理

当处理输入时发生错误,调用此方法生成错误响应。 默认实现返回 None,表示不向用户发送错误消息。

Implementors§