forked from kellyclaudeai/memory-system-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory-cli.sh
More file actions
executable file
·253 lines (218 loc) · 6.6 KB
/
memory-cli.sh
File metadata and controls
executable file
·253 lines (218 loc) · 6.6 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/bin/bash
# Memory System v2.0 - CLI Interface
MEMORY_DIR="$HOME/clawd/memory"
INDEX_DIR="$MEMORY_DIR/index"
DAILY_DIR="$MEMORY_DIR/daily"
CONSOLIDATED_DIR="$MEMORY_DIR/consolidated"
# Ensure directory structure exists
mkdir -p "$INDEX_DIR" "$DAILY_DIR" "$CONSOLIDATED_DIR"
# Initialize index if it doesn't exist
if [[ ! -f "$INDEX_DIR/memory-index.json" ]]; then
echo '{
"version": "2.0",
"lastUpdated": "'$(date -u +"%Y-%m-%dT%H:%M:%SZ")'",
"memories": [],
"stats": {
"totalMemories": 0,
"byType": {},
"byImportance": {}
}
}' > "$INDEX_DIR/memory-index.json"
fi
# Function: Generate memory ID
generate_id() {
echo "mem_$(date +%Y%m%d)_$(printf "%03d" $RANDOM)"
}
# Function: Capture memory
capture_memory() {
local type="$1"
local importance="$2"
local content="$3"
local tags="$4"
local context="$5"
local id=$(generate_id)
local timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
local today=$(date +%Y-%m-%d)
local daily_file="$DAILY_DIR/$today.md"
# Append to daily log
echo "" >> "$daily_file"
echo "## [$timestamp] $type (importance: $importance)" >> "$daily_file"
echo "$content" >> "$daily_file"
if [[ -n "$context" ]]; then
echo "**Context:** $context" >> "$daily_file"
fi
if [[ -n "$tags" ]]; then
echo "**Tags:** $tags" >> "$daily_file"
fi
echo "" >> "$daily_file"
# Get line number
local line=$(wc -l < "$daily_file")
# Create memory entry
local memory_entry=$(cat <<EOF
{
"id": "$id",
"timestamp": "$timestamp",
"type": "$type",
"importance": $importance,
"content": "$content",
"file": "daily/$today.md",
"line": $line,
"tags": $(echo "$tags" | jq -R 'split(",") | map(gsub("^\\s+|\\s+$";""))'),
"context": "$context"
}
EOF
)
# Add to index
local temp_index=$(mktemp)
jq --argjson entry "$memory_entry" \
'.memories += [$entry] | .lastUpdated = "'$timestamp'" | .stats.totalMemories += 1' \
"$INDEX_DIR/memory-index.json" > "$temp_index"
mv "$temp_index" "$INDEX_DIR/memory-index.json"
echo "✅ Memory captured: $id"
echo " Type: $type | Importance: $importance"
echo " File: $daily_file:$line"
}
# Function: Search memories
search_memory() {
local query="$1"
local limit="${2:-10}"
jq --arg query "$query" --argjson limit "$limit" '
.memories
| map(select(.content | ascii_downcase | contains($query | ascii_downcase)))
| sort_by(.importance) | reverse
| .[:$limit]
| .[]
| "\(.timestamp) | \(.type) | imp:\(.importance) | \(.content)"
' "$INDEX_DIR/memory-index.json" -r
}
# Function: Recent memories
recent_memory() {
local type="${1:-all}"
local days="${2:-7}"
local limit="${3:-20}"
local cutoff=$(date -v-${days}d -u +"%Y-%m-%dT%H:%M:%SZ" 2>/dev/null || date -u -d "$days days ago" +"%Y-%m-%dT%H:%M:%SZ")
if [[ "$type" == "all" ]]; then
jq --arg cutoff "$cutoff" --argjson limit "$limit" '
.memories
| map(select(.timestamp > $cutoff))
| sort_by(.timestamp) | reverse
| .[:$limit]
| .[]
| "\(.timestamp) | \(.type) | imp:\(.importance) | \(.content)"
' "$INDEX_DIR/memory-index.json" -r
else
jq --arg type "$type" --arg cutoff "$cutoff" --argjson limit "$limit" '
.memories
| map(select(.type == $type and .timestamp > $cutoff))
| sort_by(.timestamp) | reverse
| .[:$limit]
| .[]
| "\(.timestamp) | \(.type) | imp:\(.importance) | \(.content)"
' "$INDEX_DIR/memory-index.json" -r
fi
}
# Function: Stats
show_stats() {
jq -r '
"📊 Memory System Stats",
"========================",
"Total Memories: \(.stats.totalMemories)",
"Last Updated: \(.lastUpdated)",
"",
"By Type:",
(.memories | group_by(.type) | map({type: .[0].type, count: length}) | .[] | " \(.type): \(.count)"),
"",
"By Importance:",
(.memories | group_by(.importance) | map({importance: .[0].importance, count: length}) | sort_by(.importance) | reverse | .[] | " \(.importance): \(.count)")
' "$INDEX_DIR/memory-index.json"
}
# Function: Get memory by ID
get_memory() {
local id="$1"
jq --arg id "$id" '.memories[] | select(.id == $id)' "$INDEX_DIR/memory-index.json"
}
# Function: Consolidate (weekly)
consolidate_weekly() {
local week=$(date -v-7d +%Y-W%V 2>/dev/null || date -d "7 days ago" +%Y-W%V)
local output="$CONSOLIDATED_DIR/${week}.md"
echo "# Weekly Memory Consolidation: $week" > "$output"
echo "" >> "$output"
echo "Generated: $(date)" >> "$output"
echo "" >> "$output"
# Get high-importance memories from last week
echo "## High-Importance Memories (8+)" >> "$output"
echo "" >> "$output"
recent_memory all 7 100 | while read line; do
importance=$(echo "$line" | awk -F'imp:' '{print $2}' | awk '{print $1}')
if [[ $importance -ge 8 ]]; then
echo "- $line" >> "$output"
fi
done
echo "" >> "$output"
echo "## By Type" >> "$output"
for type in learning decision interaction event insight; do
echo "" >> "$output"
echo "### $type" >> "$output"
echo "" >> "$output"
recent_memory "$type" 7 20 | while read line; do
echo "- $line" >> "$output"
done
done
echo "✅ Weekly consolidation saved: $output"
}
# Main command router
case "$1" in
capture)
shift
# Parse arguments
type=""
importance=""
content=""
tags=""
context=""
while [[ $# -gt 0 ]]; do
case "$1" in
--type) type="$2"; shift 2 ;;
--importance) importance="$2"; shift 2 ;;
--content) content="$2"; shift 2 ;;
--tags) tags="$2"; shift 2 ;;
--context) context="$2"; shift 2 ;;
*) shift ;;
esac
done
if [[ -z "$type" || -z "$importance" || -z "$content" ]]; then
echo "❌ Usage: memory capture --type TYPE --importance 1-10 --content 'text' [--tags 'tag1,tag2'] [--context 'context']"
exit 1
fi
capture_memory "$type" "$importance" "$content" "$tags" "$context"
;;
search)
search_memory "$2" "${3:-10}"
;;
recent)
recent_memory "${2:-all}" "${3:-7}" "${4:-20}"
;;
stats)
show_stats
;;
get)
get_memory "$2"
;;
consolidate)
consolidate_weekly
;;
*)
echo "Memory System v2.0 CLI"
echo ""
echo "Usage:"
echo " memory capture --type TYPE --importance N --content 'text' [--tags 'a,b'] [--context 'ctx']"
echo " memory search 'query' [limit]"
echo " memory recent [type] [days] [limit]"
echo " memory get <id>"
echo " memory stats"
echo " memory consolidate"
echo ""
echo "Types: learning, decision, interaction, event, insight"
echo "Importance: 1-10 (10 = most important)"
;;
esac