66
77try :
88 import readline
9+
910 COMMANDS = ["/login" , "/help" , "/copy" , "/model" , "/search_provider" , ".exit" ]
11+
1012 def completer (text , state ):
1113 matches = [c for c in COMMANDS if c .startswith (text )]
1214 return matches [state ] if state < len (matches ) else None
15+
1316 readline .set_completer (completer )
1417 readline .parse_and_bind ("tab: complete" )
1518except ImportError :
@@ -31,8 +34,6 @@ def completer(text, state):
3134 (".exit" , "Quit" ),
3235]
3336
34- SEARCH_PROVIDERS = ["duckduckgo" ]
35-
3637CONFIG_PATH = Path .home () / ".config" / "mini-copilot" / "config.json"
3738TOKEN_REFRESH_INTERVAL = 24 * 60 # seconds
3839
@@ -60,11 +61,14 @@ def completer(text, state):
6061}
6162TOOLS = [WEB_SEARCH_TOOL ]
6263
64+
6365def load_github_token ():
64- if not CONFIG_PATH .exists (): return None
66+ if not CONFIG_PATH .exists ():
67+ return None
6568 config = json .loads (CONFIG_PATH .read_text ())
6669 return config .get ("github_token" )
6770
71+
6872def main ():
6973 github_token = load_github_token ()
7074 copilot_token = None
@@ -93,33 +97,43 @@ def main():
9397 try :
9498 user_input = input ("> " ).strip ()
9599 except (EOFError , KeyboardInterrupt ):
96- print ("\n Goodbye!" ); break
100+ print ("\n Goodbye!" )
101+ break
97102
98- if not user_input : continue
103+ if not user_input :
104+ continue
99105 if user_input in ("/" , "/help" ):
100106 print ("\n Available commands:" )
101107 for cmd , desc in COMMANDS_HELP :
102108 print (f" { cmd :<20} { desc } " )
103- print (); continue
104- if user_input == ".exit" : print ("Goodbye!" ); break
109+ print ()
110+ continue
111+ if user_input == ".exit" :
112+ print ("Goodbye!" )
113+ break
105114 if user_input == "/copy" :
106- handle_copy_command (last_reply ); continue
115+ handle_copy_command (last_reply )
116+ continue
107117 if user_input == "/model" :
108- current_model = handle_model_command (copilot_token , current_model ); continue
118+ current_model = handle_model_command (copilot_token , current_model )
119+ continue
109120 if user_input == "/search_provider" :
110- search_provider = handle_search_provider_command (search_provider ); continue
121+ search_provider = handle_search_provider_command (search_provider )
122+ continue
111123 if user_input == "/login" :
112124 github_token = handle_login_command (CONFIG_PATH , TOKEN_REFRESH_INTERVAL )
113125 if github_token :
114126 try :
115127 copilot_token = get_copilot_token (github_token )
116128 token_expiry = time .monotonic () + TOKEN_REFRESH_INTERVAL
117129 print ("Connected to GitHub Copilot.\n " )
118- except Exception as e : print (f"Error: { e } " , file = sys .stderr )
130+ except Exception as e :
131+ print (f"Error: { e } " , file = sys .stderr )
119132 continue
120133
121134 if not copilot_token :
122- print ("Not authenticated. Type /login first." , file = sys .stderr ); continue
135+ print ("Not authenticated. Type /login first." , file = sys .stderr )
136+ continue
123137
124138 try :
125139 if time .monotonic () >= token_expiry :
@@ -128,32 +142,42 @@ def main():
128142
129143 messages .append ({"role" : "user" , "content" : user_input })
130144 response_message = chat (messages , copilot_token , current_model , tools = TOOLS )
131-
145+
132146 while response_message .get ("tool_calls" ):
133147 messages .append (response_message )
134148 for tool_call in response_message ["tool_calls" ]:
135149 function_name = tool_call ["function" ]["name" ]
136150 function_args = json .loads (tool_call ["function" ]["arguments" ])
137-
151+
138152 if function_name == "web_search" :
139153 search_query = function_args .get ("query" )
140154 num_results = function_args .get ("num_results" , 20 )
141-
142- search_context = web_search (search_query , num_results = num_results )
143-
144- messages .append ({
145- "tool_call_id" : tool_call ["id" ],
146- "role" : "tool" ,
147- "name" : function_name ,
148- "content" : search_context ,
149- })
150- response_message = chat (messages , copilot_token , current_model , tools = TOOLS )
155+
156+ search_context = web_search (
157+ search_query ,
158+ num_results = num_results ,
159+ provider = search_provider ,
160+ )
161+
162+ messages .append (
163+ {
164+ "tool_call_id" : tool_call ["id" ],
165+ "role" : "tool" ,
166+ "name" : function_name ,
167+ "content" : search_context ,
168+ }
169+ )
170+ response_message = chat (
171+ messages , copilot_token , current_model , tools = TOOLS
172+ )
151173
152174 reply = response_message ["content" ]
153175 messages .append ({"role" : "assistant" , "content" : reply })
154176 last_reply = reply
155177 print (f"\n { reply } \n " )
156- except Exception as e : print (f"Error: { e } " , file = sys .stderr )
178+ except Exception as e :
179+ print (f"Error: { e } " , file = sys .stderr )
180+
157181
158182if __name__ == "__main__" :
159183 main ()
0 commit comments