Spaces:
Running
Running
Update services/agentic_handlers.py
Browse files- services/agentic_handlers.py +31 -112
services/agentic_handlers.py
CHANGED
@@ -40,70 +40,28 @@ 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 _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 |
"""
|
88 |
This function is intended to be triggered by changes in token_state.
|
89 |
It yields updates for the agentic report and OKR tabs.
|
90 |
State values (5th, 6th, 7th) are serialized to JSON strings.
|
91 |
-
Updates for key_results_cbg are now
|
92 |
"""
|
93 |
logging.info(f"Agentic pipeline auto-trigger. Token: {'Set' if current_token_state_val.get('token') else 'Not Set'}")
|
94 |
|
95 |
initial_report_status = "Pipeline AI: In attesa dei dati necessari..."
|
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
|
103 |
initial_krs_for_selection = self.key_results_for_selection_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,11 +69,11 @@ class AgenticHandlers:
|
|
111 |
yield (
|
112 |
report_status_md_update,
|
113 |
report_display_md_update,
|
114 |
-
|
115 |
okrs_detail_md_update,
|
116 |
json.dumps(initial_orchestration_results), # Serialize to JSON
|
117 |
-
json.dumps(initial_selected_krs
|
118 |
-
json.dumps(initial_krs_for_selection
|
119 |
)
|
120 |
return
|
121 |
|
@@ -125,17 +83,11 @@ class AgenticHandlers:
|
|
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
|
138 |
-
json.dumps(initial_krs_for_selection
|
139 |
)
|
140 |
|
141 |
if not self.agentic_modules_really_loaded:
|
@@ -146,20 +98,16 @@ class AgenticHandlers:
|
|
146 |
if self.report_components.get("agentic_report_display_md"):
|
147 |
report_display_md_update = gr.update(value=error_status)
|
148 |
|
149 |
-
#
|
150 |
-
|
|
|
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 |
-
|
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,7 +130,6 @@ class AgenticHandlers:
|
|
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
|
@@ -195,26 +142,9 @@ class AgenticHandlers:
|
|
195 |
krs_for_ui_selection_list = extract_key_results_for_selection(actionable_okrs)
|
196 |
krs_for_selection_update_val = krs_for_ui_selection_list # This is the list of dicts
|
197 |
|
198 |
-
#
|
199 |
-
|
200 |
-
|
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
|
204 |
-
kr_desc = str(kr['kr_description']).strip()
|
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):
|
@@ -234,8 +164,9 @@ class AgenticHandlers:
|
|
234 |
if self.report_components.get("agentic_report_display_md"):
|
235 |
report_display_md_update = gr.update(value="Nessun report generato dalla pipeline AI (Sempre).")
|
236 |
|
237 |
-
#
|
238 |
-
|
|
|
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,10 +175,7 @@ class AgenticHandlers:
|
|
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)
|
@@ -261,20 +189,16 @@ class AgenticHandlers:
|
|
261 |
if self.report_components.get("agentic_report_display_md"):
|
262 |
report_display_md_update = gr.update(value=f"Errore generazione report AI (Sempre): {str(e)}")
|
263 |
|
264 |
-
#
|
265 |
-
|
|
|
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 |
-
|
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,9 +216,6 @@ class AgenticHandlers:
|
|
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,8 +253,7 @@ class AgenticHandlers:
|
|
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 |
-
|
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,9 +261,8 @@ class AgenticHandlers:
|
|
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 |
-
|
345 |
-
|
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 = []
|
|
|
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.
|
46 |
It yields updates for the agentic report and OKR tabs.
|
47 |
State values (5th, 6th, 7th) are serialized to JSON strings.
|
48 |
+
Updates for key_results_cbg are now simplified without choices/interactive.
|
49 |
"""
|
50 |
logging.info(f"Agentic pipeline auto-trigger. Token: {'Set' if current_token_state_val.get('token') else 'Not Set'}")
|
51 |
|
52 |
initial_report_status = "Pipeline AI: In attesa dei dati necessari..."
|
53 |
initial_okr_details = "Pipeline AI: In attesa dei dati necessari..."
|
54 |
|
|
|
|
|
|
|
55 |
initial_orchestration_results = self.orchestration_raw_results_st.value
|
56 |
initial_selected_krs = self.selected_key_result_ids_st.value
|
57 |
initial_krs_for_selection = self.key_results_for_selection_st.value
|
58 |
|
59 |
report_status_md_update = gr.update(value=initial_report_status) if self.report_components.get("agentic_pipeline_status_md") else gr.update()
|
60 |
report_display_md_update = gr.update()
|
61 |
+
|
62 |
+
# Simple update for checkbox component - just reset value
|
63 |
+
okrs_cbg_update = gr.update(value=[]) if self.okrs_components.get("key_results_cbg") else gr.update()
|
64 |
+
|
65 |
okrs_detail_md_update = gr.update(value=initial_okr_details) if self.okrs_components.get("okr_detail_display_md") else gr.update()
|
66 |
|
67 |
if not current_token_state_val or not current_token_state_val.get("token"):
|
|
|
69 |
yield (
|
70 |
report_status_md_update,
|
71 |
report_display_md_update,
|
72 |
+
okrs_cbg_update,
|
73 |
okrs_detail_md_update,
|
74 |
json.dumps(initial_orchestration_results), # Serialize to JSON
|
75 |
+
json.dumps(initial_selected_krs), # Serialize to JSON
|
76 |
+
json.dumps(initial_krs_for_selection) # Serialize to JSON
|
77 |
)
|
78 |
return
|
79 |
|
|
|
83 |
if self.okrs_components.get("okr_detail_display_md"):
|
84 |
okrs_detail_md_update = gr.update(value="Dettagli OKR (Sempre) in corso di generazione...")
|
85 |
|
|
|
|
|
|
|
86 |
yield (
|
87 |
+
report_status_md_update, report_display_md_update, okrs_cbg_update, okrs_detail_md_update,
|
|
|
|
|
|
|
88 |
json.dumps(initial_orchestration_results), # Serialize to JSON
|
89 |
+
json.dumps(initial_selected_krs), # Serialize to JSON
|
90 |
+
json.dumps(initial_krs_for_selection) # Serialize to JSON
|
91 |
)
|
92 |
|
93 |
if not self.agentic_modules_really_loaded:
|
|
|
98 |
if self.report_components.get("agentic_report_display_md"):
|
99 |
report_display_md_update = gr.update(value=error_status)
|
100 |
|
101 |
+
# Simple update for checkbox in error case
|
102 |
+
if self.okrs_components.get("key_results_cbg"):
|
103 |
+
okrs_cbg_update = gr.update(value=[])
|
104 |
|
105 |
if self.okrs_components.get("okr_detail_display_md"):
|
106 |
okrs_detail_md_update = gr.update(value=error_status)
|
107 |
|
108 |
yield (
|
109 |
+
report_status_md_update, report_display_md_update, okrs_cbg_update, okrs_detail_md_update,
|
110 |
+
json.dumps(None), json.dumps([]), json.dumps([]) # Serialize to JSON
|
|
|
|
|
|
|
|
|
|
|
111 |
)
|
112 |
return
|
113 |
|
|
|
130 |
orchestration_results_update_val = None
|
131 |
selected_krs_update_val = [] # This will be the value for the CheckboxGroup, initially empty
|
132 |
krs_for_selection_update_val = []
|
|
|
133 |
|
134 |
if orchestration_output:
|
135 |
orchestration_results_update_val = orchestration_output
|
|
|
142 |
krs_for_ui_selection_list = extract_key_results_for_selection(actionable_okrs)
|
143 |
krs_for_selection_update_val = krs_for_ui_selection_list # This is the list of dicts
|
144 |
|
145 |
+
# For checkbox, just reset the value - don't update choices programmatically
|
146 |
+
if self.okrs_components.get("key_results_cbg"):
|
147 |
+
okrs_cbg_update = gr.update(value=[])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
148 |
|
149 |
all_okrs_md_parts = []
|
150 |
if actionable_okrs and isinstance(actionable_okrs.get("okrs"), list):
|
|
|
164 |
if self.report_components.get("agentic_report_display_md"):
|
165 |
report_display_md_update = gr.update(value="Nessun report generato dalla pipeline AI (Sempre).")
|
166 |
|
167 |
+
# Simple update for checkbox if no output
|
168 |
+
if self.okrs_components.get("key_results_cbg"):
|
169 |
+
okrs_cbg_update = gr.update(value=[])
|
170 |
|
171 |
if self.okrs_components.get("okr_detail_display_md"):
|
172 |
okrs_detail_md_update = gr.update(value="Nessun OKR generato o errore nella pipeline AI (Sempre).")
|
|
|
175 |
report_status_md_update = gr.update(value=final_status_text)
|
176 |
|
177 |
yield (
|
178 |
+
report_status_md_update, report_display_md_update, okrs_cbg_update, okrs_detail_md_update,
|
|
|
|
|
|
|
179 |
json.dumps(orchestration_results_update_val), # Serialize to JSON
|
180 |
json.dumps(selected_krs_update_val), # Serialize to JSON (value for selected_key_result_ids_st)
|
181 |
json.dumps(krs_for_selection_update_val) # Serialize to JSON (value for key_results_for_selection_st)
|
|
|
189 |
if self.report_components.get("agentic_report_display_md"):
|
190 |
report_display_md_update = gr.update(value=f"Errore generazione report AI (Sempre): {str(e)}")
|
191 |
|
192 |
+
# Simple update for checkbox in case of exception
|
193 |
+
if self.okrs_components.get("key_results_cbg"):
|
194 |
+
okrs_cbg_update = gr.update(value=[])
|
195 |
|
196 |
if self.okrs_components.get("okr_detail_display_md"):
|
197 |
okrs_detail_md_update = gr.update(value=f"Errore generazione OKR AI (Sempre): {str(e)}")
|
198 |
|
199 |
yield (
|
200 |
+
report_status_md_update, report_display_md_update, okrs_cbg_update, okrs_detail_md_update,
|
201 |
+
json.dumps(None), json.dumps([]), json.dumps([]) # Serialize to JSON
|
|
|
|
|
|
|
|
|
|
|
202 |
)
|
203 |
|
204 |
def update_okr_display_on_kr_selection(self, selected_kr_unique_ids: list,
|
|
|
216 |
if not isinstance(selected_kr_unique_ids, list):
|
217 |
selected_kr_unique_ids = []
|
218 |
|
|
|
|
|
|
|
219 |
parsed_orchestration_results = None
|
220 |
try:
|
221 |
if raw_orchestration_results_json: # Check if the string is not empty
|
|
|
253 |
if isinstance(parsed_krs_for_selection_list, list): # Ensure it's a list before iterating
|
254 |
for kr_info in parsed_krs_for_selection_list:
|
255 |
if isinstance(kr_info, dict) and 'unique_kr_id' in kr_info and 'okr_index' in kr_info and 'kr_index' in kr_info:
|
256 |
+
kr_id_to_indices[kr_info['unique_kr_id']] = (kr_info['okr_index'], kr_info['kr_index'])
|
|
|
257 |
else:
|
258 |
logging.warning(f"Skipping invalid kr_info item: {kr_info}")
|
259 |
|
|
|
261 |
# selected_kr_unique_ids comes directly from CheckboxGroup, should be a list of strings/values
|
262 |
if isinstance(selected_kr_unique_ids, list):
|
263 |
for kr_unique_id in selected_kr_unique_ids:
|
264 |
+
if kr_unique_id in kr_id_to_indices:
|
265 |
+
okr_idx, kr_idx_in_okr = kr_id_to_indices[kr_unique_id]
|
|
|
266 |
selected_krs_by_okr_idx[okr_idx].append(kr_idx_in_okr)
|
267 |
|
268 |
output_md_parts = []
|