Fixed SD RPG
This commit is contained in:
+392
-43
@@ -1,9 +1,19 @@
|
||||
import { sessionId, currentPersona, dom } from './state.js';
|
||||
import { parseImagePromptFromContent, copyToClipboard } from './utils.js';
|
||||
import { parseImagePromptFromContent, copyToClipboard, splitSdPromptForCopy } from './utils.js';
|
||||
import {
|
||||
attachFormatToggle,
|
||||
initBubbleContent,
|
||||
applyDiceOverrideToBubble,
|
||||
isPlayerChoiceContent,
|
||||
} from './rpFormat.js';
|
||||
|
||||
let _pendingUserBubble = null;
|
||||
let _selectedQuestId = null;
|
||||
let _questsCache = [];
|
||||
|
||||
export async function initChat(options = {}) {
|
||||
if (!sessionId || !currentPersona) return;
|
||||
const payload = { message: '', session_id: sessionId, persona_id: currentPersona };
|
||||
if (!sessionId) return;
|
||||
const payload = { message: '', session_id: sessionId };
|
||||
if (options.first_mes_override?.trim()) payload.first_mes_override = options.first_mes_override.trim();
|
||||
const res = await fetch('/chat/init', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload) });
|
||||
if (!res.ok) return;
|
||||
@@ -16,19 +26,22 @@ export function updateEmptyState() {
|
||||
dom.emptyState?.classList.toggle('hidden', !!hasMessages);
|
||||
}
|
||||
|
||||
export function createImagePromptBlock(promptText) {
|
||||
function createImagePromptBlockSingle(label, promptText) {
|
||||
const block = document.createElement('div');
|
||||
block.className = 'image-prompt-block';
|
||||
const header = document.createElement('div');
|
||||
header.className = 'image-prompt-header';
|
||||
header.innerHTML = '<span>🎨 SD prompt</span>';
|
||||
header.innerHTML = `<span>🎨 ${label}</span>`;
|
||||
|
||||
const copyBtn = document.createElement('button');
|
||||
copyBtn.type = 'button';
|
||||
copyBtn.className = 'copy-prompt-btn';
|
||||
copyBtn.textContent = 'Копировать';
|
||||
copyBtn.addEventListener('click', async () => {
|
||||
const ok = await copyToClipboard(promptText);
|
||||
copyBtn.addEventListener('click', async (e) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
const full = textEl.textContent?.trim() || promptText || '';
|
||||
const ok = await copyToClipboard(splitSdPromptForCopy(full));
|
||||
copyBtn.textContent = ok ? 'Скопировано' : 'Ошибка';
|
||||
setTimeout(() => { copyBtn.textContent = 'Копировать'; }, 1500);
|
||||
});
|
||||
@@ -39,11 +52,10 @@ export function createImagePromptBlock(promptText) {
|
||||
regenBtn.className = 'copy-prompt-btn';
|
||||
regenBtn.textContent = '🖼 Перегенерировать';
|
||||
regenBtn.addEventListener('click', async () => {
|
||||
const wrapper = block.parentElement;
|
||||
const wrapper = block.closest('.message');
|
||||
regenBtn.disabled = true;
|
||||
regenBtn.textContent = '⏳…';
|
||||
wrapper?.querySelector('.chat-image')?.remove();
|
||||
wrapper?.querySelector('.image-error')?.remove();
|
||||
wrapper?.querySelectorAll('.chat-image-wrap, .chat-image, .image-error').forEach(el => el.remove());
|
||||
showImageGenerating(wrapper);
|
||||
try {
|
||||
const res = await fetch('/images/generate', {
|
||||
@@ -76,6 +88,26 @@ export function createImagePromptBlock(promptText) {
|
||||
return block;
|
||||
}
|
||||
|
||||
export function createImagePromptBlock(promptText, promptAlt = null) {
|
||||
const wrap = document.createElement('div');
|
||||
wrap.className = 'image-prompt-blocks';
|
||||
wrap.appendChild(createImagePromptBlockSingle('SD prompt', promptText));
|
||||
const alt = (promptAlt || '').trim();
|
||||
const main = (promptText || '').trim();
|
||||
if (alt && alt !== main) {
|
||||
wrap.appendChild(createImagePromptBlockSingle('SD prompt (только теги)', promptAlt));
|
||||
}
|
||||
return wrap;
|
||||
}
|
||||
|
||||
/** Replace or create tag + optional hybrid prompt blocks under a message. */
|
||||
export function ensureImagePromptBlocks(wrapper, tagPrompt, altPrompt = null) {
|
||||
if (!wrapper || !tagPrompt) return;
|
||||
wrapper.querySelector('.image-prompt-blocks')?.remove();
|
||||
wrapper.querySelectorAll('.image-prompt-block').forEach(el => el.remove());
|
||||
wrapper.appendChild(createImagePromptBlock(tagPrompt, altPrompt || null));
|
||||
}
|
||||
|
||||
const OUTCOME_CLASS = {
|
||||
'critical failure': 'outcome-crit-fail',
|
||||
'failure': 'outcome-fail',
|
||||
@@ -113,26 +145,132 @@ function renderNarratorMessage(narrator) {
|
||||
return el;
|
||||
}
|
||||
|
||||
function renderChoices(wrapper, choices) {
|
||||
if (!choices?.length) return;
|
||||
export function removeChoiceRows(wrapper) {
|
||||
wrapper?.querySelectorAll('.choice-row').forEach(el => el.remove());
|
||||
}
|
||||
|
||||
export function ensureMessageActionsLast(wrapper) {
|
||||
const actions = wrapper?.querySelector('.message-actions');
|
||||
if (actions) wrapper.appendChild(actions);
|
||||
}
|
||||
|
||||
export function renderChoices(wrapper, choices) {
|
||||
if (!choices?.length || !wrapper) return;
|
||||
removeChoiceRows(wrapper);
|
||||
|
||||
const plotChoices = choices.filter(c => c?.source === 'plot_beat');
|
||||
const otherChoices = choices.filter(c => c?.source !== 'plot_beat');
|
||||
|
||||
const row = document.createElement('div');
|
||||
row.className = 'choice-row';
|
||||
for (const c of choices) {
|
||||
|
||||
const appendBtn = (container, c) => {
|
||||
const btn = document.createElement('button');
|
||||
btn.type = 'button';
|
||||
btn.className = 'choice-btn';
|
||||
btn.textContent = c.label;
|
||||
btn.addEventListener('click', () => sendMessage(c.label, true));
|
||||
row.appendChild(btn);
|
||||
}
|
||||
btn.className = c.source === 'plot_beat' ? 'choice-btn choice-btn-plot' : 'choice-btn';
|
||||
const label = c.label || '';
|
||||
btn.textContent = label;
|
||||
if (c.beat_title) btn.title = c.beat_title;
|
||||
btn.addEventListener('click', () => {
|
||||
removeChoiceRows(wrapper);
|
||||
sendMessage(label, true);
|
||||
});
|
||||
container.appendChild(btn);
|
||||
};
|
||||
|
||||
const appendSection = (items, { plot } = {}) => {
|
||||
if (!items.length) return;
|
||||
const section = document.createElement('div');
|
||||
section.className = plot ? 'choice-section choice-section-plot' : 'choice-section';
|
||||
if (plot) {
|
||||
const hdr = document.createElement('div');
|
||||
hdr.className = 'choice-section-label';
|
||||
const title = items[0].beat_title || 'Сюжетный beat';
|
||||
hdr.textContent = `📜 ${title}`;
|
||||
section.appendChild(hdr);
|
||||
const inj = (items[0].beat_injection || '').trim();
|
||||
if (inj) {
|
||||
const teaser = document.createElement('div');
|
||||
teaser.className = 'choice-beat-teaser';
|
||||
teaser.textContent = inj.length > 220 ? `${inj.slice(0, 217)}…` : inj;
|
||||
section.appendChild(teaser);
|
||||
}
|
||||
} else if (plotChoices.length) {
|
||||
const hdr = document.createElement('div');
|
||||
hdr.className = 'choice-section-label choice-section-label-generic';
|
||||
hdr.textContent = '🔘 Дальше';
|
||||
section.appendChild(hdr);
|
||||
}
|
||||
const btns = document.createElement('div');
|
||||
btns.className = 'choice-section-btns';
|
||||
for (const c of items) appendBtn(btns, c);
|
||||
section.appendChild(btns);
|
||||
row.appendChild(section);
|
||||
};
|
||||
|
||||
appendSection(plotChoices, { plot: true });
|
||||
appendSection(otherChoices);
|
||||
|
||||
wrapper.appendChild(row);
|
||||
ensureMessageActionsLast(wrapper);
|
||||
}
|
||||
|
||||
function restoreChoicesFromHistory(messages) {
|
||||
const visible = messages.filter(m => m.role !== 'system');
|
||||
if (!visible.length) return;
|
||||
const last = visible[visible.length - 1];
|
||||
if (last.role !== 'assistant' || !last.choices_json) return;
|
||||
let choices = [];
|
||||
try {
|
||||
choices = JSON.parse(last.choices_json);
|
||||
} catch { return; }
|
||||
if (!choices?.length) return;
|
||||
const wrapper = dom.messagesEl.querySelector(
|
||||
`.message.assistant[data-message-id="${last.id}"]`,
|
||||
);
|
||||
if (wrapper) renderChoices(wrapper, choices);
|
||||
}
|
||||
|
||||
function finalizeAssistantMessage(wrapper, messageId, choices) {
|
||||
if (!wrapper || !messageId) return;
|
||||
wrapper.dataset.messageId = String(messageId);
|
||||
attachMessageActions(wrapper, Number(messageId), 'assistant');
|
||||
if (choices?.length) renderChoices(wrapper, choices);
|
||||
else ensureMessageActionsLast(wrapper);
|
||||
}
|
||||
|
||||
function showNarratorActivityHint(wrapper, meta) {
|
||||
if (!wrapper || !meta) return;
|
||||
wrapper.querySelector('.narrator-activity-hint')?.remove();
|
||||
const parts = [];
|
||||
if (meta.post_ok === false && meta.pre_ok === false) {
|
||||
parts.push('⚠️ Narrator LLM не ответил — проверьте ROUTER_KEY / RPG_NARRATOR_MODEL');
|
||||
} else {
|
||||
if (meta.dice) parts.push('🎲 бросок');
|
||||
if (meta.directives_count > 0) parts.push(`📋 ${meta.directives_count} указаний`);
|
||||
if (meta.choices_count > 0) parts.push(`🔘 ${meta.choices_count} выборов`);
|
||||
if (meta.status_quo) parts.push('🌍 status_quo');
|
||||
if (meta.beats_replenished) parts.push(`📜 +${meta.beats_replenished} beats`);
|
||||
if (meta.beat_mode === 'after_dice') parts.push('📜 beat (d20)');
|
||||
if (meta.beat_mode === 'llm') parts.push('📜 beat (AI)');
|
||||
if (meta.beat_mode === 'stuck_recovery') parts.push('📜 beat (recovery)');
|
||||
if (meta.beat_mode === 'trigger') parts.push('📜 beat (keywords)');
|
||||
if (meta.arc_pruned) parts.push(`🧹 −${meta.arc_pruned} beat`);
|
||||
if (meta.facts_added) parts.push(`📌 +${meta.facts_added} фактов`);
|
||||
}
|
||||
if (!parts.length) return;
|
||||
const el = document.createElement('div');
|
||||
el.className = 'narrator-activity-hint';
|
||||
el.textContent = `📖 Narrator: ${parts.join(' · ')}`;
|
||||
wrapper.appendChild(el);
|
||||
ensureMessageActionsLast(wrapper);
|
||||
}
|
||||
|
||||
function renderDebugBlocks(wrapper, blocks) {
|
||||
if (!blocks?.length) return;
|
||||
for (const b of blocks) {
|
||||
if (!b?.text) continue;
|
||||
if (b.type === 'narrator_injection') {
|
||||
if (b.type === 'narrator_injection' || b.type === 'status_quo') {
|
||||
const w = document.createElement('div');
|
||||
w.className = 'message narrator';
|
||||
const lbl = document.createElement('div');
|
||||
@@ -148,16 +286,106 @@ function renderDebugBlocks(wrapper, blocks) {
|
||||
}
|
||||
}
|
||||
|
||||
function questIdEq(a, b) {
|
||||
return Number(a) === Number(b);
|
||||
}
|
||||
|
||||
function syncQuestActionButtons() {
|
||||
const doneBtn = document.getElementById('questBtnDone');
|
||||
const failBtn = document.getElementById('questBtnFail');
|
||||
const active = _selectedQuestId != null
|
||||
&& _questsCache.some(q => questIdEq(q.id, _selectedQuestId) && q.status === 'active');
|
||||
if (doneBtn) doneBtn.disabled = !active;
|
||||
if (failBtn) failBtn.disabled = !active;
|
||||
}
|
||||
|
||||
export function updateQuestPanel(quests) {
|
||||
const list = document.getElementById('questList');
|
||||
const actions = document.getElementById('questPanelActions');
|
||||
if (!list) return;
|
||||
_questsCache = quests || [];
|
||||
list.innerHTML = '';
|
||||
for (const q of quests) {
|
||||
|
||||
if (!_questsCache.length) {
|
||||
_selectedQuestId = null;
|
||||
syncQuestActionButtons();
|
||||
return;
|
||||
}
|
||||
|
||||
if (_selectedQuestId != null && !_questsCache.some(
|
||||
q => questIdEq(q.id, _selectedQuestId) && q.status === 'active',
|
||||
)) {
|
||||
_selectedQuestId = null;
|
||||
}
|
||||
|
||||
for (const q of _questsCache) {
|
||||
const el = document.createElement('div');
|
||||
el.className = `quest-item quest-${q.status}`;
|
||||
if (questIdEq(q.id, _selectedQuestId)) el.classList.add('quest-selected');
|
||||
if (q.status === 'done') el.classList.add('quest-done');
|
||||
if (q.status === 'failed') el.classList.add('quest-failed');
|
||||
el.dataset.questId = String(q.id);
|
||||
el.textContent = (q.status === 'done' ? '✅ ' : q.status === 'failed' ? '❌ ' : '🔸 ') + q.title;
|
||||
if (q.status === 'active') {
|
||||
el.addEventListener('click', () => {
|
||||
const qid = Number(q.id);
|
||||
_selectedQuestId = questIdEq(_selectedQuestId, qid) ? null : qid;
|
||||
updateQuestPanel(_questsCache);
|
||||
});
|
||||
}
|
||||
list.appendChild(el);
|
||||
}
|
||||
syncQuestActionButtons();
|
||||
}
|
||||
|
||||
async function patchSelectedQuest(status) {
|
||||
if (!sessionId || _selectedQuestId == null) {
|
||||
alert('Сначала выберите активный квест (🔸) в списке.');
|
||||
return;
|
||||
}
|
||||
const q = _questsCache.find(x => questIdEq(x.id, _selectedQuestId));
|
||||
if (!q || q.status !== 'active') return;
|
||||
|
||||
const verb = status === 'done' ? 'завершить' : 'отметить провалом';
|
||||
if (!confirm(`${verb.charAt(0).toUpperCase() + verb.slice(1)} квест?\n\n«${q.title}»`)) return;
|
||||
|
||||
const doneBtn = document.getElementById('questBtnDone');
|
||||
const failBtn = document.getElementById('questBtnFail');
|
||||
doneBtn && (doneBtn.disabled = true);
|
||||
failBtn && (failBtn.disabled = true);
|
||||
|
||||
try {
|
||||
const res = await fetch(`/sessions/${sessionId}/quests/${Number(_selectedQuestId)}`, {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ status }),
|
||||
});
|
||||
if (!res.ok) {
|
||||
const err = await res.json().catch(() => ({}));
|
||||
alert(err.detail || res.statusText);
|
||||
return;
|
||||
}
|
||||
_selectedQuestId = null;
|
||||
const listRes = await fetch(`/sessions/${sessionId}/quests`);
|
||||
if (listRes.ok) updateQuestPanel(await listRes.json());
|
||||
} finally {
|
||||
syncQuestActionButtons();
|
||||
}
|
||||
}
|
||||
|
||||
export function initQuestPanel() {
|
||||
const panel = document.getElementById('questPanel');
|
||||
if (!panel || panel.dataset.questBound === '1') return;
|
||||
panel.dataset.questBound = '1';
|
||||
panel.addEventListener('click', (e) => {
|
||||
if (e.target.id === 'questBtnDone') {
|
||||
e.preventDefault();
|
||||
patchSelectedQuest('done');
|
||||
} else if (e.target.id === 'questBtnFail') {
|
||||
e.preventDefault();
|
||||
patchSelectedQuest('failed');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function updateAffinityDisplay(affinity) {
|
||||
@@ -169,12 +397,38 @@ export function updateAffinityDisplay(affinity) {
|
||||
el.className = `affinity-display ${affinity > 5 ? 'affinity-high' : affinity < -3 ? 'affinity-low' : ''}`;
|
||||
}
|
||||
|
||||
export function appendChatImage(wrapper, imagePath) {
|
||||
export function updateStatsDisplay(stats) {
|
||||
const el = dom.statsDisplay;
|
||||
if (!el || !stats) return;
|
||||
const lust = Number(stats.lust ?? 0);
|
||||
const stamina = Number(stats.stamina ?? 10);
|
||||
const tension = Number(stats.tension ?? 0);
|
||||
el.textContent = `🔥${lust} ⚡${stamina} 😰${tension}`;
|
||||
el.className = 'stats-display';
|
||||
if (stamina <= 2 || tension >= 8) el.classList.add('stats-critical');
|
||||
else if (stamina <= 4 || tension >= 6) el.classList.add('stats-warn');
|
||||
el.classList.remove('hidden');
|
||||
}
|
||||
|
||||
export function hideStatsDisplay() {
|
||||
dom.statsDisplay?.classList.add('hidden');
|
||||
}
|
||||
|
||||
export function appendChatImage(wrapper, imagePath, label = '') {
|
||||
if (!imagePath) return;
|
||||
const figure = document.createElement('figure');
|
||||
figure.className = 'chat-image-wrap';
|
||||
if (label) {
|
||||
const cap = document.createElement('figcaption');
|
||||
cap.className = 'chat-image-label';
|
||||
cap.textContent = label;
|
||||
figure.appendChild(cap);
|
||||
}
|
||||
const img = document.createElement('img');
|
||||
img.className = 'chat-image';
|
||||
img.src = imagePath;
|
||||
wrapper.appendChild(img);
|
||||
figure.appendChild(img);
|
||||
wrapper.appendChild(figure);
|
||||
}
|
||||
|
||||
export function showImageGenerating(wrapper) {
|
||||
@@ -194,6 +448,7 @@ export function removeImageGenerating(wrapper) {
|
||||
function attachMessageActions(wrapper, messageId, role) {
|
||||
if (!messageId) return;
|
||||
wrapper.dataset.messageId = String(messageId);
|
||||
wrapper.querySelector('.message-actions')?.remove();
|
||||
const actions = document.createElement('div');
|
||||
actions.className = 'message-actions';
|
||||
const editBtn = document.createElement('button');
|
||||
@@ -216,7 +471,28 @@ function attachMessageActions(wrapper, messageId, role) {
|
||||
branchBtn.title = 'Ветка отсюда';
|
||||
branchBtn.addEventListener('click', () => forkFromMessage(messageId));
|
||||
actions.appendChild(branchBtn);
|
||||
const delBtn = document.createElement('button');
|
||||
delBtn.type = 'button';
|
||||
delBtn.textContent = '🗑';
|
||||
delBtn.title = 'Удалить сообщение и всё после него';
|
||||
delBtn.addEventListener('click', () => deleteMessageAndAfter(messageId, wrapper));
|
||||
actions.appendChild(delBtn);
|
||||
wrapper.appendChild(actions);
|
||||
ensureMessageActionsLast(wrapper);
|
||||
}
|
||||
|
||||
async function deleteMessageAndAfter(messageId, wrapper) {
|
||||
if (!sessionId || !messageId) return;
|
||||
if (!confirm('Удалить это сообщение и всю переписку после него?')) return;
|
||||
const res = await fetch(`/chat/messages/${messageId}`, { method: 'DELETE' });
|
||||
if (!res.ok) {
|
||||
const err = await res.json().catch(() => ({}));
|
||||
alert(err.detail || res.statusText);
|
||||
return;
|
||||
}
|
||||
await reloadChatFromServer(sessionId);
|
||||
const listRes = await fetch(`/sessions/${sessionId}/quests`);
|
||||
if (listRes.ok) updateQuestPanel(await listRes.json());
|
||||
}
|
||||
|
||||
async function startEditMessage(wrapper, messageId) {
|
||||
@@ -262,7 +538,7 @@ async function regenerateMessage(messageId, wrapper) {
|
||||
const res = await fetch('/chat/regenerate', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ session_id: sessionId, persona_id: currentPersona, message_id: messageId }),
|
||||
body: JSON.stringify({ session_id: sessionId, message_id: messageId }),
|
||||
});
|
||||
if (!res.ok) throw new Error('Ошибка: ' + res.status);
|
||||
removeTyping();
|
||||
@@ -297,14 +573,25 @@ export async function reloadChatFromServer(id) {
|
||||
const messages = await histRes.json();
|
||||
clearMessages();
|
||||
messages.filter(m => m.role !== 'system').forEach(m => {
|
||||
if (m.role === 'narrator') {
|
||||
try {
|
||||
const data = typeof m.content === 'string' ? JSON.parse(m.content) : m.content;
|
||||
if (data?.text) renderNarratorMessage(data);
|
||||
} catch { /* ignore bad narrator payload */ }
|
||||
return;
|
||||
}
|
||||
addMessage(
|
||||
m.role === 'user' ? 'user' : 'assistant',
|
||||
m.content,
|
||||
m.image_prompt,
|
||||
m.image_path ? `/static/${m.image_path}` : null,
|
||||
m.id,
|
||||
m.image_prompt_alt,
|
||||
m.image_path_alt ? `/static/${m.image_path_alt}` : null,
|
||||
m.role === 'user' ? m.action_resolution : null,
|
||||
);
|
||||
});
|
||||
restoreChoicesFromHistory(messages);
|
||||
}
|
||||
|
||||
const IMAGE_PROMPT_RE = /\[IMAGE_PROMPT:.*?\]/gs;
|
||||
@@ -330,6 +617,22 @@ async function consumeStream(res) {
|
||||
// Narrator arrives BEFORE chunks — render immediately
|
||||
if (data.narrator) {
|
||||
renderNarratorMessage(data.narrator);
|
||||
if (
|
||||
_pendingUserBubble
|
||||
&& data.narrator.roll != null
|
||||
&& data.narrator.original_intent
|
||||
&& !isPlayerChoiceContent(_pendingUserBubble.dataset.raw || '')
|
||||
) {
|
||||
const resolution = {
|
||||
intent_text: data.narrator.original_intent,
|
||||
resolution_text: data.narrator.text,
|
||||
roll: data.narrator.roll,
|
||||
outcome: data.narrator.outcome,
|
||||
};
|
||||
_pendingUserBubble._diceResolution = resolution;
|
||||
applyDiceOverrideToBubble(_pendingUserBubble, resolution);
|
||||
_pendingUserBubble.closest('.message')?.classList.add('has-dice-override');
|
||||
}
|
||||
}
|
||||
|
||||
if (data.chunk !== undefined) {
|
||||
@@ -344,8 +647,12 @@ async function consumeStream(res) {
|
||||
if (data.image_generating && bubble) {
|
||||
bubble.classList.remove('typing-active');
|
||||
const wrapper = bubble.parentElement;
|
||||
if (data.image_prompt && !wrapper.querySelector('.image-prompt-block')) {
|
||||
wrapper.appendChild(createImagePromptBlock(data.image_prompt));
|
||||
if (data.image_prompt) {
|
||||
ensureImagePromptBlocks(
|
||||
wrapper,
|
||||
data.image_prompt,
|
||||
data.image_prompt_alt || null,
|
||||
);
|
||||
}
|
||||
showImageGenerating(wrapper);
|
||||
dom.messagesEl.scrollTop = dom.messagesEl.scrollHeight;
|
||||
@@ -356,19 +663,24 @@ async function consumeStream(res) {
|
||||
removeImageGenerating(wrapper);
|
||||
bubble?.classList.remove('typing-active');
|
||||
|
||||
// Strip IMAGE_PROMPT tag from final text
|
||||
// Strip IMAGE_PROMPT tag; apply server-side OOC strip if provided
|
||||
if (bubble) {
|
||||
bubble.textContent = bubble.textContent.replace(IMAGE_PROMPT_RE, '').trim();
|
||||
const fromServer = data.assistant_content;
|
||||
const cleaned = (fromServer ?? bubble.textContent)
|
||||
.replace(IMAGE_PROMPT_RE, '')
|
||||
.trim();
|
||||
initBubbleContent(bubble, cleaned, { formatted: true });
|
||||
}
|
||||
|
||||
if (data.image_prompt && wrapper && !wrapper.querySelector('.image-prompt-block')) {
|
||||
wrapper.appendChild(createImagePromptBlock(data.image_prompt));
|
||||
if (data.image_prompt && wrapper) {
|
||||
ensureImagePromptBlocks(
|
||||
wrapper,
|
||||
data.image_prompt,
|
||||
data.image_prompt_alt || null,
|
||||
);
|
||||
}
|
||||
if (data.image_path && wrapper) {
|
||||
console.log('[image] appending', data.image_path, 'to', wrapper);
|
||||
appendChatImage(wrapper, data.image_path);
|
||||
} else {
|
||||
console.log('[image] skip: image_path=', data.image_path, 'wrapper=', wrapper);
|
||||
appendChatImage(wrapper, data.image_path, '');
|
||||
}
|
||||
if (data.image_error && wrapper) {
|
||||
const err = document.createElement('div');
|
||||
@@ -376,11 +688,25 @@ async function consumeStream(res) {
|
||||
err.textContent = '🖼 ' + data.image_error;
|
||||
wrapper.appendChild(err);
|
||||
}
|
||||
if (data.choices?.length && bubble) renderChoices(bubble.parentElement, data.choices);
|
||||
if (bubble?.parentElement && data.assistant_message_id) {
|
||||
finalizeAssistantMessage(
|
||||
bubble.parentElement,
|
||||
data.assistant_message_id,
|
||||
data.choices,
|
||||
);
|
||||
} else if (data.choices?.length && bubble) {
|
||||
renderChoices(bubble.parentElement, data.choices);
|
||||
}
|
||||
if (data.debug) renderDebugBlocks(bubble?.parentElement || dom.messagesEl, data.debug);
|
||||
if (data.narrator_meta && bubble?.parentElement) {
|
||||
showNarratorActivityHint(bubble.parentElement, data.narrator_meta);
|
||||
}
|
||||
if (data.affinity !== undefined) updateAffinityDisplay(data.affinity);
|
||||
if (data.narrative_stats) updateStatsDisplay(data.narrative_stats);
|
||||
if (data.quests?.length) updateQuestPanel(data.quests);
|
||||
|
||||
_pendingUserBubble = null;
|
||||
|
||||
const { loadSessions } = await import('./sessions.js');
|
||||
loadSessions();
|
||||
}
|
||||
@@ -388,7 +714,16 @@ async function consumeStream(res) {
|
||||
}
|
||||
}
|
||||
|
||||
export function addMessage(role, content = '', imagePrompt = null, imagePath = null, messageId = null) {
|
||||
export function addMessage(
|
||||
role,
|
||||
content = '',
|
||||
imagePrompt = null,
|
||||
imagePath = null,
|
||||
messageId = null,
|
||||
imagePromptAlt = null,
|
||||
imagePathAlt = null,
|
||||
actionResolution = null,
|
||||
) {
|
||||
updateEmptyState();
|
||||
const wrapper = document.createElement('div');
|
||||
wrapper.className = `message ${role}`;
|
||||
@@ -407,8 +742,18 @@ export function addMessage(role, content = '', imagePrompt = null, imagePath = n
|
||||
|
||||
const bubble = document.createElement('div');
|
||||
bubble.className = 'bubble';
|
||||
bubble.textContent = displayContent;
|
||||
initBubbleContent(bubble, displayContent, {
|
||||
formatted: !!displayContent,
|
||||
actionResolution: role === 'user' ? actionResolution : null,
|
||||
});
|
||||
wrapper.appendChild(bubble);
|
||||
if (role === 'user' && actionResolution?.resolution_text) {
|
||||
wrapper.classList.add('has-dice-override');
|
||||
}
|
||||
|
||||
if (displayContent) {
|
||||
attachFormatToggle(wrapper, bubble);
|
||||
}
|
||||
|
||||
if (role === 'assistant') {
|
||||
const translateBtn = document.createElement('button');
|
||||
@@ -418,12 +763,12 @@ export function addMessage(role, content = '', imagePrompt = null, imagePath = n
|
||||
let originalText = null;
|
||||
translateBtn.addEventListener('click', async () => {
|
||||
if (originalText !== null) {
|
||||
bubble.textContent = originalText;
|
||||
initBubbleContent(bubble, originalText, { formatted: true });
|
||||
originalText = null;
|
||||
translateBtn.textContent = '🌐 RU';
|
||||
return;
|
||||
}
|
||||
originalText = bubble.textContent;
|
||||
originalText = bubble.dataset.raw ?? bubble.textContent;
|
||||
translateBtn.disabled = true;
|
||||
translateBtn.textContent = '…';
|
||||
try {
|
||||
@@ -434,7 +779,8 @@ export function addMessage(role, content = '', imagePrompt = null, imagePath = n
|
||||
});
|
||||
if (!res.ok) throw new Error(res.statusText);
|
||||
const data = await res.json();
|
||||
bubble.textContent = data.translated;
|
||||
initBubbleContent(bubble, data.translated, { formatted: true });
|
||||
bubble.dataset.raw = data.translated;
|
||||
translateBtn.textContent = '↩ Оригинал';
|
||||
} catch {
|
||||
originalText = null;
|
||||
@@ -446,8 +792,9 @@ export function addMessage(role, content = '', imagePrompt = null, imagePath = n
|
||||
wrapper.appendChild(translateBtn);
|
||||
}
|
||||
|
||||
if (prompt) wrapper.appendChild(createImagePromptBlock(prompt));
|
||||
if (imagePath) appendChatImage(wrapper, imagePath);
|
||||
if (prompt) wrapper.appendChild(createImagePromptBlock(prompt, imagePromptAlt));
|
||||
if (imagePath) appendChatImage(wrapper, imagePath, imagePathAlt ? 'Теги' : '');
|
||||
if (imagePathAlt) appendChatImage(wrapper, imagePathAlt, 'Гибрид');
|
||||
attachMessageActions(wrapper, messageId, role);
|
||||
dom.messagesEl.appendChild(wrapper);
|
||||
dom.messagesEl.scrollTop = dom.messagesEl.scrollHeight;
|
||||
@@ -481,13 +828,15 @@ export async function sendMessage(text, isNarratorChoice = false) {
|
||||
dom.inputEl.value = '';
|
||||
dom.inputEl.style.height = 'auto';
|
||||
dom.sendBtn.disabled = true;
|
||||
addMessage('user', isNarratorChoice ? `[${text}]` : text);
|
||||
const userContent = isNarratorChoice ? `[Player chose: ${text}]` : text;
|
||||
dom.messagesEl.querySelectorAll('.message.assistant').forEach(w => removeChoiceRows(w));
|
||||
_pendingUserBubble = addMessage('user', userContent);
|
||||
showTyping();
|
||||
try {
|
||||
const res = await fetch('/chat/stream', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ message: text, session_id: sessionId, persona_id: currentPersona, is_narrator_choice: isNarratorChoice }),
|
||||
body: JSON.stringify({ message: text, session_id: sessionId, is_narrator_choice: isNarratorChoice }),
|
||||
});
|
||||
if (!res.ok) throw new Error('Ошибка сервера: ' + res.status);
|
||||
removeTyping();
|
||||
|
||||
Reference in New Issue
Block a user