Spaces:
Running
Running
Update services/agentic_handlers.py
Browse files- services/agentic_handlers.py +77 -10
services/agentic_handlers.py
CHANGED
@@ -40,6 +40,50 @@ 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 |
async def run_agentic_pipeline_autonomously_on_update(self, current_token_state_val):
|
44 |
"""
|
45 |
This function is intended to be triggered by changes in token_state.
|
@@ -64,7 +108,13 @@ class AgenticHandlers:
|
|
64 |
report_status_md_update = gr.update(value=initial_report_status) if self.report_components.get("agentic_pipeline_status_md") else gr.update()
|
65 |
report_display_md_update = gr.update()
|
66 |
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
okrs_detail_md_update = gr.update(value=initial_okr_details) if self.okrs_components.get("okr_detail_display_md") else gr.update()
|
69 |
|
70 |
if not current_token_state_val or not current_token_state_val.get("token"):
|
@@ -104,7 +154,7 @@ class AgenticHandlers:
|
|
104 |
|
105 |
# Update for key_results_cbg (CheckboxGroup) in error case
|
106 |
if self.okrs_components.get("key_results_cbg"):
|
107 |
-
|
108 |
|
109 |
if self.okrs_components.get("okr_detail_display_md"):
|
110 |
okrs_detail_md_update = gr.update(value=error_status)
|
@@ -147,9 +197,23 @@ class AgenticHandlers:
|
|
147 |
krs_for_selection_update_val = krs_for_ui_selection_list # This is the list of dicts
|
148 |
|
149 |
# Prepare choices for key_results_cbg (CheckboxGroup)
|
150 |
-
kr_choices_for_cbg = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
151 |
if self.okrs_components.get("key_results_cbg"):
|
152 |
-
okrs_cbg_update =
|
|
|
|
|
|
|
|
|
153 |
|
154 |
all_okrs_md_parts = []
|
155 |
if actionable_okrs and isinstance(actionable_okrs.get("okrs"), list):
|
@@ -171,7 +235,7 @@ class AgenticHandlers:
|
|
171 |
|
172 |
# Update for key_results_cbg (CheckboxGroup) if no output
|
173 |
if self.okrs_components.get("key_results_cbg"):
|
174 |
-
okrs_cbg_update =
|
175 |
|
176 |
if self.okrs_components.get("okr_detail_display_md"):
|
177 |
okrs_detail_md_update = gr.update(value="Nessun OKR generato o errore nella pipeline AI (Sempre).")
|
@@ -196,7 +260,7 @@ class AgenticHandlers:
|
|
196 |
|
197 |
# Update for key_results_cbg (CheckboxGroup) in case of exception
|
198 |
if self.okrs_components.get("key_results_cbg"):
|
199 |
-
okrs_cbg_update =
|
200 |
|
201 |
if self.okrs_components.get("okr_detail_display_md"):
|
202 |
okrs_detail_md_update = gr.update(value=f"Errore generazione OKR AI (Sempre): {str(e)}")
|
@@ -217,11 +281,15 @@ class AgenticHandlers:
|
|
217 |
if not self.agentic_modules_really_loaded:
|
218 |
return gr.update(value="Moduli AI non caricati. Impossibile visualizzare i dettagli OKR.")
|
219 |
|
|
|
|
|
|
|
|
|
220 |
parsed_orchestration_results = None
|
221 |
try:
|
222 |
if raw_orchestration_results_json: # Check if the string is not empty
|
223 |
parsed_orchestration_results = json.loads(raw_orchestration_results_json)
|
224 |
-
except json.JSONDecodeError as e:
|
225 |
logging.error(f"Failed to parse raw_orchestration_results_json: {raw_orchestration_results_json}. Error: {e}")
|
226 |
return gr.update(value="Errore: Dati interni corrotti (orchestration results).")
|
227 |
|
@@ -232,7 +300,7 @@ class AgenticHandlers:
|
|
232 |
try:
|
233 |
if all_krs_for_selection_list_json: # Check if the string is not empty
|
234 |
parsed_krs_for_selection_list = json.loads(all_krs_for_selection_list_json)
|
235 |
-
except json.JSONDecodeError as e:
|
236 |
logging.error(f"Failed to parse all_krs_for_selection_list_json: {all_krs_for_selection_list_json}. Error: {e}")
|
237 |
return gr.update(value="Errore: Dati interni corrotti (krs for selection).")
|
238 |
|
@@ -241,7 +309,6 @@ class AgenticHandlers:
|
|
241 |
logging.warning(f"Parsed all_krs_for_selection_list is not a list: {type(parsed_krs_for_selection_list)}. Defaulting to empty list.")
|
242 |
parsed_krs_for_selection_list = []
|
243 |
|
244 |
-
|
245 |
actionable_okrs_dict = parsed_orchestration_results.get("actionable_okrs_and_tasks") if isinstance(parsed_orchestration_results, dict) else None
|
246 |
|
247 |
if not actionable_okrs_dict or not isinstance(actionable_okrs_dict.get("okrs"), list):
|
@@ -315,4 +382,4 @@ class AgenticHandlers:
|
|
315 |
)
|
316 |
logging.info("Agentic OKR selection handler setup complete.")
|
317 |
else:
|
318 |
-
logging.warning("key_results_cbg component not found for agentic OKR handler setup.")
|
|
|
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=None):
|
44 |
+
"""
|
45 |
+
Create a safe update for CheckboxGroup that handles version compatibility.
|
46 |
+
"""
|
47 |
+
update_dict = {}
|
48 |
+
|
49 |
+
# Only include parameters that are supported and valid
|
50 |
+
if choices is not None:
|
51 |
+
# Ensure choices is a list of tuples (display_value, return_value)
|
52 |
+
if isinstance(choices, list):
|
53 |
+
formatted_choices = []
|
54 |
+
for choice in choices:
|
55 |
+
if isinstance(choice, tuple) and len(choice) == 2:
|
56 |
+
formatted_choices.append(choice)
|
57 |
+
else:
|
58 |
+
# Convert single values to (display, value) tuples
|
59 |
+
formatted_choices.append((str(choice), choice))
|
60 |
+
update_dict['choices'] = formatted_choices
|
61 |
+
else:
|
62 |
+
update_dict['choices'] = []
|
63 |
+
|
64 |
+
if value is not None:
|
65 |
+
# Ensure value is a list and only contains valid choices
|
66 |
+
if isinstance(value, list):
|
67 |
+
if choices is not None:
|
68 |
+
# Filter value to only include items that exist in choices
|
69 |
+
valid_values = [choice[1] if isinstance(choice, tuple) else choice for choice in (choices or [])]
|
70 |
+
filtered_value = [v for v in value if v in valid_values]
|
71 |
+
update_dict['value'] = filtered_value
|
72 |
+
else:
|
73 |
+
update_dict['value'] = []
|
74 |
+
else:
|
75 |
+
update_dict['value'] = []
|
76 |
+
|
77 |
+
# Try to include interactive parameter, but handle gracefully if not supported
|
78 |
+
if interactive is not None:
|
79 |
+
try:
|
80 |
+
update_dict['interactive'] = interactive
|
81 |
+
except:
|
82 |
+
# If interactive parameter is not supported, continue without it
|
83 |
+
pass
|
84 |
+
|
85 |
+
return gr.update(**update_dict)
|
86 |
+
|
87 |
async def run_agentic_pipeline_autonomously_on_update(self, current_token_state_val):
|
88 |
"""
|
89 |
This function is intended to be triggered by changes in token_state.
|
|
|
108 |
report_status_md_update = gr.update(value=initial_report_status) if self.report_components.get("agentic_pipeline_status_md") else gr.update()
|
109 |
report_display_md_update = gr.update()
|
110 |
|
111 |
+
# Use safe checkbox update method
|
112 |
+
okrs_cbg_update = self._safe_checkbox_update(
|
113 |
+
choices=initial_okr_cbg_choices,
|
114 |
+
value=initial_okr_cbg_value,
|
115 |
+
interactive=initial_okr_cbg_interactive
|
116 |
+
) if self.okrs_components.get("key_results_cbg") else gr.update()
|
117 |
+
|
118 |
okrs_detail_md_update = gr.update(value=initial_okr_details) if self.okrs_components.get("okr_detail_display_md") else gr.update()
|
119 |
|
120 |
if not current_token_state_val or not current_token_state_val.get("token"):
|
|
|
154 |
|
155 |
# Update for key_results_cbg (CheckboxGroup) in error case
|
156 |
if self.okrs_components.get("key_results_cbg"):
|
157 |
+
okrs_cbg_update = self._safe_checkbox_update(choices=[], value=[], interactive=False)
|
158 |
|
159 |
if self.okrs_components.get("okr_detail_display_md"):
|
160 |
okrs_detail_md_update = gr.update(value=error_status)
|
|
|
197 |
krs_for_selection_update_val = krs_for_ui_selection_list # This is the list of dicts
|
198 |
|
199 |
# Prepare choices for key_results_cbg (CheckboxGroup)
|
200 |
+
kr_choices_for_cbg = []
|
201 |
+
if krs_for_ui_selection_list:
|
202 |
+
for kr in krs_for_ui_selection_list:
|
203 |
+
if isinstance(kr, dict) and 'kr_description' in kr and 'unique_kr_id' in kr:
|
204 |
+
# Ensure kr_description is a string and clean it
|
205 |
+
kr_desc = str(kr['kr_description']).strip()
|
206 |
+
# Truncate very long descriptions to avoid UI issues
|
207 |
+
if len(kr_desc) > 100:
|
208 |
+
kr_desc = kr_desc[:97] + "..."
|
209 |
+
kr_choices_for_cbg.append((kr_desc, kr['unique_kr_id']))
|
210 |
+
|
211 |
if self.okrs_components.get("key_results_cbg"):
|
212 |
+
okrs_cbg_update = self._safe_checkbox_update(
|
213 |
+
choices=kr_choices_for_cbg,
|
214 |
+
value=[],
|
215 |
+
interactive=True
|
216 |
+
)
|
217 |
|
218 |
all_okrs_md_parts = []
|
219 |
if actionable_okrs and isinstance(actionable_okrs.get("okrs"), list):
|
|
|
235 |
|
236 |
# Update for key_results_cbg (CheckboxGroup) if no output
|
237 |
if self.okrs_components.get("key_results_cbg"):
|
238 |
+
okrs_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).")
|
|
|
260 |
|
261 |
# Update for key_results_cbg (CheckboxGroup) in case of exception
|
262 |
if self.okrs_components.get("key_results_cbg"):
|
263 |
+
okrs_cbg_update = self._safe_checkbox_update(choices=[], value=[], interactive=False)
|
264 |
|
265 |
if self.okrs_components.get("okr_detail_display_md"):
|
266 |
okrs_detail_md_update = gr.update(value=f"Errore generazione OKR AI (Sempre): {str(e)}")
|
|
|
281 |
if not self.agentic_modules_really_loaded:
|
282 |
return gr.update(value="Moduli AI non caricati. Impossibile visualizzare i dettagli OKR.")
|
283 |
|
284 |
+
# Handle case where selected_kr_unique_ids might be None or not a list
|
285 |
+
if not isinstance(selected_kr_unique_ids, list):
|
286 |
+
selected_kr_unique_ids = []
|
287 |
+
|
288 |
parsed_orchestration_results = None
|
289 |
try:
|
290 |
if raw_orchestration_results_json: # Check if the string is not empty
|
291 |
parsed_orchestration_results = json.loads(raw_orchestration_results_json)
|
292 |
+
except (json.JSONDecodeError, TypeError) as e:
|
293 |
logging.error(f"Failed to parse raw_orchestration_results_json: {raw_orchestration_results_json}. Error: {e}")
|
294 |
return gr.update(value="Errore: Dati interni corrotti (orchestration results).")
|
295 |
|
|
|
300 |
try:
|
301 |
if all_krs_for_selection_list_json: # Check if the string is not empty
|
302 |
parsed_krs_for_selection_list = json.loads(all_krs_for_selection_list_json)
|
303 |
+
except (json.JSONDecodeError, TypeError) as e:
|
304 |
logging.error(f"Failed to parse all_krs_for_selection_list_json: {all_krs_for_selection_list_json}. Error: {e}")
|
305 |
return gr.update(value="Errore: Dati interni corrotti (krs for selection).")
|
306 |
|
|
|
309 |
logging.warning(f"Parsed all_krs_for_selection_list is not a list: {type(parsed_krs_for_selection_list)}. Defaulting to empty list.")
|
310 |
parsed_krs_for_selection_list = []
|
311 |
|
|
|
312 |
actionable_okrs_dict = parsed_orchestration_results.get("actionable_okrs_and_tasks") if isinstance(parsed_orchestration_results, dict) else None
|
313 |
|
314 |
if not actionable_okrs_dict or not isinstance(actionable_okrs_dict.get("okrs"), list):
|
|
|
382 |
)
|
383 |
logging.info("Agentic OKR selection handler setup complete.")
|
384 |
else:
|
385 |
+
logging.warning("key_results_cbg component not found for agentic OKR handler setup.")
|