From 563689b41a1378d9e7f49a5c72c67c452f54f9e6 Mon Sep 17 00:00:00 2001 From: Maoyin Sun Date: Mon, 26 Aug 2024 16:38:28 +0200 Subject: [PATCH] #32 POSIX compliance --- ell.sh | 58 ++++++------- helpers/load_config.sh | 14 ++-- helpers/logging.sh | 22 ++--- helpers/parse_arguments.sh | 68 ++++++++-------- helpers/piping.sh | 4 +- llm_backends/gemini/generate_completion.sh | 29 ++++--- llm_backends/openai/generate_completion.sh | 24 +++--- plugins/paginator/90_pre_output.sh | 10 +-- plugins/syntax_highlight/50_post_llm.sh | 94 +++++++++++----------- tests/logging.sh | 46 +++++------ 10 files changed, 185 insertions(+), 184 deletions(-) diff --git a/ell.sh b/ell.sh index 8bf8255..557a137 100755 --- a/ell.sh +++ b/ell.sh @@ -13,17 +13,17 @@ fi ELL_VERSION="0.1.1"; -BASE_DIR=$(dirname ${0}); +BASE_DIR=$(dirname "${0}"); -# logging::debug "Importing helper functions"; +# logging_debug "Importing helper functions"; source "${BASE_DIR}/helpers/logging.sh"; source "${BASE_DIR}/helpers/parse_arguments.sh"; source "${BASE_DIR}/helpers/load_config.sh"; source "${BASE_DIR}/helpers/piping.sh"; -logging::debug "Starting ${0}"; +logging_debug "Starting ${0}"; -# logging::debug Loading configuration; +# logging_debug Loading configuration; # This will load the configuration in order: # 1. from configuration files # 2. from default values in the script @@ -57,17 +57,19 @@ source "${BASE_DIR}/llm_backends/generate_completion.sh"; # Deciding where to output if [[ ${ELL_OUTPUT_FILE} != "-" ]]; then if [[ ${ELL_RECORD} != true && ${ELL_INTERACTIVE} != true ]]; then - logging::debug "Outputting to file: ${ELL_OUTPUT_FILE}"; + logging_debug "Outputting to file: ${ELL_OUTPUT_FILE}"; exec 1>${ELL_OUTPUT_FILE}; fi fi -# logging::debug "Checking if we are outputting to a TTY or not"; +# logging_debug "Checking if we are outputting to a TTY or not"; if [[ -z "${TO_TTY}" ]]; then [[ -t 1 ]] && TO_TTY=true || TO_TTY=false; fi export TO_TTY; -read PAGE_SIZE COLUMNS < <(stty size); +read PAGE_SIZE COLUMNS </dev/null | sort -k3 -t/); - logging::debug "Pre LLM hooks: ${pre_llm_hooks}"; + logging_debug "Pre LLM hooks: ${pre_llm_hooks}"; post_llm_hooks=$(ls ${BASE_DIR}/plugins/*/*_post_llm.sh 2>/dev/null | sort -k3 -t/); - logging::debug "Post LLM hooks: ${post_llm_hooks}"; + logging_debug "Post LLM hooks: ${post_llm_hooks}"; piping "${pre_llm_hooks[@]}" \ | orig_generate_completion \ | piping "${post_llm_hooks[@]}"; @@ -97,81 +99,81 @@ if [[ ${ELL_RECORD} == true || ${ELL_INTERACTIVE} == true ]] && [[ "x${ELL_TMP_S export ELL_TMP_SHELL_LOG=$(mktemp); fi export ELL_RECORD=true; - logging::info "Session being recorded to ${ELL_TMP_SHELL_LOG}"; + logging_info "Session being recorded to ${ELL_TMP_SHELL_LOG}"; if [[ ${ELL_INTERACTIVE} == true ]]; then script -q -f -c "ell -i" "${ELL_TMP_SHELL_LOG}"; else script -q -f -c "bash -i" "${ELL_TMP_SHELL_LOG}"; fi - logging::debug "Removing ${ELL_TMP_SHELL_LOG}"; + logging_debug "Removing ${ELL_TMP_SHELL_LOG}"; if [[ ${ELL_OUTPUT_FILE} == "-" ]]; then rm -f "${ELL_TMP_SHELL_LOG}"; fi unset ELL_TMP_SHELL_LOG; unset ELL_RECORD; - logging::info "Record mode exited"; + logging_info "Record mode exited"; exit 0; fi # Logging_debug "Checking if the template is available"; if [[ ! -f "${ELL_TEMPLATE_PATH}${ELL_TEMPLATE}.json" ]]; then - logging::fatal "Template not found: ${ELL_TEMPLATE_PATH}${ELL_TEMPLATE}.json"; + logging_fatal "Template not found: ${ELL_TEMPLATE_PATH}${ELL_TEMPLATE}.json"; exit 1; fi # Logging_debug "Checking if we are going to read from a file"; if [[ ! -z "${ELL_INPUT_FILE}" ]]; then if [[ "x${ELL_INPUT_FILE}" != "x-" && ! -f "${ELL_INPUT_FILE}" ]]; then - logging::fatal "Input file not found: ${ELL_INPUT_FILE}"; + logging_fatal "Input file not found: ${ELL_INPUT_FILE}"; exit 1; else - logging::debug "Reading input from file: ${ELL_INPUT_FILE}, overriding USER_PROMPT"; - USER_PROMPT=$(cat ${ELL_INPUT_FILE} | sed -e 's/\\/\\\\/g' -e 's/"/\\"/g'| awk '{printf "%s\\n", $0}'); + logging_debug "Reading input from file: ${ELL_INPUT_FILE}, overriding USER_PROMPT"; + USER_PROMPT=$(sed -e 's/\\/\\\\/g' -e 's/"/\\"/g' ${ELL_INPUT_FILE} | awk '{printf "%s\\n", $0}'); fi fi # Logging_debug "Checking if we are using terminal output as context"; if [[ -z "${ELL_TMP_SHELL_LOG}" ]]; then - logging::debug "ELL_TMP_SHELL_LOG not set"; + logging_debug "ELL_TMP_SHELL_LOG not set"; else - logging::debug "Loading shell log from ${ELL_TMP_SHELL_LOG}"; - SHELL_CONTEXT="$(tail -c 3000 "${ELL_TMP_SHELL_LOG}" | ${BASE_DIR}/helpers/render_to_text.perl | sed -e 's/\\/\\\\/g' -e 's/"/\\"/g'| awk '{printf "%s\\n", $0}')"; + logging_debug "Loading shell log from ${ELL_TMP_SHELL_LOG}"; + SHELL_CONTEXT="$(tail -c 3000 "${ELL_TMP_SHELL_LOG}" | "${BASE_DIR}/helpers/render_to_text.perl" | sed -e 's/\\/\\\\/g' -e 's/"/\\"/g'| awk '{printf "%s\\n", $0}')"; fi # Logging_debug "Loading the post_input and pre_output hooks"; post_input_hooks=$(ls ${BASE_DIR}/plugins/*/*_post_input.sh 2>/dev/null | sort -k3 -t/); -logging::debug "Post input hooks: ${post_input_hooks}"; +logging_debug "Post input hooks: ${post_input_hooks}"; pre_output_hooks=$(ls ${BASE_DIR}/plugins/*/*_pre_output.sh 2>/dev/null | sort -k3 -t/); -logging::debug "Pre output hooks: ${pre_output_hooks}"; +logging_debug "Pre output hooks: ${pre_output_hooks}"; # Logging_debug "Checking if we are going to enter interactive mode"; if [[ ${ELL_INTERACTIVE} == true ]]; then - logging::info "Interactive mode enabled. ^C to exit"; + logging_info "Interactive mode enabled. ^C to exit"; while true; do echo -ne "${ELL_PS1}"; IFS= read -r USER_PROMPT; USER_PROMPT="$(echo "${USER_PROMPT}" | piping "${post_input_hooks[@]}")"; - logging::debug "Loading shell log from ${ELL_TMP_SHELL_LOG}"; + logging_debug "Loading shell log from ${ELL_TMP_SHELL_LOG}"; if [[ -z "${ELL_TMP_SHELL_LOG}" ]]; then - logging::debug "ELL_TMP_SHELL_LOG not set"; + logging_debug "ELL_TMP_SHELL_LOG not set"; else SHELL_CONTEXT="$(tail -c 3000 "${ELL_TMP_SHELL_LOG}" | ${BASE_DIR}/helpers/render_to_text.perl | sed -e 's/\\/\\\\/g' -e 's/"/\\"/g'| awk '{printf "%s\\n", $0}')"; fi PAYLOAD="$(eval "cat <&2; + echo "${LOG_STYLE_PUNC}[${LOG_STYLE_RESET}$(date +'%Y-%m-%d %H:%M:%S')${LOG_STYLE_PUNC}]${LOG_STYLE_RESET} $(basename "${0}") ${LOG_STYLE_DEBUG}DEBUG${LOG_STYLE_RESET} ${*}" >&2; fi } -function logging::warn() { +function logging_warn() { if [[ "${ELL_LOG_LEVEL}" -ge 4 ]]; then - echo "${LOG_STYLE_PUNC}[${LOG_STYLE_RESET}$(date +'%Y-%m-%d %H:%M:%S')${LOG_STYLE_PUNC}]${LOG_STYLE_RESET} $(basename "${0}") ${LOG_STYLE_WARN}WARN${LOG_STYLE_RESET} ${@}" >&2; + echo "${LOG_STYLE_PUNC}[${LOG_STYLE_RESET}$(date +'%Y-%m-%d %H:%M:%S')${LOG_STYLE_PUNC}]${LOG_STYLE_RESET} $(basename "${0}") ${LOG_STYLE_WARN}WARN${LOG_STYLE_RESET} ${*}" >&2; fi } -function logging::info() { +function logging_info() { if [[ "${ELL_LOG_LEVEL}" -ge 4 ]]; then echo -n "${LOG_STYLE_PUNC}[${LOG_STYLE_RESET}$(date +'%Y-%m-%d %H:%M:%S')${LOG_STYLE_PUNC}]${LOG_STYLE_RESET} $(basename "${0}") " >&2; fi if [[ "${ELL_LOG_LEVEL}" -ge 3 ]]; then - echo "${LOG_STYLE_INFO}INFO${LOG_STYLE_RESET} ${@}" >&2; + echo "${LOG_STYLE_INFO}INFO${LOG_STYLE_RESET} ${*}" >&2; fi } -function logging::error() { +function logging_error() { if [[ "${ELL_LOG_LEVEL}" -ge 4 ]]; then echo -n "${LOG_STYLE_PUNC}[${LOG_STYLE_RESET}$(date +'%Y-%m-%d %H:%M:%S')${LOG_STYLE_PUNC}]${LOG_STYLE_RESET} $(basename "${0}") " >&2; fi if [[ "${ELL_LOG_LEVEL}" -ge 2 ]]; then - echo "${LOG_STYLE_ERROR}ERROR${LOG_STYLE_RESET} ${@}" >&2; + echo "${LOG_STYLE_ERROR}ERROR${LOG_STYLE_RESET} ${*}" >&2; fi } -function logging::fatal() { +function logging_fatal() { if [[ "${ELL_LOG_LEVEL}" -ge 4 ]]; then echo -n "${LOG_STYLE_PUNC}[${LOG_STYLE_RESET}$(date +'%Y-%m-%d %H:%M:%S')${LOG_STYLE_PUNC}]${LOG_STYLE_RESET} $(basename "${0}") " >&2; fi if [[ "${ELL_LOG_LEVEL}" -ge 1 ]]; then - echo "${LOG_STYLE_FATAL}FATAL${LOG_STYLE_RESET} ${@}" >&2; + echo "${LOG_STYLE_FATAL}FATAL${LOG_STYLE_RESET} ${*}" >&2; fi } -export -f logging::debug logging::info logging::warn logging::error logging::fatal; \ No newline at end of file +export -f logging_debug logging_info logging_warn logging_error logging_fatal; \ No newline at end of file diff --git a/helpers/parse_arguments.sh b/helpers/parse_arguments.sh index 19def50..13c51d1 100644 --- a/helpers/parse_arguments.sh +++ b/helpers/parse_arguments.sh @@ -29,9 +29,9 @@ print_version() { function parse_arguments() { if [[ ${#} -eq 0 ]]; then if [[ ${ELL_RECORD} == true ]]; then - logging::debug "Record mode enabled. Context is used."; + logging_debug "Record mode enabled. Context is used."; else - logging::debug "No arguments provided, printing usage"; + logging_debug "No arguments provided, printing usage"; print_usage; exit 64; # EX_USAGE fi @@ -39,101 +39,101 @@ function parse_arguments() { while [[ ${#} -gt 0 ]]; do case "${1}" in -h|--help) - logging::debug "\"-h\" present in args, printing usage"; + logging_debug "\"-h\" present in args, printing usage"; print_usage; exit 0; ;; -V|--version) - logging::debug "\"-V\" present in args, printing version"; + logging_debug "\"-V\" present in args, printing version"; print_version; exit 0; ;; -l|--log-level) - logging::debug "\"-l\" present in args, setting ELL_LOG_LEVEL to ${2}"; - ELL_LOG_LEVEL="${2}"; + logging_debug "\"-l\" present in args, setting ELL_LOG_LEVEL to ${2}"; + export ELL_LOG_LEVEL="${2}"; shift 2; ;; -m|--model) - logging::debug "\"-m\" present in args, setting ELL_LLM_MODEL to ${2}"; - ELL_LLM_MODEL="${2}"; + logging_debug "\"-m\" present in args, setting ELL_LLM_MODEL to ${2}"; + export ELL_LLM_MODEL="${2}"; shift 2; ;; -T|--template-path) - logging::debug "\"-T\" present in args, setting ELL_TEMPLATE_PATH to ${2}"; - ELL_TEMPLATE_PATH="${2}"; + logging_debug "\"-T\" present in args, setting ELL_TEMPLATE_PATH to ${2}"; + export ELL_TEMPLATE_PATH="${2}"; shift 2; ;; -t|--template) - logging::debug "\"-t\" present in args, setting ELL_TEMPLATE to ${2}"; - ELL_TEMPLATE="${2}"; + logging_debug "\"-t\" present in args, setting ELL_TEMPLATE to ${2}"; + export ELL_TEMPLATE="${2}"; shift 2; ;; -f|--input-file) - logging::debug "\"-f\" present in args, setting ELL_INPUT_FILE to ${2}"; - ELL_INPUT_FILE="${2}"; + logging_debug "\"-f\" present in args, setting ELL_INPUT_FILE to ${2}"; + export ELL_INPUT_FILE="${2}"; shift 2; ;; -r|--record) - logging::debug "\"-r\" present in args, setting ELL_RECORD to true"; + logging_debug "\"-r\" present in args, setting ELL_RECORD to true"; if [[ "${ELL_RECORD}" == "true" ]]; then - logging::fatal "Record mode already enabled"; + logging_fatal "Record mode already enabled"; exit 1; fi ELL_RECORD=true; shift 1; ;; -o|--output-file) - logging::debug "\"-o\" present in args, setting ELL_OUTPUT_FILE to ${2}"; - ELL_OUTPUT_FILE="${2}"; + logging_debug "\"-o\" present in args, setting ELL_OUTPUT_FILE to ${2}"; + export ELL_OUTPUT_FILE="${2}"; shift 2; ;; -i|--interactive) - logging::debug "\"-i\" present in args, setting ELL_INTERACTIVE to true"; - ELL_INTERACTIVE=true; + logging_debug "\"-i\" present in args, setting ELL_INTERACTIVE to true"; + export ELL_INTERACTIVE=true; shift 1; ;; --api-style) - logging::debug "\"--api-style\" present in args, setting ELL_API_STYLE to ${2}"; - ELL_API_STYLE="${2}"; + logging_debug "\"--api-style\" present in args, setting ELL_API_STYLE to ${2}"; + export ELL_API_STYLE="${2}"; shift 2; ;; --api-key) - logging::debug "\"--api-key\" present in args, setting ELL_API_KEY to ${2}"; - ELL_API_KEY="${2}"; + logging_debug "\"--api-key\" present in args, setting ELL_API_KEY to ${2}"; + export ELL_API_KEY="${2}"; shift 2; ;; --api-url) - logging::debug "\"--api-url\" present in args, setting ELL_API_URL to ${2}"; - ELL_API_URL="${2}"; + logging_debug "\"--api-url\" present in args, setting ELL_API_URL to ${2}"; + export ELL_API_URL="${2}"; shift 2; ;; --api-disable-streaming) - logging::debug "\"--api-disable-streaming\" present in args, setting ELL_API_STREAM to false"; - ELL_API_STREAM="false"; + logging_debug "\"--api-disable-streaming\" present in args, setting ELL_API_STREAM to false"; + export ELL_API_STREAM="false"; shift 1; ;; -c|--config) - logging::debug "\"-c\" present in args, setting ELL_CONFIG to ${2}"; - ELL_CONFIG="${2}"; + logging_debug "\"-c\" present in args, setting ELL_CONFIG to ${2}"; + export ELL_CONFIG="${2}"; shift 2; ;; -O|--option) # -o A=b -o C=d,E=f - logging::debug "\"-o\" present in args"; + logging_debug "\"-o\" present in args"; other_options="${2}"; IFS=',' read -r -a other_options_array <<< "${other_options}"; for option in "${other_options_array[@]}"; do IFS='=' read -r -a option_array <<< "${option}"; key="${option_array[0]}"; value="${option_array[1]}"; - logging::debug "Setting ${key} to ${value}"; + logging_debug "Setting ${key} to ${value}"; eval "${key}=${value}"; done shift 2; ;; *) - logging::debug "No more options, setting prompt to ${@}"; - USER_PROMPT="${@}"; + logging_debug "No more options, setting prompt to ${*}"; + export USER_PROMPT="${*}"; break; ;; esac diff --git a/helpers/piping.sh b/helpers/piping.sh index 46962b9..3924db3 100755 --- a/helpers/piping.sh +++ b/helpers/piping.sh @@ -5,10 +5,10 @@ piping() { if [[ ${#} -eq 0 || ${1} == '' ]]; then - # logging::debug "No pipes"; + # logging_debug "No pipes"; cat -; else - # logging::debug "Piping: ${@}"; + # logging_debug "Piping: ${@}"; pipes="$(printf " | %s" "${@}")"; pipes="${pipes:3}"; bash -c "${pipes}"; diff --git a/llm_backends/gemini/generate_completion.sh b/llm_backends/gemini/generate_completion.sh index 4faa26b..697ad6f 100644 --- a/llm_backends/gemini/generate_completion.sh +++ b/llm_backends/gemini/generate_completion.sh @@ -2,22 +2,22 @@ function generate_completion() { if [[ ${ELL_API_STREAM} != true ]]; then - logging::debug "Streaming disabled"; + logging_debug "Streaming disabled"; response=$(cat - | curl "${ELL_API_URL}${ELL_LLM_MODEL}:generateContent" \ --silent \ --header "Content-Type: application/json" \ --header "x-goog-api-key: ${ELL_API_KEY}" \ --data-binary @-); # Check if curl was successful - if [ $? -ne 0 ]; then - logging::fatal "Failed to generate completion"; - logging::debug "Response: ${response}"; + if [ "${?}" -ne 0 ]; then + logging_fatal "Failed to generate completion"; + logging_debug "Response: ${response}"; exit 1; else # check if finishReason is present if (echo "${response}" | jq -e '.candidates[0].finishReason' > /dev/null); then if [[ "x$(echo "${response}" | jq -r '.candidates[0].finishReason')" != "xSTOP" ]]; then - logging::error "Unexpected finish reason: $(echo "${response}" | jq -r '.choices[0].finish_reason')"; + logging_error "Unexpected finish reason: $(echo "${response}" | jq -r '.choices[0].finish_reason')"; else echo "${response}" | jq -j -r '.candidates[0].content.parts[0].text'; echo ""; @@ -26,11 +26,11 @@ function generate_completion() { completion_tokens=$(echo "${response}" | jq -j -r '.usageMetadata.candidatesTokenCount'); total_tokens=$(echo "${response}" | jq -j -r '.usageMetadata.totalTokenCount'); echo ''; - logging::info "usage: prompt_tokens=${prompt_tokens}, completion_tokens=${completion_tokens}, total_tokens=${total_tokens}"; + logging_info "usage: prompt_tokens=${prompt_tokens}, completion_tokens=${completion_tokens}, total_tokens=${total_tokens}"; fi fi else - logging::error "Unexpected format: ${response}"; + logging_error "Unexpected format: ${response}"; fi fi else @@ -49,10 +49,10 @@ function generate_completion() { while read -r line; do line=$(echo "${line}" | tr -d '\r'); if [[ ${PART_FINISHED} == true && "x${line}" == "x]" ]]; then - logging::debug "End of stream"; + logging_debug "End of stream"; break; elif [[ ${PART_FINISHED} == true && "x${line}" == "x," ]]; then - logging::debug "skip comma"; + logging_debug "skip comma"; continue; elif [[ ${PART_FINISHED} == true ]]; then PART_FINISHED=false; @@ -61,14 +61,13 @@ function generate_completion() { BUFFER="${BUFFER}${line}"; # trying to parse the buffer as JSON if jq -e . >/dev/null 2>&1 <<<"${BUFFER}"; then - json_chunk=$(echo "${line}" | cut -c 6-); if (echo "${BUFFER}" | jq -e -r '.candidates[0].content.parts[0].text' > /dev/null); then echo "${BUFFER}" | jq -j -r '.candidates[0].content.parts[0].text'; fi if (echo "${BUFFER}" | jq -e -r '.candidates[0].finishReason' > /dev/null); then stop_reason=$(echo "${BUFFER}" | jq -j -r '.candidates[0].finishReason'); if [[ "x${stop_reason}" != "xSTOP" ]]; then - logging::error "Unexpected stop reason: ${stop_reason}"; + logging_error "Unexpected stop reason: ${stop_reason}"; break; fi fi @@ -83,12 +82,12 @@ function generate_completion() { fi fi done - logging::debug "Buffer: ${BUFFER}"; + logging_debug "Buffer: ${BUFFER}"; echo ''; - logging::info "usage: prompt_tokens=${prompt_tokens}, completion_tokens=${completion_tokens}, total_tokens=${total_tokens}"; + logging_info "usage: prompt_tokens=${prompt_tokens}, completion_tokens=${completion_tokens}, total_tokens=${total_tokens}"; } - if [ ${PIPESTATUS[0]} -ne 0 ]; then - logging::fatal "Failed to generate completion"; + if [ "${PIPESTATUS[0]}" -ne 0 ]; then + logging_fatal "Failed to generate completion"; exit 1; fi fi diff --git a/llm_backends/openai/generate_completion.sh b/llm_backends/openai/generate_completion.sh index 27f5f0e..26209b8 100644 --- a/llm_backends/openai/generate_completion.sh +++ b/llm_backends/openai/generate_completion.sh @@ -2,22 +2,22 @@ function generate_completion() { if [[ ${ELL_API_STREAM} != true ]]; then - logging::debug "Streaming disabled"; + logging_debug "Streaming disabled"; response=$(cat - | curl "${ELL_API_URL}" \ --silent \ --header "Content-Type: application/json" \ --header "Authorization: Bearer ${ELL_API_KEY}" \ --data-binary @-); # Check if curl was successful - if [ $? -ne 0 ]; then - logging::fatal "Failed to generate completion"; - logging::debug "Response: ${response}"; + if [ "${?}" -ne 0 ]; then + logging_fatal "Failed to generate completion"; + logging_debug "Response: ${response}"; exit 1; else # check if finish_reason is present if (echo "${response}" | jq -e '.choices[0].finish_reason' > /dev/null); then if [[ $(echo "${response}" | jq -r '.choices[0].finish_reason') != "stop" ]]; then - logging::error "Unexpected finish reason: $(echo "${response}" | jq -r '.choices[0].finish_reason')"; + logging_error "Unexpected finish reason: $(echo "${response}" | jq -r '.choices[0].finish_reason')"; else echo "${response}" | jq -j -r '.choices[0].message.content'; echo ""; @@ -26,11 +26,11 @@ function generate_completion() { completion_tokens=$(echo "${response}" | jq -j -r '.usage.completion_tokens'); total_tokens=$(echo "${response}" | jq -j -r '.usage.total_tokens'); echo ''; - logging::info "usage: prompt_tokens=${prompt_tokens}, completion_tokens=${completion_tokens}, total_tokens=${total_tokens}"; + logging_info "usage: prompt_tokens=${prompt_tokens}, completion_tokens=${completion_tokens}, total_tokens=${total_tokens}"; fi fi else - logging::error "Unexpected format: ${response}"; + logging_error "Unexpected format: ${response}"; fi fi else @@ -52,7 +52,7 @@ function generate_completion() { if (echo "${json_chunk}" | jq -e -r '.finish_reason' > /dev/null); then stop_reason=$(echo "${json_chunk}" | jq -j -r '.finish_reason'); if [[ "x${stop_reason}" != "xstop" ]]; then - logging::error "Unexpected stop reason: ${stop_reason}"; + logging_error "Unexpected stop reason: ${stop_reason}"; fi break; elif (echo "${json_chunk}" | jq -e -r '.usage' > /dev/null); then @@ -61,21 +61,21 @@ function generate_completion() { completion_tokens=$(echo "${json_chunk}" | jq -j -r '.usage.completion_tokens'); total_tokens=$(echo "${json_chunk}" | jq -j -r '.usage.total_tokens'); echo ''; - logging::info "usage: prompt_tokens=${prompt_tokens}, completion_tokens=${completion_tokens}, total_tokens=${total_tokens}"; + logging_info "usage: prompt_tokens=${prompt_tokens}, completion_tokens=${completion_tokens}, total_tokens=${total_tokens}"; fi fi elif [[ -z "${line}" ]]; then # Empty line, skip continue; else - logging::debug "Unexpected line: ${line}"; + logging_debug "Unexpected line: ${line}"; continue; fi done } # Check if curl was successful - if [ ${PIPESTATUS[0]} -ne 0 ]; then - logging::fatal "Failed to generate completion: ${PIPESTATUS[0]}"; + if [ "${PIPESTATUS[0]}" -ne 0 ]; then + logging_fatal "Failed to generate completion: ${PIPESTATUS[0]}"; exit 1; fi fi diff --git a/plugins/paginator/90_pre_output.sh b/plugins/paginator/90_pre_output.sh index e718d01..c96f4f3 100755 --- a/plugins/paginator/90_pre_output.sh +++ b/plugins/paginator/90_pre_output.sh @@ -9,11 +9,11 @@ get_current_column() { # tput u7 > /dev/tty # when TERM=xterm (and relatives) IFS=';' read -r -d R -a pos stty "${oldstty}" - echo "$((${pos[1]} - 1))" + echo "$((pos[1] - 1))" } if [[ ${TO_TTY} == true ]]; then - # logging::debug "Terminal detected"; + # logging_debug "Terminal detected"; CURR_COL=$(get_current_column); BUFFER=""; # Remaining characters in the of the first line is reduced by $CURR_COL @@ -21,7 +21,7 @@ if [[ ${TO_TTY} == true ]]; then LINE_NUM=1; while IFS= read -r -N 1 char; do if [[ "x${char}" == $'x\e' ]]; then - # logging::debug "Consuming escape sequence"; + # logging_debug "Consuming escape sequence"; esc_seq="${char}"; read -r -N 1 char; esc_seq+="${char}"; @@ -43,7 +43,7 @@ if [[ ${TO_TTY} == true ]]; then if [[ "x${char}" == $'x\n' ]]; then LENGTH=${COLUMNS}; NEWLINE=true; - elif (( ${LENGTH} == 0 )); then + elif (( LENGTH == 0 )); then echo; LENGTH=${COLUMNS}; NEWLINE=true; @@ -61,7 +61,7 @@ if [[ ${TO_TTY} == true ]]; then fi done else - logging::debug "Not a terminal"; + logging_debug "Not a terminal"; cat -; fi diff --git a/plugins/syntax_highlight/50_post_llm.sh b/plugins/syntax_highlight/50_post_llm.sh index c46aebc..9ac6005 100755 --- a/plugins/syntax_highlight/50_post_llm.sh +++ b/plugins/syntax_highlight/50_post_llm.sh @@ -4,7 +4,7 @@ if [[ ${TO_TTY} == true ]]; then - # logging::debug "Terminal detected. Applying syntax highlighting"; + # logging_debug "Terminal detected. Applying syntax highlighting"; # Initialize the state machine START_OF_LINE=true; @@ -78,13 +78,13 @@ if [[ ${TO_TTY} == true ]]; then IFS= read -r -N 1 char; ret=${?}; if [[ ${ret} -ne 0 ]]; then - # logging::debug "EOF"; + # logging_debug "EOF"; break; fi buffer="${buffer}${char}"; fi if [[ "x${char}" == $'x\n' ]]; then - # logging::debug "End of line, reseting state"; + # logging_debug "End of line, reseting state"; START_OF_LINE=true; START_OF_CONTENT=false; IN_HEADING=false; @@ -106,7 +106,7 @@ if [[ ${TO_TTY} == true ]]; then echo -ne "${STYLE_RESET}${STYLE_PUNCTUATION}${buffer}${STYLE_RESET}"; fi buffer=""; - # logging::debug "LINE: ${CURRENT_LINE}"; + # logging_debug "LINE: ${CURRENT_LINE}"; CURRENT_LINE=""; else if [[ ${START_OF_LINE} == true ]]; then @@ -116,67 +116,67 @@ if [[ ${TO_TTY} == true ]]; then CURRENT_LINE="${CURRENT_LINE}${char}"; fi if [[ ${START_OF_CONTENT} != true ]]; then - # logging::debug "Block mode"; - # logging::debug "$(echo -ne "${buffer}" | hexdump -C | head -n -1)"; + # logging_debug "Block mode"; + # logging_debug "$(echo -ne "${buffer}" | hexdump -C | head -n -1)"; if [[ "x${buffer}" == $'x ' ]]; then - # logging::debug "Space"; + # logging_debug "Space"; echo -ne "${buffer}"; buffer=""; continue; elif [[ "x${buffer}" =~ ^x#+[[:space:]]$ ]]; then - # logging::debug "Heading mode"; + # logging_debug "Heading mode"; echo -ne "${STYLE_RESET}${STYLE_PUNCTUATION}${buffer::${#buffer}-1}${STYLE_RESET}${STYLE_HEADING}"; buffer="${buffer: -1}"; IN_HEADING=true; START_OF_CONTENT=true; elif [[ "x${buffer}" =~ ^x[[:digit:]]+\.[[:space:]]$ ]]; then - # logging::debug "Ordered list"; + # logging_debug "Ordered list"; echo -ne "${STYLE_RESET}${STYLE_LIST}${buffer::${#buffer}-1}${STYLE_RESET}"; buffer="${buffer: -1}"; IN_LIST=true; START_OF_CONTENT=true; elif [[ "x${buffer}" =~ ^x(\*|\-|\+)[[:space:]]$ ]]; then - # logging::debug "Unordered list"; + # logging_debug "Unordered list"; echo -ne "${STYLE_RESET}${STYLE_LIST}${buffer::${#buffer}-1}${STYLE_RESET}"; buffer="${buffer: -1}"; IN_LIST=true; START_OF_CONTENT=true; elif [[ "x${buffer}" =~ ^x\>$ ]]; then - # logging::debug "Blockquote mode"; + # logging_debug "Blockquote mode"; echo -ne "${STYLE_RESET}${STYLE_PUNCTUATION}${buffer}${STYLE_RESET}${STYLE_BLOCKQUOTE}"; buffer=""; IN_BLOCKQUOTE=true; # START_OF_CONTENT=true; elif [[ "x${buffer}" =~ ^x\`\`\`$ ]]; then if [[ ${IN_CODE_BLOCK} == true ]]; then - # logging::debug "Code block mode off"; + # logging_debug "Code block mode off"; IN_CODE_BLOCK=false; else - # logging::debug "Code block mode on"; + # logging_debug "Code block mode on"; IN_CODE_BLOCK=true; fi echo -ne "${STYLE_RESET}${STYLE_PUNCTUATION}${buffer}${STYLE_RESET}"; START_OF_CONTENT=true; buffer=""; elif [[ "x${buffer}" =~ ^x((#+)|([[:digit:]]+\.?)|((\*|\-|\+){1,})|(\`\`?)|((\_|\-|\+)+))$ ]]; then - # logging::debug "Undetermined"; + # logging_debug "Undetermined"; : else - # logging::debug "Not a block mode"; + # logging_debug "Not a block mode"; START_OF_CONTENT=true; dirty=true; fi fi if [[ ${IN_CODE_BLOCK} == true && ! "x${buffer}" =~ ^x\`\`?$ ]]; then - # logging::debug "Code block mode"; + # logging_debug "Code block mode"; echo -ne "${buffer}"; buffer=""; elif [[ ${START_OF_CONTENT} == true && ${IN_CODE_BLOCK} == false ]]; then - # logging::debug "Content mode"; - # logging::debug "$(echo -ne "${buffer}" | hexdump -C | head -n -1)"; + # logging_debug "Content mode"; + # logging_debug "$(echo -ne "${buffer}" | hexdump -C | head -n -1)"; if [[ ${IN_CODE} == true && ${IN_ESCAPE} == false ]]; then if [[ "x${buffer}" =~ ^x\`$ ]]; then - # logging::debug "Code off"; + # logging_debug "Code off"; echo -ne "${STYLE_RESET}${STYLE_PUNCTUATION}${buffer}$(current_style)"; IN_CODE=false; buffer=""; @@ -185,7 +185,7 @@ if [[ ${TO_TTY} == true ]]; then buffer=""; fi elif [[ ${IN_ESCAPE} == true ]]; then - # logging::debug "Escape off"; + # logging_debug "Escape off"; echo -ne "${STYLE_RESET}${STYLE_PUNCTUATION}${buffer}$(current_style)"; IN_ESCAPE=false; buffer=""; @@ -197,7 +197,7 @@ if [[ ${TO_TTY} == true ]]; then if [[ ${IN_URL} == true ]]; then IN_URL=false; IN_TITLE=true; - # logging::debug "disable URL mode"; + # logging_debug "disable URL mode"; echo -ne "$(current_style)${buffer}"; else echo -ne "${buffer}"; @@ -205,26 +205,26 @@ if [[ ${TO_TTY} == true ]]; then buffer=""; elif [[ "x${buffer}" =~ ^x\*\*\*[^\*]$ || "x${buffer}" =~ ^x___[^_]$ ]]; then if [[ ${IN_BOLD} == true && ${IN_ITALIC} == true ]]; then - # logging::debug "Bold off"; - # logging::debug "Italic off"; + # logging_debug "Bold off"; + # logging_debug "Italic off"; IN_BOLD=false; IN_ITALIC=false; echo -ne "${STYLE_RESET}${STYLE_PUNCTUATION}${buffer::${#buffer}-1}$(current_style)"; elif [[ ${IN_BOLD} == true ]]; then - # logging::debug "Bold off"; - # logging::debug "Italic on"; + # logging_debug "Bold off"; + # logging_debug "Italic on"; IN_BOLD=false; IN_ITALIC=true; echo -ne "${STYLE_RESET}${STYLE_PUNCTUATION}${buffer::${#buffer}-1}$(current_style)"; elif [[ ${IN_ITALIC} == true ]]; then - # logging::debug "Bold on"; - # logging::debug "Italic off"; + # logging_debug "Bold on"; + # logging_debug "Italic off"; IN_BOLD=true; IN_ITALIC=false; echo -ne "${STYLE_RESET}${STYLE_PUNCTUATION}${buffer::${#buffer}-1}$(current_style)"; else - # logging::debug "Bold on"; - # logging::debug "Italic on"; + # logging_debug "Bold on"; + # logging_debug "Italic on"; IN_BOLD=true; IN_ITALIC=true; echo -ne "${STYLE_RESET}${STYLE_PUNCTUATION}${buffer::${#buffer}-1}$(current_style)"; @@ -233,11 +233,11 @@ if [[ ${TO_TTY} == true ]]; then dirty=true; elif [[ "x${buffer}" =~ ^x\*\*[^\*]$ || "x${buffer}" =~ ^x__[^_]$ ]]; then if [[ ${IN_BOLD} == true ]]; then - # logging::debug "Bold off"; + # logging_debug "Bold off"; IN_BOLD=false; echo -ne "${STYLE_RESET}${STYLE_PUNCTUATION}${buffer::${#buffer}-1}$(current_style)"; else - # logging::debug "Bold on"; + # logging_debug "Bold on"; IN_BOLD=true; echo -ne "${STYLE_RESET}${STYLE_PUNCTUATION}${buffer::${#buffer}-1}$(current_style)"; fi @@ -245,11 +245,11 @@ if [[ ${TO_TTY} == true ]]; then dirty=true; elif [[ "x${buffer}" =~ ^x\*[^\*]$ || "x${buffer}" =~ ^x_[^_]$ ]]; then if [[ ${IN_ITALIC} == true ]]; then - # logging::debug "Italic off"; + # logging_debug "Italic off"; IN_ITALIC=false; echo -ne "${STYLE_RESET}${STYLE_PUNCTUATION}${buffer::${#buffer}-1}$(current_style)"; else - # logging::debug "Italic on"; + # logging_debug "Italic on"; IN_ITALIC=true; echo -ne "${STYLE_RESET}${STYLE_PUNCTUATION}${buffer::${#buffer}-1}$(current_style)"; fi @@ -257,11 +257,11 @@ if [[ ${TO_TTY} == true ]]; then dirty=true; elif [[ "x${buffer}" =~ ^x\~\~[^~]$ ]]; then if [[ ${IN_STRIKETHROUGH} == true ]]; then - # logging::debug "Strikethrough off"; + # logging_debug "Strikethrough off"; IN_STRIKETHROUGH=false; echo -ne "${STYLE_RESET}${STYLE_PUNCTUATION}${buffer::${#buffer}-1}$(current_style)"; else - # logging::debug "Strikethrough on"; + # logging_debug "Strikethrough on"; IN_STRIKETHROUGH=true; echo -ne "${STYLE_RESET}${STYLE_PUNCTUATION}${buffer::${#buffer}-1}$(current_style)"; fi @@ -274,18 +274,18 @@ if [[ ${TO_TTY} == true ]]; then elif [[ "x${buffer}" =~ ^x][^\(]$ ]]; then if [[ ${IN_LINK_TEXT} == true ]]; then IN_LINK_TEXT=false; - # logging::debug "Exit link text mode missing URL"; + # logging_debug "Exit link text mode missing URL"; echo -ne "${STYLE_RESET}${STYLE_PUNCTUATION}${buffer::${#buffer}-1}$(current_style)"; buffer="${buffer: -1}"; dirty=true; elif [[ ${IN_IMAGE_TEXT} == true ]]; then IN_LINK_TEXT=false; - # logging::debug "Exit link text mode missing URL"; + # logging_debug "Exit link text mode missing URL"; echo -ne "${STYLE_RESET}${STYLE_PUNCTUATION}${buffer::${#buffer}-1}$(current_style)"; buffer="${buffer: -1}"; dirty=true; else - # logging::debug "']' in the wild"; + # logging_debug "']' in the wild"; echo -ne "${buffer}"; buffer=""; fi @@ -293,45 +293,45 @@ if [[ ${TO_TTY} == true ]]; then if [[ ${IN_LINK_TEXT} == true ]]; then IN_LINK_TEXT=false; IN_URL=true; - # logging::debug "Enter URL mode"; + # logging_debug "Enter URL mode"; echo -ne "${STYLE_RESET}${STYLE_PUNCTUATION}${buffer}${STYLE_RESET}${STYLE_URL}"; buffer=""; elif [[ ${IN_IMAGE_TEXT} == true ]]; then IN_IMAGE_TEXT=false; IN_URL=true; - # logging::debug "Enter URL mode"; + # logging_debug "Enter URL mode"; echo -ne "${STYLE_RESET}${STYLE_PUNCTUATION}${buffer}${STYLE_RESET}${STYLE_URL}"; buffer=""; else - # logging::debug "'](' in the wild"; + # logging_debug "'](' in the wild"; echo -ne "${buffer}"; buffer=""; fi elif [[ "x${buffer}" =~ ^x\[$ ]]; then IN_LINK_TEXT=true; - # logging::debug "Link text mode"; + # logging_debug "Link text mode"; echo -ne "${STYLE_RESET}${STYLE_PUNCTUATION}${buffer}${STYLE_RESET}$(current_style)"; buffer=""; elif [[ "x${buffer}" =~ ^x\)$ ]]; then if [[ ${IN_URL} == true || ${IN_TITLE} == true ]]; then IN_URL=false; IN_TITLE=false; - # logging::debug "Exit URL or its title mode"; + # logging_debug "Exit URL or its title mode"; echo -ne "${STYLE_RESET}${STYLE_PUNCTUATION}${buffer}$(current_style)"; buffer=""; else - # logging::debug "')' in the wild"; + # logging_debug "')' in the wild"; echo -ne "${buffer}"; buffer=""; fi buffer=""; elif [[ "x${buffer}" =~ ^x\!\[$ ]]; then - # logging::debug "Image text mode"; + # logging_debug "Image text mode"; IN_IMAGE_TEXT=true; echo -ne "${STYLE_RESET}${STYLE_PUNCTUATION}${buffer}$(current_style)"; buffer=""; elif [[ "x${buffer}" =~ ^x((_)|(~)|(\*)|(\`)|(\!)|(]))+$ ]]; then - # logging::debug "Undetermined"; + # logging_debug "Undetermined"; : else echo -ne "${buffer}"; @@ -346,7 +346,7 @@ if [[ ${TO_TTY} == true ]]; then done echo -ne "${STYLE_RESET}${STYLE_PUNCTUATION}${buffer}${STYLE_RESET}"; else - # logging::debug "Not a terminal"; + # logging_debug "Not a terminal"; cat -; fi diff --git a/tests/logging.sh b/tests/logging.sh index f6678d0..66b0cb8 100644 --- a/tests/logging.sh +++ b/tests/logging.sh @@ -9,35 +9,35 @@ source "$(dirname "${0}")/../helpers/logging.sh"; export ELL_LOG_LEVEL=5; echo "ELL_LOG_LEVEL=${ELL_LOG_LEVEL}"; -logging::debug "logging::debug"; -logging::warn "logging::warn"; -logging::info "logging::info"; -logging::error "logging::error"; -logging::fatal "logging::fatal"; +logging_debug "logging_debug"; +logging_warn "logging_warn"; +logging_info "logging_info"; +logging_error "logging_error"; +logging_fatal "logging_fatal"; -echo "ELL_LOG_LEVEL=3"; +export ELL_LOG_LEVEL=3; echo "ELL_LOG_LEVEL=${ELL_LOG_LEVEL}"; -logging::debug "logging::debug"; -logging::warn "logging::warn"; -logging::info "logging::info"; -logging::error "logging::error"; -logging::fatal "logging::fatal"; +logging_debug "logging_debug"; +logging_warn "logging_warn"; +logging_info "logging_info"; +logging_error "logging_error"; +logging_fatal "logging_fatal"; -echo "ELL_LOG_LEVEL=2"; +export ELL_LOG_LEVEL=2; echo "ELL_LOG_LEVEL=${ELL_LOG_LEVEL}"; -logging::debug "logging::debug"; -logging::warn "logging::warn"; -logging::info "logging::info"; -logging::error "logging::error"; -logging::fatal "logging::fatal"; +logging_debug "logging_debug"; +logging_warn "logging_warn"; +logging_info "logging_info"; +logging_error "logging_error"; +logging_fatal "logging_fatal"; -echo "ELL_LOG_LEVEL=0"; +export ELL_LOG_LEVEL=0; echo "ELL_LOG_LEVEL=${ELL_LOG_LEVEL}"; -logging::debug "logging::debug"; -logging::warn "logging::warn"; -logging::info "logging::info"; -logging::error "logging::error"; -logging::fatal "logging::fatal"; \ No newline at end of file +logging_debug "logging_debug"; +logging_warn "logging_warn"; +logging_info "logging_info"; +logging_error "logging_error"; +logging_fatal "logging_fatal"; \ No newline at end of file