chasm-cli 2.0.0

Universal chat session manager - harvest, merge, and analyze AI chat history from VS Code, Cursor, and other editors
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
// Copyright (c) 2024-2026 Nervosys LLC
// SPDX-License-Identifier: AGPL-3.0-only
//! LocalAI provider for multi-backend local inference
//!
//! LocalAI is a drop-in OpenAI replacement that supports multiple
//! backends including llama.cpp, whisper.cpp, GPT-J, and more.

#![allow(dead_code)]

use super::{ChatProvider, ProviderType};
use crate::models::{ChatMessage, ChatSession};
use anyhow::{anyhow, Result};
use chrono::Utc;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

/// LocalAI provider for local multi-backend inference
///
/// LocalAI typically runs as a Docker container or standalone binary
/// with an OpenAI-compatible API on port 8080.
pub struct LocalAiProvider {
    /// API endpoint
    endpoint: String,
    /// Whether LocalAI API is available
    available: bool,
    /// List of available models
    models: Vec<String>,
    /// Models directory path
    models_path: Option<PathBuf>,
}

/// LocalAI chat request (OpenAI-compatible)
#[derive(Debug, Serialize)]
struct LocalAiChatRequest {
    model: String,
    messages: Vec<LocalAiMessage>,
    #[serde(skip_serializing_if = "Option::is_none")]
    temperature: Option<f32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    max_tokens: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    stream: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    stop: Option<Vec<String>>,
}

/// LocalAI message format
#[derive(Debug, Deserialize, Serialize, Clone)]
struct LocalAiMessage {
    role: String,
    content: String,
}

/// LocalAI chat response
#[derive(Debug, Deserialize)]
struct LocalAiChatResponse {
    id: Option<String>,
    object: String,
    created: i64,
    model: String,
    choices: Vec<LocalAiChoice>,
    #[serde(default)]
    usage: Option<LocalAiUsage>,
}

#[derive(Debug, Deserialize)]
struct LocalAiChoice {
    index: i32,
    message: LocalAiMessage,
    finish_reason: Option<String>,
}

#[derive(Debug, Deserialize)]
struct LocalAiUsage {
    prompt_tokens: i32,
    completion_tokens: i32,
    total_tokens: i32,
}

/// LocalAI model info
#[derive(Debug, Deserialize)]
struct LocalAiModel {
    id: String,
    object: String,
    #[serde(default)]
    owned_by: Option<String>,
}

/// LocalAI models list response
#[derive(Debug, Deserialize)]
struct LocalAiModelsResponse {
    object: String,
    data: Vec<LocalAiModel>,
}

/// LocalAI backend info
#[derive(Debug, Deserialize)]
struct LocalAiBackendInfo {
    backend: String,
    #[serde(default)]
    available: bool,
}

impl LocalAiProvider {
    /// Discover LocalAI installation
    pub fn discover() -> Option<Self> {
        let endpoint = Self::find_api_endpoint();
        let (available, models) = Self::check_api(&endpoint);
        let models_path = Self::find_models_path();

        Some(Self {
            endpoint,
            available,
            models,
            models_path,
        })
    }

    /// Find API endpoint
    fn find_api_endpoint() -> String {
        // Check environment variable
        if let Ok(endpoint) = std::env::var("LOCALAI_API") {
            return endpoint;
        }

        // Check common alternate endpoints
        if let Ok(endpoint) = std::env::var("OPENAI_API_BASE") {
            if endpoint.contains("localhost") || endpoint.contains("127.0.0.1") {
                return endpoint;
            }
        }

        // Default endpoint
        "http://localhost:8080/v1".to_string()
    }

    /// Check API availability
    fn check_api(endpoint: &str) -> (bool, Vec<String>) {
        let url = format!("{}/models", endpoint);
        match ureq::get(&url)
            .timeout(std::time::Duration::from_secs(3))
            .call()
        {
            Ok(response) if response.status() == 200 => {
                if let Ok(models_resp) = response.into_json::<LocalAiModelsResponse>() {
                    let models: Vec<String> =
                        models_resp.data.iter().map(|m| m.id.clone()).collect();
                    return (true, models);
                }
                (true, Vec::new())
            }
            _ => (false, Vec::new()),
        }
    }

    /// Find models directory
    fn find_models_path() -> Option<PathBuf> {
        // Check environment variable
        if let Ok(path) = std::env::var("LOCALAI_MODELS") {
            let path = PathBuf::from(path);
            if path.exists() {
                return Some(path);
            }
        }

        // Check common locations
        if let Some(home) = dirs::home_dir() {
            let path = home.join(".local-ai").join("models");
            if path.exists() {
                return Some(path);
            }

            let path = home.join("localai").join("models");
            if path.exists() {
                return Some(path);
            }
        }

        None
    }

    /// List available models
    pub fn list_models(&self) -> Result<Vec<String>> {
        if !self.available {
            return Ok(self.models.clone());
        }

        let url = format!("{}/models", self.endpoint);
        let response: LocalAiModelsResponse = ureq::get(&url).call()?.into_json()?;

        Ok(response.data.iter().map(|m| m.id.clone()).collect())
    }

    /// Get system info/status
    pub fn get_system_info(&self) -> Result<serde_json::Value> {
        let url = self.endpoint.replace("/v1", "/system");
        let response: serde_json::Value = ureq::get(&url).call()?.into_json()?;
        Ok(response)
    }

