chipp 0.3.0

Rust client for the Chipp.ai API - OpenAI-compatible chat completions with streaming support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//! Chipp API client implementation.

use crate::config::ChippConfig;
use crate::error::ChippClientError;
use crate::stream::ChippStream;
use crate::types::{
    ChatCompletionRequest, ChatCompletionResponse, ChatResponse, ChippMessage, ChippSession,
};

use backoff::backoff::Backoff;
use backoff::ExponentialBackoffBuilder;
use futures::StreamExt;
use std::sync::Arc;
use tokio::sync::Mutex;
use uuid::Uuid;

/// Chipp API client.
///
/// # Example
///
/// ```no_run
/// use chipp::{ChippClient, ChippConfig, ChippSession, ChippMessage};
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let config = ChippConfig::builder()
///     .api_key("YOUR_API_KEY_HERE")
///     .model("myapp-123")
///     .build()?;
///
/// let client = ChippClient::new(config)?;
/// let mut session = ChippSession::new();
///
/// let response = client.chat(&mut session, &[ChippMessage::user("Hello!")]).await?;
/// println!("Response: {}", response);
/// # Ok(())
/// # }
/// ```
pub struct ChippClient {
    http: reqwest::Client,
    config: ChippConfig,
}

impl ChippClient {
    /// Create a new Chipp API client.
    ///
    /// # Errors
    ///
    /// Returns `ChippClientError::HttpError` if the underlying HTTP client fails to build.
    pub fn new(config: ChippConfig) -> Result<Self, ChippClientError> {
        let http = reqwest::Client::builder().timeout(config.timeout).build()?;
        Ok(Self { http, config })
    }

    /// Determine if an error is retryable.
    fn is_retryable_error(error: &ChippClientError) -> bool {
        match error {
            ChippClientError::HttpError(e) => e.is_timeout() || e.is_connect() || e.is_request(),
            ChippClientError::ApiError { status, .. } => *status >= 500 || *status == 429,
            _ => false,
        }
    }

    /// Create a backoff strategy for retries.
    fn create_backoff(&self) -> backoff::ExponentialBackoff {
        ExponentialBackoffBuilder::new()
            .with_initial_interval(self.config.initial_retry_delay)
            .with_max_interval(self.config.max_retry_delay)
            .with_max_elapsed_time(None)
            .with_multiplier(2.0)
            .with_randomization_factor(0.3)
            .build()
    }

    /// Send a chat completion request (non-streaming).
    ///
    /// This is a convenience method that returns just the response content as a string.
    /// For access to token usage and other metadata, use [`chat_detailed()`](Self::chat_detailed).
    ///
    /// # Arguments
    ///
    /// * `session` - Session to track conversation state (updates `chatSessionId`)
    /// * `messages` - Messages in the conversation
    ///
    /// # Returns
    ///
    /// The assistant's response text.
    ///
    /// # Errors
    ///
    /// Returns error if HTTP request fails, API returns error, or response parsing fails.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use chipp::{ChippClient, ChippConfig, ChippSession, ChippMessage};
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// # let config = ChippConfig::default();
    /// # let client = ChippClient::new(config)?;
    /// let mut session = ChippSession::new();
    /// let response = client.chat(&mut session, &[ChippMessage::user("Hello!")]).await?;
    /// println!("Response: {}", response);
    /// # Ok(())
    /// # }
    /// ```
    #[tracing::instrument(skip(self, session, messages), fields(correlation_id))]
    pub async fn chat(
        &self,
        session: &mut ChippSession,
        messages: &[ChippMessage],
    ) -> Result<String, ChippClientError> {
        let response = self.chat_detailed(session, messages).await?;
        Ok(response.content().to_string())
    }

