refactor(backend): update service layers

- services/agent_conversations.py: update agent conversations service
- services/agent_foundation.py: update agent foundation service
- services/orchestrator.py: update orchestrator service
- services/user_agent.py: update user agent service
This commit is contained in:
caoxiaozhu
2026-05-13 15:31:04 +00:00
parent de51ed2e9f
commit 44b2838a12
4 changed files with 462 additions and 80 deletions

View File

@@ -193,15 +193,18 @@ class AgentConversationService:
if conversation is None:
return None
next_sequence = int(conversation.message_count or 0) + 1
normalized_message_json = dict(message_json or {})
normalized_message_json.setdefault("sequence", next_sequence)
message = AgentConversationMessage(
conversation_id=conversation_id,
run_id=run_id,
role=str(role or "user").strip() or "user",
content=normalized_content,
message_json=message_json or {},
message_json=normalized_message_json,
created_at=datetime.now(UTC),
)
conversation.message_count = int(conversation.message_count or 0) + 1
conversation.message_count = next_sequence
if role == "user" and not conversation.title:
conversation.title = normalized_content[:48]
conversation.updated_at = datetime.now(UTC)
@@ -220,15 +223,9 @@ class AgentConversationService:
normalized_id = str(conversation_id or "").strip()
if not normalized_id or limit <= 0:
return []
stmt = (
select(AgentConversationMessage)
.where(AgentConversationMessage.conversation_id == normalized_id)
.order_by(AgentConversationMessage.created_at.desc())
.limit(limit)
)
messages = list(self.db.scalars(stmt).all())
messages.reverse()
messages = self.list_messages(normalized_id)
if limit > 0:
messages = messages[-limit:]
return [
{
"role": item.role,
@@ -252,11 +249,13 @@ class AgentConversationService:
stmt = (
select(AgentConversationMessage)
.where(AgentConversationMessage.conversation_id == normalized_id)
.order_by(AgentConversationMessage.created_at.asc(), AgentConversationMessage.id.asc())
.order_by(AgentConversationMessage.created_at.asc())
)
messages = list(self.db.scalars(stmt).all())
messages.sort(key=self._message_sort_key)
if limit and limit > 0:
stmt = stmt.limit(limit)
return list(self.db.scalars(stmt).all())
return messages[:limit]
return messages
def update_state(
self,
@@ -396,6 +395,30 @@ class AgentConversationService:
"created_at": message.created_at,
}
@staticmethod
def _message_sort_key(message: AgentConversationMessage) -> tuple[int, datetime, str, int, str]:
message_json = dict(message.message_json or {})
sequence = AgentConversationService._coerce_message_sequence(message_json.get("sequence"))
created_at = message.created_at or datetime.min.replace(tzinfo=UTC)
run_id = str(message.run_id or "")
role_priority = 0 if str(message.role or "").strip() == "user" else 1
fallback_sequence = sequence if sequence is not None else 10**9
return (
fallback_sequence,
created_at,
run_id,
role_priority,
str(message.id or ""),
)
@staticmethod
def _coerce_message_sequence(value: Any) -> int | None:
try:
normalized = int(value)
except (TypeError, ValueError):
return None
return normalized if normalized > 0 else None
@staticmethod
def _is_empty_value(value: Any) -> bool:
if value is None: