Skip to main content

systemprompt_models/artifacts/table/
mod.rs

1//! Tabular artifact: columnar data records returned by skills and tools.
2//!
3//! [`TableArtifact`] is the builder-style producer; [`TableResponse`] is the
4//! serialized wire shape. Column definitions live in [`mod@column`] and display
5//! hints in [`hints`].
6//!
7//! Copyright (c) systemprompt.io — Business Source License 1.1.
8//! See <https://systemprompt.io> for licensing details.
9
10pub mod column;
11pub mod hints;
12
13pub use column::Column;
14pub use hints::TableHints;
15
16use crate::artifacts::metadata::{ArtifactProvenance, ExecutionMetadata};
17use crate::artifacts::traits::Artifact;
18use crate::artifacts::types::ArtifactType;
19use crate::execution::context::RequestContext;
20use schemars::JsonSchema;
21use serde::{Deserialize, Serialize};
22use serde_json::{Value as JsonValue, json};
23use systemprompt_identifiers::{McpExecutionId, SkillId, SkillName};
24
25#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
26pub struct TableResponse {
27    #[serde(rename = "x-artifact-type")]
28    pub artifact_type: String,
29    #[serde(skip_serializing_if = "Option::is_none")]
30    pub title: Option<String>,
31    pub columns: Vec<Column>,
32    // JSON: Table rows are the tool's own row objects.
33    pub items: Vec<JsonValue>,
34    pub count: usize,
35    #[serde(skip_serializing_if = "Option::is_none")]
36    pub execution_id: Option<McpExecutionId>,
37    #[serde(skip_serializing_if = "Option::is_none")]
38    #[schemars(with = "Option<JsonValue>")]
39    // JSON: Free-form `_meta.hints` object the tool emitted; see `hints.rs`.
40    pub hints: Option<JsonValue>,
41}
42
43#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
44pub struct TableArtifact {
45    #[serde(rename = "x-artifact-type")]
46    #[serde(default = "default_artifact_type")]
47    pub artifact_type: String,
48    #[serde(default, skip_serializing_if = "Option::is_none")]
49    pub title: Option<String>,
50    pub columns: Vec<Column>,
51    // JSON: Table rows are the tool's own row objects.
52    pub items: Vec<JsonValue>,
53    #[serde(skip_serializing_if = "Option::is_none")]
54    #[schemars(with = "Option<JsonValue>")]
55    // JSON: Free-form `_meta.hints` object the tool emitted; see `hints.rs`.
56    pub hints: Option<JsonValue>,
57    #[serde(skip)]
58    #[schemars(skip)]
59    hints_builder: TableHints,
60    #[serde(skip)]
61    #[schemars(skip)]
62    metadata: ArtifactProvenance,
63}
64
65fn default_artifact_type() -> String {
66    "table".to_owned()
67}
68
69impl TableArtifact {
70    pub const ARTIFACT_TYPE_STR: &'static str = "table";
71
72    pub fn new(columns: Vec<Column>) -> Self {
73        Self {
74            artifact_type: "table".to_owned(),
75            title: None,
76            columns,
77            items: Vec::new(),
78            hints: None,
79            hints_builder: TableHints::default(),
80            metadata: ArtifactProvenance::default(),
81        }
82    }
83
84    pub fn with_request(mut self, ctx: &RequestContext) -> Self {
85        self.metadata.set_request(ctx);
86        self
87    }
88
89    #[must_use]
90    pub fn with_title(mut self, title: impl Into<String>) -> Self {
91        self.title = Some(title.into());
92        self
93    }
94
95    // JSON: Table rows are the tool's own row objects.
96    pub fn with_rows(mut self, items: Vec<JsonValue>) -> Self {
97        self.items = items;
98        self
99    }
100
101    pub fn with_hints(mut self, hints: TableHints) -> Self {
102        use crate::artifacts::traits::ArtifactSchema;
103        self.hints = Some(hints.generate_schema());
104        self.hints_builder = hints;
105        self
106    }
107
108    pub fn with_metadata(mut self, metadata: ExecutionMetadata) -> Self {
109        self.metadata.set_metadata(metadata);
110        self
111    }
112
113    pub fn with_execution_id(mut self, id: McpExecutionId) -> Self {
114        self.metadata.set_execution_id(id);
115        self
116    }
117
118    pub fn with_skill(mut self, skill_id: SkillId, skill_name: SkillName) -> Self {
119        self.metadata.set_skill(skill_id, skill_name);
120        self
121    }
122
123    // JSON: A2A `DataPart.data` payload.
124    pub fn to_response(&self) -> JsonValue {
125        use crate::artifacts::traits::ArtifactSchema;
126
127        let response = TableResponse {
128            artifact_type: "table".to_owned(),
129            title: self.title.clone(),
130            columns: self.columns.clone(),
131            items: self.items.clone(),
132            count: self.items.len(),
133            execution_id: self.metadata.execution_id().cloned(),
134            hints: Some(self.hints_builder.generate_schema()),
135        };
136        match serde_json::to_value(response) {
137            Ok(v) => v,
138            Err(e) => {
139                tracing::error!(error = %e, "Failed to serialize table response");
140                JsonValue::Null
141            },
142        }
143    }
144}
145
146impl Artifact for TableArtifact {
147    fn artifact_type(&self) -> ArtifactType {
148        ArtifactType::Table
149    }
150
151    // JSON: JSON Schema document describing the artifact for the model.
152    fn to_schema(&self) -> JsonValue {
153        use crate::artifacts::traits::ArtifactSchema;
154
155        json!({
156            "type": "object",
157            "properties": {
158                "columns": {
159                    "type": "array",
160                    "description": "Column definitions"
161                },
162                "items": {
163                    "type": "array",
164                    "description": "Array of data records"
165                },
166                "count": {
167                    "type": "integer",
168                    "description": "Total number of records"
169                },
170                "_execution_id": {
171                    "type": "string",
172                    "description": "Execution ID for tracking"
173                }
174            },
175            "required": ["columns", "items"],
176            "x-artifact-type": "table",
177            "x-table-hints": self.hints_builder.generate_schema()
178        })
179    }
180}