Address code review feedback: improve error messages and add helper method
Co-authored-by: cauvang32 <113093128+cauvang32@users.noreply.github.com>
This commit is contained in:
@@ -175,6 +175,26 @@ class MessageHandler:
|
||||
logging.warning(f"Failed to initialize tiktoken encoder: {e}")
|
||||
self.token_encoder = None
|
||||
|
||||
def _build_claude_tool_result_message(self, tool_call_id: str, content: str) -> Dict[str, Any]:
|
||||
"""
|
||||
Build a tool result message for Claude API.
|
||||
|
||||
Args:
|
||||
tool_call_id: The ID of the tool call this result is for
|
||||
content: The result content from the tool execution
|
||||
|
||||
Returns:
|
||||
Dict: A properly formatted Claude tool result message
|
||||
"""
|
||||
return {
|
||||
"role": "user",
|
||||
"content": [{
|
||||
"type": "tool_result",
|
||||
"tool_use_id": tool_call_id,
|
||||
"content": content
|
||||
}]
|
||||
}
|
||||
|
||||
def _find_user_id_from_current_task(self):
|
||||
"""
|
||||
Utility method to find user_id from the current asyncio task.
|
||||
@@ -1643,10 +1663,9 @@ print("\\n=== Correlation Analysis ===")
|
||||
"content": claude_response.get("content", "")
|
||||
})
|
||||
for result in tool_results:
|
||||
updated_messages.append({
|
||||
"role": "user",
|
||||
"content": [{"type": "tool_result", "tool_use_id": result["tool_call_id"], "content": result["content"]}]
|
||||
})
|
||||
updated_messages.append(
|
||||
self._build_claude_tool_result_message(result["tool_call_id"], result["content"])
|
||||
)
|
||||
|
||||
# Make follow-up call
|
||||
follow_up_response = await call_claude_api(
|
||||
|
||||
@@ -196,9 +196,12 @@ def parse_data_url(data_url: str) -> Tuple[str, str]:
|
||||
|
||||
Returns:
|
||||
Tuple of (media_type, base64_data)
|
||||
|
||||
Raises:
|
||||
ValueError: If the data URL format is invalid
|
||||
"""
|
||||
if not data_url.startswith("data:"):
|
||||
raise ValueError("Not a data URL")
|
||||
raise ValueError(f"Not a data URL: expected 'data:' prefix, got '{data_url[:20]}...'")
|
||||
|
||||
# Remove "data:" prefix
|
||||
content = data_url[5:]
|
||||
@@ -206,7 +209,7 @@ def parse_data_url(data_url: str) -> Tuple[str, str]:
|
||||
# Split by semicolon and comma
|
||||
parts = content.split(";base64,")
|
||||
if len(parts) != 2:
|
||||
raise ValueError("Invalid data URL format")
|
||||
raise ValueError(f"Invalid data URL format: expected ';base64,' separator, got '{content[:50]}...'")
|
||||
|
||||
media_type = parts[0]
|
||||
base64_data = parts[1]
|
||||
|
||||
Reference in New Issue
Block a user