64 lines
2.0 KiB
Python
64 lines
2.0 KiB
Python
"""ConversationRepository - an object-shaped, atomic-write-backed facade over
|
|
``core/history.py`` (R06-T02).
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
from typing import Any, Dict, List, Optional
|
|
|
|
from cowork_local.core.history import (
|
|
delete_conversation,
|
|
list_conversations,
|
|
load_conversation,
|
|
new_session_id,
|
|
rename_conversation,
|
|
save_conversation,
|
|
set_pinned,
|
|
)
|
|
|
|
|
|
class ConversationRepository:
|
|
"""CRUD + search over conversation JSON files, scoped to one
|
|
``directory`` (defaults to the app's real ``HISTORY_DIR``)."""
|
|
|
|
def __init__(self, directory: Optional[Path] = None) -> None:
|
|
if directory is not None:
|
|
self._directory = Path(directory)
|
|
else:
|
|
from cowork_local.config import HISTORY_DIR
|
|
self._directory = HISTORY_DIR
|
|
|
|
def new_session_id(self) -> str:
|
|
return new_session_id()
|
|
|
|
def save(self, kind: str, session_id: str, messages: List[Dict[str, Any]], **kwargs) -> Path:
|
|
return save_conversation(self._directory, kind, session_id, messages, **kwargs)
|
|
|
|
def load(self, path: Path) -> Dict[str, Any]:
|
|
return load_conversation(path)
|
|
|
|
def list(self, query: str = "", **kwargs) -> List[Dict[str, Any]]:
|
|
return list_conversations(self._directory, query=query)
|
|
|
|
def _resolve_path(self, target: Any) -> Path:
|
|
if isinstance(target, Path):
|
|
return target
|
|
p = Path(str(target))
|
|
if p.exists() or p.is_absolute():
|
|
return p
|
|
for file in self._directory.glob(f"*__{target}.json"):
|
|
return file
|
|
return self._directory / f"cowork__{target}.json"
|
|
|
|
def rename(self, target: Any, new_title: str) -> None:
|
|
rename_conversation(self._resolve_path(target), new_title)
|
|
|
|
def delete(self, target: Any) -> None:
|
|
delete_conversation(self._resolve_path(target))
|
|
|
|
def set_pinned(self, target: Any, pinned: bool) -> None:
|
|
set_pinned(self._resolve_path(target), pinned)
|
|
|
|
|
|
__all__ = ["ConversationRepository"]
|