Files

38 lines
1.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
记忆查询 API供前端查看当前用户的 Mem0 记忆条目。
"""
import asyncio
import logging
from fastapi import APIRouter, Depends
from app.auth import get_current_user
from app.db import User
from app.memory import get_memory
logger = logging.getLogger(__name__)
router = APIRouter(prefix="/memory", tags=["memory"])
@router.get("/list")
async def list_memories(current_user: User = Depends(get_current_user)):
"""返回当前用户的所有记忆条目(只读)。"""
memory = get_memory()
loop = asyncio.get_running_loop()
try:
result = await loop.run_in_executor(
None, lambda: memory.get_all(user_id=current_user.id, limit=200)
)
except Exception as e:
logger.error("获取记忆列表失败: %s", e)
return {"memories": [], "error": str(e)}
results = result.get("results", []) if isinstance(result, dict) else result
logger.info("Mem0 get_all 返回 %d 条记忆, result_type=%s", len(results), type(result).__name__)
if results:
sample = results[0]
logger.info("记忆样本 keys=%s, memory=%s", list(sample.keys()) if isinstance(sample, dict) else "not-dict", str(sample)[:200])
return {"memories": results}