File size: 14,931 Bytes
6e28179
298575c
b99465c
 
 
ae58435
 
 
 
b99465c
 
71032d7
b99465c
71032d7
 
b99465c
71032d7
298575c
 
 
6e28179
555bfe6
6e28179
555bfe6
6b42561
 
6e28179
b99465c
6e28179
555bfe6
 
 
6b42561
6e28179
 
 
b99465c
 
 
6e28179
 
b99465c
6e28179
 
 
b99465c
 
6e28179
 
6b42561
6e28179
b99465c
6e28179
 
6b42561
6e28179
b99465c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e28179
6b48d9e
6e28179
 
 
b99465c
 
6e28179
 
 
 
b99465c
6e28179
 
 
 
 
 
 
 
b99465c
6e28179
b99465c
 
 
298575c
564fd57
b99465c
163ae2c
b99465c
 
 
364934b
 
 
b99465c
364934b
298575c
 
 
 
 
 
 
b99465c
 
 
298575c
6065311
 
 
b99465c
 
 
 
6065311
b99465c
 
 
 
 
 
 
8be40d5
b99465c
564fd57
b99465c
 
 
 
 
 
a1ab5e3
 
b99465c
 
 
 
 
 
 
 
 
 
 
 
 
 
555bfe6
b99465c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce8bf77
555bfe6
b99465c
 
 
 
 
 
 
 
 
 
 
 
 
 
f70edbb
b99465c
fc48aca
 
f70edbb
b99465c
fc48aca
f70edbb
fc48aca
71032d7
 
f70edbb
 
 
71032d7
 
 
f70edbb
 
 
 
 
 
 
 
 
 
 
 
 
71032d7
f70edbb
 
 
 
 
71032d7
f70edbb
 
 
 
 
 
 
 
 
b99465c
 
f70edbb
 
 
 
 
 
 
 
 
 
 
 
b99465c
 
71032d7
f70edbb
b99465c
f70edbb
 
b99465c
fc48aca
 
f70edbb
 
 
fc48aca
f70edbb
fc48aca
 
 
b99465c
 
f70edbb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc48aca
b99465c
 
 
2202bbb
b99465c
 
 
 
 
 
 
 
 
 
 
298575c
b99465c
 
 
 
 
 
71032d7
 
 
 
 
 
 
 
 
 
b99465c
71032d7
 
 
 
b99465c
 
 
71032d7
b99465c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8be40d5
298575c
 
 
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

