2026-03-16 16:10:39 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from dataclasses import dataclass
|
|
|
|
|
from datetime import datetime, timezone
|
|
|
|
|
from typing import TYPE_CHECKING, Any
|
2026-03-16 18:32:09 +08:00
|
|
|
from uuid import UUID
|
2026-03-16 16:10:39 +08:00
|
|
|
|
|
|
|
|
from ag_ui.core.types import RunAgentInput
|
|
|
|
|
from agentscope.formatter import OpenAIChatFormatter
|
|
|
|
|
from agentscope.memory import InMemoryMemory
|
|
|
|
|
from agentscope.message import Msg
|
|
|
|
|
from agentscope.model import OpenAIChatModel
|
2026-03-16 18:32:09 +08:00
|
|
|
from core.agentscope.prompts.agent_prompt import build_worker_contract_prompt
|
2026-03-16 16:10:39 +08:00
|
|
|
from core.agentscope.prompts.system_prompt import build_system_prompt
|
2026-03-16 18:32:09 +08:00
|
|
|
from core.agentscope.runtime.json_react_agent import JsonReActAgent
|
|
|
|
|
from core.agentscope.runtime.model_tracking import TrackingChatModel
|
|
|
|
|
from core.agentscope.runtime.router_persistence import persist_router_message
|
|
|
|
|
from core.agentscope.runtime.stage_emitter import PipelineStageEmitter
|
|
|
|
|
from core.agentscope.tools.toolkit import build_stage_toolkit
|
|
|
|
|
from core.agentscope.utils import (
|
|
|
|
|
finalize_json_response,
|
|
|
|
|
patch_agentscope_json_repair_compat,
|
2026-03-16 16:10:39 +08:00
|
|
|
)
|
|
|
|
|
from core.db.session import AsyncSessionLocal
|
|
|
|
|
from core.logging import get_logger
|
|
|
|
|
from models.llm import Llm
|
|
|
|
|
from models.system_agents import SystemAgents
|
|
|
|
|
from schemas.agent.runtime_models import (
|
|
|
|
|
RouterAgentOutput,
|
|
|
|
|
WorkerAgentOutputLite,
|
|
|
|
|
resolve_worker_output_model,
|
|
|
|
|
)
|
|
|
|
|
from schemas.agent.system_agent import AgentType, SystemAgentLLMConfig
|
|
|
|
|
from schemas.user import UserContext
|
|
|
|
|
from services.litellm.service import LiteLLMService
|
|
|
|
|
from sqlalchemy import select
|
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from core.agentscope.runtime.orchestrator import PipelineLike
|
|
|
|
|
|
|
|
|
|
logger = get_logger("core.agentscope.runtime.runner")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
|
|
|
class SystemAgentRuntimeConfig:
|
|
|
|
|
agent_type: AgentType
|
|
|
|
|
model_code: str
|
|
|
|
|
llm_config: SystemAgentLLMConfig
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
|
|
|
class StageExecutionResult:
|
|
|
|
|
message: Msg
|
|
|
|
|
payload: dict[str, Any]
|
|
|
|
|
response_metadata: dict[str, Any]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AgentScopeRunner:
|
|
|
|
|
def __init__(self, *, litellm_service: LiteLLMService | None = None) -> None:
|
2026-03-16 18:32:09 +08:00
|
|
|
patch_agentscope_json_repair_compat()
|
2026-03-16 16:10:39 +08:00
|
|
|
self._litellm_service = litellm_service or LiteLLMService()
|
|
|
|
|
|
|
|
|
|
async def execute(
|
|
|
|
|
self,
|
|
|
|
|
*,
|
|
|
|
|
user_context: UserContext,
|
|
|
|
|
context_messages: list[Msg],
|
|
|
|
|
pipeline: PipelineLike,
|
|
|
|
|
run_input: RunAgentInput,
|
|
|
|
|
) -> dict[str, Any]:
|
|
|
|
|
owner_id = UUID(user_context.id)
|
|
|
|
|
|
|
|
|
|
async with AsyncSessionLocal() as session:
|
2026-03-16 18:32:09 +08:00
|
|
|
worker_toolkit = self._build_worker_toolkit(
|
|
|
|
|
session=session, owner_id=owner_id
|
2026-03-16 16:10:39 +08:00
|
|
|
)
|
2026-03-16 18:32:09 +08:00
|
|
|
router_config, worker_config = await self._load_stage_configs(
|
|
|
|
|
session=session
|
2026-03-16 16:10:39 +08:00
|
|
|
)
|
|
|
|
|
|
2026-03-16 18:32:09 +08:00
|
|
|
router_output = await self._execute_router_step(
|
|
|
|
|
session=session,
|
2026-03-16 16:10:39 +08:00
|
|
|
pipeline=pipeline,
|
|
|
|
|
run_input=run_input,
|
|
|
|
|
user_context=user_context,
|
|
|
|
|
context_messages=context_messages,
|
|
|
|
|
stage_config=router_config,
|
|
|
|
|
)
|
2026-03-16 18:32:09 +08:00
|
|
|
worker_output = await self._execute_worker_step(
|
2026-03-16 16:10:39 +08:00
|
|
|
pipeline=pipeline,
|
|
|
|
|
run_input=run_input,
|
|
|
|
|
user_context=user_context,
|
|
|
|
|
router_output=router_output,
|
|
|
|
|
toolkit=worker_toolkit,
|
|
|
|
|
stage_config=worker_config,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
"router": router_output.model_dump(mode="json", exclude_none=True),
|
|
|
|
|
"worker": worker_output.model_dump(mode="json", exclude_none=True),
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 18:32:09 +08:00
|
|
|
def _build_worker_toolkit(
|
2026-03-16 16:10:39 +08:00
|
|
|
self,
|
|
|
|
|
*,
|
|
|
|
|
session: AsyncSession,
|
|
|
|
|
owner_id: UUID,
|
2026-03-16 18:32:09 +08:00
|
|
|
) -> Any:
|
|
|
|
|
return build_stage_toolkit(
|
|
|
|
|
agent_type=AgentType.WORKER,
|
|
|
|
|
session=session,
|
|
|
|
|
owner_id=owner_id,
|
2026-03-16 16:10:39 +08:00
|
|
|
)
|
|
|
|
|
|
2026-03-16 18:32:09 +08:00
|
|
|
async def _load_stage_configs(
|
|
|
|
|
self,
|
|
|
|
|
*,
|
|
|
|
|
session: AsyncSession,
|
|
|
|
|
) -> tuple[SystemAgentRuntimeConfig, SystemAgentRuntimeConfig]:
|
|
|
|
|
router_config = await self._load_system_agent_config(
|
|
|
|
|
session=session,
|
|
|
|
|
agent_type=AgentType.ROUTER,
|
|
|
|
|
)
|
|
|
|
|
worker_config = await self._load_system_agent_config(
|
|
|
|
|
session=session,
|
|
|
|
|
agent_type=AgentType.WORKER,
|
|
|
|
|
)
|
|
|
|
|
return router_config, worker_config
|
|
|
|
|
|
|
|
|
|
async def _execute_router_step(
|
|
|
|
|
self,
|
|
|
|
|
*,
|
|
|
|
|
session: AsyncSession,
|
|
|
|
|
pipeline: PipelineLike,
|
|
|
|
|
run_input: RunAgentInput,
|
|
|
|
|
user_context: UserContext,
|
|
|
|
|
context_messages: list[Msg],
|
|
|
|
|
stage_config: SystemAgentRuntimeConfig,
|
|
|
|
|
) -> RouterAgentOutput:
|
|
|
|
|
await self._emit_step_event(
|
|
|
|
|
pipeline=pipeline,
|
|
|
|
|
run_input=run_input,
|
|
|
|
|
step_name="router",
|
|
|
|
|
event_type="STEP_STARTED",
|
|
|
|
|
)
|
|
|
|
|
router_result = await self._run_router_stage(
|
|
|
|
|
user_context=user_context,
|
|
|
|
|
context_messages=context_messages,
|
|
|
|
|
run_input=run_input,
|
|
|
|
|
stage_config=stage_config,
|
|
|
|
|
)
|
|
|
|
|
router_output = RouterAgentOutput.model_validate(router_result.payload)
|
|
|
|
|
await persist_router_message(
|
|
|
|
|
session=session,
|
|
|
|
|
thread_id=run_input.thread_id,
|
|
|
|
|
run_id=run_input.run_id,
|
|
|
|
|
model_code=stage_config.model_code,
|
|
|
|
|
router_output=router_output,
|
|
|
|
|
response_metadata=router_result.response_metadata,
|
|
|
|
|
)
|
|
|
|
|
await session.commit()
|
|
|
|
|
await self._emit_step_event(
|
|
|
|
|
pipeline=pipeline,
|
|
|
|
|
run_input=run_input,
|
|
|
|
|
step_name="router",
|
|
|
|
|
event_type="STEP_FINISHED",
|
|
|
|
|
)
|
|
|
|
|
return router_output
|
|
|
|
|
|
|
|
|
|
async def _execute_worker_step(
|
|
|
|
|
self,
|
|
|
|
|
*,
|
|
|
|
|
pipeline: PipelineLike,
|
|
|
|
|
run_input: RunAgentInput,
|
|
|
|
|
user_context: UserContext,
|
|
|
|
|
router_output: RouterAgentOutput,
|
|
|
|
|
toolkit: Any,
|
|
|
|
|
stage_config: SystemAgentRuntimeConfig,
|
|
|
|
|
) -> WorkerAgentOutputLite:
|
|
|
|
|
worker_output_model = resolve_worker_output_model(router_output.ui.ui_mode)
|
|
|
|
|
await self._emit_step_event(
|
|
|
|
|
pipeline=pipeline,
|
|
|
|
|
run_input=run_input,
|
|
|
|
|
step_name="worker",
|
|
|
|
|
event_type="STEP_STARTED",
|
|
|
|
|
)
|
|
|
|
|
worker_result = await self._run_worker_stage(
|
|
|
|
|
user_context=user_context,
|
|
|
|
|
router_output=router_output,
|
|
|
|
|
toolkit=toolkit,
|
|
|
|
|
run_input=run_input,
|
|
|
|
|
stage_config=stage_config,
|
|
|
|
|
worker_output_model=worker_output_model,
|
|
|
|
|
pipeline=pipeline,
|
|
|
|
|
)
|
|
|
|
|
worker_output = worker_output_model.model_validate(worker_result.payload)
|
|
|
|
|
await self._emit_step_event(
|
|
|
|
|
pipeline=pipeline,
|
|
|
|
|
run_input=run_input,
|
|
|
|
|
step_name="worker",
|
|
|
|
|
event_type="STEP_FINISHED",
|
|
|
|
|
)
|
|
|
|
|
return worker_output
|
2026-03-16 16:10:39 +08:00
|
|
|
|
|
|
|
|
async def _load_system_agent_config(
|
|
|
|
|
self,
|
|
|
|
|
*,
|
|
|
|
|
session: AsyncSession,
|
|
|
|
|
agent_type: AgentType,
|
|
|
|
|
) -> SystemAgentRuntimeConfig:
|
|
|
|
|
stmt = (
|
|
|
|
|
select(SystemAgents, Llm)
|
|
|
|
|
.join(Llm, SystemAgents.llm_id == Llm.id)
|
|
|
|
|
.where(SystemAgents.agent_type == agent_type.value)
|
|
|
|
|
)
|
|
|
|
|
row = (await session.execute(stmt)).one_or_none()
|
|
|
|
|
if row is None:
|
|
|
|
|
raise RuntimeError(f"system agent config not found: {agent_type.value}")
|
|
|
|
|
system_agent, llm = row
|
|
|
|
|
status = str(system_agent.status).strip().lower()
|
|
|
|
|
if status != "active":
|
|
|
|
|
raise RuntimeError(f"system agent is not active: {agent_type.value}")
|
|
|
|
|
return SystemAgentRuntimeConfig(
|
|
|
|
|
agent_type=agent_type,
|
|
|
|
|
model_code=llm.model_code,
|
|
|
|
|
llm_config=SystemAgentLLMConfig.model_validate(system_agent.config or {}),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
async def _run_router_stage(
|
|
|
|
|
self,
|
|
|
|
|
*,
|
|
|
|
|
user_context: UserContext,
|
|
|
|
|
context_messages: list[Msg],
|
|
|
|
|
run_input: RunAgentInput,
|
|
|
|
|
stage_config: SystemAgentRuntimeConfig,
|
|
|
|
|
) -> StageExecutionResult:
|
|
|
|
|
tracking_model = self._build_model(stage_config=stage_config)
|
|
|
|
|
system_prompt = build_system_prompt(
|
|
|
|
|
agent_type=AgentType.ROUTER,
|
|
|
|
|
user_context=user_context,
|
|
|
|
|
now_utc=datetime.now(timezone.utc),
|
|
|
|
|
tools=None,
|
|
|
|
|
)
|
2026-03-16 18:32:09 +08:00
|
|
|
response, payload = await finalize_json_response(
|
2026-03-16 16:10:39 +08:00
|
|
|
model=tracking_model,
|
2026-03-16 18:32:09 +08:00
|
|
|
formatter=OpenAIChatFormatter(),
|
|
|
|
|
base_messages=[Msg("system", system_prompt, "system"), *context_messages],
|
2026-03-16 16:10:39 +08:00
|
|
|
output_model=RouterAgentOutput,
|
2026-03-16 18:32:09 +08:00
|
|
|
retries=0,
|
2026-03-16 16:10:39 +08:00
|
|
|
)
|
2026-03-16 18:32:09 +08:00
|
|
|
response_msg = Msg(
|
|
|
|
|
name="router",
|
|
|
|
|
role="assistant",
|
|
|
|
|
content=list(getattr(response, "content", [])),
|
|
|
|
|
metadata=payload,
|
|
|
|
|
)
|
|
|
|
|
|
2026-03-16 16:10:39 +08:00
|
|
|
logger.info(
|
|
|
|
|
"router_reply_received",
|
|
|
|
|
run_id=run_input.run_id,
|
|
|
|
|
thread_id=run_input.thread_id,
|
|
|
|
|
message_id=str(response_msg.id),
|
|
|
|
|
)
|
|
|
|
|
return StageExecutionResult(
|
|
|
|
|
message=response_msg,
|
|
|
|
|
payload=payload,
|
|
|
|
|
response_metadata=self._litellm_service.build_usage_metadata(
|
|
|
|
|
model=stage_config.model_code,
|
|
|
|
|
usage_summary=tracking_model.usage_summary(),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
async def _run_worker_stage(
|
|
|
|
|
self,
|
|
|
|
|
*,
|
|
|
|
|
user_context: UserContext,
|
|
|
|
|
router_output: RouterAgentOutput,
|
|
|
|
|
toolkit: Any,
|
|
|
|
|
run_input: RunAgentInput,
|
|
|
|
|
stage_config: SystemAgentRuntimeConfig,
|
|
|
|
|
worker_output_model: type[WorkerAgentOutputLite],
|
|
|
|
|
pipeline: PipelineLike,
|
|
|
|
|
) -> StageExecutionResult:
|
2026-03-16 18:32:09 +08:00
|
|
|
worker_input = self._build_worker_input_messages(router_output=router_output)
|
2026-03-16 16:10:39 +08:00
|
|
|
tracking_model = self._build_model(stage_config=stage_config)
|
2026-03-16 18:32:09 +08:00
|
|
|
emitter = PipelineStageEmitter(
|
2026-03-16 16:10:39 +08:00
|
|
|
pipeline=pipeline,
|
|
|
|
|
session_id=run_input.thread_id,
|
|
|
|
|
run_id=run_input.run_id,
|
|
|
|
|
stage="worker",
|
|
|
|
|
emit_text_events=True,
|
|
|
|
|
emit_tool_events=True,
|
|
|
|
|
)
|
|
|
|
|
agent = self._build_agent(
|
|
|
|
|
agent_name="worker",
|
|
|
|
|
system_prompt=build_system_prompt(
|
|
|
|
|
agent_type=AgentType.WORKER,
|
|
|
|
|
user_context=user_context,
|
|
|
|
|
now_utc=datetime.now(timezone.utc),
|
2026-03-16 18:32:09 +08:00
|
|
|
tools=None,
|
2026-03-16 16:10:39 +08:00
|
|
|
),
|
|
|
|
|
toolkit=toolkit,
|
|
|
|
|
model=tracking_model,
|
|
|
|
|
emitter=emitter,
|
|
|
|
|
)
|
|
|
|
|
response_msg = await agent.reply_json(
|
2026-03-16 18:32:09 +08:00
|
|
|
worker_input, output_model=worker_output_model
|
2026-03-16 16:10:39 +08:00
|
|
|
)
|
|
|
|
|
worker_payload = worker_output_model.model_validate(response_msg.metadata or {})
|
|
|
|
|
response_metadata = self._litellm_service.build_usage_metadata(
|
|
|
|
|
model=stage_config.model_code,
|
|
|
|
|
usage_summary=tracking_model.usage_summary(),
|
|
|
|
|
)
|
|
|
|
|
await emitter.emit_final_text_end(
|
|
|
|
|
worker_output=worker_payload.model_dump(mode="json", exclude_none=True),
|
|
|
|
|
response_metadata=response_metadata,
|
|
|
|
|
)
|
|
|
|
|
return StageExecutionResult(
|
|
|
|
|
message=response_msg,
|
|
|
|
|
payload=worker_payload.model_dump(mode="json", exclude_none=True),
|
|
|
|
|
response_metadata=response_metadata,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def _build_worker_input_messages(
|
|
|
|
|
self,
|
|
|
|
|
*,
|
|
|
|
|
router_output: RouterAgentOutput,
|
|
|
|
|
) -> list[Msg]:
|
2026-03-16 18:32:09 +08:00
|
|
|
return [
|
|
|
|
|
Msg(
|
|
|
|
|
name="router",
|
|
|
|
|
role="user",
|
|
|
|
|
content=build_worker_contract_prompt(router_output=router_output),
|
|
|
|
|
)
|
|
|
|
|
]
|
2026-03-16 16:10:39 +08:00
|
|
|
|
|
|
|
|
def _build_model(
|
|
|
|
|
self, *, stage_config: SystemAgentRuntimeConfig
|
2026-03-16 18:32:09 +08:00
|
|
|
) -> TrackingChatModel:
|
2026-03-16 16:10:39 +08:00
|
|
|
generate_kwargs: dict[str, Any] = {
|
|
|
|
|
"temperature": stage_config.llm_config.temperature,
|
|
|
|
|
"max_tokens": stage_config.llm_config.max_tokens,
|
|
|
|
|
"timeout": stage_config.llm_config.timeout_seconds,
|
|
|
|
|
}
|
|
|
|
|
if stage_config.agent_type == AgentType.ROUTER:
|
|
|
|
|
generate_kwargs["extra_body"] = {"enable_thinking": False}
|
|
|
|
|
|
|
|
|
|
model = OpenAIChatModel(
|
|
|
|
|
model_name=stage_config.model_code,
|
|
|
|
|
api_key=self._litellm_service.proxy_api_key,
|
|
|
|
|
stream=False,
|
|
|
|
|
client_kwargs={"base_url": self._litellm_service.proxy_base_url},
|
|
|
|
|
generate_kwargs=generate_kwargs,
|
|
|
|
|
)
|
2026-03-16 18:32:09 +08:00
|
|
|
return TrackingChatModel(model)
|
2026-03-16 16:10:39 +08:00
|
|
|
|
|
|
|
|
def _build_agent(
|
|
|
|
|
self,
|
|
|
|
|
*,
|
|
|
|
|
agent_name: str,
|
|
|
|
|
system_prompt: str,
|
|
|
|
|
toolkit: Any,
|
2026-03-16 18:32:09 +08:00
|
|
|
model: TrackingChatModel,
|
|
|
|
|
emitter: PipelineStageEmitter | None = None,
|
2026-03-16 16:10:39 +08:00
|
|
|
) -> JsonReActAgent:
|
|
|
|
|
return JsonReActAgent(
|
|
|
|
|
name=agent_name,
|
|
|
|
|
sys_prompt=system_prompt,
|
|
|
|
|
model=model,
|
|
|
|
|
formatter=OpenAIChatFormatter(),
|
|
|
|
|
toolkit=toolkit,
|
|
|
|
|
memory=InMemoryMemory(),
|
|
|
|
|
emitter=emitter,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
async def _emit_step_event(
|
|
|
|
|
self,
|
|
|
|
|
*,
|
|
|
|
|
pipeline: PipelineLike,
|
|
|
|
|
run_input: RunAgentInput,
|
|
|
|
|
step_name: str,
|
|
|
|
|
event_type: str,
|
|
|
|
|
) -> None:
|
|
|
|
|
await pipeline.emit(
|
|
|
|
|
session_id=run_input.thread_id,
|
|
|
|
|
event={
|
|
|
|
|
"type": event_type,
|
|
|
|
|
"threadId": run_input.thread_id,
|
|
|
|
|
"runId": run_input.run_id,
|
|
|
|
|
"stepName": step_name,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AgentScopeReActRunner = AgentScopeRunner
|