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    // JSON: JSON Schema document describing the artifact for the model.
57    fn to_schema(&self) -> JsonValue {
58        json!({
59            "type": "object",
60            "properties": {
61                "messages": {
62                    "type": "array",
63                    "description": "Levelled notice lines",
64                    "items": {
65                        "type": "object",
66                        "properties": {
67                            "level": {
68                                "type": "string",
69                                "description": "Notice level: info, success, warning, or error"
70                            },
71                            "text": {
72                                "type": "string",
73                                "description": "Notice text"
74                            }
75                        },
76                        "required": ["level", "text"]
77                    }
78                }
79            },
80            "required": ["messages"],
81            "x-artifact-type": "message"
82        })
83    }
84}