cc-agent-sdk 0.1.7

claude agent sdk
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
//! # Structured Logging for Agent SDK
//!
//! This module provides structured logging capabilities with level-based filtering,
//! context support, and integration with the tracing ecosystem.
//!
//! ## Features
//!
//! - **Structured Logging**: JSON-formatted logs with consistent field names
//! - **Context Support**: Attach context to log messages automatically
//! - **Level Filtering**: Support for trace, debug, info, warn, error levels
//! - **Tracing Integration**: Compatible with the `tracing` ecosystem
//! - **Performance**: Low-overhead logging with lazy evaluation
//!
//! ## Example
//!
//! ```no_run
//! use claude_agent_sdk::observability::Logger;
//!
//! let logger = Logger::new("MyAgent");
//! logger.info("Starting agent execution", &[("task_id", "123")]);
//! logger.error("Failed to execute", Some(&anyhow::anyhow!("Connection error")));
//! ```

use std::fmt;
use std::time::{SystemTime, UNIX_EPOCH};

/// Log level
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum LogLevel {
    Trace = 0,
    Debug = 1,
    Info = 2,
    Warn = 3,
    Error = 4,
}

impl fmt::Display for LogLevel {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            LogLevel::Trace => write!(f, "TRACE"),
            LogLevel::Debug => write!(f, "DEBUG"),
            LogLevel::Info => write!(f, "INFO"),
            LogLevel::Warn => write!(f, "WARN"),
            LogLevel::Error => write!(f, "ERROR"),
        }
    }
}

impl std::str::FromStr for LogLevel {
    type Err = String;

    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
        match s.to_uppercase().as_str() {
            "TRACE" => Ok(LogLevel::Trace),
            "DEBUG" => Ok(LogLevel::Debug),
            "INFO" => Ok(LogLevel::Info),
            "WARN" => Ok(LogLevel::Warn),
            "ERROR" => Ok(LogLevel::Error),
            _ => Err(format!("Invalid log level: {}", s)),
        }
    }
}

/// Structured log entry
#[derive(Debug, Clone)]
pub struct LogEntry {
    /// Timestamp (milliseconds since epoch)
    pub timestamp: u64,

    /// Log level
    pub level: LogLevel,

    /// Logger context/name
    pub context: String,

    /// Log message
    pub message: String,

    /// Additional key-value pairs
    pub metadata: Vec<(String, String)>,

    /// Optional error details
    pub error: Option<String>,
}

impl LogEntry {
    /// Create a new log entry
    pub fn new(level: LogLevel, context: impl Into<String>, message: impl Into<String>) -> Self {
        let timestamp = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_millis() as u64;

        Self {
            timestamp,
            level,
            context: context.into(),
            message: message.into(),
            metadata: Vec::new(),
            error: None,
        }
    }

    /// Add metadata field
    pub fn with_field(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.metadata.push((key.into(), value.into()));
        self
    }

    /// Add multiple metadata fields
    pub fn with_fields(mut self, fields: &[(impl AsRef<str>, impl AsRef<str>)]) -> Self {
        for (key, value) in fields {
            self.metadata.push((key.as_ref().to_string(), value.as_ref().to_string()));
        }
        self
    }

    /// Add error information
    pub fn with_error(mut self, error: impl fmt::Display) -> Self {
        self.error = Some(error.to_string());
        self
    }

