

___x_cmd_agent_unify(){
    local X_help_cmd=; X_help_cmd='___x_cmd help -m agent unify' help:arg:parse

    local AGENT_FP="$HOME/AGENTS.md"

    local CLAUDE_FP="$HOME/CLAUDE.md"
    local CODEX_FP="$HOME/.codex/AGENTS.md"
    local GEMINI_FP="$HOME/.gemini/GEMINI.md"
    local OPENCODE_FP="$HOME/.config/opencode/AGENTS.md"

    if [ ! -e "$AGENT_FP" ]; then
        ___x_cmd_agent_setup
    fi

    ___x_cmd_agent_unify___try "$CLAUDE_FP" "$GEMINI_FP" "$CODEX_FP" "$OPENCODE_FP"

}

___x_cmd_agent_unify___try(){
    local errcode=0
    local op=""
    local op_hash=""; local agent_fp_hash=""

    while [ $# -gt 0 ]; do
        op="$1"; shift
        if [ ! -e "$op" ]; then
            agent:info "Creating symlink: $op -> $AGENT_FP"
            ___x_cmd_cmds ln -s "$AGENT_FP" "$op"
            continue
        fi

        # Check if content is identical using sha512 hash
        op_hash="$(___x_cmd sha512 "$op")"
        agent_fp_hash="$(___x_cmd sha512 "$AGENT_FP")"

        if [ "$op_hash" = "$agent_fp_hash" ]; then
            agent:info "Content identical, skipping merge: $op"
            ___x_cmd rmrf "$op"
            ___x_cmd_cmds ln -s "$AGENT_FP" "$op"
            continue
        fi

        if ! ___x_cmd agent isready; then
            x:info                      \
                --tip "x agent init"    \
                "Agent is not ready. Please setup according to the tip. You can also manually incorporate the file $op into $AGENT_FP by yourself."
            return 1
        fi

        agent:info "Incorporating file -> $op"

        ___x_cmd agent request                \
            --file "$op"                      \
            --file "$AGENT_FP"                \
            --output "$AGENT_FP"              \
            "Merge the AGENT prompts from the first file into the unified AGENTS.md (second file). Preserve the structure and combine relevant content." || errcode=$?
        case "$errcode" in
            130)        agent:info "RECV SIGINT. EXIT. "; return 130 ;;
            0)          ;;
            *)          x:info "[ERR=$errcode] Fail to incorporate the file using llm. You can retry later or manually incorporated the file $op into $AGENT_FP by yourself."; return "$errcode" ;;
        esac

        ___x_cmd bakman backup "$op"
        ___x_cmd rmrf "$op"
        ___x_cmd_cmds ln -s "$AGENT_FP" "$op"
    done
}
