mirror of
https://github.com/lWolvesl/claw-code.git
synced 2026-04-02 21:21:53 +08:00
36 lines
1022 B
Python
36 lines
1022 B
Python
from __future__ import annotations
|
|
|
|
import json
|
|
from dataclasses import asdict, dataclass
|
|
from pathlib import Path
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class StoredSession:
|
|
session_id: str
|
|
messages: tuple[str, ...]
|
|
input_tokens: int
|
|
output_tokens: int
|
|
|
|
|
|
DEFAULT_SESSION_DIR = Path('.port_sessions')
|
|
|
|
|
|
def save_session(session: StoredSession, directory: Path | None = None) -> Path:
|
|
target_dir = directory or DEFAULT_SESSION_DIR
|
|
target_dir.mkdir(parents=True, exist_ok=True)
|
|
path = target_dir / f'{session.session_id}.json'
|
|
path.write_text(json.dumps(asdict(session), indent=2))
|
|
return path
|
|
|
|
|
|
def load_session(session_id: str, directory: Path | None = None) -> StoredSession:
|
|
target_dir = directory or DEFAULT_SESSION_DIR
|
|
data = json.loads((target_dir / f'{session_id}.json').read_text())
|
|
return StoredSession(
|
|
session_id=data['session_id'],
|
|
messages=tuple(data['messages']),
|
|
input_tokens=data['input_tokens'],
|
|
output_tokens=data['output_tokens'],
|
|
)
|