refactor(hands): 移除空壳 Hand — Whiteboard/Slideshow/Speech (Phase 5)
Some checks failed
CI / Lint & TypeCheck (push) Has been cancelled
CI / Unit Tests (push) Has been cancelled
CI / Build Frontend (push) Has been cancelled
CI / Rust Check (push) Has been cancelled
CI / Security Scan (push) Has been cancelled
CI / E2E Tests (push) Has been cancelled
Some checks failed
CI / Lint & TypeCheck (push) Has been cancelled
CI / Unit Tests (push) Has been cancelled
CI / Build Frontend (push) Has been cancelled
CI / Rust Check (push) Has been cancelled
CI / Security Scan (push) Has been cancelled
CI / E2E Tests (push) Has been cancelled
删除 3 个仅含 UI 占位的 Hand,清理 Rust 实现与前端引用: - Rust: whiteboard.rs(422行) + slideshow.rs(797行) + speech.rs(442行) - 前端: WhiteboardCanvas + SlideshowRenderer + speech-synth + 相关类型/常量 - 配置: 3 个 HAND.toml - 净减 ~5400 行,Hands 9→6(启用) + Quiz/Browser/Researcher/Collector/Clip/Twitter/Reminder
This commit is contained in:
@@ -1,9 +1,6 @@
|
||||
//! Educational Hands - Teaching and presentation capabilities
|
||||
//!
|
||||
//! This module provides hands for interactive classroom experiences:
|
||||
//! - Whiteboard: Drawing and annotation
|
||||
//! - Slideshow: Presentation control
|
||||
//! - Speech: Text-to-speech synthesis
|
||||
//! This module provides hands for interactive experiences:
|
||||
//! - Quiz: Assessment and evaluation
|
||||
//! - Browser: Web automation
|
||||
//! - Researcher: Deep research and analysis
|
||||
@@ -11,9 +8,6 @@
|
||||
//! - Clip: Video processing
|
||||
//! - Twitter: Social media automation
|
||||
|
||||
mod whiteboard;
|
||||
mod slideshow;
|
||||
mod speech;
|
||||
pub mod quiz;
|
||||
mod browser;
|
||||
mod researcher;
|
||||
@@ -22,9 +16,6 @@ mod clip;
|
||||
mod twitter;
|
||||
pub mod reminder;
|
||||
|
||||
pub use whiteboard::*;
|
||||
pub use slideshow::*;
|
||||
pub use speech::*;
|
||||
pub use quiz::*;
|
||||
pub use browser::*;
|
||||
pub use researcher::*;
|
||||
|
||||
@@ -1,797 +0,0 @@
|
||||
//! Slideshow Hand - Presentation control capabilities
|
||||
//!
|
||||
//! Provides slideshow control for teaching:
|
||||
//! - next_slide/prev_slide: Navigation
|
||||
//! - goto_slide: Jump to specific slide
|
||||
//! - spotlight: Highlight elements
|
||||
//! - laser: Show laser pointer
|
||||
//! - highlight: Highlight areas
|
||||
//! - play_animation: Trigger animations
|
||||
|
||||
use async_trait::async_trait;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::RwLock;
|
||||
use zclaw_types::Result;
|
||||
|
||||
use crate::{Hand, HandConfig, HandContext, HandResult, HandStatus};
|
||||
|
||||
/// Slideshow action types
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(tag = "action", rename_all = "snake_case")]
|
||||
pub enum SlideshowAction {
|
||||
/// Go to next slide
|
||||
NextSlide,
|
||||
/// Go to previous slide
|
||||
PrevSlide,
|
||||
/// Go to specific slide
|
||||
GotoSlide {
|
||||
slide_number: usize,
|
||||
},
|
||||
/// Spotlight/highlight an element
|
||||
Spotlight {
|
||||
element_id: String,
|
||||
#[serde(default = "default_spotlight_duration")]
|
||||
duration_ms: u64,
|
||||
},
|
||||
/// Show laser pointer at position
|
||||
Laser {
|
||||
x: f64,
|
||||
y: f64,
|
||||
#[serde(default = "default_laser_duration")]
|
||||
duration_ms: u64,
|
||||
},
|
||||
/// Highlight a rectangular area
|
||||
Highlight {
|
||||
x: f64,
|
||||
y: f64,
|
||||
width: f64,
|
||||
height: f64,
|
||||
#[serde(default)]
|
||||
color: Option<String>,
|
||||
#[serde(default = "default_highlight_duration")]
|
||||
duration_ms: u64,
|
||||
},
|
||||
/// Play animation
|
||||
PlayAnimation {
|
||||
animation_id: String,
|
||||
},
|
||||
/// Pause auto-play
|
||||
Pause,
|
||||
/// Resume auto-play
|
||||
Resume,
|
||||
/// Start auto-play
|
||||
AutoPlay {
|
||||
#[serde(default = "default_interval")]
|
||||
interval_ms: u64,
|
||||
},
|
||||
/// Stop auto-play
|
||||
StopAutoPlay,
|
||||
/// Get current state
|
||||
GetState,
|
||||
/// Set slide content (for dynamic slides)
|
||||
SetContent {
|
||||
slide_number: usize,
|
||||
content: SlideContent,
|
||||
},
|
||||
}
|
||||
|
||||
fn default_spotlight_duration() -> u64 { 2000 }
|
||||
fn default_laser_duration() -> u64 { 3000 }
|
||||
fn default_highlight_duration() -> u64 { 2000 }
|
||||
fn default_interval() -> u64 { 5000 }
|
||||
|
||||
/// Slide content structure
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct SlideContent {
|
||||
pub title: String,
|
||||
#[serde(default)]
|
||||
pub subtitle: Option<String>,
|
||||
#[serde(default)]
|
||||
pub content: Vec<ContentBlock>,
|
||||
#[serde(default)]
|
||||
pub notes: Option<String>,
|
||||
#[serde(default)]
|
||||
pub background: Option<String>,
|
||||
}
|
||||
|
||||
/// Presentation/slideshow rendering content block. Domain-specific for slide content.
|
||||
/// Distinct from zclaw_types::ContentBlock (LLM messages) and zclaw_protocols::ContentBlock (MCP).
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(tag = "type", rename_all = "snake_case")]
|
||||
pub enum ContentBlock {
|
||||
Text { text: String, style: Option<TextStyle> },
|
||||
Image { url: String, alt: Option<String> },
|
||||
List { items: Vec<String>, ordered: bool },
|
||||
Code { code: String, language: Option<String> },
|
||||
Math { latex: String },
|
||||
Table { headers: Vec<String>, rows: Vec<Vec<String>> },
|
||||
Chart { chart_type: String, data: serde_json::Value },
|
||||
}
|
||||
|
||||
/// Text style options
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
||||
pub struct TextStyle {
|
||||
#[serde(default)]
|
||||
pub bold: bool,
|
||||
#[serde(default)]
|
||||
pub italic: bool,
|
||||
#[serde(default)]
|
||||
pub size: Option<u32>,
|
||||
#[serde(default)]
|
||||
pub color: Option<String>,
|
||||
}
|
||||
|
||||
/// Slideshow state
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct SlideshowState {
|
||||
pub current_slide: usize,
|
||||
pub total_slides: usize,
|
||||
pub is_playing: bool,
|
||||
pub auto_play_interval_ms: u64,
|
||||
pub slides: Vec<SlideContent>,
|
||||
}
|
||||
|
||||
impl Default for SlideshowState {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
current_slide: 0,
|
||||
total_slides: 0,
|
||||
is_playing: false,
|
||||
auto_play_interval_ms: 5000,
|
||||
slides: Vec::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Slideshow Hand implementation
|
||||
pub struct SlideshowHand {
|
||||
config: HandConfig,
|
||||
state: Arc<RwLock<SlideshowState>>,
|
||||
}
|
||||
|
||||
impl SlideshowHand {
|
||||
/// Create a new slideshow hand
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
config: HandConfig {
|
||||
id: "slideshow".to_string(),
|
||||
name: "幻灯片".to_string(),
|
||||
description: "控制演示文稿的播放、导航和标注".to_string(),
|
||||
needs_approval: false,
|
||||
dependencies: vec![],
|
||||
input_schema: Some(serde_json::json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"action": { "type": "string" },
|
||||
"slide_number": { "type": "integer" },
|
||||
"element_id": { "type": "string" },
|
||||
}
|
||||
})),
|
||||
tags: vec!["presentation".to_string(), "education".to_string()],
|
||||
enabled: true,
|
||||
max_concurrent: 0,
|
||||
timeout_secs: 0,
|
||||
},
|
||||
state: Arc::new(RwLock::new(SlideshowState::default())),
|
||||
}
|
||||
}
|
||||
|
||||
/// Create with slides (async version)
|
||||
pub async fn with_slides_async(slides: Vec<SlideContent>) -> Self {
|
||||
let hand = Self::new();
|
||||
let mut state = hand.state.write().await;
|
||||
state.total_slides = slides.len();
|
||||
state.slides = slides;
|
||||
drop(state);
|
||||
hand
|
||||
}
|
||||
|
||||
/// Execute a slideshow action
|
||||
pub async fn execute_action(&self, action: SlideshowAction) -> Result<HandResult> {
|
||||
let mut state = self.state.write().await;
|
||||
|
||||
match action {
|
||||
SlideshowAction::NextSlide => {
|
||||
if state.current_slide < state.total_slides.saturating_sub(1) {
|
||||
state.current_slide += 1;
|
||||
}
|
||||
Ok(HandResult::success(serde_json::json!({
|
||||
"status": "next",
|
||||
"current_slide": state.current_slide,
|
||||
"total_slides": state.total_slides,
|
||||
})))
|
||||
}
|
||||
SlideshowAction::PrevSlide => {
|
||||
if state.current_slide > 0 {
|
||||
state.current_slide -= 1;
|
||||
}
|
||||
Ok(HandResult::success(serde_json::json!({
|
||||
"status": "prev",
|
||||
"current_slide": state.current_slide,
|
||||
"total_slides": state.total_slides,
|
||||
})))
|
||||
}
|
||||
SlideshowAction::GotoSlide { slide_number } => {
|
||||
if slide_number < state.total_slides {
|
||||
state.current_slide = slide_number;
|
||||
Ok(HandResult::success(serde_json::json!({
|
||||
"status": "goto",
|
||||
"current_slide": state.current_slide,
|
||||
"slide_content": state.slides.get(slide_number),
|
||||
})))
|
||||
} else {
|
||||
Ok(HandResult::error(format!("Slide {} out of range", slide_number)))
|
||||
}
|
||||
}
|
||||
SlideshowAction::Spotlight { element_id, duration_ms } => {
|
||||
Ok(HandResult::success(serde_json::json!({
|
||||
"status": "spotlight",
|
||||
"element_id": element_id,
|
||||
"duration_ms": duration_ms,
|
||||
})))
|
||||
}
|
||||
SlideshowAction::Laser { x, y, duration_ms } => {
|
||||
Ok(HandResult::success(serde_json::json!({
|
||||
"status": "laser",
|
||||
"x": x,
|
||||
"y": y,
|
||||
"duration_ms": duration_ms,
|
||||
})))
|
||||
}
|
||||
SlideshowAction::Highlight { x, y, width, height, color, duration_ms } => {
|
||||
Ok(HandResult::success(serde_json::json!({
|
||||
"status": "highlight",
|
||||
"x": x, "y": y,
|
||||
"width": width, "height": height,
|
||||
"color": color.unwrap_or_else(|| "#ffcc00".to_string()),
|
||||
"duration_ms": duration_ms,
|
||||
})))
|
||||
}
|
||||
SlideshowAction::PlayAnimation { animation_id } => {
|
||||
Ok(HandResult::success(serde_json::json!({
|
||||
"status": "animation",
|
||||
"animation_id": animation_id,
|
||||
})))
|
||||
}
|
||||
SlideshowAction::Pause => {
|
||||
state.is_playing = false;
|
||||
Ok(HandResult::success(serde_json::json!({
|
||||
"status": "paused",
|
||||
})))
|
||||
}
|
||||
SlideshowAction::Resume => {
|
||||
state.is_playing = true;
|
||||
Ok(HandResult::success(serde_json::json!({
|
||||
"status": "resumed",
|
||||
})))
|
||||
}
|
||||
SlideshowAction::AutoPlay { interval_ms } => {
|
||||
state.is_playing = true;
|
||||
state.auto_play_interval_ms = interval_ms;
|
||||
Ok(HandResult::success(serde_json::json!({
|
||||
"status": "autoplay",
|
||||
"interval_ms": interval_ms,
|
||||
})))
|
||||
}
|
||||
SlideshowAction::StopAutoPlay => {
|
||||
state.is_playing = false;
|
||||
Ok(HandResult::success(serde_json::json!({
|
||||
"status": "stopped",
|
||||
})))
|
||||
}
|
||||
SlideshowAction::GetState => {
|
||||
Ok(HandResult::success(serde_json::to_value(&*state).unwrap_or(Value::Null)))
|
||||
}
|
||||
SlideshowAction::SetContent { slide_number, content } => {
|
||||
if slide_number < state.slides.len() {
|
||||
state.slides[slide_number] = content.clone();
|
||||
Ok(HandResult::success(serde_json::json!({
|
||||
"status": "content_set",
|
||||
"slide_number": slide_number,
|
||||
})))
|
||||
} else if slide_number == state.slides.len() {
|
||||
state.slides.push(content);
|
||||
state.total_slides = state.slides.len();
|
||||
Ok(HandResult::success(serde_json::json!({
|
||||
"status": "slide_added",
|
||||
"slide_number": slide_number,
|
||||
})))
|
||||
} else {
|
||||
Ok(HandResult::error(format!("Invalid slide number: {}", slide_number)))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Get current state
|
||||
pub async fn get_state(&self) -> SlideshowState {
|
||||
self.state.read().await.clone()
|
||||
}
|
||||
|
||||
/// Add a slide
|
||||
pub async fn add_slide(&self, content: SlideContent) {
|
||||
let mut state = self.state.write().await;
|
||||
state.slides.push(content);
|
||||
state.total_slides = state.slides.len();
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for SlideshowHand {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Hand for SlideshowHand {
|
||||
fn config(&self) -> &HandConfig {
|
||||
&self.config
|
||||
}
|
||||
|
||||
async fn execute(&self, _context: &HandContext, input: Value) -> Result<HandResult> {
|
||||
let action: SlideshowAction = match serde_json::from_value(input) {
|
||||
Ok(a) => a,
|
||||
Err(e) => {
|
||||
return Ok(HandResult::error(format!("Invalid slideshow action: {}", e)));
|
||||
}
|
||||
};
|
||||
|
||||
self.execute_action(action).await
|
||||
}
|
||||
|
||||
fn status(&self) -> HandStatus {
|
||||
HandStatus::Idle
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
|
||||
// === Config & Defaults ===
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_slideshow_creation() {
|
||||
let hand = SlideshowHand::new();
|
||||
assert_eq!(hand.config().id, "slideshow");
|
||||
assert_eq!(hand.config().name, "幻灯片");
|
||||
assert!(!hand.config().needs_approval);
|
||||
assert!(hand.config().enabled);
|
||||
assert!(hand.config().tags.contains(&"presentation".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_default_impl() {
|
||||
let hand = SlideshowHand::default();
|
||||
assert_eq!(hand.config().id, "slideshow");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_needs_approval() {
|
||||
let hand = SlideshowHand::new();
|
||||
assert!(!hand.needs_approval());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_status() {
|
||||
let hand = SlideshowHand::new();
|
||||
assert_eq!(hand.status(), HandStatus::Idle);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_default_state() {
|
||||
let state = SlideshowState::default();
|
||||
assert_eq!(state.current_slide, 0);
|
||||
assert_eq!(state.total_slides, 0);
|
||||
assert!(!state.is_playing);
|
||||
assert_eq!(state.auto_play_interval_ms, 5000);
|
||||
assert!(state.slides.is_empty());
|
||||
}
|
||||
|
||||
// === Navigation ===
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_navigation() {
|
||||
let hand = SlideshowHand::with_slides_async(vec![
|
||||
SlideContent { title: "Slide 1".to_string(), subtitle: None, content: vec![], notes: None, background: None },
|
||||
SlideContent { title: "Slide 2".to_string(), subtitle: None, content: vec![], notes: None, background: None },
|
||||
SlideContent { title: "Slide 3".to_string(), subtitle: None, content: vec![], notes: None, background: None },
|
||||
]).await;
|
||||
|
||||
// Next
|
||||
hand.execute_action(SlideshowAction::NextSlide).await.unwrap();
|
||||
assert_eq!(hand.get_state().await.current_slide, 1);
|
||||
|
||||
// Goto
|
||||
hand.execute_action(SlideshowAction::GotoSlide { slide_number: 2 }).await.unwrap();
|
||||
assert_eq!(hand.get_state().await.current_slide, 2);
|
||||
|
||||
// Prev
|
||||
hand.execute_action(SlideshowAction::PrevSlide).await.unwrap();
|
||||
assert_eq!(hand.get_state().await.current_slide, 1);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_next_slide_at_end() {
|
||||
let hand = SlideshowHand::with_slides_async(vec![
|
||||
SlideContent { title: "Only Slide".to_string(), subtitle: None, content: vec![], notes: None, background: None },
|
||||
]).await;
|
||||
|
||||
// At slide 0, should not advance past last slide
|
||||
hand.execute_action(SlideshowAction::NextSlide).await.unwrap();
|
||||
assert_eq!(hand.get_state().await.current_slide, 0);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_prev_slide_at_beginning() {
|
||||
let hand = SlideshowHand::with_slides_async(vec![
|
||||
SlideContent { title: "Slide 1".to_string(), subtitle: None, content: vec![], notes: None, background: None },
|
||||
SlideContent { title: "Slide 2".to_string(), subtitle: None, content: vec![], notes: None, background: None },
|
||||
]).await;
|
||||
|
||||
// At slide 0, should not go below 0
|
||||
hand.execute_action(SlideshowAction::PrevSlide).await.unwrap();
|
||||
assert_eq!(hand.get_state().await.current_slide, 0);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_goto_slide_out_of_range() {
|
||||
let hand = SlideshowHand::with_slides_async(vec![
|
||||
SlideContent { title: "Slide 1".to_string(), subtitle: None, content: vec![], notes: None, background: None },
|
||||
]).await;
|
||||
|
||||
let result = hand.execute_action(SlideshowAction::GotoSlide { slide_number: 5 }).await.unwrap();
|
||||
assert!(!result.success);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_goto_slide_returns_content() {
|
||||
let hand = SlideshowHand::with_slides_async(vec![
|
||||
SlideContent { title: "First".to_string(), subtitle: None, content: vec![], notes: None, background: None },
|
||||
SlideContent { title: "Second".to_string(), subtitle: None, content: vec![], notes: None, background: None },
|
||||
]).await;
|
||||
|
||||
let result = hand.execute_action(SlideshowAction::GotoSlide { slide_number: 1 }).await.unwrap();
|
||||
assert!(result.success);
|
||||
assert_eq!(result.output["slide_content"]["title"], "Second");
|
||||
}
|
||||
|
||||
// === Spotlight & Laser & Highlight ===
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_spotlight() {
|
||||
let hand = SlideshowHand::new();
|
||||
let action = SlideshowAction::Spotlight {
|
||||
element_id: "title".to_string(),
|
||||
duration_ms: 2000,
|
||||
};
|
||||
|
||||
let result = hand.execute_action(action).await.unwrap();
|
||||
assert!(result.success);
|
||||
assert_eq!(result.output["element_id"], "title");
|
||||
assert_eq!(result.output["duration_ms"], 2000);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_spotlight_default_duration() {
|
||||
let hand = SlideshowHand::new();
|
||||
let action = SlideshowAction::Spotlight {
|
||||
element_id: "elem".to_string(),
|
||||
duration_ms: default_spotlight_duration(),
|
||||
};
|
||||
|
||||
let result = hand.execute_action(action).await.unwrap();
|
||||
assert_eq!(result.output["duration_ms"], 2000);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_laser() {
|
||||
let hand = SlideshowHand::new();
|
||||
let action = SlideshowAction::Laser {
|
||||
x: 100.0,
|
||||
y: 200.0,
|
||||
duration_ms: 3000,
|
||||
};
|
||||
|
||||
let result = hand.execute_action(action).await.unwrap();
|
||||
assert!(result.success);
|
||||
assert_eq!(result.output["x"], 100.0);
|
||||
assert_eq!(result.output["y"], 200.0);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_highlight_default_color() {
|
||||
let hand = SlideshowHand::new();
|
||||
let action = SlideshowAction::Highlight {
|
||||
x: 10.0, y: 20.0, width: 100.0, height: 50.0,
|
||||
color: None, duration_ms: 2000,
|
||||
};
|
||||
|
||||
let result = hand.execute_action(action).await.unwrap();
|
||||
assert!(result.success);
|
||||
assert_eq!(result.output["color"], "#ffcc00");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_highlight_custom_color() {
|
||||
let hand = SlideshowHand::new();
|
||||
let action = SlideshowAction::Highlight {
|
||||
x: 0.0, y: 0.0, width: 50.0, height: 50.0,
|
||||
color: Some("#ff0000".to_string()), duration_ms: 1000,
|
||||
};
|
||||
|
||||
let result = hand.execute_action(action).await.unwrap();
|
||||
assert_eq!(result.output["color"], "#ff0000");
|
||||
}
|
||||
|
||||
// === AutoPlay / Pause / Resume ===
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_autoplay_pause_resume() {
|
||||
let hand = SlideshowHand::new();
|
||||
|
||||
// AutoPlay
|
||||
let result = hand.execute_action(SlideshowAction::AutoPlay { interval_ms: 3000 }).await.unwrap();
|
||||
assert!(result.success);
|
||||
assert!(hand.get_state().await.is_playing);
|
||||
assert_eq!(hand.get_state().await.auto_play_interval_ms, 3000);
|
||||
|
||||
// Pause
|
||||
hand.execute_action(SlideshowAction::Pause).await.unwrap();
|
||||
assert!(!hand.get_state().await.is_playing);
|
||||
|
||||
// Resume
|
||||
hand.execute_action(SlideshowAction::Resume).await.unwrap();
|
||||
assert!(hand.get_state().await.is_playing);
|
||||
|
||||
// Stop
|
||||
hand.execute_action(SlideshowAction::StopAutoPlay).await.unwrap();
|
||||
assert!(!hand.get_state().await.is_playing);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_autoplay_default_interval() {
|
||||
let hand = SlideshowHand::new();
|
||||
hand.execute_action(SlideshowAction::AutoPlay { interval_ms: default_interval() }).await.unwrap();
|
||||
assert_eq!(hand.get_state().await.auto_play_interval_ms, 5000);
|
||||
}
|
||||
|
||||
// === PlayAnimation ===
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_play_animation() {
|
||||
let hand = SlideshowHand::new();
|
||||
let result = hand.execute_action(SlideshowAction::PlayAnimation {
|
||||
animation_id: "fade_in".to_string(),
|
||||
}).await.unwrap();
|
||||
|
||||
assert!(result.success);
|
||||
assert_eq!(result.output["animation_id"], "fade_in");
|
||||
}
|
||||
|
||||
// === GetState ===
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_get_state() {
|
||||
let hand = SlideshowHand::with_slides_async(vec![
|
||||
SlideContent { title: "A".to_string(), subtitle: None, content: vec![], notes: None, background: None },
|
||||
]).await;
|
||||
|
||||
let result = hand.execute_action(SlideshowAction::GetState).await.unwrap();
|
||||
assert!(result.success);
|
||||
assert_eq!(result.output["total_slides"], 1);
|
||||
assert_eq!(result.output["current_slide"], 0);
|
||||
}
|
||||
|
||||
// === SetContent ===
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_set_content() {
|
||||
let hand = SlideshowHand::new();
|
||||
|
||||
let content = SlideContent {
|
||||
title: "Test Slide".to_string(),
|
||||
subtitle: Some("Subtitle".to_string()),
|
||||
content: vec![ContentBlock::Text {
|
||||
text: "Hello".to_string(),
|
||||
style: None,
|
||||
}],
|
||||
notes: Some("Speaker notes".to_string()),
|
||||
background: None,
|
||||
};
|
||||
|
||||
let result = hand.execute_action(SlideshowAction::SetContent {
|
||||
slide_number: 0,
|
||||
content,
|
||||
}).await.unwrap();
|
||||
|
||||
assert!(result.success);
|
||||
assert_eq!(hand.get_state().await.total_slides, 1);
|
||||
assert_eq!(hand.get_state().await.slides[0].title, "Test Slide");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_set_content_append() {
|
||||
let hand = SlideshowHand::with_slides_async(vec![
|
||||
SlideContent { title: "First".to_string(), subtitle: None, content: vec![], notes: None, background: None },
|
||||
]).await;
|
||||
|
||||
let content = SlideContent {
|
||||
title: "Appended".to_string(), subtitle: None, content: vec![], notes: None, background: None,
|
||||
};
|
||||
|
||||
let result = hand.execute_action(SlideshowAction::SetContent {
|
||||
slide_number: 1,
|
||||
content,
|
||||
}).await.unwrap();
|
||||
|
||||
assert!(result.success);
|
||||
assert_eq!(result.output["status"], "slide_added");
|
||||
assert_eq!(hand.get_state().await.total_slides, 2);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_set_content_invalid_index() {
|
||||
let hand = SlideshowHand::new();
|
||||
|
||||
let content = SlideContent {
|
||||
title: "Gap".to_string(), subtitle: None, content: vec![], notes: None, background: None,
|
||||
};
|
||||
|
||||
let result = hand.execute_action(SlideshowAction::SetContent {
|
||||
slide_number: 5,
|
||||
content,
|
||||
}).await.unwrap();
|
||||
|
||||
assert!(!result.success);
|
||||
}
|
||||
|
||||
// === Action Deserialization ===
|
||||
|
||||
#[test]
|
||||
fn test_deserialize_next_slide() {
|
||||
let action: SlideshowAction = serde_json::from_value(json!({"action": "next_slide"})).unwrap();
|
||||
assert!(matches!(action, SlideshowAction::NextSlide));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_deserialize_goto_slide() {
|
||||
let action: SlideshowAction = serde_json::from_value(json!({"action": "goto_slide", "slide_number": 3})).unwrap();
|
||||
match action {
|
||||
SlideshowAction::GotoSlide { slide_number } => assert_eq!(slide_number, 3),
|
||||
_ => panic!("Expected GotoSlide"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_deserialize_laser() {
|
||||
let action: SlideshowAction = serde_json::from_value(json!({
|
||||
"action": "laser", "x": 50.0, "y": 75.0
|
||||
})).unwrap();
|
||||
match action {
|
||||
SlideshowAction::Laser { x, y, .. } => {
|
||||
assert_eq!(x, 50.0);
|
||||
assert_eq!(y, 75.0);
|
||||
}
|
||||
_ => panic!("Expected Laser"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_deserialize_autoplay() {
|
||||
let action: SlideshowAction = serde_json::from_value(json!({"action": "auto_play"})).unwrap();
|
||||
match action {
|
||||
SlideshowAction::AutoPlay { interval_ms } => assert_eq!(interval_ms, 5000),
|
||||
_ => panic!("Expected AutoPlay"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_deserialize_invalid_action() {
|
||||
let result = serde_json::from_value::<SlideshowAction>(json!({"action": "nonexistent"}));
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
// === ContentBlock Deserialization ===
|
||||
|
||||
#[test]
|
||||
fn test_content_block_text() {
|
||||
let block: ContentBlock = serde_json::from_value(json!({
|
||||
"type": "text", "text": "Hello"
|
||||
})).unwrap();
|
||||
match block {
|
||||
ContentBlock::Text { text, style } => {
|
||||
assert_eq!(text, "Hello");
|
||||
assert!(style.is_none());
|
||||
}
|
||||
_ => panic!("Expected Text"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_content_block_list() {
|
||||
let block: ContentBlock = serde_json::from_value(json!({
|
||||
"type": "list", "items": ["A", "B"], "ordered": true
|
||||
})).unwrap();
|
||||
match block {
|
||||
ContentBlock::List { items, ordered } => {
|
||||
assert_eq!(items, vec!["A", "B"]);
|
||||
assert!(ordered);
|
||||
}
|
||||
_ => panic!("Expected List"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_content_block_code() {
|
||||
let block: ContentBlock = serde_json::from_value(json!({
|
||||
"type": "code", "code": "fn main() {}", "language": "rust"
|
||||
})).unwrap();
|
||||
match block {
|
||||
ContentBlock::Code { code, language } => {
|
||||
assert_eq!(code, "fn main() {}");
|
||||
assert_eq!(language, Some("rust".to_string()));
|
||||
}
|
||||
_ => panic!("Expected Code"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_content_block_table() {
|
||||
let block: ContentBlock = serde_json::from_value(json!({
|
||||
"type": "table",
|
||||
"headers": ["Name", "Age"],
|
||||
"rows": [["Alice", "30"]]
|
||||
})).unwrap();
|
||||
match block {
|
||||
ContentBlock::Table { headers, rows } => {
|
||||
assert_eq!(headers, vec!["Name", "Age"]);
|
||||
assert_eq!(rows, vec![vec!["Alice", "30"]]);
|
||||
}
|
||||
_ => panic!("Expected Table"),
|
||||
}
|
||||
}
|
||||
|
||||
// === Hand trait via execute ===
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_hand_execute_dispatch() {
|
||||
let hand = SlideshowHand::with_slides_async(vec![
|
||||
SlideContent { title: "S1".to_string(), subtitle: None, content: vec![], notes: None, background: None },
|
||||
SlideContent { title: "S2".to_string(), subtitle: None, content: vec![], notes: None, background: None },
|
||||
]).await;
|
||||
|
||||
let ctx = HandContext::default();
|
||||
let result = hand.execute(&ctx, json!({"action": "next_slide"})).await.unwrap();
|
||||
assert!(result.success);
|
||||
assert_eq!(result.output["current_slide"], 1);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_hand_execute_invalid_action() {
|
||||
let hand = SlideshowHand::new();
|
||||
let ctx = HandContext::default();
|
||||
let result = hand.execute(&ctx, json!({"action": "invalid"})).await.unwrap();
|
||||
assert!(!result.success);
|
||||
}
|
||||
|
||||
// === add_slide helper ===
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_add_slide() {
|
||||
let hand = SlideshowHand::new();
|
||||
hand.add_slide(SlideContent {
|
||||
title: "Dynamic".to_string(), subtitle: None, content: vec![], notes: None, background: None,
|
||||
}).await;
|
||||
hand.add_slide(SlideContent {
|
||||
title: "Dynamic 2".to_string(), subtitle: None, content: vec![], notes: None, background: None,
|
||||
}).await;
|
||||
|
||||
let state = hand.get_state().await;
|
||||
assert_eq!(state.total_slides, 2);
|
||||
assert_eq!(state.slides.len(), 2);
|
||||
}
|
||||
}
|
||||
@@ -1,442 +0,0 @@
|
||||
//! Speech Hand - Text-to-Speech synthesis capabilities
|
||||
//!
|
||||
//! Provides speech synthesis for teaching:
|
||||
//! - speak: Convert text to speech
|
||||
//! - speak_ssml: Advanced speech with SSML markup
|
||||
//! - pause/resume/stop: Playback control
|
||||
//! - list_voices: Get available voices
|
||||
//! - set_voice: Configure voice settings
|
||||
|
||||
use async_trait::async_trait;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::RwLock;
|
||||
use zclaw_types::Result;
|
||||
|
||||
use crate::{Hand, HandConfig, HandContext, HandResult, HandStatus};
|
||||
|
||||
/// TTS Provider types
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum TtsProvider {
|
||||
#[default]
|
||||
Browser,
|
||||
Azure,
|
||||
OpenAI,
|
||||
ElevenLabs,
|
||||
Local,
|
||||
}
|
||||
|
||||
/// Speech action types
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(tag = "action", rename_all = "snake_case")]
|
||||
pub enum SpeechAction {
|
||||
/// Speak text
|
||||
Speak {
|
||||
text: String,
|
||||
#[serde(default)]
|
||||
voice: Option<String>,
|
||||
#[serde(default = "default_rate")]
|
||||
rate: f32,
|
||||
#[serde(default = "default_pitch")]
|
||||
pitch: f32,
|
||||
#[serde(default = "default_volume")]
|
||||
volume: f32,
|
||||
#[serde(default)]
|
||||
language: Option<String>,
|
||||
},
|
||||
/// Speak with SSML markup
|
||||
SpeakSsml {
|
||||
ssml: String,
|
||||
#[serde(default)]
|
||||
voice: Option<String>,
|
||||
},
|
||||
/// Pause playback
|
||||
Pause,
|
||||
/// Resume playback
|
||||
Resume,
|
||||
/// Stop playback
|
||||
Stop,
|
||||
/// List available voices
|
||||
ListVoices {
|
||||
#[serde(default)]
|
||||
language: Option<String>,
|
||||
},
|
||||
/// Set default voice
|
||||
SetVoice {
|
||||
voice: String,
|
||||
#[serde(default)]
|
||||
language: Option<String>,
|
||||
},
|
||||
/// Set provider
|
||||
SetProvider {
|
||||
provider: TtsProvider,
|
||||
#[serde(default)]
|
||||
api_key: Option<String>,
|
||||
#[serde(default)]
|
||||
region: Option<String>,
|
||||
},
|
||||
}
|
||||
|
||||
fn default_rate() -> f32 { 1.0 }
|
||||
fn default_pitch() -> f32 { 1.0 }
|
||||
fn default_volume() -> f32 { 1.0 }
|
||||
|
||||
/// Voice information
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct VoiceInfo {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
pub language: String,
|
||||
pub gender: String,
|
||||
#[serde(default)]
|
||||
pub preview_url: Option<String>,
|
||||
}
|
||||
|
||||
/// Playback state
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
||||
pub enum PlaybackState {
|
||||
#[default]
|
||||
Idle,
|
||||
Playing,
|
||||
Paused,
|
||||
}
|
||||
|
||||
/// Speech configuration
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct SpeechConfig {
|
||||
pub provider: TtsProvider,
|
||||
pub default_voice: Option<String>,
|
||||
pub default_language: String,
|
||||
pub default_rate: f32,
|
||||
pub default_pitch: f32,
|
||||
pub default_volume: f32,
|
||||
}
|
||||
|
||||
impl Default for SpeechConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
provider: TtsProvider::Browser,
|
||||
default_voice: None,
|
||||
default_language: "zh-CN".to_string(),
|
||||
default_rate: 1.0,
|
||||
default_pitch: 1.0,
|
||||
default_volume: 1.0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Speech state
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct SpeechState {
|
||||
pub config: SpeechConfig,
|
||||
pub playback: PlaybackState,
|
||||
pub current_text: Option<String>,
|
||||
pub position_ms: u64,
|
||||
pub available_voices: Vec<VoiceInfo>,
|
||||
}
|
||||
|
||||
/// Speech Hand implementation
|
||||
pub struct SpeechHand {
|
||||
config: HandConfig,
|
||||
state: Arc<RwLock<SpeechState>>,
|
||||
}
|
||||
|
||||
impl SpeechHand {
|
||||
/// Create a new speech hand
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
config: HandConfig {
|
||||
id: "speech".to_string(),
|
||||
name: "语音合成".to_string(),
|
||||
description: "文本转语音合成输出".to_string(),
|
||||
needs_approval: false,
|
||||
dependencies: vec![],
|
||||
input_schema: Some(serde_json::json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"action": { "type": "string" },
|
||||
"text": { "type": "string" },
|
||||
"voice": { "type": "string" },
|
||||
"rate": { "type": "number" },
|
||||
}
|
||||
})),
|
||||
tags: vec!["audio".to_string(), "tts".to_string(), "education".to_string(), "demo".to_string()],
|
||||
enabled: true,
|
||||
max_concurrent: 0,
|
||||
timeout_secs: 0,
|
||||
},
|
||||
state: Arc::new(RwLock::new(SpeechState {
|
||||
config: SpeechConfig::default(),
|
||||
playback: PlaybackState::Idle,
|
||||
available_voices: Self::get_default_voices(),
|
||||
..Default::default()
|
||||
})),
|
||||
}
|
||||
}
|
||||
|
||||
/// Create with custom provider
|
||||
pub fn with_provider(provider: TtsProvider) -> Self {
|
||||
let hand = Self::new();
|
||||
let mut state = hand.state.blocking_write();
|
||||
state.config.provider = provider;
|
||||
drop(state);
|
||||
hand
|
||||
}
|
||||
|
||||
/// Get default voices
|
||||
fn get_default_voices() -> Vec<VoiceInfo> {
|
||||
vec![
|
||||
VoiceInfo {
|
||||
id: "zh-CN-XiaoxiaoNeural".to_string(),
|
||||
name: "Xiaoxiao".to_string(),
|
||||
language: "zh-CN".to_string(),
|
||||
gender: "female".to_string(),
|
||||
preview_url: None,
|
||||
},
|
||||
VoiceInfo {
|
||||
id: "zh-CN-YunxiNeural".to_string(),
|
||||
name: "Yunxi".to_string(),
|
||||
language: "zh-CN".to_string(),
|
||||
gender: "male".to_string(),
|
||||
preview_url: None,
|
||||
},
|
||||
VoiceInfo {
|
||||
id: "en-US-JennyNeural".to_string(),
|
||||
name: "Jenny".to_string(),
|
||||
language: "en-US".to_string(),
|
||||
gender: "female".to_string(),
|
||||
preview_url: None,
|
||||
},
|
||||
VoiceInfo {
|
||||
id: "en-US-GuyNeural".to_string(),
|
||||
name: "Guy".to_string(),
|
||||
language: "en-US".to_string(),
|
||||
gender: "male".to_string(),
|
||||
preview_url: None,
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
/// Execute a speech action
|
||||
pub async fn execute_action(&self, action: SpeechAction) -> Result<HandResult> {
|
||||
let mut state = self.state.write().await;
|
||||
|
||||
match action {
|
||||
SpeechAction::Speak { text, voice, rate, pitch, volume, language } => {
|
||||
let voice_id = voice.or(state.config.default_voice.clone())
|
||||
.unwrap_or_else(|| "default".to_string());
|
||||
let lang = language.unwrap_or_else(|| state.config.default_language.clone());
|
||||
let actual_rate = if rate == 1.0 { state.config.default_rate } else { rate };
|
||||
let actual_pitch = if pitch == 1.0 { state.config.default_pitch } else { pitch };
|
||||
let actual_volume = if volume == 1.0 { state.config.default_volume } else { volume };
|
||||
|
||||
state.playback = PlaybackState::Playing;
|
||||
state.current_text = Some(text.clone());
|
||||
|
||||
// Determine TTS method based on provider:
|
||||
// - Browser: frontend uses Web Speech API (zero deps, works offline)
|
||||
// - OpenAI: frontend calls speech_tts command (high-quality, needs API key)
|
||||
// - Others: future support
|
||||
let tts_method = match state.config.provider {
|
||||
TtsProvider::Browser => "browser",
|
||||
TtsProvider::OpenAI => "openai_api",
|
||||
TtsProvider::Azure => "azure_api",
|
||||
TtsProvider::ElevenLabs => "elevenlabs_api",
|
||||
TtsProvider::Local => "local_engine",
|
||||
};
|
||||
|
||||
let estimated_duration_ms = (text.chars().count() as f64 / 5.0 * 1000.0) as u64;
|
||||
|
||||
Ok(HandResult::success(serde_json::json!({
|
||||
"status": "speaking",
|
||||
"tts_method": tts_method,
|
||||
"text": text,
|
||||
"voice": voice_id,
|
||||
"language": lang,
|
||||
"rate": actual_rate,
|
||||
"pitch": actual_pitch,
|
||||
"volume": actual_volume,
|
||||
"provider": format!("{:?}", state.config.provider).to_lowercase(),
|
||||
"duration_ms": estimated_duration_ms,
|
||||
"instruction": "Frontend should play this via TTS engine"
|
||||
})))
|
||||
}
|
||||
SpeechAction::SpeakSsml { ssml, voice } => {
|
||||
let voice_id = voice.or(state.config.default_voice.clone())
|
||||
.unwrap_or_else(|| "default".to_string());
|
||||
|
||||
state.playback = PlaybackState::Playing;
|
||||
state.current_text = Some(ssml.clone());
|
||||
|
||||
Ok(HandResult::success(serde_json::json!({
|
||||
"status": "speaking_ssml",
|
||||
"ssml": ssml,
|
||||
"voice": voice_id,
|
||||
"provider": state.config.provider,
|
||||
})))
|
||||
}
|
||||
SpeechAction::Pause => {
|
||||
state.playback = PlaybackState::Paused;
|
||||
Ok(HandResult::success(serde_json::json!({
|
||||
"status": "paused",
|
||||
"position_ms": state.position_ms,
|
||||
})))
|
||||
}
|
||||
SpeechAction::Resume => {
|
||||
state.playback = PlaybackState::Playing;
|
||||
Ok(HandResult::success(serde_json::json!({
|
||||
"status": "resumed",
|
||||
"position_ms": state.position_ms,
|
||||
})))
|
||||
}
|
||||
SpeechAction::Stop => {
|
||||
state.playback = PlaybackState::Idle;
|
||||
state.current_text = None;
|
||||
state.position_ms = 0;
|
||||
Ok(HandResult::success(serde_json::json!({
|
||||
"status": "stopped",
|
||||
})))
|
||||
}
|
||||
SpeechAction::ListVoices { language } => {
|
||||
let voices: Vec<_> = state.available_voices.iter()
|
||||
.filter(|v| {
|
||||
language.as_ref()
|
||||
.map(|l| v.language.starts_with(l))
|
||||
.unwrap_or(true)
|
||||
})
|
||||
.cloned()
|
||||
.collect();
|
||||
|
||||
Ok(HandResult::success(serde_json::json!({
|
||||
"voices": voices,
|
||||
"count": voices.len(),
|
||||
})))
|
||||
}
|
||||
SpeechAction::SetVoice { voice, language } => {
|
||||
state.config.default_voice = Some(voice.clone());
|
||||
if let Some(lang) = language {
|
||||
state.config.default_language = lang;
|
||||
}
|
||||
Ok(HandResult::success(serde_json::json!({
|
||||
"status": "voice_set",
|
||||
"voice": voice,
|
||||
"language": state.config.default_language,
|
||||
})))
|
||||
}
|
||||
SpeechAction::SetProvider { provider, api_key, region: _ } => {
|
||||
state.config.provider = provider.clone();
|
||||
// In real implementation, would configure provider
|
||||
Ok(HandResult::success(serde_json::json!({
|
||||
"status": "provider_set",
|
||||
"provider": provider,
|
||||
"configured": api_key.is_some(),
|
||||
})))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Get current state
|
||||
pub async fn get_state(&self) -> SpeechState {
|
||||
self.state.read().await.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for SpeechHand {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Hand for SpeechHand {
|
||||
fn config(&self) -> &HandConfig {
|
||||
&self.config
|
||||
}
|
||||
|
||||
async fn execute(&self, _context: &HandContext, input: Value) -> Result<HandResult> {
|
||||
let action: SpeechAction = match serde_json::from_value(input) {
|
||||
Ok(a) => a,
|
||||
Err(e) => {
|
||||
return Ok(HandResult::error(format!("Invalid speech action: {}", e)));
|
||||
}
|
||||
};
|
||||
|
||||
self.execute_action(action).await
|
||||
}
|
||||
|
||||
fn status(&self) -> HandStatus {
|
||||
HandStatus::Idle
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_speech_creation() {
|
||||
let hand = SpeechHand::new();
|
||||
assert_eq!(hand.config().id, "speech");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_speak() {
|
||||
let hand = SpeechHand::new();
|
||||
let action = SpeechAction::Speak {
|
||||
text: "Hello, world!".to_string(),
|
||||
voice: None,
|
||||
rate: 1.0,
|
||||
pitch: 1.0,
|
||||
volume: 1.0,
|
||||
language: None,
|
||||
};
|
||||
|
||||
let result = hand.execute_action(action).await.unwrap();
|
||||
assert!(result.success);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_pause_resume() {
|
||||
let hand = SpeechHand::new();
|
||||
|
||||
// Speak first
|
||||
hand.execute_action(SpeechAction::Speak {
|
||||
text: "Test".to_string(),
|
||||
voice: None, rate: 1.0, pitch: 1.0, volume: 1.0, language: None,
|
||||
}).await.unwrap();
|
||||
|
||||
// Pause
|
||||
let result = hand.execute_action(SpeechAction::Pause).await.unwrap();
|
||||
assert!(result.success);
|
||||
|
||||
// Resume
|
||||
let result = hand.execute_action(SpeechAction::Resume).await.unwrap();
|
||||
assert!(result.success);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_list_voices() {
|
||||
let hand = SpeechHand::new();
|
||||
let action = SpeechAction::ListVoices { language: Some("zh".to_string()) };
|
||||
|
||||
let result = hand.execute_action(action).await.unwrap();
|
||||
assert!(result.success);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_set_voice() {
|
||||
let hand = SpeechHand::new();
|
||||
let action = SpeechAction::SetVoice {
|
||||
voice: "zh-CN-XiaoxiaoNeural".to_string(),
|
||||
language: Some("zh-CN".to_string()),
|
||||
};
|
||||
|
||||
let result = hand.execute_action(action).await.unwrap();
|
||||
assert!(result.success);
|
||||
|
||||
let state = hand.get_state().await;
|
||||
assert_eq!(state.config.default_voice, Some("zh-CN-XiaoxiaoNeural".to_string()));
|
||||
}
|
||||
}
|
||||
@@ -1,422 +0,0 @@
|
||||
//! Whiteboard Hand - Drawing and annotation capabilities
|
||||
//!
|
||||
//! Provides whiteboard drawing actions for teaching:
|
||||
//! - draw_text: Draw text on the whiteboard
|
||||
//! - draw_shape: Draw shapes (rectangle, circle, arrow, etc.)
|
||||
//! - draw_line: Draw lines and curves
|
||||
//! - draw_chart: Draw charts (bar, line, pie)
|
||||
//! - draw_latex: Render LaTeX formulas
|
||||
//! - draw_table: Draw data tables
|
||||
//! - clear: Clear the whiteboard
|
||||
//! - export: Export as image
|
||||
|
||||
use async_trait::async_trait;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
use zclaw_types::Result;
|
||||
|
||||
use crate::{Hand, HandConfig, HandContext, HandResult, HandStatus};
|
||||
|
||||
/// Whiteboard action types
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(tag = "action", rename_all = "snake_case")]
|
||||
pub enum WhiteboardAction {
|
||||
/// Draw text
|
||||
DrawText {
|
||||
x: f64,
|
||||
y: f64,
|
||||
text: String,
|
||||
#[serde(default = "default_font_size")]
|
||||
font_size: u32,
|
||||
#[serde(default)]
|
||||
color: Option<String>,
|
||||
#[serde(default)]
|
||||
font_family: Option<String>,
|
||||
},
|
||||
/// Draw a shape
|
||||
DrawShape {
|
||||
shape: ShapeType,
|
||||
x: f64,
|
||||
y: f64,
|
||||
width: f64,
|
||||
height: f64,
|
||||
#[serde(default)]
|
||||
fill: Option<String>,
|
||||
#[serde(default)]
|
||||
stroke: Option<String>,
|
||||
#[serde(default = "default_stroke_width")]
|
||||
stroke_width: u32,
|
||||
},
|
||||
/// Draw a line
|
||||
DrawLine {
|
||||
points: Vec<Point>,
|
||||
#[serde(default)]
|
||||
color: Option<String>,
|
||||
#[serde(default = "default_stroke_width")]
|
||||
stroke_width: u32,
|
||||
},
|
||||
/// Draw a chart
|
||||
DrawChart {
|
||||
chart_type: ChartType,
|
||||
data: ChartData,
|
||||
x: f64,
|
||||
y: f64,
|
||||
width: f64,
|
||||
height: f64,
|
||||
#[serde(default)]
|
||||
title: Option<String>,
|
||||
},
|
||||
/// Draw LaTeX formula
|
||||
DrawLatex {
|
||||
latex: String,
|
||||
x: f64,
|
||||
y: f64,
|
||||
#[serde(default = "default_font_size")]
|
||||
font_size: u32,
|
||||
#[serde(default)]
|
||||
color: Option<String>,
|
||||
},
|
||||
/// Draw a table
|
||||
DrawTable {
|
||||
headers: Vec<String>,
|
||||
rows: Vec<Vec<String>>,
|
||||
x: f64,
|
||||
y: f64,
|
||||
#[serde(default)]
|
||||
column_widths: Option<Vec<f64>>,
|
||||
},
|
||||
/// Erase area
|
||||
Erase {
|
||||
x: f64,
|
||||
y: f64,
|
||||
width: f64,
|
||||
height: f64,
|
||||
},
|
||||
/// Clear whiteboard
|
||||
Clear,
|
||||
/// Undo last action
|
||||
Undo,
|
||||
/// Redo last undone action
|
||||
Redo,
|
||||
/// Export as image
|
||||
Export {
|
||||
#[serde(default = "default_export_format")]
|
||||
format: String,
|
||||
},
|
||||
}
|
||||
|
||||
fn default_font_size() -> u32 { 16 }
|
||||
fn default_stroke_width() -> u32 { 2 }
|
||||
fn default_export_format() -> String { "png".to_string() }
|
||||
|
||||
/// Shape types
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum ShapeType {
|
||||
Rectangle,
|
||||
RoundedRectangle,
|
||||
Circle,
|
||||
Ellipse,
|
||||
Triangle,
|
||||
Arrow,
|
||||
Star,
|
||||
Checkmark,
|
||||
Cross,
|
||||
}
|
||||
|
||||
/// Point for line drawing
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Point {
|
||||
pub x: f64,
|
||||
pub y: f64,
|
||||
}
|
||||
|
||||
/// Chart types
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum ChartType {
|
||||
Bar,
|
||||
Line,
|
||||
Pie,
|
||||
Scatter,
|
||||
Area,
|
||||
Radar,
|
||||
}
|
||||
|
||||
/// Chart data
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ChartData {
|
||||
pub labels: Vec<String>,
|
||||
pub datasets: Vec<Dataset>,
|
||||
}
|
||||
|
||||
/// Dataset for charts
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Dataset {
|
||||
pub label: String,
|
||||
pub values: Vec<f64>,
|
||||
#[serde(default)]
|
||||
pub color: Option<String>,
|
||||
}
|
||||
|
||||
/// Whiteboard state (for undo/redo)
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct WhiteboardState {
|
||||
pub actions: Vec<WhiteboardAction>,
|
||||
pub undone: Vec<WhiteboardAction>,
|
||||
pub canvas_width: f64,
|
||||
pub canvas_height: f64,
|
||||
}
|
||||
|
||||
/// Whiteboard Hand implementation
|
||||
pub struct WhiteboardHand {
|
||||
config: HandConfig,
|
||||
state: std::sync::Arc<tokio::sync::RwLock<WhiteboardState>>,
|
||||
}
|
||||
|
||||
impl WhiteboardHand {
|
||||
/// Create a new whiteboard hand
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
config: HandConfig {
|
||||
id: "whiteboard".to_string(),
|
||||
name: "白板".to_string(),
|
||||
description: "在虚拟白板上绘制和标注".to_string(),
|
||||
needs_approval: false,
|
||||
dependencies: vec![],
|
||||
input_schema: Some(serde_json::json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"action": { "type": "string" },
|
||||
"x": { "type": "number" },
|
||||
"y": { "type": "number" },
|
||||
"text": { "type": "string" },
|
||||
}
|
||||
})),
|
||||
tags: vec!["presentation".to_string(), "education".to_string()],
|
||||
enabled: true,
|
||||
max_concurrent: 0,
|
||||
timeout_secs: 0,
|
||||
},
|
||||
state: std::sync::Arc::new(tokio::sync::RwLock::new(WhiteboardState {
|
||||
canvas_width: 1920.0,
|
||||
canvas_height: 1080.0,
|
||||
..Default::default()
|
||||
})),
|
||||
}
|
||||
}
|
||||
|
||||
/// Create with custom canvas size
|
||||
pub fn with_size(width: f64, height: f64) -> Self {
|
||||
let hand = Self::new();
|
||||
let mut state = hand.state.blocking_write();
|
||||
state.canvas_width = width;
|
||||
state.canvas_height = height;
|
||||
drop(state);
|
||||
hand
|
||||
}
|
||||
|
||||
/// Execute a whiteboard action
|
||||
pub async fn execute_action(&self, action: WhiteboardAction) -> Result<HandResult> {
|
||||
let mut state = self.state.write().await;
|
||||
|
||||
match &action {
|
||||
WhiteboardAction::Clear => {
|
||||
state.actions.clear();
|
||||
state.undone.clear();
|
||||
return Ok(HandResult::success(serde_json::json!({
|
||||
"status": "cleared",
|
||||
"action_count": 0
|
||||
})));
|
||||
}
|
||||
WhiteboardAction::Undo => {
|
||||
if let Some(last) = state.actions.pop() {
|
||||
state.undone.push(last);
|
||||
return Ok(HandResult::success(serde_json::json!({
|
||||
"status": "undone",
|
||||
"remaining_actions": state.actions.len()
|
||||
})));
|
||||
}
|
||||
return Ok(HandResult::success(serde_json::json!({
|
||||
"status": "no_action_to_undo"
|
||||
})));
|
||||
}
|
||||
WhiteboardAction::Redo => {
|
||||
if let Some(redone) = state.undone.pop() {
|
||||
state.actions.push(redone);
|
||||
return Ok(HandResult::success(serde_json::json!({
|
||||
"status": "redone",
|
||||
"total_actions": state.actions.len()
|
||||
})));
|
||||
}
|
||||
return Ok(HandResult::success(serde_json::json!({
|
||||
"status": "no_action_to_redo"
|
||||
})));
|
||||
}
|
||||
WhiteboardAction::Export { format } => {
|
||||
// In real implementation, would render to image
|
||||
return Ok(HandResult::success(serde_json::json!({
|
||||
"status": "exported",
|
||||
"format": format,
|
||||
"data_url": format!("data:image/{};base64,<rendered_data>", format)
|
||||
})));
|
||||
}
|
||||
_ => {
|
||||
// Regular drawing action
|
||||
state.actions.push(action.clone());
|
||||
return Ok(HandResult::success(serde_json::json!({
|
||||
"status": "drawn",
|
||||
"action": action,
|
||||
"total_actions": state.actions.len()
|
||||
})));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Get current state
|
||||
pub async fn get_state(&self) -> WhiteboardState {
|
||||
self.state.read().await.clone()
|
||||
}
|
||||
|
||||
/// Get all actions
|
||||
pub async fn get_actions(&self) -> Vec<WhiteboardAction> {
|
||||
self.state.read().await.actions.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for WhiteboardHand {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Hand for WhiteboardHand {
|
||||
fn config(&self) -> &HandConfig {
|
||||
&self.config
|
||||
}
|
||||
|
||||
async fn execute(&self, _context: &HandContext, input: Value) -> Result<HandResult> {
|
||||
// Parse action from input
|
||||
let action: WhiteboardAction = match serde_json::from_value(input.clone()) {
|
||||
Ok(a) => a,
|
||||
Err(e) => {
|
||||
return Ok(HandResult::error(format!("Invalid whiteboard action: {}", e)));
|
||||
}
|
||||
};
|
||||
|
||||
self.execute_action(action).await
|
||||
}
|
||||
|
||||
fn status(&self) -> HandStatus {
|
||||
// Check if there are any actions
|
||||
HandStatus::Idle
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_whiteboard_creation() {
|
||||
let hand = WhiteboardHand::new();
|
||||
assert_eq!(hand.config().id, "whiteboard");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_draw_text() {
|
||||
let hand = WhiteboardHand::new();
|
||||
let action = WhiteboardAction::DrawText {
|
||||
x: 100.0,
|
||||
y: 100.0,
|
||||
text: "Hello World".to_string(),
|
||||
font_size: 24,
|
||||
color: Some("#333333".to_string()),
|
||||
font_family: None,
|
||||
};
|
||||
|
||||
let result = hand.execute_action(action).await.unwrap();
|
||||
assert!(result.success);
|
||||
|
||||
let state = hand.get_state().await;
|
||||
assert_eq!(state.actions.len(), 1);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_draw_shape() {
|
||||
let hand = WhiteboardHand::new();
|
||||
let action = WhiteboardAction::DrawShape {
|
||||
shape: ShapeType::Rectangle,
|
||||
x: 50.0,
|
||||
y: 50.0,
|
||||
width: 200.0,
|
||||
height: 100.0,
|
||||
fill: Some("#4CAF50".to_string()),
|
||||
stroke: None,
|
||||
stroke_width: 2,
|
||||
};
|
||||
|
||||
let result = hand.execute_action(action).await.unwrap();
|
||||
assert!(result.success);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_undo_redo() {
|
||||
let hand = WhiteboardHand::new();
|
||||
|
||||
// Draw something
|
||||
hand.execute_action(WhiteboardAction::DrawText {
|
||||
x: 0.0, y: 0.0, text: "Test".to_string(), font_size: 16, color: None, font_family: None,
|
||||
}).await.unwrap();
|
||||
|
||||
// Undo
|
||||
let result = hand.execute_action(WhiteboardAction::Undo).await.unwrap();
|
||||
assert!(result.success);
|
||||
assert_eq!(hand.get_state().await.actions.len(), 0);
|
||||
|
||||
// Redo
|
||||
let result = hand.execute_action(WhiteboardAction::Redo).await.unwrap();
|
||||
assert!(result.success);
|
||||
assert_eq!(hand.get_state().await.actions.len(), 1);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_clear() {
|
||||
let hand = WhiteboardHand::new();
|
||||
|
||||
// Draw something
|
||||
hand.execute_action(WhiteboardAction::DrawText {
|
||||
x: 0.0, y: 0.0, text: "Test".to_string(), font_size: 16, color: None, font_family: None,
|
||||
}).await.unwrap();
|
||||
|
||||
// Clear
|
||||
let result = hand.execute_action(WhiteboardAction::Clear).await.unwrap();
|
||||
assert!(result.success);
|
||||
assert_eq!(hand.get_state().await.actions.len(), 0);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_chart() {
|
||||
let hand = WhiteboardHand::new();
|
||||
let action = WhiteboardAction::DrawChart {
|
||||
chart_type: ChartType::Bar,
|
||||
data: ChartData {
|
||||
labels: vec!["A".to_string(), "B".to_string(), "C".to_string()],
|
||||
datasets: vec![Dataset {
|
||||
label: "Values".to_string(),
|
||||
values: vec![10.0, 20.0, 15.0],
|
||||
color: Some("#2196F3".to_string()),
|
||||
}],
|
||||
},
|
||||
x: 100.0,
|
||||
y: 100.0,
|
||||
width: 400.0,
|
||||
height: 300.0,
|
||||
title: Some("Test Chart".to_string()),
|
||||
};
|
||||
|
||||
let result = hand.execute_action(action).await.unwrap();
|
||||
assert!(result.success);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user