
# like ralph .
# x kimi retry "确定所有的内容都符合 XXX.rule.txt 以及 YYY 要求" "请处理 lib 以下的文件， 使其满足 XXX.rule.txt，直至所有完成。"

___x_cmd_kimi_retry___checkprompt_(){
    local fp="$1"

    x_="

# NOTICE: Please check if the all of the mission is completed.
If completed you should create a file in $fp.DONE
If there is some thing need to do, please write the TODO in $fp.TODO
"
}

___x_cmd_kimi_retry(){
    local prompt=""
    local ncheck=1

    local max=65535

    while [ $# -gt 0 ]; do
        case "$1" in
            -h|--help)      ___x_cmd help -m kimi until "$@" ; return 0 ;;
            --prompt)       prompt="$2";    arg:2:shift ;;
            --ncheck)       ncheck="$2" ;   arg:2:shfit ;;
            *)              break ;;
        esac
    done

    local IFS=" "
    [ $# -eq 0 ] || prompt="$prompt $*"

    local fp="$___X_CMD_ROOT_TMP/kimi/until/mission/$$"

    ___x_cmd ensurefp "$fp"

    local x_=""
    ___x_cmd_kimi_retry___checkprompt_ "$fp"

    local workprompt="$prompt
${x_}
"

    local ith=0
    local icheck=0
    while [ "$ith" -lt "$max" ]; do
        ith=$((ith + 1))
        ___x_cmd kimi -C "$workprompt"

        if [ ! -f "$fp.DONE" ]; then
            icheck=0
        else
            icheck=$((icheck + 1))
            if [ "$icheck" -ge "$ncheck" ]; then
                kimi:info "Task is completed after $icheck times."
                return 0
            fi
            ___x_cmd rmrf "$fp.DONE"
        fi
    done
}
