added logging for session ids

This commit is contained in:
duffyduck 2026-03-29 03:33:09 +02:00
parent 54b4331e1e
commit 65ae75494f
1 changed files with 28 additions and 8 deletions

View File

@ -36,8 +36,15 @@ const state = {
proxy: { status: "unknown", lastError: null },
};
const SESSION_KEY_FILE = "/data/active-session";
// /data Verzeichnis sicherstellen (Volume Mount)
try { fs.mkdirSync("/data", { recursive: true }); } catch {}
let activeSessionKey = (() => {
try { return fs.readFileSync(SESSION_KEY_FILE, "utf-8").trim(); } catch { return "main"; }
try {
const saved = fs.readFileSync(SESSION_KEY_FILE, "utf-8").trim();
if (saved) { console.log(`[startup] Gespeicherte Session geladen: '${saved}'`); return saved; }
} catch {}
console.log("[startup] Keine gespeicherte Session — Fallback 'main'");
return "main";
})();
const logs = [];
let gatewayWs = null;
@ -1294,7 +1301,17 @@ async function handleDeleteSession(clientWs, sessionPath) {
// ── Session-Aufloesung: letzte aktive Session finden ────
async function resolveActiveSession() {
// Nur bei Fallback-Key "main" automatisch aufloesen — gespeicherte Wahl respektieren
const hasSavedSession = (() => {
try { return !!fs.readFileSync(SESSION_KEY_FILE, "utf-8").trim(); } catch { return false; }
})();
if (hasSavedSession && activeSessionKey !== "main") {
log("info", "server", `Gespeicherte Session '${activeSessionKey}' wird beibehalten`);
return;
}
const indexRaw = await dockerExec("aria-core", `cat ${SESSIONS_DIR}/sessions.json 2>/dev/null || echo '{}'`);
log("debug", "server", `sessions.json: ${indexRaw.slice(0, 500)}`);
let sessionsIndex = {};
try { sessionsIndex = JSON.parse(indexRaw.trim()); } catch { return; }
@ -1304,14 +1321,11 @@ async function resolveActiveSession() {
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;
}
// Vorhandene Keys loggen
const keys = entries.map(e => e.key || e.sessionKey || e.name || "?");
log("info", "server", `Verfuegbare Sessions: [${keys.join(", ")}]`);
// Neueste Session nehmen (nach updatedAt sortieren)
// Neueste Session nehmen
let newest = null;
let newestTime = 0;
for (const entry of entries) {
@ -1326,6 +1340,7 @@ async function resolveActiveSession() {
const key = newest.key || newest.sessionKey || newest.name || "";
if (key) {
activeSessionKey = key;
try { fs.writeFileSync(SESSION_KEY_FILE, activeSessionKey); } catch {}
log("info", "server", `Aktive Session auf neueste gewechselt: '${activeSessionKey}'`);
for (const c of browserClients) {
c.send(JSON.stringify({ type: "active_session", sessionKey: activeSessionKey }));
@ -1347,8 +1362,10 @@ async function handleLoadChatHistory(clientWs) {
: Object.entries(sessionsIndex).map(([k, v]) => ({ key: k, ...(typeof v === "object" ? v : { id: v }) }));
let sessionId = null;
const availableKeys = [];
for (const entry of entries) {
const key = entry.key || entry.sessionKey || entry.name || "";
availableKeys.push(key);
if (key === activeSessionKey) {
sessionId = entry.id || entry.sessionId || "";
break;
@ -1356,10 +1373,13 @@ async function handleLoadChatHistory(clientWs) {
}
if (!sessionId) {
log("warn", "server", `Chat-History: Session '${activeSessionKey}' nicht in sessions.json gefunden. Verfuegbar: [${availableKeys.join(", ")}]`);
clientWs.send(JSON.stringify({ type: "chat_history", messages: [] }));
return;
}
log("info", "server", `Chat-History: Session '${activeSessionKey}' -> ID '${sessionId}'`);
const sessionFile = `${SESSIONS_DIR}/${sessionId}.jsonl`;
const raw = await dockerExec("aria-core", `cat '${sessionFile}' 2>/dev/null || echo ''`);
const chatMessages = [];