From 553a337f7e5d653793b985f87a522c50d2a81fa8 Mon Sep 17 00:00:00 2001 From: Chris Grieser <73286100+chrisgrieser@users.noreply.github.com> Date: Fri, 26 Jan 2024 16:19:27 +0100 Subject: [PATCH] fix(rephraser): escape double quotes for JSON in request --- info.plist | 50 +++++++++++++++++++++++++++++++---- scripts/rephrase-selection.sh | 19 +++++++++---- 2 files changed, 59 insertions(+), 10 deletions(-) diff --git a/info.plist b/info.plist index e218541..dc23ecd 100644 --- a/info.plist +++ b/info.plist @@ -8,6 +8,19 @@ ⭐️ connections + 197CBF3E-ACE7-46FC-B02C-FA08363B1BA7 + + + destinationuid + B34DBBCE-C220-4020-BDB7-DFAB47719F7A + modifiers + 0 + modifiersubtext + + vitoclose + + + 322E5C7E-0088-48C7-8510-ABB6F13FAF6B @@ -142,7 +155,7 @@ destinationuid - B34DBBCE-C220-4020-BDB7-DFAB47719F7A + 197CBF3E-ACE7-46FC-B02C-FA08363B1BA7 modifiers 0 modifiersubtext @@ -501,6 +514,23 @@ version 1 + + config + + matchmode + 0 + matchstring + ' + replacestring + " + + type + alfred.workflow.utility.replace + uid + 197CBF3E-ACE7-46FC-B02C-FA08363B1BA7 + version + 2 + config @@ -560,9 +590,8 @@ The workflow offers four [hotkeys](https://www.alfredapp.com/help/workflows/trig 1. Autocorrect the word under the cursor. 2. Suggest synonyms for the word under the cursor. -3. Rephrase the selected text via ChatGPT, improving its language (requires - OpenAI API key). -4. Same as 3, but use Markdown markup to show the changes: +3. Rephrase the selected text via ChatGPT, improving its language (requires OpenAI API key). +4. Same as 3, but uses Markdown markup to show the changes: + Additions are displayed as `==highlights==`. + Deletions are displayed as `~~strikethroughs~~`. + There is a workflow configuration to alternatively format the changes as [Critic Markup](https://github.com/CriticMarkup/CriticMarkup-toolkit). @@ -572,6 +601,17 @@ The workflow offers four [hotkeys](https://www.alfredapp.com/help/workflows/trig Created by [Chris Grieser](https://chris-grieser.de/). uidata + 197CBF3E-ACE7-46FC-B02C-FA08363B1BA7 + + colorindex + 8 + note + single quotes back to double quotes + xpos + 625 + ypos + 405 + 322E5C7E-0088-48C7-8510-ABB6F13FAF6B colorindex @@ -761,7 +801,7 @@ DOUBLE CLICK TO EDIT description - OpenAI API Key. If empty, will look through `.zshenv` for a variable `$OPENAI_API_KEY`. + OpenAI API Key. If empty, will use $OPENAI_API_KEY exported in .zshenv. label Rephrasing type diff --git a/scripts/rephrase-selection.sh b/scripts/rephrase-selection.sh index d871a99..62e55cf 100755 --- a/scripts/rephrase-selection.sh +++ b/scripts/rephrase-selection.sh @@ -3,7 +3,6 @@ #─────────────────────────────────────────────────────────────────────────────── selection="$*" -the_prompt="$static_prompt$selection" # WARN `$prompt` is reserved variable in zsh cache="$alfred_workflow_cache" mkdir -p "$cache" model="gpt-3.5-turbo" # https://platform.openai.com/docs/models/gpt-3 @@ -20,23 +19,33 @@ if [[ -z "$apikey" ]]; then return 1 fi +# WARN `$prompt` is reserved variable in zsh +# escape quotes in prompt for JSON +the_prompt=$(echo "$static_prompt $selection" | sed -e "s/\"/'/g") + # OPENAI API CALL # DOCS https://platform.openai.com/docs/api-reference/making-requests response=$(curl --silent https://api.openai.com/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $apikey" \ - -d "{ \"model\": \"$model\", \"messages\": [{\"role\": \"user\", \"content\": \"$the_prompt\"}], \"temperature\": $temperature }" | - grep '"content"' | cut -d'"' -f4) # doing this avoids jq dependency + -d "{ \"model\": \"$model\", \"messages\": [{\"role\": \"user\", \"content\": \"$the_prompt\"}], \"temperature\": $temperature }") + +if grep -q '"error"' ; then + # doing this avoids jq dependency + text="ERROR: $(echo "$response" | grep '"message"' | cut -d'"' -f4)" +else + text=$(echo "$response" | grep '"content"' | cut -d'"' -f4) +fi #─────────────────────────────────────────────────────────────────────────────── if [[ "$output_type" == "plain" ]]; then - echo -n "$response" + echo -n "$text" exit 0 fi echo "$selection" >"$cache/selection.txt" -echo "$response" >"$cache/rephrased.txt" +echo "$text" >"$cache/rephrased.txt" # https://unix.stackexchange.com/questions/677764/show-differences-in-strings diff=$(git diff --word-diff "$cache/selection.txt" "$cache/rephrased.txt" |