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 wire::ProtocolError;
9use tonic::Request;
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_auth(&mut request)?;
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_auth(&mut request)?;
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_auth(&mut request)?;
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_auth(&mut request)?;
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_auth(&mut request)?;
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_auth(&mut request)?;
141        self.content
142            .list_context_suggestions(request)
143            .await
144            .map_err(status_to_protocol_error)
145            .map(|response| response.into_inner())
146    }
147
148    #[allow(clippy::too_many_arguments)]
149    pub async fn revise_context(
150        &mut self,
151        repo_path: &str,
152        annotation_id: &str,
153        content: &str,
154        tags: Vec<String>,
155        agent_provider: Option<&str>,
156        agent_model: Option<&str>,
157        kind: ContextAnnotationKind,
158    ) -> Result<ReviseContextResponse, ProtocolError> {
159        let mut request = Request::new(ReviseContextRequest {
160            repo_path: repo_path.to_string(),
161            annotation_id: annotation_id.to_string(),
162            content: content.to_string(),
163            tags,
164            agent_provider: agent_provider.unwrap_or_default().to_string(),
165            agent_model: agent_model.unwrap_or_default().to_string(),
166            kind: kind as i32,
167        });
168        self.apply_auth(&mut request)?;
169        self.content
170            .revise_context(request)
171            .await
172            .map_err(status_to_protocol_error)
173            .map(|response| response.into_inner())
174    }
175
176    #[allow(clippy::too_many_arguments)]
177    pub async fn supersede_context(
178        &mut self,
179        repo_path: &str,
180        annotation_id: &str,
181        path: Option<&str>,
182        target_state_id: Option<&str>,
183        scope: AnnotationScope,
184        tags: Vec<String>,
185        content: &str,
186        agent_provider: Option<&str>,
187        agent_model: Option<&str>,
188        kind: ContextAnnotationKind,
189    ) -> Result<SupersedeContextResponse, ProtocolError> {
190        let mut request = Request::new(SupersedeContextRequest {
191            repo_path: repo_path.to_string(),
192            annotation_id: annotation_id.to_string(),
193            path: path.unwrap_or_default().to_string(),
194            scope: Some(scope),
195            tags,
196            content: content.to_string(),
197            agent_provider: agent_provider.unwrap_or_default().to_string(),
198            agent_model: agent_model.unwrap_or_default().to_string(),
199            target_state_id: target_state_id
200                .and_then(|s| objects::object::ChangeId::parse(s).ok())
201                .map(|id| id.as_bytes().to_vec()),
202            kind: kind as i32,
203        });
204        self.apply_auth(&mut request)?;
205        self.content
206            .supersede_context(request)
207            .await
208            .map_err(status_to_protocol_error)
209            .map(|response| response.into_inner())
210    }
211}