rustchain-community 1.0.0

Open-source AI agent framework with core functionality and plugin system
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
// Web Search Tools Implementation
use crate::core::error::{RustChainError, ToolError};
use crate::core::tools::{Tool, ToolCapability, ToolResult};
use async_trait::async_trait;
use reqwest;
use serde::{Deserialize, Serialize};
use tracing::{debug, info};

// Google Search Tool
pub struct GoogleSearchTool {
    api_key: String,
    search_engine_id: String,
    client: reqwest::Client,
}

impl GoogleSearchTool {
    pub fn new(api_key: String, search_engine_id: String) -> Self {
        Self {
            api_key,
            search_engine_id,
            client: reqwest::Client::new(),
        }
    }

    async fn search_google(&self, query: &str, num_results: u32) -> Result<Vec<SearchResult>, RustChainError> {
        let url = format!(
            "https://www.googleapis.com/customsearch/v1?key={}&cx={}&q={}&num={}",
            self.api_key,
            self.search_engine_id,
            urlencoding::encode(query),
            num_results.min(10)
        );

        debug!("Making Google search request: {}", url);

        let response = self
            .client
            .get(&url)
            .send()
            .await
            .map_err(|e| RustChainError::Tool(ToolError::ExecutionFailed {
                tool_name: "google_search".to_string(),
                reason: format!("Request failed: {}", e),
            }))?;

        if !response.status().is_success() {
            let error_text = response.text().await.unwrap_or_default();
            return Err(RustChainError::Tool(ToolError::ExecutionFailed {
                tool_name: "google_search".to_string(),
                reason: format!("API error: {}", error_text),
            }));
        }

        let google_response: GoogleSearchResponse = response
            .json()
            .await
            .map_err(|e| RustChainError::Tool(ToolError::ExecutionFailed {
                tool_name: "google_search".to_string(),
                reason: format!("Failed to parse response: {}", e),
            }))?;

        let results = google_response
            .items
            .unwrap_or_default()
            .into_iter()
            .map(|item| SearchResult {
                title: item.title,
                link: item.link,
                snippet: item.snippet.unwrap_or_default(),
                source: "google".to_string(),
            })
            .collect();

        Ok(results)
    }
}

#[async_trait]
impl Tool for GoogleSearchTool {
    fn name(&self) -> &'static str {
        "google_search"
    }

    fn capabilities(&self) -> Vec<ToolCapability> {
        vec![ToolCapability::NetworkAccess, ToolCapability::Basic]
    }

    async fn invoke(&self, input: &str) -> Result<ToolResult, RustChainError> {
        let search_params: SearchParams = serde_json::from_str(input)
            .map_err(|e| RustChainError::Tool(ToolError::InvalidParameters {
                tool_name: "google_search".to_string(),
                details: format!("Invalid search parameters: {}", e),
            }))?;

        let results = self.search_google(&search_params.query, search_params.num_results.unwrap_or(5)).await?;

        info!("Google search completed: {} results for query '{}'", results.len(), search_params.query);

        let response = SearchResponse {
            query: search_params.query,
            results,
            source: "google".to_string(),
        };

        Ok(ToolResult::StructuredJson(serde_json::to_value(response)?))
    }
}

// DuckDuckGo Search Tool
pub struct DuckDuckGoSearchTool {
    client: reqwest::Client,
}

impl DuckDuckGoSearchTool {
    pub fn new() -> Self {
        Self {
            client: reqwest::Client::new(),
        }
    }

