claude-agent 0.2.25

Rust SDK for building AI agents with Anthropic's Claude - Direct API, no CLI dependency
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
//! Tool state for thread-safe state access.

use std::collections::VecDeque;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};

use tokio::sync::{Notify, RwLock, Semaphore};
use uuid::Uuid;

use super::queue::{MergedInput, QueueError, QueuedInput, SharedInputQueue};
use super::state::{Session, SessionConfig, SessionId};
use super::types::{CompactRecord, Plan, PlanStatus, TodoItem, ToolExecution};

const MAX_EXECUTION_LOG_SIZE: usize = 1000;

#[derive(Debug)]
struct ToolExecutionLog {
    entries: RwLock<VecDeque<ToolExecution>>,
}

impl Default for ToolExecutionLog {
    fn default() -> Self {
        Self::new()
    }
}

impl ToolExecutionLog {
    fn new() -> Self {
        Self {
            entries: RwLock::new(VecDeque::with_capacity(64)),
        }
    }

    async fn append(&self, exec: ToolExecution) {
        let mut entries = self.entries.write().await;
        if entries.len() >= MAX_EXECUTION_LOG_SIZE {
            entries.pop_front();
        }
        entries.push_back(exec);
    }

    async fn with_entries<F, R>(&self, f: F) -> R
    where
        F: FnOnce(&VecDeque<ToolExecution>) -> R,
    {
        let entries = self.entries.read().await;
        f(&entries)
    }

    async fn len(&self) -> usize {
        self.entries.read().await.len()
    }

    async fn clear(&self) {
        self.entries.write().await.clear();
    }
}

struct ToolStateInner {
    id: SessionId,
    session: RwLock<Session>,
    executions: ToolExecutionLog,
    input_queue: SharedInputQueue,
    execution_lock: Semaphore,
    executing: AtomicBool,
    queue_notify: Notify,
}

impl std::fmt::Debug for ToolStateInner {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ToolStateInner")
            .field("id", &self.id)
            .field("executions", &self.executions)
            .field("executing", &self.executing.load(Ordering::Relaxed))
            .finish_non_exhaustive()
    }
}

impl ToolStateInner {
    fn new(session_id: SessionId) -> Self {
        Self {
            id: session_id,
            session: RwLock::new(Session::from_id(session_id, SessionConfig::default())),
            executions: ToolExecutionLog::new(),
            input_queue: SharedInputQueue::new(),
            execution_lock: Semaphore::new(1),
            executing: AtomicBool::new(false),
            queue_notify: Notify::new(),
        }
    }

    fn from_session(session: Session) -> Self {
        let id = session.id;
        Self {
            id,
            session: RwLock::new(session),
            executions: ToolExecutionLog::new(),
            input_queue: SharedInputQueue::new(),
            execution_lock: Semaphore::new(1),
            executing: AtomicBool::new(false),
            queue_notify: Notify::new(),
        }
    }
}

/// Thread-safe tool state handle.
#[derive(Debug, Clone)]
pub struct ToolState(Arc<ToolStateInner>);

impl ToolState {
    pub fn new(session_id: SessionId) -> Self {
        Self(Arc::new(ToolStateInner::new(session_id)))
    }

    pub fn from_session(session: Session) -> Self {
        Self(Arc::new(ToolStateInner::from_session(session)))
    }

    #[inline]
    pub fn session_id(&self) -> SessionId {
        self.0.id
    }

    pub async fn session(&self) -> Session {
        self.0.session.read().await.clone()
    }

    pub async fn update_session(&self, session: Session) {
        *self.0.session.write().await = session;
    }

    pub async fn enter_plan_mode(&self, name: Option<String>) -> Plan {
        self.0.session.write().await.enter_plan_mode(name).clone()
    }

    pub async fn current_plan(&self) -> Option<Plan> {
        self.0.session.read().await.current_plan.clone()
    }

    pub async fn update_plan_content(&self, content: String) {
        self.0.session.write().await.update_plan_content(content);
    }

    pub async fn exit_plan_mode(&self) -> Option<Plan> {
        self.0.session.write().await.exit_plan_mode()
    }

    pub async fn cancel_plan(&self) -> Option<Plan> {
        self.0.session.write().await.cancel_plan()
    }

    #[inline]
    pub async fn is_in_plan_mode(&self) -> bool {
        self.0.session.read().await.is_in_plan_mode()
    }

    pub async fn set_todos(&self, todos: Vec<TodoItem>) {
        self.0.session.write().await.set_todos(todos);
    }

    pub async fn todos(&self) -> Vec<TodoItem> {
        self.0.session.read().await.todos.clone()
    }

    #[inline]
    pub async fn todos_in_progress_count(&self) -> usize {
        self.0.session.read().await.todos_in_progress_count()
    }

