-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllmem.sh
More file actions
executable file
·106 lines (87 loc) · 2.62 KB
/
llmem.sh
File metadata and controls
executable file
·106 lines (87 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/bin/bash
# Unified memory management script
# Usage: ./llmem.sh <command> [arguments]
#
# Commands:
# save <title> <content> - Save a new memory
# get_all - Retrieve all memories
# search <query> - Search memories by query
MEMORY_FILE="llmem.json"
COMMAND="$1"
# Function to display usage
usage() {
echo "Usage: $0 <command> [arguments]"
echo ""
echo "Commands:"
echo " save <title> <content> - Save a new memory"
echo " get_all - Retrieve all memories"
echo " search <query> - Search memories by query"
exit 1
}
# Function to save a memory
save_memory() {
TITLE="$1"
CONTENT="$2"
if [ -z "$TITLE" ] || [ -z "$CONTENT" ]; then
echo "Usage: $0 save \"title\" \"content\""
exit 1
fi
# Initialize llmem.json if it doesn't exist
if [ ! -f "$MEMORY_FILE" ]; then
echo "[]" > "$MEMORY_FILE"
fi
# Get the next ID
NEXT_ID=$(jq 'map(.id) | max // 0 | . + 1' "$MEMORY_FILE")
# Get current timestamp in ISO 8601 format
TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%S")
# Create new memory entry
NEW_MEMORY=$(jq -n \
--arg id "$NEXT_ID" \
--arg title "$TITLE" \
--arg content "$CONTENT" \
--arg timestamp "$TIMESTAMP" \
'{id: ($id | tonumber), title: $title, content: $content, timestamp: $timestamp}')
# Add to llmem.json
jq --argjson new "$NEW_MEMORY" '. += [$new]' "$MEMORY_FILE" > "${MEMORY_FILE}.tmp" && mv "${MEMORY_FILE}.tmp" "$MEMORY_FILE"
echo "Memory saved with ID: $NEXT_ID"
}
# Function to get all memories
get_all_memories() {
if [ ! -f "$MEMORY_FILE" ]; then
echo "No memories found. Memory file does not exist."
exit 0
fi
# Format and display all memories
jq -r '.[] | "ID: \(.id)\nTitle: \(.title)\nContent: \(.content)\n---"' "$MEMORY_FILE"
}
# Function to search memories
search_memories() {
QUERY="$1"
if [ -z "$QUERY" ]; then
echo "Usage: $0 search \"query\""
exit 1
fi
if [ ! -f "$MEMORY_FILE" ]; then
echo "No memories found. Memory file does not exist."
exit 0
fi
# Search through title and content using jq and grep
jq -r '.[] | "ID: \(.id)\nTitle: \(.title)\nContent: \(.content)\n---"' "$MEMORY_FILE" | \
grep -B 3 -A 1 -i "$QUERY" | \
sed '/^--$/d'
}
# Main command dispatcher
case "$COMMAND" in
save)
save_memory "$2" "$3"
;;
get_all)
get_all_memories
;;
search)
search_memories "$2"
;;
*)
usage
;;
esac