1010from PIL import Image
1111from io import BytesIO
1212
13- from .. import terminal , attachments , dialog , models
13+ from .. import terminal , attachments , dialog , models , chat , message
1414from ...constants import data_dir , REMBG_MODELS
1515from ...sql_manager import generate_uuid , Instance as SQL
1616
17+ class ToolRunDialog (Adw .Dialog ):
18+ __gtype_name__ = 'AlpacaToolRunDialog'
19+
20+ def __init__ (self , tool ):
21+ self .tool = tool
22+ self .main_stack = Gtk .Stack (
23+ transition_type = 1
24+ )
25+ pp = Adw .PreferencesPage (valign = 3 )
26+ self .main_stack .add_named (pp , 'arguments' )
27+ tool_properties = self .tool .tool_metadata .get ('parameters' , {}).get ('properties' , {})
28+ self .parameter_elements = []
29+ if len (tool_properties ) > 0 :
30+ factory = Gtk .SignalListItemFactory ()
31+ factory .connect ("setup" , lambda factory , list_item : list_item .set_child (Gtk .Label (ellipsize = 3 , xalign = 0 )))
32+ factory .connect ("bind" , lambda factory , list_item : list_item .get_child ().set_label (list_item .get_item ().get_string ()))
33+
34+ pg = Adw .PreferencesGroup (
35+ title = _ ("Arguments" ),
36+ description = _ ("Variables that are filled by the AI." )
37+ )
38+ pp .add (pg )
39+ for name , data in self .tool .tool_metadata .get ('parameters' , {}).get ('properties' , {}).items ():
40+ if data .get ('enum' ):
41+ combo = Adw .ComboRow (
42+ name = name ,
43+ title = name .title (),
44+ factory = factory
45+ )
46+ string_list = Gtk .StringList ()
47+ for option in data .get ('enum' ):
48+ string_list .append (option )
49+ combo .set_model (string_list )
50+ self .parameter_elements .append (combo )
51+ pg .add (combo )
52+ else :
53+ entry = Adw .EntryRow (
54+ name = name ,
55+ title = name .title ()
56+ )
57+ self .parameter_elements .append (entry )
58+ pg .add (entry )
59+
60+ pg = Adw .PreferencesGroup ()
61+ pp .add (pg )
62+ run_button = Gtk .Button (
63+ label = _ ('Run Tool' ),
64+ css_classes = ['pill' , 'suggested-action' ]
65+ )
66+ run_button .connect ('clicked' , lambda * _ : self .run_tool ())
67+ pg .add (run_button )
68+
69+ temp_chat = chat .Chat ()
70+ self .m_element_bot = message .Message (
71+ dt = datetime .datetime .now (),
72+ message_id = generate_uuid (),
73+ chat = temp_chat ,
74+ mode = 1 ,
75+ author = 'Tool Tester'
76+ )
77+ self .m_element_bot .block_container .prepare_generating_block ()
78+ self .m_element_bot .block_container .add_css_class ('dim-label' )
79+ self .m_element_bot .options_button .set_visible (False )
80+ temp_chat .add_message (self .m_element_bot )
81+ self .main_stack .add_named (temp_chat , 'chat' )
82+
83+ tbv = Adw .ToolbarView ()
84+ tbv .add_top_bar (Adw .HeaderBar ())
85+ tbv .set_content (self .main_stack )
86+ super ().__init__ (
87+ child = tbv ,
88+ title = self .tool .name ,
89+ content_width = 500
90+ )
91+ self .connect ('closed' , lambda * _ : self .on_close ())
92+
93+ def on_close (self ):
94+ SQL .delete_message (self .m_element_bot )
95+
96+ def start (self , arguments ):
97+ gen_request , response = self .tool .run (arguments , [], self .m_element_bot )
98+ attachment_content = []
99+ if len (arguments ) > 0 :
100+ attachment_content += [
101+ '## {}' .format (_ ('Arguments' )),
102+ '| {} | {} |' .format (_ ('Argument' ), _ ('Value' )),
103+ '| --- | --- |'
104+ ]
105+ attachment_content += ['| {} | {} |' .format (k , v ) for k , v in arguments .items ()]
106+
107+ attachment_content += [
108+ '## {}' .format (_ ('Result' )),
109+ response
110+ ]
111+
112+ self .m_element_bot .add_attachment (
113+ file_id = generate_uuid (),
114+ name = self .tool .name ,
115+ attachment_type = 'tool' ,
116+ content = '\n ' .join (attachment_content )
117+ )
118+ self .m_element_bot .main_stack .set_visible_child_name ('content' )
119+
120+ def run_tool (self ):
121+ arguments = {}
122+ for el in self .parameter_elements :
123+ if isinstance (el , Adw .ComboRow ):
124+ arguments [el .get_name ()] = el .get_selected_item ().get_string ()
125+ elif isinstance (el , Adw .EntryRow ):
126+ arguments [el .get_name ()] = el .get_text ()
127+ self .main_stack .set_visible_child_name ('chat' )
128+ threading .Thread (target = self .start , args = (arguments ,)).start ()
129+
130+
17131class ToolPreferencesDialog (Adw .Dialog ):
18132 __gtype_name__ = 'AlpacaToolPreferencesDialog'
19133
@@ -176,13 +290,21 @@ def __init__(self, variables:dict, enabled:bool):
176290 info_button = Gtk .Button (icon_name = 'info-outline-symbolic' , css_classes = ['flat' ], valign = 3 )
177291 info_button .connect ('clicked' , lambda * _ : self .show_dialog ())
178292 self .add_prefix (info_button )
293+
294+ run_button = Gtk .Button (icon_name = 'media-playback-start-symbolic' , css_classes = ['flat' ], valign = 3 )
295+ run_button .connect ('clicked' , lambda * _ : self .show_run_dialog ())
296+ self .add_prefix (run_button )
297+
179298 self .enable_switch = Gtk .Switch (active = enabled , valign = 3 )
180299 self .enable_switch .connect ('state-set' , lambda * _ : self .enabled_changed ())
181300 self .add_suffix (self .enable_switch )
182301
183302 def show_dialog (self ):
184303 ToolPreferencesDialog (self ).present (self .get_root ())
185304
305+ def show_run_dialog (self ):
306+ ToolRunDialog (self ).present (self .get_root ())
307+
186308 def enabled_changed (self ):
187309 SQL .insert_or_update_tool_parameters (self .tool_metadata .get ('name' ), self .extract_variables_for_sql (), self .is_enabled ())
188310
@@ -367,7 +489,7 @@ def run(self, arguments, messages, bot_message) -> tuple:
367489 data .append ('- {}' .format (meal .get ("strMeal" )))
368490
369491 if arguments .get ("mode" , "list of meals" ) == "single recipe" :
370- response2 = requests .get ('www.themealdb.com/api/json/v1/1/lookup.php?i={}' .format (random .choice (data ).get ('id ' )))
492+ response2 = requests .get ('https:// www.themealdb.com/api/json/v1/1/lookup.php?i={}' .format (random .choice (response . json ( ).get ('meals' )). get ( 'idMeal ' )))
371493 if response2 .json ().get ("meals" , [False ])[0 ]:
372494 data = response2 .json ().get ("meals" )[0 ]
373495 self .attach_online_image (bot_message , data .get ('strMeal' , 'Meal' ), data .get ('strMealThumb' ))
@@ -389,7 +511,7 @@ def run(self, arguments, messages, bot_message) -> tuple:
389511 )
390512
391513 SQL .insert_or_update_attachment (bot_message , attachment )
392- return True , '\n ' .join (data )
514+ return True , '\n ' .join ([ '**{}: **{}' . format ( key , value ) for key , value in data . items () if value ] )
393515
394516class ExtractWikipedia (Base ):
395517 tool_metadata = {
0 commit comments