    async fn search_duckduckgo(&self, query: &str, num_results: u32) -> Result<Vec<SearchResult>, RustChainError> {
        // DuckDuckGo Instant Answer API - free but limited
        let url = format!(
            "https://api.duckduckgo.com/?q={}&format=json&no_redirect=1&no_html=1",
            urlencoding::encode(query)
        );

        debug!("Making DuckDuckGo search request: {}", url);

        let response = self
            .client
            .get(&url)
            .send()
            .await
            .map_err(|e| RustChainError::Tool(ToolError::ExecutionFailed {
                tool_name: "duckduckgo_search".to_string(),
                reason: format!("Request failed: {}", e),
            }))?;

        if !response.status().is_success() {
            let error_text = response.text().await.unwrap_or_default();
            return Err(RustChainError::Tool(ToolError::ExecutionFailed {
                tool_name: "duckduckgo_search".to_string(),
                reason: format!("API error: {}", error_text),
            }));
        }

        let ddg_response: DuckDuckGoResponse = response
            .json()
            .await
            .map_err(|e| RustChainError::Tool(ToolError::ExecutionFailed {
                tool_name: "duckduckgo_search".to_string(),
                reason: format!("Failed to parse response: {}", e),
            }))?;

        let mut results = Vec::new();

        // Add instant answer if available
        if !ddg_response.instant_answer.is_empty() {
            results.push(SearchResult {
                title: "Instant Answer".to_string(),
                link: ddg_response.instant_answer_url.unwrap_or_default(),
                snippet: ddg_response.instant_answer,
                source: "duckduckgo".to_string(),
            });
        }

        // Add abstract if available
        if !ddg_response.abstract_text.is_empty() {
            results.push(SearchResult {
                title: ddg_response.heading.clone(),
                link: ddg_response.abstract_url.unwrap_or_default(),
                snippet: ddg_response.abstract_text,
                source: "duckduckgo".to_string(),
            });
        }

        // Add related topics
        for topic in ddg_response.related_topics.into_iter().take(num_results as usize) {
            if let Some(first_url) = topic.first_url {
                results.push(SearchResult {
                    title: topic.text.split(" - ").next().unwrap_or(&topic.text).to_string(),
                    link: first_url,
                    snippet: topic.text,
                    source: "duckduckgo".to_string(),
                });
            }
        }

        Ok(results)
    }
}

#[async_trait]
impl Tool for DuckDuckGoSearchTool {
    fn name(&self) -> &'static str {
        "duckduckgo_search"
    }

    fn capabilities(&self) -> Vec<ToolCapability> {
        vec![ToolCapability::NetworkAccess, ToolCapability::Basic]
    }

    async fn invoke(&self, input: &str) -> Result<ToolResult, RustChainError> {
        let search_params: SearchParams = serde_json::from_str(input)
            .map_err(|e| RustChainError::Tool(ToolError::InvalidParameters {
                tool_name: "duckduckgo_search".to_string(),
                details: format!("Invalid search parameters: {}", e),
            }))?;

        let results = self.search_duckduckgo(&search_params.query, search_params.num_results.unwrap_or(5)).await?;

        info!("DuckDuckGo search completed: {} results for query '{}'", results.len(), search_params.query);

        let response = SearchResponse {
            query: search_params.query,
            results,
            source: "duckduckgo".to_string(),
        };

        Ok(ToolResult::StructuredJson(serde_json::to_value(response)?))
    }
}

// Brave Search Tool
pub struct BraveSearchTool {
    api_key: String,
    client: reqwest::Client,
}

impl BraveSearchTool {
    pub fn new(api_key: String) -> Self {
        Self {
            api_key,
            client: reqwest::Client::new(),
        }
    }

    async fn search_brave(&self, query: &str, num_results: u32) -> Result<Vec<SearchResult>, RustChainError> {
        let url = format!(
            "https://api.search.brave.com/res/v1/web/search?q={}&count={}",
            urlencoding::encode(query),
            num_results.min(20)
        );

        debug!("Making Brave search request: {}", url);

        let response = self
            .client
            .get(&url)
            .header("X-Subscription-Token", &self.api_key)
            .send()
            .await
            .map_err(|e| RustChainError::Tool(ToolError::ExecutionFailed {
                tool_name: "brave_search".to_string(),
                reason: format!("Request failed: {}", e),
            }))?;

        if !response.status().is_success() {
            let error_text = response.text().await.unwrap_or_default();
            return Err(RustChainError::Tool(ToolError::ExecutionFailed {
                tool_name: "brave_search".to_string(),
                reason: format!("API error: {}", error_text),
            }));
        }

        let brave_response: BraveSearchResponse = response
            .json()
            .await
            .map_err(|e| RustChainError::Tool(ToolError::ExecutionFailed {
                tool_name: "brave_search".to_string(),
                reason: format!("Failed to parse response: {}", e),
            }))?;

        let results = brave_response
            .web
            .results
            .into_iter()
            .map(|item| SearchResult {
                title: item.title,
                link: item.url,
                snippet: item.description.unwrap_or_default(),
                source: "brave".to_string(),
            })
            .collect();

        Ok(results)
    }
}

