From 28f602902e2b437ce52871e65057d11891ee7f77 Mon Sep 17 00:00:00 2001 From: Johannes Bittner Date: Mon, 4 Sep 2023 13:48:19 +0200 Subject: [PATCH 1/6] Add completion script for chatblade --- src/_chatblade | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/_chatblade diff --git a/src/_chatblade b/src/_chatblade new file mode 100644 index 0000000..ac76a62 --- /dev/null +++ b/src/_chatblade @@ -0,0 +1,57 @@ +#compdef chatblade +# ------------------------------------------------------------------------------ +# Description +# ----------- +# +# Completion script for chatblade (https://github.com/npiv/chatblade) +# +# ------------------------------------------------------------------------------ +# Authors +# ------- +# +# * Johannes Bittner (https://github.com/johannes87) +# +# ------------------------------------------------------------------------------ + +_chatblade() { + local -a args + + args+=( + '(-h --help)'{-h,--help}'[show this help message and exit]' + '--openai-api-key[the OpenAI API key can also be set as env variable OPENAI_API_KEY]' + '--temperature[temperature (openai setting)]' + '(-c --chat-gpt)'{-c,--chat-gpt}'[chat GPT model 3.5/4 shorthand or full qualified model name, can also be set via e + variable OPENAI_API_MODEL]' + '(-i --interactive)'{-i,--interactive}'[start an interactive chat session. This will implicitly continue the + conversation]' + '(-s --stream)'{-s,--stream}'[Stream the incoming text to the terminal]' + '(-t --tokens)'{-t,--tokens}'[display what *would* be sent, how many tokens, and estimated costs]' + '(-p --prompt-file)'{-p,--prompt-file}'[prompt name - will load the prompt with that name at ~/.config/chatblade/nam + or a path to a file]:prompt file:_files' + '(-e --extract)'{-e,--extract}'[extract content from response if possible (either json or code block)]' + '(-r --raw)'{-r,--raw}'[print session as pure text, dont pretty print or format]' + '(-n --no-format)'{-n,--no-format}'[do not add pretty print formatting to output]' + '(-o --only)'{-o,--only}'[Only display the response, omit query]' + '--theme[Set the theme for syntax highlighting see https://pygments.org/styles/, can also be set with + CHATBLADE_THEME]:theme:_values "theme" default emacs friendly manni monokai' + '(-l --last)'{-l,--last}'[alias for -S last, the default session if none is specified]' + '(-S --session)'{-S,--session}'[initiate or continue named session]:session:->sessions' + '--session-list[list sessions]' + '--session-path[show path to session file]' + '--session-dump[dump session to stdout]' + '--session-delete[delete session]' + '--session-rename[rename session]:new session:->sessions' + ) + + case $state in + (sessions) + local -a sessions + sessions=("${(@f)$(chatblade --session-list)}") + _describe 'session' sessions + ;; + esac + + _arguments -s $args +} + +_chatblade From facf9182501c09f2955fa69b924b4e55b872a4a9 Mon Sep 17 00:00:00 2001 From: Johannes Bittner Date: Fri, 8 Sep 2023 00:41:23 +0200 Subject: [PATCH 2/6] Apply suggestions from code review Co-authored-by: Shohei YOSHIDA --- src/_chatblade | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/_chatblade b/src/_chatblade index ac76a62..682e592 100644 --- a/src/_chatblade +++ b/src/_chatblade @@ -17,23 +17,19 @@ _chatblade() { local -a args args+=( - '(-h --help)'{-h,--help}'[show this help message and exit]' - '--openai-api-key[the OpenAI API key can also be set as env variable OPENAI_API_KEY]' - '--temperature[temperature (openai setting)]' - '(-c --chat-gpt)'{-c,--chat-gpt}'[chat GPT model 3.5/4 shorthand or full qualified model name, can also be set via e - variable OPENAI_API_MODEL]' - '(-i --interactive)'{-i,--interactive}'[start an interactive chat session. This will implicitly continue the - conversation]' + '(- *)'{-h,--help}'[show this help message and exit]' + '--openai-api-key[the OpenAI API key can also be set as env variable OPENAI_API_KEY]:key' + '--temperature[temperature (openai setting)]:temperature' + '(-c --chat-gpt)'{-c,--chat-gpt}'[chat GPT model 3.5/4 shorthand or full qualified model name, can also be set via env variable OPENAI_API_MODEL]:chat GPT model:(3.5 4)' + '(-i --interactive)'{-i,--interactive}'[start an interactive chat session. This will implicitly continue the conversation]' '(-s --stream)'{-s,--stream}'[Stream the incoming text to the terminal]' '(-t --tokens)'{-t,--tokens}'[display what *would* be sent, how many tokens, and estimated costs]' - '(-p --prompt-file)'{-p,--prompt-file}'[prompt name - will load the prompt with that name at ~/.config/chatblade/nam - or a path to a file]:prompt file:_files' + '(-p --prompt-file)'{-p,--prompt-file}'[prompt name - will load the prompt with that name at ~/.config/chatblade/name or a path to a file]:prompt file:_files' '(-e --extract)'{-e,--extract}'[extract content from response if possible (either json or code block)]' '(-r --raw)'{-r,--raw}'[print session as pure text, dont pretty print or format]' '(-n --no-format)'{-n,--no-format}'[do not add pretty print formatting to output]' '(-o --only)'{-o,--only}'[Only display the response, omit query]' - '--theme[Set the theme for syntax highlighting see https://pygments.org/styles/, can also be set with - CHATBLADE_THEME]:theme:_values "theme" default emacs friendly manni monokai' + '--theme[Set the theme for syntax highlighting see https://pygments.org/styles/, can also be set with CHATBLADE_THEME]:theme:_values "theme" default emacs friendly manni monokai' '(-l --last)'{-l,--last}'[alias for -S last, the default session if none is specified]' '(-S --session)'{-S,--session}'[initiate or continue named session]:session:->sessions' '--session-list[list sessions]' @@ -55,3 +51,11 @@ _chatblade() { } _chatblade + +# Local Variables: +# mode: Shell-Script +# sh-indentation: 2 +# indent-tabs-mode: nil +# sh-basic-offset: 2 +# End: +# vim: ft=zsh sw=2 ts=2 et From 7e9e14517dcf3a553a0c3e0ca93880863ee66abf Mon Sep 17 00:00:00 2001 From: Johannes Bittner Date: Sun, 17 Sep 2023 12:43:32 +0200 Subject: [PATCH 3/6] remove trailing spaces --- src/_chatblade | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/_chatblade b/src/_chatblade index ac76a62..222f62d 100644 --- a/src/_chatblade +++ b/src/_chatblade @@ -9,13 +9,13 @@ # Authors # ------- # -# * Johannes Bittner (https://github.com/johannes87) +# * Johannes Bittner (https://github.com/johannes87) # # ------------------------------------------------------------------------------ _chatblade() { local -a args - + args+=( '(-h --help)'{-h,--help}'[show this help message and exit]' '--openai-api-key[the OpenAI API key can also be set as env variable OPENAI_API_KEY]' @@ -42,7 +42,7 @@ _chatblade() { '--session-delete[delete session]' '--session-rename[rename session]:new session:->sessions' ) - + case $state in (sessions) local -a sessions @@ -50,7 +50,7 @@ _chatblade() { _describe 'session' sessions ;; esac - + _arguments -s $args } From fb7078044b0603e1ab1ad9753eb55798d60a99e3 Mon Sep 17 00:00:00 2001 From: Johannes Bittner Date: Sun, 17 Sep 2023 12:44:31 +0200 Subject: [PATCH 4/6] prevent global variables from overwriting Co-authored-by: Shohei YOSHIDA --- src/_chatblade | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/_chatblade b/src/_chatblade index 4c0fd7b..a6d9210 100644 --- a/src/_chatblade +++ b/src/_chatblade @@ -15,6 +15,8 @@ _chatblade() { local -a args + local context state state_descr line + typeset -A opt_args args+=( '(- *)'{-h,--help}'[show this help message and exit]' From 0d26ab80ed4316f22c90787a80d26c662f97ac67 Mon Sep 17 00:00:00 2001 From: Johannes Bittner Date: Sun, 17 Sep 2023 13:07:10 +0200 Subject: [PATCH 5/6] "local state" breaks --session completion --- src/_chatblade | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/_chatblade b/src/_chatblade index a6d9210..c2bd4e6 100644 --- a/src/_chatblade +++ b/src/_chatblade @@ -15,7 +15,7 @@ _chatblade() { local -a args - local context state state_descr line + local context state_descr line typeset -A opt_args args+=( From c118ed452da2915102e35e33e629bbe2f163b6eb Mon Sep 17 00:00:00 2001 From: Johannes Bittner Date: Sun, 17 Sep 2023 13:15:19 +0200 Subject: [PATCH 6/6] implement suggestions --- src/_chatblade | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/_chatblade b/src/_chatblade index c2bd4e6..62d12e6 100644 --- a/src/_chatblade +++ b/src/_chatblade @@ -15,7 +15,7 @@ _chatblade() { local -a args - local context state_descr line + local context state state_descr line typeset -A opt_args args+=( @@ -41,6 +41,8 @@ _chatblade() { '--session-rename[rename session]:new session:->sessions' ) + _arguments -s $args + case $state in (sessions) local -a sessions @@ -48,8 +50,6 @@ _chatblade() { _describe 'session' sessions ;; esac - - _arguments -s $args } _chatblade