nothumanallowed 9.7.2 → 9.8.0

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,277 @@
1
+ /**
2
+ * Multi-conversation manager for NHA Chat.
3
+ *
4
+ * Stores conversations in ~/.nha/conversations/ as JSON files.
5
+ * Each conversation has an ID, auto-generated title, and message history.
6
+ *
7
+ * Zero npm dependencies.
8
+ */
9
+
10
+ import fs from 'fs';
11
+ import path from 'path';
12
+ import crypto from 'crypto';
13
+ import { NHA_DIR } from '../constants.mjs';
14
+
15
+ const CONVERSATIONS_DIR = path.join(NHA_DIR, 'conversations');
16
+ const ACTIVE_FILE = path.join(CONVERSATIONS_DIR, '.active');
17
+ const MAX_TITLE_LENGTH = 60;
18
+ const MAX_CONVERSATIONS = 100;
19
+
20
+ // ── Helpers ──────────────────────────────────────────────────────────────────
21
+
22
+ function ensureDir() {
23
+ fs.mkdirSync(CONVERSATIONS_DIR, { recursive: true });
24
+ }
25
+
26
+ function convPath(id) {
27
+ return path.join(CONVERSATIONS_DIR, `${id}.json`);
28
+ }
29
+
30
+ function generateId() {
31
+ return crypto.randomUUID().slice(0, 8);
32
+ }
33
+
34
+ /**
35
+ * Auto-generate a title from the first user message.
36
+ * Takes the first ~50 chars, trims to last word boundary.
37
+ */
38
+ function autoTitle(firstMessage) {
39
+ if (!firstMessage) return 'New Chat';
40
+ let title = firstMessage.replace(/\s+/g, ' ').trim();
41
+ if (title.length > MAX_TITLE_LENGTH) {
42
+ title = title.slice(0, MAX_TITLE_LENGTH);
43
+ const lastSpace = title.lastIndexOf(' ');
44
+ if (lastSpace > 20) title = title.slice(0, lastSpace);
45
+ title += '...';
46
+ }
47
+ return title;
48
+ }
49
+
50
+ // ── CRUD ─────────────────────────────────────────────────────────────────────
51
+
52
+ /**
53
+ * Create a new conversation and set it as active.
54
+ * @returns {{ id: string, title: string, messages: Array, createdAt: string, updatedAt: string }}
55
+ */
56
+ export function createConversation(title = '') {
57
+ ensureDir();
58
+ const id = generateId();
59
+ const now = new Date().toISOString();
60
+ const conv = {
61
+ id,
62
+ title: title || 'New Chat',
63
+ messages: [],
64
+ createdAt: now,
65
+ updatedAt: now,
66
+ };
67
+ fs.writeFileSync(convPath(id), JSON.stringify(conv, null, 2) + '\n', 'utf-8');
68
+ setActiveId(id);
69
+ return conv;
70
+ }
71
+
72
+ /**
73
+ * Load a conversation by ID.
74
+ * @returns {object|null}
75
+ */
76
+ export function loadConversation(id) {
77
+ try {
78
+ const data = fs.readFileSync(convPath(id), 'utf-8');
79
+ return JSON.parse(data);
80
+ } catch {
81
+ return null;
82
+ }
83
+ }
84
+
85
+ /**
86
+ * Save a conversation (full overwrite).
87
+ */
88
+ export function saveConversation(conv) {
89
+ ensureDir();
90
+ conv.updatedAt = new Date().toISOString();
91
+ fs.writeFileSync(convPath(conv.id), JSON.stringify(conv, null, 2) + '\n', 'utf-8');
92
+ }
93
+
94
+ /**
95
+ * Delete a conversation by ID.
96
+ * If it was active, clears active state.
97
+ * @returns {boolean} true if deleted
98
+ */
99
+ export function deleteConversation(id) {
100
+ const filePath = convPath(id);
101
+ if (!fs.existsSync(filePath)) return false;
102
+ fs.unlinkSync(filePath);
103
+ if (getActiveId() === id) {
104
+ clearActiveId();
105
+ }
106
+ return true;
107
+ }
108
+
109
+ /**
110
+ * List all conversations, sorted by updatedAt (newest first).
111
+ * Returns summary objects (no messages).
112
+ * @returns {Array<{ id: string, title: string, messageCount: number, createdAt: string, updatedAt: string }>}
113
+ */
114
+ export function listConversations() {
115
+ ensureDir();
116
+ const files = fs.readdirSync(CONVERSATIONS_DIR)
117
+ .filter(f => f.endsWith('.json') && !f.startsWith('.'));
118
+
119
+ const convs = [];
120
+ for (const f of files) {
121
+ try {
122
+ const data = JSON.parse(fs.readFileSync(path.join(CONVERSATIONS_DIR, f), 'utf-8'));
123
+ convs.push({
124
+ id: data.id,
125
+ title: data.title || 'Untitled',
126
+ messageCount: (data.messages || []).length,
127
+ createdAt: data.createdAt,
128
+ updatedAt: data.updatedAt,
129
+ });
130
+ } catch { /* skip corrupt files */ }
131
+ }
132
+
133
+ convs.sort((a, b) => new Date(b.updatedAt) - new Date(a.updatedAt));
134
+
135
+ // Auto-prune old conversations beyond limit
136
+ if (convs.length > MAX_CONVERSATIONS) {
137
+ for (const old of convs.slice(MAX_CONVERSATIONS)) {
138
+ try { fs.unlinkSync(convPath(old.id)); } catch {}
139
+ }
140
+ return convs.slice(0, MAX_CONVERSATIONS);
141
+ }
142
+
143
+ return convs;
144
+ }
145
+
146
+ // ── Active Conversation ──────────────────────────────────────────────────────
147
+
148
+ /**
149
+ * Get the active conversation ID.
150
+ * @returns {string|null}
151
+ */
152
+ export function getActiveId() {
153
+ try {
154
+ return fs.readFileSync(ACTIVE_FILE, 'utf-8').trim() || null;
155
+ } catch {
156
+ return null;
157
+ }
158
+ }
159
+
160
+ /**
161
+ * Set the active conversation ID.
162
+ */
163
+ export function setActiveId(id) {
164
+ ensureDir();
165
+ fs.writeFileSync(ACTIVE_FILE, id, 'utf-8');
166
+ }
167
+
168
+ /**
169
+ * Clear the active conversation.
170
+ */
171
+ export function clearActiveId() {
172
+ try { fs.unlinkSync(ACTIVE_FILE); } catch {}
173
+ }
174
+
175
+ /**
176
+ * Get or create the active conversation.
177
+ * If none exists, creates a new one.
178
+ * @returns {object} conversation object
179
+ */
180
+ export function getOrCreateActive() {
181
+ const activeId = getActiveId();
182
+ if (activeId) {
183
+ const conv = loadConversation(activeId);
184
+ if (conv) return conv;
185
+ }
186
+ return createConversation();
187
+ }
188
+
189
+ /**
190
+ * Add a message pair (user + assistant) to a conversation.
191
+ * Auto-titles the conversation from the first user message.
192
+ */
193
+ export function addMessages(conv, userContent, assistantContent) {
194
+ conv.messages.push({ role: 'user', content: userContent });
195
+ conv.messages.push({ role: 'assistant', content: assistantContent });
196
+
197
+ // Auto-title from first user message
198
+ if (conv.title === 'New Chat' && conv.messages.length === 2) {
199
+ conv.title = autoTitle(userContent);
200
+ }
201
+
202
+ saveConversation(conv);
203
+ }
204
+
205
+ /**
206
+ * Get the message history from a conversation, capped at maxTurns pairs.
207
+ * @returns {Array<{role: string, content: string}>}
208
+ */
209
+ export function getHistory(conv, maxTurns = 20) {
210
+ const messages = conv.messages || [];
211
+ return messages.slice(-(maxTurns * 2));
212
+ }
213
+
214
+ // ── Export ────────────────────────────────────────────────────────────────────
215
+
216
+ /**
217
+ * Export conversation as Markdown.
218
+ */
219
+ export function exportAsMarkdown(conv) {
220
+ const lines = [
221
+ `# ${conv.title}`,
222
+ `*Created: ${new Date(conv.createdAt).toLocaleString()}*`,
223
+ `*Messages: ${conv.messages.length}*`,
224
+ '',
225
+ '---',
226
+ '',
227
+ ];
228
+
229
+ for (const msg of conv.messages) {
230
+ if (msg.role === 'user') {
231
+ lines.push(`### You`);
232
+ lines.push(msg.content);
233
+ } else {
234
+ lines.push(`### NHA`);
235
+ lines.push(msg.content);
236
+ }
237
+ lines.push('');
238
+ }
239
+
240
+ return lines.join('\n');
241
+ }
242
+
243
+ /**
244
+ * Export conversation as JSON.
245
+ */
246
+ export function exportAsJson(conv) {
247
+ return JSON.stringify({
248
+ id: conv.id,
249
+ title: conv.title,
250
+ createdAt: conv.createdAt,
251
+ updatedAt: conv.updatedAt,
252
+ messages: conv.messages,
253
+ }, null, 2);
254
+ }
255
+
256
+ // ── Migration ────────────────────────────────────────────────────────────────
257
+
258
+ /**
259
+ * Migrate old single-file chat history to multi-conversation format.
260
+ * Called once on first run if old history exists.
261
+ */
262
+ export function migrateOldHistory() {
263
+ const oldFile = path.join(NHA_DIR, 'memory', 'chat-history.json');
264
+ if (!fs.existsSync(oldFile)) return;
265
+
266
+ try {
267
+ const messages = JSON.parse(fs.readFileSync(oldFile, 'utf-8'));
268
+ if (!Array.isArray(messages) || messages.length === 0) return;
269
+
270
+ const conv = createConversation('Previous Chat');
271
+ conv.messages = messages;
272
+ saveConversation(conv);
273
+
274
+ // Rename old file to avoid re-migration
275
+ fs.renameSync(oldFile, oldFile + '.migrated');
276
+ } catch { /* migration failed — non-critical */ }
277
+ }