use serde::Deserialize;
pub use agent_core::{AgentEvent, LlmEvent, SessionEvent, StreamEvent};
#[derive(Debug, Clone, Default)]
pub struct TurnCompletion {
completed: std::sync::Arc<std::sync::atomic::AtomicBool>,
cancel_cause: std::sync::Arc<std::sync::atomic::AtomicU8>,
default_cause: Option<CancelCause>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum CancelCause {
User = 1,
CostCap = 2,
Restart = 3,
Host = 4,
Driver = 5,
}
impl CancelCause {
pub fn phrase(self) -> &'static str {
match self {
CancelCause::User => "Canceled by user",
CancelCause::CostCap => "Canceled (session cost cap reached)",
CancelCause::Restart => "Canceled (Synaps restarted)",
CancelCause::Host => "Canceled (session stopped by the host)",
CancelCause::Driver => "Canceled (session driver revoked)",
}
}
fn from_u8(v: u8) -> Option<Self> {
Some(match v {
1 => CancelCause::User,
2 => CancelCause::CostCap,
3 => CancelCause::Restart,
4 => CancelCause::Host,
5 => CancelCause::Driver,
_ => return None,
})
}
}
impl TurnCompletion {
pub fn new() -> Self {
Self::default()
}
pub fn with_default_cause(cause: CancelCause) -> Self {
Self {
default_cause: Some(cause),
..Self::default()
}
}
pub fn completed(&self) -> bool {
self.completed.load(std::sync::atomic::Ordering::Acquire)
}
pub(crate) fn mark_completed(&self) {
self.completed
.store(true, std::sync::atomic::Ordering::Release);
}
pub fn note_cancel_cause(&self, cause: CancelCause) {
let _ = self.cancel_cause.compare_exchange(
0,
cause as u8,
std::sync::atomic::Ordering::AcqRel,
std::sync::atomic::Ordering::Acquire,
);
}
pub(crate) fn cancel_phrase(&self) -> &'static str {
CancelCause::from_u8(self.cancel_cause.load(std::sync::atomic::Ordering::Acquire))
.or(self.default_cause)
.unwrap_or(CancelCause::User)
.phrase()
}
}
#[derive(Debug, Clone)]
pub(super) struct AuthState {
pub(super) auth_token: String,
pub(super) auth_type: String,
pub(super) refresh_token: Option<String>,
pub(super) token_expires: Option<u64>,
}
#[allow(dead_code)]
#[derive(Debug, Deserialize)]
pub(super) struct AnthropicAuth {
#[serde(rename = "type")]
pub(super) auth_type: String,
pub(super) refresh: Option<String>,
pub(super) access: Option<String>,
pub(super) expires: Option<u64>,
pub(super) key: Option<String>,
}