diff --git a/claude-agent-sdk/listeners/events/app_mentioned.py b/claude-agent-sdk/listeners/events/app_mentioned.py index 9a497e4..48b8295 100644 --- a/claude-agent-sdk/listeners/events/app_mentioned.py +++ b/claude-agent-sdk/listeners/events/app_mentioned.py @@ -15,7 +15,11 @@ async def handle_app_mentioned( seeds into a channel the agent just created, which starts its first turn. """ channel_id = context.channel_id + files = event.get("files", []) text = event.get("text", "").replace(f"<@{context.bot_user_id}>", "").strip() + if files: + names = ", ".join(file.get("name") or file["id"] for file in files) + text = f"{text}\n(attachment: {names})".strip() session = await find_session(client, channel_id) if session is not None: diff --git a/claude-agent-sdk/listeners/events/message.py b/claude-agent-sdk/listeners/events/message.py index ab3abc1..f3f7618 100644 --- a/claude-agent-sdk/listeners/events/message.py +++ b/claude-agent-sdk/listeners/events/message.py @@ -13,11 +13,15 @@ async def handle_message( for bigger work. In a code channel the people in it are talking to the agent directly, so every message is a turn. Messages anywhere else are left alone. """ - # Ignore edits, joins, or bot posts. - if event.get("subtype") or event.get("bot_id"): + # Ignore edits, joins, or bot posts, but not shared files. + if event.get("subtype") not in (None, "file_share") or event.get("bot_id"): return mention = f"<@{context.bot_user_id}>" + files = event.get("files", []) text = event.get("text", "").strip() + if files: + names = ", ".join(file.get("name") or file["id"] for file in files) + text = f"{text}\n(attachment: {names})".strip() channel_id = context.channel_id if event.get("channel_type") == "im": diff --git a/claude-agent-sdk/tests/test_app_mentioned.py b/claude-agent-sdk/tests/test_app_mentioned.py index c8b2746..d11711a 100644 --- a/claude-agent-sdk/tests/test_app_mentioned.py +++ b/claude-agent-sdk/tests/test_app_mentioned.py @@ -112,6 +112,18 @@ async def test_empty_mention_explains_itself(self, client, context): assert "Mention me" in client.chat_postMessage.call_args.kwargs["text"] + async def test_mention_with_only_a_file_names_it(self, client, context): + session_store.put(CodeChannelSession(channel_id="C_CODE")) + + with patch( + "agent.turns.start_run", return_value=RunOutcome(history=[]) + ) as start_run: + await self.mention( + client, context, "", files=[{"id": "F123", "name": "trace.png"}] + ) + + assert start_run.call_args.args[1] == "<@U123>: (attachment: trace.png)" + async def test_failed_turn_in_a_code_channel_warns_top_level(self, client, context): session_store.put(CodeChannelSession(channel_id="C_CODE")) diff --git a/claude-agent-sdk/tests/test_message.py b/claude-agent-sdk/tests/test_message.py index ec3fef9..db2fdd9 100644 --- a/claude-agent-sdk/tests/test_message.py +++ b/claude-agent-sdk/tests/test_message.py @@ -38,6 +38,64 @@ async def test_message_with_a_mention_is_left_to_app_mention(self, client, conte start_run.assert_not_called() + async def test_file_shared_in_a_code_channel_runs_a_turn(self, client, context): + session_store.put(CodeChannelSession(channel_id="C_CODE")) + + with patch( + "agent.turns.start_run", return_value=RunOutcome(history=[]) + ) as start_run: + await self.send( + client, + context, + "why does this fail?", + subtype="file_share", + files=[{"id": "F123", "name": "trace.png"}], + ) + + assert ( + start_run.call_args.args[1] + == "<@U123>: why does this fail?\n(attachment: trace.png)" + ) + + async def test_file_shared_without_text_names_it(self, client, context): + session_store.put(CodeChannelSession(channel_id="C_CODE")) + + with patch( + "agent.turns.start_run", return_value=RunOutcome(history=[]) + ) as start_run: + await self.send( + client, + context, + "", + subtype="file_share", + files=[{"id": "F123", "name": "trace.png"}], + ) + + assert start_run.call_args.args[1] == "<@U123>: (attachment: trace.png)" + + async def test_edited_message_runs_no_turn(self, client, context): + session_store.put(CodeChannelSession(channel_id="C_CODE")) + + with patch("agent.turns.start_run") as start_run: + await self.send(client, context, "keep going", subtype="message_changed") + + start_run.assert_not_called() + + async def test_file_shared_by_an_app_runs_no_turn(self, client, context): + session_store.put(CodeChannelSession(channel_id="C_CODE")) + + with patch("agent.turns.start_run") as start_run: + await self.send( + client, + context, + "", + subtype="file_share", + bot_id="B123", + files=[{"id": "F123", "name": "trace.png"}], + ) + + start_run.assert_not_called() + async def test_messages_during_a_turn_run_together_next(self, client, context): session_store.put(CodeChannelSession(channel_id="C_CODE")) texts = [] diff --git a/openai-agents-sdk/listeners/events/app_mentioned.py b/openai-agents-sdk/listeners/events/app_mentioned.py index 9a497e4..48b8295 100644 --- a/openai-agents-sdk/listeners/events/app_mentioned.py +++ b/openai-agents-sdk/listeners/events/app_mentioned.py @@ -15,7 +15,11 @@ async def handle_app_mentioned( seeds into a channel the agent just created, which starts its first turn. """ channel_id = context.channel_id + files = event.get("files", []) text = event.get("text", "").replace(f"<@{context.bot_user_id}>", "").strip() + if files: + names = ", ".join(file.get("name") or file["id"] for file in files) + text = f"{text}\n(attachment: {names})".strip() session = await find_session(client, channel_id) if session is not None: diff --git a/openai-agents-sdk/listeners/events/message.py b/openai-agents-sdk/listeners/events/message.py index ab3abc1..f3f7618 100644 --- a/openai-agents-sdk/listeners/events/message.py +++ b/openai-agents-sdk/listeners/events/message.py @@ -13,11 +13,15 @@ async def handle_message( for bigger work. In a code channel the people in it are talking to the agent directly, so every message is a turn. Messages anywhere else are left alone. """ - # Ignore edits, joins, or bot posts. - if event.get("subtype") or event.get("bot_id"): + # Ignore edits, joins, or bot posts, but not shared files. + if event.get("subtype") not in (None, "file_share") or event.get("bot_id"): return mention = f"<@{context.bot_user_id}>" + files = event.get("files", []) text = event.get("text", "").strip() + if files: + names = ", ".join(file.get("name") or file["id"] for file in files) + text = f"{text}\n(attachment: {names})".strip() channel_id = context.channel_id if event.get("channel_type") == "im": diff --git a/openai-agents-sdk/tests/test_app_mentioned.py b/openai-agents-sdk/tests/test_app_mentioned.py index c8b2746..d11711a 100644 --- a/openai-agents-sdk/tests/test_app_mentioned.py +++ b/openai-agents-sdk/tests/test_app_mentioned.py @@ -112,6 +112,18 @@ async def test_empty_mention_explains_itself(self, client, context): assert "Mention me" in client.chat_postMessage.call_args.kwargs["text"] + async def test_mention_with_only_a_file_names_it(self, client, context): + session_store.put(CodeChannelSession(channel_id="C_CODE")) + + with patch( + "agent.turns.start_run", return_value=RunOutcome(history=[]) + ) as start_run: + await self.mention( + client, context, "", files=[{"id": "F123", "name": "trace.png"}] + ) + + assert start_run.call_args.args[1] == "<@U123>: (attachment: trace.png)" + async def test_failed_turn_in_a_code_channel_warns_top_level(self, client, context): session_store.put(CodeChannelSession(channel_id="C_CODE")) diff --git a/openai-agents-sdk/tests/test_message.py b/openai-agents-sdk/tests/test_message.py index ec3fef9..db2fdd9 100644 --- a/openai-agents-sdk/tests/test_message.py +++ b/openai-agents-sdk/tests/test_message.py @@ -38,6 +38,64 @@ async def test_message_with_a_mention_is_left_to_app_mention(self, client, conte start_run.assert_not_called() + async def test_file_shared_in_a_code_channel_runs_a_turn(self, client, context): + session_store.put(CodeChannelSession(channel_id="C_CODE")) + + with patch( + "agent.turns.start_run", return_value=RunOutcome(history=[]) + ) as start_run: + await self.send( + client, + context, + "why does this fail?", + subtype="file_share", + files=[{"id": "F123", "name": "trace.png"}], + ) + + assert ( + start_run.call_args.args[1] + == "<@U123>: why does this fail?\n(attachment: trace.png)" + ) + + async def test_file_shared_without_text_names_it(self, client, context): + session_store.put(CodeChannelSession(channel_id="C_CODE")) + + with patch( + "agent.turns.start_run", return_value=RunOutcome(history=[]) + ) as start_run: + await self.send( + client, + context, + "", + subtype="file_share", + files=[{"id": "F123", "name": "trace.png"}], + ) + + assert start_run.call_args.args[1] == "<@U123>: (attachment: trace.png)" + + async def test_edited_message_runs_no_turn(self, client, context): + session_store.put(CodeChannelSession(channel_id="C_CODE")) + + with patch("agent.turns.start_run") as start_run: + await self.send(client, context, "keep going", subtype="message_changed") + + start_run.assert_not_called() + + async def test_file_shared_by_an_app_runs_no_turn(self, client, context): + session_store.put(CodeChannelSession(channel_id="C_CODE")) + + with patch("agent.turns.start_run") as start_run: + await self.send( + client, + context, + "", + subtype="file_share", + bot_id="B123", + files=[{"id": "F123", "name": "trace.png"}], + ) + + start_run.assert_not_called() + async def test_messages_during_a_turn_run_together_next(self, client, context): session_store.put(CodeChannelSession(channel_id="C_CODE")) texts = [] diff --git a/pydantic-ai/listeners/events/app_mentioned.py b/pydantic-ai/listeners/events/app_mentioned.py index 9a497e4..48b8295 100644 --- a/pydantic-ai/listeners/events/app_mentioned.py +++ b/pydantic-ai/listeners/events/app_mentioned.py @@ -15,7 +15,11 @@ async def handle_app_mentioned( seeds into a channel the agent just created, which starts its first turn. """ channel_id = context.channel_id + files = event.get("files", []) text = event.get("text", "").replace(f"<@{context.bot_user_id}>", "").strip() + if files: + names = ", ".join(file.get("name") or file["id"] for file in files) + text = f"{text}\n(attachment: {names})".strip() session = await find_session(client, channel_id) if session is not None: diff --git a/pydantic-ai/listeners/events/message.py b/pydantic-ai/listeners/events/message.py index ab3abc1..f3f7618 100644 --- a/pydantic-ai/listeners/events/message.py +++ b/pydantic-ai/listeners/events/message.py @@ -13,11 +13,15 @@ async def handle_message( for bigger work. In a code channel the people in it are talking to the agent directly, so every message is a turn. Messages anywhere else are left alone. """ - # Ignore edits, joins, or bot posts. - if event.get("subtype") or event.get("bot_id"): + # Ignore edits, joins, or bot posts, but not shared files. + if event.get("subtype") not in (None, "file_share") or event.get("bot_id"): return mention = f"<@{context.bot_user_id}>" + files = event.get("files", []) text = event.get("text", "").strip() + if files: + names = ", ".join(file.get("name") or file["id"] for file in files) + text = f"{text}\n(attachment: {names})".strip() channel_id = context.channel_id if event.get("channel_type") == "im": diff --git a/pydantic-ai/tests/test_app_mentioned.py b/pydantic-ai/tests/test_app_mentioned.py index c8b2746..d11711a 100644 --- a/pydantic-ai/tests/test_app_mentioned.py +++ b/pydantic-ai/tests/test_app_mentioned.py @@ -112,6 +112,18 @@ async def test_empty_mention_explains_itself(self, client, context): assert "Mention me" in client.chat_postMessage.call_args.kwargs["text"] + async def test_mention_with_only_a_file_names_it(self, client, context): + session_store.put(CodeChannelSession(channel_id="C_CODE")) + + with patch( + "agent.turns.start_run", return_value=RunOutcome(history=[]) + ) as start_run: + await self.mention( + client, context, "", files=[{"id": "F123", "name": "trace.png"}] + ) + + assert start_run.call_args.args[1] == "<@U123>: (attachment: trace.png)" + async def test_failed_turn_in_a_code_channel_warns_top_level(self, client, context): session_store.put(CodeChannelSession(channel_id="C_CODE")) diff --git a/pydantic-ai/tests/test_message.py b/pydantic-ai/tests/test_message.py index ec3fef9..db2fdd9 100644 --- a/pydantic-ai/tests/test_message.py +++ b/pydantic-ai/tests/test_message.py @@ -38,6 +38,64 @@ async def test_message_with_a_mention_is_left_to_app_mention(self, client, conte start_run.assert_not_called() + async def test_file_shared_in_a_code_channel_runs_a_turn(self, client, context): + session_store.put(CodeChannelSession(channel_id="C_CODE")) + + with patch( + "agent.turns.start_run", return_value=RunOutcome(history=[]) + ) as start_run: + await self.send( + client, + context, + "why does this fail?", + subtype="file_share", + files=[{"id": "F123", "name": "trace.png"}], + ) + + assert ( + start_run.call_args.args[1] + == "<@U123>: why does this fail?\n(attachment: trace.png)" + ) + + async def test_file_shared_without_text_names_it(self, client, context): + session_store.put(CodeChannelSession(channel_id="C_CODE")) + + with patch( + "agent.turns.start_run", return_value=RunOutcome(history=[]) + ) as start_run: + await self.send( + client, + context, + "", + subtype="file_share", + files=[{"id": "F123", "name": "trace.png"}], + ) + + assert start_run.call_args.args[1] == "<@U123>: (attachment: trace.png)" + + async def test_edited_message_runs_no_turn(self, client, context): + session_store.put(CodeChannelSession(channel_id="C_CODE")) + + with patch("agent.turns.start_run") as start_run: + await self.send(client, context, "keep going", subtype="message_changed") + + start_run.assert_not_called() + + async def test_file_shared_by_an_app_runs_no_turn(self, client, context): + session_store.put(CodeChannelSession(channel_id="C_CODE")) + + with patch("agent.turns.start_run") as start_run: + await self.send( + client, + context, + "", + subtype="file_share", + bot_id="B123", + files=[{"id": "F123", "name": "trace.png"}], + ) + + start_run.assert_not_called() + async def test_messages_during_a_turn_run_together_next(self, client, context): session_store.put(CodeChannelSession(channel_id="C_CODE")) texts = []