Skip to main content

conversation_api/execution/
suggestion.rs

1//! Trusted, non-conversation execution for Suggestion generation.
2//!
3//! These values are assigned by Core/Lion admission, never supplied by a model or
4//! decoded from the public generate input. A scheduled service is not an enrolled
5//! Space Client. Payment attribution does not grant business authority.
6
7use serde::{Deserialize, Serialize};
8
9use super::{OperationId, RunId, SpaceContext, UserId};
10
11#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
12#[serde(tag = "type", rename_all = "SCREAMING_SNAKE_CASE", deny_unknown_fields)]
13pub enum SuggestionTrigger {
14    #[serde(deserialize_with = "crate::execution::deserialize_empty_variant")]
15    Scheduled,
16    Explicit {
17        #[serde(rename = "userId")]
18        user_id: UserId,
19    },
20}
21
22/// A reference resolved by the deployment's existing billing adapter. The owner
23/// is not copied into the execution identity or retained as a permanent payer.
24#[derive(Clone, Debug, Eq, PartialEq)]
25pub enum SuggestionPayer {
26    User(UserId),
27    CurrentSpaceOwner(SpaceContext),
28}
29
30#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
31#[serde(rename_all = "camelCase", deny_unknown_fields)]
32pub struct SuggestionInvocationContext {
33    pub space: SpaceContext,
34    pub run_id: RunId,
35    pub operation_id: OperationId,
36    pub trigger: SuggestionTrigger,
37    pub deadline_unix_ms: u64,
38    #[serde(default, skip_serializing_if = "Option::is_none")]
39    pub traceparent: Option<String>,
40}
41
42impl SuggestionInvocationContext {
43    /// Shape validation is not authentication, Space access, or an enabled-job
44    /// check. The deployment must also bind the run to the backend currently
45    /// responsible for this Space (Core or Lion).
46    pub fn validate(&self) -> Result<(), &'static str> {
47        if !valid_id(self.space.space_id.as_str())
48            || !self
49                .space
50                .tenant_id
51                .as_ref()
52                .is_some_and(|id| valid_id(id.as_str()))
53            || !valid_id(self.run_id.as_str())
54            || !valid_id(self.operation_id.as_str())
55            || self.deadline_unix_ms == 0
56            || self.deadline_unix_ms > 9_007_199_254_740_991
57        {
58            return Err("Suggestion execution requires a Space, run, operation and deadline");
59        }
60        if let SuggestionTrigger::Explicit { user_id } = &self.trigger
61            && !valid_id(user_id.as_str())
62        {
63            return Err("Explicit Suggestion generation requires the verified requesting user");
64        }
65        Ok(())
66    }
67
68    pub fn payer(&self) -> Result<SuggestionPayer, &'static str> {
69        self.validate()?;
70        Ok(match &self.trigger {
71            SuggestionTrigger::Explicit { user_id } => SuggestionPayer::User(user_id.clone()),
72            SuggestionTrigger::Scheduled => SuggestionPayer::CurrentSpaceOwner(self.space.clone()),
73        })
74    }
75}
76
77/// Minimal invocation metadata needed by shared model/tool workflows. Concrete
78/// contexts retain their distinct authority and transport contracts.
79pub trait ExecutionContext: Clone + Send + Sync {
80    fn space(&self) -> &SpaceContext;
81    fn run_id(&self) -> &RunId;
82    fn operation_id(&self) -> &OperationId;
83    fn with_operation_id(&self, operation_id: OperationId) -> Self;
84}
85
86impl ExecutionContext for SuggestionInvocationContext {
87    fn space(&self) -> &SpaceContext {
88        &self.space
89    }
90    fn run_id(&self) -> &RunId {
91        &self.run_id
92    }
93    fn operation_id(&self) -> &OperationId {
94        &self.operation_id
95    }
96    fn with_operation_id(&self, operation_id: OperationId) -> Self {
97        Self {
98            operation_id,
99            ..self.clone()
100        }
101    }
102}
103
104impl ExecutionContext for super::InvocationContext {
105    fn space(&self) -> &SpaceContext {
106        &self.space
107    }
108    fn run_id(&self) -> &RunId {
109        &self.run_id
110    }
111    fn operation_id(&self) -> &OperationId {
112        &self.operation_id
113    }
114    fn with_operation_id(&self, operation_id: OperationId) -> Self {
115        Self {
116            operation_id,
117            ..self.clone()
118        }
119    }
120}
121
122fn valid_id(value: &str) -> bool {
123    !value.is_empty()
124        && value.chars().count() <= 256
125        && value.trim() == value
126        && !value.chars().any(char::is_control)
127}