Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion plugins/anthropic_code_suggester.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,19 @@ def execute(self, inputs: dict[str, Any]) -> dict[str, Any]:
],
)

# Extract text from content blocks (Anthropic API returns a list)
if message.content and isinstance(message.content, list) and len(message.content) > 0:
# Get the text from the first content block
suggestions_text = message.content[0].text if hasattr(message.content[0], 'text') else str(message.content[0])
else:
logger.error("Anthropic API response missing expected content.")
return {
"error": "Anthropic API response missing expected content.",
"status": "error"
}

return {
"suggestions": message.content,
"suggestions": suggestions_text,
"model": "claude-3-5-sonnet-20241022",
"status": "success",
}
Expand Down
33 changes: 29 additions & 4 deletions tests/test_anthropic_code_suggester.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import os
import sys
import unittest
from unittest.mock import MagicMock, patch

import pytest
from anthropic_code_suggester import AnthropicCodeSuggesterTool
# Mock the anthropic module before importing the tool
sys.modules['anthropic'] = MagicMock()

# Add parent directory to path to import plugins
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))

from plugins.anthropic_code_suggester import AnthropicCodeSuggesterTool


class TestAnthropicCodeSuggesterTool(unittest.TestCase):
Expand Down Expand Up @@ -38,7 +44,10 @@ def test_execute_success(self, mock_anthropic_class) -> None:
mock_client = MagicMock()
mock_anthropic_class.return_value = mock_client
mock_message = MagicMock()
mock_message.content = "Some suggestions"
# Mock the content as a list with a text block (matching Anthropic API structure)
mock_content_block = MagicMock()
mock_content_block.text = "Some suggestions"
mock_message.content = [mock_content_block]
mock_client.messages.create.return_value = mock_message

with patch.dict(os.environ, {"ANTHROPIC_API_KEY": "fake_key"}):
Expand Down Expand Up @@ -71,7 +80,7 @@ def foo(): pass
def test_execute_invalid_inputs(self, mock_anthropic_class) -> None:
with patch.dict(os.environ, {"ANTHROPIC_API_KEY": "fake_key"}):
tool = AnthropicCodeSuggesterTool()
with pytest.raises(ValueError):
with self.assertRaises(ValueError):
tool.execute({})

@patch("anthropic.Anthropic")
Expand All @@ -87,6 +96,22 @@ def test_execute_api_error(self, mock_anthropic_class) -> None:
assert result["status"] == "error"
assert "API error" in result["error"]

@patch("anthropic.Anthropic")
def test_execute_empty_content(self, mock_anthropic_class) -> None:
mock_client = MagicMock()
mock_anthropic_class.return_value = mock_client
mock_message = MagicMock()
# Mock empty content list
mock_message.content = []
mock_client.messages.create.return_value = mock_message

with patch.dict(os.environ, {"ANTHROPIC_API_KEY": "fake_key"}):
tool = AnthropicCodeSuggesterTool()
result = tool.execute({"code": "def foo(): pass"})

assert result["status"] == "error"
assert "missing expected content" in result["error"]


if __name__ == "__main__":
unittest.main()
Loading