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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
// Copyright (c) 2024-2026 Nervosys LLC
// SPDX-License-Identifier: AGPL-3.0-only
//! LM Studio provider for local model inference
//!
//! LM Studio provides a GUI for running local LLMs and exposes
//! an OpenAI-compatible API on port 1234.

#![allow(dead_code)]

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

/// LM Studio provider for local model inference
///
/// LM Studio stores conversation history locally and provides
/// an OpenAI-compatible API typically on port 1234.
pub struct LmStudioProvider {
    /// Path to LM Studio data directory
    data_path: Option<PathBuf>,
    /// API endpoint
    endpoint: String,
    /// Whether LM Studio API is available
    available: bool,
    /// Loaded model name
    model_name: Option<String>,
    /// Path to chat history
    history_path: Option<PathBuf>,
}

/// LM Studio conversation history entry
#[derive(Debug, Deserialize, Serialize)]
struct LmStudioConversation {
    id: String,
    title: Option<String>,
    model: Option<String>,
    created_at: String,
    updated_at: Option<String>,
    messages: Vec<LmStudioMessage>,
}

/// LM Studio message format
#[derive(Debug, Deserialize, Serialize, Clone)]
struct LmStudioMessage {
    role: String,
    content: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    timestamp: Option<String>,
}

/// LM Studio API chat request (OpenAI-compatible)
#[derive(Debug, Serialize)]
struct LmStudioChatRequest {
    model: String,
    messages: Vec<LmStudioMessage>,
    temperature: Option<f32>,
    max_tokens: Option<i32>,
    stream: Option<bool>,
}

/// LM Studio API chat response
#[derive(Debug, Deserialize)]
struct LmStudioChatResponse {
    id: String,
    object: String,
    created: i64,
    model: String,
    choices: Vec<LmStudioChoice>,
    usage: Option<LmStudioUsage>,
}

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

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

/// LM Studio model info
#[derive(Debug, Deserialize)]
struct LmStudioModel {
    id: String,
    object: String,
    owned_by: Option<String>,
}

impl LmStudioProvider {
    /// Discover LM Studio installation
    pub fn discover() -> Option<Self> {
        let data_path = Self::find_lm_studio_data();
        let history_path = data_path.as_ref().and_then(|p| Self::find_history_path(p));
        let endpoint = Self::find_api_endpoint();
        let (available, model_name) = Self::check_api(&endpoint);

        Some(Self {
            data_path,
            endpoint,
            available,
            model_name,
            history_path,
        })
    }

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

        #[cfg(target_os = "windows")]
        {
            if let Some(home) = dirs::home_dir() {
                let path = home.join(".lmstudio");
                if path.exists() {
                    return Some(path);
                }
            }
            if let Some(app_data) = dirs::data_local_dir() {
                let path = app_data.join("LM Studio");
                if path.exists() {
                    return Some(path);
                }
            }
        }

        #[cfg(target_os = "macos")]
        {
            if let Some(home) = dirs::home_dir() {
                let path = home.join(".lmstudio");
                if path.exists() {
                    return Some(path);
                }
                let app_support = home
                    .join("Library")
                    .join("Application Support")
                    .join("LM Studio");
                if app_support.exists() {
                    return Some(app_support);
                }
            }
        }

        #[cfg(target_os = "linux")]
        {
            if let Some(home) = dirs::home_dir() {
                let path = home.join(".lmstudio");
                if path.exists() {
                    return Some(path);
                }
            }
        }

