File size: 11,478 Bytes
146b1cb
 
 
 
 
 
 
 
 
 
 
 
 
0897b9c
146b1cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0897b9c
146b1cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
579f597
146b1cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
579f597
146b1cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html>

<head>
    <title>Onboarder</title>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <!-- Meta description -->
    <meta name="description"
        content="Experience the seamless interaction with your digital lifestyle needs using Jio Copilot. From shopping with JioMart, exploring health and beauty products with TiraBeauty, streaming content with JioCinema, to navigating internet plans with JioFiber, Jio Copilot caters to all. Enjoy a streamlined, efficient digital experience across multiple platforms." />

    <!-- Open Graph -->
    <meta property="og:type" content="website" />
    <meta property="og:url" content="https://jio.copilot.live/" />
    <meta property="og:title" content="DwightAI" />
    <meta property="og:description"
        content="Experience the seamless interaction with your digital lifestyle needs using Jio Copilot. From shopping with JioMart, exploring health and beauty products with TiraBeauty, streaming content with JioCinema, to navigating internet plans with JioFiber, Jio Copilot caters to all. Enjoy a streamlined, efficient digital experience across multiple platforms." />
    <meta property="og:image" content="https://cdn.copilot.live/v2/original/icons/JioGPT_icon.png" />

    <link rel="stylesheet"
        href="https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/4.0.0/github-markdown.min.css" />
    <link rel="icon" href="https://cdn.copilot.live/v2/original/icons/JioGPT_icon.png" type="image/x-icon" />

    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: white;
            font-family: Arial, sans-serif;
        }

        #title-bar {
            font-size: 20px;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 12px 0px;
        }

        #chat-container {
            width: 95%;
            height: 100%;
            max-width: 800px;
            overflow: hidden;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
        }

        #chat {
            height: calc(100vh - 160px);
            overflow: auto;
            border-radius: 10px;
        }

        .message {
            padding: 10px;
            margin-bottom: 10px;
            border-radius: 10px;
        }

        .user {
            align-self: flex-end;
            background-color: #f4a91e;
            color: white;
            display: flex;
            align-items: flex-start;
            justify-content: unset;
            gap: 13px;
        }

        .ai {
            align-self: flex-start;
            background-color: #eee;
            color: black;
            display: flex;
            align-items: flex-start;
            justify-content: unset;
            gap: 13px;
        }

        #input-container {
            display: flex;
            height: 70px;
            border-radius: 10px;
            margin-bottom: 10px;
        }

        #message {
            width: 80%;
            border: 0;
            font-size: 16px;
            padding: 10px;
            border-radius: 10px 0 0 10px;
            background-color: #eee;
        }

        #message:focus-visible {
            outline: 0;
        }

        #send {
            width: 20%;
            max-width: 200px;
            background-color: #3635f0;
            color: white;
            border: none;
            font-size: 16px;
            cursor: pointer;
        }

        #clear-history {
            width: 80px;
            font-size: 16px;
            border: none;
            color: white;
            background-color: #1ecbae;
            border-radius: 0 10px 10px 0;
            cursor: pointer;
        }

        .disable-button {
            background-color: #eee !important;
            cursor: not-allowed !important;
            color: #c3c3c3 !important;
        }

        .cursor {
            height: 1.2em;
            width: 0.7em;
            background-color: #374151;
            animation: blink 1s infinite;
        }

        /* Animation */
        @keyframes blink {
            0% {
                opacity: 1;
            }

            50% {
                opacity: 0;
            }

            100% {
                opacity: 1;
            }
        }
    </style>
</head>

