@tensorspark/dravix-code 0.0.0-stage → 1.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,79 @@
1
+ import * as fs from 'fs';
2
+ import * as path from 'path';
3
+ import * as os from 'os';
4
+ import { createHash } from 'crypto';
5
+ const BASE_DIR = path.join(os.homedir(), '.dravix-code', 'projects');
6
+ function projectDir(cwd) {
7
+ const hash = createHash('sha256').update(cwd).digest('hex').slice(0, 12);
8
+ return path.join(BASE_DIR, hash);
9
+ }
10
+ function convDir(cwd) {
11
+ return path.join(projectDir(cwd), 'conversations');
12
+ }
13
+ function ensureDir(cwd) {
14
+ const dir = convDir(cwd);
15
+ fs.mkdirSync(dir, { recursive: true });
16
+ }
17
+ export function generateId() {
18
+ return Date.now().toString(36) + Math.random().toString(36).slice(2, 9) + Math.random().toString(36).slice(2, 5);
19
+ }
20
+ export function generateTitle(messages) {
21
+ const first = messages.find(m => m.role === 'user');
22
+ if (!first)
23
+ return 'New conversation';
24
+ return String(first.content).replace(/\s+/g, ' ').trim().slice(0, 60) || 'New conversation';
25
+ }
26
+ export function saveConversation(id, title, cwd, messages) {
27
+ const chat = messages.filter(m => m.role === 'user' || m.role === 'assistant');
28
+ if (chat.length === 0)
29
+ return;
30
+ ensureDir(cwd);
31
+ const data = {
32
+ id, title, cwd,
33
+ updatedAt: new Date().toISOString(),
34
+ messageCount: chat.length,
35
+ messages,
36
+ };
37
+ try {
38
+ fs.writeFileSync(path.join(convDir(cwd), `${id}.json`), JSON.stringify(data), 'utf-8');
39
+ }
40
+ catch { /* ignore write errors */ }
41
+ }
42
+ export function loadConversation(id, cwd) {
43
+ try {
44
+ const fp = path.join(convDir(cwd), `${id}.json`);
45
+ if (!fs.existsSync(fp)) {
46
+ // Try legacy global conversations directory
47
+ const legacyPath = path.join(os.homedir(), '.dravix-code', 'conversations', `${id}.json`);
48
+ if (fs.existsSync(legacyPath))
49
+ return JSON.parse(fs.readFileSync(legacyPath, 'utf-8'));
50
+ return null;
51
+ }
52
+ return JSON.parse(fs.readFileSync(fp, 'utf-8'));
53
+ }
54
+ catch {
55
+ return null;
56
+ }
57
+ }
58
+ export function listConversations(cwd, limit = 30) {
59
+ ensureDir(cwd);
60
+ try {
61
+ return fs.readdirSync(convDir(cwd))
62
+ .filter(f => f.endsWith('.json'))
63
+ .map(f => {
64
+ try {
65
+ const c = JSON.parse(fs.readFileSync(path.join(convDir(cwd), f), 'utf-8'));
66
+ return { id: c.id, title: c.title, cwd: c.cwd, updatedAt: c.updatedAt, messageCount: c.messageCount };
67
+ }
68
+ catch {
69
+ return null;
70
+ }
71
+ })
72
+ .filter((c) => c !== null)
73
+ .sort((a, b) => new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime())
74
+ .slice(0, limit);
75
+ }
76
+ catch {
77
+ return [];
78
+ }
79
+ }