Expand description
Hooks are functions that are called at specific points in the agent lifecycle.
Since rust does not have async closures, hooks have to return a boxed, pinned async block themselves.
§Example
agent.before_all(move |agent: &Agent| {
Box::pin(async move {
agent.context().add_message(ChatMessage::new_user("Hello, world")).await;
Ok(())
})
});
Rust has a long outstanding issue where it captures outer lifetimes when returning an impl that also has lifetimes, see this issue
This can happen if you write a method like fn return_hook(&self) -> impl HookFn
, where the
owner also has a lifetime.
The trick is to set an explicit lifetime on self, and hook, where self must outlive the hook.
§Example
struct SomeHook<'thing> {
thing: &'thing str
}
impl<'thing> SomeHook<'thing> {
fn return_hook<'tool>(&'thing self) -> impl BeforeAllFn + 'tool where 'thing: 'tool {
move |_: &Agent| {
Box::pin(async move {{ Ok(())}})
}
}
}
Enums§
Traits§
- After
Completion Fn - After
Each Fn - After
Tool Fn - Hooks that are called after each tool
- Before
AllFn - Before
Completion Fn - Before
Tool Fn - Hooks that are called before each tool
- Message
Hook Fn - Hooks that are called when a new message is added to the
AgentContext
- OnStart
Fn - Hooks that are called when the agent starts, either from pending or stopped
- OnStop
Fn - Hooks that are called when the agent stop
- OnStream
Fn