Skip to main content

gent_protocol/
notifications.rs

1//! Account-scoped events shared by desktop and future delivery clients.
2use serde::{Deserialize, Serialize};
3use uuid::Uuid;
4#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
5#[serde(deny_unknown_fields)]
6pub struct Scope {
7    pub account: String,
8    pub origin: String,
9}
10#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
11#[serde(deny_unknown_fields)]
12pub struct Notice {
13    pub id: Uuid,
14    pub created_at: i64,
15    pub event: Event,
16}
17#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
18#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
19pub enum Event {
20    GoalResumed {
21        chat: Uuid,
22        node: Uuid,
23        reason: Failure,
24    },
25    UpdateInstalled {
26        node: Uuid,
27        commit: String,
28    },
29}
30impl Notice {
31    pub fn valid(&self, now: i64) -> bool {
32        !self.id.is_nil()
33            && (now.saturating_sub(7 * 86400)..=now.saturating_add(60)).contains(&self.created_at)
34            && match &self.event {
35                Event::GoalResumed { chat, node, .. } => !chat.is_nil() && !node.is_nil(),
36                Event::UpdateInstalled { node, commit } => {
37                    !node.is_nil()
38                        && commit.len() == 40
39                        && commit.bytes().all(|b| b.is_ascii_hexdigit())
40                }
41            }
42    }
43}
44
45#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
46#[serde(rename_all = "snake_case")]
47pub enum Failure {
48    Capacity,
49    RateLimit,
50    Connection,
51    ToolSchema,
52    RequestPayload,
53}
54impl Failure {
55    pub fn label(self) -> &'static str {
56        match self {
57            Self::Capacity => "model capacity",
58            Self::RateLimit => "a rate limit",
59            Self::Connection => "a connection error",
60            Self::ToolSchema => "a provider tool-schema error",
61            Self::RequestPayload => "a provider request-format error",
62        }
63    }
64}