Skip to main content

objectiveai_sdk/
remote.rs

1//! Remote source types for function, profile, and agent hosting.
2
3use serde::{Deserialize, Serialize};
4use std::fmt;
5use schemars::JsonSchema;
6
7/// The remote source where a function, profile, or agent is hosted.
8#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash, JsonSchema, arbitrary::Arbitrary)]
9#[serde(rename_all = "snake_case")]
10#[schemars(rename = "Remote")]
11pub enum Remote {
12    /// GitHub repository.
13    #[schemars(title = "Github")]
14    Github,
15    /// Local filesystem.
16    #[schemars(title = "Filesystem")]
17    Filesystem,
18    /// Mock (for testing).
19    #[schemars(title = "Mock")]
20    Mock,
21}
22
23impl fmt::Display for Remote {
24    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25        match self {
26            Remote::Github => write!(f, "github"),
27            Remote::Filesystem => write!(f, "filesystem"),
28            Remote::Mock => write!(f, "mock"),
29        }
30    }
31}
32
33#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash, JsonSchema, arbitrary::Arbitrary)]
34#[serde(tag = "remote", rename_all = "snake_case")]
35#[schemars(rename = "RemotePath")]
36pub enum RemotePath {
37    #[schemars(title = "Github")]
38    Github {
39        owner: String,
40        repository: String,
41        commit: String,
42    },
43    #[schemars(title = "Filesystem")]
44    Filesystem {
45        owner: String,
46        repository: String,
47        commit: String,
48    },
49    #[schemars(title = "Mock")]
50    Mock {
51        name: String,
52    },
53}
54
55impl RemotePath {
56    pub fn remote(&self) -> Remote {
57        match self {
58            RemotePath::Github { .. } => Remote::Github,
59            RemotePath::Filesystem { .. } => Remote::Filesystem,
60            RemotePath::Mock { .. } => Remote::Mock,
61        }
62    }
63
64    pub fn name(&self) -> &str {
65        match self {
66            RemotePath::Github { repository, .. } => repository,
67            RemotePath::Filesystem { repository, .. } => repository,
68            RemotePath::Mock { name } => name,
69        }
70    }
71
72    pub fn key(&self) -> String {
73        match self {
74            RemotePath::Github { owner, repository, commit } => {
75                format!("{}/{}/{}/{}", self.remote(), owner, repository, commit)
76            }
77            RemotePath::Filesystem { owner, repository, commit } => {
78                format!("{}/{}/{}/{}", self.remote(), owner, repository, commit)
79            }
80            RemotePath::Mock { name } => {
81                format!("{}/{}", self.remote(), name)
82            }
83        }
84    }
85
86    pub fn url(&self) -> String {
87        match self {
88            RemotePath::Github { owner, repository, commit } => format!(
89                "[{}](https://github.com/{}/{}/commit/{})",
90                repository, owner, repository, commit
91            ),
92            RemotePath::Filesystem { owner, repository, commit } => format!(
93                "[{}](file://{}/{}) ({})",
94                repository, owner, repository, commit
95            ),
96            RemotePath::Mock { name } => format!(
97                "[{}](mock://{})",
98                name, name
99            ),
100        }
101    }
102}
103
104#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash, JsonSchema, arbitrary::Arbitrary)]
105#[serde(tag = "remote", rename_all = "snake_case")]
106#[schemars(rename = "RemotePathCommitOptional")]
107pub enum RemotePathCommitOptional {
108    #[schemars(title = "Github")]
109    Github {
110        owner: String,
111        repository: String,
112        commit: Option<String>,
113    },
114    #[schemars(title = "Filesystem")]
115    Filesystem {
116        owner: String,
117        repository: String,
118        commit: Option<String>,
119    },
120    #[schemars(title = "Mock")]
121    Mock {
122        name: String,
123    },
124}
125
126impl RemotePathCommitOptional {
127    pub fn remote(&self) -> Remote {
128        match self {
129            RemotePathCommitOptional::Github { .. } => Remote::Github,
130            RemotePathCommitOptional::Filesystem { .. } => Remote::Filesystem,
131            RemotePathCommitOptional::Mock { .. } => Remote::Mock,
132        }
133    }
134}
135
136impl From<RemotePath> for RemotePathCommitOptional {
137    fn from(path: RemotePath) -> Self {
138        match path {
139            RemotePath::Github { owner, repository, commit } => {
140                RemotePathCommitOptional::Github { owner, repository, commit: Some(commit) }
141            }
142            RemotePath::Filesystem { owner, repository, commit } => {
143                RemotePathCommitOptional::Filesystem { owner, repository, commit: Some(commit) }
144            }
145            RemotePath::Mock { name } => {
146                RemotePathCommitOptional::Mock { name }
147            }
148        }
149    }
150}