claude-sdk-rs 1.0.0

Rust SDK for Claude AI with CLI integration - type-safe async API for Claude Code and direct SDK usage
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
/// Enhanced service implementations with comprehensive error handling, rate limiting, and circuit breakers
use std::sync::Arc;
use tokio::sync::RwLock;

use crate::mcp::clients::services::{
    ErrorHandler, ErrorHandlingConfig, HelpScoutApiService, NotionApiService, RateLimitConfig,
    RateLimiter, SlackApiService,
};
use crate::mcp::core::error::WorkflowError;

/// Enhanced HelpScout service with error handling and rate limiting
#[derive(Debug)]
pub struct EnhancedHelpScoutService {
    api_service: HelpScoutApiService,
    error_handler: Arc<RwLock<ErrorHandler>>,
    rate_limiter: Arc<RwLock<RateLimiter>>,
}

impl EnhancedHelpScoutService {
    pub fn new(api_key: String) -> Self {
        let error_config = ErrorHandlingConfig::default();
        let rate_config = RateLimitConfig {
            requests_per_second: 10.0, // HelpScout rate limit
            burst_size: 20,
        };

        Self {
            api_service: HelpScoutApiService::new(api_key),
            error_handler: Arc::new(RwLock::new(ErrorHandler::new(error_config))),
            rate_limiter: Arc::new(RwLock::new(RateLimiter::new(rate_config))),
        }
    }

    pub async fn search_articles(
        &self,
        query: &str,
        collection_id: Option<&str>,
        page: Option<u32>,
        per_page: Option<u32>,
    ) -> Result<crate::mcp::clients::services::helpscout_api::SearchResult, WorkflowError> {
        // Apply rate limiting
        self.rate_limiter.write().await.wait_if_needed().await;

        // Execute with retry logic
        let api = &self.api_service;
        self.error_handler
            .write()
            .await
            .execute_with_retry(
                || async {
                    api.search_articles(query, collection_id, page, per_page)
                        .await
                },
                "HelpScout",
            )
            .await
    }

    pub async fn get_article(
        &self,
        article_id: &str,
    ) -> Result<crate::mcp::clients::services::helpscout_api::Article, WorkflowError> {
        self.rate_limiter.write().await.wait_if_needed().await;

        let api = &self.api_service;
        self.error_handler
            .write()
            .await
            .execute_with_retry(|| async { api.get_article(article_id).await }, "HelpScout")
            .await
    }
}

/// Enhanced Notion service with error handling and rate limiting
#[derive(Debug)]
pub struct EnhancedNotionService {
    api_service: NotionApiService,
    error_handler: Arc<RwLock<ErrorHandler>>,
    rate_limiter: Arc<RwLock<RateLimiter>>,
}

impl EnhancedNotionService {
    pub fn new(api_key: String) -> Self {
        let error_config = ErrorHandlingConfig::default();
        let rate_config = RateLimitConfig {
            requests_per_second: 3.0, // Notion rate limit
            burst_size: 10,
        };

        Self {
            api_service: NotionApiService::new(api_key),
            error_handler: Arc::new(RwLock::new(ErrorHandler::new(error_config))),
            rate_limiter: Arc::new(RwLock::new(RateLimiter::new(rate_config))),
        }
    }

    pub async fn search(
        &self,
        query: &str,
        filter: Option<&str>,
        sort: Option<&str>,
        start_cursor: Option<&str>,
        page_size: Option<u32>,
    ) -> Result<crate::mcp::clients::services::notion_api::SearchResponse, WorkflowError> {
        self.rate_limiter.write().await.wait_if_needed().await;

        let api = &self.api_service;
        self.error_handler
            .write()
            .await
            .execute_with_retry(
                || async {
                    api.search(query, filter, sort, start_cursor, page_size)
                        .await
                },
                "Notion",
            )
            .await
    }

    pub async fn get_page(
        &self,
        page_id: &str,
    ) -> Result<crate::mcp::clients::services::notion_api::Page, WorkflowError> {
        self.rate_limiter.write().await.wait_if_needed().await;

        let api = &self.api_service;
        self.error_handler
            .write()
            .await
            .execute_with_retry(|| async { api.get_page(page_id).await }, "Notion")
            .await
    }