    /// Send a chat message
    pub fn chat(&self, model: &str, messages: &[ChatMessage]) -> Result<String> {
        if !self.available {
            return Err(anyhow!("LocalAI API not available"));
        }

        let api_messages: Vec<LocalAiMessage> = messages
            .iter()
            .map(|m| LocalAiMessage {
                role: m.role.clone(),
                content: m.content.clone(),
            })
            .collect();

        let request = LocalAiChatRequest {
            model: model.to_string(),
            messages: api_messages,
            temperature: Some(0.7),
            max_tokens: Some(2048),
            stream: Some(false),
            stop: None,
        };

        let url = format!("{}/chat/completions", self.endpoint);
        let response: LocalAiChatResponse = ureq::post(&url)
            .set("Content-Type", "application/json")
            .send_json(&request)?
            .into_json()?;

        response
            .choices
            .first()
            .map(|c| c.message.content.clone())
            .ok_or_else(|| anyhow!("No response from LocalAI"))
    }

    /// Generate text completion (non-chat)
    pub fn complete(&self, model: &str, prompt: &str) -> Result<String> {
        if !self.available {
            return Err(anyhow!("LocalAI API not available"));
        }

        #[derive(Serialize)]
        struct CompletionRequest {
            model: String,
            prompt: String,
            max_tokens: i32,
            temperature: f32,
        }

        #[derive(Deserialize)]
        struct CompletionResponse {
            choices: Vec<CompletionChoice>,
        }

        #[derive(Deserialize)]
        struct CompletionChoice {
            text: String,
        }

        let request = CompletionRequest {
            model: model.to_string(),
            prompt: prompt.to_string(),
            max_tokens: 512,
            temperature: 0.7,
        };

        let url = format!("{}/completions", self.endpoint);
        let response: CompletionResponse = ureq::post(&url)
            .set("Content-Type", "application/json")
            .send_json(&request)?
            .into_json()?;

        response
            .choices
            .first()
            .map(|c| c.text.clone())
            .ok_or_else(|| anyhow!("No completion from LocalAI"))
    }

    /// Generate embeddings
    pub fn embeddings(&self, model: &str, input: &[String]) -> Result<Vec<Vec<f32>>> {
        if !self.available {
            return Err(anyhow!("LocalAI API not available"));
        }

        #[derive(Serialize)]
        struct EmbeddingsRequest {
            model: String,
            input: Vec<String>,
        }

        #[derive(Deserialize)]
        struct EmbeddingsResponse {
            data: Vec<EmbeddingData>,
        }

        #[derive(Deserialize)]
        struct EmbeddingData {
            embedding: Vec<f32>,
        }

        let request = EmbeddingsRequest {
            model: model.to_string(),
            input: input.to_vec(),
        };

        let url = format!("{}/embeddings", self.endpoint);
        let response: EmbeddingsResponse = ureq::post(&url)
            .set("Content-Type", "application/json")
            .send_json(&request)?
            .into_json()?;

        Ok(response.data.iter().map(|d| d.embedding.clone()).collect())
    }

    /// Transcribe audio (Whisper backend)
    pub fn transcribe(&self, model: &str, audio_path: &str) -> Result<String> {
        if !self.available {
            return Err(anyhow!("LocalAI API not available"));
        }

        let audio_data = std::fs::read(audio_path)?;
        let file_name = std::path::Path::new(audio_path)
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or("audio.wav");

        let url = format!("{}/audio/transcriptions", self.endpoint);

        // Create multipart form
        use std::io::Cursor;
        let boundary = "----WebKitFormBoundary7MA4YWxkTrZu0gW";
        let mut body = Vec::new();

        // Add model field
        body.extend_from_slice(format!("--{}\r\n", boundary).as_bytes());
        body.extend_from_slice(b"Content-Disposition: form-data; name=\"model\"\r\n\r\n");
        body.extend_from_slice(model.as_bytes());
        body.extend_from_slice(b"\r\n");

        // Add file field
        body.extend_from_slice(format!("--{}\r\n", boundary).as_bytes());
        body.extend_from_slice(
            format!(
                "Content-Disposition: form-data; name=\"file\"; filename=\"{}\"\r\n",
                file_name
            )
            .as_bytes(),
        );
        body.extend_from_slice(b"Content-Type: audio/wav\r\n\r\n");
        body.extend_from_slice(&audio_data);
        body.extend_from_slice(b"\r\n");

        // End boundary
        body.extend_from_slice(format!("--{}--\r\n", boundary).as_bytes());

        #[derive(Deserialize)]
        struct TranscriptionResponse {
            text: String,
        }

        let response: TranscriptionResponse = ureq::post(&url)
            .set(
                "Content-Type",
                &format!("multipart/form-data; boundary={}", boundary),
            )
            .send_bytes(&body)?
            .into_json()?;

        Ok(response.text)
    }
}

impl ChatProvider for LocalAiProvider {
    fn provider_type(&self) -> ProviderType {
        ProviderType::LocalAi
    }

    fn name(&self) -> &str {
        "LocalAI"
    }

    fn is_available(&self) -> bool {
        self.available
    }

    fn sessions_path(&self) -> Option<PathBuf> {
        // LocalAI is stateless - no session storage
        None
    }

    fn list_sessions(&self) -> Result<Vec<ChatSession>> {
        // LocalAI doesn't store conversations
        Ok(Vec::new())
    }

    fn import_session(&self, _session_id: &str) -> Result<ChatSession> {
        Err(anyhow!("LocalAI does not store conversation history"))
    }
}

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

    #[test]
    fn test_discover() {
        let provider = LocalAiProvider::discover();
        println!("LocalAI discovered: {:?}", provider.is_some());
    }

    #[test]
    fn test_endpoint_format() {
        let endpoint = LocalAiProvider::find_api_endpoint();
        assert!(endpoint.contains("localhost") || endpoint.contains("127.0.0.1"));
    }
}