use super::*;
pub struct RuntimeSteering {
steering_receiver: Option<UnboundedReceiver<SteeringMessage>>,
pub(super) queued_follow_up_inputs: VecDeque<QueuedFollowUpIntent>,
overflow_follow_up_inputs: VecDeque<QueuedFollowUpIntent>,
pub(super) in_flight_follow_up_intents: VecDeque<QueuedFollowUpIntent>,
applied_follow_up_intent_ids: VecDeque<String>,
}
impl Default for RuntimeSteering {
fn default() -> Self {
Self::new(None)
}
}
impl RuntimeSteering {
pub(super) fn new(steering_receiver: Option<UnboundedReceiver<SteeringMessage>>) -> Self {
Self {
steering_receiver,
queued_follow_up_inputs: VecDeque::new(),
overflow_follow_up_inputs: VecDeque::new(),
in_flight_follow_up_intents: VecDeque::new(),
applied_follow_up_intent_ids: VecDeque::new(),
}
}
pub fn set_receiver(&mut self, receiver: Option<UnboundedReceiver<SteeringMessage>>) {
self.steering_receiver = receiver;
}
pub fn take_receiver(&mut self) -> Option<UnboundedReceiver<SteeringMessage>> {
self.steering_receiver.take()
}
#[must_use]
pub fn has_pending_follow_up_inputs(&self) -> bool {
!self.queued_follow_up_inputs.is_empty() || !self.overflow_follow_up_inputs.is_empty()
}
pub fn pop_follow_up_input(&mut self) -> Option<String> {
self.pop_follow_up_intent().map(|intent| intent.into_parts().1)
}
pub fn pop_follow_up_intent(&mut self) -> Option<QueuedFollowUpIntent> {
self.drain_overflow_into_primary();
self.queued_follow_up_inputs.pop_front()
}
fn drain_overflow_into_primary(&mut self) {
while self.queued_follow_up_inputs.len() < MAX_QUEUED_FOLLOW_UP_INTENTS
&& let Some(intent) = self.overflow_follow_up_inputs.pop_front()
{
self.queued_follow_up_inputs.push_back(intent);
}
}
pub fn queue_follow_up_input(&mut self, input: String) -> bool {
match self.try_queue_follow_up_input(input) {
Ok(()) => true,
Err(error) => {
tracing::warn!(%error, "Rejected follow-up steering intent");
false
}
}
}
pub fn try_queue_follow_up_input(&mut self, input: String) -> Result<(), FollowUpQueueFull> {
self.try_queue_follow_up_intent(QueuedFollowUpIntent::new(input))
}
pub fn try_queue_synthetic_follow_up_input(&mut self, input: String) -> Result<(), FollowUpQueueFull> {
self.try_queue_follow_up_intent(QueuedFollowUpIntent::new_with_provenance(input, FollowUpProvenance::Synthetic))
}
pub fn try_queue_follow_up_intent(&mut self, intent: QueuedFollowUpIntent) -> Result<(), FollowUpQueueFull> {
if self.queued_follow_up_inputs.len() + self.in_flight_follow_up_intents.len() < MAX_QUEUED_FOLLOW_UP_INTENTS {
self.queued_follow_up_inputs.push_back(intent);
return Ok(());
}
if self.overflow_follow_up_inputs.len() < MAX_OVERFLOW_FOLLOW_UP_INTENTS {
self.overflow_follow_up_inputs.push_back(intent);
return Ok(());
}
Err(FollowUpQueueFull {
capacity: MAX_QUEUED_FOLLOW_UP_INTENTS + MAX_OVERFLOW_FOLLOW_UP_INTENTS,
})
}
pub fn acknowledge_follow_up_intent(&mut self, intent_id: impl Into<String>) {
if self.applied_follow_up_intent_ids.len() >= MAX_APPLIED_FOLLOW_UP_INTENT_IDS {
self.applied_follow_up_intent_ids.pop_front();
}
self.applied_follow_up_intent_ids.push_back(intent_id.into());
}
#[must_use]
pub fn applied_follow_up_intent_ids(&self) -> &VecDeque<String> {
&self.applied_follow_up_intent_ids
}
#[must_use]
pub fn pending_follow_up_intents(&self) -> &VecDeque<QueuedFollowUpIntent> {
&self.queued_follow_up_inputs
}
#[must_use]
pub fn pending_follow_up_intents_snapshot(&self) -> Vec<QueuedFollowUpIntent> {
self.queued_follow_up_inputs
.iter()
.chain(self.overflow_follow_up_inputs.iter())
.chain(self.in_flight_follow_up_intents.iter())
.cloned()
.collect()
}
pub fn drain_follow_up_intents_to_in_flight(&mut self) -> Vec<QueuedFollowUpIntent> {
self.drain_overflow_into_primary();
let drained: Vec<_> = self.queued_follow_up_inputs.drain(..).collect();
self.in_flight_follow_up_intents.extend(drained.iter().cloned());
drained
}
pub fn acknowledge_durable_follow_up_intents(&mut self) {
while let Some(intent) = self.in_flight_follow_up_intents.pop_front() {
self.acknowledge_follow_up_intent(intent.id());
}
}
pub fn release_in_flight_follow_up_intents_without_persistence(&mut self) {
self.in_flight_follow_up_intents.clear();
}
pub fn clear_pending_follow_up_inputs(&mut self) {
self.queued_follow_up_inputs.clear();
self.overflow_follow_up_inputs.clear();
self.in_flight_follow_up_intents.clear();
}
pub async fn poll_turn_control(&mut self) -> RuntimeControl {
self.poll_control().await
}
pub async fn poll_tool_control(&mut self) -> RuntimeControl {
self.poll_control().await
}
async fn poll_control(&mut self) -> RuntimeControl {
let mut paused = false;
loop {
let Some(receiver) = self.steering_receiver.as_mut() else {
return if paused {
RuntimeControl::Resumed
} else {
RuntimeControl::Continue
};
};
match receiver.try_recv() {
Ok(SteeringMessage::SteerStop) => return RuntimeControl::StopRequested,
Ok(SteeringMessage::Pause) => {
paused = true;
if matches!(self.wait_for_resume().await, RuntimeControl::StopRequested) {
return RuntimeControl::StopRequested;
}
}
Ok(SteeringMessage::Resume) => {
paused = true;
}
Ok(SteeringMessage::FollowUpInput(input)) => {
if let Err(error) = self.try_queue_follow_up_input(input) {
tracing::warn!(%error, "Rejected follow-up steering intent");
}
}
Err(TryRecvError::Empty | TryRecvError::Disconnected) => {
return if paused {
RuntimeControl::Resumed
} else {
RuntimeControl::Continue
};
}
}
}
}
async fn wait_for_resume(&mut self) -> RuntimeControl {
loop {
let Some(receiver) = self.steering_receiver.as_mut() else {
return RuntimeControl::Continue;
};
match receiver.recv().await {
Some(SteeringMessage::Resume) => return RuntimeControl::Continue,
Some(SteeringMessage::SteerStop) => return RuntimeControl::StopRequested,
Some(SteeringMessage::FollowUpInput(input)) => {
if let Err(error) = self.try_queue_follow_up_input(input) {
tracing::warn!(%error, "Rejected follow-up steering intent");
}
}
Some(SteeringMessage::Pause) => {}
None => return RuntimeControl::Continue,
}
}
}
}