From 556819b8245133ebd9d6cbe77358f20df4cc9ca2 Mon Sep 17 00:00:00 2001 From: Martin Bielik Date: Fri, 6 Dec 2024 20:48:05 +0100 Subject: allow override global token config --- README.md | 6 ++++++ autoload/vim_ai_config.vim | 3 +++ doc/vim-ai.txt | 3 +++ py/utils.py | 11 +++++++---- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f0ec13f..d321b35 100644 --- a/README.md +++ b/README.md @@ -293,6 +293,7 @@ END " - options.initial_prompt: prompt prepended to every chat request (list of lines or string) " - options.request_timeout: request timeout in seconds " - options.enable_auth: enable authorization using openai key +" - options.token_file_path: override global token configuration " - options.selection_boundary: selection prompt wrapper (eliminates empty responses, see #20) " - ui.paste_mode: use paste mode (see more info in the Notes below) let g:vim_ai_complete = { @@ -306,6 +307,7 @@ let g:vim_ai_complete = { \ "request_timeout": 20, \ "stream": 1, \ "enable_auth": 1, +\ "token_file_path": "", \ "selection_boundary": "#####", \ "initial_prompt": s:initial_complete_prompt, \ }, @@ -320,6 +322,7 @@ let g:vim_ai_complete = { " - options.initial_prompt: prompt prepended to every chat request (list of lines or string) " - options.request_timeout: request timeout in seconds " - options.enable_auth: enable authorization using openai key +" - options.token_file_path: override global token configuration " - options.selection_boundary: selection prompt wrapper (eliminates empty responses, see #20) " - ui.paste_mode: use paste mode (see more info in the Notes below) let g:vim_ai_edit = { @@ -333,6 +336,7 @@ let g:vim_ai_edit = { \ "request_timeout": 20, \ "stream": 1, \ "enable_auth": 1, +\ "token_file_path": "", \ "selection_boundary": "#####", \ "initial_prompt": s:initial_complete_prompt, \ }, @@ -354,6 +358,7 @@ END " - options.initial_prompt: prompt prepended to every chat request (list of lines or string) " - options.request_timeout: request timeout in seconds " - options.enable_auth: enable authorization using openai key +" - options.token_file_path: override global token configuration " - options.selection_boundary: selection prompt wrapper (eliminates empty responses, see #20) " - ui.populate_options: put [chat-options] to the chat header " - ui.open_chat_command: preset (preset_below, preset_tab, preset_right) or a custom command @@ -369,6 +374,7 @@ let g:vim_ai_chat = { \ "request_timeout": 20, \ "stream": 1, \ "enable_auth": 1, +\ "token_file_path": "", \ "selection_boundary": "", \ "initial_prompt": s:initial_chat_prompt, \ }, diff --git a/autoload/vim_ai_config.vim b/autoload/vim_ai_config.vim index 991b320..3e63e70 100644 --- a/autoload/vim_ai_config.vim +++ b/autoload/vim_ai_config.vim @@ -19,6 +19,7 @@ let g:vim_ai_complete_default = { \ "request_timeout": 20, \ "stream": 1, \ "enable_auth": 1, +\ "token_file_path": "", \ "selection_boundary": "#####", \ "initial_prompt": s:initial_complete_prompt, \ }, @@ -37,6 +38,7 @@ let g:vim_ai_edit_default = { \ "request_timeout": 20, \ "stream": 1, \ "enable_auth": 1, +\ "token_file_path": "", \ "selection_boundary": "#####", \ "initial_prompt": s:initial_complete_prompt, \ }, @@ -61,6 +63,7 @@ let g:vim_ai_chat_default = { \ "request_timeout": 20, \ "stream": 1, \ "enable_auth": 1, +\ "token_file_path": "", \ "selection_boundary": "", \ "initial_prompt": s:initial_chat_prompt, \ }, diff --git a/doc/vim-ai.txt b/doc/vim-ai.txt index 273f074..61bf1af 100644 --- a/doc/vim-ai.txt +++ b/doc/vim-ai.txt @@ -41,6 +41,7 @@ Options: > \ "request_timeout": 20, \ "stream": 1, \ "enable_auth": 1, + \ "token_file_path": "", \ "selection_boundary": "#####", \ "initial_prompt": s:initial_complete_prompt, \ }, @@ -79,6 +80,7 @@ Options: > \ "request_timeout": 20, \ "stream": 1, \ "enable_auth": 1, + \ "token_file_path": "", \ "selection_boundary": "#####", \ "initial_prompt": s:initial_complete_prompt, \ }, @@ -114,6 +116,7 @@ Options: > \ "request_timeout": 20, \ "stream": 1, \ "enable_auth": 1, + \ "token_file_path": "", \ "selection_boundary": "", \ "initial_prompt": s:initial_chat_prompt, \ }, diff --git a/py/utils.py b/py/utils.py index 3382e59..d3b7c8d 100644 --- a/py/utils.py +++ b/py/utils.py @@ -19,11 +19,13 @@ debug_log_file = vim.eval("g:vim_ai_debug_log_file") class KnownError(Exception): pass -def load_api_key(): - config_file_path = os.path.expanduser(vim.eval("g:vim_ai_token_file_path")) +def load_api_key(config_token_file_path): + # token precedence: config file path, global file path, env variable + global_token_file_path = vim.eval("g:vim_ai_token_file_path") api_key_param_value = os.getenv("OPENAI_API_KEY") try: - with open(config_file_path, 'r') as file: + token_file_path = config_token_file_path or global_token_file_path + with open(os.path.expanduser(token_file_path), 'r') as file: api_key_param_value = file.read() except Exception: pass @@ -67,6 +69,7 @@ def make_http_options(options): return { 'request_timeout': float(options['request_timeout']), 'enable_auth': bool(int(options['enable_auth'])), + 'token_file_path': options['token_file_path'], } # During text manipulation in Vim's visual mode, we utilize "normal! c" command. This command deletes the highlighted text, @@ -212,7 +215,7 @@ def openai_request(url, data, options): "Content-Type": "application/json", } if enable_auth: - (OPENAI_API_KEY, OPENAI_ORG_ID) = load_api_key() + (OPENAI_API_KEY, OPENAI_ORG_ID) = load_api_key(options['token_file_path']) headers['Authorization'] = f"Bearer {OPENAI_API_KEY}" if OPENAI_ORG_ID is not None: -- cgit v1.2.3