wauldo 0.6.0

Official Rust SDK for Wauldo — Verified AI answers from your documents
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
//! Mock HTTP client for deterministic testing

use crate::error::Result;
use crate::http_types::*;

/// Mock HTTP client that returns predefined responses
///
/// Use this for deterministic unit tests without a running server.
///
/// # Example
/// ```rust,no_run
/// use wauldo::MockHttpClient;
/// let mock = MockHttpClient::new();
/// ```
pub struct MockHttpClient {
    chat_response: Option<ChatResponse>,
    models: Option<ModelList>,
    rag_upload_response: Option<RagUploadResponse>,
    rag_query_response: Option<RagQueryResponse>,
    fact_check_response: Option<FactCheckResponse>,
    verify_citation_response: Option<VerifyCitationResponse>,
}

impl MockHttpClient {
    /// Create a new empty mock client with no preconfigured responses
    ///
    /// # Example
    /// ```rust
    /// use wauldo::MockHttpClient;
    /// let mock = MockHttpClient::new();
    /// ```
    pub fn new() -> Self {
        Self {
            chat_response: None,
            models: None,
            rag_upload_response: None,
            rag_query_response: None,
            fact_check_response: None,
            verify_citation_response: None,
        }
    }

    /// Create a mock client pre-loaded with realistic sample data for all endpoints.
    ///
    /// This is the easiest way to explore the SDK without a running server.
    ///
    /// # Example
    /// ```rust
    /// use wauldo::MockHttpClient;
    /// let mock = MockHttpClient::with_defaults();
    /// ```
    pub fn with_defaults() -> Self {
        Self::new()
            .with_chat_response(ChatResponse {
                id: "mock-chat-001".to_string(),
                object: "chat.completion".to_string(),
                created: 1700000000,
                model: "qwen2.5:7b".to_string(),
                choices: vec![ChatChoice {
                    index: 0,
                    message: ChatMessage {
                        role: "assistant".to_string(),
                        content: Some(
                            "Rust's ownership model ensures memory safety without a garbage \
                             collector by enforcing strict rules about how values are owned, \
                             borrowed, and moved at compile time."
                                .to_string(),
                        ),
                        name: None,
                    },
                    finish_reason: Some("stop".to_string()),
                }],
                usage: Usage {
                    prompt_tokens: 24,
                    completion_tokens: 38,
                    total_tokens: 62,
                },
            })
            .with_models(vec![
                Model {
                    id: "qwen2.5:7b".to_string(),
                    object: "model".to_string(),
                    created: 1700000000,
                    owned_by: "wauldo".to_string(),
                },
                Model {
                    id: "llama-4-scout".to_string(),
                    object: "model".to_string(),
                    created: 1700000000,
                    owned_by: "wauldo".to_string(),
                },
            ])
            .with_rag_upload(RagUploadResponse {
                document_id: "doc-abc123".to_string(),
                chunks_count: 5,
            })
            .with_rag_query(RagQueryResponse {
                answer: "Returns are accepted within 60 days of purchase. A valid receipt \
                         is required. [1]"
                    .to_string(),
                sources: vec![
                    RagSource {
                        document_id: "doc-abc123".to_string(),
                        content: "Our refund policy allows returns within 60 days of purchase. \
                                  Customers must provide a valid receipt."
                            .to_string(),
                        score: 0.92,
                        chunk_id: Some("chunk-001".to_string()),
                        metadata: None,
                    },
                    RagSource {
                        document_id: "doc-abc123".to_string(),
                        content: "Refunds are processed within 5 business days after approval."
                            .to_string(),
                        score: 0.78,
                        chunk_id: Some("chunk-002".to_string()),
                        metadata: None,
                    },
                ],
                audit: Some(RagAuditInfo {
                    confidence: 0.92,
                    retrieval_path: "BM25Only".to_string(),
                    sources_evaluated: 5,
                    sources_used: 2,
                    best_score: 0.92,
                    grounded: true,
                    confidence_label: "high".to_string(),
                    model: "qwen2.5:7b".to_string(),
                    latency_ms: 340,
                    candidates_found: Some(12),
                    candidates_after_tenant: Some(12),
                    candidates_after_score: Some(5),
                    query_type: Some("Factual".to_string()),
                }),
                confidence: None,
                grounded: None,
            })
            .with_fact_check(FactCheckResponse {
                verdict: "verified".to_string(),
                action: "allow".to_string(),
                hallucination_rate: 0.0,
                mode: "lexical".to_string(),
                total_claims: 2,
                supported_claims: 2,
                confidence: 0.85,
                claims: vec![
                    ClaimResult {
                        text: "Returns are accepted within 60 days.".to_string(),
                        claim_type: "factual".to_string(),
                        supported: true,
                        confidence: 0.90,
                        confidence_label: "high".to_string(),
                        verdict: "verified".to_string(),
                        action: "allow".to_string(),
                        reason: None,
                        evidence: Some("refund policy allows returns within 60 days".to_string()),
                    },
                    ClaimResult {
                        text: "A valid receipt is required.".to_string(),
                        claim_type: "factual".to_string(),
                        supported: true,
                        confidence: 0.82,
                        confidence_label: "high".to_string(),
                        verdict: "verified".to_string(),
                        action: "allow".to_string(),
                        reason: None,
                        evidence: Some("Customers must provide a valid receipt".to_string()),
                    },
                ],
                mode_warning: None,
                processing_time_ms: 1,
            })
            .with_verify_citation(VerifyCitationResponse {
                citation_ratio: 0.67,
                has_sufficient_citations: true,
                sentence_count: 3,
                citation_count: 2,
                uncited_sentences: vec![
                    "Refunds are processed quickly.".to_string(),
                ],
                citations: Some(vec![
                    CitationDetail {
                        citation: "[1]".to_string(),
                        source_name: "refund_policy.txt".to_string(),
                        is_valid: true,
                    },
                    CitationDetail {
                        citation: "[2]".to_string(),
                        source_name: "refund_policy.txt".to_string(),
                        is_valid: true,
                    },
                ]),
                phantom_count: Some(0),
                processing_time_ms: 1,
            })
    }

