Created
April 16, 2025 22:42
-
-
Save leegao/781dd8096e439ac9f7ec8a3b7abda870 to your computer and use it in GitHub Desktop.
Pair Bashing with Gemini
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
PPID1=$(ps -o ppid= "$$" | tr -d ' ') | |
PPID2=$(ps -o ppid= "$PPID1" | tr -d ' ') | |
PARENT=$(ps -p "$PPID2" -o comm=) | |
if [ -z "$1" ]; then | |
if [ "$PARENT" != "script" ] ; then | |
export HISTORY_FILE=$(mktemp) | |
echo "Run with history ($HISTORY_FILE), don't forget to ctrl+D" | |
script -f -e -q $HISTORY_FILE | |
echo "Interactions logged in $HISTORY_FILE" | |
exit 0 | |
else | |
echo "Alreadying running under script" | |
fi | |
echo "Usage: <input> | $0 <question>" | |
echo " $0 # runs subsequent commands with history" | |
echo " $0 <question>" | |
exit 1 | |
fi | |
if [ -z "$GEMINI" ]; then | |
GEMINI=gemini-2.0-flash | |
GEMINI=gemini-2.0-flash-thinking-exp-01-21 | |
fi | |
API_ENDPOINT="https://generativelanguage.googleapis.com/v1beta/models/$GEMINI:generateContent" | |
API_KEY="!!!!!!!!!!!!" # Replace with your API key | |
if [ -p /dev/stdin ]; then | |
# Input is coming from a pipe | |
INPUT_DATA=$(cat) # Read all input from the pipe | |
PROMPT="You are a commandline tool and you get your input via a pipe! Structure your answer in a way it can be piped into further tools! Don't use markdown!" | |
QUESTION="$*" | |
JSON_PAYLOAD=$(jq -n \ | |
--arg prompt "$PROMPT" \ | |
--arg question "$QUESTION" \ | |
--arg input "$INPUT_DATA" \ | |
'{ | |
contents: [{ | |
parts: [{text: ($prompt + "\n" + $question + "\nInput: " + $input)}] | |
}] | |
}') | |
else | |
# Input is not coming from a pipe | |
if [ ! -z "$HISTORY_FILE" ]; then | |
HISTORY_DATA=$(cat $HISTORY_FILE | perl -pe 's/\e([^\[\]]|\[.*?[a-zA-Z]|\].*?\a)//g' | col -b) | |
else | |
HISTORY_DATA="ai $*" | |
fi | |
echo $HISTORY_DATA > /tmp/ai.history_data | |
INPUT_DATA= | |
PROMPT=$(cat <<'EOF' | |
You are a commandline (ai) tool to help the user write bash commands. | |
You are given a set of bash interactions as well as a final line with a question (prefixed with the `ai` command, that's you!). | |
Structure your answer to help explain the question asked in the ai command. | |
If you want the user to run a command (either as the answer or to keep investigating), at the end of the message, add a PLEASE_RUN: prefix, then the command to run. | |
Only output one PLEASE_RUN command, you can tell the user to come back to you for the next steps. | |
Everything following PLEASE_RUN: must be valid Bash (IMPORTANT) | |
You don't need to output a PLEASE_RUN suggestion if not necessary | |
You can also ask the user to speak with you again by suggesting a new ai command to run. | |
Feel free to use simple markdown. | |
EOF | |
) | |
QUESTION="$*" | |
JSON_PAYLOAD=$(jq -n \ | |
--arg prompt "$PROMPT" \ | |
--arg history "$HISTORY_DATA" \ | |
'{ | |
contents: [{ | |
parts: [{text: ($prompt + "\n" + "\nHistory:\n" + $history)}] | |
}] | |
}') | |
fi | |
if [ -z "$JSON_PAYLOAD" ]; then | |
exit 1 | |
fi | |
echo "$JSON_PAYLOAD" > /tmp/ai.payload | |
RESPONSE=$(curl -s -X POST "$API_ENDPOINT?key=$API_KEY" \ | |
-H "Content-Type: application/json" \ | |
-d "@/tmp/ai.payload") | |
if [ $? -ne 0 ]; then | |
echo "Error: Failed to communicate with the API." | |
exit 1 | |
fi | |
ANSWER=$(echo "$RESPONSE" | jq -r '.candidates[0].content.parts[0].text // "Error: Unable to parse response."') | |
echo "$ANSWER" > /tmp/ai.answer.md | |
echo | |
echo | |
glow /tmp/ai.answer.md | |
SUGGESTION=$(grep -oP 'PLEASE_RUN: \K.*' <<< "$(</tmp/ai.answer.md)") | |
if [ ! -z "$SUGGESTION" ]; then | |
echo | |
echo | |
read -p "Run \`$SUGGESTION\`? [y/N]" run | |
if [[ "$run" == "Y" ]] || [[ "$run" == "y" ]]; then | |
bash -c "$SUGGESTION" | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment