agtrace-sdk 0.7.1

Public SDK for building observability tools on top of agtrace
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
//! Lightweight provider operations without database.
//!
//! The [`Providers`] type provides access to provider operations (parsing, diagnostics)
//! without requiring a full workspace with database. Use this when you only need to:
//!
//! - Parse log files directly
//! - Run diagnostics on providers
//! - Check file parseability
//! - Inspect file contents
//!
//! For session querying and indexing, use [`Client`](crate::Client) instead.
//!
//! # Examples
//!
//! ```no_run
//! use agtrace_sdk::Providers;
//! use std::path::Path;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Auto-detect providers from system paths
//! let providers = Providers::detect()?;
//!
//! // Parse a log file
//! let events = providers.parse_auto(Path::new("/path/to/log.jsonl"))?;
//! println!("Parsed {} events", events.len());
//!
//! // Run diagnostics
//! let results = providers.diagnose()?;
//! for result in &results {
//!     println!("{}: {} files, {} successful",
//!         result.provider_name,
//!         result.total_files,
//!         result.successful);
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ## Custom Provider Configuration
//!
//! ```no_run
//! use agtrace_sdk::Providers;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let providers = Providers::builder()
//!     .provider("claude_code", "/custom/.claude/projects")
//!     .provider("codex", "/custom/.codex/sessions")
//!     .build()?;
//! # Ok(())
//! # }
//! ```

use std::path::{Path, PathBuf};
use std::sync::Arc;

use crate::error::{Error, Result};
use crate::types::{
    AgentEvent, CheckResult, Config, DiagnoseResult, InspectResult, ProviderConfig,
};

/// Lightweight client for provider operations without database.
///
/// This type provides access to provider-level operations (parsing, diagnostics)
/// without requiring a full workspace with database indexing.
///
/// # When to use `Providers` vs `Client`
///
/// | Operation | `Providers` | `Client` |
/// |-----------|-------------|----------|
/// | Parse log files | Yes | Yes (via `.providers()`) |
/// | Run diagnostics | Yes | Yes (via `.system()`) |
/// | Check/inspect files | Yes | Yes (via `.system()`) |
/// | List sessions | No | Yes |
/// | Query sessions | No | Yes |
/// | Watch events | No | Yes |
/// | Index operations | No | Yes |
///
/// Use `Providers` for:
/// - Quick file parsing without workspace setup
/// - Diagnostics on provider log directories
/// - CI/CD validation of log files
/// - Tools that only need read-only file access
///
/// Use `Client` for:
/// - Session browsing and querying
/// - Real-time event monitoring
/// - Full workspace operations
#[derive(Clone)]
pub struct Providers {
    config: Arc<Config>,
    /// (provider_name, log_root)
    provider_configs: Vec<(String, PathBuf)>,
}

impl Providers {
    /// Create with auto-detected providers from system paths.
    ///
    /// Scans default log directories for each supported provider
    /// (Claude Code, Codex, Gemini) and enables those that exist.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use agtrace_sdk::Providers;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let providers = Providers::detect()?;
    /// println!("Detected {} providers", providers.list().len());
    /// # Ok(())
    /// # }
    /// ```
    pub fn detect() -> Result<Self> {
        let config = Config::detect_providers().map_err(Error::Runtime)?;
        Ok(Self::with_config(config))
    }

    /// Create with explicit configuration.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use agtrace_sdk::{Providers, types::Config};
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let config = Config::detect_providers()?;
    /// let providers = Providers::with_config(config);
    /// # Ok(())
    /// # }
    /// ```
    pub fn with_config(config: Config) -> Self {
        let provider_configs: Vec<(String, PathBuf)> = config
            .enabled_providers()
            .into_iter()
            .map(|(name, cfg)| (name.clone(), cfg.log_root.clone()))
            .collect();

        Self {
            config: Arc::new(config),
            provider_configs,
        }
    }

