refine: migrate legacy sessions on load and simplify get_history

This commit is contained in:
Re-bin 2026-02-18 05:09:57 +00:00
parent 5c61f30546
commit 27a131830f

View File

@ -42,30 +42,15 @@ class Session:
self.updated_at = datetime.now() self.updated_at = datetime.now()
def get_history(self, max_messages: int = 500) -> list[dict[str, Any]]: def get_history(self, max_messages: int = 500) -> list[dict[str, Any]]:
""" """Get recent messages in LLM format, preserving tool metadata."""
Get recent messages in LLM format. out: list[dict[str, Any]] = []
for m in self.messages[-max_messages:]:
Preserves tool metadata for replay/debugging fidelity. entry: dict[str, Any] = {"role": m["role"], "content": m.get("content", "")}
""" for k in ("tool_calls", "tool_call_id", "name"):
history: list[dict[str, Any]] = [] if k in m:
for msg in self.messages[-max_messages:]: entry[k] = m[k]
llm_msg: dict[str, Any] = { out.append(entry)
"role": msg["role"], return out
"content": msg.get("content", ""),
}
if msg["role"] == "assistant" and "tool_calls" in msg:
llm_msg["tool_calls"] = msg["tool_calls"]
if msg["role"] == "tool":
if "tool_call_id" in msg:
llm_msg["tool_call_id"] = msg["tool_call_id"]
if "name" in msg:
llm_msg["name"] = msg["name"]
history.append(llm_msg)
return history
def clear(self) -> None: def clear(self) -> None:
"""Clear all messages and reset session to initial state.""" """Clear all messages and reset session to initial state."""
@ -93,7 +78,7 @@ class SessionManager:
return self.sessions_dir / f"{safe_key}.jsonl" return self.sessions_dir / f"{safe_key}.jsonl"
def _get_legacy_session_path(self, key: str) -> Path: def _get_legacy_session_path(self, key: str) -> Path:
"""Get the legacy global session path for backward compatibility.""" """Legacy global session path (~/.nanobot/sessions/)."""
safe_key = safe_filename(key.replace(":", "_")) safe_key = safe_filename(key.replace(":", "_"))
return self.legacy_sessions_dir / f"{safe_key}.jsonl" return self.legacy_sessions_dir / f"{safe_key}.jsonl"
@ -123,7 +108,9 @@ class SessionManager:
if not path.exists(): if not path.exists():
legacy_path = self._get_legacy_session_path(key) legacy_path = self._get_legacy_session_path(key)
if legacy_path.exists(): if legacy_path.exists():
path = legacy_path import shutil
shutil.move(str(legacy_path), str(path))
logger.info(f"Migrated session {key} from legacy path")
if not path.exists(): if not path.exists():
return None return None