Setting the file. One moment. List Sessions · Extracting Session Data · bitwarden/ai-plugins · Skills Docs19
Running Work Transitions
(opens in a new tab)
scripts/list-sessions.sh
Shell·135 lines·4 KB
13SORT_BY="date" # date, size, lines
14
15# Parse arguments
16PROJECT_DIR="${PWD}"
17while [[ $# -gt 0 ]]; do
18 case $1 in
19 --format)
20 OUTPUT_FORMAT="$2"
21 shift 2
22 ;;
23 --sort)
24 SORT_BY="$2"
25 shift 2
26 ;;
27 --help)
28 echo "Usage: $0 [project-directory] [options]"
29 echo ""
30 echo "Options:"
31 echo " --format FORMAT Output format: table (default), json, csv"
32 echo " --sort FIELD Sort by: date (default), size, lines"
33 echo " --help Show this help message"
34 echo ""
35 echo "Examples:"
36 echo " $0 # List sessions for current directory"
37 echo " $0 /path/to/project # List sessions for specific project"
38 echo " $0 --format json --sort size # JSON output sorted by size"
39 exit 0
40 ;;
41 *)
42 PROJECT_DIR="$1"
43 shift
44 ;;
45 esac
46done
47
48# Get logs directory
49LOGS_DIR=$("${SCRIPT_DIR}/locate-logs.sh" "${PROJECT_DIR}")
50
51if [ ! -d "${LOGS_DIR}" ]; then
52 echo "Error: Logs directory not found: ${LOGS_DIR}" >&2
53 exit 1
54fi
55
56# Check for session files
57SESSION_FILES=$(find "${LOGS_DIR}" -maxdepth 1 -name "*.jsonl" -type f 2>/dev/null || true)
58
59if [ -z "${SESSION_FILES}" ]; then
60 echo "No session log files found in: ${LOGS_DIR}" >&2
61 exit 0
62fi
63
64# Extract metadata for each session
65declare -a SESSIONS
66
67while IFS= read -r file; do
68 SESSION_ID=$(basename "${file}" .jsonl)
69 FILE_SIZE=$(du -h "${file}" | cut -f1)
70 FILE_SIZE_BYTES=$(stat -f%z "${file}" 2>/dev/null || stat -c%s "${file}" 2>/dev/null)
71 LINE_COUNT=$(wc -l < "${file}" | tr -d ' ')
72 MODIFIED_DATE=$(stat -f%Sm -t "%Y-%m-%d %H:%M:%S" "${file}" 2>/dev/null || stat -c%y "${file}" 2>/dev/null | cut -d'.' -f1)
73
74 # Try to extract git branch from first line
75 GIT_BRANCH=$(head -1 "${file}" | jq -r '.gitBranch // "unknown"' 2>/dev/null || echo "unknown")
76
77 SESSIONS+=("${SESSION_ID}|${FILE_SIZE}|${FILE_SIZE_BYTES}|${LINE_COUNT}|${MODIFIED_DATE}|${GIT_BRANCH}")
78done <<< "${SESSION_FILES}"
79
80# Sort sessions
81case ${SORT_BY} in
82 date)
83 IFS=$'\n' SORTED=($(sort -t'|' -k5 -r <<< "${SESSIONS[*]}"))
84 ;;
85 size)
86 IFS=$'\n' SORTED=($(sort -t'|' -k3 -rn <<< "${SESSIONS[*]}"))
87 ;;
88 lines)
89 IFS=$'\n' SORTED=($(sort -t'|' -k4 -rn <<< "${SESSIONS[*]}"))
90 ;;
91 *)
92 SORTED=("${SESSIONS[@]}")
93 ;;
94esac
95
96# Output in requested format
97case ${OUTPUT_FORMAT} in
98 json)
99 echo "["
100 FIRST=true
101 for session in "${SORTED[@]}"; do
102 IFS='|' read -r id size size_bytes lines date branch <<< "${session}"
103 if [ "${FIRST}" = true ]; then
104 FIRST=false
105 else
106 echo ","
107 fi
108 echo " {"
109 echo " \"sessionId\": \"${id}\","
110 echo " \"size\": \"${size}\","
111 echo " \"sizeBytes\": ${size_bytes},"
112 echo " \"lines\": ${lines},"
113 echo " \"modifiedDate\": \"${date}\","
114 echo " \"gitBranch\": \"${branch}\""
115 echo -n " }"
116 done
117 echo ""
118 echo "]"
119 ;;
120 csv)
121 echo "SessionID,Size,SizeBytes,Lines,ModifiedDate,GitBranch"
122 for session in "${SORTED[@]}"; do
123 IFS='|' read -r id size size_bytes lines date branch <<< "${session}"
124 echo "${id},${size},${size_bytes},${lines},${date},${branch}"
125 done
126 ;;
127 table|*)
128 printf "%-40s %-10s %-10s %-20s %-20s\n" "SESSION_ID" "SIZE" "LINES" "MODIFIED" "BRANCH"
129 printf "%-40s %-10s %-10s %-20s %-20s\n" "----------------------------------------" "----------" "----------" "--------------------" "--------------------"
130 for session in "${SORTED[@]}"; do
131 IFS='|' read -r id size size_bytes lines date branch <<< "${session}"
132 printf "%-40s %-10s %-10s %-20s %-20s\n" "${id}" "${size}" "${lines}" "${date}" "${branch}"
133 done
134 ;;
135esac