    /// Create a builder for fine-grained configuration.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use agtrace_sdk::Providers;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let providers = Providers::builder()
    ///     .provider("claude_code", "/custom/.claude/projects")
    ///     .build()?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn builder() -> ProvidersBuilder {
        ProvidersBuilder::new()
    }

    // =========================================================================
    // Operations
    // =========================================================================

    /// Parse a log file with auto-detected provider.
    ///
    /// Automatically detects the appropriate provider based on file path
    /// and parses it into events.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use agtrace_sdk::Providers;
    /// use std::path::Path;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let providers = Providers::detect()?;
    /// let events = providers.parse_auto(Path::new("/path/to/session.jsonl"))?;
    /// for event in &events {
    ///     println!("{:?}", event.payload);
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub fn parse_auto(&self, path: &Path) -> Result<Vec<AgentEvent>> {
        let path_str = path
            .to_str()
            .ok_or_else(|| Error::InvalidInput("Path contains invalid UTF-8".to_string()))?;

        let adapter = agtrace_providers::detect_adapter_from_path(path_str)
            .map_err(|_| Error::NotFound("No suitable provider detected for file".to_string()))?;

        adapter
            .parser
            .parse_file(path)
            .map_err(|e| Error::InvalidInput(format!("Parse error: {}", e)))
    }

    /// Parse a log file with a specific provider.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use agtrace_sdk::Providers;
    /// use std::path::Path;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let providers = Providers::detect()?;
    /// let events = providers.parse_file(Path::new("/path/to/log.jsonl"), "claude_code")?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn parse_file(&self, path: &Path, provider_name: &str) -> Result<Vec<AgentEvent>> {
        let adapter = agtrace_providers::create_adapter(provider_name)
            .map_err(|_| Error::NotFound(format!("Unknown provider: {}", provider_name)))?;

        adapter
            .parser
            .parse_file(path)
            .map_err(|e| Error::InvalidInput(format!("Parse error: {}", e)))
    }

    /// Run diagnostics on all configured providers.
    ///
    /// Scans each provider's log directory and reports parsing statistics.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use agtrace_sdk::Providers;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let providers = Providers::detect()?;
    /// let results = providers.diagnose()?;
    ///
    /// for result in &results {
    ///     let success_rate = if result.total_files > 0 {
    ///         (result.successful as f64 / result.total_files as f64) * 100.0
    ///     } else {
    ///         100.0
    ///     };
    ///     println!("{}: {:.1}% success ({}/{})",
    ///         result.provider_name,
    ///         success_rate,
    ///         result.successful,
    ///         result.total_files);
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub fn diagnose(&self) -> Result<Vec<DiagnoseResult>> {
        let providers: Vec<_> = self
            .provider_configs
            .iter()
            .filter_map(|(name, path)| {
                agtrace_providers::create_adapter(name)
                    .ok()
                    .map(|adapter| (adapter, path.clone()))
            })
            .collect();

        agtrace_runtime::DoctorService::diagnose_all(&providers).map_err(Error::Runtime)
    }

    /// Check if a file can be parsed.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use agtrace_sdk::Providers;
    /// use std::path::Path;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let providers = Providers::detect()?;
    /// let result = providers.check_file(Path::new("/path/to/log.jsonl"), None)?;
    /// println!("Status: {:?}", result.status);
    /// # Ok(())
    /// # }
    /// ```
    pub fn check_file(&self, path: &Path, provider: Option<&str>) -> Result<CheckResult> {
        let path_str = path
            .to_str()
            .ok_or_else(|| Error::InvalidInput("Path contains invalid UTF-8".to_string()))?;

        let (adapter, provider_name) = if let Some(name) = provider {
            let adapter = agtrace_providers::create_adapter(name)
                .map_err(|_| Error::NotFound(format!("Provider: {}", name)))?;
            (adapter, name.to_string())
        } else {
            let adapter = agtrace_providers::detect_adapter_from_path(path_str)
                .map_err(|_| Error::NotFound("No suitable provider detected".to_string()))?;
            let name = format!("{} (auto-detected)", adapter.id());
            (adapter, name)
        };

        agtrace_runtime::DoctorService::check_file(path_str, &adapter, &provider_name)
            .map_err(Error::Runtime)
    }

    /// Inspect raw file contents.
    ///
    /// Returns the first N lines of the file, optionally parsed as JSON.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use agtrace_sdk::Providers;
    /// use std::path::Path;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let result = Providers::inspect_file(Path::new("/path/to/log.jsonl"), 10, true)?;
    /// println!("Showing {} of {} lines", result.shown_lines, result.total_lines);
    /// # Ok(())
    /// # }
    /// ```
    pub fn inspect_file(path: &Path, lines: usize, json_format: bool) -> Result<InspectResult> {
        let path_str = path
            .to_str()
            .ok_or_else(|| Error::InvalidInput("Path contains invalid UTF-8".to_string()))?;

        agtrace_runtime::DoctorService::inspect_file(path_str, lines, json_format)
            .map_err(Error::Runtime)
    }

    /// List configured providers.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use agtrace_sdk::Providers;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let providers = Providers::detect()?;
    /// for (name, config) in providers.list() {
    ///     println!("{}: {:?}", name, config.log_root);
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub fn list(&self) -> Vec<(&String, &ProviderConfig)> {
        self.config.enabled_providers()
    }

    /// Get current configuration.
    pub fn config(&self) -> &Config {
        &self.config
    }

    /// Get provider configurations as (name, log_root) pairs.
    pub fn provider_configs(&self) -> &[(String, PathBuf)] {
        &self.provider_configs
    }
}