    pub async fn record_tool_execution(&self, mut exec: ToolExecution) {
        let plan_id = {
            let session = self.0.session.read().await;
            if let Some(ref plan) = session.current_plan
                && plan.status == PlanStatus::Executing
            {
                Some(plan.id)
            } else {
                None
            }
        };
        exec.plan_id = plan_id;
        self.0.executions.append(exec).await;
    }

    pub async fn with_tool_executions<F, R>(&self, f: F) -> R
    where
        F: FnOnce(&VecDeque<ToolExecution>) -> R,
    {
        self.0.executions.with_entries(f).await
    }

    pub async fn execution_log_len(&self) -> usize {
        self.0.executions.len().await
    }

    pub async fn clear_execution_log(&self) {
        self.0.executions.clear().await;
    }

    pub async fn record_compact(&self, record: CompactRecord) {
        self.0.session.write().await.record_compact(record);
    }

    pub async fn with_compact_history<F, R>(&self, f: F) -> R
    where
        F: FnOnce(&VecDeque<CompactRecord>) -> R,
    {
        let session = self.0.session.read().await;
        f(&session.compact_history)
    }

    #[inline]
    pub async fn session_snapshot(&self) -> (SessionId, usize, Option<Plan>) {
        let session = self.0.session.read().await;
        (
            session.id,
            session.todos.len(),
            session.current_plan.clone(),
        )
    }

    #[inline]
    pub async fn execution_state(&self) -> (SessionId, bool, usize) {
        let session = self.0.session.read().await;
        (
            session.id,
            session.is_in_plan_mode(),
            session.todos_in_progress_count(),
        )
    }

    pub async fn record_execution_with_todos(
        &self,
        mut exec: ToolExecution,
        todos: Option<Vec<TodoItem>>,
    ) {
        let plan_id = {
            let mut session = self.0.session.write().await;
            let plan_id = if let Some(ref plan) = session.current_plan
                && plan.status == PlanStatus::Executing
            {
                Some(plan.id)
            } else {
                None
            };
            if let Some(todos) = todos {
                session.set_todos(todos);
            }
            plan_id
        };
        exec.plan_id = plan_id;
        self.0.executions.append(exec).await;
    }

    pub async fn enqueue(&self, content: impl Into<String>) -> Result<Uuid, QueueError> {
        let input = QueuedInput::new(self.session_id(), content);
        let result = self.0.input_queue.enqueue(input).await;
        if result.is_ok() {
            self.0.queue_notify.notify_waiters();
        }
        result
    }

    pub async fn dequeue_or_merge(&self) -> Option<MergedInput> {
        self.0.input_queue.merge_all().await
    }

    pub async fn pending_count(&self) -> usize {
        self.0.input_queue.pending_count().await
    }

    pub async fn cancel_pending(&self, id: Uuid) -> bool {
        self.0.input_queue.cancel(id).await.is_some()
    }

    pub async fn cancel_all_pending(&self) -> usize {
        self.0.input_queue.cancel_all().await.len()
    }

    pub fn is_executing(&self) -> bool {
        self.0.executing.load(Ordering::Acquire)
    }

    pub async fn wait_for_queue_signal(&self) {
        self.0.queue_notify.notified().await;
    }

    pub async fn acquire_execution(&self) -> ExecutionGuard<'_> {
        let permit = self
            .0
            .execution_lock
            .acquire()
            .await
            .expect("semaphore should not be closed");
        self.0.executing.store(true, Ordering::Release);
        ExecutionGuard {
            permit,
            executing: &self.0.executing,
            queue_notify: &self.0.queue_notify,
        }
    }

    pub fn try_acquire_execution(&self) -> Option<ExecutionGuard<'_>> {
        self.0.execution_lock.try_acquire().ok().map(|permit| {
            self.0.executing.store(true, Ordering::Release);
            ExecutionGuard {
                permit,
                executing: &self.0.executing,
                queue_notify: &self.0.queue_notify,
            }
        })
    }

    pub async fn with_session<F, R>(&self, f: F) -> R
    where
        F: FnOnce(&Session) -> R,
    {
        let session = self.0.session.read().await;
        f(&session)
    }

    pub async fn with_session_mut<F, R>(&self, f: F) -> R
    where
        F: FnOnce(&mut Session) -> R,
    {
        let mut session = self.0.session.write().await;
        f(&mut session)
    }

    pub async fn compact(
        &self,
        client: &crate::Client,
        keep_messages: usize,
    ) -> crate::Result<crate::types::CompactResult> {
        let mut session = self.0.session.write().await;
        session.compact(client, keep_messages).await
    }
}

