storage.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import sqlite3
  2. from .config import SQLITE_PATH
  3. def sqlite_delete_ids(memory_ids: list[str]) -> int:
  4. """Delete history rows by memory ID from SQLite and return deleted count."""
  5. if not memory_ids:
  6. return 0
  7. try:
  8. conn = sqlite3.connect(SQLITE_PATH)
  9. cur = conn.cursor()
  10. placeholders = ",".join("?" * len(memory_ids))
  11. cur.execute(f"DELETE FROM history WHERE memory_id IN ({placeholders})", memory_ids)
  12. deleted = cur.rowcount
  13. conn.commit()
  14. conn.close()
  15. return deleted
  16. except Exception as exc:
  17. print(f"[sqlite] warning: {exc}")
  18. return 0
  19. def chroma_get_all(collection, user_id: str, include: list | None = None) -> list[dict]:
  20. """Page through a Chroma collection and return all rows for one user."""
  21. if include is None:
  22. include = ["metadatas"]
  23. results = []
  24. batch = 500
  25. offset = 0
  26. while True:
  27. page = collection.get(
  28. where={"user_id": {"$eq": user_id}},
  29. limit=batch,
  30. offset=offset,
  31. include=include,
  32. )
  33. ids = page.get("ids", [])
  34. if not ids:
  35. break
  36. for i, item_id in enumerate(ids):
  37. row = {"id": item_id}
  38. for field in include:
  39. values = page.get(field, [])
  40. row[field[:-1]] = values[i] if i < len(values) else None
  41. results.append(row)
  42. offset += len(ids)
  43. if len(ids) < batch:
  44. break
  45. return results