agent-base 0.16.0

A lightweight Agent Runtime Kernel for building AI agents in Rust
Documentation
use agent_base::ChatMessage;
use llm_trait::{ChatRequest, ReasoningConfig};
use llm_unified::create_provider;
use serde_json::{Value, json};

/// Test the request format generated by the provider.
/// This test does not actually send HTTP requests; it validates the request-building logic instead.
#[tokio::test]
async fn test_provider_request_body_format() {
    // Create a test provider (uses an invalid API key, but validates the building logic)
    let provider = create_provider(&llm_trait::LlmConfig {
        protocol: Some(llm_trait::Protocol::OpenAi),
        api_key: "test-api-key".to_string(),
        model: "qwen-flash".to_string(),
        base_url: "https://test.example.com/v1".to_string(),
        options: std::collections::HashMap::new(),
    })
    .expect("should create provider");

    let messages = vec![
        ChatMessage::system("你是一个助手"),
        ChatMessage::user("你好"),
    ];

    let reasoning = ReasoningConfig {
        enabled: Some(true),
        budget_tokens: Some(128),
        effort: None,
    };

    let request = ChatRequest::new(messages).with_reasoning(reasoning);
    let result = provider.stream(request).await;

    // We expect the request to fail (because the URL is invalid), but the provider should build correctly.
    assert!(result.is_err(), "Expected error due to invalid URL");
}

/// Verify the JSON structure of the request body (by constructing the same JSON)
#[test]
fn test_request_body_json_structure() {
    // Simulate an OpenAI-compatible request body
    let mut request_body = json!({
        "model": "qwen-flash",
        "messages": [
            {
                "role": "system",
                "content": "你是一个助手"
            },
            {
                "role": "user",
                "content": "你好"
            }
        ],
        "tools": [],
        "stream": true,
        "stream_options": {
            "include_usage": true
        }
    });

    // Add enable_thinking
    if let Some(obj) = request_body.as_object_mut() {
        obj.insert("enable_thinking".to_string(), json!(true));
    }

    // Add extra_body containing thinking_budget
    let mut extra_body = serde_json::Map::new();
    extra_body.insert("thinking_budget".to_string(), json!(128));
    if let Some(obj) = request_body.as_object_mut() {
        obj.insert("extra_body".to_string(), Value::Object(extra_body));
    }

    // Verify the structure
    let json_str = serde_json::to_string_pretty(&request_body).unwrap();
    println!("Expected request body:\n{}", json_str);

    // Verify enable_thinking is at the root level
    assert_eq!(request_body["enable_thinking"], true);

    // Verify extra_body exists
    assert!(request_body["extra_body"].is_object());
    assert_eq!(request_body["extra_body"]["thinking_budget"], 128);
}

/// Test different thinking_budget values
#[test]
fn test_thinking_budget_values() {
    let budgets = vec![100, 128, 256, 512, 1024, 2048, 4096];

    for budget in budgets {
        let mut request_body = json!({
            "model": "qwen-flash",
            "messages": [],
            "tools": [],
            "stream": true,
        });

        let mut extra_body = serde_json::Map::new();
        extra_body.insert("thinking_budget".to_string(), json!(budget));
        if let Some(obj) = request_body.as_object_mut() {
            obj.insert("extra_body".to_string(), Value::Object(extra_body));
        }

        assert_eq!(
            request_body["extra_body"]["thinking_budget"], budget,
            "thinking_budget should be {}",
            budget
        );
    }
}