nucel-agent-sdk 0.2.0

Unified AI coding agent SDK — provider-agnostic abstraction for Claude Code, Codex, and OpenCode
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
//! End-to-end tests for the Nucel Agent SDK.
//!
//! These tests exercise the full lifecycle: spawn → query → cost → close.
//! - OpenCode: full E2E against wiremock with a real temp repo
//! - Claude Code / Codex: spawn with real CLI if available, otherwise skip

use nucel_agent_sdk::*;
use serde_json::json;
use tempfile::TempDir;
use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};

// ── Helpers ──────────────────────────────────────────────────────────────

/// Create a temp directory that looks like a real project workspace.
fn create_mock_repo() -> TempDir {
    let dir = TempDir::new().expect("failed to create temp dir");
    let repo_path = dir.path();

    // Initialize a git repo so agents don't complain.
    std::process::Command::new("git")
        .args(["init", "--initial-branch=main"])
        .current_dir(repo_path)
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .status()
        .ok();

    std::process::Command::new("git")
        .args(["config", "user.email", "test@nucel.dev"])
        .current_dir(repo_path)
        .stdout(std::process::Stdio::null())
        .status()
        .ok();

    std::process::Command::new("git")
        .args(["config", "user.name", "Test"])
        .current_dir(repo_path)
        .stdout(std::process::Stdio::null())
        .status()
        .ok();

    // Create some source files.
    std::fs::write(
        repo_path.join("main.rs"),
        r#"fn main() {
    println!("Hello from mock repo");
}
"#,
    )
    .unwrap();

    std::fs::write(
        repo_path.join("lib.rs"),
        r#"pub fn add(a: i32, b: i32) -> i32 {
    a + b
}

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

    #[test]
    fn test_add() {
        assert_eq!(add(2, 3), 5);
    }
}
"#,
    )
    .unwrap();

    std::fs::write(
        repo_path.join("Cargo.toml"),
        r#"[package]
name = "mock-project"
version = "0.1.0"
edition = "2021"
"#,
    )
    .unwrap();

    // Commit so the repo has history.
    std::process::Command::new("git")
        .args(["add", "."])
        .current_dir(repo_path)
        .stdout(std::process::Stdio::null())
        .status()
        .ok();

    std::process::Command::new("git")
        .args(["commit", "-m", "initial commit"])
        .current_dir(repo_path)
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .status()
        .ok();

    dir
}

/// Check if a CLI tool is available.
fn cli_available(name: &str) -> bool {
    std::process::Command::new("which")
        .arg(name)
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .status()
        .map(|s| s.success())
        .unwrap_or(false)
}

// ── OpenCode E2E (wiremock) ─────────────────────────────────────────────

/// Full lifecycle against wiremock: create session → prompt → follow-up → cost → close.
#[tokio::test]
async fn e2e_opencode_full_session_lifecycle() {
    let server = MockServer::start().await;
    let repo = create_mock_repo();

    // 1. POST /session → create session
    Mock::given(method("POST"))
        .and(path("/session"))
        .respond_with(
            ResponseTemplate::new(200).set_body_json(json!({
                "id": "e2e-sess-001",
                "status": "active"
            })),
        )
        .expect(1)
        .mount(&server)
        .await;

    // 2. POST /session/e2e-sess-001/prompt → initial response
    Mock::given(method("POST"))
        .and(path("/session/e2e-sess-001/prompt"))
        .respond_with(
            ResponseTemplate::new(200).set_body_json(json!({
                "parts": [
                    {"type": "text", "text": "I've analyzed the codebase. The main.rs file prints 'Hello from mock repo' and lib.rs has an `add` function with a passing test."}
                ],
                "cost": 0.012
            })),
        )
        .mount(&server)
        .await;

    let executor = OpencodeExecutor::with_base_url(server.uri());

    // Verify availability.
    let avail = executor.availability();
    assert!(avail.available);

    // Spawn session.
    let session = executor
        .spawn(
            repo.path(),
            "Analyze this Rust project and describe what it does",
            &SpawnConfig {
                model: Some("claude-sonnet-4-6".into()),
                budget_usd: Some(1.0),
                system_prompt: Some("You are a code reviewer.".into()),
                ..Default::default()
            },
        )
        .await
        .expect("spawn should succeed");

    // Verify session properties.
    assert_eq!(session.executor_type, ExecutorType::OpenCode);
    assert_eq!(session.model.as_deref(), Some("claude-sonnet-4-6"));
    assert_eq!(session.working_dir, repo.path());

    // Check metadata.
    let meta = session.metadata();
    assert_eq!(meta.executor_type, ExecutorType::OpenCode);
    assert_eq!(meta.model.as_deref(), Some("claude-sonnet-4-6"));
    assert!(meta.created_at <= chrono::Utc::now());

    // Check cost from initial prompt.
    let cost = session.total_cost().await.unwrap();
    assert!((cost.total_usd - 0.012).abs() < f64::EPSILON);

    // Close session.
    session.close().await.expect("close should succeed");
}

