mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-03 12:39:23 +08:00
* feat(sessions): add recycle bin for deleted chats
Implements soft-delete for chat sessions so users can restore
accidentally deleted conversations from a recycle bin.
Backend:
- Schema migration adds deleted_at REAL column to sessions
- delete_session becomes a soft-delete (sets deleted_at timestamp)
- New store methods: restore_session, purge_session, list_deleted_sessions
- Session list queries exclude soft-deleted sessions
- API endpoints: GET /sessions/recycle-bin, POST /{id}/restore,
DELETE /{id}/purge (purge triggers attachment/mastery cleanup)
Frontend:
- RecycleBinSection: collapsible sidebar section listing deleted
sessions with Restore and Delete permanently actions
- Wired into SidebarShell via recycleBinSlot prop
- API client functions for list, restore, and purge
- en/zh translations for all recycle-bin UI strings
Tests: 6 backend tests covering the soft-delete lifecycle
* style: apply ruff format to sqlite_store
* fix(sessions): preserve recycled chat artifacts
* feat(sessions): fold #1444's backend into this recycle bin
#1444 built the same feature from the other end. This PR had the SQLite
column, the routes and the sidebar UI; #1444 had the protocol declarations,
the PocketBase implementation and the CLI wording. Neither shipped alone —
this one left multi-user deployments answering AttributeError on
/recycle-bin, and #1444 built a bin nobody could see. Closes #1444; its
backend is here.
Kept from here: one `deleted_at` column. #1444 carried `is_deleted` beside it
in both backends, and `deleted_at IS NOT NULL` already says everything the
boolean did — PocketBase now derives the outward `is_deleted` field from the
timestamp instead of storing it twice.
Taken from #1444: the naming, which turns out to fix the residual both PRs
shipped with. `delete_session` keeps meaning what every existing caller
already meant by it — remove the row — so `reading.py`'s two internal
cleanups need no patch: a deleted reading workspace still takes its sessions
with it instead of depositing them in a learner's bin, where restoring one
would resurrect a session pointing at a workspace that no longer exists. The
chat surface's delete is a separate, explicitly named `soft_delete_session`,
and `hard_delete_session` empties the bin. Both backends agree, and the
protocol declares all five so a backend cannot silently lack one again.
`test_entries_cascade_on_session_purge` becomes two tests, because there are
now two deletions worth pinning: one the bin can undo, and one that skips it
and cascades immediately.
Co-Authored-By: sjagadee <srini.jagadeesh@gmail.com>
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
---------
Co-authored-by: XZH <cto@xzh.ai>
Co-authored-by: Bingxi Zhao (Frank) <13051345080@163.com>
Co-authored-by: sjagadee <srini.jagadeesh@gmail.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
248 lines
7.4 KiB
Python
248 lines
7.4 KiB
Python
"""
|
|
Structural protocol for session stores.
|
|
|
|
Both SQLiteSessionStore and PocketBaseSessionStore satisfy this protocol,
|
|
allowing the rest of the codebase to be store-agnostic.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any, Protocol, runtime_checkable
|
|
|
|
|
|
class ActiveTurnConflict(RuntimeError):
|
|
"""A session already owns a turn that has not reached a terminal state.
|
|
|
|
Named rather than a bare ``RuntimeError`` so a caller can tell "this
|
|
session is busy" from any other runtime failure without matching on the
|
|
message. Subclasses ``RuntimeError`` because every existing handler
|
|
catches that, and the message is unchanged for the same reason.
|
|
|
|
``turn_id`` is the row that blocks the session, when the store knows it;
|
|
the unique-index race reports the session instead, having lost the race
|
|
to read it.
|
|
"""
|
|
|
|
def __init__(self, message: str, *, turn_id: str = "") -> None:
|
|
super().__init__(message)
|
|
self.turn_id = turn_id
|
|
|
|
|
|
@runtime_checkable
|
|
class SessionRepository(Protocol):
|
|
async def migrate_workspace_preferences(self) -> int: ...
|
|
|
|
async def create_session(
|
|
self,
|
|
title: str | None = None,
|
|
session_id: str | None = None,
|
|
) -> dict[str, Any]: ...
|
|
|
|
async def get_session(self, session_id: str) -> dict[str, Any] | None: ...
|
|
|
|
async def ensure_session(self, session_id: str | None = None) -> dict[str, Any]: ...
|
|
|
|
async def import_legacy_session(
|
|
self,
|
|
session_id: str,
|
|
title: str,
|
|
created_at: float,
|
|
updated_at: float,
|
|
preferences: dict[str, Any],
|
|
messages: list[dict[str, Any]],
|
|
) -> dict[str, Any]: ...
|
|
|
|
async def list_sessions(self, limit: int = 50, offset: int = 0) -> list[dict[str, Any]]: ...
|
|
|
|
async def search_sessions(
|
|
self, query: str, limit: int = 50, offset: int = 0
|
|
) -> dict[str, Any]: ...
|
|
|
|
async def update_session_title(self, session_id: str, title: str) -> bool: ...
|
|
|
|
async def soft_delete_session(self, session_id: str) -> bool: ...
|
|
|
|
async def restore_session(self, session_id: str) -> bool: ...
|
|
|
|
async def hard_delete_session(self, session_id: str) -> bool: ...
|
|
|
|
async def list_deleted_sessions(
|
|
self, limit: int = 50, offset: int = 0
|
|
) -> list[dict[str, Any]]: ...
|
|
|
|
async def delete_session(self, session_id: str) -> bool: ...
|
|
|
|
|
|
@runtime_checkable
|
|
class TurnRepository(Protocol):
|
|
async def begin_turn(
|
|
self,
|
|
session_id: str,
|
|
capability: str = "",
|
|
*,
|
|
turn_id: str | None = None,
|
|
owner_id: str = "",
|
|
fencing_token: int = 0,
|
|
) -> dict[str, Any]: ...
|
|
|
|
async def get_turn(self, turn_id: str) -> dict[str, Any] | None: ...
|
|
|
|
async def get_active_turn(self, session_id: str) -> dict[str, Any] | None: ...
|
|
|
|
async def list_active_turns(self, session_id: str) -> list[dict[str, Any]]: ...
|
|
|
|
async def list_nonterminal_turns(self) -> list[dict[str, Any]]: ...
|
|
|
|
async def transition_turn(
|
|
self,
|
|
turn_id: str,
|
|
status: str,
|
|
*,
|
|
expected_status: str | None = None,
|
|
fencing_token: int | None = None,
|
|
error: str = "",
|
|
failure_code: str = "",
|
|
retryable: bool = False,
|
|
) -> bool: ...
|
|
|
|
async def append_events(
|
|
self,
|
|
turn_id: str,
|
|
events: list[dict[str, Any]],
|
|
*,
|
|
fencing_token: int | None = None,
|
|
) -> list[dict[str, Any]]: ...
|
|
|
|
async def get_events(self, turn_id: str, after_seq: int = 0) -> list[dict[str, Any]]: ...
|
|
|
|
|
|
@runtime_checkable
|
|
class MessageRepository(Protocol):
|
|
async def add_message(
|
|
self,
|
|
session_id: str,
|
|
role: str,
|
|
content: str,
|
|
capability: str = "",
|
|
events: list[dict[str, Any]] | None = None,
|
|
attachments: list[dict[str, Any]] | None = None,
|
|
metadata: dict[str, Any] | None = None,
|
|
parent_message_id: int | str | None = None,
|
|
) -> int | str: ...
|
|
|
|
async def get_messages(self, session_id: str) -> list[dict[str, Any]]: ...
|
|
|
|
|
|
@runtime_checkable
|
|
class SessionStoreProtocol(SessionRepository, TurnRepository, MessageRepository, Protocol):
|
|
async def migrate_workspace_preferences(self) -> int: ...
|
|
|
|
async def create_session(
|
|
self,
|
|
title: str | None = None,
|
|
session_id: str | None = None,
|
|
) -> dict[str, Any]: ...
|
|
|
|
async def get_session(self, session_id: str) -> dict[str, Any] | None: ...
|
|
|
|
async def ensure_session(
|
|
self,
|
|
session_id: str | None = None,
|
|
) -> dict[str, Any]: ...
|
|
|
|
async def create_turn(self, session_id: str, capability: str = "") -> dict[str, Any]: ...
|
|
|
|
async def get_turn(self, turn_id: str) -> dict[str, Any] | None: ...
|
|
|
|
async def get_active_turn(self, session_id: str) -> dict[str, Any] | None: ...
|
|
|
|
async def list_active_turns(self, session_id: str) -> list[dict[str, Any]]: ...
|
|
|
|
async def update_turn_status(self, turn_id: str, status: str, error: str = "") -> bool: ...
|
|
|
|
async def append_turn_event(self, turn_id: str, event: dict[str, Any]) -> dict[str, Any]: ...
|
|
|
|
async def get_turn_events(self, turn_id: str, after_seq: int = 0) -> list[dict[str, Any]]: ...
|
|
|
|
async def link_turn_message(self, turn_id: str, assistant_message_id: int | str) -> bool: ...
|
|
|
|
async def get_message_trace(
|
|
self,
|
|
session_id: str,
|
|
message_id: int | str,
|
|
after_seq: int = 0,
|
|
limit: int | None = None,
|
|
) -> dict[str, Any] | None: ...
|
|
|
|
async def update_session_title(self, session_id: str, title: str) -> bool: ...
|
|
|
|
async def soft_delete_session(self, session_id: str) -> bool: ...
|
|
|
|
async def restore_session(self, session_id: str) -> bool: ...
|
|
|
|
async def hard_delete_session(self, session_id: str) -> bool: ...
|
|
|
|
async def list_deleted_sessions(
|
|
self, limit: int = 50, offset: int = 0
|
|
) -> list[dict[str, Any]]: ...
|
|
|
|
async def delete_session(self, session_id: str) -> bool: ...
|
|
|
|
async def add_message(
|
|
self,
|
|
session_id: str,
|
|
role: str,
|
|
content: str,
|
|
capability: str = "",
|
|
events: list[dict[str, Any]] | None = None,
|
|
attachments: list[dict[str, Any]] | None = None,
|
|
metadata: dict[str, Any] | None = None,
|
|
parent_message_id: int | str | None = None,
|
|
) -> int | str: ...
|
|
|
|
async def delete_message(self, message_id: int | str) -> bool: ...
|
|
|
|
async def get_last_message(
|
|
self, session_id: str, role: str | None = None
|
|
) -> dict[str, Any] | None: ...
|
|
|
|
async def get_messages(self, session_id: str) -> list[dict[str, Any]]: ...
|
|
|
|
async def get_messages_for_context(
|
|
self, session_id: str, leaf_message_id: int | None = None
|
|
) -> list[dict[str, Any]]: ...
|
|
|
|
async def list_sessions(
|
|
self,
|
|
limit: int = 50,
|
|
offset: int = 0,
|
|
) -> list[dict[str, Any]]: ...
|
|
|
|
async def search_sessions(
|
|
self,
|
|
query: str,
|
|
limit: int = 50,
|
|
offset: int = 0,
|
|
) -> dict[str, Any]: ...
|
|
|
|
async def get_session_summaries(
|
|
self,
|
|
session_ids: list[str],
|
|
) -> list[dict[str, Any]]: ...
|
|
|
|
async def update_summary(self, session_id: str, summary: str, up_to_msg_id: int) -> bool: ...
|
|
|
|
async def update_session_preferences(
|
|
self, session_id: str, preferences: dict[str, Any]
|
|
) -> bool: ...
|
|
|
|
async def get_session_with_messages(self, session_id: str) -> dict[str, Any] | None: ...
|
|
|
|
|
|
__all__ = [
|
|
"MessageRepository",
|
|
"SessionRepository",
|
|
"SessionStoreProtocol",
|
|
"TurnRepository",
|
|
]
|