Refactor AI resolver to use async context manager for client connection

- Updated the AI resolver to utilize an async context manager for handling client connections, improving resource management and consistency with the codebase.
- Maintained existing functionality for querying and processing AI responses while enhancing error handling.
This commit is contained in:
AndyMik90
2025-12-16 14:19:30 +01:00
parent bf787ade0b
commit 579ea40bd2
+13 -11
View File
@@ -633,19 +633,21 @@ def create_claude_resolver() -> AIResolver:
) )
try: try:
# Use the same pattern as qa/reviewer.py - NO context manager # Use async context manager to handle connect/disconnect
await client.query(user) # This is the standard pattern used throughout the codebase
async with client:
await client.query(user)
response_text = "" response_text = ""
async for msg in client.receive_response(): async for msg in client.receive_response():
msg_type = type(msg).__name__ msg_type = type(msg).__name__
if msg_type == "AssistantMessage" and hasattr(msg, "content"): if msg_type == "AssistantMessage" and hasattr(msg, "content"):
for block in msg.content: for block in msg.content:
if hasattr(block, "text"): if hasattr(block, "text"):
response_text += block.text response_text += block.text
logger.info(f"AI merge response: {len(response_text)} chars") logger.info(f"AI merge response: {len(response_text)} chars")
return response_text return response_text
except Exception as e: except Exception as e:
logger.error(f"Claude SDK call failed: {e}") logger.error(f"Claude SDK call failed: {e}")