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
@@ -12,3 +12,8 @@ async function apiHistory(sessionId = 'null', limit = 50) {
|
||||
const resp = await fetch(`/api/history?sessionId=${sessionId}&limit=${limit}`);
|
||||
return resp.json();
|
||||
}
|
||||
|
||||
async function apiHistoryPoll(afterId) {
|
||||
const resp = await fetch(`/api/history/poll?afterId=${afterId}`);
|
||||
return resp.json();
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
// ── Chat State ──
|
||||
|
||||
const messages = [];
|
||||
let isStreaming = false;
|
||||
|
||||
function clearChat() {
|
||||
messages.length = 0;
|
||||
@@ -24,6 +25,7 @@ async function sendMessage() {
|
||||
input.value = '';
|
||||
input.style.height = 'auto';
|
||||
btn.disabled = true;
|
||||
isStreaming = true;
|
||||
setMetaHtml('<span style="color:#7c83ff">⏳ Streaming...</span>');
|
||||
|
||||
let fullText = '';
|
||||
@@ -103,6 +105,7 @@ async function sendMessage() {
|
||||
addErrorBubble('Netzwerk-Fehler: ' + e.message);
|
||||
} finally {
|
||||
btn.disabled = false;
|
||||
isStreaming = false;
|
||||
endStreamBubble();
|
||||
}
|
||||
}
|
||||
+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