Skip to content

Commit

Permalink
Ensure HUMANSCRIPT_EXECUTE is respected for cached scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
lukechilds committed Aug 5, 2023
1 parent 743ff65 commit 42d1372
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions humanscript
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,29 @@ HUMANSCRIPT_API="${HUMANSCRIPT_API:-"https://api.openai.com"}"
HUMANSCRIPT_EXECUTE="${HUMANSCRIPT_EXECUTE:-"true"}"
HUMANSCRIPT_REGENERATE="${HUMANSCRIPT_REGENERATE:-"false"}"

# Read the script passed to the interpeter
gptscript_path="${1}"
[[ "${gptscript_path}" = "" ]] && echo "Must pass a humanscript" && exit 1
gptscript=$(cat "${gptscript_path}")

# Execute from cache if we have a hit
script_name=$(basename $gptscript_path)
script_hash=$(echo $gptscript | openssl dgst -sha256 | sed 's/^.*= //')
script_cache_path="${CACHE_DIR}/${script_name}-${script_hash}"
[[ "${HUMANSCRIPT_REGENERATE}" = "true" ]] && rm "${script_cache_path}" 2> /dev/null
if [[ -f "${script_cache_path}" ]]
then
if [[ "${HUMANSCRIPT_EXECUTE}" = "true" ]]
then
exec bash "${script_cache_path}"
else
cat "${script_cache_path}"
exit
fi
fi

# System prompt
PROMPT="
system_prompt="
You are humanscript, a human readable script interpreter, here are your rules:
You read an input script consisting of human readable commands and convert it to a bash output script.
You don't talk to humans, you only output fully valid bash.
Expand All @@ -28,25 +49,15 @@ Lines in the input script starting with a '#' are comments and are ignored.
You NEVER respond with markdown, only respond in pure bash.
You NEVER explain anything about the script."

# Read the script passed to the interpeter
gptscript_path="${1}"
[[ "${gptscript_path}" = "" ]] && echo "Must pass a humanscript" && exit 1
gptscript=$(cat "${gptscript_path}")
# User prompt
user_prompt="
### Input script:
$(cat "${gptscript_path}")
### Output script:"

# Execute from cache if we have a hit
script_name=$(basename $gptscript_path)
script_hash=$(echo $gptscript | openssl dgst -sha256 | sed 's/^.*= //')
script_cache_path="${CACHE_DIR}/${script_name}-${script_hash}"
[[ "$HUMANSCRIPT_REGENERATE" = "true" ]] && rm "${script_cache_path}" 2> /dev/null
[[ -f "${script_cache_path}" ]] && exec bash "${script_cache_path}"

# JSON encode strings
stringified_system_prompt=$(echo "${PROMPT}"| jq -sRr @json)
stringified_system_prompt=$(echo "${system_prompt}"| jq -sRr @json)
stringified_user_prompt=$(echo "${user_prompt}"| jq -sRr @json)

# Format JSON payload
Expand Down

0 comments on commit 42d1372

Please sign in to comment.