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>, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait,
'life1: 'async_trait,
Self: '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>, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait { ... }
fn on_disconnect<'life0, 'life1, 'async_trait>(
&'life0 self,
_ctx: &'life1 mut SecretaryContext<Self::State>,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
where 'life0: 'async_trait,
'life1: 'async_trait,
Self: '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§
Sourcetype Input: SecretaryInput
type Input: SecretaryInput
秘书接受的输入类型
Sourcetype Output: SecretaryOutput
type Output: SecretaryOutput
秘书产生的输出类型
Required Methods§
Sourcefn 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>, Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
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>, Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
Sourcefn initial_state(&self) -> Self::State
fn initial_state(&self) -> Self::State
初始化状态
创建秘书的初始状态
Provided Methods§
Sourcefn welcome_message(&self) -> Option<Self::Output>
fn welcome_message(&self) -> Option<Self::Output>
欢迎消息
当秘书启动时,可以选择发送一条欢迎消息给用户。
返回 None 表示不发送欢迎消息。
Sourcefn periodic_check<'life0, 'life1, 'async_trait>(
&'life0 self,
_ctx: &'life1 mut SecretaryContext<Self::State>,
) -> Pin<Box<dyn Future<Output = Result<Vec<Self::Output>, Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
fn periodic_check<'life0, 'life1, 'async_trait>(
&'life0 self,
_ctx: &'life1 mut SecretaryContext<Self::State>,
) -> Pin<Box<dyn Future<Output = Result<Vec<Self::Output>, Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
定时检查
在事件循环的每次迭代中调用(当没有用户输入时)。 可以用来检查后台任务、发送提醒等。
默认实现不做任何事情。
Sourcefn on_disconnect<'life0, 'life1, 'async_trait>(
&'life0 self,
_ctx: &'life1 mut SecretaryContext<Self::State>,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
fn on_disconnect<'life0, 'life1, 'async_trait>(
&'life0 self,
_ctx: &'life1 mut SecretaryContext<Self::State>,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
连接断开时的清理
当用户连接断开时调用,可以用来保存状态、清理资源等。
Sourcefn handle_error(&self, _error: &Error) -> Option<Self::Output>
fn handle_error(&self, _error: &Error) -> Option<Self::Output>
错误处理
当处理输入时发生错误,调用此方法生成错误响应。
默认实现返回 None,表示不向用户发送错误消息。