claude_code_sdk/
config.rs1#[derive(Debug, Clone)]
5pub struct SafetyLimits {
6 pub max_line_size: usize,
8
9 pub max_text_block_size: usize,
11
12 pub max_buffer_size: usize,
14
15 pub max_log_preview_chars: usize,
17
18 pub max_buffered_messages: usize,
20
21 pub json_parse_timeout_ms: u64,
23}
24
25impl Default for SafetyLimits {
26 fn default() -> Self {
27 Self {
28 max_line_size: 10 * 1024 * 1024,
30
31 max_text_block_size: 5 * 1024 * 1024,
33
34 max_buffer_size: 50 * 1024 * 1024,
36
37 max_log_preview_chars: 200,
39
40 max_buffered_messages: 100,
42
43 json_parse_timeout_ms: 5000,
45 }
46 }
47}
48
49impl SafetyLimits {
50 pub fn new() -> Self {
52 Self::default()
53 }
54
55 pub fn conservative() -> Self {
57 Self {
58 max_line_size: 1024 * 1024, max_text_block_size: 512 * 1024, max_buffer_size: 10 * 1024 * 1024, max_log_preview_chars: 100,
62 max_buffered_messages: 50,
63 json_parse_timeout_ms: 2000,
64 }
65 }
66
67 pub fn generous() -> Self {
69 Self {
70 max_line_size: 50 * 1024 * 1024, max_text_block_size: 25 * 1024 * 1024, max_buffer_size: 200 * 1024 * 1024, max_log_preview_chars: 500,
74 max_buffered_messages: 200,
75 json_parse_timeout_ms: 10000,
76 }
77 }
78
79 pub fn is_line_size_safe(&self, size: usize) -> bool {
81 size <= self.max_line_size
82 }
83
84 pub fn is_text_block_safe(&self, size: usize) -> bool {
86 size <= self.max_text_block_size
87 }
88
89 pub fn safe_log_preview(&self, text: &str) -> String {
91 if text.len() <= self.max_log_preview_chars {
92 text.to_string()
93 } else {
94 format!("{}... ({} total chars)",
95 text.chars().take(self.max_log_preview_chars).collect::<String>(),
96 text.len())
97 }
98 }
99}
100
101#[derive(Debug, thiserror::Error)]
103pub enum SafetyError {
104 #[error("Line size {actual} bytes exceeds limit of {limit} bytes")]
105 LineTooLarge { actual: usize, limit: usize },
106
107 #[error("Text block size {actual} bytes exceeds limit of {limit} bytes")]
108 TextBlockTooLarge { actual: usize, limit: usize },
109
110 #[error("Buffer size {actual} bytes exceeds limit of {limit} bytes")]
111 BufferTooLarge { actual: usize, limit: usize },
112
113 #[error("Too many buffered messages: {actual}, limit: {limit}")]
114 TooManyMessages { actual: usize, limit: usize },
115
116 #[error("JSON parsing timeout after {timeout_ms}ms")]
117 ParseTimeout { timeout_ms: u64 },
118}