Skip to main content

systemprompt_identifiers/
context.rs

1//! Execution-context identifier — UUID v4 only.
2//!
3//! Copyright (c) systemprompt.io — Business Source License 1.1.
4//! See <https://systemprompt.io> for licensing details.
5
6use crate::error::IdValidationError;
7use crate::{EvalRunId, GatewayConversationId, SessionId, TaskId};
8
9crate::define_id!(ContextId, validated, schema, validate_uuid_v4);
10
11fn validate_uuid_v4(s: &str) -> Result<(), IdValidationError> {
12    uuid::Uuid::parse_str(s).map_err(|e| IdValidationError::invalid("ContextId", e.to_string()))?;
13    Ok(())
14}
15
16const GATEWAY_CONVERSATION_NAMESPACE: uuid::Uuid =
17    uuid::Uuid::from_u128(0x993f_3f2c_f4d9_463b_853a_d3f0_3e19_0898);
18
19const MESSAGING_NAMESPACE: uuid::Uuid =
20    uuid::Uuid::from_u128(0x6b1d_2a7e_9c84_4f31_b5e0_71a2_4d8c_3f06);
21
22const SESSION_NAMESPACE: uuid::Uuid =
23    uuid::Uuid::from_u128(0x4c1e_8b02_7a63_4d51_9f2c_0e58_a7d4_31bb);
24
25const EVALUATION_NAMESPACE: uuid::Uuid =
26    uuid::Uuid::from_u128(0x7f3a_c2d1_5b09_4e87_a6f4_2c91_d05e_88a3);
27
28const CLI_PROBE_NAMESPACE: uuid::Uuid =
29    uuid::Uuid::from_u128(0x2d84_9f60_1c3b_4a72_8e15_b7d0_63f9_a541);
30
31const MCP_VALIDATION_NAMESPACE: uuid::Uuid =
32    uuid::Uuid::from_u128(0x91b6_04ce_7d2f_4380_b8a9_5e16_c74d_2f08);
33
34const TASK_NAMESPACE: uuid::Uuid = uuid::Uuid::from_u128(0x5ae0_37b9_8f14_4c26_9d7b_e842_06c1_fd35);
35
36const LEGACY_CONTEXT_UUID: &str = "00000000-0000-0000-0000-4c4547414359";
37
38impl ContextId {
39    pub fn generate() -> Self {
40        Self::new(uuid::Uuid::new_v4().to_string())
41    }
42
43    #[must_use]
44    pub fn derived_from_gateway_conversation(gw: &GatewayConversationId) -> Self {
45        Self::new(
46            uuid::Uuid::new_v5(&GATEWAY_CONVERSATION_NAMESPACE, gw.as_str().as_bytes()).to_string(),
47        )
48    }
49
50    #[must_use]
51    pub fn derived_from_messaging(platform: &str, org: &str, channel: &str) -> Self {
52        let key = format!("{platform}:{org}:{channel}");
53        Self::new(uuid::Uuid::new_v5(&MESSAGING_NAMESPACE, key.as_bytes()).to_string())
54    }
55
56    #[must_use]
57    pub fn derived_from_session(session_id: &SessionId) -> Self {
58        Self::new(
59            uuid::Uuid::new_v5(&SESSION_NAMESPACE, session_id.as_str().as_bytes()).to_string(),
60        )
61    }
62
63    #[must_use]
64    pub fn derived_from_evaluation_run(run_id: &EvalRunId) -> Self {
65        Self::new(uuid::Uuid::new_v5(&EVALUATION_NAMESPACE, run_id.as_str().as_bytes()).to_string())
66    }
67
68    #[must_use]
69    pub fn derived_from_cli_probe(server_name: &str) -> Self {
70        Self::new(uuid::Uuid::new_v5(&CLI_PROBE_NAMESPACE, server_name.as_bytes()).to_string())
71    }
72
73    #[must_use]
74    pub fn derived_from_mcp_validation(service_name: &str) -> Self {
75        Self::new(
76            uuid::Uuid::new_v5(&MCP_VALIDATION_NAMESPACE, service_name.as_bytes()).to_string(),
77        )
78    }
79
80    #[must_use]
81    pub fn derived_from_task(task_id: &TaskId) -> Self {
82        Self::new(uuid::Uuid::new_v5(&TASK_NAMESPACE, task_id.as_str().as_bytes()).to_string())
83    }
84
85    #[must_use]
86    pub fn legacy() -> Self {
87        Self::new(LEGACY_CONTEXT_UUID)
88    }
89}