76 lines
2.2 KiB
Python
76 lines
2.2 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Export closets from a local MemPalace wing for fleet-wide sharing.
|
|
|
|
Privacy rule: only summaries/closets are exported. No raw source_file paths.
|
|
Source filenames are anonymized to just the basename.
|
|
"""
|
|
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
import chromadb
|
|
|
|
PALACE_PATH = "/root/wizards/bezalel/.mempalace/palace"
|
|
FLEET_INCOMING = "/var/lib/mempalace/fleet/incoming"
|
|
WING = "bezalel"
|
|
DOCS_PER_ROOM = 5
|
|
|
|
|
|
def main():
|
|
client = chromadb.PersistentClient(path=PALACE_PATH)
|
|
col = client.get_collection("mempalace_drawers")
|
|
|
|
# Discover rooms in this wing
|
|
all_meta = col.get(include=["metadatas"])["metadatas"]
|
|
rooms = set()
|
|
for m in all_meta:
|
|
if m.get("wing") == WING:
|
|
rooms.add(m.get("room", "general"))
|
|
|
|
Path(FLEET_INCOMING).mkdir(parents=True, exist_ok=True)
|
|
|
|
closets = []
|
|
for room in sorted(rooms):
|
|
results = col.query(
|
|
query_texts=[room],
|
|
n_results=DOCS_PER_ROOM,
|
|
where={"$and": [{"wing": WING}, {"room": room}]},
|
|
include=["documents", "metadatas"],
|
|
)
|
|
docs = results["documents"][0]
|
|
metas = results["metadatas"][0]
|
|
|
|
entries = []
|
|
for doc, meta in zip(docs, metas):
|
|
# Sanitize content: strip absolute workspace paths
|
|
sanitized = doc[:800]
|
|
sanitized = sanitized.replace("/root/wizards/bezalel/", "~/")
|
|
sanitized = sanitized.replace("/root/wizards/", "~/")
|
|
sanitized = sanitized.replace("/home/bezalel/", "~/")
|
|
sanitized = sanitized.replace("/home/", "~/")
|
|
entries.append({
|
|
"content": sanitized,
|
|
"source_basename": Path(meta.get("source_file", "?")).name,
|
|
})
|
|
|
|
closet = {
|
|
"wing": WING,
|
|
"room": room,
|
|
"type": "closet",
|
|
"entries": entries,
|
|
}
|
|
closets.append(closet)
|
|
|
|
out_file = Path(FLEET_INCOMING) / f"{WING}_closets.json"
|
|
with open(out_file, "w") as f:
|
|
json.dump(closets, f, indent=2)
|
|
|
|
print(f"Exported {len(closets)} closets to {out_file}")
|
|
for c in closets:
|
|
print(f" {c['wing']} / {c['room']} : {len(c['entries'])} entries")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|