    /// Set the chat completion response returned by `chat()`
    ///
    /// # Example
    /// ```rust,no_run
    /// # use wauldo::{MockHttpClient, ChatResponse};
    /// # let resp: ChatResponse = todo!();
    /// let mock = MockHttpClient::new().with_chat_response(resp);
    /// ```
    pub fn with_chat_response(mut self, response: ChatResponse) -> Self {
        self.chat_response = Some(response);
        self
    }

    /// Set the models list returned by `list_models()`
    ///
    /// # Example
    /// ```rust,no_run
    /// # use wauldo::{MockHttpClient, Model};
    /// let mock = MockHttpClient::new().with_models(vec![]);
    /// ```
    pub fn with_models(mut self, models: Vec<Model>) -> Self {
        self.models = Some(ModelList {
            object: "list".to_string(),
            data: models,
        });
        self
    }

    /// Set the RAG upload response returned by `rag_upload()`
    ///
    /// # Example
    /// ```rust,no_run
    /// # use wauldo::{MockHttpClient, RagUploadResponse};
    /// # let resp: RagUploadResponse = todo!();
    /// let mock = MockHttpClient::new().with_rag_upload(resp);
    /// ```
    pub fn with_rag_upload(mut self, response: RagUploadResponse) -> Self {
        self.rag_upload_response = Some(response);
        self
    }

    /// Set the RAG query response returned by `rag_query()`
    ///
    /// # Example
    /// ```rust,no_run
    /// # use wauldo::{MockHttpClient, RagQueryResponse};
    /// # let resp: RagQueryResponse = todo!();
    /// let mock = MockHttpClient::new().with_rag_query(resp);
    /// ```
    pub fn with_rag_query(mut self, response: RagQueryResponse) -> Self {
        self.rag_query_response = Some(response);
        self
    }

    /// Set the fact-check response returned by `fact_check()`
    pub fn with_fact_check(mut self, response: FactCheckResponse) -> Self {
        self.fact_check_response = Some(response);
        self
    }

    /// Set the citation verification response returned by `verify_citation()`
    pub fn with_verify_citation(mut self, response: VerifyCitationResponse) -> Self {
        self.verify_citation_response = Some(response);
        self
    }

