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