")
.addClass(
"p-2 hover:bg-gray-100 cursor-pointer author-item transition-colors text-sm",
)
.attr("data-id", author.id)
.attr("data-name", author.name)
.text(author.name)
.appendTo($dropdown);
});
}
function initGenres(genres) {
const $dropdown = $("#genre-dropdown");
$dropdown.empty();
genres.forEach((genre) => {
$("
")
.addClass(
"p-2 hover:bg-gray-100 cursor-pointer genre-item transition-colors text-sm",
)
.attr("data-id", genre.id)
.attr("data-name", genre.name)
.text(genre.name)
.appendTo($dropdown);
});
}
function renderAuthorChips() {
const $container = $("#selected-authors-container");
const $dropdown = $("#author-dropdown");
$container.empty();
selectedAuthors.forEach((name, id) => {
$(`
${Utils.escapeHtml(name)}
`).appendTo($container);
});
$dropdown.find(".author-item").each(function () {
const id = parseInt($(this).data("id"));
if (selectedAuthors.has(id)) {
$(this)
.addClass("bg-gray-200 text-gray-900 font-semibold")
.removeClass("hover:bg-gray-100");
} else {
$(this)
.removeClass("bg-gray-200 text-gray-900 font-semibold")
.addClass("hover:bg-gray-100");
}
});
}
function renderGenreChips() {
const $container = $("#selected-genres-container");
const $dropdown = $("#genre-dropdown");
$container.empty();
selectedGenres.forEach((name, id) => {
$(`
${Utils.escapeHtml(name)}
`).appendTo($container);
});
$dropdown.find(".genre-item").each(function () {
const id = parseInt($(this).data("id"));
if (selectedGenres.has(id)) {
$(this)
.addClass("bg-gray-200 text-gray-900 font-semibold")
.removeClass("hover:bg-gray-100");
} else {
$(this)
.removeClass("bg-gray-200 text-gray-900 font-semibold")
.addClass("hover:bg-gray-100");
}
});
}
function initializeDropdownListeners() {
const $authorInput = $("#author-search-input");
const $authorDropdown = $("#author-dropdown");
const $authorContainer = $("#selected-authors-container");
$authorInput.on("focus", function () {
$authorDropdown.removeClass("hidden");
});
$authorInput.on("input", function () {
const val = $(this).val().toLowerCase();
$authorDropdown.removeClass("hidden");
$authorDropdown.find(".author-item").each(function () {
const text = $(this).text().toLowerCase();
$(this).toggle(text.includes(val));
});
});
$authorDropdown.on("click", ".author-item", function (e) {
e.stopPropagation();
const id = parseInt($(this).data("id"));
const name = $(this).data("name");
if (selectedAuthors.has(id)) {
selectedAuthors.delete(id);
} else {
selectedAuthors.set(id, name);
}
$authorInput.val("");
$authorDropdown.find(".author-item").show();
renderAuthorChips();
$authorInput[0].focus();
});
$authorContainer.on("click", ".remove-author", function (e) {
e.stopPropagation();
const id = parseInt($(this).data("id"));
selectedAuthors.delete(id);
renderAuthorChips();
});
const $genreInput = $("#genre-search-input");
const $genreDropdown = $("#genre-dropdown");
const $genreContainer = $("#selected-genres-container");
$genreInput.on("focus", function () {
$genreDropdown.removeClass("hidden");
});
$genreInput.on("input", function () {
const val = $(this).val().toLowerCase();
$genreDropdown.removeClass("hidden");
$genreDropdown.find(".genre-item").each(function () {
const text = $(this).text().toLowerCase();
$(this).toggle(text.includes(val));
});
});
$genreDropdown.on("click", ".genre-item", function (e) {
e.stopPropagation();
const id = parseInt($(this).data("id"));
const name = $(this).data("name");
if (selectedGenres.has(id)) {
selectedGenres.delete(id);
} else {
selectedGenres.set(id, name);
}
$genreInput.val("");
$genreDropdown.find(".genre-item").show();
renderGenreChips();
$genreInput[0].focus();
});
$genreContainer.on("click", ".remove-genre", function (e) {
e.stopPropagation();
const id = parseInt($(this).data("id"));
selectedGenres.delete(id);
renderGenreChips();
});
$(document).on("click", function (e) {
if (
!$(e.target).closest(
"#author-search-input, #author-dropdown, #selected-authors-container",
).length
) {
$authorDropdown.addClass("hidden");
}
if (
!$(e.target).closest(
"#genre-search-input, #genre-dropdown, #selected-genres-container",
).length
) {
$genreDropdown.addClass("hidden");
}
});
}
$form.on("submit", async function (e) {
e.preventDefault();
const title = $("#book-title").val().trim();
const description = $("#book-description").val().trim();
const pageCount = parseInt($("#book-page-count").val()) || null;
if (!title) {
Utils.showToast("Введите название книги", "error");
return;
}
if (!pageCount) {
Utils.showToast("Введите количество страниц", "error");
return;
}
setLoading(true);
try {
const bookPayload = {
title: title,
description: description || null,
page_count: pageCount,
};
const createdBook = await Api.post("/api/books/", bookPayload);
const linkPromises = [];
selectedAuthors.forEach((_, authorId) => {
linkPromises.push(
Api.post(
`/api/relationships/author-book?author_id=${authorId}&book_id=${createdBook.id}`,
),
);
});
selectedGenres.forEach((_, genreId) => {
linkPromises.push(
Api.post(
`/api/relationships/genre-book?genre_id=${genreId}&book_id=${createdBook.id}`,
),
);
});
if (linkPromises.length > 0) {
await Promise.allSettled(linkPromises);
}
showSuccess(createdBook);
} catch (error) {
console.error("Ошибка создания:", error);
let errorMsg = "Произошла ошибка при создании книги";
if (error.responseJSON && error.responseJSON.detail) {
errorMsg = error.responseJSON.detail;
} else if (error.status === 401) {
errorMsg = "Вы не авторизованы";
} else if (error.status === 403) {
errorMsg = "У вас недостаточно прав";
}
Utils.showToast(errorMsg, "error");
} finally {
setLoading(false);
}
});
function setLoading(isLoading) {
$submitBtn.prop("disabled", isLoading);
if (isLoading) {
$submitText.text("Сохранение...");
$loadingSpinner.removeClass("hidden");
} else {
$submitText.text("Создать книгу");
$loadingSpinner.addClass("hidden");
}
}
function showSuccess(book) {
$("#modal-book-title").text(book.title);
$("#modal-link-btn").attr("href", `/book/${book.id}`);
$successModal.removeClass("hidden");
}
function resetForm() {
$form[0].reset();
selectedAuthors.clear();
selectedGenres.clear();
$("#selected-authors-container").empty();
$("#selected-genres-container").empty();
$("#title-counter").text("0/255");
$("#desc-counter").text("0/2000");
$("#author-dropdown .author-item")
.removeClass("bg-gray-200 text-gray-900 font-semibold")
.addClass("hover:bg-gray-100");
$("#genre-dropdown .genre-item")
.removeClass("bg-gray-200 text-gray-900 font-semibold")
.addClass("hover:bg-gray-100");
}
$("#modal-close-btn").on("click", function () {
$successModal.addClass("hidden");
resetForm();
window.scrollTo(0, 0);
});
$successModal.on("click", function (e) {
if (e.target === this) {
window.location.href = "/books";
}
});
$(document).on("keydown", function (e) {
if (e.key === "Escape" && !$successModal.hasClass("hidden")) {
window.location.href = "/books";
}
});
function initAiAssistant() {
const $aiLogo = $("#ai-logo");
const $aiWidget = $("#ai-widget");
const $aiInput = $("#ai-input");
const $btnRun = $("#ai-btn-run");
const $btnStop = $("#ai-btn-stop");
const $logWrap = $("#ai-log-container");
const $logEntries = $("#ai-log-entries");
const $dot = $("#ai-status-dot");
const $ping = $("#ai-status-ping");
const $statusTxt = $("#ai-status-text");
const AI_FIELD_TYPES = ["title", "description", "page_count"];
const AI_FIELD_LABELS = {
title: "Название",
description: "Описание",
page_count: "Кол-во страниц",
};
if ($aiLogo) {
$aiLogo.on("click", () => {
if (ws && ws.readyState === WebSocket.OPEN) {
ws.close();
}
hideWidget();
aiRunning = false;
curThinkEl = null;
curInfoEl = null;
lastType = null;
twQueues.clear();
aiStatus("ready");
connectWs();
});
}
let ws = null;
let aiRunning = false;
let curThinkEl = null;
let curInfoEl = null;
let lastType = null;
let idleTimer = null;
let thinkTimer = null;
let thinkStartTime = null;
const twQueues = new Map();
function twEnqueue(el, text, scrollCb) {
const raw = el instanceof $ ? el[0] : el;
if (!raw) return;
if (!twQueues.has(raw)) {
twQueues.set(raw, { buffer: "", raf: null, scrollCb: null });
}
const q = twQueues.get(raw);
q.buffer += text;
if (scrollCb) q.scrollCb = scrollCb;
if (!q.raf) twTick(raw);
}
function twFlush(el) {
const raw = el instanceof $ ? el[0] : el;
if (!raw) return;
const q = twQueues.get(raw);
if (!q) return;
if (q.raf) cancelAnimationFrame(q.raf);
raw.textContent += q.buffer;
q.buffer = "";
q.raf = null;
twQueues.delete(raw);
}
function twTick(raw) {
const q = twQueues.get(raw);
if (!q || !q.buffer.length) {
if (q) q.raf = null;
return;
}
const n = Math.max(1, Math.ceil(q.buffer.length * 0.1));
raw.textContent += q.buffer.substring(0, n);
q.buffer = q.buffer.substring(n);
if (q.scrollCb) q.scrollCb();
scrollLog();
if (q.buffer.length) {
q.raf = requestAnimationFrame(() => twTick(raw));
} else {
q.raf = null;
}
}
let twRafId = null;
function startTwTick() {
if (twRafId === null) {
const tick = () => {
twQueues.forEach((_, el) => twTick(el));
twRafId = requestAnimationFrame(tick);
};
twRafId = requestAnimationFrame(tick);
}
}
function stopTwTick() {
if (twRafId !== null) {
cancelAnimationFrame(twRafId);
twRafId = null;
}
}
function aiStatus(s) {
if (s === "streaming") {
$dot.removeClass("bg-gray-300 hidden").addClass("bg-green-500");
$ping.removeClass("hidden");
$statusTxt.text("Пишет…");
} else if (s === "connected") {
$dot.removeClass("bg-gray-300 hidden").addClass("bg-green-500");
$ping.addClass("hidden");
$statusTxt.text("Подключено");
} else {
$dot.removeClass("bg-green-500").addClass("bg-gray-300 hidden");
$ping.addClass("hidden");
$statusTxt.text("Готов");
}
}
function setAiRunning(isRunning) {
aiRunning = isRunning;
$btnRun[0].classList.toggle("hidden", isRunning);
$btnStop[0].classList.toggle("hidden", !isRunning);
$aiInput.prop("disabled", isRunning);
aiStatus(isRunning ? "streaming" : "ready");
}
function scrollLog() {
const el = $logWrap[0];
if (el) el.scrollTop = el.scrollHeight;
}
function resetIdle() {
clearTimeout(idleTimer);
if (aiRunning) {
idleTimer = setTimeout(() => finishResponse(), 1500);
}
}
function getFields() {
return {
title: $titleInput.val() || null,
description: $descInput.val() || null,
page_count: $pagesInput.val() ? parseInt($pagesInput.val(), 10) : null,
};
}
function aiApplyField(type, value) {
if (type === "title") {
$titleInput.val(value === null ? "" : value).trigger("input");
} else if (type === "description") {
$descInput.val(value === null ? "" : value).trigger("input");
} else if (type === "page_count") {
$pagesInput.val(value === null ? "" : value).trigger("input");
}
}
function formatThinkTime(sec) {
if (sec < 60) {
return sec.toFixed(1) + "с";
}
const m = Math.floor(sec / 60);
const s = Math.floor(sec % 60);
return m + "м " + String(s).padStart(2, "0") + "с";
}
function addThinkBlock() {
const id = "ai-t-" + Date.now();
$logEntries.append(`
`);
$(`[data-toggle="${id}-b"]`).on("click", function () {
$(`#${id}-b`).toggleClass("hidden");
$(this).find(".ai-t-chev").toggleClass("rotate-180");
});
curThinkEl = $(`#${id}`);
scrollLog();
thinkStartTime = performance.now();
clearInterval(thinkTimer);
thinkTimer = setInterval(() => {
if (!curThinkEl) return;
const elapsed = (performance.now() - thinkStartTime) / 1000;
curThinkEl.find(".ai-t-timer").text(formatThinkTime(elapsed));
}, 100);
}
function appendThink(text) {
if (!curThinkEl) addThinkBlock();
const p = curThinkEl.find(".ai-t-txt")[0];
const sp = p ? p.parentElement : null;
twEnqueue(p, text, () => {
if (sp) sp.scrollTop = sp.scrollHeight;
});
}
function endThink() {
if (!curThinkEl) return;
clearInterval(thinkTimer);
thinkTimer = null;
if (thinkStartTime) {
const elapsed = (performance.now() - thinkStartTime) / 1000;
curThinkEl.find(".ai-t-timer").text(formatThinkTime(elapsed));
thinkStartTime = null;
}
const p = curThinkEl.find(".ai-t-txt")[0];
twFlush(p);
curThinkEl.find(".ai-t-txt").removeClass("typing-cursor");
curThinkEl.find(".ai-t-spin").removeClass("animate-spin").html(`
`);
const id = curThinkEl.attr("id");
if (id) {
$(`#${id}-b`).addClass("hidden");
curThinkEl.find(".ai-t-chev").addClass("rotate-180");
}
curThinkEl = null;
}
function addToolEntry(field, value) {
const label = AI_FIELD_LABELS[field] || field;
const disp =
value === null
? '
очищено'
: Utils.escapeHtml(String(value));
$logEntries.append(`
`);
scrollLog();
}
function addInfoBlock() {
const id = "ai-i-" + Date.now();
$logEntries.append(`
`);
curInfoEl = $(`#${id}`);
scrollLog();
}
function appendInfo(text) {
if (!curInfoEl) addInfoBlock();
const el = curInfoEl.find(".ai-i-txt")[0];
twEnqueue(el, text);
}
function endInfo() {
if (!curInfoEl) return;
const el = curInfoEl.find(".ai-i-txt")[0];
twFlush(el);
curInfoEl.find(".ai-i-txt").removeClass("typing-cursor");
curInfoEl = null;
}
function addPromptSep(text) {
$logEntries.append(`
${Utils.escapeHtml(text)}
`);
scrollLog();
}
function hideWidget() {
clearInterval(thinkTimer);
thinkTimer = null;
thinkStartTime = null;
$aiWidget.addClass("hidden");
$logWrap.addClass("hidden");
$logEntries.empty();
curThinkEl = null;
curInfoEl = null;
lastType = null;
twQueues.forEach((q) => {
if (q.raf) cancelAnimationFrame(q.raf);
});
twQueues.clear();
}
function finishResponse() {
endThink();
endInfo();
setAiRunning(false);
}
function handleMsg(raw) {
let msg;
try {
msg = JSON.parse(raw);
} catch (e) {
return;
}
const { type, value } = msg;
if (type === "end") {
finishResponse();
return;
}
if (type === "thinking") {
if (lastType !== "thinking") {
endThink();
endInfo();
curThinkEl = null;
}
appendThink(value);
lastType = "thinking";
return;
}
if (AI_FIELD_TYPES.includes(type)) {
endThink();
aiApplyField(type, value);
addToolEntry(type, value);
lastType = "tool";
return;
}
if (type === "info") {
if (lastType !== "info") {
endThink();
endInfo();
curInfoEl = null;
}
appendInfo(value);
lastType = "info";
return;
}
}
function connectWs() {
if (
ws &&
(ws.readyState === WebSocket.OPEN ||
ws.readyState === WebSocket.CONNECTING)
)
return;
const proto = window.location.protocol === "https:" ? "wss:" : "ws:";
const token = StorageHelper.get("access_token");
if (!token) {
Utils.showToast("Вы не авторизованы", "error");
return;
}
ws = new WebSocket(
`${proto}//${window.location.host}/api/llm/book?token=${token}`,
);
ws.onopen = () => {
aiStatus("connected");
$aiWidget.removeClass("hidden");
};
ws.onclose = (e) => {
const wasRunning = aiRunning;
ws = null;
if (wasRunning) {
finishResponse();
}
if (e.code === 1011 || e.code === 1006) {
hideWidget();
} else if (e.code !== 1000 && e.reason) {
Utils.showToast(e.reason, "error");
}
aiStatus("ready");
};
ws.onerror = () => {
Utils.showToast("Ошибка WebSocket соединения", "error");
};
ws.onmessage = (e) => handleMsg(e.data);
}
function doSend(prompt) {
setAiRunning(true);
lastType = null;
curThinkEl = null;
curInfoEl = null;
$logWrap.removeClass("hidden");
addPromptSep(prompt);
try {
ws.send(JSON.stringify({ prompt, fields: getFields() }));
} catch (e) {
console.error("Failed to send AI prompt", e);
finishResponse();
}
$aiInput.val("");
}
function sendPrompt(prompt) {
if (!prompt) {
$aiInput[0].focus();
return;
}
if (!ws || ws.readyState !== WebSocket.OPEN) {
connectWs();
let waited = 0;
const iv = setInterval(() => {
waited += 50;
if (ws && ws.readyState === WebSocket.OPEN) {
clearInterval(iv);
doSend(prompt);
} else if (waited >= 5000) {
clearInterval(iv);
Utils.showToast("Не удалось подключиться", "error");
setAiRunning(false);
}
}, 50);
return;
}
doSend(prompt);
}
$btnRun.on("click", () => {
const p = $aiInput.val().trim();
if (!p) {
$aiInput.addClass("placeholder-red-400");
setTimeout(() => $aiInput.removeClass("placeholder-red-400"), 500);
$aiInput[0].focus();
return;
}
sendPrompt(p);
});
$aiInput.on("keydown", (e) => {
if (e.key === "Enter") {
e.preventDefault();
e.stopPropagation();
if (!aiRunning) $btnRun.trigger("click");
}
});
$aiInput.on("input", () => {
$aiInput.removeClass("placeholder-red-400");
});
$btnStop.on("click", () => {
if (ws) {
ws.close();
ws = null;
}
finishResponse();
});
connectWs();
}
});