conversation_api/execution/
suggestion.rs1use 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 Adoption { principal: crate::ClientPrincipal },
18 Explicit {
19 #[serde(rename = "userId")]
20 user_id: UserId,
21 },
22}
23
24#[derive(Clone, Debug, Eq, PartialEq)]
27pub enum SuggestionPayer {
28 User(UserId),
29 CurrentSpaceOwner(SpaceContext),
30}
31
32#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
33#[serde(rename_all = "camelCase", deny_unknown_fields)]
34pub struct SuggestionInvocationContext {
35 pub space: SpaceContext,
36 pub run_id: RunId,
37 pub operation_id: OperationId,
38 pub trigger: SuggestionTrigger,
39 pub deadline_unix_ms: u64,
40 #[serde(default, skip_serializing_if = "Option::is_none")]
41 pub traceparent: Option<String>,
42}
43
44impl SuggestionInvocationContext {
45 pub fn validate(&self) -> Result<(), &'static str> {
49 if !valid_id(self.space.space_id.as_str())
50 || !self
51 .space
52 .tenant_id
53 .as_ref()
54 .is_some_and(|id| valid_id(id.as_str()))
55 || !valid_id(self.run_id.as_str())
56 || !valid_id(self.operation_id.as_str())
57 || self.deadline_unix_ms == 0
58 || self.deadline_unix_ms > 9_007_199_254_740_991
59 {
60 return Err("Suggestion execution requires a Space, run, operation and deadline");
61 }
62 if let SuggestionTrigger::Explicit { user_id } = &self.trigger
63 && !valid_id(user_id.as_str())
64 {
65 return Err("Explicit Suggestion generation requires the verified requesting user");
66 }
67 if let SuggestionTrigger::Adoption { principal } = &self.trigger {
68 match principal {
69 crate::ClientPrincipal::User { user_id } if valid_id(user_id) => (),
70 crate::ClientPrincipal::Space {
71 tenant_id,
72 space_id,
73 } if self.space.tenant_id.as_ref().map(|id| id.as_str())
74 == Some(tenant_id.as_str())
75 && self.space.space_id.as_str() == space_id => {}
76 _ => {
77 return Err(
78 "Adoption requires the verified requesting principal in this Space",
79 );
80 }
81 }
82 }
83 Ok(())
84 }
85
86 pub fn payer(&self) -> Result<SuggestionPayer, &'static str> {
87 self.validate()?;
88 Ok(match &self.trigger {
89 SuggestionTrigger::Explicit { user_id } => SuggestionPayer::User(user_id.clone()),
90 SuggestionTrigger::Adoption {
91 principal: crate::ClientPrincipal::User { user_id },
92 } => SuggestionPayer::User(user_id.clone().into()),
93 SuggestionTrigger::Adoption {
94 principal: crate::ClientPrincipal::Space { .. },
95 } => SuggestionPayer::CurrentSpaceOwner(self.space.clone()),
96 SuggestionTrigger::Scheduled => SuggestionPayer::CurrentSpaceOwner(self.space.clone()),
97 })
98 }
99}
100
101pub trait ExecutionContext: Clone + Send + Sync {
104 fn space(&self) -> &SpaceContext;
105 fn run_id(&self) -> &RunId;
106 fn operation_id(&self) -> &OperationId;
107 fn with_operation_id(&self, operation_id: OperationId) -> Self;
108}
109
110impl ExecutionContext for SuggestionInvocationContext {
111 fn space(&self) -> &SpaceContext {
112 &self.space
113 }
114 fn run_id(&self) -> &RunId {
115 &self.run_id
116 }
117 fn operation_id(&self) -> &OperationId {
118 &self.operation_id
119 }
120 fn with_operation_id(&self, operation_id: OperationId) -> Self {
121 Self {
122 operation_id,
123 ..self.clone()
124 }
125 }
126}
127
128impl ExecutionContext for super::InvocationContext {
129 fn space(&self) -> &SpaceContext {
130 &self.space
131 }
132 fn run_id(&self) -> &RunId {
133 &self.run_id
134 }
135 fn operation_id(&self) -> &OperationId {
136 &self.operation_id
137 }
138 fn with_operation_id(&self, operation_id: OperationId) -> Self {
139 Self {
140 operation_id,
141 ..self.clone()
142 }
143 }
144}
145
146fn valid_id(value: &str) -> bool {
147 !value.is_empty()
148 && value.chars().count() <= 256
149 && value.trim() == value
150 && !value.chars().any(char::is_control)
151}