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, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
71#[serde(rename_all = "snake_case")]
72pub enum OperationStatus {
73    Running,
74    VerifiedComplete,
75    NotDispatched,
76    OutcomeUnknown,
77}
78
79impl OperationStatus {
80    pub fn exit_code(self) -> i32 {
81        match self {
82            Self::VerifiedComplete => 0,
83            Self::NotDispatched => 1,
84            Self::Running => 3,
85            Self::OutcomeUnknown => 4,
86        }
87    }
88}
89
90impl OperationRecord {
91    pub fn status(&self) -> OperationStatus {
92        if matches!(self.state, OperationState::Running) {
93            return OperationStatus::Running;
94        }
95        match self.outcome {
96            ActionOutcome::VerifiedComplete if self.error_code.is_none() => {
97                OperationStatus::VerifiedComplete
98            }
99            ActionOutcome::NotDispatched => OperationStatus::NotDispatched,
100            _ => OperationStatus::OutcomeUnknown,
101        }
102    }
103}
104
105#[derive(Debug, Clone, Default, Serialize, Deserialize, schemars::JsonSchema)]
106pub struct ActionSettings {
107    #[serde(default, skip_serializing_if = "Option::is_none")]
108    #[schemars(
109        description = "Optional UUID. Generated when omitted and returned as id. Send your own to retry safely after a lost response."
110    )]
111    pub operation_id: Option<String>,
112    #[serde(default, skip_serializing_if = "Option::is_none")]
113    #[schemars(
114        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."
115    )]
116    pub expect: Option<Expectation>,
117    #[serde(default, skip_serializing_if = "Option::is_none")]
118    pub timeout_ms: Option<u64>,
119}
120
121#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
122pub struct ActionCommandArgs {
123    pub operation_id: String,
124    pub command: String,
125    pub arguments: serde_json::Value,
126}
127
128#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
129pub struct ActionBatchArgs {
130    pub actions: Vec<ActionPerformArgs>,
131}