Skip to main content

babelforce_manager_sdk/resources/
conversations.rs

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