context7-cli 0.5.2

Search library documentation from your terminal — zero runtime, bilingual (EN/PT), multi-key rotation
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
// SPDX-License-Identifier: MIT OR Apache-2.0
//! HTTP client, retry logic, and Context7 API calls.
//!
//! This module owns the full lifecycle of API interaction:
//! request building, status handling, and key-rotation retry.
use anyhow::{bail, Context, Result};
use reqwest::StatusCode;
use serde::{Deserialize, Serialize};
use tokio::time::{sleep, Duration};
use tracing::{error, info, warn};

use crate::errors::Context7Error;
use crate::i18n::{t, Message};
use crate::storage::ApiKey;

// ─── CONSTANT ───────────────────────────────────────────────────────────────

const BASE_URL: &str = "https://context7.com/api";

// ─── API RESPONSE MODELS ───────────────────────────────────────────────

/// Represents a single library entry returned by the search endpoint.
#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct LibrarySearchResult {
    /// Unique library identifier (e.g. `/facebook/react`).
    pub id: String,
    /// Human-readable library title.
    pub title: String,
    /// Optional short description of the library.
    pub description: Option<String>,
    /// Relevance/trust score returned by the API, if available.
    pub trust_score: Option<f64>,
    /// Number of GitHub stars, if available. The API returns `-1` when unavailable.
    pub stars: Option<i64>,
    /// Total number of documentation snippets indexed.
    pub total_snippets: Option<u64>,
    /// Total number of tokens indexed.
    pub total_tokens: Option<u64>,
    /// Whether the library has been verified by the Context7 team.
    pub verified: Option<bool>,
    /// Git branch used for indexing.
    pub branch: Option<String>,
    /// Indexing state (e.g. "active", "pending").
    pub state: Option<String>,
}

/// A single code block within a documentation snippet.
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct CodeBlock {
    /// Programming language of the code (e.g. `"rust"`, `"bash"`).
    pub language: String,
    /// Source code content.
    pub code: String,
}

/// Represents a single documentation excerpt returned by the docs endpoint (JSON mode).
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct DocumentationSnippet {
    /// Page title of the source documentation page, if available.
    pub page_title: Option<String>,
    /// Title of this specific code snippet, if available.
    pub code_title: Option<String>,
    /// Description accompanying the code snippet, if available.
    pub code_description: Option<String>,
    /// Primary programming language of the snippet, if available.
    pub code_language: Option<String>,
    /// Number of tokens in this snippet, if available.
    pub code_tokens: Option<u64>,
    /// Unique identifier or source URL of this snippet, if available.
    pub code_id: Option<String>,
    /// List of code blocks contained in this snippet.
    pub code_list: Option<Vec<CodeBlock>>,
    /// Relevance score for the query, if available.
    pub relevance: Option<f64>,
    /// Model used to generate or rank this snippet, if available.
    pub model: Option<String>,
}

/// Top-level response from the library search endpoint (`GET /api/v1/search`).
#[derive(Debug, Deserialize)]
pub struct LibraryListResponse {
    /// List of matching libraries.
    pub results: Vec<LibrarySearchResult>,
}

/// Top-level response from the documentation endpoint (`GET /api/v1/{library_id}`).
#[derive(Debug, Deserialize, Serialize)]
pub struct DocumentationResponse {
    /// Structured documentation snippets (JSON mode).
    pub snippets: Option<Vec<DocumentationSnippet>>,
}

// ─── HTTP CLIENT ─────────────────────────────────────────────────────────────

/// Creates a reusable HTTP client with rustls-TLS, 30 s timeout, and HTTP/2.
///
/// The client should be created once per invocation and shared via `Arc`.
#[must_use]
pub fn create_http_client() -> Result<reqwest::Client> {
    let client = reqwest::Client::builder()
        .use_rustls_tls()
        .timeout(Duration::from_secs(30))
        .user_agent(format!("context7-cli/{}", env!("CARGO_PKG_VERSION")))
        .pool_max_idle_per_host(4)
        .build()
        .with_context(|| t(Message::HttpClientCreateFailure))?;

    Ok(client)
}

// ─── RETRY WITH KEY ROTATION ──────────────────────────────────────────────

