11import argparse
2- import os
32import sqlite3
4- from snapshell .database import create_database , update_database , DB_PATH
5- from .llm_api import suggest_command , set_api_key , load_api_key
3+ from snapshell .utils import update_database , DB_PATH , print_color , clear_history , view_history
4+ from .llm_api import LLMClient
65
7- # ANSI escape codes for colors
8- RESET = "\033 [0m"
9- CYAN = "\033 [36m"
10- GREEN = "\033 [32m"
11- WHITE = "\033 [37m"
12- BLUE = "\033 [34m"
13- YELLOW = "\033 [33m"
14- RED = "\033 [31m"
15-
16- def initial_setup ():
6+ def initial_setup (llm_client ):
177 # Prompt user for GROQ API key
18- print ( GREEN + "Please enter your GROQ API key:" + RESET )
19- groq_api_key = input (YELLOW + "> " + RESET )
8+ print_color ( "Please enter your GROQ API key:" , "GREEN" )
9+ groq_api_key = input ("> " )
2010
2111 # Set the API key
22- set_api_key (groq_api_key )
12+ llm_client .set_api_key (groq_api_key )
13+
14+ print_color (f"Setting up the database... at { DB_PATH } " , "YELLOW" )
15+ llm_client .init_client ()
16+ print_color ("Database successfully created\n " , "GREEN" )
17+ print_color ("Use the tool as snapshell\n " , "GREEN" )
2318
24- print (YELLOW + f"Setting up the database... at { DB_PATH } " + RESET )
25- create_database ()
26- update_database ()
27- print (GREEN + "Database successfully created\n " + RESET )
28- print (GREEN + "Use the tool as snapshell\n " + RESET )
29-
30- def view_history ():
31- conn = sqlite3 .connect (DB_PATH )
32- cursor = conn .cursor ()
33- cursor .execute ('SELECT user_input, command, explanation, timestamp FROM command_suggestions ORDER BY timestamp DESC' )
34- results = cursor .fetchall ()
35- conn .close ()
36-
37- if not results :
38- print (f"{ YELLOW } No history found.{ RESET } " )
39- return
40-
41- print (f"{ CYAN } Command History:{ RESET } " )
42- for entry in results :
43- user_input , command , explanation , timestamp = entry
44- print (f"{ GREEN } User Input: { user_input } { RESET } " )
45- print (f"{ WHITE } Command: { command } { RESET } " )
46- print (f"{ BLUE } Explanation: { explanation } { RESET } " )
47- print (f"{ YELLOW } Timestamp: { timestamp } { RESET } " )
48- print ("-" * 40 )
4919
50- def clear_history ():
51- conn = sqlite3 .connect (DB_PATH )
52- cursor = conn .cursor ()
53- cursor .execute ('DELETE FROM command_suggestions' )
54- conn .commit ()
55- conn .close ()
56- print (f"{ GREEN } Command history cleared successfully.{ RESET } " )
5720
5821def main ():
5922 parser = argparse .ArgumentParser (description = "Auto-complete Linux commands using an LLM." )
@@ -63,23 +26,23 @@ def main():
6326 parser .add_argument ('--set-api-key' , type = str , help = "Set the GROQ API key" )
6427 args = parser .parse_args ()
6528
29+ llm_client = LLMClient ()
30+
6631 if args .set_api_key :
67- set_api_key (args .set_api_key )
68- print ( GREEN + "API key set successfully." + RESET )
32+ llm_client . set_api_key (args .set_api_key )
33+ print_color ( "API key set successfully." , "GREEN" )
6934 return
7035
7136 # Load the API key from the configuration file
72- API_KEY = load_api_key ()
73-
37+ API_KEY = llm_client .load_api_key ()
7438
7539 if not API_KEY :
76- print ("This is working" )
77- initial_setup ()
40+ initial_setup (llm_client )
7841
7942 if args .update_db :
80- print ( f" { YELLOW } Updating database...{ RESET } " )
43+ print_color ( " Updating database..." , "YELLOW " )
8144 update_database ()
82- print ( f" { GREEN } Database updated successfully.{ RESET } " )
45+ print_color ( " Database updated successfully." , "GREEN " )
8346
8447 if args .view_history :
8548 view_history ()
@@ -89,24 +52,27 @@ def main():
8952 clear_history ()
9053 return
9154
92- print (f"{ CYAN } Welcome to the Linux Command Tool. Type 'exit' to quit.{ RESET } " )
93-
55+ print_color ("Welcome to the SnapShell. Type 'exit' to quit." , "CYAN" )
56+
57+ llm_client .init_client ()
58+
9459 conversation_history = []
95-
60+
9661 while True :
97- user_input = input (f"{ CYAN } Enter your command query: { RESET } " )
62+ user_input = input (f"Enter your command query: " )
9863
9964 if user_input .lower () == 'exit' :
100- print ( f" { CYAN } Exiting the Linux Command Tool . Goodbye!{ RESET } " )
65+ print_color ( " Exiting the SnapShell . Goodbye!" , "CYAN " )
10166 break
10267
10368 try :
104- print (f"{ CYAN } Fetching command suggestion...{ RESET } " )
105- suggestion = suggest_command (user_input , conversation_history )
106- print (f"{ GREEN } Suggested Command: \n { RESET } " )
107- print (f"{ WHITE } { suggestion .command } { RESET } \n " )
108- print (f"{ BLUE } Explanation: { suggestion .explanation } { RESET } \n " )
109- print (f"{ YELLOW } Warning: This is a suggestion. Review and execute at your own risk.{ RESET } " )
69+ print_color ("Fetching command suggestion..." , "CYAN" )
70+ suggestion = llm_client .suggest_command (user_input , conversation_history )
71+ print_color ("Suggested Command:" , "GREEN" )
72+ print_color (suggestion .command , "WHITE" )
73+ print_color ("Explanation: \n " , "BLUE" )
74+ print_color (suggestion .explanation , "BLUE" )
75+ print_color ("Warning: This is a suggestion. Review and execute at your own risk." , "YELLOW" )
11076
11177 # Update conversation history
11278 conversation_history .append ({"role" : "user" , "content" : user_input })
@@ -117,8 +83,8 @@ def main():
11783 conversation_history = conversation_history [- 16 :]
11884
11985 except ValueError as e :
120- print ( f" { RED } { e } { RESET } " )
121- print ("-" * 40 ,"\n " )
86+ print_color ( str ( e ), " RED" )
87+ print ("-" * 40 , "\n " )
12288
12389if __name__ == "__main__" :
12490 main ()
0 commit comments