systemprompt_identifiers/
context.rs1use crate::error::IdValidationError;
7use crate::{ClientSessionId, GatewayConversationId, SessionId, TaskId, UserId};
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 CLI_PROBE_NAMESPACE: uuid::Uuid =
26 uuid::Uuid::from_u128(0x2d84_9f60_1c3b_4a72_8e15_b7d0_63f9_a541);
27
28const MCP_VALIDATION_NAMESPACE: uuid::Uuid =
29 uuid::Uuid::from_u128(0x91b6_04ce_7d2f_4380_b8a9_5e16_c74d_2f08);
30
31const TASK_NAMESPACE: uuid::Uuid = uuid::Uuid::from_u128(0x5ae0_37b9_8f14_4c26_9d7b_e842_06c1_fd35);
32
33const LEGACY_CONTEXT_UUID: &str = "00000000-0000-0000-0000-4c4547414359";
34
35impl ContextId {
36 pub fn generate() -> Self {
37 Self(uuid::Uuid::new_v4().to_string())
38 }
39
40 #[must_use]
41 pub fn from_uuid(uuid: uuid::Uuid) -> Self {
42 Self(uuid.to_string())
43 }
44
45 #[must_use]
46 pub fn derived_from_gateway_conversation(user_id: &UserId, gw: &GatewayConversationId) -> Self {
47 let owner =
48 uuid::Uuid::new_v5(&GATEWAY_CONVERSATION_NAMESPACE, user_id.as_str().as_bytes());
49 Self(uuid::Uuid::new_v5(&owner, gw.as_str().as_bytes()).to_string())
50 }
51
52 #[must_use]
53 pub fn derived_from_messaging(platform: &str, org: &str, channel: &str) -> Self {
54 let key = format!("{platform}:{org}:{channel}");
55 Self(uuid::Uuid::new_v5(&MESSAGING_NAMESPACE, key.as_bytes()).to_string())
56 }
57
58 #[must_use]
59 pub fn derived_from_session(session_id: &SessionId) -> Self {
60 Self(uuid::Uuid::new_v5(&SESSION_NAMESPACE, session_id.as_str().as_bytes()).to_string())
61 }
62
63 #[must_use]
67 pub fn derived_from_client_session(session: &ClientSessionId) -> Self {
68 Self(uuid::Uuid::new_v5(&SESSION_NAMESPACE, session.as_str().as_bytes()).to_string())
69 }
70
71
72 #[must_use]
73 pub fn derived_from_cli_probe(server_name: &str) -> Self {
74 Self(uuid::Uuid::new_v5(&CLI_PROBE_NAMESPACE, server_name.as_bytes()).to_string())
75 }
76
77 #[must_use]
78 pub fn derived_from_mcp_validation(service_name: &str) -> Self {
79 Self(uuid::Uuid::new_v5(&MCP_VALIDATION_NAMESPACE, service_name.as_bytes()).to_string())
80 }
81
82 #[must_use]
83 pub fn derived_from_task(task_id: &TaskId) -> Self {
84 Self(uuid::Uuid::new_v5(&TASK_NAMESPACE, task_id.as_str().as_bytes()).to_string())
85 }
86
87 #[must_use]
88 pub fn legacy() -> Self {
89 Self(LEGACY_CONTEXT_UUID.to_owned())
90 }
91}