Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

test(cli): cover telegram voice notes through scoped stt and mirror tts

Steven Enamakel37536fcf4a1f
diff --git a/crates/openhuman-cli/Cargo.toml b/crates/openhuman-cli/Cargo.toml
index 9aaf12f6bb..4211377277 100644
--- a/crates/openhuman-cli/Cargo.toml
+++ b/crates/openhuman-cli/Cargo.toml
@@ -145,7 +145,7 @@ path = "../../tests/storage_scope_e2e.rs"
name = "native_channel_session_restart_e2e"
path = "../../tests/native_channel_session_restart_e2e.rs"
# Boots the CLI's real native Telegram listener via a compiled TinyBus artifact.
-required-features = ["modules", "inference", "channels", "storage-sqlite"]
+required-features = ["modules", "inference", "channels", "storage-sqlite", "voice"]
[[test]]
# Its own binary: two agents on one backend keep cron jobs and flows apart.
diff --git a/tests/native_channel_session_restart_e2e.rs b/tests/native_channel_session_restart_e2e.rs
index 5756096c65..96e182913d 100644
--- a/tests/native_channel_session_restart_e2e.rs
+++ b/tests/native_channel_session_restart_e2e.rs
@@ -17,6 +17,7 @@ use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use axum::extract::State;
+use axum::body::Bytes;
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use axum::routing::{get, post};
@@ -40,6 +41,9 @@ struct MockState {
update_offsets: Mutex<Vec<i64>>,
completions: Mutex<Vec<Value>>,
sent_messages: Mutex<Vec<Value>>,
+ transcription_uploads: Mutex<Vec<usize>>,
+ speech_requests: Mutex<Vec<String>>,
+ sent_voice_uploads: Mutex<Vec<usize>>,
}
struct MockServers {
@@ -62,12 +66,17 @@ async fn start_mock_servers() -> MockServers {
.route("/botsession-restart-token/sendChatAction", post(ok))
.route("/botsession-restart-token/deleteWebhook", post(ok))
.route("/botsession-restart-token/getMe", get(get_me))
+ .route("/botsession-restart-token/getFile", post(get_voice_file))
+ .route("/file/botsession-restart-token/voice/input.oga", get(download_voice_file))
+ .route("/botsession-restart-token/sendVoice", post(send_voice))
.with_state(Arc::clone(&state));
let inference_app = Router::new()
.route("/v1/chat/completions", post(chat_completion))
.route("/v1/models", get(models))
.route("/auth/me", get(auth_me))
.route("/settings", get(auth_me))
+ .route("/openai/v1/audio/transcriptions", post(transcribe_voice))
+ .route("/openai/v1/audio/speech", post(synthesize_voice))
.with_state(Arc::clone(&state));
let (telegram, telegram_task) = serve(telegram_app).await;
let (inference, inference_task) = serve(inference_app).await;
@@ -144,6 +153,35 @@ async fn get_me() -> Json<Value> {
Json(json!({ "ok": true, "result": { "id": 9, "is_bot": true, "username": "test_bot" } }))
}
+async fn get_voice_file() -> Json<Value> {
+ Json(json!({ "ok": true, "result": { "file_path": "voice/input.oga", "file_size": 2575 } }))
+}
+
+async fn download_voice_file() -> impl IntoResponse {
+ ([(axum::http::header::CONTENT_TYPE, "audio/ogg")], include_bytes!("../vendor/tinychannels/src/providers/telegram/fixtures/sine-ogg-opus.ogg").as_slice())
+}
+
+async fn send_voice(State(state): State<Arc<MockState>>, body: Bytes) -> Json<Value> {
+ state.sent_voice_uploads.lock().expect("voice uploads").push(body.len());
+ Json(json!({ "ok": true, "result": { "message_id": 700, "chat": { "id": 42 } } }))
+}
+
+async fn transcribe_voice(State(state): State<Arc<MockState>>, body: Bytes) -> Json<Value> {
+ state.transcription_uploads.lock().expect("STT uploads").push(body.len());
+ Json(json!({ "text": "the dictated voice request" }))
+}
+
+async fn synthesize_voice(State(state): State<Arc<MockState>>, Json(body): Json<Value>) -> Json<Value> {
+ state.speech_requests.lock().expect("TTS requests").push(
+ body.get("text").and_then(Value::as_str).unwrap_or_default().to_owned(),
+ );
+ let audio = base64::Engine::encode(
+ &base64::engine::general_purpose::STANDARD,
+ include_bytes!("../vendor/tinychannels/src/providers/telegram/fixtures/sine-ogg-opus.ogg"),
+ );
+ Json(json!({ "audio_base64": audio, "audio_mime": "audio/ogg" }))
+}
+
async fn chat_completion(State(state): State<Arc<MockState>>, Json(body): Json<Value>) -> Response {
let index = {
let mut completions = state.completions.lock().expect("model requests");
@@ -195,6 +233,19 @@ fn message_update_for(update_id: i64, message_id: i64, chat_id: i64, text: &str)
})
}
+fn voice_update(update_id: i64, message_id: i64) -> Value {
+ json!({
+ "update_id": update_id,
+ "message": {
+ "message_id": message_id,
+ "date": 1_800_000_000,
+ "from": { "id": 42, "is_bot": false, "first_name": "Ada" },
+ "chat": { "id": 42, "type": "private", "first_name": "Ada" },
+ "voice": { "file_id": "fixture-voice-file", "file_unique_id": "fixture-voice", "duration": 1, "mime_type": "audio/ogg" }
+ }
+ })
+}
+
fn push_update(state: &MockState, update_id: i64, message_id: i64, text: &str) {
state
.updates
@@ -238,6 +289,9 @@ bot_token = "{BOT_TOKEN}"
allowed_users = ["42", "43"]
mention_only = false
+[channel_voice]
+reply_mode = "mirror"
+
[[cloud_providers]]
id = "p_session_restart"
slug = "mock"
@@ -296,6 +350,7 @@ fn start_core(
inference: &str,
module_source_dir: &Path,
log_path: &Path,
+ voice_enabled: bool,
) -> (CoreProcess, String) {
let port = std::net::TcpListener::bind("127.0.0.1:0")
.expect("reserve core port")
@@ -348,6 +403,7 @@ fn start_core(
]
.map(|name| (format!("OPENHUMAN_COMPONENT_{name}"), "0")),
)
+ .env("OPENHUMAN_COMPONENT_VOICE", if voice_enabled { "1" } else { "0" })
.stdout(Stdio::from(stdout))
.stderr(Stdio::from(stderr))
.spawn()
@@ -508,6 +564,7 @@ async fn native_telegram_session_resumes_after_the_core_process_restarts() {
&mocks.inference,
&module_source_dir,
&logs.join("first.log"),
+ false,
);
wait_healthy(&first_rpc, &mut first, &logs.join("first.log")).await;
let first_pid = first.child.id();
@@ -597,6 +654,7 @@ async fn native_telegram_session_resumes_after_the_core_process_restarts() {
&mocks.inference,
&module_source_dir,
&logs.join("second.log"),
+ false,
);
assert_ne!(
first_pid,
@@ -647,3 +705,47 @@ async fn native_telegram_session_resumes_after_the_core_process_restarts() {
wait_for_completion(&mocks.state, 5).await;
second.stop();
}
+
+#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
+async fn telegram_voice_note_runs_scoped_stt_agent_and_mirror_tts_through_modules() {
+ let tmp = tempfile::tempdir().expect("temporary core home");
+ let home = std::fs::canonicalize(tmp.path()).expect("canonicalize temporary home");
+ let workspace = home.join("workspace");
+ let logs = home.join("logs");
+ std::fs::create_dir_all(&logs).expect("create logs");
+ let module_source_dir = std::env::var_os("OPENHUMAN_TEST_MODULE_SOURCE_DIR")
+ .map(std::path::PathBuf::from)
+ .expect("build TinyChannels and TinyVoice and set OPENHUMAN_TEST_MODULE_SOURCE_DIR");
+ for module in ["tinychannels", "tinyvoice"] {
+ assert!(module_source_dir.join(module).join("modules.toml").is_file(),
+ "compiled {module} descriptor missing from {}", module_source_dir.display());
+ }
+
+ let mocks = start_mock_servers().await;
+ write_config(&home, &workspace, &mocks.inference);
+ let (mut core, rpc) = start_core(
+ &home, &workspace, &mocks.telegram, &mocks.inference,
+ &module_source_dir, &logs.join("voice.log"),
+ true,
+ );
+ wait_healthy(&rpc, &mut core, &logs.join("voice.log")).await;
+ mocks.state.updates.lock().expect("Telegram updates")
+ .push_back(voice_update(200, 20));
+
+ wait_until("voice note reaches inference and sendVoice", || {
+ !mocks.state.completions.lock().expect("model requests").is_empty()
+ && !mocks.state.sent_voice_uploads.lock().expect("voice uploads").is_empty()
+ }).await;
+
+ let requests = mocks.state.completions.lock().expect("model requests").clone();
+ assert_eq!(requests.len(), 1, "one inbound voice update produces one agent turn");
+ assert!(model_saw_user_text(&requests[0], "the dictated voice request"),
+ "the model receives the transcript produced by the scoped cloud STT callback");
+ assert_eq!(mocks.state.transcription_uploads.lock().expect("STT uploads").len(), 1,
+ "Telegram voice media is downloaded and transcribed once");
+ assert!(!mocks.state.transcription_uploads.lock().expect("STT uploads")[0].eq(&0));
+ assert_eq!(mocks.state.speech_requests.lock().expect("TTS requests").as_slice(), &["mock reply 1"]);
+ assert!(mocks.state.sent_voice_uploads.lock().expect("voice uploads")[0] > 0,
+ "mirror mode uploads synthesized audio through Telegram sendVoice");
+ core.stop();
+}