swiftide_agents

Module hooks

Source
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 |context: &dyn AgentContext| {
    Box::pin(async move {
        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 |_: &dyn AgentContext| {
     Box::pin(async move {{ Ok(())}})
    }
  }
}

Enums§

Hook
Wrapper around the different types of hooks
HookTypes
Auto-generated discriminant enum variants

Traits§

AfterCompletionFn
AfterEachFn
AfterToolFn
Hooks that are called after each tool
BeforeAllFn
BeforeCompletionFn
BeforeToolFn
Hooks that are called before each tool
MessageHookFn
Hooks that are called when a new message is added to the AgentContext