History Polling: Message-ID-basiert, neuer /api/history/poll Endpoint
This commit is contained in:
1 parent
19f926da05
commit
fb2c3a0720
5 files changed
+90
-25
No files matched your search
+63
-25
@@ -1,40 +1,78 @@
|
||||
// ── 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) {
|
||||
for (const msg of entry.messages) {
|
||||
switch (msg.type) {
|
||||
case 'text':
|
||||
if (msg.role === 'user') {
|
||||
addUserBubble(msg.content);
|
||||
messages.push({ role: 'user', content: msg.content });
|
||||
} else if (msg.role === 'assistant') {
|
||||
addAssistantBubble(msg.content);
|
||||
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;
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user