41
41
HIGHLIGHT_END_CHAR = "\ue001 "
42
42
43
43
44
+ def _should_skip_channel (
45
+ channel_id : str ,
46
+ allowed_private_channel : str | None ,
47
+ bot_token : str | None ,
48
+ access_token : str ,
49
+ include_dm : bool ,
50
+ ) -> bool :
51
+ """
52
+ Determine if a channel should be skipped if in bot context. When an allowed_private_channel is passed in,
53
+ all other private channels are filtered out except that specific one.
54
+ """
55
+ if bot_token and not include_dm :
56
+ try :
57
+ # Use bot token if available (has full permissions), otherwise fall back to user token
58
+ token_to_use = bot_token or access_token
59
+ channel_client = WebClient (token = token_to_use )
60
+ channel_info = channel_client .conversations_info (channel = channel_id )
61
+
62
+ if isinstance (channel_info .data , dict ) and not _is_public_channel (
63
+ channel_info .data
64
+ ):
65
+ # This is a private channel - filter it out
66
+ if channel_id != allowed_private_channel :
67
+ logger .debug (
68
+ f"Skipping message from private channel { channel_id } "
69
+ f"(not the allowed private channel: { allowed_private_channel } )"
70
+ )
71
+ return True
72
+ except Exception as e :
73
+ logger .warning (
74
+ f"Could not determine channel type for { channel_id } , filtering out: { e } "
75
+ )
76
+ return True
77
+ return False
78
+
79
+
44
80
def build_slack_queries (query : SearchQuery , llm : LLM ) -> list [str ]:
45
81
# get time filter
46
82
time_filter = ""
@@ -82,12 +118,6 @@ def _is_public_channel(channel_info: dict[str, Any]) -> bool:
82
118
is_channel and not is_private and not is_group and not is_mpim and not is_im
83
119
)
84
120
85
- # Add detailed logging for debugging
86
- logger .debug (
87
- f"Channel info: is_channel={ is_channel } , is_private={ is_private } , "
88
- f"is_group={ is_group } , is_mpim={ is_mpim } , is_im={ is_im } , is_public={ is_public } "
89
- )
90
-
91
121
return is_public
92
122
93
123
@@ -96,9 +126,9 @@ def query_slack(
96
126
original_query : SearchQuery ,
97
127
access_token : str ,
98
128
limit : int | None = None ,
99
- allowed_private_channel : str | None = None , # Add allowed private channel parameter
100
- bot_token : str | None = None , # Add bot token parameter for channel info calls
101
- include_dm : bool = False , # Whether to include direct messages
129
+ allowed_private_channel : str | None = None ,
130
+ bot_token : str | None = None ,
131
+ include_dm : bool = False ,
102
132
) -> list [SlackMessage ]:
103
133
# query slack
104
134
slack_client = WebClient (token = access_token )
@@ -151,72 +181,11 @@ def query_slack(
151
181
continue
152
182
153
183
# Apply channel filtering if needed
154
- if allowed_private_channel is not None :
155
- # Private channel context: only allow the specific private channel + public channels
156
- if channel_id == allowed_private_channel :
157
- # This is the allowed private channel - keep it
158
- pass
159
- else :
160
- # Check if this is a public channel
161
- try :
162
- # Use bot token if available (has full permissions), otherwise fall back to user token
163
- token_to_use = bot_token if bot_token else access_token
164
- channel_client = WebClient (token = token_to_use )
165
- channel_info = channel_client .conversations_info (channel = channel_id )
166
-
167
- if isinstance (channel_info .data , dict ) and _is_public_channel (
168
- channel_info .data
169
- ):
170
- # This is a public channel - keep it
171
- pass
172
- else :
173
- # This is another private channel - filter it out
174
- filtered_count += 1
175
- logger .debug (
176
- f"Skipping message from private channel { channel_id } "
177
- f"(not the allowed private channel: { allowed_private_channel } )"
178
- )
179
- continue
180
- except Exception as e :
181
- logger .warning (
182
- f"Could not determine channel type for { channel_id } , filtering out: { e } "
183
- )
184
- filtered_count += 1
185
- continue
186
- elif include_dm :
187
- # Include direct messages - no filtering needed
188
- pass
189
- elif (
190
- allowed_private_channel is None and not include_dm and bot_token is not None
184
+ if _should_skip_channel (
185
+ channel_id , allowed_private_channel , bot_token , access_token , include_dm
191
186
):
192
- # Slack bot context (has bot_token but no specific channel context): apply default filtering (only public channels)
193
- try :
194
- # Use bot token if available (has full permissions), otherwise fall back to user token
195
- token_to_use = bot_token if bot_token else access_token
196
- channel_client = WebClient (token = token_to_use )
197
- channel_info = channel_client .conversations_info (channel = channel_id )
198
-
199
- if isinstance (channel_info .data , dict ) and _is_public_channel (
200
- channel_info .data
201
- ):
202
- # This is a public channel - keep it
203
- pass
204
- else :
205
- # This is a private channel - filter it out
206
- filtered_count += 1
207
- logger .debug (
208
- f"Skipping message from private channel { channel_id } (only public channels allowed in Slack bot context)"
209
- )
210
- continue
211
- except Exception as e :
212
- logger .warning (
213
- f"Could not determine channel type for { channel_id } , filtering out: { e } "
214
- )
215
- filtered_count += 1
216
- continue
217
- else :
218
- # Web chat federated search: no filtering - include all channels
219
- pass
187
+ filtered_count += 1
188
+ continue
220
189
221
190
# generate thread id and document id
222
191
thread_id = (
@@ -453,22 +422,14 @@ def slack_retrieval(
453
422
if not slack_messages :
454
423
return []
455
424
456
- # Check if we're in a bot context by looking at the access token prefix
457
- if access_token .startswith ("xoxp-" ):
458
- logger .info (
459
- "Bot context detected (user OAuth token): skipping thread context to avoid additional scope requirements"
460
- )
461
- # Use original message text without thread context
462
- else :
463
- thread_texts : list [str ] = run_functions_tuples_in_parallel (
464
- [
465
- (get_contextualized_thread_text , (slack_message , access_token ))
466
- for slack_message in slack_messages
467
- ]
468
- )
469
- for slack_message , thread_text in zip (slack_messages , thread_texts ):
470
- slack_message .text = thread_text
471
- # else: use original message text without thread context
425
+ thread_texts : list [str ] = run_functions_tuples_in_parallel (
426
+ [
427
+ (get_contextualized_thread_text , (slack_message , access_token ))
428
+ for slack_message in slack_messages
429
+ ]
430
+ )
431
+ for slack_message , thread_text in zip (slack_messages , thread_texts ):
432
+ slack_message .text = thread_text
472
433
473
434
# get the highlighted texts from shortest to longest
474
435
highlighted_texts : set [str ] = set ()
0 commit comments