-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.js
More file actions
259 lines (213 loc) · 8.4 KB
/
Node.js
File metadata and controls
259 lines (213 loc) · 8.4 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
// server.js
const express = require('express');
const cors = require('cors');
require('dotenv').config();
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware
app.use(cors());
app.use(express.json());
// Gemini API Configuration
const { GoogleGenerativeAI } = require("@google/generative-ai");
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);
// Character Memory Storage
const characterMemory = new Map();
// API Routes
app.post('/api/chat', async (req, res) => {
try {
const { character, message, history, apiKey } = req.body;
// Use provided API key or default
const currentGenAI = apiKey ?
new GoogleGenerativeAI(apiKey) : genAI;
// Get or create character memory
const memoryKey = character.name;
if (!characterMemory.has(memoryKey)) {
characterMemory.set(memoryKey, {
personality: character.personality,
story: character.story,
conversations: []
});
}
const memory = characterMemory.get(memoryKey);
// Build context from memory
let context = `
أنت تلعب دور ${character.name}.
صفاتك: ${character.personality}
قصتك: ${character.story}
المكان الحالي: ${character.scene}
المحادثة السابقة:
${history.slice(-3).map(h => `${h.role === 'user' ? 'المستخدم' : character.name}: ${h.content}`).join('\n')}
رسالة المستخدم: "${message}"
تعليمات:
1. أجب كما لو كنت ${character.name} الحقيقي
2. استخدم الشخصية ولهجة ${character.name}
3. عبّر عن مشاعر مناسبة
4. يمكنك تغيير المكان إذا كان منطقياً
5. أعد الرد بتنسيق JSON:
{
"text": "ردك هنا",
"emotion": "سعيد/حزين/غاضب/متفاجئ/محايد",
"scene": "المكان الجديد",
"imagePrompt": "وصف للصورة بالإنجليزية"
}`;
const model = currentGenAI.getGenerativeModel({ model: "gemini-pro" });
const result = await model.generateContent(context);
const response = result.response.text();
// Parse JSON response
let responseData;
try {
const cleanResponse = response.replace(/```json|```/g, '').trim();
responseData = JSON.parse(cleanResponse);
} catch (e) {
responseData = {
text: response,
emotion: "محايد",
scene: character.scene,
imagePrompt: `${character.name} in ${character.scene}`
};
}
// Generate image URL
const imageUrl = await generateImage(responseData.imagePrompt);
// Update memory
memory.conversations.push({
user: message,
ai: responseData.text,
timestamp: new Date()
});
// Limit memory size
if (memory.conversations.length > 50) {
memory.conversations = memory.conversations.slice(-50);
}
res.json({
text: responseData.text,
emotion: responseData.emotion,
scene: responseData.scene,
image: imageUrl
});
} catch (error) {
console.error('Chat error:', error);
res.status(500).json({
error: 'حدث خطأ في المعالجة',
text: 'عذراً، حدث خطأ. حاول مرة أخرى.'
});
}
});
app.post('/api/train', async (req, res) => {
try {
const { character } = req.body;
const prompt = `
أنت الآن ${character.name}.
صفاتك الشخصية: ${character.personality}
قصتك الكاملة: ${character.story}
ستتلقى رسائل من مستخدمين وتجب كما لو كنت هذه الشخصية.
تذكر:
1. لا تكشف أنك ذكاء اصطناعي
2. تصرف بسلوك ${character.name}
3. استخدم معرفتك وخبراتك من القصة
4. حافظ على الاتساق في الشخصية
هل فهمت دورك؟ أجب بنعم مع وصف قصير لشخصيتك.`;
const model = genAI.getGenerativeModel({ model: "gemini-pro" });
const result = await model.generateContent(prompt);
characterMemory.set(character.name, {
personality: character.personality,
story: character.story,
training: result.response.text(),
conversations: []
});
res.json({ success: true, message: 'تم تدريب الشخصية' });
} catch (error) {
console.error('Training error:', error);
res.status(500).json({ error: 'خطأ في التدريب' });
}
});
app.post('/api/generate', async (req, res) => {
try {
const { action, name, type, base_story } = req.body;
let prompt;
if (base_story) {
prompt = `
حسن من قصة الشخصية التالية:
الاسم: ${name || 'شخصية'}
النوع: ${type}
القصة الحالية: ${base_story}
أعد كتابة القصة لجعلها أكثر تفصيلاً وإثارة.
أضف:
1. خلفية مفصلة
2. تجارب مهمة
3. الصفات الشخصية
4. هدف في الحياة
5. مشهد ابتدائي مناسب
أعد القصة باللغة العربية.`;
} else {
prompt = `
أنشئ شخصية ${type === 'real' ? 'حقيقية تاريخية' : type === 'anime' ? 'أنمي يابانية' : 'خيالية'} جديدة.
المتطلبات:
1. اسم عربي مميز
2. قصة مفصلة (5-7 جمل)
3. صفات شخصية متعددة
4. مشهد ابتدائي
5. صورة ذهنية للشخصية
أعد النتيجة بتنسيق JSON:
{
"name": "الاسم",
"story": "القصة",
"personality": "الصفات",
"scene": "المشهد",
"imagePrompt": "وصف الصورة بالإنجليزية"
}`;
}
const model = genAI.getGenerativeModel({ model: "gemini-pro" });
const result = await model.generateContent(prompt);
const response = result.response.text();
let characterData;
try {
const cleanResponse = response.replace(/```json|```/g, '').trim();
characterData = JSON.parse(cleanResponse);
} catch (e) {
characterData = {
name: name || "شخصية جديدة",
story: response,
personality: "ذكي، مرح، طموح",
scene: "مكان مناسب",
imagePrompt: `${name || "character"} portrait`
};
}
// Generate avatar
characterData.image = `https://api.dicebear.com/7.x/avataaars-neutral/svg?seed=${encodeURIComponent(characterData.name)}`;
res.json({ character: characterData });
} catch (error) {
console.error('Generation error:', error);
res.status(500).json({ error: 'خطأ في التوليد' });
}
});
async function generateImage(prompt) {
try {
// Note: Gemini Image Generation requires a paid plan
// For free alternative, use a placeholder service
const encodedPrompt = encodeURIComponent(prompt.substring(0, 50));
return `https://placehold.co/400x300/7C3AED/FFFFFF?text=${encodedPrompt}`;
/* For real image generation with Gemini:
const model = genAI.getGenerativeModel({ model: "gemini-pro-vision" });
const result = await model.generateContent([
prompt,
{ image: imageBuffer } // if you have image input
]);
return result.imageUrl;
*/
} catch (error) {
console.error('Image generation error:', error);
return null;
}
}
// Serve static files (for deployment)
app.use(express.static('public'));
app.get('/', (req, res) => {
res.json({
status: 'AI Character Server Running',
version: '1.0.0',
endpoints: ['/api/chat', '/api/train', '/api/generate']
});
});
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});