Skip to content

Commit 3b0cd94

Browse files
committed
Safe escaping using jq
1 parent 4f1f92d commit 3b0cd94

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

chatgpt.sh

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,10 @@ build_chat_context() {
122122
else
123123
chat_context="$chat_context\nQ: $escaped_request_prompt"
124124
fi
125-
request_prompt="${chat_context//$'\n'/\\n}"
125+
}
126+
127+
escape(){
128+
echo "$1" | jq -Rrs 'tojson[1:-1]'
126129
}
127130

128131
# maintain chat context function for /completions (all models except
@@ -326,7 +329,7 @@ while $running; do
326329
echo -e "${CHATGPT_CYAN_LABEL}Complete details for model: ${prompt#*model:}\n ${model_data}"
327330
elif [[ "$prompt" =~ ^command: ]]; then
328331
# escape quotation marks, new lines, backslashes...
329-
escaped_prompt=$(echo "$prompt" | sed 's/"/\\"/g')
332+
escaped_prompt=$(escape "$prompt")
330333
escaped_prompt=${escaped_prompt#command:}
331334
request_prompt=$COMMAND_GENERATION_PROMPT$escaped_prompt
332335
build_user_chat_message "$request_prompt"
@@ -351,16 +354,14 @@ while $running; do
351354
eval $response_data
352355
fi
353356
fi
354-
add_assistant_response_to_chat_message "$(echo "$response_data" | tr '\n' ' ')"
357+
add_assistant_response_to_chat_message "$(escape "$response_data")"
355358

356359
timestamp=$(date +"%d/%m/%Y %H:%M")
357360
echo -e "$timestamp $prompt \n$response_data \n" >>~/.chatgpt_history
358361

359362
elif [[ "$MODEL" =~ ^gpt- ]]; then
360-
# escape quotation marks
361-
escaped_prompt=$(echo "$prompt" | sed 's/"/\\"/g')
362-
# escape new lines
363-
request_prompt=${escaped_prompt//$'\n'/' '}
363+
# escape quotation marks, new lines, backslashes...
364+
request_prompt=$(escape "$prompt")
364365

365366
build_user_chat_message "$request_prompt"
366367
response=$(request_to_chat "$chat_message")
@@ -376,16 +377,13 @@ while $running; do
376377
else
377378
echo -e "${CHATGPT_CYAN_LABEL}${response_data}" | fold -s -w $COLUMNS
378379
fi
379-
escaped_response_data=$(echo "$response_data" | sed 's/"/\\"/g')
380-
add_assistant_response_to_chat_message "$chat_message" "$escaped_response_data"
380+
add_assistant_response_to_chat_message "$(escape "$response_data")"
381381

382382
timestamp=$(date +"%d/%m/%Y %H:%M")
383383
echo -e "$timestamp $prompt \n$response_data \n" >>~/.chatgpt_history
384384
else
385-
# escape quotation marks
386-
escaped_prompt=$(echo "$prompt" | sed 's/"/\\"/g')
387-
# escape new lines
388-
request_prompt=${escaped_prompt//$'\n'/' '}
385+
# escape quotation marks, new lines, backslashes...
386+
request_prompt=$(escape "$prompt")
389387

390388
if [ "$CONTEXT" = true ]; then
391389
build_chat_context "$request_prompt"
@@ -407,7 +405,7 @@ while $running; do
407405
fi
408406

409407
if [ "$CONTEXT" = true ]; then
410-
maintain_chat_context "$escaped_response_data"
408+
maintain_chat_context "$(escape "$response_data")"
411409
fi
412410

413411
timestamp=$(date +"%d/%m/%Y %H:%M")

0 commit comments

Comments
 (0)