#[async_trait]
impl Tool for BraveSearchTool {
    fn name(&self) -> &'static str {
        "brave_search"
    }

    fn capabilities(&self) -> Vec<ToolCapability> {
        vec![ToolCapability::NetworkAccess, ToolCapability::Basic]
    }

    async fn invoke(&self, input: &str) -> Result<ToolResult, RustChainError> {
        let search_params: SearchParams = serde_json::from_str(input)
            .map_err(|e| RustChainError::Tool(ToolError::InvalidParameters {
                tool_name: "brave_search".to_string(),
                details: format!("Invalid search parameters: {}", e),
            }))?;

        let results = self.search_brave(&search_params.query, search_params.num_results.unwrap_or(5)).await?;

        info!("Brave search completed: {} results for query '{}'", results.len(), search_params.query);

        let response = SearchResponse {
            query: search_params.query,
            results,
            source: "brave".to_string(),
        };

        Ok(ToolResult::StructuredJson(serde_json::to_value(response)?))
    }
}

// Common data structures
#[derive(Debug, Serialize, Deserialize)]
pub struct SearchParams {
    pub query: String,
    pub num_results: Option<u32>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchResult {
    pub title: String,
    pub link: String,
    pub snippet: String,
    pub source: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct SearchResponse {
    pub query: String,
    pub results: Vec<SearchResult>,
    pub source: String,
}

// Google Search API response structures
#[derive(Debug, Deserialize)]
struct GoogleSearchResponse {
    items: Option<Vec<GoogleSearchItem>>,
}

#[derive(Debug, Deserialize)]
struct GoogleSearchItem {
    title: String,
    link: String,
    snippet: Option<String>,
}

// DuckDuckGo API response structures
#[derive(Debug, Deserialize)]
struct DuckDuckGoResponse {
    #[serde(rename = "Heading")]
    heading: String,
    #[serde(rename = "AbstractText")]
    abstract_text: String,
    #[serde(rename = "AbstractURL")]
    abstract_url: Option<String>,
    #[serde(rename = "InstantAnswer")]
    instant_answer: String,
    #[serde(rename = "InstantAnswerURL")]
    instant_answer_url: Option<String>,
    #[serde(rename = "RelatedTopics")]
    related_topics: Vec<DuckDuckGoRelatedTopic>,
}

#[derive(Debug, Deserialize)]
struct DuckDuckGoRelatedTopic {
    #[serde(rename = "Text")]
    text: String,
    #[serde(rename = "FirstURL")]
    first_url: Option<String>,
}

// Brave Search API response structures
#[derive(Debug, Deserialize)]
struct BraveSearchResponse {
    web: BraveWebResults,
}

#[derive(Debug, Deserialize)]
struct BraveWebResults {
    results: Vec<BraveSearchResult>,
}

#[derive(Debug, Deserialize)]
struct BraveSearchResult {
    title: String,
    url: String,
    description: Option<String>,
}

// Tool registry helper function
pub fn register_web_search_tools(registry: &mut crate::core::tools::ToolRegistry) {
    // Register Google Search if API key is available
    if let (Ok(api_key), Ok(search_engine_id)) = (
        std::env::var("GOOGLE_API_KEY"),
        std::env::var("GOOGLE_SEARCH_ENGINE_ID"),
    ) {
        let google_tool = GoogleSearchTool::new(api_key, search_engine_id);
        registry.register(Box::new(google_tool));
        info!("Registered Google Search tool");
    }

    // Register DuckDuckGo Search (always available, no API key required)
    let duckduckgo_tool = DuckDuckGoSearchTool::new();
    registry.register(Box::new(duckduckgo_tool));
    info!("Registered DuckDuckGo Search tool");

    // Register Brave Search if API key is available
    if let Ok(api_key) = std::env::var("BRAVE_SEARCH_API_KEY") {
        let brave_tool = BraveSearchTool::new(api_key);
        registry.register(Box::new(brave_tool));
        info!("Registered Brave Search tool");
    }
}

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

    #[test]
    fn test_search_params_serialization() {
        let params = SearchParams {
            query: "rust programming".to_string(),
            num_results: Some(10),
        };
        
        let json = serde_json::to_string(&params).unwrap();
        let deserialized: SearchParams = serde_json::from_str(&json).unwrap();
        
        assert_eq!(params.query, deserialized.query);
        assert_eq!(params.num_results, deserialized.num_results);
    }

    #[test]
    fn test_search_result_serialization() {
        let result = SearchResult {
            title: "Test Title".to_string(),
            link: "https://example.com".to_string(),
            snippet: "Test snippet".to_string(),
            source: "test".to_string(),
        };
        
        let json = serde_json::to_value(&result).unwrap();
        assert_eq!(json["title"], "Test Title");
        assert_eq!(json["link"], "https://example.com");
        assert_eq!(json["snippet"], "Test snippet");
        assert_eq!(json["source"], "test");
    }

    #[test]
    fn test_search_response_serialization() {
        let response = SearchResponse {
            query: "test query".to_string(),
            results: vec![SearchResult {
                title: "Test Title".to_string(),
                link: "https://example.com".to_string(),
                snippet: "Test snippet".to_string(),
                source: "test".to_string(),
            }],
            source: "test".to_string(),
        };
        
        let json = serde_json::to_value(&response).unwrap();
        assert_eq!(json["query"], "test query");
        assert_eq!(json["source"], "test");
        assert!(json["results"].is_array());
        assert_eq!(json["results"][0]["title"], "Test Title");
    }

    #[test]
    fn test_google_search_tool_name() {
        let tool = GoogleSearchTool::new("test-key".to_string(), "test-id".to_string());
        assert_eq!(tool.name(), "google_search");
    }

    #[test]
    fn test_google_search_tool_capabilities() {
        let tool = GoogleSearchTool::new("test-key".to_string(), "test-id".to_string());
        let capabilities = tool.capabilities();
        assert!(capabilities.contains(&ToolCapability::NetworkAccess));
        assert!(capabilities.contains(&ToolCapability::Basic));
    }

    #[test]
    fn test_duckduckgo_search_tool_name() {
        let tool = DuckDuckGoSearchTool::new();
        assert_eq!(tool.name(), "duckduckgo_search");
    }

    #[test]
    fn test_duckduckgo_search_tool_capabilities() {
        let tool = DuckDuckGoSearchTool::new();
        let capabilities = tool.capabilities();
        assert!(capabilities.contains(&ToolCapability::NetworkAccess));
        assert!(capabilities.contains(&ToolCapability::Basic));
    }

    #[test]
    fn test_brave_search_tool_name() {
        let tool = BraveSearchTool::new("test-key".to_string());
        assert_eq!(tool.name(), "brave_search");
    }

    #[test]
    fn test_brave_search_tool_capabilities() {
        let tool = BraveSearchTool::new("test-key".to_string());
        let capabilities = tool.capabilities();
        assert!(capabilities.contains(&ToolCapability::NetworkAccess));
        assert!(capabilities.contains(&ToolCapability::Basic));
    }

    #[tokio::test]
    async fn test_invalid_search_params() {
        let tool = DuckDuckGoSearchTool::new();
        let result = tool.invoke("invalid json").await;
        assert!(result.is_err());
        assert!(format!("{:?}", result.unwrap_err()).contains("Invalid"));
    }

    #[tokio::test]
    async fn test_valid_search_params() {
        let tool = DuckDuckGoSearchTool::new();
        let params = json!({
            "query": "test query",
            "num_results": 3
        });
        
        // This test will make an actual network request to DuckDuckGo
        // In a real test environment, you might want to mock this
        let result = tool.invoke(&params.to_string()).await;
        // For now, we just check that the parameters are parsed correctly
        // In a production environment, you'd want to mock the HTTP client
        if result.is_err() {
            // Network errors are acceptable in test environment
            println!("Network test skipped: {:?}", result.unwrap_err());
        }
    }
}