Skip to content

Commit 8cf7a4e

Browse files
feat: use multiple citation actions
1 parent 7058ead commit 8cf7a4e

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

packages/slackBotFunction/app/slack/slack_events.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -168,17 +168,16 @@ def _create_feedback_blocks(
168168
for i, citation in enumerate(citations):
169169
logger.info("Creating citation", extra={"Citation": citation})
170170
# Create citation blocks
171-
# keys = ["source number", "title", "filename", "reference text", "link"]
172-
title = (
173-
citation.get("title") or citation.get("filename") or f"Source {citation.get('source number', i + 1)}"
174-
)
171+
# keys = ["source number", "title", "link", "filename", "reference text"]
172+
title = citation.get("title") or citation.get("filename") or "Source"
175173
body = citation.get("reference text") or "No citation text available."
176174
citation_link = citation.get("link") or ""
175+
citation_number = citation.get("source number", 0)
177176

178177
button = {
179178
"type": "button",
180179
"text": {"type": "plain_text", "text": title},
181-
"action_id": "cite",
180+
"action_id": f"cite_{citation_number}",
182181
"value": json.dumps(
183182
{**feedback_data, "title": title, "body": body, "link": citation_link},
184183
separators=(",", ":"),
@@ -187,12 +186,17 @@ def _create_feedback_blocks(
187186
action_buttons.append(button)
188187

189188
# Update inline citations
190-
citation_number = citation.get("source number", str(i + 1))
191189
response_text = response_text.replace(
192190
f"[cit_{citation_number}]",
193191
f"<{citation_link}|[{citation_number}]>" if citation_link else f"[{citation_number}]",
194192
)
195193

194+
# Remove any citations that have not been returned
195+
response_text = response_text.replace(
196+
f"[cit_{citation_number}]",
197+
"",
198+
)
199+
196200
# Main body
197201
blocks.append({"type": "section", "text": {"type": "mrkdwn", "text": response_text}})
198202

@@ -271,6 +275,7 @@ def process_feedback_event(
271275

272276

273277
def process_async_slack_action(body: Dict[str, Any], client: WebClient) -> None:
278+
logger.info("Processing slack action", extra={"body": body})
274279
try:
275280
# Extract necessary information from the action payload
276281
message = body[
@@ -282,14 +287,14 @@ def process_async_slack_action(body: Dict[str, Any], client: WebClient) -> None:
282287

283288
# Check if this is the latest message in the conversation
284289
conversation_key = action_data["ck"]
285-
message_ts = action_data.get("mt")
290+
message_ts = action_data["mt"]
286291

287292
# Required for updating
288293
channel_id = body["channel"]["id"]
289294
timestamp = body["message"]["ts"]
290295

291-
# Check if the action is for a citation
292-
if action_id == "cite":
296+
# Check if the action is for a citation (safely)
297+
if str(action_id or "").startswith("cite"):
293298
# Update message to include citation content
294299
open_citation(channel_id, timestamp, message, action_data, client)
295300
return
@@ -595,7 +600,7 @@ def store_feedback(
595600
logger.error(f"Error storing feedback: {e}", extra={"error": traceback.format_exc()})
596601

597602

598-
def open_citation(channel: str, timestamp: str, message: Any, params: Dict[str, Any], client) -> None:
603+
def open_citation(channel: str, timestamp: str, message: Any, params: Dict[str, Any], client: WebClient) -> None:
599604
"""
600605
Open citation - update/ replace message to include citation content
601606
"""

packages/slackBotFunction/app/slack/slack_handlers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ def setup_handlers(app: App) -> None:
4141
app.event("message")(ack=respond_to_events, lazy=[unified_message_handler])
4242
app.action("feedback_yes")(ack=respond_to_action, lazy=[feedback_handler])
4343
app.action("feedback_no")(ack=respond_to_action, lazy=[feedback_handler])
44-
app.action("cite")(ack=respond_to_action, lazy=[feedback_handler])
44+
for i in range(1, 10):
45+
app.action(f"cite_{i}")(ack=respond_to_action, lazy=[feedback_handler])
4546

4647

4748
# ================================================================

0 commit comments

Comments
 (0)