
# wrap the chat

___x_cmd_agent_chat(){
    local content=""
    local harness=""
    local filelist=""
    local NL="$___X_CMD_UNSEENCHAR_NEWLINE"
    local pipe=""

    local workspace=""
    local reference_workspace=""
    local reference_name=""
    local is_no_thinking=""
    local skill_dirlist=""

    [ "$#" -gt 0 ] || set -- --help
    while [ $# -gt 0 ]; do
        case "$1" in
            -h|--help)      ___x_cmd help -m agent chat "$@" ; return 0 ;;

            --harness)      harness="$2"  ;                     arg:2:shift ;;
            --file|-f)      filelist="${filelist}${NL}${2}" ;   arg:2:shift ;;
            --pipe)         pipe="$(___x_cmd_cmds cat)" ;       shift ;;

            --workspace)    workspace="$2";                     arg:2:shift ;;
            --reference-workspace)
                            reference_workspace="$2";           arg:2:shift ;;
            --no-thinking)  is_no_thinking=1 ;                  shift ;;
            --skill-dir)    skill_dirlist="${skill_dirlist}${NL}${2}" ;   arg:2:shift ;;

            --)             shift; break ;;
            *)              break ;;
        esac
    done

    local x_=""
    if [ -z "$harness" ]; then
        x_=""; ___x_cmd_agent_harness_
        harness="$x_"
    fi

    # Phase 1: base content (pipe + files + positional args)
    local has_content=""
    if [ -n "$pipe" ]; then
        has_content=1
        content="${content}
<STDIN>
${pipe}
</STDIN>
"
    fi

    while read -r f; do
        [ -f "$f" ]  || continue
        has_content=1
        content="${content}
<FILE path=\"${f}\">
$(___x_cmd_cmds cat "$f")
</FILE>
"
    done <<A
$filelist
A

    local IFS=" " ; local msg="$*"
    [ -z "$msg" ] || {
        has_content=1
        content="${content}
<USERMSG>
${msg}
</USERMSG>
"
    }

    # workspace / reference directory handling always runs (for cd and --add-dir)
    if [ -n "$workspace" ]; then
        [ -d "$workspace" ] || ___x_cmd mkdirp "$workspace" || return $?
        x_=""; ___x_cmd abspath_ "$workspace"
        workspace="${x_:-"$workspace"}"
    fi
    if [ -n "$reference_workspace" ]; then
        if [ ! -d "$reference_workspace" ]; then
            reference_workspace=""
        else
            x_=""; ___x_cmd abspath_ "$reference_workspace"
            reference_workspace="${x_:-"$reference_workspace"}"
        fi
    fi

    # Phase 2: extra context only when base content is non-empty
    if [ -n "$has_content" ]; then
        if [ -n "$workspace" ]; then
            x_=""; ___x_cmd_agent_request___workspace_prompt_ "$workspace" || return $?
            content="${content}${x_}"
        fi
        if [ -n "$reference_workspace" ]; then
            x_=""; ___x_cmd_agent_request___reference_name_ "$reference_workspace" "$reference_name"
            reference_name="${x_:-reference}"
            x_=""; ___x_cmd_agent_request___reference_workspace_prompt_ "${workspace:-$PWD}" "$reference_workspace" "$reference_name" || return $?
            content="${content}${x_}"
        fi
        x_=""; ___x_cmd_agent___skill_prompt_ "$skill_dirlist"
        content="${content}${x_}"
    fi

    ___x_cmd_agent_chat___run
}

___x_cmd_agent_chat___run(){
    agent:info "Starting chat with harness '$harness'"
    agent:debug --m:content "$content"
    case "$harness" in
        kimi-cli)       ___x_cmd_agent_chat___run_kimi "$content" "$workspace" "$reference_workspace" "$is_no_thinking" ;;
        claude|claude-*)
            local claude_subcmd="--"
            case "$harness" in
                claude-*)   claude_subcmd="${harness#claude-}" ;;
            esac
            ___x_cmd_agent_chat___run_claude "$content" "$workspace" "$reference_workspace" "$is_no_thinking" "$claude_subcmd"
            ;;
        codex)          ___x_cmd_agent_chat___run_codex "$content" "$workspace" "$reference_workspace" "$is_no_thinking" ;;
        gemini-cli)     ___x_cmd_agent_chat___run_gemini "$content" "$workspace" "$reference_workspace" "$is_no_thinking" ;;
        opencode)       ___x_cmd_agent_chat___run_opencode "$content" "$workspace" "$reference_workspace" "$is_no_thinking" ;;
        "")             N=agent M="Please specify harness using '--harness <name>'" log:ret:64 ;;
        *)              N=agent M="Unsupported harness '$harness'" log:ret:1 ;;
    esac
}