        None
    }

    /// Find chat history path
    fn find_history_path(data_path: &PathBuf) -> Option<PathBuf> {
        let history_path = data_path.join("chat-history");
        if history_path.exists() {
            return Some(history_path);
        }

        let alt_path = data_path.join("conversations");
        if alt_path.exists() {
            return Some(alt_path);
        }

        None
    }

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

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

    /// Check API availability and get loaded model
    fn check_api(endpoint: &str) -> (bool, Option<String>) {
        let url = format!("{}/models", endpoint);
        match ureq::get(&url)
            .timeout(std::time::Duration::from_secs(2))
            .call()
        {
            Ok(response) if response.status() == 200 => {
                if let Ok(json) = response.into_json::<serde_json::Value>() {
                    if let Some(data) = json.get("data").and_then(|d| d.as_array()) {
                        let model = data
                            .first()
                            .and_then(|m| m.get("id"))
                            .and_then(|id| id.as_str())
                            .map(String::from);
                        return (true, model);
                    }
                }
                (true, None)
            }
            _ => (false, None),
        }
    }

    /// List available models
    pub fn list_models(&self) -> Result<Vec<String>> {
        let url = format!("{}/models", self.endpoint);
        let response: serde_json::Value = ureq::get(&url).call()?.into_json()?;

        if let Some(data) = response.get("data").and_then(|d| d.as_array()) {
            return Ok(data
                .iter()
                .filter_map(|m| m.get("id").and_then(|id| id.as_str()))
                .map(String::from)
                .collect());
        }

        Ok(Vec::new())
    }

    /// Load conversations from history
    fn load_conversations(&self) -> Result<Vec<LmStudioConversation>> {
        let history_path = self
            .history_path
            .as_ref()
            .ok_or_else(|| anyhow!("History path not found"))?;

        let mut conversations = Vec::new();

        for entry in std::fs::read_dir(history_path)? {
            let entry = entry?;
            let path = entry.path();

            if path.extension().map(|e| e == "json").unwrap_or(false) {
                let content = std::fs::read_to_string(&path)?;
                if let Ok(conv) = serde_json::from_str::<LmStudioConversation>(&content) {
                    conversations.push(conv);
                }
            }
        }

        // Sort by updated_at/created_at descending
        conversations.sort_by(|a, b| {
            let a_time = a.updated_at.as_ref().unwrap_or(&a.created_at);
            let b_time = b.updated_at.as_ref().unwrap_or(&b.created_at);
            b_time.cmp(a_time)
        });

        Ok(conversations)
    }

    /// Convert LM Studio conversation to ChatSession
    fn convert_to_session(&self, conv: &LmStudioConversation) -> Result<ChatSession> {
        let chat_messages: Vec<ChatMessage> = conv
            .messages
            .iter()
            .enumerate()
            .map(|(i, msg)| {
                let timestamp = msg
                    .timestamp
                    .as_ref()
                    .and_then(|ts| DateTime::parse_from_rfc3339(ts).ok())
                    .map(|dt| dt.with_timezone(&Utc));

                ChatMessage {
                    id: Some(format!("{}_{}", conv.id, i)),
                    role: msg.role.clone(),
                    content: msg.content.clone(),
                    timestamp,
                    metadata: None,
                }
            })
            .collect();

        let created_at = DateTime::parse_from_rfc3339(&conv.created_at)
            .map(|dt| dt.with_timezone(&Utc))
            .unwrap_or_else(|_| Utc::now());

        let updated_at = conv
            .updated_at
            .as_ref()
            .and_then(|ts| DateTime::parse_from_rfc3339(ts).ok())
            .map(|dt| dt.with_timezone(&Utc))
            .unwrap_or(created_at);

        Ok(ChatSession {
            id: conv.id.clone(),
            title: conv
                .title
                .clone()
                .unwrap_or_else(|| "LM Studio Chat".to_string()),
            provider: "lmstudio".to_string(),
            model: conv.model.clone(),
            messages: chat_messages,
            created_at,
            updated_at,
            workspace_id: None,
            metadata: None,
            tags: Vec::new(),
        })
    }

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

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

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

        let url = format!("{}/chat/completions", self.endpoint);
        let response: LmStudioChatResponse = 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 LM Studio"))
    }
}

impl ChatProvider for LmStudioProvider {
    fn provider_type(&self) -> ProviderType {
        ProviderType::LmStudio
    }

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

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

    fn sessions_path(&self) -> Option<PathBuf> {
        self.history_path.clone()
    }

    fn list_sessions(&self) -> Result<Vec<ChatSession>> {
        if self.history_path.is_none() {
            return Ok(Vec::new());
        }

        let conversations = self.load_conversations()?;
        conversations
            .iter()
            .map(|conv| self.convert_to_session(conv))
            .collect()
    }

    fn import_session(&self, session_id: &str) -> Result<ChatSession> {
        let conversations = self.load_conversations()?;

        let conv = conversations
            .iter()
            .find(|c| c.id == session_id)
            .ok_or_else(|| anyhow!("Conversation not found: {}", session_id))?;

        self.convert_to_session(conv)
    }

    fn export_session(&self, session: &ChatSession) -> Result<()> {
        let history_path = self
            .history_path
            .as_ref()
            .ok_or_else(|| anyhow!("History path not found"))?;

        let conv = LmStudioConversation {
            id: session.id.clone(),
            title: Some(session.title.clone()),
            model: session.model.clone(),
            created_at: session.created_at.to_rfc3339(),
            updated_at: Some(session.updated_at.to_rfc3339()),
            messages: session
                .messages
                .iter()
                .map(|m| LmStudioMessage {
                    role: m.role.clone(),
                    content: m.content.clone(),
                    timestamp: m.timestamp.map(|t| t.to_rfc3339()),
                })
                .collect(),
        };

        let file_path = history_path.join(format!("{}.json", session.id));
        let json = serde_json::to_string_pretty(&conv)?;
        std::fs::write(file_path, json)?;

        Ok(())
    }
}

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

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