Skip to main content

systemprompt_models/artifacts/card/
mod.rs

1use crate::artifacts::metadata::ExecutionMetadata;
2use crate::artifacts::traits::Artifact;
3use crate::artifacts::types::ArtifactType;
4use crate::execution::context::RequestContext;
5use schemars::JsonSchema;
6use serde::{Deserialize, Serialize};
7use serde_json::{json, Value as JsonValue};
8use systemprompt_identifiers::SkillId;
9
10#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, Default)]
11pub struct PresentationCardResponse {
12    #[serde(rename = "x-artifact-type")]
13    pub artifact_type: String,
14    pub title: String,
15    #[serde(skip_serializing_if = "Option::is_none")]
16    pub subtitle: Option<String>,
17    pub sections: Vec<CardSection>,
18    #[serde(skip_serializing_if = "Vec::is_empty", default)]
19    pub ctas: Vec<CardCta>,
20    pub theme: String,
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub execution_id: Option<String>,
23    #[serde(skip_serializing_if = "Option::is_none")]
24    pub skill_id: Option<String>,
25    #[serde(skip_serializing_if = "Option::is_none")]
26    pub skill_name: Option<String>,
27}
28
29#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
30pub struct CardSection {
31    pub heading: String,
32    pub content: String,
33    #[serde(skip_serializing_if = "Option::is_none")]
34    pub icon: Option<String>,
35}
36
37impl CardSection {
38    pub fn new(heading: impl Into<String>, content: impl Into<String>) -> Self {
39        Self {
40            heading: heading.into(),
41            content: content.into(),
42            icon: None,
43        }
44    }
45
46    pub fn with_icon(mut self, icon: impl Into<String>) -> Self {
47        self.icon = Some(icon.into());
48        self
49    }
50}
51
52#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
53pub struct CardCta {
54    pub id: String,
55    pub label: String,
56    pub message: String,
57    pub variant: String,
58    #[serde(skip_serializing_if = "Option::is_none")]
59    pub icon: Option<String>,
60}
61
62impl CardCta {
63    pub fn new(
64        id: impl Into<String>,
65        label: impl Into<String>,
66        message: impl Into<String>,
67        variant: impl Into<String>,
68    ) -> Self {
69        Self {
70            id: id.into(),
71            label: label.into(),
72            message: message.into(),
73            variant: variant.into(),
74            icon: None,
75        }
76    }
77
78    pub fn with_icon(mut self, icon: impl Into<String>) -> Self {
79        self.icon = Some(icon.into());
80        self
81    }
82}
83
84#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
85pub struct PresentationCardArtifact {
86    #[serde(rename = "x-artifact-type")]
87    #[serde(default = "default_card_artifact_type")]
88    pub artifact_type: String,
89    pub title: String,
90    #[serde(skip_serializing_if = "Option::is_none")]
91    pub subtitle: Option<String>,
92    pub sections: Vec<CardSection>,
93    #[serde(default, skip_serializing_if = "Vec::is_empty")]
94    pub ctas: Vec<CardCta>,
95    #[serde(default = "default_theme")]
96    pub theme: String,
97    #[serde(skip_serializing_if = "Option::is_none")]
98    pub execution_id: Option<String>,
99    #[serde(skip_serializing_if = "Option::is_none")]
100    pub skill_id: Option<String>,
101    #[serde(skip_serializing_if = "Option::is_none")]
102    pub skill_name: Option<String>,
103    #[serde(skip)]
104    #[schemars(skip)]
105    metadata: ExecutionMetadata,
106}
107
108fn default_theme() -> String {
109    "gradient".to_string()
110}
111
112fn default_card_artifact_type() -> String {
113    "presentation_card".to_string()
114}
115
116impl PresentationCardArtifact {
117    pub fn new(title: impl Into<String>, ctx: &RequestContext) -> Self {
118        Self {
119            artifact_type: "presentation_card".to_string(),
120            title: title.into(),
121            subtitle: None,
122            sections: Vec::new(),
123            ctas: Vec::new(),
124            theme: default_theme(),
125            execution_id: None,
126            skill_id: None,
127            skill_name: None,
128            metadata: ExecutionMetadata::with_request(ctx),
129        }
130    }
131
132    pub fn with_subtitle(mut self, subtitle: impl Into<String>) -> Self {
133        self.subtitle = Some(subtitle.into());
134        self
135    }
136
137    pub fn with_sections(mut self, sections: Vec<CardSection>) -> Self {
138        self.sections = sections;
139        self
140    }
141
142    pub fn add_section(mut self, section: CardSection) -> Self {
143        self.sections.push(section);
144        self
145    }
146
147    pub fn with_ctas(mut self, ctas: Vec<CardCta>) -> Self {
148        self.ctas = ctas;
149        self
150    }
151
152    pub fn add_cta(mut self, cta: CardCta) -> Self {
153        self.ctas.push(cta);
154        self
155    }
156
157    pub fn with_theme(mut self, theme: impl Into<String>) -> Self {
158        self.theme = theme.into();
159        self
160    }
161
162    pub fn with_execution_id(mut self, id: impl Into<String>) -> Self {
163        let id_str = id.into();
164        self.execution_id = Some(id_str.clone());
165        self.metadata.execution_id = Some(id_str);
166        self
167    }
168
169    pub fn with_skill(
170        mut self,
171        skill_id: impl Into<SkillId>,
172        skill_name: impl Into<String>,
173    ) -> Self {
174        let id = skill_id.into();
175        self.skill_id = Some(id.as_str().to_string());
176        self.skill_name = Some(skill_name.into());
177        self.metadata.skill_id = Some(id);
178        self
179    }
180}
181
182impl Artifact for PresentationCardArtifact {
183    fn artifact_type(&self) -> ArtifactType {
184        ArtifactType::PresentationCard
185    }
186
187    fn to_schema(&self) -> JsonValue {
188        json!({
189            "type": "object",
190            "properties": {
191                "title": {
192                    "type": "string",
193                    "description": "Card title"
194                },
195                "subtitle": {
196                    "type": "string",
197                    "description": "Card subtitle"
198                },
199                "sections": {
200                    "type": "array",
201                    "description": "Content sections",
202                    "items": {
203                        "type": "object",
204                        "properties": {
205                            "heading": {"type": "string"},
206                            "content": {"type": "string"},
207                            "icon": {"type": "string"}
208                        },
209                        "required": ["heading", "content"]
210                    }
211                },
212                "ctas": {
213                    "type": "array",
214                    "description": "Call-to-action buttons",
215                    "items": {
216                        "type": "object",
217                        "properties": {
218                            "id": {"type": "string"},
219                            "label": {"type": "string"},
220                            "message": {"type": "string"},
221                            "variant": {"type": "string"},
222                            "icon": {"type": "string"}
223                        },
224                        "required": ["id", "label", "message", "variant"]
225                    }
226                },
227                "theme": {
228                    "type": "string",
229                    "description": "Card theme",
230                    "default": "gradient"
231                },
232                "_execution_id": {
233                    "type": "string",
234                    "description": "Execution ID for tracking"
235                }
236            },
237            "required": ["title", "sections"],
238            "x-artifact-type": "presentation_card",
239            "x-presentation-hints": {
240                "theme": self.theme
241            }
242        })
243    }
244}