File size: 6,187 Bytes
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
<!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;
    }
    h1 {
      margin-bottom: 20px;
      color: #eaeaea;
    }
    .upload-btn {
      background-color: #007bff;
      color: #fff;
      padding: 8px 12px;
      text-decoration: none;
      border-radius: 5px;
    }
    .upload-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;
    }
  </style>
</head>
<body>
  <div class="container">
    <header>
      <h1>Agent Chat</h1>
      <!-- Link to the upload page -->
      <a href="/upload" class="upload-btn">Upload DB</a>
    </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 -->
    <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");

    // 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
      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 agent message bubble if it doesn't 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 the token to the agent's bubble.
      agentMessageBubble.textContent += data.message;
      chatContainer.scrollTop = chatContainer.scrollHeight;
    });

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

    // Append log messages as they arrive.
    socket.on("log", (data) => {
      addLogMessage(data.message);
    });
  </script>
</body>
</html>