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
 
 
 
 
 
 
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
<script>

  import { onMount } from "svelte";

  import { writable } from "svelte/store";

  import { chatEndPoint } from "../../constants/constants";

  import { createWebSocket } from "../../utils/websocketFormatter";

  import { v4 as uuidv4 } from "uuid";

  import SvelteMarkdown from "svelte-markdown";

  import { ICONS } from "$constants/imageUrls.js";



  // define variables for websocket

  let sendMessage, onMessage, formatStream, isStreaming, refresh;



  let messages = writable([]);

  let message = "";

  let running = false;

  let streaming = false;

  let instructionMessages = [

    `“Can you provide advice on refinancing my current mortgage to save on interest?”`,

    `“What strategies can I employ to pay off my mortgage faster?”`,

    `“Can you guide me through different types of mortgage plans available and which one would suit my needs best?”`,

  ];

  export let isCollapsed = true; // Make isCollapsed prop exported

  export let searchData;



  // get the input value on keypress in child and send req to websocket

  $: {

    if (searchData) {

      messages.update((msgs) => [

        ...msgs,

        { id: uuidv4(), content: searchData, type: "sent" },

      ]);

      running = true;

      sendMessage(

        JSON.stringify({

          user_input: searchData,

        })

      );

      scrollToBottom();

    }

  }



  // send message on send button

  const send = () => {

    if (message === "") return;

    running = true;

    sendMessage(

      JSON.stringify({

        user_input: message,

      })

    );

    addMsg(message);

    // setTimeout(() => addResponseMsg(message), 1000); // Echo the message after 1 second

  };



  // format the message to be rendered

  const addMsg = (msg) => {

    messages.update((msgs) => [

      ...msgs,

      { id: uuidv4(), content: msg, type: "sent" },

    ]);

    message = "";

    scrollToBottom();

  };



  // const addResponseMsg = (msg) => {

  //   messages.update((msgs) => [...msgs, { text: msg, type: "received" }]);

  //   running = false;

  //   scrollToBottom();

  // };



  const scrollToBottom = () => {

    const messageBox = document.getElementById("message-box");

    messageBox.scrollTop = messageBox.scrollHeight;

  };



  const handleEnter = (event) => {

    if (event.key === "Enter") {

      event.preventDefault();

      send();

    }

  };



  onMount(() => {

    document.getElementById("message").addEventListener("keyup", handleEnter);

  });



  let chatbotCollapsed = true;



  const toggleChatbot = (event) => {

    event.preventDefault();

    isCollapsed = !isCollapsed;

    if (!isCollapsed) {

      setTimeout(() => {

        const chatbot = document.getElementById("chatbot");

        chatbot &&

          chatbot.dispatchEvent(

            new CustomEvent("addResponseMsg", { detail: "Hi" })

          );

      }, 1000);

    }

  };



  const updateStreamingState = () => {

    streaming = isStreaming();

  };



  //connect to ws and reconnect if disconnected and store the data in messages

  onMount(() => {

    if (typeof window !== "undefined") {

      const ws = createWebSocket(chatEndPoint);

      sendMessage = ws.sendMessage;

      onMessage = ws.onMessage;

      formatStream = ws.formatStream;

      isStreaming = ws.isStreaming;

      refresh = ws.refresh;



      onMessage((message) => {

        const formattedMessage = formatStream(message);

        // If the last message in this array has an  matching id, it means it's part of a stream.

        // We update the messages array by replacing the existing message with the same id,

        // or by adding it if it doesn't exist. This ensures that the message

        // content is updated correctly for streaming data

        if (formattedMessage.length > 0) {

          running = false;

          scrollToBottom();

          const lastMessage = formattedMessage[formattedMessage.length - 1];

          messages.update((msgs) => {

            const updatedMessages = [...msgs];

            if (lastMessage.id) {

              const existingIndex = updatedMessages.findIndex(

                (msg) => msg.id === lastMessage.id

              );

              if (existingIndex !== -1) {

                updatedMessages[existingIndex] = lastMessage;

              } else {

                updatedMessages.push(lastMessage);

              }

            } else {

              updatedMessages.push(lastMessage);

            }

            return updatedMessages;

          });

        }

        updateStreamingState();

      });



      // Ensure we set the initial state correctly

      updateStreamingState();



      return () => {

        refresh();

      };

    }

  });



  function handleSendMessage(event, message) {

    messages.update((msgs) => [

      ...msgs,

      { id: uuidv4(), content: message, type: "sent" },

    ]);

    running = true;

    sendMessage(

      JSON.stringify({

        user_input: message,

      })

    );

    scrollToBottom();

  }

