Skip to main content

percli_core/agent/
protocol.rs

1use serde::{Deserialize, Serialize};
2
3use crate::{EngineSnapshot, ParamsConfig};
4
5/// Sent once at the start of an agent session.
6#[derive(Debug, Clone, Serialize)]
7pub struct InitMessage {
8    #[serde(rename = "type")]
9    pub msg_type: &'static str,
10    pub params: ParamsConfig,
11    pub accounts: Vec<String>,
12    pub snapshot: EngineSnapshot,
13}
14
15impl InitMessage {
16    pub fn new(params: ParamsConfig, accounts: Vec<String>, snapshot: EngineSnapshot) -> Self {
17        Self {
18            msg_type: "init",
19            params,
20            accounts,
21            snapshot,
22        }
23    }
24}
25
26/// Sent each tick with current market state and engine snapshot.
27#[derive(Debug, Clone, Serialize)]
28pub struct TickMessage {
29    #[serde(rename = "type")]
30    pub msg_type: &'static str,
31    pub tick: u64,
32    pub oracle_price: u64,
33    pub slot: u64,
34    pub snapshot: EngineSnapshot,
35}
36
37impl TickMessage {
38    pub fn new(tick: u64, oracle_price: u64, slot: u64, snapshot: EngineSnapshot) -> Self {
39        Self {
40            msg_type: "tick",
41            tick,
42            oracle_price,
43            slot,
44            snapshot,
45        }
46    }
47}
48
49/// Sent when the feed is exhausted or the run ends.
50#[derive(Debug, Clone, Serialize)]
51pub struct DoneMessage {
52    #[serde(rename = "type")]
53    pub msg_type: &'static str,
54    pub ticks: u64,
55    pub elapsed_s: f64,
56}
57
58impl DoneMessage {
59    pub fn new(ticks: u64, elapsed_s: f64) -> Self {
60        Self {
61            msg_type: "done",
62            ticks,
63            elapsed_s,
64        }
65    }
66}
67
68/// Response from the agent process.
69#[derive(Debug, Clone, Deserialize)]
70#[serde(untagged)]
71pub enum AgentMessage {
72    Response(AgentResponse),
73    Shutdown(ShutdownMessage),
74}
75
76#[derive(Debug, Clone, Deserialize)]
77pub struct AgentResponse {
78    pub actions: Vec<AgentAction>,
79}
80
81#[derive(Debug, Clone, Deserialize)]
82pub struct ShutdownMessage {
83    #[serde(rename = "type")]
84    pub msg_type: String,
85}
86
87impl ShutdownMessage {
88    pub fn is_shutdown(&self) -> bool {
89        self.msg_type == "shutdown"
90    }
91}
92
93/// A single action the agent wants to execute.
94#[derive(Debug, Clone, Deserialize)]
95#[serde(tag = "op", rename_all = "snake_case")]
96pub enum AgentAction {
97    Deposit {
98        account: String,
99        amount: u64,
100    },
101    Withdraw {
102        account: String,
103        amount: u64,
104    },
105    Trade {
106        long: String,
107        short: String,
108        size: i64,
109        price: u64,
110    },
111    Liquidate {
112        account: String,
113    },
114    Settle {
115        account: String,
116    },
117    Noop,
118}