agent_engine/runtime/
types.rs1use serde::Deserialize;
2
3pub use agent_core::{AgentEvent, LlmEvent, SessionEvent, StreamEvent};
6
7#[derive(Debug, Clone, Default)]
24pub struct TurnCompletion {
25 completed: std::sync::Arc<std::sync::atomic::AtomicBool>,
26 cancel_cause: std::sync::Arc<std::sync::atomic::AtomicU8>,
28 default_cause: Option<CancelCause>,
30}
31
32#[derive(Debug, Clone, Copy, PartialEq, Eq)]
34#[repr(u8)]
35pub enum CancelCause {
36 User = 1,
37 CostCap = 2,
38 Restart = 3,
39 Host = 4,
40 Driver = 5,
41}
42
43impl CancelCause {
44 pub fn phrase(self) -> &'static str {
48 match self {
49 CancelCause::User => "Canceled by user",
50 CancelCause::CostCap => "Canceled (session cost cap reached)",
51 CancelCause::Restart => "Canceled (Synaps restarted)",
52 CancelCause::Host => "Canceled (session stopped by the host)",
53 CancelCause::Driver => "Canceled (session driver revoked)",
54 }
55 }
56
57 fn from_u8(v: u8) -> Option<Self> {
58 Some(match v {
59 1 => CancelCause::User,
60 2 => CancelCause::CostCap,
61 3 => CancelCause::Restart,
62 4 => CancelCause::Host,
63 5 => CancelCause::Driver,
64 _ => return None,
65 })
66 }
67}
68
69impl TurnCompletion {
70 pub fn new() -> Self {
71 Self::default()
72 }
73
74 pub fn with_default_cause(cause: CancelCause) -> Self {
76 Self {
77 default_cause: Some(cause),
78 ..Self::default()
79 }
80 }
81
82 pub fn completed(&self) -> bool {
83 self.completed.load(std::sync::atomic::Ordering::Acquire)
84 }
85
86 pub(crate) fn mark_completed(&self) {
87 self.completed
88 .store(true, std::sync::atomic::Ordering::Release);
89 }
90
91 pub fn note_cancel_cause(&self, cause: CancelCause) {
94 let _ = self.cancel_cause.compare_exchange(
95 0,
96 cause as u8,
97 std::sync::atomic::Ordering::AcqRel,
98 std::sync::atomic::Ordering::Acquire,
99 );
100 }
101
102 pub(crate) fn cancel_phrase(&self) -> &'static str {
104 CancelCause::from_u8(self.cancel_cause.load(std::sync::atomic::Ordering::Acquire))
105 .or(self.default_cause)
106 .unwrap_or(CancelCause::User)
107 .phrase()
108 }
109}
110
111#[derive(Debug, Clone)]
114pub(super) struct AuthState {
115 pub(super) auth_token: String,
116 pub(super) auth_type: String,
117 pub(super) refresh_token: Option<String>,
118 pub(super) token_expires: Option<u64>,
119}
120
121#[allow(dead_code)]
122#[derive(Debug, Deserialize)]
123pub(super) struct AnthropicAuth {
124 #[serde(rename = "type")]
125 pub(super) auth_type: String,
126 pub(super) refresh: Option<String>,
127 pub(super) access: Option<String>,
128 pub(super) expires: Option<u64>,
129 pub(super) key: Option<String>,
130}