    pub async fn create_page(
        &self,
        parent: crate::mcp::clients::services::notion_api::Parent,
        properties: serde_json::Value,
        children: Option<Vec<crate::mcp::clients::services::notion_api::Block>>,
    ) -> Result<crate::mcp::clients::services::notion_api::Page, WorkflowError> {
        self.rate_limiter.write().await.wait_if_needed().await;

        let api = &self.api_service;
        self.error_handler
            .write()
            .await
            .execute_with_retry(
                || async {
                    api.create_page(parent.clone(), properties.clone(), children.clone())
                        .await
                },
                "Notion",
            )
            .await
    }
}

/// Enhanced Slack service with error handling and rate limiting
#[derive(Debug)]
pub struct EnhancedSlackService {
    api_service: SlackApiService,
    error_handler: Arc<RwLock<ErrorHandler>>,
    rate_limiter: Arc<RwLock<RateLimiter>>,
}

impl EnhancedSlackService {
    pub fn new(bot_token: String) -> Self {
        let mut error_config = ErrorHandlingConfig::default();
        // Slack is more resilient, reduce retry attempts
        error_config.retry_policy.max_attempts = 2;

        let rate_config = RateLimitConfig {
            requests_per_second: 20.0, // Slack Tier 2 rate limit
            burst_size: 100,
        };

        Self {
            api_service: SlackApiService::new(bot_token),
            error_handler: Arc::new(RwLock::new(ErrorHandler::new(error_config))),
            rate_limiter: Arc::new(RwLock::new(RateLimiter::new(rate_config))),
        }
    }

    pub async fn post_message(
        &self,
        channel: &str,
        text: &str,
        thread_ts: Option<&str>,
        blocks: Option<Vec<serde_json::Value>>,
    ) -> Result<crate::mcp::clients::services::slack_api::PostMessageResponse, WorkflowError> {
        self.rate_limiter.write().await.wait_if_needed().await;

        let api = &self.api_service;
        self.error_handler
            .write()
            .await
            .execute_with_retry(
                || async {
                    api.post_message(channel, text, thread_ts, blocks.clone())
                        .await
                },
                "Slack",
            )
            .await
    }

    pub async fn list_channels(
        &self,
        exclude_archived: bool,
        types: Option<&str>,
        limit: Option<u32>,
    ) -> Result<Vec<crate::mcp::clients::services::slack_api::Channel>, WorkflowError> {
        self.rate_limiter.write().await.wait_if_needed().await;

        let api = &self.api_service;
        self.error_handler
            .write()
            .await
            .execute_with_retry(
                || async { api.list_channels(exclude_archived, types, limit).await },
                "Slack",
            )
            .await
    }

    pub async fn search_messages(
        &self,
        query: &str,
        count: Option<u32>,
        page: Option<u32>,
    ) -> Result<Vec<crate::mcp::clients::services::slack_api::Message>, WorkflowError> {
        self.rate_limiter.write().await.wait_if_needed().await;

        let api = &self.api_service;
        self.error_handler
            .write()
            .await
            .execute_with_retry(
                || async { api.search_messages(query, count, page).await },
                "Slack",
            )
            .await
    }
}

/// Service health monitor for tracking the health of all external services
#[derive(Debug)]
pub struct ServiceHealthMonitor {
    helpscout: Option<Arc<EnhancedHelpScoutService>>,
    notion: Option<Arc<EnhancedNotionService>>,
    slack: Option<Arc<EnhancedSlackService>>,
}

impl ServiceHealthMonitor {
    pub fn new() -> Self {
        Self {
            helpscout: None,
            notion: None,
            slack: None,
        }
    }

    pub fn with_helpscout(mut self, service: Arc<EnhancedHelpScoutService>) -> Self {
        self.helpscout = Some(service);
        self
    }

    pub fn with_notion(mut self, service: Arc<EnhancedNotionService>) -> Self {
        self.notion = Some(service);
        self
    }

