# shellcheck shell=dash
___x_cmd_llmf_serve(){
    [ "$#" -gt 0 ]      ||   set - --

    local host="" # "127.0.0.1"
    local port="" # "8080"
    local model=""
    local mmproj=""

    while [ $# -gt 0 ]; do
        case "$1" in
            -h|--help)      ___x_cmd help -m llmf serve; return ;;

            -m|--model)     [ "$#" -ge 2 ] || N=llmf M="Please provide --model <model name or model path>"  log:ret:64;
                            model="$2"  ;;
            --mmproj)       [ "$#" -ge 2 ] || N=llmf M="Please provide --mmproj <mmproj>"                   log:ret:64;
                            mmproj="$2" ;;
            -p|--port)      [ "$#" -ge 2 ] || N=llmf M="Please provide --port <port ~ [0-9]+>"              log:ret:64;
                            port="$2"   ;;
            --host)         [ "$#" -ge 2 ] || N=llmf M="Please provide --host <host>"                       log:ret:64;
                            host="$2"   ;;
            getembedding)   shift; ___x_cmd_llmf_serve_getembedding  "$@"; return   ;;
            --)             shift;          break ;;
            *)              break ;;
        esac
        shift 2
    done

    llmf:prepare:model:mmproj
    [ -z "$model" ]  || set -- "$@" --model "$model"
    [ -z "$mmproj" ] || set -- "$@" --mmproj "$mmproj"
    [ -z "$host" ]   || set -- "$@" --host "$host"
    [ -z "$port" ]   || set -- "$@" --port "$port"

    # --nobrowser
    llmf:debug --cmd "x cosmo llamafile --server $*"
    ___x_cmd_llmf___llamafile_exec --server "$@"
}


___x_cmd_llmf_serve___exec(){
    local model="$1";    shift
    ___x_cmd_llmf___exec "$model" --server "$@"
}

# bge-basg-en gguf (非官方)文件 https://huggingface.co/CompendiumLabs/bge-base-en-v1.5-gguf
# 官方 bin 文件： https://huggingface.co/BAAI/bge-base-en-v1.5
___x_cmd_llmf_serve_getembedding(){
    {
        local x_=; local cmd=
        local url="http://127.0.0.1:8080/embedding"
        local tmp_json_file=""
        local request=" -X POST \"$url\" -H \"Content-Type: application/json\""

        ___x_cmd_llmf_model_default___get_
        [ "${x_%%/*}" = "bge" ] || N=llmf M="The current model is not an embedding model. Please provide a model of 'bge'"   log:ret:1;

        while [ "$#" -gt 0 ]; do
            case "$1" in
                -f|--file)  [ $# -ge 2 ] || N=llmf M="Please provide the filepath" log:ret:64;
                            x_=; ___x_cmd abspath_ "$2"
                            [ -f "$x_" ] || N=llmf M="Please provide a valid [filepath=$2]" log:ret:1
                            tmp_json_file="$___X_CMD_ROOT_TMP/llmf/${2}.json"
                            ___x_cmd yq eval --null-input ".content = \"$(___x_cmd_cmds cat "$x_" )\"" | ___x_cmd y2j > "$tmp_json_file" || return $?

                            cmd="${cmd} ${request} --data-binary @${tmp_json_file}";
                            [ "$#" -le 2 ] || cmd="${cmd} --next"
                            shift 2;
                        ;;

                *)      [ -z "$cmd" ] || break
                        tmp_json_file="$___X_CMD_ROOT_TMP/llmf/$$.json"
                        ___x_cmd yq eval --null-input ".content = \"$*\"" | ___x_cmd y2j > "$tmp_json_file"
                        cmd="${cmd} ${request} --data-binary @${tmp_json_file}";
                        break
                        ;;
            esac
        done

        eval  ___x_cmd curl "$cmd"

    }

}


