fix: session persistence, multi-turn memory, OCR pipeline, download UX (v7)
- graph.stream() state fix: agent_state now properly accumulates node updates - atomic session save (tempfile + os.replace) - uploaded_file_path injection for OcrExtractor + annotation_detector - download section always visible; refreshFromApi auto-reloads after generation - node_start/complete unfiltered for full progress visibility - modification_request without status=='pass' check
This commit is contained in:
+15
-3
@@ -6,6 +6,7 @@
|
||||
import json
|
||||
import os
|
||||
import uuid
|
||||
import tempfile
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
@@ -64,7 +65,7 @@ def load_session(session_id: str) -> Optional[dict]:
|
||||
|
||||
|
||||
def save_session(session_id: str, agent_state: dict, session_name: str = ""):
|
||||
"""将会话状态保存(更新)至磁盘。"""
|
||||
"""将会话状态原子保存至磁盘(temp file + rename,避免崩溃时截断)。"""
|
||||
_ensure_dir()
|
||||
fp = _session_path(session_id)
|
||||
data = {}
|
||||
@@ -82,8 +83,19 @@ def save_session(session_id: str, agent_state: dict, session_name: str = ""):
|
||||
data["created_at"] = data["updated_at"]
|
||||
data["agent_state"] = agent_state
|
||||
|
||||
with open(fp, "w", encoding="utf-8") as f:
|
||||
json.dump(data, f, ensure_ascii=False, indent=2)
|
||||
# 原子写入:先写临时文件,再 replace,避免崩溃时截断 JSON
|
||||
tmp = tempfile.NamedTemporaryFile(
|
||||
mode="w", suffix=".json", delete=False,
|
||||
dir=SESSIONS_DIR, encoding="utf-8",
|
||||
)
|
||||
try:
|
||||
json.dump(data, tmp, ensure_ascii=False, indent=2)
|
||||
tmp.close()
|
||||
os.replace(tmp.name, str(fp))
|
||||
except Exception:
|
||||
tmp.close()
|
||||
Path(tmp.name).unlink(missing_ok=True)
|
||||
raise
|
||||
|
||||
|
||||
def get_session_state(session_id: str) -> Optional[dict]:
|
||||
|
||||
Reference in New Issue
Block a user