ChatController

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

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

Convenience builder for ChatController.

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)

Uses [crate::utils::asynchronous::BasicSpawner] to spawn async tasks.

See documentation there for details.

pub fn append_plugin<P>( &mut self, plugin: P, ) -> ChatControllerPluginRegistrationId
where 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, ) -> ChatControllerPluginRegistrationId
where P: ChatControllerPlugin + 'static,

Registers a plugin to extend the controller behavior. Runs before all other plugins.

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 state(&self) -> &ChatState

Read-only access to state.

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>)

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

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>>)

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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert 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>

Convert 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)

Convert &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)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<T> PlatformSend for T
where T: PlatformSendInner,

§

impl<T> PlatformSendInner for T
where T: Send,