fixed, aria reuse old session, and reload chat
This commit is contained in:
@@ -224,6 +224,11 @@ async function connectGateway() {
|
||||
gatewayWs = ws;
|
||||
broadcastState();
|
||||
|
||||
// Nach (Re-)Connect: letzte aktive Session aus OpenClaw wiederherstellen
|
||||
resolveActiveSession().catch(err => {
|
||||
log("warn", "server", `Session-Aufloesung fehlgeschlagen: ${err.message}`);
|
||||
});
|
||||
|
||||
// Nachrichten-Loop — RAW logging fuer Debugging
|
||||
ws.on("message", (raw) => {
|
||||
try {
|
||||
@@ -971,6 +976,8 @@ wss.on("connection", (ws) => {
|
||||
if (ws._sshSock) { ws._sshSock.end(); ws._sshSock = null; }
|
||||
} else if (msg.action === "check_desktop") {
|
||||
checkDesktopAvailable(ws);
|
||||
} else if (msg.action === "load_chat_history") {
|
||||
handleLoadChatHistory(ws);
|
||||
} else if (msg.action === "list_sessions") {
|
||||
handleListSessions(ws);
|
||||
} else if (msg.action === "read_session") {
|
||||
@@ -1279,6 +1286,107 @@ async function handleDeleteSession(clientWs, sessionPath) {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Session-Aufloesung: letzte aktive Session finden ────
|
||||
async function resolveActiveSession() {
|
||||
const indexRaw = await dockerExec("aria-core", `cat ${SESSIONS_DIR}/sessions.json 2>/dev/null || echo '{}'`);
|
||||
let sessionsIndex = {};
|
||||
try { sessionsIndex = JSON.parse(indexRaw.trim()); } catch { return; }
|
||||
|
||||
const entries = Array.isArray(sessionsIndex) ? sessionsIndex
|
||||
: Array.isArray(sessionsIndex.sessions) ? sessionsIndex.sessions
|
||||
: Object.entries(sessionsIndex).map(([k, v]) => ({ key: k, ...(typeof v === "object" ? v : { id: v }) }));
|
||||
|
||||
if (entries.length === 0) return;
|
||||
|
||||
// Pruefen ob aktuelle Session noch existiert
|
||||
const currentExists = entries.some(e => (e.key || e.sessionKey || e.name || "") === activeSessionKey);
|
||||
if (currentExists) {
|
||||
log("info", "server", `Aktive Session '${activeSessionKey}' existiert noch`);
|
||||
return;
|
||||
}
|
||||
|
||||
// Neueste Session nehmen (nach updatedAt sortieren)
|
||||
let newest = null;
|
||||
let newestTime = 0;
|
||||
for (const entry of entries) {
|
||||
const t = entry.updatedAt || entry.createdAt || 0;
|
||||
if (t >= newestTime) {
|
||||
newestTime = t;
|
||||
newest = entry;
|
||||
}
|
||||
}
|
||||
|
||||
if (newest) {
|
||||
const key = newest.key || newest.sessionKey || newest.name || "";
|
||||
if (key) {
|
||||
activeSessionKey = key;
|
||||
log("info", "server", `Aktive Session auf neueste gewechselt: '${activeSessionKey}'`);
|
||||
for (const c of browserClients) {
|
||||
c.send(JSON.stringify({ type: "active_session", sessionKey: activeSessionKey }));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ── Chat-History aus aktiver Session laden (Display-Only) ──
|
||||
async function handleLoadChatHistory(clientWs) {
|
||||
try {
|
||||
// Session-ID fuer den aktiven sessionKey finden
|
||||
const indexRaw = await dockerExec("aria-core", `cat ${SESSIONS_DIR}/sessions.json 2>/dev/null || echo '{}'`);
|
||||
let sessionsIndex = {};
|
||||
try { sessionsIndex = JSON.parse(indexRaw.trim()); } catch {}
|
||||
|
||||
const entries = Array.isArray(sessionsIndex) ? sessionsIndex
|
||||
: Array.isArray(sessionsIndex.sessions) ? sessionsIndex.sessions
|
||||
: Object.entries(sessionsIndex).map(([k, v]) => ({ key: k, ...(typeof v === "object" ? v : { id: v }) }));
|
||||
|
||||
let sessionId = null;
|
||||
for (const entry of entries) {
|
||||
const key = entry.key || entry.sessionKey || entry.name || "";
|
||||
if (key === activeSessionKey) {
|
||||
sessionId = entry.id || entry.sessionId || "";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!sessionId) {
|
||||
clientWs.send(JSON.stringify({ type: "chat_history", messages: [] }));
|
||||
return;
|
||||
}
|
||||
|
||||
const sessionFile = `${SESSIONS_DIR}/${sessionId}.jsonl`;
|
||||
const raw = await dockerExec("aria-core", `cat '${sessionFile}' 2>/dev/null || echo ''`);
|
||||
const chatMessages = [];
|
||||
|
||||
for (const line of raw.split("\n")) {
|
||||
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 });
|
||||
}
|
||||
} catch {}
|
||||
}
|
||||
|
||||
clientWs.send(JSON.stringify({ type: "chat_history", messages: chatMessages }));
|
||||
log("info", "server", `Chat-History geladen: ${chatMessages.length} Nachrichten`);
|
||||
} catch (err) {
|
||||
log("error", "server", `Chat-History laden fehlgeschlagen: ${err.message}`);
|
||||
clientWs.send(JSON.stringify({ type: "chat_history", messages: [], error: err.message }));
|
||||
}
|
||||
}
|
||||
|
||||
// ── Session aktivieren ─────────────────────────────────
|
||||
function handleSetActiveSession(clientWs, sessionKey) {
|
||||
if (!sessionKey || typeof sessionKey !== "string") {
|
||||
|
||||
Reference in New Issue
Block a user