<body>
    <div id="chat-container">
        <div id="title-bar">
            <img src="https://cdn.copilot.live/v2/original/icons/JioGPT_icon.png" width="40px" height="40px" />
            DwightAI
        </div>
        <div id="chat"></div>
        <div id="input-container">
            <input id="message" type="text" placeholder="Type your message" />
            <button id="send" title="Send Message">Send</button>
            <button id="clear-history" title="Clear history">Clear</button>
        </div>
    </div>

    <!-- Import marked.js for markdown rendering -->
    <script src="https://cdn.jsdelivr.net/npm/marked@5.1.0/marked.min.js"></script>

    <script>
        const renderer = new marked.Renderer();
        const linkRenderer = renderer.link;
        renderer.link = (href, title, text) => {
            const html = linkRenderer.call(renderer, href, title, text);
            return html.replace(
                /^<a /,
                '<a target="_blank" rel="noopener noreferrer" '
            );
        };
        marked.setOptions({ renderer });

        class StateManager {
            constructor(initialState = {}) {
                this.state = initialState;
                this.listeners = [];
            }
            getState() {
                return this.state;
            }
            subscribe(listener) {
                this.listeners.push(listener);
            }
            updateState(newState) {
                this.state = { ...this.state, ...newState };
                this.listeners.forEach((listener) => listener(this.state));
            }
        }
        const stateManager = new StateManager({ inprogress: false });

        const chatElement = document.getElementById("chat");
        document.addEventListener("DOMContentLoaded", (event) => {
            const messageElement = document.querySelector("#message");
            const sendButton = document.querySelector("#send");
            const clearButton = document.querySelector("#clear-history");

            sendButton.addEventListener("click", sendMessage);
            messageElement.addEventListener("keydown", (event) => {
                if (event.key === "Enter") {
                    const inprogress = stateManager.getState().inprogress;
                    if (!inprogress) {
                        sendMessage();
                    }
                }
            });
            clearButton.addEventListener("click", clearHistory);

            loadChatHistory();
        });

        async function loadChatHistory() {
            const response = await fetch("/history", { method: "GET" });

            if (!response.ok) {
                console.error("Failed to fetch chat history");
                return;
            }

            const chatHistory = await response.json();

            for (const message of chatHistory) {
                appendMessage(
                    message.content,
                    message.role === "user" ? "user" : "ai"
                );
            }
        }

        async function clearHistory() {
            const response = await fetch("/history", { method: "DELETE" });

            if (response.ok) {
                chatElement.replaceChildren(chatElement.firstChild);

                stateManager.updateState({ inprogress: false });
            } else {
                console.error("Failed to clear chat history");
            }
        }

        async function sendMessage() {
            const messageElement = document.querySelector("#message");
            const message = messageElement.value;
            messageElement.value = "";
            appendMessage(message, "user");
            const aiMessageContainer = appendMessage("", "ai");
            const aiMessageElement = aiMessageContainer.childNodes[1];
            const sendButton = document.getElementById("send");
            sendButton.disabled = true;
            sendButton.classList.add("disable-button");
            const cursor = document.createElement("div");
            cursor.classList.add("cursor");
            aiMessageElement.appendChild(cursor)
            const response = await fetch("/openai", {
                method: "POST",
                headers: { "Content-Type": "application/json" },
                body: JSON.stringify({ prompt: message }),
            });

            const reader = response.body
                .pipeThrough(new TextDecoderStream())
                .getReader();
            let serverMessage = "";
            let count = 0;
            while (true) {
                stateManager.updateState({ inprogress: true });
                const { value, done } = await reader.read();
                if (done) {
                    stateManager.updateState({ inprogress: false });
                    document.getElementById("send").disabled = false;
                    sendButton.disabled = false;
                    sendButton.classList.remove("disable-button");
                    break;
                }
                serverMessage += value;
                aiMessageElement.innerHTML = marked.parse(
                    serverMessage.replace(/\\n/g, "<br>"),
                    { breaks: true }
                );
                if (
                    count % 10 == 0 &&
                    chatElement.scrollHeight -
                    chatElement.scrollTop -
                    chatElement.clientHeight <=
                    44
                ) {
                    aiMessageElement.scrollIntoView();
                }
                count++;
            }
            chatElement.scrollTop = chatElement.scrollHeight;
        }

        function appendMessage(message, sender) {
            const div = document.createElement("div");
            div.classList.add("message", "markdown-body", sender);
            div.appendChild(createIcon(sender));
            div.appendChild(createTextElement(message, sender));
            chatElement.appendChild(div);
            div.scrollIntoView();
            chatElement.scrollTop = chatElement.scrollHeight;
            return div;
        }

        function createIcon(sender) {
            const icon = document.createElement("img");
            icon.style.width = "40px";
            icon.style.height = "40px";
            icon.style.marginTop = "10px";
            icon.style.backgroundColor = "unset";
            icon.alt = sender.charAt(0).toUpperCase() + sender.slice(1) + " Icon";
            icon.src =
                sender === "ai"
                    ? "https://cdn.copilot.live/v2/original/icons/JioGPT_icon.png"
                    : "https://img.icons8.com/?size=512&id=108296&format=png";
            return icon;
        }

        function createTextElement(message, sender) {
            const textElement = document.createElement("div");
            textElement.innerHTML = marked.parse(message, { breaks: true });
            return textElement;
        }
    </script>
</body>

</html>