    /// List models (mocked) -- returns the value set via `with_models()`
    ///
    /// # Example
    /// ```rust,no_run
    /// # async fn run() -> wauldo::Result<()> {
    /// # use wauldo::MockHttpClient;
    /// let mock = MockHttpClient::new().with_models(vec![]);
    /// let models = mock.list_models().await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn list_models(&self) -> Result<ModelList> {
        self.models
            .clone()
            .ok_or_else(|| crate::error::Error::connection("MockHttpClient: no models configured"))
    }

    /// Chat completion (mocked, non-streaming) -- ignores the request body
    ///
    /// # Example
    /// ```rust,no_run
    /// # async fn run() -> wauldo::Result<()> {
    /// # use wauldo::{MockHttpClient, ChatRequest, ChatMessage};
    /// # let resp: wauldo::ChatResponse = todo!();
    /// let mock = MockHttpClient::new().with_chat_response(resp);
    /// let req = ChatRequest::new("model", vec![ChatMessage::user("hi")]);
    /// let chat = mock.chat(req).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn chat(&self, _request: ChatRequest) -> Result<ChatResponse> {
        self.chat_response.clone().ok_or_else(|| {
            crate::error::Error::connection("MockHttpClient: no chat response configured")
        })
    }

    /// Upload document for RAG (mocked) -- ignores the content
    ///
    /// # Example
    /// ```rust,no_run
    /// # async fn run() -> wauldo::Result<()> {
    /// # use wauldo::MockHttpClient;
    /// # let resp: wauldo::RagUploadResponse = todo!();
    /// let mock = MockHttpClient::new().with_rag_upload(resp);
    /// let upload = mock.rag_upload("text", None).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn rag_upload(
        &self,
        _content: impl Into<String>,
        _filename: Option<String>,
    ) -> Result<RagUploadResponse> {
        self.rag_upload_response.clone().ok_or_else(|| {
            crate::error::Error::connection("MockHttpClient: no rag_upload response configured")
        })
    }

    /// Query RAG (mocked) -- ignores the query string
    ///
    /// # Example
    /// ```rust,no_run
    /// # async fn run() -> wauldo::Result<()> {
    /// # use wauldo::MockHttpClient;
    /// # let resp: wauldo::RagQueryResponse = todo!();
    /// let mock = MockHttpClient::new().with_rag_query(resp);
    /// let result = mock.rag_query("search term", None).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn rag_query(
        &self,
        _query: impl Into<String>,
        _top_k: Option<usize>,
    ) -> Result<RagQueryResponse> {
        self.rag_query_response.clone().ok_or_else(|| {
            crate::error::Error::connection("MockHttpClient: no rag_query response configured")
        })
    }

    /// Query RAG with debug mode (mocked) -- same as rag_query
    pub async fn rag_query_debug(
        &self,
        query: impl Into<String>,
        top_k: Option<usize>,
    ) -> Result<RagQueryResponse> {
        self.rag_query(query, top_k).await
    }

    /// Chat completion with per-request timeout (mocked) -- timeout is ignored
    pub async fn chat_with_timeout(
        &self,
        request: ChatRequest,
        _timeout_ms: Option<u64>,
    ) -> Result<ChatResponse> {
        self.chat(request).await
    }

    /// Upload with per-request timeout (mocked) -- timeout is ignored
    pub async fn rag_upload_with_timeout(
        &self,
        content: impl Into<String>,
        filename: Option<String>,
        _timeout_ms: Option<u64>,
    ) -> Result<RagUploadResponse> {
        self.rag_upload(content, filename).await
    }

    /// Generate embeddings (mocked) -- returns a dummy embedding vector
    pub async fn embeddings(
        &self,
        _input: EmbeddingInput,
        _model: impl Into<String>,
    ) -> Result<EmbeddingResponse> {
        Ok(EmbeddingResponse {
            data: vec![EmbeddingData {
                embedding: vec![0.1, 0.2, 0.3],
                index: 0,
            }],
            model: "mock-model".to_string(),
            usage: EmbeddingUsage {
                prompt_tokens: 5,
                total_tokens: 5,
            },
        })
    }

    /// Execute orchestrator (mocked) -- returns a fixed output
    pub async fn orchestrate(&self, _prompt: impl Into<String>) -> Result<OrchestratorResponse> {
        Ok(OrchestratorResponse {
            final_output: "Mock orchestration result".to_string(),
        })
    }

    /// Execute parallel swarm (mocked) -- returns a fixed output
    pub async fn orchestrate_parallel(
        &self,
        _prompt: impl Into<String>,
    ) -> Result<OrchestratorResponse> {
        Ok(OrchestratorResponse {
            final_output: "Mock parallel result".to_string(),
        })
    }

    // ── Insights & Analytics (mocked) ─────────────────────────────────

    /// GET /v1/insights (mocked) -- returns dummy ROI metrics
    pub async fn get_insights(&self) -> Result<InsightsResponse> {
        Ok(InsightsResponse {
            tig_key: "mock-key".to_string(),
            total_requests: 100,
            intelligence_requests: 80,
            fallback_requests: 20,
            tokens: InsightsTokenStats {
                baseline_total: 50000,
                real_total: 35000,
                saved_total: 15000,
                saved_percent_avg: 30.0,
            },
            cost: InsightsCostStats {
                estimated_usd_saved: 1.25,
            },
        })
    }

    /// GET /v1/analytics (mocked) -- returns dummy usage analytics
    pub async fn get_analytics(&self, _minutes: Option<u64>) -> Result<AnalyticsResponse> {
        Ok(AnalyticsResponse {
            cache: CacheMetrics {
                total_requests: 100,
                cache_hit_rate: 0.45,
                avg_latency_ms: 120.0,
                p95_latency_ms: 350.0,
            },
            tokens: TokenSavings {
                total_baseline: 50000,
                total_real: 35000,
                total_saved: 15000,
                avg_savings_percent: 30.0,
            },
            uptime_secs: 86400,
        })
    }

    /// GET /v1/analytics/traffic (mocked) -- returns dummy traffic summary
    pub async fn get_analytics_traffic(&self) -> Result<TrafficSummary> {
        Ok(TrafficSummary {
            total_requests_today: 250,
            total_tokens_today: 125000,
            top_tenants: vec![TenantTraffic {
                tenant_id: "mock-tenant".to_string(),
                requests_today: 250,
                tokens_used: 125000,
                success_rate: 0.98,
                avg_latency_ms: 150,
            }],
            error_rate: 0.02,
            avg_latency_ms: 150,
            p95_latency_ms: 400,
            uptime_secs: 86400,
        })
    }

    /// Fact-check (mocked) -- returns the value set via `with_fact_check()`
    pub async fn fact_check(&self, _request: FactCheckRequest) -> Result<FactCheckResponse> {
        self.fact_check_response.clone().ok_or_else(|| {
            crate::error::Error::connection("MockHttpClient: no fact_check response configured")
        })
    }

    /// Verify citation coverage (mocked) -- returns the value set via `with_verify_citation()`
    pub async fn verify_citation(
        &self,
        _request: VerifyCitationRequest,
    ) -> Result<VerifyCitationResponse> {
        self.verify_citation_response.clone().ok_or_else(|| {
            crate::error::Error::connection(
                "MockHttpClient: no verify_citation response configured",
            )
        })
    }

    /// Guard (mocked) -- returns a safe result
    pub async fn guard(
        &self,
        _text: &str,
        _source: &str,
        _mode: Option<&str>,
    ) -> Result<GuardResult> {
        Ok(GuardResult {
            safe: true,
            verdict: "verified".to_string(),
            action: "allow".to_string(),
            reason: None,
            confidence: 0.95,
        })
    }

    /// Upload text into RAG and immediately query it (mocked) -- convenience one-shot
    pub async fn rag_ask(
        &self,
        _question: impl Into<String>,
        _text: impl Into<String>,
    ) -> Result<String> {
        let _ = self.rag_upload("mock", None).await?;
        Ok(self.rag_query("mock", None).await?.answer)
    }
}

impl Default for MockHttpClient {
    fn default() -> Self {
        Self::new()
    }
}