feat(call): introduce StreamingHandler, HandlerKind, ResponseStream + INVALID_OPERATION_TYPE (ADR-049)
Add the foundational types for ADR-049 streaming handlers: - StreamingHandler, ResponseStream type aliases and HandlerKind enum (Once | Stream) in registration.rs, with make_streaming_handler() helper - CallError::invalid_operation_type() in wire.rs (sixth protocol code, retryable: false) - HandlerRegistration.handler flipped from Handler to HandlerKind; HandlerRegistration::new() now takes HandlerKind - OperationRegistryBuilder absorbs wrapping: with_local/with_leaf/ with_leaf_provenance wrap raw Handler in HandlerKind::Once for Query/Mutation; new with_local_streaming/with_leaf_streaming take a StreamingHandler and wrap in HandlerKind::Stream for Subscription. Builder validates kind matches spec.op_type (mismatch = startup error) - OperationRegistry::register() returns Result<(), String> with a clear mismatch message; all call sites updated to handle the Result - invoke() matches on HandlerKind: Once -> existing path; Stream -> INVALID_OPERATION_TYPE error envelope (guards against silent truncation; invoke_streaming() added in a downstream task) - OverlayOperationEnv::invoke_with_policy matches on HandlerKind: Once -> dispatch; Stream -> INVALID_OPERATION_TYPE (composition is request/response-only) - Migrated every HandlerRegistration::new() construction site (~95) to wrap raw Handler in HandlerKind::Once(handler); the builder sites are handled by the builder-absorbs-wrapping change - Updated two websocket subscription tests that relied on Subscription ops dispatching via invoke() to expect INVALID_OPERATION_TYPE - Added unit tests for invoke/register validation and make_streaming_handler
This commit is contained in:
@@ -12,7 +12,9 @@
|
||||
use alknet_call::client::{AdapterError, OperationAdapter};
|
||||
use alknet_call::protocol::wire::{CallError, ResponseEnvelope};
|
||||
use alknet_call::registry::context::OperationContext;
|
||||
use alknet_call::registry::registration::{make_handler, HandlerRegistration, OperationProvenance};
|
||||
use alknet_call::registry::registration::{
|
||||
make_handler, HandlerKind, HandlerRegistration, OperationProvenance,
|
||||
};
|
||||
use alknet_call::registry::spec::{
|
||||
AccessControl, ErrorDefinition, OperationSpec, OperationType, Visibility,
|
||||
};
|
||||
@@ -156,7 +158,7 @@ fn build_registration(
|
||||
|
||||
HandlerRegistration::new(
|
||||
spec,
|
||||
handler,
|
||||
HandlerKind::Once(handler),
|
||||
OperationProvenance::FromMCP,
|
||||
None,
|
||||
None,
|
||||
|
||||
@@ -17,7 +17,9 @@ use std::sync::Arc;
|
||||
use alknet_call::client::{AdapterError, OperationAdapter};
|
||||
use alknet_call::protocol::wire::{CallError, ResponseEnvelope};
|
||||
use alknet_call::registry::context::OperationContext;
|
||||
use alknet_call::registry::registration::{make_handler, HandlerRegistration, OperationProvenance};
|
||||
use alknet_call::registry::registration::{
|
||||
make_handler, HandlerKind, HandlerRegistration, OperationProvenance,
|
||||
};
|
||||
use alknet_call::registry::spec::{
|
||||
AccessControl, ErrorDefinition, OperationSpec, OperationType, Visibility,
|
||||
};
|
||||
@@ -469,7 +471,7 @@ impl FromOpenAPI {
|
||||
let capabilities = Capabilities::new();
|
||||
Ok(HandlerRegistration::new(
|
||||
spec,
|
||||
handler,
|
||||
HandlerKind::Once(handler),
|
||||
OperationProvenance::FromOpenAPI,
|
||||
None,
|
||||
None,
|
||||
@@ -1151,7 +1153,10 @@ mod tests {
|
||||
.unwrap();
|
||||
let registration = &bundles[0];
|
||||
let ctx = noop_context("req-10", Capabilities::new());
|
||||
let response = (registration.handler)(serde_json::json!({}), ctx).await;
|
||||
let response = match ®istration.handler {
|
||||
HandlerKind::Once(h) => h(serde_json::json!({}), ctx).await,
|
||||
_ => panic!("expected Once handler"),
|
||||
};
|
||||
assert_eq!(response.request_id, "req-10");
|
||||
match response.result {
|
||||
Ok(v) => assert_eq!(v, serde_json::json!({"ok":true})),
|
||||
@@ -1176,7 +1181,10 @@ mod tests {
|
||||
.unwrap();
|
||||
let registration = &bundles[0];
|
||||
let ctx = noop_context("req-11", Capabilities::new());
|
||||
let response = (registration.handler)(serde_json::json!({}), ctx).await;
|
||||
let response = match ®istration.handler {
|
||||
HandlerKind::Once(h) => h(serde_json::json!({}), ctx).await,
|
||||
_ => panic!("expected Once handler"),
|
||||
};
|
||||
match response.result {
|
||||
Err(e) => {
|
||||
assert_eq!(e.code, "HTTP_404");
|
||||
@@ -1201,7 +1209,10 @@ mod tests {
|
||||
.unwrap();
|
||||
let registration = &bundles[0];
|
||||
let ctx = noop_context("req-12", Capabilities::new());
|
||||
let response = (registration.handler)(serde_json::json!({}), ctx).await;
|
||||
let response = match ®istration.handler {
|
||||
HandlerKind::Once(h) => h(serde_json::json!({}), ctx).await,
|
||||
_ => panic!("expected Once handler"),
|
||||
};
|
||||
assert!(response.result.is_ok());
|
||||
let last = response.result.unwrap();
|
||||
assert_eq!(last, serde_json::json!({"n":2}));
|
||||
@@ -1447,11 +1458,16 @@ mod tests {
|
||||
.unwrap();
|
||||
let registration = &bundles[0];
|
||||
let ctx = noop_context("req-16", Capabilities::new());
|
||||
let response = (registration.handler)(
|
||||
serde_json::json!({"id":"42","filter":"new","body":{"name":"widget"}}),
|
||||
ctx,
|
||||
)
|
||||
.await;
|
||||
let response = match ®istration.handler {
|
||||
HandlerKind::Once(h) => {
|
||||
h(
|
||||
serde_json::json!({"id":"42","filter":"new","body":{"name":"widget"}}),
|
||||
ctx,
|
||||
)
|
||||
.await
|
||||
}
|
||||
_ => panic!("expected Once handler"),
|
||||
};
|
||||
assert!(
|
||||
response.result.is_ok(),
|
||||
"expected Ok, got {:?}",
|
||||
@@ -1483,7 +1499,10 @@ mod tests {
|
||||
let registration = &bundles[0];
|
||||
let caps = Capabilities::new().with_http_token("openai", "sk-test-token".to_string());
|
||||
let ctx = noop_context("req-17", caps);
|
||||
let _ = (registration.handler)(serde_json::json!({}), ctx).await;
|
||||
let _ = match ®istration.handler {
|
||||
HandlerKind::Once(h) => h(serde_json::json!({}), ctx).await,
|
||||
_ => panic!("expected Once handler"),
|
||||
};
|
||||
let captured = rx.await.unwrap();
|
||||
assert_eq!(
|
||||
captured.headers.get("authorization").unwrap(),
|
||||
@@ -1519,7 +1538,10 @@ mod tests {
|
||||
.unwrap();
|
||||
let registration = &bundles[0];
|
||||
let ctx = noop_context("req-18", Capabilities::new());
|
||||
let response = (registration.handler)(serde_json::json!({}), ctx).await;
|
||||
let response = match ®istration.handler {
|
||||
HandlerKind::Once(h) => h(serde_json::json!({}), ctx).await,
|
||||
_ => panic!("expected Once handler"),
|
||||
};
|
||||
match response.result {
|
||||
Ok(Value::String(s)) => assert_eq!(s, "hello world"),
|
||||
other => panic!("expected String, got {other:?}"),
|
||||
@@ -1540,7 +1562,10 @@ mod tests {
|
||||
.unwrap();
|
||||
let registration = &bundles[0];
|
||||
let ctx = noop_context("req-19", Capabilities::new());
|
||||
let response = (registration.handler)(serde_json::json!({}), ctx).await;
|
||||
let response = match ®istration.handler {
|
||||
HandlerKind::Once(h) => h(serde_json::json!({}), ctx).await,
|
||||
_ => panic!("expected Once handler"),
|
||||
};
|
||||
match response.result {
|
||||
Err(e) => assert_eq!(e.code, "HTTP_500"),
|
||||
other => panic!("expected HTTP_500, got {other:?}"),
|
||||
|
||||
@@ -432,7 +432,7 @@ mod tests {
|
||||
services_list_handler, services_list_spec, services_schema_handler, services_schema_spec,
|
||||
};
|
||||
use alknet_call::registry::registration::{
|
||||
make_handler, HandlerRegistration, OperationProvenance, OperationRegistry,
|
||||
make_handler, HandlerKind, HandlerRegistration, OperationProvenance, OperationRegistry,
|
||||
};
|
||||
use alknet_call::registry::spec::{AccessControl, OperationSpec, OperationType, Visibility};
|
||||
use alknet_core::auth::{AuthToken, Identity, IdentityProvider};
|
||||
@@ -502,44 +502,52 @@ mod tests {
|
||||
) -> Arc<OperationRegistry> {
|
||||
let mut inner = OperationRegistry::new();
|
||||
for (name, op_type, acl) in specs {
|
||||
inner.register(HandlerRegistration::new(
|
||||
external_spec(&name, op_type, acl),
|
||||
make_echo_handler(),
|
||||
OperationProvenance::Local,
|
||||
None,
|
||||
None,
|
||||
Capabilities::new(),
|
||||
));
|
||||
inner
|
||||
.register(HandlerRegistration::new(
|
||||
external_spec(&name, op_type, acl),
|
||||
HandlerKind::Once(make_echo_handler()),
|
||||
OperationProvenance::Local,
|
||||
None,
|
||||
None,
|
||||
Capabilities::new(),
|
||||
))
|
||||
.unwrap();
|
||||
}
|
||||
let inner = Arc::new(inner);
|
||||
|
||||
let mut dispatch_registry = OperationRegistry::new();
|
||||
for op in inner.list_operations() {
|
||||
dispatch_registry.register(HandlerRegistration::new(
|
||||
external_spec(&op.name, op.op_type, op.access_control.clone()),
|
||||
make_echo_handler(),
|
||||
dispatch_registry
|
||||
.register(HandlerRegistration::new(
|
||||
external_spec(&op.name, op.op_type, op.access_control.clone()),
|
||||
HandlerKind::Once(make_echo_handler()),
|
||||
OperationProvenance::Local,
|
||||
None,
|
||||
None,
|
||||
Capabilities::new(),
|
||||
))
|
||||
.unwrap();
|
||||
}
|
||||
dispatch_registry
|
||||
.register(HandlerRegistration::new(
|
||||
services_list_spec(),
|
||||
HandlerKind::Once(services_list_handler(Arc::clone(&inner))),
|
||||
OperationProvenance::Local,
|
||||
None,
|
||||
None,
|
||||
ScopedPeerEnv::empty().into(),
|
||||
Capabilities::new(),
|
||||
));
|
||||
}
|
||||
dispatch_registry.register(HandlerRegistration::new(
|
||||
services_list_spec(),
|
||||
services_list_handler(Arc::clone(&inner)),
|
||||
OperationProvenance::Local,
|
||||
None,
|
||||
ScopedPeerEnv::empty().into(),
|
||||
Capabilities::new(),
|
||||
));
|
||||
dispatch_registry.register(HandlerRegistration::new(
|
||||
services_schema_spec(),
|
||||
services_schema_handler(Arc::clone(&inner)),
|
||||
OperationProvenance::Local,
|
||||
None,
|
||||
ScopedPeerEnv::empty().into(),
|
||||
Capabilities::new(),
|
||||
));
|
||||
))
|
||||
.unwrap();
|
||||
dispatch_registry
|
||||
.register(HandlerRegistration::new(
|
||||
services_schema_spec(),
|
||||
HandlerKind::Once(services_schema_handler(Arc::clone(&inner))),
|
||||
OperationProvenance::Local,
|
||||
None,
|
||||
ScopedPeerEnv::empty().into(),
|
||||
Capabilities::new(),
|
||||
))
|
||||
.unwrap();
|
||||
Arc::new(dispatch_registry)
|
||||
}
|
||||
|
||||
|
||||
@@ -528,7 +528,7 @@ mod tests {
|
||||
use super::*;
|
||||
use alknet_call::protocol::wire::ResponseEnvelope;
|
||||
use alknet_call::registry::registration::{
|
||||
make_handler, HandlerRegistration, OperationProvenance,
|
||||
make_handler, HandlerKind, HandlerRegistration, OperationProvenance,
|
||||
};
|
||||
use alknet_call::registry::spec::{AccessControl, OperationSpec, OperationType, Visibility};
|
||||
use alknet_core::types::Capabilities;
|
||||
@@ -539,14 +539,16 @@ mod tests {
|
||||
}
|
||||
|
||||
fn register(registry: &mut OperationRegistry, spec: OperationSpec) {
|
||||
registry.register(HandlerRegistration::new(
|
||||
spec,
|
||||
noop_handler(),
|
||||
OperationProvenance::Local,
|
||||
None,
|
||||
None,
|
||||
Capabilities::new(),
|
||||
));
|
||||
registry
|
||||
.register(HandlerRegistration::new(
|
||||
spec,
|
||||
HandlerKind::Once(noop_handler()),
|
||||
OperationProvenance::Local,
|
||||
None,
|
||||
None,
|
||||
Capabilities::new(),
|
||||
))
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn external_spec(name: &str, errors: Vec<ErrorDefinition>) -> OperationSpec {
|
||||
@@ -1003,22 +1005,24 @@ mod tests {
|
||||
#[test]
|
||||
fn internal_operations_excluded_from_error_projection() {
|
||||
let mut registry = OperationRegistry::new();
|
||||
registry.register(HandlerRegistration::new(
|
||||
OperationSpec::new(
|
||||
"internal/op",
|
||||
OperationType::Query,
|
||||
Visibility::Internal,
|
||||
json!({}),
|
||||
json!({}),
|
||||
vec![error("INTERNAL_ERROR", Some(418))],
|
||||
AccessControl::default(),
|
||||
),
|
||||
noop_handler(),
|
||||
OperationProvenance::Local,
|
||||
None,
|
||||
None,
|
||||
Capabilities::new(),
|
||||
));
|
||||
registry
|
||||
.register(HandlerRegistration::new(
|
||||
OperationSpec::new(
|
||||
"internal/op",
|
||||
OperationType::Query,
|
||||
Visibility::Internal,
|
||||
json!({}),
|
||||
json!({}),
|
||||
vec![error("INTERNAL_ERROR", Some(418))],
|
||||
AccessControl::default(),
|
||||
),
|
||||
HandlerKind::Once(noop_handler()),
|
||||
OperationProvenance::Local,
|
||||
None,
|
||||
None,
|
||||
Capabilities::new(),
|
||||
))
|
||||
.unwrap();
|
||||
let spec = to_openapi(®istry);
|
||||
let responses = responses(&spec, PATH_CALL, "post");
|
||||
assert!(
|
||||
|
||||
Reference in New Issue
Block a user