feat: add completion scripts (#411)

pull/412/head
sigoden 2 months ago committed by GitHub
parent 568e9a06db
commit 1c3e81afe9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,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 --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

@ -0,0 +1,18 @@
complete -c aichat -s m -l model -x -a "(aichat --list-models)" -d 'Choose a LLM model' -r
complete -c aichat -s r -l role -x -a "(aichat --list-roles)" -d 'Choose a role' -r
complete -c aichat -s s -l session -x -a"(aichat --list-sessions)" -d 'Create or reuse a session' -r
complete -c aichat -s f -l file -d 'Attach files to the message' -r -F
complete -c aichat -s w -l wrap -d 'Specify the text-wrapping mode (no, auto, <max-width>)'
complete -c aichat -l save-session -d 'Whether to save the session'
complete -c aichat -s e -l execute -d 'Execute commands using natural language'
complete -c aichat -s c -l code -d 'Generate only code'
complete -c aichat -s H -l no-highlight -d 'Disable syntax highlighting'
complete -c aichat -s S -l no-stream -d 'No stream output'
complete -c aichat -l light-theme -d 'Use light theme'
complete -c aichat -l dry-run -d 'Run in dry run mode'
complete -c aichat -l info -d 'Print related information'
complete -c aichat -l list-models -d 'List all available models'
complete -c aichat -l list-roles -d 'List all available roles'
complete -c aichat -l list-sessions -d 'List all available sessions'
complete -c aichat -s h -l help -d 'Print help'
complete -c aichat -s V -l version -d 'Print version'

@ -0,0 +1,50 @@
module completions {
def "nu-complete aichat completions" [] {
[ "bash" "zsh" "fish" "powershell" "nushell" ]
}
def "nu-complete aichat model" [] {
^aichat --list-models |
| lines
| parse "{value}"
}
def "nu-complete aichat role" [] {
^aichat --list-roles |
| lines
| parse "{value}"
}
def "nu-complete aichat session" [] {
^aichat --list-sessions |
| lines
| parse "{value}"
}
# All-in-one chat and copilot CLI that integrates 10+ AI platforms
export extern aichat [
--model(-m): string@"nu-complete aichat model" # Choose a LLM model
--role(-r): string@"nu-complete aichat role" # Choose a role
--session(-s): string@"nu-complete aichat role" # Create or reuse a session
--save-session # Whether to save the session
--execute(-e) # Execute commands using natural language
--code(-c) # Generate only code
--file(-f): string # Attach files to the message
--no-highlight(-H) # Disable syntax highlighting
--no-stream(-S) # No stream output
--wrap(-w): string # Specify the text-wrapping mode (no, auto, <max-width>)
--light-theme # Use light theme
--dry-run # Run in dry run mode
--info # Print related information
--list-models # List all available models
--list-roles # List all available roles
--list-sessions # List all available sessions
...text: string # Input text
--help(-h) # Print help
--version(-V) # Print version
]
}
export use completions *

@ -0,0 +1,79 @@
using namespace System.Management.Automation
using namespace System.Management.Automation.Language
Register-ArgumentCompleter -Native -CommandName 'aichat' -ScriptBlock {
param($wordToComplete, $commandAst, $cursorPosition)
$commandElements = $commandAst.CommandElements
$command = @(
'aichat'
for ($i = 1; $i -lt $commandElements.Count; $i++) {
$element = $commandElements[$i]
if ($element -isnot [StringConstantExpressionAst] -or
$element.StringConstantType -ne [StringConstantType]::BareWord -or
$element.Value.StartsWith('-') -or
$element.Value -eq $wordToComplete) {
break
}
$element.Value
}) -join ';'
$completions = @(switch ($command) {
'aichat' {
[CompletionResult]::new('-m', '-m', [CompletionResultType]::ParameterName, 'Choose a LLM model')
[CompletionResult]::new('--model', '--model', [CompletionResultType]::ParameterName, 'Choose a LLM model')
[CompletionResult]::new('-r', '-r', [CompletionResultType]::ParameterName, 'Choose a role')
[CompletionResult]::new('--role', '--role', [CompletionResultType]::ParameterName, 'Choose a role')
[CompletionResult]::new('-s', '-s', [CompletionResultType]::ParameterName, 'Create or reuse a session')
[CompletionResult]::new('--session', '--session', [CompletionResultType]::ParameterName, 'Create or reuse a session')
[CompletionResult]::new('-f', '-f', [CompletionResultType]::ParameterName, 'Attach files to the message')
[CompletionResult]::new('--file', '--file', [CompletionResultType]::ParameterName, 'Attach files to the message')
[CompletionResult]::new('-w', '-w', [CompletionResultType]::ParameterName, 'Specify the text-wrapping mode (no, auto, <max-width>)')
[CompletionResult]::new('--wrap', '--wrap', [CompletionResultType]::ParameterName, 'Specify the text-wrapping mode (no, auto, <max-width>)')
[CompletionResult]::new('--save-session', '--save-session', [CompletionResultType]::ParameterName, 'Whether to save the session')
[CompletionResult]::new('-e', '-e', [CompletionResultType]::ParameterName, 'Execute commands using natural language')
[CompletionResult]::new('--execute', '--execute', [CompletionResultType]::ParameterName, 'Execute commands using natural language')
[CompletionResult]::new('-c', '-c', [CompletionResultType]::ParameterName, 'Generate only code')
[CompletionResult]::new('--code', '--code', [CompletionResultType]::ParameterName, 'Generate only code')
[CompletionResult]::new('-H', '-H', [CompletionResultType]::ParameterName, 'Disable syntax highlighting')
[CompletionResult]::new('--no-highlight', '--no-highlight', [CompletionResultType]::ParameterName, 'Disable syntax highlighting')
[CompletionResult]::new('-S', '-S', [CompletionResultType]::ParameterName, 'No stream output')
[CompletionResult]::new('--no-stream', '--no-stream', [CompletionResultType]::ParameterName, 'No stream output')
[CompletionResult]::new('--light-theme', '--light-theme', [CompletionResultType]::ParameterName, 'Use light theme')
[CompletionResult]::new('--dry-run', '--dry-run', [CompletionResultType]::ParameterName, 'Run in dry run mode')
[CompletionResult]::new('--info', '--info', [CompletionResultType]::ParameterName, 'Print related information')
[CompletionResult]::new('--list-models', '--list-models', [CompletionResultType]::ParameterName, 'List all available models')
[CompletionResult]::new('--list-roles', '--list-roles', [CompletionResultType]::ParameterName, 'List all available roles')
[CompletionResult]::new('--list-sessions', '--list-sessions', [CompletionResultType]::ParameterName, 'List all available sessions')
[CompletionResult]::new('-h', '-h', [CompletionResultType]::ParameterName, 'Print help')
[CompletionResult]::new('--help', '--help', [CompletionResultType]::ParameterName, 'Print help')
[CompletionResult]::new('-V', '-V', [CompletionResultType]::ParameterName, 'Print version')
[CompletionResult]::new('--version', '--version', [CompletionResultType]::ParameterName, 'Print version')
break
}
})
function Get-AichatValues($arg) {
$(aichat $arg) -split '\n' | ForEach-Object { [CompletionResult]::new($_) }
}
if ($commandElements.Count -gt 1) {
$offset=2
if ($wordToComplete -eq "") {
$offset=1
}
$flag = $commandElements[$commandElements.Count-$offset].ToString()
if ($flag -eq "-m" -or $flag -eq "--model") {
$completions = Get-AichatValues "--list-models"
} elseif ($flag -eq "-r" -or $flag -eq "--role") {
$completions = Get-AichatValues "--list-roles"
} elseif ($flag -eq "-s" -or $flag -eq "--session") {
$completions = Get-AichatValues "--list-sessions"
} elseif ($flag -eq "-f" -or $flag -eq "--file") {
$completions = @()
}
}
$completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
Sort-Object -Property ListItemText
}

@ -0,0 +1,73 @@
#compdef aichat
autoload -U is-at-least
_aichat() {
typeset -A opt_args
typeset -a _arguments_options
local ret=1
if is-at-least 5.2; then
_arguments_options=(-s -S -C)
else
_arguments_options=(-s -C)
fi
local context curcontext="$curcontext" state line
local common=(
'-m+[Choose a LLM model]:MODEL:->models' \
'--model=[Choose a LLM model]:MODEL:->models' \
'-r+[Choose a role]:ROLE:->roles' \
'--role=[Choose a role]:ROLE:->roles' \
'-s+[Create or reuse a session]:SESSION:->sessions' \
'--session=[Create or reuse a session]:SESSION:->sessions' \
'*-f+[Attach files to the message]:FILE:_files' \
'*--file=[Attach files to the message]:FILE:_files' \
'-w+[Specify the text-wrapping mode (no, auto, <max-width>)]:WRAP: ' \
'--wrap=[Specify the text-wrapping mode (no, auto, <max-width>)]:WRAP: ' \
'--save-session[Whether to save the session]' \
'-e[Execute commands using natural language]' \
'--execute[Execute commands using natural language]' \
'-c[Generate only code]' \
'--code[Generate only code]' \
'-H[Disable syntax highlighting]' \
'--no-highlight[Disable syntax highlighting]' \
'-S[No stream output]' \
'--no-stream[No stream output]' \
'--light-theme[Use light theme]' \
'--dry-run[Run in dry run mode]' \
'--info[Print related information]' \
'--list-models[List all available models]' \
'--list-roles[List all available roles]' \
'--list-sessions[List all available sessions]' \
'-h[Print help]' \
'--help[Print help]' \
'-V[Print version]' \
'--version[Print version]' \
'*::text -- Input text:' \
)
_arguments "${_arguments_options[@]}" $common \
&& ret=0
case $state in
models|roles|sessions)
local -a values expl
values=( ${(f)"$(_call_program values aichat --list-$state)"} )
_wanted values expl $state compadd -a values && ret=0
;;
esac
return ret
}
(( $+functions[_aichat_commands] )) ||
_aichat_commands() {
local commands; commands=()
_describe -t commands 'aichat commands' commands "$@"
}
if [ "$funcstack[1]" = "_aichat" ]; then
_aichat "$@"
else
compdef _aichat aichat
fi
Loading…
Cancel
Save