refactor: 重构 Agent 模块为 AgentScope,删除旧版 CrewAI/LiteLLM 实现
This commit is contained in:
@@ -3,14 +3,16 @@ from __future__ import annotations
|
||||
from typing import Any
|
||||
from uuid import UUID
|
||||
|
||||
from core.agent.domain.user_context import UserAgentContext, parse_profile_settings
|
||||
from core.agentscope.events import (
|
||||
AgentScopeAgUiCodec,
|
||||
AgentScopeEventPipeline,
|
||||
NullEventStore,
|
||||
RedisStreamBus,
|
||||
SqlAlchemyEventStore,
|
||||
)
|
||||
from core.agentscope.schemas.user_context import (
|
||||
UserAgentContext,
|
||||
parse_profile_settings,
|
||||
)
|
||||
from core.agentscope.runtime import AgentRouteRuntime, AgentScopeRuntimeOrchestrator
|
||||
from core.agentscope.schemas.agent_runtime import ResumeCommand, RunCommand
|
||||
from core.config.settings import config
|
||||
from core.db.session import AsyncSessionLocal
|
||||
@@ -20,6 +22,26 @@ 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
|
||||
if AgentScopeRuntimeOrchestrator is None:
|
||||
from core.agentscope.runtime.orchestrator import (
|
||||
AgentScopeRuntimeOrchestrator as _ASRO,
|
||||
)
|
||||
|
||||
AgentScopeRuntimeOrchestrator = _ASRO
|
||||
return AgentRouteRuntime, AgentScopeRuntimeOrchestrator
|
||||
|
||||
|
||||
def _build_user_context(*, owner_id: UUID, run_input: RunCommand) -> UserAgentContext:
|
||||
forwarded = (
|
||||
@@ -65,6 +87,10 @@ async def run_agentscope_task(command: dict[str, Any]) -> dict[str, object]:
|
||||
raise ValueError("owner_id is required")
|
||||
|
||||
owner_id = UUID(raw_owner_id)
|
||||
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"
|
||||
@@ -82,18 +108,18 @@ async def run_agentscope_task(command: dict[str, Any]) -> dict[str, object]:
|
||||
)
|
||||
pipeline = AgentScopeEventPipeline(
|
||||
codec=AgentScopeAgUiCodec(),
|
||||
store=NullEventStore(),
|
||||
store=SqlAlchemyEventStore(session_factory=AsyncSessionLocal),
|
||||
bus=bus,
|
||||
)
|
||||
runtime = AgentRouteRuntime(
|
||||
orchestrator=AgentScopeRuntimeOrchestrator(),
|
||||
runtime = route_runtime_type(
|
||||
orchestrator=orchestrator_type(),
|
||||
pipeline=pipeline,
|
||||
)
|
||||
|
||||
async with AsyncSessionLocal() as session:
|
||||
if command_type == "resume":
|
||||
await runtime.resume(
|
||||
command=ResumeCommand.model_validate(raw_run_input),
|
||||
command=parsed_run_input,
|
||||
owner_id=owner_id,
|
||||
user_token=user_token,
|
||||
user_context=user_context,
|
||||
@@ -101,15 +127,12 @@ async def run_agentscope_task(command: dict[str, Any]) -> dict[str, object]:
|
||||
)
|
||||
elif command_type == "run":
|
||||
await runtime.run(
|
||||
command=RunCommand.model_validate(raw_run_input),
|
||||
command=parsed_run_input,
|
||||
owner_id=owner_id,
|
||||
user_token=user_token,
|
||||
user_context=user_context,
|
||||
session=session,
|
||||
)
|
||||
else:
|
||||
raise ValueError("invalid command type")
|
||||
|
||||
logger.info(
|
||||
"agentscope runtime task completed",
|
||||
command_type=command_type,
|
||||
|
||||
Reference in New Issue
Block a user