___x_cmd_agent_chat___run_kimi(){(
    local content="$1"
    local workspace="$2"
    local reference_workspace="$3"
    local is_no_thinking="$4"

    local session_id; session_id="$(___x_cmd rand uuid)"

    agent:info "session -> $session_id"
    [ -z "$reference_workspace" ] || agent:info "add-dir -> $reference_workspace"
    [ "$is_no_thinking" != "1" ] || agent:debug "no-thinking enabled"
    [ -n "$content" ] || agent:debug "no initial prompt"

    if [ -n "$workspace" ]; then
        agent:info "cwd -> $workspace"
        ___x_cmd_cmds cd "$workspace" || return $?
    fi

    set -- -S "$session_id" --yolo
    [ "$is_no_thinking" != "1" ] || set -- "$@" --no-thinking
    [ -z "$reference_workspace" ] || set -- "$@" --add-dir "$reference_workspace"

    if [ -n "$content" ]; then
        ___x_cmd kimi -- "$@" --prompt "$content" || return $?
    fi
    ___x_cmd kimi -- "$@"
)}

___x_cmd_agent_chat___run_claude(){(
    local content="$1"
    local workspace="$2"
    local reference_workspace="$3"
    local is_no_thinking="$4"
    local claude_subcmd="$5"

    local cwd="${workspace:-$PWD}"
    ___x_cmd_cmds cd "$cwd" || return $?

    agent:info "cwd -> $cwd"
    [ -z "$reference_workspace" ] || agent:info "add-dir -> $reference_workspace"
    [ "$is_no_thinking" != "1" ] || agent:debug "no-thinking enabled"
    [ -n "$content" ] || agent:debug "no initial prompt"

    set --
    [ "$is_no_thinking" != "1" ] || set -- "$@" --settings '{"alwaysThinkingEnabled": false}'
    [ -z "$reference_workspace" ] || set -- "$@" --add-dir "$reference_workspace"

    if [ -n "$content" ]; then
        set -- "$@" "$content"
    fi
    IS_SANDBOX=1 ___x_cmd claude "$claude_subcmd" "$@" \
        --allowedTools "Write,Read,Edit,Bash(x *),Bash(x-cmd *),Bash(source ~/.x-cmd.root/X *),Bash(git add *),Bash(git commit *),Bash(git diff *),Bash(git log *),Bash(git status),Bash(git status *),Bash(git push *),Bash(git pull *),Bash(git fetch *),Bash(git checkout *),Bash(git branch *),Bash(git stash *),Bash(git merge *),Bash(git tag *),Bash(npm *),Bash(pytest)" \
        --dangerously-skip-permissions
)}

___x_cmd_agent_chat___run_codex(){(
    local content="$1"
    local workspace="$2"
    local reference_workspace="$3"
    local is_no_thinking="$4"

    local cwd="${workspace:-$PWD}"
    ___x_cmd_cmds cd "$cwd" || return $?

    agent:info "cwd -> $cwd"
    [ -z "$reference_workspace" ] || agent:info "add-dir -> $reference_workspace"
    [ "$is_no_thinking" != "1" ] || agent:debug "no-thinking enabled"
    [ -n "$content" ] || agent:debug "no initial prompt"

    set --
    [ "$is_no_thinking" != "1" ] || set -- "$@" --config model_reasoning_effort=low
    [ -z "$reference_workspace" ] || set -- "$@" --add-dir "$reference_workspace"

    if [ -n "$content" ]; then
        set -- "$@" "$content"
    fi
    ___x_cmd codex -- "$@" --skip-git-repo-check
)}

___x_cmd_agent_chat___run_gemini(){(
    local content="$1"
    local workspace="$2"
    local reference_workspace="$3"
    local is_no_thinking="$4"

    local cwd="${workspace:-$PWD}"
    ___x_cmd_cmds cd "$cwd" || return $?

    agent:info "cwd -> $cwd"
    [ -z "$reference_workspace" ] || agent:info "include-dir -> $reference_workspace"
    [ "$is_no_thinking" != "1" ] || agent:debug "no-thinking enabled"
    [ -n "$content" ] || agent:debug "no initial prompt"

    set --
    [ -z "$reference_workspace" ] || set -- "$@" --include-directories "$reference_workspace"

    if [ -n "$content" ]; then
        set -- "$@" -i "$content"
    fi
    if [ "$is_no_thinking" = "1" ]; then
        GEMINI_CLI_SYSTEM_SETTINGS_PATH="$___X_CMD_ROOT_MOD/agent/lib/request/data/gemini-no-think.json" \
            ___x_cmd gemini cli -- --yolo "$@"
    else
        ___x_cmd gemini cli -- --yolo "$@"
    fi
)}

___x_cmd_agent_chat___run_opencode(){(
    local content="$1"
    local workspace="$2"
    local reference_workspace="$3"
    local is_no_thinking="$4"

    local cwd="${workspace:-$PWD}"
    ___x_cmd_cmds cd "$cwd" || return $?

    agent:info "cwd -> $cwd"
    [ -z "$reference_workspace" ] || agent:info "add-dir -> $reference_workspace"
    [ "$is_no_thinking" != "1" ] || agent:debug "no-thinking enabled"
    [ -n "$content" ] || agent:debug "no initial prompt"

    set --
    [ "$is_no_thinking" != "1" ] || set -- "$@" --variant low
    [ -z "$reference_workspace" ] || set -- "$@" --add-dir "$reference_workspace"

    if [ -n "$content" ]; then
        set -- "$@" --prompt "$content"
    fi
    OPENCODE_PERMISSION='{"*":"allow"}' ___x_cmd opencode run "$@"
)}
