File size: 7,501 Bytes
d7c11cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22aabf5
d7c11cc
 
 
 
 
22aabf5
d7c11cc
 
 
 
 
22aabf5
d7c11cc
22aabf5
d7c11cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63c632b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d7c11cc
 
 
63c632b
 
 
 
 
 
 
 
 
 
 
d7c11cc
 
 
22aabf5
98b7678
22aabf5
98b7678
22aabf5
 
d7c11cc
 
 
 
 
 
 
 
 
98b7678
d7c11cc
 
 
 
 
 
 
 
 
 
 
 
63c632b
 
 
 
 
 
 
 
d7c11cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98b7678
d7c11cc
 
 
 
 
 
 
 
 
 
 
98b7678
d7c11cc
 
 
 
 
 
 
98b7678
d7c11cc
 
 
 
98b7678
d7c11cc
 
 
 
 
 
 
 
 
98b7678
d7c11cc
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Agent Chat</title>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.5.4/socket.io.js"></script>
  <style>
    /* Overall dark theme background */
    body {
      background-color: #1a1a2e;
      color: #eaeaea;
      font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
      margin: 0;
      padding: 0;
    }
    .container {
      max-width: 800px;
      margin: 0 auto;
      padding: 20px;
    }
    header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 20px;
    }
    h1 {
      margin-bottom: 20px;
      color: #eaeaea;
    }
    .btn {
      background-color: #007bff;
      color: #fff;
      padding: 8px 12px;
      text-decoration: none;
      border-radius: 5px;
      margin-left: 10px;
    }
    .btn:hover {
      background-color: #0056b3;
    }
    /* Chat conversation container */
    .chat-container {
      background-color: #16213e;
      border-radius: 8px;
      padding: 20px;
      height: 400px;
      overflow-y: auto;
      margin-bottom: 20px;
    }
    /* Logs container */
    .logs-container {
      background-color: #0f3460;
      border-radius: 8px;
      padding: 20px;
      height: 150px;
      overflow-y: auto;
      margin-bottom: 20px;
    }
    /* Message styling */
    .message {
      margin-bottom: 10px;
      display: flex;
      align-items: flex-start;
    }
    .message.user {
      justify-content: flex-end;
    }
    .message.agent {
      justify-content: flex-start;
    }
    .bubble {
      max-width: 70%;
      padding: 10px 15px;
      border-radius: 15px;
      line-height: 1.4;
      word-wrap: break-word;
    }
    /* User message bubble style */
    .bubble.user {
      background-color: #007bff;
      color: #fff;
      border-bottom-right-radius: 0;
    }
    /* Agent message bubble style */
    .bubble.agent {
      background-color: #2d3a55;
      color: #fff;
      border-bottom-left-radius: 0;
    }
    /* Log message bubble style */
    .bubble.log {
      background-color: #444;
      color: #ccc;
      font-size: 0.85rem;
      border-radius: 5px;
      padding: 5px 10px;
      margin: 2px 0;
    }
    /* Input area styling */
    .input-area {
      display: flex;
      gap: 10px;
    }
    .input-area textarea {
      flex: 1;
      padding: 10px;
      border-radius: 5px;
      border: none;
      resize: none;
      font-size: 1rem;
    }
    .input-area button {
      padding: 10px 20px;
      border-radius: 5px;
      border: none;
      background-color: #007bff;
      color: #fff;
      cursor: pointer;
    }
    .input-area button:hover {
      background-color: #0056b3;
    }

    .alert {
      padding: 10px;
      margin-bottom: 10px;
      border-radius: 5px;
      text-align: center;
      position: absolute;
      position-area: top center;
      top: 30px;
      align-items: center;
    }

    .alert-error {
      background-color: #D84040;
      color: #ffffff;
    }

    .alert-success {
      background-color: #D2DE32;
      color: #ffffff;
    }

    .alert-warning {
      background-color: #FFC700;
      color: #ffffff;
    }

    
  </style>
