feat: 重构 agentscope 缓存架构,新增消息和附件缓存
This commit is contained in:
@@ -29,7 +29,9 @@ from models.llm_factory import LlmFactory
|
||||
from models.system_agents import SystemAgents
|
||||
from schemas.agent.forwarded_props import (
|
||||
ClientTimeContext,
|
||||
RuntimeMode,
|
||||
parse_forwarded_props_client_time,
|
||||
parse_forwarded_props_runtime_mode,
|
||||
)
|
||||
from schemas.agent.runtime_models import (
|
||||
RouterAgentOutput,
|
||||
@@ -76,6 +78,7 @@ class AgentScopeRunner:
|
||||
) -> dict[str, Any]:
|
||||
owner_id = UUID(user_context.id)
|
||||
runtime_client_time = self._resolve_runtime_client_time(run_input=run_input)
|
||||
runtime_mode = self._resolve_runtime_mode(run_input=run_input)
|
||||
|
||||
async with AsyncSessionLocal() as session:
|
||||
router_config = await self._load_stage_config(
|
||||
@@ -99,6 +102,7 @@ class AgentScopeRunner:
|
||||
context_messages=context_messages,
|
||||
stage_config=router_config,
|
||||
runtime_client_time=runtime_client_time,
|
||||
runtime_mode=runtime_mode,
|
||||
user_memory=user_memory,
|
||||
)
|
||||
worker_output = await self._execute_worker_step(
|
||||
@@ -109,6 +113,7 @@ class AgentScopeRunner:
|
||||
toolkit=worker_toolkit,
|
||||
stage_config=worker_config,
|
||||
runtime_client_time=runtime_client_time,
|
||||
runtime_mode=runtime_mode,
|
||||
work_memory=work_memory,
|
||||
)
|
||||
return {
|
||||
@@ -171,6 +176,7 @@ class AgentScopeRunner:
|
||||
context_messages: list[Msg],
|
||||
stage_config: SystemAgentRuntimeConfig,
|
||||
runtime_client_time: ClientTimeContext | None,
|
||||
runtime_mode: RuntimeMode,
|
||||
user_memory: UserMemoryContent | None,
|
||||
) -> RouterAgentOutput:
|
||||
await self._emit_step_event(
|
||||
@@ -178,6 +184,7 @@ class AgentScopeRunner:
|
||||
run_input=run_input,
|
||||
step_name=AgentType.ROUTER.value,
|
||||
event_type="STEP_STARTED",
|
||||
runtime_mode=runtime_mode,
|
||||
)
|
||||
router_result = await self._run_router_stage(
|
||||
user_context=user_context,
|
||||
@@ -193,6 +200,7 @@ class AgentScopeRunner:
|
||||
run_input=run_input,
|
||||
step_name=AgentType.ROUTER.value,
|
||||
event_type="STEP_FINISHED",
|
||||
runtime_mode=runtime_mode,
|
||||
extra_event={
|
||||
"_router_persist": {
|
||||
"router_output": router_output.model_dump(
|
||||
@@ -214,6 +222,7 @@ class AgentScopeRunner:
|
||||
toolkit: Any,
|
||||
stage_config: SystemAgentRuntimeConfig,
|
||||
runtime_client_time: ClientTimeContext | None,
|
||||
runtime_mode: RuntimeMode,
|
||||
work_memory: WorkProfileContent | None,
|
||||
) -> WorkerAgentOutputLite:
|
||||
worker_output_model = resolve_worker_output_model(router_output.ui.ui_mode)
|
||||
@@ -222,6 +231,7 @@ class AgentScopeRunner:
|
||||
run_input=run_input,
|
||||
step_name=AgentType.WORKER.value,
|
||||
event_type="STEP_STARTED",
|
||||
runtime_mode=runtime_mode,
|
||||
)
|
||||
worker_result = await self._run_worker_stage(
|
||||
user_context=user_context,
|
||||
@@ -234,6 +244,7 @@ class AgentScopeRunner:
|
||||
worker_output_model=worker_output_model,
|
||||
pipeline=pipeline,
|
||||
runtime_client_time=runtime_client_time,
|
||||
runtime_mode=runtime_mode,
|
||||
work_memory=work_memory,
|
||||
)
|
||||
worker_output = worker_output_model.model_validate(worker_result.payload)
|
||||
@@ -242,6 +253,7 @@ class AgentScopeRunner:
|
||||
run_input=run_input,
|
||||
step_name=AgentType.WORKER.value,
|
||||
event_type="STEP_FINISHED",
|
||||
runtime_mode=runtime_mode,
|
||||
)
|
||||
return worker_output
|
||||
|
||||
@@ -332,6 +344,7 @@ class AgentScopeRunner:
|
||||
worker_output_model: type[WorkerAgentOutputLite],
|
||||
pipeline: PipelineLike,
|
||||
runtime_client_time: ClientTimeContext | None,
|
||||
runtime_mode: RuntimeMode,
|
||||
work_memory: WorkProfileContent | None,
|
||||
) -> StageExecutionResult:
|
||||
tracking_model = self._build_model(stage_config=stage_config)
|
||||
@@ -340,6 +353,7 @@ class AgentScopeRunner:
|
||||
session_id=run_input.thread_id,
|
||||
run_id=run_input.run_id,
|
||||
stage=stage_config.agent_type.value,
|
||||
runtime_mode=runtime_mode.value,
|
||||
emit_text_events=True,
|
||||
emit_tool_events=True,
|
||||
)
|
||||
@@ -437,12 +451,14 @@ class AgentScopeRunner:
|
||||
run_input: RunAgentInput,
|
||||
step_name: str,
|
||||
event_type: str,
|
||||
runtime_mode: RuntimeMode,
|
||||
extra_event: dict[str, Any] | None = None,
|
||||
) -> None:
|
||||
payload: dict[str, Any] = {
|
||||
"type": event_type,
|
||||
"threadId": run_input.thread_id,
|
||||
"runId": run_input.run_id,
|
||||
"runtime_mode": runtime_mode.value,
|
||||
"stepName": step_name,
|
||||
}
|
||||
if extra_event:
|
||||
@@ -459,6 +475,15 @@ class AgentScopeRunner:
|
||||
getattr(run_input, "forwarded_props", None)
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _resolve_runtime_mode(*, run_input: RunAgentInput) -> RuntimeMode:
|
||||
try:
|
||||
return parse_forwarded_props_runtime_mode(
|
||||
getattr(run_input, "forwarded_props", None)
|
||||
)
|
||||
except ValueError:
|
||||
return RuntimeMode.CHAT
|
||||
|
||||
@staticmethod
|
||||
def _resolve_provider_api_key(*, factory_name: str) -> str:
|
||||
normalized_factory_name = factory_name.strip().upper()
|
||||
|
||||
Reference in New Issue
Block a user