chore: import upstream snapshot with attribution
CodeQL / Analyze (csharp) (push) Waiting to run
CodeQL / Analyze (python) (push) Waiting to run

This commit is contained in:
wehub-resource-sync
2026-07-13 13:21:23 +08:00
commit b957a53def
5423 changed files with 863745 additions and 0 deletions
@@ -0,0 +1,85 @@
# Copyright (c) Microsoft. All rights reserved.
from abc import ABC, abstractmethod
from collections.abc import AsyncIterable
from typing import TYPE_CHECKING, Any
from semantic_kernel.utils.feature_stage_decorator import experimental
if TYPE_CHECKING:
from semantic_kernel.agents.agent import Agent
from semantic_kernel.contents.chat_message_content import ChatMessageContent
@experimental
class AgentChannel(ABC):
"""Defines the communication protocol for a particular Agent type.
An agent provides it own AgentChannel via CreateChannel.
"""
@abstractmethod
async def receive(
self,
history: list["ChatMessageContent"],
) -> None:
"""Receive the conversation messages.
Used when joining a conversation and also during each agent interaction.
Args:
history: The history of messages in the conversation.
"""
...
@abstractmethod
def invoke(
self,
agent: "Agent",
**kwargs: Any,
) -> AsyncIterable[tuple[bool, "ChatMessageContent"]]:
"""Perform a discrete incremental interaction between a single Agent and AgentChat.
Args:
agent: The agent to interact with.
kwargs: The keyword arguments.
Returns:
An async iterable of a bool, ChatMessageContent.
"""
...
@abstractmethod
def invoke_stream(
self,
agent: "Agent",
messages: "list[ChatMessageContent]",
**kwargs: Any,
) -> AsyncIterable["ChatMessageContent"]:
"""Perform a discrete incremental stream interaction between a single Agent and AgentChat.
Args:
agent: The agent to interact with.
messages: The history of messages in the conversation.
kwargs: The keyword arguments.
Returns:
An async iterable ChatMessageContent.
"""
...
@abstractmethod
def get_history(
self,
) -> AsyncIterable["ChatMessageContent"]:
"""Retrieve the message history specific to this channel.
Returns:
An async iterable of ChatMessageContent.
"""
...
@abstractmethod
async def reset(self) -> None:
"""Reset any persistent state associated with the channel."""
...
@@ -0,0 +1,217 @@
# Copyright (c) Microsoft. All rights reserved.
import logging
import sys
from collections.abc import AsyncIterable
from typing import TYPE_CHECKING, Any, ClassVar
if sys.version_info >= (3, 12):
from typing import override # pragma: no cover
else:
from typing_extensions import override # pragma: no cover
from semantic_kernel.agents.agent import Agent
from semantic_kernel.agents.channels.agent_channel import AgentChannel
from semantic_kernel.contents.chat_history import ChatHistory
from semantic_kernel.contents.chat_message_content import ChatMessageContent
from semantic_kernel.contents.streaming_chat_message_content import StreamingChatMessageContent
from semantic_kernel.contents.utils.author_role import AuthorRole
from semantic_kernel.exceptions.agent_exceptions import AgentChatException
from semantic_kernel.utils.feature_stage_decorator import experimental
if TYPE_CHECKING:
from semantic_kernel.agents.bedrock.bedrock_agent import BedrockAgentThread
logger = logging.getLogger(__name__)
@experimental
class BedrockAgentChannel(AgentChannel, ChatHistory):
"""An AgentChannel for a BedrockAgent that is based on a ChatHistory.
The chat history will override the session state when invoking the agent.
This channel allows Bedrock agents to interact with other types of agents in Semantic Kernel in an AgentGroupChat.
However, since Bedrock agents require the chat history to alternate between user and agent messages, this channel
will preprocess the chat history to ensure that it meets the requirements of the Bedrock agent. When an invalid
pattern is detected, the channel will insert a placeholder user or assistant message to ensure that the chat history
alternates between user and agent messages.
"""
thread: "BedrockAgentThread"
MESSAGE_PLACEHOLDER: ClassVar[str] = "[SILENCE]"
@override
async def invoke(self, agent: "Agent", **kwargs: Any) -> AsyncIterable[tuple[bool, ChatMessageContent]]:
"""Perform a discrete incremental interaction between a single Agent and AgentChat.
Args:
agent: The agent to interact with.
kwargs: Additional keyword arguments.
Returns:
An async iterable of ChatMessageContent with a boolean indicating if the
message should be visible external to the agent.
"""
from semantic_kernel.agents.bedrock.bedrock_agent import BedrockAgent
if not isinstance(agent, BedrockAgent):
raise AgentChatException(f"Agent is not of the expected type {type(BedrockAgent)}.")
if not self.messages:
# This is not supposed to happen, as the channel won't get invoked
# before it has received messages. This is just extra safety.
raise AgentChatException("No chat history available.")
# Preprocess chat history
await self._ensure_history_alternates()
await self._ensure_last_message_is_user()
async for response in agent.invoke(
messages=self.messages[-1].content,
thread=self.thread,
sessionState=await self._parse_chat_history_to_session_state(),
):
# All messages from Bedrock agents are user facing, i.e., function calls are not returned as messages
self.messages.append(response.message)
yield True, response.message
@override
async def invoke_stream(
self,
agent: "Agent",
messages: list[ChatMessageContent],
**kwargs: Any,
) -> AsyncIterable[ChatMessageContent]:
"""Perform a streaming interaction between a single Agent and AgentChat.
Args:
agent: The agent to interact with.
messages: The history of messages in the conversation.
kwargs: Additional keyword arguments.
Returns:
An async iterable of ChatMessageContent.
"""
from semantic_kernel.agents.bedrock.bedrock_agent import BedrockAgent
if not isinstance(agent, BedrockAgent):
raise AgentChatException(f"Agent is not of the expected type {type(BedrockAgent)}.")
if not self.messages:
raise AgentChatException("No chat history available.")
# Preprocess chat history
await self._ensure_history_alternates()
await self._ensure_last_message_is_user()
full_message: list[StreamingChatMessageContent] = []
async for response_chunk in agent.invoke_stream(
messages=self.messages[-1].content,
thread=self.thread,
sessionState=await self._parse_chat_history_to_session_state(),
):
yield response_chunk.message
full_message.append(response_chunk.message)
messages.append(
ChatMessageContent(
role=AuthorRole.ASSISTANT,
content="".join([message.content for message in full_message]),
name=agent.name,
inner_content=full_message,
ai_model_id=agent.agent_model.foundation_model,
)
)
@override
async def receive(
self,
history: list[ChatMessageContent],
) -> None:
"""Receive the conversation messages.
Bedrock requires the chat history to alternate between user and agent messages.
Thus, when receiving the history, the message sequence will be mutated by inserting
empty agent or user messages as needed.
Args:
history: The history of messages in the conversation.
"""
for incoming_message in history:
if not self.messages or self.messages[-1].role != incoming_message.role:
self.messages.append(incoming_message)
else:
self.messages.append(
ChatMessageContent(
role=AuthorRole.ASSISTANT if incoming_message.role == AuthorRole.USER else AuthorRole.USER,
content=self.MESSAGE_PLACEHOLDER,
)
)
self.messages.append(incoming_message)
@override
async def get_history( # type: ignore
self,
) -> AsyncIterable[ChatMessageContent]:
"""Retrieve the message history specific to this channel.
Returns:
An async iterable of ChatMessageContent.
"""
for message in reversed(self.messages):
yield message
@override
async def reset(self) -> None:
"""Reset the channel state."""
self.messages.clear()
# region chat history preprocessing and parsing
async def _ensure_history_alternates(self):
"""Ensure that the chat history alternates between user and agent messages."""
if not self.messages or len(self.messages) == 1:
return
current_index = 1
while current_index < len(self.messages):
if self.messages[current_index].role == self.messages[current_index - 1].role:
self.messages.insert(
current_index,
ChatMessageContent(
role=AuthorRole.ASSISTANT
if self.messages[current_index].role == AuthorRole.USER
else AuthorRole.USER,
content=self.MESSAGE_PLACEHOLDER,
),
)
current_index += 2
else:
current_index += 1
async def _ensure_last_message_is_user(self):
"""Ensure that the last message in the chat history is a user message."""
if self.messages and self.messages[-1].role == AuthorRole.ASSISTANT:
self.messages.append(
ChatMessageContent(
role=AuthorRole.USER,
content=self.MESSAGE_PLACEHOLDER,
)
)
async def _parse_chat_history_to_session_state(self) -> dict[str, Any]:
"""Parse the chat history to a session state."""
session_state: dict[str, Any] = {"conversationHistory": {"messages": []}}
if len(self.messages) > 1:
# We don't take the last message as it needs to be sent separately in another parameter
for message in self.messages[:-1]:
if message.role not in [AuthorRole.USER, AuthorRole.ASSISTANT]:
logger.debug(f"Skipping message with unsupported role: {message}")
continue
session_state["conversationHistory"]["messages"].append({
"content": [{"text": message.content}],
"role": message.role.value,
})
return session_state
# endregion
@@ -0,0 +1,171 @@
# Copyright (c) Microsoft. All rights reserved.
import sys
from collections import deque
from collections.abc import AsyncIterable
from copy import deepcopy
if sys.version_info >= (3, 12):
from typing import override # pragma: no cover
else:
from typing_extensions import override # pragma: no cover
from typing import TYPE_CHECKING, Any, ClassVar, Deque
from semantic_kernel.agents.channels.agent_channel import AgentChannel
from semantic_kernel.contents import ChatMessageContent
from semantic_kernel.contents.chat_history import ChatHistory
from semantic_kernel.contents.function_call_content import FunctionCallContent
from semantic_kernel.contents.function_result_content import FunctionResultContent
from semantic_kernel.contents.image_content import ImageContent
from semantic_kernel.contents.streaming_text_content import StreamingTextContent
from semantic_kernel.contents.text_content import TextContent
from semantic_kernel.utils.feature_stage_decorator import experimental
if TYPE_CHECKING:
from semantic_kernel.agents.agent import Agent
from semantic_kernel.agents.chat_completion.chat_completion_agent import ChatHistoryAgentThread
from semantic_kernel.contents.chat_history import ChatHistory
from semantic_kernel.contents.streaming_chat_message_content import StreamingChatMessageContent
@experimental
class ChatHistoryChannel(AgentChannel, ChatHistory):
"""An AgentChannel specialization for that acts upon a ChatHistoryHandler."""
thread: "ChatHistoryAgentThread"
ALLOWED_CONTENT_TYPES: ClassVar[tuple[type, ...]] = (
ImageContent,
FunctionCallContent,
FunctionResultContent,
StreamingTextContent,
TextContent,
)
@override
async def invoke(
self,
agent: "Agent",
**kwargs: Any,
) -> AsyncIterable[tuple[bool, ChatMessageContent]]:
"""Perform a discrete incremental interaction between a single Agent and AgentChat.
Args:
agent: The agent to interact with.
kwargs: The keyword arguments.
Returns:
An async iterable of ChatMessageContent.
"""
message_count = len(self.messages)
mutated_history = set()
message_queue: Deque[ChatMessageContent] = deque()
async for response in agent.invoke(
messages=self.messages[-1],
thread=self.thread,
):
# Capture all messages that have been included in the mutated history.
for message_index in range(message_count, len(self.messages)):
mutated_message = self.messages[message_index]
mutated_history.add(mutated_message)
message_queue.append(mutated_message)
# Update the message count pointer to reflect the current history.
message_count = len(self.messages)
# Avoid duplicating any message included in the mutated history and also returned by the enumeration result.
if response.message not in mutated_history:
self.messages.append(response.message)
message_queue.append(response.message)
# Dequeue the next message to yield.
yield_message = message_queue.popleft()
yield (
self._is_message_visible(message=yield_message, message_queue_count=len(message_queue)),
yield_message,
)
# Dequeue any remaining messages to yield.
while message_queue:
yield_message = message_queue.popleft()
yield (
self._is_message_visible(message=yield_message, message_queue_count=len(message_queue)),
yield_message,
)
@override
async def invoke_stream(
self, agent: "Agent", messages: list[ChatMessageContent], **kwargs: Any
) -> AsyncIterable["StreamingChatMessageContent"]:
"""Perform a discrete incremental stream interaction between a single Agent and AgentChat.
Args:
agent: The agent to interact with.
messages: The history of messages in the conversation.
kwargs: The keyword arguments
Returns:
An async iterable of ChatMessageContent.
"""
message_count = len(self.messages)
async for response_message in agent.invoke_stream(
messages=self.messages[-1],
thread=self.thread,
):
if response_message.message.content:
yield response_message.message
for message_index in range(message_count, len(self.messages)):
messages.append(self.messages[message_index])
def _is_message_visible(self, message: ChatMessageContent, message_queue_count: int) -> bool:
"""Determine if a message is visible to the user."""
return (
not any(isinstance(item, (FunctionCallContent, FunctionResultContent)) for item in message.items)
or message_queue_count == 0
)
@override
async def receive(
self,
history: list[ChatMessageContent],
) -> None:
"""Receive the conversation messages.
Do not include messages that only contain file references.
Args:
history: The history of messages in the conversation.
"""
filtered_history: list[ChatMessageContent] = []
for message in history:
new_message = deepcopy(message)
if new_message.items is None:
new_message.items = []
allowed_items = [item for item in new_message.items if isinstance(item, self.ALLOWED_CONTENT_TYPES)]
if not allowed_items:
continue
new_message.items.clear()
new_message.items.extend(allowed_items)
filtered_history.append(new_message)
self.messages.extend(filtered_history)
@override
async def get_history( # type: ignore
self,
) -> AsyncIterable[ChatMessageContent]:
"""Retrieve the message history specific to this channel.
Returns:
An async iterable of ChatMessageContent.
"""
for message in reversed(self.messages):
yield message
@override
async def reset(self) -> None:
"""Reset the channel state."""
self.messages.clear()
@@ -0,0 +1,121 @@
# Copyright (c) Microsoft. All rights reserved.
import sys
from collections.abc import AsyncIterable
from typing import TYPE_CHECKING, Any
if sys.version_info >= (3, 12):
from typing import override # pragma: no cover
else:
from typing_extensions import override # pragma: no cover
from openai import AsyncOpenAI
from semantic_kernel.agents.channels.agent_channel import AgentChannel
from semantic_kernel.agents.open_ai.assistant_content_generation import create_chat_message, generate_message_content
from semantic_kernel.agents.open_ai.assistant_thread_actions import AssistantThreadActions
from semantic_kernel.contents.chat_message_content import ChatMessageContent
from semantic_kernel.contents.function_call_content import FunctionCallContent
from semantic_kernel.exceptions.agent_exceptions import AgentChatException
from semantic_kernel.utils.feature_stage_decorator import experimental
if TYPE_CHECKING:
from semantic_kernel.agents.agent import Agent
@experimental
class OpenAIAssistantChannel(AgentChannel):
"""OpenAI Assistant Channel."""
def __init__(self, client: AsyncOpenAI, thread_id: str) -> None:
"""Initialize the OpenAI Assistant Channel."""
self.client = client
self.thread_id = thread_id
@override
async def receive(self, history: list["ChatMessageContent"]) -> None:
"""Receive the conversation messages.
Args:
history: The conversation messages.
"""
for message in history:
if any(isinstance(item, FunctionCallContent) for item in message.items):
continue
await create_chat_message(self.client, self.thread_id, message)
@override
async def invoke(self, agent: "Agent", **kwargs: Any) -> AsyncIterable[tuple[bool, "ChatMessageContent"]]:
"""Invoke the agent.
Args:
agent: The agent to invoke.
kwargs: The keyword arguments.
Yields:
tuple[bool, ChatMessageContent]: The conversation messages.
"""
from semantic_kernel.agents.open_ai.openai_assistant_agent import OpenAIAssistantAgent
if not isinstance(agent, OpenAIAssistantAgent):
raise AgentChatException(f"Agent is not of the expected type {type(OpenAIAssistantAgent)}.")
async for is_visible, message in AssistantThreadActions.invoke(agent=agent, thread_id=self.thread_id, **kwargs):
yield is_visible, message
@override
async def invoke_stream(
self, agent: "Agent", messages: list[ChatMessageContent], **kwargs: Any
) -> AsyncIterable["ChatMessageContent"]:
"""Invoke the agent stream.
Args:
agent: The agent to invoke.
messages: The conversation messages.
kwargs: The keyword arguments.
Yields:
tuple[bool, StreamingChatMessageContent]: The conversation messages.
"""
from semantic_kernel.agents.open_ai.openai_assistant_agent import OpenAIAssistantAgent
if not isinstance(agent, OpenAIAssistantAgent):
raise AgentChatException(f"Agent is not of the expected type {type(OpenAIAssistantAgent)}.")
async for message in AssistantThreadActions.invoke_stream(
agent=agent, thread_id=self.thread_id, output_messages=messages, **kwargs
):
yield message
@override
async def get_history(self) -> AsyncIterable["ChatMessageContent"]:
"""Get the conversation history.
Yields:
ChatMessageContent: The conversation history.
"""
agent_names: dict[str, Any] = {}
thread_messages = await self.client.beta.threads.messages.list(
thread_id=self.thread_id, limit=100, order="desc"
)
for message in thread_messages.data:
assistant_name = None
if message.assistant_id and message.assistant_id not in agent_names:
agent = await self.client.beta.assistants.retrieve(message.assistant_id)
if agent.name:
agent_names[message.assistant_id] = agent.name
assistant_name = agent_names.get(message.assistant_id) if message.assistant_id else message.assistant_id
content: ChatMessageContent = generate_message_content(str(assistant_name), message)
if len(content.items) > 0:
yield content
@override
async def reset(self) -> None:
"""Reset the agent's thread."""
try:
await self.client.beta.threads.delete(thread_id=self.thread_id)
except Exception as e:
raise AgentChatException(f"Failed to delete thread: {e}")