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
4 changes: 4 additions & 0 deletions claude-agent-sdk/listeners/events/app_mentioned.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 6 additions & 2 deletions claude-agent-sdk/listeners/events/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
12 changes: 12 additions & 0 deletions claude-agent-sdk/tests/test_app_mentioned.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))

Expand Down
58 changes: 58 additions & 0 deletions claude-agent-sdk/tests/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
4 changes: 4 additions & 0 deletions openai-agents-sdk/listeners/events/app_mentioned.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 6 additions & 2 deletions openai-agents-sdk/listeners/events/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
12 changes: 12 additions & 0 deletions openai-agents-sdk/tests/test_app_mentioned.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))

Expand Down
58 changes: 58 additions & 0 deletions openai-agents-sdk/tests/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
4 changes: 4 additions & 0 deletions pydantic-ai/listeners/events/app_mentioned.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 6 additions & 2 deletions pydantic-ai/listeners/events/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
12 changes: 12 additions & 0 deletions pydantic-ai/tests/test_app_mentioned.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))

Expand Down
58 changes: 58 additions & 0 deletions pydantic-ai/tests/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down