pub struct AgentContext<B: ContextBackend> { /* private fields */ }Expand description
LLM 对话上下文管理器,kameo Actor。
管理三区 + Scratch 消息模型(immutable → compressed → incremental → scratch),提供:
- 消息增删改查(
AppendMsg、UpdateMsg、RemoveMsg等) - 对话发送(
SendMsg、SendStreamMsg),支持通过ScratchOpts追加临时元数据 - 上下文压缩(
CompressMsg) - Token 估算和溢出检测(
EstimateTokensMsg、IsFullMsg) - 变更回调(
ChangeEvent)
§构造
ⓘ
let ctx = AgentContext::new(backend, vec![])
.with_on_change(|event| { /* 处理变更 */ });
let actor = AgentContext::spawn(ctx);Implementations§
Source§impl<B: ContextBackend> AgentContext<B>
impl<B: ContextBackend> AgentContext<B>
Sourcepub fn new(backend: B, immutable: Vec<B::Message>) -> Self
pub fn new(backend: B, immutable: Vec<B::Message>) -> Self
创建新的上下文管理器。
backend: 实现了ContextBackend的 LLM 后端实例immutable: 初始不可变消息(系统提示词等),放入 immutable 区
Sourcepub fn with_on_change(
self,
f: impl Fn(ChangeEvent<B::Message>) + Send + Sync + 'static,
) -> Self
pub fn with_on_change( self, f: impl Fn(ChangeEvent<B::Message>) + Send + Sync + 'static, ) -> Self
注册增量区变更回调。
每次对 incremental 区的写操作(追加/更新/插入/移除/清空等)都会触发此回调。 用于 CLI 实时展示、日志记录等场景。
Trait Implementations§
Source§impl<B: ContextBackend> Actor for AgentContext<B>
impl<B: ContextBackend> Actor for AgentContext<B>
Source§type Args = AgentContext<B>
type Args = AgentContext<B>
Arguments to initialize the actor. Read more
Source§type Error = Infallible
type Error = Infallible
Actor error type. Read more
Source§fn name() -> &'static str
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>
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
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
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
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
Source§fn on_link_died(
&mut self,
actor_ref: WeakActorRef<Self>,
id: ActorId,
reason: ActorStopReason,
) -> impl Future<Output = Result<ControlFlow<ActorStopReason>, Self::Error>> + Send
fn on_link_died( &mut self, actor_ref: WeakActorRef<Self>, id: ActorId, reason: ActorStopReason, ) -> impl Future<Output = Result<ControlFlow<ActorStopReason>, Self::Error>> + Send
Called when a linked actor dies. Read more
Source§impl<B: ContextBackend> Message<AppendMsg<<B as ContextBackend>::Message>> for AgentContext<B>
impl<B: ContextBackend> Message<AppendMsg<<B as ContextBackend>::Message>> for AgentContext<B>
Source§impl<B: ContextBackend> Message<ClearMsg> for AgentContext<B>
impl<B: ContextBackend> Message<ClearMsg> for AgentContext<B>
Source§impl<B: ContextBackend> Message<CompressMsg<<B as ContextBackend>::Opts>> for AgentContext<B>
impl<B: ContextBackend> Message<CompressMsg<<B as ContextBackend>::Opts>> for AgentContext<B>
Source§impl<B: ContextBackend> Message<CompressedMsg> for AgentContext<B>
impl<B: ContextBackend> Message<CompressedMsg> for AgentContext<B>
Source§impl<B: ContextBackend> Message<EstimateTokensMsg> for AgentContext<B>
impl<B: ContextBackend> Message<EstimateTokensMsg> for AgentContext<B>
Source§impl<B: ContextBackend> Message<ExtendMsg<<B as ContextBackend>::Message>> for AgentContext<B>
impl<B: ContextBackend> Message<ExtendMsg<<B as ContextBackend>::Message>> for AgentContext<B>
Source§impl<B: ContextBackend> Message<FindByRoleMsg> for AgentContext<B>
impl<B: ContextBackend> Message<FindByRoleMsg> for AgentContext<B>
Source§impl<B: ContextBackend> Message<FromJsonlMsg> for AgentContext<B>
impl<B: ContextBackend> Message<FromJsonlMsg> for AgentContext<B>
Source§impl<B: ContextBackend> Message<Get> for AgentContext<B>
impl<B: ContextBackend> Message<Get> for AgentContext<B>
Source§impl<B: ContextBackend> Message<ImmutableMsg> for AgentContext<B>
impl<B: ContextBackend> Message<ImmutableMsg> for AgentContext<B>
Source§impl<B: ContextBackend> Message<IncrementalMsg> for AgentContext<B>
impl<B: ContextBackend> Message<IncrementalMsg> for AgentContext<B>
Source§impl<B: ContextBackend> Message<InsertMsg<<B as ContextBackend>::Message>> for AgentContext<B>
impl<B: ContextBackend> Message<InsertMsg<<B as ContextBackend>::Message>> for AgentContext<B>
Source§impl<B: ContextBackend> Message<IsEmpty> for AgentContext<B>
impl<B: ContextBackend> Message<IsEmpty> for AgentContext<B>
Source§impl<B: ContextBackend> Message<IsFullMsg> for AgentContext<B>
impl<B: ContextBackend> Message<IsFullMsg> for AgentContext<B>
Source§impl<B: ContextBackend> Message<Len> for AgentContext<B>
impl<B: ContextBackend> Message<Len> for AgentContext<B>
Source§impl<B: ContextBackend> Message<MessagesMsg> for AgentContext<B>
impl<B: ContextBackend> Message<MessagesMsg> for AgentContext<B>
Source§impl<B: ContextBackend> Message<PopMsg> for AgentContext<B>
impl<B: ContextBackend> Message<PopMsg> for AgentContext<B>
Source§impl<B: ContextBackend> Message<RemoveMsg> for AgentContext<B>
impl<B: ContextBackend> Message<RemoveMsg> for AgentContext<B>
Source§impl<B: ContextBackend> Message<RetainMsg> for AgentContext<B>
impl<B: ContextBackend> Message<RetainMsg> for AgentContext<B>
Source§impl<B: ContextBackend> Message<SendMsg<<B as ContextBackend>::Opts>> for AgentContext<B>
impl<B: ContextBackend> Message<SendMsg<<B as ContextBackend>::Opts>> for AgentContext<B>
Source§impl<B: ContextBackend + Clone> Message<SendStreamMsg<<B as ContextBackend>::Opts>> for AgentContext<B>
impl<B: ContextBackend + Clone> Message<SendStreamMsg<<B as ContextBackend>::Opts>> for AgentContext<B>
Source§impl<B: ContextBackend> Message<SilentAppendMsg<<B as ContextBackend>::Message>> for AgentContext<B>
impl<B: ContextBackend> Message<SilentAppendMsg<<B as ContextBackend>::Message>> for AgentContext<B>
Source§impl<B: ContextBackend> Message<ToJsonlMsg> for AgentContext<B>
impl<B: ContextBackend> Message<ToJsonlMsg> for AgentContext<B>
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>
impl<B> UnsafeUnpin for AgentContext<B>where
B: UnsafeUnpin,
impl<B> !UnwindSafe for AgentContext<B>
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
Mutably borrows from an owned value. Read more
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
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>
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)
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)
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
impl<T> DowncastSend for T
Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<A, T> DynMessage<A> for T
impl<A, T> DynMessage<A> for T
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>>
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§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<A> Spawn for Awhere
A: Actor,
impl<A> Spawn for Awhere
A: Actor,
Source§fn spawn(args: Self::Args) -> ActorRef<Self>
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>
fn spawn_default() -> ActorRef<Self>
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>
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
Source§fn spawn_link<L>(
link_ref: &ActorRef<L>,
args: Self::Args,
) -> impl Future<Output = ActorRef<Self>> + Sendwhere
L: Actor,
fn spawn_link<L>(
link_ref: &ActorRef<L>,
args: Self::Args,
) -> impl Future<Output = ActorRef<Self>> + Sendwhere
L: Actor,
Spawns and links the actor in a Tokio task with a default bounded mailbox. Read more
Source§fn spawn_link_with_mailbox<L>(
link_ref: &ActorRef<L>,
args: Self::Args,
_: (MailboxSender<Self>, MailboxReceiver<Self>),
) -> impl Future<Output = ActorRef<Self>> + Sendwhere
L: Actor,
fn spawn_link_with_mailbox<L>(
link_ref: &ActorRef<L>,
args: Self::Args,
_: (MailboxSender<Self>, MailboxReceiver<Self>),
) -> impl Future<Output = ActorRef<Self>> + Sendwhere
L: Actor,
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>
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>
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