File size: 6,936 Bytes
794cf6c
bc7e9cd
 
 
 
 
db9635c
bc7e9cd
 
 
 
3342a1d
bc7e9cd
 
 
 
3342a1d
bc7e9cd
 
 
3342a1d
 
bc7e9cd
 
 
 
 
3342a1d
bc7e9cd
3342a1d
bc7e9cd
 
 
 
 
794cf6c
bc7e9cd
794cf6c
bc7e9cd
 
 
7ad6991
bc7e9cd
 
 
7ad6991
bc7e9cd
 
 
7ad6991
bc7e9cd
 
 
794cf6c
bc7e9cd
 
 
794cf6c
bc7e9cd
794cf6c
 
bc7e9cd
 
 
794cf6c
bc7e9cd
 
 
794cf6c
 
 
bc7e9cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3342a1d
bc7e9cd
 
 
 
 
 
 
 
 
 
 
3342a1d
bc7e9cd
 
db9635c
 
 
 
 
bc7e9cd
 
 
 
 
 
 
 
 
 
 
 
 
 
794cf6c
 
 
bc7e9cd
 
 
 
 
db9635c
 
bc7e9cd
 
 
 
 
 
 
 
db9635c
 
bc7e9cd
 
 
 
 
 
 
db9635c
bc7e9cd
 
 
 
 
 
 
 
 
db9635c
 
 
bc7e9cd
 
 
 
 
 
 
 
 
 
 
 
 
db9635c
 
 
bc7e9cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<script lang="ts">
  import { onMount, onDestroy } from "svelte";
  import { fade } from "svelte/transition";
  import gsap from "gsap";
  import MessageList from "./MessageList.svelte";
  import MessageInput from "./MessageInput.svelte";
  import ExampleRow from "./ExampleRow.svelte";
  import { isConnected, isProcessing, messages, error } from "../../stores/chat-store";
  import { chatController } from "../../controllers/chat-controller";
  import { authStore } from "../../services/auth";
  import { agentService } from "../../services/agent";

  let inputValue = "";
  let messageListRef: any;
  let autoScroll = true;
  let headerRef: HTMLDivElement;

  onMount(() => {
    if ($authStore.isAuthenticated && !$authStore.loading) {
      agentService.connect();
    }

    if (headerRef) {
      gsap.fromTo(headerRef,
        { opacity: 0, y: -10 },
        { opacity: 1, y: 0, duration: 0.4, ease: "power2.out" }
      );
    }
  });

  $: if ($authStore.isAuthenticated && !$authStore.loading) {
    if (!$isConnected) {
      agentService.connect();
    } else {
      agentService.reauthenticate();
    }
  }

  onDestroy(() => {
    agentService.disconnect();
  });

  function handleSend(event: CustomEvent<string>) {
    chatController.sendMessage(event.detail);
  }

  function handleStop() {
    chatController.stopConversation();
  }

  function handleClear() {
    chatController.clearConversation();
  }

  function handleExampleMessage(message: string) {
    if ($authStore.isAuthenticated && $isConnected && !$isProcessing) {
      chatController.sendMessage(message);
    }
  }


  function handleScroll(event: CustomEvent<{ nearBottom: boolean }>) {
    autoScroll = event.detail.nearBottom;
  }

  function scrollToBottom() {
    messageListRef?.scrollToBottom();
  }
</script>

