Skip to content

Instantly share code, notes, and snippets.

@gitmpr
Last active July 29, 2023 17:33
Show Gist options
  • Save gitmpr/e1cb647b2ca4b016c412d19af83f180a to your computer and use it in GitHub Desktop.
Save gitmpr/e1cb647b2ca4b016c412d19af83f180a to your computer and use it in GitHub Desktop.
sgpt wrapper bash functions
# sgpt https://github.com/TheR1D/shell_gpt wrapper bash functions that lets me switch to repl mode where the last sgpt
# prompt is fed into the program so that it is saved in the cache and I can continue chatting with the previous prompt input.
# This works by feeding the previous command first argument into the program again to save the prompt as a file into the cache
# use this instead of `sgpt --editor` to be able to continue continue a chat started from an editor invocation
sgpteditor() {
sgpt --editor --chat $(head /dev/urandom | tr -dc 'a-zA-Z0-9' | head -c 16)
SGPT_EDITOR=1
}
# use this right after a `sgpt <prompt>` or `sgpteditor` command
sgptrepl() {
if [[ $SGPT_EDITOR == 1 ]]; then
CHAT_CACHE_PATH=$(grep -oP '^CHAT_CACHE_PATH=\K.*' ~/.config/shell_gpt/.sgptrc)
newest_chat_file=$(basename $(find "$CHAT_CACHE_PATH" -type f -printf '%T@ %p\n' | sort -n | tail -n 1 | cut -d ' ' -f 2-))
sgpt --repl $newest_chat_file
$SGPT_EDITOR=0
else
prompt=$_
random_name=$(head /dev/urandom | tr -dc 'a-zA-Z0-9' | head -c 16)
sgpt --chat $random_name "$prompt" &>/dev/null && sgpt --repl $random_name
fi
}
CHAT_CACHE_PATH=$(grep -oP '^CHAT_CACHE_PATH=\K.*' ~/.config/shell_gpt/.sgptrc)
newest_chat_file=$(basename $(find "$CHAT_CACHE_PATH" -type f -printf '%T@ %p\n' | sort -n | tail -n 1 | cut -d ' ' -f 2-))
echo "$newest_chat_file"
# ~/.config/shell_gpt/.sgptrc
# filename is hash/digest of prompt at /tmp/shell_gpt/cache/, file content is prompt string
# /tmp/shell_gpt/chat_cache/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment