Skip to main content

heddle_client/grpc_hosted/
content.rs

1use grpc::heddle::v1::{
2    AnnotationScope, CompareResponse, ContextAnnotationKind, GetBlameRequest, GetBlameResponse,
3    GetCompareRequest, GetContextHistoryRequest, GetContextHistoryResponse, ListContextRequest,
4    ListContextResponse, ListContextSuggestionsRequest, ListContextSuggestionsResponse,
5    ReviseContextRequest, ReviseContextResponse, SetContextRequest, SetContextResponse,
6    SupersedeContextRequest, SupersedeContextResponse,
7};
8use tonic::Request;
9use wire::ProtocolError;
10
11use super::{HostedGrpcClient, helpers::status_to_protocol_error};
12
13impl HostedGrpcClient {
14    pub async fn get_compare(
15        &mut self,
16        repo_path: &str,
17        from: &str,
18        to: &str,
19        include_semantic: bool,
20    ) -> Result<CompareResponse, ProtocolError> {
21        let mut request = Request::new(GetCompareRequest {
22            repo_path: repo_path.to_string(),
23            from: from.to_string(),
24            to: to.to_string(),
25            include_semantic,
26        });
27        self.apply_signed_auth(&mut request, "/heddle.v1.ContentService/GetCompare")?;
28        self.content
29            .get_compare(request)
30            .await
31            .map_err(status_to_protocol_error)
32            .map(|response| response.into_inner())
33    }
34
35    pub async fn get_blame(
36        &mut self,
37        repo_path: &str,
38        r#ref: Option<&str>,
39        path: &str,
40    ) -> Result<GetBlameResponse, ProtocolError> {
41        let mut request = Request::new(GetBlameRequest {
42            repo_path: repo_path.to_string(),
43            r#ref: r#ref.unwrap_or_default().to_string(),
44            path: path.to_string(),
45        });
46        self.apply_signed_auth(&mut request, "/heddle.v1.ContentService/GetBlame")?;
47        self.content
48            .get_blame(request)
49            .await
50            .map_err(status_to_protocol_error)
51            .map(|response| response.into_inner())
52    }
53
54    pub async fn list_context(
55        &mut self,
56        repo_path: &str,
57        r#ref: Option<&str>,
58        prefix: Option<&str>,
59        tag_filter: Option<&str>,
60    ) -> Result<ListContextResponse, ProtocolError> {
61        let mut request = Request::new(ListContextRequest {
62            repo_path: repo_path.to_string(),
63            r#ref: r#ref.unwrap_or_default().to_string(),
64            prefix: prefix.map(str::to_string),
65            tag_filter: tag_filter.map(str::to_string),
66        });
67        self.apply_signed_auth(&mut request, "/heddle.v1.ContentService/ListContext")?;
68        self.content
69            .list_context(request)
70            .await
71            .map_err(status_to_protocol_error)
72            .map(|response| response.into_inner())
73    }
74
75    #[allow(clippy::too_many_arguments)]
76    pub async fn set_context(
77        &mut self,
78        repo_path: &str,
79        path: &str,
80        target_state_id: Option<&str>,
81        scope: AnnotationScope,
82        kind: ContextAnnotationKind,
83        tags: Vec<String>,
84        content: &str,
85        agent_provider: Option<&str>,
86        agent_model: Option<&str>,
87    ) -> Result<SetContextResponse, ProtocolError> {
88        let mut request = Request::new(SetContextRequest {
89            repo_path: repo_path.to_string(),
90            path: path.to_string(),
91            scope: Some(scope),
92            tags,
93            content: content.to_string(),
94            agent_provider: agent_provider.unwrap_or_default().to_string(),
95            agent_model: agent_model.unwrap_or_default().to_string(),
96            target_state_id: target_state_id
97                .and_then(|s| objects::object::ChangeId::parse(s).ok())
98                .map(|id| id.as_bytes().to_vec()),
99            kind: kind as i32,
100            client_operation_id: String::new(),
101        });
102        self.apply_signed_auth(&mut request, "/heddle.v1.ContentService/SetContext")?;
103        self.content
104            .set_context(request)
105            .await
106            .map_err(status_to_protocol_error)
107            .map(|response| response.into_inner())
108    }
109
110    pub async fn get_context_history(
111        &mut self,
112        repo_path: &str,
113        r#ref: Option<&str>,
114        annotation_id: &str,
115    ) -> Result<GetContextHistoryResponse, ProtocolError> {
116        let mut request = Request::new(GetContextHistoryRequest {
117            repo_path: repo_path.to_string(),
118            r#ref: r#ref.unwrap_or_default().to_string(),
119            annotation_id: annotation_id.to_string(),
120        });
121        self.apply_signed_auth(&mut request, "/heddle.v1.ContentService/GetContextHistory")?;
122        self.content
123            .get_context_history(request)
124            .await
125            .map_err(status_to_protocol_error)
126            .map(|response| response.into_inner())
127    }
128
129    pub async fn list_context_suggestions(
130        &mut self,
131        repo_path: &str,
132        r#ref: Option<&str>,
133        limit: u32,
134    ) -> Result<ListContextSuggestionsResponse, ProtocolError> {
135        let mut request = Request::new(ListContextSuggestionsRequest {
136            repo_path: repo_path.to_string(),
137            r#ref: r#ref.unwrap_or_default().to_string(),
138            limit,
139        });
140        self.apply_signed_auth(
141            &mut request,
142            "/heddle.v1.ContentService/ListContextSuggestions",
143        )?;
144        self.content
145            .list_context_suggestions(request)
146            .await
147            .map_err(status_to_protocol_error)
148            .map(|response| response.into_inner())
149    }
150
151    #[allow(clippy::too_many_arguments)]
152    pub async fn revise_context(
153        &mut self,
154        repo_path: &str,
155        annotation_id: &str,
156        content: &str,
157        tags: Vec<String>,
158        agent_provider: Option<&str>,
159        agent_model: Option<&str>,
160        kind: ContextAnnotationKind,
161    ) -> Result<ReviseContextResponse, ProtocolError> {
162        let mut request = Request::new(ReviseContextRequest {
163            repo_path: repo_path.to_string(),
164            annotation_id: annotation_id.to_string(),
165            content: content.to_string(),
166            tags,
167            agent_provider: agent_provider.unwrap_or_default().to_string(),
168            agent_model: agent_model.unwrap_or_default().to_string(),
169            kind: kind as i32,
170        });
171        self.apply_signed_auth(&mut request, "/heddle.v1.ContentService/ReviseContext")?;
172        self.content
173            .revise_context(request)
174            .await
175            .map_err(status_to_protocol_error)
176            .map(|response| response.into_inner())
177    }
178
179    #[allow(clippy::too_many_arguments)]
180    pub async fn supersede_context(
181        &mut self,
182        repo_path: &str,
183        annotation_id: &str,
184        path: Option<&str>,
185        target_state_id: Option<&str>,
186        scope: AnnotationScope,
187        tags: Vec<String>,
188        content: &str,
189        agent_provider: Option<&str>,
190        agent_model: Option<&str>,
191        kind: ContextAnnotationKind,
192    ) -> Result<SupersedeContextResponse, ProtocolError> {
193        let mut request = Request::new(SupersedeContextRequest {
194            repo_path: repo_path.to_string(),
195            annotation_id: annotation_id.to_string(),
196            path: path.unwrap_or_default().to_string(),
197            scope: Some(scope),
198            tags,
199            content: content.to_string(),
200            agent_provider: agent_provider.unwrap_or_default().to_string(),
201            agent_model: agent_model.unwrap_or_default().to_string(),
202            target_state_id: target_state_id
203                .and_then(|s| objects::object::ChangeId::parse(s).ok())
204                .map(|id| id.as_bytes().to_vec()),
205            kind: kind as i32,
206        });
207        self.apply_signed_auth(&mut request, "/heddle.v1.ContentService/SupersedeContext")?;
208        self.content
209            .supersede_context(request)
210            .await
211            .map_err(status_to_protocol_error)
212            .map(|response| response.into_inner())
213    }
214}