feat(agent): 增强多模态链路与工具调用能力
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from typing import Any
|
||||
from uuid import UUID
|
||||
|
||||
from sqlalchemy import select
|
||||
|
||||
from core.agentscope.events import (
|
||||
AgentScopeAgUiCodec,
|
||||
AgentScopeEventPipeline,
|
||||
@@ -18,6 +21,7 @@ from core.config.settings import config
|
||||
from core.db.session import AsyncSessionLocal
|
||||
from core.logging import get_logger
|
||||
from core.taskiq.app import bulk_broker, critical_broker, default_broker
|
||||
from models.agent_chat_message import AgentChatMessage, AgentChatMessageRole
|
||||
from services.base.redis import get_or_init_redis_client
|
||||
|
||||
logger = get_logger("core.agentscope.runtime.tasks")
|
||||
@@ -76,6 +80,56 @@ def _extract_user_token(
|
||||
return None
|
||||
|
||||
|
||||
async def _build_recent_context_messages(
|
||||
*,
|
||||
session: Any,
|
||||
thread_id: str,
|
||||
current_run_id: str,
|
||||
max_messages: int = 20,
|
||||
) -> list[dict[str, Any]]:
|
||||
try:
|
||||
session_uuid = UUID(thread_id)
|
||||
except ValueError:
|
||||
return []
|
||||
|
||||
utc_now = datetime.now(timezone.utc)
|
||||
start_of_today = utc_now.replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
start_of_yesterday = start_of_today - timedelta(days=1)
|
||||
|
||||
stmt = (
|
||||
select(AgentChatMessage)
|
||||
.where(AgentChatMessage.session_id == session_uuid)
|
||||
.where(AgentChatMessage.deleted_at.is_(None))
|
||||
.where(AgentChatMessage.created_at >= start_of_yesterday)
|
||||
.order_by(AgentChatMessage.seq.asc())
|
||||
)
|
||||
rows = (await session.execute(stmt)).scalars().all()
|
||||
|
||||
normalized: list[dict[str, Any]] = []
|
||||
for row in rows:
|
||||
metadata = row.metadata_json if isinstance(row.metadata_json, dict) else {}
|
||||
if metadata.get("run_id") == current_run_id:
|
||||
continue
|
||||
role = (
|
||||
row.role.value
|
||||
if isinstance(row.role, AgentChatMessageRole)
|
||||
else str(row.role)
|
||||
)
|
||||
if role not in {"user", "assistant"}:
|
||||
continue
|
||||
normalized.append(
|
||||
{
|
||||
"id": str(row.id),
|
||||
"role": role,
|
||||
"content": row.content,
|
||||
}
|
||||
)
|
||||
|
||||
if len(normalized) <= max_messages:
|
||||
return normalized
|
||||
return normalized[-max_messages:]
|
||||
|
||||
|
||||
async def run_agentscope_task(command: dict[str, Any]) -> dict[str, object]:
|
||||
command_type = str(command.get("command", "run")).strip().lower()
|
||||
raw_run_input = command.get("run_input")
|
||||
@@ -117,6 +171,21 @@ async def run_agentscope_task(command: dict[str, Any]) -> dict[str, object]:
|
||||
)
|
||||
|
||||
async with AsyncSessionLocal() as session:
|
||||
if command_type == "run":
|
||||
context_messages = await _build_recent_context_messages(
|
||||
session=session,
|
||||
thread_id=parsed_run_input.thread_id,
|
||||
current_run_id=parsed_run_input.run_id,
|
||||
)
|
||||
parsed_run_input = parsed_run_input.model_copy(
|
||||
update={
|
||||
"messages": [
|
||||
*context_messages,
|
||||
*parsed_run_input.messages,
|
||||
]
|
||||
}
|
||||
)
|
||||
|
||||
if command_type == "resume":
|
||||
await runtime.resume(
|
||||
command=parsed_run_input,
|
||||
|
||||
Reference in New Issue
Block a user