-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
executable file
·159 lines (141 loc) · 5.17 KB
/
index.html
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
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8" />
<title>AI Chat Interface</title>
<!-- Подключаем Tailwind CSS через CDN -->
<script src="https://cdn.tailwindcss.com"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.1/styles/base16/dracula.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
<style>
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(10px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.fade-in {
animation: fadeIn 0.5s ease forwards;
}
</style>
</head>
<body class="bg-gray-900 text-gray-200">
<div class="container mx-auto max-w-5xl py-8">
<div
class="flex flex-col bg-gray-800 rounded-lg shadow-lg h-[90vh] overflow-hidden"
>
<div id="chat-window" class="flex-grow overflow-y-auto p-4 space-y-4"></div>
<div class="flex p-4 border-t border-gray-700">
<textarea
id="message-input"
class="flex-grow rounded-l-lg px-4 py-2 bg-gray-700 text-gray-200 focus:outline-none"
placeholder="Введите сообщение..."
rows="1"
style="resize: none"
></textarea>
<button
id="send-button"
class="px-4 py-2 bg-indigo-600 hover:bg-indigo-500 text-white rounded-r-lg"
>
Отправить
</button>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/showdown@1.9.1/dist/showdown.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/go.min.js"></script>
<script>
const ws = new WebSocket(`ws://${location.host}/ws`);
const chatWindow = document.getElementById("chat-window");
const messageInput = document.getElementById("message-input");
const sendButton = document.getElementById("send-button");
ws.onopen = () => {
addMessage("assistant", "💬 Привет, я твой AI Python помощник");
};
ws.onmessage = (event) => {
const data = JSON.parse(event.data);
if (data.role === "assistant") {
// Удаляем индикатор загрузки, если он есть
const loadingMessage = document.querySelector(".message.loading");
if (loadingMessage) loadingMessage.remove();
addMessage("assistant", data.content);
}
};
sendButton.addEventListener("click", sendMessage);
messageInput.addEventListener("keypress", (e) => {
if (e.key === "Enter") {
e.preventDefault();
sendMessage();
}
});
function scrollToBottom() {
chatWindow.scrollTop = chatWindow.scrollHeight;
}
function sendMessage() {
const text = messageInput.value.trim();
if (text === "") return;
addMessage("user", text);
ws.send(text);
messageInput.value = "";
// Добавляем индикатор загрузки
const loadingDiv = document.createElement("div");
loadingDiv.classList.add(
"message",
"assistant",
"loading",
"fade-in",
"text-left"
);
const bubbleDiv = document.createElement("div");
bubbleDiv.classList.add(
"inline-block",
"px-3",
"py-2",
"rounded-lg",
"mt-1",
"bg-gray-700",
"text-gray-200"
);
bubbleDiv.innerHTML = `<span>Думаю</span>
<svg class="inline-block animate-spin ml-2 h-4 w-4 text-white" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8v8H4z"></path>
</svg>`;
loadingDiv.appendChild(bubbleDiv);
chatWindow.appendChild(loadingDiv);
scrollToBottom();
}
function addMessage(role, text) {
const messageDiv = document.createElement("div");
messageDiv.classList.add("message", role, "fade-in", "w-full");
messageDiv.classList.add(role === "user" ? "text-right" : "text-left");
const bubbleDiv = document.createElement("div");
bubbleDiv.classList.add(
"inline-block",
"max-w-full",
"px-3",
"py-2",
"rounded-lg",
"mt-1"
);
if (role === "user") {
bubbleDiv.classList.add("bg-indigo-600", "text-white");
} else {
bubbleDiv.classList.add("bg-gray-700", "text-gray-200");
}
// Конвертация Markdown в HTML
const converter = new showdown.Converter();
const htmlContent = converter.makeHtml(text);
bubbleDiv.innerHTML = htmlContent;
messageDiv.appendChild(bubbleDiv);
chatWindow.appendChild(messageDiv);
hljs.highlightAll();
scrollToBottom();
}
</script>
</body>
</html>