11"""
22This module contains the main function for the DevChat CLI.
33"""
4- import os
5- from typing import Optional
4+ from contextlib import contextmanager
65import json
6+ import os
77import sys
8- from contextlib import contextmanager
8+ from typing import Optional , Tuple
99import rich_click as click
1010from devchat .store import Store
1111from devchat .openai import OpenAIChatConfig , OpenAIChat
@@ -31,7 +31,12 @@ def handle_errors():
3131 sys .exit (os .EX_SOFTWARE )
3232
3333
34- def load_config_data (chat_dir : str ) -> dict :
34+ def init_dir () -> Tuple [dict , Store ]:
35+ git_root = find_git_root ()
36+ chat_dir = os .path .join (git_root , ".chat" )
37+ if not os .path .exists (chat_dir ):
38+ os .makedirs (chat_dir )
39+
3540 default_config_data = {
3641 'llm' : 'OpenAI' ,
3742 'OpenAI' : {
@@ -46,7 +51,9 @@ def load_config_data(chat_dir: str) -> dict:
4651 except FileNotFoundError :
4752 config_data = default_config_data
4853
49- return config_data
54+ store = Store (chat_dir )
55+ git_ignore (git_root , store .graph_path , store .db_path )
56+ return config_data , store
5057
5158
5259@main .command ()
@@ -120,12 +127,7 @@ def prompt(content: Optional[str], parent: Optional[str], reference: Optional[st
120127 ```
121128
122129 """
123- git_root = find_git_root ()
124- chat_dir = os .path .join (git_root , ".chat" )
125- if not os .path .exists (chat_dir ):
126- os .makedirs (chat_dir )
127-
128- config_data = load_config_data (chat_dir )
130+ config , store = init_dir ()
129131
130132 with handle_errors ():
131133 if content is None :
@@ -137,13 +139,9 @@ def prompt(content: Optional[str], parent: Optional[str], reference: Optional[st
137139 instruct_contents = parse_files (instruct )
138140 context_contents = parse_files (context )
139141
140- store = Store (chat_dir )
141- git_ignore (git_root , store .graph_path )
142- git_ignore (git_root , store .db_path )
143-
144- llm = config_data .get ('llm' )
142+ llm = config .get ('llm' )
145143 if llm == 'OpenAI' :
146- openai_config = OpenAIChatConfig (** config_data ['OpenAI' ])
144+ openai_config = OpenAIChatConfig (** config ['OpenAI' ])
147145 chat = OpenAIChat (openai_config )
148146
149147 openai_asisstant = Assistant (chat , store )
@@ -164,12 +162,8 @@ def log(skip, max_count):
164162 """
165163 Show the prompt history.
166164 """
167- git_root = find_git_root ()
168- chat_dir = os .path .join (git_root , ".chat" )
169- if not os .path .exists (chat_dir ):
170- os .makedirs (chat_dir )
165+ _ , store = init_dir ()
171166
172- store = Store (chat_dir )
173167 recent_prompts = store .select_recent (skip , skip + max_count )
174168
175169 for record in recent_prompts :
0 commit comments