/// Executes an API call with retry and key rotation.
///
/// Shuffles a local copy of the provided keys (random draw without replacement)
/// and retries up to `min(keys.len(), 5)` times with exponential backoff:
/// 500 ms → 1 s → 2 s.
///
/// Short-circuits immediately on parse errors (status 200 but JSON failed) —
/// retrying with another key would not help in that case.
///
/// The closure receives an owned `String` (clone of the key) to satisfy the
/// `async move` ownership requirement inside the future.
pub async fn run_with_retry<F, Fut, T>(keys: &[ApiKey], operation: F) -> Result<T>
where
    F: Fn(String) -> Fut,
    Fut: std::future::Future<Output = Result<T, Context7Error>>,
{
    let max_attempts = keys.len().min(5);

    // Shuffles a local copy — avoids modifying the caller's vec
    let mut shuffled_keys = keys.to_vec();
    // Fisher-Yates shuffle with fastrand — replaces rand::SliceRandom
    for i in (1..shuffled_keys.len()).rev() {
        let j = fastrand::usize(..=i);
        shuffled_keys.swap(i, j);
    }

    let delays_ms = [500u64, 1000, 2000];
    let mut failed_auth_keys = 0usize;

    for (attempt, key) in shuffled_keys
        .into_iter()
        .take(max_attempts)
        .enumerate()
    {
        info!("Attempt {}/{}", attempt + 1, max_attempts);

        match operation(key.value().to_string()).await {
            Ok(result) => return Ok(result),

            Err(Context7Error::ApiReturned400 { message }) => {
                // 400 is not transient — abort immediately
                bail!(Context7Error::ApiReturned400 { message });
            }

            Err(Context7Error::LibraryNotFound { library_id }) => {
                // Library doesn't exist — no point trying other keys
                bail!(Context7Error::LibraryNotFound { library_id });
            }

            Err(Context7Error::NoValidApiKey) => {
                failed_auth_keys += 1;
                warn!("Invalid API key (401/403), trying next...");
            }

            Err(Context7Error::InvalidResponse { status: 200 }) => {
                // Parse failure on HTTP 200 — schema mismatch, not a key issue
                // Short-circuit: no point trying other keys
                bail!(Context7Error::InvalidResponse { status: 200 });
            }

            Err(e) => {
                warn!("Failure on attempt {}: {}", attempt + 1, e);

                // Backoff before next attempt (not on the last one)
                if attempt + 1 < max_attempts && attempt < delays_ms.len() {
                    let delay = Duration::from_millis(delays_ms[attempt]);
                    info!("Waiting {}ms before retrying...", delay.as_millis());
                    sleep(delay).await;
                }
            }
        }
    }

    if failed_auth_keys >= max_attempts {
        bail!(Context7Error::NoValidApiKey);
    }

    bail!(Context7Error::RetriesExhausted {
        attempts: max_attempts as u32,
    });
}

// ─── API CALLS ───────────────────────────────────────────────────────────

/// Searches for libraries matching `name` with optional relevance `context_query`.
///
/// Returns `Err(Context7Error)` on HTTP errors to enable retry in `run_with_retry`.
#[must_use]
pub async fn search_library(
    client: &reqwest::Client,
    key: &str,
    name: &str,
    context_query: &str,
) -> Result<LibraryListResponse, Context7Error> {
    let url = format!("{}/v1/search", BASE_URL);

    let response = client
        .get(&url)
        .bearer_auth(key)
        .query(&[("libraryName", name), ("query", context_query)])
        .send()
        .await
        .map_err(|e| {
            error!("Network error while searching library: {}", e);
            Context7Error::InvalidResponse { status: 0 }
        })?;

    handle_response_status(response).await
}