/// E2E: budget exceeded mid-session.
#[tokio::test]
async fn e2e_opencode_budget_exceeded_during_prompt() {
    let server = MockServer::start().await;
    let repo = create_mock_repo();

    Mock::given(method("POST"))
        .and(path("/session"))
        .respond_with(
            ResponseTemplate::new(200)
                .set_body_json(json!({"id": "e2e-budget"})),
        )
        .mount(&server)
        .await;

    Mock::given(method("POST"))
        .and(path("/session/e2e-budget/prompt"))
        .respond_with(
            ResponseTemplate::new(200).set_body_json(json!({
                "parts": [{"type": "text", "text": "Expensive analysis"}],
                "cost": 5.0
            })),
        )
        .mount(&server)
        .await;

    let executor = OpencodeExecutor::with_base_url(server.uri());

    let result = executor
        .spawn(
            repo.path(),
            "Deep analysis",
            &SpawnConfig {
                budget_usd: Some(0.50),
                ..Default::default()
            },
        )
        .await;

    assert!(result.is_err());
    let err = result.unwrap_err();
    assert!(
        err.to_string().contains("budget exceeded"),
        "expected budget error, got: {err}"
    );
}

/// E2E: session resume with existing session ID.
#[tokio::test]
async fn e2e_opencode_resume_existing_session() {
    let server = MockServer::start().await;
    let repo = create_mock_repo();

    Mock::given(method("POST"))
        .and(path("/session/prev-session-42/prompt"))
        .respond_with(
            ResponseTemplate::new(200).set_body_json(json!({
                "parts": [{"type": "text", "text": "Continuing from where we left off."}],
                "cost": 0.005
            })),
        )
        .expect(1)
        .mount(&server)
        .await;

    let executor = OpencodeExecutor::with_base_url(server.uri());

    let session = executor
        .resume(
            repo.path(),
            "prev-session-42",
            "What was the last change you made?",
            &SpawnConfig {
                budget_usd: Some(1.0),
                ..Default::default()
            },
        )
        .await
        .expect("resume should succeed");

    assert_eq!(session.executor_type, ExecutorType::OpenCode);
    let cost = session.total_cost().await.unwrap();
    assert!((cost.total_usd - 0.005).abs() < f64::EPSILON);

    session.close().await.unwrap();
}

/// E2E: server error during session creation.
#[tokio::test]
async fn e2e_opencode_server_error_handling() {
    let server = MockServer::start().await;
    let repo = create_mock_repo();

    Mock::given(method("POST"))
        .and(path("/session"))
        .respond_with(ResponseTemplate::new(503))
        .mount(&server)
        .await;

    let executor = OpencodeExecutor::with_base_url(server.uri());

    let result = executor
        .spawn(
            repo.path(),
            "test",
            &SpawnConfig::default(),
        )
        .await;

    assert!(result.is_err());
    let err = result.unwrap_err();
    assert!(
        err.to_string().contains("opencode"),
        "error should mention provider: {err}"
    );
}

