Skip to main content

AgentContext

Struct AgentContext 

Source
pub struct AgentContext<B: ContextBackend> { /* private fields */ }
Expand description

LLM 对话上下文管理器,kameo Actor。

管理三区 + Scratch 消息模型(immutable → compressed → incremental → scratch),提供:

§构造

let ctx = AgentContext::new(backend, vec![])
    .with_on_change(|event| { /* 处理变更 */ });
let actor = AgentContext::spawn(ctx);

Implementations§

Source§

impl<B: ContextBackend> AgentContext<B>

Source

pub fn new(backend: B, immutable: Vec<B::Message>) -> Self

创建新的上下文管理器。

  • backend: 实现了 ContextBackend 的 LLM 后端实例
  • immutable: 初始不可变消息(系统提示词等),放入 immutable 区
Source

pub fn with_on_change( self, f: impl Fn(ChangeEvent<B::Message>) + Send + Sync + 'static, ) -> Self

注册增量区变更回调。

每次对 incremental 区的写操作(追加/更新/插入/移除/清空等)都会触发此回调。 用于 CLI 实时展示、日志记录等场景。

Source

pub fn with_on_compressed( self, f: impl Fn(Vec<B::Message>, Vec<B::Message>) -> (Vec<B::Message>, Vec<B::Message>) + Send + Sync + 'static, ) -> Self

注册压缩后处理回调。

CompressMsg 生成摘要后、写入 compressed 区之前调用。 回调接收 (摘要消息列表, 保留消息列表),返回 (最终摘要, 最终保留)。 用于自定义后处理(如过滤、重新排序摘要内容)。

Trait Implementations§

Source§

impl<B: ContextBackend> Actor for AgentContext<B>

Source§

type Args = AgentContext<B>

Arguments to initialize the actor. Read more
Source§

type Error = Infallible

Actor error type. Read more
Source§

fn name() -> &'static str

The name of the actor, which can be useful for logging or debugging. Read more
Source§

async fn on_start( state: Self::Args, _actor_ref: ActorRef<Self>, ) -> Result<Self, Self::Error>

Called when the actor starts, before it processes any messages. Read more
Source§

fn supervision_strategy() -> SupervisionStrategy

Defines the supervision strategy for this actor when acting as a supervisor. Read more
Source§

fn on_message( &mut self, msg: Box<dyn DynMessage<Self>>, actor_ref: ActorRef<Self>, tx: Option<Sender<Result<Box<dyn Any + Send>, SendError<Box<dyn Any + Send>, Box<dyn Any + Send>>>>>, stop: &mut bool, ) -> impl Future<Output = Result<(), Box<dyn ReplyError>>> + Send

Called when the actor receives a message to be processed. Read more
Source§

fn on_panic( &mut self, actor_ref: WeakActorRef<Self>, err: PanicError, ) -> impl Future<Output = Result<ControlFlow<ActorStopReason>, Self::Error>> + Send

Called when the actor encounters a panic or an error during “tell” message handling. Read more
Called when a linked actor dies. Read more
Source§

fn on_stop( &mut self, actor_ref: WeakActorRef<Self>, reason: ActorStopReason, ) -> impl Future<Output = Result<(), Self::Error>> + Send

Called before the actor stops. Read more
Source§

fn next( &mut self, actor_ref: WeakActorRef<Self>, mailbox_rx: &mut MailboxReceiver<Self>, ) -> impl Future<Output = Result<Option<Signal<Self>>, Self::Error>> + Send

Awaits the next signal typically from the mailbox. Read more
Source§

impl<B: ContextBackend> Message<AppendMsg<<B as ContextBackend>::Message>> for AgentContext<B>

Source§

type Reply = ()

The reply sent back to the message caller.
Source§

async fn handle( &mut self, msg: AppendMsg<B::Message>, _ctx: &mut Context<Self, Self::Reply>, ) -> Self::Reply

Handler for this message.
Source§

fn name() -> &'static str

The name of the message, which can be useful for logging or debugging. Read more
Source§

impl<B: ContextBackend> Message<ClearMsg> for AgentContext<B>

Source§

type Reply = ()

The reply sent back to the message caller.
Source§

async fn handle( &mut self, _msg: ClearMsg, _ctx: &mut Context<Self, Self::Reply>, ) -> Self::Reply

Handler for this message.
Source§

fn name() -> &'static str

The name of the message, which can be useful for logging or debugging. Read more
Source§

impl<B: ContextBackend> Message<CompressMsg<<B as ContextBackend>::Opts>> for AgentContext<B>

Source§

type Reply = Result<(), AgentError>

The reply sent back to the message caller.
Source§

async fn handle( &mut self, msg: CompressMsg<B::Opts>, _ctx: &mut Context<Self, Self::Reply>, ) -> Self::Reply

Handler for this message.
Source§

fn name() -> &'static str

The name of the message, which can be useful for logging or debugging. Read more
Source§

impl<B: ContextBackend> Message<CompressedMsg> for AgentContext<B>

Source§

type Reply = Vec<<B as ContextBackend>::Message>

The reply sent back to the message caller.
Source§

async fn handle( &mut self, _msg: CompressedMsg, _ctx: &mut Context<Self, Self::Reply>, ) -> Self::Reply

Handler for this message.
Source§

fn name() -> &'static str

The name of the message, which can be useful for logging or debugging. Read more
Source§

impl<B: ContextBackend> Message<EstimateTokensMsg> for AgentContext<B>

Source§

type Reply = usize

The reply sent back to the message caller.
Source§

async fn handle( &mut self, _msg: EstimateTokensMsg, _ctx: &mut Context<Self, Self::Reply>, ) -> Self::Reply

Handler for this message.
Source§

fn name() -> &'static str

The name of the message, which can be useful for logging or debugging. Read more
Source§

impl<B: ContextBackend> Message<ExtendMsg<<B as ContextBackend>::Message>> for AgentContext<B>

Source§

type Reply = ()

The reply sent back to the message caller.
Source§

async fn handle( &mut self, msg: ExtendMsg<B::Message>, _ctx: &mut Context<Self, Self::Reply>, ) -> Self::Reply

Handler for this message.
Source§

fn name() -> &'static str

The name of the message, which can be useful for logging or debugging. Read more
Source§

impl<B: ContextBackend> Message<FindByRoleMsg> for AgentContext<B>

Source§

type Reply = Vec<<B as ContextBackend>::Message>

The reply sent back to the message caller.
Source§

async fn handle( &mut self, msg: FindByRoleMsg, _ctx: &mut Context<Self, Self::Reply>, ) -> Self::Reply

Handler for this message.
Source§

fn name() -> &'static str

The name of the message, which can be useful for logging or debugging. Read more
Source§

impl<B: ContextBackend> Message<FromJsonlMsg> for AgentContext<B>

Source§

type Reply = Result<(), AgentError>

The reply sent back to the message caller.
Source§

async fn handle( &mut self, msg: FromJsonlMsg, _ctx: &mut Context<Self, Self::Reply>, ) -> Self::Reply

Handler for this message.
Source§

fn name() -> &'static str

The name of the message, which can be useful for logging or debugging. Read more
Source§

impl<B: ContextBackend> Message<Get> for AgentContext<B>

Source§

type Reply = Option<<B as ContextBackend>::Message>

The reply sent back to the message caller.
Source§

async fn handle( &mut self, msg: Get, _ctx: &mut Context<Self, Self::Reply>, ) -> Self::Reply

Handler for this message.
Source§

fn name() -> &'static str

The name of the message, which can be useful for logging or debugging. Read more
Source§

impl<B: ContextBackend> Message<ImmutableMsg> for AgentContext<B>

Source§

type Reply = Vec<<B as ContextBackend>::Message>

The reply sent back to the message caller.
Source§

async fn handle( &mut self, _msg: ImmutableMsg, _ctx: &mut Context<Self, Self::Reply>, ) -> Self::Reply

Handler for this message.
Source§

fn name() -> &'static str

The name of the message, which can be useful for logging or debugging. Read more
Source§

impl<B: ContextBackend> Message<IncrementalMsg> for AgentContext<B>

Source§

type Reply = Vec<<B as ContextBackend>::Message>

The reply sent back to the message caller.
Source§

async fn handle( &mut self, _msg: IncrementalMsg, _ctx: &mut Context<Self, Self::Reply>, ) -> Self::Reply

Handler for this message.
Source§

fn name() -> &'static str

The name of the message, which can be useful for logging or debugging. Read more
Source§

impl<B: ContextBackend> Message<InsertMsg<<B as ContextBackend>::Message>> for AgentContext<B>

Source§

type Reply = Result<(), AgentError>

The reply sent back to the message caller.
Source§

async fn handle( &mut self, msg: InsertMsg<B::Message>, _ctx: &mut Context<Self, Self::Reply>, ) -> Self::Reply

Handler for this message.
Source§

fn name() -> &'static str

The name of the message, which can be useful for logging or debugging. Read more
Source§

impl<B: ContextBackend> Message<IsEmpty> for AgentContext<B>

Source§

type Reply = bool

The reply sent back to the message caller.
Source§

async fn handle( &mut self, _msg: IsEmpty, _ctx: &mut Context<Self, Self::Reply>, ) -> Self::Reply

Handler for this message.
Source§

fn name() -> &'static str

The name of the message, which can be useful for logging or debugging. Read more
Source§

impl<B: ContextBackend> Message<Len> for AgentContext<B>

Source§

type Reply = usize

The reply sent back to the message caller.
Source§

async fn handle( &mut self, _msg: Len, _ctx: &mut Context<Self, Self::Reply>, ) -> Self::Reply

Handler for this message.
Source§

fn name() -> &'static str

The name of the message, which can be useful for logging or debugging. Read more
Source§

impl<B: ContextBackend> Message<MessagesMsg> for AgentContext<B>

Source§

type Reply = Vec<<B as ContextBackend>::Message>

The reply sent back to the message caller.
Source§

async fn handle( &mut self, _msg: MessagesMsg, _ctx: &mut Context<Self, Self::Reply>, ) -> Self::Reply

Handler for this message.
Source§

fn name() -> &'static str

The name of the message, which can be useful for logging or debugging. Read more
Source§

impl<B: ContextBackend> Message<PopMsg> for AgentContext<B>

Source§

type Reply = Option<<B as ContextBackend>::Message>

The reply sent back to the message caller.
Source§

async fn handle( &mut self, _msg: PopMsg, _ctx: &mut Context<Self, Self::Reply>, ) -> Self::Reply

Handler for this message.
Source§

fn name() -> &'static str

The name of the message, which can be useful for logging or debugging. Read more
Source§

impl<B: ContextBackend> Message<RemoveMsg> for AgentContext<B>

Source§

type Reply = Result<(), AgentError>

The reply sent back to the message caller.
Source§

async fn handle( &mut self, msg: RemoveMsg, _ctx: &mut Context<Self, Self::Reply>, ) -> Self::Reply

Handler for this message.
Source§

fn name() -> &'static str

The name of the message, which can be useful for logging or debugging. Read more
Source§

impl<B: ContextBackend> Message<RetainMsg> for AgentContext<B>

Source§

type Reply = ()

The reply sent back to the message caller.
Source§

async fn handle( &mut self, msg: RetainMsg, _ctx: &mut Context<Self, Self::Reply>, ) -> Self::Reply

Handler for this message.
Source§

fn name() -> &'static str

The name of the message, which can be useful for logging or debugging. Read more
Source§

impl<B: ContextBackend> Message<SendMsg<<B as ContextBackend>::Opts>> for AgentContext<B>

Source§

type Reply = Result<<B as ContextBackend>::Response, AgentError>

The reply sent back to the message caller.
Source§

async fn handle( &mut self, msg: SendMsg<B::Opts>, _ctx: &mut Context<Self, Self::Reply>, ) -> Self::Reply

Handler for this message.
Source§

fn name() -> &'static str

The name of the message, which can be useful for logging or debugging. Read more
Source§

impl<B: ContextBackend + Clone> Message<SendStreamMsg<<B as ContextBackend>::Opts>> for AgentContext<B>

Source§

type Reply = Result<AgentSendStream<B>, AgentError>

The reply sent back to the message caller.
Source§

async fn handle( &mut self, msg: SendStreamMsg<B::Opts>, _ctx: &mut Context<Self, Self::Reply>, ) -> Self::Reply

Handler for this message.
Source§

fn name() -> &'static str

The name of the message, which can be useful for logging or debugging. Read more
Source§

impl<B: ContextBackend> Message<ToJsonlMsg> for AgentContext<B>

Source§

type Reply = Result<String, AgentError>

The reply sent back to the message caller.
Source§

async fn handle( &mut self, _msg: ToJsonlMsg, _ctx: &mut Context<Self, Self::Reply>, ) -> Self::Reply

Handler for this message.
Source§

fn name() -> &'static str

The name of the message, which can be useful for logging or debugging. Read more
Source§

impl<B: ContextBackend> Message<UpdateMsg<<B as ContextBackend>::Message>> for AgentContext<B>

Source§

type Reply = Result<(), AgentError>

The reply sent back to the message caller.
Source§

async fn handle( &mut self, msg: UpdateMsg<B::Message>, _ctx: &mut Context<Self, Self::Reply>, ) -> Self::Reply

Handler for this message.
Source§

fn name() -> &'static str

The name of the message, which can be useful for logging or debugging. Read more

Auto Trait Implementations§

§

impl<B> Freeze for AgentContext<B>
where B: Freeze,

§

impl<B> !RefUnwindSafe for AgentContext<B>

§

impl<B> Send for AgentContext<B>

§

impl<B> Sync for AgentContext<B>

§

impl<B> Unpin for AgentContext<B>
where B: Unpin, <B as ContextBackend>::Message: Unpin,

§

impl<B> UnsafeUnpin for AgentContext<B>
where B: UnsafeUnpin,

§

impl<B> !UnwindSafe for AgentContext<B>

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
Source§

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

Source§

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

Converts Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Converts Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

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

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Converts &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> DowncastSend for T
where T: Any + Send,

Source§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_sync(self: Box<T>) -> Box<dyn Any + Sync + Send>

Converts Box<Trait> (where Trait: DowncastSync) to Box<dyn Any + Send + Sync>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Converts Arc<Trait> (where Trait: DowncastSync) to Arc<Any>, which can then be downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<A, T> DynMessage<A> for T
where A: Actor + Message<T>, T: Send + 'static,

Source§

fn handle_dyn<'a>( self: Box<T>, state: &'a mut A, actor_ref: ActorRef<A>, tx: Option<Sender<Result<Box<dyn Any + Send>, SendError<Box<dyn Any + Send>, Box<dyn Any + Send>>>>>, stop: &'a mut bool, ) -> Pin<Box<dyn Future<Output = Result<(), Box<dyn ReplyError>>> + Send + 'a>>

Handles the dyn message with the provided actor state, ref, and reply sender.
Source§

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

Casts the type to a Box<dyn Any>.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

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

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

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.

Source§

impl<A> Spawn for A
where A: Actor,

Source§

fn spawn(args: Self::Args) -> ActorRef<Self>

Spawns the actor in a Tokio task, running asynchronously with a default bounded mailbox. Read more
Source§

fn spawn_default() -> ActorRef<Self>
where Self::Args: Default,

Spawns the actor with default initialization in a Tokio task. Read more
Source§

fn spawn_with_mailbox( args: Self::Args, _: (MailboxSender<Self>, MailboxReceiver<Self>), ) -> ActorRef<Self>

Spawns the actor in a Tokio task with a specific mailbox configuration. Read more
Spawns and links the actor in a Tokio task with a default bounded mailbox. Read more
Spawns and links the actor in a Tokio task with a specific mailbox configuration. Read more
Source§

fn spawn_in_thread(args: Self::Args) -> ActorRef<Self>

Spawns the actor in its own dedicated thread with a default bounded mailbox. Read more
Source§

fn spawn_in_thread_with_mailbox( args: Self::Args, _: (MailboxSender<Self>, MailboxReceiver<Self>), ) -> ActorRef<Self>

Spawns the actor in its own dedicated thread with a specific mailbox configuration. Read more
Source§

fn prepare() -> PreparedActor<Self>

Creates a new prepared actor, allowing access to its ActorRef before spawning. Read more
Source§

fn prepare_with_mailbox( _: (MailboxSender<Self>, MailboxReceiver<Self>), ) -> PreparedActor<Self>

Creates a new prepared actor with a specific mailbox configuration, allowing access to its ActorRef before spawning. Read more
Source§

fn supervise<A>( supervisor_ref: &ActorRef<A>, args: Self::Args, ) -> SupervisedActorBuilder<'_, A, Self>
where A: Actor, Self::Args: Clone + Sync,

Creates a supervised child actor under a supervisor. Read more
Source§

fn supervise_with<A>( supervisor_ref: &ActorRef<A>, f: impl Fn() -> Self::Args + Send + Sync + 'static, ) -> SupervisedActorBuilder<'_, A, Self>
where A: Actor,

Creates a supervised child actor with a factory function for generating args. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

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
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

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