Skip to content
Open
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
16 changes: 14 additions & 2 deletions python/packages/chatkit/agent_framework_chatkit/_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,18 @@ async def end_of_turn_to_input(self, item: EndOfTurnItem) -> Message | list[Mess
# End-of-turn is only used for UI hints - skip it
return None

def _generated_image_to_input(self, item: GeneratedImageItem) -> Message | None:
if item.image is None:
return None

return Message(
role="user",
contents=[
Content.from_text(text="The following image was generated by the agent."),
Content.from_uri(uri=item.image.url, media_type="image/*"),
],
)

async def _thread_item_to_input_item(
self,
item: ThreadItem,
Expand Down Expand Up @@ -534,8 +546,8 @@ async def _thread_item_to_input_item(
out = self.hidden_context_to_input(item) or []
return out if isinstance(out, list) else [out]
case GeneratedImageItem():
# TODO(evmattso): Implement generated image handling in a future PR
return []
out = self._generated_image_to_input(item)
return [] if out is None else [out]
case StructuredInputItem():
# TODO(evmattso): Implement structured input handling in a future PR
return []
Expand Down
21 changes: 21 additions & 0 deletions python/packages/chatkit/tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,27 @@ async def test_end_of_turn_to_input_returns_none(self, converter):

assert await converter.end_of_turn_to_input(end_item) is None

async def test_to_agent_input_with_generated_image(self, converter):
"""Test generated images are preserved as multimodal model context."""
from chatkit.types import GeneratedImage, GeneratedImageItem

input_item = GeneratedImageItem(
id="generated_1",
thread_id="thread_1",
created_at=datetime.now(),
type="generated_image",
image=GeneratedImage(id="image_1", url="https://example.com/image.png"),
)

result = await converter.to_agent_input(input_item)

assert len(result) == 1
assert result[0].role == "user"
assert [content.type for content in result[0].contents] == ["text", "uri"]
assert result[0].contents[0].text == "The following image was generated by the agent."
assert result[0].contents[1].uri == "https://example.com/image.png"
assert result[0].contents[1].media_type == "image/*"

async def test_to_agent_input_dispatches_supported_variants(self, converter):
"""Test thread item dispatch converts supported items and skips unsupported variants."""
from chatkit.types import (
Expand Down
Loading