mullama 0.3.0

Comprehensive Rust bindings for llama.cpp with memory-safe API and advanced features
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
//! Daemon Auto-Spawn Functionality
//!
//! Provides utilities to automatically spawn the mullama daemon when needed.
//! This enables seamless usage where users can run `mullama run llama3.2:1b`
//! without first starting the daemon manually.

use std::path::PathBuf;
use std::process::{Command, Stdio};
use std::time::Duration;

use serde::{Deserialize, Serialize};

use super::client::DaemonClient;
use super::DEFAULT_SOCKET;

/// Configuration for auto-spawning the daemon
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SpawnConfig {
    /// Path to the mullama binary
    pub binary_path: Option<PathBuf>,

    /// Socket address
    pub socket: String,

    /// HTTP port (0 to disable)
    pub http_port: u16,

    /// HTTP bind address
    pub http_addr: String,

    /// API key for HTTP endpoints
    pub api_key: Option<String>,

    /// Always require API key auth, even when bound to localhost
    pub require_api_key: bool,

    /// Default GPU layers
    pub gpu_layers: i32,

    /// Default context size
    pub context_size: u32,

    /// Number of contexts in each loaded model pool
    pub context_pool_size: usize,

    /// Timeout for waiting for daemon to be ready
    #[serde(skip)]
    pub startup_timeout: Duration,

    /// Whether to run in background (daemonize)
    pub background: bool,

    /// Log file path (for background mode)
    pub log_file: Option<PathBuf>,

    /// Enable flash attention
    pub flash_attn: bool,
    /// KV cache type for keys
    pub cache_type_k: Option<String>,
    /// KV cache type for values
    pub cache_type_v: Option<String>,
}

impl Default for SpawnConfig {
    fn default() -> Self {
        Self {
            binary_path: None,
            socket: DEFAULT_SOCKET.to_string(),
            http_port: 8080,
            http_addr: "127.0.0.1".to_string(),
            api_key: None,
            require_api_key: false,
            gpu_layers: 0,
            context_size: 4096,
            context_pool_size: super::models::DEFAULT_CONTEXT_POOL_SIZE,
            startup_timeout: Duration::from_secs(30),
            background: true,
            log_file: None,
            flash_attn: false,
            cache_type_k: None,
            cache_type_v: None,
        }
    }
}

impl SpawnConfig {
    fn config_dir() -> PathBuf {
        dirs::cache_dir()
            .unwrap_or_else(|| PathBuf::from("."))
            .join("mullama")
    }

    fn config_path() -> PathBuf {
        Self::config_dir().join("daemon_config.json")
    }

    pub fn save(&self) -> Result<(), String> {
        let dir = Self::config_dir();
        std::fs::create_dir_all(&dir).map_err(|e| format!("Failed to create config dir: {}", e))?;
        let json = serde_json::to_string_pretty(self)
            .map_err(|e| format!("Failed to serialize config: {}", e))?;
        std::fs::write(Self::config_path(), json)
            .map_err(|e| format!("Failed to write config: {}", e))
    }

    pub fn load() -> Option<Self> {
        let path = Self::config_path();
        let content = std::fs::read_to_string(&path).ok()?;
        serde_json::from_str(&content).ok()
    }
}

/// Result of a spawn operation
#[derive(Debug)]
pub enum SpawnResult {
    /// Daemon was already running
    AlreadyRunning,

    /// Daemon was spawned successfully
    Spawned {
        /// Process ID (if spawned in foreground)
        pid: Option<u32>,
    },

    /// Failed to spawn
    Failed(String),
}

/// Check if the daemon is running
pub fn is_daemon_running(socket: &str) -> bool {
    match DaemonClient::connect_with_timeout(socket, Duration::from_millis(500)) {
        Ok(client) => client.ping().is_ok(),
        Err(_) => false,
    }
}

/// Ensure the daemon is running, spawning it if necessary
pub fn ensure_daemon_running(config: &SpawnConfig) -> Result<(), String> {
    if is_daemon_running(&config.socket) {
        return Ok(());
    }

    match spawn_daemon(config) {
        SpawnResult::AlreadyRunning => Ok(()),
        SpawnResult::Spawned { .. } => {
            // Wait for daemon to be ready
            wait_for_daemon(&config.socket, config.startup_timeout)
        }
        SpawnResult::Failed(e) => Err(e),
    }
}