/// E2E: multiple text parts in response are concatenated.
#[tokio::test]
async fn e2e_opencode_multipart_response() {
    let server = MockServer::start().await;
    let repo = create_mock_repo();

    Mock::given(method("POST"))
        .and(path("/session"))
        .respond_with(
            ResponseTemplate::new(200)
                .set_body_json(json!({"id": "e2e-multi"})),
        )
        .mount(&server)
        .await;

    Mock::given(method("POST"))
        .and(path("/session/e2e-multi/prompt"))
        .respond_with(
            ResponseTemplate::new(200).set_body_json(json!({
                "parts": [
                    {"type": "text", "text": "Step 1: Read the code"},
                    {"type": "tool_call", "name": "read_file", "args": {"path": "main.rs"}},
                    {"type": "tool_result", "output": "fn main() {}", "success": true},
                    {"type": "text", "text": "Step 2: The code looks good"}
                ],
                "cost": 0.008
            })),
        )
        .mount(&server)
        .await;

    let executor = OpencodeExecutor::with_base_url(server.uri());

    let session = executor
        .spawn(
            repo.path(),
            "Review the code",
            &SpawnConfig::default(),
        )
        .await
        .unwrap();

    let cost = session.total_cost().await.unwrap();
    assert!((cost.total_usd - 0.008).abs() < f64::EPSILON);

    session.close().await.unwrap();
}

// ── build_executor E2E ──────────────────────────────────────────────────

/// E2E: build_executor produces working executors for all providers.
#[test]
fn e2e_build_executor_all_providers_construct() {
    let repo = create_mock_repo();

    for provider in available_providers() {
        let executor = build_executor(provider, None)
            .unwrap_or_else(|| panic!("build_executor({provider}) returned None"));

        // Check type matches.
        let expected_type = match *provider {
            "claude-code" => ExecutorType::ClaudeCode,
            "codex" => ExecutorType::Codex,
            "opencode" => ExecutorType::OpenCode,
            _ => panic!("unexpected provider: {provider}"),
        };
        assert_eq!(executor.executor_type(), expected_type);

        // Capabilities are populated.
        let caps = executor.capabilities();
        assert!(caps.token_usage);

        // Working dir exists in the mock repo.
        assert!(repo.path().exists());
    }
}

// ── Claude Code E2E (real CLI, skip if unavailable) ─────────────────────

/// Full E2E with real Claude Code CLI — spawns a real session.
/// Skipped if `claude` is not installed or ANTHROPIC_API_KEY is not set.
#[tokio::test]
async fn e2e_claude_code_real_cli_session() {
    if !cli_available("claude") {
        eprintln!("SKIP: claude CLI not available");
        return;
    }
    if std::env::var("ANTHROPIC_API_KEY").is_err() {
        eprintln!("SKIP: ANTHROPIC_API_KEY not set");
        return;
    }

    let repo = create_mock_repo();
    let executor = ClaudeCodeExecutor::new();

    let avail = executor.availability();
    assert!(avail.available, "claude CLI should be available");

    let session = executor
        .spawn(
            repo.path(),
            "What files are in this project? Reply in one sentence.",
            &SpawnConfig {
                budget_usd: Some(0.50),
                permission_mode: Some(PermissionMode::RejectAll),
                ..Default::default()
            },
        )
        .await;

    match session {
        Ok(sess) => {
            assert_eq!(sess.executor_type, ExecutorType::ClaudeCode);
            assert!(!sess.session_id.is_empty());

            let cost = sess.total_cost().await.unwrap();
            assert!(cost.total_usd >= 0.0, "cost should be non-negative");
            assert!(cost.total_usd <= 0.50, "cost should be within budget");

            sess.close().await.unwrap();
        }
        Err(e) => {
            // Acceptable failures: timeout, rate limit, etc.
            eprintln!("Claude Code E2E failed (acceptable): {e}");
        }
    }
}

// ── Codex E2E (real CLI, skip if unavailable) ───────────────────────────

