Skip to content

Commit

Permalink
introduced pre-defined default roles
Browse files Browse the repository at this point in the history
  • Loading branch information
madox2 committed Dec 17, 2024
1 parent 44625c9 commit 8402371
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 14 deletions.
7 changes: 1 addition & 6 deletions py/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 1 addition & 7 deletions py/roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@
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()
Expand Down
11 changes: 11 additions & 0 deletions py/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,14 @@ def map_chunk_stream(resp):
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
10 changes: 10 additions & 0 deletions roles-default.ini
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions tests/context_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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', '')
Expand Down
4 changes: 3 additions & 1 deletion tests/mocks/vim.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions tests/roles_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ def test_role_chat_only():
'chat-only-role',
'deprecated-test-role-simple',
'deprecated-test-role',
# default roles
'right',
'below',
'tab',
}

0 comments on commit 8402371

Please sign in to comment.