llm-optimizer-integrations 0.1.0

External service integrations (GitHub, Slack, Jira, Anthropic)
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
# LLM Auto Optimizer - Integrations

Production-ready integrations with external services for the LLM Auto Optimizer project.

## Features

### Jira Integration

Full-featured Jira REST API client with:

- **Authentication**: OAuth 2.0, Basic Auth, and Personal Access Tokens
- **Issue Management**: Full CRUD operations (Create, Read, Update, Delete)
- **Project Management**: List and query projects
- **Board & Sprint Management**: Agile board and sprint operations
- **JQL Queries**: Advanced search with Jira Query Language
- **Webhook Support**: Event-driven integration with signature verification
- **Rate Limiting**: Automatic rate limiting with configurable limits
- **Retry Logic**: Exponential backoff with configurable retry attempts
- **Error Handling**: Comprehensive error types and context

### Anthropic Claude Integration

Full-featured Claude API client with:

- **Authentication**: API key authentication
- **Multiple Models**: Support for Claude 3.5 Sonnet, Opus, Sonnet, and Haiku
- **Message API**: Send and receive messages with system prompts
- **Streaming**: Real-time streaming responses via Server-Sent Events
- **Token Management**: Token counting, validation, and budget tracking
- **Cost Tracking**: Automatic cost calculation and statistics
- **Rate Limiting**: Per-tier rate limiting
- **Retry Logic**: Automatic retries for transient errors
- **Error Handling**: Detailed error responses and context

## Installation

Add to your `Cargo.toml`:

```toml
[dependencies]
integrations = { path = "crates/integrations" }

# Optional: Enable only specific integrations
integrations = { path = "crates/integrations", default-features = false, features = ["jira"] }
```

## Usage

### Jira Client

#### Basic Authentication

```rust
use integrations::jira::{JiraClient, JiraConfig, JiraAuth};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let config = JiraConfig {
        base_url: "https://your-domain.atlassian.net".to_string(),
        auth: JiraAuth::Basic {
            email: "your-email@example.com".to_string(),
            api_token: "your-api-token".to_string(),
        },
        timeout_secs: 30,
        max_retries: 3,
        rate_limit_per_minute: 100,
    };

    let client = JiraClient::new(config).await?;

    // Get all projects
    let projects = client.get_projects().await?;
    for project in projects {
        println!("{}: {}", project.key, project.name);
    }

    Ok(())
}
```

#### Create an Issue

```rust
use integrations::jira::{CreateIssueRequest, CreateIssueFields, ProjectRef, IssueTypeRef};

let request = CreateIssueRequest {
    fields: CreateIssueFields {
        project: ProjectRef { key: "PROJ".to_string() },
        summary: "New feature request".to_string(),
        description: Some("Detailed description here".to_string()),
        issue_type: IssueTypeRef { name: "Task".to_string() },
        assignee: None,
        priority: None,
        labels: vec!["automation".to_string()],
        components: vec![],
    },
};

let issue = client.create_issue(request).await?;
println!("Created issue: {}", issue.key);
```

#### JQL Search

```rust
use integrations::jira::JqlSearchRequest;

let request = JqlSearchRequest {
    jql: "project = PROJ AND status = Open".to_string(),
    start_at: Some(0),
    max_results: Some(50),
    fields: Some(vec!["summary".to_string(), "status".to_string()]),
};

let results = client.search_issues(request).await?;
println!("Found {} issues", results.total);

for issue in results.issues {
    println!("{}: {}", issue.key, issue.fields.summary);
}
```

#### Webhooks

```rust
use integrations::jira::{WebhookProcessor, WebhookHandler};
use async_trait::async_trait;

struct MyHandler;

#[async_trait]
impl WebhookHandler for MyHandler {
    async fn on_issue_created(&self, event: &WebhookEvent) -> anyhow::Result<()> {
        if let Some(issue) = &event.issue {
            println!("New issue: {} - {}", issue.key, issue.fields.summary);
        }
        Ok(())
    }

    async fn on_issue_updated(&self, event: &WebhookEvent) -> anyhow::Result<()> {
        // Handle update
        Ok(())
    }

    async fn on_issue_deleted(&self, event: &WebhookEvent) -> anyhow::Result<()> {
        // Handle deletion
        Ok(())
    }

    async fn on_other_event(&self, event: &WebhookEvent) -> anyhow::Result<()> {
        // Handle other events
        Ok(())
    }
}

let processor = WebhookProcessor::new(Some("webhook-secret".to_string()));
processor.register_handler(Arc::new(MyHandler)).await;

// Process webhook payload
processor.process_event(payload, Some(signature)).await?;
```

### Anthropic Client

#### Simple Completion

```rust
use integrations::anthropic::{AnthropicClient, AnthropicConfig, ClaudeModel};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let config = AnthropicConfig {
        api_key: "your-api-key".to_string(),
        base_url: "https://api.anthropic.com".to_string(),
        timeout_secs: 60,
        max_retries: 3,
        rate_limit_per_minute: 50,
        api_version: "2023-06-01".to_string(),
    };

    let client = AnthropicClient::new(config).await?;

    let response = client.complete(
        ClaudeModel::Claude3Haiku,
        "What is the capital of France?",
        100,
    ).await?;

    println!("Response: {}", response);

    // Get cost statistics
    let stats = client.get_cost_stats().await;
    println!("Total cost: ${:.4}", stats.total_cost);
    println!("Requests: {}", stats.request_count);

    Ok(())
}
```

#### Advanced Message with System Prompt

