Skip to main content

conversation_api/execution/
operational_context.rs

1//! Structured operational-context boundary.
2
3use async_trait::async_trait;
4
5use serde::{Deserialize, Serialize};
6
7use crate::execution::{ExternalError, InvocationContext};
8
9/// Deployment-owned generation vector for independently cacheable context sections.
10#[derive(Clone, Debug, Default, Eq, Hash, PartialEq, Serialize, Deserialize)]
11pub struct ContextGeneration {
12    pub user_space: String,
13    pub identity: String,
14    pub memory: String,
15    pub integration_guide: String,
16    pub installed_integrations: String,
17    pub space_integrations: String,
18}
19
20impl ContextGeneration {
21    /// Creates a vector whose sections share one deployment-owned token.
22    #[must_use]
23    pub fn new(value: impl Into<String>) -> Self {
24        let value = value.into();
25        Self {
26            user_space: value.clone(),
27            identity: value.clone(),
28            memory: value.clone(),
29            integration_guide: value.clone(),
30            installed_integrations: value.clone(),
31            space_integrations: value,
32        }
33    }
34
35    /// Returns whether every independently cacheable section has a non-empty token.
36    #[must_use]
37    pub fn is_complete(&self) -> bool {
38        [
39            &self.user_space,
40            &self.identity,
41            &self.memory,
42            &self.integration_guide,
43            &self.installed_integrations,
44            &self.space_integrations,
45        ]
46        .into_iter()
47        .all(|value| !value.trim().is_empty())
48    }
49}
50
51/// Notification that an App Facade mutation changed facts used by the Agent prompt.
52#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
53pub struct ContextInvalidation {
54    /// New effective generation after the mutation committed.
55    pub generation: ContextGeneration,
56}
57
58/// Runtime facts loaded from the App Facade. This type deliberately contains data, not messages:
59/// prompt text and rendering order are owned by the Agent implementation.
60#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
61pub struct OperationalContext {
62    /// Authoritative Home IANA timezone for model-facing presentation; UTC when Home has none.
63    pub home_timezone: String,
64    pub display_units: std::collections::BTreeMap<String, String>,
65    pub user_space: String,
66    pub identity: String,
67    pub memory: String,
68    pub integration_guide: String,
69    pub installed_integrations: String,
70    pub space_integrations: String,
71}
72
73/// Delta returned for sections whose generation differs from the caller's cached vector.
74#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
75pub struct OperationalContextDelta {
76    /// Refreshed with the user_space generation, from the same scope snapshot.
77    #[serde(default, skip_serializing_if = "Option::is_none")]
78    pub home_timezone: Option<String>,
79    #[serde(default, skip_serializing_if = "Option::is_none")]
80    pub display_units: Option<std::collections::BTreeMap<String, String>>,
81    pub generation: ContextGeneration,
82    #[serde(default, skip_serializing_if = "Option::is_none")]
83    pub user_space: Option<String>,
84    #[serde(default, skip_serializing_if = "Option::is_none")]
85    pub identity: Option<String>,
86    #[serde(default, skip_serializing_if = "Option::is_none")]
87    pub memory: Option<String>,
88    #[serde(default, skip_serializing_if = "Option::is_none")]
89    pub integration_guide: Option<String>,
90    #[serde(default, skip_serializing_if = "Option::is_none")]
91    pub installed_integrations: Option<String>,
92    #[serde(default, skip_serializing_if = "Option::is_none")]
93    pub space_integrations: Option<String>,
94}
95
96/// Loads typed facts used by the canonical Agent prompt.
97/// The default context preserves chat callers; deployments can separately load
98/// authorized Space facts for a non-conversation execution context.
99#[async_trait]
100pub trait ContextSource<C: Send + Sync = InvocationContext>: Send + Sync {
101    async fn load(&self, context: &C) -> Result<OperationalContext, ExternalError>;
102}