|
| 1 | +use std::collections::BTreeMap; |
| 2 | +use std::sync::{Arc, Mutex}; |
| 3 | + |
| 4 | +use axum::extract::State; |
| 5 | +use axum::routing::post; |
| 6 | +use axum::{Json, Router}; |
| 7 | +use serde_json::json; |
| 8 | + |
| 9 | +#[derive(Clone, Default)] |
| 10 | +struct TestState { |
| 11 | + calls: Arc<Mutex<u32>>, |
| 12 | +} |
| 13 | + |
| 14 | +#[tokio::test] |
| 15 | +async fn reserved_cron_tools_create_list_and_cancel() { |
| 16 | + async fn handler( |
| 17 | + State(state): State<TestState>, |
| 18 | + Json(_payload): Json<serde_json::Value>, |
| 19 | + ) -> Json<serde_json::Value> { |
| 20 | + let mut calls = state.calls.lock().unwrap(); |
| 21 | + *calls += 1; |
| 22 | + |
| 23 | + if *calls == 1 { |
| 24 | + return Json(json!({ |
| 25 | + "choices": [{ |
| 26 | + "index": 0, |
| 27 | + "message": { |
| 28 | + "role": "assistant", |
| 29 | + "content": null, |
| 30 | + "tool_calls": [ |
| 31 | + { |
| 32 | + "id": "call_1", |
| 33 | + "type": "function", |
| 34 | + "function": { |
| 35 | + "name": "cron_create", |
| 36 | + "arguments": serde_json::to_string(&json!({ |
| 37 | + "job_id": "job1", |
| 38 | + "name": "Job One", |
| 39 | + "schedule": { "kind": "every", "every_secs": 60 }, |
| 40 | + "action": { "kind": "system_event", "text": "ping" }, |
| 41 | + "one_shot": false |
| 42 | + })).unwrap() |
| 43 | + } |
| 44 | + }, |
| 45 | + { |
| 46 | + "id": "call_2", |
| 47 | + "type": "function", |
| 48 | + "function": { "name": "cron_list", "arguments": "{}" } |
| 49 | + }, |
| 50 | + { |
| 51 | + "id": "call_3", |
| 52 | + "type": "function", |
| 53 | + "function": { |
| 54 | + "name": "cron_cancel", |
| 55 | + "arguments": serde_json::to_string(&json!({ "job_id": "job1" })).unwrap() |
| 56 | + } |
| 57 | + }, |
| 58 | + { |
| 59 | + "id": "call_4", |
| 60 | + "type": "function", |
| 61 | + "function": { "name": "cron_list", "arguments": "{}" } |
| 62 | + } |
| 63 | + ] |
| 64 | + }, |
| 65 | + "finish_reason": "tool_calls" |
| 66 | + }] |
| 67 | + })); |
| 68 | + } |
| 69 | + |
| 70 | + Json(json!({ |
| 71 | + "choices": [{ |
| 72 | + "index": 0, |
| 73 | + "message": { "role": "assistant", "content": "done" }, |
| 74 | + "finish_reason": "stop" |
| 75 | + }] |
| 76 | + })) |
| 77 | + } |
| 78 | + |
| 79 | + let state = TestState::default(); |
| 80 | + let app = Router::new() |
| 81 | + .route("/v1/chat/completions", post(handler)) |
| 82 | + .with_state(state); |
| 83 | + |
| 84 | + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); |
| 85 | + let addr = listener.local_addr().unwrap(); |
| 86 | + let server = tokio::spawn(async move { |
| 87 | + axum::serve(listener, app).await.unwrap(); |
| 88 | + }); |
| 89 | + |
| 90 | + let tmp = tempfile::tempdir().unwrap(); |
| 91 | + let workspace = tmp.path().join("workspace"); |
| 92 | + std::fs::create_dir_all(&workspace).unwrap(); |
| 93 | + |
| 94 | + let home = tmp.path().join("home"); |
| 95 | + let paths = rexos::paths::RexosPaths { |
| 96 | + base_dir: home.join(".rexos"), |
| 97 | + }; |
| 98 | + paths.ensure_dirs().unwrap(); |
| 99 | + |
| 100 | + let memory = rexos::memory::MemoryStore::open_or_create(&paths).unwrap(); |
| 101 | + |
| 102 | + let mut providers = BTreeMap::new(); |
| 103 | + providers.insert( |
| 104 | + "mock".to_string(), |
| 105 | + rexos::config::ProviderConfig { |
| 106 | + kind: rexos::config::ProviderKind::OpenAiCompatible, |
| 107 | + base_url: format!("http://{addr}/v1"), |
| 108 | + api_key_env: "".to_string(), |
| 109 | + default_model: "x".to_string(), |
| 110 | + }, |
| 111 | + ); |
| 112 | + |
| 113 | + let cfg = rexos::config::RexosConfig { |
| 114 | + llm: rexos::config::LlmConfig::default(), |
| 115 | + providers, |
| 116 | + router: rexos::config::RouterConfig::default(), |
| 117 | + }; |
| 118 | + let llms = rexos::llm::registry::LlmRegistry::from_config(&cfg).unwrap(); |
| 119 | + let router = rexos::router::ModelRouter::new(rexos::config::RouterConfig { |
| 120 | + planning: rexos::config::RouteConfig { |
| 121 | + provider: "mock".to_string(), |
| 122 | + model: "x".to_string(), |
| 123 | + }, |
| 124 | + coding: rexos::config::RouteConfig { |
| 125 | + provider: "mock".to_string(), |
| 126 | + model: "x".to_string(), |
| 127 | + }, |
| 128 | + summary: rexos::config::RouteConfig { |
| 129 | + provider: "mock".to_string(), |
| 130 | + model: "x".to_string(), |
| 131 | + }, |
| 132 | + }); |
| 133 | + |
| 134 | + let agent = rexos::agent::AgentRuntime::new(memory, llms, router); |
| 135 | + |
| 136 | + let out = agent |
| 137 | + .run_session( |
| 138 | + workspace, |
| 139 | + "s1", |
| 140 | + None, |
| 141 | + "exercise reserved cron tools", |
| 142 | + rexos::router::TaskKind::Coding, |
| 143 | + ) |
| 144 | + .await |
| 145 | + .unwrap(); |
| 146 | + assert_eq!(out, "done"); |
| 147 | + |
| 148 | + let memory2 = rexos::memory::MemoryStore::open_or_create(&paths).unwrap(); |
| 149 | + let msgs = memory2.list_chat_messages("s1").unwrap(); |
| 150 | + let cron_lists: Vec<String> = msgs |
| 151 | + .iter() |
| 152 | + .filter(|m| { |
| 153 | + m.role == rexos::llm::openai_compat::Role::Tool && m.name.as_deref() == Some("cron_list") |
| 154 | + }) |
| 155 | + .filter_map(|m| m.content.clone()) |
| 156 | + .collect(); |
| 157 | + assert_eq!(cron_lists.len(), 2, "cron_list outputs: {cron_lists:?}"); |
| 158 | + |
| 159 | + let v1: serde_json::Value = |
| 160 | + serde_json::from_str(&cron_lists[0]).expect("cron_list[0] should be json"); |
| 161 | + assert_eq!(v1.as_array().map(|a| a.len()), Some(1), "{v1}"); |
| 162 | + assert_eq!( |
| 163 | + v1[0].get("job_id").and_then(|v| v.as_str()), |
| 164 | + Some("job1"), |
| 165 | + "{v1}" |
| 166 | + ); |
| 167 | + |
| 168 | + let v2: serde_json::Value = |
| 169 | + serde_json::from_str(&cron_lists[1]).expect("cron_list[1] should be json"); |
| 170 | + assert_eq!(v2.as_array().map(|a| a.len()), Some(0), "{v2}"); |
| 171 | + |
| 172 | + server.abort(); |
| 173 | +} |
| 174 | + |
0 commit comments