// =============================================================================
// ProvidersBuilder
// =============================================================================

/// Builder for configuring [`Providers`].
///
/// Allows programmatic configuration of providers without relying on
/// filesystem detection or TOML files.
///
/// # Examples
///
/// ```no_run
/// use agtrace_sdk::Providers;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // Start from auto-detected providers and add custom ones
/// let providers = Providers::builder()
///     .auto_detect()
///     .provider("claude_code", "/custom/claude/path")
///     .build()?;
///
/// // Or configure entirely manually
/// let providers = Providers::builder()
///     .provider("claude_code", "/path/to/.claude/projects")
///     .provider("codex", "/path/to/.codex/sessions")
///     .build()?;
/// # Ok(())
/// # }
/// ```
#[derive(Default)]
pub struct ProvidersBuilder {
    config: Option<Config>,
    providers: Vec<(String, PathBuf)>,
}

impl ProvidersBuilder {
    /// Create a new builder with no providers configured.
    pub fn new() -> Self {
        Self::default()
    }

    /// Load configuration from a TOML file.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use agtrace_sdk::Providers;
    /// use std::path::Path;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let providers = Providers::builder()
    ///     .config_file(Path::new("/path/to/config.toml"))?
    ///     .build()?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn config_file(mut self, path: impl AsRef<Path>) -> Result<Self> {
        let config = Config::load_from(&path.as_ref().to_path_buf()).map_err(Error::Runtime)?;
        self.config = Some(config);
        Ok(self)
    }

    /// Use explicit configuration.
    pub fn config(mut self, config: Config) -> Self {
        self.config = Some(config);
        self
    }

    /// Add a provider with custom log root.
    ///
    /// This overrides any provider with the same name from config file
    /// or auto-detection.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use agtrace_sdk::Providers;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let providers = Providers::builder()
    ///     .provider("claude_code", "/custom/.claude/projects")
    ///     .build()?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn provider(mut self, name: &str, log_root: impl Into<PathBuf>) -> Self {
        self.providers.push((name.to_string(), log_root.into()));
        self
    }

    /// Enable auto-detection of providers.
    ///
    /// Scans default log directories for each supported provider
    /// and enables those that exist.
    pub fn auto_detect(mut self) -> Self {
        match Config::detect_providers() {
            Ok(config) => {
                self.config = Some(config);
            }
            Err(_) => {
                // Silently ignore detection errors
            }
        }
        self
    }

    /// Build the `Providers` instance.
    pub fn build(self) -> Result<Providers> {
        let mut config = self.config.unwrap_or_default();

        // Apply manual provider overrides
        for (name, log_root) in self.providers {
            config.set_provider(
                name,
                ProviderConfig {
                    enabled: true,
                    log_root,
                    context_window_override: None,
                },
            );
        }

        Ok(Providers::with_config(config))
    }
}

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

    #[test]
    fn test_builder_creates_empty_providers() {
        let providers = Providers::builder().build().unwrap();
        assert!(providers.list().is_empty());
    }

    #[test]
    fn test_builder_with_manual_provider() {
        let providers = Providers::builder()
            .provider("claude_code", "/tmp/test")
            .build()
            .unwrap();

        assert_eq!(providers.list().len(), 1);
        let (name, config) = &providers.list()[0];
        assert_eq!(*name, "claude_code");
        assert_eq!(config.log_root, PathBuf::from("/tmp/test"));
    }

    #[test]
    fn test_with_config() {
        let mut config = Config::default();
        config.set_provider(
            "test_provider".to_string(),
            ProviderConfig {
                enabled: true,
                log_root: PathBuf::from("/test/path"),
                context_window_override: None,
            },
        );

        let providers = Providers::with_config(config);
        assert_eq!(providers.list().len(), 1);
    }
}