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
51 changes: 42 additions & 9 deletions src/agentscope_runtime/adapters/agentscope/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
ImageContent,
AudioContent,
DataContent,
McpCall,
McpCallOutput,
FunctionCall,
FunctionCallOutput,
MessageType,
Expand Down Expand Up @@ -78,8 +80,6 @@ async def adapt_agentscope_message_stream(
# Note: Tool use content only happens in the last of messages
tool_start = False

tool_use_messages_dict = {}

# Cache msg id
msg_id = msg.id

Expand Down Expand Up @@ -251,6 +251,17 @@ async def adapt_agentscope_message_stream(
elif element.get("type") == "tool_use": # Tool use
call_id = element.get("id")

if element.get("tool_type", "plugin") == "mcp":
msg_type = MessageType.MCP_TOOL_CALL
fc_cls = McpCall
fc_kwargs = {
"server_label": element.get("server_label"),
}
else:
msg_type = MessageType.PLUGIN_CALL
fc_cls = FunctionCall
fc_kwargs = {}

if last:
plugin_call_message = tool_use_messages_dict.get(
call_id,
Expand All @@ -260,16 +271,17 @@ async def adapt_agentscope_message_stream(
# Only one tool use message yields, we fake
# Build a new tool call message
plugin_call_message = Message(
type=MessageType.PLUGIN_CALL,
type=msg_type,
role="assistant",
)

data_delta_content = DataContent(
index=index,
data=FunctionCall(
data=fc_cls(
call_id=element.get("id"),
name=element.get("name"),
arguments="",
**fc_kwargs,
).model_dump(),
delta=True,
)
Expand All @@ -290,10 +302,11 @@ async def adapt_agentscope_message_stream(
json_str = json.dumps(element.get("input"))
data_delta_content = DataContent(
index=index,
data=FunctionCall(
data=fc_cls(
call_id=element.get("id"),
name=element.get("name"),
arguments=json_str,
**fc_kwargs,
).model_dump(),
delta=True,
)
Expand All @@ -316,16 +329,17 @@ async def adapt_agentscope_message_stream(
else:
# Build a new tool call message
plugin_call_message = Message(
type=MessageType.PLUGIN_CALL,
type=msg_type,
role="assistant",
)

data_delta_content = DataContent(
index=index,
data=FunctionCall(
data=fc_cls(
call_id=element.get("id"),
name=element.get("name"),
arguments="",
**fc_kwargs,
).model_dump(),
delta=True,
)
Expand All @@ -348,20 +362,39 @@ async def adapt_agentscope_message_stream(
] = plugin_call_message

elif element.get("type") == "tool_result": # Tool result
call_id = element.get("id")

plugin_call_message = tool_use_messages_dict.get(
call_id,
)
# Determine the output message type and class to use
# for the tool result message based on the type of
# the original tool call message.
msg_type = MessageType.PLUGIN_CALL_OUTPUT
fc_cls = FunctionCallOutput

if plugin_call_message:
if (
plugin_call_message.type
== MessageType.MCP_TOOL_CALL
):
msg_type = MessageType.MCP_TOOL_CALL_OUTPUT
fc_cls = McpCallOutput

json_str = json.dumps(
element.get("output"),
ensure_ascii=False,
)
data_delta_content = DataContent(
index=index,
data=FunctionCallOutput(
data=fc_cls(
call_id=element.get("id"),
name=element.get("name"),
output=json_str,
).model_dump(),
)
plugin_output_message = Message(
type=MessageType.PLUGIN_CALL_OUTPUT,
type=msg_type,
role="tool",
content=[data_delta_content],
)
Expand Down
3 changes: 3 additions & 0 deletions src/agentscope_runtime/engine/schemas/agent_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ class McpCallOutput(BaseModel):
call_id: str
"""The unique ID of the tool call."""

name: Optional[str] = None
"""The name of the tool call."""

output: str
"""The output from the tool call."""

Expand Down