/// Fetches documentation for `library_id` with an optional `query` filter (JSON mode).
///
/// Always requests `type=json`. Use `fetch_documentation_text` for plain-text output.
/// Returns `Err(Context7Error)` on HTTP errors to enable retry in `run_with_retry`.
#[must_use]
pub async fn fetch_documentation(
    client: &reqwest::Client,
    key: &str,
    library_id: &str,
    query: Option<&str>,
) -> Result<DocumentationResponse, Context7Error> {
    // Normalise library_id: strip leading slash if present
    let normalized_id = library_id.trim_start_matches('/');
    let url = format!("{}/v1/{}", BASE_URL, normalized_id);

    let mut builder = client
        .get(&url)
        .bearer_auth(key)
        .query(&[("type", "json")]);

    if let Some(q) = query {
        builder = builder.query(&[("query", q)]);
    }

    let response = builder.send().await.map_err(|e| {
        error!("Network error while fetching documentation: {}", e);
        Context7Error::InvalidResponse { status: 0 }
    })?;

    handle_response_status(response).await.map_err(|e| match e {
        Context7Error::InvalidResponse { status: 404 } => Context7Error::LibraryNotFound {
            library_id: library_id.to_string(),
        },
        other => other,
    })
}

/// Fetches documentation for `library_id` as raw plain text (markdown).
///
/// Uses `type=txt`. Returns the raw response body as a `String`.
/// Returns `Err(Context7Error)` on HTTP errors to enable retry in `run_with_retry`.
#[must_use]
pub async fn fetch_documentation_text(
    client: &reqwest::Client,
    key: &str,
    library_id: &str,
    query: Option<&str>,
) -> Result<String, Context7Error> {
    let normalized_id = library_id.trim_start_matches('/');
    let url = format!("{}/v1/{}", BASE_URL, normalized_id);

    let mut builder = client
        .get(&url)
        .bearer_auth(key)
        .query(&[("type", "txt")]);

    if let Some(q) = query {
        builder = builder.query(&[("query", q)]);
    }

    let response = builder.send().await.map_err(|e| {
        error!("Network error while fetching documentation: {}", e);
        Context7Error::InvalidResponse { status: 0 }
    })?;

    let status = response.status();

    if !status.is_success() {
        match status {
            StatusCode::UNAUTHORIZED | StatusCode::FORBIDDEN => {
                return Err(Context7Error::NoValidApiKey);
            }
            StatusCode::BAD_REQUEST => {
                let message = response
                    .text()
                    .await
                    .unwrap_or_else(|_| "No details".to_string());
                return Err(Context7Error::ApiReturned400 { message });
            }
            StatusCode::NOT_FOUND => {
                return Err(Context7Error::LibraryNotFound {
                    library_id: library_id.to_string(),
                });
            }
            _ => {
                return Err(Context7Error::InvalidResponse {
                    status: status.as_u16(),
                });
            }
        }
    }

    response
        .text()
        .await
        .map_err(|_| Context7Error::InvalidResponse {
            status: status.as_u16(),
        })
}

/// Maps HTTP status codes to typed `Context7Error` variants or deserialises success bodies.
async fn handle_response_status<T: for<'de> Deserialize<'de>>(
    response: reqwest::Response,
) -> Result<T, Context7Error> {
    let status = response.status();

    match status {
        s if s.is_success() => response.json::<T>().await.map_err(|e| {
            error!("Failed to deserialise JSON response: {}", e);
            Context7Error::InvalidResponse {
                status: status.as_u16(),
            }
        }),

        StatusCode::BAD_REQUEST => {
            let message = response
                .text()
                .await
                .unwrap_or_else(|_| "No details".to_string());
            Err(Context7Error::ApiReturned400 { message })
        }

        StatusCode::UNAUTHORIZED | StatusCode::FORBIDDEN => Err(Context7Error::NoValidApiKey),

        StatusCode::TOO_MANY_REQUESTS => {
            warn!("Rate limit reached (429), waiting for retry...");
            Err(Context7Error::InvalidResponse {
                status: status.as_u16(),
            })
        }

        s if s.is_server_error() => {
            warn!(
                "Server error ({}), retrying...",
                status.as_u16()
            );
            Err(Context7Error::InvalidResponse {
                status: status.as_u16(),
            })
        }

        _ => Err(Context7Error::InvalidResponse {
            status: status.as_u16(),
        }),
    }
}

