@@ -178,7 +178,7 @@ _call_curl() {
178178 -N
179179 --max-time " $OBL_TIMEOUT "
180180 -H ' Content-Type: application/json'
181- -w ' \n %{http_code}'
181+ -w ' HTTP_CODE_DELIMITER %{http_code}'
182182 )
183183
184184 if [[ -n " ${OBL_TURBO_KEY} " ]]; then
@@ -210,9 +210,9 @@ _call_curl() {
210210 fi
211211
212212 local http_code
213- http_code=" $( printf ' %s ' " $ response" | tail -n1 ) "
213+ http_code=" ${ response##* HTTP_CODE_DELIMITER} "
214214 local body
215- body=" $( printf ' %s ' " $ response" | sed ' $d ' ) "
215+ body=" ${ response% HTTP_CODE_DELIMITER * } "
216216
217217 if (( http_code >= 400 )) ; then
218218 _error " _call_curl: HTTP error ${http_code} : ${body} "
@@ -1159,21 +1159,21 @@ _ollama_chat_payload() {
11591159 # return 1 # TODO - decide: return 1, or allow empty message history?
11601160 fi
11611161
1162- local messages_json
1163- messages_json=' [' $( IFS=,; echo " ${OBL_MESSAGES[*]} " ) ' ]'
1164-
11651162 local thinking=true
11661163 if [[ " $OBL_THINKING " == ' off' ]]; then
11671164 thinking=false
11681165 fi
11691166
1167+ # Safely construct the JSON payload using jq
1168+ # The OBL_MESSAGES array already contains valid JSON objects.
1169+ # We print each on a new line and use jq's slurp (-s) flag to create a JSON array.
1170+ # Then we add the other fields (model, stream, thinking).
11701171 local json_payload
1171- json_payload=" $( jq -c -n \
1172+ json_payload=" $( printf ' %s\n ' " ${OBL_MESSAGES[@]} " | jq -s -c \
11721173 --arg model " $model " \
1173- --argjson messages " $messages_json " \
11741174 --argjson stream " $stream " \
11751175 --argjson thinking " $thinking " \
1176- ' {model: $model, messages: $messages , stream: $stream, thinking: $thinking}' ) "
1176+ ' {model: $model, messages: . , stream: $stream, thinking: $thinking}' ) "
11771177
11781178 printf ' %s\n' " $json_payload "
11791179}
@@ -1356,7 +1356,8 @@ EOF
13561356
13571357 OBL_STREAM=1
13581358 (
1359- ollama_chat -m " $model " | while IFS= read -r line; do
1359+ ollama_chat_json -m " $model " | while IFS= read -r line; do
1360+ if ! _is_valid_json " $line " ; then continue ; fi
13601361 if [[ " $OBL_THINKING " == ' on' ]]; then
13611362 printf ' %s' " $( jq -r ' .thinking // empty' <<< " $line" ) " >&2
13621363 fi
@@ -1368,7 +1369,7 @@ EOF
13681369 local error_code=$?
13691370 OBL_STREAM=0
13701371 if [[ $error_code -ne 0 ]]; then
1371- _error " ollama_chat_stream: ollama_chat failed with code $error_code "
1372+ _error " ollama_chat_stream: ollama_chat_json failed with code $error_code "
13721373 return 1
13731374 fi
13741375 printf ' \n'
@@ -1423,8 +1424,8 @@ EOF
14231424 _debug " ollama_chat_stream_json: model: [${model: 0: 120} ]"
14241425
14251426 OBL_STREAM=1
1426- if ! ollama_chat -m " $model " ; then
1427- _error ' ollama_chat_stream_json: ollama_chat failed'
1427+ if ! ollama_chat_json -m " $model " ; then
1428+ _error ' ollama_chat_stream_json: ollama_chat_json failed'
14281429 OBL_STREAM=0
14291430 return 1
14301431 fi
@@ -1663,8 +1664,7 @@ EOF
16631664 printf ' %s\n' " $models " | shuf -n1
16641665 else # If shuf is unavailable, fall back to awk's srand().
16651666 # awk's built‑in random generator (more portable, but less uniform)
1666- # printf '%s\n' "$models" | awk 'BEGIN{srand()} {a[NR]=$0} END{if(NR) print a[int(rand()*NR)+1]}'
1667- printf ' %s\n' " $models " | awk ' NR>0 {a[NR]=$0} END{if(NR) print a[int(1+rand()*NR)]}'
1667+ printf ' %s\n' " $models " | awk ' BEGIN{srand()} {a[NR]=$0} END{if(NR>0) print a[int(rand()*NR)+1]}'
16681668 fi
16691669}
16701670
0 commit comments