|
1 | 1 | from collections import defaultdict |
2 | 2 | from collections.abc import Callable |
| 3 | +from typing import Any |
3 | 4 | from uuid import UUID |
4 | 5 |
|
5 | 6 | from pydantic import BaseModel |
6 | 7 | from pydantic import ConfigDict |
7 | 8 | from sqlalchemy.orm import Session |
8 | 9 |
|
9 | | -from onyx.configs.app_configs import MAX_FEDERATED_CHUNKS |
10 | 10 | from onyx.configs.constants import DocumentSource |
11 | 11 | from onyx.configs.constants import FederatedConnectorSource |
12 | 12 | from onyx.context.search.models import InferenceChunk |
|
18 | 18 | from onyx.db.models import FederatedConnector__DocumentSet |
19 | 19 | from onyx.db.slack_bot import fetch_slack_bots |
20 | 20 | from onyx.federated_connectors.factory import get_federated_connector |
| 21 | +from onyx.federated_connectors.interfaces import FederatedConnector |
21 | 22 | from onyx.onyxbot.slack.models import SlackContext |
22 | 23 | from onyx.utils.logger import setup_logger |
23 | 24 |
|
@@ -86,15 +87,31 @@ def get_federated_retrieval_functions( |
86 | 87 | credentials, |
87 | 88 | ) |
88 | 89 |
|
89 | | - federated_retrieval_infos_slack.append( |
90 | | - FederatedRetrievalInfo( |
91 | | - retrieval_function=lambda query: connector.search( |
| 90 | + # Capture variables by value to avoid lambda closure issues |
| 91 | + bot_token = tenant_slack_bot.bot_token |
| 92 | + |
| 93 | + def create_slack_retrieval_function( |
| 94 | + conn: FederatedConnector, |
| 95 | + token: str, |
| 96 | + ctx: SlackContext, |
| 97 | + bot_tok: str, |
| 98 | + ) -> Callable[[SearchQuery], list[InferenceChunk]]: |
| 99 | + def retrieval_fn(query: SearchQuery) -> list[InferenceChunk]: |
| 100 | + return conn.search( |
92 | 101 | query, |
93 | 102 | {}, # Empty entities for Slack context |
94 | | - access_token=access_token, |
95 | | - limit=MAX_FEDERATED_CHUNKS, |
96 | | - slack_event_context=slack_context, |
97 | | - bot_token=tenant_slack_bot.bot_token, |
| 103 | + access_token=token, |
| 104 | + limit=None, # Let connector use its own max_messages_per_query config |
| 105 | + slack_event_context=ctx, |
| 106 | + bot_token=bot_tok, |
| 107 | + ) |
| 108 | + |
| 109 | + return retrieval_fn |
| 110 | + |
| 111 | + federated_retrieval_infos_slack.append( |
| 112 | + FederatedRetrievalInfo( |
| 113 | + retrieval_function=create_slack_retrieval_function( |
| 114 | + connector, access_token, slack_context, bot_token |
98 | 115 | ), |
99 | 116 | source=FederatedConnectorSource.FEDERATED_SLACK, |
100 | 117 | ) |
@@ -158,22 +175,33 @@ def get_federated_retrieval_functions( |
158 | 175 | if document_set_names and not document_set_associations: |
159 | 176 | continue |
160 | 177 |
|
161 | | - if document_set_associations: |
162 | | - entities = document_set_associations[0].entities |
163 | | - else: |
164 | | - entities = {} |
| 178 | + # Only use connector-level config (no junction table entities) |
| 179 | + entities = oauth_token.federated_connector.config or {} |
165 | 180 |
|
166 | 181 | connector = get_federated_connector( |
167 | 182 | oauth_token.federated_connector.source, |
168 | 183 | oauth_token.federated_connector.credentials, |
169 | 184 | ) |
| 185 | + |
| 186 | + # Capture variables by value to avoid lambda closure issues |
| 187 | + access_token = oauth_token.token |
| 188 | + |
| 189 | + def create_retrieval_function( |
| 190 | + conn: FederatedConnector, |
| 191 | + ent: dict[str, Any], |
| 192 | + token: str, |
| 193 | + ) -> Callable[[SearchQuery], list[InferenceChunk]]: |
| 194 | + return lambda query: conn.search( |
| 195 | + query, |
| 196 | + ent, |
| 197 | + access_token=token, |
| 198 | + limit=None, # Let connector use its own max_messages_per_query config |
| 199 | + ) |
| 200 | + |
170 | 201 | federated_retrieval_infos.append( |
171 | 202 | FederatedRetrievalInfo( |
172 | | - retrieval_function=lambda query: connector.search( |
173 | | - query, |
174 | | - entities, |
175 | | - access_token=oauth_token.token, |
176 | | - limit=MAX_FEDERATED_CHUNKS, |
| 203 | + retrieval_function=create_retrieval_function( |
| 204 | + connector, entities, access_token |
177 | 205 | ), |
178 | 206 | source=oauth_token.federated_connector.source, |
179 | 207 | ) |
|
0 commit comments