Skip to main content

objectiveai_sdk/
remote.rs

1//! Remote source types for function, profile, and agent hosting.
2
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5use std::fmt;
6
7/// The remote source where a function, profile, or agent is hosted.
8#[derive(
9    Debug,
10    Clone,
11    Copy,
12    Serialize,
13    Deserialize,
14    PartialEq,
15    Eq,
16    Hash,
17    JsonSchema,
18    arbitrary::Arbitrary,
19)]
20#[serde(rename_all = "snake_case")]
21#[schemars(rename = "Remote")]
22pub enum Remote {
23    /// GitHub repository.
24    #[schemars(title = "Github")]
25    Github,
26    /// The connected client (resolved over the websocket reverse-channel).
27    #[schemars(title = "Client")]
28    Client,
29    /// Mock (for testing).
30    #[schemars(title = "Mock")]
31    Mock,
32}
33
34impl fmt::Display for Remote {
35    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36        match self {
37            Remote::Github => write!(f, "github"),
38            Remote::Client => write!(f, "client"),
39            Remote::Mock => write!(f, "mock"),
40        }
41    }
42}
43
44#[derive(
45    Debug,
46    Clone,
47    Serialize,
48    Deserialize,
49    PartialEq,
50    Eq,
51    Hash,
52    JsonSchema,
53    arbitrary::Arbitrary,
54)]
55#[serde(tag = "remote", rename_all = "snake_case")]
56#[schemars(rename = "RemotePath")]
57pub enum RemotePath {
58    #[schemars(title = "Github")]
59    Github {
60        owner: String,
61        repository: String,
62        commit: String,
63    },
64    #[schemars(title = "Client")]
65    Client {
66        owner: String,
67        repository: String,
68        commit: String,
69    },
70    #[schemars(title = "Mock")]
71    Mock { name: String },
72}
73
74impl RemotePath {
75    pub fn remote(&self) -> Remote {
76        match self {
77            RemotePath::Github { .. } => Remote::Github,
78            RemotePath::Client { .. } => Remote::Client,
79            RemotePath::Mock { .. } => Remote::Mock,
80        }
81    }
82
83    pub fn name(&self) -> &str {
84        match self {
85            RemotePath::Github { repository, .. } => repository,
86            RemotePath::Client { repository, .. } => repository,
87            RemotePath::Mock { name } => name,
88        }
89    }
90
91    pub fn key(&self) -> String {
92        match self {
93            RemotePath::Github {
94                owner,
95                repository,
96                commit,
97            } => {
98                format!("{}/{}/{}/{}", self.remote(), owner, repository, commit)
99            }
100            RemotePath::Client {
101                owner,
102                repository,
103                commit,
104            } => {
105                format!("{}/{}/{}/{}", self.remote(), owner, repository, commit)
106            }
107            RemotePath::Mock { name } => {
108                format!("{}/{}", self.remote(), name)
109            }
110        }
111    }
112
113    pub fn url(&self) -> String {
114        match self {
115            RemotePath::Github {
116                owner,
117                repository,
118                commit,
119            } => format!(
120                "[{}](https://github.com/{}/{}/commit/{})",
121                repository, owner, repository, commit
122            ),
123            RemotePath::Client {
124                owner,
125                repository,
126                commit,
127            } => format!(
128                "[{}](client://{}/{}) ({})",
129                repository, owner, repository, commit
130            ),
131            RemotePath::Mock { name } => format!("[{}](mock://{})", name, name),
132        }
133    }
134}
135
136#[derive(
137    Debug,
138    Clone,
139    Serialize,
140    Deserialize,
141    PartialEq,
142    Eq,
143    Hash,
144    JsonSchema,
145    arbitrary::Arbitrary,
146)]
147#[serde(tag = "remote", rename_all = "snake_case")]
148#[schemars(rename = "RemotePathCommitOptional")]
149pub enum RemotePathCommitOptional {
150    #[schemars(title = "Github")]
151    Github {
152        owner: String,
153        repository: String,
154        commit: Option<String>,
155    },
156    #[schemars(title = "Client")]
157    Client {
158        owner: String,
159        repository: String,
160        commit: Option<String>,
161    },
162    #[schemars(title = "Mock")]
163    Mock { name: String },
164}
165
166impl RemotePathCommitOptional {
167    pub fn remote(&self) -> Remote {
168        match self {
169            RemotePathCommitOptional::Github { .. } => Remote::Github,
170            RemotePathCommitOptional::Client { .. } => Remote::Client,
171            RemotePathCommitOptional::Mock { .. } => Remote::Mock,
172        }
173    }
174}
175
176impl From<RemotePath> for RemotePathCommitOptional {
177    fn from(path: RemotePath) -> Self {
178        match path {
179            RemotePath::Github {
180                owner,
181                repository,
182                commit,
183            } => RemotePathCommitOptional::Github {
184                owner,
185                repository,
186                commit: Some(commit),
187            },
188            RemotePath::Client {
189                owner,
190                repository,
191                commit,
192            } => RemotePathCommitOptional::Client {
193                owner,
194                repository,
195                commit: Some(commit),
196            },
197            RemotePath::Mock { name } => {
198                RemotePathCommitOptional::Mock { name }
199            }
200        }
201    }
202}