    /// Convert to JSON string
    pub fn to_json(&self) -> String {
        let mut s = String::from('{');

        s.push_str(&format!(r#""timestamp":{}"#, self.timestamp));
        s.push_str(&format!(r#","level":"{}""#, self.level));
        s.push_str(&format!(r#","context":"{}""#, escape_json(&self.context)));
        s.push_str(&format!(r#","message":"{}""#, escape_json(&self.message)));

        for (key, value) in &self.metadata {
            s.push_str(&format!(r#","{}":"{}""#, escape_json(key), escape_json(value)));
        }

        if let Some(ref error) = self.error {
            s.push_str(&format!(r#","error":"{}""#, escape_json(error)));
        }

        s.push('}');
        s
    }

    /// Convert to human-readable text
    pub fn to_text(&self) -> String {
        let timestamp = chrono::DateTime::<chrono::Utc>::from_timestamp_millis(self.timestamp as i64)
            .unwrap()
            .format("%Y-%m-%d %H:%M:%S%.3f");

        let mut s = format!("[{}] {} {}: {}", timestamp, self.level, self.context, self.message);

        for (key, value) in &self.metadata {
            s.push_str(&format!(" {}={}", key, value));
        }

        if let Some(ref error) = self.error {
            s.push_str(&format!(" error={}", error));
        }

        s
    }
}

/// Escape JSON string
fn escape_json(s: &str) -> String {
    s.replace('\\', "\\\\")
        .replace('"', "\\\"")
        .replace('\n', "\\n")
        .replace('\r', "\\r")
        .replace('\t', "\\t")
}

/// Observer for log entries
pub trait LogObserver: Send + Sync {
    /// Called when a log entry is created
    fn on_log(&self, entry: &LogEntry);
}

/// Default console observer that prints to stdout/stderr
pub struct ConsoleLogObserver {
    /// Output format
    format: LogFormat,

    /// Minimum level to output
    min_level: LogLevel,
}

/// Log output format
#[derive(Debug, Clone, Copy)]
pub enum LogFormat {
    /// Human-readable text format
    Text,

    /// JSON format
    Json,
}

impl ConsoleLogObserver {
    /// Create a new console observer
    pub fn new(min_level: LogLevel, format: LogFormat) -> Self {
        Self { format, min_level }
    }

    /// Create a text format observer
    pub fn text(min_level: LogLevel) -> Self {
        Self::new(min_level, LogFormat::Text)
    }

    /// Create a JSON format observer
    pub fn json(min_level: LogLevel) -> Self {
        Self::new(min_level, LogFormat::Json)
    }
}

impl LogObserver for ConsoleLogObserver {
    fn on_log(&self, entry: &LogEntry) {
        if entry.level < self.min_level {
            return;
        }

        let output = match self.format {
            LogFormat::Text => entry.to_text(),
            LogFormat::Json => entry.to_json(),
        };

        match entry.level {
            LogLevel::Error => eprintln!("{}", output),
            LogLevel::Warn => eprintln!("{}", output),
            _ => println!("{}", output),
        };
    }
}

/// Structured logger
pub struct Logger {
    /// Logger context/name
    context: String,

    /// Minimum log level
    min_level: LogLevel,

    /// Observers for log entries
    observers: Vec<std::sync::Arc<dyn LogObserver>>,
}

impl fmt::Debug for Logger {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Logger")
            .field("context", &self.context)
            .field("min_level", &self.min_level)
            .field("observers_count", &self.observers.len())
            .finish()
    }
}

impl Logger {
    /// Create a new logger with the given context
    pub fn new(context: impl Into<String>) -> Self {
        Self {
            context: context.into(),
            min_level: LogLevel::Info,
            observers: Vec::new(),
        }
    }

    /// Set the minimum log level
    pub fn with_min_level(mut self, level: LogLevel) -> Self {
        self.min_level = level;
        self
    }

    /// Add an observer
    pub fn with_observer(mut self, observer: std::sync::Arc<dyn LogObserver>) -> Self {
        self.observers.push(observer);
        self
    }

    /// Log a trace message
    pub fn trace(&self, message: impl fmt::Display, fields: &[(impl AsRef<str>, impl AsRef<str>)]) {
        self.log(LogLevel::Trace, message, fields, None as Option<&str>);
    }

    /// Log a debug message
    pub fn debug(&self, message: impl fmt::Display, fields: &[(impl AsRef<str>, impl AsRef<str>)]) {
        self.log(LogLevel::Debug, message, fields, None as Option<&str>);
    }

    /// Log an info message
    pub fn info(&self, message: impl fmt::Display, fields: &[(impl AsRef<str>, impl AsRef<str>)]) {
        self.log(LogLevel::Info, message, fields, None as Option<&str>);
    }

    /// Log a warning message
    pub fn warn(&self, message: impl fmt::Display, fields: &[(impl AsRef<str>, impl AsRef<str>)]) {
        self.log(LogLevel::Warn, message, fields, None as Option<&str>);
    }

    /// Log an error message
    pub fn error(&self, message: impl fmt::Display, error: Option<impl fmt::Display>) {
        const EMPTY: &[(&str, &str)] = &[];
        self.log(LogLevel::Error, message, EMPTY, error);
    }

    /// Internal log method
    fn log(
        &self,
        level: LogLevel,
        message: impl fmt::Display,
        fields: &[(impl AsRef<str>, impl AsRef<str>)],
        error: Option<impl fmt::Display>,
    ) {
        if level < self.min_level {
            return;
        }

        let mut entry = LogEntry::new(level, &self.context, message.to_string());

        for (key, value) in fields {
            entry = entry.with_field(key.as_ref(), value.as_ref());
        }

        if let Some(e) = error {
            entry = entry.with_error(e);
        }

        // Notify observers
        for observer in &self.observers {
            observer.on_log(&entry);
        }

        // Default: use tracing if available
        if self.observers.is_empty() {
            match level {
                LogLevel::Trace => {
                    tracing::trace!(context = %self.context, message = %entry.message, "TRACE")
                },
                LogLevel::Debug => {
                    tracing::debug!(context = %self.context, message = %entry.message, "DEBUG")
                },
                LogLevel::Info => {
                    tracing::info!(context = %self.context, message = %entry.message, "INFO")
                },
                LogLevel::Warn => {
                    tracing::warn!(context = %self.context, message = %entry.message, "WARN")
                },
                LogLevel::Error => {
                    tracing::error!(context = %self.context, message = %entry.message, error = ?entry.error, "ERROR")
                },
            }
        }
    }
}

impl Clone for Logger {
    fn clone(&self) -> Self {
        Self {
            context: self.context.clone(),
            min_level: self.min_level,
            observers: self.observers.clone(),
        }
    }
}

/// Global logger registry (for convenience)
pub struct GlobalLogger {
    loggers: std::sync::RwLock<std::collections::HashMap<String, Logger>>,
}

impl GlobalLogger {
    /// Get the global logger instance
    pub fn instance() -> std::sync::Arc<Self> {
        static INSTANCE: std::sync::OnceLock<std::sync::Arc<GlobalLogger>> = std::sync::OnceLock::new();
        INSTANCE
            .get_or_init(|| {
                std::sync::Arc::new(Self {
                    loggers: std::sync::RwLock::new(std::collections::HashMap::new()),
                })
            })
            .clone()
    }

    /// Get or create a logger for a context
    pub fn get(&self, context: &str) -> Logger {
        let loggers = self.loggers.read().unwrap();
        loggers
            .get(context)
            .cloned()
            .unwrap_or_else(|| Logger::new(context))
    }

    /// Register a logger
    pub fn register(&self, logger: Logger) {
        let mut loggers = self.loggers.write().unwrap();
        loggers.insert(logger.context.clone(), logger);
    }

    /// Set the default minimum log level for all loggers
    pub fn set_min_level(&self, level: LogLevel) {
        let mut loggers = self.loggers.write().unwrap();
        for logger in loggers.values_mut() {
            logger.min_level = level;
        }
    }
}

/// Get a logger for the given context
pub fn logger(context: &str) -> Logger {
    GlobalLogger::instance().get(context)
}

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

    #[test]
    fn test_log_level_from_str() {
        assert_eq!(LogLevel::from_str("INFO").unwrap(), LogLevel::Info);
        assert_eq!(LogLevel::from_str("error").unwrap(), LogLevel::Error);
        assert!(LogLevel::from_str("INVALID").is_err());
    }

    #[test]
    fn test_log_entry_creation() {
        let entry = LogEntry::new(LogLevel::Info, "TestContext", "Test message")
            .with_field("key1", "value1")
            .with_field("key2", "value2");

        assert_eq!(entry.level, LogLevel::Info);
        assert_eq!(entry.context, "TestContext");
        assert_eq!(entry.message, "Test message");
        assert_eq!(entry.metadata.len(), 2);
    }

    #[test]
    fn test_log_entry_json() {
        let entry = LogEntry::new(LogLevel::Error, "Test", "Error message")
            .with_field("code", "500")
            .with_error("Connection failed");

        let json = entry.to_json();
        assert!(json.contains(r#""level":"ERROR""#));
        assert!(json.contains(r#""context":"Test""#));
        assert!(json.contains(r#""message":"Error message""#));
        assert!(json.contains(r#""code":"500""#));
        assert!(json.contains(r#""error":"Connection failed""#));
    }

    #[test]
    fn test_log_entry_text() {
        let entry = LogEntry::new(LogLevel::Info, "MyAgent", "Processing complete")
            .with_field("duration_ms", "150");

        let text = entry.to_text();
        assert!(text.contains("INFO"));
        assert!(text.contains("MyAgent"));
        assert!(text.contains("Processing complete"));
        assert!(text.contains("duration_ms=150"));
    }

    #[test]
    fn test_logger() {
        let logger = Logger::new("TestLogger").with_min_level(LogLevel::Debug);

        const EMPTY_FIELDS: &[(&str, &str)] = &[];

        // These should not panic
        logger.trace("Trace msg", EMPTY_FIELDS);
        logger.debug("Debug msg", &[("key", "value")]);
        logger.info("Info msg", EMPTY_FIELDS);
        logger.warn("Warn msg", EMPTY_FIELDS);
        logger.error("Error msg", Some("error details"));
    }

    #[test]
    fn test_logger_with_observer() {
        struct TestObserver {
            entries: std::sync::Arc<std::sync::Mutex<Vec<LogEntry>>>,
        }

        impl LogObserver for TestObserver {
            fn on_log(&self, entry: &LogEntry) {
                self.entries.lock().unwrap().push(entry.clone());
            }
        }

        let entries = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
        let observer = std::sync::Arc::new(TestObserver {
            entries: entries.clone(),
        });

        let logger = Logger::new("Test")
            .with_min_level(LogLevel::Info)
            .with_observer(observer);

        logger.info("Test message", &[("key", "value")]);

        let logged = entries.lock().unwrap();
        assert_eq!(logged.len(), 1);
        assert_eq!(logged[0].message, "Test message");
        assert_eq!(logged[0].context, "Test");
    }
}