pub struct ExecutionGuard<'a> {
    #[allow(dead_code)]
    permit: tokio::sync::SemaphorePermit<'a>,
    executing: &'a AtomicBool,
    queue_notify: &'a Notify,
}

impl Drop for ExecutionGuard<'_> {
    fn drop(&mut self) {
        self.executing.store(false, Ordering::Release);
        self.queue_notify.notify_waiters();
    }
}

impl Default for ToolState {
    fn default() -> Self {
        Self::new(SessionId::default())
    }
}

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

    #[tokio::test]
    async fn test_plan_lifecycle() {
        let state = ToolState::new(SessionId::new());

        let plan = state.enter_plan_mode(Some("Test Plan".to_string())).await;
        assert_eq!(plan.status, PlanStatus::Draft);
        assert!(state.is_in_plan_mode().await);

        state
            .update_plan_content("Step 1\nStep 2".to_string())
            .await;

        let approved = state.exit_plan_mode().await;
        assert!(approved.is_some());
        assert_eq!(approved.unwrap().status, PlanStatus::Approved);
    }

    #[tokio::test]
    async fn test_todos() {
        let session_id = SessionId::new();
        let state = ToolState::new(session_id);

        let todos = vec![
            TodoItem::new(session_id, "Task 1", "Doing task 1"),
            TodoItem::new(session_id, "Task 2", "Doing task 2"),
        ];

        state.set_todos(todos).await;
        let loaded = state.todos().await;
        assert_eq!(loaded.len(), 2);
    }

    #[tokio::test]
    async fn test_tool_execution_recording() {
        let session_id = SessionId::new();
        let state = ToolState::new(session_id);

        let exec = ToolExecution::new(session_id, "Bash", serde_json::json!({"command": "ls"}))
            .output("file1\nfile2", false)
            .duration(100);

        state.record_tool_execution(exec).await;

        let count = state.with_tool_executions(|e| e.len()).await;
        assert_eq!(count, 1);

        let name = state
            .with_tool_executions(|e| e.front().map(|x| x.tool_name.clone()))
            .await;
        assert_eq!(name, Some("Bash".to_string()));
    }

    #[tokio::test]
    async fn test_session_persistence_ready() {
        let session_id = SessionId::new();
        let state = ToolState::new(session_id);

        let todos = vec![TodoItem::new(session_id, "Task 1", "Doing task 1")];
        state.set_todos(todos).await;
        state.enter_plan_mode(Some("My Plan".to_string())).await;

        let session = state.session().await;
        assert_eq!(session.todos.len(), 1);
        assert!(session.current_plan.is_some());
        assert_eq!(
            session.current_plan.unwrap().name,
            Some("My Plan".to_string())
        );
    }

    #[tokio::test]
    async fn test_resume_from_session() {
        let session_id = SessionId::new();

        let mut session = Session::new(SessionConfig::default());
        session.id = session_id;
        session.set_todos(vec![TodoItem::new(
            session_id,
            "Resumed Task",
            "Working on it",
        )]);
        session.enter_plan_mode(Some("Resumed Plan".to_string()));

        let state = ToolState::from_session(session);

        let todos = state.todos().await;
        assert_eq!(todos.len(), 1);
        assert_eq!(todos[0].content, "Resumed Task");

        let plan = state.current_plan().await;
        assert!(plan.is_some());
        assert_eq!(plan.unwrap().name, Some("Resumed Plan".to_string()));
    }

    #[tokio::test]
    async fn test_concurrent_execution_recording() {
        let session_id = SessionId::new();
        let state = ToolState::new(session_id);

        let handles: Vec<_> = (0..10)
            .map(|i| {
                let state = state.clone();
                let sid = session_id;
                tokio::spawn(async move {
                    let exec =
                        ToolExecution::new(sid, format!("Tool{}", i), serde_json::json!({"id": i}));
                    state.record_tool_execution(exec).await;
                })
            })
            .collect();

        for h in handles {
            h.await.unwrap();
        }

        let count = state.with_tool_executions(|e| e.len()).await;
        assert_eq!(count, 10);
    }

    #[tokio::test]
    async fn test_execution_log_limit() {
        let session_id = SessionId::new();
        let state = ToolState::new(session_id);

        for i in 0..MAX_EXECUTION_LOG_SIZE + 100 {
            let exec = ToolExecution::new(session_id, format!("Tool{}", i), serde_json::json!({}));
            state.record_tool_execution(exec).await;
        }

        let count = state.with_tool_executions(|e| e.len()).await;
        assert_eq!(count, MAX_EXECUTION_LOG_SIZE);

        let first_name = state
            .with_tool_executions(|e| e.front().map(|x| x.tool_name.clone()))
            .await;
        assert!(first_name.unwrap().contains("100"));
    }
}