Skip to content

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

refactor(telegram): consume tinychannels helpers

Steven Enamakel2cc0646e2f95
diff --git a/crates/openhuman-core/src/channels/providers/telegram/approval_surface.rs b/crates/openhuman-core/src/channels/providers/telegram/approval_surface.rs
index ca669af3ec..4d57d5bfd2 100644
--- a/crates/openhuman-core/src/channels/providers/telegram/approval_surface.rs
+++ b/crates/openhuman-core/src/channels/providers/telegram/approval_surface.rs
@@ -44,11 +44,8 @@ use std::sync::{Arc, Mutex};
use tinybus::EventHandler;
const LOG_PREFIX: &str = "[telegram-approval]";
-
-/// Identifier the dispatch loop sets as `ApprovalChatContext.client_id`
-/// for Telegram-originated turns. Used by this subscriber to filter
-/// `ApprovalRequested` events down to the ones it should surface.
-pub const TELEGRAM_APPROVAL_CLIENT_ID: &str = "telegram";
+use tinychannels::providers::telegram::format_approval_prompt;
+pub use tinychannels::providers::telegram::TELEGRAM_APPROVAL_CLIENT_ID;
/// Reply context captured from a Telegram inbound message so a later
/// `ApprovalRequested` event can be sent back to the right Telegram chat
@@ -175,15 +172,6 @@ impl TelegramApprovalSurfaceSubscriber {
}
}
-/// Render an approval request as a Telegram message body. Kept as a
-/// free function so tests can pin the exact wording without going
-/// through a real channel.
-pub(crate) fn format_approval_prompt(tool_name: &str, action_summary: &str) -> String {
- format!(
- "πŸ” Approval needed\nTool: `{tool_name}`\nAction: {action_summary}\n\nReply `yes` to approve or `no` to deny."
- )
-}
-
#[async_trait]
impl EventHandler<DomainEvent> for TelegramApprovalSurfaceSubscriber {
fn name(&self) -> &str {
diff --git a/crates/openhuman-core/src/channels/providers/telegram/approval_surface_tests.rs b/crates/openhuman-core/src/channels/providers/telegram/approval_surface_tests.rs
index cd1c9fa8b3..d7ae050748 100644
--- a/crates/openhuman-core/src/channels/providers/telegram/approval_surface_tests.rs
+++ b/crates/openhuman-core/src/channels/providers/telegram/approval_surface_tests.rs
@@ -226,12 +226,3 @@ fn telegram_history_key_matches_format_in_dispatch_helper() {
"telegram_alice_chat-42"
);
}
-
-#[test]
-fn format_approval_prompt_includes_tool_action_and_reply_instructions() {
- let body = format_approval_prompt("git_operations", "git commit -m \"fix\"");
- assert!(body.contains("git_operations"));
- assert!(body.contains("git commit"));
- assert!(body.contains("yes"));
- assert!(body.contains("no"));
-}
diff --git a/crates/openhuman-core/src/channels/providers/telegram/remote_control.rs b/crates/openhuman-core/src/channels/providers/telegram/remote_control.rs
index e3e91aa7e9..a63810dd40 100644
--- a/crates/openhuman-core/src/channels/providers/telegram/remote_control.rs
+++ b/crates/openhuman-core/src/channels/providers/telegram/remote_control.rs
@@ -5,47 +5,16 @@ use crate::channels::context::{
clear_sender_history, conversation_history_key, ChannelRouteSelection, ChannelRuntimeContext,
};
use crate::channels::traits::ChannelMessage;
-use crate::memory::conversations::{
- self as conversations, ConversationThread, CreateConversationThread,
+use crate::memory::conversations::{self as conversations, CreateConversationThread};
+pub use tinychannels::providers::telegram::TelegramRemoteCommand;
+use tinychannels::providers::telegram::{
+ build_new_session_response as render_new_session_response, build_remote_help_response,
+ build_status_response as render_status_response, format_session_line, SESSIONS_LIST_LIMIT,
};
+pub use tinychannels::providers::telegram::parse_telegram_remote_command;
const LOG_PREFIX: &str = "[telegram-remote]";
-pub(crate) const TELEGRAM_CMD_STATUS: &str = "/status";
-pub(crate) const TELEGRAM_CMD_SESSIONS: &str = "/sessions";
-pub(crate) const TELEGRAM_CMD_NEW: &str = "/new";
-pub(crate) const TELEGRAM_CMD_HELP: &str = "/help";
-
-const SESSIONS_LIST_LIMIT: usize = 8;
-
-#[derive(Debug, Clone, PartialEq, Eq)]
-pub enum TelegramRemoteCommand {
- Status,
- Sessions,
- New,
- Help,
-}
-
-pub(crate) fn parse_telegram_remote_command(content: &str) -> Option<TelegramRemoteCommand> {
- let trimmed = content.trim();
- if !trimmed.starts_with('/') {
- return None;
- }
- let command_token = trimmed.split_whitespace().next()?;
- let base = command_token
- .split('@')
- .next()
- .unwrap_or(command_token)
- .to_ascii_lowercase();
- match base.as_str() {
- TELEGRAM_CMD_STATUS => Some(TelegramRemoteCommand::Status),
- TELEGRAM_CMD_SESSIONS => Some(TelegramRemoteCommand::Sessions),
- TELEGRAM_CMD_NEW => Some(TelegramRemoteCommand::New),
- TELEGRAM_CMD_HELP => Some(TelegramRemoteCommand::Help),
- _ => None,
- }
-}
-
pub(crate) async fn build_remote_command_response(
ctx: &ChannelRuntimeContext,
msg: &ChannelMessage,
@@ -60,24 +29,10 @@ pub(crate) async fn build_remote_command_response(
TelegramRemoteCommand::Status => build_status_response(ctx, msg).await,
TelegramRemoteCommand::Sessions => build_sessions_response(ctx, msg).await,
TelegramRemoteCommand::New => build_new_session_response(ctx, msg).await,
- TelegramRemoteCommand::Help => build_help_response(),
+ TelegramRemoteCommand::Help => build_remote_help_response(),
}
}
-fn build_help_response() -> String {
- [
- "OpenHuman Telegram remote control (phase 1):",
- "",
- &format!("β€’ `{TELEGRAM_CMD_STATUS}` β€” active thread, model, and turn state"),
- &format!("β€’ `{TELEGRAM_CMD_SESSIONS}` β€” recent conversation threads"),
- &format!("β€’ `{TELEGRAM_CMD_NEW}` β€” start a fresh thread for this chat"),
- &format!("β€’ `{TELEGRAM_CMD_HELP}` β€” this message"),
- "",
- "Model routing: `/model`, `/models` (same as before).",
- ]
- .join("\n")
-}
-
fn route_for_sender(ctx: &ChannelRuntimeContext, sender_key: &str) -> ChannelRouteSelection {
ctx.route_overrides
.lock()
@@ -139,17 +94,12 @@ async fn build_status_response(ctx: &ChannelRuntimeContext, msg: &ChannelMessage
None => "Thread: _(none β€” send `/new` to bind a thread)_".to_string(),
};
- let turn_state = if busy { "in progress ⏳" } else { "idle" };
-
- format!(
- "**Status**\n\
- {thread_line}\n\
- Provider: `{provider}`\n\
- Model: `{model}`\n\
- In-memory turns: {history_len}\n\
- Turn: {turn_state}",
- provider = route.provider,
- model = route.model,
+ render_status_response(
+ &thread_line,
+ &route.provider,
+ &route.model,
+ history_len,
+ busy,
)
}
@@ -190,30 +140,17 @@ async fn build_sessions_response(ctx: &ChannelRuntimeContext, msg: &ChannelMessa
];
for thread in sorted.into_iter().take(SESSIONS_LIST_LIMIT) {
- lines.push(format_session_line(&thread, active_thread_id.as_deref()));
+ lines.push(format_session_line(
+ &thread.title,
+ &thread.id,
+ thread.message_count,
+ active_thread_id.as_deref() == Some(thread.id.as_str()),
+ ));
}
lines.join("\n")
}
-fn format_session_line(thread: &ConversationThread, active_id: Option<&str>) -> String {
- let marker = if active_id == Some(thread.id.as_str()) {
- "β†’ "
- } else {
- " "
- };
- let title = if thread.title.trim().is_empty() {
- thread.id.as_str()
- } else {
- thread.title.as_str()
- };
- format!(
- "{marker}`{title}` β€” {count} msgs (id: `{id}`)",
- count = thread.message_count,
- id = thread.id,
- )
-}
-
async fn build_new_session_response(ctx: &ChannelRuntimeContext, msg: &ChannelMessage) -> String {
let workspace = ctx.workspace_dir.as_path();
let sender_key = conversation_history_key(msg);
@@ -277,13 +214,5 @@ async fn build_new_session_response(ctx: &ChannelRuntimeContext, msg: &ChannelMe
msg.reply_target
);
- format!(
- "Started new session **{title}**.\n\
- Thread id: `{thread_id}`\n\
- In-memory channel history cleared for this chat."
- )
+ render_new_session_response(&title, &thread_id)
}
-
-#[cfg(test)]
-#[path = "remote_control_tests.rs"]
-mod tests;
diff --git a/crates/openhuman-core/src/channels/providers/telegram/remote_control_tests.rs b/crates/openhuman-core/src/channels/providers/telegram/remote_control_tests.rs
deleted file mode 100644
index 08035bb1e1..0000000000
--- a/crates/openhuman-core/src/channels/providers/telegram/remote_control_tests.rs
+++ /dev/null
@@ -1,41 +0,0 @@
-use super::*;
-
-#[test]
-fn parse_remote_commands() {
- assert_eq!(
- parse_telegram_remote_command("/status"),
- Some(TelegramRemoteCommand::Status)
- );
- assert_eq!(
- parse_telegram_remote_command("/status@MyBot"),
- Some(TelegramRemoteCommand::Status)
- );
- assert_eq!(
- parse_telegram_remote_command(" /sessions "),
- Some(TelegramRemoteCommand::Sessions)
- );
- assert_eq!(
- parse_telegram_remote_command("/new"),
- Some(TelegramRemoteCommand::New)
- );
- assert_eq!(
- parse_telegram_remote_command("/help"),
- Some(TelegramRemoteCommand::Help)
- );
- assert_eq!(
- parse_telegram_remote_command(" /STATUS@OpenHumanBot now "),
- Some(TelegramRemoteCommand::Status)
- );
- // Case insensitivity for other variants
- assert_eq!(
- parse_telegram_remote_command("/Sessions"),
- Some(TelegramRemoteCommand::Sessions)
- );
- assert_eq!(
- parse_telegram_remote_command("/NEW@Bot"),
- Some(TelegramRemoteCommand::New)
- );
- assert!(parse_telegram_remote_command("hello").is_none());
- assert!(parse_telegram_remote_command("/model").is_none());
- assert!(parse_telegram_remote_command("/unknown@OpenHumanBot").is_none());
-}
diff --git a/vendor/tinychannels b/vendor/tinychannels
index 1c287610a5..fef8fe1667 160000
--- a/vendor/tinychannels
+++ b/vendor/tinychannels
@@ -1 +1 @@
-Subproject commit 1c287610a56fda151a0b33d9d6dff6aace863b4d
+Subproject commit fef8fe16673a6e3cf08dd325fe18c4b4ceb4367a