</script>

<div id="chatbot" class="main-card {isCollapsed ? 'collapsed' : ''}">
  <div class="main-title">
    <div>
      <img

        class="!w-[100px] !max-w-[100px]"

        src={ICONS.MIRA_LOGO}

        alt="Miralogo"

      />
    </div>
    <div class="close-button" style="cursor: pointer;" on:click={toggleChatbot}>
      <img src={ICONS.CLOSE_ICON} alt="close" />
    </div>
  </div>
  <div class="chat-area" id="message-box">
    <div class="flex flex-col justify-center items-center mt-24 mb-16">
      <img src={ICONS.MIRA_LAB} class="!w-[55px]" alt="logo" />
      <h3 class="text-secondary-800 text-xl mt-4 font-bold text-nowrap">
        What can I do for you today?
      </h3>
    </div>
    {#if $messages.length === 0}
      <div class="flex flex-col gap-4 text-left">
        {#each instructionMessages as data, index}
          <div

            on:click={(e) => handleSendMessage(e, data)}
            class="rounded-md cursor-pointer flex flex-row justify-start py-2 pr-[11px] pl-[11px] border border-primary"
          >
            <div

              class="relative rounded-lg hidden max-w-full border-[1px] border-solid border-gainsboro"

            ></div>
            <div

              class="relative leading-[24px] text-black max-w-full z-[1] text-sm"

            >
              {data}
            </div>
          </div>
        {/each}
      </div>
    {/if}
    {#each $messages as msg, index (msg.id)}
      <div class="chat-message-div flex justify-end">
        {#if msg?.type === "sent"}
          <div class="chat-message-sent">
            <span class="max-w-[13rem]">{msg.content}</span>
            <div class="ml-2 w-[24px] h-[24px] rounded-full bg-[#AFBEFF]"></div>
          </div>
        {:else}
          <div class="chat-message-received flex items-start">
            <img src={ICONS.MIRA_LAB} class="!w-[25px] mr-2" alt="logo" />
            <span>
              <SvelteMarkdown source={msg.content?.replace("%J", "")} />
            </span>
          </div>
        {/if}
      </div>
      {#if running && index === $messages.length - 1}
        <div class="flex w-[10rem] flex-col gap-4 mt-2">
          <div

            class="h-[10px] w-full rounded-full loader-gradient animate-pulse"

          ></div>
          <div

            class="h-[10px] w-3/4 rounded-full loader-gradient animate-pulse"

          ></div>
        </div>
      {/if}
    {/each}
  </div>
  <!-- <div class="line"></div> -->
  <div class="input-div">
    <input

      class="input-message"

      name="message"

      type="text"

      style=" border :1px solid #e3e3e3"

      id="message"

      bind:value={message}

      placeholder="Write a message"

    />
    <button

      style=" border :1px solid #e3e3e3"

      class="input-send bg-[#1340FF] {streaming ? 'cursor-not-allowed' : ''}"

      disabled={streaming}

      on:click={send}

    >
      <img src={streaming ? ICONS.STREAMING_ICON : ICONS.SEND_BUTTON} alt="" />
    </button>
  </div>

  <div

    class="text-secondary-600 text-[10px] flex justify-center pb-2 items-center text-center w-[74%] m-auto"

  >
    Mira can sometimes produce incorrect or out of context response. Please read
    the responses carefully.
  </div>
</div>

<!-- hello -->

<style>

  .loader-gradient {

    background: linear-gradient(90deg, #e9edff 0%, #ffd0ff 121.67%);

  }



  .close-button {

    display: flex;

    align-items: center;

    justify-content: center;

  }

  .input-message {

    background: #fbfcff;

    color: #555;



    /* border: 1px solid rgba(227, 227, 227, 1); */

  }

  .main-card.collapsed {

    /* transform: translateY(0); */

  }

  .main-card {

    position: fixed;

    bottom: 0;

    right: 0;

    width: 300px;

    height: 400px;

    /* border: 1px solid #ccc; */

    display: flex;

    flex-direction: column;

    background-color: #fff;

    box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);

  }



  .chat-message-div {

    display: flex;

    margin: 5px;

  }

  .chat-message-sent {

    background-color: #dcf8c6;

    padding: 5px;

    border-radius: 5px;

  }

  .chat-message-received {

    background-color: #fff;

    padding: 5px;

    border-radius: 5px;

  }

  .chat-area {

    flex: 1;

    overflow-y: auto;

    padding: 10px;

  }

  .input-div {

    display: flex;

    padding: 10px;

    /* border-top: 1px solid #ccc; */

  }

  .input-message {

    flex: 1;

    padding: 5px;

    border: 1px solid #ccc;

    border-radius: 5px;

  }

  .input-send {

    margin-left: 10px;

    border: none;

    cursor: pointer;

    padding: 0 10px;

    border-radius: 5px;

  }



  .title {

    margin: auto;

    font-size: x-large;

    color: rebeccapurple;

  }

  @media (min-width: 450px) {

    .main-card {

      width: 96%;

      max-width: 400px;

      height: calc(100% - 8rem) !important;

      border-radius: 8px !important;

      max-height: 600px;

      margin: 16px !important;

    }

  }



  .collapsed {

    width: 0px !important; /*previous value 48px*/

    height: 0px !important; /*previous value 48px*/

    border-radius: 24px !important;

    margin: 16px !important;

  }



  .main-card {

    display: flex;

    background: white;

    color: white;

    width: 100%;

    height: 80%;

    margin: 0px;

    border-radius: 0px;

    flex-direction: column;

    overflow: hidden;

    right: 0;

    bottom: 0;

    position: fixed;

    z-index: 10000;

    transition: all 0.5s;

    box-shadow:

      0 10px 16px 0 rgba(0, 0, 0, 0.2),

      0 6px 20px 0 rgba(0, 0, 0, 0.19);

  }



  #chatbot_toggle {

    position: absolute;

    right: 0;

    border: none;

    height: 48px;

    width: 48px;

    background: rebeccapurple;

    padding: 14px;

    color: white;

  }



  .line {

    height: 1px;

    background-color: rebeccapurple;

    width: 100%;

    opacity: 0.2;

  }

  .main-title {

    background: linear-gradient(to right, #3f66fb, #cb49f4);

    font-size: large;

    font-weight: bold;

    display: flex;

    height: 48px;

    display: flex;

    align-items: center;

    justify-content: space-between;

  }

  .main-title > div {

    height: 48px;

    width: 48px;

    display: flex;

    margin-left: 8px;

  }

  .main-title svg {

    height: 24px;

    margin: auto;

  }

  .main-title > span {

    margin: auto auto auto 8px;

  }

  .chat-area {

    flex-grow: 1;

    overflow: auto;

    border-radius: 8px;

    padding: 10px;

    display: flex;

    flex-direction: column;

  }

  .input-message {

    padding: 14px 8px 14px 8px;

    flex-grow: 1;

    border: none;

  }

  .input-message:focus {

    outline: #1340ff;

  }

  .input-div {

    height: 58px;

    display: flex;

  }



  .input-send svg {

    fill: rebeccapurple;

    margin: 11px 8px;

  }

  .chat-message-div {

    display: flex;

  }



  .chat-message-sent {

    margin: 8px 0px 8px 64px;

    padding: 8px 16px;

    animation-name: fadeIn;

    display: flex;

    justify-content: end !important;

    animation-iteration-count: 1;

    animation-timing-function: ease-in;

    animation-duration: 100ms;

    color: black;

    border-radius: 8px 8px 2px 8px;

    background-color: #e9edff;

  }



  .chat-message-received {

    margin: 8px 64px 8px 0px;

    padding: 8px 16px;

    animation-name: fadeIn;

    animation-iteration-count: 1;

    animation-timing-function: ease-in;

    animation-duration: 100ms;

    color: black;

    border-radius: 8px 8px 8px 2px;

    background-color: #f4f4f4;

  }



  @keyframes fadeIn {

    from {

      opacity: 0;

    }



    to {

      opacity: 1;

    }

  }



  ::-webkit-scrollbar {

    width: 10px;

  }

  ::-webkit-scrollbar-track {

    background: #f1f1f1;

  }



  ::-webkit-scrollbar-thumb {

    background: #888;

  }



  ::-webkit-scrollbar-thumb:hover {

    background: #555;

  }



  @media screen and (max-width: 448px) {

    .main-card {

      width: 100%;

      height: 100% !important;

      z-index: 999999999999999999999999;

    }

  }

</style>