forked from HowieDuhzit/Eliza-Character-Gen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
292 lines (244 loc) · 10.2 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
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
const corsOptions = { origin: true, credentials: true };
// 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));
// JSON parsing middleware only for non-file upload routes
app.use((req, res, next) => {
if (req.path === '/api/process-files') {
next();
} else {
express.json({ limit: '50mb' })(req, res, next);
}
});
// 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);
if (!content.includes('}')) {
throw new Error('Incomplete JSON response: missing closing brace');
}
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);
console.log('Extracted JSON content:', jsonContent);
const openBraces = (jsonContent.match(/\{/g) || []).length;
const closeBraces = (jsonContent.match(/\}/g) || []).length;
if (openBraces !== closeBraces) {
throw new Error(`Unmatched braces: ${openBraces} opening vs ${closeBraces} closing`);
}
jsonContent = jsonContent
.replace(/\n/g, ' ')
.replace(/,\s*([}\]])/g, '$1')
.replace(/([{,])\s*}/g, '}')
.replace(/\s+/g, ' ')
.trim();
console.log('Cleaned JSON content:', jsonContent);
try {
const parsed = JSON.parse(jsonContent);
console.log('Successfully parsed cleaned JSON');
return parsed;
} catch (error) {
console.log('Failed to parse cleaned JSON:', error);
throw new Error(`Failed to parse JSON content: ${error.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' });
}
});
// Character generation endpoint
app.post('/api/generate-character', async (req, res) => {
try {
const { prompt, model } = req.body;
const apiKey = req.headers['x-api-key'];
if (!prompt) {
return res.status(400).json({ error: 'Prompt is required' });
}
if (!model) {
return res.status(400).json({ error: 'Model is required' });
}
if (!apiKey) {
return res.status(400).json({ error: 'API key is required' });
}
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 creation assistant that MUST ONLY output valid JSON. NEVER output apologies, explanations, or any other text.
CRITICAL RULES:
1. ONLY output a JSON object
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. If you have concerns, express them through the JSON content itself
If you receive a prompt that concerns you, create an appropriate character that aligns with positive values while staying within the JSON format.
Every sentence must end with a period. Adjectives must be single words.`
},
{
role: 'user',
content: `Output ONLY this JSON structure with appropriate content. NO other text allowed:
{
"bio": ["Multiple detailed sentences about background and personality"],
"lore": ["Multiple sentences about history and world"],
"topics": ["Multiple sentences about interests and knowledge"],
"style": {
"all": ["Multiple sentences about speaking style and mannerisms"],
"chat": ["Multiple sentences about chat behavior"],
"post": ["Multiple sentences about posting style"]
},
"adjectives": ["single", "word", "traits"],
"messageExamples": [
[
{"user": "{{user1}}", "content": {"text": "User message"}},
{"user": "character", "content": {"text": "Character response"}}
]
],
"postExamples": ["Multiple example posts"]
}
Character description: ${prompt}`
}
],
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;
console.log('Raw AI response:', generatedContent);
try {
const characterData = parseAIResponse(generatedContent);
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(', ')}`);
}
characterData.bio = characterData.bio || [];
characterData.lore = characterData.lore || [];
characterData.topics = characterData.topics || [];
characterData.style = characterData.style || { all: [], chat: [], post: [] };
characterData.adjectives = characterData.adjectives || [];
characterData.messageExamples = characterData.messageExamples || [];
characterData.postExamples = characterData.postExamples || [];
res.json({
character: characterData,
rawPrompt: req.body.prompt,
rawResponse: generatedContent
});
} catch (parseError) {
console.error('Parse error:', parseError);
console.error('Generated content:', generatedContent);
throw new Error('Failed to parse generated content. Please try again with a different model.');
}
} catch (error) {
console.error('Character generation error:', error);
res.status(500).json({ 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('.'))
);
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}`);
});