|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# summarize-notes - Add AI summaries to note listings |
| 5 | +# Usage: list-titles.sh scratch/ | summarize-notes.sh |
| 6 | +# or: search-notes.sh "pattern" | summarize-notes.sh |
| 7 | +# |
| 8 | +# Expected input format: |
| 9 | +# Line 1: Header (e.g., "PATH | TITLE") |
| 10 | +# Line 2+: Data rows (e.g., ".notes/foo.md | My Note") |
| 11 | +# If only header exists (no data rows), means no notes found |
| 12 | +# |
| 13 | +# Adds SUMMARY column with fast AI-generated summaries (parallel processing) |
| 14 | + |
| 15 | +# Model and prompt |
| 16 | +MODEL="groq:meta-llama/llama-4-scout-17b-16e-instruct" |
| 17 | +PROMPT="summarize this file in two sentences, only output summary" |
| 18 | + |
| 19 | +# Check if 'parallel' is available |
| 20 | +if ! command -v parallel &> /dev/null; then |
| 21 | + echo "Error: GNU parallel not installed. Install with: sudo pacman -S parallel" >&2 |
| 22 | + exit 1 |
| 23 | +fi |
| 24 | + |
| 25 | +# Check if 'aichat' command is available |
| 26 | +if ! command -v aichat &> /dev/null; then |
| 27 | + echo "Error: 'aichat' command not found. Make sure it's installed and in PATH." >&2 |
| 28 | + exit 1 |
| 29 | +fi |
| 30 | + |
| 31 | +# Process a single line |
| 32 | +process_line() { |
| 33 | + local line="$1" |
| 34 | + local line_number="$2" |
| 35 | + # Use exported MODEL and PROMPT environment variables |
| 36 | + |
| 37 | + # First line is always the header - append SUMMARY column |
| 38 | + if [ "$line_number" -eq 1 ]; then |
| 39 | + echo "$line | SUMMARY" |
| 40 | + return |
| 41 | + fi |
| 42 | + |
| 43 | + # Extract path (everything before first |) |
| 44 | + local path="${line%%|*}" |
| 45 | + # Trim whitespace (bash parameter expansion) |
| 46 | + path="${path#"${path%%[![:space:]]*}"}" |
| 47 | + path="${path%"${path##*[![:space:]]}"}" |
| 48 | + |
| 49 | + # Skip if file doesn't exist |
| 50 | + if [ ! -f "$path" ]; then |
| 51 | + return # Skip failed files silently |
| 52 | + fi |
| 53 | + |
| 54 | + # Get summary (pass prompt as separate words, not quoted) |
| 55 | + local summary |
| 56 | + summary=$(aichat --model "$MODEL" -f "$path" $PROMPT 2>/dev/null || echo "") |
| 57 | + |
| 58 | + # Skip if summary failed |
| 59 | + if [ -z "$summary" ]; then |
| 60 | + return |
| 61 | + fi |
| 62 | + |
| 63 | + # Collapse multiline to single line (replace newlines with spaces, collapse multiple spaces) |
| 64 | + summary=$(echo "$summary" | tr '\n' ' ' | sed 's/ */ /g') |
| 65 | + # Trim whitespace (bash parameter expansion) |
| 66 | + summary="${summary#"${summary%%[![:space:]]*}"}" |
| 67 | + summary="${summary%"${summary##*[![:space:]]}"}" |
| 68 | + |
| 69 | + # Append summary |
| 70 | + echo "$line | $summary" |
| 71 | +} |
| 72 | + |
| 73 | +export -f process_line |
| 74 | +export MODEL PROMPT |
| 75 | + |
| 76 | +# Read stdin, number lines, and process in parallel (16 jobs) |
| 77 | +cat -n | parallel -j 16 --keep-order --colsep '\t' process_line {2} {1} |
0 commit comments