    /// Send a chat completion request and return the full response with metadata.
    ///
    /// This method returns a [`ChatResponse`] containing:
    /// - The AI's response content
    /// - Token usage information for rate limiting and monitoring
    /// - Completion ID for debugging
    /// - Timestamps and finish reason
    ///
    /// For simple use cases where you only need the response text,
    /// use [`chat()`](Self::chat) instead.
    ///
    /// # Arguments
    ///
    /// * `session` - Session to track conversation state (updates `chatSessionId`)
    /// * `messages` - Messages in the conversation
    ///
    /// # Returns
    ///
    /// A [`ChatResponse`] containing the response and metadata.
    ///
    /// # Errors
    ///
    /// Returns error if HTTP request fails, API returns error, or response parsing fails.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use chipp::{ChippClient, ChippConfig, ChippSession, ChippMessage};
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// # let config = ChippConfig::default();
    /// # let client = ChippClient::new(config)?;
    /// let mut session = ChippSession::new();
    /// let response = client.chat_detailed(&mut session, &[ChippMessage::user("Hello!")]).await?;
    ///
    /// println!("Response: {}", response.content());
    /// println!("Tokens used: {}", response.usage().total_tokens);
    /// # Ok(())
    /// # }
    /// ```
    #[tracing::instrument(skip(self, session, messages), fields(correlation_id))]
    pub async fn chat_detailed(
        &self,
        session: &mut ChippSession,
        messages: &[ChippMessage],
    ) -> Result<ChatResponse, ChippClientError> {
        let correlation_id = Uuid::new_v4().to_string();
        tracing::Span::current().record("correlation_id", &correlation_id);

        let mut backoff = self.create_backoff();
        let mut attempt = 0;
        let max_attempts = self.config.max_retries + 1;

        loop {
            attempt += 1;
            let result = self.chat_attempt(session, messages, &correlation_id).await;

            match result {
                Ok(response) => return Ok(response),
                Err(e) if attempt >= max_attempts => {
                    tracing::warn!(attempt, error = %e, "Max retry attempts exceeded");
                    return Err(ChippClientError::MaxRetriesExceeded(
                        self.config.max_retries,
                    ));
                }
                Err(e) if Self::is_retryable_error(&e) => {
                    if let Some(delay) = backoff.next_backoff() {
                        tracing::warn!(attempt, error = %e, delay_ms = delay.as_millis(), "Retrying");
                        tokio::time::sleep(delay).await;
                    } else {
                        return Err(e);
                    }
                }
                Err(e) => {
                    tracing::error!(error = %e, "Non-retryable error");
                    return Err(e);
                }
            }
        }
    }

    /// Internal method for a single chat attempt.
    ///
    /// Returns a `ChatResponse` with all metadata from the API.
    async fn chat_attempt(
        &self,
        session: &mut ChippSession,
        messages: &[ChippMessage],
        correlation_id: &str,
    ) -> Result<ChatResponse, ChippClientError> {
        let request_body = ChatCompletionRequest {
            model: self.config.model.clone(),
            messages: messages.to_vec(),
            stream: false,
            chat_session_id: session.chat_session_id.clone(),
        };

        let url = format!("{}/chat/completions", self.config.base_url);

        let response = self
            .http
            .post(&url)
            .header("Authorization", format!("Bearer {}", self.config.api_key))
            .header("Content-Type", "application/json")
            .header("X-Correlation-ID", correlation_id)
            .json(&request_body)
            .send()
            .await?;

        let status = response.status();
        if !status.is_success() {
            let error_text = response.text().await.unwrap_or_default();
            return Err(ChippClientError::ApiError {
                status: status.as_u16(),
                message: error_text,
            });
        }

        let response_body: ChatCompletionResponse = response.json().await.map_err(|e| {
            ChippClientError::InvalidResponse(format!("Failed to parse response: {}", e))
        })?;

        // Validate we have at least one choice before converting
        if response_body.choices.is_empty() {
            return Err(ChippClientError::InvalidResponse(
                "No choices in response".to_string(),
            ));
        }

        // Update session with the new session ID
        session.chat_session_id = Some(response_body.chat_session_id.clone());

        // Convert internal response to public type
        Ok(response_body.into())
    }

