From e7e16ac3e433b83bbffe0728558cb76aa4f3cd7d Mon Sep 17 00:00:00 2001 From: Ayo Date: Mon, 25 Aug 2025 14:27:12 +0200 Subject: [PATCH] feat(ai): use -t flag to open answer in typora --- ai-coder.sh | 44 +++++++++++++++++++++++++++++++++++---- ai.sh | 60 ++++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 90 insertions(+), 14 deletions(-) mode change 100644 => 100755 ai.sh diff --git a/ai-coder.sh b/ai-coder.sh index d79c40d..b1243b8 100644 --- a/ai-coder.sh +++ b/ai-coder.sh @@ -4,13 +4,49 @@ model=$coder modelfile=$coder_modelfile -if ! [ "$2" = "" ]; then - if [ "$2" = "sleep" ]; then +# Initialize variables +typora_flag=false +other_args="" + +# Process arguments to handle -t flag and collect other args +while [[ $# -gt 0 ]]; do + case $1 in + -t) + typora_flag=true + shift + ;; + *) + other_args="$other_args $1" + shift + ;; + esac +done + +# Set other_args to the first argument if it exists, otherwise empty string +if [[ -n "$other_args" ]]; then + # Remove leading space + other_args="${other_args# }" + + IFS=' ' read -ra args_array <<< "$other_args" + if [[ ${#args_array[@]} -gt 1 ]]; then + # Remove first element and rejoin remaining elements + other_args="${args_array[*]:1}" + else + # If there's only one argument, set other_args to empty string + other_args="" + fi +fi + + +if ! [ "$other_args" = "" ]; then + if [ "$other_args" = "sleep" ]; then ollama stop $model - elif [ "$2" = "init" ]; then + elif [ "$other_args" = "init" ]; then ollama create "$model" -f "$modelfile" else - ollama run $model "$@" --hidethinking + tempfile="$(mktemp)" + ollama run $model "$other_args" --hidethinking > $tempfile + typora $tempfile fi else ollama run $model --hidethinking diff --git a/ai.sh b/ai.sh old mode 100644 new mode 100755 index 0bda063..9805d62 --- a/ai.sh +++ b/ai.sh @@ -4,15 +4,49 @@ model=$helper modelfile=$helper_modelfile -if ! [ "$2" = "" ]; then - if [ "$2" = "open-webui" ]; then +# Initialize variables +typora_flag=false +other_args="" + +# Process arguments to handle -t flag and collect other args +while [[ $# -gt 0 ]]; do + case $1 in + -t) + typora_flag=true + shift + ;; + *) + other_args="$other_args $1" + shift + ;; + esac +done + +# Set other_args to the first argument if it exists, otherwise empty string +if [[ -n "$other_args" ]]; then + # Remove leading space + other_args="${other_args# }" + + IFS=' ' read -ra args_array <<< "$other_args" + if [[ ${#args_array[@]} -gt 1 ]]; then + # Remove first element and rejoin remaining elements + other_args="${args_array[*]:1}" + else + # If there's only one argument, set other_args to empty string + other_args="" + fi +fi + +if ! [ "$other_args" = "" ]; then + + if [ "$other_args" = "open-webui" ]; then . $HOME/open-webui/.venv/bin/activate open-webui serve python --version deactivate - elif [ "$2" = "init" ]; then + elif [ "$other_args" = "init" ]; then ollama create $model -f $modelfile - elif [ "$2" = "wake" ]; then + elif [ "$other_args" = "wake" ]; then . $HOME/llm_env/bin/activate export OLLAMA_NUM_GPU=999 @@ -27,14 +61,20 @@ if ! [ "$2" = "" ]; then echo $ZES_ENABLE_SYSMAN echo $SYCL_CACHE_PERSISTENT - elif [ "$2" = "sleep" ]; then + elif [ "$other_args" = "sleep" ]; then ollama stop $model else - # tempfile="$(mktemp)" - # ollama run $model "$@" --hidethinking > $tempfile - # typora $tempfile - ollama run $model "$@" --hidethinking + # If -t flag is set, use typora to display output + if [ "$typora_flag" = true ]; then + tempfile="$(mktemp)" + ollama run $model "$other_args" > $tempfile + typora $tempfile + else + # If no -t flag, just run the command normally + ollama run $model "$other_args" + fi fi + else - ollama run $model --hidethinking + ollama run $model fi