Upload hooks/session_auto_reset.py with huggingface_hub
Browse files- hooks/session_auto_reset.py +33 -0
hooks/session_auto_reset.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Auto-reset channel sessions with >50 messages. Run via picoclaw cron."""
|
| 3 |
+
import os, json, shutil
|
| 4 |
+
|
| 5 |
+
sess = os.path.expanduser("~/.picoclaw/workspace/sessions")
|
| 6 |
+
backup = sess + "_backup"
|
| 7 |
+
os.makedirs(backup, exist_ok=True)
|
| 8 |
+
|
| 9 |
+
reset = []
|
| 10 |
+
if os.path.exists(sess):
|
| 11 |
+
for f in os.listdir(sess):
|
| 12 |
+
if not f.endswith('.meta.json'):
|
| 13 |
+
continue
|
| 14 |
+
full = os.path.join(sess, f)
|
| 15 |
+
try:
|
| 16 |
+
with open(full) as fh:
|
| 17 |
+
meta = json.load(fh)
|
| 18 |
+
count = meta.get("count", 0)
|
| 19 |
+
chat = meta.get("scope", {}).get("values", {}).get("chat", "")
|
| 20 |
+
if count > 50 and "channel:" in chat:
|
| 21 |
+
base = f.replace('.meta.json', '')
|
| 22 |
+
for suffix in ['.meta.json', '.jsonl']:
|
| 23 |
+
target = os.path.join(sess, base + suffix)
|
| 24 |
+
if os.path.exists(target):
|
| 25 |
+
shutil.copy2(target, os.path.join(backup, base + suffix))
|
| 26 |
+
os.remove(target)
|
| 27 |
+
reset.append(f"{base} (count={count})")
|
| 28 |
+
except:
|
| 29 |
+
pass
|
| 30 |
+
|
| 31 |
+
print(f"Auto-reset: {len(reset)} sessions reset")
|
| 32 |
+
for r in reset:
|
| 33 |
+
print(f" {r}")
|