use serde::{Deserialize, Serialize};
#[cfg(feature = "openapi")]
use utoipa::ToSchema;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
pub enum ExecutionPhase {
Commentary,
FinalAnswer,
}
impl ExecutionPhase {
pub fn from_has_tool_calls(has_tool_calls: bool) -> Self {
if has_tool_calls {
Self::Commentary
} else {
Self::FinalAnswer
}
}
pub fn from_provider_str(s: &str) -> Option<Self> {
match s {
"commentary" | "in_progress" => Some(Self::Commentary),
"final_answer" | "completed" => Some(Self::FinalAnswer),
_ => None,
}
}
pub fn as_provider_str(&self) -> &'static str {
match self {
Self::Commentary => "commentary",
Self::FinalAnswer => "final_answer",
}
}
pub fn refine_streamed_hint(current: Option<Self>, incoming: Self) -> Option<Self> {
Some(current.unwrap_or(incoming))
}
}
impl std::fmt::Display for ExecutionPhase {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_provider_str())
}
}
impl Serialize for ExecutionPhase {
fn serialize<S: serde::Serializer>(
&self,
serializer: S,
) -> std::result::Result<S::Ok, S::Error> {
serializer.serialize_str(self.as_provider_str())
}
}
impl<'de> Deserialize<'de> for ExecutionPhase {
fn deserialize<D: serde::Deserializer<'de>>(
deserializer: D,
) -> std::result::Result<Self, D::Error> {
let s = String::deserialize(deserializer)?;
match s.as_str() {
"commentary" | "in_progress" => Ok(Self::Commentary),
"final_answer" | "completed" => Ok(Self::FinalAnswer),
other => Err(serde::de::Error::unknown_variant(
other,
&["commentary", "final_answer", "in_progress", "completed"],
)),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_execution_phase_from_provider_str() {
assert_eq!(
ExecutionPhase::from_provider_str("commentary"),
Some(ExecutionPhase::Commentary)
);
assert_eq!(
ExecutionPhase::from_provider_str("final_answer"),
Some(ExecutionPhase::FinalAnswer)
);
assert_eq!(
ExecutionPhase::from_provider_str("in_progress"),
Some(ExecutionPhase::Commentary)
);
assert_eq!(
ExecutionPhase::from_provider_str("completed"),
Some(ExecutionPhase::FinalAnswer)
);
assert_eq!(ExecutionPhase::from_provider_str("unknown"), None);
}
}