Skip to content

Commit b7baae9

Browse files
authored
Merge pull request #608 from Fr4nc3/macae-rfp-af-101725
Macae rfp af 101725
2 parents 2f24bac + f6ed4d6 commit b7baae9

File tree

1 file changed

+65
-18
lines changed

1 file changed

+65
-18
lines changed

src/backend/v4/magentic_agents/foundry_agent.py

Lines changed: 65 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -100,27 +100,69 @@ async def _collect_tools(self) -> List:
100100
# -------------------------
101101
async def _create_azure_search_enabled_client(self):
102102
"""
103-
Create a server-side Azure AI agent with raw Azure AI Search tool and return an AzureAIAgentClient.
104-
This mirrors your example while fitting existing lifecycle.
103+
Create a server-side Azure AI agent with Azure AI Search raw tool.
105104
106-
If these assumptions differ, adjust accordingly.
105+
Requirements:
106+
- An Azure AI Project Connection (type=AZURE_AI_SEARCH) that contains either:
107+
a) API key + endpoint, OR
108+
b) Managed Identity (RBAC enabled on the Search service with Search Service Contributor + Search Index Data Reader).
109+
- search_config.index_name must exist in the Search service.
110+
- search_config.connection_name OR search_config.connection_id must reference the desired connection.
111+
112+
Returns:
113+
AzureAIAgentClient | None
107114
"""
115+
if not self.search:
116+
self.logger.error("Search configuration missing.")
117+
return None
108118

109-
connection_id = getattr(self.search, "connection_name", "")
119+
desired_connection_id = getattr(self.search, "connection_id", None)
120+
desired_connection_name = getattr(self.search, "connection_name", None)
110121
index_name = getattr(self.search, "index_name", "")
111122
query_type = getattr(self.search, "search_query_type", "vector")
112-
113-
# ai_search_conn_id = ""
114-
async for connection in self.client.project_client.connections.list():
115-
if connection.type == ConnectionType.AZURE_AI_SEARCH:
116-
connection_id = connection.id
117-
break
118-
if not connection_id or not index_name:
119-
self.logger.error(
120-
"Missing azure_search_connection_id or azure_search_index_name in search_config; aborting Azure Search path."
123+
124+
if not index_name:
125+
self.logger.error("index_name not provided in search_config; aborting Azure Search path.")
126+
return None
127+
128+
resolved_connection_id = None
129+
130+
try:
131+
async for connection in self.client.project_client.connections.list():
132+
if connection.type == ConnectionType.AZURE_AI_SEARCH:
133+
# Allow direct id override
134+
if desired_connection_id and connection.id == desired_connection_id:
135+
resolved_connection_id = connection.id
136+
break
137+
# Else match by name
138+
if desired_connection_name and connection.name == desired_connection_name:
139+
resolved_connection_id = connection.id
140+
break
141+
# Fallback: if no specific connection requested and none resolved yet, take the first
142+
if not desired_connection_id and not desired_connection_name and not resolved_connection_id:
143+
resolved_connection_id = connection.id
144+
# Do not break yet; we log but allow chance to find a name match later. If not, this stays.
145+
146+
if not resolved_connection_id:
147+
self.logger.error(
148+
"No Azure AI Search connection resolved. "
149+
"Provided connection_id=%s, connection_name=%s",
150+
desired_connection_id,
151+
desired_connection_name,
152+
)
153+
# return None
154+
155+
self.logger.info(
156+
"Using Azure AI Search connection (id=%s, requested_name=%s, requested_id=%s).",
157+
resolved_connection_id,
158+
desired_connection_name,
159+
desired_connection_id,
121160
)
161+
except Exception as ex:
162+
self.logger.error("Failed to enumerate connections: %s", ex)
122163
return None
123164

165+
# Create agent with raw tool
124166
try:
125167
azure_agent = await self.client.project_client.agents.create_agent(
126168
model=self.model_deployment_name,
@@ -134,19 +176,20 @@ async def _create_azure_search_enabled_client(self):
134176
"azure_ai_search": {
135177
"indexes": [
136178
{
137-
"index_connection_id": connection_id,
179+
"index_connection_id": resolved_connection_id,
138180
"index_name": index_name,
139-
"query_type": query_type,
181+
"query_type": "simple",
140182
}
141183
]
142184
}
143185
},
144186
)
145187
self._azure_server_agent_id = azure_agent.id
146188
self.logger.info(
147-
"Created Azure server agent with Azure AI Search tool (agent_id=%s, index=%s).",
189+
"Created Azure server agent with Azure AI Search tool (agent_id=%s, index=%s, query_type=%s).",
148190
azure_agent.id,
149191
index_name,
192+
query_type,
150193
)
151194

152195
chat_client = AzureAIAgentClient(
@@ -155,9 +198,13 @@ async def _create_azure_search_enabled_client(self):
155198
)
156199
return chat_client
157200
except Exception as ex:
158-
self.logger.error("Failed to create Azure Search enabled agent: %s", ex)
201+
self.logger.error(
202+
"Failed to create Azure Search enabled agent (connection_id=%s, index=%s): %s",
203+
resolved_connection_id,
204+
index_name,
205+
ex,
206+
)
159207
return None
160-
161208
# -------------------------
162209
# Agent lifecycle override
163210
# -------------------------

0 commit comments

Comments
 (0)