<div class="chat-panel">
  <div class="chat-header" bind:this={headerRef}>
    <h2>Chat</h2>
    {#if $messages.length > 0 && $authStore.isAuthenticated && !$isProcessing}
      <button
        class="clear-button"
        on:click={handleClear}
        title="Clear conversation"
        transition:fade={{ duration: 200 }}
      >
        <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
          <path d="M3 6h18M8 6V4a2 2 0 012-2h4a2 2 0 012 2v2m3 0v14a2 2 0 01-2 2H7a2 2 0 01-2-2V6h14zM10 11v6M14 11v6" />
        </svg>
        <span>Clear</span>
      </button>
    {/if}
  </div>

  {#if !autoScroll}
    <button
      class="scroll-to-bottom"
      on:click={scrollToBottom}
      title="Scroll to bottom"
      transition:fade={{ duration: 200 }}
    >
      <svg width="20" height="20" viewBox="0 0 20 20" fill="currentColor">
        <path d="M10 14L5 9L6.41 7.59L10 11.17L13.59 7.59L15 9L10 14Z" />
      </svg>
    </button>
  {/if}

  <div class="messages-container">
    {#if !$authStore.isAuthenticated && !$authStore.loading}
      <div class="auth-prompt">
        <button on:click={() => authStore.login()} class="auth-prompt-btn">
          Sign in with 🤗 Hugging Face
        </button>
      </div>
    {:else}
      <MessageList
        bind:this={messageListRef}
        messages={$messages}
        error={$error}
        {autoScroll}
        isConnected={$isConnected}
        onSendMessage={handleExampleMessage}
        on:scroll={handleScroll}
      />
    {/if}
  </div>

  <ExampleRow
    onSendMessage={handleExampleMessage}
    visible={$messages.length === 0 && $authStore.isAuthenticated && $isConnected}
  />

  <MessageInput
    bind:value={inputValue}
    placeholder={!$authStore.isAuthenticated
      ? "Sign in to chat..."
      : $isConnected
        ? $isProcessing
          ? "Processing..."
          : "Type a message..."
        : "Connecting..."}
    disabled={!$authStore.isAuthenticated || !$isConnected || $isProcessing}
    processing={$isProcessing}
    on:send={handleSend}
    on:stop={handleStop}
  />
</div>

<style>
  .chat-panel {
    display: flex;
    flex-direction: column;
    height: 100%;
    width: 100%;
    background: rgba(12, 11, 10, 0.85);
    border-top: 1px solid rgba(139, 115, 85, 0.12);
    position: relative;
  }

  .chat-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 16px;
    background: rgba(18, 17, 16, 0.6);
    border-bottom: 1px solid rgba(139, 115, 85, 0.15);
    flex-shrink: 0;
  }

  .chat-header h2 {
    margin: 0;
    font-size: 11px;
    font-weight: 500;
    color: rgba(251, 248, 244, 0.7);
    text-transform: uppercase;
    letter-spacing: 0.5px;
  }




  .clear-button {
    padding: 4px 10px;
    background: rgba(139, 115, 85, 0.08);
    color: rgba(251, 248, 244, 0.55);
    border: 1px solid rgba(139, 115, 85, 0.15);
    border-radius: 4px;
    font-size: 10px;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.3px;
    cursor: pointer;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    display: flex;
    align-items: center;
    gap: 0.25rem;
  }

  .clear-button:hover {
    background: rgba(139, 115, 85, 0.12);
    color: rgba(251, 248, 244, 0.75);
    border-color: rgba(139, 115, 85, 0.2);
  }

  .clear-button:active {
    transform: scale(0.95);
  }

  .clear-button svg {
    width: 14px;
    height: 14px;
  }

  .scroll-to-bottom {
    position: absolute;
    bottom: 4.5rem;
    right: 1rem;
    width: 32px;
    height: 32px;
    border-radius: 4px;
    background: rgba(124, 152, 133, 0.08);
    border: 1px solid rgba(124, 152, 133, 0.15);
    color: rgba(124, 152, 133, 0.9);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    z-index: 10;
    transition: all 0.2s ease;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  }

  .scroll-to-bottom:hover {
    background: rgba(124, 152, 133, 0.15);
    border-color: rgba(124, 152, 133, 0.3);
    transform: translateY(-1px);
  }

  .scroll-to-bottom:active {
    transform: translateY(0) scale(0.95);
  }

  .auth-prompt {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 2rem;
    height: 100%;
    text-align: center;
  }

  .auth-prompt-btn {
    padding: 0.6rem 1.8rem;
    background: rgba(255, 210, 30, 0.08);
    color: rgba(255, 210, 30, 0.9);
    border: 1px solid rgba(255, 210, 30, 0.2);
    border-radius: 0.5rem;
    cursor: pointer;
    font-size: 0.875rem;
    font-weight: 600;
    transition: all 0.3s ease;
    backdrop-filter: blur(10px);
    position: relative;
    overflow: hidden;
  }

  .auth-prompt-btn:hover {
    transform: translateY(-2px);
    background: rgba(255, 210, 30, 0.12);
    color: rgba(255, 210, 30, 1);
    border-color: rgba(255, 210, 30, 0.3);
  }

  .auth-prompt-btn:active {
    transform: translateY(0);
  }

  .messages-container {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    display: flex;
    flex-direction: column;
  }


</style>