    /// Send a streaming chat completion request (SSE).
    ///
    /// Returns a stream of text chunks as they arrive from the API.
    /// The session's `chatSessionId` is updated when the stream receives metadata.
    ///
    /// # Arguments
    ///
    /// * `session` - Session to track conversation state
    /// * `messages` - Messages in the conversation
    ///
    /// # Returns
    ///
    /// A stream of `Result<String, ChippClientError>` where each `Ok(String)` is a text chunk.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use chipp::{ChippClient, ChippConfig, ChippSession, ChippMessage};
    /// use futures::StreamExt;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// # let config = ChippConfig::default();
    /// # let client = ChippClient::new(config)?;
    /// let mut session = ChippSession::new();
    /// let mut stream = client.chat_stream(&mut session, &[ChippMessage::user("Hello")]).await?;
    ///
    /// while let Some(chunk) = stream.next().await {
    ///     print!("{}", chunk?);
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub async fn chat_stream(
        &self,
        session: &mut ChippSession,
        messages: &[ChippMessage],
    ) -> Result<ChippStream, ChippClientError> {
        let correlation_id = Uuid::new_v4().to_string();

        let request_body = ChatCompletionRequest {
            model: self.config.model.clone(),
            messages: messages.to_vec(),
            stream: true,
            chat_session_id: session.chat_session_id.clone(),
        };

        let url = format!("{}/chat/completions", self.config.base_url);

        tracing::debug!("Sending Chipp API streaming request");

        let response = self
            .http
            .post(&url)
            .header("Authorization", format!("Bearer {}", self.config.api_key))
            .header("Content-Type", "application/json")
            .header("X-Correlation-ID", &correlation_id)
            .header("Accept", "text/event-stream")
            .json(&request_body)
            .send()
            .await?;

        let status = response.status();
        if !status.is_success() {
            let error_text = response.text().await.unwrap_or_default();
            return Err(ChippClientError::ApiError {
                status: status.as_u16(),
                message: error_text,
            });
        }

        // Create shared session ID that stream will update
        let session_id = Arc::new(Mutex::new(None::<String>));

        // Get the byte stream for true streaming (not buffered!)
        let byte_stream = response.bytes_stream();

        // Create the stream
        let stream = ChippStream::new(Box::pin(byte_stream), session_id);

        Ok(stream)
    }

    /// Send a streaming chat completion and collect the full response.
    ///
    /// This is a convenience method that consumes the entire stream and
    /// updates the session with the captured session ID.
    ///
    /// For true streaming where you process chunks as they arrive,
    /// use [`chat_stream`](Self::chat_stream) instead.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use chipp::{ChippClient, ChippConfig, ChippSession, ChippMessage};
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// # let config = ChippConfig::default();
    /// # let client = ChippClient::new(config)?;
    /// let mut session = ChippSession::new();
    /// let response = client.chat_stream_collect(&mut session, &[ChippMessage::user("Hello")]).await?;
    /// println!("Response: {}", response);
    /// println!("Session ID: {:?}", session.chat_session_id);
    /// # Ok(())
    /// # }
    /// ```
    pub async fn chat_stream_collect(
        &self,
        session: &mut ChippSession,
        messages: &[ChippMessage],
    ) -> Result<String, ChippClientError> {
        let mut stream = self.chat_stream(session, messages).await?;
        let mut full_response = String::new();

        while let Some(chunk) = stream.next().await {
            full_response.push_str(&chunk?);
        }

        // Update session with captured ID after stream completes
        if let Some(id) = stream.session_id().await {
            session.chat_session_id = Some(id);
        }

        Ok(full_response)
    }

    /// Measure the round-trip latency to the Chipp API.
    ///
    /// This method performs a lightweight HEAD request to the chat completions endpoint
    /// and measures the time taken for the request to complete.
    ///
    /// # Returns
    ///
    /// - `Ok(Duration)` containing the round-trip latency if successful
    /// - `Err(ChippClientError::HttpError)` if a network error occurs
    ///
    /// # Use Case
    ///
    /// This is useful for monitoring API performance and deciding whether to route
    /// requests to the Chipp API or fall back to a local LLM based on latency.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use chipp::{ChippClient, ChippConfig};
    /// use std::time::Duration;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let config = ChippConfig::builder()
    ///     .api_key("YOUR_API_KEY_HERE")
    ///     .model("myapp-123")
    ///     .build()?;
    ///
    /// let client = ChippClient::new(config)?;
    ///
    /// // Measure API latency
    /// let latency = client.ping().await?;
    /// println!("API latency: {:?}", latency);
    ///
    /// if latency < Duration::from_secs(2) {
    ///     println!("Low latency, using Chipp API");
    /// } else {
    ///     println!("High latency, falling back to local LLM");
    /// }
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns `ChippClientError::HttpError` if the network request fails due to
    /// timeout, DNS resolution failure, or other connectivity issues.
    pub async fn ping(&self) -> Result<std::time::Duration, ChippClientError> {
        let url = format!("{}/chat/completions", self.config.base_url);

        // Start timer
        let start = std::time::Instant::now();

        // Use HEAD request for minimal overhead
        let _response = self.http.head(&url).send().await?;

        // Calculate elapsed time
        let latency = start.elapsed();

        Ok(latency)
    }
}