From 8402371e6af33dcf6962ffbc8cd73be0e36f6812 Mon Sep 17 00:00:00 2001 From: Martin Bielik Date: Tue, 17 Dec 2024 18:43:51 +0100 Subject: introduced pre-defined default roles --- py/context.py | 7 +------ py/roles.py | 8 +------- py/utils.py | 11 +++++++++++ roles-default.ini | 10 ++++++++++ tests/context_test.py | 14 ++++++++++++++ tests/mocks/vim.py | 4 +++- tests/roles_test.py | 4 ++++ 7 files changed, 44 insertions(+), 14 deletions(-) create mode 100644 roles-default.ini diff --git a/py/context.py b/py/context.py index 50fa170..2644bae 100644 --- a/py/context.py +++ b/py/context.py @@ -74,12 +74,7 @@ def parse_role_section(role): return result def load_role_config(role): - roles_config_path = os.path.expanduser(vim.eval("g:vim_ai_roles_config_file")) - if not os.path.exists(roles_config_path): - raise Exception(f"Role config file does not exist: {roles_config_path}") - - roles = configparser.ConfigParser() - roles.read(roles_config_path) + roles = read_role_files() roles = dict(roles) enhance_roles_with_custom_function(roles) diff --git a/py/roles.py b/py/roles.py index 692ac6c..94bcdb4 100644 --- a/py/roles.py +++ b/py/roles.py @@ -7,13 +7,7 @@ if "PYTEST_VERSION" in os.environ: roles_py_imported = True def load_ai_role_names(command_type): - roles_config_path = os.path.expanduser(vim.eval("g:vim_ai_roles_config_file")) - if not os.path.exists(roles_config_path): - raise Exception(f"Role config file does not exist: {roles_config_path}") - - roles = configparser.ConfigParser() - roles.read(roles_config_path) - + roles = read_role_files() enhance_roles_with_custom_function(roles) role_names = set() diff --git a/py/utils.py b/py/utils.py index ce95ed6..89bef3e 100644 --- a/py/utils.py +++ b/py/utils.py @@ -317,3 +317,14 @@ def make_chat_text_chunks(messages, config_options): map_chunk = map_chunk_stream if openai_options['stream'] else map_chunk_no_stream return map(map_chunk, response) + +def read_role_files(): + plugin_root = vim.eval("s:plugin_root") + default_roles_config_path = str(os.path.join(plugin_root, "roles-default.ini")) + roles_config_path = os.path.expanduser(vim.eval("g:vim_ai_roles_config_file")) + if not os.path.exists(roles_config_path): + raise Exception(f"Role config file does not exist: {roles_config_path}") + + roles = configparser.ConfigParser() + roles.read([default_roles_config_path, roles_config_path]) + return roles diff --git a/roles-default.ini b/roles-default.ini new file mode 100644 index 0000000..1b653f6 --- /dev/null +++ b/roles-default.ini @@ -0,0 +1,10 @@ +# predefined roles + +[right.chat] +ui.open_chat_command = preset_right + +[below.chat] +ui.open_chat_command = preset_below + +[tab.chat] +ui.open_chat_command = preset_tab diff --git a/tests/context_test.py b/tests/context_test.py index 9f7d004..1cc0330 100644 --- a/tests/context_test.py +++ b/tests/context_test.py @@ -126,6 +126,20 @@ def test_chat_only_role(): actual_config = context['config'] assert 'preset_tab' == actual_config['options']['open_chat_command'] +def test_default_roles(): + base = { + 'config_default': default_config, + 'config_extension': {}, + 'user_instruction': '/chat-only-role', + 'user_selection': '', + 'command_type': 'chat', + } + context = make_ai_context({ **base, 'user_instruction': '/right hello world!' }) + assert 'preset_right' == context['config']['ui']['open_chat_command'] + + context = make_ai_context({ **base, 'user_instruction': '/tab' }) + assert 'preset_tab' == context['config']['ui']['open_chat_command'] + def test_user_prompt(): assert 'fix grammar: helo word' == make_prompt( '', 'fix grammar: helo word', '', '') assert 'fix grammar:\nhelo word' == make_prompt( '', 'fix grammar', 'helo word', '') diff --git a/tests/mocks/vim.py b/tests/mocks/vim.py index c0e01e9..9dd8860 100644 --- a/tests/mocks/vim.py +++ b/tests/mocks/vim.py @@ -7,7 +7,9 @@ def eval(cmd): case 'g:vim_ai_debug_log_file': return '/tmp/vim_ai_debug.log' case 'g:vim_ai_roles_config_file': - return os.path.join(dirname, '..', 'resources/roles.ini') + return os.path.join(dirname, '../resources/roles.ini') + case 's:plugin_root': + return os.path.join(dirname, '../..') case _: return None diff --git a/tests/roles_test.py b/tests/roles_test.py index ac5525d..da9b9a4 100644 --- a/tests/roles_test.py +++ b/tests/roles_test.py @@ -17,4 +17,8 @@ def test_role_chat_only(): 'chat-only-role', 'deprecated-test-role-simple', 'deprecated-test-role', + # default roles + 'right', + 'below', + 'tab', } -- cgit v1.2.3