Skip to main content

babelforce_manager_sdk/resources/
conversations.rs

1use crate::error::{map_manager_err, ManagerError};
2use crate::gen::manager::apis::configuration::Configuration;
3use crate::gen::manager::apis::{conversation_api as api, conversations_api as sub};
4use crate::gen::manager::models;
5use crate::http::collect_all;
6use crate::retry::{with_retry, RetryPolicy};
7use crate::token::SharedCfg;
8
9/// Conversations — `/api/v2/conversations`, including their events and session variables.
10pub struct ConversationsResource {
11    pub(crate) cfg: SharedCfg<Configuration>,
12    pub(crate) retry: RetryPolicy,
13}
14
15impl ConversationsResource {
16    /// List all conversations (auto-paginated).
17    pub async fn list_all(&self) -> Result<Vec<models::Conversation>, ManagerError> {
18        let cfg = self.cfg.get().await?;
19        let cfg = cfg.as_ref();
20        collect_all(&self.retry, map_manager_err, |page| async move {
21            let r = api::list_conversations(cfg, Some(page), None, None, None).await?;
22            Ok((
23                r.items,
24                r.pagination.pages.unwrap_or(1),
25                r.pagination.current.unwrap_or(1),
26            ))
27        })
28        .await
29    }
30
31    /// Create a conversation.
32    pub async fn create(
33        &self,
34        body: models::RestCreateConversation,
35    ) -> Result<models::ConversationItemResponse, ManagerError> {
36        let cfg = self.cfg.get().await?;
37        let cfg = cfg.as_ref();
38        with_retry(&self.retry, false, || {
39            api::create_conversation(cfg, body.clone())
40        })
41        .await
42        .map_err(map_manager_err)
43    }
44
45    /// Get a conversation by id.
46    pub async fn get(&self, id: &str) -> Result<models::ConversationItemResponse, ManagerError> {
47        let cfg = self.cfg.get().await?;
48        let cfg = cfg.as_ref();
49        with_retry(&self.retry, true, || api::get_conversation(cfg, id))
50            .await
51            .map_err(map_manager_err)
52    }
53
54    /// Update a conversation.
55    pub async fn update(
56        &self,
57        id: &str,
58        body: models::RestUpdateConversation,
59    ) -> Result<models::ConversationItemResponse, ManagerError> {
60        let cfg = self.cfg.get().await?;
61        let cfg = cfg.as_ref();
62        with_retry(&self.retry, false, || {
63            api::update_conversation(cfg, id, body.clone())
64        })
65        .await
66        .map_err(map_manager_err)
67    }
68
69    /// Delete a conversation.
70    pub async fn delete(&self, id: &str) -> Result<(), ManagerError> {
71        let cfg = self.cfg.get().await?;
72        let cfg = cfg.as_ref();
73        with_retry(&self.retry, false, || api::delete_conversation(cfg, id))
74            .await
75            .map_err(map_manager_err)?;
76        Ok(())
77    }
78
79    /// List a conversation's events.
80    pub async fn events(
81        &self,
82        conversation_id: &str,
83    ) -> Result<Vec<models::ConversationEvent>, ManagerError> {
84        let cfg = self.cfg.get().await?;
85        let cfg = cfg.as_ref();
86        let resp = with_retry(&self.retry, true, || {
87            sub::list_conversation_events(cfg, conversation_id)
88        })
89        .await
90        .map_err(map_manager_err)?;
91        Ok(resp.items)
92    }
93
94    /// Add an event to a conversation.
95    pub async fn add_event(
96        &self,
97        conversation_id: &str,
98        body: models::ConversationEventRequest,
99    ) -> Result<models::ConversationEventItemResponse, ManagerError> {
100        let cfg = self.cfg.get().await?;
101        let cfg = cfg.as_ref();
102        with_retry(&self.retry, false, || {
103            sub::add_conversation_event(cfg, conversation_id, body.clone())
104        })
105        .await
106        .map_err(map_manager_err)
107    }
108
109    /// Open a conversation.
110    pub async fn open(
111        &self,
112        conversation_id: &str,
113    ) -> Result<models::ConversationItemResponse, ManagerError> {
114        let cfg = self.cfg.get().await?;
115        let cfg = cfg.as_ref();
116        with_retry(&self.retry, false, || {
117            sub::open_conversation(cfg, conversation_id)
118        })
119        .await
120        .map_err(map_manager_err)
121    }
122
123    /// Close a conversation.
124    pub async fn close(
125        &self,
126        conversation_id: &str,
127    ) -> Result<models::ConversationItemResponse, ManagerError> {
128        let cfg = self.cfg.get().await?;
129        let cfg = cfg.as_ref();
130        with_retry(&self.retry, false, || {
131            sub::close_conversation(cfg, conversation_id)
132        })
133        .await
134        .map_err(map_manager_err)
135    }
136
137    /// Get a conversation's first event.
138    pub async fn first_event(
139        &self,
140        conversation_id: &str,
141    ) -> Result<models::ConversationEventItemResponse, ManagerError> {
142        let cfg = self.cfg.get().await?;
143        let cfg = cfg.as_ref();
144        with_retry(&self.retry, true, || {
145            sub::get_first_conversation_event(cfg, conversation_id)
146        })
147        .await
148        .map_err(map_manager_err)
149    }
150
151    /// Get a conversation's latest event.
152    pub async fn latest_event(
153        &self,
154        conversation_id: &str,
155    ) -> Result<models::ConversationEventItemResponse, ManagerError> {
156        let cfg = self.cfg.get().await?;
157        let cfg = cfg.as_ref();
158        with_retry(&self.retry, true, || {
159            sub::get_latest_conversation_event(cfg, conversation_id)
160        })
161        .await
162        .map_err(map_manager_err)
163    }
164
165    /// List events across all conversations (auto-paginated).
166    pub async fn all_events(&self) -> Result<Vec<models::ConversationEvent>, ManagerError> {
167        let cfg = self.cfg.get().await?;
168        let cfg = cfg.as_ref();
169        collect_all(&self.retry, map_manager_err, |page| async move {
170            let r = sub::list_all_conversation_events(cfg, Some(page), None).await?;
171            Ok((
172                r.items,
173                r.pagination.pages.unwrap_or(1),
174                r.pagination.current.unwrap_or(1),
175            ))
176        })
177        .await
178    }
179
180    /// Get a conversation's session variables.
181    pub async fn get_session(
182        &self,
183        conversation_id: &str,
184    ) -> Result<models::ConversationSessionVariablesItemResponse, ManagerError> {
185        let cfg = self.cfg.get().await?;
186        let cfg = cfg.as_ref();
187        with_retry(&self.retry, true, || {
188            sub::get_conversation_session(cfg, conversation_id)
189        })
190        .await
191        .map_err(map_manager_err)
192    }
193
194    /// Update a conversation's session variables.
195    pub async fn update_session(
196        &self,
197        conversation_id: &str,
198        variables: std::collections::HashMap<String, serde_json::Value>,
199    ) -> Result<models::ConversationSessionVariablesItemResponse, ManagerError> {
200        let cfg = self.cfg.get().await?;
201        let cfg = cfg.as_ref();
202        with_retry(&self.retry, false, || {
203            sub::update_conversation_session(cfg, conversation_id, Some(variables.clone()))
204        })
205        .await
206        .map_err(map_manager_err)
207    }
208}