-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathidstack-learnings-search
More file actions
executable file
·88 lines (78 loc) · 2.35 KB
/
Copy pathidstack-learnings-search
File metadata and controls
executable file
·88 lines (78 loc) · 2.35 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
#!/usr/bin/env bash
# Search learnings from .idstack/learnings.jsonl (and optionally global store).
# Usage: idstack-learnings-search --limit 3
# idstack-learnings-search --limit 5 --type operational
# idstack-learnings-search --keyword canvas
# idstack-learnings-search --cross-project --keyword rubric
# Outputs matching JSONL lines to stdout. Exit 0 always.
LIMIT=3
TYPE=""
KEYWORD=""
CROSS_PROJECT=0
while [ $# -gt 0 ]; do
case "$1" in
--limit) LIMIT="$2"; shift 2 ;;
--limit=*) LIMIT="${1#--limit=}"; shift ;;
--type) TYPE="$2"; shift 2 ;;
--type=*) TYPE="${1#--type=}"; shift ;;
--keyword) KEYWORD="$2"; shift 2 ;;
--keyword=*) KEYWORD="${1#--keyword=}"; shift ;;
--cross-project) CROSS_PROJECT=1; shift ;;
*) shift ;;
esac
done
LOCAL_LEARNINGS=".idstack/learnings.jsonl"
GLOBAL_LEARNINGS="$HOME/.idstack/global/learnings.jsonl"
# Collect source files
SOURCES=""
[ -f "$LOCAL_LEARNINGS" ] && SOURCES="$LOCAL_LEARNINGS"
if [ "$CROSS_PROJECT" -eq 1 ] && [ -f "$GLOBAL_LEARNINGS" ]; then
SOURCES="$SOURCES $GLOBAL_LEARNINGS"
fi
[ -z "$SOURCES" ] && exit 0
fallback_search() {
if [ -n "$KEYWORD" ]; then
cat $SOURCES 2>/dev/null | grep -i "$KEYWORD" | tail -"$LIMIT"
elif [ -n "$TYPE" ]; then
cat $SOURCES 2>/dev/null | grep "\"type\":\"$TYPE\"" | tail -"$LIMIT"
else
cat $SOURCES 2>/dev/null | tail -"$LIMIT"
fi
}
if command -v python3 &>/dev/null; then
python3 -c "
import json, sys
sources = '$SOURCES'.split()
type_filter = '$TYPE'
keyword = '$KEYWORD'.lower()
limit = $LIMIT
matches = []
for src in sources:
is_global = 'global' in src
try:
for line in open(src):
try:
d = json.loads(line)
if type_filter and d.get('type') != type_filter:
continue
if keyword:
insight = (d.get('insight', '') + ' ' + d.get('key', '')).lower()
if keyword not in insight:
continue
if is_global:
d['_source'] = 'global'
matches.append(json.dumps(d))
except Exception:
pass
except Exception:
pass
# Local learnings first (take precedence), then global
for m in matches[-limit:]:
print(m)
" 2>/dev/null || {
# Fallback: basic grep
fallback_search
}
else
fallback_search
fi