use std::fmt;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
pub use turnframe_core::ids::{ModelKey, ProviderKey};
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(transparent)]
pub struct CallId(pub String);
impl CallId {
#[must_use]
pub fn new(value: impl Into<String>) -> Self {
Self(value.into())
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
#[must_use]
pub fn into_string(self) -> String {
self.0
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
impl From<&str> for CallId {
fn from(value: &str) -> Self {
Self(value.to_owned())
}
}
impl From<String> for CallId {
fn from(value: String) -> Self {
Self(value)
}
}
impl From<CallId> for String {
fn from(value: CallId) -> Self {
value.0
}
}
impl AsRef<str> for CallId {
fn as_ref(&self) -> &str {
&self.0
}
}
impl std::borrow::Borrow<str> for CallId {
fn borrow(&self) -> &str {
&self.0
}
}
impl fmt::Display for CallId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct ModelRef {
pub provider: ProviderKey,
pub model: ModelKey,
}
impl ModelRef {
#[must_use]
pub fn new(provider: impl Into<ProviderKey>, model: impl Into<ModelKey>) -> Self {
Self {
provider: provider.into(),
model: model.into(),
}
}
}
impl fmt::Display for ModelRef {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}/{}", self.provider, self.model)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(transparent)]
pub struct RequestId(pub Uuid);
impl RequestId {
#[must_use]
pub fn new() -> Self {
Self(Uuid::now_v7())
}
#[must_use]
pub const fn nil() -> Self {
Self(Uuid::nil())
}
#[must_use]
pub const fn as_uuid(&self) -> &Uuid {
&self.0
}
}
impl Default for RequestId {
fn default() -> Self {
Self::new()
}
}
impl From<Uuid> for RequestId {
fn from(value: Uuid) -> Self {
Self(value)
}
}
impl fmt::Display for RequestId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.0.hyphenated(), f)
}
}
impl std::str::FromStr for RequestId {
type Err = uuid::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Uuid::parse_str(s).map(Self)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(transparent)]
pub struct AttemptNumber(pub u32);
impl AttemptNumber {
pub const FIRST: Self = Self(1);
#[must_use]
pub const fn next(self) -> Self {
Self(self.0.saturating_add(1))
}
#[must_use]
pub const fn get(self) -> u32 {
self.0
}
}
impl Default for AttemptNumber {
fn default() -> Self {
Self::FIRST
}
}
impl fmt::Display for AttemptNumber {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn request_ids_are_time_ordered_and_round_trip() {
let a = RequestId::new();
let b = RequestId::new();
assert!(a <= b);
let json = serde_json::to_string(&a).unwrap();
let back: RequestId = serde_json::from_str(&json).unwrap();
assert_eq!(a, back);
assert_eq!(a.to_string().parse::<RequestId>().unwrap(), a);
assert_eq!(RequestId::nil().as_uuid(), &Uuid::nil());
}
#[test]
fn keys_are_cores_own_types() {
let core_key = turnframe_core::ids::ProviderKey::from("openai");
let model_ref = ModelRef::new(core_key.clone(), ModelKey::from("gpt-4o"));
assert_eq!(model_ref.provider, core_key);
assert_eq!(model_ref.to_string(), "openai/gpt-4o");
assert_eq!(serde_json::to_string(&core_key).unwrap(), "\"openai\"");
}
#[test]
fn call_ids_are_transparent_strings() {
let id = CallId::from("call_123");
assert_eq!(serde_json::to_string(&id).unwrap(), "\"call_123\"");
assert_eq!(id.as_str(), "call_123");
assert_eq!(id.to_string(), "call_123");
assert!(!id.is_empty());
assert!(CallId::new(String::new()).is_empty());
}
#[test]
fn attempts_start_at_one() {
assert_eq!(AttemptNumber::default(), AttemptNumber::FIRST);
assert_eq!(AttemptNumber::FIRST.next().get(), 2);
assert_eq!(AttemptNumber(u32::MAX).next().get(), u32::MAX);
assert_eq!(AttemptNumber::FIRST.to_string(), "1");
}
}