Skip to content

Commit

Permalink
#32 POSIX compliance
Browse files Browse the repository at this point in the history
  • Loading branch information
simonmysun committed Aug 26, 2024
1 parent 427a79b commit 563689b
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 184 deletions.
58 changes: 30 additions & 28 deletions ell.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <<EOF
$(stty size)
EOF
if [[ -z "${PAGE_SIZE}" ]]; then
PAGE_SIZE=24;
fi
Expand All @@ -81,9 +83,9 @@ export COLUMNS;
eval "$(echo -ne "orig_"; declare -f generate_completion)";
generate_completion() {
pre_llm_hooks=$(ls ${BASE_DIR}/plugins/*/*_pre_llm.sh 2>/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[@]}";
Expand All @@ -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 <<EOF
$(<${ELL_TEMPLATE_PATH}${ELL_TEMPLATE}.json)
$(<"${ELL_TEMPLATE_PATH}${ELL_TEMPLATE}.json")
EOF")";
echo -ne "${ELL_PS2}";
echo "${PAYLOAD}" | generate_completion | piping "${pre_output_hooks[@]}";
done
logging::debug "Exiting interactive mode";
logging_debug "Exiting interactive mode";
else
USER_PROMPT=$(echo "${USER_PROMPT}" | piping "${post_input_hooks[@]}");

PAYLOAD="$(eval "cat <<EOF
$(<${ELL_TEMPLATE_PATH}${ELL_TEMPLATE}.json)
$(<"${ELL_TEMPLATE_PATH}${ELL_TEMPLATE}.json")
EOF")";

echo "${PAYLOAD}" | generate_completion | piping "${pre_output_hooks[@]}";
fi

logging::debug "END OF ELL";
logging_debug "END OF ELL";
14 changes: 7 additions & 7 deletions helpers/load_config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,31 @@
# Will read from $HOME/.ellrc, $PWD/.ellrc, and $ELL_CONFIG

function load_config() {
logging::debug "Storing current environment";
logging_debug "Storing current environment";
current_env=$(declare -p -x | sed -e 's/declare -x /export /');
set -o allexport;
if [[ -f "${HOME}/.ellrc" ]]; then
logging::debug "Loading config from ${HOME}/.ellrc (from \$HOME)";
logging_debug "Loading config from ${HOME}/.ellrc (from \$HOME)";
source "${HOME}/.ellrc"
fi

if [[ -f "${PWD}/.ellrc" ]]; then
logging::debug "Loading config from ${PWD}/.ellrc (from \$PWD)";
logging_debug "Loading config from ${PWD}/.ellrc (from \$PWD)";
source "${PWD}/.ellrc"
fi

if [[ -z "${ELL_CONFIG}" ]]; then
logging::debug "ELL_CONFIG is not set";
logging_debug "ELL_CONFIG is not set";
else
if [[ -f "${ELL_CONFIG}" ]]; then
logging::debug "Loading config from ${ELL_CONFIG}";
logging_debug "Loading config from ${ELL_CONFIG}";
source "${ELL_CONFIG}"
else
logging::fatal "Config file ${ELL_CONFIG} not found";
logging_fatal "Config file ${ELL_CONFIG} not found";
exit 1;
fi
fi
logging::debug "Restoring environment";
logging_debug "Restoring environment";
eval "${current_env}";
set +o allexport;
}
Expand Down
22 changes: 11 additions & 11 deletions helpers/logging.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,43 @@
[[ ${TO_TTY} == true ]] && LOG_STYLE_ERROR="$(echo -ne "\e[93m\e[1m")" || LOG_STYLE_ERROR="";
[[ ${TO_TTY} == true ]] && LOG_STYLE_FATAL="$(echo -ne "\e[91m\e[1m")" || LOG_STYLE_FATAL="";

function logging::debug() {
function logging_debug() {
if [[ "${ELL_LOG_LEVEL}" -ge 5 ]]; 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_DEBUG}DEBUG${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_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;
export -f logging_debug logging_info logging_warn logging_error logging_fatal;
Loading

0 comments on commit 563689b

Please sign in to comment.