refactor: reduce invocation helper arguments

This commit is contained in:
a.tolmachev
2026-03-30 09:40:11 +03:00
parent e4d2f6adc9
commit 0cec8cc826
2 changed files with 103 additions and 91 deletions
+37 -29
View File
@@ -244,14 +244,16 @@ async fn mcp_post(
let _ = persist_invocation(
&state,
&tool,
InvocationStatus::Ok,
InvocationLevel::Info,
"agent tool call completed",
None,
None,
started_at.elapsed(),
request_preview,
output.clone(),
InvocationRecord {
status: InvocationStatus::Ok,
level: InvocationLevel::Info,
message: "agent tool call completed",
status_code: None,
error_kind: None,
duration: started_at.elapsed(),
request_preview,
response_preview: output.clone(),
},
)
.await;
jsonrpc_result(
@@ -275,14 +277,16 @@ async fn mcp_post(
let _ = persist_invocation(
&state,
&tool,
InvocationStatus::Error,
InvocationLevel::Error,
&error.to_string(),
None,
Some(runtime_error_code(&error)),
started_at.elapsed(),
request_preview,
Value::Null,
InvocationRecord {
status: InvocationStatus::Error,
level: InvocationLevel::Error,
message: &error.to_string(),
status_code: None,
error_kind: Some(runtime_error_code(&error)),
duration: started_at.elapsed(),
request_preview,
response_preview: Value::Null,
},
)
.await;
json_response(
@@ -550,38 +554,42 @@ fn build_request_preview(
}
}
async fn persist_invocation(
state: &Arc<AppState>,
tool: &PublishedAgentTool,
struct InvocationRecord<'a> {
status: InvocationStatus,
level: InvocationLevel,
message: &str,
message: &'a str,
status_code: Option<u16>,
error_kind: Option<&str>,
error_kind: Option<&'a str>,
duration: Duration,
request_preview: Value,
response_preview: Value,
}
async fn persist_invocation(
state: &Arc<AppState>,
tool: &PublishedAgentTool,
record: InvocationRecord<'_>,
) -> Result<(), crank_registry::RegistryError> {
let created_at = OffsetDateTime::now_utc()
.format(&Rfc3339)
.unwrap_or_else(|_| "1970-01-01T00:00:00Z".to_owned());
let duration_ms = u64::try_from(duration.as_millis()).unwrap_or(u64::MAX);
let duration_ms = u64::try_from(record.duration.as_millis()).unwrap_or(u64::MAX);
let log = InvocationLog {
id: InvocationLogId::new(format!("log_{}", uuid::Uuid::now_v7().simple())),
workspace_id: tool.workspace_id.clone(),
agent_id: Some(tool.agent_id.clone()),
operation_id: tool.operation.id.clone(),
source: InvocationSource::AgentToolCall,
level,
status,
level: record.level,
status: record.status,
tool_name: tool.tool_name.clone(),
message: message.to_owned(),
message: record.message.to_owned(),
request_id: None,
status_code,
status_code: record.status_code,
duration_ms,
error_kind: error_kind.map(ToOwned::to_owned),
request_preview,
response_preview,
error_kind: record.error_kind.map(ToOwned::to_owned),
request_preview: record.request_preview,
response_preview: record.response_preview,
created_at,
};