ra2a 0.10.0

A Rust implementation of the Agent2Agent (A2A) Protocol SDK
Documentation
//! Artifact types for the A2A protocol.
//!
//! Maps to proto `Artifact` message.

use serde::{Deserialize, Serialize};

use super::id::ArtifactId;
use super::{Metadata, Part};

/// A file, data structure, or other resource generated by an agent during a task.
///
/// Maps to proto `Artifact`.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct Artifact {
    /// Unique identifier within the task scope.
    pub artifact_id: ArtifactId,
    /// Human-readable name.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// Human-readable description.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Content parts that make up the artifact.
    pub parts: Vec<Part>,
    /// Extension metadata.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub metadata: Option<Metadata>,
    /// URIs of extensions relevant to this artifact.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub extensions: Vec<String>,
}

impl Artifact {
    /// Creates a new artifact with the given ID and parts.
    pub fn new(artifact_id: impl Into<ArtifactId>, parts: Vec<Part>) -> Self {
        Self {
            artifact_id: artifact_id.into(),
            parts,
            name: None,
            description: None,
            metadata: None,
            extensions: Vec::new(),
        }
    }

    /// Creates an artifact with an auto-generated `UUIDv7` ID.
    #[must_use]
    pub fn create(parts: Vec<Part>) -> Self {
        Self::new(ArtifactId::random(), parts)
    }

    /// Shorthand: single-text artifact.
    pub fn text(artifact_id: impl Into<ArtifactId>, text: impl Into<String>) -> Self {
        Self::new(artifact_id, vec![Part::text(text)])
    }

    /// Sets the name.
    #[must_use]
    pub fn with_name(mut self, name: impl Into<String>) -> Self {
        self.name = Some(name.into());
        self
    }

    /// Sets the description.
    #[must_use]
    pub fn with_description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(description.into());
        self
    }
}