fixed, chat messages, reload

This commit is contained in:
duffyduck 2026-03-29 03:04:45 +02:00
parent 4722e1a0ee
commit f2aebcbad9
1 changed files with 20 additions and 13 deletions

View File

@ -1362,19 +1362,26 @@ async function handleLoadChatHistory(clientWs) {
if (!line.trim()) continue;
try {
const obj = JSON.parse(line);
// User-Nachrichten
if (obj.role === "user" && obj.content) {
const text = typeof obj.content === "string" ? obj.content
: Array.isArray(obj.content) ? obj.content.filter(c => c.type === "text").map(c => c.text).join("\n")
: "";
if (text) chatMessages.push({ type: "sent", text, meta: "Gateway direkt", ts: obj.timestamp || 0 });
}
// Assistant-Nachrichten
if (obj.role === "assistant" && obj.content) {
const text = typeof obj.content === "string" ? obj.content
: Array.isArray(obj.content) ? obj.content.filter(c => c.type === "text").map(c => c.text).join("\n")
: "";
if (text) chatMessages.push({ type: "received", text, meta: "chat:final", ts: obj.timestamp || 0 });
// OpenClaw Format: {"type":"message","message":{"role":"user|assistant","content":[...]}}
if (obj.type !== "message" || !obj.message) continue;
const msg = obj.message;
const role = msg.role;
if (!role) continue;
// Text aus content-Array extrahieren
let text = "";
if (typeof msg.content === "string") text = msg.content;
else if (Array.isArray(msg.content)) text = msg.content.filter(c => c.type === "text").map(c => c.text || "").join("\n");
if (!text) continue;
if (role === "user") {
// Metadata-Prefix entfernen: "Sender (untrusted metadata):\n```json\n{...}\n```\n\n[timestamp] "
text = text.replace(/^Sender \(untrusted metadata\):[\s\S]*?```\s*\n*(?:\[.*?\]\s*)?/m, "").trim();
chatMessages.push({ type: "sent", text, meta: "Gateway direkt", ts: msg.timestamp || obj.timestamp || 0 });
} else if (role === "assistant") {
// Reply-Prefix entfernen: "[[reply_to_current]] "
text = text.replace(/^\[\[reply_to_\w+\]\]\s*/g, "").trim();
if (text) chatMessages.push({ type: "received", text, meta: "chat:final", ts: msg.timestamp || obj.timestamp || 0 });
}
} catch {}
}