refactor: 重构 AgentScope ReAct Runner 与事件处理

- 重构 runtime/runner.py 实现 ReAct Agent 核心逻辑
- 更新事件编码器与存储机制
- 优化 prompt 系统与 tool 调用
- 调整 agent service 与 repository 配合
This commit is contained in:
qzl
2026-03-16 16:10:39 +08:00
parent ab073c88ed
commit 36b104fa37
22 changed files with 1288 additions and 319 deletions
+28
View File
@@ -53,6 +53,12 @@ class FriendshipRepository(Protocol):
"""Get friendship by ID."""
...
async def get_friendships_by_ids(
self, friendship_ids: list[UUID]
) -> dict[UUID, Friendship]:
"""Batch get friendships by IDs."""
...
async def get_inbox_messages_for_user(
self, user_id: UUID, status: InboxMessageStatus | None = None
) -> list[InboxMessage]:
@@ -214,6 +220,28 @@ class SQLAlchemyFriendshipRepository(BaseRepository[Friendship]):
)
raise
async def get_friendships_by_ids(
self, friendship_ids: list[UUID]
) -> dict[UUID, Friendship]:
if not friendship_ids:
return {}
try:
unique_ids = list(dict.fromkeys(friendship_ids))
stmt = (
select(Friendship)
.where(Friendship.id.in_(unique_ids))
.where(Friendship.deleted_at.is_(None))
)
result = await self._session.execute(stmt)
friendships = list(result.scalars().all())
return {friendship.id: friendship for friendship in friendships}
except SQLAlchemyError:
logger.exception(
"Failed to get friendships by ids",
friendship_ids=[str(i) for i in friendship_ids],
)
raise
async def get_inbox_messages_for_user(
self, user_id: UUID, status: InboxMessageStatus | None = None
) -> list[InboxMessage]:
+43 -6
View File
@@ -362,6 +362,28 @@ class FriendshipService(BaseService):
status_code=503, detail="Friendship service unavailable"
)
candidate_inbox = [
inbox
for inbox in inbox_messages
if inbox.message_type == InboxMessageType.FRIEND_REQUEST
and inbox.friendship_id is not None
and inbox.sender_id is not None
]
if not candidate_inbox:
return []
friendship_ids = [inbox.friendship_id for inbox in candidate_inbox]
friendships_by_id = await self._repository.get_friendships_by_ids(
cast(list[UUID], friendship_ids)
)
profile_ids = {user_id}
for inbox in candidate_inbox:
sender_id = cast(UUID, inbox.sender_id)
profile_ids.add(sender_id)
profiles_by_id = await self._user_repository.get_by_user_ids(list(profile_ids))
recipient = profiles_by_id.get(user_id)
result: list[FriendRequestResponse] = []
for inbox in inbox_messages:
if inbox.message_type != InboxMessageType.FRIEND_REQUEST:
@@ -371,7 +393,7 @@ class FriendshipService(BaseService):
if friendship_id is None:
continue
friendship = await self._repository.get_friendship_by_id(friendship_id)
friendship = friendships_by_id.get(friendship_id)
if friendship is None or friendship.status != FriendshipStatus.PENDING:
continue
@@ -379,8 +401,7 @@ class FriendshipService(BaseService):
if sender_id is None:
continue
sender = await self._user_repository.get_by_user_id(sender_id)
recipient = await self._user_repository.get_by_user_id(user_id)
sender = profiles_by_id.get(sender_id)
result.append(
FriendRequestResponse(
@@ -460,11 +481,19 @@ class FriendshipService(BaseService):
status_code=503, detail="Friendship service unavailable"
)
if not outgoing:
return []
user_ids = {user_id}
for friendship in outgoing:
user_ids.add(self._get_other_user_id(friendship, user_id))
profiles_by_id = await self._user_repository.get_by_user_ids(list(user_ids))
sender = profiles_by_id.get(user_id)
result: list[FriendRequestResponse] = []
for friendship in outgoing:
other_user_id = self._get_other_user_id(friendship, user_id)
sender = await self._user_repository.get_by_user_id(user_id)
recipient = await self._user_repository.get_by_user_id(other_user_id)
recipient = profiles_by_id.get(other_user_id)
result.append(
FriendRequestResponse(
@@ -489,10 +518,18 @@ class FriendshipService(BaseService):
status_code=503, detail="Friendship service unavailable"
)
if not friendships:
return []
friend_ids = [
self._get_other_user_id(friendship, user_id) for friendship in friendships
]
profiles_by_id = await self._user_repository.get_by_user_ids(friend_ids)
result: list[FriendResponse] = []
for friendship in friendships:
friend_id = self._get_other_user_id(friendship, user_id)
friend = await self._user_repository.get_by_user_id(friend_id)
friend = profiles_by_id.get(friend_id)
result.append(
FriendResponse(