Skip to main content

miyabi_a2a/
types.rs

1//! Type definitions for A2A Protocol
2//!
3//! This module provides core types used throughout the A2A system.
4
5use chrono::{DateTime, Utc};
6use serde::{Deserialize, Serialize};
7
8// Re-export from task module
9pub use crate::task::{TaskStatus, TaskType};
10
11/// Agent card for A2A Protocol
12#[derive(Debug, Clone, Serialize, Deserialize)]
13pub struct AgentCard {
14    /// Name of the resource
15    pub name: String,
16    pub description: Option<String>,
17    /// Version
18    pub version: String,
19    pub capabilities: Vec<String>,
20    /// Auth methods
21    pub auth_methods: Vec<String>,
22    pub url: String,
23}
24
25/// Task representation
26#[derive(Debug, Clone, Serialize, Deserialize)]
27pub struct Task {
28    /// Unique identifier
29    pub id: String,
30    pub status: TaskStatus,
31    /// Input data
32    pub input: TaskInput,
33    pub output: Option<TaskOutput>,
34    /// Context id
35    pub context_id: Option<String>,
36    pub created_at: DateTime<Utc>,
37    /// Last update timestamp
38    pub updated_at: DateTime<Utc>,
39}
40
41impl Task {
42    pub fn new(prompt: String) -> Self {
43        let now = Utc::now();
44        Self {
45            id: uuid::Uuid::new_v4().to_string(),
46            status: TaskStatus::Submitted,
47            input: TaskInput {
48                prompt,
49                params: serde_json::Value::Object(serde_json::Map::new()),
50            },
51            output: None,
52            context_id: None,
53            created_at: now,
54            updated_at: now,
55        }
56    }
57
58    pub fn set_status(&mut self, status: TaskStatus) {
59        self.status = status;
60        self.updated_at = Utc::now();
61    }
62
63    pub fn is_terminal(&self) -> bool {
64        matches!(self.status, TaskStatus::Completed | TaskStatus::Failed | TaskStatus::Cancelled)
65    }
66}
67
68/// Task input
69#[derive(Debug, Clone, Serialize, Deserialize)]
70pub struct TaskInput {
71    /// Prompt text
72    pub prompt: String,
73    pub params: serde_json::Value,
74}
75
76/// Task output
77#[derive(Debug, Clone, Serialize, Deserialize)]
78pub struct TaskOutput {
79    /// Result data
80    pub result: serde_json::Value,
81    pub error: Option<String>,
82}
83
84/// Message role
85#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
86#[serde(rename_all = "lowercase")]
87pub enum Role {
88    User,
89    Agent,
90    System,
91}
92
93/// Message representation
94#[derive(Debug, Clone, Serialize, Deserialize)]
95pub struct Message {
96    /// Role type
97    pub role: Role,
98    pub parts: Vec<Part>,
99}
100
101/// Message part
102#[derive(Debug, Clone, Serialize, Deserialize)]
103#[serde(tag = "type")]
104pub enum Part {
105    #[serde(rename = "text")]
106    Text { content: String },
107    #[serde(rename = "image")]
108    Image { url: String },
109    #[serde(rename = "data")]
110    Data { content: Vec<u8>, mime_type: String },
111}
112
113impl Part {
114    pub fn text(content: impl Into<String>) -> Self {
115        Part::Text {
116            content: content.into(),
117        }
118    }
119}
120
121/// Artifact type
122#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
123#[serde(rename_all = "lowercase")]
124pub enum ArtifactType {
125    Code,
126    Document,
127    Image,
128    Data,
129}
130
131/// Artifact representation
132#[derive(Debug, Clone, Serialize, Deserialize)]
133pub struct Artifact {
134    /// Unique identifier
135    pub id: String,
136    pub artifact_type: ArtifactType,
137    /// Message content
138    pub content: String,
139    pub metadata: Option<serde_json::Value>,
140}