/// Spawn the daemon
pub fn spawn_daemon(config: &SpawnConfig) -> SpawnResult {
    // Check if already running
    if is_daemon_running(&config.socket) {
        return SpawnResult::AlreadyRunning;
    }

    // Find the mullama binary
    let binary = find_mullama_binary(config.binary_path.as_ref());
    let binary = match binary {
        Some(b) => b,
        None => return SpawnResult::Failed("Could not find mullama binary".to_string()),
    };

    // Build command
    let mut cmd = Command::new(&binary);
    cmd.arg("serve");
    cmd.arg("--socket").arg(&config.socket);
    cmd.arg("--http-port").arg(config.http_port.to_string());
    cmd.arg("--http-addr").arg(&config.http_addr);
    cmd.arg("--gpu-layers").arg(config.gpu_layers.to_string());
    cmd.arg("--context-size")
        .arg(config.context_size.to_string());
    cmd.arg("--context-pool-size")
        .arg(config.context_pool_size.to_string());
    if let Some(api_key) = &config.api_key {
        cmd.arg("--api-key").arg(api_key);
    }
    if config.require_api_key {
        cmd.arg("--require-api-key");
    }
    if config.flash_attn {
        cmd.arg("--flash-attn");
    }
    if let Some(ref k) = config.cache_type_k {
        cmd.arg("--cache-type-k").arg(k);
    }
    if let Some(ref v) = config.cache_type_v {
        cmd.arg("--cache-type-v").arg(v);
    }

    if config.background {
        // Spawn in background
        cmd.stdin(Stdio::null());

        if let Some(ref log_file) = config.log_file {
            // Create parent directory if needed
            if let Some(parent) = log_file.parent() {
                let _ = std::fs::create_dir_all(parent);
            }

            match std::fs::File::create(log_file) {
                Ok(file) => {
                    match file.try_clone() {
                        Ok(stdout_file) => {
                            cmd.stdout(Stdio::from(stdout_file));
                        }
                        Err(_) => {
                            cmd.stdout(Stdio::null());
                        }
                    }
                    cmd.stderr(Stdio::from(file));
                }
                Err(_) => {
                    cmd.stdout(Stdio::null());
                    cmd.stderr(Stdio::null());
                }
            }
        } else {
            cmd.stdout(Stdio::null());
            cmd.stderr(Stdio::null());
        }

        // Platform-specific daemonization
        #[cfg(unix)]
        {
            use std::os::unix::process::CommandExt;
            // Create new process group
            cmd.process_group(0);
        }

        match cmd.spawn() {
            Ok(child) => SpawnResult::Spawned {
                pid: Some(child.id()),
            },
            Err(e) => SpawnResult::Failed(format!("Failed to spawn daemon: {}", e)),
        }
    } else {
        // Spawn in foreground (for debugging)
        cmd.stdout(Stdio::inherit());
        cmd.stderr(Stdio::inherit());

        match cmd.spawn() {
            Ok(child) => SpawnResult::Spawned {
                pid: Some(child.id()),
            },
            Err(e) => SpawnResult::Failed(format!("Failed to spawn daemon: {}", e)),
        }
    }
}

