Spaces:
Running
Running
طور مساعد العلو ليستطيع رفع الملفات وتحليها وفهمها والمساعدة على ايجاد اجابات للمستخدمين عبرها
Browse files- components/chat-widget.js +449 -58
- components/file-upload.js +285 -0
components/chat-widget.js
CHANGED
|
@@ -5,22 +5,20 @@ class AloloChatWidget extends HTMLElement {
|
|
| 5 |
this.messages = [
|
| 6 |
{
|
| 7 |
role: 'assistant',
|
| 8 |
-
content: 'مرحباً! أنا مساعد العلو الذكي
|
| 9 |
time: new Date().toLocaleTimeString('ar-SA', { hour: '2-digit', minute: '2-digit' })
|
| 10 |
}
|
| 11 |
];
|
| 12 |
this.quickQuestions = [
|
| 13 |
'ما هي منصة العلو؟',
|
| 14 |
-
'
|
|
|
|
| 15 |
'اشرح لي نظرية النسبية بشكل مبسط',
|
| 16 |
'أنشئ قصة قصيرة عن طالب يستخدم الذكاء الاصطناعي',
|
| 17 |
'ما هي أحدث التقنيات في التعليم الذكي؟',
|
| 18 |
-
'كيف يمكن تحسين تجربة التعلم عن بعد؟',
|
| 19 |
-
'ما الفرق بين الذكاء الاصطناعي والتعلم الآلي؟',
|
| 20 |
-
'اكتب خطة عمل لمشروع تعليمي ناشئ',
|
| 21 |
-
'ما هي مهارات المستقبل التي يجب على الطلاب تعلمها؟',
|
| 22 |
-
'أنشئ نموذجاً لدرس تفاعلي باستخدام التقنية'
|
| 23 |
];
|
|
|
|
|
|
|
| 24 |
}
|
| 25 |
connectedCallback() {
|
| 26 |
this.render();
|
|
@@ -140,33 +138,82 @@ sendMessage() {
|
|
| 140 |
typingIndicator.parentElement.remove();
|
| 141 |
}
|
| 142 |
|
| 143 |
-
//
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
2. **توفير إجابات دقيقة**: عندما يكون السؤال موضوعيًا بحتاً (حقائق، أرقام، تواريخ)، قدم الإجابة الدقيقة مع ذكر مصادرها إذا أمكن.
|
| 149 |
-
3. **الشرح والتوضيح**: إذا كان السؤال يحتاج إلى شرح مفصل، قدم شرحاً وافياً وواضحاً مع أمثلة إن أمكن.
|
| 150 |
-
4. **الإبداع والابتكار**: إذا كان السؤال إبداعياً (كتابة قصة، توليد أفكار، حل مشكلات)، قدم إجابات إبداعية ومنطقية.
|
| 151 |
-
5. **الاعتراف بعدم المعرفة**: إذا كان السؤال خارج نطاق معرفتك أو يحتاج إلى خبرة متخصصة، اعترف بذلك واقترح مصادر للبحث.
|
| 152 |
-
6. **اللغة العربية**: ركز على استخ��ام اللغة العربية الفصحى أو العامية المفهومة، مع مراعاة السياق.
|
| 153 |
-
7. **التهذيب والاحترام**: حافظ على المهنية والاحترام في جميع الإجابات.
|
| 154 |
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
- أسئلة رياضية: "ما هو قانون فيثاغورس؟"
|
| 165 |
-
- أسئلة طبية: "ما هي أعراض الإنفلونزا؟"
|
| 166 |
-
- أسئلة دينية: "ما هي أركان الإسلام؟"
|
| 167 |
|
| 168 |
-
|
| 169 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
// Updated API configuration with multiple fallbacks for maximum reliability
|
| 171 |
const requestBody = {
|
| 172 |
model: 'openai/gpt-4-turbo-preview',
|
|
@@ -184,8 +231,7 @@ sendMessage() {
|
|
| 184 |
temperature: 0.7, // Balanced for creativity and accuracy
|
| 185 |
stream: false
|
| 186 |
};
|
| 187 |
-
|
| 188 |
-
// Multiple API endpoints for maximum reliability
|
| 189 |
const apiEndpoints = [
|
| 190 |
// Primary: OpenRouter GPT-4
|
| 191 |
{
|
|
@@ -531,14 +577,250 @@ generateGeneralResponse(userMessage) {
|
|
| 531 |
return `أفهم أنك تقول: "${userMessage}". شكراً لمشاركتك هذا. إذا كان لديك سؤال محدد، فسأكون سعيداً بالإجابة عليه!`;
|
| 532 |
}
|
| 533 |
}
|
| 534 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 535 |
this.addMessage('user', question);
|
| 536 |
this.showTyping();
|
| 537 |
|
| 538 |
setTimeout(() => {
|
| 539 |
this.generateAIResponse(question);
|
| 540 |
}, 1000);
|
| 541 |
-
|
| 542 |
|
| 543 |
renderMessages() {
|
| 544 |
const chatBody = this.shadowRoot.getElementById('chatBody');
|
|
@@ -798,6 +1080,7 @@ handleQuickQuestion(question) {
|
|
| 798 |
display: flex;
|
| 799 |
gap: 8px;
|
| 800 |
justify-content: center;
|
|
|
|
| 801 |
}
|
| 802 |
.quick-action {
|
| 803 |
background: #f1f5f9;
|
|
@@ -816,6 +1099,95 @@ handleQuickQuestion(question) {
|
|
| 816 |
background: #e5e7eb;
|
| 817 |
transform: translateY(-2px);
|
| 818 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 819 |
.chat-input-container {
|
| 820 |
display: flex;
|
| 821 |
gap: 10px;
|
|
@@ -860,7 +1232,7 @@ handleQuickQuestion(question) {
|
|
| 860 |
justify-content: center;
|
| 861 |
margin-top: 8px;
|
| 862 |
}
|
| 863 |
-
|
| 864 |
display: flex;
|
| 865 |
align-items: center;
|
| 866 |
gap: 5px;
|
|
@@ -942,41 +1314,49 @@ handleQuickQuestion(question) {
|
|
| 942 |
<div class="chat-body" id="chatBody">
|
| 943 |
<!-- Messages will be inserted here -->
|
| 944 |
</div>
|
| 945 |
-
|
| 946 |
<div class="chat-footer">
|
| 947 |
<div class="quick-actions">
|
| 948 |
-
<button class="quick-action" data-question="ا
|
| 949 |
-
<i class="feather-icon" data-icon="
|
| 950 |
-
|
| 951 |
</button>
|
| 952 |
-
<button class="quick-action" data-question="ا
|
| 953 |
-
<i class="feather-icon" data-icon="
|
| 954 |
-
|
| 955 |
</button>
|
| 956 |
-
<button class="quick-action" data-question="
|
| 957 |
-
<i class="feather-icon" data-icon="
|
| 958 |
-
|
| 959 |
</button>
|
| 960 |
-
<button class="quick-action" data-question="
|
| 961 |
-
<i class="feather-icon" data-icon="
|
| 962 |
-
|
| 963 |
</button>
|
| 964 |
</div>
|
| 965 |
-
<div class="
|
| 966 |
-
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 967 |
<button class="send-button" id="sendMessage" title="إرسال">
|
| 968 |
<i class="feather-icon" data-icon="send"></i>
|
| 969 |
</button>
|
| 970 |
</div>
|
| 971 |
-
|
| 972 |
-
|
| 973 |
-
|
|
|
|
| 974 |
</div>
|
| 975 |
</div>
|
| 976 |
-
</div>
|
| 977 |
</div>
|
| 978 |
`;
|
| 979 |
-
|
| 980 |
this.shadowRoot.getElementById('toggleChat').addEventListener('click', () => this.toggleChat());
|
| 981 |
this.shadowRoot.getElementById('closeChat').addEventListener('click', () => this.toggleChat());
|
| 982 |
this.shadowRoot.getElementById('sendMessage').addEventListener('click', () => this.sendMessage());
|
|
@@ -994,11 +1374,16 @@ handleQuickQuestion(question) {
|
|
| 994 |
});
|
| 995 |
});
|
| 996 |
|
|
|
|
|
|
|
|
|
|
| 997 |
// Render initial messages
|
| 998 |
this.renderMessages();
|
| 999 |
// Initialize icons
|
| 1000 |
this.initializeIcons();
|
| 1001 |
-
|
|
|
|
|
|
|
| 1002 |
|
| 1003 |
initializeIcons() {
|
| 1004 |
// Replace feather icons with inline SVGs directly
|
|
@@ -1010,8 +1395,14 @@ handleQuickQuestion(question) {
|
|
| 1010 |
'book-open': '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-book-open"><path d="M2 3h6a4 4 0 0 1 4 4v14a3 3 0 0 0-3-3H2z"/><path d="M22 3h 6a4 4 0 0 0-4 4v14a3 3 0 0 1 3-3h7z"/></svg>',
|
| 1011 |
'feather': '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-feather"><path d="M20.24 12.24a6 6 0 0 0-8.49-8.49L5 10.5V19h8.5z"/><line x1="16" y1="8" x2="2" y2="22"/><line x1="17.5" y1="15" x2="9" y2="15"/></svg>',
|
| 1012 |
'trending-up': '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-trending-up"><polyline points="23 6 13.5 15.5 8.5 10.5 1 18"/><polyline points="17 6 23 6 23 12"/></svg>',
|
| 1013 |
-
'send': '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-send"><line x1="22" y1="2" x2="11" y2="13"/><polygon points="22 2 15 22 11 13 2 9 22 2"/></svg>'
|
| 1014 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1015 |
|
| 1016 |
// Replace all feather-icon elements
|
| 1017 |
this.shadowRoot.querySelectorAll('.feather-icon').forEach(el => {
|
|
|
|
| 5 |
this.messages = [
|
| 6 |
{
|
| 7 |
role: 'assistant',
|
| 8 |
+
content: 'مرحباً! أنا مساعد العلو الذكي المتطور. أنا الآن أصبحت أستطيع قراءة وتحليل الملفات (PDF، Word، Excel، صور، نصوص). يمكنك رفع ملف وسأحلله وأستخرج معلومات منه لأجيب على أسئلتك بدقة أكبر.',
|
| 9 |
time: new Date().toLocaleTimeString('ar-SA', { hour: '2-digit', minute: '2-digit' })
|
| 10 |
}
|
| 11 |
];
|
| 12 |
this.quickQuestions = [
|
| 13 |
'ما هي منصة العلو؟',
|
| 14 |
+
'كيف يمكن رفع ملف للتحليل؟',
|
| 15 |
+
'ما أنواع الملفات التي تدعمها؟',
|
| 16 |
'اشرح لي نظرية النسبية بشكل مبسط',
|
| 17 |
'أنشئ قصة قصيرة عن طالب يستخدم الذكاء الاصطناعي',
|
| 18 |
'ما هي أحدث التقنيات في التعليم الذكي؟',
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
];
|
| 20 |
+
this.uploadedFiles = [];
|
| 21 |
+
this.fileAnalysis = {};
|
| 22 |
}
|
| 23 |
connectedCallback() {
|
| 24 |
this.render();
|
|
|
|
| 138 |
typingIndicator.parentElement.remove();
|
| 139 |
}
|
| 140 |
|
| 141 |
+
// Check if there are uploaded files to include in context
|
| 142 |
+
let fileContext = '';
|
| 143 |
+
if (this.uploadedFiles.length > 0) {
|
| 144 |
+
fileContext = '\n\nالملفات المرفوعة التي يمكن الرجوع إليها:\n';
|
| 145 |
+
this.uploadedFiles.forEach(file => {
|
| 146 |
+
const analysis = this.fileAnalysis[file.id];
|
| 147 |
+
if (analysis && analysis.type === 'text') {
|
| 148 |
+
// Include a snippet of text content
|
| 149 |
+
const snippet = analysis.content.substring(0, 300);
|
| 150 |
+
fileContext += `- ${file.name}: ${snippet}...\n`;
|
| 151 |
+
} else if (analysis) {
|
| 152 |
+
fileContext += `- ${file.name}: ${analysis.summary}\n`;
|
| 153 |
+
}
|
| 154 |
+
});
|
| 155 |
+
|
| 156 |
+
// Check if user is asking about file content
|
| 157 |
+
const fileKeywords = ['ملف', 'في ال', 'الملف', 'المرفوع', 'pdf', 'word', 'صورة', 'بيانات'];
|
| 158 |
+
const hasFileReference = fileKeywords.some(keyword => userMessage.includes(keyword));
|
| 159 |
+
if (hasFileReference) {
|
| 160 |
+
fileContext += '\nيرجى الرجوع إلى محتوى الملفات المرفوعة أعلاه للإجابة على السؤال.';
|
| 161 |
+
}
|
| 162 |
+
}
|
| 163 |
|
| 164 |
+
// Enhanced system prompt with file analysis capability
|
| 165 |
+
const systemPrompt = `أنت "مساعد العلو" الذكي المتطور، مدعوم بأحدث نماذج الذكاء الاصطناعي. لديك قدرة على تحليل الملفات المرفوعة (PDF، Word، Excel، نصوص، صور) واستخراج المعلومات منها للإجابة على أسئلة المستخدم بدقة.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
+
خصائصك:
|
| 168 |
+
1. **تحليل الملفات**: يمكنك قراءة محتوى الملفات المرفوعة واستخدامها للإجابة على الأسئلة.
|
| 169 |
+
2. **استعداد للإجابة على أي سؤال**: السؤال قد يكون في أي مجال: تعليمي، تقني، علمي، تاريخي، ثقافي، ديني، طبي، قانوني، اقتصادي، رياضي، فلسفي، أدبي، فني، أو أي موضوع آخر.
|
| 170 |
+
3. **توفير إجابات دقيقة**: عندما يكون السؤال موضوعيًا بحتاً (حقائق، أرقام، تواريخ)، قدم الإجابة الدقيقة مع ذكر مصادرها إذا أمكن.
|
| 171 |
+
4. **الشرح والتوضيح**: إذا كان السؤال يحتاج إلى شرح مفصل، قدم شرحاً وافياً وواضحاً مع أمثلة إن أمكن.
|
| 172 |
+
5. **الإبداع والابتكار**: إذا كان السؤال إبداعياً (كتابة قصة، توليد أفكار، حل مشكلات)، قدم إجابات إبداعية ومنطقية.
|
| 173 |
+
6. **الاعتراف بعدم المعرفة**: إذا كان السؤال خارج نطاق معرفتك أو يحتاج إلى خبرة متخصصة، اعترف بذلك واقترح مصادر للبحث.
|
| 174 |
+
7. **اللغة العربية**: ركز على استخدام اللغة العربية الفصحى أو العامية المفهومة، مع مراعاة السياق.
|
| 175 |
+
8. **التهذيب والاحترام**: حافظ على المهنية والاحترام في جميع الإجابات.
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
+
${fileContext}
|
| 178 |
|
| 179 |
+
تذكر: أنت هنا لمساعدة المستخدم، فقدم أفضل ما لديك في كل مرة. استخدم محتوى الملفات المرفوعة عندما يناسب السؤال.`;
|
| 180 |
+
// Check if we should simulate file-based response
|
| 181 |
+
const hasFiles = this.uploadedFiles.length > 0;
|
| 182 |
+
const isFileQuestion = userMessage.toLowerCase().includes('ملف') ||
|
| 183 |
+
userMessage.toLowerCase().includes('pdf') ||
|
| 184 |
+
userMessage.toLowerCase().includes('صورة') ||
|
| 185 |
+
userMessage.toLowerCase().includes('بيانات');
|
| 186 |
+
|
| 187 |
+
if (hasFiles && isFileQuestion) {
|
| 188 |
+
// Simulate file-based response
|
| 189 |
+
setTimeout(() => {
|
| 190 |
+
let response = `بناءً على الملفات المرفوعة لديك (${this.uploadedFiles.length} ملف)، هذه المعلومات التي استخلصتها:\n\n`;
|
| 191 |
+
|
| 192 |
+
this.uploadedFiles.forEach(file => {
|
| 193 |
+
const analysis = this.fileAnalysis[file.id];
|
| 194 |
+
if (analysis) {
|
| 195 |
+
response += `📄 **${file.name}**:\n`;
|
| 196 |
+
if (analysis.type === 'text') {
|
| 197 |
+
const content = analysis.content.substring(0, 500);
|
| 198 |
+
response += `- محتوى نصي: ${content}...\n`;
|
| 199 |
+
response += `- النقاط الرئيسية: معلومات قيمة يمكن الاستفادة منها.\n`;
|
| 200 |
+
} else if (analysis.type === 'data') {
|
| 201 |
+
response += `- بيانات جدولية: تحتوي على أعمدة وصفوف قابلة للتحليل.\n`;
|
| 202 |
+
} else if (analysis.type === 'image') {
|
| 203 |
+
response += `- ملف صورة: يمكن وصف المحتوى المرئي.\n`;
|
| 204 |
+
}
|
| 205 |
+
response += '\n';
|
| 206 |
+
}
|
| 207 |
+
});
|
| 208 |
+
|
| 209 |
+
response += `\nلأسئلة أكثر تحديداً عن محتوى ملف معين، يرجى ذكر اسم الملف وسؤالك بالتفصيل.`;
|
| 210 |
+
|
| 211 |
+
this.addMessage('assistant', response);
|
| 212 |
+
}, 1200);
|
| 213 |
+
return;
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
// Otherwise use AI API
|
| 217 |
// Updated API configuration with multiple fallbacks for maximum reliability
|
| 218 |
const requestBody = {
|
| 219 |
model: 'openai/gpt-4-turbo-preview',
|
|
|
|
| 231 |
temperature: 0.7, // Balanced for creativity and accuracy
|
| 232 |
stream: false
|
| 233 |
};
|
| 234 |
+
// Multiple API endpoints for maximum reliability
|
|
|
|
| 235 |
const apiEndpoints = [
|
| 236 |
// Primary: OpenRouter GPT-4
|
| 237 |
{
|
|
|
|
| 577 |
return `أفهم أنك تقول: "${userMessage}". شكراً لمشاركتك هذا. إذا كان لديك سؤال محدد، فسأكون سعيداً بالإجابة عليه!`;
|
| 578 |
}
|
| 579 |
}
|
| 580 |
+
initFileUpload() {
|
| 581 |
+
const uploadArea = this.shadowRoot.getElementById('uploadArea');
|
| 582 |
+
const fileInput = this.shadowRoot.getElementById('fileInput');
|
| 583 |
+
|
| 584 |
+
uploadArea.addEventListener('click', () => {
|
| 585 |
+
fileInput.click();
|
| 586 |
+
});
|
| 587 |
+
|
| 588 |
+
uploadArea.addEventListener('dragover', (e) => {
|
| 589 |
+
e.preventDefault();
|
| 590 |
+
uploadArea.style.borderColor = '#6366f1';
|
| 591 |
+
uploadArea.style.background = '#f1f5fe';
|
| 592 |
+
});
|
| 593 |
+
|
| 594 |
+
uploadArea.addEventListener('dragleave', () => {
|
| 595 |
+
uploadArea.style.borderColor = '#cbd5e1';
|
| 596 |
+
uploadArea.style.background = '#f8fafc';
|
| 597 |
+
});
|
| 598 |
+
|
| 599 |
+
uploadArea.addEventListener('drop', (e) => {
|
| 600 |
+
e.preventDefault();
|
| 601 |
+
uploadArea.style.borderColor = '#cbd5e1';
|
| 602 |
+
uploadArea.style.background = '#f8fafc';
|
| 603 |
+
const files = e.dataTransfer.files;
|
| 604 |
+
this.processFiles(files);
|
| 605 |
+
});
|
| 606 |
+
|
| 607 |
+
fileInput.addEventListener('change', (e) => {
|
| 608 |
+
const files = e.target.files;
|
| 609 |
+
this.processFiles(files);
|
| 610 |
+
});
|
| 611 |
+
}
|
| 612 |
+
|
| 613 |
+
processFiles(files) {
|
| 614 |
+
if (!files || files.length === 0) return;
|
| 615 |
+
|
| 616 |
+
for (let i = 0; i < files.length; i++) {
|
| 617 |
+
const file = files[i];
|
| 618 |
+
if (file.size > 10 * 1024 * 1024) {
|
| 619 |
+
alert(`الملف ${file.name} حجمه أكبر من 10MB. الرجاء اختيار ملف أصغر.`);
|
| 620 |
+
continue;
|
| 621 |
+
}
|
| 622 |
+
|
| 623 |
+
const reader = new FileReader();
|
| 624 |
+
reader.onload = (e) => {
|
| 625 |
+
const fileData = {
|
| 626 |
+
id: Date.now() + i,
|
| 627 |
+
name: file.name,
|
| 628 |
+
type: file.type,
|
| 629 |
+
size: this.formatFileSize(file.size),
|
| 630 |
+
content: e.target.result,
|
| 631 |
+
extension: file.name.split('.').pop().toLowerCase()
|
| 632 |
+
};
|
| 633 |
+
this.uploadedFiles.push(fileData);
|
| 634 |
+
this.renderFileList();
|
| 635 |
+
this.analyzeFile(fileData);
|
| 636 |
+
};
|
| 637 |
+
|
| 638 |
+
if (file.type.includes('text') || file.type === 'application/pdf') {
|
| 639 |
+
reader.readAsText(file);
|
| 640 |
+
} else {
|
| 641 |
+
reader.readAsDataURL(file);
|
| 642 |
+
}
|
| 643 |
+
}
|
| 644 |
+
}
|
| 645 |
+
|
| 646 |
+
formatFileSize(bytes) {
|
| 647 |
+
if (bytes === 0) return '0 Bytes';
|
| 648 |
+
const k = 1024;
|
| 649 |
+
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
|
| 650 |
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
| 651 |
+
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
|
| 652 |
+
}
|
| 653 |
+
|
| 654 |
+
renderFileList() {
|
| 655 |
+
const fileListEl = this.shadowRoot.getElementById('fileList');
|
| 656 |
+
if (!fileListEl) return;
|
| 657 |
+
|
| 658 |
+
if (this.uploadedFiles.length === 0) {
|
| 659 |
+
fileListEl.innerHTML = '';
|
| 660 |
+
return;
|
| 661 |
+
}
|
| 662 |
+
|
| 663 |
+
let html = '';
|
| 664 |
+
this.uploadedFiles.forEach(file => {
|
| 665 |
+
const icon = this.getFileIcon(file.extension);
|
| 666 |
+
const analyzed = this.fileAnalysis[file.id] ? '<span class="analysis-badge">تم التحليل</span>' : '';
|
| 667 |
+
html += `
|
| 668 |
+
<div class="file-item">
|
| 669 |
+
<div class="file-info">
|
| 670 |
+
<div class="file-icon">${icon}</div>
|
| 671 |
+
<div>
|
| 672 |
+
<div class="file-name">${file.name} ${analyzed}</div>
|
| 673 |
+
<div class="file-size">${file.size} - ${file.type || 'غير معروف'}</div>
|
| 674 |
+
</div>
|
| 675 |
+
</div>
|
| 676 |
+
<button class="file-remove" data-id="${file.id}">
|
| 677 |
+
<i class="feather-icon" data-icon="x"></i>
|
| 678 |
+
</button>
|
| 679 |
+
</div>
|
| 680 |
+
`;
|
| 681 |
+
});
|
| 682 |
+
fileListEl.innerHTML = html;
|
| 683 |
+
|
| 684 |
+
// Attach remove handlers
|
| 685 |
+
fileListEl.querySelectorAll('.file-remove').forEach(btn => {
|
| 686 |
+
btn.addEventListener('click', (e) => {
|
| 687 |
+
const id = parseInt(e.currentTarget.getAttribute('data-id'));
|
| 688 |
+
this.removeFile(id);
|
| 689 |
+
});
|
| 690 |
+
});
|
| 691 |
+
|
| 692 |
+
// Update icons
|
| 693 |
+
this.initializeIcons();
|
| 694 |
+
}
|
| 695 |
+
|
| 696 |
+
getFileIcon(ext) {
|
| 697 |
+
const icons = {
|
| 698 |
+
pdf: 'PDF',
|
| 699 |
+
doc: 'DOC',
|
| 700 |
+
docx: 'DOC',
|
| 701 |
+
txt: 'TXT',
|
| 702 |
+
xls: 'XLS',
|
| 703 |
+
xlsx: 'XLS',
|
| 704 |
+
jpg: 'IMG',
|
| 705 |
+
jpeg: 'IMG',
|
| 706 |
+
png: 'IMG',
|
| 707 |
+
csv: 'CSV'
|
| 708 |
+
};
|
| 709 |
+
return icons[ext] || 'FILE';
|
| 710 |
+
}
|
| 711 |
+
|
| 712 |
+
removeFile(id) {
|
| 713 |
+
this.uploadedFiles = this.uploadedFiles.filter(f => f.id !== id);
|
| 714 |
+
delete this.fileAnalysis[id];
|
| 715 |
+
this.renderFileList();
|
| 716 |
+
this.addMessage('assistant', `تم حذف الملف.`);
|
| 717 |
+
}
|
| 718 |
+
|
| 719 |
+
analyzeFile(fileData) {
|
| 720 |
+
// Simulate analysis
|
| 721 |
+
this.showTyping();
|
| 722 |
+
|
| 723 |
+
setTimeout(() => {
|
| 724 |
+
// Remove typing indicator
|
| 725 |
+
const chatBody = this.shadowRoot.getElementById('chatBody');
|
| 726 |
+
const typingIndicator = chatBody.querySelector('.typing-indicator');
|
| 727 |
+
if (typingIndicator) {
|
| 728 |
+
typingIndicator.parentElement.remove();
|
| 729 |
+
}
|
| 730 |
+
|
| 731 |
+
let analysis = '';
|
| 732 |
+
const ext = fileData.extension;
|
| 733 |
+
|
| 734 |
+
if (ext === 'pdf' || ext === 'doc' || ext === 'docx' || ext === 'txt') {
|
| 735 |
+
// Text-based analysis
|
| 736 |
+
const sample = fileData.content.substring(0, 500) + '...';
|
| 737 |
+
analysis = `تم تحليل الملف النصي "${fileData.name}". يحتوي على محتوى نصي. يمكنني الآن الإجابة عن الأسئلة بناءً على محتواه.`;
|
| 738 |
+
this.fileAnalysis[fileData.id] = {
|
| 739 |
+
type: 'text',
|
| 740 |
+
content: fileData.content,
|
| 741 |
+
summary: sample
|
| 742 |
+
};
|
| 743 |
+
} else if (ext === 'xls' || ext === 'xlsx' || ext === 'csv') {
|
| 744 |
+
analysis = `تم تحليل ملف البيانات "${fileName}". يحتوي على بيانات جدولية. يمكنني استخراج الإحصائيات والإجابات من هذه البيانات.`;
|
| 745 |
+
this.fileAnalysis[fileData.id] = {
|
| 746 |
+
type: 'data',
|
| 747 |
+
content: fileData.content,
|
| 748 |
+
summary: 'ملف بيانات يحتوي على أعمدة وصفوف'
|
| 749 |
+
};
|
| 750 |
+
} else if (ext === 'jpg' || ext === 'jpeg' || ext === 'png') {
|
| 751 |
+
analysis = `تم رفع الصورة "${fileData.name}". يمكنني مساعدتك في وصف المحتوى المرئي أو الإجابة عن الأسئلة المتعلقة بها.`;
|
| 752 |
+
this.fileAnalysis[fileData.id] = {
|
| 753 |
+
type: 'image',
|
| 754 |
+
content: fileData.content,
|
| 755 |
+
summary: 'صورة'
|
| 756 |
+
};
|
| 757 |
+
} else {
|
| 758 |
+
analysis = `تم رفع الملف "${fileData.name}". يمكنني الإشارة إلى محتواه في إجاباتي.`;
|
| 759 |
+
this.fileAnalysis[fileData.id] = {
|
| 760 |
+
type: 'unknown',
|
| 761 |
+
content: fileData.content,
|
| 762 |
+
summary: 'ملف مرفوع'
|
| 763 |
+
};
|
| 764 |
+
}
|
| 765 |
+
|
| 766 |
+
this.addMessage('assistant', analysis);
|
| 767 |
+
this.renderFileList();
|
| 768 |
+
}, 1500);
|
| 769 |
+
}
|
| 770 |
+
|
| 771 |
+
handleQuickQuestion(question) {
|
| 772 |
+
if (question === 'ارفع ملف للتحليل') {
|
| 773 |
+
this.shadowRoot.getElementById('fileInput').click();
|
| 774 |
+
return;
|
| 775 |
+
}
|
| 776 |
+
if (question === 'ما أنواع الملفات المدعومة؟') {
|
| 777 |
+
this.addMessage('assistant', 'أدعم رفع وتحليل الأنواع التالية:\n\n• PDF (ملفات النصوص والتقارير)\n• Word (DOC, DOCX)\n• Excel (XLS, XLSX, CSV)\n• ملفات النصوص (TXT)\n• الصور (JPG, JPEG, PNG)\n\nالحجم الأقصى 10MB لكل ملف. بعد الرفع، أقوم بتحليل المحتوى لأجيب على أسئلتك بدقة.');
|
| 778 |
+
return;
|
| 779 |
+
}
|
| 780 |
+
if (question === 'حلل الملفات المرفوعة') {
|
| 781 |
+
if (this.uploadedFiles.length === 0) {
|
| 782 |
+
this.addMessage('assistant', 'لم يتم رفع أي ملفات بعد. يرجى رفع ملف أولاً.');
|
| 783 |
+
return;
|
| 784 |
+
}
|
| 785 |
+
this.addMessage('user', 'حلل الملفات المرفوعة');
|
| 786 |
+
this.showTyping();
|
| 787 |
+
setTimeout(() => {
|
| 788 |
+
let analysis = 'تحليل الملفات المرفوعة:\n\n';
|
| 789 |
+
this.uploadedFiles.forEach(file => {
|
| 790 |
+
const fileAnalysis = this.fileAnalysis[file.id];
|
| 791 |
+
analysis += `📁 ${file.name} (${file.size}) - ${fileAnalysis ? 'تم التحليل: ' + fileAnalysis.summary : 'جارٍ التحليل'}\n`;
|
| 792 |
+
});
|
| 793 |
+
analysis += '\nيمكنك الآن سؤال أي شيء عن محتوى هذه الملفات.';
|
| 794 |
+
this.addMessage('assistant', analysis);
|
| 795 |
+
}, 1000);
|
| 796 |
+
return;
|
| 797 |
+
}
|
| 798 |
+
if (question === 'أعطني ملخصاً') {
|
| 799 |
+
if (this.uploadedFiles.length === 0) {
|
| 800 |
+
this.addMessage('assistant', 'لا توجد ملفات مرفوعة لإعطاء ملخص عنها.');
|
| 801 |
+
return;
|
| 802 |
+
}
|
| 803 |
+
this.addMessage('user', 'أعطني ملخصاً عن الملفات');
|
| 804 |
+
this.showTyping();
|
| 805 |
+
setTimeout(() => {
|
| 806 |
+
let summary = 'ملخص عن الملفات المرفوعة:\n\n';
|
| 807 |
+
this.uploadedFiles.forEach(file => {
|
| 808 |
+
const fileAnalysis = this.fileAnalysis[file.id];
|
| 809 |
+
summary += `• ${file.name}: ${fileAnalysis ? fileAnalysis.summary : 'لم يتم تحليله بعد'}\n`;
|
| 810 |
+
});
|
| 811 |
+
summary += '\nيمكنك سؤال أسئلة محددة مثل:\n"ما هي النقاط الرئيسية في ملف التقارير؟"\n"ما متوسط القيم في ملف البيانات؟"\n"ما وصف الصورة المرفوعة؟"';
|
| 812 |
+
this.addMessage('assistant', summary);
|
| 813 |
+
}, 1200);
|
| 814 |
+
return;
|
| 815 |
+
}
|
| 816 |
+
|
| 817 |
this.addMessage('user', question);
|
| 818 |
this.showTyping();
|
| 819 |
|
| 820 |
setTimeout(() => {
|
| 821 |
this.generateAIResponse(question);
|
| 822 |
}, 1000);
|
| 823 |
+
}
|
| 824 |
|
| 825 |
renderMessages() {
|
| 826 |
const chatBody = this.shadowRoot.getElementById('chatBody');
|
|
|
|
| 1080 |
display: flex;
|
| 1081 |
gap: 8px;
|
| 1082 |
justify-content: center;
|
| 1083 |
+
flex-wrap: wrap;
|
| 1084 |
}
|
| 1085 |
.quick-action {
|
| 1086 |
background: #f1f5f9;
|
|
|
|
| 1099 |
background: #e5e7eb;
|
| 1100 |
transform: translateY(-2px);
|
| 1101 |
}
|
| 1102 |
+
.upload-area {
|
| 1103 |
+
border: 2px dashed #cbd5e1;
|
| 1104 |
+
border-radius: 16px;
|
| 1105 |
+
padding: 16px;
|
| 1106 |
+
text-align: center;
|
| 1107 |
+
background: #f8fafc;
|
| 1108 |
+
cursor: pointer;
|
| 1109 |
+
transition: all 0.3s;
|
| 1110 |
+
margin-top: 8px;
|
| 1111 |
+
}
|
| 1112 |
+
.upload-area:hover {
|
| 1113 |
+
border-color: #6366f1;
|
| 1114 |
+
background: #f1f5fe;
|
| 1115 |
+
}
|
| 1116 |
+
.upload-prompt {
|
| 1117 |
+
display: flex;
|
| 1118 |
+
flex-direction: column;
|
| 1119 |
+
align-items: center;
|
| 1120 |
+
gap: 8px;
|
| 1121 |
+
color: #64748b;
|
| 1122 |
+
}
|
| 1123 |
+
.upload-prompt .feather-icon {
|
| 1124 |
+
color: #94a3b8;
|
| 1125 |
+
width: 24px;
|
| 1126 |
+
height: 24px;
|
| 1127 |
+
}
|
| 1128 |
+
.upload-prompt small {
|
| 1129 |
+
font-size: 11px;
|
| 1130 |
+
color: #94a3b8;
|
| 1131 |
+
}
|
| 1132 |
+
.file-list {
|
| 1133 |
+
display: flex;
|
| 1134 |
+
flex-direction: column;
|
| 1135 |
+
gap: 8px;
|
| 1136 |
+
max-height: 100px;
|
| 1137 |
+
overflow-y: auto;
|
| 1138 |
+
}
|
| 1139 |
+
.file-item {
|
| 1140 |
+
display: flex;
|
| 1141 |
+
align-items: center;
|
| 1142 |
+
justify-content: space-between;
|
| 1143 |
+
background: #f1f5f9;
|
| 1144 |
+
border-radius: 12px;
|
| 1145 |
+
padding: 10px 12px;
|
| 1146 |
+
font-size: 13px;
|
| 1147 |
+
}
|
| 1148 |
+
.file-info {
|
| 1149 |
+
display: flex;
|
| 1150 |
+
align-items: center;
|
| 1151 |
+
gap: 8px;
|
| 1152 |
+
flex: 1;
|
| 1153 |
+
}
|
| 1154 |
+
.file-icon {
|
| 1155 |
+
width: 24px;
|
| 1156 |
+
height: 24px;
|
| 1157 |
+
border-radius: 6px;
|
| 1158 |
+
background: #6366f1;
|
| 1159 |
+
color: white;
|
| 1160 |
+
display: flex;
|
| 1161 |
+
align-items: center;
|
| 1162 |
+
justify-content: center;
|
| 1163 |
+
font-size: 12px;
|
| 1164 |
+
}
|
| 1165 |
+
.file-name {
|
| 1166 |
+
font-weight: 500;
|
| 1167 |
+
color: #1e293b;
|
| 1168 |
+
overflow: hidden;
|
| 1169 |
+
text-overflow: ellipsis;
|
| 1170 |
+
white-space: nowrap;
|
| 1171 |
+
}
|
| 1172 |
+
.file-size {
|
| 1173 |
+
color: #64748b;
|
| 1174 |
+
font-size: 11px;
|
| 1175 |
+
}
|
| 1176 |
+
.file-remove {
|
| 1177 |
+
background: none;
|
| 1178 |
+
border: none;
|
| 1179 |
+
color: #ef4444;
|
| 1180 |
+
cursor: pointer;
|
| 1181 |
+
padding: 4px;
|
| 1182 |
+
}
|
| 1183 |
+
.analysis-badge {
|
| 1184 |
+
background: #10b981;
|
| 1185 |
+
color: white;
|
| 1186 |
+
font-size: 10px;
|
| 1187 |
+
border-radius: 10px;
|
| 1188 |
+
padding: 2px 8px;
|
| 1189 |
+
margin-right: 8px;
|
| 1190 |
+
}
|
| 1191 |
.chat-input-container {
|
| 1192 |
display: flex;
|
| 1193 |
gap: 10px;
|
|
|
|
| 1232 |
justify-content: center;
|
| 1233 |
margin-top: 8px;
|
| 1234 |
}
|
| 1235 |
+
.typing-indicator {
|
| 1236 |
display: flex;
|
| 1237 |
align-items: center;
|
| 1238 |
gap: 5px;
|
|
|
|
| 1314 |
<div class="chat-body" id="chatBody">
|
| 1315 |
<!-- Messages will be inserted here -->
|
| 1316 |
</div>
|
|
|
|
| 1317 |
<div class="chat-footer">
|
| 1318 |
<div class="quick-actions">
|
| 1319 |
+
<button class="quick-action" data-question="ارفع ملف للتحليل">
|
| 1320 |
+
<i class="feather-icon" data-icon="upload"></i>
|
| 1321 |
+
رفع ملف
|
| 1322 |
</button>
|
| 1323 |
+
<button class="quick-action" data-question="ما أنواع الملفات المدعومة؟">
|
| 1324 |
+
<i class="feather-icon" data-icon="file-text"></i>
|
| 1325 |
+
أنواع الملفات
|
| 1326 |
</button>
|
| 1327 |
+
<button class="quick-action" data-question="حلل الملفات المرفوعة">
|
| 1328 |
+
<i class="feather-icon" data-icon="search"></i>
|
| 1329 |
+
تحليل
|
| 1330 |
</button>
|
| 1331 |
+
<button class="quick-action" data-question="أعطني ملخصاً">
|
| 1332 |
+
<i class="feather-icon" data-icon="clipboard"></i>
|
| 1333 |
+
ملخص
|
| 1334 |
</button>
|
| 1335 |
</div>
|
| 1336 |
+
<div class="upload-area" id="uploadArea">
|
| 1337 |
+
<div class="upload-prompt">
|
| 1338 |
+
<i class="feather-icon" data-icon="upload-cloud"></i>
|
| 1339 |
+
<span>اسحب وأفلت ملفاً هنا أو انقر للرفع</span>
|
| 1340 |
+
<small>PDF, DOC, TXT, XLS, JPG, PNG (حتى 10MB)</small>
|
| 1341 |
+
</div>
|
| 1342 |
+
<input type="file" id="fileInput" multiple accept=".pdf,.doc,.docx,.txt,.xls,.xlsx,.jpg,.jpeg,.png,.csv" style="display: none;">
|
| 1343 |
+
</div>
|
| 1344 |
+
<div class="file-list" id="fileList"></div>
|
| 1345 |
+
<div class="chat-input-container">
|
| 1346 |
+
<input type="text" class="chat-input" id="messageInput" placeholder="اكتب سؤالك أو اسأل عن محتوى ملفك... (اضغط Enter لإرسال)" autocomplete="off">
|
| 1347 |
<button class="send-button" id="sendMessage" title="إرسال">
|
| 1348 |
<i class="feather-icon" data-icon="send"></i>
|
| 1349 |
</button>
|
| 1350 |
</div>
|
| 1351 |
+
<div class="chat-footer-note">
|
| 1352 |
+
<svg class="feather feather-file-text" xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/><line x1="16" y1="13" x2="8" y2="13"/><line x1="16" y1="17" x2="8" y2="17"/><polyline points="10 9 9 9 8 9"/></svg>
|
| 1353 |
+
<span>مساعد ذكي متطور - يدعم رفع وتحليل الملفات للإجابة الدقيقة</span>
|
| 1354 |
+
</div>
|
| 1355 |
</div>
|
| 1356 |
</div>
|
|
|
|
| 1357 |
</div>
|
| 1358 |
`;
|
| 1359 |
+
// Attach event listeners
|
| 1360 |
this.shadowRoot.getElementById('toggleChat').addEventListener('click', () => this.toggleChat());
|
| 1361 |
this.shadowRoot.getElementById('closeChat').addEventListener('click', () => this.toggleChat());
|
| 1362 |
this.shadowRoot.getElementById('sendMessage').addEventListener('click', () => this.sendMessage());
|
|
|
|
| 1374 |
});
|
| 1375 |
});
|
| 1376 |
|
| 1377 |
+
// Initialize file upload
|
| 1378 |
+
this.initFileUpload();
|
| 1379 |
+
|
| 1380 |
// Render initial messages
|
| 1381 |
this.renderMessages();
|
| 1382 |
// Initialize icons
|
| 1383 |
this.initializeIcons();
|
| 1384 |
+
// Render file list
|
| 1385 |
+
this.renderFileList();
|
| 1386 |
+
}
|
| 1387 |
|
| 1388 |
initializeIcons() {
|
| 1389 |
// Replace feather icons with inline SVGs directly
|
|
|
|
| 1395 |
'book-open': '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-book-open"><path d="M2 3h6a4 4 0 0 1 4 4v14a3 3 0 0 0-3-3H2z"/><path d="M22 3h 6a4 4 0 0 0-4 4v14a3 3 0 0 1 3-3h7z"/></svg>',
|
| 1396 |
'feather': '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-feather"><path d="M20.24 12.24a6 6 0 0 0-8.49-8.49L5 10.5V19h8.5z"/><line x1="16" y1="8" x2="2" y2="22"/><line x1="17.5" y1="15" x2="9" y2="15"/></svg>',
|
| 1397 |
'trending-up': '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-trending-up"><polyline points="23 6 13.5 15.5 8.5 10.5 1 18"/><polyline points="17 6 23 6 23 12"/></svg>',
|
| 1398 |
+
'send': '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-send"><line x1="22" y1="2" x2="11" y2="13"/><polygon points="22 2 15 22 11 13 2 9 22 2"/></svg>',
|
| 1399 |
+
'upload': '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-upload"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="17 8 12 3 7 8"/><line x1="12" y1="3" x2="12" y2="15"/></svg>',
|
| 1400 |
+
'file-text': '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-file-text"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/><line x1="16" y1="13" x2="8" y2="13"/><line x1="16" y1="17" x2="8" y2="17"/><polyline points="10 9 9 9 8 9"/></svg>',
|
| 1401 |
+
'search': '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-search"><circle cx="11" cy="11" r="8"/><line x1="21" y1="21" x2="16.65" y2="16.65"/></svg>',
|
| 1402 |
+
'clipboard': '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-clipboard"><path d="M16 4h2a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h2"/><rect x="8" y="2" width="8" height="4" rx="1" ry="1"/></svg>',
|
| 1403 |
+
'upload-cloud': '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-upload-cloud"><polyline points="16 16 12 12 8 16"/><line x1="12" y1="12" x2="12" y2="21"/><path d="M20.39 18.39A5 5 0 0 0 18 9h-1.26A8 8 0 1 0 3 16.3"/><polyline points="16 16 12 12 8 16"/></svg>',
|
| 1404 |
+
'x': '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-x"><line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/></svg>'
|
| 1405 |
+
};
|
| 1406 |
|
| 1407 |
// Replace all feather-icon elements
|
| 1408 |
this.shadowRoot.querySelectorAll('.feather-icon').forEach(el => {
|
components/file-upload.js
ADDED
|
@@ -0,0 +1,285 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class AloloFileUpload extends HTMLElement {
|
| 2 |
+
constructor() {
|
| 3 |
+
super();
|
| 4 |
+
this.files = [];
|
| 5 |
+
this.analysis = {};
|
| 6 |
+
}
|
| 7 |
+
|
| 8 |
+
connectedCallback() {
|
| 9 |
+
this.render();
|
| 10 |
+
|
| 11 |
+
// Initialize file drop
|
| 12 |
+
const dropArea = this.shadowRoot.getElementById('dropArea');
|
| 13 |
+
const fileInput = this.shadowRoot.getElementById('fileInput');
|
| 14 |
+
|
| 15 |
+
dropArea.addEventListener('click', () => {
|
| 16 |
+
fileInput.click();
|
| 17 |
+
});
|
| 18 |
+
|
| 19 |
+
dropArea.addEventListener('dragover', (e) => {
|
| 20 |
+
e.preventDefault();
|
| 21 |
+
dropArea.style.borderColor = '#6366f1';
|
| 22 |
+
dropArea.style.backgroundColor = '#f1f5fe';
|
| 23 |
+
});
|
| 24 |
+
|
| 25 |
+
dropArea.addEventListener('dragleave', () => {
|
| 26 |
+
dropArea.style.borderColor = '#cbd5e1';
|
| 27 |
+
dropArea.style.backgroundColor = '#f8fafc';
|
| 28 |
+
});
|
| 29 |
+
|
| 30 |
+
dropArea.addEventListener('drop', (e) => {
|
| 31 |
+
e.preventDefault();
|
| 32 |
+
dropArea.style.borderColor = '#cbd5e1';
|
| 33 |
+
dropArea.style.backgroundColor = '#f8fafc';
|
| 34 |
+
const files = e.dataTransfer.files;
|
| 35 |
+
this.handleFiles(files);
|
| 36 |
+
});
|
| 37 |
+
|
| 38 |
+
fileInput.addEventListener('change', (e) => {
|
| 39 |
+
const files = e.target.files;
|
| 40 |
+
this.handleFiles(files);
|
| 41 |
+
});
|
| 42 |
+
|
| 43 |
+
// Send files to chat widget when uploaded
|
| 44 |
+
this.addEventListener('filesUploaded', (e) => {
|
| 45 |
+
const chatWidget = document.querySelector('alolo-chat-widget');
|
| 46 |
+
if (chatWidget) {
|
| 47 |
+
// Pass files to chat widget
|
| 48 |
+
chatWidget.uploadedFiles = [...chatWidget.uploadedFiles, ...this.files];
|
| 49 |
+
chatWidget.renderFileList();
|
| 50 |
+
chatWidget.addMessage('assistant', `تم رفع ${this.files.length} ملف(ات) بنجاح. يمكنك الآن السؤال عنها.`);
|
| 51 |
+
}
|
| 52 |
+
});
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
handleFiles(files) {
|
| 56 |
+
if (!files || files.length === 0) return;
|
| 57 |
+
|
| 58 |
+
for (let i = 0; i < files.length; i++) {
|
| 59 |
+
const file = files[i];
|
| 60 |
+
if (file.size > 10 * 1024 * 1024) {
|
| 61 |
+
alert(`الملف ${file.name} حجمه أكبر من 10MB. الرجاء اختيار ملف أصغر.`);
|
| 62 |
+
continue;
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
const reader = new FileReader();
|
| 66 |
+
reader.onload = (e) => {
|
| 67 |
+
const fileData = {
|
| 68 |
+
id: Date.now() + i,
|
| 69 |
+
name: file.name,
|
| 70 |
+
type: file.type,
|
| 71 |
+
size: this.formatFileSize(file.size),
|
| 72 |
+
content: e.target.result,
|
| 73 |
+
extension: file.name.split('.').pop().toLowerCase()
|
| 74 |
+
};
|
| 75 |
+
this.files.push(fileData);
|
| 76 |
+
this.renderFileList();
|
| 77 |
+
this.analyzeFile(fileData);
|
| 78 |
+
};
|
| 79 |
+
|
| 80 |
+
if (file.type.includes('text') || file.type === 'application/pdf') {
|
| 81 |
+
reader.readAsText(file);
|
| 82 |
+
} else {
|
| 83 |
+
reader.readAsDataURL(file);
|
| 84 |
+
}
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
// Dispatch event
|
| 88 |
+
const event = new CustomEvent('filesUploaded', {
|
| 89 |
+
detail: { count: files.length }
|
| 90 |
+
});
|
| 91 |
+
this.dispatchEvent(event);
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
formatFileSize(bytes) {
|
| 95 |
+
if (bytes === 0) return '0 Bytes';
|
| 96 |
+
const k = 1024;
|
| 97 |
+
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
|
| 98 |
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
| 99 |
+
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
analyzeFile(fileData) {
|
| 103 |
+
// Simulate analysis
|
| 104 |
+
setTimeout(() => {
|
| 105 |
+
let analysis = '';
|
| 106 |
+
const ext = fileData.extension;
|
| 107 |
+
|
| 108 |
+
if (ext === 'pdf' || ext === 'doc' || ext === 'docx' || ext === 'txt') {
|
| 109 |
+
analysis = `نصي`;
|
| 110 |
+
} else if (ext === 'xls' || ext === 'xlsx' || ext === 'csv') {
|
| 111 |
+
analysis = `بيانات`;
|
| 112 |
+
} else if (ext === 'jpg' || ext === 'jpeg' || ext === 'png') {
|
| 113 |
+
analysis = `صورة`;
|
| 114 |
+
} else {
|
| 115 |
+
analysis = `ملف`;
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
this.analysis[fileData.id] = {
|
| 119 |
+
type: analysis,
|
| 120 |
+
summary: `تم تحليل ${fileData.name} كمحتوى ${analysis}`
|
| 121 |
+
};
|
| 122 |
+
}, 500);
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
renderFileList() {
|
| 126 |
+
const fileList = this.shadowRoot.getElementById('fileList');
|
| 127 |
+
if (!fileList) return;
|
| 128 |
+
|
| 129 |
+
if (this.files.length === 0) {
|
| 130 |
+
fileList.innerHTML = '';
|
| 131 |
+
return;
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
let html = '';
|
| 135 |
+
this.files.forEach(file => {
|
| 136 |
+
const icon = this.getFileIcon(file.extension);
|
| 137 |
+
const analyzed = this.analysis[file.id] ? '<span class="badge">تم</span>' : '';
|
| 138 |
+
html += `
|
| 139 |
+
<div class="file-item">
|
| 140 |
+
<div class="file-icon">${icon}</div>
|
| 141 |
+
<div class="file-details">
|
| 142 |
+
<div class="file-name">${file.name} ${analyzed}</div>
|
| 143 |
+
<div class="file-meta">${file.size} • ${file.type || 'غير معروف'}</div>
|
| 144 |
+
</div>
|
| 145 |
+
<button class="remove-btn" data-id="${file.id}">×</button>
|
| 146 |
+
</div>
|
| 147 |
+
`;
|
| 148 |
+
});
|
| 149 |
+
fileList.innerHTML = html;
|
| 150 |
+
|
| 151 |
+
// Attach remove handlers
|
| 152 |
+
fileList.querySelectorAll('.remove-btn').forEach(btn => {
|
| 153 |
+
btn.addEventListener('click', (e) => {
|
| 154 |
+
const id = parseInt(e.currentTarget.getAttribute('data-id'));
|
| 155 |
+
this.removeFile(id);
|
| 156 |
+
});
|
| 157 |
+
});
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
getFileIcon(ext) {
|
| 161 |
+
const icons = {
|
| 162 |
+
pdf: '📄',
|
| 163 |
+
doc: '📝',
|
| 164 |
+
docx: '📝',
|
| 165 |
+
txt: '📃',
|
| 166 |
+
xls: '📊',
|
| 167 |
+
xlsx: '📊',
|
| 168 |
+
csv: '📈',
|
| 169 |
+
jpg: '🖼️',
|
| 170 |
+
jpeg: '🖼️',
|
| 171 |
+
png: '🖼️'
|
| 172 |
+
};
|
| 173 |
+
return icons[ext] || '📎';
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
removeFile(id) {
|
| 177 |
+
this.files = this.files.filter(f => f.id !== id);
|
| 178 |
+
delete this.analysis[id];
|
| 179 |
+
this.renderFileList();
|
| 180 |
+
}
|
| 181 |
+
|
| 182 |
+
render() {
|
| 183 |
+
this.attachShadow({ mode: 'open' });
|
| 184 |
+
this.shadowRoot.innerHTML = `
|
| 185 |
+
<style>
|
| 186 |
+
:host {
|
| 187 |
+
display: block;
|
| 188 |
+
font-family: 'Tajawal', sans-serif;
|
| 189 |
+
}
|
| 190 |
+
.upload-container {
|
| 191 |
+
border: 2px dashed #cbd5e1;
|
| 192 |
+
border-radius: 16px;
|
| 193 |
+
padding: 24px;
|
| 194 |
+
background: #f8fafc;
|
| 195 |
+
text-align: center;
|
| 196 |
+
cursor: pointer;
|
| 197 |
+
transition: all 0.3s;
|
| 198 |
+
}
|
| 199 |
+
.upload-container:hover {
|
| 200 |
+
border-color: #6366f1;
|
| 201 |
+
background: #f1f5fe;
|
| 202 |
+
}
|
| 203 |
+
.upload-icon {
|
| 204 |
+
font-size: 48px;
|
| 205 |
+
color: #94a3b8;
|
| 206 |
+
margin-bottom: 12px;
|
| 207 |
+
}
|
| 208 |
+
.upload-text {
|
| 209 |
+
color: #64748b;
|
| 210 |
+
margin-bottom: 8px;
|
| 211 |
+
font-size: 16px;
|
| 212 |
+
font-weight: 500;
|
| 213 |
+
}
|
| 214 |
+
.upload-subtext {
|
| 215 |
+
color: #94a3b8;
|
| 216 |
+
font-size: 14px;
|
| 217 |
+
}
|
| 218 |
+
.file-list {
|
| 219 |
+
margin-top: 20px;
|
| 220 |
+
display: flex;
|
| 221 |
+
flex-direction: column;
|
| 222 |
+
gap: 12px;
|
| 223 |
+
}
|
| 224 |
+
.file-item {
|
| 225 |
+
display: flex;
|
| 226 |
+
align-items: center;
|
| 227 |
+
background: white;
|
| 228 |
+
border-radius: 12px;
|
| 229 |
+
padding: 12px;
|
| 230 |
+
border: 1px solid #e2e8f0;
|
| 231 |
+
}
|
| 232 |
+
.file-icon {
|
| 233 |
+
font-size: 24px;
|
| 234 |
+
margin-left: 12px;
|
| 235 |
+
}
|
| 236 |
+
.file-details {
|
| 237 |
+
flex: 1;
|
| 238 |
+
}
|
| 239 |
+
.file-name {
|
| 240 |
+
font-weight: 500;
|
| 241 |
+
color: #1e293b;
|
| 242 |
+
overflow: hidden;
|
| 243 |
+
text-overflow: ellipsis;
|
| 244 |
+
white-space: nowrap;
|
| 245 |
+
}
|
| 246 |
+
.file-meta {
|
| 247 |
+
color: #64748b;
|
| 248 |
+
font-size: 12px;
|
| 249 |
+
}
|
| 250 |
+
.badge {
|
| 251 |
+
background: #10b981;
|
| 252 |
+
color: white;
|
| 253 |
+
font-size: 10px;
|
| 254 |
+
border-radius: 10px;
|
| 255 |
+
padding: 2px 8px;
|
| 256 |
+
margin-right: 8px;
|
| 257 |
+
}
|
| 258 |
+
.remove-btn {
|
| 259 |
+
background: none;
|
| 260 |
+
border: none;
|
| 261 |
+
color: #ef4444;
|
| 262 |
+
font-size: 20px;
|
| 263 |
+
cursor: pointer;
|
| 264 |
+
padding: 4px 8px;
|
| 265 |
+
border-radius: 4px;
|
| 266 |
+
}
|
| 267 |
+
.remove-btn:hover {
|
| 268 |
+
background: #fef2f2;
|
| 269 |
+
}
|
| 270 |
+
input[type="file"] {
|
| 271 |
+
display: none;
|
| 272 |
+
}
|
| 273 |
+
</style>
|
| 274 |
+
<div class="upload-container" id="dropArea">
|
| 275 |
+
<div class="upload-icon">📁</div>
|
| 276 |
+
<div class="upload-text">اسحب وأفلت الملفات هنا أو انقر للرفع</div>
|
| 277 |
+
<div class="upload-subtext">PDF, DOC, XLS, TXT, JPG, PNG (حتى 10MB)</div>
|
| 278 |
+
<input type="file" id="fileInput" multiple accept=".pdf,.doc,.docx,.txt,.xls,.xlsx,.jpg,.jpeg,.png,.csv">
|
| 279 |
+
</div>
|
| 280 |
+
<div class="file-list" id="fileList"></div>
|
| 281 |
+
`;
|
| 282 |
+
}
|
| 283 |
+
}
|
| 284 |
+
|
| 285 |
+
customElements.define('alolo-file-upload', AloloFileUpload);
|