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(skip)]
49 pub response_detail: super::ResponseDetail,
50 #[serde(deserialize_with = "deserialize_response")]
52 pub value: Value,
53 #[serde(default, skip_serializing_if = "Option::is_none")]
55 pub context_invalidation: Option<crate::execution::ContextInvalidation>,
56}
57
58#[async_trait]
63pub trait AppFacade: Send + Sync {
64 async fn invoke(
66 &self,
67 context: &InvocationContext,
68 request: FacadeRequest,
69 ) -> Result<FacadeResult, ExternalError>;
70
71 async fn prepare(
73 &self,
74 context: &InvocationContext,
75 request: FacadeRequest,
76 ) -> Result<PreparedAction, ExternalError>;
77
78 async fn commit(
80 &self,
81 context: &InvocationContext,
82 action: PreparedAction,
83 ) -> Result<FacadeResult, ExternalError>;
84
85 async fn reject(
87 &self,
88 context: &InvocationContext,
89 action: PreparedAction,
90 ) -> Result<(), ExternalError>;
91}
92
93fn deserialize_response<'de, D: serde::Deserializer<'de>>(
94 deserializer: D,
95) -> Result<Value, D::Error> {
96 use serde::de::Error;
97 let response = crate::execution::FacadeResponse::deserialize(deserializer)?;
98 serde_json::to_value(response).map_err(D::Error::custom)
99}