/// Full E2E with real Codex CLI.
/// Skipped if `codex` is not installed or CODEX_API_KEY is not set.
#[tokio::test]
async fn e2e_codex_real_cli_session() {
    if !cli_available("codex") {
        eprintln!("SKIP: codex CLI not available");
        return;
    }
    if std::env::var("CODEX_API_KEY").is_err() && std::env::var("OPENAI_API_KEY").is_err() {
        eprintln!("SKIP: CODEX_API_KEY/OPENAI_API_KEY not set");
        return;
    }

    let repo = create_mock_repo();
    let executor = CodexExecutor::new();

    let avail = executor.availability();
    assert!(avail.available, "codex CLI should be available");

    let session = executor
        .spawn(
            repo.path(),
            "List the files in this directory",
            &SpawnConfig {
                budget_usd: Some(0.50),
                ..Default::default()
            },
        )
        .await;

    match session {
        Ok(sess) => {
            assert_eq!(sess.executor_type, ExecutorType::Codex);
            let cost = sess.total_cost().await.unwrap();
            assert!(cost.total_usd >= 0.0);
            sess.close().await.unwrap();
        }
        Err(e) => {
            eprintln!("Codex E2E failed (acceptable): {e}");
        }
    }
}

// ── 0.2.0 streaming tests ───────────────────────────────────────────────

#[tokio::test]
async fn e2e_default_query_stream_replays_query_result() {
    use std::sync::Arc;
    use async_trait::async_trait;
    use futures::StreamExt;
    use nucel_agent_sdk::{
        AgentCost, AgentResponse, AgentSession, ExecutorType, MessageEvent, Result, SessionImpl,
    };

    struct EchoSession;
    #[async_trait]
    impl SessionImpl for EchoSession {
        async fn query(&self, prompt: &str) -> Result<AgentResponse> {
            Ok(AgentResponse {
                content: format!("echo: {prompt}"),
                cost: AgentCost { input_tokens: 1, output_tokens: 2, total_usd: 0.0, ..Default::default() },
                ..Default::default()
            })
        }
        async fn total_cost(&self) -> Result<AgentCost> { Ok(AgentCost::default()) }
        async fn close(&self) -> Result<()> { Ok(()) }
    }

    let session = AgentSession::new(
        "test",
        ExecutorType::ClaudeCode,
        "/tmp",
        None,
        Arc::new(EchoSession),
    );
    let mut s = session.query_stream("hello").await.unwrap();
    let mut chunks = Vec::new();
    let mut terminal = false;
    while let Some(evt) = s.next().await {
        match evt.unwrap() {
            MessageEvent::TextChunk { text } => chunks.push(text),
            MessageEvent::ResultDone { content, .. } => {
                assert_eq!(content, "echo: hello");
                terminal = true;
            }
            _ => {}
        }
    }
    assert!(terminal);
    assert_eq!(chunks.concat(), "echo: hello");
}

#[tokio::test]
async fn e2e_collect_stream_round_trips_through_session() {
    use std::sync::Arc;
    use async_trait::async_trait;
    use nucel_agent_sdk::{
        AgentCost, AgentResponse, AgentSession, ExecutorType, Result, SessionImpl,
    };

    struct S;
    #[async_trait]
    impl SessionImpl for S {
        async fn query(&self, _: &str) -> Result<AgentResponse> {
            Ok(AgentResponse {
                content: "hi".into(),
                cost: AgentCost { input_tokens: 5, output_tokens: 7, total_usd: 0.01, cache_read_tokens: 100, cache_creation_tokens: 50 },
                ..Default::default()
            })
        }
        async fn total_cost(&self) -> Result<AgentCost> { Ok(AgentCost::default()) }
        async fn close(&self) -> Result<()> { Ok(()) }
    }

    let session = AgentSession::new("t", ExecutorType::ClaudeCode, "/tmp", None, Arc::new(S));
    let stream = session.query_stream("hi").await.unwrap();
    let resp = AgentSession::collect_stream(stream).await.unwrap();
    assert_eq!(resp.content, "hi");
    assert_eq!(resp.cost.input_tokens, 5);
    assert_eq!(resp.cost.cache_read_tokens, 100);
}