88from prompt_toolkit import PromptSession
99from prompt_toolkit .history import FileHistory
1010from prompt_toolkit .styles import Style
11+ from prompt_toolkit .completion import WordCompleter
1112
1213from .aipy import Agent
1314from .aipy .i18n import T
1718class InteractiveConsole ():
1819 def __init__ (self , ai , console , settings ):
1920 self .ai = ai
21+ self .llms = ai .llm .names
22+ completer = WordCompleter (['/use' , 'use' , '/done' ,'done' ] + list (self .llms ['available' ]), ignore_case = True )
2023 self .history = FileHistory (str (Path .cwd () / settings .history ))
21- self .session = PromptSession (history = self .history )
24+ self .session = PromptSession (history = self .history , completer = completer )
2225 self .console = console
2326 self .style_main = Style .from_dict ({"prompt" : "green" })
2427 self .style_ai = Style .from_dict ({"prompt" : "cyan" })
25-
28+
2629 def input_with_possible_multiline (self , prompt_text , is_ai = False ):
2730 prompt_style = self .style_ai if is_ai else self .style_main
2831
@@ -57,19 +60,17 @@ def run_ai_mode(self, initial_text):
5760 user_input = self .input_with_possible_multiline (">>> " , is_ai = True ).strip ()
5861 except (EOFError , KeyboardInterrupt ):
5962 break
63+
6064 if not user_input :
6165 continue
62-
63- if user_input .startswith ("/" ):
64- if user_input .startswith ("/done" ):
65- break
66- elif user_input .startswith ("/use " ):
67- llm = user_input [5 :].strip ()
68- if llm : ai .use (llm )
69- else :
70- self .console .print (f"{ T ('ai_mode_unknown_command' )} " , style = "cyan" )
66+ if user_input in ('/done' , 'done' ):
67+ break
68+ name = self .parse_use_command (user_input , ai .llm .names ['available' ])
69+ if name != None :
70+ if name : ai .use (name )
7171 else :
7272 self .run_ai_task (user_input )
73+
7374 try :
7475 ai .publish (verbose = False )
7576 except Exception as e :
@@ -81,6 +82,14 @@ def run_ai_mode(self, initial_text):
8182 pass
8283 self .console .print (f"{ T ('ai_mode_exit' )} " , style = "cyan" )
8384
85+ def parse_use_command (self , user_input , llms ):
86+ words = user_input .split ()
87+ if len (words ) > 2 :
88+ return None
89+ if words [0 ] in ('/use' , 'use' ):
90+ return words [1 ] if len (words ) > 1 else ''
91+ return words [0 ] if len (words ) == 1 and words [0 ] in llms else None
92+
8493 def run (self ):
8594 names = self .ai .llm .names
8695 self .console .print (f"{ T ('banner1' )} " , style = "green" )
@@ -90,9 +99,9 @@ def run(self):
9099 user_input = self .input_with_possible_multiline (">> " ).strip ()
91100 if len (user_input ) < 2 :
92101 continue
93- if user_input . startswith ( "/use " ):
94- llm = user_input [ 5 :]. strip ()
95- if llm : self .ai .use (llm )
102+ name = self . parse_use_command ( user_input , names [ 'available' ])
103+ if name != None :
104+ if name : self .ai .use (name )
96105 else :
97106 self .run_ai_mode (user_input )
98107 except (EOFError , KeyboardInterrupt ):
0 commit comments