Spaces:
Running
Running
Update services/agentic_handlers.py
Browse files- services/agentic_handlers.py +81 -65
services/agentic_handlers.py
CHANGED
@@ -40,40 +40,48 @@ class AgenticHandlers:
|
|
40 |
self.agentic_modules_really_loaded = AGENTIC_MODULES_LOADED
|
41 |
logging.info(f"AgenticHandlers initialized. Modules loaded: {self.agentic_modules_really_loaded}")
|
42 |
|
43 |
-
def
|
44 |
"""
|
45 |
-
|
46 |
-
since gr.update() doesn't support choices parameter in some Gradio versions.
|
47 |
"""
|
48 |
try:
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
# Format choices properly
|
53 |
if choices is not None:
|
|
|
54 |
formatted_choices = []
|
55 |
for choice in choices:
|
56 |
if isinstance(choice, tuple) and len(choice) == 2:
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
59 |
# Convert single values to (display, value) tuples
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
64 |
|
|
|
|
|
65 |
if value is not None:
|
|
|
66 |
if isinstance(value, list):
|
67 |
-
|
|
|
|
|
68 |
else:
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
|
|
73 |
|
74 |
except Exception as e:
|
75 |
-
logging.error(f"Error
|
76 |
-
return gr.update()
|
77 |
|
78 |
async def run_agentic_pipeline_autonomously_on_update(self, current_token_state_val):
|
79 |
"""
|
@@ -88,9 +96,7 @@ class AgenticHandlers:
|
|
88 |
initial_okr_details = "Pipeline AI: In attesa dei dati necessari..."
|
89 |
|
90 |
# Initial state for key_results_cbg (CheckboxGroup)
|
91 |
-
|
92 |
-
initial_okr_cbg_value = []
|
93 |
-
initial_okr_cbg_interactive = False
|
94 |
|
95 |
initial_orchestration_results = self.orchestration_raw_results_st.value
|
96 |
initial_selected_krs = self.selected_key_result_ids_st.value
|
@@ -98,10 +104,6 @@ class AgenticHandlers:
|
|
98 |
|
99 |
report_status_md_update = gr.update(value=initial_report_status) if self.report_components.get("agentic_pipeline_status_md") else gr.update()
|
100 |
report_display_md_update = gr.update()
|
101 |
-
|
102 |
-
# Initialize checkbox component - no update needed initially
|
103 |
-
okrs_cbg_update = gr.update() if self.okrs_components.get("key_results_cbg") else gr.update()
|
104 |
-
|
105 |
okrs_detail_md_update = gr.update(value=initial_okr_details) if self.okrs_components.get("okr_detail_display_md") else gr.update()
|
106 |
|
107 |
if not current_token_state_val or not current_token_state_val.get("token"):
|
@@ -109,11 +111,11 @@ class AgenticHandlers:
|
|
109 |
yield (
|
110 |
report_status_md_update,
|
111 |
report_display_md_update,
|
112 |
-
|
113 |
okrs_detail_md_update,
|
114 |
json.dumps(initial_orchestration_results), # Serialize to JSON
|
115 |
-
json.dumps(initial_selected_krs), # Serialize to JSON
|
116 |
-
json.dumps(initial_krs_for_selection) # Serialize to JSON
|
117 |
)
|
118 |
return
|
119 |
|
@@ -123,12 +125,17 @@ class AgenticHandlers:
|
|
123 |
if self.okrs_components.get("okr_detail_display_md"):
|
124 |
okrs_detail_md_update = gr.update(value="Dettagli OKR (Sempre) in corso di generazione...")
|
125 |
|
126 |
-
#
|
|
|
|
|
127 |
yield (
|
128 |
-
report_status_md_update,
|
|
|
|
|
|
|
129 |
json.dumps(initial_orchestration_results), # Serialize to JSON
|
130 |
-
json.dumps(initial_selected_krs), # Serialize to JSON
|
131 |
-
json.dumps(initial_krs_for_selection) # Serialize to JSON
|
132 |
)
|
133 |
|
134 |
if not self.agentic_modules_really_loaded:
|
@@ -140,15 +147,19 @@ class AgenticHandlers:
|
|
140 |
report_display_md_update = gr.update(value=error_status)
|
141 |
|
142 |
# Update for key_results_cbg (CheckboxGroup) in error case
|
143 |
-
|
144 |
-
okrs_cbg_update = gr.update()
|
145 |
|
146 |
if self.okrs_components.get("okr_detail_display_md"):
|
147 |
okrs_detail_md_update = gr.update(value=error_status)
|
148 |
|
149 |
yield (
|
150 |
-
report_status_md_update,
|
151 |
-
|
|
|
|
|
|
|
|
|
|
|
152 |
)
|
153 |
return
|
154 |
|
@@ -171,6 +182,7 @@ class AgenticHandlers:
|
|
171 |
orchestration_results_update_val = None
|
172 |
selected_krs_update_val = [] # This will be the value for the CheckboxGroup, initially empty
|
173 |
krs_for_selection_update_val = []
|
|
|
174 |
|
175 |
if orchestration_output:
|
176 |
orchestration_results_update_val = orchestration_output
|
@@ -185,7 +197,7 @@ class AgenticHandlers:
|
|
185 |
|
186 |
# Prepare choices for key_results_cbg (CheckboxGroup)
|
187 |
kr_choices_for_cbg = []
|
188 |
-
if krs_for_ui_selection_list:
|
189 |
for kr in krs_for_ui_selection_list:
|
190 |
if isinstance(kr, dict) and 'kr_description' in kr and 'unique_kr_id' in kr:
|
191 |
# Ensure kr_description is a string and clean it
|
@@ -193,17 +205,16 @@ class AgenticHandlers:
|
|
193 |
# Truncate very long descriptions to avoid UI issues
|
194 |
if len(kr_desc) > 100:
|
195 |
kr_desc = kr_desc[:97] + "..."
|
196 |
-
|
|
|
|
|
197 |
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
value=[]
|
205 |
-
)
|
206 |
-
okrs_cbg_update = gr.update()
|
207 |
|
208 |
all_okrs_md_parts = []
|
209 |
if actionable_okrs and isinstance(actionable_okrs.get("okrs"), list):
|
@@ -224,14 +235,7 @@ class AgenticHandlers:
|
|
224 |
report_display_md_update = gr.update(value="Nessun report generato dalla pipeline AI (Sempre).")
|
225 |
|
226 |
# Update for key_results_cbg (CheckboxGroup) if no output
|
227 |
-
|
228 |
-
checkbox_component = self.okrs_components["key_results_cbg"]
|
229 |
-
self._update_checkbox_component_directly(
|
230 |
-
checkbox_component,
|
231 |
-
choices=[],
|
232 |
-
value=[]
|
233 |
-
)
|
234 |
-
okrs_cbg_update = gr.update()
|
235 |
|
236 |
if self.okrs_components.get("okr_detail_display_md"):
|
237 |
okrs_detail_md_update = gr.update(value="Nessun OKR generato o errore nella pipeline AI (Sempre).")
|
@@ -240,7 +244,10 @@ class AgenticHandlers:
|
|
240 |
report_status_md_update = gr.update(value=final_status_text)
|
241 |
|
242 |
yield (
|
243 |
-
report_status_md_update,
|
|
|
|
|
|
|
244 |
json.dumps(orchestration_results_update_val), # Serialize to JSON
|
245 |
json.dumps(selected_krs_update_val), # Serialize to JSON (value for selected_key_result_ids_st)
|
246 |
json.dumps(krs_for_selection_update_val) # Serialize to JSON (value for key_results_for_selection_st)
|
@@ -255,15 +262,19 @@ class AgenticHandlers:
|
|
255 |
report_display_md_update = gr.update(value=f"Errore generazione report AI (Sempre): {str(e)}")
|
256 |
|
257 |
# Update for key_results_cbg (CheckboxGroup) in case of exception
|
258 |
-
|
259 |
-
okrs_cbg_update = self._safe_checkbox_update(choices=[], value=[], interactive=False)
|
260 |
|
261 |
if self.okrs_components.get("okr_detail_display_md"):
|
262 |
okrs_detail_md_update = gr.update(value=f"Errore generazione OKR AI (Sempre): {str(e)}")
|
263 |
|
264 |
yield (
|
265 |
-
report_status_md_update,
|
266 |
-
|
|
|
|
|
|
|
|
|
|
|
267 |
)
|
268 |
|
269 |
def update_okr_display_on_kr_selection(self, selected_kr_unique_ids: list,
|
@@ -281,6 +292,9 @@ class AgenticHandlers:
|
|
281 |
if not isinstance(selected_kr_unique_ids, list):
|
282 |
selected_kr_unique_ids = []
|
283 |
|
|
|
|
|
|
|
284 |
parsed_orchestration_results = None
|
285 |
try:
|
286 |
if raw_orchestration_results_json: # Check if the string is not empty
|
@@ -318,7 +332,8 @@ class AgenticHandlers:
|
|
318 |
if isinstance(parsed_krs_for_selection_list, list): # Ensure it's a list before iterating
|
319 |
for kr_info in parsed_krs_for_selection_list:
|
320 |
if isinstance(kr_info, dict) and 'unique_kr_id' in kr_info and 'okr_index' in kr_info and 'kr_index' in kr_info:
|
321 |
-
|
|
|
322 |
else:
|
323 |
logging.warning(f"Skipping invalid kr_info item: {kr_info}")
|
324 |
|
@@ -326,8 +341,9 @@ class AgenticHandlers:
|
|
326 |
# selected_kr_unique_ids comes directly from CheckboxGroup, should be a list of strings/values
|
327 |
if isinstance(selected_kr_unique_ids, list):
|
328 |
for kr_unique_id in selected_kr_unique_ids:
|
329 |
-
|
330 |
-
|
|
|
331 |
selected_krs_by_okr_idx[okr_idx].append(kr_idx_in_okr)
|
332 |
|
333 |
output_md_parts = []
|
|
|
40 |
self.agentic_modules_really_loaded = AGENTIC_MODULES_LOADED
|
41 |
logging.info(f"AgenticHandlers initialized. Modules loaded: {self.agentic_modules_really_loaded}")
|
42 |
|
43 |
+
def _safe_checkbox_update(self, choices=None, value=None, interactive=True):
|
44 |
"""
|
45 |
+
Safely create a CheckboxGroup update with proper format.
|
|
|
46 |
"""
|
47 |
try:
|
48 |
+
update_dict = {}
|
49 |
+
|
|
|
|
|
50 |
if choices is not None:
|
51 |
+
# Ensure choices is a list of tuples (display, value)
|
52 |
formatted_choices = []
|
53 |
for choice in choices:
|
54 |
if isinstance(choice, tuple) and len(choice) == 2:
|
55 |
+
# Ensure both elements are strings
|
56 |
+
display_text = str(choice[0]).strip()
|
57 |
+
choice_value = str(choice[1]).strip()
|
58 |
+
formatted_choices.append((display_text, choice_value))
|
59 |
+
elif isinstance(choice, (str, int)):
|
60 |
# Convert single values to (display, value) tuples
|
61 |
+
choice_str = str(choice).strip()
|
62 |
+
formatted_choices.append((choice_str, choice_str))
|
63 |
+
else:
|
64 |
+
logging.warning(f"Invalid choice format: {choice}")
|
65 |
+
continue
|
66 |
|
67 |
+
update_dict['choices'] = formatted_choices
|
68 |
+
|
69 |
if value is not None:
|
70 |
+
# Ensure value is a list of strings that match choice values
|
71 |
if isinstance(value, list):
|
72 |
+
# Ensure all values are strings
|
73 |
+
formatted_value = [str(v).strip() for v in value if v is not None]
|
74 |
+
update_dict['value'] = formatted_value
|
75 |
else:
|
76 |
+
update_dict['value'] = []
|
77 |
+
|
78 |
+
update_dict['interactive'] = interactive
|
79 |
+
|
80 |
+
return gr.update(**update_dict)
|
81 |
|
82 |
except Exception as e:
|
83 |
+
logging.error(f"Error creating checkbox update: {e}")
|
84 |
+
return gr.update(choices=[], value=[], interactive=False)
|
85 |
|
86 |
async def run_agentic_pipeline_autonomously_on_update(self, current_token_state_val):
|
87 |
"""
|
|
|
96 |
initial_okr_details = "Pipeline AI: In attesa dei dati necessari..."
|
97 |
|
98 |
# Initial state for key_results_cbg (CheckboxGroup)
|
99 |
+
initial_okr_cbg_update = self._safe_checkbox_update(choices=[], value=[], interactive=False)
|
|
|
|
|
100 |
|
101 |
initial_orchestration_results = self.orchestration_raw_results_st.value
|
102 |
initial_selected_krs = self.selected_key_result_ids_st.value
|
|
|
104 |
|
105 |
report_status_md_update = gr.update(value=initial_report_status) if self.report_components.get("agentic_pipeline_status_md") else gr.update()
|
106 |
report_display_md_update = gr.update()
|
|
|
|
|
|
|
|
|
107 |
okrs_detail_md_update = gr.update(value=initial_okr_details) if self.okrs_components.get("okr_detail_display_md") else gr.update()
|
108 |
|
109 |
if not current_token_state_val or not current_token_state_val.get("token"):
|
|
|
111 |
yield (
|
112 |
report_status_md_update,
|
113 |
report_display_md_update,
|
114 |
+
initial_okr_cbg_update,
|
115 |
okrs_detail_md_update,
|
116 |
json.dumps(initial_orchestration_results), # Serialize to JSON
|
117 |
+
json.dumps(initial_selected_krs if isinstance(initial_selected_krs, list) else []), # Serialize to JSON
|
118 |
+
json.dumps(initial_krs_for_selection if isinstance(initial_krs_for_selection, list) else []) # Serialize to JSON
|
119 |
)
|
120 |
return
|
121 |
|
|
|
125 |
if self.okrs_components.get("okr_detail_display_md"):
|
126 |
okrs_detail_md_update = gr.update(value="Dettagli OKR (Sempre) in corso di generazione...")
|
127 |
|
128 |
+
# Show loading state for CheckboxGroup
|
129 |
+
loading_okr_cbg_update = self._safe_checkbox_update(choices=[], value=[], interactive=False)
|
130 |
+
|
131 |
yield (
|
132 |
+
report_status_md_update,
|
133 |
+
report_display_md_update,
|
134 |
+
loading_okr_cbg_update,
|
135 |
+
okrs_detail_md_update,
|
136 |
json.dumps(initial_orchestration_results), # Serialize to JSON
|
137 |
+
json.dumps(initial_selected_krs if isinstance(initial_selected_krs, list) else []), # Serialize to JSON
|
138 |
+
json.dumps(initial_krs_for_selection if isinstance(initial_krs_for_selection, list) else []) # Serialize to JSON
|
139 |
)
|
140 |
|
141 |
if not self.agentic_modules_really_loaded:
|
|
|
147 |
report_display_md_update = gr.update(value=error_status)
|
148 |
|
149 |
# Update for key_results_cbg (CheckboxGroup) in error case
|
150 |
+
error_okr_cbg_update = self._safe_checkbox_update(choices=[], value=[], interactive=False)
|
|
|
151 |
|
152 |
if self.okrs_components.get("okr_detail_display_md"):
|
153 |
okrs_detail_md_update = gr.update(value=error_status)
|
154 |
|
155 |
yield (
|
156 |
+
report_status_md_update,
|
157 |
+
report_display_md_update,
|
158 |
+
error_okr_cbg_update,
|
159 |
+
okrs_detail_md_update,
|
160 |
+
json.dumps(None),
|
161 |
+
json.dumps([]),
|
162 |
+
json.dumps([]) # Serialize to JSON
|
163 |
)
|
164 |
return
|
165 |
|
|
|
182 |
orchestration_results_update_val = None
|
183 |
selected_krs_update_val = [] # This will be the value for the CheckboxGroup, initially empty
|
184 |
krs_for_selection_update_val = []
|
185 |
+
final_okr_cbg_update = self._safe_checkbox_update(choices=[], value=[], interactive=False)
|
186 |
|
187 |
if orchestration_output:
|
188 |
orchestration_results_update_val = orchestration_output
|
|
|
197 |
|
198 |
# Prepare choices for key_results_cbg (CheckboxGroup)
|
199 |
kr_choices_for_cbg = []
|
200 |
+
if krs_for_ui_selection_list and isinstance(krs_for_ui_selection_list, list):
|
201 |
for kr in krs_for_ui_selection_list:
|
202 |
if isinstance(kr, dict) and 'kr_description' in kr and 'unique_kr_id' in kr:
|
203 |
# Ensure kr_description is a string and clean it
|
|
|
205 |
# Truncate very long descriptions to avoid UI issues
|
206 |
if len(kr_desc) > 100:
|
207 |
kr_desc = kr_desc[:97] + "..."
|
208 |
+
# Ensure unique_kr_id is a string
|
209 |
+
kr_id = str(kr['unique_kr_id']).strip()
|
210 |
+
kr_choices_for_cbg.append((kr_desc, kr_id))
|
211 |
|
212 |
+
# Create CheckboxGroup update with proper choices
|
213 |
+
final_okr_cbg_update = self._safe_checkbox_update(
|
214 |
+
choices=kr_choices_for_cbg,
|
215 |
+
value=[],
|
216 |
+
interactive=True
|
217 |
+
)
|
|
|
|
|
|
|
218 |
|
219 |
all_okrs_md_parts = []
|
220 |
if actionable_okrs and isinstance(actionable_okrs.get("okrs"), list):
|
|
|
235 |
report_display_md_update = gr.update(value="Nessun report generato dalla pipeline AI (Sempre).")
|
236 |
|
237 |
# Update for key_results_cbg (CheckboxGroup) if no output
|
238 |
+
final_okr_cbg_update = self._safe_checkbox_update(choices=[], value=[], interactive=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
239 |
|
240 |
if self.okrs_components.get("okr_detail_display_md"):
|
241 |
okrs_detail_md_update = gr.update(value="Nessun OKR generato o errore nella pipeline AI (Sempre).")
|
|
|
244 |
report_status_md_update = gr.update(value=final_status_text)
|
245 |
|
246 |
yield (
|
247 |
+
report_status_md_update,
|
248 |
+
report_display_md_update,
|
249 |
+
final_okr_cbg_update,
|
250 |
+
okrs_detail_md_update,
|
251 |
json.dumps(orchestration_results_update_val), # Serialize to JSON
|
252 |
json.dumps(selected_krs_update_val), # Serialize to JSON (value for selected_key_result_ids_st)
|
253 |
json.dumps(krs_for_selection_update_val) # Serialize to JSON (value for key_results_for_selection_st)
|
|
|
262 |
report_display_md_update = gr.update(value=f"Errore generazione report AI (Sempre): {str(e)}")
|
263 |
|
264 |
# Update for key_results_cbg (CheckboxGroup) in case of exception
|
265 |
+
error_okr_cbg_update = self._safe_checkbox_update(choices=[], value=[], interactive=False)
|
|
|
266 |
|
267 |
if self.okrs_components.get("okr_detail_display_md"):
|
268 |
okrs_detail_md_update = gr.update(value=f"Errore generazione OKR AI (Sempre): {str(e)}")
|
269 |
|
270 |
yield (
|
271 |
+
report_status_md_update,
|
272 |
+
report_display_md_update,
|
273 |
+
error_okr_cbg_update,
|
274 |
+
okrs_detail_md_update,
|
275 |
+
json.dumps(None),
|
276 |
+
json.dumps([]),
|
277 |
+
json.dumps([]) # Serialize to JSON
|
278 |
)
|
279 |
|
280 |
def update_okr_display_on_kr_selection(self, selected_kr_unique_ids: list,
|
|
|
292 |
if not isinstance(selected_kr_unique_ids, list):
|
293 |
selected_kr_unique_ids = []
|
294 |
|
295 |
+
# Ensure all selected IDs are strings
|
296 |
+
selected_kr_unique_ids = [str(id).strip() for id in selected_kr_unique_ids if id is not None]
|
297 |
+
|
298 |
parsed_orchestration_results = None
|
299 |
try:
|
300 |
if raw_orchestration_results_json: # Check if the string is not empty
|
|
|
332 |
if isinstance(parsed_krs_for_selection_list, list): # Ensure it's a list before iterating
|
333 |
for kr_info in parsed_krs_for_selection_list:
|
334 |
if isinstance(kr_info, dict) and 'unique_kr_id' in kr_info and 'okr_index' in kr_info and 'kr_index' in kr_info:
|
335 |
+
kr_id = str(kr_info['unique_kr_id']).strip()
|
336 |
+
kr_id_to_indices[kr_id] = (kr_info['okr_index'], kr_info['kr_index'])
|
337 |
else:
|
338 |
logging.warning(f"Skipping invalid kr_info item: {kr_info}")
|
339 |
|
|
|
341 |
# selected_kr_unique_ids comes directly from CheckboxGroup, should be a list of strings/values
|
342 |
if isinstance(selected_kr_unique_ids, list):
|
343 |
for kr_unique_id in selected_kr_unique_ids:
|
344 |
+
kr_unique_id_str = str(kr_unique_id).strip()
|
345 |
+
if kr_unique_id_str in kr_id_to_indices:
|
346 |
+
okr_idx, kr_idx_in_okr = kr_id_to_indices[kr_unique_id_str]
|
347 |
selected_krs_by_okr_idx[okr_idx].append(kr_idx_in_okr)
|
348 |
|
349 |
output_md_parts = []
|