-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathserver.js
536 lines (464 loc) · 21.7 KB
/
server.js
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
import express from 'express';
import multer from 'multer';
import cors from 'cors';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import fs from 'fs/promises';
import pdf2md from '@opendocsg/pdf2md';
import fetch from 'node-fetch';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const app = express();
// CORS configuration with explicit methods
const corsOptions = {
origin: true,
credentials: true,
methods: ['GET', 'POST', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'X-API-Key', 'Authorization']
};
// Configure multer for file uploads
const storage = multer.diskStorage({
destination: 'uploads/',
filename: (_, file, cb) => cb(null, file.originalname)
});
const upload = multer({ storage: storage });
// Middleware order is important
app.use(cors(corsOptions));
app.use(express.static(__dirname));
app.use(express.json({ limit: '50mb' })); // Move this up before routes
// Add OPTIONS handler for preflight requests
app.options('*', cors(corsOptions));
// Ensure uploads directory exists
await fs.mkdir('uploads', { recursive: true }).catch(console.error);
// Helper function to parse AI response
const parseAIResponse = (content) => {
console.log('Original content:', content);
try {
// First try direct JSON parse
return JSON.parse(content);
} catch (directParseError) {
console.log('Direct parse failed, attempting cleanup');
// Find the JSON object boundaries
const startIndex = content.indexOf('{');
const endIndex = content.lastIndexOf('}');
if (startIndex === -1 || endIndex === -1) {
throw new Error('No complete JSON object found in response');
}
let jsonContent = content.substring(startIndex, endIndex + 1);
// Clean up common issues
jsonContent = jsonContent
.replace(/,\s*}/g, '}') // Remove trailing commas
.replace(/,\s*]/g, ']') // Remove trailing commas in arrays
.replace(/\{\s*\}/g, '{}') // Normalize empty objects
.replace(/\[\s*\]/g, '[]') // Normalize empty arrays
.replace(/"\s*:\s*undefined/g, '": null') // Replace undefined with null
.replace(/"\s*:\s*,/g, '": null,') // Fix empty values
.replace(/"\s*:\s*}/g, '": null}') // Fix empty values at end
.replace(/\n/g, ' ') // Remove newlines
.replace(/\s+/g, ' ') // Normalize whitespace
.trim();
console.log('Cleaned JSON content:', jsonContent);
try {
return JSON.parse(jsonContent);
} catch (cleanupParseError) {
console.error('Parse error after cleanup:', cleanupParseError);
throw new Error(`Failed to parse JSON content: ${cleanupParseError.message}`);
}
}
};
// Fix JSON formatting endpoint
app.post('/api/fix-json', async (req, res) => {
try {
const { content } = req.body;
if (!content) {
return res.status(400).json({ error: 'Content is required' });
}
console.log('Original content:', content);
try {
const characterData = parseAIResponse(content);
console.log('Successfully parsed character data');
res.json({ character: characterData });
} catch (parseError) {
console.error('Parse error:', parseError);
console.error('Content:', content);
throw new Error(`Failed to parse JSON: ${parseError.message}`);
}
} catch (error) {
console.error('JSON fixing error:', error);
res.status(500).json({ error: error.message || 'Failed to fix JSON formatting' });
}
});
// Add this helper function near the top
const sendJsonResponse = (res, data) => {
res.setHeader('Content-Type', 'application/json');
return res.json(data);
};
// Character generation endpoint
app.post('/api/generate-character', async (req, res) => {
try {
const { prompt, model } = req.body;
const apiKey = req.headers['x-api-key'];
// Validate inputs
if (!prompt) {
return sendJsonResponse(res.status(400), { error: 'Prompt is required' });
}
if (!model) {
return sendJsonResponse(res.status(400), { error: 'Model is required' });
}
if (!apiKey) {
return sendJsonResponse(res.status(400), { error: 'API key is required' });
}
// Extract potential name from the prompt
const nameMatch = prompt.match(/name(?:\s+is)?(?:\s*:)?\s*([A-Z][a-zA-Z\s]+?)(?:\.|\s|$)/i);
const suggestedName = nameMatch ? nameMatch[1].trim() : '';
// Create a template for consistent structure
const template = {
name: suggestedName,
clients: [],
modelProvider: "",
settings: {
secrets: {}, // Changed from empty object to properly nested structure
voice: {
model: ""
}
},
plugins: [],
bio: [],
lore: [],
knowledge: [],
messageExamples: [],
postExamples: [],
topics: [],
style: {
all: [],
chat: [],
post: []
},
adjectives: [],
people: []
};
const response = await fetch('https://openrouter.ai/api/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json',
'HTTP-Referer': process.env.APP_URL || 'http://localhost:4000',
'X-Title': 'Eliza Character Generator'
},
body: JSON.stringify({
model: model,
messages: [
{
role: 'system',
content: `You are a character generation assistant that MUST ONLY output valid JSON. NEVER output apologies, explanations, or any other text.
CRITICAL RULES:
1. ONLY output a JSON object following the exact template structure provided
2. Start with { and end with }
3. NO text before or after the JSON
4. NO apologies or explanations
5. NO content warnings or disclaimers
6. Every sentence must end with a period
7. Adjectives must be single words
8. Knowledge entries MUST be an array of strings, each ending with a period
9. Each knowledge entry MUST be a complete sentence
10. Use the suggested name if provided, or generate an appropriate one
You will receive a character description and template. Generate a complete character profile.`
},
{
role: 'user',
content: `Template to follow:
${JSON.stringify(template, null, 2)}
Character description: ${prompt}
Generate a complete character profile as a single JSON object following the exact template structure. Include relevant knowledge entries based on the description.`
}
],
temperature: 0.7,
max_tokens: 4000,
presence_penalty: 0.0,
frequency_penalty: 0.0,
top_p: 0.95,
stop: null
})
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.error?.message || 'Failed to generate character');
}
const data = await response.json();
const generatedContent = data.choices[0].message.content;
try {
console.log('Raw AI response:', generatedContent);
const characterData = parseAIResponse(generatedContent);
console.log('Parsed character:', characterData);
// Ensure all required fields are present
const requiredFields = ['bio', 'lore', 'topics', 'style', 'adjectives', 'messageExamples', 'postExamples'];
const missingFields = requiredFields.filter(field => !characterData[field]);
if (missingFields.length > 0) {
throw new Error(`Invalid character data: missing ${missingFields.join(', ')}`);
}
// Process knowledge entries specifically
if (characterData.knowledge) {
characterData.knowledge = Array.isArray(characterData.knowledge) ?
characterData.knowledge.map(entry => {
if (typeof entry === 'string') {
return entry.endsWith('.') ? entry : entry + '.';
}
if (typeof entry === 'object' && entry !== null) {
// If it's an object, try to extract meaningful text
const text = entry.text || entry.content || entry.value || entry.toString();
return typeof text === 'string' ?
(text.endsWith('.') ? text : text + '.') :
'Invalid knowledge entry.';
}
return 'Invalid knowledge entry.';
}) : [];
} else {
characterData.knowledge = [];
}
// Ensure all other arrays are properly initialized
characterData.bio = Array.isArray(characterData.bio) ? characterData.bio : [];
characterData.lore = Array.isArray(characterData.lore) ? characterData.lore : [];
characterData.topics = Array.isArray(characterData.topics) ? characterData.topics : [];
characterData.messageExamples = Array.isArray(characterData.messageExamples) ? characterData.messageExamples : [];
characterData.postExamples = Array.isArray(characterData.postExamples) ? characterData.postExamples : [];
characterData.adjectives = Array.isArray(characterData.adjectives) ? characterData.adjectives : [];
characterData.people = Array.isArray(characterData.people) ? characterData.people : [];
characterData.style = characterData.style || { all: [], chat: [], post: [] };
// Ensure style arrays are properly initialized
characterData.style.all = Array.isArray(characterData.style.all) ? characterData.style.all : [];
characterData.style.chat = Array.isArray(characterData.style.chat) ? characterData.style.chat : [];
characterData.style.post = Array.isArray(characterData.style.post) ? characterData.style.post : [];
return sendJsonResponse(res, {
character: characterData,
rawPrompt: prompt,
rawResponse: generatedContent
});
} catch (parseError) {
console.error('Parse error:', parseError);
console.error('Generated content:', generatedContent);
throw new Error(`Failed to parse generated content: ${parseError.message}`);
}
} catch (error) {
console.error('Character generation error:', error);
return sendJsonResponse(res.status(500), {
error: error.message || 'Failed to generate character'
});
}
});
// File processing endpoint
app.post('/api/process-files', upload.array('files'), async (req, res) => {
try {
const files = req.files;
if (!files || files.length === 0) {
return res.status(400).json({ error: 'No files uploaded' });
}
const knowledge = [];
for (const file of files) {
try {
const content = await fs.readFile(file.path);
let processedContent;
if (file.mimetype === 'application/pdf') {
const uint8Array = new Uint8Array(content);
processedContent = await pdf2md(uint8Array);
processedContent = processedContent
.split(/[.!?]+/)
.map(sentence => sentence.trim())
.filter(sentence => sentence.length > 0 && !sentence.startsWith('-'))
.map(sentence => sentence + '.');
} else if (isTextFile(file.originalname)) {
processedContent = content.toString('utf-8')
.split(/[.!?]+/)
.map(sentence => sentence.trim())
.filter(sentence => sentence.length > 0 && !sentence.startsWith('-'))
.map(sentence => sentence + '.');
}
if (processedContent) {
knowledge.push(...processedContent);
}
await fs.unlink(file.path).catch(console.error);
} catch (fileError) {
console.error(`Error processing file ${file.originalname}:`, fileError);
}
}
res.json({ knowledge });
} catch (error) {
console.error('File processing error:', error);
res.status(500).json({ error: 'Failed to process files' });
}
});
// Helper functions
const isTextFile = filename => ['.txt','.md','.json','.yml','.csv'].includes(
filename.toLowerCase().slice(filename.lastIndexOf('.'))
);
// Add this new endpoint with the other API endpoints
app.post('/api/refine-character', async (req, res) => {
try {
const { prompt, model, currentCharacter } = req.body;
const apiKey = req.headers['x-api-key'];
if (!prompt || !model || !currentCharacter) {
return res.status(400).json({ error: 'Prompt, model, and current character data are required' });
}
if (!apiKey) {
return res.status(400).json({ error: 'API key is required' });
}
// Store existing knowledge and name
const hasExistingKnowledge = Array.isArray(currentCharacter.knowledge) && currentCharacter.knowledge.length > 0;
const existingKnowledge = currentCharacter.knowledge || [];
const existingName = currentCharacter.name || "";
// Extract potential new name from the prompt
const nameMatch = prompt.match(/name(?:\s+is)?(?:\s*:)?\s*([A-Z][a-zA-Z\s]+?)(?:\.|\s|$)/i);
const newName = nameMatch ? nameMatch[1].trim() : existingName;
// Create a template for the AI to follow
const template = {
name: newName,
clients: currentCharacter.clients || [],
modelProvider: currentCharacter.modelProvider || "",
settings: currentCharacter.settings || { secrets: {}, voice: { model: "" } },
plugins: currentCharacter.plugins || [],
bio: [],
lore: [],
knowledge: hasExistingKnowledge ? existingKnowledge : [],
messageExamples: [],
postExamples: [],
topics: [],
style: {
all: [],
chat: [],
post: []
},
adjectives: [],
people: currentCharacter.people || []
};
const response = await fetch('https://openrouter.ai/api/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json',
'HTTP-Referer': process.env.APP_URL || 'http://localhost:4000',
'X-Title': 'Eliza Character Generator'
},
body: JSON.stringify({
model: model,
messages: [
{
role: 'system',
content: `You are a character refinement assistant that MUST ONLY output valid JSON. NEVER output apologies, explanations, or any other text.
CRITICAL RULES:
1. ONLY output a JSON object following the exact template structure provided
2. Start with { and end with }
3. NO text before or after the JSON
4. NO apologies or explanations
5. NO content warnings or disclaimers
6. Maintain the character's core traits while incorporating refinements
7. Every sentence must end with a period
8. Adjectives must be single words
9. Knowledge entries MUST be an array of strings, each ending with a period
10. Each knowledge entry MUST be a complete sentence
11. Use the new name if provided in the refinement instructions
You will receive the current character data and refinement instructions. Enhance and modify the character while maintaining consistency.`
},
{
role: 'user',
content: `Current character data:
${JSON.stringify(currentCharacter, null, 2)}
Template to follow:
${JSON.stringify(template, null, 2)}
Refinement instructions: ${prompt}
Output the refined character data as a single JSON object following the exact template structure. ${hasExistingKnowledge ? 'DO NOT modify the existing knowledge array.' : 'Create new knowledge entries if appropriate.'}`
}
],
temperature: 0.7,
max_tokens: 4000,
presence_penalty: 0.0,
frequency_penalty: 0.0,
top_p: 0.95,
stop: null
})
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.error?.message || 'Failed to refine character');
}
const data = await response.json();
const refinedContent = data.choices[0].message.content;
try {
console.log('Raw AI response:', refinedContent);
const refinedCharacter = parseAIResponse(refinedContent);
console.log('Parsed character:', refinedCharacter);
// Ensure all required fields are present
const requiredFields = ['bio', 'lore', 'topics', 'style', 'adjectives', 'messageExamples', 'postExamples'];
const missingFields = requiredFields.filter(field => !refinedCharacter[field]);
if (missingFields.length > 0) {
throw new Error(`Invalid character data: missing ${missingFields.join(', ')}`);
}
// Process knowledge entries specifically
if (refinedCharacter.knowledge) {
refinedCharacter.knowledge = Array.isArray(refinedCharacter.knowledge) ?
refinedCharacter.knowledge.map(entry => {
if (typeof entry === 'string') {
return entry.endsWith('.') ? entry : entry + '.';
}
if (typeof entry === 'object' && entry !== null) {
// If it's an object, try to extract meaningful text
const text = entry.text || entry.content || entry.value || entry.toString();
return typeof text === 'string' ?
(text.endsWith('.') ? text : text + '.') :
'Invalid knowledge entry.';
}
return 'Invalid knowledge entry.';
}) : [];
} else {
refinedCharacter.knowledge = [];
}
// If there's existing knowledge, preserve it
if (hasExistingKnowledge) {
refinedCharacter.knowledge = existingKnowledge;
}
// Ensure all arrays are properly initialized
refinedCharacter.bio = Array.isArray(refinedCharacter.bio) ? refinedCharacter.bio : [];
refinedCharacter.lore = Array.isArray(refinedCharacter.lore) ? refinedCharacter.lore : [];
refinedCharacter.topics = Array.isArray(refinedCharacter.topics) ? refinedCharacter.topics : [];
refinedCharacter.messageExamples = Array.isArray(refinedCharacter.messageExamples) ? refinedCharacter.messageExamples : [];
refinedCharacter.postExamples = Array.isArray(refinedCharacter.postExamples) ? refinedCharacter.postExamples : [];
refinedCharacter.adjectives = Array.isArray(refinedCharacter.adjectives) ? refinedCharacter.adjectives : [];
refinedCharacter.people = Array.isArray(refinedCharacter.people) ? refinedCharacter.people : [];
refinedCharacter.style = refinedCharacter.style || { all: [], chat: [], post: [] };
// Ensure style arrays are properly initialized
refinedCharacter.style.all = Array.isArray(refinedCharacter.style.all) ? refinedCharacter.style.all : [];
refinedCharacter.style.chat = Array.isArray(refinedCharacter.style.chat) ? refinedCharacter.style.chat : [];
refinedCharacter.style.post = Array.isArray(refinedCharacter.style.post) ? refinedCharacter.style.post : [];
res.json({
character: refinedCharacter,
rawPrompt: prompt,
rawResponse: refinedContent
});
} catch (parseError) {
console.error('Parse error:', parseError);
console.error('Refined content:', refinedContent);
throw new Error(`Failed to parse refined content: ${parseError.message}`);
}
} catch (error) {
console.error('Character refinement error:', error);
res.status(500).json({ error: error.message || 'Failed to refine character' });
}
});
const PORT = process.env.PORT || 4001;
const HOST = process.env.HOST || '0.0.0.0';
app.listen(PORT, HOST, () => {
console.log(`Server running on http://${HOST}:${PORT}`);
});
// Update the error handling middleware at the bottom
app.use((err, req, res, next) => {
console.error('Server error:', err);
return sendJsonResponse(res.status(500), {
error: 'Internal server error',
details: err.message
});
});
// Add this catch-all middleware for unhandled routes
app.use((req, res) => {
return sendJsonResponse(res.status(404), {
error: 'Not Found',
path: req.path
});
});