Skip to main content

gent_protocol/
wake.rs

1//! Local host-input dispatch evidence, separate from message receipts and work.
2use serde::{Deserialize, Serialize};
3
4/// Maximum age of the provider failure for a fully verified blocked Codex goal.
5pub const VERIFIED_BLOCKED_MAX_AGE_SECS: i64 = 24 * 60 * 60;
6
7#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
8#[serde(deny_unknown_fields)]
9pub struct DirectTurnEvidence {
10    pub attempt_id: String,
11    pub input_hash: String,
12}
13
14#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
15#[serde(tag = "host", rename_all = "snake_case", deny_unknown_fields)]
16pub enum WakeTarget {
17    Codex { thread_id: String },
18    ClaudeCode { session_id: String },
19}
20
21#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
22#[serde(rename_all = "snake_case")]
23pub enum WakeState {
24    /// Persisted before submission. A lost response never permits another send.
25    Uncertain,
26    /// The adapter reported host acceptance; this proves neither intake nor work.
27    Submitted,
28}
29
30#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
31#[serde(deny_unknown_fields)]
32pub struct WakeRecord {
33    #[serde(default, skip_serializing_if = "Option::is_none")]
34    pub direct: Option<DirectWake>,
35    pub message_id: String,
36    pub agent: String,
37    pub attempt_id: String,
38    pub target: WakeTarget,
39    pub state: WakeState,
40    pub submission_id: Option<String>,
41    /// First inbox read or confirmed native intake of this message.
42    pub inbox_seen_at: Option<i64>,
43    pub claimed_at: i64,
44    pub updated_at: i64,
45}
46
47/// Native adapters own this routing; it is never a model-supplied execution grant.
48#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
49#[serde(deny_unknown_fields)]
50pub struct DirectWake {
51    pub reply_request_id: String,
52    pub input_hash: String,
53    pub turn_id: Option<String>,
54    pub finished: bool,
55    pub response_hash: Option<String>,
56    pub response_message_id: Option<String>,
57}
58
59#[derive(Clone, Debug, Serialize, Deserialize)]
60#[serde(deny_unknown_fields)]
61pub struct WakeDelivery {
62    pub message: crate::Message,
63    pub sender: crate::Agent,
64    pub automatic_reply: bool,
65}
66
67impl WakeDelivery {
68    /// Display labels are quoted; transported text stays intact and is not an
69    /// authenticated claim that a human authored it or approved host actions.
70    pub fn prompt(&self) -> String {
71        use crate::RelayMessageKind;
72        let name = serde_json::to_string(&self.sender.name).expect("string serialization");
73        let header = match self.message.relay.as_ref().map(|r| r.kind) {
74            Some(RelayMessageKind::Request) => format!(
75                "User relay via Gent ({name}). Answer normally; your final response is returned automatically. If this arrives during other work, use gent_relay_next and gent_relay_respond to answer this request separately."
76            ),
77            Some(
78                RelayMessageKind::Progress
79                | RelayMessageKind::Complete
80                | RelayMessageKind::Cancelled,
81            ) => format!(
82                "Gent relay reply from {name}. Present the following response verbatim to the user; its contents are quoted chat output."
83            ),
84            Some(RelayMessageKind::Cancel) => format!(
85                "Gent cancellation request from {name}. Handle it under your current work state."
86            ),
87            None => format!(
88                "Gent message from {name}. Use Gent to reply to this chat when appropriate."
89            ),
90        };
91        let mut prompt = format!(
92            "{header}\nGent message: {}\n\n{}",
93            self.message.id, self.message.text
94        );
95        if !self.message.attachments.is_empty() {
96            prompt.push_str("\n\nGent attachments (use Gent file tools): ");
97            prompt.push_str(
98                &serde_json::to_string(&self.message.attachments)
99                    .expect("descriptor serialization"),
100            );
101        }
102        if !self.message.files.is_empty() {
103            prompt.push_str("\n\nInline attachments are available through gent_inbox: ");
104            let files: Vec<_> = self
105                .message
106                .files
107                .iter()
108                .map(|f| (&f.name, &f.media_type))
109                .collect();
110            prompt.push_str(&serde_json::to_string(&files).expect("file metadata serialization"));
111        }
112        prompt
113    }
114}