Skip to content

Commit 6ac037f

Browse files
committed
Make to preserve config value if exists
1 parent ae635dc commit 6ac037f

File tree

3 files changed

+92
-12
lines changed

3 files changed

+92
-12
lines changed

aishell/cli/config_aishell.py

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,19 @@
1111

1212

1313
def config_aishell():
14-
rich.print('''Hi! 🙌 I am [bold blue]AiShell[/bold blue], [yellow]your powerful terminal assistant[/yellow] 🔥
14+
rich.print('''
15+
Hi! 🙌 I am [bold blue]AiShell[/bold blue], [yellow]your powerful terminal assistant[/yellow] 🔥
1516
I am here to assist you with configuring AiShell. 💪
1617
1718
Please make sure that you have logged into chat.openai.com on your browser before we continue. 🗝️
1819
19-
''')
20+
'''[1:])
2021
typer.confirm('Are you ready to proceed? 🚀', abort=True)
2122

22-
rich.print(f'''Which browser did you use to log in to chat.openai.com?
23+
rich.print(f'''
24+
Which browser did you use to log in to chat.openai.com?
2325
24-
We support the following browsers: [{SUPPORTED_BROWSERS}]''')
26+
We support the following browsers: [{SUPPORTED_BROWSERS}]'''[1:])
2527
browser_name = typer.prompt('Please enter your choice here: ')
2628
if browser_name not in SUPPORTED_BROWSERS:
2729
rich.print(f'Browser {browser_name} is not supported. Supported browsers are: {SUPPORTED_BROWSERS}')
@@ -33,18 +35,33 @@ def config_aishell():
3335
rich.print('Failed to get session token. 😓 Can you check if you are logged in to https://chat.openai.com?')
3436
sys.exit(1)
3537

36-
is_paid = typer.confirm("It's my last question! 🤩 Are you a PLUS user?")
38+
config_manager = save_config(session_token)
3739

38-
chatgpt_config = RevChatGPTChatbotConfigModel(session_token=session_token, paid=is_paid)
39-
aishell_config = AiShellConfigModel(chatgpt_config=chatgpt_config)
40-
config_manager = AiShellConfigManager(config_model=aishell_config)
41-
config_manager.save_config()
42-
43-
rich.print(f'''[green bold]Excellent![/green bold] You are now ready to use [bold blue]AiShell[/bold blue] 🚀
40+
rich.print(f'''
41+
[green bold]Excellent![/green bold] You are now ready to use [bold blue]AiShell[/bold blue] 🚀
4442
4543
Enjoy your AI powered terminal assistant! 🎉
4644
4745
[dim]To check your settings file, it's at: {config_manager.config_path}[/dim]
4846
49-
''')
47+
'''[1:])
48+
return config_manager
49+
50+
51+
def save_config(session_token: str):
52+
is_config_file_available = AiShellConfigManager.is_config_file_available(AiShellConfigManager.DEFAULT_CONFIG_PATH)
53+
if is_config_file_available:
54+
config_manager = AiShellConfigManager(load_config=True)
55+
is_chatgpt_config_available = config_manager.config_model.chatgpt_config is not None
56+
if is_chatgpt_config_available:
57+
assert config_manager.config_model.chatgpt_config # for type hinting
58+
config_manager.config_model.chatgpt_config.session_token = session_token
59+
else:
60+
config_manager.config_model.chatgpt_config = RevChatGPTChatbotConfigModel(session_token=session_token)
61+
else:
62+
chatgpt_config = RevChatGPTChatbotConfigModel(session_token=session_token)
63+
aishell_config = AiShellConfigModel(chatgpt_config=chatgpt_config)
64+
config_manager = AiShellConfigManager(config_model=aishell_config)
65+
66+
config_manager.save_config()
5067
return config_manager

aishell/cli/test/__init__.py

Whitespace-only changes.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from unittest.mock import MagicMock, patch
2+
3+
from aishell.cli.config_aishell import save_config
4+
from aishell.models import AiShellConfigModel, LanguageModel, RevChatGPTChatbotConfigModel
5+
from aishell.utils import AiShellConfigManager
6+
7+
8+
class TestSaveConfig:
9+
10+
@patch('aishell.cli.config_aishell.AiShellConfigManager', spec=AiShellConfigManager)
11+
def test_success_when_config_file_not_available(
12+
self,
13+
mocked_config_manager_class: MagicMock,
14+
):
15+
'''config file 이 없는 경우 테스트 성공해야 한다'''
16+
# given
17+
mocked_config_manager_class.is_config_file_available.return_value = False
18+
19+
# when
20+
save_config('valid_session_token')
21+
22+
# then
23+
mocked_config_manager_class.return_value.save_config.assert_called_once()
24+
25+
@patch('aishell.cli.config_aishell.AiShellConfigManager', spec=AiShellConfigManager)
26+
def test_success_when_config_file_available_with_chatgpt_config(
27+
self,
28+
mocked_config_manager_class: MagicMock,
29+
):
30+
'''config file 이 있고, chatgpt_config 가 있는 경우 테스트 성공해야 한다.'''
31+
# given
32+
mocked_config_manager_class.is_config_file_available.return_value = True
33+
mocked_config_manager_instance = mocked_config_manager_class.return_value
34+
mocked_config_manager_instance.config_model =\
35+
AiShellConfigModel(chatgpt_config=RevChatGPTChatbotConfigModel(session_token='invalid_session_token'))
36+
37+
# when
38+
save_config('valid_session_token')
39+
40+
# then
41+
mocked_config_manager_class.return_value.save_config.assert_called_once()
42+
43+
@patch('aishell.cli.config_aishell.AiShellConfigManager', spec=AiShellConfigManager)
44+
def test_success_when_config_file_available_without_chatgpt_config(
45+
self,
46+
mocked_config_manager_class: MagicMock,
47+
):
48+
'''config file 이 있고, chatgpt_config 가 없는 경우 테스트 성공해야 한다.'''
49+
# given
50+
mocked_config_manager_class.is_config_file_available.return_value = True
51+
mocked_config_manager_instance = mocked_config_manager_class.return_value
52+
mocked_config_manager_instance.config_model =\
53+
AiShellConfigModel(language_model=LanguageModel.GPT3, openai_api_key='valid_openai_api_key')
54+
55+
# when
56+
save_config('valid_session_token')
57+
58+
# then
59+
mocked_config_manager_class.return_value.save_config.assert_called_once()
60+
61+
# when config file available - with chatgpt_config
62+
# when config file available - without chatgpt_config
63+
# when config file not available

0 commit comments

Comments
 (0)