runtime: propagate request context through adapters

This commit is contained in:
a.tolmachev
2026-05-01 11:44:43 +00:00
parent 3b1a7c6993
commit 7be6bed347
6 changed files with 530 additions and 24 deletions
+28 -5
View File
@@ -29,7 +29,10 @@ use crank_registry::{
PostgresRegistry, PublishedAgentTool, UpdateAsyncJobStatusRequest,
UpdateStreamSessionStateRequest,
};
use crank_runtime::{ResolvedAuth, RuntimeError, RuntimeExecutor, RuntimeOperation, SecretCrypto};
use crank_runtime::{
ResolvedAuth, RuntimeError, RuntimeExecutor, RuntimeOperation, RuntimeRequestContext,
SecretCrypto,
};
use futures_util::stream;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
@@ -628,6 +631,7 @@ async fn handle_base_tool_call(
transport_request_id: &str,
) -> Response {
let operation = runtime_operation(&tool);
let runtime_request_context = RuntimeRequestContext::from_request_id(transport_request_id);
let request_preview = build_request_preview(&state.runtime, &operation, &arguments);
let started_at = Instant::now();
let is_window_mode = matches!(
@@ -644,7 +648,12 @@ async fn handle_base_tool_call(
let result = match resolved_auth {
Ok(resolved_auth) if is_window_mode => state
.runtime
.execute_window_with_auth(&operation, &arguments, resolved_auth.as_ref())
.execute_window_with_auth_and_context(
&operation,
&arguments,
resolved_auth.as_ref(),
Some(&runtime_request_context),
)
.await
.map(|output| {
json!({
@@ -659,7 +668,12 @@ async fn handle_base_tool_call(
Ok(resolved_auth) => {
state
.runtime
.execute_with_auth(&operation, &arguments, resolved_auth.as_ref())
.execute_with_auth_and_context(
&operation,
&arguments,
resolved_auth.as_ref(),
Some(&runtime_request_context),
)
.await
}
Err(error) => Err(error),
@@ -728,6 +742,7 @@ async fn handle_session_start_call(
transport_request_id: &str,
) -> Response {
let runtime_operation = runtime_operation(&tool);
let runtime_request_context = RuntimeRequestContext::from_request_id(transport_request_id);
let request_preview = build_request_preview(&state.runtime, &runtime_operation, &arguments);
let started_at = Instant::now();
let Some(streaming) = runtime_operation.execution_config.streaming.as_ref() else {
@@ -751,10 +766,11 @@ async fn handle_session_start_call(
Ok(resolved_auth) => {
state
.runtime
.execute_session_seed_with_auth(
.execute_session_seed_with_auth_and_context(
&runtime_operation,
&arguments,
resolved_auth.as_ref(),
Some(&runtime_request_context),
)
.await
}
@@ -1135,6 +1151,7 @@ async fn handle_async_job_start_call(
transport_request_id: &str,
) -> Response {
let operation = runtime_operation(&tool);
let runtime_request_context = RuntimeRequestContext::from_request_id(transport_request_id);
let request_preview = build_request_preview(&state.runtime, &operation, &arguments);
let Some(streaming) = tool.operation.execution_config.streaming.as_ref() else {
return tool_error_response(
@@ -1178,6 +1195,7 @@ async fn handle_async_job_start_call(
let secret_crypto = state.secret_crypto.clone();
let tool_for_task = tool.clone();
let arguments_for_task = arguments.clone();
let request_context_for_task = runtime_request_context.clone();
let job_id = job.id.clone();
tokio::spawn(async move {
let runtime = RuntimeExecutor::new();
@@ -1192,7 +1210,12 @@ async fn handle_async_job_start_call(
let result = match resolved_auth {
Ok(resolved_auth) => {
runtime
.execute_with_auth(&task_operation, &arguments_for_task, resolved_auth.as_ref())
.execute_with_auth_and_context(
&task_operation,
&arguments_for_task,
resolved_auth.as_ref(),
Some(&request_context_for_task),
)
.await
}
Err(error) => Err(error),