pub struct MaxTurnsNudgeMiddleware { /* private fields */ }Expand description
Middleware that nudges the model when it nears the turn limit. Middleware that injects a nudge message when approaching the max turns limit.
This is a soft intervention: the LLM can choose to continue working, but the nudge encourages it to wrap up gracefully before hitting the hard limit (which causes an error).
§How it works
- On each LLM call, checks
remaining_turns = max_turns - turn_count - If
remaining_turns <= threshold, injects the nudge as a user message - The LLM sees the nudge before generating its response (this turn)
§Usage
ⓘ
use agent_base::engine::{AgentBuilder, max_turns_nudge::{MaxTurnsNudgeMiddleware, MaxTurnsNudgeConfig}};
let runtime = AgentBuilder::new(client)
.middleware(MaxTurnsNudgeMiddleware::new(MaxTurnsNudgeConfig {
threshold: 3,
message: "Please wrap up soon.".to_string(),
}))
.build()?;Implementations§
Source§impl MaxTurnsNudgeMiddleware
impl MaxTurnsNudgeMiddleware
pub fn new(config: MaxTurnsNudgeConfig) -> MaxTurnsNudgeMiddleware
Trait Implementations§
Source§impl Middleware for MaxTurnsNudgeMiddleware
impl Middleware for MaxTurnsNudgeMiddleware
fn on_pre_llm<'life0, 'life1, 'async_trait>(
&'life0 self,
ctx: &'life1 mut PreLlmCtx,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
MaxTurnsNudgeMiddleware: 'async_trait,
fn on_user_message<'life0, 'life1, 'async_trait>(
&'life0 self,
_ctx: &'life1 mut UserMessageCtx,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
fn on_post_llm<'life0, 'life1, 'async_trait>(
&'life0 self,
_ctx: &'life1 mut PostLlmCtx,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
Auto Trait Implementations§
impl Freeze for MaxTurnsNudgeMiddleware
impl RefUnwindSafe for MaxTurnsNudgeMiddleware
impl Send for MaxTurnsNudgeMiddleware
impl Sync for MaxTurnsNudgeMiddleware
impl Unpin for MaxTurnsNudgeMiddleware
impl UnsafeUnpin for MaxTurnsNudgeMiddleware
impl UnwindSafe for MaxTurnsNudgeMiddleware
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