79 lines
1.9 KiB
JavaScript
79 lines
1.9 KiB
JavaScript
// ── History Loading ──
|
|
|
|
let lastKnownMessageId = 0;
|
|
let historyPollInterval = null;
|
|
|
|
async function loadHistory() {
|
|
try {
|
|
const data = await apiHistory('null');
|
|
data.reverse(); // DESC → chronologisch
|
|
|
|
for (const entry of data) {
|
|
renderMessages(entry.messages, true);
|
|
}
|
|
|
|
scrollDown();
|
|
startHistoryPolling();
|
|
} catch (e) {
|
|
console.error('History laden fehlgeschlagen:', e);
|
|
}
|
|
}
|
|
|
|
function renderMessages(msgs, addToConversation) {
|
|
for (const msg of msgs) {
|
|
if (msg.id > lastKnownMessageId) lastKnownMessageId = msg.id;
|
|
|
|
switch (msg.type) {
|
|
case 'text':
|
|
if (msg.role === 'user') {
|
|
addUserBubble(msg.content);
|
|
if (addToConversation) messages.push({ role: 'user', content: msg.content });
|
|
} else if (msg.role === 'assistant') {
|
|
addAssistantBubble(msg.content);
|
|
if (addToConversation) messages.push({ role: 'assistant', content: msg.content });
|
|
}
|
|
break;
|
|
|
|
case 'tool_use':
|
|
addToolBoxStatic(msg.toolName, msg.content, null, false);
|
|
break;
|
|
|
|
case 'tool_result':
|
|
addToolBoxStatic(null, null, msg.content, msg.isError);
|
|
break;
|
|
|
|
case 'error':
|
|
addErrorBubble(msg.content);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
// ── Polling ──
|
|
|
|
function startHistoryPolling(intervalMs = 3000) {
|
|
if (historyPollInterval) return;
|
|
historyPollInterval = setInterval(pollHistory, intervalMs);
|
|
}
|
|
|
|
function stopHistoryPolling() {
|
|
if (historyPollInterval) {
|
|
clearInterval(historyPollInterval);
|
|
historyPollInterval = null;
|
|
}
|
|
}
|
|
|
|
async function pollHistory() {
|
|
if (isStreaming) return;
|
|
|
|
try {
|
|
const newMessages = await apiHistoryPoll(lastKnownMessageId);
|
|
if (newMessages.length > 0) {
|
|
renderMessages(newMessages, true);
|
|
scrollDown();
|
|
}
|
|
} catch (e) {
|
|
console.error('History poll fehlgeschlagen:', e);
|
|
}
|
|
}
|