Skip to content
Merged
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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ framework/component-config
components/model-clients
components/model-context
components/tools
components/workbench
components/command-line-code-executors
```

Expand Down
22 changes: 22 additions & 0 deletions python/packages/autogen-core/src/autogen_core/tools/_workbench.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,28 @@
is_error: bool = False
"""Whether the tool execution resulted in an error."""

def to_text(self, replace_image: str | None = None) -> str:
"""
Convert the result to a text string.

Args:
replace_image (str | None): The string to replace the image content with.
If None, the image content will be included in the text as base64 string.

Returns:
str: The text representation of the result.
"""
parts: List[str] = []
for content in self.result:
if isinstance(content, TextResultContent):
parts.append(content.content)
elif isinstance(content, ImageResultContent):
if replace_image is not None:
parts.append(replace_image)

Check warning on line 72 in python/packages/autogen-core/src/autogen_core/tools/_workbench.py

View check run for this annotation

Codecov / codecov/patch

python/packages/autogen-core/src/autogen_core/tools/_workbench.py#L70-L72

Added lines #L70 - L72 were not covered by tests
else:
parts.append(f"[Image: {content.content.to_base64()}]")

Check warning on line 74 in python/packages/autogen-core/src/autogen_core/tools/_workbench.py

View check run for this annotation

Codecov / codecov/patch

python/packages/autogen-core/src/autogen_core/tools/_workbench.py#L74

Added line #L74 was not covered by tests
return "\n".join(parts)


class Workbench(ABC, ComponentBase[BaseModel]):
"""
Expand Down
4 changes: 4 additions & 0 deletions python/packages/autogen-core/tests/test_workbench.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@ def test_tool_func_2(x: Annotated[int, "The number to add 2."]) -> int:
assert result_1.name == "test_tool_1"
assert result_1.result[0].type == "TextResultContent"
assert result_1.result[0].content == "10"
assert result_1.to_text() == "10"
assert result_1.is_error is False

# Call tool with error
result_2 = await workbench.call_tool("test_tool_2", {"x": 5})
assert result_2.name == "test_tool_2"
assert result_2.result[0].type == "TextResultContent"
assert result_2.result[0].content == "This is a test error"
assert result_2.to_text() == "This is a test error"
assert result_2.is_error is True

# Save state.
Expand Down Expand Up @@ -109,11 +111,13 @@ def test_tool_func_2(x: Annotated[int, "The number to add 2."]) -> int:
assert result_1.name == "test_tool_1"
assert result_1.result[0].type == "TextResultContent"
assert result_1.result[0].content == "10"
assert result_1.to_text() == "10"
assert result_1.is_error is False

# Call tool with error
result_2 = await new_workbench.call_tool("test_tool_2", {"x": 5})
assert result_2.name == "test_tool_2"
assert result_2.result[0].type == "TextResultContent"
assert result_2.result[0].content == "This is a test error"
assert result_2.to_text() == "This is a test error"
assert result_2.is_error is True
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
) as session:
yield session
elif isinstance(server_params, SseServerParams):
async with sse_client(**server_params.model_dump()) as (read, write):
async with sse_client(**server_params.model_dump(exclude={"type"})) as (read, write):

Check warning on line 26 in python/packages/autogen-ext/src/autogen_ext/tools/mcp/_session.py

View check run for this annotation

Codecov / codecov/patch

python/packages/autogen-ext/src/autogen_ext/tools/mcp/_session.py#L26

Added line #L26 was not covered by tests
async with ClientSession(read_stream=read, write_stream=write) as session:
yield session
Loading