77import logging
88
99from api .status_updates import send_status_update
10+ from app import get_sql_agents , update_agent_config
1011
11- from azure .identity .aio import DefaultAzureCredential
12-
13- from common .config .config import app_config
1412from common .models .api import (
1513 FileProcessUpdate ,
1614 FileRecord ,
2321
2422from fastapi import HTTPException
2523
26-
27- from semantic_kernel .agents .azure_ai .azure_ai_agent import AzureAIAgent # pylint: disable=E0611
2824from semantic_kernel .contents import AuthorRole
2925from semantic_kernel .exceptions .service_exceptions import ServiceResponseException
3026
31- from sql_agents .agents .agent_config import AgentBaseConfig
3227from sql_agents .convert_script import convert_script
33- from sql_agents .helpers .agents_manager import SqlAgents
3428from sql_agents .helpers .models import AgentType
3529from sql_agents .helpers .utils import is_text
3630
@@ -57,22 +51,20 @@ async def process_batch_async(
5751 except Exception as exc :
5852 logger .error ("Error updating batch status. %s" , exc )
5953
60- # Add client and auto cleanup
61- async with (
62- DefaultAzureCredential () as creds ,
63- AzureAIAgent .create_client (credential = creds , endpoint = app_config .ai_project_endpoint ) as client ,
64- ):
65-
66- # setup all agent settings and agents per batch
67- agent_config = AgentBaseConfig (
68- project_client = client , sql_from = convert_from , sql_to = convert_to
69- )
70- sql_agents = await SqlAgents .create (agent_config )
71-
72- # Walk through each file name and retrieve it from blob storage
73- # Send file to the agents for processing
74- # Send status update to the client of type in progress, completed, or failed
75- for file in batch_files :
54+ # Get the global SQL agents instance
55+ sql_agents = get_sql_agents ()
56+ if not sql_agents :
57+ logger .error ("SQL agents not initialized. Application may not have started properly." )
58+ await batch_service .update_batch (batch_id , ProcessStatus .FAILED )
59+ return
60+
61+ # Update agent configuration for this batch's conversion requirements
62+ await update_agent_config (convert_from , convert_to )
63+
64+ # Walk through each file name and retrieve it from blob storage
65+ # Send file to the agents for processing
66+ # Send status update to the client of type in progress, completed, or failed
67+ for file in batch_files :
7668 # Get the file from blob storage
7769 try :
7870 file_record = FileRecord .fromdb (file )
@@ -145,16 +137,14 @@ async def process_batch_async(
145137 # insert data base write to file record stating invalid file
146138 await process_error (exc , file_record , batch_service )
147139
148- # Cleanup the agents
149- await sql_agents .delete_agents ()
150-
151- try :
152- await batch_service .batch_files_final_update (batch_id )
153- await batch_service .update_batch (batch_id , ProcessStatus .COMPLETED )
154- except Exception as exc :
155- await batch_service .update_batch (batch_id , ProcessStatus .FAILED )
156- logger .error ("Error updating batch status. %s" , exc )
157- logger .info ("Batch processing complete." )
140+ # Update batch status to completed or failed
141+ try :
142+ await batch_service .batch_files_final_update (batch_id )
143+ await batch_service .update_batch (batch_id , ProcessStatus .COMPLETED )
144+ except Exception as exc :
145+ await batch_service .update_batch (batch_id , ProcessStatus .FAILED )
146+ logger .error ("Error updating batch status. %s" , exc )
147+ logger .info ("Batch processing complete." )
158148
159149
160150async def process_error (
0 commit comments