Skip to main content

systemprompt_models/artifacts/text/
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
10fn default_artifact_type() -> String {
11    "text".to_string()
12}
13
14#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
15pub struct TextArtifact {
16    #[serde(rename = "x-artifact-type")]
17    #[serde(default = "default_artifact_type")]
18    pub artifact_type: String,
19    pub content: String,
20    #[serde(skip_serializing_if = "Option::is_none")]
21    pub title: Option<String>,
22    #[serde(skip)]
23    #[schemars(skip)]
24    metadata: ExecutionMetadata,
25}
26
27impl TextArtifact {
28    pub const ARTIFACT_TYPE_STR: &'static str = "text";
29
30    pub fn new(content: impl Into<String>, ctx: &RequestContext) -> Self {
31        Self {
32            artifact_type: "text".to_string(),
33            content: content.into(),
34            title: None,
35            metadata: ExecutionMetadata::with_request(ctx),
36        }
37    }
38
39    pub fn with_title(mut self, title: impl Into<String>) -> Self {
40        self.title = Some(title.into());
41        self
42    }
43
44    pub fn with_execution_id(mut self, id: impl Into<String>) -> Self {
45        self.metadata.execution_id = Some(id.into());
46        self
47    }
48
49    pub fn with_skill(
50        mut self,
51        skill_id: impl Into<SkillId>,
52        skill_name: impl Into<String>,
53    ) -> Self {
54        self.metadata.skill_id = Some(skill_id.into());
55        self.metadata.skill_name = Some(skill_name.into());
56        self
57    }
58}
59
60impl Artifact for TextArtifact {
61    fn artifact_type(&self) -> ArtifactType {
62        ArtifactType::Text
63    }
64
65    fn to_schema(&self) -> JsonValue {
66        json!({
67            "type": "object",
68            "properties": {
69                "content": {
70                    "type": "string",
71                    "description": "Text content"
72                },
73                "title": {
74                    "type": "string",
75                    "description": "Optional title for the text"
76                }
77            },
78            "required": ["content"],
79            "x-artifact-type": "text"
80        })
81    }
82}