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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
// Copyright (c) 2024-2026 Nervosys LLC
// SPDX-License-Identifier: AGPL-3.0-only
//! GPT4All provider for local LLM inference
//!
//! GPT4All is a free-to-use, locally running, privacy-aware chatbot.
//! It supports many models and stores conversation history locally.

#![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;
use uuid::Uuid;

/// GPT4All provider for local LLM chat
///
/// GPT4All stores conversations in a local SQLite database and
/// provides a REST API when running.
pub struct Gpt4AllProvider {
    /// Path to GPT4All data directory
    data_path: PathBuf,
    /// API endpoint (when GPT4All API server is running)
    endpoint: Option<String>,
    /// Whether GPT4All is available
    available: bool,
    /// Path to conversation database
    db_path: Option<PathBuf>,
}

/// GPT4All conversation from database
#[derive(Debug, Deserialize, Serialize)]
struct Gpt4AllConversation {
    id: i64,
    name: Option<String>,
    created_at: String,
    updated_at: String,
    model: Option<String>,
}

/// GPT4All message from database
#[derive(Debug, Deserialize, Serialize)]
struct Gpt4AllMessage {
    id: i64,
    conversation_id: i64,
    role: String,
    content: String,
    created_at: String,
    model: Option<String>,
    tokens: Option<i64>,
}

/// GPT4All API chat request
#[derive(Debug, Serialize)]
struct Gpt4AllChatRequest {
    model: String,
    messages: Vec<Gpt4AllApiMessage>,
    temperature: f32,
    max_tokens: i32,
}

/// GPT4All API message format
#[derive(Debug, Serialize, Deserialize)]
struct Gpt4AllApiMessage {
    role: String,
    content: String,
}

/// GPT4All API response
#[derive(Debug, Deserialize)]
struct Gpt4AllChatResponse {
    choices: Vec<Gpt4AllChoice>,
    usage: Option<Gpt4AllUsage>,
}

#[derive(Debug, Deserialize)]
struct Gpt4AllChoice {
    message: Gpt4AllApiMessage,
    finish_reason: Option<String>,
}

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

/// GPT4All model info
#[derive(Debug, Deserialize)]
struct Gpt4AllModel {
    id: String,
    object: String,
    name: Option<String>,
}

impl Gpt4AllProvider {
    /// Discover GPT4All installation and create provider
    pub fn discover() -> Option<Self> {
        let data_path = Self::find_gpt4all_data()?;
        let db_path = Self::find_database(&data_path);
        let endpoint = Self::find_api_endpoint();
        let available = db_path.is_some() || endpoint.is_some();

        Some(Self {
            data_path,
            endpoint,
            available,
            db_path,
        })
    }

    /// Find GPT4All's data directory
    fn find_gpt4all_data() -> Option<PathBuf> {
        // Check environment variable first
        if let Ok(path) = std::env::var("GPT4ALL_DATA") {
            let path = PathBuf::from(path);
            if path.exists() {
                return Some(path);
            }
        }

        // Platform-specific default locations
        #[cfg(target_os = "windows")]
        {
            if let Some(local_app_data) = dirs::data_local_dir() {
                let path = local_app_data.join("nomic.ai").join("GPT4All");
                if path.exists() {
                    return Some(path);
                }
            }
        }

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

        #[cfg(target_os = "linux")]
        {
            if let Some(home) = dirs::home_dir() {
                // Check ~/.local/share/nomic.ai/GPT4All
                let path = home
                    .join(".local")
                    .join("share")
                    .join("nomic.ai")
                    .join("GPT4All");
                if path.exists() {
                    return Some(path);
                }

                // Alternative: ~/.gpt4all
                let alt_path = home.join(".gpt4all");
                if alt_path.exists() {
                    return Some(alt_path);
                }
            }
        }

        None
    }

    /// Find conversation database
    fn find_database(data_path: &PathBuf) -> Option<PathBuf> {
        // GPT4All stores conversations in a SQLite database
        let db_path = data_path.join("chats.db");
        if db_path.exists() {
            return Some(db_path);
        }

        // Alternative location
        let alt_db = data_path.join("conversations.db");
        if alt_db.exists() {
            return Some(alt_db);
        }

        None
    }

    /// Find API endpoint if GPT4All server is running
    fn find_api_endpoint() -> Option<String> {
        // Check environment variable
        if let Ok(endpoint) = std::env::var("GPT4ALL_API") {
            return Some(endpoint);
        }

        // Default API endpoint
        let default_endpoint = "http://localhost:4891/v1";

        // Check if API is responding
        if Self::check_api_availability(default_endpoint) {
            return Some(default_endpoint.to_string());
        }

        None
    }

    /// Check if API endpoint is available
    fn check_api_availability(endpoint: &str) -> bool {
        // Simple connectivity check
        let url = format!("{}/models", endpoint);
        match ureq::get(&url)
            .timeout(std::time::Duration::from_secs(2))
            .call()
        {
            Ok(response) => response.status() == 200,
            Err(_) => false,
        }
    }

