|
|
@@ -17,10 +17,11 @@ from core_domain import (
|
|
|
from core_shared import JSONValue
|
|
|
|
|
|
from app.bootstrap.settings import AgentServiceSettings
|
|
|
-from app.db.models import AgentDefinition, AgentRun, AgentVersion
|
|
|
+from app.db.models import AgentDefinition, AgentRun, AgentToolInvocation, AgentVersion
|
|
|
from app.domain.repositories import (
|
|
|
AgentDefinitionRepository,
|
|
|
AgentRunRepository,
|
|
|
+ AgentToolInvocationRepository,
|
|
|
AgentVersionRepository,
|
|
|
)
|
|
|
from app.infrastructure.model_gateway_client import ModelGatewayClient, ModelGatewayClientError
|
|
|
@@ -44,6 +45,7 @@ class AgentApplicationService:
|
|
|
agent_repository: AgentDefinitionRepository,
|
|
|
agent_version_repository: AgentVersionRepository,
|
|
|
agent_run_repository: AgentRunRepository,
|
|
|
+ agent_tool_invocation_repository: AgentToolInvocationRepository,
|
|
|
model_gateway_client: ModelGatewayClient | None = None,
|
|
|
memory_client: MemoryClient | None = None,
|
|
|
tool_client: ToolServiceClient | None = None,
|
|
|
@@ -53,6 +55,7 @@ class AgentApplicationService:
|
|
|
self.agent_repository = agent_repository
|
|
|
self.agent_version_repository = agent_version_repository
|
|
|
self.agent_run_repository = agent_run_repository
|
|
|
+ self.agent_tool_invocation_repository = agent_tool_invocation_repository
|
|
|
self.model_gateway_client = model_gateway_client
|
|
|
self.memory_client = memory_client
|
|
|
self.tool_client = tool_client
|
|
|
@@ -146,6 +149,17 @@ class AgentApplicationService:
|
|
|
session_id=session_id,
|
|
|
)
|
|
|
|
|
|
+ def list_agent_tool_invocations(
|
|
|
+ self,
|
|
|
+ *,
|
|
|
+ tenant_id: str,
|
|
|
+ agent_run_id: str,
|
|
|
+ ) -> list[AgentToolInvocation]:
|
|
|
+ return self.agent_tool_invocation_repository.list_by_run(
|
|
|
+ tenant_id=tenant_id,
|
|
|
+ agent_run_id=agent_run_id,
|
|
|
+ )
|
|
|
+
|
|
|
def update_agent_run_status(
|
|
|
self,
|
|
|
*,
|
|
|
@@ -252,6 +266,7 @@ class AgentApplicationService:
|
|
|
|
|
|
tool_invocations = self._invoke_selected_tools(
|
|
|
agent_run=agent_run,
|
|
|
+ agent_version=agent_version,
|
|
|
selected_tools=selected_tools,
|
|
|
)
|
|
|
skill_invocations = self._invoke_selected_skills(
|
|
|
@@ -494,11 +509,27 @@ class AgentApplicationService:
|
|
|
self,
|
|
|
*,
|
|
|
agent_run: AgentRun,
|
|
|
+ agent_version: AgentVersion,
|
|
|
selected_tools: list[AgentToolRefContract],
|
|
|
) -> list[dict[str, JSONValue]]:
|
|
|
invocations: list[dict[str, JSONValue]] = []
|
|
|
for ref in selected_tools:
|
|
|
+ invocation = self.agent_tool_invocation_repository.create(
|
|
|
+ tenant_id=agent_run.tenant_id,
|
|
|
+ agent_run_id=agent_run.id,
|
|
|
+ agent_id=agent_run.agent_id,
|
|
|
+ agent_version_id=agent_version.id,
|
|
|
+ tool_code=ref.tool_code,
|
|
|
+ tool_binding_id=ref.tool_binding_id,
|
|
|
+ status="selected",
|
|
|
+ input_json=agent_run.input_json or {},
|
|
|
+ )
|
|
|
if ref.tool_binding_id is None:
|
|
|
+ self.agent_tool_invocation_repository.update_status(
|
|
|
+ invocation_id=invocation.id,
|
|
|
+ status="skipped",
|
|
|
+ reason="tool_binding_id_missing",
|
|
|
+ )
|
|
|
invocations.append(
|
|
|
{
|
|
|
"status": "skipped",
|
|
|
@@ -508,6 +539,12 @@ class AgentApplicationService:
|
|
|
)
|
|
|
continue
|
|
|
if self.tool_client is None:
|
|
|
+ self.agent_tool_invocation_repository.update_status(
|
|
|
+ invocation_id=invocation.id,
|
|
|
+ status="failed",
|
|
|
+ reason="tool_client_missing",
|
|
|
+ error_message="tool client is not configured",
|
|
|
+ )
|
|
|
invocations.append(
|
|
|
{
|
|
|
"status": "failed",
|
|
|
@@ -517,11 +554,21 @@ class AgentApplicationService:
|
|
|
)
|
|
|
continue
|
|
|
try:
|
|
|
+ self.agent_tool_invocation_repository.update_status(
|
|
|
+ invocation_id=invocation.id,
|
|
|
+ status="running",
|
|
|
+ )
|
|
|
detail = self.tool_client.get_tool_binding_detail(
|
|
|
tenant_id=agent_run.tenant_id,
|
|
|
binding_id=ref.tool_binding_id,
|
|
|
)
|
|
|
if not detail.binding.enabled:
|
|
|
+ self.agent_tool_invocation_repository.update_status(
|
|
|
+ invocation_id=invocation.id,
|
|
|
+ status="failed",
|
|
|
+ reason="tool_binding_disabled",
|
|
|
+ error_message="tool binding is disabled",
|
|
|
+ )
|
|
|
invocations.append(
|
|
|
{
|
|
|
"status": "failed",
|
|
|
@@ -531,6 +578,12 @@ class AgentApplicationService:
|
|
|
)
|
|
|
continue
|
|
|
if detail.tool_definition.tool_type != "http":
|
|
|
+ self.agent_tool_invocation_repository.update_status(
|
|
|
+ invocation_id=invocation.id,
|
|
|
+ status="skipped",
|
|
|
+ reason="unsupported_tool_type",
|
|
|
+ error_message=detail.tool_definition.tool_type,
|
|
|
+ )
|
|
|
invocations.append(
|
|
|
{
|
|
|
"status": "skipped",
|
|
|
@@ -546,6 +599,12 @@ class AgentApplicationService:
|
|
|
config_json=ref.config_json,
|
|
|
)
|
|
|
except ToolServiceClientError as exc:
|
|
|
+ self.agent_tool_invocation_repository.update_status(
|
|
|
+ invocation_id=invocation.id,
|
|
|
+ status="failed",
|
|
|
+ reason="tool_service_error",
|
|
|
+ error_message=str(exc),
|
|
|
+ )
|
|
|
invocations.append(
|
|
|
{
|
|
|
"status": "failed",
|
|
|
@@ -555,6 +614,12 @@ class AgentApplicationService:
|
|
|
)
|
|
|
continue
|
|
|
|
|
|
+ self.agent_tool_invocation_repository.update_status(
|
|
|
+ invocation_id=invocation.id,
|
|
|
+ status="completed",
|
|
|
+ output_text=output_text,
|
|
|
+ output_json=output_json,
|
|
|
+ )
|
|
|
invocations.append(
|
|
|
{
|
|
|
"status": "completed",
|
|
|
@@ -939,6 +1004,7 @@ def build_agent_application_service(
|
|
|
agent_repository=AgentDefinitionRepository(db),
|
|
|
agent_version_repository=AgentVersionRepository(db),
|
|
|
agent_run_repository=AgentRunRepository(db),
|
|
|
+ agent_tool_invocation_repository=AgentToolInvocationRepository(db),
|
|
|
model_gateway_client=ModelGatewayClient(
|
|
|
base_url=settings.model_gateway_service_url,
|
|
|
timeout_seconds=settings.model_gateway_timeout_seconds,
|