</head>
<body>
  
  <div class="container-flash">
      {% with messages = get_flashed_messages(with_categories=true) %}
      {% if messages %}
          <div id="flash-message" class="alert alert-{{ messages[0][0] }}">
              <strong>{{ messages[0][1] }}</strong>
          </div>
      {% endif %}
  {% endwith %}
  </div>

  <div class="container">
    <header>
      <h1>Agent Chat</h1>
      <div>
        <!-- Link to upload page -->
        <a href="/upload" class="btn">Upload DB</a>
        <!-- Link to view the uploaded DB file using the static route -->
        <a href="/files/uploaded.db" class="btn">View Uploaded DB</a>
      </div>
    </header>
    <!-- Chat conversation container -->
    <div class="chat-container" id="chat">
      <!-- Conversation messages will appear here -->
    </div>
    <!-- Logs container -->
    <div class="logs-container" id="logs">
      <!-- Log messages will appear here -->
    </div>
    <!-- Input area for sending prompts -->
    <div class="input-area">
      <textarea id="prompt" rows="2" placeholder="Type your message here..."></textarea>
      <button id="send">Send</button>
    </div>
  </div>
  
  <script>
    const socket = io();
    const chatContainer = document.getElementById("chat");
    const logsContainer = document.getElementById("logs");
    const sendButton = document.getElementById("send");
    const promptTextarea = document.getElementById("prompt");
    const flashMessage = document.getElementById('flash-message');
    //for flash message
        if (flashMessage) {
            setTimeout(function() {
                flashMessage.style.display = 'none';
            }, 2000); // Hide after 2 seconds
        }
    
    // Function to add a chat message bubble
    function addMessage(sender, text) {
      const messageDiv = document.createElement("div");
      messageDiv.classList.add("message", sender);
      const bubbleDiv = document.createElement("div");
      bubbleDiv.classList.add("bubble", sender);
      bubbleDiv.textContent = text;
      messageDiv.appendChild(bubbleDiv);
      chatContainer.appendChild(messageDiv);
      chatContainer.scrollTop = chatContainer.scrollHeight;
    }

    // Function to add a log message bubble
    function addLogMessage(text) {
      const logDiv = document.createElement("div");
      logDiv.classList.add("bubble", "log");
      logDiv.textContent = text;
      logsContainer.appendChild(logDiv);
      logsContainer.scrollTop = logsContainer.scrollHeight;
    }

    // Send user's message when "Send" button is clicked
    sendButton.addEventListener("click", () => {
      const prompt = promptTextarea.value.trim();
      if (!prompt) return;
      addMessage("user", prompt);
      promptTextarea.value = "";
      // Send prompt to the server via a POST request
      fetch("/generate", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ prompt: prompt })
      });
    });

    // Handle streaming tokens for the agent's reply.
    let agentMessageBubble = null;
    socket.on("final_stream", (data) => {
      if (!agentMessageBubble) {
        // Create an agent message bubble if one does not exist.
        const messageDiv = document.createElement("div");
        messageDiv.classList.add("message", "agent");
        agentMessageBubble = document.createElement("div");
        agentMessageBubble.classList.add("bubble", "agent");
        messageDiv.appendChild(agentMessageBubble);
        chatContainer.appendChild(messageDiv);
      }
      // Append incoming data to the agent's bubble.
      agentMessageBubble.textContent += data.message;
      chatContainer.scrollTop = chatContainer.scrollHeight;
    });

    // When the final answer is sent, finish the bubble.
    socket.on("final", (data) => {
      if (!agentMessageBubble) {
        addMessage("agent", data.message);
      } else {
        agentMessageBubble.textContent = data.message;
        agentMessageBubble = null;
      }
    });

    // Append incoming log messages to the logs container.
    socket.on("log", (data) => {
      addLogMessage(data.message);
    });
  </script>
</body>
</html>