class CustomChat extends HTMLElement {
  constructor() {
    super();
    this._logs = [
      "Rosalinda • Démarrage autonome...",
      "Chargement des services locaux...",
      "Initialisation sécurisée...",
"Prêt à aider ✅"
    ];
    this._messages = [
      { role: "assistant", content: "Bonjour ! Je suis Rosalinda, votre IA de création. Je peux vous aider avec des projets, du code, des images et des vidéos. Que souhaitez-vous créer aujourd'hui ?" }
    ];
    this._files = [];
    this._speechState = "idle"; // idle, listening, unsupported, denied
  }
connectedCallback() {
    this.attachShadow({ mode: 'open' });
    this.shadowRoot.innerHTML = `
      <style>
        :host {
          display: none;
          flex-direction: column;
          height: 100%;
          background: #0b0f19;
          color: white;
          position: relative;
          width: 100%;
        }
        :host(.active) {
          display: flex;
        }
        .chat-container {
          flex: 1;
          overflow-y: auto;
          padding: 1rem;
          display: flex;
          flex-direction: column;
          gap: 0.5rem;
        }
        .message {
          margin-bottom: 0.5rem;
          padding: 0.75rem 1rem;
          border-radius: 0.75rem;
          max-width: 80%;
          font-size: 0.875rem;
          line-height: 1.4;
        }
        .user-message {
          background: #3b82f6;
          margin-left: auto;
          border: 1px solid #2563eb;
        }
        .ai-message {
          background: #1e293b;
          margin-right: auto;
          border: 1px solid #334155;
        }
        .computer-screen {
          background: rgba(255, 255, 255, 0.05);
          border: 1px solid rgba(255, 255, 255, 0.1);
          border-radius: 0.5rem;
          margin: 0.5rem;
          overflow: hidden;
        }
        .screen-header {
          display: flex;
          justify-content: space-between;
          padding: 0.5rem 1rem;
          border-bottom: 1px solid rgba(255, 255, 255, 0.1);
          font-size: 0.75rem;
          font-weight: 500;
        }
        .logs-container {
          height: 7rem;
          overflow-y: auto;
          padding: 0.5rem 1rem;
          font-family: monospace;
          font-size: 0.7rem;
          line-height: 1.5;
          color: rgba(255, 255, 255, 0.7);
        }
        .log-line {
          white-space: pre-wrap;
          margin-bottom: 0.25rem;
        }
        .input-container {
          padding: 1rem;
          background: #0f172a;
          border-top: 1px solid #1e293b;
          position: sticky;
          bottom: 0;
        }
        .input-area {
          display: flex;
          gap: 0.5rem;
          align-items: center;
        }
        input {
          flex: 1;
          padding: 0.75rem;
          border-radius: 0.5rem;
          border: 1px solid #334155;
          background: #1e293b;
          color: white;
          font-size: 0.875rem;
        }
        .action-button {
          width: 2.25rem;
          height: 2.25rem;
          display: flex;
          align-items: center;
          justify-content: center;
          border-radius: 0.5rem;
          background: transparent;
          border: 1px solid rgba(255, 255, 255, 0.1);
          color: white;
          cursor: pointer;
        }
        .action-button:hover {
          background: rgba(255, 255, 255, 0.1);
        }
        .send-button {
          background: #3b82f6;
          border: none;
        }
        .send-button:hover {
          background: #2563eb;
        }
        .mic-button.listening {
          background: white;
          color: black;
        }
        .file-input {
          display: none;
        }
        .files-info {
          font-size: 0.7rem;
          color: rgba(255, 255, 255, 0.6);
          padding: 0.25rem 0.5rem;
        }
      </style>
      <div class="chat-container" id="chat">
        ${this._messages.map(msg => `
          <div class="message ${msg.role === 'user' ? 'user-message' : 'ai-message'}">
            ${msg.content}
          </div>
        `).join('')}
      </div>

      <div class="computer-screen">
        <div class="screen-header">
          <span>Voir l'ordinateur de Espace Codage</span>
          <span id="micStatus">En ligne</span>
        </div>
        <div class="logs-container" id="logsContainer">
          ${this._logs.map(log => `<div class="log-line">${log}</div>`).join('')}
        </div>
      </div>

      <div class="input-container">
        <div class="input-area">
          <button class="action-button" title="Ajouter des fichiers" id="fileButton">
            <svg width="18" height="18" viewBox="0 0 24 24" fill="none">
              <path d="M12 5v14M5 12h14" stroke="currentColor" stroke-width="2" stroke-linecap="round" />
            </svg>
          </button>
          <button class="action-button" title="Connecter des applications" id="connectButton">
            <svg width="18" height="18" viewBox="0 0 24 24" fill="none">
              <path d="M9 7v5M15 7v5M7 12h10M10 12v4a4 4 0 0 0 4 4h1" stroke="currentColor" stroke-width="2" stroke-linecap="round" />
              <path d="M6 7h12" stroke="currentColor" stroke-width="2" stroke-linecap="round" />
            </svg>
          </button>
          <input type="text" class="message-input" placeholder="Envoyer un message à Rosalinda" id="messageInput">
          <button class="action-button mic-button" title="Saisie vocale" id="micButton">
            <svg width="18" height="18" viewBox="0 0 24 24" fill="none">
              <path d="M12 14a3 3 0 0 0 3-3V7a3 3 0 0 0-6 0v4a3 3 0 0 0 3 3Z" stroke="currentColor" stroke-width="2" />
              <path d="M19 11a7 7 0 0 1-14 0" stroke="currentColor" stroke-width="2" stroke-linecap="round" />
              <path d="M12 18v3" stroke="currentColor" stroke-width="2" stroke-linecap="round" />
              <path d="M8 21h8" stroke="currentColor" stroke-width="2" stroke-linecap="round" />
            </svg>
          </button>
          <button class="action-button send-button" title="Envoyer" id="sendButton">
            <svg width="18" height="18" viewBox="0 0 24 24" fill="none">
              <path d="M5 12h12" stroke="currentColor" stroke-width="2" stroke-linecap="round" />
              <path d="M13 6l6 6-6 6" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" />
            </svg>
          </button>
          <input type="file" class="file-input" id="fileInput" multiple>
        </div>
        <div class="files-info" id="filesInfo"></div>
      </div>
`;

    this._setupEventListeners();
    this._startLogsSimulation();
  }
  _setupEventListeners() {
    const shadow = this.shadowRoot;
    const fileButton = shadow.getElementById('fileButton');
    const fileInput = shadow.getElementById('fileInput');
    const connectButton = shadow.getElementById('connectButton');
    const micButton = shadow.getElementById('micButton');
    const sendButton = shadow.getElementById('sendButton');
    const messageInput = shadow.getElementById('messageInput');
    const filesInfo = shadow.getElementById('filesInfo');
    const micStatus = shadow.getElementById('micStatus');
    const chatContainer = shadow.getElementById('chat');
    let recognition;
    let isListening = false;

    // Enhanced microphone functionality
    micButton.addEventListener('click', () => {
      if (isListening) {
        this._stopSpeechRecognition(recognition, micButton, micStatus);
        isListening = false;
        return;
      }

      if (!('webkitSpeechRecognition' in window || 'SpeechRecognition' in window)) {
        this._addMessage('assistant', 'La reconnaissance vocale n\'est pas supportée par votre navigateur. Essayez Chrome ou Edge.');
        return;
      }

      recognition = this._startSpeechRecognition(micButton, micStatus, messageInput);
      isListening = true;
      
      // Add visual feedback
      micButton.classList.add('listening');
      micStatus.textContent = '🎤 En écoute...';
      this._addMessage('assistant', 'Je vous écoute... Parlez maintenant.');
    });
    // Enhanced file upload with drag & drop
    fileButton.addEventListener('click', () => fileInput.click());
    fileInput.addEventListener('change', (e) => {
      this._handleFiles(e.target.files, filesInfo);
    });

    // Drag & drop support
    chatContainer.addEventListener('dragover', (e) => {
      e.preventDefault();
      chatContainer.style.border = '2px dashed #3b82f6';
    });

    chatContainer.addEventListener('dragleave', () => {
      chatContainer.style.border = 'none';
    });

    chatContainer.addEventListener('drop', (e) => {
      e.preventDefault();
      chatContainer.style.border = 'none';
      if (e.dataTransfer.files.length) {
        this._handleFiles(e.dataTransfer.files, filesInfo);
      }
    });
    // Connect apps with Rosalinda integration
    connectButton.addEventListener('click', () => {
      this._addMessage('assistant', 
        `🔌 Rosalinda peut se connecter à:
        - Votre ordinateur (fichiers locaux)
        - Votre micro (dictée vocale)
        - Votre caméra (analyse visuelle)
        - Vos applications préférées
        
        Que souhaitez-vous connecter en premier ?`);
    });
    // Enhanced send message with files
    const sendMessage = () => {
      const message = messageInput.value.trim();
      if (message || this._files.length) {
        this._addMessage('user', message);
        messageInput.value = '';
        
        // Simulate Rosalinda processing
        setTimeout(() => {
          let response;
          if (this._files.length > 0) {
            response = `J'ai bien reçu ${this._files.length} fichier(s). Je les analyse maintenant...`;
            this._files = [];
            filesInfo.innerHTML = '';
          } else {
            response = this._generateRosalindaResponse(message);
          }
          this._addMessage('assistant', response);
        }, 1000);
      }
    };
    // Helper methods
    _handleFiles(files, filesInfo) {
      this._files = Array.from(files);
      if (this._files.length) {
        filesInfo.innerHTML = `
          <div style="display:flex; gap:0.5rem; align-items:center;">
            <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor">
              <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" />
            </svg>
            ${this._files.length} fichier(s) sélectionné(s)
            <button id="clearFiles" style="margin-left:auto; background:none; border:none; color:#3b82f6; cursor:pointer;">
              Effacer
            </button>
          </div>
        `;
        shadow.getElementById('clearFiles').addEventListener('click', () => {
          this._files = [];
          filesInfo.innerHTML = '';
          fileInput.value = '';
        });
      } else {
        filesInfo.textContent = '';
      }
    }

    _generateRosalindaResponse(message) {
      if (message.toLowerCase().includes('projet')) {
        return `Pour votre projet, voici ce que je propose:
1. Structure claire et modulaire
2. Design cohérent avec votre identité
3. Tests automatisés
4. Documentation complète

Par où commençons-nous ?`;
      } else if (message.toLowerCase().includes('image')) {
        return "Je peux générer des images personnalisées. Décrivez-moi ce que vous imaginez (couleurs, style, éléments).";
      } else if (message.toLowerCase().includes('vidéo')) {
        return "Pour la vidéo, je peux vous aider avec:\n- Scénarisation\n- Montage\n- Effets\n- Sous-titres\nQuel aspect souhaitez-vous développer ?";
      } else {
        const responses = [
          "J'ai bien compris votre demande. Voici mes suggestions...",
          "Analyse terminée. Voici ce que je propose:",
          "Excellente idée ! Voici comment nous pourrions procéder:",
          "Je peux vous aider avec cela. Voici un plan d'action:"
        ];
        return responses[Math.floor(Math.random() * responses.length)];
      }
    }
sendButton.addEventListener('click', sendMessage);
    messageInput.addEventListener('keydown', (e) => {
      if (e.key === 'Enter') sendMessage();
    });
}

