-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
312 lines (265 loc) · 10.9 KB
/
server.js
File metadata and controls
312 lines (265 loc) · 10.9 KB
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
import 'dotenv/config';
import { logger } from './utils/logger.js';
import { MemoryCleanup } from './memory/memory-cleanup.js';
import { ErrorRecovery } from './utils/error-recovery.js';
import { ConversationContext } from './memory/conversation-context.js';
import { MemoryPriority } from './memory/memory-priority.js';
import { MemoryCache } from './memory/memory-cache.js';
import fs from 'fs';
import express from 'express';
import rateLimit from 'express-rate-limit';
import { chatCompletion } from './chat-completion.js';
import { saveMemory, loadMemory } from './memory/memory.js';
import { VectorDB } from './memory/vector-db.js';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// AI Configuration
const AI_BACKEND_URL = 'http://127.0.0.1:1234';
const AI_MODEL = 'deepseek-r1-distill-qwen-14b';
// Check environment variables
const ENCRYPTION_KEY = process.env.ENCRYPTION_KEY;
if (!ENCRYPTION_KEY) {
logger.warn('ENCRYPTION_KEY not provided - running without encryption');
}
const app = express();
app.use(express.json());
app.use(express.static('public'));
// Create necessary directories
if (!fs.existsSync('memories')) {
fs.mkdirSync('memories');
}
if (!fs.existsSync('backups')) {
fs.mkdirSync('backups');
}
// Initialize empty memory file if it doesn't exist
if (!fs.existsSync('memories/memory.json')) {
fs.writeFileSync('memories/memory.json', JSON.stringify({ messages: [] }, null, 2));
}
// Rate limiting middleware
const limiter = rateLimit({
windowMs: 60 * 1000, // 1 minute
max: 30, // 30 requests per minute
message: { error: 'Too many requests, please try again later.' }
});
// Input validation middleware
const validateInput = (req, res, next) => {
const { prompt } = req.body;
if (!prompt || typeof prompt !== 'string') {
return res.status(400).json({ error: 'Invalid input' });
}
if (prompt.length < 1 || prompt.length > 2000) {
return res.status(400).json({ error: 'Message length must be between 1 and 2000 characters' });
}
next();
};
const memoryCleanup = new MemoryCleanup();
const errorRecovery = new ErrorRecovery();
const conversationContext = new ConversationContext();
const memoryPriority = new MemoryPriority();
const memoryCache = new MemoryCache();
async function initializeAI() {
const vectorDB = new VectorDB();
try {
await vectorDB.loadIndex();
vectorDB.startMaintenance();
memoryCleanup.start();
// Prewarm cache with recent memories
logger.debug('Checking ENCRYPTION_KEY value:', { ENCRYPTION_KEY });
let memories = loadMemory('memories/memory.json', ENCRYPTION_KEY) || { messages: [] };
if (memories && memories.messages) {
const prioritized = memoryPriority.sortMemoriesByPriority(memories.messages);
const topMemories = prioritized.slice(0, 100); // Cache top 100 memories
memoryCache.setMany(
Object.fromEntries(topMemories.map(m => [m.id, m]))
);
}
logger.info('System initialized successfully', {
cacheStats: memoryCache.getStats()
});
} catch (error) {
logger.error('System initialization failed', { error: error.message });
process.exit(1);
}
}
// Apply rate limiting to all routes
app.use('/api/', limiter);
app.post('/api/chat', validateInput, async (req, res) => {
const { prompt, conversationId = conversationContext.generateConversationId() } = req.body;
try {
logger.info('Processing chat request', {
prompt_length: prompt.length,
conversationId,
prompt_preview: prompt.substring(0, 100)
});
const context = conversationContext.getContext(conversationId);
logger.debug('Retrieved conversation context', { contextLength: context.length, conversationId });
// Log context BEFORE reformatting, especially for new conversations
if (!conversationId) {
logger.debug('Context BEFORE reformatting (new conversation):', { context });
}
// Reformat context items
const reformattedContext = context.map(item => ({
role: 'assistant', // Assuming previous turns are assistant responses
content: item.response
}));
// Add user message to context
const contextWithUserMessage = [...reformattedContext, { role: 'user', content: prompt }];
// Log context AFTER reformatting, especially for new conversations
if (!conversationId) {
logger.debug('Context AFTER reformatting (new conversation):', { context: contextWithUserMessage });
}
const completionResult = await errorRecovery.withRecovery(
async () => chatCompletion(prompt, contextWithUserMessage), // Use reformatted context
{ type: 'chat_completion', conversationId }
);
if (!completionResult || !completionResult.response) {
logger.error('Chat completion returned invalid result');
return res.status(500).json({ error: 'Invalid response from AI service' });
}
// Send thinking message separately if available
if (completionResult.thinking) {
sendThinkingMessage(completionResult.thinking);
}
// Construct response JSON
const responseJson = {
response: completionResult.response,
conversationId: conversationId,
tokens: completionResult.thinking, // Still send token count as 'thinking' for now
};
// Add to conversation context
conversationContext.addToContext(conversationId, prompt, completionResult.response);
// Enhance memory data with conversation info
const memoryId = `mem_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
const enhancedMemoryData = {
...completionResult.memoryData,
id: memoryId,
conversationId,
personalityTraits: completionResult.personalityTraits,
contextSize: context?.length || 0
};
try {
await errorRecovery.withRecovery(
async () => {
// Update memory priority
const priority = memoryPriority.updatePriority(memoryId, enhancedMemoryData, 'write');
// Cache the new memory
memoryCache.set(memoryId, {
...enhancedMemoryData,
priority
});
// Save to persistent storage
return saveMemory(enhancedMemoryData, ENCRYPTION_KEY);
},
{ type: 'memory_save', filePath: 'memories/memory.json' }
);
logger.debug('Memory saved successfully');
} catch (saveError) {
logger.error('Memory save failed', { error: saveError.message });
}
res.json(responseJson);
} catch (error) {
logger.error('API Error', { error: error.message, stack: error.stack });
res.status(500).json({
error: 'Internal server error',
details: error.message
});
}
});
// Add context management endpoints
app.get('/api/context/:conversationId', (req, res) => {
const { conversationId } = req.params;
const summary = conversationContext.summarizeContext(conversationId);
if (!summary) {
return res.status(404).json({ error: 'Context not found' });
}
res.json(summary);
});
app.post('/api/context/merge', (req, res) => {
const { sourceId, targetId } = req.body;
if (!sourceId || !targetId) {
return res.status(400).json({ error: 'Source and target IDs required' });
}
const success = conversationContext.mergeContexts(sourceId, targetId);
if (!success) {
return res.status(400).json({ error: 'Failed to merge contexts' });
}
res.json({ message: 'Contexts merged successfully' });
});
app.get('/api/debug/write-test', (req, res) => {
try {
fs.writeFileSync('memories/test.txt', 'test content');
res.send('Write successful');
} catch (err) {
res.status(500).send(`Write failed: ${err.message}`);
}
});
app.get('/api/memory', (req, res) => {
try {
const memories = loadMemory('memories/memory.json', ENCRYPTION_KEY);
res.json(memories);
} catch (error) {
res.status(500).json({ error: 'Failed to load memories' });
}
});
// Add memory management endpoints
app.get('/api/memory/stats', (req, res) => {
res.json({
cache: memoryCache.getStats(),
conversations: {
active: conversationContext.conversations.size
}
});
});
app.post('/api/memory/optimize', async (req, res) => {
try {
const memories = loadMemory('memories/memory.json', ENCRYPTION_KEY);
if (memories && memories.messages) {
// Prioritize and prune memories
const optimizedMemories = memoryPriority.pruneByPriority(
memories.messages,
1024 * 1024 * 100 // 100MB max size
);
// Update cache with optimized memories
memoryCache.clear();
memoryCache.setMany(optimizedMemories);
// Save optimized memories
await saveMemory({ messages: optimizedMemories });
res.json({
message: 'Memory optimization complete',
stats: {
originalSize: Object.keys(memories.messages).length,
optimizedSize: Object.keys(optimizedMemories).length,
cache: memoryCache.getStats()
}
});
} else {
res.status(400).json({ error: 'No memories to optimize' });
}
} catch (error) {
logger.error('Memory optimization failed', { error: error.message });
res.status(500).json({ error: 'Memory optimization failed' });
}
});
// Add test endpoint
app.get('/api/test', (req, res) => {
res.json({ status: 'ok', message: 'Server is responding' });
});
// Serve root route
app.get('/', function(req, res) {
res.sendFile(__dirname + '/public/index.html');
});
process.on('SIGINT', () => {
logger.info('Shutting down server');
memoryCleanup.stop();
process.exit(0);
});
const PORT = process.env.PORT || 3001;
app.listen(PORT, () => {
logger.info(`Server running on port ${PORT}`);
initializeAI();
});
function sendThinkingMessage(thinking) {
logger.info('Thinking process:', { thinking }); // Log the thinking TEXT
}