feat(phase4): complete zclaw-skills, zclaw-hands, zclaw-channels, zclaw-protocols 模块实现

This commit is contained in:
iven
2026-03-22 08:57:37 +08:00
parent 7abfca9d5c
commit 0ab2f7afda
24 changed files with 2060 additions and 0 deletions

View File

@@ -0,0 +1,156 @@
//! A2A (Agent-to-Agent) protocol support
//!
//! Implements communication between AI agents.
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use zclaw_types::{Result, AgentId};
/// A2A message envelope
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct A2aEnvelope {
/// Message ID
pub id: String,
/// Sender agent ID
pub from: AgentId,
/// Recipient agent ID (or broadcast)
pub to: A2aRecipient,
/// Message type
pub message_type: A2aMessageType,
/// Message payload
pub payload: serde_json::Value,
/// Timestamp
pub timestamp: i64,
/// Conversation/thread ID
pub conversation_id: Option<String>,
/// Reply-to message ID
pub reply_to: Option<String>,
}
/// Recipient specification
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum A2aRecipient {
/// Direct message to specific agent
Direct { agent_id: AgentId },
/// Broadcast to all agents in a group
Group { group_id: String },
/// Broadcast to all agents
Broadcast,
}
/// A2A message types
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum A2aMessageType {
/// Request for information or action
Request,
/// Response to a request
Response,
/// Notification (no response expected)
Notification,
/// Error message
Error,
/// Heartbeat/ping
Heartbeat,
/// Capability advertisement
Capability,
}
/// Agent capability advertisement
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct A2aCapability {
/// Capability name
pub name: String,
/// Capability description
pub description: String,
/// Input schema
pub input_schema: Option<serde_json::Value>,
/// Output schema
pub output_schema: Option<serde_json::Value>,
/// Whether this capability requires approval
pub requires_approval: bool,
}
/// Agent profile for A2A
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct A2aAgentProfile {
/// Agent ID
pub id: AgentId,
/// Agent name
pub name: String,
/// Agent description
pub description: String,
/// Agent capabilities
pub capabilities: Vec<A2aCapability>,
/// Supported protocols
pub protocols: Vec<String>,
/// Agent metadata
pub metadata: HashMap<String, String>,
}
/// A2A client trait
#[async_trait]
pub trait A2aClient: Send + Sync {
/// Send a message to another agent
async fn send(&self, envelope: A2aEnvelope) -> Result<()>;
/// Receive messages (streaming)
async fn receive(&self) -> Result<tokio::sync::mpsc::Receiver<A2aEnvelope>>;
/// Get agent profile
async fn get_profile(&self, agent_id: &AgentId) -> Result<Option<A2aAgentProfile>>;
/// Discover agents with specific capabilities
async fn discover(&self, capability: &str) -> Result<Vec<A2aAgentProfile>>;
/// Advertise own capabilities
async fn advertise(&self, profile: A2aAgentProfile) -> Result<()>;
}
/// Basic A2A client implementation
pub struct BasicA2aClient {
agent_id: AgentId,
profiles: std::sync::Arc<tokio::sync::RwLock<HashMap<AgentId, A2aAgentProfile>>>,
}
impl BasicA2aClient {
pub fn new(agent_id: AgentId) -> Self {
Self {
agent_id,
profiles: std::sync::Arc::new(tokio::sync::RwLock::new(HashMap::new())),
}
}
}
#[async_trait]
impl A2aClient for BasicA2aClient {
async fn send(&self, _envelope: A2aEnvelope) -> Result<()> {
// TODO: Implement actual A2A protocol communication
tracing::info!("A2A send called");
Ok(())
}
async fn receive(&self) -> Result<tokio::sync::mpsc::Receiver<A2aEnvelope>> {
let (_tx, rx) = tokio::sync::mpsc::channel(100);
// TODO: Implement actual A2A protocol communication
Ok(rx)
}
async fn get_profile(&self, agent_id: &AgentId) -> Result<Option<A2aAgentProfile>> {
let profiles = self.profiles.read().await;
Ok(profiles.get(agent_id).cloned())
}
async fn discover(&self, _capability: &str) -> Result<Vec<A2aAgentProfile>> {
let profiles = self.profiles.read().await;
Ok(profiles.values().cloned().collect())
}
async fn advertise(&self, profile: A2aAgentProfile) -> Result<()> {
let mut profiles = self.profiles.write().await;
profiles.insert(profile.id.clone(), profile);
Ok(())
}
}