Skip to main content

eoka_protocol/
operations.rs

1pub use eoka::{
2    Action, ActionOptions, ActionOutcome, ActionReceipt, Destination, DispatchState, Expectation,
3    InputMode, LocatorQuery, LocatorSpec,
4};
5use serde::{Deserialize, Serialize};
6
7#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
8pub struct ActionPerformArgs {
9    pub operation_id: String,
10    pub action: Action,
11    #[serde(default)]
12    pub options: ActionOptions,
13}
14
15#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
16pub struct OperationIdArgs {
17    #[serde(alias = "operation_id")]
18    #[schemars(description = "Operation ID: the id field of the action's result.")]
19    pub id: String,
20}
21
22#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
23pub struct OperationWaitArgs {
24    #[serde(alias = "operation_id")]
25    #[schemars(description = "Operation ID: the id field of the action's result.")]
26    pub id: String,
27    #[serde(default = "wait_timeout")]
28    #[schemars(range(max = 86400000))]
29    pub timeout_ms: u64,
30}
31fn wait_timeout() -> u64 {
32    30_000
33}
34
35#[derive(Debug, Clone, Default, Serialize, Deserialize, schemars::JsonSchema)]
36pub struct OperationListArgs {
37    #[serde(default)]
38    pub before_ms: Option<u64>,
39    #[serde(default)]
40    pub limit: Option<u32>,
41}
42
43#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
44#[serde(rename_all = "snake_case")]
45pub enum OperationState {
46    Running,
47    Finished,
48    Interrupted,
49    Expired,
50}
51
52#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
53pub struct OperationRecord {
54    pub id: String,
55    pub runtime_id: String,
56    pub target_id: String,
57    pub action: String,
58    pub state: OperationState,
59    pub outcome: ActionOutcome,
60    pub dispatch_intent: bool,
61    pub preparation_intent: bool,
62    pub started_at_ms: u64,
63    pub finished_at_ms: Option<u64>,
64    pub receipt: Option<ActionReceipt>,
65    pub error_code: Option<String>,
66    #[serde(default)]
67    pub error: Option<crate::ErrorDetail>,
68}
69
70#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
71pub struct ActionResult {
72    #[serde(flatten)]
73    pub record: OperationRecord,
74    pub status: OperationStatus,
75}
76
77impl From<OperationRecord> for ActionResult {
78    fn from(record: OperationRecord) -> Self {
79        Self {
80            status: record.status(),
81            record,
82        }
83    }
84}
85
86#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
87#[serde(rename_all = "snake_case")]
88pub enum OperationStatus {
89    Running,
90    VerifiedComplete,
91    NotDispatched,
92    OutcomeUnknown,
93}
94
95impl OperationStatus {
96    pub fn exit_code(self) -> i32 {
97        match self {
98            Self::VerifiedComplete => 0,
99            Self::NotDispatched => 1,
100            Self::Running => 3,
101            Self::OutcomeUnknown => 4,
102        }
103    }
104}
105
106impl OperationRecord {
107    pub fn status(&self) -> OperationStatus {
108        if matches!(self.state, OperationState::Running) {
109            return OperationStatus::Running;
110        }
111        match self.outcome {
112            ActionOutcome::VerifiedComplete if self.error_code.is_none() => {
113                OperationStatus::VerifiedComplete
114            }
115            ActionOutcome::NotDispatched => OperationStatus::NotDispatched,
116            _ => OperationStatus::OutcomeUnknown,
117        }
118    }
119}
120
121#[derive(Debug, Clone, Default, Serialize, Deserialize, schemars::JsonSchema)]
122pub struct ActionSettings {
123    #[serde(default, skip_serializing_if = "Option::is_none")]
124    #[schemars(
125        description = "Optional UUID. Generated when omitted and returned as id. Send your own to retry safely after a lost response."
126    )]
127    pub operation_id: Option<String>,
128    #[serde(default, skip_serializing_if = "Option::is_none")]
129    #[schemars(
130        description = "A condition that is false now and true after the action, for example {\"kind\":\"url\",\"value\":\"/submit\",\"contains\":true}. A wrong guess fails after the action has already run."
131    )]
132    pub expect: Option<Expectation>,
133    #[serde(default, skip_serializing_if = "Option::is_none")]
134    pub timeout_ms: Option<u64>,
135}
136
137#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
138pub struct ActionCommandArgs {
139    pub operation_id: String,
140    pub command: String,
141    pub arguments: serde_json::Value,
142}
143
144#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
145pub struct ActionBatchArgs {
146    pub actions: Vec<ActionPerformArgs>,
147}