Skip to main content

systemprompt_models/artifacts/message/
mod.rs

1//! Message/notice artifact: levelled notice lines a command emits as structured
2//! output so machine consumers never receive an empty response.
3
4use crate::artifacts::traits::Artifact;
5use crate::artifacts::types::ArtifactType;
6use schemars::JsonSchema;
7use serde::{Deserialize, Serialize};
8use serde_json::{Value as JsonValue, json};
9
10fn default_artifact_type() -> String {
11    "message".to_owned()
12}
13
14#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
15pub struct NoticeLine {
16    pub level: String,
17    pub text: String,
18}
19
20impl NoticeLine {
21    pub fn new(level: impl Into<String>, text: impl Into<String>) -> Self {
22        Self {
23            level: level.into(),
24            text: text.into(),
25        }
26    }
27}
28
29#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
30pub struct MessageArtifact {
31    #[serde(rename = "x-artifact-type")]
32    #[serde(default = "default_artifact_type")]
33    pub artifact_type: String,
34    pub messages: Vec<NoticeLine>,
35}
36
37impl MessageArtifact {
38    pub const ARTIFACT_TYPE_STR: &'static str = "message";
39
40    pub fn new(messages: Vec<NoticeLine>) -> Self {
41        Self {
42            artifact_type: "message".to_owned(),
43            messages,
44        }
45    }
46}
47
48impl Artifact for MessageArtifact {
49    fn artifact_type(&self) -> ArtifactType {
50        ArtifactType::Custom("message".to_owned())
51    }
52
53    fn to_schema(&self) -> JsonValue {
54        json!({
55            "type": "object",
56            "properties": {
57                "messages": {
58                    "type": "array",
59                    "description": "Levelled notice lines",
60                    "items": {
61                        "type": "object",
62                        "properties": {
63                            "level": {
64                                "type": "string",
65                                "description": "Notice level: info, success, warning, or error"
66                            },
67                            "text": {
68                                "type": "string",
69                                "description": "Notice text"
70                            }
71                        },
72                        "required": ["level", "text"]
73                    }
74                }
75            },
76            "required": ["messages"],
77            "x-artifact-type": "message"
78        })
79    }
80}