  _addMessage(role, content) {
    this._messages.push({ role, content });
    this._renderMessages();
  }

  _renderMessages() {
    const chatContainer = this.shadowRoot.getElementById('chat');
    chatContainer.innerHTML = this._messages.map(msg => `
      <div class="message ${msg.role === 'user' ? 'user-message' : 'ai-message'}">
        ${msg.content}
      </div>
    `).join('');
    chatContainer.scrollTop = chatContainer.scrollHeight;
  }
  _startLogsSimulation() {
    const logsContainer = this.shadowRoot.getElementById('logsContainer');
    setInterval(() => {
      const actions = [
        'Analyse du projet',
        'Optimisation des ressources',
        'Génération de contenu',
        'Connexion aux services',
        'Préparation des résultats'
      ];
      const randomAction = actions[Math.floor(Math.random() * actions.length)];
      
      this._logs.push(`Rosalinda • ${new Date().toLocaleTimeString()}${randomAction}...`);
      if (this._logs.length > 20) this._logs.shift();
      
      logsContainer.innerHTML = this._logs.map(log => 
        `<div class="log-line">${log}</div>`
      ).join('');
      logsContainer.scrollTop = logsContainer.scrollHeight;
    }, 1400);
  }
_startSpeechRecognition(micButton, micStatus, messageInput) {
    const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
    const recognition = new SpeechRecognition();
    recognition.lang = 'fr-FR';
    recognition.interimResults = true;
    recognition.continuous = true;

    micButton.classList.add('listening');
    micStatus.textContent = 'Micro actif...';

    recognition.onresult = (event) => {
      let transcript = '';
      for (let i = event.resultIndex; i < event.results.length; i++) {
        transcript += event.results[i][0].transcript;
      }
      messageInput.value = transcript;
    };

    recognition.onerror = (event) => {
      console.error('Speech recognition error', event.error);
      micButton.classList.remove('listening');
      micStatus.textContent = 'Erreur micro';
      setTimeout(() => micStatus.textContent = 'En ligne', 2000);
    };

    recognition.onend = () => {
      micButton.classList.remove('listening');
      micStatus.textContent = 'En ligne';
    };

    recognition.start();
    return recognition;
  }

  _stopSpeechRecognition(recognition, micButton, micStatus) {
    if (recognition) {
      recognition.stop();
    }
    micButton.classList.remove('listening');
    micStatus.textContent = 'En ligne';
  }
`;
  }
}
customElements.define('custom-chat', CustomChat);