use serde::{Deserialize, Serialize};
use std::fmt;
use uuid::Uuid;
pub const MAX_QUEUED_FOLLOW_UP_INTENTS: usize = 64;
pub const MAX_OVERFLOW_FOLLOW_UP_INTENTS: usize = 64;
pub const MAX_PENDING_FOLLOW_UP_INTENTS: usize = MAX_QUEUED_FOLLOW_UP_INTENTS + MAX_OVERFLOW_FOLLOW_UP_INTENTS + 1;
pub const MAX_APPLIED_FOLLOW_UP_INTENT_IDS: usize = 64;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum FollowUpProvenance {
#[default]
UserInput,
Synthetic,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct QueuedFollowUpIntent {
id: String,
text: String,
#[serde(default)]
provenance: FollowUpProvenance,
}
impl QueuedFollowUpIntent {
#[must_use]
pub fn new(text: String) -> Self {
Self::new_with_provenance(text, FollowUpProvenance::UserInput)
}
#[must_use]
pub fn new_with_provenance(text: String, provenance: FollowUpProvenance) -> Self {
Self { id: Uuid::new_v4().to_string(), text, provenance }
}
pub fn from_parts(id: impl Into<String>, text: impl Into<String>) -> Self {
Self::from_parts_with_provenance(id, text, FollowUpProvenance::UserInput)
}
pub fn from_parts_with_provenance(
id: impl Into<String>,
text: impl Into<String>,
provenance: FollowUpProvenance,
) -> Self {
Self { id: id.into(), text: text.into(), provenance }
}
#[must_use]
pub fn id(&self) -> &str {
&self.id
}
#[must_use]
pub fn text(&self) -> &str {
&self.text
}
#[must_use]
pub const fn provenance(&self) -> FollowUpProvenance {
self.provenance
}
#[must_use]
pub fn into_parts(self) -> (String, String) {
(self.id, self.text)
}
#[must_use]
pub fn into_parts_with_provenance(self) -> (String, String, FollowUpProvenance) {
(self.id, self.text, self.provenance)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FollowUpQueueFull {
pub capacity: usize,
}
impl fmt::Display for FollowUpQueueFull {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "follow-up intent queue is full (capacity {})", self.capacity)
}
}
impl std::error::Error for FollowUpQueueFull {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SteerRejection {
TurnClosing,
CancellationPrecedence,
QueueFull,
NoLiveTurn,
Superseded,
}
impl fmt::Display for SteerRejection {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let label = match self {
Self::TurnClosing => "turn is closing",
Self::CancellationPrecedence => "cancellation takes precedence",
Self::QueueFull => "follow-up queue is full",
Self::NoLiveTurn => "no live turn",
Self::Superseded => "session or turn was superseded",
};
f.write_str(label)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SteerOutcome {
Accepted { intent_id: String },
Rejected { reason: SteerRejection },
}
impl SteerOutcome {
#[must_use]
pub const fn accepted(&self) -> bool {
matches!(self, Self::Accepted { .. })
}
}
pub type SteerReceiptSender = tokio::sync::oneshot::Sender<SteerOutcome>;
pub type SteerReceipt = tokio::sync::oneshot::Receiver<SteerOutcome>;
#[derive(Debug)]
pub struct FollowUpSteer {
pub text: String,
pub receipt: Option<SteerReceiptSender>,
}
impl FollowUpSteer {
#[must_use]
pub fn new(text: String) -> Self {
Self { text, receipt: None }
}
#[must_use]
pub fn with_receipt(text: String, receipt: SteerReceiptSender) -> Self {
Self { text, receipt: Some(receipt) }
}
#[must_use]
pub fn channel(text: String) -> (Self, SteerReceipt) {
let (sender, receiver) = tokio::sync::oneshot::channel();
(Self::with_receipt(text, sender), receiver)
}
pub fn resolve(self, outcome: SteerOutcome) {
if let Some(receipt) = self.receipt {
let _ = receipt.send(outcome);
}
}
pub fn reject(self, reason: SteerRejection) {
self.resolve(SteerOutcome::Rejected { reason });
}
}
#[derive(Debug)]
pub enum SteeringMessage {
SteerStop,
Pause,
Resume,
FollowUpInput(FollowUpSteer),
}