// ─── TESTS ───────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    // ── Struct deserialisation ────────────────────────────────────────────

    #[test]
    fn test_library_search_result_deserialisation() {
        let json = r#"{
            "id": "/facebook/react",
            "title": "React",
            "description": "A JavaScript library for building user interfaces",
            "trustScore": 95.0
        }"#;

        let result: LibrarySearchResult =
            serde_json::from_str(json).expect("Deve deserializar LibrarySearchResult");

        assert_eq!(result.id, "/facebook/react");
        assert_eq!(result.title, "React");
        assert_eq!(
            result.description.as_deref(),
            Some("A JavaScript library for building user interfaces")
        );
        assert!((result.trust_score.unwrap() - 95.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_library_search_result_deserialisation_tolerates_missing_fields() {
        let json = r#"{
            "id": "/minimal/library",
            "title": "MinimalLib"
        }"#;

        let result: LibrarySearchResult =
            serde_json::from_str(json).expect("Deve deserializar mesmo com campos ausentes");

        assert_eq!(result.id, "/minimal/library");
        assert_eq!(result.title, "MinimalLib");
        assert!(result.description.is_none(), "description deve ser None");
        assert!(result.trust_score.is_none(), "trust_score deve ser None");
    }

    #[test]
    fn test_library_search_result_deserialisation_with_optional_fields() {
        let json = r#"{
            "id": "/facebook/react",
            "title": "React",
            "trustScore": 95.0,
            "stars": 228000,
            "totalSnippets": 1500,
            "totalTokens": 250000,
            "verified": true,
            "branch": "main",
            "state": "active"
        }"#;

        let result: LibrarySearchResult =
            serde_json::from_str(json).expect("Deve deserializar com campos opcionais");

        assert_eq!(result.stars, Some(228_000i64));
        assert_eq!(result.total_snippets, Some(1_500));
        assert_eq!(result.total_tokens, Some(250_000));
        assert_eq!(result.verified, Some(true));
        assert_eq!(result.branch.as_deref(), Some("main"));
        assert_eq!(result.state.as_deref(), Some("active"));
    }

    #[test]
    fn test_documentation_snippet_deserialisation() {
        let json = r#"{
            "pageTitle": "React Hooks API",
            "codeTitle": "useEffect example",
            "codeDescription": "The Effect Hook lets you perform side effects.",
            "codeLanguage": "javascript",
            "codeTokens": 68,
            "codeId": "https://github.com/facebook/react/blob/main/packages/react/src/ReactHooks.js",
            "codeList": [
                {"language": "javascript", "code": "useEffect(() => { /* effect */ }, []);"}
            ],
            "relevance": 0.032,
            "model": "gemini-2.5-flash"
        }"#;

        let snippet: DocumentationSnippet =
            serde_json::from_str(json).expect("Deve deserializar DocumentationSnippet");

        assert_eq!(snippet.page_title.as_deref(), Some("React Hooks API"));
        assert_eq!(snippet.code_title.as_deref(), Some("useEffect example"));
        assert_eq!(snippet.code_language.as_deref(), Some("javascript"));
        assert_eq!(snippet.code_tokens, Some(68));
        let list = snippet.code_list.as_ref().expect("Deve ter code_list");
        assert_eq!(list.len(), 1);
        assert_eq!(list[0].language, "javascript");
        assert!((snippet.relevance.unwrap() - 0.032).abs() < f64::EPSILON);
    }

    #[test]
    fn test_documentation_snippet_deserialisation_without_optional_fields() {
        let json = r#"{}"#;

        let snippet: DocumentationSnippet =
            serde_json::from_str(json).expect("Deve deserializar snippet completamente vazio");

        assert!(snippet.page_title.is_none());
        assert!(snippet.code_title.is_none());
        assert!(snippet.code_list.is_none());
    }

    #[test]
    fn test_code_block_deserialisation() {
        let json = r#"{"language": "rust", "code": "fn main() {}"}"#;

        let block: CodeBlock = serde_json::from_str(json).expect("Deve deserializar CodeBlock");

        assert_eq!(block.language, "rust");
        assert_eq!(block.code, "fn main() {}");
    }

    // ── Mock HTTP ─────────────────────────────────────────────────────────────

    #[tokio::test]
    async fn test_search_library_with_mock_server_returns_200() {
        use wiremock::matchers::{method, path};
        use wiremock::{Mock, MockServer, ResponseTemplate};

        let mock_server = MockServer::start().await;

        let json_response = serde_json::json!({
            "results": [
                {
                    "id": "/axum-rs/axum",
                    "title": "axum",
                    "description": "Framework web para Rust",
                    "trustScore": 90.0
                }
            ]
        });

        Mock::given(method("GET"))
            .and(path("/api/v1/search"))
            .respond_with(ResponseTemplate::new(200).set_body_json(&json_response))
            .mount(&mock_server)
            .await;

        let client = reqwest::Client::new();
        let url = format!("{}/api/v1/search", mock_server.uri());

        let response = client
            .get(&url)
            .bearer_auth("ctx7sk-teste-mock")
            .query(&[("libraryName", "axum"), ("query", "axum")])
            .send()
            .await
            .expect("Deve conectar ao mock server");

        assert!(response.status().is_success(), "Status deve ser 200");

        let data: LibraryListResponse = response
            .json()
            .await
            .expect("Deve deserializar response do mock");

        assert_eq!(data.results.len(), 1);
        assert_eq!(data.results[0].id, "/axum-rs/axum");
        assert_eq!(data.results[0].title, "axum");
    }

    #[tokio::test]
    async fn test_fetch_documentation_with_mock_server_returns_200() {
        use wiremock::matchers::{method, path};
        use wiremock::{Mock, MockServer, ResponseTemplate};

        let mock_server = MockServer::start().await;

        let json_response = serde_json::json!({
            "snippets": [
                {
                    "pageTitle": "axum::Router",
                    "codeTitle": "Basic Router setup",
                    "codeDescription": "O Router do Axum permite definir rotas HTTP de forma declarativa.",
                    "codeLanguage": "rust",
                    "codeList": [
                        {"language": "rust", "code": "let app = Router::new().route(\"/\", get(handler));"}
                    ]
                }
            ]
        });

        Mock::given(method("GET"))
            .and(path("/api/v1/axum-rs/axum"))
            .respond_with(ResponseTemplate::new(200).set_body_json(&json_response))
            .mount(&mock_server)
            .await;

        let client = reqwest::Client::new();
        let url = format!("{}/api/v1/axum-rs/axum", mock_server.uri());

        let response = client
            .get(&url)
            .bearer_auth("ctx7sk-teste-docs-mock")
            .query(&[("type", "json"), ("query", "como criar router")])
            .send()
            .await
            .expect("Deve conectar ao mock server");

        assert!(response.status().is_success());

        let data: DocumentationResponse = response
            .json()
            .await
            .expect("Deve deserializar response do mock");

        let snippets = data.snippets.as_ref().expect("Deve ter snippets");
        assert_eq!(snippets.len(), 1);
        let list = snippets[0].code_list.as_ref().expect("Deve ter code_list");
        assert!(list[0].code.contains("Router::new"));
    }

    // ── Keys shuffle ─────────────────────────────────────────────────────

    #[test]
    fn test_keys_shuffle_preserves_all_elements() {
        let original_keys: Vec<String> =
            (0..10).map(|i| format!("ctx7sk-key-{:02}", i)).collect();

        let mut copy_keys = original_keys.clone();
        // Fisher-Yates shuffle with fastrand — replaces rand::SliceRandom
        for i in (1..copy_keys.len()).rev() {
            let j = fastrand::usize(..=i);
            copy_keys.swap(i, j);
        }

        assert_eq!(
            copy_keys.len(),
            original_keys.len(),
            "Shuffle must preserve all elements"
        );

        let mut sorted_original = original_keys.clone();
        let mut sorted_copy = copy_keys.clone();
        sorted_original.sort();
        sorted_copy.sort();
        assert_eq!(
            sorted_original, sorted_copy,
            "Shuffle must contain the same elements, just in different order"
        );
    }

    #[test]
    fn test_max_attempts_limited_to_5() {
        // Verifies that keys.len().min(5) works correctly
        let many_keys: Vec<String> =
            (0..10).map(|i| format!("ctx7sk-key-{:02}", i)).collect();
        let max = many_keys.len().min(5);
        assert_eq!(
            max, 5,
            "Max attempts deve ser limitado a 5 mesmo com 10 keys"
        );

        let few_keys: Vec<String> = vec!["ctx7sk-a".to_string(), "ctx7sk-b".to_string()];
        let max2 = few_keys.len().min(5);
        assert_eq!(max2, 2, "Com 2 keys, max deve ser 2");
    }
}