Upload memory_cli.py
Browse files- memory_cli.py +75 -0
memory_cli.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Memory CLI - Search and retrieve saved conversations."""
|
| 3 |
+
|
| 4 |
+
import argparse
|
| 5 |
+
import json
|
| 6 |
+
import os
|
| 7 |
+
from memory_system import ConversationMemory
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def main():
|
| 11 |
+
parser = argparse.ArgumentParser(description="Conversation Memory CLI")
|
| 12 |
+
subparsers = parser.add_subparsers(dest="command")
|
| 13 |
+
|
| 14 |
+
search_parser = subparsers.add_parser("search", help="Search memories by text")
|
| 15 |
+
search_parser.add_argument("query", help="Search query")
|
| 16 |
+
search_parser.add_argument("--thread", help="Limit to specific thread")
|
| 17 |
+
|
| 18 |
+
subparsers.add_parser("threads", help="List all conversation threads")
|
| 19 |
+
|
| 20 |
+
show_parser = subparsers.add_parser("show", help="Show a specific thread")
|
| 21 |
+
show_parser.add_argument("thread_id", help="Thread ID to display")
|
| 22 |
+
|
| 23 |
+
export_parser = subparsers.add_parser("export", help="Export memories")
|
| 24 |
+
export_parser.add_argument("--thread", help="Export specific thread")
|
| 25 |
+
export_parser.add_argument("--all", action="store_true", help="Export all threads")
|
| 26 |
+
export_parser.add_argument("--format", choices=["json", "md"], default="json")
|
| 27 |
+
|
| 28 |
+
args = parser.parse_args()
|
| 29 |
+
memory = ConversationMemory(user_id="scottzilla")
|
| 30 |
+
|
| 31 |
+
if args.command == "search":
|
| 32 |
+
results = memory.search(args.query, thread_id=args.thread)
|
| 33 |
+
print(f"\n๐ Found {len(results)} results for '{args.query}':\n")
|
| 34 |
+
for r in results:
|
| 35 |
+
thread = r.get("thread_id", "unknown")
|
| 36 |
+
print(f" [{r['role'].upper()}] {r['content'][:120]}...")
|
| 37 |
+
print(f" Thread: {thread} | {r['timestamp']}\n")
|
| 38 |
+
|
| 39 |
+
elif args.command == "threads":
|
| 40 |
+
threads = memory.get_all_threads()
|
| 41 |
+
print(f"\n๐ {len(threads)} conversation threads:\n")
|
| 42 |
+
for t in threads:
|
| 43 |
+
print(f" ๐ {t['thread_id']}")
|
| 44 |
+
print(f" Messages: {t['message_count']} | Started: {t['started']} | Last: {t['last_message']}")
|
| 45 |
+
|
| 46 |
+
elif args.command == "show":
|
| 47 |
+
messages = memory.get_thread(args.thread_id)
|
| 48 |
+
print(f"\n๐ Thread: {args.thread_id} ({len(messages)} messages)\n")
|
| 49 |
+
for m in messages:
|
| 50 |
+
emoji = "๐ค" if m['role'] == 'user' else "๐ค"
|
| 51 |
+
print(f"{emoji} {m['role'].title()} ({m['timestamp']}):")
|
| 52 |
+
print(f" {m['content']}\n")
|
| 53 |
+
|
| 54 |
+
elif args.command == "export":
|
| 55 |
+
os.makedirs("./memory_exports", exist_ok=True)
|
| 56 |
+
if args.all:
|
| 57 |
+
memory.export_to_json("./memory_exports/all_memories.json")
|
| 58 |
+
print("โ
Exported all memories")
|
| 59 |
+
elif args.thread:
|
| 60 |
+
ext = "md" if args.format == "md" else "json"
|
| 61 |
+
filepath = f"./memory_exports/{args.thread}.{ext}"
|
| 62 |
+
if args.format == "md":
|
| 63 |
+
memory.export_to_markdown(filepath, args.thread)
|
| 64 |
+
else:
|
| 65 |
+
memory.export_to_json(filepath, args.thread)
|
| 66 |
+
else:
|
| 67 |
+
print("โ Use --thread <id> or --all")
|
| 68 |
+
else:
|
| 69 |
+
parser.print_help()
|
| 70 |
+
|
| 71 |
+
memory.close()
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
if __name__ == "__main__":
|
| 75 |
+
main()
|