Skip to content

Commit 66f1d4a

Browse files
fix: Add kb_response back in
1 parent 78aff9f commit 66f1d4a

File tree

1 file changed

+51
-11
lines changed

1 file changed

+51
-11
lines changed

packages/slackBotFunction/app/slack/slack_events.py

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -172,27 +172,20 @@ def _create_feedback_blocks(
172172
retrieved_refs = citation.get("retrievedReferences", [])
173173
first_ref = retrieved_refs[0] if retrieved_refs else {}
174174

175-
title = first_ref.get("metadata", {}).get("title") or "Source"
175+
title = first_ref.get("metadata", {}).get("x-amz-bedrock-kb-source-uri").split("/")[-1] or f"Source {i + 1}"
176+
title_trunc = title[:100]
177+
176178
body = (
177179
first_ref.get("content", {}).get("text")
178180
or citation.get("generatedResponsePart", {}).get("textResponsePart", {}).get("text")
179181
or "No citation text available."
180182
)
181183

182-
title_trunc = title[:100]
183-
body_trunc = body[:300]
184-
185184
button = {
186185
"type": "button",
187186
"text": {"type": "plain_text", "text": title_trunc},
188187
"action_id": f"cite_{i}",
189-
"value": f"cite_{i}",
190-
"confirm": {
191-
"title": {"type": "plain_text", "text": title_trunc},
192-
"text": {"type": "mrkdwn", "text": body_trunc},
193-
"confirm": {"type": "plain_text", "text": "Close"},
194-
"deny": {"type": "plain_text", "text": "Cancel"},
195-
},
188+
"value": {"channel": channel, "mt": message_ts, "title": title, "body": body},
196189
}
197190
action_elements.append(button)
198191
blocks.append({"type": "actions", "block_id": "citation_block", "elements": action_elements})
@@ -281,6 +274,14 @@ def process_async_slack_action(body: Dict[str, Any], client: WebClient) -> None:
281274
elif action_id == "feedback_no":
282275
feedback_type = "negative"
283276
response_message = bot_messages.FEEDBACK_NEGATIVE_THANKS
277+
elif action_id.startswith("cite_"):
278+
citation_index = int(action_id.split("_")[1])
279+
feedback_type = "citation"
280+
conversation_key = feedback_data["ck"]
281+
message_ts = feedback_data.get("mt")
282+
title = feedback_data.get("title", "No title available.")
283+
body = feedback_data.get("body", "No citation text available.")
284+
open_citation(citation_index, conversation_key, message_ts, title, body, client)
284285
else:
285286
logger.error(f"Unknown feedback action: {action_id}")
286287
return
@@ -557,6 +558,45 @@ def store_feedback(
557558
logger.error(f"Error storing feedback: {e}", extra={"error": traceback.format_exc()})
558559

559560

561+
def open_citation(
562+
citation_index: int, conversation_key: str, message_ts: str | None, title: str, body: str, client
563+
) -> None:
564+
"""
565+
Open citation - placeholder for actual implementation
566+
"""
567+
logger.info(
568+
f"Opening citation {citation_index}", extra={"conversation_key": conversation_key, "message_ts": message_ts}
569+
)
570+
# Get Message
571+
try:
572+
response = client.conversations_history(channel=conversation_key, latest=message_ts, inclusive=True, limit=1)
573+
574+
message = response["messages"][0]
575+
logger.info("Message found for citation", extra={"message": message})
576+
577+
blocks = message.get("blocks", [])
578+
# Remove citation block (and divider), if it exists
579+
blocks = [block for block in blocks if block.get("block_id") not in ["citation_block", "citation_divider"]]
580+
# Add citation content before feedback block
581+
citation_block = {
582+
"type": "section",
583+
"text": {"type": "mrkdwn", "text": f"*{title}*\n{body}"},
584+
"block_id": "citation_block",
585+
}
586+
587+
feedback_block_index = next(
588+
(i for i, block in enumerate(blocks) if block.get("block_id") == "feedback_block"), len(blocks)
589+
)
590+
blocks.insert(feedback_block_index, {"type": "divider", "block_id": "citation_divider"})
591+
blocks.insert(feedback_block_index, citation_block)
592+
593+
# Update message with new blocks
594+
client.chat_update(channel=conversation_key, ts=message_ts, blocks=blocks)
595+
596+
except Exception as e:
597+
logger.error(f"Error retrieving message for citation: {e}", extra={"error": traceback.format_exc()})
598+
599+
560600
# ================================================================
561601
# Session management
562602
# ================================================================

0 commit comments

Comments
 (0)