refactor: 重构 AgentScope 运行时模块并优化前端附件展示

This commit is contained in:
qzl
2026-03-13 15:42:01 +08:00
parent a10a2db27a
commit 4c10929498
28 changed files with 1494 additions and 2163 deletions
+32 -32
View File
@@ -4,6 +4,7 @@ from datetime import datetime, timedelta, timezone
from typing import Any
from uuid import UUID
from ag_ui.core import RunAgentInput
from sqlalchemy import select
from core.agentscope.events import (
@@ -12,61 +13,63 @@ from core.agentscope.events import (
RedisStreamBus,
SqlAlchemyEventStore,
)
from core.agentscope.schemas.user_context import (
UserAgentContext,
parse_profile_settings,
from core.agentscope.schemas.agui_input import (
extract_latest_tool_result,
parse_run_input,
)
from core.agentscope.schemas.agent_runtime import ResumeCommand, RunCommand
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 core.agentscope.tools.tool_result_storage import create_tool_result_storage
from models.agent_chat_message import AgentChatMessage, AgentChatMessageRole
from schemas.user import UserContext, parse_profile_settings
from services.base.redis import get_or_init_redis_client
logger = get_logger("core.agentscope.runtime.tasks")
AgentRouteRuntime: type[Any] | None = None
AgentScopeRuntimeOrchestrator: type[Any] | None = None
def _load_runtime_types() -> tuple[type[Any], type[Any]]:
global AgentRouteRuntime, AgentScopeRuntimeOrchestrator
if AgentRouteRuntime is None:
from core.agentscope.runtime.agent_route_runtime import (
AgentRouteRuntime as _ARR,
)
AgentRouteRuntime = _ARR
def _load_runtime_type() -> type[Any]:
global AgentScopeRuntimeOrchestrator
if AgentScopeRuntimeOrchestrator is None:
from core.agentscope.runtime.orchestrator import (
AgentScopeRuntimeOrchestrator as _ASRO,
)
AgentScopeRuntimeOrchestrator = _ASRO
return AgentRouteRuntime, AgentScopeRuntimeOrchestrator
runtime_type = AgentScopeRuntimeOrchestrator
if runtime_type is None:
raise RuntimeError("failed to load AgentScopeRuntimeOrchestrator")
return runtime_type
def _build_user_context(*, owner_id: UUID, run_input: RunCommand) -> UserAgentContext:
def _build_user_context(*, owner_id: UUID, run_input: RunAgentInput) -> UserContext:
forwarded = (
run_input.forwarded_props if isinstance(run_input.forwarded_props, dict) else {}
)
username = str(forwarded.get("username", "user")).strip() or "user"
bio_value = forwarded.get("bio")
bio = str(bio_value).strip() if isinstance(bio_value, str) else None
email_value = forwarded.get("email")
email = str(email_value).strip() if isinstance(email_value, str) else None
avatar_value = forwarded.get("avatarUrl")
avatar_url = str(avatar_value).strip() if isinstance(avatar_value, str) else None
profile_settings = forwarded.get("profileSettings")
settings_raw = profile_settings if isinstance(profile_settings, dict) else None
return UserAgentContext(
user_id=owner_id,
return UserContext(
id=str(owner_id),
username=username,
email=email,
avatar_url=avatar_url,
bio=bio,
settings=parse_profile_settings(settings_raw),
)
def _extract_user_token(
*, command: dict[str, Any], run_input: RunCommand
*, command: dict[str, Any], run_input: RunAgentInput
) -> str | None:
del run_input
raw_token = command.get("user_token")
@@ -139,12 +142,10 @@ async def run_agentscope_task(command: dict[str, Any]) -> dict[str, object]:
if command_type not in {"run", "resume"}:
raise ValueError("invalid command type")
route_runtime_type, orchestrator_type = _load_runtime_types()
parsed_run_input = (
ResumeCommand.model_validate(raw_run_input)
if command_type == "resume"
else RunCommand.model_validate(raw_run_input)
)
orchestrator_type = _load_runtime_type()
parsed_run_input = parse_run_input(raw_run_input)
if command_type == "resume":
extract_latest_tool_result(parsed_run_input)
user_context = _build_user_context(owner_id=owner_id, run_input=parsed_run_input)
user_token = _extract_user_token(command=command, run_input=parsed_run_input) or ""
@@ -164,18 +165,17 @@ async def run_agentscope_task(command: dict[str, Any]) -> dict[str, object]:
),
bus=bus,
)
runtime = route_runtime_type(
orchestrator=orchestrator_type(),
runtime = orchestrator_type(
pipeline=pipeline,
)
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,
)
context_messages = await _build_recent_context_messages(
session=session,
thread_id=parsed_run_input.thread_id,
current_run_id=parsed_run_input.run_id,
)
if context_messages:
parsed_run_input = parsed_run_input.model_copy(
update={
"messages": [