summaryrefslogtreecommitdiffstats
path: root/scripts/completions/aichat.bash
blob: f0b9bc35a30c55bb36ef5efeb9a19a708f0b98f6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
_aichat() {
    local i cur prev opts cmd
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    cmd=""
    opts=""

    for i in ${COMP_WORDS[@]}
    do
        case "${cmd},${i}" in
            ",$1")
                cmd="aichat"
                ;;
            *)
                ;;
        esac
    done

    case "${cmd}" in
        aichat)
            opts="-m -r -s -e -c -f -H -S -w -h -V --model --role --session --save-session --serve --execute --code --file --no-highlight --no-stream --wrap --light-theme --dry-run --info --list-models --list-roles --list-sessions --help --version"
            if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
                COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
                return 0
            fi

            # hacking -m or --model completion value that contains colon
            if [ "$cur" == ":" ] || [ "$prev" == ":" ]; then
                local option client
                if [ "$cur" = ":" ]; then
                    option="${COMP_WORDS[COMP_CWORD-2]}"
                    client="$prev"
                else
                    option="${COMP_WORDS[COMP_CWORD-3]}"
                    client="${COMP_WORDS[COMP_CWORD-2]}"
                fi
                if [ "$option" == "-m" ] || [ "$flag" == "--model" ]; then
                    if [ "$cur" == ":" ]; then
                        cur=""
                    fi
                    COMPREPLY=($(compgen -W "$("$1" --list-models | sed -n '/'"${client}"'/ s/'"${client%:*}"'://p')" -- "${cur}"))
                    return 0
                fi
            fi

            case "${prev}" in
                -m|--model)
                    COMPREPLY=($(compgen -W "$("$1" --list-models)" -- "${cur}"))
                    return 0
                    ;;
                -r|--role)
                    COMPREPLY=($(compgen -W "$("$1" --list-roles)" -- "${cur}"))
                    return 0
                    ;;
                -s|--session)
                    COMPREPLY=($(compgen -W "$("$1" --list-sessions)" -- "${cur}"))
                    return 0
                    ;;
                -f|--file)
                    local oldifs
                    if [[ -v IFS ]]; then
                        oldifs="$IFS"
                    fi
                    IFS=$'\n'
                    COMPREPLY=($(compgen -f "${cur}"))
                    if [[ -v oldifs ]]; then
                        IFS="$oldifs"
                    fi
                    if [[ "${BASH_VERSINFO[0]}" -ge 4 ]]; then
                        compopt -o filenames
                    fi
                    return 0
                    ;;
                -w|--wrap)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                *)
                    COMPREPLY=()
                    ;;
            esac
            COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
            return 0
            ;;
    esac
}

if [[ "${BASH_VERSINFO[0]}" -eq 4 && "${BASH_VERSINFO[1]}" -ge 4 || "${BASH_VERSINFO[0]}" -gt 4 ]]; then
    complete -F _aichat -o nosort -o bashdefault -o default aichat
else
    complete -F _aichat -o bashdefault -o default aichat
fi