@@ -158,69 +158,7 @@ def _create_feedback_blocks(
158158 feedback_value = json .dumps (feedback_data , separators = ("," , ":" ))
159159
160160 # Main response block
161- blocks = []
162- action_buttons = []
163-
164- # Create citation buttons
165- if citations is None or len (citations ) == 0 :
166- logger .info ("No citations" )
167- else :
168- invalid_body = "No document excerpt available."
169- for i , citation in enumerate (citations ):
170- logger .info ("Creating citation" , extra = {"Citation" : citation })
171- # Create citation blocks ["sourceNumber", "title", "link", "filename", "reference_text"]
172- title = citation .get ("title" ) or citation .get ("filename" ) or "Source"
173- body = citation .get ("reference_text" ) or invalid_body
174- citation_link = citation .get ("link" ) or ""
175- source_number = (citation .get ("source_number" , "0" )).replace ("\n " , "" )
176-
177- # If snippet is from dev table or is a single word, skip
178- if re .fullmatch (r"[A-Za-z0-9-_]+" , body .strip ()):
179- body = invalid_body
180-
181- # Buttons can only be 75 characters long, truncate to be safe
182- button_text = f"[{ source_number } ] { title } "
183- button = {
184- "type" : "button" ,
185- "text" : {
186- "type" : "plain_text" ,
187- "text" : button_text if len (button_text ) < 75 else f"{ button_text [:70 ]} ..." ,
188- },
189- "action_id" : f"cite_{ source_number } " ,
190- "value" : json .dumps (
191- {
192- ** feedback_data ,
193- "source_number" : source_number ,
194- "title" : title ,
195- "body" : body ,
196- "link" : citation_link ,
197- },
198- separators = ("," , ":" ),
199- ),
200- }
201- action_buttons .append (button )
202-
203- # Update inline citations
204- response_text = response_text .replace (
205- f"[cit_{ source_number } ]" ,
206- f"<{ citation_link } |[{ source_number } ]>" if citation_link else f"[{ source_number } ]" ,
207- )
208-
209- # Remove any citations that have not been returned
210- response_text = response_text .replace ("cit_" , "" )
211-
212- # Main body
213- blocks .append ({"type" : "section" , "text" : {"type" : "mrkdwn" , "text" : response_text }})
214-
215- # Citation action block
216- if action_buttons :
217- blocks .append (
218- {
219- "type" : "actions" ,
220- "block_id" : f"citation_actions_{ i } " ,
221- "elements" : action_buttons ,
222- }
223- )
161+ blocks = _create_response_body (citations , feedback_data , response_text )
224162
225163 # Feedback buttons
226164 blocks .append ({"type" : "divider" , "block_id" : "feedback-divider" })
@@ -250,6 +188,81 @@ def _create_feedback_blocks(
250188 return blocks
251189
252190
191+ def _create_response_body (citations : list [dict [str , str ]], feedback_data : dict [str , str ], response_text : str ):
192+ blocks = []
193+ action_buttons = []
194+
195+ # Create citation buttons
196+ if citations is None or len (citations ) == 0 :
197+ logger .info ("No citations" )
198+ else :
199+ for i , citation in enumerate (citations ):
200+ result = _create_citation (citation , feedback_data , response_text )
201+
202+ action_buttons .append (result [0 ])
203+ response_text = result [1 ]
204+
205+ # Remove any citations that have not been returned
206+ response_text = response_text .replace ("cit_" , "" )
207+
208+ # Main body
209+ blocks .append ({"type" : "section" , "text" : {"type" : "mrkdwn" , "text" : response_text }})
210+
211+ # Citation action block
212+ if action_buttons :
213+ blocks .append (
214+ {
215+ "type" : "actions" ,
216+ "block_id" : "citation_actions" ,
217+ "elements" : action_buttons ,
218+ }
219+ )
220+
221+ return blocks
222+
223+
224+ def _create_citation (citation : dict [str , str ], feedback_data : dict , response_text : str ):
225+ logger .info ("Creating citation" , extra = {"Citation" : citation })
226+ invalid_body = "No document excerpt available."
227+ action_buttons = []
228+
229+ # Create citation blocks ["sourceNumber", "title", "link", "filename", "reference_text"]
230+ title = citation .get ("title" ) or citation .get ("filename" ) or "Source"
231+ body = citation .get ("reference_text" ) or invalid_body
232+ citation_link = citation .get ("link" ) or ""
233+ source_number = (citation .get ("source_number" , "0" )).replace ("\n " , "" )
234+
235+ # Buttons can only be 75 characters long, truncate to be safe
236+ button_text = f"[{ source_number } ] { title } "
237+ button = {
238+ "type" : "button" ,
239+ "text" : {
240+ "type" : "plain_text" ,
241+ "text" : button_text if len (button_text ) < 75 else f"{ button_text [:70 ]} ..." ,
242+ },
243+ "action_id" : f"cite_{ source_number } " ,
244+ "value" : json .dumps (
245+ {
246+ ** feedback_data ,
247+ "source_number" : source_number ,
248+ "title" : title ,
249+ "body" : body ,
250+ "link" : citation_link ,
251+ },
252+ separators = ("," , ":" ),
253+ ),
254+ }
255+ action_buttons .append (button )
256+
257+ # Update inline citations
258+ response_text = response_text .replace (
259+ f"[cit_{ source_number } ]" ,
260+ f"<{ citation_link } |[{ source_number } ]>" if citation_link else f"[{ source_number } ]" ,
261+ )
262+
263+ return [* action_buttons , response_text ]
264+
265+
253266# ================================================================
254267# Main async event processing
255268# ================================================================
0 commit comments