feat(import): preserve supported HTTP contracts

This commit is contained in:
bsodfather committed 2026-09-13 03:30:31 +03:00
1 parent 083974c27f
commit f8ee7c9bfe
87 files changed
+7496 -534

No files matched your search

+51 -5
View File
@@ -6,7 +6,7 @@ use std::time::{Duration, Instant};
use crank_core::{
AdapterRegistry, CoordinationStateStore, ExecutionMode, InvocationSource, InvocationStatus,
MeteringEvent, ResponseCacheStore, SharedMeteringSink, SharedProtocolAdapter,
MeteringEvent, ResponseCacheStore, SharedMeteringSink, SharedProtocolAdapter, Target,
};
use crank_metrics::{
CacheOutcome, ConfirmationOutcome, IdempotencyOutcome, InFlightGuard,
@@ -24,7 +24,7 @@ use crate::{
AdapterResponse, ExecutionAuthorization, PreparedRequest, RuntimeError,
RuntimeExecutionRequest, RuntimeLimits, RuntimeOperation, RuntimeRequestContext,
auth::apply_resolved_auth,
request_preparation::adapter_prepared_request,
request_preparation::{adapter_prepared_request, validate_selected_body_variant},
response_cache::{
adapter_response_from_cached, cached_response_from_adapter, response_cache_key,
response_cache_ttl,
@@ -284,6 +284,7 @@ impl RuntimeExecutor {
.input_schema
.validate_shape(input)
.map_err(RuntimeError::InputSchema)?;
validate_selected_body_variant(operation, input)?;
if operation.input_mapping.is_empty() {
return Ok(PreparedRequest::default());
@@ -293,7 +294,16 @@ impl RuntimeExecutor {
.input_mapping
.apply(&json!({ "mcp": input }))
.map_err(RuntimeError::InputMapping)?;
PreparedRequest::from_mapping_output(&mapped_input)
let mut prepared = PreparedRequest::from_mapping_output(&mapped_input, &operation.target)?;
let Target::Rest(target) = &operation.target;
if let Some(contract) = &target.request_contract
&& input.get(&contract.body_field).is_none()
{
// Mapping initializes an empty body object for legacy field mappings.
// A typed optional body is absent unless its public argument was supplied.
prepared.body = None;
}
Ok(prepared)
}
async fn execute_prepared(
@@ -838,8 +848,7 @@ fn transform_response(
let span = Stage::RuntimeResponseTransform.span();
let result = span.in_scope(|| {
let finalized_output = finalize_output(operation, response)?;
operation
.output_schema
output_schema_for_response(operation, response)?
.validate_shape(&finalized_output)
.map_err(RuntimeError::OutputSchema)?;
Ok(finalized_output)
@@ -854,6 +863,43 @@ fn transform_response(
result
}
fn output_schema_for_response<'a>(
operation: &'a RuntimeOperation,
response: &AdapterResponse,
) -> Result<&'a crank_schema::Schema, RuntimeError> {
let Target::Rest(target) = &operation.target;
if target.response_contract.is_empty() {
return Ok(&operation.output_schema);
}
let media_type = response
.headers
.get("content-type")
.and_then(|value| value.split(';').next())
.map(str::trim);
let descriptor = target
.response_contract
.iter()
.filter(|descriptor| descriptor.status_code == response.status_code)
.find(|descriptor| match (&descriptor.media_type, media_type) {
(Some(expected), Some(actual)) => expected.eq_ignore_ascii_case(actual),
(None, _) => response.body.is_null(),
_ => false,
})
.ok_or(RuntimeError::InvalidExecutionRequest)?;
if target.response_contract.len() > 1
&& operation.output_schema.kind == crank_schema::SchemaKind::Oneof
{
return operation
.output_schema
.variants
.get(descriptor.schema_variant)
.ok_or(RuntimeError::InvalidExecutionRequest);
}
(descriptor.schema_variant == 0)
.then_some(&operation.output_schema)
.ok_or(RuntimeError::InvalidExecutionRequest)
}
fn record_runtime_result<T>(span: &Span, result: &Result<T, RuntimeError>) {
match result {
Ok(_) => StageOutcome::Success.record(span),