everruns_core/
agent_trigger.rs1use chrono::{DateTime, Utc};
11use serde::{Deserialize, Serialize};
12
13use crate::app::InvocationSessionMode;
16use crate::typed_id::{AgentId, TriggerId};
17
18#[cfg(feature = "openapi")]
19use utoipa::ToSchema;
20
21#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default)]
24#[cfg_attr(feature = "openapi", derive(ToSchema))]
25#[serde(rename_all = "lowercase")]
26pub enum AgentTriggerType {
27 #[default]
29 Schedule,
30}
31
32impl std::fmt::Display for AgentTriggerType {
33 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34 match self {
35 AgentTriggerType::Schedule => write!(f, "schedule"),
36 }
37 }
38}
39
40impl From<&str> for AgentTriggerType {
41 fn from(_value: &str) -> Self {
42 Self::Schedule
44 }
45}
46
47#[derive(Debug, Clone, Serialize, Deserialize)]
52#[cfg_attr(feature = "openapi", derive(ToSchema))]
53pub struct ScheduleTriggerConfig {
54 pub cron_expression: String,
56 #[serde(default = "default_timezone")]
58 pub timezone: String,
59 #[serde(default)]
61 pub session_mode: InvocationSessionMode,
62 pub message: String,
64}
65
66fn default_timezone() -> String {
67 "UTC".to_string()
68}
69
70#[derive(Debug, Clone, Serialize, Deserialize)]
72#[cfg_attr(feature = "openapi", derive(ToSchema))]
73pub struct AgentTrigger {
74 #[serde(rename = "id")]
76 #[cfg_attr(feature = "openapi", schema(value_type = String, example = "trg_01933b5a000070008000000000000001"))]
77 pub id: TriggerId,
78 #[cfg_attr(feature = "openapi", schema(value_type = String, example = "agent_01933b5a000070008000000000000001"))]
80 pub agent_id: AgentId,
81 pub trigger_type: AgentTriggerType,
83 pub config: serde_json::Value,
85 pub enabled: bool,
87 pub created_at: DateTime<Utc>,
89 pub updated_at: DateTime<Utc>,
91 #[serde(skip_serializing_if = "Option::is_none")]
93 pub archived_at: Option<DateTime<Utc>>,
94 #[serde(skip_serializing_if = "Option::is_none")]
96 pub deleted_at: Option<DateTime<Utc>>,
97}
98
99impl AgentTrigger {
100 pub fn schedule_config(&self) -> anyhow::Result<ScheduleTriggerConfig> {
104 if self.trigger_type != AgentTriggerType::Schedule {
105 anyhow::bail!("agent trigger {} is not a schedule trigger", self.id);
106 }
107 Ok(serde_json::from_value(self.config.clone())?)
108 }
109}