conversation_api/execution/
facade.rs1use async_trait::async_trait;
4use serde::{Deserialize, Serialize};
5use serde_json::Value;
6
7use crate::execution::{ActionId, ExternalError, InvocationContext, OperationId};
8
9#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
11#[serde(deny_unknown_fields)]
12pub struct FacadeRequest {
13 pub operation_id: OperationId,
15 pub target: String,
17 pub input: Value,
19 pub response_detail: crate::execution::ResponseDetail,
21}
22
23pub use crate::{InteractionCard, InteractionKind, InteractionTone};
24
25#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
27#[serde(deny_unknown_fields)]
28pub struct PreparedAction {
29 pub action_id: ActionId,
31 pub operation_id: OperationId,
33 pub card: InteractionCard,
35 #[serde(default)]
37 pub payload: Value,
38 pub revision: u64,
40}
41
42#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
44#[serde(deny_unknown_fields)]
45pub struct FacadeResult {
46 #[serde(deserialize_with = "deserialize_response")]
48 pub value: Value,
49 #[serde(default, skip_serializing_if = "Option::is_none")]
51 pub context_invalidation: Option<crate::execution::ContextInvalidation>,
52}
53
54#[async_trait]
59pub trait AppFacade: Send + Sync {
60 async fn invoke(
62 &self,
63 context: &InvocationContext,
64 request: FacadeRequest,
65 ) -> Result<FacadeResult, ExternalError>;
66
67 async fn prepare(
69 &self,
70 context: &InvocationContext,
71 request: FacadeRequest,
72 ) -> Result<PreparedAction, ExternalError>;
73
74 async fn commit(
76 &self,
77 context: &InvocationContext,
78 action: PreparedAction,
79 ) -> Result<FacadeResult, ExternalError>;
80
81 async fn reject(
83 &self,
84 context: &InvocationContext,
85 action: PreparedAction,
86 ) -> Result<(), ExternalError>;
87}
88
89fn deserialize_response<'de, D: serde::Deserializer<'de>>(
90 deserializer: D,
91) -> Result<Value, D::Error> {
92 use serde::de::Error;
93 let response = crate::execution::FacadeResponse::deserialize(deserializer)?;
94 serde_json::to_value(response).map_err(D::Error::custom)
95}