/// Find the mullama binary
fn find_mullama_binary(override_path: Option<&PathBuf>) -> Option<PathBuf> {
    // Use override if provided
    if let Some(path) = override_path {
        if path.exists() {
            return Some(path.clone());
        }
    }

    // Check MULLAMA_BIN environment variable
    if let Ok(path) = std::env::var("MULLAMA_BIN") {
        let p = PathBuf::from(&path);
        if p.exists() {
            return Some(p);
        }
    }

    // Check current executable's directory
    if let Ok(exe) = std::env::current_exe() {
        if let Some(dir) = exe.parent() {
            let candidate = dir.join("mullama");
            if candidate.exists() {
                return Some(candidate);
            }
            #[cfg(windows)]
            {
                let candidate = dir.join("mullama.exe");
                if candidate.exists() {
                    return Some(candidate);
                }
            }
        }
    }

    // Check PATH
    if let Ok(output) = Command::new("which").arg("mullama").output() {
        if output.status.success() {
            let path = String::from_utf8_lossy(&output.stdout).trim().to_string();
            if !path.is_empty() {
                return Some(PathBuf::from(path));
            }
        }
    }

    // Check common locations
    let common_paths = [
        "/usr/local/bin/mullama",
        "/usr/bin/mullama",
        "~/.local/bin/mullama",
        "~/.cargo/bin/mullama",
    ];

    for path in common_paths {
        let expanded = if path.starts_with('~') {
            if let Some(home) = dirs::home_dir() {
                home.join(&path[2..])
            } else {
                PathBuf::from(path)
            }
        } else {
            PathBuf::from(path)
        };

        if expanded.exists() {
            return Some(expanded);
        }
    }

    None
}

/// Wait for the daemon to be ready
fn wait_for_daemon(socket: &str, timeout: Duration) -> Result<(), String> {
    let start = std::time::Instant::now();
    let poll_interval = Duration::from_millis(100);

    while start.elapsed() < timeout {
        if is_daemon_running(socket) {
            return Ok(());
        }
        std::thread::sleep(poll_interval);
    }

    Err(format!(
        "Daemon did not start within {} seconds",
        timeout.as_secs()
    ))
}

/// Stop the daemon
pub fn stop_daemon(socket: &str) -> Result<(), String> {
    match DaemonClient::connect_with_timeout(socket, Duration::from_secs(5)) {
        Ok(client) => client
            .shutdown()
            .map_err(|e| format!("Failed to shutdown daemon: {}", e)),
        Err(_) => Ok(()), // Already not running
    }
}

/// Get daemon status
pub fn daemon_status(socket: &str) -> Result<DaemonInfo, String> {
    let client = DaemonClient::connect_with_timeout(socket, Duration::from_secs(5))
        .map_err(|e| format!("Failed to connect: {}", e))?;

    let (uptime, version) = client
        .ping()
        .map_err(|e| format!("Failed to ping: {}", e))?;

    let status = client
        .status()
        .map_err(|e| format!("Failed to get status: {}", e))?;

    Ok(DaemonInfo {
        running: true,
        version,
        uptime_secs: uptime,
        models_loaded: status.models_loaded as u32,
        socket: socket.to_string(),
        http_endpoint: status.http_endpoint,
    })
}

/// Daemon information
#[derive(Debug, Clone)]
pub struct DaemonInfo {
    pub running: bool,
    pub version: String,
    pub uptime_secs: u64,
    pub models_loaded: u32,
    pub socket: String,
    pub http_endpoint: Option<String>,
}

/// Get the default log file path
pub fn default_log_path() -> PathBuf {
    if cfg!(target_os = "macos") {
        PathBuf::from("/tmp/mullamad.log")
    } else if cfg!(windows) {
        dirs::data_local_dir()
            .unwrap_or_else(|| PathBuf::from("."))
            .join("mullama")
            .join("mullamad.log")
    } else {
        // Linux and other Unix
        PathBuf::from("/tmp/mullamad.log")
    }
}

/// Get the default PID file path
pub fn default_pid_path() -> PathBuf {
    if cfg!(target_os = "macos") {
        PathBuf::from("/tmp/mullamad.pid")
    } else if cfg!(windows) {
        dirs::data_local_dir()
            .unwrap_or_else(|| PathBuf::from("."))
            .join("mullama")
            .join("mullamad.pid")
    } else {
        // Linux and other Unix - try XDG_RUNTIME_DIR first
        if let Ok(runtime_dir) = std::env::var("XDG_RUNTIME_DIR") {
            PathBuf::from(runtime_dir).join("mullamad.pid")
        } else {
            PathBuf::from("/tmp/mullamad.pid")
        }
    }
}

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

    #[test]
    fn test_find_binary() {
        // Just test that it doesn't panic
        let _ = find_mullama_binary(None);
    }

    #[test]
    fn test_spawn_config_default() {
        let config = SpawnConfig::default();
        assert_eq!(config.http_port, 8080);
        assert!(config.background);
    }
}