language_barrier_core/
executor.rs

1use reqwest::Client;
2use tracing::{debug, error, info, instrument, trace, warn};
3
4use crate::{Chat, Message, ModelInfo, error::Result, provider::HTTPProvider};
5
6pub struct SingleRequestExecutor<M: ModelInfo> {
7    provider: Box<dyn HTTPProvider<M>>,
8    client: Client,
9}
10
11impl<M: ModelInfo> SingleRequestExecutor<M> {
12    #[instrument(skip_all, level = "debug")]
13    pub fn new(provider: impl HTTPProvider<M> + 'static) -> Self {
14        info!("Creating new SingleRequestExecutor");
15        Self {
16            provider: Box::new(provider),
17            client: Client::new(),
18        }
19    }
20
21    #[instrument(skip(self, chat), fields(model_type = std::any::type_name::<M>()), level = "debug")]
22    pub async fn send(&self, chat: Chat<M>) -> Result<Message> {
23        info!("Sending chat request with {} messages", chat.history.len());
24        trace!("System prompt: {}", chat.system_prompt);
25        
26        // Convert chat to request using provider
27        debug!("Converting chat to HTTP request");
28        let request = match self.provider.accept(chat) {
29            Ok(req) => {
30                debug!("Request created successfully: {} {}", req.method(), req.url());
31                trace!("Request headers: {:#?}", req.headers());
32                req
33            },
34            Err(e) => {
35                error!("Failed to create request: {}", e);
36                return Err(e);
37            }
38        };
39        
40        // Send request and get response
41        debug!("Sending HTTP request");
42        let response = match self.client.execute(request).await {
43            Ok(resp) => {
44                info!("Received response with status: {}", resp.status());
45                trace!("Response headers: {:#?}", resp.headers());
46                resp
47            },
48            Err(e) => {
49                error!("HTTP request failed: {}", e);
50                return Err(e.into());
51            }
52        };
53        
54        // Get response text
55        debug!("Reading response body");
56        let response_text = match response.text().await {
57            Ok(text) => {
58                trace!("Response body: {}", text);
59                text
60            },
61            Err(e) => {
62                error!("Failed to read response body: {}", e);
63                return Err(e.into());
64            }
65        };
66        
67        // Parse response using provider
68        debug!("Parsing response");
69        let message = match self.provider.parse(response_text) {
70            Ok(msg) => {
71                info!("Successfully parsed response into message");
72                debug!("Message role: {}", msg.role_str());
73                // Message content is now accessed through pattern matching
74                msg
75            },
76            Err(e) => {
77                error!("Failed to parse response: {}", e);
78                return Err(e);
79            }
80        };
81        
82        Ok(message)
83    }
84}