249 lines
7.4 KiB
Rust
249 lines
7.4 KiB
Rust
use std::collections::BTreeMap;
|
|
|
|
use crank_core::{
|
|
AgentId, CorrelationContext, InvocationSource, RequestId, TraceContext, WorkspaceId,
|
|
};
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
pub struct ResponseCacheScope {
|
|
pub workspace_key: String,
|
|
pub agent_key: String,
|
|
}
|
|
|
|
#[derive(Clone, PartialEq, Eq)]
|
|
pub struct RuntimeRequestContext {
|
|
pub request_id: RequestId,
|
|
pub trace_context: TraceContext,
|
|
pub response_cache_scope: Option<ResponseCacheScope>,
|
|
pub metering_context: Option<MeteringContext>,
|
|
pub confirmation_token: Option<String>,
|
|
pub approval_granted: bool,
|
|
}
|
|
|
|
impl std::fmt::Debug for RuntimeRequestContext {
|
|
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
formatter
|
|
.debug_struct("RuntimeRequestContext")
|
|
.field("request_id", &self.request_id)
|
|
.field("trace_context", &self.trace_context)
|
|
.field("response_cache_scope", &self.response_cache_scope)
|
|
.field("metering_context", &self.metering_context)
|
|
.field(
|
|
"confirmation_token",
|
|
&self.confirmation_token.as_ref().map(|_| "[REDACTED]"),
|
|
)
|
|
.field("approval_granted", &self.approval_granted)
|
|
.finish()
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
pub struct MeteringContext {
|
|
pub workspace_id: WorkspaceId,
|
|
pub agent_id: Option<AgentId>,
|
|
pub source: InvocationSource,
|
|
}
|
|
|
|
impl RuntimeRequestContext {
|
|
pub fn new(request_id: RequestId, trace_context: TraceContext) -> Self {
|
|
Self {
|
|
request_id,
|
|
trace_context,
|
|
response_cache_scope: None,
|
|
metering_context: None,
|
|
confirmation_token: None,
|
|
approval_granted: false,
|
|
}
|
|
}
|
|
|
|
pub fn from_request_id(request_id: impl Into<String>) -> Self {
|
|
let request_id = request_id.into();
|
|
Self::new(
|
|
RequestId::resolve(Some(&request_id)),
|
|
TraceContext::generate(),
|
|
)
|
|
}
|
|
|
|
pub fn from_correlation(context: &CorrelationContext) -> Self {
|
|
Self::new(
|
|
context.request_id().clone(),
|
|
context.trace_context().clone(),
|
|
)
|
|
}
|
|
|
|
pub fn outbound_headers(&self) -> BTreeMap<String, String> {
|
|
BTreeMap::from([
|
|
("x-request-id".to_owned(), self.request_id.to_string()),
|
|
(
|
|
"x-trace-id".to_owned(),
|
|
self.trace_context.trace_id().to_string(),
|
|
),
|
|
(
|
|
"traceparent".to_owned(),
|
|
self.trace_context.traceparent().to_owned(),
|
|
),
|
|
("x-correlation-id".to_owned(), self.request_id.to_string()),
|
|
])
|
|
}
|
|
|
|
pub fn with_response_cache_scope(
|
|
mut self,
|
|
workspace_key: impl Into<String>,
|
|
agent_key: impl Into<String>,
|
|
) -> Self {
|
|
self.response_cache_scope = Some(ResponseCacheScope {
|
|
workspace_key: workspace_key.into(),
|
|
agent_key: agent_key.into(),
|
|
});
|
|
self
|
|
}
|
|
|
|
pub fn response_cache_scope(&self) -> Option<&ResponseCacheScope> {
|
|
self.response_cache_scope.as_ref()
|
|
}
|
|
|
|
pub fn with_metering_context(
|
|
mut self,
|
|
workspace_id: WorkspaceId,
|
|
agent_id: Option<AgentId>,
|
|
source: InvocationSource,
|
|
) -> Self {
|
|
self.metering_context = Some(MeteringContext {
|
|
workspace_id,
|
|
agent_id,
|
|
source,
|
|
});
|
|
self
|
|
}
|
|
|
|
pub fn metering_context(&self) -> Option<&MeteringContext> {
|
|
self.metering_context.as_ref()
|
|
}
|
|
|
|
pub fn with_confirmation_token(mut self, token: impl Into<String>) -> Self {
|
|
self.confirmation_token = Some(token.into());
|
|
self
|
|
}
|
|
|
|
pub fn confirmation_token(&self) -> Option<&str> {
|
|
self.confirmation_token.as_deref()
|
|
}
|
|
|
|
pub fn with_approval_granted(mut self) -> Self {
|
|
self.approval_granted = true;
|
|
self
|
|
}
|
|
|
|
pub fn approval_granted(&self) -> bool {
|
|
self.approval_granted
|
|
}
|
|
}
|
|
|
|
impl From<ResponseCacheScope> for crank_core::ResponseCacheScope {
|
|
fn from(value: ResponseCacheScope) -> Self {
|
|
Self {
|
|
workspace_key: value.workspace_key,
|
|
agent_key: value.agent_key,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<&ResponseCacheScope> for crank_core::ResponseCacheScope {
|
|
fn from(value: &ResponseCacheScope) -> Self {
|
|
Self {
|
|
workspace_key: value.workspace_key.clone(),
|
|
agent_key: value.agent_key.clone(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<&RuntimeRequestContext> for crank_core::RuntimeRequestContext {
|
|
fn from(value: &RuntimeRequestContext) -> Self {
|
|
let mut context = crank_core::RuntimeRequestContext::new(
|
|
value.request_id.clone(),
|
|
value.trace_context.clone(),
|
|
);
|
|
context.response_cache_scope = value.response_cache_scope.as_ref().map(Into::into);
|
|
context.metering_context = value.metering_context.as_ref().map(Into::into);
|
|
context
|
|
}
|
|
}
|
|
|
|
impl From<MeteringContext> for crank_core::MeteringContext {
|
|
fn from(value: MeteringContext) -> Self {
|
|
Self {
|
|
workspace_id: value.workspace_id,
|
|
agent_id: value.agent_id,
|
|
source: value.source,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<&MeteringContext> for crank_core::MeteringContext {
|
|
fn from(value: &MeteringContext) -> Self {
|
|
Self {
|
|
workspace_id: value.workspace_id.clone(),
|
|
agent_id: value.agent_id.clone(),
|
|
source: value.source,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use crank_core::{InvocationSource, WorkspaceId};
|
|
|
|
use super::RuntimeRequestContext;
|
|
|
|
#[test]
|
|
fn generates_a_separate_default_trace_identity() {
|
|
let context = RuntimeRequestContext::from_request_id("req_123");
|
|
|
|
assert_eq!(context.request_id.as_str(), "req_123");
|
|
assert_ne!(context.trace_context.trace_id().as_str(), "req_123");
|
|
assert!(context.response_cache_scope.is_none());
|
|
assert!(context.confirmation_token.is_none());
|
|
assert!(!context.approval_granted());
|
|
}
|
|
|
|
#[test]
|
|
fn attaches_response_cache_scope() {
|
|
let context = RuntimeRequestContext::from_request_id("req_123")
|
|
.with_response_cache_scope("ws_01", "agent_01");
|
|
|
|
let scope = context.response_cache_scope().unwrap();
|
|
assert_eq!(scope.workspace_key, "ws_01");
|
|
assert_eq!(scope.agent_key, "agent_01");
|
|
}
|
|
|
|
#[test]
|
|
fn attaches_metering_context() {
|
|
let context = RuntimeRequestContext::from_request_id("req_123").with_metering_context(
|
|
WorkspaceId::new("ws_01"),
|
|
None,
|
|
InvocationSource::AdminTestRun,
|
|
);
|
|
|
|
let metering = context.metering_context().unwrap();
|
|
assert_eq!(metering.workspace_id, WorkspaceId::new("ws_01"));
|
|
assert_eq!(metering.agent_id, None);
|
|
assert_eq!(metering.source, InvocationSource::AdminTestRun);
|
|
}
|
|
|
|
#[test]
|
|
fn attaches_confirmation_token() {
|
|
let context =
|
|
RuntimeRequestContext::from_request_id("req_123").with_confirmation_token("ct_123");
|
|
|
|
assert_eq!(context.confirmation_token(), Some("ct_123"));
|
|
assert!(!format!("{context:?}").contains("ct_123"));
|
|
}
|
|
|
|
#[test]
|
|
fn records_prior_human_approval() {
|
|
let context = RuntimeRequestContext::from_request_id("req_123").with_approval_granted();
|
|
|
|
assert!(context.approval_granted());
|
|
}
|
|
}
|