Skip to main content

relay_knowledge/api/contracts/
context.rs

1use std::time::{SystemTime, UNIX_EPOCH};
2
3use serde::{Deserialize, Serialize};
4
5/// External interface that initiated a request.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
7#[serde(rename_all = "kebab-case")]
8pub enum InterfaceKind {
9    /// Command-line interface adapter.
10    Cli,
11    /// Web user interface adapter.
12    Web,
13    /// Future HTTP or RPC API adapter.
14    Api,
15    /// Model Context Protocol adapter.
16    Mcp,
17    /// Agent Client Protocol adapter.
18    Acp,
19}
20
21/// Request-scoped identity propagated through application services.
22#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
23pub struct RequestContext {
24    pub interface: InterfaceKind,
25    pub request_id: String,
26    pub trace_id: String,
27}
28
29impl RequestContext {
30    /// Creates a request context for an interface with generated local IDs.
31    pub fn for_interface(interface: InterfaceKind) -> Self {
32        let nanos = SystemTime::now()
33            .duration_since(UNIX_EPOCH)
34            .map_or(0, |duration| duration.as_nanos());
35
36        Self {
37            interface,
38            request_id: format!("req-{nanos}"),
39            trace_id: format!("trace-{nanos}"),
40        }
41    }
42
43    /// Creates a request context with explicit IDs for tests and adapter bridges.
44    pub fn with_ids(
45        interface: InterfaceKind,
46        request_id: impl Into<String>,
47        trace_id: impl Into<String>,
48    ) -> Self {
49        Self {
50            interface,
51            request_id: request_id.into(),
52            trace_id: trace_id.into(),
53        }
54    }
55}