systemprompt_models/artifacts/table/
mod.rs

1pub mod column;
2pub mod hints;
3
4pub use column::Column;
5pub use hints::TableHints;
6
7use crate::artifacts::metadata::ExecutionMetadata;
8use crate::artifacts::traits::Artifact;
9use crate::artifacts::types::ArtifactType;
10use crate::execution::context::RequestContext;
11use schemars::JsonSchema;
12use serde::{Deserialize, Serialize};
13use serde_json::{json, Value as JsonValue};
14use systemprompt_identifiers::SkillId;
15
16#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
17pub struct TableResponse {
18    #[serde(rename = "x-artifact-type")]
19    pub artifact_type: String,
20    pub columns: Vec<Column>,
21    pub items: Vec<JsonValue>,
22    pub count: usize,
23    #[serde(skip_serializing_if = "Option::is_none")]
24    pub execution_id: Option<String>,
25    #[serde(skip_serializing_if = "Option::is_none")]
26    #[schemars(with = "Option<JsonValue>")]
27    pub hints: Option<JsonValue>,
28}
29
30#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
31pub struct TableArtifact {
32    #[serde(rename = "x-artifact-type")]
33    #[serde(default = "default_artifact_type")]
34    pub artifact_type: String,
35    pub columns: Vec<Column>,
36    pub items: Vec<JsonValue>,
37    #[serde(skip_serializing_if = "Option::is_none")]
38    #[schemars(with = "Option<JsonValue>")]
39    pub hints: Option<JsonValue>,
40    #[serde(skip)]
41    #[schemars(skip)]
42    hints_builder: TableHints,
43    #[serde(skip)]
44    #[schemars(skip)]
45    metadata: ExecutionMetadata,
46}
47
48fn default_artifact_type() -> String {
49    "table".to_string()
50}
51
52impl TableArtifact {
53    pub fn new(columns: Vec<Column>, ctx: &RequestContext) -> Self {
54        Self {
55            artifact_type: "table".to_string(),
56            columns,
57            items: Vec::new(),
58            hints: None,
59            hints_builder: TableHints::default(),
60            metadata: ExecutionMetadata::with_request(ctx),
61        }
62    }
63
64    pub fn with_rows(mut self, items: Vec<JsonValue>) -> Self {
65        self.items = items;
66        self
67    }
68
69    pub fn with_hints(mut self, hints: TableHints) -> Self {
70        use crate::artifacts::traits::ArtifactSchema;
71        self.hints = Some(hints.generate_schema());
72        self.hints_builder = hints;
73        self
74    }
75
76    pub fn with_metadata(mut self, metadata: ExecutionMetadata) -> Self {
77        self.metadata = metadata;
78        self
79    }
80
81    pub fn with_execution_id(mut self, id: impl Into<String>) -> Self {
82        self.metadata.execution_id = Some(id.into());
83        self
84    }
85
86    pub fn with_skill(
87        mut self,
88        skill_id: impl Into<SkillId>,
89        skill_name: impl Into<String>,
90    ) -> Self {
91        self.metadata.skill_id = Some(skill_id.into());
92        self.metadata.skill_name = Some(skill_name.into());
93        self
94    }
95
96    pub fn to_response(&self) -> JsonValue {
97        use crate::artifacts::traits::ArtifactSchema;
98
99        let response = TableResponse {
100            artifact_type: "table".to_string(),
101            columns: self.columns.clone(),
102            items: self.items.clone(),
103            count: self.items.len(),
104            execution_id: self.metadata.execution_id.clone(),
105            hints: Some(self.hints_builder.generate_schema()),
106        };
107        serde_json::to_value(response).unwrap_or(JsonValue::Null)
108    }
109}
110
111impl Artifact for TableArtifact {
112    fn artifact_type(&self) -> ArtifactType {
113        ArtifactType::Table
114    }
115
116    fn to_schema(&self) -> JsonValue {
117        use crate::artifacts::traits::ArtifactSchema;
118
119        json!({
120            "type": "object",
121            "properties": {
122                "columns": {
123                    "type": "array",
124                    "description": "Column definitions"
125                },
126                "items": {
127                    "type": "array",
128                    "description": "Array of data records"
129                },
130                "count": {
131                    "type": "integer",
132                    "description": "Total number of records"
133                },
134                "_execution_id": {
135                    "type": "string",
136                    "description": "Execution ID for tracking"
137                }
138            },
139            "required": ["columns", "items"],
140            "x-artifact-type": "table",
141            "x-table-hints": self.hints_builder.generate_schema()
142        })
143    }
144}