Skip to content

Commit 401010a

Browse files
committed
more typing
1 parent a9f5cca commit 401010a

File tree

4 files changed

+44
-27
lines changed

4 files changed

+44
-27
lines changed

packages/slackBotFunction/app/slack/slack_events.py

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import time
88
import traceback
99
import json
10-
from typing import Any, Dict
10+
from typing import Any, Dict, Tuple
1111
from botocore.exceptions import ClientError
1212
from slack_sdk import WebClient
1313
from app.core.config import (
@@ -34,7 +34,7 @@
3434
# ================================================================
3535

3636

37-
def cleanup_previous_unfeedback_qa(conversation_key, current_message_ts, session_data):
37+
def cleanup_previous_unfeedback_qa(conversation_key: str, current_message_ts: str, session_data: Dict[str, Any]):
3838
"""Delete previous Q&A pair if no feedback received using atomic operation"""
3939
try:
4040
previous_message_ts = session_data.get("latest_message_ts")
@@ -57,7 +57,9 @@ def cleanup_previous_unfeedback_qa(conversation_key, current_message_ts, session
5757
logger.error("Error cleaning up unfeedback Q&A", extra={"error": traceback.format_exc()})
5858

5959

60-
def store_qa_pair(conversation_key, user_query, bot_response, message_ts, session_id, user_id):
60+
def store_qa_pair(
61+
conversation_key: str, user_query: str, bot_response: str, message_ts: str, session_id: str, user_id: str
62+
):
6163
"""
6264
Store Q&A pair for feedback correlation
6365
"""
@@ -79,7 +81,7 @@ def store_qa_pair(conversation_key, user_query, bot_response, message_ts, sessio
7981
logger.error("Failed to store Q&A pair", extra={"error": traceback.format_exc()})
8082

8183

82-
def _mark_qa_feedback_received(conversation_key, message_ts):
84+
def _mark_qa_feedback_received(conversation_key: str, message_ts: str):
8385
"""
8486
Mark Q&A record as having received feedback to prevent deletion
8587
"""
@@ -98,7 +100,7 @@ def _mark_qa_feedback_received(conversation_key, message_ts):
98100
# ================================================================
99101

100102

101-
def _extract_conversation_context(event: Dict[str, Any]):
103+
def _extract_conversation_context(event: Dict[str, Any]) -> Tuple[str, str, str | None]:
102104
"""Extract conversation key and thread context from event"""
103105
channel = event["channel"]
104106
# Determine conversation context: DM vs channel thread
@@ -110,7 +112,15 @@ def _extract_conversation_context(event: Dict[str, Any]):
110112

111113

112114
def _handle_session_management(
113-
conversation_key, session_data, session_id, kb_response, user_id, channel, thread_ts, context_type, message_ts
115+
conversation_key: str,
116+
session_data: Dict[str, Any],
117+
session_id: str,
118+
kb_response: Dict[str, Any],
119+
user_id: str,
120+
channel: str,
121+
thread_ts: str,
122+
context_type: str,
123+
message_ts: str,
114124
):
115125
"""Handle Bedrock session creation and cleanup"""
116126
# Handle conversation session management
@@ -131,7 +141,9 @@ def _handle_session_management(
131141
update_session_latest_message(conversation_key, message_ts)
132142

133143

134-
def _create_feedback_blocks(response_text, conversation_key, channel, message_ts, thread_ts):
144+
def _create_feedback_blocks(
145+
response_text: str, conversation_key: str, channel: str, message_ts: str, thread_ts: str
146+
) -> list[dict[str, Any]]:
135147
"""Create Slack blocks with feedback buttons"""
136148
# Create compact feedback payload for button actions
137149
feedback_data = {"ck": conversation_key, "ch": channel, "mt": message_ts}
@@ -281,14 +293,14 @@ def log_query_stats(user_query: str, event: Dict[str, Any], channel: str, client
281293

282294

283295
def store_feedback(
284-
conversation_key,
285-
feedback_type,
286-
user_id,
287-
channel_id,
296+
conversation_key: str,
297+
feedback_type: str,
298+
user_id: str,
299+
channel_id: str,
288300
client: WebClient,
289-
thread_ts=None,
290-
message_ts=None,
291-
feedback_text=None,
301+
thread_ts: str = None,
302+
message_ts: str = None,
303+
feedback_text: str = None,
292304
):
293305
"""
294306
Store user feedback with reference to Q&A record
@@ -373,15 +385,15 @@ def store_feedback(
373385
# ================================================================
374386

375387

376-
def get_conversation_session(conversation_key):
388+
def get_conversation_session(conversation_key: str) -> str | None:
377389
"""
378390
Get existing Bedrock session for this conversation
379391
"""
380392
session_data = get_conversation_session_data(conversation_key)
381393
return session_data.get("session_id") if session_data else None
382394

383395

384-
def get_conversation_session_data(conversation_key):
396+
def get_conversation_session_data(conversation_key: str) -> Dict[str, Any]:
385397
"""
386398
Get full session data for this conversation
387399
"""
@@ -396,7 +408,7 @@ def get_conversation_session_data(conversation_key):
396408
return None
397409

398410

399-
def get_latest_message_ts(conversation_key):
411+
def get_latest_message_ts(conversation_key: str) -> str:
400412
"""
401413
Get latest message timestamp from session
402414
"""
@@ -411,7 +423,12 @@ def get_latest_message_ts(conversation_key):
411423

412424

413425
def store_conversation_session(
414-
conversation_key, session_id, user_id, channel_id, thread_ts=None, latest_message_ts=None
426+
conversation_key: str,
427+
session_id: str,
428+
user_id: str,
429+
channel_id: str,
430+
thread_ts: str = None,
431+
latest_message_ts: str = None,
415432
):
416433
"""
417434
Store new Bedrock session for conversation memory
@@ -440,7 +457,7 @@ def store_conversation_session(
440457
logger.error("Error storing session", extra={"error": traceback.format_exc()})
441458

442459

443-
def update_session_latest_message(conversation_key, message_ts):
460+
def update_session_latest_message(conversation_key: str, message_ts: str):
444461
"""
445462
Update session with latest message timestamp
446463
"""

packages/slackBotFunction/app/slack/slack_handlers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import traceback
1313
from typing import Any, Dict
1414
from botocore.exceptions import ClientError
15-
from slack_bolt import Ack
15+
from slack_bolt import Ack, App
1616
from slack_sdk import WebClient
1717
from app.core.config import (
1818
BOT_MESSAGES,
@@ -40,7 +40,7 @@
4040

4141

4242
@lru_cache
43-
def setup_handlers(app):
43+
def setup_handlers(app: App):
4444
"""Register handlers. Intentionally minimal—no branching here."""
4545
app.event("app_mention")(mention_handler)
4646
app.event("message")(unified_message_handler)

packages/slackBotFunction/app/utils/handler_utils.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import time
77
import json
88
import traceback
9-
from typing import Any, Dict
9+
from typing import Any, Dict, Tuple
1010
import boto3
1111
from botocore.exceptions import ClientError
1212
from slack_sdk import WebClient
@@ -23,7 +23,7 @@
2323
logger = get_logger()
2424

2525

26-
def is_duplicate_event(event_id):
26+
def is_duplicate_event(event_id: str) -> bool:
2727
"""
2828
Check if we've already processed this event using DynamoDB conditional writes
2929
@@ -98,7 +98,7 @@ def trigger_pull_request_processing(pull_request_id: str, event: Dict[str, Any],
9898
raise e
9999

100100

101-
def is_latest_message(conversation_key, message_ts):
101+
def is_latest_message(conversation_key: str, message_ts: str) -> bool:
102102
"""Check if message_ts is the latest bot message using session data"""
103103
try:
104104
response = get_state_information({"pk": conversation_key, "sk": constants.SESSION_SK})
@@ -111,7 +111,7 @@ def is_latest_message(conversation_key, message_ts):
111111
return False
112112

113113

114-
def gate_common(event: Dict[str, Any], body: Dict[str, Any]):
114+
def gate_common(event: Dict[str, Any], body: Dict[str, Any]) -> str | None:
115115
"""
116116
Apply common early checks that are shared across handlers.
117117
@@ -153,7 +153,7 @@ def extract_pull_request_id(text: str) -> str:
153153
return pr_number, rest_text
154154

155155

156-
def conversation_key_and_root(event: Dict[str, Any]):
156+
def conversation_key_and_root(event: Dict[str, Any]) -> Tuple[str, str]:
157157
"""
158158
Build a stable conversation scope and its root timestamp.
159159

packages/slackBotFunction/tests/test_query_reformulator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def test_reformulate_query_returns_string(mock_invoke_model: Mock, mock_load_pro
3030
assert isinstance(result, str)
3131
assert len(result) > 0
3232
assert result == "foo"
33-
mock_load_prompt.assert_called_once_with(ANY, "test-prompt", "DRAFT")
33+
mock_load_prompt.assert_called_once_with("test-prompt", "DRAFT")
3434
mock_invoke_model.assert_called_once_with(
3535
prompt="Test reformat. How do I use EPS?", model_id="test-model", client=ANY
3636
)

0 commit comments

Comments
 (0)