```rust
use integrations::anthropic::{MessageRequest, Message, Role, MessageContent, ClaudeModel};

let request = MessageRequest {
    model: ClaudeModel::Claude35Sonnet.as_str().to_string(),
    messages: vec![
        Message {
            role: Role::User,
            content: MessageContent::Text("Explain quantum computing".to_string()),
        },
    ],
    max_tokens: 500,
    system: Some("You are a physics professor explaining complex topics simply.".to_string()),
    temperature: Some(0.7),
    top_p: None,
    top_k: None,
    stop_sequences: None,
    stream: false,
    metadata: None,
};

let response = client.send_message(request).await?;
```

#### Streaming Responses

```rust
use integrations::anthropic::{StreamHandler, StreamCollector, StreamEvent};
use futures::StreamExt;

let stream_handler = StreamHandler::new(
    config_arc.clone(),
    http_client.clone(),
    cost_tracker.clone(),
);

let mut stream = stream_handler.stream_message(request).await?;
let mut collector = StreamCollector::new();

while let Some(event_result) = stream.next().await {
    let event = event_result?;

    match &event {
        StreamEvent::ContentBlockDelta { delta, .. } => {
            if let Delta::TextDelta { text } = delta {
                print!("{}", text); // Print text as it arrives
            }
        }
        _ => {}
    }

    if collector.process_event(event) {
        break; // Final event received
    }
}

let response = collector.to_response()?;
println!("\n\nFinal usage: {:?}", response.usage);
```

#### Token Management

```rust
use integrations::anthropic::{TokenCounter, TokenBudget};

let mut counter = TokenCounter::new();

// Estimate tokens in text
let text = "Hello, world!";
let tokens = counter.count_text(text);
println!("Estimated tokens: {}", tokens);

// Estimate cost for a request
let cost = counter.estimate_cost(&request, ClaudeModel::Claude3Haiku);
println!("Estimated cost: ${:.6}", cost);

// Manage token budget
let mut budget = TokenBudget::new(200_000, 4096)?;

if budget.can_allocate(1000) {
    budget.allocate(1000)?;
    println!("Remaining budget: {}", budget.remaining());
}
```

## Architecture

### Design Principles

1. **Type Safety**: All API types are strongly typed with Serde support
2. **Error Handling**: Comprehensive error types with context
3. **Async First**: Built on Tokio for high-performance async operations
4. **Observability**: Tracing integration for logging and monitoring
5. **Resilience**: Automatic retries, rate limiting, and error recovery
6. **Testing**: Comprehensive unit and integration tests

### Module Structure

```
integrations/
├── src/
│   ├── jira/
│   │   ├── mod.rs          # Module exports
│   │   ├── types.rs        # Type definitions
│   │   ├── auth.rs         # Authentication manager
│   │   ├── client.rs       # Main API client
│   │   └── webhooks.rs     # Webhook processor
│   ├── anthropic/
│   │   ├── mod.rs          # Module exports
│   │   ├── types.rs        # Type definitions
│   │   ├── client.rs       # Main API client
│   │   ├── streaming.rs    # Streaming support
│   │   └── tokens.rs       # Token utilities
│   └── lib.rs              # Library root
├── tests/
│   ├── jira_tests.rs       # Jira integration tests
│   └── anthropic_tests.rs  # Anthropic integration tests
├── Cargo.toml
└── README.md
```

## Configuration

### Environment Variables

```bash
# Jira
export JIRA_BASE_URL="https://your-domain.atlassian.net"
export JIRA_EMAIL="your-email@example.com"
export JIRA_API_TOKEN="your-api-token"

# Anthropic
export ANTHROPIC_API_KEY="your-api-key"
```

### Configuration Files

See `config.example.yaml` in the project root for configuration file examples.

## Error Handling

All clients return `anyhow::Result<T>` for consistent error handling:

```rust
match client.get_issue("PROJ-123").await {
    Ok(issue) => println!("Issue: {}", issue.fields.summary),
    Err(e) => {
        eprintln!("Error: {}", e);
        // Access error chain
        for cause in e.chain() {
            eprintln!("  Caused by: {}", cause);
        }
    }
}
```

## Rate Limiting

Both clients implement rate limiting to prevent API quota exhaustion:

- **Jira**: Configurable per-minute rate limit (default: 100 requests/minute)
- **Anthropic**: Tier-based rate limiting (default: 50 requests/minute)

Rate limiters use the token bucket algorithm with automatic backoff.

## Testing

Run tests:

```bash
# All tests
cargo test -p integrations

# Specific integration
cargo test -p integrations --features jira
cargo test -p integrations --features anthropic

# With logging
RUST_LOG=debug cargo test -p integrations
```

## Performance

Both integrations are optimized for production use:

- **Async I/O**: Non-blocking operations with Tokio
- **Connection Pooling**: Reusable HTTP connections
- **Response Caching**: Configurable caching for frequently accessed data
- **Batch Operations**: Support for bulk operations where available

## Security

- **API Keys**: Never logged or exposed in error messages
- **TLS**: All connections use HTTPS with certificate validation
- **Webhook Signatures**: HMAC-SHA256 signature verification for webhooks
- **Input Validation**: All inputs validated before API calls

## Contributing

See [CONTRIBUTING.md](../../CONTRIBUTING.md) for guidelines.

## License

Apache 2.0 - See [LICENSE](../../LICENSE) for details.

## Support

- **Documentation**: [docs.rs/integrations]https://docs.rs/integrations
- **Issues**: [GitHub Issues]https://github.com/llm-devops/llm-auto-optimizer/issues
- **Discussions**: [GitHub Discussions]https://github.com/llm-devops/llm-auto-optimizer/discussions