    /// List available models
    pub fn list_models(&self) -> Result<Vec<String>> {
        if let Some(ref endpoint) = self.endpoint {
            let url = format!("{}/models", 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());
            }
        }

        // Fall back to scanning models directory
        let models_dir = self.data_path.join("models");
        if models_dir.exists() {
            let models: Vec<String> = std::fs::read_dir(models_dir)?
                .filter_map(|entry| entry.ok())
                .filter(|entry| {
                    entry
                        .path()
                        .extension()
                        .map(|e| e == "gguf" || e == "bin")
                        .unwrap_or(false)
                })
                .filter_map(|entry| {
                    entry
                        .path()
                        .file_stem()
                        .map(|s| s.to_string_lossy().to_string())
                })
                .collect();
            return Ok(models);
        }

        Ok(Vec::new())
    }

    /// Load conversations from database
    fn load_conversations(&self) -> Result<Vec<Gpt4AllConversation>> {
        let db_path = self
            .db_path
            .as_ref()
            .ok_or_else(|| anyhow!("No database found"))?;

        let conn = rusqlite::Connection::open(db_path)?;

        let mut stmt = conn.prepare(
            "SELECT id, name, created_at, updated_at, model FROM conversations ORDER BY updated_at DESC",
        )?;

        let conversations = stmt
            .query_map([], |row| {
                Ok(Gpt4AllConversation {
                    id: row.get(0)?,
                    name: row.get(1)?,
                    created_at: row.get(2)?,
                    updated_at: row.get(3)?,
                    model: row.get(4)?,
                })
            })?
            .filter_map(|r| r.ok())
            .collect();

        Ok(conversations)
    }

    /// Load messages for a conversation
    fn load_messages(&self, conversation_id: i64) -> Result<Vec<Gpt4AllMessage>> {
        let db_path = self
            .db_path
            .as_ref()
            .ok_or_else(|| anyhow!("No database found"))?;

        let conn = rusqlite::Connection::open(db_path)?;

        let mut stmt = conn.prepare(
            "SELECT id, conversation_id, role, content, created_at, model, tokens 
             FROM messages 
             WHERE conversation_id = ? 
             ORDER BY created_at ASC",
        )?;

        let messages = stmt
            .query_map([conversation_id], |row| {
                Ok(Gpt4AllMessage {
                    id: row.get(0)?,
                    conversation_id: row.get(1)?,
                    role: row.get(2)?,
                    content: row.get(3)?,
                    created_at: row.get(4)?,
                    model: row.get(5)?,
                    tokens: row.get(6)?,
                })
            })?
            .filter_map(|r| r.ok())
            .collect();

        Ok(messages)
    }

    /// Convert GPT4All conversation to ChatSession
    fn convert_to_session(&self, conv: &Gpt4AllConversation) -> Result<ChatSession> {
        let messages = self.load_messages(conv.id)?;

        let chat_messages: Vec<ChatMessage> = messages
            .iter()
            .map(|msg| ChatMessage {
                id: Some(msg.id.to_string()),
                role: msg.role.clone(),
                content: msg.content.clone(),
                timestamp: DateTime::parse_from_rfc3339(&msg.created_at)
                    .map(|dt| dt.with_timezone(&Utc))
                    .ok(),
                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 = DateTime::parse_from_rfc3339(&conv.updated_at)
            .map(|dt| dt.with_timezone(&Utc))
            .unwrap_or_else(|_| Utc::now());

        Ok(ChatSession {
            id: conv.id.to_string(),
            title: conv
                .name
                .clone()
                .unwrap_or_else(|| "GPT4All Chat".to_string()),
            provider: "gpt4all".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 via API
    pub fn chat(&self, model: &str, messages: &[ChatMessage]) -> Result<String> {
        let endpoint = self
            .endpoint
            .as_ref()
            .ok_or_else(|| anyhow!("GPT4All API not available"))?;

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

        let request = Gpt4AllChatRequest {
            model: model.to_string(),
            messages: api_messages,
            temperature: 0.7,
            max_tokens: 2048,
        };

        let url = format!("{}/chat/completions", endpoint);
        let response: Gpt4AllChatResponse = 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 GPT4All"))
    }
}

impl ChatProvider for Gpt4AllProvider {
    fn provider_type(&self) -> ProviderType {
        ProviderType::Gpt4All
    }

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

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

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

    fn list_sessions(&self) -> Result<Vec<ChatSession>> {
        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 conv_id: i64 = session_id.parse()?;
        let conversations = self.load_conversations()?;

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

        self.convert_to_session(conv)
    }

    fn export_session(&self, _session: &ChatSession) -> Result<()> {
        // GPT4All doesn't support importing external conversations easily
        Err(anyhow!("Export to GPT4All is not supported"))
    }
}

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

    #[test]
    fn test_discover() {
        // This test will only pass if GPT4All is installed
        let provider = Gpt4AllProvider::discover();
        // Provider discovery should not panic
        println!("GPT4All discovered: {:?}", provider.is_some());
    }
}