-
Notifications
You must be signed in to change notification settings - Fork 418
Expand file tree
/
Copy pathconvert_script.py
More file actions
369 lines (339 loc) · 16.6 KB
/
convert_script.py
File metadata and controls
369 lines (339 loc) · 16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
"""This module loops through each file in a batch and processes it using the SQL agents.
It sets up a group chat for the agents, sends the source script to the chat, and processes
the responses from the agents. It also reports in real-time to the client using websockets
and updates the database with the results.
"""
import json
import logging
from api.status_updates import send_status_update
from common.models.api import (
FileProcessUpdate,
FileRecord,
FileResult,
LogType,
ProcessStatus,
)
from common.services.batch_service import BatchService
from semantic_kernel.contents import AuthorRole, ChatMessageContent
from sql_agents.agents.fixer.response import FixerResponse
from sql_agents.agents.migrator.response import MigratorResponse
from sql_agents.agents.picker.response import PickerResponse
from sql_agents.agents.semantic_verifier.response import SemanticVerifierResponse
from sql_agents.agents.syntax_checker.response import SyntaxCheckerResponse
from sql_agents.helpers.agents_manager import SqlAgents
from sql_agents.helpers.comms_manager import CommsManager
from sql_agents.helpers.models import AgentType
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
async def convert_script(
source_script,
file: FileRecord,
batch_service: BatchService,
sql_agents: SqlAgents,
# agent_config: AgentBaseConfig,
) -> str:
"""Use the team of agents to migrate a sql script."""
logger.info("Migrating query: %s\n", source_script)
# Setup the group chat for the agents
comms_manager = CommsManager(
sql_agents.idx_agents,
max_retries=5, # Retry up to 5 times for rate limits
initial_delay=1.0, # Start with 1 second delay
backoff_factor=2.0, # Double delay each retry
)
try:
# send websocket notification that file processing has started
send_status_update(
status=FileProcessUpdate(
file.batch_id,
file.file_id,
ProcessStatus.IN_PROGRESS,
AgentType.ALL,
"File processing started",
file_result=FileResult.INFO,
),
)
# orchestrate the chat
current_migration = "No migration"
is_complete: bool = False
while not is_complete:
await comms_manager.group_chat.add_chat_message(
ChatMessageContent(role=AuthorRole.USER, content=source_script)
)
carry_response = None
try:
async for response in comms_manager.async_invoke():
carry_response = response
if response.role == AuthorRole.ASSISTANT.value:
# Our process can terminate with either of these as the last response
# before syntax check
match response.name:
case AgentType.MIGRATOR.value:
result = MigratorResponse.model_validate_json(
response.content or ""
)
if result.input_error or result.rai_error:
# If there is an error in input, we end the processing here.
# We do not include this in termination to avoid forking the chat process.
description = {
"role": response.role,
"name": response.name or "*",
"content": response.content,
}
await batch_service.create_file_log(
str(file.file_id),
description,
current_migration,
LogType.ERROR,
AgentType(response.name),
AuthorRole(response.role),
)
current_migration = None
is_complete = True
break
case AgentType.SYNTAX_CHECKER.value:
result = SyntaxCheckerResponse.model_validate_json(
response.content.lower() or ""
)
# If there are no syntax errors, we can move to the semantic verifier
# We provide both scripts by injecting them into the chat history
if result.syntax_errors == []:
comms_manager.group_chat.history.add_message(
ChatMessageContent(
role=AuthorRole.USER,
name="candidate",
content=(
f"source_script: {source_script}, \n "
+ f"migrated_script: {current_migration}"
),
)
)
case AgentType.PICKER.value:
try:
result = PickerResponse.model_validate_json(
response.content or ""
)
except Exception as picker_exc:
logger.error("Picker agent returned invalid response: %s", picker_exc)
# Fallback to a valid PickerResponse with default values
result = PickerResponse(
conclusion="No valid candidate could be selected. The agent did not return a proper response.",
picked_query="",
summary="Picker agent encountered an error and could not select a query."
)
current_migration = result.picked_query
case AgentType.FIXER.value:
result = FixerResponse.model_validate_json(
response.content or ""
)
current_migration = result.fixed_query
case AgentType.SEMANTIC_VERIFIER.value:
logger.info(
"Semantic verifier agent response: %s", response.content
)
try:
result = SemanticVerifierResponse.model_validate_json(
response.content or ""
)
except Exception as verifier_exc:
logger.error("Semantic Verifier agent returned invalid response: %s", verifier_exc)
# Fallback to a valid SemanticVerifierResponse with default values
result = SemanticVerifierResponse(
judgement="Semantic verifier agent failed to return a valid response.",
differences=[],
summary="No summary available."
)
# If the semantic verifier agent returns a difference, we need to report it
if len(result.differences) > 0:
description = {
"role": AuthorRole.ASSISTANT.value,
"name": AgentType.SEMANTIC_VERIFIER.value,
"content": "\n".join(result.differences),
}
logger.info(
"Semantic verification had issues. Pass with warnings."
)
# send status update to the client of type in progress with agent status
send_status_update(
status=FileProcessUpdate(
file.batch_id,
file.file_id,
ProcessStatus.COMPLETED,
AgentType.SEMANTIC_VERIFIER,
result.summary,
FileResult.WARNING,
),
)
await batch_service.create_file_log(
str(file.file_id),
description,
current_migration,
LogType.WARNING,
AgentType.SEMANTIC_VERIFIER,
AuthorRole.ASSISTANT,
)
elif response == "":
# If the semantic verifier agent returns an empty response
logger.info(
"Semantic verification had no return value. Pass with warnings."
)
# send status update to the client of type in progress with agent status
send_status_update(
status=FileProcessUpdate(
file.batch_id,
file.file_id,
ProcessStatus.COMPLETED,
AgentType.SEMANTIC_VERIFIER,
"No return value from semantic verifier agent.",
FileResult.WARNING,
),
)
await batch_service.create_file_log(
str(file.file_id),
"No return value from semantic verifier agent.",
current_migration,
LogType.WARNING,
AgentType.SEMANTIC_VERIFIER,
AuthorRole.ASSISTANT,
)
description = {
"role": response.role,
"name": response.name or "*",
"content": response.content,
}
logger.info(description)
try:
parsed_content = json.loads(response.content or "{}")
except json.JSONDecodeError:
logger.warning("Invalid JSON from agent: %s", response.content)
parsed_content = {
"input_summary": "",
"candidates": [],
"summary": "",
"input_error": "",
"rai_error": "Invalid JSON from agent.",
}
# Send status update using safe fallback values
send_status_update(
status=FileProcessUpdate(
file.batch_id,
file.file_id,
ProcessStatus.IN_PROGRESS,
AgentType(response.name),
parsed_content.get("summary", ""),
FileResult.INFO,
),
)
# end
await batch_service.create_file_log(
str(file.file_id),
description,
current_migration,
LogType.INFO,
AgentType(response.name),
AuthorRole(response.role),
)
except Exception as e:
logger.error("Error during comms_manager.async_invoke(): %s", str(e))
# Log the error to the batch service for tracking
await batch_service.create_file_log(
str(file.file_id),
f"Critical error during agent communication: {str(e)}",
current_migration,
LogType.ERROR,
AgentType.ALL,
AuthorRole.ASSISTANT,
)
# Send error status update
send_status_update(
status=FileProcessUpdate(
file.batch_id,
file.file_id,
ProcessStatus.COMPLETED,
AgentType.ALL,
f"Processing failed: {str(e)}",
FileResult.ERROR,
),
)
is_complete = True
break
if comms_manager.group_chat.is_complete:
is_complete = True
migrated_query = current_migration
# Handle the case where migration failed and current_migration is None
if current_migration is None:
logger.info("# Migration failed - no valid migration produced.")
return ""
is_valid = await validate_migration(
migrated_query, carry_response, file, batch_service
)
if not is_valid:
logger.info("# Migration failed.")
return ""
logger.info("# Migration complete.")
logger.info("Final query: %s\n", migrated_query)
logger.info(
"Analysis of source and migrated queries:\n%s", "semantic verifier response"
)
return migrated_query
finally:
# Clean up threads and communication resources - guaranteed to run
try:
await comms_manager.cleanup()
logger.debug("Thread cleanup completed successfully for file %s", file.file_id)
except Exception as cleanup_exc:
logger.error("Error during thread cleanup for file %s: %s", file.file_id, cleanup_exc)
async def validate_migration(
migrated_query: str,
carry_response: ChatMessageContent,
file: FileRecord,
batch_service: BatchService,
) -> bool:
"""Make sure the migrated query was returned"""
if not migrated_query:
# send status update to the client of type failed
send_status_update(
status=FileProcessUpdate(
file.batch_id,
file.file_id,
ProcessStatus.COMPLETED,
file_result=FileResult.ERROR,
),
)
await batch_service.create_file_log(
str(file.file_id),
"No migrated query returned. Migration failed.",
"",
LogType.ERROR,
(
AgentType.SEMANTIC_VERIFIER
if carry_response is None
else AgentType(carry_response.name)
),
(
AuthorRole.ASSISTANT
if carry_response is None
else AuthorRole(carry_response.role)
),
)
logger.error("No migrated query returned. Migration failed.")
return False
# send status update to the client of type completed / success
send_status_update(
status=FileProcessUpdate(
batch_id=file.batch_id,
file_id=file.file_id,
process_status=ProcessStatus.COMPLETED,
agent_type=AgentType.ALL,
file_result=FileResult.SUCCESS,
),
)
await batch_service.create_file_log(
file_id=str(file.file_id),
description="Migration completed successfully.",
last_candidate=migrated_query,
log_type=LogType.SUCCESS,
agent_type=AgentType.ALL,
author_role=AuthorRole.ASSISTANT,
)
return True