    pub fn with_slack(mut self, service: Arc<EnhancedSlackService>) -> Self {
        self.slack = Some(service);
        self
    }

    /// Check health of all configured services
    pub async fn check_all_services(&self) -> Vec<ServiceHealthStatus> {
        let mut results = Vec::new();

        // Check HelpScout
        if let Some(ref _helpscout) = self.helpscout {
            let status = self.check_helpscout_health().await;
            results.push(status);
        }

        // Check Notion
        if let Some(ref _notion) = self.notion {
            let status = self.check_notion_health().await;
            results.push(status);
        }

        // Check Slack
        if let Some(ref _slack) = self.slack {
            let status = self.check_slack_health().await;
            results.push(status);
        }

        results
    }

    async fn check_helpscout_health(&self) -> ServiceHealthStatus {
        let start = std::time::Instant::now();

        // Try to list a single article as health check
        let health = if let Some(ref service) = self.helpscout {
            match service
                .api_service
                .list_articles(None, Some(1), Some(1))
                .await
            {
                Ok(_) => ServiceHealth::Healthy,
                Err(e) => ServiceHealth::Unhealthy(format!("Health check failed: {:?}", e)),
            }
        } else {
            ServiceHealth::Unhealthy("Service not configured".to_string())
        };

        ServiceHealthStatus {
            service: "HelpScout".to_string(),
            health,
            response_time: start.elapsed(),
            last_checked: std::time::SystemTime::now(),
        }
    }

    async fn check_notion_health(&self) -> ServiceHealthStatus {
        let start = std::time::Instant::now();

        // Try a minimal search as health check
        let health = if let Some(ref service) = self.notion {
            match service
                .api_service
                .search("", None, None, None, Some(1))
                .await
            {
                Ok(_) => ServiceHealth::Healthy,
                Err(e) => ServiceHealth::Unhealthy(format!("Health check failed: {:?}", e)),
            }
        } else {
            ServiceHealth::Unhealthy("Service not configured".to_string())
        };

        ServiceHealthStatus {
            service: "Notion".to_string(),
            health,
            response_time: start.elapsed(),
            last_checked: std::time::SystemTime::now(),
        }
    }

    async fn check_slack_health(&self) -> ServiceHealthStatus {
        let start = std::time::Instant::now();

        // Try to list channels as health check
        let health = if let Some(ref service) = self.slack {
            match service.api_service.list_channels(true, None, Some(1)).await {
                Ok(_) => ServiceHealth::Healthy,
                Err(e) => ServiceHealth::Unhealthy(format!("Health check failed: {:?}", e)),
            }
        } else {
            ServiceHealth::Unhealthy("Service not configured".to_string())
        };

        ServiceHealthStatus {
            service: "Slack".to_string(),
            health,
            response_time: start.elapsed(),
            last_checked: std::time::SystemTime::now(),
        }
    }
}

/// Service health status
#[derive(Debug, Clone)]
pub struct ServiceHealthStatus {
    pub service: String,
    pub health: ServiceHealth,
    pub response_time: std::time::Duration,
    pub last_checked: std::time::SystemTime,
}

/// Service health state
#[derive(Debug, Clone)]
pub enum ServiceHealth {
    Healthy,
    Degraded(String),
    Unhealthy(String),
}

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

    #[test]
    fn test_enhanced_service_creation() {
        let _ = EnhancedHelpScoutService::new("test-key".to_string());
        let _ = EnhancedNotionService::new("test-key".to_string());
        let _ = EnhancedSlackService::new("test-token".to_string());
    }

    #[test]
    fn test_health_monitor_builder() {
        let monitor = ServiceHealthMonitor::new()
            .with_helpscout(Arc::new(EnhancedHelpScoutService::new("key".to_string())))
            .with_notion(Arc::new(EnhancedNotionService::new("key".to_string())))
            .with_slack(Arc::new(EnhancedSlackService::new("token".to_string())));

        assert!(monitor.helpscout.is_some());
        assert!(monitor.notion.is_some());
        assert!(monitor.slack.is_some());
    }
}