Skip to content

Instantly share code, notes, and snippets.

@plainOldCode
Created July 16, 2025 08:30
Show Gist options
  • Save plainOldCode/50c008e9b19b02b0e9e59f357cc1f552 to your computer and use it in GitHub Desktop.
Save plainOldCode/50c008e9b19b02b0e9e59f357cc1f552 to your computer and use it in GitHub Desktop.
gemini-cli, summarize file and store to redis, using redis mcp, another AI Agent get it.
#!/bin/bash
# This script interacts with the Gemini CLI and stores its output in Redis.
# Check if gemini CLI is available
if ! command -v gemini &> /dev/null
then
echo "Error: Gemini CLI could not be found. Please install it."
exit 1
fi
# Check if redis-cli is available
if ! command -v redis-cli &> /dev/null
then
echo "Error: redis-cli could not be found. Please install Redis."
exit 1
fi
# Usage function
usage() {
echo "Usage: $0 <command> [arguments]"
echo "\nCommands:"
echo " store <file_path> : Summarizes the content of a file and stores it in Redis."
echo " find <query> : Searches for summaries in Redis containing the specified query."
exit 1
}
# Check for command argument
if [ "$#" -eq 0 ]; then
usage
fi
COMMAND="$1"
shift # Remove the command from the arguments list
case "$COMMAND" in
store)
if [ "$#" -ne 1 ]; then
echo "Error: store command requires a file path."
usage
fi
FILE_PATH="$1"
# Check if the file exists
if [ ! -f "$FILE_PATH" ]; then
echo "Error: File not found at '$FILE_PATH'"
exit 1
fi
# Read the content of the file
FILE_CONTENT=$(cat "$FILE_PATH")
if [ -z "$FILE_CONTENT" ]; then
echo "Error: File '$FILE_PATH' is empty."
exit 1
fi
# Construct the prompt for Gemini to generate a summary and a human-readable title
# We instruct Gemini to format the output with 'TITLE: [Your Title]' on the first line.
PROMPT_FOR_GEMINI="Summarize the following text, ensuring all important information is covered. Present the summary concisely using bullet points. Provide a human-readable title for this summary. Format your response as: 'TITLE: [Your Title]\n[Your Summary with bullet points]'\n\nText to summarize:\n${FILE_CONTENT}"
echo "Sending prompt to Gemini CLI..."
GEMINI_RAW_OUTPUT=$(gemini -p "$PROMPT_FOR_GEMINI" --model gemini-2.5-flash)
if [ -z "$GEMINI_RAW_OUTPUT" ]; then
echo "Error: Gemini CLI returned empty output."
exit 1
fi
# Filter out MCP STDERR lines and extract the title and summary from Gemini's output
CLEAN_GEMINI_OUTPUT=$(echo "$GEMINI_RAW_OUTPUT" | grep -v "^MCP STDERR")
# The title is expected to be on a line prefixed with 'TITLE: '
REDIS_KEY=$(echo "$CLEAN_GEMINI_OUTPUT" | grep "^TITLE:" | sed -n 's/^TITLE: \(.*\)/\1/p')
# The summary is the clean output with the TITLE line removed
SUMMARY=$(echo "$CLEAN_GEMINI_OUTPUT" | sed '/^TITLE:/d')
# Fallback to filename if title extraction fails
if [ -z "$REDIS_KEY" ]; then
echo "Warning: Could not extract a human-readable title from Gemini's output. Using filename as key."
REDIS_KEY=$(basename "$FILE_PATH")
fi
# Check if the key already exists in Redis and append timestamp if it does
if redis-cli EXISTS "$REDIS_KEY" &> /dev/null; then
TIMESTAMP=$(date +%Y%m%d%H%M%S)
REDIS_KEY="${REDIS_KEY}_${TIMESTAMP}"
echo "Warning: Key already exists. Appending timestamp to key: '$REDIS_KEY'"
fi
echo "Storing summary in Redis under key: '$REDIS_KEY'"
redis-cli SET "$REDIS_KEY" "$SUMMARY"
echo "Done."
;;
find)
if [ "$#" -ne 1 ]; then
echo "Error: find command requires a query string."
usage
fi
QUERY="$1"
echo "Searching Redis for summaries matching: '$QUERY'"
# Get all keys from Redis
KEYS=$(redis-cli KEYS "*")
FOUND_MATCHES=0
for KEY in $KEYS
do
SUMMARY=$(redis-cli GET "$KEY")
if echo "$SUMMARY" | grep -qi "$QUERY"; then
echo "\n--- Match Found (Key: $KEY) ---"
echo "$SUMMARY"
FOUND_MATCHES=$((FOUND_MATCHES+1))
fi
done
if [ "$FOUND_MATCHES" -eq 0 ]; then
echo "No summaries found matching '$QUERY'."
else
echo "\nFound $FOUND_MATCHES matching summaries."
fi
;;
*)
echo "Error: Unknown command '$COMMAND'"
usage
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment