Struct ChatController
pub struct ChatController { /* private fields */ }Expand description
State management abstraction specialized in handling the core complex chat logic.
This follow a controller-like interface to be part of an MVC-like architecture. The controller receives different inputs, which causes work to happen inside, and finally causes state to be changed and notified.
Inputs may be state mutations (which are simply closures that are executed immediately), or tasks (more complex operations that may be async and cause multiple mutations over time).
The main objective of the controller is to implement default reusable behavior, but this controller idea is extended with the concept of “plugins”, which have various objetives like getting notified of state changes to integrate with a view or to customize the behavior of the controller itself.
Implementations§
§impl ChatController
impl ChatController
pub fn new_arc() -> Arc<Mutex<ChatController>>
pub fn new_arc() -> Arc<Mutex<ChatController>>
Creates a new reference-counted ChatController.
This is the only public way to create a ChatController. A weak ref is
internally used and passed to built-in async tasks (or threads) and you
may find this useful when integrating the controller with your framework
of choice.
pub fn builder() -> ChatControllerBuilder
pub fn builder() -> ChatControllerBuilder
Convenience builder for ChatController.
pub fn set_spawner<S>(&mut self, spawner: Option<S>)where
S: ErasedSpawner + 'static,
pub fn set_spawner<S>(&mut self, spawner: Option<S>)where
S: ErasedSpawner + 'static,
Sets the spawner used to spawn async tasks.
pub fn set_basic_spawner(&mut self)
pub fn set_basic_spawner(&mut self)
Uses [crate::utils::asynchronous::BasicSpawner] to spawn async tasks.
See documentation there for details.
pub fn append_plugin<P>(
&mut self,
plugin: P,
) -> ChatControllerPluginRegistrationIdwhere
P: ChatControllerPlugin + 'static,
pub fn append_plugin<P>(
&mut self,
plugin: P,
) -> ChatControllerPluginRegistrationIdwhere
P: ChatControllerPlugin + 'static,
Registers a plugin to extend the controller behavior. Runs after all other plugins.
pub fn prepend_plugin<P>(
&mut self,
plugin: P,
) -> ChatControllerPluginRegistrationIdwhere
P: ChatControllerPlugin + 'static,
pub fn prepend_plugin<P>(
&mut self,
plugin: P,
) -> ChatControllerPluginRegistrationIdwhere
P: ChatControllerPlugin + 'static,
Registers a plugin to extend the controller behavior. Runs before all other plugins.
pub fn remove_plugin(&mut self, id: ChatControllerPluginRegistrationId)
pub fn remove_plugin(&mut self, id: ChatControllerPluginRegistrationId)
Unregisters a previously registered plugin.
pub fn plugins( &self, ) -> impl Iterator<Item = (ChatControllerPluginRegistrationId, &dyn ChatControllerPlugin)>
pub fn plugins_mut<'a>( &'a mut self, ) -> impl Iterator<Item = (ChatControllerPluginRegistrationId, &'a mut dyn ChatControllerPlugin)> + 'a
pub fn plugins_as<P>(
&self,
) -> impl Iterator<Item = (ChatControllerPluginRegistrationId, &P)>where
P: ChatControllerPlugin + 'static,
pub fn plugins_mut_as<P>(
&mut self,
) -> impl Iterator<Item = (ChatControllerPluginRegistrationId, &mut P)>where
P: ChatControllerPlugin + 'static,
pub fn dispatch_mutations(&mut self, mutations: Vec<ChatStateMutation>)
pub fn dispatch_mutations(&mut self, mutations: Vec<ChatStateMutation>)
Dispatch mutations to state in a single transactional batch.
on_state_mutation will be called between each mutation application. However,
on_state_ready will only be called once, after all mutations grouped in
this call have been applied.
This means each reported mutation can be analyzed against the previous state snapshot, before the final state is reported.
At [on_state_mutation], any [ListMutation] can use log() against
the state snapshot without getting wrong indices.
This also means that a “delete” may not have effect on the state reported to
on_state_ready if a subsequent mutation in the same batch “re-inserts” the deleted
item. This is important for plugin implementers to understand. In this example, if
your plugin has side effects over “deleted” items, it’s better to simply flag
the delation on on_state_mutation and perform a full scan on the whole state
that is commited to on_state_ready.
As the “caller” of this method, you are responsible for grouping mutations that may alter the effect of each other.
pub fn dispatch_mutation(&mut self, mutation: impl Into<ChatStateMutation>)
pub fn dispatch_mutation(&mut self, mutation: impl Into<ChatStateMutation>)
Shorthand for dispatching a single mutation.
Accepts any type that can be converted into a ChatStateMutation.
See Self::dispatch_mutations for details about mutation grouping and plugin
notifications.
pub fn dangerous_state_mut(&mut self) -> &mut ChatState
pub fn dangerous_state_mut(&mut self) -> &mut ChatState
Get access to state to perform arbitrary un-notified mutations.
§Danger
Plugins will not get notified of this, and you may cause serious inconsistencies.
This function only exists to perform quick modifications that are reverted almost immediately, leaving state as it was before.
If you are using this, you should keep the lock to the controller until you undo what you did.
pub fn dispatch_task(&mut self, task: ChatTask)
pub fn set_client(&mut self, client: Option<Box<dyn BotClient>>)
pub fn set_client(&mut self, client: Option<Box<dyn BotClient>>)
Changes the client used by this controller when sending messages and laoding bots.
NOTE: Calling this will reset the current bots and load status.
pub fn bot_client(&self) -> Option<&dyn BotClient>
pub fn bot_client_mut(&mut self) -> Option<&mut (dyn BotClient + 'static)>
pub fn tool_manager(&self) -> Option<&McpManagerClient>
pub fn tool_manager_mut(&mut self) -> Option<&mut McpManagerClient>
pub fn set_tool_manager(&mut self, tool_manager: Option<McpManagerClient>)
Auto Trait Implementations§
impl Freeze for ChatController
impl !RefUnwindSafe for ChatController
impl Send for ChatController
impl !Sync for ChatController
impl Unpin for ChatController
impl !UnwindSafe for ChatController
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.