Skip to content

Instantly share code, notes, and snippets.

@korchasa
Last active May 20, 2024 23:55
Show Gist options
  • Save korchasa/5887f859200a9513c2196c52d208b50d to your computer and use it in GitHub Desktop.
Save korchasa/5887f859200a9513c2196c52d208b50d to your computer and use it in GitHub Desktop.
A script that places all project files in the aichat REPL
#!/bin/bash
# Function: aidir
# Description:
# This function analyzes the project code in a given directory by recursively
# finding all files and processing them. It skips directories specified in the
# environment variable AIDIR_SKIP. If AIDIR_SKIP is not set, no directories are skipped.
# The function concatenates the content of text/source files into a single string
# and saves it to ~/.aidir. Then, it starts a REPL session with aichat, using the
# concatenated content as input.
#
# Usage:
# aidir <directory> [additional aichat options]
#
# Arguments:
# <directory> - The directory to analyze.
# [additional aichat options] - Optional additional options to pass to aichat.
#
# Environment Variables:
# AIDIR_SKIP - A space-separated list of directory names to skip during analysis.
#
# Example:
# export AIDIR_SKIP=".git .idea .terraform"
# aidir /path/to/project
function aidir() {
if [ "${#}" -lt 1 ]; then
echo "Usage: aidir <directory> [additional aichat options]"
return 1
fi
local dir="$1"
shift
local skip_dirs=(.idea .git)
# Read the skip directories from the environment variable AIDIR_SKIP
if [ -n "$AIDIR_SKIP" ]; then
skip_dirs=(`echo ${AIDIR_SKIP}`);
fi
echo "Skip dirs: ${skip_dirs}"
# Add initial prompt directly to the variable
local aichat_input="Analyze the following project code and answer \"OK\""
# Find all files recursively in the given directory
local files
files=$(find "$dir" -type f)
while IFS= read -r file; do
# Check if the file is in one of the skip directories
local skip=false
for skip_dir in "${skip_dirs[@]}"; do
if [[ "$file" == *"/$skip_dir/"* ]]; then
skip=true
break
fi
done
if $skip; then
# echo "Skipping directory: $file"
continue
fi
# Check if the file is a text/source file
if file "$file" | grep -qE 'text|source'; then
content=$(cat "${file}")
echo "Added $file"
aichat_input+="
--- File: $file ---
$content"
else
echo "Skipping binary file: $file"
fi
done <<< "$files"
echo "${aichat_input}" > ~/.aidir
# Start REPL in aichat with the prepared input
aichat --session aidir --save-session -f ~/.aidir $@
aichat --session aidir
}
# Call the aidir function with the provided arguments
aidir "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment