File size: 8,930 Bytes
fa2eb40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Agent Chat - Idea Generator</title>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.5.4/socket.io.js"></script>
  <style>

    /* Overall dark theme */

    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;

      display: flex;

      flex-direction: column;

      gap: 20px;

    }

    h1 {

      text-align: center;

      margin-bottom: 10px;

      color: #eaeaea;

    }

    /* Wrapper for chat area, logs, and input */

    .chat-section {

      display: flex;

      flex-direction: column;

      border: 1px solid #0f3460;

      border-radius: 8px;

      overflow: hidden;

    }

    /* Chat container */

    .chat-container {

      background-color: #16213e;

      padding: 20px;

      height: 400px;

      overflow-y: auto;

    }

    /* Logs container with a flash (fade-in) effect */

    .logs-container {

      background-color: #0f3460;

      padding: 10px 20px;

      height: 80px;

      overflow-y: auto;

    }

    .bubble.log {

      background-color: #444;

      color: #ccc;

      font-size: 0.85rem;

      border-radius: 5px;

      padding: 5px 10px;

      margin: 2px 0;

      animation: flash 0.5s ease-in-out;

    }

    @keyframes flash {

      0% { opacity: 0; }

      100% { opacity: 1; }

    }

    /* Chat message styles */

    .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;

    }

    .bubble.user {

      background-color: #007bff;

      color: #fff;

      border-bottom-right-radius: 0;

    }

    .bubble.agent {

      background-color: #2d3a55;

      color: #fff;

      border-bottom-left-radius: 0;

      position: relative;

        }

       /* Input area styles */

    .input-area {

      display: flex;

      flex-direction: column;

      gap: 10px;

      padding: 10px;               /* Reduced padding */

      background-color: #16213e;

      box-sizing: border-box;      /* Ensure padding counts toward total width/height */

    }



    .input-area input,

    .input-area textarea {

      width: 100%;

      padding: 10px;

      border-radius: 5px;

      border: none;

      resize: none;

      font-size: 1rem;

      box-sizing: border-box;      /* Prevents overflow when combined with padding */

    }



    .input-area button {

      padding: 10px 20px;

      border-radius: 5px;

      border: none;

      background-color: #007bff;

      color: #fff;

      cursor: pointer;

      align-self: flex-end;

    }    

    

    .input-area button:hover {

      background-color: #0056b3;

    }

    /* Loader spinner styling */

    .loader {

      border: 3px solid #f3f3f3;

      border-top: 3px solid #007bff;

      border-radius: 50%;

      width: 16px;

      height: 16px;

      animation: spin 1s linear infinite;

      margin-right: 10px;

      display: inline-block;

      vertical-align: middle;

    }

    @keyframes spin {

      0% { transform: rotate(0deg); }

      100% { transform: rotate(360deg); }

    }

  </style>
</head>
<body>
  <div class="container">
    <h1>Agent Chat - Idea Generator</h1>
    <div class="chat-section">
      <!-- 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">
        <input type="text" id="title" placeholder="Title of Idea (optional)" />
        <textarea id="roughIdea" rows="2" placeholder="Rough Idea (optional)"></textarea>
        <input type="text" id="category" placeholder="Category (optional)" />
        <textarea id="query" rows="3" placeholder="User Query (mandatory)"></textarea>
        <button id="send">Send</button>
      </div>
    </div>
  </div>
  
  <script>

    const socket = io();

    const chatContainer = document.getElementById("chat");

    const logsContainer = document.getElementById("logs");

    const sendButton = document.getElementById("send");



    // 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 with flash effect

    function addLogMessage(text) {

      const logDiv = document.createElement("div");

      logDiv.classList.add("bubble", "log");

      logDiv.textContent = text;

      logsContainer.appendChild(logDiv);

      logsContainer.scrollTop = logsContainer.scrollHeight;

    }



    // Variable to hold the agent's message bubble (with loader)

    let agentMessageBubble = null;



    // When the user clicks Send

    sendButton.addEventListener("click", () => {

      const title = document.getElementById("title").value.trim();

      const roughIdea = document.getElementById("roughIdea").value.trim();

      const category = document.getElementById("category").value.trim();

      const query = document.getElementById("query").value.trim();



      if (!query) {

        alert("User Query is required.");

        return;

      }

      

      // Display the user's message in chat.

      addMessage("user", query);



      // Clear input fields.

      document.getElementById("title").value = "";

      document.getElementById("roughIdea").value = "";

      document.getElementById("category").value = "";

      document.getElementById("query").value = "";



      // Create an agent bubble with a loader to indicate AI is processing.

      if (!agentMessageBubble) {

        const messageDiv = document.createElement("div");

        messageDiv.classList.add("message", "agent");

        agentMessageBubble = document.createElement("div");

        agentMessageBubble.classList.add("bubble", "agent");

        

        // Create and add loader element.

        const loader = document.createElement("div");

        loader.classList.add("loader");

        agentMessageBubble.appendChild(loader);

        

        messageDiv.appendChild(agentMessageBubble);

        chatContainer.appendChild(messageDiv);

        chatContainer.scrollTop = chatContainer.scrollHeight;

      }



      // Send the collected fields to the server.

      fetch("/generate", {

        method: "POST",

        headers: { "Content-Type": "application/json" },

        body: JSON.stringify({

          title: title,

          rough_idea: roughIdea,

          category: category,

          query: query

        })

      });

    });



    // Handle streaming tokens from the agent.

    socket.on("final_stream", (data) => {

      if (agentMessageBubble) {

        // Remove loader if still present.

        const loader = agentMessageBubble.querySelector(".loader");

        if (loader) {

          loader.remove();

          // Clear any leftover content.

          agentMessageBubble.textContent = "";

        }

        // Append the streaming token.

        agentMessageBubble.textContent += data.message;

        chatContainer.scrollTop = chatContainer.scrollHeight;

      } else {

        // Fallback: create a new agent message if needed.

        addMessage("agent", data.message);

      }

    });



    // When the final answer is complete.

    // When the final answer is complete.

    socket.on("final", (data) => {

      // If there's no existing agent bubble, create one.

      if (!agentMessageBubble) {

        addMessage("agent", "");

      }

      // Use innerHTML to preserve any HTML tags or Markdown-converted HTML

      agentMessageBubble.innerHTML = data.message;

      // Reset agentMessageBubble for next time

      agentMessageBubble = null;

    });



    // Append log messages as they arrive.

    socket.on("log", (data) => {

      addLogMessage(data.message);

    });

  </script>
</body>
</html>