activity-simulator / src /core /conversationSimulator.js
abedelbahnasy55's picture
feat: cloud simulator - Docker, dashboard, auto-start
ccb6b75
import logger from '../utils/logger.js';
class ConversationSimulator {
constructor(aiProvider, memory, developer) {
this.ai = aiProvider;
this.memory = memory;
this.developer = developer;
}
async generateIssueComment(issue, existingComments = []) {
try {
const roleContext = this.developer.getRoleContext();
const previousComments = existingComments.map(c =>
`@${c.user?.login || 'user'}: ${c.body?.slice(0, 200)}...`
).join('\n');
const prompt = `أنت مطور محترف (${roleContext.role}) تتفاعل مع GitHub Issue.
Issue #${issue.number}: ${issue.title}
${issue.body?.slice(0, 500) || ''}
${previousComments ? `المحادثات السابقة:\n${previousComments}\n` : ''}
الدور: ${roleContext.personality}
التركيز: ${roleContext.focus.join(', ')}
اكتب تعليق طبيعي على هذا Issue. يجب أن يكون:
- مفيد ومحدد
- يعكس خبرة المطور
- يستخدم مصطلحات تقنية مناسبة
- ليس طويلاً جداً (2-4 جمل)
- يبدو بشري وليس AI
- يمكن أن يسأل أسئلة أو يقدم اقتراحات
اكتب التعليق بالعربية أو الإنجليزية حسب سياق النقاش.`;
const comment = await this.ai.generate(prompt, {
maxTokens: 300,
temperature: 0.8,
});
this.memory.addConversation({
type: 'issue-comment',
issueNumber: issue.number,
content: comment,
context: {
issueTitle: issue.title,
role: roleContext.role,
},
});
return comment.trim();
} catch (error) {
logger.error(`Failed to generate issue comment: ${error.message}`);
return this._fallbackComment('issue');
}
}
async generatePRComment(pr, existingComments = [], files = []) {
try {
const roleContext = this.developer.getRoleContext();
const fileNames = files.map(f => f.filename || f.path).join(', ');
const previousComments = existingComments.map(c =>
`@${c.user?.login || 'user'}: ${c.body?.slice(0, 200)}...`
).join('\n');
const prompt = `أنت مطور محترف (${roleContext.role}) تراجع Pull Request على GitHub.
PR #${pr.number}: ${pr.title}
الملفات المتغيرة: ${fileNames || 'غير محدد'}
${previousComments ? `المحادثات السابقة:\n${previousComments}\n` : ''}
الدور: ${roleContext.personality}
التركيز: ${roleContext.focus.join(', ')}
اكتب مراجعة PR طبيعية. يجب أن:
- تكون محددة عن الكود
- تعكس خبرة المطور
- تستخدم مصطلحات تقنية
- تبدو بشرية
- يمكن أن تمدح أو تنتقد بشكل بناء
- 2-4 جمل
يمكنك:
- طلب توضيح
- اقتراح تحسين
- الإشارة لمشكلة محتملة
- المدح على شيء جيد
- طلب إضافة tests
اكتب المراجعة بالعربية أو الإنجليزية حسب السياق.`;
const comment = await this.ai.generate(prompt, {
maxTokens: 300,
temperature: 0.85,
});
this.memory.addConversation({
type: 'pr-comment',
prNumber: pr.number,
content: comment,
context: {
prTitle: pr.title,
role: roleContext.role,
files: fileNames,
},
});
return comment.trim();
} catch (error) {
logger.error(`Failed to generate PR comment: ${error.message}`);
return this._fallbackComment('pr');
}
}
async generatePRReviewDecision(pr, comment) {
try {
const lowerComment = comment.toLowerCase();
if (lowerComment.includes('lgtm') || lowerComment.includes('looks good') || lowerComment.includes('approved') || lowerComment.includes('ممتاز') || lowerComment.includes('رائع')) {
return 'approve';
}
if (lowerComment.includes('request changes') || lowerComment.includes('needs work') || lowerComment.includes('يجب اصلاح') || lowerComment.includes('خطأ')) {
return 'request_changes';
}
return Math.random() < 0.7 ? 'approve' : 'comment';
} catch (error) {
logger.error(`Failed to generate PR review decision: ${error.message}`);
return 'comment';
}
}
async generateIssueDiscussion(issue, numReplies = 3) {
const discussions = [];
let currentContext = issue.body || '';
for (let i = 0; i < numReplies; i++) {
try {
const roleContext = this.developer.getRoleContext();
const prompt = `أنت في مناقشة GitHub Issue.
Issue: ${issue.title}
${currentContext}
اكتب رد طبيعي في المناقشة (2-3 جمل).
يجب أن يكون الرد:
- متصلاً بما قيل قبله
- يضيف قيمة جديدة
- يبدو بشري
- يستخدم مصطلحات تقنية
اكتب بالعربية أو الإنجليزية.`;
const reply = await this.ai.generate(prompt, {
maxTokens: 250,
temperature: 0.8,
});
discussions.push({
type: 'discussion-reply',
content: reply.trim(),
timestamp: Date.now() + i * 3600000 * (Math.random() * 4 + 1),
});
currentContext += `\n\nرد: ${reply.trim()}`;
} catch (error) {
logger.error(`Failed to generate discussion reply ${i + 1}: ${error.message}`);
discussions.push({
type: 'discussion-reply',
content: this._fallbackComment('discussion'),
timestamp: Date.now(),
});
}
}
return discussions;
}
async generateFollowUpComment(pr, originalComment, changes = []) {
try {
const changeList = changes.map(c => `- ${c}`).join('\n');
const prompt = `أنت ترد على مراجعة PR.
المراجعة الأصلية: ${originalComment}
التغييرات التي تمت:
${changeList}
اكتب رد قصير (1-2 جملة) يشكر على المراجعة ويوضح ما تم فعله.
يجب أن يكون طبيعي ومهذب.`;
const reply = await this.ai.generate(prompt, {
maxTokens: 200,
temperature: 0.7,
});
return reply.trim();
} catch (error) {
logger.error(`Failed to generate follow-up comment: ${error.message}`);
return 'Thanks for the review! I\'ve addressed the feedback.';
}
}
async generateIssueClarification(issue) {
try {
const prompt = `أنت تحتاج توضيح من صاحب هذا Issue.
Issue: ${issue.title}
${issue.body?.slice(0, 500) || ''}
اكتب سؤال توضيحي طبيعي (1-2 جملة).
يجب أن يكون:
- محدد ومفيد
- يساعد في فهم المشكلة
- يبدو بشري
- مهذب
مثال: "Can you share the error logs?" أو "What browser are you seeing this on?"`;
const question = await this.ai.generate(prompt, {
maxTokens: 150,
temperature: 0.8,
});
return question.trim();
} catch (error) {
logger.error(`Failed to generate clarification: ${error.message}`);
return 'Can you provide more details about this issue?';
}
}
async generateThankYouComment() {
const phrases = [
'Thanks for the quick review!',
'Appreciate the feedback, fixed now.',
'Good catch! Thanks for pointing that out.',
'Thanks, updated the PR with the changes.',
'Appreciate it! Merging now.',
'Thanks everyone for the reviews.',
];
return phrases[Math.floor(Math.random() * phrases.length)];
}
async generateStatusUpdate(issue, status) {
const statusMessages = {
'in-progress': [
'Working on this now',
'Started implementing',
'On it',
'Taking a look',
],
'blocked': [
'Blocked on this, need more info',
'Waiting for clarification',
'Can\'t proceed until X is resolved',
],
'done': [
'Done! Ready for review',
'Finished implementation',
'All tests pass, ready to merge',
],
'review': [
'Ready for review',
'PR is up for review',
'Would love some eyes on this',
],
};
const messages = statusMessages[status] || statusMessages['in-progress'];
return messages[Math.floor(Math.random() * messages.length)];
}
_fallbackComment(type) {
const fallbacks = {
issue: 'Looking into this. Will update once I have more information.',
pr: 'Thanks for the PR. I\'ll review it shortly.',
discussion: 'Good point. Let me think about this and get back to you.',
};
return fallbacks[type] || fallbacks.issue;
}
}
export default ConversationSimulator;